xmlschemastypes.c 173 KB

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