1 /*
2  * This file is part of the TINICONV Library.
3  *
4  * The TINICONV Library is free software; you can redistribute it
5  * and/or modify it under the terms of the GNU Library General Public
6  * License version 2 as published by the Free Software Foundation.
7  */
8 // ----------------------------------------------------------------------------
9 // Copyright (C) 2014
10 //              David Freese, W1HKJ
11 //
12 // This file is part of fldigi
13 //
14 // fldigi 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 // You should have received a copy of the GNU General Public License
20 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
21 // ----------------------------------------------------------------------------
22 
23 #ifndef TINICONV_H_
24 #define TINICONV_H_
25 
26 /* For exporting functions under WIN32 */
27 #ifdef WIN32
28 #ifdef DLL
29 #define EXPORT(type) __declspec(dllexport) type
30 #else
31 #define EXPORT(type) /*__declspec(dllimport)*/ type
32 #endif/*DLL*/
33 #else
34 #define EXPORT(type) type
35 #endif /*WIN32*/
36 
37 typedef unsigned int ucs4_t;
38 typedef struct tiniconv_ctx_s * conv_t;
39 
40 /*
41  * int xxx_mb2wc (conv_t conv, ucs4_t *pwc, unsigned char const *s, int n)
42  * converts the byte sequence starting at s to a wide character. Up to n bytes
43  * are available at s. n is >= 1.
44  * Result is number of bytes consumed (if a wide character was read),
45  * or -1 if invalid, or -2 if n too small, or -2-(number of bytes consumed)
46  * if only a shift sequence was read.
47  */
48 typedef int (*xxx_mb2wc_t) (conv_t conv, ucs4_t *pwc, unsigned char const *s, int n);
49 
50 /*
51  * int xxx_flushwc (conv_t conv, ucs4_t *pwc)
52  * returns to the initial state and stores the pending wide character, if any.
53  * Result is 1 (if a wide character was read) or 0 if none was pending.
54  */
55 typedef int (*xxx_flushwc_t) (conv_t conv, ucs4_t *pwc);
56 
57 /*
58  * int xxx_wc2mb (conv_t conv, unsigned char *r, ucs4_t wc, int n)
59  * converts the wide character wc to the character set xxx, and stores the
60  * result beginning at r. Up to n bytes may be written at r. n is >= 1.
61  * Result is number of bytes written, or -1 if invalid, or -2 if n too small.
62  */
63 typedef int (*xxx_wc2mb_t) (conv_t conv, unsigned char *r, ucs4_t wc, int n);
64 
65 /*
66  * int xxx_reset (conv_t conv, unsigned char *r, int n)
67  * stores a shift sequences returning to the initial state beginning at r.
68  * Up to n bytes may be written at r. n is >= 0.
69  * Result is number of bytes written, or -2 if n too small.
70  */
71 typedef int (*xxx_reset_t) (conv_t conv, unsigned char *r, int n);
72 
73 typedef unsigned int conv_state_t;
74 
75 struct tiniconv_ctx_s {
76   conv_state_t istate;
77   conv_state_t ostate;
78   xxx_mb2wc_t mb2wc;
79   xxx_flushwc_t flushwc;
80   xxx_wc2mb_t wc2mb;
81   xxx_reset_t reset;
82   int options;
83 };
84 
85 /*
86  * tiniconv_init
87  */
88 
89 #define TINICONV_CHARSET_ASCII       0
90 #define TINICONV_CHARSET_CP1250      1
91 #define TINICONV_CHARSET_CP1251      2
92 #define TINICONV_CHARSET_CP1252      3
93 #define TINICONV_CHARSET_CP1253      4
94 #define TINICONV_CHARSET_CP1254      5
95 #define TINICONV_CHARSET_CP1255      6
96 #define TINICONV_CHARSET_CP1256      7
97 #define TINICONV_CHARSET_CP1257      8
98 #define TINICONV_CHARSET_CP1258      9
99 #define TINICONV_CHARSET_CP936       10
100 #define TINICONV_CHARSET_GB2312      11
101 #define TINICONV_CHARSET_GBK         12
102 #define TINICONV_CHARSET_ISO_2022_JP 13
103 #define TINICONV_CHARSET_ISO_8859_1  14
104 #define TINICONV_CHARSET_ISO_8859_2  15
105 #define TINICONV_CHARSET_ISO_8859_3  16
106 #define TINICONV_CHARSET_ISO_8859_4  17
107 #define TINICONV_CHARSET_ISO_8859_5  18
108 #define TINICONV_CHARSET_ISO_8859_6  19
109 #define TINICONV_CHARSET_ISO_8859_7  20
110 #define TINICONV_CHARSET_ISO_8859_8  21
111 #define TINICONV_CHARSET_ISO_8859_9  22
112 #define TINICONV_CHARSET_ISO_8859_10 23
113 #define TINICONV_CHARSET_ISO_8859_11 24
114 #define TINICONV_CHARSET_ISO_8859_13 25
115 #define TINICONV_CHARSET_ISO_8859_14 26
116 #define TINICONV_CHARSET_ISO_8859_15 27
117 #define TINICONV_CHARSET_ISO_8859_16 28
118 #define TINICONV_CHARSET_CP866       29
119 #define TINICONV_CHARSET_KOI8_R      30
120 #define TINICONV_CHARSET_KOI8_RU     31
121 #define TINICONV_CHARSET_KOI8_U      32
122 #define TINICONV_CHARSET_MACCYRILLIC 33
123 #define TINICONV_CHARSET_UCS_2       34
124 #define TINICONV_CHARSET_UTF_7       35
125 #define TINICONV_CHARSET_UTF_8       36
126 #define TINICONV_CHARSET_CHINESE     37
127 #define TINICONV_CHARSET_BIG5        38
128 #define TINICONV_CHARSETSIZE         39
129 
130 #define TINICONV_OPTION_IGNORE_IN_ILSEQ 1 /*< ignore incorrect input sequences */
131 #define TINICONV_OPTION_IGNORE_OUT_ILSEQ 2 /*< replace sequence which can't be converted to OUT charset with OUTIL_CHAR */
132 /* #define TINICONV_OPTION_TRANSLIT 4 */
133 #define TINICONV_OPTION_OUT_ILSEQ_CHAR(ch) (ch << 8)
134 
135 #define TINICONV_INIT_OK 0
136 #define TINICONV_INIT_IN_CHARSET_NA -1
137 #define TINICONV_INIT_OUT_CHARSET_NA -1
138 
139 
140 #ifdef __cplusplus
141 extern "C"
142 {
143 #endif
144 EXPORT(int) tiniconv_init(int in_charset_id, int out_charset_id, int options, struct tiniconv_ctx_s *ctx);
145 
146 
147 /*
148  * tiniconv_convert
149  */
150 #define TINICONV_CONVERT_OK 0
151 #define TINICONV_CONVERT_IN_TOO_SMALL -1
152 #define TINICONV_CONVERT_OUT_TOO_SMALL -2
153 #define TINICONV_CONVERT_IN_ILSEQ -3
154 #define TINICONV_CONVERT_OUT_ILSEQ -4
155 
156 EXPORT(int) tiniconv_convert(struct tiniconv_ctx_s *ctx,
157   unsigned char const *in_buf, int in_size, int *p_in_size_consumed,
158   unsigned char *out_buf, int out_size, int *p_out_size_consumed);
159 #ifdef __cplusplus
160 }
161 #endif
162 
163 #endif /*TINICONV_H_*/
164