forked from sahlberg/libsmb2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunicode.c
197 lines (171 loc) · 4.85 KB
/
unicode.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
/* -*- mode:c; tab-width:8; c-basic-offset:8; indent-tabs-mode:nil; -*- */
/*
Copyright (C) 2016 by Ronnie Sahlberg <[email protected]>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#ifdef HAVE_STDINT_H
#include <stdint.h>
#endif
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#ifdef HAVE_STRING_H
#include <string.h>
#endif
#ifdef STDC_HEADERS
#include <stddef.h>
#endif
#include "portable-endian.h"
#include <smb2.h>
#include <libsmb2.h>
#include "libsmb2-private.h"
/* Count number of leading 1 bits in the char */
static int
l1(char c)
{
int i = 0;
while (c & 0x80) {
i++;
c <<= 1;
}
return i;
}
/* Validates that utf8 points to a valid utf8 codepoint.
* Will update **utf8 to point at the next character in the string.
* return 0 if the encoding is valid and
* -1 if not.
* If the encoding is valid the codepoint will be returned in *cp.
*/
static int
validate_utf8_cp(const char **utf8, uint16_t *cp)
{
int c = *(*utf8)++;
int l = l1(c);
switch (l) {
case 0:
/* 7-bit ascii is always ok */
*cp = c & 0x7f;
return 0;
case 1:
/* 10.. .... can never start a new codepoint */
return -1;
case 2:
case 3:
*cp = c & 0x1f;
/* 2 and 3 byte sequences must always be followed by exactly
* 1 or 2 chars matching 10.. ....
*/
while(--l) {
c = *(*utf8)++;
if (l1(c) != 1) {
return -1;
}
*cp <<= 6;
*cp |= (c & 0x3f);
}
return 0;
}
return -1;
}
/* Validate that the given string is properly formated UTF8.
* Returns >=0 if valid UTF8 and -1 if not.
*/
static int
validate_utf8_str(const char *utf8)
{
const char *u = utf8;
int i = 0;
uint16_t cp;
while (*u) {
if (validate_utf8_cp(&u, &cp) < 0) {
return -1;
}
i++;
}
return i;
}
/* Convert a UTF8 string into UCS2 Little Endian */
struct ucs2 *
utf8_to_ucs2(const char *utf8)
{
struct ucs2 *ucs2;
int i, len;
len = validate_utf8_str(utf8);
if (len < 0) {
return NULL;
}
ucs2 = malloc(offsetof(struct ucs2, val) + 2 * len);
if (ucs2 == NULL) {
return NULL;
}
ucs2->len = len;
for (i = 0; i < len; i++) {
validate_utf8_cp(&utf8, &ucs2->val[i]);
ucs2->val[i] = htole32(ucs2->val[i]);
}
return ucs2;
}
/* Returns how many bytes we need to store a UCS2 codepoint
*/
static int
ucs2_cp_size(uint16_t cp)
{
if (cp > 0x07ff) {
return 3;
}
if (cp > 0x007f) {
return 2;
}
return 1;
}
/* Convert a UCS2 string into UTF8
*/
const char *
ucs2_to_utf8(const uint16_t *ucs2, int ucs2_len)
{
int i, utf8_len = 1;
char *str, *tmp;
/* How many bytes do we need for utf8 ? */
for (i = 0; i < ucs2_len; i++) {
utf8_len += ucs2_cp_size(ucs2[i]);
}
str = tmp = malloc(utf8_len);
if (str == NULL) {
return NULL;
}
str[utf8_len - 1] = 0;
for (i = 0; i < ucs2_len; i++) {
uint16_t c = le32toh(ucs2[i]);
int l = ucs2_cp_size(c);
switch (l) {
case 3:
*tmp++ = 0xe0 | (c >> 12);
*tmp++ = 0x80 | ((c >> 6) & 0xbf);
*tmp++ = 0x80 | ((c ) & 0xbf);
break;
case 2:
*tmp++ = 0xc0 | (c >> 6);
*tmp++ = 0x80 | ((c ) & 0xbf);
break;
case 1:
*tmp++ = c;
break;
}
}
return str;
}