5 Author: Pekka Riikonen <priikone@poseidon.pspt.fi>
7 Copyright (C) 1997 - 2000 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; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
24 #include "silcbuffer.h"
26 /* Clears and initialiazes the buffer to the state as if it was just
27 allocated by silc_buffer_alloc. */
30 void silc_buffer_clear(SilcBuffer sb)
32 memset(sb->head, 0, sb->truelen);
38 /* Generates copy of a SilcBuffer. This copies everything inside the
39 currently valid data area, nothing more. Use silc_buffer_clone to
40 copy entire buffer. */
43 SilcBuffer silc_buffer_copy(SilcBuffer sb)
47 sb_new = silc_buffer_alloc(sb->len);
48 silc_buffer_pull_tail(sb_new, SILC_BUFFER_END(sb_new));
49 silc_buffer_put(sb_new, sb->data, sb->len);
54 /* Clones SilcBuffer. This generates new SilcBuffer and copies
55 everything from the source buffer. The result is exact clone of
56 the original buffer. */
59 SilcBuffer silc_buffer_clone(SilcBuffer sb)
63 sb_new = silc_buffer_alloc(sb->truelen);
64 silc_buffer_pull_tail(sb_new, SILC_BUFFER_END(sb_new));
65 silc_buffer_put(sb_new, sb->head, sb->truelen);
66 sb_new->data = sb_new->head + (sb->data - sb->head);
67 sb_new->tail = sb_new->data + sb->len;
68 sb_new->len = sb->len;
73 /* Reallocates buffer. Old data is saved into the new buffer. Returns
74 new SilcBuffer pointer. The buffer is exact clone of the old one
75 except that there is now more space at the end of buffer. */
78 SilcBuffer silc_buffer_realloc(SilcBuffer sb, uint32 newsize)
83 return silc_buffer_alloc(newsize);
85 if (newsize <= sb->truelen)
88 sb_new = silc_buffer_alloc(newsize);
89 silc_buffer_pull_tail(sb_new, SILC_BUFFER_END(sb_new));
90 silc_buffer_put(sb_new, sb->head, sb->truelen);
91 sb_new->data = sb_new->head + (sb->data - sb->head);
92 sb_new->tail = sb_new->data + sb->len;
93 sb_new->len = sb->len;