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