0d22c09acc299e2f37b929412a5c3d45b30a3821
[yaz-moved-to-github.git] / src / odr-priv.h
1 /*
2  * Copyright (c) 1995-2004, Index Data.
3  *
4  * Permission to use, copy, modify, distribute, and sell this software and
5  * its documentation, in whole or in part, for any purpose, is hereby granted,
6  * provided that:
7  *
8  * 1. This copyright and permission notice appear in all copies of the
9  * software and its documentation. Notices of copyright or attribution
10  * which appear at the beginning of any file must remain unchanged.
11  *
12  * 2. The name of Index Data or the individual authors may not be used to
13  * endorse or promote products derived from this software without specific
14  * prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS, IMPLIED, OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
18  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
19  * IN NO EVENT SHALL INDEX DATA BE LIABLE FOR ANY SPECIAL, INCIDENTAL,
20  * INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES
21  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER OR
22  * NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
23  * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
24  * OF THIS SOFTWARE.
25  *
26  * $Id: odr-priv.h,v 1.3 2004-08-13 07:30:06 adam Exp $
27  */
28
29 #ifndef ODR_PRIV_H
30
31 #define ODR_PRIV_H
32
33 #include <yaz/odr.h>
34 #include <yaz/yaz-util.h>
35
36 struct Odr_ber_tag {      /* used to be statics in ber_tag... */
37     int lclass;
38     int ltag;
39     int br;
40     int lcons;
41 };
42
43 #define odr_max(o) ((o)->size - ((o)->bp - (o)->buf))
44 #define odr_offset(o) ((o)->bp - (o)->buf)
45
46 typedef struct odr_constack
47 {
48     const unsigned char *base;   /* starting point of data */
49     int base_offset;
50     int len;                     /* length of data, if known, else -1
51                                         (decoding only) */
52     const unsigned char *lenb;   /* where to encode length */
53     int len_offset;
54     int lenlen;                  /* length of length-field */
55 } odr_constack;
56
57 struct Odr_private {
58     /* stack for constructed types */
59 #define ODR_MAX_STACK 50
60     int stackp;          /* top of stack (-1 == initial state) */
61     odr_constack stack[ODR_MAX_STACK];
62     const char *stack_names[1 + ODR_MAX_STACK];
63
64     struct Odr_ber_tag odr_ber_tag;
65     yaz_iconv_t iconv_handle;
66     int error_id;
67     char element[80];
68     void (*stream_write)(ODR o, void *handle, int type,
69                          const char *buf, int len);
70     void (*stream_close)(void *handle);
71 };
72
73 /* Private macro.
74  * write a single character at the current position - grow buffer if
75  * necessary.
76  * (no, we're not usually this anal about our macros, but this baby is
77  *  next to unreadable without some indentation  :)
78  */
79 #define odr_putc(o, c) \
80 ( \
81     ( \
82         (o)->pos < (o)->size ? \
83         ( \
84             (o)->buf[(o)->pos++] = (c), \
85             0 \
86         ) : \
87         ( \
88             odr_grow_block((o), 1) == 0 ? \
89             ( \
90                 (o)->buf[(o)->pos++] = (c), \
91                 0 \
92             ) : \
93             ( \
94                 (o)->error = OSPACE, \
95                 -1 \
96             ) \
97         ) \
98     ) == 0 ? \
99     ( \
100         (o)->pos > (o)->top ? \
101         ( \
102             (o)->top = (o)->pos, \
103             0 \
104         ) : \
105         0 \
106     ) : \
107         -1 \
108 )
109
110 #endif