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