Works better now.
[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.5  1995-02-09 15:51:49  quinn
8  * Works better now.
9  *
10  * Revision 1.4  1995/02/07  14:13:46  quinn
11  * Bug fixes.
12  *
13  * Revision 1.3  1995/02/03  17:04:38  quinn
14  * *** empty log message ***
15  *
16  * Revision 1.2  1995/02/02  20:38:51  quinn
17  * Updates.
18  *
19  * Revision 1.1  1995/02/02  16:21:54  quinn
20  * First kick.
21  *
22  */
23
24 #include <odr.h>
25
26 /*
27  * Top level octet string en/decoder.
28  * Returns 1 on success, 0 on error.
29  */
30 int odr_octetstring(ODR o, Odr_oct **p, int opt)
31 {
32     int res, cons = 0;
33
34     if (o->t_class < 0)
35     {
36         o->t_class = ODR_UNIVERSAL;
37         o->t_tag = ODR_OCTETSTRING;
38     }
39     if (o->direction == ODR_DECODE)
40         *p = 0;
41     if ((res = ber_tag(o, *p, o->t_class, o->t_tag, &cons)) < 0)
42     {
43         *p = 0;
44         return 0;
45     }
46     if (!res)
47     {
48         *p = 0;
49         return opt;
50     }
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     *p = 0;
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->t_class < 0)
78     {
79         o->t_class = ODR_UNIVERSAL;
80         o->t_tag = ODR_OCTETSTRING;
81     }
82     if (o->direction == ODR_DECODE)
83         *p = 0;
84     if ((res = ber_tag(o, *p, o->t_class, o->t_tag, &cons)) < 0)
85         return 0;
86     if (!res)
87     {
88         *p = 0;
89         return opt;
90     }
91     if (o->direction == ODR_PRINT)
92     {
93         fprintf(o->print, "%s'%s'\n", odr_indent(o), *p);
94         return 1;
95     }
96     t = nalloc(o, sizeof(Odr_oct));   /* wrapper for octstring */
97     if (o->direction == ODR_ENCODE)
98     {
99         t->buf = (unsigned char *) *p;
100         t->size = t->len = strlen(*p);
101     }
102     else
103     {
104         t->size= 0;
105         t->len = 0;
106         t->buf = 0;
107     }
108     if (!ber_octetstring(o, t, cons))
109         return 0;
110     if (o->direction == ODR_DECODE)
111     {
112         *p = (char *) t->buf;
113         *(*p + t->len) = '\0';  /* ber_octs reserves space for this */
114     }
115     return 1;
116 }