OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright (c) 2001-2004 Twisted Matrix Laboratories. | |
3 * See LICENSE for details. | |
4 | |
5 * | |
6 */ | |
7 /* _c_urlarg.c */ | |
8 | |
9 #ifdef __cplusplus | |
10 extern "C" { | |
11 #endif | |
12 #include <Python.h> | |
13 #include <cStringIO.h> | |
14 #ifdef __cplusplus | |
15 } | |
16 #endif | |
17 | |
18 #ifdef __GNUC__ | |
19 # define TM_INLINE inline | |
20 #else | |
21 # define TM_INLINE /* */ | |
22 #endif | |
23 | |
24 static PyObject* UrlargError; | |
25 | |
26 #define OUTPUTCHAR(c,n) PycStringIO->cwrite(output, c, n) | |
27 | |
28 #define STATE_INITIAL 0 | |
29 #define STATE_PERCENT 1 | |
30 #define STATE_HEXDIGIT 2 | |
31 | |
32 #define NOT_HEXDIGIT 255 | |
33 unsigned char hexdigits[256]; | |
34 | |
35 TM_INLINE int ishexdigit(unsigned char c) { | |
36 return hexdigits[c]; | |
37 } | |
38 | |
39 static PyObject *unquote(PyObject *self, PyObject *args, PyObject *kwargs) | |
40 { | |
41 unsigned char *s, *r, *end; | |
42 unsigned char quotedchar, quotedchartmp = 0, tmp; | |
43 unsigned char escchar = '%'; /* the character we use to begin %AB sequences
*/ | |
44 static char *kwlist[] = {"s", "escchar", NULL}; | |
45 int state = STATE_INITIAL, length; | |
46 PyObject *output, *str; | |
47 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s#|c:unquote", kwlist, &s, &
length, &escchar)) { | |
48 return NULL; | |
49 } | |
50 /* output = cStringIO() */ | |
51 output = PycStringIO->NewOutput(length); | |
52 if (output == NULL) { | |
53 return NULL; | |
54 } | |
55 end = s + length; | |
56 s = s - 1; | |
57 while ((++s) < end) { | |
58 switch(state) { | |
59 case STATE_INITIAL: | |
60 if (*s == escchar) { | |
61 state = STATE_PERCENT; | |
62 } else { | |
63 r = s - 1; | |
64 while (*(++r) != escchar && r < end); | |
65 OUTPUTCHAR(s, r-s); | |
66 s = r-1; | |
67 } | |
68 break; | |
69 case STATE_PERCENT: | |
70 if ((quotedchartmp = ishexdigit(*s)) != NOT_HEXDIGIT) { | |
71 tmp = *s; | |
72 state = STATE_HEXDIGIT; | |
73 } else { | |
74 state = STATE_INITIAL; | |
75 OUTPUTCHAR(&escchar, 1); | |
76 s--; | |
77 } | |
78 break; | |
79 case STATE_HEXDIGIT: | |
80 state = STATE_INITIAL; | |
81 if ((quotedchar = ishexdigit(*s)) != NOT_HEXDIGIT) { | |
82 quotedchar |= (quotedchartmp << 4); | |
83 OUTPUTCHAR("edchar, 1); | |
84 } else { | |
85 OUTPUTCHAR(&escchar, 1); | |
86 s -= 2; | |
87 } | |
88 break; | |
89 } | |
90 } | |
91 switch(state) { | |
92 case STATE_PERCENT: | |
93 OUTPUTCHAR(&escchar, 1); | |
94 break; | |
95 case STATE_HEXDIGIT: | |
96 OUTPUTCHAR(&escchar, 1); | |
97 OUTPUTCHAR(&tmp, 1); | |
98 break; | |
99 } | |
100 | |
101 /* return output.getvalue() */ | |
102 str = PycStringIO->cgetvalue(output); | |
103 Py_DECREF(output); | |
104 return str; | |
105 } | |
106 | |
107 static PyMethodDef _c_urlarg_methods[] = { | |
108 {"unquote", (PyCFunction)unquote, METH_VARARGS|METH_KEYWORDS}, | |
109 {NULL, NULL} /* sentinel */ | |
110 }; | |
111 | |
112 DL_EXPORT(void) init_c_urlarg(void) | |
113 { | |
114 PyObject* m; | |
115 PyObject* d; | |
116 unsigned char i; | |
117 | |
118 PycString_IMPORT; | |
119 m = Py_InitModule("_c_urlarg", _c_urlarg_methods); | |
120 d = PyModule_GetDict(m); | |
121 | |
122 /* add our base exception class */ | |
123 UrlargError = PyErr_NewException("urlarg.UrlargError", PyExc_Exception, NULL
); | |
124 PyDict_SetItemString(d, "UrlargError", UrlargError); | |
125 | |
126 /* initialize hexdigits */ | |
127 for(i = 0; i < 255; i++) { | |
128 hexdigits[i] = NOT_HEXDIGIT; | |
129 } | |
130 hexdigits[255] = NOT_HEXDIGIT; | |
131 for(i = '0'; i <= '9'; i++) { | |
132 hexdigits[i] = i - '0'; | |
133 } | |
134 for(i = 'a'; i <= 'f'; i++) { | |
135 hexdigits[i] = 10 + (i - 'a'); | |
136 } | |
137 for(i = 'A'; i <= 'F'; i++) { | |
138 hexdigits[i] = 10 + (i - 'A'); | |
139 } | |
140 /* Check for errors */ | |
141 if (PyErr_Occurred()) { | |
142 PyErr_Print(); | |
143 Py_FatalError("can't initialize module _c_urlarg"); | |
144 } | |
145 } | |
146 | |
OLD | NEW |