File: utf8.c

package info (click to toggle)
spl 1.0~pre6-3.1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 2,360 kB
  • sloc: ansic: 16,614; yacc: 3,182; sh: 299; makefile: 167; xml: 156
file content (173 lines) | stat: -rw-r--r-- 4,195 bytes parent folder | download | duplicates (3)
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
/*
 *  SPL - The SPL Programming Language
 *  Copyright (C) 2004, 2005  Clifford Wolf <clifford@clifford.at>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 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 General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 *  utf8.c: Simple utf8 conversion and check functions
 */

#include <string.h>
#include <stdlib.h>
#include <assert.h>

#include "spl.h"
#include "utf8tab.h"

typedef unsigned char charset_map_table_type[4];

static struct {
	charset_map_table_type *table;
	char *name;
} charset_map[] = {
	{ UTF8TAB_ISO8859_1, "ascii" },
	{ UTF8TAB_ISO8859_1, "latin_1" },
	{ UTF8TAB_ISO8859_1, "iso8859_1" },
	{ 0, 0 }
};

const char *spl_utf8_check(const char *text)
{
	const unsigned char *utext = (const unsigned char *)text;
	int inc = 0;

	for (const unsigned char *t = utext; *t; t += inc + 1)
	{
#define IN(off, mask, value) ((t[inc=off] & (mask)) == (value))

		if (IN(0, 0x80, 0x00)) continue;

		if (IN(0, 0xE0, 0xC0) &&
		    IN(1, 0xC0, 0x80)) continue;

		if (IN(0, 0xF0, 0xE0) &&
		    IN(1, 0xC0, 0x80) &&
		    IN(2, 0xC0, 0x80)) continue;

		if (IN(0, 0xF8, 0xF0) &&
		    IN(1, 0xC0, 0x80) &&
		    IN(2, 0xC0, 0x80) &&
		    IN(3, 0xC0, 0x80)) continue;

		return (char*)t;
#undef IN
	}
	return 0;
}

char *spl_utf8_import(const char *text, const char *charset)
{
	const unsigned char *utext = (const unsigned char *)text;
	charset_map_table_type *tab = 0;

	if (!strcmp("utf_8", charset))
		return strdup(text);

	for (int i=0; charset_map[i].table; i++)
		if (!strcmp(charset_map[i].name, charset))
			tab = charset_map[i].table;

	if (!tab)
		return 0;

	int result_len = 0;

	for (const unsigned char *t = utext; *t; t++)
		if ((*t & 0x80) == 0) result_len++;
		else result_len += strlen((char*)(tab[*t-128]));

	char *result = malloc(result_len+1);
	char *r = result;

	for (const unsigned char *t = utext; *t; t++)
		if ((*t & 0x80) == 0) *(r++) = *t;
		else {
			strcpy(r, (const char*)tab[*t-128]);
			r += strlen((const char*)tab[*t-128]);
		}

	assert(r == result+result_len);
	result[result_len] = 0;
	return result;
}

char *spl_utf8_export(const char *text, const char *charset)
{
	const unsigned char *utext = (const unsigned char *)text;
	charset_map_table_type *tab = 0;

	if (!strcmp("utf_8", charset))
		return strdup(text);

	for (int i=0; charset_map[i].table; i++)
		if (!strcmp(charset_map[i].name, charset))
			tab = charset_map[i].table;

	if (!tab)
		return 0;

	int result_len = 0;
	char *result = malloc(strlen(text)+1);

	int inc = 0;
	for (const unsigned char *t = utext; *t; t += inc + 1)
	{
#define IN(off, mask, value) ((t[inc=off] & (mask)) == (value))

		if (IN(0, 0x80, 0x00)) {
			result[result_len++] = t[0];
			continue;
		}

		for (int i=0; i<128; i++) {
			inc = strlen((const char*)tab[i]);
			if (!strncmp((const char*)t, (const char*)tab[i], inc)) {
				result[result_len++] = 128+i;

				// in fact a strlen of 2 would result in t increase of 3
				// decrement inc to get correct t increment in for loop
				// raphael, 2007-10-08
				inc--; 
				
				goto next_export_char;
			}
		}

		result[result_len++] = '?';

		if (IN(0, 0xE0, 0xC0) &&
		    IN(1, 0xC0, 0x80)) continue;

		if (IN(0, 0xF0, 0xE0) &&
		    IN(1, 0xC0, 0x80) &&
		    IN(2, 0xC0, 0x80)) continue;

		if (IN(0, 0xF8, 0xF0) &&
		    IN(1, 0xC0, 0x80) &&
		    IN(2, 0xC0, 0x80) &&
		    IN(3, 0xC0, 0x80)) continue;

		/* Input is not UTF-8. This should not happen.. */
		for (inc=1; t[inc] & 0x80; t++)
			result[result_len++] = '?';

next_export_char:;
#undef IN
	}

	result[result_len++] = 0;
	return realloc(result, result_len);
}