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