dstring.c 7.9 KB

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