5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 1998 - 2005 Pekka Riikonen
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; version 2 of the License.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
21 /****h* silcutil/SILC Buffer Interface
25 * SilcBuffer is very simple and easy to use, yet you can do to the
26 * buffer almost anything you want with its method functions. The buffer
27 * is constructed of four different data sections that in whole creates
28 * the allocated data area.
35 /****s* silcutil/SilcBufferAPI/SilcBuffer
39 * typedef struct { ... } *SilcBuffer, SilcBufferStruct;
43 * SILC Buffer object. Following short description of the fields
50 * Head of the allocated buffer. This is the start of the allocated
51 * data area and remains as same throughout the lifetime of the buffer.
52 * However, the end of the head area or the start of the currently valid
53 * data area is variable.
55 * --------------------------------
56 * | head | data | tail |
57 * --------------------------------
60 * Current head section in the buffer is sb->data - sb->head.
62 * unsigned char *data;
64 * Currently valid data area. This is the start of the currently valid
65 * main data area. The data area is variable in all directions.
67 * --------------------------------
68 * | head | data | tail |
69 * --------------------------------
72 * Current valid data area in the buffer is sb->tail - sb->data.
74 * unsigned char *tail;
76 * Tail of the buffer. This is the end of the currently valid data area
77 * or start of the tail area. The start of the tail area is variable.
79 * --------------------------------
80 * | head | data | tail |
81 * --------------------------------
84 * Current tail section in the buffer is sb->end - sb->tail.
88 * End of the allocated buffer. This is the end of the allocated data
89 * area and remains as same throughout the lifetime of the buffer.
90 * Usually this field is not needed except when checking the size
93 * --------------------------------
94 * | head | data | tail |
95 * --------------------------------
98 * Length of the entire buffer is (ie. truelen) sb->end - sb->head.
100 * Currently valid data area is considered to be the main data area in
101 * the buffer. However, the entire buffer is of course valid data and can
102 * be used as such. Usually head section of the buffer includes different
103 * kind of headers or similar. Data section includes the main data of
104 * the buffer. Tail section can be seen as a reserve space of the data
105 * section. Tail section can be pulled towards end, and thus the data
106 * section becomes larger.
115 } *SilcBuffer, SilcBufferStruct;
120 /****d* silcutil/SilcBufferAPI/silc_buffer_truelen
124 * #define silc_buffer_truelen(buffer)
128 * Returns the true length of the buffer.
132 #define silc_buffer_truelen(x) (SilcUInt32)((x)->end - (x)->head)
135 /****d* silcutil/SilcBufferAPI/silc_buffer_len
139 * #define silc_buffer_len(buffer)
143 * Returns the current length of the data area of the buffer.
147 #define silc_buffer_len(x) (SilcUInt32)((x)->tail - (x)->data)
150 /****d* silcutil/SilcBufferAPI/silc_buffer_headlen
154 * #define silc_buffer_headlen(buffer)
158 * Returns the current length of the head data area of the buffer.
162 #define silc_buffer_headlen(x) (SilcUInt32)((x)->data - (x)->head)
165 /****d* silcutil/SilcBufferAPI/silc_buffer_taillen
169 * #define silc_buffer_taillen(buffer)
173 * Returns the current length of the tail data area of the buffer.
177 #define silc_buffer_taillen(x) (SilcUInt32)((x)->end - (x)->tail)
180 /* Inline functions */
182 /****f* silcutil/SilcBufferAPI/silc_buffer_alloc
187 * SilcBuffer silc_buffer_alloc(SilcUInt32 len);
191 * Allocates new SilcBuffer and returns it. Returns NULL on error.
196 SilcBuffer silc_buffer_alloc(SilcUInt32 len)
200 /* Allocate new SilcBuffer */
201 sb = (SilcBuffer)silc_calloc(1, sizeof(*sb));
205 /* Allocate the actual data area */
206 sb->head = (unsigned char *)silc_calloc(len, sizeof(*sb->head));
210 /* Set pointers to the new buffer */
213 sb->end = sb->head + len;
218 /****f* silcutil/SilcBufferAPI/silc_buffer_free
223 * void silc_buffer_free(SilcBuffer sb);
232 void silc_buffer_free(SilcBuffer sb)
235 #if defined(SILC_DEBUG)
237 memset(sb->head, 'F', silc_buffer_truelen(sb));
244 /****f* silcutil/SilcBufferAPI/silc_buffer_steal
249 * unsigned char *silc_buffer_steal(SilcBuffer sb, SilcUInt32 *data_len);
253 * Steals the data from the buffer `sb'. This returns pointer to the
254 * start of the buffer and the true length of that buffer. The `sb'
255 * cannot be used anymore after calling this function because the
256 * data buffer was stolen. The `sb' must be freed with silc_buffer_free.
257 * The caller is responsible of freeing the stolen data buffer with
263 unsigned char *silc_buffer_steal(SilcBuffer sb, SilcUInt32 *data_len)
265 unsigned char *buf = sb->head;
267 *data_len = silc_buffer_truelen(sb);
268 sb->head = sb->data = sb->tail = sb->end = NULL;
272 /****f* silcutil/SilcBufferAPI/silc_buffer_set
277 * void silc_buffer_set(SilcBuffer sb,
278 * unsigned char *data,
279 * SilcUInt32 data_len);
283 * Sets the `data' and `data_len' to the buffer pointer sent as argument.
284 * The data area is automatically set to the `data_len'. This function
285 * can be used to set the data to static buffer without needing any
286 * memory allocations. The `data' will not be copied to the buffer.
290 * SilcBufferStruct buf;
291 * silc_buffer_set(&buf, data, data_len);
296 void silc_buffer_set(SilcBuffer sb, unsigned char *data, SilcUInt32 data_len)
298 sb->data = sb->head = data;
299 sb->tail = sb->end = data + data_len;
302 /****f* silcutil/SilcBufferAPI/silc_buffer_pull
307 * unsigned char *silc_buffer_pull(SilcBuffer sb, SilcUInt32 len);
311 * Pulls current data area towards end. The length of the currently
312 * valid data area is also decremented. Returns pointer to the data
313 * area before pulling. Returns NULL on error.
317 * ---------------------------------
318 * | head | data | tail |
319 * ---------------------------------
321 * Pulls the start of the data area.
323 * ---------------------------------
324 * | head | data | tail |
325 * ---------------------------------
328 * silc_buffer_pull(sb, 20);
333 unsigned char *silc_buffer_pull(SilcBuffer sb, SilcUInt32 len)
335 unsigned char *old_data = sb->data;
336 #if defined(SILC_DEBUG)
337 assert(len <= silc_buffer_len(sb));
339 if (len > silc_buffer_len(sb))
346 /****f* silcutil/SilcBufferAPI/silc_buffer_push
351 * unsigned char *silc_buffer_push(SilcBuffer sb, SilcUInt32 len);
355 * Pushes current data area towards beginning. Length of the currently
356 * valid data area is also incremented. Returns a pointer to the
357 * data area before pushing. Returns NULL on error.
361 * ---------------------------------
362 * | head | data | tail |
363 * ---------------------------------
365 * Pushes the start of the data area.
367 * ---------------------------------
368 * | head | data | tail |
369 * ---------------------------------
372 * silc_buffer_push(sb, 20);
377 unsigned char *silc_buffer_push(SilcBuffer sb, SilcUInt32 len)
379 unsigned char *old_data = sb->data;
380 #if defined(SILC_DEBUG)
381 assert((sb->data - len) >= sb->head);
383 if ((sb->data - len) < sb->head)
390 /****f* silcutil/SilcBufferAPI/silc_buffer_pull_tail
395 * unsigned char *silc_buffer_pull_tail(SilcBuffer sb, SilcUInt32 len);
399 * Pulls current tail section towards end. Length of the current valid
400 * data area is also incremented. Returns a pointer to the data area
401 * before pulling. Returns NULL on error.
405 * ---------------------------------
406 * | head | data | tail |
407 * ---------------------------------
409 * Pulls the start of the tail section.
411 * ---------------------------------
412 * | head | data | tail |
413 * ---------------------------------
416 * silc_buffer_pull_tail(sb, 23);
421 unsigned char *silc_buffer_pull_tail(SilcBuffer sb, SilcUInt32 len)
423 unsigned char *old_tail = sb->tail;
424 #if defined(SILC_DEBUG)
425 assert(len <= silc_buffer_taillen(sb));
427 if (len > silc_buffer_taillen(sb))
434 /****f* silcutil/SilcBufferAPI/silc_buffer_push_tail
439 * unsigned char *silc_buffer_push_tail(SilcBuffer sb, SilcUInt32 len);
443 * Pushes current tail section towards beginning. Length of the current
444 * valid data area is also decremented. Returns a pointer to the
445 * tail section before pushing. Returns NULL on error.
449 * ---------------------------------
450 * | head | data | tail |
451 * ---------------------------------
453 * Pushes the start of the tail section.
455 * ---------------------------------
456 * | head | data | tail |
457 * ---------------------------------
460 * silc_buffer_push_tail(sb, 23);
465 unsigned char *silc_buffer_push_tail(SilcBuffer sb, SilcUInt32 len)
467 unsigned char *old_tail = sb->tail;
468 #if defined(SILC_DEBUG)
469 assert((sb->tail - len) >= sb->data);
471 if ((sb->tail - len) < sb->data)
478 /****f* silcutil/SilcBufferAPI/silc_buffer_put_head
483 * unsigned char *silc_buffer_put_head(SilcBuffer sb,
484 * const unsigned char *data,
489 * Puts data at the head of the buffer. Returns pointer to the copied
490 * data area. Returns NULL on error.
494 * ---------------------------------
495 * | head | data | tail |
496 * ---------------------------------
498 * Puts data to the head section.
500 * silc_buffer_put_head(sb, data, data_len);
505 unsigned char *silc_buffer_put_head(SilcBuffer sb,
506 const unsigned char *data,
509 #if defined(SILC_DEBUG)
510 assert(len <= silc_buffer_headlen(sb));
512 if (len > silc_buffer_headlen(sb))
515 return (unsigned char *)memcpy(sb->head, data, len);
518 /****f* silcutil/SilcBufferAPI/silc_buffer_put
523 * unsigned char *silc_buffer_put(SilcBuffer sb,
524 * const unsigned char *data,
529 * Puts data at the start of the valid data area. Returns a pointer
530 * to the copied data area. Returns NULL on error.
534 * ---------------------------------
535 * | head | data | tail |
536 * ---------------------------------
538 * Puts data to the data section.
540 * silc_buffer_put(sb, data, data_len);
545 unsigned char *silc_buffer_put(SilcBuffer sb,
546 const unsigned char *data,
549 #if defined(SILC_DEBUG)
550 assert(len <= silc_buffer_len(sb));
552 if (len > silc_buffer_len(sb))
555 return (unsigned char *)memcpy(sb->data, data, len);
558 /****f* silcutil/SilcBufferAPI/silc_buffer_put_tail
563 * unsigned char *silc_buffer_put_tail(SilcBuffer sb,
564 * const unsigned char *data,
569 * Puts data at the tail of the buffer. Returns pointer to the copied
570 * data area. Returns NULL on error.
574 * ---------------------------------
575 * | head | data | tail |
576 * ---------------------------------
578 * Puts data to the tail section.
580 * silc_buffer_put_tail(sb, data, data_len);
585 unsigned char *silc_buffer_put_tail(SilcBuffer sb,
586 const unsigned char *data,
589 #if defined(SILC_DEBUG)
590 assert(len <= silc_buffer_taillen(sb));
592 if (len > silc_buffer_taillen(sb))
595 return (unsigned char *)memcpy(sb->tail, data, len);
598 /****f* silcutil/SilcBufferAPI/silc_buffer_alloc_size
603 * SilcBuffer silc_buffer_alloc_size(SilcUInt32 len);
607 * Allocates `len' bytes size buffer and moves the tail area automatically
608 * `len' bytes so that the buffer is ready to use without calling the
609 * silc_buffer_pull_tail. Returns NULL on error.
614 SilcBuffer silc_buffer_alloc_size(SilcUInt32 len)
616 SilcBuffer sb = silc_buffer_alloc(len);
619 silc_buffer_pull_tail(sb, len);
623 /****f* silcutil/SilcBufferAPI/silc_buffer_reset
628 * void silc_buffer_reset(SilcBuffer sb);
632 * Resets the buffer to the state as if it was just allocated by
633 * silc_buffer_alloc. This does not clear the data area. Use
634 * silc_buffer_clear if you also want to clear the data area.
639 void silc_buffer_reset(SilcBuffer sb)
641 sb->data = sb->tail = sb->head;
644 /****f* silcutil/SilcBufferAPI/silc_buffer_clear
649 * void silc_buffer_clear(SilcBuffer sb);
653 * Clears and initialiazes the buffer to the state as if it was just
654 * allocated by silc_buffer_alloc.
659 void silc_buffer_clear(SilcBuffer sb)
661 memset(sb->head, 0, silc_buffer_truelen(sb));
662 silc_buffer_reset(sb);
665 /****f* silcutil/SilcBufferAPI/silc_buffer_copy
670 * SilcBuffer silc_buffer_copy(SilcBuffer sb);
674 * Generates copy of a SilcBuffer. This copies everything inside the
675 * currently valid data area, nothing more. Use silc_buffer_clone to
676 * copy entire buffer. Returns NULL on error.
681 SilcBuffer silc_buffer_copy(SilcBuffer sb)
685 sb_new = silc_buffer_alloc_size(silc_buffer_len(sb));
688 silc_buffer_put(sb_new, sb->data, silc_buffer_len(sb));
693 /****f* silcutil/SilcBufferAPI/silc_buffer_clone
698 * SilcBuffer silc_buffer_clone(SilcBuffer sb);
702 * Clones SilcBuffer. This generates new SilcBuffer and copies
703 * everything from the source buffer. The result is exact clone of
704 * the original buffer. Returns NULL on error.
709 SilcBuffer silc_buffer_clone(SilcBuffer sb)
713 sb_new = silc_buffer_alloc_size(silc_buffer_truelen(sb));
716 silc_buffer_put(sb_new, sb->head, silc_buffer_truelen(sb));
717 sb_new->data = sb_new->head + silc_buffer_headlen(sb);
718 sb_new->tail = sb_new->data + silc_buffer_len(sb);
723 /****f* silcutil/SilcBufferAPI/silc_buffer_realloc
728 * SilcBuffer silc_buffer_realloc(SilcBuffer sb, SilcUInt32 newsize);
732 * Reallocates buffer. Old data is saved into the new buffer. The buffer
733 * is exact clone of the old one except that there is now more space
734 * at the end of buffer. This always returns the same `sb' unless `sb'
735 * was NULL. Returns NULL on error.
740 SilcBuffer silc_buffer_realloc(SilcBuffer sb, SilcUInt32 newsize)
742 SilcUInt32 hlen, dlen;
746 return silc_buffer_alloc(newsize);
748 if (newsize <= silc_buffer_truelen(sb))
751 hlen = silc_buffer_headlen(sb);
752 dlen = silc_buffer_len(sb);
753 h = (unsigned char *)silc_realloc(sb->head, newsize);
757 sb->data = sb->head + hlen;
758 sb->tail = sb->data + dlen;
759 sb->end = sb->head + newsize;
764 /****f* silcutil/SilcBufferAPI/silc_buffer_realloc_size
769 * SilcBuffer silc_buffer_realloc_size(SilcBuffer sb, SilcUInt32 newsize);
773 * Same as silc_buffer_realloc but moves moves the tail area
774 * automatically so that the buffer is ready to use without calling the
775 * silc_buffer_pull_tail. Returns NULL on error.
780 SilcBuffer silc_buffer_realloc_size(SilcBuffer sb, SilcUInt32 newsize)
782 sb = silc_buffer_realloc(sb, newsize);
785 silc_buffer_pull_tail(sb, silc_buffer_taillen(sb));
790 /* SilcStack aware SilcBuffer routines */
792 /****f* silcutil/SilcBufferAPI/silc_buffer_salloc
797 * SilcBuffer silc_buffer_salloc(SilcStack stack, SilcUInt32 len);
801 * Allocates new SilcBuffer and returns it.
803 * This routine use SilcStack are memory source.
808 SilcBuffer silc_buffer_salloc(SilcStack stack, SilcUInt32 len)
812 /* Allocate new SilcBuffer */
813 sb = (SilcBuffer)silc_scalloc(stack, 1, sizeof(*sb));
817 /* Allocate the actual data area */
818 sb->head = (unsigned char *)silc_smalloc_ua(stack, len);
822 /* Set pointers to the new buffer */
825 sb->end = sb->head + len;
830 /****f* silcutil/SilcBufferAPI/silc_buffer_salloc_size
835 * SilcBuffer silc_buffer_salloc_size(SilcStack stack, SilcUInt32 len);
839 * Allocates `len' bytes size buffer and moves the tail area automatically
840 * `len' bytes so that the buffer is ready to use without calling the
841 * silc_buffer_pull_tail.
843 * This routine use SilcStack are memory source.
848 SilcBuffer silc_buffer_salloc_size(SilcStack stack, SilcUInt32 len)
850 SilcBuffer sb = silc_buffer_salloc(stack, len);
853 silc_buffer_pull_tail(sb, len);
857 /****f* silcutil/SilcBufferAPI/silc_buffer_srealloc
862 * SilcBuffer silc_buffer_srealloc(SilcStack stack,
863 * SilcBuffer sb, SilcUInt32 newsize);
867 * Reallocates buffer. Old data is saved into the new buffer. The buffer
868 * is exact clone of the old one except that there is now more space
869 * at the end of buffer.
871 * This routine use SilcStack are memory source.
876 SilcBuffer silc_buffer_srealloc(SilcStack stack,
877 SilcBuffer sb, SilcUInt32 newsize)
879 SilcUInt32 hlen, dlen;
883 return silc_buffer_salloc(stack, newsize);
885 if (newsize <= silc_buffer_truelen(sb))
888 hlen = silc_buffer_headlen(sb);
889 dlen = silc_buffer_len(sb);
890 h = (unsigned char *)silc_srealloc_ua(stack, silc_buffer_truelen(sb),
893 /* Do slow and stack wasting realloc. The old sb->head is lost and
894 is freed eventually. */
895 h = silc_smalloc_ua(stack, newsize);
898 memcpy(h, sb->head, silc_buffer_truelen(sb));
902 sb->data = sb->head + hlen;
903 sb->tail = sb->data + dlen;
904 sb->end = sb->head + newsize;
909 /****f* silcutil/SilcBufferAPI/silc_buffer_srealloc_size
914 * SilcBuffer silc_buffer_srealloc_size(SilcStack stack,
915 * SilcBuffer sb, SilcUInt32 newsize);
919 * Same as silc_buffer_srealloc but moves moves the tail area
920 * automatically so that the buffer is ready to use without calling the
921 * silc_buffer_pull_tail.
923 * This routine use SilcStack are memory source.
928 SilcBuffer silc_buffer_srealloc_size(SilcStack stack,
929 SilcBuffer sb, SilcUInt32 newsize)
931 sb = silc_buffer_srealloc(stack, sb, newsize);
934 silc_buffer_pull_tail(sb, silc_buffer_taillen(sb));
938 /****f* silcutil/SilcBufferAPI/silc_buffer_scopy
943 * SilcBuffer silc_buffer_scopy(SilcStack stack, SilcBuffer sb);
947 * Generates copy of a SilcBuffer. This copies everything inside the
948 * currently valid data area, nothing more. Use silc_buffer_clone to
949 * copy entire buffer.
951 * This routine use SilcStack are memory source.
956 SilcBuffer silc_buffer_scopy(SilcStack stack, SilcBuffer sb)
960 sb_new = silc_buffer_salloc_size(stack, silc_buffer_len(sb));
963 silc_buffer_put(sb_new, sb->data, silc_buffer_len(sb));
968 /****f* silcutil/SilcBufferAPI/silc_buffer_sclone
973 * SilcBuffer silc_buffer_sclone(SilcStack stack, SilcBuffer sb);
977 * Clones SilcBuffer. This generates new SilcBuffer and copies
978 * everything from the source buffer. The result is exact clone of
979 * the original buffer.
981 * This routine use SilcStack are memory source.
986 SilcBuffer silc_buffer_sclone(SilcStack stack, SilcBuffer sb)
990 sb_new = silc_buffer_salloc_size(stack, silc_buffer_truelen(sb));
993 silc_buffer_put(sb_new, sb->head, silc_buffer_truelen(sb));
994 sb_new->data = sb_new->head + silc_buffer_headlen(sb);
995 sb_new->tail = sb_new->data + silc_buffer_len(sb);
1000 #endif /* SILCBUFFER_H */