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