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