]> git.proxmox.com Git - libxdgmime-perl.git/blob - xdgmime-source/src/xdgmimeint.c
Merge commit '73d5a30a1d93eb79761f2472c685afb8e42a8646' from upstream
[libxdgmime-perl.git] / xdgmime-source / src / xdgmimeint.c
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 * SPDX-License-Identifier: LGPL-2.1-or-later or AFL-2.0
10 */
11
12 #ifdef HAVE_CONFIG_H
13 #include <config.h>
14 #endif
15
16 #include "xdgmimeint.h"
17 #include <ctype.h>
18 #include <string.h>
19
20 #ifndef FALSE
21 #define FALSE (0)
22 #endif
23
24 #ifndef TRUE
25 #define TRUE (!FALSE)
26 #endif
27
28 static const char _xdg_utf8_skip_data[256] = {
29 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,
30 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,
31 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,
32 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,
33 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,
34 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,
35 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,
36 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
37 };
38
39 const char * const _xdg_utf8_skip = _xdg_utf8_skip_data;
40
41
42
43 /* Returns the number of unprocessed characters. */
44 xdg_unichar_t
45 _xdg_utf8_to_ucs4(const char *source)
46 {
47 xdg_unichar_t ucs32;
48 if( ! ( *source & 0x80 ) )
49 {
50 ucs32 = *source;
51 }
52 else
53 {
54 int bytelength = 0;
55 xdg_unichar_t result;
56 if ( ! (*source & 0x40) )
57 {
58 ucs32 = *source;
59 }
60 else
61 {
62 if ( ! (*source & 0x20) )
63 {
64 result = *source++ & 0x1F;
65 bytelength = 2;
66 }
67 else if ( ! (*source & 0x10) )
68 {
69 result = *source++ & 0x0F;
70 bytelength = 3;
71 }
72 else if ( ! (*source & 0x08) )
73 {
74 result = *source++ & 0x07;
75 bytelength = 4;
76 }
77 else if ( ! (*source & 0x04) )
78 {
79 result = *source++ & 0x03;
80 bytelength = 5;
81 }
82 else if ( ! (*source & 0x02) )
83 {
84 result = *source++ & 0x01;
85 bytelength = 6;
86 }
87 else
88 {
89 result = *source++;
90 bytelength = 1;
91 }
92
93 for ( bytelength --; bytelength > 0; bytelength -- )
94 {
95 result <<= 6;
96 result |= *source++ & 0x3F;
97 }
98 ucs32 = result;
99 }
100 }
101 return ucs32;
102 }
103
104
105 /* hullo. this is great code. don't rewrite it */
106
107 xdg_unichar_t
108 _xdg_ucs4_to_lower (xdg_unichar_t source)
109 {
110 /* FIXME: Do a real to_upper sometime */
111 /* CaseFolding-3.2.0.txt has a table of rules. */
112 if ((source & 0xFF) == source)
113 return (xdg_unichar_t) tolower ((unsigned char) source);
114 return source;
115 }
116
117 int
118 _xdg_utf8_validate (const char *source)
119 {
120 /* FIXME: actually write */
121 return TRUE;
122 }
123
124 const char *
125 _xdg_get_base_name (const char *file_name)
126 {
127 const char *base_name;
128
129 if (file_name == NULL)
130 return NULL;
131
132 base_name = strrchr (file_name, '/');
133
134 if (base_name == NULL)
135 return file_name;
136 else
137 return base_name + 1;
138 }
139
140 xdg_unichar_t *
141 _xdg_convert_to_ucs4 (const char *source, int *len)
142 {
143 xdg_unichar_t *out;
144 int i;
145 const char *p;
146
147 out = malloc (sizeof (xdg_unichar_t) * (strlen (source) + 1));
148
149 p = source;
150 i = 0;
151 while (*p)
152 {
153 out[i++] = _xdg_utf8_to_ucs4 (p);
154 p = _xdg_utf8_next_char (p);
155 }
156 out[i] = 0;
157 *len = i;
158
159 return out;
160 }
161
162 void
163 _xdg_reverse_ucs4 (xdg_unichar_t *source, int len)
164 {
165 xdg_unichar_t c;
166 int i;
167
168 for (i = 0; i < len - i - 1; i++)
169 {
170 c = source[i];
171 source[i] = source[len - i - 1];
172 source[len - i - 1] = c;
173 }
174 }
175
176 const char *
177 _xdg_binary_or_text_fallback(const void *data, size_t len)
178 {
179 unsigned char *chardata;
180 size_t i;
181
182 chardata = (unsigned char *) data;
183 for (i = 0; i < 128 && i < len; ++i)
184 {
185 if (chardata[i] < 32 && chardata[i] != 9 && chardata[i] != 10 && chardata[i] != 13)
186 return XDG_MIME_TYPE_UNKNOWN; /* binary data */
187 }
188
189 return XDG_MIME_TYPE_TEXTPLAIN;
190 }