More in the way of error-checking.
[yaz-moved-to-github.git] / odr / ber_tag.c
1 /*
2  * Copyright (C) 1994, Index Data I/S 
3  * All rights reserved.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: ber_tag.c,v $
7  * Revision 1.5  1995-02-10 18:57:24  quinn
8  * More in the way of error-checking.
9  *
10  * Revision 1.4  1995/02/10  15:55:28  quinn
11  * Bug fixes, mostly.
12  *
13  * Revision 1.3  1995/02/09  15:51:46  quinn
14  * Works better now.
15  *
16  * Revision 1.2  1995/02/07  17:52:59  quinn
17  * A damn mess, but now things work, I think.
18  *
19  * Revision 1.1  1995/02/02  16:21:53  quinn
20  * First kick.
21  *
22  */
23
24 #include <stdio.h>
25 #include <odr.h>
26
27 /* ber_tag
28  * On encoding:
29  *      if  p: write tag. return 1 (success) or -1 (error).
30  *      if !p: return 0.
31  * On decoding:
32  *      if tag && class match up, advance pointer and return 1. set cons.
33  *      else leave pointer unchanged. Return 0.
34  *
35  * Should perhaps be odr_tag?
36 */
37 int ber_tag(ODR o, void *p, int class, int tag, int *constructed)
38 {
39     static int lclass = -1, ltag, br, lcons; /* save t&c rather than
40                                                 decoding twice */
41     int rd;
42     char **pp = p;
43
44     if (o->direction == ODR_DECODE)
45         *pp = 0;
46     o->t_class = -1;
47     if (o->buf == o->bp)   /* This is insurance. It shouldn't be necessary */
48         lclass = -1;
49     switch (o->direction)
50     {
51         case ODR_ENCODE:
52             if (!*pp)
53                 return 0;
54             if ((rd = ber_enctag(o->bp, class, tag, *constructed, o->left))
55                 <=0)
56                 return -1;
57             o->bp += rd;
58             o->left -= rd;
59 #ifdef ODR_DEBUG
60             fprintf(stderr, "\n[class=%d,tag=%d,cons=%d]", class, tag,
61                 *constructed);
62 #endif
63             return 1;
64         case ODR_DECODE:
65             if (o->stackp > -1 && !odr_constructed_more(o))
66                 return 0;
67             if (lclass < 0)
68             {
69                 if ((br = ber_dectag(o->bp, &lclass, &ltag, &lcons)) <= 0)
70                     return 0;
71 #ifdef ODR_DEBUG
72                 fprintf(stderr, "\n[class=%d,tag=%d,cons=%d]", lclass, ltag,
73                     lcons);
74 #endif
75             }
76             if (class == lclass && tag == ltag)
77             {
78                 o->bp += br;
79                 o->left -= br;
80                 *constructed = lcons;
81                 lclass = -1;
82                 return 1;
83             }
84             else
85                 return 0;
86         case ODR_PRINT: return *pp != 0;
87         default: return 0;
88     }
89 }
90
91 /* ber_enctag
92  * BER-encode a class/tag/constructed package (identifier octets). Return
93  * number of bytes encoded, or -1 if out of bounds.
94  */
95 int ber_enctag(unsigned char *buf, int class, int tag, int constructed, int len)
96 {
97     int cons = (constructed ? 1 : 0), n = 0;
98     unsigned char octs[sizeof(int)], *b = buf;
99
100     *b = (class << 6) & 0XC0;
101     *b |= (cons << 5) & 0X20;
102     if (tag <= 30)
103     {
104         *b |= tag & 0X1F;
105         return 1;
106     }
107     else
108     {
109         *(b++) |= 0x1F;
110         do
111         {
112             octs[n++] = tag & 0X7F;
113             tag >>= 7;
114             if (n >= len) /* bounds check */
115                 return -1;
116         }
117         while (tag);
118         while (n--)
119             *(b++) = octs[n] | ((n > 0) << 7);
120         return b - buf;
121     }
122 }
123
124 /* ber_dectag
125  * Decode BER identifier octets. Return number of bytes read or -1 for error.
126  */
127 int ber_dectag(unsigned char *buf, int *class, int *tag, int *constructed)
128 {
129     unsigned char *b = buf;
130
131     *class = *b >> 6;
132     *constructed = (*b >> 5) & 0X01;
133     if ((*tag = *b & 0x1F) <= 30)
134         return 1;
135     b++;
136     *tag = 0;
137     do
138     {
139         *tag <<= 7;
140         *tag |= *b & 0X7F;
141         if (b - buf >= 5) /* Precaution */
142             return -1;
143     }
144     while (*(b++) & 0X80);
145     return b - buf;
146 }