Bug fixes.
[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.4  1995-02-07 14:13:46  quinn
8  * Bug fixes.
9  *
10  * Revision 1.3  1995/02/03  17:04:38  quinn
11  * *** empty log message ***
12  *
13  * Revision 1.2  1995/02/02  20:38:51  quinn
14  * Updates.
15  *
16  * Revision 1.1  1995/02/02  16:21:54  quinn
17  * First kick.
18  *
19  */
20
21 #include <odr.h>
22
23 /*
24  * Top level octet string en/decoder.
25  * Returns 1 on success, 0 on error.
26  */
27 int odr_octetstring(ODR o, Odr_oct **p, int opt)
28 {
29     int res, cons = 0;
30
31     if (o->t_class < 0)
32     {
33         o->t_class = ODR_UNIVERSAL;
34         o->t_tag = ODR_OCTETSTRING;
35     }
36     if ((res = ber_tag(o, *p, o->t_class, o->t_tag, &cons)) < 0)
37     {
38         *p = 0;
39         return 0;
40     }
41     if (!res)
42     {
43         *p = 0;
44         return opt;
45     }
46     if (o->direction == ODR_PRINT)
47     {
48         fprintf(o->print, "OCTETSTRING(len=%d)\n", (*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     return ber_octetstring(o, *p, cons);
59 }
60
61 /*
62  * Friendlier interface to octetstring.
63  */
64 int odr_cstring(ODR o, char **p, int opt)
65 {
66     int cons = 0, res;
67     Odr_oct *t;
68
69     if (o->t_class < 0)
70     {
71         o->t_class = ODR_UNIVERSAL;
72         o->t_tag = ODR_OCTETSTRING;
73     }
74     if ((res = ber_tag(o, *p, o->t_class, o->t_tag, &cons)) < 0)
75         return 0;
76     if (!res)
77     {
78         *p = 0;
79         return opt;
80     }
81     if (o->direction == ODR_PRINT)
82     {
83         fprintf(o->print, "'%s'\n", *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 }