Added dynamic memory allocation on encoding (whew). Code is now somewhat
[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.6  1995-04-18 08:15:14  quinn
8  * Added dynamic memory allocation on encoding (whew). Code is now somewhat
9  * neater. We'll make the same change for decoding one day.
10  *
11  * Revision 1.5  1995/03/27  15:01:44  quinn
12  * Added include of sys/types to further portability
13  *
14  * Revision 1.4  1995/03/08  12:12:07  quinn
15  * Added better error checking.
16  *
17  * Revision 1.3  1995/02/09  15:51:46  quinn
18  * Works better now.
19  *
20  * Revision 1.2  1995/02/07  17:52:58  quinn
21  * A damn mess, but now things work, I think.
22  *
23  * Revision 1.1  1995/02/02  16:21:52  quinn
24  * First kick.
25  *
26  */
27
28 #include <odr.h>
29 #include <sys/types.h>
30 #include <netinet/in.h>  /* for htons... */
31 #include <string.h>
32
33 static int ber_encinteger(ODR o, int val);
34 static int ber_decinteger(unsigned char *buf, int *val);
35
36 int ber_integer(ODR o, int *val)
37 {
38     int res;
39
40     switch (o->direction)
41     {
42         case ODR_DECODE:
43             if ((res = ber_decinteger(o->bp, val)) <= 0)
44             {
45                 o->error = OPROTO;
46                 return 0;
47             }
48             o->bp += res;
49             o->left -= res;
50             return 1;
51         case ODR_ENCODE:
52             if ((res = ber_encinteger(o, *val)) < 0)
53                 return 0;
54             return 1;
55         case ODR_PRINT: return 1;
56         default: o->error = OOTHER;  return 0;
57     }
58 }
59
60 /*
61  * Returns: number of bytes written or -1 for error (out of bounds).
62  */
63 int ber_encinteger(ODR o, int val)
64 {
65     int lenpos;
66     int a, len;
67     union { int i; unsigned char c[sizeof(int)]; } tmp;
68
69     lenpos = odr_tell(o);
70     if (odr_putc(o, 0) < 0)  /* dummy */
71         return -1;
72
73     tmp.i = htonl(val);   /* ensure that that we're big-endian */
74     for (a = 0; a < sizeof(int) - 1; a++)  /* skip superfluous octets */
75         if (!((tmp.c[a] == 0 && !(tmp.c[a+1] & 0X80)) ||
76             (tmp.c[a] == 0XFF && (tmp.c[a+1] & 0X80))))
77             break;
78     len = sizeof(int) - a;
79     if (odr_write(o, (unsigned char*) tmp.c + a, len) < 0)
80         return -1;
81     odr_seek(o, ODR_S_SET, lenpos);
82     if (ber_enclen(o, len, 1, 1) != 1)
83         return -1;
84     odr_seek(o, ODR_S_END, 0);
85 #ifdef ODR_DEBUG
86     fprintf(stderr, "[val=%d]", val);
87 #endif
88     return 0;
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 }