Source restructure. yaz-marcdump part of installation
[yaz-moved-to-github.git] / src / ber_tag.c
1 /*
2  * Copyright (c) 1995-2003, Index Data
3  * See the file LICENSE for details.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Id: ber_tag.c,v 1.1 2003-10-27 12:21:30 adam Exp $
7  */
8 #if HAVE_CONFIG_H
9 #include <config.h>
10 #endif
11
12 #include <stdio.h>
13 #include "odr-priv.h"
14
15 /* ber_tag
16  * On encoding:
17  *      if  p: write tag. return 1 (success) or -1 (error).
18  *      if !p: return 0.
19  * On decoding:
20  *      if tag && zclass match up, advance pointer and return 1. set cons.
21  *      else leave pointer unchanged. Return 0.
22  *
23  * Should perhaps be odr_tag?
24  */
25 int ber_tag(ODR o, void *p, int zclass, int tag, int *constructed, int opt,
26             const char *name)
27 {
28     struct Odr_ber_tag *odr_ber_tag = &o->op->odr_ber_tag;
29     int rd;
30     char **pp = (char **)p;
31
32     if (o->direction == ODR_DECODE)
33         *pp = 0;
34     o->t_class = -1;
35     if (o->op->stackp < 0)
36     {
37         odr_seek(o, ODR_S_SET, 0);
38         o->top = 0;
39         o->bp = o->buf;
40         odr_ber_tag->lclass = -1;
41     }
42     switch (o->direction)
43     {
44     case ODR_ENCODE:
45         if (!*pp)
46         {
47             if (!opt)
48             {
49                 odr_seterror(o, OREQUIRED, 24);
50                 odr_setelement (o, name);
51             }
52             return 0;
53         }
54         if ((rd = ber_enctag(o, zclass, tag, *constructed)) < 0)
55             return -1;
56 #ifdef ODR_DEBUG
57         fprintf(stderr, "\n[class=%d,tag=%d,cons=%d,stackp=%d]", zclass, tag,
58                 *constructed, o->stackp);
59 #endif
60         return 1;
61         
62     case ODR_DECODE:
63         if (o->op->stackp > -1 && !odr_constructed_more(o))
64         {
65             if (!opt)
66             {
67                 odr_seterror(o, OREQUIRED, 25);
68                 odr_setelement(o, name);
69             }
70             return 0;
71         }
72         if (odr_ber_tag->lclass < 0)
73         {
74             if ((odr_ber_tag->br =
75                  ber_dectag(o->bp, &odr_ber_tag->lclass,
76                             &odr_ber_tag->ltag, &odr_ber_tag->lcons,
77                             odr_max(o))) <= 0)
78             {
79                 odr_seterror(o, OPROTO, 26);
80                 odr_setelement(o, name);
81                 return 0;
82             }
83 #ifdef ODR_DEBUG
84             fprintf(stderr,
85                     "\n[class=%d,tag=%d,cons=%d,stackp=%d]",
86                     odr_ber_tag->lclass, odr_ber_tag->ltag,
87                     odr_ber_tag->lcons, o->stackp);
88 #endif
89         }
90         if (zclass == odr_ber_tag->lclass && tag == odr_ber_tag->ltag)
91         {
92             o->bp += odr_ber_tag->br;
93             *constructed = odr_ber_tag->lcons;
94             odr_ber_tag->lclass = -1;
95             return 1;
96         }
97         else
98         {
99             if (!opt)
100             {
101                 odr_seterror(o, OREQUIRED, 27);
102                 odr_setelement(o, name);
103             }
104             return 0;
105         }
106     case ODR_PRINT:
107         if (!*pp && !opt)
108         {
109             odr_seterror(o,OREQUIRED, 28);
110             odr_setelement(o, name);
111         }
112         return *pp != 0;
113     default:
114         odr_seterror(o, OOTHER, 29);
115         odr_setelement(o, name);
116         return 0;
117     }
118 }
119
120 /* ber_enctag
121  * BER-encode a zclass/tag/constructed package (identifier octets). Return
122  * number of bytes encoded, or -1 if out of bounds.
123  */
124 int ber_enctag(ODR o, int zclass, int tag, int constructed)
125 {
126     int cons = (constructed ? 1 : 0), n = 0;
127     unsigned char octs[sizeof(int)], b;
128
129     b = (zclass << 6) & 0XC0;
130     b |= (cons << 5) & 0X20;
131     if (tag <= 30)
132     {
133         b |= tag & 0X1F;
134         if (odr_putc(o, b) < 0)
135             return -1;
136         return 1;
137     }
138     else
139     {
140         b |= 0X1F;
141         if (odr_putc(o, b) < 0)
142             return -1;
143         do
144         {
145             octs[n++] = tag & 0X7F;
146             tag >>= 7;
147         }
148         while (tag);
149         while (n--)
150         {
151             unsigned char oo;
152
153             oo = octs[n] | ((n > 0) << 7);
154             if (odr_putc(o, oo) < 0)
155                 return -1;
156         }
157         return 0;
158     }
159 }
160
161 /* ber_dectag
162  * Decode BER identifier octets. Return number of bytes read or -1 for error.
163  */
164 int ber_dectag(const unsigned char *b, int *zclass, int *tag,
165                int *constructed, int max)
166 {
167     int l = 1;
168
169     if (l > max)
170         return -1;
171
172     *zclass = *b >> 6;
173     *constructed = (*b >> 5) & 0X01;
174     if ((*tag = *b & 0x1F) <= 30)
175         return 1;
176     *tag = 0;
177     do
178     {
179         if (l >= max)
180             return -1;
181         *tag <<= 7;
182         *tag |= b[l] & 0X7F;
183     }
184     while (b[l++] & 0X80);
185     return l;
186 }