2 * Copyright (C) 1994, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.5 1995-03-27 15:01:44 quinn
8 * Added include of sys/types to further portability
10 * Revision 1.4 1995/03/08 12:12:07 quinn
11 * Added better error checking.
13 * Revision 1.3 1995/02/09 15:51:46 quinn
16 * Revision 1.2 1995/02/07 17:52:58 quinn
17 * A damn mess, but now things work, I think.
19 * Revision 1.1 1995/02/02 16:21:52 quinn
25 #include <sys/types.h>
26 #include <netinet/in.h> /* for htons... */
29 static int ber_encinteger(unsigned char *buf, int val, int maxlen);
30 static int ber_decinteger(unsigned char *buf, int *val);
32 int ber_integer(ODR o, int *val)
39 if ((res = ber_decinteger(o->bp, val)) <= 0)
48 if ((res = ber_encinteger(o->bp, *val, o->left)) <= 0)
56 case ODR_PRINT: return 1;
57 default: o->error = OOTHER; return 0;
62 * Returns: number of bytes written or -1 for error (out of bounds).
64 int ber_encinteger(unsigned char *buf, int val, int maxlen)
66 unsigned char *b = buf, *lenpos;
68 union { int i; unsigned char c[sizeof(int)]; } tmp;
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))))
79 if ((len = sizeof(int) - a) > maxlen)
81 memcpy(b, tmp.c + a, len);
83 if (ber_enclen(lenpos, len, 1, 1) != 1)
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);