2 * Copyright (C) 1995-1999, Index Data.
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.7 1999-01-08 11:23:23 adam
8 * Added const modifier to some of the BER/ODR encoding routines.
10 * Revision 1.6 1995/09/29 17:12:17 quinn
13 * Revision 1.5 1995/09/27 15:02:55 quinn
14 * Modified function heads & prototypes.
16 * Revision 1.4 1995/05/16 08:50:45 quinn
17 * License, documentation, and memory fixes
26 * Encode BER length octets. If exact, lenlen is the exact desired
27 * encoding size, else, lenlen is the max available space. Len < 0 =
28 * Indefinite encoding.
29 * Returns: >0 success, number of bytes encoded.
30 * Returns: =0 success, indefinite start-marker set. 1 byte encoded.
31 * Returns: -1 failure, out of bounds.
33 int ber_enclen(ODR o, int len, int lenlen, int exact)
35 unsigned char octs[sizeof(int)];
40 fprintf(stderr, "[len=%d]", len);
42 if (len < 0) /* Indefinite */
44 if (odr_putc(o, 0x80) < 0)
47 fprintf(stderr, "[indefinite]");
51 if (len <= 127 && (lenlen == 1 || !exact)) /* definite short form */
53 if (odr_putc(o, (unsigned char) len) < 0)
59 if (odr_putc(o, 0x80) < 0)
63 /* definite long form */
72 lenpos = odr_tell(o); /* remember length-of-length position */
73 if (odr_putc(o, 0) < 0) /* dummy */
76 while (n < --lenlen) /* pad length octets */
77 if (odr_putc(o, 0) < 0)
80 if (odr_putc(o, octs[n]) < 0)
82 /* set length of length */
84 odr_seek(o, ODR_S_SET, lenpos);
85 if (odr_putc(o, (end - lenpos - 1) | 0X80) < 0)
87 odr_seek(o, ODR_S_END, 0);
88 return odr_tell(o) - lenpos;
92 * Decode BER length octets. Returns number of bytes read or -1 for error.
94 * len = -1 indefinite.
97 int ber_declen(const unsigned char *buf, int *len)
99 const unsigned char *b = buf;
102 if (*b == 0X80) /* Indefinite */
106 fprintf(stderr, "[len=%d]", *len);
110 if (!(*b & 0X80)) /* Definite short form */
114 fprintf(stderr, "[len=%d]", *len);
118 if (*b == 0XFF) /* reserved value */
120 /* indefinite long form */
130 fprintf(stderr, "[len=%d]", *len);