blob: 1e4fe5947ff856a67e32813bbb9da11fa06198ba [file] [log] [blame]
Lev Walkinf15320b2004-06-03 03:38:44 +00001/*-
2 * Copyright (c) 2003, 2004 Lev Walkin <vlm@lionet.info>. All rights reserved.
3 * Redistribution and modifications are permitted subject to BSD license.
4 */
5#include <OBJECT_IDENTIFIER.h>
Lev Walkin29a044b2004-06-14 07:24:36 +00006#include <limits.h> /* for CHAR_BIT */
Lev Walkinf15320b2004-06-03 03:38:44 +00007#include <assert.h>
8#include <errno.h>
9
10/*
11 * OBJECT IDENTIFIER basic type description.
12 */
13static ber_tlv_tag_t asn1_DEF_OBJECT_IDENTIFIER_tags[] = {
14 (ASN_TAG_CLASS_UNIVERSAL | (6 << 2))
15};
16asn1_TYPE_descriptor_t asn1_DEF_OBJECT_IDENTIFIER = {
17 "OBJECT IDENTIFIER",
18 OBJECT_IDENTIFIER_constraint,
19 INTEGER_decode_ber, /* Implemented in terms of INTEGER type */
20 OBJECT_IDENTIFIER_encode_der,
21 OBJECT_IDENTIFIER_print,
22 INTEGER_free,
23 0, /* Use generic outmost tag fetcher */
24 asn1_DEF_OBJECT_IDENTIFIER_tags,
25 sizeof(asn1_DEF_OBJECT_IDENTIFIER_tags)
26 / sizeof(asn1_DEF_OBJECT_IDENTIFIER_tags[0]),
27 1, /* Single UNIVERSAL tag may be implicitly overriden */
Lev Walkind9bd7752004-06-05 08:17:50 +000028 0, /* Always in primitive form */
29 0 /* No specifics */
Lev Walkinf15320b2004-06-03 03:38:44 +000030};
31
32
33/*
34 * Encode OBJECT IDENTIFIER type using DER.
35 */
36der_enc_rval_t
37OBJECT_IDENTIFIER_encode_der(asn1_TYPE_descriptor_t *sd, void *ptr,
38 int tag_mode, ber_tlv_tag_t tag,
39 asn_app_consume_bytes_f *cb, void *app_key) {
40 der_enc_rval_t erval;
41 OBJECT_IDENTIFIER_t *st = ptr;
42
43 ASN_DEBUG("%s %s as OBJECT IDENTIFIER (tm=%d)",
44 cb?"Encoding":"Estimating", sd->name, tag_mode);
45
46 erval.encoded = der_write_tags(sd, st->size, tag_mode, tag,
47 cb, app_key);
48 ASN_DEBUG("OBJECT IDENTIFIER %s wrote tags %d",
49 sd->name, (int)erval.encoded);
50 if(erval.encoded == -1) {
51 erval.failed_type = sd;
52 erval.structure_ptr = ptr;
53 return erval;
54 }
55
56 if(cb && st->buf) {
57 ssize_t ret;
58
59 ret = cb(st->buf, st->size, app_key);
60 if(ret == -1) {
61 erval.encoded = -1;
62 erval.failed_type = sd;
63 erval.structure_ptr = ptr;
64 return erval;
65 }
66 } else {
67 assert(st->buf || st->size == 0);
68 }
69
70 erval.encoded += st->size;
71
72 return erval;
73}
74
75int
76OBJECT_IDENTIFIER_constraint(asn1_TYPE_descriptor_t *td, const void *sptr,
77 asn_app_consume_bytes_f *app_errlog, void *app_key) {
78 const OBJECT_IDENTIFIER_t *st = sptr;
79
80 if(st && st->buf) {
81 if(st->size < 1) {
82 _ASN_ERRLOG("%s: at least one numerical value expected",
83 td->name);
84 return -1;
85 }
86 } else {
87 _ASN_ERRLOG("%s: value not given", td->name);
88 return -1;
89 }
90
91 return 0;
92}
93
Lev Walkinc4c61962004-06-14 08:17:27 +000094
Lev Walkinf15320b2004-06-03 03:38:44 +000095int
Lev Walkin29a044b2004-06-14 07:24:36 +000096OBJECT_IDENTIFIER_get_single_arc(uint8_t *arcbuf, unsigned int arclen, signed int add, void *rvbuf, unsigned int rvsize) {
97 unsigned LE = 1; /* Little endian (x86) */
98 uint8_t *arcend = arcbuf + arclen; /* End of arc */
99 void *rvstart = rvbuf; /* Original start of the value buffer */
100 unsigned int cache = 0; /* No more than 14 significant bits */
101 int inc; /* Return value growth direction */
Lev Walkinf15320b2004-06-03 03:38:44 +0000102
Lev Walkin29a044b2004-06-14 07:24:36 +0000103 rvsize *= CHAR_BIT; /* bytes to bits */
104 arclen *= 7; /* bytes to bits */
105
106 /*
107 * The arc has the number of bits
108 * cannot be represented using supplied return value type.
109 */
110 if(arclen > rvsize) {
111 if(arclen > (rvsize + CHAR_BIT)) {
112 errno = ERANGE; /* Overflow */
113 return -1;
114 } else {
115 /*
116 * Even if the number of bits in the arc representation
117 * is higher than the width of supplied * return value
118 * type, there is still possible to fit it when there
119 * are few unused high bits in the arc value
120 * representaion.
Lev Walkin0787ff02004-06-17 23:43:39 +0000121 *
122 * Moreover, there is a possibility that the
123 * number could actually fit the arc space, given
124 * that add is negative, but we don't handle
125 * such "temporary lack of precision" situation here.
126 * May be considered as a bug.
Lev Walkin29a044b2004-06-14 07:24:36 +0000127 */
128 uint8_t mask = (0xff << (7-(arclen - rvsize))) & 0x7f;
129 if((*arcbuf & mask)) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000130 errno = ERANGE; /* Overflow */
131 return -1;
132 }
Lev Walkin29a044b2004-06-14 07:24:36 +0000133 /* Fool the routine computing unused bits */
134 arclen -= 7;
135 cache = *arcbuf & 0x7f;
136 arcbuf++;
137 }
138 }
139
Lev Walkinc4c61962004-06-14 08:17:27 +0000140 /* Faster path for common size */
141 if(rvsize == (CHAR_BIT * sizeof(unsigned long))) {
142 unsigned long accum;
143 /* Gather all bits into the accumulator */
144 for(accum = cache; arcbuf < arcend; arcbuf++)
145 accum = (accum << 7) | (*arcbuf & ~0x80);
146 if(accum < (unsigned)-add) {
147 errno = ERANGE; /* Overflow */
148 return -1;
149 }
150 *(unsigned long *)rvbuf = accum + add;
151 return 0;
152 }
153
Lev Walkin29a044b2004-06-14 07:24:36 +0000154#ifndef WORDS_BIGENDIAN
155 if(*(unsigned char *)&LE) { /* Little endian (x86) */
156 /* "Convert" to big endian */
Lev Walkinc4c61962004-06-14 08:17:27 +0000157 rvbuf += rvsize / CHAR_BIT - 1;
158 ((unsigned char *)rvstart)--;
Lev Walkin29a044b2004-06-14 07:24:36 +0000159 inc = -1; /* Descending */
Lev Walkinc4c61962004-06-14 08:17:27 +0000160 } else
Lev Walkin29a044b2004-06-14 07:24:36 +0000161#endif /* !WORDS_BIGENDIAN */
Lev Walkinc4c61962004-06-14 08:17:27 +0000162 inc = +1; /* Big endian is known [at compile time] */
Lev Walkin29a044b2004-06-14 07:24:36 +0000163
Lev Walkinc4c61962004-06-14 08:17:27 +0000164 {
Lev Walkin0787ff02004-06-17 23:43:39 +0000165 int bits; /* typically no more than 3-4 bits */
Lev Walkinc4c61962004-06-14 08:17:27 +0000166
Lev Walkin29a044b2004-06-14 07:24:36 +0000167 /* Clear the high unused bits */
168 for(bits = rvsize - arclen;
169 bits > CHAR_BIT;
170 rvbuf += inc, bits -= CHAR_BIT)
171 *(unsigned char *)rvbuf = 0;
Lev Walkinc4c61962004-06-14 08:17:27 +0000172
Lev Walkin29a044b2004-06-14 07:24:36 +0000173 /* Fill the body of a value */
174 for(; arcbuf < arcend; arcbuf++) {
175 cache = (cache << 7) | (*arcbuf & 0x7f);
176 bits += 7;
177 if(bits >= CHAR_BIT) {
178 bits -= CHAR_BIT;
179 *(unsigned char *)rvbuf = (cache >> bits);
180 rvbuf += inc;
181 }
182 }
183 if(bits) {
184 *(unsigned char *)rvbuf = cache;
185 rvbuf += inc;
186 }
187 }
188
189 if(add) {
190 for(rvbuf -= inc; rvbuf != rvstart; rvbuf -= inc) {
191 int v = add + *(unsigned char *)rvbuf;
192 if(v & (-1 << CHAR_BIT)) {
193 *(unsigned char *)rvbuf
194 = v + (1 << CHAR_BIT);
195 add = -1;
196 } else {
197 *(unsigned char *)rvbuf = v;
198 break;
199 }
200 }
201 if(rvbuf == rvstart) {
202 /* No space to carry over */
Lev Walkinf15320b2004-06-03 03:38:44 +0000203 errno = ERANGE; /* Overflow */
204 return -1;
205 }
206 }
207
Lev Walkinf15320b2004-06-03 03:38:44 +0000208 return 0;
209}
210
Lev Walkin29a044b2004-06-14 07:24:36 +0000211
Lev Walkinf15320b2004-06-03 03:38:44 +0000212int
213OBJECT_IDENTIFIER_print_arc(uint8_t *arcbuf, int arclen, int add,
214 asn_app_consume_bytes_f *cb, void *app_key) {
215 char scratch[64]; /* Conservative estimate */
216 unsigned long accum; /* Bits accumulator */
217 char *p; /* Position in the scratch buffer */
218
Lev Walkin29a044b2004-06-14 07:24:36 +0000219 if(OBJECT_IDENTIFIER_get_single_arc(arcbuf, arclen, add,
220 &accum, sizeof(accum)))
Lev Walkinf15320b2004-06-03 03:38:44 +0000221 return -1;
222
223 /* Fill the scratch buffer in reverse. */
224 p = scratch + sizeof(scratch);
225 for(; accum; accum /= 10)
226 *(--p) = (accum % 10) + 0x30;
227
228 return cb(p, sizeof(scratch) - (p - scratch), app_key);
229}
230
231int
232OBJECT_IDENTIFIER_print(asn1_TYPE_descriptor_t *td, const void *sptr,
233 int ilevel, asn_app_consume_bytes_f *cb, void *app_key) {
234 const OBJECT_IDENTIFIER_t *st = sptr;
235 int startn;
236 int add = 0;
237 int i;
238
Lev Walkind9bd7752004-06-05 08:17:50 +0000239 (void)td; /* Unused argument */
240 (void)ilevel; /* Unused argument */
241
Lev Walkinf15320b2004-06-03 03:38:44 +0000242 if(!st || !st->buf)
243 return cb("<absent>", 8, app_key);
244
245 /* Dump preamble */
246 if(cb("{ ", 2, app_key))
247 return -1;
248
249 for(i = 0, startn = 0; i < st->size; i++) {
250 uint8_t b = st->buf[i];
251 if((b & 0x80)) /* Continuation expected */
252 continue;
253
254 if(startn == 0) {
255 /*
256 * First two arcs are encoded through the backdoor.
257 */
258 if(i) {
259 add = -80;
260 if(cb("2", 1, app_key)) return -1;
261 } else if(b <= 39) {
262 add = 0;
263 if(cb("0", 1, app_key)) return -1;
264 } else if(b < 79) {
265 add = -40;
266 if(cb("1", 1, app_key)) return -1;
267 } else {
268 add = -80;
269 if(cb("2", 1, app_key)) return -1;
270 }
271 }
272
273 if(cb(" ", 1, app_key)) /* Separate arcs */
274 return -1;
275
276 if(OBJECT_IDENTIFIER_print_arc(&st->buf[startn],
277 i - startn + 1, add,
278 cb, app_key))
279 return -1;
280 startn = i + 1;
281 add = 0;
282 }
283
284 return cb(" }", 2, app_key);
285}
286
287int
Lev Walkin29a044b2004-06-14 07:24:36 +0000288OBJECT_IDENTIFIER_get_arcs(OBJECT_IDENTIFIER_t *oid, void *arcs,
289 unsigned int arc_type_size, unsigned int arc_slots) {
290 void *arcs_end = arcs + (arc_type_size * arc_slots);
291 int num_arcs = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000292 int startn = 0;
293 int add = 0;
294 int i;
295
Lev Walkin0787ff02004-06-17 23:43:39 +0000296 if(!oid || !oid->buf || (arc_slots && arc_type_size <= 1)) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000297 errno = EINVAL;
298 return -1;
299 }
300
301 for(i = 0; i < oid->size; i++) {
302 uint8_t b = oid->buf[i];
303 if((b & 0x80)) /* Continuation expected */
304 continue;
305
Lev Walkin29a044b2004-06-14 07:24:36 +0000306 if(num_arcs == 0) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000307 /*
308 * First two arcs are encoded through the backdoor.
309 */
Lev Walkin29a044b2004-06-14 07:24:36 +0000310 unsigned LE = 1; /* Little endian */
311 int first_arc;
312 num_arcs++;
313 if(!arc_slots) { num_arcs++; continue; }
314
315 if(i) first_arc = 2;
316 else if(b <= 39) first_arc = 0;
317 else if(b < 79) first_arc = 1;
318 else first_arc = 2;
319
320 add = -40 * first_arc;
321 memset(arcs, 0, arc_type_size);
322 *(unsigned char *)(arcs
323 + ((*(char *)&LE)?0:(arc_type_size - 1)))
324 = first_arc;
325 arcs += arc_type_size;
Lev Walkinf15320b2004-06-03 03:38:44 +0000326 }
327
Lev Walkin29a044b2004-06-14 07:24:36 +0000328 /* Decode, if has space */
329 if(arcs < arcs_end) {
330 if(OBJECT_IDENTIFIER_get_single_arc(&oid->buf[startn],
331 i - startn + 1, add,
332 arcs, arc_type_size))
333 return -1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000334 startn = i + 1;
Lev Walkin29a044b2004-06-14 07:24:36 +0000335 arcs += arc_type_size;
336 add = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000337 }
Lev Walkin29a044b2004-06-14 07:24:36 +0000338 num_arcs++;
Lev Walkinf15320b2004-06-03 03:38:44 +0000339 }
340
Lev Walkin29a044b2004-06-14 07:24:36 +0000341 return num_arcs;
Lev Walkinf15320b2004-06-03 03:38:44 +0000342}
343
Lev Walkin0787ff02004-06-17 23:43:39 +0000344
345/*
346 * Save the single value as an object identifier arc.
347 */
348inline int
349OBJECT_IDENTIFIER_set_single_arc(uint8_t *arcbuf, void *arcval, unsigned int arcval_size, int prepared_order) {
350 /*
351 * The following conditions must hold:
352 * assert(arcval);
353 * assert(arcval_size > 0);
354 * assert(arcbuf);
355 */
356#ifdef WORDS_BIGENDIAN
357 const unsigned isLittleEndian = 0;
358#else
359 unsigned LE = 1;
360 unsigned isLittleEndian = *(char *)&LE;
361#endif
Lev Walkin0787ff02004-06-17 23:43:39 +0000362 uint8_t *tp, *tend;
363 unsigned int cache;
364 uint8_t *bp = arcbuf;
365 int bits;
Lev Walkin64399722004-08-11 07:17:22 +0000366#ifdef __GNUC__
367 uint8_t buffer[arcval_size];
368#else
369 uint8_t *buffer = alloca(arcval_size);
370#endif
Lev Walkin0787ff02004-06-17 23:43:39 +0000371
372 if(isLittleEndian && !prepared_order) {
373 uint8_t *a = arcval + arcval_size - 1;
374 uint8_t *aend = arcval;
375 uint8_t *msb = buffer + arcval_size - 1;
376 for(tp = buffer; a >= aend; tp++, a--)
377 if((*tp = *a) && (tp < msb))
378 msb = tp;
379 tend = &buffer[arcval_size];
380 tp = msb; /* Most significant non-zero byte */
381 } else {
382 /* Look for most significant non-zero byte */
383 tend = arcval + arcval_size;
384 for(tp = arcval; tp < tend - 1; tp++)
385 if(*tp) break;
386 }
387
388 /*
389 * Split the value in 7-bits chunks.
390 */
391 bits = ((tend - tp) * CHAR_BIT) % 7;
392 if(bits) {
393 cache = *tp >> (CHAR_BIT - bits);
394 if(cache) {
395 *bp++ = cache | 0x80;
396 cache = *tp++;
397 bits = CHAR_BIT - bits;
398 } else {
399 bits = -bits;
400 }
401 } else {
402 cache = 0;
403 }
404 for(; tp < tend; tp++) {
405 cache = (cache << CHAR_BIT) + *tp;
406 bits += CHAR_BIT;
407 while(bits >= 7) {
408 bits -= 7;
409 *bp++ = 0x80 | (cache >> bits);
410 }
411 }
412 if(bits) *bp++ = cache;
413 bp[-1] &= 0x7f; /* Clear the last bit */
414
415 return bp - arcbuf;
416}
417
Lev Walkinf15320b2004-06-03 03:38:44 +0000418int
Lev Walkin0787ff02004-06-17 23:43:39 +0000419OBJECT_IDENTIFIER_set_arcs(OBJECT_IDENTIFIER_t *oid, void *arcs, unsigned int arc_type_size, unsigned int arc_slots) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000420 uint8_t *buf;
421 uint8_t *bp;
Lev Walkin0787ff02004-06-17 23:43:39 +0000422 unsigned LE = 1; /* Little endian (x86) */
423 unsigned isLittleEndian = *((char *)&LE);
424 unsigned int arc0;
425 unsigned int arc1;
426 unsigned size;
Lev Walkinc4c61962004-06-14 08:17:27 +0000427 unsigned i;
Lev Walkinf15320b2004-06-03 03:38:44 +0000428
Lev Walkin0787ff02004-06-17 23:43:39 +0000429 if(!oid || !arcs || arc_type_size < 1 || arc_slots < 2) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000430 errno = EINVAL;
431 return -1;
432 }
433
Lev Walkin0787ff02004-06-17 23:43:39 +0000434 switch(arc_type_size) {
435 case sizeof(char):
436 arc0 = ((unsigned char *)arcs)[0];
437 arc1 = ((unsigned char *)arcs)[1];
438 break;
439 case sizeof(short):
440 arc0 = ((unsigned short *)arcs)[0];
441 arc1 = ((unsigned short *)arcs)[1];
442 break;
443 case sizeof(int):
444 arc0 = ((unsigned int *)arcs)[0];
445 arc1 = ((unsigned int *)arcs)[1];
446 break;
447 default:
448 arc1 = arc0 = 0;
449 if(isLittleEndian) { /* Little endian (x86) */
450 unsigned char *ps, *pe;
451 /* If more significant bytes are present,
452 * make them > 255 quick */
453 for(ps = arcs + 1, pe = ps+arc_type_size; ps < pe; ps++)
454 arc0 |= *ps, arc1 |= *(ps + arc_type_size);
455 arc0 <<= CHAR_BIT, arc1 <<= CHAR_BIT;
456 arc0 = *((unsigned char *)arcs + 0);
457 arc1 = *((unsigned char *)arcs + arc_type_size);
458 } else {
459 unsigned char *ps, *pe;
460 /* If more significant bytes are present,
461 * make them > 255 quick */
462 for(ps = arcs, pe = ps+arc_type_size - 1; ps < pe; ps++)
463 arc0 |= *ps, arc1 |= *(ps + arc_type_size);
464 arc0 = *((unsigned char *)arcs + arc_type_size - 1);
465 arc1 = *((unsigned char *)arcs +(arc_type_size<< 1)-1);
466 }
467 }
468
469 /*
470 * The previous chapter left us with the first and the second arcs.
471 * The values are not precise (that is, they are valid only if
472 * they're less than 255), but OK for the purposes of making
473 * the sanity test below.
474 */
475 if(arc0 <= 1) {
476 if(arc1 >= 39) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000477 /* 8.19.4: At most 39 subsequent values (including 0) */
478 errno = ERANGE;
479 return -1;
480 }
Lev Walkin0787ff02004-06-17 23:43:39 +0000481 } else if(arc0 > 2) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000482 /* 8.19.4: Only three values are allocated from the root node */
483 errno = ERANGE;
484 return -1;
485 }
Lev Walkin0787ff02004-06-17 23:43:39 +0000486 /*
487 * After above tests it is known that the value of arc0 is completely
488 * trustworthy (0..2). However, the arc1's value is still meaningless.
489 */
Lev Walkinf15320b2004-06-03 03:38:44 +0000490
491 /*
492 * Roughly estimate the maximum size necessary to encode these arcs.
Lev Walkin0787ff02004-06-17 23:43:39 +0000493 * This estimation implicitly takes in account the following facts,
494 * that cancel each other:
495 * * the first two arcs are encoded in a single value.
496 * * the first value may require more space (+1 byte)
497 * * the value of the first arc which is in range (0..2)
Lev Walkinf15320b2004-06-03 03:38:44 +0000498 */
Lev Walkin0787ff02004-06-17 23:43:39 +0000499 size = ((arc_type_size * CHAR_BIT + 6) / 7) * arc_slots;
Lev Walkinf15320b2004-06-03 03:38:44 +0000500 bp = buf = MALLOC(size + 1);
501 if(!buf) {
502 /* ENOMEM */
503 return -1;
504 }
505
506 /*
Lev Walkin0787ff02004-06-17 23:43:39 +0000507 * Encode the first two arcs.
508 * These require special treatment.
Lev Walkinf15320b2004-06-03 03:38:44 +0000509 */
Lev Walkinf15320b2004-06-03 03:38:44 +0000510 {
Lev Walkin0787ff02004-06-17 23:43:39 +0000511 uint8_t first_value[1 + arc_type_size]; /* of two arcs */
512 uint8_t *fv = first_value;
513 uint8_t *tp;
Lev Walkinf15320b2004-06-03 03:38:44 +0000514
Lev Walkin0787ff02004-06-17 23:43:39 +0000515 /*
516 * Simulate first_value = arc0 * 40 + arc1;
517 */
518 /* Copy the second (1'st) arcs[1] into the first_value */
519 *fv++ = 0;
520 (char *)arcs += arc_type_size;
521 if(isLittleEndian) {
522 uint8_t *aend = arcs - 1;
523 uint8_t *a1 = arcs + arc_type_size - 1;
524 for(; a1 > aend; fv++, a1--) *fv = *a1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000525 } else {
Lev Walkin0787ff02004-06-17 23:43:39 +0000526 uint8_t *a1 = arcs;
527 uint8_t *aend = a1 + arc_type_size;
528 for(; a1 < aend; fv++, a1++) *fv = *a1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000529 }
Lev Walkin0787ff02004-06-17 23:43:39 +0000530 /* Increase the first_value by arc0 */
531 arc0 *= 40; /* (0..80) */
532 for(tp = first_value + arc_type_size; tp >= first_value; tp--) {
533 unsigned int v = *tp;
534 v += arc0;
535 *tp = v;
536 if(v >= (1 << CHAR_BIT)) arc0 = v >> CHAR_BIT;
537 else break;
538 }
539
540 assert(tp >= first_value);
541
542 bp += OBJECT_IDENTIFIER_set_single_arc(bp, first_value,
543 fv - first_value, 1);
544 }
545
546 /*
547 * Save the rest of arcs.
548 */
549 for((char *)arcs += arc_type_size, i = 2;
550 i < arc_slots; i++, (char *)arcs += arc_type_size) {
551 bp += OBJECT_IDENTIFIER_set_single_arc(bp,
552 arcs, arc_type_size, 0);
Lev Walkinf15320b2004-06-03 03:38:44 +0000553 }
554
Lev Walkin34b2a932004-06-17 23:46:45 +0000555 assert((unsigned)(bp - buf) <= size);
Lev Walkinf15320b2004-06-03 03:38:44 +0000556
557 /*
558 * Replace buffer.
559 */
Lev Walkin0787ff02004-06-17 23:43:39 +0000560 oid->size = bp - buf;
Lev Walkinf15320b2004-06-03 03:38:44 +0000561 bp = oid->buf;
562 oid->buf = buf;
563 if(bp) FREEMEM(bp);
564
565 return 0;
566}
Lev Walkinc4c61962004-06-14 08:17:27 +0000567