2 * Copyright (C) 1994, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
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.
11 * Revision 1.5 1995/03/27 15:01:44 quinn
12 * Added include of sys/types to further portability
14 * Revision 1.4 1995/03/08 12:12:07 quinn
15 * Added better error checking.
17 * Revision 1.3 1995/02/09 15:51:46 quinn
20 * Revision 1.2 1995/02/07 17:52:58 quinn
21 * A damn mess, but now things work, I think.
23 * Revision 1.1 1995/02/02 16:21:52 quinn
29 #include <sys/types.h>
30 #include <netinet/in.h> /* for htons... */
33 static int ber_encinteger(ODR o, int val);
34 static int ber_decinteger(unsigned char *buf, int *val);
36 int ber_integer(ODR o, int *val)
43 if ((res = ber_decinteger(o->bp, val)) <= 0)
52 if ((res = ber_encinteger(o, *val)) < 0)
55 case ODR_PRINT: return 1;
56 default: o->error = OOTHER; return 0;
61 * Returns: number of bytes written or -1 for error (out of bounds).
63 int ber_encinteger(ODR o, int val)
67 union { int i; unsigned char c[sizeof(int)]; } tmp;
70 if (odr_putc(o, 0) < 0) /* dummy */
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))))
78 len = sizeof(int) - a;
79 if (odr_write(o, (unsigned char*) tmp.c + a, len) < 0)
81 odr_seek(o, ODR_S_SET, lenpos);
82 if (ber_enclen(o, len, 1, 1) != 1)
84 odr_seek(o, ODR_S_END, 0);
86 fprintf(stderr, "[val=%d]", val);
92 * Returns: Number of bytes read or 0 if no match, -1 if error.
94 int ber_decinteger(unsigned char *buf, int *val)
96 unsigned char *b = buf, fill;
97 int res, len, remains;
98 union { int i; unsigned char c[sizeof(int)]; } tmp;
100 if ((res = ber_declen(b, &len)) < 0)
102 if (len > sizeof(int)) /* let's be reasonable, here */
106 remains = sizeof(int) - len;
107 memcpy(tmp.c + remains, b, len);
112 memset(tmp.c, fill, remains);
117 fprintf(stderr, "[val=%d]", *val);