/src/rauc/subprojects/glib-2.76.5/gio/xdgmime/xdgmimeint.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* -*- mode: C; c-file-style: "gnu" -*- */ |
2 | | /* xdgmimeint.c: Internal defines and functions. |
3 | | * |
4 | | * More info can be found at http://www.freedesktop.org/standards/ |
5 | | * |
6 | | * Copyright (C) 2003 Red Hat, Inc. |
7 | | * Copyright (C) 2003 Jonathan Blandford <jrb@alum.mit.edu> |
8 | | * |
9 | | * Licensed under the Academic Free License version 2.0 |
10 | | * Or under the following terms: |
11 | | * |
12 | | * This library is free software; you can redistribute it and/or |
13 | | * modify it under the terms of the GNU Lesser General Public |
14 | | * License as published by the Free Software Foundation; either |
15 | | * version 2.1 of the License, or (at your option) any later version. |
16 | | * |
17 | | * This library is distributed in the hope that it will be useful, |
18 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
19 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
20 | | * Lesser General Public License for more details. |
21 | | * |
22 | | * You should have received a copy of the GNU Lesser General Public |
23 | | * License along with this library; if not, write to the |
24 | | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
25 | | * Boston, MA 02111-1307, USA. |
26 | | */ |
27 | | |
28 | | #ifdef HAVE_CONFIG_H |
29 | | #include <config.h> |
30 | | #endif |
31 | | |
32 | | #include "xdgmimeint.h" |
33 | | #include <ctype.h> |
34 | | #include <string.h> |
35 | | |
36 | | #ifndef FALSE |
37 | | #define FALSE (0) |
38 | | #endif |
39 | | |
40 | | #ifndef TRUE |
41 | | #define TRUE (!FALSE) |
42 | | #endif |
43 | | |
44 | | static const char _xdg_utf8_skip_data[256] = { |
45 | | 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, |
46 | | 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, |
47 | | 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, |
48 | | 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, |
49 | | 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, |
50 | | 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, |
51 | | 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, |
52 | | 3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,6,6,1,1 |
53 | | }; |
54 | | |
55 | | const char * const _xdg_utf8_skip = _xdg_utf8_skip_data; |
56 | | |
57 | | |
58 | | |
59 | | /* Returns the number of unprocessed characters. */ |
60 | | xdg_unichar_t |
61 | | _xdg_utf8_to_ucs4(const char *source) |
62 | 0 | { |
63 | 0 | xdg_unichar_t ucs32; |
64 | 0 | if( ! ( *source & 0x80 ) ) |
65 | 0 | { |
66 | 0 | ucs32 = *source; |
67 | 0 | } |
68 | 0 | else |
69 | 0 | { |
70 | 0 | int bytelength = 0; |
71 | 0 | xdg_unichar_t result; |
72 | 0 | if ( ! (*source & 0x40) ) |
73 | 0 | { |
74 | 0 | ucs32 = *source; |
75 | 0 | } |
76 | 0 | else |
77 | 0 | { |
78 | 0 | if ( ! (*source & 0x20) ) |
79 | 0 | { |
80 | 0 | result = *source++ & 0x1F; |
81 | 0 | bytelength = 2; |
82 | 0 | } |
83 | 0 | else if ( ! (*source & 0x10) ) |
84 | 0 | { |
85 | 0 | result = *source++ & 0x0F; |
86 | 0 | bytelength = 3; |
87 | 0 | } |
88 | 0 | else if ( ! (*source & 0x08) ) |
89 | 0 | { |
90 | 0 | result = *source++ & 0x07; |
91 | 0 | bytelength = 4; |
92 | 0 | } |
93 | 0 | else if ( ! (*source & 0x04) ) |
94 | 0 | { |
95 | 0 | result = *source++ & 0x03; |
96 | 0 | bytelength = 5; |
97 | 0 | } |
98 | 0 | else if ( ! (*source & 0x02) ) |
99 | 0 | { |
100 | 0 | result = *source++ & 0x01; |
101 | 0 | bytelength = 6; |
102 | 0 | } |
103 | 0 | else |
104 | 0 | { |
105 | 0 | result = *source++; |
106 | 0 | bytelength = 1; |
107 | 0 | } |
108 | |
|
109 | 0 | for ( bytelength --; bytelength > 0; bytelength -- ) |
110 | 0 | { |
111 | 0 | result <<= 6; |
112 | 0 | result |= *source++ & 0x3F; |
113 | 0 | } |
114 | 0 | ucs32 = result; |
115 | 0 | } |
116 | 0 | } |
117 | 0 | return ucs32; |
118 | 0 | } |
119 | | |
120 | | |
121 | | /* hullo. this is great code. don't rewrite it */ |
122 | | |
123 | | xdg_unichar_t |
124 | | _xdg_ucs4_to_lower (xdg_unichar_t source) |
125 | 0 | { |
126 | | /* FIXME: Do a real to_upper sometime */ |
127 | | /* CaseFolding-3.2.0.txt has a table of rules. */ |
128 | 0 | if ((source & 0xFF) == source) |
129 | 0 | return (xdg_unichar_t) tolower ((unsigned char) source); |
130 | 0 | return source; |
131 | 0 | } |
132 | | |
133 | | int |
134 | | _xdg_utf8_validate (const char *source) |
135 | 0 | { |
136 | | /* FIXME: actually write */ |
137 | 0 | return TRUE; |
138 | 0 | } |
139 | | |
140 | | const char * |
141 | | _xdg_get_base_name (const char *file_name) |
142 | 0 | { |
143 | 0 | const char *base_name; |
144 | |
|
145 | 0 | if (file_name == NULL) |
146 | 0 | return NULL; |
147 | | |
148 | 0 | base_name = strrchr (file_name, '/'); |
149 | |
|
150 | 0 | if (base_name == NULL) |
151 | 0 | return file_name; |
152 | 0 | else |
153 | 0 | return base_name + 1; |
154 | 0 | } |
155 | | |
156 | | xdg_unichar_t * |
157 | | _xdg_convert_to_ucs4 (const char *source, int *len) |
158 | 0 | { |
159 | 0 | xdg_unichar_t *out; |
160 | 0 | int i; |
161 | 0 | const char *p; |
162 | |
|
163 | 0 | out = malloc (sizeof (xdg_unichar_t) * (strlen (source) + 1)); |
164 | |
|
165 | 0 | p = source; |
166 | 0 | i = 0; |
167 | 0 | while (*p) |
168 | 0 | { |
169 | 0 | out[i++] = _xdg_utf8_to_ucs4 (p); |
170 | 0 | p = _xdg_utf8_next_char (p); |
171 | 0 | } |
172 | 0 | out[i] = 0; |
173 | 0 | *len = i; |
174 | | |
175 | 0 | return out; |
176 | 0 | } |
177 | | |
178 | | void |
179 | | _xdg_reverse_ucs4 (xdg_unichar_t *source, int len) |
180 | 0 | { |
181 | 0 | xdg_unichar_t c; |
182 | 0 | int i; |
183 | |
|
184 | 0 | for (i = 0; i < len - i - 1; i++) |
185 | 0 | { |
186 | 0 | c = source[i]; |
187 | 0 | source[i] = source[len - i - 1]; |
188 | 0 | source[len - i - 1] = c; |
189 | 0 | } |
190 | 0 | } |
191 | | |
192 | | const char * |
193 | | _xdg_binary_or_text_fallback(const void *data, size_t len) |
194 | 0 | { |
195 | 0 | unsigned char *chardata; |
196 | 0 | size_t i; |
197 | |
|
198 | 0 | chardata = (unsigned char *) data; |
199 | 0 | for (i = 0; i < 128 && i < len; ++i) |
200 | 0 | { |
201 | 0 | if (chardata[i] < 32 && chardata[i] != 9 && chardata[i] != 10 && chardata[i] != 13) |
202 | 0 | return XDG_MIME_TYPE_UNKNOWN; /* binary data */ |
203 | 0 | } |
204 | | |
205 | 0 | return XDG_MIME_TYPE_TEXTPLAIN; |
206 | 0 | } |