1 #ifndef NU_UTF16_H
2 #define NU_UTF16_H
3 
4 #include <stdint.h>
5 
6 #include "config.h"
7 #include "defines.h"
8 #include "strings.h"
9 #include "validate.h"
10 
11 /** @defgroup utf16 UTF-16 support
12  *
13  * @example utf16.c
14  */
15 
16 #if defined (__cplusplus) || defined (c_plusplus)
17 extern "C" {
18 #endif
19 
20 #if (defined NU_WITH_UTF16_READER) || (defined NU_WITH_UTF16_WRITER)
21 /** For sizeof() only
22  *
23  * @ingroup utf16
24  */
25 static const uint16_t NU_UTF16_BOM = 0;
26 #endif
27 
28 /** Endianess-specific function to write BOM
29  *
30  * @ingroup utf16
31  * @see nu_utf16le_write_bom
32  */
33 typedef char* (*nu_utf16_write_bom_t)(char *);
34 
35 #ifdef NU_WITH_UTF16_READER
36 
37 /** Holder for endianess-specific UTF-16 functions
38  *
39  * @ingroup utf16
40  */
41 typedef struct {
42 	/** Read (decode) function
43 	 */
44 	nu_read_iterator_t read;
45 	/** Write (encode) function
46 	 */
47 	nu_write_iterator_t write;
48 	/** Reverse-read (decode) function
49 	 */
50 	nu_revread_iterator_t revread;
51 	/** Validation function
52 	 */
53 	nu_validread_iterator_t validread;
54 	/** BOM writing function
55 	 */
56 	nu_utf16_write_bom_t write_bom;
57 } nu_utf16_bom_t;
58 
59 /** Read BOM from encoded string
60  *
61  * Note that if BOM is not specified in string, it defaults to big-endian
62  *
63  * @ingroup utf16
64  * @param encoded pointer to encoded strings
65  * @param bom optional, this struct will be filled with read, write, etc
66  * function for detected BOM. Note revread, validread and write might be 0
67  * if not enabled in build options
68  * @return pointer to next codepoint in UTF-16 string or encoded if BOM is
69  * not found
70  */
71 NU_EXPORT
72 const char* nu_utf16_read_bom(const char *encoded, nu_utf16_bom_t *bom);
73 
74 #endif /* NU_WITH_UTF16_READER */
75 
76 #ifdef NU_WITH_UTF16_WRITER
77 
78 /** Write little-endian BOM to a string
79  *
80  * @ingroup utf16
81  * @param encoded pointer to encoded string or 0
82  * @return pointer to byte after written BOM
83  */
84 NU_EXPORT
85 char* nu_utf16le_write_bom(char *encoded);
86 
87 /** Write big-endian BOM to a string
88  *
89  * @ingroup utf16
90  * @param encoded pointer to encoded string or 0
91  * @return pointer to byte after written BOM
92  */
93 NU_EXPORT
94 char* nu_utf16be_write_bom(char *encoded);
95 
96 #endif /* NU_WITH_UTF16_WRITER */
97 
98 #if defined (__cplusplus) || defined (c_plusplus)
99 }
100 #endif
101 
102 #endif /* NU_UTF16_H */
103