123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627 |
- /*-
- * Copyright 2016 Vsevolod Stakhov
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- #include "lua_common.h"
- #include "html.h"
- #include "tokenizers/tokenizers.h"
- #include "unix-std.h"
- #include "contrib/zstd/zstd.h"
- #include "contrib/uthash/utlist.h"
- #include "libmime/email_addr.h"
- #include "libmime/content_type.h"
- #include "libmime/mime_headers.h"
- #include "linenoise.h"
- #include <math.h>
- #include <glob.h>
- #include <zlib.h>
-
- #include "unicode/uspoof.h"
-
- /***
- * @module rspamd_util
- * This module contains some generic purpose utilities that could be useful for
- * testing and production rules.
- */
-
- /***
- * @function util.create_event_base()
- * Creates new event base for processing asynchronous events
- * @return {ev_base} new event processing base
- */
- LUA_FUNCTION_DEF (util, create_event_base);
- /***
- * @function util.load_rspamd_config(filename)
- * Load rspamd config from the specified file
- * @return {confg} new configuration object suitable for access
- */
- LUA_FUNCTION_DEF (util, load_rspamd_config);
- /***
- * @function util.config_from_ucl(any, string)
- * Load rspamd config from ucl reperesented by any lua table
- * @return {confg} new configuration object suitable for access
- */
- LUA_FUNCTION_DEF (util, config_from_ucl);
- /***
- * @function util.encode_base64(input[, str_len, [newlines_type]])
- * Encodes data in base64 breaking lines if needed
- * @param {text or string} input input data
- * @param {number} str_len optional size of lines or 0 if split is not needed
- * @return {rspamd_text} encoded data chunk
- */
- LUA_FUNCTION_DEF (util, encode_base64);
- /***
- * @function util.encode_qp(input[, str_len, [newlines_type]])
- * Encodes data in quouted printable breaking lines if needed
- * @param {text or string} input input data
- * @param {number} str_len optional size of lines or 0 if split is not needed
- * @return {rspamd_text} encoded data chunk
- */
- LUA_FUNCTION_DEF (util, encode_qp);
- /***
- * @function util.decode_base64(input)
- * Decodes data from base64 ignoring whitespace characters
- * @param {text or string} input data to decode; if `rspamd{text}` is used then the string is modified **in-place**
- * @return {rspamd_text} decoded data chunk
- */
- LUA_FUNCTION_DEF (util, decode_base64);
-
- /***
- * @function util.encode_base32(input)
- * Encodes data in base32 breaking lines if needed
- * @param {text or string} input input data
- * @return {rspamd_text} encoded data chunk
- */
- LUA_FUNCTION_DEF (util, encode_base32);
- /***
- * @function util.decode_base32(input)
- * Decodes data from base32 ignoring whitespace characters
- * @param {text or string} input data to decode
- * @return {rspamd_text} decoded data chunk
- */
- LUA_FUNCTION_DEF (util, decode_base32);
-
- /***
- * @function util.decode_url(input)
- * Decodes data from url encoding
- * @param {text or string} input data to decode
- * @return {rspamd_text} decoded data chunk
- */
- LUA_FUNCTION_DEF (util, decode_url);
-
- /***
- * @function util.tokenize_text(input[, exceptions])
- * Create tokens from a text using optional exceptions list
- * @param {text/string} input input data
- * @param {table} exceptions, a table of pairs containing <start_pos,length> of exceptions in the input
- * @return {table/strings} list of strings representing words in the text
- */
- LUA_FUNCTION_DEF (util, tokenize_text);
- LUA_FUNCTION_DEF (util, process_message);
- /***
- * @function util.tanh(num)
- * Calculates hyperbolic tanhent of the specified floating point value
- * @param {number} num input number
- * @return {number} hyperbolic tanhent of the variable
- */
- LUA_FUNCTION_DEF (util, tanh);
-
- /***
- * @function util.parse_html(input)
- * Parses HTML and returns the according text
- * @param {string|text} in input HTML
- * @return {rspamd_text} processed text with no HTML tags
- */
- LUA_FUNCTION_DEF (util, parse_html);
-
- /***
- * @function util.levenshtein_distance(s1, s2)
- * Returns levenstein distance between two strings
- * @param {string} s1 the first string
- * @param {string} s2 the second string
- * @return {number} number of differences in two strings
- */
- LUA_FUNCTION_DEF (util, levenshtein_distance);
-
- /***
- * @function util.parse_addr(str)
- * Parse rfc822 address to components. Returns a table of components:
- *
- * - `name`: name of address (e.g. Some User)
- * - `addr`: address part (e.g. user@example.com)
- *
- * @param {string} str input string
- * @return {table} resulting table of components
- */
- LUA_FUNCTION_DEF (util, parse_addr);
-
- /***
- * @function util.fold_header(name, value, [how, [stop_chars]])
- * Fold rfc822 header according to the folding rules
- *
- * @param {string} name name of the header
- * @param {string} value value of the header
- * @param {string} how "cr" for \r, "lf" for \n and "crlf" for \r\n (default)
- * @param {string} stop_chars also fold header when the
- * @return {string} Folded value of the header
- */
- LUA_FUNCTION_DEF (util, fold_header);
-
- /***
- * @function util.is_uppercase(str)
- * Returns true if a string is all uppercase
- *
- * @param {string} str input string
- * @return {bool} true if a string is all uppercase
- */
- LUA_FUNCTION_DEF (util, is_uppercase);
-
- /***
- * @function util.humanize_number(num)
- * Returns humanized representation of given number (like 1k instead of 1000)
- *
- * @param {number} num number to humanize
- * @return {string} humanized representation of a number
- */
- LUA_FUNCTION_DEF (util, humanize_number);
-
- /***
- * @function util.get_tld(host)
- * Returns effective second level domain part (eSLD) for the specified host
- *
- * @param {string} host hostname
- * @return {string} eSLD part of the hostname or the full hostname if eSLD was not found
- */
- LUA_FUNCTION_DEF (util, get_tld);
-
- /***
- * @function util.glob(pattern)
- * Returns results for the glob match for the specified pattern
- *
- * @param {string} pattern glob pattern to match ('?' and '*' are supported)
- * @return {table/string} list of matched files
- */
- LUA_FUNCTION_DEF (util, glob);
-
- /***
- * @function util.parse_mail_address(str, pool)
- * Parses email address and returns a table of tables in the following format:
- *
- * - `name` - name of internet address in UTF8, e.g. for `Vsevolod Stakhov <blah@foo.com>` it returns `Vsevolod Stakhov`
- * - `addr` - address part of the address
- * - `user` - user part (if present) of the address, e.g. `blah`
- * - `domain` - domain part (if present), e.g. `foo.com`
- *
- * @param {string} str input string
- * @param {rspamd_mempool} pool memory pool to use
- * @return {table/tables} parsed list of mail addresses
- */
- LUA_FUNCTION_DEF (util, parse_mail_address);
-
- /***
- * @function util.strlen_utf8(str)
- * Returns length of string encoded in utf-8 in characters.
- * If invalid characters are found, then this function returns number of bytes.
- * @param {string} str utf8 encoded string
- * @return {number} number of characters in string
- */
- LUA_FUNCTION_DEF (util, strlen_utf8);
-
-
- /***
- * @function util.strcasecmp(str1, str2)
- * Compares two ascii strings regardless of their case. Return value >0, 0 and <0
- * if `str1` is more, equal or less than `str2`
- * @param {string} str1 plain string
- * @param {string} str2 plain string
- * @return {number} result of comparison
- */
- LUA_FUNCTION_DEF (util, strcasecmp_ascii);
-
- /***
- * @function util.strequal_caseless(str1, str2)
- * Compares two utf8 strings regardless of their case. Return `true` if `str1` is
- * equal to `str2`
- * @param {string} str1 utf8 encoded string
- * @param {string} str2 utf8 encoded string
- * @return {bool} result of comparison
- */
- LUA_FUNCTION_DEF (util, strequal_caseless);
-
- /***
- * @function util.get_ticks()
- * Returns current number of ticks as floating point number
- * @return {number} number of current clock ticks (monotonically increasing)
- */
- LUA_FUNCTION_DEF (util, get_ticks);
-
- /***
- * @function util.get_time()
- * Returns current time as unix time in floating point representation
- * @return {number} number of seconds since 01.01.1970
- */
- LUA_FUNCTION_DEF (util, get_time);
-
- /***
- * @function util.time_to_string(seconds)
- * Converts time from Unix time to HTTP date format
- * @param {number} seconds unix timestamp
- * @return {string} date as HTTP date
- */
- LUA_FUNCTION_DEF (util, time_to_string);
-
- /***
- * @function util.stat(fname)
- * Performs stat(2) on a specified filepath and returns table of values
- *
- * - `size`: size of file in bytes
- * - `type`: type of filepath: `regular`, `directory`, `special`
- * - `mtime`: modification time as unix time
- *
- * @return {string,table} string is returned when error is occurred
- * @example
- *
- * local err,st = util.stat('/etc/password')
- *
- * if err then
- * -- handle error
- * else
- * print(st['size'])
- * end
- */
- LUA_FUNCTION_DEF (util, stat);
-
- /***
- * @function util.unlink(fname)
- * Removes the specified file from the filesystem
- *
- * @param {string} fname filename to remove
- * @return {boolean,[string]} true if file has been deleted or false,'error string'
- */
- LUA_FUNCTION_DEF (util, unlink);
-
- /***
- * @function util.lock_file(fname, [fd])
- * Lock the specified file. This function returns {number} which must be passed to `util.unlock_file` after usage
- * or you'll have a resource leak
- *
- * @param {string} fname filename to lock
- * @param {number} fd use the specified fd instead of opening one
- * @return {number|nil,string} number if locking was successful or nil + error otherwise
- */
- LUA_FUNCTION_DEF (util, lock_file);
-
- /***
- * @function util.unlock_file(fd, [close_fd])
- * Unlock the specified file closing the file descriptor associated.
- *
- * @param {number} fd descriptor to unlock
- * @param {boolean} close_fd close descriptor on unlocking (default: TRUE)
- * @return {boolean[,string]} true if a file was unlocked
- */
- LUA_FUNCTION_DEF (util, unlock_file);
-
- /***
- * @function util.create_file(fname, [mode])
- * Creates the specified file with the default mode 0644
- *
- * @param {string} fname filename to create
- * @param {number} mode open mode (you should use octal number here)
- * @return {number|nil,string} file descriptor or pair nil + error string
- */
- LUA_FUNCTION_DEF (util, create_file);
-
- /***
- * @function util.close_file(fd)
- * Closes descriptor fd
- *
- * @param {number} fd descriptor to close
- * @return {boolean[,string]} true if a file was closed
- */
- LUA_FUNCTION_DEF (util, close_file);
-
- /***
- * @function util.random_hex(size)
- * Returns random hex string of the specified size
- *
- * @param {number} len length of desired string in bytes
- * @return {string} string with random hex digests
- */
- LUA_FUNCTION_DEF (util, random_hex);
-
- /***
- * @function util.zstd_compress(data)
- * Compresses input using zstd compression
- *
- * @param {string/rspamd_text} data input data
- * @return {rspamd_text} compressed data
- */
- LUA_FUNCTION_DEF (util, zstd_compress);
-
- /***
- * @function util.zstd_decompress(data)
- * Decompresses input using zstd algorithm
- *
- * @param {string/rspamd_text} data compressed data
- * @return {error,rspamd_text} pair of error + decompressed text
- */
- LUA_FUNCTION_DEF (util, zstd_decompress);
-
- /***
- * @function util.gzip_decompress(data)
- * Decompresses input using gzip algorithm
- *
- * @param {string/rspamd_text} data compressed data
- * @return {error,rspamd_text} pair of error + decompressed text
- */
- LUA_FUNCTION_DEF (util, gzip_decompress);
-
- /***
- * @function util.gzip_compress(data)
- * Compresses input using gzip compression
- *
- * @param {string/rspamd_text} data input data
- * @return {rspamd_text} compressed data
- */
- LUA_FUNCTION_DEF (util, gzip_compress);
-
- /***
- * @function util.normalize_prob(prob, [bias = 0.5])
- * Normalize probabilities using polynom
- *
- * @param {number} prob probability param
- * @param {number} bias number to subtract for making the final solution
- * @return {number} normalized number
- */
- LUA_FUNCTION_DEF (util, normalize_prob);
- /***
- * @function util.is_utf_spoofed(str, [str2])
- * Returns true if a string is spoofed (possibly with another string `str2`)
- * @return {boolean} true if a string is spoofed
- */
- LUA_FUNCTION_DEF (util, is_utf_spoofed);
-
- /***
- * @function util.is_valid_utf8(str)
- * Returns true if a string is valid UTF8 string
- * @return {boolean} true if a string is spoofed
- */
- LUA_FUNCTION_DEF (util, is_valid_utf8);
-
- /***
- * @function util.has_obscured_unicode(str)
- * Returns true if a string has obscure UTF symbols (zero width spaces, order marks), ignores invalid utf characters
- * @return {boolean} true if a has obscured unicode characters (+ character and offset if found)
- */
- LUA_FUNCTION_DEF (util, has_obscured_unicode);
-
- /***
- * @function util.readline([prompt])
- * Returns string read from stdin with history and editing support
- * @return {string} string read from the input (with line endings stripped)
- */
- LUA_FUNCTION_DEF (util, readline);
-
- /***
- * @function util.readpassphrase([prompt])
- * Returns string read from stdin disabling echo
- * @return {string} string read from the input (with line endings stripped)
- */
- LUA_FUNCTION_DEF (util, readpassphrase);
-
- /***
- * @function util.file_exists(file)
- * Checks if a specified file exists and is available for reading
- * @return {boolean,string} true if file exists + string error if not
- */
- LUA_FUNCTION_DEF (util, file_exists);
-
- /***
- * @function util.mkdir(dir[, recursive])
- * Creates a specified directory
- * @return {boolean[,error]} true if directory has been created
- */
- LUA_FUNCTION_DEF (util, mkdir);
-
- /***
- * @function util.umask(mask)
- * Sets new umask. Accepts either numeric octal string, e.g. '022' or a plain
- * number, e.g. 0x12 (since Lua does not support octal integrals)
- * @return {number} old umask
- */
- LUA_FUNCTION_DEF (util, umask);
-
- /***
- * @function util.isatty()
- * Returns if stdout is a tty
- * @return {boolean} true in case of output being tty
- */
- LUA_FUNCTION_DEF (util, isatty);
-
- /***
- * @function util.pack(fmt, ...)
- *
- * Backport of Lua 5.3 `string.pack` function:
- * Returns a binary string containing the values v1, v2, etc. packed (that is,
- * serialized in binary form) according to the format string `fmt`
- * A format string is a sequence of conversion options. The conversion
- * options are as follows:
- *
- * * <: sets little endian
- * * >: sets big endian
- * * =: sets native endian
- * * ![n]: sets maximum alignment to n (default is native alignment)
- * * b: a signed byte (char)
- * * B: an unsigned byte (char)
- * * h: a signed short (native size)
- * * H: an unsigned short (native size)
- * * l: a signed long (native size)
- * * L: an unsigned long (native size)
- * * j: a lua_Integer
- * * J: a lua_Unsigned
- * * T: a size_t (native size)
- * * i[n]: a signed int with n bytes (default is native size)
- * * I[n]: an unsigned int with n bytes (default is native size)
- * * f: a float (native size)
- * * d: a double (native size)
- * * n: a lua_Number
- * * cn: a fixed-sized string with n bytes
- * * z: a zero-terminated string
- * * s[n]: a string preceded by its length coded as an unsigned integer with
- * * n bytes (default is a size_t)
- * * x: one byte of padding
- * * Xop: an empty item that aligns according to option op (which is otherwise ignored)
- * * ' ': (empty space) ignored
- *
- * (A "[n]" means an optional integral numeral.) Except for padding, spaces,
- * and configurations (options "xX <=>!"), each option corresponds to an
- * argument (in string.pack) or a result (in string.unpack).
- *
- * For options "!n", "sn", "in", and "In", n can be any integer between 1 and
- * All integral options check overflows; string.pack checks whether the given
- * value fits in the given size; string.unpack checks whether the read value
- * fits in a Lua integer.
- *
- * Any format string starts as if prefixed by "!1=", that is, with maximum
- * alignment of 1 (no alignment) and native endianness.
- *
- * Alignment works as follows: For each option, the format gets extra padding
- * until the data starts at an offset that is a multiple of the minimum
- * between the option size and the maximum alignment; this minimum must be a
- * power of 2. Options "c" and "z" are not aligned; option "s" follows the
- * alignment of its starting integer.
- *
- * All padding is filled with zeros by string.pack (and ignored by unpack).
- */
- LUA_FUNCTION_DEF (util, pack);
-
- /***
- * @function util.packsize(fmt)
- *
- * Returns size of the packed binary string returned for the same `fmt` argument
- * by @see util.pack
- */
- LUA_FUNCTION_DEF (util, packsize);
-
- /***
- * @function util.unpack(fmt, s [, pos])
- * Unpacks string `s` according to the format string `fmt` as described in
- * @see util.pack
- *
- * @returns {multiple} list of unpacked values according to `fmt`
- */
- LUA_FUNCTION_DEF (util, unpack);
-
- /***
- * @function util.caseless_hash(str[, seed])
- * Calculates caseless non-crypto hash from a string or rspamd text
- * @param str string or lua_text
- * @param seed mandatory seed (0xdeadbabe by default)
- * @return {int64} boxed int64_t
- */
- LUA_FUNCTION_DEF (util, caseless_hash);
-
- /***
- * @function util.caseless_hash_fast(str[, seed])
- * Calculates caseless non-crypto hash from a string or rspamd text
- * @param str string or lua_text
- * @param seed mandatory seed (0xdeadbabe by default)
- * @return {number} number from int64_t
- */
- LUA_FUNCTION_DEF (util, caseless_hash_fast);
-
- /***
- * @function util.get_hostname()
- * Returns hostname for this machine
- * @return {string} hostname
- */
- LUA_FUNCTION_DEF (util, get_hostname);
-
- /***
- * @function util.parse_content_type(ct_string, mempool)
- * Parses content-type string to a table:
- * - `type`
- * - `subtype`
- * - `charset`
- * - `boundary`
- * - other attributes
- *
- * @param {string} ct_string content type as string
- * @param {rspamd_mempool} mempool needed to store temporary data (e.g. task pool)
- * @return table or nil if cannot parse content type
- */
- LUA_FUNCTION_DEF (util, parse_content_type);
-
- /***
- * @function util.mime_header_encode(hdr)
- * Encodes header if needed
- * @param {string} hdr input header
- * @return encoded header
- */
- LUA_FUNCTION_DEF (util, mime_header_encode);
-
-
- static const struct luaL_reg utillib_f[] = {
- LUA_INTERFACE_DEF (util, create_event_base),
- LUA_INTERFACE_DEF (util, load_rspamd_config),
- LUA_INTERFACE_DEF (util, config_from_ucl),
- LUA_INTERFACE_DEF (util, process_message),
- LUA_INTERFACE_DEF (util, encode_base64),
- LUA_INTERFACE_DEF (util, encode_qp),
- LUA_INTERFACE_DEF (util, decode_base64),
- LUA_INTERFACE_DEF (util, encode_base32),
- LUA_INTERFACE_DEF (util, decode_base32),
- LUA_INTERFACE_DEF (util, decode_url),
- LUA_INTERFACE_DEF (util, tokenize_text),
- LUA_INTERFACE_DEF (util, tanh),
- LUA_INTERFACE_DEF (util, parse_html),
- LUA_INTERFACE_DEF (util, levenshtein_distance),
- LUA_INTERFACE_DEF (util, parse_addr),
- LUA_INTERFACE_DEF (util, fold_header),
- LUA_INTERFACE_DEF (util, is_uppercase),
- LUA_INTERFACE_DEF (util, humanize_number),
- LUA_INTERFACE_DEF (util, get_tld),
- LUA_INTERFACE_DEF (util, glob),
- LUA_INTERFACE_DEF (util, parse_mail_address),
- LUA_INTERFACE_DEF (util, strlen_utf8),
- LUA_INTERFACE_DEF (util, strcasecmp_ascii),
- LUA_INTERFACE_DEF (util, strequal_caseless),
- LUA_INTERFACE_DEF (util, get_ticks),
- LUA_INTERFACE_DEF (util, get_time),
- LUA_INTERFACE_DEF (util, time_to_string),
- LUA_INTERFACE_DEF (util, stat),
- LUA_INTERFACE_DEF (util, unlink),
- LUA_INTERFACE_DEF (util, lock_file),
- LUA_INTERFACE_DEF (util, unlock_file),
- LUA_INTERFACE_DEF (util, create_file),
- LUA_INTERFACE_DEF (util, close_file),
- LUA_INTERFACE_DEF (util, random_hex),
- LUA_INTERFACE_DEF (util, zstd_compress),
- LUA_INTERFACE_DEF (util, zstd_decompress),
- LUA_INTERFACE_DEF (util, gzip_compress),
- LUA_INTERFACE_DEF (util, gzip_decompress),
- LUA_INTERFACE_DEF (util, normalize_prob),
- LUA_INTERFACE_DEF (util, caseless_hash),
- LUA_INTERFACE_DEF (util, caseless_hash_fast),
- LUA_INTERFACE_DEF (util, is_utf_spoofed),
- LUA_INTERFACE_DEF (util, is_valid_utf8),
- LUA_INTERFACE_DEF (util, has_obscured_unicode),
- LUA_INTERFACE_DEF (util, readline),
- LUA_INTERFACE_DEF (util, readpassphrase),
- LUA_INTERFACE_DEF (util, file_exists),
- LUA_INTERFACE_DEF (util, mkdir),
- LUA_INTERFACE_DEF (util, umask),
- LUA_INTERFACE_DEF (util, isatty),
- LUA_INTERFACE_DEF (util, get_hostname),
- LUA_INTERFACE_DEF (util, parse_content_type),
- LUA_INTERFACE_DEF (util, mime_header_encode),
- LUA_INTERFACE_DEF (util, pack),
- LUA_INTERFACE_DEF (util, unpack),
- LUA_INTERFACE_DEF (util, packsize),
- {NULL, NULL}
- };
-
- LUA_FUNCTION_DEF (int64, tostring);
- LUA_FUNCTION_DEF (int64, tonumber);
- LUA_FUNCTION_DEF (int64, hex);
-
- static const struct luaL_reg int64lib_m[] = {
- LUA_INTERFACE_DEF (int64, tostring),
- LUA_INTERFACE_DEF (int64, tonumber),
- LUA_INTERFACE_DEF (int64, hex),
- {"__tostring", lua_int64_tostring},
- {NULL, NULL}
- };
-
- LUA_FUNCTION_DEF (ev_base, loop);
-
- static const struct luaL_reg ev_baselib_m[] = {
- LUA_INTERFACE_DEF (ev_base, loop),
- {"__tostring", rspamd_lua_class_tostring},
- {NULL, NULL}
- };
-
- static gint64
- lua_check_int64 (lua_State * L, gint pos)
- {
- void *ud = rspamd_lua_check_udata (L, pos, "rspamd{int64}");
- luaL_argcheck (L, ud != NULL, pos, "'int64' expected");
- return ud ? *((gint64 *)ud) : 0LL;
- }
-
-
- static gint
- lua_util_create_event_base (lua_State *L)
- {
- LUA_TRACE_POINT;
- struct event_base **pev_base;
-
- pev_base = lua_newuserdata (L, sizeof (struct event_base *));
- rspamd_lua_setclass (L, "rspamd{ev_base}", -1);
- *pev_base = event_init ();
-
- return 1;
- }
-
- static gint
- lua_util_load_rspamd_config (lua_State *L)
- {
- LUA_TRACE_POINT;
- struct rspamd_config *cfg, **pcfg;
- const gchar *cfg_name;
-
- cfg_name = luaL_checkstring (L, 1);
-
- if (cfg_name) {
- cfg = rspamd_config_new (RSPAMD_CONFIG_INIT_SKIP_LUA);
- cfg->lua_state = L;
-
- if (rspamd_config_read (cfg, cfg_name, NULL, NULL, NULL)) {
- msg_err_config ("cannot load config from %s", cfg_name);
- lua_pushnil (L);
- }
- else {
- rspamd_config_post_load (cfg, 0);
- pcfg = lua_newuserdata (L, sizeof (struct rspamd_config *));
- rspamd_lua_setclass (L, "rspamd{config}", -1);
- *pcfg = cfg;
- }
- }
-
- return 1;
- }
-
- static gint
- parse_config_options (const char *str_options)
- {
- gint ret = 0;
- gchar **vec;
- const gchar *str;
- guint i, l;
-
- vec = g_strsplit_set (str_options, ",;", -1);
- if (vec) {
- l = g_strv_length (vec);
- for (i = 0; i < l; i ++) {
- str = vec[i];
-
- if (g_ascii_strcasecmp (str, "INIT_URL") == 0) {
- ret |= RSPAMD_CONFIG_INIT_URL;
- } else if (g_ascii_strcasecmp (str, "INIT_LIBS") == 0) {
- ret |= RSPAMD_CONFIG_INIT_LIBS;
- } else if (g_ascii_strcasecmp (str, "INIT_SYMCACHE") == 0) {
- ret |= RSPAMD_CONFIG_INIT_SYMCACHE;
- } else if (g_ascii_strcasecmp (str, "INIT_VALIDATE") == 0) {
- ret |= RSPAMD_CONFIG_INIT_VALIDATE;
- } else if (g_ascii_strcasecmp (str, "INIT_NO_TLD") == 0) {
- ret |= RSPAMD_CONFIG_INIT_NO_TLD;
- } else if (g_ascii_strcasecmp (str, "INIT_PRELOAD_MAPS") == 0) {
- ret |= RSPAMD_CONFIG_INIT_PRELOAD_MAPS;
- } else {
- msg_warn ("bad type: %s", str);
- }
- }
-
- g_strfreev (vec);
- }
-
- return ret;
- }
-
- static gint
- lua_util_config_from_ucl (lua_State *L)
- {
- LUA_TRACE_POINT;
- struct rspamd_config *cfg = NULL, **pcfg;
- struct rspamd_rcl_section *top;
- GError *err = NULL;
- ucl_object_t *obj;
- const char *str_options = NULL;
- gint int_options = 0;
-
-
- obj = ucl_object_lua_import (L, 1);
- if (lua_gettop (L) == 2) {
- if (lua_type (L, 2) == LUA_TSTRING) {
- str_options = lua_tostring (L, 2);
- int_options = parse_config_options(str_options);
- }
- else {
- msg_err ("config_from_ucl: second parameter is expected to be string");
- ucl_object_unref (obj);
- lua_pushnil (L);
- }
- }
-
- if (obj) {
- cfg = rspamd_config_new (RSPAMD_CONFIG_INIT_SKIP_LUA);
- cfg->lua_state = L;
-
- cfg->rcl_obj = obj;
- cfg->cache = rspamd_symcache_new (cfg);
- top = rspamd_rcl_config_init (cfg, NULL);
-
- if (!rspamd_rcl_parse (top, cfg, cfg, cfg->cfg_pool, cfg->rcl_obj, &err)) {
- msg_err ("rcl parse error: %s", err->message);
- ucl_object_unref (obj);
- lua_pushnil (L);
- }
- else {
-
- if (int_options & RSPAMD_CONFIG_INIT_LIBS) {
- cfg->libs_ctx = rspamd_init_libs ();
- }
-
- rspamd_config_post_load (cfg, int_options);
- pcfg = lua_newuserdata (L, sizeof (struct rspamd_config *));
- rspamd_lua_setclass (L, "rspamd{config}", -1);
- *pcfg = cfg;
- }
- }
-
- return 1;
- }
-
- static gboolean
- lua_util_task_fin (struct rspamd_task *task, void *ud)
- {
- ucl_object_t **target = ud;
-
- *target = rspamd_protocol_write_ucl (task, RSPAMD_PROTOCOL_DEFAULT);
- rdns_resolver_release (task->resolver->r);
-
- return TRUE;
- }
-
- static gint
- lua_util_process_message (lua_State *L)
- {
- LUA_TRACE_POINT;
- struct rspamd_config *cfg = lua_check_config (L, 1);
- const gchar *message;
- gsize mlen;
- struct rspamd_task *task;
- struct event_base *base;
- ucl_object_t *res = NULL;
-
- message = luaL_checklstring (L, 2, &mlen);
-
- if (cfg != NULL && message != NULL) {
- base = event_init ();
- rspamd_init_filters (cfg, FALSE);
- task = rspamd_task_new (NULL, cfg, NULL, NULL, base);
- task->msg.begin = rspamd_mempool_alloc (task->task_pool, mlen);
- rspamd_strlcpy ((gpointer)task->msg.begin, message, mlen);
- task->msg.len = mlen;
- task->fin_callback = lua_util_task_fin;
- task->fin_arg = &res;
- task->resolver = dns_resolver_init (NULL, base, cfg);
- task->s = rspamd_session_create (task->task_pool, rspamd_task_fin,
- rspamd_task_restore, (event_finalizer_t)rspamd_task_free, task);
-
- if (!rspamd_task_load_message (task, NULL, message, mlen)) {
- lua_pushnil (L);
- }
- else {
- if (rspamd_task_process (task, RSPAMD_TASK_PROCESS_ALL)) {
- event_base_loop (base, 0);
-
- if (res != NULL) {
- ucl_object_push_lua (L, res, true);
-
- ucl_object_unref (res);
- }
- else {
- ucl_object_push_lua (L,
- rspamd_protocol_write_ucl (task, RSPAMD_PROTOCOL_DEFAULT),
- true);
- rdns_resolver_release (task->resolver->r);
- rspamd_session_destroy (task->s);
- }
- }
- else {
- lua_pushnil (L);
- }
- }
-
- event_base_free (base);
- }
- else {
- lua_pushnil (L);
- }
-
- return 1;
- }
-
- static gint
- lua_util_encode_base64 (lua_State *L)
- {
- LUA_TRACE_POINT;
- struct rspamd_lua_text *t;
- const gchar *s = NULL;
- gchar *out;
- gsize inlen, outlen;
- guint str_lim = 0;
- gboolean fold = FALSE;
-
- if (lua_type (L, 1) == LUA_TSTRING) {
- s = luaL_checklstring (L, 1, &inlen);
- }
- else if (lua_type (L, 1) == LUA_TUSERDATA) {
- t = lua_check_text (L, 1);
-
- if (t != NULL) {
- s = t->start;
- inlen = t->len;
- }
- }
-
- if (lua_gettop (L) > 1) {
- str_lim = luaL_checknumber (L, 2);
-
- fold = !!(str_lim > 0);
- }
-
- if (s == NULL) {
- lua_pushnil (L);
- }
- else {
-
- if (fold) {
- out = rspamd_encode_base64 (s, inlen, str_lim, &outlen);
- }
- else {
- enum rspamd_newlines_type how = RSPAMD_TASK_NEWLINES_CRLF;
-
- if (lua_type (L, 3) == LUA_TSTRING) {
- const gchar *how_str = lua_tostring (L, 3);
-
- if (g_ascii_strcasecmp (how_str, "cr") == 0) {
- how = RSPAMD_TASK_NEWLINES_CR;
- }
- else if (g_ascii_strcasecmp (how_str, "lf") == 0) {
- how = RSPAMD_TASK_NEWLINES_LF;
- }
- else if (g_ascii_strcasecmp (how_str, "crlf") != 0) {
- return luaL_error (L, "invalid newline style: %s", how_str);
- }
- }
-
- out = rspamd_encode_base64_fold (s, inlen, str_lim, &outlen, how);
- }
-
- if (out != NULL) {
- t = lua_newuserdata (L, sizeof (*t));
- rspamd_lua_setclass (L, "rspamd{text}", -1);
- t->start = out;
- t->len = outlen;
- /* Need destruction */
- t->flags = RSPAMD_TEXT_FLAG_OWN;
- }
- else {
- lua_pushnil (L);
- }
- }
-
- return 1;
- }
-
- static gint
- lua_util_encode_qp (lua_State *L)
- {
- LUA_TRACE_POINT;
- struct rspamd_lua_text *t;
- const gchar *s = NULL;
- gchar *out;
- gsize inlen, outlen;
- guint str_lim = 0;
-
- if (lua_type (L, 1) == LUA_TSTRING) {
- s = luaL_checklstring (L, 1, &inlen);
- }
- else if (lua_type (L, 1) == LUA_TUSERDATA) {
- t = lua_check_text (L, 1);
-
- if (t != NULL) {
- s = t->start;
- inlen = t->len;
- }
- }
-
- if (lua_gettop (L) > 1) {
- str_lim = luaL_checknumber (L, 2);
- }
-
- if (s == NULL) {
- lua_pushnil (L);
- }
- else {
- enum rspamd_newlines_type how = RSPAMD_TASK_NEWLINES_CRLF;
-
- if (lua_type (L, 3) == LUA_TSTRING) {
- const gchar *how_str = lua_tostring (L, 3);
-
- if (g_ascii_strcasecmp (how_str, "cr") == 0) {
- how = RSPAMD_TASK_NEWLINES_CR;
- }
- else if (g_ascii_strcasecmp (how_str, "lf") == 0) {
- how = RSPAMD_TASK_NEWLINES_LF;
- }
- else if (g_ascii_strcasecmp (how_str, "crlf") != 0) {
- return luaL_error (L, "invalid newline style: %s", how_str);
- }
- }
-
- out = rspamd_encode_qp_fold (s, inlen, str_lim, &outlen, how);
-
- if (out != NULL) {
- t = lua_newuserdata (L, sizeof (*t));
- rspamd_lua_setclass (L, "rspamd{text}", -1);
- t->start = out;
- t->len = outlen;
- /* Need destruction */
- t->flags = RSPAMD_TEXT_FLAG_OWN;
- }
- else {
- lua_pushnil (L);
- }
- }
-
- return 1;
- }
-
- static gint
- lua_util_decode_base64 (lua_State *L)
- {
- LUA_TRACE_POINT;
- struct rspamd_lua_text *t;
- const gchar *s = NULL;
- gsize inlen, outlen;
- gboolean zero_copy = FALSE, grab_own = FALSE;
- gint state = 0;
- guint save = 0;
-
- if (lua_type (L, 1) == LUA_TSTRING) {
- s = luaL_checklstring (L, 1, &inlen);
- }
- else if (lua_type (L, 1) == LUA_TUSERDATA) {
- t = lua_check_text (L, 1);
-
- if (t != NULL) {
- s = t->start;
- inlen = t->len;
- }
- }
-
- if (s != NULL) {
- if (zero_copy) {
- /* Decode in place */
- outlen = g_base64_decode_step (s, inlen, (guchar *)s, &state, &save);
- t = lua_newuserdata (L, sizeof (*t));
- rspamd_lua_setclass (L, "rspamd{text}", -1);
- t->start = s;
- t->len = outlen;
-
- if (grab_own) {
- t->flags |= RSPAMD_TEXT_FLAG_OWN;
- }
- else {
- t->flags = 0;
- }
- }
- else {
- t = lua_newuserdata (L, sizeof (*t));
- rspamd_lua_setclass (L, "rspamd{text}", -1);
- t->len = (inlen / 4) * 3 + 3;
- t->start = g_malloc (t->len);
- outlen = g_base64_decode_step (s, inlen, (guchar *)t->start,
- &state, &save);
- t->len = outlen;
- t->flags = RSPAMD_TEXT_FLAG_OWN;
- }
- }
- else {
- lua_pushnil (L);
- }
-
- return 1;
- }
-
- static gint
- lua_util_encode_base32 (lua_State *L)
- {
- LUA_TRACE_POINT;
- struct rspamd_lua_text *t;
- const gchar *s = NULL;
- gchar *out;
- gsize inlen, outlen;
-
- if (lua_type (L, 1) == LUA_TSTRING) {
- s = luaL_checklstring (L, 1, &inlen);
- }
- else if (lua_type (L, 1) == LUA_TUSERDATA) {
- t = lua_check_text (L, 1);
-
- if (t != NULL) {
- s = t->start;
- inlen = t->len;
- }
- }
-
- if (s == NULL) {
- lua_pushnil (L);
- }
- else {
- out = rspamd_encode_base32 (s, inlen);
-
- if (out != NULL) {
- t = lua_newuserdata (L, sizeof (*t));
- outlen = strlen (out);
- rspamd_lua_setclass (L, "rspamd{text}", -1);
- t->start = out;
- t->len = outlen;
- /* Need destruction */
- t->flags = RSPAMD_TEXT_FLAG_OWN;
- }
- else {
- lua_pushnil (L);
- }
- }
-
- return 1;
- }
-
- static gint
- lua_util_decode_base32 (lua_State *L)
- {
- LUA_TRACE_POINT;
- struct rspamd_lua_text *t;
- const gchar *s = NULL;
- gsize inlen, outlen;
-
- if (lua_type (L, 1) == LUA_TSTRING) {
- s = luaL_checklstring (L, 1, &inlen);
- }
- else if (lua_type (L, 1) == LUA_TUSERDATA) {
- t = lua_check_text (L, 1);
-
- if (t != NULL) {
- s = t->start;
- inlen = t->len;
- }
- }
-
- if (s != NULL) {
- t = lua_newuserdata (L, sizeof (*t));
- rspamd_lua_setclass (L, "rspamd{text}", -1);
- t->start = rspamd_decode_base32 (s, inlen, &outlen);
- t->len = outlen;
- t->flags = RSPAMD_TEXT_FLAG_OWN;
- }
- else {
- lua_pushnil (L);
- }
-
- return 1;
- }
-
- static gint
- lua_util_decode_url (lua_State *L)
- {
- LUA_TRACE_POINT;
- struct rspamd_lua_text *t;
- const gchar *s = NULL;
- gsize inlen;
-
- if (lua_type (L, 1) == LUA_TSTRING) {
- s = luaL_checklstring (L, 1, &inlen);
- }
- else if (lua_type (L, 1) == LUA_TUSERDATA) {
- t = lua_check_text (L, 1);
-
- if (t != NULL) {
- s = t->start;
- inlen = t->len;
- }
- }
-
- if (s != NULL) {
- t = lua_newuserdata (L, sizeof (*t));
- rspamd_lua_setclass (L, "rspamd{text}", -1);
- t->start = g_malloc (inlen);
- memcpy ((char *)t->start, s, inlen);
- t->len = rspamd_url_decode ((char *)t->start, s, inlen);
- t->flags = RSPAMD_TEXT_FLAG_OWN;
- }
- else {
- lua_pushnil (L);
- }
-
- return 1;
- }
-
-
- static gint
- lua_util_tokenize_text (lua_State *L)
- {
- LUA_TRACE_POINT;
- const gchar *in = NULL;
- gsize len = 0, pos, ex_len, i;
- GList *exceptions = NULL, *cur;
- struct rspamd_lua_text *t;
- struct rspamd_process_exception *ex;
- UText utxt = UTEXT_INITIALIZER;
- GArray *res;
- rspamd_stat_token_t *w;
-
- if (lua_type (L, 1) == LUA_TSTRING) {
- in = luaL_checklstring (L, 1, &len);
- }
- else if (lua_type (L, 1) == LUA_TUSERDATA) {
- t = lua_check_text (L, 1);
-
- if (t) {
- in = t->start;
- len = t->len;
- }
- }
-
- if (in == NULL) {
- lua_pushnil (L);
- return 1;
- }
-
- if (lua_gettop (L) > 1 && lua_type (L, 2) == LUA_TTABLE) {
- lua_pushvalue (L, 2);
- lua_pushnil (L);
-
- while (lua_next (L, -2) != 0) {
- if (lua_type (L, -1) == LUA_TTABLE) {
- lua_rawgeti (L, -1, 1);
- pos = luaL_checknumber (L, -1);
- lua_pop (L, 1);
- lua_rawgeti (L, -1, 2);
- ex_len = luaL_checknumber (L, -1);
- lua_pop (L, 1);
-
- if (ex_len > 0) {
- ex = g_malloc0 (sizeof (*ex));
- ex->pos = pos;
- ex->len = ex_len;
- ex->type = RSPAMD_EXCEPTION_GENERIC;
- exceptions = g_list_prepend (exceptions, ex);
- }
- }
- lua_pop (L, 1);
- }
-
- lua_pop (L, 1);
- }
-
- if (exceptions) {
- exceptions = g_list_reverse (exceptions);
- }
-
- UErrorCode uc_err = U_ZERO_ERROR;
- utext_openUTF8 (&utxt,
- in,
- len,
- &uc_err);
-
- res = rspamd_tokenize_text ((gchar *)in, len,
- &utxt,
- RSPAMD_TOKENIZE_UTF, NULL,
- exceptions,
- NULL, NULL);
-
- if (res == NULL) {
- lua_pushnil (L);
- }
- else {
- lua_createtable (L, res->len, 0);
-
- for (i = 0; i < res->len; i ++) {
- w = &g_array_index (res, rspamd_stat_token_t, i);
- lua_pushlstring (L, w->original.begin, w->original.len);
- lua_rawseti (L, -2, i + 1);
- }
- }
-
- cur = exceptions;
- while (cur) {
- ex = cur->data;
- g_free (ex);
- cur = g_list_next (cur);
- }
-
- g_list_free (exceptions);
- utext_close (&utxt);
-
- return 1;
- }
-
- static gint
- lua_util_tanh (lua_State *L)
- {
- LUA_TRACE_POINT;
- gdouble in = luaL_checknumber (L, 1);
-
- lua_pushnumber (L, tanh (in));
-
- return 1;
- }
-
- static gint
- lua_util_parse_html (lua_State *L)
- {
- LUA_TRACE_POINT;
- struct rspamd_lua_text *t;
- const gchar *start = NULL;
- gsize len;
- GByteArray *res, *in;
- rspamd_mempool_t *pool;
- struct html_content *hc;
-
- if (lua_type (L, 1) == LUA_TUSERDATA) {
- t = lua_check_text (L, 1);
-
- if (t != NULL) {
- start = t->start;
- len = t->len;
- }
- }
- else if (lua_type (L, 1) == LUA_TSTRING) {
- start = luaL_checklstring (L, 1, &len);
- }
-
- if (start != NULL) {
- pool = rspamd_mempool_new (rspamd_mempool_suggest_size (), NULL);
- hc = rspamd_mempool_alloc0 (pool, sizeof (*hc));
- in = g_byte_array_sized_new (len);
- g_byte_array_append (in, start, len);
-
- res = rspamd_html_process_part (pool, hc, in);
-
- t = lua_newuserdata (L, sizeof (*t));
- rspamd_lua_setclass (L, "rspamd{text}", -1);
- t->start = res->data;
- t->len = res->len;
- t->flags = RSPAMD_TEXT_FLAG_OWN;
-
- g_byte_array_free (res, FALSE);
- g_byte_array_free (in, TRUE);
- rspamd_mempool_delete (pool);
- }
- else {
- lua_pushnil (L);
- }
-
- return 1;
- }
-
- static gint
- lua_util_levenshtein_distance (lua_State *L)
- {
- LUA_TRACE_POINT;
- const gchar *s1, *s2;
- gsize s1len, s2len;
- gint dist = 0;
- guint replace_cost = 1;
-
- s1 = luaL_checklstring (L, 1, &s1len);
- s2 = luaL_checklstring (L, 2, &s2len);
-
- if (lua_isnumber (L, 3)) {
- replace_cost = lua_tonumber (L, 3);
- }
-
- if (s1 && s2) {
- dist = rspamd_strings_levenshtein_distance (s1, s1len, s2, s2len,
- replace_cost);
- }
-
- lua_pushinteger (L, dist);
-
- return 1;
- }
-
- static gint
- lua_util_parse_addr (lua_State *L)
- {
- LUA_TRACE_POINT;
- GPtrArray *addrs;
- gsize len;
- const gchar *str = luaL_checklstring (L, 1, &len);
- rspamd_mempool_t *pool;
- gboolean own_pool = FALSE;
-
- if (str) {
-
- if (lua_type (L, 2) == LUA_TUSERDATA) {
- pool = rspamd_lua_check_mempool (L, 2);
-
- if (pool == NULL) {
- return luaL_error (L, "invalid arguments");
- }
- }
- else {
- pool = rspamd_mempool_new (rspamd_mempool_suggest_size (), "lua util");
- own_pool = TRUE;
- }
-
- addrs = rspamd_email_address_from_mime (pool, str, len, NULL);
-
- if (addrs == NULL) {
- lua_pushnil (L);
- }
- else {
- lua_push_emails_address_list (L, addrs);
- }
-
- if (own_pool) {
- rspamd_mempool_delete (pool);
- }
- }
- else {
- lua_pushnil (L);
- }
-
- return 1;
- }
-
- static gint
- lua_util_fold_header (lua_State *L)
- {
- LUA_TRACE_POINT;
- const gchar *name, *value, *how, *stop_chars = NULL;
- GString *folded;
-
- name = luaL_checkstring (L, 1);
- value = luaL_checkstring (L, 2);
-
- if (name && value) {
-
- if (lua_isstring (L, 3)) {
-
- how = lua_tostring (L, 3);
-
- if (lua_isstring (L, 4)) {
- stop_chars = lua_tostring (L, 4);
- }
-
- if (strcmp (how, "cr") == 0) {
- folded = rspamd_header_value_fold (name, value, 0,
- RSPAMD_TASK_NEWLINES_CR, stop_chars);
- }
- else if (strcmp (how, "lf") == 0) {
- folded = rspamd_header_value_fold (name, value, 0,
- RSPAMD_TASK_NEWLINES_LF, stop_chars);
- }
- else {
- folded = rspamd_header_value_fold (name, value, 0,
- RSPAMD_TASK_NEWLINES_CRLF, stop_chars);
- }
- }
- else {
- folded = rspamd_header_value_fold (name, value, 0,
- RSPAMD_TASK_NEWLINES_CRLF, stop_chars);
- }
-
- if (folded) {
- lua_pushlstring (L, folded->str, folded->len);
- g_string_free (folded, TRUE);
-
- return 1;
- }
- }
-
- lua_pushnil (L);
- return 1;
- }
-
- static gint
- lua_util_is_uppercase (lua_State *L)
- {
- LUA_TRACE_POINT;
- const gchar *str;
- gsize sz;
- gint32 i = 0;
- UChar32 uc;
- guint nlc = 0, nuc = 0;
-
- str = luaL_checklstring (L, 1, &sz);
-
- if (str && sz > 0) {
- while (i >= 0 && i < sz) {
- U8_NEXT (str, i, sz, uc);
-
- if (uc < 0) {
- break;
- }
-
- if (u_isupper (uc)) {
- nuc++;
- }
- else if (u_islower (uc)) {
- nlc++;
- }
- }
- }
-
- if (nuc > 0 && nlc == 0) {
- lua_pushboolean (L, TRUE);
- }
- else {
- lua_pushboolean (L, FALSE);
- }
-
- return 1;
- }
-
- static gint
- lua_util_humanize_number (lua_State *L)
- {
- LUA_TRACE_POINT;
- gdouble number = luaL_checknumber (L, 1);
- gchar numbuf[32];
-
-
- rspamd_snprintf (numbuf, sizeof (numbuf), "%hL", (gint64)number);
- lua_pushstring (L, numbuf);
-
- return 1;
- }
-
- static gint
- lua_util_get_tld (lua_State *L)
- {
- LUA_TRACE_POINT;
- const gchar *host;
- gsize hostlen;
- rspamd_ftok_t tld;
-
- host = luaL_checklstring (L, 1, &hostlen);
-
- if (host) {
- if (!rspamd_url_find_tld (host, hostlen, &tld)) {
- lua_pushlstring (L, host, hostlen);
- }
- else {
- lua_pushlstring (L, tld.begin, tld.len);
- }
- }
- else {
- lua_pushnil (L);
- }
-
- return 1;
- }
-
-
- static gint
- lua_util_glob (lua_State *L)
- {
- LUA_TRACE_POINT;
- const gchar *pattern;
- glob_t gl;
- gint top, i, flags;
-
- top = lua_gettop (L);
- memset (&gl, 0, sizeof (gl));
- flags = GLOB_NOSORT;
-
- for (i = 1; i <= top; i ++, flags |= GLOB_APPEND) {
- pattern = luaL_checkstring (L, i);
-
- if (pattern) {
- glob (pattern, flags, NULL, &gl);
- }
- }
-
- lua_createtable (L, gl.gl_pathc, 0);
- /* Push results */
- for (i = 0; i < (gint)gl.gl_pathc; i ++) {
- lua_pushstring (L, gl.gl_pathv[i]);
- lua_rawseti (L, -2, i + 1);
- }
-
- globfree (&gl);
-
- return 1;
- }
-
- static gint
- lua_util_parse_mail_address (lua_State *L)
- {
- LUA_TRACE_POINT;
- GPtrArray *addrs;
- gsize len;
- const gchar *str = luaL_checklstring (L, 1, &len);
- rspamd_mempool_t *pool;
- gboolean own_pool = FALSE;
-
- if (str) {
-
- if (lua_type (L, 2) == LUA_TUSERDATA) {
- pool = rspamd_lua_check_mempool (L, 2);
-
- if (pool == NULL) {
- return luaL_error (L, "invalid arguments");
- }
- }
- else {
- pool = rspamd_mempool_new (rspamd_mempool_suggest_size (), "lua util");
- own_pool = TRUE;
- }
-
- addrs = rspamd_email_address_from_mime (pool, str, len, NULL);
-
- if (addrs == NULL) {
- lua_pushnil (L);
- }
- else {
- lua_push_emails_address_list (L, addrs);
- }
-
- if (own_pool) {
- rspamd_mempool_delete (pool);
- }
- }
- else {
- lua_pushnil (L);
- }
-
- return 1;
- }
-
- static gint
- lua_util_strlen_utf8 (lua_State *L)
- {
- LUA_TRACE_POINT;
- const gchar *str, *end;
- gsize len;
-
- str = lua_tolstring (L, 1, &len);
-
- if (str) {
- if (g_utf8_validate (str, len, &end)) {
- len = g_utf8_strlen (str, len);
- }
- else if (end != NULL && end > str) {
- len = (g_utf8_strlen (str, end - str)) /* UTF part */
- + (len - (end - str)) /* raw part */;
- }
-
- lua_pushinteger (L, len);
- }
- else {
- return luaL_error (L, "invalid arguments");
- }
-
- return 1;
- }
-
- static gint
- lua_util_strcasecmp_ascii (lua_State *L)
- {
- LUA_TRACE_POINT;
- const gchar *str1, *str2;
- gsize len1, len2;
- gint ret = -1;
-
- str1 = lua_tolstring (L, 1, &len1);
- str2 = lua_tolstring (L, 2, &len2);
-
- if (str1 && str2) {
-
- if (len1 == len2) {
- ret = g_ascii_strncasecmp (str1, str2, len1);
- }
- else {
- ret = len1 - len2;
- }
- }
- else {
- return luaL_error (L, "invalid arguments");
- }
-
- lua_pushinteger (L, ret);
- return 1;
- }
-
- static gint
- lua_util_strequal_caseless (lua_State *L)
- {
- LUA_TRACE_POINT;
- const gchar *str1, *str2;
- gsize len1, len2;
- gint ret = -1;
-
- str1 = lua_tolstring (L, 1, &len1);
- str2 = lua_tolstring (L, 2, &len2);
-
- if (str1 && str2) {
-
- if (len1 == len2) {
- ret = rspamd_lc_cmp (str1, str2, len1);
- }
- else {
- ret = len1 - len2;
- }
- }
- else {
- return luaL_error (L, "invalid arguments");
- }
-
- lua_pushboolean (L, (ret == 0) ? true : false);
- return 1;
- }
-
- static gint
- lua_util_get_ticks (lua_State *L)
- {
- LUA_TRACE_POINT;
- gdouble ticks;
- gboolean rdtsc = FALSE;
-
- if (lua_isboolean (L, 1)) {
- rdtsc = lua_toboolean (L, 1);
- }
-
- ticks = rspamd_get_ticks (rdtsc);
- lua_pushnumber (L, ticks);
-
- return 1;
- }
-
- static gint
- lua_util_get_time (lua_State *L)
- {
- LUA_TRACE_POINT;
- gdouble seconds;
- struct timeval tv;
-
- if (gettimeofday (&tv, NULL) == 0) {
- seconds = tv_to_double (&tv);
- }
- else {
- seconds = time (NULL);
- }
-
- lua_pushnumber (L, seconds);
-
- return 1;
- }
-
- static gint
- lua_util_time_to_string (lua_State *L)
- {
- LUA_TRACE_POINT;
- gdouble seconds;
- struct timeval tv;
- char timebuf[128];
-
- if (lua_isnumber (L, 1)) {
- seconds = lua_tonumber (L, 1);
- }
- else {
- if (gettimeofday (&tv, NULL) == 0) {
- seconds = tv_to_double (&tv);
- }
- else {
- seconds = time (NULL);
- }
- }
-
- rspamd_http_date_format (timebuf, sizeof (timebuf), seconds);
- lua_pushstring (L, timebuf);
-
- return 1;
- }
-
- static gint
- lua_util_stat (lua_State *L)
- {
- LUA_TRACE_POINT;
- const gchar *fpath;
- struct stat st;
-
- fpath = luaL_checkstring (L, 1);
-
- if (fpath) {
- if (stat (fpath, &st) == -1) {
- lua_pushstring (L, strerror (errno));
- lua_pushnil (L);
- }
- else {
- lua_pushnil (L);
- lua_createtable (L, 0, 3);
-
- lua_pushstring (L, "size");
- lua_pushinteger (L, st.st_size);
- lua_settable (L, -3);
-
- lua_pushstring (L, "mtime");
- lua_pushinteger (L, st.st_mtime);
- lua_settable (L, -3);
-
- lua_pushstring (L, "type");
- if (S_ISREG (st.st_mode)) {
- lua_pushstring (L, "regular");
- }
- else if (S_ISDIR (st.st_mode)) {
- lua_pushstring (L, "directory");
- }
- else {
- lua_pushstring (L, "special");
- }
- lua_settable (L, -3);
- }
- }
- else {
- return luaL_error (L, "invalid arguments");
- }
-
- return 2;
- }
-
- static gint
- lua_util_unlink (lua_State *L)
- {
- LUA_TRACE_POINT;
- const gchar *fpath;
- gint ret;
-
- fpath = luaL_checkstring (L, 1);
-
- if (fpath) {
- ret = unlink (fpath);
-
- if (ret == -1) {
- lua_pushboolean (L, false);
- lua_pushstring (L, strerror (errno));
-
- return 2;
- }
-
- lua_pushboolean (L, true);
- }
- else {
- return luaL_error (L, "invalid arguments");
- }
-
- return 1;
- }
-
- static gint
- lua_util_lock_file (lua_State *L)
- {
- LUA_TRACE_POINT;
- const gchar *fpath;
- gint fd = -1;
- gboolean own = FALSE;
-
- #if !HAVE_FLOCK
- struct flock fl = {
- .l_type = F_WRLCK,
- .l_whence = SEEK_SET,
- .l_start = 0,
- .l_len = 0
- };
- #endif
-
- fpath = luaL_checkstring (L, 1);
-
- if (fpath) {
- if (lua_isnumber (L, 2)) {
- fd = lua_tonumber (L, 2);
- }
- else {
- fd = open (fpath, O_RDONLY);
- own = TRUE;
- }
-
- if (fd == -1) {
- lua_pushnil (L);
- lua_pushstring (L, strerror (errno));
-
- return 2;
- }
-
- #if HAVE_FLOCK
- if (flock (fd, LOCK_EX) == -1) {
- #else
- if (fcntl (fd, F_SETLKW, &fl) == -1) {
- #endif
- lua_pushnil (L);
- lua_pushstring (L, strerror (errno));
-
- if (own) {
- close (fd);
- }
-
- return 2;
- }
-
- lua_pushinteger (L, fd);
- }
- else {
- return luaL_error (L, "invalid arguments");
- }
-
- return 1;
- }
-
- static gint
- lua_util_unlock_file (lua_State *L)
- {
- LUA_TRACE_POINT;
- gint fd = -1, ret, serrno;
- gboolean do_close = TRUE;
-
- #if !HAVE_FLOCK
- struct flock fl = {
- .l_type = F_UNLCK,
- .l_whence = SEEK_SET,
- .l_start = 0,
- .l_len = 0
- };
- #endif
-
- if (lua_isnumber (L, 1)) {
- fd = lua_tointeger (L, 1);
-
- if (lua_isboolean (L, 2)) {
- do_close = lua_toboolean (L, 2);
- }
-
- #if HAVE_FLOCK
- ret = flock (fd, LOCK_UN);
- #else
- ret = fcntl (fd, F_SETLKW, &fl);
- #endif
-
- if (do_close) {
- serrno = errno;
- close (fd);
- errno = serrno;
- }
-
- if (ret == -1) {
- lua_pushboolean (L, false);
- lua_pushstring (L, strerror (errno));
-
- return 2;
- }
-
- lua_pushboolean (L, true);
- }
- else {
- return luaL_error (L, "invalid arguments");
- }
-
- return 1;
- }
-
- static gint
- lua_util_create_file (lua_State *L)
- {
- LUA_TRACE_POINT;
- gint fd, mode = 00644;
- const gchar *fpath;
-
- fpath = luaL_checkstring (L, 1);
-
- if (fpath) {
- if (lua_isnumber (L, 2)) {
- mode = lua_tointeger (L, 2);
- }
-
- fd = rspamd_file_xopen (fpath, O_RDWR | O_CREAT | O_TRUNC, mode, 0);
-
- if (fd == -1) {
- lua_pushnil (L);
- lua_pushstring (L, strerror (errno));
-
- return 2;
- }
-
- lua_pushinteger (L, fd);
- }
- else {
- return luaL_error (L, "invalid arguments");
- }
-
- return 1;
- }
-
- static gint
- lua_util_close_file (lua_State *L)
- {
- LUA_TRACE_POINT;
- gint fd = -1;
-
- if (lua_isnumber (L, 1)) {
- fd = lua_tointeger (L, 1);
-
- if (close (fd) == -1) {
- lua_pushboolean (L, false);
- lua_pushstring (L, strerror (errno));
-
- return 2;
- }
-
- lua_pushboolean (L, true);
- }
- else {
- return luaL_error (L, "invalid arguments");
- }
-
- return 1;
- }
-
- static gint
- lua_util_random_hex (lua_State *L)
- {
- LUA_TRACE_POINT;
- gchar *buf;
- gint buflen;
-
- buflen = lua_tointeger (L, 1);
-
- if (buflen <= 0) {
- return luaL_error (L, "invalid arguments");
- }
-
- buf = g_malloc (buflen);
- rspamd_random_hex (buf, buflen);
- lua_pushlstring (L, buf, buflen);
- g_free (buf);
-
- return 1;
- }
-
- static gint
- lua_util_zstd_compress (lua_State *L)
- {
- LUA_TRACE_POINT;
- struct rspamd_lua_text *t = NULL, *res, tmp;
- gsize sz, r;
-
- if (lua_type (L, 1) == LUA_TSTRING) {
- t = &tmp;
- t->start = lua_tolstring (L, 1, &sz);
- t->len = sz;
- }
- else {
- t = lua_check_text (L, 1);
- }
-
- if (t == NULL || t->start == NULL) {
- return luaL_error (L, "invalid arguments");
- }
-
- sz = ZSTD_compressBound (t->len);
-
- if (ZSTD_isError (sz)) {
- msg_err ("cannot compress data: %s", ZSTD_getErrorName (sz));
- lua_pushnil (L);
-
- return 1;
- }
-
- res = lua_newuserdata (L, sizeof (*res));
- res->start = g_malloc (sz);
- res->flags = RSPAMD_TEXT_FLAG_OWN;
- rspamd_lua_setclass (L, "rspamd{text}", -1);
- r = ZSTD_compress ((void *)res->start, sz, t->start, t->len, 1);
-
- if (ZSTD_isError (r)) {
- msg_err ("cannot compress data: %s", ZSTD_getErrorName (r));
- lua_pop (L, 1); /* Text will be freed here */
- lua_pushnil (L);
-
- return 1;
- }
-
- res->len = r;
-
- return 1;
- }
-
- static gint
- lua_util_zstd_decompress (lua_State *L)
- {
- LUA_TRACE_POINT;
- struct rspamd_lua_text *t = NULL, *res;
- gsize outlen, sz, r;
- ZSTD_DStream *zstream;
- ZSTD_inBuffer zin;
- ZSTD_outBuffer zout;
- gchar *out;
-
- if (lua_type (L, 1) == LUA_TSTRING) {
- t = g_alloca (sizeof (*t));
- t->start = lua_tolstring (L, 1, &sz);
- t->len = sz;
- }
- else {
- t = lua_check_text (L, 1);
- }
-
- if (t == NULL || t->start == NULL) {
- return luaL_error (L, "invalid arguments");
- }
-
- zstream = ZSTD_createDStream ();
- ZSTD_initDStream (zstream);
-
- zin.pos = 0;
- zin.src = t->start;
- zin.size = t->len;
-
- if ((outlen = ZSTD_getDecompressedSize (zin.src, zin.size)) == 0) {
- outlen = ZSTD_DStreamOutSize ();
- }
-
- out = g_malloc (outlen);
-
- zout.dst = out;
- zout.pos = 0;
- zout.size = outlen;
-
- while (zin.pos < zin.size) {
- r = ZSTD_decompressStream (zstream, &zout, &zin);
-
- if (ZSTD_isError (r)) {
- msg_err ("cannot decompress data: %s", ZSTD_getErrorName (r));
- ZSTD_freeDStream (zstream);
- g_free (out);
- lua_pushstring (L, ZSTD_getErrorName (r));
- lua_pushnil (L);
-
- return 2;
- }
-
- if (zin.pos < zin.size && zout.pos == zout.size) {
- /* We need to extend output buffer */
- zout.size = zout.size * 1.5 + 1.0;
- out = g_realloc (zout.dst, zout.size);
- zout.dst = out;
- }
- }
-
- ZSTD_freeDStream (zstream);
- lua_pushnil (L); /* Error */
- res = lua_newuserdata (L, sizeof (*res));
- res->start = out;
- res->flags = RSPAMD_TEXT_FLAG_OWN;
- rspamd_lua_setclass (L, "rspamd{text}", -1);
- res->len = zout.pos;
-
- return 2;
- }
-
- static gint
- lua_util_gzip_compress (lua_State *L)
- {
- LUA_TRACE_POINT;
- struct rspamd_lua_text *t = NULL, *res, tmp;
- gsize sz;
- z_stream strm;
- gint rc;
- guchar *p;
- gsize remain;
-
- if (lua_type (L, 1) == LUA_TSTRING) {
- t = &tmp;
- t->start = lua_tolstring (L, 1, &sz);
- t->len = sz;
- }
- else {
- t = lua_check_text (L, 1);
- }
-
- if (t == NULL || t->start == NULL) {
- return luaL_error (L, "invalid arguments");
- }
-
-
-
- memset (&strm, 0, sizeof (strm));
- rc = deflateInit2 (&strm, Z_DEFAULT_COMPRESSION, Z_DEFLATED,
- MAX_WBITS + 16, MAX_MEM_LEVEL - 1, Z_DEFAULT_STRATEGY);
-
- if (rc != Z_OK) {
- return luaL_error (L, "cannot init zlib: %s", zError (rc));
- }
-
- sz = deflateBound (&strm, t->len);
-
- strm.avail_in = t->len;
- strm.next_in = (guchar *)t->start;
-
- res = lua_newuserdata (L, sizeof (*res));
- res->start = g_malloc (sz);
- res->flags = RSPAMD_TEXT_FLAG_OWN;
- rspamd_lua_setclass (L, "rspamd{text}", -1);
-
- p = (guchar *)res->start;
- remain = sz;
-
- while (strm.avail_in != 0) {
- strm.avail_out = remain;
- strm.next_out = p;
-
- rc = deflate (&strm, Z_FINISH);
-
- if (rc != Z_OK && rc != Z_BUF_ERROR) {
- if (rc == Z_STREAM_END) {
- break;
- }
- else {
- msg_err ("cannot compress data: %s", zError (rc));
- lua_pop (L, 1); /* Text will be freed here */
- lua_pushnil (L);
- deflateEnd (&strm);
-
- return 1;
- }
- }
-
- res->len = strm.total_out;
-
- if (strm.avail_out == 0 && strm.avail_in != 0) {
- /* Need to allocate more */
- remain = res->len;
- res->start = g_realloc ((gpointer)res->start, strm.avail_in + sz);
- sz = strm.avail_in + sz;
- p = (guchar *)res->start + remain;
- remain = sz - remain;
- }
- }
-
- deflateEnd (&strm);
- res->len = strm.total_out;
-
- return 1;
- }
-
-
- static gint
- lua_util_gzip_decompress (lua_State *L)
- {
- LUA_TRACE_POINT;
- struct rspamd_lua_text *t = NULL, *res, tmp;
- gsize sz;
- z_stream strm;
- gint rc;
- guchar *p;
- gsize remain;
-
- if (lua_type (L, 1) == LUA_TSTRING) {
- t = &tmp;
- t->start = lua_tolstring (L, 1, &sz);
- t->len = sz;
- }
- else {
- t = lua_check_text (L, 1);
- }
-
- if (t == NULL || t->start == NULL) {
- return luaL_error (L, "invalid arguments");
- }
-
- sz = t->len;
-
- memset (&strm, 0, sizeof (strm));
- /* windowBits +16 to decode gzip, zlib 1.2.0.4+ */
- rc = inflateInit2 (&strm, MAX_WBITS + 16);
-
- if (rc != Z_OK) {
- return luaL_error (L, "cannot init zlib");
- }
-
- strm.avail_in = t->len;
- strm.next_in = (guchar *)t->start;
-
- res = lua_newuserdata (L, sizeof (*res));
- res->start = g_malloc (sz);
- res->flags = RSPAMD_TEXT_FLAG_OWN;
- rspamd_lua_setclass (L, "rspamd{text}", -1);
-
- p = (guchar *)res->start;
- remain = sz;
-
- while (strm.avail_in != 0) {
- strm.avail_out = remain;
- strm.next_out = p;
-
- rc = inflate (&strm, Z_FINISH);
-
- if (rc != Z_OK && rc != Z_BUF_ERROR) {
- if (rc == Z_STREAM_END) {
- break;
- }
- else {
- msg_err ("cannot decompress data: %s", zError (rc));
- lua_pop (L, 1); /* Text will be freed here */
- lua_pushnil (L);
- inflateEnd (&strm);
-
- return 1;
- }
- }
-
- res->len = strm.total_out;
-
- if (strm.avail_out == 0 && strm.avail_in != 0) {
- /* Need to allocate more */
- remain = res->len;
- res->start = g_realloc ((gpointer)res->start, strm.avail_in + sz);
- sz = strm.avail_in + sz;
- p = (guchar *)res->start + remain;
- remain = sz - remain;
- }
- }
-
- inflateEnd (&strm);
- res->len = strm.total_out;
-
- return 2;
- }
-
-
- static gint
- lua_util_normalize_prob (lua_State *L)
- {
- LUA_TRACE_POINT;
- gdouble x, bias = 0.5;
-
- x = lua_tonumber (L, 1);
-
- if (lua_type (L, 2) == LUA_TNUMBER) {
- bias = lua_tonumber (L, 2);
- }
-
- lua_pushnumber (L, rspamd_normalize_probability (x, bias));
-
- return 1;
- }
-
- static gint
- lua_util_caseless_hash (lua_State *L)
- {
- LUA_TRACE_POINT;
- guint64 seed = 0xdeadbabe, h;
- struct rspamd_lua_text *t = NULL;
- gint64 *r;
- gsize sz;
-
- if (lua_type (L, 1) == LUA_TSTRING) {
- t = g_alloca (sizeof (*t));
- t->start = lua_tolstring (L, 1, &sz);
- t->len = sz;
- }
- else {
- t = lua_check_text (L, 1);
- }
-
- if (t == NULL || t->start == NULL) {
- return luaL_error (L, "invalid arguments");
- }
-
- if (lua_type (L, 2) == LUA_TNUMBER) {
- seed = lua_tonumber (L, 2);
- }
- else if (lua_type (L, 2) == LUA_TUSERDATA) {
- seed = lua_check_int64 (L, 2);
- }
-
- h = rspamd_icase_hash (t->start, t->len, seed);
- r = lua_newuserdata (L, sizeof (*r));
- *r = h;
- rspamd_lua_setclass (L, "rspamd{int64}", -1);
-
- return 1;
- }
-
- static gint
- lua_util_caseless_hash_fast (lua_State *L)
- {
- LUA_TRACE_POINT;
- guint64 seed = 0xdeadbabe, h;
- struct rspamd_lua_text *t = NULL;
- gsize sz;
- union {
- guint64 i;
- double d;
- } u;
-
- if (lua_type (L, 1) == LUA_TSTRING) {
- t = g_alloca (sizeof (*t));
- t->start = lua_tolstring (L, 1, &sz);
- t->len = sz;
- }
- else {
- t = lua_check_text (L, 1);
- }
-
- if (t == NULL || t->start == NULL) {
- return luaL_error (L, "invalid arguments");
- }
-
- if (lua_type (L, 2) == LUA_TNUMBER) {
- seed = lua_tonumber (L, 2);
- }
- else if (lua_type (L, 2) == LUA_TUSERDATA) {
- seed = lua_check_int64 (L, 2);
- }
-
- /*
- * Here, we loose entropy from 64 bits to 52 bits roughly, however,
- * it is still fine for practical applications
- */
-
- h = rspamd_icase_hash (t->start, t->len, seed);
- u.i = G_GUINT64_CONSTANT(0x3FF) << 52 | h >> 12;
- lua_pushnumber (L, u.d - 1.0);
-
- return 1;
- }
-
- static gint
- lua_util_is_utf_spoofed (lua_State *L)
- {
- LUA_TRACE_POINT;
- gsize l1, l2;
- gint ret, nres = 2;
- const gchar *s1 = lua_tolstring (L, 1, &l1),
- *s2 = lua_tolstring (L, 2, &l2);
- static USpoofChecker *spc, *spc_sgl;
- UErrorCode uc_err = U_ZERO_ERROR;
-
- if (s1 && s2) {
- if (spc == NULL) {
- spc = uspoof_open (&uc_err);
-
- if (uc_err != U_ZERO_ERROR) {
- msg_err ("cannot init spoof checker: %s", u_errorName (uc_err));
- lua_pushboolean (L, false);
-
- return 1;
- }
- }
-
- ret = uspoof_areConfusableUTF8 (spc, s1, l1, s2, l2, &uc_err);
- }
- else if (s1) {
- /* We have just s1, not s2 */
- if (spc_sgl == NULL) {
- spc_sgl = uspoof_open (&uc_err);
-
- if (uc_err != U_ZERO_ERROR) {
- msg_err ("cannot init spoof checker: %s", u_errorName (uc_err));
- lua_pushboolean (L, false);
-
- return 1;
- }
-
- uspoof_setChecks (spc_sgl,
- USPOOF_INVISIBLE | USPOOF_MIXED_SCRIPT_CONFUSABLE | USPOOF_ANY_CASE,
- &uc_err);
- }
-
- ret = uspoof_checkUTF8 (spc_sgl, s1, l1, NULL, &uc_err);
- }
- else {
- return luaL_error (L, "invalid arguments");
- }
-
- lua_pushboolean (L, !!(ret != 0));
-
- switch (ret) {
- case 0:
- nres = 1;
- break;
- case USPOOF_SINGLE_SCRIPT_CONFUSABLE:
- lua_pushstring (L, "single");
- break;
- case USPOOF_MIXED_SCRIPT_CONFUSABLE:
- lua_pushstring (L, "multiple");
- break;
- case USPOOF_WHOLE_SCRIPT_CONFUSABLE:
- lua_pushstring (L, "whole");
- break;
- default:
- lua_pushstring (L, "unknown");
- break;
- }
-
- return nres;
- }
-
- static gint
- lua_util_get_hostname (lua_State *L)
- {
- LUA_TRACE_POINT;
- gchar *hostbuf;
- gsize hostlen;
-
- hostlen = sysconf (_SC_HOST_NAME_MAX);
-
- if (hostlen <= 0) {
- hostlen = 256;
- }
- else {
- hostlen ++;
- }
-
- hostbuf = g_alloca (hostlen);
- memset (hostbuf, 0, hostlen);
- gethostname (hostbuf, hostlen - 1);
-
- lua_pushstring (L, hostbuf);
-
- return 1;
- }
-
- static gint
- lua_util_parse_content_type (lua_State *L)
- {
- LUA_TRACE_POINT;
- gsize len;
- const gchar *ct_str = luaL_checklstring (L, 1, &len);
- rspamd_mempool_t *pool = rspamd_lua_check_mempool (L, 2);
- struct rspamd_content_type *ct;
-
- if (!ct_str || !pool) {
- return luaL_error (L, "invalid arguments");
- }
-
- ct = rspamd_content_type_parse (ct_str, len, pool);
-
- if (ct == NULL) {
- lua_pushnil (L);
- }
- else {
- GHashTableIter it;
- gpointer k, v;
-
- lua_createtable (L, 0, 4 + (ct->attrs ? g_hash_table_size (ct->attrs) : 0));
-
- if (ct->type.len > 0) {
- lua_pushstring (L, "type");
- lua_pushlstring (L, ct->type.begin, ct->type.len);
- lua_settable (L, -3);
- }
-
- if (ct->subtype.len > 0) {
- lua_pushstring (L, "subtype");
- lua_pushlstring (L, ct->subtype.begin, ct->subtype.len);
- lua_settable (L, -3);
- }
-
- if (ct->charset.len > 0) {
- lua_pushstring (L, "charset");
- lua_pushlstring (L, ct->charset.begin, ct->charset.len);
- lua_settable (L, -3);
- }
-
- if (ct->orig_boundary.len > 0) {
- lua_pushstring (L, "boundary");
- lua_pushlstring (L, ct->orig_boundary.begin, ct->orig_boundary.len);
- lua_settable (L, -3);
- }
-
- if (ct->attrs) {
- g_hash_table_iter_init (&it, ct->attrs);
-
- while (g_hash_table_iter_next (&it, &k, &v)) {
- struct rspamd_content_type_param *param =
- (struct rspamd_content_type_param *)v, *cur;
- guint i = 1;
-
- lua_pushlstring (L, param->name.begin, param->name.len);
- lua_createtable (L, 1, 0);
-
- DL_FOREACH (param, cur) {
- lua_pushlstring (L, cur->value.begin, cur->value.len);
- lua_rawseti (L, -2, i++);
- }
-
- lua_settable (L, -3);
- }
- }
- }
-
- return 1;
- }
-
-
- static gint
- lua_util_mime_header_encode (lua_State *L)
- {
- LUA_TRACE_POINT;
- gsize len;
- const gchar *hdr = luaL_checklstring (L, 1, &len);
- gchar *encoded;
-
- if (!hdr) {
- return luaL_error (L, "invalid arguments");
- }
-
- encoded = rspamd_mime_header_encode (hdr, len);
- lua_pushstring (L, encoded);
- g_free (encoded);
-
- return 1;
- }
-
- static gint
- lua_util_is_valid_utf8 (lua_State *L)
- {
- LUA_TRACE_POINT;
- const gchar *str;
- gsize len;
-
- str = lua_tolstring (L, 1, &len);
-
- if (str) {
- lua_pushboolean (L, g_utf8_validate (str, len, NULL));
- }
- else {
- return luaL_error (L, "invalid arguments");
- }
-
- return 1;
- }
-
- static gint
- lua_util_has_obscured_unicode (lua_State *L)
- {
- LUA_TRACE_POINT;
- const gchar *str;
- gsize len;
- gint32 i = 0, prev_i;
- UChar32 uc;
-
- str = lua_tolstring (L, 1, &len);
-
- while (i < len) {
- prev_i = i;
- U8_NEXT (str, i, len, uc);
-
- if (uc > 0) {
- if (IS_OBSCURED_CHAR (uc)) {
- lua_pushboolean (L, true);
- lua_pushnumber (L, uc); /* Character */
- lua_pushnumber (L, prev_i); /* Offset */
-
- return 3;
- }
- }
- }
-
- lua_pushboolean (L, false);
-
- return 1;
- }
-
- static gint
- lua_util_readline (lua_State *L)
- {
- LUA_TRACE_POINT;
- const gchar *prompt = "";
- gchar *input;
-
- if (lua_type (L, 1) == LUA_TSTRING) {
- prompt = lua_tostring (L, 1);
- }
-
- input = linenoise (prompt);
-
- if (input) {
- lua_pushstring (L, input);
- linenoiseHistoryAdd (input);
- linenoiseFree (input);
- }
- else {
- lua_pushnil (L);
- }
-
- return 1;
- }
-
- static gint
- lua_util_readpassphrase (lua_State *L)
- {
- LUA_TRACE_POINT;
- gchar test_password[8192];
- gsize r;
-
- r = rspamd_read_passphrase (test_password, sizeof (test_password), 0, NULL);
-
- if (r > 0) {
- lua_pushlstring (L, test_password, r);
- }
- else {
- lua_pushnil (L);
- }
-
- /* In fact, we still pass it to Lua which is not very safe */
- rspamd_explicit_memzero (test_password, sizeof (test_password));
-
- return 1;
- }
-
- static gint
- lua_util_file_exists (lua_State *L)
- {
- LUA_TRACE_POINT;
- const gchar *fname = luaL_checkstring (L, 1);
- gint serrno;
-
- if (fname) {
- if (access (fname, R_OK) == -1) {
- serrno = errno;
- lua_pushboolean (L, false);
- lua_pushstring (L, strerror (serrno));
- }
- else {
- lua_pushboolean (L, true);
- lua_pushnil (L);
- }
- }
- else {
- return luaL_error (L, "invalid arguments");
- }
-
- return 2;
- }
-
- static gint
- lua_util_mkdir (lua_State *L)
- {
- LUA_TRACE_POINT;
- const gchar *dname = luaL_checkstring (L, 1);
- gboolean recursive = FALSE;
- gint r = -1;
-
- if (dname) {
- if (lua_isboolean (L, 2)) {
- recursive = lua_toboolean (L, 2);
- }
-
- if (recursive) {
- char path[PATH_MAX];
- gsize len, i;
-
- len = rspamd_strlcpy (path, dname, sizeof (path));
-
- /* Strip last / */
- if (path[len - 1] == '/') {
- path[len - 1] = '\0';
- len --;
- }
-
- for (i = 1; i < len; i ++) {
- if (path[i] == '/') {
- path[i] = '\0';
-
- errno = 0;
- r = mkdir (path, S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH);
-
- if (r == -1 && errno != EEXIST) {
- break;
- }
-
- path[i] = '/';
- }
- }
-
- /* Final path component */
- r = mkdir (path, S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH);
- }
- else {
- r = mkdir (dname, S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH);
- }
-
- if (r == -1 && errno != EEXIST) {
- lua_pushboolean (L, false);
- lua_pushstring (L, strerror (errno));
-
- return 2;
- }
-
- lua_pushboolean (L, true);
- }
- else {
- return luaL_error (L, "invalid arguments");
- }
-
- return 1;
- }
-
-
- static gint
- lua_util_umask (lua_State *L)
- {
- LUA_TRACE_POINT;
- mode_t mask = 0, old;
-
- if (lua_type (L, 1) == LUA_TSTRING) {
- const gchar *str = lua_tostring (L, 1);
-
- if (str[0] == '0') {
- /* e.g. '022' */
- mask = strtol (str, NULL, 8);
- }
- else {
- /* XXX: implement modestring parsing at some point */
- return luaL_error (L, "invalid arguments");
- }
- }
- else if (lua_type (L, 1) == LUA_TNUMBER) {
- mask = lua_tonumber (L, 1);
- }
- else {
- return luaL_error (L, "invalid arguments");
- }
-
- old = umask (mask);
-
- lua_pushinteger (L, old);
-
- return 1;
- }
-
- static gint
- lua_util_isatty (lua_State *L)
- {
- LUA_TRACE_POINT;
- if (isatty (STDOUT_FILENO)) {
- lua_pushboolean (L, true);
- }
- else {
- lua_pushboolean (L, false);
- }
-
- return 1;
- }
-
- /* Backport from Lua 5.3 */
-
- /******************************************************************************
- * Copyright (C) 1994-2016 Lua.org, PUC-Rio.
- *
- * Permission is hereby granted, free of charge, to any person obtaining
- * a copy of this software and associated documentation files (the
- * "Software"), to deal in the Software without restriction, including
- * without limitation the rights to use, copy, modify, merge, publish,
- * distribute, sublicense, and/or sell copies of the Software, and to
- * permit persons to whom the Software is furnished to do so, subject to
- * the following conditions:
- *
- * The above copyright notice and this permission notice shall be
- * included in all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
- * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
- * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
- * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
- * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
- * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
- * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- ******************************************************************************/
-
- /*
- ** {======================================================
- ** PACK/UNPACK
- ** =======================================================
- */
-
-
- /* value used for padding */
- #if !defined(LUA_PACKPADBYTE)
- #define LUA_PACKPADBYTE 0x00
- #endif
-
- /* maximum size for the binary representation of an integer */
- #define MAXINTSIZE 16
-
- /* number of bits in a character */
- #define NB CHAR_BIT
-
- /* mask for one character (NB 1's) */
- #define MC ((1 << NB) - 1)
-
- /* size of a lua_Integer */
- #define SZINT ((int)sizeof(lua_Integer))
-
- #define MAX_SIZET ((size_t)(~(size_t)0))
-
- #define MAXSIZE \
- (sizeof(size_t) < sizeof(int) ? MAX_SIZET : (size_t)(INT_MAX))
-
-
- /* dummy union to get native endianness */
- static const union {
- int dummy;
- char little; /* true if machine is little endian */
- } nativeendian = {1};
-
-
- /* dummy structure to get native alignment requirements */
- struct cD {
- char c;
- union {
- double d;
- void *p;
- lua_Integer i;
- lua_Number n;
- } u;
- };
-
- #define MAXALIGN (offsetof(struct cD, u))
-
- /*
- ** Union for serializing floats
- */
- typedef union Ftypes {
- float f;
- double d;
- lua_Number n;
- char buff[5 * sizeof (lua_Number)]; /* enough for any float type */
- } Ftypes;
-
-
- /*
- ** information to pack/unpack stuff
- */
- typedef struct Header {
- lua_State *L;
- int islittle;
- int maxalign;
- } Header;
-
- /*
- ** options for pack/unpack
- */
- typedef enum KOption {
- Kint, /* signed integers */
- Kuint, /* unsigned integers */
- Kfloat, /* floating-point numbers */
- Kchar, /* fixed-length strings */
- Kstring, /* strings with prefixed length */
- Kzstr, /* zero-terminated strings */
- Kpadding, /* padding */
- Kpaddalign, /* padding for alignment */
- Knop /* no-op (configuration or spaces) */
- } KOption;
-
- #if LUA_VERSION_NUM < 503
- #define lua_Unsigned size_t
-
- typedef struct luaL_Buffer_53 {
- luaL_Buffer b; /* make incorrect code crash! */
- char *ptr;
- size_t nelems;
- size_t capacity;
- lua_State *L2;
- } luaL_Buffer_53;
-
- #define luaL_Buffer luaL_Buffer_53
- #define COMPAT53_PREFIX lua
- #undef COMPAT53_API
- #if defined(__GNUC__) || defined(__clang__)
- # define COMPAT53_API __attribute__((__unused__)) static
- #else
- # define COMPAT53_API static
- #endif
-
- #define COMPAT53_CONCAT_HELPER(a, b) a##b
- #define COMPAT53_CONCAT(a, b) COMPAT53_CONCAT_HELPER(a, b)
-
- #define luaL_buffinit COMPAT53_CONCAT(COMPAT53_PREFIX, _buffinit_53)
- COMPAT53_API void luaL_buffinit (lua_State *L, luaL_Buffer_53 *B);
- #define luaL_prepbuffsize COMPAT53_CONCAT(COMPAT53_PREFIX, _prepbufsize_53)
- COMPAT53_API char *luaL_prepbuffsize (luaL_Buffer_53 *B, size_t s);
- #define luaL_addlstring COMPAT53_CONCAT(COMPAT53_PREFIX, _addlstring_53)
- COMPAT53_API void luaL_addlstring (luaL_Buffer_53 *B, const char *s, size_t l);
- #define luaL_addvalue COMPAT53_CONCAT(COMPAT53_PREFIX, _addvalue_53)
- COMPAT53_API void luaL_addvalue (luaL_Buffer_53 *B);
- #define luaL_pushresult COMPAT53_CONCAT(COMPAT53_PREFIX, _pushresult_53)
- COMPAT53_API void luaL_pushresult (luaL_Buffer_53 *B);
- #undef luaL_buffinitsize
- #define luaL_buffinitsize(L, B, s) \
- (luaL_buffinit(L, B), luaL_prepbuffsize(B, s))
-
- #undef luaL_prepbuffer
- #define luaL_prepbuffer(B) \
- luaL_prepbuffsize(B, LUAL_BUFFERSIZE)
-
- #undef luaL_addchar
- #define luaL_addchar(B, c) \
- ((void)((B)->nelems < (B)->capacity || luaL_prepbuffsize(B, 1)), \
- ((B)->ptr[(B)->nelems++] = (c)))
-
- #undef luaL_addsize
- #define luaL_addsize(B, s) \
- ((B)->nelems += (s))
-
- #undef luaL_addstring
- #define luaL_addstring(B, s) \
- luaL_addlstring(B, s, strlen(s))
-
- #undef luaL_pushresultsize
- #define luaL_pushresultsize(B, s) \
- (luaL_addsize(B, s), luaL_pushresult(B))
-
- COMPAT53_API void
- luaL_buffinit (lua_State *L, luaL_Buffer_53 *B)
- {
- /* make it crash if used via pointer to a 5.1-style luaL_Buffer */
- #if LUA_VERSION_NUM < 502
- B->b.p = NULL;
- B->b.L = NULL;
- B->b.lvl = 0;
- /* reuse the buffer from the 5.1-style luaL_Buffer though! */
- B->ptr = B->b.buffer;
- B->nelems = 0;
- #elif LUA_VERSION_NUM == 502
- B->ptr = B->b.b;
- B->nelems = B->b.n;
- #endif
- B->capacity = LUAL_BUFFERSIZE;
- B->L2 = L;
- }
-
-
- COMPAT53_API char *
- luaL_prepbuffsize (luaL_Buffer_53 *B, size_t s)
- {
- if (B->capacity - B->nelems < s) { /* needs to grow */
- char *newptr = NULL;
- size_t newcap = B->capacity * 2;
- if (newcap - B->nelems < s)
- newcap = B->nelems + s;
- if (newcap < B->capacity) /* overflow */
- luaL_error (B->L2, "buffer too large");
- newptr = (char *) lua_newuserdata (B->L2, newcap);
- memcpy(newptr, B->ptr, B->nelems);
- #if LUA_VERSION_NUM < 502
- if (B->ptr != B->b.buffer)
- #elif LUA_VERSION_NUM == 502
- if (B->ptr != B->b.b)
- #endif
- lua_replace (B->L2, -2); /* remove old buffer */
- B->ptr = newptr;
- B->capacity = newcap;
- }
- return B->ptr + B->nelems;
- }
-
-
- COMPAT53_API void
- luaL_addlstring (luaL_Buffer_53 *B, const char *s, size_t l)
- {
- memcpy(luaL_prepbuffsize (B, l), s, l);
- luaL_addsize(B, l);
- }
-
-
- COMPAT53_API void
- luaL_addvalue (luaL_Buffer_53 *B)
- {
- size_t len = 0;
- const char *s = lua_tolstring (B->L2, -1, &len);
- if (!s)
- luaL_error (B->L2, "cannot convert value to string");
- #if LUA_VERSION_NUM < 502
- if (B->ptr != B->b.buffer)
- #elif LUA_VERSION_NUM == 502
- if (B->ptr != B->b.b)
- #endif
- lua_insert (B->L2, -2); /* userdata buffer must be at stack top */
- luaL_addlstring (B, s, len);
- #if LUA_VERSION_NUM < 502
- lua_remove (B->L2, B->ptr != B->b.buffer ? -2 : -1);
- #elif LUA_VERSION_NUM == 502
- lua_remove (B->L2, B->ptr != B->b.b ? -2 : -1);
- #endif
- }
-
-
- COMPAT53_API void
- luaL_pushresult (luaL_Buffer_53 *B)
- {
- lua_pushlstring (B->L2, B->ptr, B->nelems);
- #if LUA_VERSION_NUM < 502
- if (B->ptr != B->b.buffer)
- #elif LUA_VERSION_NUM == 502
- if (B->ptr != B->b.b)
- #endif
- lua_replace (B->L2, -2); /* remove userdata buffer */
- }
-
- #endif
-
- /*
- ** Read an integer numeral from string 'fmt' or return 'df' if
- ** there is no numeral
- */
- static int
- digit (int c)
- {
- return '0' <= c && c <= '9';
- }
-
- static int
- getnum (const char **fmt, int df)
- {
- if (!digit (**fmt)) /* no number? */
- return df; /* return default value */
- else {
- int a = 0;
- do {
- a = a * 10 + (*((*fmt)++) - '0');
- } while (digit (**fmt) && a <= ((int) MAXSIZE - 9) / 10);
- return a;
- }
- }
-
-
- /*
- ** Read an integer numeral and raises an error if it is larger
- ** than the maximum size for integers.
- */
- static int
- getnumlimit (Header *h, const char **fmt, int df)
- {
- int sz = getnum (fmt, df);
- if (sz > MAXINTSIZE || sz <= 0)
- luaL_error (h->L, "integral size (%d) out of limits [1,%d]",
- sz, MAXINTSIZE);
- return sz;
- }
-
-
- /*
- ** Initialize Header
- */
- static void
- initheader (lua_State *L, Header *h)
- {
- h->L = L;
- h->islittle = nativeendian.little;
- h->maxalign = 1;
- }
-
-
- /*
- ** Read and classify next option. 'size' is filled with option's size.
- */
- static KOption
- getoption (Header *h, const char **fmt, int *size)
- {
- int opt = *((*fmt)++);
- *size = 0; /* default */
- switch (opt) {
- case 'b':
- *size = sizeof (char);
- return Kint;
- case 'B':
- *size = sizeof (char);
- return Kuint;
- case 'h':
- *size = sizeof (short);
- return Kint;
- case 'H':
- *size = sizeof (short);
- return Kuint;
- case 'l':
- *size = sizeof (long);
- return Kint;
- case 'L':
- *size = sizeof (long);
- return Kuint;
- case 'j':
- *size = sizeof (lua_Integer);
- return Kint;
- case 'J':
- *size = sizeof (lua_Integer);
- return Kuint;
- case 'T':
- *size = sizeof (size_t);
- return Kuint;
- case 'f':
- *size = sizeof (float);
- return Kfloat;
- case 'd':
- *size = sizeof (double);
- return Kfloat;
- case 'n':
- *size = sizeof (lua_Number);
- return Kfloat;
- case 'i':
- *size = getnumlimit (h, fmt, sizeof (int));
- return Kint;
- case 'I':
- *size = getnumlimit (h, fmt, sizeof (int));
- return Kuint;
- case 's':
- *size = getnumlimit (h, fmt, sizeof (size_t));
- return Kstring;
- case 'c':
- *size = getnum (fmt, -1);
- if (*size == -1)
- luaL_error (h->L, "missing size for format option 'c'");
- return Kchar;
- case 'z':
- return Kzstr;
- case 'x':
- *size = 1;
- return Kpadding;
- case 'X':
- return Kpaddalign;
- case ' ':
- break;
- case '<':
- h->islittle = 1;
- break;
- case '>':
- h->islittle = 0;
- break;
- case '=':
- h->islittle = nativeendian.little;
- break;
- case '!':
- h->maxalign = getnumlimit (h, fmt, MAXALIGN);
- break;
- default:
- luaL_error (h->L, "invalid format option '%c'", opt);
- }
- return Knop;
- }
-
-
- /*
- ** Read, classify, and fill other details about the next option.
- ** 'psize' is filled with option's size, 'notoalign' with its
- ** alignment requirements.
- ** Local variable 'size' gets the size to be aligned. (Kpadal option
- ** always gets its full alignment, other options are limited by
- ** the maximum alignment ('maxalign'). Kchar option needs no alignment
- ** despite its size.
- */
- static KOption
- getdetails (Header *h, size_t totalsize,
- const char **fmt, int *psize, int *ntoalign)
- {
- KOption opt = getoption (h, fmt, psize);
- int align = *psize; /* usually, alignment follows size */
- if (opt == Kpaddalign) { /* 'X' gets alignment from following option */
- if (**fmt == '\0' || getoption (h, fmt, &align) == Kchar || align == 0)
- luaL_argerror (h->L, 1, "invalid next option for option 'X'");
- }
- if (align <= 1 || opt == Kchar) /* need no alignment? */
- *ntoalign = 0;
- else {
- if (align > h->maxalign) /* enforce maximum alignment */
- align = h->maxalign;
- if ((align & (align - 1)) != 0) /* is 'align' not a power of 2? */
- luaL_argerror (h->L, 1, "format asks for alignment not power of 2");
- *ntoalign = (align - (int) (totalsize & (align - 1))) & (align - 1);
- }
- return opt;
- }
-
-
- /*
- ** Pack integer 'n' with 'size' bytes and 'islittle' endianness.
- ** The final 'if' handles the case when 'size' is larger than
- ** the size of a Lua integer, correcting the extra sign-extension
- ** bytes if necessary (by default they would be zeros).
- */
- static void
- packint (luaL_Buffer *b, lua_Unsigned n,
- int islittle, int size, int neg)
- {
- char *buff = luaL_prepbuffsize (b, size);
- int i;
- buff[islittle ? 0 : size - 1] = (char) (n & MC); /* first byte */
- for (i = 1; i < size; i++) {
- n >>= NB;
- buff[islittle ? i : size - 1 - i] = (char) (n & MC);
- }
- if (neg && size > SZINT) { /* negative number need sign extension? */
- for (i = SZINT; i < size; i++) /* correct extra bytes */
- buff[islittle ? i : size - 1 - i] = (char) MC;
- }
- luaL_addsize(b, size); /* add result to buffer */
- }
-
-
- /*
- ** Copy 'size' bytes from 'src' to 'dest', correcting endianness if
- ** given 'islittle' is different from native endianness.
- */
- static void
- copywithendian (volatile char *dest, volatile const char *src,
- int size, int islittle)
- {
- if (islittle == nativeendian.little) {
- while (size-- != 0)
- *(dest++) = *(src++);
- }
- else {
- dest += size - 1;
- while (size-- != 0)
- *(dest--) = *(src++);
- }
- }
-
-
- static int
- lua_util_pack (lua_State *L)
- {
- luaL_Buffer b;
- Header h;
- const char *fmt = luaL_checkstring(L, 1); /* format string */
- int arg = 1; /* current argument to pack */
- size_t totalsize = 0; /* accumulate total size of result */
- initheader (L, &h);
- lua_pushnil (L); /* mark to separate arguments from string buffer */
- luaL_buffinit (L, &b);
-
- while (*fmt != '\0') {
- int size, ntoalign;
- KOption opt = getdetails (&h, totalsize, &fmt, &size, &ntoalign);
- totalsize += ntoalign + size;
- while (ntoalign-- > 0)
- luaL_addchar(&b, LUA_PACKPADBYTE); /* fill alignment */
- arg++;
- switch (opt) {
- case Kint: { /* signed integers */
- lua_Integer n = luaL_checkinteger (L, arg);
- if (size < SZINT) { /* need overflow check? */
- lua_Integer lim = (lua_Integer) 1 << ((size * NB) - 1);
- luaL_argcheck(L, -lim <= n && n < lim, arg, "integer overflow");
- }
- packint (&b, (lua_Unsigned) n, h.islittle, size, (n < 0));
- break;
- }
- case Kuint: { /* unsigned integers */
- lua_Integer n = luaL_checkinteger (L, arg);
- if (size < SZINT) /* need overflow check? */
- luaL_argcheck(L,
- (lua_Unsigned) n < ((lua_Unsigned) 1 << (size * NB)),
- arg,
- "unsigned overflow");
- packint (&b, (lua_Unsigned) n, h.islittle, size, 0);
- break;
- }
- case Kfloat: { /* floating-point options */
- volatile Ftypes u;
- char *buff = luaL_prepbuffsize (&b, size);
- lua_Number n = luaL_checknumber (L, arg); /* get argument */
- if (size == sizeof (u.f))
- u.f = (float) n; /* copy it into 'u' */
- else if (size == sizeof (u.d))
- u.d = (double) n;
- else
- u.n = n;
- /* move 'u' to final result, correcting endianness if needed */
- copywithendian (buff, u.buff, size, h.islittle);
- luaL_addsize(&b, size);
- break;
- }
- case Kchar: { /* fixed-size string */
- size_t len;
- const char *s = luaL_checklstring (L, arg, &len);
- if ((size_t) size <=
- len) /* string larger than (or equal to) needed? */
- luaL_addlstring (&b,
- s,
- size); /* truncate string to asked size */
- else { /* string smaller than needed */
- luaL_addlstring (&b, s, len); /* add it all */
- while (len++ < (size_t) size) /* pad extra space */
- luaL_addchar(&b, LUA_PACKPADBYTE);
- }
- break;
- }
- case Kstring: { /* strings with length count */
- size_t len;
- const char *s = luaL_checklstring (L, arg, &len);
- luaL_argcheck(L, size >= (int) sizeof (size_t) ||
- len < ((size_t) 1 << (size * NB)),
- arg, "string length does not fit in given size");
- packint (&b,
- (lua_Unsigned) len,
- h.islittle,
- size,
- 0); /* pack length */
- luaL_addlstring (&b, s, len);
- totalsize += len;
- break;
- }
- case Kzstr: { /* zero-terminated string */
- size_t len;
- const char *s = luaL_checklstring (L, arg, &len);
- luaL_argcheck(L, strlen (s) == len, arg, "string contains zeros");
- luaL_addlstring (&b, s, len);
- luaL_addchar(&b, '\0'); /* add zero at the end */
- totalsize += len + 1;
- break;
- }
- case Kpadding:
- luaL_addchar(&b, LUA_PACKPADBYTE); /* FALLTHROUGH */
- case Kpaddalign:
- case Knop:
- arg--; /* undo increment */
- break;
- }
- }
- luaL_pushresult (&b);
- return 1;
- }
-
-
- static int
- lua_util_packsize (lua_State *L)
- {
- Header h;
- const char *fmt = luaL_checkstring(L, 1); /* format string */
- size_t totalsize = 0; /* accumulate total size of result */
- initheader (L, &h);
- while (*fmt != '\0') {
- int size, ntoalign;
- KOption opt = getdetails (&h, totalsize, &fmt, &size, &ntoalign);
- size += ntoalign; /* total space used by option */
- luaL_argcheck(L, totalsize <= MAXSIZE - size, 1,
- "format result too large");
- totalsize += size;
- switch (opt) {
- case Kstring: /* strings with length count */
- case Kzstr: /* zero-terminated string */
- luaL_argerror (L, 1, "variable-length format");
- /* call never return, but to avoid warnings: *//* FALLTHROUGH */
- default:
- break;
- }
- }
- lua_pushinteger (L, (lua_Integer) totalsize);
- return 1;
- }
-
-
- /*
- ** Unpack an integer with 'size' bytes and 'islittle' endianness.
- ** If size is smaller than the size of a Lua integer and integer
- ** is signed, must do sign extension (propagating the sign to the
- ** higher bits); if size is larger than the size of a Lua integer,
- ** it must check the unread bytes to see whether they do not cause an
- ** overflow.
- */
- static lua_Integer
- unpackint (lua_State *L, const char *str,
- int islittle, int size, int issigned)
- {
- lua_Unsigned res = 0;
- int i;
- int limit = (size <= SZINT) ? size : SZINT;
- for (i = limit - 1; i >= 0; i--) {
- res <<= NB;
- res |= (lua_Unsigned) (
- unsigned char)str[islittle ? i : size - 1 - i];
- }
- if (size < SZINT) { /* real size smaller than lua_Integer? */
- if (issigned) { /* needs sign extension? */
- lua_Unsigned mask = (lua_Unsigned) 1 << (size * NB - 1);
- res = ((res ^ mask) - mask); /* do sign extension */
- }
- }
- else if (size > SZINT) { /* must check unread bytes */
- int mask = (!issigned || (lua_Integer) res >= 0) ? 0 : MC;
- for (i = limit; i < size; i++) {
- if ((unsigned char) str[islittle ? i : size - 1 - i] != mask)
- luaL_error (L,
- "%d-byte integer does not fit into Lua Integer",
- size);
- }
- }
- return (lua_Integer) res;
- }
-
- static lua_Integer
- posrelat (lua_Integer pos, size_t len)
- {
- if (pos >= 0)
- return pos;
- else if (0u - (size_t) pos > len)
- return 0;
- else
- return (lua_Integer) len + pos + 1;
- }
-
- static int
- lua_util_unpack (lua_State *L)
- {
- Header h;
- const char *fmt = luaL_checkstring(L, 1);
- size_t ld;
- const char *data = luaL_checklstring (L, 2, &ld);
- size_t pos = (size_t) posrelat (luaL_optinteger (L, 3, 1), ld) - 1;
- int n = 0; /* number of results */
- luaL_argcheck(L, pos <= ld, 3, "initial position out of string");
- initheader (L, &h);
- while (*fmt != '\0') {
- int size, ntoalign;
- KOption opt = getdetails (&h, pos, &fmt, &size, &ntoalign);
- if ((size_t) ntoalign + size > ~pos || pos + ntoalign + size > ld)
- luaL_argerror (L, 2, "data string too short");
- pos += ntoalign; /* skip alignment */
- /* stack space for item + next position */
- luaL_checkstack (L, 2, "too many results");
- n++;
- switch (opt) {
- case Kint:
- case Kuint: {
- lua_Integer res = unpackint (L, data + pos, h.islittle, size,
- (opt == Kint));
- lua_pushinteger (L, res);
- break;
- }
- case Kfloat: {
- volatile Ftypes u;
- lua_Number num;
- copywithendian (u.buff, data + pos, size, h.islittle);
- if (size == sizeof (u.f))
- num = (lua_Number) u.f;
- else if (size == sizeof (u.d))
- num = (lua_Number) u.d;
- else
- num = u.n;
- lua_pushnumber (L, num);
- break;
- }
- case Kchar: {
- lua_pushlstring (L, data + pos, size);
- break;
- }
- case Kstring: {
- size_t len = (size_t) unpackint (L,
- data + pos,
- h.islittle,
- size,
- 0);
- luaL_argcheck(L,
- pos + len + size <= ld,
- 2,
- "data string too short");
- lua_pushlstring (L, data + pos + size, len);
- pos += len; /* skip string */
- break;
- }
- case Kzstr: {
- size_t len = (int) strlen (data + pos);
- lua_pushlstring (L, data + pos, len);
- pos += len + 1; /* skip string plus final '\0' */
- break;
- }
- case Kpaddalign:
- case Kpadding:
- case Knop:
- n--; /* undo increment */
- break;
- }
- pos += size;
- }
- lua_pushinteger (L, pos + 1); /* next position */
- return n + 1;
- }
-
- static gint
- lua_load_util (lua_State * L)
- {
- lua_newtable (L);
- luaL_register (L, NULL, utillib_f);
-
- return 1;
- }
-
- void
- luaopen_util (lua_State * L)
- {
- rspamd_lua_new_class (L, "rspamd{ev_base}", ev_baselib_m);
- lua_pop (L, 1);
- rspamd_lua_new_class (L, "rspamd{int64}", int64lib_m);
- lua_pop (L, 1);
- rspamd_lua_add_preload (L, "rspamd_util", lua_load_util);
- }
-
- static int
- lua_int64_tostring (lua_State *L)
- {
- gint64 n = lua_check_int64 (L, 1);
- gchar buf[32];
-
- rspamd_snprintf (buf, sizeof (buf), "%uL", n);
- lua_pushstring (L, buf);
-
- return 1;
- }
-
- static int
- lua_int64_tonumber (lua_State *L)
- {
- gint64 n = lua_check_int64 (L, 1);
- gdouble d;
-
- d = n;
- lua_pushnumber (L, d);
-
- return 1;
- }
-
- static int
- lua_int64_hex (lua_State *L)
- {
- gint64 n = lua_check_int64 (L, 1);
- gchar buf[32];
-
- rspamd_snprintf (buf, sizeof (buf), "%XL", n);
- lua_pushstring (L, buf);
-
- return 1;
- }
-
- static int
- lua_ev_base_loop (lua_State *L)
- {
- int flags = 0;
- struct event_base *ev_base;
-
- ev_base = lua_check_ev_base (L, 1);
- if (lua_isnumber (L, 2)) {
- flags = lua_tonumber (L, 2);
- }
-
- int ret = event_base_loop (ev_base, flags);
- lua_pushinteger (L, ret);
-
- return 1;
- }
|