First kick.
[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.1  1995-02-02 16:21:53  quinn
8  * First kick.
9  *
10  */
11
12 #include <stdio.h>
13 #include <odr.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 && class match up, advance pointer and return 1. set cons.
21  *      else leave pointer unchanged. Return 0.
22 */
23 int ber_tag(ODR o, const void *p, int class, int tag, int *constructed)
24 {
25     static int lclass = -1, ltag, br, lcons; /* save t&c rather than
26                                                 decoding twice */
27     int rd;
28
29     o->t_class = -1;
30     switch (o->direction)
31     {
32         case ODR_ENCODE:
33             if (!p)
34                 return 0;
35             if ((rd = ber_enctag(o->bp, class, tag, *constructed, o->left))
36                 <=0)
37                 return -1;
38             o->bp += rd;
39             o->left -= rd;
40             fprintf(stderr, "[class=%d,tag=%d,cons=%d]", class, tag,
41                 *constructed);
42             return 1;
43         case ODR_DECODE:
44             if (lclass < 0)
45             {
46                 if ((br = ber_dectag(o->bp, &lclass, &ltag, &lcons)) <= 0)
47                     return -1;
48                 fprintf(stderr, "[class=%d,tag=%d,cons=%d]", lclass, ltag,
49                     lcons);
50             }
51             if (class == lclass && tag == ltag)
52             {
53                 o->bp += br;
54                 o->left -= br;
55                 *constructed = lcons;
56                 lclass = -1;
57                 return 1;
58             }
59             else
60                 return 0;
61         case ODR_PRINT: return p != 0;
62         default: return 0;
63     }
64 }
65
66 /* ber_enctag
67  * BER-encode a class/tag/constructed package (identifier octets). Return
68  * number of bytes encoded, or -1 if out of bounds.
69  */
70 int ber_enctag(unsigned char *buf, int class, int tag, int constructed, int len)
71 {
72     int cons = (constructed ? 1 : 0), n = 0;
73     unsigned char octs[sizeof(int)], *b = buf;
74
75     *b = (class << 6) & 0XC0;
76     *b |= (cons << 5) & 0X20;
77     if (tag <= 30)
78     {
79         *b |= tag & 0X1F;
80         return 1;
81     }
82     else
83     {
84         *(b++) |= 0x1F;
85         do
86         {
87             octs[n++] = tag & 0X7F;
88             tag >>= 7;
89             if (n >= len) /* bounds check */
90                 return -1;
91         }
92         while (tag);
93         while (n--)
94             *(b++) = octs[n] | ((n > 0) << 7);
95         return b - buf;
96     }
97 }
98
99 /* ber_dectag
100  * Decode BER identifier octets. Return number of bytes read or -1 for error.
101  */
102 int ber_dectag(unsigned char *buf, int *class, int *tag, int *constructed)
103 {
104     unsigned char *b = buf;
105
106     *class = *b >> 6;
107     *constructed = (*b >> 5) & 0X01;
108     if ((*tag = *b & 0x1F) <= 30)
109         return 1;
110     b++;
111     *tag = 0;
112     do
113     {
114         *tag <<= 7;
115         *tag |= *b & 0X7F;
116         if (b - buf >= 5) /* Precaution */
117             return -1;
118     }
119     while (*(b++) & 0X80);
120     return b - buf;
121 }