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