Added SILC Thread Queue API
[silc.git] / lib / silcutil / silcutil.h
1 /*
2
3   silcutil.h
4
5   Author: Pekka Riikonen <priikone@silcnet.org>
6
7   Copyright (C) 1997 - 2008 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; version 2 of the License.
12
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.
17
18 */
19
20 /****h* silcutil/SILC Utilities
21  *
22  * DESCRIPTION
23  *
24  *    Utility functions.
25  *
26  ***/
27
28 #ifndef SILCUTIL_H
29 #define SILCUTIL_H
30
31 /****f* silcutil/SilcUtilAPI/silc_gets
32  *
33  * SYNOPSIS
34  *
35  *    int silc_gets(char *dest, int destlen, const char *src, int srclen,
36  *                  int begin);
37  *
38  * DESCRIPTION
39  *
40  *    Gets line from a buffer. Stops reading when a newline or EOF occurs.
41  *    This doesn't remove the newline sign from the destination buffer. The
42  *    argument begin is returned and should be passed again for the function.
43  *
44  ***/
45 int silc_gets(char *dest, int destlen, const char *src, int srclen, int begin);
46
47 /****f* silcutil/SilcUtilAPI/silc_to_upper
48  *
49  * SYNOPSIS
50  *
51  *    SilcBool silc_to_upper(const char *string, char *dest,
52  *                           SilcUInt32 dest_size);
53  *
54  * DESCRIPTION
55  *
56  *    Converts string to capital characters.
57  *
58  ***/
59 SilcBool silc_to_upper(const char *string, char *dest, SilcUInt32 dest_size);
60
61 /****f* silcutil/SilcUtilAPI/silc_to_lower
62  *
63  * SYNOPSIS
64  *
65  *    SilcBool silc_to_lower(const char *string, char *dest,
66  *                           SilcUInt32 dest_size);
67  *
68  * DESCRIPTION
69  *
70  *    Converts string to capital characters.
71  *
72  ***/
73 SilcBool silc_to_lower(const char *string, char *dest, SilcUInt32 dest_size);
74
75 /****f* silcutil/SilcUtilAPI/silc_parse_userfqdn
76  *
77  * SYNOPSIS
78  *
79  *    int silc_parse_userfqdn(const char *string,
80  *                            char *user, SilcUInt32 user_size,
81  *                            char *fqdn, SilcUInt32 fqdn_size);
82  *
83  * DESCRIPTION
84  *
85  *    Parse userfqdn string which is in user@fqdn format.  Returns 0 on
86  *    error, 1 if `user' was filled and 2 if both `user' and `fqdn'
87  *    was filled.
88  *
89  ***/
90 int silc_parse_userfqdn(const char *string,
91                         char *user, SilcUInt32 user_size,
92                         char *fqdn, SilcUInt32 fqdn_size);
93
94 /****f* silcutil/SilcUtilAPI/silc_parse_command_line
95  *
96  * SYNOPSIS
97  *
98  *    void silc_parse_command_line(unsigned char *buffer,
99  *                                 unsigned char ***parsed,
100  *                                 SilcUInt32 **parsed_lens,
101  *                                 SilcUInt32 **parsed_types,
102  *                                 SilcUInt32 *parsed_num,
103  *                                 SilcUInt32 max_args);
104  *
105  * DESCRIPTION
106  *
107  *    Parses command line. At most `max_args' is taken. Rest of the line
108  *    will be allocated as the last argument if there are more than `max_args'
109  *    arguments in the line. Note that the command name is counted as one
110  *    argument and is saved.
111  *
112  ***/
113 void silc_parse_command_line(unsigned char *buffer,
114                              unsigned char ***parsed,
115                              SilcUInt32 **parsed_lens,
116                              SilcUInt32 **parsed_types,
117                              SilcUInt32 *parsed_num,
118                              SilcUInt32 max_args);
119
120 /****f* silcutil/SilcUtilAPI/silc_format
121  *
122  * SYNOPSIS
123  *
124  *    char *silc_format(char *fmt, ...);
125  *
126  * DESCRIPTION
127  *
128  *    Formats arguments to a string and returns it after allocating memory
129  *    for it. It must be remembered to free it later.
130  *
131  ***/
132 char *silc_format(char *fmt, ...);
133
134 /****f* silcutil/SilcUtilAPI/silc_fingerprint
135  *
136  * SYNOPSIS
137  *
138  *    char *silc_fingerprint(const unsigned char *data, SilcUInt32 data_len);
139  *
140  * DESCRIPTION
141  *
142  *    Return a textual representation of the fingerprint in *data, the
143  *    caller must free the returned string.  Returns NULL on error.  If
144  *    the `data_len' is longer than 255 bytes, only the first 255 bytes are
145  *    used to create the fingerprint.
146  *
147  ***/
148 char *silc_fingerprint(const unsigned char *data, SilcUInt32 data_len);
149
150 /****f* silcutil/SilcUtilAPI/silc_string_is_ascii
151  *
152  * SYNOPSIS
153  *
154  *    SilcBool silc_string_is_ascii(const unsigned char *data,
155  *                              SilcUInt32 data_len);
156  *
157  * DESCRIPTION
158  *
159  *    Return TRUE if the `data' is ASCII string.
160  *
161  ***/
162 SilcBool silc_string_is_ascii(const unsigned char *data, SilcUInt32 data_len);
163
164 /****f* silcutil/SilcUtilAPI/silc_get_input
165  *
166  * SYNOPSIS
167  *
168  *    char *silc_get_input(const char *prompt, SilcBool echo_off);
169  *
170  * DESCRIPTION
171  *
172  *    Displays input prompt on command line and takes input data from user.
173  *
174  ***/
175 char *silc_get_input(const char *prompt, SilcBool echo_off);
176
177 /* System dependant prototypes */
178
179 /****f* silcutil/SilcUtilAPI/silc_get_username
180  *
181  * SYNOPSIS
182  *
183  *    char *silc_get_username();
184  *
185  * DESCRIPTION
186  *
187  *    Returns the username of the user. If the global variable LOGNAME
188  *    does not exists we will get the name from the passwd file.  The
189  *    caller must free the returned name.
190  *
191  *    This function is system dependant.
192  *
193  ***/
194 char *silc_get_username();
195
196 /****f* silcutil/SilcUtilAPI/silc_get_real_name
197  *
198  * SYNOPSIS
199  *
200  *    char *silc_get_real_name();
201  *
202  * DESCRIPTION
203  *
204  *    Returns the real name of ther user from the passwd file.  The
205  *    caller must free the returned name.
206  *
207  *    This function is system dependant.
208  *
209  ***/
210 char *silc_get_real_name();
211
212 /****f* silcutil/SilcUtilAPI/silc_va_copy
213  *
214  * SYNOPSIS
215  *
216  *    void silc_va_copy(va_list dest, va_list src);
217  *
218  * DESCRIPTION
219  *
220  *    Copies variable argument list.  This must be called in case the
221  *    variable argument list must be evaluated multiple times.  For each
222  *    evaluation the list must be copied and va_end must be called for
223  *    each copied list.
224  *
225  ***/
226 #if defined(HAVE_VA_COPY)
227 #define silc_va_copy(dest, src) va_copy(dest, src);
228 #elif defined(HAVE___VA_COPY)
229 #define silc_va_copy(dest, src) __va_copy(dest, src);
230 #elif defined(SILC_VA_COPY_ARRAY)
231 #define silc_va_copy(dest, src) memmove(dest, src, sizeof(va_list));
232 #else
233 #define silc_va_copy(dest, src) dest = src;
234 #endif
235
236 /****f* silcutil/SilcUtilAPI/silc_hexdump
237  *
238  * SYNOPSIS
239  *
240  *    void silc_hexdump(const unsigned char *data, SilcUInt32 data_len,
241  *                      FILE *output);
242  *
243  * DESCRIPTION
244  *
245  *    Dumps the `data' of length of `data_len' bytes as HEX.  The `output'
246  *    file specifies the destination.
247  *
248  ***/
249 void silc_hexdump(const unsigned char *data, SilcUInt32 data_len,
250                   FILE *output);
251
252 /****f* silcutil/SilcUtilAPI/silc_hex2data
253  *
254  * SYNOPSIS
255  *
256  *    SilcBool silc_hex2data(const char *hex, unsigned char *data,
257  *                           SilcUInt32 data_size, SilcUInt32 *ret_data_len);
258  *
259  * DESCRIPTION
260  *
261  *    Converts HEX character string to binary data.  Each HEX numbers must
262  *    have two characters in the `hex' string.
263  *
264  ***/
265 SilcBool silc_hex2data(const char *hex, unsigned char *data,
266                        SilcUInt32 data_size, SilcUInt32 *ret_data_len);
267
268 /****f* silcutil/SilcUtilAPI/silc_data2hex
269  *
270  * SYNOPSIS
271  *
272  *    SilcBool silc_data2hex(const unsigned char *data, SilcUInt32 data_len,
273  *                           char *hex, SilcUInt32 hex_size);
274  *
275  * DESCRIPTION
276  *
277  *    Converts binary data to HEX string.  This NULL terminates the `hex'
278  *    buffer automatically.
279  *
280  ***/
281 SilcBool silc_data2hex(const unsigned char *data, SilcUInt32 data_len,
282                        char *hex, SilcUInt32 hex_size);
283
284 #endif  /* !SILCUTIL_H */