_speedups.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. #include <Python.h>
  2. static PyObject* markup;
  3. static int
  4. init_constants(void)
  5. {
  6. PyObject *module;
  7. /* import markup type so that we can mark the return value */
  8. module = PyImport_ImportModule("markupsafe");
  9. if (!module)
  10. return 0;
  11. markup = PyObject_GetAttrString(module, "Markup");
  12. Py_DECREF(module);
  13. return 1;
  14. }
  15. #define GET_DELTA(inp, inp_end, delta) \
  16. while (inp < inp_end) { \
  17. switch (*inp++) { \
  18. case '"': \
  19. case '\'': \
  20. case '&': \
  21. delta += 4; \
  22. break; \
  23. case '<': \
  24. case '>': \
  25. delta += 3; \
  26. break; \
  27. } \
  28. }
  29. #define DO_ESCAPE(inp, inp_end, outp) \
  30. { \
  31. Py_ssize_t ncopy = 0; \
  32. while (inp < inp_end) { \
  33. switch (*inp) { \
  34. case '"': \
  35. memcpy(outp, inp-ncopy, sizeof(*outp)*ncopy); \
  36. outp += ncopy; ncopy = 0; \
  37. *outp++ = '&'; \
  38. *outp++ = '#'; \
  39. *outp++ = '3'; \
  40. *outp++ = '4'; \
  41. *outp++ = ';'; \
  42. break; \
  43. case '\'': \
  44. memcpy(outp, inp-ncopy, sizeof(*outp)*ncopy); \
  45. outp += ncopy; ncopy = 0; \
  46. *outp++ = '&'; \
  47. *outp++ = '#'; \
  48. *outp++ = '3'; \
  49. *outp++ = '9'; \
  50. *outp++ = ';'; \
  51. break; \
  52. case '&': \
  53. memcpy(outp, inp-ncopy, sizeof(*outp)*ncopy); \
  54. outp += ncopy; ncopy = 0; \
  55. *outp++ = '&'; \
  56. *outp++ = 'a'; \
  57. *outp++ = 'm'; \
  58. *outp++ = 'p'; \
  59. *outp++ = ';'; \
  60. break; \
  61. case '<': \
  62. memcpy(outp, inp-ncopy, sizeof(*outp)*ncopy); \
  63. outp += ncopy; ncopy = 0; \
  64. *outp++ = '&'; \
  65. *outp++ = 'l'; \
  66. *outp++ = 't'; \
  67. *outp++ = ';'; \
  68. break; \
  69. case '>': \
  70. memcpy(outp, inp-ncopy, sizeof(*outp)*ncopy); \
  71. outp += ncopy; ncopy = 0; \
  72. *outp++ = '&'; \
  73. *outp++ = 'g'; \
  74. *outp++ = 't'; \
  75. *outp++ = ';'; \
  76. break; \
  77. default: \
  78. ncopy++; \
  79. } \
  80. inp++; \
  81. } \
  82. memcpy(outp, inp-ncopy, sizeof(*outp)*ncopy); \
  83. }
  84. static PyObject*
  85. escape_unicode_kind1(PyUnicodeObject *in)
  86. {
  87. Py_UCS1 *inp = PyUnicode_1BYTE_DATA(in);
  88. Py_UCS1 *inp_end = inp + PyUnicode_GET_LENGTH(in);
  89. Py_UCS1 *outp;
  90. PyObject *out;
  91. Py_ssize_t delta = 0;
  92. GET_DELTA(inp, inp_end, delta);
  93. if (!delta) {
  94. Py_INCREF(in);
  95. return (PyObject*)in;
  96. }
  97. out = PyUnicode_New(PyUnicode_GET_LENGTH(in) + delta,
  98. PyUnicode_IS_ASCII(in) ? 127 : 255);
  99. if (!out)
  100. return NULL;
  101. inp = PyUnicode_1BYTE_DATA(in);
  102. outp = PyUnicode_1BYTE_DATA(out);
  103. DO_ESCAPE(inp, inp_end, outp);
  104. return out;
  105. }
  106. static PyObject*
  107. escape_unicode_kind2(PyUnicodeObject *in)
  108. {
  109. Py_UCS2 *inp = PyUnicode_2BYTE_DATA(in);
  110. Py_UCS2 *inp_end = inp + PyUnicode_GET_LENGTH(in);
  111. Py_UCS2 *outp;
  112. PyObject *out;
  113. Py_ssize_t delta = 0;
  114. GET_DELTA(inp, inp_end, delta);
  115. if (!delta) {
  116. Py_INCREF(in);
  117. return (PyObject*)in;
  118. }
  119. out = PyUnicode_New(PyUnicode_GET_LENGTH(in) + delta, 65535);
  120. if (!out)
  121. return NULL;
  122. inp = PyUnicode_2BYTE_DATA(in);
  123. outp = PyUnicode_2BYTE_DATA(out);
  124. DO_ESCAPE(inp, inp_end, outp);
  125. return out;
  126. }
  127. static PyObject*
  128. escape_unicode_kind4(PyUnicodeObject *in)
  129. {
  130. Py_UCS4 *inp = PyUnicode_4BYTE_DATA(in);
  131. Py_UCS4 *inp_end = inp + PyUnicode_GET_LENGTH(in);
  132. Py_UCS4 *outp;
  133. PyObject *out;
  134. Py_ssize_t delta = 0;
  135. GET_DELTA(inp, inp_end, delta);
  136. if (!delta) {
  137. Py_INCREF(in);
  138. return (PyObject*)in;
  139. }
  140. out = PyUnicode_New(PyUnicode_GET_LENGTH(in) + delta, 1114111);
  141. if (!out)
  142. return NULL;
  143. inp = PyUnicode_4BYTE_DATA(in);
  144. outp = PyUnicode_4BYTE_DATA(out);
  145. DO_ESCAPE(inp, inp_end, outp);
  146. return out;
  147. }
  148. static PyObject*
  149. escape_unicode(PyUnicodeObject *in)
  150. {
  151. if (PyUnicode_READY(in))
  152. return NULL;
  153. switch (PyUnicode_KIND(in)) {
  154. case PyUnicode_1BYTE_KIND:
  155. return escape_unicode_kind1(in);
  156. case PyUnicode_2BYTE_KIND:
  157. return escape_unicode_kind2(in);
  158. case PyUnicode_4BYTE_KIND:
  159. return escape_unicode_kind4(in);
  160. }
  161. assert(0); /* shouldn't happen */
  162. return NULL;
  163. }
  164. static PyObject*
  165. escape(PyObject *self, PyObject *text)
  166. {
  167. static PyObject *id_html;
  168. PyObject *s = NULL, *rv = NULL, *html;
  169. if (id_html == NULL) {
  170. id_html = PyUnicode_InternFromString("__html__");
  171. if (id_html == NULL) {
  172. return NULL;
  173. }
  174. }
  175. /* we don't have to escape integers, bools or floats */
  176. if (PyLong_CheckExact(text) ||
  177. PyFloat_CheckExact(text) || PyBool_Check(text) ||
  178. text == Py_None)
  179. return PyObject_CallFunctionObjArgs(markup, text, NULL);
  180. /* if the object has an __html__ method that performs the escaping */
  181. html = PyObject_GetAttr(text ,id_html);
  182. if (html) {
  183. s = PyObject_CallObject(html, NULL);
  184. Py_DECREF(html);
  185. if (s == NULL) {
  186. return NULL;
  187. }
  188. /* Convert to Markup object */
  189. rv = PyObject_CallFunctionObjArgs(markup, (PyObject*)s, NULL);
  190. Py_DECREF(s);
  191. return rv;
  192. }
  193. /* otherwise make the object unicode if it isn't, then escape */
  194. PyErr_Clear();
  195. if (!PyUnicode_Check(text)) {
  196. PyObject *unicode = PyObject_Str(text);
  197. if (!unicode)
  198. return NULL;
  199. s = escape_unicode((PyUnicodeObject*)unicode);
  200. Py_DECREF(unicode);
  201. }
  202. else
  203. s = escape_unicode((PyUnicodeObject*)text);
  204. /* convert the unicode string into a markup object. */
  205. rv = PyObject_CallFunctionObjArgs(markup, (PyObject*)s, NULL);
  206. Py_DECREF(s);
  207. return rv;
  208. }
  209. static PyObject*
  210. escape_silent(PyObject *self, PyObject *text)
  211. {
  212. if (text != Py_None)
  213. return escape(self, text);
  214. return PyObject_CallFunctionObjArgs(markup, NULL);
  215. }
  216. static PyObject*
  217. soft_str(PyObject *self, PyObject *s)
  218. {
  219. if (!PyUnicode_Check(s))
  220. return PyObject_Str(s);
  221. Py_INCREF(s);
  222. return s;
  223. }
  224. static PyMethodDef module_methods[] = {
  225. {
  226. "escape",
  227. (PyCFunction)escape,
  228. METH_O,
  229. "Replace the characters ``&``, ``<``, ``>``, ``'``, and ``\"`` in"
  230. " the string with HTML-safe sequences. Use this if you need to display"
  231. " text that might contain such characters in HTML.\n\n"
  232. "If the object has an ``__html__`` method, it is called and the"
  233. " return value is assumed to already be safe for HTML.\n\n"
  234. ":param s: An object to be converted to a string and escaped.\n"
  235. ":return: A :class:`Markup` string with the escaped text.\n"
  236. },
  237. {
  238. "escape_silent",
  239. (PyCFunction)escape_silent,
  240. METH_O,
  241. "Like :func:`escape` but treats ``None`` as the empty string."
  242. " Useful with optional values, as otherwise you get the string"
  243. " ``'None'`` when the value is ``None``.\n\n"
  244. ">>> escape(None)\n"
  245. "Markup('None')\n"
  246. ">>> escape_silent(None)\n"
  247. "Markup('')\n"
  248. },
  249. {
  250. "soft_str",
  251. (PyCFunction)soft_str,
  252. METH_O,
  253. "Convert an object to a string if it isn't already. This preserves"
  254. " a :class:`Markup` string rather than converting it back to a basic"
  255. " string, so it will still be marked as safe and won't be escaped"
  256. " again.\n\n"
  257. ">>> value = escape(\"<User 1>\")\n"
  258. ">>> value\n"
  259. "Markup('&lt;User 1&gt;')\n"
  260. ">>> escape(str(value))\n"
  261. "Markup('&amp;lt;User 1&amp;gt;')\n"
  262. ">>> escape(soft_str(value))\n"
  263. "Markup('&lt;User 1&gt;')\n"
  264. },
  265. {NULL, NULL, 0, NULL} /* Sentinel */
  266. };
  267. static struct PyModuleDef module_definition = {
  268. PyModuleDef_HEAD_INIT,
  269. "markupsafe._speedups",
  270. NULL,
  271. -1,
  272. module_methods,
  273. NULL,
  274. NULL,
  275. NULL,
  276. NULL
  277. };
  278. PyMODINIT_FUNC
  279. PyInit__speedups(void)
  280. {
  281. if (!init_constants())
  282. return NULL;
  283. return PyModule_Create(&module_definition);
  284. }