2 * Copyright (c) 1995-2002, Index Data
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
6 * $Id: ber_int.c,v 1.20 2002-07-25 12:51:08 adam Exp $
17 #include <sys/types.h>
18 #include <netinet/in.h>
23 static int ber_encinteger(ODR o, int val);
24 static int ber_decinteger(const unsigned char *buf, int *val);
26 int ber_integer(ODR o, int *val)
33 if ((res = ber_decinteger(o->bp, val)) <= 0)
41 if ((res = ber_encinteger(o, *val)) < 0)
44 case ODR_PRINT: return 1;
45 default: o->error = OOTHER; return 0;
50 * Returns: number of bytes written or -1 for error (out of bounds).
52 int ber_encinteger(ODR o, int val)
56 union { int i; unsigned char c[sizeof(int)]; } tmp;
59 if (odr_putc(o, 0) < 0) /* dummy */
61 tmp.i = htonl(val); /* ensure that that we're big-endian */
62 for (a = 0; a < (int) sizeof(int) - 1; a++) /* skip superfluous octets */
63 if (!((tmp.c[a] == 0 && !(tmp.c[a+1] & 0X80)) ||
64 (tmp.c[a] == 0XFF && (tmp.c[a+1] & 0X80))))
66 len = sizeof(int) - a;
67 if (odr_write(o, (unsigned char*) tmp.c + a, len) < 0)
69 odr_seek(o, ODR_S_SET, lenpos);
70 if (ber_enclen(o, len, 1, 1) != 1)
72 odr_seek(o, ODR_S_END, 0);
74 fprintf(stderr, "[val=%d]", val);
80 * Returns: Number of bytes read or 0 if no match, -1 if error.
82 int ber_decinteger(const unsigned char *buf, int *val)
84 const unsigned char *b = buf;
86 int res, len, remains;
87 union { int i; unsigned char c[sizeof(int)]; } tmp;
89 if ((res = ber_declen(b, &len)) < 0)
91 if (len > (int) sizeof(int)) /* let's be reasonable, here */
95 remains = sizeof(int) - len;
96 memcpy(tmp.c + remains, b, len);
101 memset(tmp.c, fill, remains);
106 fprintf(stderr, "[val=%d]", *val);