+
+/* ---------- memory management for data encoding ----------*/
+
+
+int odr_grow_block(odr_ecblock *b, int min_bytes)
+{
+ int togrow;
+
+ if (!b->can_grow)
+ return -1;
+ if (!b->size)
+ togrow = 1024;
+ else
+ togrow = b->size;
+ if (togrow < min_bytes)
+ togrow = min_bytes;
+ if (b->size && !(b->buf = realloc(b->buf, b->size += togrow)))
+ abort();
+ else if (!b->size && !(b->buf = malloc(b->size = togrow)))
+ abort();
+#ifdef ODR_DEBUG
+ fprintf(stderr, "New size for encode_buffer: %d\n", b->size);
+#endif
+ return 0;
+}
+
+int odr_write(ODR o, unsigned char *buf, int bytes)
+{
+ if (o->ecb.pos + bytes >= o->ecb.size && odr_grow_block(&o->ecb, bytes))
+ {
+ o->error = OSPACE;
+ return -1;
+ }
+ memcpy(o->ecb.buf + o->ecb.pos, buf, bytes);
+ o->ecb.pos += bytes;
+ if (o->ecb.pos > o->ecb.top)
+ o->ecb.top = o->ecb.pos;
+ return 0;
+}
+
+int odr_seek(ODR o, int whence, int offset)
+{
+ if (whence == ODR_S_CUR)
+ offset += o->ecb.pos;
+ else if (whence == ODR_S_END)
+ offset += o->ecb.top;
+ if (offset > o->ecb.size && odr_grow_block(&o->ecb, offset - o->ecb.size))
+ {
+ o->error = OSPACE;
+ return -1;
+ }
+ o->ecb.pos = offset;
+ return 0;
+}