1 /*
2  charencode.h - miscellaneous character encoding
3 
4  This software may be used and distributed according to the terms of
5  the GNU General Public License, incorporated herein by reference.
6 */
7 
8 #ifndef _HG_CHARENCODE_H_
9 #define _HG_CHARENCODE_H_
10 
11 #include <Python.h>
12 #include "compat.h"
13 
14 /* This should be kept in sync with normcasespecs in encoding.py. */
15 enum normcase_spec {
16 	NORMCASE_LOWER = -1,
17 	NORMCASE_UPPER = 1,
18 	NORMCASE_OTHER = 0
19 };
20 
21 PyObject *unhexlify(const char *str, Py_ssize_t len);
22 PyObject *isasciistr(PyObject *self, PyObject *args);
23 PyObject *asciilower(PyObject *self, PyObject *args);
24 PyObject *asciiupper(PyObject *self, PyObject *args);
25 PyObject *make_file_foldmap(PyObject *self, PyObject *args);
26 PyObject *jsonescapeu8fast(PyObject *self, PyObject *args);
27 
28 /* clang-format off */
29 static const int8_t hextable[256] = {
30 	-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,
32 	-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
33 	 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, -1, -1, -1, -1, -1, -1, /* 0-9 */
34 	-1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* A-F */
35 	-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
36 	-1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* a-f */
37 	-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
38 	-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
39 	-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
40 	-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
41 	-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,
43 	-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,
45 	-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
46 };
47 /* clang-format on */
48 
hexdigit(const char * p,Py_ssize_t off)49 static inline int hexdigit(const char *p, Py_ssize_t off)
50 {
51 	int8_t val = hextable[(unsigned char)p[off]];
52 
53 	if (val >= 0) {
54 		return val;
55 	}
56 
57 	PyErr_SetString(PyExc_ValueError, "input contains non-hex character");
58 	return 0;
59 }
60 
61 #endif /* _HG_CHARENCODE_H_ */
62