Merged silc_1_0_branch to trunk.
[silc.git] / lib / silcutil / silcstrutil.h
1 /*
2
3   silcstrutil.h
4
5   Author: Pekka Riikonen <priikone@silcnet.org>
6
7   Copyright (C) 2002 - 2005 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 String Utilities
21  *
22  * DESCRIPTION
23  *
24  * String manipulation utility routines.  These routines provides
25  * various helper functions for encoding, decoding and otherwise
26  * managing strings.
27  *
28  ***/
29
30 #ifndef SILCSTRUTIL_H
31 #define SILCSTRUTIL_H
32
33 /****d* silcutil/SilcStrUtilAPI/SilcStringEncoding
34  *
35  * NAME
36  *
37  *    typedef enum { ... } SilcStringEncoding;
38  *
39  * DESCRIPTION
40  *
41  *    String encoding definitions used with various string manipulation
42  *    routines.  By default, applications are suggested to use
43  *    SILC_STRING_LOCALE since it encodes and decodes correctly according
44  *    to local system language and character set (locale).
45  *
46  * SOURCE
47  */
48 typedef enum {
49   SILC_STRING_ASCII         = 0,  /* Any 8 bit ASCII encoding (default) */
50   SILC_STRING_ASCII_ESC     = 1,  /* 7 bit ASCII (>0x7f escaped) */
51   SILC_STRING_BMP           = 2,  /* 16 bit, UCS-2, BMP, ISO/IEC 10646 */
52   SILC_STRING_BMP_LSB       = 3,  /* BMP, least significant byte first */
53   SILC_STRING_UNIVERSAL     = 4,  /* 32 bit, UCS-4, Universal, ISO/IEC 10646 */
54   SILC_STRING_UNIVERSAL_LSB = 5,  /* Universal, least significant byte first */
55   SILC_STRING_LOCALE        = 6,  /* A locale specific conversion on
56                                      those platforms that support iconv().
57                                      Fallback is SILC_STRING_ASCII. */
58   SILC_STRING_UTF8          = 7,  /* UTF-8 encoding */
59   SILC_STRING_PRINTABLE     = 8,  /* Printable ASCII (no escaping) */
60   SILC_STRING_VISIBLE       = 9,  /* Visible ASCII string */
61   SILC_STRING_TELETEX       = 10, /* Teletex ASCII string */
62   SILC_STRING_NUMERICAL     = 11, /* Numerical ASCII string (digits) */
63   SILC_STRING_LDAP_DN       = 12, /* Strings for LDAP DNs, RFC 2253 */
64   SILC_STRING_UTF8_ESCAPE   = 12, /* Escaped UTF-8 as defined in RFC 2253 */
65
66   SILC_STRING_LANGUAGE      = 6,  /* _Deprecated_, use SILC_STRING_LOCALE. */
67 } SilcStringEncoding;
68 /***/
69
70 /****f* silcutil/SilcStrUtilAPI/silc_pem_encode
71  *
72  * SYNOPSIS
73  *
74  *    char *silc_pem_encode(unsigned char *data, SilcUInt32 len);
75  *
76  * DESCRIPTION
77  *
78  *    Encodes data into PEM encoding. Returns NULL terminated PEM encoded
79  *    data string. Note: This is originally public domain code and is
80  *    still PD.
81  *
82  ***/
83 char *silc_pem_encode(unsigned char *data, SilcUInt32 len);
84
85 /****f* silcutil/SilcStrUtilAPI/silc_pem_encode_file
86  *
87  * SYNOPSIS
88  *
89  *    char *silc_pem_encode_file(unsigned char *data, SilcUInt32 data_len);
90  *
91  * DESCRIPTION
92  *
93  *    Same as silc_pem_encode() but puts newline ('\n') every 72 characters.
94  *
95  ***/
96 char *silc_pem_encode_file(unsigned char *data, SilcUInt32 data_len);
97
98 /****f* silcutil/SilcStrUtilAPI/silc_pem_decode
99  *
100  * SYNOPSIS
101  *
102  *    unsigned char *silc_pem_decode(unsigned char *pem, SilcUInt32 pem_len,
103  *                                   SilcUInt32 *ret_len);
104  *
105  * DESCRIPTION
106  *
107  *    Decodes PEM into data. Returns the decoded data. Note: This is
108  *    originally public domain code and is still PD.
109  *
110  ***/
111 unsigned char *silc_pem_decode(unsigned char *pem, SilcUInt32 pem_len,
112                                SilcUInt32 *ret_len);
113
114 /****f* silcutil/SilcStrUtilAPI/silc_mime_parse
115  *
116  * SYNOPSIS
117  *
118  *    bool
119  *    silc_mime_parse(const unsigned char *mime, SilcUInt32 mime_len,
120  *                    char *version, SilcUInt32 version_size,
121  *                    char *content_type, SilcUInt32 content_type_size,
122  *                    char *transfer_encoding,
123  *                    SilcUInt32 transfer_encoding_size,
124  *                    unsigned char **mime_data_ptr,
125  *                    SilcUInt32 *mime_data_len);
126  *
127  * DESCRIPTION
128  *
129  *    Parses MIME header indicated by `mime' data block of length of
130  *    `mime_len'.  Returns TRUE if the `mime' is valid MIME object.
131  *    Parses from the MIME header the MIME Version (if present) and
132  *    copies it to the `version' pointer if provided, content type
133  *    indicating the data in the MIME object and copies it to the
134  *    `content_type' if provided, and the tranfer encoding (if present)
135  *    indicating the encoding of the data and copies it to the
136  *    `content_transfer_encoding' if provided.
137  *
138  *    The pointer to the actual data in the MIME object is saved into
139  *    `mime_data_ptr'.  The pointer is a location in the `mime' and it
140  *    does not allocate or copy anything, ie. the `mime_data_ptr' is a
141  *    pointer to the `mime'.  The `mime_data_len' indicates the length of
142  *    the data without the MIME header.  The caller is responsible of
143  *    NULL terminating the buffers it provides.
144  *
145  ***/
146 bool
147 silc_mime_parse(const unsigned char *mime, SilcUInt32 mime_len,
148                 char *version, SilcUInt32 version_size,
149                 char *content_type, SilcUInt32 content_type_size,
150                 char *transfer_encoding, SilcUInt32 transfer_encoding_size,
151                 unsigned char **mime_data_ptr, SilcUInt32 *mime_data_len);
152
153 /****f* silcutil/SilcStrUtilAPI/silc_strncat
154  *
155  * SYNOPSIS
156  *
157  *    char *silc_strncat(char *dest, SilcUInt32 dest_size,
158  *                       const char *src, SilcUInt32 src_len);
159  *
160  * DESCRIPTION
161  *
162  *    Concatenates the `src' into `dest'.  If `src_len' is more than the
163  *    size of the `dest' (minus NULL at the end) the `src' will be
164  *    truncated to fit.
165  *
166  ***/
167 char *silc_strncat(char *dest, SilcUInt32 dest_size,
168                    const char *src, SilcUInt32 src_len);
169
170 /****f* silcutil/SilcStrUtilAPI/silc_identifier_check
171  *
172  * SYNOPSIS
173  *
174  *    unsigned char *
175  *    silc_identifier_check(const unsigned char *identifier,
176  *                          SilcUInt32 identifier_len,
177  *                          SilcStringEncoding identifier_encoding,
178  *                          SilcUInt32 max_allowed_length,
179  *                          SilcUInt32 *out_len);
180  *
181  * DESCRIPTION
182  *
183  *    Checks that the 'identifier' string is valid identifier string
184  *    and does not contain any unassigned or prohibited character.  This
185  *    function is used to check for valid nicknames, server names, 
186  *    usernames, hostnames, service names, algorithm names, other security 
187  *    property names, and SILC Public Key name.
188  *
189  *    If the 'max_allowed_length' is non-zero the identifier cannot be
190  *    longer than that, and NULL is returned if it is.  If zero (0), no
191  *    length limit exist.  For nicknames the max length must be 128 bytes.
192  *    Other identifiers has no default limit, but application may choose 
193 *     one anyway.
194  *
195  *    Returns the validated string, that the caller must free.  Returns
196  *    NULL if the identifier string is not valid or contain unassigned or
197  *    prohibited characters.  Such identifier strings must not be used
198  *    SILC protocol.  The returned string is always in UTF-8 encoding.
199  *    The length of the returned string is in 'out_len'.
200  *
201  * NOTES
202  *
203  *    In addition of validating the identifier string, this function
204  *    may map characters to other characters or remove characters from the
205  *    original string.  This is done as defined in the SILC protocol.  Error
206  *    is returned only if the string contains unassigned or prohibited
207  *    characters.  The original 'identifier' is not modified at any point.
208  *
209  ***/
210 unsigned char *silc_identifier_check(const unsigned char *identifier,
211                                      SilcUInt32 identifier_len,
212                                      SilcStringEncoding identifier_encoding,
213                                      SilcUInt32 max_allowed_length,
214                                      SilcUInt32 *out_len);
215
216 /****f* silcutil/SilcStrUtilAPI/silc_identifier_verify
217  *
218  * SYNOPSIS
219  *
220  *    bool
221  *    silc_identifier_check(const unsigned char *identifier,
222  *                          SilcUInt32 identifier_len,
223  *                          SilcStringEncoding identifier_encoding,
224  *                          SilcUInt32 max_allowed_length);
225  *
226  * DESCRIPTION
227  *
228  *    Checks that the 'identifier' string is valid identifier string
229  *    and does not contain any unassigned or prohibited character.  This
230  *    function is used to check for valid nicknames, server names, 
231  *    usernames, hostnames, service names, algorithm names, other security 
232  *    property names, and SILC Public Key name.
233  *
234  *    If the 'max_allowed_length' is non-zero the identifier cannot be
235  *    longer than that, and NULL is returned if it is.  If zero (0), no
236  *    length limit exist.  For nicknames the max length must be 128 bytes.
237  *    Other identifiers has no default limit, but application may choose 
238  *    one anyway.
239  *
240  *    Returns TRUE if the string is valid and FALSE if it is prohibited.
241  *
242  ***/
243 bool silc_identifier_verify(const unsigned char *identifier,
244                             SilcUInt32 identifier_len,
245                             SilcStringEncoding identifier_encoding,
246                             SilcUInt32 max_allowed_length);
247
248 /****f* silcutil/SilcStrUtilAPI/silc_channel_name_check
249  *
250  * SYNOPSIS
251  *
252  *    unsigned char *
253  *    silc_channel_name_check(const unsigned char *identifier,
254  *                            SilcUInt32 identifier_len,
255  *                            SilcStringEncoding identifier_encoding,
256  *                            SilcUInt32 max_allowed_length,
257  *                            SilcUInt32 *out_len);
258  *
259  * DESCRIPTION
260  *
261  *    Checks that the 'identifier' string is valid channel name string
262  *    and does not contain any unassigned or prohibited character.
263  *
264  *    If the 'max_allowed_length' is non-zero the identifier cannot be
265  *    longer than that, and NULL is returned if it is.  If zero (0), no
266  *    length limit exist.  For channel names the max length must be 256
267  *    bytes.
268  *
269  *    Returns the validated string, that the caller must free.  Returns
270  *    NULL if the identifier string is not valid or contain unassigned or
271  *    prohibited characters.  Such identifier strings must not be used
272  *    SILC protocol.  The returned string is always in UTF-8 encoding.
273  *    The length of the returned string is in 'out_len'.
274  *
275  * NOTES
276  *
277  *    In addition of validating the channel name string, this function
278  *    may map characters to other characters or remove characters from the
279  *    original string.  This is done as defined in the SILC protocol.  Error
280  *    is returned only if the string contains unassigned or prohibited
281  *    characters.  The original 'identifier' is not modified at any point.
282  *
283  ***/
284 unsigned char *silc_channel_name_check(const unsigned char *identifier,
285                                        SilcUInt32 identifier_len,
286                                        SilcStringEncoding identifier_encoding,
287                                        SilcUInt32 max_allowed_length,
288                                        SilcUInt32 *out_len);
289
290 /****f* silcutil/SilcStrUtilAPI/silc_channel_name_verify
291  *
292  * SYNOPSIS
293  *
294  *    bool
295  *    silc_channel_name_check(const unsigned char *identifier,
296  *                            SilcUInt32 identifier_len,
297  *                            SilcStringEncoding identifier_encoding,
298  *                            SilcUInt32 max_allowed_length);
299  *
300  * DESCRIPTION
301  *
302  *    Checks that the 'identifier' string is valid channel name string
303  *    and does not contain any unassigned or prohibited character.
304  *
305  *    If the 'max_allowed_length' is non-zero the identifier cannot be
306  *    longer than that, and NULL is returned if it is.  If zero (0), no
307  *    length limit exist.  For channel names the max length must be 256
308  *    bytes.
309  *
310  *    Returns TRUE if the string is valid and FALSE if it is prohibited.
311  *
312  ***/
313 bool silc_channel_name_verify(const unsigned char *identifier,
314                               SilcUInt32 identifier_len,
315                               SilcStringEncoding identifier_encoding,
316                               SilcUInt32 max_allowed_length);
317
318 #endif /* SILCSTRUTIL_H */