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