You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

lua_text.c 35KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649
  1. /*-
  2. * Copyright 2019 Vsevolod Stakhov
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "lua_common.h"
  17. #include "libcryptobox/cryptobox.h"
  18. #include "contrib/fastutf8/fastutf8.h"
  19. #include "unix-std.h"
  20. /***
  21. * @module rspamd_text
  22. * This module provides access to opaque text structures used widely to prevent
  23. * copying between Lua and C for various concerns: performance, security etc...
  24. *
  25. * You can convert rspamd_text into string but it will copy data.
  26. */
  27. /***
  28. * @function rspamd_text.fromstring(str)
  29. * Creates rspamd_text from Lua string (copied to the text)
  30. * @param {string} str string to use
  31. * @return {rspamd_text} resulting text
  32. */
  33. LUA_FUNCTION_DEF (text, fromstring);
  34. /***
  35. * @function rspamd_text.null()
  36. * Creates rspamd_text with NULL pointer for testing purposes
  37. * @param {string} str string to use
  38. * @return {rspamd_text} resulting text
  39. */
  40. LUA_FUNCTION_DEF (text, null);
  41. /***
  42. * @function rspamd_text.randombytes(nbytes)
  43. * Creates rspamd_text with random bytes inside (raw bytes)
  44. * @param {number} nbytes number of random bytes generated
  45. * @return {rspamd_text} random bytes text
  46. */
  47. LUA_FUNCTION_DEF (text, randombytes);
  48. /***
  49. * @function rspamd_text.fromtable(tbl[, delim])
  50. * Same as `table.concat` but generates rspamd_text instead of the Lua string
  51. * @param {table} tbl table to use
  52. * @param {string} delim optional delimiter
  53. * @return {rspamd_text} resulting text
  54. */
  55. LUA_FUNCTION_DEF (text, fromtable);
  56. /***
  57. * @method rspamd_text:byte(pos[, pos2])
  58. * Returns a byte at the position `pos` or bytes from `pos` to `pos2` if specified
  59. * @param {integer} pos index
  60. * @param {integer} pos2 index
  61. * @return {integer} byte at the position `pos` or varargs of bytes
  62. */
  63. LUA_FUNCTION_DEF (text, byte);
  64. /***
  65. * @method rspamd_text:len()
  66. * Returns length of a string
  67. * @return {number} length of string in **bytes**
  68. */
  69. LUA_FUNCTION_DEF (text, len);
  70. /***
  71. * @method rspamd_text:str()
  72. * Converts text to string by copying its content
  73. * @return {string} copy of text as Lua string
  74. */
  75. LUA_FUNCTION_DEF (text, str);
  76. /***
  77. * @method rspamd_text:ptr()
  78. * Converts text to lightuserdata
  79. * @return {lightuserdata} pointer value of rspamd_text
  80. */
  81. LUA_FUNCTION_DEF (text, ptr);
  82. /***
  83. * @method rspamd_text:save_in_file(fname[, mode])
  84. * Saves text in file
  85. * @return {boolean} true if save has been completed
  86. */
  87. LUA_FUNCTION_DEF (text, save_in_file);
  88. /***
  89. * @method rspamd_text:span(start[, len])
  90. * Returns a span for lua_text starting at pos [start] (1 indexed) and with
  91. * length `len` (or to the end of the text)
  92. * @param {integer} start start index
  93. * @param {integer} len length of span
  94. * @return {rspamd_text} new rspamd_text with span (must be careful when using with owned texts...)
  95. */
  96. LUA_FUNCTION_DEF (text, span);
  97. /***
  98. * @method rspamd_text:sub(start[, len])
  99. * Returns a substrin for lua_text similar to string.sub from Lua
  100. * @return {rspamd_text} new rspamd_text with span (must be careful when using with owned texts...)
  101. */
  102. LUA_FUNCTION_DEF (text, sub);
  103. /***
  104. * @method rspamd_text:lines([stringify])
  105. * Returns an iter over all lines as rspamd_text objects or as strings if `stringify` is true
  106. * @param {boolean} stringify stringify lines
  107. * @return {iterator} iterator triplet
  108. */
  109. LUA_FUNCTION_DEF (text, lines);
  110. /***
  111. * @method rspamd_text:split(regexp, [stringify])
  112. * Returns an iter over all encounters of the specific regexp as rspamd_text objects or as strings if `stringify` is true
  113. * @param {rspamd_regexp} regexp regexp (pcre syntax) used for splitting
  114. * @param {boolean} stringify stringify lines
  115. * @return {iterator} iterator triplet
  116. */
  117. LUA_FUNCTION_DEF (text, split);
  118. /***
  119. * @method rspamd_text:at(pos)
  120. * Returns a byte at the position `pos`
  121. * @param {integer} pos index
  122. * @return {integer} byte at the position `pos` or nil if pos out of bound
  123. */
  124. LUA_FUNCTION_DEF (text, at);
  125. /***
  126. * @method rspamd_text:memchr(chr, [reverse])
  127. * Returns the first or the last position of the character `chr` in the text or
  128. * -1 in case if a character has not been found. Indexes start from `1`
  129. * @param {string/number} chr character or a character code to find
  130. * @param {boolean} reverse last character if `true`
  131. * @return {integer} position of the character or `-1`
  132. */
  133. LUA_FUNCTION_DEF (text, memchr);
  134. /***
  135. * @method rspamd_text:bytes()
  136. * Converts text to an array of bytes
  137. * @return {table|integer} bytes in the array (as unsigned char)
  138. */
  139. LUA_FUNCTION_DEF (text, bytes);
  140. /***
  141. * @method rspamd_text:lower([is_utf, [inplace]])
  142. * Return a new text with lowercased characters, if is_utf is true then Rspamd applies utf8 lowercase
  143. * @param {boolean} is_utf apply utf8 lowercase
  144. * @param {boolean} inplace lowercase the original text
  145. * @return rspamd_text} new rspamd_text (or the original text if inplace) with lowercased letters
  146. */
  147. LUA_FUNCTION_DEF (text, lower);
  148. LUA_FUNCTION_DEF (text, take_ownership);
  149. /***
  150. * @method rspamd_text:exclude_chars(set_to_exclude, [always_copy])
  151. * Returns a text (if owned, then the original text is modified, if not, then it is copied and owned)
  152. * where all chars from `set_to_exclude` are removed
  153. * Patterns supported:
  154. *
  155. * - %s - all space characters
  156. * - %n - all newline characters
  157. * - %c - all control characters (it includes 8bit characters and spaces)
  158. * - %8 - all 8 bit characters
  159. * - %% - just a percent character
  160. *
  161. * @param {string} set_to_exclude characters to exclude
  162. * @param {boolean} always_copy always copy the source text
  163. * @return {tspamd_text} modified or copied text
  164. */
  165. LUA_FUNCTION_DEF (text, exclude_chars);
  166. /***
  167. * @method rspamd_text:oneline([always_copy])
  168. * Returns a text (if owned, then the original text is modified, if not, then it is copied and owned)
  169. * where the following transformations are made:
  170. * - All spaces sequences are replaced with a single space
  171. * - All newlines sequences are replaced with a single space
  172. * - Trailing and leading spaces are removed
  173. * - Control characters are excluded
  174. * - UTF8 sequences are normalised
  175. *
  176. * @param {boolean} always_copy always copy the source text
  177. * @return {tspamd_text} modified or copied text
  178. */
  179. LUA_FUNCTION_DEF (text, oneline);
  180. /***
  181. * @method rspamd_text:base32([b32type])
  182. * Returns a text encoded in base32 (new rspamd_text is allocated)
  183. *
  184. * @param {string} b32type base32 type (default, bleach, rfc)
  185. * @return {tspamd_text} new text encoded in base32
  186. */
  187. LUA_FUNCTION_DEF (text, base32);
  188. /***
  189. * @method rspamd_text:base64([line_length, [nline, [fold]]])
  190. * Returns a text encoded in base64 (new rspamd_text is allocated)
  191. *
  192. * @param {number} line_length return text splited with newlines up to this attribute
  193. * @param {string} nline newline type: `cr`, `lf`, `crlf`
  194. * @param {boolean} fold use folding when splitting into lines (false by default)
  195. * @return {tspamd_text} new text encoded in base64
  196. */
  197. LUA_FUNCTION_DEF (text, base64);
  198. /***
  199. * @method rspamd_text:hex()
  200. * Returns a text encoded in hex (new rspamd_text is allocated)
  201. *
  202. * @return {tspamd_text} new text encoded in hex
  203. */
  204. LUA_FUNCTION_DEF (text, hex);
  205. LUA_FUNCTION_DEF (text, gc);
  206. LUA_FUNCTION_DEF (text, eq);
  207. LUA_FUNCTION_DEF (text, lt);
  208. LUA_FUNCTION_DEF (text, concat);
  209. static const struct luaL_reg textlib_f[] = {
  210. LUA_INTERFACE_DEF (text, fromstring),
  211. {"from_string", lua_text_fromstring},
  212. LUA_INTERFACE_DEF (text, fromtable),
  213. {"from_table", lua_text_fromtable},
  214. LUA_INTERFACE_DEF (text, null),
  215. LUA_INTERFACE_DEF (text, randombytes),
  216. {NULL, NULL}
  217. };
  218. static const struct luaL_reg textlib_m[] = {
  219. LUA_INTERFACE_DEF (text, len),
  220. LUA_INTERFACE_DEF (text, str),
  221. LUA_INTERFACE_DEF (text, ptr),
  222. LUA_INTERFACE_DEF (text, take_ownership),
  223. LUA_INTERFACE_DEF (text, save_in_file),
  224. LUA_INTERFACE_DEF (text, span),
  225. LUA_INTERFACE_DEF (text, sub),
  226. LUA_INTERFACE_DEF (text, lines),
  227. LUA_INTERFACE_DEF (text, split),
  228. LUA_INTERFACE_DEF (text, at),
  229. LUA_INTERFACE_DEF (text, memchr),
  230. LUA_INTERFACE_DEF (text, byte),
  231. LUA_INTERFACE_DEF (text, bytes),
  232. LUA_INTERFACE_DEF (text, lower),
  233. LUA_INTERFACE_DEF (text, exclude_chars),
  234. LUA_INTERFACE_DEF (text, oneline),
  235. LUA_INTERFACE_DEF (text, base32),
  236. LUA_INTERFACE_DEF (text, base64),
  237. LUA_INTERFACE_DEF (text, hex),
  238. {"write", lua_text_save_in_file},
  239. {"__len", lua_text_len},
  240. {"__tostring", lua_text_str},
  241. {"__gc", lua_text_gc},
  242. {"__eq", lua_text_eq},
  243. {"__lt", lua_text_lt},
  244. {"__concat", lua_text_concat},
  245. {NULL, NULL}
  246. };
  247. struct rspamd_lua_text *
  248. lua_check_text (lua_State * L, gint pos)
  249. {
  250. void *ud = rspamd_lua_check_udata (L, pos, "rspamd{text}");
  251. luaL_argcheck (L, ud != NULL, pos, "'text' expected");
  252. return ud ? (struct rspamd_lua_text *)ud : NULL;
  253. }
  254. struct rspamd_lua_text *
  255. lua_check_text_or_string (lua_State * L, gint pos)
  256. {
  257. gint pos_type = lua_type (L, pos);
  258. if (pos_type == LUA_TUSERDATA) {
  259. void *ud = rspamd_lua_check_udata (L, pos, "rspamd{text}");
  260. luaL_argcheck (L, ud != NULL, pos, "'text' expected");
  261. return ud ? (struct rspamd_lua_text *) ud : NULL;
  262. }
  263. else if (pos_type == LUA_TSTRING) {
  264. /* Fake static lua_text */
  265. static struct rspamd_lua_text fake_text;
  266. gsize len;
  267. fake_text.start = lua_tolstring (L, pos, &len);
  268. if (len >= G_MAXUINT) {
  269. return NULL;
  270. }
  271. fake_text.len = len;
  272. fake_text.flags = RSPAMD_TEXT_FLAG_FAKE;
  273. return &fake_text;
  274. }
  275. return NULL;
  276. }
  277. struct rspamd_lua_text *
  278. lua_new_text (lua_State *L, const gchar *start, gsize len, gboolean own)
  279. {
  280. struct rspamd_lua_text *t;
  281. t = lua_newuserdata (L, sizeof (*t));
  282. t->flags = 0;
  283. if (own) {
  284. gchar *storage;
  285. if (len > 0) {
  286. storage = g_malloc (len);
  287. if (start != NULL) {
  288. memcpy (storage, start, len);
  289. }
  290. t->start = storage;
  291. t->flags = RSPAMD_TEXT_FLAG_OWN;
  292. }
  293. else {
  294. t->start = "";
  295. }
  296. }
  297. else {
  298. t->start = start;
  299. }
  300. t->len = len;
  301. rspamd_lua_setclass (L, "rspamd{text}", -1);
  302. return t;
  303. }
  304. static gint
  305. lua_text_fromstring (lua_State *L)
  306. {
  307. LUA_TRACE_POINT;
  308. const gchar *str;
  309. gsize l = 0;
  310. gboolean transparent = FALSE;
  311. str = luaL_checklstring (L, 1, &l);
  312. if (str) {
  313. if (lua_isboolean (L, 2)) {
  314. transparent = lua_toboolean (L, 2);
  315. }
  316. lua_new_text (L, str, l, !transparent);
  317. }
  318. else {
  319. return luaL_error (L, "invalid arguments");
  320. }
  321. return 1;
  322. }
  323. static gint
  324. lua_text_null (lua_State *L)
  325. {
  326. LUA_TRACE_POINT;
  327. lua_new_text (L, NULL, 0, false);
  328. return 1;
  329. }
  330. static gint
  331. lua_text_randombytes (lua_State *L)
  332. {
  333. LUA_TRACE_POINT;
  334. guint nbytes = luaL_checkinteger (L, 1);
  335. struct rspamd_lua_text *out;
  336. out = lua_new_text (L, NULL, nbytes, TRUE);
  337. randombytes_buf ((char *)out->start, nbytes);
  338. out->len = nbytes;
  339. return 1;
  340. }
  341. #define MAX_REC 10
  342. static void
  343. lua_text_tbl_length (lua_State *L, gsize dlen, gsize *dest, guint rec)
  344. {
  345. gsize tblen, stlen;
  346. struct rspamd_lua_text *elt;
  347. if (rec > MAX_REC) {
  348. luaL_error (L, "lua_text_tbl_length: recursion limit exceeded");
  349. return;
  350. }
  351. tblen = rspamd_lua_table_size (L, -1);
  352. for (gsize i = 0; i < tblen; i ++) {
  353. lua_rawgeti (L, -1, i + 1);
  354. if (lua_type (L, -1) == LUA_TSTRING) {
  355. #if LUA_VERSION_NUM >= 502
  356. stlen = lua_rawlen (L, -1);
  357. #else
  358. stlen = lua_objlen (L, -1);
  359. #endif
  360. (*dest) += stlen;
  361. }
  362. else if (lua_type (L, -1) == LUA_TUSERDATA){
  363. elt = (struct rspamd_lua_text *)lua_touserdata (L, -1);
  364. if (elt) {
  365. (*dest) += elt->len;
  366. }
  367. }
  368. else if (lua_type (L, -1) == LUA_TTABLE) {
  369. lua_text_tbl_length (L, dlen, dest, rec + 1);
  370. }
  371. if (i != tblen - 1) {
  372. (*dest) += dlen;
  373. }
  374. lua_pop (L, 1);
  375. }
  376. }
  377. static void
  378. lua_text_tbl_append (lua_State *L,
  379. const gchar *delim,
  380. gsize dlen,
  381. gchar **dest,
  382. guint rec)
  383. {
  384. const gchar *st;
  385. gsize tblen, stlen;
  386. struct rspamd_lua_text *elt;
  387. if (rec > MAX_REC) {
  388. luaL_error (L, "lua_text_tbl_length: recursion limit exceeded");
  389. return;
  390. }
  391. tblen = rspamd_lua_table_size (L, -1);
  392. for (guint i = 0; i < tblen; i ++) {
  393. lua_rawgeti (L, -1, i + 1);
  394. if (lua_type (L, -1) == LUA_TSTRING) {
  395. st = lua_tolstring (L, -1, &stlen);
  396. memcpy ((*dest), st, stlen);
  397. (*dest) += stlen;
  398. }
  399. else if (lua_type (L, -1) == LUA_TUSERDATA){
  400. elt = (struct rspamd_lua_text *)lua_touserdata (L, -1);
  401. if (elt) {
  402. memcpy ((*dest), elt->start, elt->len);
  403. (*dest) += elt->len;
  404. }
  405. }
  406. else if (lua_type (L, -1) == LUA_TTABLE) {
  407. lua_text_tbl_append (L, delim, dlen, dest, rec + 1);
  408. }
  409. if (dlen && i != tblen - 1) {
  410. memcpy ((*dest), delim, dlen);
  411. (*dest) += dlen;
  412. }
  413. lua_pop (L, 1);
  414. }
  415. }
  416. static gint
  417. lua_text_fromtable (lua_State *L)
  418. {
  419. LUA_TRACE_POINT;
  420. const gchar *delim = "";
  421. struct rspamd_lua_text *t;
  422. gsize textlen = 0, dlen, oldtop = lua_gettop (L);
  423. gchar *dest;
  424. if (!lua_istable (L, 1)) {
  425. return luaL_error (L, "invalid arguments");
  426. }
  427. if (lua_type (L, 2) == LUA_TSTRING) {
  428. delim = lua_tolstring (L, 2, &dlen);
  429. }
  430. else {
  431. dlen = 0;
  432. }
  433. /* Calculate length needed */
  434. lua_pushvalue (L, 1);
  435. lua_text_tbl_length (L, dlen, &textlen, 0);
  436. lua_pop (L, 1);
  437. /* Allocate new text */
  438. t = lua_newuserdata (L, sizeof (*t));
  439. dest = g_malloc (textlen);
  440. t->start = dest;
  441. t->len = textlen;
  442. t->flags = RSPAMD_TEXT_FLAG_OWN;
  443. rspamd_lua_setclass (L, "rspamd{text}", -1);
  444. lua_pushvalue (L, 1);
  445. lua_text_tbl_append (L, delim, dlen, &dest, 0);
  446. lua_pop (L, 1); /* Table arg */
  447. gint newtop = lua_gettop (L);
  448. g_assert ( newtop== oldtop + 1);
  449. return 1;
  450. }
  451. static gint
  452. lua_text_len (lua_State *L)
  453. {
  454. LUA_TRACE_POINT;
  455. struct rspamd_lua_text *t = lua_check_text (L, 1);
  456. gsize l = 0;
  457. if (t != NULL) {
  458. l = t->len;
  459. }
  460. else {
  461. return luaL_error (L, "invalid arguments");
  462. }
  463. lua_pushinteger (L, l);
  464. return 1;
  465. }
  466. static gint
  467. lua_text_str (lua_State *L)
  468. {
  469. LUA_TRACE_POINT;
  470. struct rspamd_lua_text *t = lua_check_text (L, 1);
  471. if (t != NULL) {
  472. lua_pushlstring (L, t->start, t->len);
  473. }
  474. else {
  475. return luaL_error (L, "invalid arguments");
  476. }
  477. return 1;
  478. }
  479. static gint
  480. lua_text_ptr (lua_State *L)
  481. {
  482. LUA_TRACE_POINT;
  483. struct rspamd_lua_text *t = lua_check_text (L, 1);
  484. if (t != NULL) {
  485. lua_pushlightuserdata (L, (gpointer)t->start);
  486. }
  487. else {
  488. return luaL_error (L, "invalid arguments");
  489. }
  490. return 1;
  491. }
  492. static gint
  493. lua_text_take_ownership (lua_State *L)
  494. {
  495. LUA_TRACE_POINT;
  496. struct rspamd_lua_text *t = lua_check_text (L, 1);
  497. gchar *dest;
  498. if (t != NULL) {
  499. if (t->flags & RSPAMD_TEXT_FLAG_OWN) {
  500. /* We already own it */
  501. lua_pushboolean (L, true);
  502. }
  503. else {
  504. dest = g_malloc (t->len);
  505. memcpy (dest, t->start, t->len);
  506. t->start = dest;
  507. t->flags |= RSPAMD_TEXT_FLAG_OWN;
  508. lua_pushboolean (L, true);
  509. }
  510. }
  511. else {
  512. return luaL_error (L, "invalid arguments");
  513. }
  514. return 1;
  515. }
  516. static gint
  517. lua_text_span (lua_State *L)
  518. {
  519. LUA_TRACE_POINT;
  520. struct rspamd_lua_text *t = lua_check_text (L, 1);
  521. gint64 start = lua_tointeger (L, 2), len = -1;
  522. if (t && start >= 1 && start <= t->len) {
  523. if (lua_isnumber (L, 3)) {
  524. len = lua_tonumber (L, 3);
  525. }
  526. if (len == -1) {
  527. len = t->len - (start - 1);
  528. }
  529. if (len < 0 || (len > (t->len - (start - 1)))) {
  530. return luaL_error (L, "invalid length");
  531. }
  532. lua_new_text (L, t->start + (start - 1), len, FALSE);
  533. }
  534. else {
  535. if (!t) {
  536. return luaL_error (L, "invalid arguments, text required");
  537. }
  538. else {
  539. return luaL_error (L, "invalid arguments: start offset %d "
  540. "is larger than text len %d", (int)start, (int)t->len);
  541. }
  542. }
  543. return 1;
  544. }
  545. /* Helpers to behave exactly as Lua does */
  546. static inline gsize
  547. relative_pos_start (gint pos, gsize len)
  548. {
  549. if (pos > 0) {
  550. return pos;
  551. }
  552. else if (pos == 0) {
  553. return 1;
  554. }
  555. else if (pos < -((gint) len)) {
  556. return 1;
  557. }
  558. /* Negative pos inside str */
  559. return len + ((gsize)pos) + 1;
  560. }
  561. static inline gsize
  562. relative_pos_end (gint pos, gsize len)
  563. {
  564. if (pos > (gint)len) {
  565. return len;
  566. }
  567. else if (pos >= 0) {
  568. return (size_t) pos;
  569. }
  570. else if (pos < -((gint)len)) {
  571. return 0;
  572. }
  573. return len + ((gsize)pos) + 1;
  574. }
  575. static gint
  576. lua_text_sub (lua_State *L)
  577. {
  578. LUA_TRACE_POINT;
  579. struct rspamd_lua_text *t = lua_check_text (L, 1);
  580. if (t) {
  581. size_t start = relative_pos_start (luaL_checkinteger (L, 2),
  582. t->len);
  583. size_t end = relative_pos_end (luaL_optinteger (L, 3, -1),
  584. t->len);
  585. if (start <= end) {
  586. lua_new_text (L, t->start + (start - 1),
  587. (end - start) + 1, FALSE);
  588. }
  589. else {
  590. lua_new_text (L, "", 0, TRUE);
  591. }
  592. }
  593. else {
  594. return luaL_error (L, "invalid arguments");
  595. }
  596. return 1;
  597. }
  598. static gint64
  599. rspamd_lua_text_push_line (lua_State *L,
  600. struct rspamd_lua_text *t,
  601. gint64 start_offset,
  602. const gchar *sep_pos,
  603. gboolean stringify)
  604. {
  605. const gchar *start;
  606. gsize len;
  607. gint64 ret;
  608. start = t->start + start_offset;
  609. len = sep_pos ? (sep_pos - start) : (t->len - start_offset);
  610. ret = start_offset + len;
  611. /* Trim line */
  612. while (len > 0) {
  613. if (start[len - 1] == '\r' || start[len - 1] == '\n') {
  614. len --;
  615. }
  616. else {
  617. break;
  618. }
  619. }
  620. if (stringify) {
  621. lua_pushlstring (L, start, len);
  622. }
  623. else {
  624. struct rspamd_lua_text *ntext;
  625. ntext = lua_newuserdata (L, sizeof (*ntext));
  626. rspamd_lua_setclass (L, "rspamd{text}", -1);
  627. ntext->start = start;
  628. ntext->len = len;
  629. ntext->flags = 0; /* Not own as it must be owned by a top object */
  630. }
  631. return ret;
  632. }
  633. static gint
  634. rspamd_lua_text_readline (lua_State *L)
  635. {
  636. struct rspamd_lua_text *t = lua_touserdata (L, lua_upvalueindex (1));
  637. gboolean stringify = lua_toboolean (L, lua_upvalueindex (2));
  638. gint64 pos = lua_tointeger (L, lua_upvalueindex (3));
  639. if (pos < 0) {
  640. return luaL_error (L, "invalid pos: %d", (gint)pos);
  641. }
  642. if (pos >= t->len) {
  643. /* We are done */
  644. return 0;
  645. }
  646. const gchar *sep_pos;
  647. /* We look just for `\n` ignoring `\r` as it is very rare nowadays */
  648. sep_pos = memchr (t->start + pos, '\n', t->len - pos);
  649. if (sep_pos == NULL) {
  650. /* Either last `\n` or `\r` separated text */
  651. sep_pos = memchr (t->start + pos, '\r', t->len - pos);
  652. }
  653. pos = rspamd_lua_text_push_line (L, t, pos, sep_pos, stringify);
  654. /* Skip separators */
  655. while (pos < t->len) {
  656. if (t->start[pos] == '\n' || t->start[pos] == '\r') {
  657. pos ++;
  658. }
  659. else {
  660. break;
  661. }
  662. }
  663. /* Update pos */
  664. lua_pushinteger (L, pos);
  665. lua_replace (L, lua_upvalueindex (3));
  666. return 1;
  667. }
  668. static gint
  669. lua_text_lines (lua_State *L)
  670. {
  671. LUA_TRACE_POINT;
  672. struct rspamd_lua_text *t = lua_check_text (L, 1);
  673. gboolean stringify = FALSE;
  674. if (t) {
  675. if (lua_isboolean (L, 2)) {
  676. stringify = lua_toboolean (L, 2);
  677. }
  678. lua_pushvalue (L, 1);
  679. lua_pushboolean (L, stringify);
  680. lua_pushinteger (L, 0); /* Current pos */
  681. lua_pushcclosure (L, rspamd_lua_text_readline, 3);
  682. }
  683. else {
  684. return luaL_error (L, "invalid arguments");
  685. }
  686. return 1;
  687. }
  688. static gint
  689. rspamd_lua_text_regexp_split (lua_State *L) {
  690. struct rspamd_lua_text *t = lua_touserdata (L, lua_upvalueindex (1)),
  691. *new_t;
  692. struct rspamd_lua_regexp *re = *(struct rspamd_lua_regexp **)
  693. lua_touserdata (L, lua_upvalueindex (2));
  694. gboolean stringify = lua_toboolean (L, lua_upvalueindex (3));
  695. gint64 pos = lua_tointeger (L, lua_upvalueindex (4));
  696. gboolean matched;
  697. if (pos < 0) {
  698. return luaL_error (L, "invalid pos: %d", (gint) pos);
  699. }
  700. if (pos >= t->len) {
  701. /* We are done */
  702. return 0;
  703. }
  704. const gchar *start, *end, *old_start;
  705. end = t->start + pos;
  706. for (;;) {
  707. old_start = end;
  708. matched = rspamd_regexp_search (re->re, t->start, t->len, &start, &end, FALSE,
  709. NULL);
  710. if (matched) {
  711. if (start - old_start > 0) {
  712. if (stringify) {
  713. lua_pushlstring (L, old_start, start - old_start);
  714. }
  715. else {
  716. new_t = lua_newuserdata (L, sizeof (*t));
  717. rspamd_lua_setclass (L, "rspamd{text}", -1);
  718. new_t->start = old_start;
  719. new_t->len = start - old_start;
  720. new_t->flags = 0;
  721. }
  722. break;
  723. }
  724. else {
  725. if (start == end) {
  726. matched = FALSE;
  727. break;
  728. }
  729. /*
  730. * All match separators (e.g. starting separator,
  731. * we need to skip it). Continue iterations.
  732. */
  733. }
  734. }
  735. else {
  736. /* No match, stop */
  737. break;
  738. }
  739. }
  740. if (!matched && (t->len > 0 && (end == NULL || end < t->start + t->len))) {
  741. /* No more matches, but we might need to push the last element */
  742. if (end == NULL) {
  743. end = t->start;
  744. }
  745. /* No separators, need to push the whole remaining part */
  746. if (stringify) {
  747. lua_pushlstring (L, end, (t->start + t->len) - end);
  748. }
  749. else {
  750. new_t = lua_newuserdata (L, sizeof (*t));
  751. rspamd_lua_setclass (L, "rspamd{text}", -1);
  752. new_t->start = end;
  753. new_t->len = (t->start + t->len) - end;
  754. new_t->flags = 0;
  755. }
  756. pos = t->len;
  757. }
  758. else {
  759. pos = end - t->start;
  760. }
  761. /* Update pos */
  762. lua_pushinteger (L, pos);
  763. lua_replace (L, lua_upvalueindex (4));
  764. return 1;
  765. }
  766. static gint
  767. lua_text_split (lua_State *L)
  768. {
  769. LUA_TRACE_POINT;
  770. struct rspamd_lua_text *t = lua_check_text (L, 1);
  771. struct rspamd_lua_regexp *re;
  772. gboolean stringify = FALSE, own_re = FALSE;
  773. if (lua_type (L, 2) == LUA_TUSERDATA) {
  774. re = lua_check_regexp (L, 2);
  775. }
  776. else {
  777. rspamd_regexp_t *c_re;
  778. GError *err = NULL;
  779. c_re = rspamd_regexp_new (lua_tostring (L, 2), NULL, &err);
  780. if (c_re == NULL) {
  781. gint ret = luaL_error (L, "cannot parse regexp: %s, error: %s",
  782. lua_tostring (L, 2),
  783. err == NULL ? "undefined" : err->message);
  784. if (err) {
  785. g_error_free (err);
  786. }
  787. return ret;
  788. }
  789. re = g_malloc0 (sizeof (struct rspamd_lua_regexp));
  790. re->re = c_re;
  791. re->re_pattern = g_strdup (lua_tostring (L, 2));
  792. re->module = rspamd_lua_get_module_name (L);
  793. own_re = TRUE;
  794. }
  795. if (t && re) {
  796. if (lua_isboolean (L, 3)) {
  797. stringify = lua_toboolean (L, 3);
  798. }
  799. /* Upvalues */
  800. lua_pushvalue (L, 1); /* text */
  801. if (own_re) {
  802. struct rspamd_lua_regexp **pre;
  803. pre = lua_newuserdata (L, sizeof (struct rspamd_lua_regexp *));
  804. rspamd_lua_setclass (L, "rspamd{regexp}", -1);
  805. *pre = re;
  806. }
  807. else {
  808. lua_pushvalue (L, 2); /* regexp */
  809. }
  810. lua_pushboolean (L, stringify);
  811. lua_pushinteger (L, 0); /* Current pos */
  812. lua_pushcclosure (L, rspamd_lua_text_regexp_split, 4);
  813. }
  814. else {
  815. return luaL_error (L, "invalid arguments");
  816. }
  817. return 1;
  818. }
  819. static gint
  820. lua_text_at (lua_State *L)
  821. {
  822. return lua_text_byte(L);
  823. }
  824. static gint
  825. lua_text_byte (lua_State *L)
  826. {
  827. LUA_TRACE_POINT;
  828. struct rspamd_lua_text *t = lua_check_text (L, 1);
  829. if (!t) {
  830. return luaL_error (L, "invalid arguments");
  831. }
  832. gsize start = relative_pos_start (luaL_optinteger (L, 2, 1), t->len);
  833. gsize end = relative_pos_end (luaL_optinteger (L, 3, start), t->len);
  834. start--;
  835. if (start >= end) {
  836. return 0;
  837. }
  838. for (gsize i = start; i < end; i++) {
  839. lua_pushinteger (L, t->start[i]);
  840. }
  841. return end - start;
  842. }
  843. static gint
  844. lua_text_memchr (lua_State *L)
  845. {
  846. LUA_TRACE_POINT;
  847. struct rspamd_lua_text *t = lua_check_text (L, 1);
  848. int c;
  849. bool reverse = false;
  850. if (lua_isnumber (L, 2)) {
  851. c = lua_tonumber (L, 2);
  852. }
  853. else {
  854. gsize l;
  855. const gchar *str = lua_tolstring (L, 2, &l);
  856. if (str) {
  857. c = str[0];
  858. if (l != 1) {
  859. return luaL_error (L, "need exactly one character to search");
  860. }
  861. }
  862. else {
  863. return luaL_error (L, "invalid arguments");
  864. }
  865. }
  866. if (t) {
  867. void *f;
  868. if (lua_isboolean (L, 3)) {
  869. reverse = lua_toboolean (L, 3);
  870. }
  871. if (reverse) {
  872. f = rspamd_memrchr (t->start, c, t->len);
  873. }
  874. else {
  875. f = memchr (t->start, c, t->len);
  876. }
  877. if (f) {
  878. lua_pushinteger (L, ((const char *)f) - t->start + 1);
  879. }
  880. else {
  881. lua_pushinteger (L, -1);
  882. }
  883. }
  884. else {
  885. return luaL_error (L, "invalid arguments");
  886. }
  887. return 1;
  888. }
  889. static gint
  890. lua_text_bytes (lua_State *L)
  891. {
  892. LUA_TRACE_POINT;
  893. struct rspamd_lua_text *t = lua_check_text (L, 1);
  894. if (t) {
  895. lua_createtable (L, t->len, 0);
  896. for (gsize i = 0; i < t->len; i ++) {
  897. lua_pushinteger (L, (guchar)t->start[i]);
  898. lua_rawseti (L, -2, i + 1);
  899. }
  900. }
  901. else {
  902. return luaL_error (L, "invalid arguments");
  903. }
  904. return 1;
  905. }
  906. static gint
  907. lua_text_save_in_file (lua_State *L)
  908. {
  909. LUA_TRACE_POINT;
  910. struct rspamd_lua_text *t = lua_check_text (L, 1);
  911. const gchar *fname = NULL;
  912. guint mode = 00644;
  913. gint fd = -1;
  914. gboolean need_close = FALSE;
  915. if (t != NULL) {
  916. if (lua_type (L, 2) == LUA_TSTRING) {
  917. fname = luaL_checkstring (L, 2);
  918. if (lua_type (L, 3) == LUA_TNUMBER) {
  919. mode = lua_tonumber (L, 3);
  920. }
  921. }
  922. else if (lua_type (L, 2) == LUA_TNUMBER) {
  923. /* Created fd */
  924. fd = lua_tonumber (L, 2);
  925. }
  926. if (fd == -1) {
  927. if (fname) {
  928. fd = rspamd_file_xopen (fname, O_CREAT | O_WRONLY | O_EXCL, mode, 0);
  929. if (fd == -1) {
  930. lua_pushboolean (L, false);
  931. lua_pushstring (L, strerror (errno));
  932. return 2;
  933. }
  934. need_close = TRUE;
  935. }
  936. else {
  937. fd = STDOUT_FILENO;
  938. }
  939. }
  940. if (write (fd, t->start, t->len) == -1) {
  941. if (fd != STDOUT_FILENO) {
  942. close (fd);
  943. }
  944. lua_pushboolean (L, false);
  945. lua_pushstring (L, strerror (errno));
  946. return 2;
  947. }
  948. if (need_close) {
  949. close (fd);
  950. }
  951. lua_pushboolean (L, true);
  952. }
  953. else {
  954. return luaL_error (L, "invalid arguments");
  955. }
  956. return 1;
  957. }
  958. static gint
  959. lua_text_gc (lua_State *L)
  960. {
  961. LUA_TRACE_POINT;
  962. struct rspamd_lua_text *t = lua_check_text (L, 1);
  963. if (t != NULL) {
  964. g_assert (!(t->flags & RSPAMD_TEXT_FLAG_FAKE));
  965. if (t->flags & RSPAMD_TEXT_FLAG_OWN) {
  966. if (t->flags & RSPAMD_TEXT_FLAG_WIPE) {
  967. rspamd_explicit_memzero ((guchar *)t->start, t->len);
  968. }
  969. if (t->flags & RSPAMD_TEXT_FLAG_MMAPED) {
  970. munmap ((gpointer)t->start, t->len);
  971. }
  972. else {
  973. if (t->flags & RSPAMD_TEXT_FLAG_SYSMALLOC) {
  974. free ((gpointer) t->start);
  975. }
  976. else {
  977. g_free ((gpointer) t->start);
  978. }
  979. }
  980. }
  981. }
  982. return 0;
  983. }
  984. static gint
  985. lua_text_eq (lua_State *L)
  986. {
  987. LUA_TRACE_POINT;
  988. struct rspamd_lua_text *t1 = lua_check_text_or_string (L, 1),
  989. *t2 = lua_check_text_or_string (L, 2);
  990. if (t1->len == t2->len) {
  991. lua_pushboolean (L, memcmp (t1->start, t2->start, t1->len) == 0);
  992. }
  993. else {
  994. lua_pushboolean (L, false);
  995. }
  996. return 1;
  997. }
  998. static gint
  999. lua_text_lt (lua_State *L)
  1000. {
  1001. LUA_TRACE_POINT;
  1002. struct rspamd_lua_text *t1 = lua_check_text_or_string (L, 1),
  1003. *t2 = lua_check_text_or_string (L, 2);
  1004. if (t1 && t2) {
  1005. if (t1->len == t2->len) {
  1006. lua_pushboolean (L, memcmp (t1->start, t2->start, t1->len) < 0);
  1007. }
  1008. else {
  1009. lua_pushboolean (L, t1->len < t2->len);
  1010. }
  1011. }
  1012. return 1;
  1013. }
  1014. static gint
  1015. lua_text_concat (lua_State *L)
  1016. {
  1017. LUA_TRACE_POINT;
  1018. struct rspamd_lua_text *t1 = lua_check_text_or_string (L, 1),
  1019. *t2 = lua_check_text_or_string (L, 2);
  1020. if (t1 && t2) {
  1021. struct rspamd_lua_text *final;
  1022. final = lua_new_text (L, NULL, t1->len + t2->len, TRUE);
  1023. memcpy ((void *)final->start, t1->start, t1->len);
  1024. memcpy ((void *)(final->start + t1->len), t2->start, t2->len);
  1025. }
  1026. return 1;
  1027. }
  1028. static gint
  1029. lua_text_wipe (lua_State *L)
  1030. {
  1031. LUA_TRACE_POINT;
  1032. struct rspamd_lua_text *t = lua_check_text (L, 1);
  1033. if (t != NULL) {
  1034. if (t->flags & RSPAMD_TEXT_FLAG_OWN) {
  1035. rspamd_explicit_memzero ((guchar *)t->start, t->len);
  1036. }
  1037. else {
  1038. return luaL_error (L, "cannot wipe not owned text");
  1039. }
  1040. }
  1041. else {
  1042. return luaL_error (L, "invalid arguments");
  1043. }
  1044. return 0;
  1045. }
  1046. static gint
  1047. lua_text_base32 (lua_State *L)
  1048. {
  1049. LUA_TRACE_POINT;
  1050. struct rspamd_lua_text *t = lua_check_text (L, 1), *out;
  1051. enum rspamd_base32_type btype = RSPAMD_BASE32_DEFAULT;
  1052. if (t != NULL) {
  1053. if (lua_type (L, 2) == LUA_TSTRING) {
  1054. btype = rspamd_base32_decode_type_from_str (lua_tostring (L, 2));
  1055. if (btype == RSPAMD_BASE32_INVALID) {
  1056. return luaL_error (L, "invalid b32 type: %s", lua_tostring (L, 2));
  1057. }
  1058. }
  1059. out = lua_new_text (L, NULL, t->len * 8 / 5 + 2, TRUE);
  1060. out->len = rspamd_encode_base32_buf (t->start, t->len, (gchar *)out->start,
  1061. out->len, btype);
  1062. }
  1063. else {
  1064. return luaL_error (L, "invalid arguments");
  1065. }
  1066. return 1;
  1067. }
  1068. static gint
  1069. lua_text_base64 (lua_State *L)
  1070. {
  1071. LUA_TRACE_POINT;
  1072. struct rspamd_lua_text *t = lua_check_text (L, 1), *out;
  1073. gsize line_len = 0;
  1074. gboolean fold = FALSE;
  1075. if (t != NULL) {
  1076. if (lua_type (L, 2) == LUA_TNUMBER) {
  1077. line_len = lua_tointeger (L, 2);
  1078. if (line_len <= 8) {
  1079. return luaL_error (L, "too small line length (at least 8 is required)");
  1080. }
  1081. }
  1082. enum rspamd_newlines_type how = RSPAMD_TASK_NEWLINES_CRLF;
  1083. if (lua_type (L, 3) == LUA_TSTRING) {
  1084. const gchar *how_str = lua_tostring (L, 3);
  1085. if (g_ascii_strcasecmp (how_str, "cr") == 0) {
  1086. how = RSPAMD_TASK_NEWLINES_CR;
  1087. }
  1088. else if (g_ascii_strcasecmp (how_str, "lf") == 0) {
  1089. how = RSPAMD_TASK_NEWLINES_LF;
  1090. }
  1091. else if (g_ascii_strcasecmp (how_str, "crlf") != 0) {
  1092. return luaL_error (L, "invalid newline style: %s", how_str);
  1093. }
  1094. }
  1095. if (lua_type (L, 4) == LUA_TBOOLEAN) {
  1096. fold = lua_toboolean (L, 4);
  1097. }
  1098. gsize sz_len;
  1099. out = lua_newuserdata (L, sizeof (*t));
  1100. out->flags = RSPAMD_TEXT_FLAG_OWN;
  1101. out->start = rspamd_encode_base64_common (t->start, t->len,
  1102. line_len, &sz_len, fold, how);
  1103. out->len = sz_len;
  1104. rspamd_lua_setclass (L, "rspamd{text}", -1);
  1105. }
  1106. else {
  1107. return luaL_error (L, "invalid arguments");
  1108. }
  1109. return 1;
  1110. }
  1111. static gint
  1112. lua_text_hex (lua_State *L)
  1113. {
  1114. LUA_TRACE_POINT;
  1115. struct rspamd_lua_text *t = lua_check_text (L, 1), *out;
  1116. if (t != NULL) {
  1117. out = lua_new_text (L, NULL, t->len * 2, TRUE);
  1118. out->len = rspamd_encode_hex_buf (t->start, t->len, (gchar *)out->start,
  1119. out->len);
  1120. }
  1121. else {
  1122. return luaL_error (L, "invalid arguments");
  1123. }
  1124. return 1;
  1125. }
  1126. #define BITOP(a,b,op) \
  1127. ((a)[(gsize)(b)/(8*sizeof *(a))] op (gsize)1<<((gsize)(b)%(8*sizeof *(a))))
  1128. static gint
  1129. lua_text_exclude_chars (lua_State *L)
  1130. {
  1131. LUA_TRACE_POINT;
  1132. struct rspamd_lua_text *t = lua_check_text (L, 1);
  1133. gssize patlen;
  1134. const gchar *pat = lua_tolstring (L, 2, &patlen), *p, *end;
  1135. gchar *dest, *d;
  1136. gsize byteset[32 / sizeof(gsize)]; /* Bitset for ascii */
  1137. gboolean copy = TRUE;
  1138. guint *plen;
  1139. if (t != NULL && pat && patlen > 0) {
  1140. if (lua_isboolean (L, 3)) {
  1141. copy = lua_toboolean (L, 3);
  1142. }
  1143. else if (t->flags & RSPAMD_TEXT_FLAG_OWN) {
  1144. copy = FALSE;
  1145. }
  1146. if (!copy) {
  1147. dest = (gchar *)t->start;
  1148. plen = &t->len;
  1149. lua_pushvalue (L, 1); /* Push text as a result */
  1150. }
  1151. else {
  1152. /* We need to copy read only text */
  1153. struct rspamd_lua_text *nt;
  1154. dest = g_malloc (t->len);
  1155. nt = lua_newuserdata (L, sizeof (*nt));
  1156. rspamd_lua_setclass (L, "rspamd{text}", -1);
  1157. nt->len = t->len;
  1158. nt->flags = RSPAMD_TEXT_FLAG_OWN;
  1159. memcpy (dest, t->start, t->len);
  1160. nt->start = dest;
  1161. plen = &nt->len;
  1162. }
  1163. /* Fill pattern bitset */
  1164. memset (byteset, 0, sizeof byteset);
  1165. while (patlen > 0) {
  1166. if (*pat == '%') {
  1167. pat ++;
  1168. patlen --;
  1169. if (patlen > 0) {
  1170. /*
  1171. * This stuff assumes little endian, but GSIZE_FROM_LE should
  1172. * deal with proper conversion
  1173. */
  1174. switch (*pat) {
  1175. case '%':
  1176. BITOP (byteset, *(guchar *) pat, |=);
  1177. break;
  1178. case 's':
  1179. /* "\r\n\t\f " */
  1180. byteset[0] |= GSIZE_FROM_LE (0x100003600);
  1181. break;
  1182. case 'n':
  1183. /* newlines: "\r\n" */
  1184. byteset[0] |= GSIZE_FROM_LE (0x2400);
  1185. break;
  1186. case '8':
  1187. /* 8 bit characters */
  1188. byteset[2] |= GSIZE_FROM_LE (0xffffffffffffffffLLU);
  1189. byteset[3] |= GSIZE_FROM_LE (0xffffffffffffffffLLU);
  1190. break;
  1191. case 'c':
  1192. /* Non printable (control) characters */
  1193. byteset[0] |= GSIZE_FROM_LE (0xffffffff);
  1194. /* Del character */
  1195. byteset[1] |= GSIZE_FROM_LE (0x8000000000000000);
  1196. break;
  1197. }
  1198. }
  1199. else {
  1200. /* Last '%' */
  1201. BITOP (byteset, (guchar)'%', |=);
  1202. }
  1203. }
  1204. else {
  1205. BITOP (byteset, *(guchar *)pat, |=);
  1206. }
  1207. pat ++;
  1208. patlen --;
  1209. }
  1210. for (; patlen > 0 && BITOP (byteset, *(guchar *)pat, |=); pat++, patlen --);
  1211. p = t->start;
  1212. end = t->start + t->len;
  1213. d = dest;
  1214. while (p < end) {
  1215. if (!BITOP (byteset, *(guchar *)p, &)) {
  1216. *d++ = *p;
  1217. }
  1218. p ++;
  1219. }
  1220. *(plen) = d - dest;
  1221. }
  1222. else {
  1223. return luaL_error (L, "invalid arguments");
  1224. }
  1225. return 1;
  1226. }
  1227. static gint
  1228. lua_text_oneline (lua_State *L)
  1229. {
  1230. LUA_TRACE_POINT;
  1231. struct rspamd_lua_text *t = lua_check_text (L, 1);
  1232. const gchar *p, *end;
  1233. gchar *dest, *d;
  1234. gsize byteset[32 / sizeof(gsize)]; /* Bitset for ascii */
  1235. gboolean copy = TRUE, seen_8bit = FALSE;
  1236. guint *plen;
  1237. if (t != NULL) {
  1238. if (lua_isboolean (L, 2)) {
  1239. copy = lua_toboolean (L, 2);
  1240. }
  1241. else if (t->flags & RSPAMD_TEXT_FLAG_OWN) {
  1242. copy = FALSE;
  1243. }
  1244. if (!copy) {
  1245. dest = (gchar *)t->start;
  1246. plen = &t->len;
  1247. lua_pushvalue (L, 1); /* Push text as a result */
  1248. }
  1249. else {
  1250. /* We need to copy read only text */
  1251. struct rspamd_lua_text *nt;
  1252. dest = g_malloc (t->len);
  1253. nt = lua_newuserdata (L, sizeof (*nt));
  1254. rspamd_lua_setclass (L, "rspamd{text}", -1);
  1255. nt->len = t->len;
  1256. nt->flags = RSPAMD_TEXT_FLAG_OWN;
  1257. memcpy (dest, t->start, t->len);
  1258. nt->start = dest;
  1259. plen = &nt->len;
  1260. }
  1261. /* Fill pattern bitset */
  1262. memset (byteset, 0, sizeof byteset);
  1263. /* All spaces */
  1264. byteset[0] |= GSIZE_FROM_LE (0x100003600);
  1265. /* Control characters */
  1266. byteset[0] |= GSIZE_FROM_LE (0xffffffff);
  1267. /* Del character */
  1268. byteset[1] |= GSIZE_FROM_LE (0x8000000000000000);
  1269. /* 8 bit characters */
  1270. byteset[2] |= GSIZE_FROM_LE (0xffffffffffffffffLLU);
  1271. byteset[3] |= GSIZE_FROM_LE (0xffffffffffffffffLLU);
  1272. p = t->start;
  1273. end = t->start + t->len;
  1274. d = dest;
  1275. while (p < end) {
  1276. if (!BITOP (byteset, *(guchar *)p, &)) {
  1277. *d++ = *p;
  1278. }
  1279. else {
  1280. if ((*(guchar *)p) & 0x80) {
  1281. seen_8bit = TRUE;
  1282. *d++ = *p;
  1283. }
  1284. else {
  1285. if (*p == ' ') {
  1286. if (d != dest) {
  1287. *d++ = *p++;
  1288. }
  1289. while (p < end && g_ascii_isspace (*p)) {
  1290. p ++;
  1291. }
  1292. continue; /* To avoid p++ */
  1293. }
  1294. else if (*p == '\r' || *p == '\n') {
  1295. if (d != dest) {
  1296. *d++ = ' ';
  1297. p ++;
  1298. }
  1299. while (p < end && g_ascii_isspace (*p)) {
  1300. p ++;
  1301. }
  1302. continue; /* To avoid p++ */
  1303. }
  1304. }
  1305. }
  1306. p ++;
  1307. }
  1308. while (d > dest && g_ascii_isspace (*(d - 1))) {
  1309. d --;
  1310. }
  1311. if (seen_8bit) {
  1312. if (rspamd_fast_utf8_validate (dest, d - dest) != 0) {
  1313. /* Need to make it valid :( */
  1314. UChar32 uc;
  1315. goffset err_offset;
  1316. gsize remain = d - dest;
  1317. gchar *nd = dest;
  1318. while (remain > 0 && (err_offset = rspamd_fast_utf8_validate (nd, remain)) > 0) {
  1319. gint i = 0;
  1320. err_offset --; /* As it returns it 1 indexed */
  1321. nd += err_offset;
  1322. remain -= err_offset;
  1323. /* Each invalid character of input requires 3 bytes of output (+2 bytes) */
  1324. while (i < remain) {
  1325. gint old_pos = i;
  1326. U8_NEXT (nd, i, remain, uc);
  1327. if (uc < 0) {
  1328. nd[old_pos] = '?';
  1329. }
  1330. else {
  1331. break;
  1332. }
  1333. }
  1334. nd += i;
  1335. remain -= i;
  1336. }
  1337. }
  1338. }
  1339. *(plen) = d - dest;
  1340. }
  1341. else {
  1342. return luaL_error (L, "invalid arguments");
  1343. }
  1344. return 1;
  1345. }
  1346. static gint
  1347. lua_text_lower (lua_State *L)
  1348. {
  1349. LUA_TRACE_POINT;
  1350. struct rspamd_lua_text *t = lua_check_text (L, 1), *nt;
  1351. gboolean is_utf8 = FALSE, is_inplace = FALSE;
  1352. if (t != NULL) {
  1353. if (lua_isboolean (L, 2)) {
  1354. is_utf8 = lua_toboolean (L, 2);
  1355. }
  1356. if (lua_isboolean (L, 3)) {
  1357. is_inplace = lua_toboolean (L, 3);
  1358. }
  1359. if (is_inplace) {
  1360. nt = t;
  1361. lua_pushvalue (L, 1);
  1362. }
  1363. else {
  1364. nt = lua_new_text (L, t->start, t->len, TRUE);
  1365. }
  1366. if (!is_utf8) {
  1367. rspamd_str_lc ((gchar *) nt->start, nt->len);
  1368. }
  1369. else {
  1370. rspamd_str_lc_utf8 ((gchar *) nt->start, nt->len);
  1371. }
  1372. }
  1373. else {
  1374. return luaL_error (L, "invalid arguments");
  1375. }
  1376. return 1;
  1377. }
  1378. /* Used to distinguish lua text metatable */
  1379. static const guint rspamd_lua_text_cookie = 0x2b21ef6fU;
  1380. static gint
  1381. lua_load_text (lua_State * L)
  1382. {
  1383. lua_newtable (L);
  1384. lua_pushstring (L, "cookie");
  1385. lua_pushnumber (L, rspamd_lua_text_cookie);
  1386. lua_settable (L, -3);
  1387. luaL_register (L, NULL, textlib_f);
  1388. return 1;
  1389. }
  1390. void
  1391. luaopen_text (lua_State *L)
  1392. {
  1393. rspamd_lua_new_class (L, "rspamd{text}", textlib_m);
  1394. lua_pushstring (L, "cookie");
  1395. lua_pushnumber (L, rspamd_lua_text_cookie);
  1396. lua_settable (L, -3);
  1397. lua_pop (L, 1);
  1398. rspamd_lua_add_preload (L, "rspamd_text", lua_load_text);
  1399. }