Coverage Report

Created: 2025-06-13 06:55

/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
 * 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
0
{
47
0
  xdg_unichar_t ucs32;
48
0
  if( ! ( *source & 0x80 ) )
49
0
    {
50
0
      ucs32 = *source;
51
0
    }
52
0
  else
53
0
    {
54
0
      int bytelength = 0;
55
0
      xdg_unichar_t result;
56
0
      if ( ! (*source & 0x40) )
57
0
  {
58
0
    ucs32 = *source;
59
0
  }
60
0
      else
61
0
  {
62
0
    if ( ! (*source & 0x20) )
63
0
      {
64
0
        result = *source++ & 0x1F;
65
0
        bytelength = 2;
66
0
      }
67
0
    else if ( ! (*source & 0x10) )
68
0
      {
69
0
        result = *source++ & 0x0F;
70
0
        bytelength = 3;
71
0
      }
72
0
    else if ( ! (*source & 0x08) )
73
0
      {
74
0
        result = *source++ & 0x07;
75
0
        bytelength = 4;
76
0
      }
77
0
    else if ( ! (*source & 0x04) )
78
0
      {
79
0
        result = *source++ & 0x03;
80
0
        bytelength = 5;
81
0
      }
82
0
    else if ( ! (*source & 0x02) )
83
0
      {
84
0
        result = *source++ & 0x01;
85
0
        bytelength = 6;
86
0
      }
87
0
    else
88
0
      {
89
0
        result = *source++;
90
0
        bytelength = 1;
91
0
      }
92
93
0
    for ( bytelength --; bytelength > 0; bytelength -- )
94
0
      {
95
0
        result <<= 6;
96
0
        result |= *source++ & 0x3F;
97
0
      }
98
0
    ucs32 = result;
99
0
  }
100
0
    }
101
0
  return ucs32;
102
0
}
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
0
{
110
  /* FIXME: Do a real to_upper sometime */
111
  /* CaseFolding-3.2.0.txt has a table of rules. */
112
0
  if ((source & 0xFF) == source)
113
0
    return (xdg_unichar_t) tolower ((unsigned char) source);
114
0
  return source;
115
0
}
116
117
int
118
_xdg_utf8_validate (const char *source __attribute__((unused)))
119
0
{
120
  /* FIXME: actually write */
121
0
  return TRUE;
122
0
}
123
124
const char *
125
_xdg_get_base_name (const char *file_name)
126
0
{
127
0
  const char *base_name;
128
129
0
  if (file_name == NULL)
130
0
    return NULL;
131
132
0
  base_name = strrchr (file_name, '/');
133
134
0
  if (base_name == NULL)
135
0
    return file_name;
136
0
  else
137
0
    return base_name + 1;
138
0
}
139
140
xdg_unichar_t *
141
_xdg_convert_to_ucs4 (const char *source, int *len)
142
0
{
143
0
  xdg_unichar_t *out;
144
0
  int i;
145
0
  const char *p;
146
147
0
  out = malloc (sizeof (xdg_unichar_t) * (strlen (source) + 1));
148
149
0
  p = source;
150
0
  i = 0;
151
0
  while (*p) 
152
0
    {
153
0
      out[i++] = _xdg_utf8_to_ucs4 (p);
154
0
      p = _xdg_utf8_next_char (p); 
155
0
    }
156
0
  out[i] = 0;
157
0
  *len = i;
158
 
159
0
  return out;
160
0
}
161
162
void
163
_xdg_reverse_ucs4 (xdg_unichar_t *source, int len)
164
0
{
165
0
  xdg_unichar_t c;
166
0
  int i;
167
168
0
  for (i = 0; i < len - i - 1; i++) 
169
0
    {
170
0
      c = source[i]; 
171
0
      source[i] = source[len - i - 1];
172
0
      source[len - i - 1] = c;
173
0
    }
174
0
}
175
176
const char *
177
_xdg_binary_or_text_fallback(const void *data, size_t len)
178
0
{
179
0
  unsigned char *chardata;
180
0
  size_t i;
181
182
0
  chardata = (unsigned char *) data;
183
0
  for (i = 0; i < 128 && i < len; ++i)
184
0
    {
185
0
       if (chardata[i] < 32 && chardata[i] != 9 && chardata[i] != 10 && chardata[i] != 13)
186
0
         return XDG_MIME_TYPE_UNKNOWN; /* binary data */
187
0
    }
188
189
0
  return XDG_MIME_TYPE_TEXTPLAIN;
190
0
}