2 * Copyright (c) 1995, Index Data
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.13 1998-02-11 11:53:34 adam
8 * Changed code so that it compiles as C++.
10 * Revision 1.12 1995/11/08 17:41:33 quinn
13 * Revision 1.11 1995/11/01 13:54:43 quinn
16 * Revision 1.10 1995/10/25 16:58:19 quinn
17 * Stupid bug in odr_malloc
19 * Revision 1.9 1995/10/13 16:08:08 quinn
22 * Revision 1.8 1995/09/29 17:12:24 quinn
25 * Revision 1.7 1995/09/27 15:02:59 quinn
26 * Modified function heads & prototypes.
28 * Revision 1.6 1995/08/21 09:10:41 quinn
29 * Smallish fixes to suppport new formats.
31 * Revision 1.5 1995/05/16 08:50:55 quinn
32 * License, documentation, and memory fixes
34 * Revision 1.4 1995/05/15 11:56:09 quinn
35 * More work on memory management.
37 * Revision 1.3 1995/04/18 08:15:21 quinn
38 * Added dynamic memory allocation on encoding (whew). Code is now somewhat
39 * neater. We'll make the same change for decoding one day.
41 * Revision 1.2 1995/03/17 10:17:52 quinn
42 * Added memory management.
44 * Revision 1.1 1995/03/14 10:27:40 quinn
45 * Modified makefile to use common lib
46 * Beginning to add memory management to odr
54 /* ------------------------ NIBBLE MEMORY ---------------------- */
57 * Extract the memory control block from o.
59 NMEM odr_extract_mem(ODR o)
67 void *odr_malloc(ODR o, int size)
70 o->mem = nmem_create();
71 return nmem_malloc(o ? o->mem : 0, size);
76 return o->mem ? nmem_total(o->mem) : 0;
79 /* ---------- memory management for data encoding ----------*/
82 int odr_grow_block(odr_ecblock *b, int min_bytes)
92 if (togrow < min_bytes)
94 if (b->size && !(b->buf =(unsigned char *)xrealloc(b->buf, b->size += togrow)))
96 else if (!b->size && !(b->buf = (unsigned char *)xmalloc(b->size = togrow)))
99 fprintf(stderr, "New size for encode_buffer: %d\n", b->size);
104 int odr_write(ODR o, unsigned char *buf, int bytes)
106 if (o->ecb.pos + bytes >= o->ecb.size && odr_grow_block(&o->ecb, bytes))
111 memcpy(o->ecb.buf + o->ecb.pos, buf, bytes);
113 if (o->ecb.pos > o->ecb.top)
114 o->ecb.top = o->ecb.pos;
118 int odr_seek(ODR o, int whence, int offset)
120 if (whence == ODR_S_CUR)
121 offset += o->ecb.pos;
122 else if (whence == ODR_S_END)
123 offset += o->ecb.top;
124 if (offset > o->ecb.size && odr_grow_block(&o->ecb, offset - o->ecb.size))