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