1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632
|
/* $Cambridge: hermes/src/prayer/lib/buffer.c,v 1.3 2008/09/16 09:59:57 dpc22 Exp $ */
/************************************************
* Prayer - a Webmail Interface *
************************************************/
/* Copyright (c) University of Cambridge 2000 - 2008 */
/* See the file NOTICE for conditions of use and distribution. */
#include "lib.h"
/* Class for processing arbitary length strings with append facility and
* linear search/read access. The name "buffer" is historical, its possible
* that this should be called something else today. However I can't really
* face renaming several thousand references to "buffer" right now */
/* ====================================================================== */
/* buffer_create() ******************************************************
*
* Create a new buffer structure.
* pool: Target pool for storage
* blocksize: Preferred size for individual allocation blocks.
* Typically quite large for IO operations, small if we
* have a good feel for like upper bound on size of object.
* 0 => Picks default which would appropriate for IO objects
*
* Returns: New buffer object
***********************************************************************/
struct buffer *buffer_create(struct pool *pool, unsigned long blocksize)
{
struct buffer *b = pool_alloc(pool, sizeof(struct buffer));
b->size = 0; /* Buffer starts out empty */
b->pool = pool; /* Allocate from this pool */
b->blocksize =
(blocksize > 0) ? blocksize : PREFERRED_BUFFER_BLOCK_SIZE;
b->first = NIL;
b->last = NIL;
b->avail = 0; /* Forces allocation */
b->offset = 0; /* State used by read methods */
b->fetch = NIL;
b->fetch_avail = 0;
return (b);
}
/* buffer_free() ********************************************************
*
* Free buffer including all allocated blocks. NOOP if pool defined.
* b: buffer to free
***********************************************************************/
void buffer_free(struct buffer *b)
{
struct msgblock *current, *next;
if (b->pool) /* Noop if data allocated from pool */
return;
for (current = b->first; current; current = next) {
next = current->next;
free(current);
}
free(b);
}
/* buffer_reset() *******************************************************
*
* Wipe existing buffer so that caller can overwrite existing data
***********************************************************************/
void buffer_reset(struct buffer *b)
{
b->size = 0; /* Buffer starts out empty */
b->first = NIL;
b->last = NIL;
b->avail = 0; /* Forces allocation */
b->offset = 0; /* State used by read methods */
b->fetch = NIL;
b->fetch_avail = 0;
}
/* buffer_size() ********************************************************
*
* Returns number of characters currently stored in buffer
***********************************************************************/
unsigned long buffer_size(struct buffer *b)
{
return (b->size);
}
/* ====================================================================== */
/* put/extend methods */
/* buffer_add_msgblock() ************************************************
*
* Adds a new msgblock (historical name for allocation space) to buffer:
* gives us some room to expand.
* b: Buffer to extend.
***********************************************************************/
static void buffer_add_msgblock(struct buffer *b)
{
struct msgblock *mb;
mb = pool_alloc(b->pool, sizeof(struct msgblock) + (b->blocksize) - 1);
mb->next = NIL;
if (b->first) {
b->last->next = mb; /* Add msgblock to end of chain */
b->last = mb;
} else
b->first = b->last = mb; /* First msgblock in chain */
}
/* buffer_putchar() *****************************************************
*
* Add a single character to the end of the buffer, extending buffer if
* required. Typically access via macro bputc().
* b: Buffer
* c: Character to add
***********************************************************************/
void buffer_putchar(struct buffer *b, unsigned char c)
{
/* Space available in current msgblock */
if (b->avail > 0) {
b->last->data[b->blocksize - b->avail] = c;
b->avail--;
b->size++;
return;
}
/* Need to allocate a fresh msgblock */
buffer_add_msgblock(b);
b->avail = b->blocksize - 1;
b->last->data[0] = c;
b->size++;
}
/* buffer_print_ulong() *************************************************
*
* Print number (as decimal represention) at end of buffer.
* b: Buffer
* value: value
***********************************************************************/
static void buffer_print_ulong(struct buffer *b, unsigned long value)
{
unsigned long tmp, weight;
/* All numbers contain at least one digit.
* Find weight of most significant digit. */
for (weight = 1, tmp = value / 10; tmp > 0; tmp /= 10)
weight *= 10;
for (tmp = value; weight > 0; weight /= 10) {
if (value >= weight) { /* Strictly speaking redundant... */
bputc(b, '0' + (value / weight)); /* Digit other than zero */
value -= weight * (value / weight); /* Calculate remainder */
} else
bputc(b, '0');
}
}
/* buffer_print_hex() ***************************************************
*
* Print number (as hex represention) at end of buffer.
* b: Buffer
* value: value
***********************************************************************/
static void buffer_print_hex(struct buffer *b, unsigned long value)
{
unsigned long tmp, weight;
/* All numbers contain at least one digit.
* Find weight of most significant digit. */
for (weight = 1, tmp = value / 16; tmp > 0; tmp /= 16)
weight *= 16;
for (tmp = value; weight > 0; weight /= 16) {
unsigned long digit = value / weight;
unsigned char c =
(digit > 9) ? ('a' + (digit - 10)) : ('0' + digit);
bputc(b, c);
value -= weight * digit;
}
}
/* buffer_vaprintf() ****************************************************
*
* vaprintf equivalent for buffer
* b: Buffer
* fmt: vaprintf format string, followed by arguments.
***********************************************************************/
void buffer_vaprintf(struct buffer *b, char *fmt, va_list ap)
{
unsigned char *s, c;
while ((c = *fmt++)) {
if (c != '%') {
bputc(b, c);
} else
switch (*fmt++) {
case 's': /* string */
if ((s = (unsigned char *) va_arg(ap, char *))) {
while ((c = *s++))
bputc(b, c);
} else
bputs(b, "(nil)");
break;
case 'l':
if (*fmt == 'u') {
buffer_print_ulong(b, va_arg(ap, unsigned long));
fmt++;
} else if (*fmt == 'x') {
buffer_print_hex(b, va_arg(ap, unsigned long));
fmt++;
} else
buffer_print_ulong(b, va_arg(ap, long));
break;
case 'd':
if (*fmt == 'u') {
buffer_print_ulong(b, va_arg(ap, unsigned int));
fmt++;
} else
buffer_print_ulong(b, va_arg(ap, int));
break;
case 'c':
bputc(b, (unsigned char) va_arg(ap, int));
break;
case 'x':
buffer_print_hex(b, va_arg(ap, unsigned long));
break;
case '%':
bputc(b, '%');
break;
default:
log_fatal("Bad format string to buffer_printf");
}
}
}
/* buffer_printf() ******************************************************
*
* printf equivalent for buffer. Typically accessed via bprintf() macro
* b: Buffer
* fmt: vaprintf format string, followed by arguments.
***********************************************************************/
void buffer_printf(struct buffer *b, char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
buffer_vaprintf(b, fmt, ap);
va_end(ap);
}
/* buffer_printf() ******************************************************
*
* puts equivalent for buffer. Typically accessed via bputs() macro
* b: Buffer
* t: String to print
***********************************************************************/
void buffer_puts(struct buffer *b, char *t)
{
unsigned char *s = (unsigned char *) t;
char c;
if (!s)
bputs(b, "(nil)");
else
while ((c = *s++))
bputc(b, c);
}
/* ====================================================================== */
/* buffer_vaprintf_translate() ******************************************
*
* Print string translating '/' characters with '@'. Used by short URL
* translation stuff.
* b: Buffer
* fmt: vaprintf format string, followed by arguments.
***********************************************************************/
void buffer_vaprintf_translate(struct buffer *b, char *fmt, va_list ap)
{
unsigned char *s, c;
while ((c = *fmt++)) {
switch (c) {
case '%':
switch (*fmt++) {
case 's': /* string */
if ((s = (unsigned char *) va_arg(ap, char *))) {
while ((c = *s++))
bputc(b, (c == '/') ? '@' : c);
} else
bputs(b, "(nil)");
break;
case 'l':
if (*fmt == 'u') {
buffer_print_ulong(b, va_arg(ap, unsigned long));
fmt++;
} else
buffer_print_ulong(b, va_arg(ap, long));
break;
case 'd':
if (*fmt == 'u') {
buffer_print_ulong(b, va_arg(ap, unsigned int));
fmt++;
} else
buffer_print_ulong(b, va_arg(ap, int));
break;
case 'c':
bputc(b, (unsigned char) va_arg(ap, int));
break;
case '%':
bputc(b, '%');
break;
default:
log_fatal("Bad format string to buffer_printf");
}
break;
case '/':
bputc(b, '@');
break;
default:
bputc(b, c);
}
}
}
/* buffer_printf_translate() ********************************************
*
* Print string translating '/' characters with '@'. Used by short URL
* translation stuff.
* b: Buffer
* fmt: vaprintf format string, followed by arguments.
***********************************************************************/
void buffer_printf_translate(struct buffer *b, char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
buffer_vaprintf_translate(b, fmt, ap);
va_end(ap);
}
/* buffer_puts_translate() ***********************************************
*
* Print string translating '/' characters with '@'. Used by short URL
* translation stuff.
* b: Buffer
* t: String to print/translate
***********************************************************************/
void buffer_puts_translate(struct buffer *b, char *t)
{
unsigned char *s = (unsigned char *) t;
char c;
if (!s)
bputs(b, "(nil)");
else
while ((c = *s++))
bputc(b, (c == '/') ? '@' : c);
}
/* ====================================================================== */
/* Fetch methods */
/* buffer_rewind() ******************************************************
*
* Rewind read access ptrs to start of the buffer
***********************************************************************/
void buffer_rewind(struct buffer *b)
{
b->offset = 0;
b->fetch = b->first;
b->fetch_avail = b->blocksize;
}
/* buffer_seek_offset() *************************************************
*
* Seek to given offset in buffer
* b: Buffer
* offset: Offset into buffer
*
* Returns: T on sucess. NIL if offset is out of range.
***********************************************************************/
BOOL buffer_seek_offset(struct buffer *b, unsigned long offset)
{
struct msgblock *mb = b->first;
if ((b->offset = offset) > b->size)
return (NIL);
while (offset > b->blocksize) {
mb = mb->next;
offset -= b->blocksize;
}
b->fetch = mb; /* Correct block */
b->fetch_avail = b->blocksize - offset; /* Data left in this block */
return (T);
}
/* buffer_getchar() *****************************************************
*
* Get character from current read location in buffer. Usually used via
* bgetc() macro.
* b: Buffer
***********************************************************************/
int buffer_getchar(struct buffer *b)
{
unsigned char result;
if (b->offset >= b->size) /* Nothing more available */
return (EOF);
if (b->fetch == NIL) /* Need to set up fetch ptrs */
buffer_rewind(b);
if (b->fetch_avail == 0) {
b->fetch = b->fetch->next; /* Next block in chain */
b->fetch_avail = b->blocksize;
}
/* Record current character */
result = b->fetch->data[b->blocksize - b->fetch_avail];
/* Then update pointers */
b->offset++;
b->fetch_avail--;
return ((int) result);
}
/* ====================================================================== */
/* buffer_getblock() *****************************************************
*
* Get block of characters from buffer. Static support fn for buffer_fetch
* b: Buffer
* block: Target location
* count: Number of characters.
***********************************************************************/
static unsigned long
buffer_getblock(struct buffer *b, void *block, unsigned long count)
{
char *s = (char *) block;
unsigned long result;
if (b->offset >= b->size) /* No more bytes available */
return (0);
if (count > (b->size - b->offset))
count = b->size - b->offset; /* Only this many bytes available */
result = count; /* Return (adjusted) count to caller */
if (b->fetch == NIL) /* Need to set up fetch ptrs */
buffer_rewind(b);
b->offset += count;
if (count < b->fetch_avail) {
/* Can fetch block from current bucket */
memcpy(s, &(b->fetch->data[b->blocksize - b->fetch_avail]), count);
b->fetch_avail -= count;
return (result);
}
/* Otherwise block fetch will overflow into next bucket */
if (b->fetch_avail > 0) {
/* Take partial chunk from current bucket */
/* NB: this deals with (count == b->fetch_avail) case too */
memcpy(s, &(b->fetch->data[b->blocksize - b->fetch_avail]),
b->fetch_avail);
s += b->fetch_avail;
count -= b->fetch_avail;
/* Set up next full bucket */
b->fetch = b->fetch->next;
b->fetch_avail = b->blocksize;
}
while (count >= b->blocksize) {
/* Copy in full b->blocksize chunks */
memcpy(s, b->fetch->data, b->blocksize);
s += b->blocksize;
count -= b->blocksize;
/* Set up next full bucket */
b->fetch = b->fetch->next;
b->fetch_avail = b->blocksize;
}
/* Possible final (partial) bucket will be < b->blocksize */
if (count > 0) {
memcpy(s, b->fetch->data, count);
/* More data to process in this bucket. b->fetch stays unchanged */
b->fetch_avail = b->blocksize - count;
}
return (result);
}
/* buffer_fetch() *******************************************************
*
* Retrive block of data from buffer
* b: Buffer
* offset: Offset into buffer
* count: Number of characters to retrieve
* copy: Generate separate copy of data.
* NIL => okay to return ptr to data in place if byte range
* falls within a single msgblock object.
***********************************************************************/
void *buffer_fetch(struct buffer *b,
unsigned long offset, unsigned long count, BOOL copy)
{
char *result;
buffer_seek_offset(b, offset);
if (count == 0)
return (pool_strdup(b->pool, ""));
if (copy || (b->fetch_avail < count + 1)) {
result = pool_alloc(b->pool, count + 1);
buffer_getblock(b, result, count);
result[count] = '\0';
} else {
unsigned long init_offset = b->blocksize - b->fetch_avail;
b->fetch->data[init_offset + count] = '\0';
result = (char *) &(b->fetch->data[init_offset]);
}
return ((void *) result);
}
/* buffer_fetch_block() *************************************************
*
*
* Fetch single block from buffer from current seek position. Repeated
* calls will step through the buffer one block at a time.
* b: Buffer
* ptrp: Used to return next block
* sizep: Used to return size of next block
*
* Returns: T => data available.
* NIL => no data available.
***********************************************************************/
BOOL
buffer_fetch_block(struct buffer *b,
unsigned char **ptrp, unsigned long *sizep)
{
if (b->fetch == NIL)
return (NIL);
if (b->fetch->next) {
*ptrp = &b->fetch->data[0];
*sizep = b->blocksize;
b->fetch = b->fetch->next;
} else {
*ptrp = &b->fetch->data[0];
*sizep = b->blocksize - b->avail;
b->fetch = NIL;
}
return (T);
}
/* ====================================================================== */
static void buffer_encode_common(struct buffer *b, char *t, char quote)
{
unsigned char *s = (unsigned char *) t;
static char hex[] = "0123456789abcdef";
unsigned char c;
while ((c=*s++)) {
if (Uisalnum(c))
bputc(b, c);
else {
bputc(b, quote);
bputc(b, hex[c >> 4]);
bputc(b, hex[c & 15]);
}
}
}
void buffer_encode_url(struct buffer *b, char *s)
{
buffer_encode_common(b, s, '%');
}
void buffer_encode_canon(struct buffer *b, char *s)
{
buffer_encode_common(b, s, '*');
}
|