Line data Source code
1 : /* vim: set sw=8: -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 : /*
3 : * utils.c: Various utility routines that should have been in glib.
4 : *
5 : * Authors:
6 : * Miguel de Icaza (miguel@gnu.org)
7 : * Jukka-Pekka Iivonen (iivonen@iki.fi)
8 : * Zbigniew Chyla (cyba@gnome.pl)
9 : * Morten Welinder (terra@gnome.org)
10 : */
11 : #include <config.h>
12 : #include "go-glib-extras.h"
13 :
14 : #include <glib/gi18n-lib.h>
15 : #include <libxml/encoding.h>
16 :
17 : #include <stdlib.h>
18 : #include <math.h>
19 : #include <stdio.h>
20 : #include <string.h>
21 : #include <sys/types.h>
22 : #include <sys/stat.h>
23 : #ifdef HAVE_UNISTD_H
24 : #include <unistd.h>
25 : #endif
26 : #include <fcntl.h>
27 : #include <errno.h>
28 :
29 0 : gint go_ascii_strcase_equal(gconstpointer v1, gconstpointer v2)
30 : {
31 0 : return g_ascii_strcasecmp((char const *) v1, (char const *) v2) == 0;
32 : }
33 :
34 : /* a char* hash function from ASU */
35 0 : guint go_ascii_strcase_hash(gconstpointer v)
36 : {
37 0 : unsigned const char *s = (unsigned const char *) v;
38 : unsigned const char *p;
39 0 : guint h = 0, g;
40 :
41 0 : for (p = s; *p != '\0'; p += 1)
42 : {
43 0 : h = (h << 4) + g_ascii_tolower(*p);
44 0 : if ((g = h & 0xf0000000))
45 : {
46 0 : h = h ^ (g >> 24);
47 0 : h = h ^ g;
48 : }
49 : }
50 :
51 0 : return h /* % M */;
52 : }
53 :
54 : const char *
55 3 : go_guess_encoding(const char *raw, size_t len, const char *user_guess,
56 : char **utf8_str)
57 : {
58 : int try;
59 3 : gboolean debug = FALSE;
60 :
61 3 : g_return_val_if_fail(raw != NULL, NULL);
62 :
63 0 : for (try = 1; 1; try++)
64 0 : {
65 3 : char const *guess = NULL;
66 3 : GError *error = NULL;
67 : char *utf8_data;
68 :
69 3 : switch (try)
70 : {
71 3 : case 1:
72 3 : guess = user_guess;
73 3 : break;
74 0 : case 2:
75 0 : g_get_charset(&guess);
76 0 : break;
77 0 : case 3:
78 : {
79 0 : xmlCharEncoding enc = xmlDetectCharEncoding(
80 : (const unsigned char*) raw, len);
81 0 : switch (enc)
82 : {
83 0 : case XML_CHAR_ENCODING_ERROR:
84 : case XML_CHAR_ENCODING_NONE:
85 0 : break;
86 0 : case XML_CHAR_ENCODING_UTF16LE:
87 : /* Default would give "UTF-16". */
88 0 : guess = "UTF-16LE";
89 0 : break;
90 0 : case XML_CHAR_ENCODING_UTF16BE:
91 : /* Default would give "UTF-16". */
92 0 : guess = "UTF-16BE";
93 0 : break;
94 0 : default:
95 0 : guess = xmlGetCharEncodingName(enc);
96 : }
97 0 : break;
98 : }
99 0 : case 4:
100 0 : guess = "ASCII";
101 0 : break;
102 0 : case 5:
103 0 : guess = "ISO-8859-1";
104 0 : break;
105 0 : case 6:
106 0 : guess = "UTF-8";
107 0 : break;
108 0 : default:
109 0 : return NULL;
110 : }
111 :
112 3 : if (!guess)
113 0 : continue;
114 :
115 3 : if (debug)
116 0 : g_print("Trying %s as encoding.\n", guess);
117 :
118 3 : utf8_data = g_convert(raw, len, "UTF-8", guess,
119 : NULL, NULL, &error);
120 3 : if (!error)
121 : {
122 : /*
123 : * We can actually fail this test when guess is UTF-8,
124 : * see #401588.
125 : */
126 3 : if (!g_utf8_validate(utf8_data, -1, NULL))
127 0 : continue;
128 3 : if (debug)
129 0 : g_print("Guessed %s as encoding.\n", guess);
130 3 : if (utf8_str)
131 0 : *utf8_str = utf8_data;
132 : else
133 3 : g_free(utf8_data);
134 3 : return guess;
135 : }
136 :
137 0 : g_error_free(error);
138 : }
139 : }
|