5 * Encode BER length octets. If exact, lenlen is the exact desired
6 * encoding size, else, lenlen is the max available space. Len < 0 =
8 * Returns: >0 success, number of bytes encoded.
9 * Returns: =0 success, indefinite start-marker set. 1 byte encoded.
10 * Returns: -1 failure, out of bounds.
12 int ber_enclen(ODR o, int len, int lenlen, int exact)
14 unsigned char octs[sizeof(int)];
19 fprintf(stderr, "[len=%d]", len);
21 if (len < 0) /* Indefinite */
23 if (odr_putc(o, 0x80) < 0)
26 fprintf(stderr, "[indefinite]");
30 if (len <= 127 && (lenlen == 1 || !exact)) /* definite short form */
32 if (odr_putc(o, (unsigned char) len) < 0)
38 if (odr_putc(o, 0x80) < 0)
42 /* definite long form */
51 lenpos = odr_tell(o); /* remember length-of-length position */
52 if (odr_putc(o, 0) < 0) /* dummy */
55 while (n < --lenlen) /* pad length octets */
56 if (odr_putc(o, 0) < 0)
59 if (odr_putc(o, octs[n]) < 0)
61 /* set length of length */
63 odr_seek(o, ODR_S_SET, lenpos);
64 if (odr_putc(o, (end - lenpos - 1) | 0X80) < 0)
66 odr_seek(o, ODR_S_END, 0);
67 return odr_tell(o) - lenpos;
71 * Decode BER length octets. Returns number of bytes read or -1 for error.
73 * len = -1 indefinite.
76 int ber_declen(unsigned char *buf, int *len)
78 unsigned char *b = buf;
81 if (*b == 0X80) /* Indefinite */
85 fprintf(stderr, "[len=%d]", *len);
89 if (!(*b & 0X80)) /* Definite short form */
93 fprintf(stderr, "[len=%d]", *len);
97 if (*b == 0XFF) /* reserved value */
99 /* indefinite long form */
109 fprintf(stderr, "[len=%d]", *len);