/src/netcdf-c/libdispatch/dstring.c
| Line | Count | Source | 
| 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 | 488k | { | 
| 31 | 488k |   if(ncstrp==NULL) | 
| 32 | 0 |     return; | 
| 33 | 488k |   free(ncstrp); | 
| 34 | 488k | } | 
| 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 | 488k | { | 
| 233 | 488k |   NC_string *ncstrp; | 
| 234 | 488k |   size_t sz; | 
| 235 |  |  | 
| 236 | 488k |   if (slen > SIZE_MAX - M_RNDUP(sizeof(NC_string)) - 1) | 
| 237 | 0 |     return NULL; | 
| 238 |  |    | 
| 239 | 488k |   sz = M_RNDUP(sizeof(NC_string)) + slen + 1; | 
| 240 |  |  | 
| 241 |  | #if 0 | 
| 242 |  |   sz = _RNDUP(sz, X_ALIGN); | 
| 243 |  | #endif | 
| 244 |  |  | 
| 245 | 488k |   ncstrp = (NC_string *)malloc(sz); | 
| 246 | 488k |   if( ncstrp == NULL ) | 
| 247 | 10 |     return NULL; | 
| 248 | 488k |   (void) memset(ncstrp, 0, sz); | 
| 249 |  |  | 
| 250 | 488k |   ncstrp->nchars = sz - M_RNDUP(sizeof(NC_string)) - 1; | 
| 251 | 488k |   assert(ncstrp->nchars + 1 > slen); | 
| 252 | 488k |   ncstrp->cp = (char *)ncstrp + M_RNDUP(sizeof(NC_string)); | 
| 253 |  |  | 
| 254 | 488k |   if(str != NULL && *str != 0) | 
| 255 | 0 |   { | 
| 256 | 0 |     (void) strncpy(ncstrp->cp, str, ncstrp->nchars +1); | 
| 257 | 0 |     ncstrp->cp[ncstrp->nchars] = 0; | 
| 258 | 0 |   } | 
| 259 |  |  | 
| 260 | 488k |   return(ncstrp); | 
| 261 | 488k | } | 
| 262 |  |  | 
| 263 |  |  | 
| 264 |  | /* | 
| 265 |  |  * If possible, change the value of an NC_string to 'str'. | 
| 266 |  |  * | 
| 267 |  |  * Formerly | 
| 268 |  | NC_re_string() | 
| 269 |  |  */ | 
| 270 |  |  | 
| 271 |  | int | 
| 272 |  |    set_NC_string(NC_string *ncstrp, const char *str) | 
| 273 | 0 |  { | 
| 274 | 0 |   size_t slen; | 
| 275 |  | 
 | 
| 276 | 0 |   assert(str != NULL && *str != 0); | 
| 277 |  | 
 | 
| 278 | 0 |   slen = strlen(str); | 
| 279 |  | 
 | 
| 280 | 0 |   if(ncstrp->nchars < slen) | 
| 281 | 0 |     return NC_ENOTINDEFINE; | 
| 282 |  |  | 
| 283 | 0 |   strncpy(ncstrp->cp, str, ncstrp->nchars); | 
| 284 |  |   /* Don't adjust ncstrp->nchars, it includes extra space in the | 
| 285 |  |    * header for potential later expansion of string. */ | 
| 286 |  | 
 | 
| 287 | 0 |   return NC_NOERR; | 
| 288 | 0 | } |