2 * Copyright (C) 1995, Index Data.
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.4 1995-05-16 08:50:45 quinn
8 * License, documentation, and memory fixes
17 * Encode BER length octets. If exact, lenlen is the exact desired
18 * encoding size, else, lenlen is the max available space. Len < 0 =
19 * Indefinite encoding.
20 * Returns: >0 success, number of bytes encoded.
21 * Returns: =0 success, indefinite start-marker set. 1 byte encoded.
22 * Returns: -1 failure, out of bounds.
24 int ber_enclen(ODR o, int len, int lenlen, int exact)
26 unsigned char octs[sizeof(int)];
31 fprintf(stderr, "[len=%d]", len);
33 if (len < 0) /* Indefinite */
35 if (odr_putc(o, 0x80) < 0)
38 fprintf(stderr, "[indefinite]");
42 if (len <= 127 && (lenlen == 1 || !exact)) /* definite short form */
44 if (odr_putc(o, (unsigned char) len) < 0)
50 if (odr_putc(o, 0x80) < 0)
54 /* definite long form */
63 lenpos = odr_tell(o); /* remember length-of-length position */
64 if (odr_putc(o, 0) < 0) /* dummy */
67 while (n < --lenlen) /* pad length octets */
68 if (odr_putc(o, 0) < 0)
71 if (odr_putc(o, octs[n]) < 0)
73 /* set length of length */
75 odr_seek(o, ODR_S_SET, lenpos);
76 if (odr_putc(o, (end - lenpos - 1) | 0X80) < 0)
78 odr_seek(o, ODR_S_END, 0);
79 return odr_tell(o) - lenpos;
83 * Decode BER length octets. Returns number of bytes read or -1 for error.
85 * len = -1 indefinite.
88 int ber_declen(unsigned char *buf, int *len)
90 unsigned char *b = buf;
93 if (*b == 0X80) /* Indefinite */
97 fprintf(stderr, "[len=%d]", *len);
101 if (!(*b & 0X80)) /* Definite short form */
105 fprintf(stderr, "[len=%d]", *len);
109 if (*b == 0XFF) /* reserved value */
111 /* indefinite long form */
121 fprintf(stderr, "[len=%d]", *len);