forked from t4rd15/asterisk-chan-quectel
-
Notifications
You must be signed in to change notification settings - Fork 34
/
char_conv.c
228 lines (200 loc) · 5.89 KB
/
char_conv.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/*
Copyright (C) 2009 - 2010
Artem Makhutov <[email protected]>
http://www.makhutov.org
Dmitry Vagin <[email protected]>
Copyright (C) 2010 - 2011
bg <[email protected]>
Copyright (C) 2020 Max von Buelow <[email protected]>
*/
#include "ast_config.h"
#include <sys/types.h>
#include <iconv.h> /* iconv_t iconv() */
#include <string.h> /* memcpy() */
#include <stdio.h> /* sscanf() snprintf() */
#include <errno.h> /* EINVAL */
#include "char_conv.h"
#include "mutils.h" /* ITEMS_OF() */
#include "gsm7_luts.h"
static char lut_hex2val[] = {
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1,
-1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
};
static const char *lut_val2hex = "0123456789ABCDEF";
static ssize_t convert_string(const char *in, size_t in_length, char *out, size_t out_size, char *from, char *to)
{
ICONV_CONST char *in_ptr = (ICONV_CONST char*)in;
size_t in_bytesleft = in_length;
char *out_ptr = out;
size_t out_bytesleft = out_size - 1;
ICONV_T cd = (ICONV_T)-1;
ssize_t res;
cd = iconv_open(to, from);
if (cd == (ICONV_T)-1) {
return -1;
}
res = iconv(cd, &in_ptr, &in_bytesleft, &out_ptr, &out_bytesleft);
if (res < 0) {
iconv_close(cd);
return -1;
}
iconv_close(cd);
return out_ptr - out;
}
EXPORT_DEF ssize_t utf8_to_ucs2(const char *in, size_t in_length, uint16_t *out, size_t out_size)
{
ssize_t res = convert_string(in, in_length, (char*)out, out_size * 2, "UTF-8", "UTF-16BE");
if (res < 0) return res;
return res / 2;
}
EXPORT_DEF ssize_t ucs2_to_utf8(const uint16_t *in, size_t in_length, char *out, size_t out_size)
{
return convert_string((const char*)in, in_length * 2, out, out_size, "UTF-16BE", "UTF-8");
}
static char hexchar2val(unsigned char h)
{
return lut_hex2val[h];
}
static char val2hexchar(unsigned char h)
{
return lut_val2hex[h];
}
EXPORT_DEF int unhex(const char *in, uint8_t *out)
{
int len = 0, nibbles = 0;
while (in[0]) {
nibbles += 1 + !!in[1];
char p0 = hexchar2val(*in++);
char p1 = *in ? hexchar2val(*in++) : 0;
if (p0 == -1 || p1 == -1) {
return -1;
}
out[len++] = p0 << 4 | p1;
}
return nibbles;
}
EXPORT_DEF void hexify(const uint8_t *in, size_t in_length, char *out)
{
// code from end of string to allow in-place encoding
for (int i = in_length - 1; i >= 0; --i) {
char c0 = val2hexchar(in[i] >> 4), c1 = val2hexchar(in[i] & 15);
out[i * 2] = c0;
out[i * 2 + 1] = c1;
}
out[in_length * 2] = '\0';
}
#/* */
static const uint8_t *get_char_gsm7_encoding(uint16_t c)
{
int minor = c >> 8, major = c & 255;
int subtab = LUT_GSM7_REV1[major];
if (subtab == -1) return LUT_GSM7_REV2_INV;
return LUT_GSM7_REV2[subtab][minor];
}
EXPORT_DEF ssize_t gsm7_encode(const uint16_t *in, size_t in_length, uint16_t *out)
{
// TODO: Should we check for other tables or just use UCS-2?
unsigned bytes = 0;
const uint8_t *escenc = get_char_gsm7_encoding(0x1B00);
for (unsigned i = 0; i < in_length; ++i) {
const uint8_t *enc = get_char_gsm7_encoding(in[i]);
uint8_t c = enc[0];
if (c == GSM7_INVALID) {
return -1;
}
if (c > 127) {
bytes += 2;
out[i] = (escenc[0] << 8) | (c - 128);
} else {
++bytes;
out[i] = c;
}
}
return bytes;
}
EXPORT_DEF ssize_t gsm7_pack(const uint16_t *in, size_t in_length, char *out, size_t out_size, unsigned out_padding)
{
size_t i, x;
unsigned value = 0;
/* compute number of bytes we need for the final string, rounded up */
x = ((out_padding + (7 * in_length) + 7) / 8) + 1;
/* check that the buffer is not too small */
if (x > out_size)
return -1;
for (x = i = 0; i != in_length; i++) {
char c[] = { in[i] >> 8, in[i] & 255 };
for (int j = c[0] == 0; j < 2; ++j) {
value |= (c[j] & 0x7F) << out_padding;
out_padding += 7;
if (out_padding < 8)
continue;
/* output one byte */
out[x++] = value & 0xff;
value >>= 8;
out_padding -= 8;
}
}
if (out_padding != 0) {
out[x++] = value & 0xff;
}
/* return total string length in nibbles, excluding terminating zero */
return x * 2 - (out_padding == 1 || out_padding == 2 || out_padding == 3 ? 1 : 0);
}
EXPORT_DEF ssize_t gsm7_unpack_decode(const char *in, size_t in_nibbles, uint16_t *out, size_t out_size, unsigned in_padding, uint8_t ls, uint8_t ss)
{
if (ls > 13) ls = 0;
if (ss > 13) ss = 0;
size_t i;
size_t x;
unsigned value = 0;
unsigned c;
if (out_size == 0) {
return -1;
}
/* check if string is empty */
if (in_nibbles < 2) {
out[0] = '\0';
return 0;
}
/* account for the bit padding */
in_padding = 7 - in_padding;
/* parse the hexstring */
int esc = 0;
for (x = i = 0; i < in_nibbles; ++i) {
if (x >= out_size)
return -1;
c = in[i / 2];
if (i & 1) c >>= 4;
uint8_t n = c & 0xf;
value |= n << in_padding;
in_padding += 4;
while (in_padding >= 7 * 2) {
in_padding -= 7;
value >>= 7;
{
uint16_t val = (esc ? LUT_GSM7_SS16 : LUT_GSM7_LS16)[esc ? ss : ls][value & 0x7f];
if (val == 0x1b) {
esc = 1;
} else {
esc = 0;
out[x++] = ((val & 0xff) << 8) | (val >> 8);
}
}
}
}
return x;
}