xmlschemastypes.c 176 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281
  1. /*
  2. * schemastypes.c : implementation of the XML Schema Datatypes
  3. * definition and validity checking
  4. *
  5. * See Copyright for the status of this software.
  6. *
  7. * Daniel Veillard <veillard@redhat.com>
  8. */
  9. /* To avoid EBCDIC trouble when parsing on zOS */
  10. #if defined(__MVS__)
  11. #pragma convert("ISO8859-1")
  12. #endif
  13. #define IN_LIBXML
  14. #include "libxml.h"
  15. #ifdef LIBXML_SCHEMAS_ENABLED
  16. #include <string.h>
  17. #include <libxml/xmlmemory.h>
  18. #include <libxml/parser.h>
  19. #include <libxml/parserInternals.h>
  20. #include <libxml/hash.h>
  21. #include <libxml/valid.h>
  22. #include <libxml/xpath.h>
  23. #include <libxml/uri.h>
  24. #include <libxml/xmlschemas.h>
  25. #include <libxml/schemasInternals.h>
  26. #include <libxml/xmlschemastypes.h>
  27. #ifdef HAVE_MATH_H
  28. #include <math.h>
  29. #endif
  30. #ifdef HAVE_FLOAT_H
  31. #include <float.h>
  32. #endif
  33. #define DEBUG
  34. #ifndef LIBXML_XPATH_ENABLED
  35. extern double xmlXPathNAN;
  36. extern double xmlXPathPINF;
  37. extern double xmlXPathNINF;
  38. #endif
  39. #define TODO \
  40. xmlGenericError(xmlGenericErrorContext, \
  41. "Unimplemented block at %s:%d\n", \
  42. __FILE__, __LINE__);
  43. #define XML_SCHEMAS_NAMESPACE_NAME \
  44. (const xmlChar *)"http://www.w3.org/2001/XMLSchema"
  45. #define IS_WSP_REPLACE_CH(c) ((((c) == 0x9) || ((c) == 0xa)) || \
  46. ((c) == 0xd))
  47. #define IS_WSP_SPACE_CH(c) ((c) == 0x20)
  48. #define IS_WSP_BLANK_CH(c) IS_BLANK_CH(c)
  49. /* Date value */
  50. typedef struct _xmlSchemaValDate xmlSchemaValDate;
  51. typedef xmlSchemaValDate *xmlSchemaValDatePtr;
  52. struct _xmlSchemaValDate {
  53. long year;
  54. unsigned int mon :4; /* 1 <= mon <= 12 */
  55. unsigned int day :5; /* 1 <= day <= 31 */
  56. unsigned int hour :5; /* 0 <= hour <= 24 */
  57. unsigned int min :6; /* 0 <= min <= 59 */
  58. double sec;
  59. unsigned int tz_flag :1; /* is tzo explicitly set? */
  60. signed int tzo :12; /* -1440 <= tzo <= 1440;
  61. currently only -840 to +840 are needed */
  62. };
  63. /* Duration value */
  64. typedef struct _xmlSchemaValDuration xmlSchemaValDuration;
  65. typedef xmlSchemaValDuration *xmlSchemaValDurationPtr;
  66. struct _xmlSchemaValDuration {
  67. long mon; /* mon stores years also */
  68. long day;
  69. double sec; /* sec stores min and hour also */
  70. };
  71. typedef struct _xmlSchemaValDecimal xmlSchemaValDecimal;
  72. typedef xmlSchemaValDecimal *xmlSchemaValDecimalPtr;
  73. struct _xmlSchemaValDecimal {
  74. /* would use long long but not portable */
  75. unsigned long lo;
  76. unsigned long mi;
  77. unsigned long hi;
  78. unsigned int extra;
  79. unsigned int sign:1;
  80. unsigned int frac:7;
  81. unsigned int total:8;
  82. };
  83. typedef struct _xmlSchemaValQName xmlSchemaValQName;
  84. typedef xmlSchemaValQName *xmlSchemaValQNamePtr;
  85. struct _xmlSchemaValQName {
  86. xmlChar *name;
  87. xmlChar *uri;
  88. };
  89. typedef struct _xmlSchemaValHex xmlSchemaValHex;
  90. typedef xmlSchemaValHex *xmlSchemaValHexPtr;
  91. struct _xmlSchemaValHex {
  92. xmlChar *str;
  93. unsigned int total;
  94. };
  95. typedef struct _xmlSchemaValBase64 xmlSchemaValBase64;
  96. typedef xmlSchemaValBase64 *xmlSchemaValBase64Ptr;
  97. struct _xmlSchemaValBase64 {
  98. xmlChar *str;
  99. unsigned int total;
  100. };
  101. struct _xmlSchemaVal {
  102. xmlSchemaValType type;
  103. struct _xmlSchemaVal *next;
  104. union {
  105. xmlSchemaValDecimal decimal;
  106. xmlSchemaValDate date;
  107. xmlSchemaValDuration dur;
  108. xmlSchemaValQName qname;
  109. xmlSchemaValHex hex;
  110. xmlSchemaValBase64 base64;
  111. float f;
  112. double d;
  113. int b;
  114. xmlChar *str;
  115. } value;
  116. };
  117. static int xmlSchemaTypesInitialized = 0;
  118. static xmlHashTablePtr xmlSchemaTypesBank = NULL;
  119. /*
  120. * Basic types
  121. */
  122. static xmlSchemaTypePtr xmlSchemaTypeStringDef = NULL;
  123. static xmlSchemaTypePtr xmlSchemaTypeAnyTypeDef = NULL;
  124. static xmlSchemaTypePtr xmlSchemaTypeAnySimpleTypeDef = NULL;
  125. static xmlSchemaTypePtr xmlSchemaTypeDecimalDef = NULL;
  126. static xmlSchemaTypePtr xmlSchemaTypeDatetimeDef = NULL;
  127. static xmlSchemaTypePtr xmlSchemaTypeDateDef = NULL;
  128. static xmlSchemaTypePtr xmlSchemaTypeTimeDef = NULL;
  129. static xmlSchemaTypePtr xmlSchemaTypeGYearDef = NULL;
  130. static xmlSchemaTypePtr xmlSchemaTypeGYearMonthDef = NULL;
  131. static xmlSchemaTypePtr xmlSchemaTypeGDayDef = NULL;
  132. static xmlSchemaTypePtr xmlSchemaTypeGMonthDayDef = NULL;
  133. static xmlSchemaTypePtr xmlSchemaTypeGMonthDef = NULL;
  134. static xmlSchemaTypePtr xmlSchemaTypeDurationDef = NULL;
  135. static xmlSchemaTypePtr xmlSchemaTypeFloatDef = NULL;
  136. static xmlSchemaTypePtr xmlSchemaTypeBooleanDef = NULL;
  137. static xmlSchemaTypePtr xmlSchemaTypeDoubleDef = NULL;
  138. static xmlSchemaTypePtr xmlSchemaTypeHexBinaryDef = NULL;
  139. static xmlSchemaTypePtr xmlSchemaTypeBase64BinaryDef = NULL;
  140. static xmlSchemaTypePtr xmlSchemaTypeAnyURIDef = NULL;
  141. /*
  142. * Derived types
  143. */
  144. static xmlSchemaTypePtr xmlSchemaTypePositiveIntegerDef = NULL;
  145. static xmlSchemaTypePtr xmlSchemaTypeNonPositiveIntegerDef = NULL;
  146. static xmlSchemaTypePtr xmlSchemaTypeNegativeIntegerDef = NULL;
  147. static xmlSchemaTypePtr xmlSchemaTypeNonNegativeIntegerDef = NULL;
  148. static xmlSchemaTypePtr xmlSchemaTypeIntegerDef = NULL;
  149. static xmlSchemaTypePtr xmlSchemaTypeLongDef = NULL;
  150. static xmlSchemaTypePtr xmlSchemaTypeIntDef = NULL;
  151. static xmlSchemaTypePtr xmlSchemaTypeShortDef = NULL;
  152. static xmlSchemaTypePtr xmlSchemaTypeByteDef = NULL;
  153. static xmlSchemaTypePtr xmlSchemaTypeUnsignedLongDef = NULL;
  154. static xmlSchemaTypePtr xmlSchemaTypeUnsignedIntDef = NULL;
  155. static xmlSchemaTypePtr xmlSchemaTypeUnsignedShortDef = NULL;
  156. static xmlSchemaTypePtr xmlSchemaTypeUnsignedByteDef = NULL;
  157. static xmlSchemaTypePtr xmlSchemaTypeNormStringDef = NULL;
  158. static xmlSchemaTypePtr xmlSchemaTypeTokenDef = NULL;
  159. static xmlSchemaTypePtr xmlSchemaTypeLanguageDef = NULL;
  160. static xmlSchemaTypePtr xmlSchemaTypeNameDef = NULL;
  161. static xmlSchemaTypePtr xmlSchemaTypeQNameDef = NULL;
  162. static xmlSchemaTypePtr xmlSchemaTypeNCNameDef = NULL;
  163. static xmlSchemaTypePtr xmlSchemaTypeIdDef = NULL;
  164. static xmlSchemaTypePtr xmlSchemaTypeIdrefDef = NULL;
  165. static xmlSchemaTypePtr xmlSchemaTypeIdrefsDef = NULL;
  166. static xmlSchemaTypePtr xmlSchemaTypeEntityDef = NULL;
  167. static xmlSchemaTypePtr xmlSchemaTypeEntitiesDef = NULL;
  168. static xmlSchemaTypePtr xmlSchemaTypeNotationDef = NULL;
  169. static xmlSchemaTypePtr xmlSchemaTypeNmtokenDef = NULL;
  170. static xmlSchemaTypePtr xmlSchemaTypeNmtokensDef = NULL;
  171. /************************************************************************
  172. * *
  173. * Datatype error handlers *
  174. * *
  175. ************************************************************************/
  176. /**
  177. * xmlSchemaTypeErrMemory:
  178. * @extra: extra information
  179. *
  180. * Handle an out of memory condition
  181. */
  182. static void
  183. xmlSchemaTypeErrMemory(xmlNodePtr node, const char *extra)
  184. {
  185. __xmlSimpleError(XML_FROM_DATATYPE, XML_ERR_NO_MEMORY, node, NULL, extra);
  186. }
  187. /************************************************************************
  188. * *
  189. * Base types support *
  190. * *
  191. ************************************************************************/
  192. /**
  193. * xmlSchemaNewValue:
  194. * @type: the value type
  195. *
  196. * Allocate a new simple type value
  197. *
  198. * Returns a pointer to the new value or NULL in case of error
  199. */
  200. static xmlSchemaValPtr
  201. xmlSchemaNewValue(xmlSchemaValType type) {
  202. xmlSchemaValPtr value;
  203. value = (xmlSchemaValPtr) xmlMalloc(sizeof(xmlSchemaVal));
  204. if (value == NULL) {
  205. return(NULL);
  206. }
  207. memset(value, 0, sizeof(xmlSchemaVal));
  208. value->type = type;
  209. return(value);
  210. }
  211. static xmlSchemaFacetPtr
  212. xmlSchemaNewMinLengthFacet(int value)
  213. {
  214. xmlSchemaFacetPtr ret;
  215. ret = xmlSchemaNewFacet();
  216. if (ret == NULL) {
  217. return(NULL);
  218. }
  219. ret->type = XML_SCHEMA_FACET_MINLENGTH;
  220. ret->val = xmlSchemaNewValue(XML_SCHEMAS_NNINTEGER);
  221. if (ret->val == NULL) {
  222. xmlFree(ret);
  223. return(NULL);
  224. }
  225. ret->val->value.decimal.lo = value;
  226. return (ret);
  227. }
  228. /*
  229. * xmlSchemaInitBasicType:
  230. * @name: the type name
  231. * @type: the value type associated
  232. *
  233. * Initialize one primitive built-in type
  234. */
  235. static xmlSchemaTypePtr
  236. xmlSchemaInitBasicType(const char *name, xmlSchemaValType type,
  237. xmlSchemaTypePtr baseType) {
  238. xmlSchemaTypePtr ret;
  239. ret = (xmlSchemaTypePtr) xmlMalloc(sizeof(xmlSchemaType));
  240. if (ret == NULL) {
  241. xmlSchemaTypeErrMemory(NULL, "could not initialize basic types");
  242. return(NULL);
  243. }
  244. memset(ret, 0, sizeof(xmlSchemaType));
  245. ret->name = (const xmlChar *)name;
  246. ret->targetNamespace = XML_SCHEMAS_NAMESPACE_NAME;
  247. ret->type = XML_SCHEMA_TYPE_BASIC;
  248. ret->baseType = baseType;
  249. ret->contentType = XML_SCHEMA_CONTENT_BASIC;
  250. /*
  251. * Primitive types.
  252. */
  253. switch (type) {
  254. case XML_SCHEMAS_STRING:
  255. case XML_SCHEMAS_DECIMAL:
  256. case XML_SCHEMAS_DATE:
  257. case XML_SCHEMAS_DATETIME:
  258. case XML_SCHEMAS_TIME:
  259. case XML_SCHEMAS_GYEAR:
  260. case XML_SCHEMAS_GYEARMONTH:
  261. case XML_SCHEMAS_GMONTH:
  262. case XML_SCHEMAS_GMONTHDAY:
  263. case XML_SCHEMAS_GDAY:
  264. case XML_SCHEMAS_DURATION:
  265. case XML_SCHEMAS_FLOAT:
  266. case XML_SCHEMAS_DOUBLE:
  267. case XML_SCHEMAS_BOOLEAN:
  268. case XML_SCHEMAS_ANYURI:
  269. case XML_SCHEMAS_HEXBINARY:
  270. case XML_SCHEMAS_BASE64BINARY:
  271. case XML_SCHEMAS_QNAME:
  272. case XML_SCHEMAS_NOTATION:
  273. ret->flags |= XML_SCHEMAS_TYPE_BUILTIN_PRIMITIVE;
  274. break;
  275. default:
  276. break;
  277. }
  278. /*
  279. * Set variety.
  280. */
  281. switch (type) {
  282. case XML_SCHEMAS_ANYTYPE:
  283. case XML_SCHEMAS_ANYSIMPLETYPE:
  284. break;
  285. case XML_SCHEMAS_IDREFS:
  286. case XML_SCHEMAS_NMTOKENS:
  287. case XML_SCHEMAS_ENTITIES:
  288. ret->flags |= XML_SCHEMAS_TYPE_VARIETY_LIST;
  289. ret->facets = xmlSchemaNewMinLengthFacet(1);
  290. ret->flags |= XML_SCHEMAS_TYPE_HAS_FACETS;
  291. break;
  292. default:
  293. ret->flags |= XML_SCHEMAS_TYPE_VARIETY_ATOMIC;
  294. break;
  295. }
  296. xmlHashAddEntry2(xmlSchemaTypesBank, ret->name,
  297. XML_SCHEMAS_NAMESPACE_NAME, ret);
  298. ret->builtInType = type;
  299. return(ret);
  300. }
  301. /*
  302. * WARNING: Those type reside normally in xmlschemas.c but are
  303. * redefined here locally in oder of being able to use them for xs:anyType-
  304. * TODO: Remove those definition if we move the types to a header file.
  305. * TODO: Always keep those structs up-to-date with the originals.
  306. */
  307. #define UNBOUNDED (1 << 30)
  308. typedef struct _xmlSchemaTreeItem xmlSchemaTreeItem;
  309. typedef xmlSchemaTreeItem *xmlSchemaTreeItemPtr;
  310. struct _xmlSchemaTreeItem {
  311. xmlSchemaTypeType type;
  312. xmlSchemaAnnotPtr annot;
  313. xmlSchemaTreeItemPtr next;
  314. xmlSchemaTreeItemPtr children;
  315. };
  316. typedef struct _xmlSchemaParticle xmlSchemaParticle;
  317. typedef xmlSchemaParticle *xmlSchemaParticlePtr;
  318. struct _xmlSchemaParticle {
  319. xmlSchemaTypeType type;
  320. xmlSchemaAnnotPtr annot;
  321. xmlSchemaTreeItemPtr next;
  322. xmlSchemaTreeItemPtr children;
  323. int minOccurs;
  324. int maxOccurs;
  325. xmlNodePtr node;
  326. };
  327. typedef struct _xmlSchemaModelGroup xmlSchemaModelGroup;
  328. typedef xmlSchemaModelGroup *xmlSchemaModelGroupPtr;
  329. struct _xmlSchemaModelGroup {
  330. xmlSchemaTypeType type;
  331. xmlSchemaAnnotPtr annot;
  332. xmlSchemaTreeItemPtr next;
  333. xmlSchemaTreeItemPtr children;
  334. xmlNodePtr node;
  335. };
  336. static xmlSchemaParticlePtr
  337. xmlSchemaAddParticle(void)
  338. {
  339. xmlSchemaParticlePtr ret = NULL;
  340. ret = (xmlSchemaParticlePtr)
  341. xmlMalloc(sizeof(xmlSchemaParticle));
  342. if (ret == NULL) {
  343. xmlSchemaTypeErrMemory(NULL, "allocating particle component");
  344. return (NULL);
  345. }
  346. memset(ret, 0, sizeof(xmlSchemaParticle));
  347. ret->type = XML_SCHEMA_TYPE_PARTICLE;
  348. ret->minOccurs = 1;
  349. ret->maxOccurs = 1;
  350. return (ret);
  351. }
  352. /*
  353. * xmlSchemaInitTypes:
  354. *
  355. * Initialize the default XML Schemas type library
  356. */
  357. void
  358. xmlSchemaInitTypes(void)
  359. {
  360. if (xmlSchemaTypesInitialized != 0)
  361. return;
  362. xmlSchemaTypesBank = xmlHashCreate(40);
  363. /*
  364. * 3.4.7 Built-in Complex Type Definition
  365. */
  366. xmlSchemaTypeAnyTypeDef = xmlSchemaInitBasicType("anyType",
  367. XML_SCHEMAS_ANYTYPE,
  368. NULL);
  369. xmlSchemaTypeAnyTypeDef->baseType = xmlSchemaTypeAnyTypeDef;
  370. xmlSchemaTypeAnyTypeDef->contentType = XML_SCHEMA_CONTENT_MIXED;
  371. /*
  372. * Init the content type.
  373. */
  374. xmlSchemaTypeAnyTypeDef->contentType = XML_SCHEMA_CONTENT_MIXED;
  375. {
  376. xmlSchemaParticlePtr particle;
  377. xmlSchemaModelGroupPtr sequence;
  378. xmlSchemaWildcardPtr wild;
  379. /* First particle. */
  380. particle = xmlSchemaAddParticle();
  381. if (particle == NULL)
  382. return;
  383. xmlSchemaTypeAnyTypeDef->subtypes = (xmlSchemaTypePtr) particle;
  384. /* Sequence model group. */
  385. sequence = (xmlSchemaModelGroupPtr)
  386. xmlMalloc(sizeof(xmlSchemaModelGroup));
  387. if (sequence == NULL) {
  388. xmlSchemaTypeErrMemory(NULL, "allocating model group component");
  389. return;
  390. }
  391. memset(sequence, 0, sizeof(xmlSchemaModelGroup));
  392. sequence->type = XML_SCHEMA_TYPE_SEQUENCE;
  393. particle->children = (xmlSchemaTreeItemPtr) sequence;
  394. /* Second particle. */
  395. particle = xmlSchemaAddParticle();
  396. if (particle == NULL)
  397. return;
  398. particle->minOccurs = 0;
  399. particle->maxOccurs = UNBOUNDED;
  400. sequence->children = (xmlSchemaTreeItemPtr) particle;
  401. /* The wildcard */
  402. wild = (xmlSchemaWildcardPtr) xmlMalloc(sizeof(xmlSchemaWildcard));
  403. if (wild == NULL) {
  404. xmlSchemaTypeErrMemory(NULL, "allocating wildcard component");
  405. return;
  406. }
  407. memset(wild, 0, sizeof(xmlSchemaWildcard));
  408. wild->type = XML_SCHEMA_TYPE_ANY;
  409. wild->any = 1;
  410. wild->processContents = XML_SCHEMAS_ANY_LAX;
  411. particle->children = (xmlSchemaTreeItemPtr) wild;
  412. /*
  413. * Create the attribute wildcard.
  414. */
  415. wild = (xmlSchemaWildcardPtr) xmlMalloc(sizeof(xmlSchemaWildcard));
  416. if (wild == NULL) {
  417. xmlSchemaTypeErrMemory(NULL, "could not create an attribute "
  418. "wildcard on anyType");
  419. return;
  420. }
  421. memset(wild, 0, sizeof(xmlSchemaWildcard));
  422. wild->any = 1;
  423. wild->processContents = XML_SCHEMAS_ANY_LAX;
  424. xmlSchemaTypeAnyTypeDef->attributeWildcard = wild;
  425. }
  426. xmlSchemaTypeAnySimpleTypeDef = xmlSchemaInitBasicType("anySimpleType",
  427. XML_SCHEMAS_ANYSIMPLETYPE,
  428. xmlSchemaTypeAnyTypeDef);
  429. /*
  430. * primitive datatypes
  431. */
  432. xmlSchemaTypeStringDef = xmlSchemaInitBasicType("string",
  433. XML_SCHEMAS_STRING,
  434. xmlSchemaTypeAnySimpleTypeDef);
  435. xmlSchemaTypeDecimalDef = xmlSchemaInitBasicType("decimal",
  436. XML_SCHEMAS_DECIMAL,
  437. xmlSchemaTypeAnySimpleTypeDef);
  438. xmlSchemaTypeDateDef = xmlSchemaInitBasicType("date",
  439. XML_SCHEMAS_DATE,
  440. xmlSchemaTypeAnySimpleTypeDef);
  441. xmlSchemaTypeDatetimeDef = xmlSchemaInitBasicType("dateTime",
  442. XML_SCHEMAS_DATETIME,
  443. xmlSchemaTypeAnySimpleTypeDef);
  444. xmlSchemaTypeTimeDef = xmlSchemaInitBasicType("time",
  445. XML_SCHEMAS_TIME,
  446. xmlSchemaTypeAnySimpleTypeDef);
  447. xmlSchemaTypeGYearDef = xmlSchemaInitBasicType("gYear",
  448. XML_SCHEMAS_GYEAR,
  449. xmlSchemaTypeAnySimpleTypeDef);
  450. xmlSchemaTypeGYearMonthDef = xmlSchemaInitBasicType("gYearMonth",
  451. XML_SCHEMAS_GYEARMONTH,
  452. xmlSchemaTypeAnySimpleTypeDef);
  453. xmlSchemaTypeGMonthDef = xmlSchemaInitBasicType("gMonth",
  454. XML_SCHEMAS_GMONTH,
  455. xmlSchemaTypeAnySimpleTypeDef);
  456. xmlSchemaTypeGMonthDayDef = xmlSchemaInitBasicType("gMonthDay",
  457. XML_SCHEMAS_GMONTHDAY,
  458. xmlSchemaTypeAnySimpleTypeDef);
  459. xmlSchemaTypeGDayDef = xmlSchemaInitBasicType("gDay",
  460. XML_SCHEMAS_GDAY,
  461. xmlSchemaTypeAnySimpleTypeDef);
  462. xmlSchemaTypeDurationDef = xmlSchemaInitBasicType("duration",
  463. XML_SCHEMAS_DURATION,
  464. xmlSchemaTypeAnySimpleTypeDef);
  465. xmlSchemaTypeFloatDef = xmlSchemaInitBasicType("float",
  466. XML_SCHEMAS_FLOAT,
  467. xmlSchemaTypeAnySimpleTypeDef);
  468. xmlSchemaTypeDoubleDef = xmlSchemaInitBasicType("double",
  469. XML_SCHEMAS_DOUBLE,
  470. xmlSchemaTypeAnySimpleTypeDef);
  471. xmlSchemaTypeBooleanDef = xmlSchemaInitBasicType("boolean",
  472. XML_SCHEMAS_BOOLEAN,
  473. xmlSchemaTypeAnySimpleTypeDef);
  474. xmlSchemaTypeAnyURIDef = xmlSchemaInitBasicType("anyURI",
  475. XML_SCHEMAS_ANYURI,
  476. xmlSchemaTypeAnySimpleTypeDef);
  477. xmlSchemaTypeHexBinaryDef = xmlSchemaInitBasicType("hexBinary",
  478. XML_SCHEMAS_HEXBINARY,
  479. xmlSchemaTypeAnySimpleTypeDef);
  480. xmlSchemaTypeBase64BinaryDef
  481. = xmlSchemaInitBasicType("base64Binary", XML_SCHEMAS_BASE64BINARY,
  482. xmlSchemaTypeAnySimpleTypeDef);
  483. xmlSchemaTypeNotationDef = xmlSchemaInitBasicType("NOTATION",
  484. XML_SCHEMAS_NOTATION,
  485. xmlSchemaTypeAnySimpleTypeDef);
  486. xmlSchemaTypeQNameDef = xmlSchemaInitBasicType("QName",
  487. XML_SCHEMAS_QNAME,
  488. xmlSchemaTypeAnySimpleTypeDef);
  489. /*
  490. * derived datatypes
  491. */
  492. xmlSchemaTypeIntegerDef = xmlSchemaInitBasicType("integer",
  493. XML_SCHEMAS_INTEGER,
  494. xmlSchemaTypeDecimalDef);
  495. xmlSchemaTypeNonPositiveIntegerDef =
  496. xmlSchemaInitBasicType("nonPositiveInteger",
  497. XML_SCHEMAS_NPINTEGER,
  498. xmlSchemaTypeIntegerDef);
  499. xmlSchemaTypeNegativeIntegerDef =
  500. xmlSchemaInitBasicType("negativeInteger", XML_SCHEMAS_NINTEGER,
  501. xmlSchemaTypeNonPositiveIntegerDef);
  502. xmlSchemaTypeLongDef =
  503. xmlSchemaInitBasicType("long", XML_SCHEMAS_LONG,
  504. xmlSchemaTypeIntegerDef);
  505. xmlSchemaTypeIntDef = xmlSchemaInitBasicType("int", XML_SCHEMAS_INT,
  506. xmlSchemaTypeLongDef);
  507. xmlSchemaTypeShortDef = xmlSchemaInitBasicType("short",
  508. XML_SCHEMAS_SHORT,
  509. xmlSchemaTypeIntDef);
  510. xmlSchemaTypeByteDef = xmlSchemaInitBasicType("byte",
  511. XML_SCHEMAS_BYTE,
  512. xmlSchemaTypeShortDef);
  513. xmlSchemaTypeNonNegativeIntegerDef =
  514. xmlSchemaInitBasicType("nonNegativeInteger",
  515. XML_SCHEMAS_NNINTEGER,
  516. xmlSchemaTypeIntegerDef);
  517. xmlSchemaTypeUnsignedLongDef =
  518. xmlSchemaInitBasicType("unsignedLong", XML_SCHEMAS_ULONG,
  519. xmlSchemaTypeNonNegativeIntegerDef);
  520. xmlSchemaTypeUnsignedIntDef =
  521. xmlSchemaInitBasicType("unsignedInt", XML_SCHEMAS_UINT,
  522. xmlSchemaTypeUnsignedLongDef);
  523. xmlSchemaTypeUnsignedShortDef =
  524. xmlSchemaInitBasicType("unsignedShort", XML_SCHEMAS_USHORT,
  525. xmlSchemaTypeUnsignedIntDef);
  526. xmlSchemaTypeUnsignedByteDef =
  527. xmlSchemaInitBasicType("unsignedByte", XML_SCHEMAS_UBYTE,
  528. xmlSchemaTypeUnsignedShortDef);
  529. xmlSchemaTypePositiveIntegerDef =
  530. xmlSchemaInitBasicType("positiveInteger", XML_SCHEMAS_PINTEGER,
  531. xmlSchemaTypeNonNegativeIntegerDef);
  532. xmlSchemaTypeNormStringDef = xmlSchemaInitBasicType("normalizedString",
  533. XML_SCHEMAS_NORMSTRING,
  534. xmlSchemaTypeStringDef);
  535. xmlSchemaTypeTokenDef = xmlSchemaInitBasicType("token",
  536. XML_SCHEMAS_TOKEN,
  537. xmlSchemaTypeNormStringDef);
  538. xmlSchemaTypeLanguageDef = xmlSchemaInitBasicType("language",
  539. XML_SCHEMAS_LANGUAGE,
  540. xmlSchemaTypeTokenDef);
  541. xmlSchemaTypeNameDef = xmlSchemaInitBasicType("Name",
  542. XML_SCHEMAS_NAME,
  543. xmlSchemaTypeTokenDef);
  544. xmlSchemaTypeNmtokenDef = xmlSchemaInitBasicType("NMTOKEN",
  545. XML_SCHEMAS_NMTOKEN,
  546. xmlSchemaTypeTokenDef);
  547. xmlSchemaTypeNCNameDef = xmlSchemaInitBasicType("NCName",
  548. XML_SCHEMAS_NCNAME,
  549. xmlSchemaTypeNameDef);
  550. xmlSchemaTypeIdDef = xmlSchemaInitBasicType("ID", XML_SCHEMAS_ID,
  551. xmlSchemaTypeNCNameDef);
  552. xmlSchemaTypeIdrefDef = xmlSchemaInitBasicType("IDREF",
  553. XML_SCHEMAS_IDREF,
  554. xmlSchemaTypeNCNameDef);
  555. xmlSchemaTypeEntityDef = xmlSchemaInitBasicType("ENTITY",
  556. XML_SCHEMAS_ENTITY,
  557. xmlSchemaTypeNCNameDef);
  558. /*
  559. * Derived list types.
  560. */
  561. /* ENTITIES */
  562. xmlSchemaTypeEntitiesDef = xmlSchemaInitBasicType("ENTITIES",
  563. XML_SCHEMAS_ENTITIES,
  564. xmlSchemaTypeAnySimpleTypeDef);
  565. xmlSchemaTypeEntitiesDef->subtypes = xmlSchemaTypeEntityDef;
  566. /* IDREFS */
  567. xmlSchemaTypeIdrefsDef = xmlSchemaInitBasicType("IDREFS",
  568. XML_SCHEMAS_IDREFS,
  569. xmlSchemaTypeAnySimpleTypeDef);
  570. xmlSchemaTypeIdrefsDef->subtypes = xmlSchemaTypeIdrefDef;
  571. /* NMTOKENS */
  572. xmlSchemaTypeNmtokensDef = xmlSchemaInitBasicType("NMTOKENS",
  573. XML_SCHEMAS_NMTOKENS,
  574. xmlSchemaTypeAnySimpleTypeDef);
  575. xmlSchemaTypeNmtokensDef->subtypes = xmlSchemaTypeNmtokenDef;
  576. xmlSchemaTypesInitialized = 1;
  577. }
  578. static void
  579. xmlSchemaFreeTypeEntry(void *type, const xmlChar *name ATTRIBUTE_UNUSED) {
  580. xmlSchemaFreeType((xmlSchemaTypePtr) type);
  581. }
  582. /**
  583. * xmlSchemaCleanupTypes:
  584. *
  585. * Cleanup the default XML Schemas type library
  586. */
  587. void
  588. xmlSchemaCleanupTypes(void) {
  589. if (xmlSchemaTypesInitialized == 0)
  590. return;
  591. /*
  592. * Free xs:anyType.
  593. */
  594. {
  595. xmlSchemaParticlePtr particle;
  596. /* Attribute wildcard. */
  597. xmlSchemaFreeWildcard(xmlSchemaTypeAnyTypeDef->attributeWildcard);
  598. /* Content type. */
  599. particle = (xmlSchemaParticlePtr) xmlSchemaTypeAnyTypeDef->subtypes;
  600. /* Wildcard. */
  601. xmlSchemaFreeWildcard((xmlSchemaWildcardPtr)
  602. particle->children->children->children);
  603. xmlFree((xmlSchemaParticlePtr) particle->children->children);
  604. /* Sequence model group. */
  605. xmlFree((xmlSchemaModelGroupPtr) particle->children);
  606. xmlFree((xmlSchemaParticlePtr) particle);
  607. xmlSchemaTypeAnyTypeDef->subtypes = NULL;
  608. }
  609. xmlHashFree(xmlSchemaTypesBank, xmlSchemaFreeTypeEntry);
  610. xmlSchemaTypesInitialized = 0;
  611. }
  612. /**
  613. * xmlSchemaIsBuiltInTypeFacet:
  614. * @type: the built-in type
  615. * @facetType: the facet type
  616. *
  617. * Evaluates if a specific facet can be
  618. * used in conjunction with a type.
  619. *
  620. * Returns 1 if the facet can be used with the given built-in type,
  621. * 0 otherwise and -1 in case the type is not a built-in type.
  622. */
  623. int
  624. xmlSchemaIsBuiltInTypeFacet(xmlSchemaTypePtr type, int facetType)
  625. {
  626. if (type == NULL)
  627. return (-1);
  628. if (type->type != XML_SCHEMA_TYPE_BASIC)
  629. return (-1);
  630. switch (type->builtInType) {
  631. case XML_SCHEMAS_BOOLEAN:
  632. if ((facetType == XML_SCHEMA_FACET_PATTERN) ||
  633. (facetType == XML_SCHEMA_FACET_WHITESPACE))
  634. return (1);
  635. else
  636. return (0);
  637. case XML_SCHEMAS_STRING:
  638. case XML_SCHEMAS_NOTATION:
  639. case XML_SCHEMAS_QNAME:
  640. case XML_SCHEMAS_ANYURI:
  641. case XML_SCHEMAS_BASE64BINARY:
  642. case XML_SCHEMAS_HEXBINARY:
  643. if ((facetType == XML_SCHEMA_FACET_LENGTH) ||
  644. (facetType == XML_SCHEMA_FACET_MINLENGTH) ||
  645. (facetType == XML_SCHEMA_FACET_MAXLENGTH) ||
  646. (facetType == XML_SCHEMA_FACET_PATTERN) ||
  647. (facetType == XML_SCHEMA_FACET_ENUMERATION) ||
  648. (facetType == XML_SCHEMA_FACET_WHITESPACE))
  649. return (1);
  650. else
  651. return (0);
  652. case XML_SCHEMAS_DECIMAL:
  653. if ((facetType == XML_SCHEMA_FACET_TOTALDIGITS) ||
  654. (facetType == XML_SCHEMA_FACET_FRACTIONDIGITS) ||
  655. (facetType == XML_SCHEMA_FACET_PATTERN) ||
  656. (facetType == XML_SCHEMA_FACET_WHITESPACE) ||
  657. (facetType == XML_SCHEMA_FACET_ENUMERATION) ||
  658. (facetType == XML_SCHEMA_FACET_MAXINCLUSIVE) ||
  659. (facetType == XML_SCHEMA_FACET_MAXEXCLUSIVE) ||
  660. (facetType == XML_SCHEMA_FACET_MININCLUSIVE) ||
  661. (facetType == XML_SCHEMA_FACET_MINEXCLUSIVE))
  662. return (1);
  663. else
  664. return (0);
  665. case XML_SCHEMAS_TIME:
  666. case XML_SCHEMAS_GDAY:
  667. case XML_SCHEMAS_GMONTH:
  668. case XML_SCHEMAS_GMONTHDAY:
  669. case XML_SCHEMAS_GYEAR:
  670. case XML_SCHEMAS_GYEARMONTH:
  671. case XML_SCHEMAS_DATE:
  672. case XML_SCHEMAS_DATETIME:
  673. case XML_SCHEMAS_DURATION:
  674. case XML_SCHEMAS_FLOAT:
  675. case XML_SCHEMAS_DOUBLE:
  676. if ((facetType == XML_SCHEMA_FACET_PATTERN) ||
  677. (facetType == XML_SCHEMA_FACET_ENUMERATION) ||
  678. (facetType == XML_SCHEMA_FACET_WHITESPACE) ||
  679. (facetType == XML_SCHEMA_FACET_MAXINCLUSIVE) ||
  680. (facetType == XML_SCHEMA_FACET_MAXEXCLUSIVE) ||
  681. (facetType == XML_SCHEMA_FACET_MININCLUSIVE) ||
  682. (facetType == XML_SCHEMA_FACET_MINEXCLUSIVE))
  683. return (1);
  684. else
  685. return (0);
  686. default:
  687. break;
  688. }
  689. return (0);
  690. }
  691. /**
  692. * xmlSchemaGetBuiltInType:
  693. * @type: the type of the built in type
  694. *
  695. * Gives you the type struct for a built-in
  696. * type by its type id.
  697. *
  698. * Returns the type if found, NULL otherwise.
  699. */
  700. xmlSchemaTypePtr
  701. xmlSchemaGetBuiltInType(xmlSchemaValType type)
  702. {
  703. if (xmlSchemaTypesInitialized == 0)
  704. xmlSchemaInitTypes();
  705. switch (type) {
  706. case XML_SCHEMAS_ANYSIMPLETYPE:
  707. return (xmlSchemaTypeAnySimpleTypeDef);
  708. case XML_SCHEMAS_STRING:
  709. return (xmlSchemaTypeStringDef);
  710. case XML_SCHEMAS_NORMSTRING:
  711. return (xmlSchemaTypeNormStringDef);
  712. case XML_SCHEMAS_DECIMAL:
  713. return (xmlSchemaTypeDecimalDef);
  714. case XML_SCHEMAS_TIME:
  715. return (xmlSchemaTypeTimeDef);
  716. case XML_SCHEMAS_GDAY:
  717. return (xmlSchemaTypeGDayDef);
  718. case XML_SCHEMAS_GMONTH:
  719. return (xmlSchemaTypeGMonthDef);
  720. case XML_SCHEMAS_GMONTHDAY:
  721. return (xmlSchemaTypeGMonthDayDef);
  722. case XML_SCHEMAS_GYEAR:
  723. return (xmlSchemaTypeGYearDef);
  724. case XML_SCHEMAS_GYEARMONTH:
  725. return (xmlSchemaTypeGYearMonthDef);
  726. case XML_SCHEMAS_DATE:
  727. return (xmlSchemaTypeDateDef);
  728. case XML_SCHEMAS_DATETIME:
  729. return (xmlSchemaTypeDatetimeDef);
  730. case XML_SCHEMAS_DURATION:
  731. return (xmlSchemaTypeDurationDef);
  732. case XML_SCHEMAS_FLOAT:
  733. return (xmlSchemaTypeFloatDef);
  734. case XML_SCHEMAS_DOUBLE:
  735. return (xmlSchemaTypeDoubleDef);
  736. case XML_SCHEMAS_BOOLEAN:
  737. return (xmlSchemaTypeBooleanDef);
  738. case XML_SCHEMAS_TOKEN:
  739. return (xmlSchemaTypeTokenDef);
  740. case XML_SCHEMAS_LANGUAGE:
  741. return (xmlSchemaTypeLanguageDef);
  742. case XML_SCHEMAS_NMTOKEN:
  743. return (xmlSchemaTypeNmtokenDef);
  744. case XML_SCHEMAS_NMTOKENS:
  745. return (xmlSchemaTypeNmtokensDef);
  746. case XML_SCHEMAS_NAME:
  747. return (xmlSchemaTypeNameDef);
  748. case XML_SCHEMAS_QNAME:
  749. return (xmlSchemaTypeQNameDef);
  750. case XML_SCHEMAS_NCNAME:
  751. return (xmlSchemaTypeNCNameDef);
  752. case XML_SCHEMAS_ID:
  753. return (xmlSchemaTypeIdDef);
  754. case XML_SCHEMAS_IDREF:
  755. return (xmlSchemaTypeIdrefDef);
  756. case XML_SCHEMAS_IDREFS:
  757. return (xmlSchemaTypeIdrefsDef);
  758. case XML_SCHEMAS_ENTITY:
  759. return (xmlSchemaTypeEntityDef);
  760. case XML_SCHEMAS_ENTITIES:
  761. return (xmlSchemaTypeEntitiesDef);
  762. case XML_SCHEMAS_NOTATION:
  763. return (xmlSchemaTypeNotationDef);
  764. case XML_SCHEMAS_ANYURI:
  765. return (xmlSchemaTypeAnyURIDef);
  766. case XML_SCHEMAS_INTEGER:
  767. return (xmlSchemaTypeIntegerDef);
  768. case XML_SCHEMAS_NPINTEGER:
  769. return (xmlSchemaTypeNonPositiveIntegerDef);
  770. case XML_SCHEMAS_NINTEGER:
  771. return (xmlSchemaTypeNegativeIntegerDef);
  772. case XML_SCHEMAS_NNINTEGER:
  773. return (xmlSchemaTypeNonNegativeIntegerDef);
  774. case XML_SCHEMAS_PINTEGER:
  775. return (xmlSchemaTypePositiveIntegerDef);
  776. case XML_SCHEMAS_INT:
  777. return (xmlSchemaTypeIntDef);
  778. case XML_SCHEMAS_UINT:
  779. return (xmlSchemaTypeUnsignedIntDef);
  780. case XML_SCHEMAS_LONG:
  781. return (xmlSchemaTypeLongDef);
  782. case XML_SCHEMAS_ULONG:
  783. return (xmlSchemaTypeUnsignedLongDef);
  784. case XML_SCHEMAS_SHORT:
  785. return (xmlSchemaTypeShortDef);
  786. case XML_SCHEMAS_USHORT:
  787. return (xmlSchemaTypeUnsignedShortDef);
  788. case XML_SCHEMAS_BYTE:
  789. return (xmlSchemaTypeByteDef);
  790. case XML_SCHEMAS_UBYTE:
  791. return (xmlSchemaTypeUnsignedByteDef);
  792. case XML_SCHEMAS_HEXBINARY:
  793. return (xmlSchemaTypeHexBinaryDef);
  794. case XML_SCHEMAS_BASE64BINARY:
  795. return (xmlSchemaTypeBase64BinaryDef);
  796. case XML_SCHEMAS_ANYTYPE:
  797. return (xmlSchemaTypeAnyTypeDef);
  798. default:
  799. return (NULL);
  800. }
  801. }
  802. /**
  803. * xmlSchemaValueAppend:
  804. * @prev: the value
  805. * @cur: the value to be appended
  806. *
  807. * Appends a next sibling to a list of computed values.
  808. *
  809. * Returns 0 if succeeded and -1 on API errors.
  810. */
  811. int
  812. xmlSchemaValueAppend(xmlSchemaValPtr prev, xmlSchemaValPtr cur) {
  813. if ((prev == NULL) || (cur == NULL))
  814. return (-1);
  815. prev->next = cur;
  816. return (0);
  817. }
  818. /**
  819. * xmlSchemaValueGetNext:
  820. * @cur: the value
  821. *
  822. * Accessor for the next sibling of a list of computed values.
  823. *
  824. * Returns the next value or NULL if there was none, or on
  825. * API errors.
  826. */
  827. xmlSchemaValPtr
  828. xmlSchemaValueGetNext(xmlSchemaValPtr cur) {
  829. if (cur == NULL)
  830. return (NULL);
  831. return (cur->next);
  832. }
  833. /**
  834. * xmlSchemaValueGetAsString:
  835. * @val: the value
  836. *
  837. * Accessor for the string value of a computed value.
  838. *
  839. * Returns the string value or NULL if there was none, or on
  840. * API errors.
  841. */
  842. const xmlChar *
  843. xmlSchemaValueGetAsString(xmlSchemaValPtr val)
  844. {
  845. if (val == NULL)
  846. return (NULL);
  847. switch (val->type) {
  848. case XML_SCHEMAS_STRING:
  849. case XML_SCHEMAS_NORMSTRING:
  850. case XML_SCHEMAS_ANYSIMPLETYPE:
  851. case XML_SCHEMAS_TOKEN:
  852. case XML_SCHEMAS_LANGUAGE:
  853. case XML_SCHEMAS_NMTOKEN:
  854. case XML_SCHEMAS_NAME:
  855. case XML_SCHEMAS_NCNAME:
  856. case XML_SCHEMAS_ID:
  857. case XML_SCHEMAS_IDREF:
  858. case XML_SCHEMAS_ENTITY:
  859. case XML_SCHEMAS_ANYURI:
  860. return (BAD_CAST val->value.str);
  861. default:
  862. break;
  863. }
  864. return (NULL);
  865. }
  866. /**
  867. * xmlSchemaValueGetAsBoolean:
  868. * @val: the value
  869. *
  870. * Accessor for the boolean value of a computed value.
  871. *
  872. * Returns 1 if true and 0 if false, or in case of an error. Hmm.
  873. */
  874. int
  875. xmlSchemaValueGetAsBoolean(xmlSchemaValPtr val)
  876. {
  877. if ((val == NULL) || (val->type != XML_SCHEMAS_BOOLEAN))
  878. return (0);
  879. return (val->value.b);
  880. }
  881. /**
  882. * xmlSchemaNewStringValue:
  883. * @type: the value type
  884. * @value: the value
  885. *
  886. * Allocate a new simple type value. The type can be
  887. * of XML_SCHEMAS_STRING.
  888. * WARNING: This one is intended to be expanded for other
  889. * string based types. We need this for anySimpleType as well.
  890. * The given value is consumed and freed with the struct.
  891. *
  892. * Returns a pointer to the new value or NULL in case of error
  893. */
  894. xmlSchemaValPtr
  895. xmlSchemaNewStringValue(xmlSchemaValType type,
  896. const xmlChar *value)
  897. {
  898. xmlSchemaValPtr val;
  899. if (type != XML_SCHEMAS_STRING)
  900. return(NULL);
  901. val = (xmlSchemaValPtr) xmlMalloc(sizeof(xmlSchemaVal));
  902. if (val == NULL) {
  903. return(NULL);
  904. }
  905. memset(val, 0, sizeof(xmlSchemaVal));
  906. val->type = type;
  907. val->value.str = (xmlChar *) value;
  908. return(val);
  909. }
  910. /**
  911. * xmlSchemaNewNOTATIONValue:
  912. * @name: the notation name
  913. * @ns: the notation namespace name or NULL
  914. *
  915. * Allocate a new NOTATION value.
  916. * The given values are consumed and freed with the struct.
  917. *
  918. * Returns a pointer to the new value or NULL in case of error
  919. */
  920. xmlSchemaValPtr
  921. xmlSchemaNewNOTATIONValue(const xmlChar *name,
  922. const xmlChar *ns)
  923. {
  924. xmlSchemaValPtr val;
  925. val = xmlSchemaNewValue(XML_SCHEMAS_NOTATION);
  926. if (val == NULL)
  927. return (NULL);
  928. val->value.qname.name = (xmlChar *)name;
  929. if (ns != NULL)
  930. val->value.qname.uri = (xmlChar *)ns;
  931. return(val);
  932. }
  933. /**
  934. * xmlSchemaNewQNameValue:
  935. * @namespaceName: the namespace name
  936. * @localName: the local name
  937. *
  938. * Allocate a new QName value.
  939. * The given values are consumed and freed with the struct.
  940. *
  941. * Returns a pointer to the new value or NULL in case of an error.
  942. */
  943. xmlSchemaValPtr
  944. xmlSchemaNewQNameValue(const xmlChar *namespaceName,
  945. const xmlChar *localName)
  946. {
  947. xmlSchemaValPtr val;
  948. val = xmlSchemaNewValue(XML_SCHEMAS_QNAME);
  949. if (val == NULL)
  950. return (NULL);
  951. val->value.qname.name = (xmlChar *) localName;
  952. val->value.qname.uri = (xmlChar *) namespaceName;
  953. return(val);
  954. }
  955. /**
  956. * xmlSchemaFreeValue:
  957. * @value: the value to free
  958. *
  959. * Cleanup the default XML Schemas type library
  960. */
  961. void
  962. xmlSchemaFreeValue(xmlSchemaValPtr value) {
  963. xmlSchemaValPtr prev;
  964. while (value != NULL) {
  965. switch (value->type) {
  966. case XML_SCHEMAS_STRING:
  967. case XML_SCHEMAS_NORMSTRING:
  968. case XML_SCHEMAS_TOKEN:
  969. case XML_SCHEMAS_LANGUAGE:
  970. case XML_SCHEMAS_NMTOKEN:
  971. case XML_SCHEMAS_NMTOKENS:
  972. case XML_SCHEMAS_NAME:
  973. case XML_SCHEMAS_NCNAME:
  974. case XML_SCHEMAS_ID:
  975. case XML_SCHEMAS_IDREF:
  976. case XML_SCHEMAS_IDREFS:
  977. case XML_SCHEMAS_ENTITY:
  978. case XML_SCHEMAS_ENTITIES:
  979. case XML_SCHEMAS_ANYURI:
  980. case XML_SCHEMAS_ANYSIMPLETYPE:
  981. if (value->value.str != NULL)
  982. xmlFree(value->value.str);
  983. break;
  984. case XML_SCHEMAS_NOTATION:
  985. case XML_SCHEMAS_QNAME:
  986. if (value->value.qname.uri != NULL)
  987. xmlFree(value->value.qname.uri);
  988. if (value->value.qname.name != NULL)
  989. xmlFree(value->value.qname.name);
  990. break;
  991. case XML_SCHEMAS_HEXBINARY:
  992. if (value->value.hex.str != NULL)
  993. xmlFree(value->value.hex.str);
  994. break;
  995. case XML_SCHEMAS_BASE64BINARY:
  996. if (value->value.base64.str != NULL)
  997. xmlFree(value->value.base64.str);
  998. break;
  999. default:
  1000. break;
  1001. }
  1002. prev = value;
  1003. value = value->next;
  1004. xmlFree(prev);
  1005. }
  1006. }
  1007. /**
  1008. * xmlSchemaGetPredefinedType:
  1009. * @name: the type name
  1010. * @ns: the URI of the namespace usually "http://www.w3.org/2001/XMLSchema"
  1011. *
  1012. * Lookup a type in the default XML Schemas type library
  1013. *
  1014. * Returns the type if found, NULL otherwise
  1015. */
  1016. xmlSchemaTypePtr
  1017. xmlSchemaGetPredefinedType(const xmlChar *name, const xmlChar *ns) {
  1018. if (xmlSchemaTypesInitialized == 0)
  1019. xmlSchemaInitTypes();
  1020. if (name == NULL)
  1021. return(NULL);
  1022. return((xmlSchemaTypePtr) xmlHashLookup2(xmlSchemaTypesBank, name, ns));
  1023. }
  1024. /**
  1025. * xmlSchemaGetBuiltInListSimpleTypeItemType:
  1026. * @type: the built-in simple type.
  1027. *
  1028. * Lookup function
  1029. *
  1030. * Returns the item type of @type as defined by the built-in datatype
  1031. * hierarchy of XML Schema Part 2: Datatypes, or NULL in case of an error.
  1032. */
  1033. xmlSchemaTypePtr
  1034. xmlSchemaGetBuiltInListSimpleTypeItemType(xmlSchemaTypePtr type)
  1035. {
  1036. if ((type == NULL) || (type->type != XML_SCHEMA_TYPE_BASIC))
  1037. return (NULL);
  1038. switch (type->builtInType) {
  1039. case XML_SCHEMAS_NMTOKENS:
  1040. return (xmlSchemaTypeNmtokenDef );
  1041. case XML_SCHEMAS_IDREFS:
  1042. return (xmlSchemaTypeIdrefDef);
  1043. case XML_SCHEMAS_ENTITIES:
  1044. return (xmlSchemaTypeEntityDef);
  1045. default:
  1046. return (NULL);
  1047. }
  1048. }
  1049. /****************************************************************
  1050. * *
  1051. * Convenience macros and functions *
  1052. * *
  1053. ****************************************************************/
  1054. #define IS_TZO_CHAR(c) \
  1055. ((c == 0) || (c == 'Z') || (c == '+') || (c == '-'))
  1056. #define VALID_YEAR(yr) (yr != 0)
  1057. #define VALID_MONTH(mon) ((mon >= 1) && (mon <= 12))
  1058. /* VALID_DAY should only be used when month is unknown */
  1059. #define VALID_DAY(day) ((day >= 1) && (day <= 31))
  1060. #define VALID_HOUR(hr) ((hr >= 0) && (hr <= 23))
  1061. #define VALID_MIN(min) ((min >= 0) && (min <= 59))
  1062. #define VALID_SEC(sec) ((sec >= 0) && (sec < 60))
  1063. #define VALID_TZO(tzo) ((tzo >= -840) && (tzo <= 840))
  1064. #define IS_LEAP(y) \
  1065. (((y % 4 == 0) && (y % 100 != 0)) || (y % 400 == 0))
  1066. static const unsigned int daysInMonth[12] =
  1067. { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
  1068. static const unsigned int daysInMonthLeap[12] =
  1069. { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
  1070. #define MAX_DAYINMONTH(yr,mon) \
  1071. (IS_LEAP(yr) ? daysInMonthLeap[mon - 1] : daysInMonth[mon - 1])
  1072. #define VALID_MDAY(dt) \
  1073. (IS_LEAP(dt->year) ? \
  1074. (dt->day <= daysInMonthLeap[dt->mon - 1]) : \
  1075. (dt->day <= daysInMonth[dt->mon - 1]))
  1076. #define VALID_DATE(dt) \
  1077. (VALID_YEAR(dt->year) && VALID_MONTH(dt->mon) && VALID_MDAY(dt))
  1078. #define VALID_END_OF_DAY(dt) \
  1079. ((dt)->hour == 24 && (dt)->min == 0 && (dt)->sec == 0)
  1080. #define VALID_TIME(dt) \
  1081. (((VALID_HOUR(dt->hour) && VALID_MIN(dt->min) && \
  1082. VALID_SEC(dt->sec)) || VALID_END_OF_DAY(dt)) && \
  1083. VALID_TZO(dt->tzo))
  1084. #define VALID_DATETIME(dt) \
  1085. (VALID_DATE(dt) && VALID_TIME(dt))
  1086. #define SECS_PER_MIN 60
  1087. #define MINS_PER_HOUR 60
  1088. #define HOURS_PER_DAY 24
  1089. #define SECS_PER_HOUR (MINS_PER_HOUR * SECS_PER_MIN)
  1090. #define SECS_PER_DAY (HOURS_PER_DAY * SECS_PER_HOUR)
  1091. #define MINS_PER_DAY (HOURS_PER_DAY * MINS_PER_HOUR)
  1092. static const long dayInYearByMonth[12] =
  1093. { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
  1094. static const long dayInLeapYearByMonth[12] =
  1095. { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335 };
  1096. #define DAY_IN_YEAR(day, month, year) \
  1097. ((IS_LEAP(year) ? \
  1098. dayInLeapYearByMonth[month - 1] : \
  1099. dayInYearByMonth[month - 1]) + day)
  1100. #ifdef DEBUG
  1101. #define DEBUG_DATE(dt) \
  1102. xmlGenericError(xmlGenericErrorContext, \
  1103. "type=%o %04ld-%02u-%02uT%02u:%02u:%03f", \
  1104. dt->type,dt->value.date.year,dt->value.date.mon, \
  1105. dt->value.date.day,dt->value.date.hour,dt->value.date.min, \
  1106. dt->value.date.sec); \
  1107. if (dt->value.date.tz_flag) \
  1108. if (dt->value.date.tzo != 0) \
  1109. xmlGenericError(xmlGenericErrorContext, \
  1110. "%+05d\n",dt->value.date.tzo); \
  1111. else \
  1112. xmlGenericError(xmlGenericErrorContext, "Z\n"); \
  1113. else \
  1114. xmlGenericError(xmlGenericErrorContext,"\n")
  1115. #else
  1116. #define DEBUG_DATE(dt)
  1117. #endif
  1118. /**
  1119. * _xmlSchemaParseGYear:
  1120. * @dt: pointer to a date structure
  1121. * @str: pointer to the string to analyze
  1122. *
  1123. * Parses a xs:gYear without time zone and fills in the appropriate
  1124. * field of the @dt structure. @str is updated to point just after the
  1125. * xs:gYear. It is supposed that @dt->year is big enough to contain
  1126. * the year.
  1127. *
  1128. * Returns 0 or the error code
  1129. */
  1130. static int
  1131. _xmlSchemaParseGYear (xmlSchemaValDatePtr dt, const xmlChar **str) {
  1132. const xmlChar *cur = *str, *firstChar;
  1133. int isneg = 0, digcnt = 0;
  1134. if (((*cur < '0') || (*cur > '9')) &&
  1135. (*cur != '-') && (*cur != '+'))
  1136. return -1;
  1137. if (*cur == '-') {
  1138. isneg = 1;
  1139. cur++;
  1140. }
  1141. firstChar = cur;
  1142. while ((*cur >= '0') && (*cur <= '9')) {
  1143. int digit = *cur - '0';
  1144. if (dt->year > LONG_MAX / 10)
  1145. return 2;
  1146. dt->year *= 10;
  1147. if (dt->year > LONG_MAX - digit)
  1148. return 2;
  1149. dt->year += digit;
  1150. cur++;
  1151. digcnt++;
  1152. }
  1153. /* year must be at least 4 digits (CCYY); over 4
  1154. * digits cannot have a leading zero. */
  1155. if ((digcnt < 4) || ((digcnt > 4) && (*firstChar == '0')))
  1156. return 1;
  1157. if (isneg)
  1158. dt->year = - dt->year;
  1159. if (!VALID_YEAR(dt->year))
  1160. return 2;
  1161. *str = cur;
  1162. return 0;
  1163. }
  1164. /**
  1165. * PARSE_2_DIGITS:
  1166. * @num: the integer to fill in
  1167. * @cur: an #xmlChar *
  1168. * @invalid: an integer
  1169. *
  1170. * Parses a 2-digits integer and updates @num with the value. @cur is
  1171. * updated to point just after the integer.
  1172. * In case of error, @invalid is set to %TRUE, values of @num and
  1173. * @cur are undefined.
  1174. */
  1175. #define PARSE_2_DIGITS(num, cur, invalid) \
  1176. if ((cur[0] < '0') || (cur[0] > '9') || \
  1177. (cur[1] < '0') || (cur[1] > '9')) \
  1178. invalid = 1; \
  1179. else \
  1180. num = (cur[0] - '0') * 10 + (cur[1] - '0'); \
  1181. cur += 2;
  1182. /**
  1183. * PARSE_FLOAT:
  1184. * @num: the double to fill in
  1185. * @cur: an #xmlChar *
  1186. * @invalid: an integer
  1187. *
  1188. * Parses a float and updates @num with the value. @cur is
  1189. * updated to point just after the float. The float must have a
  1190. * 2-digits integer part and may or may not have a decimal part.
  1191. * In case of error, @invalid is set to %TRUE, values of @num and
  1192. * @cur are undefined.
  1193. */
  1194. #define PARSE_FLOAT(num, cur, invalid) \
  1195. PARSE_2_DIGITS(num, cur, invalid); \
  1196. if (!invalid && (*cur == '.')) { \
  1197. double mult = 1; \
  1198. cur++; \
  1199. if ((*cur < '0') || (*cur > '9')) \
  1200. invalid = 1; \
  1201. while ((*cur >= '0') && (*cur <= '9')) { \
  1202. mult /= 10; \
  1203. num += (*cur - '0') * mult; \
  1204. cur++; \
  1205. } \
  1206. }
  1207. /**
  1208. * _xmlSchemaParseGMonth:
  1209. * @dt: pointer to a date structure
  1210. * @str: pointer to the string to analyze
  1211. *
  1212. * Parses a xs:gMonth without time zone and fills in the appropriate
  1213. * field of the @dt structure. @str is updated to point just after the
  1214. * xs:gMonth.
  1215. *
  1216. * Returns 0 or the error code
  1217. */
  1218. static int
  1219. _xmlSchemaParseGMonth (xmlSchemaValDatePtr dt, const xmlChar **str) {
  1220. const xmlChar *cur = *str;
  1221. int ret = 0;
  1222. unsigned int value = 0;
  1223. PARSE_2_DIGITS(value, cur, ret);
  1224. if (ret != 0)
  1225. return ret;
  1226. if (!VALID_MONTH(value))
  1227. return 2;
  1228. dt->mon = value;
  1229. *str = cur;
  1230. return 0;
  1231. }
  1232. /**
  1233. * _xmlSchemaParseGDay:
  1234. * @dt: pointer to a date structure
  1235. * @str: pointer to the string to analyze
  1236. *
  1237. * Parses a xs:gDay without time zone and fills in the appropriate
  1238. * field of the @dt structure. @str is updated to point just after the
  1239. * xs:gDay.
  1240. *
  1241. * Returns 0 or the error code
  1242. */
  1243. static int
  1244. _xmlSchemaParseGDay (xmlSchemaValDatePtr dt, const xmlChar **str) {
  1245. const xmlChar *cur = *str;
  1246. int ret = 0;
  1247. unsigned int value = 0;
  1248. PARSE_2_DIGITS(value, cur, ret);
  1249. if (ret != 0)
  1250. return ret;
  1251. if (!VALID_DAY(value))
  1252. return 2;
  1253. dt->day = value;
  1254. *str = cur;
  1255. return 0;
  1256. }
  1257. /**
  1258. * _xmlSchemaParseTime:
  1259. * @dt: pointer to a date structure
  1260. * @str: pointer to the string to analyze
  1261. *
  1262. * Parses a xs:time without time zone and fills in the appropriate
  1263. * fields of the @dt structure. @str is updated to point just after the
  1264. * xs:time.
  1265. * In case of error, values of @dt fields are undefined.
  1266. *
  1267. * Returns 0 or the error code
  1268. */
  1269. static int
  1270. _xmlSchemaParseTime (xmlSchemaValDatePtr dt, const xmlChar **str) {
  1271. const xmlChar *cur = *str;
  1272. int ret = 0;
  1273. int value = 0;
  1274. PARSE_2_DIGITS(value, cur, ret);
  1275. if (ret != 0)
  1276. return ret;
  1277. if (*cur != ':')
  1278. return 1;
  1279. if (!VALID_HOUR(value) && value != 24 /* Allow end-of-day hour */)
  1280. return 2;
  1281. cur++;
  1282. /* the ':' insures this string is xs:time */
  1283. dt->hour = value;
  1284. PARSE_2_DIGITS(value, cur, ret);
  1285. if (ret != 0)
  1286. return ret;
  1287. if (!VALID_MIN(value))
  1288. return 2;
  1289. dt->min = value;
  1290. if (*cur != ':')
  1291. return 1;
  1292. cur++;
  1293. PARSE_FLOAT(dt->sec, cur, ret);
  1294. if (ret != 0)
  1295. return ret;
  1296. if (!VALID_TIME(dt))
  1297. return 2;
  1298. *str = cur;
  1299. return 0;
  1300. }
  1301. /**
  1302. * _xmlSchemaParseTimeZone:
  1303. * @dt: pointer to a date structure
  1304. * @str: pointer to the string to analyze
  1305. *
  1306. * Parses a time zone without time zone and fills in the appropriate
  1307. * field of the @dt structure. @str is updated to point just after the
  1308. * time zone.
  1309. *
  1310. * Returns 0 or the error code
  1311. */
  1312. static int
  1313. _xmlSchemaParseTimeZone (xmlSchemaValDatePtr dt, const xmlChar **str) {
  1314. const xmlChar *cur;
  1315. int ret = 0;
  1316. if (str == NULL)
  1317. return -1;
  1318. cur = *str;
  1319. switch (*cur) {
  1320. case 0:
  1321. dt->tz_flag = 0;
  1322. dt->tzo = 0;
  1323. break;
  1324. case 'Z':
  1325. dt->tz_flag = 1;
  1326. dt->tzo = 0;
  1327. cur++;
  1328. break;
  1329. case '+':
  1330. case '-': {
  1331. int isneg = 0, tmp = 0;
  1332. isneg = (*cur == '-');
  1333. cur++;
  1334. PARSE_2_DIGITS(tmp, cur, ret);
  1335. if (ret != 0)
  1336. return ret;
  1337. if (!VALID_HOUR(tmp))
  1338. return 2;
  1339. if (*cur != ':')
  1340. return 1;
  1341. cur++;
  1342. dt->tzo = tmp * 60;
  1343. PARSE_2_DIGITS(tmp, cur, ret);
  1344. if (ret != 0)
  1345. return ret;
  1346. if (!VALID_MIN(tmp))
  1347. return 2;
  1348. dt->tzo += tmp;
  1349. if (isneg)
  1350. dt->tzo = - dt->tzo;
  1351. if (!VALID_TZO(dt->tzo))
  1352. return 2;
  1353. dt->tz_flag = 1;
  1354. break;
  1355. }
  1356. default:
  1357. return 1;
  1358. }
  1359. *str = cur;
  1360. return 0;
  1361. }
  1362. /**
  1363. * _xmlSchemaBase64Decode:
  1364. * @ch: a character
  1365. *
  1366. * Converts a base64 encoded character to its base 64 value.
  1367. *
  1368. * Returns 0-63 (value), 64 (pad), or -1 (not recognized)
  1369. */
  1370. static int
  1371. _xmlSchemaBase64Decode (const xmlChar ch) {
  1372. if (('A' <= ch) && (ch <= 'Z')) return ch - 'A';
  1373. if (('a' <= ch) && (ch <= 'z')) return ch - 'a' + 26;
  1374. if (('0' <= ch) && (ch <= '9')) return ch - '0' + 52;
  1375. if ('+' == ch) return 62;
  1376. if ('/' == ch) return 63;
  1377. if ('=' == ch) return 64;
  1378. return -1;
  1379. }
  1380. /****************************************************************
  1381. * *
  1382. * XML Schema Dates/Times Datatypes Handling *
  1383. * *
  1384. ****************************************************************/
  1385. /**
  1386. * PARSE_DIGITS:
  1387. * @num: the integer to fill in
  1388. * @cur: an #xmlChar *
  1389. * @num_type: an integer flag
  1390. *
  1391. * Parses a digits integer and updates @num with the value. @cur is
  1392. * updated to point just after the integer.
  1393. * In case of error, @num_type is set to -1, values of @num and
  1394. * @cur are undefined.
  1395. */
  1396. #define PARSE_DIGITS(num, cur, num_type) \
  1397. if ((*cur < '0') || (*cur > '9')) \
  1398. num_type = -1; \
  1399. else \
  1400. while ((*cur >= '0') && (*cur <= '9')) { \
  1401. num = num * 10 + (*cur - '0'); \
  1402. cur++; \
  1403. }
  1404. /**
  1405. * PARSE_NUM:
  1406. * @num: the double to fill in
  1407. * @cur: an #xmlChar *
  1408. * @num_type: an integer flag
  1409. *
  1410. * Parses a float or integer and updates @num with the value. @cur is
  1411. * updated to point just after the number. If the number is a float,
  1412. * then it must have an integer part and a decimal part; @num_type will
  1413. * be set to 1. If there is no decimal part, @num_type is set to zero.
  1414. * In case of error, @num_type is set to -1, values of @num and
  1415. * @cur are undefined.
  1416. */
  1417. #define PARSE_NUM(num, cur, num_type) \
  1418. num = 0; \
  1419. PARSE_DIGITS(num, cur, num_type); \
  1420. if (!num_type && (*cur == '.')) { \
  1421. double mult = 1; \
  1422. cur++; \
  1423. if ((*cur < '0') || (*cur > '9')) \
  1424. num_type = -1; \
  1425. else \
  1426. num_type = 1; \
  1427. while ((*cur >= '0') && (*cur <= '9')) { \
  1428. mult /= 10; \
  1429. num += (*cur - '0') * mult; \
  1430. cur++; \
  1431. } \
  1432. }
  1433. /**
  1434. * xmlSchemaValidateDates:
  1435. * @type: the expected type or XML_SCHEMAS_UNKNOWN
  1436. * @dateTime: string to analyze
  1437. * @val: the return computed value
  1438. *
  1439. * Check that @dateTime conforms to the lexical space of one of the date types.
  1440. * if true a value is computed and returned in @val.
  1441. *
  1442. * Returns 0 if this validates, a positive error code number otherwise
  1443. * and -1 in case of internal or API error.
  1444. */
  1445. static int
  1446. xmlSchemaValidateDates (xmlSchemaValType type,
  1447. const xmlChar *dateTime, xmlSchemaValPtr *val,
  1448. int collapse) {
  1449. xmlSchemaValPtr dt;
  1450. int ret;
  1451. const xmlChar *cur = dateTime;
  1452. #define RETURN_TYPE_IF_VALID(t) \
  1453. if (IS_TZO_CHAR(*cur)) { \
  1454. ret = _xmlSchemaParseTimeZone(&(dt->value.date), &cur); \
  1455. if (ret == 0) { \
  1456. if (*cur != 0) \
  1457. goto error; \
  1458. dt->type = t; \
  1459. goto done; \
  1460. } \
  1461. }
  1462. if (dateTime == NULL)
  1463. return -1;
  1464. if (collapse)
  1465. while IS_WSP_BLANK_CH(*cur) cur++;
  1466. if ((*cur != '-') && (*cur < '0') && (*cur > '9'))
  1467. return 1;
  1468. dt = xmlSchemaNewValue(XML_SCHEMAS_UNKNOWN);
  1469. if (dt == NULL)
  1470. return -1;
  1471. if ((cur[0] == '-') && (cur[1] == '-')) {
  1472. /*
  1473. * It's an incomplete date (xs:gMonthDay, xs:gMonth or
  1474. * xs:gDay)
  1475. */
  1476. cur += 2;
  1477. /* is it an xs:gDay? */
  1478. if (*cur == '-') {
  1479. if (type == XML_SCHEMAS_GMONTH)
  1480. goto error;
  1481. ++cur;
  1482. ret = _xmlSchemaParseGDay(&(dt->value.date), &cur);
  1483. if (ret != 0)
  1484. goto error;
  1485. RETURN_TYPE_IF_VALID(XML_SCHEMAS_GDAY);
  1486. goto error;
  1487. }
  1488. /*
  1489. * it should be an xs:gMonthDay or xs:gMonth
  1490. */
  1491. ret = _xmlSchemaParseGMonth(&(dt->value.date), &cur);
  1492. if (ret != 0)
  1493. goto error;
  1494. /*
  1495. * a '-' char could indicate this type is xs:gMonthDay or
  1496. * a negative time zone offset. Check for xs:gMonthDay first.
  1497. * Also the first three char's of a negative tzo (-MM:SS) can
  1498. * appear to be a valid day; so even if the day portion
  1499. * of the xs:gMonthDay verifies, we must insure it was not
  1500. * a tzo.
  1501. */
  1502. if (*cur == '-') {
  1503. const xmlChar *rewnd = cur;
  1504. cur++;
  1505. ret = _xmlSchemaParseGDay(&(dt->value.date), &cur);
  1506. if ((ret == 0) && ((*cur == 0) || (*cur != ':'))) {
  1507. /*
  1508. * we can use the VALID_MDAY macro to validate the month
  1509. * and day because the leap year test will flag year zero
  1510. * as a leap year (even though zero is an invalid year).
  1511. * FUTURE TODO: Zero will become valid in XML Schema 1.1
  1512. * probably.
  1513. */
  1514. if (VALID_MDAY((&(dt->value.date)))) {
  1515. RETURN_TYPE_IF_VALID(XML_SCHEMAS_GMONTHDAY);
  1516. goto error;
  1517. }
  1518. }
  1519. /*
  1520. * not xs:gMonthDay so rewind and check if just xs:gMonth
  1521. * with an optional time zone.
  1522. */
  1523. cur = rewnd;
  1524. }
  1525. RETURN_TYPE_IF_VALID(XML_SCHEMAS_GMONTH);
  1526. goto error;
  1527. }
  1528. /*
  1529. * It's a right-truncated date or an xs:time.
  1530. * Try to parse an xs:time then fallback on right-truncated dates.
  1531. */
  1532. if ((*cur >= '0') && (*cur <= '9')) {
  1533. ret = _xmlSchemaParseTime(&(dt->value.date), &cur);
  1534. if (ret == 0) {
  1535. /* it's an xs:time */
  1536. RETURN_TYPE_IF_VALID(XML_SCHEMAS_TIME);
  1537. }
  1538. }
  1539. /* fallback on date parsing */
  1540. cur = dateTime;
  1541. ret = _xmlSchemaParseGYear(&(dt->value.date), &cur);
  1542. if (ret != 0)
  1543. goto error;
  1544. /* is it an xs:gYear? */
  1545. RETURN_TYPE_IF_VALID(XML_SCHEMAS_GYEAR);
  1546. if (*cur != '-')
  1547. goto error;
  1548. cur++;
  1549. ret = _xmlSchemaParseGMonth(&(dt->value.date), &cur);
  1550. if (ret != 0)
  1551. goto error;
  1552. /* is it an xs:gYearMonth? */
  1553. RETURN_TYPE_IF_VALID(XML_SCHEMAS_GYEARMONTH);
  1554. if (*cur != '-')
  1555. goto error;
  1556. cur++;
  1557. ret = _xmlSchemaParseGDay(&(dt->value.date), &cur);
  1558. if ((ret != 0) || !VALID_DATE((&(dt->value.date))))
  1559. goto error;
  1560. /* is it an xs:date? */
  1561. RETURN_TYPE_IF_VALID(XML_SCHEMAS_DATE);
  1562. if (*cur != 'T')
  1563. goto error;
  1564. cur++;
  1565. /* it should be an xs:dateTime */
  1566. ret = _xmlSchemaParseTime(&(dt->value.date), &cur);
  1567. if (ret != 0)
  1568. goto error;
  1569. ret = _xmlSchemaParseTimeZone(&(dt->value.date), &cur);
  1570. if (collapse)
  1571. while IS_WSP_BLANK_CH(*cur) cur++;
  1572. if ((ret != 0) || (*cur != 0) || (!(VALID_DATETIME((&(dt->value.date))))))
  1573. goto error;
  1574. dt->type = XML_SCHEMAS_DATETIME;
  1575. done:
  1576. #if 1
  1577. if ((type != XML_SCHEMAS_UNKNOWN) && (type != dt->type))
  1578. goto error;
  1579. #else
  1580. /*
  1581. * insure the parsed type is equal to or less significant (right
  1582. * truncated) than the desired type.
  1583. */
  1584. if ((type != XML_SCHEMAS_UNKNOWN) && (type != dt->type)) {
  1585. /* time only matches time */
  1586. if ((type == XML_SCHEMAS_TIME) && (dt->type == XML_SCHEMAS_TIME))
  1587. goto error;
  1588. if ((type == XML_SCHEMAS_DATETIME) &&
  1589. ((dt->type != XML_SCHEMAS_DATE) ||
  1590. (dt->type != XML_SCHEMAS_GYEARMONTH) ||
  1591. (dt->type != XML_SCHEMAS_GYEAR)))
  1592. goto error;
  1593. if ((type == XML_SCHEMAS_DATE) &&
  1594. ((dt->type != XML_SCHEMAS_GYEAR) ||
  1595. (dt->type != XML_SCHEMAS_GYEARMONTH)))
  1596. goto error;
  1597. if ((type == XML_SCHEMAS_GYEARMONTH) && (dt->type != XML_SCHEMAS_GYEAR))
  1598. goto error;
  1599. if ((type == XML_SCHEMAS_GMONTHDAY) && (dt->type != XML_SCHEMAS_GMONTH))
  1600. goto error;
  1601. }
  1602. #endif
  1603. if (val != NULL)
  1604. *val = dt;
  1605. else
  1606. xmlSchemaFreeValue(dt);
  1607. return 0;
  1608. error:
  1609. if (dt != NULL)
  1610. xmlSchemaFreeValue(dt);
  1611. return 1;
  1612. }
  1613. /**
  1614. * xmlSchemaValidateDuration:
  1615. * @type: the predefined type
  1616. * @duration: string to analyze
  1617. * @val: the return computed value
  1618. *
  1619. * Check that @duration conforms to the lexical space of the duration type.
  1620. * if true a value is computed and returned in @val.
  1621. *
  1622. * Returns 0 if this validates, a positive error code number otherwise
  1623. * and -1 in case of internal or API error.
  1624. */
  1625. static int
  1626. xmlSchemaValidateDuration (xmlSchemaTypePtr type ATTRIBUTE_UNUSED,
  1627. const xmlChar *duration, xmlSchemaValPtr *val,
  1628. int collapse) {
  1629. const xmlChar *cur = duration;
  1630. xmlSchemaValPtr dur;
  1631. int isneg = 0;
  1632. unsigned int seq = 0;
  1633. long days, secs = 0;
  1634. double sec_frac = 0.0;
  1635. if (duration == NULL)
  1636. return -1;
  1637. if (collapse)
  1638. while IS_WSP_BLANK_CH(*cur) cur++;
  1639. if (*cur == '-') {
  1640. isneg = 1;
  1641. cur++;
  1642. }
  1643. /* duration must start with 'P' (after sign) */
  1644. if (*cur++ != 'P')
  1645. return 1;
  1646. if (*cur == 0)
  1647. return 1;
  1648. dur = xmlSchemaNewValue(XML_SCHEMAS_DURATION);
  1649. if (dur == NULL)
  1650. return -1;
  1651. while (*cur != 0) {
  1652. long num = 0;
  1653. size_t has_digits = 0;
  1654. int has_frac = 0;
  1655. const xmlChar desig[] = {'Y', 'M', 'D', 'H', 'M', 'S'};
  1656. /* input string should be empty or invalid date/time item */
  1657. if (seq >= sizeof(desig))
  1658. goto error;
  1659. /* T designator must be present for time items */
  1660. if (*cur == 'T') {
  1661. if (seq > 3)
  1662. goto error;
  1663. cur++;
  1664. seq = 3;
  1665. } else if (seq == 3)
  1666. goto error;
  1667. /* Parse integral part. */
  1668. while (*cur >= '0' && *cur <= '9') {
  1669. long digit = *cur - '0';
  1670. if (num > LONG_MAX / 10)
  1671. goto error;
  1672. num *= 10;
  1673. if (num > LONG_MAX - digit)
  1674. goto error;
  1675. num += digit;
  1676. has_digits = 1;
  1677. cur++;
  1678. }
  1679. if (*cur == '.') {
  1680. /* Parse fractional part. */
  1681. double mult = 1.0;
  1682. cur++;
  1683. has_frac = 1;
  1684. while (*cur >= '0' && *cur <= '9') {
  1685. mult /= 10.0;
  1686. sec_frac += (*cur - '0') * mult;
  1687. has_digits = 1;
  1688. cur++;
  1689. }
  1690. }
  1691. while (*cur != desig[seq]) {
  1692. seq++;
  1693. /* No T designator or invalid char. */
  1694. if (seq == 3 || seq == sizeof(desig))
  1695. goto error;
  1696. }
  1697. cur++;
  1698. if (!has_digits || (has_frac && (seq != 5)))
  1699. goto error;
  1700. switch (seq) {
  1701. case 0:
  1702. /* Year */
  1703. if (num > LONG_MAX / 12)
  1704. goto error;
  1705. dur->value.dur.mon = num * 12;
  1706. break;
  1707. case 1:
  1708. /* Month */
  1709. if (dur->value.dur.mon > LONG_MAX - num)
  1710. goto error;
  1711. dur->value.dur.mon += num;
  1712. break;
  1713. case 2:
  1714. /* Day */
  1715. dur->value.dur.day = num;
  1716. break;
  1717. case 3:
  1718. /* Hour */
  1719. days = num / HOURS_PER_DAY;
  1720. if (dur->value.dur.day > LONG_MAX - days)
  1721. goto error;
  1722. dur->value.dur.day += days;
  1723. secs = (num % HOURS_PER_DAY) * SECS_PER_HOUR;
  1724. break;
  1725. case 4:
  1726. /* Minute */
  1727. days = num / MINS_PER_DAY;
  1728. if (dur->value.dur.day > LONG_MAX - days)
  1729. goto error;
  1730. dur->value.dur.day += days;
  1731. secs += (num % MINS_PER_DAY) * SECS_PER_MIN;
  1732. break;
  1733. case 5:
  1734. /* Second */
  1735. days = num / SECS_PER_DAY;
  1736. if (dur->value.dur.day > LONG_MAX - days)
  1737. goto error;
  1738. dur->value.dur.day += days;
  1739. secs += num % SECS_PER_DAY;
  1740. break;
  1741. }
  1742. seq++;
  1743. }
  1744. days = secs / SECS_PER_DAY;
  1745. if (dur->value.dur.day > LONG_MAX - days)
  1746. goto error;
  1747. dur->value.dur.day += days;
  1748. dur->value.dur.sec = (secs % SECS_PER_DAY) + sec_frac;
  1749. if (isneg) {
  1750. dur->value.dur.mon = -dur->value.dur.mon;
  1751. dur->value.dur.day = -dur->value.dur.day;
  1752. dur->value.dur.sec = -dur->value.dur.sec;
  1753. }
  1754. if (val != NULL)
  1755. *val = dur;
  1756. else
  1757. xmlSchemaFreeValue(dur);
  1758. return 0;
  1759. error:
  1760. if (dur != NULL)
  1761. xmlSchemaFreeValue(dur);
  1762. return 1;
  1763. }
  1764. /**
  1765. * xmlSchemaStrip:
  1766. * @value: a value
  1767. *
  1768. * Removes the leading and ending spaces of a string
  1769. *
  1770. * Returns the new string or NULL if no change was required.
  1771. */
  1772. static xmlChar *
  1773. xmlSchemaStrip(const xmlChar *value) {
  1774. const xmlChar *start = value, *end, *f;
  1775. if (value == NULL) return(NULL);
  1776. while ((*start != 0) && (IS_BLANK_CH(*start))) start++;
  1777. end = start;
  1778. while (*end != 0) end++;
  1779. f = end;
  1780. end--;
  1781. while ((end > start) && (IS_BLANK_CH(*end))) end--;
  1782. end++;
  1783. if ((start == value) && (f == end)) return(NULL);
  1784. return(xmlStrndup(start, end - start));
  1785. }
  1786. /**
  1787. * xmlSchemaWhiteSpaceReplace:
  1788. * @value: a value
  1789. *
  1790. * Replaces 0xd, 0x9 and 0xa with a space.
  1791. *
  1792. * Returns the new string or NULL if no change was required.
  1793. */
  1794. xmlChar *
  1795. xmlSchemaWhiteSpaceReplace(const xmlChar *value) {
  1796. const xmlChar *cur = value;
  1797. xmlChar *ret = NULL, *mcur;
  1798. if (value == NULL)
  1799. return(NULL);
  1800. while ((*cur != 0) &&
  1801. (((*cur) != 0xd) && ((*cur) != 0x9) && ((*cur) != 0xa))) {
  1802. cur++;
  1803. }
  1804. if (*cur == 0)
  1805. return (NULL);
  1806. ret = xmlStrdup(value);
  1807. /* TODO FIXME: I guess gcc will bark at this. */
  1808. mcur = (xmlChar *) (ret + (cur - value));
  1809. do {
  1810. if ( ((*mcur) == 0xd) || ((*mcur) == 0x9) || ((*mcur) == 0xa) )
  1811. *mcur = ' ';
  1812. mcur++;
  1813. } while (*mcur != 0);
  1814. return(ret);
  1815. }
  1816. /**
  1817. * xmlSchemaCollapseString:
  1818. * @value: a value
  1819. *
  1820. * Removes and normalize white spaces in the string
  1821. *
  1822. * Returns the new string or NULL if no change was required.
  1823. */
  1824. xmlChar *
  1825. xmlSchemaCollapseString(const xmlChar *value) {
  1826. const xmlChar *start = value, *end, *f;
  1827. xmlChar *g;
  1828. int col = 0;
  1829. if (value == NULL) return(NULL);
  1830. while ((*start != 0) && (IS_BLANK_CH(*start))) start++;
  1831. end = start;
  1832. while (*end != 0) {
  1833. if ((*end == ' ') && (IS_BLANK_CH(end[1]))) {
  1834. col = end - start;
  1835. break;
  1836. } else if ((*end == 0xa) || (*end == 0x9) || (*end == 0xd)) {
  1837. col = end - start;
  1838. break;
  1839. }
  1840. end++;
  1841. }
  1842. if (col == 0) {
  1843. f = end;
  1844. end--;
  1845. while ((end > start) && (IS_BLANK_CH(*end))) end--;
  1846. end++;
  1847. if ((start == value) && (f == end)) return(NULL);
  1848. return(xmlStrndup(start, end - start));
  1849. }
  1850. start = xmlStrdup(start);
  1851. if (start == NULL) return(NULL);
  1852. g = (xmlChar *) (start + col);
  1853. end = g;
  1854. while (*end != 0) {
  1855. if (IS_BLANK_CH(*end)) {
  1856. end++;
  1857. while (IS_BLANK_CH(*end)) end++;
  1858. if (*end != 0)
  1859. *g++ = ' ';
  1860. } else
  1861. *g++ = *end++;
  1862. }
  1863. *g = 0;
  1864. return((xmlChar *) start);
  1865. }
  1866. /**
  1867. * xmlSchemaValAtomicListNode:
  1868. * @type: the predefined atomic type for a token in the list
  1869. * @value: the list value to check
  1870. * @ret: the return computed value
  1871. * @node: the node containing the value
  1872. *
  1873. * Check that a value conforms to the lexical space of the predefined
  1874. * list type. if true a value is computed and returned in @ret.
  1875. *
  1876. * Returns the number of items if this validates, a negative error code
  1877. * number otherwise
  1878. */
  1879. static int
  1880. xmlSchemaValAtomicListNode(xmlSchemaTypePtr type, const xmlChar *value,
  1881. xmlSchemaValPtr *ret, xmlNodePtr node) {
  1882. xmlChar *val, *cur, *endval;
  1883. int nb_values = 0;
  1884. int tmp = 0;
  1885. if (value == NULL) {
  1886. return(-1);
  1887. }
  1888. val = xmlStrdup(value);
  1889. if (val == NULL) {
  1890. return(-1);
  1891. }
  1892. if (ret != NULL) {
  1893. *ret = NULL;
  1894. }
  1895. cur = val;
  1896. /*
  1897. * Split the list
  1898. */
  1899. while (IS_BLANK_CH(*cur)) *cur++ = 0;
  1900. while (*cur != 0) {
  1901. if (IS_BLANK_CH(*cur)) {
  1902. *cur = 0;
  1903. cur++;
  1904. while (IS_BLANK_CH(*cur)) *cur++ = 0;
  1905. } else {
  1906. nb_values++;
  1907. cur++;
  1908. while ((*cur != 0) && (!IS_BLANK_CH(*cur))) cur++;
  1909. }
  1910. }
  1911. if (nb_values == 0) {
  1912. xmlFree(val);
  1913. return(nb_values);
  1914. }
  1915. endval = cur;
  1916. cur = val;
  1917. while ((*cur == 0) && (cur != endval)) cur++;
  1918. while (cur != endval) {
  1919. tmp = xmlSchemaValPredefTypeNode(type, cur, NULL, node);
  1920. if (tmp != 0)
  1921. break;
  1922. while (*cur != 0) cur++;
  1923. while ((*cur == 0) && (cur != endval)) cur++;
  1924. }
  1925. /* TODO what return value ? c.f. bug #158628
  1926. if (ret != NULL) {
  1927. TODO
  1928. } */
  1929. xmlFree(val);
  1930. if (tmp == 0)
  1931. return(nb_values);
  1932. return(-1);
  1933. }
  1934. /**
  1935. * xmlSchemaParseUInt:
  1936. * @str: pointer to the string R/W
  1937. * @llo: pointer to the low result
  1938. * @lmi: pointer to the mid result
  1939. * @lhi: pointer to the high result
  1940. *
  1941. * Parse an unsigned long into 3 fields.
  1942. *
  1943. * Returns the number of significant digits in the number or
  1944. * -1 if overflow of the capacity and -2 if it's not a number.
  1945. */
  1946. static int
  1947. xmlSchemaParseUInt(const xmlChar **str, unsigned long *llo,
  1948. unsigned long *lmi, unsigned long *lhi) {
  1949. unsigned long lo = 0, mi = 0, hi = 0;
  1950. const xmlChar *tmp, *cur = *str;
  1951. int ret = 0, i = 0;
  1952. if (!((*cur >= '0') && (*cur <= '9')))
  1953. return(-2);
  1954. while (*cur == '0') { /* ignore leading zeroes */
  1955. cur++;
  1956. }
  1957. tmp = cur;
  1958. while ((*tmp != 0) && (*tmp >= '0') && (*tmp <= '9')) {
  1959. i++;tmp++;ret++;
  1960. }
  1961. if (i > 24) {
  1962. *str = tmp;
  1963. return(-1);
  1964. }
  1965. while (i > 16) {
  1966. hi = hi * 10 + (*cur++ - '0');
  1967. i--;
  1968. }
  1969. while (i > 8) {
  1970. mi = mi * 10 + (*cur++ - '0');
  1971. i--;
  1972. }
  1973. while (i > 0) {
  1974. lo = lo * 10 + (*cur++ - '0');
  1975. i--;
  1976. }
  1977. *str = cur;
  1978. *llo = lo;
  1979. *lmi = mi;
  1980. *lhi = hi;
  1981. return(ret);
  1982. }
  1983. /*
  1984. * xmlSchemaCheckLanguageType
  1985. * @value: the value to check
  1986. *
  1987. * Check that a value conforms to the lexical space of the language datatype.
  1988. * Must conform to [a-zA-Z]{1,8}(-[a-zA-Z0-9]{1,8})*
  1989. *
  1990. * Returns 1 if this validates, 0 otherwise.
  1991. */
  1992. static int
  1993. xmlSchemaCheckLanguageType(const xmlChar* value) {
  1994. int first = 1, len = 0;
  1995. const xmlChar* cur = value;
  1996. if (value == NULL)
  1997. return (0);
  1998. while (cur[0] != 0) {
  1999. if (!( ((cur[0] >= 'a') && (cur[0] <= 'z')) || ((cur[0] >= 'A') && (cur[0] <= 'Z'))
  2000. || (cur[0] == '-')
  2001. || ((first == 0) && (xmlIsDigit_ch(cur[0]))) ))
  2002. return (0);
  2003. if (cur[0] == '-') {
  2004. if ((len < 1) || (len > 8))
  2005. return (0);
  2006. len = 0;
  2007. first = 0;
  2008. }
  2009. else
  2010. len++;
  2011. cur++;
  2012. }
  2013. if ((len < 1) || (len > 8))
  2014. return (0);
  2015. return (1);
  2016. }
  2017. /**
  2018. * xmlSchemaValAtomicType:
  2019. * @type: the predefined type
  2020. * @value: the value to check
  2021. * @val: the return computed value
  2022. * @node: the node containing the value
  2023. * flags: flags to control the validation
  2024. *
  2025. * Check that a value conforms to the lexical space of the atomic type.
  2026. * if true a value is computed and returned in @val.
  2027. * This checks the value space for list types as well (IDREFS, NMTOKENS).
  2028. *
  2029. * Returns 0 if this validates, a positive error code number otherwise
  2030. * and -1 in case of internal or API error.
  2031. */
  2032. static int
  2033. xmlSchemaValAtomicType(xmlSchemaTypePtr type, const xmlChar * value,
  2034. xmlSchemaValPtr * val, xmlNodePtr node, int flags,
  2035. xmlSchemaWhitespaceValueType ws,
  2036. int normOnTheFly, int applyNorm, int createStringValue)
  2037. {
  2038. xmlSchemaValPtr v;
  2039. xmlChar *norm = NULL;
  2040. int ret = 0;
  2041. if (xmlSchemaTypesInitialized == 0)
  2042. xmlSchemaInitTypes();
  2043. if (type == NULL)
  2044. return (-1);
  2045. /*
  2046. * validating a non existent text node is similar to validating
  2047. * an empty one.
  2048. */
  2049. if (value == NULL)
  2050. value = BAD_CAST "";
  2051. if (val != NULL)
  2052. *val = NULL;
  2053. if ((flags == 0) && (value != NULL)) {
  2054. if ((type->builtInType != XML_SCHEMAS_STRING) &&
  2055. (type->builtInType != XML_SCHEMAS_ANYTYPE) &&
  2056. (type->builtInType != XML_SCHEMAS_ANYSIMPLETYPE)) {
  2057. if (type->builtInType == XML_SCHEMAS_NORMSTRING)
  2058. norm = xmlSchemaWhiteSpaceReplace(value);
  2059. else
  2060. norm = xmlSchemaCollapseString(value);
  2061. if (norm != NULL)
  2062. value = norm;
  2063. }
  2064. }
  2065. switch (type->builtInType) {
  2066. case XML_SCHEMAS_UNKNOWN:
  2067. goto error;
  2068. case XML_SCHEMAS_ANYTYPE:
  2069. case XML_SCHEMAS_ANYSIMPLETYPE:
  2070. if ((createStringValue) && (val != NULL)) {
  2071. v = xmlSchemaNewValue(XML_SCHEMAS_ANYSIMPLETYPE);
  2072. if (v != NULL) {
  2073. v->value.str = xmlStrdup(value);
  2074. *val = v;
  2075. } else {
  2076. goto error;
  2077. }
  2078. }
  2079. goto return0;
  2080. case XML_SCHEMAS_STRING:
  2081. if (! normOnTheFly) {
  2082. const xmlChar *cur = value;
  2083. if (ws == XML_SCHEMA_WHITESPACE_REPLACE) {
  2084. while (*cur != 0) {
  2085. if ((*cur == 0xd) || (*cur == 0xa) || (*cur == 0x9)) {
  2086. goto return1;
  2087. } else {
  2088. cur++;
  2089. }
  2090. }
  2091. } else if (ws == XML_SCHEMA_WHITESPACE_COLLAPSE) {
  2092. while (*cur != 0) {
  2093. if ((*cur == 0xd) || (*cur == 0xa) || (*cur == 0x9)) {
  2094. goto return1;
  2095. } else if IS_WSP_SPACE_CH(*cur) {
  2096. cur++;
  2097. if IS_WSP_SPACE_CH(*cur)
  2098. goto return1;
  2099. } else {
  2100. cur++;
  2101. }
  2102. }
  2103. }
  2104. }
  2105. if (createStringValue && (val != NULL)) {
  2106. if (applyNorm) {
  2107. if (ws == XML_SCHEMA_WHITESPACE_COLLAPSE)
  2108. norm = xmlSchemaCollapseString(value);
  2109. else if (ws == XML_SCHEMA_WHITESPACE_REPLACE)
  2110. norm = xmlSchemaWhiteSpaceReplace(value);
  2111. if (norm != NULL)
  2112. value = norm;
  2113. }
  2114. v = xmlSchemaNewValue(XML_SCHEMAS_STRING);
  2115. if (v != NULL) {
  2116. v->value.str = xmlStrdup(value);
  2117. *val = v;
  2118. } else {
  2119. goto error;
  2120. }
  2121. }
  2122. goto return0;
  2123. case XML_SCHEMAS_NORMSTRING:{
  2124. if (normOnTheFly) {
  2125. if (applyNorm) {
  2126. if (ws == XML_SCHEMA_WHITESPACE_COLLAPSE)
  2127. norm = xmlSchemaCollapseString(value);
  2128. else
  2129. norm = xmlSchemaWhiteSpaceReplace(value);
  2130. if (norm != NULL)
  2131. value = norm;
  2132. }
  2133. } else {
  2134. const xmlChar *cur = value;
  2135. while (*cur != 0) {
  2136. if ((*cur == 0xd) || (*cur == 0xa) || (*cur == 0x9)) {
  2137. goto return1;
  2138. } else {
  2139. cur++;
  2140. }
  2141. }
  2142. }
  2143. if (val != NULL) {
  2144. v = xmlSchemaNewValue(XML_SCHEMAS_NORMSTRING);
  2145. if (v != NULL) {
  2146. v->value.str = xmlStrdup(value);
  2147. *val = v;
  2148. } else {
  2149. goto error;
  2150. }
  2151. }
  2152. goto return0;
  2153. }
  2154. case XML_SCHEMAS_DECIMAL:{
  2155. const xmlChar *cur = value;
  2156. unsigned int len, neg, integ, hasLeadingZeroes;
  2157. xmlChar cval[25];
  2158. xmlChar *cptr = cval;
  2159. if ((cur == NULL) || (*cur == 0))
  2160. goto return1;
  2161. /*
  2162. * xs:decimal has a whitespace-facet value of 'collapse'.
  2163. */
  2164. if (normOnTheFly)
  2165. while IS_WSP_BLANK_CH(*cur) cur++;
  2166. /*
  2167. * First we handle an optional sign.
  2168. */
  2169. neg = 0;
  2170. if (*cur == '-') {
  2171. neg = 1;
  2172. cur++;
  2173. } else if (*cur == '+')
  2174. cur++;
  2175. /*
  2176. * Disallow: "", "-", "- "
  2177. */
  2178. if (*cur == 0)
  2179. goto return1;
  2180. /*
  2181. * Next we "pre-parse" the number, in preparation for calling
  2182. * the common routine xmlSchemaParseUInt. We get rid of any
  2183. * leading zeroes (because we have reserved only 25 chars),
  2184. * and note the position of a decimal point.
  2185. */
  2186. len = 0;
  2187. integ = ~0u;
  2188. hasLeadingZeroes = 0;
  2189. /*
  2190. * Skip leading zeroes.
  2191. */
  2192. while (*cur == '0') {
  2193. cur++;
  2194. hasLeadingZeroes = 1;
  2195. }
  2196. if (*cur != 0) {
  2197. do {
  2198. if ((*cur >= '0') && (*cur <= '9')) {
  2199. *cptr++ = *cur++;
  2200. len++;
  2201. } else if (*cur == '.') {
  2202. cur++;
  2203. integ = len;
  2204. do {
  2205. if ((*cur >= '0') && (*cur <= '9')) {
  2206. *cptr++ = *cur++;
  2207. len++;
  2208. } else
  2209. break;
  2210. } while (len < 24);
  2211. /*
  2212. * Disallow "." but allow "00."
  2213. */
  2214. if ((len == 0) && (!hasLeadingZeroes))
  2215. goto return1;
  2216. break;
  2217. } else
  2218. break;
  2219. } while (len < 24);
  2220. }
  2221. if (normOnTheFly)
  2222. while IS_WSP_BLANK_CH(*cur) cur++;
  2223. if (*cur != 0)
  2224. goto return1; /* error if any extraneous chars */
  2225. if (val != NULL) {
  2226. v = xmlSchemaNewValue(XML_SCHEMAS_DECIMAL);
  2227. if (v != NULL) {
  2228. /*
  2229. * Now evaluate the significant digits of the number
  2230. */
  2231. if (len != 0) {
  2232. if (integ != ~0u) {
  2233. /*
  2234. * Get rid of trailing zeroes in the
  2235. * fractional part.
  2236. */
  2237. while ((len != integ) && (*(cptr-1) == '0')) {
  2238. cptr--;
  2239. len--;
  2240. }
  2241. }
  2242. /*
  2243. * Terminate the (preparsed) string.
  2244. */
  2245. if (len != 0) {
  2246. *cptr = 0;
  2247. cptr = cval;
  2248. xmlSchemaParseUInt((const xmlChar **)&cptr,
  2249. &v->value.decimal.lo,
  2250. &v->value.decimal.mi,
  2251. &v->value.decimal.hi);
  2252. }
  2253. }
  2254. /*
  2255. * Set the total digits to 1 if a zero value.
  2256. */
  2257. v->value.decimal.sign = neg;
  2258. if (len == 0) {
  2259. /* Speedup for zero values. */
  2260. v->value.decimal.total = 1;
  2261. } else {
  2262. v->value.decimal.total = len;
  2263. if (integ == ~0u)
  2264. v->value.decimal.frac = 0;
  2265. else
  2266. v->value.decimal.frac = len - integ;
  2267. }
  2268. *val = v;
  2269. }
  2270. }
  2271. goto return0;
  2272. }
  2273. case XML_SCHEMAS_TIME:
  2274. case XML_SCHEMAS_GDAY:
  2275. case XML_SCHEMAS_GMONTH:
  2276. case XML_SCHEMAS_GMONTHDAY:
  2277. case XML_SCHEMAS_GYEAR:
  2278. case XML_SCHEMAS_GYEARMONTH:
  2279. case XML_SCHEMAS_DATE:
  2280. case XML_SCHEMAS_DATETIME:
  2281. ret = xmlSchemaValidateDates(type->builtInType, value, val,
  2282. normOnTheFly);
  2283. break;
  2284. case XML_SCHEMAS_DURATION:
  2285. ret = xmlSchemaValidateDuration(type, value, val,
  2286. normOnTheFly);
  2287. break;
  2288. case XML_SCHEMAS_FLOAT:
  2289. case XML_SCHEMAS_DOUBLE: {
  2290. const xmlChar *cur = value;
  2291. int neg = 0;
  2292. int digits_before = 0;
  2293. int digits_after = 0;
  2294. if (normOnTheFly)
  2295. while IS_WSP_BLANK_CH(*cur) cur++;
  2296. if ((cur[0] == 'N') && (cur[1] == 'a') && (cur[2] == 'N')) {
  2297. cur += 3;
  2298. if (*cur != 0)
  2299. goto return1;
  2300. if (val != NULL) {
  2301. if (type == xmlSchemaTypeFloatDef) {
  2302. v = xmlSchemaNewValue(XML_SCHEMAS_FLOAT);
  2303. if (v != NULL) {
  2304. v->value.f = (float) xmlXPathNAN;
  2305. } else {
  2306. xmlSchemaFreeValue(v);
  2307. goto error;
  2308. }
  2309. } else {
  2310. v = xmlSchemaNewValue(XML_SCHEMAS_DOUBLE);
  2311. if (v != NULL) {
  2312. v->value.d = xmlXPathNAN;
  2313. } else {
  2314. xmlSchemaFreeValue(v);
  2315. goto error;
  2316. }
  2317. }
  2318. *val = v;
  2319. }
  2320. goto return0;
  2321. }
  2322. if (*cur == '-') {
  2323. neg = 1;
  2324. cur++;
  2325. }
  2326. if ((cur[0] == 'I') && (cur[1] == 'N') && (cur[2] == 'F')) {
  2327. cur += 3;
  2328. if (*cur != 0)
  2329. goto return1;
  2330. if (val != NULL) {
  2331. if (type == xmlSchemaTypeFloatDef) {
  2332. v = xmlSchemaNewValue(XML_SCHEMAS_FLOAT);
  2333. if (v != NULL) {
  2334. if (neg)
  2335. v->value.f = (float) xmlXPathNINF;
  2336. else
  2337. v->value.f = (float) xmlXPathPINF;
  2338. } else {
  2339. xmlSchemaFreeValue(v);
  2340. goto error;
  2341. }
  2342. } else {
  2343. v = xmlSchemaNewValue(XML_SCHEMAS_DOUBLE);
  2344. if (v != NULL) {
  2345. if (neg)
  2346. v->value.d = xmlXPathNINF;
  2347. else
  2348. v->value.d = xmlXPathPINF;
  2349. } else {
  2350. xmlSchemaFreeValue(v);
  2351. goto error;
  2352. }
  2353. }
  2354. *val = v;
  2355. }
  2356. goto return0;
  2357. }
  2358. if ((neg == 0) && (*cur == '+'))
  2359. cur++;
  2360. if ((cur[0] == 0) || (cur[0] == '+') || (cur[0] == '-'))
  2361. goto return1;
  2362. while ((*cur >= '0') && (*cur <= '9')) {
  2363. cur++;
  2364. digits_before++;
  2365. }
  2366. if (*cur == '.') {
  2367. cur++;
  2368. while ((*cur >= '0') && (*cur <= '9')) {
  2369. cur++;
  2370. digits_after++;
  2371. }
  2372. }
  2373. if ((digits_before == 0) && (digits_after == 0))
  2374. goto return1;
  2375. if ((*cur == 'e') || (*cur == 'E')) {
  2376. cur++;
  2377. if ((*cur == '-') || (*cur == '+'))
  2378. cur++;
  2379. while ((*cur >= '0') && (*cur <= '9'))
  2380. cur++;
  2381. }
  2382. if (normOnTheFly)
  2383. while IS_WSP_BLANK_CH(*cur) cur++;
  2384. if (*cur != 0)
  2385. goto return1;
  2386. if (val != NULL) {
  2387. if (type == xmlSchemaTypeFloatDef) {
  2388. v = xmlSchemaNewValue(XML_SCHEMAS_FLOAT);
  2389. if (v != NULL) {
  2390. /*
  2391. * TODO: sscanf seems not to give the correct
  2392. * value for extremely high/low values.
  2393. * E.g. "1E-149" results in zero.
  2394. */
  2395. if (sscanf((const char *) value, "%f",
  2396. &(v->value.f)) == 1) {
  2397. *val = v;
  2398. } else {
  2399. xmlSchemaFreeValue(v);
  2400. goto return1;
  2401. }
  2402. } else {
  2403. goto error;
  2404. }
  2405. } else {
  2406. v = xmlSchemaNewValue(XML_SCHEMAS_DOUBLE);
  2407. if (v != NULL) {
  2408. /*
  2409. * TODO: sscanf seems not to give the correct
  2410. * value for extremely high/low values.
  2411. */
  2412. if (sscanf((const char *) value, "%lf",
  2413. &(v->value.d)) == 1) {
  2414. *val = v;
  2415. } else {
  2416. xmlSchemaFreeValue(v);
  2417. goto return1;
  2418. }
  2419. } else {
  2420. goto error;
  2421. }
  2422. }
  2423. }
  2424. goto return0;
  2425. }
  2426. case XML_SCHEMAS_BOOLEAN:{
  2427. const xmlChar *cur = value;
  2428. if (normOnTheFly) {
  2429. while IS_WSP_BLANK_CH(*cur) cur++;
  2430. if (*cur == '0') {
  2431. ret = 0;
  2432. cur++;
  2433. } else if (*cur == '1') {
  2434. ret = 1;
  2435. cur++;
  2436. } else if (*cur == 't') {
  2437. cur++;
  2438. if ((*cur++ == 'r') && (*cur++ == 'u') &&
  2439. (*cur++ == 'e')) {
  2440. ret = 1;
  2441. } else
  2442. goto return1;
  2443. } else if (*cur == 'f') {
  2444. cur++;
  2445. if ((*cur++ == 'a') && (*cur++ == 'l') &&
  2446. (*cur++ == 's') && (*cur++ == 'e')) {
  2447. ret = 0;
  2448. } else
  2449. goto return1;
  2450. } else
  2451. goto return1;
  2452. if (*cur != 0) {
  2453. while IS_WSP_BLANK_CH(*cur) cur++;
  2454. if (*cur != 0)
  2455. goto return1;
  2456. }
  2457. } else {
  2458. if ((cur[0] == '0') && (cur[1] == 0))
  2459. ret = 0;
  2460. else if ((cur[0] == '1') && (cur[1] == 0))
  2461. ret = 1;
  2462. else if ((cur[0] == 't') && (cur[1] == 'r')
  2463. && (cur[2] == 'u') && (cur[3] == 'e')
  2464. && (cur[4] == 0))
  2465. ret = 1;
  2466. else if ((cur[0] == 'f') && (cur[1] == 'a')
  2467. && (cur[2] == 'l') && (cur[3] == 's')
  2468. && (cur[4] == 'e') && (cur[5] == 0))
  2469. ret = 0;
  2470. else
  2471. goto return1;
  2472. }
  2473. if (val != NULL) {
  2474. v = xmlSchemaNewValue(XML_SCHEMAS_BOOLEAN);
  2475. if (v != NULL) {
  2476. v->value.b = ret;
  2477. *val = v;
  2478. } else {
  2479. goto error;
  2480. }
  2481. }
  2482. goto return0;
  2483. }
  2484. case XML_SCHEMAS_TOKEN:{
  2485. const xmlChar *cur = value;
  2486. if (! normOnTheFly) {
  2487. while (*cur != 0) {
  2488. if ((*cur == 0xd) || (*cur == 0xa) || (*cur == 0x9)) {
  2489. goto return1;
  2490. } else if (*cur == ' ') {
  2491. cur++;
  2492. if (*cur == 0)
  2493. goto return1;
  2494. if (*cur == ' ')
  2495. goto return1;
  2496. } else {
  2497. cur++;
  2498. }
  2499. }
  2500. }
  2501. if (val != NULL) {
  2502. v = xmlSchemaNewValue(XML_SCHEMAS_TOKEN);
  2503. if (v != NULL) {
  2504. v->value.str = xmlStrdup(value);
  2505. *val = v;
  2506. } else {
  2507. goto error;
  2508. }
  2509. }
  2510. goto return0;
  2511. }
  2512. case XML_SCHEMAS_LANGUAGE:
  2513. if ((norm == NULL) && (normOnTheFly)) {
  2514. norm = xmlSchemaCollapseString(value);
  2515. if (norm != NULL)
  2516. value = norm;
  2517. }
  2518. if (xmlSchemaCheckLanguageType(value) == 1) {
  2519. if (val != NULL) {
  2520. v = xmlSchemaNewValue(XML_SCHEMAS_LANGUAGE);
  2521. if (v != NULL) {
  2522. v->value.str = xmlStrdup(value);
  2523. *val = v;
  2524. } else {
  2525. goto error;
  2526. }
  2527. }
  2528. goto return0;
  2529. }
  2530. goto return1;
  2531. case XML_SCHEMAS_NMTOKEN:
  2532. if (xmlValidateNMToken(value, 1) == 0) {
  2533. if (val != NULL) {
  2534. v = xmlSchemaNewValue(XML_SCHEMAS_NMTOKEN);
  2535. if (v != NULL) {
  2536. v->value.str = xmlStrdup(value);
  2537. *val = v;
  2538. } else {
  2539. goto error;
  2540. }
  2541. }
  2542. goto return0;
  2543. }
  2544. goto return1;
  2545. case XML_SCHEMAS_NMTOKENS:
  2546. ret = xmlSchemaValAtomicListNode(xmlSchemaTypeNmtokenDef,
  2547. value, val, node);
  2548. if (ret > 0)
  2549. ret = 0;
  2550. else
  2551. ret = 1;
  2552. goto done;
  2553. case XML_SCHEMAS_NAME:
  2554. ret = xmlValidateName(value, 1);
  2555. if ((ret == 0) && (val != NULL) && (value != NULL)) {
  2556. v = xmlSchemaNewValue(XML_SCHEMAS_NAME);
  2557. if (v != NULL) {
  2558. const xmlChar *start = value, *end;
  2559. while (IS_BLANK_CH(*start)) start++;
  2560. end = start;
  2561. while ((*end != 0) && (!IS_BLANK_CH(*end))) end++;
  2562. v->value.str = xmlStrndup(start, end - start);
  2563. *val = v;
  2564. } else {
  2565. goto error;
  2566. }
  2567. }
  2568. goto done;
  2569. case XML_SCHEMAS_QNAME:{
  2570. const xmlChar *uri = NULL;
  2571. xmlChar *local = NULL;
  2572. ret = xmlValidateQName(value, 1);
  2573. if (ret != 0)
  2574. goto done;
  2575. if (node != NULL) {
  2576. xmlChar *prefix;
  2577. xmlNsPtr ns;
  2578. local = xmlSplitQName2(value, &prefix);
  2579. ns = xmlSearchNs(node->doc, node, prefix);
  2580. if ((ns == NULL) && (prefix != NULL)) {
  2581. xmlFree(prefix);
  2582. if (local != NULL)
  2583. xmlFree(local);
  2584. goto return1;
  2585. }
  2586. if (ns != NULL)
  2587. uri = ns->href;
  2588. if (prefix != NULL)
  2589. xmlFree(prefix);
  2590. }
  2591. if (val != NULL) {
  2592. v = xmlSchemaNewValue(XML_SCHEMAS_QNAME);
  2593. if (v == NULL) {
  2594. if (local != NULL)
  2595. xmlFree(local);
  2596. goto error;
  2597. }
  2598. if (local != NULL)
  2599. v->value.qname.name = local;
  2600. else
  2601. v->value.qname.name = xmlStrdup(value);
  2602. if (uri != NULL)
  2603. v->value.qname.uri = xmlStrdup(uri);
  2604. *val = v;
  2605. } else
  2606. if (local != NULL)
  2607. xmlFree(local);
  2608. goto done;
  2609. }
  2610. case XML_SCHEMAS_NCNAME:
  2611. ret = xmlValidateNCName(value, 1);
  2612. if ((ret == 0) && (val != NULL)) {
  2613. v = xmlSchemaNewValue(XML_SCHEMAS_NCNAME);
  2614. if (v != NULL) {
  2615. v->value.str = xmlStrdup(value);
  2616. *val = v;
  2617. } else {
  2618. goto error;
  2619. }
  2620. }
  2621. goto done;
  2622. case XML_SCHEMAS_ID:
  2623. ret = xmlValidateNCName(value, 1);
  2624. if ((ret == 0) && (val != NULL)) {
  2625. v = xmlSchemaNewValue(XML_SCHEMAS_ID);
  2626. if (v != NULL) {
  2627. v->value.str = xmlStrdup(value);
  2628. *val = v;
  2629. } else {
  2630. goto error;
  2631. }
  2632. }
  2633. if ((ret == 0) && (node != NULL) &&
  2634. (node->type == XML_ATTRIBUTE_NODE)) {
  2635. xmlAttrPtr attr = (xmlAttrPtr) node;
  2636. /*
  2637. * NOTE: the IDness might have already be declared in the DTD
  2638. */
  2639. if (attr->atype != XML_ATTRIBUTE_ID) {
  2640. xmlIDPtr res;
  2641. xmlChar *strip;
  2642. strip = xmlSchemaStrip(value);
  2643. if (strip != NULL) {
  2644. res = xmlAddID(NULL, node->doc, strip, attr);
  2645. xmlFree(strip);
  2646. } else
  2647. res = xmlAddID(NULL, node->doc, value, attr);
  2648. if (res == NULL) {
  2649. ret = 2;
  2650. } else {
  2651. attr->atype = XML_ATTRIBUTE_ID;
  2652. }
  2653. }
  2654. }
  2655. goto done;
  2656. case XML_SCHEMAS_IDREF:
  2657. ret = xmlValidateNCName(value, 1);
  2658. if ((ret == 0) && (val != NULL)) {
  2659. v = xmlSchemaNewValue(XML_SCHEMAS_IDREF);
  2660. if (v == NULL)
  2661. goto error;
  2662. v->value.str = xmlStrdup(value);
  2663. *val = v;
  2664. }
  2665. if ((ret == 0) && (node != NULL) &&
  2666. (node->type == XML_ATTRIBUTE_NODE)) {
  2667. xmlAttrPtr attr = (xmlAttrPtr) node;
  2668. xmlChar *strip;
  2669. strip = xmlSchemaStrip(value);
  2670. if (strip != NULL) {
  2671. xmlAddRef(NULL, node->doc, strip, attr);
  2672. xmlFree(strip);
  2673. } else
  2674. xmlAddRef(NULL, node->doc, value, attr);
  2675. attr->atype = XML_ATTRIBUTE_IDREF;
  2676. }
  2677. goto done;
  2678. case XML_SCHEMAS_IDREFS:
  2679. ret = xmlSchemaValAtomicListNode(xmlSchemaTypeIdrefDef,
  2680. value, val, node);
  2681. if (ret < 0)
  2682. ret = 2;
  2683. else
  2684. ret = 0;
  2685. if ((ret == 0) && (node != NULL) &&
  2686. (node->type == XML_ATTRIBUTE_NODE)) {
  2687. xmlAttrPtr attr = (xmlAttrPtr) node;
  2688. attr->atype = XML_ATTRIBUTE_IDREFS;
  2689. }
  2690. goto done;
  2691. case XML_SCHEMAS_ENTITY:{
  2692. xmlChar *strip;
  2693. ret = xmlValidateNCName(value, 1);
  2694. if ((node == NULL) || (node->doc == NULL))
  2695. ret = 3;
  2696. if (ret == 0) {
  2697. xmlEntityPtr ent;
  2698. strip = xmlSchemaStrip(value);
  2699. if (strip != NULL) {
  2700. ent = xmlGetDocEntity(node->doc, strip);
  2701. xmlFree(strip);
  2702. } else {
  2703. ent = xmlGetDocEntity(node->doc, value);
  2704. }
  2705. if ((ent == NULL) ||
  2706. (ent->etype !=
  2707. XML_EXTERNAL_GENERAL_UNPARSED_ENTITY))
  2708. ret = 4;
  2709. }
  2710. if ((ret == 0) && (val != NULL)) {
  2711. TODO;
  2712. }
  2713. if ((ret == 0) && (node != NULL) &&
  2714. (node->type == XML_ATTRIBUTE_NODE)) {
  2715. xmlAttrPtr attr = (xmlAttrPtr) node;
  2716. attr->atype = XML_ATTRIBUTE_ENTITY;
  2717. }
  2718. goto done;
  2719. }
  2720. case XML_SCHEMAS_ENTITIES:
  2721. if ((node == NULL) || (node->doc == NULL))
  2722. goto return3;
  2723. ret = xmlSchemaValAtomicListNode(xmlSchemaTypeEntityDef,
  2724. value, val, node);
  2725. if (ret <= 0)
  2726. ret = 1;
  2727. else
  2728. ret = 0;
  2729. if ((ret == 0) && (node != NULL) &&
  2730. (node->type == XML_ATTRIBUTE_NODE)) {
  2731. xmlAttrPtr attr = (xmlAttrPtr) node;
  2732. attr->atype = XML_ATTRIBUTE_ENTITIES;
  2733. }
  2734. goto done;
  2735. case XML_SCHEMAS_NOTATION:{
  2736. xmlChar *uri = NULL;
  2737. xmlChar *local = NULL;
  2738. ret = xmlValidateQName(value, 1);
  2739. if ((ret == 0) && (node != NULL)) {
  2740. xmlChar *prefix;
  2741. local = xmlSplitQName2(value, &prefix);
  2742. if (prefix != NULL) {
  2743. xmlNsPtr ns;
  2744. ns = xmlSearchNs(node->doc, node, prefix);
  2745. if (ns == NULL)
  2746. ret = 1;
  2747. else if (val != NULL)
  2748. uri = xmlStrdup(ns->href);
  2749. }
  2750. if ((local != NULL) && ((val == NULL) || (ret != 0)))
  2751. xmlFree(local);
  2752. if (prefix != NULL)
  2753. xmlFree(prefix);
  2754. }
  2755. if ((node == NULL) || (node->doc == NULL))
  2756. ret = 3;
  2757. if (ret == 0) {
  2758. ret = xmlValidateNotationUse(NULL, node->doc, value);
  2759. if (ret == 1)
  2760. ret = 0;
  2761. else
  2762. ret = 1;
  2763. }
  2764. if ((ret == 0) && (val != NULL)) {
  2765. v = xmlSchemaNewValue(XML_SCHEMAS_NOTATION);
  2766. if (v != NULL) {
  2767. if (local != NULL)
  2768. v->value.qname.name = local;
  2769. else
  2770. v->value.qname.name = xmlStrdup(value);
  2771. if (uri != NULL)
  2772. v->value.qname.uri = uri;
  2773. *val = v;
  2774. } else {
  2775. if (local != NULL)
  2776. xmlFree(local);
  2777. if (uri != NULL)
  2778. xmlFree(uri);
  2779. goto error;
  2780. }
  2781. }
  2782. goto done;
  2783. }
  2784. case XML_SCHEMAS_ANYURI:{
  2785. if (*value != 0) {
  2786. xmlURIPtr uri;
  2787. xmlChar *tmpval, *cur;
  2788. if ((norm == NULL) && (normOnTheFly)) {
  2789. norm = xmlSchemaCollapseString(value);
  2790. if (norm != NULL)
  2791. value = norm;
  2792. }
  2793. tmpval = xmlStrdup(value);
  2794. for (cur = tmpval; *cur; ++cur) {
  2795. if (*cur < 32 || *cur >= 127 || *cur == ' ' ||
  2796. *cur == '<' || *cur == '>' || *cur == '"' ||
  2797. *cur == '{' || *cur == '}' || *cur == '|' ||
  2798. *cur == '\\' || *cur == '^' || *cur == '`' ||
  2799. *cur == '\'')
  2800. *cur = '_';
  2801. }
  2802. uri = xmlParseURI((const char *) tmpval);
  2803. xmlFree(tmpval);
  2804. if (uri == NULL)
  2805. goto return1;
  2806. xmlFreeURI(uri);
  2807. }
  2808. if (val != NULL) {
  2809. v = xmlSchemaNewValue(XML_SCHEMAS_ANYURI);
  2810. if (v == NULL)
  2811. goto error;
  2812. v->value.str = xmlStrdup(value);
  2813. *val = v;
  2814. }
  2815. goto return0;
  2816. }
  2817. case XML_SCHEMAS_HEXBINARY:{
  2818. const xmlChar *cur = value, *start;
  2819. xmlChar *base;
  2820. int total, i = 0;
  2821. if (cur == NULL)
  2822. goto return1;
  2823. if (normOnTheFly)
  2824. while IS_WSP_BLANK_CH(*cur) cur++;
  2825. start = cur;
  2826. while (((*cur >= '0') && (*cur <= '9')) ||
  2827. ((*cur >= 'A') && (*cur <= 'F')) ||
  2828. ((*cur >= 'a') && (*cur <= 'f'))) {
  2829. i++;
  2830. cur++;
  2831. }
  2832. if (normOnTheFly)
  2833. while IS_WSP_BLANK_CH(*cur) cur++;
  2834. if (*cur != 0)
  2835. goto return1;
  2836. if ((i % 2) != 0)
  2837. goto return1;
  2838. if (val != NULL) {
  2839. v = xmlSchemaNewValue(XML_SCHEMAS_HEXBINARY);
  2840. if (v == NULL)
  2841. goto error;
  2842. /*
  2843. * Copy only the normalized piece.
  2844. * CRITICAL TODO: Check this.
  2845. */
  2846. cur = xmlStrndup(start, i);
  2847. if (cur == NULL) {
  2848. xmlSchemaTypeErrMemory(node, "allocating hexbin data");
  2849. xmlFree(v);
  2850. goto return1;
  2851. }
  2852. total = i / 2; /* number of octets */
  2853. base = (xmlChar *) cur;
  2854. while (i-- > 0) {
  2855. if (*base >= 'a')
  2856. *base = *base - ('a' - 'A');
  2857. base++;
  2858. }
  2859. v->value.hex.str = (xmlChar *) cur;
  2860. v->value.hex.total = total;
  2861. *val = v;
  2862. }
  2863. goto return0;
  2864. }
  2865. case XML_SCHEMAS_BASE64BINARY:{
  2866. /* ISSUE:
  2867. *
  2868. * Ignore all stray characters? (yes, currently)
  2869. * Worry about long lines? (no, currently)
  2870. *
  2871. * rfc2045.txt:
  2872. *
  2873. * "The encoded output stream must be represented in lines of
  2874. * no more than 76 characters each. All line breaks or other
  2875. * characters not found in Table 1 must be ignored by decoding
  2876. * software. In base64 data, characters other than those in
  2877. * Table 1, line breaks, and other white space probably
  2878. * indicate a transmission error, about which a warning
  2879. * message or even a message rejection might be appropriate
  2880. * under some circumstances." */
  2881. const xmlChar *cur = value;
  2882. xmlChar *base;
  2883. int total, i = 0, pad = 0;
  2884. if (cur == NULL)
  2885. goto return1;
  2886. for (; *cur; ++cur) {
  2887. int decc;
  2888. decc = _xmlSchemaBase64Decode(*cur);
  2889. if (decc < 0) ;
  2890. else if (decc < 64)
  2891. i++;
  2892. else
  2893. break;
  2894. }
  2895. for (; *cur; ++cur) {
  2896. int decc;
  2897. decc = _xmlSchemaBase64Decode(*cur);
  2898. if (decc < 0) ;
  2899. else if (decc < 64)
  2900. goto return1;
  2901. if (decc == 64)
  2902. pad++;
  2903. }
  2904. /* rfc2045.txt: "Special processing is performed if fewer than
  2905. * 24 bits are available at the end of the data being encoded.
  2906. * A full encoding quantum is always completed at the end of a
  2907. * body. When fewer than 24 input bits are available in an
  2908. * input group, zero bits are added (on the right) to form an
  2909. * integral number of 6-bit groups. Padding at the end of the
  2910. * data is performed using the "=" character. Since all
  2911. * base64 input is an integral number of octets, only the
  2912. * following cases can arise: (1) the final quantum of
  2913. * encoding input is an integral multiple of 24 bits; here,
  2914. * the final unit of encoded output will be an integral
  2915. * multiple of indent: Standard input:701: Warning:old style
  2916. * assignment ambiguity in "=*". Assuming "= *" 4 characters
  2917. * with no "=" padding, (2) the final
  2918. * quantum of encoding input is exactly 8 bits; here, the
  2919. * final unit of encoded output will be two characters
  2920. * followed by two "=" padding characters, or (3) the final
  2921. * quantum of encoding input is exactly 16 bits; here, the
  2922. * final unit of encoded output will be three characters
  2923. * followed by one "=" padding character." */
  2924. total = 3 * (i / 4);
  2925. if (pad == 0) {
  2926. if (i % 4 != 0)
  2927. goto return1;
  2928. } else if (pad == 1) {
  2929. int decc;
  2930. if (i % 4 != 3)
  2931. goto return1;
  2932. for (decc = _xmlSchemaBase64Decode(*cur);
  2933. (decc < 0) || (decc > 63);
  2934. decc = _xmlSchemaBase64Decode(*cur))
  2935. --cur;
  2936. /* 16bits in 24bits means 2 pad bits: nnnnnn nnmmmm mmmm00*/
  2937. /* 00111100 -> 0x3c */
  2938. if (decc & ~0x3c)
  2939. goto return1;
  2940. total += 2;
  2941. } else if (pad == 2) {
  2942. int decc;
  2943. if (i % 4 != 2)
  2944. goto return1;
  2945. for (decc = _xmlSchemaBase64Decode(*cur);
  2946. (decc < 0) || (decc > 63);
  2947. decc = _xmlSchemaBase64Decode(*cur))
  2948. --cur;
  2949. /* 8bits in 12bits means 4 pad bits: nnnnnn nn0000 */
  2950. /* 00110000 -> 0x30 */
  2951. if (decc & ~0x30)
  2952. goto return1;
  2953. total += 1;
  2954. } else
  2955. goto return1;
  2956. if (val != NULL) {
  2957. v = xmlSchemaNewValue(XML_SCHEMAS_BASE64BINARY);
  2958. if (v == NULL)
  2959. goto error;
  2960. base =
  2961. (xmlChar *) xmlMallocAtomic((i + pad + 1) *
  2962. sizeof(xmlChar));
  2963. if (base == NULL) {
  2964. xmlSchemaTypeErrMemory(node, "allocating base64 data");
  2965. xmlFree(v);
  2966. goto return1;
  2967. }
  2968. v->value.base64.str = base;
  2969. for (cur = value; *cur; ++cur)
  2970. if (_xmlSchemaBase64Decode(*cur) >= 0) {
  2971. *base = *cur;
  2972. ++base;
  2973. }
  2974. *base = 0;
  2975. v->value.base64.total = total;
  2976. *val = v;
  2977. }
  2978. goto return0;
  2979. }
  2980. case XML_SCHEMAS_INTEGER:
  2981. case XML_SCHEMAS_PINTEGER:
  2982. case XML_SCHEMAS_NPINTEGER:
  2983. case XML_SCHEMAS_NINTEGER:
  2984. case XML_SCHEMAS_NNINTEGER:{
  2985. const xmlChar *cur = value;
  2986. unsigned long lo, mi, hi;
  2987. int sign = 0;
  2988. if (cur == NULL)
  2989. goto return1;
  2990. if (normOnTheFly)
  2991. while IS_WSP_BLANK_CH(*cur) cur++;
  2992. if (*cur == '-') {
  2993. sign = 1;
  2994. cur++;
  2995. } else if (*cur == '+')
  2996. cur++;
  2997. ret = xmlSchemaParseUInt(&cur, &lo, &mi, &hi);
  2998. if (ret < 0)
  2999. goto return1;
  3000. if (normOnTheFly)
  3001. while IS_WSP_BLANK_CH(*cur) cur++;
  3002. if (*cur != 0)
  3003. goto return1;
  3004. if (type->builtInType == XML_SCHEMAS_NPINTEGER) {
  3005. if ((sign == 0) &&
  3006. ((hi != 0) || (mi != 0) || (lo != 0)))
  3007. goto return1;
  3008. } else if (type->builtInType == XML_SCHEMAS_PINTEGER) {
  3009. if (sign == 1)
  3010. goto return1;
  3011. if ((hi == 0) && (mi == 0) && (lo == 0))
  3012. goto return1;
  3013. } else if (type->builtInType == XML_SCHEMAS_NINTEGER) {
  3014. if (sign == 0)
  3015. goto return1;
  3016. if ((hi == 0) && (mi == 0) && (lo == 0))
  3017. goto return1;
  3018. } else if (type->builtInType == XML_SCHEMAS_NNINTEGER) {
  3019. if ((sign == 1) &&
  3020. ((hi != 0) || (mi != 0) || (lo != 0)))
  3021. goto return1;
  3022. }
  3023. if (val != NULL) {
  3024. v = xmlSchemaNewValue(type->builtInType);
  3025. if (v != NULL) {
  3026. if (ret == 0)
  3027. ret++;
  3028. v->value.decimal.lo = lo;
  3029. v->value.decimal.mi = mi;
  3030. v->value.decimal.hi = hi;
  3031. v->value.decimal.sign = sign;
  3032. v->value.decimal.frac = 0;
  3033. v->value.decimal.total = ret;
  3034. *val = v;
  3035. }
  3036. }
  3037. goto return0;
  3038. }
  3039. case XML_SCHEMAS_LONG:
  3040. case XML_SCHEMAS_BYTE:
  3041. case XML_SCHEMAS_SHORT:
  3042. case XML_SCHEMAS_INT:{
  3043. const xmlChar *cur = value;
  3044. unsigned long lo, mi, hi;
  3045. int sign = 0;
  3046. if (cur == NULL)
  3047. goto return1;
  3048. if (*cur == '-') {
  3049. sign = 1;
  3050. cur++;
  3051. } else if (*cur == '+')
  3052. cur++;
  3053. ret = xmlSchemaParseUInt(&cur, &lo, &mi, &hi);
  3054. if (ret < 0)
  3055. goto return1;
  3056. if (*cur != 0)
  3057. goto return1;
  3058. if (type->builtInType == XML_SCHEMAS_LONG) {
  3059. if (hi >= 922) {
  3060. if (hi > 922)
  3061. goto return1;
  3062. if (mi >= 33720368) {
  3063. if (mi > 33720368)
  3064. goto return1;
  3065. if ((sign == 0) && (lo > 54775807))
  3066. goto return1;
  3067. if ((sign == 1) && (lo > 54775808))
  3068. goto return1;
  3069. }
  3070. }
  3071. } else if (type->builtInType == XML_SCHEMAS_INT) {
  3072. if (hi != 0)
  3073. goto return1;
  3074. if (mi >= 21) {
  3075. if (mi > 21)
  3076. goto return1;
  3077. if ((sign == 0) && (lo > 47483647))
  3078. goto return1;
  3079. if ((sign == 1) && (lo > 47483648))
  3080. goto return1;
  3081. }
  3082. } else if (type->builtInType == XML_SCHEMAS_SHORT) {
  3083. if ((mi != 0) || (hi != 0))
  3084. goto return1;
  3085. if ((sign == 1) && (lo > 32768))
  3086. goto return1;
  3087. if ((sign == 0) && (lo > 32767))
  3088. goto return1;
  3089. } else if (type->builtInType == XML_SCHEMAS_BYTE) {
  3090. if ((mi != 0) || (hi != 0))
  3091. goto return1;
  3092. if ((sign == 1) && (lo > 128))
  3093. goto return1;
  3094. if ((sign == 0) && (lo > 127))
  3095. goto return1;
  3096. }
  3097. if (val != NULL) {
  3098. v = xmlSchemaNewValue(type->builtInType);
  3099. if (v != NULL) {
  3100. v->value.decimal.lo = lo;
  3101. v->value.decimal.mi = mi;
  3102. v->value.decimal.hi = hi;
  3103. v->value.decimal.sign = sign;
  3104. v->value.decimal.frac = 0;
  3105. v->value.decimal.total = ret;
  3106. *val = v;
  3107. }
  3108. }
  3109. goto return0;
  3110. }
  3111. case XML_SCHEMAS_UINT:
  3112. case XML_SCHEMAS_ULONG:
  3113. case XML_SCHEMAS_USHORT:
  3114. case XML_SCHEMAS_UBYTE:{
  3115. const xmlChar *cur = value;
  3116. unsigned long lo, mi, hi;
  3117. if (cur == NULL)
  3118. goto return1;
  3119. ret = xmlSchemaParseUInt(&cur, &lo, &mi, &hi);
  3120. if (ret < 0)
  3121. goto return1;
  3122. if (*cur != 0)
  3123. goto return1;
  3124. if (type->builtInType == XML_SCHEMAS_ULONG) {
  3125. if (hi >= 1844) {
  3126. if (hi > 1844)
  3127. goto return1;
  3128. if (mi >= 67440737) {
  3129. if (mi > 67440737)
  3130. goto return1;
  3131. if (lo > 9551615)
  3132. goto return1;
  3133. }
  3134. }
  3135. } else if (type->builtInType == XML_SCHEMAS_UINT) {
  3136. if (hi != 0)
  3137. goto return1;
  3138. if (mi >= 42) {
  3139. if (mi > 42)
  3140. goto return1;
  3141. if (lo > 94967295)
  3142. goto return1;
  3143. }
  3144. } else if (type->builtInType == XML_SCHEMAS_USHORT) {
  3145. if ((mi != 0) || (hi != 0))
  3146. goto return1;
  3147. if (lo > 65535)
  3148. goto return1;
  3149. } else if (type->builtInType == XML_SCHEMAS_UBYTE) {
  3150. if ((mi != 0) || (hi != 0))
  3151. goto return1;
  3152. if (lo > 255)
  3153. goto return1;
  3154. }
  3155. if (val != NULL) {
  3156. v = xmlSchemaNewValue(type->builtInType);
  3157. if (v != NULL) {
  3158. v->value.decimal.lo = lo;
  3159. v->value.decimal.mi = mi;
  3160. v->value.decimal.hi = hi;
  3161. v->value.decimal.sign = 0;
  3162. v->value.decimal.frac = 0;
  3163. v->value.decimal.total = ret;
  3164. *val = v;
  3165. }
  3166. }
  3167. goto return0;
  3168. }
  3169. }
  3170. done:
  3171. if (norm != NULL)
  3172. xmlFree(norm);
  3173. return (ret);
  3174. return3:
  3175. if (norm != NULL)
  3176. xmlFree(norm);
  3177. return (3);
  3178. return1:
  3179. if (norm != NULL)
  3180. xmlFree(norm);
  3181. return (1);
  3182. return0:
  3183. if (norm != NULL)
  3184. xmlFree(norm);
  3185. return (0);
  3186. error:
  3187. if (norm != NULL)
  3188. xmlFree(norm);
  3189. return (-1);
  3190. }
  3191. /**
  3192. * xmlSchemaValPredefTypeNode:
  3193. * @type: the predefined type
  3194. * @value: the value to check
  3195. * @val: the return computed value
  3196. * @node: the node containing the value
  3197. *
  3198. * Check that a value conforms to the lexical space of the predefined type.
  3199. * if true a value is computed and returned in @val.
  3200. *
  3201. * Returns 0 if this validates, a positive error code number otherwise
  3202. * and -1 in case of internal or API error.
  3203. */
  3204. int
  3205. xmlSchemaValPredefTypeNode(xmlSchemaTypePtr type, const xmlChar *value,
  3206. xmlSchemaValPtr *val, xmlNodePtr node) {
  3207. return(xmlSchemaValAtomicType(type, value, val, node, 0,
  3208. XML_SCHEMA_WHITESPACE_UNKNOWN, 1, 1, 0));
  3209. }
  3210. /**
  3211. * xmlSchemaValPredefTypeNodeNoNorm:
  3212. * @type: the predefined type
  3213. * @value: the value to check
  3214. * @val: the return computed value
  3215. * @node: the node containing the value
  3216. *
  3217. * Check that a value conforms to the lexical space of the predefined type.
  3218. * if true a value is computed and returned in @val.
  3219. * This one does apply any normalization to the value.
  3220. *
  3221. * Returns 0 if this validates, a positive error code number otherwise
  3222. * and -1 in case of internal or API error.
  3223. */
  3224. int
  3225. xmlSchemaValPredefTypeNodeNoNorm(xmlSchemaTypePtr type, const xmlChar *value,
  3226. xmlSchemaValPtr *val, xmlNodePtr node) {
  3227. return(xmlSchemaValAtomicType(type, value, val, node, 1,
  3228. XML_SCHEMA_WHITESPACE_UNKNOWN, 1, 0, 1));
  3229. }
  3230. /**
  3231. * xmlSchemaValidatePredefinedType:
  3232. * @type: the predefined type
  3233. * @value: the value to check
  3234. * @val: the return computed value
  3235. *
  3236. * Check that a value conforms to the lexical space of the predefined type.
  3237. * if true a value is computed and returned in @val.
  3238. *
  3239. * Returns 0 if this validates, a positive error code number otherwise
  3240. * and -1 in case of internal or API error.
  3241. */
  3242. int
  3243. xmlSchemaValidatePredefinedType(xmlSchemaTypePtr type, const xmlChar *value,
  3244. xmlSchemaValPtr *val) {
  3245. return(xmlSchemaValPredefTypeNode(type, value, val, NULL));
  3246. }
  3247. /**
  3248. * xmlSchemaCompareDecimals:
  3249. * @x: a first decimal value
  3250. * @y: a second decimal value
  3251. *
  3252. * Compare 2 decimals
  3253. *
  3254. * Returns -1 if x < y, 0 if x == y, 1 if x > y and -2 in case of error
  3255. */
  3256. static int
  3257. xmlSchemaCompareDecimals(xmlSchemaValPtr x, xmlSchemaValPtr y)
  3258. {
  3259. xmlSchemaValPtr swp;
  3260. int order = 1, integx, integy, dlen;
  3261. unsigned long hi, mi, lo;
  3262. /*
  3263. * First test: If x is -ve and not zero
  3264. */
  3265. if ((x->value.decimal.sign) &&
  3266. ((x->value.decimal.lo != 0) ||
  3267. (x->value.decimal.mi != 0) ||
  3268. (x->value.decimal.hi != 0))) {
  3269. /*
  3270. * Then if y is -ve and not zero reverse the compare
  3271. */
  3272. if ((y->value.decimal.sign) &&
  3273. ((y->value.decimal.lo != 0) ||
  3274. (y->value.decimal.mi != 0) ||
  3275. (y->value.decimal.hi != 0)))
  3276. order = -1;
  3277. /*
  3278. * Otherwise (y >= 0) we have the answer
  3279. */
  3280. else
  3281. return (-1);
  3282. /*
  3283. * If x is not -ve and y is -ve we have the answer
  3284. */
  3285. } else if ((y->value.decimal.sign) &&
  3286. ((y->value.decimal.lo != 0) ||
  3287. (y->value.decimal.mi != 0) ||
  3288. (y->value.decimal.hi != 0))) {
  3289. return (1);
  3290. }
  3291. /*
  3292. * If it's not simply determined by a difference in sign,
  3293. * then we need to compare the actual values of the two nums.
  3294. * To do this, we start by looking at the integral parts.
  3295. * If the number of integral digits differ, then we have our
  3296. * answer.
  3297. */
  3298. integx = x->value.decimal.total - x->value.decimal.frac;
  3299. integy = y->value.decimal.total - y->value.decimal.frac;
  3300. /*
  3301. * NOTE: We changed the "total" for values like "0.1"
  3302. * (or "-0.1" or ".1") to be 1, which was 2 previously.
  3303. * Therefore the special case, when such values are
  3304. * compared with 0, needs to be handled separately;
  3305. * otherwise a zero would be recognized incorrectly as
  3306. * greater than those values. This has the nice side effect
  3307. * that we gain an overall optimized comparison with zeroes.
  3308. * Note that a "0" has a "total" of 1 already.
  3309. */
  3310. if (integx == 1) {
  3311. if (x->value.decimal.lo == 0) {
  3312. if (integy != 1)
  3313. return -order;
  3314. else if (y->value.decimal.lo != 0)
  3315. return -order;
  3316. else
  3317. return(0);
  3318. }
  3319. }
  3320. if (integy == 1) {
  3321. if (y->value.decimal.lo == 0) {
  3322. if (integx != 1)
  3323. return order;
  3324. else if (x->value.decimal.lo != 0)
  3325. return order;
  3326. else
  3327. return(0);
  3328. }
  3329. }
  3330. if (integx > integy)
  3331. return order;
  3332. else if (integy > integx)
  3333. return -order;
  3334. /*
  3335. * If the number of integral digits is the same for both numbers,
  3336. * then things get a little more complicated. We need to "normalize"
  3337. * the numbers in order to properly compare them. To do this, we
  3338. * look at the total length of each number (length => number of
  3339. * significant digits), and divide the "shorter" by 10 (decreasing
  3340. * the length) until they are of equal length.
  3341. */
  3342. dlen = x->value.decimal.total - y->value.decimal.total;
  3343. if (dlen < 0) { /* y has more digits than x */
  3344. swp = x;
  3345. hi = y->value.decimal.hi;
  3346. mi = y->value.decimal.mi;
  3347. lo = y->value.decimal.lo;
  3348. dlen = -dlen;
  3349. order = -order;
  3350. } else { /* x has more digits than y */
  3351. swp = y;
  3352. hi = x->value.decimal.hi;
  3353. mi = x->value.decimal.mi;
  3354. lo = x->value.decimal.lo;
  3355. }
  3356. while (dlen > 8) { /* in effect, right shift by 10**8 */
  3357. lo = mi;
  3358. mi = hi;
  3359. hi = 0;
  3360. dlen -= 8;
  3361. }
  3362. while (dlen > 0) {
  3363. unsigned long rem1, rem2;
  3364. rem1 = (hi % 10) * 100000000L;
  3365. hi = hi / 10;
  3366. rem2 = (mi % 10) * 100000000L;
  3367. mi = (mi + rem1) / 10;
  3368. lo = (lo + rem2) / 10;
  3369. dlen--;
  3370. }
  3371. if (hi > swp->value.decimal.hi) {
  3372. return order;
  3373. } else if (hi == swp->value.decimal.hi) {
  3374. if (mi > swp->value.decimal.mi) {
  3375. return order;
  3376. } else if (mi == swp->value.decimal.mi) {
  3377. if (lo > swp->value.decimal.lo) {
  3378. return order;
  3379. } else if (lo == swp->value.decimal.lo) {
  3380. if (x->value.decimal.total == y->value.decimal.total) {
  3381. return 0;
  3382. } else {
  3383. return order;
  3384. }
  3385. }
  3386. }
  3387. }
  3388. return -order;
  3389. }
  3390. /**
  3391. * xmlSchemaCompareDurations:
  3392. * @x: a first duration value
  3393. * @y: a second duration value
  3394. *
  3395. * Compare 2 durations
  3396. *
  3397. * Returns -1 if x < y, 0 if x == y, 1 if x > y, 2 if x <> y, and -2 in
  3398. * case of error
  3399. */
  3400. static int
  3401. xmlSchemaCompareDurations(xmlSchemaValPtr x, xmlSchemaValPtr y)
  3402. {
  3403. long carry, mon, day;
  3404. double sec;
  3405. int invert = 1;
  3406. long xmon, xday, myear, minday, maxday;
  3407. static const long dayRange [2][12] = {
  3408. { 0, 28, 59, 89, 120, 150, 181, 212, 242, 273, 303, 334, },
  3409. { 0, 31, 62, 92, 123, 153, 184, 215, 245, 276, 306, 337} };
  3410. if ((x == NULL) || (y == NULL))
  3411. return -2;
  3412. /* months */
  3413. mon = x->value.dur.mon - y->value.dur.mon;
  3414. /* seconds */
  3415. sec = x->value.dur.sec - y->value.dur.sec;
  3416. carry = (long)(sec / SECS_PER_DAY);
  3417. sec -= ((double)carry) * SECS_PER_DAY;
  3418. /* days */
  3419. day = x->value.dur.day - y->value.dur.day + carry;
  3420. /* easy test */
  3421. if (mon == 0) {
  3422. if (day == 0)
  3423. if (sec == 0.0)
  3424. return 0;
  3425. else if (sec < 0.0)
  3426. return -1;
  3427. else
  3428. return 1;
  3429. else if (day < 0)
  3430. return -1;
  3431. else
  3432. return 1;
  3433. }
  3434. if (mon > 0) {
  3435. if ((day >= 0) && (sec >= 0.0))
  3436. return 1;
  3437. else {
  3438. xmon = mon;
  3439. xday = -day;
  3440. }
  3441. } else if ((day <= 0) && (sec <= 0.0)) {
  3442. return -1;
  3443. } else {
  3444. invert = -1;
  3445. xmon = -mon;
  3446. xday = day;
  3447. }
  3448. myear = xmon / 12;
  3449. if (myear == 0) {
  3450. minday = 0;
  3451. maxday = 0;
  3452. } else {
  3453. if (myear > LONG_MAX / 366)
  3454. return -2;
  3455. /* FIXME: This doesn't take leap year exceptions every 100/400 years
  3456. into account. */
  3457. maxday = 365 * myear + (myear + 3) / 4;
  3458. /* FIXME: Needs to be calculated separately */
  3459. minday = maxday - 1;
  3460. }
  3461. xmon = xmon % 12;
  3462. minday += dayRange[0][xmon];
  3463. maxday += dayRange[1][xmon];
  3464. if ((maxday == minday) && (maxday == xday))
  3465. return(0); /* can this really happen ? */
  3466. if (maxday < xday)
  3467. return(-invert);
  3468. if (minday > xday)
  3469. return(invert);
  3470. /* indeterminate */
  3471. return 2;
  3472. }
  3473. /*
  3474. * macros for adding date/times and durations
  3475. */
  3476. #define FQUOTIENT(a,b) (floor(((double)a/(double)b)))
  3477. #define MODULO(a,b) (a - FQUOTIENT(a,b) * b)
  3478. #define FQUOTIENT_RANGE(a,low,high) (FQUOTIENT((a-low),(high-low)))
  3479. #define MODULO_RANGE(a,low,high) ((MODULO((a-low),(high-low)))+low)
  3480. /**
  3481. * xmlSchemaDupVal:
  3482. * @v: the #xmlSchemaValPtr value to duplicate
  3483. *
  3484. * Makes a copy of @v. The calling program is responsible for freeing
  3485. * the returned value.
  3486. *
  3487. * returns a pointer to a duplicated #xmlSchemaValPtr or NULL if error.
  3488. */
  3489. static xmlSchemaValPtr
  3490. xmlSchemaDupVal (xmlSchemaValPtr v)
  3491. {
  3492. xmlSchemaValPtr ret = xmlSchemaNewValue(v->type);
  3493. if (ret == NULL)
  3494. return NULL;
  3495. memcpy(ret, v, sizeof(xmlSchemaVal));
  3496. ret->next = NULL;
  3497. return ret;
  3498. }
  3499. /**
  3500. * xmlSchemaCopyValue:
  3501. * @val: the precomputed value to be copied
  3502. *
  3503. * Copies the precomputed value. This duplicates any string within.
  3504. *
  3505. * Returns the copy or NULL if a copy for a data-type is not implemented.
  3506. */
  3507. xmlSchemaValPtr
  3508. xmlSchemaCopyValue(xmlSchemaValPtr val)
  3509. {
  3510. xmlSchemaValPtr ret = NULL, prev = NULL, cur;
  3511. /*
  3512. * Copy the string values.
  3513. */
  3514. while (val != NULL) {
  3515. switch (val->type) {
  3516. case XML_SCHEMAS_ANYTYPE:
  3517. case XML_SCHEMAS_IDREFS:
  3518. case XML_SCHEMAS_ENTITIES:
  3519. case XML_SCHEMAS_NMTOKENS:
  3520. xmlSchemaFreeValue(ret);
  3521. return (NULL);
  3522. case XML_SCHEMAS_ANYSIMPLETYPE:
  3523. case XML_SCHEMAS_STRING:
  3524. case XML_SCHEMAS_NORMSTRING:
  3525. case XML_SCHEMAS_TOKEN:
  3526. case XML_SCHEMAS_LANGUAGE:
  3527. case XML_SCHEMAS_NAME:
  3528. case XML_SCHEMAS_NCNAME:
  3529. case XML_SCHEMAS_ID:
  3530. case XML_SCHEMAS_IDREF:
  3531. case XML_SCHEMAS_ENTITY:
  3532. case XML_SCHEMAS_NMTOKEN:
  3533. case XML_SCHEMAS_ANYURI:
  3534. cur = xmlSchemaDupVal(val);
  3535. if (val->value.str != NULL)
  3536. cur->value.str = xmlStrdup(BAD_CAST val->value.str);
  3537. break;
  3538. case XML_SCHEMAS_QNAME:
  3539. case XML_SCHEMAS_NOTATION:
  3540. cur = xmlSchemaDupVal(val);
  3541. if (val->value.qname.name != NULL)
  3542. cur->value.qname.name =
  3543. xmlStrdup(BAD_CAST val->value.qname.name);
  3544. if (val->value.qname.uri != NULL)
  3545. cur->value.qname.uri =
  3546. xmlStrdup(BAD_CAST val->value.qname.uri);
  3547. break;
  3548. case XML_SCHEMAS_HEXBINARY:
  3549. cur = xmlSchemaDupVal(val);
  3550. if (val->value.hex.str != NULL)
  3551. cur->value.hex.str = xmlStrdup(BAD_CAST val->value.hex.str);
  3552. break;
  3553. case XML_SCHEMAS_BASE64BINARY:
  3554. cur = xmlSchemaDupVal(val);
  3555. if (val->value.base64.str != NULL)
  3556. cur->value.base64.str =
  3557. xmlStrdup(BAD_CAST val->value.base64.str);
  3558. break;
  3559. default:
  3560. cur = xmlSchemaDupVal(val);
  3561. break;
  3562. }
  3563. if (ret == NULL)
  3564. ret = cur;
  3565. else
  3566. prev->next = cur;
  3567. prev = cur;
  3568. val = val->next;
  3569. }
  3570. return (ret);
  3571. }
  3572. /**
  3573. * _xmlSchemaDateAdd:
  3574. * @dt: an #xmlSchemaValPtr
  3575. * @dur: an #xmlSchemaValPtr of type #XS_DURATION
  3576. *
  3577. * Compute a new date/time from @dt and @dur. This function assumes @dt
  3578. * is either #XML_SCHEMAS_DATETIME, #XML_SCHEMAS_DATE, #XML_SCHEMAS_GYEARMONTH,
  3579. * or #XML_SCHEMAS_GYEAR. The returned #xmlSchemaVal is the same type as
  3580. * @dt. The calling program is responsible for freeing the returned value.
  3581. *
  3582. * Returns a pointer to a new #xmlSchemaVal or NULL if error.
  3583. */
  3584. static xmlSchemaValPtr
  3585. _xmlSchemaDateAdd (xmlSchemaValPtr dt, xmlSchemaValPtr dur)
  3586. {
  3587. xmlSchemaValPtr ret, tmp;
  3588. long carry, tempdays, temp;
  3589. xmlSchemaValDatePtr r, d;
  3590. xmlSchemaValDurationPtr u;
  3591. if ((dt == NULL) || (dur == NULL))
  3592. return NULL;
  3593. ret = xmlSchemaNewValue(dt->type);
  3594. if (ret == NULL)
  3595. return NULL;
  3596. /* make a copy so we don't alter the original value */
  3597. tmp = xmlSchemaDupVal(dt);
  3598. if (tmp == NULL) {
  3599. xmlSchemaFreeValue(ret);
  3600. return NULL;
  3601. }
  3602. r = &(ret->value.date);
  3603. d = &(tmp->value.date);
  3604. u = &(dur->value.dur);
  3605. /* normalization */
  3606. if (d->mon == 0)
  3607. d->mon = 1;
  3608. /* normalize for time zone offset */
  3609. u->sec -= (d->tzo * 60);
  3610. d->tzo = 0;
  3611. /* normalization */
  3612. if (d->day == 0)
  3613. d->day = 1;
  3614. /* month */
  3615. carry = d->mon + u->mon;
  3616. r->mon = (unsigned int) MODULO_RANGE(carry, 1, 13);
  3617. carry = (long) FQUOTIENT_RANGE(carry, 1, 13);
  3618. /* year (may be modified later) */
  3619. r->year = d->year + carry;
  3620. if (r->year == 0) {
  3621. if (d->year > 0)
  3622. r->year--;
  3623. else
  3624. r->year++;
  3625. }
  3626. /* time zone */
  3627. r->tzo = d->tzo;
  3628. r->tz_flag = d->tz_flag;
  3629. /* seconds */
  3630. r->sec = d->sec + u->sec;
  3631. carry = (long) FQUOTIENT((long)r->sec, 60);
  3632. if (r->sec != 0.0) {
  3633. r->sec = MODULO(r->sec, 60.0);
  3634. }
  3635. /* minute */
  3636. carry += d->min;
  3637. r->min = (unsigned int) MODULO(carry, 60);
  3638. carry = (long) FQUOTIENT(carry, 60);
  3639. /* hours */
  3640. carry += d->hour;
  3641. r->hour = (unsigned int) MODULO(carry, 24);
  3642. carry = (long)FQUOTIENT(carry, 24);
  3643. /*
  3644. * days
  3645. * Note we use tempdays because the temporary values may need more
  3646. * than 5 bits
  3647. */
  3648. if ((VALID_YEAR(r->year)) && (VALID_MONTH(r->mon)) &&
  3649. (d->day > MAX_DAYINMONTH(r->year, r->mon)))
  3650. tempdays = MAX_DAYINMONTH(r->year, r->mon);
  3651. else if (d->day < 1)
  3652. tempdays = 1;
  3653. else
  3654. tempdays = d->day;
  3655. tempdays += u->day + carry;
  3656. while (1) {
  3657. if (tempdays < 1) {
  3658. long tmon = (long) MODULO_RANGE((int)r->mon-1, 1, 13);
  3659. long tyr = r->year + (long)FQUOTIENT_RANGE((int)r->mon-1, 1, 13);
  3660. if (tyr == 0)
  3661. tyr--;
  3662. /*
  3663. * Coverity detected an overrun in daysInMonth
  3664. * of size 12 at position 12 with index variable "((r)->mon - 1)"
  3665. */
  3666. if (tmon < 1)
  3667. tmon = 1;
  3668. if (tmon > 12)
  3669. tmon = 12;
  3670. tempdays += MAX_DAYINMONTH(tyr, tmon);
  3671. carry = -1;
  3672. } else if (VALID_YEAR(r->year) && VALID_MONTH(r->mon) &&
  3673. tempdays > (long) MAX_DAYINMONTH(r->year, r->mon)) {
  3674. tempdays = tempdays - MAX_DAYINMONTH(r->year, r->mon);
  3675. carry = 1;
  3676. } else
  3677. break;
  3678. temp = r->mon + carry;
  3679. r->mon = (unsigned int) MODULO_RANGE(temp, 1, 13);
  3680. r->year = r->year + (long) FQUOTIENT_RANGE(temp, 1, 13);
  3681. if (r->year == 0) {
  3682. if (temp < 1)
  3683. r->year--;
  3684. else
  3685. r->year++;
  3686. }
  3687. }
  3688. r->day = tempdays;
  3689. /*
  3690. * adjust the date/time type to the date values
  3691. */
  3692. if (ret->type != XML_SCHEMAS_DATETIME) {
  3693. if ((r->hour) || (r->min) || (r->sec))
  3694. ret->type = XML_SCHEMAS_DATETIME;
  3695. else if (ret->type != XML_SCHEMAS_DATE) {
  3696. if ((r->mon != 1) && (r->day != 1))
  3697. ret->type = XML_SCHEMAS_DATE;
  3698. else if ((ret->type != XML_SCHEMAS_GYEARMONTH) && (r->mon != 1))
  3699. ret->type = XML_SCHEMAS_GYEARMONTH;
  3700. }
  3701. }
  3702. xmlSchemaFreeValue(tmp);
  3703. return ret;
  3704. }
  3705. /**
  3706. * xmlSchemaDateNormalize:
  3707. * @dt: an #xmlSchemaValPtr of a date/time type value.
  3708. * @offset: number of seconds to adjust @dt by.
  3709. *
  3710. * Normalize @dt to GMT time. The @offset parameter is subtracted from
  3711. * the return value is a time-zone offset is present on @dt.
  3712. *
  3713. * Returns a normalized copy of @dt or NULL if error.
  3714. */
  3715. static xmlSchemaValPtr
  3716. xmlSchemaDateNormalize (xmlSchemaValPtr dt, double offset)
  3717. {
  3718. xmlSchemaValPtr dur, ret;
  3719. if (dt == NULL)
  3720. return NULL;
  3721. if (((dt->type != XML_SCHEMAS_TIME) &&
  3722. (dt->type != XML_SCHEMAS_DATETIME) &&
  3723. (dt->type != XML_SCHEMAS_DATE)) || (dt->value.date.tzo == 0))
  3724. return xmlSchemaDupVal(dt);
  3725. dur = xmlSchemaNewValue(XML_SCHEMAS_DURATION);
  3726. if (dur == NULL)
  3727. return NULL;
  3728. dur->value.date.sec -= offset;
  3729. ret = _xmlSchemaDateAdd(dt, dur);
  3730. if (ret == NULL)
  3731. return NULL;
  3732. xmlSchemaFreeValue(dur);
  3733. /* ret->value.date.tzo = 0; */
  3734. return ret;
  3735. }
  3736. /**
  3737. * _xmlSchemaDateCastYMToDays:
  3738. * @dt: an #xmlSchemaValPtr
  3739. *
  3740. * Convert mon and year of @dt to total number of days. Take the
  3741. * number of years since (or before) 1 AD and add the number of leap
  3742. * years. This is a function because negative
  3743. * years must be handled a little differently and there is no zero year.
  3744. *
  3745. * Returns number of days.
  3746. */
  3747. static long
  3748. _xmlSchemaDateCastYMToDays (const xmlSchemaValPtr dt)
  3749. {
  3750. long ret;
  3751. int mon;
  3752. mon = dt->value.date.mon;
  3753. if (mon <= 0) mon = 1; /* normalization */
  3754. if (dt->value.date.year <= 0)
  3755. ret = (dt->value.date.year * 365) +
  3756. (((dt->value.date.year+1)/4)-((dt->value.date.year+1)/100)+
  3757. ((dt->value.date.year+1)/400)) +
  3758. DAY_IN_YEAR(0, mon, dt->value.date.year);
  3759. else
  3760. ret = ((dt->value.date.year-1) * 365) +
  3761. (((dt->value.date.year-1)/4)-((dt->value.date.year-1)/100)+
  3762. ((dt->value.date.year-1)/400)) +
  3763. DAY_IN_YEAR(0, mon, dt->value.date.year);
  3764. return ret;
  3765. }
  3766. /**
  3767. * TIME_TO_NUMBER:
  3768. * @dt: an #xmlSchemaValPtr
  3769. *
  3770. * Calculates the number of seconds in the time portion of @dt.
  3771. *
  3772. * Returns seconds.
  3773. */
  3774. #define TIME_TO_NUMBER(dt) \
  3775. ((double)((dt->value.date.hour * SECS_PER_HOUR) + \
  3776. (dt->value.date.min * SECS_PER_MIN) + \
  3777. (dt->value.date.tzo * SECS_PER_MIN)) + \
  3778. dt->value.date.sec)
  3779. /**
  3780. * xmlSchemaCompareDates:
  3781. * @x: a first date/time value
  3782. * @y: a second date/time value
  3783. *
  3784. * Compare 2 date/times
  3785. *
  3786. * Returns -1 if x < y, 0 if x == y, 1 if x > y, 2 if x <> y, and -2 in
  3787. * case of error
  3788. */
  3789. static int
  3790. xmlSchemaCompareDates (xmlSchemaValPtr x, xmlSchemaValPtr y)
  3791. {
  3792. unsigned char xmask, ymask, xor_mask, and_mask;
  3793. xmlSchemaValPtr p1, p2, q1, q2;
  3794. long p1d, p2d, q1d, q2d;
  3795. if ((x == NULL) || (y == NULL))
  3796. return -2;
  3797. if ((x->value.date.year > LONG_MAX / 366) ||
  3798. (x->value.date.year < LONG_MIN / 366) ||
  3799. (y->value.date.year > LONG_MAX / 366) ||
  3800. (y->value.date.year < LONG_MIN / 366)) {
  3801. /* Possible overflow when converting to days. */
  3802. return -2;
  3803. }
  3804. if (x->value.date.tz_flag) {
  3805. if (!y->value.date.tz_flag) {
  3806. p1 = xmlSchemaDateNormalize(x, 0);
  3807. p1d = _xmlSchemaDateCastYMToDays(p1) + p1->value.date.day;
  3808. /* normalize y + 14:00 */
  3809. q1 = xmlSchemaDateNormalize(y, (14 * SECS_PER_HOUR));
  3810. q1d = _xmlSchemaDateCastYMToDays(q1) + q1->value.date.day;
  3811. if (p1d < q1d) {
  3812. xmlSchemaFreeValue(p1);
  3813. xmlSchemaFreeValue(q1);
  3814. return -1;
  3815. } else if (p1d == q1d) {
  3816. double sec;
  3817. sec = TIME_TO_NUMBER(p1) - TIME_TO_NUMBER(q1);
  3818. if (sec < 0.0) {
  3819. xmlSchemaFreeValue(p1);
  3820. xmlSchemaFreeValue(q1);
  3821. return -1;
  3822. } else {
  3823. int ret = 0;
  3824. /* normalize y - 14:00 */
  3825. q2 = xmlSchemaDateNormalize(y, -(14 * SECS_PER_HOUR));
  3826. q2d = _xmlSchemaDateCastYMToDays(q2) + q2->value.date.day;
  3827. if (p1d > q2d)
  3828. ret = 1;
  3829. else if (p1d == q2d) {
  3830. sec = TIME_TO_NUMBER(p1) - TIME_TO_NUMBER(q2);
  3831. if (sec > 0.0)
  3832. ret = 1;
  3833. else
  3834. ret = 2; /* indeterminate */
  3835. }
  3836. xmlSchemaFreeValue(p1);
  3837. xmlSchemaFreeValue(q1);
  3838. xmlSchemaFreeValue(q2);
  3839. if (ret != 0)
  3840. return(ret);
  3841. }
  3842. } else {
  3843. xmlSchemaFreeValue(p1);
  3844. xmlSchemaFreeValue(q1);
  3845. }
  3846. }
  3847. } else if (y->value.date.tz_flag) {
  3848. q1 = xmlSchemaDateNormalize(y, 0);
  3849. q1d = _xmlSchemaDateCastYMToDays(q1) + q1->value.date.day;
  3850. /* normalize x - 14:00 */
  3851. p1 = xmlSchemaDateNormalize(x, -(14 * SECS_PER_HOUR));
  3852. p1d = _xmlSchemaDateCastYMToDays(p1) + p1->value.date.day;
  3853. if (p1d < q1d) {
  3854. xmlSchemaFreeValue(p1);
  3855. xmlSchemaFreeValue(q1);
  3856. return -1;
  3857. } else if (p1d == q1d) {
  3858. double sec;
  3859. sec = TIME_TO_NUMBER(p1) - TIME_TO_NUMBER(q1);
  3860. if (sec < 0.0) {
  3861. xmlSchemaFreeValue(p1);
  3862. xmlSchemaFreeValue(q1);
  3863. return -1;
  3864. } else {
  3865. int ret = 0;
  3866. /* normalize x + 14:00 */
  3867. p2 = xmlSchemaDateNormalize(x, (14 * SECS_PER_HOUR));
  3868. p2d = _xmlSchemaDateCastYMToDays(p2) + p2->value.date.day;
  3869. if (p2d > q1d) {
  3870. ret = 1;
  3871. } else if (p2d == q1d) {
  3872. sec = TIME_TO_NUMBER(p2) - TIME_TO_NUMBER(q1);
  3873. if (sec > 0.0)
  3874. ret = 1;
  3875. else
  3876. ret = 2; /* indeterminate */
  3877. }
  3878. xmlSchemaFreeValue(p1);
  3879. xmlSchemaFreeValue(q1);
  3880. xmlSchemaFreeValue(p2);
  3881. if (ret != 0)
  3882. return(ret);
  3883. }
  3884. } else {
  3885. xmlSchemaFreeValue(p1);
  3886. xmlSchemaFreeValue(q1);
  3887. }
  3888. }
  3889. /*
  3890. * if the same type then calculate the difference
  3891. */
  3892. if (x->type == y->type) {
  3893. int ret = 0;
  3894. q1 = xmlSchemaDateNormalize(y, 0);
  3895. q1d = _xmlSchemaDateCastYMToDays(q1) + q1->value.date.day;
  3896. p1 = xmlSchemaDateNormalize(x, 0);
  3897. p1d = _xmlSchemaDateCastYMToDays(p1) + p1->value.date.day;
  3898. if (p1d < q1d) {
  3899. ret = -1;
  3900. } else if (p1d > q1d) {
  3901. ret = 1;
  3902. } else {
  3903. double sec;
  3904. sec = TIME_TO_NUMBER(p1) - TIME_TO_NUMBER(q1);
  3905. if (sec < 0.0)
  3906. ret = -1;
  3907. else if (sec > 0.0)
  3908. ret = 1;
  3909. }
  3910. xmlSchemaFreeValue(p1);
  3911. xmlSchemaFreeValue(q1);
  3912. return(ret);
  3913. }
  3914. switch (x->type) {
  3915. case XML_SCHEMAS_DATETIME:
  3916. xmask = 0xf;
  3917. break;
  3918. case XML_SCHEMAS_DATE:
  3919. xmask = 0x7;
  3920. break;
  3921. case XML_SCHEMAS_GYEAR:
  3922. xmask = 0x1;
  3923. break;
  3924. case XML_SCHEMAS_GMONTH:
  3925. xmask = 0x2;
  3926. break;
  3927. case XML_SCHEMAS_GDAY:
  3928. xmask = 0x3;
  3929. break;
  3930. case XML_SCHEMAS_GYEARMONTH:
  3931. xmask = 0x3;
  3932. break;
  3933. case XML_SCHEMAS_GMONTHDAY:
  3934. xmask = 0x6;
  3935. break;
  3936. case XML_SCHEMAS_TIME:
  3937. xmask = 0x8;
  3938. break;
  3939. default:
  3940. xmask = 0;
  3941. break;
  3942. }
  3943. switch (y->type) {
  3944. case XML_SCHEMAS_DATETIME:
  3945. ymask = 0xf;
  3946. break;
  3947. case XML_SCHEMAS_DATE:
  3948. ymask = 0x7;
  3949. break;
  3950. case XML_SCHEMAS_GYEAR:
  3951. ymask = 0x1;
  3952. break;
  3953. case XML_SCHEMAS_GMONTH:
  3954. ymask = 0x2;
  3955. break;
  3956. case XML_SCHEMAS_GDAY:
  3957. ymask = 0x3;
  3958. break;
  3959. case XML_SCHEMAS_GYEARMONTH:
  3960. ymask = 0x3;
  3961. break;
  3962. case XML_SCHEMAS_GMONTHDAY:
  3963. ymask = 0x6;
  3964. break;
  3965. case XML_SCHEMAS_TIME:
  3966. ymask = 0x8;
  3967. break;
  3968. default:
  3969. ymask = 0;
  3970. break;
  3971. }
  3972. xor_mask = xmask ^ ymask; /* mark type differences */
  3973. and_mask = xmask & ymask; /* mark field specification */
  3974. /* year */
  3975. if (xor_mask & 1)
  3976. return 2; /* indeterminate */
  3977. else if (and_mask & 1) {
  3978. if (x->value.date.year < y->value.date.year)
  3979. return -1;
  3980. else if (x->value.date.year > y->value.date.year)
  3981. return 1;
  3982. }
  3983. /* month */
  3984. if (xor_mask & 2)
  3985. return 2; /* indeterminate */
  3986. else if (and_mask & 2) {
  3987. if (x->value.date.mon < y->value.date.mon)
  3988. return -1;
  3989. else if (x->value.date.mon > y->value.date.mon)
  3990. return 1;
  3991. }
  3992. /* day */
  3993. if (xor_mask & 4)
  3994. return 2; /* indeterminate */
  3995. else if (and_mask & 4) {
  3996. if (x->value.date.day < y->value.date.day)
  3997. return -1;
  3998. else if (x->value.date.day > y->value.date.day)
  3999. return 1;
  4000. }
  4001. /* time */
  4002. if (xor_mask & 8)
  4003. return 2; /* indeterminate */
  4004. else if (and_mask & 8) {
  4005. if (x->value.date.hour < y->value.date.hour)
  4006. return -1;
  4007. else if (x->value.date.hour > y->value.date.hour)
  4008. return 1;
  4009. else if (x->value.date.min < y->value.date.min)
  4010. return -1;
  4011. else if (x->value.date.min > y->value.date.min)
  4012. return 1;
  4013. else if (x->value.date.sec < y->value.date.sec)
  4014. return -1;
  4015. else if (x->value.date.sec > y->value.date.sec)
  4016. return 1;
  4017. }
  4018. return 0;
  4019. }
  4020. /**
  4021. * xmlSchemaComparePreserveReplaceStrings:
  4022. * @x: a first string value
  4023. * @y: a second string value
  4024. * @invert: inverts the result if x < y or x > y.
  4025. *
  4026. * Compare 2 string for their normalized values.
  4027. * @x is a string with whitespace of "preserve", @y is
  4028. * a string with a whitespace of "replace". I.e. @x could
  4029. * be an "xsd:string" and @y an "xsd:normalizedString".
  4030. *
  4031. * Returns -1 if x < y, 0 if x == y, 1 if x > y, and -2 in
  4032. * case of error
  4033. */
  4034. static int
  4035. xmlSchemaComparePreserveReplaceStrings(const xmlChar *x,
  4036. const xmlChar *y,
  4037. int invert)
  4038. {
  4039. int tmp;
  4040. while ((*x != 0) && (*y != 0)) {
  4041. if (IS_WSP_REPLACE_CH(*y)) {
  4042. if (! IS_WSP_SPACE_CH(*x)) {
  4043. if ((*x - 0x20) < 0) {
  4044. if (invert)
  4045. return(1);
  4046. else
  4047. return(-1);
  4048. } else {
  4049. if (invert)
  4050. return(-1);
  4051. else
  4052. return(1);
  4053. }
  4054. }
  4055. } else {
  4056. tmp = *x - *y;
  4057. if (tmp < 0) {
  4058. if (invert)
  4059. return(1);
  4060. else
  4061. return(-1);
  4062. }
  4063. if (tmp > 0) {
  4064. if (invert)
  4065. return(-1);
  4066. else
  4067. return(1);
  4068. }
  4069. }
  4070. x++;
  4071. y++;
  4072. }
  4073. if (*x != 0) {
  4074. if (invert)
  4075. return(-1);
  4076. else
  4077. return(1);
  4078. }
  4079. if (*y != 0) {
  4080. if (invert)
  4081. return(1);
  4082. else
  4083. return(-1);
  4084. }
  4085. return(0);
  4086. }
  4087. /**
  4088. * xmlSchemaComparePreserveCollapseStrings:
  4089. * @x: a first string value
  4090. * @y: a second string value
  4091. *
  4092. * Compare 2 string for their normalized values.
  4093. * @x is a string with whitespace of "preserve", @y is
  4094. * a string with a whitespace of "collapse". I.e. @x could
  4095. * be an "xsd:string" and @y an "xsd:normalizedString".
  4096. *
  4097. * Returns -1 if x < y, 0 if x == y, 1 if x > y, and -2 in
  4098. * case of error
  4099. */
  4100. static int
  4101. xmlSchemaComparePreserveCollapseStrings(const xmlChar *x,
  4102. const xmlChar *y,
  4103. int invert)
  4104. {
  4105. int tmp;
  4106. /*
  4107. * Skip leading blank chars of the collapsed string.
  4108. */
  4109. while IS_WSP_BLANK_CH(*y)
  4110. y++;
  4111. while ((*x != 0) && (*y != 0)) {
  4112. if IS_WSP_BLANK_CH(*y) {
  4113. if (! IS_WSP_SPACE_CH(*x)) {
  4114. /*
  4115. * The yv character would have been replaced to 0x20.
  4116. */
  4117. if ((*x - 0x20) < 0) {
  4118. if (invert)
  4119. return(1);
  4120. else
  4121. return(-1);
  4122. } else {
  4123. if (invert)
  4124. return(-1);
  4125. else
  4126. return(1);
  4127. }
  4128. }
  4129. x++;
  4130. y++;
  4131. /*
  4132. * Skip contiguous blank chars of the collapsed string.
  4133. */
  4134. while IS_WSP_BLANK_CH(*y)
  4135. y++;
  4136. } else {
  4137. tmp = *x++ - *y++;
  4138. if (tmp < 0) {
  4139. if (invert)
  4140. return(1);
  4141. else
  4142. return(-1);
  4143. }
  4144. if (tmp > 0) {
  4145. if (invert)
  4146. return(-1);
  4147. else
  4148. return(1);
  4149. }
  4150. }
  4151. }
  4152. if (*x != 0) {
  4153. if (invert)
  4154. return(-1);
  4155. else
  4156. return(1);
  4157. }
  4158. if (*y != 0) {
  4159. /*
  4160. * Skip trailing blank chars of the collapsed string.
  4161. */
  4162. while IS_WSP_BLANK_CH(*y)
  4163. y++;
  4164. if (*y != 0) {
  4165. if (invert)
  4166. return(1);
  4167. else
  4168. return(-1);
  4169. }
  4170. }
  4171. return(0);
  4172. }
  4173. /**
  4174. * xmlSchemaComparePreserveCollapseStrings:
  4175. * @x: a first string value
  4176. * @y: a second string value
  4177. *
  4178. * Compare 2 string for their normalized values.
  4179. * @x is a string with whitespace of "preserve", @y is
  4180. * a string with a whitespace of "collapse". I.e. @x could
  4181. * be an "xsd:string" and @y an "xsd:normalizedString".
  4182. *
  4183. * Returns -1 if x < y, 0 if x == y, 1 if x > y, and -2 in
  4184. * case of error
  4185. */
  4186. static int
  4187. xmlSchemaCompareReplaceCollapseStrings(const xmlChar *x,
  4188. const xmlChar *y,
  4189. int invert)
  4190. {
  4191. int tmp;
  4192. /*
  4193. * Skip leading blank chars of the collapsed string.
  4194. */
  4195. while IS_WSP_BLANK_CH(*y)
  4196. y++;
  4197. while ((*x != 0) && (*y != 0)) {
  4198. if IS_WSP_BLANK_CH(*y) {
  4199. if (! IS_WSP_BLANK_CH(*x)) {
  4200. /*
  4201. * The yv character would have been replaced to 0x20.
  4202. */
  4203. if ((*x - 0x20) < 0) {
  4204. if (invert)
  4205. return(1);
  4206. else
  4207. return(-1);
  4208. } else {
  4209. if (invert)
  4210. return(-1);
  4211. else
  4212. return(1);
  4213. }
  4214. }
  4215. x++;
  4216. y++;
  4217. /*
  4218. * Skip contiguous blank chars of the collapsed string.
  4219. */
  4220. while IS_WSP_BLANK_CH(*y)
  4221. y++;
  4222. } else {
  4223. if IS_WSP_BLANK_CH(*x) {
  4224. /*
  4225. * The xv character would have been replaced to 0x20.
  4226. */
  4227. if ((0x20 - *y) < 0) {
  4228. if (invert)
  4229. return(1);
  4230. else
  4231. return(-1);
  4232. } else {
  4233. if (invert)
  4234. return(-1);
  4235. else
  4236. return(1);
  4237. }
  4238. }
  4239. tmp = *x++ - *y++;
  4240. if (tmp < 0)
  4241. return(-1);
  4242. if (tmp > 0)
  4243. return(1);
  4244. }
  4245. }
  4246. if (*x != 0) {
  4247. if (invert)
  4248. return(-1);
  4249. else
  4250. return(1);
  4251. }
  4252. if (*y != 0) {
  4253. /*
  4254. * Skip trailing blank chars of the collapsed string.
  4255. */
  4256. while IS_WSP_BLANK_CH(*y)
  4257. y++;
  4258. if (*y != 0) {
  4259. if (invert)
  4260. return(1);
  4261. else
  4262. return(-1);
  4263. }
  4264. }
  4265. return(0);
  4266. }
  4267. /**
  4268. * xmlSchemaCompareReplacedStrings:
  4269. * @x: a first string value
  4270. * @y: a second string value
  4271. *
  4272. * Compare 2 string for their normalized values.
  4273. *
  4274. * Returns -1 if x < y, 0 if x == y, 1 if x > y, and -2 in
  4275. * case of error
  4276. */
  4277. static int
  4278. xmlSchemaCompareReplacedStrings(const xmlChar *x,
  4279. const xmlChar *y)
  4280. {
  4281. int tmp;
  4282. while ((*x != 0) && (*y != 0)) {
  4283. if IS_WSP_BLANK_CH(*y) {
  4284. if (! IS_WSP_BLANK_CH(*x)) {
  4285. if ((*x - 0x20) < 0)
  4286. return(-1);
  4287. else
  4288. return(1);
  4289. }
  4290. } else {
  4291. if IS_WSP_BLANK_CH(*x) {
  4292. if ((0x20 - *y) < 0)
  4293. return(-1);
  4294. else
  4295. return(1);
  4296. }
  4297. tmp = *x - *y;
  4298. if (tmp < 0)
  4299. return(-1);
  4300. if (tmp > 0)
  4301. return(1);
  4302. }
  4303. x++;
  4304. y++;
  4305. }
  4306. if (*x != 0)
  4307. return(1);
  4308. if (*y != 0)
  4309. return(-1);
  4310. return(0);
  4311. }
  4312. /**
  4313. * xmlSchemaCompareNormStrings:
  4314. * @x: a first string value
  4315. * @y: a second string value
  4316. *
  4317. * Compare 2 string for their normalized values.
  4318. *
  4319. * Returns -1 if x < y, 0 if x == y, 1 if x > y, and -2 in
  4320. * case of error
  4321. */
  4322. static int
  4323. xmlSchemaCompareNormStrings(const xmlChar *x,
  4324. const xmlChar *y) {
  4325. int tmp;
  4326. while (IS_BLANK_CH(*x)) x++;
  4327. while (IS_BLANK_CH(*y)) y++;
  4328. while ((*x != 0) && (*y != 0)) {
  4329. if (IS_BLANK_CH(*x)) {
  4330. if (!IS_BLANK_CH(*y)) {
  4331. tmp = *x - *y;
  4332. return(tmp);
  4333. }
  4334. while (IS_BLANK_CH(*x)) x++;
  4335. while (IS_BLANK_CH(*y)) y++;
  4336. } else {
  4337. tmp = *x++ - *y++;
  4338. if (tmp < 0)
  4339. return(-1);
  4340. if (tmp > 0)
  4341. return(1);
  4342. }
  4343. }
  4344. if (*x != 0) {
  4345. while (IS_BLANK_CH(*x)) x++;
  4346. if (*x != 0)
  4347. return(1);
  4348. }
  4349. if (*y != 0) {
  4350. while (IS_BLANK_CH(*y)) y++;
  4351. if (*y != 0)
  4352. return(-1);
  4353. }
  4354. return(0);
  4355. }
  4356. /**
  4357. * xmlSchemaCompareFloats:
  4358. * @x: a first float or double value
  4359. * @y: a second float or double value
  4360. *
  4361. * Compare 2 values
  4362. *
  4363. * Returns -1 if x < y, 0 if x == y, 1 if x > y, 2 if x <> y, and -2 in
  4364. * case of error
  4365. */
  4366. static int
  4367. xmlSchemaCompareFloats(xmlSchemaValPtr x, xmlSchemaValPtr y) {
  4368. double d1, d2;
  4369. if ((x == NULL) || (y == NULL))
  4370. return(-2);
  4371. /*
  4372. * Cast everything to doubles.
  4373. */
  4374. if (x->type == XML_SCHEMAS_DOUBLE)
  4375. d1 = x->value.d;
  4376. else if (x->type == XML_SCHEMAS_FLOAT)
  4377. d1 = x->value.f;
  4378. else
  4379. return(-2);
  4380. if (y->type == XML_SCHEMAS_DOUBLE)
  4381. d2 = y->value.d;
  4382. else if (y->type == XML_SCHEMAS_FLOAT)
  4383. d2 = y->value.f;
  4384. else
  4385. return(-2);
  4386. /*
  4387. * Check for special cases.
  4388. */
  4389. if (xmlXPathIsNaN(d1)) {
  4390. if (xmlXPathIsNaN(d2))
  4391. return(0);
  4392. return(1);
  4393. }
  4394. if (xmlXPathIsNaN(d2))
  4395. return(-1);
  4396. if (d1 == xmlXPathPINF) {
  4397. if (d2 == xmlXPathPINF)
  4398. return(0);
  4399. return(1);
  4400. }
  4401. if (d2 == xmlXPathPINF)
  4402. return(-1);
  4403. if (d1 == xmlXPathNINF) {
  4404. if (d2 == xmlXPathNINF)
  4405. return(0);
  4406. return(-1);
  4407. }
  4408. if (d2 == xmlXPathNINF)
  4409. return(1);
  4410. /*
  4411. * basic tests, the last one we should have equality, but
  4412. * portability is more important than speed and handling
  4413. * NaN or Inf in a portable way is always a challenge, so ...
  4414. */
  4415. if (d1 < d2)
  4416. return(-1);
  4417. if (d1 > d2)
  4418. return(1);
  4419. if (d1 == d2)
  4420. return(0);
  4421. return(2);
  4422. }
  4423. /**
  4424. * xmlSchemaCompareValues:
  4425. * @x: a first value
  4426. * @xvalue: the first value as a string (optional)
  4427. * @xwtsp: the whitespace type
  4428. * @y: a second value
  4429. * @xvalue: the second value as a string (optional)
  4430. * @ywtsp: the whitespace type
  4431. *
  4432. * Compare 2 values
  4433. *
  4434. * Returns -1 if x < y, 0 if x == y, 1 if x > y, 2 if x <> y, 3 if not
  4435. * comparable and -2 in case of error
  4436. */
  4437. static int
  4438. xmlSchemaCompareValuesInternal(xmlSchemaValType xtype,
  4439. xmlSchemaValPtr x,
  4440. const xmlChar *xvalue,
  4441. xmlSchemaWhitespaceValueType xws,
  4442. xmlSchemaValType ytype,
  4443. xmlSchemaValPtr y,
  4444. const xmlChar *yvalue,
  4445. xmlSchemaWhitespaceValueType yws)
  4446. {
  4447. switch (xtype) {
  4448. case XML_SCHEMAS_UNKNOWN:
  4449. case XML_SCHEMAS_ANYTYPE:
  4450. return(-2);
  4451. case XML_SCHEMAS_INTEGER:
  4452. case XML_SCHEMAS_NPINTEGER:
  4453. case XML_SCHEMAS_NINTEGER:
  4454. case XML_SCHEMAS_NNINTEGER:
  4455. case XML_SCHEMAS_PINTEGER:
  4456. case XML_SCHEMAS_INT:
  4457. case XML_SCHEMAS_UINT:
  4458. case XML_SCHEMAS_LONG:
  4459. case XML_SCHEMAS_ULONG:
  4460. case XML_SCHEMAS_SHORT:
  4461. case XML_SCHEMAS_USHORT:
  4462. case XML_SCHEMAS_BYTE:
  4463. case XML_SCHEMAS_UBYTE:
  4464. case XML_SCHEMAS_DECIMAL:
  4465. if ((x == NULL) || (y == NULL))
  4466. return(-2);
  4467. if (ytype == xtype)
  4468. return(xmlSchemaCompareDecimals(x, y));
  4469. if ((ytype == XML_SCHEMAS_DECIMAL) ||
  4470. (ytype == XML_SCHEMAS_INTEGER) ||
  4471. (ytype == XML_SCHEMAS_NPINTEGER) ||
  4472. (ytype == XML_SCHEMAS_NINTEGER) ||
  4473. (ytype == XML_SCHEMAS_NNINTEGER) ||
  4474. (ytype == XML_SCHEMAS_PINTEGER) ||
  4475. (ytype == XML_SCHEMAS_INT) ||
  4476. (ytype == XML_SCHEMAS_UINT) ||
  4477. (ytype == XML_SCHEMAS_LONG) ||
  4478. (ytype == XML_SCHEMAS_ULONG) ||
  4479. (ytype == XML_SCHEMAS_SHORT) ||
  4480. (ytype == XML_SCHEMAS_USHORT) ||
  4481. (ytype == XML_SCHEMAS_BYTE) ||
  4482. (ytype == XML_SCHEMAS_UBYTE))
  4483. return(xmlSchemaCompareDecimals(x, y));
  4484. return(-2);
  4485. case XML_SCHEMAS_DURATION:
  4486. if ((x == NULL) || (y == NULL))
  4487. return(-2);
  4488. if (ytype == XML_SCHEMAS_DURATION)
  4489. return(xmlSchemaCompareDurations(x, y));
  4490. return(-2);
  4491. case XML_SCHEMAS_TIME:
  4492. case XML_SCHEMAS_GDAY:
  4493. case XML_SCHEMAS_GMONTH:
  4494. case XML_SCHEMAS_GMONTHDAY:
  4495. case XML_SCHEMAS_GYEAR:
  4496. case XML_SCHEMAS_GYEARMONTH:
  4497. case XML_SCHEMAS_DATE:
  4498. case XML_SCHEMAS_DATETIME:
  4499. if ((x == NULL) || (y == NULL))
  4500. return(-2);
  4501. if ((ytype == XML_SCHEMAS_DATETIME) ||
  4502. (ytype == XML_SCHEMAS_TIME) ||
  4503. (ytype == XML_SCHEMAS_GDAY) ||
  4504. (ytype == XML_SCHEMAS_GMONTH) ||
  4505. (ytype == XML_SCHEMAS_GMONTHDAY) ||
  4506. (ytype == XML_SCHEMAS_GYEAR) ||
  4507. (ytype == XML_SCHEMAS_DATE) ||
  4508. (ytype == XML_SCHEMAS_GYEARMONTH))
  4509. return (xmlSchemaCompareDates(x, y));
  4510. return (-2);
  4511. /*
  4512. * Note that we will support comparison of string types against
  4513. * anySimpleType as well.
  4514. */
  4515. case XML_SCHEMAS_ANYSIMPLETYPE:
  4516. case XML_SCHEMAS_STRING:
  4517. case XML_SCHEMAS_NORMSTRING:
  4518. case XML_SCHEMAS_TOKEN:
  4519. case XML_SCHEMAS_LANGUAGE:
  4520. case XML_SCHEMAS_NMTOKEN:
  4521. case XML_SCHEMAS_NAME:
  4522. case XML_SCHEMAS_NCNAME:
  4523. case XML_SCHEMAS_ID:
  4524. case XML_SCHEMAS_IDREF:
  4525. case XML_SCHEMAS_ENTITY:
  4526. case XML_SCHEMAS_ANYURI:
  4527. {
  4528. const xmlChar *xv, *yv;
  4529. if (x == NULL)
  4530. xv = xvalue;
  4531. else
  4532. xv = x->value.str;
  4533. if (y == NULL)
  4534. yv = yvalue;
  4535. else
  4536. yv = y->value.str;
  4537. /*
  4538. * TODO: Compare those against QName.
  4539. */
  4540. if (ytype == XML_SCHEMAS_QNAME) {
  4541. TODO
  4542. if (y == NULL)
  4543. return(-2);
  4544. return (-2);
  4545. }
  4546. if ((ytype == XML_SCHEMAS_ANYSIMPLETYPE) ||
  4547. (ytype == XML_SCHEMAS_STRING) ||
  4548. (ytype == XML_SCHEMAS_NORMSTRING) ||
  4549. (ytype == XML_SCHEMAS_TOKEN) ||
  4550. (ytype == XML_SCHEMAS_LANGUAGE) ||
  4551. (ytype == XML_SCHEMAS_NMTOKEN) ||
  4552. (ytype == XML_SCHEMAS_NAME) ||
  4553. (ytype == XML_SCHEMAS_NCNAME) ||
  4554. (ytype == XML_SCHEMAS_ID) ||
  4555. (ytype == XML_SCHEMAS_IDREF) ||
  4556. (ytype == XML_SCHEMAS_ENTITY) ||
  4557. (ytype == XML_SCHEMAS_ANYURI)) {
  4558. if (xws == XML_SCHEMA_WHITESPACE_PRESERVE) {
  4559. if (yws == XML_SCHEMA_WHITESPACE_PRESERVE) {
  4560. /* TODO: What about x < y or x > y. */
  4561. if (xmlStrEqual(xv, yv))
  4562. return (0);
  4563. else
  4564. return (2);
  4565. } else if (yws == XML_SCHEMA_WHITESPACE_REPLACE)
  4566. return (xmlSchemaComparePreserveReplaceStrings(xv, yv, 0));
  4567. else if (yws == XML_SCHEMA_WHITESPACE_COLLAPSE)
  4568. return (xmlSchemaComparePreserveCollapseStrings(xv, yv, 0));
  4569. } else if (xws == XML_SCHEMA_WHITESPACE_REPLACE) {
  4570. if (yws == XML_SCHEMA_WHITESPACE_PRESERVE)
  4571. return (xmlSchemaComparePreserveReplaceStrings(yv, xv, 1));
  4572. if (yws == XML_SCHEMA_WHITESPACE_REPLACE)
  4573. return (xmlSchemaCompareReplacedStrings(xv, yv));
  4574. if (yws == XML_SCHEMA_WHITESPACE_COLLAPSE)
  4575. return (xmlSchemaCompareReplaceCollapseStrings(xv, yv, 0));
  4576. } else if (xws == XML_SCHEMA_WHITESPACE_COLLAPSE) {
  4577. if (yws == XML_SCHEMA_WHITESPACE_PRESERVE)
  4578. return (xmlSchemaComparePreserveCollapseStrings(yv, xv, 1));
  4579. if (yws == XML_SCHEMA_WHITESPACE_REPLACE)
  4580. return (xmlSchemaCompareReplaceCollapseStrings(yv, xv, 1));
  4581. if (yws == XML_SCHEMA_WHITESPACE_COLLAPSE)
  4582. return (xmlSchemaCompareNormStrings(xv, yv));
  4583. } else
  4584. return (-2);
  4585. }
  4586. return (-2);
  4587. }
  4588. case XML_SCHEMAS_QNAME:
  4589. case XML_SCHEMAS_NOTATION:
  4590. if ((x == NULL) || (y == NULL))
  4591. return(-2);
  4592. if ((ytype == XML_SCHEMAS_QNAME) ||
  4593. (ytype == XML_SCHEMAS_NOTATION)) {
  4594. if ((xmlStrEqual(x->value.qname.name, y->value.qname.name)) &&
  4595. (xmlStrEqual(x->value.qname.uri, y->value.qname.uri)))
  4596. return(0);
  4597. return(2);
  4598. }
  4599. return (-2);
  4600. case XML_SCHEMAS_FLOAT:
  4601. case XML_SCHEMAS_DOUBLE:
  4602. if ((x == NULL) || (y == NULL))
  4603. return(-2);
  4604. if ((ytype == XML_SCHEMAS_FLOAT) ||
  4605. (ytype == XML_SCHEMAS_DOUBLE))
  4606. return (xmlSchemaCompareFloats(x, y));
  4607. return (-2);
  4608. case XML_SCHEMAS_BOOLEAN:
  4609. if ((x == NULL) || (y == NULL))
  4610. return(-2);
  4611. if (ytype == XML_SCHEMAS_BOOLEAN) {
  4612. if (x->value.b == y->value.b)
  4613. return(0);
  4614. if (x->value.b == 0)
  4615. return(-1);
  4616. return(1);
  4617. }
  4618. return (-2);
  4619. case XML_SCHEMAS_HEXBINARY:
  4620. if ((x == NULL) || (y == NULL))
  4621. return(-2);
  4622. if (ytype == XML_SCHEMAS_HEXBINARY) {
  4623. if (x->value.hex.total == y->value.hex.total) {
  4624. int ret = xmlStrcmp(x->value.hex.str, y->value.hex.str);
  4625. if (ret > 0)
  4626. return(1);
  4627. else if (ret == 0)
  4628. return(0);
  4629. }
  4630. else if (x->value.hex.total > y->value.hex.total)
  4631. return(1);
  4632. return(-1);
  4633. }
  4634. return (-2);
  4635. case XML_SCHEMAS_BASE64BINARY:
  4636. if ((x == NULL) || (y == NULL))
  4637. return(-2);
  4638. if (ytype == XML_SCHEMAS_BASE64BINARY) {
  4639. if (x->value.base64.total == y->value.base64.total) {
  4640. int ret = xmlStrcmp(x->value.base64.str,
  4641. y->value.base64.str);
  4642. if (ret > 0)
  4643. return(1);
  4644. else if (ret == 0)
  4645. return(0);
  4646. else
  4647. return(-1);
  4648. }
  4649. else if (x->value.base64.total > y->value.base64.total)
  4650. return(1);
  4651. else
  4652. return(-1);
  4653. }
  4654. return (-2);
  4655. case XML_SCHEMAS_IDREFS:
  4656. case XML_SCHEMAS_ENTITIES:
  4657. case XML_SCHEMAS_NMTOKENS:
  4658. TODO
  4659. break;
  4660. }
  4661. return -2;
  4662. }
  4663. /**
  4664. * xmlSchemaCompareValues:
  4665. * @x: a first value
  4666. * @y: a second value
  4667. *
  4668. * Compare 2 values
  4669. *
  4670. * Returns -1 if x < y, 0 if x == y, 1 if x > y, 2 if x <> y, and -2 in
  4671. * case of error
  4672. */
  4673. int
  4674. xmlSchemaCompareValues(xmlSchemaValPtr x, xmlSchemaValPtr y) {
  4675. xmlSchemaWhitespaceValueType xws, yws;
  4676. if ((x == NULL) || (y == NULL))
  4677. return(-2);
  4678. if (x->type == XML_SCHEMAS_STRING)
  4679. xws = XML_SCHEMA_WHITESPACE_PRESERVE;
  4680. else if (x->type == XML_SCHEMAS_NORMSTRING)
  4681. xws = XML_SCHEMA_WHITESPACE_REPLACE;
  4682. else
  4683. xws = XML_SCHEMA_WHITESPACE_COLLAPSE;
  4684. if (y->type == XML_SCHEMAS_STRING)
  4685. yws = XML_SCHEMA_WHITESPACE_PRESERVE;
  4686. else if (y->type == XML_SCHEMAS_NORMSTRING)
  4687. yws = XML_SCHEMA_WHITESPACE_REPLACE;
  4688. else
  4689. yws = XML_SCHEMA_WHITESPACE_COLLAPSE;
  4690. return(xmlSchemaCompareValuesInternal(x->type, x, NULL, xws, y->type,
  4691. y, NULL, yws));
  4692. }
  4693. /**
  4694. * xmlSchemaCompareValuesWhtsp:
  4695. * @x: a first value
  4696. * @xws: the whitespace value of x
  4697. * @y: a second value
  4698. * @yws: the whitespace value of y
  4699. *
  4700. * Compare 2 values
  4701. *
  4702. * Returns -1 if x < y, 0 if x == y, 1 if x > y, 2 if x <> y, and -2 in
  4703. * case of error
  4704. */
  4705. int
  4706. xmlSchemaCompareValuesWhtsp(xmlSchemaValPtr x,
  4707. xmlSchemaWhitespaceValueType xws,
  4708. xmlSchemaValPtr y,
  4709. xmlSchemaWhitespaceValueType yws)
  4710. {
  4711. if ((x == NULL) || (y == NULL))
  4712. return(-2);
  4713. return(xmlSchemaCompareValuesInternal(x->type, x, NULL, xws, y->type,
  4714. y, NULL, yws));
  4715. }
  4716. /**
  4717. * xmlSchemaCompareValuesWhtspExt:
  4718. * @x: a first value
  4719. * @xws: the whitespace value of x
  4720. * @y: a second value
  4721. * @yws: the whitespace value of y
  4722. *
  4723. * Compare 2 values
  4724. *
  4725. * Returns -1 if x < y, 0 if x == y, 1 if x > y, 2 if x <> y, and -2 in
  4726. * case of error
  4727. */
  4728. static int
  4729. xmlSchemaCompareValuesWhtspExt(xmlSchemaValType xtype,
  4730. xmlSchemaValPtr x,
  4731. const xmlChar *xvalue,
  4732. xmlSchemaWhitespaceValueType xws,
  4733. xmlSchemaValType ytype,
  4734. xmlSchemaValPtr y,
  4735. const xmlChar *yvalue,
  4736. xmlSchemaWhitespaceValueType yws)
  4737. {
  4738. return(xmlSchemaCompareValuesInternal(xtype, x, xvalue, xws, ytype, y,
  4739. yvalue, yws));
  4740. }
  4741. /**
  4742. * xmlSchemaNormLen:
  4743. * @value: a string
  4744. *
  4745. * Computes the UTF8 length of the normalized value of the string
  4746. *
  4747. * Returns the length or -1 in case of error.
  4748. */
  4749. static int
  4750. xmlSchemaNormLen(const xmlChar *value) {
  4751. const xmlChar *utf;
  4752. int ret = 0;
  4753. if (value == NULL)
  4754. return(-1);
  4755. utf = value;
  4756. while (IS_BLANK_CH(*utf)) utf++;
  4757. while (*utf != 0) {
  4758. if (utf[0] & 0x80) {
  4759. if ((utf[1] & 0xc0) != 0x80)
  4760. return(-1);
  4761. if ((utf[0] & 0xe0) == 0xe0) {
  4762. if ((utf[2] & 0xc0) != 0x80)
  4763. return(-1);
  4764. if ((utf[0] & 0xf0) == 0xf0) {
  4765. if ((utf[0] & 0xf8) != 0xf0 || (utf[3] & 0xc0) != 0x80)
  4766. return(-1);
  4767. utf += 4;
  4768. } else {
  4769. utf += 3;
  4770. }
  4771. } else {
  4772. utf += 2;
  4773. }
  4774. } else if (IS_BLANK_CH(*utf)) {
  4775. while (IS_BLANK_CH(*utf)) utf++;
  4776. if (*utf == 0)
  4777. break;
  4778. } else {
  4779. utf++;
  4780. }
  4781. ret++;
  4782. }
  4783. return(ret);
  4784. }
  4785. /**
  4786. * xmlSchemaGetFacetValueAsULong:
  4787. * @facet: an schemas type facet
  4788. *
  4789. * Extract the value of a facet
  4790. *
  4791. * Returns the value as a long
  4792. */
  4793. unsigned long
  4794. xmlSchemaGetFacetValueAsULong(xmlSchemaFacetPtr facet)
  4795. {
  4796. /*
  4797. * TODO: Check if this is a decimal.
  4798. */
  4799. if (facet == NULL || facet->val == NULL)
  4800. return 0;
  4801. return ((unsigned long) facet->val->value.decimal.lo);
  4802. }
  4803. /**
  4804. * xmlSchemaValidateListSimpleTypeFacet:
  4805. * @facet: the facet to check
  4806. * @value: the lexical repr of the value to validate
  4807. * @actualLen: the number of list items
  4808. * @expectedLen: the resulting expected number of list items
  4809. *
  4810. * Checks the value of a list simple type against a facet.
  4811. *
  4812. * Returns 0 if the value is valid, a positive error code
  4813. * number otherwise and -1 in case of an internal error.
  4814. */
  4815. int
  4816. xmlSchemaValidateListSimpleTypeFacet(xmlSchemaFacetPtr facet,
  4817. const xmlChar *value,
  4818. unsigned long actualLen,
  4819. unsigned long *expectedLen)
  4820. {
  4821. if (facet == NULL)
  4822. return(-1);
  4823. /*
  4824. * TODO: Check if this will work with large numbers.
  4825. * (compare value.decimal.mi and value.decimal.hi as well?).
  4826. */
  4827. if (facet->type == XML_SCHEMA_FACET_LENGTH) {
  4828. if (actualLen != facet->val->value.decimal.lo) {
  4829. if (expectedLen != NULL)
  4830. *expectedLen = facet->val->value.decimal.lo;
  4831. return (XML_SCHEMAV_CVC_LENGTH_VALID);
  4832. }
  4833. } else if (facet->type == XML_SCHEMA_FACET_MINLENGTH) {
  4834. if (actualLen < facet->val->value.decimal.lo) {
  4835. if (expectedLen != NULL)
  4836. *expectedLen = facet->val->value.decimal.lo;
  4837. return (XML_SCHEMAV_CVC_MINLENGTH_VALID);
  4838. }
  4839. } else if (facet->type == XML_SCHEMA_FACET_MAXLENGTH) {
  4840. if (actualLen > facet->val->value.decimal.lo) {
  4841. if (expectedLen != NULL)
  4842. *expectedLen = facet->val->value.decimal.lo;
  4843. return (XML_SCHEMAV_CVC_MAXLENGTH_VALID);
  4844. }
  4845. } else
  4846. /*
  4847. * NOTE: That we can pass NULL as xmlSchemaValPtr to
  4848. * xmlSchemaValidateFacet, since the remaining facet types
  4849. * are: XML_SCHEMA_FACET_PATTERN, XML_SCHEMA_FACET_ENUMERATION.
  4850. */
  4851. return(xmlSchemaValidateFacet(NULL, facet, value, NULL));
  4852. return (0);
  4853. }
  4854. /**
  4855. * xmlSchemaValidateLengthFacet:
  4856. * @type: the built-in type
  4857. * @facet: the facet to check
  4858. * @value: the lexical repr. of the value to be validated
  4859. * @val: the precomputed value
  4860. * @ws: the whitespace type of the value
  4861. * @length: the actual length of the value
  4862. *
  4863. * Checka a value against a "length", "minLength" and "maxLength"
  4864. * facet; sets @length to the computed length of @value.
  4865. *
  4866. * Returns 0 if the value is valid, a positive error code
  4867. * otherwise and -1 in case of an internal or API error.
  4868. */
  4869. static int
  4870. xmlSchemaValidateLengthFacetInternal(xmlSchemaFacetPtr facet,
  4871. xmlSchemaValType valType,
  4872. const xmlChar *value,
  4873. xmlSchemaValPtr val,
  4874. unsigned long *length,
  4875. xmlSchemaWhitespaceValueType ws)
  4876. {
  4877. unsigned int len = 0;
  4878. if ((length == NULL) || (facet == NULL))
  4879. return (-1);
  4880. *length = 0;
  4881. if ((facet->type != XML_SCHEMA_FACET_LENGTH) &&
  4882. (facet->type != XML_SCHEMA_FACET_MAXLENGTH) &&
  4883. (facet->type != XML_SCHEMA_FACET_MINLENGTH))
  4884. return (-1);
  4885. /*
  4886. * TODO: length, maxLength and minLength must be of type
  4887. * nonNegativeInteger only. Check if decimal is used somehow.
  4888. */
  4889. if ((facet->val == NULL) ||
  4890. ((facet->val->type != XML_SCHEMAS_DECIMAL) &&
  4891. (facet->val->type != XML_SCHEMAS_NNINTEGER)) ||
  4892. (facet->val->value.decimal.frac != 0)) {
  4893. return(-1);
  4894. }
  4895. if ((val != NULL) && (val->type == XML_SCHEMAS_HEXBINARY))
  4896. len = val->value.hex.total;
  4897. else if ((val != NULL) && (val->type == XML_SCHEMAS_BASE64BINARY))
  4898. len = val->value.base64.total;
  4899. else {
  4900. switch (valType) {
  4901. case XML_SCHEMAS_STRING:
  4902. case XML_SCHEMAS_NORMSTRING:
  4903. if (ws == XML_SCHEMA_WHITESPACE_UNKNOWN) {
  4904. /*
  4905. * This is to ensure API compatibility with the old
  4906. * xmlSchemaValidateLengthFacet(). Anyway, this was and
  4907. * is not the correct handling.
  4908. * TODO: Get rid of this case somehow.
  4909. */
  4910. if (valType == XML_SCHEMAS_STRING)
  4911. len = xmlUTF8Strlen(value);
  4912. else
  4913. len = xmlSchemaNormLen(value);
  4914. } else if (value != NULL) {
  4915. if (ws == XML_SCHEMA_WHITESPACE_COLLAPSE)
  4916. len = xmlSchemaNormLen(value);
  4917. else
  4918. /*
  4919. * Should be OK for "preserve" as well.
  4920. */
  4921. len = xmlUTF8Strlen(value);
  4922. }
  4923. break;
  4924. case XML_SCHEMAS_IDREF:
  4925. case XML_SCHEMAS_TOKEN:
  4926. case XML_SCHEMAS_LANGUAGE:
  4927. case XML_SCHEMAS_NMTOKEN:
  4928. case XML_SCHEMAS_NAME:
  4929. case XML_SCHEMAS_NCNAME:
  4930. case XML_SCHEMAS_ID:
  4931. /*
  4932. * FIXME: What exactly to do with anyURI?
  4933. */
  4934. case XML_SCHEMAS_ANYURI:
  4935. if (value != NULL)
  4936. len = xmlSchemaNormLen(value);
  4937. break;
  4938. case XML_SCHEMAS_QNAME:
  4939. case XML_SCHEMAS_NOTATION:
  4940. /*
  4941. * For QName and NOTATION, those facets are
  4942. * deprecated and should be ignored.
  4943. */
  4944. return (0);
  4945. default:
  4946. TODO
  4947. }
  4948. }
  4949. *length = (unsigned long) len;
  4950. /*
  4951. * TODO: Return the whole expected value, i.e. "lo", "mi" and "hi".
  4952. */
  4953. if (facet->type == XML_SCHEMA_FACET_LENGTH) {
  4954. if (len != facet->val->value.decimal.lo)
  4955. return(XML_SCHEMAV_CVC_LENGTH_VALID);
  4956. } else if (facet->type == XML_SCHEMA_FACET_MINLENGTH) {
  4957. if (len < facet->val->value.decimal.lo)
  4958. return(XML_SCHEMAV_CVC_MINLENGTH_VALID);
  4959. } else {
  4960. if (len > facet->val->value.decimal.lo)
  4961. return(XML_SCHEMAV_CVC_MAXLENGTH_VALID);
  4962. }
  4963. return (0);
  4964. }
  4965. /**
  4966. * xmlSchemaValidateLengthFacet:
  4967. * @type: the built-in type
  4968. * @facet: the facet to check
  4969. * @value: the lexical repr. of the value to be validated
  4970. * @val: the precomputed value
  4971. * @length: the actual length of the value
  4972. *
  4973. * Checka a value against a "length", "minLength" and "maxLength"
  4974. * facet; sets @length to the computed length of @value.
  4975. *
  4976. * Returns 0 if the value is valid, a positive error code
  4977. * otherwise and -1 in case of an internal or API error.
  4978. */
  4979. int
  4980. xmlSchemaValidateLengthFacet(xmlSchemaTypePtr type,
  4981. xmlSchemaFacetPtr facet,
  4982. const xmlChar *value,
  4983. xmlSchemaValPtr val,
  4984. unsigned long *length)
  4985. {
  4986. if (type == NULL)
  4987. return(-1);
  4988. return (xmlSchemaValidateLengthFacetInternal(facet,
  4989. type->builtInType, value, val, length,
  4990. XML_SCHEMA_WHITESPACE_UNKNOWN));
  4991. }
  4992. /**
  4993. * xmlSchemaValidateLengthFacetWhtsp:
  4994. * @facet: the facet to check
  4995. * @valType: the built-in type
  4996. * @value: the lexical repr. of the value to be validated
  4997. * @val: the precomputed value
  4998. * @ws: the whitespace type of the value
  4999. * @length: the actual length of the value
  5000. *
  5001. * Checka a value against a "length", "minLength" and "maxLength"
  5002. * facet; sets @length to the computed length of @value.
  5003. *
  5004. * Returns 0 if the value is valid, a positive error code
  5005. * otherwise and -1 in case of an internal or API error.
  5006. */
  5007. int
  5008. xmlSchemaValidateLengthFacetWhtsp(xmlSchemaFacetPtr facet,
  5009. xmlSchemaValType valType,
  5010. const xmlChar *value,
  5011. xmlSchemaValPtr val,
  5012. unsigned long *length,
  5013. xmlSchemaWhitespaceValueType ws)
  5014. {
  5015. return (xmlSchemaValidateLengthFacetInternal(facet, valType, value, val,
  5016. length, ws));
  5017. }
  5018. /**
  5019. * xmlSchemaValidateFacetInternal:
  5020. * @facet: the facet to check
  5021. * @fws: the whitespace type of the facet's value
  5022. * @valType: the built-in type of the value
  5023. * @value: the lexical repr of the value to validate
  5024. * @val: the precomputed value
  5025. * @ws: the whitespace type of the value
  5026. *
  5027. * Check a value against a facet condition
  5028. *
  5029. * Returns 0 if the element is schemas valid, a positive error code
  5030. * number otherwise and -1 in case of internal or API error.
  5031. */
  5032. static int
  5033. xmlSchemaValidateFacetInternal(xmlSchemaFacetPtr facet,
  5034. xmlSchemaWhitespaceValueType fws,
  5035. xmlSchemaValType valType,
  5036. const xmlChar *value,
  5037. xmlSchemaValPtr val,
  5038. xmlSchemaWhitespaceValueType ws)
  5039. {
  5040. int ret;
  5041. if (facet == NULL)
  5042. return(-1);
  5043. switch (facet->type) {
  5044. case XML_SCHEMA_FACET_PATTERN:
  5045. /*
  5046. * NOTE that for patterns, the @value needs to be the normalized
  5047. * value, *not* the lexical initial value or the canonical value.
  5048. */
  5049. if (value == NULL)
  5050. return(-1);
  5051. /*
  5052. * If string-derived type, regexp must be tested on the value space of
  5053. * the datatype.
  5054. * See https://www.w3.org/TR/xmlschema-2/#rf-pattern
  5055. */
  5056. if (val &&
  5057. val->value.str &&
  5058. ((val->type >= XML_SCHEMAS_STRING &&
  5059. val->type <= XML_SCHEMAS_NORMSTRING) ||
  5060. (val->type >= XML_SCHEMAS_TOKEN &&
  5061. val->type <= XML_SCHEMAS_ENTITIES &&
  5062. val->type != XML_SCHEMAS_QNAME))) {
  5063. value = val->value.str;
  5064. }
  5065. ret = xmlRegexpExec(facet->regexp, value);
  5066. if (ret == 1)
  5067. return(0);
  5068. if (ret == 0)
  5069. return(XML_SCHEMAV_CVC_PATTERN_VALID);
  5070. return(ret);
  5071. case XML_SCHEMA_FACET_MAXEXCLUSIVE:
  5072. ret = xmlSchemaCompareValues(val, facet->val);
  5073. if (ret == -2)
  5074. return(-1);
  5075. if (ret == -1)
  5076. return(0);
  5077. return(XML_SCHEMAV_CVC_MAXEXCLUSIVE_VALID);
  5078. case XML_SCHEMA_FACET_MAXINCLUSIVE:
  5079. ret = xmlSchemaCompareValues(val, facet->val);
  5080. if (ret == -2)
  5081. return(-1);
  5082. if ((ret == -1) || (ret == 0))
  5083. return(0);
  5084. return(XML_SCHEMAV_CVC_MAXINCLUSIVE_VALID);
  5085. case XML_SCHEMA_FACET_MINEXCLUSIVE:
  5086. ret = xmlSchemaCompareValues(val, facet->val);
  5087. if (ret == -2)
  5088. return(-1);
  5089. if (ret == 1)
  5090. return(0);
  5091. return(XML_SCHEMAV_CVC_MINEXCLUSIVE_VALID);
  5092. case XML_SCHEMA_FACET_MININCLUSIVE:
  5093. ret = xmlSchemaCompareValues(val, facet->val);
  5094. if (ret == -2)
  5095. return(-1);
  5096. if ((ret == 1) || (ret == 0))
  5097. return(0);
  5098. return(XML_SCHEMAV_CVC_MININCLUSIVE_VALID);
  5099. case XML_SCHEMA_FACET_WHITESPACE:
  5100. /* TODO whitespaces */
  5101. /*
  5102. * NOTE: Whitespace should be handled to normalize
  5103. * the value to be validated against a the facets;
  5104. * not to normalize the value in-between.
  5105. */
  5106. return(0);
  5107. case XML_SCHEMA_FACET_ENUMERATION:
  5108. if (ws == XML_SCHEMA_WHITESPACE_UNKNOWN) {
  5109. /*
  5110. * This is to ensure API compatibility with the old
  5111. * xmlSchemaValidateFacet().
  5112. * TODO: Get rid of this case.
  5113. */
  5114. if ((facet->value != NULL) &&
  5115. (xmlStrEqual(facet->value, value)))
  5116. return(0);
  5117. } else {
  5118. ret = xmlSchemaCompareValuesWhtspExt(facet->val->type,
  5119. facet->val, facet->value, fws, valType, val,
  5120. value, ws);
  5121. if (ret == -2)
  5122. return(-1);
  5123. if (ret == 0)
  5124. return(0);
  5125. }
  5126. return(XML_SCHEMAV_CVC_ENUMERATION_VALID);
  5127. case XML_SCHEMA_FACET_LENGTH:
  5128. /*
  5129. * SPEC (1.3) "if {primitive type definition} is QName or NOTATION,
  5130. * then any {value} is facet-valid."
  5131. */
  5132. if ((valType == XML_SCHEMAS_QNAME) ||
  5133. (valType == XML_SCHEMAS_NOTATION))
  5134. return (0);
  5135. /* Falls through. */
  5136. case XML_SCHEMA_FACET_MAXLENGTH:
  5137. case XML_SCHEMA_FACET_MINLENGTH: {
  5138. unsigned int len = 0;
  5139. if ((valType == XML_SCHEMAS_QNAME) ||
  5140. (valType == XML_SCHEMAS_NOTATION))
  5141. return (0);
  5142. /*
  5143. * TODO: length, maxLength and minLength must be of type
  5144. * nonNegativeInteger only. Check if decimal is used somehow.
  5145. */
  5146. if ((facet->val == NULL) ||
  5147. ((facet->val->type != XML_SCHEMAS_DECIMAL) &&
  5148. (facet->val->type != XML_SCHEMAS_NNINTEGER)) ||
  5149. (facet->val->value.decimal.frac != 0)) {
  5150. return(-1);
  5151. }
  5152. if ((val != NULL) && (val->type == XML_SCHEMAS_HEXBINARY))
  5153. len = val->value.hex.total;
  5154. else if ((val != NULL) && (val->type == XML_SCHEMAS_BASE64BINARY))
  5155. len = val->value.base64.total;
  5156. else {
  5157. switch (valType) {
  5158. case XML_SCHEMAS_STRING:
  5159. case XML_SCHEMAS_NORMSTRING:
  5160. if (ws == XML_SCHEMA_WHITESPACE_UNKNOWN) {
  5161. /*
  5162. * This is to ensure API compatibility with the old
  5163. * xmlSchemaValidateFacet(). Anyway, this was and
  5164. * is not the correct handling.
  5165. * TODO: Get rid of this case somehow.
  5166. */
  5167. if (valType == XML_SCHEMAS_STRING)
  5168. len = xmlUTF8Strlen(value);
  5169. else
  5170. len = xmlSchemaNormLen(value);
  5171. } else if (value != NULL) {
  5172. if (ws == XML_SCHEMA_WHITESPACE_COLLAPSE)
  5173. len = xmlSchemaNormLen(value);
  5174. else
  5175. /*
  5176. * Should be OK for "preserve" as well.
  5177. */
  5178. len = xmlUTF8Strlen(value);
  5179. }
  5180. break;
  5181. case XML_SCHEMAS_IDREF:
  5182. case XML_SCHEMAS_TOKEN:
  5183. case XML_SCHEMAS_LANGUAGE:
  5184. case XML_SCHEMAS_NMTOKEN:
  5185. case XML_SCHEMAS_NAME:
  5186. case XML_SCHEMAS_NCNAME:
  5187. case XML_SCHEMAS_ID:
  5188. case XML_SCHEMAS_ANYURI:
  5189. if (value != NULL)
  5190. len = xmlSchemaNormLen(value);
  5191. break;
  5192. default:
  5193. TODO
  5194. }
  5195. }
  5196. if (facet->type == XML_SCHEMA_FACET_LENGTH) {
  5197. if (len != facet->val->value.decimal.lo)
  5198. return(XML_SCHEMAV_CVC_LENGTH_VALID);
  5199. } else if (facet->type == XML_SCHEMA_FACET_MINLENGTH) {
  5200. if (len < facet->val->value.decimal.lo)
  5201. return(XML_SCHEMAV_CVC_MINLENGTH_VALID);
  5202. } else {
  5203. if (len > facet->val->value.decimal.lo)
  5204. return(XML_SCHEMAV_CVC_MAXLENGTH_VALID);
  5205. }
  5206. break;
  5207. }
  5208. case XML_SCHEMA_FACET_TOTALDIGITS:
  5209. case XML_SCHEMA_FACET_FRACTIONDIGITS:
  5210. if ((facet->val == NULL) ||
  5211. ((facet->val->type != XML_SCHEMAS_PINTEGER) &&
  5212. (facet->val->type != XML_SCHEMAS_NNINTEGER)) ||
  5213. (facet->val->value.decimal.frac != 0)) {
  5214. return(-1);
  5215. }
  5216. if ((val == NULL) ||
  5217. ((val->type != XML_SCHEMAS_DECIMAL) &&
  5218. (val->type != XML_SCHEMAS_INTEGER) &&
  5219. (val->type != XML_SCHEMAS_NPINTEGER) &&
  5220. (val->type != XML_SCHEMAS_NINTEGER) &&
  5221. (val->type != XML_SCHEMAS_NNINTEGER) &&
  5222. (val->type != XML_SCHEMAS_PINTEGER) &&
  5223. (val->type != XML_SCHEMAS_INT) &&
  5224. (val->type != XML_SCHEMAS_UINT) &&
  5225. (val->type != XML_SCHEMAS_LONG) &&
  5226. (val->type != XML_SCHEMAS_ULONG) &&
  5227. (val->type != XML_SCHEMAS_SHORT) &&
  5228. (val->type != XML_SCHEMAS_USHORT) &&
  5229. (val->type != XML_SCHEMAS_BYTE) &&
  5230. (val->type != XML_SCHEMAS_UBYTE))) {
  5231. return(-1);
  5232. }
  5233. if (facet->type == XML_SCHEMA_FACET_TOTALDIGITS) {
  5234. if (val->value.decimal.total > facet->val->value.decimal.lo)
  5235. return(XML_SCHEMAV_CVC_TOTALDIGITS_VALID);
  5236. } else if (facet->type == XML_SCHEMA_FACET_FRACTIONDIGITS) {
  5237. if (val->value.decimal.frac > facet->val->value.decimal.lo)
  5238. return(XML_SCHEMAV_CVC_FRACTIONDIGITS_VALID);
  5239. }
  5240. break;
  5241. default:
  5242. TODO
  5243. }
  5244. return(0);
  5245. }
  5246. /**
  5247. * xmlSchemaValidateFacet:
  5248. * @base: the base type
  5249. * @facet: the facet to check
  5250. * @value: the lexical repr of the value to validate
  5251. * @val: the precomputed value
  5252. *
  5253. * Check a value against a facet condition
  5254. *
  5255. * Returns 0 if the element is schemas valid, a positive error code
  5256. * number otherwise and -1 in case of internal or API error.
  5257. */
  5258. int
  5259. xmlSchemaValidateFacet(xmlSchemaTypePtr base,
  5260. xmlSchemaFacetPtr facet,
  5261. const xmlChar *value,
  5262. xmlSchemaValPtr val)
  5263. {
  5264. /*
  5265. * This tries to ensure API compatibility regarding the old
  5266. * xmlSchemaValidateFacet() and the new xmlSchemaValidateFacetInternal() and
  5267. * xmlSchemaValidateFacetWhtsp().
  5268. */
  5269. if (val != NULL)
  5270. return(xmlSchemaValidateFacetInternal(facet,
  5271. XML_SCHEMA_WHITESPACE_UNKNOWN, val->type, value, val,
  5272. XML_SCHEMA_WHITESPACE_UNKNOWN));
  5273. else if (base != NULL)
  5274. return(xmlSchemaValidateFacetInternal(facet,
  5275. XML_SCHEMA_WHITESPACE_UNKNOWN, base->builtInType, value, val,
  5276. XML_SCHEMA_WHITESPACE_UNKNOWN));
  5277. return(-1);
  5278. }
  5279. /**
  5280. * xmlSchemaValidateFacetWhtsp:
  5281. * @facet: the facet to check
  5282. * @fws: the whitespace type of the facet's value
  5283. * @valType: the built-in type of the value
  5284. * @value: the lexical (or normalized for pattern) repr of the value to validate
  5285. * @val: the precomputed value
  5286. * @ws: the whitespace type of the value
  5287. *
  5288. * Check a value against a facet condition. This takes value normalization
  5289. * according to the specified whitespace types into account.
  5290. * Note that @value needs to be the *normalized* value if the facet
  5291. * is of type "pattern".
  5292. *
  5293. * Returns 0 if the element is schemas valid, a positive error code
  5294. * number otherwise and -1 in case of internal or API error.
  5295. */
  5296. int
  5297. xmlSchemaValidateFacetWhtsp(xmlSchemaFacetPtr facet,
  5298. xmlSchemaWhitespaceValueType fws,
  5299. xmlSchemaValType valType,
  5300. const xmlChar *value,
  5301. xmlSchemaValPtr val,
  5302. xmlSchemaWhitespaceValueType ws)
  5303. {
  5304. return(xmlSchemaValidateFacetInternal(facet, fws, valType,
  5305. value, val, ws));
  5306. }
  5307. #if 0
  5308. #ifndef DBL_DIG
  5309. #define DBL_DIG 16
  5310. #endif
  5311. #ifndef DBL_EPSILON
  5312. #define DBL_EPSILON 1E-9
  5313. #endif
  5314. #define INTEGER_DIGITS DBL_DIG
  5315. #define FRACTION_DIGITS (DBL_DIG + 1)
  5316. #define EXPONENT_DIGITS (3 + 2)
  5317. /**
  5318. * xmlXPathFormatNumber:
  5319. * @number: number to format
  5320. * @buffer: output buffer
  5321. * @buffersize: size of output buffer
  5322. *
  5323. * Convert the number into a string representation.
  5324. */
  5325. static void
  5326. xmlSchemaFormatFloat(double number, char buffer[], int buffersize)
  5327. {
  5328. switch (xmlXPathIsInf(number)) {
  5329. case 1:
  5330. if (buffersize > (int)sizeof("INF"))
  5331. snprintf(buffer, buffersize, "INF");
  5332. break;
  5333. case -1:
  5334. if (buffersize > (int)sizeof("-INF"))
  5335. snprintf(buffer, buffersize, "-INF");
  5336. break;
  5337. default:
  5338. if (xmlXPathIsNaN(number)) {
  5339. if (buffersize > (int)sizeof("NaN"))
  5340. snprintf(buffer, buffersize, "NaN");
  5341. } else if (number == 0) {
  5342. snprintf(buffer, buffersize, "0.0E0");
  5343. } else {
  5344. /* 3 is sign, decimal point, and terminating zero */
  5345. char work[DBL_DIG + EXPONENT_DIGITS + 3];
  5346. int integer_place, fraction_place;
  5347. char *ptr;
  5348. char *after_fraction;
  5349. double absolute_value;
  5350. int size;
  5351. absolute_value = fabs(number);
  5352. /*
  5353. * Result is in work, and after_fraction points
  5354. * just past the fractional part.
  5355. * Use scientific notation
  5356. */
  5357. integer_place = DBL_DIG + EXPONENT_DIGITS + 1;
  5358. fraction_place = DBL_DIG - 1;
  5359. snprintf(work, sizeof(work),"%*.*e",
  5360. integer_place, fraction_place, number);
  5361. after_fraction = strchr(work + DBL_DIG, 'e');
  5362. /* Remove fractional trailing zeroes */
  5363. ptr = after_fraction;
  5364. while (*(--ptr) == '0')
  5365. ;
  5366. if (*ptr != '.')
  5367. ptr++;
  5368. while ((*ptr++ = *after_fraction++) != 0);
  5369. /* Finally copy result back to caller */
  5370. size = strlen(work) + 1;
  5371. if (size > buffersize) {
  5372. work[buffersize - 1] = 0;
  5373. size = buffersize;
  5374. }
  5375. memmove(buffer, work, size);
  5376. }
  5377. break;
  5378. }
  5379. }
  5380. #endif
  5381. /**
  5382. * xmlSchemaGetCanonValue:
  5383. * @val: the precomputed value
  5384. * @retValue: the returned value
  5385. *
  5386. * Get the canonical lexical representation of the value.
  5387. * The caller has to FREE the returned retValue.
  5388. *
  5389. * WARNING: Some value types are not supported yet, resulting
  5390. * in a @retValue of "???".
  5391. *
  5392. * TODO: XML Schema 1.0 does not define canonical representations
  5393. * for: duration, gYearMonth, gYear, gMonthDay, gMonth, gDay,
  5394. * anyURI, QName, NOTATION. This will be fixed in XML Schema 1.1.
  5395. *
  5396. *
  5397. * Returns 0 if the value could be built, 1 if the value type is
  5398. * not supported yet and -1 in case of API errors.
  5399. */
  5400. int
  5401. xmlSchemaGetCanonValue(xmlSchemaValPtr val, const xmlChar **retValue)
  5402. {
  5403. if ((retValue == NULL) || (val == NULL))
  5404. return (-1);
  5405. *retValue = NULL;
  5406. switch (val->type) {
  5407. case XML_SCHEMAS_STRING:
  5408. if (val->value.str == NULL)
  5409. *retValue = BAD_CAST xmlStrdup(BAD_CAST "");
  5410. else
  5411. *retValue =
  5412. BAD_CAST xmlStrdup((const xmlChar *) val->value.str);
  5413. break;
  5414. case XML_SCHEMAS_NORMSTRING:
  5415. if (val->value.str == NULL)
  5416. *retValue = BAD_CAST xmlStrdup(BAD_CAST "");
  5417. else {
  5418. *retValue = xmlSchemaWhiteSpaceReplace(
  5419. (const xmlChar *) val->value.str);
  5420. if ((*retValue) == NULL)
  5421. *retValue = BAD_CAST xmlStrdup(
  5422. (const xmlChar *) val->value.str);
  5423. }
  5424. break;
  5425. case XML_SCHEMAS_TOKEN:
  5426. case XML_SCHEMAS_LANGUAGE:
  5427. case XML_SCHEMAS_NMTOKEN:
  5428. case XML_SCHEMAS_NAME:
  5429. case XML_SCHEMAS_NCNAME:
  5430. case XML_SCHEMAS_ID:
  5431. case XML_SCHEMAS_IDREF:
  5432. case XML_SCHEMAS_ENTITY:
  5433. case XML_SCHEMAS_NOTATION: /* Unclear */
  5434. case XML_SCHEMAS_ANYURI: /* Unclear */
  5435. if (val->value.str == NULL)
  5436. return (-1);
  5437. *retValue =
  5438. BAD_CAST xmlSchemaCollapseString(BAD_CAST val->value.str);
  5439. if (*retValue == NULL)
  5440. *retValue =
  5441. BAD_CAST xmlStrdup((const xmlChar *) val->value.str);
  5442. break;
  5443. case XML_SCHEMAS_QNAME:
  5444. /* TODO: Unclear in XML Schema 1.0. */
  5445. if (val->value.qname.uri == NULL) {
  5446. *retValue = BAD_CAST xmlStrdup(BAD_CAST val->value.qname.name);
  5447. return (0);
  5448. } else {
  5449. *retValue = BAD_CAST xmlStrdup(BAD_CAST "{");
  5450. *retValue = BAD_CAST xmlStrcat((xmlChar *) (*retValue),
  5451. BAD_CAST val->value.qname.uri);
  5452. *retValue = BAD_CAST xmlStrcat((xmlChar *) (*retValue),
  5453. BAD_CAST "}");
  5454. *retValue = BAD_CAST xmlStrcat((xmlChar *) (*retValue),
  5455. BAD_CAST val->value.qname.uri);
  5456. }
  5457. break;
  5458. case XML_SCHEMAS_DECIMAL:
  5459. /*
  5460. * TODO: Lookout for a more simple implementation.
  5461. */
  5462. if ((val->value.decimal.total == 1) &&
  5463. (val->value.decimal.lo == 0)) {
  5464. *retValue = xmlStrdup(BAD_CAST "0.0");
  5465. } else {
  5466. xmlSchemaValDecimal dec = val->value.decimal;
  5467. int bufsize;
  5468. char *buf = NULL, *offs;
  5469. /* Add room for the decimal point as well. */
  5470. bufsize = dec.total + 2;
  5471. if (dec.sign)
  5472. bufsize++;
  5473. /* Add room for leading/trailing zero. */
  5474. if ((dec.frac == 0) || (dec.frac == dec.total))
  5475. bufsize++;
  5476. buf = xmlMalloc(bufsize);
  5477. if (buf == NULL)
  5478. return(-1);
  5479. offs = buf;
  5480. if (dec.sign)
  5481. *offs++ = '-';
  5482. if (dec.frac == dec.total) {
  5483. *offs++ = '0';
  5484. *offs++ = '.';
  5485. }
  5486. if (dec.hi != 0)
  5487. snprintf(offs, bufsize - (offs - buf),
  5488. "%lu%lu%lu", dec.hi, dec.mi, dec.lo);
  5489. else if (dec.mi != 0)
  5490. snprintf(offs, bufsize - (offs - buf),
  5491. "%lu%lu", dec.mi, dec.lo);
  5492. else
  5493. snprintf(offs, bufsize - (offs - buf),
  5494. "%lu", dec.lo);
  5495. if (dec.frac != 0) {
  5496. if (dec.frac != dec.total) {
  5497. int diff = dec.total - dec.frac;
  5498. /*
  5499. * Insert the decimal point.
  5500. */
  5501. memmove(offs + diff + 1, offs + diff, dec.frac +1);
  5502. offs[diff] = '.';
  5503. } else {
  5504. unsigned int i = 0;
  5505. /*
  5506. * Insert missing zeroes behind the decimal point.
  5507. */
  5508. while (*(offs + i) != 0)
  5509. i++;
  5510. if (i < dec.total) {
  5511. memmove(offs + (dec.total - i), offs, i +1);
  5512. memset(offs, '0', dec.total - i);
  5513. }
  5514. }
  5515. } else {
  5516. /*
  5517. * Append decimal point and zero.
  5518. */
  5519. offs = buf + bufsize - 1;
  5520. *offs-- = 0;
  5521. *offs-- = '0';
  5522. *offs-- = '.';
  5523. }
  5524. *retValue = BAD_CAST buf;
  5525. }
  5526. break;
  5527. case XML_SCHEMAS_INTEGER:
  5528. case XML_SCHEMAS_PINTEGER:
  5529. case XML_SCHEMAS_NPINTEGER:
  5530. case XML_SCHEMAS_NINTEGER:
  5531. case XML_SCHEMAS_NNINTEGER:
  5532. case XML_SCHEMAS_LONG:
  5533. case XML_SCHEMAS_BYTE:
  5534. case XML_SCHEMAS_SHORT:
  5535. case XML_SCHEMAS_INT:
  5536. case XML_SCHEMAS_UINT:
  5537. case XML_SCHEMAS_ULONG:
  5538. case XML_SCHEMAS_USHORT:
  5539. case XML_SCHEMAS_UBYTE:
  5540. if ((val->value.decimal.total == 1) &&
  5541. (val->value.decimal.lo == 0))
  5542. *retValue = xmlStrdup(BAD_CAST "0");
  5543. else {
  5544. xmlSchemaValDecimal dec = val->value.decimal;
  5545. int bufsize = dec.total + 1;
  5546. /* Add room for the decimal point as well. */
  5547. if (dec.sign)
  5548. bufsize++;
  5549. *retValue = xmlMalloc(bufsize);
  5550. if (*retValue == NULL)
  5551. return(-1);
  5552. if (dec.hi != 0) {
  5553. if (dec.sign)
  5554. snprintf((char *) *retValue, bufsize,
  5555. "-%lu%lu%lu", dec.hi, dec.mi, dec.lo);
  5556. else
  5557. snprintf((char *) *retValue, bufsize,
  5558. "%lu%lu%lu", dec.hi, dec.mi, dec.lo);
  5559. } else if (dec.mi != 0) {
  5560. if (dec.sign)
  5561. snprintf((char *) *retValue, bufsize,
  5562. "-%lu%lu", dec.mi, dec.lo);
  5563. else
  5564. snprintf((char *) *retValue, bufsize,
  5565. "%lu%lu", dec.mi, dec.lo);
  5566. } else {
  5567. if (dec.sign)
  5568. snprintf((char *) *retValue, bufsize, "-%lu", dec.lo);
  5569. else
  5570. snprintf((char *) *retValue, bufsize, "%lu", dec.lo);
  5571. }
  5572. }
  5573. break;
  5574. case XML_SCHEMAS_BOOLEAN:
  5575. if (val->value.b)
  5576. *retValue = BAD_CAST xmlStrdup(BAD_CAST "true");
  5577. else
  5578. *retValue = BAD_CAST xmlStrdup(BAD_CAST "false");
  5579. break;
  5580. case XML_SCHEMAS_DURATION: {
  5581. char buf[100];
  5582. unsigned long year;
  5583. unsigned long mon, day, hour = 0, min = 0;
  5584. double sec = 0, left;
  5585. /* TODO: Unclear in XML Schema 1.0 */
  5586. /*
  5587. * TODO: This results in a normalized output of the value
  5588. * - which is NOT conformant to the spec -
  5589. * since the exact values of each property are not
  5590. * recoverable. Think about extending the structure to
  5591. * provide a field for every property.
  5592. */
  5593. year = (unsigned long) FQUOTIENT(labs(val->value.dur.mon), 12);
  5594. mon = labs(val->value.dur.mon) - 12 * year;
  5595. day = (unsigned long) FQUOTIENT(fabs(val->value.dur.sec), 86400);
  5596. left = fabs(val->value.dur.sec) - day * 86400;
  5597. if (left > 0) {
  5598. hour = (unsigned long) FQUOTIENT(left, 3600);
  5599. left = left - (hour * 3600);
  5600. if (left > 0) {
  5601. min = (unsigned long) FQUOTIENT(left, 60);
  5602. sec = left - (min * 60);
  5603. }
  5604. }
  5605. if ((val->value.dur.mon < 0) || (val->value.dur.sec < 0))
  5606. snprintf(buf, 100, "P%luY%luM%luDT%luH%luM%.14gS",
  5607. year, mon, day, hour, min, sec);
  5608. else
  5609. snprintf(buf, 100, "-P%luY%luM%luDT%luH%luM%.14gS",
  5610. year, mon, day, hour, min, sec);
  5611. *retValue = BAD_CAST xmlStrdup(BAD_CAST buf);
  5612. }
  5613. break;
  5614. case XML_SCHEMAS_GYEAR: {
  5615. char buf[30];
  5616. /* TODO: Unclear in XML Schema 1.0 */
  5617. /* TODO: What to do with the timezone? */
  5618. snprintf(buf, 30, "%04ld", val->value.date.year);
  5619. *retValue = BAD_CAST xmlStrdup(BAD_CAST buf);
  5620. }
  5621. break;
  5622. case XML_SCHEMAS_GMONTH: {
  5623. /* TODO: Unclear in XML Schema 1.0 */
  5624. /* TODO: What to do with the timezone? */
  5625. *retValue = xmlMalloc(6);
  5626. if (*retValue == NULL)
  5627. return(-1);
  5628. snprintf((char *) *retValue, 6, "--%02u",
  5629. val->value.date.mon);
  5630. }
  5631. break;
  5632. case XML_SCHEMAS_GDAY: {
  5633. /* TODO: Unclear in XML Schema 1.0 */
  5634. /* TODO: What to do with the timezone? */
  5635. *retValue = xmlMalloc(6);
  5636. if (*retValue == NULL)
  5637. return(-1);
  5638. snprintf((char *) *retValue, 6, "---%02u",
  5639. val->value.date.day);
  5640. }
  5641. break;
  5642. case XML_SCHEMAS_GMONTHDAY: {
  5643. /* TODO: Unclear in XML Schema 1.0 */
  5644. /* TODO: What to do with the timezone? */
  5645. *retValue = xmlMalloc(8);
  5646. if (*retValue == NULL)
  5647. return(-1);
  5648. snprintf((char *) *retValue, 8, "--%02u-%02u",
  5649. val->value.date.mon, val->value.date.day);
  5650. }
  5651. break;
  5652. case XML_SCHEMAS_GYEARMONTH: {
  5653. char buf[35];
  5654. /* TODO: Unclear in XML Schema 1.0 */
  5655. /* TODO: What to do with the timezone? */
  5656. if (val->value.date.year < 0)
  5657. snprintf(buf, 35, "-%04ld-%02u",
  5658. labs(val->value.date.year),
  5659. val->value.date.mon);
  5660. else
  5661. snprintf(buf, 35, "%04ld-%02u",
  5662. val->value.date.year, val->value.date.mon);
  5663. *retValue = BAD_CAST xmlStrdup(BAD_CAST buf);
  5664. }
  5665. break;
  5666. case XML_SCHEMAS_TIME:
  5667. {
  5668. char buf[30];
  5669. if (val->value.date.tz_flag) {
  5670. xmlSchemaValPtr norm;
  5671. norm = xmlSchemaDateNormalize(val, 0);
  5672. if (norm == NULL)
  5673. return (-1);
  5674. /*
  5675. * TODO: Check if "%.14g" is portable.
  5676. */
  5677. snprintf(buf, 30,
  5678. "%02u:%02u:%02.14gZ",
  5679. norm->value.date.hour,
  5680. norm->value.date.min,
  5681. norm->value.date.sec);
  5682. xmlSchemaFreeValue(norm);
  5683. } else {
  5684. snprintf(buf, 30,
  5685. "%02u:%02u:%02.14g",
  5686. val->value.date.hour,
  5687. val->value.date.min,
  5688. val->value.date.sec);
  5689. }
  5690. *retValue = BAD_CAST xmlStrdup(BAD_CAST buf);
  5691. }
  5692. break;
  5693. case XML_SCHEMAS_DATE:
  5694. {
  5695. char buf[30];
  5696. if (val->value.date.tz_flag) {
  5697. xmlSchemaValPtr norm;
  5698. norm = xmlSchemaDateNormalize(val, 0);
  5699. if (norm == NULL)
  5700. return (-1);
  5701. /*
  5702. * TODO: Append the canonical value of the
  5703. * recoverable timezone and not "Z".
  5704. */
  5705. snprintf(buf, 30,
  5706. "%04ld-%02u-%02uZ",
  5707. norm->value.date.year, norm->value.date.mon,
  5708. norm->value.date.day);
  5709. xmlSchemaFreeValue(norm);
  5710. } else {
  5711. snprintf(buf, 30,
  5712. "%04ld-%02u-%02u",
  5713. val->value.date.year, val->value.date.mon,
  5714. val->value.date.day);
  5715. }
  5716. *retValue = BAD_CAST xmlStrdup(BAD_CAST buf);
  5717. }
  5718. break;
  5719. case XML_SCHEMAS_DATETIME:
  5720. {
  5721. char buf[50];
  5722. if (val->value.date.tz_flag) {
  5723. xmlSchemaValPtr norm;
  5724. norm = xmlSchemaDateNormalize(val, 0);
  5725. if (norm == NULL)
  5726. return (-1);
  5727. /*
  5728. * TODO: Check if "%.14g" is portable.
  5729. */
  5730. snprintf(buf, 50,
  5731. "%04ld-%02u-%02uT%02u:%02u:%02.14gZ",
  5732. norm->value.date.year, norm->value.date.mon,
  5733. norm->value.date.day, norm->value.date.hour,
  5734. norm->value.date.min, norm->value.date.sec);
  5735. xmlSchemaFreeValue(norm);
  5736. } else {
  5737. snprintf(buf, 50,
  5738. "%04ld-%02u-%02uT%02u:%02u:%02.14g",
  5739. val->value.date.year, val->value.date.mon,
  5740. val->value.date.day, val->value.date.hour,
  5741. val->value.date.min, val->value.date.sec);
  5742. }
  5743. *retValue = BAD_CAST xmlStrdup(BAD_CAST buf);
  5744. }
  5745. break;
  5746. case XML_SCHEMAS_HEXBINARY:
  5747. *retValue = BAD_CAST xmlStrdup(BAD_CAST val->value.hex.str);
  5748. break;
  5749. case XML_SCHEMAS_BASE64BINARY:
  5750. /*
  5751. * TODO: Is the following spec piece implemented?:
  5752. * SPEC: "Note: For some values the canonical form defined
  5753. * above does not conform to [RFC 2045], which requires breaking
  5754. * with linefeeds at appropriate intervals."
  5755. */
  5756. *retValue = BAD_CAST xmlStrdup(BAD_CAST val->value.base64.str);
  5757. break;
  5758. case XML_SCHEMAS_FLOAT: {
  5759. char buf[30];
  5760. /*
  5761. * |m| < 16777216, -149 <= e <= 104.
  5762. * TODO: Handle, NaN, INF, -INF. The format is not
  5763. * yet conformant. The c type float does not cover
  5764. * the whole range.
  5765. */
  5766. snprintf(buf, 30, "%01.14e", val->value.f);
  5767. *retValue = BAD_CAST xmlStrdup(BAD_CAST buf);
  5768. }
  5769. break;
  5770. case XML_SCHEMAS_DOUBLE: {
  5771. char buf[40];
  5772. /* |m| < 9007199254740992, -1075 <= e <= 970 */
  5773. /*
  5774. * TODO: Handle, NaN, INF, -INF. The format is not
  5775. * yet conformant. The c type float does not cover
  5776. * the whole range.
  5777. */
  5778. snprintf(buf, 40, "%01.14e", val->value.d);
  5779. *retValue = BAD_CAST xmlStrdup(BAD_CAST buf);
  5780. }
  5781. break;
  5782. default:
  5783. *retValue = BAD_CAST xmlStrdup(BAD_CAST "???");
  5784. return (1);
  5785. }
  5786. if (*retValue == NULL)
  5787. return(-1);
  5788. return (0);
  5789. }
  5790. /**
  5791. * xmlSchemaGetCanonValueWhtsp:
  5792. * @val: the precomputed value
  5793. * @retValue: the returned value
  5794. * @ws: the whitespace type of the value
  5795. *
  5796. * Get the canonical representation of the value.
  5797. * The caller has to free the returned @retValue.
  5798. *
  5799. * Returns 0 if the value could be built, 1 if the value type is
  5800. * not supported yet and -1 in case of API errors.
  5801. */
  5802. int
  5803. xmlSchemaGetCanonValueWhtsp(xmlSchemaValPtr val,
  5804. const xmlChar **retValue,
  5805. xmlSchemaWhitespaceValueType ws)
  5806. {
  5807. if ((retValue == NULL) || (val == NULL))
  5808. return (-1);
  5809. if ((ws == XML_SCHEMA_WHITESPACE_UNKNOWN) ||
  5810. (ws > XML_SCHEMA_WHITESPACE_COLLAPSE))
  5811. return (-1);
  5812. *retValue = NULL;
  5813. switch (val->type) {
  5814. case XML_SCHEMAS_STRING:
  5815. if (val->value.str == NULL)
  5816. *retValue = BAD_CAST xmlStrdup(BAD_CAST "");
  5817. else if (ws == XML_SCHEMA_WHITESPACE_COLLAPSE)
  5818. *retValue = xmlSchemaCollapseString(val->value.str);
  5819. else if (ws == XML_SCHEMA_WHITESPACE_REPLACE)
  5820. *retValue = xmlSchemaWhiteSpaceReplace(val->value.str);
  5821. if ((*retValue) == NULL)
  5822. *retValue = BAD_CAST xmlStrdup(val->value.str);
  5823. break;
  5824. case XML_SCHEMAS_NORMSTRING:
  5825. if (val->value.str == NULL)
  5826. *retValue = BAD_CAST xmlStrdup(BAD_CAST "");
  5827. else {
  5828. if (ws == XML_SCHEMA_WHITESPACE_COLLAPSE)
  5829. *retValue = xmlSchemaCollapseString(val->value.str);
  5830. else
  5831. *retValue = xmlSchemaWhiteSpaceReplace(val->value.str);
  5832. if ((*retValue) == NULL)
  5833. *retValue = BAD_CAST xmlStrdup(val->value.str);
  5834. }
  5835. break;
  5836. default:
  5837. return (xmlSchemaGetCanonValue(val, retValue));
  5838. }
  5839. return (0);
  5840. }
  5841. /**
  5842. * xmlSchemaGetValType:
  5843. * @val: a schemas value
  5844. *
  5845. * Accessor for the type of a value
  5846. *
  5847. * Returns the xmlSchemaValType of the value
  5848. */
  5849. xmlSchemaValType
  5850. xmlSchemaGetValType(xmlSchemaValPtr val)
  5851. {
  5852. if (val == NULL)
  5853. return(XML_SCHEMAS_UNKNOWN);
  5854. return (val->type);
  5855. }
  5856. #define bottom_xmlschemastypes
  5857. #include "elfgcchack.h"
  5858. #endif /* LIBXML_SCHEMAS_ENABLED */