📄 asn1_dec.c
字号:
/** * @file * Abstract Syntax Notation One (ISO 8824, 8825) decoding * * @todo not optimised (yet), favor correctness over speed, favor speed over size *//* * Copyright (c) 2006 Axon Digital Design B.V., The Netherlands. * All rights reserved. * * Redistribution and use in source and binary forms, with or without modification, * are permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * 3. The name of the author may not be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY * OF SUCH DAMAGE. * * Author: Christiaan Simons <christiaan.simons@axon.tv> */#include "lwip/opt.h"#if LWIP_SNMP#include "lwip/snmp_asn1.h"/** * Retrieves type field from incoming pbuf chain. * * @param p points to a pbuf holding an ASN1 coded type field * @param ofs points to the offset within the pbuf chain of the ASN1 coded type field * @param type return ASN1 type * @return ERR_OK if successfull, ERR_ARG if we can't (or won't) decode */err_tsnmp_asn1_dec_type(struct pbuf *p, u16_t ofs, u8_t *type){ u16_t plen, base; u8_t *msg_ptr; plen = 0; while (p != NULL) { base = plen; plen += p->len; if (ofs < plen) { msg_ptr = p->payload; msg_ptr += ofs - base; *type = *msg_ptr; return ERR_OK; } p = p->next; } /* p == NULL, ofs >= plen */ return ERR_ARG;}/** * Decodes length field from incoming pbuf chain into host length. * * @param p points to a pbuf holding an ASN1 coded length * @param ofs points to the offset within the pbuf chain of the ASN1 coded length * @param octets_used returns number of octets used by the length code * @param length return host order length, upto 64k * @return ERR_OK if successfull, ERR_ARG if we can't (or won't) decode */err_tsnmp_asn1_dec_length(struct pbuf *p, u16_t ofs, u8_t *octets_used, u16_t *length){ u16_t plen, base; u8_t *msg_ptr; plen = 0; while (p != NULL) { base = plen; plen += p->len; if (ofs < plen) { msg_ptr = p->payload; msg_ptr += ofs - base; if (*msg_ptr < 0x80) { /* primitive definite length format */ *octets_used = 1; *length = *msg_ptr; return ERR_OK; } else if (*msg_ptr == 0x80) { /* constructed indefinite length format, termination with two zero octets */ u8_t zeros; u8_t i; *length = 0; zeros = 0; while (zeros != 2) { i = 2; while (i > 0) { i--; (*length) += 1; ofs += 1; if (ofs >= plen) { /* next octet in next pbuf */ p = p->next; if (p == NULL) { return ERR_ARG; } msg_ptr = p->payload; plen += p->len; } else { /* next octet in same pbuf */ msg_ptr++; } if (*msg_ptr == 0) { zeros++; if (zeros == 2) { /* stop while (i > 0) */ i = 0; } } else { zeros = 0; } } } *octets_used = 1; return ERR_OK; } else if (*msg_ptr == 0x81) { /* constructed definite length format, one octet */ ofs += 1; if (ofs >= plen) { /* next octet in next pbuf */ p = p->next; if (p == NULL) { return ERR_ARG; } msg_ptr = p->payload; } else { /* next octet in same pbuf */ msg_ptr++; } *length = *msg_ptr; *octets_used = 2; return ERR_OK; } else if (*msg_ptr == 0x82) { u8_t i; /* constructed definite length format, two octets */ i = 2; while (i > 0) { i--; ofs += 1; if (ofs >= plen) { /* next octet in next pbuf */ p = p->next; if (p == NULL) { return ERR_ARG; } msg_ptr = p->payload; plen += p->len; } else { /* next octet in same pbuf */ msg_ptr++; } if (i == 0) { /* least significant length octet */ *length |= *msg_ptr; } else { /* most significant length octet */ *length = (*msg_ptr) << 8; } } *octets_used = 3; return ERR_OK; } else { /* constructed definite length format 3..127 octets, this is too big (>64k) */ /** @todo: do we need to accept inefficient codings with many leading zero's? */ *octets_used = 1 + ((*msg_ptr) & 0x7f); return ERR_ARG; } } p = p->next; } /* p == NULL, ofs >= plen */ return ERR_ARG;}/** * Decodes positive integer (counter, gauge, timeticks) into u32_t. * * @param p points to a pbuf holding an ASN1 coded integer * @param ofs points to the offset within the pbuf chain of the ASN1 coded integer * @param len length of the coded integer field * @param value return host order integer * @return ERR_OK if successfull, ERR_ARG if we can't (or won't) decode * * @note ASN coded integers are _always_ signed. E.g. +0xFFFF is coded * as 0x00,0xFF,0xFF. Note the leading sign octet. A positive value * of 0xFFFFFFFF is preceded with 0x00 and the length is 5 octets!! */err_tsnmp_asn1_dec_u32t(struct pbuf *p, u16_t ofs, u16_t len, u32_t *value){ u16_t plen, base; u8_t *msg_ptr; plen = 0; while (p != NULL) { base = plen; plen += p->len; if (ofs < plen) { msg_ptr = p->payload; msg_ptr += ofs - base; if ((len > 0) && (len < 6)) { /* start from zero */ *value = 0; if (*msg_ptr & 0x80) { /* negative, expecting zero sign bit! */ return ERR_ARG; } else { /* positive */ if ((len > 1) && (*msg_ptr == 0)) { /* skip leading "sign byte" octet 0x00 */ len--; ofs += 1; if (ofs >= plen) { /* next octet in next pbuf */ p = p->next; if (p == NULL) { return ERR_ARG; } msg_ptr = p->payload; plen += p->len; } else { /* next octet in same pbuf */ msg_ptr++; } } } /* OR octets with value */ while (len > 1) { len--; *value |= *msg_ptr; *value <<= 8; ofs += 1; if (ofs >= plen) { /* next octet in next pbuf */ p = p->next; if (p == NULL) { return ERR_ARG; } msg_ptr = p->payload; plen += p->len; } else { /* next octet in same pbuf */ msg_ptr++; } } *value |= *msg_ptr; return ERR_OK; } else { return ERR_ARG; } } p = p->next; } /* p == NULL, ofs >= plen */ return ERR_ARG;}/** * Decodes integer into s32_t. * * @param p points to a pbuf holding an ASN1 coded integer * @param ofs points to the offset within the pbuf chain of the ASN1 coded integer * @param len length of the coded integer field * @param value return host order integer * @return ERR_OK if successfull, ERR_ARG if we can't (or won't) decode
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -