Moved silc_client_ch[u]mode[_char] to client library from silc/.
[silc.git] / lib / silcutil / silcbufutil.h
1 /*
2
3   silcbufutil.h
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 #ifndef SILCBUFUTIL_H
22 #define SILCBUFUTIL_H
23
24 #ifndef SILC_DEBUG              /* When we are not doing debugging we use
25                                    optimized inline buffer functions. */
26
27 /* Clears and initialiazes the buffer to the state as if it was just
28    allocated by silc_buffer_alloc. */
29
30 extern inline
31 void silc_buffer_clear(SilcBuffer sb)
32 {
33   memset(sb->head, 0, sb->truelen);
34   sb->data = sb->head;
35   sb->tail = sb->head;
36   sb->len = 0;
37 }
38
39 /* Generates copy of a SilcBuffer. This copies everything inside the
40    currently valid data area, nothing more. Use silc_buffer_clone to
41    copy entire buffer. */
42
43 extern inline
44 SilcBuffer silc_buffer_copy(SilcBuffer sb)
45 {
46   SilcBuffer sb_new;
47
48   sb_new = silc_buffer_alloc(sb->len);
49   silc_buffer_pull_tail(sb_new, SILC_BUFFER_END(sb_new));
50   silc_buffer_put(sb_new, sb->data, sb->len);
51
52   return sb_new;
53 }
54
55 /* Clones SilcBuffer. This generates new SilcBuffer and copies
56    everything from the source buffer. The result is exact clone of
57    the original buffer. */
58
59 extern inline
60 SilcBuffer silc_buffer_clone(SilcBuffer sb)
61 {
62   SilcBuffer sb_new;
63
64   sb_new = silc_buffer_alloc(sb->truelen);
65   silc_buffer_pull_tail(sb_new, SILC_BUFFER_END(sb_new));
66   silc_buffer_put(sb_new, sb->head, sb->truelen);
67   sb_new->data = sb_new->head + (sb->data - sb->head);
68   sb_new->tail = sb_new->data + sb->len;
69   sb_new->len = sb->len;
70
71   return sb_new;
72 }
73
74 /* Reallocates buffer. Old data is saved into the new buffer. Returns
75    new SilcBuffer pointer. The buffer is exact clone of the old one
76    except that there is now more space at the end of buffer. */
77
78 extern inline
79 SilcBuffer silc_buffer_realloc(SilcBuffer sb, unsigned int newsize)
80 {
81   SilcBuffer sb_new;
82
83   sb_new = silc_buffer_alloc(newsize);
84   silc_buffer_pull_tail(sb_new, SILC_BUFFER_END(sb_new));
85   silc_buffer_put(sb_new, sb->head, sb->truelen);
86   sb_new->data = sb_new->head + (sb->data - sb->head);
87   sb_new->tail = sb_new->data + sb->len;
88   sb_new->len = sb->len;
89
90   silc_buffer_free(sb);
91
92   return sb_new;
93 }
94
95 #endif /* !SILC_DEBUG */
96
97 /* Prototypes */
98 #ifdef SILC_DEBUG
99 void silc_buffer_clear(SilcBuffer sb);
100 SilcBuffer silc_buffer_copy(SilcBuffer sb);
101 SilcBuffer silc_buffer_clone(SilcBuffer sb);
102 SilcBuffer silc_buffer_realloc(SilcBuffer sb, unsigned int newsize);
103 #endif
104
105 #endif