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