e29be0724152d616371c53980e236a9488b3c3ac
[silc.git] / lib / silcutil / silcstrutil.h
1 /*
2
3   silcstrutil.h 
4
5   Author: Pekka Riikonen <priikone@silcnet.org>
6
7   Copyright (C) 2002 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 /****f* silcutil/SilcStrUtilAPI/silc_pem_encode
34  *
35  * SYNOPSIS
36  *
37  *    char *silc_pem_encode(unsigned char *data, SilcUInt32 len);
38  *
39  * DESCRIPTION
40  *
41  *    Encodes data into PEM encoding. Returns NULL terminated PEM encoded
42  *    data string. Note: This is originally public domain code and is
43  *    still PD.
44  *
45  ***/
46 char *silc_pem_encode(unsigned char *data, SilcUInt32 len);
47
48 /****f* silcutil/SilcStrUtilAPI/silc_pem_encode_file
49  *
50  * SYNOPSIS
51  *
52  *    char *silc_pem_encode_file(unsigned char *data, SilcUInt32 data_len);
53  *
54  * DESCRIPTION
55  *
56  *    Same as silc_pem_encode() but puts newline ('\n') every 72 characters.
57  *
58  ***/
59 char *silc_pem_encode_file(unsigned char *data, SilcUInt32 data_len);
60
61 /****f* silcutil/SilcStrUtilAPI/silc_pem_decode
62  *
63  * SYNOPSIS
64  *
65  *    unsigned char *silc_pem_decode(unsigned char *pem, SilcUInt32 pem_len,
66  *                                   SilcUInt32 *ret_len);
67  *
68  * DESCRIPTION
69  *
70  *    Decodes PEM into data. Returns the decoded data. Note: This is
71  *    originally public domain code and is still PD.
72  *
73  ***/
74 unsigned char *silc_pem_decode(unsigned char *pem, SilcUInt32 pem_len,
75                                SilcUInt32 *ret_len);
76
77 /****d* silcutil/SilcStrUtilAPI/SilcStringEncoding
78  *
79  * NAME
80  * 
81  *    typedef enum { ... } SilcStringEncoding;
82  *
83  * DESCRIPTION
84  *
85  *    String encoding definitions used with the UTF-8 encoding and
86  *    decoding functions.
87  *
88  * SOURCE
89  */
90 typedef enum {
91   SILC_STRING_ASCII         = 0, /* Any 8 bit ASCII encoding (default) */
92   SILC_STRING_ASCII_ESC     = 1, /* 7 bit ASCII (>0x7f escaped) */
93   SILC_STRING_BMP           = 2, /* 16 bit, UCS-2, BMP, ISO/IEC 10646 */
94   SILC_STRING_BMP_LSB       = 3, /* BMP, least significant byte first */
95   SILC_STRING_UNIVERSAL     = 4, /* 32 bit, UCS-4, Universal, ISO/IEC 10646 */
96   SILC_STRING_UNIVERSAL_LSB = 5, /* Universal, least significant byte first */
97   SILC_STRING_LANGUAGE      = 6, /* Language and charset specific conversion
98                                     on those platforms that support iconv().
99                                     Fallback is SILC_STRING_ASCII. */
100 } SilcStringEncoding;
101 /***/
102
103 /****f* silcutil/SilcStrUtilAPI/silc_utf8_encode
104  *
105  * SYNOPSIS
106  *
107  *    SilcUInt32 silc_utf8_encode(const unsigned char *bin, SilcUInt32 bin_len,
108  *                                SilcStringEncoding bin_encoding,
109  *                                unsigned char *utf8, SilcUInt32 utf8_size);
110  *
111  * DESCRIPTION
112  *
113  *    Encodes the string `bin' of which encoding is `bin_encoding' to the
114  *    UTF-8 encoding into the buffer `utf8' which is of size of `utf8_size'.
115  *    Returns the length of the UTF-8 encoded string, or zero (0) on error.
116  *    By default `bin_encoding' is ASCII, and the caller needs to know the
117  *    encoding of the input string if it is anything else.
118  *
119  ***/
120 SilcUInt32 silc_utf8_encode(const unsigned char *bin, SilcUInt32 bin_len,
121                             SilcStringEncoding bin_encoding,
122                             unsigned char *utf8, SilcUInt32 utf8_size);
123
124 /****f* silcutil/SilcStrUtilAPI/silc_utf8_decode
125  *
126  * SYNOPSIS
127  *
128  *    SilcUInt32 silc_utf8_decode(const unsigned char *utf8, 
129  *                                SilcUInt32 utf8_len,
130  *                                SilcStringEncoding bin_encoding,
131  *                                unsigned char *bin, SilcUInt32 bin_size);
132  *
133  * DESCRIPTION
134  *
135  *    Decodes UTF-8 encoded string `utf8' to string of which encoding is
136  *    to be `bin_encoding', into the `bin' buffer of size of `bin_size'.
137  *    Returns the length of the decoded buffer, or zero (0) on error.
138  *    By default `bin_encoding' is ASCII, and the caller needs to know to
139  *    which encoding the output string is to be encoded if ASCII is not
140  *    desired. 
141  *
142  ***/
143 SilcUInt32 silc_utf8_decode(const unsigned char *utf8, SilcUInt32 utf8_len,
144                             SilcStringEncoding bin_encoding,
145                             unsigned char *bin, SilcUInt32 bin_size);
146
147 /****f* silcutil/SilcStrUtilAPI/silc_utf8_encoded_len
148  *
149  * SYNOPSIS
150  *
151  *    SilcUInt32 silc_utf8_encoded_len(const unsigned char *bin, 
152  *                                     SilcUInt32 bin_len,
153  *                                     SilcStringEncoding bin_encoding);
154  *
155  * DESCRIPTION
156  *
157  *    Returns the length of UTF-8 encoded string if the `bin' of
158  *    encoding of `bin_encoding' is encoded with silc_utf8_encode.
159  *
160  ***/
161 SilcUInt32 silc_utf8_encoded_len(const unsigned char *bin, SilcUInt32 bin_len,
162                                  SilcStringEncoding bin_encoding);
163
164 /****f* silcutil/SilcStrUtilAPI/silc_utf8_valid
165  *
166  * SYNOPSIS
167  *
168  *    bool silc_utf8_valid(const unsigned char *utf8, SilcUInt32 utf8_len);
169  *
170  * DESCRIPTION
171  *
172  *    Returns TRUE if the `utf8' string of length of `utf8_len' is valid
173  *    UTF-8 encoded string, FALSE if it is not UTF-8 encoded string.
174  *
175  ***/
176 bool silc_utf8_valid(const unsigned char *utf8, SilcUInt32 utf8_len);
177
178 /****f* silcutil/SilcStrUtilAPI/silc_mime_parse
179  *
180  * SYNOPSIS
181  *
182  *    bool
183  *    silc_mime_parse(const unsigned char *mime, SilcUInt32 mime_len,
184  *                    char *version, SilcUInt32 version_size,
185  *                    char *content_type, SilcUInt32 content_type_size,
186  *                    char *transfer_encoding,
187  *                    SilcUInt32 transfer_encoding_size,
188  *                    unsigned char **mime_data_ptr,
189  *                    SilcUInt32 *mime_data_len);
190  *
191  * DESCRIPTION
192  *
193  *    Parses MIME header indicated by `mime' data block of length of
194  *    `mime_len'.  Returns TRUE if the `mime' is valid MIME object.
195  *    Parses from the MIME header the MIME Version (if present) and
196  *    copies it to the `version' pointer if provided, content type
197  *    indicating the data in the MIME object and copies it to the
198  *    `content_type' if provided, and the tranfer encoding (if present)
199  *    indicating the encoding of the data and copies it to the
200  *    `content_transfer_encoding' if provided.
201  *
202  *    The pointer to the actual data in the MIME object is saved into 
203  *    `mime_data_ptr'.  The pointer is a location in the `mime' and it 
204  *    does not allocate or copy anything, ie. the `mime_data_ptr' is a 
205  *    pointer to the `mime'.  The `mime_data_len' indicates the length of 
206  *    the data without the MIME header.  The caller is responsible of
207  *    NULL terminating the buffers it provides.
208  *
209  ***/
210 bool
211 silc_mime_parse(const unsigned char *mime, SilcUInt32 mime_len,
212                 char *version, SilcUInt32 version_size,
213                 char *content_type, SilcUInt32 content_type_size,
214                 char *transfer_encoding, SilcUInt32 transfer_encoding_size,
215                 unsigned char **mime_data_ptr, SilcUInt32 *mime_data_len);
216
217 /****f* silcutil/SilcStrUtilAPI/silc_strncat
218  *
219  * SYNOPSIS
220  *
221  *    char *silc_strncat(char *dest, SilcUInt32 dest_size,
222  *                       const char *src, SilcUInt32 src_len);
223  *
224  * DESCRIPTION
225  *
226  *    Concatenates the `src' into `dest'.  If `src_len' is more than the
227  *    size of the `dest' (minus NULL at the end) the `src' will be
228  *    truncated to fit.
229  *
230  ***/
231 char *silc_strncat(char *dest, SilcUInt32 dest_size,
232                    const char *src, SilcUInt32 src_len);
233
234 #endif /* SILCSTRUTIL_H */