00001
00002
00003
00004
00009 #if HAVE_CONFIG_H
00010 #include <config.h>
00011 #endif
00012
00013 #include <stdlib.h>
00014 #include "odr-priv.h"
00015 #include <yaz/xmalloc.h>
00016
00017
00018
00019
00020
00021
00022 NMEM odr_extract_mem(ODR o)
00023 {
00024 NMEM r = o->mem;
00025
00026 o->mem = nmem_create();
00027 return r;
00028 }
00029
00030 void *odr_malloc(ODR o, int size)
00031 {
00032 return nmem_malloc(o->mem, size);
00033 }
00034
00035 char *odr_strdup(ODR o, const char *str)
00036 {
00037 return nmem_strdup(o->mem, str);
00038 }
00039
00040 char *odr_strdup_null(ODR o, const char *str)
00041 {
00042 return nmem_strdup_null(o->mem, str);
00043 }
00044
00045 char *odr_strdupn(ODR o, const char *str, size_t n)
00046 {
00047 return nmem_strdupn(o->mem, str, n);
00048 }
00049
00050 int *odr_intdup(ODR o, int v)
00051 {
00052 return nmem_intdup(o->mem, v);
00053 }
00054
00055 int odr_total(ODR o)
00056 {
00057 return nmem_total(o->mem);
00058 }
00059
00060 Odr_oct *odr_create_Odr_oct(ODR o, const unsigned char *buf, int sz)
00061 {
00062 Odr_oct *p = (Odr_oct *) odr_malloc(o, sizeof(Odr_oct));
00063 p->buf = (unsigned char *) odr_malloc(o, sz);
00064 memcpy(p->buf, buf, sz);
00065 p->size = sz;
00066 p->len = sz;
00067 return p;
00068 }
00069
00070
00071
00072
00073 int odr_grow_block(ODR b, int min_bytes)
00074 {
00075 int togrow;
00076
00077 if (!b->op->can_grow)
00078 return -1;
00079 if (!b->size)
00080 togrow = 1024;
00081 else
00082 togrow = b->size;
00083 if (togrow < min_bytes)
00084 togrow = min_bytes;
00085 if (b->size && !(b->buf =
00086 (unsigned char *) xrealloc(b->buf, b->size += togrow)))
00087 abort();
00088 else if (!b->size && !(b->buf = (unsigned char *)
00089 xmalloc(b->size = togrow)))
00090 abort();
00091 return 0;
00092 }
00093
00094 int odr_write(ODR o, unsigned char *buf, int bytes)
00095 {
00096 if (o->pos + bytes >= o->size && odr_grow_block(o, bytes))
00097 {
00098 odr_seterror(o, OSPACE, 40);
00099 return -1;
00100 }
00101 memcpy(o->buf + o->pos, buf, bytes);
00102 o->pos += bytes;
00103 if (o->pos > o->top)
00104 o->top = o->pos;
00105 return 0;
00106 }
00107
00108 int odr_seek(ODR o, int whence, int offset)
00109 {
00110 if (whence == ODR_S_CUR)
00111 offset += o->pos;
00112 else if (whence == ODR_S_END)
00113 offset += o->top;
00114 if (offset > o->size && odr_grow_block(o, offset - o->size))
00115 {
00116 odr_seterror(o, OSPACE, 41);
00117 return -1;
00118 }
00119 o->pos = offset;
00120 return 0;
00121 }
00122
00123
00124
00125
00126
00127
00128
00129