c1242d2fe11381f24c2892627bdacecf257b6055
[yaz-moved-to-github.git] / odr / odr_oct.c
1 /*
2  * Copyright (c) 1995, Index Data
3  * See the file LICENSE for details.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: odr_oct.c,v $
7  * Revision 1.9  1995-05-16 08:50:56  quinn
8  * License, documentation, and memory fixes
9  *
10  * Revision 1.8  1995/03/17  10:17:54  quinn
11  * Added memory management.
12  *
13  * Revision 1.7  1995/03/08  12:12:27  quinn
14  * Added better error checking.
15  *
16  * Revision 1.6  1995/02/10  18:57:26  quinn
17  * More in the way of error-checking.
18  *
19  * Revision 1.5  1995/02/09  15:51:49  quinn
20  * Works better now.
21  *
22  * Revision 1.4  1995/02/07  14:13:46  quinn
23  * Bug fixes.
24  *
25  * Revision 1.3  1995/02/03  17:04:38  quinn
26  * *** empty log message ***
27  *
28  * Revision 1.2  1995/02/02  20:38:51  quinn
29  * Updates.
30  *
31  * Revision 1.1  1995/02/02  16:21:54  quinn
32  * First kick.
33  *
34  */
35
36 #include <odr.h>
37
38 /*
39  * Top level octet string en/decoder.
40  * Returns 1 on success, 0 on error.
41  */
42 int odr_octetstring(ODR o, Odr_oct **p, int opt)
43 {
44     int res, cons = 0;
45
46     if (o->error)
47         return 0;
48     if (o->t_class < 0)
49     {
50         o->t_class = ODR_UNIVERSAL;
51         o->t_tag = ODR_OCTETSTRING;
52     }
53     if ((res = ber_tag(o, p, o->t_class, o->t_tag, &cons, opt)) < 0)
54         return 0;
55     if (!res)
56         return opt;
57     if (o->direction == ODR_PRINT)
58     {
59         fprintf(o->print, "%sOCTETSTRING(len=%d)\n", odr_indent(o), (*p)->len);
60         return 1;
61     }
62     if (o->direction == ODR_DECODE)
63     {
64         *p = odr_malloc(o, sizeof(Odr_oct));
65         (*p)->size= 0;
66         (*p)->len = 0;
67         (*p)->buf = 0;
68     }
69     if (ber_octetstring(o, *p, cons))
70         return 1;
71     o->error = OOTHER;
72     return 0;
73 }
74
75 /*
76  * Friendlier interface to octetstring.
77  */
78 int odr_cstring(ODR o, char **p, int opt)
79 {
80     int cons = 0, res;
81     Odr_oct *t;
82
83     if (o->error)
84         return 0;
85     if (o->t_class < 0)
86     {
87         o->t_class = ODR_UNIVERSAL;
88         o->t_tag = ODR_OCTETSTRING;
89     }
90     if ((res = ber_tag(o, p, o->t_class, o->t_tag, &cons, opt)) < 0)
91         return 0;
92     if (!res)
93         return opt;
94     if (o->direction == ODR_PRINT)
95     {
96         fprintf(o->print, "%s'%s'\n", odr_indent(o), *p);
97         return 1;
98     }
99     t = odr_malloc(o, sizeof(Odr_oct));   /* wrapper for octstring */
100     if (o->direction == ODR_ENCODE)
101     {
102         t->buf = (unsigned char *) *p;
103         t->size = t->len = strlen(*p);
104     }
105     else
106     {
107         t->size= 0;
108         t->len = 0;
109         t->buf = 0;
110     }
111     if (!ber_octetstring(o, t, cons))
112         return 0;
113     if (o->direction == ODR_DECODE)
114     {
115         *p = (char *) t->buf;
116         *(*p + t->len) = '\0';  /* ber_octs reserves space for this */
117     }
118     return 1;
119 }