12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632 |
- /*-
- * 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 "config.h"
- #include "util.h"
- #include "cryptobox.h"
- #include "url.h"
- #include "str_util.h"
- #include "logger.h"
- #include "contrib/t1ha/t1ha.h"
- #include <unicode/uversion.h>
- #include <unicode/ucnv.h>
- #if U_ICU_VERSION_MAJOR_NUM >= 44
- #include <unicode/unorm2.h>
- #endif
- #include <math.h>
-
- #ifdef __x86_64__
- #include <immintrin.h>
- #endif
-
- #include "contrib/fastutf8/fastutf8.h"
-
- const guchar lc_map[256] = {
- 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
- 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
- 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
- 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
- 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
- 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
- 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
- 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
- 0x40, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,
- 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
- 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77,
- 0x78, 0x79, 0x7a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f,
- 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,
- 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
- 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77,
- 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f,
- 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
- 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
- 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
- 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f,
- 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
- 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf,
- 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7,
- 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf,
- 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
- 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf,
- 0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7,
- 0xd8, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf,
- 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7,
- 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef,
- 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
- 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff
- };
-
- guint
- rspamd_str_lc (gchar *str, guint size)
- {
- guint leftover = size % 4;
- guint fp, i;
- const uint8_t* s = (const uint8_t*) str;
- gchar *dest = str;
- guchar c1, c2, c3, c4;
-
- fp = size - leftover;
-
- for (i = 0; i != fp; i += 4) {
- c1 = s[i], c2 = s[i + 1], c3 = s[i + 2], c4 = s[i + 3];
- dest[0] = lc_map[c1];
- dest[1] = lc_map[c2];
- dest[2] = lc_map[c3];
- dest[3] = lc_map[c4];
- dest += 4;
- }
-
- switch (leftover) {
- case 3:
- *dest++ = lc_map[(guchar)str[i++]];
- /* FALLTHRU */
- case 2:
- *dest++ = lc_map[(guchar)str[i++]];
- /* FALLTHRU */
- case 1:
- *dest = lc_map[(guchar)str[i]];
- }
-
- return size;
- }
-
- gsize
- rspamd_str_copy_lc (const gchar *src, gchar *dst, gsize size)
- {
- gchar *d = dst;
-
- /* Find aligned start */
- while ((0xf & (uintptr_t)src) && size > 0) {
- *d++ = lc_map[(guchar)*src++];
- size --;
- }
-
- /* Aligned start in src */
- #ifdef __x86_64__
- while (size >= 16) {
- __m128i sv = _mm_load_si128((const __m128i*)src);
- /* From A */
- __m128i rangeshift = _mm_sub_epi8(sv, _mm_set1_epi8((char)('A'+128)));
- /* To Z */
- __m128i nomodify = _mm_cmpgt_epi8(rangeshift, _mm_set1_epi8(-128 + 25));
- /* ^ ' ' */
- __m128i flip = _mm_andnot_si128(nomodify, _mm_set1_epi8(0x20));
- __m128i uc = _mm_xor_si128(sv, flip);
- _mm_storeu_si128((__m128i*)d, uc);
- d += 16;
- src += 16;
- size -= 16;
- }
- #endif
-
- /* Leftover */
- while (size > 0) {
- *d++ = lc_map[(guchar)*src++];
- size --;
- }
-
- return (d - dst);
- }
-
- gint
- rspamd_lc_cmp (const gchar *s, const gchar *d, gsize l)
- {
- gsize fp, i;
- guchar c1, c2, c3, c4;
- union {
- guchar c[4];
- guint32 n;
- } cmp1, cmp2;
- gsize leftover = l % 4;
- gint ret = 0;
-
- fp = l - leftover;
-
- for (i = 0; i != fp; i += 4) {
- c1 = s[i], c2 = s[i + 1], c3 = s[i + 2], c4 = s[i + 3];
- cmp1.c[0] = lc_map[c1];
- cmp1.c[1] = lc_map[c2];
- cmp1.c[2] = lc_map[c3];
- cmp1.c[3] = lc_map[c4];
-
- c1 = d[i], c2 = d[i + 1], c3 = d[i + 2], c4 = d[i + 3];
- cmp2.c[0] = lc_map[c1];
- cmp2.c[1] = lc_map[c2];
- cmp2.c[2] = lc_map[c3];
- cmp2.c[3] = lc_map[c4];
-
- if (cmp1.n != cmp2.n) {
- return cmp1.n - cmp2.n;
- }
- }
-
- while (leftover > 0) {
- if (g_ascii_tolower (s[i]) != g_ascii_tolower (d[i])) {
- return s[i] - d[i];
- }
-
- leftover--;
- i++;
- }
-
- return ret;
- }
-
- /*
- * The purpose of this function is fast and in place conversion of a unicode
- * string to lower case, so some locale peculiarities are simply ignored
- * If the target string is longer than initial one, then we just trim it
- */
- guint
- rspamd_str_lc_utf8 (gchar *str, guint size)
- {
- guchar *d = (guchar *)str, tst[6];
- gint32 i = 0, prev = 0;
- UChar32 uc;
-
- while (i < size) {
- prev = i;
-
- U8_NEXT ((guint8*)str, i, size, uc);
- uc = u_tolower (uc);
-
- gint32 olen = 0;
- U8_APPEND_UNSAFE (tst, olen, uc);
-
- if (olen <= (i - prev)) {
- memcpy (d, tst, olen);
- d += olen;
- }
- else {
- /* Lowercasing has increased the length, so we need to ignore it */
- d += i - prev;
- }
- }
-
- return d - (guchar *)str;
- }
-
- gboolean
- rspamd_strcase_equal (gconstpointer v, gconstpointer v2)
- {
- if (g_ascii_strcasecmp ((const gchar *)v, (const gchar *)v2) == 0) {
- return TRUE;
- }
-
- return FALSE;
- }
-
- guint64
- rspamd_icase_hash (const gchar *in, gsize len, guint64 seed)
- {
- guint leftover = len % sizeof (guint64);
- guint fp, i;
- const uint8_t* s = (const uint8_t*) in;
- union {
- struct {
- guchar c1, c2, c3, c4, c5, c6, c7, c8;
- } c;
- guint64 pp;
- } u;
- guint64 h = seed;
-
- fp = len - leftover;
-
- for (i = 0; i != fp; i += 8) {
- u.c.c1 = s[i], u.c.c2 = s[i + 1], u.c.c3 = s[i + 2], u.c.c4 = s[i + 3];
- u.c.c5 = s[i + 4], u.c.c6 = s[i + 5], u.c.c7 = s[i + 6], u.c.c8 = s[i + 7];
- u.c.c1 = lc_map[u.c.c1];
- u.c.c2 = lc_map[u.c.c2];
- u.c.c3 = lc_map[u.c.c3];
- u.c.c4 = lc_map[u.c.c4];
- u.c.c5 = lc_map[u.c.c5];
- u.c.c6 = lc_map[u.c.c6];
- u.c.c7 = lc_map[u.c.c7];
- u.c.c8 = lc_map[u.c.c8];
- h = t1ha (&u.pp, sizeof (u), h);
- }
-
- u.pp = 0;
-
- switch (leftover) {
- case 7:
- u.c.c7 = lc_map[(guchar)s[i++]]; /* FALLTHRU */
- case 6:
- u.c.c6 = lc_map[(guchar)s[i++]]; /* FALLTHRU */
- case 5:
- u.c.c5 = lc_map[(guchar)s[i++]]; /* FALLTHRU */
- case 4:
- u.c.c4 = lc_map[(guchar)s[i++]]; /* FALLTHRU */
- case 3:
- u.c.c3 = lc_map[(guchar)s[i++]]; /* FALLTHRU */
- case 2:
- u.c.c2 = lc_map[(guchar)s[i++]]; /* FALLTHRU */
- case 1:
- u.c.c1 = lc_map[(guchar)s[i]];
- break;
- }
-
- h = t1ha (&u.pp, sizeof (u), h);
-
- return h;
- }
-
- guint
- rspamd_strcase_hash (gconstpointer key)
- {
- const gchar *p = key;
- gsize len;
-
- len = strlen (p);
-
- return (guint)rspamd_icase_hash (p, len, rspamd_hash_seed ());
- }
-
- guint
- rspamd_str_hash (gconstpointer key)
- {
- gsize len;
-
- len = strlen ((const gchar *)key);
-
- return (guint)rspamd_cryptobox_fast_hash (key, len, rspamd_hash_seed ());
- }
-
- gboolean
- rspamd_str_equal (gconstpointer v, gconstpointer v2)
- {
- return strcmp ((const gchar *)v, (const gchar *)v2) == 0;
- }
-
- gboolean
- rspamd_ftok_icase_equal (gconstpointer v, gconstpointer v2)
- {
- const rspamd_ftok_t *f1 = v, *f2 = v2;
-
- if (f1->len == f2->len &&
- rspamd_lc_cmp (f1->begin, f2->begin, f1->len) == 0) {
- return TRUE;
- }
-
- return FALSE;
- }
-
-
- guint
- rspamd_ftok_icase_hash (gconstpointer key)
- {
- const rspamd_ftok_t *f = key;
-
- return (guint)rspamd_icase_hash (f->begin, f->len, rspamd_hash_seed ());
- }
-
- gboolean
- rspamd_ftok_equal (gconstpointer v, gconstpointer v2)
- {
- const rspamd_ftok_t *f1 = v, *f2 = v2;
-
- if (f1->len == f2->len &&
- memcmp (f1->begin, f2->begin, f1->len) == 0) {
- return TRUE;
- }
-
- return FALSE;
- }
-
- guint
- rspamd_ftok_hash (gconstpointer key)
- {
- const rspamd_ftok_t *f = key;
-
- return (guint)rspamd_cryptobox_fast_hash (f->begin, f->len, rspamd_hash_seed ());
- }
-
- gboolean
- rspamd_gstring_icase_equal (gconstpointer v, gconstpointer v2)
- {
- const GString *f1 = v, *f2 = v2;
- if (f1->len == f2->len &&
- rspamd_lc_cmp (f1->str, f2->str, f1->len) == 0) {
- return TRUE;
- }
-
- return FALSE;
- }
-
- guint
- rspamd_gstring_icase_hash (gconstpointer key)
- {
- const GString *f = key;
-
- return (guint)rspamd_icase_hash (f->str, f->len, rspamd_hash_seed ());
- }
-
- /* https://graphics.stanford.edu/~seander/bithacks.html#ZeroInWord */
- #define MEM_ALIGN (sizeof(gsize)-1)
- #if defined(__LP64__) || defined(_LP64)
- #define WORD_TYPE guint64
- #define ZEROMASK 0x7F7F7F7F7F7F7F7FLLU
- #else
- #define WORD_TYPE guint32
- #define ZEROMASK 0x7F7F7F7FU
- #endif
-
- #define HASZERO(x) ~(((((x) & ZEROMASK) + ZEROMASK) | (x)) | ZEROMASK)
-
- gsize
- rspamd_strlcpy_fast (gchar *dst, const gchar *src, gsize siz)
- {
- gchar *d = dst;
- const gchar *s = src;
- gsize n = siz;
- WORD_TYPE *wd;
- const WORD_TYPE *ws;
-
- /* Copy as many bytes as will fit */
- if (n-- != 0) {
- if (((uintptr_t) s & MEM_ALIGN) == ((uintptr_t) d & MEM_ALIGN)) {
- /* Init copy byte by byte */
- for (; ((uintptr_t) s & MEM_ALIGN) && n && (*d = *s); n--, s++, d++);
- if (n && *s) {
- wd = (void *) d;
- ws = (const void *) s;
- /*
- * Copy by 32 or 64 bits (causes valgrind warnings)
- */
- for (; n >= sizeof (WORD_TYPE) && !HASZERO(*ws);
- n -= sizeof (WORD_TYPE), ws++, wd++) {
- *wd = *ws;
- }
-
- d = (void *) wd;
- s = (const void *) ws;
- }
- }
-
- /* Copy the rest */
- for (; n && (*d = *s); n--, s++, d++);
-
- *d = 0;
- }
- else {
- return 0;
- }
-
- return (d - dst);
- }
-
- gsize
- rspamd_null_safe_copy (const gchar *src, gsize srclen,
- gchar *dest, gsize destlen)
- {
- gsize copied = 0, si = 0, di = 0;
-
- if (destlen == 0) {
- return 0;
- }
-
- while (si < srclen && di + 1 < destlen) {
- if (src[si] != '\0') {
- dest[di++] = src[si++];
- copied ++;
- }
- else {
- si ++;
- }
- }
-
- dest[di] = '\0';
-
- return copied;
- }
-
-
- size_t
- rspamd_strlcpy_safe (gchar *dst, const gchar *src, gsize siz)
- {
- gchar *d = dst;
- gsize nleft = siz;
-
- if (nleft != 0) {
- while (--nleft != 0) {
- if ((*d++ = *src++) == '\0') {
- d --;
- break;
- }
- }
- }
-
- if (nleft == 0) {
- if (siz != 0) {
- *d = '\0';
- }
- }
-
- return (d - dst);
- }
-
- /*
- * Try to convert string of length to long
- */
- gboolean
- rspamd_strtol (const gchar *s, gsize len, glong *value)
- {
- const gchar *p = s, *end = s + len;
- gchar c;
- glong v = 0;
- const glong cutoff = G_MAXLONG / 10, cutlim = G_MAXLONG % 10;
- gboolean neg;
-
- /* Case negative values */
- if (*p == '-') {
- neg = TRUE;
- p++;
- }
- else {
- neg = FALSE;
- }
- /* Some preparations for range errors */
-
- while (p < end) {
- c = *p;
- if (c >= '0' && c <= '9') {
- c -= '0';
- if (v > cutoff || (v == cutoff && c > cutlim)) {
- /* Range error */
- *value = neg ? G_MINLONG : G_MAXLONG;
- return FALSE;
- }
- else {
- v *= 10;
- v += c;
- }
- }
- else {
- return FALSE;
- }
- p++;
- }
-
- *value = neg ? -(v) : v;
- return TRUE;
- }
-
- /*
- * Try to convert string of length to long
- */
- #define CONV_STR_LIM_DECIMAL(max_num) do { \
- while (p < end) { \
- c = *p; \
- if (c >= '0' && c <= '9') { \
- c -= '0'; \
- if (v > cutoff || (v == cutoff && (guint8)c > cutlim)) { \
- *value = (max_num); \
- return FALSE; \
- } \
- else { \
- v *= 10; \
- v += c; \
- } \
- } \
- else { \
- *value = v; \
- return FALSE; \
- } \
- p++; \
- } \
- } while(0)
-
- gboolean
- rspamd_strtoul (const gchar *s, gsize len, gulong *value)
- {
- const gchar *p = s, *end = s + len;
- gchar c;
- gulong v = 0;
- const gulong cutoff = G_MAXULONG / 10, cutlim = G_MAXULONG % 10;
-
- /* Some preparations for range errors */
- CONV_STR_LIM_DECIMAL(G_MAXULONG);
-
- *value = v;
- return TRUE;
- }
-
- gboolean
- rspamd_strtou64 (const gchar *s, gsize len, guint64 *value)
- {
- const gchar *p = s, *end = s + len;
- gchar c;
- guint64 v = 0;
- const guint64 cutoff = G_MAXUINT64 / 10, cutlim = G_MAXUINT64 % 10;
-
- /* Some preparations for range errors */
- CONV_STR_LIM_DECIMAL(G_MAXUINT64);
-
- *value = v;
- return TRUE;
- }
-
- gboolean
- rspamd_xstrtoul (const gchar *s, gsize len, gulong *value)
- {
- const gchar *p = s, *end = s + len;
- gchar c;
- gulong v = 0;
- const gulong cutoff = G_MAXULONG / 10, cutlim = G_MAXULONG % 10;
-
- /* Some preparations for range errors */
- while (p < end) {
- c = g_ascii_tolower (*p);
- if (c >= '0' && c <= '9') {
- c -= '0';
- if (v > cutoff || (v == cutoff && (guint8)c > cutlim)) {
- /* Range error */
- *value = G_MAXULONG;
- return FALSE;
- }
- else {
- v *= 16;
- v += c;
- }
- }
- else if (c >= 'a' || c <= 'f') {
- c = c - 'a' + 10;
- if (v > cutoff || (v == cutoff && (guint8)c > cutlim)) {
- /* Range error */
- *value = G_MAXULONG;
- return FALSE;
- }
- else {
- v *= 16;
- v += c;
- }
- }
- else {
- *value = v;
-
- return FALSE;
- }
- p++;
- }
-
- *value = v;
- return TRUE;
- }
-
- /**
- * Utility function to provide mem_pool copy for rspamd_hash_table_copy function
- * @param data string to copy
- * @param ud memory pool to use
- * @return
- */
- gpointer
- rspamd_str_pool_copy (gconstpointer data, gpointer ud)
- {
- rspamd_mempool_t *pool = ud;
-
- return data ? rspamd_mempool_strdup (pool, data) : NULL;
- }
-
- /*
- * We use here z-base32 encoding described here:
- * http://philzimmermann.com/docs/human-oriented-base-32-encoding.txt
- */
-
- gint
- rspamd_encode_base32_buf (const guchar *in, gsize inlen, gchar *out, gsize outlen,
- enum rspamd_base32_type type)
- {
- static const char b32_default[] = "ybndrfg8ejkmcpqxot1uwisza345h769",
- b32_bleach[] = "qpzry9x8gf2tvdw0s3jn54khce6mua7l",
- b32_rfc[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567",
- *b32;
- gchar *o, *end;
- gsize i;
- gint remain = -1, x;
- bool inverse_order = true;
-
- end = out + outlen;
- o = out;
-
- switch (type) {
- case RSPAMD_BASE32_DEFAULT:
- b32 = b32_default;
- break;
- case RSPAMD_BASE32_BLEACH:
- b32 = b32_bleach;
- inverse_order = false;
- break;
- case RSPAMD_BASE32_RFC:
- b32 = b32_rfc;
- inverse_order = false;
- break;
- default:
- g_assert_not_reached ();
- abort ();
- }
-
- if (inverse_order) {
- /* Zbase32 as used in Rspamd */
- for (i = 0; i < inlen && o < end - 1; i++) {
- switch (i % 5) {
- case 0:
- /* 8 bits of input and 3 to remain */
- x = in[i];
- remain = in[i] >> 5;
- *o++ = b32[x & 0x1F];
- break;
- case 1:
- /* 11 bits of input, 1 to remain */
- x = remain | in[i] << 3;
- *o++ = b32[x & 0x1F];
- *o++ = b32[x >> 5 & 0x1F];
- remain = x >> 10;
- break;
- case 2:
- /* 9 bits of input, 4 to remain */
- x = remain | in[i] << 1;
- *o++ = b32[x & 0x1F];
- remain = x >> 5;
- break;
- case 3:
- /* 12 bits of input, 2 to remain */
- x = remain | in[i] << 4;
- *o++ = b32[x & 0x1F];
- *o++ = b32[x >> 5 & 0x1F];
- remain = x >> 10 & 0x3;
- break;
- case 4:
- /* 10 bits of output, nothing to remain */
- x = remain | in[i] << 2;
- *o++ = b32[x & 0x1F];
- *o++ = b32[x >> 5 & 0x1F];
- remain = -1;
- break;
- default:
- /* Not to be happen */
- break;
- }
- }
- }
- else {
- /* Traditional base32 with no bits inversion */
- for (i = 0; i < inlen && o < end - 1; i++) {
- switch (i % 5) {
- case 0:
- /* 8 bits of input and 3 to remain */
- x = in[i] >> 3;
- remain = (in[i] & 7) << 2;
- *o++ = b32[x & 0x1F];
- break;
- case 1:
- /* 11 bits of input, 1 to remain */
- x = (remain << 6) | in[i];
- *o++ = b32[(x >> 6) & 0x1F];
- *o++ = b32[(x >> 1) & 0x1F];
- remain = (x & 0x1) << 4;
- break;
- case 2:
- /* 9 bits of input, 4 to remain */
- x = (remain << 4) | in[i];
- *o++ = b32[(x >> 4) & 0x1F];
- remain = (x & 15) << 1;
- break;
- case 3:
- /* 12 bits of input, 2 to remain */
- x = (remain << 7) | in[i];
- *o++ = b32[(x >> 7) & 0x1F];
- *o++ = b32[(x >> 2) & 0x1F];
- remain = (x & 3) << 3;
- break;
- case 4:
- /* 10 bits of output, nothing to remain */
- x = (remain << 5) | in[i];
- *o++ = b32[(x >> 5) & 0x1F];
- *o++ = b32[x & 0x1F];
- remain = -1;
- break;
- default:
- /* Not to be happen */
- break;
- }
- }
- }
- if (remain >= 0 && o < end) {
- *o++ = b32[remain & 0x1F];
- }
-
- if (o <= end) {
- return (o - out);
- }
-
- return -1;
- }
-
- gchar *
- rspamd_encode_base32 (const guchar *in, gsize inlen, enum rspamd_base32_type type)
- {
- gsize allocated_len = inlen * 8 / 5 + 2;
- gchar *out;
- gint outlen;
-
- out = g_malloc (allocated_len);
- outlen = rspamd_encode_base32_buf (in, inlen, out,
- allocated_len - 1, type);
-
- if (outlen >= 0) {
- out[outlen] = 0;
-
- return out;
- }
-
- g_free (out);
-
- return NULL;
- }
-
- enum rspamd_base32_type
- rspamd_base32_decode_type_from_str (const gchar *str)
- {
- enum rspamd_base32_type ret = RSPAMD_BASE32_INVALID;
-
- if (str == NULL) {
- return RSPAMD_BASE32_DEFAULT;
- }
-
- if (strcmp (str, "default") == 0 || strcmp (str, "zbase") == 0) {
- ret = RSPAMD_BASE32_ZBASE;
- }
- else if (strcmp (str, "bleach") == 0) {
- ret = RSPAMD_BASE32_BLEACH;
- }
- else if (strcmp (str, "rfc") == 0) {
- ret = RSPAMD_BASE32_RFC;
- }
-
- return ret;
- }
-
- static const guchar b32_dec_zbase[] = {
- 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
- 0xff, 0x12, 0xff, 0x19, 0x1a, 0x1b, 0x1e, 0x1d,
- 0x07, 0x1f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
- 0xff, 0x18, 0x01, 0x0c, 0x03, 0x08, 0x05, 0x06,
- 0x1c, 0x15, 0x09, 0x0a, 0xff, 0x0b, 0x02, 0x10,
- 0x0d, 0x0e, 0x04, 0x16, 0x11, 0x13, 0xff, 0x14,
- 0x0f, 0x00, 0x17, 0xff, 0xff, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
- };
- static const guchar b32_dec_bleach[] = {
- 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
- 0x0f, 0xff, 0x0a, 0x11, 0x15, 0x14, 0x1a, 0x1e,
- 0x07, 0x05, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
- 0xff, 0x1d, 0xff, 0x18, 0x0d, 0x19, 0x09, 0x08,
- 0x17, 0xff, 0x12, 0x16, 0x1f, 0x1b, 0x13, 0xff,
- 0x01, 0x00, 0x03, 0x10, 0x0b, 0x1c, 0x0c, 0x0e,
- 0x06, 0x04, 0x02, 0xff, 0xff, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
- };
- static const guchar b32_dec_rfc[] = {
- 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
- 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
- 0xff, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
- 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
- 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16,
- 0x17, 0x18, 0x19, 0xff, 0xff, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
- };
-
-
- gint
- rspamd_decode_base32_buf (const gchar *in, gsize inlen, guchar *out, gsize outlen,
- enum rspamd_base32_type type)
- {
- guchar *o, *end, decoded;
- guchar c;
- guint acc = 0U;
- guint processed_bits = 0;
- gsize i;
- const guchar *b32_dec;
- bool inverse_bits = true;
-
- end = out + outlen;
- o = out;
-
- switch (type) {
- case RSPAMD_BASE32_DEFAULT:
- b32_dec = b32_dec_zbase;
- break;
- case RSPAMD_BASE32_BLEACH:
- b32_dec = b32_dec_bleach;
- inverse_bits = false;
- break;
- case RSPAMD_BASE32_RFC:
- b32_dec = b32_dec_rfc;
- inverse_bits = false;
- break;
- default:
- g_assert_not_reached ();
- abort ();
- }
-
- if (inverse_bits) {
- for (i = 0; i < inlen; i++) {
- c = (guchar) in[i];
-
- if (processed_bits >= 8) {
- /* Emit from left to right */
- processed_bits -= 8;
- *o++ = acc & 0xFF;
- acc >>= 8;
- }
-
- decoded = b32_dec[c];
- if (decoded == 0xff || o >= end) {
- return -1;
- }
-
- acc = (decoded << processed_bits) | acc;
- processed_bits += 5;
- }
-
- if (processed_bits > 0 && o < end) {
- *o++ = (acc & 0xFF);
- }
- else if (o > end) {
- return -1;
- }
- }
- else {
- for (i = 0; i < inlen; i++) {
- c = (guchar) in[i];
-
- decoded = b32_dec[c];
- if (decoded == 0xff) {
- return -1;
- }
-
- acc = (acc << 5) | decoded;
- processed_bits += 5;
-
- if (processed_bits >= 8) {
- /* Emit from right to left */
- processed_bits -= 8;
-
- /* Output buffer overflow */
- if (o >= end) {
- return -1;
- }
-
- *o++ = (acc >> processed_bits) & 0xFF;
- /* Preserve lowers at the higher parts of the input */
- acc = (acc & ((1u << processed_bits) - 1));
- }
- }
-
- if (processed_bits > 0 && o < end) {
- *o++ = (acc & 0xFF);
- }
- else if (o > end) {
- return -1;
- }
- }
-
- return (o - out);
- }
-
- guchar *
- rspamd_decode_base32 (const gchar *in, gsize inlen, gsize *outlen,
- enum rspamd_base32_type type)
- {
- guchar *res;
-
- gsize allocated_len = inlen * 5 / 8 + 2;
- gssize olen;
-
- res = g_malloc (allocated_len);
-
- olen = rspamd_decode_base32_buf (in, inlen, res, allocated_len - 1,
- type);
-
- if (olen >= 0) {
- res[olen] = '\0';
- }
- else {
- g_free (res);
-
- if (outlen) {
- *outlen = 0;
- }
-
- return NULL;
- }
-
- if (outlen) {
- *outlen = olen;
- }
-
- return res;
- }
-
-
- gchar *
- rspamd_encode_base64_common (const guchar *in, gsize inlen, gint str_len,
- gsize *outlen, gboolean fold, enum rspamd_newlines_type how)
- {
- #define ADD_SPLIT do { \
- if (how == RSPAMD_TASK_NEWLINES_CR || how == RSPAMD_TASK_NEWLINES_CRLF) *o++ = '\r'; \
- if (how == RSPAMD_TASK_NEWLINES_LF || how == RSPAMD_TASK_NEWLINES_CRLF) *o++ = '\n'; \
- if (fold) *o++ = '\t'; \
- } while (0)
- #define CHECK_SPLIT \
- do { if (str_len > 0 && cols >= str_len) { \
- ADD_SPLIT; \
- cols = 0; \
- } } \
- while (0)
-
- gsize allocated_len = (inlen / 3) * 4 + 5;
- gchar *out, *o;
- guint64 n;
- guint32 rem, t, carry;
- gint cols, shift;
- static const char b64_enc[] =
- "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
- "abcdefghijklmnopqrstuvwxyz"
- "0123456789+/";
-
- if (str_len > 0) {
- g_assert (str_len > 8);
- if (fold) {
- switch (how) {
- case RSPAMD_TASK_NEWLINES_CR:
- case RSPAMD_TASK_NEWLINES_LF:
- allocated_len += (allocated_len / str_len + 1) * 2 + 1;
- break;
- default:
- allocated_len += (allocated_len / str_len + 1) * 3 + 1;
- break;
- }
- }
- else {
- switch (how) {
- case RSPAMD_TASK_NEWLINES_CR:
- case RSPAMD_TASK_NEWLINES_LF:
- allocated_len += (allocated_len / str_len + 1) * 1 + 1;
- break;
- default:
- allocated_len += (allocated_len / str_len + 1) * 2 + 1;
- break;
- }
- }
- }
-
- out = g_malloc (allocated_len);
- o = out;
- cols = 0;
-
- while (inlen > 6) {
- memcpy (&n, in, sizeof (n));
- n = GUINT64_TO_BE (n);
-
- if (str_len <= 0 || cols <= str_len - 8) {
- *o++ = b64_enc[(n >> 58) & 0x3F];
- *o++ = b64_enc[(n >> 52) & 0x3F];
- *o++ = b64_enc[(n >> 46) & 0x3F];
- *o++ = b64_enc[(n >> 40) & 0x3F];
- *o++ = b64_enc[(n >> 34) & 0x3F];
- *o++ = b64_enc[(n >> 28) & 0x3F];
- *o++ = b64_enc[(n >> 22) & 0x3F];
- *o++ = b64_enc[(n >> 16) & 0x3F];
- cols += 8;
- }
- else {
- cols = str_len - cols;
- shift = 58;
- while (cols) {
- *o++ = b64_enc[(n >> shift) & 0x3F];
- shift -= 6;
- cols --;
- }
-
- ADD_SPLIT;
-
- /* Remaining bytes */
- while (shift >= 16) {
- *o++ = b64_enc[(n >> shift) & 0x3F];
- shift -= 6;
- cols ++;
- }
- }
-
- in += 6;
- inlen -= 6;
- }
-
- CHECK_SPLIT;
-
- rem = 0;
- carry = 0;
-
- for (;;) {
- /* Padding + remaining data (0 - 2 bytes) */
- switch (rem) {
- case 0:
- if (inlen-- == 0) {
- goto end;
- }
- t = *in++;
- *o++ = b64_enc[t >> 2];
- carry = (t << 4) & 0x30;
- rem = 1;
- cols ++;
- case 1:
- if (inlen-- == 0) {
- goto end;
- }
- CHECK_SPLIT;
- t = *in++;
- *o++ = b64_enc[carry | (t >> 4)];
- carry = (t << 2) & 0x3C;
- rem = 2;
- cols ++;
- default:
- if (inlen-- == 0) {
- goto end;
- }
- CHECK_SPLIT;
- t = *in ++;
- *o++ = b64_enc[carry | (t >> 6)];
- cols ++;
- CHECK_SPLIT;
- *o++ = b64_enc[t & 0x3F];
- cols ++;
- CHECK_SPLIT;
- rem = 0;
- }
- }
-
- end:
- if (rem == 1) {
- *o++ = b64_enc[carry];
- cols ++;
- CHECK_SPLIT;
- *o++ = '=';
- cols ++;
- CHECK_SPLIT;
- *o++ = '=';
- cols ++;
- CHECK_SPLIT;
- }
- else if (rem == 2) {
- *o++ = b64_enc[carry];
- cols ++;
- CHECK_SPLIT;
- *o++ = '=';
- cols ++;
- }
-
- CHECK_SPLIT;
-
- *o = '\0';
-
- if (outlen != NULL) {
- *outlen = o - out;
- }
-
- return out;
- }
-
- gchar *
- rspamd_encode_base64 (const guchar *in, gsize inlen, gint str_len,
- gsize *outlen)
- {
- return rspamd_encode_base64_common (in, inlen, str_len, outlen, FALSE,
- RSPAMD_TASK_NEWLINES_CRLF);
- }
-
- gchar *
- rspamd_encode_base64_fold (const guchar *in, gsize inlen, gint str_len,
- gsize *outlen, enum rspamd_newlines_type how)
- {
- return rspamd_encode_base64_common (in, inlen, str_len, outlen, TRUE, how);
- }
-
- #define QP_RANGE(x) (((x) >= 33 && (x) <= 60) || ((x) >= 62 && (x) <= 126) \
- || (x) == '\r' || (x) == '\n' || (x) == ' ' || (x) == '\t')
- #define QP_SPAN_NORMAL(span, str_len) ((str_len) > 0 && \
- ((span) + 1) >= (str_len))
- #define QP_SPAN_SPECIAL(span, str_len) ((str_len) > 0 && \
- ((span) + 4) >= (str_len))
-
- gchar *
- rspamd_encode_qp_fold (const guchar *in, gsize inlen, gint str_len,
- gsize *outlen, enum rspamd_newlines_type how)
- {
- gsize olen = 0, span = 0, i = 0, seen_spaces = 0;
- gchar *out;
- gint ch, last_sp;
- const guchar *end = in + inlen, *p = in;
- static const gchar hexdigests[16] = "0123456789ABCDEF";
-
- while (p < end) {
- ch = *p;
-
- if (QP_RANGE(ch)) {
- olen ++;
- span ++;
-
- if (ch == '\r' || ch == '\n') {
- if (seen_spaces > 0) {
- /* We must encode spaces at the end of line */
- olen += 3;
- seen_spaces = 0;
- /* Special stuff for space character at the end */
- if (QP_SPAN_SPECIAL(span, str_len)) {
- if (how == RSPAMD_TASK_NEWLINES_CRLF) {
- /* =\r\n */
- olen += 3;
- }
- else {
- olen += 2;
- }
- }
- /* Continue with the same `ch` but without spaces logic */
- continue;
- }
-
- span = 0;
- }
- else if (ch == ' ' || ch == '\t') {
- seen_spaces ++;
- last_sp = ch;
- }
- else {
- seen_spaces = 0;
- }
- }
- else {
- if (QP_SPAN_SPECIAL(span, str_len)) {
- if (how == RSPAMD_TASK_NEWLINES_CRLF) {
- /* =\r\n */
- olen += 3;
- }
- else {
- olen += 2;
- }
- span = 0;
- }
-
- olen += 3;
- span += 3;
- }
-
- if (QP_SPAN_NORMAL(span, str_len)) {
- if (how == RSPAMD_TASK_NEWLINES_CRLF) {
- /* =\r\n */
- olen += 3;
- }
- else {
- olen += 2;
- }
- span = 0;
- }
-
- p ++;
- }
-
- if (seen_spaces > 0) {
- /* Reserve length for the last space encoded */
- olen += 3;
- }
-
- out = g_malloc (olen + 1);
- p = in;
- i = 0;
- span = 0;
- seen_spaces = 0;
-
- while (p < end) {
- ch = *p;
-
- if (QP_RANGE (ch)) {
- if (ch == '\r' || ch == '\n') {
- if (seen_spaces > 0) {
- if (QP_SPAN_SPECIAL(span, str_len)) {
- /* Add soft newline */
- i --;
-
- if (p + 1 < end || span + 3 >= str_len) {
- switch (how) {
- default:
- case RSPAMD_TASK_NEWLINES_CRLF:
- out[i++] = '=';
- out[i++] = '\r';
- out[i++] = '\n';
- break;
- case RSPAMD_TASK_NEWLINES_LF:
- out[i++] = '=';
- out[i++] = '\n';
- break;
- case RSPAMD_TASK_NEWLINES_CR:
- out[i++] = '=';
- out[i++] = '\r';
- break;
- }
- }
-
- /* Now write encoded `last_sp` but after newline */
- out[i++] = '=';
- out[i++] = hexdigests[((last_sp >> 4) & 0xF)];
- out[i++] = hexdigests[(last_sp & 0xF)];
-
- span = 0;
- }
- else {
- /* Encode last space */
- --i;
- out[i++] = '=';
- out[i++] = hexdigests[((last_sp >> 4) & 0xF)];
- out[i++] = hexdigests[(last_sp & 0xF)];
- seen_spaces = 0;
- }
-
- continue;
- }
- span = 0;
- }
- else if (ch == ' ' || ch == '\t') {
- seen_spaces ++;
- last_sp = ch;
- span ++;
- }
- else {
- seen_spaces = 0;
- span ++;
- }
-
- out[i++] = ch;
- }
- else {
- if (QP_SPAN_SPECIAL(span, str_len)) {
- /* Add new line and then continue */
- if (p + 1 < end || span + 3 >= str_len) {
- switch (how) {
- default:
- case RSPAMD_TASK_NEWLINES_CRLF:
- out[i++] = '=';
- out[i++] = '\r';
- out[i++] = '\n';
- break;
- case RSPAMD_TASK_NEWLINES_LF:
- out[i++] = '=';
- out[i++] = '\n';
- break;
- case RSPAMD_TASK_NEWLINES_CR:
- out[i++] = '=';
- out[i++] = '\r';
- break;
- }
- span = 0;
- }
- }
-
- out[i++] = '=';
- out[i++] = hexdigests[((ch >> 4) & 0xF)];
- out[i++] = hexdigests[(ch & 0xF)];
- span += 3;
- seen_spaces = 0;
- }
-
- if (QP_SPAN_NORMAL(span, str_len)) {
- /* Add new line and then continue */
- if (p + 1 < end || span > str_len || seen_spaces) {
- switch (how) {
- default:
- case RSPAMD_TASK_NEWLINES_CRLF:
- out[i++] = '=';
- out[i++] = '\r';
- out[i++] = '\n';
- break;
- case RSPAMD_TASK_NEWLINES_LF:
- out[i++] = '=';
- out[i++] = '\n';
- break;
- case RSPAMD_TASK_NEWLINES_CR:
- out[i++] = '=';
- out[i++] = '\r';
- break;
- }
- span = 0;
- seen_spaces = 0;
- }
- }
-
- g_assert (i <= olen);
- p ++;
- }
-
- /* Deal with the last space character */
- if (seen_spaces > 0) {
- i --;
- out[i++] = '=';
- out[i++] = hexdigests[((last_sp >> 4) & 0xF)];
- out[i++] = hexdigests[(last_sp & 0xF)];
- }
-
- out[i] = '\0';
-
- if (outlen) {
- *outlen = i;
- }
-
- return out;
- }
-
- #define MIN3(a, b, c) ((a) < (b) ? ((a) < (c) ? (a) : (c)) : ((b) < (c) ? (b) : (c)))
-
- gint
- rspamd_strings_levenshtein_distance (const gchar *s1, gsize s1len,
- const gchar *s2, gsize s2len,
- guint replace_cost)
- {
- gchar c1, c2, last_c2, last_c1;
- static GArray *current_row = NULL, *prev_row = NULL, *transp_row = NULL;
- gint eq;
- static const guint max_cmp = 8192;
- gint ret;
-
- g_assert (s1 != NULL);
- g_assert (s2 != NULL);
-
- if (s1len == 0) {
- s1len = strlen (s1);
- }
- if (s2len == 0) {
- s2len = strlen (s2);
- }
-
- if (MAX(s1len, s2len) > max_cmp) {
- /* Cannot compare too many characters */
- return max_cmp;
- }
-
- if (s1len > s2len) {
- /* Exchange s1 and s2 */
- const gchar *tmp;
- gsize tmplen;
-
- tmp = s2;
- s2 = s1;
- s1 = tmp;
-
- tmplen = s2len;
- s2len = s1len;
- s1len = tmplen;
- }
-
- /* Adjust static space */
- if (current_row == NULL) {
- current_row = g_array_sized_new (FALSE, FALSE, sizeof (gint), s1len + 1);
- prev_row = g_array_sized_new (FALSE, FALSE, sizeof (gint), s1len + 1);
- transp_row = g_array_sized_new (FALSE, FALSE, sizeof (gint), s1len + 1);
- g_array_set_size (current_row, s1len + 1);
- g_array_set_size (prev_row, s1len + 1);
- g_array_set_size (transp_row, s1len + 1);
- }
- else if (current_row->len < s1len + 1) {
- g_array_set_size (current_row, s1len + 1);
- g_array_set_size (prev_row, s1len + 1);
- g_array_set_size (transp_row, s1len + 1);
- }
-
- memset (current_row->data, 0, (s1len + 1) * sizeof (gint));
- memset (transp_row->data, 0, (s1len + 1) * sizeof (gint));
-
- for (gint i = 0; i <= s1len; i++) {
- g_array_index (prev_row, gint, i) = i;
- }
-
- last_c2 = '\0';
-
- for (gint i = 1; i <= s2len; i++) {
- c2 = s2[i - 1];
- g_array_index (current_row, gint, 0) = i;
- last_c1 = '\0';
-
- for (gint j = 1; j <= s1len; j++) {
- c1 = s1[j - 1];
- eq = c1 == c2 ? 0 : replace_cost;
- ret = MIN3 (g_array_index (current_row, gint, j - 1) + 1, /* Insert */
- g_array_index (prev_row, gint, j) + 1, /* Remove */
- g_array_index (prev_row, gint, j - 1) + eq /* Replace */);
-
- /* Take reordering into account */
- if (c1 == last_c2 && c2 == last_c1 && j >= 2) {
- ret = MIN (ret, g_array_index (transp_row, gint, j - 2) + eq);
- }
-
- g_array_index (current_row, gint, j) = ret;
- last_c1 = c1;
- }
-
- last_c2 = c2;
-
- /* Exchange pointers */
- GArray *tmp;
- tmp = transp_row;
- transp_row = prev_row;
- prev_row = current_row;
- current_row = tmp;
- }
-
- ret = g_array_index (prev_row, gint, s1len);
-
- return ret;
- }
-
- GString *
- rspamd_header_value_fold (const gchar *name, gsize name_len,
- const gchar *value,
- gsize value_len,
- guint fold_max,
- enum rspamd_newlines_type how,
- const gchar *fold_on_chars)
- {
- GString *res;
- const guint default_fold_max = 76;
- guint cur_len;
- const gchar *p, *c, *end, *fold_sequence;
- guint nspaces = 0;
- gboolean first_token = TRUE;
- enum {
- fold_before = 0,
- fold_after
- } fold_type = fold_before;
- enum {
- read_token = 0,
- read_quoted,
- after_quote,
- fold_token,
- } state = read_token, next_state = read_token;
-
- g_assert (name != NULL);
- g_assert (value != NULL);
-
- /* Filter insane values */
- if (fold_max < 20) {
- fold_max = default_fold_max;
- }
-
- switch (how) {
- case RSPAMD_TASK_NEWLINES_LF:
- fold_sequence = "\n\t";
- break;
- case RSPAMD_TASK_NEWLINES_CR:
- fold_sequence = "\r\t";
- break;
- case RSPAMD_TASK_NEWLINES_CRLF:
- default:
- fold_sequence ="\r\n\t";
- break;
- }
-
- res = g_string_sized_new (value_len);
-
- c = value;
- p = c;
- end = value + value_len;
- /* name:<WSP> */
- cur_len = name_len + 2;
-
- while (p < end) {
- switch (state) {
-
- case read_token:
- if (fold_on_chars) {
- if (strchr (fold_on_chars, *p) != NULL) {
- fold_type = fold_after;
- state = fold_token;
- next_state = read_token;
- }
-
- p ++;
- }
- else {
- if (*p == ',' || *p == ';') {
- /* We have something similar to the token's end, so check len */
- if (cur_len > fold_max * 0.8 && cur_len < fold_max) {
- /* We want fold */
- fold_type = fold_after;
- state = fold_token;
- next_state = read_token;
- } else if (cur_len > fold_max && !first_token) {
- fold_type = fold_before;
- state = fold_token;
- next_state = read_token;
- } else {
- g_string_append_len (res, c, p - c + 1);
- c = p + 1;
- first_token = FALSE;
- }
- p++;
- } else if (*p == '"') {
- /* Fold before quoted tokens */
- g_string_append_len (res, c, p - c);
- c = p;
- state = read_quoted;
- } else if (*p == '\r' || *p == '\n') {
- if (cur_len > fold_max && !first_token) {
- fold_type = fold_before;
- state = fold_token;
- next_state = read_token;
- } else {
- /* We need to ensure that it is a folding and not something else */
-
- const char *t = p;
- bool seen_fold = false;
-
- while (t < end) {
- if (*t == ' ' || *t == '\t') {
- seen_fold = true;
- break;
- }
- else if (!g_ascii_isspace(*t)) {
- break;
- }
-
- t++;
- }
-
- if (seen_fold) {
- /* Reset line length */
- cur_len = 0;
-
- while (g_ascii_isspace (*p)) {
- p++;
- }
-
- g_string_append_len(res, c, p - c);
- c = p;
- first_token = TRUE;
- }
- else {
- /* Not seen folding, inject it */
- g_string_append_len (res, c, p - c);
- g_string_append (res, fold_sequence);
- p = t; /* Adjust p to ensure that we do not append extra stuff */
- state = read_token;
- first_token = TRUE;
- c = p;
- }
- }
- } else if (g_ascii_isspace (*p)) {
- if (cur_len > fold_max * 0.8 && cur_len < fold_max) {
- /* We want fold */
- fold_type = fold_after;
- state = fold_token;
- next_state = read_token;
- } else if (cur_len > fold_max && !first_token) {
- fold_type = fold_before;
- state = fold_token;
- next_state = read_token;
- } else {
- g_string_append_len (res, c, p - c);
- c = p;
- first_token = FALSE;
- p++;
- cur_len++;
- }
- } else {
- p++;
- cur_len++;
- }
- }
- break;
- case fold_token:
- /* Here, we have token start at 'c' and token end at 'p' */
- if (fold_type == fold_after) {
- nspaces = 0;
- if (p > c) {
- g_string_append_len (res, c, p - c);
-
- /*
- * Check any spaces that are appended to the result
- * before folding
- */
- const gchar *last = &res->str[res->len - 1];
-
- while (g_ascii_isspace (*last)) {
- last --;
- nspaces ++;
- res->len --;
- }
- }
-
- g_string_append (res, fold_sequence);
-
- /* Skip space if needed */
- if (g_ascii_isspace (*p)) {
- p ++;
- }
-
- /* Move leftover spaces */
- while (nspaces) {
- g_string_append_c (res, ' ');
- nspaces --;
- }
-
- cur_len = 0;
- }
- else {
- const gchar *last;
-
- /* Skip space if needed */
- if (g_ascii_isspace (*c) && p > c) {
- c ++;
- }
-
- /* Avoid double folding */
- last = &res->str[res->len - 1];
- last --;
-
- if (*last != '\r' && *last != '\n') {
- last ++;
- while (g_ascii_isspace (*last)) {
- last --;
- nspaces ++;
- res->len --;
- }
-
- g_string_append (res, fold_sequence);
- }
-
- /* Move leftover spaces */
- cur_len = nspaces;
-
- while (nspaces) {
- g_string_append_c (res, ' ');
- nspaces --;
- }
-
- if (p > c) {
- g_string_append_len (res, c, p - c);
- cur_len += p - c;
- }
- else {
- cur_len = 0;
- }
- }
-
- first_token = TRUE;
- c = p;
- state = next_state;
- break;
-
- case read_quoted:
- if (p != c && *p == '"') {
- state = after_quote;
- }
- p ++;
- cur_len ++;
- break;
-
- case after_quote:
- state = read_token;
- /* Skip one more character after the quote */
- p ++;
- cur_len ++;
- g_string_append_len (res, c, p - c);
- c = p;
- first_token = TRUE;
- break;
- }
- }
-
- /* Last token */
- switch (state) {
- case read_token:
- if (!fold_on_chars && cur_len > fold_max && !first_token) {
- if (g_ascii_isspace (*c)) {
- c ++;
- }
- g_string_append (res, fold_sequence);
- g_string_append_len (res, c, p - c);
- }
- else {
- g_string_append_len (res, c, p - c);
- }
- break;
- case read_quoted:
- case after_quote:
- g_string_append_len (res, c, p - c);
- break;
- case fold_token:
- /* Here, we have token start at 'c' and token end at 'p' */
- if (g_ascii_isspace (res->str[res->len - 1])) {
- g_string_append_len (res, c, p - c);
- }
- else {
- if (*c != '\r' && *c != '\n') {
- /* We need to add folding as well */
- g_string_append (res, fold_sequence);
- g_string_append_len (res, c, p - c);
- }
- else {
- g_string_append_len (res, c, p - c);
- }
- }
- break;
- default:
- g_assert (p == c);
- break;
- }
-
- return res;
- }
-
- static inline bool rspamd_substring_cmp_func (guchar a, guchar b) { return a == b; }
-
- static inline bool rspamd_substring_casecmp_func (guchar a, guchar b) { return lc_map[a] == lc_map[b]; }
-
- typedef bool (*rspamd_cmpchar_func_t) (guchar a, guchar b);
-
- static inline void
- rspamd_substring_preprocess_kmp (const gchar *pat, gsize len, goffset *fsm,
- rspamd_cmpchar_func_t f)
- {
- goffset i, j;
-
- i = 0;
- j = -1;
- fsm[0] = -1;
-
- while (i < len) {
- while (j > -1 && !f(pat[i], pat[j])) {
- j = fsm[j];
- }
-
- i++;
- j++;
-
- if (i < len && j < len && f(pat[i], pat[j])) {
- fsm[i] = fsm[j];
- }
- else {
- fsm[i] = j;
- }
- }
- }
-
- static inline goffset
- rspamd_substring_search_preprocessed (const gchar *in, gsize inlen,
- const gchar *srch,
- gsize srchlen,
- const goffset *fsm,
- rspamd_cmpchar_func_t f)
- {
- goffset i, j, k, ell;
-
- for (ell = 1; f(srch[ell - 1], srch[ell]); ell++) {}
- if (ell == srchlen) {
- ell = 0;
- }
-
- /* Searching */
- i = ell;
- j = k = 0;
-
- while (j <= inlen - srchlen) {
- while (i < srchlen && f(srch[i], in[i + j])) {
- ++i;
- }
-
- if (i >= srchlen) {
- while (k < ell && f(srch[k], in[j + k])) {
- ++k;
- }
-
- if (k >= ell) {
- return j;
- }
- }
-
- j += (i - fsm[i]);
-
- if (i == ell) {
- k = MAX(0, k - 1);
- }
- else {
- if (fsm[i] <= ell) {
- k = MAX(0, fsm[i]);
- i = ell;
- } else {
- k = ell;
- i = fsm[i];
- }
- }
- }
-
- return -1;
- }
-
- static inline goffset
- rspamd_substring_search_common (const gchar *in, gsize inlen,
- const gchar *srch, gsize srchlen, rspamd_cmpchar_func_t f)
- {
- static goffset st_fsm[128];
- goffset *fsm, ret;
-
- if (G_LIKELY (srchlen < G_N_ELEMENTS (st_fsm))) {
- fsm = st_fsm;
- }
- else {
- fsm = g_malloc ((srchlen + 1) * sizeof (*fsm));
- }
-
- rspamd_substring_preprocess_kmp (srch, srchlen, fsm, f);
- ret = rspamd_substring_search_preprocessed (in, inlen, srch, srchlen, fsm, f);
-
- if (G_UNLIKELY (srchlen >= G_N_ELEMENTS (st_fsm))) {
- g_free (fsm);
- }
-
- return ret;
- }
-
- goffset
- rspamd_substring_search (const gchar *in, gsize inlen,
- const gchar *srch, gsize srchlen)
- {
- if (inlen > srchlen) {
- if (G_UNLIKELY (srchlen == 1)) {
- const gchar *p;
-
- p = memchr (in, srch[0], inlen);
-
- if (p) {
- return p - in;
- }
-
- return (-1);
- }
- else if (G_UNLIKELY (srchlen == 0)) {
- return 0;
- }
-
- return rspamd_substring_search_common (in, inlen, srch, srchlen,
- rspamd_substring_cmp_func);
- }
- else if (inlen == srchlen) {
- return (rspamd_lc_cmp (srch, in, srchlen) == 0 ? 0 : -1);
- }
- else {
- return (-1);
- }
-
- return (-1);
- }
-
- goffset
- rspamd_substring_search_caseless (const gchar *in, gsize inlen,
- const gchar *srch, gsize srchlen)
- {
- if (inlen > srchlen) {
- if (G_UNLIKELY (srchlen == 1)) {
- goffset i;
- gchar s = lc_map[(guchar)srch[0]];
-
- for (i = 0; i < inlen; i++) {
- if (lc_map[(guchar)in[i]] == s) {
- return i;
- }
- }
-
- return (-1);
- }
-
- return rspamd_substring_search_common (in, inlen, srch, srchlen,
- rspamd_substring_casecmp_func);
- }
- else if (inlen == srchlen) {
- return rspamd_lc_cmp (srch, in, srchlen) == 0 ? 0 : (-1);
- }
-
- return (-1);
- }
-
- goffset
- rspamd_string_find_eoh (GString *input, goffset *body_start)
- {
- const gchar *p, *c = NULL, *end;
- enum {
- skip_char = 0,
- got_cr,
- got_lf,
- got_linebreak,
- got_linebreak_cr,
- got_linebreak_lf,
- obs_fws
- } state = skip_char;
-
- g_assert (input != NULL);
-
- p = input->str;
- end = p + input->len;
-
- while (p < end) {
- switch (state) {
- case skip_char:
- if (*p == '\r') {
- p++;
- state = got_cr;
- }
- else if (*p == '\n') {
- p++;
- state = got_lf;
- }
- else {
- p++;
- }
- break;
-
- case got_cr:
- if (*p == '\r') {
- /*
- * Double \r\r, so need to check the current char
- * if it is '\n', then we have \r\r\n sequence, that is NOT
- * double end of line
- */
- if (p < end && p[1] == '\n') {
- p++;
- state = got_lf;
- }
- else {
- /* We have \r\r[^\n] */
- if (body_start) {
- *body_start = p - input->str + 1;
- }
-
- return p - input->str;
- }
- }
- else if (*p == '\n') {
- p++;
- state = got_lf;
- }
- else if (g_ascii_isspace (*p)) {
- /* We have \r<space>*, allow to stay in this state */
- c = p;
- p ++;
- state = obs_fws;
- }
- else {
- p++;
- state = skip_char;
- }
- break;
- case got_lf:
- if (*p == '\n') {
- /* We have \n\n, which is obviously end of headers */
- if (body_start) {
- *body_start = p - input->str + 1;
- }
- return p - input->str;
- }
- else if (*p == '\r') {
- state = got_linebreak;
- }
- else if (g_ascii_isspace (*p)) {
- /* We have \n<space>*, allow to stay in this state */
- c = p;
- p ++;
- state = obs_fws;
- }
- else {
- p++;
- state = skip_char;
- }
- break;
- case got_linebreak:
- if (*p == '\r') {
- c = p;
- p++;
- state = got_linebreak_cr;
- }
- else if (*p == '\n') {
- c = p;
- p++;
- state = got_linebreak_lf;
- }
- else if (g_ascii_isspace (*p)) {
- /* We have <linebreak><space>*, allow to stay in this state */
- c = p;
- p ++;
- state = obs_fws;
- }
- else {
- p++;
- state = skip_char;
- }
- break;
- case got_linebreak_cr:
- if (*p == '\r') {
- /* Got double \r\r after \n, so does not treat it as EOH */
- state = got_linebreak_cr;
- p++;
- }
- else if (*p == '\n') {
- state = got_linebreak_lf;
- p++;
- }
- else if (g_ascii_isspace (*p)) {
- /* We have \r\n<space>*, allow to keep in this state */
- c = p;
- state = obs_fws;
- p ++;
- }
- else {
- p++;
- state = skip_char;
- }
- break;
- case got_linebreak_lf:
- g_assert (c != NULL);
- if (body_start) {
- /* \r\n\r\n */
- *body_start = p - input->str;
- }
-
- return c - input->str;
- case obs_fws:
- if (*p == ' ' || *p == '\t') {
- p ++;
- }
- else if (*p == '\r') {
- /* Perform lookahead due to #2349 */
- if (end - p > 2) {
- if (p[1] == '\n' && g_ascii_isspace (p[2])) {
- /* Real obs_fws state, switch */
- c = p;
- p ++;
- state = got_cr;
- }
- else if (g_ascii_isspace (p[1])) {
- p ++;
- state = obs_fws;
- }
- else {
- /*
- * <nline> <wsp>+ \r <nwsp>.
- * It is an empty header likely, so we can go further...
- * https://tools.ietf.org/html/rfc2822#section-4.2
- */
- c = p;
- p ++;
- state = got_cr;
- }
- }
- else {
- /* shortage */
- if (body_start) {
- *body_start = p - input->str + 1;
- }
-
- return p - input->str;
- }
- }
- else if (*p == '\n') {
- /* Perform lookahead due to #2349 */
- if (end - p > 1) {
- /* Continue folding with an empty line */
- if (p[1] == ' ' || p[1] == '\t') {
- c = p;
- p ++;
- state = obs_fws;
- }
- else if (p[1] == '\r') {
- /* WTF state: we have seen spaces, \n and then it follows \r */
- c = p;
- p ++;
- state = got_lf;
- }
- else if (p[1] == '\n') {
- /*
- * Switching to got_lf state here will let us to finish
- * the cycle.
- */
- c = p;
- p ++;
- state = got_lf;
- }
- else {
- /*
- * <nline> <wsp>+ \n <nwsp>.
- * It is an empty header likely, so we can go further...
- * https://tools.ietf.org/html/rfc2822#section-4.2
- */
- c = p;
- p ++;
- state = got_lf;
- }
-
- }
- else {
- /* shortage */
- if (body_start) {
- *body_start = p - input->str + 1;
- }
-
- return p - input->str;
- }
- }
- else {
- p++;
- state = skip_char;
- }
- break;
- }
-
- }
-
- if (state == got_linebreak_lf) {
- if (body_start) {
- /* \r\n\r\n */
- *body_start = p - input->str;
- }
-
- return c - input->str;
- }
-
- return -1;
- }
-
- gint
- rspamd_encode_hex_buf (const guchar *in, gsize inlen, gchar *out,
- gsize outlen)
- {
- gchar *o, *end;
- const guchar *p;
- static const gchar hexdigests[16] = "0123456789abcdef";
-
- end = out + outlen;
- o = out;
- p = in;
-
- while (inlen > 0 && o < end - 1) {
- *o++ = hexdigests[((*p >> 4) & 0xF)];
- *o++ = hexdigests[((*p++) & 0xF)];
- inlen --;
- }
-
- if (o <= end) {
- return (o - out);
- }
-
- return -1;
- }
-
- gchar *
- rspamd_encode_hex (const guchar *in, gsize inlen)
- {
- gchar *out;
- gsize outlen = inlen * 2 + 1;
- gint olen;
-
- if (in == NULL) {
- return NULL;
- }
-
- out = g_malloc (outlen);
- olen = rspamd_encode_hex_buf (in, inlen, out, outlen - 1);
-
- if (olen >= 0) {
- out[olen] = '\0';
- }
- else {
- g_free (out);
-
- return NULL;
- }
-
- return out;
- }
-
- gssize
- rspamd_decode_hex_buf (const gchar *in, gsize inlen,
- guchar *out, gsize outlen)
- {
- guchar *o, *end, ret = 0;
- const gchar *p;
- gchar c;
-
- end = out + outlen;
- o = out;
- p = in;
-
- /* We ignore trailing chars if we have not even input */
- inlen = inlen - inlen % 2;
-
- while (inlen > 1 && o < end) {
- c = *p++;
-
- if (c >= '0' && c <= '9') ret = c - '0';
- else if (c >= 'A' && c <= 'F') ret = c - 'A' + 10;
- else if (c >= 'a' && c <= 'f') ret = c - 'a' + 10;
-
- c = *p++;
- ret *= 16;
-
- if (c >= '0' && c <= '9') ret += c - '0';
- else if (c >= 'A' && c <= 'F') ret += c - 'A' + 10;
- else if (c >= 'a' && c <= 'f') ret += c - 'a' + 10;
-
- *o++ = ret;
-
- inlen -= 2;
- }
-
- if (o <= end) {
- return (o - out);
- }
-
- return -1;
- }
-
- guchar*
- rspamd_decode_hex (const gchar *in, gsize inlen)
- {
- guchar *out;
- gsize outlen = (inlen / 2 + inlen % 2) + 1;
- gint olen;
-
- if (in == NULL) {
- return NULL;
- }
-
- out = g_malloc (outlen);
-
- olen = rspamd_decode_hex_buf (in, inlen, out, outlen - 1);
-
- if (olen >= 0) {
- out[olen] = '\0';
-
- return out;
- }
-
- g_free (out);
-
- return NULL;
- }
-
- gssize
- rspamd_decode_qp_buf (const gchar *in, gsize inlen,
- gchar *out, gsize outlen)
- {
- gchar *o, *end, *pos, c;
- const gchar *p;
- guchar ret;
- gssize remain, processed;
-
- p = in;
- o = out;
- end = out + outlen;
- remain = inlen;
-
- while (remain > 0 && o < end) {
- if (*p == '=') {
- remain --;
-
- if (remain == 0) {
- /* Last '=' character, bugon */
- if (end - o > 0) {
- *o++ = *p;
- }
- else {
- /* Buffer overflow */
- return (-1);
- }
-
- break;
- }
-
- p ++;
- decode:
- /* Decode character after '=' */
- c = *p++;
- remain --;
- ret = 0;
-
- if (c >= '0' && c <= '9') {
- ret = c - '0';
- }
- else if (c >= 'A' && c <= 'F') {
- ret = c - 'A' + 10;
- }
- else if (c >= 'a' && c <= 'f') {
- ret = c - 'a' + 10;
- }
- else if (c == '\r') {
- /* Eat one more endline */
- if (remain > 0 && *p == '\n') {
- p ++;
- remain --;
- }
-
- continue;
- }
- else if (c == '\n') {
- /* Soft line break */
- continue;
- }
- else {
- /* Hack, hack, hack, treat =<garbadge> as =<garbadge> */
- if (end - o > 1) {
- *o++ = '=';
- *o++ = *(p - 1);
- }
- else {
- return (-1);
- }
-
- continue;
- }
-
- if (remain > 0) {
- c = *p++;
- ret *= 16;
- remain --;
-
- if (c >= '0' && c <= '9') {
- ret += c - '0';
- }
- else if (c >= 'A' && c <= 'F') {
- ret += c - 'A' + 10;
- }
- else if (c >= 'a' && c <= 'f') {
- ret += c - 'a' + 10;
- }
- else {
- /* Treat =<good><rubbish> as =<good><rubbish> */
- if (end - o > 2) {
- *o++ = '=';
- *o++ = *(p - 2);
- *o++ = *(p - 1);
- }
- else {
- return (-1);
- }
-
- continue;
- }
-
- if (end - o > 0) {
- *o++ = (gchar)ret;
- }
- else {
- return (-1);
- }
- }
- }
- else {
- if (end - o >= remain) {
- if ((pos = memccpy (o, p, '=', remain)) == NULL) {
- /* All copied */
- o += remain;
- break;
- }
- else {
- processed = pos - o;
- remain -= processed;
- p += processed;
-
- if (remain > 0) {
- o = pos - 1;
- /*
- * Skip comparison and jump inside decode branch,
- * as we know that we have found match
- */
- goto decode;
- }
- else {
- /* Last '=' character, bugon */
- o = pos;
-
- if (end - o > 0) {
- *o = '=';
- }
- else {
- /* Buffer overflow */
- return (-1);
- }
-
- break;
- }
- }
- }
- else {
- /* Buffer overflow */
- return (-1);
- }
- }
- }
-
- return (o - out);
- }
-
- gssize
- rspamd_decode_uue_buf (const gchar *in, gsize inlen,
- gchar *out, gsize outlen)
- {
- gchar *o, *out_end;
- const gchar *p;
- gssize remain;
- gboolean base64 = FALSE;
- goffset pos;
- const gchar *nline = "\r\n";
-
- p = in;
- o = out;
- out_end = out + outlen;
- remain = inlen;
-
- /* Skip newlines */
- #define SKIP_NEWLINE do { while (remain > 0 && (*p == '\n' || *p == '\r')) {p ++; remain --; } } while (0)
- SKIP_NEWLINE;
-
- /* First of all, we need to read the first line (and probably skip it) */
- if (remain < sizeof ("begin-base64 ")) {
- /* Obviously truncated */
- return -1;
- }
-
- if (memcmp (p, "begin ", sizeof ("begin ") - 1) == 0) {
- p += sizeof ("begin ") - 1;
- remain -= sizeof ("begin ") - 1;
-
- pos = rspamd_memcspn (p, nline, remain);
- }
- else if (memcmp (p, "begin-base64 ", sizeof ("begin-base64 ") - 1) == 0) {
- base64 = TRUE;
- p += sizeof ("begin-base64 ") - 1;
- remain -= sizeof ("begin-base64 ") - 1;
- pos = rspamd_memcspn (p, nline, remain);
- }
- else {
- /* Crap */
- return (-1);
- }
-
- if (pos == -1 || remain == 0) {
- /* Crap */
- return (-1);
- }
-
- #define DEC(c) (((c) - ' ') & 077) /* single character decode */
- #define IS_DEC(c) ( (((c) - ' ') >= 0) && (((c) - ' ') <= 077 + 1) )
- #define CHAR_OUT(c) do { if (o < out_end) { *o++ = c; } else { return (-1); } } while(0)
-
- remain -= pos;
- p = p + pos;
- SKIP_NEWLINE;
-
- if (base64) {
- if (!rspamd_cryptobox_base64_decode (p,
- remain,
- out, &outlen)) {
- return (-1);
- }
-
- return outlen;
- }
-
- while (remain > 0 && o < out_end) {
- /* Main cycle */
- const gchar *eol;
- gint i, ch;
-
- pos = rspamd_memcspn (p, nline, remain);
-
- if (pos == 0) {
- /* Skip empty lines */
- SKIP_NEWLINE;
-
- if (remain == 0) {
- break;
- }
- }
-
- eol = p + pos;
- remain -= eol - p;
-
- if ((i = DEC(*p)) <= 0) {
- /* Last pos */
- break;
- }
-
- /* i can be less than eol - p, it means uue padding which we ignore */
- for (++p; i > 0 && p < eol; p += 4, i -= 3) {
- if (i >= 3 && p + 3 < eol) {
- /* Process 4 bytes of input */
- if (!IS_DEC(*p)) {
- return (-1);
- }
- if (!IS_DEC(*(p + 1))) {
- return (-1);
- }
- if (!IS_DEC(*(p + 2))) {
- return (-1);
- }
- if (!IS_DEC(*(p + 3))) {
- return (-1);
- }
- ch = DEC(p[0]) << 2 | DEC(p[1]) >> 4;
- CHAR_OUT(ch);
- ch = DEC(p[1]) << 4 | DEC(p[2]) >> 2;
- CHAR_OUT(ch);
- ch = DEC(p[2]) << 6 | DEC(p[3]);
- CHAR_OUT(ch);
- }
- else {
- if (i >= 1 && p + 1 < eol) {
- if (!IS_DEC(*p)) {
- return (-1);
- }
- if (!IS_DEC(*(p + 1))) {
- return (-1);
- }
-
- ch = DEC(p[0]) << 2 | DEC(p[1]) >> 4;
- CHAR_OUT(ch);
- }
- if (i >= 2 && p + 2 < eol) {
- if (!IS_DEC(*(p + 1))) {
- return (-1);
- }
- if (!IS_DEC(*(p + 2))) {
- return (-1);
- }
-
- ch = DEC(p[1]) << 4 | DEC(p[2]) >> 2;
- CHAR_OUT(ch);
- }
- }
- }
- /* Skip newline */
- p = eol;
- SKIP_NEWLINE;
- }
-
- return (o - out);
- }
-
- #define BITOP(a,b,op) \
- ((a)[(gsize)(b)/(8*sizeof *(a))] op (gsize)1<<((gsize)(b)%(8*sizeof *(a))))
-
-
- gsize
- rspamd_memcspn (const gchar *s, const gchar *e, gsize len)
- {
- gsize byteset[32 / sizeof(gsize)];
- const gchar *p = s, *end = s + len;
-
- if (!e[1]) {
- for (; p < end && *p != *e; p++);
- return p - s;
- }
-
- memset (byteset, 0, sizeof byteset);
-
- for (; *e && BITOP (byteset, *(guchar *)e, |=); e++);
- for (; p < end && !BITOP (byteset, *(guchar *)p, &); p++);
-
- return p - s;
- }
-
- gsize
- rspamd_memspn (const gchar *s, const gchar *e, gsize len)
- {
- gsize byteset[32 / sizeof(gsize)];
- const gchar *p = s, *end = s + len;
-
- if (!e[1]) {
- for (; p < end && *p == *e; p++);
- return p - s;
- }
-
- memset (byteset, 0, sizeof byteset);
-
- for (; *e && BITOP (byteset, *(guchar *)e, |=); e++);
- for (; p < end && BITOP (byteset, *(guchar *)p, &); p++);
-
- return p - s;
- }
-
- gssize
- rspamd_decode_qp2047_buf (const gchar *in, gsize inlen,
- gchar *out, gsize outlen)
- {
- gchar *o, *end, c;
- const gchar *p;
- guchar ret;
- gsize remain, processed;
-
- p = in;
- o = out;
- end = out + outlen;
- remain = inlen;
-
- while (remain > 0 && o < end) {
- if (*p == '=') {
- p ++;
- remain --;
-
- if (remain == 0) {
- if (end - o > 0) {
- *o++ = *p;
- break;
- }
- }
- decode:
- /* Decode character after '=' */
- c = *p++;
- remain --;
- ret = 0;
-
- if (c >= '0' && c <= '9') { ret = c - '0'; }
- else if (c >= 'A' && c <= 'F') { ret = c - 'A' + 10; }
- else if (c >= 'a' && c <= 'f') { ret = c - 'a' + 10; }
- else if (c == '\r' || c == '\n') {
- /* Soft line break */
- while (remain > 0 && (*p == '\r' || *p == '\n')) {
- remain --;
- p ++;
- }
-
- continue;
- }
-
- if (remain > 0) {
- c = *p++;
- ret *= 16;
-
- if (c >= '0' && c <= '9') { ret += c - '0'; }
- else if (c >= 'A' && c <= 'F') { ret += c - 'A' + 10; }
- else if (c >= 'a' && c <= 'f') { ret += c - 'a' + 10; }
-
- if (end - o > 0) {
- *o++ = (gchar)ret;
- }
- else {
- return (-1);
- }
-
- remain --;
- }
- }
- else {
- if (end - o >= remain) {
- processed = rspamd_memcspn (p, "=_", remain);
- memcpy (o, p, processed);
- o += processed;
-
- if (processed == remain) {
- break;
- }
- else {
-
- remain -= processed;
- p += processed;
-
- if (G_LIKELY (*p == '=')) {
- p ++;
- /* Skip comparison, as we know that we have found match */
- remain --;
- goto decode;
- }
- else {
- *o++ = ' ';
- p ++;
- remain --;
- }
- }
- }
- else {
- /* Buffer overflow */
- return (-1);
- }
- }
- }
-
- return (o - out);
- }
-
- gssize
- rspamd_encode_qp2047_buf (const gchar *in, gsize inlen,
- gchar *out, gsize outlen)
- {
- gchar *o = out, *end = out + outlen, c;
- static const gchar hexdigests[16] = "0123456789ABCDEF";
-
- while (inlen > 0 && o < end) {
- c = *in;
-
- if (g_ascii_isalnum (c)) {
- *o++ = c;
- }
- else if (c == ' ') {
- *o++ = '_';
- }
- else if (end - o >= 3){
- *o++ = '=';
- *o++ = hexdigests[((c >> 4) & 0xF)];
- *o++ = hexdigests[(c & 0xF)];
- }
- else {
- return (-1);
- }
-
- in ++;
- inlen --;
- }
-
- if (inlen != 0) {
- return (-1);
- }
-
- return (o - out);
- }
-
-
- /*
- * GString ucl emitting functions
- */
- static int
- rspamd_gstring_append_character (unsigned char c, size_t len, void *ud)
- {
- GString *buf = ud;
- gsize old_len;
-
- if (len == 1) {
- g_string_append_c (buf, c);
- }
- else {
- if (buf->allocated_len - buf->len <= len) {
- old_len = buf->len;
- g_string_set_size (buf, buf->len + len + 1);
- buf->len = old_len;
- }
- memset (&buf->str[buf->len], c, len);
- buf->len += len;
- }
-
- return 0;
- }
-
- static int
- rspamd_gstring_append_len (const unsigned char *str, size_t len, void *ud)
- {
- GString *buf = ud;
-
- g_string_append_len (buf, str, len);
-
- return 0;
- }
-
- static int
- rspamd_gstring_append_int (int64_t val, void *ud)
- {
- GString *buf = ud;
-
- rspamd_printf_gstring (buf, "%L", (intmax_t) val);
- return 0;
- }
-
- static int
- rspamd_gstring_append_double (double val, void *ud)
- {
- GString *buf = ud;
- const double delta = 0.0000001;
-
- if (isfinite (val)) {
- if (val == (double) (int) val) {
- rspamd_printf_gstring (buf, "%.1f", val);
- } else if (fabs (val - (double) (int) val) < delta) {
- /* Write at maximum precision */
- rspamd_printf_gstring (buf, "%.*g", DBL_DIG, val);
- } else {
- rspamd_printf_gstring (buf, "%f", val);
- }
- }
- else {
- rspamd_printf_gstring (buf, "null");
- }
-
- return 0;
- }
-
- void
- rspamd_ucl_emit_gstring_comments (const ucl_object_t *obj,
- enum ucl_emitter emit_type,
- GString *target,
- const ucl_object_t *comments)
- {
- struct ucl_emitter_functions func = {
- .ucl_emitter_append_character = rspamd_gstring_append_character,
- .ucl_emitter_append_len = rspamd_gstring_append_len,
- .ucl_emitter_append_int = rspamd_gstring_append_int,
- .ucl_emitter_append_double = rspamd_gstring_append_double
- };
-
- func.ud = target;
- ucl_object_emit_full (obj, emit_type, &func, comments);
- }
-
- /*
- * FString ucl emitting functions
- */
- static int
- rspamd_fstring_emit_append_character (unsigned char c, size_t len, void *ud)
- {
- rspamd_fstring_t **buf = ud;
-
- *buf = rspamd_fstring_append_chars (*buf, c, len);
-
- return 0;
- }
-
- static int
- rspamd_fstring_emit_append_len (const unsigned char *str, size_t len, void *ud)
- {
- rspamd_fstring_t **buf = ud;
-
- *buf = rspamd_fstring_append (*buf, str, len);
-
- return 0;
- }
-
- static int
- rspamd_fstring_emit_append_int (int64_t val, void *ud)
- {
- rspamd_fstring_t **buf = ud;
-
- rspamd_printf_fstring (buf, "%L", (intmax_t) val);
- return 0;
- }
-
- static int
- rspamd_fstring_emit_append_double (double val, void *ud)
- {
- rspamd_fstring_t **buf = ud;
- #define MAX_PRECISION 6
-
- if (isfinite (val)) {
- if (val == (double) ((gint) val)) {
- rspamd_printf_fstring (buf, "%.1f", val);
- } else {
- rspamd_printf_fstring (buf, "%." G_STRINGIFY (MAX_PRECISION) "f",
- val);
- }
- }
- else {
- rspamd_printf_fstring (buf, "null");
- }
-
- return 0;
- }
-
- void
- rspamd_ucl_emit_fstring_comments (const ucl_object_t *obj,
- enum ucl_emitter emit_type,
- rspamd_fstring_t **buf,
- const ucl_object_t *comments)
- {
- struct ucl_emitter_functions func = {
- .ucl_emitter_append_character = rspamd_fstring_emit_append_character,
- .ucl_emitter_append_len = rspamd_fstring_emit_append_len,
- .ucl_emitter_append_int = rspamd_fstring_emit_append_int,
- .ucl_emitter_append_double = rspamd_fstring_emit_append_double
- };
-
- func.ud = buf;
- ucl_object_emit_full (obj, emit_type, &func, comments);
- }
-
- #ifndef HAVE_MEMRCHR
- void *
- rspamd_memrchr (const void *m, gint c, gsize len)
- {
- const guint8 *p = m;
-
- for (gsize i = len; i > 0; i --) {
- if (p[i - 1] == c) {
- return (void *)(p + i - 1);
- }
- }
-
- return NULL;
- }
- #endif
-
- struct UConverter *
- rspamd_get_utf8_converter (void)
- {
- static UConverter *utf8_conv = NULL;
- UErrorCode uc_err = U_ZERO_ERROR;
-
- if (utf8_conv == NULL) {
- utf8_conv = ucnv_open ("UTF-8", &uc_err);
- if (!U_SUCCESS (uc_err)) {
- msg_err ("FATAL error: cannot open converter for utf8: %s",
- u_errorName (uc_err));
-
- g_assert_not_reached ();
- }
-
- ucnv_setFromUCallBack (utf8_conv,
- UCNV_FROM_U_CALLBACK_SUBSTITUTE,
- NULL,
- NULL,
- NULL,
- &uc_err);
- ucnv_setToUCallBack (utf8_conv,
- UCNV_TO_U_CALLBACK_SUBSTITUTE,
- NULL,
- NULL,
- NULL,
- &uc_err);
- }
-
- return utf8_conv;
- }
-
-
- const struct UNormalizer2 *
- rspamd_get_unicode_normalizer (void)
- {
- #if U_ICU_VERSION_MAJOR_NUM >= 44
- UErrorCode uc_err = U_ZERO_ERROR;
- static const UNormalizer2 *norm = NULL;
-
- if (norm == NULL) {
- norm = unorm2_getInstance (NULL, "nfkc", UNORM2_COMPOSE, &uc_err);
- g_assert (U_SUCCESS (uc_err));
- }
-
- return norm;
- #else
- /* Old libicu */
- return NULL;
- #endif
- }
-
- gchar *
- rspamd_str_regexp_escape (const gchar *pattern, gsize slen,
- gsize *dst_len, enum rspamd_regexp_escape_flags flags)
- {
- const gchar *p, *end = pattern + slen;
- gchar *res, *d, t, *tmp_utf = NULL, *dend;
- gsize len;
- static const gchar hexdigests[16] = "0123456789abcdef";
-
- len = 0;
- p = pattern;
-
- /* [-[\]{}()*+?.,\\^$|#\s] need to be escaped */
- while (p < end) {
- t = *p ++;
-
- switch (t) {
- case '[':
- case ']':
- case '-':
- case '\\':
- case '{':
- case '}':
- case '(':
- case ')':
- case '*':
- case '+':
- case '?':
- case '.':
- case ',':
- case '^':
- case '$':
- case '|':
- case '#':
- if (!(flags & RSPAMD_REGEXP_ESCAPE_RE)) {
- len++;
- }
- break;
- default:
- if (g_ascii_isspace (t)) {
- len ++;
- }
- else {
- if (!g_ascii_isprint (t) || (t & 0x80)) {
-
- if (flags & RSPAMD_REGEXP_ESCAPE_UTF) {
- /* \x{code}, where code can be up to 5 digits */
- len += 4;
- }
- else {
- /* \\xHH -> 4 symbols */
- len += 3;
- }
- }
- }
- break;
- }
- }
-
- if (flags & RSPAMD_REGEXP_ESCAPE_UTF) {
- if (rspamd_fast_utf8_validate (pattern, slen) != 0) {
- tmp_utf = rspamd_str_make_utf_valid (pattern, slen, NULL, NULL);
- }
- }
-
- if (len == 0) {
- /* No need to escape anything */
-
- if (dst_len) {
- *dst_len = slen;
- }
-
- if (tmp_utf) {
- return tmp_utf;
- }
- else {
- return g_strdup (pattern);
- }
- }
-
- /* Escape logic */
- if (tmp_utf) {
- pattern = tmp_utf;
- }
-
- len = slen + len;
- res = g_malloc (len + 1);
- p = pattern;
- d = res;
- dend = d + len;
-
- while (p < end) {
- g_assert (d < dend);
- t = *p ++;
-
- switch (t) {
- case '[':
- case ']':
- case '-':
- case '\\':
- case '{':
- case '}':
- case '(':
- case ')':
- case '.':
- case ',':
- case '^':
- case '$':
- case '|':
- case '#':
- if (!(flags & RSPAMD_REGEXP_ESCAPE_RE)) {
- *d++ = '\\';
- }
- break;
- case '*':
- case '?':
- case '+':
- if (flags & RSPAMD_REGEXP_ESCAPE_GLOB) {
- /* Treat * as .* and ? as .? */
- *d++ = '.';
- }
- else {
- if (!(flags & RSPAMD_REGEXP_ESCAPE_RE)) {
- *d++ = '\\';
- }
- }
- break;
- default:
- if (g_ascii_isspace (t)) {
- if (!(flags & RSPAMD_REGEXP_ESCAPE_RE)) {
- *d++ = '\\';
- }
- }
- else if (t & 0x80 || !g_ascii_isprint (t)) {
- if (!(flags & RSPAMD_REGEXP_ESCAPE_UTF)) {
- *d++ = '\\';
- *d++ = 'x';
- *d++ = hexdigests[((t >> 4) & 0xF)];
- *d++ = hexdigests[((t) & 0xF)];
- continue; /* To avoid *d++ = t; */
- }
- else {
- if (flags & (RSPAMD_REGEXP_ESCAPE_RE|RSPAMD_REGEXP_ESCAPE_GLOB)) {
- UChar32 uc;
- gint32 off = p - pattern - 1;
- U8_NEXT (pattern, off, slen, uc);
-
- if (uc > 0) {
- d += rspamd_snprintf (d, dend - d,
- "\\x{%xd}", uc);
- p = pattern + off;
- }
-
- continue; /* To avoid *d++ = t; */
- }
- }
- }
- break;
- }
-
- *d++ = t;
- }
-
- *d = '\0';
-
- if (dst_len) {
- *dst_len = d - res;
- }
-
- if (tmp_utf) {
- g_free (tmp_utf);
- }
-
- return res;
- }
-
-
- gchar *
- rspamd_str_make_utf_valid (const guchar *src, gsize slen,
- gsize *dstlen,
- rspamd_mempool_t *pool)
- {
- UChar32 uc;
- goffset err_offset;
- const guchar *p;
- gchar *dst, *d;
- gsize remain = slen, dlen = 0;
-
- if (src == NULL) {
- return NULL;
- }
-
- if (slen == 0) {
- if (dstlen) {
- *dstlen = 0;
- }
-
- return pool ? rspamd_mempool_strdup (pool, "") : g_strdup ("");
- }
-
- p = src;
- dlen = slen + 1; /* As we add '\0' */
-
- /* Check space required */
- while (remain > 0 && (err_offset = rspamd_fast_utf8_validate (p, remain)) > 0) {
- gint i = 0;
-
- err_offset --; /* As it returns it 1 indexed */
- p += err_offset;
- remain -= err_offset;
- dlen += err_offset;
-
- /* Each invalid character of input requires 3 bytes of output (+2 bytes) */
- while (i < remain) {
- U8_NEXT (p, i, remain, uc);
-
- if (uc < 0) {
- dlen += 2;
- }
- else {
- break;
- }
- }
-
- p += i;
- remain -= i;
- }
-
- if (pool) {
- dst = rspamd_mempool_alloc (pool, dlen + 1);
- }
- else {
- dst = g_malloc (dlen + 1);
- }
-
- p = src;
- d = dst;
- remain = slen;
-
- while (remain > 0 && (err_offset = rspamd_fast_utf8_validate (p, remain)) > 0) {
- /* Copy valid */
- err_offset --; /* As it returns it 1 indexed */
- memcpy (d, p, err_offset);
- d += err_offset;
-
- /* Append 0xFFFD for each bad character */
- gint i = 0;
-
- p += err_offset;
- remain -= err_offset;
-
- while (i < remain) {
- gint old_i = i;
- U8_NEXT (p, i, remain, uc);
-
- if (uc < 0) {
- *d++ = '\357';
- *d++ = '\277';
- *d++ = '\275';
- }
- else {
- /* Adjust p and remaining stuff and go to the outer cycle */
- i = old_i;
- break;
- }
- }
- /*
- * Now p is the first valid utf8 character and remain is the rest of the string
- * so we can continue our loop
- */
- p += i;
- remain -= i;
- }
-
- if (err_offset == 0 && remain > 0) {
- /* Last piece */
- memcpy (d, p, remain);
- d += remain;
- }
-
- /* Last '\0' */
- g_assert (dlen > d - dst);
- *d = '\0';
-
- if (dstlen) {
- *dstlen = d - dst;
- }
-
- return dst;
- }
-
- gsize
- rspamd_gstring_strip (GString *s, const gchar *strip_chars)
- {
- const gchar *p, *sc;
- gsize strip_len = 0, total = 0;
-
- p = s->str + s->len - 1;
-
- while (p >= s->str) {
- gboolean seen = FALSE;
-
- sc = strip_chars;
-
- while (*sc != '\0') {
- if (*p == *sc) {
- strip_len ++;
- seen = TRUE;
- break;
- }
-
- sc ++;
- }
-
- if (!seen) {
- break;
- }
-
- p --;
- }
-
- if (strip_len > 0) {
- s->len -= strip_len;
- s->str[s->len] = '\0';
- total += strip_len;
- }
-
- if (s->len > 0) {
- strip_len = rspamd_memspn (s->str, strip_chars, s->len);
-
- if (strip_len > 0) {
- memmove (s->str, s->str + strip_len, s->len - strip_len);
- s->len -= strip_len;
- total += strip_len;
- }
- }
-
- return total;
- }
-
- const gchar* rspamd_string_len_strip (const gchar *in,
- gsize *len,
- const gchar *strip_chars)
- {
- const gchar *p, *sc;
- gsize strip_len = 0, old_len = *len;
-
- p = in + old_len - 1;
-
- /* Trail */
- while (p >= in) {
- gboolean seen = FALSE;
-
- sc = strip_chars;
-
- while (*sc != '\0') {
- if (*p == *sc) {
- strip_len ++;
- seen = TRUE;
- break;
- }
-
- sc ++;
- }
-
- if (!seen) {
- break;
- }
-
- p --;
- }
-
- if (strip_len > 0) {
- *len -= strip_len;
- }
-
- /* Head */
- old_len = *len;
-
- if (old_len > 0) {
- strip_len = rspamd_memspn (in, strip_chars, old_len);
-
- if (strip_len > 0) {
- *len -= strip_len;
-
- return in + strip_len;
- }
- }
-
- return in;
- }
-
- gchar **
- rspamd_string_len_split (const gchar *in, gsize len, const gchar *spill,
- gint max_elts, rspamd_mempool_t *pool)
- {
- const gchar *p = in, *end = in + len;
- gsize detected_elts = 0;
- gchar **res;
-
- /* Detect number of elements */
- while (p < end) {
- gsize cur_fragment = rspamd_memcspn (p, spill, end - p);
-
- if (cur_fragment > 0) {
- detected_elts ++;
- p += cur_fragment;
-
- if (max_elts > 0 && detected_elts >= max_elts) {
- break;
- }
- }
-
- /* Something like a,,b produces {'a', 'b'} not {'a', '', 'b'} */
- p += rspamd_memspn (p, spill, end - p);
- }
-
- res = pool ?
- rspamd_mempool_alloc (pool, sizeof (gchar *) * (detected_elts + 1)) :
- g_malloc (sizeof (gchar *) * (detected_elts + 1));
- /* Last one */
- res[detected_elts] = NULL;
- detected_elts = 0;
- p = in;
-
- while (p < end) {
- gsize cur_fragment = rspamd_memcspn (p, spill, end - p);
-
- if (cur_fragment > 0) {
- gchar *elt;
-
- elt = pool ?
- rspamd_mempool_alloc (pool, cur_fragment + 1) :
- g_malloc (cur_fragment + 1);
-
- memcpy (elt, p, cur_fragment);
- elt[cur_fragment] = '\0';
-
- res[detected_elts ++] = elt;
- p += cur_fragment;
-
- if (max_elts > 0 && detected_elts >= max_elts) {
- break;
- }
- }
-
- p += rspamd_memspn (p, spill, end - p);
- }
-
- return res;
- }
-
- #if defined(__x86_64__)
- #include <x86intrin.h>
- #endif
-
- static inline gboolean
- rspamd_str_has_8bit_u64 (const guchar *beg, gsize len)
- {
- guint8 orb = 0;
-
- if (len >= 16) {
- const guchar *nextd = beg + sizeof(guint64);
- guint64 n1 = 0, n2 = 0;
-
- do {
- guint64 t;
- memcpy(&t, beg, sizeof(t));
- n1 |= t;
- memcpy(&t, nextd, sizeof(t));
- n2 |= t;
- beg += 16;
- nextd += 16;
- len -= 16;
- } while (len >= 16);
-
- /*
- * Idea from Benny Halevy <bhalevy@scylladb.com>
- * - 7-th bit set ==> orb = !(non-zero) - 1 = 0 - 1 = 0xFF
- * - 7-th bit clear ==> orb = !0 - 1 = 1 - 1 = 0x00
- */
- orb = !((n1 | n2) & 0x8080808080808080ULL) - 1;
- }
-
- while (len--) {
- orb |= *beg++;
- }
-
- return orb >= 0x80;
- }
-
- gboolean
- rspamd_str_has_8bit (const guchar *beg, gsize len)
- {
- #if defined(__x86_64__)
- if (len >= 32) {
- const uint8_t *nextd = beg + 16;
-
- __m128i n1 = _mm_set1_epi8 (0), n2;
-
- n2 = n1;
-
- while (len >= 32) {
- __m128i xmm1 = _mm_loadu_si128 ((const __m128i *)beg);
- __m128i xmm2 = _mm_loadu_si128 ((const __m128i *)nextd);
-
- n1 = _mm_or_si128 (n1, xmm1);
- n2 = _mm_or_si128 (n2, xmm2);
-
- beg += 32;
- nextd += 32;
- len -= 32;
- }
-
- n1 = _mm_or_si128 (n1, n2);
-
- /* We assume 2 complement here */
- if (_mm_movemask_epi8 (n1)) {
- return TRUE;
- }
- }
- #endif
-
- return rspamd_str_has_8bit_u64 (beg, len);
- }
|