3 * Abstract Syntax Notation One (ISO 8824, 8825) decoding
5 * @todo not optimised (yet), favor correctness over speed, favor speed over size
9 * Copyright (c) 2006 Axon Digital Design B.V., The Netherlands.
10 * All rights reserved.
12 * Redistribution and use in source and binary forms, with or without modification,
13 * are permitted provided that the following conditions are met:
15 * 1. Redistributions of source code must retain the above copyright notice,
16 * this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright notice,
18 * this list of conditions and the following disclaimer in the documentation
19 * and/or other materials provided with the distribution.
20 * 3. The name of the author may not be used to endorse or promote products
21 * derived from this software without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
24 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
25 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
26 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
27 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
28 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
31 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
34 * Author: Christiaan Simons <christiaan.simons@axon.tv>
39 #if LWIP_SNMP /* don't build if not configured for use in lwipopts.h */
41 #include "lwip/snmp_asn1.h"
44 * Retrieves type field from incoming pbuf chain.
46 * @param p points to a pbuf holding an ASN1 coded type field
47 * @param ofs points to the offset within the pbuf chain of the ASN1 coded type field
48 * @param type return ASN1 type
49 * @return ERR_OK if successfull, ERR_ARG if we can't (or won't) decode
52 snmp_asn1_dec_type(struct pbuf *p, u16_t ofs, u8_t *type)
65 msg_ptr += ofs - base;
71 /* p == NULL, ofs >= plen */
76 * Decodes length field from incoming pbuf chain into host length.
78 * @param p points to a pbuf holding an ASN1 coded length
79 * @param ofs points to the offset within the pbuf chain of the ASN1 coded length
80 * @param octets_used returns number of octets used by the length code
81 * @param length return host order length, upto 64k
82 * @return ERR_OK if successfull, ERR_ARG if we can't (or won't) decode
85 snmp_asn1_dec_length(struct pbuf *p, u16_t ofs, u8_t *octets_used, u16_t *length)
98 msg_ptr += ofs - base;
102 /* primitive definite length format */
107 else if (*msg_ptr == 0x80)
109 /* constructed indefinite length format, termination with two zero octets */
125 /* next octet in next pbuf */
127 if (p == NULL) { return ERR_ARG; }
128 msg_ptr = p->payload;
133 /* next octet in same pbuf */
141 /* stop while (i > 0) */
154 else if (*msg_ptr == 0x81)
156 /* constructed definite length format, one octet */
160 /* next octet in next pbuf */
162 if (p == NULL) { return ERR_ARG; }
163 msg_ptr = p->payload;
167 /* next octet in same pbuf */
174 else if (*msg_ptr == 0x82)
178 /* constructed definite length format, two octets */
186 /* next octet in next pbuf */
188 if (p == NULL) { return ERR_ARG; }
189 msg_ptr = p->payload;
194 /* next octet in same pbuf */
199 /* least significant length octet */
204 /* most significant length octet */
205 *length = (*msg_ptr) << 8;
213 /* constructed definite length format 3..127 octets, this is too big (>64k) */
214 /** @todo: do we need to accept inefficient codings with many leading zero's? */
215 *octets_used = 1 + ((*msg_ptr) & 0x7f);
222 /* p == NULL, ofs >= plen */
227 * Decodes positive integer (counter, gauge, timeticks) into u32_t.
229 * @param p points to a pbuf holding an ASN1 coded integer
230 * @param ofs points to the offset within the pbuf chain of the ASN1 coded integer
231 * @param len length of the coded integer field
232 * @param value return host order integer
233 * @return ERR_OK if successfull, ERR_ARG if we can't (or won't) decode
235 * @note ASN coded integers are _always_ signed. E.g. +0xFFFF is coded
236 * as 0x00,0xFF,0xFF. Note the leading sign octet. A positive value
237 * of 0xFFFFFFFF is preceded with 0x00 and the length is 5 octets!!
240 snmp_asn1_dec_u32t(struct pbuf *p, u16_t ofs, u16_t len, u32_t *value)
252 msg_ptr = p->payload;
253 msg_ptr += ofs - base;
254 if ((len > 0) && (len < 6))
256 /* start from zero */
260 /* negative, expecting zero sign bit! */
266 if ((len > 1) && (*msg_ptr == 0))
268 /* skip leading "sign byte" octet 0x00 */
273 /* next octet in next pbuf */
275 if (p == NULL) { return ERR_ARG; }
276 msg_ptr = p->payload;
281 /* next octet in same pbuf */
286 /* OR octets with value */
295 /* next octet in next pbuf */
297 if (p == NULL) { return ERR_ARG; }
298 msg_ptr = p->payload;
303 /* next octet in same pbuf */
317 /* p == NULL, ofs >= plen */
322 * Decodes integer into s32_t.
324 * @param p points to a pbuf holding an ASN1 coded integer
325 * @param ofs points to the offset within the pbuf chain of the ASN1 coded integer
326 * @param len length of the coded integer field
327 * @param value return host order integer
328 * @return ERR_OK if successfull, ERR_ARG if we can't (or won't) decode
330 * @note ASN coded integers are _always_ signed!
333 snmp_asn1_dec_s32t(struct pbuf *p, u16_t ofs, u16_t len, s32_t *value)
337 #if BYTE_ORDER == LITTLE_ENDIAN
338 u8_t *lsb_ptr = (u8_t*)value;
340 #if BYTE_ORDER == BIG_ENDIAN
341 u8_t *lsb_ptr = (u8_t*)value + sizeof(s32_t) - 1;
352 msg_ptr = p->payload;
353 msg_ptr += ofs - base;
354 if ((len > 0) && (len < 5))
358 /* negative, start from -1 */
364 /* positive, start from 0 */
368 /* OR/AND octets with value */
374 *lsb_ptr &= *msg_ptr;
380 *lsb_ptr |= *msg_ptr;
386 /* next octet in next pbuf */
388 if (p == NULL) { return ERR_ARG; }
389 msg_ptr = p->payload;
394 /* next octet in same pbuf */
400 *lsb_ptr &= *msg_ptr;
404 *lsb_ptr |= *msg_ptr;
415 /* p == NULL, ofs >= plen */
420 * Decodes object identifier from incoming message into array of s32_t.
422 * @param p points to a pbuf holding an ASN1 coded object identifier
423 * @param ofs points to the offset within the pbuf chain of the ASN1 coded object identifier
424 * @param len length of the coded object identifier
425 * @param oid return object identifier struct
426 * @return ERR_OK if successfull, ERR_ARG if we can't (or won't) decode
429 snmp_asn1_dec_oid(struct pbuf *p, u16_t ofs, u16_t len, struct snmp_obj_id *oid)
442 msg_ptr = p->payload;
443 msg_ptr += ofs - base;
446 oid_ptr = &oid->id[0];
449 /* first compressed octet */
450 if (*msg_ptr == 0x2B)
452 /* (most) common case 1.3 (iso.org) */
458 else if (*msg_ptr < 40)
465 else if (*msg_ptr < 80)
469 *oid_ptr = (*msg_ptr) - 40;
476 *oid_ptr = (*msg_ptr) - 80;
483 /* accepting zero length identifiers e.g. for
484 getnext operation. uncommon but valid */
493 /* next octet in next pbuf */
495 if (p == NULL) { return ERR_ARG; }
496 msg_ptr = p->payload;
501 /* next octet in same pbuf */
505 while ((len > 0) && (oid->len < LWIP_SNMP_OBJ_ID_LEN))
507 /* sub-identifier uses multiple octets */
512 while ((*msg_ptr & 0x80) && (len > 1))
515 sub_id = (sub_id << 7) + (*msg_ptr & ~0x80);
519 /* next octet in next pbuf */
521 if (p == NULL) { return ERR_ARG; }
522 msg_ptr = p->payload;
527 /* next octet in same pbuf */
531 if (!(*msg_ptr & 0x80) && (len > 0))
533 /* last octet sub-identifier */
535 sub_id = (sub_id << 7) + *msg_ptr;
541 /* !(*msg_ptr & 0x80) sub-identifier uses single octet */
547 /* remaining oid bytes available ... */
551 /* next octet in next pbuf */
553 if (p == NULL) { return ERR_ARG; }
554 msg_ptr = p->payload;
559 /* next octet in same pbuf */
568 /* len == 0, end of oid */
573 /* len > 0, oid->len == LWIP_SNMP_OBJ_ID_LEN or malformed encoding */
580 /* p == NULL, ofs >= plen */
585 * Decodes (copies) raw data (ip-addresses, octet strings, opaque encoding)
586 * from incoming message into array.
588 * @param p points to a pbuf holding an ASN1 coded raw data
589 * @param ofs points to the offset within the pbuf chain of the ASN1 coded raw data
590 * @param len length of the coded raw data (zero is valid, e.g. empty string!)
591 * @param raw_len length of the raw return value
592 * @param raw return raw bytes
593 * @return ERR_OK if successfull, ERR_ARG if we can't (or won't) decode
596 snmp_asn1_dec_raw(struct pbuf *p, u16_t ofs, u16_t len, u16_t raw_len, u8_t *raw)
610 msg_ptr = p->payload;
611 msg_ptr += ofs - base;
616 /* copy len - 1 octets */
623 /* next octet in next pbuf */
625 if (p == NULL) { return ERR_ARG; }
626 msg_ptr = p->payload;
631 /* next octet in same pbuf */
635 /* copy last octet */
641 /* raw_len < len, not enough dst space */
647 /* p == NULL, ofs >= plen */
652 /* len == 0, empty string */
657 #endif /* LWIP_SNMP */