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