Coverage Report

Created: 2023-05-28 06:42

/src/netcdf-c/libdispatch/dstring.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 *  Copyright 2018, University Corporation for Atmospheric Research
3
 *      See netcdf/COPYRIGHT file for copying and redistribution conditions.
4
 */
5
/* $Id: string.c,v 1.76 2010/05/26 21:43:33 dmh Exp $ */
6
7
#include "config.h"
8
#include <stdlib.h>
9
#include <stdio.h>
10
#include <string.h>
11
#include <ctype.h>
12
#include <assert.h>
13
#include "ncdispatch.h"
14
#include "rnd.h"
15
#include "ncutf8.h"
16
17
/* There are 3 levels of UTF8 checking: 1=> (exact)validating 2=>relaxed
18
   and 3=>very relaxed
19
*/
20
/* Use semi-relaxed check */
21
#define UTF8_CHECK 2
22
23
/*
24
 * Free string, and, if needed, its values.
25
 * Formerly
26
NC_free_string()
27
 */
28
void
29
free_NC_string(NC_string *ncstrp)
30
357k
{
31
357k
  if(ncstrp==NULL)
32
0
    return;
33
357k
  free(ncstrp);
34
357k
}
35
36
37
static int
38
nextUTF8(const char* cp)
39
0
{
40
    /*  The goal here is to recognize the length of each
41
  multibyte utf8 character sequence and skip it.
42
        Again, we assume that every non-ascii character is legal.
43
        We can define three possible tests of decreasing correctness
44
        (in the sense that the least correct will allow some sequences that
45
        are technically illegal UTF8).
46
        As Regular expressions they are as follows:
47
        1. most correct:
48
            UTF8   ([\xC2-\xDF][\x80-\xBF])                       \
49
                 | (\xE0[\xA0-\xBF][\x80-\xBF])                   \
50
                 | ([\xE1-\xEC][\x80-\xBF][\x80-\xBF])            \
51
                 | (\xED[\x80-\x9F][\x80-\xBF])                   \
52
                 | ([\xEE-\xEF][\x80-\xBF][\x80-\xBF])            \
53
                 | (\xF0[\x90-\xBF][\x80-\xBF][\x80-\xBF])        \
54
                 | ([\xF1-\xF3][\x80-\xBF][\x80-\xBF][\x80-\xBF]) \
55
                 | (\xF4[\x80-\x8F][\x80-\xBF][\x80-\xBF])        \
56
57
        2. partially relaxed:
58
            UTF8 ([\xC0-\xDF][\x80-\xBF])
59
                 |([\xE0-\xEF][\x80-\xBF][\x80-\xBF])
60
                 |([\xF0-\xF7][\x80-\xBF][\x80-\xBF][\x80-\xBF])
61
62
        3. The most relaxed version of UTF8:
63
            UTF8 ([\xC0-\xD6].)|([\xE0-\xEF]..)|([\xF0-\xF7]...)
64
65
        We use #2 here.
66
67
  The tests are derived from the table at
68
      http://www.w3.org/2005/03/23-lex-U
69
    */
70
71
/* Define a test macro to test against a range */
72
0
#define RANGE(c,lo,hi) (((uchar)c) >= lo && ((uchar)c) <= hi)
73
/* Define a common RANGE */
74
0
#define RANGE0(c) RANGE(c,0x80,0xBF)
75
76
0
    int ch0;
77
78
0
    int skip = -1; /* assume failed */
79
80
0
    ch0 = (uchar)*cp;
81
0
    if(ch0 <= 0x7f) skip = 1; /* remove ascii case */
82
0
    else
83
84
0
#if UTF8_CHECK == 2
85
    /* Do relaxed validation check */
86
0
    if(RANGE(ch0,0xC0,0XDF)) {/* 2-bytes, but check */
87
0
        if(cp[1] != 0 && RANGE0(cp[1]))
88
0
    skip = 2; /* two bytes */
89
0
    } else if(RANGE(ch0,0xE0,0XEF)) {/* 3-bytes, but check */
90
0
        if(cp[1] != 0 && RANGE0(cp[1]) && cp[2] != 0 && RANGE0(cp[1]))
91
0
    skip = 3; /* three bytes */
92
0
    } else if(RANGE(ch0,0xF0,0XF7)) {/* 3-bytes, but check */
93
0
        if(cp[1] != 0 && RANGE0(cp[1]) && cp[2] != 0
94
0
           && RANGE0(cp[1]) && cp[3] != 0 && RANGE0(cp[1]))
95
0
    skip = 4; /* four bytes*/
96
0
    }
97
#elif UTF8_CHECK == 1
98
    /* Do exact validation check */
99
    if(RANGE(ch0,0xC2,0xDF)) {/* non-overlong 2-bytes */
100
  int ch1 = (uchar)cp[1];
101
  if(ch1 != 0 && RANGE0(ch1)) skip = 2;
102
    } else if((ch0 == 0xE0)) {/* 3-bytes, not overlong */
103
  int ch1 = (uchar)cp[1];
104
  if(ch1 != 0 && RANGE(ch1,0xA0,0xBF)) {
105
      int ch2 = (uchar)cp[2];
106
      if(ch2 != 0 && RANGE0(ch2)) skip = 3;
107
    } else if((ch0 == 0xED)) {/* 3-bytes minus surrogates */
108
  int ch1 = (uchar)cp[1];
109
  if(ch1 != 0 && RANGE(ch1,0x80,0x9f)) {
110
      int ch2 = (uchar)cp[2];
111
      if(ch2 != 0 && RANGE0(ch2)) skip = 3;
112
    } else if(RANGE(ch0,0xE1,0xEC) || ch0 == 0xEE || ch0 == 0xEF)
113
  int ch1 = (uchar)cp[1];
114
  if(ch1 != 0 && RANGE0(ch1)) {
115
      int ch2 = (uchar)cp[2];
116
      if(ch2 != 0 && RANGE0(ch2)) skip = 3;
117
  }
118
    } else if((ch0 == 0xF0)) {/* planes 1-3 */
119
  int ch1 = (uchar)cp[1];
120
  if(ch1 != 0 && RANGE(ch1,0x90,0xBF) {
121
      int ch2 = (uchar)cp[2];
122
      if(ch2 != 0 && RANGE0(ch2)) {
123
          int ch3 = (uchar)cp[3];
124
          if(ch3 != 0 && RANGE0(ch3)) skip = 4;
125
      }
126
  }
127
    } else if((ch0 == 0xF4)) {/* plane 16 */
128
  int ch1 = (uchar)cp[1];
129
  if(ch1 != 0 && RANGE0(ch1)) {
130
      int ch2 = (uchar)cp[2];
131
      if(ch2 != 0 && RANGE0(ch2)) {
132
          int ch3 = (uchar)cp[3];
133
          if(ch3 != 0 && RANGE0(ch3)) skip = 4;
134
      }
135
  }
136
    } else if(RANGE(ch0,0xF1,0xF3) { /* planes 4-15 */
137
  int ch1 = (uchar)cp[1];
138
  if(ch1 != 0 && RANGE0(ch1)) {
139
      int ch2 = (uchar)cp[2];
140
      if(ch2 != 0 && RANGE0(ch2)) {
141
          int ch3 = (uchar)cp[3];
142
          if(ch3 != 0 && RANGE0(ch3)) skip = 4;
143
      }
144
  }
145
    }
146
#else
147
#error "Must Define UTF8_CHECK as 1 or 2"
148
#endif
149
0
    return skip;
150
0
}
151
152
153
/*
154
 * Verify that a name string is valid syntax.  The allowed name
155
 * syntax (in RE form) is:
156
 *
157
 * ([a-zA-Z0-9_]|{UTF8})([^\x00-\x1F\x7F/]|{UTF8})*
158
 *
159
 * where UTF8 represents a multibyte UTF-8 encoding.  Also, no
160
 * trailing spaces are permitted in names.  This definition
161
 * must be consistent with the one in ncgen.l.  We do not allow '/'
162
 * because HDF5 does not permit slashes in names as slash is used as a
163
 * group separator.  If UTF-8 is supported, then a multi-byte UTF-8
164
 * character can occur anywhere within an identifier.  We later
165
 * normalize UTF-8 strings to NFC to facilitate matching and queries.
166
 */
167
int
168
NC_check_name(const char *name)
169
0
{
170
0
  int skip;
171
0
  int ch;
172
0
  const char *cp = name;
173
0
  int stat;
174
175
0
  assert(name != NULL);
176
177
0
  if(*name == 0    /* empty names disallowed */
178
0
     || strchr(cp, '/')) /* '/' can't be in a name */
179
0
    goto fail;
180
181
  /* check validity of any UTF-8 */
182
0
  stat = nc_utf8_validate((const unsigned char *)name);
183
0
  if (stat != NC_NOERR)
184
0
      goto fail;
185
186
  /* First char must be [a-z][A-Z][0-9]_ | UTF8 */
187
0
  ch = (uchar)*cp;
188
0
  if(ch <= 0x7f) {
189
0
      if(   !('A' <= ch && ch <= 'Z')
190
0
         && !('a' <= ch && ch <= 'z')
191
0
         && !('0' <= ch && ch <= '9')
192
0
         && ch != '_' )
193
0
    goto fail;
194
0
      cp++;
195
0
  } else {
196
0
      if((skip = nextUTF8(cp)) < 0)
197
0
    goto fail;
198
0
      cp += skip;
199
0
  }
200
201
0
  while(*cp != 0) {
202
0
      ch = (uchar)*cp;
203
      /* handle simple 0x00-0x7f characters here */
204
0
      if(ch <= 0x7f) {
205
0
                if( ch < ' ' || ch > 0x7E) /* control char or DEL */
206
0
      goto fail;
207
0
    cp++;
208
0
      } else {
209
0
    if((skip = nextUTF8(cp)) < 0) goto fail;
210
0
    cp += skip;
211
0
      }
212
0
      if(cp - name > NC_MAX_NAME)
213
0
    return NC_EMAXNAME;
214
0
  }
215
0
  if(ch <= 0x7f && isspace(ch)) /* trailing spaces disallowed */
216
0
      goto fail;
217
0
  return NC_NOERR;
218
0
fail:
219
0
        return NC_EBADNAME;
220
0
}
221
222
223
/*
224
 * Allocate a NC_string structure large enough
225
 * to hold slen characters.
226
 * Formerly
227
NC_new_string(count, str)
228
 */
229
230
NC_string *
231
new_NC_string(size_t slen, const char *str)
232
357k
{
233
357k
  NC_string *ncstrp;
234
357k
  size_t sz = M_RNDUP(sizeof(NC_string)) + slen + 1;
235
236
#if 0
237
  sz = _RNDUP(sz, X_ALIGN);
238
#endif
239
240
357k
  ncstrp = (NC_string *)malloc(sz);
241
357k
  if( ncstrp == NULL )
242
2
    return NULL;
243
357k
  (void) memset(ncstrp, 0, sz);
244
245
357k
  ncstrp->nchars = sz - M_RNDUP(sizeof(NC_string)) - 1;
246
357k
  assert(ncstrp->nchars + 1 > slen);
247
357k
  ncstrp->cp = (char *)ncstrp + M_RNDUP(sizeof(NC_string));
248
249
357k
  if(str != NULL && *str != 0)
250
0
  {
251
0
    (void) strncpy(ncstrp->cp, str, ncstrp->nchars +1);
252
0
    ncstrp->cp[ncstrp->nchars] = 0;
253
0
  }
254
255
357k
  return(ncstrp);
256
357k
}
257
258
259
/*
260
 * If possible, change the value of an NC_string to 'str'.
261
 *
262
 * Formerly
263
NC_re_string()
264
 */
265
266
int
267
   set_NC_string(NC_string *ncstrp, const char *str)
268
0
 {
269
0
  size_t slen;
270
271
0
  assert(str != NULL && *str != 0);
272
273
0
  slen = strlen(str);
274
275
0
  if(ncstrp->nchars < slen)
276
0
    return NC_ENOTINDEFINE;
277
278
0
  strncpy(ncstrp->cp, str, ncstrp->nchars);
279
  /* Don't adjust ncstrp->nchars, it includes extra space in the
280
   * header for potential later expansion of string. */
281
282
0
  return NC_NOERR;
283
0
}