7599868baeea4234906d7cefc0acde91c26da1b4
[silc.git] / lib / silccore / silcbufutil.c
1 /*
2
3   silcbufutil.c
4
5   Author: Pekka Riikonen <priikone@poseidon.pspt.fi>
6
7   Copyright (C) 1997 - 2000 Pekka Riikonen
8
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.
13   
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.
18
19 */
20 /*
21  * $Id$
22  * $Log$
23  * Revision 1.3  2000/07/14 06:08:49  priikone
24  *      Added silc_buffer_realloc. Fixed silc_buffer_clone.
25  *
26  * Revision 1.2  2000/07/05 06:06:35  priikone
27  *      Global cosmetic change.
28  *
29  * Revision 1.1.1.1  2000/06/27 11:36:55  priikone
30  *      Imported from internal CVS/Added Log headers.
31  *
32  *
33  */
34
35 #include "silcincludes.h"
36
37 #ifdef SILC_DEBUG               /* If we are doing debugging we won't
38                                    have the optimized inline buffer functions
39                                    available as optimization is not set
40                                    to compiler. These normal routines are
41                                    used in debugging mode. */
42
43 /* Clears and initialiazes the buffer to the state as if it was just
44    allocated by silc_buffer_alloc. */
45
46 void silc_buffer_clear(SilcBuffer sb)
47 {
48   memset(sb->head, 0, sb->truelen);
49   sb->data = sb->head;
50   sb->tail = sb->head;
51   sb->len = 0;
52 }
53
54 /* Generates copy of a SilcBuffer. This copies everything inside the
55    currently valid data area, nothing more. Use silc_buffer_clone to
56    copy entire buffer. */
57
58 SilcBuffer silc_buffer_copy(SilcBuffer sb)
59 {
60   SilcBuffer sb_new;
61
62   sb_new = silc_buffer_alloc(sb->len);
63   silc_buffer_pull_tail(sb_new, SILC_BUFFER_END(sb_new));
64   silc_buffer_put(sb_new, sb->data, sb->len);
65
66   return sb_new;
67 }
68
69 /* Clones SilcBuffer. This generates new SilcBuffer and copies
70    everything from the source buffer. The result is exact clone of
71    the original buffer. */
72
73 SilcBuffer silc_buffer_clone(SilcBuffer sb)
74 {
75   SilcBuffer sb_new;
76
77   sb_new = silc_buffer_alloc(sb->truelen);
78   silc_buffer_pull_tail(sb_new, SILC_BUFFER_END(sb_new));
79   silc_buffer_put(sb_new, sb->head, sb->truelen);
80   sb_new->data = sb_new->head + (sb->data - sb->head);
81   sb_new->tail = sb_new->data + sb->len;
82   sb_new->len = sb->len;
83
84   return sb_new;
85 }
86
87 /* Reallocates buffer. Old data is saved into the new buffer. Returns
88    new SilcBuffer pointer. The buffer is exact clone of the old one
89    except that there is now more space at the end of buffer. */
90
91 SilcBuffer silc_buffer_realloc(SilcBuffer sb, unsigned int newsize)
92 {
93   SilcBuffer sb_new;
94
95   sb_new = silc_buffer_alloc(newsize);
96   silc_buffer_pull_tail(sb_new, SILC_BUFFER_END(sb_new));
97   silc_buffer_put(sb_new, sb->head, sb->truelen);
98   sb_new->data = sb_new->head + (sb->data - sb->head);
99   sb_new->tail = sb_new->data + sb->len;
100   sb_new->len = sb->len;
101
102   silc_buffer_free(sb);
103
104   return sb_new;
105 }
106
107 #endif /* SILC_DEBUG */