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