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