| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806178071780817809178101781117812178131781417815178161781717818178191782017821178221782317824178251782617827178281782917830178311783217833178341783517836178371783817839178401784117842178431784417845178461784717848178491785017851178521785317854178551785617857178581785917860178611786217863178641786517866178671786817869178701787117872178731787417875178761787717878178791788017881178821788317884178851788617887178881788917890178911789217893178941789517896178971789817899179001790117902179031790417905179061790717908179091791017911179121791317914179151791617917179181791917920179211792217923179241792517926179271792817929179301793117932179331793417935179361793717938179391794017941179421794317944179451794617947179481794917950179511795217953179541795517956179571795817959179601796117962179631796417965179661796717968179691797017971179721797317974179751797617977179781797917980179811798217983179841798517986179871798817989179901799117992179931799417995179961799717998179991800018001180021800318004180051800618007180081800918010180111801218013180141801518016180171801818019180201802118022180231802418025180261802718028180291803018031180321803318034180351803618037180381803918040180411804218043180441804518046180471804818049180501805118052180531805418055180561805718058180591806018061180621806318064180651806618067180681806918070180711807218073180741807518076180771807818079180801808118082180831808418085180861808718088180891809018091180921809318094180951809618097180981809918100181011810218103181041810518106181071810818109181101811118112181131811418115181161811718118181191812018121181221812318124181251812618127181281812918130181311813218133181341813518136181371813818139181401814118142181431814418145181461814718148181491815018151181521815318154181551815618157181581815918160181611816218163181641816518166181671816818169181701817118172181731817418175181761817718178181791818018181181821818318184181851818618187181881818918190181911819218193181941819518196181971819818199182001820118202182031820418205182061820718208182091821018211182121821318214182151821618217182181821918220182211822218223182241822518226182271822818229182301823118232182331823418235182361823718238182391824018241182421824318244182451824618247182481824918250182511825218253182541825518256182571825818259182601826118262182631826418265182661826718268182691827018271182721827318274182751827618277182781827918280182811828218283182841828518286182871828818289182901829118292182931829418295182961829718298182991830018301183021830318304183051830618307183081830918310183111831218313183141831518316183171831818319183201832118322183231832418325183261832718328183291833018331183321833318334183351833618337183381833918340183411834218343183441834518346183471834818349183501835118352183531835418355183561835718358183591836018361183621836318364183651836618367183681836918370183711837218373183741837518376183771837818379183801838118382183831838418385183861838718388183891839018391183921839318394183951839618397183981839918400184011840218403184041840518406184071840818409184101841118412184131841418415184161841718418184191842018421184221842318424184251842618427184281842918430184311843218433184341843518436184371843818439184401844118442184431844418445184461844718448184491845018451184521845318454184551845618457184581845918460184611846218463184641846518466184671846818469184701847118472184731847418475184761847718478184791848018481184821848318484184851848618487184881848918490184911849218493184941849518496184971849818499185001850118502185031850418505185061850718508185091851018511185121851318514185151851618517185181851918520185211852218523185241852518526185271852818529185301853118532185331853418535185361853718538185391854018541185421854318544185451854618547185481854918550185511855218553185541855518556185571855818559185601856118562185631856418565185661856718568185691857018571185721857318574185751857618577185781857918580185811858218583185841858518586185871858818589185901859118592185931859418595185961859718598185991860018601186021860318604186051860618607186081860918610186111861218613186141861518616186171861818619186201862118622186231862418625186261862718628186291863018631186321863318634186351863618637186381863918640186411864218643186441864518646186471864818649186501865118652186531865418655186561865718658186591866018661186621866318664186651866618667186681866918670186711867218673186741867518676186771867818679186801868118682186831868418685186861868718688186891869018691186921869318694186951869618697186981869918700187011870218703187041870518706187071870818709187101871118712187131871418715187161871718718187191872018721187221872318724187251872618727187281872918730187311873218733187341873518736187371873818739187401874118742187431874418745187461874718748187491875018751187521875318754187551875618757187581875918760187611876218763187641876518766187671876818769187701877118772187731877418775187761877718778187791878018781187821878318784187851878618787187881878918790187911879218793187941879518796187971879818799188001880118802188031880418805188061880718808188091881018811188121881318814188151881618817188181881918820188211882218823188241882518826188271882818829188301883118832188331883418835188361883718838188391884018841188421884318844188451884618847188481884918850188511885218853188541885518856188571885818859188601886118862188631886418865188661886718868188691887018871188721887318874188751887618877188781887918880188811888218883188841888518886188871888818889188901889118892188931889418895188961889718898188991890018901189021890318904189051890618907189081890918910189111891218913189141891518916189171891818919189201892118922189231892418925189261892718928189291893018931189321893318934189351893618937189381893918940189411894218943189441894518946189471894818949189501895118952189531895418955189561895718958189591896018961189621896318964189651896618967189681896918970189711897218973189741897518976189771897818979189801898118982189831898418985189861898718988189891899018991189921899318994189951899618997189981899919000190011900219003190041900519006190071900819009190101901119012190131901419015190161901719018190191902019021190221902319024190251902619027190281902919030190311903219033190341903519036190371903819039190401904119042190431904419045190461904719048190491905019051190521905319054190551905619057190581905919060190611906219063190641906519066190671906819069190701907119072190731907419075190761907719078190791908019081190821908319084190851908619087190881908919090190911909219093190941909519096190971909819099191001910119102191031910419105191061910719108191091911019111191121911319114191151911619117191181911919120191211912219123191241912519126191271912819129191301913119132191331913419135191361913719138191391914019141191421914319144191451914619147191481914919150191511915219153191541915519156191571915819159191601916119162191631916419165191661916719168191691917019171191721917319174191751917619177191781917919180191811918219183191841918519186191871918819189191901919119192191931919419195191961919719198191991920019201192021920319204192051920619207192081920919210192111921219213192141921519216192171921819219192201922119222192231922419225192261922719228192291923019231192321923319234192351923619237192381923919240192411924219243192441924519246192471924819249192501925119252192531925419255192561925719258192591926019261192621926319264192651926619267192681926919270192711927219273192741927519276192771927819279192801928119282192831928419285192861928719288192891929019291192921929319294192951929619297192981929919300193011930219303193041930519306193071930819309193101931119312193131931419315193161931719318193191932019321193221932319324193251932619327193281932919330193311933219333193341933519336193371933819339193401934119342193431934419345193461934719348193491935019351193521935319354193551935619357193581935919360193611936219363193641936519366193671936819369193701937119372193731937419375193761937719378193791938019381193821938319384193851938619387193881938919390193911939219393193941939519396193971939819399194001940119402194031940419405194061940719408194091941019411194121941319414194151941619417194181941919420194211942219423194241942519426194271942819429194301943119432194331943419435194361943719438194391944019441194421944319444194451944619447194481944919450194511945219453194541945519456194571945819459194601946119462194631946419465194661946719468194691947019471194721947319474194751947619477194781947919480194811948219483194841948519486194871948819489194901949119492194931949419495194961949719498194991950019501195021950319504195051950619507195081950919510195111951219513195141951519516195171951819519195201952119522195231952419525195261952719528195291953019531195321953319534195351953619537195381953919540195411954219543195441954519546195471954819549195501955119552195531955419555195561955719558195591956019561195621956319564195651956619567195681956919570195711957219573195741957519576195771957819579195801958119582195831958419585195861958719588195891959019591195921959319594195951959619597195981959919600196011960219603196041960519606196071960819609196101961119612196131961419615196161961719618196191962019621196221962319624196251962619627196281962919630196311963219633196341963519636196371963819639196401964119642196431964419645196461964719648196491965019651196521965319654196551965619657196581965919660196611966219663196641966519666196671966819669196701967119672196731967419675196761967719678196791968019681196821968319684196851968619687196881968919690196911969219693196941969519696196971969819699197001970119702197031970419705197061970719708197091971019711197121971319714197151971619717197181971919720197211972219723197241972519726197271972819729197301973119732197331973419735197361973719738197391974019741197421974319744197451974619747197481974919750197511975219753197541975519756197571975819759197601976119762197631976419765197661976719768197691977019771197721977319774197751977619777197781977919780197811978219783197841978519786197871978819789197901979119792197931979419795197961979719798197991980019801198021980319804198051980619807198081980919810198111981219813198141981519816198171981819819198201982119822198231982419825198261982719828198291983019831198321983319834198351983619837198381983919840198411984219843198441984519846198471984819849198501985119852198531985419855198561985719858198591986019861198621986319864198651986619867198681986919870198711987219873198741987519876198771987819879198801988119882198831988419885198861988719888198891989019891198921989319894198951989619897198981989919900199011990219903199041990519906199071990819909199101991119912199131991419915199161991719918199191992019921199221992319924199251992619927199281992919930199311993219933199341993519936199371993819939199401994119942199431994419945199461994719948199491995019951199521995319954199551995619957199581995919960199611996219963199641996519966199671996819969199701997119972199731997419975199761997719978199791998019981199821998319984199851998619987199881998919990199911999219993199941999519996199971999819999200002000120002200032000420005200062000720008200092001020011200122001320014200152001620017200182001920020200212002220023200242002520026200272002820029200302003120032200332003420035200362003720038200392004020041200422004320044200452004620047200482004920050200512005220053200542005520056200572005820059200602006120062200632006420065200662006720068200692007020071200722007320074200752007620077200782007920080200812008220083200842008520086200872008820089200902009120092200932009420095200962009720098200992010020101201022010320104201052010620107201082010920110201112011220113201142011520116201172011820119201202012120122201232012420125201262012720128201292013020131201322013320134201352013620137201382013920140201412014220143201442014520146201472014820149201502015120152201532015420155201562015720158201592016020161201622016320164201652016620167201682016920170201712017220173201742017520176201772017820179201802018120182201832018420185201862018720188201892019020191201922019320194201952019620197201982019920200202012020220203202042020520206202072020820209202102021120212202132021420215202162021720218202192022020221202222022320224202252022620227202282022920230202312023220233202342023520236202372023820239202402024120242202432024420245202462024720248202492025020251202522025320254202552025620257202582025920260202612026220263202642026520266202672026820269202702027120272202732027420275202762027720278202792028020281202822028320284202852028620287202882028920290202912029220293202942029520296202972029820299203002030120302203032030420305203062030720308203092031020311203122031320314203152031620317203182031920320203212032220323203242032520326203272032820329203302033120332203332033420335203362033720338203392034020341203422034320344203452034620347203482034920350203512035220353203542035520356203572035820359203602036120362203632036420365203662036720368203692037020371203722037320374203752037620377203782037920380203812038220383203842038520386203872038820389203902039120392203932039420395203962039720398203992040020401204022040320404204052040620407204082040920410204112041220413204142041520416204172041820419204202042120422204232042420425204262042720428204292043020431204322043320434204352043620437204382043920440204412044220443204442044520446204472044820449204502045120452204532045420455204562045720458204592046020461204622046320464204652046620467204682046920470204712047220473204742047520476204772047820479204802048120482204832048420485204862048720488204892049020491204922049320494204952049620497204982049920500205012050220503205042050520506205072050820509205102051120512205132051420515205162051720518205192052020521205222052320524205252052620527205282052920530205312053220533205342053520536205372053820539205402054120542205432054420545205462054720548205492055020551205522055320554205552055620557205582055920560205612056220563205642056520566205672056820569205702057120572205732057420575205762057720578205792058020581205822058320584205852058620587205882058920590205912059220593205942059520596205972059820599206002060120602206032060420605206062060720608206092061020611206122061320614206152061620617206182061920620206212062220623206242062520626206272062820629206302063120632206332063420635206362063720638206392064020641206422064320644206452064620647206482064920650206512065220653206542065520656206572065820659206602066120662206632066420665206662066720668206692067020671206722067320674206752067620677206782067920680206812068220683206842068520686206872068820689206902069120692206932069420695206962069720698206992070020701207022070320704207052070620707207082070920710207112071220713207142071520716207172071820719207202072120722207232072420725207262072720728207292073020731207322073320734207352073620737207382073920740207412074220743207442074520746207472074820749207502075120752207532075420755207562075720758207592076020761207622076320764207652076620767207682076920770207712077220773207742077520776207772077820779207802078120782207832078420785207862078720788207892079020791207922079320794207952079620797207982079920800208012080220803208042080520806208072080820809208102081120812208132081420815208162081720818208192082020821208222082320824208252082620827208282082920830208312083220833208342083520836208372083820839208402084120842208432084420845208462084720848208492085020851208522085320854208552085620857208582085920860208612086220863208642086520866208672086820869208702087120872208732087420875208762087720878208792088020881208822088320884208852088620887208882088920890208912089220893208942089520896208972089820899209002090120902209032090420905209062090720908209092091020911209122091320914209152091620917209182091920920209212092220923209242092520926209272092820929209302093120932209332093420935209362093720938209392094020941209422094320944209452094620947209482094920950209512095220953209542095520956209572095820959209602096120962209632096420965209662096720968209692097020971209722097320974209752097620977209782097920980209812098220983209842098520986209872098820989209902099120992209932099420995209962099720998209992100021001210022100321004210052100621007210082100921010210112101221013210142101521016210172101821019210202102121022210232102421025210262102721028210292103021031210322103321034210352103621037210382103921040210412104221043210442104521046210472104821049210502105121052210532105421055210562105721058210592106021061210622106321064210652106621067210682106921070210712107221073210742107521076210772107821079210802108121082210832108421085210862108721088210892109021091210922109321094210952109621097210982109921100211012110221103211042110521106211072110821109211102111121112211132111421115211162111721118211192112021121211222112321124211252112621127211282112921130211312113221133211342113521136211372113821139211402114121142211432114421145211462114721148211492115021151211522115321154211552115621157211582115921160211612116221163211642116521166211672116821169211702117121172211732117421175211762117721178211792118021181211822118321184211852118621187211882118921190211912119221193211942119521196211972119821199212002120121202212032120421205212062120721208212092121021211212122121321214212152121621217212182121921220212212122221223212242122521226212272122821229212302123121232212332123421235212362123721238212392124021241212422124321244212452124621247212482124921250212512125221253212542125521256212572125821259212602126121262212632126421265212662126721268212692127021271212722127321274212752127621277212782127921280212812128221283212842128521286212872128821289212902129121292212932129421295212962129721298212992130021301213022130321304213052130621307213082130921310213112131221313213142131521316213172131821319213202132121322213232132421325213262132721328213292133021331213322133321334213352133621337213382133921340213412134221343213442134521346213472134821349213502135121352213532135421355213562135721358213592136021361213622136321364213652136621367213682136921370213712137221373213742137521376213772137821379213802138121382213832138421385213862138721388213892139021391213922139321394213952139621397213982139921400214012140221403214042140521406214072140821409214102141121412214132141421415214162141721418214192142021421214222142321424214252142621427214282142921430214312143221433214342143521436214372143821439214402144121442214432144421445214462144721448214492145021451214522145321454214552145621457214582145921460214612146221463214642146521466214672146821469214702147121472214732147421475214762147721478214792148021481214822148321484214852148621487214882148921490214912149221493214942149521496214972149821499215002150121502215032150421505215062150721508215092151021511215122151321514215152151621517215182151921520215212152221523215242152521526215272152821529215302153121532215332153421535215362153721538215392154021541215422154321544215452154621547215482154921550215512155221553215542155521556215572155821559215602156121562215632156421565215662156721568215692157021571215722157321574215752157621577215782157921580215812158221583215842158521586215872158821589215902159121592215932159421595215962159721598215992160021601216022160321604216052160621607216082160921610216112161221613216142161521616216172161821619216202162121622216232162421625216262162721628216292163021631216322163321634216352163621637216382163921640216412164221643216442164521646216472164821649216502165121652216532165421655216562165721658216592166021661216622166321664216652166621667216682166921670216712167221673216742167521676216772167821679216802168121682216832168421685216862168721688216892169021691216922169321694216952169621697216982169921700217012170221703217042170521706217072170821709217102171121712217132171421715217162171721718217192172021721217222172321724217252172621727217282172921730217312173221733217342173521736217372173821739217402174121742217432174421745217462174721748217492175021751217522175321754217552175621757217582175921760217612176221763217642176521766217672176821769217702177121772217732177421775217762177721778217792178021781217822178321784217852178621787217882178921790217912179221793217942179521796217972179821799218002180121802218032180421805218062180721808218092181021811218122181321814218152181621817218182181921820218212182221823218242182521826218272182821829218302183121832218332183421835218362183721838218392184021841218422184321844218452184621847218482184921850218512185221853218542185521856218572185821859218602186121862218632186421865218662186721868218692187021871218722187321874218752187621877218782187921880218812188221883218842188521886218872188821889218902189121892218932189421895218962189721898218992190021901219022190321904219052190621907219082190921910219112191221913219142191521916219172191821919219202192121922219232192421925219262192721928219292193021931219322193321934219352193621937219382193921940219412194221943219442194521946219472194821949219502195121952219532195421955219562195721958219592196021961219622196321964219652196621967219682196921970219712197221973219742197521976219772197821979219802198121982219832198421985219862198721988219892199021991219922199321994219952199621997219982199922000220012200222003220042200522006220072200822009220102201122012220132201422015220162201722018220192202022021220222202322024220252202622027220282202922030220312203222033220342203522036220372203822039220402204122042220432204422045220462204722048220492205022051220522205322054220552205622057220582205922060220612206222063220642206522066220672206822069220702207122072220732207422075220762207722078220792208022081220822208322084220852208622087220882208922090220912209222093220942209522096220972209822099221002210122102221032210422105221062210722108221092211022111221122211322114221152211622117221182211922120221212212222123221242212522126221272212822129221302213122132221332213422135221362213722138221392214022141221422214322144221452214622147221482214922150221512215222153221542215522156221572215822159221602216122162221632216422165221662216722168221692217022171221722217322174221752217622177221782217922180221812218222183221842218522186221872218822189221902219122192221932219422195221962219722198221992220022201222022220322204222052220622207222082220922210222112221222213222142221522216222172221822219222202222122222222232222422225222262222722228222292223022231222322223322234222352223622237222382223922240222412224222243222442224522246222472224822249222502225122252222532225422255222562225722258222592226022261222622226322264222652226622267222682226922270222712227222273222742227522276222772227822279222802228122282222832228422285222862228722288222892229022291222922229322294222952229622297222982229922300223012230222303223042230522306223072230822309223102231122312223132231422315223162231722318223192232022321223222232322324223252232622327223282232922330223312233222333223342233522336223372233822339223402234122342223432234422345223462234722348223492235022351223522235322354223552235622357223582235922360223612236222363223642236522366223672236822369223702237122372223732237422375223762237722378223792238022381223822238322384223852238622387223882238922390223912239222393223942239522396223972239822399224002240122402224032240422405224062240722408224092241022411224122241322414224152241622417224182241922420224212242222423224242242522426224272242822429224302243122432224332243422435224362243722438224392244022441224422244322444224452244622447224482244922450224512245222453224542245522456224572245822459224602246122462224632246422465224662246722468224692247022471224722247322474224752247622477224782247922480224812248222483224842248522486224872248822489224902249122492224932249422495224962249722498224992250022501225022250322504225052250622507225082250922510225112251222513225142251522516225172251822519225202252122522225232252422525225262252722528225292253022531225322253322534225352253622537225382253922540225412254222543225442254522546225472254822549225502255122552225532255422555225562255722558225592256022561225622256322564225652256622567225682256922570225712257222573225742257522576225772257822579225802258122582225832258422585225862258722588225892259022591225922259322594225952259622597225982259922600226012260222603226042260522606226072260822609226102261122612226132261422615226162261722618226192262022621226222262322624226252262622627226282262922630226312263222633226342263522636226372263822639226402264122642226432264422645226462264722648226492265022651226522265322654226552265622657226582265922660226612266222663226642266522666226672266822669226702267122672226732267422675226762267722678226792268022681226822268322684226852268622687226882268922690226912269222693226942269522696226972269822699227002270122702227032270422705227062270722708227092271022711227122271322714227152271622717227182271922720227212272222723227242272522726227272272822729227302273122732227332273422735227362273722738227392274022741227422274322744227452274622747227482274922750227512275222753227542275522756227572275822759227602276122762227632276422765227662276722768227692277022771227722277322774227752277622777227782277922780227812278222783227842278522786227872278822789227902279122792227932279422795227962279722798227992280022801228022280322804228052280622807228082280922810228112281222813228142281522816228172281822819228202282122822228232282422825228262282722828228292283022831228322283322834228352283622837228382283922840228412284222843228442284522846228472284822849228502285122852228532285422855228562285722858228592286022861228622286322864228652286622867228682286922870228712287222873228742287522876228772287822879228802288122882228832288422885228862288722888228892289022891228922289322894228952289622897228982289922900229012290222903229042290522906229072290822909229102291122912229132291422915229162291722918229192292022921229222292322924229252292622927229282292922930229312293222933229342293522936229372293822939229402294122942229432294422945229462294722948229492295022951229522295322954229552295622957229582295922960229612296222963229642296522966229672296822969229702297122972229732297422975229762297722978229792298022981229822298322984229852298622987229882298922990229912299222993229942299522996229972299822999230002300123002230032300423005230062300723008230092301023011230122301323014230152301623017230182301923020230212302223023230242302523026230272302823029230302303123032230332303423035230362303723038230392304023041230422304323044230452304623047230482304923050230512305223053230542305523056230572305823059230602306123062230632306423065230662306723068230692307023071230722307323074230752307623077230782307923080230812308223083230842308523086230872308823089230902309123092230932309423095230962309723098230992310023101231022310323104231052310623107231082310923110231112311223113231142311523116231172311823119231202312123122231232312423125231262312723128231292313023131231322313323134231352313623137231382313923140231412314223143231442314523146231472314823149231502315123152231532315423155231562315723158231592316023161231622316323164231652316623167231682316923170231712317223173231742317523176231772317823179231802318123182231832318423185231862318723188231892319023191231922319323194231952319623197231982319923200232012320223203232042320523206232072320823209232102321123212232132321423215232162321723218232192322023221232222322323224232252322623227232282322923230232312323223233232342323523236232372323823239232402324123242232432324423245232462324723248232492325023251232522325323254232552325623257232582325923260232612326223263232642326523266232672326823269232702327123272232732327423275232762327723278232792328023281232822328323284232852328623287232882328923290232912329223293232942329523296232972329823299233002330123302233032330423305233062330723308233092331023311233122331323314233152331623317233182331923320233212332223323233242332523326233272332823329233302333123332233332333423335233362333723338233392334023341233422334323344233452334623347233482334923350233512335223353233542335523356233572335823359233602336123362233632336423365233662336723368233692337023371233722337323374233752337623377233782337923380233812338223383233842338523386233872338823389233902339123392233932339423395233962339723398233992340023401234022340323404234052340623407234082340923410234112341223413234142341523416234172341823419234202342123422234232342423425234262342723428234292343023431234322343323434234352343623437234382343923440234412344223443234442344523446234472344823449234502345123452234532345423455234562345723458234592346023461234622346323464234652346623467234682346923470234712347223473234742347523476234772347823479234802348123482234832348423485234862348723488234892349023491234922349323494234952349623497234982349923500235012350223503 | 
#if (defined(_WIN32) || defined(WIN32)) && !defined(_CRT_SECURE_NO_WARNINGS)#define _CRT_SECURE_NO_WARNINGS#endif# define SHELL_STRINGIFY_(f) #f# define SHELL_STRINGIFY(f) SHELL_STRINGIFY_(f)#ifdef SQLITE_CUSTOM_INCLUDE# include SHELL_STRINGIFY(SQLITE_CUSTOM_INCLUDE)#endif#if !defined(SQLITE_OS_WINRT)# define SQLITE_OS_WINRT 0#endif#if defined(_MSC_VER)#pragma warning(disable : 4054)#pragma warning(disable : 4055)#pragma warning(disable : 4100)#pragma warning(disable : 4127)#pragma warning(disable : 4130)#pragma warning(disable : 4152)#pragma warning(disable : 4189)#pragma warning(disable : 4206)#pragma warning(disable : 4210)#pragma warning(disable : 4232)#pragma warning(disable : 4244)#pragma warning(disable : 4305)#pragma warning(disable : 4306)#pragma warning(disable : 4702)#pragma warning(disable : 4706)#endif #if (defined(__RTP__) || defined(_WRS_KERNEL)) && !SQLITE_OMIT_LOAD_EXTENSION# define SQLITE_OMIT_LOAD_EXTENSION 1#endif#ifndef SQLITE_DISABLE_LFS# define _LARGE_FILE       1# ifndef _FILE_OFFSET_BITS#   define _FILE_OFFSET_BITS 64# endif# define _LARGEFILE_SOURCE 1#endif#include <stdlib.h>#include <string.h>#include <stdio.h>#include <assert.h>#include "sqlite3.h"typedef sqlite3_int64 i64;typedef sqlite3_uint64 u64;typedef unsigned char u8;#if SQLITE_USER_AUTHENTICATION# include "sqlite3userauth.h"#endif#include <ctype.h>#include <stdarg.h>#if !defined(_WIN32) && !defined(WIN32)# include <signal.h># if !defined(__RTP__) && !defined(_WRS_KERNEL)#  include <pwd.h># endif#endif#if (!defined(_WIN32) && !defined(WIN32)) || defined(__MINGW32__)# include <unistd.h># include <dirent.h># define GETPID getpid# if defined(__MINGW32__)#  define DIRENT dirent#  ifndef S_ISLNK#   define S_ISLNK(mode) (0)#  endif# endif#else# define GETPID (int)GetCurrentProcessId#endif#include <sys/types.h>#include <sys/stat.h>#if HAVE_READLINE# include <readline/readline.h># include <readline/history.h>#endif#if HAVE_EDITLINE# include <editline/readline.h>#endif#if HAVE_EDITLINE || HAVE_READLINE# define shell_add_history(X) add_history(X)# define shell_read_history(X) read_history(X)# define shell_write_history(X) write_history(X)# define shell_stifle_history(X) stifle_history(X)# define shell_readline(X) readline(X)#elif HAVE_LINENOISE# include "linenoise.h"# define shell_add_history(X) linenoiseHistoryAdd(X)# define shell_read_history(X) linenoiseHistoryLoad(X)# define shell_write_history(X) linenoiseHistorySave(X)# define shell_stifle_history(X) linenoiseHistorySetMaxLen(X)# define shell_readline(X) linenoise(X)#else# define shell_read_history(X)# define shell_write_history(X)# define shell_stifle_history(X)# define SHELL_USE_LOCAL_GETLINE 1#endif#if defined(_WIN32) || defined(WIN32)# if SQLITE_OS_WINRT#  define SQLITE_OMIT_POPEN 1# else#  include <io.h>#  include <fcntl.h>#  define isatty(h) _isatty(h)#  ifndef access#   define access(f,m) _access((f),(m))#  endif#  ifndef unlink#   define unlink _unlink#  endif#  ifndef strdup#   define strdup _strdup#  endif#  undef popen#  define popen _popen#  undef pclose#  define pclose _pclose# endif#else  extern int isatty(int);# if !defined(__RTP__) && !defined(_WRS_KERNEL)     extern FILE *popen(const char*,const char*);   extern int pclose(FILE*);# else#  define SQLITE_OMIT_POPEN 1# endif#endif#if defined(_WIN32_WCE)#define isatty(x) 1#endif#define IsSpace(X)  isspace((unsigned char)X)#define IsDigit(X)  isdigit((unsigned char)X)#define ToLower(X)  (char)tolower((unsigned char)X)#if defined(_WIN32) || defined(WIN32)#if SQLITE_OS_WINRT#include <intrin.h>#endif#include <windows.h>extern char *sqlite3_win32_unicode_to_utf8(LPCWSTR);extern char *sqlite3_win32_mbcs_to_utf8_v2(const char *, int);extern char *sqlite3_win32_utf8_to_mbcs_v2(const char *, int);extern LPWSTR sqlite3_win32_utf8_to_unicode(const char *zText);#endif#if (defined(_WIN32) || defined(WIN32)) && !SQLITE_OS_WINRTstatic void setBinaryMode(FILE *file, int isOutput){  if( isOutput ) fflush(file);  _setmode(_fileno(file), _O_BINARY);}static void setTextMode(FILE *file, int isOutput){  if( isOutput ) fflush(file);  _setmode(_fileno(file), _O_TEXT);}#else# define setBinaryMode(X,Y)# define setTextMode(X,Y)#endifstatic int enableTimer = 0;static sqlite3_int64 timeOfDay(void){  static sqlite3_vfs *clockVfs = 0;  sqlite3_int64 t;  if( clockVfs==0 ) clockVfs = sqlite3_vfs_find(0);  if( clockVfs==0 ) return 0;    if( clockVfs->iVersion>=2 && clockVfs->xCurrentTimeInt64!=0 ){    clockVfs->xCurrentTimeInt64(clockVfs, &t);  }else{    double r;    clockVfs->xCurrentTime(clockVfs, &r);    t = (sqlite3_int64)(r*86400000.0);  }  return t;}#if !defined(_WIN32) && !defined(WIN32) && !defined(__minux)#include <sys/time.h>#include <sys/resource.h>#if defined(_WRS_KERNEL) || defined(__RTP__)struct rusage {  struct timeval ru_utime;   struct timeval ru_stime; };#define getrusage(A,B) memset(B,0,sizeof(*B))#endifstatic struct rusage sBegin;  static sqlite3_int64 iBegin;  static void beginTimer(void){  if( enableTimer ){    getrusage(RUSAGE_SELF, &sBegin);    iBegin = timeOfDay();  }}static double timeDiff(struct timeval *pStart, struct timeval *pEnd){  return (pEnd->tv_usec - pStart->tv_usec)*0.000001 +         (double)(pEnd->tv_sec - pStart->tv_sec);}static void endTimer(void){  if( enableTimer ){    sqlite3_int64 iEnd = timeOfDay();    struct rusage sEnd;    getrusage(RUSAGE_SELF, &sEnd);    printf("Run Time: real %.3f user %f sys %f\n",       (iEnd - iBegin)*0.001,       timeDiff(&sBegin.ru_utime, &sEnd.ru_utime),       timeDiff(&sBegin.ru_stime, &sEnd.ru_stime));  }}#define BEGIN_TIMER beginTimer()#define END_TIMER endTimer()#define HAS_TIMER 1#elif (defined(_WIN32) || defined(WIN32))static HANDLE hProcess;static FILETIME ftKernelBegin;static FILETIME ftUserBegin;static sqlite3_int64 ftWallBegin;typedef BOOL (WINAPI *GETPROCTIMES)(HANDLE, LPFILETIME, LPFILETIME,                                    LPFILETIME, LPFILETIME);static GETPROCTIMES getProcessTimesAddr = NULL;static int hasTimer(void){  if( getProcessTimesAddr ){    return 1;  } else {#if !SQLITE_OS_WINRT        hProcess = GetCurrentProcess();    if( hProcess ){      HINSTANCE hinstLib = LoadLibrary(TEXT("Kernel32.dll"));      if( NULL != hinstLib ){        getProcessTimesAddr =            (GETPROCTIMES) GetProcAddress(hinstLib, "GetProcessTimes");        if( NULL != getProcessTimesAddr ){          return 1;        }        FreeLibrary(hinstLib);      }    }#endif  }  return 0;}static void beginTimer(void){  if( enableTimer && getProcessTimesAddr ){    FILETIME ftCreation, ftExit;    getProcessTimesAddr(hProcess,&ftCreation,&ftExit,                        &ftKernelBegin,&ftUserBegin);    ftWallBegin = timeOfDay();  }}static double timeDiff(FILETIME *pStart, FILETIME *pEnd){  sqlite_int64 i64Start = *((sqlite_int64 *) pStart);  sqlite_int64 i64End = *((sqlite_int64 *) pEnd);  return (double) ((i64End - i64Start) / 10000000.0);}static void endTimer(void){  if( enableTimer && getProcessTimesAddr){    FILETIME ftCreation, ftExit, ftKernelEnd, ftUserEnd;    sqlite3_int64 ftWallEnd = timeOfDay();    getProcessTimesAddr(hProcess,&ftCreation,&ftExit,&ftKernelEnd,&ftUserEnd);    printf("Run Time: real %.3f user %f sys %f\n",       (ftWallEnd - ftWallBegin)*0.001,       timeDiff(&ftUserBegin, &ftUserEnd),       timeDiff(&ftKernelBegin, &ftKernelEnd));  }}#define BEGIN_TIMER beginTimer()#define END_TIMER endTimer()#define HAS_TIMER hasTimer()#else#define BEGIN_TIMER#define END_TIMER#define HAS_TIMER 0#endif#define UNUSED_PARAMETER(x) (void)(x)#define ArraySize(X)  (int)(sizeof(X)/sizeof(X[0]))static int bail_on_error = 0;static int stdin_is_interactive = 1;static int stdout_is_console = 1;static sqlite3 *globalDb = 0;static volatile int seenInterrupt = 0;static char *Argv0;static char mainPrompt[20];     static char continuePrompt[20]; #if defined(_WIN32) || defined(WIN32)void utf8_printf(FILE *out, const char *zFormat, ...){  va_list ap;  va_start(ap, zFormat);  if( stdout_is_console && (out==stdout || out==stderr) ){    char *z1 = sqlite3_vmprintf(zFormat, ap);    char *z2 = sqlite3_win32_utf8_to_mbcs_v2(z1, 0);    sqlite3_free(z1);    fputs(z2, out);    sqlite3_free(z2);  }else{    vfprintf(out, zFormat, ap);  }  va_end(ap);}#elif !defined(utf8_printf)# define utf8_printf fprintf#endif#if !defined(raw_printf)# define raw_printf fprintf#endifstatic void shell_out_of_memory(void){  raw_printf(stderr,"Error: out of memory\n");  exit(1);}static void shell_check_oom(void *p){  if( p==0 ) shell_out_of_memory();}#ifdef SQLITE_ENABLE_IOTRACEstatic FILE *iotrace = 0;#endif#ifdef SQLITE_ENABLE_IOTRACEstatic void SQLITE_CDECL iotracePrintf(const char *zFormat, ...){  va_list ap;  char *z;  if( iotrace==0 ) return;  va_start(ap, zFormat);  z = sqlite3_vmprintf(zFormat, ap);  va_end(ap);  utf8_printf(iotrace, "%s", z);  sqlite3_free(z);}#endifstatic void utf8_width_print(FILE *pOut, int w, const char *zUtf){  int i;  int n;  int aw = w<0 ? -w : w;  for(i=n=0; zUtf[i]; i++){    if( (zUtf[i]&0xc0)!=0x80 ){      n++;      if( n==aw ){        do{ i++; }while( (zUtf[i]&0xc0)==0x80 );        break;      }    }  }  if( n>=aw ){    utf8_printf(pOut, "%.*s", i, zUtf);  }else if( w<0 ){    utf8_printf(pOut, "%*s%s", aw-n, "", zUtf);  }else{    utf8_printf(pOut, "%s%*s", zUtf, aw-n, "");  }}static int isNumber(const char *z, int *realnum){  if( *z=='-' || *z=='+' ) z++;  if( !IsDigit(*z) ){    return 0;  }  z++;  if( realnum ) *realnum = 0;  while( IsDigit(*z) ){ z++; }  if( *z=='.' ){    z++;    if( !IsDigit(*z) ) return 0;    while( IsDigit(*z) ){ z++; }    if( realnum ) *realnum = 1;  }  if( *z=='e' || *z=='E' ){    z++;    if( *z=='+' || *z=='-' ) z++;    if( !IsDigit(*z) ) return 0;    while( IsDigit(*z) ){ z++; }    if( realnum ) *realnum = 1;  }  return *z==0;}static int strlen30(const char *z){  const char *z2 = z;  while( *z2 ){ z2++; }  return 0x3fffffff & (int)(z2 - z);}static int strlenChar(const char *z){  int n = 0;  while( *z ){    if( (0xc0&*(z++))!=0x80 ) n++;  }  return n;}static FILE * openChrSource(const char *zFile){#ifdef _WIN32  struct _stat x = {0};# define STAT_CHR_SRC(mode) ((mode & (_S_IFCHR|_S_IFIFO|_S_IFREG))!=0)    FILE *rv = fopen(zFile, "rb");  if( rv==0 ) return 0;  if( _fstat(_fileno(rv), &x) != 0      || !STAT_CHR_SRC(x.st_mode)){    fclose(rv);    rv = 0;  }  return rv;#else  struct stat x = {0};  int rc = stat(zFile, &x);# define STAT_CHR_SRC(mode) (S_ISREG(mode)||S_ISFIFO(mode)||S_ISCHR(mode))  if( rc!=0 ) return 0;  if( STAT_CHR_SRC(x.st_mode) ){    return fopen(zFile, "rb");  }else{    return 0;  }#endif#undef STAT_CHR_SRC}static char *local_getline(char *zLine, FILE *in){  int nLine = zLine==0 ? 0 : 100;  int n = 0;  while( 1 ){    if( n+100>nLine ){      nLine = nLine*2 + 100;      zLine = realloc(zLine, nLine);      shell_check_oom(zLine);    }    if( fgets(&zLine[n], nLine - n, in)==0 ){      if( n==0 ){        free(zLine);        return 0;      }      zLine[n] = 0;      break;    }    while( zLine[n] ) n++;    if( n>0 && zLine[n-1]=='\n' ){      n--;      if( n>0 && zLine[n-1]=='\r' ) n--;      zLine[n] = 0;      break;    }  }#if defined(_WIN32) || defined(WIN32)    if( stdin_is_interactive && in==stdin ){    char *zTrans = sqlite3_win32_mbcs_to_utf8_v2(zLine, 0);    if( zTrans ){      int nTrans = strlen30(zTrans)+1;      if( nTrans>nLine ){        zLine = realloc(zLine, nTrans);        shell_check_oom(zLine);      }      memcpy(zLine, zTrans, nTrans);      sqlite3_free(zTrans);    }  }#endif   return zLine;}static char *one_input_line(FILE *in, char *zPrior, int isContinuation){  char *zPrompt;  char *zResult;  if( in!=0 ){    zResult = local_getline(zPrior, in);  }else{    zPrompt = isContinuation ? continuePrompt : mainPrompt;#if SHELL_USE_LOCAL_GETLINE    printf("%s", zPrompt);    fflush(stdout);    zResult = local_getline(zPrior, stdin);#else    free(zPrior);    zResult = shell_readline(zPrompt);    if( zResult && *zResult ) shell_add_history(zResult);#endif  }  return zResult;}static int hexDigitValue(char c){  if( c>='0' && c<='9' ) return c - '0';  if( c>='a' && c<='f' ) return c - 'a' + 10;  if( c>='A' && c<='F' ) return c - 'A' + 10;  return -1;}static sqlite3_int64 integerValue(const char *zArg){  sqlite3_int64 v = 0;  static const struct { char *zSuffix; int iMult; } aMult[] = {    { "KiB", 1024 },    { "MiB", 1024*1024 },    { "GiB", 1024*1024*1024 },    { "KB",  1000 },    { "MB",  1000000 },    { "GB",  1000000000 },    { "K",   1000 },    { "M",   1000000 },    { "G",   1000000000 },  };  int i;  int isNeg = 0;  if( zArg[0]=='-' ){    isNeg = 1;    zArg++;  }else if( zArg[0]=='+' ){    zArg++;  }  if( zArg[0]=='0' && zArg[1]=='x' ){    int x;    zArg += 2;    while( (x = hexDigitValue(zArg[0]))>=0 ){      v = (v<<4) + x;      zArg++;    }  }else{    while( IsDigit(zArg[0]) ){      v = v*10 + zArg[0] - '0';      zArg++;    }  }  for(i=0; i<ArraySize(aMult); i++){    if( sqlite3_stricmp(aMult[i].zSuffix, zArg)==0 ){      v *= aMult[i].iMult;      break;    }  }  return isNeg? -v : v;}typedef struct ShellText ShellText;struct ShellText {  char *z;  int n;  int nAlloc;};static void initText(ShellText *p){  memset(p, 0, sizeof(*p));}static void freeText(ShellText *p){  free(p->z);  initText(p);}static void appendText(ShellText *p, char const *zAppend, char quote){  int len;  int i;  int nAppend = strlen30(zAppend);  len = nAppend+p->n+1;  if( quote ){    len += 2;    for(i=0; i<nAppend; i++){      if( zAppend[i]==quote ) len++;    }  }  if( p->z==0 || p->n+len>=p->nAlloc ){    p->nAlloc = p->nAlloc*2 + len + 20;    p->z = realloc(p->z, p->nAlloc);    shell_check_oom(p->z);  }  if( quote ){    char *zCsr = p->z+p->n;    *zCsr++ = quote;    for(i=0; i<nAppend; i++){      *zCsr++ = zAppend[i];      if( zAppend[i]==quote ) *zCsr++ = quote;    }    *zCsr++ = quote;    p->n = (int)(zCsr - p->z);    *zCsr = '\0';  }else{    memcpy(p->z+p->n, zAppend, nAppend);    p->n += nAppend;    p->z[p->n] = '\0';  }}static char quoteChar(const char *zName){  int i;  if( !isalpha((unsigned char)zName[0]) && zName[0]!='_' ) return '"';  for(i=0; zName[i]; i++){    if( !isalnum((unsigned char)zName[i]) && zName[i]!='_' ) return '"';  }  return sqlite3_keyword_check(zName, i) ? '"' : 0;}static char *shellFakeSchema(  sqlite3 *db,              const char *zSchema,      const char *zName       ){  sqlite3_stmt *pStmt = 0;  char *zSql;  ShellText s;  char cQuote;  char *zDiv = "(";  int nRow = 0;  zSql = sqlite3_mprintf("PRAGMA \"%w\".table_info=%Q;",                         zSchema ? zSchema : "main", zName);  shell_check_oom(zSql);  sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);  sqlite3_free(zSql);  initText(&s);  if( zSchema ){    cQuote = quoteChar(zSchema);    if( cQuote && sqlite3_stricmp(zSchema,"temp")==0 ) cQuote = 0;    appendText(&s, zSchema, cQuote);    appendText(&s, ".", 0);  }  cQuote = quoteChar(zName);  appendText(&s, zName, cQuote);  while( sqlite3_step(pStmt)==SQLITE_ROW ){    const char *zCol = (const char*)sqlite3_column_text(pStmt, 1);    nRow++;    appendText(&s, zDiv, 0);    zDiv = ",";    if( zCol==0 ) zCol = "";    cQuote = quoteChar(zCol);    appendText(&s, zCol, cQuote);  }  appendText(&s, ")", 0);  sqlite3_finalize(pStmt);  if( nRow==0 ){    freeText(&s);    s.z = 0;  }  return s.z;}static void shellModuleSchema(  sqlite3_context *pCtx,  int nVal,  sqlite3_value **apVal){  const char *zName;  char *zFake;  UNUSED_PARAMETER(nVal);  zName = (const char*)sqlite3_value_text(apVal[0]);  zFake = zName ? shellFakeSchema(sqlite3_context_db_handle(pCtx), 0, zName) : 0;  if( zFake ){    sqlite3_result_text(pCtx, sqlite3_mprintf("/* %s */", zFake),                        -1, sqlite3_free);    free(zFake);  }}static void shellAddSchemaName(  sqlite3_context *pCtx,  int nVal,  sqlite3_value **apVal){  static const char *aPrefix[] = {     "TABLE",     "INDEX",     "UNIQUE INDEX",     "VIEW",     "TRIGGER",     "VIRTUAL TABLE"  };  int i = 0;  const char *zIn = (const char*)sqlite3_value_text(apVal[0]);  const char *zSchema = (const char*)sqlite3_value_text(apVal[1]);  const char *zName = (const char*)sqlite3_value_text(apVal[2]);  sqlite3 *db = sqlite3_context_db_handle(pCtx);  UNUSED_PARAMETER(nVal);  if( zIn!=0 && strncmp(zIn, "CREATE ", 7)==0 ){    for(i=0; i<ArraySize(aPrefix); i++){      int n = strlen30(aPrefix[i]);      if( strncmp(zIn+7, aPrefix[i], n)==0 && zIn[n+7]==' ' ){        char *z = 0;        char *zFake = 0;        if( zSchema ){          char cQuote = quoteChar(zSchema);          if( cQuote && sqlite3_stricmp(zSchema,"temp")!=0 ){            z = sqlite3_mprintf("%.*s \"%w\".%s", n+7, zIn, zSchema, zIn+n+8);          }else{            z = sqlite3_mprintf("%.*s %s.%s", n+7, zIn, zSchema, zIn+n+8);          }        }        if( zName         && aPrefix[i][0]=='V'         && (zFake = shellFakeSchema(db, zSchema, zName))!=0        ){          if( z==0 ){            z = sqlite3_mprintf("%s\n/* %s */", zIn, zFake);          }else{            z = sqlite3_mprintf("%z\n/* %s */", z, zFake);          }          free(zFake);        }        if( z ){          sqlite3_result_text(pCtx, z, -1, sqlite3_free);          return;        }      }    }  }  sqlite3_result_value(pCtx, apVal[0]);}#define SQLITE_EXTENSION_INIT1#define SQLITE_EXTENSION_INIT2(X) (void)(X)#if defined(_WIN32) && defined(_MSC_VER)#if defined(_WIN32) && defined(_MSC_VER) && !defined(SQLITE_WINDIRENT_H)#define SQLITE_WINDIRENT_H#ifndef WIN32_LEAN_AND_MEAN#define WIN32_LEAN_AND_MEAN#endif#include "windows.h"#include <stdio.h>#include <stdlib.h>#include <errno.h>#include <io.h>#include <limits.h>#include <sys/types.h>#include <sys/stat.h>#ifndef S_ISREG#define S_ISREG(mode) (((mode) & S_IFMT) == S_IFREG)#endif#ifndef S_ISDIR#define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)#endif#ifndef S_ISLNK#define S_ISLNK(mode) (0)#endif#ifndef MODE_T_DEFINED  #define MODE_T_DEFINED  typedef unsigned short mode_t;#endif#ifndef INO_T_DEFINED  #define INO_T_DEFINED  typedef unsigned short ino_t;#endif#ifndef NAME_MAX#  ifdef FILENAME_MAX#    define NAME_MAX (FILENAME_MAX)#  else#    define NAME_MAX (260)#  endif#endif#ifndef NULL_INTPTR_T#  define NULL_INTPTR_T ((intptr_t)(0))#endif#ifndef BAD_INTPTR_T#  define BAD_INTPTR_T ((intptr_t)(-1))#endif#ifndef DIRENT_DEFINED#define DIRENT_DEFINEDtypedef struct DIRENT DIRENT;typedef DIRENT *LPDIRENT;struct DIRENT {  ino_t d_ino;                 unsigned d_attributes;       char d_name[NAME_MAX + 1]; };#endif#ifndef DIR_DEFINED#define DIR_DEFINEDtypedef struct DIR DIR;typedef DIR *LPDIR;struct DIR {  intptr_t d_handle;   DIRENT d_first;      DIRENT d_next;     };#endif#ifndef is_filtered#  define is_filtered(a) ((((a).attrib)&_A_HIDDEN) || (((a).attrib)&_A_SYSTEM))#endifextern const char *windirent_getenv(const char *name);extern LPDIR opendir(const char *dirname);extern LPDIRENT readdir(LPDIR dirp);extern INT readdir_r(LPDIR dirp, LPDIRENT entry, LPDIRENT *result);extern INT closedir(LPDIR dirp);#endif #if defined(_WIN32) && defined(_MSC_VER)const char *windirent_getenv(  const char *name){  static char value[32768];   DWORD dwSize = sizeof(value) / sizeof(char);   DWORD dwRet;   memset(value, 0, sizeof(value));  dwRet = GetEnvironmentVariableA(name, value, dwSize);  if( dwRet==0 || dwRet>dwSize ){        return 0;  }else{        return value;  }}LPDIR opendir(  const char *dirname){  struct _finddata_t data;  LPDIR dirp = (LPDIR)sqlite3_malloc(sizeof(DIR));  SIZE_T namesize = sizeof(data.name) / sizeof(data.name[0]);  if( dirp==NULL ) return NULL;  memset(dirp, 0, sizeof(DIR));    if( sqlite3_stricmp(dirname, "/")==0 ){    dirname = windirent_getenv("SystemDrive");  }  memset(&data, 0, sizeof(struct _finddata_t));  _snprintf(data.name, namesize, "%s\\*", dirname);  dirp->d_handle = _findfirst(data.name, &data);  if( dirp->d_handle==BAD_INTPTR_T ){    closedir(dirp);    return NULL;  }    if( is_filtered(data) ){next:    memset(&data, 0, sizeof(struct _finddata_t));    if( _findnext(dirp->d_handle, &data)==-1 ){      closedir(dirp);      return NULL;    }        if( is_filtered(data) ) goto next;  }  dirp->d_first.d_attributes = data.attrib;  strncpy(dirp->d_first.d_name, data.name, NAME_MAX);  dirp->d_first.d_name[NAME_MAX] = '\0';  return dirp;}LPDIRENT readdir(  LPDIR dirp){  struct _finddata_t data;  if( dirp==NULL ) return NULL;  if( dirp->d_first.d_ino==0 ){    dirp->d_first.d_ino++;    dirp->d_next.d_ino++;    return &dirp->d_first;  }next:  memset(&data, 0, sizeof(struct _finddata_t));  if( _findnext(dirp->d_handle, &data)==-1 ) return NULL;    if( is_filtered(data) ) goto next;  dirp->d_next.d_ino++;  dirp->d_next.d_attributes = data.attrib;  strncpy(dirp->d_next.d_name, data.name, NAME_MAX);  dirp->d_next.d_name[NAME_MAX] = '\0';  return &dirp->d_next;}INT readdir_r(  LPDIR dirp,  LPDIRENT entry,  LPDIRENT *result){  struct _finddata_t data;  if( dirp==NULL ) return EBADF;  if( dirp->d_first.d_ino==0 ){    dirp->d_first.d_ino++;    dirp->d_next.d_ino++;    entry->d_ino = dirp->d_first.d_ino;    entry->d_attributes = dirp->d_first.d_attributes;    strncpy(entry->d_name, dirp->d_first.d_name, NAME_MAX);    entry->d_name[NAME_MAX] = '\0';    *result = entry;    return 0;  }next:  memset(&data, 0, sizeof(struct _finddata_t));  if( _findnext(dirp->d_handle, &data)==-1 ){    *result = NULL;    return ENOENT;  }    if( is_filtered(data) ) goto next;  entry->d_ino = (ino_t)-1;   entry->d_attributes = data.attrib;  strncpy(entry->d_name, data.name, NAME_MAX);  entry->d_name[NAME_MAX] = '\0';  *result = entry;  return 0;}INT closedir(  LPDIR dirp){  INT result = 0;  if( dirp==NULL ) return EINVAL;  if( dirp->d_handle!=NULL_INTPTR_T && dirp->d_handle!=BAD_INTPTR_T ){    result = _findclose(dirp->d_handle);  }  sqlite3_free(dirp);  return result;}#endif #define dirent DIRENT#endifSQLITE_EXTENSION_INIT1#include <assert.h>#include <string.h>#include <stdarg.h>#ifndef SQLITE_AMALGAMATION#endif #ifndef SHA3_BYTEORDER# if defined(i386)     || defined(__i386__)   || defined(_M_IX86) ||    \     defined(__x86_64) || defined(__x86_64__) || defined(_M_X64)  ||    \     defined(_M_AMD64) || defined(_M_ARM)     || defined(__x86)   ||    \     defined(__arm__)#   define SHA3_BYTEORDER    1234# elif defined(sparc)    || defined(__ppc__)#   define SHA3_BYTEORDER    4321# else#   define SHA3_BYTEORDER 0# endif#endiftypedef struct SHA3Context SHA3Context;struct SHA3Context {  union {    u64 s[25];                    unsigned char x[1600];      } u;  unsigned nRate;          unsigned nLoaded;        unsigned ixMask;       };static void KeccakF1600Step(SHA3Context *p){  int i;  u64 b0, b1, b2, b3, b4;  u64 c0, c1, c2, c3, c4;  u64 d0, d1, d2, d3, d4;  static const u64 RC[] = {    0x0000000000000001ULL,  0x0000000000008082ULL,    0x800000000000808aULL,  0x8000000080008000ULL,    0x000000000000808bULL,  0x0000000080000001ULL,    0x8000000080008081ULL,  0x8000000000008009ULL,    0x000000000000008aULL,  0x0000000000000088ULL,    0x0000000080008009ULL,  0x000000008000000aULL,    0x000000008000808bULL,  0x800000000000008bULL,    0x8000000000008089ULL,  0x8000000000008003ULL,    0x8000000000008002ULL,  0x8000000000000080ULL,    0x000000000000800aULL,  0x800000008000000aULL,    0x8000000080008081ULL,  0x8000000000008080ULL,    0x0000000080000001ULL,  0x8000000080008008ULL  };# define a00 (p->u.s[0])# define a01 (p->u.s[1])# define a02 (p->u.s[2])# define a03 (p->u.s[3])# define a04 (p->u.s[4])# define a10 (p->u.s[5])# define a11 (p->u.s[6])# define a12 (p->u.s[7])# define a13 (p->u.s[8])# define a14 (p->u.s[9])# define a20 (p->u.s[10])# define a21 (p->u.s[11])# define a22 (p->u.s[12])# define a23 (p->u.s[13])# define a24 (p->u.s[14])# define a30 (p->u.s[15])# define a31 (p->u.s[16])# define a32 (p->u.s[17])# define a33 (p->u.s[18])# define a34 (p->u.s[19])# define a40 (p->u.s[20])# define a41 (p->u.s[21])# define a42 (p->u.s[22])# define a43 (p->u.s[23])# define a44 (p->u.s[24])# define ROL64(a,x) ((a<<x)|(a>>(64-x)))  for(i=0; i<24; i+=4){    c0 = a00^a10^a20^a30^a40;    c1 = a01^a11^a21^a31^a41;    c2 = a02^a12^a22^a32^a42;    c3 = a03^a13^a23^a33^a43;    c4 = a04^a14^a24^a34^a44;    d0 = c4^ROL64(c1, 1);    d1 = c0^ROL64(c2, 1);    d2 = c1^ROL64(c3, 1);    d3 = c2^ROL64(c4, 1);    d4 = c3^ROL64(c0, 1);    b0 = (a00^d0);    b1 = ROL64((a11^d1), 44);    b2 = ROL64((a22^d2), 43);    b3 = ROL64((a33^d3), 21);    b4 = ROL64((a44^d4), 14);    a00 =   b0 ^((~b1)&  b2 );    a00 ^= RC[i];    a11 =   b1 ^((~b2)&  b3 );    a22 =   b2 ^((~b3)&  b4 );    a33 =   b3 ^((~b4)&  b0 );    a44 =   b4 ^((~b0)&  b1 );    b2 = ROL64((a20^d0), 3);    b3 = ROL64((a31^d1), 45);    b4 = ROL64((a42^d2), 61);    b0 = ROL64((a03^d3), 28);    b1 = ROL64((a14^d4), 20);    a20 =   b0 ^((~b1)&  b2 );    a31 =   b1 ^((~b2)&  b3 );    a42 =   b2 ^((~b3)&  b4 );    a03 =   b3 ^((~b4)&  b0 );    a14 =   b4 ^((~b0)&  b1 );    b4 = ROL64((a40^d0), 18);    b0 = ROL64((a01^d1), 1);    b1 = ROL64((a12^d2), 6);    b2 = ROL64((a23^d3), 25);    b3 = ROL64((a34^d4), 8);    a40 =   b0 ^((~b1)&  b2 );    a01 =   b1 ^((~b2)&  b3 );    a12 =   b2 ^((~b3)&  b4 );    a23 =   b3 ^((~b4)&  b0 );    a34 =   b4 ^((~b0)&  b1 );    b1 = ROL64((a10^d0), 36);    b2 = ROL64((a21^d1), 10);    b3 = ROL64((a32^d2), 15);    b4 = ROL64((a43^d3), 56);    b0 = ROL64((a04^d4), 27);    a10 =   b0 ^((~b1)&  b2 );    a21 =   b1 ^((~b2)&  b3 );    a32 =   b2 ^((~b3)&  b4 );    a43 =   b3 ^((~b4)&  b0 );    a04 =   b4 ^((~b0)&  b1 );    b3 = ROL64((a30^d0), 41);    b4 = ROL64((a41^d1), 2);    b0 = ROL64((a02^d2), 62);    b1 = ROL64((a13^d3), 55);    b2 = ROL64((a24^d4), 39);    a30 =   b0 ^((~b1)&  b2 );    a41 =   b1 ^((~b2)&  b3 );    a02 =   b2 ^((~b3)&  b4 );    a13 =   b3 ^((~b4)&  b0 );    a24 =   b4 ^((~b0)&  b1 );    c0 = a00^a20^a40^a10^a30;    c1 = a11^a31^a01^a21^a41;    c2 = a22^a42^a12^a32^a02;    c3 = a33^a03^a23^a43^a13;    c4 = a44^a14^a34^a04^a24;    d0 = c4^ROL64(c1, 1);    d1 = c0^ROL64(c2, 1);    d2 = c1^ROL64(c3, 1);    d3 = c2^ROL64(c4, 1);    d4 = c3^ROL64(c0, 1);    b0 = (a00^d0);    b1 = ROL64((a31^d1), 44);    b2 = ROL64((a12^d2), 43);    b3 = ROL64((a43^d3), 21);    b4 = ROL64((a24^d4), 14);    a00 =   b0 ^((~b1)&  b2 );    a00 ^= RC[i+1];    a31 =   b1 ^((~b2)&  b3 );    a12 =   b2 ^((~b3)&  b4 );    a43 =   b3 ^((~b4)&  b0 );    a24 =   b4 ^((~b0)&  b1 );    b2 = ROL64((a40^d0), 3);    b3 = ROL64((a21^d1), 45);    b4 = ROL64((a02^d2), 61);    b0 = ROL64((a33^d3), 28);    b1 = ROL64((a14^d4), 20);    a40 =   b0 ^((~b1)&  b2 );    a21 =   b1 ^((~b2)&  b3 );    a02 =   b2 ^((~b3)&  b4 );    a33 =   b3 ^((~b4)&  b0 );    a14 =   b4 ^((~b0)&  b1 );    b4 = ROL64((a30^d0), 18);    b0 = ROL64((a11^d1), 1);    b1 = ROL64((a42^d2), 6);    b2 = ROL64((a23^d3), 25);    b3 = ROL64((a04^d4), 8);    a30 =   b0 ^((~b1)&  b2 );    a11 =   b1 ^((~b2)&  b3 );    a42 =   b2 ^((~b3)&  b4 );    a23 =   b3 ^((~b4)&  b0 );    a04 =   b4 ^((~b0)&  b1 );    b1 = ROL64((a20^d0), 36);    b2 = ROL64((a01^d1), 10);    b3 = ROL64((a32^d2), 15);    b4 = ROL64((a13^d3), 56);    b0 = ROL64((a44^d4), 27);    a20 =   b0 ^((~b1)&  b2 );    a01 =   b1 ^((~b2)&  b3 );    a32 =   b2 ^((~b3)&  b4 );    a13 =   b3 ^((~b4)&  b0 );    a44 =   b4 ^((~b0)&  b1 );    b3 = ROL64((a10^d0), 41);    b4 = ROL64((a41^d1), 2);    b0 = ROL64((a22^d2), 62);    b1 = ROL64((a03^d3), 55);    b2 = ROL64((a34^d4), 39);    a10 =   b0 ^((~b1)&  b2 );    a41 =   b1 ^((~b2)&  b3 );    a22 =   b2 ^((~b3)&  b4 );    a03 =   b3 ^((~b4)&  b0 );    a34 =   b4 ^((~b0)&  b1 );    c0 = a00^a40^a30^a20^a10;    c1 = a31^a21^a11^a01^a41;    c2 = a12^a02^a42^a32^a22;    c3 = a43^a33^a23^a13^a03;    c4 = a24^a14^a04^a44^a34;    d0 = c4^ROL64(c1, 1);    d1 = c0^ROL64(c2, 1);    d2 = c1^ROL64(c3, 1);    d3 = c2^ROL64(c4, 1);    d4 = c3^ROL64(c0, 1);    b0 = (a00^d0);    b1 = ROL64((a21^d1), 44);    b2 = ROL64((a42^d2), 43);    b3 = ROL64((a13^d3), 21);    b4 = ROL64((a34^d4), 14);    a00 =   b0 ^((~b1)&  b2 );    a00 ^= RC[i+2];    a21 =   b1 ^((~b2)&  b3 );    a42 =   b2 ^((~b3)&  b4 );    a13 =   b3 ^((~b4)&  b0 );    a34 =   b4 ^((~b0)&  b1 );    b2 = ROL64((a30^d0), 3);    b3 = ROL64((a01^d1), 45);    b4 = ROL64((a22^d2), 61);    b0 = ROL64((a43^d3), 28);    b1 = ROL64((a14^d4), 20);    a30 =   b0 ^((~b1)&  b2 );    a01 =   b1 ^((~b2)&  b3 );    a22 =   b2 ^((~b3)&  b4 );    a43 =   b3 ^((~b4)&  b0 );    a14 =   b4 ^((~b0)&  b1 );    b4 = ROL64((a10^d0), 18);    b0 = ROL64((a31^d1), 1);    b1 = ROL64((a02^d2), 6);    b2 = ROL64((a23^d3), 25);    b3 = ROL64((a44^d4), 8);    a10 =   b0 ^((~b1)&  b2 );    a31 =   b1 ^((~b2)&  b3 );    a02 =   b2 ^((~b3)&  b4 );    a23 =   b3 ^((~b4)&  b0 );    a44 =   b4 ^((~b0)&  b1 );    b1 = ROL64((a40^d0), 36);    b2 = ROL64((a11^d1), 10);    b3 = ROL64((a32^d2), 15);    b4 = ROL64((a03^d3), 56);    b0 = ROL64((a24^d4), 27);    a40 =   b0 ^((~b1)&  b2 );    a11 =   b1 ^((~b2)&  b3 );    a32 =   b2 ^((~b3)&  b4 );    a03 =   b3 ^((~b4)&  b0 );    a24 =   b4 ^((~b0)&  b1 );    b3 = ROL64((a20^d0), 41);    b4 = ROL64((a41^d1), 2);    b0 = ROL64((a12^d2), 62);    b1 = ROL64((a33^d3), 55);    b2 = ROL64((a04^d4), 39);    a20 =   b0 ^((~b1)&  b2 );    a41 =   b1 ^((~b2)&  b3 );    a12 =   b2 ^((~b3)&  b4 );    a33 =   b3 ^((~b4)&  b0 );    a04 =   b4 ^((~b0)&  b1 );    c0 = a00^a30^a10^a40^a20;    c1 = a21^a01^a31^a11^a41;    c2 = a42^a22^a02^a32^a12;    c3 = a13^a43^a23^a03^a33;    c4 = a34^a14^a44^a24^a04;    d0 = c4^ROL64(c1, 1);    d1 = c0^ROL64(c2, 1);    d2 = c1^ROL64(c3, 1);    d3 = c2^ROL64(c4, 1);    d4 = c3^ROL64(c0, 1);    b0 = (a00^d0);    b1 = ROL64((a01^d1), 44);    b2 = ROL64((a02^d2), 43);    b3 = ROL64((a03^d3), 21);    b4 = ROL64((a04^d4), 14);    a00 =   b0 ^((~b1)&  b2 );    a00 ^= RC[i+3];    a01 =   b1 ^((~b2)&  b3 );    a02 =   b2 ^((~b3)&  b4 );    a03 =   b3 ^((~b4)&  b0 );    a04 =   b4 ^((~b0)&  b1 );    b2 = ROL64((a10^d0), 3);    b3 = ROL64((a11^d1), 45);    b4 = ROL64((a12^d2), 61);    b0 = ROL64((a13^d3), 28);    b1 = ROL64((a14^d4), 20);    a10 =   b0 ^((~b1)&  b2 );    a11 =   b1 ^((~b2)&  b3 );    a12 =   b2 ^((~b3)&  b4 );    a13 =   b3 ^((~b4)&  b0 );    a14 =   b4 ^((~b0)&  b1 );    b4 = ROL64((a20^d0), 18);    b0 = ROL64((a21^d1), 1);    b1 = ROL64((a22^d2), 6);    b2 = ROL64((a23^d3), 25);    b3 = ROL64((a24^d4), 8);    a20 =   b0 ^((~b1)&  b2 );    a21 =   b1 ^((~b2)&  b3 );    a22 =   b2 ^((~b3)&  b4 );    a23 =   b3 ^((~b4)&  b0 );    a24 =   b4 ^((~b0)&  b1 );    b1 = ROL64((a30^d0), 36);    b2 = ROL64((a31^d1), 10);    b3 = ROL64((a32^d2), 15);    b4 = ROL64((a33^d3), 56);    b0 = ROL64((a34^d4), 27);    a30 =   b0 ^((~b1)&  b2 );    a31 =   b1 ^((~b2)&  b3 );    a32 =   b2 ^((~b3)&  b4 );    a33 =   b3 ^((~b4)&  b0 );    a34 =   b4 ^((~b0)&  b1 );    b3 = ROL64((a40^d0), 41);    b4 = ROL64((a41^d1), 2);    b0 = ROL64((a42^d2), 62);    b1 = ROL64((a43^d3), 55);    b2 = ROL64((a44^d4), 39);    a40 =   b0 ^((~b1)&  b2 );    a41 =   b1 ^((~b2)&  b3 );    a42 =   b2 ^((~b3)&  b4 );    a43 =   b3 ^((~b4)&  b0 );    a44 =   b4 ^((~b0)&  b1 );  }}static void SHA3Init(SHA3Context *p, int iSize){  memset(p, 0, sizeof(*p));  if( iSize>=128 && iSize<=512 ){    p->nRate = (1600 - ((iSize + 31)&~31)*2)/8;  }else{    p->nRate = (1600 - 2*256)/8;  }#if SHA3_BYTEORDER==1234  #elif SHA3_BYTEORDER==4321  p->ixMask = 7;  #else  {    static unsigned int one = 1;    if( 1==*(unsigned char*)&one ){            p->ixMask = 0;    }else{            p->ixMask = 7;    }  }#endif}static void SHA3Update(  SHA3Context *p,  const unsigned char *aData,  unsigned int nData){  unsigned int i = 0;  if( aData==0 ) return;#if SHA3_BYTEORDER==1234  if( (p->nLoaded % 8)==0 && ((aData - (const unsigned char*)0)&7)==0 ){    for(; i+7<nData; i+=8){      p->u.s[p->nLoaded/8] ^= *(u64*)&aData[i];      p->nLoaded += 8;      if( p->nLoaded>=p->nRate ){        KeccakF1600Step(p);        p->nLoaded = 0;      }    }  }#endif  for(; i<nData; i++){#if SHA3_BYTEORDER==1234    p->u.x[p->nLoaded] ^= aData[i];#elif SHA3_BYTEORDER==4321    p->u.x[p->nLoaded^0x07] ^= aData[i];#else    p->u.x[p->nLoaded^p->ixMask] ^= aData[i];#endif    p->nLoaded++;    if( p->nLoaded==p->nRate ){      KeccakF1600Step(p);      p->nLoaded = 0;    }  }}static unsigned char *SHA3Final(SHA3Context *p){  unsigned int i;  if( p->nLoaded==p->nRate-1 ){    const unsigned char c1 = 0x86;    SHA3Update(p, &c1, 1);  }else{    const unsigned char c2 = 0x06;    const unsigned char c3 = 0x80;    SHA3Update(p, &c2, 1);    p->nLoaded = p->nRate - 1;    SHA3Update(p, &c3, 1);  }  for(i=0; i<p->nRate; i++){    p->u.x[i+p->nRate] = p->u.x[i^p->ixMask];  }  return &p->u.x[p->nRate];}static void sha3Func(  sqlite3_context *context,  int argc,  sqlite3_value **argv){  SHA3Context cx;  int eType = sqlite3_value_type(argv[0]);  int nByte = sqlite3_value_bytes(argv[0]);  int iSize;  if( argc==1 ){    iSize = 256;  }else{    iSize = sqlite3_value_int(argv[1]);    if( iSize!=224 && iSize!=256 && iSize!=384 && iSize!=512 ){      sqlite3_result_error(context, "SHA3 size should be one of: 224 256 "                                    "384 512", -1);      return;    }  }  if( eType==SQLITE_NULL ) return;  SHA3Init(&cx, iSize);  if( eType==SQLITE_BLOB ){    SHA3Update(&cx, sqlite3_value_blob(argv[0]), nByte);  }else{    SHA3Update(&cx, sqlite3_value_text(argv[0]), nByte);  }  sqlite3_result_blob(context, SHA3Final(&cx), iSize/8, SQLITE_TRANSIENT);}static void hash_step_vformat(  SHA3Context *p,                   const char *zFormat,  ...){  va_list ap;  int n;  char zBuf[50];  va_start(ap, zFormat);  sqlite3_vsnprintf(sizeof(zBuf),zBuf,zFormat,ap);  va_end(ap);  n = (int)strlen(zBuf);  SHA3Update(p, (unsigned char*)zBuf, n);}static void sha3QueryFunc(  sqlite3_context *context,  int argc,  sqlite3_value **argv){  sqlite3 *db = sqlite3_context_db_handle(context);  const char *zSql = (const char*)sqlite3_value_text(argv[0]);  sqlite3_stmt *pStmt = 0;  int nCol;                     int i;                        int rc;  int n;  const char *z;  SHA3Context cx;  int iSize;  if( argc==1 ){    iSize = 256;  }else{    iSize = sqlite3_value_int(argv[1]);    if( iSize!=224 && iSize!=256 && iSize!=384 && iSize!=512 ){      sqlite3_result_error(context, "SHA3 size should be one of: 224 256 "                                    "384 512", -1);      return;    }  }  if( zSql==0 ) return;  SHA3Init(&cx, iSize);  while( zSql[0] ){    rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zSql);    if( rc ){      char *zMsg = sqlite3_mprintf("error SQL statement [%s]: %s",                                   zSql, sqlite3_errmsg(db));      sqlite3_finalize(pStmt);      sqlite3_result_error(context, zMsg, -1);      sqlite3_free(zMsg);      return;    }    if( !sqlite3_stmt_readonly(pStmt) ){      char *zMsg = sqlite3_mprintf("non-query: [%s]", sqlite3_sql(pStmt));      sqlite3_finalize(pStmt);      sqlite3_result_error(context, zMsg, -1);      sqlite3_free(zMsg);      return;    }    nCol = sqlite3_column_count(pStmt);    z = sqlite3_sql(pStmt);    if( z ){      n = (int)strlen(z);      hash_step_vformat(&cx,"S%d:",n);      SHA3Update(&cx,(unsigned char*)z,n);    }        while( SQLITE_ROW==sqlite3_step(pStmt) ){      SHA3Update(&cx,(const unsigned char*)"R",1);      for(i=0; i<nCol; i++){        switch( sqlite3_column_type(pStmt,i) ){          case SQLITE_NULL: {            SHA3Update(&cx, (const unsigned char*)"N",1);            break;          }          case SQLITE_INTEGER: {            sqlite3_uint64 u;            int j;            unsigned char x[9];            sqlite3_int64 v = sqlite3_column_int64(pStmt,i);            memcpy(&u, &v, 8);            for(j=8; j>=1; j--){              x[j] = u & 0xff;              u >>= 8;            }            x[0] = 'I';            SHA3Update(&cx, x, 9);            break;          }          case SQLITE_FLOAT: {            sqlite3_uint64 u;            int j;            unsigned char x[9];            double r = sqlite3_column_double(pStmt,i);            memcpy(&u, &r, 8);            for(j=8; j>=1; j--){              x[j] = u & 0xff;              u >>= 8;            }            x[0] = 'F';            SHA3Update(&cx,x,9);            break;          }          case SQLITE_TEXT: {            int n2 = sqlite3_column_bytes(pStmt, i);            const unsigned char *z2 = sqlite3_column_text(pStmt, i);            hash_step_vformat(&cx,"T%d:",n2);            SHA3Update(&cx, z2, n2);            break;          }          case SQLITE_BLOB: {            int n2 = sqlite3_column_bytes(pStmt, i);            const unsigned char *z2 = sqlite3_column_blob(pStmt, i);            hash_step_vformat(&cx,"B%d:",n2);            SHA3Update(&cx, z2, n2);            break;          }        }      }    }    sqlite3_finalize(pStmt);  }  sqlite3_result_blob(context, SHA3Final(&cx), iSize/8, SQLITE_TRANSIENT);}#ifdef _WIN32#endifint sqlite3_shathree_init(  sqlite3 *db,  char **pzErrMsg,  const sqlite3_api_routines *pApi){  int rc = SQLITE_OK;  SQLITE_EXTENSION_INIT2(pApi);  (void)pzErrMsg;    rc = sqlite3_create_function(db, "sha3", 1,                      SQLITE_UTF8 | SQLITE_INNOCUOUS | SQLITE_DETERMINISTIC,                      0, sha3Func, 0, 0);  if( rc==SQLITE_OK ){    rc = sqlite3_create_function(db, "sha3", 2,                      SQLITE_UTF8 | SQLITE_INNOCUOUS | SQLITE_DETERMINISTIC,                      0, sha3Func, 0, 0);  }  if( rc==SQLITE_OK ){    rc = sqlite3_create_function(db, "sha3_query", 1,                      SQLITE_UTF8 | SQLITE_DIRECTONLY,                      0, sha3QueryFunc, 0, 0);  }  if( rc==SQLITE_OK ){    rc = sqlite3_create_function(db, "sha3_query", 2,                      SQLITE_UTF8 | SQLITE_DIRECTONLY,                      0, sha3QueryFunc, 0, 0);  }  return rc;}SQLITE_EXTENSION_INIT1#include <stdio.h>#include <string.h>#include <assert.h>#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#if !defined(_WIN32) && !defined(WIN32)#  include <unistd.h>#  include <dirent.h>#  include <utime.h>#  include <sys/time.h>#else#  include "windows.h"#  include <io.h>#  include <direct.h>#  define dirent DIRENT#  ifndef chmod#    define chmod _chmod#  endif#  ifndef stat#    define stat _stat#  endif#  define mkdir(path,mode) _mkdir(path)#  define lstat(path,buf) stat(path,buf)#endif#include <time.h>#include <errno.h>                 #define FSDIR_SCHEMA "(name,mode,mtime,data,path HIDDEN,dir HIDDEN)"#define FSDIR_COLUMN_NAME     0     #define FSDIR_COLUMN_MODE     1     #define FSDIR_COLUMN_MTIME    2     #define FSDIR_COLUMN_DATA     3     #define FSDIR_COLUMN_PATH     4     #define FSDIR_COLUMN_DIR      5     static void readFileContents(sqlite3_context *ctx, const char *zName){  FILE *in;  sqlite3_int64 nIn;  void *pBuf;  sqlite3 *db;  int mxBlob;  in = fopen(zName, "rb");  if( in==0 ){        return;  }  fseek(in, 0, SEEK_END);  nIn = ftell(in);  rewind(in);  db = sqlite3_context_db_handle(ctx);  mxBlob = sqlite3_limit(db, SQLITE_LIMIT_LENGTH, -1);  if( nIn>mxBlob ){    sqlite3_result_error_code(ctx, SQLITE_TOOBIG);    fclose(in);    return;  }  pBuf = sqlite3_malloc64( nIn ? nIn : 1 );  if( pBuf==0 ){    sqlite3_result_error_nomem(ctx);    fclose(in);    return;  }  if( nIn==(sqlite3_int64)fread(pBuf, 1, (size_t)nIn, in) ){    sqlite3_result_blob64(ctx, pBuf, nIn, sqlite3_free);  }else{    sqlite3_result_error_code(ctx, SQLITE_IOERR);    sqlite3_free(pBuf);  }  fclose(in);}static void readfileFunc(  sqlite3_context *context,  int argc,  sqlite3_value **argv){  const char *zName;  (void)(argc);    zName = (const char*)sqlite3_value_text(argv[0]);  if( zName==0 ) return;  readFileContents(context, zName);}static void ctxErrorMsg(sqlite3_context *ctx, const char *zFmt, ...){  char *zMsg = 0;  va_list ap;  va_start(ap, zFmt);  zMsg = sqlite3_vmprintf(zFmt, ap);  sqlite3_result_error(ctx, zMsg, -1);  sqlite3_free(zMsg);  va_end(ap);}#if defined(_WIN32)static sqlite3_uint64 fileTimeToUnixTime(  LPFILETIME pFileTime){  SYSTEMTIME epochSystemTime;  ULARGE_INTEGER epochIntervals;  FILETIME epochFileTime;  ULARGE_INTEGER fileIntervals;  memset(&epochSystemTime, 0, sizeof(SYSTEMTIME));  epochSystemTime.wYear = 1970;  epochSystemTime.wMonth = 1;  epochSystemTime.wDay = 1;  SystemTimeToFileTime(&epochSystemTime, &epochFileTime);  epochIntervals.LowPart = epochFileTime.dwLowDateTime;  epochIntervals.HighPart = epochFileTime.dwHighDateTime;  fileIntervals.LowPart = pFileTime->dwLowDateTime;  fileIntervals.HighPart = pFileTime->dwHighDateTime;  return (fileIntervals.QuadPart - epochIntervals.QuadPart) / 10000000;}#if defined(FILEIO_WIN32_DLL) && (defined(_WIN32) || defined(WIN32))#  #  undef sqlite3_win32_utf8_to_unicode#  define sqlite3_win32_utf8_to_unicode utf8_to_utf16#LPWSTR utf8_to_utf16(const char *z){  int nAllot = MultiByteToWideChar(CP_UTF8, 0, z, -1, NULL, 0);  LPWSTR rv = sqlite3_malloc(nAllot * sizeof(WCHAR));  if( rv!=0 && 0 < MultiByteToWideChar(CP_UTF8, 0, z, -1, rv, nAllot) )    return rv;  sqlite3_free(rv);  return 0;}#endifstatic void statTimesToUtc(  const char *zPath,  struct stat *pStatBuf){  HANDLE hFindFile;  WIN32_FIND_DATAW fd;  LPWSTR zUnicodeName;  extern LPWSTR sqlite3_win32_utf8_to_unicode(const char*);  zUnicodeName = sqlite3_win32_utf8_to_unicode(zPath);  if( zUnicodeName ){    memset(&fd, 0, sizeof(WIN32_FIND_DATAW));    hFindFile = FindFirstFileW(zUnicodeName, &fd);    if( hFindFile!=NULL ){      pStatBuf->st_ctime = (time_t)fileTimeToUnixTime(&fd.ftCreationTime);      pStatBuf->st_atime = (time_t)fileTimeToUnixTime(&fd.ftLastAccessTime);      pStatBuf->st_mtime = (time_t)fileTimeToUnixTime(&fd.ftLastWriteTime);      FindClose(hFindFile);    }    sqlite3_free(zUnicodeName);  }}#endifstatic int fileStat(  const char *zPath,  struct stat *pStatBuf){#if defined(_WIN32)  int rc = stat(zPath, pStatBuf);  if( rc==0 ) statTimesToUtc(zPath, pStatBuf);  return rc;#else  return stat(zPath, pStatBuf);#endif}static int fileLinkStat(  const char *zPath,  struct stat *pStatBuf){#if defined(_WIN32)  int rc = lstat(zPath, pStatBuf);  if( rc==0 ) statTimesToUtc(zPath, pStatBuf);  return rc;#else  return lstat(zPath, pStatBuf);#endif}static int makeDirectory(  const char *zFile){  char *zCopy = sqlite3_mprintf("%s", zFile);  int rc = SQLITE_OK;  if( zCopy==0 ){    rc = SQLITE_NOMEM;  }else{    int nCopy = (int)strlen(zCopy);    int i = 1;    while( rc==SQLITE_OK ){      struct stat sStat;      int rc2;      for(; zCopy[i]!='/' && i<nCopy; i++);      if( i==nCopy ) break;      zCopy[i] = '\0';      rc2 = fileStat(zCopy, &sStat);      if( rc2!=0 ){        if( mkdir(zCopy, 0777) ) rc = SQLITE_ERROR;      }else{        if( !S_ISDIR(sStat.st_mode) ) rc = SQLITE_ERROR;      }      zCopy[i] = '/';      i++;    }    sqlite3_free(zCopy);  }  return rc;}static int writeFile(  sqlite3_context *pCtx,            const char *zFile,                sqlite3_value *pData,             mode_t mode,                      sqlite3_int64 mtime             ){  if( zFile==0 ) return 1;#if !defined(_WIN32) && !defined(WIN32)  if( S_ISLNK(mode) ){    const char *zTo = (const char*)sqlite3_value_text(pData);    if( zTo==0 || symlink(zTo, zFile)<0 ) return 1;  }else#endif  {    if( S_ISDIR(mode) ){      if( mkdir(zFile, mode) ){                struct stat sStat;        if( errno!=EEXIST         || 0!=fileStat(zFile, &sStat)         || !S_ISDIR(sStat.st_mode)         || ((sStat.st_mode&0777)!=(mode&0777) && 0!=chmod(zFile, mode&0777))        ){          return 1;        }      }    }else{      sqlite3_int64 nWrite = 0;      const char *z;      int rc = 0;      FILE *out = fopen(zFile, "wb");      if( out==0 ) return 1;      z = (const char*)sqlite3_value_blob(pData);      if( z ){        sqlite3_int64 n = fwrite(z, 1, sqlite3_value_bytes(pData), out);        nWrite = sqlite3_value_bytes(pData);        if( nWrite!=n ){          rc = 1;        }      }      fclose(out);      if( rc==0 && mode && chmod(zFile, mode & 0777) ){        rc = 1;      }      if( rc ) return 2;      sqlite3_result_int64(pCtx, nWrite);    }  }  if( mtime>=0 ){#if defined(_WIN32)#if !SQLITE_OS_WINRT        FILETIME lastAccess;    FILETIME lastWrite;    SYSTEMTIME currentTime;    LONGLONG intervals;    HANDLE hFile;    LPWSTR zUnicodeName;    extern LPWSTR sqlite3_win32_utf8_to_unicode(const char*);    GetSystemTime(¤tTime);    SystemTimeToFileTime(¤tTime, &lastAccess);    intervals = Int32x32To64(mtime, 10000000) + 116444736000000000;    lastWrite.dwLowDateTime = (DWORD)intervals;    lastWrite.dwHighDateTime = intervals >> 32;    zUnicodeName = sqlite3_win32_utf8_to_unicode(zFile);    if( zUnicodeName==0 ){      return 1;    }    hFile = CreateFileW(      zUnicodeName, FILE_WRITE_ATTRIBUTES, 0, NULL, OPEN_EXISTING,      FILE_FLAG_BACKUP_SEMANTICS, NULL    );    sqlite3_free(zUnicodeName);    if( hFile!=INVALID_HANDLE_VALUE ){      BOOL bResult = SetFileTime(hFile, NULL, &lastAccess, &lastWrite);      CloseHandle(hFile);      return !bResult;    }else{      return 1;    }#endif#elif defined(AT_FDCWD) && 0         struct timespec times[2];    times[0].tv_nsec = times[1].tv_nsec = 0;    times[0].tv_sec = time(0);    times[1].tv_sec = mtime;    if( utimensat(AT_FDCWD, zFile, times, AT_SYMLINK_NOFOLLOW) ){      return 1;    }#else        struct timeval times[2];    times[0].tv_usec = times[1].tv_usec = 0;    times[0].tv_sec = time(0);    times[1].tv_sec = mtime;    if( utimes(zFile, times) ){      return 1;    }#endif  }  return 0;}static void writefileFunc(  sqlite3_context *context,  int argc,  sqlite3_value **argv){  const char *zFile;  mode_t mode = 0;  int res;  sqlite3_int64 mtime = -1;  if( argc<2 || argc>4 ){    sqlite3_result_error(context,         "wrong number of arguments to function writefile()", -1    );    return;  }  zFile = (const char*)sqlite3_value_text(argv[0]);  if( zFile==0 ) return;  if( argc>=3 ){    mode = (mode_t)sqlite3_value_int(argv[2]);  }  if( argc==4 ){    mtime = sqlite3_value_int64(argv[3]);  }  res = writeFile(context, zFile, argv[1], mode, mtime);  if( res==1 && errno==ENOENT ){    if( makeDirectory(zFile)==SQLITE_OK ){      res = writeFile(context, zFile, argv[1], mode, mtime);    }  }  if( argc>2 && res!=0 ){    if( S_ISLNK(mode) ){      ctxErrorMsg(context, "failed to create symlink: %s", zFile);    }else if( S_ISDIR(mode) ){      ctxErrorMsg(context, "failed to create directory: %s", zFile);    }else{      ctxErrorMsg(context, "failed to write file: %s", zFile);    }  }}static void lsModeFunc(  sqlite3_context *context,  int argc,  sqlite3_value **argv){  int i;  int iMode = sqlite3_value_int(argv[0]);  char z[16];  (void)argc;  if( S_ISLNK(iMode) ){    z[0] = 'l';  }else if( S_ISREG(iMode) ){    z[0] = '-';  }else if( S_ISDIR(iMode) ){    z[0] = 'd';  }else{    z[0] = '?';  }  for(i=0; i<3; i++){    int m = (iMode >> ((2-i)*3));    char *a = &z[1 + i*3];    a[0] = (m & 0x4) ? 'r' : '-';    a[1] = (m & 0x2) ? 'w' : '-';    a[2] = (m & 0x1) ? 'x' : '-';  }  z[10] = '\0';  sqlite3_result_text(context, z, -1, SQLITE_TRANSIENT);}#ifndef SQLITE_OMIT_VIRTUALTABLEtypedef struct fsdir_cursor fsdir_cursor;typedef struct FsdirLevel FsdirLevel;struct FsdirLevel {  DIR *pDir;                   char *zDir;                };struct fsdir_cursor {  sqlite3_vtab_cursor base;    int nLvl;                    int iLvl;                    FsdirLevel *aLvl;            const char *zBase;  int nBase;  struct stat sStat;           char *zPath;                 sqlite3_int64 iRowid;      };typedef struct fsdir_tab fsdir_tab;struct fsdir_tab {  sqlite3_vtab base;         };static int fsdirConnect(  sqlite3 *db,  void *pAux,  int argc, const char *const*argv,  sqlite3_vtab **ppVtab,  char **pzErr){  fsdir_tab *pNew = 0;  int rc;  (void)pAux;  (void)argc;  (void)argv;  (void)pzErr;  rc = sqlite3_declare_vtab(db, "CREATE TABLE x" FSDIR_SCHEMA);  if( rc==SQLITE_OK ){    pNew = (fsdir_tab*)sqlite3_malloc( sizeof(*pNew) );    if( pNew==0 ) return SQLITE_NOMEM;    memset(pNew, 0, sizeof(*pNew));    sqlite3_vtab_config(db, SQLITE_VTAB_DIRECTONLY);  }  *ppVtab = (sqlite3_vtab*)pNew;  return rc;}static int fsdirDisconnect(sqlite3_vtab *pVtab){  sqlite3_free(pVtab);  return SQLITE_OK;}static int fsdirOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){  fsdir_cursor *pCur;  (void)p;  pCur = sqlite3_malloc( sizeof(*pCur) );  if( pCur==0 ) return SQLITE_NOMEM;  memset(pCur, 0, sizeof(*pCur));  pCur->iLvl = -1;  *ppCursor = &pCur->base;  return SQLITE_OK;}static void fsdirResetCursor(fsdir_cursor *pCur){  int i;  for(i=0; i<=pCur->iLvl; i++){    FsdirLevel *pLvl = &pCur->aLvl[i];    if( pLvl->pDir ) closedir(pLvl->pDir);    sqlite3_free(pLvl->zDir);  }  sqlite3_free(pCur->zPath);  sqlite3_free(pCur->aLvl);  pCur->aLvl = 0;  pCur->zPath = 0;  pCur->zBase = 0;  pCur->nBase = 0;  pCur->nLvl = 0;  pCur->iLvl = -1;  pCur->iRowid = 1;}static int fsdirClose(sqlite3_vtab_cursor *cur){  fsdir_cursor *pCur = (fsdir_cursor*)cur;  fsdirResetCursor(pCur);  sqlite3_free(pCur);  return SQLITE_OK;}static void fsdirSetErrmsg(fsdir_cursor *pCur, const char *zFmt, ...){  va_list ap;  va_start(ap, zFmt);  pCur->base.pVtab->zErrMsg = sqlite3_vmprintf(zFmt, ap);  va_end(ap);}static int fsdirNext(sqlite3_vtab_cursor *cur){  fsdir_cursor *pCur = (fsdir_cursor*)cur;  mode_t m = pCur->sStat.st_mode;  pCur->iRowid++;  if( S_ISDIR(m) ){        int iNew = pCur->iLvl + 1;    FsdirLevel *pLvl;    if( iNew>=pCur->nLvl ){      int nNew = iNew+1;      sqlite3_int64 nByte = nNew*sizeof(FsdirLevel);      FsdirLevel *aNew = (FsdirLevel*)sqlite3_realloc64(pCur->aLvl, nByte);      if( aNew==0 ) return SQLITE_NOMEM;      memset(&aNew[pCur->nLvl], 0, sizeof(FsdirLevel)*(nNew-pCur->nLvl));      pCur->aLvl = aNew;      pCur->nLvl = nNew;    }    pCur->iLvl = iNew;    pLvl = &pCur->aLvl[iNew];        pLvl->zDir = pCur->zPath;    pCur->zPath = 0;    pLvl->pDir = opendir(pLvl->zDir);    if( pLvl->pDir==0 ){      fsdirSetErrmsg(pCur, "cannot read directory: %s", pCur->zPath);      return SQLITE_ERROR;    }  }  while( pCur->iLvl>=0 ){    FsdirLevel *pLvl = &pCur->aLvl[pCur->iLvl];    struct dirent *pEntry = readdir(pLvl->pDir);    if( pEntry ){      if( pEntry->d_name[0]=='.' ){       if( pEntry->d_name[1]=='.' && pEntry->d_name[2]=='\0' ) continue;       if( pEntry->d_name[1]=='\0' ) continue;      }      sqlite3_free(pCur->zPath);      pCur->zPath = sqlite3_mprintf("%s/%s", pLvl->zDir, pEntry->d_name);      if( pCur->zPath==0 ) return SQLITE_NOMEM;      if( fileLinkStat(pCur->zPath, &pCur->sStat) ){        fsdirSetErrmsg(pCur, "cannot stat file: %s", pCur->zPath);        return SQLITE_ERROR;      }      return SQLITE_OK;    }    closedir(pLvl->pDir);    sqlite3_free(pLvl->zDir);    pLvl->pDir = 0;    pLvl->zDir = 0;    pCur->iLvl--;  }    sqlite3_free(pCur->zPath);  pCur->zPath = 0;  return SQLITE_OK;}static int fsdirColumn(  sqlite3_vtab_cursor *cur,     sqlite3_context *ctx,         int i                       ){  fsdir_cursor *pCur = (fsdir_cursor*)cur;  switch( i ){    case FSDIR_COLUMN_NAME: {      sqlite3_result_text(ctx, &pCur->zPath[pCur->nBase], -1, SQLITE_TRANSIENT);      break;    }    case FSDIR_COLUMN_MODE:      sqlite3_result_int64(ctx, pCur->sStat.st_mode);      break;    case FSDIR_COLUMN_MTIME:      sqlite3_result_int64(ctx, pCur->sStat.st_mtime);      break;    case FSDIR_COLUMN_DATA: {      mode_t m = pCur->sStat.st_mode;      if( S_ISDIR(m) ){        sqlite3_result_null(ctx);#if !defined(_WIN32) && !defined(WIN32)      }else if( S_ISLNK(m) ){        char aStatic[64];        char *aBuf = aStatic;        sqlite3_int64 nBuf = 64;        int n;        while( 1 ){          n = readlink(pCur->zPath, aBuf, nBuf);          if( n<nBuf ) break;          if( aBuf!=aStatic ) sqlite3_free(aBuf);          nBuf = nBuf*2;          aBuf = sqlite3_malloc64(nBuf);          if( aBuf==0 ){            sqlite3_result_error_nomem(ctx);            return SQLITE_NOMEM;          }        }        sqlite3_result_text(ctx, aBuf, n, SQLITE_TRANSIENT);        if( aBuf!=aStatic ) sqlite3_free(aBuf);#endif      }else{        readFileContents(ctx, pCur->zPath);      }    }    case FSDIR_COLUMN_PATH:    default: {            break;    }  }  return SQLITE_OK;}static int fsdirRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){  fsdir_cursor *pCur = (fsdir_cursor*)cur;  *pRowid = pCur->iRowid;  return SQLITE_OK;}static int fsdirEof(sqlite3_vtab_cursor *cur){  fsdir_cursor *pCur = (fsdir_cursor*)cur;  return (pCur->zPath==0);}static int fsdirFilter(  sqlite3_vtab_cursor *cur,   int idxNum, const char *idxStr,  int argc, sqlite3_value **argv){  const char *zDir = 0;  fsdir_cursor *pCur = (fsdir_cursor*)cur;  (void)idxStr;  fsdirResetCursor(pCur);  if( idxNum==0 ){    fsdirSetErrmsg(pCur, "table function fsdir requires an argument");    return SQLITE_ERROR;  }  assert( argc==idxNum && (argc==1 || argc==2) );  zDir = (const char*)sqlite3_value_text(argv[0]);  if( zDir==0 ){    fsdirSetErrmsg(pCur, "table function fsdir requires a non-NULL argument");    return SQLITE_ERROR;  }  if( argc==2 ){    pCur->zBase = (const char*)sqlite3_value_text(argv[1]);  }  if( pCur->zBase ){    pCur->nBase = (int)strlen(pCur->zBase)+1;    pCur->zPath = sqlite3_mprintf("%s/%s", pCur->zBase, zDir);  }else{    pCur->zPath = sqlite3_mprintf("%s", zDir);  }  if( pCur->zPath==0 ){    return SQLITE_NOMEM;  }  if( fileLinkStat(pCur->zPath, &pCur->sStat) ){    fsdirSetErrmsg(pCur, "cannot stat file: %s", pCur->zPath);    return SQLITE_ERROR;  }  return SQLITE_OK;}static int fsdirBestIndex(  sqlite3_vtab *tab,  sqlite3_index_info *pIdxInfo){  int i;                   int idxPath = -1;        int idxDir = -1;         int seenPath = 0;        int seenDir = 0;         const struct sqlite3_index_constraint *pConstraint;  (void)tab;  pConstraint = pIdxInfo->aConstraint;  for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){    if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;    switch( pConstraint->iColumn ){      case FSDIR_COLUMN_PATH: {        if( pConstraint->usable ){          idxPath = i;          seenPath = 0;        }else if( idxPath<0 ){          seenPath = 1;        }        break;      }      case FSDIR_COLUMN_DIR: {        if( pConstraint->usable ){          idxDir = i;          seenDir = 0;        }else if( idxDir<0 ){          seenDir = 1;        }        break;      }    }   }  if( seenPath || seenDir ){        return SQLITE_CONSTRAINT;  }  if( idxPath<0 ){    pIdxInfo->idxNum = 0;        pIdxInfo->estimatedRows = 0x7fffffff;  }else{    pIdxInfo->aConstraintUsage[idxPath].omit = 1;    pIdxInfo->aConstraintUsage[idxPath].argvIndex = 1;    if( idxDir>=0 ){      pIdxInfo->aConstraintUsage[idxDir].omit = 1;      pIdxInfo->aConstraintUsage[idxDir].argvIndex = 2;      pIdxInfo->idxNum = 2;      pIdxInfo->estimatedCost = 10.0;    }else{      pIdxInfo->idxNum = 1;      pIdxInfo->estimatedCost = 100.0;    }  }  return SQLITE_OK;}static int fsdirRegister(sqlite3 *db){  static sqlite3_module fsdirModule = {    0,                             0,                             fsdirConnect,                  fsdirBestIndex,                fsdirDisconnect,               0,                             fsdirOpen,                     fsdirClose,                    fsdirFilter,                   fsdirNext,                     fsdirEof,                      fsdirColumn,                   fsdirRowid,                    0,                             0,                             0,                             0,                             0,                             0,                             0,                             0,                             0,                             0,                             0,                           };  int rc = sqlite3_create_module(db, "fsdir", &fsdirModule, 0);  return rc;}#else         # define fsdirRegister(x) SQLITE_OK#endif#ifdef _WIN32#endifint sqlite3_fileio_init(  sqlite3 *db,   char **pzErrMsg,   const sqlite3_api_routines *pApi){  int rc = SQLITE_OK;  SQLITE_EXTENSION_INIT2(pApi);  (void)pzErrMsg;    rc = sqlite3_create_function(db, "readfile", 1,                                SQLITE_UTF8|SQLITE_DIRECTONLY, 0,                               readfileFunc, 0, 0);  if( rc==SQLITE_OK ){    rc = sqlite3_create_function(db, "writefile", -1,                                 SQLITE_UTF8|SQLITE_DIRECTONLY, 0,                                 writefileFunc, 0, 0);  }  if( rc==SQLITE_OK ){    rc = sqlite3_create_function(db, "lsmode", 1, SQLITE_UTF8, 0,                                 lsModeFunc, 0, 0);  }  if( rc==SQLITE_OK ){    rc = fsdirRegister(db);  }  return rc;}#if defined(FILEIO_WIN32_DLL) && (defined(_WIN32) || defined(WIN32))#  include "test_windirent.c"#endifSQLITE_EXTENSION_INIT1#include <assert.h>#include <string.h>#include <ctype.h>#ifndef SQLITE_OMIT_VIRTUALTABLEtypedef struct completion_vtab completion_vtab;struct completion_vtab {  sqlite3_vtab base;    sqlite3 *db;        };typedef struct completion_cursor completion_cursor;struct completion_cursor {  sqlite3_vtab_cursor base;    sqlite3 *db;                 int nPrefix, nLine;          char *zPrefix;               char *zLine;                 const char *zCurrentRow;     int szRow;                   sqlite3_stmt *pStmt;         sqlite3_int64 iRowid;        int ePhase;                  int j;                     };#define COMPLETION_FIRST_PHASE   1#define COMPLETION_KEYWORDS      1#define COMPLETION_PRAGMAS       2#define COMPLETION_FUNCTIONS     3#define COMPLETION_COLLATIONS    4#define COMPLETION_INDEXES       5#define COMPLETION_TRIGGERS      6#define COMPLETION_DATABASES     7#define COMPLETION_TABLES        8    #define COMPLETION_COLUMNS       9#define COMPLETION_MODULES       10#define COMPLETION_EOF           11static int completionConnect(  sqlite3 *db,  void *pAux,  int argc, const char *const*argv,  sqlite3_vtab **ppVtab,  char **pzErr){  completion_vtab *pNew;  int rc;  (void)(pAux);      (void)(argc);      (void)(argv);      (void)(pzErr);   #define COMPLETION_COLUMN_CANDIDATE 0  #define COMPLETION_COLUMN_PREFIX    1  #define COMPLETION_COLUMN_WHOLELINE 2  #define COMPLETION_COLUMN_PHASE     3    sqlite3_vtab_config(db, SQLITE_VTAB_INNOCUOUS);  rc = sqlite3_declare_vtab(db,      "CREATE TABLE x("      "  candidate TEXT,"      "  prefix TEXT HIDDEN,"      "  wholeline TEXT HIDDEN,"      "  phase INT HIDDEN"              ")");  if( rc==SQLITE_OK ){    pNew = sqlite3_malloc( sizeof(*pNew) );    *ppVtab = (sqlite3_vtab*)pNew;    if( pNew==0 ) return SQLITE_NOMEM;    memset(pNew, 0, sizeof(*pNew));    pNew->db = db;  }  return rc;}static int completionDisconnect(sqlite3_vtab *pVtab){  sqlite3_free(pVtab);  return SQLITE_OK;}static int completionOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){  completion_cursor *pCur;  pCur = sqlite3_malloc( sizeof(*pCur) );  if( pCur==0 ) return SQLITE_NOMEM;  memset(pCur, 0, sizeof(*pCur));  pCur->db = ((completion_vtab*)p)->db;  *ppCursor = &pCur->base;  return SQLITE_OK;}static void completionCursorReset(completion_cursor *pCur){  sqlite3_free(pCur->zPrefix);   pCur->zPrefix = 0;  pCur->nPrefix = 0;  sqlite3_free(pCur->zLine);     pCur->zLine = 0;    pCur->nLine = 0;  sqlite3_finalize(pCur->pStmt); pCur->pStmt = 0;  pCur->j = 0;}static int completionClose(sqlite3_vtab_cursor *cur){  completionCursorReset((completion_cursor*)cur);  sqlite3_free(cur);  return SQLITE_OK;}static int completionNext(sqlite3_vtab_cursor *cur){  completion_cursor *pCur = (completion_cursor*)cur;  int eNextPhase = 0;    int iCol = -1;         pCur->iRowid++;  while( pCur->ePhase!=COMPLETION_EOF ){    switch( pCur->ePhase ){      case COMPLETION_KEYWORDS: {        if( pCur->j >= sqlite3_keyword_count() ){          pCur->zCurrentRow = 0;          pCur->ePhase = COMPLETION_DATABASES;        }else{          sqlite3_keyword_name(pCur->j++, &pCur->zCurrentRow, &pCur->szRow);        }        iCol = -1;        break;      }      case COMPLETION_DATABASES: {        if( pCur->pStmt==0 ){          sqlite3_prepare_v2(pCur->db, "PRAGMA database_list", -1,                             &pCur->pStmt, 0);        }        iCol = 1;        eNextPhase = COMPLETION_TABLES;        break;      }      case COMPLETION_TABLES: {        if( pCur->pStmt==0 ){          sqlite3_stmt *pS2;          char *zSql = 0;          const char *zSep = "";          sqlite3_prepare_v2(pCur->db, "PRAGMA database_list", -1, &pS2, 0);          while( sqlite3_step(pS2)==SQLITE_ROW ){            const char *zDb = (const char*)sqlite3_column_text(pS2, 1);            zSql = sqlite3_mprintf(               "%z%s"               "SELECT name FROM \"%w\".sqlite_schema",               zSql, zSep, zDb            );            if( zSql==0 ) return SQLITE_NOMEM;            zSep = " UNION ";          }          sqlite3_finalize(pS2);          sqlite3_prepare_v2(pCur->db, zSql, -1, &pCur->pStmt, 0);          sqlite3_free(zSql);        }        iCol = 0;        eNextPhase = COMPLETION_COLUMNS;        break;      }      case COMPLETION_COLUMNS: {        if( pCur->pStmt==0 ){          sqlite3_stmt *pS2;          char *zSql = 0;          const char *zSep = "";          sqlite3_prepare_v2(pCur->db, "PRAGMA database_list", -1, &pS2, 0);          while( sqlite3_step(pS2)==SQLITE_ROW ){            const char *zDb = (const char*)sqlite3_column_text(pS2, 1);            zSql = sqlite3_mprintf(               "%z%s"               "SELECT pti.name FROM \"%w\".sqlite_schema AS sm"                       " JOIN pragma_table_info(sm.name,%Q) AS pti"               " WHERE sm.type='table'",               zSql, zSep, zDb, zDb            );            if( zSql==0 ) return SQLITE_NOMEM;            zSep = " UNION ";          }          sqlite3_finalize(pS2);          sqlite3_prepare_v2(pCur->db, zSql, -1, &pCur->pStmt, 0);          sqlite3_free(zSql);        }        iCol = 0;        eNextPhase = COMPLETION_EOF;        break;      }    }    if( iCol<0 ){            if( pCur->zCurrentRow==0 ) continue;    }else{      if( sqlite3_step(pCur->pStmt)==SQLITE_ROW ){                pCur->zCurrentRow = (const char*)sqlite3_column_text(pCur->pStmt, iCol);        pCur->szRow = sqlite3_column_bytes(pCur->pStmt, iCol);      }else{                sqlite3_finalize(pCur->pStmt);        pCur->pStmt = 0;        pCur->ePhase = eNextPhase;        continue;      }    }    if( pCur->nPrefix==0 ) break;    if( pCur->nPrefix<=pCur->szRow     && sqlite3_strnicmp(pCur->zPrefix, pCur->zCurrentRow, pCur->nPrefix)==0    ){      break;    }  }  return SQLITE_OK;}static int completionColumn(  sqlite3_vtab_cursor *cur,     sqlite3_context *ctx,         int i                       ){  completion_cursor *pCur = (completion_cursor*)cur;  switch( i ){    case COMPLETION_COLUMN_CANDIDATE: {      sqlite3_result_text(ctx, pCur->zCurrentRow, pCur->szRow,SQLITE_TRANSIENT);      break;    }    case COMPLETION_COLUMN_PREFIX: {      sqlite3_result_text(ctx, pCur->zPrefix, -1, SQLITE_TRANSIENT);      break;    }    case COMPLETION_COLUMN_WHOLELINE: {      sqlite3_result_text(ctx, pCur->zLine, -1, SQLITE_TRANSIENT);      break;    }    case COMPLETION_COLUMN_PHASE: {      sqlite3_result_int(ctx, pCur->ePhase);      break;    }  }  return SQLITE_OK;}static int completionRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){  completion_cursor *pCur = (completion_cursor*)cur;  *pRowid = pCur->iRowid;  return SQLITE_OK;}static int completionEof(sqlite3_vtab_cursor *cur){  completion_cursor *pCur = (completion_cursor*)cur;  return pCur->ePhase >= COMPLETION_EOF;}static int completionFilter(  sqlite3_vtab_cursor *pVtabCursor,   int idxNum, const char *idxStr,  int argc, sqlite3_value **argv){  completion_cursor *pCur = (completion_cursor *)pVtabCursor;  int iArg = 0;  (void)(idxStr);     (void)(argc);       completionCursorReset(pCur);  if( idxNum & 1 ){    pCur->nPrefix = sqlite3_value_bytes(argv[iArg]);    if( pCur->nPrefix>0 ){      pCur->zPrefix = sqlite3_mprintf("%s", sqlite3_value_text(argv[iArg]));      if( pCur->zPrefix==0 ) return SQLITE_NOMEM;    }    iArg = 1;  }  if( idxNum & 2 ){    pCur->nLine = sqlite3_value_bytes(argv[iArg]);    if( pCur->nLine>0 ){      pCur->zLine = sqlite3_mprintf("%s", sqlite3_value_text(argv[iArg]));      if( pCur->zLine==0 ) return SQLITE_NOMEM;    }  }  if( pCur->zLine!=0 && pCur->zPrefix==0 ){    int i = pCur->nLine;    while( i>0 && (isalnum(pCur->zLine[i-1]) || pCur->zLine[i-1]=='_') ){      i--;    }    pCur->nPrefix = pCur->nLine - i;    if( pCur->nPrefix>0 ){      pCur->zPrefix = sqlite3_mprintf("%.*s", pCur->nPrefix, pCur->zLine + i);      if( pCur->zPrefix==0 ) return SQLITE_NOMEM;    }  }  pCur->iRowid = 0;  pCur->ePhase = COMPLETION_FIRST_PHASE;  return completionNext(pVtabCursor);}static int completionBestIndex(  sqlite3_vtab *tab,  sqlite3_index_info *pIdxInfo){  int i;                   int idxNum = 0;          int prefixIdx = -1;      int wholelineIdx = -1;   int nArg = 0;            const struct sqlite3_index_constraint *pConstraint;  (void)(tab);      pConstraint = pIdxInfo->aConstraint;  for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){    if( pConstraint->usable==0 ) continue;    if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;    switch( pConstraint->iColumn ){      case COMPLETION_COLUMN_PREFIX:        prefixIdx = i;        idxNum |= 1;        break;      case COMPLETION_COLUMN_WHOLELINE:        wholelineIdx = i;        idxNum |= 2;        break;    }  }  if( prefixIdx>=0 ){    pIdxInfo->aConstraintUsage[prefixIdx].argvIndex = ++nArg;    pIdxInfo->aConstraintUsage[prefixIdx].omit = 1;  }  if( wholelineIdx>=0 ){    pIdxInfo->aConstraintUsage[wholelineIdx].argvIndex = ++nArg;    pIdxInfo->aConstraintUsage[wholelineIdx].omit = 1;  }  pIdxInfo->idxNum = idxNum;  pIdxInfo->estimatedCost = (double)5000 - 1000*nArg;  pIdxInfo->estimatedRows = 500 - 100*nArg;  return SQLITE_OK;}static sqlite3_module completionModule = {  0,                           0,                           completionConnect,           completionBestIndex,         completionDisconnect,        0,                           completionOpen,              completionClose,             completionFilter,            completionNext,              completionEof,               completionColumn,            completionRowid,             0,                           0,                           0,                           0,                           0,                           0,                           0,                           0,                           0,                           0,                           0                          };#endif int sqlite3CompletionVtabInit(sqlite3 *db){  int rc = SQLITE_OK;#ifndef SQLITE_OMIT_VIRTUALTABLE  rc = sqlite3_create_module(db, "completion", &completionModule, 0);#endif  return rc;}#ifdef _WIN32#endifint sqlite3_completion_init(  sqlite3 *db,   char **pzErrMsg,   const sqlite3_api_routines *pApi){  int rc = SQLITE_OK;  SQLITE_EXTENSION_INIT2(pApi);  (void)(pzErrMsg);  #ifndef SQLITE_OMIT_VIRTUALTABLE  rc = sqlite3CompletionVtabInit(db);#endif  return rc;}SQLITE_EXTENSION_INIT1#include <string.h>#include <assert.h>#define APND_MARK_PREFIX     "Start-Of-SQLite3-"#define APND_MARK_PREFIX_SZ  17#define APND_MARK_FOS_SZ      8#define APND_MARK_SIZE       (APND_MARK_PREFIX_SZ+APND_MARK_FOS_SZ)#define APND_MAX_SIZE  (0x40000000)#ifndef APND_ROUNDUP#define APND_ROUNDUP 4096#endif#define APND_ALIGN_MASK         ((sqlite3_int64)(APND_ROUNDUP-1))#define APND_START_ROUNDUP(fsz) (((fsz)+APND_ALIGN_MASK) & ~APND_ALIGN_MASK)typedef struct sqlite3_vfs ApndVfs;typedef struct ApndFile ApndFile;#define ORIGVFS(p)  ((sqlite3_vfs*)((p)->pAppData))#define ORIGFILE(p) ((sqlite3_file*)(((ApndFile*)(p))+1))struct ApndFile {  sqlite3_file base;          sqlite3_int64 iPgOne;       sqlite3_int64 iMark;        };static int apndClose(sqlite3_file*);static int apndRead(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst);static int apndWrite(sqlite3_file*,const void*,int iAmt, sqlite3_int64 iOfst);static int apndTruncate(sqlite3_file*, sqlite3_int64 size);static int apndSync(sqlite3_file*, int flags);static int apndFileSize(sqlite3_file*, sqlite3_int64 *pSize);static int apndLock(sqlite3_file*, int);static int apndUnlock(sqlite3_file*, int);static int apndCheckReservedLock(sqlite3_file*, int *pResOut);static int apndFileControl(sqlite3_file*, int op, void *pArg);static int apndSectorSize(sqlite3_file*);static int apndDeviceCharacteristics(sqlite3_file*);static int apndShmMap(sqlite3_file*, int iPg, int pgsz, int, void volatile**);static int apndShmLock(sqlite3_file*, int offset, int n, int flags);static void apndShmBarrier(sqlite3_file*);static int apndShmUnmap(sqlite3_file*, int deleteFlag);static int apndFetch(sqlite3_file*, sqlite3_int64 iOfst, int iAmt, void **pp);static int apndUnfetch(sqlite3_file*, sqlite3_int64 iOfst, void *p);static int apndOpen(sqlite3_vfs*, const char *, sqlite3_file*, int , int *);static int apndDelete(sqlite3_vfs*, const char *zName, int syncDir);static int apndAccess(sqlite3_vfs*, const char *zName, int flags, int *);static int apndFullPathname(sqlite3_vfs*, const char *zName, int, char *zOut);static void *apndDlOpen(sqlite3_vfs*, const char *zFilename);static void apndDlError(sqlite3_vfs*, int nByte, char *zErrMsg);static void (*apndDlSym(sqlite3_vfs *pVfs, void *p, const char*zSym))(void);static void apndDlClose(sqlite3_vfs*, void*);static int apndRandomness(sqlite3_vfs*, int nByte, char *zOut);static int apndSleep(sqlite3_vfs*, int microseconds);static int apndCurrentTime(sqlite3_vfs*, double*);static int apndGetLastError(sqlite3_vfs*, int, char *);static int apndCurrentTimeInt64(sqlite3_vfs*, sqlite3_int64*);static int apndSetSystemCall(sqlite3_vfs*, const char*,sqlite3_syscall_ptr);static sqlite3_syscall_ptr apndGetSystemCall(sqlite3_vfs*, const char *z);static const char *apndNextSystemCall(sqlite3_vfs*, const char *zName);static sqlite3_vfs apnd_vfs = {  3,                              0,                              1024,                           0,                              "apndvfs",                      0,                               apndOpen,                       apndDelete,                     apndAccess,                     apndFullPathname,               apndDlOpen,                     apndDlError,                    apndDlSym,                      apndDlClose,                    apndRandomness,                 apndSleep,                      apndCurrentTime,                apndGetLastError,               apndCurrentTimeInt64,           apndSetSystemCall,              apndGetSystemCall,              apndNextSystemCall            };static const sqlite3_io_methods apnd_io_methods = {  3,                                apndClose,                        apndRead,                         apndWrite,                        apndTruncate,                     apndSync,                         apndFileSize,                     apndLock,                         apndUnlock,                       apndCheckReservedLock,            apndFileControl,                  apndSectorSize,                   apndDeviceCharacteristics,        apndShmMap,                       apndShmLock,                      apndShmBarrier,                   apndShmUnmap,                     apndFetch,                        apndUnfetch                     };static int apndClose(sqlite3_file *pFile){  pFile = ORIGFILE(pFile);  return pFile->pMethods->xClose(pFile);}static int apndRead(  sqlite3_file *pFile,   void *zBuf,   int iAmt,   sqlite_int64 iOfst){  ApndFile *paf = (ApndFile *)pFile;  pFile = ORIGFILE(pFile);  return pFile->pMethods->xRead(pFile, zBuf, iAmt, paf->iPgOne+iOfst);}static int apndWriteMark(  ApndFile *paf,  sqlite3_file *pFile,  sqlite_int64 iWriteEnd){  sqlite_int64 iPgOne = paf->iPgOne;  unsigned char a[APND_MARK_SIZE];  int i = APND_MARK_FOS_SZ;  int rc;  assert(pFile == ORIGFILE(paf));  memcpy(a, APND_MARK_PREFIX, APND_MARK_PREFIX_SZ);  while( --i >= 0 ){    a[APND_MARK_PREFIX_SZ+i] = (unsigned char)(iPgOne & 0xff);    iPgOne >>= 8;  }  iWriteEnd += paf->iPgOne;  if( SQLITE_OK==(rc = pFile->pMethods->xWrite                  (pFile, a, APND_MARK_SIZE, iWriteEnd)) ){    paf->iMark = iWriteEnd;  }  return rc;}static int apndWrite(  sqlite3_file *pFile,  const void *zBuf,  int iAmt,  sqlite_int64 iOfst){  ApndFile *paf = (ApndFile *)pFile;  sqlite_int64 iWriteEnd = iOfst + iAmt;  if( iWriteEnd>=APND_MAX_SIZE ) return SQLITE_FULL;  pFile = ORIGFILE(pFile);    if( paf->iMark < 0 || paf->iPgOne + iWriteEnd > paf->iMark ){    int rc = apndWriteMark(paf, pFile, iWriteEnd);    if( SQLITE_OK!=rc ) return rc;  }  return pFile->pMethods->xWrite(pFile, zBuf, iAmt, paf->iPgOne+iOfst);}static int apndTruncate(sqlite3_file *pFile, sqlite_int64 size){  ApndFile *paf = (ApndFile *)pFile;  pFile = ORIGFILE(pFile);    if( SQLITE_OK!=apndWriteMark(paf, pFile, size) ) return SQLITE_IOERR;    return pFile->pMethods->xTruncate(pFile, paf->iMark+APND_MARK_SIZE);}static int apndSync(sqlite3_file *pFile, int flags){  pFile = ORIGFILE(pFile);  return pFile->pMethods->xSync(pFile, flags);}static int apndFileSize(sqlite3_file *pFile, sqlite_int64 *pSize){  ApndFile *paf = (ApndFile *)pFile;  *pSize = ( paf->iMark >= 0 )? (paf->iMark - paf->iPgOne) : 0;  return SQLITE_OK;}static int apndLock(sqlite3_file *pFile, int eLock){  pFile = ORIGFILE(pFile);  return pFile->pMethods->xLock(pFile, eLock);}static int apndUnlock(sqlite3_file *pFile, int eLock){  pFile = ORIGFILE(pFile);  return pFile->pMethods->xUnlock(pFile, eLock);}static int apndCheckReservedLock(sqlite3_file *pFile, int *pResOut){  pFile = ORIGFILE(pFile);  return pFile->pMethods->xCheckReservedLock(pFile, pResOut);}static int apndFileControl(sqlite3_file *pFile, int op, void *pArg){  ApndFile *paf = (ApndFile *)pFile;  int rc;  pFile = ORIGFILE(pFile);  if( op==SQLITE_FCNTL_SIZE_HINT ) *(sqlite3_int64*)pArg += paf->iPgOne;  rc = pFile->pMethods->xFileControl(pFile, op, pArg);  if( rc==SQLITE_OK && op==SQLITE_FCNTL_VFSNAME ){    *(char**)pArg = sqlite3_mprintf("apnd(%lld)/%z", paf->iPgOne,*(char**)pArg);  }  return rc;}static int apndSectorSize(sqlite3_file *pFile){  pFile = ORIGFILE(pFile);  return pFile->pMethods->xSectorSize(pFile);}static int apndDeviceCharacteristics(sqlite3_file *pFile){  pFile = ORIGFILE(pFile);  return pFile->pMethods->xDeviceCharacteristics(pFile);}static int apndShmMap(  sqlite3_file *pFile,  int iPg,  int pgsz,  int bExtend,  void volatile **pp){  pFile = ORIGFILE(pFile);  return pFile->pMethods->xShmMap(pFile,iPg,pgsz,bExtend,pp);}static int apndShmLock(sqlite3_file *pFile, int offset, int n, int flags){  pFile = ORIGFILE(pFile);  return pFile->pMethods->xShmLock(pFile,offset,n,flags);}static void apndShmBarrier(sqlite3_file *pFile){  pFile = ORIGFILE(pFile);  pFile->pMethods->xShmBarrier(pFile);}static int apndShmUnmap(sqlite3_file *pFile, int deleteFlag){  pFile = ORIGFILE(pFile);  return pFile->pMethods->xShmUnmap(pFile,deleteFlag);}static int apndFetch(  sqlite3_file *pFile,  sqlite3_int64 iOfst,  int iAmt,  void **pp){  ApndFile *p = (ApndFile *)pFile;  if( p->iMark < 0 || iOfst+iAmt > p->iMark ){    return SQLITE_IOERR;   }  pFile = ORIGFILE(pFile);  return pFile->pMethods->xFetch(pFile, iOfst+p->iPgOne, iAmt, pp);}static int apndUnfetch(sqlite3_file *pFile, sqlite3_int64 iOfst, void *pPage){  ApndFile *p = (ApndFile *)pFile;  pFile = ORIGFILE(pFile);  return pFile->pMethods->xUnfetch(pFile, iOfst+p->iPgOne, pPage);}static sqlite3_int64 apndReadMark(sqlite3_int64 sz, sqlite3_file *pFile){  int rc, i;  sqlite3_int64 iMark;  int msbs = 8 * (APND_MARK_FOS_SZ-1);  unsigned char a[APND_MARK_SIZE];  if( APND_MARK_SIZE!=(sz & 0x1ff) ) return -1;  rc = pFile->pMethods->xRead(pFile, a, APND_MARK_SIZE, sz-APND_MARK_SIZE);  if( rc ) return -1;  if( memcmp(a, APND_MARK_PREFIX, APND_MARK_PREFIX_SZ)!=0 ) return -1;  iMark = ((sqlite3_int64)(a[APND_MARK_PREFIX_SZ] & 0x7f)) << msbs;  for(i=1; i<8; i++){    msbs -= 8;    iMark |= (sqlite3_int64)a[APND_MARK_PREFIX_SZ+i]<<msbs;  }  if( iMark > (sz - APND_MARK_SIZE - 512) ) return -1;  if( iMark & 0x1ff ) return -1;  return iMark;}static const char apvfsSqliteHdr[] = "SQLite format 3";static int apndIsAppendvfsDatabase(sqlite3_int64 sz, sqlite3_file *pFile){  int rc;  char zHdr[16];  sqlite3_int64 iMark = apndReadMark(sz, pFile);  if( iMark>=0 ){        rc = pFile->pMethods->xRead(pFile, zHdr, sizeof(zHdr), iMark);    if( SQLITE_OK==rc     && memcmp(zHdr, apvfsSqliteHdr, sizeof(zHdr))==0     && (sz & 0x1ff) == APND_MARK_SIZE     && sz>=512+APND_MARK_SIZE    ){      return 1;     }  }  return 0;}static int apndIsOrdinaryDatabaseFile(sqlite3_int64 sz, sqlite3_file *pFile){  char zHdr[16];  if( apndIsAppendvfsDatabase(sz, pFile)    || (sz & 0x1ff) != 0   || SQLITE_OK!=pFile->pMethods->xRead(pFile, zHdr, sizeof(zHdr), 0)   || memcmp(zHdr, apvfsSqliteHdr, sizeof(zHdr))!=0  ){    return 0;  }else{    return 1;  }}static int apndOpen(  sqlite3_vfs *pApndVfs,  const char *zName,  sqlite3_file *pFile,  int flags,  int *pOutFlags){  ApndFile *pApndFile = (ApndFile*)pFile;  sqlite3_file *pBaseFile = ORIGFILE(pFile);  sqlite3_vfs *pBaseVfs = ORIGVFS(pApndVfs);  int rc;  sqlite3_int64 sz = 0;  if( (flags & SQLITE_OPEN_MAIN_DB)==0 ){        return pBaseVfs->xOpen(pBaseVfs, zName, pFile, flags, pOutFlags);  }  memset(pApndFile, 0, sizeof(ApndFile));  pFile->pMethods = &apnd_io_methods;  pApndFile->iMark = -1;      rc = pBaseVfs->xOpen(pBaseVfs, zName, pBaseFile, flags, pOutFlags);  if( rc==SQLITE_OK ){    rc = pBaseFile->pMethods->xFileSize(pBaseFile, &sz);    if( rc ){      pBaseFile->pMethods->xClose(pBaseFile);    }  }  if( rc ){    pFile->pMethods = 0;    return rc;  }  if( apndIsOrdinaryDatabaseFile(sz, pBaseFile) ){        memmove(pApndFile, pBaseFile, pBaseVfs->szOsFile);    return SQLITE_OK;  }  pApndFile->iPgOne = apndReadMark(sz, pFile);  if( pApndFile->iPgOne>=0 ){    pApndFile->iMark = sz - APND_MARK_SIZE;     return SQLITE_OK;  }  if( (flags & SQLITE_OPEN_CREATE)==0 ){    pBaseFile->pMethods->xClose(pBaseFile);    rc = SQLITE_CANTOPEN;    pFile->pMethods = 0;  }else{        pApndFile->iPgOne = APND_START_ROUNDUP(sz);  }  return rc;}static int apndDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){  return ORIGVFS(pVfs)->xDelete(ORIGVFS(pVfs), zPath, dirSync);}static int apndAccess(  sqlite3_vfs *pVfs,   const char *zPath,   int flags,   int *pResOut){  return ORIGVFS(pVfs)->xAccess(ORIGVFS(pVfs), zPath, flags, pResOut);}static int apndFullPathname(  sqlite3_vfs *pVfs,   const char *zPath,   int nOut,   char *zOut){  return ORIGVFS(pVfs)->xFullPathname(ORIGVFS(pVfs),zPath,nOut,zOut);}static void *apndDlOpen(sqlite3_vfs *pVfs, const char *zPath){  return ORIGVFS(pVfs)->xDlOpen(ORIGVFS(pVfs), zPath);}static void apndDlError(sqlite3_vfs *pVfs, int nByte, char *zErrMsg){  ORIGVFS(pVfs)->xDlError(ORIGVFS(pVfs), nByte, zErrMsg);}static void (*apndDlSym(sqlite3_vfs *pVfs, void *p, const char *zSym))(void){  return ORIGVFS(pVfs)->xDlSym(ORIGVFS(pVfs), p, zSym);}static void apndDlClose(sqlite3_vfs *pVfs, void *pHandle){  ORIGVFS(pVfs)->xDlClose(ORIGVFS(pVfs), pHandle);}static int apndRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){  return ORIGVFS(pVfs)->xRandomness(ORIGVFS(pVfs), nByte, zBufOut);}static int apndSleep(sqlite3_vfs *pVfs, int nMicro){  return ORIGVFS(pVfs)->xSleep(ORIGVFS(pVfs), nMicro);}static int apndCurrentTime(sqlite3_vfs *pVfs, double *pTimeOut){  return ORIGVFS(pVfs)->xCurrentTime(ORIGVFS(pVfs), pTimeOut);}static int apndGetLastError(sqlite3_vfs *pVfs, int a, char *b){  return ORIGVFS(pVfs)->xGetLastError(ORIGVFS(pVfs), a, b);}static int apndCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *p){  return ORIGVFS(pVfs)->xCurrentTimeInt64(ORIGVFS(pVfs), p);}static int apndSetSystemCall(  sqlite3_vfs *pVfs,  const char *zName,  sqlite3_syscall_ptr pCall){  return ORIGVFS(pVfs)->xSetSystemCall(ORIGVFS(pVfs),zName,pCall);}static sqlite3_syscall_ptr apndGetSystemCall(  sqlite3_vfs *pVfs,  const char *zName){  return ORIGVFS(pVfs)->xGetSystemCall(ORIGVFS(pVfs),zName);}static const char *apndNextSystemCall(sqlite3_vfs *pVfs, const char *zName){  return ORIGVFS(pVfs)->xNextSystemCall(ORIGVFS(pVfs), zName);}  #ifdef _WIN32#endifint sqlite3_appendvfs_init(  sqlite3 *db,   char **pzErrMsg,   const sqlite3_api_routines *pApi){  int rc = SQLITE_OK;  sqlite3_vfs *pOrig;  SQLITE_EXTENSION_INIT2(pApi);  (void)pzErrMsg;  (void)db;  pOrig = sqlite3_vfs_find(0);  if( pOrig==0 ) return SQLITE_ERROR;  apnd_vfs.iVersion = pOrig->iVersion;  apnd_vfs.pAppData = pOrig;  apnd_vfs.szOsFile = pOrig->szOsFile + sizeof(ApndFile);  rc = sqlite3_vfs_register(&apnd_vfs, 0);#ifdef APPENDVFS_TEST  if( rc==SQLITE_OK ){    rc = sqlite3_auto_extension((void(*)(void))apndvfsRegister);  }#endif  if( rc==SQLITE_OK ) rc = SQLITE_OK_LOAD_PERMANENTLY;  return rc;}#include <assert.h>#include <string.h>#include <stdio.h>static sqlite3_mem_methods memtraceBase;static FILE *memtraceOut;static void *memtraceMalloc(int n){  if( memtraceOut ){    fprintf(memtraceOut, "MEMTRACE: allocate %d bytes\n",             memtraceBase.xRoundup(n));  }  return memtraceBase.xMalloc(n);}static void memtraceFree(void *p){  if( p==0 ) return;  if( memtraceOut ){    fprintf(memtraceOut, "MEMTRACE: free %d bytes\n", memtraceBase.xSize(p));  }  memtraceBase.xFree(p);}static void *memtraceRealloc(void *p, int n){  if( p==0 ) return memtraceMalloc(n);  if( n==0 ){    memtraceFree(p);    return 0;  }  if( memtraceOut ){    fprintf(memtraceOut, "MEMTRACE: resize %d -> %d bytes\n",            memtraceBase.xSize(p), memtraceBase.xRoundup(n));  }  return memtraceBase.xRealloc(p, n);}static int memtraceSize(void *p){  return memtraceBase.xSize(p);}static int memtraceRoundup(int n){  return memtraceBase.xRoundup(n);}static int memtraceInit(void *p){  return memtraceBase.xInit(p);}static void memtraceShutdown(void *p){  memtraceBase.xShutdown(p);}static sqlite3_mem_methods ersaztMethods = {  memtraceMalloc,  memtraceFree,  memtraceRealloc,  memtraceSize,  memtraceRoundup,  memtraceInit,  memtraceShutdown,  0};int sqlite3MemTraceActivate(FILE *out){  int rc = SQLITE_OK;  if( memtraceBase.xMalloc==0 ){    rc = sqlite3_config(SQLITE_CONFIG_GETMALLOC, &memtraceBase);    if( rc==SQLITE_OK ){      rc = sqlite3_config(SQLITE_CONFIG_MALLOC, &ersaztMethods);    }  }  memtraceOut = out;  return rc;}int sqlite3MemTraceDeactivate(void){  int rc = SQLITE_OK;  if( memtraceBase.xMalloc!=0 ){    rc = sqlite3_config(SQLITE_CONFIG_MALLOC, &memtraceBase);    if( rc==SQLITE_OK ){      memset(&memtraceBase, 0, sizeof(memtraceBase));    }  }  memtraceOut = 0;  return rc;}SQLITE_EXTENSION_INIT1#include <assert.h>#include <string.h>#include <ctype.h>static int uintCollFunc(  void *notUsed,  int nKey1, const void *pKey1,  int nKey2, const void *pKey2){  const unsigned char *zA = (const unsigned char*)pKey1;  const unsigned char *zB = (const unsigned char*)pKey2;  int i=0, j=0, x;  (void)notUsed;  while( i<nKey1 && j<nKey2 ){    x = zA[i] - zB[j];    if( isdigit(zA[i]) ){      int k;      if( !isdigit(zB[j]) ) return x;      while( i<nKey1 && zA[i]=='0' ){ i++; }      while( j<nKey2 && zB[j]=='0' ){ j++; }      k = 0;      while( i+k<nKey1 && isdigit(zA[i+k])             && j+k<nKey2 && isdigit(zB[j+k]) ){        k++;      }      if( i+k<nKey1 && isdigit(zA[i+k]) ){        return +1;      }else if( j+k<nKey2 && isdigit(zB[j+k]) ){        return -1;      }else{        x = memcmp(zA+i, zB+j, k);        if( x ) return x;        i += k;        j += k;      }    }else if( x ){      return x;    }else{      i++;      j++;    }  }  return (nKey1 - i) - (nKey2 - j);}#ifdef _WIN32#endifint sqlite3_uint_init(  sqlite3 *db,   char **pzErrMsg,   const sqlite3_api_routines *pApi){  SQLITE_EXTENSION_INIT2(pApi);  (void)pzErrMsg;    return sqlite3_create_collation(db, "uint", SQLITE_UTF8, 0, uintCollFunc);}SQLITE_EXTENSION_INIT1#include <assert.h>#include <string.h>#include <ctype.h>#include <stdlib.h>#ifndef UNUSED_PARAMETER# define UNUSED_PARAMETER(X)  (void)(X)#endiftypedef struct Decimal Decimal;struct Decimal {  char sign;          char oom;           char isNull;        char isInit;        int nDigit;         int nFrac;          signed char *a;   };static void decimal_clear(Decimal *p){  sqlite3_free(p->a);}static void decimal_free(Decimal *p){  if( p ){    decimal_clear(p);    sqlite3_free(p);  }}static Decimal *decimal_new(  sqlite3_context *pCtx,  sqlite3_value *pIn,  int nAlt,  const unsigned char *zAlt){  Decimal *p;  int n, i;  const unsigned char *zIn;  int iExp = 0;  p = sqlite3_malloc( sizeof(*p) );  if( p==0 ) goto new_no_mem;  p->sign = 0;  p->oom = 0;  p->isInit = 1;  p->isNull = 0;  p->nDigit = 0;  p->nFrac = 0;  if( zAlt ){    n = nAlt,    zIn = zAlt;  }else{    if( sqlite3_value_type(pIn)==SQLITE_NULL ){      p->a = 0;      p->isNull = 1;      return p;    }    n = sqlite3_value_bytes(pIn);    zIn = sqlite3_value_text(pIn);  }  p->a = sqlite3_malloc64( n+1 );  if( p->a==0 ) goto new_no_mem;  for(i=0; isspace(zIn[i]); i++){}  if( zIn[i]=='-' ){    p->sign = 1;    i++;  }else if( zIn[i]=='+' ){    i++;  }  while( i<n && zIn[i]=='0' ) i++;  while( i<n ){    char c = zIn[i];    if( c>='0' && c<='9' ){      p->a[p->nDigit++] = c - '0';    }else if( c=='.' ){      p->nFrac = p->nDigit + 1;    }else if( c=='e' || c=='E' ){      int j = i+1;      int neg = 0;      if( j>=n ) break;      if( zIn[j]=='-' ){        neg = 1;        j++;      }else if( zIn[j]=='+' ){        j++;      }      while( j<n && iExp<1000000 ){        if( zIn[j]>='0' && zIn[j]<='9' ){          iExp = iExp*10 + zIn[j] - '0';        }        j++;      }      if( neg ) iExp = -iExp;      break;    }    i++;  }  if( p->nFrac ){    p->nFrac = p->nDigit - (p->nFrac - 1);  }  if( iExp>0 ){    if( p->nFrac>0 ){      if( iExp<=p->nFrac ){        p->nFrac -= iExp;        iExp = 0;      }else{        iExp -= p->nFrac;        p->nFrac = 0;      }    }    if( iExp>0 ){         p->a = sqlite3_realloc64(p->a, p->nDigit + iExp + 1 );      if( p->a==0 ) goto new_no_mem;      memset(p->a+p->nDigit, 0, iExp);      p->nDigit += iExp;    }  }else if( iExp<0 ){    int nExtra;    iExp = -iExp;    nExtra = p->nDigit - p->nFrac - 1;    if( nExtra ){      if( nExtra>=iExp ){        p->nFrac += iExp;        iExp  = 0;      }else{        iExp -= nExtra;        p->nFrac = p->nDigit - 1;      }    }    if( iExp>0 ){      p->a = sqlite3_realloc64(p->a, p->nDigit + iExp + 1 );      if( p->a==0 ) goto new_no_mem;      memmove(p->a+iExp, p->a, p->nDigit);      memset(p->a, 0, iExp);      p->nDigit += iExp;      p->nFrac += iExp;    }  }  return p;new_no_mem:  if( pCtx ) sqlite3_result_error_nomem(pCtx);  sqlite3_free(p);  return 0;}static void decimal_result(sqlite3_context *pCtx, Decimal *p){  char *z;  int i, j;  int n;  if( p==0 || p->oom ){    sqlite3_result_error_nomem(pCtx);    return;  }  if( p->isNull ){    sqlite3_result_null(pCtx);    return;  }  z = sqlite3_malloc( p->nDigit+4 );  if( z==0 ){    sqlite3_result_error_nomem(pCtx);    return;  }  i = 0;  if( p->nDigit==0 || (p->nDigit==1 && p->a[0]==0) ){    p->sign = 0;  }  if( p->sign ){    z[0] = '-';    i = 1;  }  n = p->nDigit - p->nFrac;  if( n<=0 ){    z[i++] = '0';  }  j = 0;  while( n>1 && p->a[j]==0 ){    j++;    n--;  }  while( n>0  ){    z[i++] = p->a[j] + '0';    j++;    n--;  }  if( p->nFrac ){    z[i++] = '.';    do{      z[i++] = p->a[j] + '0';      j++;    }while( j<p->nDigit );  }  z[i] = 0;  sqlite3_result_text(pCtx, z, i, sqlite3_free);}static void decimalFunc(  sqlite3_context *context,  int argc,  sqlite3_value **argv){  Decimal *p = decimal_new(context, argv[0], 0, 0);  UNUSED_PARAMETER(argc);  decimal_result(context, p);  decimal_free(p);}static int decimal_cmp(const Decimal *pA, const Decimal *pB){  int nASig, nBSig, rc, n;  if( pA->sign!=pB->sign ){    return pA->sign ? -1 : +1;  }  if( pA->sign ){    const Decimal *pTemp = pA;    pA = pB;    pB = pTemp;  }  nASig = pA->nDigit - pA->nFrac;  nBSig = pB->nDigit - pB->nFrac;  if( nASig!=nBSig ){    return nASig - nBSig;  }  n = pA->nDigit;  if( n>pB->nDigit ) n = pB->nDigit;  rc = memcmp(pA->a, pB->a, n);  if( rc==0 ){    rc = pA->nDigit - pB->nDigit;  }  return rc;}static void decimalCmpFunc(  sqlite3_context *context,  int argc,  sqlite3_value **argv){  Decimal *pA = 0, *pB = 0;  int rc;  UNUSED_PARAMETER(argc);  pA = decimal_new(context, argv[0], 0, 0);  if( pA==0 || pA->isNull ) goto cmp_done;  pB = decimal_new(context, argv[1], 0, 0);  if( pB==0 || pB->isNull ) goto cmp_done;  rc = decimal_cmp(pA, pB);  if( rc<0 ) rc = -1;  else if( rc>0 ) rc = +1;  sqlite3_result_int(context, rc);cmp_done:  decimal_free(pA);  decimal_free(pB);}static void decimal_expand(Decimal *p, int nDigit, int nFrac){  int nAddSig;  int nAddFrac;  if( p==0 ) return;  nAddFrac = nFrac - p->nFrac;  nAddSig = (nDigit - p->nDigit) - nAddFrac;  if( nAddFrac==0 && nAddSig==0 ) return;  p->a = sqlite3_realloc64(p->a, nDigit+1);  if( p->a==0 ){    p->oom = 1;    return;  }  if( nAddSig ){    memmove(p->a+nAddSig, p->a, p->nDigit);    memset(p->a, 0, nAddSig);    p->nDigit += nAddSig;  }  if( nAddFrac ){    memset(p->a+p->nDigit, 0, nAddFrac);    p->nDigit += nAddFrac;    p->nFrac += nAddFrac;  }}static void decimal_add(Decimal *pA, Decimal *pB){  int nSig, nFrac, nDigit;  int i, rc;  if( pA==0 ){    return;  }  if( pA->oom || pB==0 || pB->oom ){    pA->oom = 1;    return;  }  if( pA->isNull || pB->isNull ){    pA->isNull = 1;    return;  }  nSig = pA->nDigit - pA->nFrac;  if( nSig && pA->a[0]==0 ) nSig--;  if( nSig<pB->nDigit-pB->nFrac ){    nSig = pB->nDigit - pB->nFrac;  }  nFrac = pA->nFrac;  if( nFrac<pB->nFrac ) nFrac = pB->nFrac;  nDigit = nSig + nFrac + 1;  decimal_expand(pA, nDigit, nFrac);  decimal_expand(pB, nDigit, nFrac);  if( pA->oom || pB->oom ){    pA->oom = 1;  }else{    if( pA->sign==pB->sign ){      int carry = 0;      for(i=nDigit-1; i>=0; i--){        int x = pA->a[i] + pB->a[i] + carry;        if( x>=10 ){          carry = 1;          pA->a[i] = x - 10;        }else{          carry = 0;          pA->a[i] = x;        }      }    }else{      signed char *aA, *aB;      int borrow = 0;      rc = memcmp(pA->a, pB->a, nDigit);      if( rc<0 ){        aA = pB->a;        aB = pA->a;        pA->sign = !pA->sign;      }else{        aA = pA->a;        aB = pB->a;      }      for(i=nDigit-1; i>=0; i--){        int x = aA[i] - aB[i] - borrow;        if( x<0 ){          pA->a[i] = x+10;          borrow = 1;        }else{          pA->a[i] = x;          borrow = 0;        }      }    }  }}static int decimalCollFunc(  void *notUsed,  int nKey1, const void *pKey1,  int nKey2, const void *pKey2){  const unsigned char *zA = (const unsigned char*)pKey1;  const unsigned char *zB = (const unsigned char*)pKey2;  Decimal *pA = decimal_new(0, 0, nKey1, zA);  Decimal *pB = decimal_new(0, 0, nKey2, zB);  int rc;  UNUSED_PARAMETER(notUsed);  if( pA==0 || pB==0 ){    rc = 0;  }else{    rc = decimal_cmp(pA, pB);  }  decimal_free(pA);  decimal_free(pB);  return rc;}static void decimalAddFunc(  sqlite3_context *context,  int argc,  sqlite3_value **argv){  Decimal *pA = decimal_new(context, argv[0], 0, 0);  Decimal *pB = decimal_new(context, argv[1], 0, 0);  UNUSED_PARAMETER(argc);  decimal_add(pA, pB);  decimal_result(context, pA);  decimal_free(pA);  decimal_free(pB);}static void decimalSubFunc(  sqlite3_context *context,  int argc,  sqlite3_value **argv){  Decimal *pA = decimal_new(context, argv[0], 0, 0);  Decimal *pB = decimal_new(context, argv[1], 0, 0);  UNUSED_PARAMETER(argc);  if( pB ){    pB->sign = !pB->sign;    decimal_add(pA, pB);    decimal_result(context, pA);  }  decimal_free(pA);  decimal_free(pB);}static void decimalSumStep(  sqlite3_context *context,  int argc,  sqlite3_value **argv){  Decimal *p;  Decimal *pArg;  UNUSED_PARAMETER(argc);  p = sqlite3_aggregate_context(context, sizeof(*p));  if( p==0 ) return;  if( !p->isInit ){    p->isInit = 1;    p->a = sqlite3_malloc(2);    if( p->a==0 ){      p->oom = 1;    }else{      p->a[0] = 0;    }    p->nDigit = 1;    p->nFrac = 0;  }  if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;  pArg = decimal_new(context, argv[0], 0, 0);  decimal_add(p, pArg);  decimal_free(pArg);}static void decimalSumInverse(  sqlite3_context *context,  int argc,  sqlite3_value **argv){  Decimal *p;  Decimal *pArg;  UNUSED_PARAMETER(argc);  p = sqlite3_aggregate_context(context, sizeof(*p));  if( p==0 ) return;  if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;  pArg = decimal_new(context, argv[0], 0, 0);  if( pArg ) pArg->sign = !pArg->sign;  decimal_add(p, pArg);  decimal_free(pArg);}static void decimalSumValue(sqlite3_context *context){  Decimal *p = sqlite3_aggregate_context(context, 0);  if( p==0 ) return;  decimal_result(context, p);}static void decimalSumFinalize(sqlite3_context *context){  Decimal *p = sqlite3_aggregate_context(context, 0);  if( p==0 ) return;  decimal_result(context, p);  decimal_clear(p);}static void decimalMulFunc(  sqlite3_context *context,  int argc,  sqlite3_value **argv){  Decimal *pA = decimal_new(context, argv[0], 0, 0);  Decimal *pB = decimal_new(context, argv[1], 0, 0);  signed char *acc = 0;  int i, j, k;  int minFrac;  UNUSED_PARAMETER(argc);  if( pA==0 || pA->oom || pA->isNull   || pB==0 || pB->oom || pB->isNull   ){    goto mul_end;  }  acc = sqlite3_malloc64( pA->nDigit + pB->nDigit + 2 );  if( acc==0 ){    sqlite3_result_error_nomem(context);    goto mul_end;  }  memset(acc, 0, pA->nDigit + pB->nDigit + 2);  minFrac = pA->nFrac;  if( pB->nFrac<minFrac ) minFrac = pB->nFrac;  for(i=pA->nDigit-1; i>=0; i--){    signed char f = pA->a[i];    int carry = 0, x;    for(j=pB->nDigit-1, k=i+j+3; j>=0; j--, k--){      x = acc[k] + f*pB->a[j] + carry;      acc[k] = x%10;      carry = x/10;    }    x = acc[k] + carry;    acc[k] = x%10;    acc[k-1] += x/10;  }  sqlite3_free(pA->a);  pA->a = acc;  acc = 0;  pA->nDigit += pB->nDigit + 2;  pA->nFrac += pB->nFrac;  pA->sign ^= pB->sign;  while( pA->nFrac>minFrac && pA->a[pA->nDigit-1]==0 ){    pA->nFrac--;    pA->nDigit--;  }  decimal_result(context, pA);mul_end:  sqlite3_free(acc);  decimal_free(pA);  decimal_free(pB);}#ifdef _WIN32#endifint sqlite3_decimal_init(  sqlite3 *db,   char **pzErrMsg,   const sqlite3_api_routines *pApi){  int rc = SQLITE_OK;  static const struct {    const char *zFuncName;    int nArg;    void (*xFunc)(sqlite3_context*,int,sqlite3_value**);  } aFunc[] = {    { "decimal",       1,   decimalFunc        },    { "decimal_cmp",   2,   decimalCmpFunc     },    { "decimal_add",   2,   decimalAddFunc     },    { "decimal_sub",   2,   decimalSubFunc     },    { "decimal_mul",   2,   decimalMulFunc     },  };  unsigned int i;  (void)pzErrMsg;    SQLITE_EXTENSION_INIT2(pApi);  for(i=0; i<sizeof(aFunc)/sizeof(aFunc[0]) && rc==SQLITE_OK; i++){    rc = sqlite3_create_function(db, aFunc[i].zFuncName, aFunc[i].nArg,                   SQLITE_UTF8|SQLITE_INNOCUOUS|SQLITE_DETERMINISTIC,                   0, aFunc[i].xFunc, 0, 0);  }  if( rc==SQLITE_OK ){    rc = sqlite3_create_window_function(db, "decimal_sum", 1,                   SQLITE_UTF8|SQLITE_INNOCUOUS|SQLITE_DETERMINISTIC, 0,                   decimalSumStep, decimalSumFinalize,                   decimalSumValue, decimalSumInverse, 0);  }  if( rc==SQLITE_OK ){    rc = sqlite3_create_collation(db, "decimal", SQLITE_UTF8,                                  0, decimalCollFunc);  }  return rc;}SQLITE_EXTENSION_INIT1#include <assert.h>#include <string.h>#ifndef UNUSED_PARAMETER# define UNUSED_PARAMETER(X)  (void)(X)#endifstatic void ieee754func(  sqlite3_context *context,  int argc,  sqlite3_value **argv){  if( argc==1 ){    sqlite3_int64 m, a;    double r;    int e;    int isNeg;    char zResult[100];    assert( sizeof(m)==sizeof(r) );    if( sqlite3_value_type(argv[0])==SQLITE_BLOB     && sqlite3_value_bytes(argv[0])==sizeof(r)    ){      const unsigned char *x = sqlite3_value_blob(argv[0]);      unsigned int i;      sqlite3_uint64 v = 0;      for(i=0; i<sizeof(r); i++){        v = (v<<8) | x[i];      }      memcpy(&r, &v, sizeof(r));    }else{      r = sqlite3_value_double(argv[0]);    }    if( r<0.0 ){      isNeg = 1;      r = -r;    }else{      isNeg = 0;    }    memcpy(&a,&r,sizeof(a));    if( a==0 ){      e = 0;      m = 0;    }else{      e = a>>52;      m = a & ((((sqlite3_int64)1)<<52)-1);      if( e==0 ){        m <<= 1;      }else{        m |= ((sqlite3_int64)1)<<52;      }      while( e<1075 && m>0 && (m&1)==0 ){        m >>= 1;        e++;      }      if( isNeg ) m = -m;    }    switch( *(int*)sqlite3_user_data(context) ){      case 0:        sqlite3_snprintf(sizeof(zResult), zResult, "ieee754(%lld,%d)",                         m, e-1075);        sqlite3_result_text(context, zResult, -1, SQLITE_TRANSIENT);        break;      case 1:        sqlite3_result_int64(context, m);        break;      case 2:        sqlite3_result_int(context, e-1075);        break;    }  }else{    sqlite3_int64 m, e, a;    double r;    int isNeg = 0;    m = sqlite3_value_int64(argv[0]);    e = sqlite3_value_int64(argv[1]);        if( e>10000 ){      e = 10000;    }else if( e<-10000 ){      e = -10000;    }    if( m<0 ){      isNeg = 1;      m = -m;      if( m<0 ) return;    }else if( m==0 && e>-1000 && e<1000 ){      sqlite3_result_double(context, 0.0);      return;    }    while( (m>>32)&0xffe00000 ){      m >>= 1;      e++;    }    while( m!=0 && ((m>>32)&0xfff00000)==0 ){      m <<= 1;      e--;    }    e += 1075;    if( e<=0 ){            if( 1-e >= 64 ){        m = 0;      }else{        m >>= 1-e;      }      e = 0;    }else if( e>0x7ff ){      e = 0x7ff;    }    a = m & ((((sqlite3_int64)1)<<52)-1);    a |= e<<52;    if( isNeg ) a |= ((sqlite3_uint64)1)<<63;    memcpy(&r, &a, sizeof(r));    sqlite3_result_double(context, r);  }}static void ieee754func_from_blob(  sqlite3_context *context,  int argc,  sqlite3_value **argv){  UNUSED_PARAMETER(argc);  if( sqlite3_value_type(argv[0])==SQLITE_BLOB   && sqlite3_value_bytes(argv[0])==sizeof(double)  ){    double r;    const unsigned char *x = sqlite3_value_blob(argv[0]);    unsigned int i;    sqlite3_uint64 v = 0;    for(i=0; i<sizeof(r); i++){      v = (v<<8) | x[i];    }    memcpy(&r, &v, sizeof(r));    sqlite3_result_double(context, r);  }}static void ieee754func_to_blob(  sqlite3_context *context,  int argc,  sqlite3_value **argv){  UNUSED_PARAMETER(argc);  if( sqlite3_value_type(argv[0])==SQLITE_FLOAT   || sqlite3_value_type(argv[0])==SQLITE_INTEGER  ){    double r = sqlite3_value_double(argv[0]);    sqlite3_uint64 v;    unsigned char a[sizeof(r)];    unsigned int i;    memcpy(&v, &r, sizeof(r));    for(i=1; i<=sizeof(r); i++){      a[sizeof(r)-i] = v&0xff;      v >>= 8;    }    sqlite3_result_blob(context, a, sizeof(r), SQLITE_TRANSIENT);  }}#ifdef _WIN32#endifint sqlite3_ieee_init(  sqlite3 *db,   char **pzErrMsg,   const sqlite3_api_routines *pApi){  static const struct {    char *zFName;    int nArg;    int iAux;    void (*xFunc)(sqlite3_context*,int,sqlite3_value**);  } aFunc[] = {    { "ieee754",           1,   0, ieee754func },    { "ieee754",           2,   0, ieee754func },    { "ieee754_mantissa",  1,   1, ieee754func },    { "ieee754_exponent",  1,   2, ieee754func },    { "ieee754_to_blob",   1,   0, ieee754func_to_blob },    { "ieee754_from_blob", 1,   0, ieee754func_from_blob },  };  unsigned int i;  int rc = SQLITE_OK;  SQLITE_EXTENSION_INIT2(pApi);  (void)pzErrMsg;    for(i=0; i<sizeof(aFunc)/sizeof(aFunc[0]) && rc==SQLITE_OK; i++){    rc = sqlite3_create_function(db, aFunc[i].zFName, aFunc[i].nArg,                               SQLITE_UTF8|SQLITE_INNOCUOUS,                               (void*)&aFunc[i].iAux,                               aFunc[i].xFunc, 0, 0);  }  return rc;}SQLITE_EXTENSION_INIT1#include <assert.h>#include <string.h>#ifndef SQLITE_OMIT_VIRTUALTABLEtypedef struct series_cursor series_cursor;struct series_cursor {  sqlite3_vtab_cursor base;    int isDesc;                  sqlite3_int64 iRowid;        sqlite3_int64 iValue;        sqlite3_int64 mnValue;       sqlite3_int64 mxValue;       sqlite3_int64 iStep;       };static int seriesConnect(  sqlite3 *db,  void *pUnused,  int argcUnused, const char *const*argvUnused,  sqlite3_vtab **ppVtab,  char **pzErrUnused){  sqlite3_vtab *pNew;  int rc;#define SERIES_COLUMN_VALUE 0#define SERIES_COLUMN_START 1#define SERIES_COLUMN_STOP  2#define SERIES_COLUMN_STEP  3  (void)pUnused;  (void)argcUnused;  (void)argvUnused;  (void)pzErrUnused;  rc = sqlite3_declare_vtab(db,     "CREATE TABLE x(value,start hidden,stop hidden,step hidden)");  if( rc==SQLITE_OK ){    pNew = *ppVtab = sqlite3_malloc( sizeof(*pNew) );    if( pNew==0 ) return SQLITE_NOMEM;    memset(pNew, 0, sizeof(*pNew));    sqlite3_vtab_config(db, SQLITE_VTAB_INNOCUOUS);  }  return rc;}static int seriesDisconnect(sqlite3_vtab *pVtab){  sqlite3_free(pVtab);  return SQLITE_OK;}static int seriesOpen(sqlite3_vtab *pUnused, sqlite3_vtab_cursor **ppCursor){  series_cursor *pCur;  (void)pUnused;  pCur = sqlite3_malloc( sizeof(*pCur) );  if( pCur==0 ) return SQLITE_NOMEM;  memset(pCur, 0, sizeof(*pCur));  *ppCursor = &pCur->base;  return SQLITE_OK;}static int seriesClose(sqlite3_vtab_cursor *cur){  sqlite3_free(cur);  return SQLITE_OK;}static int seriesNext(sqlite3_vtab_cursor *cur){  series_cursor *pCur = (series_cursor*)cur;  if( pCur->isDesc ){    pCur->iValue -= pCur->iStep;  }else{    pCur->iValue += pCur->iStep;  }  pCur->iRowid++;  return SQLITE_OK;}static int seriesColumn(  sqlite3_vtab_cursor *cur,     sqlite3_context *ctx,         int i                       ){  series_cursor *pCur = (series_cursor*)cur;  sqlite3_int64 x = 0;  switch( i ){    case SERIES_COLUMN_START:  x = pCur->mnValue; break;    case SERIES_COLUMN_STOP:   x = pCur->mxValue; break;    case SERIES_COLUMN_STEP:   x = pCur->iStep;   break;    default:                   x = pCur->iValue;  break;  }  sqlite3_result_int64(ctx, x);  return SQLITE_OK;}static int seriesRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){  series_cursor *pCur = (series_cursor*)cur;  *pRowid = pCur->iRowid;  return SQLITE_OK;}static int seriesEof(sqlite3_vtab_cursor *cur){  series_cursor *pCur = (series_cursor*)cur;  if( pCur->isDesc ){    return pCur->iValue < pCur->mnValue;  }else{    return pCur->iValue > pCur->mxValue;  }}#ifndef SQLITE_SERIES_CONSTRAINT_VERIFY# define SQLITE_SERIES_CONSTRAINT_VERIFY 0#endifstatic int seriesFilter(  sqlite3_vtab_cursor *pVtabCursor,   int idxNum, const char *idxStrUnused,  int argc, sqlite3_value **argv){  series_cursor *pCur = (series_cursor *)pVtabCursor;  int i = 0;  (void)idxStrUnused;  if( idxNum & 1 ){    pCur->mnValue = sqlite3_value_int64(argv[i++]);  }else{    pCur->mnValue = 0;  }  if( idxNum & 2 ){    pCur->mxValue = sqlite3_value_int64(argv[i++]);  }else{    pCur->mxValue = 0xffffffff;  }  if( idxNum & 4 ){    pCur->iStep = sqlite3_value_int64(argv[i++]);    if( pCur->iStep==0 ){      pCur->iStep = 1;    }else if( pCur->iStep<0 ){      pCur->iStep = -pCur->iStep;      if( (idxNum & 16)==0 ) idxNum |= 8;    }  }else{    pCur->iStep = 1;  }  for(i=0; i<argc; i++){    if( sqlite3_value_type(argv[i])==SQLITE_NULL ){            pCur->mnValue = 1;      pCur->mxValue = 0;      break;    }  }  if( idxNum & 8 ){    pCur->isDesc = 1;    pCur->iValue = pCur->mxValue;    if( pCur->iStep>0 ){      pCur->iValue -= (pCur->mxValue - pCur->mnValue)%pCur->iStep;    }  }else{    pCur->isDesc = 0;    pCur->iValue = pCur->mnValue;  }  pCur->iRowid = 1;  return SQLITE_OK;}static int seriesBestIndex(  sqlite3_vtab *pVTab,  sqlite3_index_info *pIdxInfo){  int i, j;                int idxNum = 0;          int bStartSeen = 0;      int unusableMask = 0;    int nArg = 0;            int aIdx[3];             const struct sqlite3_index_constraint *pConstraint;    assert( SERIES_COLUMN_STOP == SERIES_COLUMN_START+1 );  assert( SERIES_COLUMN_STEP == SERIES_COLUMN_START+2 );  aIdx[0] = aIdx[1] = aIdx[2] = -1;  pConstraint = pIdxInfo->aConstraint;  for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){    int iCol;        int iMask;       if( pConstraint->iColumn<SERIES_COLUMN_START ) continue;    iCol = pConstraint->iColumn - SERIES_COLUMN_START;    assert( iCol>=0 && iCol<=2 );    iMask = 1 << iCol;    if( iCol==0 ) bStartSeen = 1;    if( pConstraint->usable==0 ){      unusableMask |=  iMask;      continue;    }else if( pConstraint->op==SQLITE_INDEX_CONSTRAINT_EQ ){      idxNum |= iMask;      aIdx[iCol] = i;    }  }  for(i=0; i<3; i++){    if( (j = aIdx[i])>=0 ){      pIdxInfo->aConstraintUsage[j].argvIndex = ++nArg;      pIdxInfo->aConstraintUsage[j].omit = !SQLITE_SERIES_CONSTRAINT_VERIFY;    }  }  #ifndef ZERO_ARGUMENT_GENERATE_SERIES  if( !bStartSeen ){    sqlite3_free(pVTab->zErrMsg);    pVTab->zErrMsg = sqlite3_mprintf(        "first argument to \"generate_series()\" missing or unusable");    return SQLITE_ERROR;  }#endif  if( (unusableMask & ~idxNum)!=0 ){        return SQLITE_CONSTRAINT;  }  if( (idxNum & 3)==3 ){        pIdxInfo->estimatedCost = (double)(2 - ((idxNum&4)!=0));    pIdxInfo->estimatedRows = 1000;    if( pIdxInfo->nOrderBy>=1 && pIdxInfo->aOrderBy[0].iColumn==0 ){      if( pIdxInfo->aOrderBy[0].desc ){        idxNum |= 8;      }else{        idxNum |= 16;      }      pIdxInfo->orderByConsumed = 1;    }  }else{        pIdxInfo->estimatedRows = 2147483647;  }  pIdxInfo->idxNum = idxNum;  return SQLITE_OK;}static sqlite3_module seriesModule = {  0,                           0,                           seriesConnect,               seriesBestIndex,             seriesDisconnect,            0,                           seriesOpen,                  seriesClose,                 seriesFilter,                seriesNext,                  seriesEof,                   seriesColumn,                seriesRowid,                 0,                           0,                           0,                           0,                           0,                           0,                           0,                           0,                           0,                           0,                           0                          };#endif #ifdef _WIN32#endifint sqlite3_series_init(  sqlite3 *db,   char **pzErrMsg,   const sqlite3_api_routines *pApi){  int rc = SQLITE_OK;  SQLITE_EXTENSION_INIT2(pApi);#ifndef SQLITE_OMIT_VIRTUALTABLE  if( sqlite3_libversion_number()<3008012 && pzErrMsg!=0 ){    *pzErrMsg = sqlite3_mprintf(        "generate_series() requires SQLite 3.8.12 or later");    return SQLITE_ERROR;  }  rc = sqlite3_create_module(db, "generate_series", &seriesModule, 0);#endif  return rc;}#include <string.h>#include <stdlib.h>SQLITE_EXTENSION_INIT1#define re_match   sqlite3re_match#define re_compile sqlite3re_compile#define re_free    sqlite3re_free#define RE_EOF            0    #define RE_OP_MATCH       1    #define RE_OP_ANY         2    #define RE_OP_ANYSTAR     3    #define RE_OP_FORK        4    #define RE_OP_GOTO        5    #define RE_OP_ACCEPT      6    #define RE_OP_CC_INC      7    #define RE_OP_CC_EXC      8    #define RE_OP_CC_VALUE    9    #define RE_OP_CC_RANGE   10    #define RE_OP_WORD       11    #define RE_OP_NOTWORD    12    #define RE_OP_DIGIT      13    #define RE_OP_NOTDIGIT   14    #define RE_OP_SPACE      15    #define RE_OP_NOTSPACE   16    #define RE_OP_BOUNDARY   17    typedef unsigned short ReStateNumber;typedef struct ReStateSet {  unsigned nState;              ReStateNumber *aState;      } ReStateSet;typedef struct ReInput ReInput;struct ReInput {  const unsigned char *z;    int i;                     int mx;                  };typedef struct ReCompiled ReCompiled;struct ReCompiled {  ReInput sIn;                  const char *zErr;             char *aOp;                    int *aArg;                    unsigned (*xNextChar)(ReInput*);    unsigned char zInit[12];      int nInit;                    unsigned nState;              unsigned nAlloc;            };static void re_add_state(ReStateSet *pSet, int newState){  unsigned i;  for(i=0; i<pSet->nState; i++) if( pSet->aState[i]==newState ) return;  pSet->aState[pSet->nState++] = (ReStateNumber)newState;}static unsigned re_next_char(ReInput *p){  unsigned c;  if( p->i>=p->mx ) return 0;  c = p->z[p->i++];  if( c>=0x80 ){    if( (c&0xe0)==0xc0 && p->i<p->mx && (p->z[p->i]&0xc0)==0x80 ){      c = (c&0x1f)<<6 | (p->z[p->i++]&0x3f);      if( c<0x80 ) c = 0xfffd;    }else if( (c&0xf0)==0xe0 && p->i+1<p->mx && (p->z[p->i]&0xc0)==0x80           && (p->z[p->i+1]&0xc0)==0x80 ){      c = (c&0x0f)<<12 | ((p->z[p->i]&0x3f)<<6) | (p->z[p->i+1]&0x3f);      p->i += 2;      if( c<=0x7ff || (c>=0xd800 && c<=0xdfff) ) c = 0xfffd;    }else if( (c&0xf8)==0xf0 && p->i+3<p->mx && (p->z[p->i]&0xc0)==0x80           && (p->z[p->i+1]&0xc0)==0x80 && (p->z[p->i+2]&0xc0)==0x80 ){      c = (c&0x07)<<18 | ((p->z[p->i]&0x3f)<<12) | ((p->z[p->i+1]&0x3f)<<6)                       | (p->z[p->i+2]&0x3f);      p->i += 3;      if( c<=0xffff || c>0x10ffff ) c = 0xfffd;    }else{      c = 0xfffd;    }  }  return c;}static unsigned re_next_char_nocase(ReInput *p){  unsigned c = re_next_char(p);  if( c>='A' && c<='Z' ) c += 'a' - 'A';  return c;}static int re_word_char(int c){  return (c>='0' && c<='9') || (c>='a' && c<='z')      || (c>='A' && c<='Z') || c=='_';}static int re_digit_char(int c){  return (c>='0' && c<='9');}static int re_space_char(int c){  return c==' ' || c=='\t' || c=='\n' || c=='\r' || c=='\v' || c=='\f';}static int re_match(ReCompiled *pRe, const unsigned char *zIn, int nIn){  ReStateSet aStateSet[2], *pThis, *pNext;  ReStateNumber aSpace[100];  ReStateNumber *pToFree;  unsigned int i = 0;  unsigned int iSwap = 0;  int c = RE_EOF+1;  int cPrev = 0;  int rc = 0;  ReInput in;  in.z = zIn;  in.i = 0;  in.mx = nIn>=0 ? nIn : (int)strlen((char const*)zIn);    if( pRe->nInit ){    unsigned char x = pRe->zInit[0];    while( in.i+pRe->nInit<=in.mx      && (zIn[in.i]!=x ||         strncmp((const char*)zIn+in.i, (const char*)pRe->zInit, pRe->nInit)!=0)    ){      in.i++;    }    if( in.i+pRe->nInit>in.mx ) return 0;  }  if( pRe->nState<=(sizeof(aSpace)/(sizeof(aSpace[0])*2)) ){    pToFree = 0;    aStateSet[0].aState = aSpace;  }else{    pToFree = sqlite3_malloc64( sizeof(ReStateNumber)*2*pRe->nState );    if( pToFree==0 ) return -1;    aStateSet[0].aState = pToFree;  }  aStateSet[1].aState = &aStateSet[0].aState[pRe->nState];  pNext = &aStateSet[1];  pNext->nState = 0;  re_add_state(pNext, 0);  while( c!=RE_EOF && pNext->nState>0 ){    cPrev = c;    c = pRe->xNextChar(&in);    pThis = pNext;    pNext = &aStateSet[iSwap];    iSwap = 1 - iSwap;    pNext->nState = 0;    for(i=0; i<pThis->nState; i++){      int x = pThis->aState[i];      switch( pRe->aOp[x] ){        case RE_OP_MATCH: {          if( pRe->aArg[x]==c ) re_add_state(pNext, x+1);          break;        }        case RE_OP_ANY: {          if( c!=0 ) re_add_state(pNext, x+1);          break;        }        case RE_OP_WORD: {          if( re_word_char(c) ) re_add_state(pNext, x+1);          break;        }        case RE_OP_NOTWORD: {          if( !re_word_char(c) && c!=0 ) re_add_state(pNext, x+1);          break;        }        case RE_OP_DIGIT: {          if( re_digit_char(c) ) re_add_state(pNext, x+1);          break;        }        case RE_OP_NOTDIGIT: {          if( !re_digit_char(c) && c!=0 ) re_add_state(pNext, x+1);          break;        }        case RE_OP_SPACE: {          if( re_space_char(c) ) re_add_state(pNext, x+1);          break;        }        case RE_OP_NOTSPACE: {          if( !re_space_char(c) && c!=0 ) re_add_state(pNext, x+1);          break;        }        case RE_OP_BOUNDARY: {          if( re_word_char(c)!=re_word_char(cPrev) ) re_add_state(pThis, x+1);          break;        }        case RE_OP_ANYSTAR: {          re_add_state(pNext, x);          re_add_state(pThis, x+1);          break;        }        case RE_OP_FORK: {          re_add_state(pThis, x+pRe->aArg[x]);          re_add_state(pThis, x+1);          break;        }        case RE_OP_GOTO: {          re_add_state(pThis, x+pRe->aArg[x]);          break;        }        case RE_OP_ACCEPT: {          rc = 1;          goto re_match_end;        }        case RE_OP_CC_EXC: {          if( c==0 ) break;           goto re_op_cc_inc;        }        case RE_OP_CC_INC: re_op_cc_inc: {          int j = 1;          int n = pRe->aArg[x];          int hit = 0;          for(j=1; j>0 && j<n; j++){            if( pRe->aOp[x+j]==RE_OP_CC_VALUE ){              if( pRe->aArg[x+j]==c ){                hit = 1;                j = -1;              }            }else{              if( pRe->aArg[x+j]<=c && pRe->aArg[x+j+1]>=c ){                hit = 1;                j = -1;              }else{                j++;              }            }          }          if( pRe->aOp[x]==RE_OP_CC_EXC ) hit = !hit;          if( hit ) re_add_state(pNext, x+n);          break;        }      }    }  }  for(i=0; i<pNext->nState; i++){    if( pRe->aOp[pNext->aState[i]]==RE_OP_ACCEPT ){ rc = 1; break; }  }re_match_end:  sqlite3_free(pToFree);  return rc;}static int re_resize(ReCompiled *p, int N){  char *aOp;  int *aArg;  aOp = sqlite3_realloc64(p->aOp, N*sizeof(p->aOp[0]));  if( aOp==0 ) return 1;  p->aOp = aOp;  aArg = sqlite3_realloc64(p->aArg, N*sizeof(p->aArg[0]));  if( aArg==0 ) return 1;  p->aArg = aArg;  p->nAlloc = N;  return 0;}static int re_insert(ReCompiled *p, int iBefore, int op, int arg){  int i;  if( p->nAlloc<=p->nState && re_resize(p, p->nAlloc*2) ) return 0;  for(i=p->nState; i>iBefore; i--){    p->aOp[i] = p->aOp[i-1];    p->aArg[i] = p->aArg[i-1];  }  p->nState++;  p->aOp[iBefore] = (char)op;  p->aArg[iBefore] = arg;  return iBefore;}static int re_append(ReCompiled *p, int op, int arg){  return re_insert(p, p->nState, op, arg);}static void re_copy(ReCompiled *p, int iStart, int N){  if( p->nState+N>=p->nAlloc && re_resize(p, p->nAlloc*2+N) ) return;  memcpy(&p->aOp[p->nState], &p->aOp[iStart], N*sizeof(p->aOp[0]));  memcpy(&p->aArg[p->nState], &p->aArg[iStart], N*sizeof(p->aArg[0]));  p->nState += N;}static int re_hex(int c, int *pV){  if( c>='0' && c<='9' ){    c -= '0';  }else if( c>='a' && c<='f' ){    c -= 'a' - 10;  }else if( c>='A' && c<='F' ){    c -= 'A' - 10;  }else{    return 0;  }  *pV = (*pV)*16 + (c & 0xff);  return 1;}static unsigned re_esc_char(ReCompiled *p){  static const char zEsc[] = "afnrtv\\()*.+?[$^{|}]";  static const char zTrans[] = "\a\f\n\r\t\v";  int i, v = 0;  char c;  if( p->sIn.i>=p->sIn.mx ) return 0;  c = p->sIn.z[p->sIn.i];  if( c=='u' && p->sIn.i+4<p->sIn.mx ){    const unsigned char *zIn = p->sIn.z + p->sIn.i;    if( re_hex(zIn[1],&v)     && re_hex(zIn[2],&v)     && re_hex(zIn[3],&v)     && re_hex(zIn[4],&v)    ){      p->sIn.i += 5;      return v;    }  }  if( c=='x' && p->sIn.i+2<p->sIn.mx ){    const unsigned char *zIn = p->sIn.z + p->sIn.i;    if( re_hex(zIn[1],&v)     && re_hex(zIn[2],&v)    ){      p->sIn.i += 3;      return v;    }  }  for(i=0; zEsc[i] && zEsc[i]!=c; i++){}  if( zEsc[i] ){    if( i<6 ) c = zTrans[i];    p->sIn.i++;  }else{    p->zErr = "unknown \\ escape";  }  return c;}static const char *re_subcompile_string(ReCompiled*);static unsigned char rePeek(ReCompiled *p){  return p->sIn.i<p->sIn.mx ? p->sIn.z[p->sIn.i] : 0;}static const char *re_subcompile_re(ReCompiled *p){  const char *zErr;  int iStart, iEnd, iGoto;  iStart = p->nState;  zErr = re_subcompile_string(p);  if( zErr ) return zErr;  while( rePeek(p)=='|' ){    iEnd = p->nState;    re_insert(p, iStart, RE_OP_FORK, iEnd + 2 - iStart);    iGoto = re_append(p, RE_OP_GOTO, 0);    p->sIn.i++;    zErr = re_subcompile_string(p);    if( zErr ) return zErr;    p->aArg[iGoto] = p->nState - iGoto;  }  return 0;}static const char *re_subcompile_string(ReCompiled *p){  int iPrev = -1;  int iStart;  unsigned c;  const char *zErr;  while( (c = p->xNextChar(&p->sIn))!=0 ){    iStart = p->nState;    switch( c ){      case '|':      case '$':      case ')': {        p->sIn.i--;        return 0;      }      case '(': {        zErr = re_subcompile_re(p);        if( zErr ) return zErr;        if( rePeek(p)!=')' ) return "unmatched '('";        p->sIn.i++;        break;      }      case '.': {        if( rePeek(p)=='*' ){          re_append(p, RE_OP_ANYSTAR, 0);          p->sIn.i++;        }else{          re_append(p, RE_OP_ANY, 0);        }        break;      }      case '*': {        if( iPrev<0 ) return "'*' without operand";        re_insert(p, iPrev, RE_OP_GOTO, p->nState - iPrev + 1);        re_append(p, RE_OP_FORK, iPrev - p->nState + 1);        break;      }      case '+': {        if( iPrev<0 ) return "'+' without operand";        re_append(p, RE_OP_FORK, iPrev - p->nState);        break;      }      case '?': {        if( iPrev<0 ) return "'?' without operand";        re_insert(p, iPrev, RE_OP_FORK, p->nState - iPrev+1);        break;      }      case '{': {        int m = 0, n = 0;        int sz, j;        if( iPrev<0 ) return "'{m,n}' without operand";        while( (c=rePeek(p))>='0' && c<='9' ){ m = m*10 + c - '0'; p->sIn.i++; }        n = m;        if( c==',' ){          p->sIn.i++;          n = 0;          while( (c=rePeek(p))>='0' && c<='9' ){ n = n*10 + c-'0'; p->sIn.i++; }        }        if( c!='}' ) return "unmatched '{'";        if( n>0 && n<m ) return "n less than m in '{m,n}'";        p->sIn.i++;        sz = p->nState - iPrev;        if( m==0 ){          if( n==0 ) return "both m and n are zero in '{m,n}'";          re_insert(p, iPrev, RE_OP_FORK, sz+1);          n--;        }else{          for(j=1; j<m; j++) re_copy(p, iPrev, sz);        }        for(j=m; j<n; j++){          re_append(p, RE_OP_FORK, sz+1);          re_copy(p, iPrev, sz);        }        if( n==0 && m>0 ){          re_append(p, RE_OP_FORK, -sz);        }        break;      }      case '[': {        int iFirst = p->nState;        if( rePeek(p)=='^' ){          re_append(p, RE_OP_CC_EXC, 0);          p->sIn.i++;        }else{          re_append(p, RE_OP_CC_INC, 0);        }        while( (c = p->xNextChar(&p->sIn))!=0 ){          if( c=='[' && rePeek(p)==':' ){            return "POSIX character classes not supported";          }          if( c=='\\' ) c = re_esc_char(p);          if( rePeek(p)=='-' ){            re_append(p, RE_OP_CC_RANGE, c);            p->sIn.i++;            c = p->xNextChar(&p->sIn);            if( c=='\\' ) c = re_esc_char(p);            re_append(p, RE_OP_CC_RANGE, c);          }else{            re_append(p, RE_OP_CC_VALUE, c);          }          if( rePeek(p)==']' ){ p->sIn.i++; break; }        }        if( c==0 ) return "unclosed '['";        p->aArg[iFirst] = p->nState - iFirst;        break;      }      case '\\': {        int specialOp = 0;        switch( rePeek(p) ){          case 'b': specialOp = RE_OP_BOUNDARY;   break;          case 'd': specialOp = RE_OP_DIGIT;      break;          case 'D': specialOp = RE_OP_NOTDIGIT;   break;          case 's': specialOp = RE_OP_SPACE;      break;          case 'S': specialOp = RE_OP_NOTSPACE;   break;          case 'w': specialOp = RE_OP_WORD;       break;          case 'W': specialOp = RE_OP_NOTWORD;    break;        }        if( specialOp ){          p->sIn.i++;          re_append(p, specialOp, 0);        }else{          c = re_esc_char(p);          re_append(p, RE_OP_MATCH, c);        }        break;      }      default: {        re_append(p, RE_OP_MATCH, c);        break;      }    }    iPrev = iStart;  }  return 0;}static void re_free(ReCompiled *pRe){  if( pRe ){    sqlite3_free(pRe->aOp);    sqlite3_free(pRe->aArg);    sqlite3_free(pRe);  }}static const char *re_compile(ReCompiled **ppRe, const char *zIn, int noCase){  ReCompiled *pRe;  const char *zErr;  int i, j;  *ppRe = 0;  pRe = sqlite3_malloc( sizeof(*pRe) );  if( pRe==0 ){    return "out of memory";  }  memset(pRe, 0, sizeof(*pRe));  pRe->xNextChar = noCase ? re_next_char_nocase : re_next_char;  if( re_resize(pRe, 30) ){    re_free(pRe);    return "out of memory";  }  if( zIn[0]=='^' ){    zIn++;  }else{    re_append(pRe, RE_OP_ANYSTAR, 0);  }  pRe->sIn.z = (unsigned char*)zIn;  pRe->sIn.i = 0;  pRe->sIn.mx = (int)strlen(zIn);  zErr = re_subcompile_re(pRe);  if( zErr ){    re_free(pRe);    return zErr;  }  if( rePeek(pRe)=='$' && pRe->sIn.i+1>=pRe->sIn.mx ){    re_append(pRe, RE_OP_MATCH, RE_EOF);    re_append(pRe, RE_OP_ACCEPT, 0);    *ppRe = pRe;  }else if( pRe->sIn.i>=pRe->sIn.mx ){    re_append(pRe, RE_OP_ACCEPT, 0);    *ppRe = pRe;  }else{    re_free(pRe);    return "unrecognized character";  }    if( pRe->aOp[0]==RE_OP_ANYSTAR && !noCase ){    for(j=0, i=1; j<(int)sizeof(pRe->zInit)-2 && pRe->aOp[i]==RE_OP_MATCH; i++){      unsigned x = pRe->aArg[i];      if( x<=127 ){        pRe->zInit[j++] = (unsigned char)x;      }else if( x<=0xfff ){        pRe->zInit[j++] = (unsigned char)(0xc0 | (x>>6));        pRe->zInit[j++] = 0x80 | (x&0x3f);      }else if( x<=0xffff ){        pRe->zInit[j++] = (unsigned char)(0xd0 | (x>>12));        pRe->zInit[j++] = 0x80 | ((x>>6)&0x3f);        pRe->zInit[j++] = 0x80 | (x&0x3f);      }else{        break;      }    }    if( j>0 && pRe->zInit[j-1]==0 ) j--;    pRe->nInit = j;  }  return pRe->zErr;}static void re_sql_func(  sqlite3_context *context,  int argc,  sqlite3_value **argv){  ReCompiled *pRe;            const char *zPattern;       const unsigned char *zStr;  const char *zErr;           int setAux = 0;             (void)argc;    pRe = sqlite3_get_auxdata(context, 0);  if( pRe==0 ){    zPattern = (const char*)sqlite3_value_text(argv[0]);    if( zPattern==0 ) return;    zErr = re_compile(&pRe, zPattern, sqlite3_user_data(context)!=0);    if( zErr ){      re_free(pRe);      sqlite3_result_error(context, zErr, -1);      return;    }    if( pRe==0 ){      sqlite3_result_error_nomem(context);      return;    }    setAux = 1;  }  zStr = (const unsigned char*)sqlite3_value_text(argv[1]);  if( zStr!=0 ){    sqlite3_result_int(context, re_match(pRe, zStr, -1));  }  if( setAux ){    sqlite3_set_auxdata(context, 0, pRe, (void(*)(void*))re_free);  }}#ifdef _WIN32#endifint sqlite3_regexp_init(  sqlite3 *db,   char **pzErrMsg,   const sqlite3_api_routines *pApi){  int rc = SQLITE_OK;  SQLITE_EXTENSION_INIT2(pApi);  (void)pzErrMsg;    rc = sqlite3_create_function(db, "regexp", 2,                             SQLITE_UTF8|SQLITE_INNOCUOUS|SQLITE_DETERMINISTIC,                            0, re_sql_func, 0, 0);  if( rc==SQLITE_OK ){        rc = sqlite3_create_function(db, "regexpi", 2,                            SQLITE_UTF8|SQLITE_INNOCUOUS|SQLITE_DETERMINISTIC,                            (void*)db, re_sql_func, 0, 0);  }  return rc;}#ifdef SQLITE_HAVE_ZLIBSQLITE_EXTENSION_INIT1#include <stdio.h>#include <string.h>#include <assert.h>#include <zlib.h>#ifndef SQLITE_OMIT_VIRTUALTABLE#ifndef SQLITE_AMALGAMATION#ifndef UINT32_TYPE# ifdef HAVE_UINT32_T#  define UINT32_TYPE uint32_t# else#  define UINT32_TYPE unsigned int# endif#endif#ifndef UINT16_TYPE# ifdef HAVE_UINT16_T#  define UINT16_TYPE uint16_t# else#  define UINT16_TYPE unsigned short int# endif#endiftypedef UINT32_TYPE u32;           typedef UINT16_TYPE u16;           #define MIN(a,b) ((a)<(b) ? (a) : (b))#if defined(SQLITE_COVERAGE_TEST) || defined(SQLITE_MUTATION_TEST)# define SQLITE_OMIT_AUXILIARY_SAFETY_CHECKS 1#endif#if defined(SQLITE_OMIT_AUXILIARY_SAFETY_CHECKS)# define ALWAYS(X)      (1)# define NEVER(X)       (0)#elif !defined(NDEBUG)# define ALWAYS(X)      ((X)?1:(assert(0),0))# define NEVER(X)       ((X)?(assert(0),1):0)#else# define ALWAYS(X)      (X)# define NEVER(X)       (X)#endif#endif   #ifndef S_IFDIR# define S_IFDIR 0040000#endif#ifndef S_IFREG# define S_IFREG 0100000#endif#ifndef S_IFLNK# define S_IFLNK 0120000#endifstatic const char ZIPFILE_SCHEMA[] =   "CREATE TABLE y("    "name PRIMARY KEY,"      "mode,"                  "mtime,"                 "sz,"                    "rawdata,"               "data,"                  "method,"                "z HIDDEN"             ") WITHOUT ROWID;";#define ZIPFILE_F_COLUMN_IDX 7    #define ZIPFILE_BUFFER_SIZE (64*1024)#define ZIPFILE_EXTRA_TIMESTAMP   0x5455#define ZIPFILE_NEWENTRY_MADEBY   ((3<<8) + 30)#define ZIPFILE_NEWENTRY_REQUIRED 20#define ZIPFILE_NEWENTRY_FLAGS    0x800#define ZIPFILE_SIGNATURE_CDS     0x02014b50#define ZIPFILE_SIGNATURE_LFH     0x04034b50#define ZIPFILE_SIGNATURE_EOCD    0x06054b50#define ZIPFILE_LFH_FIXED_SZ      30#define ZIPFILE_EOCD_FIXED_SZ     22#define ZIPFILE_CDS_FIXED_SZ      46typedef struct ZipfileEOCD ZipfileEOCD;struct ZipfileEOCD {  u16 iDisk;  u16 iFirstDisk;  u16 nEntry;  u16 nEntryTotal;  u32 nSize;  u32 iOffset;};typedef struct ZipfileCDS ZipfileCDS;struct ZipfileCDS {  u16 iVersionMadeBy;  u16 iVersionExtract;  u16 flags;  u16 iCompression;  u16 mTime;  u16 mDate;  u32 crc32;  u32 szCompressed;  u32 szUncompressed;  u16 nFile;  u16 nExtra;  u16 nComment;  u16 iDiskStart;  u16 iInternalAttr;  u32 iExternalAttr;  u32 iOffset;  char *zFile;                    };typedef struct ZipfileLFH ZipfileLFH;struct ZipfileLFH {  u16 iVersionExtract;  u16 flags;  u16 iCompression;  u16 mTime;  u16 mDate;  u32 crc32;  u32 szCompressed;  u32 szUncompressed;  u16 nFile;  u16 nExtra;};typedef struct ZipfileEntry ZipfileEntry;struct ZipfileEntry {  ZipfileCDS cds;              u32 mUnixTime;               u8 *aExtra;                  i64 iDataOff;                u8 *aData;                   ZipfileEntry *pNext;       };typedef struct ZipfileCsr ZipfileCsr;struct ZipfileCsr {  sqlite3_vtab_cursor base;    i64 iId;                     u8 bEof;                     u8 bNoop;                      FILE *pFile;                 i64 iNextOff;                ZipfileEOCD eocd;            ZipfileEntry *pFreeEntry;    ZipfileEntry *pCurrent;      ZipfileCsr *pCsrNext;      };typedef struct ZipfileTab ZipfileTab;struct ZipfileTab {  sqlite3_vtab base;           char *zFile;                 sqlite3 *db;                 u8 *aBuffer;                 ZipfileCsr *pCsrList;        i64 iNextCsrid;    ZipfileEntry *pFirstEntry;   ZipfileEntry *pLastEntry;    FILE *pWriteFd;              i64 szCurrent;               i64 szOrig;                };static void zipfileCtxErrorMsg(sqlite3_context *ctx, const char *zFmt, ...){  char *zMsg = 0;  va_list ap;  va_start(ap, zFmt);  zMsg = sqlite3_vmprintf(zFmt, ap);  sqlite3_result_error(ctx, zMsg, -1);  sqlite3_free(zMsg);  va_end(ap);}static void zipfileDequote(char *zIn){  char q = zIn[0];  if( q=='"' || q=='\'' || q=='`' || q=='[' ){    int iIn = 1;    int iOut = 0;    if( q=='[' ) q = ']';    while( ALWAYS(zIn[iIn]) ){      char c = zIn[iIn++];      if( c==q && zIn[iIn++]!=q ) break;      zIn[iOut++] = c;    }    zIn[iOut] = '\0';  }}static int zipfileConnect(  sqlite3 *db,  void *pAux,  int argc, const char *const*argv,  sqlite3_vtab **ppVtab,  char **pzErr){  int nByte = sizeof(ZipfileTab) + ZIPFILE_BUFFER_SIZE;  int nFile = 0;  const char *zFile = 0;  ZipfileTab *pNew = 0;  int rc;    assert( 0==sqlite3_stricmp(argv[0], "zipfile") );  if( (0!=sqlite3_stricmp(argv[2], "zipfile") && argc<4) || argc>4 ){    *pzErr = sqlite3_mprintf("zipfile constructor requires one argument");    return SQLITE_ERROR;  }  if( argc>3 ){    zFile = argv[3];    nFile = (int)strlen(zFile)+1;  }  rc = sqlite3_declare_vtab(db, ZIPFILE_SCHEMA);  if( rc==SQLITE_OK ){    pNew = (ZipfileTab*)sqlite3_malloc64((sqlite3_int64)nByte+nFile);    if( pNew==0 ) return SQLITE_NOMEM;    memset(pNew, 0, nByte+nFile);    pNew->db = db;    pNew->aBuffer = (u8*)&pNew[1];    if( zFile ){      pNew->zFile = (char*)&pNew->aBuffer[ZIPFILE_BUFFER_SIZE];      memcpy(pNew->zFile, zFile, nFile);      zipfileDequote(pNew->zFile);    }  }  sqlite3_vtab_config(db, SQLITE_VTAB_DIRECTONLY);  *ppVtab = (sqlite3_vtab*)pNew;  return rc;}static void zipfileEntryFree(ZipfileEntry *p){  if( p ){    sqlite3_free(p->cds.zFile);    sqlite3_free(p);  }}static void zipfileCleanupTransaction(ZipfileTab *pTab){  ZipfileEntry *pEntry;  ZipfileEntry *pNext;  if( pTab->pWriteFd ){    fclose(pTab->pWriteFd);    pTab->pWriteFd = 0;  }  for(pEntry=pTab->pFirstEntry; pEntry; pEntry=pNext){    pNext = pEntry->pNext;    zipfileEntryFree(pEntry);  }  pTab->pFirstEntry = 0;  pTab->pLastEntry = 0;  pTab->szCurrent = 0;  pTab->szOrig = 0;}static int zipfileDisconnect(sqlite3_vtab *pVtab){  zipfileCleanupTransaction((ZipfileTab*)pVtab);  sqlite3_free(pVtab);  return SQLITE_OK;}static int zipfileOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCsr){  ZipfileTab *pTab = (ZipfileTab*)p;  ZipfileCsr *pCsr;  pCsr = sqlite3_malloc(sizeof(*pCsr));  *ppCsr = (sqlite3_vtab_cursor*)pCsr;  if( pCsr==0 ){    return SQLITE_NOMEM;  }  memset(pCsr, 0, sizeof(*pCsr));  pCsr->iId = ++pTab->iNextCsrid;  pCsr->pCsrNext = pTab->pCsrList;  pTab->pCsrList = pCsr;  return SQLITE_OK;}static void zipfileResetCursor(ZipfileCsr *pCsr){  ZipfileEntry *p;  ZipfileEntry *pNext;  pCsr->bEof = 0;  if( pCsr->pFile ){    fclose(pCsr->pFile);    pCsr->pFile = 0;    zipfileEntryFree(pCsr->pCurrent);    pCsr->pCurrent = 0;  }  for(p=pCsr->pFreeEntry; p; p=pNext){    pNext = p->pNext;    zipfileEntryFree(p);  }}static int zipfileClose(sqlite3_vtab_cursor *cur){  ZipfileCsr *pCsr = (ZipfileCsr*)cur;  ZipfileTab *pTab = (ZipfileTab*)(pCsr->base.pVtab);  ZipfileCsr **pp;  zipfileResetCursor(pCsr);    for(pp=&pTab->pCsrList; *pp!=pCsr; pp=&((*pp)->pCsrNext));  *pp = pCsr->pCsrNext;  sqlite3_free(pCsr);  return SQLITE_OK;}static void zipfileTableErr(ZipfileTab *pTab, const char *zFmt, ...){  va_list ap;  va_start(ap, zFmt);  sqlite3_free(pTab->base.zErrMsg);  pTab->base.zErrMsg = sqlite3_vmprintf(zFmt, ap);  va_end(ap);}static void zipfileCursorErr(ZipfileCsr *pCsr, const char *zFmt, ...){  va_list ap;  va_start(ap, zFmt);  sqlite3_free(pCsr->base.pVtab->zErrMsg);  pCsr->base.pVtab->zErrMsg = sqlite3_vmprintf(zFmt, ap);  va_end(ap);}static int zipfileReadData(  FILE *pFile,                      u8 *aRead,                        int nRead,                        i64 iOff,                         char **pzErrmsg                 ){  size_t n;  fseek(pFile, (long)iOff, SEEK_SET);  n = fread(aRead, 1, nRead, pFile);  if( (int)n!=nRead ){    *pzErrmsg = sqlite3_mprintf("error in fread()");    return SQLITE_ERROR;  }  return SQLITE_OK;}static int zipfileAppendData(  ZipfileTab *pTab,  const u8 *aWrite,  int nWrite){  if( nWrite>0 ){    size_t n = nWrite;    fseek(pTab->pWriteFd, (long)pTab->szCurrent, SEEK_SET);    n = fwrite(aWrite, 1, nWrite, pTab->pWriteFd);    if( (int)n!=nWrite ){      pTab->base.zErrMsg = sqlite3_mprintf("error in fwrite()");      return SQLITE_ERROR;    }    pTab->szCurrent += nWrite;  }  return SQLITE_OK;}static u16 zipfileGetU16(const u8 *aBuf){  return (aBuf[1] << 8) + aBuf[0];}static u32 zipfileGetU32(const u8 *aBuf){  if( aBuf==0 ) return 0;  return ((u32)(aBuf[3]) << 24)       + ((u32)(aBuf[2]) << 16)       + ((u32)(aBuf[1]) <<  8)       + ((u32)(aBuf[0]) <<  0);}static void zipfilePutU16(u8 *aBuf, u16 val){  aBuf[0] = val & 0xFF;  aBuf[1] = (val>>8) & 0xFF;}static void zipfilePutU32(u8 *aBuf, u32 val){  aBuf[0] = val & 0xFF;  aBuf[1] = (val>>8) & 0xFF;  aBuf[2] = (val>>16) & 0xFF;  aBuf[3] = (val>>24) & 0xFF;}#define zipfileRead32(aBuf) ( aBuf+=4, zipfileGetU32(aBuf-4) )#define zipfileRead16(aBuf) ( aBuf+=2, zipfileGetU16(aBuf-2) )#define zipfileWrite32(aBuf,val) { zipfilePutU32(aBuf,val); aBuf+=4; }#define zipfileWrite16(aBuf,val) { zipfilePutU16(aBuf,val); aBuf+=2; }#define ZIPFILE_CDS_NFILE_OFF        28#define ZIPFILE_CDS_SZCOMPRESSED_OFF 20static int zipfileReadCDS(u8 *aBuf, ZipfileCDS *pCDS){  u8 *aRead = aBuf;  u32 sig = zipfileRead32(aRead);  int rc = SQLITE_OK;  if( sig!=ZIPFILE_SIGNATURE_CDS ){    rc = SQLITE_ERROR;  }else{    pCDS->iVersionMadeBy = zipfileRead16(aRead);    pCDS->iVersionExtract = zipfileRead16(aRead);    pCDS->flags = zipfileRead16(aRead);    pCDS->iCompression = zipfileRead16(aRead);    pCDS->mTime = zipfileRead16(aRead);    pCDS->mDate = zipfileRead16(aRead);    pCDS->crc32 = zipfileRead32(aRead);    pCDS->szCompressed = zipfileRead32(aRead);    pCDS->szUncompressed = zipfileRead32(aRead);    assert( aRead==&aBuf[ZIPFILE_CDS_NFILE_OFF] );    pCDS->nFile = zipfileRead16(aRead);    pCDS->nExtra = zipfileRead16(aRead);    pCDS->nComment = zipfileRead16(aRead);    pCDS->iDiskStart = zipfileRead16(aRead);    pCDS->iInternalAttr = zipfileRead16(aRead);    pCDS->iExternalAttr = zipfileRead32(aRead);    pCDS->iOffset = zipfileRead32(aRead);    assert( aRead==&aBuf[ZIPFILE_CDS_FIXED_SZ] );  }  return rc;}static int zipfileReadLFH(  u8 *aBuffer,  ZipfileLFH *pLFH){  u8 *aRead = aBuffer;  int rc = SQLITE_OK;  u32 sig = zipfileRead32(aRead);  if( sig!=ZIPFILE_SIGNATURE_LFH ){    rc = SQLITE_ERROR;  }else{    pLFH->iVersionExtract = zipfileRead16(aRead);    pLFH->flags = zipfileRead16(aRead);    pLFH->iCompression = zipfileRead16(aRead);    pLFH->mTime = zipfileRead16(aRead);    pLFH->mDate = zipfileRead16(aRead);    pLFH->crc32 = zipfileRead32(aRead);    pLFH->szCompressed = zipfileRead32(aRead);    pLFH->szUncompressed = zipfileRead32(aRead);    pLFH->nFile = zipfileRead16(aRead);    pLFH->nExtra = zipfileRead16(aRead);  }  return rc;}static int zipfileScanExtra(u8 *aExtra, int nExtra, u32 *pmTime){  int ret = 0;  u8 *p = aExtra;  u8 *pEnd = &aExtra[nExtra];  while( p<pEnd ){    u16 id = zipfileRead16(p);    u16 nByte = zipfileRead16(p);    switch( id ){      case ZIPFILE_EXTRA_TIMESTAMP: {        u8 b = p[0];        if( b & 0x01 ){               *pmTime = zipfileGetU32(&p[1]);          ret = 1;        }        break;      }    }    p += nByte;  }  return ret;}static u32 zipfileMtime(ZipfileCDS *pCDS){  int Y,M,D,X1,X2,A,B,sec,min,hr;  i64 JDsec;  Y = (1980 + ((pCDS->mDate >> 9) & 0x7F));  M = ((pCDS->mDate >> 5) & 0x0F);  D = (pCDS->mDate & 0x1F);  sec = (pCDS->mTime & 0x1F)*2;  min = (pCDS->mTime >> 5) & 0x3F;  hr = (pCDS->mTime >> 11) & 0x1F;  if( M<=2 ){    Y--;    M += 12;  }  X1 = 36525*(Y+4716)/100;  X2 = 306001*(M+1)/10000;  A = Y/100;  B = 2 - A + (A/4);  JDsec = (i64)((X1 + X2 + D + B - 1524.5)*86400) + hr*3600 + min*60 + sec;  return (u32)(JDsec - (i64)24405875*(i64)8640);}static void zipfileMtimeToDos(ZipfileCDS *pCds, u32 mUnixTime){    i64 JD = (i64)2440588 + mUnixTime / (24*60*60);  int A, B, C, D, E;  int yr, mon, day;  int hr, min, sec;  A = (int)((JD - 1867216.25)/36524.25);  A = (int)(JD + 1 + A - (A/4));  B = A + 1524;  C = (int)((B - 122.1)/365.25);  D = (36525*(C&32767))/100;  E = (int)((B-D)/30.6001);  day = B - D - (int)(30.6001*E);  mon = (E<14 ? E-1 : E-13);  yr = mon>2 ? C-4716 : C-4715;  hr = (mUnixTime % (24*60*60)) / (60*60);  min = (mUnixTime % (60*60)) / 60;  sec = (mUnixTime % 60);  if( yr>=1980 ){    pCds->mDate = (u16)(day + (mon << 5) + ((yr-1980) << 9));    pCds->mTime = (u16)(sec/2 + (min<<5) + (hr<<11));  }else{    pCds->mDate = pCds->mTime = 0;  }  assert( mUnixTime<315507600        || mUnixTime==zipfileMtime(pCds)        || ((mUnixTime % 2) && mUnixTime-1==zipfileMtime(pCds))          );}static int zipfileGetEntry(  ZipfileTab *pTab,                 const u8 *aBlob,                  int nBlob,                        FILE *pFile,                      i64 iOff,                         ZipfileEntry **ppEntry          ){  u8 *aRead;  char **pzErr = &pTab->base.zErrMsg;  int rc = SQLITE_OK;  if( aBlob==0 ){    aRead = pTab->aBuffer;    rc = zipfileReadData(pFile, aRead, ZIPFILE_CDS_FIXED_SZ, iOff, pzErr);  }else{    aRead = (u8*)&aBlob[iOff];  }  if( rc==SQLITE_OK ){    sqlite3_int64 nAlloc;    ZipfileEntry *pNew;    int nFile = zipfileGetU16(&aRead[ZIPFILE_CDS_NFILE_OFF]);    int nExtra = zipfileGetU16(&aRead[ZIPFILE_CDS_NFILE_OFF+2]);    nExtra += zipfileGetU16(&aRead[ZIPFILE_CDS_NFILE_OFF+4]);    nAlloc = sizeof(ZipfileEntry) + nExtra;    if( aBlob ){      nAlloc += zipfileGetU32(&aRead[ZIPFILE_CDS_SZCOMPRESSED_OFF]);    }    pNew = (ZipfileEntry*)sqlite3_malloc64(nAlloc);    if( pNew==0 ){      rc = SQLITE_NOMEM;    }else{      memset(pNew, 0, sizeof(ZipfileEntry));      rc = zipfileReadCDS(aRead, &pNew->cds);      if( rc!=SQLITE_OK ){        *pzErr = sqlite3_mprintf("failed to read CDS at offset %lld", iOff);      }else if( aBlob==0 ){        rc = zipfileReadData(            pFile, aRead, nExtra+nFile, iOff+ZIPFILE_CDS_FIXED_SZ, pzErr        );      }else{        aRead = (u8*)&aBlob[iOff + ZIPFILE_CDS_FIXED_SZ];      }    }    if( rc==SQLITE_OK ){      u32 *pt = &pNew->mUnixTime;      pNew->cds.zFile = sqlite3_mprintf("%.*s", nFile, aRead);       pNew->aExtra = (u8*)&pNew[1];      memcpy(pNew->aExtra, &aRead[nFile], nExtra);      if( pNew->cds.zFile==0 ){        rc = SQLITE_NOMEM;      }else if( 0==zipfileScanExtra(&aRead[nFile], pNew->cds.nExtra, pt) ){        pNew->mUnixTime = zipfileMtime(&pNew->cds);      }    }    if( rc==SQLITE_OK ){      static const int szFix = ZIPFILE_LFH_FIXED_SZ;      ZipfileLFH lfh;      if( pFile ){        rc = zipfileReadData(pFile, aRead, szFix, pNew->cds.iOffset, pzErr);      }else{        aRead = (u8*)&aBlob[pNew->cds.iOffset];      }      if( rc==SQLITE_OK ) rc = zipfileReadLFH(aRead, &lfh);      if( rc==SQLITE_OK ){        pNew->iDataOff =  pNew->cds.iOffset + ZIPFILE_LFH_FIXED_SZ;        pNew->iDataOff += lfh.nFile + lfh.nExtra;        if( aBlob && pNew->cds.szCompressed ){          pNew->aData = &pNew->aExtra[nExtra];          memcpy(pNew->aData, &aBlob[pNew->iDataOff], pNew->cds.szCompressed);        }      }else{        *pzErr = sqlite3_mprintf("failed to read LFH at offset %d",             (int)pNew->cds.iOffset        );      }    }    if( rc!=SQLITE_OK ){      zipfileEntryFree(pNew);    }else{      *ppEntry = pNew;    }  }  return rc;}static int zipfileNext(sqlite3_vtab_cursor *cur){  ZipfileCsr *pCsr = (ZipfileCsr*)cur;  int rc = SQLITE_OK;  if( pCsr->pFile ){    i64 iEof = pCsr->eocd.iOffset + pCsr->eocd.nSize;    zipfileEntryFree(pCsr->pCurrent);    pCsr->pCurrent = 0;    if( pCsr->iNextOff>=iEof ){      pCsr->bEof = 1;    }else{      ZipfileEntry *p = 0;      ZipfileTab *pTab = (ZipfileTab*)(cur->pVtab);      rc = zipfileGetEntry(pTab, 0, 0, pCsr->pFile, pCsr->iNextOff, &p);      if( rc==SQLITE_OK ){        pCsr->iNextOff += ZIPFILE_CDS_FIXED_SZ;        pCsr->iNextOff += (int)p->cds.nExtra + p->cds.nFile + p->cds.nComment;      }      pCsr->pCurrent = p;    }  }else{    if( !pCsr->bNoop ){      pCsr->pCurrent = pCsr->pCurrent->pNext;    }    if( pCsr->pCurrent==0 ){      pCsr->bEof = 1;    }  }  pCsr->bNoop = 0;  return rc;}static void zipfileFree(void *p) {   sqlite3_free(p); }static void zipfileInflate(  sqlite3_context *pCtx,            const u8 *aIn,                    int nIn,                          int nOut                        ){  u8 *aRes = sqlite3_malloc(nOut);  if( aRes==0 ){    sqlite3_result_error_nomem(pCtx);  }else{    int err;    z_stream str;    memset(&str, 0, sizeof(str));    str.next_in = (Byte*)aIn;    str.avail_in = nIn;    str.next_out = (Byte*)aRes;    str.avail_out = nOut;    err = inflateInit2(&str, -15);    if( err!=Z_OK ){      zipfileCtxErrorMsg(pCtx, "inflateInit2() failed (%d)", err);    }else{      err = inflate(&str, Z_NO_FLUSH);      if( err!=Z_STREAM_END ){        zipfileCtxErrorMsg(pCtx, "inflate() failed (%d)", err);      }else{        sqlite3_result_blob(pCtx, aRes, nOut, zipfileFree);        aRes = 0;      }    }    sqlite3_free(aRes);    inflateEnd(&str);  }}static int zipfileDeflate(  const u8 *aIn, int nIn,           u8 **ppOut, int *pnOut,           char **pzErr                    ){  int rc = SQLITE_OK;  sqlite3_int64 nAlloc;  z_stream str;  u8 *aOut;  memset(&str, 0, sizeof(str));  str.next_in = (Bytef*)aIn;  str.avail_in = nIn;  deflateInit2(&str, 9, Z_DEFLATED, -15, 8, Z_DEFAULT_STRATEGY);  nAlloc = deflateBound(&str, nIn);  aOut = (u8*)sqlite3_malloc64(nAlloc);  if( aOut==0 ){    rc = SQLITE_NOMEM;  }else{    int res;    str.next_out = aOut;    str.avail_out = nAlloc;    res = deflate(&str, Z_FINISH);    if( res==Z_STREAM_END ){      *ppOut = aOut;      *pnOut = (int)str.total_out;    }else{      sqlite3_free(aOut);      *pzErr = sqlite3_mprintf("zipfile: deflate() error");      rc = SQLITE_ERROR;    }    deflateEnd(&str);  }  return rc;}static int zipfileColumn(  sqlite3_vtab_cursor *cur,     sqlite3_context *ctx,         int i                       ){  ZipfileCsr *pCsr = (ZipfileCsr*)cur;  ZipfileCDS *pCDS = &pCsr->pCurrent->cds;  int rc = SQLITE_OK;  switch( i ){    case 0:         sqlite3_result_text(ctx, pCDS->zFile, -1, SQLITE_TRANSIENT);      break;    case 1:               sqlite3_result_int(ctx, pCDS->iExternalAttr >> 16);      break;    case 2: {       sqlite3_result_int64(ctx, pCsr->pCurrent->mUnixTime);      break;    }    case 3: {       if( sqlite3_vtab_nochange(ctx)==0 ){        sqlite3_result_int64(ctx, pCDS->szUncompressed);      }      break;    }    case 4:         if( sqlite3_vtab_nochange(ctx) ) break;    case 5: {       if( i==4 || pCDS->iCompression==0 || pCDS->iCompression==8 ){        int sz = pCDS->szCompressed;        int szFinal = pCDS->szUncompressed;        if( szFinal>0 ){          u8 *aBuf;          u8 *aFree = 0;          if( pCsr->pCurrent->aData ){            aBuf = pCsr->pCurrent->aData;          }else{            aBuf = aFree = sqlite3_malloc64(sz);            if( aBuf==0 ){              rc = SQLITE_NOMEM;            }else{              FILE *pFile = pCsr->pFile;              if( pFile==0 ){                pFile = ((ZipfileTab*)(pCsr->base.pVtab))->pWriteFd;              }              rc = zipfileReadData(pFile, aBuf, sz, pCsr->pCurrent->iDataOff,                  &pCsr->base.pVtab->zErrMsg              );            }          }          if( rc==SQLITE_OK ){            if( i==5 && pCDS->iCompression ){              zipfileInflate(ctx, aBuf, sz, szFinal);            }else{              sqlite3_result_blob(ctx, aBuf, sz, SQLITE_TRANSIENT);            }          }          sqlite3_free(aFree);        }else{                    u32 mode = pCDS->iExternalAttr >> 16;          if( !(mode & S_IFDIR) && pCDS->zFile[pCDS->nFile-1]!='/' ){            sqlite3_result_blob(ctx, "", 0, SQLITE_STATIC);          }        }      }      break;    }    case 6:         sqlite3_result_int(ctx, pCDS->iCompression);      break;    default:        assert( i==7 );      sqlite3_result_int64(ctx, pCsr->iId);      break;  }  return rc;}static int zipfileEof(sqlite3_vtab_cursor *cur){  ZipfileCsr *pCsr = (ZipfileCsr*)cur;  return pCsr->bEof;}static int zipfileReadEOCD(  ZipfileTab *pTab,                 const u8 *aBlob,                  int nBlob,                        FILE *pFile,                      ZipfileEOCD *pEOCD              ){  u8 *aRead = pTab->aBuffer;        int nRead;                        int rc = SQLITE_OK;  memset(pEOCD, 0, sizeof(ZipfileEOCD));  if( aBlob==0 ){    i64 iOff;                         i64 szFile;                       fseek(pFile, 0, SEEK_END);    szFile = (i64)ftell(pFile);    if( szFile==0 ){      return SQLITE_OK;    }    nRead = (int)(MIN(szFile, ZIPFILE_BUFFER_SIZE));    iOff = szFile - nRead;    rc = zipfileReadData(pFile, aRead, nRead, iOff, &pTab->base.zErrMsg);  }else{    nRead = (int)(MIN(nBlob, ZIPFILE_BUFFER_SIZE));    aRead = (u8*)&aBlob[nBlob-nRead];  }  if( rc==SQLITE_OK ){    int i;        for(i=nRead-20; i>=0; i--){      if( aRead[i]==0x50 && aRead[i+1]==0x4b        && aRead[i+2]==0x05 && aRead[i+3]==0x06       ){        break;      }    }    if( i<0 ){      pTab->base.zErrMsg = sqlite3_mprintf(          "cannot find end of central directory record"      );      return SQLITE_ERROR;    }    aRead += i+4;    pEOCD->iDisk = zipfileRead16(aRead);    pEOCD->iFirstDisk = zipfileRead16(aRead);    pEOCD->nEntry = zipfileRead16(aRead);    pEOCD->nEntryTotal = zipfileRead16(aRead);    pEOCD->nSize = zipfileRead32(aRead);    pEOCD->iOffset = zipfileRead32(aRead);  }  return rc;}static void zipfileAddEntry(  ZipfileTab *pTab,   ZipfileEntry *pBefore,   ZipfileEntry *pNew){  assert( (pTab->pFirstEntry==0)==(pTab->pLastEntry==0) );  assert( pNew->pNext==0 );  if( pBefore==0 ){    if( pTab->pFirstEntry==0 ){      pTab->pFirstEntry = pTab->pLastEntry = pNew;    }else{      assert( pTab->pLastEntry->pNext==0 );      pTab->pLastEntry->pNext = pNew;      pTab->pLastEntry = pNew;    }  }else{    ZipfileEntry **pp;    for(pp=&pTab->pFirstEntry; *pp!=pBefore; pp=&((*pp)->pNext));    pNew->pNext = pBefore;    *pp = pNew;  }}static int zipfileLoadDirectory(ZipfileTab *pTab, const u8 *aBlob, int nBlob){  ZipfileEOCD eocd;  int rc;  int i;  i64 iOff;  rc = zipfileReadEOCD(pTab, aBlob, nBlob, pTab->pWriteFd, &eocd);  iOff = eocd.iOffset;  for(i=0; rc==SQLITE_OK && i<eocd.nEntry; i++){    ZipfileEntry *pNew = 0;    rc = zipfileGetEntry(pTab, aBlob, nBlob, pTab->pWriteFd, iOff, &pNew);    if( rc==SQLITE_OK ){      zipfileAddEntry(pTab, 0, pNew);      iOff += ZIPFILE_CDS_FIXED_SZ;      iOff += (int)pNew->cds.nExtra + pNew->cds.nFile + pNew->cds.nComment;    }  }  return rc;}static int zipfileFilter(  sqlite3_vtab_cursor *cur,   int idxNum, const char *idxStr,  int argc, sqlite3_value **argv){  ZipfileTab *pTab = (ZipfileTab*)cur->pVtab;  ZipfileCsr *pCsr = (ZipfileCsr*)cur;  const char *zFile = 0;            int rc = SQLITE_OK;               int bInMemory = 0;                zipfileResetCursor(pCsr);  if( pTab->zFile ){    zFile = pTab->zFile;  }else if( idxNum==0 ){    zipfileCursorErr(pCsr, "zipfile() function requires an argument");    return SQLITE_ERROR;  }else if( sqlite3_value_type(argv[0])==SQLITE_BLOB ){    static const u8 aEmptyBlob = 0;    const u8 *aBlob = (const u8*)sqlite3_value_blob(argv[0]);    int nBlob = sqlite3_value_bytes(argv[0]);    assert( pTab->pFirstEntry==0 );    if( aBlob==0 ){      aBlob = &aEmptyBlob;      nBlob = 0;    }    rc = zipfileLoadDirectory(pTab, aBlob, nBlob);    pCsr->pFreeEntry = pTab->pFirstEntry;    pTab->pFirstEntry = pTab->pLastEntry = 0;    if( rc!=SQLITE_OK ) return rc;    bInMemory = 1;  }else{    zFile = (const char*)sqlite3_value_text(argv[0]);  }  if( 0==pTab->pWriteFd && 0==bInMemory ){    pCsr->pFile = fopen(zFile, "rb");    if( pCsr->pFile==0 ){      zipfileCursorErr(pCsr, "cannot open file: %s", zFile);      rc = SQLITE_ERROR;    }else{      rc = zipfileReadEOCD(pTab, 0, 0, pCsr->pFile, &pCsr->eocd);      if( rc==SQLITE_OK ){        if( pCsr->eocd.nEntry==0 ){          pCsr->bEof = 1;        }else{          pCsr->iNextOff = pCsr->eocd.iOffset;          rc = zipfileNext(cur);        }      }    }  }else{    pCsr->bNoop = 1;    pCsr->pCurrent = pCsr->pFreeEntry ? pCsr->pFreeEntry : pTab->pFirstEntry;    rc = zipfileNext(cur);  }  return rc;}static int zipfileBestIndex(  sqlite3_vtab *tab,  sqlite3_index_info *pIdxInfo){  int i;  int idx = -1;  int unusable = 0;  for(i=0; i<pIdxInfo->nConstraint; i++){    const struct sqlite3_index_constraint *pCons = &pIdxInfo->aConstraint[i];    if( pCons->iColumn!=ZIPFILE_F_COLUMN_IDX ) continue;    if( pCons->usable==0 ){      unusable = 1;    }else if( pCons->op==SQLITE_INDEX_CONSTRAINT_EQ ){      idx = i;    }  }  pIdxInfo->estimatedCost = 1000.0;  if( idx>=0 ){    pIdxInfo->aConstraintUsage[idx].argvIndex = 1;    pIdxInfo->aConstraintUsage[idx].omit = 1;    pIdxInfo->idxNum = 1;  }else if( unusable ){    return SQLITE_CONSTRAINT;  }  return SQLITE_OK;}static ZipfileEntry *zipfileNewEntry(const char *zPath){  ZipfileEntry *pNew;  pNew = sqlite3_malloc(sizeof(ZipfileEntry));  if( pNew ){    memset(pNew, 0, sizeof(ZipfileEntry));    pNew->cds.zFile = sqlite3_mprintf("%s", zPath);    if( pNew->cds.zFile==0 ){      sqlite3_free(pNew);      pNew = 0;    }  }  return pNew;}static int zipfileSerializeLFH(ZipfileEntry *pEntry, u8 *aBuf){  ZipfileCDS *pCds = &pEntry->cds;  u8 *a = aBuf;  pCds->nExtra = 9;    zipfileWrite32(a, ZIPFILE_SIGNATURE_LFH);  zipfileWrite16(a, pCds->iVersionExtract);  zipfileWrite16(a, pCds->flags);  zipfileWrite16(a, pCds->iCompression);  zipfileWrite16(a, pCds->mTime);  zipfileWrite16(a, pCds->mDate);  zipfileWrite32(a, pCds->crc32);  zipfileWrite32(a, pCds->szCompressed);  zipfileWrite32(a, pCds->szUncompressed);  zipfileWrite16(a, (u16)pCds->nFile);  zipfileWrite16(a, pCds->nExtra);  assert( a==&aBuf[ZIPFILE_LFH_FIXED_SZ] );    memcpy(a, pCds->zFile, (int)pCds->nFile);  a += (int)pCds->nFile;    zipfileWrite16(a, ZIPFILE_EXTRA_TIMESTAMP);  zipfileWrite16(a, 5);  *a++ = 0x01;  zipfileWrite32(a, pEntry->mUnixTime);  return a-aBuf;}static int zipfileAppendEntry(  ZipfileTab *pTab,  ZipfileEntry *pEntry,  const u8 *pData,  int nData){  u8 *aBuf = pTab->aBuffer;  int nBuf;  int rc;  nBuf = zipfileSerializeLFH(pEntry, aBuf);  rc = zipfileAppendData(pTab, aBuf, nBuf);  if( rc==SQLITE_OK ){    pEntry->iDataOff = pTab->szCurrent;    rc = zipfileAppendData(pTab, pData, nData);  }  return rc;}static int zipfileGetMode(  sqlite3_value *pVal,   int bIsDir,                       u32 *pMode,                       char **pzErr                    ){  const char *z = (const char*)sqlite3_value_text(pVal);  u32 mode = 0;  if( z==0 ){    mode = (bIsDir ? (S_IFDIR + 0755) : (S_IFREG + 0644));  }else if( z[0]>='0' && z[0]<='9' ){    mode = (unsigned int)sqlite3_value_int(pVal);  }else{    const char zTemplate[11] = "-rwxrwxrwx";    int i;    if( strlen(z)!=10 ) goto parse_error;    switch( z[0] ){      case '-': mode |= S_IFREG; break;      case 'd': mode |= S_IFDIR; break;      case 'l': mode |= S_IFLNK; break;      default: goto parse_error;    }    for(i=1; i<10; i++){      if( z[i]==zTemplate[i] ) mode |= 1 << (9-i);      else if( z[i]!='-' ) goto parse_error;    }  }  if( ((mode & S_IFDIR)==0)==bIsDir ){        *pzErr = sqlite3_mprintf("zipfile: mode does not match data");    return SQLITE_CONSTRAINT;  }  *pMode = mode;  return SQLITE_OK; parse_error:  *pzErr = sqlite3_mprintf("zipfile: parse error in mode: %s", z);  return SQLITE_ERROR;}static int zipfileComparePath(const char *zA, const char *zB, int nB){  int nA = (int)strlen(zA);  if( nA>0 && zA[nA-1]=='/' ) nA--;  if( nB>0 && zB[nB-1]=='/' ) nB--;  if( nA==nB && memcmp(zA, zB, nA)==0 ) return 0;  return 1;}static int zipfileBegin(sqlite3_vtab *pVtab){  ZipfileTab *pTab = (ZipfileTab*)pVtab;  int rc = SQLITE_OK;  assert( pTab->pWriteFd==0 );  if( pTab->zFile==0 || pTab->zFile[0]==0 ){    pTab->base.zErrMsg = sqlite3_mprintf("zipfile: missing filename");    return SQLITE_ERROR;  }    pTab->pWriteFd = fopen(pTab->zFile, "ab+");  if( pTab->pWriteFd==0 ){    pTab->base.zErrMsg = sqlite3_mprintf(        "zipfile: failed to open file %s for writing", pTab->zFile        );    rc = SQLITE_ERROR;  }else{    fseek(pTab->pWriteFd, 0, SEEK_END);    pTab->szCurrent = pTab->szOrig = (i64)ftell(pTab->pWriteFd);    rc = zipfileLoadDirectory(pTab, 0, 0);  }  if( rc!=SQLITE_OK ){    zipfileCleanupTransaction(pTab);  }  return rc;}static u32 zipfileTime(void){  sqlite3_vfs *pVfs = sqlite3_vfs_find(0);  u32 ret;  if( pVfs==0 ) return 0;  if( pVfs->iVersion>=2 && pVfs->xCurrentTimeInt64 ){    i64 ms;    pVfs->xCurrentTimeInt64(pVfs, &ms);    ret = (u32)((ms/1000) - ((i64)24405875 * 8640));  }else{    double day;    pVfs->xCurrentTime(pVfs, &day);    ret = (u32)((day - 2440587.5) * 86400);  }  return ret;}static u32 zipfileGetTime(sqlite3_value *pVal){  if( pVal==0 || sqlite3_value_type(pVal)==SQLITE_NULL ){    return zipfileTime();  }  return (u32)sqlite3_value_int64(pVal);}static void zipfileRemoveEntryFromList(ZipfileTab *pTab, ZipfileEntry *pOld){  if( pOld ){    ZipfileEntry **pp;    for(pp=&pTab->pFirstEntry; (*pp)!=pOld; pp=&((*pp)->pNext));    *pp = (*pp)->pNext;    zipfileEntryFree(pOld);  }}static int zipfileUpdate(  sqlite3_vtab *pVtab,   int nVal,   sqlite3_value **apVal,   sqlite_int64 *pRowid){  ZipfileTab *pTab = (ZipfileTab*)pVtab;  int rc = SQLITE_OK;               ZipfileEntry *pNew = 0;           u32 mode = 0;                     u32 mTime = 0;                    i64 sz = 0;                       const char *zPath = 0;            int nPath = 0;                    const u8 *pData = 0;              int nData = 0;                    int iMethod = 0;                  u8 *pFree = 0;                    char *zFree = 0;                  ZipfileEntry *pOld = 0;  ZipfileEntry *pOld2 = 0;  int bUpdate = 0;                  int bIsDir = 0;  u32 iCrc32 = 0;  if( pTab->pWriteFd==0 ){    rc = zipfileBegin(pVtab);    if( rc!=SQLITE_OK ) return rc;  }    if( sqlite3_value_type(apVal[0])!=SQLITE_NULL ){    const char *zDelete = (const char*)sqlite3_value_text(apVal[0]);    int nDelete = (int)strlen(zDelete);    if( nVal>1 ){      const char *zUpdate = (const char*)sqlite3_value_text(apVal[1]);      if( zUpdate && zipfileComparePath(zUpdate, zDelete, nDelete)!=0 ){        bUpdate = 1;      }    }    for(pOld=pTab->pFirstEntry; 1; pOld=pOld->pNext){      if( zipfileComparePath(pOld->cds.zFile, zDelete, nDelete)==0 ){        break;      }      assert( pOld->pNext );    }  }  if( nVal>1 ){        if( sqlite3_value_type(apVal[5])!=SQLITE_NULL ){      zipfileTableErr(pTab, "sz must be NULL");      rc = SQLITE_CONSTRAINT;    }    if( sqlite3_value_type(apVal[6])!=SQLITE_NULL ){      zipfileTableErr(pTab, "rawdata must be NULL");       rc = SQLITE_CONSTRAINT;    }    if( rc==SQLITE_OK ){      if( sqlite3_value_type(apVal[7])==SQLITE_NULL ){                bIsDir = 1;      }else{                const u8 *aIn = sqlite3_value_blob(apVal[7]);        int nIn = sqlite3_value_bytes(apVal[7]);        int bAuto = sqlite3_value_type(apVal[8])==SQLITE_NULL;        iMethod = sqlite3_value_int(apVal[8]);        sz = nIn;        pData = aIn;        nData = nIn;        if( iMethod!=0 && iMethod!=8 ){          zipfileTableErr(pTab, "unknown compression method: %d", iMethod);          rc = SQLITE_CONSTRAINT;        }else{          if( bAuto || iMethod ){            int nCmp;            rc = zipfileDeflate(aIn, nIn, &pFree, &nCmp, &pTab->base.zErrMsg);            if( rc==SQLITE_OK ){              if( iMethod || nCmp<nIn ){                iMethod = 8;                pData = pFree;                nData = nCmp;              }            }          }          iCrc32 = crc32(0, aIn, nIn);        }      }    }    if( rc==SQLITE_OK ){      rc = zipfileGetMode(apVal[3], bIsDir, &mode, &pTab->base.zErrMsg);    }    if( rc==SQLITE_OK ){      zPath = (const char*)sqlite3_value_text(apVal[2]);      if( zPath==0 ) zPath = "";      nPath = (int)strlen(zPath);      mTime = zipfileGetTime(apVal[4]);    }    if( rc==SQLITE_OK && bIsDir ){            if( nPath<=0 || zPath[nPath-1]!='/' ){        zFree = sqlite3_mprintf("%s/", zPath);        zPath = (const char*)zFree;        if( zFree==0 ){          rc = SQLITE_NOMEM;          nPath = 0;        }else{          nPath = (int)strlen(zPath);        }      }    }        if( (pOld==0 || bUpdate) && rc==SQLITE_OK ){      ZipfileEntry *p;      for(p=pTab->pFirstEntry; p; p=p->pNext){        if( zipfileComparePath(p->cds.zFile, zPath, nPath)==0 ){          switch( sqlite3_vtab_on_conflict(pTab->db) ){            case SQLITE_IGNORE: {              goto zipfile_update_done;            }            case SQLITE_REPLACE: {              pOld2 = p;              break;            }            default: {              zipfileTableErr(pTab, "duplicate name: \"%s\"", zPath);              rc = SQLITE_CONSTRAINT;              break;            }          }          break;        }      }    }    if( rc==SQLITE_OK ){            pNew = zipfileNewEntry(zPath);      if( pNew==0 ){        rc = SQLITE_NOMEM;      }else{        pNew->cds.iVersionMadeBy = ZIPFILE_NEWENTRY_MADEBY;        pNew->cds.iVersionExtract = ZIPFILE_NEWENTRY_REQUIRED;        pNew->cds.flags = ZIPFILE_NEWENTRY_FLAGS;        pNew->cds.iCompression = (u16)iMethod;        zipfileMtimeToDos(&pNew->cds, mTime);        pNew->cds.crc32 = iCrc32;        pNew->cds.szCompressed = nData;        pNew->cds.szUncompressed = (u32)sz;        pNew->cds.iExternalAttr = (mode<<16);        pNew->cds.iOffset = (u32)pTab->szCurrent;        pNew->cds.nFile = (u16)nPath;        pNew->mUnixTime = (u32)mTime;        rc = zipfileAppendEntry(pTab, pNew, pData, nData);        zipfileAddEntry(pTab, pOld, pNew);      }    }  }  if( rc==SQLITE_OK && (pOld || pOld2) ){    ZipfileCsr *pCsr;    for(pCsr=pTab->pCsrList; pCsr; pCsr=pCsr->pCsrNext){      if( pCsr->pCurrent && (pCsr->pCurrent==pOld || pCsr->pCurrent==pOld2) ){        pCsr->pCurrent = pCsr->pCurrent->pNext;        pCsr->bNoop = 1;      }    }    zipfileRemoveEntryFromList(pTab, pOld);    zipfileRemoveEntryFromList(pTab, pOld2);  }zipfile_update_done:  sqlite3_free(pFree);  sqlite3_free(zFree);  return rc;}static int zipfileSerializeEOCD(ZipfileEOCD *p, u8 *aBuf){  u8 *a = aBuf;  zipfileWrite32(a, ZIPFILE_SIGNATURE_EOCD);  zipfileWrite16(a, p->iDisk);  zipfileWrite16(a, p->iFirstDisk);  zipfileWrite16(a, p->nEntry);  zipfileWrite16(a, p->nEntryTotal);  zipfileWrite32(a, p->nSize);  zipfileWrite32(a, p->iOffset);  zipfileWrite16(a, 0);          return a-aBuf;}static int zipfileAppendEOCD(ZipfileTab *pTab, ZipfileEOCD *p){  int nBuf = zipfileSerializeEOCD(p, pTab->aBuffer);  assert( nBuf==ZIPFILE_EOCD_FIXED_SZ );  return zipfileAppendData(pTab, pTab->aBuffer, nBuf);}static int zipfileSerializeCDS(ZipfileEntry *pEntry, u8 *aBuf){  u8 *a = aBuf;  ZipfileCDS *pCDS = &pEntry->cds;  if( pEntry->aExtra==0 ){    pCDS->nExtra = 9;  }  zipfileWrite32(a, ZIPFILE_SIGNATURE_CDS);  zipfileWrite16(a, pCDS->iVersionMadeBy);  zipfileWrite16(a, pCDS->iVersionExtract);  zipfileWrite16(a, pCDS->flags);  zipfileWrite16(a, pCDS->iCompression);  zipfileWrite16(a, pCDS->mTime);  zipfileWrite16(a, pCDS->mDate);  zipfileWrite32(a, pCDS->crc32);  zipfileWrite32(a, pCDS->szCompressed);  zipfileWrite32(a, pCDS->szUncompressed);  assert( a==&aBuf[ZIPFILE_CDS_NFILE_OFF] );  zipfileWrite16(a, pCDS->nFile);  zipfileWrite16(a, pCDS->nExtra);  zipfileWrite16(a, pCDS->nComment);  zipfileWrite16(a, pCDS->iDiskStart);  zipfileWrite16(a, pCDS->iInternalAttr);  zipfileWrite32(a, pCDS->iExternalAttr);  zipfileWrite32(a, pCDS->iOffset);  memcpy(a, pCDS->zFile, pCDS->nFile);  a += pCDS->nFile;  if( pEntry->aExtra ){    int n = (int)pCDS->nExtra + (int)pCDS->nComment;    memcpy(a, pEntry->aExtra, n);    a += n;  }else{    assert( pCDS->nExtra==9 );    zipfileWrite16(a, ZIPFILE_EXTRA_TIMESTAMP);    zipfileWrite16(a, 5);    *a++ = 0x01;    zipfileWrite32(a, pEntry->mUnixTime);  }  return a-aBuf;}static int zipfileCommit(sqlite3_vtab *pVtab){  ZipfileTab *pTab = (ZipfileTab*)pVtab;  int rc = SQLITE_OK;  if( pTab->pWriteFd ){    i64 iOffset = pTab->szCurrent;    ZipfileEntry *p;    ZipfileEOCD eocd;    int nEntry = 0;        for(p=pTab->pFirstEntry; rc==SQLITE_OK && p; p=p->pNext){      int n = zipfileSerializeCDS(p, pTab->aBuffer);      rc = zipfileAppendData(pTab, pTab->aBuffer, n);      nEntry++;    }        eocd.iDisk = 0;    eocd.iFirstDisk = 0;    eocd.nEntry = (u16)nEntry;    eocd.nEntryTotal = (u16)nEntry;    eocd.nSize = (u32)(pTab->szCurrent - iOffset);    eocd.iOffset = (u32)iOffset;    rc = zipfileAppendEOCD(pTab, &eocd);    zipfileCleanupTransaction(pTab);  }  return rc;}static int zipfileRollback(sqlite3_vtab *pVtab){  return zipfileCommit(pVtab);}static ZipfileCsr *zipfileFindCursor(ZipfileTab *pTab, i64 iId){  ZipfileCsr *pCsr;  for(pCsr=pTab->pCsrList; pCsr; pCsr=pCsr->pCsrNext){    if( iId==pCsr->iId ) break;  }  return pCsr;}static void zipfileFunctionCds(  sqlite3_context *context,  int argc,  sqlite3_value **argv){  ZipfileCsr *pCsr;  ZipfileTab *pTab = (ZipfileTab*)sqlite3_user_data(context);  assert( argc>0 );  pCsr = zipfileFindCursor(pTab, sqlite3_value_int64(argv[0]));  if( pCsr ){    ZipfileCDS *p = &pCsr->pCurrent->cds;    char *zRes = sqlite3_mprintf("{"        "\"version-made-by\" : %u, "        "\"version-to-extract\" : %u, "        "\"flags\" : %u, "        "\"compression\" : %u, "        "\"time\" : %u, "        "\"date\" : %u, "        "\"crc32\" : %u, "        "\"compressed-size\" : %u, "        "\"uncompressed-size\" : %u, "        "\"file-name-length\" : %u, "        "\"extra-field-length\" : %u, "        "\"file-comment-length\" : %u, "        "\"disk-number-start\" : %u, "        "\"internal-attr\" : %u, "        "\"external-attr\" : %u, "        "\"offset\" : %u }",        (u32)p->iVersionMadeBy, (u32)p->iVersionExtract,        (u32)p->flags, (u32)p->iCompression,        (u32)p->mTime, (u32)p->mDate,        (u32)p->crc32, (u32)p->szCompressed,        (u32)p->szUncompressed, (u32)p->nFile,        (u32)p->nExtra, (u32)p->nComment,        (u32)p->iDiskStart, (u32)p->iInternalAttr,        (u32)p->iExternalAttr, (u32)p->iOffset    );    if( zRes==0 ){      sqlite3_result_error_nomem(context);    }else{      sqlite3_result_text(context, zRes, -1, SQLITE_TRANSIENT);      sqlite3_free(zRes);    }  }}static int zipfileFindFunction(  sqlite3_vtab *pVtab,              int nArg,                         const char *zName,                void (**pxFunc)(sqlite3_context*,int,sqlite3_value**),   void **ppArg                    ){  if( sqlite3_stricmp("zipfile_cds", zName)==0 ){    *pxFunc = zipfileFunctionCds;    *ppArg = (void*)pVtab;    return 1;  }  return 0;}typedef struct ZipfileBuffer ZipfileBuffer;struct ZipfileBuffer {  u8 *a;                            int n;                            int nAlloc;                     };typedef struct ZipfileCtx ZipfileCtx;struct ZipfileCtx {  int nEntry;  ZipfileBuffer body;  ZipfileBuffer cds;};static int zipfileBufferGrow(ZipfileBuffer *pBuf, int nByte){  if( pBuf->n+nByte>pBuf->nAlloc ){    u8 *aNew;    sqlite3_int64 nNew = pBuf->n ? pBuf->n*2 : 512;    int nReq = pBuf->n + nByte;    while( nNew<nReq ) nNew = nNew*2;    aNew = sqlite3_realloc64(pBuf->a, nNew);    if( aNew==0 ) return SQLITE_NOMEM;    pBuf->a = aNew;    pBuf->nAlloc = (int)nNew;  }  return SQLITE_OK;}static void zipfileStep(sqlite3_context *pCtx, int nVal, sqlite3_value **apVal){  ZipfileCtx *p;                    ZipfileEntry e;                   sqlite3_value *pName = 0;  sqlite3_value *pMode = 0;  sqlite3_value *pMtime = 0;  sqlite3_value *pData = 0;  sqlite3_value *pMethod = 0;  int bIsDir = 0;  u32 mode;  int rc = SQLITE_OK;  char *zErr = 0;  int iMethod = -1;                 const u8 *aData = 0;              int nData = 0;                    int szUncompressed = 0;           u8 *aFree = 0;                    u32 iCrc32 = 0;                   char *zName = 0;                  int nName = 0;                    char *zFree = 0;                  int nByte;  memset(&e, 0, sizeof(e));  p = (ZipfileCtx*)sqlite3_aggregate_context(pCtx, sizeof(ZipfileCtx));  if( p==0 ) return;    if( nVal!=2 && nVal!=4 && nVal!=5 ){    zErr = sqlite3_mprintf("wrong number of arguments to function zipfile()");    rc = SQLITE_ERROR;    goto zipfile_step_out;  }  pName = apVal[0];  if( nVal==2 ){    pData = apVal[1];  }else{    pMode = apVal[1];    pMtime = apVal[2];    pData = apVal[3];    if( nVal==5 ){      pMethod = apVal[4];    }  }    zName = (char*)sqlite3_value_text(pName);  nName = sqlite3_value_bytes(pName);  if( zName==0 ){    zErr = sqlite3_mprintf("first argument to zipfile() must be non-NULL");    rc = SQLITE_ERROR;    goto zipfile_step_out;  }    if( pMethod && SQLITE_NULL!=sqlite3_value_type(pMethod) ){    iMethod = (int)sqlite3_value_int64(pMethod);    if( iMethod!=0 && iMethod!=8 ){      zErr = sqlite3_mprintf("illegal method value: %d", iMethod);      rc = SQLITE_ERROR;      goto zipfile_step_out;    }  }    if( sqlite3_value_type(pData)==SQLITE_NULL ){    bIsDir = 1;    iMethod = 0;  }else{    aData = sqlite3_value_blob(pData);    szUncompressed = nData = sqlite3_value_bytes(pData);    iCrc32 = crc32(0, aData, nData);    if( iMethod<0 || iMethod==8 ){      int nOut = 0;      rc = zipfileDeflate(aData, nData, &aFree, &nOut, &zErr);      if( rc!=SQLITE_OK ){        goto zipfile_step_out;      }      if( iMethod==8 || nOut<nData ){        aData = aFree;        nData = nOut;        iMethod = 8;      }else{        iMethod = 0;      }    }  }    rc = zipfileGetMode(pMode, bIsDir, &mode, &zErr);  if( rc ) goto zipfile_step_out;    e.mUnixTime = zipfileGetTime(pMtime);    if( bIsDir==0 ){    if( nName>0 && zName[nName-1]=='/' ){      zErr = sqlite3_mprintf("non-directory name must not end with /");      rc = SQLITE_ERROR;      goto zipfile_step_out;    }  }else{    if( nName==0 || zName[nName-1]!='/' ){      zName = zFree = sqlite3_mprintf("%s/", zName);      if( zName==0 ){        rc = SQLITE_NOMEM;        goto zipfile_step_out;      }      nName = (int)strlen(zName);    }else{      while( nName>1 && zName[nName-2]=='/' ) nName--;    }  }    e.cds.iVersionMadeBy = ZIPFILE_NEWENTRY_MADEBY;  e.cds.iVersionExtract = ZIPFILE_NEWENTRY_REQUIRED;  e.cds.flags = ZIPFILE_NEWENTRY_FLAGS;  e.cds.iCompression = (u16)iMethod;  zipfileMtimeToDos(&e.cds, (u32)e.mUnixTime);  e.cds.crc32 = iCrc32;  e.cds.szCompressed = nData;  e.cds.szUncompressed = szUncompressed;  e.cds.iExternalAttr = (mode<<16);  e.cds.iOffset = p->body.n;  e.cds.nFile = (u16)nName;  e.cds.zFile = zName;    nByte = ZIPFILE_LFH_FIXED_SZ + e.cds.nFile + 9;  if( (rc = zipfileBufferGrow(&p->body, nByte)) ) goto zipfile_step_out;  p->body.n += zipfileSerializeLFH(&e, &p->body.a[p->body.n]);    if( nData>0 ){    if( (rc = zipfileBufferGrow(&p->body, nData)) ) goto zipfile_step_out;    memcpy(&p->body.a[p->body.n], aData, nData);    p->body.n += nData;  }    nByte = ZIPFILE_CDS_FIXED_SZ + e.cds.nFile + 9;  if( (rc = zipfileBufferGrow(&p->cds, nByte)) ) goto zipfile_step_out;  p->cds.n += zipfileSerializeCDS(&e, &p->cds.a[p->cds.n]);    p->nEntry++; zipfile_step_out:  sqlite3_free(aFree);  sqlite3_free(zFree);  if( rc ){    if( zErr ){      sqlite3_result_error(pCtx, zErr, -1);    }else{      sqlite3_result_error_code(pCtx, rc);    }  }  sqlite3_free(zErr);}static void zipfileFinal(sqlite3_context *pCtx){  ZipfileCtx *p;  ZipfileEOCD eocd;  sqlite3_int64 nZip;  u8 *aZip;  p = (ZipfileCtx*)sqlite3_aggregate_context(pCtx, sizeof(ZipfileCtx));  if( p==0 ) return;  if( p->nEntry>0 ){    memset(&eocd, 0, sizeof(eocd));    eocd.nEntry = (u16)p->nEntry;    eocd.nEntryTotal = (u16)p->nEntry;    eocd.nSize = p->cds.n;    eocd.iOffset = p->body.n;    nZip = p->body.n + p->cds.n + ZIPFILE_EOCD_FIXED_SZ;    aZip = (u8*)sqlite3_malloc64(nZip);    if( aZip==0 ){      sqlite3_result_error_nomem(pCtx);    }else{      memcpy(aZip, p->body.a, p->body.n);      memcpy(&aZip[p->body.n], p->cds.a, p->cds.n);      zipfileSerializeEOCD(&eocd, &aZip[p->body.n + p->cds.n]);      sqlite3_result_blob(pCtx, aZip, (int)nZip, zipfileFree);    }  }  sqlite3_free(p->body.a);  sqlite3_free(p->cds.a);}static int zipfileRegister(sqlite3 *db){  static sqlite3_module zipfileModule = {    1,                             zipfileConnect,                zipfileConnect,                zipfileBestIndex,              zipfileDisconnect,             zipfileDisconnect,             zipfileOpen,                   zipfileClose,                  zipfileFilter,                 zipfileNext,                   zipfileEof,                    zipfileColumn,                 0,                             zipfileUpdate,                 zipfileBegin,                  0,                             zipfileCommit,                 zipfileRollback,               zipfileFindFunction,           0,                           };  int rc = sqlite3_create_module(db, "zipfile"  , &zipfileModule, 0);  if( rc==SQLITE_OK ) rc = sqlite3_overload_function(db, "zipfile_cds", -1);  if( rc==SQLITE_OK ){    rc = sqlite3_create_function(db, "zipfile", -1, SQLITE_UTF8, 0, 0,         zipfileStep, zipfileFinal    );  }  assert( sizeof(i64)==8 );  assert( sizeof(u32)==4 );  assert( sizeof(u16)==2 );  assert( sizeof(u8)==1 );  return rc;}#else         # define zipfileRegister(x) SQLITE_OK#endif#ifdef _WIN32#endifint sqlite3_zipfile_init(  sqlite3 *db,   char **pzErrMsg,   const sqlite3_api_routines *pApi){  SQLITE_EXTENSION_INIT2(pApi);  (void)pzErrMsg;    return zipfileRegister(db);}SQLITE_EXTENSION_INIT1#include <zlib.h>#include <assert.h>static void sqlarCompressFunc(  sqlite3_context *context,  int argc,  sqlite3_value **argv){  assert( argc==1 );  if( sqlite3_value_type(argv[0])==SQLITE_BLOB ){    const Bytef *pData = sqlite3_value_blob(argv[0]);    uLong nData = sqlite3_value_bytes(argv[0]);    uLongf nOut = compressBound(nData);    Bytef *pOut;    pOut = (Bytef*)sqlite3_malloc(nOut);    if( pOut==0 ){      sqlite3_result_error_nomem(context);      return;    }else{      if( Z_OK!=compress(pOut, &nOut, pData, nData) ){        sqlite3_result_error(context, "error in compress()", -1);      }else if( nOut<nData ){        sqlite3_result_blob(context, pOut, nOut, SQLITE_TRANSIENT);      }else{        sqlite3_result_value(context, argv[0]);      }      sqlite3_free(pOut);    }  }else{    sqlite3_result_value(context, argv[0]);  }}static void sqlarUncompressFunc(  sqlite3_context *context,  int argc,  sqlite3_value **argv){  uLong nData;  uLongf sz;  assert( argc==2 );  sz = sqlite3_value_int(argv[1]);  if( sz<=0 || sz==(nData = sqlite3_value_bytes(argv[0])) ){    sqlite3_result_value(context, argv[0]);  }else{    const Bytef *pData= sqlite3_value_blob(argv[0]);    Bytef *pOut = sqlite3_malloc(sz);    if( Z_OK!=uncompress(pOut, &sz, pData, nData) ){      sqlite3_result_error(context, "error in uncompress()", -1);    }else{      sqlite3_result_blob(context, pOut, sz, SQLITE_TRANSIENT);    }    sqlite3_free(pOut);  }}#ifdef _WIN32#endifint sqlite3_sqlar_init(  sqlite3 *db,   char **pzErrMsg,   const sqlite3_api_routines *pApi){  int rc = SQLITE_OK;  SQLITE_EXTENSION_INIT2(pApi);  (void)pzErrMsg;    rc = sqlite3_create_function(db, "sqlar_compress", 1,                                SQLITE_UTF8|SQLITE_INNOCUOUS, 0,                               sqlarCompressFunc, 0, 0);  if( rc==SQLITE_OK ){    rc = sqlite3_create_function(db, "sqlar_uncompress", 2,                                 SQLITE_UTF8|SQLITE_INNOCUOUS, 0,                                 sqlarUncompressFunc, 0, 0);  }  return rc;}#endif#if !defined(SQLITEEXPERT_H)#define SQLITEEXPERT_H 1typedef struct sqlite3expert sqlite3expert;sqlite3expert *sqlite3_expert_new(sqlite3 *db, char **pzErr);int sqlite3_expert_config(sqlite3expert *p, int op, ...);#define EXPERT_CONFIG_SAMPLE 1    int sqlite3_expert_sql(  sqlite3expert *p,                 const char *zSql,                 char **pzErr                    );int sqlite3_expert_analyze(sqlite3expert *p, char **pzErr);int sqlite3_expert_count(sqlite3expert*);const char *sqlite3_expert_report(sqlite3expert*, int iStmt, int eReport);#define EXPERT_REPORT_SQL        1#define EXPERT_REPORT_INDEXES    2#define EXPERT_REPORT_PLAN       3#define EXPERT_REPORT_CANDIDATES 4void sqlite3_expert_destroy(sqlite3expert*);#endif  #include <assert.h>#include <string.h>#include <stdio.h>#if !defined(SQLITE_AMALGAMATION)#if defined(SQLITE_COVERAGE_TEST) || defined(SQLITE_MUTATION_TEST)# define SQLITE_OMIT_AUXILIARY_SAFETY_CHECKS 1#endif#if defined(SQLITE_OMIT_AUXILIARY_SAFETY_CHECKS)# define ALWAYS(X)      (1)# define NEVER(X)       (0)#elif !defined(NDEBUG)# define ALWAYS(X)      ((X)?1:(assert(0),0))# define NEVER(X)       ((X)?(assert(0),1):0)#else# define ALWAYS(X)      (X)# define NEVER(X)       (X)#endif#endif #ifndef SQLITE_OMIT_VIRTUALTABLE typedef struct IdxColumn IdxColumn;typedef struct IdxConstraint IdxConstraint;typedef struct IdxScan IdxScan;typedef struct IdxStatement IdxStatement;typedef struct IdxTable IdxTable;typedef struct IdxWrite IdxWrite;#define STRLEN  (int)strlen#define UNIQUE_TABLE_NAME "t592690916721053953805701627921227776"struct IdxConstraint {  char *zColl;                      int bRange;                       int iCol;                         int bFlag;                        int bDesc;                        IdxConstraint *pNext;             IdxConstraint *pLink;           };struct IdxScan {  IdxTable *pTab;                   int iDb;                          i64 covering;                     IdxConstraint *pOrder;            IdxConstraint *pEq;               IdxConstraint *pRange;            IdxScan *pNextScan;             };struct IdxColumn {  char *zName;  char *zColl;  int iPk;};struct IdxTable {  int nCol;  char *zName;                      IdxColumn *aCol;  IdxTable *pNext;                };struct IdxWrite {  IdxTable *pTab;  int eOp;                          IdxWrite *pNext;};struct IdxStatement {  int iId;                          char *zSql;                       char *zIdx;                       char *zEQP;                       IdxStatement *pNext;};#define IDX_HASH_SIZE 1023typedef struct IdxHashEntry IdxHashEntry;typedef struct IdxHash IdxHash;struct IdxHashEntry {  char *zKey;                       char *zVal;                       char *zVal2;                      IdxHashEntry *pHashNext;          IdxHashEntry *pNext;            };struct IdxHash {  IdxHashEntry *pFirst;  IdxHashEntry *aHash[IDX_HASH_SIZE];};struct sqlite3expert {  int iSample;                      sqlite3 *db;                      sqlite3 *dbm;                     sqlite3 *dbv;                     IdxTable *pTable;                 IdxScan *pScan;                   IdxWrite *pWrite;                 IdxStatement *pStatement;         int bRun;                         char **pzErrmsg;  int rc;                           IdxHash hIdx;                     char *zCandidates;              };static void *idxMalloc(int *pRc, int nByte){  void *pRet;  assert( *pRc==SQLITE_OK );  assert( nByte>0 );  pRet = sqlite3_malloc(nByte);  if( pRet ){    memset(pRet, 0, nByte);  }else{    *pRc = SQLITE_NOMEM;  }  return pRet;}static void idxHashInit(IdxHash *pHash){  memset(pHash, 0, sizeof(IdxHash));}static void idxHashClear(IdxHash *pHash){  int i;  for(i=0; i<IDX_HASH_SIZE; i++){    IdxHashEntry *pEntry;    IdxHashEntry *pNext;    for(pEntry=pHash->aHash[i]; pEntry; pEntry=pNext){      pNext = pEntry->pHashNext;      sqlite3_free(pEntry->zVal2);      sqlite3_free(pEntry);    }  }  memset(pHash, 0, sizeof(IdxHash));}static int idxHashString(const char *z, int n){  unsigned int ret = 0;  int i;  for(i=0; i<n; i++){    ret += (ret<<3) + (unsigned char)(z[i]);  }  return (int)(ret % IDX_HASH_SIZE);}static int idxHashAdd(  int *pRc,   IdxHash *pHash,   const char *zKey,  const char *zVal){  int nKey = STRLEN(zKey);  int iHash = idxHashString(zKey, nKey);  int nVal = (zVal ? STRLEN(zVal) : 0);  IdxHashEntry *pEntry;  assert( iHash>=0 );  for(pEntry=pHash->aHash[iHash]; pEntry; pEntry=pEntry->pHashNext){    if( STRLEN(pEntry->zKey)==nKey && 0==memcmp(pEntry->zKey, zKey, nKey) ){      return 1;    }  }  pEntry = idxMalloc(pRc, sizeof(IdxHashEntry) + nKey+1 + nVal+1);  if( pEntry ){    pEntry->zKey = (char*)&pEntry[1];    memcpy(pEntry->zKey, zKey, nKey);    if( zVal ){      pEntry->zVal = &pEntry->zKey[nKey+1];      memcpy(pEntry->zVal, zVal, nVal);    }    pEntry->pHashNext = pHash->aHash[iHash];    pHash->aHash[iHash] = pEntry;    pEntry->pNext = pHash->pFirst;    pHash->pFirst = pEntry;  }  return 0;}static IdxHashEntry *idxHashFind(IdxHash *pHash, const char *zKey, int nKey){  int iHash;  IdxHashEntry *pEntry;  if( nKey<0 ) nKey = STRLEN(zKey);  iHash = idxHashString(zKey, nKey);  assert( iHash>=0 );  for(pEntry=pHash->aHash[iHash]; pEntry; pEntry=pEntry->pHashNext){    if( STRLEN(pEntry->zKey)==nKey && 0==memcmp(pEntry->zKey, zKey, nKey) ){      return pEntry;    }  }  return 0;}static const char *idxHashSearch(IdxHash *pHash, const char *zKey, int nKey){  IdxHashEntry *pEntry = idxHashFind(pHash, zKey, nKey);  if( pEntry ) return pEntry->zVal;  return 0;}static IdxConstraint *idxNewConstraint(int *pRc, const char *zColl){  IdxConstraint *pNew;  int nColl = STRLEN(zColl);  assert( *pRc==SQLITE_OK );  pNew = (IdxConstraint*)idxMalloc(pRc, sizeof(IdxConstraint) * nColl + 1);  if( pNew ){    pNew->zColl = (char*)&pNew[1];    memcpy(pNew->zColl, zColl, nColl+1);  }  return pNew;}static void idxDatabaseError(  sqlite3 *db,                      char **pzErrmsg                 ){  *pzErrmsg = sqlite3_mprintf("%s", sqlite3_errmsg(db));}static int idxPrepareStmt(  sqlite3 *db,                      sqlite3_stmt **ppStmt,            char **pzErrmsg,                  const char *zSql                ){  int rc = sqlite3_prepare_v2(db, zSql, -1, ppStmt, 0);  if( rc!=SQLITE_OK ){    *ppStmt = 0;    idxDatabaseError(db, pzErrmsg);  }  return rc;}static int idxPrintfPrepareStmt(  sqlite3 *db,                      sqlite3_stmt **ppStmt,            char **pzErrmsg,                  const char *zFmt,                 ...                             ){  va_list ap;  int rc;  char *zSql;  va_start(ap, zFmt);  zSql = sqlite3_vmprintf(zFmt, ap);  if( zSql==0 ){    rc = SQLITE_NOMEM;  }else{    rc = idxPrepareStmt(db, ppStmt, pzErrmsg, zSql);    sqlite3_free(zSql);  }  va_end(ap);  return rc;}typedef struct ExpertVtab ExpertVtab;struct ExpertVtab {  sqlite3_vtab base;  IdxTable *pTab;  sqlite3expert *pExpert;};typedef struct ExpertCsr ExpertCsr;struct ExpertCsr {  sqlite3_vtab_cursor base;  sqlite3_stmt *pData;};static char *expertDequote(const char *zIn){  int n = STRLEN(zIn);  char *zRet = sqlite3_malloc(n);  assert( zIn[0]=='\'' );  assert( zIn[n-1]=='\'' );  if( zRet ){    int iOut = 0;    int iIn = 0;    for(iIn=1; iIn<(n-1); iIn++){      if( zIn[iIn]=='\'' ){        assert( zIn[iIn+1]=='\'' );        iIn++;      }      zRet[iOut++] = zIn[iIn];    }    zRet[iOut] = '\0';  }  return zRet;}static int expertConnect(  sqlite3 *db,  void *pAux,  int argc, const char *const*argv,  sqlite3_vtab **ppVtab,  char **pzErr){  sqlite3expert *pExpert = (sqlite3expert*)pAux;  ExpertVtab *p = 0;  int rc;  if( argc!=4 ){    *pzErr = sqlite3_mprintf("internal error!");    rc = SQLITE_ERROR;  }else{    char *zCreateTable = expertDequote(argv[3]);    if( zCreateTable ){      rc = sqlite3_declare_vtab(db, zCreateTable);      if( rc==SQLITE_OK ){        p = idxMalloc(&rc, sizeof(ExpertVtab));      }      if( rc==SQLITE_OK ){        p->pExpert = pExpert;        p->pTab = pExpert->pTable;        assert( sqlite3_stricmp(p->pTab->zName, argv[2])==0 );      }      sqlite3_free(zCreateTable);    }else{      rc = SQLITE_NOMEM;    }  }  *ppVtab = (sqlite3_vtab*)p;  return rc;}static int expertDisconnect(sqlite3_vtab *pVtab){  ExpertVtab *p = (ExpertVtab*)pVtab;  sqlite3_free(p);  return SQLITE_OK;}static int expertBestIndex(sqlite3_vtab *pVtab, sqlite3_index_info *pIdxInfo){  ExpertVtab *p = (ExpertVtab*)pVtab;  int rc = SQLITE_OK;  int n = 0;  IdxScan *pScan;  const int opmask =     SQLITE_INDEX_CONSTRAINT_EQ | SQLITE_INDEX_CONSTRAINT_GT |    SQLITE_INDEX_CONSTRAINT_LT | SQLITE_INDEX_CONSTRAINT_GE |    SQLITE_INDEX_CONSTRAINT_LE;  pScan = idxMalloc(&rc, sizeof(IdxScan));  if( pScan ){    int i;        pScan->pTab = p->pTab;    pScan->pNextScan = p->pExpert->pScan;    p->pExpert->pScan = pScan;        for(i=0; i<pIdxInfo->nConstraint; i++){      struct sqlite3_index_constraint *pCons = &pIdxInfo->aConstraint[i];      if( pCons->usable        && pCons->iColumn>=0        && p->pTab->aCol[pCons->iColumn].iPk==0       && (pCons->op & opmask)       ){        IdxConstraint *pNew;        const char *zColl = sqlite3_vtab_collation(pIdxInfo, i);        pNew = idxNewConstraint(&rc, zColl);        if( pNew ){          pNew->iCol = pCons->iColumn;          if( pCons->op==SQLITE_INDEX_CONSTRAINT_EQ ){            pNew->pNext = pScan->pEq;            pScan->pEq = pNew;          }else{            pNew->bRange = 1;            pNew->pNext = pScan->pRange;            pScan->pRange = pNew;          }        }        n++;        pIdxInfo->aConstraintUsage[i].argvIndex = n;      }    }        for(i=pIdxInfo->nOrderBy-1; i>=0; i--){      int iCol = pIdxInfo->aOrderBy[i].iColumn;      if( iCol>=0 ){        IdxConstraint *pNew = idxNewConstraint(&rc, p->pTab->aCol[iCol].zColl);        if( pNew ){          pNew->iCol = iCol;          pNew->bDesc = pIdxInfo->aOrderBy[i].desc;          pNew->pNext = pScan->pOrder;          pNew->pLink = pScan->pOrder;          pScan->pOrder = pNew;          n++;        }      }    }  }  pIdxInfo->estimatedCost = 1000000.0 / (n+1);  return rc;}static int expertUpdate(  sqlite3_vtab *pVtab,   int nData,   sqlite3_value **azData,   sqlite_int64 *pRowid){  (void)pVtab;  (void)nData;  (void)azData;  (void)pRowid;  return SQLITE_OK;}static int expertOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){  int rc = SQLITE_OK;  ExpertCsr *pCsr;  (void)pVTab;  pCsr = idxMalloc(&rc, sizeof(ExpertCsr));  *ppCursor = (sqlite3_vtab_cursor*)pCsr;  return rc;}static int expertClose(sqlite3_vtab_cursor *cur){  ExpertCsr *pCsr = (ExpertCsr*)cur;  sqlite3_finalize(pCsr->pData);  sqlite3_free(pCsr);  return SQLITE_OK;}static int expertEof(sqlite3_vtab_cursor *cur){  ExpertCsr *pCsr = (ExpertCsr*)cur;  return pCsr->pData==0;}static int expertNext(sqlite3_vtab_cursor *cur){  ExpertCsr *pCsr = (ExpertCsr*)cur;  int rc = SQLITE_OK;  assert( pCsr->pData );  rc = sqlite3_step(pCsr->pData);  if( rc!=SQLITE_ROW ){    rc = sqlite3_finalize(pCsr->pData);    pCsr->pData = 0;  }else{    rc = SQLITE_OK;  }  return rc;}static int expertRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){  (void)cur;  *pRowid = 0;  return SQLITE_OK;}static int expertColumn(sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int i){  ExpertCsr *pCsr = (ExpertCsr*)cur;  sqlite3_value *pVal;  pVal = sqlite3_column_value(pCsr->pData, i);  if( pVal ){    sqlite3_result_value(ctx, pVal);  }  return SQLITE_OK;}static int expertFilter(  sqlite3_vtab_cursor *cur,   int idxNum, const char *idxStr,  int argc, sqlite3_value **argv){  ExpertCsr *pCsr = (ExpertCsr*)cur;  ExpertVtab *pVtab = (ExpertVtab*)(cur->pVtab);  sqlite3expert *pExpert = pVtab->pExpert;  int rc;  (void)idxNum;  (void)idxStr;  (void)argc;  (void)argv;  rc = sqlite3_finalize(pCsr->pData);  pCsr->pData = 0;  if( rc==SQLITE_OK ){    rc = idxPrintfPrepareStmt(pExpert->db, &pCsr->pData, &pVtab->base.zErrMsg,        "SELECT * FROM main.%Q WHERE sample()", pVtab->pTab->zName    );  }  if( rc==SQLITE_OK ){    rc = expertNext(cur);  }  return rc;}static int idxRegisterVtab(sqlite3expert *p){  static sqlite3_module expertModule = {    2,                                expertConnect,                    expertConnect,                    expertBestIndex,                  expertDisconnect,                 expertDisconnect,                 expertOpen,                       expertClose,                      expertFilter,                     expertNext,                       expertEof,                        expertColumn,                     expertRowid,                      expertUpdate,                     0,                                0,                                0,                                0,                                0,                                0,                                0,                                0,                                0,                                0,                              };  return sqlite3_create_module(p->dbv, "expert", &expertModule, (void*)p);}static void idxFinalize(int *pRc, sqlite3_stmt *pStmt){  int rc = sqlite3_finalize(pStmt);  if( *pRc==SQLITE_OK ) *pRc = rc;}static int idxGetTableInfo(  sqlite3 *db,                      const char *zTab,                 IdxTable **ppOut,                 char **pzErrmsg                 ){  sqlite3_stmt *p1 = 0;  int nCol = 0;  int nTab;  int nByte;  IdxTable *pNew = 0;  int rc, rc2;  char *pCsr = 0;  int nPk = 0;  *ppOut = 0;  if( zTab==0 ) return SQLITE_ERROR;  nTab = STRLEN(zTab);  nByte = sizeof(IdxTable) + nTab + 1;  rc = idxPrintfPrepareStmt(db, &p1, pzErrmsg, "PRAGMA table_xinfo=%Q", zTab);  while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(p1) ){    const char *zCol = (const char*)sqlite3_column_text(p1, 1);    const char *zColSeq = 0;    if( zCol==0 ){      rc = SQLITE_ERROR;      break;    }    nByte += 1 + STRLEN(zCol);    rc = sqlite3_table_column_metadata(        db, "main", zTab, zCol, 0, &zColSeq, 0, 0, 0    );    if( zColSeq==0 ) zColSeq = "binary";    nByte += 1 + STRLEN(zColSeq);    nCol++;    nPk += (sqlite3_column_int(p1, 5)>0);  }  rc2 = sqlite3_reset(p1);  if( rc==SQLITE_OK ) rc = rc2;  nByte += sizeof(IdxColumn) * nCol;  if( rc==SQLITE_OK ){    pNew = idxMalloc(&rc, nByte);  }  if( rc==SQLITE_OK ){    pNew->aCol = (IdxColumn*)&pNew[1];    pNew->nCol = nCol;    pCsr = (char*)&pNew->aCol[nCol];  }  nCol = 0;  while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(p1) ){    const char *zCol = (const char*)sqlite3_column_text(p1, 1);    const char *zColSeq = 0;    int nCopy;    if( zCol==0 ) continue;    nCopy = STRLEN(zCol) + 1;    pNew->aCol[nCol].zName = pCsr;    pNew->aCol[nCol].iPk = (sqlite3_column_int(p1, 5)==1 && nPk==1);    memcpy(pCsr, zCol, nCopy);    pCsr += nCopy;    rc = sqlite3_table_column_metadata(        db, "main", zTab, zCol, 0, &zColSeq, 0, 0, 0    );    if( rc==SQLITE_OK ){      if( zColSeq==0 ) zColSeq = "binary";      nCopy = STRLEN(zColSeq) + 1;      pNew->aCol[nCol].zColl = pCsr;      memcpy(pCsr, zColSeq, nCopy);      pCsr += nCopy;    }    nCol++;  }  idxFinalize(&rc, p1);  if( rc!=SQLITE_OK ){    sqlite3_free(pNew);    pNew = 0;  }else if( ALWAYS(pNew!=0) ){    pNew->zName = pCsr;    if( ALWAYS(pNew->zName!=0) ) memcpy(pNew->zName, zTab, nTab+1);  }  *ppOut = pNew;  return rc;}static char *idxAppendText(int *pRc, char *zIn, const char *zFmt, ...){  va_list ap;  char *zAppend = 0;  char *zRet = 0;  int nIn = zIn ? STRLEN(zIn) : 0;  int nAppend = 0;  va_start(ap, zFmt);  if( *pRc==SQLITE_OK ){    zAppend = sqlite3_vmprintf(zFmt, ap);    if( zAppend ){      nAppend = STRLEN(zAppend);      zRet = (char*)sqlite3_malloc(nIn + nAppend + 1);    }    if( zAppend && zRet ){      if( nIn ) memcpy(zRet, zIn, nIn);      memcpy(&zRet[nIn], zAppend, nAppend+1);    }else{      sqlite3_free(zRet);      zRet = 0;      *pRc = SQLITE_NOMEM;    }    sqlite3_free(zAppend);    sqlite3_free(zIn);  }  va_end(ap);  return zRet;}static int idxIdentifierRequiresQuotes(const char *zId){  int i;  for(i=0; zId[i]; i++){    if( !(zId[i]=='_')     && !(zId[i]>='0' && zId[i]<='9')     && !(zId[i]>='a' && zId[i]<='z')     && !(zId[i]>='A' && zId[i]<='Z')    ){      return 1;    }  }  return 0;}static char *idxAppendColDefn(  int *pRc,                         char *zIn,                        IdxTable *pTab,                   IdxConstraint *pCons){  char *zRet = zIn;  IdxColumn *p = &pTab->aCol[pCons->iCol];  if( zRet ) zRet = idxAppendText(pRc, zRet, ", ");  if( idxIdentifierRequiresQuotes(p->zName) ){    zRet = idxAppendText(pRc, zRet, "%Q", p->zName);  }else{    zRet = idxAppendText(pRc, zRet, "%s", p->zName);  }  if( sqlite3_stricmp(p->zColl, pCons->zColl) ){    if( idxIdentifierRequiresQuotes(pCons->zColl) ){      zRet = idxAppendText(pRc, zRet, " COLLATE %Q", pCons->zColl);    }else{      zRet = idxAppendText(pRc, zRet, " COLLATE %s", pCons->zColl);    }  }  if( pCons->bDesc ){    zRet = idxAppendText(pRc, zRet, " DESC");  }  return zRet;}static int idxFindCompatible(  int *pRc,                         sqlite3* dbm,                     IdxScan *pScan,                   IdxConstraint *pEq,               IdxConstraint *pTail            ){  const char *zTbl = pScan->pTab->zName;  sqlite3_stmt *pIdxList = 0;  IdxConstraint *pIter;  int nEq = 0;                      int rc;    for(pIter=pEq; pIter; pIter=pIter->pLink) nEq++;  rc = idxPrintfPrepareStmt(dbm, &pIdxList, 0, "PRAGMA index_list=%Q", zTbl);  while( rc==SQLITE_OK && sqlite3_step(pIdxList)==SQLITE_ROW ){    int bMatch = 1;    IdxConstraint *pT = pTail;    sqlite3_stmt *pInfo = 0;    const char *zIdx = (const char*)sqlite3_column_text(pIdxList, 1);    if( zIdx==0 ) continue;        for(pIter=pEq; pIter; pIter=pIter->pLink) pIter->bFlag = 0;    rc = idxPrintfPrepareStmt(dbm, &pInfo, 0, "PRAGMA index_xInfo=%Q", zIdx);    while( rc==SQLITE_OK && sqlite3_step(pInfo)==SQLITE_ROW ){      int iIdx = sqlite3_column_int(pInfo, 0);      int iCol = sqlite3_column_int(pInfo, 1);      const char *zColl = (const char*)sqlite3_column_text(pInfo, 4);      if( iIdx<nEq ){        for(pIter=pEq; pIter; pIter=pIter->pLink){          if( pIter->bFlag ) continue;          if( pIter->iCol!=iCol ) continue;          if( sqlite3_stricmp(pIter->zColl, zColl) ) continue;          pIter->bFlag = 1;          break;        }        if( pIter==0 ){          bMatch = 0;          break;        }      }else{        if( pT ){          if( pT->iCol!=iCol || sqlite3_stricmp(pT->zColl, zColl) ){            bMatch = 0;            break;          }          pT = pT->pLink;        }      }    }    idxFinalize(&rc, pInfo);    if( rc==SQLITE_OK && bMatch ){      sqlite3_finalize(pIdxList);      return 1;    }  }  idxFinalize(&rc, pIdxList);  *pRc = rc;  return 0;}static int countNonzeros(void* pCount, int nc,                         char* azResults[], char* azColumns[]){  (void)azColumns;    if( nc>0 && (azResults[0][0]!='0' || azResults[0][1]!=0) ){    *((int *)pCount) += 1;  }  return 0;}static int idxCreateFromCons(  sqlite3expert *p,  IdxScan *pScan,  IdxConstraint *pEq,   IdxConstraint *pTail){  sqlite3 *dbm = p->dbm;  int rc = SQLITE_OK;  if( (pEq || pTail) && 0==idxFindCompatible(&rc, dbm, pScan, pEq, pTail) ){    IdxTable *pTab = pScan->pTab;    char *zCols = 0;    char *zIdx = 0;    IdxConstraint *pCons;    unsigned int h = 0;    const char *zFmt;    for(pCons=pEq; pCons; pCons=pCons->pLink){      zCols = idxAppendColDefn(&rc, zCols, pTab, pCons);    }    for(pCons=pTail; pCons; pCons=pCons->pLink){      zCols = idxAppendColDefn(&rc, zCols, pTab, pCons);    }    if( rc==SQLITE_OK ){            const char *zTable = pScan->pTab->zName;      int quoteTable = idxIdentifierRequiresQuotes(zTable);      char *zName = 0;                int collisions = 0;      do{        int i;        char *zFind;        for(i=0; zCols[i]; i++){          h += ((h<<3) + zCols[i]);        }        sqlite3_free(zName);        zName = sqlite3_mprintf("%s_idx_%08x", zTable, h);        if( zName==0 ) break;                zFmt = "SELECT count(*) FROM sqlite_schema WHERE name=%Q"          " AND type in ('index','table','view')";        zFind = sqlite3_mprintf(zFmt, zName);        i = 0;        rc = sqlite3_exec(dbm, zFind, countNonzeros, &i, 0);        assert(rc==SQLITE_OK);        sqlite3_free(zFind);        if( i==0 ){          collisions = 0;          break;        }        ++collisions;      }while( collisions<50 && zName!=0 );      if( collisions ){                rc = SQLITE_BUSY_TIMEOUT;      }else if( zName==0 ){        rc = SQLITE_NOMEM;      }else{        if( quoteTable ){          zFmt = "CREATE INDEX \"%w\" ON \"%w\"(%s)";        }else{          zFmt = "CREATE INDEX %s ON %s(%s)";        }        zIdx = sqlite3_mprintf(zFmt, zName, zTable, zCols);        if( !zIdx ){          rc = SQLITE_NOMEM;        }else{          rc = sqlite3_exec(dbm, zIdx, 0, 0, p->pzErrmsg);          if( rc!=SQLITE_OK ){            rc = SQLITE_BUSY_TIMEOUT;          }else{            idxHashAdd(&rc, &p->hIdx, zName, zIdx);          }        }        sqlite3_free(zName);        sqlite3_free(zIdx);      }    }    sqlite3_free(zCols);  }  return rc;}static int idxFindConstraint(IdxConstraint *pList, IdxConstraint *p){  IdxConstraint *pCmp;  for(pCmp=pList; pCmp; pCmp=pCmp->pLink){    if( p->iCol==pCmp->iCol ) return 1;  }  return 0;}static int idxCreateFromWhere(  sqlite3expert *p,   IdxScan *pScan,                   IdxConstraint *pTail            ){  IdxConstraint *p1 = 0;  IdxConstraint *pCon;  int rc;    for(pCon=pScan->pEq; pCon; pCon=pCon->pNext){    if( !idxFindConstraint(p1, pCon) && !idxFindConstraint(pTail, pCon) ){      pCon->pLink = p1;      p1 = pCon;    }  }    rc = idxCreateFromCons(p, pScan, p1, pTail);    if( pTail==0 ){    for(pCon=pScan->pRange; rc==SQLITE_OK && pCon; pCon=pCon->pNext){      assert( pCon->pLink==0 );      if( !idxFindConstraint(p1, pCon) && !idxFindConstraint(pTail, pCon) ){        rc = idxCreateFromCons(p, pScan, p1, pCon);      }    }  }  return rc;}static int idxCreateCandidates(sqlite3expert *p){  int rc = SQLITE_OK;  IdxScan *pIter;  for(pIter=p->pScan; pIter && rc==SQLITE_OK; pIter=pIter->pNextScan){    rc = idxCreateFromWhere(p, pIter, 0);    if( rc==SQLITE_OK && pIter->pOrder ){      rc = idxCreateFromWhere(p, pIter, pIter->pOrder);    }  }  return rc;}static void idxConstraintFree(IdxConstraint *pConstraint){  IdxConstraint *pNext;  IdxConstraint *p;  for(p=pConstraint; p; p=pNext){    pNext = p->pNext;    sqlite3_free(p);  }}static void idxScanFree(IdxScan *pScan, IdxScan *pLast){  IdxScan *p;  IdxScan *pNext;  for(p=pScan; p!=pLast; p=pNext){    pNext = p->pNextScan;    idxConstraintFree(p->pOrder);    idxConstraintFree(p->pEq);    idxConstraintFree(p->pRange);    sqlite3_free(p);  }}static void idxStatementFree(IdxStatement *pStatement, IdxStatement *pLast){  IdxStatement *p;  IdxStatement *pNext;  for(p=pStatement; p!=pLast; p=pNext){    pNext = p->pNext;    sqlite3_free(p->zEQP);    sqlite3_free(p->zIdx);    sqlite3_free(p);  }}static void idxTableFree(IdxTable *pTab){  IdxTable *pIter;  IdxTable *pNext;  for(pIter=pTab; pIter; pIter=pNext){    pNext = pIter->pNext;    sqlite3_free(pIter);  }}static void idxWriteFree(IdxWrite *pTab){  IdxWrite *pIter;  IdxWrite *pNext;  for(pIter=pTab; pIter; pIter=pNext){    pNext = pIter->pNext;    sqlite3_free(pIter);  }}static int idxFindIndexes(  sqlite3expert *p,  char **pzErr                         ){  IdxStatement *pStmt;  sqlite3 *dbm = p->dbm;  int rc = SQLITE_OK;  IdxHash hIdx;  idxHashInit(&hIdx);  for(pStmt=p->pStatement; rc==SQLITE_OK && pStmt; pStmt=pStmt->pNext){    IdxHashEntry *pEntry;    sqlite3_stmt *pExplain = 0;    idxHashClear(&hIdx);    rc = idxPrintfPrepareStmt(dbm, &pExplain, pzErr,        "EXPLAIN QUERY PLAN %s", pStmt->zSql    );    while( rc==SQLITE_OK && sqlite3_step(pExplain)==SQLITE_ROW ){                        const char *zDetail = (const char*)sqlite3_column_text(pExplain, 3);      int nDetail;      int i;      if( !zDetail ) continue;      nDetail = STRLEN(zDetail);      for(i=0; i<nDetail; i++){        const char *zIdx = 0;        if( i+13<nDetail && memcmp(&zDetail[i], " USING INDEX ", 13)==0 ){          zIdx = &zDetail[i+13];        }else if( i+22<nDetail             && memcmp(&zDetail[i], " USING COVERING INDEX ", 22)==0         ){          zIdx = &zDetail[i+22];        }        if( zIdx ){          const char *zSql;          int nIdx = 0;          while( zIdx[nIdx]!='\0' && (zIdx[nIdx]!=' ' || zIdx[nIdx+1]!='(') ){            nIdx++;          }          zSql = idxHashSearch(&p->hIdx, zIdx, nIdx);          if( zSql ){            idxHashAdd(&rc, &hIdx, zSql, 0);            if( rc ) goto find_indexes_out;          }          break;        }      }      if( zDetail[0]!='-' ){        pStmt->zEQP = idxAppendText(&rc, pStmt->zEQP, "%s\n", zDetail);      }    }    for(pEntry=hIdx.pFirst; pEntry; pEntry=pEntry->pNext){      pStmt->zIdx = idxAppendText(&rc, pStmt->zIdx, "%s;\n", pEntry->zKey);    }    idxFinalize(&rc, pExplain);  } find_indexes_out:  idxHashClear(&hIdx);  return rc;}static int idxAuthCallback(  void *pCtx,  int eOp,  const char *z3,  const char *z4,  const char *zDb,  const char *zTrigger){  int rc = SQLITE_OK;  (void)z4;  (void)zTrigger;  if( eOp==SQLITE_INSERT || eOp==SQLITE_UPDATE || eOp==SQLITE_DELETE ){    if( sqlite3_stricmp(zDb, "main")==0 ){      sqlite3expert *p = (sqlite3expert*)pCtx;      IdxTable *pTab;      for(pTab=p->pTable; pTab; pTab=pTab->pNext){        if( 0==sqlite3_stricmp(z3, pTab->zName) ) break;      }      if( pTab ){        IdxWrite *pWrite;        for(pWrite=p->pWrite; pWrite; pWrite=pWrite->pNext){          if( pWrite->pTab==pTab && pWrite->eOp==eOp ) break;        }        if( pWrite==0 ){          pWrite = idxMalloc(&rc, sizeof(IdxWrite));          if( rc==SQLITE_OK ){            pWrite->pTab = pTab;            pWrite->eOp = eOp;            pWrite->pNext = p->pWrite;            p->pWrite = pWrite;          }        }      }    }  }  return rc;}static int idxProcessOneTrigger(  sqlite3expert *p,   IdxWrite *pWrite,   char **pzErr){  static const char *zInt = UNIQUE_TABLE_NAME;  static const char *zDrop = "DROP TABLE " UNIQUE_TABLE_NAME;  IdxTable *pTab = pWrite->pTab;  const char *zTab = pTab->zName;  const char *zSql =     "SELECT 'CREATE TEMP' || substr(sql, 7) FROM sqlite_schema "    "WHERE tbl_name = %Q AND type IN ('table', 'trigger') "    "ORDER BY type;";  sqlite3_stmt *pSelect = 0;  int rc = SQLITE_OK;  char *zWrite = 0;    rc = idxPrintfPrepareStmt(p->db, &pSelect, pzErr, zSql, zTab, zTab);  while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSelect) ){    const char *zCreate = (const char*)sqlite3_column_text(pSelect, 0);    if( zCreate==0 ) continue;    rc = sqlite3_exec(p->dbv, zCreate, 0, 0, pzErr);  }  idxFinalize(&rc, pSelect);    if( rc==SQLITE_OK ){    char *z = sqlite3_mprintf("ALTER TABLE temp.%Q RENAME TO %Q", zTab, zInt);    if( z==0 ){      rc = SQLITE_NOMEM;    }else{      rc = sqlite3_exec(p->dbv, z, 0, 0, pzErr);      sqlite3_free(z);    }  }  switch( pWrite->eOp ){    case SQLITE_INSERT: {      int i;      zWrite = idxAppendText(&rc, zWrite, "INSERT INTO %Q VALUES(", zInt);      for(i=0; i<pTab->nCol; i++){        zWrite = idxAppendText(&rc, zWrite, "%s?", i==0 ? "" : ", ");      }      zWrite = idxAppendText(&rc, zWrite, ")");      break;    }    case SQLITE_UPDATE: {      int i;      zWrite = idxAppendText(&rc, zWrite, "UPDATE %Q SET ", zInt);      for(i=0; i<pTab->nCol; i++){        zWrite = idxAppendText(&rc, zWrite, "%s%Q=?", i==0 ? "" : ", ",             pTab->aCol[i].zName        );      }      break;    }    default: {      assert( pWrite->eOp==SQLITE_DELETE );      if( rc==SQLITE_OK ){        zWrite = sqlite3_mprintf("DELETE FROM %Q", zInt);        if( zWrite==0 ) rc = SQLITE_NOMEM;      }    }  }  if( rc==SQLITE_OK ){    sqlite3_stmt *pX = 0;    rc = sqlite3_prepare_v2(p->dbv, zWrite, -1, &pX, 0);    idxFinalize(&rc, pX);    if( rc!=SQLITE_OK ){      idxDatabaseError(p->dbv, pzErr);    }  }  sqlite3_free(zWrite);  if( rc==SQLITE_OK ){    rc = sqlite3_exec(p->dbv, zDrop, 0, 0, pzErr);  }  return rc;}static int idxProcessTriggers(sqlite3expert *p, char **pzErr){  int rc = SQLITE_OK;  IdxWrite *pEnd = 0;  IdxWrite *pFirst = p->pWrite;  while( rc==SQLITE_OK && pFirst!=pEnd ){    IdxWrite *pIter;    for(pIter=pFirst; rc==SQLITE_OK && pIter!=pEnd; pIter=pIter->pNext){      rc = idxProcessOneTrigger(p, pIter, pzErr);    }    pEnd = pFirst;    pFirst = p->pWrite;  }  return rc;}static int idxCreateVtabSchema(sqlite3expert *p, char **pzErrmsg){  int rc = idxRegisterVtab(p);  sqlite3_stmt *pSchema = 0;    rc = idxPrepareStmt(p->db, &pSchema, pzErrmsg,      "SELECT type, name, sql, 1 FROM sqlite_schema "      "WHERE type IN ('table','view') AND name NOT LIKE 'sqlite_%%' "      " UNION ALL "      "SELECT type, name, sql, 2 FROM sqlite_schema "      "WHERE type = 'trigger'"      "  AND tbl_name IN(SELECT name FROM sqlite_schema WHERE type = 'view') "      "ORDER BY 4, 1"  );  while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSchema) ){    const char *zType = (const char*)sqlite3_column_text(pSchema, 0);    const char *zName = (const char*)sqlite3_column_text(pSchema, 1);    const char *zSql = (const char*)sqlite3_column_text(pSchema, 2);    if( zType==0 || zName==0 ) continue;    if( zType[0]=='v' || zType[1]=='r' ){      if( zSql ) rc = sqlite3_exec(p->dbv, zSql, 0, 0, pzErrmsg);    }else{      IdxTable *pTab;      rc = idxGetTableInfo(p->db, zName, &pTab, pzErrmsg);      if( rc==SQLITE_OK ){        int i;        char *zInner = 0;        char *zOuter = 0;        pTab->pNext = p->pTable;        p->pTable = pTab;                zInner = idxAppendText(&rc, 0, "CREATE TABLE x(");        for(i=0; i<pTab->nCol; i++){          zInner = idxAppendText(&rc, zInner, "%s%Q COLLATE %s",               (i==0 ? "" : ", "), pTab->aCol[i].zName, pTab->aCol[i].zColl          );        }        zInner = idxAppendText(&rc, zInner, ")");                zOuter = idxAppendText(&rc, 0,             "CREATE VIRTUAL TABLE %Q USING expert(%Q)", zName, zInner        );        if( rc==SQLITE_OK ){          rc = sqlite3_exec(p->dbv, zOuter, 0, 0, pzErrmsg);        }        sqlite3_free(zInner);        sqlite3_free(zOuter);      }    }  }  idxFinalize(&rc, pSchema);  return rc;}struct IdxSampleCtx {  int iTarget;  double target;                    double nRow;                      double nRet;                    };static void idxSampleFunc(  sqlite3_context *pCtx,  int argc,  sqlite3_value **argv){  struct IdxSampleCtx *p = (struct IdxSampleCtx*)sqlite3_user_data(pCtx);  int bRet;  (void)argv;  assert( argc==0 );  if( p->nRow==0.0 ){    bRet = 1;  }else{    bRet = (p->nRet / p->nRow) <= p->target;    if( bRet==0 ){      unsigned short rnd;      sqlite3_randomness(2, (void*)&rnd);      bRet = ((int)rnd % 100) <= p->iTarget;    }  }  sqlite3_result_int(pCtx, bRet);  p->nRow += 1.0;  p->nRet += (double)bRet;}struct IdxRemCtx {  int nSlot;  struct IdxRemSlot {    int eType;                        i64 iVal;                         double rVal;                      int nByte;                        int n;                            char *z;                        } aSlot[1];};static void idxRemFunc(  sqlite3_context *pCtx,  int argc,  sqlite3_value **argv){  struct IdxRemCtx *p = (struct IdxRemCtx*)sqlite3_user_data(pCtx);  struct IdxRemSlot *pSlot;  int iSlot;  assert( argc==2 );  iSlot = sqlite3_value_int(argv[0]);  assert( iSlot<=p->nSlot );  pSlot = &p->aSlot[iSlot];  switch( pSlot->eType ){    case SQLITE_NULL:            break;    case SQLITE_INTEGER:      sqlite3_result_int64(pCtx, pSlot->iVal);      break;    case SQLITE_FLOAT:      sqlite3_result_double(pCtx, pSlot->rVal);      break;    case SQLITE_BLOB:      sqlite3_result_blob(pCtx, pSlot->z, pSlot->n, SQLITE_TRANSIENT);      break;    case SQLITE_TEXT:      sqlite3_result_text(pCtx, pSlot->z, pSlot->n, SQLITE_TRANSIENT);      break;  }  pSlot->eType = sqlite3_value_type(argv[1]);  switch( pSlot->eType ){    case SQLITE_NULL:            break;    case SQLITE_INTEGER:      pSlot->iVal = sqlite3_value_int64(argv[1]);      break;    case SQLITE_FLOAT:      pSlot->rVal = sqlite3_value_double(argv[1]);      break;    case SQLITE_BLOB:    case SQLITE_TEXT: {      int nByte = sqlite3_value_bytes(argv[1]);      const void *pData = 0;      if( nByte>pSlot->nByte ){        char *zNew = (char*)sqlite3_realloc(pSlot->z, nByte*2);        if( zNew==0 ){          sqlite3_result_error_nomem(pCtx);          return;        }        pSlot->nByte = nByte*2;        pSlot->z = zNew;      }      pSlot->n = nByte;      if( pSlot->eType==SQLITE_BLOB ){        pData = sqlite3_value_blob(argv[1]);        if( pData ) memcpy(pSlot->z, pData, nByte);      }else{        pData = sqlite3_value_text(argv[1]);        memcpy(pSlot->z, pData, nByte);      }      break;    }  }}static int idxLargestIndex(sqlite3 *db, int *pnMax, char **pzErr){  int rc = SQLITE_OK;  const char *zMax =     "SELECT max(i.seqno) FROM "    "  sqlite_schema AS s, "    "  pragma_index_list(s.name) AS l, "    "  pragma_index_info(l.name) AS i "    "WHERE s.type = 'table'";  sqlite3_stmt *pMax = 0;  *pnMax = 0;  rc = idxPrepareStmt(db, &pMax, pzErr, zMax);  if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pMax) ){    *pnMax = sqlite3_column_int(pMax, 0) + 1;  }  idxFinalize(&rc, pMax);  return rc;}static int idxPopulateOneStat1(  sqlite3expert *p,  sqlite3_stmt *pIndexXInfo,  sqlite3_stmt *pWriteStat,  const char *zTab,  const char *zIdx,  char **pzErr){  char *zCols = 0;  char *zOrder = 0;  char *zQuery = 0;  int nCol = 0;  int i;  sqlite3_stmt *pQuery = 0;  int *aStat = 0;  int rc = SQLITE_OK;  assert( p->iSample>0 );    sqlite3_bind_text(pIndexXInfo, 1, zIdx, -1, SQLITE_STATIC);  while( SQLITE_OK==rc && SQLITE_ROW==sqlite3_step(pIndexXInfo) ){    const char *zComma = zCols==0 ? "" : ", ";    const char *zName = (const char*)sqlite3_column_text(pIndexXInfo, 0);    const char *zColl = (const char*)sqlite3_column_text(pIndexXInfo, 1);    zCols = idxAppendText(&rc, zCols,         "%sx.%Q IS rem(%d, x.%Q) COLLATE %s", zComma, zName, nCol, zName, zColl    );    zOrder = idxAppendText(&rc, zOrder, "%s%d", zComma, ++nCol);  }  sqlite3_reset(pIndexXInfo);  if( rc==SQLITE_OK ){    if( p->iSample==100 ){      zQuery = sqlite3_mprintf(          "SELECT %s FROM %Q x ORDER BY %s", zCols, zTab, zOrder      );    }else{      zQuery = sqlite3_mprintf(          "SELECT %s FROM temp."UNIQUE_TABLE_NAME" x ORDER BY %s", zCols, zOrder      );    }  }  sqlite3_free(zCols);  sqlite3_free(zOrder);    if( rc==SQLITE_OK ){    sqlite3 *dbrem = (p->iSample==100 ? p->db : p->dbv);    rc = idxPrepareStmt(dbrem, &pQuery, pzErr, zQuery);  }  sqlite3_free(zQuery);  if( rc==SQLITE_OK ){    aStat = (int*)idxMalloc(&rc, sizeof(int)*(nCol+1));  }  if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pQuery) ){    IdxHashEntry *pEntry;    char *zStat = 0;    for(i=0; i<=nCol; i++) aStat[i] = 1;    while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pQuery) ){      aStat[0]++;      for(i=0; i<nCol; i++){        if( sqlite3_column_int(pQuery, i)==0 ) break;      }      for(; i<nCol; i++){        aStat[i+1]++;      }    }    if( rc==SQLITE_OK ){      int s0 = aStat[0];      zStat = sqlite3_mprintf("%d", s0);      if( zStat==0 ) rc = SQLITE_NOMEM;      for(i=1; rc==SQLITE_OK && i<=nCol; i++){        zStat = idxAppendText(&rc, zStat, " %d", (s0+aStat[i]/2) / aStat[i]);      }    }    if( rc==SQLITE_OK ){      sqlite3_bind_text(pWriteStat, 1, zTab, -1, SQLITE_STATIC);      sqlite3_bind_text(pWriteStat, 2, zIdx, -1, SQLITE_STATIC);      sqlite3_bind_text(pWriteStat, 3, zStat, -1, SQLITE_STATIC);      sqlite3_step(pWriteStat);      rc = sqlite3_reset(pWriteStat);    }    pEntry = idxHashFind(&p->hIdx, zIdx, STRLEN(zIdx));    if( pEntry ){      assert( pEntry->zVal2==0 );      pEntry->zVal2 = zStat;    }else{      sqlite3_free(zStat);    }  }  sqlite3_free(aStat);  idxFinalize(&rc, pQuery);  return rc;}static int idxBuildSampleTable(sqlite3expert *p, const char *zTab){  int rc;  char *zSql;  rc = sqlite3_exec(p->dbv,"DROP TABLE IF EXISTS temp."UNIQUE_TABLE_NAME,0,0,0);  if( rc!=SQLITE_OK ) return rc;  zSql = sqlite3_mprintf(      "CREATE TABLE temp." UNIQUE_TABLE_NAME " AS SELECT * FROM %Q", zTab  );  if( zSql==0 ) return SQLITE_NOMEM;  rc = sqlite3_exec(p->dbv, zSql, 0, 0, 0);  sqlite3_free(zSql);  return rc;}static int idxPopulateStat1(sqlite3expert *p, char **pzErr){  int rc = SQLITE_OK;  int nMax =0;  struct IdxRemCtx *pCtx = 0;  struct IdxSampleCtx samplectx;   int i;  i64 iPrev = -100000;  sqlite3_stmt *pAllIndex = 0;  sqlite3_stmt *pIndexXInfo = 0;  sqlite3_stmt *pWrite = 0;  const char *zAllIndex =    "SELECT s.rowid, s.name, l.name FROM "    "  sqlite_schema AS s, "    "  pragma_index_list(s.name) AS l "    "WHERE s.type = 'table'";  const char *zIndexXInfo =     "SELECT name, coll FROM pragma_index_xinfo(?) WHERE key";  const char *zWrite = "INSERT INTO sqlite_stat1 VALUES(?, ?, ?)";    if( p->iSample==0 ) return SQLITE_OK;  rc = idxLargestIndex(p->dbm, &nMax, pzErr);  if( nMax<=0 || rc!=SQLITE_OK ) return rc;  rc = sqlite3_exec(p->dbm, "ANALYZE; PRAGMA writable_schema=1", 0, 0, 0);  if( rc==SQLITE_OK ){    int nByte = sizeof(struct IdxRemCtx) + (sizeof(struct IdxRemSlot) * nMax);    pCtx = (struct IdxRemCtx*)idxMalloc(&rc, nByte);  }  if( rc==SQLITE_OK ){    sqlite3 *dbrem = (p->iSample==100 ? p->db : p->dbv);    rc = sqlite3_create_function(        dbrem, "rem", 2, SQLITE_UTF8, (void*)pCtx, idxRemFunc, 0, 0    );  }  if( rc==SQLITE_OK ){    rc = sqlite3_create_function(        p->db, "sample", 0, SQLITE_UTF8, (void*)&samplectx, idxSampleFunc, 0, 0    );  }  if( rc==SQLITE_OK ){    pCtx->nSlot = nMax+1;    rc = idxPrepareStmt(p->dbm, &pAllIndex, pzErr, zAllIndex);  }  if( rc==SQLITE_OK ){    rc = idxPrepareStmt(p->dbm, &pIndexXInfo, pzErr, zIndexXInfo);  }  if( rc==SQLITE_OK ){    rc = idxPrepareStmt(p->dbm, &pWrite, pzErr, zWrite);  }  while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pAllIndex) ){    i64 iRowid = sqlite3_column_int64(pAllIndex, 0);    const char *zTab = (const char*)sqlite3_column_text(pAllIndex, 1);    const char *zIdx = (const char*)sqlite3_column_text(pAllIndex, 2);    if( zTab==0 || zIdx==0 ) continue;    if( p->iSample<100 && iPrev!=iRowid ){      samplectx.target = (double)p->iSample / 100.0;      samplectx.iTarget = p->iSample;      samplectx.nRow = 0.0;      samplectx.nRet = 0.0;      rc = idxBuildSampleTable(p, zTab);      if( rc!=SQLITE_OK ) break;    }    rc = idxPopulateOneStat1(p, pIndexXInfo, pWrite, zTab, zIdx, pzErr);    iPrev = iRowid;  }  if( rc==SQLITE_OK && p->iSample<100 ){    rc = sqlite3_exec(p->dbv,         "DROP TABLE IF EXISTS temp." UNIQUE_TABLE_NAME, 0,0,0    );  }  idxFinalize(&rc, pAllIndex);  idxFinalize(&rc, pIndexXInfo);  idxFinalize(&rc, pWrite);  if( pCtx ){    for(i=0; i<pCtx->nSlot; i++){      sqlite3_free(pCtx->aSlot[i].z);    }    sqlite3_free(pCtx);  }  if( rc==SQLITE_OK ){    rc = sqlite3_exec(p->dbm, "ANALYZE sqlite_schema", 0, 0, 0);  }  sqlite3_exec(p->db, "DROP TABLE IF EXISTS temp."UNIQUE_TABLE_NAME,0,0,0);  return rc;}sqlite3expert *sqlite3_expert_new(sqlite3 *db, char **pzErrmsg){  int rc = SQLITE_OK;  sqlite3expert *pNew;  pNew = (sqlite3expert*)idxMalloc(&rc, sizeof(sqlite3expert));    if( rc==SQLITE_OK ){    pNew->db = db;    pNew->iSample = 100;    rc = sqlite3_open(":memory:", &pNew->dbv);  }  if( rc==SQLITE_OK ){    rc = sqlite3_open(":memory:", &pNew->dbm);    if( rc==SQLITE_OK ){      sqlite3_db_config(pNew->dbm, SQLITE_DBCONFIG_TRIGGER_EQP, 1, (int*)0);    }  }      if( rc==SQLITE_OK ){    sqlite3_stmt *pSql = 0;    rc = idxPrintfPrepareStmt(pNew->db, &pSql, pzErrmsg,         "SELECT sql FROM sqlite_schema WHERE name NOT LIKE 'sqlite_%%'"        " AND sql NOT LIKE 'CREATE VIRTUAL %%'"    );    while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){      const char *zSql = (const char*)sqlite3_column_text(pSql, 0);      if( zSql ) rc = sqlite3_exec(pNew->dbm, zSql, 0, 0, pzErrmsg);    }    idxFinalize(&rc, pSql);  }    if( rc==SQLITE_OK ){    rc = idxCreateVtabSchema(pNew, pzErrmsg);  }    if( rc==SQLITE_OK ){    sqlite3_set_authorizer(pNew->dbv, idxAuthCallback, (void*)pNew);  }    if( rc!=SQLITE_OK ){    sqlite3_expert_destroy(pNew);    pNew = 0;  }  return pNew;}int sqlite3_expert_config(sqlite3expert *p, int op, ...){  int rc = SQLITE_OK;  va_list ap;  va_start(ap, op);  switch( op ){    case EXPERT_CONFIG_SAMPLE: {      int iVal = va_arg(ap, int);      if( iVal<0 ) iVal = 0;      if( iVal>100 ) iVal = 100;      p->iSample = iVal;      break;    }    default:      rc = SQLITE_NOTFOUND;      break;  }  va_end(ap);  return rc;}int sqlite3_expert_sql(  sqlite3expert *p,                 const char *zSql,                 char **pzErr                    ){  IdxScan *pScanOrig = p->pScan;  IdxStatement *pStmtOrig = p->pStatement;  int rc = SQLITE_OK;  const char *zStmt = zSql;  if( p->bRun ) return SQLITE_MISUSE;  while( rc==SQLITE_OK && zStmt && zStmt[0] ){    sqlite3_stmt *pStmt = 0;    rc = sqlite3_prepare_v2(p->dbv, zStmt, -1, &pStmt, &zStmt);    if( rc==SQLITE_OK ){      if( pStmt ){        IdxStatement *pNew;        const char *z = sqlite3_sql(pStmt);        int n = STRLEN(z);        pNew = (IdxStatement*)idxMalloc(&rc, sizeof(IdxStatement) + n+1);        if( rc==SQLITE_OK ){          pNew->zSql = (char*)&pNew[1];          memcpy(pNew->zSql, z, n+1);          pNew->pNext = p->pStatement;          if( p->pStatement ) pNew->iId = p->pStatement->iId+1;          p->pStatement = pNew;        }        sqlite3_finalize(pStmt);      }    }else{      idxDatabaseError(p->dbv, pzErr);    }  }  if( rc!=SQLITE_OK ){    idxScanFree(p->pScan, pScanOrig);    idxStatementFree(p->pStatement, pStmtOrig);    p->pScan = pScanOrig;    p->pStatement = pStmtOrig;  }  return rc;}int sqlite3_expert_analyze(sqlite3expert *p, char **pzErr){  int rc;  IdxHashEntry *pEntry;    rc = idxProcessTriggers(p, pzErr);    if( rc==SQLITE_OK ){    rc = idxCreateCandidates(p);  }else if ( rc==SQLITE_BUSY_TIMEOUT ){    if( pzErr )      *pzErr = sqlite3_mprintf("Cannot find a unique index name to propose.");    return rc;  }    if( rc==SQLITE_OK ){    rc = idxPopulateStat1(p, pzErr);  }    for(pEntry=p->hIdx.pFirst; pEntry; pEntry=pEntry->pNext){    p->zCandidates = idxAppendText(&rc, p->zCandidates,         "%s;%s%s\n", pEntry->zVal,         pEntry->zVal2 ? " -- stat1: " : "", pEntry->zVal2    );  }    if( rc==SQLITE_OK ){    rc = idxFindIndexes(p, pzErr);  }  if( rc==SQLITE_OK ){    p->bRun = 1;  }  return rc;}int sqlite3_expert_count(sqlite3expert *p){  int nRet = 0;  if( p->pStatement ) nRet = p->pStatement->iId+1;  return nRet;}const char *sqlite3_expert_report(sqlite3expert *p, int iStmt, int eReport){  const char *zRet = 0;  IdxStatement *pStmt;  if( p->bRun==0 ) return 0;  for(pStmt=p->pStatement; pStmt && pStmt->iId!=iStmt; pStmt=pStmt->pNext);  switch( eReport ){    case EXPERT_REPORT_SQL:      if( pStmt ) zRet = pStmt->zSql;      break;    case EXPERT_REPORT_INDEXES:      if( pStmt ) zRet = pStmt->zIdx;      break;    case EXPERT_REPORT_PLAN:      if( pStmt ) zRet = pStmt->zEQP;      break;    case EXPERT_REPORT_CANDIDATES:      zRet = p->zCandidates;      break;  }  return zRet;}void sqlite3_expert_destroy(sqlite3expert *p){  if( p ){    sqlite3_close(p->dbm);    sqlite3_close(p->dbv);    idxScanFree(p->pScan, 0);    idxStatementFree(p->pStatement, 0);    idxTableFree(p->pTable);    idxWriteFree(p->pWrite);    idxHashClear(&p->hIdx);    sqlite3_free(p->zCandidates);    sqlite3_free(p);  }}#endif #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)#if !defined(SQLITEINT_H) #endifSQLITE_EXTENSION_INIT1#include <string.h>#include <assert.h>#define DBDATA_PADDING_BYTES 100 typedef struct DbdataTable DbdataTable;typedef struct DbdataCursor DbdataCursor;struct DbdataCursor {  sqlite3_vtab_cursor base;         sqlite3_stmt *pStmt;              int iPgno;                        u8 *aPage;                        int nPage;                        int nCell;                        int iCell;                        int bOnePage;                     int szDb;  sqlite3_int64 iRowid;    u8 *pRec;                         int nRec;                         int nHdr;                         int iField;                       u8 *pHdrPtr;  u8 *pPtr;    sqlite3_int64 iIntkey;          };struct DbdataTable {  sqlite3_vtab base;                sqlite3 *db;                      sqlite3_stmt *pStmt;              int bPtr;                       };#define DBDATA_COLUMN_PGNO        0#define DBDATA_COLUMN_CELL        1#define DBDATA_COLUMN_FIELD       2#define DBDATA_COLUMN_VALUE       3#define DBDATA_COLUMN_SCHEMA      4#define DBDATA_SCHEMA             \      "CREATE TABLE x("           \      "  pgno INTEGER,"           \      "  cell INTEGER,"           \      "  field INTEGER,"          \      "  value ANY,"              \      "  schema TEXT HIDDEN"      \      ")"#define DBPTR_COLUMN_PGNO         0#define DBPTR_COLUMN_CHILD        1#define DBPTR_COLUMN_SCHEMA       2#define DBPTR_SCHEMA              \      "CREATE TABLE x("           \      "  pgno INTEGER,"           \      "  child INTEGER,"          \      "  schema TEXT HIDDEN"      \      ")"static int dbdataConnect(  sqlite3 *db,  void *pAux,  int argc, const char *const*argv,  sqlite3_vtab **ppVtab,  char **pzErr){  DbdataTable *pTab = 0;  int rc = sqlite3_declare_vtab(db, pAux ? DBPTR_SCHEMA : DBDATA_SCHEMA);  if( rc==SQLITE_OK ){    pTab = (DbdataTable*)sqlite3_malloc64(sizeof(DbdataTable));    if( pTab==0 ){      rc = SQLITE_NOMEM;    }else{      memset(pTab, 0, sizeof(DbdataTable));      pTab->db = db;      pTab->bPtr = (pAux!=0);    }  }  *ppVtab = (sqlite3_vtab*)pTab;  return rc;}static int dbdataDisconnect(sqlite3_vtab *pVtab){  DbdataTable *pTab = (DbdataTable*)pVtab;  if( pTab ){    sqlite3_finalize(pTab->pStmt);    sqlite3_free(pVtab);  }  return SQLITE_OK;}static int dbdataBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdx){  DbdataTable *pTab = (DbdataTable*)tab;  int i;  int iSchema = -1;  int iPgno = -1;  int colSchema = (pTab->bPtr ? DBPTR_COLUMN_SCHEMA : DBDATA_COLUMN_SCHEMA);  for(i=0; i<pIdx->nConstraint; i++){    struct sqlite3_index_constraint *p = &pIdx->aConstraint[i];    if( p->op==SQLITE_INDEX_CONSTRAINT_EQ ){      if( p->iColumn==colSchema ){        if( p->usable==0 ) return SQLITE_CONSTRAINT;        iSchema = i;      }      if( p->iColumn==DBDATA_COLUMN_PGNO && p->usable ){        iPgno = i;      }    }  }  if( iSchema>=0 ){    pIdx->aConstraintUsage[iSchema].argvIndex = 1;    pIdx->aConstraintUsage[iSchema].omit = 1;  }  if( iPgno>=0 ){    pIdx->aConstraintUsage[iPgno].argvIndex = 1 + (iSchema>=0);    pIdx->aConstraintUsage[iPgno].omit = 1;    pIdx->estimatedCost = 100;    pIdx->estimatedRows =  50;    if( pTab->bPtr==0 && pIdx->nOrderBy && pIdx->aOrderBy[0].desc==0 ){      int iCol = pIdx->aOrderBy[0].iColumn;      if( pIdx->nOrderBy==1 ){        pIdx->orderByConsumed = (iCol==0 || iCol==1);      }else if( pIdx->nOrderBy==2 && pIdx->aOrderBy[1].desc==0 && iCol==0 ){        pIdx->orderByConsumed = (pIdx->aOrderBy[1].iColumn==1);      }    }  }else{    pIdx->estimatedCost = 100000000;    pIdx->estimatedRows = 1000000000;  }  pIdx->idxNum = (iSchema>=0 ? 0x01 : 0x00) | (iPgno>=0 ? 0x02 : 0x00);  return SQLITE_OK;}static int dbdataOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){  DbdataCursor *pCsr;  pCsr = (DbdataCursor*)sqlite3_malloc64(sizeof(DbdataCursor));  if( pCsr==0 ){    return SQLITE_NOMEM;  }else{    memset(pCsr, 0, sizeof(DbdataCursor));    pCsr->base.pVtab = pVTab;  }  *ppCursor = (sqlite3_vtab_cursor *)pCsr;  return SQLITE_OK;}static void dbdataResetCursor(DbdataCursor *pCsr){  DbdataTable *pTab = (DbdataTable*)(pCsr->base.pVtab);  if( pTab->pStmt==0 ){    pTab->pStmt = pCsr->pStmt;  }else{    sqlite3_finalize(pCsr->pStmt);  }  pCsr->pStmt = 0;  pCsr->iPgno = 1;  pCsr->iCell = 0;  pCsr->iField = 0;  pCsr->bOnePage = 0;  sqlite3_free(pCsr->aPage);  sqlite3_free(pCsr->pRec);  pCsr->pRec = 0;  pCsr->aPage = 0;}static int dbdataClose(sqlite3_vtab_cursor *pCursor){  DbdataCursor *pCsr = (DbdataCursor*)pCursor;  dbdataResetCursor(pCsr);  sqlite3_free(pCsr);  return SQLITE_OK;}static unsigned int get_uint16(unsigned char *a){  return (a[0]<<8)|a[1];}static unsigned int get_uint32(unsigned char *a){  return ((unsigned int)a[0]<<24)       | ((unsigned int)a[1]<<16)       | ((unsigned int)a[2]<<8)       | ((unsigned int)a[3]);}static int dbdataLoadPage(  DbdataCursor *pCsr,               unsigned int pgno,                u8 **ppPage,                      int *pnPage                     ){  int rc2;  int rc = SQLITE_OK;  sqlite3_stmt *pStmt = pCsr->pStmt;  *ppPage = 0;  *pnPage = 0;  sqlite3_bind_int64(pStmt, 2, pgno);  if( SQLITE_ROW==sqlite3_step(pStmt) ){    int nCopy = sqlite3_column_bytes(pStmt, 0);    if( nCopy>0 ){      u8 *pPage;      pPage = (u8*)sqlite3_malloc64(nCopy + DBDATA_PADDING_BYTES);      if( pPage==0 ){        rc = SQLITE_NOMEM;      }else{        const u8 *pCopy = sqlite3_column_blob(pStmt, 0);        memcpy(pPage, pCopy, nCopy);        memset(&pPage[nCopy], 0, DBDATA_PADDING_BYTES);      }      *ppPage = pPage;      *pnPage = nCopy;    }  }  rc2 = sqlite3_reset(pStmt);  if( rc==SQLITE_OK ) rc = rc2;  return rc;}static int dbdataGetVarint(const u8 *z, sqlite3_int64 *pVal){  sqlite3_int64 v = 0;  int i;  for(i=0; i<8; i++){    v = (v<<7) + (z[i]&0x7f);    if( (z[i]&0x80)==0 ){ *pVal = v; return i+1; }  }  v = (v<<8) + (z[i]&0xff);  *pVal = v;  return 9;}static int dbdataValueBytes(int eType){  switch( eType ){    case 0: case 8: case 9:    case 10: case 11:      return 0;    case 1:      return 1;    case 2:      return 2;    case 3:      return 3;    case 4:      return 4;    case 5:      return 6;    case 6:    case 7:      return 8;    default:      if( eType>0 ){        return ((eType-12) / 2);      }      return 0;  }}static void dbdataValue(  sqlite3_context *pCtx,   int eType,   u8 *pData,  int nData){  if( eType>=0 && dbdataValueBytes(eType)<=nData ){    switch( eType ){      case 0:       case 10:       case 11:         sqlite3_result_null(pCtx);        break;            case 8:         sqlite3_result_int(pCtx, 0);        break;      case 9:        sqlite3_result_int(pCtx, 1);        break;        case 1: case 2: case 3: case 4: case 5: case 6: case 7: {        sqlite3_uint64 v = (signed char)pData[0];        pData++;        switch( eType ){          case 7:          case 6:  v = (v<<16) + (pData[0]<<8) + pData[1];  pData += 2;          case 5:  v = (v<<16) + (pData[0]<<8) + pData[1];  pData += 2;          case 4:  v = (v<<8) + pData[0];  pData++;          case 3:  v = (v<<8) + pData[0];  pData++;          case 2:  v = (v<<8) + pData[0];  pData++;        }          if( eType==7 ){          double r;          memcpy(&r, &v, sizeof(r));          sqlite3_result_double(pCtx, r);        }else{          sqlite3_result_int64(pCtx, (sqlite3_int64)v);        }        break;      }        default: {        int n = ((eType-12) / 2);        if( eType % 2 ){          sqlite3_result_text(pCtx, (const char*)pData, n, SQLITE_TRANSIENT);        }else{          sqlite3_result_blob(pCtx, pData, n, SQLITE_TRANSIENT);        }      }    }  }}static int dbdataNext(sqlite3_vtab_cursor *pCursor){  DbdataCursor *pCsr = (DbdataCursor*)pCursor;  DbdataTable *pTab = (DbdataTable*)pCursor->pVtab;  pCsr->iRowid++;  while( 1 ){    int rc;    int iOff = (pCsr->iPgno==1 ? 100 : 0);    int bNextPage = 0;    if( pCsr->aPage==0 ){      while( 1 ){        if( pCsr->bOnePage==0 && pCsr->iPgno>pCsr->szDb ) return SQLITE_OK;        rc = dbdataLoadPage(pCsr, pCsr->iPgno, &pCsr->aPage, &pCsr->nPage);        if( rc!=SQLITE_OK ) return rc;        if( pCsr->aPage ) break;        pCsr->iPgno++;      }      pCsr->iCell = pTab->bPtr ? -2 : 0;      pCsr->nCell = get_uint16(&pCsr->aPage[iOff+3]);    }    if( pTab->bPtr ){      if( pCsr->aPage[iOff]!=0x02 && pCsr->aPage[iOff]!=0x05 ){        pCsr->iCell = pCsr->nCell;      }      pCsr->iCell++;      if( pCsr->iCell>=pCsr->nCell ){        sqlite3_free(pCsr->aPage);        pCsr->aPage = 0;        if( pCsr->bOnePage ) return SQLITE_OK;        pCsr->iPgno++;      }else{        return SQLITE_OK;      }    }else{            if( pCsr->pRec==0 ){        int bHasRowid = 0;        int nPointer = 0;        sqlite3_int64 nPayload = 0;        sqlite3_int64 nHdr = 0;        int iHdr;        int U, X;        int nLocal;          switch( pCsr->aPage[iOff] ){          case 0x02:            nPointer = 4;            break;          case 0x0a:            break;          case 0x0d:            bHasRowid = 1;            break;          default:                        pCsr->iCell = pCsr->nCell;            break;        }        if( pCsr->iCell>=pCsr->nCell ){          bNextPage = 1;        }else{            iOff += 8 + nPointer + pCsr->iCell*2;          if( iOff>pCsr->nPage ){            bNextPage = 1;          }else{            iOff = get_uint16(&pCsr->aPage[iOff]);          }                        iOff += nPointer;                        if( bNextPage || iOff>pCsr->nPage ){            bNextPage = 1;          }else{            iOff += dbdataGetVarint(&pCsr->aPage[iOff], &nPayload);          }                        if( bHasRowid && !bNextPage && iOff<pCsr->nPage ){            iOff += dbdataGetVarint(&pCsr->aPage[iOff], &pCsr->iIntkey);          }                        U = pCsr->nPage;          if( bHasRowid ){            X = U-35;          }else{            X = ((U-12)*64/255)-23;          }          if( nPayload<=X ){            nLocal = nPayload;          }else{            int M, K;            M = ((U-12)*32/255)-23;            K = M+((nPayload-M)%(U-4));            if( K<=X ){              nLocal = K;            }else{              nLocal = M;            }          }          if( bNextPage || nLocal+iOff>pCsr->nPage ){            bNextPage = 1;          }else{                        pCsr->pRec = (u8*)sqlite3_malloc64(nPayload+DBDATA_PADDING_BYTES);            if( pCsr->pRec==0 ) return SQLITE_NOMEM;            memset(pCsr->pRec, 0, nPayload+DBDATA_PADDING_BYTES);            pCsr->nRec = nPayload;                        memcpy(pCsr->pRec, &pCsr->aPage[iOff], nLocal);            iOff += nLocal;                        if( nPayload>nLocal ){              sqlite3_int64 nRem = nPayload - nLocal;              unsigned int pgnoOvfl = get_uint32(&pCsr->aPage[iOff]);              while( nRem>0 ){                u8 *aOvfl = 0;                int nOvfl = 0;                int nCopy;                rc = dbdataLoadPage(pCsr, pgnoOvfl, &aOvfl, &nOvfl);                assert( rc!=SQLITE_OK || aOvfl==0 || nOvfl==pCsr->nPage );                if( rc!=SQLITE_OK ) return rc;                if( aOvfl==0 ) break;                nCopy = U-4;                if( nCopy>nRem ) nCopy = nRem;                memcpy(&pCsr->pRec[nPayload-nRem], &aOvfl[4], nCopy);                nRem -= nCopy;                pgnoOvfl = get_uint32(aOvfl);                sqlite3_free(aOvfl);              }            }                iHdr = dbdataGetVarint(pCsr->pRec, &nHdr);            pCsr->nHdr = nHdr;            pCsr->pHdrPtr = &pCsr->pRec[iHdr];            pCsr->pPtr = &pCsr->pRec[pCsr->nHdr];            pCsr->iField = (bHasRowid ? -1 : 0);          }        }      }else{        pCsr->iField++;        if( pCsr->iField>0 ){          sqlite3_int64 iType;          if( pCsr->pHdrPtr>&pCsr->pRec[pCsr->nRec] ){            bNextPage = 1;          }else{            pCsr->pHdrPtr += dbdataGetVarint(pCsr->pHdrPtr, &iType);            pCsr->pPtr += dbdataValueBytes(iType);          }        }      }      if( bNextPage ){        sqlite3_free(pCsr->aPage);        sqlite3_free(pCsr->pRec);        pCsr->aPage = 0;        pCsr->pRec = 0;        if( pCsr->bOnePage ) return SQLITE_OK;        pCsr->iPgno++;      }else{        if( pCsr->iField<0 || pCsr->pHdrPtr<&pCsr->pRec[pCsr->nHdr] ){          return SQLITE_OK;        }                sqlite3_free(pCsr->pRec);        pCsr->pRec = 0;        pCsr->iCell++;      }    }  }  assert( !"can't get here" );  return SQLITE_OK;}static int dbdataEof(sqlite3_vtab_cursor *pCursor){  DbdataCursor *pCsr = (DbdataCursor*)pCursor;  return pCsr->aPage==0;}static int dbdataDbsize(DbdataCursor *pCsr, const char *zSchema){  DbdataTable *pTab = (DbdataTable*)pCsr->base.pVtab;  char *zSql = 0;  int rc, rc2;  sqlite3_stmt *pStmt = 0;  zSql = sqlite3_mprintf("PRAGMA %Q.page_count", zSchema);  if( zSql==0 ) return SQLITE_NOMEM;  rc = sqlite3_prepare_v2(pTab->db, zSql, -1, &pStmt, 0);  sqlite3_free(zSql);  if( rc==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){    pCsr->szDb = sqlite3_column_int(pStmt, 0);  }  rc2 = sqlite3_finalize(pStmt);  if( rc==SQLITE_OK ) rc = rc2;  return rc;}static int dbdataFilter(  sqlite3_vtab_cursor *pCursor,   int idxNum, const char *idxStr,  int argc, sqlite3_value **argv){  DbdataCursor *pCsr = (DbdataCursor*)pCursor;  DbdataTable *pTab = (DbdataTable*)pCursor->pVtab;  int rc = SQLITE_OK;  const char *zSchema = "main";  dbdataResetCursor(pCsr);  assert( pCsr->iPgno==1 );  if( idxNum & 0x01 ){    zSchema = (const char*)sqlite3_value_text(argv[0]);  }  if( idxNum & 0x02 ){    pCsr->iPgno = sqlite3_value_int(argv[(idxNum & 0x01)]);    pCsr->bOnePage = 1;  }else{    pCsr->nPage = dbdataDbsize(pCsr, zSchema);    rc = dbdataDbsize(pCsr, zSchema);  }  if( rc==SQLITE_OK ){    if( pTab->pStmt ){      pCsr->pStmt = pTab->pStmt;      pTab->pStmt = 0;    }else{      rc = sqlite3_prepare_v2(pTab->db,           "SELECT data FROM sqlite_dbpage(?) WHERE pgno=?", -1,          &pCsr->pStmt, 0      );    }  }  if( rc==SQLITE_OK ){    rc = sqlite3_bind_text(pCsr->pStmt, 1, zSchema, -1, SQLITE_TRANSIENT);  }else{    pTab->base.zErrMsg = sqlite3_mprintf("%s", sqlite3_errmsg(pTab->db));  }  if( rc==SQLITE_OK ){    rc = dbdataNext(pCursor);  }  return rc;}static int dbdataColumn(  sqlite3_vtab_cursor *pCursor,   sqlite3_context *ctx,   int i){  DbdataCursor *pCsr = (DbdataCursor*)pCursor;  DbdataTable *pTab = (DbdataTable*)pCursor->pVtab;  if( pTab->bPtr ){    switch( i ){      case DBPTR_COLUMN_PGNO:        sqlite3_result_int64(ctx, pCsr->iPgno);        break;      case DBPTR_COLUMN_CHILD: {        int iOff = pCsr->iPgno==1 ? 100 : 0;        if( pCsr->iCell<0 ){          iOff += 8;        }else{          iOff += 12 + pCsr->iCell*2;          if( iOff>pCsr->nPage ) return SQLITE_OK;          iOff = get_uint16(&pCsr->aPage[iOff]);        }        if( iOff<=pCsr->nPage ){          sqlite3_result_int64(ctx, get_uint32(&pCsr->aPage[iOff]));        }        break;      }    }  }else{    switch( i ){      case DBDATA_COLUMN_PGNO:        sqlite3_result_int64(ctx, pCsr->iPgno);        break;      case DBDATA_COLUMN_CELL:        sqlite3_result_int(ctx, pCsr->iCell);        break;      case DBDATA_COLUMN_FIELD:        sqlite3_result_int(ctx, pCsr->iField);        break;      case DBDATA_COLUMN_VALUE: {        if( pCsr->iField<0 ){          sqlite3_result_int64(ctx, pCsr->iIntkey);        }else{          sqlite3_int64 iType;          dbdataGetVarint(pCsr->pHdrPtr, &iType);          dbdataValue(              ctx, iType, pCsr->pPtr, &pCsr->pRec[pCsr->nRec] - pCsr->pPtr          );        }        break;      }    }  }  return SQLITE_OK;}static int dbdataRowid(sqlite3_vtab_cursor *pCursor, sqlite_int64 *pRowid){  DbdataCursor *pCsr = (DbdataCursor*)pCursor;  *pRowid = pCsr->iRowid;  return SQLITE_OK;}static int sqlite3DbdataRegister(sqlite3 *db){  static sqlite3_module dbdata_module = {    0,                                0,                                dbdataConnect,                    dbdataBestIndex,                  dbdataDisconnect,                 0,                                dbdataOpen,                       dbdataClose,                      dbdataFilter,                     dbdataNext,                       dbdataEof,                        dbdataColumn,                     dbdataRowid,                      0,                                0,                                0,                                0,                                0,                                0,                                0,                                0,                                0,                                0,                                0                               };  int rc = sqlite3_create_module(db, "sqlite_dbdata", &dbdata_module, 0);  if( rc==SQLITE_OK ){    rc = sqlite3_create_module(db, "sqlite_dbptr", &dbdata_module, (void*)1);  }  return rc;}#ifdef _WIN32#endifint sqlite3_dbdata_init(  sqlite3 *db,   char **pzErrMsg,   const sqlite3_api_routines *pApi){  SQLITE_EXTENSION_INIT2(pApi);  return sqlite3DbdataRegister(db);}#endif#if defined(SQLITE_ENABLE_SESSION)typedef struct OpenSession OpenSession;struct OpenSession {  char *zName;               int nFilter;               char **azFilter;           sqlite3_session *p;      };#endiftypedef struct ExpertInfo ExpertInfo;struct ExpertInfo {  sqlite3expert *pExpert;  int bVerbose;};typedef struct EQPGraphRow EQPGraphRow;struct EQPGraphRow {  int iEqpId;             int iParentId;          EQPGraphRow *pNext;     char zText[1];        };typedef struct EQPGraph EQPGraph;struct EQPGraph {  EQPGraphRow *pRow;      EQPGraphRow *pLast;     char zPrefix[100];    };typedef struct ColModeOpts {  int iWrap;              u8 bQuote;              u8 bWordWrap;         } ColModeOpts;#define ColModeOpts_default { 60, 0, 0 }#define ColModeOpts_default_qbox { 60, 1, 0 }typedef struct ShellState ShellState;struct ShellState {  sqlite3 *db;             u8 autoExplain;          u8 autoEQP;              u8 autoEQPtest;          u8 autoEQPtrace;         u8 scanstatsOn;          u8 openMode;             u8 doXdgOpen;            u8 nEqpLevel;            u8 eTraceType;           u8 bSafeMode;            u8 bSafeModePersist;     ColModeOpts cmOpts;      unsigned statsOn;        unsigned mEqpLines;      int inputNesting;        int outCount;            int cnt;                 int lineno;              int openFlags;           FILE *in;                FILE *out;               FILE *traceOut;          int nErr;                int mode;                int modePrior;           int cMode;               int normalMode;          int writableSchema;      int showHeader;          int nCheck;              unsigned nProgress;      unsigned mxProgress;     unsigned flgProgress;    unsigned shellFlgs;      unsigned priorShFlgs;    sqlite3_int64 szMax;     char *zDestTable;        char *zTempFile;         char zTestcase[30];      char colSeparator[20];   char rowSeparator[20];   char colSepPrior[20];    char rowSepPrior[20];    int *colWidth;           int *actualWidth;        int nWidth;              char nullValue[20];      char outfile[FILENAME_MAX];   sqlite3_stmt *pStmt;     FILE *pLog;              struct AuxDb {             sqlite3 *db;                   const char *zDbFilename;       char *zFreeOnClose;        #if defined(SQLITE_ENABLE_SESSION)    int nSession;                  OpenSession aSession[4];   #endif  } aAuxDb[5],               *pAuxDb;               int *aiIndent;           int nIndent;             int iIndent;             char *zNonce;            EQPGraph sGraph;         ExpertInfo expert;     };#define AUTOEQP_off      0           #define AUTOEQP_on       1           #define AUTOEQP_trigger  2           #define AUTOEQP_full     3           #define SHELL_OPEN_UNSPEC      0      #define SHELL_OPEN_NORMAL      1      #define SHELL_OPEN_APPENDVFS   2      #define SHELL_OPEN_ZIPFILE     3      #define SHELL_OPEN_READONLY    4      #define SHELL_OPEN_DESERIALIZE 5      #define SHELL_OPEN_HEXDB       6      #define SHELL_TRACE_PLAIN      0      #define SHELL_TRACE_EXPANDED   1      #define SHELL_TRACE_NORMALIZED 2      #define SHELL_PROGRESS_QUIET 0x01  #define SHELL_PROGRESS_RESET 0x02  #define SHELL_PROGRESS_ONCE  0x04  #define SHFLG_Pagecache      0x00000001 #define SHFLG_Lookaside      0x00000002 #define SHFLG_Backslash      0x00000004 #define SHFLG_PreserveRowid  0x00000008 #define SHFLG_Newlines       0x00000010 #define SHFLG_CountChanges   0x00000020 #define SHFLG_Echo           0x00000040 #define SHFLG_HeaderSet      0x00000080 #define SHFLG_DumpDataOnly   0x00000100 #define SHFLG_DumpNoSys      0x00000200 #define ShellHasFlag(P,X)    (((P)->shellFlgs & (X))!=0)#define ShellSetFlag(P,X)    ((P)->shellFlgs|=(X))#define ShellClearFlag(P,X)  ((P)->shellFlgs&=(~(X)))#define MODE_Line     0  #define MODE_Column   1  #define MODE_List     2  #define MODE_Semi     3  #define MODE_Html     4  #define MODE_Insert   5  #define MODE_Quote    6  #define MODE_Tcl      7  #define MODE_Csv      8  #define MODE_Explain  9  #define MODE_Ascii   10  #define MODE_Pretty  11  #define MODE_EQP     12  #define MODE_Json    13  #define MODE_Markdown 14 #define MODE_Table   15  #define MODE_Box     16  #define MODE_Count   17  #define MODE_Off     18  static const char *modeDescr[] = {  "line",  "column",  "list",  "semi",  "html",  "insert",  "quote",  "tcl",  "csv",  "explain",  "ascii",  "prettyprint",  "eqp",  "json",  "markdown",  "table",  "box",  "count",  "off"};#define SEP_Column    "|"#define SEP_Row       "\n"#define SEP_Tab       "\t"#define SEP_Space     " "#define SEP_Comma     ","#define SEP_CrLf      "\r\n"#define SEP_Unit      "\x1F"#define SEP_Record    "\x1E"#define MAX_INPUT_NESTING 25static void shellLog(void *pArg, int iErrCode, const char *zMsg){  ShellState *p = (ShellState*)pArg;  if( p->pLog==0 ) return;  utf8_printf(p->pLog, "(%d) %s\n", iErrCode, zMsg);  fflush(p->pLog);}static void shellPutsFunc(  sqlite3_context *pCtx,  int nVal,  sqlite3_value **apVal){  ShellState *p = (ShellState*)sqlite3_user_data(pCtx);  (void)nVal;  utf8_printf(p->out, "%s\n", sqlite3_value_text(apVal[0]));  sqlite3_result_value(pCtx, apVal[0]);}static void failIfSafeMode(  ShellState *p,  const char *zErrMsg,  ...){  if( p->bSafeMode ){    va_list ap;    char *zMsg;    va_start(ap, zErrMsg);    zMsg = sqlite3_vmprintf(zErrMsg, ap);    va_end(ap);    raw_printf(stderr, "line %d: ", p->lineno);    utf8_printf(stderr, "%s\n", zMsg);    exit(1);  }}#ifndef SQLITE_NOHAVE_SYSTEMstatic void editFunc(  sqlite3_context *context,  int argc,  sqlite3_value **argv){  const char *zEditor;  char *zTempFile = 0;  sqlite3 *db;  char *zCmd = 0;  int bBin;  int rc;  int hasCRNL = 0;  FILE *f = 0;  sqlite3_int64 sz;  sqlite3_int64 x;  unsigned char *p = 0;  if( argc==2 ){    zEditor = (const char*)sqlite3_value_text(argv[1]);  }else{    zEditor = getenv("VISUAL");  }  if( zEditor==0 ){    sqlite3_result_error(context, "no editor for edit()", -1);    return;  }  if( sqlite3_value_type(argv[0])==SQLITE_NULL ){    sqlite3_result_error(context, "NULL input to edit()", -1);    return;  }  db = sqlite3_context_db_handle(context);  zTempFile = 0;  sqlite3_file_control(db, 0, SQLITE_FCNTL_TEMPFILENAME, &zTempFile);  if( zTempFile==0 ){    sqlite3_uint64 r = 0;    sqlite3_randomness(sizeof(r), &r);    zTempFile = sqlite3_mprintf("temp%llx", r);    if( zTempFile==0 ){      sqlite3_result_error_nomem(context);      return;    }  }  bBin = sqlite3_value_type(argv[0])==SQLITE_BLOB;    f = fopen(zTempFile, bBin ? "wb" : "w");  if( f==0 ){    sqlite3_result_error(context, "edit() cannot open temp file", -1);    goto edit_func_end;  }  sz = sqlite3_value_bytes(argv[0]);  if( bBin ){    x = fwrite(sqlite3_value_blob(argv[0]), 1, (size_t)sz, f);  }else{    const char *z = (const char*)sqlite3_value_text(argv[0]);        if( z && strstr(z,"\r\n")!=0 ) hasCRNL = 1;    x = fwrite(sqlite3_value_text(argv[0]), 1, (size_t)sz, f);  }  fclose(f);  f = 0;  if( x!=sz ){    sqlite3_result_error(context, "edit() could not write the whole file", -1);    goto edit_func_end;  }  zCmd = sqlite3_mprintf("%s \"%s\"", zEditor, zTempFile);  if( zCmd==0 ){    sqlite3_result_error_nomem(context);    goto edit_func_end;  }  rc = system(zCmd);  sqlite3_free(zCmd);  if( rc ){    sqlite3_result_error(context, "EDITOR returned non-zero", -1);    goto edit_func_end;  }  f = fopen(zTempFile, "rb");  if( f==0 ){    sqlite3_result_error(context,      "edit() cannot reopen temp file after edit", -1);    goto edit_func_end;  }  fseek(f, 0, SEEK_END);  sz = ftell(f);  rewind(f);  p = sqlite3_malloc64( sz+1 );  if( p==0 ){    sqlite3_result_error_nomem(context);    goto edit_func_end;  }  x = fread(p, 1, (size_t)sz, f);  fclose(f);  f = 0;  if( x!=sz ){    sqlite3_result_error(context, "could not read back the whole file", -1);    goto edit_func_end;  }  if( bBin ){    sqlite3_result_blob64(context, p, sz, sqlite3_free);  }else{    sqlite3_int64 i, j;    if( hasCRNL ){          }else{            for(i=j=0; i<sz; i++){        if( p[i]=='\r' && p[i+1]=='\n' ) i++;        p[j++] = p[i];      }      sz = j;      p[sz] = 0;    }     sqlite3_result_text64(context, (const char*)p, sz,                          sqlite3_free, SQLITE_UTF8);  }  p = 0;edit_func_end:  if( f ) fclose(f);  unlink(zTempFile);  sqlite3_free(zTempFile);  sqlite3_free(p);}#endif static void outputModePush(ShellState *p){  p->modePrior = p->mode;  p->priorShFlgs = p->shellFlgs;  memcpy(p->colSepPrior, p->colSeparator, sizeof(p->colSeparator));  memcpy(p->rowSepPrior, p->rowSeparator, sizeof(p->rowSeparator));}static void outputModePop(ShellState *p){  p->mode = p->modePrior;  p->shellFlgs = p->priorShFlgs;  memcpy(p->colSeparator, p->colSepPrior, sizeof(p->colSeparator));  memcpy(p->rowSeparator, p->rowSepPrior, sizeof(p->rowSeparator));}static void output_hex_blob(FILE *out, const void *pBlob, int nBlob){  int i;  char *zBlob = (char *)pBlob;  raw_printf(out,"X'");  for(i=0; i<nBlob; i++){ raw_printf(out,"%02x",zBlob[i]&0xff); }  raw_printf(out,"'");}static const char *unused_string(  const char *z,                      const char *zA, const char *zB,     char *zBuf                        ){  unsigned i = 0;  if( strstr(z, zA)==0 ) return zA;  if( strstr(z, zB)==0 ) return zB;  do{    sqlite3_snprintf(20,zBuf,"(%s%u)", zA, i++);  }while( strstr(z,zBuf)!=0 );  return zBuf;}static void output_quoted_string(FILE *out, const char *z){  int i;  char c;  setBinaryMode(out, 1);  for(i=0; (c = z[i])!=0 && c!='\''; i++){}  if( c==0 ){    utf8_printf(out,"'%s'",z);  }else{    raw_printf(out, "'");    while( *z ){      for(i=0; (c = z[i])!=0 && c!='\''; i++){}      if( c=='\'' ) i++;      if( i ){        utf8_printf(out, "%.*s", i, z);        z += i;      }      if( c=='\'' ){        raw_printf(out, "'");        continue;      }      if( c==0 ){        break;      }      z++;    }    raw_printf(out, "'");  }  setTextMode(out, 1);}static void output_quoted_escaped_string(FILE *out, const char *z){  int i;  char c;  setBinaryMode(out, 1);  for(i=0; (c = z[i])!=0 && c!='\'' && c!='\n' && c!='\r'; i++){}  if( c==0 ){    utf8_printf(out,"'%s'",z);  }else{    const char *zNL = 0;    const char *zCR = 0;    int nNL = 0;    int nCR = 0;    char zBuf1[20], zBuf2[20];    for(i=0; z[i]; i++){      if( z[i]=='\n' ) nNL++;      if( z[i]=='\r' ) nCR++;    }    if( nNL ){      raw_printf(out, "replace(");      zNL = unused_string(z, "\\n", "\\012", zBuf1);    }    if( nCR ){      raw_printf(out, "replace(");      zCR = unused_string(z, "\\r", "\\015", zBuf2);    }    raw_printf(out, "'");    while( *z ){      for(i=0; (c = z[i])!=0 && c!='\n' && c!='\r' && c!='\''; i++){}      if( c=='\'' ) i++;      if( i ){        utf8_printf(out, "%.*s", i, z);        z += i;      }      if( c=='\'' ){        raw_printf(out, "'");        continue;      }      if( c==0 ){        break;      }      z++;      if( c=='\n' ){        raw_printf(out, "%s", zNL);        continue;      }      raw_printf(out, "%s", zCR);    }    raw_printf(out, "'");    if( nCR ){      raw_printf(out, ",'%s',char(13))", zCR);    }    if( nNL ){      raw_printf(out, ",'%s',char(10))", zNL);    }  }  setTextMode(out, 1);}static void output_c_string(FILE *out, const char *z){  unsigned int c;  fputc('"', out);  while( (c = *(z++))!=0 ){    if( c=='\\' ){      fputc(c, out);      fputc(c, out);    }else if( c=='"' ){      fputc('\\', out);      fputc('"', out);    }else if( c=='\t' ){      fputc('\\', out);      fputc('t', out);    }else if( c=='\n' ){      fputc('\\', out);      fputc('n', out);    }else if( c=='\r' ){      fputc('\\', out);      fputc('r', out);    }else if( !isprint(c&0xff) ){      raw_printf(out, "\\%03o", c&0xff);    }else{      fputc(c, out);    }  }  fputc('"', out);}static void output_json_string(FILE *out, const char *z, int n){  unsigned int c;  if( n<0 ) n = (int)strlen(z);  fputc('"', out);  while( n-- ){    c = *(z++);    if( c=='\\' || c=='"' ){      fputc('\\', out);      fputc(c, out);    }else if( c<=0x1f ){      fputc('\\', out);      if( c=='\b' ){        fputc('b', out);      }else if( c=='\f' ){        fputc('f', out);      }else if( c=='\n' ){        fputc('n', out);      }else if( c=='\r' ){        fputc('r', out);      }else if( c=='\t' ){        fputc('t', out);      }else{         raw_printf(out, "u%04x",c);      }    }else{      fputc(c, out);    }  }  fputc('"', out);}static void output_html_string(FILE *out, const char *z){  int i;  if( z==0 ) z = "";  while( *z ){    for(i=0;   z[i]            && z[i]!='<'            && z[i]!='&'            && z[i]!='>'            && z[i]!='\"'            && z[i]!='\'';        i++){}    if( i>0 ){      utf8_printf(out,"%.*s",i,z);    }    if( z[i]=='<' ){      raw_printf(out,"<");    }else if( z[i]=='&' ){      raw_printf(out,"&");    }else if( z[i]=='>' ){      raw_printf(out,">");    }else if( z[i]=='\"' ){      raw_printf(out,""");    }else if( z[i]=='\'' ){      raw_printf(out,"'");    }else{      break;    }    z += i + 1;  }}static const char needCsvQuote[] = {  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,  1, 0, 1, 0, 0, 0, 0, 1,   0, 0, 0, 0, 0, 0, 0, 0,  0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,  0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,  0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,  0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,  0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 1,  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,};static void output_csv(ShellState *p, const char *z, int bSep){  FILE *out = p->out;  if( z==0 ){    utf8_printf(out,"%s",p->nullValue);  }else{    unsigned i;    for(i=0; z[i]; i++){      if( needCsvQuote[((unsigned char*)z)[i]] ){        i = 0;        break;      }    }    if( i==0 || strstr(z, p->colSeparator)!=0 ){      char *zQuoted = sqlite3_mprintf("\"%w\"", z);      shell_check_oom(zQuoted);      utf8_printf(out, "%s", zQuoted);      sqlite3_free(zQuoted);    }else{      utf8_printf(out, "%s", z);    }  }  if( bSep ){    utf8_printf(p->out, "%s", p->colSeparator);  }}static void interrupt_handler(int NotUsed){  UNUSED_PARAMETER(NotUsed);  seenInterrupt++;  if( seenInterrupt>2 ) exit(1);  if( globalDb ) sqlite3_interrupt(globalDb);}#if (defined(_WIN32) || defined(WIN32)) && !defined(_WIN32_WCE)static BOOL WINAPI ConsoleCtrlHandler(  DWORD dwCtrlType ){  if( dwCtrlType==CTRL_C_EVENT ){    interrupt_handler(0);    return TRUE;  }  return FALSE;}#endif#ifndef SQLITE_OMIT_AUTHORIZATIONstatic int safeModeAuth(  void *pClientData,  int op,  const char *zA1,  const char *zA2,  const char *zA3,  const char *zA4){  ShellState *p = (ShellState*)pClientData;  static const char *azProhibitedFunctions[] = {    "edit",    "fts3_tokenizer",    "load_extension",    "readfile",    "writefile",    "zipfile",    "zipfile_cds",  };  UNUSED_PARAMETER(zA2);  UNUSED_PARAMETER(zA3);  UNUSED_PARAMETER(zA4);  switch( op ){    case SQLITE_ATTACH: {      failIfSafeMode(p, "cannot run ATTACH in safe mode");      break;    }    case SQLITE_FUNCTION: {      int i;      for(i=0; i<ArraySize(azProhibitedFunctions); i++){        if( sqlite3_stricmp(zA1, azProhibitedFunctions[i])==0 ){          failIfSafeMode(p, "cannot use the %s() function in safe mode",                         azProhibitedFunctions[i]);        }      }      break;    }  }  return SQLITE_OK;}static int shellAuth(  void *pClientData,  int op,  const char *zA1,  const char *zA2,  const char *zA3,  const char *zA4){  ShellState *p = (ShellState*)pClientData;  static const char *azAction[] = { 0,     "CREATE_INDEX",         "CREATE_TABLE",         "CREATE_TEMP_INDEX",     "CREATE_TEMP_TABLE",    "CREATE_TEMP_TRIGGER",  "CREATE_TEMP_VIEW",     "CREATE_TRIGGER",       "CREATE_VIEW",          "DELETE",     "DROP_INDEX",           "DROP_TABLE",           "DROP_TEMP_INDEX",     "DROP_TEMP_TABLE",      "DROP_TEMP_TRIGGER",    "DROP_TEMP_VIEW",     "DROP_TRIGGER",         "DROP_VIEW",            "INSERT",     "PRAGMA",               "READ",                 "SELECT",     "TRANSACTION",          "UPDATE",               "ATTACH",     "DETACH",               "ALTER_TABLE",          "REINDEX",     "ANALYZE",              "CREATE_VTABLE",        "DROP_VTABLE",     "FUNCTION",             "SAVEPOINT",            "RECURSIVE"  };  int i;  const char *az[4];  az[0] = zA1;  az[1] = zA2;  az[2] = zA3;  az[3] = zA4;  utf8_printf(p->out, "authorizer: %s", azAction[op]);  for(i=0; i<4; i++){    raw_printf(p->out, " ");    if( az[i] ){      output_c_string(p->out, az[i]);    }else{      raw_printf(p->out, "NULL");    }  }  raw_printf(p->out, "\n");  if( p->bSafeMode ) (void)safeModeAuth(pClientData, op, zA1, zA2, zA3, zA4);  return SQLITE_OK;}#endifstatic void printSchemaLine(FILE *out, const char *z, const char *zTail){  if( z==0 ) return;  if( zTail==0 ) return;  if( sqlite3_strglob("CREATE TABLE ['\"]*", z)==0 ){    utf8_printf(out, "CREATE TABLE IF NOT EXISTS %s%s", z+13, zTail);  }else{    utf8_printf(out, "%s%s", z, zTail);  }}static void printSchemaLineN(FILE *out, char *z, int n, const char *zTail){  char c = z[n];  z[n] = 0;  printSchemaLine(out, z, zTail);  z[n] = c;}static int wsToEol(const char *z){  int i;  for(i=0; z[i]; i++){    if( z[i]=='\n' ) return 1;    if( IsSpace(z[i]) ) continue;    if( z[i]=='-' && z[i+1]=='-' ) return 1;    return 0;  }  return 1;}static void eqp_append(ShellState *p, int iEqpId, int p2, const char *zText){  EQPGraphRow *pNew;  int nText = strlen30(zText);  if( p->autoEQPtest ){    utf8_printf(p->out, "%d,%d,%s\n", iEqpId, p2, zText);  }  pNew = sqlite3_malloc64( sizeof(*pNew) + nText );  shell_check_oom(pNew);  pNew->iEqpId = iEqpId;  pNew->iParentId = p2;  memcpy(pNew->zText, zText, nText+1);  pNew->pNext = 0;  if( p->sGraph.pLast ){    p->sGraph.pLast->pNext = pNew;  }else{    p->sGraph.pRow = pNew;  }  p->sGraph.pLast = pNew;}static void eqp_reset(ShellState *p){  EQPGraphRow *pRow, *pNext;  for(pRow = p->sGraph.pRow; pRow; pRow = pNext){    pNext = pRow->pNext;    sqlite3_free(pRow);  }  memset(&p->sGraph, 0, sizeof(p->sGraph));}static EQPGraphRow *eqp_next_row(ShellState *p, int iEqpId, EQPGraphRow *pOld){  EQPGraphRow *pRow = pOld ? pOld->pNext : p->sGraph.pRow;  while( pRow && pRow->iParentId!=iEqpId ) pRow = pRow->pNext;  return pRow;}static void eqp_render_level(ShellState *p, int iEqpId){  EQPGraphRow *pRow, *pNext;  int n = strlen30(p->sGraph.zPrefix);  char *z;  for(pRow = eqp_next_row(p, iEqpId, 0); pRow; pRow = pNext){    pNext = eqp_next_row(p, iEqpId, pRow);    z = pRow->zText;    utf8_printf(p->out, "%s%s%s\n", p->sGraph.zPrefix,                pNext ? "|--" : "`--", z);    if( n<(int)sizeof(p->sGraph.zPrefix)-7 ){      memcpy(&p->sGraph.zPrefix[n], pNext ? "|  " : "   ", 4);      eqp_render_level(p, pRow->iEqpId);      p->sGraph.zPrefix[n] = 0;    }  }}static void eqp_render(ShellState *p){  EQPGraphRow *pRow = p->sGraph.pRow;  if( pRow ){    if( pRow->zText[0]=='-' ){      if( pRow->pNext==0 ){        eqp_reset(p);        return;      }      utf8_printf(p->out, "%s\n", pRow->zText+3);      p->sGraph.pRow = pRow->pNext;      sqlite3_free(pRow);    }else{      utf8_printf(p->out, "QUERY PLAN\n");    }    p->sGraph.zPrefix[0] = 0;    eqp_render_level(p, 0);    eqp_reset(p);  }}#ifndef SQLITE_OMIT_PROGRESS_CALLBACKstatic int progress_handler(void *pClientData) {  ShellState *p = (ShellState*)pClientData;  p->nProgress++;  if( p->nProgress>=p->mxProgress && p->mxProgress>0 ){    raw_printf(p->out, "Progress limit reached (%u)\n", p->nProgress);    if( p->flgProgress & SHELL_PROGRESS_RESET ) p->nProgress = 0;    if( p->flgProgress & SHELL_PROGRESS_ONCE ) p->mxProgress = 0;    return 1;  }  if( (p->flgProgress & SHELL_PROGRESS_QUIET)==0 ){    raw_printf(p->out, "Progress %u\n", p->nProgress);  }  return 0;}#endif static void print_dashes(FILE *out, int N){  const char zDash[] = "--------------------------------------------------";  const int nDash = sizeof(zDash) - 1;  while( N>nDash ){    fputs(zDash, out);    N -= nDash;  }  raw_printf(out, "%.*s", N, zDash);}static void print_row_separator(  ShellState *p,  int nArg,  const char *zSep){  int i;  if( nArg>0 ){    fputs(zSep, p->out);    print_dashes(p->out, p->actualWidth[0]+2);    for(i=1; i<nArg; i++){      fputs(zSep, p->out);      print_dashes(p->out, p->actualWidth[i]+2);    }    fputs(zSep, p->out);  }  fputs("\n", p->out);}static int shell_callback(  void *pArg,  int nArg,          char **azArg,      char **azCol,      int *aiType      ){  int i;  ShellState *p = (ShellState*)pArg;  if( azArg==0 ) return 0;  switch( p->cMode ){    case MODE_Count:    case MODE_Off: {      break;    }    case MODE_Line: {      int w = 5;      if( azArg==0 ) break;      for(i=0; i<nArg; i++){        int len = strlen30(azCol[i] ? azCol[i] : "");        if( len>w ) w = len;      }      if( p->cnt++>0 ) utf8_printf(p->out, "%s", p->rowSeparator);      for(i=0; i<nArg; i++){        utf8_printf(p->out,"%*s = %s%s", w, azCol[i],                azArg[i] ? azArg[i] : p->nullValue, p->rowSeparator);      }      break;    }    case MODE_Explain: {      static const int aExplainWidth[] = {4, 13, 4, 4, 4, 13, 2, 13};      if( nArg>ArraySize(aExplainWidth) ){        nArg = ArraySize(aExplainWidth);      }      if( p->cnt++==0 ){        for(i=0; i<nArg; i++){          int w = aExplainWidth[i];          utf8_width_print(p->out, w, azCol[i]);          fputs(i==nArg-1 ? "\n" : "  ", p->out);        }        for(i=0; i<nArg; i++){          int w = aExplainWidth[i];          print_dashes(p->out, w);          fputs(i==nArg-1 ? "\n" : "  ", p->out);        }      }      if( azArg==0 ) break;      for(i=0; i<nArg; i++){        int w = aExplainWidth[i];        if( i==nArg-1 ) w = 0;        if( azArg[i] && strlenChar(azArg[i])>w ){          w = strlenChar(azArg[i]);        }        if( i==1 && p->aiIndent && p->pStmt ){          if( p->iIndent<p->nIndent ){            utf8_printf(p->out, "%*.s", p->aiIndent[p->iIndent], "");          }          p->iIndent++;        }        utf8_width_print(p->out, w, azArg[i] ? azArg[i] : p->nullValue);        fputs(i==nArg-1 ? "\n" : "  ", p->out);      }      break;    }    case MODE_Semi: {         printSchemaLine(p->out, azArg[0], ";\n");      break;    }    case MODE_Pretty: {        char *z;      int j;      int nParen = 0;      char cEnd = 0;      char c;      int nLine = 0;      assert( nArg==1 );      if( azArg[0]==0 ) break;      if( sqlite3_strlike("CREATE VIEW%", azArg[0], 0)==0       || sqlite3_strlike("CREATE TRIG%", azArg[0], 0)==0      ){        utf8_printf(p->out, "%s;\n", azArg[0]);        break;      }      z = sqlite3_mprintf("%s", azArg[0]);      shell_check_oom(z);      j = 0;      for(i=0; IsSpace(z[i]); i++){}      for(; (c = z[i])!=0; i++){        if( IsSpace(c) ){          if( z[j-1]=='\r' ) z[j-1] = '\n';          if( IsSpace(z[j-1]) || z[j-1]=='(' ) continue;        }else if( (c=='(' || c==')') && j>0 && IsSpace(z[j-1]) ){          j--;        }        z[j++] = c;      }      while( j>0 && IsSpace(z[j-1]) ){ j--; }      z[j] = 0;      if( strlen30(z)>=79 ){        for(i=j=0; (c = z[i])!=0; i++){           if( c==cEnd ){            cEnd = 0;          }else if( c=='"' || c=='\'' || c=='`' ){            cEnd = c;          }else if( c=='[' ){            cEnd = ']';          }else if( c=='-' && z[i+1]=='-' ){            cEnd = '\n';          }else if( c=='(' ){            nParen++;          }else if( c==')' ){            nParen--;            if( nLine>0 && nParen==0 && j>0 ){              printSchemaLineN(p->out, z, j, "\n");              j = 0;            }          }          z[j++] = c;          if( nParen==1 && cEnd==0           && (c=='(' || c=='\n' || (c==',' && !wsToEol(z+i+1)))          ){            if( c=='\n' ) j--;            printSchemaLineN(p->out, z, j, "\n  ");            j = 0;            nLine++;            while( IsSpace(z[i+1]) ){ i++; }          }        }        z[j] = 0;      }      printSchemaLine(p->out, z, ";\n");      sqlite3_free(z);      break;    }    case MODE_List: {      if( p->cnt++==0 && p->showHeader ){        for(i=0; i<nArg; i++){          utf8_printf(p->out,"%s%s",azCol[i],                  i==nArg-1 ? p->rowSeparator : p->colSeparator);        }      }      if( azArg==0 ) break;      for(i=0; i<nArg; i++){        char *z = azArg[i];        if( z==0 ) z = p->nullValue;        utf8_printf(p->out, "%s", z);        if( i<nArg-1 ){          utf8_printf(p->out, "%s", p->colSeparator);        }else{          utf8_printf(p->out, "%s", p->rowSeparator);        }      }      break;    }    case MODE_Html: {      if( p->cnt++==0 && p->showHeader ){        raw_printf(p->out,"<TR>");        for(i=0; i<nArg; i++){          raw_printf(p->out,"<TH>");          output_html_string(p->out, azCol[i]);          raw_printf(p->out,"</TH>\n");        }        raw_printf(p->out,"</TR>\n");      }      if( azArg==0 ) break;      raw_printf(p->out,"<TR>");      for(i=0; i<nArg; i++){        raw_printf(p->out,"<TD>");        output_html_string(p->out, azArg[i] ? azArg[i] : p->nullValue);        raw_printf(p->out,"</TD>\n");      }      raw_printf(p->out,"</TR>\n");      break;    }    case MODE_Tcl: {      if( p->cnt++==0 && p->showHeader ){        for(i=0; i<nArg; i++){          output_c_string(p->out,azCol[i] ? azCol[i] : "");          if(i<nArg-1) utf8_printf(p->out, "%s", p->colSeparator);        }        utf8_printf(p->out, "%s", p->rowSeparator);      }      if( azArg==0 ) break;      for(i=0; i<nArg; i++){        output_c_string(p->out, azArg[i] ? azArg[i] : p->nullValue);        if(i<nArg-1) utf8_printf(p->out, "%s", p->colSeparator);      }      utf8_printf(p->out, "%s", p->rowSeparator);      break;    }    case MODE_Csv: {      setBinaryMode(p->out, 1);      if( p->cnt++==0 && p->showHeader ){        for(i=0; i<nArg; i++){          output_csv(p, azCol[i] ? azCol[i] : "", i<nArg-1);        }        utf8_printf(p->out, "%s", p->rowSeparator);      }      if( nArg>0 ){        for(i=0; i<nArg; i++){          output_csv(p, azArg[i], i<nArg-1);        }        utf8_printf(p->out, "%s", p->rowSeparator);      }      setTextMode(p->out, 1);      break;    }    case MODE_Insert: {      if( azArg==0 ) break;      utf8_printf(p->out,"INSERT INTO %s",p->zDestTable);      if( p->showHeader ){        raw_printf(p->out,"(");        for(i=0; i<nArg; i++){          if( i>0 ) raw_printf(p->out, ",");          if( quoteChar(azCol[i]) ){            char *z = sqlite3_mprintf("\"%w\"", azCol[i]);            shell_check_oom(z);            utf8_printf(p->out, "%s", z);            sqlite3_free(z);          }else{            raw_printf(p->out, "%s", azCol[i]);          }        }        raw_printf(p->out,")");      }      p->cnt++;      for(i=0; i<nArg; i++){        raw_printf(p->out, i>0 ? "," : " VALUES(");        if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){          utf8_printf(p->out,"NULL");        }else if( aiType && aiType[i]==SQLITE_TEXT ){          if( ShellHasFlag(p, SHFLG_Newlines) ){            output_quoted_string(p->out, azArg[i]);          }else{            output_quoted_escaped_string(p->out, azArg[i]);          }        }else if( aiType && aiType[i]==SQLITE_INTEGER ){          utf8_printf(p->out,"%s", azArg[i]);        }else if( aiType && aiType[i]==SQLITE_FLOAT ){          char z[50];          double r = sqlite3_column_double(p->pStmt, i);          sqlite3_uint64 ur;          memcpy(&ur,&r,sizeof(r));          if( ur==0x7ff0000000000000LL ){            raw_printf(p->out, "1e999");          }else if( ur==0xfff0000000000000LL ){            raw_printf(p->out, "-1e999");          }else{            sqlite3_int64 ir = (sqlite3_int64)r;            if( r==(double)ir ){              sqlite3_snprintf(50,z,"%lld.0", ir);            }else{              sqlite3_snprintf(50,z,"%!.20g", r);            }            raw_printf(p->out, "%s", z);          }        }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){          const void *pBlob = sqlite3_column_blob(p->pStmt, i);          int nBlob = sqlite3_column_bytes(p->pStmt, i);          output_hex_blob(p->out, pBlob, nBlob);        }else if( isNumber(azArg[i], 0) ){          utf8_printf(p->out,"%s", azArg[i]);        }else if( ShellHasFlag(p, SHFLG_Newlines) ){          output_quoted_string(p->out, azArg[i]);        }else{          output_quoted_escaped_string(p->out, azArg[i]);        }      }      raw_printf(p->out,");\n");      break;    }    case MODE_Json: {      if( azArg==0 ) break;      if( p->cnt==0 ){        fputs("[{", p->out);      }else{        fputs(",\n{", p->out);      }      p->cnt++;      for(i=0; i<nArg; i++){        output_json_string(p->out, azCol[i], -1);        putc(':', p->out);        if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){          fputs("null",p->out);        }else if( aiType && aiType[i]==SQLITE_FLOAT ){          char z[50];          double r = sqlite3_column_double(p->pStmt, i);          sqlite3_uint64 ur;          memcpy(&ur,&r,sizeof(r));          if( ur==0x7ff0000000000000LL ){            raw_printf(p->out, "1e999");          }else if( ur==0xfff0000000000000LL ){            raw_printf(p->out, "-1e999");          }else{            sqlite3_snprintf(50,z,"%!.20g", r);            raw_printf(p->out, "%s", z);          }        }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){          const void *pBlob = sqlite3_column_blob(p->pStmt, i);          int nBlob = sqlite3_column_bytes(p->pStmt, i);          output_json_string(p->out, pBlob, nBlob);        }else if( aiType && aiType[i]==SQLITE_TEXT ){          output_json_string(p->out, azArg[i], -1);        }else{          utf8_printf(p->out,"%s", azArg[i]);        }        if( i<nArg-1 ){          putc(',', p->out);        }      }      putc('}', p->out);      break;    }    case MODE_Quote: {      if( azArg==0 ) break;      if( p->cnt==0 && p->showHeader ){        for(i=0; i<nArg; i++){          if( i>0 ) fputs(p->colSeparator, p->out);          output_quoted_string(p->out, azCol[i]);        }        fputs(p->rowSeparator, p->out);      }      p->cnt++;      for(i=0; i<nArg; i++){        if( i>0 ) fputs(p->colSeparator, p->out);        if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){          utf8_printf(p->out,"NULL");        }else if( aiType && aiType[i]==SQLITE_TEXT ){          output_quoted_string(p->out, azArg[i]);        }else if( aiType && aiType[i]==SQLITE_INTEGER ){          utf8_printf(p->out,"%s", azArg[i]);        }else if( aiType && aiType[i]==SQLITE_FLOAT ){          char z[50];          double r = sqlite3_column_double(p->pStmt, i);          sqlite3_snprintf(50,z,"%!.20g", r);          raw_printf(p->out, "%s", z);        }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){          const void *pBlob = sqlite3_column_blob(p->pStmt, i);          int nBlob = sqlite3_column_bytes(p->pStmt, i);          output_hex_blob(p->out, pBlob, nBlob);        }else if( isNumber(azArg[i], 0) ){          utf8_printf(p->out,"%s", azArg[i]);        }else{          output_quoted_string(p->out, azArg[i]);        }      }      fputs(p->rowSeparator, p->out);      break;    }    case MODE_Ascii: {      if( p->cnt++==0 && p->showHeader ){        for(i=0; i<nArg; i++){          if( i>0 ) utf8_printf(p->out, "%s", p->colSeparator);          utf8_printf(p->out,"%s",azCol[i] ? azCol[i] : "");        }        utf8_printf(p->out, "%s", p->rowSeparator);      }      if( azArg==0 ) break;      for(i=0; i<nArg; i++){        if( i>0 ) utf8_printf(p->out, "%s", p->colSeparator);        utf8_printf(p->out,"%s",azArg[i] ? azArg[i] : p->nullValue);      }      utf8_printf(p->out, "%s", p->rowSeparator);      break;    }    case MODE_EQP: {      eqp_append(p, atoi(azArg[0]), atoi(azArg[1]), azArg[3]);      break;    }  }  return 0;}static int callback(void *pArg, int nArg, char **azArg, char **azCol){    return shell_callback(pArg, nArg, azArg, azCol, NULL);}static int captureOutputCallback(void *pArg, int nArg, char **azArg, char **az){  ShellText *p = (ShellText*)pArg;  int i;  UNUSED_PARAMETER(az);  if( azArg==0 ) return 0;  if( p->n ) appendText(p, "|", 0);  for(i=0; i<nArg; i++){    if( i ) appendText(p, ",", 0);    if( azArg[i] ) appendText(p, azArg[i], 0);  }  return 0;}static void createSelftestTable(ShellState *p){  char *zErrMsg = 0;  sqlite3_exec(p->db,    "SAVEPOINT selftest_init;\n"    "CREATE TABLE IF NOT EXISTS selftest(\n"    "  tno INTEGER PRIMARY KEY,\n"       "  op TEXT,\n"                       "  cmd TEXT,\n"                      "  ans TEXT\n"                       ");"    "CREATE TEMP TABLE [_shell$self](op,cmd,ans);\n"    "INSERT INTO [_shell$self](rowid,op,cmd)\n"    "  VALUES(coalesce((SELECT (max(tno)+100)/10 FROM selftest),10),\n"    "         'memo','Tests generated by --init');\n"    "INSERT INTO [_shell$self]\n"    "  SELECT 'run',\n"    "    'SELECT hex(sha3_query(''SELECT type,name,tbl_name,sql "                                 "FROM sqlite_schema ORDER BY 2'',224))',\n"    "    hex(sha3_query('SELECT type,name,tbl_name,sql "                          "FROM sqlite_schema ORDER BY 2',224));\n"    "INSERT INTO [_shell$self]\n"    "  SELECT 'run',"    "    'SELECT hex(sha3_query(''SELECT * FROM \"' ||"    "        printf('%w',name) || '\" NOT INDEXED'',224))',\n"    "    hex(sha3_query(printf('SELECT * FROM \"%w\" NOT INDEXED',name),224))\n"    "  FROM (\n"    "    SELECT name FROM sqlite_schema\n"    "     WHERE type='table'\n"    "       AND name<>'selftest'\n"    "       AND coalesce(rootpage,0)>0\n"    "  )\n"    " ORDER BY name;\n"    "INSERT INTO [_shell$self]\n"    "  VALUES('run','PRAGMA integrity_check','ok');\n"    "INSERT INTO selftest(tno,op,cmd,ans)"    "  SELECT rowid*10,op,cmd,ans FROM [_shell$self];\n"    "DROP TABLE [_shell$self];"    ,0,0,&zErrMsg);  if( zErrMsg ){    utf8_printf(stderr, "SELFTEST initialization failure: %s\n", zErrMsg);    sqlite3_free(zErrMsg);  }  sqlite3_exec(p->db, "RELEASE selftest_init",0,0,0);}static void set_table_name(ShellState *p, const char *zName){  int i, n;  char cQuote;  char *z;  if( p->zDestTable ){    free(p->zDestTable);    p->zDestTable = 0;  }  if( zName==0 ) return;  cQuote = quoteChar(zName);  n = strlen30(zName);  if( cQuote ) n += n+2;  z = p->zDestTable = malloc( n+1 );  shell_check_oom(z);  n = 0;  if( cQuote ) z[n++] = cQuote;  for(i=0; zName[i]; i++){    z[n++] = zName[i];    if( zName[i]==cQuote ) z[n++] = cQuote;  }  if( cQuote ) z[n++] = cQuote;  z[n] = 0;}static char *shell_error_context(const char *zSql, sqlite3 *db){  int iOffset;  size_t len;  char *zCode;  char *zMsg;  int i;  if( db==0   || zSql==0   || (iOffset = sqlite3_error_offset(db))<0  ){    return sqlite3_mprintf("");  }  while( iOffset>50 ){    iOffset--;    zSql++;    while( (zSql[0]&0xc0)==0x80 ){ zSql++; iOffset--; }  }  len = strlen(zSql);  if( len>78 ){    len = 78;    while( (zSql[len]&0xc0)==0x80 ) len--;  }  zCode = sqlite3_mprintf("%.*s", len, zSql);  for(i=0; zCode[i]; i++){ if( IsSpace(zSql[i]) ) zCode[i] = ' '; }  if( iOffset<25 ){    zMsg = sqlite3_mprintf("\n  %z\n  %*s^--- error here", zCode, iOffset, "");  }else{    zMsg = sqlite3_mprintf("\n  %z\n  %*serror here ---^", zCode, iOffset-14, "");  }  return zMsg;}static int run_table_dump_query(  ShellState *p,             const char *zSelect      ){  sqlite3_stmt *pSelect;  int rc;  int nResult;  int i;  const char *z;  rc = sqlite3_prepare_v2(p->db, zSelect, -1, &pSelect, 0);  if( rc!=SQLITE_OK || !pSelect ){    char *zContext = shell_error_context(zSelect, p->db);    utf8_printf(p->out, "/**** ERROR: (%d) %s *****/\n%s", rc,                sqlite3_errmsg(p->db), zContext);    sqlite3_free(zContext);    if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++;    return rc;  }  rc = sqlite3_step(pSelect);  nResult = sqlite3_column_count(pSelect);  while( rc==SQLITE_ROW ){    z = (const char*)sqlite3_column_text(pSelect, 0);    utf8_printf(p->out, "%s", z);    for(i=1; i<nResult; i++){      utf8_printf(p->out, ",%s", sqlite3_column_text(pSelect, i));    }    if( z==0 ) z = "";    while( z[0] && (z[0]!='-' || z[1]!='-') ) z++;    if( z[0] ){      raw_printf(p->out, "\n;\n");    }else{      raw_printf(p->out, ";\n");    }    rc = sqlite3_step(pSelect);  }  rc = sqlite3_finalize(pSelect);  if( rc!=SQLITE_OK ){    utf8_printf(p->out, "/**** ERROR: (%d) %s *****/\n", rc,                sqlite3_errmsg(p->db));    if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++;  }  return rc;}static char *save_err_msg(  sqlite3 *db,             const char *zPhase,      int rc,                  const char *zSql       ){  char *zErr;  char *zContext;  sqlite3_str *pStr = sqlite3_str_new(0);  sqlite3_str_appendf(pStr, "%s, %s", zPhase, sqlite3_errmsg(db));  if( rc>1 ){    sqlite3_str_appendf(pStr, " (%d)", rc);  }  zContext = shell_error_context(zSql, db);  if( zContext ){    sqlite3_str_appendall(pStr, zContext);    sqlite3_free(zContext);  }  zErr = sqlite3_str_finish(pStr);  shell_check_oom(zErr);  return zErr;}#ifdef __linux__static void displayLinuxIoStats(FILE *out){  FILE *in;  char z[200];  sqlite3_snprintf(sizeof(z), z, "/proc/%d/io", getpid());  in = fopen(z, "rb");  if( in==0 ) return;  while( fgets(z, sizeof(z), in)!=0 ){    static const struct {      const char *zPattern;      const char *zDesc;    } aTrans[] = {      { "rchar: ",                  "Bytes received by read():" },      { "wchar: ",                  "Bytes sent to write():"    },      { "syscr: ",                  "Read() system calls:"      },      { "syscw: ",                  "Write() system calls:"     },      { "read_bytes: ",             "Bytes read from storage:"  },      { "write_bytes: ",            "Bytes written to storage:" },      { "cancelled_write_bytes: ",  "Cancelled write bytes:"    },    };    int i;    for(i=0; i<ArraySize(aTrans); i++){      int n = strlen30(aTrans[i].zPattern);      if( strncmp(aTrans[i].zPattern, z, n)==0 ){        utf8_printf(out, "%-36s %s", aTrans[i].zDesc, &z[n]);        break;      }    }  }  fclose(in);}#endifstatic void displayStatLine(  ShellState *p,              char *zLabel,               char *zFormat,              int iStatusCtrl,            int bReset                ){  sqlite3_int64 iCur = -1;  sqlite3_int64 iHiwtr = -1;  int i, nPercent;  char zLine[200];  sqlite3_status64(iStatusCtrl, &iCur, &iHiwtr, bReset);  for(i=0, nPercent=0; zFormat[i]; i++){    if( zFormat[i]=='%' ) nPercent++;  }  if( nPercent>1 ){    sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iCur, iHiwtr);  }else{    sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iHiwtr);  }  raw_printf(p->out, "%-36s %s\n", zLabel, zLine);}static int display_stats(  sqlite3 *db,                  ShellState *pArg,             int bReset                  ){  int iCur;  int iHiwtr;  FILE *out;  if( pArg==0 || pArg->out==0 ) return 0;  out = pArg->out;  if( pArg->pStmt && pArg->statsOn==2 ){    int nCol, i, x;    sqlite3_stmt *pStmt = pArg->pStmt;    char z[100];    nCol = sqlite3_column_count(pStmt);    raw_printf(out, "%-36s %d\n", "Number of output columns:", nCol);    for(i=0; i<nCol; i++){      sqlite3_snprintf(sizeof(z),z,"Column %d %nname:", i, &x);      utf8_printf(out, "%-36s %s\n", z, sqlite3_column_name(pStmt,i));#ifndef SQLITE_OMIT_DECLTYPE      sqlite3_snprintf(30, z+x, "declared type:");      utf8_printf(out, "%-36s %s\n", z, sqlite3_column_decltype(pStmt, i));#endif#ifdef SQLITE_ENABLE_COLUMN_METADATA      sqlite3_snprintf(30, z+x, "database name:");      utf8_printf(out, "%-36s %s\n", z, sqlite3_column_database_name(pStmt,i));      sqlite3_snprintf(30, z+x, "table name:");      utf8_printf(out, "%-36s %s\n", z, sqlite3_column_table_name(pStmt,i));      sqlite3_snprintf(30, z+x, "origin name:");      utf8_printf(out, "%-36s %s\n", z, sqlite3_column_origin_name(pStmt,i));#endif    }  }  if( pArg->statsOn==3 ){    if( pArg->pStmt ){      iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_VM_STEP, bReset);      raw_printf(pArg->out, "VM-steps: %d\n", iCur);    }    return 0;  }  displayStatLine(pArg, "Memory Used:",     "%lld (max %lld) bytes", SQLITE_STATUS_MEMORY_USED, bReset);  displayStatLine(pArg, "Number of Outstanding Allocations:",     "%lld (max %lld)", SQLITE_STATUS_MALLOC_COUNT, bReset);  if( pArg->shellFlgs & SHFLG_Pagecache ){    displayStatLine(pArg, "Number of Pcache Pages Used:",       "%lld (max %lld) pages", SQLITE_STATUS_PAGECACHE_USED, bReset);  }  displayStatLine(pArg, "Number of Pcache Overflow Bytes:",     "%lld (max %lld) bytes", SQLITE_STATUS_PAGECACHE_OVERFLOW, bReset);  displayStatLine(pArg, "Largest Allocation:",     "%lld bytes", SQLITE_STATUS_MALLOC_SIZE, bReset);  displayStatLine(pArg, "Largest Pcache Allocation:",     "%lld bytes", SQLITE_STATUS_PAGECACHE_SIZE, bReset);#ifdef YYTRACKMAXSTACKDEPTH  displayStatLine(pArg, "Deepest Parser Stack:",     "%lld (max %lld)", SQLITE_STATUS_PARSER_STACK, bReset);#endif  if( db ){    if( pArg->shellFlgs & SHFLG_Lookaside ){      iHiwtr = iCur = -1;      sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_USED,                        &iCur, &iHiwtr, bReset);      raw_printf(pArg->out,              "Lookaside Slots Used:                %d (max %d)\n",              iCur, iHiwtr);      sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_HIT,                        &iCur, &iHiwtr, bReset);      raw_printf(pArg->out, "Successful lookaside attempts:       %d\n",              iHiwtr);      sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE,                        &iCur, &iHiwtr, bReset);      raw_printf(pArg->out, "Lookaside failures due to size:      %d\n",              iHiwtr);      sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL,                        &iCur, &iHiwtr, bReset);      raw_printf(pArg->out, "Lookaside failures due to OOM:       %d\n",              iHiwtr);    }    iHiwtr = iCur = -1;    sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_USED, &iCur, &iHiwtr, bReset);    raw_printf(pArg->out, "Pager Heap Usage:                    %d bytes\n",            iCur);    iHiwtr = iCur = -1;    sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_HIT, &iCur, &iHiwtr, 1);    raw_printf(pArg->out, "Page cache hits:                     %d\n", iCur);    iHiwtr = iCur = -1;    sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_MISS, &iCur, &iHiwtr, 1);    raw_printf(pArg->out, "Page cache misses:                   %d\n", iCur);    iHiwtr = iCur = -1;    sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_WRITE, &iCur, &iHiwtr, 1);    raw_printf(pArg->out, "Page cache writes:                   %d\n", iCur);    iHiwtr = iCur = -1;    sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_SPILL, &iCur, &iHiwtr, 1);    raw_printf(pArg->out, "Page cache spills:                   %d\n", iCur);    iHiwtr = iCur = -1;    sqlite3_db_status(db, SQLITE_DBSTATUS_SCHEMA_USED, &iCur, &iHiwtr, bReset);    raw_printf(pArg->out, "Schema Heap Usage:                   %d bytes\n",            iCur);    iHiwtr = iCur = -1;    sqlite3_db_status(db, SQLITE_DBSTATUS_STMT_USED, &iCur, &iHiwtr, bReset);    raw_printf(pArg->out, "Statement Heap/Lookaside Usage:      %d bytes\n",            iCur);  }  if( pArg->pStmt ){    int iHit, iMiss;    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FULLSCAN_STEP,                               bReset);    raw_printf(pArg->out, "Fullscan Steps:                      %d\n", iCur);    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_SORT, bReset);    raw_printf(pArg->out, "Sort Operations:                     %d\n", iCur);    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_AUTOINDEX,bReset);    raw_printf(pArg->out, "Autoindex Inserts:                   %d\n", iCur);    iHit = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FILTER_HIT, bReset);    iMiss = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FILTER_MISS, bReset);    if( iHit || iMiss ){      raw_printf(pArg->out, "Bloom filter bypass taken:           %d/%d\n",            iHit, iHit+iMiss);    }    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_VM_STEP, bReset);    raw_printf(pArg->out, "Virtual Machine Steps:               %d\n", iCur);    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_REPREPARE,bReset);    raw_printf(pArg->out, "Reprepare operations:                %d\n", iCur);    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_RUN, bReset);    raw_printf(pArg->out, "Number of times run:                 %d\n", iCur);    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_MEMUSED, bReset);    raw_printf(pArg->out, "Memory used by prepared stmt:        %d\n", iCur);  }#ifdef __linux__  displayLinuxIoStats(pArg->out);#endif    return 0;}static void display_scanstats(  sqlite3 *db,                      ShellState *pArg                ){#ifndef SQLITE_ENABLE_STMT_SCANSTATUS  UNUSED_PARAMETER(db);  UNUSED_PARAMETER(pArg);#else  int i, k, n, mx;  raw_printf(pArg->out, "-------- scanstats --------\n");  mx = 0;  for(k=0; k<=mx; k++){    double rEstLoop = 1.0;    for(i=n=0; 1; i++){      sqlite3_stmt *p = pArg->pStmt;      sqlite3_int64 nLoop, nVisit;      double rEst;      int iSid;      const char *zExplain;      if( sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_NLOOP, (void*)&nLoop) ){        break;      }      sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_SELECTID, (void*)&iSid);      if( iSid>mx ) mx = iSid;      if( iSid!=k ) continue;      if( n==0 ){        rEstLoop = (double)nLoop;        if( k>0 ) raw_printf(pArg->out, "-------- subquery %d -------\n", k);      }      n++;      sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_NVISIT, (void*)&nVisit);      sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_EST, (void*)&rEst);      sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_EXPLAIN, (void*)&zExplain);      utf8_printf(pArg->out, "Loop %2d: %s\n", n, zExplain);      rEstLoop *= rEst;      raw_printf(pArg->out,          "         nLoop=%-8lld nRow=%-8lld estRow=%-8lld estRow/Loop=%-8g\n",          nLoop, nVisit, (sqlite3_int64)(rEstLoop+0.5), rEst      );    }  }  raw_printf(pArg->out, "---------------------------\n");#endif}static int str_in_array(const char *zStr, const char **azArray){  int i;  for(i=0; azArray[i]; i++){    if( 0==strcmp(zStr, azArray[i]) ) return 1;  }  return 0;}static void explain_data_prepare(ShellState *p, sqlite3_stmt *pSql){  const char *zSql;                 const char *z;                    int *abYield = 0;                 int nAlloc = 0;                   int iOp;                          const char *azNext[] = { "Next", "Prev", "VPrev", "VNext", "SorterNext", 0 };  const char *azYield[] = { "Yield", "SeekLT", "SeekGT", "RowSetRead",                            "Rewind", 0 };  const char *azGoto[] = { "Goto", 0 };    if( sqlite3_column_count(pSql)!=8 ){    p->cMode = p->mode;    return;  }  zSql = sqlite3_sql(pSql);  if( zSql==0 ) return;  for(z=zSql; *z==' ' || *z=='\t' || *z=='\n' || *z=='\f' || *z=='\r'; z++);  if( sqlite3_strnicmp(z, "explain", 7) ){    p->cMode = p->mode;    return;  }  for(iOp=0; SQLITE_ROW==sqlite3_step(pSql); iOp++){    int i;    int iAddr = sqlite3_column_int(pSql, 0);    const char *zOp = (const char*)sqlite3_column_text(pSql, 1);        int p2 = sqlite3_column_int(pSql, 3);    int p2op = (p2 + (iOp-iAddr));        if( iOp>=nAlloc ){      if( iOp==0 ){                static const char *explainCols[] = {           "addr", "opcode", "p1", "p2", "p3", "p4", "p5", "comment" };        int jj;        for(jj=0; jj<ArraySize(explainCols); jj++){          if( strcmp(sqlite3_column_name(pSql,jj),explainCols[jj])!=0 ){            p->cMode = p->mode;            sqlite3_reset(pSql);            return;          }        }      }      nAlloc += 100;      p->aiIndent = (int*)sqlite3_realloc64(p->aiIndent, nAlloc*sizeof(int));      shell_check_oom(p->aiIndent);      abYield = (int*)sqlite3_realloc64(abYield, nAlloc*sizeof(int));      shell_check_oom(abYield);    }    abYield[iOp] = str_in_array(zOp, azYield);    p->aiIndent[iOp] = 0;    p->nIndent = iOp+1;    if( str_in_array(zOp, azNext) ){      for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2;    }    if( str_in_array(zOp, azGoto) && p2op<p->nIndent     && (abYield[p2op] || sqlite3_column_int(pSql, 2))    ){      for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2;    }  }  p->iIndent = 0;  sqlite3_free(abYield);  sqlite3_reset(pSql);}static void explain_data_delete(ShellState *p){  sqlite3_free(p->aiIndent);  p->aiIndent = 0;  p->nIndent = 0;  p->iIndent = 0;}static unsigned int savedSelectTrace;static unsigned int savedWhereTrace;static void disable_debug_trace_modes(void){  unsigned int zero = 0;  sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 0, &savedSelectTrace);  sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 1, &zero);  sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 2, &savedWhereTrace);  sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 3, &zero);}static void restore_debug_trace_modes(void){  sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 1, &savedSelectTrace);  sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 3, &savedWhereTrace);}static void bind_table_init(ShellState *p){  int wrSchema = 0;  int defensiveMode = 0;  sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, -1, &defensiveMode);  sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, 0, 0);  sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, -1, &wrSchema);  sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, 1, 0);  sqlite3_exec(p->db,    "CREATE TABLE IF NOT EXISTS temp.sqlite_parameters(\n"    "  key TEXT PRIMARY KEY,\n"    "  value\n"    ") WITHOUT ROWID;",    0, 0, 0);  sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, wrSchema, 0);  sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, defensiveMode, 0);}static void bind_prepared_stmt(ShellState *pArg, sqlite3_stmt *pStmt){  int nVar;  int i;  int rc;  sqlite3_stmt *pQ = 0;  nVar = sqlite3_bind_parameter_count(pStmt);  if( nVar==0 ) return;    if( sqlite3_table_column_metadata(pArg->db, "TEMP", "sqlite_parameters",                                    "key", 0, 0, 0, 0, 0)!=SQLITE_OK ){    return;   }  rc = sqlite3_prepare_v2(pArg->db,          "SELECT value FROM temp.sqlite_parameters"          " WHERE key=?1", -1, &pQ, 0);  if( rc || pQ==0 ) return;  for(i=1; i<=nVar; i++){    char zNum[30];    const char *zVar = sqlite3_bind_parameter_name(pStmt, i);    if( zVar==0 ){      sqlite3_snprintf(sizeof(zNum),zNum,"?%d",i);      zVar = zNum;    }    sqlite3_bind_text(pQ, 1, zVar, -1, SQLITE_STATIC);    if( sqlite3_step(pQ)==SQLITE_ROW ){      sqlite3_bind_value(pStmt, i, sqlite3_column_value(pQ, 0));    }else{      sqlite3_bind_null(pStmt, i);    }    sqlite3_reset(pQ);  }  sqlite3_finalize(pQ);}#define BOX_24   "\342\224\200"  #define BOX_13   "\342\224\202"  #define BOX_23   "\342\224\214"  #define BOX_34   "\342\224\220"  #define BOX_12   "\342\224\224"  #define BOX_14   "\342\224\230"  #define BOX_123  "\342\224\234"  #define BOX_134  "\342\224\244"  #define BOX_234  "\342\224\254"  #define BOX_124  "\342\224\264"  #define BOX_1234 "\342\224\274"  static void print_box_line(FILE *out, int N){  const char zDash[] =       BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24      BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24;  const int nDash = sizeof(zDash) - 1;  N *= 3;  while( N>nDash ){    utf8_printf(out, zDash);    N -= nDash;  }  utf8_printf(out, "%.*s", N, zDash);}static void print_box_row_separator(  ShellState *p,  int nArg,  const char *zSep1,  const char *zSep2,  const char *zSep3){  int i;  if( nArg>0 ){    utf8_printf(p->out, "%s", zSep1);    print_box_line(p->out, p->actualWidth[0]+2);    for(i=1; i<nArg; i++){      utf8_printf(p->out, "%s", zSep2);      print_box_line(p->out, p->actualWidth[i]+2);    }    utf8_printf(p->out, "%s", zSep3);  }  fputs("\n", p->out);}static char *translateForDisplayAndDup(  const unsigned char *z,              const unsigned char **pzTail,        int mxWidth,                         u8 bWordWrap                       ){  int i;                   int j;                   int k;                   int n;                   unsigned char *zOut;     if( z==0 ){    *pzTail = 0;    return 0;  }  if( mxWidth<0 ) mxWidth = -mxWidth;  if( mxWidth==0 ) mxWidth = 1000000;  i = j = n = 0;  while( n<mxWidth ){    if( z[i]>=' ' ){      n++;      do{ i++; j++; }while( (z[i]&0xc0)==0x80 );      continue;    }    if( z[i]=='\t' ){      do{        n++;        j++;      }while( (n&7)!=0 && n<mxWidth );      i++;      continue;    }    break;  }  if( n>=mxWidth && bWordWrap  ){        for(k=i; k>i/2; k--){      if( isspace(z[k-1]) ) break;    }    if( k<=i/2 ){      for(k=i; k>i/2; k--){        if( isalnum(z[k-1])!=isalnum(z[k]) && (z[k]&0xc0)!=0x80 ) break;      }    }    if( k<=i/2 ){      k = i;    }else{      i = k;      while( z[i]==' ' ) i++;    }  }else{    k = i;  }  if( n>=mxWidth && z[i]>=' ' ){   *pzTail = &z[i];  }else if( z[i]=='\r' && z[i+1]=='\n' ){    *pzTail = z[i+2] ? &z[i+2] : 0;  }else if( z[i]==0 || z[i+1]==0 ){    *pzTail = 0;  }else{    *pzTail = &z[i+1];  }  zOut = malloc( j+1 );  shell_check_oom(zOut);  i = j = n = 0;  while( i<k ){    if( z[i]>=' ' ){      n++;      do{ zOut[j++] = z[i++]; }while( (z[i]&0xc0)==0x80 );      continue;    }    if( z[i]=='\t' ){      do{        n++;        zOut[j++] = ' ';      }while( (n&7)!=0 && n<mxWidth );      i++;      continue;    }    break;  }  zOut[j] = 0;  return (char*)zOut;  }static char *quoted_column(sqlite3_stmt *pStmt, int i){  switch( sqlite3_column_type(pStmt, i) ){    case SQLITE_NULL: {      return sqlite3_mprintf("NULL");    }    case SQLITE_INTEGER:    case SQLITE_FLOAT: {      return sqlite3_mprintf("%s",sqlite3_column_text(pStmt,i));    }    case SQLITE_TEXT: {      return sqlite3_mprintf("%Q",sqlite3_column_text(pStmt,i));    }    case SQLITE_BLOB: {      int j;      sqlite3_str *pStr = sqlite3_str_new(0);      const unsigned char *a = sqlite3_column_blob(pStmt,i);      int n = sqlite3_column_bytes(pStmt,i);      sqlite3_str_append(pStr, "x'", 2);      for(j=0; j<n; j++){        sqlite3_str_appendf(pStr, "%02x", a[j]);      }      sqlite3_str_append(pStr, "'", 1);      return sqlite3_str_finish(pStr);    }  }  return 0; }static void exec_prepared_stmt_columnar(  ShellState *p,                          sqlite3_stmt *pStmt                   ){  sqlite3_int64 nRow = 0;  int nColumn = 0;  char **azData = 0;  sqlite3_int64 nAlloc = 0;  char *abRowDiv = 0;  const unsigned char *uz;  const char *z;  char **azQuoted = 0;  int rc;  sqlite3_int64 i, nData;  int j, nTotal, w, n;  const char *colSep = 0;  const char *rowSep = 0;  const unsigned char **azNextLine = 0;  int bNextLine = 0;  int bMultiLineRowExists = 0;  int bw = p->cmOpts.bWordWrap;  const char *zEmpty = "";  const char *zShowNull = p->nullValue;  rc = sqlite3_step(pStmt);  if( rc!=SQLITE_ROW ) return;  nColumn = sqlite3_column_count(pStmt);  nAlloc = nColumn*4;  if( nAlloc<=0 ) nAlloc = 1;  azData = sqlite3_malloc64( nAlloc*sizeof(char*) );  shell_check_oom(azData);  azNextLine = sqlite3_malloc64( nColumn*sizeof(char*) );  shell_check_oom((void*)azNextLine);  memset((void*)azNextLine, 0, nColumn*sizeof(char*) );  if( p->cmOpts.bQuote ){    azQuoted = sqlite3_malloc64( nColumn*sizeof(char*) );    shell_check_oom(azQuoted);    memset(azQuoted, 0, nColumn*sizeof(char*) );  }  abRowDiv = sqlite3_malloc64( nAlloc/nColumn );  shell_check_oom(abRowDiv);  if( nColumn>p->nWidth ){    p->colWidth = realloc(p->colWidth, (nColumn+1)*2*sizeof(int));    shell_check_oom(p->colWidth);    for(i=p->nWidth; i<nColumn; i++) p->colWidth[i] = 0;    p->nWidth = nColumn;    p->actualWidth = &p->colWidth[nColumn];  }  memset(p->actualWidth, 0, nColumn*sizeof(int));  for(i=0; i<nColumn; i++){    w = p->colWidth[i];    if( w<0 ) w = -w;    p->actualWidth[i] = w;  }  for(i=0; i<nColumn; i++){    const unsigned char *zNotUsed;    int wx = p->colWidth[i];    if( wx==0 ){      wx = p->cmOpts.iWrap;    }    if( wx<0 ) wx = -wx;    uz = (const unsigned char*)sqlite3_column_name(pStmt,i);    azData[i] = translateForDisplayAndDup(uz, &zNotUsed, wx, bw);  }  do{    int useNextLine = bNextLine;    bNextLine = 0;    if( (nRow+2)*nColumn >= nAlloc ){      nAlloc *= 2;      azData = sqlite3_realloc64(azData, nAlloc*sizeof(char*));      shell_check_oom(azData);      abRowDiv = sqlite3_realloc64(abRowDiv, nAlloc/nColumn);      shell_check_oom(abRowDiv);    }    abRowDiv[nRow] = 1;    nRow++;    for(i=0; i<nColumn; i++){      int wx = p->colWidth[i];      if( wx==0 ){        wx = p->cmOpts.iWrap;      }      if( wx<0 ) wx = -wx;      if( useNextLine ){        uz = azNextLine[i];        if( uz==0 ) uz = (u8*)zEmpty;      }else if( p->cmOpts.bQuote ){        sqlite3_free(azQuoted[i]);        azQuoted[i] = quoted_column(pStmt,i);        uz = (const unsigned char*)azQuoted[i];      }else{        uz = (const unsigned char*)sqlite3_column_text(pStmt,i);        if( uz==0 ) uz = (u8*)zShowNull;      }      azData[nRow*nColumn + i]        = translateForDisplayAndDup(uz, &azNextLine[i], wx, bw);      if( azNextLine[i] ){        bNextLine = 1;        abRowDiv[nRow-1] = 0;        bMultiLineRowExists = 1;      }    }  }while( bNextLine || sqlite3_step(pStmt)==SQLITE_ROW );  nTotal = nColumn*(nRow+1);  for(i=0; i<nTotal; i++){    z = azData[i];    if( z==0 ) z = (char*)zEmpty;    n = strlenChar(z);    j = i%nColumn;    if( n>p->actualWidth[j] ) p->actualWidth[j] = n;  }  if( seenInterrupt ) goto columnar_end;  if( nColumn==0 ) goto columnar_end;  switch( p->cMode ){    case MODE_Column: {      colSep = "  ";      rowSep = "\n";      if( p->showHeader ){        for(i=0; i<nColumn; i++){          w = p->actualWidth[i];          if( p->colWidth[i]<0 ) w = -w;          utf8_width_print(p->out, w, azData[i]);          fputs(i==nColumn-1?"\n":"  ", p->out);        }        for(i=0; i<nColumn; i++){          print_dashes(p->out, p->actualWidth[i]);          fputs(i==nColumn-1?"\n":"  ", p->out);        }      }      break;    }    case MODE_Table: {      colSep = " | ";      rowSep = " |\n";      print_row_separator(p, nColumn, "+");      fputs("| ", p->out);      for(i=0; i<nColumn; i++){        w = p->actualWidth[i];        n = strlenChar(azData[i]);        utf8_printf(p->out, "%*s%s%*s", (w-n)/2, "", azData[i], (w-n+1)/2, "");        fputs(i==nColumn-1?" |\n":" | ", p->out);      }      print_row_separator(p, nColumn, "+");      break;    }    case MODE_Markdown: {      colSep = " | ";      rowSep = " |\n";      fputs("| ", p->out);      for(i=0; i<nColumn; i++){        w = p->actualWidth[i];        n = strlenChar(azData[i]);        utf8_printf(p->out, "%*s%s%*s", (w-n)/2, "", azData[i], (w-n+1)/2, "");        fputs(i==nColumn-1?" |\n":" | ", p->out);      }      print_row_separator(p, nColumn, "|");      break;    }    case MODE_Box: {      colSep = " " BOX_13 " ";      rowSep = " " BOX_13 "\n";      print_box_row_separator(p, nColumn, BOX_23, BOX_234, BOX_34);      utf8_printf(p->out, BOX_13 " ");      for(i=0; i<nColumn; i++){        w = p->actualWidth[i];        n = strlenChar(azData[i]);        utf8_printf(p->out, "%*s%s%*s%s",            (w-n)/2, "", azData[i], (w-n+1)/2, "",            i==nColumn-1?" "BOX_13"\n":" "BOX_13" ");      }      print_box_row_separator(p, nColumn, BOX_123, BOX_1234, BOX_134);      break;    }  }  for(i=nColumn, j=0; i<nTotal; i++, j++){    if( j==0 && p->cMode!=MODE_Column ){      utf8_printf(p->out, "%s", p->cMode==MODE_Box?BOX_13" ":"| ");    }    z = azData[i];    if( z==0 ) z = p->nullValue;    w = p->actualWidth[j];    if( p->colWidth[j]<0 ) w = -w;    utf8_width_print(p->out, w, z);    if( j==nColumn-1 ){      utf8_printf(p->out, "%s", rowSep);      if( bMultiLineRowExists && abRowDiv[i/nColumn-1] && i+1<nTotal ){        if( p->cMode==MODE_Table ){          print_row_separator(p, nColumn, "+");        }else if( p->cMode==MODE_Box ){          print_box_row_separator(p, nColumn, BOX_123, BOX_1234, BOX_134);        }else if( p->cMode==MODE_Column ){          raw_printf(p->out, "\n");        }      }      j = -1;      if( seenInterrupt ) goto columnar_end;    }else{      utf8_printf(p->out, "%s", colSep);    }  }  if( p->cMode==MODE_Table ){    print_row_separator(p, nColumn, "+");  }else if( p->cMode==MODE_Box ){    print_box_row_separator(p, nColumn, BOX_12, BOX_124, BOX_14);  }columnar_end:  if( seenInterrupt ){    utf8_printf(p->out, "Interrupt\n");  }  nData = (nRow+1)*nColumn;  for(i=0; i<nData; i++){    z = azData[i];    if( z!=zEmpty && z!=zShowNull ) free(azData[i]);  }  sqlite3_free(azData);  sqlite3_free((void*)azNextLine);  sqlite3_free(abRowDiv);  if( azQuoted ){    for(i=0; i<nColumn; i++) sqlite3_free(azQuoted[i]);    sqlite3_free(azQuoted);  }}static void exec_prepared_stmt(  ShellState *pArg,                                  sqlite3_stmt *pStmt                              ){  int rc;  sqlite3_uint64 nRow = 0;  if( pArg->cMode==MODE_Column   || pArg->cMode==MODE_Table   || pArg->cMode==MODE_Box   || pArg->cMode==MODE_Markdown  ){    exec_prepared_stmt_columnar(pArg, pStmt);    return;  }    rc = sqlite3_step(pStmt);    if( SQLITE_ROW == rc ){        int nCol = sqlite3_column_count(pStmt);    void *pData = sqlite3_malloc64(3*nCol*sizeof(const char*) + 1);    if( !pData ){      shell_out_of_memory();    }else{      char **azCols = (char **)pData;            char **azVals = &azCols[nCol];             int *aiTypes = (int *)&azVals[nCol];       int i, x;      assert(sizeof(int) <= sizeof(char *));            for(i=0; i<nCol; i++){        azCols[i] = (char *)sqlite3_column_name(pStmt, i);      }      do{        nRow++;                for(i=0; i<nCol; i++){          aiTypes[i] = x = sqlite3_column_type(pStmt, i);          if( x==SQLITE_BLOB           && pArg           && (pArg->cMode==MODE_Insert || pArg->cMode==MODE_Quote)          ){            azVals[i] = "";          }else{            azVals[i] = (char*)sqlite3_column_text(pStmt, i);          }          if( !azVals[i] && (aiTypes[i]!=SQLITE_NULL) ){            rc = SQLITE_NOMEM;            break;           }        }                 if( SQLITE_ROW == rc ){                    if( shell_callback(pArg, nCol, azVals, azCols, aiTypes) ){            rc = SQLITE_ABORT;          }else{            rc = sqlite3_step(pStmt);          }        }      } while( SQLITE_ROW == rc );      sqlite3_free(pData);      if( pArg->cMode==MODE_Json ){        fputs("]\n", pArg->out);      }else if( pArg->cMode==MODE_Count ){        char zBuf[200];        sqlite3_snprintf(sizeof(zBuf), zBuf, "%llu row%s\n",                         nRow, nRow!=1 ? "s" : "");        printf("%s", zBuf);      }    }  }}#ifndef SQLITE_OMIT_VIRTUALTABLEstatic int expertHandleSQL(  ShellState *pState,   const char *zSql,   char **pzErr){  assert( pState->expert.pExpert );  assert( pzErr==0 || *pzErr==0 );  return sqlite3_expert_sql(pState->expert.pExpert, zSql, pzErr);}static int expertFinish(  ShellState *pState,  int bCancel,  char **pzErr){  int rc = SQLITE_OK;  sqlite3expert *p = pState->expert.pExpert;  assert( p );  assert( bCancel || pzErr==0 || *pzErr==0 );  if( bCancel==0 ){    FILE *out = pState->out;    int bVerbose = pState->expert.bVerbose;    rc = sqlite3_expert_analyze(p, pzErr);    if( rc==SQLITE_OK ){      int nQuery = sqlite3_expert_count(p);      int i;      if( bVerbose ){        const char *zCand = sqlite3_expert_report(p,0,EXPERT_REPORT_CANDIDATES);        raw_printf(out, "-- Candidates -----------------------------\n");        raw_printf(out, "%s\n", zCand);      }      for(i=0; i<nQuery; i++){        const char *zSql = sqlite3_expert_report(p, i, EXPERT_REPORT_SQL);        const char *zIdx = sqlite3_expert_report(p, i, EXPERT_REPORT_INDEXES);        const char *zEQP = sqlite3_expert_report(p, i, EXPERT_REPORT_PLAN);        if( zIdx==0 ) zIdx = "(no new indexes)\n";        if( bVerbose ){          raw_printf(out, "-- Query %d --------------------------------\n",i+1);          raw_printf(out, "%s\n\n", zSql);        }        raw_printf(out, "%s\n", zIdx);        raw_printf(out, "%s\n", zEQP);      }    }  }  sqlite3_expert_destroy(p);  pState->expert.pExpert = 0;  return rc;}static int expertDotCommand(  ShellState *pState,               char **azArg,                     int nArg                        ){  int rc = SQLITE_OK;  char *zErr = 0;  int i;  int iSample = 0;  assert( pState->expert.pExpert==0 );  memset(&pState->expert, 0, sizeof(ExpertInfo));  for(i=1; rc==SQLITE_OK && i<nArg; i++){    char *z = azArg[i];    int n;    if( z[0]=='-' && z[1]=='-' ) z++;    n = strlen30(z);    if( n>=2 && 0==strncmp(z, "-verbose", n) ){      pState->expert.bVerbose = 1;    }    else if( n>=2 && 0==strncmp(z, "-sample", n) ){      if( i==(nArg-1) ){        raw_printf(stderr, "option requires an argument: %s\n", z);        rc = SQLITE_ERROR;      }else{        iSample = (int)integerValue(azArg[++i]);        if( iSample<0 || iSample>100 ){          raw_printf(stderr, "value out of range: %s\n", azArg[i]);          rc = SQLITE_ERROR;        }      }    }    else{      raw_printf(stderr, "unknown option: %s\n", z);      rc = SQLITE_ERROR;    }  }  if( rc==SQLITE_OK ){    pState->expert.pExpert = sqlite3_expert_new(pState->db, &zErr);    if( pState->expert.pExpert==0 ){      raw_printf(stderr, "sqlite3_expert_new: %s\n", zErr ? zErr : "out of memory");      rc = SQLITE_ERROR;    }else{      sqlite3_expert_config(          pState->expert.pExpert, EXPERT_CONFIG_SAMPLE, iSample      );    }  }  sqlite3_free(zErr);  return rc;}#endif static int shell_exec(  ShellState *pArg,                           const char *zSql,                           char **pzErrMsg                           ){  sqlite3_stmt *pStmt = NULL;       int rc = SQLITE_OK;               int rc2;  const char *zLeftover;            sqlite3 *db = pArg->db;  if( pzErrMsg ){    *pzErrMsg = NULL;  }#ifndef SQLITE_OMIT_VIRTUALTABLE  if( pArg->expert.pExpert ){    rc = expertHandleSQL(pArg, zSql, pzErrMsg);    return expertFinish(pArg, (rc!=SQLITE_OK), pzErrMsg);  }#endif  while( zSql[0] && (SQLITE_OK == rc) ){    static const char *zStmtSql;    rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zLeftover);    if( SQLITE_OK != rc ){      if( pzErrMsg ){        *pzErrMsg = save_err_msg(db, "in prepare", rc, zSql);      }    }else{      if( !pStmt ){                zSql = zLeftover;        while( IsSpace(zSql[0]) ) zSql++;        continue;      }      zStmtSql = sqlite3_sql(pStmt);      if( zStmtSql==0 ) zStmtSql = "";      while( IsSpace(zStmtSql[0]) ) zStmtSql++;            if( pArg ){        pArg->pStmt = pStmt;        pArg->cnt = 0;      }            if( pArg && ShellHasFlag(pArg, SHFLG_Echo) ){        utf8_printf(pArg->out, "%s\n", zStmtSql ? zStmtSql : zSql);      }            if( pArg && pArg->autoEQP && sqlite3_stmt_isexplain(pStmt)==0 ){        sqlite3_stmt *pExplain;        char *zEQP;        int triggerEQP = 0;        disable_debug_trace_modes();        sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, -1, &triggerEQP);        if( pArg->autoEQP>=AUTOEQP_trigger ){          sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, 1, 0);        }        zEQP = sqlite3_mprintf("EXPLAIN QUERY PLAN %s", zStmtSql);        shell_check_oom(zEQP);        rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);        if( rc==SQLITE_OK ){          while( sqlite3_step(pExplain)==SQLITE_ROW ){            const char *zEQPLine = (const char*)sqlite3_column_text(pExplain,3);            int iEqpId = sqlite3_column_int(pExplain, 0);            int iParentId = sqlite3_column_int(pExplain, 1);            if( zEQPLine==0 ) zEQPLine = "";            if( zEQPLine[0]=='-' ) eqp_render(pArg);            eqp_append(pArg, iEqpId, iParentId, zEQPLine);          }          eqp_render(pArg);        }        sqlite3_finalize(pExplain);        sqlite3_free(zEQP);        if( pArg->autoEQP>=AUTOEQP_full ){                    zEQP = sqlite3_mprintf("EXPLAIN %s", zStmtSql);          shell_check_oom(zEQP);          rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);          if( rc==SQLITE_OK ){            pArg->cMode = MODE_Explain;            explain_data_prepare(pArg, pExplain);            exec_prepared_stmt(pArg, pExplain);            explain_data_delete(pArg);          }          sqlite3_finalize(pExplain);          sqlite3_free(zEQP);        }        if( pArg->autoEQP>=AUTOEQP_trigger && triggerEQP==0 ){          sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, 0, 0);                    sqlite3_finalize(pStmt);          sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);          if( pArg ) pArg->pStmt = pStmt;        }        restore_debug_trace_modes();      }      if( pArg ){        pArg->cMode = pArg->mode;        if( pArg->autoExplain ){          if( sqlite3_stmt_isexplain(pStmt)==1 ){            pArg->cMode = MODE_Explain;          }          if( sqlite3_stmt_isexplain(pStmt)==2 ){            pArg->cMode = MODE_EQP;          }        }                if( pArg->cMode==MODE_Explain ){          explain_data_prepare(pArg, pStmt);        }      }      bind_prepared_stmt(pArg, pStmt);      exec_prepared_stmt(pArg, pStmt);      explain_data_delete(pArg);      eqp_render(pArg);            if( pArg && pArg->statsOn ){        display_stats(db, pArg, 0);      }            if( pArg && pArg->scanstatsOn ){        display_scanstats(db, pArg);      }            rc2 = sqlite3_finalize(pStmt);      if( rc!=SQLITE_NOMEM ) rc = rc2;      if( rc==SQLITE_OK ){        zSql = zLeftover;        while( IsSpace(zSql[0]) ) zSql++;      }else if( pzErrMsg ){        *pzErrMsg = save_err_msg(db, "stepping", rc, 0);      }            if( pArg ){        pArg->pStmt = NULL;      }    }  }   return rc;}static void freeColumnList(char **azCol){  int i;  for(i=1; azCol[i]; i++){    sqlite3_free(azCol[i]);  }    sqlite3_free(azCol);}static char **tableColumnList(ShellState *p, const char *zTab){  char **azCol = 0;  sqlite3_stmt *pStmt;  char *zSql;  int nCol = 0;  int nAlloc = 0;  int nPK = 0;         int isIPK = 0;       int preserveRowid = ShellHasFlag(p, SHFLG_PreserveRowid);  int rc;  zSql = sqlite3_mprintf("PRAGMA table_info=%Q", zTab);  shell_check_oom(zSql);  rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);  sqlite3_free(zSql);  if( rc ) return 0;  while( sqlite3_step(pStmt)==SQLITE_ROW ){    if( nCol>=nAlloc-2 ){      nAlloc = nAlloc*2 + nCol + 10;      azCol = sqlite3_realloc(azCol, nAlloc*sizeof(azCol[0]));      shell_check_oom(azCol);    }    azCol[++nCol] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 1));    shell_check_oom(azCol[nCol]);    if( sqlite3_column_int(pStmt, 5) ){      nPK++;      if( nPK==1       && sqlite3_stricmp((const char*)sqlite3_column_text(pStmt,2),                          "INTEGER")==0      ){        isIPK = 1;      }else{        isIPK = 0;      }    }  }  sqlite3_finalize(pStmt);  if( azCol==0 ) return 0;  azCol[0] = 0;  azCol[nCol+1] = 0;    if( preserveRowid && isIPK ){        zSql = sqlite3_mprintf("SELECT 1 FROM pragma_index_list(%Q)"                           " WHERE origin='pk'", zTab);    shell_check_oom(zSql);    rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);    sqlite3_free(zSql);    if( rc ){      freeColumnList(azCol);      return 0;    }    rc = sqlite3_step(pStmt);    sqlite3_finalize(pStmt);    preserveRowid = rc==SQLITE_ROW;  }  if( preserveRowid ){        static char *azRowid[] = { "rowid", "_rowid_", "oid" };    int i, j;    for(j=0; j<3; j++){      for(i=1; i<=nCol; i++){        if( sqlite3_stricmp(azRowid[j],azCol[i])==0 ) break;      }      if( i>nCol ){                rc = sqlite3_table_column_metadata(p->db,0,zTab,azRowid[j],0,0,0,0,0);        if( rc==SQLITE_OK ) azCol[0] = azRowid[j];        break;      }    }  }  return azCol;}static void toggleSelectOrder(sqlite3 *db){  sqlite3_stmt *pStmt = 0;  int iSetting = 0;  char zStmt[100];  sqlite3_prepare_v2(db, "PRAGMA reverse_unordered_selects", -1, &pStmt, 0);  if( sqlite3_step(pStmt)==SQLITE_ROW ){    iSetting = sqlite3_column_int(pStmt, 0);  }  sqlite3_finalize(pStmt);  sqlite3_snprintf(sizeof(zStmt), zStmt,       "PRAGMA reverse_unordered_selects(%d)", !iSetting);  sqlite3_exec(db, zStmt, 0, 0, 0);}static int dump_callback(void *pArg, int nArg, char **azArg, char **azNotUsed){  int rc;  const char *zTable;  const char *zType;  const char *zSql;  ShellState *p = (ShellState *)pArg;  int dataOnly;  int noSys;  UNUSED_PARAMETER(azNotUsed);  if( nArg!=3 || azArg==0 ) return 0;  zTable = azArg[0];  zType = azArg[1];  zSql = azArg[2];  dataOnly = (p->shellFlgs & SHFLG_DumpDataOnly)!=0;  noSys    = (p->shellFlgs & SHFLG_DumpNoSys)!=0;  if( strcmp(zTable, "sqlite_sequence")==0 && !noSys ){    if( !dataOnly ) raw_printf(p->out, "DELETE FROM sqlite_sequence;\n");  }else if( sqlite3_strglob("sqlite_stat?", zTable)==0 && !noSys ){    if( !dataOnly ) raw_printf(p->out, "ANALYZE sqlite_schema;\n");  }else if( strncmp(zTable, "sqlite_", 7)==0 ){    return 0;  }else if( dataOnly ){      }else if( strncmp(zSql, "CREATE VIRTUAL TABLE", 20)==0 ){    char *zIns;    if( !p->writableSchema ){      raw_printf(p->out, "PRAGMA writable_schema=ON;\n");      p->writableSchema = 1;    }    zIns = sqlite3_mprintf(       "INSERT INTO sqlite_schema(type,name,tbl_name,rootpage,sql)"       "VALUES('table','%q','%q',0,'%q');",       zTable, zTable, zSql);    shell_check_oom(zIns);    utf8_printf(p->out, "%s\n", zIns);    sqlite3_free(zIns);    return 0;  }else{    printSchemaLine(p->out, zSql, ";\n");  }  if( strcmp(zType, "table")==0 ){    ShellText sSelect;    ShellText sTable;    char **azCol;    int i;    char *savedDestTable;    int savedMode;    azCol = tableColumnList(p, zTable);    if( azCol==0 ){      p->nErr++;      return 0;    }        initText(&sTable);    appendText(&sTable, zTable, quoteChar(zTable));        if( azCol[0] ){      appendText(&sTable, "(", 0);      appendText(&sTable, azCol[0], 0);      for(i=1; azCol[i]; i++){        appendText(&sTable, ",", 0);        appendText(&sTable, azCol[i], quoteChar(azCol[i]));      }      appendText(&sTable, ")", 0);    }        initText(&sSelect);    appendText(&sSelect, "SELECT ", 0);    if( azCol[0] ){      appendText(&sSelect, azCol[0], 0);      appendText(&sSelect, ",", 0);    }    for(i=1; azCol[i]; i++){      appendText(&sSelect, azCol[i], quoteChar(azCol[i]));      if( azCol[i+1] ){        appendText(&sSelect, ",", 0);      }    }    freeColumnList(azCol);    appendText(&sSelect, " FROM ", 0);    appendText(&sSelect, zTable, quoteChar(zTable));    savedDestTable = p->zDestTable;    savedMode = p->mode;    p->zDestTable = sTable.z;    p->mode = p->cMode = MODE_Insert;    rc = shell_exec(p, sSelect.z, 0);    if( (rc&0xff)==SQLITE_CORRUPT ){      raw_printf(p->out, "/****** CORRUPTION ERROR *******/\n");      toggleSelectOrder(p->db);      shell_exec(p, sSelect.z, 0);      toggleSelectOrder(p->db);    }    p->zDestTable = savedDestTable;    p->mode = savedMode;    freeText(&sTable);    freeText(&sSelect);    if( rc ) p->nErr++;  }  return 0;}static int run_schema_dump_query(  ShellState *p,  const char *zQuery){  int rc;  char *zErr = 0;  rc = sqlite3_exec(p->db, zQuery, dump_callback, p, &zErr);  if( rc==SQLITE_CORRUPT ){    char *zQ2;    int len = strlen30(zQuery);    raw_printf(p->out, "/****** CORRUPTION ERROR *******/\n");    if( zErr ){      utf8_printf(p->out, "/****** %s ******/\n", zErr);      sqlite3_free(zErr);      zErr = 0;    }    zQ2 = malloc( len+100 );    if( zQ2==0 ) return rc;    sqlite3_snprintf(len+100, zQ2, "%s ORDER BY rowid DESC", zQuery);    rc = sqlite3_exec(p->db, zQ2, dump_callback, p, &zErr);    if( rc ){      utf8_printf(p->out, "/****** ERROR: %s ******/\n", zErr);    }else{      rc = SQLITE_CORRUPT;    }    sqlite3_free(zErr);    free(zQ2);  }  return rc;}static const char *(azHelp[]) = {#if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE)  ".archive ...             Manage SQL archives",  "   Each command must have exactly one of the following options:",  "     -c, --create               Create a new archive",  "     -u, --update               Add or update files with changed mtime",  "     -i, --insert               Like -u but always add even if unchanged",  "     -r, --remove               Remove files from archive",  "     -t, --list                 List contents of archive",  "     -x, --extract              Extract files from archive",  "   Optional arguments:",  "     -v, --verbose              Print each filename as it is processed",  "     -f FILE, --file FILE       Use archive FILE (default is current db)",  "     -a FILE, --append FILE     Open FILE using the apndvfs VFS",  "     -C DIR, --directory DIR    Read/extract files from directory DIR",  "     -g, --glob                 Use glob matching for names in archive",  "     -n, --dryrun               Show the SQL that would have occurred",  "   Examples:",  "     .ar -cf ARCHIVE foo bar  # Create ARCHIVE from files foo and bar",  "     .ar -tf ARCHIVE          # List members of ARCHIVE",  "     .ar -xvf ARCHIVE         # Verbosely extract files from ARCHIVE",  "   See also:",  "      http://sqlite.org/cli.html#sqlite_archive_support",#endif#ifndef SQLITE_OMIT_AUTHORIZATION  ".auth ON|OFF             Show authorizer callbacks",#endif  ".backup ?DB? FILE        Backup DB (default \"main\") to FILE",  "   Options:",  "       --append            Use the appendvfs",  "       --async             Write to FILE without journal and fsync()",  ".bail on|off             Stop after hitting an error.  Default OFF",  ".binary on|off           Turn binary output on or off.  Default OFF",  ".cd DIRECTORY            Change the working directory to DIRECTORY",  ".changes on|off          Show number of rows changed by SQL",  ".check GLOB              Fail if output since .testcase does not match",  ".clone NEWDB             Clone data into NEWDB from the existing database",  ".connection [close] [#]  Open or close an auxiliary database connection",  ".databases               List names and files of attached databases",  ".dbconfig ?op? ?val?     List or change sqlite3_db_config() options",  ".dbinfo ?DB?             Show status information about the database",  ".dump ?OBJECTS?          Render database content as SQL",  "   Options:",  "     --data-only            Output only INSERT statements",  "     --newlines             Allow unescaped newline characters in output",  "     --nosys                Omit system tables (ex: \"sqlite_stat1\")",  "     --preserve-rowids      Include ROWID values in the output",  "   OBJECTS is a LIKE pattern for tables, indexes, triggers or views to dump",  "   Additional LIKE patterns can be given in subsequent arguments",  ".echo on|off             Turn command echo on or off",  ".eqp on|off|full|...     Enable or disable automatic EXPLAIN QUERY PLAN",  "   Other Modes:",#ifdef SQLITE_DEBUG  "      test                  Show raw EXPLAIN QUERY PLAN output",  "      trace                 Like \"full\" but enable \"PRAGMA vdbe_trace\"",#endif  "      trigger               Like \"full\" but also show trigger bytecode",  ".excel                   Display the output of next command in spreadsheet",  "   --bom                   Put a UTF8 byte-order mark on intermediate file",  ".exit ?CODE?             Exit this program with return-code CODE",  ".expert                  EXPERIMENTAL. Suggest indexes for queries",  ".explain ?on|off|auto?   Change the EXPLAIN formatting mode.  Default: auto",  ".filectrl CMD ...        Run various sqlite3_file_control() operations",  "   --schema SCHEMA         Use SCHEMA instead of \"main\"",  "   --help                  Show CMD details",  ".fullschema ?--indent?   Show schema and the content of sqlite_stat tables",  ".headers on|off          Turn display of headers on or off",  ".help ?-all? ?PATTERN?   Show help text for PATTERN",  ".import FILE TABLE       Import data from FILE into TABLE",  "   Options:",  "     --ascii               Use \\037 and \\036 as column and row separators",  "     --csv                 Use , and \\n as column and row separators",  "     --skip N              Skip the first N rows of input",  "     --schema S            Target table to be S.TABLE",  "     -v                    \"Verbose\" - increase auxiliary output",  "   Notes:",  "     *  If TABLE does not exist, it is created.  The first row of input",  "        determines the column names.",  "     *  If neither --csv or --ascii are used, the input mode is derived",  "        from the \".mode\" output mode",  "     *  If FILE begins with \"|\" then it is a command that generates the",  "        input text.",#ifndef SQLITE_OMIT_TEST_CONTROL  ".imposter INDEX TABLE    Create imposter table TABLE on index INDEX",#endif  ".indexes ?TABLE?         Show names of indexes",  "                           If TABLE is specified, only show indexes for",  "                           tables matching TABLE using the LIKE operator.",#ifdef SQLITE_ENABLE_IOTRACE  ".iotrace FILE            Enable I/O diagnostic logging to FILE",#endif  ".limit ?LIMIT? ?VAL?     Display or change the value of an SQLITE_LIMIT",  ".lint OPTIONS            Report potential schema issues.",  "     Options:",  "        fkey-indexes     Find missing foreign key indexes",#ifndef SQLITE_OMIT_LOAD_EXTENSION  ".load FILE ?ENTRY?       Load an extension library",#endif  ".log FILE|off            Turn logging on or off.  FILE can be stderr/stdout",  ".mode MODE ?OPTIONS?     Set output mode",  "   MODE is one of:",  "     ascii       Columns/rows delimited by 0x1F and 0x1E",  "     box         Tables using unicode box-drawing characters",  "     csv         Comma-separated values",  "     column      Output in columns.  (See .width)",  "     html        HTML <table> code",  "     insert      SQL insert statements for TABLE",  "     json        Results in a JSON array",  "     line        One value per line",  "     list        Values delimited by \"|\"",  "     markdown    Markdown table format",  "     qbox        Shorthand for \"box --width 60 --quote\"",  "     quote       Escape answers as for SQL",  "     table       ASCII-art table",  "     tabs        Tab-separated values",  "     tcl         TCL list elements",  "   OPTIONS: (for columnar modes or insert mode):",  "     --wrap N       Wrap output lines to no longer than N characters",  "     --wordwrap B   Wrap or not at word boundaries per B (on/off)",  "     --ww           Shorthand for \"--wordwrap 1\"",  "     --quote        Quote output text as SQL literals",  "     --noquote      Do not quote output text",  "     TABLE          The name of SQL table used for \"insert\" mode",  ".nonce STRING            Suspend safe mode for one command if nonce matches",  ".nullvalue STRING        Use STRING in place of NULL values",  ".once ?OPTIONS? ?FILE?   Output for the next SQL command only to FILE",  "     If FILE begins with '|' then open as a pipe",  "       --bom  Put a UTF8 byte-order mark at the beginning",  "       -e     Send output to the system text editor",  "       -x     Send output as CSV to a spreadsheet (same as \".excel\")",  ".open ?OPTIONS? ?FILE?   Close existing database and reopen FILE",  "     Options:",  "        --append        Use appendvfs to append database to the end of FILE",#ifndef SQLITE_OMIT_DESERIALIZE  "        --deserialize   Load into memory using sqlite3_deserialize()",  "        --hexdb         Load the output of \"dbtotxt\" as an in-memory db",  "        --maxsize N     Maximum size for --hexdb or --deserialized database",#endif  "        --new           Initialize FILE to an empty database",  "        --nofollow      Do not follow symbolic links",  "        --readonly      Open FILE readonly",  "        --zip           FILE is a ZIP archive",  ".output ?FILE?           Send output to FILE or stdout if FILE is omitted",  "   If FILE begins with '|' then open it as a pipe.",  "   Options:",  "     --bom                 Prefix output with a UTF8 byte-order mark",  "     -e                    Send output to the system text editor",  "     -x                    Send output as CSV to a spreadsheet",  ".parameter CMD ...       Manage SQL parameter bindings",  "   clear                   Erase all bindings",  "   init                    Initialize the TEMP table that holds bindings",  "   list                    List the current parameter bindings",  "   set PARAMETER VALUE     Given SQL parameter PARAMETER a value of VALUE",  "                           PARAMETER should start with one of: $ : @ ?",  "   unset PARAMETER         Remove PARAMETER from the binding table",  ".print STRING...         Print literal STRING",#ifndef SQLITE_OMIT_PROGRESS_CALLBACK  ".progress N              Invoke progress handler after every N opcodes",  "   --limit N                 Interrupt after N progress callbacks",  "   --once                    Do no more than one progress interrupt",  "   --quiet|-q                No output except at interrupts",  "   --reset                   Reset the count for each input and interrupt",#endif  ".prompt MAIN CONTINUE    Replace the standard prompts",  ".quit                    Exit this program",  ".read FILE               Read input from FILE or command output",  "    If FILE begins with \"|\", it is a command that generates the input.",#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)  ".recover                 Recover as much data as possible from corrupt db.",  "   --freelist-corrupt       Assume the freelist is corrupt",  "   --recovery-db NAME       Store recovery metadata in database file NAME",  "   --lost-and-found TABLE   Alternative name for the lost-and-found table",  "   --no-rowids              Do not attempt to recover rowid values",  "                            that are not also INTEGER PRIMARY KEYs",#endif  ".restore ?DB? FILE       Restore content of DB (default \"main\") from FILE",  ".save ?OPTIONS? FILE     Write database to FILE (an alias for .backup ...)",  ".scanstats on|off        Turn sqlite3_stmt_scanstatus() metrics on or off",  ".schema ?PATTERN?        Show the CREATE statements matching PATTERN",  "   Options:",  "      --indent             Try to pretty-print the schema",  "      --nosys              Omit objects whose names start with \"sqlite_\"",  ".selftest ?OPTIONS?      Run tests defined in the SELFTEST table",  "    Options:",  "       --init               Create a new SELFTEST table",  "       -v                   Verbose output",  ".separator COL ?ROW?     Change the column and row separators",#if defined(SQLITE_ENABLE_SESSION)  ".session ?NAME? CMD ...  Create or control sessions",  "   Subcommands:",  "     attach TABLE             Attach TABLE",  "     changeset FILE           Write a changeset into FILE",  "     close                    Close one session",  "     enable ?BOOLEAN?         Set or query the enable bit",  "     filter GLOB...           Reject tables matching GLOBs",  "     indirect ?BOOLEAN?       Mark or query the indirect status",  "     isempty                  Query whether the session is empty",  "     list                     List currently open session names",  "     open DB NAME             Open a new session on DB",  "     patchset FILE            Write a patchset into FILE",  "   If ?NAME? is omitted, the first defined session is used.",#endif  ".sha3sum ...             Compute a SHA3 hash of database content",  "    Options:",  "      --schema              Also hash the sqlite_schema table",  "      --sha3-224            Use the sha3-224 algorithm",  "      --sha3-256            Use the sha3-256 algorithm (default)",  "      --sha3-384            Use the sha3-384 algorithm",  "      --sha3-512            Use the sha3-512 algorithm",  "    Any other argument is a LIKE pattern for tables to hash",#ifndef SQLITE_NOHAVE_SYSTEM  ".shell CMD ARGS...       Run CMD ARGS... in a system shell",#endif  ".show                    Show the current values for various settings",  ".stats ?ARG?             Show stats or turn stats on or off",  "   off                      Turn off automatic stat display",  "   on                       Turn on automatic stat display",  "   stmt                     Show statement stats",  "   vmstep                   Show the virtual machine step count only",#ifndef SQLITE_NOHAVE_SYSTEM  ".system CMD ARGS...      Run CMD ARGS... in a system shell",#endif  ".tables ?TABLE?          List names of tables matching LIKE pattern TABLE",  ".testcase NAME           Begin redirecting output to 'testcase-out.txt'",  ".testctrl CMD ...        Run various sqlite3_test_control() operations",  "                           Run \".testctrl\" with no arguments for details",  ".timeout MS              Try opening locked tables for MS milliseconds",  ".timer on|off            Turn SQL timer on or off",#ifndef SQLITE_OMIT_TRACE  ".trace ?OPTIONS?         Output each SQL statement as it is run",  "    FILE                    Send output to FILE",  "    stdout                  Send output to stdout",  "    stderr                  Send output to stderr",  "    off                     Disable tracing",  "    --expanded              Expand query parameters",#ifdef SQLITE_ENABLE_NORMALIZE  "    --normalized            Normal the SQL statements",#endif  "    --plain                 Show SQL as it is input",  "    --stmt                  Trace statement execution (SQLITE_TRACE_STMT)",  "    --profile               Profile statements (SQLITE_TRACE_PROFILE)",  "    --row                   Trace each row (SQLITE_TRACE_ROW)",  "    --close                 Trace connection close (SQLITE_TRACE_CLOSE)",#endif #ifdef SQLITE_DEBUG  ".unmodule NAME ...       Unregister virtual table modules",  "    --allexcept             Unregister everything except those named",#endif  ".vfsinfo ?AUX?           Information about the top-level VFS",  ".vfslist                 List all available VFSes",  ".vfsname ?AUX?           Print the name of the VFS stack",  ".width NUM1 NUM2 ...     Set minimum column widths for columnar output",  "     Negative values right-justify",};static int showHelp(FILE *out, const char *zPattern){  int i = 0;  int j = 0;  int n = 0;  char *zPat;  if( zPattern==0   || zPattern[0]=='0'   || strcmp(zPattern,"-a")==0   || strcmp(zPattern,"-all")==0   || strcmp(zPattern,"--all")==0  ){        if( zPattern==0 ) zPattern = "";    for(i=0; i<ArraySize(azHelp); i++){      if( azHelp[i][0]=='.' || zPattern[0] ){        utf8_printf(out, "%s\n", azHelp[i]);        n++;      }    }  }else{        zPat = sqlite3_mprintf(".%s*", zPattern);    shell_check_oom(zPat);    for(i=0; i<ArraySize(azHelp); i++){      if( sqlite3_strglob(zPat, azHelp[i])==0 ){        utf8_printf(out, "%s\n", azHelp[i]);        j = i+1;        n++;      }    }    sqlite3_free(zPat);    if( n ){      if( n==1 ){                while( j<ArraySize(azHelp)-1 && azHelp[j][0]!='.' ){          utf8_printf(out, "%s\n", azHelp[j]);          j++;        }      }      return n;    }        zPat = sqlite3_mprintf("%%%s%%", zPattern);    shell_check_oom(zPat);    for(i=0; i<ArraySize(azHelp); i++){      if( azHelp[i][0]=='.' ) j = i;      if( sqlite3_strlike(zPat, azHelp[i], 0)==0 ){        utf8_printf(out, "%s\n", azHelp[j]);        while( j<ArraySize(azHelp)-1 && azHelp[j+1][0]!='.' ){          j++;          utf8_printf(out, "%s\n", azHelp[j]);        }        i = j;        n++;      }    }    sqlite3_free(zPat);  }  return n;}static int process_input(ShellState *p);static char *readFile(const char *zName, int *pnByte){  FILE *in = fopen(zName, "rb");  long nIn;  size_t nRead;  char *pBuf;  if( in==0 ) return 0;  fseek(in, 0, SEEK_END);  nIn = ftell(in);  rewind(in);  pBuf = sqlite3_malloc64( nIn+1 );  if( pBuf==0 ){ fclose(in); return 0; }  nRead = fread(pBuf, nIn, 1, in);  fclose(in);  if( nRead!=1 ){    sqlite3_free(pBuf);    return 0;  }  pBuf[nIn] = 0;  if( pnByte ) *pnByte = nIn;  return pBuf;}#if defined(SQLITE_ENABLE_SESSION)static void session_close(OpenSession *pSession){  int i;  sqlite3session_delete(pSession->p);  sqlite3_free(pSession->zName);  for(i=0; i<pSession->nFilter; i++){    sqlite3_free(pSession->azFilter[i]);  }  sqlite3_free(pSession->azFilter);  memset(pSession, 0, sizeof(OpenSession));}#endif#if defined(SQLITE_ENABLE_SESSION)static void session_close_all(ShellState *p, int i){  int j;  struct AuxDb *pAuxDb = i<0 ? p->pAuxDb : &p->aAuxDb[i];  for(j=0; j<pAuxDb->nSession; j++){    session_close(&pAuxDb->aSession[j]);  }  pAuxDb->nSession = 0;}#else# define session_close_all(X,Y)#endif#if defined(SQLITE_ENABLE_SESSION)static int session_filter(void *pCtx, const char *zTab){  OpenSession *pSession = (OpenSession*)pCtx;  int i;  for(i=0; i<pSession->nFilter; i++){    if( sqlite3_strglob(pSession->azFilter[i], zTab)==0 ) return 0;  }  return 1;}#endifint deduceDatabaseType(const char *zName, int dfltZip){  FILE *f = fopen(zName, "rb");  size_t n;  int rc = SHELL_OPEN_UNSPEC;  char zBuf[100];  if( f==0 ){    if( dfltZip && sqlite3_strlike("%.zip",zName,0)==0 ){       return SHELL_OPEN_ZIPFILE;    }else{       return SHELL_OPEN_NORMAL;    }  }  n = fread(zBuf, 16, 1, f);  if( n==1 && memcmp(zBuf, "SQLite format 3", 16)==0 ){    fclose(f);    return SHELL_OPEN_NORMAL;  }  fseek(f, -25, SEEK_END);  n = fread(zBuf, 25, 1, f);  if( n==1 && memcmp(zBuf, "Start-Of-SQLite3-", 17)==0 ){    rc = SHELL_OPEN_APPENDVFS;  }else{    fseek(f, -22, SEEK_END);    n = fread(zBuf, 22, 1, f);    if( n==1 && zBuf[0]==0x50 && zBuf[1]==0x4b && zBuf[2]==0x05       && zBuf[3]==0x06 ){      rc = SHELL_OPEN_ZIPFILE;    }else if( n==0 && dfltZip && sqlite3_strlike("%.zip",zName,0)==0 ){      rc = SHELL_OPEN_ZIPFILE;    }  }  fclose(f);  return rc;  }#ifndef SQLITE_OMIT_DESERIALIZEstatic unsigned char *readHexDb(ShellState *p, int *pnData){  unsigned char *a = 0;  int nLine;  int n = 0;  int pgsz = 0;  int iOffset = 0;  int j, k;  int rc;  FILE *in;  const char *zDbFilename = p->pAuxDb->zDbFilename;  unsigned int x[16];  char zLine[1000];  if( zDbFilename ){    in = fopen(zDbFilename, "r");    if( in==0 ){      utf8_printf(stderr, "cannot open \"%s\" for reading\n", zDbFilename);      return 0;    }    nLine = 0;  }else{    in = p->in;    nLine = p->lineno;    if( in==0 ) in = stdin;  }  *pnData = 0;  nLine++;  if( fgets(zLine, sizeof(zLine), in)==0 ) goto readHexDb_error;  rc = sscanf(zLine, "| size %d pagesize %d", &n, &pgsz);  if( rc!=2 ) goto readHexDb_error;  if( n<0 ) goto readHexDb_error;  if( pgsz<512 || pgsz>65536 || (pgsz&(pgsz-1))!=0 ) goto readHexDb_error;  n = (n+pgsz-1)&~(pgsz-1);    a = sqlite3_malloc( n ? n : 1 );  shell_check_oom(a);  memset(a, 0, n);  if( pgsz<512 || pgsz>65536 || (pgsz & (pgsz-1))!=0 ){    utf8_printf(stderr, "invalid pagesize\n");    goto readHexDb_error;  }  for(nLine++; fgets(zLine, sizeof(zLine), in)!=0; nLine++){    rc = sscanf(zLine, "| page %d offset %d", &j, &k);    if( rc==2 ){      iOffset = k;      continue;    }    if( strncmp(zLine, "| end ", 6)==0 ){      break;    }    rc = sscanf(zLine,"| %d: %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x",                &j, &x[0], &x[1], &x[2], &x[3], &x[4], &x[5], &x[6], &x[7],                &x[8], &x[9], &x[10], &x[11], &x[12], &x[13], &x[14], &x[15]);    if( rc==17 ){      k = iOffset+j;      if( k+16<=n && k>=0 ){        int ii;        for(ii=0; ii<16; ii++) a[k+ii] = x[ii]&0xff;      }    }  }  *pnData = n;  if( in!=p->in ){    fclose(in);  }else{    p->lineno = nLine;  }  return a;readHexDb_error:  if( in!=p->in ){    fclose(in);  }else{    while( fgets(zLine, sizeof(zLine), p->in)!=0 ){      nLine++;      if(strncmp(zLine, "| end ", 6)==0 ) break;    }    p->lineno = nLine;  }  sqlite3_free(a);  utf8_printf(stderr,"Error on line %d of --hexdb input\n", nLine);  return 0;}#endif static void shellInt32(  sqlite3_context *context,   int argc,   sqlite3_value **argv){  const unsigned char *pBlob;  int nBlob;  int iInt;  UNUSED_PARAMETER(argc);  nBlob = sqlite3_value_bytes(argv[0]);  pBlob = (const unsigned char*)sqlite3_value_blob(argv[0]);  iInt = sqlite3_value_int(argv[1]);  if( iInt>=0 && (iInt+1)*4<=nBlob ){    const unsigned char *a = &pBlob[iInt*4];    sqlite3_int64 iVal = ((sqlite3_int64)a[0]<<24)                       + ((sqlite3_int64)a[1]<<16)                       + ((sqlite3_int64)a[2]<< 8)                       + ((sqlite3_int64)a[3]<< 0);    sqlite3_result_int64(context, iVal);  }}static void shellIdQuote(  sqlite3_context *context,   int argc,   sqlite3_value **argv){  const char *zName = (const char*)sqlite3_value_text(argv[0]);  UNUSED_PARAMETER(argc);  if( zName ){    char *z = sqlite3_mprintf("\"%w\"", zName);    sqlite3_result_text(context, z, -1, sqlite3_free);  }}static void shellUSleepFunc(  sqlite3_context *context,   int argcUnused,   sqlite3_value **argv){  int sleep = sqlite3_value_int(argv[0]);  (void)argcUnused;  sqlite3_sleep(sleep/1000);  sqlite3_result_int(context, sleep);}static void shellEscapeCrnl(  sqlite3_context *context,   int argc,   sqlite3_value **argv){  const char *zText = (const char*)sqlite3_value_text(argv[0]);  UNUSED_PARAMETER(argc);  if( zText && zText[0]=='\'' ){    int nText = sqlite3_value_bytes(argv[0]);    int i;    char zBuf1[20];    char zBuf2[20];    const char *zNL = 0;    const char *zCR = 0;    int nCR = 0;    int nNL = 0;    for(i=0; zText[i]; i++){      if( zNL==0 && zText[i]=='\n' ){        zNL = unused_string(zText, "\\n", "\\012", zBuf1);        nNL = (int)strlen(zNL);      }      if( zCR==0 && zText[i]=='\r' ){        zCR = unused_string(zText, "\\r", "\\015", zBuf2);        nCR = (int)strlen(zCR);      }    }    if( zNL || zCR ){      int iOut = 0;      i64 nMax = (nNL > nCR) ? nNL : nCR;      i64 nAlloc = nMax * nText + (nMax+64)*2;      char *zOut = (char*)sqlite3_malloc64(nAlloc);      if( zOut==0 ){        sqlite3_result_error_nomem(context);        return;      }      if( zNL && zCR ){        memcpy(&zOut[iOut], "replace(replace(", 16);        iOut += 16;      }else{        memcpy(&zOut[iOut], "replace(", 8);        iOut += 8;      }      for(i=0; zText[i]; i++){        if( zText[i]=='\n' ){          memcpy(&zOut[iOut], zNL, nNL);          iOut += nNL;        }else if( zText[i]=='\r' ){          memcpy(&zOut[iOut], zCR, nCR);          iOut += nCR;        }else{          zOut[iOut] = zText[i];          iOut++;        }      }      if( zNL ){        memcpy(&zOut[iOut], ",'", 2); iOut += 2;        memcpy(&zOut[iOut], zNL, nNL); iOut += nNL;        memcpy(&zOut[iOut], "', char(10))", 12); iOut += 12;      }      if( zCR ){        memcpy(&zOut[iOut], ",'", 2); iOut += 2;        memcpy(&zOut[iOut], zCR, nCR); iOut += nCR;        memcpy(&zOut[iOut], "', char(13))", 12); iOut += 12;      }      sqlite3_result_text(context, zOut, iOut, SQLITE_TRANSIENT);      sqlite3_free(zOut);      return;    }  }  sqlite3_result_value(context, argv[0]);}#define OPEN_DB_KEEPALIVE   0x001   #define OPEN_DB_ZIPFILE     0x002   static void open_db(ShellState *p, int openFlags){  if( p->db==0 ){    const char *zDbFilename = p->pAuxDb->zDbFilename;    if( p->openMode==SHELL_OPEN_UNSPEC ){      if( zDbFilename==0 || zDbFilename[0]==0 ){        p->openMode = SHELL_OPEN_NORMAL;      }else{        p->openMode = (u8)deduceDatabaseType(zDbFilename,                              (openFlags & OPEN_DB_ZIPFILE)!=0);      }    }    switch( p->openMode ){      case SHELL_OPEN_APPENDVFS: {        sqlite3_open_v2(zDbFilename, &p->db,            SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|p->openFlags, "apndvfs");        break;      }      case SHELL_OPEN_HEXDB:      case SHELL_OPEN_DESERIALIZE: {        sqlite3_open(0, &p->db);        break;      }      case SHELL_OPEN_ZIPFILE: {        sqlite3_open(":memory:", &p->db);        break;      }      case SHELL_OPEN_READONLY: {        sqlite3_open_v2(zDbFilename, &p->db,            SQLITE_OPEN_READONLY|p->openFlags, 0);        break;      }      case SHELL_OPEN_UNSPEC:      case SHELL_OPEN_NORMAL: {        sqlite3_open_v2(zDbFilename, &p->db,           SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|p->openFlags, 0);        break;      }    }    globalDb = p->db;    if( p->db==0 || SQLITE_OK!=sqlite3_errcode(p->db) ){      utf8_printf(stderr,"Error: unable to open database \"%s\": %s\n",          zDbFilename, sqlite3_errmsg(p->db));      if( openFlags & OPEN_DB_KEEPALIVE ){        sqlite3_open(":memory:", &p->db);        return;      }      exit(1);    }#ifndef SQLITE_OMIT_LOAD_EXTENSION    sqlite3_enable_load_extension(p->db, 1);#endif    sqlite3_fileio_init(p->db, 0, 0);    sqlite3_shathree_init(p->db, 0, 0);    sqlite3_completion_init(p->db, 0, 0);    sqlite3_uint_init(p->db, 0, 0);    sqlite3_decimal_init(p->db, 0, 0);    sqlite3_regexp_init(p->db, 0, 0);    sqlite3_ieee_init(p->db, 0, 0);    sqlite3_series_init(p->db, 0, 0);#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)    sqlite3_dbdata_init(p->db, 0, 0);#endif#ifdef SQLITE_HAVE_ZLIB    sqlite3_zipfile_init(p->db, 0, 0);    sqlite3_sqlar_init(p->db, 0, 0);#endif    sqlite3_create_function(p->db, "shell_add_schema", 3, SQLITE_UTF8, 0,                            shellAddSchemaName, 0, 0);    sqlite3_create_function(p->db, "shell_module_schema", 1, SQLITE_UTF8, 0,                            shellModuleSchema, 0, 0);    sqlite3_create_function(p->db, "shell_putsnl", 1, SQLITE_UTF8, p,                            shellPutsFunc, 0, 0);    sqlite3_create_function(p->db, "shell_escape_crnl", 1, SQLITE_UTF8, 0,                            shellEscapeCrnl, 0, 0);    sqlite3_create_function(p->db, "shell_int32", 2, SQLITE_UTF8, 0,                            shellInt32, 0, 0);    sqlite3_create_function(p->db, "shell_idquote", 1, SQLITE_UTF8, 0,                            shellIdQuote, 0, 0);    sqlite3_create_function(p->db, "usleep",1,SQLITE_UTF8,0,                            shellUSleepFunc, 0, 0);#ifndef SQLITE_NOHAVE_SYSTEM    sqlite3_create_function(p->db, "edit", 1, SQLITE_UTF8, 0,                            editFunc, 0, 0);    sqlite3_create_function(p->db, "edit", 2, SQLITE_UTF8, 0,                            editFunc, 0, 0);#endif    if( p->openMode==SHELL_OPEN_ZIPFILE ){      char *zSql = sqlite3_mprintf(         "CREATE VIRTUAL TABLE zip USING zipfile(%Q);", zDbFilename);      shell_check_oom(zSql);      sqlite3_exec(p->db, zSql, 0, 0, 0);      sqlite3_free(zSql);    }#ifndef SQLITE_OMIT_DESERIALIZE    else    if( p->openMode==SHELL_OPEN_DESERIALIZE || p->openMode==SHELL_OPEN_HEXDB ){      int rc;      int nData = 0;      unsigned char *aData;      if( p->openMode==SHELL_OPEN_DESERIALIZE ){        aData = (unsigned char*)readFile(zDbFilename, &nData);      }else{        aData = readHexDb(p, &nData);        if( aData==0 ){          return;        }      }      rc = sqlite3_deserialize(p->db, "main", aData, nData, nData,                   SQLITE_DESERIALIZE_RESIZEABLE |                   SQLITE_DESERIALIZE_FREEONCLOSE);      if( rc ){        utf8_printf(stderr, "Error: sqlite3_deserialize() returns %d\n", rc);      }      if( p->szMax>0 ){        sqlite3_file_control(p->db, "main", SQLITE_FCNTL_SIZE_LIMIT, &p->szMax);      }    }#endif  }  if( p->bSafeModePersist && p->db!=0 ){    sqlite3_set_authorizer(p->db, safeModeAuth, p);  }}void close_db(sqlite3 *db){  int rc = sqlite3_close(db);  if( rc ){    utf8_printf(stderr, "Error: sqlite3_close() returns %d: %s\n",        rc, sqlite3_errmsg(db));  } }#if HAVE_READLINE || HAVE_EDITLINEstatic char *readline_completion_generator(const char *text, int state){  static sqlite3_stmt *pStmt = 0;  char *zRet;  if( state==0 ){    char *zSql;    sqlite3_finalize(pStmt);    zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase"                           "  FROM completion(%Q) ORDER BY 1", text);    shell_check_oom(zSql);    sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0);    sqlite3_free(zSql);  }  if( sqlite3_step(pStmt)==SQLITE_ROW ){    const char *z = (const char*)sqlite3_column_text(pStmt,0);    zRet = z ? strdup(z) : 0;  }else{    sqlite3_finalize(pStmt);    pStmt = 0;    zRet = 0;  }  return zRet;}static char **readline_completion(const char *zText, int iStart, int iEnd){  rl_attempted_completion_over = 1;  return rl_completion_matches(zText, readline_completion_generator);}#elif HAVE_LINENOISEstatic void linenoise_completion(const char *zLine, linenoiseCompletions *lc){  int nLine = strlen30(zLine);  int i, iStart;  sqlite3_stmt *pStmt = 0;  char *zSql;  char zBuf[1000];  if( nLine>sizeof(zBuf)-30 ) return;  if( zLine[0]=='.' || zLine[0]=='#') return;  for(i=nLine-1; i>=0 && (isalnum(zLine[i]) || zLine[i]=='_'); i--){}  if( i==nLine-1 ) return;  iStart = i+1;  memcpy(zBuf, zLine, iStart);  zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase"                         "  FROM completion(%Q,%Q) ORDER BY 1",                         &zLine[iStart], zLine);  shell_check_oom(zSql);  sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0);  sqlite3_free(zSql);  sqlite3_exec(globalDb, "PRAGMA page_count", 0, 0, 0);   while( sqlite3_step(pStmt)==SQLITE_ROW ){    const char *zCompletion = (const char*)sqlite3_column_text(pStmt, 0);    int nCompletion = sqlite3_column_bytes(pStmt, 0);    if( iStart+nCompletion < sizeof(zBuf)-1 && zCompletion ){      memcpy(zBuf+iStart, zCompletion, nCompletion+1);      linenoiseAddCompletion(lc, zBuf);    }  }  sqlite3_finalize(pStmt);}#endifstatic void resolve_backslashes(char *z){  int i, j;  char c;  while( *z && *z!='\\' ) z++;  for(i=j=0; (c = z[i])!=0; i++, j++){    if( c=='\\' && z[i+1]!=0 ){      c = z[++i];      if( c=='a' ){        c = '\a';      }else if( c=='b' ){        c = '\b';      }else if( c=='t' ){        c = '\t';      }else if( c=='n' ){        c = '\n';      }else if( c=='v' ){        c = '\v';      }else if( c=='f' ){        c = '\f';      }else if( c=='r' ){        c = '\r';      }else if( c=='"' ){        c = '"';      }else if( c=='\'' ){        c = '\'';      }else if( c=='\\' ){        c = '\\';      }else if( c>='0' && c<='7' ){        c -= '0';        if( z[i+1]>='0' && z[i+1]<='7' ){          i++;          c = (c<<3) + z[i] - '0';          if( z[i+1]>='0' && z[i+1]<='7' ){            i++;            c = (c<<3) + z[i] - '0';          }        }      }    }    z[j] = c;  }  if( j<i ) z[j] = 0;}static int booleanValue(const char *zArg){  int i;  if( zArg[0]=='0' && zArg[1]=='x' ){    for(i=2; hexDigitValue(zArg[i])>=0; i++){}  }else{    for(i=0; zArg[i]>='0' && zArg[i]<='9'; i++){}  }  if( i>0 && zArg[i]==0 ) return (int)(integerValue(zArg) & 0xffffffff);  if( sqlite3_stricmp(zArg, "on")==0 || sqlite3_stricmp(zArg,"yes")==0 ){    return 1;  }  if( sqlite3_stricmp(zArg, "off")==0 || sqlite3_stricmp(zArg,"no")==0 ){    return 0;  }  utf8_printf(stderr, "ERROR: Not a boolean value: \"%s\". Assuming \"no\".\n",          zArg);  return 0;}static void setOrClearFlag(ShellState *p, unsigned mFlag, const char *zArg){  if( booleanValue(zArg) ){    ShellSetFlag(p, mFlag);  }else{    ShellClearFlag(p, mFlag);  }}static void output_file_close(FILE *f){  if( f && f!=stdout && f!=stderr ) fclose(f);}static FILE *output_file_open(const char *zFile, int bTextMode){  FILE *f;  if( strcmp(zFile,"stdout")==0 ){    f = stdout;  }else if( strcmp(zFile, "stderr")==0 ){    f = stderr;  }else if( strcmp(zFile, "off")==0 ){    f = 0;  }else{    f = fopen(zFile, bTextMode ? "w" : "wb");    if( f==0 ){      utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile);    }  }  return f;}#ifndef SQLITE_OMIT_TRACEstatic int sql_trace_callback(  unsigned mType,           void *pArg,               void *pP,                 void *pX                ){  ShellState *p = (ShellState*)pArg;  sqlite3_stmt *pStmt;  const char *zSql;  int nSql;  if( p->traceOut==0 ) return 0;  if( mType==SQLITE_TRACE_CLOSE ){    utf8_printf(p->traceOut, "-- closing database connection\n");    return 0;  }  if( mType!=SQLITE_TRACE_ROW && ((const char*)pX)[0]=='-' ){    zSql = (const char*)pX;  }else{    pStmt = (sqlite3_stmt*)pP;    switch( p->eTraceType ){      case SHELL_TRACE_EXPANDED: {        zSql = sqlite3_expanded_sql(pStmt);        break;      }#ifdef SQLITE_ENABLE_NORMALIZE      case SHELL_TRACE_NORMALIZED: {        zSql = sqlite3_normalized_sql(pStmt);        break;      }#endif      default: {        zSql = sqlite3_sql(pStmt);        break;      }    }  }  if( zSql==0 ) return 0;  nSql = strlen30(zSql);  while( nSql>0 && zSql[nSql-1]==';' ){ nSql--; }  switch( mType ){    case SQLITE_TRACE_ROW:    case SQLITE_TRACE_STMT: {      utf8_printf(p->traceOut, "%.*s;\n", nSql, zSql);      break;    }    case SQLITE_TRACE_PROFILE: {      sqlite3_int64 nNanosec = *(sqlite3_int64*)pX;      utf8_printf(p->traceOut, "%.*s; -- %lld ns\n", nSql, zSql, nNanosec);      break;    }  }  return 0;}#endifstatic void test_breakpoint(void){  static int nCall = 0;  nCall++;}typedef struct ImportCtx ImportCtx;struct ImportCtx {  const char *zFile;    FILE *in;             int (SQLITE_CDECL *xCloser)(FILE*);        char *z;              int n;                int nAlloc;           int nLine;            int nRow;             int nErr;             int bNotFirst;        int cTerm;            int cColSep;          int cRowSep;        };static void import_cleanup(ImportCtx *p){  if( p->in!=0 && p->xCloser!=0 ){    p->xCloser(p->in);    p->in = 0;  }  sqlite3_free(p->z);  p->z = 0;}static void import_append_char(ImportCtx *p, int c){  if( p->n+1>=p->nAlloc ){    p->nAlloc += p->nAlloc + 100;    p->z = sqlite3_realloc64(p->z, p->nAlloc);    shell_check_oom(p->z);  }  p->z[p->n++] = (char)c;}static char *SQLITE_CDECL csv_read_one_field(ImportCtx *p){  int c;  int cSep = p->cColSep;  int rSep = p->cRowSep;  p->n = 0;  c = fgetc(p->in);  if( c==EOF || seenInterrupt ){    p->cTerm = EOF;    return 0;  }  if( c=='"' ){    int pc, ppc;    int startLine = p->nLine;    int cQuote = c;    pc = ppc = 0;    while( 1 ){      c = fgetc(p->in);      if( c==rSep ) p->nLine++;      if( c==cQuote ){        if( pc==cQuote ){          pc = 0;          continue;        }      }      if( (c==cSep && pc==cQuote)       || (c==rSep && pc==cQuote)       || (c==rSep && pc=='\r' && ppc==cQuote)       || (c==EOF && pc==cQuote)      ){        do{ p->n--; }while( p->z[p->n]!=cQuote );        p->cTerm = c;        break;      }      if( pc==cQuote && c!='\r' ){        utf8_printf(stderr, "%s:%d: unescaped %c character\n",                p->zFile, p->nLine, cQuote);      }      if( c==EOF ){        utf8_printf(stderr, "%s:%d: unterminated %c-quoted field\n",                p->zFile, startLine, cQuote);        p->cTerm = c;        break;      }      import_append_char(p, c);      ppc = pc;      pc = c;    }  }else{        if( (c&0xff)==0xef && p->bNotFirst==0 ){      import_append_char(p, c);      c = fgetc(p->in);      if( (c&0xff)==0xbb ){        import_append_char(p, c);        c = fgetc(p->in);        if( (c&0xff)==0xbf ){          p->bNotFirst = 1;          p->n = 0;          return csv_read_one_field(p);        }      }    }    while( c!=EOF && c!=cSep && c!=rSep ){      import_append_char(p, c);      c = fgetc(p->in);    }    if( c==rSep ){      p->nLine++;      if( p->n>0 && p->z[p->n-1]=='\r' ) p->n--;    }    p->cTerm = c;  }  if( p->z ) p->z[p->n] = 0;  p->bNotFirst = 1;  return p->z;}static char *SQLITE_CDECL ascii_read_one_field(ImportCtx *p){  int c;  int cSep = p->cColSep;  int rSep = p->cRowSep;  p->n = 0;  c = fgetc(p->in);  if( c==EOF || seenInterrupt ){    p->cTerm = EOF;    return 0;  }  while( c!=EOF && c!=cSep && c!=rSep ){    import_append_char(p, c);    c = fgetc(p->in);  }  if( c==rSep ){    p->nLine++;  }  p->cTerm = c;  if( p->z ) p->z[p->n] = 0;  return p->z;}static void tryToCloneData(  ShellState *p,  sqlite3 *newDb,  const char *zTable){  sqlite3_stmt *pQuery = 0;  sqlite3_stmt *pInsert = 0;  char *zQuery = 0;  char *zInsert = 0;  int rc;  int i, j, n;  int nTable = strlen30(zTable);  int k = 0;  int cnt = 0;  const int spinRate = 10000;  zQuery = sqlite3_mprintf("SELECT * FROM \"%w\"", zTable);  shell_check_oom(zQuery);  rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);  if( rc ){    utf8_printf(stderr, "Error %d: %s on [%s]\n",            sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),            zQuery);    goto end_data_xfer;  }  n = sqlite3_column_count(pQuery);  zInsert = sqlite3_malloc64(200 + nTable + n*3);  shell_check_oom(zInsert);  sqlite3_snprintf(200+nTable,zInsert,                   "INSERT OR IGNORE INTO \"%s\" VALUES(?", zTable);  i = strlen30(zInsert);  for(j=1; j<n; j++){    memcpy(zInsert+i, ",?", 2);    i += 2;  }  memcpy(zInsert+i, ");", 3);  rc = sqlite3_prepare_v2(newDb, zInsert, -1, &pInsert, 0);  if( rc ){    utf8_printf(stderr, "Error %d: %s on [%s]\n",            sqlite3_extended_errcode(newDb), sqlite3_errmsg(newDb),            zQuery);    goto end_data_xfer;  }  for(k=0; k<2; k++){    while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){      for(i=0; i<n; i++){        switch( sqlite3_column_type(pQuery, i) ){          case SQLITE_NULL: {            sqlite3_bind_null(pInsert, i+1);            break;          }          case SQLITE_INTEGER: {            sqlite3_bind_int64(pInsert, i+1, sqlite3_column_int64(pQuery,i));            break;          }          case SQLITE_FLOAT: {            sqlite3_bind_double(pInsert, i+1, sqlite3_column_double(pQuery,i));            break;          }          case SQLITE_TEXT: {            sqlite3_bind_text(pInsert, i+1,                             (const char*)sqlite3_column_text(pQuery,i),                             -1, SQLITE_STATIC);            break;          }          case SQLITE_BLOB: {            sqlite3_bind_blob(pInsert, i+1, sqlite3_column_blob(pQuery,i),                                            sqlite3_column_bytes(pQuery,i),                                            SQLITE_STATIC);            break;          }        }      }       rc = sqlite3_step(pInsert);      if( rc!=SQLITE_OK && rc!=SQLITE_ROW && rc!=SQLITE_DONE ){        utf8_printf(stderr, "Error %d: %s\n", sqlite3_extended_errcode(newDb),                        sqlite3_errmsg(newDb));      }      sqlite3_reset(pInsert);      cnt++;      if( (cnt%spinRate)==0 ){        printf("%c\b", "|/-\\"[(cnt/spinRate)%4]);        fflush(stdout);      }    }     if( rc==SQLITE_DONE ) break;    sqlite3_finalize(pQuery);    sqlite3_free(zQuery);    zQuery = sqlite3_mprintf("SELECT * FROM \"%w\" ORDER BY rowid DESC;",                             zTable);    shell_check_oom(zQuery);    rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);    if( rc ){      utf8_printf(stderr, "Warning: cannot step \"%s\" backwards", zTable);      break;    }  } end_data_xfer:  sqlite3_finalize(pQuery);  sqlite3_finalize(pInsert);  sqlite3_free(zQuery);  sqlite3_free(zInsert);}static void tryToCloneSchema(  ShellState *p,  sqlite3 *newDb,  const char *zWhere,  void (*xForEach)(ShellState*,sqlite3*,const char*)){  sqlite3_stmt *pQuery = 0;  char *zQuery = 0;  int rc;  const unsigned char *zName;  const unsigned char *zSql;  char *zErrMsg = 0;  zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_schema"                           " WHERE %s", zWhere);  shell_check_oom(zQuery);  rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);  if( rc ){    utf8_printf(stderr, "Error: (%d) %s on [%s]\n",                    sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),                    zQuery);    goto end_schema_xfer;  }  while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){    zName = sqlite3_column_text(pQuery, 0);    zSql = sqlite3_column_text(pQuery, 1);    if( zName==0 || zSql==0 ) continue;    printf("%s... ", zName); fflush(stdout);    sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);    if( zErrMsg ){      utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql);      sqlite3_free(zErrMsg);      zErrMsg = 0;    }    if( xForEach ){      xForEach(p, newDb, (const char*)zName);    }    printf("done\n");  }  if( rc!=SQLITE_DONE ){    sqlite3_finalize(pQuery);    sqlite3_free(zQuery);    zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_schema"                             " WHERE %s ORDER BY rowid DESC", zWhere);    shell_check_oom(zQuery);    rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);    if( rc ){      utf8_printf(stderr, "Error: (%d) %s on [%s]\n",                      sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),                      zQuery);      goto end_schema_xfer;    }    while( sqlite3_step(pQuery)==SQLITE_ROW ){      zName = sqlite3_column_text(pQuery, 0);      zSql = sqlite3_column_text(pQuery, 1);      if( zName==0 || zSql==0 ) continue;      printf("%s... ", zName); fflush(stdout);      sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);      if( zErrMsg ){        utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql);        sqlite3_free(zErrMsg);        zErrMsg = 0;      }      if( xForEach ){        xForEach(p, newDb, (const char*)zName);      }      printf("done\n");    }  }end_schema_xfer:  sqlite3_finalize(pQuery);  sqlite3_free(zQuery);}static void tryToClone(ShellState *p, const char *zNewDb){  int rc;  sqlite3 *newDb = 0;  if( access(zNewDb,0)==0 ){    utf8_printf(stderr, "File \"%s\" already exists.\n", zNewDb);    return;  }  rc = sqlite3_open(zNewDb, &newDb);  if( rc ){    utf8_printf(stderr, "Cannot create output database: %s\n",            sqlite3_errmsg(newDb));  }else{    sqlite3_exec(p->db, "PRAGMA writable_schema=ON;", 0, 0, 0);    sqlite3_exec(newDb, "BEGIN EXCLUSIVE;", 0, 0, 0);    tryToCloneSchema(p, newDb, "type='table'", tryToCloneData);    tryToCloneSchema(p, newDb, "type!='table'", 0);    sqlite3_exec(newDb, "COMMIT;", 0, 0, 0);    sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0);  }  close_db(newDb);}static void output_reset(ShellState *p){  if( p->outfile[0]=='|' ){#ifndef SQLITE_OMIT_POPEN    pclose(p->out);#endif  }else{    output_file_close(p->out);#ifndef SQLITE_NOHAVE_SYSTEM    if( p->doXdgOpen ){      const char *zXdgOpenCmd =#if defined(_WIN32)      "start";#elif defined(__APPLE__)      "open";#else      "xdg-open";#endif      char *zCmd;      zCmd = sqlite3_mprintf("%s %s", zXdgOpenCmd, p->zTempFile);      if( system(zCmd) ){        utf8_printf(stderr, "Failed: [%s]\n", zCmd);      }else{                sqlite3_sleep(2000);      }      sqlite3_free(zCmd);      outputModePop(p);      p->doXdgOpen = 0;    }#endif   }  p->outfile[0] = 0;  p->out = stdout;}static int db_int(sqlite3 *db, const char *zSql){  sqlite3_stmt *pStmt;  int res = 0;  sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);  if( pStmt && sqlite3_step(pStmt)==SQLITE_ROW ){    res = sqlite3_column_int(pStmt,0);  }  sqlite3_finalize(pStmt);  return res;}static unsigned int get2byteInt(unsigned char *a){  return (a[0]<<8) + a[1];}static unsigned int get4byteInt(unsigned char *a){  return (a[0]<<24) + (a[1]<<16) + (a[2]<<8) + a[3];}static int shell_dbinfo_command(ShellState *p, int nArg, char **azArg){  static const struct { const char *zName; int ofst; } aField[] = {     { "file change counter:",  24  },     { "database page count:",  28  },     { "freelist page count:",  36  },     { "schema cookie:",        40  },     { "schema format:",        44  },     { "default cache size:",   48  },     { "autovacuum top root:",  52  },     { "incremental vacuum:",   64  },     { "text encoding:",        56  },     { "user version:",         60  },     { "application id:",       68  },     { "software version:",     96  },  };  static const struct { const char *zName; const char *zSql; } aQuery[] = {     { "number of tables:",       "SELECT count(*) FROM %s WHERE type='table'" },     { "number of indexes:",       "SELECT count(*) FROM %s WHERE type='index'" },     { "number of triggers:",       "SELECT count(*) FROM %s WHERE type='trigger'" },     { "number of views:",       "SELECT count(*) FROM %s WHERE type='view'" },     { "schema size:",       "SELECT total(length(sql)) FROM %s" },  };  int i, rc;  unsigned iDataVersion;  char *zSchemaTab;  char *zDb = nArg>=2 ? azArg[1] : "main";  sqlite3_stmt *pStmt = 0;  unsigned char aHdr[100];  open_db(p, 0);  if( p->db==0 ) return 1;  rc = sqlite3_prepare_v2(p->db,             "SELECT data FROM sqlite_dbpage(?1) WHERE pgno=1",             -1, &pStmt, 0);  if( rc ){    utf8_printf(stderr, "error: %s\n", sqlite3_errmsg(p->db));    sqlite3_finalize(pStmt);    return 1;  }  sqlite3_bind_text(pStmt, 1, zDb, -1, SQLITE_STATIC);  if( sqlite3_step(pStmt)==SQLITE_ROW   && sqlite3_column_bytes(pStmt,0)>100  ){    memcpy(aHdr, sqlite3_column_blob(pStmt,0), 100);    sqlite3_finalize(pStmt);  }else{    raw_printf(stderr, "unable to read database header\n");    sqlite3_finalize(pStmt);    return 1;  }  i = get2byteInt(aHdr+16);  if( i==1 ) i = 65536;  utf8_printf(p->out, "%-20s %d\n", "database page size:", i);  utf8_printf(p->out, "%-20s %d\n", "write format:", aHdr[18]);  utf8_printf(p->out, "%-20s %d\n", "read format:", aHdr[19]);  utf8_printf(p->out, "%-20s %d\n", "reserved bytes:", aHdr[20]);  for(i=0; i<ArraySize(aField); i++){    int ofst = aField[i].ofst;    unsigned int val = get4byteInt(aHdr + ofst);    utf8_printf(p->out, "%-20s %u", aField[i].zName, val);    switch( ofst ){      case 56: {        if( val==1 ) raw_printf(p->out, " (utf8)");        if( val==2 ) raw_printf(p->out, " (utf16le)");        if( val==3 ) raw_printf(p->out, " (utf16be)");      }    }    raw_printf(p->out, "\n");  }  if( zDb==0 ){    zSchemaTab = sqlite3_mprintf("main.sqlite_schema");  }else if( strcmp(zDb,"temp")==0 ){    zSchemaTab = sqlite3_mprintf("%s", "sqlite_temp_schema");  }else{    zSchemaTab = sqlite3_mprintf("\"%w\".sqlite_schema", zDb);  }  for(i=0; i<ArraySize(aQuery); i++){    char *zSql = sqlite3_mprintf(aQuery[i].zSql, zSchemaTab);    int val = db_int(p->db, zSql);    sqlite3_free(zSql);    utf8_printf(p->out, "%-20s %d\n", aQuery[i].zName, val);  }  sqlite3_free(zSchemaTab);  sqlite3_file_control(p->db, zDb, SQLITE_FCNTL_DATA_VERSION, &iDataVersion);  utf8_printf(p->out, "%-20s %u\n", "data version", iDataVersion);  return 0;}static int shellDatabaseError(sqlite3 *db){  const char *zErr = sqlite3_errmsg(db);  utf8_printf(stderr, "Error: %s\n", zErr);  return 1;}static int testcase_glob(const char *zGlob, const char *z){  int c, c2;  int invert;  int seen;  while( (c = (*(zGlob++)))!=0 ){    if( IsSpace(c) ){      if( !IsSpace(*z) ) return 0;      while( IsSpace(*zGlob) ) zGlob++;      while( IsSpace(*z) ) z++;    }else if( c=='*' ){      while( (c=(*(zGlob++))) == '*' || c=='?' ){        if( c=='?' && (*(z++))==0 ) return 0;      }      if( c==0 ){        return 1;      }else if( c=='[' ){        while( *z && testcase_glob(zGlob-1,z)==0 ){          z++;        }        return (*z)!=0;      }      while( (c2 = (*(z++)))!=0 ){        while( c2!=c ){          c2 = *(z++);          if( c2==0 ) return 0;        }        if( testcase_glob(zGlob,z) ) return 1;      }      return 0;    }else if( c=='?' ){      if( (*(z++))==0 ) return 0;    }else if( c=='[' ){      int prior_c = 0;      seen = 0;      invert = 0;      c = *(z++);      if( c==0 ) return 0;      c2 = *(zGlob++);      if( c2=='^' ){        invert = 1;        c2 = *(zGlob++);      }      if( c2==']' ){        if( c==']' ) seen = 1;        c2 = *(zGlob++);      }      while( c2 && c2!=']' ){        if( c2=='-' && zGlob[0]!=']' && zGlob[0]!=0 && prior_c>0 ){          c2 = *(zGlob++);          if( c>=prior_c && c<=c2 ) seen = 1;          prior_c = 0;        }else{          if( c==c2 ){            seen = 1;          }          prior_c = c2;        }        c2 = *(zGlob++);      }      if( c2==0 || (seen ^ invert)==0 ) return 0;    }else if( c=='#' ){      if( (z[0]=='-' || z[0]=='+') && IsDigit(z[1]) ) z++;      if( !IsDigit(z[0]) ) return 0;      z++;      while( IsDigit(z[0]) ){ z++; }    }else{      if( c!=(*(z++)) ) return 0;    }  }  while( IsSpace(*z) ){ z++; }  return *z==0;}static int optionMatch(const char *zStr, const char *zOpt){  if( zStr[0]!='-' ) return 0;  zStr++;  if( zStr[0]=='-' ) zStr++;  return strcmp(zStr, zOpt)==0;}int shellDeleteFile(const char *zFilename){  int rc;#ifdef _WIN32  wchar_t *z = sqlite3_win32_utf8_to_unicode(zFilename);  rc = _wunlink(z);  sqlite3_free(z);#else  rc = unlink(zFilename);#endif  return rc;}static void clearTempFile(ShellState *p){  if( p->zTempFile==0 ) return;  if( p->doXdgOpen ) return;  if( shellDeleteFile(p->zTempFile) ) return;  sqlite3_free(p->zTempFile);  p->zTempFile = 0;}static void newTempFile(ShellState *p, const char *zSuffix){  clearTempFile(p);  sqlite3_free(p->zTempFile);  p->zTempFile = 0;  if( p->db ){    sqlite3_file_control(p->db, 0, SQLITE_FCNTL_TEMPFILENAME, &p->zTempFile);  }  if( p->zTempFile==0 ){        char *zTemp;    sqlite3_uint64 r;    sqlite3_randomness(sizeof(r), &r);    zTemp = getenv("TEMP");    if( zTemp==0 ) zTemp = getenv("TMP");    if( zTemp==0 ){#ifdef _WIN32      zTemp = "\\tmp";#else      zTemp = "/tmp";#endif    }    p->zTempFile = sqlite3_mprintf("%s/temp%llx.%s", zTemp, r, zSuffix);  }else{    p->zTempFile = sqlite3_mprintf("%z.%s", p->zTempFile, zSuffix);  }  shell_check_oom(p->zTempFile);}static void shellFkeyCollateClause(  sqlite3_context *pCtx,  int nVal,  sqlite3_value **apVal){  sqlite3 *db = sqlite3_context_db_handle(pCtx);  const char *zParent;  const char *zParentCol;  const char *zParentSeq;  const char *zChild;  const char *zChildCol;  const char *zChildSeq = 0;    int rc;  assert( nVal==4 );  zParent = (const char*)sqlite3_value_text(apVal[0]);  zParentCol = (const char*)sqlite3_value_text(apVal[1]);  zChild = (const char*)sqlite3_value_text(apVal[2]);  zChildCol = (const char*)sqlite3_value_text(apVal[3]);  sqlite3_result_text(pCtx, "", -1, SQLITE_STATIC);  rc = sqlite3_table_column_metadata(      db, "main", zParent, zParentCol, 0, &zParentSeq, 0, 0, 0  );  if( rc==SQLITE_OK ){    rc = sqlite3_table_column_metadata(        db, "main", zChild, zChildCol, 0, &zChildSeq, 0, 0, 0    );  }  if( rc==SQLITE_OK && sqlite3_stricmp(zParentSeq, zChildSeq) ){    char *z = sqlite3_mprintf(" COLLATE %s", zParentSeq);    sqlite3_result_text(pCtx, z, -1, SQLITE_TRANSIENT);    sqlite3_free(z);  }}static int lintFkeyIndexes(  ShellState *pState,               char **azArg,                     int nArg                        ){  sqlite3 *db = pState->db;         FILE *out = pState->out;          int bVerbose = 0;                 int bGroupByParent = 0;           int i;                            const char *zIndent = "";         int rc;                           sqlite3_stmt *pSql = 0;             const char *zSql =  "SELECT "    "     'EXPLAIN QUERY PLAN SELECT 1 FROM ' || quote(s.name) || ' WHERE '"    "  || group_concat(quote(s.name) || '.' || quote(f.[from]) || '=?' "    "  || fkey_collate_clause("    "       f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]),' AND ')"    ", "    "     'SEARCH ' || s.name || ' USING COVERING INDEX*('"    "  || group_concat('*=?', ' AND ') || ')'"    ", "    "     s.name  || '(' || group_concat(f.[from],  ', ') || ')'"    ", "    "     f.[table] || '(' || group_concat(COALESCE(f.[to], p.[name])) || ')'"    ", "    "     'CREATE INDEX ' || quote(s.name ||'_'|| group_concat(f.[from], '_'))"    "  || ' ON ' || quote(s.name) || '('"    "  || group_concat(quote(f.[from]) ||"    "        fkey_collate_clause("    "          f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]), ', ')"    "  || ');'"    ", "    "     f.[table] "    "FROM sqlite_schema AS s, pragma_foreign_key_list(s.name) AS f "    "LEFT JOIN pragma_table_info AS p ON (pk-1=seq AND p.arg=f.[table]) "    "GROUP BY s.name, f.id "    "ORDER BY (CASE WHEN ? THEN f.[table] ELSE s.name END)"  ;  const char *zGlobIPK = "SEARCH * USING INTEGER PRIMARY KEY (rowid=?)";  for(i=2; i<nArg; i++){    int n = strlen30(azArg[i]);    if( n>1 && sqlite3_strnicmp("-verbose", azArg[i], n)==0 ){      bVerbose = 1;    }    else if( n>1 && sqlite3_strnicmp("-groupbyparent", azArg[i], n)==0 ){      bGroupByParent = 1;      zIndent = "    ";    }    else{      raw_printf(stderr, "Usage: %s %s ?-verbose? ?-groupbyparent?\n",          azArg[0], azArg[1]      );      return SQLITE_ERROR;    }  }    rc = sqlite3_create_function(db, "fkey_collate_clause", 4, SQLITE_UTF8,      0, shellFkeyCollateClause, 0, 0  );  if( rc==SQLITE_OK ){    rc = sqlite3_prepare_v2(db, zSql, -1, &pSql, 0);  }  if( rc==SQLITE_OK ){    sqlite3_bind_int(pSql, 1, bGroupByParent);  }  if( rc==SQLITE_OK ){    int rc2;    char *zPrev = 0;    while( SQLITE_ROW==sqlite3_step(pSql) ){      int res = -1;      sqlite3_stmt *pExplain = 0;      const char *zEQP = (const char*)sqlite3_column_text(pSql, 0);      const char *zGlob = (const char*)sqlite3_column_text(pSql, 1);      const char *zFrom = (const char*)sqlite3_column_text(pSql, 2);      const char *zTarget = (const char*)sqlite3_column_text(pSql, 3);      const char *zCI = (const char*)sqlite3_column_text(pSql, 4);      const char *zParent = (const char*)sqlite3_column_text(pSql, 5);      if( zEQP==0 ) continue;      if( zGlob==0 ) continue;      rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);      if( rc!=SQLITE_OK ) break;      if( SQLITE_ROW==sqlite3_step(pExplain) ){        const char *zPlan = (const char*)sqlite3_column_text(pExplain, 3);        res = zPlan!=0 && (  0==sqlite3_strglob(zGlob, zPlan)                          || 0==sqlite3_strglob(zGlobIPK, zPlan));      }      rc = sqlite3_finalize(pExplain);      if( rc!=SQLITE_OK ) break;      if( res<0 ){        raw_printf(stderr, "Error: internal error");        break;      }else{        if( bGroupByParent        && (bVerbose || res==0)        && (zPrev==0 || sqlite3_stricmp(zParent, zPrev))        ){          raw_printf(out, "-- Parent table %s\n", zParent);          sqlite3_free(zPrev);          zPrev = sqlite3_mprintf("%s", zParent);        }        if( res==0 ){          raw_printf(out, "%s%s --> %s\n", zIndent, zCI, zTarget);        }else if( bVerbose ){          raw_printf(out, "%s/* no extra indexes required for %s -> %s */\n",              zIndent, zFrom, zTarget          );        }      }    }    sqlite3_free(zPrev);    if( rc!=SQLITE_OK ){      raw_printf(stderr, "%s\n", sqlite3_errmsg(db));    }    rc2 = sqlite3_finalize(pSql);    if( rc==SQLITE_OK && rc2!=SQLITE_OK ){      rc = rc2;      raw_printf(stderr, "%s\n", sqlite3_errmsg(db));    }  }else{    raw_printf(stderr, "%s\n", sqlite3_errmsg(db));  }  return rc;}static int lintDotCommand(  ShellState *pState,               char **azArg,                     int nArg                        ){  int n;  n = (nArg>=2 ? strlen30(azArg[1]) : 0);  if( n<1 || sqlite3_strnicmp(azArg[1], "fkey-indexes", n) ) goto usage;  return lintFkeyIndexes(pState, azArg, nArg); usage:  raw_printf(stderr, "Usage %s sub-command ?switches...?\n", azArg[0]);  raw_printf(stderr, "Where sub-commands are:\n");  raw_printf(stderr, "    fkey-indexes\n");  return SQLITE_ERROR;}#if !defined SQLITE_OMIT_VIRTUALTABLEstatic void shellPrepare(  sqlite3 *db,   int *pRc,   const char *zSql,   sqlite3_stmt **ppStmt){  *ppStmt = 0;  if( *pRc==SQLITE_OK ){    int rc = sqlite3_prepare_v2(db, zSql, -1, ppStmt, 0);    if( rc!=SQLITE_OK ){      raw_printf(stderr, "sql error: %s (%d)\n",           sqlite3_errmsg(db), sqlite3_errcode(db)      );      *pRc = rc;    }  }}void shellPreparePrintf(  sqlite3 *db,   int *pRc,   sqlite3_stmt **ppStmt,  const char *zFmt,   ...){  *ppStmt = 0;  if( *pRc==SQLITE_OK ){    va_list ap;    char *z;    va_start(ap, zFmt);    z = sqlite3_vmprintf(zFmt, ap);    va_end(ap);    if( z==0 ){      *pRc = SQLITE_NOMEM;    }else{      shellPrepare(db, pRc, z, ppStmt);      sqlite3_free(z);    }  }}void shellFinalize(  int *pRc,   sqlite3_stmt *pStmt){  if( pStmt ){    sqlite3 *db = sqlite3_db_handle(pStmt);    int rc = sqlite3_finalize(pStmt);    if( *pRc==SQLITE_OK ){      if( rc!=SQLITE_OK ){        raw_printf(stderr, "SQL error: %s\n", sqlite3_errmsg(db));      }      *pRc = rc;    }  }}void shellReset(  int *pRc,   sqlite3_stmt *pStmt){  int rc = sqlite3_reset(pStmt);  if( *pRc==SQLITE_OK ){    if( rc!=SQLITE_OK ){      sqlite3 *db = sqlite3_db_handle(pStmt);      raw_printf(stderr, "SQL error: %s\n", sqlite3_errmsg(db));    }    *pRc = rc;  }}#endif #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)typedef struct ArCommand ArCommand;struct ArCommand {  u8 eCmd;                          u8 bVerbose;                      u8 bZip;                          u8 bDryRun;                       u8 bAppend;                       u8 bGlob;                         u8 fromCmdLine;                   int nArg;                         char *zSrcTable;                  const char *zFile;                const char *zDir;                 char **azArg;                     ShellState *p;                    sqlite3 *db;                    };static int arUsage(FILE *f){  showHelp(f,"archive");  return SQLITE_ERROR;}static int arErrorMsg(ArCommand *pAr, const char *zFmt, ...){  va_list ap;  char *z;  va_start(ap, zFmt);  z = sqlite3_vmprintf(zFmt, ap);  va_end(ap);  utf8_printf(stderr, "Error: %s\n", z);  if( pAr->fromCmdLine ){    utf8_printf(stderr, "Use \"-A\" for more help\n");  }else{    utf8_printf(stderr, "Use \".archive --help\" for more help\n");  }  sqlite3_free(z);  return SQLITE_ERROR;}#define AR_CMD_CREATE       1#define AR_CMD_UPDATE       2#define AR_CMD_INSERT       3#define AR_CMD_EXTRACT      4#define AR_CMD_LIST         5#define AR_CMD_HELP         6#define AR_CMD_REMOVE       7#define AR_SWITCH_VERBOSE     8#define AR_SWITCH_FILE        9#define AR_SWITCH_DIRECTORY  10#define AR_SWITCH_APPEND     11#define AR_SWITCH_DRYRUN     12#define AR_SWITCH_GLOB       13static int arProcessSwitch(ArCommand *pAr, int eSwitch, const char *zArg){  switch( eSwitch ){    case AR_CMD_CREATE:    case AR_CMD_EXTRACT:    case AR_CMD_LIST:    case AR_CMD_REMOVE:    case AR_CMD_UPDATE:    case AR_CMD_INSERT:    case AR_CMD_HELP:      if( pAr->eCmd ){        return arErrorMsg(pAr, "multiple command options");      }      pAr->eCmd = eSwitch;      break;    case AR_SWITCH_DRYRUN:      pAr->bDryRun = 1;      break;    case AR_SWITCH_GLOB:      pAr->bGlob = 1;      break;    case AR_SWITCH_VERBOSE:      pAr->bVerbose = 1;      break;    case AR_SWITCH_APPEND:      pAr->bAppend = 1;          case AR_SWITCH_FILE:      pAr->zFile = zArg;      break;    case AR_SWITCH_DIRECTORY:      pAr->zDir = zArg;      break;  }  return SQLITE_OK;}static int arParseCommand(  char **azArg,                     int nArg,                         ArCommand *pAr                  ){  struct ArSwitch {    const char *zLong;    char cShort;    u8 eSwitch;    u8 bArg;  } aSwitch[] = {    { "create",    'c', AR_CMD_CREATE,       0 },    { "extract",   'x', AR_CMD_EXTRACT,      0 },    { "insert",    'i', AR_CMD_INSERT,       0 },    { "list",      't', AR_CMD_LIST,         0 },    { "remove",    'r', AR_CMD_REMOVE,       0 },    { "update",    'u', AR_CMD_UPDATE,       0 },    { "help",      'h', AR_CMD_HELP,         0 },    { "verbose",   'v', AR_SWITCH_VERBOSE,   0 },    { "file",      'f', AR_SWITCH_FILE,      1 },    { "append",    'a', AR_SWITCH_APPEND,    1 },    { "directory", 'C', AR_SWITCH_DIRECTORY, 1 },    { "dryrun",    'n', AR_SWITCH_DRYRUN,    0 },    { "glob",      'g', AR_SWITCH_GLOB,      0 },  };  int nSwitch = sizeof(aSwitch) / sizeof(struct ArSwitch);  struct ArSwitch *pEnd = &aSwitch[nSwitch];  if( nArg<=1 ){    utf8_printf(stderr, "Wrong number of arguments.  Usage:\n");    return arUsage(stderr);  }else{    char *z = azArg[1];    if( z[0]!='-' ){            int i;      int iArg = 2;      for(i=0; z[i]; i++){        const char *zArg = 0;        struct ArSwitch *pOpt;        for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){          if( z[i]==pOpt->cShort ) break;        }        if( pOpt==pEnd ){          return arErrorMsg(pAr, "unrecognized option: %c", z[i]);        }        if( pOpt->bArg ){          if( iArg>=nArg ){            return arErrorMsg(pAr, "option requires an argument: %c",z[i]);          }          zArg = azArg[iArg++];        }        if( arProcessSwitch(pAr, pOpt->eSwitch, zArg) ) return SQLITE_ERROR;      }      pAr->nArg = nArg-iArg;      if( pAr->nArg>0 ){        pAr->azArg = &azArg[iArg];      }    }else{            int iArg;      for(iArg=1; iArg<nArg; iArg++){        int n;        z = azArg[iArg];        if( z[0]!='-' ){                    pAr->azArg = &azArg[iArg];          pAr->nArg = nArg-iArg;          break;        }        n = strlen30(z);        if( z[1]!='-' ){          int i;                    for(i=1; i<n; i++){            const char *zArg = 0;            struct ArSwitch *pOpt;            for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){              if( z[i]==pOpt->cShort ) break;            }            if( pOpt==pEnd ){              return arErrorMsg(pAr, "unrecognized option: %c", z[i]);            }            if( pOpt->bArg ){              if( i<(n-1) ){                zArg = &z[i+1];                i = n;              }else{                if( iArg>=(nArg-1) ){                  return arErrorMsg(pAr, "option requires an argument: %c",                                    z[i]);                }                zArg = azArg[++iArg];              }            }            if( arProcessSwitch(pAr, pOpt->eSwitch, zArg) ) return SQLITE_ERROR;          }        }else if( z[2]=='\0' ){                    pAr->azArg = &azArg[iArg+1];          pAr->nArg = nArg-iArg-1;          break;        }else{                    const char *zArg = 0;                       struct ArSwitch *pMatch = 0;                struct ArSwitch *pOpt;                      for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){            const char *zLong = pOpt->zLong;            if( (n-2)<=strlen30(zLong) && 0==memcmp(&z[2], zLong, n-2) ){              if( pMatch ){                return arErrorMsg(pAr, "ambiguous option: %s",z);              }else{                pMatch = pOpt;              }            }          }          if( pMatch==0 ){            return arErrorMsg(pAr, "unrecognized option: %s", z);          }          if( pMatch->bArg ){            if( iArg>=(nArg-1) ){              return arErrorMsg(pAr, "option requires an argument: %s", z);            }            zArg = azArg[++iArg];          }          if( arProcessSwitch(pAr, pMatch->eSwitch, zArg) ) return SQLITE_ERROR;        }      }    }  }  return SQLITE_OK;}static int arCheckEntries(ArCommand *pAr){  int rc = SQLITE_OK;  if( pAr->nArg ){    int i, j;    sqlite3_stmt *pTest = 0;    const char *zSel = (pAr->bGlob)      ? "SELECT name FROM %s WHERE glob($name,name)"      : "SELECT name FROM %s WHERE name=$name";    shellPreparePrintf(pAr->db, &rc, &pTest, zSel, pAr->zSrcTable);    j = sqlite3_bind_parameter_index(pTest, "$name");    for(i=0; i<pAr->nArg && rc==SQLITE_OK; i++){      char *z = pAr->azArg[i];      int n = strlen30(z);      int bOk = 0;      while( n>0 && z[n-1]=='/' ) n--;      z[n] = '\0';      sqlite3_bind_text(pTest, j, z, -1, SQLITE_STATIC);      if( SQLITE_ROW==sqlite3_step(pTest) ){        bOk = 1;      }      shellReset(&rc, pTest);      if( rc==SQLITE_OK && bOk==0 ){        utf8_printf(stderr, "not found in archive: %s\n", z);        rc = SQLITE_ERROR;      }    }    shellFinalize(&rc, pTest);  }  return rc;}static void arWhereClause(  int *pRc,   ArCommand *pAr,  char **pzWhere                  ){  char *zWhere = 0;  const char *zSameOp = (pAr->bGlob)? "GLOB" : "=";  if( *pRc==SQLITE_OK ){    if( pAr->nArg==0 ){      zWhere = sqlite3_mprintf("1");    }else{      int i;      const char *zSep = "";      for(i=0; i<pAr->nArg; i++){        const char *z = pAr->azArg[i];        zWhere = sqlite3_mprintf(          "%z%s name %s '%q' OR substr(name,1,%d) %s '%q/'",           zWhere, zSep, zSameOp, z, strlen30(z)+1, zSameOp, z        );        if( zWhere==0 ){          *pRc = SQLITE_NOMEM;          break;        }        zSep = " OR ";      }    }  }  *pzWhere = zWhere;}static int arListCommand(ArCommand *pAr){  const char *zSql = "SELECT %s FROM %s WHERE %s";   const char *azCols[] = {    "name",    "lsmode(mode), sz, datetime(mtime, 'unixepoch'), name"  };  char *zWhere = 0;  sqlite3_stmt *pSql = 0;  int rc;  rc = arCheckEntries(pAr);  arWhereClause(&rc, pAr, &zWhere);  shellPreparePrintf(pAr->db, &rc, &pSql, zSql, azCols[pAr->bVerbose],                     pAr->zSrcTable, zWhere);  if( pAr->bDryRun ){    utf8_printf(pAr->p->out, "%s\n", sqlite3_sql(pSql));  }else{    while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){      if( pAr->bVerbose ){        utf8_printf(pAr->p->out, "%s % 10d  %s  %s\n",            sqlite3_column_text(pSql, 0),            sqlite3_column_int(pSql, 1),             sqlite3_column_text(pSql, 2),            sqlite3_column_text(pSql, 3)        );      }else{        utf8_printf(pAr->p->out, "%s\n", sqlite3_column_text(pSql, 0));      }    }  }  shellFinalize(&rc, pSql);  sqlite3_free(zWhere);  return rc;}static int arRemoveCommand(ArCommand *pAr){  int rc = 0;  char *zSql = 0;  char *zWhere = 0;  if( pAr->nArg ){        rc = arCheckEntries(pAr);    arWhereClause(&rc, pAr, &zWhere);  }  if( rc==SQLITE_OK ){    zSql = sqlite3_mprintf("DELETE FROM %s WHERE %s;",                           pAr->zSrcTable, zWhere);    if( pAr->bDryRun ){      utf8_printf(pAr->p->out, "%s\n", zSql);    }else{      char *zErr = 0;      rc = sqlite3_exec(pAr->db, "SAVEPOINT ar;", 0, 0, 0);      if( rc==SQLITE_OK ){        rc = sqlite3_exec(pAr->db, zSql, 0, 0, &zErr);        if( rc!=SQLITE_OK ){          sqlite3_exec(pAr->db, "ROLLBACK TO ar; RELEASE ar;", 0, 0, 0);        }else{          rc = sqlite3_exec(pAr->db, "RELEASE ar;", 0, 0, 0);        }      }      if( zErr ){        utf8_printf(stdout, "ERROR: %s\n", zErr);        sqlite3_free(zErr);      }    }  }  sqlite3_free(zWhere);  sqlite3_free(zSql);  return rc;}static int arExtractCommand(ArCommand *pAr){  const char *zSql1 =     "SELECT "    " ($dir || name),"    " writefile(($dir || name), %s, mode, mtime) "    "FROM %s WHERE (%s) AND (data IS NULL OR $dirOnly = 0)"    " AND name NOT GLOB '*..[/\\]*'";  const char *azExtraArg[] = {     "sqlar_uncompress(data, sz)",    "data"  };  sqlite3_stmt *pSql = 0;  int rc = SQLITE_OK;  char *zDir = 0;  char *zWhere = 0;  int i, j;    rc = arCheckEntries(pAr);  arWhereClause(&rc, pAr, &zWhere);  if( rc==SQLITE_OK ){    if( pAr->zDir ){      zDir = sqlite3_mprintf("%s/", pAr->zDir);    }else{      zDir = sqlite3_mprintf("");    }    if( zDir==0 ) rc = SQLITE_NOMEM;  }  shellPreparePrintf(pAr->db, &rc, &pSql, zSql1,       azExtraArg[pAr->bZip], pAr->zSrcTable, zWhere  );  if( rc==SQLITE_OK ){    j = sqlite3_bind_parameter_index(pSql, "$dir");    sqlite3_bind_text(pSql, j, zDir, -1, SQLITE_STATIC);        for(i=0; i<2; i++){      j = sqlite3_bind_parameter_index(pSql, "$dirOnly");      sqlite3_bind_int(pSql, j, i);      if( pAr->bDryRun ){        utf8_printf(pAr->p->out, "%s\n", sqlite3_sql(pSql));      }else{        while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){          if( i==0 && pAr->bVerbose ){            utf8_printf(pAr->p->out, "%s\n", sqlite3_column_text(pSql, 0));          }        }      }      shellReset(&rc, pSql);    }    shellFinalize(&rc, pSql);  }  sqlite3_free(zDir);  sqlite3_free(zWhere);  return rc;}static int arExecSql(ArCommand *pAr, const char *zSql){  int rc;  if( pAr->bDryRun ){    utf8_printf(pAr->p->out, "%s\n", zSql);    rc = SQLITE_OK;  }else{    char *zErr = 0;    rc = sqlite3_exec(pAr->db, zSql, 0, 0, &zErr);    if( zErr ){      utf8_printf(stdout, "ERROR: %s\n", zErr);      sqlite3_free(zErr);    }  }  return rc;}static int arCreateOrUpdateCommand(  ArCommand *pAr,                   int bUpdate,                      int bOnlyIfChanged              ){  const char *zCreate =       "CREATE TABLE IF NOT EXISTS sqlar(\n"      "  name TEXT PRIMARY KEY,  -- name of the file\n"      "  mode INT,               -- access permissions\n"      "  mtime INT,              -- last modification time\n"      "  sz INT,                 -- original file size\n"      "  data BLOB               -- compressed content\n"      ")";  const char *zDrop = "DROP TABLE IF EXISTS sqlar";  const char *zInsertFmt[2] = {     "REPLACE INTO %s(name,mode,mtime,sz,data)\n"     "  SELECT\n"     "    %s,\n"     "    mode,\n"     "    mtime,\n"     "    CASE substr(lsmode(mode),1,1)\n"     "      WHEN '-' THEN length(data)\n"     "      WHEN 'd' THEN 0\n"     "      ELSE -1 END,\n"     "    sqlar_compress(data)\n"     "  FROM fsdir(%Q,%Q) AS disk\n"     "  WHERE lsmode(mode) NOT LIKE '?%%'%s;"     ,     "REPLACE INTO %s(name,mode,mtime,data)\n"     "  SELECT\n"     "    %s,\n"     "    mode,\n"     "    mtime,\n"     "    data\n"     "  FROM fsdir(%Q,%Q) AS disk\n"     "  WHERE lsmode(mode) NOT LIKE '?%%'%s;"  };  int i;                            int rc;                           const char *zTab = 0;             char *zSql;  char zTemp[50];  char *zExists = 0;  arExecSql(pAr, "PRAGMA page_size=512");  rc = arExecSql(pAr, "SAVEPOINT ar;");  if( rc!=SQLITE_OK ) return rc;  zTemp[0] = 0;   if( pAr->bZip ){        if( pAr->zFile ){      sqlite3_uint64 r;      sqlite3_randomness(sizeof(r),&r);      sqlite3_snprintf(sizeof(zTemp),zTemp,"zip%016llx",r);      zTab = zTemp;      zSql = sqlite3_mprintf(         "CREATE VIRTUAL TABLE temp.%s USING zipfile(%Q)",         zTab, pAr->zFile      );      rc = arExecSql(pAr, zSql);      sqlite3_free(zSql);    }else{      zTab = "zip";    }  }else{        zTab = "sqlar";    if( bUpdate==0 ){      rc = arExecSql(pAr, zDrop);      if( rc!=SQLITE_OK ) goto end_ar_transaction;    }    rc = arExecSql(pAr, zCreate);  }  if( bOnlyIfChanged ){    zExists = sqlite3_mprintf(      " AND NOT EXISTS("          "SELECT 1 FROM %s AS mem"          " WHERE mem.name=disk.name"          " AND mem.mtime=disk.mtime"          " AND mem.mode=disk.mode)", zTab);  }else{    zExists = sqlite3_mprintf("");  }  if( zExists==0 ) rc = SQLITE_NOMEM;  for(i=0; i<pAr->nArg && rc==SQLITE_OK; i++){    char *zSql2 = sqlite3_mprintf(zInsertFmt[pAr->bZip], zTab,        pAr->bVerbose ? "shell_putsnl(name)" : "name",        pAr->azArg[i], pAr->zDir, zExists);    rc = arExecSql(pAr, zSql2);    sqlite3_free(zSql2);  }end_ar_transaction:  if( rc!=SQLITE_OK ){    sqlite3_exec(pAr->db, "ROLLBACK TO ar; RELEASE ar;", 0, 0, 0);  }else{    rc = arExecSql(pAr, "RELEASE ar;");    if( pAr->bZip && pAr->zFile ){      zSql = sqlite3_mprintf("DROP TABLE %s", zTemp);      arExecSql(pAr, zSql);      sqlite3_free(zSql);    }  }  sqlite3_free(zExists);  return rc;}static int arDotCommand(  ShellState *pState,            int fromCmdLine,               char **azArg,                  int nArg                     ){  ArCommand cmd;  int rc;  memset(&cmd, 0, sizeof(cmd));  cmd.fromCmdLine = fromCmdLine;  rc = arParseCommand(azArg, nArg, &cmd);  if( rc==SQLITE_OK ){    int eDbType = SHELL_OPEN_UNSPEC;    cmd.p = pState;    cmd.db = pState->db;    if( cmd.zFile ){      eDbType = deduceDatabaseType(cmd.zFile, 1);    }else{      eDbType = pState->openMode;    }    if( eDbType==SHELL_OPEN_ZIPFILE ){      if( cmd.eCmd==AR_CMD_EXTRACT || cmd.eCmd==AR_CMD_LIST ){        if( cmd.zFile==0 ){          cmd.zSrcTable = sqlite3_mprintf("zip");        }else{          cmd.zSrcTable = sqlite3_mprintf("zipfile(%Q)", cmd.zFile);        }      }      cmd.bZip = 1;    }else if( cmd.zFile ){      int flags;      if( cmd.bAppend ) eDbType = SHELL_OPEN_APPENDVFS;      if( cmd.eCmd==AR_CMD_CREATE || cmd.eCmd==AR_CMD_INSERT            || cmd.eCmd==AR_CMD_REMOVE || cmd.eCmd==AR_CMD_UPDATE ){        flags = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE;      }else{        flags = SQLITE_OPEN_READONLY;      }      cmd.db = 0;      if( cmd.bDryRun ){        utf8_printf(pState->out, "-- open database '%s'%s\n", cmd.zFile,             eDbType==SHELL_OPEN_APPENDVFS ? " using 'apndvfs'" : "");      }      rc = sqlite3_open_v2(cmd.zFile, &cmd.db, flags,              eDbType==SHELL_OPEN_APPENDVFS ? "apndvfs" : 0);      if( rc!=SQLITE_OK ){        utf8_printf(stderr, "cannot open file: %s (%s)\n",             cmd.zFile, sqlite3_errmsg(cmd.db)        );        goto end_ar_command;      }      sqlite3_fileio_init(cmd.db, 0, 0);      sqlite3_sqlar_init(cmd.db, 0, 0);      sqlite3_create_function(cmd.db, "shell_putsnl", 1, SQLITE_UTF8, cmd.p,                              shellPutsFunc, 0, 0);    }    if( cmd.zSrcTable==0 && cmd.bZip==0 && cmd.eCmd!=AR_CMD_HELP ){      if( cmd.eCmd!=AR_CMD_CREATE       && sqlite3_table_column_metadata(cmd.db,0,"sqlar","name",0,0,0,0,0)      ){        utf8_printf(stderr, "database does not contain an 'sqlar' table\n");        rc = SQLITE_ERROR;        goto end_ar_command;      }      cmd.zSrcTable = sqlite3_mprintf("sqlar");    }    switch( cmd.eCmd ){      case AR_CMD_CREATE:        rc = arCreateOrUpdateCommand(&cmd, 0, 0);        break;      case AR_CMD_EXTRACT:        rc = arExtractCommand(&cmd);        break;      case AR_CMD_LIST:        rc = arListCommand(&cmd);        break;      case AR_CMD_HELP:        arUsage(pState->out);        break;      case AR_CMD_INSERT:        rc = arCreateOrUpdateCommand(&cmd, 1, 0);        break;      case AR_CMD_REMOVE:        rc = arRemoveCommand(&cmd);        break;      default:        assert( cmd.eCmd==AR_CMD_UPDATE );        rc = arCreateOrUpdateCommand(&cmd, 1, 1);        break;    }  }end_ar_command:  if( cmd.db!=pState->db ){    close_db(cmd.db);  }  sqlite3_free(cmd.zSrcTable);  return rc;}#endif #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)static void shellExec(sqlite3 *db, int *pRc, const char *zSql){  int rc = *pRc;  if( rc==SQLITE_OK ){    char *zErr = 0;    rc = sqlite3_exec(db, zSql, 0, 0, &zErr);    if( rc!=SQLITE_OK ){      raw_printf(stderr, "SQL error: %s\n", zErr);    }    sqlite3_free(zErr);    *pRc = rc;  }}static void shellExecPrintf(sqlite3 *db, int *pRc, const char *zFmt, ...){  char *z = 0;  if( *pRc==SQLITE_OK ){    va_list ap;    va_start(ap, zFmt);    z = sqlite3_vmprintf(zFmt, ap);    va_end(ap);    if( z==0 ){      *pRc = SQLITE_NOMEM;    }else{      shellExec(db, pRc, z);    }    sqlite3_free(z);  }}static void *shellMalloc(int *pRc, sqlite3_int64 nByte){  void *pRet = 0;  if( *pRc==SQLITE_OK ){    pRet = sqlite3_malloc64(nByte);    if( pRet==0 ){      *pRc = SQLITE_NOMEM;    }else{      memset(pRet, 0, nByte);    }  }  return pRet;}static char *shellMPrintf(int *pRc, const char *zFmt, ...){  char *z = 0;  if( *pRc==SQLITE_OK ){    va_list ap;    va_start(ap, zFmt);    z = sqlite3_vmprintf(zFmt, ap);    va_end(ap);    if( z==0 ){      *pRc = SQLITE_NOMEM;    }  }  return z;}typedef struct RecoverTable RecoverTable;struct RecoverTable {  char *zQuoted;                    int nCol;                         char **azlCol;                    int iPk;                        };static void recoverFreeTable(RecoverTable *pTab){  if( pTab ){    sqlite3_free(pTab->zQuoted);    if( pTab->azlCol ){      int i;      for(i=0; i<=pTab->nCol; i++){        sqlite3_free(pTab->azlCol[i]);      }      sqlite3_free(pTab->azlCol);    }    sqlite3_free(pTab);  }}static RecoverTable *recoverNewTable(  int *pRc,                         const char *zName,                const char *zSql,                 int bIntkey,   int nCol){  sqlite3 *dbtmp = 0;               int rc = *pRc;  RecoverTable *pTab = 0;  pTab = (RecoverTable*)shellMalloc(&rc, sizeof(RecoverTable));  if( rc==SQLITE_OK ){    int nSqlCol = 0;    int bSqlIntkey = 0;    sqlite3_stmt *pStmt = 0;        rc = sqlite3_open("", &dbtmp);    if( rc==SQLITE_OK ){      sqlite3_create_function(dbtmp, "shell_idquote", 1, SQLITE_UTF8, 0,                              shellIdQuote, 0, 0);    }    if( rc==SQLITE_OK ){      rc = sqlite3_exec(dbtmp, "PRAGMA writable_schema = on", 0, 0, 0);    }    if( rc==SQLITE_OK ){      rc = sqlite3_exec(dbtmp, zSql, 0, 0, 0);      if( rc==SQLITE_ERROR ){        rc = SQLITE_OK;        goto finished;      }    }    shellPreparePrintf(dbtmp, &rc, &pStmt,         "SELECT count(*) FROM pragma_table_info(%Q)", zName    );    if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){      nSqlCol = sqlite3_column_int(pStmt, 0);    }    shellFinalize(&rc, pStmt);    if( rc!=SQLITE_OK || nSqlCol<nCol ){      goto finished;    }    shellPreparePrintf(dbtmp, &rc, &pStmt,       "SELECT ("      "  SELECT substr(data,1,1)==X'0D' FROM sqlite_dbpage WHERE pgno=rootpage"      ") FROM sqlite_schema WHERE name = %Q", zName    );    if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){      bSqlIntkey = sqlite3_column_int(pStmt, 0);    }    shellFinalize(&rc, pStmt);    if( bIntkey==bSqlIntkey ){      int i;      const char *zPk = "_rowid_";      sqlite3_stmt *pPkFinder = 0;            pTab->iPk = -2;      if( bIntkey ){        shellPreparePrintf(dbtmp, &rc, &pPkFinder,           "SELECT cid, name FROM pragma_table_info(%Q) "          "  WHERE pk=1 AND type='integer' COLLATE nocase"          "  AND NOT EXISTS (SELECT cid FROM pragma_table_info(%Q) WHERE pk=2)"          , zName, zName        );        if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pPkFinder) ){          pTab->iPk = sqlite3_column_int(pPkFinder, 0);          zPk = (const char*)sqlite3_column_text(pPkFinder, 1);          if( zPk==0 ){ zPk = "_";   }        }      }      pTab->zQuoted = shellMPrintf(&rc, "\"%w\"", zName);      pTab->azlCol = (char**)shellMalloc(&rc, sizeof(char*) * (nSqlCol+1));      pTab->nCol = nSqlCol;      if( bIntkey ){        pTab->azlCol[0] = shellMPrintf(&rc, "\"%w\"", zPk);      }else{        pTab->azlCol[0] = shellMPrintf(&rc, "");      }      i = 1;      shellPreparePrintf(dbtmp, &rc, &pStmt,           "SELECT %Q || group_concat(shell_idquote(name), ', ') "          "  FILTER (WHERE cid!=%d) OVER (ORDER BY %s cid) "          "FROM pragma_table_info(%Q)",           bIntkey ? ", " : "", pTab->iPk,           bIntkey ? "" : "(CASE WHEN pk=0 THEN 1000000 ELSE pk END), ",          zName      );      while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){        const char *zText = (const char*)sqlite3_column_text(pStmt, 0);        pTab->azlCol[i] = shellMPrintf(&rc, "%s%s", pTab->azlCol[0], zText);        i++;      }      shellFinalize(&rc, pStmt);      shellFinalize(&rc, pPkFinder);    }  } finished:  sqlite3_close(dbtmp);  *pRc = rc;  if( rc!=SQLITE_OK || (pTab && pTab->zQuoted==0) ){    recoverFreeTable(pTab);    pTab = 0;  }  return pTab;}static RecoverTable *recoverFindTable(  ShellState *pState,               int *pRc,                         int iRoot,                        int bIntkey,                      int nCol,                         int *pbNoop                     ){  sqlite3_stmt *pStmt = 0;  RecoverTable *pRet = 0;  int bNoop = 0;  const char *zSql = 0;  const char *zName = 0;    shellPreparePrintf(pState->db, pRc, &pStmt,      "SELECT type, name, sql FROM recovery.schema WHERE rootpage=%d", iRoot  );  while( *pRc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){    const char *zType = (const char*)sqlite3_column_text(pStmt, 0);    if( bIntkey==0 && sqlite3_stricmp(zType, "index")==0 ){      bNoop = 1;      break;    }    if( sqlite3_stricmp(zType, "table")==0 ){      zName = (const char*)sqlite3_column_text(pStmt, 1);      zSql = (const char*)sqlite3_column_text(pStmt, 2);      if( zName!=0 && zSql!=0 ){        pRet = recoverNewTable(pRc, zName, zSql, bIntkey, nCol);        break;      }    }  }  shellFinalize(pRc, pStmt);  *pbNoop = bNoop;  return pRet;}static RecoverTable *recoverOrphanTable(  ShellState *pState,               int *pRc,                         const char *zLostAndFound,        int nCol                        ){  RecoverTable *pTab = 0;  if( nCol>=0 && *pRc==SQLITE_OK ){    int i;        int iTab = 0;    char *zTab = shellMPrintf(pRc, "%s", zLostAndFound);    sqlite3_stmt *pTest = 0;    shellPrepare(pState->db, pRc,        "SELECT 1 FROM recovery.schema WHERE name=?", &pTest    );    if( pTest ) sqlite3_bind_text(pTest, 1, zTab, -1, SQLITE_TRANSIENT);    while( *pRc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pTest) ){      shellReset(pRc, pTest);      sqlite3_free(zTab);      zTab = shellMPrintf(pRc, "%s_%d", zLostAndFound, iTab++);      sqlite3_bind_text(pTest, 1, zTab, -1, SQLITE_TRANSIENT);    }    shellFinalize(pRc, pTest);    pTab = (RecoverTable*)shellMalloc(pRc, sizeof(RecoverTable));    if( pTab ){      pTab->zQuoted = shellMPrintf(pRc, "\"%w\"", zTab);      pTab->nCol = nCol;      pTab->iPk = -2;      if( nCol>0 ){        pTab->azlCol = (char**)shellMalloc(pRc, sizeof(char*) * (nCol+1));        if( pTab->azlCol ){          pTab->azlCol[nCol] = shellMPrintf(pRc, "");          for(i=nCol-1; i>=0; i--){            pTab->azlCol[i] = shellMPrintf(pRc, "%s, NULL", pTab->azlCol[i+1]);          }        }      }      if( *pRc!=SQLITE_OK ){        recoverFreeTable(pTab);        pTab = 0;      }else{        raw_printf(pState->out,             "CREATE TABLE %s(rootpgno INTEGER, "            "pgno INTEGER, nfield INTEGER, id INTEGER", pTab->zQuoted        );        for(i=0; i<nCol; i++){          raw_printf(pState->out, ", c%d", i);        }        raw_printf(pState->out, ");\n");      }    }    sqlite3_free(zTab);  }  return pTab;}static int recoverDatabaseCmd(ShellState *pState, int nArg, char **azArg){  int rc = SQLITE_OK;  sqlite3_stmt *pLoop = 0;          sqlite3_stmt *pPages = 0;         sqlite3_stmt *pCells = 0;         const char *zRecoveryDb = "";     const char *zLostAndFound = "lost_and_found";  int i;  int nOrphan = -1;  RecoverTable *pOrphan = 0;  int bFreelist = 1;                int bRowids = 1;                  for(i=1; i<nArg; i++){    char *z = azArg[i];    int n;    if( z[0]=='-' && z[1]=='-' ) z++;    n = strlen30(z);    if( n<=17 && memcmp("-freelist-corrupt", z, n)==0 ){      bFreelist = 0;    }else    if( n<=12 && memcmp("-recovery-db", z, n)==0 && i<(nArg-1) ){      i++;      zRecoveryDb = azArg[i];    }else    if( n<=15 && memcmp("-lost-and-found", z, n)==0 && i<(nArg-1) ){      i++;      zLostAndFound = azArg[i];    }else    if( n<=10 && memcmp("-no-rowids", z, n)==0 ){      bRowids = 0;    }    else{      utf8_printf(stderr, "unexpected option: %s\n", azArg[i]);       showHelp(pState->out, azArg[0]);      return 1;    }  }  shellExecPrintf(pState->db, &rc,        "PRAGMA writable_schema = on;"    "ATTACH %Q AS recovery;"    "DROP TABLE IF EXISTS recovery.dbptr;"    "DROP TABLE IF EXISTS recovery.freelist;"    "DROP TABLE IF EXISTS recovery.map;"    "DROP TABLE IF EXISTS recovery.schema;"    "CREATE TABLE recovery.freelist(pgno INTEGER PRIMARY KEY);", zRecoveryDb  );  if( bFreelist ){    shellExec(pState->db, &rc,      "WITH trunk(pgno) AS ("      "  SELECT shell_int32("      "      (SELECT data FROM sqlite_dbpage WHERE pgno=1), 8) AS x "      "      WHERE x>0"      "    UNION"      "  SELECT shell_int32("      "      (SELECT data FROM sqlite_dbpage WHERE pgno=trunk.pgno), 0) AS x "      "      FROM trunk WHERE x>0"      "),"      "freelist(data, n, freepgno) AS ("      "  SELECT data, min(16384, shell_int32(data, 1)-1), t.pgno "      "      FROM trunk t, sqlite_dbpage s WHERE s.pgno=t.pgno"      "    UNION ALL"      "  SELECT data, n-1, shell_int32(data, 2+n) "      "      FROM freelist WHERE n>=0"      ")"      "REPLACE INTO recovery.freelist SELECT freepgno FROM freelist;"    );  }    shellExec(pState->db, &rc,     "WITH ptrmap(pgno) AS ("    "  SELECT 2 WHERE shell_int32("    "    (SELECT data FROM sqlite_dbpage WHERE pgno=1), 13"    "  )"    "    UNION ALL "    "  SELECT pgno+1+(SELECT page_size FROM pragma_page_size)/5 AS pp "    "  FROM ptrmap WHERE pp<=(SELECT page_count FROM pragma_page_count)"    ")"    "REPLACE INTO recovery.freelist SELECT pgno FROM ptrmap"  );  shellExec(pState->db, &rc,     "CREATE TABLE recovery.dbptr("    "      pgno, child, PRIMARY KEY(child, pgno)"    ") WITHOUT ROWID;"    "INSERT OR IGNORE INTO recovery.dbptr(pgno, child) "    "    SELECT * FROM sqlite_dbptr"    "      WHERE pgno NOT IN freelist AND child NOT IN freelist;"        "DELETE FROM recovery.dbptr WHERE child = 1;"        "DELETE FROM recovery.dbptr WHERE child IN ("    "  SELECT child FROM recovery.dbptr GROUP BY child HAVING count(*)>1"    ");"        "CREATE TABLE recovery.map("      "pgno INTEGER PRIMARY KEY, maxlen INT, intkey, root INT"    ");"        "WITH pages(i, maxlen) AS ("    "  SELECT page_count, ("    "    SELECT max(field+1) FROM sqlite_dbdata WHERE pgno=page_count"    "  ) FROM pragma_page_count WHERE page_count>0"    "    UNION ALL"    "  SELECT i-1, ("    "    SELECT max(field+1) FROM sqlite_dbdata WHERE pgno=i-1"    "  ) FROM pages WHERE i>=2"    ")"    "INSERT INTO recovery.map(pgno, maxlen, intkey, root) "    "  SELECT i, maxlen, NULL, ("    "    WITH p(orig, pgno, parent) AS ("    "      SELECT 0, i, (SELECT pgno FROM recovery.dbptr WHERE child=i)"    "        UNION "    "      SELECT i, p.parent, "    "        (SELECT pgno FROM recovery.dbptr WHERE child=p.parent) FROM p"    "    )"    "    SELECT pgno FROM p WHERE (parent IS NULL OR pgno = orig)"    ") "    "FROM pages WHERE maxlen IS NOT NULL AND i NOT IN freelist;"    "UPDATE recovery.map AS o SET intkey = ("    "  SELECT substr(data, 1, 1)==X'0D' FROM sqlite_dbpage WHERE pgno=o.pgno"    ");"        "CREATE TABLE recovery.schema(type, name, tbl_name, rootpage, sql);"    "INSERT INTO recovery.schema SELECT "    "  max(CASE WHEN field=0 THEN value ELSE NULL END),"    "  max(CASE WHEN field=1 THEN value ELSE NULL END),"    "  max(CASE WHEN field=2 THEN value ELSE NULL END),"    "  max(CASE WHEN field=3 THEN value ELSE NULL END),"    "  max(CASE WHEN field=4 THEN value ELSE NULL END)"    "FROM sqlite_dbdata WHERE pgno IN ("    "  SELECT pgno FROM recovery.map WHERE root=1"    ")"    "GROUP BY pgno, cell;"    "CREATE INDEX recovery.schema_rootpage ON schema(rootpage);"  );    if( rc==SQLITE_OK ){    sqlite3_stmt *pStmt = 0;        raw_printf(pState->out, "PRAGMA foreign_keys=OFF;\n");    raw_printf(pState->out, "BEGIN;\n");    raw_printf(pState->out, "PRAGMA writable_schema = on;\n");    shellPrepare(pState->db, &rc,        "SELECT sql FROM recovery.schema "        "WHERE type='table' AND sql LIKE 'create table%'", &pStmt    );    while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){      const char *zCreateTable = (const char*)sqlite3_column_text(pStmt, 0);      raw_printf(pState->out, "CREATE TABLE IF NOT EXISTS %s;\n",           &zCreateTable[12]      );    }    shellFinalize(&rc, pStmt);  }    shellPrepare(pState->db, &rc,       "SELECT coalesce(max(maxlen), -2) FROM recovery.map WHERE root>1"      , &pLoop  );  if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pLoop) ){    nOrphan = sqlite3_column_int(pLoop, 0);  }  shellFinalize(&rc, pLoop);  pLoop = 0;  shellPrepare(pState->db, &rc,      "SELECT pgno FROM recovery.map WHERE root=?", &pPages  );  shellPrepare(pState->db, &rc,      "SELECT max(field), group_concat(shell_escape_crnl(quote"      "(case when (? AND field<0) then NULL else value end)"      "), ', ')"      ", min(field) "      "FROM sqlite_dbdata WHERE pgno = ? AND field != ?"      "GROUP BY cell", &pCells  );    shellPrepare(pState->db, &rc,       "SELECT root, intkey, max(maxlen) FROM recovery.map"       " WHERE root>1 GROUP BY root, intkey ORDER BY root=("      "  SELECT rootpage FROM recovery.schema WHERE name='sqlite_sequence'"      ")", &pLoop  );  while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pLoop) ){    int iRoot = sqlite3_column_int(pLoop, 0);    int bIntkey = sqlite3_column_int(pLoop, 1);    int nCol = sqlite3_column_int(pLoop, 2);    int bNoop = 0;    RecoverTable *pTab;    assert( bIntkey==0 || bIntkey==1 );    pTab = recoverFindTable(pState, &rc, iRoot, bIntkey, nCol, &bNoop);    if( bNoop || rc ) continue;    if( pTab==0 ){      if( pOrphan==0 ){        pOrphan = recoverOrphanTable(pState, &rc, zLostAndFound, nOrphan);      }      pTab = pOrphan;      if( pTab==0 ) break;    }    if( 0==sqlite3_stricmp(pTab->zQuoted, "\"sqlite_sequence\"") ){      raw_printf(pState->out, "DELETE FROM sqlite_sequence;\n");    }    sqlite3_bind_int(pPages, 1, iRoot);    if( bRowids==0 && pTab->iPk<0 ){      sqlite3_bind_int(pCells, 1, 1);    }else{      sqlite3_bind_int(pCells, 1, 0);    }    sqlite3_bind_int(pCells, 3, pTab->iPk);    while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pPages) ){      int iPgno = sqlite3_column_int(pPages, 0);      sqlite3_bind_int(pCells, 2, iPgno);      while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pCells) ){        int nField = sqlite3_column_int(pCells, 0);        int iMin = sqlite3_column_int(pCells, 2);        const char *zVal = (const char*)sqlite3_column_text(pCells, 1);        RecoverTable *pTab2 = pTab;        if( pTab!=pOrphan && (iMin<0)!=bIntkey ){          if( pOrphan==0 ){            pOrphan = recoverOrphanTable(pState, &rc, zLostAndFound, nOrphan);          }          pTab2 = pOrphan;          if( pTab2==0 ) break;        }        nField = nField+1;        if( pTab2==pOrphan ){          raw_printf(pState->out,               "INSERT INTO %s VALUES(%d, %d, %d, %s%s%s);\n",              pTab2->zQuoted, iRoot, iPgno, nField,              iMin<0 ? "" : "NULL, ", zVal, pTab2->azlCol[nField]          );        }else{          raw_printf(pState->out, "INSERT INTO %s(%s) VALUES( %s );\n",               pTab2->zQuoted, pTab2->azlCol[nField], zVal          );        }      }      shellReset(&rc, pCells);    }    shellReset(&rc, pPages);    if( pTab!=pOrphan ) recoverFreeTable(pTab);  }  shellFinalize(&rc, pLoop);  shellFinalize(&rc, pPages);  shellFinalize(&rc, pCells);  recoverFreeTable(pOrphan);    if( rc==SQLITE_OK ){    sqlite3_stmt *pStmt = 0;    shellPrepare(pState->db, &rc,         "SELECT sql, name FROM recovery.schema "        "WHERE sql NOT LIKE 'create table%'", &pStmt    );    while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){      const char *zSql = (const char*)sqlite3_column_text(pStmt, 0);      if( sqlite3_strnicmp(zSql, "create virt", 11)==0 ){        const char *zName = (const char*)sqlite3_column_text(pStmt, 1);        char *zPrint = shellMPrintf(&rc,           "INSERT INTO sqlite_schema VALUES('table', %Q, %Q, 0, %Q)",          zName, zName, zSql        );        raw_printf(pState->out, "%s;\n", zPrint);        sqlite3_free(zPrint);      }else{        raw_printf(pState->out, "%s;\n", zSql);      }    }    shellFinalize(&rc, pStmt);  }  if( rc==SQLITE_OK ){    raw_printf(pState->out, "PRAGMA writable_schema = off;\n");    raw_printf(pState->out, "COMMIT;\n");  }  sqlite3_exec(pState->db, "DETACH recovery", 0, 0, 0);  return rc;}#endif #ifdef SHELL_DEBUG#define rc_err_oom_die(rc) \  if( rc==SQLITE_NOMEM ) shell_check_oom(0); \  else if(!(rc==SQLITE_OK||rc==SQLITE_DONE)) \    fprintf(stderr,"E:%d\n",rc), assert(0)#elsestatic void rc_err_oom_die(int rc){  if( rc==SQLITE_NOMEM ) shell_check_oom(0);  assert(rc==SQLITE_OK||rc==SQLITE_DONE);}#endif#ifdef SHELL_COLFIX_DB static char zCOL_DB[] = SHELL_STRINGIFY(SHELL_COLFIX_DB);#else  static const char *zCOL_DB = ":memory:";#endif#ifndef SHELL_AUTOCOLUMN_SEP# define AUTOCOLUMN_SEP "_"#else# define AUTOCOLUMN_SEP SHELL_STRINGIFY(SHELL_AUTOCOLUMN_SEP)#endifstatic char *zAutoColumn(const char *zColNew, sqlite3 **pDb, char **pzRenamed){    static const char * const zTabMake = "\CREATE TABLE ColNames(\ cpos INTEGER PRIMARY KEY,\ name TEXT, nlen INT, chop INT, reps INT, suff TEXT);\CREATE VIEW RepeatedNames AS \SELECT DISTINCT t.name FROM ColNames t \WHERE t.name COLLATE NOCASE IN (\ SELECT o.name FROM ColNames o WHERE o.cpos<>t.cpos\);\";  static const char * const zTabFill = "\INSERT INTO ColNames(name,nlen,chop,reps,suff)\ VALUES(iif(length(?1)>0,?1,'?'),max(length(?1),1),0,0,'')\";  static const char * const zHasDupes = "\SELECT count(DISTINCT (substring(name,1,nlen-chop)||suff) COLLATE NOCASE)\ <count(name) FROM ColNames\";#ifdef SHELL_COLUMN_RENAME_CLEAN  static const char * const zDedoctor = "\UPDATE ColNames SET chop=iif(\  (substring(name,nlen,1) BETWEEN '0' AND '9')\  AND (rtrim(name,'0123456790') glob '*"AUTOCOLUMN_SEP"'),\ nlen-length(rtrim(name, '"AUTOCOLUMN_SEP"0123456789')),\ 0\)\";#endif  static const char * const zSetReps = "\UPDATE ColNames AS t SET reps=\(SELECT count(*) FROM ColNames d \ WHERE substring(t.name,1,t.nlen-t.chop)=substring(d.name,1,d.nlen-d.chop)\ COLLATE NOCASE\)\";#ifdef SQLITE_ENABLE_MATH_FUNCTIONS  static const char * const zColDigits = "\SELECT CAST(ceil(log(count(*)+0.5)) AS INT) FROM ColNames \";#endif  static const char * const zRenameRank =#ifdef SHELL_COLUMN_RENAME_CLEAN    "UPDATE ColNames AS t SET suff="    "iif(reps>1, printf('%c%0*d', '"AUTOCOLUMN_SEP"', $1, cpos), '')"#else "WITH Lzn(nlz) AS (" "  SELECT 0 AS nlz""  UNION""  SELECT nlz+1 AS nlz FROM Lzn""  WHERE EXISTS(""   SELECT 1""   FROM ColNames t, ColNames o""   WHERE""    iif(t.name IN (SELECT * FROM RepeatedNames),""     printf('%s"AUTOCOLUMN_SEP"%s',""      t.name, substring(printf('%.*c%0.*d',nlz+1,'0',$1,t.cpos),2)),""     t.name""    )""    =""    iif(o.name IN (SELECT * FROM RepeatedNames),""     printf('%s"AUTOCOLUMN_SEP"%s',""      o.name, substring(printf('%.*c%0.*d',nlz+1,'0',$1,o.cpos),2)),""     o.name""    )""    COLLATE NOCASE""    AND o.cpos<>t.cpos""   GROUP BY t.cpos""  )"") UPDATE Colnames AS t SET"" chop = 0," " suff = iif(name IN (SELECT * FROM RepeatedNames),""  printf('"AUTOCOLUMN_SEP"%s', substring(""   printf('%.*c%0.*d',(SELECT max(nlz) FROM Lzn)+1,'0',1,t.cpos),2)),""  ''"" )"#endif    ;  static const char * const zCollectVar = "\SELECT\ '('||x'0a'\ || group_concat(\  cname||' TEXT',\  ','||iif((cpos-1)%4>0, ' ', x'0a'||' '))\ ||')' AS ColsSpec \FROM (\ SELECT cpos, printf('\"%w\"',printf('%!.*s%s', nlen-chop,name,suff)) AS cname \ FROM ColNames ORDER BY cpos\)";  static const char * const zRenamesDone =    "SELECT group_concat("    " printf('\"%w\" to \"%w\"',name,printf('%!.*s%s', nlen-chop, name, suff)),"    " ','||x'0a')"    "FROM ColNames WHERE suff<>'' OR chop!=0"    ;  int rc;  sqlite3_stmt *pStmt = 0;  assert(pDb!=0);  if( zColNew ){        if( *pDb==0 ){      if( SQLITE_OK!=sqlite3_open(zCOL_DB, pDb) ) return 0;#ifdef SHELL_COLFIX_DB      if(*zCOL_DB!=':')        sqlite3_exec(*pDb,"drop table if exists ColNames;"                     "drop view if exists RepeatedNames;",0,0,0);#endif      rc = sqlite3_exec(*pDb, zTabMake, 0, 0, 0);      rc_err_oom_die(rc);    }    assert(*pDb!=0);    rc = sqlite3_prepare_v2(*pDb, zTabFill, -1, &pStmt, 0);    rc_err_oom_die(rc);    rc = sqlite3_bind_text(pStmt, 1, zColNew, -1, 0);    rc_err_oom_die(rc);    rc = sqlite3_step(pStmt);    rc_err_oom_die(rc);    sqlite3_finalize(pStmt);    return 0;  }else if( *pDb==0 ){    return 0;  }else{        char *zColsSpec = 0;    int hasDupes = db_int(*pDb, zHasDupes);#ifdef SQLITE_ENABLE_MATH_FUNCTIONS    int nDigits = (hasDupes)? db_int(*pDb, zColDigits) : 0;#else# define nDigits 2#endif    if( hasDupes ){#ifdef SHELL_COLUMN_RENAME_CLEAN      rc = sqlite3_exec(*pDb, zDedoctor, 0, 0, 0);      rc_err_oom_die(rc);#endif      rc = sqlite3_exec(*pDb, zSetReps, 0, 0, 0);      rc_err_oom_die(rc);      rc = sqlite3_prepare_v2(*pDb, zRenameRank, -1, &pStmt, 0);      rc_err_oom_die(rc);      sqlite3_bind_int(pStmt, 1, nDigits);      rc = sqlite3_step(pStmt);      sqlite3_finalize(pStmt);      assert(rc==SQLITE_DONE);    }    assert(db_int(*pDb, zHasDupes)==0);     rc = sqlite3_prepare_v2(*pDb, zCollectVar, -1, &pStmt, 0);    rc_err_oom_die(rc);    rc = sqlite3_step(pStmt);    if( rc==SQLITE_ROW ){      zColsSpec = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 0));    }else{      zColsSpec = 0;    }    if( pzRenamed!=0 ){      if( !hasDupes ) *pzRenamed = 0;      else{        sqlite3_finalize(pStmt);        if( SQLITE_OK==sqlite3_prepare_v2(*pDb, zRenamesDone, -1, &pStmt, 0)            && SQLITE_ROW==sqlite3_step(pStmt) ){          *pzRenamed = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 0));        }else          *pzRenamed = 0;      }    }    sqlite3_finalize(pStmt);    sqlite3_close(*pDb);    *pDb = 0;    return zColsSpec;  }}static int do_meta_command(char *zLine, ShellState *p){  int h = 1;  int nArg = 0;  int n, c;  int rc = 0;  char *azArg[52];#ifndef SQLITE_OMIT_VIRTUALTABLE  if( p->expert.pExpert ){    expertFinish(p, 1, 0);  }#endif    while( zLine[h] && nArg<ArraySize(azArg)-1 ){    while( IsSpace(zLine[h]) ){ h++; }    if( zLine[h]==0 ) break;    if( zLine[h]=='\'' || zLine[h]=='"' ){      int delim = zLine[h++];      azArg[nArg++] = &zLine[h];      while( zLine[h] && zLine[h]!=delim ){        if( zLine[h]=='\\' && delim=='"' && zLine[h+1]!=0 ) h++;        h++;      }      if( zLine[h]==delim ){        zLine[h++] = 0;      }      if( delim=='"' ) resolve_backslashes(azArg[nArg-1]);    }else{      azArg[nArg++] = &zLine[h];      while( zLine[h] && !IsSpace(zLine[h]) ){ h++; }      if( zLine[h] ) zLine[h++] = 0;      resolve_backslashes(azArg[nArg-1]);    }  }  azArg[nArg] = 0;    if( nArg==0 ) return 0;   n = strlen30(azArg[0]);  c = azArg[0][0];  clearTempFile(p);#ifndef SQLITE_OMIT_AUTHORIZATION  if( c=='a' && strncmp(azArg[0], "auth", n)==0 ){    if( nArg!=2 ){      raw_printf(stderr, "Usage: .auth ON|OFF\n");      rc = 1;      goto meta_command_exit;    }    open_db(p, 0);    if( booleanValue(azArg[1]) ){      sqlite3_set_authorizer(p->db, shellAuth, p);    }else if( p->bSafeModePersist ){      sqlite3_set_authorizer(p->db, safeModeAuth, p);    }else{      sqlite3_set_authorizer(p->db, 0, 0);    }  }else#endif#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)  if( c=='a' && strncmp(azArg[0], "archive", n)==0 ){    open_db(p, 0);    failIfSafeMode(p, "cannot run .archive in safe mode");    rc = arDotCommand(p, 0, azArg, nArg);  }else#endif  if( (c=='b' && n>=3 && strncmp(azArg[0], "backup", n)==0)   || (c=='s' && n>=3 && strncmp(azArg[0], "save", n)==0)  ){    const char *zDestFile = 0;    const char *zDb = 0;    sqlite3 *pDest;    sqlite3_backup *pBackup;    int j;    int bAsync = 0;    const char *zVfs = 0;    failIfSafeMode(p, "cannot run .%s in safe mode", azArg[0]);    for(j=1; j<nArg; j++){      const char *z = azArg[j];      if( z[0]=='-' ){        if( z[1]=='-' ) z++;        if( strcmp(z, "-append")==0 ){          zVfs = "apndvfs";        }else        if( strcmp(z, "-async")==0 ){          bAsync = 1;        }else        {          utf8_printf(stderr, "unknown option: %s\n", azArg[j]);          return 1;        }      }else if( zDestFile==0 ){        zDestFile = azArg[j];      }else if( zDb==0 ){        zDb = zDestFile;        zDestFile = azArg[j];      }else{        raw_printf(stderr, "Usage: .backup ?DB? ?OPTIONS? FILENAME\n");        return 1;      }    }    if( zDestFile==0 ){      raw_printf(stderr, "missing FILENAME argument on .backup\n");      return 1;    }    if( zDb==0 ) zDb = "main";    rc = sqlite3_open_v2(zDestFile, &pDest,                   SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE, zVfs);    if( rc!=SQLITE_OK ){      utf8_printf(stderr, "Error: cannot open \"%s\"\n", zDestFile);      close_db(pDest);      return 1;    }    if( bAsync ){      sqlite3_exec(pDest, "PRAGMA synchronous=OFF; PRAGMA journal_mode=OFF;",                   0, 0, 0);    }    open_db(p, 0);    pBackup = sqlite3_backup_init(pDest, "main", p->db, zDb);    if( pBackup==0 ){      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest));      close_db(pDest);      return 1;    }    while(  (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK ){}    sqlite3_backup_finish(pBackup);    if( rc==SQLITE_DONE ){      rc = 0;    }else{      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest));      rc = 1;    }    close_db(pDest);  }else  if( c=='b' && n>=3 && strncmp(azArg[0], "bail", n)==0 ){    if( nArg==2 ){      bail_on_error = booleanValue(azArg[1]);    }else{      raw_printf(stderr, "Usage: .bail on|off\n");      rc = 1;    }  }else  if( c=='b' && n>=3 && strncmp(azArg[0], "binary", n)==0 ){    if( nArg==2 ){      if( booleanValue(azArg[1]) ){        setBinaryMode(p->out, 1);      }else{        setTextMode(p->out, 1);      }    }else{      raw_printf(stderr, "Usage: .binary on|off\n");      rc = 1;    }  }else    if( c=='b' && n>=3 && strncmp(azArg[0], "breakpoint", n)==0 ){    test_breakpoint();  }else  if( c=='c' && strcmp(azArg[0],"cd")==0 ){    failIfSafeMode(p, "cannot run .cd in safe mode");    if( nArg==2 ){#if defined(_WIN32) || defined(WIN32)      wchar_t *z = sqlite3_win32_utf8_to_unicode(azArg[1]);      rc = !SetCurrentDirectoryW(z);      sqlite3_free(z);#else      rc = chdir(azArg[1]);#endif      if( rc ){        utf8_printf(stderr, "Cannot change to directory \"%s\"\n", azArg[1]);        rc = 1;      }    }else{      raw_printf(stderr, "Usage: .cd DIRECTORY\n");      rc = 1;    }  }else  if( c=='c' && n>=3 && strncmp(azArg[0], "changes", n)==0 ){    if( nArg==2 ){      setOrClearFlag(p, SHFLG_CountChanges, azArg[1]);    }else{      raw_printf(stderr, "Usage: .changes on|off\n");      rc = 1;    }  }else    if( c=='c' && n>=3 && strncmp(azArg[0], "check", n)==0 ){    char *zRes = 0;    output_reset(p);    if( nArg!=2 ){      raw_printf(stderr, "Usage: .check GLOB-PATTERN\n");      rc = 2;    }else if( (zRes = readFile("testcase-out.txt", 0))==0 ){      raw_printf(stderr, "Error: cannot read 'testcase-out.txt'\n");      rc = 2;    }else if( testcase_glob(azArg[1],zRes)==0 ){      utf8_printf(stderr,                 "testcase-%s FAILED\n Expected: [%s]\n      Got: [%s]\n",                 p->zTestcase, azArg[1], zRes);      rc = 1;    }else{      utf8_printf(stdout, "testcase-%s ok\n", p->zTestcase);      p->nCheck++;    }    sqlite3_free(zRes);  }else  if( c=='c' && strncmp(azArg[0], "clone", n)==0 ){    failIfSafeMode(p, "cannot run .clone in safe mode");    if( nArg==2 ){      tryToClone(p, azArg[1]);    }else{      raw_printf(stderr, "Usage: .clone FILENAME\n");      rc = 1;    }  }else  if( c=='c' && strncmp(azArg[0], "connection", n)==0 ){    if( nArg==1 ){            int i;      for(i=0; i<ArraySize(p->aAuxDb); i++){        const char *zFile = p->aAuxDb[i].zDbFilename;        if( p->aAuxDb[i].db==0 && p->pAuxDb!=&p->aAuxDb[i] ){          zFile = "(not open)";        }else if( zFile==0 ){          zFile = "(memory)";        }else if( zFile[0]==0 ){          zFile = "(temporary-file)";        }        if( p->pAuxDb == &p->aAuxDb[i] ){          utf8_printf(stdout, "ACTIVE %d: %s\n", i, zFile);        }else if( p->aAuxDb[i].db!=0 ){          utf8_printf(stdout, "       %d: %s\n", i, zFile);        }      }    }else if( nArg==2 && IsDigit(azArg[1][0]) && azArg[1][1]==0 ){      int i = azArg[1][0] - '0';      if( p->pAuxDb != &p->aAuxDb[i] && i>=0 && i<ArraySize(p->aAuxDb) ){        p->pAuxDb->db = p->db;        p->pAuxDb = &p->aAuxDb[i];        globalDb = p->db = p->pAuxDb->db;        p->pAuxDb->db = 0;      }    }else if( nArg==3 && strcmp(azArg[1], "close")==0           && IsDigit(azArg[2][0]) && azArg[2][1]==0 ){      int i = azArg[2][0] - '0';      if( i<0 || i>=ArraySize(p->aAuxDb) ){              }else if( p->pAuxDb == &p->aAuxDb[i] ){        raw_printf(stderr, "cannot close the active database connection\n");        rc = 1;      }else if( p->aAuxDb[i].db ){        session_close_all(p, i);        close_db(p->aAuxDb[i].db);        p->aAuxDb[i].db = 0;      }    }else{      raw_printf(stderr, "Usage: .connection [close] [CONNECTION-NUMBER]\n");      rc = 1;    }  }else  if( c=='d' && n>1 && strncmp(azArg[0], "databases", n)==0 ){    char **azName = 0;    int nName = 0;    sqlite3_stmt *pStmt;    int i;    open_db(p, 0);    rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0);    if( rc ){      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));      rc = 1;    }else{      while( sqlite3_step(pStmt)==SQLITE_ROW ){        const char *zSchema = (const char *)sqlite3_column_text(pStmt,1);        const char *zFile = (const char*)sqlite3_column_text(pStmt,2);        if( zSchema==0 || zFile==0 ) continue;        azName = sqlite3_realloc(azName, (nName+1)*2*sizeof(char*));        shell_check_oom(azName);        azName[nName*2] = strdup(zSchema);        azName[nName*2+1] = strdup(zFile);        nName++;      }    }    sqlite3_finalize(pStmt);    for(i=0; i<nName; i++){      int eTxn = sqlite3_txn_state(p->db, azName[i*2]);      int bRdonly = sqlite3_db_readonly(p->db, azName[i*2]);      const char *z = azName[i*2+1];      utf8_printf(p->out, "%s: %s %s%s\n",         azName[i*2],         z && z[0] ? z : "\"\"",         bRdonly ? "r/o" : "r/w",         eTxn==SQLITE_TXN_NONE ? "" :            eTxn==SQLITE_TXN_READ ? " read-txn" : " write-txn");      free(azName[i*2]);      free(azName[i*2+1]);    }    sqlite3_free(azName);  }else  if( c=='d' && n>=3 && strncmp(azArg[0], "dbconfig", n)==0 ){    static const struct DbConfigChoices {      const char *zName;      int op;    } aDbConfig[] = {        { "defensive",          SQLITE_DBCONFIG_DEFENSIVE             },        { "dqs_ddl",            SQLITE_DBCONFIG_DQS_DDL               },        { "dqs_dml",            SQLITE_DBCONFIG_DQS_DML               },        { "enable_fkey",        SQLITE_DBCONFIG_ENABLE_FKEY           },        { "enable_qpsg",        SQLITE_DBCONFIG_ENABLE_QPSG           },        { "enable_trigger",     SQLITE_DBCONFIG_ENABLE_TRIGGER        },        { "enable_view",        SQLITE_DBCONFIG_ENABLE_VIEW           },        { "fts3_tokenizer",     SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER },        { "legacy_alter_table", SQLITE_DBCONFIG_LEGACY_ALTER_TABLE    },        { "legacy_file_format", SQLITE_DBCONFIG_LEGACY_FILE_FORMAT    },        { "load_extension",     SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION },        { "no_ckpt_on_close",   SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE      },        { "reset_database",     SQLITE_DBCONFIG_RESET_DATABASE        },        { "trigger_eqp",        SQLITE_DBCONFIG_TRIGGER_EQP           },        { "trusted_schema",     SQLITE_DBCONFIG_TRUSTED_SCHEMA        },        { "writable_schema",    SQLITE_DBCONFIG_WRITABLE_SCHEMA       },    };    int ii, v;    open_db(p, 0);    for(ii=0; ii<ArraySize(aDbConfig); ii++){      if( nArg>1 && strcmp(azArg[1], aDbConfig[ii].zName)!=0 ) continue;      if( nArg>=3 ){        sqlite3_db_config(p->db, aDbConfig[ii].op, booleanValue(azArg[2]), 0);      }      sqlite3_db_config(p->db, aDbConfig[ii].op, -1, &v);      utf8_printf(p->out, "%19s %s\n", aDbConfig[ii].zName, v ? "on" : "off");      if( nArg>1 ) break;    }    if( nArg>1 && ii==ArraySize(aDbConfig) ){      utf8_printf(stderr, "Error: unknown dbconfig \"%s\"\n", azArg[1]);      utf8_printf(stderr, "Enter \".dbconfig\" with no arguments for a list\n");    }     }else  if( c=='d' && n>=3 && strncmp(azArg[0], "dbinfo", n)==0 ){    rc = shell_dbinfo_command(p, nArg, azArg);  }else#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)  if( c=='r' && strncmp(azArg[0], "recover", n)==0 ){    open_db(p, 0);    rc = recoverDatabaseCmd(p, nArg, azArg);  }else#endif   if( c=='d' && strncmp(azArg[0], "dump", n)==0 ){    char *zLike = 0;    char *zSql;    int i;    int savedShowHeader = p->showHeader;    int savedShellFlags = p->shellFlgs;    ShellClearFlag(p,        SHFLG_PreserveRowid|SHFLG_Newlines|SHFLG_Echo       |SHFLG_DumpDataOnly|SHFLG_DumpNoSys);    for(i=1; i<nArg; i++){      if( azArg[i][0]=='-' ){        const char *z = azArg[i]+1;        if( z[0]=='-' ) z++;        if( strcmp(z,"preserve-rowids")==0 ){#ifdef SQLITE_OMIT_VIRTUALTABLE          raw_printf(stderr, "The --preserve-rowids option is not compatible"                             " with SQLITE_OMIT_VIRTUALTABLE\n");          rc = 1;          sqlite3_free(zLike);          goto meta_command_exit;#else          ShellSetFlag(p, SHFLG_PreserveRowid);#endif        }else        if( strcmp(z,"newlines")==0 ){          ShellSetFlag(p, SHFLG_Newlines);        }else        if( strcmp(z,"data-only")==0 ){          ShellSetFlag(p, SHFLG_DumpDataOnly);        }else        if( strcmp(z,"nosys")==0 ){          ShellSetFlag(p, SHFLG_DumpNoSys);        }else        {          raw_printf(stderr, "Unknown option \"%s\" on \".dump\"\n", azArg[i]);          rc = 1;          sqlite3_free(zLike);          goto meta_command_exit;        }      }else{                char *zExpr = sqlite3_mprintf(            "name LIKE %Q ESCAPE '\\' OR EXISTS ("            "  SELECT 1 FROM sqlite_schema WHERE "            "    name LIKE %Q ESCAPE '\\' AND"            "    sql LIKE 'CREATE VIRTUAL TABLE%%' AND"            "    substr(o.name, 1, length(name)+1) == (name||'_')"            ")", azArg[i], azArg[i]        );              if( zLike ){          zLike = sqlite3_mprintf("%z OR %z", zLike, zExpr);        }else{          zLike = zExpr;        }      }    }    open_db(p, 0);    if( (p->shellFlgs & SHFLG_DumpDataOnly)==0 ){            raw_printf(p->out, "PRAGMA foreign_keys=OFF;\n");      raw_printf(p->out, "BEGIN TRANSACTION;\n");    }    p->writableSchema = 0;    p->showHeader = 0;        sqlite3_exec(p->db, "SAVEPOINT dump; PRAGMA writable_schema=ON", 0, 0, 0);    p->nErr = 0;    if( zLike==0 ) zLike = sqlite3_mprintf("true");    zSql = sqlite3_mprintf(      "SELECT name, type, sql FROM sqlite_schema AS o "      "WHERE (%s) AND type=='table'"      "  AND sql NOT NULL"      " ORDER BY tbl_name='sqlite_sequence', rowid",      zLike    );    run_schema_dump_query(p,zSql);    sqlite3_free(zSql);    if( (p->shellFlgs & SHFLG_DumpDataOnly)==0 ){      zSql = sqlite3_mprintf(        "SELECT sql FROM sqlite_schema AS o "        "WHERE (%s) AND sql NOT NULL"        "  AND type IN ('index','trigger','view')",        zLike      );      run_table_dump_query(p, zSql);      sqlite3_free(zSql);    }    sqlite3_free(zLike);    if( p->writableSchema ){      raw_printf(p->out, "PRAGMA writable_schema=OFF;\n");      p->writableSchema = 0;    }    sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0);    sqlite3_exec(p->db, "RELEASE dump;", 0, 0, 0);    if( (p->shellFlgs & SHFLG_DumpDataOnly)==0 ){      raw_printf(p->out, p->nErr?"ROLLBACK; -- due to errors\n":"COMMIT;\n");    }    p->showHeader = savedShowHeader;    p->shellFlgs = savedShellFlags;  }else  if( c=='e' && strncmp(azArg[0], "echo", n)==0 ){    if( nArg==2 ){      setOrClearFlag(p, SHFLG_Echo, azArg[1]);    }else{      raw_printf(stderr, "Usage: .echo on|off\n");      rc = 1;    }  }else  if( c=='e' && strncmp(azArg[0], "eqp", n)==0 ){    if( nArg==2 ){      p->autoEQPtest = 0;      if( p->autoEQPtrace ){        if( p->db ) sqlite3_exec(p->db, "PRAGMA vdbe_trace=OFF;", 0, 0, 0);        p->autoEQPtrace = 0;      }      if( strcmp(azArg[1],"full")==0 ){        p->autoEQP = AUTOEQP_full;      }else if( strcmp(azArg[1],"trigger")==0 ){        p->autoEQP = AUTOEQP_trigger;#ifdef SQLITE_DEBUG      }else if( strcmp(azArg[1],"test")==0 ){        p->autoEQP = AUTOEQP_on;        p->autoEQPtest = 1;      }else if( strcmp(azArg[1],"trace")==0 ){        p->autoEQP = AUTOEQP_full;        p->autoEQPtrace = 1;        open_db(p, 0);        sqlite3_exec(p->db, "SELECT name FROM sqlite_schema LIMIT 1", 0, 0, 0);        sqlite3_exec(p->db, "PRAGMA vdbe_trace=ON;", 0, 0, 0);#endif      }else{        p->autoEQP = (u8)booleanValue(azArg[1]);      }    }else{      raw_printf(stderr, "Usage: .eqp off|on|trace|trigger|full\n");      rc = 1;    }  }else  if( c=='e' && strncmp(azArg[0], "exit", n)==0 ){    if( nArg>1 && (rc = (int)integerValue(azArg[1]))!=0 ) exit(rc);    rc = 2;  }else    if( c=='e' && strncmp(azArg[0], "explain", n)==0 ){    int val = 1;    if( nArg>=2 ){      if( strcmp(azArg[1],"auto")==0 ){        val = 99;      }else{        val =  booleanValue(azArg[1]);      }    }    if( val==1 && p->mode!=MODE_Explain ){      p->normalMode = p->mode;      p->mode = MODE_Explain;      p->autoExplain = 0;    }else if( val==0 ){      if( p->mode==MODE_Explain ) p->mode = p->normalMode;      p->autoExplain = 0;    }else if( val==99 ){      if( p->mode==MODE_Explain ) p->mode = p->normalMode;      p->autoExplain = 1;    }  }else#ifndef SQLITE_OMIT_VIRTUALTABLE  if( c=='e' && strncmp(azArg[0], "expert", n)==0 ){    if( p->bSafeMode ){      raw_printf(stderr,         "Cannot run experimental commands such as \"%s\" in safe mode\n",        azArg[0]);      rc = 1;    }else{      open_db(p, 0);      expertDotCommand(p, azArg, nArg);    }  }else#endif  if( c=='f' && strncmp(azArg[0], "filectrl", n)==0 ){    static const struct {       const char *zCtrlName;          int ctrlCode;                   const char *zUsage;          } aCtrl[] = {      { "chunk_size",     SQLITE_FCNTL_CHUNK_SIZE,      "SIZE"           },      { "data_version",   SQLITE_FCNTL_DATA_VERSION,    ""               },      { "has_moved",      SQLITE_FCNTL_HAS_MOVED,       ""               },        { "lock_timeout",   SQLITE_FCNTL_LOCK_TIMEOUT,    "MILLISEC"       },      { "persist_wal",    SQLITE_FCNTL_PERSIST_WAL,     "[BOOLEAN]"      },         { "psow",       SQLITE_FCNTL_POWERSAFE_OVERWRITE, "[BOOLEAN]"      },      { "reserve_bytes",  SQLITE_FCNTL_RESERVE_BYTES,   "[N]"            },      { "size_limit",     SQLITE_FCNTL_SIZE_LIMIT,      "[LIMIT]"        },      { "tempfilename",   SQLITE_FCNTL_TEMPFILENAME,    ""               },       };    int filectrl = -1;    int iCtrl = -1;    sqlite3_int64 iRes = 0;      int isOk = 0;                int n2, i;    const char *zCmd = 0;    const char *zSchema = 0;    open_db(p, 0);    zCmd = nArg>=2 ? azArg[1] : "help";    if( zCmd[0]=='-'      && (strcmp(zCmd,"--schema")==0 || strcmp(zCmd,"-schema")==0)     && nArg>=4    ){      zSchema = azArg[2];      for(i=3; i<nArg; i++) azArg[i-2] = azArg[i];      nArg -= 2;      zCmd = azArg[1];    }        if( zCmd[0]=='-' && zCmd[1] ){      zCmd++;      if( zCmd[0]=='-' && zCmd[1] ) zCmd++;    }        if( strcmp(zCmd,"help")==0 ){      utf8_printf(p->out, "Available file-controls:\n");      for(i=0; i<ArraySize(aCtrl); i++){        utf8_printf(p->out, "  .filectrl %s %s\n",                    aCtrl[i].zCtrlName, aCtrl[i].zUsage);      }      rc = 1;      goto meta_command_exit;    }        n2 = strlen30(zCmd);    for(i=0; i<ArraySize(aCtrl); i++){      if( strncmp(zCmd, aCtrl[i].zCtrlName, n2)==0 ){        if( filectrl<0 ){          filectrl = aCtrl[i].ctrlCode;          iCtrl = i;        }else{          utf8_printf(stderr, "Error: ambiguous file-control: \"%s\"\n"                              "Use \".filectrl --help\" for help\n", zCmd);          rc = 1;          goto meta_command_exit;        }      }    }    if( filectrl<0 ){      utf8_printf(stderr,"Error: unknown file-control: %s\n"                         "Use \".filectrl --help\" for help\n", zCmd);    }else{      switch(filectrl){        case SQLITE_FCNTL_SIZE_LIMIT: {          if( nArg!=2 && nArg!=3 ) break;          iRes = nArg==3 ? integerValue(azArg[2]) : -1;          sqlite3_file_control(p->db, zSchema, SQLITE_FCNTL_SIZE_LIMIT, &iRes);          isOk = 1;          break;        }        case SQLITE_FCNTL_LOCK_TIMEOUT:        case SQLITE_FCNTL_CHUNK_SIZE: {          int x;          if( nArg!=3 ) break;          x = (int)integerValue(azArg[2]);          sqlite3_file_control(p->db, zSchema, filectrl, &x);          isOk = 2;          break;        }        case SQLITE_FCNTL_PERSIST_WAL:        case SQLITE_FCNTL_POWERSAFE_OVERWRITE: {          int x;          if( nArg!=2 && nArg!=3 ) break;          x = nArg==3 ? booleanValue(azArg[2]) : -1;          sqlite3_file_control(p->db, zSchema, filectrl, &x);          iRes = x;          isOk = 1;          break;        }        case SQLITE_FCNTL_DATA_VERSION:        case SQLITE_FCNTL_HAS_MOVED: {          int x;          if( nArg!=2 ) break;          sqlite3_file_control(p->db, zSchema, filectrl, &x);          iRes = x;          isOk = 1;          break;        }        case SQLITE_FCNTL_TEMPFILENAME: {          char *z = 0;          if( nArg!=2 ) break;          sqlite3_file_control(p->db, zSchema, filectrl, &z);          if( z ){            utf8_printf(p->out, "%s\n", z);            sqlite3_free(z);          }          isOk = 2;          break;        }        case SQLITE_FCNTL_RESERVE_BYTES: {          int x;          if( nArg>=3 ){            x = atoi(azArg[2]);            sqlite3_file_control(p->db, zSchema, filectrl, &x);          }          x = -1;          sqlite3_file_control(p->db, zSchema, filectrl, &x);          utf8_printf(p->out,"%d\n", x);          isOk = 2;          break;        }      }    }    if( isOk==0 && iCtrl>=0 ){      utf8_printf(p->out, "Usage: .filectrl %s %s\n", zCmd,aCtrl[iCtrl].zUsage);      rc = 1;    }else if( isOk==1 ){      char zBuf[100];      sqlite3_snprintf(sizeof(zBuf), zBuf, "%lld", iRes);      raw_printf(p->out, "%s\n", zBuf);    }  }else  if( c=='f' && strncmp(azArg[0], "fullschema", n)==0 ){    ShellState data;    int doStats = 0;    memcpy(&data, p, sizeof(data));    data.showHeader = 0;    data.cMode = data.mode = MODE_Semi;    if( nArg==2 && optionMatch(azArg[1], "indent") ){      data.cMode = data.mode = MODE_Pretty;      nArg = 1;    }    if( nArg!=1 ){      raw_printf(stderr, "Usage: .fullschema ?--indent?\n");      rc = 1;      goto meta_command_exit;    }    open_db(p, 0);    rc = sqlite3_exec(p->db,       "SELECT sql FROM"       "  (SELECT sql sql, type type, tbl_name tbl_name, name name, rowid x"       "     FROM sqlite_schema UNION ALL"       "   SELECT sql, type, tbl_name, name, rowid FROM sqlite_temp_schema) "       "WHERE type!='meta' AND sql NOTNULL AND name NOT LIKE 'sqlite_%' "       "ORDER BY x",       callback, &data, 0    );    if( rc==SQLITE_OK ){      sqlite3_stmt *pStmt;      rc = sqlite3_prepare_v2(p->db,               "SELECT rowid FROM sqlite_schema"               " WHERE name GLOB 'sqlite_stat[134]'",               -1, &pStmt, 0);      doStats = sqlite3_step(pStmt)==SQLITE_ROW;      sqlite3_finalize(pStmt);    }    if( doStats==0 ){      raw_printf(p->out, "/* No STAT tables available */\n");    }else{      raw_printf(p->out, "ANALYZE sqlite_schema;\n");      data.cMode = data.mode = MODE_Insert;      data.zDestTable = "sqlite_stat1";      shell_exec(&data, "SELECT * FROM sqlite_stat1", 0);      data.zDestTable = "sqlite_stat4";      shell_exec(&data, "SELECT * FROM sqlite_stat4", 0);      raw_printf(p->out, "ANALYZE sqlite_schema;\n");    }  }else  if( c=='h' && strncmp(azArg[0], "headers", n)==0 ){    if( nArg==2 ){      p->showHeader = booleanValue(azArg[1]);      p->shellFlgs |= SHFLG_HeaderSet;    }else{      raw_printf(stderr, "Usage: .headers on|off\n");      rc = 1;    }  }else  if( c=='h' && strncmp(azArg[0], "help", n)==0 ){    if( nArg>=2 ){      n = showHelp(p->out, azArg[1]);      if( n==0 ){        utf8_printf(p->out, "Nothing matches '%s'\n", azArg[1]);      }    }else{      showHelp(p->out, 0);    }  }else  if( c=='i' && strncmp(azArg[0], "import", n)==0 ){    char *zTable = 0;               char *zSchema = 0;              char *zFile = 0;                sqlite3_stmt *pStmt = NULL;     int nCol;                       int nByte;                      int i, j;                       int needCommit;                 int nSep;                       char *zSql;                     char *zFullTabName;             ImportCtx sCtx;                 char *(SQLITE_CDECL *xRead)(ImportCtx*);     int eVerbose = 0;               int nSkip = 0;                  int useOutputMode = 1;          char *zCreate = 0;              failIfSafeMode(p, "cannot run .import in safe mode");    memset(&sCtx, 0, sizeof(sCtx));    sCtx.z = sqlite3_malloc64(120);    if( sCtx.z==0 ){      import_cleanup(&sCtx);      shell_out_of_memory();    }    if( p->mode==MODE_Ascii ){      xRead = ascii_read_one_field;    }else{      xRead = csv_read_one_field;    }    for(i=1; i<nArg; i++){      char *z = azArg[i];      if( z[0]=='-' && z[1]=='-' ) z++;      if( z[0]!='-' ){        if( zFile==0 ){          zFile = z;        }else if( zTable==0 ){          zTable = z;        }else{          utf8_printf(p->out, "ERROR: extra argument: \"%s\".  Usage:\n", z);          showHelp(p->out, "import");          rc = 1;          goto meta_command_exit;        }      }else if( strcmp(z,"-v")==0 ){        eVerbose++;      }else if( strcmp(z,"-schema")==0 && i<nArg-1 ){        zSchema = azArg[++i];      }else if( strcmp(z,"-skip")==0 && i<nArg-1 ){        nSkip = integerValue(azArg[++i]);      }else if( strcmp(z,"-ascii")==0 ){        sCtx.cColSep = SEP_Unit[0];        sCtx.cRowSep = SEP_Record[0];        xRead = ascii_read_one_field;        useOutputMode = 0;      }else if( strcmp(z,"-csv")==0 ){        sCtx.cColSep = ',';        sCtx.cRowSep = '\n';        xRead = csv_read_one_field;        useOutputMode = 0;      }else{        utf8_printf(p->out, "ERROR: unknown option: \"%s\".  Usage:\n", z);        showHelp(p->out, "import");        rc = 1;        goto meta_command_exit;      }    }    if( zTable==0 ){      utf8_printf(p->out, "ERROR: missing %s argument. Usage:\n",                  zFile==0 ? "FILE" : "TABLE");      showHelp(p->out, "import");      rc = 1;      goto meta_command_exit;    }    seenInterrupt = 0;    open_db(p, 0);    if( useOutputMode ){            nSep = strlen30(p->colSeparator);      if( nSep==0 ){        raw_printf(stderr,                   "Error: non-null column separator required for import\n");        rc = 1;        goto meta_command_exit;      }      if( nSep>1 ){        raw_printf(stderr,               "Error: multi-character column separators not allowed"              " for import\n");        rc = 1;        goto meta_command_exit;      }      nSep = strlen30(p->rowSeparator);      if( nSep==0 ){        raw_printf(stderr,            "Error: non-null row separator required for import\n");        rc = 1;        goto meta_command_exit;      }      if( nSep==2 && p->mode==MODE_Csv && strcmp(p->rowSeparator,SEP_CrLf)==0 ){                sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);        nSep = strlen30(p->rowSeparator);      }      if( nSep>1 ){        raw_printf(stderr, "Error: multi-character row separators not allowed"                           " for import\n");        rc = 1;        goto meta_command_exit;      }      sCtx.cColSep = p->colSeparator[0];      sCtx.cRowSep = p->rowSeparator[0];    }    sCtx.zFile = zFile;    sCtx.nLine = 1;    if( sCtx.zFile[0]=='|' ){#ifdef SQLITE_OMIT_POPEN      raw_printf(stderr, "Error: pipes are not supported in this OS\n");      rc = 1;      goto meta_command_exit;#else      sCtx.in = popen(sCtx.zFile+1, "r");      sCtx.zFile = "<pipe>";      sCtx.xCloser = pclose;#endif    }else{      sCtx.in = fopen(sCtx.zFile, "rb");      sCtx.xCloser = fclose;    }    if( sCtx.in==0 ){      utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile);      rc = 1;      import_cleanup(&sCtx);      goto meta_command_exit;    }    if( eVerbose>=2 || (eVerbose>=1 && useOutputMode) ){      char zSep[2];      zSep[1] = 0;      zSep[0] = sCtx.cColSep;      utf8_printf(p->out, "Column separator ");      output_c_string(p->out, zSep);      utf8_printf(p->out, ", row separator ");      zSep[0] = sCtx.cRowSep;      output_c_string(p->out, zSep);      utf8_printf(p->out, "\n");    }        while( (nSkip--)>0 ){      while( xRead(&sCtx) && sCtx.cTerm==sCtx.cColSep ){}    }    if( zSchema!=0 ){      zFullTabName = sqlite3_mprintf("\"%w\".\"%w\"", zSchema, zTable);    }else{      zFullTabName = sqlite3_mprintf("\"%w\"", zTable);    }    zSql = sqlite3_mprintf("SELECT * FROM %s", zFullTabName);    if( zSql==0 || zFullTabName==0 ){      import_cleanup(&sCtx);      shell_out_of_memory();    }    nByte = strlen30(zSql);    rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);    import_append_char(&sCtx, 0);        if( rc && sqlite3_strglob("no such table: *", sqlite3_errmsg(p->db))==0 ){      sqlite3 *dbCols = 0;      char *zRenames = 0;      char *zColDefs;      zCreate = sqlite3_mprintf("CREATE TABLE %s", zFullTabName);      while( xRead(&sCtx) ){        zAutoColumn(sCtx.z, &dbCols, 0);        if( sCtx.cTerm!=sCtx.cColSep ) break;      }      zColDefs = zAutoColumn(0, &dbCols, &zRenames);      if( zRenames!=0 ){        utf8_printf((stdin_is_interactive && p->in==stdin)? p->out : stderr,                    "Columns renamed during .import %s due to duplicates:\n"                    "%s\n", sCtx.zFile, zRenames);        sqlite3_free(zRenames);      }      assert(dbCols==0);      if( zColDefs==0 ){        utf8_printf(stderr,"%s: empty file\n", sCtx.zFile);      import_fail:        sqlite3_free(zCreate);        sqlite3_free(zSql);        sqlite3_free(zFullTabName);        import_cleanup(&sCtx);        rc = 1;        goto meta_command_exit;      }      zCreate = sqlite3_mprintf("%z%z\n", zCreate, zColDefs);      if( eVerbose>=1 ){        utf8_printf(p->out, "%s\n", zCreate);      }      rc = sqlite3_exec(p->db, zCreate, 0, 0, 0);      if( rc ){        utf8_printf(stderr, "%s failed:\n%s\n", zCreate, sqlite3_errmsg(p->db));        goto import_fail;      }      sqlite3_free(zCreate);      zCreate = 0;      rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);    }    if( rc ){      if (pStmt) sqlite3_finalize(pStmt);      utf8_printf(stderr,"Error: %s\n", sqlite3_errmsg(p->db));      goto import_fail;    }    sqlite3_free(zSql);    nCol = sqlite3_column_count(pStmt);    sqlite3_finalize(pStmt);    pStmt = 0;    if( nCol==0 ) return 0;     zSql = sqlite3_malloc64( nByte*2 + 20 + nCol*2 );    if( zSql==0 ){      import_cleanup(&sCtx);      shell_out_of_memory();    }    sqlite3_snprintf(nByte+20, zSql, "INSERT INTO %s VALUES(?", zFullTabName);    j = strlen30(zSql);    for(i=1; i<nCol; i++){      zSql[j++] = ',';      zSql[j++] = '?';    }    zSql[j++] = ')';    zSql[j] = 0;    if( eVerbose>=2 ){      utf8_printf(p->out, "Insert using: %s\n", zSql);    }    rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);    if( rc ){      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));      if (pStmt) sqlite3_finalize(pStmt);      goto import_fail;    }    sqlite3_free(zSql);    sqlite3_free(zFullTabName);    needCommit = sqlite3_get_autocommit(p->db);    if( needCommit ) sqlite3_exec(p->db, "BEGIN", 0, 0, 0);    do{      int startLine = sCtx.nLine;      for(i=0; i<nCol; i++){        char *z = xRead(&sCtx);                if( z==0 && i==0 ) break;                if( p->mode==MODE_Ascii && (z==0 || z[0]==0) && i==0 ) break;        sqlite3_bind_text(pStmt, i+1, z, -1, SQLITE_TRANSIENT);        if( i<nCol-1 && sCtx.cTerm!=sCtx.cColSep ){          utf8_printf(stderr, "%s:%d: expected %d columns but found %d - "                          "filling the rest with NULL\n",                          sCtx.zFile, startLine, nCol, i+1);          i += 2;          while( i<=nCol ){ sqlite3_bind_null(pStmt, i); i++; }        }      }      if( sCtx.cTerm==sCtx.cColSep ){        do{          xRead(&sCtx);          i++;        }while( sCtx.cTerm==sCtx.cColSep );        utf8_printf(stderr, "%s:%d: expected %d columns but found %d - "                        "extras ignored\n",                        sCtx.zFile, startLine, nCol, i);      }      if( i>=nCol ){        sqlite3_step(pStmt);        rc = sqlite3_reset(pStmt);        if( rc!=SQLITE_OK ){          utf8_printf(stderr, "%s:%d: INSERT failed: %s\n", sCtx.zFile,                      startLine, sqlite3_errmsg(p->db));          sCtx.nErr++;        }else{          sCtx.nRow++;        }      }    }while( sCtx.cTerm!=EOF );    import_cleanup(&sCtx);    sqlite3_finalize(pStmt);    if( needCommit ) sqlite3_exec(p->db, "COMMIT", 0, 0, 0);    if( eVerbose>0 ){      utf8_printf(p->out,          "Added %d rows with %d errors using %d lines of input\n",          sCtx.nRow, sCtx.nErr, sCtx.nLine-1);    }  }else#ifndef SQLITE_UNTESTABLE  if( c=='i' && strncmp(azArg[0], "imposter", n)==0 ){    char *zSql;    char *zCollist = 0;    sqlite3_stmt *pStmt;    int tnum = 0;    int isWO = 0;      int lenPK = 0;     int i;    if( !(nArg==3 || (nArg==2 && sqlite3_stricmp(azArg[1],"off")==0)) ){      utf8_printf(stderr, "Usage: .imposter INDEX IMPOSTER\n"                          "       .imposter off\n");            rc = 1;      goto meta_command_exit;    }    open_db(p, 0);    if( nArg==2 ){      sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 1);      goto meta_command_exit;    }    zSql = sqlite3_mprintf(      "SELECT rootpage, 0 FROM sqlite_schema"      " WHERE name='%q' AND type='index'"      "UNION ALL "      "SELECT rootpage, 1 FROM sqlite_schema"      " WHERE name='%q' AND type='table'"      "   AND sql LIKE '%%without%%rowid%%'",      azArg[1], azArg[1]    );    sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);    sqlite3_free(zSql);    if( sqlite3_step(pStmt)==SQLITE_ROW ){      tnum = sqlite3_column_int(pStmt, 0);      isWO = sqlite3_column_int(pStmt, 1);    }    sqlite3_finalize(pStmt);    zSql = sqlite3_mprintf("PRAGMA index_xinfo='%q'", azArg[1]);    rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);    sqlite3_free(zSql);    i = 0;    while( rc==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){      char zLabel[20];      const char *zCol = (const char*)sqlite3_column_text(pStmt,2);      i++;      if( zCol==0 ){        if( sqlite3_column_int(pStmt,1)==-1 ){          zCol = "_ROWID_";        }else{          sqlite3_snprintf(sizeof(zLabel),zLabel,"expr%d",i);          zCol = zLabel;        }      }      if( isWO && lenPK==0 && sqlite3_column_int(pStmt,5)==0 && zCollist ){        lenPK = (int)strlen(zCollist);      }      if( zCollist==0 ){        zCollist = sqlite3_mprintf("\"%w\"", zCol);      }else{        zCollist = sqlite3_mprintf("%z,\"%w\"", zCollist, zCol);      }    }    sqlite3_finalize(pStmt);    if( i==0 || tnum==0 ){      utf8_printf(stderr, "no such index: \"%s\"\n", azArg[1]);      rc = 1;      sqlite3_free(zCollist);      goto meta_command_exit;    }    if( lenPK==0 ) lenPK = 100000;    zSql = sqlite3_mprintf(          "CREATE TABLE \"%w\"(%s,PRIMARY KEY(%.*s))WITHOUT ROWID",          azArg[2], zCollist, lenPK, zCollist);    sqlite3_free(zCollist);    rc = sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 1, tnum);    if( rc==SQLITE_OK ){      rc = sqlite3_exec(p->db, zSql, 0, 0, 0);      sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 0);      if( rc ){        utf8_printf(stderr, "Error in [%s]: %s\n", zSql, sqlite3_errmsg(p->db));      }else{        utf8_printf(stdout, "%s;\n", zSql);        raw_printf(stdout,          "WARNING: writing to an imposter table will corrupt the \"%s\" %s!\n",          azArg[1], isWO ? "table" : "index"        );      }    }else{      raw_printf(stderr, "SQLITE_TESTCTRL_IMPOSTER returns %d\n", rc);      rc = 1;    }    sqlite3_free(zSql);  }else#endif #ifdef SQLITE_ENABLE_IOTRACE  if( c=='i' && strncmp(azArg[0], "iotrace", n)==0 ){    SQLITE_API extern void (SQLITE_CDECL *sqlite3IoTrace)(const char*, ...);    if( iotrace && iotrace!=stdout ) fclose(iotrace);    iotrace = 0;    if( nArg<2 ){      sqlite3IoTrace = 0;    }else if( strcmp(azArg[1], "-")==0 ){      sqlite3IoTrace = iotracePrintf;      iotrace = stdout;    }else{      iotrace = fopen(azArg[1], "w");      if( iotrace==0 ){        utf8_printf(stderr, "Error: cannot open \"%s\"\n", azArg[1]);        sqlite3IoTrace = 0;        rc = 1;      }else{        sqlite3IoTrace = iotracePrintf;      }    }  }else#endif  if( c=='l' && n>=5 && strncmp(azArg[0], "limits", n)==0 ){    static const struct {       const char *zLimitName;          int limitCode;                } aLimit[] = {      { "length",                SQLITE_LIMIT_LENGTH                    },      { "sql_length",            SQLITE_LIMIT_SQL_LENGTH                },      { "column",                SQLITE_LIMIT_COLUMN                    },      { "expr_depth",            SQLITE_LIMIT_EXPR_DEPTH                },      { "compound_select",       SQLITE_LIMIT_COMPOUND_SELECT           },      { "vdbe_op",               SQLITE_LIMIT_VDBE_OP                   },      { "function_arg",          SQLITE_LIMIT_FUNCTION_ARG              },      { "attached",              SQLITE_LIMIT_ATTACHED                  },      { "like_pattern_length",   SQLITE_LIMIT_LIKE_PATTERN_LENGTH       },      { "variable_number",       SQLITE_LIMIT_VARIABLE_NUMBER           },      { "trigger_depth",         SQLITE_LIMIT_TRIGGER_DEPTH             },      { "worker_threads",        SQLITE_LIMIT_WORKER_THREADS            },    };    int i, n2;    open_db(p, 0);    if( nArg==1 ){      for(i=0; i<ArraySize(aLimit); i++){        printf("%20s %d\n", aLimit[i].zLimitName,               sqlite3_limit(p->db, aLimit[i].limitCode, -1));      }    }else if( nArg>3 ){      raw_printf(stderr, "Usage: .limit NAME ?NEW-VALUE?\n");      rc = 1;      goto meta_command_exit;    }else{      int iLimit = -1;      n2 = strlen30(azArg[1]);      for(i=0; i<ArraySize(aLimit); i++){        if( sqlite3_strnicmp(aLimit[i].zLimitName, azArg[1], n2)==0 ){          if( iLimit<0 ){            iLimit = i;          }else{            utf8_printf(stderr, "ambiguous limit: \"%s\"\n", azArg[1]);            rc = 1;            goto meta_command_exit;          }        }      }      if( iLimit<0 ){        utf8_printf(stderr, "unknown limit: \"%s\"\n"                        "enter \".limits\" with no arguments for a list.\n",                         azArg[1]);        rc = 1;        goto meta_command_exit;      }      if( nArg==3 ){        sqlite3_limit(p->db, aLimit[iLimit].limitCode,                      (int)integerValue(azArg[2]));      }      printf("%20s %d\n", aLimit[iLimit].zLimitName,             sqlite3_limit(p->db, aLimit[iLimit].limitCode, -1));    }  }else  if( c=='l' && n>2 && strncmp(azArg[0], "lint", n)==0 ){    open_db(p, 0);    lintDotCommand(p, azArg, nArg);  }else#ifndef SQLITE_OMIT_LOAD_EXTENSION  if( c=='l' && strncmp(azArg[0], "load", n)==0 ){    const char *zFile, *zProc;    char *zErrMsg = 0;    failIfSafeMode(p, "cannot run .load in safe mode");    if( nArg<2 ){      raw_printf(stderr, "Usage: .load FILE ?ENTRYPOINT?\n");      rc = 1;      goto meta_command_exit;    }    zFile = azArg[1];    zProc = nArg>=3 ? azArg[2] : 0;    open_db(p, 0);    rc = sqlite3_load_extension(p->db, zFile, zProc, &zErrMsg);    if( rc!=SQLITE_OK ){      utf8_printf(stderr, "Error: %s\n", zErrMsg);      sqlite3_free(zErrMsg);      rc = 1;    }  }else#endif  if( c=='l' && strncmp(azArg[0], "log", n)==0 ){    failIfSafeMode(p, "cannot run .log in safe mode");    if( nArg!=2 ){      raw_printf(stderr, "Usage: .log FILENAME\n");      rc = 1;    }else{      const char *zFile = azArg[1];      output_file_close(p->pLog);      p->pLog = output_file_open(zFile, 0);    }  }else  if( c=='m' && strncmp(azArg[0], "mode", n)==0 ){    const char *zMode = 0;    const char *zTabname = 0;    int i, n2;    ColModeOpts cmOpts = ColModeOpts_default;    for(i=1; i<nArg; i++){      const char *z = azArg[i];      if( optionMatch(z,"wrap") && i+1<nArg ){        cmOpts.iWrap = integerValue(azArg[++i]);      }else if( optionMatch(z,"ww") ){        cmOpts.bWordWrap = 1;      }else if( optionMatch(z,"wordwrap") && i+1<nArg ){        cmOpts.bWordWrap = (u8)booleanValue(azArg[++i]);      }else if( optionMatch(z,"quote") ){        cmOpts.bQuote = 1;      }else if( optionMatch(z,"noquote") ){        cmOpts.bQuote = 0;      }else if( zMode==0 ){        zMode = z;                if( strcmp(z, "qbox")==0 ){          ColModeOpts cmo = ColModeOpts_default_qbox;          zMode = "box";          cmOpts = cmo;        }      }else if( zTabname==0 ){        zTabname = z;      }else if( z[0]=='-' ){        utf8_printf(stderr, "unknown option: %s\n", z);        utf8_printf(stderr, "options:\n"                            "  --noquote\n"                            "  --quote\n"                            "  --wordwrap on/off\n"                            "  --wrap N\n"                            "  --ww\n");        rc = 1;        goto meta_command_exit;      }else{        utf8_printf(stderr, "extra argument: \"%s\"\n", z);        rc = 1;        goto meta_command_exit;      }    }    if( zMode==0 ){      if( p->mode==MODE_Column       || (p->mode>=MODE_Markdown && p->mode<=MODE_Box)      ){        raw_printf          (p->out,           "current output mode: %s --wrap %d --wordwrap %s --%squote\n",           modeDescr[p->mode], p->cmOpts.iWrap,           p->cmOpts.bWordWrap ? "on" : "off",           p->cmOpts.bQuote ? "" : "no");      }else{        raw_printf(p->out, "current output mode: %s\n", modeDescr[p->mode]);      }      zMode = modeDescr[p->mode];    }    n2 = strlen30(zMode);    if( strncmp(zMode,"lines",n2)==0 ){      p->mode = MODE_Line;      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);    }else if( strncmp(zMode,"columns",n2)==0 ){      p->mode = MODE_Column;      if( (p->shellFlgs & SHFLG_HeaderSet)==0 ){        p->showHeader = 1;      }      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);      p->cmOpts = cmOpts;    }else if( strncmp(zMode,"list",n2)==0 ){      p->mode = MODE_List;      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Column);      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);    }else if( strncmp(zMode,"html",n2)==0 ){      p->mode = MODE_Html;    }else if( strncmp(zMode,"tcl",n2)==0 ){      p->mode = MODE_Tcl;      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Space);      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);    }else if( strncmp(zMode,"csv",n2)==0 ){      p->mode = MODE_Csv;      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf);    }else if( strncmp(zMode,"tabs",n2)==0 ){      p->mode = MODE_List;      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Tab);    }else if( strncmp(zMode,"insert",n2)==0 ){      p->mode = MODE_Insert;      set_table_name(p, zTabname ? zTabname : "table");    }else if( strncmp(zMode,"quote",n2)==0 ){      p->mode = MODE_Quote;      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);    }else if( strncmp(zMode,"ascii",n2)==0 ){      p->mode = MODE_Ascii;      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Unit);      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Record);    }else if( strncmp(zMode,"markdown",n2)==0 ){      p->mode = MODE_Markdown;      p->cmOpts = cmOpts;    }else if( strncmp(zMode,"table",n2)==0 ){      p->mode = MODE_Table;      p->cmOpts = cmOpts;    }else if( strncmp(zMode,"box",n2)==0 ){      p->mode = MODE_Box;      p->cmOpts = cmOpts;    }else if( strncmp(zMode,"count",n2)==0 ){      p->mode = MODE_Count;    }else if( strncmp(zMode,"off",n2)==0 ){      p->mode = MODE_Off;    }else if( strncmp(zMode,"json",n2)==0 ){      p->mode = MODE_Json;    }else{      raw_printf(stderr, "Error: mode should be one of: "         "ascii box column csv html insert json line list markdown "         "qbox quote table tabs tcl\n");      rc = 1;    }    p->cMode = p->mode;  }else  if( c=='n' && strcmp(azArg[0], "nonce")==0 ){    if( nArg!=2 ){      raw_printf(stderr, "Usage: .nonce NONCE\n");      rc = 1;    }else if( p->zNonce==0 || strcmp(azArg[1],p->zNonce)!=0 ){      raw_printf(stderr, "line %d: incorrect nonce: \"%s\"\n",                 p->lineno, azArg[1]);      exit(1);    }else{      p->bSafeMode = 0;      return 0;      }  }else  if( c=='n' && strncmp(azArg[0], "nullvalue", n)==0 ){    if( nArg==2 ){      sqlite3_snprintf(sizeof(p->nullValue), p->nullValue,                       "%.*s", (int)ArraySize(p->nullValue)-1, azArg[1]);    }else{      raw_printf(stderr, "Usage: .nullvalue STRING\n");      rc = 1;    }  }else  if( c=='o' && strncmp(azArg[0], "open", n)==0 && n>=2 ){    const char *zFN = 0;         char *zNewFilename = 0;      int iName = 1;               int newFlag = 0;             int openMode = SHELL_OPEN_UNSPEC;        for(iName=1; iName<nArg; iName++){      const char *z = azArg[iName];      if( optionMatch(z,"new") ){        newFlag = 1;#ifdef SQLITE_HAVE_ZLIB      }else if( optionMatch(z, "zip") ){        openMode = SHELL_OPEN_ZIPFILE;#endif      }else if( optionMatch(z, "append") ){        openMode = SHELL_OPEN_APPENDVFS;      }else if( optionMatch(z, "readonly") ){        openMode = SHELL_OPEN_READONLY;      }else if( optionMatch(z, "nofollow") ){        p->openFlags |= SQLITE_OPEN_NOFOLLOW;#ifndef SQLITE_OMIT_DESERIALIZE      }else if( optionMatch(z, "deserialize") ){        openMode = SHELL_OPEN_DESERIALIZE;      }else if( optionMatch(z, "hexdb") ){        openMode = SHELL_OPEN_HEXDB;      }else if( optionMatch(z, "maxsize") && iName+1<nArg ){        p->szMax = integerValue(azArg[++iName]);#endif       }else if( z[0]=='-' ){        utf8_printf(stderr, "unknown option: %s\n", z);        rc = 1;        goto meta_command_exit;      }else if( zFN ){        utf8_printf(stderr, "extra argument: \"%s\"\n", z);        rc = 1;        goto meta_command_exit;      }else{        zFN = z;      }    }        session_close_all(p, -1);    close_db(p->db);    p->db = 0;    p->pAuxDb->zDbFilename = 0;    sqlite3_free(p->pAuxDb->zFreeOnClose);    p->pAuxDb->zFreeOnClose = 0;    p->openMode = openMode;    p->openFlags = 0;    p->szMax = 0;        if( zFN || p->openMode==SHELL_OPEN_HEXDB ){      if( newFlag && zFN && !p->bSafeMode ) shellDeleteFile(zFN);      if( p->bSafeMode       && p->openMode!=SHELL_OPEN_HEXDB       && zFN       && strcmp(zFN,":memory:")!=0      ){        failIfSafeMode(p, "cannot open disk-based database files in safe mode");      }      if( zFN ){        zNewFilename = sqlite3_mprintf("%s", zFN);        shell_check_oom(zNewFilename);      }else{        zNewFilename = 0;      }      p->pAuxDb->zDbFilename = zNewFilename;      open_db(p, OPEN_DB_KEEPALIVE);      if( p->db==0 ){        utf8_printf(stderr, "Error: cannot open '%s'\n", zNewFilename);        sqlite3_free(zNewFilename);      }else{        p->pAuxDb->zFreeOnClose = zNewFilename;      }    }    if( p->db==0 ){            p->pAuxDb->zDbFilename = 0;      open_db(p, 0);    }  }else  if( (c=='o'        && (strncmp(azArg[0], "output", n)==0||strncmp(azArg[0], "once", n)==0))   || (c=='e' && n==5 && strcmp(azArg[0],"excel")==0)  ){    char *zFile = 0;    int bTxtMode = 0;    int i;    int eMode = 0;    int bBOM = 0;    int bOnce = 0;      failIfSafeMode(p, "cannot run .%s in safe mode", azArg[0]);    if( c=='e' ){      eMode = 'x';      bOnce = 2;    }else if( strncmp(azArg[0],"once",n)==0 ){      bOnce = 1;    }    for(i=1; i<nArg; i++){      char *z = azArg[i];      if( z[0]=='-' ){        if( z[1]=='-' ) z++;        if( strcmp(z,"-bom")==0 ){          bBOM = 1;        }else if( c!='e' && strcmp(z,"-x")==0 ){          eMode = 'x';          }else if( c!='e' && strcmp(z,"-e")==0 ){          eMode = 'e';          }else{          utf8_printf(p->out, "ERROR: unknown option: \"%s\".  Usage:\n",                      azArg[i]);          showHelp(p->out, azArg[0]);          rc = 1;          goto meta_command_exit;        }      }else if( zFile==0 && eMode!='e' && eMode!='x' ){        zFile = sqlite3_mprintf("%s", z);        if( zFile && zFile[0]=='|' ){          while( i+1<nArg ) zFile = sqlite3_mprintf("%z %s", zFile, azArg[++i]);          break;        }      }else{        utf8_printf(p->out,"ERROR: extra parameter: \"%s\".  Usage:\n",                    azArg[i]);        showHelp(p->out, azArg[0]);        rc = 1;        sqlite3_free(zFile);        goto meta_command_exit;      }    }    if( zFile==0 ){      zFile = sqlite3_mprintf("stdout");    }    if( bOnce ){      p->outCount = 2;    }else{      p->outCount = 0;    }    output_reset(p);#ifndef SQLITE_NOHAVE_SYSTEM    if( eMode=='e' || eMode=='x' ){      p->doXdgOpen = 1;      outputModePush(p);      if( eMode=='x' ){                newTempFile(p, "csv");        ShellClearFlag(p, SHFLG_Echo);        p->mode = MODE_Csv;        sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);        sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf);      }else{                newTempFile(p, "txt");        bTxtMode = 1;      }      sqlite3_free(zFile);      zFile = sqlite3_mprintf("%s", p->zTempFile);    }#endif     shell_check_oom(zFile);    if( zFile[0]=='|' ){#ifdef SQLITE_OMIT_POPEN      raw_printf(stderr, "Error: pipes are not supported in this OS\n");      rc = 1;      p->out = stdout;#else      p->out = popen(zFile + 1, "w");      if( p->out==0 ){        utf8_printf(stderr,"Error: cannot open pipe \"%s\"\n", zFile + 1);        p->out = stdout;        rc = 1;      }else{        if( bBOM ) fprintf(p->out,"\357\273\277");        sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile);      }#endif    }else{      p->out = output_file_open(zFile, bTxtMode);      if( p->out==0 ){        if( strcmp(zFile,"off")!=0 ){          utf8_printf(stderr,"Error: cannot write to \"%s\"\n", zFile);        }        p->out = stdout;        rc = 1;      } else {        if( bBOM ) fprintf(p->out,"\357\273\277");        sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile);      }    }    sqlite3_free(zFile);  }else  if( c=='p' && n>=3 && strncmp(azArg[0], "parameter", n)==0 ){    open_db(p,0);    if( nArg<=1 ) goto parameter_syntax_error;        if( nArg==2 && strcmp(azArg[1],"clear")==0 ){      sqlite3_exec(p->db, "DROP TABLE IF EXISTS temp.sqlite_parameters;",                   0, 0, 0);    }else        if( nArg==2 && strcmp(azArg[1],"list")==0 ){      sqlite3_stmt *pStmt = 0;      int rx;      int len = 0;      rx = sqlite3_prepare_v2(p->db,             "SELECT max(length(key)) "             "FROM temp.sqlite_parameters;", -1, &pStmt, 0);      if( rx==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){        len = sqlite3_column_int(pStmt, 0);        if( len>40 ) len = 40;      }      sqlite3_finalize(pStmt);      pStmt = 0;      if( len ){        rx = sqlite3_prepare_v2(p->db,             "SELECT key, quote(value) "             "FROM temp.sqlite_parameters;", -1, &pStmt, 0);        while( rx==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){          utf8_printf(p->out, "%-*s %s\n", len, sqlite3_column_text(pStmt,0),                      sqlite3_column_text(pStmt,1));        }        sqlite3_finalize(pStmt);      }    }else        if( nArg==2 && strcmp(azArg[1],"init")==0 ){      bind_table_init(p);    }else        if( nArg==4 && strcmp(azArg[1],"set")==0 ){      int rx;      char *zSql;      sqlite3_stmt *pStmt;      const char *zKey = azArg[2];      const char *zValue = azArg[3];      bind_table_init(p);      zSql = sqlite3_mprintf(                  "REPLACE INTO temp.sqlite_parameters(key,value)"                  "VALUES(%Q,%s);", zKey, zValue);      shell_check_oom(zSql);      pStmt = 0;      rx = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);      sqlite3_free(zSql);      if( rx!=SQLITE_OK ){        sqlite3_finalize(pStmt);        pStmt = 0;        zSql = sqlite3_mprintf(                   "REPLACE INTO temp.sqlite_parameters(key,value)"                   "VALUES(%Q,%Q);", zKey, zValue);        shell_check_oom(zSql);        rx = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);        sqlite3_free(zSql);        if( rx!=SQLITE_OK ){          utf8_printf(p->out, "Error: %s\n", sqlite3_errmsg(p->db));          sqlite3_finalize(pStmt);          pStmt = 0;          rc = 1;        }      }      sqlite3_step(pStmt);      sqlite3_finalize(pStmt);    }else        if( nArg==3 && strcmp(azArg[1],"unset")==0 ){      char *zSql = sqlite3_mprintf(          "DELETE FROM temp.sqlite_parameters WHERE key=%Q", azArg[2]);      shell_check_oom(zSql);      sqlite3_exec(p->db, zSql, 0, 0, 0);      sqlite3_free(zSql);    }else        parameter_syntax_error:    showHelp(p->out, "parameter");  }else  if( c=='p' && n>=3 && strncmp(azArg[0], "print", n)==0 ){    int i;    for(i=1; i<nArg; i++){      if( i>1 ) raw_printf(p->out, " ");      utf8_printf(p->out, "%s", azArg[i]);    }    raw_printf(p->out, "\n");  }else#ifndef SQLITE_OMIT_PROGRESS_CALLBACK  if( c=='p' && n>=3 && strncmp(azArg[0], "progress", n)==0 ){    int i;    int nn = 0;    p->flgProgress = 0;    p->mxProgress = 0;    p->nProgress = 0;    for(i=1; i<nArg; i++){      const char *z = azArg[i];      if( z[0]=='-' ){        z++;        if( z[0]=='-' ) z++;        if( strcmp(z,"quiet")==0 || strcmp(z,"q")==0 ){          p->flgProgress |= SHELL_PROGRESS_QUIET;          continue;        }        if( strcmp(z,"reset")==0 ){          p->flgProgress |= SHELL_PROGRESS_RESET;          continue;        }        if( strcmp(z,"once")==0 ){          p->flgProgress |= SHELL_PROGRESS_ONCE;          continue;        }        if( strcmp(z,"limit")==0 ){          if( i+1>=nArg ){            utf8_printf(stderr, "Error: missing argument on --limit\n");            rc = 1;            goto meta_command_exit;          }else{            p->mxProgress = (int)integerValue(azArg[++i]);          }          continue;        }        utf8_printf(stderr, "Error: unknown option: \"%s\"\n", azArg[i]);        rc = 1;        goto meta_command_exit;      }else{        nn = (int)integerValue(z);      }    }    open_db(p, 0);    sqlite3_progress_handler(p->db, nn, progress_handler, p);  }else#endif   if( c=='p' && strncmp(azArg[0], "prompt", n)==0 ){    if( nArg >= 2) {      strncpy(mainPrompt,azArg[1],(int)ArraySize(mainPrompt)-1);    }    if( nArg >= 3) {      strncpy(continuePrompt,azArg[2],(int)ArraySize(continuePrompt)-1);    }  }else  if( c=='q' && strncmp(azArg[0], "quit", n)==0 ){    rc = 2;  }else  if( c=='r' && n>=3 && strncmp(azArg[0], "read", n)==0 ){    FILE *inSaved = p->in;    int savedLineno = p->lineno;    failIfSafeMode(p, "cannot run .read in safe mode");    if( nArg!=2 ){      raw_printf(stderr, "Usage: .read FILE\n");      rc = 1;      goto meta_command_exit;    }    if( azArg[1][0]=='|' ){#ifdef SQLITE_OMIT_POPEN      raw_printf(stderr, "Error: pipes are not supported in this OS\n");      rc = 1;      p->out = stdout;#else      p->in = popen(azArg[1]+1, "r");      if( p->in==0 ){        utf8_printf(stderr, "Error: cannot open \"%s\"\n", azArg[1]);        rc = 1;      }else{        rc = process_input(p);        pclose(p->in);      }#endif    }else if( (p->in = openChrSource(azArg[1]))==0 ){      utf8_printf(stderr,"Error: cannot open \"%s\"\n", azArg[1]);      rc = 1;    }else{      rc = process_input(p);      fclose(p->in);    }    p->in = inSaved;    p->lineno = savedLineno;  }else  if( c=='r' && n>=3 && strncmp(azArg[0], "restore", n)==0 ){    const char *zSrcFile;    const char *zDb;    sqlite3 *pSrc;    sqlite3_backup *pBackup;    int nTimeout = 0;    failIfSafeMode(p, "cannot run .restore in safe mode");    if( nArg==2 ){      zSrcFile = azArg[1];      zDb = "main";    }else if( nArg==3 ){      zSrcFile = azArg[2];      zDb = azArg[1];    }else{      raw_printf(stderr, "Usage: .restore ?DB? FILE\n");      rc = 1;      goto meta_command_exit;    }    rc = sqlite3_open(zSrcFile, &pSrc);    if( rc!=SQLITE_OK ){      utf8_printf(stderr, "Error: cannot open \"%s\"\n", zSrcFile);      close_db(pSrc);      return 1;    }    open_db(p, 0);    pBackup = sqlite3_backup_init(p->db, zDb, pSrc, "main");    if( pBackup==0 ){      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));      close_db(pSrc);      return 1;    }    while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK          || rc==SQLITE_BUSY  ){      if( rc==SQLITE_BUSY ){        if( nTimeout++ >= 3 ) break;        sqlite3_sleep(100);      }    }    sqlite3_backup_finish(pBackup);    if( rc==SQLITE_DONE ){      rc = 0;    }else if( rc==SQLITE_BUSY || rc==SQLITE_LOCKED ){      raw_printf(stderr, "Error: source database is busy\n");      rc = 1;    }else{      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));      rc = 1;    }    close_db(pSrc);  }else  if( c=='s' && strncmp(azArg[0], "scanstats", n)==0 ){    if( nArg==2 ){      p->scanstatsOn = (u8)booleanValue(azArg[1]);#ifndef SQLITE_ENABLE_STMT_SCANSTATUS      raw_printf(stderr, "Warning: .scanstats not available in this build.\n");#endif    }else{      raw_printf(stderr, "Usage: .scanstats on|off\n");      rc = 1;    }  }else  if( c=='s' && strncmp(azArg[0], "schema", n)==0 ){    ShellText sSelect;    ShellState data;    char *zErrMsg = 0;    const char *zDiv = "(";    const char *zName = 0;    int iSchema = 0;    int bDebug = 0;    int bNoSystemTabs = 0;    int ii;    open_db(p, 0);    memcpy(&data, p, sizeof(data));    data.showHeader = 0;    data.cMode = data.mode = MODE_Semi;    initText(&sSelect);    for(ii=1; ii<nArg; ii++){      if( optionMatch(azArg[ii],"indent") ){        data.cMode = data.mode = MODE_Pretty;      }else if( optionMatch(azArg[ii],"debug") ){        bDebug = 1;      }else if( optionMatch(azArg[ii],"nosys") ){        bNoSystemTabs = 1;      }else if( azArg[ii][0]=='-' ){        utf8_printf(stderr, "Unknown option: \"%s\"\n", azArg[ii]);        rc = 1;        goto meta_command_exit;      }else if( zName==0 ){        zName = azArg[ii];      }else{        raw_printf(stderr, "Usage: .schema ?--indent? ?--nosys? ?LIKE-PATTERN?\n");        rc = 1;        goto meta_command_exit;      }    }    if( zName!=0 ){      int isSchema = sqlite3_strlike(zName, "sqlite_master", '\\')==0                  || sqlite3_strlike(zName, "sqlite_schema", '\\')==0                  || sqlite3_strlike(zName,"sqlite_temp_master", '\\')==0                  || sqlite3_strlike(zName,"sqlite_temp_schema", '\\')==0;      if( isSchema ){        char *new_argv[2], *new_colv[2];        new_argv[0] = sqlite3_mprintf(                      "CREATE TABLE %s (\n"                      "  type text,\n"                      "  name text,\n"                      "  tbl_name text,\n"                      "  rootpage integer,\n"                      "  sql text\n"                      ")", zName);        shell_check_oom(new_argv[0]);        new_argv[1] = 0;        new_colv[0] = "sql";        new_colv[1] = 0;        callback(&data, 1, new_argv, new_colv);        sqlite3_free(new_argv[0]);      }    }    if( zDiv ){      sqlite3_stmt *pStmt = 0;      rc = sqlite3_prepare_v2(p->db, "SELECT name FROM pragma_database_list",                              -1, &pStmt, 0);      if( rc ){        utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));        sqlite3_finalize(pStmt);        rc = 1;        goto meta_command_exit;      }      appendText(&sSelect, "SELECT sql FROM", 0);      iSchema = 0;      while( sqlite3_step(pStmt)==SQLITE_ROW ){        const char *zDb = (const char*)sqlite3_column_text(pStmt, 0);        char zScNum[30];        sqlite3_snprintf(sizeof(zScNum), zScNum, "%d", ++iSchema);        appendText(&sSelect, zDiv, 0);        zDiv = " UNION ALL ";        appendText(&sSelect, "SELECT shell_add_schema(sql,", 0);        if( sqlite3_stricmp(zDb, "main")!=0 ){          appendText(&sSelect, zDb, '\'');        }else{          appendText(&sSelect, "NULL", 0);        }        appendText(&sSelect, ",name) AS sql, type, tbl_name, name, rowid,", 0);        appendText(&sSelect, zScNum, 0);        appendText(&sSelect, " AS snum, ", 0);        appendText(&sSelect, zDb, '\'');        appendText(&sSelect, " AS sname FROM ", 0);        appendText(&sSelect, zDb, quoteChar(zDb));        appendText(&sSelect, ".sqlite_schema", 0);      }      sqlite3_finalize(pStmt);#ifndef SQLITE_OMIT_INTROSPECTION_PRAGMAS      if( zName ){        appendText(&sSelect,           " UNION ALL SELECT shell_module_schema(name),"           " 'table', name, name, name, 9e+99, 'main' FROM pragma_module_list",        0);      }#endif      appendText(&sSelect, ") WHERE ", 0);      if( zName ){        char *zQarg = sqlite3_mprintf("%Q", zName);        int bGlob;        shell_check_oom(zQarg);        bGlob = strchr(zName, '*') != 0 || strchr(zName, '?') != 0 ||                strchr(zName, '[') != 0;        if( strchr(zName, '.') ){          appendText(&sSelect, "lower(printf('%s.%s',sname,tbl_name))", 0);        }else{          appendText(&sSelect, "lower(tbl_name)", 0);        }        appendText(&sSelect, bGlob ? " GLOB " : " LIKE ", 0);        appendText(&sSelect, zQarg, 0);        if( !bGlob ){          appendText(&sSelect, " ESCAPE '\\' ", 0);        }        appendText(&sSelect, " AND ", 0);        sqlite3_free(zQarg);      }      if( bNoSystemTabs ){        appendText(&sSelect, "name NOT LIKE 'sqlite_%%' AND ", 0);      }      appendText(&sSelect, "sql IS NOT NULL"                           " ORDER BY snum, rowid", 0);      if( bDebug ){        utf8_printf(p->out, "SQL: %s;\n", sSelect.z);      }else{        rc = sqlite3_exec(p->db, sSelect.z, callback, &data, &zErrMsg);      }      freeText(&sSelect);    }    if( zErrMsg ){      utf8_printf(stderr,"Error: %s\n", zErrMsg);      sqlite3_free(zErrMsg);      rc = 1;    }else if( rc != SQLITE_OK ){      raw_printf(stderr,"Error: querying schema information\n");      rc = 1;    }else{      rc = 0;    }  }else  if( c=='s' && n==11 && strncmp(azArg[0], "selecttrace", n)==0 ){    unsigned int x = nArg>=2 ? (unsigned int)integerValue(azArg[1]) : 0xffffffff;    sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 1, &x);  }else#if defined(SQLITE_ENABLE_SESSION)  if( c=='s' && strncmp(azArg[0],"session",n)==0 && n>=3 ){    struct AuxDb *pAuxDb = p->pAuxDb;    OpenSession *pSession = &pAuxDb->aSession[0];    char **azCmd = &azArg[1];    int iSes = 0;    int nCmd = nArg - 1;    int i;    if( nArg<=1 ) goto session_syntax_error;    open_db(p, 0);    if( nArg>=3 ){      for(iSes=0; iSes<pAuxDb->nSession; iSes++){        if( strcmp(pAuxDb->aSession[iSes].zName, azArg[1])==0 ) break;      }      if( iSes<pAuxDb->nSession ){        pSession = &pAuxDb->aSession[iSes];        azCmd++;        nCmd--;      }else{        pSession = &pAuxDb->aSession[0];        iSes = 0;      }    }        if( strcmp(azCmd[0],"attach")==0 ){      if( nCmd!=2 ) goto session_syntax_error;      if( pSession->p==0 ){        session_not_open:        raw_printf(stderr, "ERROR: No sessions are open\n");      }else{        rc = sqlite3session_attach(pSession->p, azCmd[1]);        if( rc ){          raw_printf(stderr, "ERROR: sqlite3session_attach() returns %d\n", rc);          rc = 0;        }      }    }else        if( strcmp(azCmd[0],"changeset")==0 || strcmp(azCmd[0],"patchset")==0 ){      FILE *out = 0;      failIfSafeMode(p, "cannot run \".session %s\" in safe mode", azCmd[0]);      if( nCmd!=2 ) goto session_syntax_error;      if( pSession->p==0 ) goto session_not_open;      out = fopen(azCmd[1], "wb");      if( out==0 ){        utf8_printf(stderr, "ERROR: cannot open \"%s\" for writing\n",                    azCmd[1]);      }else{        int szChng;        void *pChng;        if( azCmd[0][0]=='c' ){          rc = sqlite3session_changeset(pSession->p, &szChng, &pChng);        }else{          rc = sqlite3session_patchset(pSession->p, &szChng, &pChng);        }        if( rc ){          printf("Error: error code %d\n", rc);          rc = 0;        }        if( pChng          && fwrite(pChng, szChng, 1, out)!=1 ){          raw_printf(stderr, "ERROR: Failed to write entire %d-byte output\n",                  szChng);        }        sqlite3_free(pChng);        fclose(out);      }    }else        if( strcmp(azCmd[0], "close")==0 ){      if( nCmd!=1 ) goto session_syntax_error;      if( pAuxDb->nSession ){        session_close(pSession);        pAuxDb->aSession[iSes] = pAuxDb->aSession[--pAuxDb->nSession];      }    }else        if( strcmp(azCmd[0], "enable")==0 ){      int ii;      if( nCmd>2 ) goto session_syntax_error;      ii = nCmd==1 ? -1 : booleanValue(azCmd[1]);      if( pAuxDb->nSession ){        ii = sqlite3session_enable(pSession->p, ii);        utf8_printf(p->out, "session %s enable flag = %d\n",                    pSession->zName, ii);      }    }else        if( strcmp(azCmd[0], "filter")==0 ){      int ii, nByte;      if( nCmd<2 ) goto session_syntax_error;      if( pAuxDb->nSession ){        for(ii=0; ii<pSession->nFilter; ii++){          sqlite3_free(pSession->azFilter[ii]);        }        sqlite3_free(pSession->azFilter);        nByte = sizeof(pSession->azFilter[0])*(nCmd-1);        pSession->azFilter = sqlite3_malloc( nByte );        if( pSession->azFilter==0 ){          raw_printf(stderr, "Error: out or memory\n");          exit(1);        }        for(ii=1; ii<nCmd; ii++){          char *x = pSession->azFilter[ii-1] = sqlite3_mprintf("%s", azCmd[ii]);          shell_check_oom(x);        }        pSession->nFilter = ii-1;      }    }else        if( strcmp(azCmd[0], "indirect")==0 ){      int ii;      if( nCmd>2 ) goto session_syntax_error;      ii = nCmd==1 ? -1 : booleanValue(azCmd[1]);      if( pAuxDb->nSession ){        ii = sqlite3session_indirect(pSession->p, ii);        utf8_printf(p->out, "session %s indirect flag = %d\n",                    pSession->zName, ii);      }    }else        if( strcmp(azCmd[0], "isempty")==0 ){      int ii;      if( nCmd!=1 ) goto session_syntax_error;      if( pAuxDb->nSession ){        ii = sqlite3session_isempty(pSession->p);        utf8_printf(p->out, "session %s isempty flag = %d\n",                    pSession->zName, ii);      }    }else        if( strcmp(azCmd[0],"list")==0 ){      for(i=0; i<pAuxDb->nSession; i++){        utf8_printf(p->out, "%d %s\n", i, pAuxDb->aSession[i].zName);      }    }else        if( strcmp(azCmd[0],"open")==0 ){      char *zName;      if( nCmd!=3 ) goto session_syntax_error;      zName = azCmd[2];      if( zName[0]==0 ) goto session_syntax_error;      for(i=0; i<pAuxDb->nSession; i++){        if( strcmp(pAuxDb->aSession[i].zName,zName)==0 ){          utf8_printf(stderr, "Session \"%s\" already exists\n", zName);          goto meta_command_exit;        }      }      if( pAuxDb->nSession>=ArraySize(pAuxDb->aSession) ){        raw_printf(stderr, "Maximum of %d sessions\n", ArraySize(pAuxDb->aSession));        goto meta_command_exit;      }      pSession = &pAuxDb->aSession[pAuxDb->nSession];      rc = sqlite3session_create(p->db, azCmd[1], &pSession->p);      if( rc ){        raw_printf(stderr, "Cannot open session: error code=%d\n", rc);        rc = 0;        goto meta_command_exit;      }      pSession->nFilter = 0;      sqlite3session_table_filter(pSession->p, session_filter, pSession);      pAuxDb->nSession++;      pSession->zName = sqlite3_mprintf("%s", zName);      shell_check_oom(pSession->zName);    }else        session_syntax_error:    showHelp(p->out, "session");  }else#endif#ifdef SQLITE_DEBUG    if( c=='s' && n>=10 && strncmp(azArg[0], "selftest-", 9)==0 ){    if( strncmp(azArg[0]+9, "boolean", n-9)==0 ){      int i, v;      for(i=1; i<nArg; i++){        v = booleanValue(azArg[i]);        utf8_printf(p->out, "%s: %d 0x%x\n", azArg[i], v, v);      }    }    if( strncmp(azArg[0]+9, "integer", n-9)==0 ){      int i; sqlite3_int64 v;      for(i=1; i<nArg; i++){        char zBuf[200];        v = integerValue(azArg[i]);        sqlite3_snprintf(sizeof(zBuf),zBuf,"%s: %lld 0x%llx\n", azArg[i],v,v);        utf8_printf(p->out, "%s", zBuf);      }    }  }else#endif  if( c=='s' && n>=4 && strncmp(azArg[0],"selftest",n)==0 ){    int bIsInit = 0;             int bVerbose = 0;            int bSelftestExists;         int i, k;                    int nTest = 0;               int nErr = 0;                ShellText str;               sqlite3_stmt *pStmt = 0;     open_db(p,0);    for(i=1; i<nArg; i++){      const char *z = azArg[i];      if( z[0]=='-' && z[1]=='-' ) z++;      if( strcmp(z,"-init")==0 ){        bIsInit = 1;      }else      if( strcmp(z,"-v")==0 ){        bVerbose++;      }else      {        utf8_printf(stderr, "Unknown option \"%s\" on \"%s\"\n",                    azArg[i], azArg[0]);        raw_printf(stderr, "Should be one of: --init -v\n");        rc = 1;        goto meta_command_exit;      }    }    if( sqlite3_table_column_metadata(p->db,"main","selftest",0,0,0,0,0,0)           != SQLITE_OK ){      bSelftestExists = 0;    }else{      bSelftestExists = 1;    }    if( bIsInit ){      createSelftestTable(p);      bSelftestExists = 1;    }    initText(&str);    appendText(&str, "x", 0);    for(k=bSelftestExists; k>=0; k--){      if( k==1 ){        rc = sqlite3_prepare_v2(p->db,            "SELECT tno,op,cmd,ans FROM selftest ORDER BY tno",            -1, &pStmt, 0);      }else{        rc = sqlite3_prepare_v2(p->db,          "VALUES(0,'memo','Missing SELFTEST table - default checks only',''),"          "      (1,'run','PRAGMA integrity_check','ok')",          -1, &pStmt, 0);      }      if( rc ){        raw_printf(stderr, "Error querying the selftest table\n");        rc = 1;        sqlite3_finalize(pStmt);        goto meta_command_exit;      }      for(i=1; sqlite3_step(pStmt)==SQLITE_ROW; i++){        int tno = sqlite3_column_int(pStmt, 0);        const char *zOp = (const char*)sqlite3_column_text(pStmt, 1);        const char *zSql = (const char*)sqlite3_column_text(pStmt, 2);        const char *zAns = (const char*)sqlite3_column_text(pStmt, 3);        if( zOp==0 ) continue;        if( zSql==0 ) continue;        if( zAns==0 ) continue;        k = 0;        if( bVerbose>0 ){          printf("%d: %s %s\n", tno, zOp, zSql);        }        if( strcmp(zOp,"memo")==0 ){          utf8_printf(p->out, "%s\n", zSql);        }else        if( strcmp(zOp,"run")==0 ){          char *zErrMsg = 0;          str.n = 0;          str.z[0] = 0;          rc = sqlite3_exec(p->db, zSql, captureOutputCallback, &str, &zErrMsg);          nTest++;          if( bVerbose ){            utf8_printf(p->out, "Result: %s\n", str.z);          }          if( rc || zErrMsg ){            nErr++;            rc = 1;            utf8_printf(p->out, "%d: error-code-%d: %s\n", tno, rc, zErrMsg);            sqlite3_free(zErrMsg);          }else if( strcmp(zAns,str.z)!=0 ){            nErr++;            rc = 1;            utf8_printf(p->out, "%d: Expected: [%s]\n", tno, zAns);            utf8_printf(p->out, "%d:      Got: [%s]\n", tno, str.z);          }        }else        {          utf8_printf(stderr,            "Unknown operation \"%s\" on selftest line %d\n", zOp, tno);          rc = 1;          break;        }      }       sqlite3_finalize(pStmt);    }     freeText(&str);    utf8_printf(p->out, "%d errors out of %d tests\n", nErr, nTest);  }else  if( c=='s' && strncmp(azArg[0], "separator", n)==0 ){    if( nArg<2 || nArg>3 ){      raw_printf(stderr, "Usage: .separator COL ?ROW?\n");      rc = 1;    }    if( nArg>=2 ){      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator,                       "%.*s", (int)ArraySize(p->colSeparator)-1, azArg[1]);    }    if( nArg>=3 ){      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator,                       "%.*s", (int)ArraySize(p->rowSeparator)-1, azArg[2]);    }  }else  if( c=='s' && n>=4 && strncmp(azArg[0],"sha3sum",n)==0 ){    const char *zLike = 0;       int i;                       int bSchema = 0;             int bSeparate = 0;           int iSize = 224;             int bDebug = 0;              sqlite3_stmt *pStmt;         char *zSql;                  char *zSep;                  ShellText sSql;              ShellText sQuery;            open_db(p, 0);    for(i=1; i<nArg; i++){      const char *z = azArg[i];      if( z[0]=='-' ){        z++;        if( z[0]=='-' ) z++;        if( strcmp(z,"schema")==0 ){          bSchema = 1;        }else        if( strcmp(z,"sha3-224")==0 || strcmp(z,"sha3-256")==0         || strcmp(z,"sha3-384")==0 || strcmp(z,"sha3-512")==0        ){          iSize = atoi(&z[5]);        }else        if( strcmp(z,"debug")==0 ){          bDebug = 1;        }else        {          utf8_printf(stderr, "Unknown option \"%s\" on \"%s\"\n",                      azArg[i], azArg[0]);          showHelp(p->out, azArg[0]);          rc = 1;          goto meta_command_exit;        }      }else if( zLike ){        raw_printf(stderr, "Usage: .sha3sum ?OPTIONS? ?LIKE-PATTERN?\n");        rc = 1;        goto meta_command_exit;      }else{        zLike = z;        bSeparate = 1;        if( sqlite3_strlike("sqlite\\_%", zLike, '\\')==0 ) bSchema = 1;      }    }    if( bSchema ){      zSql = "SELECT lower(name) FROM sqlite_schema"             " WHERE type='table' AND coalesce(rootpage,0)>1"             " UNION ALL SELECT 'sqlite_schema'"             " ORDER BY 1 collate nocase";    }else{      zSql = "SELECT lower(name) FROM sqlite_schema"             " WHERE type='table' AND coalesce(rootpage,0)>1"             " AND name NOT LIKE 'sqlite_%'"             " ORDER BY 1 collate nocase";    }    sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);    initText(&sQuery);    initText(&sSql);    appendText(&sSql, "WITH [sha3sum$query](a,b) AS(",0);    zSep = "VALUES(";    while( SQLITE_ROW==sqlite3_step(pStmt) ){      const char *zTab = (const char*)sqlite3_column_text(pStmt,0);      if( zTab==0 ) continue;      if( zLike && sqlite3_strlike(zLike, zTab, 0)!=0 ) continue;      if( strncmp(zTab, "sqlite_",7)!=0 ){        appendText(&sQuery,"SELECT * FROM ", 0);        appendText(&sQuery,zTab,'"');        appendText(&sQuery," NOT INDEXED;", 0);      }else if( strcmp(zTab, "sqlite_schema")==0 ){        appendText(&sQuery,"SELECT type,name,tbl_name,sql FROM sqlite_schema"                           " ORDER BY name;", 0);      }else if( strcmp(zTab, "sqlite_sequence")==0 ){        appendText(&sQuery,"SELECT name,seq FROM sqlite_sequence"                           " ORDER BY name;", 0);      }else if( strcmp(zTab, "sqlite_stat1")==0 ){        appendText(&sQuery,"SELECT tbl,idx,stat FROM sqlite_stat1"                           " ORDER BY tbl,idx;", 0);      }else if( strcmp(zTab, "sqlite_stat4")==0 ){        appendText(&sQuery, "SELECT * FROM ", 0);        appendText(&sQuery, zTab, 0);        appendText(&sQuery, " ORDER BY tbl, idx, rowid;\n", 0);      }      appendText(&sSql, zSep, 0);      appendText(&sSql, sQuery.z, '\'');      sQuery.n = 0;      appendText(&sSql, ",", 0);      appendText(&sSql, zTab, '\'');      zSep = "),(";    }    sqlite3_finalize(pStmt);    if( bSeparate ){      zSql = sqlite3_mprintf(          "%s))"          " SELECT lower(hex(sha3_query(a,%d))) AS hash, b AS label"          "   FROM [sha3sum$query]",          sSql.z, iSize);    }else{      zSql = sqlite3_mprintf(          "%s))"          " SELECT lower(hex(sha3_query(group_concat(a,''),%d))) AS hash"          "   FROM [sha3sum$query]",          sSql.z, iSize);    }    shell_check_oom(zSql);    freeText(&sQuery);    freeText(&sSql);    if( bDebug ){      utf8_printf(p->out, "%s\n", zSql);    }else{      shell_exec(p, zSql, 0);    }    sqlite3_free(zSql);  }else#ifndef SQLITE_NOHAVE_SYSTEM  if( c=='s'   && (strncmp(azArg[0], "shell", n)==0 || strncmp(azArg[0],"system",n)==0)  ){    char *zCmd;    int i, x;    failIfSafeMode(p, "cannot run .%s in safe mode", azArg[0]);    if( nArg<2 ){      raw_printf(stderr, "Usage: .system COMMAND\n");      rc = 1;      goto meta_command_exit;    }    zCmd = sqlite3_mprintf(strchr(azArg[1],' ')==0?"%s":"\"%s\"", azArg[1]);    for(i=2; i<nArg && zCmd!=0; i++){      zCmd = sqlite3_mprintf(strchr(azArg[i],' ')==0?"%z %s":"%z \"%s\"",                             zCmd, azArg[i]);    }    x = zCmd!=0 ? system(zCmd) : 1;    sqlite3_free(zCmd);    if( x ) raw_printf(stderr, "System command returns %d\n", x);  }else#endif   if( c=='s' && strncmp(azArg[0], "show", n)==0 ){    static const char *azBool[] = { "off", "on", "trigger", "full"};    const char *zOut;    int i;    if( nArg!=1 ){      raw_printf(stderr, "Usage: .show\n");      rc = 1;      goto meta_command_exit;    }    utf8_printf(p->out, "%12.12s: %s\n","echo",                                  azBool[ShellHasFlag(p, SHFLG_Echo)]);    utf8_printf(p->out, "%12.12s: %s\n","eqp", azBool[p->autoEQP&3]);    utf8_printf(p->out, "%12.12s: %s\n","explain",         p->mode==MODE_Explain ? "on" : p->autoExplain ? "auto" : "off");    utf8_printf(p->out,"%12.12s: %s\n","headers", azBool[p->showHeader!=0]);    if( p->mode==MODE_Column     || (p->mode>=MODE_Markdown && p->mode<=MODE_Box)    ){      utf8_printf        (p->out, "%12.12s: %s --wrap %d --wordwrap %s --%squote\n", "mode",         modeDescr[p->mode], p->cmOpts.iWrap,         p->cmOpts.bWordWrap ? "on" : "off",         p->cmOpts.bQuote ? "" : "no");    }else{      utf8_printf(p->out, "%12.12s: %s\n","mode", modeDescr[p->mode]);    }    utf8_printf(p->out, "%12.12s: ", "nullvalue");      output_c_string(p->out, p->nullValue);      raw_printf(p->out, "\n");    utf8_printf(p->out,"%12.12s: %s\n","output",            strlen30(p->outfile) ? p->outfile : "stdout");    utf8_printf(p->out,"%12.12s: ", "colseparator");      output_c_string(p->out, p->colSeparator);      raw_printf(p->out, "\n");    utf8_printf(p->out,"%12.12s: ", "rowseparator");      output_c_string(p->out, p->rowSeparator);      raw_printf(p->out, "\n");    switch( p->statsOn ){      case 0:  zOut = "off";     break;      default: zOut = "on";      break;      case 2:  zOut = "stmt";    break;      case 3:  zOut = "vmstep";  break;    }    utf8_printf(p->out, "%12.12s: %s\n","stats", zOut);    utf8_printf(p->out, "%12.12s: ", "width");    for (i=0;i<p->nWidth;i++) {      raw_printf(p->out, "%d ", p->colWidth[i]);    }    raw_printf(p->out, "\n");    utf8_printf(p->out, "%12.12s: %s\n", "filename",                p->pAuxDb->zDbFilename ? p->pAuxDb->zDbFilename : "");  }else  if( c=='s' && strncmp(azArg[0], "stats", n)==0 ){    if( nArg==2 ){      if( strcmp(azArg[1],"stmt")==0 ){        p->statsOn = 2;      }else if( strcmp(azArg[1],"vmstep")==0 ){        p->statsOn = 3;      }else{        p->statsOn = (u8)booleanValue(azArg[1]);      }    }else if( nArg==1 ){      display_stats(p->db, p, 0);    }else{      raw_printf(stderr, "Usage: .stats ?on|off|stmt|vmstep?\n");      rc = 1;    }  }else  if( (c=='t' && n>1 && strncmp(azArg[0], "tables", n)==0)   || (c=='i' && (strncmp(azArg[0], "indices", n)==0                 || strncmp(azArg[0], "indexes", n)==0) )  ){    sqlite3_stmt *pStmt;    char **azResult;    int nRow, nAlloc;    int ii;    ShellText s;    initText(&s);    open_db(p, 0);    rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0);    if( rc ){      sqlite3_finalize(pStmt);      return shellDatabaseError(p->db);    }    if( nArg>2 && c=='i' ){            raw_printf(stderr, "Usage: .indexes ?LIKE-PATTERN?\n");      rc = 1;      sqlite3_finalize(pStmt);      goto meta_command_exit;    }    for(ii=0; sqlite3_step(pStmt)==SQLITE_ROW; ii++){      const char *zDbName = (const char*)sqlite3_column_text(pStmt, 1);      if( zDbName==0 ) continue;      if( s.z && s.z[0] ) appendText(&s, " UNION ALL ", 0);      if( sqlite3_stricmp(zDbName, "main")==0 ){        appendText(&s, "SELECT name FROM ", 0);      }else{        appendText(&s, "SELECT ", 0);        appendText(&s, zDbName, '\'');        appendText(&s, "||'.'||name FROM ", 0);      }      appendText(&s, zDbName, '"');      appendText(&s, ".sqlite_schema ", 0);      if( c=='t' ){        appendText(&s," WHERE type IN ('table','view')"                      "   AND name NOT LIKE 'sqlite_%'"                      "   AND name LIKE ?1", 0);      }else{        appendText(&s," WHERE type='index'"                      "   AND tbl_name LIKE ?1", 0);      }    }    rc = sqlite3_finalize(pStmt);    if( rc==SQLITE_OK ){      appendText(&s, " ORDER BY 1", 0);      rc = sqlite3_prepare_v2(p->db, s.z, -1, &pStmt, 0);    }    freeText(&s);    if( rc ) return shellDatabaseError(p->db);        nRow = nAlloc = 0;    azResult = 0;    if( nArg>1 ){      sqlite3_bind_text(pStmt, 1, azArg[1], -1, SQLITE_TRANSIENT);    }else{      sqlite3_bind_text(pStmt, 1, "%", -1, SQLITE_STATIC);    }    while( sqlite3_step(pStmt)==SQLITE_ROW ){      if( nRow>=nAlloc ){        char **azNew;        int n2 = nAlloc*2 + 10;        azNew = sqlite3_realloc64(azResult, sizeof(azResult[0])*n2);        shell_check_oom(azNew);        nAlloc = n2;        azResult = azNew;      }      azResult[nRow] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 0));      shell_check_oom(azResult[nRow]);      nRow++;    }    if( sqlite3_finalize(pStmt)!=SQLITE_OK ){      rc = shellDatabaseError(p->db);    }        if( rc==0 && nRow>0 ){      int len, maxlen = 0;      int i, j;      int nPrintCol, nPrintRow;      for(i=0; i<nRow; i++){        len = strlen30(azResult[i]);        if( len>maxlen ) maxlen = len;      }      nPrintCol = 80/(maxlen+2);      if( nPrintCol<1 ) nPrintCol = 1;      nPrintRow = (nRow + nPrintCol - 1)/nPrintCol;      for(i=0; i<nPrintRow; i++){        for(j=i; j<nRow; j+=nPrintRow){          char *zSp = j<nPrintRow ? "" : "  ";          utf8_printf(p->out, "%s%-*s", zSp, maxlen,                      azResult[j] ? azResult[j]:"");        }        raw_printf(p->out, "\n");      }    }    for(ii=0; ii<nRow; ii++) sqlite3_free(azResult[ii]);    sqlite3_free(azResult);  }else    if( c=='t' && strcmp(azArg[0],"testcase")==0 ){    output_reset(p);    p->out = output_file_open("testcase-out.txt", 0);    if( p->out==0 ){      raw_printf(stderr, "Error: cannot open 'testcase-out.txt'\n");    }    if( nArg>=2 ){      sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "%s", azArg[1]);    }else{      sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "?");    }  }else#ifndef SQLITE_UNTESTABLE  if( c=='t' && n>=8 && strncmp(azArg[0], "testctrl", n)==0 ){    static const struct {       const char *zCtrlName;          int ctrlCode;                   int unSafe;                     const char *zUsage;          } aCtrl[] = {      { "always",             SQLITE_TESTCTRL_ALWAYS, 1,     "BOOLEAN"         },      { "assert",             SQLITE_TESTCTRL_ASSERT, 1,     "BOOLEAN"         },              { "byteorder",          SQLITE_TESTCTRL_BYTEORDER, 0,  ""                },      { "extra_schema_checks",SQLITE_TESTCTRL_EXTRA_SCHEMA_CHECKS,0,"BOOLEAN"  },          { "imposter",         SQLITE_TESTCTRL_IMPOSTER,1,"SCHEMA ON/OFF ROOTPAGE"},      { "internal_functions", SQLITE_TESTCTRL_INTERNAL_FUNCTIONS,0,""          },      { "localtime_fault",    SQLITE_TESTCTRL_LOCALTIME_FAULT,0,"BOOLEAN"      },      { "never_corrupt",      SQLITE_TESTCTRL_NEVER_CORRUPT,1, "BOOLEAN"       },      { "optimizations",      SQLITE_TESTCTRL_OPTIMIZATIONS,0,"DISABLE-MASK"   },#ifdef YYCOVERAGE      { "parser_coverage",    SQLITE_TESTCTRL_PARSER_COVERAGE,0,""             },#endif      { "pending_byte",       SQLITE_TESTCTRL_PENDING_BYTE,0, "OFFSET  "       },      { "prng_restore",       SQLITE_TESTCTRL_PRNG_RESTORE,0, ""               },      { "prng_save",          SQLITE_TESTCTRL_PRNG_SAVE,   0, ""               },      { "prng_seed",          SQLITE_TESTCTRL_PRNG_SEED,   0, "SEED ?db?"      },      { "seek_count",         SQLITE_TESTCTRL_SEEK_COUNT,  0, ""               },      { "sorter_mmap",        SQLITE_TESTCTRL_SORTER_MMAP, 0, "NMAX"           },      { "tune",               SQLITE_TESTCTRL_TUNE,        1, "ID VALUE"       },    };    int testctrl = -1;    int iCtrl = -1;    int rc2 = 0;        int isOk = 0;    int i, n2;    const char *zCmd = 0;    open_db(p, 0);    zCmd = nArg>=2 ? azArg[1] : "help";        if( zCmd[0]=='-' && zCmd[1] ){      zCmd++;      if( zCmd[0]=='-' && zCmd[1] ) zCmd++;    }        if( strcmp(zCmd,"help")==0 ){      utf8_printf(p->out, "Available test-controls:\n");      for(i=0; i<ArraySize(aCtrl); i++){        utf8_printf(p->out, "  .testctrl %s %s\n",                    aCtrl[i].zCtrlName, aCtrl[i].zUsage);      }      rc = 1;      goto meta_command_exit;    }        n2 = strlen30(zCmd);    for(i=0; i<ArraySize(aCtrl); i++){      if( strncmp(zCmd, aCtrl[i].zCtrlName, n2)==0 ){        if( testctrl<0 ){          testctrl = aCtrl[i].ctrlCode;          iCtrl = i;        }else{          utf8_printf(stderr, "Error: ambiguous test-control: \"%s\"\n"                              "Use \".testctrl --help\" for help\n", zCmd);          rc = 1;          goto meta_command_exit;        }      }    }    if( testctrl<0 ){      utf8_printf(stderr,"Error: unknown test-control: %s\n"                         "Use \".testctrl --help\" for help\n", zCmd);    }else if( aCtrl[iCtrl].unSafe && p->bSafeMode ){      utf8_printf(stderr,         "line %d: \".testctrl %s\" may not be used in safe mode\n",         p->lineno, aCtrl[iCtrl].zCtrlName);      exit(1);    }else{      switch(testctrl){                case SQLITE_TESTCTRL_OPTIMIZATIONS:          if( nArg==3 ){            unsigned int opt = (unsigned int)strtol(azArg[2], 0, 0);            rc2 = sqlite3_test_control(testctrl, p->db, opt);            isOk = 3;          }          break;                case SQLITE_TESTCTRL_PRNG_SAVE:        case SQLITE_TESTCTRL_PRNG_RESTORE:        case SQLITE_TESTCTRL_BYTEORDER:          if( nArg==2 ){            rc2 = sqlite3_test_control(testctrl);            isOk = testctrl==SQLITE_TESTCTRL_BYTEORDER ? 1 : 3;          }          break;                case SQLITE_TESTCTRL_PENDING_BYTE:          if( nArg==3 ){            unsigned int opt = (unsigned int)integerValue(azArg[2]);            rc2 = sqlite3_test_control(testctrl, opt);            isOk = 3;          }          break;                case SQLITE_TESTCTRL_PRNG_SEED:          if( nArg==3 || nArg==4 ){            int ii = (int)integerValue(azArg[2]);            sqlite3 *db;            if( ii==0 && strcmp(azArg[2],"random")==0 ){              sqlite3_randomness(sizeof(ii),&ii);              printf("-- random seed: %d\n", ii);            }            if( nArg==3 ){              db = 0;            }else{              db = p->db;                            sqlite3_table_column_metadata(db, 0, "x", 0, 0, 0, 0, 0, 0);            }            rc2 = sqlite3_test_control(testctrl, ii, db);            isOk = 3;          }          break;                case SQLITE_TESTCTRL_ASSERT:        case SQLITE_TESTCTRL_ALWAYS:          if( nArg==3 ){            int opt = booleanValue(azArg[2]);            rc2 = sqlite3_test_control(testctrl, opt);            isOk = 1;          }          break;                case SQLITE_TESTCTRL_LOCALTIME_FAULT:        case SQLITE_TESTCTRL_NEVER_CORRUPT:          if( nArg==3 ){            int opt = booleanValue(azArg[2]);            rc2 = sqlite3_test_control(testctrl, opt);            isOk = 3;          }          break;                case SQLITE_TESTCTRL_INTERNAL_FUNCTIONS:          rc2 = sqlite3_test_control(testctrl, p->db);          isOk = 3;          break;        case SQLITE_TESTCTRL_IMPOSTER:          if( nArg==5 ){            rc2 = sqlite3_test_control(testctrl, p->db,                          azArg[2],                          integerValue(azArg[3]),                          integerValue(azArg[4]));            isOk = 3;          }          break;        case SQLITE_TESTCTRL_SEEK_COUNT: {          u64 x = 0;          rc2 = sqlite3_test_control(testctrl, p->db, &x);          utf8_printf(p->out, "%llu\n", x);          isOk = 3;          break;        }#ifdef YYCOVERAGE        case SQLITE_TESTCTRL_PARSER_COVERAGE: {          if( nArg==2 ){            sqlite3_test_control(testctrl, p->out);            isOk = 3;          }          break;        }#endif#ifdef SQLITE_DEBUG        case SQLITE_TESTCTRL_TUNE: {          if( nArg==4 ){            int id = (int)integerValue(azArg[2]);            int val = (int)integerValue(azArg[3]);            sqlite3_test_control(testctrl, id, &val);            isOk = 3;          }else if( nArg==3 ){            int id = (int)integerValue(azArg[2]);            sqlite3_test_control(testctrl, -id, &rc2);            isOk = 1;          }else if( nArg==2 ){            int id = 1;            while(1){              int val = 0;              rc2 = sqlite3_test_control(testctrl, -id, &val);              if( rc2!=SQLITE_OK ) break;              if( id>1 ) utf8_printf(p->out, "  ");              utf8_printf(p->out, "%d: %d", id, val);              id++;            }            if( id>1 ) utf8_printf(p->out, "\n");            isOk = 3;          }          break;        }#endif        case SQLITE_TESTCTRL_SORTER_MMAP:          if( nArg==3 ){            int opt = (unsigned int)integerValue(azArg[2]);            rc2 = sqlite3_test_control(testctrl, p->db, opt);            isOk = 3;          }          break;      }    }    if( isOk==0 && iCtrl>=0 ){      utf8_printf(p->out, "Usage: .testctrl %s %s\n", zCmd,aCtrl[iCtrl].zUsage);      rc = 1;    }else if( isOk==1 ){      raw_printf(p->out, "%d\n", rc2);    }else if( isOk==2 ){      raw_printf(p->out, "0x%08x\n", rc2);    }  }else#endif   if( c=='t' && n>4 && strncmp(azArg[0], "timeout", n)==0 ){    open_db(p, 0);    sqlite3_busy_timeout(p->db, nArg>=2 ? (int)integerValue(azArg[1]) : 0);  }else  if( c=='t' && n>=5 && strncmp(azArg[0], "timer", n)==0 ){    if( nArg==2 ){      enableTimer = booleanValue(azArg[1]);      if( enableTimer && !HAS_TIMER ){        raw_printf(stderr, "Error: timer not available on this system.\n");        enableTimer = 0;      }    }else{      raw_printf(stderr, "Usage: .timer on|off\n");      rc = 1;    }  }else#ifndef SQLITE_OMIT_TRACE  if( c=='t' && strncmp(azArg[0], "trace", n)==0 ){    int mType = 0;    int jj;    open_db(p, 0);    for(jj=1; jj<nArg; jj++){      const char *z = azArg[jj];      if( z[0]=='-' ){        if( optionMatch(z, "expanded") ){          p->eTraceType = SHELL_TRACE_EXPANDED;        }#ifdef SQLITE_ENABLE_NORMALIZE        else if( optionMatch(z, "normalized") ){          p->eTraceType = SHELL_TRACE_NORMALIZED;        }#endif        else if( optionMatch(z, "plain") ){          p->eTraceType = SHELL_TRACE_PLAIN;        }        else if( optionMatch(z, "profile") ){          mType |= SQLITE_TRACE_PROFILE;        }        else if( optionMatch(z, "row") ){          mType |= SQLITE_TRACE_ROW;        }        else if( optionMatch(z, "stmt") ){          mType |= SQLITE_TRACE_STMT;        }        else if( optionMatch(z, "close") ){          mType |= SQLITE_TRACE_CLOSE;        }        else {          raw_printf(stderr, "Unknown option \"%s\" on \".trace\"\n", z);          rc = 1;          goto meta_command_exit;        }      }else{        output_file_close(p->traceOut);        p->traceOut = output_file_open(azArg[1], 0);      }    }    if( p->traceOut==0 ){      sqlite3_trace_v2(p->db, 0, 0, 0);    }else{      if( mType==0 ) mType = SQLITE_TRACE_STMT;      sqlite3_trace_v2(p->db, mType, sql_trace_callback, p);    }  }else#endif #if defined(SQLITE_DEBUG) && !defined(SQLITE_OMIT_VIRTUALTABLE)  if( c=='u' && strncmp(azArg[0], "unmodule", n)==0 ){    int ii;    int lenOpt;    char *zOpt;    if( nArg<2 ){      raw_printf(stderr, "Usage: .unmodule [--allexcept] NAME ...\n");      rc = 1;      goto meta_command_exit;    }    open_db(p, 0);    zOpt = azArg[1];    if( zOpt[0]=='-' && zOpt[1]=='-' && zOpt[2]!=0 ) zOpt++;    lenOpt = (int)strlen(zOpt);    if( lenOpt>=3 && strncmp(zOpt, "-allexcept",lenOpt)==0 ){      assert( azArg[nArg]==0 );      sqlite3_drop_modules(p->db, nArg>2 ? (const char**)(azArg+2) : 0);    }else{      for(ii=1; ii<nArg; ii++){        sqlite3_create_module(p->db, azArg[ii], 0, 0);      }    }  }else#endif#if SQLITE_USER_AUTHENTICATION  if( c=='u' && strncmp(azArg[0], "user", n)==0 ){    if( nArg<2 ){      raw_printf(stderr, "Usage: .user SUBCOMMAND ...\n");      rc = 1;      goto meta_command_exit;    }    open_db(p, 0);    if( strcmp(azArg[1],"login")==0 ){      if( nArg!=4 ){        raw_printf(stderr, "Usage: .user login USER PASSWORD\n");        rc = 1;        goto meta_command_exit;      }      rc = sqlite3_user_authenticate(p->db, azArg[2], azArg[3],                                     strlen30(azArg[3]));      if( rc ){        utf8_printf(stderr, "Authentication failed for user %s\n", azArg[2]);        rc = 1;      }    }else if( strcmp(azArg[1],"add")==0 ){      if( nArg!=5 ){        raw_printf(stderr, "Usage: .user add USER PASSWORD ISADMIN\n");        rc = 1;        goto meta_command_exit;      }      rc = sqlite3_user_add(p->db, azArg[2], azArg[3], strlen30(azArg[3]),                            booleanValue(azArg[4]));      if( rc ){        raw_printf(stderr, "User-Add failed: %d\n", rc);        rc = 1;      }    }else if( strcmp(azArg[1],"edit")==0 ){      if( nArg!=5 ){        raw_printf(stderr, "Usage: .user edit USER PASSWORD ISADMIN\n");        rc = 1;        goto meta_command_exit;      }      rc = sqlite3_user_change(p->db, azArg[2], azArg[3], strlen30(azArg[3]),                              booleanValue(azArg[4]));      if( rc ){        raw_printf(stderr, "User-Edit failed: %d\n", rc);        rc = 1;      }    }else if( strcmp(azArg[1],"delete")==0 ){      if( nArg!=3 ){        raw_printf(stderr, "Usage: .user delete USER\n");        rc = 1;        goto meta_command_exit;      }      rc = sqlite3_user_delete(p->db, azArg[2]);      if( rc ){        raw_printf(stderr, "User-Delete failed: %d\n", rc);        rc = 1;      }    }else{      raw_printf(stderr, "Usage: .user login|add|edit|delete ...\n");      rc = 1;      goto meta_command_exit;    }  }else#endif   if( c=='v' && strncmp(azArg[0], "version", n)==0 ){    utf8_printf(p->out, "SQLite %s %s\n" ,        sqlite3_libversion(), sqlite3_sourceid());#if SQLITE_HAVE_ZLIB    utf8_printf(p->out, "zlib version %s\n", zlibVersion());#endif#define CTIMEOPT_VAL_(opt) #opt#define CTIMEOPT_VAL(opt) CTIMEOPT_VAL_(opt)#if defined(__clang__) && defined(__clang_major__)    utf8_printf(p->out, "clang-" CTIMEOPT_VAL(__clang_major__) "."                    CTIMEOPT_VAL(__clang_minor__) "."                    CTIMEOPT_VAL(__clang_patchlevel__) "\n");#elif defined(_MSC_VER)    utf8_printf(p->out, "msvc-" CTIMEOPT_VAL(_MSC_VER) "\n");#elif defined(__GNUC__) && defined(__VERSION__)    utf8_printf(p->out, "gcc-" __VERSION__ "\n");#endif  }else  if( c=='v' && strncmp(azArg[0], "vfsinfo", n)==0 ){    const char *zDbName = nArg==2 ? azArg[1] : "main";    sqlite3_vfs *pVfs = 0;    if( p->db ){      sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFS_POINTER, &pVfs);      if( pVfs ){        utf8_printf(p->out, "vfs.zName      = \"%s\"\n", pVfs->zName);        raw_printf(p->out, "vfs.iVersion   = %d\n", pVfs->iVersion);        raw_printf(p->out, "vfs.szOsFile   = %d\n", pVfs->szOsFile);        raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname);      }    }  }else  if( c=='v' && strncmp(azArg[0], "vfslist", n)==0 ){    sqlite3_vfs *pVfs;    sqlite3_vfs *pCurrent = 0;    if( p->db ){      sqlite3_file_control(p->db, "main", SQLITE_FCNTL_VFS_POINTER, &pCurrent);    }    for(pVfs=sqlite3_vfs_find(0); pVfs; pVfs=pVfs->pNext){      utf8_printf(p->out, "vfs.zName      = \"%s\"%s\n", pVfs->zName,           pVfs==pCurrent ? "  <--- CURRENT" : "");      raw_printf(p->out, "vfs.iVersion   = %d\n", pVfs->iVersion);      raw_printf(p->out, "vfs.szOsFile   = %d\n", pVfs->szOsFile);      raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname);      if( pVfs->pNext ){        raw_printf(p->out, "-----------------------------------\n");      }    }  }else  if( c=='v' && strncmp(azArg[0], "vfsname", n)==0 ){    const char *zDbName = nArg==2 ? azArg[1] : "main";    char *zVfsName = 0;    if( p->db ){      sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFSNAME, &zVfsName);      if( zVfsName ){        utf8_printf(p->out, "%s\n", zVfsName);        sqlite3_free(zVfsName);      }    }  }else  if( c=='w' && strncmp(azArg[0], "wheretrace", n)==0 ){    unsigned int x = nArg>=2 ? (unsigned int)integerValue(azArg[1]) : 0xffffffff;    sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 3, &x);  }else  if( c=='w' && strncmp(azArg[0], "width", n)==0 ){    int j;    assert( nArg<=ArraySize(azArg) );    p->nWidth = nArg-1;    p->colWidth = realloc(p->colWidth, (p->nWidth+1)*sizeof(int)*2);    if( p->colWidth==0 && p->nWidth>0 ) shell_out_of_memory();    if( p->nWidth ) p->actualWidth = &p->colWidth[p->nWidth];    for(j=1; j<nArg; j++){      p->colWidth[j-1] = (int)integerValue(azArg[j]);    }  }else  {    utf8_printf(stderr, "Error: unknown command or invalid arguments: "      " \"%s\". Enter \".help\" for help\n", azArg[0]);    rc = 1;  }meta_command_exit:  if( p->outCount ){    p->outCount--;    if( p->outCount==0 ) output_reset(p);  }  p->bSafeMode = p->bSafeModePersist;  return rc;}#ifndef CHAR_BIT# define CHAR_BIT 8#endiftypedef enum {  QSS_HasDark = 1<<CHAR_BIT, QSS_EndingSemi = 2<<CHAR_BIT,  QSS_CharMask = (1<<CHAR_BIT)-1, QSS_ScanMask = 3<<CHAR_BIT,  QSS_Start = 0} QuickScanState;#define QSS_SETV(qss, newst) ((newst) | ((qss) & QSS_ScanMask))#define QSS_INPLAIN(qss) (((qss)&QSS_CharMask)==QSS_Start)#define QSS_PLAINWHITE(qss) (((qss)&~QSS_EndingSemi)==QSS_Start)#define QSS_PLAINDARK(qss) (((qss)&~QSS_EndingSemi)==QSS_HasDark)#define QSS_SEMITERM(qss) (((qss)&~QSS_HasDark)==QSS_EndingSemi)static QuickScanState quickscan(char *zLine, QuickScanState qss){  char cin;  char cWait = (char)qss;   if( cWait==0 ){  PlainScan:    assert( cWait==0 );    while( (cin = *zLine++)!=0 ){      if( IsSpace(cin) )        continue;      switch (cin){      case '-':        if( *zLine!='-' )          break;        while((cin = *++zLine)!=0 )          if( cin=='\n')            goto PlainScan;        return qss;      case ';':        qss |= QSS_EndingSemi;        continue;      case '/':        if( *zLine=='*' ){          ++zLine;          cWait = '*';          qss = QSS_SETV(qss, cWait);          goto TermScan;        }        break;      case '[':        cin = ']';              case '`': case '\'': case '"':        cWait = cin;        qss = QSS_HasDark | cWait;        goto TermScan;      default:        break;      }      qss = (qss & ~QSS_EndingSemi) | QSS_HasDark;    }  }else{  TermScan:    while( (cin = *zLine++)!=0 ){      if( cin==cWait ){        switch( cWait ){        case '*':          if( *zLine != '/' )            continue;          ++zLine;          cWait = 0;          qss = QSS_SETV(qss, 0);          goto PlainScan;        case '`': case '\'': case '"':          if(*zLine==cWait){            ++zLine;            continue;          }                  case ']':          cWait = 0;          qss = QSS_SETV(qss, 0);          goto PlainScan;        default: assert(0);         }      }    }  }  return qss;}static int line_is_command_terminator(char *zLine){  while( IsSpace(zLine[0]) ){ zLine++; };  if( zLine[0]=='/' )    zLine += 1;   else if ( ToLower(zLine[0])=='g' && ToLower(zLine[1])=='o' )    zLine += 2;   else    return 0;  return quickscan(zLine,QSS_Start)==QSS_Start;}#ifdef SQLITE_OMIT_COMPLETE#define sqlite3_complete(x) 1#endifstatic int line_is_complete(char *zSql, int nSql){  int rc;  if( zSql==0 ) return 1;  zSql[nSql] = ';';  zSql[nSql+1] = 0;  rc = sqlite3_complete(zSql);  zSql[nSql] = 0;  return rc;}static int runOneSqlLine(ShellState *p, char *zSql, FILE *in, int startline){  int rc;  char *zErrMsg = 0;  open_db(p, 0);  if( ShellHasFlag(p,SHFLG_Backslash) ) resolve_backslashes(zSql);  if( p->flgProgress & SHELL_PROGRESS_RESET ) p->nProgress = 0;  BEGIN_TIMER;  rc = shell_exec(p, zSql, &zErrMsg);  END_TIMER;  if( rc || zErrMsg ){    char zPrefix[100];    const char *zErrorTail;    const char *zErrorType;    if( zErrMsg==0 ){      zErrorType = "Error";      zErrorTail = sqlite3_errmsg(p->db);    }else if( strncmp(zErrMsg, "in prepare, ",12)==0 ){      zErrorType = "Parse error";      zErrorTail = &zErrMsg[12];    }else if( strncmp(zErrMsg, "stepping, ", 10)==0 ){      zErrorType = "Runtime error";      zErrorTail = &zErrMsg[10];    }else{      zErrorType = "Error";      zErrorTail = zErrMsg;    }    if( in!=0 || !stdin_is_interactive ){      sqlite3_snprintf(sizeof(zPrefix), zPrefix,                       "%s near line %d:", zErrorType, startline);    }else{      sqlite3_snprintf(sizeof(zPrefix), zPrefix, "%s:", zErrorType);    }    utf8_printf(stderr, "%s %s\n", zPrefix, zErrorTail);    sqlite3_free(zErrMsg);    zErrMsg = 0;    return 1;  }else if( ShellHasFlag(p, SHFLG_CountChanges) ){    char zLineBuf[2000];    sqlite3_snprintf(sizeof(zLineBuf), zLineBuf,            "changes: %lld   total_changes: %lld",            sqlite3_changes64(p->db), sqlite3_total_changes64(p->db));    raw_printf(p->out, "%s\n", zLineBuf);  }  return 0;}static int process_input(ShellState *p){  char *zLine = 0;            char *zSql = 0;             int nLine;                  int nSql = 0;               int nAlloc = 0;             int rc;                     int errCnt = 0;             int startline = 0;          QuickScanState qss = QSS_Start;   if( p->inputNesting==MAX_INPUT_NESTING ){        utf8_printf(stderr,"Input nesting limit (%d) reached at line %d."                " Check recursion.\n", MAX_INPUT_NESTING, p->lineno);    return 1;  }  ++p->inputNesting;  p->lineno = 0;  while( errCnt==0 || !bail_on_error || (p->in==0 && stdin_is_interactive) ){    fflush(p->out);    zLine = one_input_line(p->in, zLine, nSql>0);    if( zLine==0 ){            if( p->in==0 && stdin_is_interactive ) printf("\n");      break;    }    if( seenInterrupt ){      if( p->in!=0 ) break;      seenInterrupt = 0;    }    p->lineno++;    if( QSS_INPLAIN(qss)        && line_is_command_terminator(zLine)        && line_is_complete(zSql, nSql) ){      memcpy(zLine,";",2);    }    qss = quickscan(zLine, qss);    if( QSS_PLAINWHITE(qss) && nSql==0 ){      if( ShellHasFlag(p, SHFLG_Echo) )        printf("%s\n", zLine);            qss = QSS_Start;      continue;    }    if( zLine && (zLine[0]=='.' || zLine[0]=='#') && nSql==0 ){      if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zLine);      if( zLine[0]=='.' ){        rc = do_meta_command(zLine, p);        if( rc==2 ){           break;        }else if( rc ){          errCnt++;        }      }      qss = QSS_Start;      continue;    }        nLine = strlen30(zLine);    if( nSql+nLine+2>=nAlloc ){            nAlloc = nSql+(nSql>>1)+nLine+100;      zSql = realloc(zSql, nAlloc);      shell_check_oom(zSql);    }    if( nSql==0 ){      int i;      for(i=0; zLine[i] && IsSpace(zLine[i]); i++){}      assert( nAlloc>0 && zSql!=0 );      memcpy(zSql, zLine+i, nLine+1-i);      startline = p->lineno;      nSql = nLine-i;    }else{      zSql[nSql++] = '\n';      memcpy(zSql+nSql, zLine, nLine+1);      nSql += nLine;    }    if( nSql && QSS_SEMITERM(qss) && sqlite3_complete(zSql) ){      errCnt += runOneSqlLine(p, zSql, p->in, startline);      nSql = 0;      if( p->outCount ){        output_reset(p);        p->outCount = 0;      }else{        clearTempFile(p);      }      p->bSafeMode = p->bSafeModePersist;      qss = QSS_Start;    }else if( nSql && QSS_PLAINWHITE(qss) ){      if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zSql);      nSql = 0;      qss = QSS_Start;    }  }  if( nSql ){        errCnt += runOneSqlLine(p, zSql, p->in, startline);  }  free(zSql);  free(zLine);  --p->inputNesting;  return errCnt>0;}static char *find_home_dir(int clearFlag){  static char *home_dir = NULL;  if( clearFlag ){    free(home_dir);    home_dir = 0;    return 0;  }  if( home_dir ) return home_dir;#if !defined(_WIN32) && !defined(WIN32) && !defined(_WIN32_WCE) \     && !defined(__RTP__) && !defined(_WRS_KERNEL)  {    struct passwd *pwent;    uid_t uid = getuid();    if( (pwent=getpwuid(uid)) != NULL) {      home_dir = pwent->pw_dir;    }  }#endif#if defined(_WIN32_WCE)    home_dir = "/";#else#if defined(_WIN32) || defined(WIN32)  if (!home_dir) {    home_dir = getenv("USERPROFILE");  }#endif  if (!home_dir) {    home_dir = getenv("HOME");  }#if defined(_WIN32) || defined(WIN32)  if (!home_dir) {    char *zDrive, *zPath;    int n;    zDrive = getenv("HOMEDRIVE");    zPath = getenv("HOMEPATH");    if( zDrive && zPath ){      n = strlen30(zDrive) + strlen30(zPath) + 1;      home_dir = malloc( n );      if( home_dir==0 ) return 0;      sqlite3_snprintf(n, home_dir, "%s%s", zDrive, zPath);      return home_dir;    }    home_dir = "c:\\";  }#endif#endif   if( home_dir ){    int n = strlen30(home_dir) + 1;    char *z = malloc( n );    if( z ) memcpy(z, home_dir, n);    home_dir = z;  }  return home_dir;}static void process_sqliterc(  ShellState *p,                    const char *sqliterc_override   ){  char *home_dir = NULL;  const char *sqliterc = sqliterc_override;  char *zBuf = 0;  FILE *inSaved = p->in;  int savedLineno = p->lineno;  if (sqliterc == NULL) {    home_dir = find_home_dir(0);    if( home_dir==0 ){      raw_printf(stderr, "-- warning: cannot find home directory;"                      " cannot read ~/.sqliterc\n");      return;    }    zBuf = sqlite3_mprintf("%s/.sqliterc",home_dir);    shell_check_oom(zBuf);    sqliterc = zBuf;  }  p->in = fopen(sqliterc,"rb");  if( p->in ){    if( stdin_is_interactive ){      utf8_printf(stderr,"-- Loading resources from %s\n",sqliterc);    }    if( process_input(p) && bail_on_error ) exit(1);    fclose(p->in);  }else if( sqliterc_override!=0 ){    utf8_printf(stderr,"cannot open: \"%s\"\n", sqliterc);    if( bail_on_error ) exit(1);  }  p->in = inSaved;  p->lineno = savedLineno;  sqlite3_free(zBuf);}static const char zOptions[] =#if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE)  "   -A ARGS...           run \".archive ARGS\" and exit\n"#endif  "   -append              append the database to the end of the file\n"  "   -ascii               set output mode to 'ascii'\n"  "   -bail                stop after hitting an error\n"  "   -batch               force batch I/O\n"  "   -box                 set output mode to 'box'\n"  "   -column              set output mode to 'column'\n"  "   -cmd COMMAND         run \"COMMAND\" before reading stdin\n"  "   -csv                 set output mode to 'csv'\n"#if !defined(SQLITE_OMIT_DESERIALIZE)  "   -deserialize         open the database using sqlite3_deserialize()\n"#endif  "   -echo                print commands before execution\n"  "   -init FILENAME       read/process named file\n"  "   -[no]header          turn headers on or off\n"#if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)  "   -heap SIZE           Size of heap for memsys3 or memsys5\n"#endif  "   -help                show this message\n"  "   -html                set output mode to HTML\n"  "   -interactive         force interactive I/O\n"  "   -json                set output mode to 'json'\n"  "   -line                set output mode to 'line'\n"  "   -list                set output mode to 'list'\n"  "   -lookaside SIZE N    use N entries of SZ bytes for lookaside memory\n"  "   -markdown            set output mode to 'markdown'\n"#if !defined(SQLITE_OMIT_DESERIALIZE)  "   -maxsize N           maximum size for a --deserialize database\n"#endif  "   -memtrace            trace all memory allocations and deallocations\n"  "   -mmap N              default mmap size set to N\n"#ifdef SQLITE_ENABLE_MULTIPLEX  "   -multiplex           enable the multiplexor VFS\n"#endif  "   -newline SEP         set output row separator. Default: '\\n'\n"  "   -nofollow            refuse to open symbolic links to database files\n"  "   -nonce STRING        set the safe-mode escape nonce\n"  "   -nullvalue TEXT      set text string for NULL values. Default ''\n"  "   -pagecache SIZE N    use N slots of SZ bytes each for page cache memory\n"  "   -quote               set output mode to 'quote'\n"  "   -readonly            open the database read-only\n"  "   -safe                enable safe-mode\n"  "   -separator SEP       set output column separator. Default: '|'\n"#ifdef SQLITE_ENABLE_SORTER_REFERENCES  "   -sorterref SIZE      sorter references threshold size\n"#endif  "   -stats               print memory stats before each finalize\n"  "   -table               set output mode to 'table'\n"  "   -tabs                set output mode to 'tabs'\n"  "   -version             show SQLite version\n"  "   -vfs NAME            use NAME as the default VFS\n"#ifdef SQLITE_ENABLE_VFSTRACE  "   -vfstrace            enable tracing of all VFS calls\n"#endif#ifdef SQLITE_HAVE_ZLIB  "   -zip                 open the file as a ZIP Archive\n"#endif;static void usage(int showDetail){  utf8_printf(stderr,      "Usage: %s [OPTIONS] FILENAME [SQL]\n"      "FILENAME is the name of an SQLite database. A new database is created\n"      "if the file does not previously exist.\n", Argv0);  if( showDetail ){    utf8_printf(stderr, "OPTIONS include:\n%s", zOptions);  }else{    raw_printf(stderr, "Use the -help option for additional information\n");  }  exit(1);}static void verify_uninitialized(void){  if( sqlite3_config(-1)==SQLITE_MISUSE ){    utf8_printf(stdout, "WARNING: attempt to configure SQLite after"                        " initialization.\n");  }}static void main_init(ShellState *data) {  memset(data, 0, sizeof(*data));  data->normalMode = data->cMode = data->mode = MODE_List;  data->autoExplain = 1;  data->pAuxDb = &data->aAuxDb[0];  memcpy(data->colSeparator,SEP_Column, 2);  memcpy(data->rowSeparator,SEP_Row, 2);  data->showHeader = 0;  data->shellFlgs = SHFLG_Lookaside;  verify_uninitialized();  sqlite3_config(SQLITE_CONFIG_URI, 1);  sqlite3_config(SQLITE_CONFIG_LOG, shellLog, data);  sqlite3_config(SQLITE_CONFIG_MULTITHREAD);  sqlite3_snprintf(sizeof(mainPrompt), mainPrompt,"sqlite> ");  sqlite3_snprintf(sizeof(continuePrompt), continuePrompt,"   ...> ");}#ifdef _WIN32static void printBold(const char *zText){#if !SQLITE_OS_WINRT  HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE);  CONSOLE_SCREEN_BUFFER_INFO defaultScreenInfo;  GetConsoleScreenBufferInfo(out, &defaultScreenInfo);  SetConsoleTextAttribute(out,         FOREGROUND_RED|FOREGROUND_INTENSITY  );#endif  printf("%s", zText);#if !SQLITE_OS_WINRT  SetConsoleTextAttribute(out, defaultScreenInfo.wAttributes);#endif}#elsestatic void printBold(const char *zText){  printf("\033[1m%s\033[0m", zText);}#endifstatic char *cmdline_option_value(int argc, char **argv, int i){  if( i==argc ){    utf8_printf(stderr, "%s: Error: missing argument to %s\n",            argv[0], argv[argc-1]);    exit(1);  }  return argv[i];}#ifndef SQLITE_SHELL_IS_UTF8#  if (defined(_WIN32) || defined(WIN32)) \   && (defined(_MSC_VER) || (defined(UNICODE) && defined(__GNUC__)))#    define SQLITE_SHELL_IS_UTF8          (0)#  else#    define SQLITE_SHELL_IS_UTF8          (1)#  endif#endif#if SQLITE_SHELL_IS_UTF8int SQLITE_CDECL main(int argc, char **argv){#elseint SQLITE_CDECL wmain(int argc, wchar_t **wargv){  char **argv;#endif  char *zErrMsg = 0;  ShellState data;  const char *zInitFile = 0;  int i;  int rc = 0;  int warnInmemoryDb = 0;  int readStdin = 1;  int nCmd = 0;  char **azCmd = 0;  const char *zVfs = 0;           #if !SQLITE_SHELL_IS_UTF8  char **argvToFree = 0;  int argcToFree = 0;#endif  setBinaryMode(stdin, 0);  setvbuf(stderr, 0, _IONBF, 0);   stdin_is_interactive = isatty(0);  stdout_is_console = isatty(1);#if !defined(_WIN32_WCE)  if( getenv("SQLITE_DEBUG_BREAK") ){    if( isatty(0) && isatty(2) ){      fprintf(stderr,          "attach debugger to process %d and press any key to continue.\n",          GETPID());      fgetc(stdin);    }else{#if defined(_WIN32) || defined(WIN32)#if SQLITE_OS_WINRT      __debugbreak();#else      DebugBreak();#endif#elif defined(SIGTRAP)      raise(SIGTRAP);#endif    }  }#endif#if USE_SYSTEM_SQLITE+0!=1  if( strncmp(sqlite3_sourceid(),SQLITE_SOURCE_ID,60)!=0 ){    utf8_printf(stderr, "SQLite header and source version mismatch\n%s\n%s\n",            sqlite3_sourceid(), SQLITE_SOURCE_ID);    exit(1);  }#endif  main_init(&data);  #if !SQLITE_SHELL_IS_UTF8  sqlite3_initialize();  argvToFree = malloc(sizeof(argv[0])*argc*2);  shell_check_oom(argvToFree);  argcToFree = argc;  argv = argvToFree + argc;  for(i=0; i<argc; i++){    char *z = sqlite3_win32_unicode_to_utf8(wargv[i]);    int n;    shell_check_oom(z);    n = (int)strlen(z);    argv[i] = malloc( n+1 );    shell_check_oom(argv[i]);    memcpy(argv[i], z, n+1);    argvToFree[i] = argv[i];    sqlite3_free(z);  }  sqlite3_shutdown();#endif  assert( argc>=1 && argv && argv[0] );  Argv0 = argv[0];  #ifdef SIGINT  signal(SIGINT, interrupt_handler);#elif (defined(_WIN32) || defined(WIN32)) && !defined(_WIN32_WCE)  SetConsoleCtrlHandler(ConsoleCtrlHandler, TRUE);#endif#ifdef SQLITE_SHELL_DBNAME_PROC  {        extern void SQLITE_SHELL_DBNAME_PROC(const char**);    SQLITE_SHELL_DBNAME_PROC(&data.pAuxDb->zDbFilename);    warnInmemoryDb = 0;  }#endif    verify_uninitialized();  for(i=1; i<argc; i++){    char *z;    z = argv[i];    if( z[0]!='-' ){      if( data.aAuxDb->zDbFilename==0 ){        data.aAuxDb->zDbFilename = z;      }else{                readStdin = 0;        nCmd++;        azCmd = realloc(azCmd, sizeof(azCmd[0])*nCmd);        shell_check_oom(azCmd);        azCmd[nCmd-1] = z;      }    }    if( z[1]=='-' ) z++;    if( strcmp(z,"-separator")==0     || strcmp(z,"-nullvalue")==0     || strcmp(z,"-newline")==0     || strcmp(z,"-cmd")==0    ){      (void)cmdline_option_value(argc, argv, ++i);    }else if( strcmp(z,"-init")==0 ){      zInitFile = cmdline_option_value(argc, argv, ++i);    }else if( strcmp(z,"-batch")==0 ){            stdin_is_interactive = 0;    }else if( strcmp(z,"-heap")==0 ){#if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)      const char *zSize;      sqlite3_int64 szHeap;      zSize = cmdline_option_value(argc, argv, ++i);      szHeap = integerValue(zSize);      if( szHeap>0x7fff0000 ) szHeap = 0x7fff0000;      sqlite3_config(SQLITE_CONFIG_HEAP, malloc((int)szHeap), (int)szHeap, 64);#else      (void)cmdline_option_value(argc, argv, ++i);#endif    }else if( strcmp(z,"-pagecache")==0 ){      sqlite3_int64 n, sz;      sz = integerValue(cmdline_option_value(argc,argv,++i));      if( sz>70000 ) sz = 70000;      if( sz<0 ) sz = 0;      n = integerValue(cmdline_option_value(argc,argv,++i));      if( sz>0 && n>0 && 0xffffffffffffLL/sz<n ){        n = 0xffffffffffffLL/sz;      }      sqlite3_config(SQLITE_CONFIG_PAGECACHE,                    (n>0 && sz>0) ? malloc(n*sz) : 0, sz, n);      data.shellFlgs |= SHFLG_Pagecache;    }else if( strcmp(z,"-lookaside")==0 ){      int n, sz;      sz = (int)integerValue(cmdline_option_value(argc,argv,++i));      if( sz<0 ) sz = 0;      n = (int)integerValue(cmdline_option_value(argc,argv,++i));      if( n<0 ) n = 0;      sqlite3_config(SQLITE_CONFIG_LOOKASIDE, sz, n);      if( sz*n==0 ) data.shellFlgs &= ~SHFLG_Lookaside;    }else if( strcmp(z,"-threadsafe")==0 ){      int n;      n = (int)integerValue(cmdline_option_value(argc,argv,++i));      switch( n ){         case 0:  sqlite3_config(SQLITE_CONFIG_SINGLETHREAD);  break;         case 2:  sqlite3_config(SQLITE_CONFIG_MULTITHREAD);   break;         default: sqlite3_config(SQLITE_CONFIG_SERIALIZED);    break;      }#ifdef SQLITE_ENABLE_VFSTRACE    }else if( strcmp(z,"-vfstrace")==0 ){      extern int vfstrace_register(         const char *zTraceName,         const char *zOldVfsName,         int (*xOut)(const char*,void*),         void *pOutArg,         int makeDefault      );      vfstrace_register("trace",0,(int(*)(const char*,void*))fputs,stderr,1);#endif#ifdef SQLITE_ENABLE_MULTIPLEX    }else if( strcmp(z,"-multiplex")==0 ){      extern int sqlite3_multiple_initialize(const char*,int);      sqlite3_multiplex_initialize(0, 1);#endif    }else if( strcmp(z,"-mmap")==0 ){      sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i));      sqlite3_config(SQLITE_CONFIG_MMAP_SIZE, sz, sz);#ifdef SQLITE_ENABLE_SORTER_REFERENCES    }else if( strcmp(z,"-sorterref")==0 ){      sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i));      sqlite3_config(SQLITE_CONFIG_SORTERREF_SIZE, (int)sz);#endif    }else if( strcmp(z,"-vfs")==0 ){      zVfs = cmdline_option_value(argc, argv, ++i);#ifdef SQLITE_HAVE_ZLIB    }else if( strcmp(z,"-zip")==0 ){      data.openMode = SHELL_OPEN_ZIPFILE;#endif    }else if( strcmp(z,"-append")==0 ){      data.openMode = SHELL_OPEN_APPENDVFS;#ifndef SQLITE_OMIT_DESERIALIZE    }else if( strcmp(z,"-deserialize")==0 ){      data.openMode = SHELL_OPEN_DESERIALIZE;    }else if( strcmp(z,"-maxsize")==0 && i+1<argc ){      data.szMax = integerValue(argv[++i]);#endif    }else if( strcmp(z,"-readonly")==0 ){      data.openMode = SHELL_OPEN_READONLY;    }else if( strcmp(z,"-nofollow")==0 ){      data.openFlags = SQLITE_OPEN_NOFOLLOW;#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)    }else if( strncmp(z, "-A",2)==0 ){            break;#endif    }else if( strcmp(z, "-memtrace")==0 ){      sqlite3MemTraceActivate(stderr);    }else if( strcmp(z,"-bail")==0 ){      bail_on_error = 1;    }else if( strcmp(z,"-nonce")==0 ){      free(data.zNonce);      data.zNonce = strdup(argv[++i]);    }else if( strcmp(z,"-safe")==0 ){          }  }  verify_uninitialized();#ifdef SQLITE_SHELL_INIT_PROC  {        extern void SQLITE_SHELL_INIT_PROC(void);    SQLITE_SHELL_INIT_PROC();  }#else    sqlite3_initialize();#endif  if( zVfs ){    sqlite3_vfs *pVfs = sqlite3_vfs_find(zVfs);    if( pVfs ){      sqlite3_vfs_register(pVfs, 1);    }else{      utf8_printf(stderr, "no such VFS: \"%s\"\n", argv[i]);      exit(1);    }  }  if( data.pAuxDb->zDbFilename==0 ){#ifndef SQLITE_OMIT_MEMORYDB    data.pAuxDb->zDbFilename = ":memory:";    warnInmemoryDb = argc==1;#else    utf8_printf(stderr,"%s: Error: no database filename specified\n", Argv0);    return 1;#endif  }  data.out = stdout;  sqlite3_appendvfs_init(0,0,0);    if( access(data.pAuxDb->zDbFilename, 0)==0 ){    open_db(&data, 0);  }    process_sqliterc(&data,zInitFile);    for(i=1; i<argc; i++){    char *z = argv[i];    if( z[0]!='-' ) continue;    if( z[1]=='-' ){ z++; }    if( strcmp(z,"-init")==0 ){      i++;    }else if( strcmp(z,"-html")==0 ){      data.mode = MODE_Html;    }else if( strcmp(z,"-list")==0 ){      data.mode = MODE_List;    }else if( strcmp(z,"-quote")==0 ){      data.mode = MODE_Quote;      sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator, SEP_Comma);      sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator, SEP_Row);    }else if( strcmp(z,"-line")==0 ){      data.mode = MODE_Line;    }else if( strcmp(z,"-column")==0 ){      data.mode = MODE_Column;    }else if( strcmp(z,"-json")==0 ){      data.mode = MODE_Json;    }else if( strcmp(z,"-markdown")==0 ){      data.mode = MODE_Markdown;    }else if( strcmp(z,"-table")==0 ){      data.mode = MODE_Table;    }else if( strcmp(z,"-box")==0 ){      data.mode = MODE_Box;    }else if( strcmp(z,"-csv")==0 ){      data.mode = MODE_Csv;      memcpy(data.colSeparator,",",2);#ifdef SQLITE_HAVE_ZLIB    }else if( strcmp(z,"-zip")==0 ){      data.openMode = SHELL_OPEN_ZIPFILE;#endif    }else if( strcmp(z,"-append")==0 ){      data.openMode = SHELL_OPEN_APPENDVFS;#ifndef SQLITE_OMIT_DESERIALIZE    }else if( strcmp(z,"-deserialize")==0 ){      data.openMode = SHELL_OPEN_DESERIALIZE;    }else if( strcmp(z,"-maxsize")==0 && i+1<argc ){      data.szMax = integerValue(argv[++i]);#endif    }else if( strcmp(z,"-readonly")==0 ){      data.openMode = SHELL_OPEN_READONLY;    }else if( strcmp(z,"-nofollow")==0 ){      data.openFlags |= SQLITE_OPEN_NOFOLLOW;    }else if( strcmp(z,"-ascii")==0 ){      data.mode = MODE_Ascii;      sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator, SEP_Unit);      sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator, SEP_Record);    }else if( strcmp(z,"-tabs")==0 ){      data.mode = MODE_List;      sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator, SEP_Tab);      sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator, SEP_Row);    }else if( strcmp(z,"-separator")==0 ){      sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator,                       "%s",cmdline_option_value(argc,argv,++i));    }else if( strcmp(z,"-newline")==0 ){      sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator,                       "%s",cmdline_option_value(argc,argv,++i));    }else if( strcmp(z,"-nullvalue")==0 ){      sqlite3_snprintf(sizeof(data.nullValue), data.nullValue,                       "%s",cmdline_option_value(argc,argv,++i));    }else if( strcmp(z,"-header")==0 ){      data.showHeader = 1;      ShellSetFlag(&data, SHFLG_HeaderSet);     }else if( strcmp(z,"-noheader")==0 ){      data.showHeader = 0;      ShellSetFlag(&data, SHFLG_HeaderSet);    }else if( strcmp(z,"-echo")==0 ){      ShellSetFlag(&data, SHFLG_Echo);    }else if( strcmp(z,"-eqp")==0 ){      data.autoEQP = AUTOEQP_on;    }else if( strcmp(z,"-eqpfull")==0 ){      data.autoEQP = AUTOEQP_full;    }else if( strcmp(z,"-stats")==0 ){      data.statsOn = 1;    }else if( strcmp(z,"-scanstats")==0 ){      data.scanstatsOn = 1;    }else if( strcmp(z,"-backslash")==0 ){            ShellSetFlag(&data, SHFLG_Backslash);    }else if( strcmp(z,"-bail")==0 ){          }else if( strcmp(z,"-version")==0 ){      printf("%s %s\n", sqlite3_libversion(), sqlite3_sourceid());      return 0;    }else if( strcmp(z,"-interactive")==0 ){      stdin_is_interactive = 1;    }else if( strcmp(z,"-batch")==0 ){      stdin_is_interactive = 0;    }else if( strcmp(z,"-heap")==0 ){      i++;    }else if( strcmp(z,"-pagecache")==0 ){      i+=2;    }else if( strcmp(z,"-lookaside")==0 ){      i+=2;    }else if( strcmp(z,"-threadsafe")==0 ){      i+=2;    }else if( strcmp(z,"-nonce")==0 ){      i += 2;    }else if( strcmp(z,"-mmap")==0 ){      i++;    }else if( strcmp(z,"-memtrace")==0 ){      i++;#ifdef SQLITE_ENABLE_SORTER_REFERENCES    }else if( strcmp(z,"-sorterref")==0 ){      i++;#endif    }else if( strcmp(z,"-vfs")==0 ){      i++;#ifdef SQLITE_ENABLE_VFSTRACE    }else if( strcmp(z,"-vfstrace")==0 ){      i++;#endif#ifdef SQLITE_ENABLE_MULTIPLEX    }else if( strcmp(z,"-multiplex")==0 ){      i++;#endif    }else if( strcmp(z,"-help")==0 ){      usage(1);    }else if( strcmp(z,"-cmd")==0 ){            if( i==argc-1 ) break;      z = cmdline_option_value(argc,argv,++i);      if( z[0]=='.' ){        rc = do_meta_command(z, &data);        if( rc && bail_on_error ) return rc==2 ? 0 : rc;      }else{        open_db(&data, 0);        rc = shell_exec(&data, z, &zErrMsg);        if( zErrMsg!=0 ){          utf8_printf(stderr,"Error: %s\n", zErrMsg);          if( bail_on_error ) return rc!=0 ? rc : 1;        }else if( rc!=0 ){          utf8_printf(stderr,"Error: unable to process SQL \"%s\"\n", z);          if( bail_on_error ) return rc;        }      }#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)    }else if( strncmp(z, "-A", 2)==0 ){      if( nCmd>0 ){        utf8_printf(stderr, "Error: cannot mix regular SQL or dot-commands"                            " with \"%s\"\n", z);        return 1;      }      open_db(&data, OPEN_DB_ZIPFILE);      if( z[2] ){        argv[i] = &z[2];        arDotCommand(&data, 1, argv+(i-1), argc-(i-1));      }else{        arDotCommand(&data, 1, argv+i, argc-i);      }      readStdin = 0;      break;#endif    }else if( strcmp(z,"-safe")==0 ){      data.bSafeMode = data.bSafeModePersist = 1;    }else{      utf8_printf(stderr,"%s: Error: unknown option: %s\n", Argv0, z);      raw_printf(stderr,"Use -help for a list of options.\n");      return 1;    }    data.cMode = data.mode;  }  if( !readStdin ){        for(i=0; i<nCmd; i++){      if( azCmd[i][0]=='.' ){        rc = do_meta_command(azCmd[i], &data);        if( rc ){          free(azCmd);          return rc==2 ? 0 : rc;        }      }else{        open_db(&data, 0);        rc = shell_exec(&data, azCmd[i], &zErrMsg);        if( zErrMsg || rc ){          if( zErrMsg!=0 ){            utf8_printf(stderr,"Error: %s\n", zErrMsg);          }else{            utf8_printf(stderr,"Error: unable to process SQL: %s\n", azCmd[i]);          }          sqlite3_free(zErrMsg);          free(azCmd);          return rc!=0 ? rc : 1;        }      }    }  }else{        if( stdin_is_interactive ){      char *zHome;      char *zHistory;      int nHistory;      printf(        "SQLite version %s %.19s\n"         "Enter \".help\" for usage hints.\n",        sqlite3_libversion(), sqlite3_sourceid()      );      if( warnInmemoryDb ){        printf("Connected to a ");        printBold("transient in-memory database");        printf(".\nUse \".open FILENAME\" to reopen on a "               "persistent database.\n");      }      zHistory = getenv("SQLITE_HISTORY");      if( zHistory ){        zHistory = strdup(zHistory);      }else if( (zHome = find_home_dir(0))!=0 ){        nHistory = strlen30(zHome) + 20;        if( (zHistory = malloc(nHistory))!=0 ){          sqlite3_snprintf(nHistory, zHistory,"%s/.sqlite_history", zHome);        }      }      if( zHistory ){ shell_read_history(zHistory); }#if HAVE_READLINE || HAVE_EDITLINE      rl_attempted_completion_function = readline_completion;#elif HAVE_LINENOISE      linenoiseSetCompletionCallback(linenoise_completion);#endif      data.in = 0;      rc = process_input(&data);      if( zHistory ){        shell_stifle_history(2000);        shell_write_history(zHistory);        free(zHistory);      }    }else{      data.in = stdin;      rc = process_input(&data);    }  }  free(azCmd);  set_table_name(&data, 0);  if( data.db ){    session_close_all(&data, -1);    close_db(data.db);  }  for(i=0; i<ArraySize(data.aAuxDb); i++){    sqlite3_free(data.aAuxDb[i].zFreeOnClose);    if( data.aAuxDb[i].db ){      session_close_all(&data, i);      close_db(data.aAuxDb[i].db);    }  }  find_home_dir(1);  output_reset(&data);  data.doXdgOpen = 0;  clearTempFile(&data);#if !SQLITE_SHELL_IS_UTF8  for(i=0; i<argcToFree; i++) free(argvToFree[i]);  free(argvToFree);#endif  free(data.colWidth);  free(data.zNonce);    memset(&data, 0, sizeof(data));  return rc;}
 |