2 * Copyright (C) 1995, Index Data.
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.5 1995-09-27 15:02:55 quinn
8 * Modified function heads & prototypes.
10 * Revision 1.4 1995/05/16 08:50:45 quinn
11 * License, documentation, and memory fixes
20 * Encode BER length octets. If exact, lenlen is the exact desired
21 * encoding size, else, lenlen is the max available space. Len < 0 =
22 * Indefinite encoding.
23 * Returns: >0 success, number of bytes encoded.
24 * Returns: =0 success, indefinite start-marker set. 1 byte encoded.
25 * Returns: -1 failure, out of bounds.
27 int MDF ber_enclen(ODR o, int len, int lenlen, int exact)
29 unsigned char octs[sizeof(int)];
34 fprintf(stderr, "[len=%d]", len);
36 if (len < 0) /* Indefinite */
38 if (odr_putc(o, 0x80) < 0)
41 fprintf(stderr, "[indefinite]");
45 if (len <= 127 && (lenlen == 1 || !exact)) /* definite short form */
47 if (odr_putc(o, (unsigned char) len) < 0)
53 if (odr_putc(o, 0x80) < 0)
57 /* definite long form */
66 lenpos = odr_tell(o); /* remember length-of-length position */
67 if (odr_putc(o, 0) < 0) /* dummy */
70 while (n < --lenlen) /* pad length octets */
71 if (odr_putc(o, 0) < 0)
74 if (odr_putc(o, octs[n]) < 0)
76 /* set length of length */
78 odr_seek(o, ODR_S_SET, lenpos);
79 if (odr_putc(o, (end - lenpos - 1) | 0X80) < 0)
81 odr_seek(o, ODR_S_END, 0);
82 return odr_tell(o) - lenpos;
86 * Decode BER length octets. Returns number of bytes read or -1 for error.
88 * len = -1 indefinite.
91 int MDF ber_declen(unsigned char *buf, int *len)
93 unsigned char *b = buf;
96 if (*b == 0X80) /* Indefinite */
100 fprintf(stderr, "[len=%d]", *len);
104 if (!(*b & 0X80)) /* Definite short form */
108 fprintf(stderr, "[len=%d]", *len);
112 if (*b == 0XFF) /* reserved value */
114 /* indefinite long form */
124 fprintf(stderr, "[len=%d]", *len);