2 * Copyright (C) 1995, Index Data.
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.6 1995-09-29 17:12:17 quinn
10 * Revision 1.5 1995/09/27 15:02:55 quinn
11 * Modified function heads & prototypes.
13 * Revision 1.4 1995/05/16 08:50:45 quinn
14 * License, documentation, and memory fixes
23 * Encode BER length octets. If exact, lenlen is the exact desired
24 * encoding size, else, lenlen is the max available space. Len < 0 =
25 * Indefinite encoding.
26 * Returns: >0 success, number of bytes encoded.
27 * Returns: =0 success, indefinite start-marker set. 1 byte encoded.
28 * Returns: -1 failure, out of bounds.
30 int ber_enclen(ODR o, int len, int lenlen, int exact)
32 unsigned char octs[sizeof(int)];
37 fprintf(stderr, "[len=%d]", len);
39 if (len < 0) /* Indefinite */
41 if (odr_putc(o, 0x80) < 0)
44 fprintf(stderr, "[indefinite]");
48 if (len <= 127 && (lenlen == 1 || !exact)) /* definite short form */
50 if (odr_putc(o, (unsigned char) len) < 0)
56 if (odr_putc(o, 0x80) < 0)
60 /* definite long form */
69 lenpos = odr_tell(o); /* remember length-of-length position */
70 if (odr_putc(o, 0) < 0) /* dummy */
73 while (n < --lenlen) /* pad length octets */
74 if (odr_putc(o, 0) < 0)
77 if (odr_putc(o, octs[n]) < 0)
79 /* set length of length */
81 odr_seek(o, ODR_S_SET, lenpos);
82 if (odr_putc(o, (end - lenpos - 1) | 0X80) < 0)
84 odr_seek(o, ODR_S_END, 0);
85 return odr_tell(o) - lenpos;
89 * Decode BER length octets. Returns number of bytes read or -1 for error.
91 * len = -1 indefinite.
94 int ber_declen(unsigned char *buf, int *len)
96 unsigned char *b = buf;
99 if (*b == 0X80) /* Indefinite */
103 fprintf(stderr, "[len=%d]", *len);
107 if (!(*b & 0X80)) /* Definite short form */
111 fprintf(stderr, "[len=%d]", *len);
115 if (*b == 0XFF) /* reserved value */
117 /* indefinite long form */
127 fprintf(stderr, "[len=%d]", *len);