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