2 * Copyright (C) 1995-2000, Index Data.
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.9 2000-02-29 13:44:55 adam
8 * Check for config.h (currently not generated).
10 * Revision 1.8 1999/11/30 13:47:11 adam
11 * Improved installation. Moved header files to include/yaz.
13 * Revision 1.7 1999/01/08 11:23:23 adam
14 * Added const modifier to some of the BER/ODR encoding routines.
16 * Revision 1.6 1995/09/29 17:12:17 quinn
19 * Revision 1.5 1995/09/27 15:02:55 quinn
20 * Modified function heads & prototypes.
22 * Revision 1.4 1995/05/16 08:50:45 quinn
23 * License, documentation, and memory fixes
35 * Encode BER length octets. If exact, lenlen is the exact desired
36 * encoding size, else, lenlen is the max available space. Len < 0 =
37 * Indefinite encoding.
38 * Returns: >0 success, number of bytes encoded.
39 * Returns: =0 success, indefinite start-marker set. 1 byte encoded.
40 * Returns: -1 failure, out of bounds.
42 int ber_enclen(ODR o, int len, int lenlen, int exact)
44 unsigned char octs[sizeof(int)];
49 fprintf(stderr, "[len=%d]", len);
51 if (len < 0) /* Indefinite */
53 if (odr_putc(o, 0x80) < 0)
56 fprintf(stderr, "[indefinite]");
60 if (len <= 127 && (lenlen == 1 || !exact)) /* definite short form */
62 if (odr_putc(o, (unsigned char) len) < 0)
68 if (odr_putc(o, 0x80) < 0)
72 /* definite long form */
81 lenpos = odr_tell(o); /* remember length-of-length position */
82 if (odr_putc(o, 0) < 0) /* dummy */
85 while (n < --lenlen) /* pad length octets */
86 if (odr_putc(o, 0) < 0)
89 if (odr_putc(o, octs[n]) < 0)
91 /* set length of length */
93 odr_seek(o, ODR_S_SET, lenpos);
94 if (odr_putc(o, (end - lenpos - 1) | 0X80) < 0)
96 odr_seek(o, ODR_S_END, 0);
97 return odr_tell(o) - lenpos;
101 * Decode BER length octets. Returns number of bytes read or -1 for error.
103 * len = -1 indefinite.
106 int ber_declen(const unsigned char *buf, int *len)
108 const unsigned char *b = buf;
111 if (*b == 0X80) /* Indefinite */
115 fprintf(stderr, "[len=%d]", *len);
119 if (!(*b & 0X80)) /* Definite short form */
123 fprintf(stderr, "[len=%d]", *len);
127 if (*b == 0XFF) /* reserved value */
129 /* indefinite long form */
139 fprintf(stderr, "[len=%d]", *len);