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