6d0da3504a3702543c1627a54c070b1d9f2394bf
[yaz-moved-to-github.git] / odr / ber_int.c
1 /*
2  * Copyright (c) 1995-1999, Index Data
3  * See the file LICENSE for details.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: ber_int.c,v $
7  * Revision 1.13  1999-01-08 11:23:22  adam
8  * Added const modifier to some of the BER/ODR encoding routines.
9  *
10  * Revision 1.12  1996/07/06 19:58:33  quinn
11  * System headerfiles gathered in yconfig
12  *
13  * Revision 1.11  1995/09/29  17:12:16  quinn
14  * Smallish
15  *
16  * Revision 1.10  1995/09/29  17:01:50  quinn
17  * More Windows work
18  *
19  * Revision 1.9  1995/09/28  10:12:39  quinn
20  * Windows-support changes
21  *
22  * Revision 1.8  1995/09/27  15:02:55  quinn
23  * Modified function heads & prototypes.
24  *
25  * Revision 1.7  1995/05/16  08:50:44  quinn
26  * License, documentation, and memory fixes
27  *
28  * Revision 1.6  1995/04/18  08:15:14  quinn
29  * Added dynamic memory allocation on encoding (whew). Code is now somewhat
30  * neater. We'll make the same change for decoding one day.
31  *
32  * Revision 1.5  1995/03/27  15:01:44  quinn
33  * Added include of sys/types to further portability
34  *
35  * Revision 1.4  1995/03/08  12:12:07  quinn
36  * Added better error checking.
37  *
38  * Revision 1.3  1995/02/09  15:51:46  quinn
39  * Works better now.
40  *
41  * Revision 1.2  1995/02/07  17:52:58  quinn
42  * A damn mess, but now things work, I think.
43  *
44  * Revision 1.1  1995/02/02  16:21:52  quinn
45  * First kick.
46  *
47  */
48
49
50 #define YNETINCLUDE
51 #include <yconfig.h>
52
53 #include <string.h>
54
55 #include <odr.h>
56
57 static int ber_encinteger(ODR o, int val);
58 static int ber_decinteger(const unsigned char *buf, int *val);
59
60 int ber_integer(ODR o, int *val)
61 {
62     int res;
63
64     switch (o->direction)
65     {
66         case ODR_DECODE:
67             if ((res = ber_decinteger(o->bp, val)) <= 0)
68             {
69                 o->error = OPROTO;
70                 return 0;
71             }
72             o->bp += res;
73             o->left -= res;
74             return 1;
75         case ODR_ENCODE:
76             if ((res = ber_encinteger(o, *val)) < 0)
77                 return 0;
78             return 1;
79         case ODR_PRINT: return 1;
80         default: o->error = OOTHER;  return 0;
81     }
82 }
83
84 /*
85  * Returns: number of bytes written or -1 for error (out of bounds).
86  */
87 int ber_encinteger(ODR o, int val)
88 {
89     int lenpos;
90     int a, len;
91     union { int i; unsigned char c[sizeof(int)]; } tmp;
92
93     lenpos = odr_tell(o);
94     if (odr_putc(o, 0) < 0)  /* dummy */
95         return -1;
96     tmp.i = htonl(val);   /* ensure that that we're big-endian */
97     for (a = 0; a < sizeof(int) - 1; a++)  /* skip superfluous octets */
98         if (!((tmp.c[a] == 0 && !(tmp.c[a+1] & 0X80)) ||
99             (tmp.c[a] == 0XFF && (tmp.c[a+1] & 0X80))))
100             break;
101     len = sizeof(int) - a;
102     if (odr_write(o, (unsigned char*) tmp.c + a, len) < 0)
103         return -1;
104     odr_seek(o, ODR_S_SET, lenpos);
105     if (ber_enclen(o, len, 1, 1) != 1)
106         return -1;
107     odr_seek(o, ODR_S_END, 0);
108 #ifdef ODR_DEBUG
109     fprintf(stderr, "[val=%d]", val);
110 #endif
111     return 0;
112 }
113
114 /*
115  * Returns: Number of bytes read or 0 if no match, -1 if error.
116  */
117 int ber_decinteger(const unsigned char *buf, int *val)
118 {
119     const unsigned char *b = buf;
120     unsigned char fill;
121     int res, len, remains;
122     union { int i; unsigned char c[sizeof(int)]; } tmp;
123
124     if ((res = ber_declen(b, &len)) < 0)
125         return -1;
126     if (len > sizeof(int))    /* let's be reasonable, here */
127         return -1;
128     b+= res;
129
130     remains = sizeof(int) - len;
131     memcpy(tmp.c + remains, b, len);
132     if (*b & 0X80)
133         fill = 0XFF;
134     else
135         fill = 0X00;
136     memset(tmp.c, fill, remains);
137     *val = ntohl(tmp.i);
138
139     b += len;
140 #ifdef ODR_DEBUG
141     fprintf(stderr, "[val=%d]", *val);
142 #endif
143     return b - buf;
144 }