1 | /*
|
---|
2 | * Copyright (c) 1995-2000, Index Data
|
---|
3 | * See the file LICENSE for details.
|
---|
4 | * Sebastian Hammer, Adam Dickmeiss
|
---|
5 | *
|
---|
6 | * $Log$
|
---|
7 | * Revision 1.1 2000/08/03 03:11:16 johnmcp
|
---|
8 | * Added the YAZ toolkit source to the packages directory (for z39.50 stuff)
|
---|
9 | *
|
---|
10 | * Revision 1.12 2000/02/29 13:44:55 adam
|
---|
11 | * Check for config.h (currently not generated).
|
---|
12 | *
|
---|
13 | * Revision 1.11 2000/01/31 13:15:21 adam
|
---|
14 | * Removed uses of assert(3). Cleanup of ODR. CCL parser update so
|
---|
15 | * that some characters are not surrounded by spaces in resulting term.
|
---|
16 | * ILL-code updates.
|
---|
17 | *
|
---|
18 | * Revision 1.10 1999/11/30 13:47:11 adam
|
---|
19 | * Improved installation. Moved header files to include/yaz.
|
---|
20 | *
|
---|
21 | * Revision 1.9 1995/09/29 17:12:19 quinn
|
---|
22 | * Smallish
|
---|
23 | *
|
---|
24 | * Revision 1.8 1995/09/27 15:02:56 quinn
|
---|
25 | * Modified function heads & prototypes.
|
---|
26 | *
|
---|
27 | * Revision 1.7 1995/05/16 08:50:47 quinn
|
---|
28 | * License, documentation, and memory fixes
|
---|
29 | *
|
---|
30 | * Revision 1.6 1995/04/18 08:15:18 quinn
|
---|
31 | * Added dynamic memory allocation on encoding (whew). Code is now somewhat
|
---|
32 | * neater. We'll make the same change for decoding one day.
|
---|
33 | *
|
---|
34 | * Revision 1.5 1995/03/20 12:18:22 quinn
|
---|
35 | * Fixed bug in ber_oid
|
---|
36 | *
|
---|
37 | * Revision 1.4 1995/03/08 12:12:11 quinn
|
---|
38 | * Added better error checking.
|
---|
39 | *
|
---|
40 | * Revision 1.3 1995/03/01 08:40:56 quinn
|
---|
41 | * Smallish changes.
|
---|
42 | *
|
---|
43 | * Revision 1.2 1995/02/14 20:39:55 quinn
|
---|
44 | * Fixed bugs in completeBER and (serious one in) ber_oid.
|
---|
45 | *
|
---|
46 | * Revision 1.1 1995/02/03 17:04:36 quinn
|
---|
47 | * Initial revision
|
---|
48 | *
|
---|
49 | */
|
---|
50 | #if HAVE_CONFIG_H
|
---|
51 | #include <config.h>
|
---|
52 | #endif
|
---|
53 |
|
---|
54 | #include <yaz/odr.h>
|
---|
55 |
|
---|
56 | int ber_oidc(ODR o, Odr_oid *p)
|
---|
57 | {
|
---|
58 | int len, lenp, end;
|
---|
59 | int pos, n, res, id;
|
---|
60 | unsigned char octs[8];
|
---|
61 |
|
---|
62 | switch (o->direction)
|
---|
63 | {
|
---|
64 | case ODR_DECODE:
|
---|
65 | if ((res = ber_declen(o->bp, &len)) < 1)
|
---|
66 | {
|
---|
67 | o->error = OPROTO;
|
---|
68 | return 0;
|
---|
69 | }
|
---|
70 | if (len < 0)
|
---|
71 | {
|
---|
72 | o->error = OPROTO;
|
---|
73 | return 0;
|
---|
74 | }
|
---|
75 | o->bp += res;
|
---|
76 | if (len == 0)
|
---|
77 | {
|
---|
78 | *p = -1;
|
---|
79 | return 1;
|
---|
80 | }
|
---|
81 | p[0] = *o->bp / 40;
|
---|
82 | if (p[0] > 2)
|
---|
83 | p[0] = 2;
|
---|
84 | p[1] = *o->bp - p[0] * 40;
|
---|
85 | o->bp++;
|
---|
86 | pos = 2;
|
---|
87 | len--;
|
---|
88 | while (len)
|
---|
89 | {
|
---|
90 | p[pos] = 0;
|
---|
91 | do
|
---|
92 | {
|
---|
93 | if (!len)
|
---|
94 | {
|
---|
95 | o->error = OPROTO;
|
---|
96 | return 0;
|
---|
97 | }
|
---|
98 | p[pos] <<= 7;
|
---|
99 | p[pos] |= *o->bp & 0X7F;
|
---|
100 | len--;
|
---|
101 | }
|
---|
102 | while (*(o->bp++) & 0X80);
|
---|
103 | pos++;
|
---|
104 | }
|
---|
105 | p[pos] = -1;
|
---|
106 | return 1;
|
---|
107 | case ODR_ENCODE:
|
---|
108 | /* we'll allow ourselves the quiet luxury of only doing encodings
|
---|
109 | shorter than 127 */
|
---|
110 | lenp = odr_tell(o);
|
---|
111 | if (odr_putc(o, 0) < 0) /* dummy */
|
---|
112 | return 0;
|
---|
113 | if (p[0] < 0 && p[1] <= 0)
|
---|
114 | {
|
---|
115 | o->error = ODATA;
|
---|
116 | return 0;
|
---|
117 | }
|
---|
118 | for (pos = 1; p[pos] >= 0; pos++)
|
---|
119 | {
|
---|
120 | id = pos > 1 ? p[pos] : p[0] * 40 + p[1];
|
---|
121 | n = 0;
|
---|
122 | do
|
---|
123 | {
|
---|
124 | octs[n++] = id & 0X7F;
|
---|
125 | id >>= 7;
|
---|
126 | }
|
---|
127 | while (id);
|
---|
128 | while (n--)
|
---|
129 | {
|
---|
130 | unsigned char p;
|
---|
131 |
|
---|
132 | p = octs[n] | ((n > 0) << 7);
|
---|
133 | if (odr_putc(o, p) < 0)
|
---|
134 | return 0;
|
---|
135 | }
|
---|
136 | }
|
---|
137 | end = odr_tell(o);
|
---|
138 | odr_seek(o, ODR_S_SET, lenp);
|
---|
139 | if (ber_enclen(o, (end - lenp) - 1, 1, 1) != 1)
|
---|
140 | {
|
---|
141 | o->error = OOTHER;
|
---|
142 | return 0;
|
---|
143 | }
|
---|
144 | odr_seek(o, ODR_S_END, 0);
|
---|
145 | return 1;
|
---|
146 | default: o->error = OOTHER; return 0;
|
---|
147 | }
|
---|
148 | }
|
---|