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