cfa28cc38fc3ddd7b95c5d631f1ce86c2e400469
[yaz-moved-to-github.git] / odr / odr_util.c
1 #include <stdlib.h>
2 #include <string.h>
3 #include <ctype.h>
4 #include <odr.h>
5 #include <oid.h>
6
7 char *odr_indent(ODR o)
8 {
9     static char buf[512];
10     int i = o->indent;
11
12     memset(buf, ' ', 512);
13     if (i >= 128)
14         i = 127;
15     buf[o->indent * 4] = 0;
16     return buf;
17 }
18
19 int odp_more_chunks(ODR o, unsigned char *base, int len)
20 {
21     if (!len)
22         return 0;
23     if (len < 0) /* indefinite length */
24     {
25         if (*o->bp == 0 && *(o->bp + 1) == 0)
26         {
27             o->bp += 2;
28             o->left -= 2;
29             return 0;
30         }
31         else
32             return 1;
33     }
34     else
35         return o->bp - base < len;
36 }
37
38 Odr_oid *odr_oiddup(ODR odr, Odr_oid *o)
39 {
40     Odr_oid *r;
41
42     if (!o)
43         return 0;
44     if (!(r = odr_malloc(odr, (oid_oidlen(o) + 1) * sizeof(int))))
45         return 0;
46     oid_oidcpy(r, o);
47     return r;
48 }
49
50 Odr_oid *odr_getoidbystr(ODR o, char *str)
51 {
52     int num = 1, i = 0;
53     char *p = str;
54     Odr_oid *ret;
55
56     if (!isdigit(*str))
57         return 0;
58     while ((p = strchr(p, '.')))
59         num++, p++;
60     ret = odr_malloc(o, sizeof(*ret)*(num + 1));
61     p = str;
62     do
63         ret[i++] = atoi(p);
64     while ((p = strchr(p, '.')) && ++p);
65     ret[i] = -1;
66     return ret;
67 }