1 | /*
|
---|
2 | * Copyright (c) 1995-2000, Index Data
|
---|
3 | * See the file LICENSE for details.
|
---|
4 | *
|
---|
5 | * $Log$
|
---|
6 | * Revision 1.1 2000/08/03 03:11:22 johnmcp
|
---|
7 | * Added the YAZ toolkit source to the packages directory (for z39.50 stuff)
|
---|
8 | *
|
---|
9 | * Revision 1.18 2000/02/29 13:44:55 adam
|
---|
10 | * Check for config.h (currently not generated).
|
---|
11 | *
|
---|
12 | * Revision 1.17 2000/01/31 13:15:21 adam
|
---|
13 | * Removed uses of assert(3). Cleanup of ODR. CCL parser update so
|
---|
14 | * that some characters are not surrounded by spaces in resulting term.
|
---|
15 | * ILL-code updates.
|
---|
16 | *
|
---|
17 | * Revision 1.16 1999/11/30 13:47:11 adam
|
---|
18 | * Improved installation. Moved header files to include/yaz.
|
---|
19 | *
|
---|
20 | * Revision 1.15 1999/03/31 11:18:25 adam
|
---|
21 | * Implemented odr_strdup. Added Reference ID to backend server API.
|
---|
22 | *
|
---|
23 | * Revision 1.14 1998/07/20 12:38:15 adam
|
---|
24 | * More LOG_DEBUG-diagnostics.
|
---|
25 | *
|
---|
26 | * Revision 1.13 1998/02/11 11:53:34 adam
|
---|
27 | * Changed code so that it compiles as C++.
|
---|
28 | *
|
---|
29 | * Revision 1.12 1995/11/08 17:41:33 quinn
|
---|
30 | * Smallish.
|
---|
31 | *
|
---|
32 | * Revision 1.11 1995/11/01 13:54:43 quinn
|
---|
33 | * Minor adjustments
|
---|
34 | *
|
---|
35 | * Revision 1.10 1995/10/25 16:58:19 quinn
|
---|
36 | * Stupid bug in odr_malloc
|
---|
37 | *
|
---|
38 | * Revision 1.9 1995/10/13 16:08:08 quinn
|
---|
39 | * Added OID utility
|
---|
40 | *
|
---|
41 | * Revision 1.8 1995/09/29 17:12:24 quinn
|
---|
42 | * Smallish
|
---|
43 | *
|
---|
44 | * Revision 1.7 1995/09/27 15:02:59 quinn
|
---|
45 | * Modified function heads & prototypes.
|
---|
46 | *
|
---|
47 | * Revision 1.6 1995/08/21 09:10:41 quinn
|
---|
48 | * Smallish fixes to suppport new formats.
|
---|
49 | *
|
---|
50 | * Revision 1.5 1995/05/16 08:50:55 quinn
|
---|
51 | * License, documentation, and memory fixes
|
---|
52 | *
|
---|
53 | * Revision 1.4 1995/05/15 11:56:09 quinn
|
---|
54 | * More work on memory management.
|
---|
55 | *
|
---|
56 | * Revision 1.3 1995/04/18 08:15:21 quinn
|
---|
57 | * Added dynamic memory allocation on encoding (whew). Code is now somewhat
|
---|
58 | * neater. We'll make the same change for decoding one day.
|
---|
59 | *
|
---|
60 | * Revision 1.2 1995/03/17 10:17:52 quinn
|
---|
61 | * Added memory management.
|
---|
62 | *
|
---|
63 | * Revision 1.1 1995/03/14 10:27:40 quinn
|
---|
64 | * Modified makefile to use common lib
|
---|
65 | * Beginning to add memory management to odr
|
---|
66 | *
|
---|
67 | */
|
---|
68 | #if HAVE_CONFIG_H
|
---|
69 | #include <config.h>
|
---|
70 | #endif
|
---|
71 |
|
---|
72 | #include <stdlib.h>
|
---|
73 | #include <yaz/odr.h>
|
---|
74 | #include <yaz/xmalloc.h>
|
---|
75 |
|
---|
76 | /* ------------------------ NIBBLE MEMORY ---------------------- */
|
---|
77 |
|
---|
78 | /*
|
---|
79 | * Extract the memory control block from o.
|
---|
80 | */
|
---|
81 | NMEM odr_extract_mem(ODR o)
|
---|
82 | {
|
---|
83 | NMEM r = o->mem;
|
---|
84 |
|
---|
85 | o->mem = 0;
|
---|
86 | return r;
|
---|
87 | }
|
---|
88 |
|
---|
89 | void *odr_malloc(ODR o, int size)
|
---|
90 | {
|
---|
91 | if (o && !o->mem)
|
---|
92 | o->mem = nmem_create();
|
---|
93 | return nmem_malloc(o ? o->mem : 0, size);
|
---|
94 | }
|
---|
95 |
|
---|
96 | char *odr_strdup(ODR o, const char *str)
|
---|
97 | {
|
---|
98 | return nmem_strdup(o->mem, str);
|
---|
99 | }
|
---|
100 |
|
---|
101 | int odr_total(ODR o)
|
---|
102 | {
|
---|
103 | return o->mem ? nmem_total(o->mem) : 0;
|
---|
104 | }
|
---|
105 |
|
---|
106 | /* ---------- memory management for data encoding ----------*/
|
---|
107 |
|
---|
108 |
|
---|
109 | int odr_grow_block(ODR b, int min_bytes)
|
---|
110 | {
|
---|
111 | int togrow;
|
---|
112 |
|
---|
113 | if (!b->can_grow)
|
---|
114 | return -1;
|
---|
115 | if (!b->size)
|
---|
116 | togrow = 1024;
|
---|
117 | else
|
---|
118 | togrow = b->size;
|
---|
119 | if (togrow < min_bytes)
|
---|
120 | togrow = min_bytes;
|
---|
121 | if (b->size && !(b->buf =
|
---|
122 | (unsigned char *) xrealloc(b->buf, b->size += togrow)))
|
---|
123 | abort();
|
---|
124 | else if (!b->size && !(b->buf = (unsigned char *)
|
---|
125 | xmalloc(b->size = togrow)))
|
---|
126 | abort();
|
---|
127 | #ifdef ODR_DEBUG
|
---|
128 | fprintf(stderr, "New size for encode_buffer: %d\n", b->size);
|
---|
129 | #endif
|
---|
130 | return 0;
|
---|
131 | }
|
---|
132 |
|
---|
133 | int odr_write(ODR o, unsigned char *buf, int bytes)
|
---|
134 | {
|
---|
135 | if (o->pos + bytes >= o->size && odr_grow_block(o, bytes))
|
---|
136 | {
|
---|
137 | o->error = OSPACE;
|
---|
138 | return -1;
|
---|
139 | }
|
---|
140 | memcpy(o->buf + o->pos, buf, bytes);
|
---|
141 | o->pos += bytes;
|
---|
142 | if (o->pos > o->top)
|
---|
143 | o->top = o->pos;
|
---|
144 | return 0;
|
---|
145 | }
|
---|
146 |
|
---|
147 | int odr_seek(ODR o, int whence, int offset)
|
---|
148 | {
|
---|
149 | if (whence == ODR_S_CUR)
|
---|
150 | offset += o->pos;
|
---|
151 | else if (whence == ODR_S_END)
|
---|
152 | offset += o->top;
|
---|
153 | if (offset > o->size && odr_grow_block(o, offset - o->size))
|
---|
154 | {
|
---|
155 | o->error = OSPACE;
|
---|
156 | return -1;
|
---|
157 | }
|
---|
158 | o->pos = offset;
|
---|
159 | return 0;
|
---|
160 | }
|
---|