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