Fixed possible buf in proto.c
[yaz-moved-to-github.git] / odr / ber_int.c
1 /*
2  * Copyright (C) 1994, Index Data I/S 
3  * All rights reserved.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: ber_int.c,v $
7  * Revision 1.5  1995-03-27 15:01:44  quinn
8  * Added include of sys/types to further portability
9  *
10  * Revision 1.4  1995/03/08  12:12:07  quinn
11  * Added better error checking.
12  *
13  * Revision 1.3  1995/02/09  15:51:46  quinn
14  * Works better now.
15  *
16  * Revision 1.2  1995/02/07  17:52:58  quinn
17  * A damn mess, but now things work, I think.
18  *
19  * Revision 1.1  1995/02/02  16:21:52  quinn
20  * First kick.
21  *
22  */
23
24 #include <odr.h>
25 #include <sys/types.h>
26 #include <netinet/in.h>  /* for htons... */
27 #include <string.h>
28
29 static int ber_encinteger(unsigned char *buf, int val, int maxlen);
30 static int ber_decinteger(unsigned char *buf, int *val);
31
32 int ber_integer(ODR o, int *val)
33 {
34     int res;
35
36     switch (o->direction)
37     {
38         case ODR_DECODE:
39             if ((res = ber_decinteger(o->bp, val)) <= 0)
40             {
41                 o->error = OPROTO;
42                 return 0;
43             }
44             o->bp += res;
45             o->left -= res;
46             return 1;
47         case ODR_ENCODE:
48             if ((res = ber_encinteger(o->bp, *val, o->left)) <= 0)
49             {
50                 o->error = OSPACE;
51                 return 0;
52             }
53             o->bp += res;
54             o->left -= res;
55             return 1;
56         case ODR_PRINT: return 1;
57         default: o->error = OOTHER;  return 0;
58     }
59 }
60
61 /*
62  * Returns: number of bytes written or -1 for error (out of bounds).
63  */
64 int ber_encinteger(unsigned char *buf, int val, int maxlen)
65 {
66     unsigned char *b = buf, *lenpos;
67     int a, len;
68     union { int i; unsigned char c[sizeof(int)]; } tmp;
69
70     lenpos = b;
71     maxlen--;
72     b++;
73
74     tmp.i = htonl(val);   /* ensure that that we're big-endian */
75     for (a = 0; a < sizeof(int) - 1; a++)  /* skip superfluous octets */
76         if (!((tmp.c[a] == 0 && !(tmp.c[a+1] & 0X80)) ||
77             (tmp.c[a] == 0XFF && (tmp.c[a+1] & 0X80))))
78             break;
79     if ((len = sizeof(int) - a) > maxlen)
80         return -1;
81     memcpy(b, tmp.c + a, len);
82     b += len;
83     if (ber_enclen(lenpos, len, 1, 1) != 1)
84         return -1;
85 #ifdef ODR_DEBUG
86     fprintf(stderr, "[val=%d]", val);
87 #endif
88     return b - buf;
89 }
90
91 /*
92  * Returns: Number of bytes read or 0 if no match, -1 if error.
93  */
94 int ber_decinteger(unsigned char *buf, int *val)
95 {
96     unsigned char *b = buf, fill;
97     int res, len, remains;
98     union { int i; unsigned char c[sizeof(int)]; } tmp;
99
100     if ((res = ber_declen(b, &len)) < 0)
101         return -1;
102     if (len > sizeof(int))    /* let's be reasonable, here */
103         return -1;
104     b+= res;
105
106     remains = sizeof(int) - len;
107     memcpy(tmp.c + remains, b, len);
108     if (*b & 0X80)
109         fill = 0XFF;
110     else
111         fill = 0X00;
112     memset(tmp.c, fill, remains);
113     *val = ntohl(tmp.i);
114
115     b += len;
116 #ifdef ODR_DEBUG
117     fprintf(stderr, "[val=%d]", *val);
118 #endif
119     return b - buf;
120 }