1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) Index Data
3 * See the file LICENSE for details.
4 */
5
6 /**
7 * \file ber_oct.c
8 * \brief Implements ber_octetstring
9 *
10 * This source file implements BER encoding and decoding of
11 * the OCTETSTRING type.
12 */
13
14 #if HAVE_CONFIG_H
15 #include <config.h>
16 #endif
17
18 #include "odr-priv.h"
19 #include <yaz/log.h>
20 #include <assert.h>
21
ber_octetstring(ODR o,Odr_oct * p,int cons)22 int ber_octetstring(ODR o, Odr_oct *p, int cons)
23 {
24 int res, len;
25 const char *base;
26
27 switch (o->direction)
28 {
29 case ODR_DECODE:
30 if ((res = ber_declen(o->op->bp, &len, odr_max(o))) < 0)
31 {
32 odr_seterror(o, OPROTO, 14);
33 return 0;
34 }
35 o->op->bp += res;
36 if (cons) /* fetch component strings */
37 {
38 base = o->op->bp;
39 while (odp_more_chunks(o, base, len))
40 if (!odr_octetstring(o, &p, 0, 0))
41 return 0;
42 return 1;
43 }
44 /* primitive octetstring */
45 if (len < 0)
46 {
47 odr_seterror(o, OOTHER, 15);
48 return 0;
49 }
50 if (len > odr_max(o))
51 {
52 odr_seterror(o, OOTHER, 16);
53 return 0;
54 }
55 p->len = len;
56 p->buf = odr_strdupn(o, o->op->bp, len);
57 o->op->bp += len;
58 return 1;
59 case ODR_ENCODE:
60 if ((res = ber_enclen(o, p->len, 5, 0)) < 0)
61 return 0;
62 if (p->len == 0)
63 return 1;
64 if (odr_write(o, p->buf, p->len) < 0)
65 return 0;
66 return 1;
67 case ODR_PRINT:
68 return 1;
69 default:
70 odr_seterror(o, OOTHER, 17);
71 return 0;
72 }
73 }
74 /*
75 * Local variables:
76 * c-basic-offset: 4
77 * c-file-style: "Stroustrup"
78 * indent-tabs-mode: nil
79 * End:
80 * vim: shiftwidth=4 tabstop=8 expandtab
81 */
82
83