5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 1998 - 2007 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.
108 * SILC Buffer is not thread-safe. If the same SilcBuffer context must be
109 * used in multithreaded environment concurrency control must be employed.
113 typedef struct SilcBufferObject {
118 } *SilcBuffer, SilcBufferStruct;
123 /****f* silcutil/SilcBufferAPI/silc_buffer_data
127 * unsigned char *silc_buffer_data(SilcBuffer sb)
131 * Returns pointer to the data area of the buffer.
135 #define silc_buffer_data(x) (x)->data
138 /****f* silcutil/SilcBufferAPI/silc_buffer_datalen
142 * #define silc_buffer_datalen ...
146 * Macro that can be used in function argument list to give the data
147 * pointer and the data length, instead of calling both silc_buffer_data
148 * and silc_buffer_len separately.
152 * // Following are the same thing
153 * silc_foo_function(foo, silc_buffer_datalen(buf));
154 * silc_foo_function(foo, silc_buffer_data(buf), silc_buffer_len(buf));
158 #define silc_buffer_datalen(x) (x) ? silc_buffer_data((x)) : NULL, \
159 (x) ? silc_buffer_len((x)) : 0
162 /* Inline functions */
164 /****d* silcutil/SilcBufferAPI/silc_buffer_truelen
168 * SilcUInt32 silc_buffer_truelen(SilcBuffer sb)
172 * Returns the true length of the buffer.
176 SilcUInt32 silc_buffer_truelen(SilcBuffer x)
178 return (SilcUInt32)(x->end - x->head);
181 /****d* silcutil/SilcBufferAPI/silc_buffer_len
185 * SilcUInt32 silc_buffer_len(SilcBuffer sb)
189 * Returns the current length of the data area of the buffer.
193 SilcUInt32 silc_buffer_len(SilcBuffer x)
195 return (SilcUInt32)(x->tail - x->data);
198 /****d* silcutil/SilcBufferAPI/silc_buffer_headlen
202 * SilcUInt32 silc_buffer_headlen(SilcBuffer sb)
206 * Returns the current length of the head data area of the buffer.
210 SilcUInt32 silc_buffer_headlen(SilcBuffer x)
212 return (SilcUInt32)(x->data - x->head);
215 /****d* silcutil/SilcBufferAPI/silc_buffer_taillen
219 * SilcUInt32 silc_buffer_taillen(SilcBuffer sb)
223 * Returns the current length of the tail data area of the buffer.
227 SilcUInt32 silc_buffer_taillen(SilcBuffer x)
229 return (SilcUInt32)(x->end - x->tail);
232 /****f* silcutil/SilcBufferAPI/silc_buffer_alloc
237 * SilcBuffer silc_buffer_alloc(SilcUInt32 len);
241 * Allocates new SilcBuffer and returns it. Returns NULL if system is
247 SilcBuffer silc_buffer_alloc(SilcUInt32 len)
251 /* Allocate new SilcBuffer */
252 sb = (SilcBuffer)silc_calloc(1, sizeof(*sb));
253 if (silc_unlikely(!sb))
256 if (silc_likely(len)) {
257 /* Allocate the actual data area */
258 sb->head = (unsigned char *)silc_malloc(len * sizeof(*sb->head));
259 if (silc_unlikely(!sb->head))
262 /* Set pointers to the new buffer */
265 sb->end = sb->head + len;
271 /****f* silcutil/SilcBufferAPI/silc_buffer_salloc
276 * SilcBuffer silc_buffer_salloc(SilcStack stack, SilcUInt32 len);
280 * Allocates new SilcBuffer and returns it. Returns NULL if system is
283 * This routine use SilcStack are memory source. If `stack' is NULL
284 * reverts back to normal allocating routine.
286 * Note that this call consumes the `stack'. The caller should push the
287 * stack before calling the function and pop it later.
292 SilcBuffer silc_buffer_salloc(SilcStack stack, SilcUInt32 len)
297 return silc_buffer_alloc(len);
299 /* Allocate new SilcBuffer */
300 sb = (SilcBuffer)silc_scalloc(stack, 1, sizeof(*sb));
301 if (silc_unlikely(!sb))
304 if (silc_likely(len)) {
305 /* Allocate the actual data area */
306 sb->head = (unsigned char *)silc_smalloc(stack, len * sizeof(*sb->head));
307 if (silc_unlikely(!sb->head))
310 /* Set pointers to the new buffer */
313 sb->end = sb->head + len;
319 /****f* silcutil/SilcBufferAPI/silc_buffer_free
324 * void silc_buffer_free(SilcBuffer sb);
328 * Frees SilcBuffer. Can be called safely `sb' as NULL.
332 * Must not be called for buffers allocated with silc_buffer_salloc,
333 * silc_buffer_salloc_size, silc_buffer_scopy and silc_buffer_sclone.
334 * Call silc_buffer_sfree instead.
339 void silc_buffer_free(SilcBuffer sb)
342 #if defined(SILC_DEBUG)
344 memset(sb->head, 'F', silc_buffer_truelen(sb));
351 /****f* silcutil/SilcBufferAPI/silc_buffer_sfree
356 * void silc_buffer_free(SilcStack stack, SilcBuffer sb);
360 * Frees SilcBuffer. If `stack' is NULL this calls silc_buffer_free. Can
361 * be called safely `sb' as NULL.
366 void silc_buffer_sfree(SilcStack stack, SilcBuffer sb)
372 memset(sb->head, 'F', silc_buffer_truelen(sb));
373 memset(sb, 'F', sizeof(*sb));
375 #endif /* SILC_DEBUG */
379 silc_buffer_free(sb);
382 /****f* silcutil/SilcBufferAPI/silc_buffer_steal
387 * unsigned char *silc_buffer_steal(SilcBuffer sb, SilcUInt32 *data_len);
391 * Steals the data from the buffer `sb'. This returns pointer to the
392 * start of the buffer and the true length of that buffer. The `sb'
393 * cannot be used anymore after calling this function because the
394 * data buffer was stolen. The `sb' must be freed with silc_buffer_free.
395 * The caller is responsible of freeing the stolen data buffer with
401 unsigned char *silc_buffer_steal(SilcBuffer sb, SilcUInt32 *data_len)
403 unsigned char *buf = sb->head;
405 *data_len = silc_buffer_truelen(sb);
406 sb->head = sb->data = sb->tail = sb->end = NULL;
410 /****f* silcutil/SilcBufferAPI/silc_buffer_purge
415 * void silc_buffer_purge(SilcBuffer sb);
419 * Same as silc_buffer_free but free's only the contents of the buffer
420 * not the buffer itself. The `sb' remains intact, data is freed. Buffer
421 * is ready for re-use after calling this function.
425 * Must not be called for buffers allocated with silc_buffer_salloc,
426 * silc_buffer_salloc_size, silc_buffer_scopy and silc_buffer_sclone.
427 * Use silc_buffer_spurge instead.
432 void silc_buffer_purge(SilcBuffer sb)
434 silc_free(silc_buffer_steal(sb, NULL));
437 /****f* silcutil/SilcBufferAPI/silc_buffer_spurge
442 * void silc_buffer_spurge(SilcStack stack, SilcBuffer sb);
446 * Same as silc_buffer_free but free's only the contents of the buffer
447 * not the buffer itself. The `sb' remains intact, data is freed. Buffer
448 * is ready for re-use after calling this function. If `stack' is NULL
449 * this calls silc_buffer_purge.
454 void silc_buffer_spurge(SilcStack stack, SilcBuffer sb)
459 memset(silc_buffer_steal(sb, NULL), 'F', silc_buffer_truelen(sb));
460 #endif /* SILC_DEBUG */
464 silc_buffer_purge(sb);
467 /****f* silcutil/SilcBufferAPI/silc_buffer_set
472 * void silc_buffer_set(SilcBuffer sb,
473 * unsigned char *data,
474 * SilcUInt32 data_len);
478 * Sets the `data' and `data_len' to the buffer pointer sent as argument.
479 * The data area is automatically set to the `data_len'. This function
480 * can be used to set the data to static buffer without needing any
481 * memory allocations. The `data' will not be copied to the buffer.
485 * SilcBufferStruct buf;
486 * silc_buffer_set(&buf, data, data_len);
491 void silc_buffer_set(SilcBuffer sb, unsigned char *data, SilcUInt32 data_len)
493 sb->data = sb->head = data;
494 sb->tail = sb->end = data + data_len;
497 /****f* silcutil/SilcBufferAPI/silc_buffer_pull
502 * unsigned char *silc_buffer_pull(SilcBuffer sb, SilcUInt32 len);
506 * Pulls current data area towards end. The length of the currently
507 * valid data area is also decremented. Returns pointer to the data
508 * area before pulling. Returns NULL if the pull would lead to buffer
509 * overflow or would go beyond the valid data area.
513 * ---------------------------------
514 * | head | data | tail |
515 * ---------------------------------
517 * Pulls the start of the data area.
519 * ---------------------------------
520 * | head | data | tail |
521 * ---------------------------------
524 * silc_buffer_pull(sb, 20);
529 unsigned char *silc_buffer_pull(SilcBuffer sb, SilcUInt32 len)
531 unsigned char *old_data = sb->data;
533 #ifdef SILC_DIST_INPLACE
534 SILC_ASSERT(len <= silc_buffer_len(sb));
535 #endif /* SILC_DIST_INPLACE */
536 if (silc_unlikely(len > silc_buffer_len(sb))) {
537 silc_set_errno(SILC_ERR_OVERFLOW);
545 /****f* silcutil/SilcBufferAPI/silc_buffer_push
550 * unsigned char *silc_buffer_push(SilcBuffer sb, SilcUInt32 len);
554 * Pushes current data area towards beginning. Length of the currently
555 * valid data area is also incremented. Returns a pointer to the
556 * data area before pushing. Returns NULL if the push would lead to
557 * buffer underflow or would go under the valid data area.
561 * ---------------------------------
562 * | head | data | tail |
563 * ---------------------------------
565 * Pushes the start of the data area.
567 * ---------------------------------
568 * | head | data | tail |
569 * ---------------------------------
572 * silc_buffer_push(sb, 20);
577 unsigned char *silc_buffer_push(SilcBuffer sb, SilcUInt32 len)
579 unsigned char *old_data = sb->data;
581 #ifdef SILC_DIST_INPLACE
582 SILC_ASSERT((sb->data - len) >= sb->head);
583 #endif /* SILC_DIST_INPLACE */
584 if (silc_unlikely((sb->data - len) < sb->head)) {
585 silc_set_errno(SILC_ERR_UNDERFLOW);
593 /****f* silcutil/SilcBufferAPI/silc_buffer_pull_tail
598 * unsigned char *silc_buffer_pull_tail(SilcBuffer sb, SilcUInt32 len);
602 * Pulls current tail section towards end. Length of the current valid
603 * data area is also incremented. Returns a pointer to the data area
604 * before pulling. Returns NULL if the pull would lead to buffer overflow.
608 * ---------------------------------
609 * | head | data | tail |
610 * ---------------------------------
612 * Pulls the start of the tail section.
614 * ---------------------------------
615 * | head | data | tail |
616 * ---------------------------------
619 * silc_buffer_pull_tail(sb, 23);
624 unsigned char *silc_buffer_pull_tail(SilcBuffer sb, SilcUInt32 len)
626 unsigned char *old_tail = sb->tail;
628 #ifdef SILC_DIST_INPLACE
629 SILC_ASSERT(len <= silc_buffer_taillen(sb));
630 #endif /* SILC_DIST_INPLACE */
631 if (silc_unlikely(len > silc_buffer_taillen(sb))) {
632 silc_set_errno(SILC_ERR_OVERFLOW);
640 /****f* silcutil/SilcBufferAPI/silc_buffer_push_tail
645 * unsigned char *silc_buffer_push_tail(SilcBuffer sb, SilcUInt32 len);
649 * Pushes current tail section towards beginning. Length of the current
650 * valid data area is also decremented. Returns a pointer to the
651 * tail section before pushing. Returns NULL if the push would lead to
652 * buffer underflow or go under valid tail area.
656 * ---------------------------------
657 * | head | data | tail |
658 * ---------------------------------
660 * Pushes the start of the tail section.
662 * ---------------------------------
663 * | head | data | tail |
664 * ---------------------------------
667 * silc_buffer_push_tail(sb, 23);
672 unsigned char *silc_buffer_push_tail(SilcBuffer sb, SilcUInt32 len)
674 unsigned char *old_tail = sb->tail;
676 #ifdef SILC_DIST_INPLACE
677 SILC_ASSERT((sb->tail - len) >= sb->data);
678 #endif /* SILC_DIST_INPLACE */
679 if (silc_unlikely((sb->tail - len) < sb->data)) {
680 silc_set_errno(SILC_ERR_UNDERFLOW);
688 /****f* silcutil/SilcBufferAPI/silc_buffer_put_head
693 * unsigned char *silc_buffer_put_head(SilcBuffer sb,
694 * const unsigned char *data,
699 * Puts data at the head of the buffer. Returns pointer to the copied
700 * data area. Returns NULL if the data is longer that the current head
705 * ---------------------------------
706 * | head | data | tail |
707 * ---------------------------------
709 * Puts data to the head section.
711 * silc_buffer_put_head(sb, data, data_len);
716 unsigned char *silc_buffer_put_head(SilcBuffer sb,
717 const unsigned char *data,
720 #ifdef SILC_DIST_INPLACE
721 SILC_ASSERT(len <= silc_buffer_headlen(sb));
722 #endif /* SILC_DIST_INPLACE */
723 if (silc_unlikely(len > silc_buffer_headlen(sb))) {
724 silc_set_errno(SILC_ERR_OVERFLOW);
728 return (unsigned char *)memcpy(sb->head, data, len);
731 /****f* silcutil/SilcBufferAPI/silc_buffer_put
736 * unsigned char *silc_buffer_put(SilcBuffer sb,
737 * const unsigned char *data,
742 * Puts data at the start of the valid data area. Returns a pointer
743 * to the copied data area. Returns NULL if the data is longer than the
748 * ---------------------------------
749 * | head | data | tail |
750 * ---------------------------------
752 * Puts data to the data section.
754 * silc_buffer_put(sb, data, data_len);
759 unsigned char *silc_buffer_put(SilcBuffer sb,
760 const unsigned char *data,
763 #ifdef SILC_DIST_INPLACE
764 SILC_ASSERT(len <= silc_buffer_len(sb));
765 #endif /* SILC_DIST_INPLACE */
766 if (silc_unlikely(len > silc_buffer_len(sb))) {
767 silc_set_errno(SILC_ERR_OVERFLOW);
771 return (unsigned char *)memcpy(sb->data, data, len);
774 /****f* silcutil/SilcBufferAPI/silc_buffer_put_tail
779 * unsigned char *silc_buffer_put_tail(SilcBuffer sb,
780 * const unsigned char *data,
785 * Puts data at the tail of the buffer. Returns pointer to the copied
786 * data area. Returns NULL if the data is longer than the current tail
791 * ---------------------------------
792 * | head | data | tail |
793 * ---------------------------------
795 * Puts data to the tail section.
797 * silc_buffer_put_tail(sb, data, data_len);
802 unsigned char *silc_buffer_put_tail(SilcBuffer sb,
803 const unsigned char *data,
806 #ifdef SILC_DIST_INPLACE
807 SILC_ASSERT(len <= silc_buffer_taillen(sb));
808 #endif /* SILC_DIST_INPLACE */
809 if (silc_unlikely(len > silc_buffer_taillen(sb))) {
810 silc_set_errno(SILC_ERR_OVERFLOW);
814 return (unsigned char *)memcpy(sb->tail, data, len);
817 /****f* silcutil/SilcBufferAPI/silc_buffer_alloc_size
822 * SilcBuffer silc_buffer_alloc_size(SilcUInt32 len);
826 * Allocates `len' bytes size buffer and moves the tail area automatically
827 * `len' bytes so that the buffer is ready to use without calling the
828 * silc_buffer_pull_tail. Returns NULL if system is out of memory.
833 SilcBuffer silc_buffer_alloc_size(SilcUInt32 len)
835 SilcBuffer sb = silc_buffer_alloc(len);
836 if (silc_unlikely(!sb))
838 silc_buffer_pull_tail(sb, len);
842 /****f* silcutil/SilcBufferAPI/silc_buffer_salloc_size
847 * SilcBuffer silc_buffer_salloc_size(SilcStack stack, SilcUInt32 len);
851 * Allocates `len' bytes size buffer and moves the tail area automatically
852 * `len' bytes so that the buffer is ready to use without calling the
853 * silc_buffer_pull_tail. Returns NULL if system is out of memory.
855 * This routine use SilcStack are memory source. If `stack' is NULL
856 * reverts back to normal allocating routine.
858 * Note that this call consumes the `stack'. The caller should push the
859 * stack before calling the function and pop it later.
864 SilcBuffer silc_buffer_salloc_size(SilcStack stack, SilcUInt32 len)
866 SilcBuffer sb = silc_buffer_salloc(stack, len);
867 if (silc_unlikely(!sb))
869 silc_buffer_pull_tail(sb, len);
873 /****f* silcutil/SilcBufferAPI/silc_buffer_reset
878 * void silc_buffer_reset(SilcBuffer sb);
882 * Resets the buffer to the state as if it was just allocated by
883 * silc_buffer_alloc. This does not clear the data area. Use
884 * silc_buffer_clear if you also want to clear the data area.
889 void silc_buffer_reset(SilcBuffer sb)
891 sb->data = sb->tail = sb->head;
894 /****f* silcutil/SilcBufferAPI/silc_buffer_clear
899 * void silc_buffer_clear(SilcBuffer sb);
903 * Clears and initialiazes the buffer to the state as if it was just
904 * allocated by silc_buffer_alloc.
909 void silc_buffer_clear(SilcBuffer sb)
911 memset(sb->head, 0, silc_buffer_truelen(sb));
912 silc_buffer_reset(sb);
915 /****f* silcutil/SilcBufferAPI/silc_buffer_start
920 * void silc_buffer_start(SilcBuffer sb);
924 * Moves the data area at the start of the buffer. The tail area remains
930 void silc_buffer_start(SilcBuffer sb)
935 /****f* silcutil/SilcBufferAPI/silc_buffer_end
940 * void silc_buffer_end(SilcBuffer sb);
944 * Moves the end of the data area to the end of the buffer. The start
945 * of the data area remains same. If the start of data area is at the
946 * start of the buffer, after this function returns the buffer's data
947 * area length is the length of the entire buffer.
952 void silc_buffer_end(SilcBuffer sb)
957 /****f* silcutil/SilcBufferAPI/silc_buffer_copy
962 * SilcBuffer silc_buffer_copy(SilcBuffer sb);
966 * Generates copy of a SilcBuffer. This copies everything inside the
967 * currently valid data area, nothing more. Use silc_buffer_clone to
968 * copy entire buffer. Returns NULL if system is out of memory.
973 SilcBuffer silc_buffer_copy(SilcBuffer sb)
977 sb_new = silc_buffer_alloc_size(silc_buffer_len(sb));
978 if (silc_unlikely(!sb_new))
980 silc_buffer_put(sb_new, sb->data, silc_buffer_len(sb));
985 /****f* silcutil/SilcBufferAPI/silc_buffer_scopy
990 * SilcBuffer silc_buffer_scopy(SilcStack stack, SilcBuffer sb);
994 * Generates copy of a SilcBuffer. This copies everything inside the
995 * currently valid data area, nothing more. Use silc_buffer_clone to
996 * copy entire buffer. Returns NULL if system is out of memory.
998 * This routine use SilcStack are memory source. If `stack' is NULL
999 * reverts back to normal allocating routine.
1001 * Note that this call consumes the `stack'. The caller should push the
1002 * stack before calling the function and pop it later.
1007 SilcBuffer silc_buffer_scopy(SilcStack stack, SilcBuffer sb)
1011 sb_new = silc_buffer_salloc_size(stack, silc_buffer_len(sb));
1012 if (silc_unlikely(!sb_new))
1014 silc_buffer_put(sb_new, sb->data, silc_buffer_len(sb));
1019 /****f* silcutil/SilcBufferAPI/silc_buffer_clone
1024 * SilcBuffer silc_buffer_clone(SilcBuffer sb);
1028 * Clones SilcBuffer. This generates new SilcBuffer and copies
1029 * everything from the source buffer. The result is exact clone of
1030 * the original buffer. Returns NULL if system is out of memory.
1035 SilcBuffer silc_buffer_clone(SilcBuffer sb)
1039 sb_new = silc_buffer_alloc_size(silc_buffer_truelen(sb));
1040 if (silc_unlikely(!sb_new))
1042 silc_buffer_put(sb_new, sb->head, silc_buffer_truelen(sb));
1043 sb_new->data = sb_new->head + silc_buffer_headlen(sb);
1044 sb_new->tail = sb_new->data + silc_buffer_len(sb);
1049 /****f* silcutil/SilcBufferAPI/silc_buffer_sclone
1054 * SilcBuffer silc_buffer_sclone(SilcStack stack, SilcBuffer sb);
1058 * Clones SilcBuffer. This generates new SilcBuffer and copies
1059 * everything from the source buffer. The result is exact clone of
1060 * the original buffer. Returns NULL if system is out of memory.
1062 * This routine use SilcStack are memory source. If `stack' is NULL
1063 * reverts back to normal allocating routine.
1065 * Note that this call consumes the `stack'. The caller should push the
1066 * stack before calling the function and pop it later.
1071 SilcBuffer silc_buffer_sclone(SilcStack stack, SilcBuffer sb)
1075 sb_new = silc_buffer_salloc_size(stack, silc_buffer_truelen(sb));
1076 if (silc_unlikely(!sb_new))
1078 silc_buffer_put(sb_new, sb->head, silc_buffer_truelen(sb));
1079 sb_new->data = sb_new->head + silc_buffer_headlen(sb);
1080 sb_new->tail = sb_new->data + silc_buffer_len(sb);
1085 /****f* silcutil/SilcBufferAPI/silc_buffer_realloc
1090 * SilcBuffer silc_buffer_realloc(SilcBuffer sb, SilcUInt32 newsize);
1094 * Reallocates buffer. Old data is saved into the new buffer. The buffer
1095 * is exact clone of the old one except that there is now more space
1096 * at the end of buffer. This always returns the same `sb' unless `sb'
1097 * was NULL. Returns NULL if system is out of memory.
1102 SilcBuffer silc_buffer_realloc(SilcBuffer sb, SilcUInt32 newsize)
1104 SilcUInt32 hlen, dlen;
1108 return silc_buffer_alloc(newsize);
1110 if (silc_unlikely(newsize <= silc_buffer_truelen(sb)))
1113 hlen = silc_buffer_headlen(sb);
1114 dlen = silc_buffer_len(sb);
1115 h = (unsigned char *)silc_realloc(sb->head, newsize);
1116 if (silc_unlikely(!h))
1119 sb->data = sb->head + hlen;
1120 sb->tail = sb->data + dlen;
1121 sb->end = sb->head + newsize;
1126 /****f* silcutil/SilcBufferAPI/silc_buffer_srealloc
1131 * SilcBuffer silc_buffer_srealloc(SilcStack stack,
1132 * SilcBuffer sb, SilcUInt32 newsize);
1136 * Reallocates buffer. Old data is saved into the new buffer. The buffer
1137 * is exact clone of the old one except that there is now more space
1138 * at the end of buffer. Returns NULL if system is out of memory.
1140 * This routine use SilcStack are memory source. If `stack' is NULL
1141 * reverts back to normal allocating routine.
1143 * Note that this call consumes the `stack'. The caller should push the
1144 * stack before calling the function and pop it later.
1149 SilcBuffer silc_buffer_srealloc(SilcStack stack,
1150 SilcBuffer sb, SilcUInt32 newsize)
1152 SilcUInt32 hlen, dlen;
1156 return silc_buffer_realloc(sb, newsize);
1159 return silc_buffer_salloc(stack, newsize);
1161 if (newsize <= silc_buffer_truelen(sb))
1164 hlen = silc_buffer_headlen(sb);
1165 dlen = silc_buffer_len(sb);
1166 h = (unsigned char *)silc_srealloc(stack, silc_buffer_truelen(sb),
1169 /* Do slow and stack wasting realloc. The old sb->head is lost and
1170 is freed eventually. */
1171 h = (unsigned char *)silc_smalloc(stack, newsize);
1172 if (silc_unlikely(!h))
1174 memcpy(h, sb->head, silc_buffer_truelen(sb));
1178 sb->data = sb->head + hlen;
1179 sb->tail = sb->data + dlen;
1180 sb->end = sb->head + newsize;
1185 /****f* silcutil/SilcBufferAPI/silc_buffer_realloc_size
1190 * SilcBuffer silc_buffer_realloc_size(SilcBuffer sb, SilcUInt32 newsize);
1194 * Same as silc_buffer_realloc but moves moves the tail area
1195 * automatically so that the buffer is ready to use without calling the
1196 * silc_buffer_pull_tail. Returns NULL if system is out of memory.
1201 SilcBuffer silc_buffer_realloc_size(SilcBuffer sb, SilcUInt32 newsize)
1203 sb = silc_buffer_realloc(sb, newsize);
1204 if (silc_unlikely(!sb))
1206 silc_buffer_pull_tail(sb, silc_buffer_taillen(sb));
1210 /****f* silcutil/SilcBufferAPI/silc_buffer_srealloc_size
1215 * SilcBuffer silc_buffer_srealloc_size(SilcStack stack,
1216 * SilcBuffer sb, SilcUInt32 newsize);
1220 * Same as silc_buffer_srealloc but moves moves the tail area
1221 * automatically so that the buffer is ready to use without calling the
1222 * silc_buffer_pull_tail. Returns NULL if system is out of memory.
1224 * This routine use SilcStack are memory source. If `stack' is NULL
1225 * reverts back to normal allocating routine.
1227 * Note that this call consumes the `stack'. The caller should push the
1228 * stack before calling the function and pop it later.
1233 SilcBuffer silc_buffer_srealloc_size(SilcStack stack,
1234 SilcBuffer sb, SilcUInt32 newsize)
1236 sb = silc_buffer_srealloc(stack, sb, newsize);
1237 if (silc_unlikely(!sb))
1239 silc_buffer_pull_tail(sb, silc_buffer_taillen(sb));
1243 /****f* silcutil/SilcBufferAPI/silc_buffer_enlarge
1248 * SilcBuffer silc_buffer_enlarge(SilcBuffer sb, SilcUInt32 size);
1252 * Enlarges the buffer by the amount of `size' if it doesn't have that
1253 * must space in the data area and in the tail area. Moves the tail
1254 * area automatically after enlarging so that the current data area
1255 * is at least the size of `size'. If there is more space than `size'
1256 * in the data area this does not do anything. If there is enough
1257 * space in the tail area this merely moves the tail area to reveal
1258 * the extra space. Returns FALSE if system is out of memory.
1263 SilcBool silc_buffer_enlarge(SilcBuffer sb, SilcUInt32 size)
1265 if (size > silc_buffer_len(sb)) {
1266 if (size > silc_buffer_taillen(sb) + silc_buffer_len(sb))
1267 if (silc_unlikely(!silc_buffer_realloc(sb, silc_buffer_truelen(sb) +
1268 (size - silc_buffer_taillen(sb) -
1269 silc_buffer_len(sb)))))
1271 silc_buffer_pull_tail(sb, size - silc_buffer_len(sb));
1276 /****f* silcutil/SilcBufferAPI/silc_buffer_senlarge
1281 * SilcBuffer silc_buffer_senlarge(SilcStack stack, SilcBuffer sb,
1286 * Enlarges the buffer by the amount of `size' if it doesn't have that
1287 * must space in the data area and in the tail area. Moves the tail
1288 * area automatically after enlarging so that the current data area
1289 * is at least the size of `size'. If there is more space than `size'
1290 * in the data area this does not do anything. If there is enough
1291 * space in the tail area this merely moves the tail area to reveal
1292 * the extra space. Returns FALSE if system is out of memory.
1294 * This routine use SilcStack are memory source. If `stack' is NULL
1295 * reverts back to normal allocating routine.
1297 * Note that this call consumes the `stack'. The caller should push the
1298 * stack before calling the function and pop it later.
1303 SilcBool silc_buffer_senlarge(SilcStack stack, SilcBuffer sb, SilcUInt32 size)
1305 if (size > silc_buffer_len(sb)) {
1306 if (size > silc_buffer_taillen(sb) + silc_buffer_len(sb))
1307 if (silc_unlikely(!silc_buffer_srealloc(stack, sb,
1308 silc_buffer_truelen(sb) +
1309 (size - silc_buffer_taillen(sb) -
1310 silc_buffer_len(sb)))))
1312 silc_buffer_pull_tail(sb, size - silc_buffer_len(sb));
1317 /****f* silcutil/SilcBufferAPI/silc_buffer_strchr
1322 * unsigned char *silc_buffer_strchr(SilcBuffer sb, int c, SilcBool first);
1326 * Returns pointer to the occurence of the character `c' in the buffer
1327 * `sb'. If the `first' is TRUE this finds the first occurene of `c',
1328 * if it is FALSE this finds the last occurence of `c'. If the character
1329 * is found the `sb' data area is moved to that location and its pointer
1330 * is returned. The silc_buffer_data call will return the same pointer.
1331 * Returns NULL if such character could not be located and the buffer
1332 * remains unmodified.
1334 * This call is equivalent to strchr(), strrchr(), memchr() and memrchr()
1335 * except it works with SilcBuffer.
1339 * This searches only the data area of the buffer. Head and tail area
1342 * The `sb' data need not be NULL terminated.
1347 unsigned char *silc_buffer_strchr(SilcBuffer sb, int c, SilcBool first)
1352 for (i = 0; i < silc_buffer_len(sb); i++) {
1353 if (sb->data[i] == (unsigned char)c) {
1354 sb->data = &sb->data[i];
1359 for (i = silc_buffer_len(sb) - 1; 1 >= 0; i--) {
1360 if (sb->data[i] == (unsigned char)c) {
1361 sb->data = &sb->data[i];
1370 #endif /* SILCBUFFER_H */