2 * Copyright (C) 1995-1999, Index Data.
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.8 1999-11-30 13:47:11 adam
8 * Improved installation. Moved header files to include/yaz.
10 * Revision 1.7 1999/01/08 11:23:23 adam
11 * Added const modifier to some of the BER/ODR encoding routines.
13 * Revision 1.6 1995/09/29 17:12:17 quinn
16 * Revision 1.5 1995/09/27 15:02:55 quinn
17 * Modified function heads & prototypes.
19 * Revision 1.4 1995/05/16 08:50:45 quinn
20 * License, documentation, and memory fixes
29 * Encode BER length octets. If exact, lenlen is the exact desired
30 * encoding size, else, lenlen is the max available space. Len < 0 =
31 * Indefinite encoding.
32 * Returns: >0 success, number of bytes encoded.
33 * Returns: =0 success, indefinite start-marker set. 1 byte encoded.
34 * Returns: -1 failure, out of bounds.
36 int ber_enclen(ODR o, int len, int lenlen, int exact)
38 unsigned char octs[sizeof(int)];
43 fprintf(stderr, "[len=%d]", len);
45 if (len < 0) /* Indefinite */
47 if (odr_putc(o, 0x80) < 0)
50 fprintf(stderr, "[indefinite]");
54 if (len <= 127 && (lenlen == 1 || !exact)) /* definite short form */
56 if (odr_putc(o, (unsigned char) len) < 0)
62 if (odr_putc(o, 0x80) < 0)
66 /* definite long form */
75 lenpos = odr_tell(o); /* remember length-of-length position */
76 if (odr_putc(o, 0) < 0) /* dummy */
79 while (n < --lenlen) /* pad length octets */
80 if (odr_putc(o, 0) < 0)
83 if (odr_putc(o, octs[n]) < 0)
85 /* set length of length */
87 odr_seek(o, ODR_S_SET, lenpos);
88 if (odr_putc(o, (end - lenpos - 1) | 0X80) < 0)
90 odr_seek(o, ODR_S_END, 0);
91 return odr_tell(o) - lenpos;
95 * Decode BER length octets. Returns number of bytes read or -1 for error.
97 * len = -1 indefinite.
100 int ber_declen(const unsigned char *buf, int *len)
102 const unsigned char *b = buf;
105 if (*b == 0X80) /* Indefinite */
109 fprintf(stderr, "[len=%d]", *len);
113 if (!(*b & 0X80)) /* Definite short form */
117 fprintf(stderr, "[len=%d]", *len);
121 if (*b == 0XFF) /* reserved value */
123 /* indefinite long form */
133 fprintf(stderr, "[len=%d]", *len);