Coverage Report

Created: 2025-07-01 07:09

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