3 * Abstract Syntax Notation One (ISO 8824, 8825) encoding
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 * Returns octet count for length.
47 * @param octets_needed points to the return value
50 snmp_asn1_enc_length_cnt(u16_t length, u8_t *octets_needed)
56 else if (length < 0x100U)
67 * Returns octet count for an u32_t.
70 * @param octets_needed points to the return value
72 * @note ASN coded integers are _always_ signed. E.g. +0xFFFF is coded
73 * as 0x00,0xFF,0xFF. Note the leading sign octet. A positive value
74 * of 0xFFFFFFFF is preceded with 0x00 and the length is 5 octets!!
77 snmp_asn1_enc_u32t_cnt(u32_t value, u16_t *octets_needed)
83 else if (value < 0x8000UL)
87 else if (value < 0x800000UL)
91 else if (value < 0x80000000UL)
102 * Returns octet count for an s32_t.
105 * @param octets_needed points to the return value
107 * @note ASN coded integers are _always_ signed.
110 snmp_asn1_enc_s32t_cnt(s32_t value, u16_t *octets_needed)
120 else if (value < 0x8000L)
124 else if (value < 0x800000L)
135 * Returns octet count for an object identifier.
137 * @param ident_len object identifier array length
138 * @param ident points to object identifier array
139 * @param octets_needed points to the return value
142 snmp_asn1_enc_oid_cnt(u8_t ident_len, s32_t *ident, u16_t *octets_needed)
150 /* compressed prefix in one octet */
169 *octets_needed = cnt;
173 * Encodes ASN type field into a pbuf chained ASN1 msg.
175 * @param p points to output pbuf to encode value into
176 * @param ofs points to the offset within the pbuf chain
177 * @param type input ASN1 type
178 * @return ERR_OK if successfull, ERR_ARG if we can't (or won't) encode
181 snmp_asn1_enc_type(struct pbuf *p, u16_t ofs, u8_t type)
193 msg_ptr = p->payload;
194 msg_ptr += ofs - base;
200 /* p == NULL, ofs >= plen */
205 * Encodes host order length field into a pbuf chained ASN1 msg.
207 * @param p points to output pbuf to encode length into
208 * @param ofs points to the offset within the pbuf chain
209 * @param length is the host order length to be encoded
210 * @return ERR_OK if successfull, ERR_ARG if we can't (or won't) encode
213 snmp_asn1_enc_length(struct pbuf *p, u16_t ofs, u16_t length)
225 msg_ptr = p->payload;
226 msg_ptr += ofs - base;
233 else if (length < 0x100)
239 /* next octet in next pbuf */
241 if (p == NULL) { return ERR_ARG; }
242 msg_ptr = p->payload;
246 /* next octet in same pbuf */
256 /* length >= 0x100 && length <= 0xFFFF */
265 /* next octet in next pbuf */
267 if (p == NULL) { return ERR_ARG; }
268 msg_ptr = p->payload;
273 /* next octet in same pbuf */
278 /* least significant length octet */
283 /* most significant length octet */
284 *msg_ptr = length >> 8;
292 /* p == NULL, ofs >= plen */
297 * Encodes u32_t (counter, gauge, timeticks) into a pbuf chained ASN1 msg.
299 * @param p points to output pbuf to encode value into
300 * @param ofs points to the offset within the pbuf chain
301 * @param octets_needed encoding length (from snmp_asn1_enc_u32t_cnt())
302 * @param value is the host order u32_t value to be encoded
303 * @return ERR_OK if successfull, ERR_ARG if we can't (or won't) encode
305 * @see snmp_asn1_enc_u32t_cnt()
308 snmp_asn1_enc_u32t(struct pbuf *p, u16_t ofs, u8_t octets_needed, u32_t value)
320 msg_ptr = p->payload;
321 msg_ptr += ofs - base;
323 if (octets_needed == 5)
325 /* not enough bits in 'value' add leading 0x00 */
331 /* next octet in next pbuf */
333 if (p == NULL) { return ERR_ARG; }
334 msg_ptr = p->payload;
339 /* next octet in same pbuf */
343 while (octets_needed > 1)
346 *msg_ptr = value >> (octets_needed << 3);
350 /* next octet in next pbuf */
352 if (p == NULL) { return ERR_ARG; }
353 msg_ptr = p->payload;
358 /* next octet in same pbuf */
362 /* (only) one least significant octet */
368 /* p == NULL, ofs >= plen */
373 * Encodes s32_t integer into a pbuf chained ASN1 msg.
375 * @param p points to output pbuf to encode value into
376 * @param ofs points to the offset within the pbuf chain
377 * @param octets_needed encoding length (from snmp_asn1_enc_s32t_cnt())
378 * @param value is the host order s32_t value to be encoded
379 * @return ERR_OK if successfull, ERR_ARG if we can't (or won't) encode
381 * @see snmp_asn1_enc_s32t_cnt()
384 snmp_asn1_enc_s32t(struct pbuf *p, u16_t ofs, u8_t octets_needed, s32_t value)
396 msg_ptr = p->payload;
397 msg_ptr += ofs - base;
399 while (octets_needed > 1)
402 *msg_ptr = value >> (octets_needed << 3);
406 /* next octet in next pbuf */
408 if (p == NULL) { return ERR_ARG; }
409 msg_ptr = p->payload;
414 /* next octet in same pbuf */
418 /* (only) one least significant octet */
424 /* p == NULL, ofs >= plen */
429 * Encodes object identifier into a pbuf chained ASN1 msg.
431 * @param p points to output pbuf to encode oid into
432 * @param ofs points to the offset within the pbuf chain
433 * @param ident_len object identifier array length
434 * @param ident points to object identifier array
435 * @return ERR_OK if successfull, ERR_ARG if we can't (or won't) encode
438 snmp_asn1_enc_oid(struct pbuf *p, u16_t ofs, u8_t ident_len, s32_t *ident)
450 msg_ptr = p->payload;
451 msg_ptr += ofs - base;
455 if ((ident[0] == 1) && (ident[1] == 3))
457 /* compressed (most common) prefix .iso.org */
462 /* calculate prefix */
463 *msg_ptr = (ident[0] * 40) + ident[1];
468 /* next octet in next pbuf */
470 if (p == NULL) { return ERR_ARG; }
471 msg_ptr = p->payload;
476 /* next octet in same pbuf */
484 /* @bug: allow empty varbinds for symmetry (we must decode them for getnext), allow partial compression?? */
485 /* ident_len <= 1, at least we need zeroDotZero (0.0) (ident_len == 2) */
488 while (ident_len > 0)
501 code = sub_id >> shift;
502 if ((code != 0) || (tail != 0))
505 *msg_ptr = code | 0x80;
509 /* next octet in next pbuf */
511 if (p == NULL) { return ERR_ARG; }
512 msg_ptr = p->payload;
517 /* next octet in same pbuf */
523 *msg_ptr = (u8_t)sub_id & 0x7F;
529 /* next octet in next pbuf */
531 if (p == NULL) { return ERR_ARG; }
532 msg_ptr = p->payload;
537 /* next octet in same pbuf */
541 /* proceed to next sub-identifier */
548 /* p == NULL, ofs >= plen */
553 * Encodes raw data (octet string, opaque) into a pbuf chained ASN1 msg.
555 * @param p points to output pbuf to encode raw data into
556 * @param ofs points to the offset within the pbuf chain
557 * @param raw_len raw data length
558 * @param raw points raw data
559 * @return ERR_OK if successfull, ERR_ARG if we can't (or won't) encode
562 snmp_asn1_enc_raw(struct pbuf *p, u16_t ofs, u8_t raw_len, u8_t *raw)
574 msg_ptr = p->payload;
575 msg_ptr += ofs - base;
579 /* copy raw_len - 1 octets */
586 /* next octet in next pbuf */
588 if (p == NULL) { return ERR_ARG; }
589 msg_ptr = p->payload;
594 /* next octet in same pbuf */
600 /* copy last or single octet */
607 /* p == NULL, ofs >= plen */
611 #endif /* LWIP_SNMP */