stdsoap2.cpp 706 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244172451724617247172481724917250172511725217253172541725517256172571725817259172601726117262172631726417265172661726717268172691727017271172721727317274172751727617277172781727917280172811728217283172841728517286172871728817289172901729117292172931729417295172961729717298172991730017301173021730317304173051730617307173081730917310173111731217313173141731517316173171731817319173201732117322173231732417325173261732717328173291733017331173321733317334173351733617337173381733917340173411734217343173441734517346173471734817349173501735117352173531735417355173561735717358173591736017361173621736317364173651736617367173681736917370173711737217373173741737517376173771737817379173801738117382173831738417385173861738717388173891739017391173921739317394173951739617397173981739917400174011740217403174041740517406174071740817409174101741117412174131741417415174161741717418174191742017421174221742317424174251742617427174281742917430174311743217433174341743517436174371743817439174401744117442174431744417445174461744717448174491745017451174521745317454174551745617457174581745917460174611746217463174641746517466174671746817469174701747117472174731747417475174761747717478174791748017481174821748317484174851748617487174881748917490174911749217493174941749517496174971749817499175001750117502175031750417505175061750717508175091751017511175121751317514175151751617517175181751917520175211752217523175241752517526175271752817529175301753117532175331753417535175361753717538175391754017541175421754317544175451754617547175481754917550175511755217553175541755517556175571755817559175601756117562175631756417565175661756717568175691757017571175721757317574175751757617577175781757917580175811758217583175841758517586175871758817589175901759117592175931759417595175961759717598175991760017601176021760317604176051760617607176081760917610176111761217613176141761517616176171761817619176201762117622176231762417625176261762717628176291763017631176321763317634176351763617637176381763917640176411764217643176441764517646176471764817649176501765117652176531765417655176561765717658176591766017661176621766317664176651766617667176681766917670176711767217673176741767517676176771767817679176801768117682176831768417685176861768717688176891769017691176921769317694176951769617697176981769917700177011770217703177041770517706177071770817709177101771117712177131771417715177161771717718177191772017721177221772317724177251772617727177281772917730177311773217733177341773517736177371773817739177401774117742177431774417745177461774717748177491775017751177521775317754177551775617757177581775917760177611776217763177641776517766177671776817769177701777117772177731777417775177761777717778177791778017781177821778317784177851778617787177881778917790177911779217793177941779517796177971779817799178001780117802178031780417805178061780717808178091781017811178121781317814178151781617817178181781917820178211782217823178241782517826178271782817829178301783117832178331783417835178361783717838178391784017841178421784317844178451784617847178481784917850178511785217853178541785517856178571785817859178601786117862178631786417865178661786717868178691787017871178721787317874178751787617877178781787917880178811788217883178841788517886178871788817889178901789117892178931789417895178961789717898178991790017901179021790317904179051790617907179081790917910179111791217913179141791517916179171791817919179201792117922179231792417925179261792717928179291793017931179321793317934179351793617937179381793917940179411794217943179441794517946179471794817949179501795117952179531795417955179561795717958179591796017961179621796317964179651796617967179681796917970179711797217973179741797517976179771797817979179801798117982179831798417985179861798717988179891799017991179921799317994179951799617997179981799918000180011800218003180041800518006180071800818009180101801118012180131801418015180161801718018180191802018021180221802318024180251802618027180281802918030180311803218033180341803518036180371803818039180401804118042180431804418045180461804718048180491805018051180521805318054180551805618057180581805918060180611806218063180641806518066180671806818069180701807118072180731807418075180761807718078180791808018081180821808318084180851808618087180881808918090180911809218093180941809518096180971809818099181001810118102181031810418105181061810718108181091811018111181121811318114181151811618117181181811918120181211812218123181241812518126181271812818129181301813118132181331813418135181361813718138181391814018141181421814318144181451814618147181481814918150181511815218153181541815518156181571815818159181601816118162181631816418165181661816718168181691817018171181721817318174181751817618177181781817918180181811818218183181841818518186181871818818189181901819118192181931819418195181961819718198181991820018201182021820318204182051820618207182081820918210182111821218213182141821518216182171821818219182201822118222182231822418225182261822718228182291823018231182321823318234182351823618237182381823918240182411824218243182441824518246182471824818249182501825118252182531825418255182561825718258182591826018261182621826318264182651826618267182681826918270182711827218273182741827518276182771827818279182801828118282182831828418285182861828718288182891829018291182921829318294182951829618297182981829918300183011830218303183041830518306183071830818309183101831118312183131831418315183161831718318183191832018321183221832318324183251832618327183281832918330183311833218333183341833518336183371833818339183401834118342183431834418345183461834718348183491835018351183521835318354183551835618357183581835918360183611836218363183641836518366183671836818369183701837118372183731837418375183761837718378183791838018381183821838318384183851838618387183881838918390183911839218393183941839518396183971839818399184001840118402184031840418405184061840718408184091841018411184121841318414184151841618417184181841918420184211842218423184241842518426184271842818429184301843118432184331843418435184361843718438184391844018441184421844318444184451844618447184481844918450184511845218453184541845518456184571845818459184601846118462184631846418465184661846718468184691847018471184721847318474184751847618477184781847918480184811848218483184841848518486184871848818489184901849118492184931849418495184961849718498184991850018501185021850318504185051850618507185081850918510185111851218513185141851518516185171851818519185201852118522185231852418525185261852718528185291853018531185321853318534185351853618537185381853918540185411854218543185441854518546185471854818549185501855118552185531855418555185561855718558185591856018561185621856318564185651856618567185681856918570185711857218573185741857518576185771857818579185801858118582185831858418585185861858718588185891859018591185921859318594185951859618597185981859918600186011860218603186041860518606186071860818609186101861118612186131861418615186161861718618186191862018621186221862318624186251862618627186281862918630186311863218633186341863518636186371863818639186401864118642186431864418645186461864718648186491865018651186521865318654186551865618657186581865918660186611866218663186641866518666186671866818669186701867118672186731867418675186761867718678186791868018681186821868318684186851868618687186881868918690186911869218693186941869518696186971869818699187001870118702187031870418705187061870718708187091871018711187121871318714187151871618717187181871918720187211872218723187241872518726187271872818729187301873118732187331873418735187361873718738187391874018741187421874318744187451874618747187481874918750187511875218753187541875518756187571875818759187601876118762187631876418765187661876718768187691877018771187721877318774187751877618777187781877918780187811878218783187841878518786187871878818789187901879118792187931879418795187961879718798187991880018801188021880318804188051880618807188081880918810188111881218813188141881518816188171881818819188201882118822188231882418825188261882718828188291883018831188321883318834188351883618837188381883918840188411884218843188441884518846188471884818849188501885118852188531885418855188561885718858188591886018861188621886318864188651886618867188681886918870188711887218873188741887518876188771887818879188801888118882188831888418885188861888718888188891889018891188921889318894188951889618897188981889918900189011890218903189041890518906189071890818909189101891118912189131891418915189161891718918189191892018921189221892318924189251892618927189281892918930189311893218933189341893518936189371893818939189401894118942189431894418945189461894718948189491895018951189521895318954189551895618957189581895918960189611896218963189641896518966189671896818969189701897118972189731897418975189761897718978189791898018981189821898318984189851898618987189881898918990189911899218993189941899518996189971899818999190001900119002190031900419005190061900719008190091901019011190121901319014190151901619017190181901919020190211902219023190241902519026190271902819029190301903119032190331903419035190361903719038190391904019041190421904319044190451904619047190481904919050190511905219053190541905519056190571905819059190601906119062190631906419065190661906719068190691907019071190721907319074190751907619077190781907919080190811908219083190841908519086190871908819089190901909119092190931909419095190961909719098190991910019101191021910319104191051910619107191081910919110191111911219113191141911519116191171911819119191201912119122191231912419125191261912719128191291913019131191321913319134191351913619137191381913919140191411914219143191441914519146191471914819149191501915119152191531915419155191561915719158191591916019161191621916319164191651916619167191681916919170191711917219173191741917519176191771917819179191801918119182191831918419185191861918719188191891919019191191921919319194191951919619197191981919919200192011920219203192041920519206192071920819209192101921119212192131921419215192161921719218192191922019221192221922319224192251922619227192281922919230192311923219233192341923519236192371923819239192401924119242192431924419245192461924719248192491925019251192521925319254192551925619257192581925919260192611926219263192641926519266192671926819269192701927119272192731927419275192761927719278192791928019281192821928319284192851928619287192881928919290192911929219293192941929519296192971929819299193001930119302193031930419305193061930719308193091931019311193121931319314193151931619317193181931919320193211932219323193241932519326193271932819329193301933119332193331933419335193361933719338193391934019341193421934319344193451934619347193481934919350193511935219353193541935519356193571935819359193601936119362193631936419365193661936719368193691937019371193721937319374193751937619377193781937919380193811938219383193841938519386193871938819389193901939119392193931939419395193961939719398193991940019401194021940319404194051940619407194081940919410194111941219413194141941519416194171941819419194201942119422194231942419425194261942719428194291943019431194321943319434194351943619437194381943919440194411944219443194441944519446194471944819449194501945119452194531945419455194561945719458194591946019461194621946319464194651946619467194681946919470194711947219473194741947519476194771947819479194801948119482194831948419485194861948719488194891949019491194921949319494194951949619497194981949919500195011950219503195041950519506195071950819509195101951119512195131951419515195161951719518195191952019521195221952319524195251952619527195281952919530195311953219533195341953519536195371953819539195401954119542195431954419545195461954719548195491955019551195521955319554195551955619557195581955919560195611956219563195641956519566195671956819569195701957119572195731957419575195761957719578195791958019581195821958319584195851958619587195881958919590195911959219593195941959519596195971959819599196001960119602196031960419605196061960719608196091961019611196121961319614196151961619617196181961919620196211962219623196241962519626196271962819629196301963119632196331963419635196361963719638196391964019641196421964319644196451964619647196481964919650196511965219653196541965519656196571965819659196601966119662196631966419665196661966719668196691967019671196721967319674196751967619677196781967919680196811968219683196841968519686196871968819689196901969119692196931969419695196961969719698196991970019701197021970319704197051970619707197081970919710197111971219713197141971519716197171971819719197201972119722197231972419725197261972719728197291973019731197321973319734197351973619737197381973919740197411974219743197441974519746197471974819749197501975119752197531975419755197561975719758197591976019761197621976319764197651976619767197681976919770197711977219773197741977519776197771977819779197801978119782197831978419785197861978719788197891979019791197921979319794197951979619797197981979919800198011980219803198041980519806198071980819809198101981119812198131981419815198161981719818198191982019821198221982319824198251982619827198281982919830198311983219833198341983519836198371983819839198401984119842198431984419845198461984719848198491985019851198521985319854198551985619857198581985919860198611986219863198641986519866198671986819869198701987119872198731987419875198761987719878198791988019881198821988319884198851988619887198881988919890198911989219893198941989519896198971989819899199001990119902199031990419905199061990719908199091991019911199121991319914199151991619917199181991919920199211992219923199241992519926199271992819929199301993119932199331993419935199361993719938199391994019941199421994319944199451994619947199481994919950199511995219953199541995519956199571995819959199601996119962199631996419965199661996719968199691997019971199721997319974199751997619977199781997919980199811998219983199841998519986199871998819989199901999119992199931999419995199961999719998199992000020001200022000320004200052000620007200082000920010200112001220013200142001520016200172001820019200202002120022200232002420025200262002720028200292003020031200322003320034200352003620037200382003920040200412004220043200442004520046200472004820049200502005120052200532005420055200562005720058200592006020061200622006320064200652006620067200682006920070200712007220073200742007520076200772007820079200802008120082200832008420085200862008720088200892009020091200922009320094200952009620097200982009920100201012010220103201042010520106201072010820109201102011120112201132011420115201162011720118201192012020121201222012320124201252012620127201282012920130201312013220133201342013520136201372013820139201402014120142201432014420145201462014720148201492015020151201522015320154201552015620157201582015920160201612016220163201642016520166201672016820169201702017120172201732017420175201762017720178201792018020181201822018320184201852018620187201882018920190201912019220193201942019520196201972019820199202002020120202202032020420205202062020720208202092021020211202122021320214202152021620217202182021920220202212022220223202242022520226202272022820229202302023120232202332023420235202362023720238202392024020241202422024320244202452024620247202482024920250202512025220253202542025520256202572025820259202602026120262202632026420265202662026720268202692027020271202722027320274202752027620277202782027920280202812028220283202842028520286202872028820289202902029120292202932029420295202962029720298202992030020301203022030320304203052030620307203082030920310203112031220313203142031520316203172031820319203202032120322203232032420325203262032720328203292033020331203322033320334203352033620337203382033920340203412034220343203442034520346203472034820349203502035120352203532035420355203562035720358203592036020361203622036320364203652036620367203682036920370203712037220373203742037520376203772037820379203802038120382203832038420385203862038720388203892039020391203922039320394203952039620397203982039920400204012040220403204042040520406204072040820409204102041120412204132041420415204162041720418204192042020421204222042320424204252042620427204282042920430204312043220433204342043520436204372043820439204402044120442204432044420445204462044720448204492045020451204522045320454204552045620457204582045920460204612046220463204642046520466204672046820469204702047120472204732047420475204762047720478204792048020481204822048320484204852048620487204882048920490204912049220493204942049520496204972049820499205002050120502205032050420505205062050720508205092051020511205122051320514205152051620517205182051920520205212052220523205242052520526205272052820529205302053120532205332053420535205362053720538205392054020541205422054320544205452054620547205482054920550205512055220553205542055520556205572055820559205602056120562205632056420565205662056720568205692057020571205722057320574205752057620577205782057920580205812058220583205842058520586205872058820589205902059120592205932059420595205962059720598205992060020601206022060320604206052060620607206082060920610206112061220613206142061520616206172061820619206202062120622206232062420625206262062720628206292063020631206322063320634206352063620637206382063920640206412064220643206442064520646206472064820649206502065120652206532065420655206562065720658206592066020661206622066320664206652066620667206682066920670206712067220673206742067520676206772067820679206802068120682206832068420685206862068720688206892069020691206922069320694206952069620697206982069920700207012070220703207042070520706207072070820709207102071120712207132071420715207162071720718207192072020721207222072320724207252072620727207282072920730207312073220733207342073520736207372073820739207402074120742207432074420745207462074720748207492075020751207522075320754207552075620757207582075920760207612076220763207642076520766207672076820769207702077120772207732077420775207762077720778207792078020781207822078320784207852078620787207882078920790207912079220793207942079520796207972079820799208002080120802208032080420805208062080720808208092081020811208122081320814208152081620817208182081920820208212082220823208242082520826208272082820829208302083120832208332083420835208362083720838208392084020841208422084320844208452084620847208482084920850208512085220853208542085520856208572085820859208602086120862208632086420865208662086720868208692087020871208722087320874208752087620877208782087920880208812088220883208842088520886208872088820889208902089120892208932089420895208962089720898208992090020901209022090320904209052090620907209082090920910209112091220913209142091520916209172091820919209202092120922209232092420925209262092720928209292093020931209322093320934209352093620937209382093920940209412094220943209442094520946209472094820949209502095120952209532095420955209562095720958209592096020961209622096320964209652096620967209682096920970209712097220973209742097520976209772097820979209802098120982209832098420985209862098720988209892099020991209922099320994209952099620997209982099921000210012100221003210042100521006210072100821009210102101121012210132101421015210162101721018210192102021021210222102321024210252102621027210282102921030210312103221033210342103521036210372103821039210402104121042210432104421045210462104721048210492105021051210522105321054210552105621057210582105921060210612106221063210642106521066210672106821069210702107121072210732107421075210762107721078210792108021081210822108321084210852108621087210882108921090210912109221093210942109521096210972109821099211002110121102
  1. /*
  2. stdsoap2.c[pp] 2.8.83
  3. gSOAP runtime engine
  4. gSOAP XML Web services tools
  5. Copyright (C) 2000-2019, Robert van Engelen, Genivia Inc., All Rights Reserved.
  6. This part of the software is released under ONE of the following licenses:
  7. GPL, or the gSOAP public license, or Genivia's license for commercial use.
  8. --------------------------------------------------------------------------------
  9. Contributors:
  10. Wind River Systems, Inc., for the following addition licensed under the gSOAP
  11. public license:
  12. - vxWorks compatible, enabled with compiler option -DVXWORKS
  13. --------------------------------------------------------------------------------
  14. gSOAP public license.
  15. The contents of this file are subject to the gSOAP Public License Version 1.3
  16. (the "License"); you may not use this file except in compliance with the
  17. License. You may obtain a copy of the License at
  18. http://www.cs.fsu.edu/~engelen/soaplicense.html
  19. Software distributed under the License is distributed on an "AS IS" basis,
  20. WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  21. for the specific language governing rights and limitations under the License.
  22. The Initial Developer of the Original Code is Robert A. van Engelen.
  23. Copyright (C) 2000-2019, Robert van Engelen, Genivia Inc., All Rights Reserved.
  24. --------------------------------------------------------------------------------
  25. GPL license.
  26. This program is free software; you can redistribute it and/or modify it under
  27. the terms of the GNU General Public License as published by the Free Software
  28. Foundation; either version 2 of the License, or (at your option) any later
  29. version.
  30. This program is distributed in the hope that it will be useful, but WITHOUT ANY
  31. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
  32. PARTICULAR PURPOSE. See the GNU General Public License for more details.
  33. You should have received a copy of the GNU General Public License along with
  34. this program; if not, write to the Free Software Foundation, Inc., 59 Temple
  35. Place, Suite 330, Boston, MA 02111-1307 USA
  36. Author contact information:
  37. engelen@genivia.com / engelen@acm.org
  38. This program is released under the GPL with the additional exemption that
  39. compiling, linking, and/or using OpenSSL is allowed.
  40. --------------------------------------------------------------------------------
  41. A commercial use license is available from Genivia, Inc., contact@genivia.com
  42. --------------------------------------------------------------------------------
  43. */
  44. const char *debug;
  45. //std::cerr<<"Recieved data: s "<<soap->body<<std::endl;
  46. #define GSOAP_LIB_VERSION 20883
  47. #ifdef AS400
  48. # pragma convert(819) /* EBCDIC to ASCII */
  49. #endif
  50. #if defined(__gnu_linux__) && !defined(_GNU_SOURCE)
  51. # define _GNU_SOURCE 1
  52. #endif
  53. #include "stdsoap2.h"
  54. #if GSOAP_VERSION != GSOAP_LIB_VERSION
  55. # error "GSOAP VERSION MISMATCH IN LIBRARY: PLEASE REINSTALL PACKAGE"
  56. #endif
  57. #if defined(VXWORKS) && defined(WM_SECURE_KEY_STORAGE)
  58. # include <ipcom_key_db.h> /* vxWorks compatible */
  59. #endif
  60. #ifdef __BORLANDC__
  61. # pragma warn -8060
  62. #else
  63. # ifdef WIN32
  64. # ifdef UNDER_CE
  65. # pragma comment(lib, "ws2.lib") /* WinCE */
  66. # else
  67. # pragma comment(lib, "Ws2_32.lib")
  68. # endif
  69. # pragma warning(disable : 4996) /* disable deprecation warnings */
  70. # endif
  71. #endif
  72. #ifdef __cplusplus
  73. SOAP_SOURCE_STAMP("@(#) stdsoap2.cpp ver 2.8.83 2019-04-18 00:00:00 GMT")
  74. extern "C" {
  75. #else
  76. SOAP_SOURCE_STAMP("@(#) stdsoap2.c ver 2.8.83 2019-04-18 00:00:00 GMT")
  77. #endif
  78. /* 8bit character representing unknown character entity or multibyte data */
  79. #ifndef SOAP_UNKNOWN_CHAR
  80. # define SOAP_UNKNOWN_CHAR (0x7F)
  81. #endif
  82. /* unicode character representing unknown characters outside the XML 1.0 UTF8 unicode space */
  83. #ifdef WITH_REPLACE_ILLEGAL_UTF8
  84. # ifndef SOAP_UNKNOWN_UNICODE_CHAR
  85. # define SOAP_UNKNOWN_UNICODE_CHAR (0xFFFD)
  86. # endif
  87. #endif
  88. /* EOF=-1 */
  89. #define SOAP_LT (soap_wchar)(-2) /* XML-specific '<' */
  90. #define SOAP_TT (soap_wchar)(-3) /* XML-specific '</' */
  91. #define SOAP_GT (soap_wchar)(-4) /* XML-specific '>' */
  92. #define SOAP_QT (soap_wchar)(-5) /* XML-specific '"' */
  93. #define SOAP_AP (soap_wchar)(-6) /* XML-specific ''' */
  94. #define soap_coblank(c) ((c)+1 > 0 && (c) <= 32)
  95. #if defined(WIN32) && !defined(UNDER_CE)
  96. #define soap_hash_ptr(p) ((size_t)((PtrToUlong(p) >> 3) & (SOAP_PTRHASH - 1)))
  97. #else
  98. #define soap_hash_ptr(p) ((size_t)(((unsigned long)(p) >> 3) & (SOAP_PTRHASH-1)))
  99. #endif
  100. #ifdef SOAP_DEBUG
  101. static void soap_init_logs(struct soap*);
  102. static void soap_close_logfile(struct soap*, int);
  103. static void soap_set_logfile(struct soap*, int, const char*);
  104. #endif
  105. #ifdef SOAP_MEM_DEBUG
  106. static void soap_init_mht(struct soap *);
  107. static void soap_free_mht(struct soap *);
  108. static void soap_track_unlink(struct soap *, const void *);
  109. #endif
  110. static int soap_set_error(struct soap *, const char *, const char *, const char *, const char *, int);
  111. static int soap_copy_fault(struct soap *, const char *, const char *, const char *, const char *);
  112. static int soap_getattrval(struct soap *, char *, size_t *, soap_wchar);
  113. static void soap_version(struct soap *);
  114. static void soap_free_ns(struct soap *);
  115. static soap_wchar soap_char(struct soap *);
  116. static soap_wchar soap_getpi(struct soap *);
  117. static int soap_isxdigit(int);
  118. static void *fplugin(struct soap *, const char *);
  119. static ULONG64 soap_count_attachments(struct soap *);
  120. static int soap_try_connect_command(struct soap *, int http_command, const char *endpoint, const char *action);
  121. static int soap_init_send(struct soap *);
  122. #ifdef WITH_NTLM
  123. static int soap_ntlm_handshake(struct soap *soap, int command, const char *endpoint, const char *host, int port);
  124. #endif
  125. #ifndef WITH_NOIDREF
  126. static int soap_has_copies(struct soap *, const char *, const char *);
  127. static int soap_type_punned(struct soap *, const struct soap_ilist *);
  128. static int soap_is_shaky(struct soap *, void *);
  129. static void soap_init_iht(struct soap *);
  130. static void soap_free_iht(struct soap *);
  131. #endif
  132. static void soap_init_pht(struct soap *);
  133. static void soap_free_pht(struct soap *);
  134. #ifndef WITH_LEAN
  135. static const char *soap_set_validation_fault(struct soap *, const char *, const char *);
  136. static int soap_isnumeric(struct soap *, const char *);
  137. static struct soap_nlist *
  138. soap_push_ns(struct soap *soap, const char *id, const char *ns, short utilized, short isearly);
  139. static void soap_utilize_ns(struct soap *soap, const char *tag, short isearly);
  140. static const wchar_t *
  141. soap_wstring(struct soap *soap, const char *s, int flag, long minlen, long maxlen, const char *pattern);
  142. static wchar_t *soap_wcollapse(struct soap *soap, wchar_t *s, int flag, int insitu);
  143. #endif
  144. static const char *
  145. soap_string(struct soap *soap, const char *s, int flag, long minlen, long maxlen, const char *pattern);
  146. static char *soap_collapse(struct soap *soap, char *s, int flag, int insitu);
  147. static const char *soap_QName(struct soap *soap, const char *s, long minlen, long maxlen, const char *pattern);
  148. #ifndef WITH_LEANER
  149. static int soap_begin_attachments(struct soap *);
  150. static int soap_end_attachments(struct soap *soap);
  151. static struct soap_multipart *
  152. soap_alloc_multipart(struct soap *, struct soap_multipart **, struct soap_multipart **, const char *, size_t);
  153. static int soap_putdimefield(struct soap *, const char *, size_t);
  154. static char *soap_getdimefield(struct soap *, size_t);
  155. static void soap_select_mime_boundary(struct soap *);
  156. static int soap_valid_mime_boundary(struct soap *);
  157. static void soap_resolve_attachment(struct soap *, struct soap_multipart *);
  158. #endif
  159. #ifdef WITH_GZIP
  160. static int soap_getgziphdr(struct soap*);
  161. #endif
  162. #ifdef WITH_OPENSSL
  163. # ifndef SOAP_SSL_RSA_BITS
  164. # define SOAP_SSL_RSA_BITS 2048
  165. # endif
  166. static int soap_ssl_init_done = 0;
  167. static int ssl_auth_init(struct soap*);
  168. static int ssl_verify_callback(int, X509_STORE_CTX*);
  169. static int ssl_verify_callback_allow_expired_certificate(int, X509_STORE_CTX*);
  170. static int ssl_password(char*, int, int, void *);
  171. #endif
  172. #ifdef WITH_GNUTLS
  173. # ifndef SOAP_SSL_RSA_BITS
  174. # define SOAP_SSL_RSA_BITS 2048
  175. # endif
  176. static int soap_ssl_init_done = 0;
  177. static int ssl_auth_init(struct soap*);
  178. static const char *ssl_verify(struct soap *soap, const char *host);
  179. # if GNUTLS_VERSION_NUMBER < 0x020b00
  180. # if defined(HAVE_PTHREAD_H)
  181. # include <pthread.h>
  182. /* make GNUTLS thread safe with pthreads */
  183. GCRY_THREAD_OPTION_PTHREAD_IMPL;
  184. # elif defined(HAVE_PTH_H)
  185. #include <pth.h>
  186. /* make GNUTLS thread safe with PTH */
  187. GCRY_THREAD_OPTION_PTH_IMPL;
  188. # endif
  189. # endif
  190. #endif
  191. #ifdef WITH_SYSTEMSSL
  192. static int ssl_auth_init(struct soap*);
  193. static int ssl_recv(int sk, void *s, int n, char *user);
  194. static int ssl_send(int sk, void *s, int n, char *user);
  195. #endif
  196. #if !defined(WITH_NOHTTP) || !defined(WITH_LEANER)
  197. static const char *soap_decode(char *, size_t, const char *, const char *);
  198. #endif
  199. #ifndef WITH_NOHTTP
  200. static soap_wchar soap_getchunkchar(struct soap *);
  201. static const char *http_error(struct soap *, int);
  202. static int http_get(struct soap *);
  203. static int http_put(struct soap *);
  204. static int http_patch(struct soap *);
  205. static int http_del(struct soap *);
  206. static int http_200(struct soap *);
  207. static int http_post(struct soap *, const char *, const char *, int, const char *, const char *, ULONG64);
  208. static int http_send_header(struct soap *, const char *);
  209. static int http_post_header(struct soap *, const char *, const char *);
  210. static int http_response(struct soap *, int, ULONG64);
  211. static int http_parse(struct soap *);
  212. static int http_parse_header(struct soap *, const char *, const char *);
  213. #endif
  214. #ifndef WITH_NOIO
  215. static int fsend(struct soap *, const char *, size_t);
  216. static size_t frecv(struct soap *, char *, size_t);
  217. static int tcp_init(struct soap *);
  218. static const char *tcp_error(struct soap *);
  219. #if !defined(WITH_IPV6)
  220. static int tcp_gethost(struct soap *, const char *addr, struct in_addr *inaddr);
  221. #endif
  222. #if !defined(WITH_IPV6) || defined(WITH_COOKIES)
  223. static int tcp_gethostbyname(struct soap *, const char *addr, struct hostent *hostent, struct in_addr *inaddr);
  224. #endif
  225. static SOAP_SOCKET tcp_connect(struct soap *, const char *endpoint, const char *host, int port);
  226. static SOAP_SOCKET tcp_accept(struct soap *, SOAP_SOCKET, struct sockaddr *, int *);
  227. static int tcp_select(struct soap *, SOAP_SOCKET, int, int);
  228. static int tcp_disconnect(struct soap *);
  229. static int tcp_closesocket(struct soap *, SOAP_SOCKET);
  230. static int tcp_shutdownsocket(struct soap *, SOAP_SOCKET, int);
  231. static const char *soap_strerror(struct soap *);
  232. #define SOAP_TCP_SELECT_RCV 0x1
  233. #define SOAP_TCP_SELECT_SND 0x2
  234. #define SOAP_TCP_SELECT_ERR 0x4
  235. #define SOAP_TCP_SELECT_ALL 0x7
  236. #define SOAP_TCP_SELECT_PIP 0x8
  237. #if defined(WIN32)
  238. #define SOAP_SOCKBLOCK(fd) \
  239. { \
  240. u_long blocking = 0; \
  241. ioctlsocket(fd, FIONBIO, &blocking); \
  242. }
  243. #define SOAP_SOCKNONBLOCK(fd) \
  244. { \
  245. u_long nonblocking = 1; \
  246. ioctlsocket(fd, FIONBIO, &nonblocking); \
  247. }
  248. #elif defined(VXWORKS)
  249. #define SOAP_SOCKBLOCK(fd) \
  250. { \
  251. u_long blocking = 0; \
  252. ioctl(fd, FIONBIO, (int)(&blocking)); \
  253. }
  254. #define SOAP_SOCKNONBLOCK(fd) \
  255. { \
  256. u_long nonblocking = 1; \
  257. ioctl(fd, FIONBIO, (int)(&nonblocking)); \
  258. }
  259. #elif defined(__VMS)
  260. #define SOAP_SOCKBLOCK(fd) \
  261. { \
  262. int blocking = 0; \
  263. ioctl(fd, FIONBIO, &blocking); \
  264. }
  265. #define SOAP_SOCKNONBLOCK(fd) \
  266. { \
  267. int nonblocking = 1; \
  268. ioctl(fd, FIONBIO, &nonblocking); \
  269. }
  270. #elif defined(SYMBIAN)
  271. #define SOAP_SOCKBLOCK(fd) \
  272. { \
  273. long blocking = 0; \
  274. ioctl(fd, 0/*FIONBIO*/, &blocking); \
  275. }
  276. #define SOAP_SOCKNONBLOCK(fd) \
  277. { \
  278. long nonblocking = 1; \
  279. ioctl(fd, 0/*FIONBIO*/, &nonblocking); \
  280. }
  281. #else
  282. #define SOAP_SOCKBLOCK(fd) fcntl(fd, F_SETFL, fcntl(fd, F_GETFL)&~O_NONBLOCK);
  283. #define SOAP_SOCKNONBLOCK(fd) fcntl(fd, F_SETFL, fcntl(fd, F_GETFL)|O_NONBLOCK);
  284. #endif
  285. #endif
  286. static const char soap_env1[42] = "http://schemas.xmlsoap.org/soap/envelope/";
  287. static const char soap_enc1[42] = "http://schemas.xmlsoap.org/soap/encoding/";
  288. static const char soap_env2[40] = "http://www.w3.org/2003/05/soap-envelope";
  289. static const char soap_enc2[40] = "http://www.w3.org/2003/05/soap-encoding";
  290. static const char soap_rpc[35] = "http://www.w3.org/2003/05/soap-rpc";
  291. const union soap_double_nan soap_double_nan = {{0xFFFFFFFF, 0xFFFFFFFF}};
  292. const char soap_base64o[65] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  293. const char soap_base64i[81] = "\76XXX\77\64\65\66\67\70\71\72\73\74\75XXXXXXX\00\01\02\03\04\05\06\07\10\11\12\13\14\15\16\17\20\21\22\23\24\25\26\27\30\31XXXXXX\32\33\34\35\36\37\40\41\42\43\44\45\46\47\50\51\52\53\54\55\56\57\60\61\62\63";
  294. #ifndef WITH_LEAN
  295. static const char soap_indent[21] = "\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t";
  296. /* Alternative indentation form for SOAP_XML_INDENT with spaces instead of tabs:
  297. static const char soap_indent[41] = "\n ";
  298. */
  299. #endif
  300. #ifndef SOAP_CANARY
  301. # define SOAP_CANARY (0xC0DE)
  302. #endif
  303. static const char soap_padding[4] = "\0\0\0";
  304. #define SOAP_STR_PADDING (soap_padding)
  305. #define SOAP_STR_EOS (soap_padding)
  306. #define SOAP_NON_NULL (soap_padding)
  307. #ifndef WITH_LEAN
  308. static const struct soap_code_map html_entity_codes[] = /* entities for XHTML parsing */
  309. {
  310. {160, "nbsp"},
  311. {161, "iexcl"},
  312. {162, "cent"},
  313. {163, "pound"},
  314. {164, "curren"},
  315. {165, "yen"},
  316. {166, "brvbar"},
  317. {167, "sect"},
  318. {168, "uml"},
  319. {169, "copy"},
  320. {170, "ordf"},
  321. {171, "laquo"},
  322. {172, "not"},
  323. {173, "shy"},
  324. {174, "reg"},
  325. {175, "macr"},
  326. {176, "deg"},
  327. {177, "plusmn"},
  328. {178, "sup2"},
  329. {179, "sup3"},
  330. {180, "acute"},
  331. {181, "micro"},
  332. {182, "para"},
  333. {183, "middot"},
  334. {184, "cedil"},
  335. {185, "sup1"},
  336. {186, "ordm"},
  337. {187, "raquo"},
  338. {188, "frac14"},
  339. {189, "frac12"},
  340. {190, "frac34"},
  341. {191, "iquest"},
  342. {192, "Agrave"},
  343. {193, "Aacute"},
  344. {194, "Acirc"},
  345. {195, "Atilde"},
  346. {196, "Auml"},
  347. {197, "Aring"},
  348. {198, "AElig"},
  349. {199, "Ccedil"},
  350. {200, "Egrave"},
  351. {201, "Eacute"},
  352. {202, "Ecirc"},
  353. {203, "Euml"},
  354. {204, "Igrave"},
  355. {205, "Iacute"},
  356. {206, "Icirc"},
  357. {207, "Iuml"},
  358. {208, "ETH"},
  359. {209, "Ntilde"},
  360. {210, "Ograve"},
  361. {211, "Oacute"},
  362. {212, "Ocirc"},
  363. {213, "Otilde"},
  364. {214, "Ouml"},
  365. {215, "times"},
  366. {216, "Oslash"},
  367. {217, "Ugrave"},
  368. {218, "Uacute"},
  369. {219, "Ucirc"},
  370. {220, "Uuml"},
  371. {221, "Yacute"},
  372. {222, "THORN"},
  373. {223, "szlig"},
  374. {224, "agrave"},
  375. {225, "aacute"},
  376. {226, "acirc"},
  377. {227, "atilde"},
  378. {228, "auml"},
  379. {229, "aring"},
  380. {230, "aelig"},
  381. {231, "ccedil"},
  382. {232, "egrave"},
  383. {233, "eacute"},
  384. {234, "ecirc"},
  385. {235, "euml"},
  386. {236, "igrave"},
  387. {237, "iacute"},
  388. {238, "icirc"},
  389. {239, "iuml"},
  390. {240, "eth"},
  391. {241, "ntilde"},
  392. {242, "ograve"},
  393. {243, "oacute"},
  394. {244, "ocirc"},
  395. {245, "otilde"},
  396. {246, "ouml"},
  397. {247, "divide"},
  398. {248, "oslash"},
  399. {249, "ugrave"},
  400. {250, "uacute"},
  401. {251, "ucirc"},
  402. {252, "uuml"},
  403. {253, "yacute"},
  404. {254, "thorn"},
  405. {255, "yuml"},
  406. {0, NULL}
  407. };
  408. #endif
  409. #ifndef WITH_NOIO
  410. #ifndef WITH_LEAN
  411. static const struct soap_code_map h_error_codes[] =
  412. {
  413. #ifdef HOST_NOT_FOUND
  414. {HOST_NOT_FOUND, "Host not found"},
  415. #endif
  416. #ifdef TRY_AGAIN
  417. {TRY_AGAIN, "Try Again"},
  418. #endif
  419. #ifdef NO_RECOVERY
  420. {NO_RECOVERY, "No Recovery"},
  421. #endif
  422. #ifdef NO_DATA
  423. {NO_DATA, "No Data"},
  424. #endif
  425. #ifdef NO_ADDRESS
  426. {NO_ADDRESS, "No Address"},
  427. #endif
  428. {0, NULL}
  429. };
  430. #endif
  431. #endif
  432. #ifndef WITH_NOHTTP
  433. #ifndef WITH_LEAN
  434. static const struct soap_code_map h_http_error_codes[] =
  435. {
  436. {100, "Continue"},
  437. {101, "Switching Protocols"},
  438. {200, "OK"},
  439. {201, "Created"},
  440. {202, "Accepted"},
  441. {203, "Non-Authoritative Information"},
  442. {204, "No Content"},
  443. {205, "Reset Content"},
  444. {206, "Partial Content"},
  445. {300, "Multiple Choices"},
  446. {301, "Moved Permanently"},
  447. {302, "Found"},
  448. {303, "See Other"},
  449. {304, "Not Modified"},
  450. {305, "Use Proxy"},
  451. {307, "Temporary Redirect"},
  452. {400, "Bad Request"},
  453. {401, "Unauthorized"},
  454. {402, "Payment Required"},
  455. {403, "Forbidden"},
  456. {404, "Not Found"},
  457. {405, "Method Not Allowed"},
  458. {406, "Not Acceptable"},
  459. {407, "Proxy Authentication Required"},
  460. {408, "Request Time-out"},
  461. {409, "Conflict"},
  462. {410, "Gone"},
  463. {411, "Length Required"},
  464. {412, "Precondition Failed"},
  465. {413, "Request Entity Too Large"},
  466. {414, "Request-URI Too Large"},
  467. {415, "Unsupported Media Type"},
  468. {416, "Requested range not satisfiable"},
  469. {417, "Expectation Failed"},
  470. {422, "Unprocessable Entity"},
  471. {426, "Upgrade Required"},
  472. {428, "Precondition Required"},
  473. {429, "Too Many Requests"},
  474. {431, "Request Header Fields Too Large"},
  475. {500, "Internal Server Error"},
  476. {501, "Not Implemented"},
  477. {502, "Bad Gateway"},
  478. {503, "Service Unavailable"},
  479. {504, "Gateway Time-out"},
  480. {505, "HTTP Version not supported"},
  481. {511, "Network Authentication Required"},
  482. {0, NULL}
  483. };
  484. #endif
  485. #endif
  486. #ifdef WITH_OPENSSL
  487. static const struct soap_code_map h_ssl_error_codes[] =
  488. {
  489. #define _SSL_ERROR(e) { e, #e }
  490. _SSL_ERROR(SSL_ERROR_SSL),
  491. _SSL_ERROR(SSL_ERROR_ZERO_RETURN),
  492. _SSL_ERROR(SSL_ERROR_WANT_READ),
  493. _SSL_ERROR(SSL_ERROR_WANT_WRITE),
  494. _SSL_ERROR(SSL_ERROR_WANT_CONNECT),
  495. _SSL_ERROR(SSL_ERROR_WANT_X509_LOOKUP),
  496. _SSL_ERROR(SSL_ERROR_SYSCALL),
  497. { 0, NULL }
  498. };
  499. #endif
  500. #ifndef WITH_LEANER
  501. static const struct soap_code_map mime_codes[] =
  502. {
  503. {SOAP_MIME_7BIT, "7bit"},
  504. {SOAP_MIME_8BIT, "8bit"},
  505. {SOAP_MIME_BINARY, "binary"},
  506. {SOAP_MIME_QUOTED_PRINTABLE, "quoted-printable"},
  507. {SOAP_MIME_BASE64, "base64"},
  508. {SOAP_MIME_IETF_TOKEN, "ietf-token"},
  509. {SOAP_MIME_X_TOKEN, "x-token"},
  510. {0, NULL}
  511. };
  512. #endif
  513. #ifdef WIN32
  514. static int tcp_done = 0;
  515. #endif
  516. #if (defined(_AIX43) || defined(TRU64) || defined(HP_UX)) && defined(HAVE_GETHOSTBYNAME_R)
  517. #ifndef h_errno
  518. extern int h_errno;
  519. #endif
  520. #endif
  521. /******************************************************************************/
  522. #ifndef WITH_NOIO
  523. static int
  524. fsend(struct soap *soap, const char *s, size_t n) {
  525. int nwritten, err;
  526. SOAP_SOCKET sk;
  527. soap->errnum = 0;
  528. #if defined(__cplusplus) && !defined(WITH_COMPAT)
  529. if (soap->os) {
  530. soap->os->write(s, (std::streamsize) n);
  531. if (soap->os->good())
  532. return SOAP_OK;
  533. return SOAP_EOF;
  534. }
  535. #endif
  536. sk = soap->sendsk;
  537. if (!soap_valid_socket(sk))
  538. sk = soap->socket;
  539. while (n) {
  540. if (soap_valid_socket(sk)) {
  541. if (soap->send_timeout) {
  542. for (;;) {
  543. int r;
  544. #ifdef WITH_SELF_PIPE
  545. #ifdef WITH_OPENSSL
  546. if (soap->ssl)
  547. r = tcp_select(soap, sk, SOAP_TCP_SELECT_ALL | SOAP_TCP_SELECT_PIP, soap->send_timeout);
  548. else
  549. #endif
  550. #ifdef WITH_GNUTLS
  551. if (soap->session)
  552. r = tcp_select(soap, sk, SOAP_TCP_SELECT_ALL | SOAP_TCP_SELECT_PIP, soap->send_timeout);
  553. else
  554. #endif
  555. #ifdef WITH_SYSTEMSSL
  556. if (soap->ssl)
  557. r = tcp_select(soap, sk, SOAP_TCP_SELECT_ALL | SOAP_TCP_SELECT_PIP, soap->send_timeout);
  558. else
  559. #endif
  560. r = tcp_select(soap, sk, SOAP_TCP_SELECT_SND | SOAP_TCP_SELECT_ERR | SOAP_TCP_SELECT_PIP, soap->send_timeout);
  561. if ((r & SOAP_TCP_SELECT_PIP)) /* abort if data is pending on pipe */
  562. {
  563. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Connection closed by self pipe\n"));
  564. return SOAP_EOF;
  565. }
  566. #else
  567. #ifdef WITH_OPENSSL
  568. if (soap->ssl)
  569. r = tcp_select(soap, sk, SOAP_TCP_SELECT_ALL, soap->send_timeout);
  570. else
  571. #endif
  572. #ifdef WITH_GNUTLS
  573. if (soap->session)
  574. r = tcp_select(soap, sk, SOAP_TCP_SELECT_ALL, soap->send_timeout);
  575. else
  576. #endif
  577. #ifdef WITH_SYSTEMSSL
  578. if (soap->ssl)
  579. r = tcp_select(soap, sk, SOAP_TCP_SELECT_ALL, soap->send_timeout);
  580. else
  581. #endif
  582. r = tcp_select(soap, sk, SOAP_TCP_SELECT_SND | SOAP_TCP_SELECT_ERR, soap->send_timeout);
  583. #endif
  584. if (r > 0)
  585. break;
  586. if (!r)
  587. return SOAP_EOF;
  588. err = soap->errnum;
  589. if (!err)
  590. return soap->error;
  591. if (err != SOAP_EAGAIN && err != SOAP_EWOULDBLOCK)
  592. return SOAP_EOF;
  593. }
  594. }
  595. #ifndef WITH_LEAN
  596. if (soap->transfer_timeout) {
  597. time_t now = time(NULL);
  598. if ((soap->transfer_timeout > 0 &&
  599. difftime(now, (time_t) soap->start) > (double) soap->transfer_timeout)
  600. || (soap->transfer_timeout < 0 &&
  601. difftime(now, (time_t) soap->start) > -1000000.0 * (double) soap->transfer_timeout))
  602. return SOAP_EOF;
  603. }
  604. #endif
  605. #ifdef WITH_OPENSSL
  606. if (soap->ssl)
  607. nwritten = SSL_write(soap->ssl, s, (int)n);
  608. else if (soap->bio)
  609. nwritten = BIO_write(soap->bio, s, (int)n);
  610. else
  611. #endif
  612. #ifdef WITH_GNUTLS
  613. if (soap->session)
  614. nwritten = gnutls_record_send(soap->session, s, n);
  615. else
  616. #endif
  617. #ifdef WITH_SYSTEMSSL
  618. if (soap->ssl)
  619. {
  620. err = gsk_secure_socket_write(soap->ssl, (char*)s, n, &nwritten);
  621. if (err != GSK_OK)
  622. nwritten = 0;
  623. }
  624. else
  625. #endif
  626. #ifndef WITH_LEAN
  627. if ((soap->omode & SOAP_IO_UDP)) {
  628. if (soap->peerlen)
  629. nwritten = sendto(sk, (char *) s, (SOAP_WINSOCKINT) n, soap->socket_flags, &soap->peer.addr,
  630. (SOAP_WINSOCKINT) soap->peerlen);
  631. else
  632. nwritten = send(sk, s, (SOAP_WINSOCKINT) n, soap->socket_flags);
  633. /* retry and back-off algorithm */
  634. /* TODO: this is not very clear from specs so verify and limit conditions under which we should loop (e.g. ENOBUFS) */
  635. if (nwritten < 0) {
  636. int udp_repeat;
  637. int udp_delay;
  638. if ((soap->connect_flags & SO_BROADCAST))
  639. udp_repeat = 2; /* SOAP-over-UDP MULTICAST_UDP_REPEAT - 1 */
  640. else
  641. udp_repeat = 1; /* SOAP-over-UDP UNICAST_UDP_REPEAT - 1 */
  642. udp_delay = ((unsigned int) soap_random % 201) + 50; /* UDP_MIN_DELAY .. UDP_MAX_DELAY */
  643. do {
  644. tcp_select(soap, sk, SOAP_TCP_SELECT_ERR, -1000 * udp_delay);
  645. if (soap->peerlen)
  646. nwritten = sendto(sk, (char *) s, (SOAP_WINSOCKINT) n, soap->socket_flags, &soap->peer.addr,
  647. (SOAP_WINSOCKINT) soap->peerlen);
  648. else
  649. nwritten = send(sk, s, (SOAP_WINSOCKINT) n, soap->socket_flags);
  650. udp_delay <<= 1;
  651. if (udp_delay > 500) /* UDP_UPPER_DELAY */
  652. udp_delay = 500;
  653. } while (nwritten < 0 && --udp_repeat > 0);
  654. }
  655. if (nwritten < 0) {
  656. err = soap_socket_errno(sk);
  657. if (err && err != SOAP_EINTR) {
  658. soap->errnum = err;
  659. return SOAP_EOF;
  660. }
  661. nwritten = 0; /* and call write() again */
  662. }
  663. } else
  664. #endif
  665. #if !defined(AS400)
  666. nwritten = send(sk, s, (int) n, soap->socket_flags);
  667. #else
  668. nwritten = send(sk, (void*)s, n, soap->socket_flags);
  669. #endif
  670. if (nwritten <= 0) {
  671. int r = 0;
  672. err = soap_socket_errno(sk);
  673. #ifdef WITH_OPENSSL
  674. if (soap->ssl && (r = SSL_get_error(soap->ssl, nwritten)) != SSL_ERROR_NONE && r != SSL_ERROR_WANT_READ && r != SSL_ERROR_WANT_WRITE)
  675. {
  676. soap->errnum = err;
  677. return SOAP_EOF;
  678. }
  679. #endif
  680. #ifdef WITH_GNUTLS
  681. if (soap->session)
  682. {
  683. if (nwritten == GNUTLS_E_INTERRUPTED)
  684. err = SOAP_EINTR;
  685. else if (nwritten == GNUTLS_E_AGAIN)
  686. err = SOAP_EAGAIN;
  687. }
  688. #endif
  689. if (err == SOAP_EWOULDBLOCK || err == SOAP_EAGAIN) {
  690. #if defined(WITH_OPENSSL)
  691. if (soap->ssl && r == SSL_ERROR_WANT_READ)
  692. r = tcp_select(soap, sk, SOAP_TCP_SELECT_RCV | SOAP_TCP_SELECT_ERR, soap->send_timeout ? soap->send_timeout : -10000);
  693. else
  694. #elif defined(WITH_GNUTLS)
  695. if (soap->session && !gnutls_record_get_direction(soap->session))
  696. r = tcp_select(soap, sk, SOAP_TCP_SELECT_RCV | SOAP_TCP_SELECT_ERR, soap->send_timeout ? soap->send_timeout : -10000);
  697. else
  698. #endif
  699. r = tcp_select(soap, sk, SOAP_TCP_SELECT_SND | SOAP_TCP_SELECT_ERR,
  700. soap->send_timeout ? soap->send_timeout : -10000);
  701. if (!r && soap->send_timeout)
  702. return SOAP_EOF;
  703. if (r < 0)
  704. return SOAP_EOF;
  705. } else if (err && err != SOAP_EINTR) {
  706. soap->errnum = err;
  707. return SOAP_EOF;
  708. }
  709. nwritten = 0; /* and call write() again */
  710. }
  711. } else {
  712. #ifdef WITH_FASTCGI
  713. nwritten = fwrite((void*)s, 1, n, stdout);
  714. fflush(stdout);
  715. #else
  716. #ifdef UNDER_CE
  717. nwritten = fwrite(s, 1, n, soap->sendfd);
  718. #else
  719. #ifdef WMW_RPM_IO
  720. /* vxWorks compatible */
  721. if (soap->rpmreqid)
  722. nwritten = (httpBlockPut(soap->rpmreqid, (char*)s, n) == 0) ? n : -1;
  723. else
  724. #endif
  725. #ifdef WIN32
  726. nwritten = _write(soap->sendfd, s, (unsigned int)n);
  727. #else
  728. nwritten = write(soap->sendfd, s, (unsigned int) n);
  729. #endif
  730. #endif
  731. #endif
  732. if (nwritten <= 0) {
  733. #ifndef WITH_FASTCGI
  734. err = soap_errno;
  735. #else
  736. err = EOF;
  737. #endif
  738. if (err && err != SOAP_EINTR && err != SOAP_EWOULDBLOCK && err != SOAP_EAGAIN) {
  739. soap->errnum = err;
  740. return SOAP_EOF;
  741. }
  742. nwritten = 0; /* and call write() again */
  743. }
  744. }
  745. n -= nwritten;
  746. s += nwritten;
  747. }
  748. return SOAP_OK;
  749. }
  750. #endif
  751. /******************************************************************************/
  752. SOAP_FMAC1
  753. int
  754. SOAP_FMAC2
  755. soap_send_raw(struct soap *soap, const char *s, size_t n) {
  756. if (!n)
  757. return SOAP_OK;
  758. #ifndef WITH_LEANER
  759. if (soap->fpreparesend && (soap->mode & SOAP_IO) != SOAP_IO_STORE && (soap->mode & SOAP_IO_LENGTH) &&
  760. (soap->error = soap->fpreparesend(soap, s, n)) != SOAP_OK)
  761. return soap->error;
  762. if (soap->ffiltersend && (soap->error = soap->ffiltersend(soap, &s, &n)) != SOAP_OK)
  763. return soap->error;
  764. #endif
  765. if ((soap->mode & SOAP_IO_LENGTH)) {
  766. soap->count += n;
  767. } else if ((soap->mode & SOAP_IO)) {
  768. size_t i = sizeof(soap->buf) - soap->bufidx;
  769. while (n >= i) {
  770. (void) soap_memcpy((void *) (soap->buf + soap->bufidx), i, (const void *) s, i);
  771. soap->bufidx = sizeof(soap->buf);
  772. if (soap_flush(soap))
  773. return soap->error;
  774. s += i;
  775. n -= i;
  776. i = sizeof(soap->buf);
  777. }
  778. (void) soap_memcpy((void *) (soap->buf + soap->bufidx), sizeof(soap->buf) - soap->bufidx, (const void *) s, n);
  779. soap->bufidx += n;
  780. } else {
  781. return soap_flush_raw(soap, s, n);
  782. }
  783. return SOAP_OK;
  784. }
  785. /******************************************************************************/
  786. SOAP_FMAC1
  787. int
  788. SOAP_FMAC2
  789. soap_flush(struct soap *soap) {
  790. size_t n = soap->bufidx;
  791. if (!n)
  792. return soap->error = soap->fsend(soap, SOAP_STR_EOS, 0); /* force a zero send for HTTP GET and DELETE */
  793. #ifndef WITH_LEANER
  794. if ((soap->mode & SOAP_IO) == SOAP_IO_STORE) {
  795. int r;
  796. if (soap->fpreparesend && (r = soap->fpreparesend(soap, soap->buf, n)) != SOAP_OK)
  797. return soap->error = r;
  798. }
  799. #endif
  800. soap->bufidx = 0;
  801. #ifdef WITH_ZLIB
  802. if ((soap->mode & SOAP_ENC_ZLIB) && soap->d_stream)
  803. {
  804. soap->d_stream->next_in = (Byte*)soap->buf;
  805. soap->d_stream->avail_in = (unsigned int)n;
  806. #ifdef WITH_GZIP
  807. soap->z_crc = crc32(soap->z_crc, (Byte*)soap->buf, (unsigned int)n);
  808. #endif
  809. do
  810. {
  811. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Deflating %u bytes\n", soap->d_stream->avail_in));
  812. if (deflate(soap->d_stream, Z_NO_FLUSH) != Z_OK)
  813. {
  814. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Unable to deflate: %s\n", soap->d_stream->msg ? soap->d_stream->msg : SOAP_STR_EOS));
  815. return soap->error = SOAP_ZLIB_ERROR;
  816. }
  817. if (!soap->d_stream->avail_out)
  818. {
  819. if (soap_flush_raw(soap, soap->z_buf, sizeof(soap->buf)))
  820. return soap->error;
  821. soap->d_stream->next_out = (Byte*)soap->z_buf;
  822. soap->d_stream->avail_out = sizeof(soap->buf);
  823. }
  824. } while (soap->d_stream->avail_in);
  825. return SOAP_OK;
  826. }
  827. #endif
  828. return soap_flush_raw(soap, soap->buf, n);
  829. }
  830. /******************************************************************************/
  831. SOAP_FMAC1
  832. int
  833. SOAP_FMAC2
  834. soap_flush_raw(struct soap *soap, const char *s, size_t n) {
  835. if ((soap->mode & SOAP_IO) == SOAP_IO_STORE) {
  836. void *t;
  837. t = soap_push_block(soap, NULL, n);
  838. if (!t)
  839. return soap->error = SOAP_EOM;
  840. (void) soap_memcpy(t, n, (const void *) s, n);
  841. return SOAP_OK;
  842. }
  843. #ifndef WITH_LEANER
  844. if ((soap->mode & SOAP_IO) == SOAP_IO_CHUNK) {
  845. char t[24];
  846. (SOAP_SNPRINTF(t, sizeof(t), 20), &"\r\n%lX\r\n"[soap->chunksize ? 0 : 2], (unsigned long) n);
  847. DBGMSG(SENT, t, strlen(t));
  848. soap->error = soap->fsend(soap, t, strlen(t));
  849. if (soap->error)
  850. return soap->error;
  851. soap->chunksize += n;
  852. }
  853. DBGMSG(SENT, s, n);
  854. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Send %u bytes to socket=%d/fd=%d\n", (unsigned int) n, (int) soap->socket,
  855. soap->sendfd));
  856. #endif
  857. return soap->error = soap->fsend(soap, s, n);
  858. }
  859. /******************************************************************************/
  860. SOAP_FMAC1
  861. int
  862. SOAP_FMAC2
  863. soap_send(struct soap *soap, const char *s) {
  864. if (s)
  865. return soap_send_raw(soap, s, strlen(s));
  866. return SOAP_OK;
  867. }
  868. /******************************************************************************/
  869. #ifndef WITH_LEANER
  870. SOAP_FMAC1
  871. int
  872. SOAP_FMAC2
  873. soap_send2(struct soap *soap, const char *s1, const char *s2) {
  874. if (soap_send(soap, s1))
  875. return soap->error;
  876. return soap_send(soap, s2);
  877. }
  878. #endif
  879. /******************************************************************************/
  880. #ifndef WITH_LEANER
  881. SOAP_FMAC1
  882. int
  883. SOAP_FMAC2
  884. soap_send3(struct soap *soap, const char *s1, const char *s2, const char *s3) {
  885. if (soap_send(soap, s1)
  886. || soap_send(soap, s2))
  887. return soap->error;
  888. return soap_send(soap, s3);
  889. }
  890. #endif
  891. /******************************************************************************/
  892. #ifndef WITH_LEANER
  893. SOAP_FMAC1
  894. int
  895. SOAP_FMAC2
  896. soap_query_send_key(struct soap *soap, const char *s) {
  897. if (!s)
  898. return SOAP_OK;
  899. if (!soap->body && soap_send_raw(soap, "&", 1))
  900. return soap->error;
  901. soap->body = 0;
  902. (void) soap_encode_url(s, soap->msgbuf,
  903. (int) sizeof(soap->msgbuf)); /* msgbuf length is max SOAP_TMPLEN or just 1024 bytes */
  904. return soap_send(soap, soap->msgbuf);
  905. }
  906. #endif
  907. /******************************************************************************/
  908. #ifndef WITH_LEANER
  909. SOAP_FMAC1
  910. int
  911. SOAP_FMAC2
  912. soap_query_send_val(struct soap *soap, const char *s) {
  913. if (!s)
  914. return SOAP_OK;
  915. if (soap_send_raw(soap, "=", 1))
  916. return soap->error;
  917. (void) soap_encode_url(s, soap->msgbuf,
  918. (int) sizeof(soap->msgbuf)); /* msgbuf length is max SOAP_TMPLEN or just 1024 bytes */
  919. return soap_send(soap, soap->msgbuf);
  920. }
  921. #endif
  922. /******************************************************************************/
  923. #ifndef WITH_LEANER
  924. SOAP_FMAC1
  925. char *
  926. SOAP_FMAC2
  927. soap_query(struct soap *soap) {
  928. return strchr(soap->path, '?');
  929. }
  930. #endif
  931. /******************************************************************************/
  932. #ifndef WITH_LEANER
  933. SOAP_FMAC1
  934. char *
  935. SOAP_FMAC2
  936. soap_query_key(struct soap *soap, char **s) {
  937. char *t = *s;
  938. (void) soap;
  939. if (t && *t) {
  940. *s = (char *) soap_query_decode(t, strlen(t), t + 1);
  941. return t;
  942. }
  943. return *s = NULL;
  944. }
  945. #endif
  946. /******************************************************************************/
  947. #ifndef WITH_LEANER
  948. SOAP_FMAC1
  949. char *
  950. SOAP_FMAC2
  951. soap_query_val(struct soap *soap, char **s) {
  952. char *t = *s;
  953. (void) soap;
  954. if (t && *t == '=') {
  955. *s = (char *) soap_query_decode(t, strlen(t), t + 1);
  956. return t;
  957. }
  958. return NULL;
  959. }
  960. #endif
  961. /******************************************************************************/
  962. #ifndef WITH_LEANER
  963. SOAP_FMAC1
  964. const char *
  965. SOAP_FMAC2
  966. soap_query_decode(char *buf, size_t len, const char *val) {
  967. const char *s;
  968. char *t;
  969. for (s = val; *s; s++)
  970. if (*s != ' ' && *s != '=')
  971. break;
  972. if (*s == '"') {
  973. t = buf;
  974. s++;
  975. while (*s && *s != '"' && --len)
  976. *t++ = *s++;
  977. *t = '\0';
  978. do s++;
  979. while (*s && *s != '&' && *s != '=');
  980. } else {
  981. t = buf;
  982. while (*s && *s != '&' && *s != '=' && --len) {
  983. switch (*s) {
  984. case '+':
  985. *t++ = ' ';
  986. s++;
  987. break;
  988. case '\t':
  989. case '\n':
  990. case '\r':
  991. case ' ':
  992. s++;
  993. break;
  994. case '%':
  995. *t++ = ((s[1] >= 'A' ? (s[1] & 0x7) + 9 : s[1] - '0') << 4) +
  996. (s[2] >= 'A' ? (s[2] & 0x7) + 9 : s[2] - '0');
  997. s += 3;
  998. break;
  999. default:
  1000. *t++ = *s++;
  1001. }
  1002. }
  1003. *t = '\0';
  1004. }
  1005. return s;
  1006. }
  1007. #endif
  1008. /******************************************************************************/
  1009. #ifndef WITH_NOIO
  1010. static size_t
  1011. frecv(struct soap *soap, char *s, size_t n) {
  1012. int r;
  1013. int retries = 100; /* max 100 retries with non-blocking sockets */
  1014. SOAP_SOCKET sk;
  1015. soap->errnum = 0;
  1016. #if defined(__cplusplus) && !defined(WITH_COMPAT)
  1017. if (soap->is) /* recv from C++ stream */
  1018. {
  1019. if (soap->is->good())
  1020. return (size_t) soap->is->read(s,
  1021. (std::streamsize) n).gcount(); /* downcast to std::streamsize is OK: gcount() returns how much we got in s[] */
  1022. return 0;
  1023. }
  1024. #else
  1025. if (soap->is) /* recv from C buffer until NUL */
  1026. {
  1027. size_t l = strlen(soap->is);
  1028. if (l > n)
  1029. l = n;
  1030. (void)soap_memcpy((void*)s, n, soap->is, l);
  1031. soap->is += l;
  1032. return l;
  1033. }
  1034. #endif
  1035. sk = soap->recvsk;
  1036. if (!soap_valid_socket(sk))
  1037. sk = soap->socket;
  1038. if (soap_valid_socket(sk)) {
  1039. for (;;) {
  1040. #if defined(WITH_OPENSSL) || defined(WITH_SYSTEMSSL)
  1041. int err = 0;
  1042. #endif
  1043. #ifdef WITH_OPENSSL
  1044. if (soap->recv_timeout && !soap->ssl) /* OpenSSL: sockets are nonblocking so go ahead to read */
  1045. #else
  1046. if (soap->recv_timeout)
  1047. #endif
  1048. {
  1049. for (;;) {
  1050. #ifdef WITH_SELF_PIPE
  1051. r = tcp_select(soap, sk, SOAP_TCP_SELECT_RCV | SOAP_TCP_SELECT_ERR | SOAP_TCP_SELECT_PIP, soap->recv_timeout);
  1052. if ((r & SOAP_TCP_SELECT_PIP)) /* abort if data is pending on pipe */
  1053. {
  1054. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Connection closed by self pipe\n"));
  1055. return 0;
  1056. }
  1057. #else
  1058. r = tcp_select(soap, sk, SOAP_TCP_SELECT_RCV | SOAP_TCP_SELECT_ERR, soap->recv_timeout);
  1059. #endif
  1060. if (r > 0)
  1061. break;
  1062. if (!r)
  1063. return 0;
  1064. r = soap->errnum;
  1065. if (r != SOAP_EAGAIN && r != SOAP_EWOULDBLOCK)
  1066. return 0;
  1067. }
  1068. }
  1069. #ifndef WITH_LEAN
  1070. if (soap->transfer_timeout) {
  1071. time_t now = time(NULL);
  1072. if ((soap->transfer_timeout > 0 &&
  1073. difftime(now, (time_t) soap->start) > (double) soap->transfer_timeout)
  1074. || (soap->transfer_timeout < 0 &&
  1075. difftime(now, (time_t) soap->start) > -1000000.0 * (double) soap->transfer_timeout))
  1076. return 0;
  1077. }
  1078. #endif
  1079. #ifdef WITH_OPENSSL
  1080. if (soap->ssl)
  1081. {
  1082. r = SSL_read(soap->ssl, s, (int)n);
  1083. if (r > 0)
  1084. return (size_t)r;
  1085. err = SSL_get_error(soap->ssl, r);
  1086. if (err != SSL_ERROR_NONE && err != SSL_ERROR_WANT_READ && err != SSL_ERROR_WANT_WRITE)
  1087. return 0;
  1088. }
  1089. else if (soap->bio)
  1090. {
  1091. r = BIO_read(soap->bio, s, (int)n);
  1092. if (r > 0)
  1093. return (size_t)r;
  1094. return 0;
  1095. }
  1096. else
  1097. #endif
  1098. #ifdef WITH_GNUTLS
  1099. if (soap->session)
  1100. {
  1101. r = (int)gnutls_record_recv(soap->session, s, n);
  1102. if (r >= 0)
  1103. return (size_t)r;
  1104. }
  1105. else
  1106. #endif
  1107. #ifdef WITH_SYSTEMSSL
  1108. if (soap->ssl)
  1109. {
  1110. err = gsk_secure_socket_read(soap->ssl, s, n, &r);
  1111. if (err == GSK_OK && r > 0)
  1112. return (size_t)r;
  1113. if (err != GSK_OK && err != GSK_WOULD_BLOCK && err != GSK_WOULD_BLOCK_WRITE)
  1114. return 0;
  1115. }
  1116. else
  1117. #endif
  1118. {
  1119. #ifndef WITH_LEAN
  1120. if ((soap->omode & SOAP_IO_UDP)) {
  1121. SOAP_SOCKLEN_T k = (SOAP_SOCKLEN_T) sizeof(soap->peer);
  1122. memset((void *) &soap->peer, 0, sizeof(soap->peer));
  1123. r = recvfrom(sk, s, (SOAP_WINSOCKINT) n, soap->socket_flags, &soap->peer.addr,
  1124. &k); /* portability note: see SOAP_SOCKLEN_T definition in stdsoap2.h, SOAP_WINSOCKINT cast is safe due to limited range of n in the engine (64K) */
  1125. soap->peerlen = (size_t) k;
  1126. #ifdef WITH_IPV6
  1127. soap->ip = 0;
  1128. soap->ip6[0] = 0;
  1129. soap->ip6[1] = 0;
  1130. soap->ip6[2] = 0;
  1131. soap->ip6[3] = 0;
  1132. #else
  1133. soap->ip = ntohl(soap->peer.in.sin_addr.s_addr);
  1134. soap->ip6[0] = 0;
  1135. soap->ip6[1] = 0;
  1136. soap->ip6[2] = 0xFFFF;
  1137. soap->ip6[3] = soap->ip;
  1138. #endif
  1139. } else
  1140. #endif
  1141. r = recv(sk, s, (SOAP_WINSOCKINT) n,
  1142. soap->socket_flags); /* SOAP_WINSOCKINT cast is safe due to limited range of n in the engine (64K) */
  1143. if (r >= 0)
  1144. return (size_t) r;
  1145. r = soap_socket_errno(sk);
  1146. if (r != SOAP_EINTR && r != SOAP_EAGAIN && r != SOAP_EWOULDBLOCK) {
  1147. soap->errnum = r;
  1148. return 0;
  1149. }
  1150. }
  1151. #if defined(WITH_OPENSSL)
  1152. if (soap->ssl && err == SSL_ERROR_WANT_WRITE)
  1153. r = tcp_select(soap, sk, SOAP_TCP_SELECT_SND | SOAP_TCP_SELECT_ERR, soap->recv_timeout ? soap->recv_timeout : 5);
  1154. else
  1155. #elif defined(WITH_GNUTLS)
  1156. if (soap->session && gnutls_record_get_direction(soap->session))
  1157. r = tcp_select(soap, sk, SOAP_TCP_SELECT_SND | SOAP_TCP_SELECT_ERR, soap->recv_timeout ? soap->recv_timeout : 5);
  1158. else
  1159. #elif defined(WITH_SYSTEMSSL)
  1160. if (soap->ssl && err == GSK_WOULD_BLOCK_WRITE)
  1161. r = tcp_select(soap, sk, SOAP_TCP_SELECT_SND | SOAP_TCP_SELECT_ERR, soap->recv_timeout ? soap->recv_timeout : 5);
  1162. else
  1163. #endif
  1164. r = tcp_select(soap, sk, SOAP_TCP_SELECT_RCV | SOAP_TCP_SELECT_ERR,
  1165. soap->recv_timeout ? soap->recv_timeout : 5);
  1166. if (!r && soap->recv_timeout)
  1167. return 0;
  1168. if (r < 0) {
  1169. r = soap->errnum;
  1170. if (r != SOAP_EAGAIN && r != SOAP_EWOULDBLOCK)
  1171. return 0;
  1172. }
  1173. if (retries-- <= 0)
  1174. return 0;
  1175. }
  1176. }
  1177. #ifdef WITH_FASTCGI
  1178. return fread(s, 1, n, stdin);
  1179. #else
  1180. #ifdef UNDER_CE
  1181. return fread(s, 1, n, soap->recvfd);
  1182. #else
  1183. #ifdef WMW_RPM_IO
  1184. if (soap->rpmreqid)
  1185. r = httpBlockRead(soap->rpmreqid, s, n);
  1186. else
  1187. #endif
  1188. #ifdef WIN32
  1189. r = _read(soap->recvfd, s, (unsigned int)n);
  1190. #else
  1191. r = read(soap->recvfd, s, n);
  1192. #endif
  1193. if (r >= 0)
  1194. return (size_t) r;
  1195. soap->errnum = soap_errno;
  1196. return 0;
  1197. #endif
  1198. #endif
  1199. }
  1200. #endif
  1201. /******************************************************************************/
  1202. #ifndef WITH_NOHTTP
  1203. static soap_wchar
  1204. soap_getchunkchar(struct soap *soap) {
  1205. if (soap->bufidx < soap->buflen)
  1206. return soap->buf[soap->bufidx++];
  1207. soap->bufidx = 0;
  1208. soap->buflen = soap->chunkbuflen = soap->frecv(soap, soap->buf, sizeof(soap->buf));
  1209. DBGLOG(TEST,
  1210. SOAP_MESSAGE(fdebug, "Read %u bytes from socket=%d/fd=%d\n", (unsigned int) soap->buflen, (int) soap->socket,
  1211. soap->recvfd));
  1212. DBGMSG(RECV, soap->buf, soap->buflen);
  1213. if (soap->buflen)
  1214. return soap->buf[soap->bufidx++];
  1215. return EOF;
  1216. }
  1217. #endif
  1218. /******************************************************************************/
  1219. static int
  1220. soap_isxdigit(int c) {
  1221. return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'F') || (c >= 'a' && c <= 'f');
  1222. }
  1223. /******************************************************************************/
  1224. SOAP_FMAC1
  1225. int
  1226. SOAP_FMAC2
  1227. soap_recv_raw(struct soap *soap) {
  1228. size_t ret;
  1229. #if !defined(WITH_LEANER) || defined(WITH_ZLIB)
  1230. int r;
  1231. #endif
  1232. #ifdef WITH_ZLIB
  1233. if ((soap->mode & SOAP_ENC_ZLIB) && soap->d_stream)
  1234. {
  1235. if (soap->d_stream->next_out == Z_NULL)
  1236. {
  1237. soap->bufidx = soap->buflen = 0;
  1238. return EOF;
  1239. }
  1240. if (soap->d_stream->avail_in || !soap->d_stream->avail_out)
  1241. {
  1242. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Inflating\n"));
  1243. soap->d_stream->next_out = (Byte*)soap->buf;
  1244. soap->d_stream->avail_out = sizeof(soap->buf);
  1245. r = inflate(soap->d_stream, Z_NO_FLUSH);
  1246. if (r == Z_NEED_DICT && soap->z_dict)
  1247. r = inflateSetDictionary(soap->d_stream, (const Bytef*)soap->z_dict, soap->z_dict_len);
  1248. if (r == Z_OK || r == Z_STREAM_END)
  1249. {
  1250. soap->bufidx = 0;
  1251. ret = soap->buflen = sizeof(soap->buf) - soap->d_stream->avail_out;
  1252. if (soap->zlib_in == SOAP_ZLIB_GZIP)
  1253. soap->z_crc = crc32(soap->z_crc, (Byte*)soap->buf, (unsigned int)ret);
  1254. if (r == Z_STREAM_END)
  1255. {
  1256. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Inflated %lu->%lu bytes\n", soap->d_stream->total_in, soap->d_stream->total_out));
  1257. soap->z_ratio_in = (float)soap->d_stream->total_in / (float)soap->d_stream->total_out;
  1258. soap->d_stream->next_out = Z_NULL;
  1259. }
  1260. if (ret)
  1261. {
  1262. if (soap->count + ret < soap->count)
  1263. return soap->error = SOAP_EOF;
  1264. soap->count += ret;
  1265. if (soap->recv_maxlength && soap->count > soap->recv_maxlength)
  1266. return soap->error = SOAP_EOF;
  1267. if (soap->count > SOAP_MAXINFLATESIZE && soap->z_ratio_in < SOAP_MINDEFLATERATIO)
  1268. {
  1269. soap->d_stream->msg = (char*)"caught SOAP_MINDEFLATERATIO explosive decompression guard (remedy: increase SOAP_MAXINFLATESIZE and/or decrease SOAP_MINDEFLATERATIO)";
  1270. return soap->error = SOAP_ZLIB_ERROR;
  1271. }
  1272. DBGLOG(RECV, SOAP_MESSAGE(fdebug, "\n---- decompressed ----\n"));
  1273. DBGMSG(RECV, soap->buf, ret);
  1274. DBGLOG(RECV, SOAP_MESSAGE(fdebug, "\n----\n"));
  1275. #ifndef WITH_LEANER
  1276. if (soap->fpreparerecv && (r = soap->fpreparerecv(soap, soap->buf, ret)) != SOAP_OK)
  1277. return soap->error = r;
  1278. #endif
  1279. return SOAP_OK;
  1280. }
  1281. }
  1282. else if (r != Z_BUF_ERROR)
  1283. {
  1284. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Inflate error: %s\n", soap->d_stream->msg ? soap->d_stream->msg : SOAP_STR_EOS));
  1285. soap->d_stream->next_out = Z_NULL;
  1286. return soap->error = SOAP_ZLIB_ERROR;
  1287. }
  1288. }
  1289. zlib_again:
  1290. if ((soap->mode & SOAP_IO) == SOAP_IO_CHUNK && !soap->chunksize)
  1291. {
  1292. (void)soap_memcpy((void*)soap->buf, sizeof(soap->buf), (const void*)soap->z_buf, sizeof(soap->buf));
  1293. soap->buflen = soap->z_buflen;
  1294. }
  1295. DBGLOG(RECV, SOAP_MESSAGE(fdebug, "\n---- compressed ----\n"));
  1296. }
  1297. #endif
  1298. #ifndef WITH_NOHTTP
  1299. if ((soap->mode & SOAP_IO) == SOAP_IO_CHUNK) /* read HTTP chunked transfer */
  1300. {
  1301. for (;;) {
  1302. soap_wchar c;
  1303. char *t, tmp[17];
  1304. if (soap->chunksize) {
  1305. soap->buflen = ret = soap->frecv(soap, soap->buf,
  1306. soap->chunksize > sizeof(soap->buf) ? sizeof(soap->buf)
  1307. : soap->chunksize);
  1308. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Getting chunk: read %u bytes\n", (unsigned int) ret));
  1309. DBGMSG(RECV, soap->buf, ret);
  1310. soap->bufidx = 0;
  1311. if (!ret) {
  1312. soap->ahead = EOF;
  1313. return EOF;
  1314. }
  1315. soap->chunksize -= ret;
  1316. break;
  1317. }
  1318. if (!soap->chunkbuflen) {
  1319. soap->chunkbuflen = ret = soap->frecv(soap, soap->buf, sizeof(soap->buf));
  1320. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Read %u bytes (chunked) from socket=%d\n", (unsigned int) ret,
  1321. (int) soap->socket));
  1322. DBGMSG(RECV, soap->buf, ret);
  1323. soap->bufidx = 0;
  1324. if (!ret) {
  1325. soap->ahead = EOF;
  1326. return EOF;
  1327. }
  1328. } else {
  1329. soap->bufidx = soap->buflen;
  1330. }
  1331. soap->buflen = soap->chunkbuflen;
  1332. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Getting chunk size (idx=%u len=%u)\n", (unsigned int) soap->bufidx,
  1333. (unsigned int) soap->buflen));
  1334. while (!soap_isxdigit((int) (c = soap_getchunkchar(soap)))) {
  1335. if ((int) c == EOF) {
  1336. soap->ahead = EOF;
  1337. return EOF;
  1338. }
  1339. }
  1340. t = tmp;
  1341. do {
  1342. *t++ = (char) c;
  1343. } while (soap_isxdigit((int) (c = soap_getchunkchar(soap))) && (size_t) (t - tmp) < sizeof(tmp) - 1);
  1344. while ((int) c != EOF && c != '\n')
  1345. c = soap_getchunkchar(soap);
  1346. if ((int) c == EOF) {
  1347. soap->ahead = EOF;
  1348. return EOF;
  1349. }
  1350. *t = '\0';
  1351. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Chunk size = %s (hex)\n", tmp));
  1352. soap->chunksize = (size_t) soap_strtoul(tmp, &t, 16);
  1353. if (!soap->chunksize) {
  1354. soap->bufidx = soap->buflen = soap->chunkbuflen = 0;
  1355. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "End of chunked message\n"));
  1356. ret = 0;
  1357. soap->ahead = EOF;
  1358. break;
  1359. }
  1360. soap->buflen = soap->bufidx + soap->chunksize;
  1361. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Moving buf len to idx=%u len=%u (%s)\n", (unsigned int) soap->bufidx,
  1362. (unsigned int) soap->buflen, tmp));
  1363. if (soap->buflen > soap->chunkbuflen) {
  1364. soap->buflen = soap->chunkbuflen;
  1365. soap->chunksize -= soap->buflen - soap->bufidx;
  1366. soap->chunkbuflen = 0;
  1367. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Passed end of buffer for chunked HTTP (%u bytes left)\n",
  1368. (unsigned int) (soap->buflen - soap->bufidx)));
  1369. } else if (soap->chunkbuflen) {
  1370. soap->chunksize = 0;
  1371. }
  1372. ret = soap->buflen - soap->bufidx;
  1373. if (ret)
  1374. break;
  1375. }
  1376. } else
  1377. #endif
  1378. {
  1379. soap->bufidx = 0;
  1380. soap->buflen = ret = soap->frecv(soap, soap->buf, sizeof(soap->buf));
  1381. DBGLOG(TEST,
  1382. SOAP_MESSAGE(fdebug, "Read %u bytes from socket=%d/fd=%d\n", (unsigned int) ret, (int) soap->socket,
  1383. soap->recvfd));
  1384. DBGMSG(RECV, soap->buf, ret);
  1385. }
  1386. #ifdef WITH_ZLIB
  1387. if ((soap->mode & SOAP_ENC_ZLIB))
  1388. {
  1389. (void)soap_memcpy((void*)soap->z_buf, sizeof(soap->buf), (const void*)soap->buf, sizeof(soap->buf));
  1390. soap->d_stream->next_in = (Byte*)(soap->z_buf + soap->bufidx);
  1391. soap->d_stream->avail_in = (unsigned int)ret;
  1392. soap->d_stream->next_out = (Byte*)soap->buf;
  1393. soap->d_stream->avail_out = sizeof(soap->buf);
  1394. r = inflate(soap->d_stream, Z_NO_FLUSH);
  1395. if (r == Z_NEED_DICT && soap->z_dict)
  1396. r = inflateSetDictionary(soap->d_stream, (const Bytef*)soap->z_dict, soap->z_dict_len);
  1397. if (r == Z_OK || r == Z_STREAM_END)
  1398. {
  1399. soap->bufidx = 0;
  1400. soap->z_buflen = soap->buflen;
  1401. soap->buflen = sizeof(soap->buf) - soap->d_stream->avail_out;
  1402. if (soap->zlib_in == SOAP_ZLIB_GZIP)
  1403. soap->z_crc = crc32(soap->z_crc, (Byte*)soap->buf, (unsigned int)soap->buflen);
  1404. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Inflated %u bytes\n", (unsigned int)soap->buflen));
  1405. if (ret && !soap->buflen && r != Z_STREAM_END)
  1406. goto zlib_again;
  1407. ret = soap->buflen;
  1408. if (r == Z_STREAM_END)
  1409. {
  1410. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Inflated total %lu->%lu bytes\n", soap->d_stream->total_in, soap->d_stream->total_out));
  1411. soap->z_ratio_in = (float)soap->d_stream->total_in / (float)soap->d_stream->total_out;
  1412. soap->d_stream->next_out = Z_NULL;
  1413. }
  1414. if (soap->count + ret > SOAP_MAXINFLATESIZE && soap->z_ratio_in < SOAP_MINDEFLATERATIO)
  1415. {
  1416. soap->d_stream->msg = (char*)"caught SOAP_MINDEFLATERATIO explosive decompression guard (remedy: increase SOAP_MAXINFLATESIZE and/or decrease SOAP_MINDEFLATERATIO)";
  1417. return soap->error = SOAP_ZLIB_ERROR;
  1418. }
  1419. DBGLOG(RECV, SOAP_MESSAGE(fdebug, "\n---- decompressed ----\n"));
  1420. DBGMSG(RECV, soap->buf, ret);
  1421. #ifndef WITH_LEANER
  1422. if (soap->fpreparerecv && (r = soap->fpreparerecv(soap, soap->buf, ret)) != SOAP_OK)
  1423. return soap->error = r;
  1424. #endif
  1425. }
  1426. else
  1427. {
  1428. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Unable to inflate: (%d) %s\n", r, soap->d_stream->msg ? soap->d_stream->msg : SOAP_STR_EOS));
  1429. soap->d_stream->next_out = Z_NULL;
  1430. return soap->error = SOAP_ZLIB_ERROR;
  1431. }
  1432. }
  1433. #endif
  1434. #ifndef WITH_LEANER
  1435. if (soap->fpreparerecv
  1436. #ifdef WITH_ZLIB
  1437. && soap->zlib_in == SOAP_ZLIB_NONE
  1438. #endif
  1439. && (r = soap->fpreparerecv(soap, soap->buf + soap->bufidx, ret)))
  1440. return soap->error = r;
  1441. #endif
  1442. if (ret) {
  1443. if (soap->count + ret < soap->count)
  1444. return EOF;
  1445. soap->count += ret;
  1446. if (soap->recv_maxlength && soap->count > soap->recv_maxlength)
  1447. return EOF;
  1448. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Read count="
  1449. SOAP_ULONG_FORMAT
  1450. " (+%lu)\n", soap->count, (unsigned long) ret));
  1451. return SOAP_OK;
  1452. }
  1453. return EOF;
  1454. }
  1455. /******************************************************************************/
  1456. SOAP_FMAC1
  1457. int
  1458. SOAP_FMAC2
  1459. soap_recv(struct soap *soap) {
  1460. #ifndef WITH_LEANER
  1461. if ((soap->mode & SOAP_ENC_DIME)) {
  1462. if (soap->dime.buflen) {
  1463. char *s;
  1464. int i;
  1465. unsigned char tmp[12];
  1466. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "DIME hdr for chunked SOAP in DIME is in buffer\n"));
  1467. soap->buflen = soap->dime.buflen;
  1468. soap->dime.buflen = 0;
  1469. soap->dime.chunksize = 0;
  1470. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Skip padding (%ld bytes)\n", -(long) soap->dime.size & 3));
  1471. for (i = -(long) soap->dime.size & 3; i > 0; i--) {
  1472. soap->bufidx++;
  1473. if (soap->bufidx >= soap->buflen)
  1474. if (soap_recv_raw(soap))
  1475. return EOF;
  1476. }
  1477. if (!(soap->dime.flags & SOAP_DIME_CF))
  1478. return SOAP_OK;
  1479. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Get DIME hdr for next SOAP in DIME chunk\n"));
  1480. s = (char *) tmp;
  1481. for (i = 12; i > 0; i--) {
  1482. *s++ = soap->buf[soap->bufidx++];
  1483. if (soap->bufidx >= soap->buflen)
  1484. if (soap_recv_raw(soap))
  1485. return EOF;
  1486. }
  1487. if ((tmp[0] & 0xF8) != SOAP_DIME_VERSION)
  1488. return soap->error = SOAP_DIME_MISMATCH;
  1489. soap->dime.flags = (tmp[0] & 0x7) | (tmp[1] & 0xF0);
  1490. soap->dime.size =
  1491. ((size_t) tmp[8] << 24) | ((size_t) tmp[9] << 16) | ((size_t) tmp[10] << 8) | ((size_t) tmp[11]);
  1492. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Get SOAP in DIME chunk (%u bytes)\n", (unsigned int) soap->dime.size));
  1493. soap->dime.chunksize = soap->dime.size;
  1494. if (soap->buflen - soap->bufidx >= soap->dime.size) {
  1495. if ((soap->dime.flags & SOAP_DIME_ME)) {
  1496. soap->mode &= ~SOAP_ENC_DIME;
  1497. } else {
  1498. soap->dime.buflen = soap->buflen;
  1499. soap->buflen = soap->bufidx + soap->dime.chunksize;
  1500. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Chunked SOAP in DIME (%u bytes buffered)\n",
  1501. (unsigned int) soap->buflen));
  1502. }
  1503. } else {
  1504. soap->dime.chunksize -= soap->buflen - soap->bufidx;
  1505. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Chunked SOAP in DIME (%u bytes in chunk left)\n",
  1506. (unsigned int) soap->dime.chunksize));
  1507. }
  1508. return SOAP_OK;
  1509. }
  1510. if (soap->dime.chunksize) {
  1511. if (soap_recv_raw(soap))
  1512. return EOF;
  1513. if (soap->buflen - soap->bufidx >= soap->dime.chunksize) {
  1514. if ((soap->dime.flags & SOAP_DIME_ME)) {
  1515. soap->dime.chunksize = 0;
  1516. soap->mode &= ~SOAP_ENC_DIME;
  1517. } else {
  1518. soap->dime.buflen = soap->buflen;
  1519. soap->buflen = soap->bufidx + soap->dime.chunksize;
  1520. soap->dime.chunksize = 0;
  1521. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Chunked SOAP in DIME (%u bytes buffered)\n",
  1522. (unsigned int) soap->buflen));
  1523. }
  1524. } else {
  1525. soap->dime.chunksize -= soap->buflen - soap->bufidx;
  1526. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Chunked SOAP in DIME (%u bytes in chunk left)\n",
  1527. (unsigned int) soap->dime.chunksize));
  1528. }
  1529. return SOAP_OK;
  1530. }
  1531. }
  1532. if (soap->ffilterrecv) {
  1533. int err;
  1534. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Filter recverror = %d bufidx = %lu buflen = %lu\n", soap->recverror,
  1535. (unsigned long) soap->bufidx, (unsigned long) soap->buflen));
  1536. if (soap->recverror) {
  1537. soap->bufidx = soap->buflen = 0;
  1538. } else {
  1539. soap->bufidx = soap->buflen = 0;
  1540. err = soap->ffilterrecv(soap, soap->buf, &soap->buflen, sizeof(soap->buf));
  1541. if (err) {
  1542. if (err == SOAP_EOF)
  1543. return SOAP_EOF;
  1544. return soap->error = err;
  1545. }
  1546. if (soap->buflen) {
  1547. DBGLOG(TEST,
  1548. SOAP_MESSAGE(fdebug, "Filtered output continued %lu bytes\n", (unsigned long) soap->buflen));
  1549. return SOAP_OK;
  1550. }
  1551. soap->recverror = soap_recv_raw(soap);
  1552. soap->buflen -= soap->bufidx; /* chunked may set bufidx > 0 to skip hex chunk length */
  1553. }
  1554. while (soap->ffilterrecv) {
  1555. err = soap->ffilterrecv(soap, soap->buf + soap->bufidx, &soap->buflen, sizeof(soap->buf) - soap->bufidx);
  1556. if (err) {
  1557. if (err == SOAP_EOF)
  1558. return SOAP_EOF;
  1559. return soap->error = err;
  1560. }
  1561. if (soap->buflen) {
  1562. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Filtered output %lu bytes\n", (unsigned long) soap->buflen));
  1563. soap->buflen += soap->bufidx;
  1564. return SOAP_OK;
  1565. }
  1566. if (soap->recverror) {
  1567. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Returning postponed error %d\n", soap->recverror));
  1568. return soap->recverror;
  1569. }
  1570. soap->recverror = soap_recv_raw(soap);
  1571. soap->buflen -= soap->bufidx; /* chunked may set bufidx > 0 to skip hex chunk length */
  1572. }
  1573. }
  1574. return soap->recverror = soap_recv_raw(soap);
  1575. #else
  1576. return soap_recv_raw(soap);
  1577. #endif
  1578. }
  1579. /******************************************************************************/
  1580. SOAP_FMAC1
  1581. soap_wchar
  1582. SOAP_FMAC2
  1583. soap_getchar(struct soap *soap) {
  1584. soap_wchar c;
  1585. c = soap->ahead;
  1586. if (c) {
  1587. if ((int) c != EOF)
  1588. soap->ahead = 0;
  1589. return c;
  1590. }
  1591. return soap_get1(soap);
  1592. }
  1593. /******************************************************************************/
  1594. SOAP_FMAC1
  1595. const struct soap_code_map *
  1596. SOAP_FMAC2
  1597. soap_code(const struct soap_code_map *code_map, const char *str) {
  1598. if (code_map && str) {
  1599. while (code_map->string) {
  1600. if (!strcmp(str, code_map->string)) /* case sensitive */
  1601. return code_map;
  1602. code_map++;
  1603. }
  1604. }
  1605. return NULL;
  1606. }
  1607. /******************************************************************************/
  1608. SOAP_FMAC1
  1609. LONG64
  1610. SOAP_FMAC2
  1611. soap_code_int(const struct soap_code_map *code_map, const char *str, LONG64 other) {
  1612. if (code_map) {
  1613. while (code_map->string) {
  1614. if (!soap_tag_cmp(str, code_map->string)) /* case insensitive */
  1615. return code_map->code;
  1616. code_map++;
  1617. }
  1618. }
  1619. return other;
  1620. }
  1621. /******************************************************************************/
  1622. SOAP_FMAC1
  1623. const char *
  1624. SOAP_FMAC2
  1625. soap_code_str(const struct soap_code_map *code_map, long code) {
  1626. if (!code_map)
  1627. return NULL;
  1628. while (code_map->code != code && code_map->string)
  1629. code_map++;
  1630. return code_map->string;
  1631. }
  1632. /******************************************************************************/
  1633. SOAP_FMAC1
  1634. LONG64
  1635. SOAP_FMAC2
  1636. soap_code_bits(const struct soap_code_map *code_map, const char *str) {
  1637. LONG64 bits = 0;
  1638. if (code_map) {
  1639. while (str && *str) {
  1640. const struct soap_code_map *p;
  1641. for (p = code_map; p->string; p++) {
  1642. size_t n = strlen(p->string);
  1643. if (!strncmp(p->string, str, n) && soap_coblank((soap_wchar) str[n])) {
  1644. bits |= p->code;
  1645. str += n;
  1646. while (*str > 0 && *str <= 32)
  1647. str++;
  1648. break;
  1649. }
  1650. }
  1651. if (!p->string)
  1652. return 0;
  1653. }
  1654. }
  1655. return bits;
  1656. }
  1657. /******************************************************************************/
  1658. SOAP_FMAC1
  1659. const char *
  1660. SOAP_FMAC2
  1661. soap_code_list(struct soap *soap, const struct soap_code_map *code_map, long code) {
  1662. char *t = soap->tmpbuf;
  1663. if (code_map) {
  1664. while (code_map->string) {
  1665. if ((code_map->code & code)) {
  1666. const char *s = code_map->string;
  1667. if (t != soap->tmpbuf)
  1668. *t++ = ' ';
  1669. while (*s && t < soap->tmpbuf + sizeof(soap->tmpbuf) - 1)
  1670. *t++ = *s++;
  1671. if (t == soap->tmpbuf + sizeof(soap->tmpbuf) - 1)
  1672. break;
  1673. }
  1674. code_map++;
  1675. }
  1676. }
  1677. *t = '\0';
  1678. return soap->tmpbuf;
  1679. }
  1680. /******************************************************************************/
  1681. SOAP_FMAC1
  1682. int
  1683. SOAP_FMAC2
  1684. soap_binary_search_string(const char **a, int n, const char *s) {
  1685. int min = 0, max = n - 1;
  1686. while (min <= max) {
  1687. int mid = (min + max) / 2;
  1688. int r = strcmp(s, a[mid]);
  1689. if (r < 0)
  1690. max = mid - 1;
  1691. else if (r > 0)
  1692. min = mid + 1;
  1693. else
  1694. return mid;
  1695. }
  1696. return -1;
  1697. }
  1698. /******************************************************************************/
  1699. static soap_wchar
  1700. soap_char(struct soap *soap) {
  1701. char tmp[8];
  1702. int i;
  1703. soap_wchar c;
  1704. char *s = tmp;
  1705. for (i = 0; i < (int) sizeof(tmp) - 1; i++) {
  1706. c = soap_get1(soap);
  1707. if (c == ';' || (int) c == EOF)
  1708. break;
  1709. *s++ = (char) c;
  1710. }
  1711. *s = '\0';
  1712. if (*tmp == '#') {
  1713. if (tmp[1] == 'x' || tmp[1] == 'X')
  1714. return (soap_wchar) soap_strtol(tmp + 2, NULL, 16);
  1715. return (soap_wchar) soap_strtol(tmp + 1, NULL, 10);
  1716. }
  1717. if (!strcmp(tmp, "lt"))
  1718. return '<';
  1719. if (!strcmp(tmp, "gt"))
  1720. return '>';
  1721. if (!strcmp(tmp, "amp"))
  1722. return '&';
  1723. if (!strcmp(tmp, "quot"))
  1724. return '"';
  1725. if (!strcmp(tmp, "apos"))
  1726. return '\'';
  1727. #ifndef WITH_LEAN
  1728. return (soap_wchar) soap_code_int(html_entity_codes, tmp, (LONG64) SOAP_UNKNOWN_CHAR);
  1729. #else
  1730. return SOAP_UNKNOWN_CHAR; /* use this to represent unknown code */
  1731. #endif
  1732. }
  1733. /******************************************************************************/
  1734. #ifdef WITH_LEAN
  1735. soap_wchar
  1736. soap_get0(struct soap *soap)
  1737. {
  1738. if (soap->bufidx >= soap->buflen && soap_recv(soap))
  1739. return EOF;
  1740. return (unsigned char)soap->buf[soap->bufidx];
  1741. }
  1742. #endif
  1743. /******************************************************************************/
  1744. #ifdef WITH_LEAN
  1745. soap_wchar
  1746. soap_get1(struct soap *soap)
  1747. {
  1748. if (soap->bufidx >= soap->buflen && soap_recv(soap))
  1749. return EOF;
  1750. return (unsigned char)soap->buf[soap->bufidx++];
  1751. }
  1752. #endif
  1753. /******************************************************************************/
  1754. SOAP_FMAC1
  1755. soap_wchar
  1756. SOAP_FMAC2
  1757. soap_get(struct soap *soap) {
  1758. soap_wchar c;
  1759. c = soap->ahead;
  1760. if (c) {
  1761. if ((int) c != EOF)
  1762. soap->ahead = 0;
  1763. } else {
  1764. c = soap_get1(soap);
  1765. }
  1766. while ((int) c != EOF) {
  1767. if (soap->cdata) {
  1768. if (c == ']') {
  1769. c = soap_get1(soap);
  1770. if (c == ']') {
  1771. c = soap_get0(soap);
  1772. if (c == '>') {
  1773. soap->cdata = 0;
  1774. c = soap_get1(soap);
  1775. c = soap_get1(soap);
  1776. } else {
  1777. soap_unget(soap, ']');
  1778. return ']';
  1779. }
  1780. } else {
  1781. soap_revget1(soap);
  1782. return ']';
  1783. }
  1784. } else {
  1785. return c;
  1786. }
  1787. }
  1788. switch (c) {
  1789. case '<':
  1790. do {
  1791. c = soap_get1(soap);
  1792. } while (soap_coblank(c));
  1793. if (c == '!' || c == '?' || c == '%') {
  1794. int k = 1;
  1795. if (c == '!') {
  1796. c = soap_get1(soap);
  1797. if (c == '[') {
  1798. do {
  1799. c = soap_get1(soap);
  1800. } while ((int) c != EOF && c != '[');
  1801. if ((int) c == EOF)
  1802. break;
  1803. soap->cdata = 1;
  1804. c = soap_get1(soap);
  1805. continue;
  1806. }
  1807. if (c == '-' && (c = soap_get1(soap)) == '-') {
  1808. do {
  1809. c = soap_get1(soap);
  1810. if (c == '-' && (c = soap_get1(soap)) == '-')
  1811. break;
  1812. } while ((int) c != EOF);
  1813. }
  1814. } else if (c == '?') {
  1815. c = soap_getpi(soap);
  1816. }
  1817. while ((int) c != EOF) {
  1818. if (c == '<') {
  1819. k++;
  1820. } else if (c == '>') {
  1821. if (--k <= 0)
  1822. break;
  1823. }
  1824. c = soap_get1(soap);
  1825. }
  1826. if ((int) c == EOF)
  1827. break;
  1828. c = soap_get1(soap);
  1829. continue;
  1830. }
  1831. if (c == '/')
  1832. return SOAP_TT;
  1833. soap_revget1(soap);
  1834. return SOAP_LT;
  1835. case '>':
  1836. return SOAP_GT;
  1837. case '"':
  1838. return SOAP_QT;
  1839. case '\'':
  1840. return SOAP_AP;
  1841. case '&':
  1842. return soap_char(soap) | 0x80000000;
  1843. }
  1844. break;
  1845. }
  1846. return c;
  1847. }
  1848. /******************************************************************************/
  1849. static soap_wchar
  1850. soap_getpi(struct soap *soap) {
  1851. char buf[64];
  1852. char *s = buf;
  1853. size_t i = sizeof(buf);
  1854. soap_wchar c;
  1855. while ((int) (c = soap_getchar(soap)) != EOF && c != '?') {
  1856. if (i > 1) {
  1857. if (soap_coblank(c))
  1858. c = ' ';
  1859. *s++ = (char) c;
  1860. i--;
  1861. }
  1862. }
  1863. *s = '\0';
  1864. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "XML PI <?%s?>\n", buf));
  1865. if (!strncmp(buf, "xml ", 4)) {
  1866. s = strstr(buf, " encoding=");
  1867. if (s && s[10]) {
  1868. if (!soap_tag_cmp(s + 11, "iso-8859-1*")
  1869. || !soap_tag_cmp(s + 11, "latin1*")) {
  1870. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Switching to latin1 encoding\n"));
  1871. soap->mode |= SOAP_ENC_LATIN;
  1872. } else if (!soap_tag_cmp(s + 11, "utf-8*")) {
  1873. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Switching to utf-8 encoding\n"));
  1874. soap->mode &= ~SOAP_ENC_LATIN;
  1875. }
  1876. }
  1877. }
  1878. if ((int) c != EOF)
  1879. c = soap_getchar(soap);
  1880. return c;
  1881. }
  1882. /******************************************************************************/
  1883. #ifndef WITH_LEANER
  1884. SOAP_FMAC1
  1885. int
  1886. SOAP_FMAC2
  1887. soap_move(struct soap *soap, ULONG64 n) {
  1888. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Moving "
  1889. SOAP_ULONG_FORMAT
  1890. " bytes forward\n", n));
  1891. for (; n; n--)
  1892. if ((int) soap_getchar(soap) == EOF)
  1893. return SOAP_EOF;
  1894. return SOAP_OK;
  1895. }
  1896. #endif
  1897. /******************************************************************************/
  1898. #ifndef WITH_LEANER
  1899. SOAP_FMAC1
  1900. ULONG64
  1901. SOAP_FMAC2
  1902. soap_tell(struct soap *soap) {
  1903. return soap->count - soap->buflen + soap->bufidx - (soap->ahead != 0);
  1904. }
  1905. #endif
  1906. /******************************************************************************/
  1907. SOAP_FMAC1
  1908. int
  1909. SOAP_FMAC2
  1910. soap_pututf8(struct soap *soap, unsigned long c) {
  1911. char tmp[24];
  1912. if ((c < 0x7F && c > 0x1F)) {
  1913. *tmp = (char) c;
  1914. return soap_send_raw(soap, tmp, 1);
  1915. }
  1916. #ifdef WITH_REPLACE_ILLEGAL_UTF8
  1917. if (!(c == 0x09 || c == 0x0A || c == 0x0D || (c >= 0x80 && c <= 0xD7FF) || (c >= 0xE000 && c <= 0xFFFD) || (c >= 0x10000 && c <= 0x10FFFF)))
  1918. c = SOAP_UNKNOWN_UNICODE_CHAR;
  1919. #endif
  1920. #ifndef WITH_LEAN
  1921. if (c > 0x9F) {
  1922. char *t = tmp;
  1923. if (c < 0x0800) {
  1924. *t++ = (char) (0xC0 | ((c >> 6) & 0x1F));
  1925. } else {
  1926. if (c < 0x010000) {
  1927. *t++ = (char) (0xE0 | ((c >> 12) & 0x0F));
  1928. } else {
  1929. if (c < 0x200000) {
  1930. *t++ = (char) (0xF0 | ((c >> 18) & 0x07));
  1931. } else {
  1932. if (c < 0x04000000) {
  1933. *t++ = (char) (0xF8 | ((c >> 24) & 0x03));
  1934. } else {
  1935. *t++ = (char) (0xFC | ((c >> 30) & 0x01));
  1936. *t++ = (char) (0x80 | ((c >> 24) & 0x3F));
  1937. }
  1938. *t++ = (char) (0x80 | ((c >> 18) & 0x3F));
  1939. }
  1940. *t++ = (char) (0x80 | ((c >> 12) & 0x3F));
  1941. }
  1942. *t++ = (char) (0x80 | ((c >> 6) & 0x3F));
  1943. }
  1944. *t++ = (char) (0x80 | (c & 0x3F));
  1945. *t = '\0';
  1946. } else
  1947. #endif
  1948. (SOAP_SNPRINTF(tmp, sizeof(tmp), 20), "&#x%lX;", c);
  1949. return soap_send(soap, tmp);
  1950. }
  1951. /******************************************************************************/
  1952. SOAP_FMAC1
  1953. soap_wchar
  1954. SOAP_FMAC2
  1955. soap_getutf8(struct soap *soap) {
  1956. #ifdef WITH_REPLACE_ILLEGAL_UTF8
  1957. soap_wchar c, c1, c2, c3;
  1958. #else
  1959. soap_wchar c, c1, c2, c3, c4;
  1960. #endif
  1961. c = soap->ahead;
  1962. if (c >= 0x80)
  1963. soap->ahead = 0;
  1964. else
  1965. c = (soap_wchar) soap_get(soap);
  1966. if (c < 0x80 || c > 0xFF || (soap->mode & SOAP_ENC_LATIN))
  1967. return c;
  1968. #ifdef WITH_REPLACE_ILLEGAL_UTF8
  1969. c1 = (soap_wchar)soap_get1(soap);
  1970. if (c <= 0xC1 || (c1 & 0xC0) != 0x80)
  1971. {
  1972. soap_revget1(soap);
  1973. return SOAP_UNKNOWN_UNICODE_CHAR;
  1974. }
  1975. c1 &= 0x3F;
  1976. if (c < 0xE0)
  1977. return (((c & 0x1F) << 6) | c1);
  1978. c2 = (soap_wchar)soap_get1(soap);
  1979. if ((c == 0xE0 && c1 < 0x20) || (c2 & 0xC0) != 0x80)
  1980. {
  1981. soap_revget1(soap);
  1982. return SOAP_UNKNOWN_UNICODE_CHAR;
  1983. }
  1984. c2 &= 0x3F;
  1985. if (c < 0xF0)
  1986. return (((c & 0x0F) << 12) | (c1 << 6) | c2);
  1987. c3 = (soap_wchar)soap_get1(soap);
  1988. if ((c == 0xF0 && c1 < 0x10) || (c == 0xF4 && c1 >= 0x10) || c >= 0xF5 || (c3 & 0xC0) != 0x80)
  1989. {
  1990. soap_revget1(soap);
  1991. return SOAP_UNKNOWN_UNICODE_CHAR;
  1992. }
  1993. return (((c & 0x07) << 18) | (c1 << 12) | (c2 << 6) | (c3 & 0x3F));
  1994. #else
  1995. c1 = (soap_wchar) soap_get1(soap);
  1996. if (c < 0xC0 || (c1 & 0xC0) != 0x80) {
  1997. soap_revget1(soap);
  1998. /* doesn't look like this is UTF-8, try continue as if ISO-8859-1 */
  1999. return c;
  2000. }
  2001. c1 &= 0x3F;
  2002. if (c < 0xE0)
  2003. return ((soap_wchar) (c & 0x1F) << 6) | c1;
  2004. c2 = (soap_wchar) soap_get1(soap) & 0x3F;
  2005. if (c < 0xF0)
  2006. return ((soap_wchar) (c & 0x0F) << 12) | (c1 << 6) | c2;
  2007. c3 = (soap_wchar) soap_get1(soap) & 0x3F;
  2008. if (c < 0xF8)
  2009. return ((soap_wchar) (c & 0x07) << 18) | (c1 << 12) | (c2 << 6) | c3;
  2010. c4 = (soap_wchar) soap_get1(soap) & 0x3F;
  2011. if (c < 0xFC)
  2012. return ((soap_wchar) (c & 0x03) << 24) | (c1 << 18) | (c2 << 12) | (c3 << 6) | c4;
  2013. return ((soap_wchar) (c & 0x01) << 30) | (c1 << 24) | (c2 << 18) | (c3 << 12) | (c4 << 6) |
  2014. (soap_wchar) (soap_get1(soap) & 0x3F);
  2015. #endif
  2016. }
  2017. /******************************************************************************/
  2018. SOAP_FMAC1
  2019. size_t
  2020. SOAP_FMAC2
  2021. soap_utf8len(const char *s) {
  2022. size_t l = 0;
  2023. while (*s)
  2024. if ((*s++ & 0xC0) != 0x80)
  2025. l++;
  2026. return l;
  2027. }
  2028. /******************************************************************************/
  2029. SOAP_FMAC1
  2030. int
  2031. SOAP_FMAC2
  2032. soap_puthex(struct soap *soap, const unsigned char *s, int n) {
  2033. char d[2 * SOAP_BINARY_BUFLEN], *p = d;
  2034. #ifdef WITH_DOM
  2035. if ((soap->mode & SOAP_XML_DOM) && soap->dom)
  2036. {
  2037. soap->dom->text = soap_s2hex(soap, s, NULL, n);
  2038. if (!soap->dom->text)
  2039. return soap->error;
  2040. return SOAP_OK;
  2041. }
  2042. #endif
  2043. for (; n > 0; n--) {
  2044. int m = *s++;
  2045. p[0] = (char) ((m >> 4) + (m > 159 ? '7' : '0'));
  2046. m &= 0x0F;
  2047. p[1] = (char) (m + (m > 9 ? '7' : '0'));
  2048. p += 2;
  2049. if (p - d == sizeof(d)) {
  2050. if (soap_send_raw(soap, d, sizeof(d)))
  2051. return soap->error;
  2052. p = d;
  2053. }
  2054. }
  2055. if (p != d && soap_send_raw(soap, d, p - d))
  2056. return soap->error;
  2057. return SOAP_OK;
  2058. }
  2059. /******************************************************************************/
  2060. SOAP_FMAC1
  2061. unsigned char *
  2062. SOAP_FMAC2
  2063. soap_gethex(struct soap *soap, int *n) {
  2064. size_t l = 0;
  2065. #ifdef WITH_DOM
  2066. if ((soap->mode & SOAP_XML_DOM) && soap->dom)
  2067. {
  2068. soap->dom->text = soap_string_in(soap, 0, -1, -1, NULL);
  2069. return (unsigned char*)soap_hex2s(soap, soap->dom->text, NULL, 0, n);
  2070. }
  2071. #endif
  2072. #ifdef WITH_FAST
  2073. soap->labidx = 0;
  2074. for (;;) {
  2075. char *s;
  2076. size_t i, k;
  2077. if (soap_append_lab(soap, NULL, 0))
  2078. return NULL;
  2079. s = soap->labbuf + soap->labidx;
  2080. k = soap->lablen - soap->labidx;
  2081. soap->labidx = soap->lablen;
  2082. for (i = 0; i < k; i++) {
  2083. char d1, d2;
  2084. soap_wchar c;
  2085. c = soap_get(soap);
  2086. if (soap_isxdigit(c)) {
  2087. d1 = (char) c;
  2088. c = soap_get(soap);
  2089. if (soap_isxdigit(c)) {
  2090. d2 = (char) c;
  2091. } else {
  2092. soap->error = SOAP_TYPE;
  2093. return NULL;
  2094. }
  2095. } else {
  2096. unsigned char *p = NULL;
  2097. l = soap->lablen + i - k;
  2098. soap_unget(soap, c);
  2099. if (n)
  2100. *n = (int) l;
  2101. if (soap->maxlength > 0 && l > (size_t) soap->maxlength) {
  2102. soap->error = SOAP_LENGTH;
  2103. } else {
  2104. p = (unsigned char *) soap_malloc(soap, l);
  2105. if (p)
  2106. (void) soap_memcpy((void *) p, l, (const void *) soap->labbuf, l);
  2107. }
  2108. return p;
  2109. }
  2110. *s++ = (char) (((d1 >= 'A' ? (d1 & 0x7) + 9 : d1 - '0') << 4) + (d2 >= 'A' ? (d2 & 0x7) + 9 : d2 - '0'));
  2111. }
  2112. l = soap->lablen;
  2113. if (soap->maxlength > 0 && l > (size_t) soap->maxlength) {
  2114. soap->error = SOAP_LENGTH;
  2115. return NULL;
  2116. }
  2117. }
  2118. #else
  2119. if (soap_alloc_block(soap) == NULL)
  2120. return NULL;
  2121. for (;;)
  2122. {
  2123. int i;
  2124. char *s = (char*)soap_push_block(soap, NULL, SOAP_BLKLEN);
  2125. if (!s)
  2126. {
  2127. soap_end_block(soap, NULL);
  2128. return NULL;
  2129. }
  2130. for (i = 0; i < SOAP_BLKLEN; i++)
  2131. {
  2132. char d1, d2;
  2133. soap_wchar c = soap_get(soap);
  2134. if (soap_isxdigit(c))
  2135. {
  2136. d1 = (char)c;
  2137. c = soap_get(soap);
  2138. if (soap_isxdigit(c))
  2139. d2 = (char)c;
  2140. else
  2141. {
  2142. soap_end_block(soap, NULL);
  2143. soap->error = SOAP_TYPE;
  2144. return NULL;
  2145. }
  2146. }
  2147. else
  2148. {
  2149. unsigned char *p;
  2150. soap_unget(soap, c);
  2151. if (soap->maxlength > 0 && l > (size_t)soap->maxlength)
  2152. {
  2153. soap->error = SOAP_LENGTH;
  2154. soap_end_block(soap, NULL);
  2155. return NULL;
  2156. }
  2157. if (n)
  2158. *n = (int)soap_size_block(soap, NULL, i);
  2159. p = (unsigned char*)soap_save_block(soap, NULL, NULL, 0);
  2160. return p;
  2161. }
  2162. *s++ = ((d1 >= 'A' ? (d1 & 0x7) + 9 : d1 - '0') << 4) + (d2 >= 'A' ? (d2 & 0x7) + 9 : d2 - '0');
  2163. l++;
  2164. }
  2165. if (soap->maxlength > 0 && l > (size_t)soap->maxlength)
  2166. {
  2167. soap->error = SOAP_LENGTH;
  2168. soap_end_block(soap, NULL);
  2169. return NULL;
  2170. }
  2171. }
  2172. #endif
  2173. }
  2174. /******************************************************************************/
  2175. SOAP_FMAC1
  2176. int
  2177. SOAP_FMAC2
  2178. soap_putbase64(struct soap *soap, const unsigned char *s, int n) {
  2179. char d[4 * SOAP_BINARY_BUFLEN], *p = d;
  2180. if (!s)
  2181. return SOAP_OK;
  2182. #ifdef WITH_DOM
  2183. if ((soap->mode & SOAP_XML_DOM) && soap->dom)
  2184. {
  2185. soap->dom->text = soap_s2base64(soap, s, NULL, n);
  2186. if (!soap->dom->text)
  2187. return soap->error;
  2188. return SOAP_OK;
  2189. }
  2190. #endif
  2191. for (; n > 2; n -= 3, s += 3) {
  2192. p[0] = soap_base64o[(s[0] & 0xFC) >> 2];
  2193. p[1] = soap_base64o[((s[0] & 0x03) << 4) | ((s[1] & 0xF0) >> 4)];
  2194. p[2] = soap_base64o[((s[1] & 0x0F) << 2) | ((s[2] & 0xC0) >> 6)];
  2195. p[3] = soap_base64o[s[2] & 0x3F];
  2196. p += 4;
  2197. if (p - d == sizeof(d)) {
  2198. if (soap_send_raw(soap, d, sizeof(d)))
  2199. return soap->error;
  2200. p = d;
  2201. }
  2202. }
  2203. if (n == 2) {
  2204. p[0] = soap_base64o[(s[0] & 0xFC) >> 2];
  2205. p[1] = soap_base64o[((s[0] & 0x03) << 4) | ((s[1] & 0xF0) >> 4)];
  2206. p[2] = soap_base64o[(s[1] & 0x0F) << 2];
  2207. p[3] = '=';
  2208. p += 4;
  2209. } else if (n == 1) {
  2210. p[0] = soap_base64o[(s[0] & 0xFC) >> 2];
  2211. p[1] = soap_base64o[(s[0] & 0x03) << 4];
  2212. p[2] = '=';
  2213. p[3] = '=';
  2214. p += 4;
  2215. }
  2216. if (p != d && soap_send_raw(soap, d, p - d))
  2217. return soap->error;
  2218. return SOAP_OK;
  2219. }
  2220. /******************************************************************************/
  2221. SOAP_FMAC1
  2222. unsigned char *
  2223. SOAP_FMAC2
  2224. soap_getbase64(struct soap *soap, int *n, int malloc_flag) {
  2225. size_t l = 0;
  2226. (void) malloc_flag;
  2227. #ifdef WITH_DOM
  2228. if ((soap->mode & SOAP_XML_DOM) && soap->dom)
  2229. {
  2230. soap->dom->text = soap_string_in(soap, 0, -1, -1, NULL);
  2231. return (unsigned char*)soap_base642s(soap, soap->dom->text, NULL, 0, n);
  2232. }
  2233. #endif
  2234. #ifdef WITH_FAST
  2235. soap->labidx = 0;
  2236. for (;;) {
  2237. size_t i, k;
  2238. char *s;
  2239. if (soap_append_lab(soap, NULL, 2))
  2240. return NULL;
  2241. s = soap->labbuf + soap->labidx;
  2242. k = soap->lablen - soap->labidx;
  2243. soap->labidx = 3 * (soap->lablen / 3);
  2244. if (k > 2) {
  2245. for (i = 0; i < k - 2; i += 3) {
  2246. unsigned long m = 0;
  2247. int j = 0;
  2248. do {
  2249. soap_wchar c = soap_get(soap);
  2250. if (c < SOAP_AP)
  2251. c &= 0x7FFFFFFF;
  2252. if (c == '=' || c < 0) {
  2253. unsigned char *p = NULL;
  2254. switch (j) {
  2255. case 2:
  2256. *s++ = (char) ((m >> 4) & 0xFF);
  2257. i++;
  2258. break;
  2259. case 3:
  2260. *s++ = (char) ((m >> 10) & 0xFF);
  2261. *s++ = (char) ((m >> 2) & 0xFF);
  2262. i += 2;
  2263. }
  2264. l = soap->lablen + i - k;
  2265. if (n)
  2266. *n = (int) l;
  2267. if (soap->maxlength > 0 && l > (size_t) soap->maxlength)
  2268. soap->error = SOAP_LENGTH;
  2269. else {
  2270. p = (unsigned char *) soap_malloc(soap, l);
  2271. if (p)
  2272. (void) soap_memcpy((void *) p, l, (const void *) soap->labbuf, l);
  2273. }
  2274. if (c >= 0) {
  2275. while ((int) ((c = soap_get(soap)) != EOF) && c != SOAP_LT && c != SOAP_TT)
  2276. continue;
  2277. }
  2278. soap_unget(soap, c);
  2279. return p;
  2280. }
  2281. c -= '+';
  2282. if (c >= 0 && c <= 79) {
  2283. int b = soap_base64i[c];
  2284. if (b >= 64) {
  2285. soap->error = SOAP_TYPE;
  2286. return NULL;
  2287. }
  2288. m = (m << 6) + b;
  2289. j++;
  2290. } else if (!soap_coblank(c + '+')) {
  2291. soap->error = SOAP_TYPE;
  2292. return NULL;
  2293. }
  2294. } while (j < 4);
  2295. *s++ = (char) ((m >> 16) & 0xFF);
  2296. *s++ = (char) ((m >> 8) & 0xFF);
  2297. *s++ = (char) (m & 0xFF);
  2298. }
  2299. l = soap->lablen;
  2300. if (soap->maxlength > 0 && l > (size_t) soap->maxlength) {
  2301. soap->error = SOAP_LENGTH;
  2302. return NULL;
  2303. }
  2304. }
  2305. }
  2306. #else
  2307. if (soap_alloc_block(soap) == NULL)
  2308. return NULL;
  2309. for (;;)
  2310. {
  2311. int i;
  2312. char *s = (char*)soap_push_block(soap, NULL, 3 * SOAP_BLKLEN); /* must be multiple of 3 */
  2313. if (!s)
  2314. {
  2315. soap_end_block(soap, NULL);
  2316. return NULL;
  2317. }
  2318. for (i = 0; i < SOAP_BLKLEN; i++)
  2319. {
  2320. unsigned long m = 0;
  2321. int j = 0;
  2322. do
  2323. {
  2324. soap_wchar c = soap_get(soap);
  2325. if (c < SOAP_AP)
  2326. c &= 0x7FFFFFFF;
  2327. if (c == '=' || c < 0)
  2328. {
  2329. unsigned char *p;
  2330. i *= 3;
  2331. switch (j)
  2332. {
  2333. case 2:
  2334. *s++ = (char)((m >> 4) & 0xFF);
  2335. i++;
  2336. l++;
  2337. break;
  2338. case 3:
  2339. *s++ = (char)((m >> 10) & 0xFF);
  2340. *s++ = (char)((m >> 2) & 0xFF);
  2341. l += 2;
  2342. i += 2;
  2343. }
  2344. if (n)
  2345. *n = (int)soap_size_block(soap, NULL, i);
  2346. if (soap->maxlength > 0 && l > (size_t)soap->maxlength)
  2347. {
  2348. soap->error = SOAP_LENGTH;
  2349. soap_end_block(soap, NULL);
  2350. return NULL;
  2351. }
  2352. p = (unsigned char*)soap_save_block(soap, NULL, NULL, 0);
  2353. if (c >= 0)
  2354. {
  2355. while ((int)((c = soap_get(soap)) != EOF) && c != SOAP_LT && c != SOAP_TT)
  2356. continue;
  2357. }
  2358. soap_unget(soap, c);
  2359. return p;
  2360. }
  2361. c -= '+';
  2362. if (c >= 0 && c <= 79)
  2363. {
  2364. int b = soap_base64i[c];
  2365. if (b >= 64)
  2366. {
  2367. soap->error = SOAP_TYPE;
  2368. return NULL;
  2369. }
  2370. m = (m << 6) + b;
  2371. j++;
  2372. }
  2373. else if (!soap_coblank(c + '+'))
  2374. {
  2375. soap->error = SOAP_TYPE;
  2376. return NULL;
  2377. }
  2378. } while (j < 4);
  2379. *s++ = (char)((m >> 16) & 0xFF);
  2380. *s++ = (char)((m >> 8) & 0xFF);
  2381. *s++ = (char)(m & 0xFF);
  2382. l += 3;
  2383. }
  2384. if (soap->maxlength > 0 && l > (size_t)soap->maxlength)
  2385. {
  2386. soap->error = SOAP_LENGTH;
  2387. soap_end_block(soap, NULL);
  2388. return NULL;
  2389. }
  2390. }
  2391. #endif
  2392. }
  2393. /******************************************************************************/
  2394. #ifndef WITH_LEANER
  2395. SOAP_FMAC1
  2396. int
  2397. SOAP_FMAC2
  2398. soap_xop_forward(struct soap *soap, unsigned char **ptr, int *size, char **id, char **type, char **options) {
  2399. /* Check MTOM xop:Include element (within hex/base64Binary) */
  2400. /* TODO: this code to be obsoleted with new import/xop.h conventions */
  2401. short body = soap->body; /* should save type too? */
  2402. if (!soap_peek_element(soap)) {
  2403. if (!soap_element_begin_in(soap, ":Include", 0, NULL)) {
  2404. if (soap_attachment_forward(soap, ptr, size, id, type, options)
  2405. || (soap->body && soap_element_end_in(soap, ":Include")))
  2406. return soap->error;
  2407. } else if (soap->error == SOAP_TAG_MISMATCH)
  2408. soap_retry(soap);
  2409. else
  2410. return soap->error;
  2411. }
  2412. soap->body = body;
  2413. return SOAP_OK;
  2414. }
  2415. #endif
  2416. /******************************************************************************/
  2417. #ifndef WITH_LEANER
  2418. SOAP_FMAC1
  2419. int
  2420. SOAP_FMAC2
  2421. soap_attachment_forward(struct soap *soap, unsigned char **ptr, int *size, char **id, char **type, char **options) {
  2422. struct soap_xlist *xp;
  2423. *ptr = NULL;
  2424. *size = 0;
  2425. *id = NULL;
  2426. *type = NULL;
  2427. *options = NULL;
  2428. if (!*soap->href)
  2429. return SOAP_OK;
  2430. *id = soap_strdup(soap, soap->href);
  2431. xp = (struct soap_xlist *) SOAP_MALLOC(soap, sizeof(struct soap_xlist));
  2432. if (!xp)
  2433. return soap->error = SOAP_EOM;
  2434. xp->next = soap->xlist;
  2435. xp->ptr = ptr;
  2436. xp->size = size;
  2437. xp->id = *id;
  2438. xp->type = type;
  2439. xp->options = options;
  2440. soap->xlist = xp;
  2441. return SOAP_OK;
  2442. }
  2443. #endif
  2444. /******************************************************************************/
  2445. SOAP_FMAC1
  2446. void *
  2447. SOAP_FMAC2
  2448. soap_memdup(struct soap *soap, const void *s, size_t n) {
  2449. void *t = NULL;
  2450. if (s) {
  2451. t = soap_malloc(soap, n);
  2452. if (t)
  2453. (void) soap_memcpy(t, n, s, n);
  2454. }
  2455. return t;
  2456. }
  2457. /******************************************************************************/
  2458. SOAP_FMAC1
  2459. char *
  2460. SOAP_FMAC2
  2461. soap_strdup(struct soap *soap, const char *s) {
  2462. char *t = NULL;
  2463. if (s) {
  2464. size_t n = strlen(s) + 1;
  2465. if (n > 0) {
  2466. t = (char *) soap_malloc(soap, n);
  2467. if (t) {
  2468. (void) soap_memcpy((void *) t, n, (const void *) s, n);
  2469. t[n - 1] = '\0';
  2470. }
  2471. }
  2472. }
  2473. return t;
  2474. }
  2475. /******************************************************************************/
  2476. SOAP_FMAC1
  2477. wchar_t *
  2478. SOAP_FMAC2
  2479. soap_wstrdup(struct soap *soap, const wchar_t *s) {
  2480. wchar_t *t = NULL;
  2481. if (s) {
  2482. size_t n = 0, m;
  2483. while (s[n])
  2484. n++;
  2485. n++;
  2486. m = sizeof(wchar_t) * n;
  2487. if (n > 0) {
  2488. t = (wchar_t *) soap_malloc(soap, m);
  2489. if (t) {
  2490. (void) soap_memcpy((void *) t, m, (const void *) s, m);
  2491. t[n - 1] = L'\0';
  2492. }
  2493. }
  2494. }
  2495. return t;
  2496. }
  2497. /******************************************************************************/
  2498. SOAP_FMAC1
  2499. char *
  2500. SOAP_FMAC2
  2501. soap_strtrim(struct soap *soap, char *s) {
  2502. (void) soap;
  2503. if (s) {
  2504. char *t;
  2505. while ((*s >= 9 && *s <= 13) || *s == 32)
  2506. s++;
  2507. t = s;
  2508. while (*t)
  2509. t++;
  2510. while (--t > s && ((*t >= 9 && *t <= 13) || *t == 32))
  2511. continue;
  2512. t[1] = '\0';
  2513. }
  2514. return s;
  2515. }
  2516. /******************************************************************************/
  2517. SOAP_FMAC1
  2518. wchar_t *
  2519. SOAP_FMAC2
  2520. soap_wstrtrim(struct soap *soap, wchar_t *s) {
  2521. (void) soap;
  2522. if (s) {
  2523. wchar_t *t;
  2524. while ((*s >= 9 && *s <= 13) || *s == 32)
  2525. s++;
  2526. t = s;
  2527. while (*t)
  2528. t++;
  2529. while (--t > s && ((*t >= 9 && *t <= 13) || *t == 32))
  2530. continue;
  2531. t[1] = L'\0';
  2532. }
  2533. return s;
  2534. }
  2535. /******************************************************************************/
  2536. SOAP_FMAC1
  2537. struct soap_blist *
  2538. SOAP_FMAC2
  2539. soap_alloc_block(struct soap *soap) {
  2540. struct soap_blist *p;
  2541. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "New block sequence (prev=%p)\n", (void *) soap->blist));
  2542. p = (struct soap_blist *) SOAP_MALLOC(soap, sizeof(struct soap_blist));
  2543. if (!p) {
  2544. soap->error = SOAP_EOM;
  2545. return NULL;
  2546. }
  2547. p->next = soap->blist;
  2548. p->head = NULL;
  2549. p->size = 0;
  2550. p->item = 0;
  2551. soap->blist = p;
  2552. return p;
  2553. }
  2554. /******************************************************************************/
  2555. SOAP_FMAC1
  2556. void *
  2557. SOAP_FMAC2
  2558. soap_push_block(struct soap *soap, struct soap_blist *b, size_t n) {
  2559. struct soap_bhead *p;
  2560. if (!b)
  2561. b = soap->blist;
  2562. if (!b
  2563. || b->size + n < b->size
  2564. || sizeof(struct soap_bhead) + n < n
  2565. || (SOAP_MAXALLOCSIZE > 0 && sizeof(struct soap_bhead) + n > SOAP_MAXALLOCSIZE)) {
  2566. soap->error = SOAP_EOM;
  2567. return NULL;
  2568. }
  2569. p = (struct soap_bhead *) SOAP_MALLOC(soap, sizeof(struct soap_bhead) + n);
  2570. if (!p) {
  2571. soap->error = SOAP_EOM;
  2572. return NULL;
  2573. }
  2574. p->next = b->head;
  2575. b->head = p;
  2576. p->size = n;
  2577. b->size += n;
  2578. DBGLOG(TEST,
  2579. SOAP_MESSAGE(fdebug, "Push block %p of %u bytes on %lu previous blocks (%lu bytes total)\n", (void *) p,
  2580. (unsigned int) n, (unsigned long) b->item, (unsigned long) b->size));
  2581. b->item++;
  2582. return (void *) (p + 1); /* skip block header and point to n allocated bytes */
  2583. }
  2584. /******************************************************************************/
  2585. SOAP_FMAC1
  2586. void *
  2587. SOAP_FMAC2
  2588. soap_push_block_max(struct soap *soap, struct soap_blist *b, size_t n) {
  2589. if (b && b->item >= soap->maxoccurs) /* restrict block array length */
  2590. {
  2591. soap->error = SOAP_OCCURS;
  2592. return NULL;
  2593. }
  2594. return soap_push_block(soap, b, n);
  2595. }
  2596. /******************************************************************************/
  2597. SOAP_FMAC1
  2598. void
  2599. SOAP_FMAC2
  2600. soap_pop_block(struct soap *soap, struct soap_blist *b) {
  2601. struct soap_bhead *p;
  2602. if (!b)
  2603. b = soap->blist;
  2604. if (!b || !b->head)
  2605. return;
  2606. p = b->head;
  2607. b->size -= p->size;
  2608. b->head = p->next;
  2609. b->item--;
  2610. DBGLOG(TEST,
  2611. SOAP_MESSAGE(fdebug, "Pop block %p (%lu items of %lu bytes total)\n", (void *) p, (unsigned long) b->item,
  2612. (unsigned long) b->size));
  2613. SOAP_FREE(soap, p);
  2614. }
  2615. /******************************************************************************/
  2616. SOAP_FMAC1
  2617. void
  2618. SOAP_FMAC2
  2619. soap_update_pointers(struct soap *soap, const char *dst, const char *src, size_t len) {
  2620. const void *start = src, *end = src + len;
  2621. #ifndef WITH_LEANER
  2622. struct soap_xlist *xp;
  2623. #endif
  2624. #ifndef WITH_NOIDREF
  2625. if ((soap->version && !(soap->imode & SOAP_XML_TREE)) || (soap->mode & SOAP_XML_GRAPH)) {
  2626. int i;
  2627. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Update pointers %p (%lu bytes) -> %p\n", (void *) src, (unsigned long) len,
  2628. (void *) dst));
  2629. for (i = 0; i < SOAP_IDHASH; i++) {
  2630. struct soap_ilist *ip;
  2631. for (ip = soap->iht[i]; ip; ip = ip->next) {
  2632. struct soap_flist *fp;
  2633. void *p, **q;
  2634. if (ip->shaky) {
  2635. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Update shaky id='%s'\n", ip->id));
  2636. if (ip->ptr && ip->ptr >= start && ip->ptr < end) {
  2637. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Update ptr %p -> %p\n", ip->ptr,
  2638. (void *) ((const char *) ip->ptr + (dst - src))));
  2639. ip->ptr = (void *) ((const char *) ip->ptr + (dst - src));
  2640. }
  2641. for (q = &ip->link; q; q = (void **) p) {
  2642. p = *q;
  2643. if (p && p >= start && p < end) {
  2644. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Link update id='%s' %p -> %p\n", ip->id, p,
  2645. (void *) ((const char *) p + (dst - src))));
  2646. *q = (void *) ((const char *) p + (dst - src));
  2647. }
  2648. }
  2649. for (q = &ip->copy; q; q = (void **) p) {
  2650. p = *q;
  2651. if (p && p >= start && p < end) {
  2652. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Copy chain update id='%s' %p -> %p\n", ip->id, p,
  2653. (void *) ((const char *) p + (dst - src))));
  2654. *q = (void *) ((const char *) p + (dst - src));
  2655. }
  2656. }
  2657. for (fp = ip->flist; fp; fp = fp->next) {
  2658. if (fp->ptr >= start && fp->ptr < end) {
  2659. DBGLOG(TEST,
  2660. SOAP_MESSAGE(fdebug, "Copy list update id='%s' target type=%d %p -> %p\n", ip->id,
  2661. fp->type, fp->ptr, (void *) ((char *) fp->ptr + (dst - src))));
  2662. fp->ptr = (void *) ((const char *) fp->ptr + (dst - src));
  2663. }
  2664. }
  2665. if (ip->smart && ip->smart >= start && ip->smart < end) {
  2666. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Smart shared pointer update %p -> %p\n", ip->smart,
  2667. (void *) ((const char *) ip->smart + (dst - src))));
  2668. ip->smart = (void *) ((const char *) ip->smart + (dst - src));
  2669. }
  2670. }
  2671. }
  2672. }
  2673. }
  2674. #else
  2675. (void)soap; (void)start; (void)end; (void)dst; (void)src;
  2676. #endif
  2677. #ifndef WITH_LEANER
  2678. for (xp = soap->xlist; xp; xp = xp->next) {
  2679. if (xp->ptr && (void *) xp->ptr >= start && (void *) xp->ptr < end) {
  2680. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Update attachment id='%s' %p -> %p\n", xp->id ? xp->id : SOAP_STR_EOS,
  2681. (void *) xp->ptr, (void *) ((char *) xp->ptr + (dst - src))));
  2682. xp->ptr = (unsigned char **) ((char *) xp->ptr + (dst - src));
  2683. xp->size = (int *) ((char *) xp->size + (dst - src));
  2684. xp->type = (char **) ((char *) xp->type + (dst - src));
  2685. xp->options = (char **) ((char *) xp->options + (dst - src));
  2686. }
  2687. }
  2688. #endif
  2689. }
  2690. /******************************************************************************/
  2691. #ifndef WITH_NOIDREF
  2692. static int
  2693. soap_has_copies(struct soap *soap, const char *start, const char *end) {
  2694. int i;
  2695. struct soap_ilist *ip = NULL;
  2696. struct soap_flist *fp = NULL;
  2697. const char *p;
  2698. for (i = 0; i < SOAP_IDHASH; i++) {
  2699. for (ip = soap->iht[i]; ip; ip = ip->next) {
  2700. for (p = (const char *) ip->copy; p; p = *(const char **) p)
  2701. if (p >= start && p < end)
  2702. return SOAP_ERR;
  2703. for (fp = ip->flist; fp; fp = fp->next)
  2704. if (fp->type == ip->type && (const char *) fp->ptr >= start && (const char *) fp->ptr < end)
  2705. return SOAP_ERR;
  2706. }
  2707. }
  2708. return SOAP_OK;
  2709. }
  2710. #endif
  2711. /******************************************************************************/
  2712. #ifndef WITH_NOIDREF
  2713. SOAP_FMAC1
  2714. int
  2715. SOAP_FMAC2
  2716. soap_resolve(struct soap *soap) {
  2717. int i;
  2718. short flag;
  2719. const char *id;
  2720. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Resolving forwarded refs\n"));
  2721. for (i = 0; i < SOAP_IDHASH; i++) {
  2722. struct soap_ilist *ip;
  2723. for (ip = soap->iht[i]; ip; ip = ip->next) {
  2724. if (ip->ptr) {
  2725. void **q;
  2726. struct soap_flist *fp, **fpp = &ip->flist;
  2727. if (ip->spine)
  2728. ip->spine[0] = ip->ptr;
  2729. q = (void **) ip->link;
  2730. ip->link = NULL;
  2731. DBGLOG(TEST, if (q)
  2732. SOAP_MESSAGE(fdebug, "Traversing link chain to resolve id='%s' type=%d\n", ip->id, ip->type));
  2733. while (q) {
  2734. void *p = *q;
  2735. *q = ip->ptr;
  2736. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "... link %p -> %p\n", (void *) q, ip->ptr));
  2737. q = (void **) p;
  2738. }
  2739. while ((fp = *fpp)) {
  2740. if (fp->level > 0 && fp->finsert) {
  2741. DBGLOG(TEST,
  2742. SOAP_MESSAGE(fdebug, "... insert type=%d link %p -> %p\n", fp->type, fp->ptr, ip->ptr));
  2743. if (ip->spine && fp->level <= SOAP_MAXPTRS)
  2744. fp->finsert(soap, ip->type, fp->type, fp->ptr, fp->index, &ip->spine[fp->level - 1],
  2745. &ip->smart);
  2746. else if (fp->level == 1)
  2747. fp->finsert(soap, ip->type, fp->type, fp->ptr, fp->index, &ip->ptr, &ip->smart);
  2748. else if (fp->level <= SOAP_MAXPTRS) {
  2749. int i;
  2750. ip->spine = (void **) soap_malloc(soap, SOAP_MAXPTRS * sizeof(void *));
  2751. if (!ip->spine)
  2752. return soap->error = SOAP_EOM;
  2753. ip->spine[0] = ip->ptr;
  2754. for (i = 1; i < SOAP_MAXPTRS; i++)
  2755. ip->spine[i] = &ip->spine[i - 1];
  2756. fp->finsert(soap, ip->type, fp->type, fp->ptr, fp->index, &ip->spine[fp->level - 1],
  2757. &ip->smart);
  2758. }
  2759. *fpp = fp->next;
  2760. SOAP_FREE(soap, fp);
  2761. } else
  2762. fpp = &fp->next;
  2763. }
  2764. } else if (*ip->id == '#') {
  2765. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Missing id='%s'\n", ip->id));
  2766. soap_strcpy(soap->id, sizeof(soap->id), ip->id + 1);
  2767. return soap->error = SOAP_MISSING_ID;
  2768. }
  2769. }
  2770. }
  2771. do {
  2772. flag = 0;
  2773. id = NULL;
  2774. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Resolution phase\n"));
  2775. for (i = 0; i < SOAP_IDHASH; i++) {
  2776. struct soap_ilist *ip;
  2777. for (ip = soap->iht[i]; ip; ip = ip->next) {
  2778. if (ip->copy || ip->flist) {
  2779. if (ip->ptr && !soap_has_copies(soap, (const char *) ip->ptr, (const char *) ip->ptr + ip->size)) {
  2780. struct soap_flist *fp;
  2781. DBGLOG(TEST,
  2782. SOAP_MESSAGE(fdebug, "Resolving id='%s' type=%d ptr=%p size=%lu ...\n", ip->id, ip->type,
  2783. ip->ptr, (unsigned long) ip->size));
  2784. if (ip->copy) {
  2785. void *p, **q = (void **) ip->copy;
  2786. DBGLOG(TEST,
  2787. if (q) SOAP_MESSAGE(fdebug, "Traversing copy chain to resolve id='%s'\n", ip->id));
  2788. ip->copy = NULL;
  2789. do {
  2790. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "... copy %p -> %p (%u bytes)\n", ip->ptr, (void *) q,
  2791. (unsigned int) ip->size));
  2792. p = *q;
  2793. (void) soap_memcpy((void *) q, ip->size, (const void *) ip->ptr, ip->size);
  2794. q = (void **) p;
  2795. } while (q);
  2796. flag = 1;
  2797. }
  2798. while ((fp = ip->flist)) {
  2799. DBGLOG(TEST, SOAP_MESSAGE(fdebug,
  2800. "Resolving forwarded data type=%d target type=%d location=%p level=%u id='%s'\n",
  2801. ip->type, fp->type, ip->ptr, fp->level, ip->id));
  2802. if (fp->level == 0) {
  2803. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "... copy %p -> %p (%lu bytes)\n", ip->ptr, fp->ptr,
  2804. (unsigned long) ip->size));
  2805. if (fp->finsert)
  2806. fp->finsert(soap, ip->type, fp->type, fp->ptr, fp->index, ip->ptr, &ip->smart);
  2807. else
  2808. (void) soap_memcpy((void *) fp->ptr, ip->size, (const void *) ip->ptr, ip->size);
  2809. }
  2810. ip->flist = fp->next;
  2811. SOAP_FREE(soap, fp);
  2812. flag = 1;
  2813. }
  2814. } else if (*ip->id == '#')
  2815. id = ip->id;
  2816. }
  2817. }
  2818. }
  2819. } while (flag);
  2820. if (id) {
  2821. DBGLOG(TEST, SOAP_MESSAGE(fdebug,
  2822. "Resolution error: forwarded data for id='%s' could not be propagated, please report this problem to the gSOAP developers\n",
  2823. id));
  2824. return soap_id_nullify(soap, id);
  2825. }
  2826. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Resolution done\n"));
  2827. return SOAP_OK;
  2828. }
  2829. #endif
  2830. /******************************************************************************/
  2831. SOAP_FMAC1
  2832. size_t
  2833. SOAP_FMAC2
  2834. soap_size_block(struct soap *soap, struct soap_blist *b, size_t n) {
  2835. if (!b)
  2836. b = soap->blist;
  2837. if (b->head) {
  2838. b->size -= b->head->size - n;
  2839. b->head->size = n;
  2840. }
  2841. return b->size;
  2842. }
  2843. /******************************************************************************/
  2844. SOAP_FMAC1
  2845. char *
  2846. SOAP_FMAC2
  2847. soap_first_block(struct soap *soap, struct soap_blist *b) {
  2848. struct soap_bhead *p, *q, *r;
  2849. if (!b)
  2850. b = soap->blist;
  2851. p = b->head;
  2852. if (!p)
  2853. return NULL;
  2854. r = NULL;
  2855. do {
  2856. q = p->next;
  2857. p->next = r;
  2858. r = p;
  2859. p = q;
  2860. } while (p);
  2861. b->head = r;
  2862. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "First block %p\n", (void *) (r + 1)));
  2863. return (char *) (r + 1);
  2864. }
  2865. /******************************************************************************/
  2866. SOAP_FMAC1
  2867. char *
  2868. SOAP_FMAC2
  2869. soap_next_block(struct soap *soap, struct soap_blist *b) {
  2870. struct soap_bhead *p;
  2871. if (!b)
  2872. b = soap->blist;
  2873. p = b->head;
  2874. if (p) {
  2875. b->head = p->next;
  2876. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Next block %p, deleting current block\n",
  2877. (void *) (b->head ? b->head + 1 : NULL)));
  2878. SOAP_FREE(soap, p);
  2879. if (b->head)
  2880. return (char *) (b->head + 1);
  2881. }
  2882. return NULL;
  2883. }
  2884. /******************************************************************************/
  2885. SOAP_FMAC1
  2886. size_t
  2887. SOAP_FMAC2
  2888. soap_block_size(struct soap *soap, struct soap_blist *b) {
  2889. if (!b)
  2890. b = soap->blist;
  2891. return b->head->size;
  2892. }
  2893. /******************************************************************************/
  2894. SOAP_FMAC1
  2895. void
  2896. SOAP_FMAC2
  2897. soap_end_block(struct soap *soap, struct soap_blist *b) {
  2898. struct soap_bhead *p, *q;
  2899. if (!b)
  2900. b = soap->blist;
  2901. if (b) {
  2902. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "End of block sequence, free all remaining blocks\n"));
  2903. for (p = b->head; p; p = q) {
  2904. q = p->next;
  2905. SOAP_FREE(soap, p);
  2906. }
  2907. if (soap->blist == b)
  2908. soap->blist = b->next;
  2909. else {
  2910. struct soap_blist *bp;
  2911. for (bp = soap->blist; bp; bp = bp->next) {
  2912. if (bp->next == b) {
  2913. bp->next = b->next;
  2914. break;
  2915. }
  2916. }
  2917. }
  2918. SOAP_FREE(soap, b);
  2919. }
  2920. DBGLOG(TEST, if (soap->blist) SOAP_MESSAGE(fdebug, "Restored previous block sequence\n"));
  2921. #ifndef WITH_NOIDREF
  2922. if (!soap->blist && ((soap->version && !(soap->imode & SOAP_XML_TREE)) || (soap->mode & SOAP_XML_GRAPH))) {
  2923. int i;
  2924. struct soap_ilist *ip = NULL;
  2925. for (i = 0; i < SOAP_IDHASH; i++)
  2926. for (ip = soap->iht[i]; ip; ip = ip->next)
  2927. ip->shaky = 0;
  2928. }
  2929. #endif
  2930. }
  2931. /******************************************************************************/
  2932. SOAP_FMAC1
  2933. char *
  2934. SOAP_FMAC2
  2935. soap_save_block(struct soap *soap, struct soap_blist *b, char *p, int flag) {
  2936. size_t n;
  2937. char *q, *s;
  2938. if (!b)
  2939. b = soap->blist;
  2940. if (b->size) {
  2941. if (!p)
  2942. p = (char *) soap_malloc(soap, b->size);
  2943. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Save all %lu blocks in contiguous memory space of %u bytes (%p->%p)\n",
  2944. (unsigned long) b->item, (unsigned int) b->size, (void *) b->head, (void *) p));
  2945. if (p) {
  2946. s = p;
  2947. for (q = soap_first_block(soap, b); q; q = soap_next_block(soap, b)) {
  2948. n = soap_block_size(soap, b);
  2949. DBGLOG(TEST,
  2950. SOAP_MESSAGE(fdebug, "Copy %u bytes from %p to %p\n", (unsigned int) n, (void *) q, (void *) s));
  2951. if (flag)
  2952. soap_update_pointers(soap, s, q, n);
  2953. (void) soap_memcpy((void *) s, n, (const void *) q, n);
  2954. s += n;
  2955. }
  2956. } else
  2957. soap->error = SOAP_EOM;
  2958. }
  2959. soap_end_block(soap, b);
  2960. return p;
  2961. }
  2962. /******************************************************************************/
  2963. SOAP_FMAC1
  2964. char *
  2965. SOAP_FMAC2
  2966. soap_putsizesoffsets(struct soap *soap, const char *type, const int *size, const int *offset, int dim) {
  2967. int i;
  2968. const char *t = ",%d";
  2969. if (!type)
  2970. return NULL;
  2971. if (soap->version == 2)
  2972. t = " %d";
  2973. if (soap->version != 2 && offset) {
  2974. (SOAP_SNPRINTF(soap->type, sizeof(soap->type) - 1, strlen(type) + 20), "%s[%d", type, size[0] + offset[0]);
  2975. for (i = 1; i < dim; i++) {
  2976. size_t l = strlen(soap->type);
  2977. (SOAP_SNPRINTF(soap->type + l, sizeof(soap->type) - l - 1, 20), t, size[i] + offset[i]);
  2978. }
  2979. } else {
  2980. (SOAP_SNPRINTF(soap->type, sizeof(soap->type) - 1, strlen(type) + 20), "%s[%d", type, size[0]);
  2981. for (i = 1; i < dim; i++) {
  2982. size_t l = strlen(soap->type);
  2983. (SOAP_SNPRINTF(soap->type + l, sizeof(soap->type) - l - 1, 20), t, size[i]);
  2984. }
  2985. }
  2986. soap_strcat(soap->type, sizeof(soap->type), "]");
  2987. return soap->type;
  2988. }
  2989. /******************************************************************************/
  2990. SOAP_FMAC1
  2991. char *
  2992. SOAP_FMAC2
  2993. soap_putoffsets(struct soap *soap, const int *offset, int dim) {
  2994. int i;
  2995. soap->arrayOffset[0] = '\0';
  2996. if (soap->version == 1) {
  2997. (SOAP_SNPRINTF(soap->arrayOffset, sizeof(soap->arrayOffset) - 1, 20), "[%d", offset[0]);
  2998. for (i = 1; i < dim; i++) {
  2999. size_t l = strlen(soap->arrayOffset);
  3000. (SOAP_SNPRINTF(soap->arrayOffset + l, sizeof(soap->arrayOffset) - l - 1, 20), ",%d", offset[i]);
  3001. }
  3002. soap_strcat(soap->arrayOffset, sizeof(soap->arrayOffset), "]");
  3003. }
  3004. return soap->arrayOffset;
  3005. }
  3006. /******************************************************************************/
  3007. SOAP_FMAC1
  3008. size_t
  3009. SOAP_FMAC2
  3010. soap_size(const int *size, int dim) {
  3011. int i;
  3012. size_t n = 0;
  3013. if (size[0] <= 0)
  3014. return 0;
  3015. n = (size_t) size[0];
  3016. for (i = 1; i < dim; i++) {
  3017. if (size[i] <= 0)
  3018. return 0;
  3019. n *= (size_t) size[i];
  3020. }
  3021. return (size_t) n;
  3022. }
  3023. /******************************************************************************/
  3024. SOAP_FMAC1
  3025. size_t
  3026. SOAP_FMAC2
  3027. soap_getsizes(const char *attr, int *size, int dim) {
  3028. size_t i, k, n;
  3029. if (!*attr)
  3030. return 0;
  3031. i = strlen(attr);
  3032. n = 1;
  3033. do {
  3034. for (; i > 0; i--)
  3035. if (attr[i - 1] == '[' || attr[i - 1] == ',' || attr[i - 1] == ' ')
  3036. break;
  3037. n *= k = (size_t) soap_strtoul(attr + i, NULL, 10);
  3038. size[--dim] = (int) k;
  3039. if (n > SOAP_MAXARRAYSIZE)
  3040. return 0;
  3041. } while (dim > 0 && --i > 0 && attr[i] != '[');
  3042. return n;
  3043. }
  3044. /******************************************************************************/
  3045. SOAP_FMAC1
  3046. int
  3047. SOAP_FMAC2
  3048. soap_getoffsets(const char *attr, const int *size, int *offset, int dim) {
  3049. int i, j = 0;
  3050. if (offset) {
  3051. for (i = 0; i < dim && attr && *attr; i++) {
  3052. attr++;
  3053. j *= size[i];
  3054. j += offset[i] = (int) soap_strtol(attr, NULL, 10);
  3055. attr = strchr(attr, ',');
  3056. }
  3057. } else {
  3058. for (i = 0; i < dim && attr && *attr; i++) {
  3059. attr++;
  3060. j *= size[i];
  3061. j += (int) soap_strtol(attr, NULL, 10);
  3062. attr = strchr(attr, ',');
  3063. }
  3064. }
  3065. return j;
  3066. }
  3067. /******************************************************************************/
  3068. SOAP_FMAC1
  3069. int
  3070. SOAP_FMAC2
  3071. soap_getposition(const char *attr, int *pos) {
  3072. int i, n;
  3073. if (!*attr)
  3074. return -1;
  3075. n = 0;
  3076. i = 1;
  3077. do {
  3078. pos[n++] = (int) soap_strtol(attr + i, NULL, 10);
  3079. while (attr[i] && attr[i] != ',' && attr[i] != ']')
  3080. i++;
  3081. if (attr[i] == ',')
  3082. i++;
  3083. } while (n < SOAP_MAXDIMS && attr[i] && attr[i] != ']');
  3084. return n;
  3085. }
  3086. /******************************************************************************/
  3087. SOAP_FMAC1
  3088. struct soap_nlist *
  3089. SOAP_FMAC2
  3090. soap_push_namespace(struct soap *soap, const char *id, const char *ns) {
  3091. struct soap_nlist *np = NULL;
  3092. struct Namespace *p;
  3093. short i = -1;
  3094. size_t n, k;
  3095. n = strlen(id);
  3096. k = strlen(ns) + 1;
  3097. p = soap->local_namespaces;
  3098. if (p) {
  3099. for (i = 0; p->id; p++, i++) {
  3100. if (p->ns && !strcmp(ns, p->ns))
  3101. break;
  3102. if (p->out) {
  3103. if (!strcmp(ns, p->out))
  3104. break;
  3105. } else if (p->in) {
  3106. if (!soap_tag_cmp(ns, p->in)) {
  3107. if (SOAP_MAXALLOCSIZE <= 0 || k <= SOAP_MAXALLOCSIZE)
  3108. p->out = (char *) SOAP_MALLOC(soap, k);
  3109. if (p->out)
  3110. soap_strcpy(p->out, k, ns);
  3111. break;
  3112. }
  3113. }
  3114. }
  3115. if (!p->id)
  3116. i = -1;
  3117. }
  3118. if (i >= 0)
  3119. k = 0;
  3120. if (sizeof(struct soap_nlist) + n + k > n &&
  3121. (SOAP_MAXALLOCSIZE <= 0 || sizeof(struct soap_nlist) + n + k <= SOAP_MAXALLOCSIZE))
  3122. np = (struct soap_nlist *) SOAP_MALLOC(soap, sizeof(struct soap_nlist) + n + k);
  3123. if (!np) {
  3124. soap->error = SOAP_EOM;
  3125. return NULL;
  3126. }
  3127. np->next = soap->nlist;
  3128. soap->nlist = np;
  3129. np->level = soap->level;
  3130. np->index = i;
  3131. soap_strcpy((char *) np->id, n + 1, id);
  3132. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Push namespace binding (level=%u) '%s'='%s'\n", soap->level, id, ns));
  3133. if (i < 0) {
  3134. np->ns = np->id + n + 1;
  3135. soap_strcpy((char *) np->ns, k, ns);
  3136. DBGLOG(TEST, SOAP_MESSAGE(fdebug,
  3137. "Push NOT OK: no match found for '%s' in namespace mapping table (added to stack anyway)\n",
  3138. ns));
  3139. } else {
  3140. np->ns = NULL;
  3141. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Push OK ('%s' matches '%s' in namespace table)\n", id, p->id));
  3142. }
  3143. return np;
  3144. }
  3145. /******************************************************************************/
  3146. SOAP_FMAC1
  3147. void
  3148. SOAP_FMAC2
  3149. soap_pop_namespace(struct soap *soap) {
  3150. struct soap_nlist *np, *nq;
  3151. for (np = soap->nlist; np && np->level >= soap->level; np = nq) {
  3152. nq = np->next;
  3153. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Pop namespace binding (level=%u) '%s' level=%u\n", soap->level, np->id,
  3154. np->level));
  3155. SOAP_FREE(soap, np);
  3156. }
  3157. soap->nlist = np;
  3158. }
  3159. /******************************************************************************/
  3160. SOAP_FMAC1
  3161. int
  3162. SOAP_FMAC2
  3163. soap_match_namespace(struct soap *soap, const char *id1, const char *id2, size_t n1, size_t n2) {
  3164. struct soap_nlist *np = soap->nlist;
  3165. const char *s;
  3166. // std::cerr<<"Sassan Debug1: "<<np->ns<<std::endl;
  3167. while (np && (strncmp(np->id, id1, n1) || np->id[n1]))
  3168. np = np->next;
  3169. // std::cerr<<"Debug soap_match_namespace: "<<np<<" soap_match_namespace np->id: "<<np->id<<" id1: "<<id1 <<" n1: "<<n1<<" n2 "<<n2<<" id2 "<<id2<<" s "<<std::endl;
  3170. if (np) {
  3171. if (!(soap->mode & SOAP_XML_IGNORENS) && (n2 > 0 || !np->ns || *np->ns)) {
  3172. if (np->index < 0
  3173. || ((s = soap->local_namespaces[np->index].id) && (strncmp(s, id2, n2) || (s[n2] && s[n2] != '_'))))
  3174. return SOAP_NAMESPACE;
  3175. }
  3176. return SOAP_OK;
  3177. }
  3178. if (n1 == 0)
  3179. return n2 == 0 || (soap->mode & SOAP_XML_IGNORENS) ? SOAP_OK : SOAP_NAMESPACE;
  3180. if ((n1 == 3 && n1 == n2 && !strncmp(id1, "xml", 3) && !strncmp(id1, id2, 3))
  3181. || (soap->mode & SOAP_XML_IGNORENS))
  3182. return SOAP_OK;
  3183. return soap->error = SOAP_SYNTAX_ERROR;
  3184. }
  3185. /******************************************************************************/
  3186. SOAP_FMAC1
  3187. const char *
  3188. SOAP_FMAC2
  3189. soap_current_namespace_tag(struct soap *soap, const char *tag) {
  3190. struct soap_nlist *np;
  3191. const char *s;
  3192. if (!tag || !strncmp(tag, "xml", 3))
  3193. return NULL;
  3194. np = soap->nlist;
  3195. s = strchr(tag, ':');
  3196. if (!s) {
  3197. while (np && *np->id) /* find default namespace, if present */
  3198. np = np->next;
  3199. } else {
  3200. while (np && (strncmp(np->id, tag, s - tag) || np->id[s - tag]))
  3201. np = np->next;
  3202. if (!np)
  3203. soap->error = SOAP_NAMESPACE;
  3204. }
  3205. if (np) {
  3206. if (np->index >= 0)
  3207. return soap->namespaces[np->index].ns;
  3208. if (np->ns) {
  3209. s = np->ns;
  3210. if (*s)
  3211. return soap_strdup(soap, s);
  3212. do
  3213. np = np->next;
  3214. while (np && *np->id); /* find if there is any other default namespace */
  3215. if (np)
  3216. return soap_strdup(soap, s);
  3217. }
  3218. }
  3219. return NULL;
  3220. }
  3221. /******************************************************************************/
  3222. SOAP_FMAC1
  3223. const char *
  3224. SOAP_FMAC2
  3225. soap_current_namespace_att(struct soap *soap, const char *tag) {
  3226. struct soap_nlist *np;
  3227. const char *s;
  3228. if (!tag || !strncmp(tag, "xml", 3))
  3229. return NULL;
  3230. s = strchr(tag, ':');
  3231. if (!s)
  3232. return NULL;
  3233. np = soap->nlist;
  3234. while (np && (strncmp(np->id, tag, s - tag) || np->id[s - tag]))
  3235. np = np->next;
  3236. if (!np)
  3237. soap->error = SOAP_NAMESPACE;
  3238. if (np) {
  3239. if (np->index >= 0)
  3240. return soap->namespaces[np->index].ns;
  3241. if (np->ns && *np->ns)
  3242. return soap_strdup(soap, np->ns);
  3243. }
  3244. return NULL;
  3245. }
  3246. /******************************************************************************/
  3247. SOAP_FMAC1
  3248. int
  3249. SOAP_FMAC2
  3250. soap_tag_cmp(const char *s, const char *t) {
  3251. for (;;) {
  3252. int c1 = *s;
  3253. int c2 = *t;
  3254. if (!c1 || c1 == '"')
  3255. break;
  3256. if (c2 != '-') {
  3257. if (c1 != c2) {
  3258. if (c1 >= 'A' && c1 <= 'Z')
  3259. c1 += 'a' - 'A';
  3260. if (c2 >= 'A' && c2 <= 'Z')
  3261. c2 += 'a' - 'A';
  3262. }
  3263. if (c1 != c2) {
  3264. if (c2 != '*')
  3265. return 1;
  3266. c2 = *++t;
  3267. if (!c2)
  3268. return 0;
  3269. if (c2 >= 'A' && c2 <= 'Z')
  3270. c2 += 'a' - 'A';
  3271. for (;;) {
  3272. c1 = *s;
  3273. if (!c1 || c1 == '"')
  3274. break;
  3275. if (c1 >= 'A' && c1 <= 'Z')
  3276. c1 += 'a' - 'A';
  3277. if (c1 == c2 && !soap_tag_cmp(s + 1, t + 1))
  3278. return 0;
  3279. s++;
  3280. }
  3281. break;
  3282. }
  3283. }
  3284. s++;
  3285. t++;
  3286. }
  3287. if (*t == '*' && !t[1])
  3288. return 0;
  3289. return *t;
  3290. }
  3291. /******************************************************************************/
  3292. SOAP_FMAC1
  3293. int
  3294. SOAP_FMAC2
  3295. soap_match_tag(struct soap *soap, const char *tag1, const char *tag2) {
  3296. const char *s, *t;
  3297. int err;
  3298. if (!tag1 || !tag2 || !*tag2)
  3299. return SOAP_OK;
  3300. s = strchr(tag1, ':');
  3301. t = strchr(tag2, ':');
  3302. if (t) {
  3303. if (s) {
  3304. if (t[1] && SOAP_STRCMP(s + 1, t + 1))
  3305. return SOAP_TAG_MISMATCH;
  3306. if (t != tag2 && !(soap->mode & SOAP_XML_IGNORENS)) {
  3307. // err = soap_match_namespace(soap, tag1, tag2, s - tag1, t - tag2);
  3308. err = SOAP_OK;
  3309. // std::cerr<<"soap_match_namespace err: "<<err<<std::endl;
  3310. if (err) {
  3311. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Sassan Debug '%s' and '%s' r\n", tag1, tag2));
  3312. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Tags '%s' and '%s' match but namespaces differ\n", tag1, tag2));
  3313. if (err == SOAP_NAMESPACE)
  3314. return SOAP_TAG_MISMATCH;
  3315. return err;
  3316. }
  3317. }
  3318. } else if (!t[1]) {
  3319. if ((soap->mode & SOAP_XML_IGNORENS) || soap_match_namespace(soap, tag1, tag2, 0, t - tag2))
  3320. return SOAP_TAG_MISMATCH;
  3321. } else if (SOAP_STRCMP(tag1, t + 1)) {
  3322. return SOAP_TAG_MISMATCH;
  3323. } else if (t != tag2) {
  3324. err = soap_match_namespace(soap, tag1, tag2, 0, t - tag2);
  3325. if (err) {
  3326. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Tags '%s' and '%s' match but namespaces differ\n", tag1, tag2));
  3327. if (err == SOAP_NAMESPACE)
  3328. return SOAP_TAG_MISMATCH;
  3329. return err;
  3330. }
  3331. }
  3332. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Tags and (default) namespaces match: '%s' '%s'\n", tag1, tag2));
  3333. return SOAP_OK;
  3334. }
  3335. if (s) {
  3336. if (!(soap->mode & SOAP_XML_IGNORENS) || SOAP_STRCMP(s + 1, tag2)) /* always fails (except when ignoring ns) */
  3337. return SOAP_TAG_MISMATCH;
  3338. } else if (SOAP_STRCMP(tag1, tag2)
  3339. #ifndef WITH_NOEMPTYNAMESPACES
  3340. || ((soap->mode & SOAP_XML_STRICT) && !(soap->mode & SOAP_XML_IGNORENS) &&
  3341. soap_match_namespace(soap, tag1, tag2, 0,
  3342. 0)) /* strict checking: default namespace must be null namespace */
  3343. #endif
  3344. ) {
  3345. return SOAP_TAG_MISMATCH;
  3346. }
  3347. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Tags match: '%s' '%s'\n", tag1, tag2));
  3348. return SOAP_OK;
  3349. }
  3350. /******************************************************************************/
  3351. SOAP_FMAC1
  3352. int
  3353. SOAP_FMAC2
  3354. soap_match_att(struct soap *soap, const char *tag1, const char *tag2) {
  3355. const char *s, *t;
  3356. int err;
  3357. if (!tag1 || !tag2 || !*tag2)
  3358. return SOAP_OK;
  3359. s = strchr(tag1, ':');
  3360. t = strchr(tag2, ':');
  3361. if (t) {
  3362. if (s) {
  3363. if (t[1] && SOAP_STRCMP(s + 1, t + 1))
  3364. return SOAP_TAG_MISMATCH;
  3365. if (t != tag2 && !(soap->mode & SOAP_XML_IGNORENS)) {
  3366. err = soap_match_namespace(soap, tag1, tag2, s - tag1, t - tag2);
  3367. if (err) {
  3368. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Atts '%s' and '%s' match but namespaces differ\n", tag1, tag2));
  3369. if (err == SOAP_NAMESPACE)
  3370. return SOAP_TAG_MISMATCH;
  3371. return err;
  3372. }
  3373. }
  3374. } else if (!t[1] || t != tag2 || SOAP_STRCMP(tag1, t + 1))
  3375. return SOAP_TAG_MISMATCH;
  3376. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Atts and (default) namespaces match: '%s' '%s'\n", tag1, tag2));
  3377. return SOAP_OK;
  3378. }
  3379. if (s) {
  3380. if (!(soap->mode & SOAP_XML_IGNORENS) || SOAP_STRCMP(s + 1, tag2)) /* always fails (except when ignoring ns) */
  3381. return SOAP_TAG_MISMATCH;
  3382. } else if (SOAP_STRCMP(tag1, tag2))
  3383. return SOAP_TAG_MISMATCH;
  3384. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Atts match: '%s' '%s'\n", tag1, tag2));
  3385. return SOAP_OK;
  3386. }
  3387. /******************************************************************************/
  3388. SOAP_FMAC1
  3389. int
  3390. SOAP_FMAC2
  3391. soap_match_array(struct soap *soap, const char *type) {
  3392. if (type && *soap->arrayType) {
  3393. if (soap->version == 1 || !strchr(type, '[')) {
  3394. if (soap_match_tag(soap, soap->arrayType, type)
  3395. && soap_match_tag(soap, soap->arrayType, "xsd:anyType")
  3396. && soap_match_tag(soap, soap->arrayType, "xsd:ur-type")) {
  3397. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "SOAP array type mismatch: '%s' '%s'\n", soap->arrayType, type));
  3398. return SOAP_TAG_MISMATCH;
  3399. }
  3400. }
  3401. }
  3402. return SOAP_OK;
  3403. }
  3404. /******************************************************************************\
  3405. *
  3406. * SSL/TLS
  3407. *
  3408. \******************************************************************************/
  3409. #ifdef WITH_OPENSSL
  3410. SOAP_FMAC1
  3411. int
  3412. SOAP_FMAC2
  3413. soap_rand()
  3414. {
  3415. int r;
  3416. if (!soap_ssl_init_done)
  3417. soap_ssl_init();
  3418. #if OPENSSL_VERSION_NUMBER < 0x10100000L
  3419. RAND_pseudo_bytes((unsigned char*)&r, sizeof(int));
  3420. #else
  3421. RAND_bytes((unsigned char*)&r, sizeof(int));
  3422. #endif
  3423. return r;
  3424. }
  3425. #endif
  3426. /******************************************************************************/
  3427. #if defined(WITH_OPENSSL) || defined(WITH_GNUTLS) || defined(WITH_SYSTEMSSL)
  3428. SOAP_FMAC1
  3429. int
  3430. SOAP_FMAC2
  3431. #if defined(VXWORKS) && defined(WM_SECURE_KEY_STORAGE)
  3432. soap_ssl_server_context(struct soap *soap, unsigned short flags, const char *keyfile, const char *keyid, const char *password, const char *cafile, const char *capath, const char *dhfile, const char *randfile, const char *sid)
  3433. #else
  3434. soap_ssl_server_context(struct soap *soap, unsigned short flags, const char *keyfile, const char *password, const char *cafile, const char *capath, const char *dhfile, const char *randfile, const char *sid)
  3435. #endif
  3436. {
  3437. int err;
  3438. soap->keyfile = keyfile;
  3439. #if defined(VXWORKS) && defined(WM_SECURE_KEY_STORAGE)
  3440. soap->keyid = keyid; /* vxWorks compatible */
  3441. #endif
  3442. soap->password = password;
  3443. soap->cafile = cafile;
  3444. soap->capath = capath;
  3445. #ifdef WITH_OPENSSL
  3446. soap->dhfile = dhfile;
  3447. soap->randfile = randfile;
  3448. if (!soap->fsslverify)
  3449. soap->fsslverify = ssl_verify_callback;
  3450. #endif
  3451. soap->ssl_flags = flags | (dhfile == NULL ? SOAP_SSL_RSA : 0);
  3452. #ifdef WITH_GNUTLS
  3453. (void)randfile; (void)sid;
  3454. if (dhfile)
  3455. {
  3456. char *s;
  3457. int n = (int)soap_strtoul(dhfile, &s, 10);
  3458. if (!soap->dh_params)
  3459. gnutls_dh_params_init(&soap->dh_params);
  3460. /* if dhfile is numeric, treat it as a key length to generate DH params which can take a while */
  3461. if (n >= 512 && s && *s == '\0')
  3462. gnutls_dh_params_generate2(soap->dh_params, (unsigned int)n);
  3463. else
  3464. {
  3465. unsigned int dparams_len;
  3466. unsigned char dparams_buf[1024];
  3467. FILE *fd = fopen(dhfile, "r");
  3468. if (!fd)
  3469. return soap_set_receiver_error(soap, "SSL/TLS error", "Invalid DH file", SOAP_SSL_ERROR);
  3470. dparams_len = (unsigned int)fread(dparams_buf, 1, sizeof(dparams_buf), fd);
  3471. fclose(fd);
  3472. gnutls_datum_t dparams = {
  3473. dparams_buf, dparams_len };
  3474. if (gnutls_dh_params_import_pkcs3(soap->dh_params, &dparams, GNUTLS_X509_FMT_PEM))
  3475. return soap_set_receiver_error(soap, "SSL/TLS error", "Invalid DH file", SOAP_SSL_ERROR);
  3476. }
  3477. }
  3478. else
  3479. {
  3480. #if GNUTLS_VERSION_NUMBER < 0x030300
  3481. if (!soap->rsa_params)
  3482. gnutls_rsa_params_init(&soap->rsa_params);
  3483. gnutls_rsa_params_generate2(soap->rsa_params, SOAP_SSL_RSA_BITS);
  3484. #endif
  3485. }
  3486. if (soap->session)
  3487. {
  3488. gnutls_deinit(soap->session);
  3489. soap->session = NULL;
  3490. }
  3491. if (soap->xcred)
  3492. {
  3493. gnutls_certificate_free_credentials(soap->xcred);
  3494. soap->xcred = NULL;
  3495. }
  3496. #endif
  3497. #ifdef WITH_SYSTEMSSL
  3498. (void)randfile; (void)sid;
  3499. if (soap->ctx)
  3500. gsk_environment_close(&soap->ctx);
  3501. #endif
  3502. err = soap->fsslauth(soap);
  3503. #ifdef WITH_OPENSSL
  3504. if (!err)
  3505. {
  3506. if (sid)
  3507. SSL_CTX_set_session_id_context(soap->ctx, (unsigned char*)sid, (unsigned int)strlen(sid));
  3508. else
  3509. SSL_CTX_set_session_cache_mode(soap->ctx, SSL_SESS_CACHE_OFF);
  3510. }
  3511. #endif
  3512. return err;
  3513. }
  3514. #endif
  3515. /******************************************************************************/
  3516. #if defined(WITH_OPENSSL) || defined(WITH_GNUTLS) || defined(WITH_SYSTEMSSL)
  3517. SOAP_FMAC1
  3518. int
  3519. SOAP_FMAC2
  3520. #if defined(VXWORKS) && defined(WM_SECURE_KEY_STORAGE)
  3521. soap_ssl_client_context(struct soap *soap, unsigned short flags, const char *keyfile, const char *keyid, const char *password, const char *cafile, const char *capath, const char *randfile)
  3522. #else
  3523. soap_ssl_client_context(struct soap *soap, unsigned short flags, const char *keyfile, const char *password, const char *cafile, const char *capath, const char *randfile)
  3524. #endif
  3525. {
  3526. soap->keyfile = keyfile;
  3527. #if defined(VXWORKS) && defined(WM_SECURE_KEY_STORAGE)
  3528. soap->keyid = keyid; /* vxWorks compatible */
  3529. #endif
  3530. soap->password = password;
  3531. soap->cafile = cafile;
  3532. soap->capath = capath;
  3533. soap->ssl_flags = SOAP_SSL_CLIENT | flags;
  3534. #ifdef WITH_OPENSSL
  3535. soap->dhfile = NULL;
  3536. soap->randfile = randfile;
  3537. if (!soap->fsslverify)
  3538. soap->fsslverify = (flags & SOAP_SSL_ALLOW_EXPIRED_CERTIFICATE) == 0 ? ssl_verify_callback : ssl_verify_callback_allow_expired_certificate;
  3539. #endif
  3540. #ifdef WITH_GNUTLS
  3541. (void)randfile;
  3542. if (soap->session)
  3543. {
  3544. gnutls_deinit(soap->session);
  3545. soap->session = NULL;
  3546. }
  3547. if (soap->xcred)
  3548. {
  3549. gnutls_certificate_free_credentials(soap->xcred);
  3550. soap->xcred = NULL;
  3551. }
  3552. #endif
  3553. #ifdef WITH_SYSTEMSSL
  3554. (void)randfile;
  3555. if (soap->ctx)
  3556. gsk_environment_close(&soap->ctx);
  3557. #endif
  3558. return soap->fsslauth(soap);
  3559. }
  3560. #endif
  3561. /******************************************************************************/
  3562. #if defined(WITH_OPENSSL) || defined(WITH_GNUTLS)
  3563. SOAP_FMAC1
  3564. int
  3565. SOAP_FMAC2
  3566. soap_ssl_crl(struct soap *soap, const char *crlfile)
  3567. {
  3568. #ifdef WITH_OPENSSL
  3569. if (crlfile && soap->ctx)
  3570. {
  3571. #if OPENSSL_VERSION_NUMBER > 0x00907000L
  3572. X509_STORE *store = SSL_CTX_get_cert_store(soap->ctx);
  3573. if (*crlfile)
  3574. {
  3575. int ret;
  3576. X509_LOOKUP *lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file());
  3577. if (!lookup)
  3578. return soap_set_receiver_error(soap, "SSL/TLS error", "Can't create X509_LOOKUP object", SOAP_SSL_ERROR);
  3579. ret = X509_load_crl_file(lookup, crlfile, X509_FILETYPE_PEM);
  3580. if (ret <= 0)
  3581. return soap_set_receiver_error(soap, "SSL/TLS error", "Can't read CRL PEM file", SOAP_SSL_ERROR);
  3582. }
  3583. X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK | X509_V_FLAG_CRL_CHECK_ALL);
  3584. #endif
  3585. }
  3586. else
  3587. soap->crlfile = crlfile; /* activate later when store is available */
  3588. #endif
  3589. #ifdef WITH_GNUTLS
  3590. if (crlfile && soap->xcred)
  3591. {
  3592. if (*crlfile)
  3593. if (gnutls_certificate_set_x509_crl_file(soap->xcred, crlfile, GNUTLS_X509_FMT_PEM) < 0)
  3594. return soap_set_receiver_error(soap, "SSL/TLS error", "Can't read CRL PEM file", SOAP_SSL_ERROR);
  3595. }
  3596. else
  3597. {
  3598. soap->crlfile = crlfile; /* activate later when xcred is available */
  3599. }
  3600. #endif
  3601. return SOAP_OK;
  3602. }
  3603. #endif
  3604. /******************************************************************************/
  3605. #if defined(WITH_OPENSSL) || defined(WITH_GNUTLS)
  3606. SOAP_FMAC1
  3607. void
  3608. SOAP_FMAC2
  3609. soap_ssl_init()
  3610. {
  3611. /* Note: for multi-threaded applications, the main program should call soap_ssl_init() before any threads are started */
  3612. if (!soap_ssl_init_done)
  3613. {
  3614. soap_ssl_init_done = 1;
  3615. #ifdef WITH_OPENSSL
  3616. SSL_library_init();
  3617. OpenSSL_add_all_algorithms(); /* we keep ciphers and digests for the program's lifetime */
  3618. #ifndef WITH_LEAN
  3619. SSL_load_error_strings();
  3620. #endif
  3621. #if !defined(WIN32) && !defined(CYGWIN) && !defined(__MINGW32__) && !defined(__MINGW64__)
  3622. if (!RAND_load_file("/dev/urandom", 1024))
  3623. #else
  3624. if (1)
  3625. #endif
  3626. {
  3627. /* if /dev/urandom does not exist we need to do at least some pertubations to seed the OpenSSL PRNG */
  3628. char buf[1024];
  3629. RAND_seed(buf, sizeof(buf));
  3630. #ifdef HAVE_RANDOM
  3631. srandom((unsigned long)time(NULL));
  3632. #else
  3633. srand((unsigned int)time(NULL));
  3634. #endif
  3635. do
  3636. {
  3637. #ifdef HAVE_RANDOM
  3638. long r = random(); /* we actually do no use random() anywhere, except to help seed the OpenSSL PRNG */
  3639. RAND_seed(&r, sizeof(long));
  3640. #else
  3641. int r = rand(); /* we actually do no use rand() anywhere, except to help seed the OpenSSL PRNG */
  3642. RAND_seed(&r, sizeof(int));
  3643. #endif
  3644. } while (!RAND_status());
  3645. }
  3646. #endif
  3647. #ifdef WITH_GNUTLS
  3648. # if GNUTLS_VERSION_NUMBER < 0x020b00
  3649. # if defined(HAVE_PTHREAD_H)
  3650. gcry_control(GCRYCTL_SET_THREAD_CBS, &gcry_threads_pthread);
  3651. # elif defined(HAVE_PTH_H)
  3652. gcry_control(GCRYCTL_SET_THREAD_CBS, &gcry_threads_pth);
  3653. # endif
  3654. gcry_control(GCRYCTL_ENABLE_QUICK_RANDOM, 0);
  3655. gcry_control(GCRYCTL_DISABLE_SECMEM, 0);
  3656. gcry_control(GCRYCTL_INITIALIZATION_FINISHED, 0); /* libgcrypt init done */
  3657. # endif
  3658. # if GNUTLS_VERSION_NUMBER < 0x030300
  3659. gnutls_global_init();
  3660. # endif
  3661. #endif
  3662. }
  3663. }
  3664. #endif
  3665. /******************************************************************************/
  3666. #if defined(WITH_OPENSSL) || defined(WITH_GNUTLS)
  3667. SOAP_FMAC1
  3668. void
  3669. SOAP_FMAC2
  3670. soap_ssl_noinit()
  3671. {
  3672. /* Call this first to bypass SSL init is SSL is already initialized elsewhere */
  3673. soap_ssl_init_done = 1;
  3674. }
  3675. #endif
  3676. /******************************************************************************/
  3677. #if defined(WITH_OPENSSL) || defined(WITH_GNUTLS)
  3678. SOAP_FMAC1
  3679. const char *
  3680. SOAP_FMAC2
  3681. soap_ssl_error(struct soap *soap, int ret, int err)
  3682. {
  3683. #ifdef WITH_OPENSSL
  3684. const char *msg = soap_code_str(h_ssl_error_codes, err);
  3685. if (!msg)
  3686. return ERR_error_string(err, soap->msgbuf);
  3687. (SOAP_SNPRINTF(soap->msgbuf, sizeof(soap->msgbuf), strlen(msg) + 1), "%s\n", msg);
  3688. if (ERR_peek_error())
  3689. {
  3690. unsigned long r;
  3691. while ((r = ERR_get_error()))
  3692. {
  3693. size_t l = strlen(soap->msgbuf);
  3694. ERR_error_string_n(r, soap->msgbuf + l, sizeof(soap->msgbuf) - l);
  3695. l = strlen(soap->msgbuf);
  3696. if (l + 1 < sizeof(soap->msgbuf))
  3697. {
  3698. soap->msgbuf[l++] = '\n';
  3699. soap->msgbuf[l] = '\0';
  3700. }
  3701. if (ERR_GET_REASON(r) == SSL_R_CERTIFICATE_VERIFY_FAILED && l < sizeof(soap->msgbuf))
  3702. {
  3703. const char *s = X509_verify_cert_error_string(SSL_get_verify_result(soap->ssl));
  3704. (SOAP_SNPRINTF(soap->msgbuf + l, sizeof(soap->msgbuf) - l, strlen(s)), "%s", s);
  3705. }
  3706. }
  3707. }
  3708. else
  3709. {
  3710. size_t l = strlen(soap->msgbuf);
  3711. switch (ret)
  3712. {
  3713. case 0:
  3714. soap_strcpy(soap->msgbuf + l, sizeof(soap->msgbuf) - l, "EOF was observed that violates the SSL/TLS protocol. The client probably provided invalid authentication information.");
  3715. break;
  3716. case -1:
  3717. {
  3718. const char *s = strerror(soap_errno);
  3719. (SOAP_SNPRINTF(soap->msgbuf + l, sizeof(soap->msgbuf) - l, strlen(s) + 42), "Error observed by underlying SSL/TLS BIO: %s", s);
  3720. }
  3721. break;
  3722. }
  3723. }
  3724. ERR_clear_error();
  3725. return soap->msgbuf;
  3726. #endif
  3727. #ifdef WITH_GNUTLS
  3728. (void)soap;
  3729. (void)err;
  3730. return gnutls_strerror(ret);
  3731. #endif
  3732. }
  3733. #endif
  3734. /******************************************************************************/
  3735. #ifdef WITH_SYSTEMSSL
  3736. static int
  3737. ssl_recv(int sk, void *s, int n, char *user)
  3738. {
  3739. (void)user;
  3740. return recv(sk, s, n, 0);
  3741. }
  3742. #endif
  3743. /******************************************************************************/
  3744. #ifdef WITH_SYSTEMSSL
  3745. static int
  3746. ssl_send(int sk, void *s, int n, char *user)
  3747. {
  3748. (void)user;
  3749. return send(sk, s, n, 0);
  3750. }
  3751. #endif
  3752. /******************************************************************************/
  3753. #if defined(WITH_OPENSSL) || defined(WITH_GNUTLS) || defined(WITH_SYSTEMSSL)
  3754. static int
  3755. ssl_auth_init(struct soap *soap)
  3756. {
  3757. #ifdef WITH_OPENSSL
  3758. #if OPENSSL_VERSION_NUMBER >= 0x10101000L
  3759. int minv = 0, maxv = 0;
  3760. #endif
  3761. long flags = SSL_OP_ALL;
  3762. int mode;
  3763. #if defined(VXWORKS) && defined(WM_SECURE_KEY_STORAGE)
  3764. EVP_PKEY* pkey; /* vxWorks compatible */
  3765. #endif
  3766. if (!soap_ssl_init_done)
  3767. soap_ssl_init();
  3768. ERR_clear_error();
  3769. if (!soap->ctx)
  3770. {
  3771. #if OPENSSL_VERSION_NUMBER >= 0x10100000L
  3772. /* TLS_method: a TLS/SSL connection established may understand the SSLv3, TLSv1, TLSv1.1 and TLSv1.2 protocols. */
  3773. soap->ctx = SSL_CTX_new(TLS_method());
  3774. #else
  3775. /* SSLv23_method: a TLS/SSL connection established may understand the SSLv3, TLSv1, TLSv1.1 and TLSv1.2 protocols. */
  3776. soap->ctx = SSL_CTX_new(SSLv23_method());
  3777. #endif
  3778. if (!soap->ctx)
  3779. return soap_set_receiver_error(soap, "SSL/TLS error", "Can't setup context", SOAP_SSL_ERROR);
  3780. /* The following alters the behavior of SSL read/write: */
  3781. #if 0
  3782. SSL_CTX_set_mode(soap->ctx, SSL_MODE_ENABLE_PARTIAL_WRITE | SSL_MODE_AUTO_RETRY);
  3783. #endif
  3784. }
  3785. if (soap->randfile)
  3786. {
  3787. if (!RAND_load_file(soap->randfile, -1))
  3788. return soap_set_receiver_error(soap, "SSL/TLS error", "Can't load randomness", SOAP_SSL_ERROR);
  3789. }
  3790. if (soap->cafile || soap->capath)
  3791. {
  3792. if (!SSL_CTX_load_verify_locations(soap->ctx, soap->cafile, soap->capath))
  3793. return soap_set_receiver_error(soap, "SSL/TLS error", "Can't read CA PEM file", SOAP_SSL_ERROR);
  3794. if (soap->cafile && (soap->ssl_flags & SOAP_SSL_REQUIRE_CLIENT_AUTHENTICATION))
  3795. SSL_CTX_set_client_CA_list(soap->ctx, SSL_load_client_CA_file(soap->cafile));
  3796. }
  3797. if (!(soap->ssl_flags & SOAP_SSL_NO_DEFAULT_CA_PATH))
  3798. {
  3799. if (!SSL_CTX_set_default_verify_paths(soap->ctx))
  3800. return soap_set_receiver_error(soap, "SSL/TLS error", "Can't read default CA PEM file and/or directory", SOAP_SSL_ERROR);
  3801. }
  3802. if (soap->crlfile)
  3803. {
  3804. if (soap_ssl_crl(soap, soap->crlfile))
  3805. return soap->error;
  3806. }
  3807. /* This code assumes a typical scenario with key and cert in one PEM file, see alternative code below */
  3808. #if 1
  3809. if (soap->keyfile)
  3810. {
  3811. if (!SSL_CTX_use_certificate_chain_file(soap->ctx, soap->keyfile))
  3812. return soap_set_receiver_error(soap, "SSL/TLS error", "Can't find or read certificate in private key PEM file", SOAP_SSL_ERROR);
  3813. if (soap->password)
  3814. {
  3815. SSL_CTX_set_default_passwd_cb_userdata(soap->ctx, (void*)soap->password);
  3816. SSL_CTX_set_default_passwd_cb(soap->ctx, ssl_password);
  3817. }
  3818. #ifndef WM_SECURE_KEY_STORAGE
  3819. if (!SSL_CTX_use_PrivateKey_file(soap->ctx, soap->keyfile, SSL_FILETYPE_PEM))
  3820. return soap_set_receiver_error(soap, "SSL/TLS error", "Can't read private key PEM file", SOAP_SSL_ERROR);
  3821. #endif
  3822. }
  3823. #else
  3824. /* Suggested alternative approach to check the key file for cert only when cafile==NULL */
  3825. if (soap->password)
  3826. {
  3827. SSL_CTX_set_default_passwd_cb_userdata(soap->ctx, (void*)soap->password);
  3828. SSL_CTX_set_default_passwd_cb(soap->ctx, ssl_password);
  3829. }
  3830. if (!soap->cafile)
  3831. {
  3832. if (soap->keyfile)
  3833. {
  3834. if (!SSL_CTX_use_certificate_chain_file(soap->ctx, soap->keyfile))
  3835. return soap_set_receiver_error(soap, "SSL/TLS error", "Can't find or read certificate in private key PEM file", SOAP_SSL_ERROR);
  3836. if (!SSL_CTX_use_PrivateKey_file(soap->ctx, soap->keyfile, SSL_FILETYPE_PEM))
  3837. return soap_set_receiver_error(soap, "SSL/TLS error", "Can't read private key PEM file", SOAP_SSL_ERROR);
  3838. }
  3839. }
  3840. else /* use cafile for (server) cert and keyfile for (server) key */
  3841. {
  3842. if (!SSL_CTX_use_certificate_chain_file(soap->ctx, soap->cafile))
  3843. return soap_set_receiver_error(soap, "SSL/TLS error", "Can't read CA PEM file", SOAP_SSL_ERROR);
  3844. if (soap->keyfile)
  3845. if (!SSL_CTX_use_PrivateKey_file(soap->ctx, soap->keyfile, SSL_FILETYPE_PEM))
  3846. return soap_set_receiver_error(soap, "SSL/TLS error", "Can't read private key PEM file", SOAP_SSL_ERROR);
  3847. }
  3848. #endif
  3849. #if defined(VXWORKS) && defined(WM_SECURE_KEY_STORAGE)
  3850. /* vxWorks compatible */
  3851. pkey = ipcom_key_db_pkey_get(soap->keyid);
  3852. if (!pkey)
  3853. return soap_set_receiver_error(soap, "SSL error", "Can't find key", SOAP_SSL_ERROR);
  3854. if (!SSL_CTX_use_PrivateKey(soap->ctx, pkey))
  3855. return soap_set_receiver_error(soap, "SSL error", "Can't read key", SOAP_SSL_ERROR);
  3856. #endif
  3857. if ((soap->ssl_flags & SOAP_SSL_RSA))
  3858. {
  3859. #if OPENSSL_VERSION_NUMBER >= 0x10002000L
  3860. if (SSL_CTX_need_tmp_RSA(soap->ctx))
  3861. {
  3862. unsigned long e = RSA_F4;
  3863. BIGNUM *bne = BN_new();
  3864. RSA *rsa = RSA_new();
  3865. if (!bne || !rsa || !BN_set_word(bne, e) || !RSA_generate_key_ex(rsa, SOAP_SSL_RSA_BITS, bne, NULL) || !SSL_CTX_set_tmp_rsa(soap->ctx, rsa))
  3866. {
  3867. if (bne)
  3868. BN_free(bne);
  3869. if (rsa)
  3870. RSA_free(rsa);
  3871. return soap_set_receiver_error(soap, "SSL/TLS error", "Can't generate RSA key", SOAP_SSL_ERROR);
  3872. }
  3873. BN_free(bne);
  3874. RSA_free(rsa);
  3875. }
  3876. #else
  3877. RSA *rsa = RSA_generate_key(SOAP_SSL_RSA_BITS, RSA_F4, NULL, NULL);
  3878. if (!rsa || !SSL_CTX_set_tmp_rsa(soap->ctx, rsa))
  3879. {
  3880. if (rsa)
  3881. RSA_free(rsa);
  3882. return soap_set_receiver_error(soap, "SSL/TLS error", "Can't generate RSA key", SOAP_SSL_ERROR);
  3883. }
  3884. RSA_free(rsa);
  3885. #endif
  3886. }
  3887. else if (soap->dhfile)
  3888. {
  3889. DH *dh = NULL;
  3890. char *s;
  3891. int n = (int)soap_strtoul(soap->dhfile, &s, 10);
  3892. /* if dhfile is numeric, treat it as a key length to generate DH params which can take a while */
  3893. if (n >= 512 && s && *s == '\0')
  3894. {
  3895. #if OPENSSL_VERSION_NUMBER >= 0x10002000L
  3896. dh = DH_new();
  3897. if (!DH_generate_parameters_ex(dh, n, 2/*or 5*/, NULL))
  3898. {
  3899. DH_free(dh);
  3900. return soap_set_receiver_error(soap, "SSL/TLS error", "Can't generate DH parameters", SOAP_SSL_ERROR);
  3901. }
  3902. #elif defined(VXWORKS)
  3903. dh = DH_new();
  3904. DH_generate_parameters_ex(dh, n, 2/*or 5*/, NULL);
  3905. #else
  3906. dh = DH_generate_parameters(n, 2/*or 5*/, NULL, NULL);
  3907. #endif
  3908. }
  3909. else
  3910. {
  3911. BIO *bio;
  3912. bio = BIO_new_file(soap->dhfile, "r");
  3913. if (!bio)
  3914. return soap_set_receiver_error(soap, "SSL/TLS error", "Can't read DH PEM file", SOAP_SSL_ERROR);
  3915. dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL);
  3916. BIO_free(bio);
  3917. }
  3918. if (!dh || DH_check(dh, &n) != 1 || SSL_CTX_set_tmp_dh(soap->ctx, dh) < 0)
  3919. {
  3920. if (dh)
  3921. DH_free(dh);
  3922. return soap_set_receiver_error(soap, "SSL/TLS error", "Can't set DH parameters", SOAP_SSL_ERROR);
  3923. }
  3924. DH_free(dh);
  3925. }
  3926. /* enable all TSLv1 protocols and disable SSLv3 by default if no SSL/TLS flags are set */
  3927. if (!(soap->ssl_flags & SOAP_SSLv3_TLSv1))
  3928. soap->ssl_flags = SOAP_TLSv1;
  3929. #if OPENSSL_VERSION_NUMBER >= 0x10101000L
  3930. if ((soap->ssl_flags & SOAP_SSLv3))
  3931. minv = SSL3_VERSION;
  3932. else if ((soap->ssl_flags & SOAP_TLSv1_0))
  3933. minv = TLS1_VERSION;
  3934. else if ((soap->ssl_flags & SOAP_TLSv1_1))
  3935. minv = TLS1_1_VERSION;
  3936. else if ((soap->ssl_flags & SOAP_TLSv1_2))
  3937. minv = TLS1_2_VERSION;
  3938. else if ((soap->ssl_flags & SOAP_TLSv1_3))
  3939. minv = TLS1_3_VERSION;
  3940. if ((soap->ssl_flags & SOAP_TLSv1_3) && OpenSSL_version_num() >= 0x10101000L)
  3941. maxv = TLS1_3_VERSION;
  3942. else if ((soap->ssl_flags & SOAP_TLSv1_2))
  3943. maxv = TLS1_2_VERSION;
  3944. else if ((soap->ssl_flags & SOAP_TLSv1_1))
  3945. maxv = TLS1_1_VERSION;
  3946. else if ((soap->ssl_flags & SOAP_TLSv1_0))
  3947. maxv = TLS1_VERSION;
  3948. else
  3949. maxv = SSL3_VERSION;
  3950. if (!SSL_CTX_set_min_proto_version(soap->ctx, minv)
  3951. || !SSL_CTX_set_max_proto_version(soap->ctx, maxv))
  3952. return soap_set_receiver_error(soap, "SSL/TLS error", "Can't set protocol version", SOAP_SSL_ERROR);
  3953. #else
  3954. /* disable SSL v2 by default and enable specific protos */
  3955. flags = SSL_OP_NO_SSLv2;
  3956. if (!(soap->ssl_flags & SOAP_SSLv3))
  3957. flags |= SSL_OP_NO_SSLv3;
  3958. #if OPENSSL_VERSION_NUMBER >= 0x10001000L
  3959. if (!(soap->ssl_flags & SOAP_TLSv1_0))
  3960. flags |= SSL_OP_NO_TLSv1;
  3961. if (!(soap->ssl_flags & SOAP_TLSv1_1))
  3962. flags |= SSL_OP_NO_TLSv1_1;
  3963. if (!(soap->ssl_flags & SOAP_TLSv1_2))
  3964. flags |= SSL_OP_NO_TLSv1_2;
  3965. #endif
  3966. #endif
  3967. #ifdef SSL_OP_NO_TICKET
  3968. /* TLS extension is enabled by default in OPENSSL v0.9.8k disable it by */
  3969. flags |= SSL_OP_NO_TICKET;
  3970. #endif
  3971. SSL_CTX_set_options(soap->ctx, flags);
  3972. if ((soap->ssl_flags & SOAP_SSL_REQUIRE_CLIENT_AUTHENTICATION))
  3973. mode = (SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT);
  3974. else if ((soap->ssl_flags & SOAP_SSL_REQUIRE_SERVER_AUTHENTICATION))
  3975. mode = SSL_VERIFY_PEER;
  3976. else
  3977. mode = SSL_VERIFY_NONE;
  3978. SSL_CTX_set_verify(soap->ctx, mode, soap->fsslverify);
  3979. #if OPENSSL_VERSION_NUMBER < 0x00905100L
  3980. SSL_CTX_set_verify_depth(soap->ctx, 1);
  3981. #else
  3982. SSL_CTX_set_verify_depth(soap->ctx, 9);
  3983. #endif
  3984. #endif
  3985. #ifdef WITH_GNUTLS
  3986. int ret;
  3987. char priority[80];
  3988. soap_strcpy(priority, sizeof(priority), "PERFORMANCE");
  3989. if (!soap_ssl_init_done)
  3990. soap_ssl_init();
  3991. if (!soap->xcred)
  3992. {
  3993. if (gnutls_certificate_allocate_credentials(&soap->xcred) != GNUTLS_E_SUCCESS)
  3994. return soap_set_receiver_error(soap, "SSL/TLS error", "Can't allocate credentials or trust", SOAP_SSL_ERROR);
  3995. #if GNUTLS_VERSION_NUMBER >= 0x030300
  3996. gnutls_certificate_set_x509_system_trust(soap->xcred);
  3997. #endif
  3998. if (soap->cafile)
  3999. {
  4000. if (gnutls_certificate_set_x509_trust_file(soap->xcred, soap->cafile, GNUTLS_X509_FMT_PEM) < 0)
  4001. return soap_set_receiver_error(soap, "SSL/TLS error", "Can't read CA PEM file", SOAP_SSL_ERROR);
  4002. }
  4003. if (soap->crlfile)
  4004. {
  4005. if (soap_ssl_crl(soap, soap->crlfile))
  4006. return soap->error;
  4007. }
  4008. if (soap->keyfile)
  4009. {
  4010. if (gnutls_certificate_set_x509_key_file2(soap->xcred, soap->keyfile, soap->keyfile, GNUTLS_X509_FMT_PEM, soap->password, GNUTLS_PKCS_PKCS12_3DES | GNUTLS_PKCS_PKCS12_ARCFOUR | GNUTLS_PKCS_PKCS12_RC2_40 | GNUTLS_PKCS_PBES2_AES_128 | GNUTLS_PKCS_PBES2_AES_192 | GNUTLS_PKCS_PBES2_AES_256 | GNUTLS_PKCS_PBES2_DES) < 0) /* Assumes that key and cert(s) are concatenated in the keyfile and the key is encrypted with one of these algorithms */
  4011. return soap_set_receiver_error(soap, "SSL/TLS error", "Can't read private key PEM file", SOAP_SSL_ERROR);
  4012. }
  4013. }
  4014. if ((soap->ssl_flags & SOAP_SSL_CLIENT))
  4015. {
  4016. gnutls_init(&soap->session, GNUTLS_CLIENT);
  4017. if (soap->cafile || soap->crlfile || soap->keyfile)
  4018. {
  4019. ret = gnutls_priority_set_direct(soap->session, "PERFORMANCE", NULL);
  4020. if (ret != GNUTLS_E_SUCCESS)
  4021. return soap_set_receiver_error(soap, soap_ssl_error(soap, ret, 0), "SSL/TLS set priority error", SOAP_SSL_ERROR);
  4022. gnutls_credentials_set(soap->session, GNUTLS_CRD_CERTIFICATE, soap->xcred);
  4023. }
  4024. else
  4025. {
  4026. if (!soap->acred)
  4027. gnutls_anon_allocate_client_credentials(&soap->acred);
  4028. ret = gnutls_priority_set_direct(soap->session, "PERFORMANCE:+ANON-DH:!ARCFOUR-128", NULL);
  4029. if (ret != GNUTLS_E_SUCCESS)
  4030. return soap_set_receiver_error(soap, soap_ssl_error(soap, ret, 0), "SSL/TLS set priority error", SOAP_SSL_ERROR);
  4031. gnutls_credentials_set(soap->session, GNUTLS_CRD_ANON, soap->acred);
  4032. }
  4033. }
  4034. else if (!soap->keyfile)
  4035. {
  4036. return soap_set_receiver_error(soap, "SSL/TLS error", "No key file: anonymous server authentication not supported in this release", SOAP_SSL_ERROR);
  4037. }
  4038. else
  4039. {
  4040. #if GNUTLS_VERSION_NUMBER < 0x030300
  4041. int protocol_priority[] = { 0, 0, 0, 0, 0 };
  4042. int *protocol = protocol_priority;
  4043. if ((soap->ssl_flags & SOAP_SSL_RSA) && soap->rsa_params)
  4044. gnutls_certificate_set_rsa_export_params(soap->xcred, soap->rsa_params);
  4045. #endif
  4046. if (!(soap->ssl_flags & SOAP_SSL_RSA) && soap->dh_params)
  4047. gnutls_certificate_set_dh_params(soap->xcred, soap->dh_params);
  4048. if (!soap->cache)
  4049. gnutls_priority_init(&soap->cache, "NORMAL", NULL);
  4050. gnutls_init(&soap->session, GNUTLS_SERVER);
  4051. gnutls_priority_set(soap->session, soap->cache);
  4052. gnutls_credentials_set(soap->session, GNUTLS_CRD_CERTIFICATE, soap->xcred);
  4053. if ((soap->ssl_flags & SOAP_SSL_REQUIRE_CLIENT_AUTHENTICATION))
  4054. gnutls_certificate_server_set_request(soap->session, GNUTLS_CERT_REQUEST);
  4055. gnutls_session_enable_compatibility_mode(soap->session);
  4056. /* enable all TSLv1 protocols and disable SSLv3 by default if no SSL/TLS flags are set */
  4057. if (!(soap->ssl_flags & SOAP_SSLv3_TLSv1))
  4058. soap->ssl_flags = SOAP_TLSv1;
  4059. #if GNUTLS_VERSION_NUMBER < 0x030300
  4060. if ((soap->ssl_flags & SOAP_SSLv3))
  4061. *protocol++ = GNUTLS_SSL3;
  4062. if ((soap->ssl_flags & SOAP_TLSv1_0))
  4063. *protocol++ = GNUTLS_TLS1_0;
  4064. if ((soap->ssl_flags & SOAP_TLSv1_1))
  4065. *protocol++ = GNUTLS_TLS1_1;
  4066. if ((soap->ssl_flags & SOAP_TLSv1_2))
  4067. *protocol++ = GNUTLS_TLS1_2;
  4068. if (gnutls_protocol_set_priority(soap->session, protocol_priority) != GNUTLS_E_SUCCESS)
  4069. return soap_set_receiver_error(soap, "SSL/TLS error", "Can't set protocol", SOAP_SSL_ERROR);
  4070. #else
  4071. soap_strcpy(soap->tmpbuf, sizeof(soap->tmpbuf), "NORMAL:+VERS-ALL");
  4072. if (!(soap->ssl_flags & SOAP_TLSv1_0))
  4073. soap_strcat(soap->tmpbuf, sizeof(soap->tmpbuf), ":-VERS-TLS1.0");
  4074. if (!(soap->ssl_flags & SOAP_TLSv1_1))
  4075. soap_strcat(soap->tmpbuf, sizeof(soap->tmpbuf), ":-VERS-TLS1.1");
  4076. if (!(soap->ssl_flags & SOAP_TLSv1_2))
  4077. soap_strcat(soap->tmpbuf, sizeof(soap->tmpbuf), ":-VERS-TLS1.2");
  4078. if (gnutls_priority_set_direct(soap->session, soap->tmpbuf, NULL) != GNUTLS_E_SUCCESS)
  4079. return soap_set_receiver_error(soap, "SSL/TLS error", "Can't set protocol priority", SOAP_SSL_ERROR);
  4080. #endif
  4081. }
  4082. #endif
  4083. #ifdef WITH_SYSTEMSSL
  4084. if (!soap->ctx)
  4085. {
  4086. int err;
  4087. err = gsk_environment_open(&soap->ctx);
  4088. if (err == GSK_OK)
  4089. err = gsk_attribute_set_enum(soap->ctx, GSK_PROTOCOL_SSLV2, GSK_PROTOCOL_SSLV2_OFF);
  4090. /* enable all TSLv1 protocols and disable SSLv3 by default if no SSL/TLS flags are set */
  4091. if (!(soap->ssl_flags & SOAP_SSLv3_TLSv1))
  4092. soap->ssl_flags = SOAP_TLSv1;
  4093. if (err == GSK_OK)
  4094. {
  4095. if ((soap->ssl_flags & SOAP_SSLv3))
  4096. err = gsk_attribute_set_enum(soap->ctx, GSK_PROTOCOL_SSLV3, GSK_PROTOCOL_SSLV3_ON);
  4097. else
  4098. err = gsk_attribute_set_enum(soap->ctx, GSK_PROTOCOL_SSLV3, GSK_PROTOCOL_SSLV3_OFF);
  4099. }
  4100. if (err == GSK_OK)
  4101. {
  4102. if ((soap->ssl_flags & SOAP_TLSv1_0))
  4103. err = gsk_attribute_set_enum(soap->ctx, GSK_PROTOCOL_TLSV1, GSK_PROTOCOL_TLSV1_ON);
  4104. else
  4105. err = gsk_attribute_set_enum(soap->ctx, GSK_PROTOCOL_TLSV1, GSK_PROTOCOL_TLSV1_OFF);
  4106. }
  4107. if (err == GSK_OK)
  4108. {
  4109. if ((soap->ssl_flags & SOAP_TLSv1_1))
  4110. err = gsk_attribute_set_enum(soap->ctx, GSK_PROTOCOL_TLSV1, GSK_PROTOCOL_TLSV1_1_ON);
  4111. else
  4112. err = gsk_attribute_set_enum(soap->ctx, GSK_PROTOCOL_TLSV1, GSK_PROTOCOL_TLSV1_1_OFF);
  4113. }
  4114. if (err == GSK_OK)
  4115. {
  4116. if ((soap->ssl_flags & SOAP_TLSv1_2))
  4117. err = gsk_attribute_set_enum(soap->ctx, GSK_PROTOCOL_TLSV1, GSK_PROTOCOL_TLSV1_2_ON);
  4118. else
  4119. err = gsk_attribute_set_enum(soap->ctx, GSK_PROTOCOL_TLSV1, GSK_PROTOCOL_TLSV1_2_OFF);
  4120. }
  4121. if (err == GSK_OK)
  4122. err = gsk_attribute_set_buffer(soap->ctx, GSK_KEYRING_FILE, soap->keyfile, 0); /* keyfile is a keyring .kdb file */
  4123. if (err == GSK_OK)
  4124. err = gsk_attribute_set_buffer(soap->ctx, GSK_KEYRING_PW, soap->password, 0); /* locked by password */
  4125. if (err == GSK_OK)
  4126. err = gsk_environment_init(soap->ctx);
  4127. if (err != GSK_OK)
  4128. return soap_set_receiver_error(soap, gsk_strerror(err), "SYSTEM SSL error in ssl_auth_init()", SOAP_SSL_ERROR);
  4129. }
  4130. #endif
  4131. return SOAP_OK;
  4132. }
  4133. #endif
  4134. /******************************************************************************/
  4135. #ifdef WITH_OPENSSL
  4136. static int
  4137. ssl_password(char *buf, int num, int rwflag, void *userdata)
  4138. {
  4139. (void)rwflag;
  4140. if (!buf || !userdata)
  4141. return 0;
  4142. soap_strcpy(buf, (size_t)num, (char*)userdata);
  4143. return (int)strlen(buf);
  4144. }
  4145. #endif
  4146. /******************************************************************************/
  4147. #ifdef WITH_OPENSSL
  4148. static int
  4149. ssl_verify_callback(int ok, X509_STORE_CTX *store)
  4150. {
  4151. (void)store;
  4152. #ifdef SOAP_DEBUG
  4153. if (!ok)
  4154. {
  4155. char buf[1024];
  4156. int err = X509_STORE_CTX_get_error(store);
  4157. X509 *cert = X509_STORE_CTX_get_current_cert(store);
  4158. fprintf(stderr, "\nDEBUG mode TLS/SSL warnings:\nSSL verify error %d or warning with certificate at depth %d: %s\n", err, X509_STORE_CTX_get_error_depth(store), X509_verify_cert_error_string(err));
  4159. X509_NAME_oneline(X509_get_issuer_name(cert), buf, sizeof(buf)-1);
  4160. fprintf(stderr, " certificate issuer: %s\n", buf);
  4161. X509_NAME_oneline(X509_get_subject_name(cert), buf, sizeof(buf)-1);
  4162. fprintf(stderr, " certificate subject: %s\n", buf);
  4163. /* accept self-signed certificates and certificates out of date */
  4164. switch (err)
  4165. {
  4166. case X509_V_ERR_CERT_NOT_YET_VALID:
  4167. case X509_V_ERR_CERT_HAS_EXPIRED:
  4168. case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT:
  4169. case X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN:
  4170. case X509_V_ERR_UNABLE_TO_GET_CRL:
  4171. case X509_V_ERR_CRL_NOT_YET_VALID:
  4172. case X509_V_ERR_CRL_HAS_EXPIRED:
  4173. X509_STORE_CTX_set_error(store, X509_V_OK);
  4174. ok = 1;
  4175. fprintf(stderr, "Initialize soap_ssl_client_context with SOAP_SSL_ALLOW_EXPIRED_CERTIFICATE to allow this verification error to pass without DEBUG mode enabled\n");
  4176. }
  4177. }
  4178. #endif
  4179. /* Note: return 1 to try to continue, but unsafe progress will be terminated by OpenSSL */
  4180. return ok;
  4181. }
  4182. #endif
  4183. /******************************************************************************/
  4184. #ifdef WITH_OPENSSL
  4185. static int
  4186. ssl_verify_callback_allow_expired_certificate(int ok, X509_STORE_CTX *store)
  4187. {
  4188. ok = ssl_verify_callback(ok, store);
  4189. if (!ok)
  4190. {
  4191. /* accept self signed certificates, expired certificates, and certficiates w/o CRL */
  4192. switch (X509_STORE_CTX_get_error(store))
  4193. {
  4194. case X509_V_ERR_CERT_NOT_YET_VALID:
  4195. case X509_V_ERR_CERT_HAS_EXPIRED:
  4196. case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT:
  4197. case X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN:
  4198. case X509_V_ERR_UNABLE_TO_GET_CRL:
  4199. case X509_V_ERR_CRL_NOT_YET_VALID:
  4200. case X509_V_ERR_CRL_HAS_EXPIRED:
  4201. X509_STORE_CTX_set_error(store, X509_V_OK);
  4202. ok = 1;
  4203. }
  4204. }
  4205. /* Note: return 1 to continue, but unsafe progress will be terminated by SSL */
  4206. return ok;
  4207. }
  4208. #endif
  4209. /******************************************************************************/
  4210. #ifdef WITH_GNUTLS
  4211. static const char *
  4212. ssl_verify(struct soap *soap, const char *host)
  4213. {
  4214. unsigned int status;
  4215. const char *err = NULL;
  4216. int r = gnutls_certificate_verify_peers2(soap->session, &status);
  4217. if (r < 0)
  4218. err = "Certificate verify error";
  4219. else if ((status & GNUTLS_CERT_INVALID))
  4220. err = "The certificate is not trusted";
  4221. else if ((status & GNUTLS_CERT_SIGNER_NOT_FOUND))
  4222. err = "The certificate hasn't got a known issuer";
  4223. else if ((status & GNUTLS_CERT_REVOKED))
  4224. err = "The certificate has been revoked";
  4225. else if (gnutls_certificate_type_get(soap->session) == GNUTLS_CRT_X509)
  4226. {
  4227. gnutls_x509_crt_t cert;
  4228. const gnutls_datum_t *cert_list;
  4229. unsigned int cert_list_size;
  4230. if (gnutls_x509_crt_init(&cert) < 0)
  4231. err = "Could not get X509 certificates";
  4232. else if ((cert_list = gnutls_certificate_get_peers(soap->session, &cert_list_size)) == NULL)
  4233. err = "Could not get X509 certificates";
  4234. else if (gnutls_x509_crt_import(cert, &cert_list[0], GNUTLS_X509_FMT_DER) < 0)
  4235. err = "Error parsing X509 certificate";
  4236. else if (!(soap->ssl_flags & SOAP_SSL_ALLOW_EXPIRED_CERTIFICATE) && gnutls_x509_crt_get_expiration_time(cert) < time(NULL))
  4237. err = "The certificate has expired";
  4238. else if (!(soap->ssl_flags & SOAP_SSL_ALLOW_EXPIRED_CERTIFICATE) && gnutls_x509_crt_get_activation_time(cert) > time(NULL))
  4239. err = "The certificate is not yet activated";
  4240. else if (host && !(soap->ssl_flags & SOAP_SSL_SKIP_HOST_CHECK))
  4241. {
  4242. if (!gnutls_x509_crt_check_hostname(cert, host))
  4243. err = "Certificate host name mismatch";
  4244. }
  4245. gnutls_x509_crt_deinit(cert);
  4246. }
  4247. return err;
  4248. }
  4249. #endif
  4250. /******************************************************************************/
  4251. #if defined(WITH_OPENSSL) || defined(WITH_GNUTLS)
  4252. #ifndef WITH_NOIO
  4253. SOAP_FMAC1
  4254. int
  4255. SOAP_FMAC2
  4256. soap_ssl_accept(struct soap *soap)
  4257. {
  4258. SOAP_SOCKET sk = soap->socket;
  4259. #ifdef WITH_OPENSSL
  4260. BIO *bio;
  4261. int err = SSL_ERROR_NONE;
  4262. int retries, r, s;
  4263. ERR_clear_error();
  4264. if (!soap_valid_socket(sk))
  4265. return soap_set_receiver_error(soap, "SSL/TLS error", "No socket in soap_ssl_accept()", SOAP_SSL_ERROR);
  4266. soap->ssl_flags &= ~SOAP_SSL_CLIENT;
  4267. if (!soap->ctx && (soap->error = soap->fsslauth(soap)) != SOAP_OK)
  4268. return soap_closesock(soap);
  4269. if (!soap->ssl)
  4270. {
  4271. soap->ssl = SSL_new(soap->ctx);
  4272. if (!soap->ssl)
  4273. {
  4274. soap_closesock(soap);
  4275. return soap_set_receiver_error(soap, "SSL/TLS error", "SSL_new() failed in soap_ssl_accept()", SOAP_SSL_ERROR);
  4276. }
  4277. }
  4278. else
  4279. {
  4280. SSL_clear(soap->ssl);
  4281. }
  4282. bio = BIO_new_socket((int)sk, BIO_NOCLOSE);
  4283. SSL_set_bio(soap->ssl, bio, bio);
  4284. /* Default timeout: 10 sec retries, 100 times 0.1 sec */
  4285. retries = 100;
  4286. if (soap->recv_timeout || soap->send_timeout)
  4287. {
  4288. int t = soap->recv_timeout > soap->send_timeout ? soap->recv_timeout : soap->send_timeout;
  4289. if (t > 0)
  4290. retries = 10 * t;
  4291. else if (t > -100000)
  4292. retries = 1;
  4293. else
  4294. retries = t/-100000;
  4295. }
  4296. SOAP_SOCKNONBLOCK(sk)
  4297. while ((r = SSL_accept(soap->ssl)) <= 0)
  4298. {
  4299. err = SSL_get_error(soap->ssl, r);
  4300. if (err == SSL_ERROR_WANT_ACCEPT || err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE)
  4301. {
  4302. if (err == SSL_ERROR_WANT_READ)
  4303. s = tcp_select(soap, sk, SOAP_TCP_SELECT_RCV | SOAP_TCP_SELECT_ERR, -100000);
  4304. else
  4305. s = tcp_select(soap, sk, SOAP_TCP_SELECT_SND | SOAP_TCP_SELECT_ERR, -100000);
  4306. if (s < 0)
  4307. break;
  4308. }
  4309. else
  4310. {
  4311. soap->errnum = soap_socket_errno(sk);
  4312. break;
  4313. }
  4314. if (retries-- <= 0)
  4315. break;
  4316. }
  4317. if (!soap->recv_timeout && !soap->send_timeout)
  4318. SOAP_SOCKBLOCK(sk)
  4319. if (r <= 0)
  4320. {
  4321. soap_set_receiver_error(soap, soap_ssl_error(soap, r, err), "SSL_accept() failed in soap_ssl_accept()", SOAP_SSL_ERROR);
  4322. return soap_closesock(soap);
  4323. }
  4324. if ((soap->ssl_flags & SOAP_SSL_REQUIRE_CLIENT_AUTHENTICATION))
  4325. {
  4326. X509 *peer;
  4327. int err;
  4328. if ((err = SSL_get_verify_result(soap->ssl)) != X509_V_OK)
  4329. {
  4330. soap_closesock(soap);
  4331. return soap_set_sender_error(soap, X509_verify_cert_error_string(err), "SSL certificate presented by peer cannot be verified in soap_ssl_accept()", SOAP_SSL_ERROR);
  4332. }
  4333. peer = SSL_get_peer_certificate(soap->ssl);
  4334. if (!peer)
  4335. {
  4336. soap_closesock(soap);
  4337. return soap_set_sender_error(soap, "SSL/TLS error", "No SSL certificate was presented by the peer in soap_ssl_accept()", SOAP_SSL_ERROR);
  4338. }
  4339. X509_free(peer);
  4340. }
  4341. #endif
  4342. #ifdef WITH_GNUTLS
  4343. int retries, r, s;
  4344. if (!soap_valid_socket(sk))
  4345. return soap_set_receiver_error(soap, "SSL/TLS error", "No socket in soap_ssl_accept()", SOAP_SSL_ERROR);
  4346. soap->ssl_flags &= ~SOAP_SSL_CLIENT;
  4347. if (!soap->session && (soap->error = soap->fsslauth(soap)) != SOAP_OK)
  4348. return soap_closesock(soap);
  4349. gnutls_transport_set_ptr(soap->session, (gnutls_transport_ptr_t)(long)sk);
  4350. /* default timeout: 10 sec retries, 100 times 0.1 sec */
  4351. retries = 100;
  4352. if (soap->recv_timeout || soap->send_timeout)
  4353. {
  4354. int t = soap->recv_timeout > soap->send_timeout ? soap->recv_timeout : soap->send_timeout;
  4355. if (t > 0)
  4356. retries = 10 * t;
  4357. else if (t > -100000)
  4358. retries = 1;
  4359. else
  4360. retries = t/-100000;
  4361. }
  4362. SOAP_SOCKNONBLOCK(sk)
  4363. while ((r = gnutls_handshake(soap->session)))
  4364. {
  4365. /* GNUTLS repeat handhake when GNUTLS_E_AGAIN */
  4366. if (r == GNUTLS_E_AGAIN || r == GNUTLS_E_INTERRUPTED)
  4367. {
  4368. if (!gnutls_record_get_direction(soap->session))
  4369. s = tcp_select(soap, sk, SOAP_TCP_SELECT_RCV | SOAP_TCP_SELECT_ERR, -100000);
  4370. else
  4371. s = tcp_select(soap, sk, SOAP_TCP_SELECT_SND | SOAP_TCP_SELECT_ERR, -100000);
  4372. if (s < 0)
  4373. break;
  4374. }
  4375. else
  4376. {
  4377. soap->errnum = soap_socket_errno(sk);
  4378. break;
  4379. }
  4380. if (retries-- <= 0)
  4381. break;
  4382. }
  4383. if (!soap->recv_timeout && !soap->send_timeout)
  4384. SOAP_SOCKBLOCK(sk)
  4385. if (r)
  4386. {
  4387. soap_set_receiver_error(soap, soap_ssl_error(soap, r, 0), "SSL/TLS handshake failed", SOAP_SSL_ERROR);
  4388. return soap_closesock(soap);
  4389. }
  4390. if ((soap->ssl_flags & SOAP_SSL_REQUIRE_CLIENT_AUTHENTICATION))
  4391. {
  4392. const char *err = ssl_verify(soap, NULL);
  4393. if (err)
  4394. {
  4395. soap_closesock(soap);
  4396. return soap_set_receiver_error(soap, "SSL/TLS error", err, SOAP_SSL_ERROR);
  4397. }
  4398. }
  4399. #endif
  4400. #ifdef WITH_SYSTEMSSL
  4401. gsk_iocallback local_io = { ssl_recv, ssl_send, NULL, NULL, NULL, NULL };
  4402. int retries, r, s;
  4403. if (!soap_valid_socket(sk))
  4404. return soap_set_receiver_error(soap, "SSL/TLS error", "No socket in soap_ssl_accept()", SOAP_SSL_ERROR);
  4405. soap->ssl_flags &= ~SOAP_SSL_CLIENT;
  4406. /* default timeout: 10 sec retries, 100 times 0.1 sec */
  4407. retries = 100;
  4408. if (soap->recv_timeout || soap->send_timeout)
  4409. {
  4410. int t = soap->recv_timeout > soap->send_timeout ? soap->recv_timeout : soap->send_timeout;
  4411. if (t > 0)
  4412. retries = 10 * t;
  4413. else if (t > -100000)
  4414. retries = 1;
  4415. else
  4416. retries = t/-100000;
  4417. }
  4418. SOAP_SOCKNONBLOCK(sk)
  4419. r = gsk_secure_socket_open(soap->ctx, &soap->ssl);
  4420. if (r == GSK_OK)
  4421. r = gsk_attribute_set_numeric_value(soap->ssl, GSK_FD, sk);
  4422. if (r == GSK_OK)
  4423. r = gsk_attribute_set_buffer(soap->ssl, GSK_KEYRING_LABEL, soap->cafile, 0);
  4424. if (r == GSK_OK)
  4425. r = gsk_attribute_set_enum(soap->ssl, GSK_SESSION_TYPE, GSK_SERVER_SESSION);
  4426. if (r == GSK_OK)
  4427. r = gsk_attribute_set_buffer(soap->ssl, GSK_V3_CIPHER_SPECS_EXPANDED, "0035002F000A", 0);
  4428. if (r == GSK_OK)
  4429. r = gsk_attribute_set_enum(soap->ssl, GSK_V3_CIPHERS, GSK_V3_CIPHERS_CHAR4);
  4430. if (r == GSK_OK)
  4431. r = gsk_attribute_set_callback(soap->ssl, GSK_IO_CALLBACK, &local_io);
  4432. if (r != GSK_OK)
  4433. return soap_set_receiver_error(soap, gsk_strerror(r), "SYSTEM SSL error in soap_ssl_accept()", SOAP_SSL_ERROR);
  4434. while ((r = gsk_secure_socket_init(soap->ssl)) != GSK_OK)
  4435. {
  4436. if (r == GSK_WOULD_BLOCK_READ || r == GSK_WOULD_BLOCK_WRITE)
  4437. {
  4438. if (r == GSK_WOULD_BLOCK_READ)
  4439. s = tcp_select(soap, sk, SOAP_TCP_SELECT_RCV | SOAP_TCP_SELECT_ERR, -100000);
  4440. else
  4441. s = tcp_select(soap, sk, SOAP_TCP_SELECT_SND | SOAP_TCP_SELECT_ERR, -100000);
  4442. if (s < 0)
  4443. break;
  4444. }
  4445. else
  4446. {
  4447. soap->errnum = soap_socket_errno(sk);
  4448. break;
  4449. }
  4450. if (retries-- <= 0)
  4451. break;
  4452. }
  4453. if (!soap->recv_timeout && !soap->send_timeout)
  4454. SOAP_SOCKBLOCK(sk)
  4455. if (r != GSK_OK)
  4456. {
  4457. soap_set_receiver_error(soap, gsk_strerror(r), "gsk_secure_socket_init() failed in soap_ssl_accept()", SOAP_SSL_ERROR);
  4458. return soap_closesock(soap);
  4459. }
  4460. #endif
  4461. soap->imode |= SOAP_ENC_SSL;
  4462. soap->omode |= SOAP_ENC_SSL;
  4463. return SOAP_OK;
  4464. }
  4465. #endif
  4466. #endif
  4467. /******************************************************************************\
  4468. *
  4469. * TCP/UDP [SSL/TLS] IPv4 and IPv6
  4470. *
  4471. \******************************************************************************/
  4472. #ifndef WITH_NOIO
  4473. static int
  4474. tcp_init(struct soap *soap) {
  4475. soap->errmode = 1;
  4476. #ifdef WIN32
  4477. if (tcp_done)
  4478. return 0;
  4479. else
  4480. {
  4481. WSADATA w;
  4482. if (WSAStartup(MAKEWORD(1, 1), &w))
  4483. return -1;
  4484. tcp_done = 1;
  4485. }
  4486. #endif
  4487. return 0;
  4488. }
  4489. #endif
  4490. /******************************************************************************/
  4491. #ifndef WITH_NOIO
  4492. static const char *
  4493. tcp_error(struct soap *soap) {
  4494. const char *msg = NULL;
  4495. switch (soap->errmode) {
  4496. case 0:
  4497. msg = soap_strerror(soap);
  4498. break;
  4499. case 1:
  4500. msg = "WSAStartup failed";
  4501. break;
  4502. case 2: {
  4503. #ifndef WITH_LEAN
  4504. msg = soap_code_str(h_error_codes, soap->errnum);
  4505. if (!msg)
  4506. #endif
  4507. {
  4508. (SOAP_SNPRINTF(soap->msgbuf, sizeof(soap->msgbuf), 37), "TCP/UDP IP error %d", soap->errnum);
  4509. msg = soap->msgbuf;
  4510. }
  4511. }
  4512. }
  4513. return msg;
  4514. }
  4515. #endif
  4516. /******************************************************************************/
  4517. #if !defined(WITH_IPV6) || defined(WITH_COOKIES)
  4518. #ifndef WITH_NOIO
  4519. static int
  4520. tcp_gethostbyname(struct soap *soap, const char *addr, struct hostent *hostent, struct in_addr *inaddr) {
  4521. #if defined(__GLIBC__) && (!_GNU_SOURCE && !defined(_POSIX_C_SOURCE) && !defined(_XOPEN_SOURCE) && defined(HAVE_GETHOSTBYNAME_R)) || _POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600 || defined(__ANDROID__) || (defined(HAVE_GETHOSTBYNAME_R) && (defined(FREEBSD) || defined(__FreeBSD__)))
  4522. int r;
  4523. char *tmpbuf = soap->tmpbuf;
  4524. size_t tmplen = sizeof(soap->tmpbuf);
  4525. #elif (defined(_AIX43) || defined(TRU64) || defined(HP_UX)) && defined(HAVE_GETHOSTBYNAME_R)
  4526. struct hostent_data ht_data;
  4527. #elif defined(HAVE_GETHOSTBYNAME_R)
  4528. char *tmpbuf = soap->tmpbuf;
  4529. size_t tmplen = sizeof(soap->tmpbuf);
  4530. #endif
  4531. #ifdef VXWORKS
  4532. int hostint; /* vxWorks compatible */
  4533. #endif
  4534. if (inaddr) {
  4535. soap_int32 iadd = -1;
  4536. #ifdef AS400
  4537. iadd = inet_addr((void*)addr);
  4538. #else
  4539. iadd = inet_addr((char *) addr);
  4540. #endif
  4541. if (iadd != -1) {
  4542. if (soap_memcpy((void *) inaddr, sizeof(struct in_addr), (const void *) &iadd, sizeof(iadd)))
  4543. return soap->error = SOAP_EOM;
  4544. return SOAP_OK;
  4545. }
  4546. }
  4547. #if defined(__GLIBC__) && (!_GNU_SOURCE && !defined(_POSIX_C_SOURCE) && !defined(_XOPEN_SOURCE) && defined(HAVE_GETHOSTBYNAME_R)) || _POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600 || defined(__ANDROID__) || (defined(HAVE_GETHOSTBYNAME_R) && (defined(FREEBSD) || defined(__FreeBSD__)))
  4548. while ((r = gethostbyname_r(addr, hostent, tmpbuf, tmplen, &hostent, &soap->errnum)) < 0) {
  4549. if (tmpbuf != soap->tmpbuf)
  4550. SOAP_FREE(soap, tmpbuf);
  4551. if (r != SOAP_ERANGE) {
  4552. hostent = NULL;
  4553. break;
  4554. }
  4555. tmplen *= 2;
  4556. tmpbuf = (char *) SOAP_MALLOC(soap, tmplen);
  4557. if (!tmpbuf)
  4558. break;
  4559. }
  4560. #elif (defined(_AIX43) || defined(TRU64) || defined(HP_UX)) && defined(HAVE_GETHOSTBYNAME_R)
  4561. memset((void*)&ht_data, 0, sizeof(ht_data));
  4562. if (gethostbyname_r(addr, hostent, &ht_data) < 0)
  4563. {
  4564. hostent = NULL;
  4565. soap->errnum = h_errno;
  4566. }
  4567. #elif defined(HAVE_GETHOSTBYNAME_R)
  4568. hostent = gethostbyname_r(addr, hostent, tmpbuf, tmplen, &soap->errnum);
  4569. #elif defined(VXWORKS)
  4570. /* vxWorks compatible */
  4571. /* If the DNS resolver library resolvLib has been configured in the vxWorks
  4572. * image, a query for the host IP address is sent to the DNS server, if the
  4573. * name was not found in the local host table. */
  4574. hostint = hostGetByName((char*)addr);
  4575. if (hostint == ERROR)
  4576. {
  4577. hostent = NULL;
  4578. soap->errnum = soap_errno;
  4579. }
  4580. #else
  4581. #ifdef AS400
  4582. hostent = gethostbyname((void*)addr);
  4583. #else
  4584. hostent = gethostbyname((char*)addr);
  4585. #endif
  4586. if (!hostent)
  4587. soap->errnum = h_errno;
  4588. #endif
  4589. if (!hostent) {
  4590. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Host name not found\n"));
  4591. return SOAP_ERR;
  4592. }
  4593. if (inaddr) {
  4594. #ifdef VXWORKS
  4595. inaddr->s_addr = hostint; /* vxWorks compatible */
  4596. #else
  4597. if (soap_memcpy((void *) inaddr, sizeof(struct in_addr), (const void *) hostent->h_addr,
  4598. (size_t) hostent->h_length)) {
  4599. #if defined(__GLIBC__) && (!_GNU_SOURCE && !defined(_POSIX_C_SOURCE) && !defined(_XOPEN_SOURCE) && defined(HAVE_GETHOSTBYNAME_R)) || _POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600 || defined(__ANDROID__) || (defined(HAVE_GETHOSTBYNAME_R) && (defined(__GNU__) || defined(__GNUC__))) || (defined(HAVE_GETHOSTBYNAME_R) && (defined(FREEBSD) || defined(__FreeBSD__)))
  4600. if (tmpbuf && tmpbuf != soap->tmpbuf)
  4601. SOAP_FREE(soap, tmpbuf);
  4602. #endif
  4603. return soap->error = SOAP_EOM;
  4604. }
  4605. #endif
  4606. }
  4607. #if defined(__GLIBC__) && (!_GNU_SOURCE && !defined(_POSIX_C_SOURCE) && !defined(_XOPEN_SOURCE) && defined(HAVE_GETHOSTBYNAME_R)) || _POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600 || defined(__ANDROID__) || (defined(HAVE_GETHOSTBYNAME_R) && (defined(__GNU__) || defined(__GNUC__))) || (defined(HAVE_GETHOSTBYNAME_R) && (defined(FREEBSD) || defined(__FreeBSD__)))
  4608. if (tmpbuf && tmpbuf != soap->tmpbuf)
  4609. SOAP_FREE(soap, tmpbuf);
  4610. #endif
  4611. return SOAP_OK;
  4612. }
  4613. #endif
  4614. #endif
  4615. /******************************************************************************/
  4616. #if !defined(WITH_IPV6)
  4617. #ifndef WITH_NOIO
  4618. static int
  4619. tcp_gethost(struct soap *soap, const char *addr, struct in_addr *inaddr) {
  4620. struct hostent hostent;
  4621. return tcp_gethostbyname(soap, addr, &hostent, inaddr);
  4622. }
  4623. #endif
  4624. #endif
  4625. /******************************************************************************/
  4626. #ifndef WITH_NOIO
  4627. static SOAP_SOCKET
  4628. tcp_connect(struct soap *soap, const char *endpoint, const char *host, int port) {
  4629. #ifdef WITH_IPV6
  4630. struct addrinfo hints, *res, *ressave;
  4631. #endif
  4632. SOAP_SOCKET sk;
  4633. int err = 0;
  4634. #ifndef WITH_LEAN
  4635. int set = 1;
  4636. #endif
  4637. #if !defined(WITH_LEAN) || defined(WITH_OPENSSL) || defined(WITH_GNUTLS) || defined(WITH_SYSTEMSSL)
  4638. int retries;
  4639. #endif
  4640. soap->errnum = 0;
  4641. soap->errmode = 0;
  4642. if (soap_valid_socket(soap->socket)) {
  4643. if ((soap->omode & SOAP_IO_UDP) && soap->socket == soap->master) {
  4644. #ifdef IP_MULTICAST_TTL
  4645. #ifndef WITH_IPV6
  4646. soap->errmode = 2;
  4647. if (soap->fresolve(soap, host, &soap->peer.in.sin_addr)) {
  4648. soap_set_receiver_error(soap, tcp_error(soap), "get host by name failed in tcp_connect()",
  4649. SOAP_TCP_ERROR);
  4650. soap->fclosesocket(soap, soap->socket);
  4651. return soap->socket = SOAP_INVALID_SOCKET;
  4652. }
  4653. soap->peer.in.sin_port = htons((short) port);
  4654. soap->errmode = 0;
  4655. #else
  4656. if (getaddrinfo(host, soap_int2s(soap, port), &hints, &res) || !res)
  4657. {
  4658. soap_set_receiver_error(soap, SOAP_GAI_STRERROR(err), "getaddrinfo failed in tcp_connect()", SOAP_TCP_ERROR);
  4659. soap->fclosesocket(soap, soap->socket);
  4660. return soap->socket = SOAP_INVALID_SOCKET;
  4661. }
  4662. if (soap_memcpy((void*)&soap->peer.storage, sizeof(soap->peer.storage), (const void*)res->ai_addr, res->ai_addrlen))
  4663. {
  4664. soap->error = SOAP_EOM;
  4665. soap->fclosesocket(soap, soap->socket);
  4666. freeaddrinfo(res);
  4667. return soap->socket = SOAP_INVALID_SOCKET;
  4668. }
  4669. soap->peerlen = res->ai_addrlen;
  4670. freeaddrinfo(res);
  4671. #endif
  4672. if (soap->ipv4_multicast_ttl) {
  4673. unsigned char ttl = soap->ipv4_multicast_ttl;
  4674. if (setsockopt(soap->socket, IPPROTO_IP, IP_MULTICAST_TTL, (char *) &ttl, sizeof(ttl))) {
  4675. soap->errnum = soap_socket_errno(soap->socket);
  4676. soap_set_receiver_error(soap, tcp_error(soap),
  4677. "setsockopt IP_MULTICAST_TTL failed in tcp_connect()", SOAP_TCP_ERROR);
  4678. soap->fclosesocket(soap, soap->socket);
  4679. return soap->socket = SOAP_INVALID_SOCKET;
  4680. }
  4681. }
  4682. if (soap->ipv4_multicast_if && !soap->ipv6_multicast_if) {
  4683. if (setsockopt(soap->socket, IPPROTO_IP, IP_MULTICAST_IF, (char *) soap->ipv4_multicast_if,
  4684. sizeof(struct in_addr)))
  4685. #ifndef WINDOWS
  4686. {
  4687. soap->errnum = soap_socket_errno(soap->socket);
  4688. soap_set_receiver_error(soap, tcp_error(soap), "setsockopt IP_MULTICAST_IF failed in tcp_connect()",
  4689. SOAP_TCP_ERROR);
  4690. soap->fclosesocket(soap, soap->socket);
  4691. return soap->socket = SOAP_INVALID_SOCKET;
  4692. }
  4693. #else
  4694. #ifndef IP_MULTICAST_IF
  4695. #define IP_MULTICAST_IF 2
  4696. #endif
  4697. if (setsockopt(soap->socket, IPPROTO_IP, IP_MULTICAST_IF, (char*)soap->ipv4_multicast_if, sizeof(struct in_addr)))
  4698. {
  4699. soap->errnum = soap_socket_errno(soap->socket);
  4700. soap_set_receiver_error(soap, tcp_error(soap), "setsockopt IP_MULTICAST_IF failed in tcp_connect()", SOAP_TCP_ERROR);
  4701. soap->fclosesocket(soap, soap->socket);
  4702. return soap->socket = SOAP_INVALID_SOCKET;
  4703. }
  4704. #endif
  4705. }
  4706. #endif
  4707. return soap->socket;
  4708. }
  4709. soap->fclosesocket(soap, soap->socket);
  4710. soap->socket = SOAP_INVALID_SOCKET;
  4711. }
  4712. if (tcp_init(soap)) {
  4713. soap_set_receiver_error(soap, tcp_error(soap), "TCP init failed in tcp_connect()", SOAP_TCP_ERROR);
  4714. return SOAP_INVALID_SOCKET;
  4715. }
  4716. soap->errmode = 0;
  4717. #ifdef WITH_IPV6
  4718. memset((void*)&hints, 0, sizeof(hints));
  4719. hints.ai_family = PF_UNSPEC;
  4720. #ifndef WITH_LEAN
  4721. if ((soap->omode & SOAP_IO_UDP))
  4722. hints.ai_socktype = SOCK_DGRAM;
  4723. else
  4724. #endif
  4725. hints.ai_socktype = SOCK_STREAM;
  4726. soap->errmode = 2;
  4727. if (soap->proxy_host)
  4728. err = getaddrinfo(soap->proxy_host, soap_int2s(soap, soap->proxy_port), &hints, &res);
  4729. else
  4730. err = getaddrinfo(host, soap_int2s(soap, port), &hints, &res);
  4731. if (err || !res)
  4732. {
  4733. soap_set_receiver_error(soap, SOAP_GAI_STRERROR(err), "getaddrinfo failed in tcp_connect()", SOAP_TCP_ERROR);
  4734. return SOAP_INVALID_SOCKET;
  4735. }
  4736. ressave = res;
  4737. again:
  4738. sk = soap->socket = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
  4739. soap->error = SOAP_OK;
  4740. soap->errmode = 0;
  4741. #else
  4742. #ifndef WITH_LEAN
  4743. again:
  4744. #endif
  4745. #ifndef WITH_LEAN
  4746. if ((soap->omode & SOAP_IO_UDP))
  4747. sk = soap->socket = socket(AF_INET, SOCK_DGRAM, 0);
  4748. else
  4749. #endif
  4750. sk = soap->socket = socket(AF_INET, SOCK_STREAM, 0);
  4751. #endif
  4752. if (!soap_valid_socket(sk)) {
  4753. #ifdef WITH_IPV6
  4754. if (res->ai_next)
  4755. {
  4756. res = res->ai_next;
  4757. goto again;
  4758. }
  4759. #endif
  4760. soap->errnum = soap_socket_errno(sk);
  4761. soap_set_receiver_error(soap, tcp_error(soap), "socket failed in tcp_connect()", SOAP_TCP_ERROR);
  4762. #ifdef WITH_IPV6
  4763. freeaddrinfo(ressave);
  4764. #endif
  4765. return SOAP_INVALID_SOCKET;
  4766. }
  4767. #ifdef WITH_SOCKET_CLOSE_ON_EXIT
  4768. #ifdef WIN32
  4769. #ifndef UNDER_CE
  4770. SetHandleInformation((HANDLE)sk, HANDLE_FLAG_INHERIT, 0);
  4771. #endif
  4772. #else
  4773. fcntl(sk, F_SETFD, 1);
  4774. #endif
  4775. #endif
  4776. #ifndef WITH_LEAN
  4777. if ((soap->connect_flags & SO_LINGER)) {
  4778. struct linger linger;
  4779. memset((void *) &linger, 0, sizeof(linger));
  4780. linger.l_onoff = 1;
  4781. linger.l_linger = soap->linger_time;
  4782. if (setsockopt(sk, SOL_SOCKET, SO_LINGER, (char *) &linger, sizeof(struct linger))) {
  4783. soap->errnum = soap_socket_errno(sk);
  4784. soap_set_receiver_error(soap, tcp_error(soap), "setsockopt SO_LINGER failed in tcp_connect()",
  4785. SOAP_TCP_ERROR);
  4786. soap->fclosesocket(soap, sk);
  4787. #ifdef WITH_IPV6
  4788. freeaddrinfo(ressave);
  4789. #endif
  4790. return soap->socket = SOAP_INVALID_SOCKET;
  4791. }
  4792. }
  4793. if ((soap->connect_flags & ~SO_LINGER) &&
  4794. setsockopt(sk, SOL_SOCKET, soap->connect_flags & ~SO_LINGER, (char *) &set, sizeof(int))) {
  4795. soap->errnum = soap_socket_errno(sk);
  4796. #ifdef WITH_IPV6
  4797. freeaddrinfo(ressave);
  4798. #endif
  4799. soap_set_receiver_error(soap, tcp_error(soap), "setsockopt failed in tcp_connect()", SOAP_TCP_ERROR);
  4800. soap->fclosesocket(soap, sk);
  4801. return soap->socket = SOAP_INVALID_SOCKET;
  4802. }
  4803. #ifndef UNDER_CE
  4804. if ((soap->keep_alive || soap->tcp_keep_alive) &&
  4805. setsockopt(sk, SOL_SOCKET, SO_KEEPALIVE, (char *) &set, sizeof(int))) {
  4806. soap->errnum = soap_socket_errno(sk);
  4807. #ifdef WITH_IPV6
  4808. freeaddrinfo(ressave);
  4809. #endif
  4810. soap_set_receiver_error(soap, tcp_error(soap), "setsockopt SO_KEEPALIVE failed in tcp_connect()",
  4811. SOAP_TCP_ERROR);
  4812. soap->fclosesocket(soap, sk);
  4813. return soap->socket = SOAP_INVALID_SOCKET;
  4814. }
  4815. if (soap->sndbuf > 0 && setsockopt(sk, SOL_SOCKET, SO_SNDBUF, (char *) &soap->sndbuf, sizeof(int))) {
  4816. soap->errnum = soap_socket_errno(sk);
  4817. #ifdef WITH_IPV6
  4818. freeaddrinfo(ressave);
  4819. #endif
  4820. soap_set_receiver_error(soap, tcp_error(soap), "setsockopt SO_SNDBUF failed in tcp_connect()", SOAP_TCP_ERROR);
  4821. soap->fclosesocket(soap, sk);
  4822. return soap->socket = SOAP_INVALID_SOCKET;
  4823. }
  4824. if (soap->rcvbuf > 0 && setsockopt(sk, SOL_SOCKET, SO_RCVBUF, (char *) &soap->rcvbuf, sizeof(int))) {
  4825. soap->errnum = soap_socket_errno(sk);
  4826. #ifdef WITH_IPV6
  4827. freeaddrinfo(ressave);
  4828. #endif
  4829. soap_set_receiver_error(soap, tcp_error(soap), "setsockopt SO_RCVBUF failed in tcp_connect()", SOAP_TCP_ERROR);
  4830. soap->fclosesocket(soap, sk);
  4831. return soap->socket = SOAP_INVALID_SOCKET;
  4832. }
  4833. #ifdef TCP_KEEPIDLE
  4834. if (soap->tcp_keep_idle &&
  4835. setsockopt(sk, IPPROTO_TCP, TCP_KEEPIDLE, (char *) &(soap->tcp_keep_idle), sizeof(int))) {
  4836. soap->errnum = soap_socket_errno(sk);
  4837. #ifdef WITH_IPV6
  4838. freeaddrinfo(ressave);
  4839. #endif
  4840. soap_set_receiver_error(soap, tcp_error(soap), "setsockopt TCP_KEEPIDLE failed in tcp_connect()",
  4841. SOAP_TCP_ERROR);
  4842. soap->fclosesocket(soap, sk);
  4843. return soap->socket = SOAP_INVALID_SOCKET;
  4844. }
  4845. #endif
  4846. #ifdef TCP_KEEPINTVL
  4847. if (soap->tcp_keep_intvl &&
  4848. setsockopt(sk, IPPROTO_TCP, TCP_KEEPINTVL, (char *) &(soap->tcp_keep_intvl), sizeof(int))) {
  4849. soap->errnum = soap_socket_errno(sk);
  4850. #ifdef WITH_IPV6
  4851. freeaddrinfo(ressave);
  4852. #endif
  4853. soap_set_receiver_error(soap, tcp_error(soap), "setsockopt TCP_KEEPINTVL failed in tcp_connect()",
  4854. SOAP_TCP_ERROR);
  4855. soap->fclosesocket(soap, sk);
  4856. return soap->socket = SOAP_INVALID_SOCKET;
  4857. }
  4858. #endif
  4859. #ifdef TCP_KEEPCNT
  4860. if (soap->tcp_keep_cnt && setsockopt(sk, IPPROTO_TCP, TCP_KEEPCNT, (char *) &(soap->tcp_keep_cnt), sizeof(int))) {
  4861. soap->errnum = soap_socket_errno(sk);
  4862. #ifdef WITH_IPV6
  4863. freeaddrinfo(ressave);
  4864. #endif
  4865. soap_set_receiver_error(soap, tcp_error(soap), "setsockopt TCP_KEEPCNT failed in tcp_connect()",
  4866. SOAP_TCP_ERROR);
  4867. soap->fclosesocket(soap, sk);
  4868. return soap->socket = SOAP_INVALID_SOCKET;
  4869. }
  4870. #endif
  4871. #ifdef TCP_NODELAY
  4872. if (!(soap->omode & SOAP_IO_UDP) && setsockopt(sk, IPPROTO_TCP, TCP_NODELAY, (char *) &set, sizeof(int))) {
  4873. soap->errnum = soap_socket_errno(sk);
  4874. #ifdef WITH_IPV6
  4875. freeaddrinfo(ressave);
  4876. #endif
  4877. soap_set_receiver_error(soap, tcp_error(soap), "setsockopt TCP_NODELAY failed in tcp_connect()",
  4878. SOAP_TCP_ERROR);
  4879. soap->fclosesocket(soap, sk);
  4880. return soap->socket = SOAP_INVALID_SOCKET;
  4881. }
  4882. #endif
  4883. #ifdef WITH_IPV6
  4884. if ((soap->omode & SOAP_IO_UDP) && soap->ipv6_multicast_if)
  4885. {
  4886. struct sockaddr_in6 *in6addr = (struct sockaddr_in6*)res->ai_addr;
  4887. in6addr->sin6_scope_id = soap->ipv6_multicast_if;
  4888. }
  4889. #endif
  4890. #endif
  4891. #ifdef IP_MULTICAST_TTL
  4892. if ((soap->omode & SOAP_IO_UDP)) {
  4893. if (soap->ipv4_multicast_ttl) {
  4894. unsigned char ttl = soap->ipv4_multicast_ttl;
  4895. if (setsockopt(sk, IPPROTO_IP, IP_MULTICAST_TTL, (char *) &ttl, sizeof(ttl))) {
  4896. soap->errnum = soap_socket_errno(sk);
  4897. #ifdef WITH_IPV6
  4898. freeaddrinfo(ressave);
  4899. #endif
  4900. soap_set_receiver_error(soap, tcp_error(soap), "setsockopt IP_MULTICAST_TTL failed in tcp_connect()",
  4901. SOAP_TCP_ERROR);
  4902. soap->fclosesocket(soap, sk);
  4903. return soap->socket = SOAP_INVALID_SOCKET;
  4904. }
  4905. }
  4906. if ((soap->omode & SOAP_IO_UDP) && soap->ipv4_multicast_if && !soap->ipv6_multicast_if) {
  4907. if (setsockopt(sk, IPPROTO_IP, IP_MULTICAST_IF, (char *) soap->ipv4_multicast_if, sizeof(struct in_addr)))
  4908. #ifndef WINDOWS
  4909. {
  4910. soap->errnum = soap_socket_errno(sk);
  4911. #ifdef WITH_IPV6
  4912. freeaddrinfo(ressave);
  4913. #endif
  4914. soap_set_receiver_error(soap, tcp_error(soap), "setsockopt IP_MULTICAST_IF failed in tcp_connect()",
  4915. SOAP_TCP_ERROR);
  4916. soap->fclosesocket(soap, sk);
  4917. return soap->socket = SOAP_INVALID_SOCKET;
  4918. }
  4919. #else
  4920. #ifndef IP_MULTICAST_IF
  4921. #define IP_MULTICAST_IF 2
  4922. #endif
  4923. if (setsockopt(sk, IPPROTO_IP, IP_MULTICAST_IF, (char*)soap->ipv4_multicast_if, sizeof(struct in_addr)))
  4924. {
  4925. soap->errnum = soap_socket_errno(sk);
  4926. #ifdef WITH_IPV6
  4927. freeaddrinfo(ressave);
  4928. #endif
  4929. soap_set_receiver_error(soap, tcp_error(soap), "setsockopt IP_MULTICAST_IF failed in tcp_connect()", SOAP_TCP_ERROR);
  4930. soap->fclosesocket(soap, sk);
  4931. return soap->socket = SOAP_INVALID_SOCKET;
  4932. }
  4933. #endif
  4934. }
  4935. }
  4936. #endif
  4937. #endif
  4938. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Opening socket=%d to host='%s' port=%d\n", (int) sk, host, port));
  4939. #ifndef WITH_IPV6
  4940. soap->peerlen = sizeof(soap->peer.in);
  4941. memset((void *) &soap->peer.in, 0, sizeof(soap->peer.in));
  4942. soap->peer.in.sin_family = AF_INET;
  4943. if (soap->client_port >= 0) {
  4944. struct sockaddr_in addr;
  4945. memset((void *) &addr, 0, sizeof(addr));
  4946. addr.sin_family = AF_INET;
  4947. addr.sin_port = htons(soap->client_port);
  4948. if (bind(sk, (struct sockaddr *) &addr, sizeof(addr))) {
  4949. soap->errnum = soap_socket_errno(sk);
  4950. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Could not bind before connect\n"));
  4951. soap_set_receiver_error(soap, tcp_error(soap), "bind failed in tcp_connect()", SOAP_TCP_ERROR);
  4952. soap->fclosesocket(soap, sk);
  4953. soap->client_port = -1;
  4954. return soap->socket = SOAP_INVALID_SOCKET;
  4955. }
  4956. soap->client_port = -1; /* disable bind before connect, so need to set it again before the next connect */
  4957. }
  4958. #ifndef WIN32
  4959. if (soap->client_interface) {
  4960. if (inet_pton(AF_INET, soap->client_interface, &soap->peer.in.sin_addr) != 1) {
  4961. soap->errnum = soap_socket_errno(sk);
  4962. soap_set_receiver_error(soap, tcp_error(soap), "inet_pton() failed in tcp_connect()", SOAP_TCP_ERROR);
  4963. soap->fclosesocket(soap, sk);
  4964. soap->client_interface = NULL;
  4965. return soap->socket = SOAP_INVALID_SOCKET;
  4966. }
  4967. soap->client_interface = NULL; /* disable client interface, so need to set it again before the next connect */
  4968. }
  4969. #endif
  4970. soap->errmode = 2;
  4971. if (soap->proxy_host) {
  4972. if (soap->fresolve(soap, soap->proxy_host, &soap->peer.in.sin_addr)) {
  4973. soap_set_receiver_error(soap, tcp_error(soap), "get proxy host by name failed in tcp_connect()",
  4974. SOAP_TCP_ERROR);
  4975. soap->fclosesocket(soap, sk);
  4976. return soap->socket = SOAP_INVALID_SOCKET;
  4977. }
  4978. soap->peer.in.sin_port = htons((short) soap->proxy_port);
  4979. } else {
  4980. if (soap->fresolve(soap, host, &soap->peer.in.sin_addr)) {
  4981. soap_set_receiver_error(soap, tcp_error(soap), "get host by name failed in tcp_connect()", SOAP_TCP_ERROR);
  4982. soap->fclosesocket(soap, sk);
  4983. return soap->socket = SOAP_INVALID_SOCKET;
  4984. }
  4985. soap->peer.in.sin_port = htons((short) port);
  4986. }
  4987. soap->errmode = 0;
  4988. #ifndef WITH_LEAN
  4989. if ((soap->omode & SOAP_IO_UDP))
  4990. return sk;
  4991. #endif
  4992. #else
  4993. if (soap->client_port >= 0)
  4994. {
  4995. struct sockaddr_in6 addr;
  4996. memset((void*)&addr, 0, sizeof(addr));
  4997. addr.sin6_family = AF_INET6;
  4998. addr.sin6_port = htons(soap->client_port);
  4999. if (bind(sk, (struct sockaddr*)&addr, sizeof(addr)))
  5000. {
  5001. soap->errnum = soap_socket_errno(sk);
  5002. freeaddrinfo(ressave);
  5003. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Could not bind before connect\n"));
  5004. soap_set_receiver_error(soap, tcp_error(soap), "bind failed in tcp_connect()", SOAP_TCP_ERROR);
  5005. soap->fclosesocket(soap, sk);
  5006. soap->client_port = -1;
  5007. return soap->socket = SOAP_INVALID_SOCKET;
  5008. }
  5009. soap->client_port = -1; /* disable bind before connect, so need to set t again before the next connect */
  5010. }
  5011. if (soap->client_interface)
  5012. {
  5013. if (inet_pton(AF_INET6, soap->client_interface, res->ai_addr) != 1)
  5014. {
  5015. if (inet_pton(AF_INET, soap->client_interface, res->ai_addr) != 1)
  5016. {
  5017. soap->errnum = soap_socket_errno(sk);
  5018. freeaddrinfo(ressave);
  5019. soap_set_receiver_error(soap, tcp_error(soap), "inet_pton() failed in tcp_connect()", SOAP_TCP_ERROR);
  5020. soap->fclosesocket(soap, sk);
  5021. soap->client_interface = NULL;
  5022. return soap->socket = SOAP_INVALID_SOCKET;
  5023. }
  5024. }
  5025. soap->client_interface = NULL; /* disable client interface, so need to set it again before the next connect */
  5026. }
  5027. #ifndef WITH_LEAN
  5028. if ((soap->omode & SOAP_IO_UDP))
  5029. {
  5030. if (soap_memcpy((void*)&soap->peer.storage, sizeof(soap->peer.storage), (const void*)res->ai_addr, res->ai_addrlen))
  5031. {
  5032. soap->error = SOAP_EOM;
  5033. soap->fclosesocket(soap, sk);
  5034. soap->socket = sk = SOAP_INVALID_SOCKET;
  5035. }
  5036. soap->peerlen = res->ai_addrlen;
  5037. freeaddrinfo(ressave);
  5038. return sk;
  5039. }
  5040. #endif
  5041. #endif
  5042. #ifndef WITH_LEAN
  5043. if (soap->connect_timeout)
  5044. SOAP_SOCKNONBLOCK(sk)
  5045. else
  5046. SOAP_SOCKBLOCK(sk)
  5047. retries = 10;
  5048. #endif
  5049. for (;;) {
  5050. #ifdef WITH_IPV6
  5051. if (connect(sk, res->ai_addr, (int)res->ai_addrlen))
  5052. #else
  5053. if (connect(sk, &soap->peer.addr, sizeof(soap->peer.in)))
  5054. #endif
  5055. {
  5056. err = soap_socket_errno(sk);
  5057. #ifdef WITH_IPV6
  5058. if (err == SOAP_ECONNREFUSED && res->ai_next)
  5059. {
  5060. soap->fclosesocket(soap, sk);
  5061. res = res->ai_next;
  5062. goto again;
  5063. }
  5064. #endif
  5065. #ifndef WITH_LEAN
  5066. if (err == SOAP_EADDRINUSE) {
  5067. soap->fclosesocket(soap, sk);
  5068. if (retries-- > 0)
  5069. goto again;
  5070. } else if (soap->connect_timeout &&
  5071. (err == SOAP_EINPROGRESS || err == SOAP_EAGAIN || err == SOAP_EWOULDBLOCK)) {
  5072. SOAP_SOCKLEN_T k;
  5073. for (;;) {
  5074. int r;
  5075. #ifdef WITH_SELF_PIPE
  5076. r = tcp_select(soap, sk, SOAP_TCP_SELECT_SND | SOAP_TCP_SELECT_PIP, soap->connect_timeout);
  5077. if ((r & SOAP_TCP_SELECT_PIP))
  5078. {
  5079. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Connection closed by self pipe\n"));
  5080. soap->fclosesocket(soap, sk);
  5081. return soap->socket = SOAP_INVALID_SOCKET;
  5082. }
  5083. #else
  5084. r = tcp_select(soap, sk, SOAP_TCP_SELECT_SND, soap->connect_timeout);
  5085. #endif
  5086. if (r > 0)
  5087. break;
  5088. if (!r) {
  5089. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Connect timeout\n"));
  5090. soap_set_receiver_error(soap, "Timeout", "connect failed in tcp_connect()", SOAP_TCP_ERROR);
  5091. soap->fclosesocket(soap, sk);
  5092. #ifdef WITH_IPV6
  5093. if (res->ai_next)
  5094. {
  5095. res = res->ai_next;
  5096. goto again;
  5097. }
  5098. freeaddrinfo(ressave);
  5099. #endif
  5100. return soap->socket = SOAP_INVALID_SOCKET;
  5101. }
  5102. r = soap->errnum = soap_socket_errno(sk);
  5103. if (r != SOAP_EINTR) {
  5104. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Could not connect to host\n"));
  5105. soap_set_receiver_error(soap, tcp_error(soap), "connect failed in tcp_connect()",
  5106. SOAP_TCP_ERROR);
  5107. soap->fclosesocket(soap, sk);
  5108. #ifdef WITH_IPV6
  5109. if (res->ai_next)
  5110. {
  5111. res = res->ai_next;
  5112. goto again;
  5113. }
  5114. freeaddrinfo(ressave);
  5115. #endif
  5116. return soap->socket = SOAP_INVALID_SOCKET;
  5117. }
  5118. }
  5119. k = (SOAP_SOCKLEN_T) sizeof(soap->errnum);
  5120. if (!getsockopt(sk, SOL_SOCKET, SO_ERROR, (char *) &soap->errnum, &k) &&
  5121. !soap->errnum) /* portability note: see SOAP_SOCKLEN_T definition in stdsoap2.h */
  5122. break;
  5123. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Could not connect to host\n"));
  5124. if (!soap->errnum)
  5125. soap->errnum = soap_socket_errno(sk);
  5126. soap_set_receiver_error(soap, tcp_error(soap), "connect failed in tcp_connect()", SOAP_TCP_ERROR);
  5127. soap->fclosesocket(soap, sk);
  5128. #ifdef WITH_IPV6
  5129. if (res->ai_next)
  5130. {
  5131. res = res->ai_next;
  5132. goto again;
  5133. }
  5134. freeaddrinfo(ressave);
  5135. #endif
  5136. return soap->socket = SOAP_INVALID_SOCKET;
  5137. }
  5138. #endif
  5139. #ifdef WITH_IPV6
  5140. if (res->ai_next)
  5141. {
  5142. res = res->ai_next;
  5143. soap->fclosesocket(soap, sk);
  5144. goto again;
  5145. }
  5146. #endif
  5147. if (err && err != SOAP_EINTR) {
  5148. soap->errnum = err;
  5149. #ifdef WITH_IPV6
  5150. freeaddrinfo(ressave);
  5151. #endif
  5152. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Could not connect to host\n"));
  5153. soap_set_receiver_error(soap, tcp_error(soap), "connect failed in tcp_connect()", SOAP_TCP_ERROR);
  5154. soap->fclosesocket(soap, sk);
  5155. return soap->socket = SOAP_INVALID_SOCKET;
  5156. }
  5157. } else {
  5158. break;
  5159. }
  5160. }
  5161. #ifdef WITH_IPV6
  5162. soap->peerlen = 0; /* IPv6: already connected so use send() */
  5163. freeaddrinfo(ressave);
  5164. #endif
  5165. soap->imode &= ~SOAP_ENC_SSL;
  5166. soap->omode &= ~SOAP_ENC_SSL;
  5167. if (endpoint && !soap_tag_cmp(endpoint, "https:*")) {
  5168. #if defined(WITH_OPENSSL) || defined(WITH_GNUTLS) || defined(WITH_SYSTEMSSL)
  5169. #ifdef WITH_OPENSSL
  5170. BIO *bio;
  5171. #endif
  5172. #ifdef WITH_SYSTEMSSL
  5173. gsk_iocallback local_io = { ssl_recv, ssl_send, NULL, NULL, NULL, NULL };
  5174. #endif
  5175. int r;
  5176. if (soap->proxy_host)
  5177. {
  5178. soap_mode m = soap->mode; /* preserve settings */
  5179. soap_mode om = soap->omode; /* make sure we only parse HTTP */
  5180. ULONG64 n = soap->count; /* save the content length */
  5181. const char *userid, *passwd;
  5182. int status = soap->status; /* save the current status/command */
  5183. int keep_alive = soap->keep_alive; /* save the KA status */
  5184. soap->omode &= ~SOAP_ENC; /* mask IO and ENC */
  5185. soap->omode |= SOAP_IO_BUFFER;
  5186. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Connecting to %s proxy server %s for destination endpoint %s\n", soap->proxy_http_version, soap->proxy_host, endpoint));
  5187. #ifdef WITH_NTLM
  5188. if (soap->ntlm_challenge && soap_ntlm_handshake(soap, SOAP_CONNECT, endpoint, host, port))
  5189. {
  5190. soap->fclosesocket(soap, sk);
  5191. return soap->socket = SOAP_INVALID_SOCKET;
  5192. }
  5193. #endif
  5194. if (soap_init_send(soap))
  5195. {
  5196. soap->fclosesocket(soap, sk);
  5197. return soap->socket = SOAP_INVALID_SOCKET;
  5198. }
  5199. soap->status = SOAP_CONNECT;
  5200. if (!soap->keep_alive)
  5201. soap->keep_alive = -1; /* must keep alive */
  5202. soap->error = soap->fpost(soap, endpoint, host, port, NULL, NULL, 0);
  5203. if (soap->error
  5204. || soap_end_send_flush(soap))
  5205. {
  5206. soap->fclosesocket(soap, sk);
  5207. return soap->socket = SOAP_INVALID_SOCKET;
  5208. }
  5209. soap->keep_alive = keep_alive;
  5210. soap->omode = om;
  5211. om = soap->imode;
  5212. soap->imode &= ~SOAP_ENC; /* mask IO and ENC */
  5213. userid = soap->userid; /* preserve */
  5214. passwd = soap->passwd; /* preserve */
  5215. soap->error = soap->fparse(soap);
  5216. if (soap->error)
  5217. {
  5218. soap->fclosesocket(soap, sk);
  5219. return soap->socket = SOAP_INVALID_SOCKET;
  5220. }
  5221. soap->status = status; /* restore */
  5222. soap->userid = userid; /* restore */
  5223. soap->passwd = passwd; /* restore */
  5224. soap->imode = om; /* restore */
  5225. soap->count = n; /* restore */
  5226. if (soap_init_send(soap))
  5227. {
  5228. soap->fclosesocket(soap, sk);
  5229. return soap->socket = SOAP_INVALID_SOCKET;
  5230. }
  5231. if (endpoint)
  5232. soap_strcpy(soap->endpoint, sizeof(soap->endpoint), endpoint); /* restore */
  5233. soap->mode = m;
  5234. }
  5235. #ifdef WITH_OPENSSL
  5236. ERR_clear_error();
  5237. soap->ssl_flags |= SOAP_SSL_CLIENT;
  5238. if (!soap->ctx && (soap->error = soap->fsslauth(soap)) != SOAP_OK)
  5239. {
  5240. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "SSL required, but no ctx set\n"));
  5241. soap->fclosesocket(soap, sk);
  5242. soap->error = SOAP_SSL_ERROR;
  5243. return soap->socket = SOAP_INVALID_SOCKET;
  5244. }
  5245. if (!soap->ssl)
  5246. {
  5247. soap->ssl = SSL_new(soap->ctx);
  5248. if (!soap->ssl)
  5249. {
  5250. soap->fclosesocket(soap, sk);
  5251. soap->error = SOAP_SSL_ERROR;
  5252. return soap->socket = SOAP_INVALID_SOCKET;
  5253. }
  5254. }
  5255. else
  5256. {
  5257. SSL_clear(soap->ssl);
  5258. }
  5259. if (soap->session)
  5260. {
  5261. if (!strcmp(soap->session_host, host) && soap->session_port == port)
  5262. SSL_set_session(soap->ssl, soap->session);
  5263. SSL_SESSION_free(soap->session);
  5264. soap->session = NULL;
  5265. }
  5266. #if OPENSSL_VERSION_NUMBER >= 0x1000000aL
  5267. if (!(soap->ssl_flags & SOAP_SSLv3) && !SSL_set_tlsext_host_name(soap->ssl, host))
  5268. {
  5269. soap_set_receiver_error(soap, "SSL/TLS error", "SNI failed", SOAP_SSL_ERROR);
  5270. soap->fclosesocket(soap, sk);
  5271. return soap->socket = SOAP_INVALID_SOCKET;
  5272. }
  5273. #elif (OPENSSL_VERSION_NUMBER >= 0x0090800fL) && defined(SSL_CTRL_SET_TLSEXT_HOSTNAME)
  5274. if (!SSL_ctrl(soap->ssl, SSL_CTRL_SET_TLSEXT_HOSTNAME, TLSEXT_NAMETYPE_host_name, (void*)host))
  5275. {
  5276. soap_set_receiver_error(soap, "SSL/TLS error", "SNI failed", SOAP_SSL_ERROR);
  5277. soap->fclosesocket(soap, sk);
  5278. return soap->socket = SOAP_INVALID_SOCKET;
  5279. }
  5280. #endif
  5281. bio = BIO_new_socket((int)sk, BIO_NOCLOSE);
  5282. SSL_set_bio(soap->ssl, bio, bio);
  5283. if (soap->connect_timeout || soap->recv_timeout || soap->send_timeout)
  5284. {
  5285. /* Set SSL connect timeout and set SSL sockets to non-blocking */
  5286. int t = soap->recv_timeout > soap->send_timeout ? soap->recv_timeout : soap->send_timeout;
  5287. if (soap->connect_timeout > 0 && t < soap->connect_timeout)
  5288. t = soap->connect_timeout;
  5289. if (t > 0)
  5290. retries = 10 * t;
  5291. else if (t > -100000)
  5292. retries = 1;
  5293. else
  5294. retries = t/-100000;
  5295. SOAP_SOCKNONBLOCK(sk)
  5296. }
  5297. else
  5298. {
  5299. /* Set sockets to blocking */
  5300. retries = 1;
  5301. SOAP_SOCKBLOCK(sk)
  5302. }
  5303. err = SSL_ERROR_NONE;
  5304. /* Try connecting until success or timeout */
  5305. do
  5306. {
  5307. if ((r = SSL_connect(soap->ssl)) <= 0)
  5308. {
  5309. err = SSL_get_error(soap->ssl, r);
  5310. if (err == SSL_ERROR_WANT_CONNECT || err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE)
  5311. {
  5312. int s;
  5313. if (err == SSL_ERROR_WANT_READ)
  5314. s = tcp_select(soap, sk, SOAP_TCP_SELECT_RCV | SOAP_TCP_SELECT_ERR, -100000);
  5315. else
  5316. s = tcp_select(soap, sk, SOAP_TCP_SELECT_SND | SOAP_TCP_SELECT_ERR, -100000);
  5317. if (s < 0)
  5318. break;
  5319. if (s == 0 && retries-- <= 0)
  5320. {
  5321. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "SSL/TLS connect timeout\n"));
  5322. soap_set_receiver_error(soap, "Timeout", "SSL_connect() failed in tcp_connect()", SOAP_TCP_ERROR);
  5323. soap->fclosesocket(soap, sk);
  5324. return soap->socket = SOAP_INVALID_SOCKET;
  5325. }
  5326. }
  5327. else
  5328. {
  5329. soap->errnum = soap_socket_errno(sk);
  5330. break;
  5331. }
  5332. }
  5333. } while (!SSL_is_init_finished(soap->ssl));
  5334. if (r <= 0)
  5335. {
  5336. soap_set_sender_error(soap, soap_ssl_error(soap, r, err), "SSL/TLS handshake failed", SOAP_SSL_ERROR);
  5337. soap->fclosesocket(soap, sk);
  5338. return soap->socket = SOAP_INVALID_SOCKET;
  5339. }
  5340. /* Check server credentials when required */
  5341. if ((soap->ssl_flags & SOAP_SSL_REQUIRE_SERVER_AUTHENTICATION))
  5342. {
  5343. if ((err = SSL_get_verify_result(soap->ssl)) != X509_V_OK)
  5344. {
  5345. soap_set_sender_error(soap, X509_verify_cert_error_string(err), "SSL/TLS certificate presented by peer cannot be verified in tcp_connect()", SOAP_SSL_ERROR);
  5346. soap->fclosesocket(soap, sk);
  5347. return soap->socket = SOAP_INVALID_SOCKET;
  5348. }
  5349. if (!(soap->ssl_flags & SOAP_SSL_SKIP_HOST_CHECK))
  5350. {
  5351. X509_NAME *subj;
  5352. STACK_OF(CONF_VALUE) *val = NULL;
  5353. #if OPENSSL_VERSION_NUMBER >= 0x0090800fL
  5354. GENERAL_NAMES *names = NULL;
  5355. #else
  5356. int ext_count;
  5357. #endif
  5358. int ok = 0;
  5359. X509 *peer = SSL_get_peer_certificate(soap->ssl);
  5360. if (!peer)
  5361. {
  5362. soap_set_sender_error(soap, "SSL/TLS error", "No SSL/TLS certificate was presented by the peer in tcp_connect()", SOAP_SSL_ERROR);
  5363. soap->fclosesocket(soap, sk);
  5364. return soap->socket = SOAP_INVALID_SOCKET;
  5365. }
  5366. #if OPENSSL_VERSION_NUMBER < 0x0090800fL
  5367. ext_count = X509_get_ext_count(peer);
  5368. if (ext_count > 0)
  5369. {
  5370. int i;
  5371. for (i = 0; i < ext_count; i++)
  5372. {
  5373. X509_EXTENSION *ext = X509_get_ext(peer, i);
  5374. const char *ext_str = OBJ_nid2sn(OBJ_obj2nid(X509_EXTENSION_get_object(ext)));
  5375. if (ext_str && !strcmp(ext_str, "subjectAltName"))
  5376. {
  5377. X509V3_EXT_METHOD *meth = (X509V3_EXT_METHOD*)X509V3_EXT_get(ext);
  5378. unsigned char *data;
  5379. if (!meth)
  5380. break;
  5381. data = ext->value->data;
  5382. if (data)
  5383. {
  5384. #if OPENSSL_VERSION_NUMBER > 0x00907000L
  5385. void *ext_data;
  5386. if (meth->it)
  5387. ext_data = ASN1_item_d2i(NULL, &data, ext->value->length, ASN1_ITEM_ptr(meth->it));
  5388. else
  5389. {
  5390. #if OPENSSL_VERSION_NUMBER > 0x0090800fL
  5391. ext_data = meth->d2i(NULL, (const unsigned char **)&data, ext->value->length);
  5392. #else
  5393. ext_data = meth->d2i(NULL, &data, ext->value->length);
  5394. #endif
  5395. }
  5396. if (ext_data)
  5397. val = meth->i2v(meth, ext_data, NULL);
  5398. else
  5399. val = NULL;
  5400. if (meth->it)
  5401. ASN1_item_free((ASN1_VALUE*)ext_data, ASN1_ITEM_ptr(meth->it));
  5402. else
  5403. meth->ext_free(ext_data);
  5404. #else
  5405. void *ext_data = meth->d2i(NULL, &data, ext->value->length);
  5406. if (ext_data)
  5407. val = meth->i2v(meth, ext_data, NULL);
  5408. meth->ext_free(ext_data);
  5409. #endif
  5410. if (val)
  5411. {
  5412. int j;
  5413. for (j = 0; j < sk_CONF_VALUE_num(val); j++)
  5414. {
  5415. CONF_VALUE *nval = sk_CONF_VALUE_value(val, j);
  5416. if (nval && (!strcmp(nval->name, "DNS") || !strcmp(nval->name, "IP Address")) && !soap_tag_cmp(host, nval->value))
  5417. {
  5418. ok = 1;
  5419. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "SSL: host name %s match with certificate %s %s\n", host, nval->name, nval->value));
  5420. break;
  5421. }
  5422. else
  5423. {
  5424. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "SSL: host name %s mismatch with certificate %s %s\n", host, nval->name, nval->value));
  5425. }
  5426. }
  5427. sk_CONF_VALUE_pop_free(val, X509V3_conf_free);
  5428. }
  5429. }
  5430. }
  5431. if (ok)
  5432. break;
  5433. }
  5434. }
  5435. #else
  5436. names = (GENERAL_NAMES*)X509_get_ext_d2i(peer, NID_subject_alt_name, NULL, NULL);
  5437. if (names)
  5438. {
  5439. val = i2v_GENERAL_NAMES(NULL, names, val);
  5440. sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free);
  5441. }
  5442. if (val)
  5443. {
  5444. int j;
  5445. for (j = 0; j < sk_CONF_VALUE_num(val); j++)
  5446. {
  5447. CONF_VALUE *nval = sk_CONF_VALUE_value(val, j);
  5448. if (nval && (!strcmp(nval->name, "DNS") || !strcmp(nval->name, "IP Address")) && !soap_tag_cmp(host, nval->value))
  5449. {
  5450. ok = 1;
  5451. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "SSL: host name %s match with certificate %s %s\n", host, nval->name, nval->value));
  5452. break;
  5453. }
  5454. else
  5455. {
  5456. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "SSL: host name %s mismatch with certificate %s %s\n", host, nval->name, nval->value));
  5457. }
  5458. }
  5459. sk_CONF_VALUE_pop_free(val, X509V3_conf_free);
  5460. }
  5461. #endif
  5462. if (!ok && (subj = X509_get_subject_name(peer)) != 0)
  5463. {
  5464. int i = -1;
  5465. do
  5466. {
  5467. ASN1_STRING *name;
  5468. i = X509_NAME_get_index_by_NID(subj, NID_commonName, i);
  5469. if (i == -1)
  5470. break;
  5471. name = X509_NAME_ENTRY_get_data(X509_NAME_get_entry(subj, i));
  5472. if (name)
  5473. {
  5474. #if OPENSSL_VERSION_NUMBER < 0x10100000L
  5475. const char *tmp = (const char*)ASN1_STRING_data(name);
  5476. #else
  5477. const char *tmp = (const char*)ASN1_STRING_get0_data(name);
  5478. #endif
  5479. if (!soap_tag_cmp(host, tmp))
  5480. {
  5481. ok = 1;
  5482. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "SSL: host name %s match with certificate subject %s\n", host, tmp));
  5483. }
  5484. else
  5485. {
  5486. unsigned char *tmp = NULL;
  5487. ASN1_STRING_to_UTF8(&tmp, name);
  5488. if (tmp)
  5489. {
  5490. if (!soap_tag_cmp(host, (const char*)tmp))
  5491. {
  5492. ok = 1;
  5493. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "SSL: host name %s match with certificate subject %s\n", host, tmp));
  5494. }
  5495. else if (tmp[0] == '*') /* wildcard domain */
  5496. {
  5497. const char *t = strchr(host, '.');
  5498. if (t && !soap_tag_cmp(t, (const char*)tmp + 1))
  5499. {
  5500. ok = 1;
  5501. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "SSL: host name %s match with certificate subject %s\n", host, tmp));
  5502. }
  5503. }
  5504. else
  5505. {
  5506. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "SSL: host name %s mismatch with certificate %s\n", host, tmp));
  5507. }
  5508. OPENSSL_free(tmp);
  5509. }
  5510. }
  5511. }
  5512. } while (!ok);
  5513. }
  5514. X509_free(peer);
  5515. if (!ok)
  5516. {
  5517. soap_set_sender_error(soap, "SSL/TLS error", "SSL/TLS certificate host name mismatch in tcp_connect()", SOAP_SSL_ERROR);
  5518. soap->fclosesocket(soap, sk);
  5519. return soap->socket = SOAP_INVALID_SOCKET;
  5520. }
  5521. }
  5522. }
  5523. #endif
  5524. #ifdef WITH_GNUTLS
  5525. soap->ssl_flags |= SOAP_SSL_CLIENT;
  5526. if (!soap->session && (soap->error = soap->fsslauth(soap)) != SOAP_OK)
  5527. {
  5528. soap->fclosesocket(soap, sk);
  5529. return soap->socket = SOAP_INVALID_SOCKET;
  5530. }
  5531. gnutls_transport_set_ptr(soap->session, (gnutls_transport_ptr_t)(long)sk);
  5532. if (soap->connect_timeout || soap->recv_timeout || soap->send_timeout)
  5533. {
  5534. /* Set SSL connect timeout and set SSL sockets to non-blocking */
  5535. int t = soap->recv_timeout > soap->send_timeout ? soap->recv_timeout : soap->send_timeout;
  5536. if (soap->connect_timeout > 0 && t < soap->connect_timeout)
  5537. t = soap->connect_timeout;
  5538. if (t > 0)
  5539. retries = 10 * t;
  5540. else if (t > -100000)
  5541. retries = 1;
  5542. else
  5543. retries = t/-100000;
  5544. SOAP_SOCKNONBLOCK(sk)
  5545. }
  5546. else
  5547. {
  5548. /* Set sockets to blocking */
  5549. retries = 1;
  5550. SOAP_SOCKBLOCK(sk)
  5551. }
  5552. /* Try connecting until success or timeout */
  5553. while ((r = gnutls_handshake(soap->session)))
  5554. {
  5555. /* GNUTLS repeat handhake when GNUTLS_E_AGAIN */
  5556. if (r == GNUTLS_E_AGAIN || r == GNUTLS_E_INTERRUPTED)
  5557. {
  5558. int s;
  5559. if (!gnutls_record_get_direction(soap->session))
  5560. s = tcp_select(soap, sk, SOAP_TCP_SELECT_RCV | SOAP_TCP_SELECT_ERR, -100000);
  5561. else
  5562. s = tcp_select(soap, sk, SOAP_TCP_SELECT_SND | SOAP_TCP_SELECT_ERR, -100000);
  5563. if (s < 0)
  5564. break;
  5565. if (s == 0 && retries-- <= 0)
  5566. {
  5567. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "SSL/TLS connect timeout\n"));
  5568. soap_set_receiver_error(soap, "Timeout", "SSL_connect() failed in tcp_connect()", SOAP_TCP_ERROR);
  5569. soap->fclosesocket(soap, sk);
  5570. return soap->socket = SOAP_INVALID_SOCKET;
  5571. }
  5572. }
  5573. else
  5574. {
  5575. soap->errnum = soap_socket_errno(sk);
  5576. break;
  5577. }
  5578. }
  5579. if (r)
  5580. {
  5581. soap_set_sender_error(soap, soap_ssl_error(soap, r, 0), "SSL/TLS handshake failed", SOAP_SSL_ERROR);
  5582. soap->fclosesocket(soap, sk);
  5583. return soap->socket = SOAP_INVALID_SOCKET;
  5584. }
  5585. if ((soap->ssl_flags & SOAP_SSL_REQUIRE_SERVER_AUTHENTICATION))
  5586. {
  5587. const char *s = ssl_verify(soap, host);
  5588. if (s)
  5589. {
  5590. soap->fclosesocket(soap, sk);
  5591. soap->error = soap_set_sender_error(soap, "SSL/TLS verify error", s, SOAP_SSL_ERROR);
  5592. return soap->socket = SOAP_INVALID_SOCKET;
  5593. }
  5594. }
  5595. #endif
  5596. #ifdef WITH_SYSTEMSSL
  5597. soap->ssl_flags |= SOAP_SSL_CLIENT;
  5598. if (!soap->ctx && (soap->error = soap->fsslauth(soap)) != SOAP_OK)
  5599. {
  5600. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "SSL required, but no ctx set\n"));
  5601. soap->fclosesocket(soap, sk);
  5602. soap->error = SOAP_SSL_ERROR;
  5603. return soap->socket = SOAP_INVALID_SOCKET;
  5604. }
  5605. if (soap->connect_timeout || soap->recv_timeout || soap->send_timeout)
  5606. {
  5607. /* Set SSL connect timeout and set SSL sockets to non-blocking */
  5608. int t = soap->recv_timeout > soap->send_timeout ? soap->recv_timeout : soap->send_timeout;
  5609. if (soap->connect_timeout > 0 && t < soap->connect_timeout)
  5610. t = soap->connect_timeout;
  5611. if (t > 0)
  5612. retries = 10 * t;
  5613. else if (t > -100000)
  5614. retries = 1;
  5615. else
  5616. retries = t/-100000;
  5617. SOAP_SOCKNONBLOCK(sk)
  5618. }
  5619. else
  5620. {
  5621. /* Set sockets to blocking */
  5622. retries = 1;
  5623. SOAP_SOCKBLOCK(sk)
  5624. }
  5625. r = gsk_secure_socket_open(soap->ctx, &soap->ssl);
  5626. if (r == GSK_OK)
  5627. r = gsk_attribute_set_numeric_value(soap->ssl, GSK_FD, sk);
  5628. if (r == GSK_OK)
  5629. r = gsk_attribute_set_buffer(soap->ssl, GSK_KEYRING_LABEL, soap->cafile, 0); /* Certificate label */
  5630. if (r == GSK_OK)
  5631. r = gsk_attribute_set_enum(soap->ssl, GSK_SESSION_TYPE, GSK_CLIENT_SESSION);
  5632. if (r == GSK_OK)
  5633. r = gsk_attribute_set_buffer(soap->ssl, GSK_V3_CIPHER_SPECS_EXPANDED, "0035002F000A", 0);
  5634. if (r == GSK_OK)
  5635. r = gsk_attribute_set_enum(soap->ssl, GSK_V3_CIPHERS, GSK_V3_CIPHERS_CHAR4);
  5636. if (r == GSK_OK)
  5637. r = gsk_attribute_set_callback(soap->ssl, GSK_IO_CALLBACK, &local_io);
  5638. if (r != GSK_OK)
  5639. {
  5640. soap_set_receiver_error(soap, gsk_strerror(r), "SYSTEM SSL error in tcp_connect()", SOAP_SSL_ERROR);
  5641. return soap->socket = SOAP_INVALID_SOCKET;
  5642. }
  5643. /* Try connecting until success or timeout */
  5644. while ((r = gsk_secure_socket_init(soap->ssl)) != GSK_OK)
  5645. {
  5646. if (r == GSK_WOULD_BLOCK_READ || r == GSK_WOULD_BLOCK_WRITE)
  5647. {
  5648. int s;
  5649. if (r == GSK_WOULD_BLOCK_READ)
  5650. s = tcp_select(soap, sk, SOAP_TCP_SELECT_RCV | SOAP_TCP_SELECT_ERR, -100000);
  5651. else
  5652. s = tcp_select(soap, sk, SOAP_TCP_SELECT_SND | SOAP_TCP_SELECT_ERR, -100000);
  5653. if (s < 0)
  5654. break;
  5655. if (s == 0 && retries-- <= 0)
  5656. {
  5657. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "SSL/TLS connect timeout\n"));
  5658. soap_set_receiver_error(soap, "Timeout", "SSL_connect() failed in tcp_connect()", SOAP_TCP_ERROR);
  5659. soap->fclosesocket(soap, sk);
  5660. return soap->socket = SOAP_INVALID_SOCKET;
  5661. }
  5662. }
  5663. else
  5664. {
  5665. soap->errnum = soap_socket_errno(sk);
  5666. break;
  5667. }
  5668. }
  5669. if (r != GSK_OK)
  5670. {
  5671. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "SSL_connect/select error in tcp_connect\n"));
  5672. soap_set_receiver_error(soap, gsk_strerror(r), "SSL/TLS handshake failed", SOAP_SSL_ERROR);
  5673. soap->fclosesocket(soap, sk);
  5674. return soap->socket = SOAP_INVALID_SOCKET;
  5675. }
  5676. #endif
  5677. soap->imode |= SOAP_ENC_SSL;
  5678. soap->omode |= SOAP_ENC_SSL;
  5679. #else
  5680. soap->fclosesocket(soap, sk);
  5681. soap->error = SOAP_SSL_ERROR;
  5682. return soap->socket = SOAP_INVALID_SOCKET;
  5683. #endif
  5684. }
  5685. if (soap->recv_timeout || soap->send_timeout)
  5686. SOAP_SOCKNONBLOCK(sk)
  5687. else
  5688. SOAP_SOCKBLOCK(sk)
  5689. return sk;
  5690. }
  5691. #endif
  5692. /******************************************************************************/
  5693. #ifndef WITH_NOIO
  5694. static int
  5695. tcp_select(struct soap *soap, SOAP_SOCKET sk, int flags, int timeout) {
  5696. int r;
  5697. struct timeval tv;
  5698. fd_set fd[3], *rfd, *sfd, *efd;
  5699. int retries = 0;
  5700. int eintr = SOAP_MAXEINTR;
  5701. soap->errnum = 0;
  5702. if (!soap_valid_socket(sk)) {
  5703. soap->error = SOAP_EOF;
  5704. return -1;
  5705. }
  5706. #ifndef WIN32
  5707. #if !defined(FD_SETSIZE) || defined(__QNX__) || defined(QNX)
  5708. /* no FD_SETSIZE or select() is not MT safe on some QNX: always poll */
  5709. if (1)
  5710. #else
  5711. /* if fd max set size exceeded, use poll() when available */
  5712. if ((int) sk >= (int) FD_SETSIZE)
  5713. #endif
  5714. #ifdef HAVE_POLL
  5715. {
  5716. #ifdef WITH_SELF_PIPE
  5717. struct pollfd pollfd[2];
  5718. pollfd[1].fd = soap->pipe_fd[0];
  5719. pollfd[1].events = POLLIN;
  5720. #else
  5721. struct pollfd pollfd[1];
  5722. #endif
  5723. pollfd[0].fd = (int) sk;
  5724. pollfd[0].events = 0;
  5725. if ((flags & SOAP_TCP_SELECT_RCV))
  5726. pollfd[0].events |= POLLIN;
  5727. if ((flags & SOAP_TCP_SELECT_SND))
  5728. pollfd[0].events |= POLLOUT;
  5729. if ((flags & SOAP_TCP_SELECT_ERR))
  5730. pollfd[0].events |= POLLERR;
  5731. if (timeout <= 0)
  5732. timeout /= -1000; /* -usec -> ms */
  5733. else {
  5734. retries = timeout - 1;
  5735. timeout = 1000;
  5736. }
  5737. do {
  5738. #ifdef WITH_SELF_PIPE
  5739. r = poll(pollfd, 2, timeout);
  5740. #else
  5741. r = poll(pollfd, 1, timeout);
  5742. #endif
  5743. if (r < 0 && (soap->errnum = soap_socket_errno(sk)) == SOAP_EINTR && eintr > 0) {
  5744. eintr--;
  5745. r = 0;
  5746. } else if (retries-- <= 0) {
  5747. break;
  5748. }
  5749. } while (r == 0);
  5750. if (r > 0) {
  5751. r = 0;
  5752. if ((flags & SOAP_TCP_SELECT_RCV) && (pollfd[0].revents & POLLIN))
  5753. r |= SOAP_TCP_SELECT_RCV;
  5754. if ((flags & SOAP_TCP_SELECT_SND) && (pollfd[0].revents & POLLOUT))
  5755. r |= SOAP_TCP_SELECT_SND;
  5756. if ((flags & SOAP_TCP_SELECT_ERR) && (pollfd[0].revents & POLLERR))
  5757. r |= SOAP_TCP_SELECT_ERR;
  5758. #ifdef WITH_SELF_PIPE
  5759. if ((flags & SOAP_TCP_SELECT_PIP) && (pollfd[1].revents & POLLIN))
  5760. {
  5761. char ch;
  5762. for (;;)
  5763. {
  5764. if (read(soap->pipe_fd[0], &ch, 1) == -1)
  5765. {
  5766. if (soap_socket_errno(soap->pipe_fd[0]) == SOAP_EAGAIN)
  5767. break;
  5768. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Self pipe read error\n"));
  5769. return -1;
  5770. }
  5771. }
  5772. r |= SOAP_TCP_SELECT_PIP;
  5773. }
  5774. #endif
  5775. } else if (r == 0) {
  5776. soap->errnum = 0;
  5777. }
  5778. return r;
  5779. }
  5780. #else
  5781. {
  5782. soap->error = SOAP_FD_EXCEEDED;
  5783. return -1;
  5784. }
  5785. #endif
  5786. #endif
  5787. if (timeout > 0)
  5788. retries = timeout - 1;
  5789. do {
  5790. rfd = sfd = efd = NULL;
  5791. #ifdef WITH_SELF_PIPE
  5792. if ((flags & SOAP_TCP_SELECT_PIP) || (flags & SOAP_TCP_SELECT_RCV))
  5793. {
  5794. rfd = &fd[0];
  5795. FD_ZERO(rfd);
  5796. if ((flags & SOAP_TCP_SELECT_PIP))
  5797. FD_SET(soap->pipe_fd[0], rfd);
  5798. if ((flags & SOAP_TCP_SELECT_RCV))
  5799. FD_SET(sk, rfd);
  5800. }
  5801. #else
  5802. if ((flags & SOAP_TCP_SELECT_RCV)) {
  5803. rfd = &fd[0];
  5804. FD_ZERO(rfd);
  5805. FD_SET(sk, rfd);
  5806. }
  5807. #endif
  5808. if ((flags & SOAP_TCP_SELECT_SND)) {
  5809. sfd = &fd[1];
  5810. FD_ZERO(sfd);
  5811. FD_SET(sk, sfd);
  5812. }
  5813. if ((flags & SOAP_TCP_SELECT_ERR)) {
  5814. efd = &fd[2];
  5815. FD_ZERO(efd);
  5816. FD_SET(sk, efd);
  5817. }
  5818. if (timeout <= 0) {
  5819. tv.tv_sec = -timeout / 1000000;
  5820. tv.tv_usec = -timeout % 1000000;
  5821. } else {
  5822. tv.tv_sec = 1;
  5823. tv.tv_usec = 0;
  5824. }
  5825. #ifdef WITH_SELF_PIPE
  5826. r = select((int)(sk > soap->pipe_fd[0] ? sk : soap->pipe_fd[0]) + 1, rfd, sfd, efd, &tv);
  5827. #else
  5828. r = select((int) sk + 1, rfd, sfd, efd, &tv);
  5829. #endif
  5830. if (r < 0 && (soap->errnum = soap_socket_errno(sk)) == SOAP_EINTR && eintr > 0) {
  5831. eintr--;
  5832. r = 0;
  5833. } else if (retries-- <= 0) {
  5834. break;
  5835. }
  5836. } while (r == 0);
  5837. if (r > 0) {
  5838. r = 0;
  5839. if ((flags & SOAP_TCP_SELECT_RCV) && FD_ISSET(sk, rfd))
  5840. r |= SOAP_TCP_SELECT_RCV;
  5841. if ((flags & SOAP_TCP_SELECT_SND) && FD_ISSET(sk, sfd))
  5842. r |= SOAP_TCP_SELECT_SND;
  5843. if ((flags & SOAP_TCP_SELECT_ERR) && FD_ISSET(sk, efd))
  5844. r |= SOAP_TCP_SELECT_ERR;
  5845. #ifdef WITH_SELF_PIPE
  5846. if ((flags & SOAP_TCP_SELECT_PIP) && FD_ISSET(soap->pipe_fd[0], rfd))
  5847. {
  5848. char ch;
  5849. for (;;)
  5850. {
  5851. if (read(soap->pipe_fd[0], &ch, 1) == -1)
  5852. {
  5853. if (soap_socket_errno(soap->pipe_fd[0]) == SOAP_EAGAIN)
  5854. break;
  5855. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Self pipe read error\n"));
  5856. return -1;
  5857. }
  5858. }
  5859. r |= SOAP_TCP_SELECT_PIP;
  5860. }
  5861. #endif
  5862. } else if (r == 0) {
  5863. soap->errnum = 0;
  5864. }
  5865. return r;
  5866. }
  5867. #endif
  5868. /******************************************************************************/
  5869. #ifndef WITH_NOIO
  5870. static SOAP_SOCKET
  5871. tcp_accept(struct soap *soap, SOAP_SOCKET sk, struct sockaddr *addr, int *len) {
  5872. SOAP_SOCKET s;
  5873. (void) soap;
  5874. s = accept(sk, addr, (SOAP_SOCKLEN_T *) len); /* portability note: see SOAP_SOCKLEN_T definition in stdsoap2.h */
  5875. #ifdef WITH_SOCKET_CLOSE_ON_EXIT
  5876. #ifdef WIN32
  5877. #ifndef UNDER_CE
  5878. SetHandleInformation((HANDLE)s, HANDLE_FLAG_INHERIT, 0);
  5879. #endif
  5880. #else
  5881. fcntl(s, F_SETFD, FD_CLOEXEC);
  5882. #endif
  5883. #endif
  5884. return s;
  5885. }
  5886. #endif
  5887. /******************************************************************************/
  5888. #ifndef WITH_NOIO
  5889. static int
  5890. tcp_disconnect(struct soap *soap) {
  5891. #ifdef WITH_OPENSSL
  5892. if (soap->ssl)
  5893. {
  5894. int r;
  5895. if (soap->session)
  5896. {
  5897. SSL_SESSION_free(soap->session);
  5898. soap->session = NULL;
  5899. }
  5900. if (*soap->host)
  5901. {
  5902. soap->session = SSL_get1_session(soap->ssl);
  5903. if (soap->session)
  5904. {
  5905. soap_strcpy(soap->session_host, sizeof(soap->session_host), soap->host);
  5906. soap->session_port = soap->port;
  5907. }
  5908. }
  5909. r = SSL_shutdown(soap->ssl);
  5910. /* SSL shutdown does not work when reads are pending, non-blocking */
  5911. if (r == 0)
  5912. {
  5913. while (SSL_want_read(soap->ssl))
  5914. {
  5915. if (SSL_read(soap->ssl, NULL, 0)
  5916. || soap_socket_errno(soap->socket) != SOAP_EAGAIN)
  5917. {
  5918. r = SSL_shutdown(soap->ssl);
  5919. break;
  5920. }
  5921. }
  5922. }
  5923. if (r == 0)
  5924. {
  5925. if (soap_valid_socket(soap->socket))
  5926. {
  5927. if (!soap->fshutdownsocket(soap, soap->socket, SOAP_SHUT_WR))
  5928. {
  5929. #if !defined(WITH_LEAN) && !defined(WIN32)
  5930. /*
  5931. wait up to 5 seconds for close_notify to be sent by peer (if peer not
  5932. present, this avoids calling SSL_shutdown() which has a lengthy return
  5933. timeout)
  5934. */
  5935. r = tcp_select(soap, soap->socket, SOAP_TCP_SELECT_RCV | SOAP_TCP_SELECT_ERR, 5);
  5936. if (r <= 0)
  5937. {
  5938. soap->errnum = 0;
  5939. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Connection lost...\n"));
  5940. soap->fclosesocket(soap, soap->socket);
  5941. soap->socket = SOAP_INVALID_SOCKET;
  5942. ERR_clear_error();
  5943. SSL_free(soap->ssl);
  5944. soap->ssl = NULL;
  5945. return SOAP_OK;
  5946. }
  5947. #else
  5948. r = SSL_shutdown(soap->ssl);
  5949. if (r <= 0)
  5950. {
  5951. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Shutdown failed: %d\n", SSL_get_error(soap->ssl, r)));
  5952. if (soap_valid_socket(soap->socket) && !(soap->omode & SOAP_IO_UDP))
  5953. {
  5954. soap->fclosesocket(soap, soap->socket);
  5955. soap->socket = SOAP_INVALID_SOCKET;
  5956. }
  5957. }
  5958. #endif
  5959. }
  5960. }
  5961. }
  5962. SSL_free(soap->ssl);
  5963. soap->ssl = NULL;
  5964. ERR_clear_error();
  5965. }
  5966. #endif
  5967. #ifdef WITH_GNUTLS
  5968. if (soap->session)
  5969. {
  5970. gnutls_bye(soap->session, GNUTLS_SHUT_RDWR);
  5971. gnutls_deinit(soap->session);
  5972. soap->session = NULL;
  5973. }
  5974. #endif
  5975. #ifdef WITH_SYSTEMSSL
  5976. if (soap->ssl)
  5977. {
  5978. gsk_secure_socket_shutdown(soap->ssl);
  5979. gsk_secure_socket_close(&soap->ssl);
  5980. }
  5981. #endif
  5982. if (soap_valid_socket(soap->socket) && !(soap->omode & SOAP_IO_UDP)) {
  5983. soap->fshutdownsocket(soap, soap->socket, SOAP_SHUT_RDWR);
  5984. soap->fclosesocket(soap, soap->socket);
  5985. soap->socket = SOAP_INVALID_SOCKET;
  5986. }
  5987. return SOAP_OK;
  5988. }
  5989. #endif
  5990. /******************************************************************************/
  5991. #ifndef WITH_NOIO
  5992. static int
  5993. tcp_closesocket(struct soap *soap, SOAP_SOCKET sk) {
  5994. (void) soap;
  5995. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Close socket=%d\n", (int) sk));
  5996. return soap_closesocket(sk);
  5997. }
  5998. #endif
  5999. /******************************************************************************/
  6000. #ifndef WITH_NOIO
  6001. static int
  6002. tcp_shutdownsocket(struct soap *soap, SOAP_SOCKET sk, int how) {
  6003. (void) soap;
  6004. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Shutdown socket=%d how=%d\n", (int) sk, how));
  6005. return shutdown(sk, how);
  6006. }
  6007. #endif
  6008. /******************************************************************************/
  6009. #ifndef WITH_NOIO
  6010. SOAP_FMAC1
  6011. SOAP_SOCKET
  6012. SOAP_FMAC2
  6013. soap_bind(struct soap *soap, const char *host, int port, int backlog) {
  6014. #if defined(WITH_IPV6)
  6015. struct addrinfo *addrinfo = NULL;
  6016. struct addrinfo hints;
  6017. struct addrinfo res;
  6018. int err;
  6019. int set = 1;
  6020. int unset = 0;
  6021. #elif !defined(WITH_LEAN)
  6022. int set = 1;
  6023. #endif
  6024. if (soap_valid_socket(soap->master)) {
  6025. soap->fclosesocket(soap, soap->master);
  6026. soap->master = SOAP_INVALID_SOCKET;
  6027. }
  6028. soap->socket = SOAP_INVALID_SOCKET;
  6029. soap->errnum = 0;
  6030. soap->errmode = 1;
  6031. if (tcp_init(soap)) {
  6032. soap_set_receiver_error(soap, tcp_error(soap), "TCP init failed in soap_bind()", SOAP_TCP_ERROR);
  6033. return SOAP_INVALID_SOCKET;
  6034. }
  6035. #ifdef WITH_IPV6
  6036. memset((void*)&hints, 0, sizeof(hints));
  6037. hints.ai_family = soap->bind_inet6 ? AF_INET6 : PF_UNSPEC;
  6038. #ifndef WITH_LEAN
  6039. if ((soap->omode & SOAP_IO_UDP))
  6040. hints.ai_socktype = SOCK_DGRAM;
  6041. else
  6042. #endif
  6043. hints.ai_socktype = SOCK_STREAM;
  6044. hints.ai_flags = AI_PASSIVE;
  6045. soap->errmode = 2;
  6046. err = getaddrinfo(host, soap_int2s(soap, port), &hints, &addrinfo);
  6047. if (err || !addrinfo)
  6048. {
  6049. soap_set_receiver_error(soap, SOAP_GAI_STRERROR(err), "getaddrinfo failed in soap_bind()", SOAP_TCP_ERROR);
  6050. if (addrinfo)
  6051. freeaddrinfo(addrinfo);
  6052. return SOAP_INVALID_SOCKET;
  6053. }
  6054. res = *addrinfo;
  6055. if (soap_memcpy((void*)&soap->peer.storage, sizeof(soap->peer.storage), (const void*)addrinfo->ai_addr, addrinfo->ai_addrlen))
  6056. {
  6057. freeaddrinfo(addrinfo);
  6058. soap->error = SOAP_EOM;
  6059. return SOAP_INVALID_SOCKET;
  6060. }
  6061. soap->peerlen = addrinfo->ai_addrlen;
  6062. res.ai_addr = &soap->peer.addr;
  6063. res.ai_addrlen = soap->peerlen;
  6064. freeaddrinfo(addrinfo);
  6065. soap->master = (int)socket(res.ai_family, res.ai_socktype, res.ai_protocol);
  6066. #else
  6067. #ifndef WITH_LEAN
  6068. if ((soap->omode & SOAP_IO_UDP))
  6069. soap->master = (int) socket(AF_INET, SOCK_DGRAM, 0);
  6070. else
  6071. #endif
  6072. soap->master = (int) socket(AF_INET, SOCK_STREAM, 0);
  6073. #endif
  6074. soap->errmode = 0;
  6075. if (!soap_valid_socket(soap->master)) {
  6076. soap->errnum = soap_socket_errno(soap->master);
  6077. soap_set_receiver_error(soap, tcp_error(soap), "socket failed in soap_bind()", SOAP_TCP_ERROR);
  6078. return SOAP_INVALID_SOCKET;
  6079. }
  6080. soap->port = port;
  6081. #ifndef WITH_LEAN
  6082. if ((soap->omode & SOAP_IO_UDP))
  6083. soap->socket = soap->master;
  6084. #endif
  6085. #ifdef WITH_SOCKET_CLOSE_ON_EXIT
  6086. #ifdef WIN32
  6087. #ifndef UNDER_CE
  6088. SetHandleInformation((HANDLE)soap->master, HANDLE_FLAG_INHERIT, 0);
  6089. #endif
  6090. #else
  6091. fcntl(soap->master, F_SETFD, 1);
  6092. #endif
  6093. #endif
  6094. #ifndef WITH_LEAN
  6095. if (soap->bind_flags && setsockopt(soap->master, SOL_SOCKET, soap->bind_flags, (char *) &set, sizeof(int))) {
  6096. soap->errnum = soap_socket_errno(soap->master);
  6097. soap_set_receiver_error(soap, tcp_error(soap), "setsockopt failed in soap_bind()", SOAP_TCP_ERROR);
  6098. return SOAP_INVALID_SOCKET;
  6099. }
  6100. #ifndef UNDER_CE
  6101. if (((soap->imode | soap->omode) & SOAP_IO_KEEPALIVE) && (!((soap->imode | soap->omode) & SOAP_IO_UDP)) &&
  6102. setsockopt(soap->master, SOL_SOCKET, SO_KEEPALIVE, (char *) &set, sizeof(int))) {
  6103. soap->errnum = soap_socket_errno(soap->master);
  6104. soap_set_receiver_error(soap, tcp_error(soap), "setsockopt SO_KEEPALIVE failed in soap_bind()", SOAP_TCP_ERROR);
  6105. return SOAP_INVALID_SOCKET;
  6106. }
  6107. if (soap->sndbuf > 0 && setsockopt(soap->master, SOL_SOCKET, SO_SNDBUF, (char *) &soap->sndbuf, sizeof(int))) {
  6108. soap->errnum = soap_socket_errno(soap->master);
  6109. soap_set_receiver_error(soap, tcp_error(soap), "setsockopt SO_SNDBUF failed in soap_bind()", SOAP_TCP_ERROR);
  6110. return SOAP_INVALID_SOCKET;
  6111. }
  6112. if (soap->rcvbuf > 0 && setsockopt(soap->master, SOL_SOCKET, SO_RCVBUF, (char *) &soap->rcvbuf, sizeof(int))) {
  6113. soap->errnum = soap_socket_errno(soap->master);
  6114. soap_set_receiver_error(soap, tcp_error(soap), "setsockopt SO_RCVBUF failed in soap_bind()", SOAP_TCP_ERROR);
  6115. return SOAP_INVALID_SOCKET;
  6116. }
  6117. #ifdef TCP_NODELAY
  6118. if (!(soap->omode & SOAP_IO_UDP) &&
  6119. setsockopt(soap->master, IPPROTO_TCP, TCP_NODELAY, (char *) &set, sizeof(int))) {
  6120. soap->errnum = soap_socket_errno(soap->master);
  6121. soap_set_receiver_error(soap, tcp_error(soap), "setsockopt TCP_NODELAY failed in soap_bind()", SOAP_TCP_ERROR);
  6122. return SOAP_INVALID_SOCKET;
  6123. }
  6124. #endif
  6125. #ifdef TCP_FASTOPEN
  6126. if (!(soap->omode & SOAP_IO_UDP) &&
  6127. setsockopt(soap->master, IPPROTO_TCP, TCP_FASTOPEN, (char *) &set, sizeof(int))) {
  6128. /* silently ignore */
  6129. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "setsockopt TCP_FASTOPEN failed in soap_bind()\n"));
  6130. }
  6131. #endif
  6132. #endif
  6133. #endif
  6134. #ifdef WITH_IPV6
  6135. if (res.ai_family == AF_INET6 && setsockopt(soap->master, IPPROTO_IPV6, IPV6_V6ONLY, soap->bind_v6only ? (char*)&set : (char*)&unset, sizeof(int)))
  6136. {
  6137. soap->errnum = soap_socket_errno(soap->master);
  6138. soap_set_receiver_error(soap, tcp_error(soap), "setsockopt IPV6_V6ONLY failed in soap_bind()", SOAP_TCP_ERROR);
  6139. return SOAP_INVALID_SOCKET;
  6140. }
  6141. soap->errmode = 0;
  6142. if (bind(soap->master, res.ai_addr, (int)res.ai_addrlen))
  6143. {
  6144. soap->errnum = soap_socket_errno(soap->master);
  6145. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Could not bind to host, bind failed\n"));
  6146. soap_closesock(soap);
  6147. soap_set_receiver_error(soap, tcp_error(soap), "bind failed in soap_bind()", SOAP_TCP_ERROR);
  6148. return SOAP_INVALID_SOCKET;
  6149. }
  6150. #else
  6151. soap->peerlen = sizeof(soap->peer.in);
  6152. memset((void *) &soap->peer.in, 0, sizeof(soap->peer.in));
  6153. soap->peer.in.sin_family = AF_INET;
  6154. soap->errmode = 2;
  6155. if (host) {
  6156. if (soap->fresolve(soap, host, &soap->peer.in.sin_addr)) {
  6157. soap_set_receiver_error(soap, tcp_error(soap), "get host by name failed in soap_bind()", SOAP_TCP_ERROR);
  6158. return SOAP_INVALID_SOCKET;
  6159. }
  6160. } else
  6161. soap->peer.in.sin_addr.s_addr = htonl(INADDR_ANY);
  6162. soap->peer.in.sin_port = htons((short) port);
  6163. soap->errmode = 0;
  6164. if (bind(soap->master, &soap->peer.addr, (int) soap->peerlen)) {
  6165. soap->errnum = soap_socket_errno(soap->master);
  6166. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Could not bind to host, bind failed\n"));
  6167. soap_closesock(soap);
  6168. soap_set_receiver_error(soap, tcp_error(soap), "bind failed in soap_bind()", SOAP_TCP_ERROR);
  6169. return SOAP_INVALID_SOCKET;
  6170. }
  6171. #endif
  6172. if (!(soap->omode & SOAP_IO_UDP) && listen(soap->master, backlog)) {
  6173. soap->errnum = soap_socket_errno(soap->master);
  6174. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Could not bind to host, listen failed\n"));
  6175. soap_closesock(soap);
  6176. soap_set_receiver_error(soap, tcp_error(soap), "listen failed in soap_bind()", SOAP_TCP_ERROR);
  6177. return SOAP_INVALID_SOCKET;
  6178. }
  6179. return soap->master;
  6180. }
  6181. #endif
  6182. /******************************************************************************/
  6183. #ifndef WITH_NOIO
  6184. SOAP_FMAC1
  6185. int
  6186. SOAP_FMAC2
  6187. soap_poll(struct soap *soap) {
  6188. #ifndef WITH_LEAN
  6189. int r;
  6190. if (soap_valid_socket(soap->socket)) {
  6191. r = tcp_select(soap, soap->socket, SOAP_TCP_SELECT_ALL, 0);
  6192. if (r > 0 && (r & SOAP_TCP_SELECT_ERR))
  6193. r = -1;
  6194. } else if (soap_valid_socket(soap->master)) {
  6195. r = tcp_select(soap, soap->master, SOAP_TCP_SELECT_SND | SOAP_TCP_SELECT_ERR, 0);
  6196. } else {
  6197. return SOAP_OK; /* OK when no socket! */
  6198. }
  6199. if (r > 0) {
  6200. int t;
  6201. #ifdef WITH_OPENSSL
  6202. if ((soap->imode & SOAP_ENC_SSL))
  6203. {
  6204. if (soap_valid_socket(soap->socket)
  6205. && (r & SOAP_TCP_SELECT_SND)
  6206. && (!(r & SOAP_TCP_SELECT_RCV)
  6207. || SSL_peek(soap->ssl, (char*)&t, 1) > 0))
  6208. return SOAP_OK;
  6209. }
  6210. else
  6211. #endif
  6212. {
  6213. if (soap_valid_socket(soap->socket)
  6214. && (r & SOAP_TCP_SELECT_SND)
  6215. && (!(r & SOAP_TCP_SELECT_RCV)
  6216. || recv(soap->socket, (char *) &t, 1, MSG_PEEK) > 0))
  6217. return SOAP_OK;
  6218. }
  6219. } else if (r < 0) {
  6220. if ((soap_valid_socket(soap->master) && soap_socket_errno(soap->master) != SOAP_EINTR)
  6221. || (soap_valid_socket(soap->socket) && soap_socket_errno(soap->socket) != SOAP_EINTR))
  6222. return soap_set_receiver_error(soap, tcp_error(soap), "select failed in soap_poll()", SOAP_TCP_ERROR);
  6223. }
  6224. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "soap_poll: other end down on socket=%d select=%d\n", (int) soap->socket, r));
  6225. return SOAP_EOF;
  6226. #else
  6227. (void)soap;
  6228. return SOAP_OK;
  6229. #endif
  6230. }
  6231. #endif
  6232. /******************************************************************************/
  6233. #ifndef WITH_NOIO
  6234. SOAP_FMAC1
  6235. int
  6236. SOAP_FMAC2
  6237. soap_ready(struct soap *soap) {
  6238. #ifndef WITH_LEAN
  6239. int r;
  6240. if (!soap_valid_socket(soap->socket))
  6241. return SOAP_OK; /* OK when no socket! */
  6242. r = tcp_select(soap, soap->socket, SOAP_TCP_SELECT_RCV | SOAP_TCP_SELECT_ERR, 0);
  6243. if (r > 0 && (r & SOAP_TCP_SELECT_ERR))
  6244. r = -1;
  6245. if (r < 0 && soap_socket_errno(soap->socket) != SOAP_EINTR)
  6246. return soap_set_receiver_error(soap, tcp_error(soap), "select failed in soap_ready()", SOAP_TCP_ERROR);
  6247. if (r > 0) {
  6248. char t;
  6249. #ifdef WITH_OPENSSL
  6250. if ((soap->imode & SOAP_ENC_SSL))
  6251. {
  6252. if (SSL_peek(soap->ssl, &t, 1) > 0)
  6253. return SOAP_OK;
  6254. }
  6255. else
  6256. #endif
  6257. {
  6258. if (recv(soap->socket, &t, 1, MSG_PEEK) > 0)
  6259. return SOAP_OK;
  6260. }
  6261. }
  6262. DBGLOG(TEST,
  6263. SOAP_MESSAGE(fdebug, "soap_ready: other end not ready to send on socket=%d select=%d\n", (int) soap->socket,
  6264. r));
  6265. return SOAP_EOF;
  6266. #else
  6267. (void)soap;
  6268. return SOAP_OK;
  6269. #endif
  6270. }
  6271. #endif
  6272. /******************************************************************************/
  6273. #ifndef WITH_NOIO
  6274. SOAP_FMAC1
  6275. SOAP_SOCKET
  6276. SOAP_FMAC2
  6277. soap_accept(struct soap *soap) {
  6278. int n = (int) sizeof(soap->peer);
  6279. int err;
  6280. #ifndef WITH_LEAN
  6281. int set = 1;
  6282. #endif
  6283. soap->error = SOAP_OK;
  6284. memset((void *) &soap->peer, 0, sizeof(soap->peer));
  6285. soap->socket = SOAP_INVALID_SOCKET;
  6286. soap->errmode = 0;
  6287. soap->errnum = 0;
  6288. soap->keep_alive = 0;
  6289. if (!soap_valid_socket(soap->master)) {
  6290. soap_set_receiver_error(soap, tcp_error(soap), "no master socket in soap_accept()", SOAP_TCP_ERROR);
  6291. return SOAP_INVALID_SOCKET;
  6292. }
  6293. #ifndef WITH_LEAN
  6294. if ((soap->omode & SOAP_IO_UDP))
  6295. return soap->socket = soap->master;
  6296. #endif
  6297. for (;;) {
  6298. if (soap->accept_timeout) {
  6299. for (;;) {
  6300. int r;
  6301. r = tcp_select(soap, soap->master, SOAP_TCP_SELECT_ALL, soap->accept_timeout);
  6302. if (r > 0)
  6303. break;
  6304. if (!r) {
  6305. soap_set_receiver_error(soap, "Timeout", "accept failed in soap_accept()", SOAP_TCP_ERROR);
  6306. return SOAP_INVALID_SOCKET;
  6307. }
  6308. if (r < 0) {
  6309. r = soap->errnum;
  6310. if (r != SOAP_EINTR) {
  6311. soap_closesock(soap);
  6312. soap_set_receiver_error(soap, tcp_error(soap), "accept failed in soap_accept()",
  6313. SOAP_TCP_ERROR);
  6314. return SOAP_INVALID_SOCKET;
  6315. }
  6316. }
  6317. }
  6318. }
  6319. n = (int) sizeof(soap->peer);
  6320. soap->socket = soap->faccept(soap, soap->master, &soap->peer.addr, &n);
  6321. soap->peerlen = (size_t) n;
  6322. if (soap_valid_socket(soap->socket)) {
  6323. #ifdef WITH_IPV6
  6324. char port[16];
  6325. struct addrinfo *res = NULL;
  6326. struct addrinfo hints;
  6327. memset(&hints, 0, sizeof(struct addrinfo));
  6328. hints.ai_family = PF_UNSPEC;
  6329. hints.ai_socktype = SOCK_STREAM;
  6330. hints.ai_flags = AI_NUMERICHOST | AI_NUMERICSERV;
  6331. getnameinfo(&soap->peer.addr, n, soap->host, sizeof(soap->host), port, sizeof(port), NI_NUMERICHOST | NI_NUMERICSERV);
  6332. soap->ip = 0;
  6333. soap->ip6[0] = 0;
  6334. soap->ip6[1] = 0;
  6335. soap->ip6[2] = 0;
  6336. soap->ip6[3] = 0;
  6337. if (getaddrinfo(soap->host, NULL, &hints, &res) == 0 && res)
  6338. {
  6339. struct sockaddr_storage result;
  6340. soap_memcpy(&result, sizeof(result), res->ai_addr, res->ai_addrlen);
  6341. freeaddrinfo(res);
  6342. if (result.ss_family == AF_INET6)
  6343. {
  6344. struct sockaddr_in6 *addr = (struct sockaddr_in6*)&result;
  6345. struct in6_addr *inaddr = &addr->sin6_addr;
  6346. int i;
  6347. for (i = 0; i < 16; i++)
  6348. soap->ip6[i/4] = (soap->ip6[i/4] << 8) + inaddr->s6_addr[i];
  6349. }
  6350. else if (result.ss_family == AF_INET)
  6351. {
  6352. struct sockaddr_in *addr = (struct sockaddr_in*)&result;
  6353. soap->ip = ntohl(addr->sin_addr.s_addr);
  6354. soap->ip6[2] = 0xFFFF;
  6355. soap->ip6[3] = soap->ip;
  6356. }
  6357. }
  6358. soap->port = soap_strtol(port, NULL, 10);
  6359. #else
  6360. soap->ip = ntohl(soap->peer.in.sin_addr.s_addr);
  6361. soap->ip6[0] = 0;
  6362. soap->ip6[1] = 0;
  6363. soap->ip6[2] = 0xFFFF;
  6364. soap->ip6[3] = soap->ip;
  6365. (SOAP_SNPRINTF(soap->host, sizeof(soap->host), 80), "%u.%u.%u.%u", (int) (soap->ip >> 24) & 0xFF,
  6366. (int) (soap->ip >> 16) & 0xFF,
  6367. (int) (soap->ip >> 8) & 0xFF, (int) soap->ip & 0xFF);
  6368. soap->port = (int) ntohs(soap->peer.in.sin_port); /* does not return port number on some systems */
  6369. #endif
  6370. DBGLOG(TEST,
  6371. SOAP_MESSAGE(fdebug, "Accept socket=%d at port=%d from IP='%s'\n", (int) soap->socket, soap->port,
  6372. soap->host));
  6373. #ifndef WITH_LEAN
  6374. if ((soap->accept_flags & SO_LINGER)) {
  6375. struct linger linger;
  6376. memset((void *) &linger, 0, sizeof(linger));
  6377. linger.l_onoff = 1;
  6378. linger.l_linger = soap->linger_time;
  6379. if (setsockopt(soap->socket, SOL_SOCKET, SO_LINGER, (char *) &linger, sizeof(struct linger))) {
  6380. soap->errnum = soap_socket_errno(soap->socket);
  6381. soap_set_receiver_error(soap, tcp_error(soap), "setsockopt SO_LINGER failed in soap_accept()",
  6382. SOAP_TCP_ERROR);
  6383. soap_closesock(soap);
  6384. return SOAP_INVALID_SOCKET;
  6385. }
  6386. }
  6387. if ((soap->accept_flags & ~SO_LINGER) &&
  6388. setsockopt(soap->socket, SOL_SOCKET, soap->accept_flags & ~SO_LINGER, (char *) &set, sizeof(int))) {
  6389. soap->errnum = soap_socket_errno(soap->socket);
  6390. soap_set_receiver_error(soap, tcp_error(soap), "setsockopt failed in soap_accept()", SOAP_TCP_ERROR);
  6391. soap_closesock(soap);
  6392. return SOAP_INVALID_SOCKET;
  6393. }
  6394. #ifndef UNDER_CE
  6395. if (((soap->imode | soap->omode) & SOAP_IO_KEEPALIVE) &&
  6396. setsockopt(soap->socket, SOL_SOCKET, SO_KEEPALIVE, (char *) &set, sizeof(int))) {
  6397. soap->errnum = soap_socket_errno(soap->socket);
  6398. soap_set_receiver_error(soap, tcp_error(soap), "setsockopt SO_KEEPALIVE failed in soap_accept()",
  6399. SOAP_TCP_ERROR);
  6400. soap_closesock(soap);
  6401. return SOAP_INVALID_SOCKET;
  6402. }
  6403. if (soap->sndbuf > 0 &&
  6404. setsockopt(soap->socket, SOL_SOCKET, SO_SNDBUF, (char *) &soap->sndbuf, sizeof(int))) {
  6405. soap->errnum = soap_socket_errno(soap->socket);
  6406. soap_set_receiver_error(soap, tcp_error(soap), "setsockopt SO_SNDBUF failed in soap_accept()",
  6407. SOAP_TCP_ERROR);
  6408. soap_closesock(soap);
  6409. return SOAP_INVALID_SOCKET;
  6410. }
  6411. if (soap->rcvbuf > 0 &&
  6412. setsockopt(soap->socket, SOL_SOCKET, SO_RCVBUF, (char *) &soap->rcvbuf, sizeof(int))) {
  6413. soap->errnum = soap_socket_errno(soap->socket);
  6414. soap_set_receiver_error(soap, tcp_error(soap), "setsockopt SO_RCVBUF failed in soap_accept()",
  6415. SOAP_TCP_ERROR);
  6416. soap_closesock(soap);
  6417. return SOAP_INVALID_SOCKET;
  6418. }
  6419. #ifdef TCP_NODELAY
  6420. if (setsockopt(soap->socket, IPPROTO_TCP, TCP_NODELAY, (char *) &set, sizeof(int))) {
  6421. soap->errnum = soap_socket_errno(soap->socket);
  6422. soap_set_receiver_error(soap, tcp_error(soap), "setsockopt TCP_NODELAY failed in soap_accept()",
  6423. SOAP_TCP_ERROR);
  6424. soap_closesock(soap);
  6425. return SOAP_INVALID_SOCKET;
  6426. }
  6427. #endif
  6428. #endif
  6429. #endif
  6430. soap->keep_alive = -(((soap->imode | soap->omode) & SOAP_IO_KEEPALIVE) != 0);
  6431. if (soap->send_timeout || soap->recv_timeout)
  6432. SOAP_SOCKNONBLOCK(soap->socket)
  6433. else
  6434. SOAP_SOCKBLOCK(soap->socket)
  6435. return soap->socket;
  6436. }
  6437. err = soap_socket_errno(soap->socket);
  6438. if (err != 0 && err != SOAP_EINTR && err != SOAP_EAGAIN && err != SOAP_EWOULDBLOCK) {
  6439. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Accept failed from %s\n", soap->host));
  6440. soap->errnum = err;
  6441. soap_set_receiver_error(soap, tcp_error(soap), "accept failed in soap_accept()", SOAP_TCP_ERROR);
  6442. soap_closesock(soap);
  6443. return SOAP_INVALID_SOCKET;
  6444. }
  6445. }
  6446. }
  6447. #endif
  6448. /******************************************************************************/
  6449. SOAP_FMAC1
  6450. int
  6451. SOAP_FMAC2
  6452. soap_closesock(struct soap *soap) {
  6453. int status = soap->error;
  6454. int err = SOAP_OK;
  6455. soap->part = SOAP_END;
  6456. #ifndef WITH_LEANER
  6457. if (status && status < 200) /* attachment state is not to be trusted */
  6458. {
  6459. soap->mime.first = NULL;
  6460. soap->mime.last = NULL;
  6461. soap->dime.first = NULL;
  6462. soap->dime.last = NULL;
  6463. }
  6464. #endif
  6465. if (soap->fdisconnect)
  6466. err = soap->fdisconnect(soap);
  6467. if (err || status == SOAP_EOF || status == SOAP_TCP_ERROR || status == SOAP_SSL_ERROR || !soap->keep_alive) {
  6468. soap->keep_alive = 0;
  6469. if (soap->fclose && (soap->error = soap->fclose(soap)) != SOAP_OK)
  6470. return soap->error;
  6471. if (err)
  6472. return soap->error = err;
  6473. }
  6474. #ifdef WITH_ZLIB
  6475. if (!(soap->mode & SOAP_MIME_POSTCHECK))
  6476. {
  6477. if (soap->zlib_state == SOAP_ZLIB_DEFLATE)
  6478. deflateEnd(soap->d_stream);
  6479. else if (soap->zlib_state == SOAP_ZLIB_INFLATE)
  6480. inflateEnd(soap->d_stream);
  6481. soap->zlib_state = SOAP_ZLIB_NONE;
  6482. }
  6483. #endif
  6484. return soap->error = status;
  6485. }
  6486. /******************************************************************************/
  6487. SOAP_FMAC1
  6488. int
  6489. SOAP_FMAC2
  6490. soap_force_closesock(struct soap *soap) {
  6491. soap->keep_alive = 0;
  6492. if (soap_valid_socket(soap->socket) && soap->fclosesocket) {
  6493. soap->fclosesocket(soap, soap->socket);
  6494. soap->socket = SOAP_INVALID_SOCKET;
  6495. }
  6496. return soap->error;
  6497. }
  6498. /******************************************************************************/
  6499. #ifdef WITH_SELF_PIPE
  6500. SOAP_FMAC1
  6501. void
  6502. SOAP_FMAC2
  6503. soap_close_connection(struct soap *soap)
  6504. {
  6505. if (soap_valid_socket(soap->socket))
  6506. write(soap->pipe_fd[1], "1", 1);
  6507. }
  6508. #endif
  6509. /******************************************************************************/
  6510. #ifndef WITH_NOIO
  6511. SOAP_FMAC1
  6512. void
  6513. SOAP_FMAC2
  6514. soap_cleanup(struct soap *soap) {
  6515. soap_done(soap);
  6516. #ifdef WIN32
  6517. if (!tcp_done)
  6518. return;
  6519. tcp_done = 0;
  6520. WSACleanup();
  6521. #endif
  6522. }
  6523. #endif
  6524. /******************************************************************************/
  6525. SOAP_FMAC1
  6526. void
  6527. SOAP_FMAC2
  6528. soap_done(struct soap *soap) {
  6529. #ifdef SOAP_DEBUG
  6530. int i;
  6531. #endif
  6532. if (soap_check_state(soap))
  6533. return;
  6534. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Done with context%s\n", soap->state == SOAP_COPY ? " copy" : ""));
  6535. soap_free_temp(soap);
  6536. while (soap->clist) {
  6537. struct soap_clist *p = soap->clist->next;
  6538. SOAP_FREE(soap, soap->clist);
  6539. soap->clist = p;
  6540. }
  6541. if (soap->state == SOAP_INIT)
  6542. soap->omode &= ~SOAP_IO_UDP; /* to force close the socket */
  6543. soap->keep_alive = 0; /* to force close the socket */
  6544. if (soap->master == soap->socket) /* do not close twice */
  6545. soap->master = SOAP_INVALID_SOCKET;
  6546. soap_closesock(soap);
  6547. #ifdef WITH_COOKIES
  6548. soap_free_cookies(soap);
  6549. #endif
  6550. while (soap->plugins) {
  6551. struct soap_plugin *p = soap->plugins->next;
  6552. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Removing plugin '%s'\n", soap->plugins->id));
  6553. if (soap->plugins->fcopy || soap->state == SOAP_INIT)
  6554. soap->plugins->fdelete(soap, soap->plugins);
  6555. SOAP_FREE(soap, soap->plugins);
  6556. soap->plugins = p;
  6557. }
  6558. soap->fplugin = fplugin;
  6559. #ifndef WITH_NOHTTP
  6560. soap->fpost = http_post;
  6561. soap->fget = http_get;
  6562. soap->fput = http_put;
  6563. soap->fpatch = http_patch;
  6564. soap->fdel = http_del;
  6565. soap->fopt = http_200;
  6566. soap->fhead = http_200;
  6567. soap->fform = NULL;
  6568. soap->fposthdr = http_post_header;
  6569. soap->fresponse = http_response;
  6570. soap->fparse = http_parse;
  6571. soap->fparsehdr = http_parse_header;
  6572. #endif
  6573. soap->fheader = NULL;
  6574. #ifndef WITH_NOIO
  6575. #ifndef WITH_IPV6
  6576. soap->fresolve = tcp_gethost;
  6577. #else
  6578. soap->fresolve = NULL;
  6579. #endif
  6580. soap->faccept = tcp_accept;
  6581. soap->fopen = tcp_connect;
  6582. soap->fclose = tcp_disconnect;
  6583. soap->fclosesocket = tcp_closesocket;
  6584. soap->fshutdownsocket = tcp_shutdownsocket;
  6585. soap->fsend = fsend;
  6586. soap->frecv = frecv;
  6587. soap->fpoll = soap_poll;
  6588. #else
  6589. soap->fopen = NULL;
  6590. soap->fclose = NULL;
  6591. soap->fpoll = NULL;
  6592. #endif
  6593. #ifndef WITH_LEANER
  6594. soap->fsvalidate = NULL;
  6595. soap->fwvalidate = NULL;
  6596. soap->feltbegin = NULL;
  6597. soap->feltendin = NULL;
  6598. soap->feltbegout = NULL;
  6599. soap->feltendout = NULL;
  6600. soap->fprepareinitsend = NULL;
  6601. soap->fprepareinitrecv = NULL;
  6602. soap->fpreparesend = NULL;
  6603. soap->fpreparerecv = NULL;
  6604. soap->fpreparefinalsend = NULL;
  6605. soap->fpreparefinalrecv = NULL;
  6606. soap->ffiltersend = NULL;
  6607. soap->ffilterrecv = NULL;
  6608. #endif
  6609. soap->fseterror = NULL;
  6610. soap->fignore = NULL;
  6611. soap->fserveloop = NULL;
  6612. #ifdef WITH_OPENSSL
  6613. if (soap->session)
  6614. {
  6615. SSL_SESSION_free(soap->session);
  6616. soap->session = NULL;
  6617. }
  6618. #endif
  6619. if (soap->state == SOAP_INIT) {
  6620. if (soap_valid_socket(soap->master)) {
  6621. soap->fclosesocket(soap, soap->master);
  6622. soap->master = SOAP_INVALID_SOCKET;
  6623. }
  6624. }
  6625. #ifdef WITH_OPENSSL
  6626. if (soap->ssl)
  6627. {
  6628. SSL_free(soap->ssl);
  6629. soap->ssl = NULL;
  6630. }
  6631. if (soap->state == SOAP_INIT)
  6632. {
  6633. if (soap->ctx)
  6634. {
  6635. SSL_CTX_free(soap->ctx);
  6636. soap->ctx = NULL;
  6637. }
  6638. }
  6639. ERR_clear_error();
  6640. # if OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(LIBRESSL_VERSION_NUMBER)
  6641. /* OpenSSL libraries handle thread init and deinit */
  6642. # elif OPENSSL_VERSION_NUMBER >= 0x10000000L
  6643. ERR_remove_thread_state(NULL);
  6644. # else
  6645. ERR_remove_state(0);
  6646. # endif
  6647. #endif
  6648. #ifdef WITH_GNUTLS
  6649. if (soap->state == SOAP_INIT)
  6650. {
  6651. if (soap->xcred)
  6652. {
  6653. gnutls_certificate_free_credentials(soap->xcred);
  6654. soap->xcred = NULL;
  6655. }
  6656. if (soap->acred)
  6657. {
  6658. gnutls_anon_free_client_credentials(soap->acred);
  6659. soap->acred = NULL;
  6660. }
  6661. if (soap->cache)
  6662. {
  6663. gnutls_priority_deinit(soap->cache);
  6664. soap->cache = NULL;
  6665. }
  6666. if (soap->dh_params)
  6667. {
  6668. gnutls_dh_params_deinit(soap->dh_params);
  6669. soap->dh_params = NULL;
  6670. }
  6671. # if GNUTLS_VERSION_NUMBER < 0x030300
  6672. if (soap->rsa_params)
  6673. {
  6674. gnutls_rsa_params_deinit(soap->rsa_params);
  6675. soap->rsa_params = NULL;
  6676. }
  6677. #endif
  6678. }
  6679. if (soap->session)
  6680. {
  6681. gnutls_deinit(soap->session);
  6682. soap->session = NULL;
  6683. }
  6684. #endif
  6685. #ifdef WITH_SYSTEMSSL
  6686. if (soap->ssl)
  6687. gsk_secure_socket_close(&soap->ssl);
  6688. if (soap->state == SOAP_INIT)
  6689. if (soap->ctx)
  6690. gsk_environment_close(&soap->ctx);
  6691. #endif
  6692. #ifdef WITH_C_LOCALE
  6693. SOAP_FREELOCALE(soap);
  6694. #endif
  6695. #ifdef WITH_ZLIB
  6696. if (soap->d_stream)
  6697. {
  6698. SOAP_FREE(soap, soap->d_stream);
  6699. soap->d_stream = NULL;
  6700. }
  6701. if (soap->z_buf)
  6702. {
  6703. SOAP_FREE(soap, soap->z_buf);
  6704. soap->z_buf = NULL;
  6705. }
  6706. #endif
  6707. #ifdef SOAP_DEBUG
  6708. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Free logfiles\n"));
  6709. for (i = 0; i < SOAP_MAXLOGS; i++)
  6710. {
  6711. soap_close_logfile(soap, i);
  6712. if (soap->logfile[i])
  6713. {
  6714. SOAP_FREE_UNMANAGED(soap->logfile[i]);
  6715. soap->logfile[i] = NULL;
  6716. }
  6717. }
  6718. #endif
  6719. #ifdef WITH_SELF_PIPE
  6720. close(soap->pipe_fd[0]);
  6721. close(soap->pipe_fd[1]);
  6722. #endif
  6723. #ifdef SOAP_MEM_DEBUG
  6724. soap_free_mht(soap);
  6725. #endif
  6726. soap->state = SOAP_NONE;
  6727. }
  6728. /******************************************************************************\
  6729. *
  6730. * HTTP
  6731. *
  6732. \******************************************************************************/
  6733. #ifndef WITH_NOHTTP
  6734. static int
  6735. http_parse(struct soap *soap) {
  6736. char header[SOAP_HDRLEN], *s;
  6737. int err = SOAP_OK;
  6738. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Waiting for HTTP request/response...\n"));
  6739. soap->fform = NULL;
  6740. *soap->endpoint = '\0';
  6741. soap->bearer = NULL;
  6742. #ifdef WITH_NTLM
  6743. if (!soap->ntlm_challenge)
  6744. #endif
  6745. {
  6746. soap->userid = NULL;
  6747. soap->passwd = NULL;
  6748. soap->authrealm = NULL;
  6749. }
  6750. #ifdef WITH_NTLM
  6751. soap->ntlm_challenge = NULL;
  6752. #endif
  6753. soap->proxy_from = NULL;
  6754. soap->cors_origin = NULL;
  6755. soap->cors_method = NULL;
  6756. soap->cors_header = NULL;
  6757. do {
  6758. soap->length = 0;
  6759. soap->http_content = NULL;
  6760. soap->action = NULL;
  6761. soap->status = 0;
  6762. soap->body = 1;
  6763. if (soap_getline(soap, soap->msgbuf, sizeof(soap->msgbuf))) {
  6764. if (soap->error == SOAP_EOF)
  6765. return SOAP_EOF;
  6766. return soap->error = 414;
  6767. }
  6768. s = strchr(soap->msgbuf, ' ');
  6769. if (s) {
  6770. soap->status = (unsigned short) soap_strtoul(s, &s, 10);
  6771. if (!soap_coblank((soap_wchar) *s))
  6772. soap->status = 0;
  6773. }
  6774. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "HTTP status: %s\n", soap->msgbuf));
  6775. for (;;) {
  6776. if (soap_getline(soap, header, SOAP_HDRLEN)) {
  6777. if (soap->error == SOAP_EOF) {
  6778. soap->error = SOAP_OK;
  6779. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "EOF in HTTP header, try to continue anyway\n"));
  6780. break;
  6781. }
  6782. return soap->error;
  6783. }
  6784. if (!*header)
  6785. break;
  6786. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "HTTP header: %s\n", header));
  6787. s = strchr(header, ':');
  6788. if (s) {
  6789. char *t;
  6790. *s = '\0';
  6791. do {
  6792. s++;
  6793. } while (*s && *s <= 32);
  6794. if (*s == '"')
  6795. s++;
  6796. t = s + strlen(s) - 1;
  6797. while (t > s && *t <= 32)
  6798. t--;
  6799. if (t >= s && *t == '"')
  6800. t--;
  6801. t[1] = '\0';
  6802. soap->error = soap->fparsehdr(soap, header, s);
  6803. if (soap->error) {
  6804. if (soap->error < SOAP_STOP)
  6805. return soap->error;
  6806. err = soap->error;
  6807. soap->error = SOAP_OK;
  6808. }
  6809. }
  6810. }
  6811. } while (soap->status == 100);
  6812. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Finished HTTP header parsing, status = %d\n", soap->status));
  6813. s = strstr(soap->msgbuf, "HTTP/");
  6814. if (s && s[5] == '1' && s[6] == '.' && s[7] == '0') {
  6815. soap->keep_alive = 0; /* HTTP 1.0 does not support keep-alive */
  6816. if (soap->status == 0 && (soap->omode & SOAP_IO) == SOAP_IO_CHUNK) /* soap->status == 0 for HTTP request */
  6817. soap->omode = (soap->omode & ~SOAP_IO) | SOAP_IO_STORE; /* HTTP 1.0 does not support chunked transfers */
  6818. }
  6819. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Keep alive connection = %d\n", soap->keep_alive));
  6820. if (soap->status == 0) {
  6821. size_t l = 0;
  6822. if (s) {
  6823. if (!strncmp(soap->msgbuf, "POST ", l = 5))
  6824. soap->status = SOAP_POST;
  6825. else if (!strncmp(soap->msgbuf, "GET ", l = 4))
  6826. soap->status = SOAP_GET;
  6827. else if (!strncmp(soap->msgbuf, "PUT ", l = 4))
  6828. soap->status = SOAP_PUT;
  6829. else if (!strncmp(soap->msgbuf, "PATCH ", l = 4))
  6830. soap->status = SOAP_PATCH;
  6831. else if (!strncmp(soap->msgbuf, "DELETE ", l = 7))
  6832. soap->status = SOAP_DEL;
  6833. else if (!strncmp(soap->msgbuf, "HEAD ", l = 5))
  6834. soap->status = SOAP_HEAD;
  6835. else if (!strncmp(soap->msgbuf, "OPTIONS ", l = 8))
  6836. soap->status = SOAP_OPTIONS;
  6837. }
  6838. if (s && soap->status) {
  6839. size_t m, n, k;
  6840. int r;
  6841. while (soap->msgbuf[l] && soap_coblank((soap_wchar) soap->msgbuf[l]))
  6842. l++;
  6843. m = strlen(soap->endpoint);
  6844. n = m + (s - soap->msgbuf) - l - 1;
  6845. if (n >= sizeof(soap->endpoint))
  6846. n = sizeof(soap->endpoint) - 1;
  6847. if (m > n)
  6848. m = n;
  6849. k = n - m + 1;
  6850. if (k >= sizeof(soap->path))
  6851. k = sizeof(soap->path) - 1;
  6852. while (k > 0 && soap_coblank((soap_wchar) soap->msgbuf[l + k - 1]))
  6853. k--;
  6854. if (soap_strncpy(soap->path, sizeof(soap->path), soap->msgbuf + l, k))
  6855. return soap->error = 414;
  6856. if (*soap->path && *soap->path != '/')
  6857. r = soap_strncpy(soap->endpoint, sizeof(soap->endpoint), soap->path, k);
  6858. else
  6859. r = soap_strncat(soap->endpoint, sizeof(soap->endpoint), soap->path, k);
  6860. if (r)
  6861. return soap->error = 414;
  6862. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Target endpoint='%s' path='%s'\n", soap->endpoint, soap->path));
  6863. if (err)
  6864. return soap->error = err;
  6865. } else if (err) {
  6866. return soap->error = err;
  6867. } else if (s) {
  6868. return soap->error = 405;
  6869. }
  6870. return SOAP_OK;
  6871. }
  6872. if ((soap->status >= 200 && soap->status <= 299) /* OK, Accepted, etc */
  6873. || soap->status == 400 /* Bad Request */
  6874. || soap->status == 500) /* Internal Server Error */
  6875. return soap->error = SOAP_OK;
  6876. return soap->error = soap->status;
  6877. }
  6878. #endif
  6879. /******************************************************************************/
  6880. #ifndef WITH_NOHTTP
  6881. static int
  6882. http_parse_header(struct soap *soap, const char *key, const char *val) {
  6883. if (!soap_tag_cmp(key, "Host")) {
  6884. #if defined(WITH_OPENSSL) || defined(WITH_GNUTLS)
  6885. if ((soap->imode & SOAP_ENC_SSL))
  6886. soap_strcpy(soap->endpoint, sizeof(soap->endpoint), "https://");
  6887. else
  6888. #endif
  6889. soap_strcpy(soap->endpoint, sizeof(soap->endpoint), "http://");
  6890. if (soap_strncat(soap->endpoint, sizeof(soap->endpoint), val, sizeof(soap->endpoint) - 9))
  6891. return soap->error = SOAP_HDR;
  6892. }
  6893. #ifndef WITH_LEANER
  6894. else if (!soap_tag_cmp(key, "Content-Type")) {
  6895. const char *action;
  6896. soap->http_content = soap_strdup(soap, val);
  6897. if (soap_http_header_attribute(soap, val, "application/dime"))
  6898. soap->imode |= SOAP_ENC_DIME;
  6899. else if (soap_http_header_attribute(soap, val, "multipart/related")
  6900. || soap_http_header_attribute(soap, val, "multipart/form-data")) {
  6901. const char *type;
  6902. soap->mime.boundary = soap_strdup(soap, soap_http_header_attribute(soap, val, "boundary"));
  6903. soap->mime.start = soap_strdup(soap, soap_http_header_attribute(soap, val, "start"));
  6904. soap->imode |= SOAP_ENC_MIME;
  6905. type = soap_http_header_attribute(soap, val, "type");
  6906. if (type && !strcmp(type, "application/xop+xml"))
  6907. soap->imode |= SOAP_ENC_MTOM;
  6908. }
  6909. action = soap_http_header_attribute(soap, val, "action");
  6910. if (action) {
  6911. if (*action == '"') {
  6912. soap->action = soap_strdup(soap, action + 1);
  6913. if (soap->action && *soap->action)
  6914. soap->action[strlen(soap->action) - 1] = '\0';
  6915. } else
  6916. soap->action = soap_strdup(soap, action);
  6917. }
  6918. }
  6919. #endif
  6920. else if (!soap_tag_cmp(key, "Content-Length")) {
  6921. soap->length = soap_strtoull(val, NULL, 10);
  6922. if (soap->length == 0)
  6923. soap->body = 0;
  6924. } else if (!soap_tag_cmp(key, "Content-Encoding")) {
  6925. if (!soap_tag_cmp(val, "deflate"))
  6926. #ifdef WITH_ZLIB
  6927. soap->zlib_in = SOAP_ZLIB_DEFLATE;
  6928. #else
  6929. return SOAP_ZLIB_ERROR;
  6930. #endif
  6931. else if (!soap_tag_cmp(val, "gzip"))
  6932. #ifdef WITH_GZIP
  6933. soap->zlib_in = SOAP_ZLIB_GZIP;
  6934. #else
  6935. return SOAP_ZLIB_ERROR;
  6936. #endif
  6937. }
  6938. #ifdef WITH_ZLIB
  6939. else if (!soap_tag_cmp(key, "Accept-Encoding"))
  6940. {
  6941. #ifdef WITH_GZIP
  6942. if (strchr(val, '*') || soap_http_header_attribute(soap, val, "gzip"))
  6943. soap->zlib_out = SOAP_ZLIB_GZIP;
  6944. else
  6945. #endif
  6946. if (strchr(val, '*') || soap_http_header_attribute(soap, val, "deflate"))
  6947. soap->zlib_out = SOAP_ZLIB_DEFLATE;
  6948. else
  6949. soap->zlib_out = SOAP_ZLIB_NONE;
  6950. }
  6951. #endif
  6952. else if (!soap_tag_cmp(key, "Transfer-Encoding")) {
  6953. soap->imode &= ~SOAP_IO;
  6954. if (!soap_tag_cmp(val, "chunked"))
  6955. soap->imode |= SOAP_IO_CHUNK;
  6956. } else if (!soap_tag_cmp(key, "Connection")) {
  6957. if (!soap_tag_cmp(val, "close"))
  6958. soap->keep_alive = 0;
  6959. }
  6960. #if !defined(WITH_LEAN) || defined(WITH_NTLM)
  6961. else if (!soap_tag_cmp(key, "Authorization") || !soap_tag_cmp(key, "Proxy-Authorization")) {
  6962. #ifdef WITH_NTLM
  6963. if (!soap_tag_cmp(val, "NTLM*"))
  6964. {
  6965. soap->ntlm_challenge = soap_strdup(soap, val + 4);
  6966. }
  6967. else
  6968. #endif
  6969. if (!soap_tag_cmp(val, "Bearer *")) {
  6970. soap->bearer = soap_strdup(soap, val + 7);
  6971. } else if (!soap_tag_cmp(val, "Basic *")) {
  6972. int n;
  6973. char *s;
  6974. soap_base642s(soap, val + 6, soap->tmpbuf, sizeof(soap->tmpbuf) - 1, &n);
  6975. soap->tmpbuf[n] = '\0';
  6976. s = strchr(soap->tmpbuf, ':');
  6977. if (s) {
  6978. *s = '\0';
  6979. soap->userid = soap_strdup(soap, soap->tmpbuf);
  6980. soap->passwd = soap_strdup(soap, s + 1);
  6981. }
  6982. }
  6983. } else if (!soap_tag_cmp(key, "WWW-Authenticate") || !soap_tag_cmp(key, "Proxy-Authenticate")) {
  6984. #ifdef WITH_NTLM
  6985. if (!soap_tag_cmp(val, "NTLM*"))
  6986. soap->ntlm_challenge = soap_strdup(soap, val + 4);
  6987. else
  6988. #endif
  6989. soap->authrealm = soap_strdup(soap, soap_http_header_attribute(soap, val + 6, "realm"));
  6990. } else if (!soap_tag_cmp(key, "Expect")) {
  6991. if (!soap_tag_cmp(val, "100-continue")) {
  6992. if ((soap->error = soap->fposthdr(soap, "HTTP/1.1 100 Continue", NULL)) != SOAP_OK
  6993. || (soap->error = soap->fposthdr(soap, NULL, NULL)) != SOAP_OK)
  6994. return soap->error;
  6995. }
  6996. }
  6997. #endif
  6998. else if (!soap_tag_cmp(key, "SOAPAction")) {
  6999. if (*val == '"') {
  7000. soap->action = soap_strdup(soap, val + 1);
  7001. if (*soap->action)
  7002. soap->action[strlen(soap->action) - 1] = '\0';
  7003. } else
  7004. soap->action = soap_strdup(soap, val);
  7005. } else if (!soap_tag_cmp(key, "Location")) {
  7006. soap_strcpy(soap->endpoint, sizeof(soap->endpoint), val);
  7007. } else if (!soap_tag_cmp(key, "X-Forwarded-For")) {
  7008. soap->proxy_from = soap_strdup(soap, val);
  7009. } else if (!soap_tag_cmp(key, "Origin")) {
  7010. soap->origin = soap_strdup(soap, val);
  7011. soap->cors_origin = soap->cors_allow;
  7012. } else if (!soap_tag_cmp(key, "Access-Control-Request-Method")) {
  7013. soap->cors_method = soap_strdup(soap, val);
  7014. } else if (!soap_tag_cmp(key, "Access-Control-Request-Headers")) {
  7015. soap->cors_header = soap_strdup(soap, val);
  7016. }
  7017. #ifdef WITH_COOKIES
  7018. else if (!soap_tag_cmp(key, "Cookie")
  7019. || !soap_tag_cmp(key, "Cookie2")
  7020. || !soap_tag_cmp(key, "Set-Cookie")
  7021. || !soap_tag_cmp(key, "Set-Cookie2"))
  7022. {
  7023. soap_getcookies(soap, val);
  7024. }
  7025. #endif
  7026. return SOAP_OK;
  7027. }
  7028. #endif
  7029. /******************************************************************************/
  7030. #if !defined(WITH_NOHTTP) || !defined(WITH_LEANER)
  7031. SOAP_FMAC1
  7032. const char *
  7033. SOAP_FMAC2
  7034. soap_http_header_attribute(struct soap *soap, const char *line, const char *key) {
  7035. const char *s = line;
  7036. if (s) {
  7037. while (*s) {
  7038. short flag;
  7039. s = soap_decode_key(soap->tmpbuf, sizeof(soap->tmpbuf), s);
  7040. flag = soap_tag_cmp(soap->tmpbuf, key);
  7041. s = soap_decode_val(soap->tmpbuf, sizeof(soap->tmpbuf), s);
  7042. if (!flag)
  7043. return soap->tmpbuf;
  7044. }
  7045. }
  7046. return NULL;
  7047. }
  7048. #endif
  7049. /******************************************************************************/
  7050. #if !defined(WITH_NOHTTP) || !defined(WITH_LEANER)
  7051. SOAP_FMAC1
  7052. const char *
  7053. SOAP_FMAC2
  7054. soap_decode_key(char *buf, size_t len, const char *val) {
  7055. return soap_decode(buf, len, val, "=,;");
  7056. }
  7057. #endif
  7058. /******************************************************************************/
  7059. #if !defined(WITH_NOHTTP) || !defined(WITH_LEANER)
  7060. SOAP_FMAC1
  7061. const char *
  7062. SOAP_FMAC2
  7063. soap_decode_val(char *buf, size_t len, const char *val) {
  7064. if (*val != '=') {
  7065. *buf = '\0';
  7066. return val;
  7067. }
  7068. return soap_decode(buf, len, val + 1, ",;");
  7069. }
  7070. #endif
  7071. /******************************************************************************/
  7072. #if !defined(WITH_NOHTTP) || !defined(WITH_LEANER)
  7073. static const char *
  7074. soap_decode(char *buf, size_t len, const char *val, const char *sep) {
  7075. const char *s;
  7076. char *t = buf;
  7077. size_t i = len;
  7078. for (s = val; *s; s++)
  7079. if (*s != ' ' && *s != '\t' && !strchr(sep, *s))
  7080. break;
  7081. if (len > 0) {
  7082. if (*s == '"') {
  7083. s++;
  7084. while (*s && *s != '"' && --i)
  7085. *t++ = *s++;
  7086. } else {
  7087. while (*s && !strchr(sep, *s) && --i) {
  7088. if (*s == '%' && s[1] && s[2]) {
  7089. *t++ = ((s[1] >= 'A' ? (s[1] & 0x7) + 9 : s[1] - '0') << 4)
  7090. + (s[2] >= 'A' ? (s[2] & 0x7) + 9 : s[2] - '0');
  7091. s += 3;
  7092. } else
  7093. *t++ = *s++;
  7094. }
  7095. }
  7096. buf[len - 1] = '\0'; /* appease static checkers that get confused */
  7097. }
  7098. *t = '\0';
  7099. while (*s && !strchr(sep, *s))
  7100. s++;
  7101. return s;
  7102. }
  7103. #endif
  7104. /******************************************************************************/
  7105. #ifndef WITH_NOHTTP
  7106. static const char *
  7107. http_error(struct soap *soap, int status) {
  7108. const char *msg = SOAP_STR_EOS;
  7109. (void) soap;
  7110. (void) status;
  7111. #ifndef WITH_LEAN
  7112. msg = soap_code_str(h_http_error_codes, status);
  7113. if (!msg)
  7114. msg = SOAP_STR_EOS;
  7115. #endif
  7116. return msg;
  7117. }
  7118. #endif
  7119. /******************************************************************************/
  7120. #ifndef WITH_NOHTTP
  7121. static int
  7122. http_get(struct soap *soap) {
  7123. (void) soap;
  7124. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "HTTP GET request\n"));
  7125. return SOAP_GET_METHOD;
  7126. }
  7127. #endif
  7128. /******************************************************************************/
  7129. #ifndef WITH_NOHTTP
  7130. static int
  7131. http_put(struct soap *soap) {
  7132. (void) soap;
  7133. return SOAP_PUT_METHOD;
  7134. }
  7135. #endif
  7136. /******************************************************************************/
  7137. #ifndef WITH_NOHTTP
  7138. static int
  7139. http_patch(struct soap *soap) {
  7140. (void) soap;
  7141. return SOAP_PATCH_METHOD;
  7142. }
  7143. #endif
  7144. /******************************************************************************/
  7145. #ifndef WITH_NOHTTP
  7146. static int
  7147. http_del(struct soap *soap) {
  7148. (void) soap;
  7149. return SOAP_DEL_METHOD;
  7150. }
  7151. #endif
  7152. /******************************************************************************/
  7153. #ifndef WITH_NOHTTP
  7154. static int
  7155. http_200(struct soap *soap) {
  7156. if (soap->origin && soap->cors_method) /* CORS Origin and Access-Control-Request-Method headers */
  7157. {
  7158. soap->cors_origin = soap->cors_allow; /* modify this code or hook your own soap->fopt() callback with logic */
  7159. soap->cors_methods = "GET, PUT, PATCH, POST, HEAD, OPTIONS";
  7160. soap->cors_headers = soap->cors_header;
  7161. }
  7162. return soap_send_empty_response(soap, 200);
  7163. }
  7164. #endif
  7165. /******************************************************************************/
  7166. #ifndef WITH_NOHTTP
  7167. static int
  7168. http_post(struct soap *soap, const char *endpoint, const char *host, int port, const char *path, const char *action,
  7169. ULONG64 count) {
  7170. const char *s;
  7171. int err;
  7172. size_t l;
  7173. switch (soap->status) {
  7174. case SOAP_GET:
  7175. s = "GET";
  7176. break;
  7177. case SOAP_PUT:
  7178. s = "PUT";
  7179. break;
  7180. case SOAP_PATCH:
  7181. s = "PATCH";
  7182. break;
  7183. case SOAP_DEL:
  7184. s = "DELETE";
  7185. break;
  7186. case SOAP_CONNECT:
  7187. s = "CONNECT";
  7188. break;
  7189. case SOAP_HEAD:
  7190. s = "HEAD";
  7191. break;
  7192. case SOAP_OPTIONS:
  7193. s = "OPTIONS";
  7194. break;
  7195. default:
  7196. s = "POST";
  7197. }
  7198. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "HTTP %s to %s\n", s, endpoint ? endpoint : "(null)"));
  7199. if (!endpoint ||
  7200. (soap_tag_cmp(endpoint, "http:*") && soap_tag_cmp(endpoint, "https:*") && soap_tag_cmp(endpoint, "httpg:*")))
  7201. return SOAP_OK;
  7202. /* set l to prevent overruns ('host' and 'soap->host' are substrings of 'endpoint') */
  7203. l = strlen(endpoint) + strlen(soap->http_version) + 80;
  7204. if (l > sizeof(soap->tmpbuf))
  7205. return soap->error = SOAP_EOM;
  7206. if (soap->status == SOAP_CONNECT)
  7207. (SOAP_SNPRINTF(soap->tmpbuf, sizeof(soap->tmpbuf), l), "%s %s:%d HTTP/%s", s, soap->host, soap->port,
  7208. soap->http_version);
  7209. else if (soap->proxy_host && endpoint)
  7210. (SOAP_SNPRINTF(soap->tmpbuf, sizeof(soap->tmpbuf), l), "%s %s HTTP/%s", s, endpoint, soap->http_version);
  7211. else
  7212. (SOAP_SNPRINTF(soap->tmpbuf, sizeof(soap->tmpbuf), l), "%s /%s HTTP/%s", s, (*path == '/' ? path + 1 : path),
  7213. soap->http_version);
  7214. err = soap->fposthdr(soap, soap->tmpbuf, NULL);
  7215. if (err)
  7216. return err;
  7217. #ifdef WITH_OPENSSL
  7218. if ((soap->ssl && port != 443) || (!soap->ssl && port != 80))
  7219. #else
  7220. if (port != 80)
  7221. #endif
  7222. {
  7223. #ifdef WITH_IPV6
  7224. if (*host != '[' && strchr(host, ':'))
  7225. (SOAP_SNPRINTF(soap->tmpbuf, sizeof(soap->tmpbuf), l), "[%s]:%d", host, port); /* RFC 2732 */
  7226. else
  7227. #endif
  7228. (SOAP_SNPRINTF(soap->tmpbuf, sizeof(soap->tmpbuf), l), "%s:%d", host, port);
  7229. } else {
  7230. #ifdef WITH_IPV6
  7231. if (*host != '[' && strchr(host, ':'))
  7232. (SOAP_SNPRINTF(soap->tmpbuf, sizeof(soap->tmpbuf), l), "[%s]", host); /* RFC 2732 */
  7233. else
  7234. #endif
  7235. soap_strcpy(soap->tmpbuf, sizeof(soap->tmpbuf), host);
  7236. }
  7237. err = soap->fposthdr(soap, "Host", soap->tmpbuf);
  7238. if (err)
  7239. return err;
  7240. err = soap->fposthdr(soap, "User-Agent", "gSOAP/2.8");
  7241. if (err)
  7242. return err;
  7243. if (soap->origin) {
  7244. err = soap->fposthdr(soap, "Origin", soap->origin);
  7245. if (err)
  7246. return err;
  7247. if (soap->status == SOAP_OPTIONS) {
  7248. err = soap->fposthdr(soap, "Access-Control-Request-Method", soap->cors_method ? soap->cors_method : "POST");
  7249. if (err)
  7250. return err;
  7251. if (soap->cors_header) {
  7252. err = soap->fposthdr(soap, "Access-Control-Request-Headers", soap->cors_header);
  7253. if (err)
  7254. return err;
  7255. }
  7256. }
  7257. }
  7258. err = soap_puthttphdr(soap, SOAP_OK, count);
  7259. if (err)
  7260. return err;
  7261. #ifndef WITH_LEANER
  7262. if ((soap->imode & SOAP_ENC_MTOM)) {
  7263. err = soap->fposthdr(soap, "Accept", "multipart/related,application/xop+xml,*/*;q=0.8");
  7264. if (err)
  7265. return err;
  7266. }
  7267. #endif
  7268. #ifdef WITH_ZLIB
  7269. #ifdef WITH_GZIP
  7270. err = soap->fposthdr(soap, "Accept-Encoding", "gzip,deflate");
  7271. #else
  7272. err = soap->fposthdr(soap, "Accept-Encoding", "deflate");
  7273. #endif
  7274. if (err)
  7275. return err;
  7276. #endif
  7277. #if !defined(WITH_LEAN) || defined(WITH_NTLM)
  7278. if (soap->bearer) {
  7279. l = strlen(soap->bearer);
  7280. (SOAP_SNPRINTF(soap->tmpbuf, sizeof(soap->tmpbuf), l + 1), "Bearer %s", soap->bearer);
  7281. err = soap->fposthdr(soap, "Authorization", soap->tmpbuf);
  7282. if (err)
  7283. return err;
  7284. }
  7285. #ifdef WITH_NTLM
  7286. if (soap->ntlm_challenge)
  7287. {
  7288. l = strlen(soap->ntlm_challenge);
  7289. if (l)
  7290. {
  7291. (SOAP_SNPRINTF(soap->tmpbuf, sizeof(soap->tmpbuf), l + 5), "NTLM %s", soap->ntlm_challenge);
  7292. if (soap->proxy_host)
  7293. {
  7294. err = soap->fposthdr(soap, "Proxy-Authorization", soap->tmpbuf);
  7295. if (err)
  7296. return err;
  7297. }
  7298. else
  7299. {
  7300. err = soap->fposthdr(soap, "Authorization", soap->tmpbuf);
  7301. if (err)
  7302. return err;
  7303. }
  7304. }
  7305. }
  7306. else
  7307. {
  7308. #endif
  7309. if (soap->userid && soap->passwd) {
  7310. soap_strcpy(soap->tmpbuf, sizeof(soap->tmpbuf), "Basic ");
  7311. (SOAP_SNPRINTF(soap->tmpbuf + 262, sizeof(soap->tmpbuf) - 262, strlen(soap->userid) + strlen(soap->passwd) + 1),
  7312. "%s:%s", soap->userid, soap->passwd);
  7313. soap_s2base64(soap, (const unsigned char *) (soap->tmpbuf + 262), soap->tmpbuf + 6,
  7314. (int) strlen(soap->tmpbuf + 262));
  7315. err = soap->fposthdr(soap, "Authorization", soap->tmpbuf);
  7316. if (err)
  7317. return err;
  7318. }
  7319. if (soap->proxy_userid && soap->proxy_passwd) {
  7320. soap_strcpy(soap->tmpbuf, sizeof(soap->tmpbuf), "Basic ");
  7321. (SOAP_SNPRINTF(soap->tmpbuf + 262, sizeof(soap->tmpbuf) - 262,
  7322. strlen(soap->proxy_userid) + strlen(soap->proxy_passwd) + 1), "%s:%s", soap->proxy_userid,
  7323. soap->proxy_passwd);
  7324. soap_s2base64(soap, (const unsigned char *) (soap->tmpbuf + 262), soap->tmpbuf + 6,
  7325. (int) strlen(soap->tmpbuf + 262));
  7326. err = soap->fposthdr(soap, "Proxy-Authorization", soap->tmpbuf);
  7327. if (err)
  7328. return err;
  7329. }
  7330. #ifdef WITH_NTLM
  7331. }
  7332. #endif
  7333. #endif
  7334. #ifdef WITH_COOKIES
  7335. #ifdef WITH_OPENSSL
  7336. if (soap_putcookies(soap, host, path, soap->ssl != NULL))
  7337. return soap->error;
  7338. #else
  7339. if (soap_putcookies(soap, host, path, 0))
  7340. return soap->error;
  7341. #endif
  7342. #endif
  7343. if (action && soap->status != SOAP_GET && soap->status != SOAP_DEL) {
  7344. (SOAP_SNPRINTF(soap->tmpbuf, sizeof(soap->tmpbuf), strlen(action) + 2), "\"%s\"", action);
  7345. err = soap->fposthdr(soap, "SOAPAction", soap->tmpbuf);
  7346. if (err)
  7347. return err;
  7348. }
  7349. return soap->fposthdr(soap, NULL, NULL);
  7350. }
  7351. #endif
  7352. /******************************************************************************/
  7353. #ifndef WITH_NOHTTP
  7354. static int
  7355. http_send_header(struct soap *soap, const char *s) {
  7356. const char *t;
  7357. do {
  7358. t = strchr(s, '\n'); /* disallow \n in HTTP headers */
  7359. if (!t)
  7360. t = s + strlen(s);
  7361. if (soap_send_raw(soap, s, t - s))
  7362. return soap->error;
  7363. s = t + 1;
  7364. } while (*t);
  7365. return SOAP_OK;
  7366. }
  7367. #endif
  7368. /******************************************************************************/
  7369. #ifndef WITH_NOHTTP
  7370. static int
  7371. http_post_header(struct soap *soap, const char *key, const char *val) {
  7372. if (key) {
  7373. if (http_send_header(soap, key))
  7374. return soap->error;
  7375. if (val && (soap_send_raw(soap, ": ", 2) || http_send_header(soap, val)))
  7376. return soap->error;
  7377. }
  7378. return soap_send_raw(soap, "\r\n", 2);
  7379. }
  7380. #endif
  7381. /******************************************************************************/
  7382. #ifndef WITH_NOHTTP
  7383. static int
  7384. http_response(struct soap *soap, int status, ULONG64 count) {
  7385. int err;
  7386. char http[32];
  7387. int code = status;
  7388. const char *line;
  7389. #ifdef WMW_RPM_IO
  7390. if (soap->rpmreqid)
  7391. httpOutputEnable(soap->rpmreqid);
  7392. if (soap->rpmreqid
  7393. || soap_valid_socket(soap->master)
  7394. || soap_valid_socket(soap->socket)
  7395. || soap->recvfd != 0
  7396. || soap->sendfd != 1
  7397. || soap->os) /* RPM behaves as if standalone */
  7398. #else
  7399. if (soap_valid_socket(soap->master)
  7400. || soap_valid_socket(soap->socket)
  7401. #ifndef UNDER_CE
  7402. || soap->recvfd != 0
  7403. || soap->sendfd != 1
  7404. #else
  7405. || soap->recvfd != stdin
  7406. || soap->sendfd != stdout
  7407. #endif
  7408. || soap->os) /* standalone server application (over sockets), not CGI (over stdin/out) */
  7409. #endif
  7410. (SOAP_SNPRINTF(http, sizeof(http), strlen(soap->http_version) + 5), "HTTP/%s", soap->http_version);
  7411. else
  7412. soap_strcpy(http, sizeof(http), "Status:");
  7413. if (status >= SOAP_FILE && status < SOAP_FILE + 600) {
  7414. code = status - SOAP_FILE;
  7415. if (code == 0)
  7416. code = 200;
  7417. } else if (!status || status == SOAP_HTML) {
  7418. if (count || ((soap->omode & SOAP_IO) == SOAP_IO_CHUNK))
  7419. code = 200;
  7420. else
  7421. code = 202;
  7422. } else if (status < 200 || status >= 600) {
  7423. const char *s = *soap_faultcode(soap);
  7424. if (status >= SOAP_GET_METHOD && status <= SOAP_HTTP_METHOD)
  7425. code = 405;
  7426. else if (soap->version == 2 && (!s || !strcmp(s, "SOAP-ENV:Sender")))
  7427. code = 400;
  7428. else
  7429. code = 500;
  7430. }
  7431. line = http_error(soap, code);
  7432. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "HTTP Status = %d %s\n", code, line));
  7433. (SOAP_SNPRINTF(soap->tmpbuf, sizeof(soap->tmpbuf), sizeof(http) + 22 + strlen(line)), "%s %d %s", http, code, line);
  7434. err = soap->fposthdr(soap, soap->tmpbuf, NULL);
  7435. if (err)
  7436. return err;
  7437. #ifndef WITH_LEAN
  7438. if (status == 401) {
  7439. (SOAP_SNPRINTF_SAFE(soap->tmpbuf, sizeof(soap->tmpbuf)), "Basic realm=\"%s\"", (soap->authrealm &&
  7440. strlen(soap->authrealm) + 14 <
  7441. sizeof(soap->tmpbuf))
  7442. ? soap->authrealm
  7443. : "gSOAP Web Service");
  7444. err = soap->fposthdr(soap, "WWW-Authenticate", soap->tmpbuf);
  7445. if (err)
  7446. return err;
  7447. } else if ((status >= 301 && status <= 303) || status == 307) {
  7448. err = soap->fposthdr(soap, "Location", soap->endpoint);
  7449. if (err)
  7450. return err;
  7451. }
  7452. #endif
  7453. err = soap->fposthdr(soap, "Server", "ACS/1.1 By Sassan");
  7454. // err = soap->fposthdr(soap, "Server", "gSOAP/2.8");
  7455. if (err)
  7456. return err;
  7457. if (soap->cors_origin) {
  7458. err = soap->fposthdr(soap, "Access-Control-Allow-Origin", soap->cors_origin);
  7459. if (err)
  7460. return err;
  7461. err = soap->fposthdr(soap, "Access-Control-Allow-Credentials", "true");
  7462. if (err)
  7463. return err;
  7464. if (soap->cors_methods) {
  7465. err = soap->fposthdr(soap, "Access-Control-Allow-Methods", soap->cors_methods);
  7466. if (err)
  7467. return err;
  7468. if (soap->cors_headers) {
  7469. err = soap->fposthdr(soap, "Access-Control-Allow-Headers", soap->cors_headers);
  7470. if (err)
  7471. return err;
  7472. }
  7473. }
  7474. }
  7475. if (soap->x_frame_options) {
  7476. err = soap->fposthdr(soap, "X-Frame-Options", soap->x_frame_options);
  7477. if (err)
  7478. return err;
  7479. }
  7480. soap->cors_origin = NULL;
  7481. soap->cors_methods = NULL;
  7482. soap->cors_headers = NULL;
  7483. err = soap_puthttphdr(soap, status, count);
  7484. if (err)
  7485. return err;
  7486. #ifdef WITH_COOKIES
  7487. if (soap_putsetcookies(soap))
  7488. return soap->error;
  7489. soap_free_cookies(soap);
  7490. #endif
  7491. return soap->fposthdr(soap, NULL, NULL);
  7492. }
  7493. #endif
  7494. /******************************************************************************/
  7495. SOAP_FMAC1
  7496. int
  7497. SOAP_FMAC2
  7498. soap_response(struct soap *soap, int status) {
  7499. ULONG64 count;
  7500. if (!(soap->omode & (SOAP_ENC_PLAIN | SOAP_IO_STORE /* this tests for chunking too */))
  7501. && (status == SOAP_HTML || (status >= SOAP_FILE && status < SOAP_FILE + 600)))
  7502. soap->omode = (soap->omode & ~SOAP_IO) | SOAP_IO_STORE;
  7503. soap->status = status;
  7504. count = soap_count_attachments(soap);
  7505. if (soap_init_send(soap))
  7506. return soap->error;
  7507. #ifndef WITH_NOHTTP
  7508. if ((soap->mode & SOAP_IO) != SOAP_IO_STORE && !(soap->mode & SOAP_ENC_PLAIN)) {
  7509. int k = soap->mode;
  7510. soap->mode &= ~(SOAP_IO | SOAP_ENC_ZLIB);
  7511. if ((k & SOAP_IO) != SOAP_IO_FLUSH)
  7512. soap->mode |= SOAP_IO_BUFFER;
  7513. soap->error = soap->fresponse(soap, status, count);
  7514. if (soap->error)
  7515. return soap->error;
  7516. #ifndef WITH_LEANER
  7517. if ((k & SOAP_IO) == SOAP_IO_CHUNK) {
  7518. if (soap_flush(soap))
  7519. return soap->error;
  7520. }
  7521. #endif
  7522. soap->mode = k;
  7523. }
  7524. #endif
  7525. #ifndef WITH_LEANER
  7526. if (soap_begin_attachments(soap))
  7527. return soap->error;
  7528. #endif
  7529. return SOAP_OK;
  7530. }
  7531. /******************************************************************************/
  7532. SOAP_FMAC1
  7533. const char *
  7534. SOAP_FMAC2
  7535. soap_extend_url(struct soap *soap, const char *s, const char *t) {
  7536. if (s)
  7537. soap_strcpy(soap->msgbuf, sizeof(soap->msgbuf), s);
  7538. else
  7539. *soap->msgbuf = '\0';
  7540. if (t && (*t == '/' || *t == '?')) {
  7541. char *r = strchr(soap->msgbuf, '?');
  7542. if (r) {
  7543. if (*t == '?') {
  7544. soap_strcat(soap->msgbuf, sizeof(soap->msgbuf), "&");
  7545. soap_strcat(soap->msgbuf, sizeof(soap->msgbuf), t + 1);
  7546. } else /* *t == '/' */
  7547. {
  7548. size_t l = r - soap->msgbuf;
  7549. *r = '\0';
  7550. soap_strcat(soap->msgbuf, sizeof(soap->msgbuf), t);
  7551. if (s)
  7552. soap_strcat(soap->msgbuf, sizeof(soap->msgbuf), s + l);
  7553. }
  7554. } else {
  7555. soap_strcat(soap->msgbuf, sizeof(soap->msgbuf), t);
  7556. }
  7557. }
  7558. return soap->msgbuf;
  7559. }
  7560. /******************************************************************************/
  7561. SOAP_FMAC1
  7562. const char *
  7563. SOAP_FMAC2
  7564. soap_extend_url_query(struct soap *soap, const char *s, const char *t) {
  7565. (void) soap_extend_url(soap, s, t); /* fills and returns soap->msgbuf */
  7566. if (strchr(soap->msgbuf, '?'))
  7567. soap_strcat(soap->msgbuf, sizeof(soap->msgbuf), "&");
  7568. else
  7569. soap_strcat(soap->msgbuf, sizeof(soap->msgbuf), "?");
  7570. return soap->msgbuf;
  7571. }
  7572. /******************************************************************************/
  7573. SOAP_FMAC1
  7574. void
  7575. SOAP_FMAC2
  7576. soap_url_query(struct soap *soap, const char *s, const char *t) {
  7577. size_t n = strlen(s);
  7578. if (n) {
  7579. char *r = soap->msgbuf;
  7580. size_t k = n - (s[n - 1] == '=');
  7581. while ((r = strchr(r, '{')) != NULL)
  7582. if (!strncmp(++r, s, k) && r[k] == '}')
  7583. break;
  7584. if (r) {
  7585. size_t m = t ? strlen(t) : 0;
  7586. (void) soap_memmove(r + m - 1, soap->msgbuf + sizeof(soap->msgbuf) - (r + n + 1), r + k + 1,
  7587. strlen(r + k + 1) + 1);
  7588. if (m)
  7589. (void) soap_memmove(r - 1, soap->msgbuf + sizeof(soap->msgbuf) - (r - 1), t, m);
  7590. } else {
  7591. soap_strcat(soap->msgbuf, sizeof(soap->msgbuf), s);
  7592. if (t) {
  7593. int m = (int) strlen(soap->msgbuf); /* msgbuf length is max SOAP_TMPLEN or just 1024 bytes */
  7594. (void) soap_encode_url(t, soap->msgbuf + m, (int) sizeof(soap->msgbuf) - m);
  7595. }
  7596. soap_strcat(soap->msgbuf, sizeof(soap->msgbuf), "&");
  7597. }
  7598. }
  7599. }
  7600. /******************************************************************************/
  7601. SOAP_FMAC1
  7602. int
  7603. SOAP_FMAC2
  7604. soap_encode_url(const char *s, char *t, int len) {
  7605. int c;
  7606. int n = len;
  7607. if (s && n > 0) {
  7608. while ((c = *s++) && --n > 0) {
  7609. if (c == '-'
  7610. || c == '.'
  7611. || (c >= '0' && c <= '9')
  7612. || (c >= 'A' && c <= 'Z')
  7613. || c == '_'
  7614. || (c >= 'a' && c <= 'z')
  7615. || c == '~') {
  7616. *t++ = c;
  7617. } else if (n > 2) {
  7618. *t++ = '%';
  7619. *t++ = (c >> 4) + (c > 159 ? '7' : '0');
  7620. c &= 0xF;
  7621. *t++ = c + (c > 9 ? '7' : '0');
  7622. n -= 2;
  7623. } else {
  7624. break;
  7625. }
  7626. }
  7627. *t = '\0';
  7628. }
  7629. return len - n;
  7630. }
  7631. /******************************************************************************/
  7632. SOAP_FMAC1
  7633. const char *
  7634. SOAP_FMAC2
  7635. soap_encode_url_string(struct soap *soap, const char *s) {
  7636. if (s) {
  7637. int n = 3 * (int) strlen(s) + 1;
  7638. char *t = (char *) soap_malloc(soap, n);
  7639. if (t) {
  7640. (void) soap_encode_url(s, t, n);
  7641. return t;
  7642. }
  7643. }
  7644. return SOAP_STR_EOS;
  7645. }
  7646. /******************************************************************************\
  7647. *
  7648. * HTTP Cookies RFC 6265
  7649. *
  7650. \******************************************************************************/
  7651. #ifdef WITH_COOKIES
  7652. SOAP_FMAC1
  7653. struct soap_cookie*
  7654. SOAP_FMAC2
  7655. soap_cookie(struct soap *soap, const char *name, const char *domain, const char *path)
  7656. {
  7657. return soap_cookie_env(soap, name, domain, path, 0);
  7658. }
  7659. /******************************************************************************/
  7660. SOAP_FMAC1
  7661. struct soap_cookie*
  7662. SOAP_FMAC2
  7663. soap_cookie_env(struct soap *soap, const char *name, const char *domain, const char *path, short env)
  7664. {
  7665. struct soap_cookie *p;
  7666. if (!domain && !env)
  7667. domain = soap->cookie_domain;
  7668. if (!path)
  7669. path = soap->cookie_path;
  7670. if (!path)
  7671. path = SOAP_STR_EOS;
  7672. else if (*path == '/')
  7673. path++;
  7674. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Search cookie='%s' domain='%s' path='%s' env=%hd\n", name, domain ? domain : "(null)", path ? path : "(null)", env));
  7675. for (p = soap->cookies; p; p = p->next)
  7676. {
  7677. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Cookie in database: %s='%s' domain='%s' path='%s' env=%hd\n", p->name, p->value ? p->value : "(null)", p->domain ? p->domain : "(null)", p->path ? p->path : "(null)", p->env));
  7678. if ((!env || p->env)
  7679. && !strcmp(p->name, name)
  7680. && (!domain || (domain && p->domain && !strcmp(p->domain, domain)))
  7681. && (!path || (path && p->path && !strncmp(p->path, path, strlen(p->path)))))
  7682. break;
  7683. }
  7684. return p;
  7685. }
  7686. /******************************************************************************/
  7687. SOAP_FMAC1
  7688. struct soap_cookie*
  7689. SOAP_FMAC2
  7690. soap_set_cookie(struct soap *soap, const char *name, const char *value, const char *domain, const char *path)
  7691. {
  7692. struct soap_cookie **p, *q;
  7693. int n;
  7694. if (!domain)
  7695. domain = soap->cookie_domain;
  7696. if (!path)
  7697. path = soap->cookie_path;
  7698. if (!path)
  7699. path = SOAP_STR_EOS;
  7700. else if (*path == '/')
  7701. path++;
  7702. q = soap_cookie(soap, name, domain, path);
  7703. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Set %scookie: %s='%s' domain='%s' path='%s'\n", q ? SOAP_STR_EOS : "new ", name, value ? value : "(null)", domain ? domain : "(null)", path ? path : "(null)"));
  7704. if (!q)
  7705. {
  7706. q = (struct soap_cookie*)SOAP_MALLOC(soap, sizeof(struct soap_cookie));
  7707. if (q)
  7708. {
  7709. size_t l = strlen(name) + 1;
  7710. q->name = NULL;
  7711. if (SOAP_MAXALLOCSIZE <= 0 || l <= SOAP_MAXALLOCSIZE)
  7712. q->name = (char*)SOAP_MALLOC(soap, l);
  7713. if (q->name)
  7714. (void)soap_memcpy(q->name, l, name, l);
  7715. q->value = NULL;
  7716. q->domain = NULL;
  7717. q->path = NULL;
  7718. q->expire = 0;
  7719. q->maxage = -1;
  7720. q->version = 1;
  7721. q->secure = 0;
  7722. q->modified = 0;
  7723. for (p = &soap->cookies, n = soap->cookie_max; *p && n; p = &(*p)->next, n--)
  7724. if (!strcmp((*p)->name, name) && (*p)->path && path && strcmp((*p)->path, path) < 0)
  7725. break;
  7726. if (n)
  7727. {
  7728. q->next = *p;
  7729. *p = q;
  7730. }
  7731. else
  7732. {
  7733. SOAP_FREE(soap, q->name);
  7734. SOAP_FREE(soap, q);
  7735. q = NULL;
  7736. }
  7737. }
  7738. }
  7739. else
  7740. {
  7741. q->modified = 1;
  7742. }
  7743. if (q)
  7744. {
  7745. if (q->value)
  7746. {
  7747. if (!value || strcmp(value, q->value))
  7748. {
  7749. SOAP_FREE(soap, q->value);
  7750. q->value = NULL;
  7751. }
  7752. }
  7753. if (value && *value && !q->value)
  7754. {
  7755. size_t l = strlen(value) + 1;
  7756. q->value = NULL;
  7757. if (SOAP_MAXALLOCSIZE <= 0 || l <= SOAP_MAXALLOCSIZE)
  7758. q->value = (char*)SOAP_MALLOC(soap, l);
  7759. if (q->value)
  7760. soap_strcpy(q->value, l, value);
  7761. }
  7762. if (q->domain)
  7763. {
  7764. if (!domain || strcmp(domain, q->domain))
  7765. {
  7766. SOAP_FREE(soap, q->domain);
  7767. q->domain = NULL;
  7768. }
  7769. }
  7770. if (domain && !q->domain)
  7771. {
  7772. size_t l = strlen(domain) + 1;
  7773. q->domain = NULL;
  7774. if (SOAP_MAXALLOCSIZE <= 0 || l <= SOAP_MAXALLOCSIZE)
  7775. q->domain = (char*)SOAP_MALLOC(soap, l);
  7776. if (q->domain)
  7777. soap_strcpy(q->domain, l, domain);
  7778. }
  7779. if (q->path)
  7780. {
  7781. if (!path || strncmp(path, q->path, strlen(q->path)))
  7782. {
  7783. SOAP_FREE(soap, q->path);
  7784. q->path = NULL;
  7785. }
  7786. }
  7787. if (path && !q->path)
  7788. {
  7789. size_t l = strlen(path) + 1;
  7790. q->path = NULL;
  7791. if (SOAP_MAXALLOCSIZE <= 0 || l <= SOAP_MAXALLOCSIZE)
  7792. q->path = (char*)SOAP_MALLOC(soap, l);
  7793. if (q->path)
  7794. soap_strcpy(q->path, l, path);
  7795. }
  7796. q->session = 1;
  7797. q->env = 0;
  7798. }
  7799. return q;
  7800. }
  7801. /******************************************************************************/
  7802. SOAP_FMAC1
  7803. void
  7804. SOAP_FMAC2
  7805. soap_clr_cookie(struct soap *soap, const char *name, const char *domain, const char *path)
  7806. {
  7807. struct soap_cookie **p, *q;
  7808. if (!domain)
  7809. domain = soap->cookie_domain;
  7810. if (!domain)
  7811. {
  7812. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Error in clear cookie='%s': cookie domain not set\n", name ? name : "(null)"));
  7813. return;
  7814. }
  7815. if (!path)
  7816. path = soap->cookie_path;
  7817. if (!path)
  7818. path = SOAP_STR_EOS;
  7819. else if (*path == '/')
  7820. path++;
  7821. for (p = &soap->cookies, q = *p; q; q = *p)
  7822. {
  7823. if (!strcmp(q->name, name) && (!q->domain || !strcmp(q->domain, domain)) && (!q->path || !strncmp(q->path, path, strlen(q->path))))
  7824. {
  7825. SOAP_FREE(soap, q->name);
  7826. if (q->value)
  7827. SOAP_FREE(soap, q->value);
  7828. if (q->domain)
  7829. SOAP_FREE(soap, q->domain);
  7830. if (q->path)
  7831. SOAP_FREE(soap, q->path);
  7832. *p = q->next;
  7833. SOAP_FREE(soap, q);
  7834. }
  7835. else
  7836. p = &q->next;
  7837. }
  7838. }
  7839. /******************************************************************************/
  7840. SOAP_FMAC1
  7841. const char *
  7842. SOAP_FMAC2
  7843. soap_cookie_value(struct soap *soap, const char *name, const char *domain, const char *path)
  7844. {
  7845. struct soap_cookie *p;
  7846. p = soap_cookie(soap, name, domain, path);
  7847. if (p)
  7848. return p->value;
  7849. return NULL;
  7850. }
  7851. /******************************************************************************/
  7852. SOAP_FMAC1
  7853. const char *
  7854. SOAP_FMAC2
  7855. soap_env_cookie_value(struct soap *soap, const char *name, const char *domain, const char *path)
  7856. {
  7857. struct soap_cookie *p;
  7858. p = soap_cookie(soap, name, domain, path);
  7859. if (p && p->env)
  7860. return p->value;
  7861. return NULL;
  7862. }
  7863. /******************************************************************************/
  7864. SOAP_FMAC1
  7865. time_t
  7866. SOAP_FMAC2
  7867. soap_cookie_expire(struct soap *soap, const char *name, const char *domain, const char *path)
  7868. {
  7869. struct soap_cookie *p;
  7870. p = soap_cookie(soap, name, domain, path);
  7871. if (p)
  7872. return (time_t)p->expire;
  7873. return -1;
  7874. }
  7875. /******************************************************************************/
  7876. SOAP_FMAC1
  7877. int
  7878. SOAP_FMAC2
  7879. soap_set_cookie_expire(struct soap *soap, const char *name, long maxage, const char *domain, const char *path)
  7880. {
  7881. struct soap_cookie *p;
  7882. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Set cookie expiration max-age=%ld: cookie='%s' domain='%s' path='%s'\n", maxage, name, domain ? domain : "(null)", path ? path : "(null)"));
  7883. p = soap_cookie(soap, name, domain, path);
  7884. if (p)
  7885. {
  7886. p->maxage = maxage;
  7887. p->modified = 1;
  7888. return SOAP_OK;
  7889. }
  7890. return SOAP_ERR;
  7891. }
  7892. /******************************************************************************/
  7893. SOAP_FMAC1
  7894. int
  7895. SOAP_FMAC2
  7896. soap_set_cookie_secure(struct soap *soap, const char *name, const char *domain, const char *path)
  7897. {
  7898. struct soap_cookie *p;
  7899. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Set cookie secure: cookie='%s' domain='%s' path='%s'\n", name, domain ? domain : "(null)", path ? path : "(null)"));
  7900. p = soap_cookie(soap, name, domain, path);
  7901. if (p)
  7902. {
  7903. p->secure = 1;
  7904. p->modified = 1;
  7905. return SOAP_OK;
  7906. }
  7907. return SOAP_ERR;
  7908. }
  7909. /******************************************************************************/
  7910. SOAP_FMAC1
  7911. int
  7912. SOAP_FMAC2
  7913. soap_set_cookie_session(struct soap *soap, const char *name, const char *domain, const char *path)
  7914. {
  7915. struct soap_cookie *p;
  7916. p = soap_cookie(soap, name, domain, path);
  7917. if (p)
  7918. {
  7919. p->session = 1;
  7920. p->modified = 1;
  7921. return SOAP_OK;
  7922. }
  7923. return SOAP_ERR;
  7924. }
  7925. /******************************************************************************/
  7926. SOAP_FMAC1
  7927. int
  7928. SOAP_FMAC2
  7929. soap_clr_cookie_session(struct soap *soap, const char *name, const char *domain, const char *path)
  7930. {
  7931. struct soap_cookie *p;
  7932. p = soap_cookie(soap, name, domain, path);
  7933. if (p)
  7934. {
  7935. p->session = 0;
  7936. p->modified = 1;
  7937. return SOAP_OK;
  7938. }
  7939. return SOAP_ERR;
  7940. }
  7941. /******************************************************************************/
  7942. SOAP_FMAC1
  7943. int
  7944. SOAP_FMAC2
  7945. soap_putsetcookies(struct soap *soap)
  7946. {
  7947. struct soap_cookie *p;
  7948. char *s, tmp[4096];
  7949. const char *t;
  7950. for (p = soap->cookies; p; p = p->next)
  7951. {
  7952. if ((p->modified
  7953. #ifdef WITH_OPENSSL
  7954. || (!p->env && !soap->ssl == !p->secure)
  7955. #endif
  7956. ) && p->name && p->value && *p->name && *p->value)
  7957. {
  7958. s = tmp;
  7959. s += soap_encode_url(p->name, s, 3967);
  7960. *s++ = '=';
  7961. s += soap_encode_url(p->value, s, 3968 - (int)(s-tmp));
  7962. t = p->domain ? p->domain : soap->cookie_domain;
  7963. if (t && (int)strlen(t) < 3968 - (int)(s-tmp))
  7964. {
  7965. soap_strcpy(s, 4096 - (s-tmp), ";Domain=");
  7966. s += 8;
  7967. soap_strcpy(s, 4096 - (s-tmp), t);
  7968. s += strlen(s);
  7969. }
  7970. t = p->path ? p->path : soap->cookie_path;
  7971. if (t && (int)strlen(t) < 3976 - (int)(s-tmp))
  7972. {
  7973. soap_strcpy(s, 4096 - (s-tmp), ";Path=/");
  7974. s += 7;
  7975. if (*t == '/')
  7976. t++;
  7977. if (strchr(t, '%')) /* already URL encoded? */
  7978. {
  7979. soap_strcpy(s, 4096 - (s-tmp), t);
  7980. s += strlen(s);
  7981. }
  7982. else
  7983. {
  7984. s += soap_encode_url(t, s, 4096 - (int)(s-tmp));
  7985. }
  7986. }
  7987. if (p->version > 0 && s-tmp < 3983)
  7988. {
  7989. (SOAP_SNPRINTF(s, 4096 - (s-tmp), 29), ";Version=%u", p->version);
  7990. s += strlen(s);
  7991. }
  7992. if (p->maxage >= 0 && s-tmp < 4012)
  7993. {
  7994. (SOAP_SNPRINTF(s, 4096 - (s-tmp), 29), ";Max-Age=%ld", p->maxage);
  7995. s += strlen(s);
  7996. }
  7997. #if !defined(WITH_LEAN)
  7998. #if defined(HAVE_GMTIME_R) || defined(HAVE_GMTIME)
  7999. if (p->maxage >= 0 && s-tmp < 4041)
  8000. {
  8001. time_t n = time(NULL) + p->maxage;
  8002. struct tm T, *pT = &T;
  8003. size_t l = 0;
  8004. /* format is Wed, 09 Jun 2021 10:18:14 GMT */
  8005. #if defined(HAVE_GMTIME_R)
  8006. if (gmtime_r(&n, pT) != SOAP_FUNC_R_ERR)
  8007. l = strftime(s, 4096 - (s-tmp), ";Expires=%a, %d %b %Y %H:%M:%S GMT", pT);
  8008. #else
  8009. pT = gmtime(&n);
  8010. if (pT)
  8011. l = strftime(s, 4096 - (s-tmp), ";Expires=%a, %d %b %Y %H:%M:%S GMT", pT);
  8012. #endif
  8013. s += l;
  8014. }
  8015. #endif
  8016. #endif
  8017. if (s-tmp < 4079
  8018. && (p->secure
  8019. #ifdef WITH_OPENSSL
  8020. || soap->ssl
  8021. #endif
  8022. ))
  8023. {
  8024. soap_strcpy(s, 4096 - (s-tmp), ";Secure");
  8025. s += strlen(s);
  8026. }
  8027. if (s-tmp < 4086)
  8028. soap_strcpy(s, 4096 - (s-tmp), ";HttpOnly");
  8029. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Set-Cookie: %s\n", tmp));
  8030. soap->error = soap->fposthdr(soap, "Set-Cookie", tmp);
  8031. if (soap->error)
  8032. return soap->error;
  8033. }
  8034. }
  8035. return SOAP_OK;
  8036. }
  8037. /******************************************************************************/
  8038. SOAP_FMAC1
  8039. int
  8040. SOAP_FMAC2
  8041. soap_putcookies(struct soap *soap, const char *domain, const char *path, int secure)
  8042. {
  8043. struct soap_cookie **p, *q;
  8044. char *s, tmp[4096];
  8045. unsigned int version = 0;
  8046. time_t now = time(NULL);
  8047. if (!domain || !path)
  8048. return SOAP_OK;
  8049. s = tmp;
  8050. p = &soap->cookies;
  8051. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Sending cookies for domain='%s' path='%s'\n", domain, path));
  8052. if (*path == '/')
  8053. path++;
  8054. while ((q = *p))
  8055. {
  8056. if (q->expire && now >= (time_t)q->expire)
  8057. {
  8058. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Cookie %s expired\n", q->name));
  8059. SOAP_FREE(soap, q->name);
  8060. if (q->value)
  8061. SOAP_FREE(soap, q->value);
  8062. if (q->domain)
  8063. SOAP_FREE(soap, q->domain);
  8064. if (q->path)
  8065. SOAP_FREE(soap, q->path);
  8066. *p = q->next;
  8067. SOAP_FREE(soap, q);
  8068. }
  8069. else
  8070. {
  8071. int flag;
  8072. char *t = q->domain;
  8073. size_t n = 0;
  8074. if (!t)
  8075. flag = 1;
  8076. else
  8077. {
  8078. const char *r = strchr(t, ':');
  8079. if (r)
  8080. n = r - t;
  8081. else
  8082. n = strlen(t);
  8083. flag = !strncmp(t, domain, n);
  8084. }
  8085. /* domain-level cookies, cannot compile when WITH_NOIO set */
  8086. #ifndef WITH_NOIO
  8087. if (!flag)
  8088. {
  8089. struct hostent hostent;
  8090. if (!tcp_gethostbyname(soap, (char*)domain, &hostent, NULL))
  8091. {
  8092. const char *r = hostent.h_name;
  8093. if (*t == '.')
  8094. {
  8095. size_t k = strlen(hostent.h_name);
  8096. if (k >= n)
  8097. r = hostent.h_name + k - n;
  8098. }
  8099. flag = !strncmp(t, r, n);
  8100. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Domain cookie %s host %s (match=%d)\n", t, r, flag));
  8101. }
  8102. }
  8103. #endif
  8104. if (flag
  8105. && (!q->path || !strncmp(q->path, path, strlen(q->path)))
  8106. #ifndef WITH_INSECURE_COOKIES
  8107. && (!q->secure || secure)
  8108. #endif
  8109. )
  8110. {
  8111. size_t n = 12;
  8112. if (q->name)
  8113. n += 3*strlen(q->name);
  8114. if (q->value && *q->value)
  8115. n += 3*strlen(q->value) + 1;
  8116. if (q->path && *q->path)
  8117. n += strlen(q->path) + 9;
  8118. if (q->domain)
  8119. n += strlen(q->domain) + 11;
  8120. if (s + n >= tmp + sizeof(tmp))
  8121. {
  8122. if (s == tmp)
  8123. return SOAP_OK; /* header too big, cannot split */
  8124. /* split up HTTP header */
  8125. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Cookie: %s\n", tmp));
  8126. soap->error = soap->fposthdr(soap, "Cookie", tmp);
  8127. if (soap->error)
  8128. return soap->error;
  8129. s = tmp;
  8130. }
  8131. else if (s != tmp)
  8132. {
  8133. *s++ = ';';
  8134. }
  8135. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Sending cookie %s=%s path=\"/%s\" domain=\"%s\"\n", q->name ? q->name : "(null)", q->value ? q->value : "(null)", q->path ? q->path : "(null)", q->domain ? q->domain : "(null)"));
  8136. if (q->version != version && (s-tmp) + (size_t)36 < sizeof(tmp))
  8137. {
  8138. (SOAP_SNPRINTF_SAFE(s, sizeof(tmp) - (s-tmp)), "$Version=%u;", q->version);
  8139. version = q->version;
  8140. s += strlen(s);
  8141. }
  8142. if (q->name && (s-tmp) + strlen(q->name) + (size_t)15 < sizeof(tmp))
  8143. {
  8144. s += soap_encode_url(q->name, s, (int)(tmp+sizeof(tmp)-s)-15);
  8145. }
  8146. if (q->value && *q->value && (s-tmp) + strlen(q->value) + (size_t)16 < sizeof(tmp))
  8147. {
  8148. *s++ = '=';
  8149. s += soap_encode_url(q->value, s, (int)(tmp+sizeof(tmp)-s)-16);
  8150. }
  8151. if (q->path && (s-tmp) + strlen(q->path) + (size_t)36 < sizeof(tmp))
  8152. {
  8153. (SOAP_SNPRINTF_SAFE(s, sizeof(tmp) - (s-tmp)), ";$Path=\"/%s\"", (*q->path == '/' ? q->path + 1 : q->path));
  8154. s += strlen(s);
  8155. }
  8156. if (q->domain && (s-tmp) + strlen(q->domain) + (size_t)36 < sizeof(tmp))
  8157. {
  8158. (SOAP_SNPRINTF_SAFE(s, sizeof(tmp) - (s-tmp)), ";$Domain=\"%s\"", q->domain);
  8159. s += strlen(s);
  8160. }
  8161. }
  8162. p = &q->next;
  8163. }
  8164. }
  8165. if (s != tmp)
  8166. {
  8167. soap->error = soap->fposthdr(soap, "Cookie", tmp);
  8168. if (soap->error)
  8169. return soap->error;
  8170. }
  8171. return SOAP_OK;
  8172. }
  8173. /******************************************************************************/
  8174. SOAP_FMAC1
  8175. void
  8176. SOAP_FMAC2
  8177. soap_getcookies(struct soap *soap, const char *val)
  8178. {
  8179. struct soap_cookie *p = NULL, *q;
  8180. const char *s;
  8181. char *t, tmp[4096]; /* cookie size is up to 4096 bytes [RFC2109] */
  8182. char *domain = NULL;
  8183. char *path = NULL;
  8184. unsigned int version = 0;
  8185. time_t now = time(NULL);
  8186. if (!val)
  8187. return;
  8188. s = val;
  8189. while (*s)
  8190. {
  8191. s = soap_decode_key(tmp, sizeof(tmp), s);
  8192. if (!soap_tag_cmp(tmp, "$Version"))
  8193. {
  8194. s = soap_decode_val(tmp, sizeof(tmp), s);
  8195. if (s)
  8196. {
  8197. if (p)
  8198. p->version = (int)soap_strtol(tmp, NULL, 10);
  8199. else
  8200. version = (int)soap_strtol(tmp, NULL, 10);
  8201. }
  8202. }
  8203. else if (!soap_tag_cmp(tmp, "$Path"))
  8204. {
  8205. s = soap_decode_val(tmp, sizeof(tmp), s);
  8206. if (*tmp)
  8207. {
  8208. size_t l = strlen(tmp) + 1;
  8209. t = NULL;
  8210. if (SOAP_MAXALLOCSIZE <= 0 || l <= SOAP_MAXALLOCSIZE)
  8211. t = (char*)SOAP_MALLOC(soap, l);
  8212. if (t)
  8213. (void)soap_memcpy((void*)t, l, (const void*)tmp, l);
  8214. }
  8215. else
  8216. {
  8217. t = NULL;
  8218. }
  8219. if (p)
  8220. {
  8221. if (p->path)
  8222. SOAP_FREE(soap, p->path);
  8223. p->path = t;
  8224. }
  8225. else
  8226. {
  8227. if (path)
  8228. SOAP_FREE(soap, path);
  8229. path = t;
  8230. }
  8231. }
  8232. else if (!soap_tag_cmp(tmp, "$Domain"))
  8233. {
  8234. s = soap_decode_val(tmp, sizeof(tmp), s);
  8235. if (*tmp)
  8236. {
  8237. size_t l = strlen(tmp) + 1;
  8238. t = NULL;
  8239. if (SOAP_MAXALLOCSIZE <= 0 || l <= SOAP_MAXALLOCSIZE)
  8240. t = (char*)SOAP_MALLOC(soap, l);
  8241. if (t)
  8242. (void)soap_memcpy((void*)t, l, (const void*)tmp, l);
  8243. }
  8244. else
  8245. {
  8246. t = NULL;
  8247. }
  8248. if (p)
  8249. {
  8250. if (p->domain)
  8251. SOAP_FREE(soap, p->domain);
  8252. p->domain = t;
  8253. }
  8254. else
  8255. {
  8256. if (domain)
  8257. SOAP_FREE(soap, domain);
  8258. domain = t;
  8259. }
  8260. }
  8261. else if (p && !soap_tag_cmp(tmp, "Path"))
  8262. {
  8263. if (p->path)
  8264. SOAP_FREE(soap, p->path);
  8265. s = soap_decode_val(tmp, sizeof(tmp), s);
  8266. if (*tmp)
  8267. {
  8268. size_t l = strlen(tmp) + 1;
  8269. p->path = NULL;
  8270. if (SOAP_MAXALLOCSIZE <= 0 || l <= SOAP_MAXALLOCSIZE)
  8271. p->path = (char*)SOAP_MALLOC(soap, l);
  8272. if (p->path)
  8273. (void)soap_memcpy((void*)p->path, l, (const void*)tmp, l);
  8274. }
  8275. else
  8276. {
  8277. p->path = NULL;
  8278. }
  8279. }
  8280. else if (p && !soap_tag_cmp(tmp, "Domain"))
  8281. {
  8282. if (p->domain)
  8283. SOAP_FREE(soap, p->domain);
  8284. s = soap_decode_val(tmp, sizeof(tmp), s);
  8285. if (*tmp)
  8286. {
  8287. size_t l = strlen(tmp) + 1;
  8288. p->domain = NULL;
  8289. if (SOAP_MAXALLOCSIZE <= 0 || l <= SOAP_MAXALLOCSIZE)
  8290. p->domain = (char*)SOAP_MALLOC(soap, l);
  8291. if (p->domain)
  8292. (void)soap_memcpy((void*)p->domain, l, (const void*)tmp, l);
  8293. }
  8294. else
  8295. {
  8296. p->domain = NULL;
  8297. }
  8298. }
  8299. else if (p && !soap_tag_cmp(tmp, "Version"))
  8300. {
  8301. s = soap_decode_val(tmp, sizeof(tmp), s);
  8302. p->version = (unsigned int)soap_strtoul(tmp, NULL, 10);
  8303. }
  8304. else if (p && !soap_tag_cmp(tmp, "Max-Age"))
  8305. {
  8306. s = soap_decode_val(tmp, sizeof(tmp), s);
  8307. p->expire = (ULONG64)(now + soap_strtol(tmp, NULL, 10));
  8308. }
  8309. else if (p && !soap_tag_cmp(tmp, "Expires"))
  8310. {
  8311. if (*s == '=')
  8312. {
  8313. s = soap_decode(tmp, sizeof(tmp), s + 1, ";");
  8314. if (!p->expire && strlen(tmp) >= 23)
  8315. {
  8316. char a[3];
  8317. struct tm T;
  8318. static const char mns[] = "anebarprayunulugepctovec";
  8319. const char *t = strchr(tmp, ' ');
  8320. if (t)
  8321. {
  8322. a[2] = '\0';
  8323. memset((void*)&T, 0, sizeof(T));
  8324. if (t[1] >= 'A')
  8325. {
  8326. /* format is Sun Nov 6 08:49:37 94 */
  8327. a[0] = t[2];
  8328. a[1] = t[3];
  8329. T.tm_mon = (int)(strstr(mns, a) - mns) / 2;
  8330. a[0] = t[5];
  8331. a[1] = t[6];
  8332. T.tm_mday = (int)soap_strtol(a, NULL, 10);
  8333. if (t[17] && t[18] && t[19] != ' ')
  8334. t += 2; /* format is Sun Nov 6 08:49:37 2017 - ANSI-C */
  8335. a[0] = t[17];
  8336. a[1] = t[18];
  8337. T.tm_year = 100 + (int)soap_strtol(a, NULL, 10);
  8338. t += 6;
  8339. }
  8340. else
  8341. {
  8342. /* format is Sunday, 06-Nov-17 08:49:37 GMT - RFC 850 */
  8343. a[0] = t[1];
  8344. a[1] = t[2];
  8345. T.tm_mday = (int)soap_strtol(a, NULL, 10);
  8346. a[0] = t[5];
  8347. a[1] = t[6];
  8348. T.tm_mon = (int)(strstr(mns, a) - mns) / 2;
  8349. if (t[10] != ' ')
  8350. t += 2; /* format is Wed, 09 Jun 2021 10:18:14 GMT - RFC 822 */
  8351. a[0] = t[8];
  8352. a[1] = t[9];
  8353. T.tm_year = 100 + (int)soap_strtol(a, NULL, 10);
  8354. t += 11;
  8355. }
  8356. a[0] = t[0];
  8357. a[1] = t[1];
  8358. T.tm_hour = (int)soap_strtol(a, NULL, 10);
  8359. a[0] = t[3];
  8360. a[1] = t[4];
  8361. T.tm_min = (int)soap_strtol(a, NULL, 10);
  8362. a[0] = t[6];
  8363. a[1] = t[7];
  8364. T.tm_sec = (int)soap_strtol(a, NULL, 10);
  8365. p->expire = (ULONG64)soap_timegm(&T);
  8366. }
  8367. }
  8368. }
  8369. }
  8370. else if (p && !soap_tag_cmp(tmp, "Secure"))
  8371. {
  8372. p->secure = 1;
  8373. s = soap_decode_val(tmp, sizeof(tmp), s);
  8374. }
  8375. else if (p && !soap_tag_cmp(tmp, "HttpOnly"))
  8376. {
  8377. s = soap_decode_val(tmp, sizeof(tmp), s);
  8378. }
  8379. else if (p && !soap_tag_cmp(tmp, "Comment"))
  8380. {
  8381. s = soap_decode_val(tmp, sizeof(tmp), s);
  8382. }
  8383. else if (*tmp)
  8384. {
  8385. if (p)
  8386. {
  8387. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Got environment cookie='%s' value='%s' domain='%s' path='%s' expire=" SOAP_ULONG_FORMAT " secure=%d\n", p->name, p->value ? p->value : "(null)", p->domain ? p->domain : "(null)", p->path ? p->path : "(null)", p->expire, p->secure));
  8388. q = soap_set_cookie(soap, p->name, p->value, p->domain, p->path);
  8389. if (q)
  8390. {
  8391. q->version = p->version;
  8392. q->expire = p->expire;
  8393. q->secure = p->secure;
  8394. q->env = 1;
  8395. }
  8396. if (p->name)
  8397. SOAP_FREE(soap, p->name);
  8398. if (p->value)
  8399. SOAP_FREE(soap, p->value);
  8400. if (p->domain)
  8401. SOAP_FREE(soap, p->domain);
  8402. if (p->path)
  8403. SOAP_FREE(soap, p->path);
  8404. SOAP_FREE(soap, p);
  8405. }
  8406. p = (struct soap_cookie*)SOAP_MALLOC(soap, sizeof(struct soap_cookie));
  8407. if (p)
  8408. {
  8409. size_t l = strlen(tmp) + 1;
  8410. p->name = NULL;
  8411. if (SOAP_MAXALLOCSIZE <= 0 || l <= SOAP_MAXALLOCSIZE)
  8412. p->name = (char*)SOAP_MALLOC(soap, l);
  8413. if (p->name)
  8414. (void)soap_memcpy(p->name, l, tmp, l);
  8415. s = soap_decode_val(tmp, sizeof(tmp), s);
  8416. if (*tmp)
  8417. {
  8418. l = strlen(tmp) + 1;
  8419. p->value = NULL;
  8420. if (SOAP_MAXALLOCSIZE <= 0 || l <= SOAP_MAXALLOCSIZE)
  8421. p->value = (char*)SOAP_MALLOC(soap, l);
  8422. if (p->value)
  8423. (void)soap_memcpy((void*)p->value, l, (const void*)tmp, l);
  8424. }
  8425. else
  8426. {
  8427. p->value = NULL;
  8428. }
  8429. if (domain)
  8430. {
  8431. p->domain = domain;
  8432. }
  8433. else
  8434. {
  8435. p->domain = NULL;
  8436. }
  8437. if (path)
  8438. {
  8439. p->path = path;
  8440. }
  8441. else if (*soap->path)
  8442. {
  8443. l = strlen(soap->path) + 1;
  8444. p->path = NULL;
  8445. if (SOAP_MAXALLOCSIZE <= 0 || l <= SOAP_MAXALLOCSIZE)
  8446. p->path = (char*)SOAP_MALLOC(soap, l);
  8447. if (p->path)
  8448. (void)soap_memcpy((void*)p->path, l, (const void*)soap->path, l);
  8449. }
  8450. else
  8451. {
  8452. p->path = (char*)SOAP_MALLOC(soap, 2);
  8453. if (p->path)
  8454. (void)soap_memcpy((void*)p->path, 2, (const void*)"/", 2);
  8455. }
  8456. p->expire = 0;
  8457. p->secure = 0;
  8458. p->version = version;
  8459. }
  8460. }
  8461. }
  8462. if (p)
  8463. {
  8464. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Got environment cookie='%s' value='%s' domain='%s' path='%s' expire=" SOAP_ULONG_FORMAT " secure=%d\n", p->name, p->value ? p->value : "(null)", p->domain ? p->domain : "(null)", p->path ? p->path : "(null)", p->expire, p->secure));
  8465. q = soap_set_cookie(soap, p->name, p->value, p->domain, p->path);
  8466. if (q)
  8467. {
  8468. q->version = p->version;
  8469. q->expire = p->expire;
  8470. q->secure = p->secure;
  8471. q->env = 1;
  8472. }
  8473. if (p->name)
  8474. SOAP_FREE(soap, p->name);
  8475. if (p->value)
  8476. SOAP_FREE(soap, p->value);
  8477. if (p->domain)
  8478. SOAP_FREE(soap, p->domain);
  8479. if (p->path)
  8480. SOAP_FREE(soap, p->path);
  8481. SOAP_FREE(soap, p);
  8482. }
  8483. if (domain)
  8484. SOAP_FREE(soap, domain);
  8485. if (path)
  8486. SOAP_FREE(soap, path);
  8487. }
  8488. /******************************************************************************/
  8489. SOAP_FMAC1
  8490. int
  8491. SOAP_FMAC2
  8492. soap_getenv_cookies(struct soap *soap)
  8493. {
  8494. struct soap_cookie *p;
  8495. const char *s;
  8496. char key[4096], val[4096]; /* cookie size is up to 4096 bytes [RFC2109] */
  8497. s = getenv("HTTP_COOKIE");
  8498. if (!s)
  8499. return SOAP_ERR;
  8500. do
  8501. {
  8502. s = soap_decode_key(key, sizeof(key), s);
  8503. s = soap_decode_val(val, sizeof(val), s);
  8504. p = soap_set_cookie(soap, key, val, NULL, NULL);
  8505. if (p)
  8506. p->env = 1;
  8507. } while (*s);
  8508. return SOAP_OK;
  8509. }
  8510. /******************************************************************************/
  8511. SOAP_FMAC1
  8512. struct soap_cookie*
  8513. SOAP_FMAC2
  8514. soap_copy_cookies(struct soap *copy, const struct soap *soap)
  8515. {
  8516. struct soap_cookie *p, **q, *r;
  8517. (void)copy;
  8518. q = &r;
  8519. for (p = soap->cookies; p; p = p->next)
  8520. {
  8521. *q = (struct soap_cookie*)SOAP_MALLOC(copy, sizeof(struct soap_cookie));
  8522. if (!*q)
  8523. return r;
  8524. **q = *p;
  8525. if (p->name)
  8526. {
  8527. size_t l = strlen(p->name) + 1;
  8528. (*q)->name = NULL;
  8529. if (SOAP_MAXALLOCSIZE <= 0 || l <= SOAP_MAXALLOCSIZE)
  8530. (*q)->name = (char*)SOAP_MALLOC(copy, l);
  8531. if ((*q)->name)
  8532. (void)soap_memcpy((*q)->name, l, p->name, l);
  8533. }
  8534. if (p->value)
  8535. {
  8536. size_t l = strlen(p->value) + 1;
  8537. (*q)->value = NULL;
  8538. if (SOAP_MAXALLOCSIZE <= 0 || l <= SOAP_MAXALLOCSIZE)
  8539. (*q)->value = (char*)SOAP_MALLOC(copy, l);
  8540. if ((*q)->value)
  8541. (void)soap_memcpy((*q)->value, l, p->value, l);
  8542. }
  8543. if (p->domain)
  8544. {
  8545. size_t l = strlen(p->domain) + 1;
  8546. (*q)->domain = NULL;
  8547. if (SOAP_MAXALLOCSIZE <= 0 || l <= SOAP_MAXALLOCSIZE)
  8548. (*q)->domain = (char*)SOAP_MALLOC(copy, l);
  8549. if ((*q)->domain)
  8550. (void)soap_memcpy((*q)->domain, l, p->domain, l);
  8551. }
  8552. if (p->path)
  8553. {
  8554. size_t l = strlen(p->path) + 1;
  8555. (*q)->path = NULL;
  8556. if (SOAP_MAXALLOCSIZE <= 0 || l <= SOAP_MAXALLOCSIZE)
  8557. (*q)->path = (char*)SOAP_MALLOC(copy, l);
  8558. if ((*q)->path)
  8559. (void)soap_memcpy((*q)->path, l, p->path, l);
  8560. }
  8561. q = &(*q)->next;
  8562. }
  8563. *q = NULL;
  8564. return r;
  8565. }
  8566. /******************************************************************************/
  8567. SOAP_FMAC1
  8568. void
  8569. SOAP_FMAC2
  8570. soap_free_cookies(struct soap *soap)
  8571. {
  8572. struct soap_cookie *p;
  8573. for (p = soap->cookies; p; p = soap->cookies)
  8574. {
  8575. soap->cookies = p->next;
  8576. SOAP_FREE(soap, p->name);
  8577. if (p->value)
  8578. SOAP_FREE(soap, p->value);
  8579. if (p->domain)
  8580. SOAP_FREE(soap, p->domain);
  8581. if (p->path)
  8582. SOAP_FREE(soap, p->path);
  8583. SOAP_FREE(soap, p);
  8584. }
  8585. }
  8586. /******************************************************************************/
  8587. #endif /* WITH_COOKIES */
  8588. /******************************************************************************/
  8589. SOAP_FMAC1
  8590. size_t
  8591. SOAP_FMAC2
  8592. soap_hash(const char *s) {
  8593. size_t h = 0;
  8594. while (*s)
  8595. h = 65599 * h + *s++;
  8596. return h % SOAP_IDHASH;
  8597. }
  8598. /******************************************************************************/
  8599. static void
  8600. soap_init_pht(struct soap *soap) {
  8601. int i;
  8602. soap->pblk = NULL;
  8603. soap->pidx = 0;
  8604. for (i = 0; i < (int) SOAP_PTRHASH; i++)
  8605. soap->pht[i] = NULL;
  8606. }
  8607. /******************************************************************************/
  8608. SOAP_FMAC1
  8609. struct soap *
  8610. SOAP_FMAC2
  8611. soap_versioning(soap_new)(soap_mode imode, soap_mode omode) {
  8612. struct soap *soap;
  8613. #ifdef __cplusplus
  8614. soap = SOAP_NEW_UNMANAGED(struct soap);
  8615. #else
  8616. soap = (struct soap*)SOAP_MALLOC_UNMANAGED(sizeof(struct soap));
  8617. #endif
  8618. if (soap)
  8619. soap_versioning(soap_init)(soap, imode, omode);
  8620. return soap;
  8621. }
  8622. /******************************************************************************/
  8623. SOAP_FMAC1
  8624. void
  8625. SOAP_FMAC2
  8626. soap_free(struct soap *soap) {
  8627. soap_done(soap);
  8628. #ifdef __cplusplus
  8629. SOAP_DELETE_UNMANAGED(soap);
  8630. #else
  8631. SOAP_FREE_UNMANAGED(soap);
  8632. #endif
  8633. }
  8634. /******************************************************************************/
  8635. SOAP_FMAC1
  8636. void
  8637. SOAP_FMAC2
  8638. soap_del(struct soap *soap) {
  8639. free(soap);
  8640. }
  8641. /******************************************************************************/
  8642. static void
  8643. soap_free_pht(struct soap *soap) {
  8644. struct soap_pblk *pb, *next;
  8645. int i;
  8646. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Free pointer hashtable\n"));
  8647. for (pb = soap->pblk; pb; pb = next) {
  8648. next = pb->next;
  8649. SOAP_FREE(soap, pb);
  8650. }
  8651. soap->pblk = NULL;
  8652. soap->pidx = 0;
  8653. for (i = 0; i < (int) SOAP_PTRHASH; i++)
  8654. soap->pht[i] = NULL;
  8655. }
  8656. /******************************************************************************/
  8657. #ifndef WITH_NOIDREF
  8658. SOAP_FMAC1
  8659. int
  8660. SOAP_FMAC2
  8661. soap_embed(struct soap *soap, const void *p, const void *a, int n, int t) {
  8662. int id;
  8663. struct soap_plist *pp;
  8664. if (soap->version == 2)
  8665. soap->encoding = 1;
  8666. if (!p || (!soap->encodingStyle && !(soap->mode & SOAP_XML_GRAPH)) || (soap->mode & SOAP_XML_TREE))
  8667. return 0;
  8668. if (a)
  8669. id = soap_array_pointer_lookup(soap, p, a, n, t, &pp);
  8670. else
  8671. id = soap_pointer_lookup(soap, p, t, &pp);
  8672. if (id) {
  8673. if (soap_is_embedded(soap, pp)
  8674. || soap_is_single(soap, pp))
  8675. return 0;
  8676. soap_set_embedded(soap, pp);
  8677. }
  8678. return id;
  8679. }
  8680. #endif
  8681. /******************************************************************************/
  8682. SOAP_FMAC1
  8683. int
  8684. SOAP_FMAC2
  8685. soap_pointer_lookup(struct soap *soap, const void *p, int type, struct soap_plist **ppp) {
  8686. struct soap_plist *pp;
  8687. *ppp = NULL;
  8688. if (p) {
  8689. for (pp = soap->pht[soap_hash_ptr(p)]; pp; pp = pp->next) {
  8690. if (pp->ptr == p && pp->type == type) {
  8691. *ppp = pp;
  8692. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Lookup location=%p type=%d id=%d\n", p, type, pp->id));
  8693. return pp->id;
  8694. }
  8695. }
  8696. }
  8697. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Lookup location=%p type=%d: not found\n", p, type));
  8698. return 0;
  8699. }
  8700. /******************************************************************************/
  8701. SOAP_FMAC1
  8702. int
  8703. SOAP_FMAC2
  8704. soap_pointer_enter(struct soap *soap, const void *p, const void *a, int n, int type, struct soap_plist **ppp) {
  8705. size_t h;
  8706. struct soap_plist *pp;
  8707. (void) n;
  8708. if (!soap->pblk || soap->pidx >= SOAP_PTRBLK) {
  8709. struct soap_pblk *pb = (struct soap_pblk *) SOAP_MALLOC(soap, sizeof(struct soap_pblk));
  8710. if (!pb) {
  8711. soap->error = SOAP_EOM;
  8712. return 0;
  8713. }
  8714. pb->next = soap->pblk;
  8715. soap->pblk = pb;
  8716. soap->pidx = 0;
  8717. }
  8718. *ppp = pp = &soap->pblk->plist[soap->pidx++];
  8719. if (a)
  8720. h = soap_hash_ptr(a);
  8721. else
  8722. h = soap_hash_ptr(p);
  8723. DBGLOG(TEST,
  8724. SOAP_MESSAGE(fdebug, "Pointer enter location=%p array=%p size=%lu type=%d id=%d\n", p, a, (unsigned long) n,
  8725. type, soap->idnum + 1));
  8726. pp->next = soap->pht[h];
  8727. pp->type = type;
  8728. pp->mark1 = 0;
  8729. pp->mark2 = 0;
  8730. pp->ptr = p;
  8731. pp->dup = NULL;
  8732. pp->array = a;
  8733. pp->size = n;
  8734. soap->pht[h] = pp;
  8735. pp->id = ++soap->idnum;
  8736. return pp->id;
  8737. }
  8738. /******************************************************************************/
  8739. SOAP_FMAC1
  8740. int
  8741. SOAP_FMAC2
  8742. soap_array_pointer_lookup(struct soap *soap, const void *p, const void *a, int n, int type, struct soap_plist **ppp) {
  8743. struct soap_plist *pp;
  8744. *ppp = NULL;
  8745. if (!p || !a)
  8746. return 0;
  8747. for (pp = soap->pht[soap_hash_ptr(a)]; pp; pp = pp->next) {
  8748. if (pp->type == type && pp->array == a && pp->size == n) {
  8749. *ppp = pp;
  8750. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Array lookup location=%p type=%d id=%d\n", a, type, pp->id));
  8751. return pp->id;
  8752. }
  8753. }
  8754. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Array lookup location=%p type=%d: not found\n", a, type));
  8755. return 0;
  8756. }
  8757. /******************************************************************************/
  8758. SOAP_FMAC1
  8759. int
  8760. SOAP_FMAC2
  8761. soap_begin_count(struct soap *soap) {
  8762. soap_free_ns(soap);
  8763. soap->error = SOAP_OK;
  8764. #ifndef WITH_LEANER
  8765. if ((soap->mode & SOAP_ENC_DIME) || (soap->omode & SOAP_ENC_DIME)) {
  8766. soap->mode = soap->omode | SOAP_IO_LENGTH | SOAP_ENC_DIME;
  8767. } else
  8768. #endif
  8769. {
  8770. soap->mode = soap->omode;
  8771. if ((soap->mode & SOAP_IO_UDP)) {
  8772. soap->mode &= SOAP_IO;
  8773. soap->mode |= SOAP_IO_BUFFER | SOAP_ENC_PLAIN;
  8774. }
  8775. if ((soap->mode & SOAP_IO) == SOAP_IO_STORE
  8776. || (((soap->mode & SOAP_IO) == SOAP_IO_CHUNK || (soap->mode & SOAP_ENC_PLAIN))
  8777. #ifndef WITH_LEANER
  8778. && !soap->fpreparesend
  8779. #endif
  8780. ))
  8781. soap->mode &= ~SOAP_IO_LENGTH;
  8782. else
  8783. soap->mode |= SOAP_IO_LENGTH;
  8784. }
  8785. #ifdef WITH_ZLIB
  8786. if ((soap->mode & SOAP_ENC_ZLIB) && (soap->mode & SOAP_IO) == SOAP_IO_FLUSH)
  8787. {
  8788. if (!(soap->mode & SOAP_ENC_DIME))
  8789. soap->mode &= ~SOAP_IO_LENGTH;
  8790. if ((soap->mode & SOAP_ENC_PLAIN))
  8791. soap->mode |= SOAP_IO_BUFFER;
  8792. else
  8793. soap->mode |= SOAP_IO_STORE;
  8794. }
  8795. #endif
  8796. #ifndef WITH_LEANER
  8797. if ((soap->mode & SOAP_ENC_MTOM) && (soap->mode & SOAP_ENC_DIME))
  8798. soap->mode |= SOAP_ENC_MIME;
  8799. else if (!(soap->mode & SOAP_ENC_MIME))
  8800. soap->mode &= ~SOAP_ENC_MTOM;
  8801. if ((soap->mode & SOAP_ENC_MIME))
  8802. soap_select_mime_boundary(soap);
  8803. soap->dime.list = soap->dime.last; /* keep track of last DIME attachment */
  8804. #endif
  8805. soap->count = 0;
  8806. soap->ns = 0;
  8807. soap->null = 0;
  8808. soap->position = 0;
  8809. soap->mustUnderstand = 0;
  8810. soap->encoding = 0;
  8811. soap->part = SOAP_BEGIN_SEND;
  8812. soap->event = 0;
  8813. soap->evlev = 0;
  8814. soap->idnum = 0;
  8815. soap->body = 1;
  8816. soap->level = 0;
  8817. soap_clr_attr(soap);
  8818. soap_set_local_namespaces(soap);
  8819. #ifndef WITH_LEANER
  8820. soap->dime.size = 0; /* accumulate total size of attachments */
  8821. if (soap->fprepareinitsend && (soap->mode & SOAP_IO) != SOAP_IO_STORE &&
  8822. (soap->error = soap->fprepareinitsend(soap)) != SOAP_OK)
  8823. return soap->error;
  8824. #endif
  8825. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Begin count phase (socket=%d mode=0x%x count="
  8826. SOAP_ULONG_FORMAT
  8827. ")\n", (int) soap->socket, (unsigned int) soap->mode, soap->count));
  8828. #ifndef WITH_LEANER
  8829. if ((soap->mode & SOAP_IO_LENGTH))
  8830. return soap_begin_attachments(soap);
  8831. #endif
  8832. return SOAP_OK;
  8833. }
  8834. /******************************************************************************/
  8835. SOAP_FMAC1
  8836. int
  8837. SOAP_FMAC2
  8838. soap_end_count(struct soap *soap) {
  8839. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "End of count phase\n"));
  8840. #ifndef WITH_LEANER
  8841. if ((soap->mode & SOAP_IO_LENGTH)) {
  8842. if (soap_end_attachments(soap))
  8843. return soap->error;
  8844. if (soap->fpreparefinalsend && (soap->error = soap->fpreparefinalsend(soap)) != SOAP_OK)
  8845. return soap->error;
  8846. }
  8847. #else
  8848. (void)soap;
  8849. #endif
  8850. return SOAP_OK;
  8851. }
  8852. /******************************************************************************/
  8853. static int
  8854. soap_init_send(struct soap *soap) {
  8855. DBGLOG(TEST,
  8856. SOAP_MESSAGE(fdebug, "Initializing for output to socket=%d/fd=%d\n", (int) soap->socket, soap->sendfd));
  8857. *soap->tag = '\0';
  8858. soap_free_ns(soap);
  8859. soap->error = SOAP_OK;
  8860. soap->mode = soap->omode | (soap->mode & (SOAP_IO_LENGTH | SOAP_ENC_DIME));
  8861. #ifndef WITH_LEAN
  8862. if ((soap->mode & SOAP_IO_UDP)) {
  8863. soap->mode &= ~SOAP_IO;
  8864. soap->mode |= SOAP_IO_BUFFER | SOAP_ENC_PLAIN;
  8865. if ((soap->mode & SOAP_IO_LENGTH) && soap->count > sizeof(soap->buf))
  8866. return soap->error = SOAP_UDP_ERROR;
  8867. }
  8868. #endif
  8869. #ifdef WITH_ZLIB
  8870. if ((soap->mode & SOAP_ENC_ZLIB) && (soap->mode & SOAP_IO) == SOAP_IO_FLUSH)
  8871. {
  8872. if ((soap->mode & SOAP_ENC_PLAIN))
  8873. soap->mode |= SOAP_IO_BUFFER;
  8874. else
  8875. soap->mode |= SOAP_IO_STORE;
  8876. }
  8877. #endif
  8878. #if !defined(__cplusplus) || defined(WITH_COMPAT)
  8879. if (soap->os)
  8880. {
  8881. *soap->os = NULL;
  8882. soap->mode = (soap->mode & ~SOAP_IO) | SOAP_IO_STORE;
  8883. }
  8884. else
  8885. #endif
  8886. if ((soap->mode & SOAP_IO) == SOAP_IO_FLUSH && soap_valid_socket(soap->socket)) {
  8887. if ((soap->mode & SOAP_IO_LENGTH) || (soap->mode & SOAP_ENC_PLAIN))
  8888. soap->mode |= SOAP_IO_BUFFER;
  8889. else
  8890. soap->mode |= SOAP_IO_STORE;
  8891. }
  8892. soap->mode &= ~SOAP_IO_LENGTH;
  8893. if ((soap->mode & SOAP_IO) == SOAP_IO_STORE && soap_alloc_block(soap) == NULL)
  8894. return soap->error;
  8895. if (!(soap->mode & SOAP_IO_KEEPALIVE))
  8896. soap->keep_alive = 0;
  8897. #ifndef WITH_LEANER
  8898. if ((soap->mode & SOAP_ENC_MTOM) && (soap->mode & SOAP_ENC_DIME)) {
  8899. soap->mode |= SOAP_ENC_MIME;
  8900. soap->mode &= ~SOAP_ENC_DIME;
  8901. } else if (!(soap->mode & SOAP_ENC_MIME)) {
  8902. soap->mode &= ~SOAP_ENC_MTOM;
  8903. }
  8904. if ((soap->mode & SOAP_ENC_MIME))
  8905. soap_select_mime_boundary(soap);
  8906. #ifdef WIN32
  8907. #ifndef UNDER_CE
  8908. #ifndef WITH_FASTCGI
  8909. if (!soap_valid_socket(soap->socket) && !soap->os && soap->sendfd >= 0) /* Set win32 stdout or soap->sendfd to BINARY, e.g. to support DIME */
  8910. #ifdef __BORLANDC__
  8911. setmode(soap->sendfd, _O_BINARY);
  8912. #else
  8913. _setmode(soap->sendfd, _O_BINARY);
  8914. #endif
  8915. #endif
  8916. #endif
  8917. #endif
  8918. #endif
  8919. if ((soap->mode & SOAP_IO))
  8920. soap->buflen = soap->bufidx = 0;
  8921. soap->chunksize = 0;
  8922. soap->ns = 0;
  8923. soap->null = 0;
  8924. soap->position = 0;
  8925. soap->mustUnderstand = 0;
  8926. soap->encoding = 0;
  8927. soap->event = 0;
  8928. soap->evlev = 0;
  8929. soap->idnum = 0;
  8930. soap->body = 1;
  8931. soap->level = 0;
  8932. soap_clr_attr(soap);
  8933. soap_set_local_namespaces(soap);
  8934. #ifdef WITH_ZLIB
  8935. soap->z_ratio_out = 1.0;
  8936. if ((soap->mode & SOAP_ENC_ZLIB) && soap->zlib_state != SOAP_ZLIB_DEFLATE)
  8937. {
  8938. if (!soap->d_stream)
  8939. {
  8940. soap->d_stream = (z_stream*)SOAP_MALLOC(soap, sizeof(z_stream));
  8941. if (!soap->d_stream)
  8942. return soap->error = SOAP_EOM;
  8943. soap->d_stream->zalloc = Z_NULL;
  8944. soap->d_stream->zfree = Z_NULL;
  8945. soap->d_stream->opaque = Z_NULL;
  8946. soap->d_stream->next_in = Z_NULL;
  8947. }
  8948. if (!soap->z_buf)
  8949. soap->z_buf = (char*)SOAP_MALLOC(soap, sizeof(soap->buf));
  8950. if (!soap->z_buf)
  8951. return soap->error = SOAP_EOM;
  8952. soap->d_stream->next_out = (Byte*)soap->z_buf;
  8953. soap->d_stream->avail_out = sizeof(soap->buf);
  8954. #ifdef WITH_GZIP
  8955. if (soap->zlib_out != SOAP_ZLIB_DEFLATE)
  8956. {
  8957. (void)soap_memcpy((void*)soap->z_buf, sizeof(soap->buf), (const void*)"\37\213\10\0\0\0\0\0\0\377", 10);
  8958. soap->d_stream->next_out = (Byte*)soap->z_buf + 10;
  8959. soap->d_stream->avail_out = sizeof(soap->buf) - 10;
  8960. soap->z_crc = crc32(0L, NULL, 0);
  8961. soap->zlib_out = SOAP_ZLIB_GZIP;
  8962. if (soap->z_dict)
  8963. *((Byte*)soap->z_buf + 2) = 0xff;
  8964. if (deflateInit2(soap->d_stream, soap->z_level, Z_DEFLATED, -MAX_WBITS, 8, Z_DEFAULT_STRATEGY) != Z_OK)
  8965. return soap->error = SOAP_ZLIB_ERROR;
  8966. }
  8967. else
  8968. #endif
  8969. if (deflateInit(soap->d_stream, soap->z_level) != Z_OK)
  8970. return soap->error = SOAP_ZLIB_ERROR;
  8971. if (soap->z_dict)
  8972. {
  8973. if (deflateSetDictionary(soap->d_stream, (const Bytef*)soap->z_dict, soap->z_dict_len) != Z_OK)
  8974. return soap->error = SOAP_ZLIB_ERROR;
  8975. }
  8976. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Deflate initialized\n"));
  8977. soap->zlib_state = SOAP_ZLIB_DEFLATE;
  8978. }
  8979. #endif
  8980. #ifdef WITH_OPENSSL
  8981. if (soap->ssl)
  8982. ERR_clear_error();
  8983. #endif
  8984. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Begin send phase (socket=%d mode=0x%x count="
  8985. SOAP_ULONG_FORMAT
  8986. ")\n", (int) soap->socket, soap->mode, soap->count));
  8987. soap->part = SOAP_BEGIN_SEND;
  8988. #ifndef WITH_LEANER
  8989. if (soap->fprepareinitsend && (soap->mode & SOAP_IO) == SOAP_IO_STORE &&
  8990. (soap->error = soap->fprepareinitsend(soap)) != SOAP_OK)
  8991. return soap->error;
  8992. #endif
  8993. #ifndef WITH_LEAN
  8994. soap->start = (ULONG64) time(NULL);
  8995. #endif
  8996. return SOAP_OK;
  8997. }
  8998. /******************************************************************************/
  8999. SOAP_FMAC1
  9000. int
  9001. SOAP_FMAC2
  9002. soap_begin_send(struct soap *soap) {
  9003. #ifndef WITH_LEANER
  9004. if (soap_init_send(soap))
  9005. return soap->error;
  9006. return soap_begin_attachments(soap);
  9007. #else
  9008. return soap_init_send(soap);
  9009. #endif
  9010. }
  9011. /******************************************************************************/
  9012. #ifndef WITH_NOIDREF
  9013. SOAP_FMAC1
  9014. void
  9015. SOAP_FMAC2
  9016. soap_embedded(struct soap *soap, const void *p, int t) {
  9017. struct soap_plist *pp;
  9018. if (soap_pointer_lookup(soap, p, t, &pp)) {
  9019. pp->mark1 = 1;
  9020. pp->mark2 = 1;
  9021. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Embedded %p type=%d mark set to 1\n", p, t));
  9022. }
  9023. }
  9024. #endif
  9025. /******************************************************************************/
  9026. #ifndef WITH_NOIDREF
  9027. SOAP_FMAC1
  9028. int
  9029. SOAP_FMAC2
  9030. soap_reference(struct soap *soap, const void *p, int t) {
  9031. struct soap_plist *pp;
  9032. if (!p ||
  9033. (!soap->encodingStyle && !(soap->omode & (SOAP_ENC_DIME | SOAP_ENC_MIME | SOAP_ENC_MTOM | SOAP_XML_GRAPH))) ||
  9034. (soap->omode & SOAP_XML_TREE))
  9035. return 1;
  9036. if (soap_pointer_lookup(soap, p, t, &pp)) {
  9037. if (pp->mark1 == 0) {
  9038. pp->mark1 = 2;
  9039. pp->mark2 = 2;
  9040. }
  9041. } else if (!soap_pointer_enter(soap, p, NULL, 0, t, &pp)) {
  9042. return 1;
  9043. }
  9044. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Reference %p type=%d (%d %d)\n", p, t, (int) pp->mark1, (int) pp->mark2));
  9045. return pp->mark1;
  9046. }
  9047. #endif
  9048. /******************************************************************************/
  9049. #ifndef WITH_NOIDREF
  9050. SOAP_FMAC1
  9051. int
  9052. SOAP_FMAC2
  9053. soap_array_reference(struct soap *soap, const void *p, const void *a, int n, int t) {
  9054. struct soap_plist *pp;
  9055. if (!p || !a || (!soap->encodingStyle && !(soap->omode & SOAP_XML_GRAPH)) || (soap->omode & SOAP_XML_TREE))
  9056. return 1;
  9057. if (soap_array_pointer_lookup(soap, p, a, n, t, &pp)) {
  9058. if (pp->mark1 == 0) {
  9059. pp->mark1 = 2;
  9060. pp->mark2 = 2;
  9061. }
  9062. } else if (!soap_pointer_enter(soap, p, a, n, t, &pp)) {
  9063. return 1;
  9064. }
  9065. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Array reference %p ptr=%p n=%lu t=%d (%d %d)\n", p, a, (unsigned long) n, t,
  9066. (int) pp->mark1, (int) pp->mark2));
  9067. return pp->mark1;
  9068. }
  9069. #endif
  9070. /******************************************************************************/
  9071. #ifndef WITH_NOIDREF
  9072. SOAP_FMAC1
  9073. int
  9074. SOAP_FMAC2
  9075. soap_attachment_reference(struct soap *soap, const void *p, const void *a, int n, int t, const char *id,
  9076. const char *type) {
  9077. struct soap_plist *pp;
  9078. if (!p || !a || (!soap->encodingStyle && !(soap->omode & SOAP_XML_GRAPH) && !id && !type) ||
  9079. (soap->omode & SOAP_XML_TREE))
  9080. return 1;
  9081. if (soap_array_pointer_lookup(soap, p, a, n, t, &pp)) {
  9082. if (pp->mark1 == 0) {
  9083. pp->mark1 = 2;
  9084. pp->mark2 = 2;
  9085. }
  9086. } else if (!soap_pointer_enter(soap, p, a, n, t, &pp)) {
  9087. return 1;
  9088. }
  9089. if (id || type)
  9090. soap->mode |= SOAP_ENC_DIME;
  9091. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Attachment reference %p ptr=%p n=%lu t=%d (%d %d)\n", p, a, (unsigned long) n, t,
  9092. (int) pp->mark1, (int) pp->mark2));
  9093. return pp->mark1;
  9094. }
  9095. #endif
  9096. /******************************************************************************/
  9097. #ifndef WITH_NOIDREF
  9098. SOAP_FMAC1
  9099. int
  9100. SOAP_FMAC2
  9101. soap_embedded_id(struct soap *soap, int id, const void *p, int t) {
  9102. struct soap_plist *pp = NULL;
  9103. if (id >= 0 || (!soap->encodingStyle && !(soap->omode & SOAP_XML_GRAPH)) || (soap->omode & SOAP_XML_TREE))
  9104. return id;
  9105. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Embedded_id %p type=%d id=%d\n", p, t, id));
  9106. if (id < -1)
  9107. return soap_embed(soap, p, NULL, 0, t);
  9108. if (id < 0) {
  9109. id = soap_pointer_lookup(soap, p, t, &pp);
  9110. if (soap->version == 1 && soap->part != SOAP_IN_HEADER) {
  9111. if (id) {
  9112. if ((soap->mode & SOAP_IO_LENGTH))
  9113. pp->mark1 = 2;
  9114. else
  9115. pp->mark2 = 2;
  9116. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Embedded_id multiref id=%d %p type=%d = (%d %d)\n", id, p, t,
  9117. (int) pp->mark1, (int) pp->mark2));
  9118. }
  9119. return -1;
  9120. } else if (id) {
  9121. if ((soap->mode & SOAP_IO_LENGTH))
  9122. pp->mark1 = 1;
  9123. else
  9124. pp->mark2 = 1;
  9125. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Embedded_id embedded ref id=%d %p type=%d = (%d %d)\n", id, p, t,
  9126. (int) pp->mark1, (int) pp->mark2));
  9127. }
  9128. }
  9129. return id;
  9130. }
  9131. #endif
  9132. /******************************************************************************/
  9133. #ifndef WITH_NOIDREF
  9134. SOAP_FMAC1
  9135. int
  9136. SOAP_FMAC2
  9137. soap_is_embedded(struct soap *soap, struct soap_plist *pp) {
  9138. if (!pp)
  9139. return 0;
  9140. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Is embedded? %d %d\n", (int) pp->mark1, (int) pp->mark2));
  9141. if (soap->version == 1 && soap->encodingStyle && !(soap->omode & SOAP_XML_GRAPH) && soap->part != SOAP_IN_HEADER) {
  9142. if ((soap->mode & SOAP_IO_LENGTH))
  9143. return pp->mark1 != 0;
  9144. return pp->mark2 != 0;
  9145. }
  9146. if ((soap->mode & SOAP_IO_LENGTH))
  9147. return pp->mark1 == 1;
  9148. return pp->mark2 == 1;
  9149. }
  9150. #endif
  9151. /******************************************************************************/
  9152. #ifndef WITH_NOIDREF
  9153. SOAP_FMAC1
  9154. int
  9155. SOAP_FMAC2
  9156. soap_is_single(struct soap *soap, struct soap_plist *pp) {
  9157. if (soap->part == SOAP_IN_HEADER)
  9158. return 1;
  9159. if (!pp)
  9160. return 0;
  9161. if ((soap->mode & SOAP_IO_LENGTH))
  9162. return pp->mark1 == 0;
  9163. return pp->mark2 == 0;
  9164. }
  9165. #endif
  9166. /******************************************************************************/
  9167. #ifndef WITH_NOIDREF
  9168. SOAP_FMAC1
  9169. void
  9170. SOAP_FMAC2
  9171. soap_set_embedded(struct soap *soap, struct soap_plist *pp) {
  9172. if (!pp)
  9173. return;
  9174. if ((soap->mode & SOAP_IO_LENGTH))
  9175. pp->mark1 = 1;
  9176. else
  9177. pp->mark2 = 1;
  9178. }
  9179. #endif
  9180. /******************************************************************************/
  9181. #ifndef WITH_LEANER
  9182. SOAP_FMAC1
  9183. int
  9184. SOAP_FMAC2
  9185. soap_attachment(struct soap *soap, const char *tag, int id, const void *p, const void *a, int n, const char *aid,
  9186. const char *atype, const char *aoptions, const char *type, int t) {
  9187. struct soap_plist *pp;
  9188. int i;
  9189. if (!p || !a || (!aid && !atype) ||
  9190. (!soap->encodingStyle && !(soap->omode & (SOAP_ENC_DIME | SOAP_ENC_MIME | SOAP_ENC_MTOM | SOAP_XML_GRAPH))) ||
  9191. (soap->omode & SOAP_XML_TREE))
  9192. return soap_element_id(soap, tag, id, p, a, n, type, t, NULL);
  9193. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Attachment tag='%s' id='%s' (%d) type='%s'\n", tag, aid ? aid : SOAP_STR_EOS, id,
  9194. atype ? atype : SOAP_STR_EOS));
  9195. i = soap_array_pointer_lookup(soap, p, a, n, t, &pp);
  9196. if (!i) {
  9197. i = soap_pointer_enter(soap, p, a, n, t, &pp);
  9198. if (!i) {
  9199. soap->error = SOAP_EOM;
  9200. return -1;
  9201. }
  9202. }
  9203. if (id <= 0)
  9204. id = i;
  9205. if (!aid) {
  9206. (SOAP_SNPRINTF(soap->tmpbuf, sizeof(soap->tmpbuf), strlen(soap->dime_id_format) + 20), soap->dime_id_format,
  9207. id);
  9208. aid = soap_strdup(soap, soap->tmpbuf);
  9209. if (!aid)
  9210. return -1;
  9211. }
  9212. /* Add MTOM xop:Include element when necessary */
  9213. /* TODO: this code to be obsoleted with new import/xop.h conventions */
  9214. if ((soap->omode & SOAP_ENC_MTOM) && strcmp(tag, "xop:Include")) {
  9215. if (soap_element_begin_out(soap, tag, 0, type)
  9216. ||
  9217. soap_element_href(soap, "xop:Include", 0, "xmlns:xop=\"http://www.w3.org/2004/08/xop/include\" href", aid)
  9218. || soap_element_end_out(soap, tag))
  9219. return soap->error;
  9220. } else if (soap_element_href(soap, tag, 0, "href", aid)) {
  9221. return soap->error;
  9222. }
  9223. if ((soap->mode & SOAP_IO_LENGTH)) {
  9224. if (pp->mark1 != 3) {
  9225. struct soap_multipart *content;
  9226. if ((soap->omode & SOAP_ENC_MTOM))
  9227. content = soap_alloc_multipart(soap, &soap->mime.first, &soap->mime.last, (const char *) a, n);
  9228. else
  9229. content = soap_alloc_multipart(soap, &soap->dime.first, &soap->dime.last, (const char *) a, n);
  9230. if (!content) {
  9231. soap->error = SOAP_EOM;
  9232. return -1;
  9233. }
  9234. if (!strncmp(aid, "cid:", 4)) /* RFC 2111 */
  9235. {
  9236. if ((soap->omode & SOAP_ENC_MTOM)) {
  9237. size_t l = strlen(aid) - 1;
  9238. char *s = (char *) soap_malloc(soap, l);
  9239. if (s) {
  9240. s[0] = '<';
  9241. (void) soap_strncpy(s + 1, l - 1, aid + 4, l - 3);
  9242. s[l - 2] = '>';
  9243. s[l - 1] = '\0';
  9244. content->id = s;
  9245. }
  9246. } else {
  9247. content->id = aid + 4;
  9248. }
  9249. } else {
  9250. content->id = aid;
  9251. }
  9252. content->type = atype;
  9253. content->options = aoptions;
  9254. content->encoding = SOAP_MIME_BINARY;
  9255. pp->mark1 = 3;
  9256. }
  9257. } else {
  9258. pp->mark2 = 3;
  9259. }
  9260. return -1;
  9261. }
  9262. #endif
  9263. /******************************************************************************/
  9264. #ifndef WITH_NOIDREF
  9265. static void
  9266. soap_init_iht(struct soap *soap) {
  9267. int i;
  9268. for (i = 0; i < SOAP_IDHASH; i++)
  9269. soap->iht[i] = NULL;
  9270. }
  9271. #endif
  9272. /******************************************************************************/
  9273. #ifndef WITH_NOIDREF
  9274. static void
  9275. soap_free_iht(struct soap *soap) {
  9276. int i;
  9277. struct soap_ilist *ip = NULL, *p = NULL;
  9278. struct soap_flist *fp = NULL, *fq = NULL;
  9279. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Free ID hashtable\n"));
  9280. for (i = 0; i < SOAP_IDHASH; i++) {
  9281. for (ip = soap->iht[i]; ip; ip = p) {
  9282. for (fp = ip->flist; fp; fp = fq) {
  9283. fq = fp->next;
  9284. SOAP_FREE(soap, fp);
  9285. }
  9286. p = ip->next;
  9287. SOAP_FREE(soap, ip);
  9288. }
  9289. soap->iht[i] = NULL;
  9290. }
  9291. }
  9292. #endif
  9293. /******************************************************************************/
  9294. #ifndef WITH_NOIDREF
  9295. SOAP_FMAC1
  9296. struct soap_ilist *
  9297. SOAP_FMAC2
  9298. soap_lookup(struct soap *soap, const char *id) {
  9299. struct soap_ilist *ip = NULL;
  9300. for (ip = soap->iht[soap_hash(id)]; ip; ip = ip->next)
  9301. if (!strcmp(ip->id, id))
  9302. return ip;
  9303. return NULL;
  9304. }
  9305. #endif
  9306. /******************************************************************************/
  9307. #ifndef WITH_NOIDREF
  9308. SOAP_FMAC1
  9309. struct soap_ilist *
  9310. SOAP_FMAC2
  9311. soap_enter(struct soap *soap, const char *id, int t, size_t n) {
  9312. size_t h;
  9313. struct soap_ilist *ip = NULL;
  9314. size_t l = strlen(id);
  9315. if (sizeof(struct soap_ilist) + l > l &&
  9316. (SOAP_MAXALLOCSIZE <= 0 || sizeof(struct soap_ilist) + l <= SOAP_MAXALLOCSIZE))
  9317. ip = (struct soap_ilist *) SOAP_MALLOC(soap, sizeof(struct soap_ilist) + l);
  9318. if (ip) {
  9319. ip->type = t;
  9320. ip->size = n;
  9321. ip->ptr = NULL;
  9322. ip->spine = NULL;
  9323. ip->link = NULL;
  9324. ip->copy = NULL;
  9325. ip->flist = NULL;
  9326. ip->smart = NULL;
  9327. ip->shaky = 0;
  9328. (void) soap_memcpy((char *) ip->id, l + 1, id, l + 1);
  9329. h = soap_hash(id); /* h = (HASH(id) % SOAP_IDHASH) so soap->iht[h] is safe */
  9330. ip->next = soap->iht[h];
  9331. soap->iht[h] = ip;
  9332. }
  9333. return ip;
  9334. }
  9335. #endif
  9336. /******************************************************************************/
  9337. SOAP_FMAC1
  9338. void *
  9339. SOAP_FMAC2
  9340. soap_malloc(struct soap *soap, size_t n) {
  9341. char *p;
  9342. size_t k = n;
  9343. if (SOAP_MAXALLOCSIZE > 0 && n > SOAP_MAXALLOCSIZE) {
  9344. soap->error = SOAP_EOM;
  9345. return NULL;
  9346. }
  9347. if (!soap)
  9348. return SOAP_MALLOC(soap, n);
  9349. n += sizeof(short);
  9350. n += (~n + 1) & (sizeof(void *) - 1); /* align at 4-, 8- or 16-byte boundary by rounding up */
  9351. if (n + sizeof(void *) + sizeof(size_t) < k) {
  9352. soap->error = SOAP_EOM;
  9353. return NULL;
  9354. }
  9355. p = (char *) SOAP_MALLOC(soap, n + sizeof(void *) + sizeof(size_t));
  9356. if (!p) {
  9357. soap->error = SOAP_EOM;
  9358. return NULL;
  9359. }
  9360. /* set a canary word to detect memory overruns and data corruption */
  9361. *(unsigned short *) (p + n - sizeof(unsigned short)) = (unsigned short) SOAP_CANARY;
  9362. /* keep chain of alloced cells for destruction */
  9363. *(void **) (p + n) = soap->alist;
  9364. *(size_t *) (p + n + sizeof(void *)) = n;
  9365. soap->alist = p + n;
  9366. return p;
  9367. }
  9368. /******************************************************************************/
  9369. #ifdef SOAP_MEM_DEBUG
  9370. static void
  9371. soap_init_mht(struct soap *soap) {
  9372. int i;
  9373. for (i = 0; i < (int) SOAP_PTRHASH; i++)
  9374. soap->mht[i] = NULL;
  9375. }
  9376. #endif
  9377. /******************************************************************************/
  9378. #ifdef SOAP_MEM_DEBUG
  9379. static void
  9380. soap_free_mht(struct soap *soap) {
  9381. int i;
  9382. struct soap_mlist *mp, *mq;
  9383. for (i = 0; i < (int) SOAP_PTRHASH; i++) {
  9384. for (mp = soap->mht[i]; mp; mp = mq) {
  9385. mq = mp->next;
  9386. if (mp->live)
  9387. fprintf(stderr, "%s(%d): malloc() = %p not freed (memory leak or forgot to call soap_end()?)\n",
  9388. mp->file, mp->line, mp->ptr);
  9389. free(mp);
  9390. }
  9391. soap->mht[i] = NULL;
  9392. }
  9393. }
  9394. #endif
  9395. /******************************************************************************/
  9396. #ifdef SOAP_MEM_DEBUG
  9397. SOAP_FMAC1
  9398. void *
  9399. SOAP_FMAC2
  9400. soap_track_malloc(struct soap *soap, const char *file, int line, size_t size) {
  9401. void *p = malloc(size);
  9402. if (soap) {
  9403. size_t h = soap_hash_ptr(p);
  9404. struct soap_mlist *mp = (struct soap_mlist *) malloc(sizeof(struct soap_mlist));
  9405. if (soap->fdebug[SOAP_INDEX_TEST]) {
  9406. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "%s(%d): malloc(%lu) = %p\n", file, line, (unsigned long) size, p));
  9407. }
  9408. mp->next = soap->mht[h];
  9409. mp->ptr = p;
  9410. mp->file = file;
  9411. mp->line = line;
  9412. mp->live = 1;
  9413. soap->mht[h] = mp;
  9414. }
  9415. return p;
  9416. }
  9417. #endif
  9418. /******************************************************************************/
  9419. #ifdef SOAP_MEM_DEBUG
  9420. SOAP_FMAC1
  9421. void
  9422. SOAP_FMAC2
  9423. soap_track_free(struct soap *soap, const char *file, int line, void *p) {
  9424. if (!soap) {
  9425. free(p);
  9426. } else {
  9427. size_t h = soap_hash_ptr(p);
  9428. struct soap_mlist *mp;
  9429. for (mp = soap->mht[h]; mp; mp = mp->next)
  9430. if (mp->ptr == p)
  9431. break;
  9432. if (mp) {
  9433. if (mp->live) {
  9434. if (soap->fdebug[SOAP_INDEX_TEST]) {
  9435. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "%s(%d): free(%p)\n", file, line, p));
  9436. }
  9437. free(p);
  9438. mp->live = 0;
  9439. } else {
  9440. fprintf(stderr, "%s(%d): free(%p) double free of pointer malloced at %s(%d)\n", file, line, p, mp->file,
  9441. mp->line);
  9442. }
  9443. } else {
  9444. fprintf(stderr, "%s(%d): free(%p) pointer not malloced\n", file, line, p);
  9445. }
  9446. }
  9447. }
  9448. #endif
  9449. /******************************************************************************/
  9450. #ifdef SOAP_MEM_DEBUG
  9451. static void
  9452. soap_track_unlink(struct soap *soap, const void *p) {
  9453. size_t h = soap_hash_ptr(p);
  9454. struct soap_mlist *mp;
  9455. for (mp = soap->mht[h]; mp; mp = mp->next)
  9456. if (mp->ptr == p)
  9457. break;
  9458. if (mp)
  9459. mp->live = 0;
  9460. }
  9461. #endif
  9462. /******************************************************************************/
  9463. SOAP_FMAC1
  9464. void
  9465. SOAP_FMAC2
  9466. soap_dealloc(struct soap *soap, void *p) {
  9467. if (soap_check_state(soap))
  9468. return;
  9469. if (p) {
  9470. char **q;
  9471. for (q = (char **) (void *) &soap->alist; *q; q = *(char ***) q) {
  9472. if (*(unsigned short *) (char *) (*q - sizeof(unsigned short)) != (unsigned short) SOAP_CANARY) {
  9473. #ifdef SOAP_MEM_DEBUG
  9474. fprintf(stderr, "Data corruption in dynamic allocation (see logs)\n");
  9475. #endif
  9476. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Data corruption:\n"));
  9477. DBGHEX(TEST, *q - 200, 200);
  9478. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "\n"));
  9479. soap->error = SOAP_MOE;
  9480. return;
  9481. }
  9482. if (p == (void *) (*q - *(size_t *) (*q + sizeof(void *)))) {
  9483. *q = **(char ***) q;
  9484. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Freed data at %p\n", p));
  9485. SOAP_FREE(soap, p);
  9486. return;
  9487. }
  9488. }
  9489. soap_delete(soap, p);
  9490. } else {
  9491. char *q;
  9492. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Free all soap_malloc() data\n"));
  9493. while (soap->alist) {
  9494. q = (char *) soap->alist;
  9495. if (*(unsigned short *) (char *) (q - sizeof(unsigned short)) != (unsigned short) SOAP_CANARY) {
  9496. #ifdef SOAP_MEM_DEBUG
  9497. fprintf(stderr, "Data corruption in dynamic allocation (see logs)\n");
  9498. #endif
  9499. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Data corruption:\n"));
  9500. DBGHEX(TEST, q - 200, 200);
  9501. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "\n"));
  9502. soap->error = SOAP_MOE;
  9503. return;
  9504. }
  9505. soap->alist = *(void **) q;
  9506. q -= *(size_t *) (q + sizeof(void *));
  9507. SOAP_FREE(soap, q);
  9508. }
  9509. /* assume these were deallocated: */
  9510. soap->http_content = NULL;
  9511. soap->action = NULL;
  9512. soap->fault = NULL;
  9513. soap->header = NULL;
  9514. soap->bearer = NULL;
  9515. soap->userid = NULL;
  9516. soap->passwd = NULL;
  9517. soap->authrealm = NULL;
  9518. #ifdef WITH_NTLM
  9519. soap->ntlm_challenge = NULL;
  9520. #endif
  9521. #ifndef WITH_LEANER
  9522. soap_clr_mime(soap);
  9523. #endif
  9524. }
  9525. }
  9526. /******************************************************************************/
  9527. SOAP_FMAC1
  9528. void
  9529. SOAP_FMAC2
  9530. soap_delete(struct soap *soap, void *p) {
  9531. struct soap_clist **cp;
  9532. if (soap_check_state(soap))
  9533. return;
  9534. cp = &soap->clist;
  9535. if (p) {
  9536. while (*cp) {
  9537. if (p == (*cp)->ptr) {
  9538. struct soap_clist *q = *cp;
  9539. *cp = q->next;
  9540. if (q->fdelete(soap, q)) {
  9541. DBGLOG(TEST, SOAP_MESSAGE(fdebug,
  9542. "Could not dealloc data %p: deletion callback failed for object type=%d\n",
  9543. q->ptr, q->type));
  9544. #ifdef SOAP_MEM_DEBUG
  9545. fprintf(stderr, "new(object type=%d) = %p not freed: deletion callback failed\n", q->type, q->ptr);
  9546. #endif
  9547. }
  9548. SOAP_FREE(soap, q);
  9549. return;
  9550. }
  9551. cp = &(*cp)->next;
  9552. }
  9553. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Could not dealloc data %p: address not in list\n", p));
  9554. } else {
  9555. while (*cp) {
  9556. struct soap_clist *q = *cp;
  9557. *cp = q->next;
  9558. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Delete %p type=%d (cp=%p)\n", q->ptr, q->type, (void *) q));
  9559. if (q->fdelete(soap, q)) {
  9560. DBGLOG(TEST,
  9561. SOAP_MESSAGE(fdebug, "Could not dealloc data %p: deletion callback failed for object type=%d\n",
  9562. q->ptr, q->type));
  9563. #ifdef SOAP_MEM_DEBUG
  9564. fprintf(stderr, "new(object type=%d) = %p not freed: deletion callback failed\n", q->type, q->ptr);
  9565. #endif
  9566. }
  9567. SOAP_FREE(soap, q);
  9568. }
  9569. }
  9570. soap->fault = NULL; /* assume this was deallocated */
  9571. soap->header = NULL; /* assume this was deallocated */
  9572. }
  9573. /******************************************************************************/
  9574. SOAP_FMAC1
  9575. void
  9576. SOAP_FMAC2
  9577. soap_delegate_deletion(struct soap *soap, struct soap *soap_to) {
  9578. struct soap_clist *cp;
  9579. char **q;
  9580. #ifdef SOAP_MEM_DEBUG
  9581. void *p;
  9582. struct soap_mlist **mp, *mq;
  9583. size_t h;
  9584. #endif
  9585. for (q = (char **) (void *) &soap->alist; *q; q = *(char ***) q) {
  9586. if (*(unsigned short *) (char *) (*q - sizeof(unsigned short)) != (unsigned short) SOAP_CANARY) {
  9587. #ifdef SOAP_MEM_DEBUG
  9588. fprintf(stderr, "Data corruption in dynamic allocation (see logs)\n");
  9589. #endif
  9590. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Data corruption:\n"));
  9591. DBGHEX(TEST, *q - 200, 200);
  9592. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "\n"));
  9593. soap->error = SOAP_MOE;
  9594. return;
  9595. }
  9596. #ifdef SOAP_MEM_DEBUG
  9597. p = (void *) (*q - *(size_t *) (*q + sizeof(void *)));
  9598. h = soap_hash_ptr(p);
  9599. for (mp = &soap->mht[h]; *mp; mp = &(*mp)->next) {
  9600. if ((*mp)->ptr == p) {
  9601. mq = *mp;
  9602. *mp = mq->next;
  9603. mq->next = soap_to->mht[h];
  9604. soap_to->mht[h] = mq;
  9605. break;
  9606. }
  9607. }
  9608. #endif
  9609. }
  9610. *q = (char *) soap_to->alist;
  9611. soap_to->alist = soap->alist;
  9612. soap->alist = NULL;
  9613. #ifdef SOAP_MEM_DEBUG
  9614. cp = soap->clist;
  9615. while (cp) {
  9616. h = soap_hash_ptr(cp);
  9617. for (mp = &soap->mht[h]; *mp; mp = &(*mp)->next) {
  9618. if ((*mp)->ptr == cp) {
  9619. mq = *mp;
  9620. *mp = mq->next;
  9621. mq->next = soap_to->mht[h];
  9622. soap_to->mht[h] = mq;
  9623. break;
  9624. }
  9625. }
  9626. cp = cp->next;
  9627. }
  9628. #endif
  9629. cp = soap_to->clist;
  9630. if (cp) {
  9631. while (cp->next)
  9632. cp = cp->next;
  9633. cp->next = soap->clist;
  9634. } else {
  9635. soap_to->clist = soap->clist;
  9636. }
  9637. soap->clist = NULL;
  9638. }
  9639. /******************************************************************************/
  9640. SOAP_FMAC1
  9641. struct soap_clist *
  9642. SOAP_FMAC2
  9643. soap_link(struct soap *soap, int t, int n, int (*fdelete)(struct soap *, struct soap_clist *)) {
  9644. struct soap_clist *cp = NULL;
  9645. if (soap) {
  9646. if (n != SOAP_NO_LINK_TO_DELETE) {
  9647. cp = (struct soap_clist *) SOAP_MALLOC(soap, sizeof(struct soap_clist));
  9648. if (!cp) {
  9649. soap->error = SOAP_EOM;
  9650. } else {
  9651. cp->next = soap->clist;
  9652. cp->type = t;
  9653. cp->size = n;
  9654. cp->ptr = NULL;
  9655. cp->fdelete = fdelete;
  9656. soap->clist = cp;
  9657. }
  9658. }
  9659. soap->alloced = t;
  9660. }
  9661. return cp;
  9662. }
  9663. /******************************************************************************/
  9664. SOAP_FMAC1
  9665. int
  9666. SOAP_FMAC2
  9667. soap_unlink(struct soap *soap, const void *p) {
  9668. char **q;
  9669. struct soap_clist **cp;
  9670. if (soap && p) {
  9671. for (q = (char **) (void *) &soap->alist; *q; q = *(char ***) q) {
  9672. if (p == (void *) (*q - *(size_t *) (*q + sizeof(void *)))) {
  9673. *q = **(char ***) q;
  9674. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Unlinked data %p\n", p));
  9675. #ifdef SOAP_MEM_DEBUG
  9676. soap_track_unlink(soap, p);
  9677. #endif
  9678. return SOAP_OK; /* found and removed from dealloc chain */
  9679. }
  9680. }
  9681. for (cp = &soap->clist; *cp; cp = &(*cp)->next) {
  9682. if (p == (*cp)->ptr) {
  9683. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Unlinked class instance %p\n", p));
  9684. q = (char **) *cp;
  9685. *cp = (*cp)->next;
  9686. SOAP_FREE(soap, q);
  9687. return SOAP_OK; /* found and removed from dealloc chain */
  9688. }
  9689. }
  9690. }
  9691. return SOAP_ERR;
  9692. }
  9693. /******************************************************************************/
  9694. #ifndef WITH_NOIDREF
  9695. SOAP_FMAC1
  9696. int
  9697. SOAP_FMAC2
  9698. soap_lookup_type(struct soap *soap, const char *id) {
  9699. struct soap_ilist *ip;
  9700. if (id && *id) {
  9701. ip = soap_lookup(soap, id);
  9702. if (ip) {
  9703. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Lookup id='%s' type=%d\n", id, ip->type));
  9704. return ip->type;
  9705. }
  9706. }
  9707. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "lookup type id='%s' NOT FOUND! Need to get it from xsi:type\n", id));
  9708. return 0;
  9709. }
  9710. #endif
  9711. /******************************************************************************/
  9712. #ifndef WITH_NOIDREF
  9713. SOAP_FMAC1
  9714. short
  9715. SOAP_FMAC2
  9716. soap_begin_shaky(struct soap *soap) {
  9717. short f = soap->shaky;
  9718. soap->shaky = 1;
  9719. return f;
  9720. }
  9721. #endif
  9722. /******************************************************************************/
  9723. #ifndef WITH_NOIDREF
  9724. SOAP_FMAC1
  9725. void
  9726. SOAP_FMAC2
  9727. soap_end_shaky(struct soap *soap, short f) {
  9728. soap->shaky = f;
  9729. }
  9730. #endif
  9731. /******************************************************************************/
  9732. #ifndef WITH_NOIDREF
  9733. static int
  9734. soap_is_shaky(struct soap *soap, void *p) {
  9735. (void) p;
  9736. if (!soap->blist && !soap->shaky)
  9737. return 0;
  9738. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Shaky %p\n", p));
  9739. return 1;
  9740. }
  9741. #endif
  9742. /******************************************************************************/
  9743. #ifndef WITH_NOIDREF
  9744. SOAP_FMAC1
  9745. void *
  9746. SOAP_FMAC2
  9747. soap_id_lookup(struct soap *soap, const char *id, void **p, int t, size_t n, unsigned int k, int (*fbase)(int, int)) {
  9748. struct soap_ilist *ip;
  9749. if (!p || !id || !*id)
  9750. return p;
  9751. ip = soap_lookup(soap, id); /* lookup pointer to hash table entry for string id */
  9752. if (!ip) {
  9753. ip = soap_enter(soap, id, t, n); /* new hash table entry for string id */
  9754. if (!ip)
  9755. return NULL;
  9756. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Forwarding first href='%s' type=%d location=%p (%u bytes) level=%u\n", id, t,
  9757. (void *) p, (unsigned int) n, k));
  9758. *p = NULL;
  9759. if (k) {
  9760. int i;
  9761. if (k > SOAP_MAXPTRS)
  9762. return NULL;
  9763. ip->spine = (void **) soap_malloc(soap, SOAP_MAXPTRS * sizeof(void *));
  9764. if (!ip->spine)
  9765. return NULL;
  9766. ip->spine[0] = NULL;
  9767. for (i = 1; i < SOAP_MAXPTRS; i++)
  9768. ip->spine[i] = &ip->spine[i - 1];
  9769. *p = &ip->spine[k - 1];
  9770. } else {
  9771. ip->link = p;
  9772. ip->shaky = soap_is_shaky(soap, (void *) p);
  9773. }
  9774. } else if (ip->type != t && (!fbase || !fbase(ip->type, t)) &&
  9775. (!fbase || !fbase(t, ip->type) || soap_type_punned(soap, ip))) {
  9776. DBGLOG(TEST,
  9777. SOAP_MESSAGE(fdebug, "Lookup type incompatibility: ref='%s' id-type=%d ref-type=%d\n", id, ip->type, t));
  9778. (void) soap_id_nullify(soap, id);
  9779. return NULL;
  9780. } else if (k == 0 && ip->ptr && !ip->shaky) /* when block lists are in use, ip->ptr will change */
  9781. {
  9782. DBGLOG(TEST,
  9783. SOAP_MESSAGE(fdebug, "Resolved href='%s' type=%d location=%p (%u bytes) level=%u\n", id, t, ip->ptr,
  9784. (unsigned int) n, k));
  9785. *p = ip->ptr;
  9786. } else {
  9787. DBGLOG(TEST,
  9788. SOAP_MESSAGE(fdebug, "Forwarded href='%s' type=%d location=%p (%u bytes) level=%u\n", id, t, (void *) p,
  9789. (unsigned int) n, k));
  9790. if (fbase && fbase(t, ip->type) && !soap_type_punned(soap, ip)) {
  9791. ip->type = t;
  9792. ip->size = n;
  9793. }
  9794. *p = NULL;
  9795. if (k) {
  9796. if (!ip->spine) {
  9797. int i;
  9798. if (k > SOAP_MAXPTRS)
  9799. return NULL;
  9800. ip->spine = (void **) soap_malloc(soap, SOAP_MAXPTRS * sizeof(void *));
  9801. if (!ip->spine)
  9802. return NULL;
  9803. ip->spine[0] = NULL;
  9804. for (i = 1; i < SOAP_MAXPTRS; i++)
  9805. ip->spine[i] = &ip->spine[i - 1];
  9806. }
  9807. *p = &ip->spine[k - 1];
  9808. if (ip->ptr && !ip->shaky)
  9809. ip->spine[0] = ip->ptr;
  9810. } else {
  9811. void *q = ip->link;
  9812. ip->link = p;
  9813. ip->shaky = soap_is_shaky(soap, (void *) p);
  9814. *p = q;
  9815. }
  9816. }
  9817. return p;
  9818. }
  9819. #endif
  9820. /******************************************************************************/
  9821. #ifndef WITH_NOIDREF
  9822. SOAP_FMAC1
  9823. void *
  9824. SOAP_FMAC2
  9825. soap_id_forward(struct soap *soap, const char *href, void *p, size_t i, int t, int tt, size_t n, unsigned int k,
  9826. void (*finsert)(struct soap *, int, int, void *, size_t, const void *, void **),
  9827. int (*fbase)(int, int)) {
  9828. struct soap_ilist *ip;
  9829. if (!p || !href || !*href)
  9830. return p;
  9831. ip = soap_lookup(soap, href); /* lookup pointer to hash table entry for string id */
  9832. if (!ip) {
  9833. ip = soap_enter(soap, href, t, n); /* new hash table entry for string id */
  9834. if (!ip)
  9835. return NULL;
  9836. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "New entry href='%s' type=%d size=%lu level=%d location=%p\n", href, t,
  9837. (unsigned long) n, k, p));
  9838. } else if ((ip->type != t || ip->size != n) && k == 0) {
  9839. DBGLOG(TEST, SOAP_MESSAGE(fdebug,
  9840. "Forward type incompatibility id='%s' expect type=%d size=%lu level=%u got type=%d size=%lu\n",
  9841. href, ip->type, (unsigned long) ip->size, k, t, (unsigned long) n));
  9842. (void) soap_id_nullify(soap, href);
  9843. return NULL;
  9844. }
  9845. if (finsert || n < sizeof(void *)) {
  9846. struct soap_flist *fp = (struct soap_flist *) SOAP_MALLOC(soap, sizeof(struct soap_flist));
  9847. if (!fp) {
  9848. soap->error = SOAP_EOM;
  9849. return NULL;
  9850. }
  9851. if (fbase && fbase(t, ip->type) && !soap_type_punned(soap, ip)) {
  9852. ip->type = t;
  9853. ip->size = n;
  9854. }
  9855. if ((ip->type != t || ip->size != n) && (!fbase || !fbase(ip->type, t))) {
  9856. DBGLOG(TEST, SOAP_MESSAGE(fdebug,
  9857. "Forward type incompatibility id='%s' expect type=%d size=%lu level=%u got type=%d size=%lu\n",
  9858. href, ip->type, (unsigned long) ip->size, k, t, (unsigned long) n));
  9859. SOAP_FREE(soap, fp);
  9860. (void) soap_id_nullify(soap, href);
  9861. return NULL;
  9862. }
  9863. fp->next = ip->flist;
  9864. fp->type = tt;
  9865. fp->ptr = p;
  9866. fp->level = k;
  9867. fp->index = i;
  9868. fp->finsert = finsert;
  9869. ip->flist = fp;
  9870. DBGLOG(TEST, SOAP_MESSAGE(fdebug,
  9871. "Forwarding type=%d (target type=%d) size=%lu location=%p level=%u index=%lu href='%s'\n",
  9872. t, tt, (unsigned long) n, p, k, (unsigned long) i, href));
  9873. } else {
  9874. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Forwarding copying address %p for type=%d href='%s'\n", p, t, href));
  9875. *(void **) p = ip->copy;
  9876. ip->copy = p;
  9877. }
  9878. ip->shaky = soap_is_shaky(soap, p);
  9879. return p;
  9880. }
  9881. #endif
  9882. /******************************************************************************/
  9883. SOAP_FMAC1
  9884. void *
  9885. SOAP_FMAC2
  9886. soap_id_enter(struct soap *soap, const char *id, void *p, int t, size_t n, const char *type, const char *arrayType,
  9887. void *(*finstantiate)(struct soap *, int, const char *, const char *, size_t *), int (*fbase)(int, int)) {
  9888. #ifndef WITH_NOIDREF
  9889. struct soap_ilist *ip;
  9890. #endif
  9891. (void) id;
  9892. (void) fbase;
  9893. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Enter id='%s' type=%d location=%p size=%lu\n", id, t, p, (unsigned long) n));
  9894. soap->alloced = 0;
  9895. if (!p) {
  9896. if (finstantiate) {
  9897. p = finstantiate(soap, t, type, arrayType, &n); /* soap->alloced is set in soap_link() */
  9898. t = soap->alloced;
  9899. } else {
  9900. p = soap_malloc(soap, n);
  9901. soap->alloced = t;
  9902. }
  9903. }
  9904. #ifndef WITH_NOIDREF
  9905. if (!id || !*id)
  9906. #endif
  9907. return p;
  9908. #ifndef WITH_NOIDREF
  9909. ip = soap_lookup(soap, id); /* lookup pointer to hash table entry for string id */
  9910. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Lookup entry id='%s' for location=%p type=%d\n", id, p, t));
  9911. if (!ip) {
  9912. ip = soap_enter(soap, id, t, n); /* new hash table entry for string id */
  9913. if (!ip)
  9914. return NULL;
  9915. ip->ptr = p;
  9916. DBGLOG(TEST,
  9917. SOAP_MESSAGE(fdebug, "New entry id='%s' type=%d size=%lu location=%p\n", id, t, (unsigned long) n, p));
  9918. if (!soap->alloced)
  9919. ip->shaky = soap_is_shaky(soap, p);
  9920. } else if (ip->ptr) {
  9921. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Multiply defined id='%s'\n", id));
  9922. soap_strcpy(soap->id, sizeof(soap->id), id);
  9923. soap->error = SOAP_DUPLICATE_ID;
  9924. return NULL;
  9925. } else if ((ip->type != t && (!fbase || !fbase(t, ip->type) || soap_type_punned(soap, ip)))
  9926. || (ip->type == t && ip->size != n && soap_type_punned(soap, ip))) {
  9927. DBGLOG(TEST,
  9928. SOAP_MESSAGE(fdebug, "Enter type incompatibility id='%s' expect type=%d size=%lu got type=%d size=%lu\n",
  9929. id, ip->type, (unsigned long) ip->size, t, (unsigned long) n));
  9930. (void) soap_id_nullify(soap, id);
  9931. return NULL;
  9932. } else {
  9933. ip->type = t;
  9934. ip->size = n;
  9935. ip->ptr = p;
  9936. if (!soap->alloced)
  9937. ip->shaky = soap_is_shaky(soap, p);
  9938. if (soap->alloced || !ip->shaky) {
  9939. void **q; /* ptr will not change later, so resolve links now */
  9940. if (ip->spine)
  9941. ip->spine[0] = p;
  9942. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Traversing link chain to resolve id='%s' type=%d\n", ip->id, ip->type));
  9943. q = (void **) ip->link;
  9944. while (q) {
  9945. void *r = *q;
  9946. *q = p;
  9947. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "... link %p -> %p\n", (void *) q, p));
  9948. q = (void **) r;
  9949. }
  9950. ip->link = NULL;
  9951. }
  9952. }
  9953. return ip->ptr;
  9954. #endif
  9955. }
  9956. /******************************************************************************/
  9957. SOAP_FMAC1
  9958. void **
  9959. SOAP_FMAC2
  9960. soap_id_smart(struct soap *soap, const char *id, int t, size_t n) {
  9961. (void) soap;
  9962. (void) id;
  9963. (void) t;
  9964. (void) n;
  9965. #ifndef WITH_NOIDREF
  9966. if (id && *id) {
  9967. struct soap_ilist *ip = soap_lookup(soap, id); /* lookup pointer to hash table entry for string id */
  9968. if (!ip) {
  9969. ip = soap_enter(soap, id, t, n); /* new hash table entry for string id */
  9970. if (!ip)
  9971. return NULL;
  9972. }
  9973. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "New smart shared pointer entry id='%s' type=%d size=%lu smart=%p\n", id, t,
  9974. (unsigned long) n, ip->smart));
  9975. return &ip->smart;
  9976. }
  9977. #endif
  9978. return NULL;
  9979. }
  9980. /******************************************************************************/
  9981. #ifndef WITH_NOIDREF
  9982. static int
  9983. soap_type_punned(struct soap *soap, const struct soap_ilist *ip) {
  9984. const struct soap_flist *fp;
  9985. (void) soap;
  9986. if (ip->ptr || ip->copy)
  9987. return 1;
  9988. for (fp = ip->flist; fp; fp = fp->next)
  9989. if (fp->level == 0)
  9990. return 1;
  9991. return 0;
  9992. }
  9993. #endif
  9994. /******************************************************************************/
  9995. #ifndef WITH_NOIDREF
  9996. SOAP_FMAC1
  9997. int
  9998. SOAP_FMAC2
  9999. soap_id_nullify(struct soap *soap, const char *id) {
  10000. int i;
  10001. for (i = 0; i < SOAP_IDHASH; i++) {
  10002. struct soap_ilist *ip;
  10003. for (ip = soap->iht[i]; ip; ip = ip->next) {
  10004. void *p, *q;
  10005. for (p = ip->link; p; p = q) {
  10006. q = *(void **) p;
  10007. *(void **) p = NULL;
  10008. }
  10009. ip->link = NULL;
  10010. }
  10011. }
  10012. soap_strcpy(soap->id, sizeof(soap->id), id);
  10013. return soap->error = SOAP_HREF;
  10014. }
  10015. #endif
  10016. /******************************************************************************/
  10017. SOAP_FMAC1
  10018. int
  10019. SOAP_FMAC2
  10020. soap_end_send(struct soap *soap) {
  10021. #ifndef WITH_LEANER
  10022. int err;
  10023. err = soap_end_attachments(soap);
  10024. if (soap->dime.list) {
  10025. /* SOAP body referenced attachments must appear first */
  10026. soap->dime.last->next = soap->dime.first;
  10027. soap->dime.first = soap->dime.list->next;
  10028. soap->dime.list->next = NULL;
  10029. soap->dime.last = soap->dime.list;
  10030. }
  10031. if (!err)
  10032. err = soap_putdime(soap);
  10033. if (!err)
  10034. err = soap_putmime(soap);
  10035. soap->mime.list = NULL;
  10036. soap->mime.first = NULL;
  10037. soap->mime.last = NULL;
  10038. soap->dime.list = NULL;
  10039. soap->dime.first = NULL;
  10040. soap->dime.last = NULL;
  10041. if (err)
  10042. return err;
  10043. #endif
  10044. return soap_end_send_flush(soap);
  10045. }
  10046. /******************************************************************************/
  10047. SOAP_FMAC1
  10048. int
  10049. SOAP_FMAC2
  10050. soap_end_send_flush(struct soap *soap) {
  10051. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "End send mode=0x%x\n", soap->mode));
  10052. if ((soap->mode & SOAP_IO)) /* need to flush the remaining data in buffer */
  10053. {
  10054. if (soap_flush(soap))
  10055. #ifdef WITH_ZLIB
  10056. {
  10057. if ((soap->mode & SOAP_ENC_ZLIB) && soap->zlib_state == SOAP_ZLIB_DEFLATE)
  10058. {
  10059. soap->zlib_state = SOAP_ZLIB_NONE;
  10060. deflateEnd(soap->d_stream);
  10061. }
  10062. return soap->error;
  10063. }
  10064. #else
  10065. return soap->error;
  10066. #endif
  10067. #ifdef WITH_ZLIB
  10068. if ((soap->mode & SOAP_ENC_ZLIB) && soap->d_stream)
  10069. {
  10070. int r;
  10071. soap->d_stream->avail_in = 0;
  10072. do
  10073. {
  10074. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Deflating remainder\n"));
  10075. r = deflate(soap->d_stream, Z_FINISH);
  10076. if (soap->d_stream->avail_out != sizeof(soap->buf))
  10077. {
  10078. if (soap_flush_raw(soap, soap->z_buf, sizeof(soap->buf) - soap->d_stream->avail_out))
  10079. {
  10080. soap->zlib_state = SOAP_ZLIB_NONE;
  10081. deflateEnd(soap->d_stream);
  10082. return soap->error;
  10083. }
  10084. soap->d_stream->next_out = (Byte*)soap->z_buf;
  10085. soap->d_stream->avail_out = sizeof(soap->buf);
  10086. }
  10087. } while (r == Z_OK);
  10088. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Deflated total %lu->%lu bytes\n", soap->d_stream->total_in, soap->d_stream->total_out));
  10089. soap->z_ratio_out = (float)soap->d_stream->total_out / (float)soap->d_stream->total_in;
  10090. soap->mode &= ~SOAP_ENC_ZLIB;
  10091. soap->zlib_state = SOAP_ZLIB_NONE;
  10092. if (deflateEnd(soap->d_stream) != Z_OK || r != Z_STREAM_END)
  10093. {
  10094. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Unable to end deflate: %s\n", soap->d_stream->msg ? soap->d_stream->msg : SOAP_STR_EOS));
  10095. return soap->error = SOAP_ZLIB_ERROR;
  10096. }
  10097. #ifdef WITH_GZIP
  10098. if (soap->zlib_out != SOAP_ZLIB_DEFLATE)
  10099. {
  10100. soap->z_buf[0] = soap->z_crc & 0xFF;
  10101. soap->z_buf[1] = (soap->z_crc >> 8) & 0xFF;
  10102. soap->z_buf[2] = (soap->z_crc >> 16) & 0xFF;
  10103. soap->z_buf[3] = (soap->z_crc >> 24) & 0xFF;
  10104. soap->z_buf[4] = soap->d_stream->total_in & 0xFF;
  10105. soap->z_buf[5] = (soap->d_stream->total_in >> 8) & 0xFF;
  10106. soap->z_buf[6] = (soap->d_stream->total_in >> 16) & 0xFF;
  10107. soap->z_buf[7] = (soap->d_stream->total_in >> 24) & 0xFF;
  10108. if (soap_flush_raw(soap, soap->z_buf, 8))
  10109. return soap->error;
  10110. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "gzip crc32=%lu\n", (unsigned long)soap->z_crc));
  10111. }
  10112. #endif
  10113. }
  10114. #endif
  10115. if ((soap->mode & SOAP_IO) == SOAP_IO_STORE) {
  10116. #if !defined(__cplusplus) || defined(WITH_COMPAT)
  10117. if (soap->os)
  10118. {
  10119. char *b = (char*)soap_push_block(soap, NULL, 1);
  10120. if (b)
  10121. {
  10122. *b = '\0';
  10123. *soap->os = soap_save_block(soap, NULL, NULL, 0);
  10124. }
  10125. }
  10126. else
  10127. #endif
  10128. {
  10129. char *p;
  10130. #ifndef WITH_NOHTTP
  10131. if (!(soap->mode & SOAP_ENC_PLAIN)) {
  10132. soap->mode--;
  10133. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Sending buffered message of length %u\n",
  10134. (unsigned int) soap->blist->size));
  10135. if (soap->status >= SOAP_POST)
  10136. soap->error = soap->fpost(soap, soap->endpoint, soap->host, soap->port, soap->path,
  10137. soap->action, soap->blist->size);
  10138. else if (soap->status != SOAP_STOP)
  10139. soap->error = soap->fresponse(soap, soap->status, soap->blist->size);
  10140. if (soap->error || soap_flush(soap))
  10141. return soap->error;
  10142. soap->mode++;
  10143. }
  10144. #endif
  10145. for (p = soap_first_block(soap, NULL); p; p = soap_next_block(soap, NULL)) {
  10146. DBGMSG(SENT, p, soap_block_size(soap, NULL));
  10147. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Send %u bytes to socket=%d/fd=%d\n",
  10148. (unsigned int) soap_block_size(soap, NULL), (int) soap->socket,
  10149. soap->sendfd));
  10150. soap->error = soap->fsend(soap, p, soap_block_size(soap, NULL));
  10151. if (soap->error) {
  10152. soap_end_block(soap, NULL);
  10153. return soap->error;
  10154. }
  10155. }
  10156. soap_end_block(soap, NULL);
  10157. }
  10158. #ifndef WITH_LEANER
  10159. if (soap->fpreparefinalsend && (soap->error = soap->fpreparefinalsend(soap)) != SOAP_OK)
  10160. return soap->error;
  10161. #endif
  10162. if ((soap->omode & SOAP_IO) == SOAP_IO_STORE && (soap->imode & SOAP_IO) != SOAP_IO_STORE)
  10163. soap->omode = (soap->omode & ~SOAP_IO) | (soap->imode & SOAP_IO);
  10164. }
  10165. #ifndef WITH_LEANER
  10166. else if ((soap->mode & SOAP_IO) == SOAP_IO_CHUNK) {
  10167. DBGMSG(SENT, "\r\n0\r\n\r\n", 7);
  10168. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Send 7 bytes to socket=%d/fd=%d\n", (int) soap->socket, soap->sendfd));
  10169. soap->error = soap->fsend(soap, "\r\n0\r\n\r\n", 7);
  10170. if (soap->error)
  10171. return soap->error;
  10172. }
  10173. #endif
  10174. }
  10175. #ifdef WITH_TCPFIN
  10176. #if defined(WITH_OPENSSL) || defined(WITH_SYSTEMSSL)
  10177. if (!soap->ssl)
  10178. #endif
  10179. if (soap_valid_socket(soap->socket) && !soap->keep_alive && !(soap->omode & SOAP_IO_UDP))
  10180. soap->fshutdownsocket(soap, soap->socket, SOAP_SHUT_WR); /* Send TCP FIN */
  10181. #endif
  10182. #if defined(__cplusplus) && !defined(WITH_COMPAT)
  10183. if (soap->os)
  10184. soap->os->flush();
  10185. #endif
  10186. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "End of send phase\n"));
  10187. soap->omode &= ~SOAP_SEC_WSUID;
  10188. soap->count = 0;
  10189. soap->part = SOAP_END;
  10190. return SOAP_OK;
  10191. }
  10192. /******************************************************************************/
  10193. SOAP_FMAC1
  10194. int
  10195. SOAP_FMAC2
  10196. soap_end_recv(struct soap *soap) {
  10197. soap->part = SOAP_END;
  10198. #ifndef WITH_LEAN
  10199. soap->wsuid = NULL; /* reset before next send */
  10200. soap->c14nexclude = NULL; /* reset before next send */
  10201. soap->c14ninclude = NULL; /* reset before next send */
  10202. #endif
  10203. #ifndef WITH_LEANER
  10204. soap->ffilterrecv = NULL;
  10205. if ((soap->mode & SOAP_ENC_DIME) && soap_getdime(soap)) {
  10206. soap->dime.first = NULL;
  10207. soap->dime.last = NULL;
  10208. return soap->error;
  10209. }
  10210. soap->dime.list = soap->dime.first;
  10211. soap->dime.first = NULL;
  10212. soap->dime.last = NULL;
  10213. /* Check if MIME attachments and mime-post-check flag is set, if so call soap_resolve() and return */
  10214. if ((soap->mode & SOAP_ENC_MIME)) {
  10215. if ((soap->mode & SOAP_MIME_POSTCHECK)) {
  10216. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Post checking MIME attachments\n"));
  10217. if (!soap->keep_alive)
  10218. soap->keep_alive = -2; /* special case to keep alive */
  10219. #ifndef WITH_NOIDREF
  10220. soap_resolve(soap);
  10221. #endif
  10222. return SOAP_OK;
  10223. }
  10224. if (soap_getmime(soap))
  10225. return soap->error;
  10226. }
  10227. soap->mime.list = soap->mime.first;
  10228. soap->mime.first = NULL;
  10229. soap->mime.last = NULL;
  10230. soap->mime.boundary = NULL;
  10231. if (soap->xlist) {
  10232. struct soap_multipart *content;
  10233. for (content = soap->mime.list; content; content = content->next)
  10234. soap_resolve_attachment(soap, content);
  10235. }
  10236. #endif
  10237. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "End of receive message ok\n"));
  10238. #ifdef WITH_ZLIB
  10239. if ((soap->mode & SOAP_ENC_ZLIB) && soap->d_stream)
  10240. {
  10241. /* Make sure end of compressed content is reached */
  10242. while (soap->d_stream->next_out != Z_NULL)
  10243. if ((int)soap_get1(soap) == EOF)
  10244. break;
  10245. soap->mode &= ~SOAP_ENC_ZLIB;
  10246. (void)soap_memcpy((void*)soap->buf, sizeof(soap->buf), (const void*)soap->z_buf, sizeof(soap->buf));
  10247. soap->bufidx = (char*)soap->d_stream->next_in - soap->z_buf;
  10248. soap->buflen = soap->z_buflen;
  10249. soap->zlib_state = SOAP_ZLIB_NONE;
  10250. if (inflateEnd(soap->d_stream) != Z_OK)
  10251. return soap->error = SOAP_ZLIB_ERROR;
  10252. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Inflate end ok\n"));
  10253. #ifdef WITH_GZIP
  10254. if (soap->zlib_in == SOAP_ZLIB_GZIP)
  10255. {
  10256. soap_wchar c;
  10257. short i;
  10258. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Inflate gzip crc check\n"));
  10259. for (i = 0; i < 8; i++)
  10260. {
  10261. if ((int)(c = soap_get1(soap)) == EOF)
  10262. {
  10263. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Gzip error: unable to read crc value\n"));
  10264. return soap->error = SOAP_ZLIB_ERROR;
  10265. }
  10266. soap->z_buf[i] = (char)c;
  10267. }
  10268. if (soap->z_crc != ((uLong)(unsigned char)soap->z_buf[0] | ((uLong)(unsigned char)soap->z_buf[1] << 8) | ((uLong)(unsigned char)soap->z_buf[2] << 16) | ((uLong)(unsigned char)soap->z_buf[3] << 24)))
  10269. {
  10270. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Gzip inflate error: crc check failed, message corrupted? (crc32=%lu)\n", (unsigned long)soap->z_crc));
  10271. return soap->error = SOAP_ZLIB_ERROR;
  10272. }
  10273. if (soap->d_stream->total_out != ((uLong)(unsigned char)soap->z_buf[4] | ((uLong)(unsigned char)soap->z_buf[5] << 8) | ((uLong)(unsigned char)soap->z_buf[6] << 16) | ((uLong)(unsigned char)soap->z_buf[7] << 24)))
  10274. {
  10275. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Gzip inflate error: incorrect message length\n"));
  10276. return soap->error = SOAP_ZLIB_ERROR;
  10277. }
  10278. }
  10279. soap->zlib_in = SOAP_ZLIB_NONE;
  10280. #endif
  10281. }
  10282. #endif
  10283. if ((soap->mode & SOAP_IO) == SOAP_IO_CHUNK)
  10284. while ((int) soap->ahead != EOF && !soap_recv_raw(soap))
  10285. continue;
  10286. #ifndef WITH_NOIDREF
  10287. if (soap_resolve(soap))
  10288. return soap->error;
  10289. #endif
  10290. #ifndef WITH_LEANER
  10291. if (soap->xlist) {
  10292. if ((soap->mode & SOAP_ENC_MTOM))
  10293. return soap->error = SOAP_MIME_HREF;
  10294. return soap->error = SOAP_DIME_HREF;
  10295. }
  10296. #endif
  10297. soap_free_ns(soap);
  10298. #ifndef WITH_LEANER
  10299. if (soap->fpreparefinalrecv)
  10300. return soap->error = soap->fpreparefinalrecv(soap);
  10301. #endif
  10302. return SOAP_OK;
  10303. }
  10304. /******************************************************************************/
  10305. SOAP_FMAC1
  10306. void
  10307. SOAP_FMAC2
  10308. soap_free_temp(struct soap *soap) {
  10309. struct soap_attribute *tp, *tq;
  10310. struct Namespace *ns;
  10311. soap_free_ns(soap);
  10312. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Free any remaining temp blocks\n"));
  10313. while (soap->blist)
  10314. soap_end_block(soap, NULL);
  10315. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Free attribute storage\n"));
  10316. for (tp = soap->attributes; tp; tp = tq) {
  10317. tq = tp->next;
  10318. if (tp->value)
  10319. SOAP_FREE(soap, tp->value);
  10320. SOAP_FREE(soap, tp);
  10321. }
  10322. soap->attributes = NULL;
  10323. #ifdef WITH_FAST
  10324. if (soap->labbuf)
  10325. SOAP_FREE(soap, soap->labbuf);
  10326. soap->labbuf = NULL;
  10327. soap->lablen = 0;
  10328. soap->labidx = 0;
  10329. #endif
  10330. ns = soap->local_namespaces;
  10331. if (ns) {
  10332. for (; ns->id; ns++) {
  10333. if (ns->out) {
  10334. SOAP_FREE(soap, ns->out);
  10335. ns->out = NULL;
  10336. }
  10337. }
  10338. SOAP_FREE(soap, soap->local_namespaces);
  10339. soap->local_namespaces = NULL;
  10340. }
  10341. #ifndef WITH_LEANER
  10342. while (soap->xlist) {
  10343. struct soap_xlist *xp = soap->xlist->next;
  10344. SOAP_FREE(soap, soap->xlist);
  10345. soap->xlist = xp;
  10346. }
  10347. #endif
  10348. #ifndef WITH_NOIDREF
  10349. soap_free_iht(soap);
  10350. #endif
  10351. soap_free_pht(soap);
  10352. }
  10353. /******************************************************************************/
  10354. static void
  10355. soap_free_ns(struct soap *soap) {
  10356. struct soap_nlist *np, *nq;
  10357. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Free namespace stack\n"));
  10358. for (np = soap->nlist; np; np = nq) {
  10359. nq = np->next;
  10360. SOAP_FREE(soap, np);
  10361. }
  10362. soap->nlist = NULL;
  10363. }
  10364. /******************************************************************************/
  10365. #ifdef SOAP_DEBUG
  10366. static void
  10367. soap_init_logs(struct soap *soap)
  10368. {
  10369. int i;
  10370. for (i = 0; i < SOAP_MAXLOGS; i++)
  10371. {
  10372. soap->logfile[i] = NULL;
  10373. soap->fdebug[i] = NULL;
  10374. }
  10375. }
  10376. #endif
  10377. /******************************************************************************/
  10378. #ifdef SOAP_DEBUG
  10379. SOAP_FMAC1
  10380. void
  10381. SOAP_FMAC2
  10382. soap_open_logfile(struct soap *soap, int i)
  10383. {
  10384. if (soap->logfile[i])
  10385. soap->fdebug[i] = fopen(soap->logfile[i], i < 2 ? "ab" : "a");
  10386. }
  10387. #endif
  10388. /******************************************************************************/
  10389. #ifdef SOAP_DEBUG
  10390. static void
  10391. soap_close_logfile(struct soap *soap, int i)
  10392. {
  10393. if (soap->fdebug[i])
  10394. {
  10395. fclose(soap->fdebug[i]);
  10396. soap->fdebug[i] = NULL;
  10397. }
  10398. }
  10399. #endif
  10400. /******************************************************************************/
  10401. #ifdef SOAP_DEBUG
  10402. SOAP_FMAC1
  10403. void
  10404. SOAP_FMAC2
  10405. soap_close_logfiles(struct soap *soap)
  10406. {
  10407. int i;
  10408. for (i = 0; i < SOAP_MAXLOGS; i++)
  10409. soap_close_logfile(soap, i);
  10410. }
  10411. #endif
  10412. /******************************************************************************/
  10413. #ifdef SOAP_DEBUG
  10414. static void
  10415. soap_set_logfile(struct soap *soap, int i, const char *logfile)
  10416. {
  10417. const char *s;
  10418. char *t = NULL;
  10419. soap_close_logfile(soap, i);
  10420. s = soap->logfile[i];
  10421. if (s)
  10422. SOAP_FREE_UNMANAGED(s);
  10423. if (logfile)
  10424. {
  10425. size_t l = strlen(logfile) + 1;
  10426. t = (char*)SOAP_MALLOC_UNMANAGED(l);
  10427. if (t)
  10428. (void)soap_memcpy((void*)t, l, (const void*)logfile, l);
  10429. }
  10430. soap->logfile[i] = t;
  10431. }
  10432. #endif
  10433. /******************************************************************************/
  10434. SOAP_FMAC1
  10435. void
  10436. SOAP_FMAC2
  10437. soap_set_recv_logfile(struct soap *soap, const char *logfile) {
  10438. (void) soap;
  10439. (void) logfile;
  10440. #ifdef SOAP_DEBUG
  10441. soap_set_logfile(soap, SOAP_INDEX_RECV, logfile);
  10442. #endif
  10443. }
  10444. /******************************************************************************/
  10445. SOAP_FMAC1
  10446. void
  10447. SOAP_FMAC2
  10448. soap_set_sent_logfile(struct soap *soap, const char *logfile) {
  10449. (void) soap;
  10450. (void) logfile;
  10451. #ifdef SOAP_DEBUG
  10452. soap_set_logfile(soap, SOAP_INDEX_SENT, logfile);
  10453. #endif
  10454. }
  10455. /******************************************************************************/
  10456. SOAP_FMAC1
  10457. void
  10458. SOAP_FMAC2
  10459. soap_set_test_logfile(struct soap *soap, const char *logfile) {
  10460. (void) soap;
  10461. (void) logfile;
  10462. #ifdef SOAP_DEBUG
  10463. soap_set_logfile(soap, SOAP_INDEX_TEST, logfile);
  10464. #endif
  10465. }
  10466. /******************************************************************************/
  10467. SOAP_FMAC1
  10468. struct soap *
  10469. SOAP_FMAC2
  10470. soap_copy(const struct soap *soap) {
  10471. struct soap *copy = soap_versioning(soap_new)(SOAP_IO_DEFAULT, SOAP_IO_DEFAULT);
  10472. soap_done(copy);
  10473. if (soap_copy_context(copy, soap) != NULL)
  10474. return copy;
  10475. soap_free(copy);
  10476. return NULL;
  10477. }
  10478. /******************************************************************************/
  10479. SOAP_FMAC1
  10480. struct soap *
  10481. SOAP_FMAC2
  10482. soap_copy_context(struct soap *copy, const struct soap *soap) {
  10483. if (copy == soap)
  10484. return copy;
  10485. if (soap_check_state(soap))
  10486. return NULL;
  10487. if (copy) {
  10488. struct soap_plugin *p = NULL;
  10489. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Copying context\n"));
  10490. (void) soap_memcpy((void *) copy, sizeof(struct soap), (const void *) soap, sizeof(struct soap));
  10491. copy->state = SOAP_COPY;
  10492. copy->error = SOAP_OK;
  10493. copy->bearer = NULL;
  10494. copy->userid = NULL;
  10495. copy->passwd = NULL;
  10496. #ifdef WITH_NTLM
  10497. copy->ntlm_challenge = NULL;
  10498. #endif
  10499. copy->nlist = NULL;
  10500. copy->blist = NULL;
  10501. copy->clist = NULL;
  10502. copy->alist = NULL;
  10503. copy->attributes = NULL;
  10504. copy->labbuf = NULL;
  10505. copy->lablen = 0;
  10506. copy->labidx = 0;
  10507. #ifdef SOAP_MEM_DEBUG
  10508. soap_init_mht(copy);
  10509. #endif
  10510. #ifdef SOAP_DEBUG
  10511. soap_init_logs(copy);
  10512. soap_set_test_logfile(copy, soap->logfile[SOAP_INDEX_TEST]);
  10513. soap_set_sent_logfile(copy, soap->logfile[SOAP_INDEX_SENT]);
  10514. soap_set_recv_logfile(copy, soap->logfile[SOAP_INDEX_RECV]);
  10515. #endif
  10516. copy->namespaces = soap->local_namespaces;
  10517. copy->local_namespaces = NULL;
  10518. soap_set_local_namespaces(copy); /* copy content of soap->local_namespaces */
  10519. copy->namespaces = soap->namespaces; /* point to shared read-only namespaces table */
  10520. copy->c_locale = NULL;
  10521. #ifdef WITH_OPENSSL
  10522. copy->bio = NULL;
  10523. copy->ssl = NULL;
  10524. copy->session = NULL;
  10525. copy->session_host[0] = '\0';
  10526. copy->session_port = 443;
  10527. #endif
  10528. #ifdef WITH_GNUTLS
  10529. copy->session = NULL;
  10530. #endif
  10531. #ifdef WITH_ZLIB
  10532. copy->d_stream = NULL;
  10533. copy->z_buf = NULL;
  10534. #endif
  10535. #ifndef WITH_NOIDREF
  10536. soap_init_iht(copy);
  10537. #endif
  10538. soap_init_pht(copy);
  10539. copy->header = NULL;
  10540. copy->fault = NULL;
  10541. copy->action = NULL;
  10542. #ifndef WITH_LEAN
  10543. #ifdef WITH_COOKIES
  10544. copy->cookies = soap_copy_cookies(copy, soap);
  10545. #else
  10546. copy->cookies = NULL;
  10547. #endif
  10548. #endif
  10549. copy->plugins = NULL;
  10550. for (p = soap->plugins; p; p = p->next) {
  10551. struct soap_plugin *q = (struct soap_plugin *) SOAP_MALLOC(copy, sizeof(struct soap_plugin));
  10552. if (!q) {
  10553. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Could not allocate plugin '%s'\n", p->id));
  10554. soap_end(copy);
  10555. soap_done(copy);
  10556. return NULL;
  10557. }
  10558. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Copying plugin '%s'\n", p->id));
  10559. *q = *p;
  10560. if (p->fcopy && (copy->error = p->fcopy(copy, q, p)) != SOAP_OK) {
  10561. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Could not copy plugin '%s' error = %d\n", p->id, copy->error));
  10562. SOAP_FREE(copy, q);
  10563. soap_end(copy);
  10564. soap_done(copy);
  10565. return NULL;
  10566. }
  10567. q->next = copy->plugins;
  10568. copy->plugins = q;
  10569. }
  10570. }
  10571. #ifdef WITH_SELF_PIPE
  10572. pipe(copy->pipe_fd);
  10573. SOAP_SOCKNONBLOCK(copy->pipe_fd[0])
  10574. SOAP_SOCKNONBLOCK(copy->pipe_fd[1])
  10575. #endif
  10576. return copy;
  10577. }
  10578. /******************************************************************************/
  10579. SOAP_FMAC1
  10580. void
  10581. SOAP_FMAC2
  10582. soap_copy_stream(struct soap *copy, struct soap *soap) {
  10583. struct soap_attribute *tp = NULL, *tq;
  10584. if (copy == soap)
  10585. return;
  10586. copy->header = soap->header;
  10587. copy->mode = soap->mode;
  10588. copy->imode = soap->imode;
  10589. copy->omode = soap->omode;
  10590. copy->socket = soap->socket;
  10591. copy->sendsk = soap->sendsk;
  10592. copy->recvsk = soap->recvsk;
  10593. copy->transfer_timeout = soap->transfer_timeout;
  10594. copy->recv_maxlength = soap->recv_maxlength;
  10595. copy->recv_timeout = soap->recv_timeout;
  10596. copy->send_timeout = soap->send_timeout;
  10597. copy->connect_timeout = soap->connect_timeout;
  10598. copy->accept_timeout = soap->accept_timeout;
  10599. copy->socket_flags = soap->socket_flags;
  10600. copy->connect_flags = soap->connect_flags;
  10601. copy->bind_flags = soap->bind_flags;
  10602. copy->bind_inet6 = soap->bind_inet6;
  10603. copy->bind_v6only = soap->bind_v6only;
  10604. copy->accept_flags = soap->accept_flags;
  10605. copy->sndbuf = soap->sndbuf;
  10606. copy->rcvbuf = soap->rcvbuf;
  10607. copy->linger_time = soap->linger_time;
  10608. copy->maxlevel = soap->maxlevel;
  10609. copy->maxlength = soap->maxlength;
  10610. copy->maxoccurs = soap->maxoccurs;
  10611. copy->os = soap->os;
  10612. copy->is = soap->is;
  10613. copy->sendfd = soap->sendfd;
  10614. copy->recvfd = soap->recvfd;
  10615. copy->bufidx = soap->bufidx;
  10616. copy->buflen = soap->buflen;
  10617. copy->ahead = soap->ahead;
  10618. copy->cdata = soap->cdata;
  10619. copy->chunksize = soap->chunksize;
  10620. copy->chunkbuflen = soap->chunkbuflen;
  10621. copy->keep_alive = soap->keep_alive;
  10622. copy->tcp_keep_alive = soap->tcp_keep_alive;
  10623. copy->tcp_keep_idle = soap->tcp_keep_idle;
  10624. copy->tcp_keep_intvl = soap->tcp_keep_intvl;
  10625. copy->tcp_keep_cnt = soap->tcp_keep_cnt;
  10626. copy->max_keep_alive = soap->max_keep_alive;
  10627. #ifndef WITH_NOIO
  10628. copy->peer = soap->peer;
  10629. copy->peerlen = soap->peerlen;
  10630. copy->ip = soap->ip;
  10631. copy->ip6[0] = soap->ip6[0];
  10632. copy->ip6[1] = soap->ip6[1];
  10633. copy->ip6[2] = soap->ip6[2];
  10634. copy->ip6[3] = soap->ip6[3];
  10635. copy->port = soap->port;
  10636. (void) soap_memcpy((void *) copy->host, sizeof(copy->host), (const void *) soap->host, sizeof(soap->host));
  10637. (void) soap_memcpy((void *) copy->endpoint, sizeof(copy->endpoint), (const void *) soap->endpoint,
  10638. sizeof(soap->endpoint));
  10639. #endif
  10640. #ifdef WITH_OPENSSL
  10641. copy->bio = soap->bio;
  10642. copy->ctx = soap->ctx;
  10643. copy->ssl = soap->ssl;
  10644. #endif
  10645. #ifdef WITH_GNUTLS
  10646. copy->session = soap->session;
  10647. #endif
  10648. #ifdef WITH_SYSTEMSSL
  10649. copy->ctx = soap->ctx;
  10650. copy->ssl = soap->ssl;
  10651. #endif
  10652. #ifdef WITH_ZLIB
  10653. copy->zlib_state = soap->zlib_state;
  10654. copy->zlib_in = soap->zlib_in;
  10655. copy->zlib_out = soap->zlib_out;
  10656. if (soap->d_stream && soap->zlib_state != SOAP_ZLIB_NONE)
  10657. {
  10658. if (!copy->d_stream)
  10659. copy->d_stream = (z_stream*)SOAP_MALLOC(copy, sizeof(z_stream));
  10660. if (copy->d_stream)
  10661. (void)soap_memcpy((void*)copy->d_stream, sizeof(z_stream), (const void*)soap->d_stream, sizeof(z_stream));
  10662. }
  10663. copy->z_crc = soap->z_crc;
  10664. copy->z_ratio_in = soap->z_ratio_in;
  10665. copy->z_ratio_out = soap->z_ratio_out;
  10666. copy->z_buf = NULL;
  10667. copy->z_buflen = soap->z_buflen;
  10668. copy->z_level = soap->z_level;
  10669. if (soap->z_buf && soap->zlib_state != SOAP_ZLIB_NONE)
  10670. {
  10671. copy->z_buf = (char*)SOAP_MALLOC(copy, sizeof(soap->buf));
  10672. if (copy->z_buf)
  10673. (void)soap_memcpy((void*)copy->z_buf, sizeof(soap->buf), (const void*)soap->z_buf, sizeof(soap->buf));
  10674. }
  10675. copy->z_dict = soap->z_dict;
  10676. copy->z_dict_len = soap->z_dict_len;
  10677. #endif
  10678. (void) soap_memcpy((void *) copy->buf, sizeof(copy->buf), (const void *) soap->buf, sizeof(soap->buf));
  10679. /* copy XML parser state */
  10680. soap_free_ns(copy);
  10681. soap_set_local_namespaces(copy);
  10682. copy->version = soap->version;
  10683. if (soap->nlist && soap->local_namespaces) {
  10684. struct soap_nlist *np = NULL, *nq;
  10685. /* copy reversed nlist */
  10686. for (nq = soap->nlist; nq; nq = nq->next) {
  10687. struct soap_nlist *nr = np;
  10688. size_t n = sizeof(struct soap_nlist) + strlen(nq->id);
  10689. np = (struct soap_nlist *) SOAP_MALLOC(copy, n);
  10690. if (!np) {
  10691. np = nr;
  10692. break;
  10693. }
  10694. (void) soap_memcpy((void *) np, n, (const void *) nq, n);
  10695. np->next = nr;
  10696. }
  10697. while (np) {
  10698. const char *s = np->ns;
  10699. copy->level = np->level; /* preserve element nesting level */
  10700. if (!s && np->index >= 0) {
  10701. s = soap->local_namespaces[np->index].out;
  10702. if (!s)
  10703. s = soap->local_namespaces[np->index].ns;
  10704. }
  10705. if (s)
  10706. (void) soap_push_namespace(copy, np->id, s);
  10707. nq = np;
  10708. np = np->next;
  10709. SOAP_FREE(copy, nq);
  10710. }
  10711. }
  10712. (void) soap_memcpy((void *) copy->tag, sizeof(copy->tag), (const void *) soap->tag, sizeof(soap->tag));
  10713. (void) soap_memcpy((void *) copy->id, sizeof(copy->id), (const void *) soap->id, sizeof(soap->id));
  10714. (void) soap_memcpy((void *) copy->href, sizeof(copy->href), (const void *) soap->href, sizeof(soap->href));
  10715. (void) soap_memcpy((void *) copy->type, sizeof(copy->type), (const void *) soap->type, sizeof(soap->type));
  10716. copy->other = soap->other;
  10717. copy->root = soap->root;
  10718. copy->null = soap->null;
  10719. copy->body = soap->body;
  10720. copy->part = soap->part;
  10721. copy->mustUnderstand = soap->mustUnderstand;
  10722. copy->level = soap->level;
  10723. copy->peeked = soap->peeked;
  10724. /* copy attributes */
  10725. for (tq = soap->attributes; tq; tq = tq->next) {
  10726. struct soap_attribute *tr = tp;
  10727. size_t n = sizeof(struct soap_attribute) + strlen(tq->name);
  10728. tp = (struct soap_attribute *) SOAP_MALLOC(copy, n);
  10729. (void) soap_memcpy((void *) tp, n, (const void *) tq, n);
  10730. if (tp->size) {
  10731. tp->value = (char *) SOAP_MALLOC(copy, tp->size);
  10732. if (tp->value)
  10733. (void) soap_memcpy((void *) tp->value, tp->size, (const void *) tq->value, tp->size);
  10734. }
  10735. tp->ns = NULL;
  10736. tp->next = tr;
  10737. }
  10738. copy->attributes = tp;
  10739. }
  10740. /******************************************************************************/
  10741. SOAP_FMAC1
  10742. void
  10743. SOAP_FMAC2
  10744. soap_free_stream(struct soap *soap) {
  10745. soap->socket = SOAP_INVALID_SOCKET;
  10746. soap->sendsk = SOAP_INVALID_SOCKET;
  10747. soap->recvsk = SOAP_INVALID_SOCKET;
  10748. #ifdef WITH_OPENSSL
  10749. soap->bio = NULL;
  10750. soap->ctx = NULL;
  10751. soap->ssl = NULL;
  10752. #endif
  10753. #ifdef WITH_GNUTLS
  10754. soap->xcred = NULL;
  10755. soap->acred = NULL;
  10756. soap->cache = NULL;
  10757. soap->session = NULL;
  10758. soap->dh_params = NULL;
  10759. soap->rsa_params = NULL;
  10760. #endif
  10761. #ifdef WITH_SYSTEMSSL
  10762. soap->ctx = (gsk_handle)NULL;
  10763. soap->ssl = (gsk_handle)NULL;
  10764. #endif
  10765. #ifdef WITH_ZLIB
  10766. if (soap->z_buf)
  10767. SOAP_FREE(soap, soap->z_buf);
  10768. soap->z_buf = NULL;
  10769. #endif
  10770. }
  10771. /******************************************************************************/
  10772. SOAP_FMAC1
  10773. void
  10774. SOAP_FMAC2
  10775. soap_initialize(struct soap *soap) {
  10776. soap_versioning(soap_init)(soap, SOAP_IO_DEFAULT, SOAP_IO_DEFAULT);
  10777. }
  10778. /******************************************************************************/
  10779. SOAP_FMAC1
  10780. void
  10781. SOAP_FMAC2
  10782. soap_versioning(soap_init)(struct soap *soap, soap_mode imode, soap_mode omode) {
  10783. size_t i;
  10784. soap->state = SOAP_INIT;
  10785. #ifdef SOAP_MEM_DEBUG
  10786. soap_init_mht(soap);
  10787. #endif
  10788. #ifdef SOAP_DEBUG
  10789. soap_init_logs(soap);
  10790. #endif
  10791. #ifdef TANDEM_NONSTOP
  10792. soap_set_test_logfile(soap, "TESTLOG");
  10793. soap_set_sent_logfile(soap, "SENTLOG");
  10794. soap_set_recv_logfile(soap, "RECVLOG");
  10795. #else
  10796. soap_set_test_logfile(soap, "TEST.log");
  10797. soap_set_sent_logfile(soap, "SENT.log");
  10798. soap_set_recv_logfile(soap, "RECV.log");
  10799. #endif
  10800. #ifdef WITH_SELF_PIPE
  10801. pipe(soap->pipe_fd);
  10802. SOAP_SOCKNONBLOCK(soap->pipe_fd[0])
  10803. SOAP_SOCKNONBLOCK(soap->pipe_fd[1])
  10804. #endif
  10805. soap->version = 0;
  10806. soap->imode = imode;
  10807. soap->omode = omode;
  10808. soap->mode = imode;
  10809. soap->plugins = NULL;
  10810. soap->user = NULL;
  10811. for (i = 0; i < sizeof(soap->data) / sizeof(*soap->data); i++)
  10812. soap->data[i] = NULL;
  10813. soap->bearer = NULL;
  10814. soap->userid = NULL;
  10815. soap->passwd = NULL;
  10816. soap->authrealm = NULL;
  10817. #ifdef WITH_NTLM
  10818. soap->ntlm_challenge = NULL;
  10819. #endif
  10820. #ifndef WITH_NOHTTP
  10821. soap->fpost = http_post;
  10822. soap->fget = http_get;
  10823. soap->fput = http_put;
  10824. soap->fpatch = http_patch;
  10825. soap->fdel = http_del;
  10826. soap->fopt = http_200;
  10827. soap->fhead = http_200;
  10828. soap->fform = NULL;
  10829. soap->fposthdr = http_post_header;
  10830. soap->fresponse = http_response;
  10831. soap->fparse = http_parse;
  10832. soap->fparsehdr = http_parse_header;
  10833. #endif
  10834. soap->fheader = NULL;
  10835. soap->fconnect = NULL;
  10836. soap->fdisconnect = NULL;
  10837. #ifndef WITH_NOIO
  10838. soap->ipv6_multicast_if = 0; /* in_addr_t value */
  10839. soap->ipv4_multicast_if = NULL; /* points to struct in_addr or in_addr_t */
  10840. soap->ipv4_multicast_ttl = 0; /* 0: use default */
  10841. soap->client_port = -1; /* client port to bind, -1 for none */
  10842. soap->client_interface = NULL; /* client interface address, NULL for none */
  10843. #ifndef WITH_IPV6
  10844. soap->fresolve = tcp_gethost;
  10845. #else
  10846. soap->fresolve = NULL;
  10847. #endif
  10848. soap->faccept = tcp_accept;
  10849. soap->fopen = tcp_connect;
  10850. soap->fclose = tcp_disconnect;
  10851. soap->fclosesocket = tcp_closesocket;
  10852. soap->fshutdownsocket = tcp_shutdownsocket;
  10853. soap->fsend = fsend;
  10854. soap->frecv = frecv;
  10855. soap->fpoll = soap_poll;
  10856. #else
  10857. soap->fopen = NULL;
  10858. soap->fclose = NULL;
  10859. soap->fpoll = NULL;
  10860. #endif
  10861. soap->fseterror = NULL;
  10862. soap->fignore = NULL;
  10863. soap->fserveloop = NULL;
  10864. soap->fplugin = fplugin;
  10865. #ifndef WITH_LEANER
  10866. soap->fsvalidate = NULL;
  10867. soap->fwvalidate = NULL;
  10868. soap->feltbegin = NULL;
  10869. soap->feltendin = NULL;
  10870. soap->feltbegout = NULL;
  10871. soap->feltendout = NULL;
  10872. soap->fprepareinitsend = NULL;
  10873. soap->fprepareinitrecv = NULL;
  10874. soap->fpreparesend = NULL;
  10875. soap->fpreparerecv = NULL;
  10876. soap->fpreparefinalsend = NULL;
  10877. soap->fpreparefinalrecv = NULL;
  10878. soap->ffiltersend = NULL;
  10879. soap->ffilterrecv = NULL;
  10880. soap->fdimereadopen = NULL;
  10881. soap->fdimewriteopen = NULL;
  10882. soap->fdimereadclose = NULL;
  10883. soap->fdimewriteclose = NULL;
  10884. soap->fdimeread = NULL;
  10885. soap->fdimewrite = NULL;
  10886. soap->fmimereadopen = NULL;
  10887. soap->fmimewriteopen = NULL;
  10888. soap->fmimereadclose = NULL;
  10889. soap->fmimewriteclose = NULL;
  10890. soap->fmimeread = NULL;
  10891. soap->fmimewrite = NULL;
  10892. #endif
  10893. soap->float_format = "%.9G"; /* Alternative: use "%G" */
  10894. soap->double_format = "%.17lG"; /* Alternative: use "%lG" */
  10895. soap->long_double_format = NULL; /* Defined in custom serializer custom/long_double.c */
  10896. soap->dime_id_format = "cid:id%d"; /* default DIME id format for int id index */
  10897. soap->recv_maxlength = 0x7FFFFFFF; /* default max length of messages received (2GB) */
  10898. soap->recv_timeout = 0;
  10899. soap->send_timeout = 0;
  10900. soap->transfer_timeout = 0;
  10901. soap->connect_timeout = 0;
  10902. soap->accept_timeout = 0;
  10903. soap->socket_flags = 0;
  10904. soap->connect_flags = 0;
  10905. soap->bind_flags = 0;
  10906. #ifdef WITH_IPV6_V6ONLY
  10907. soap->bind_inet6 = 1;
  10908. soap->bind_v6only = 1;
  10909. #else
  10910. soap->bind_inet6 = 0;
  10911. soap->bind_v6only = 0;
  10912. #endif
  10913. soap->accept_flags = 0;
  10914. #ifdef WIN32
  10915. soap->sndbuf = SOAP_BUFLEN + 1; /* this size speeds up windows xfer */
  10916. soap->rcvbuf = SOAP_BUFLEN + 1;
  10917. #else
  10918. soap->sndbuf = SOAP_BUFLEN;
  10919. soap->rcvbuf = SOAP_BUFLEN;
  10920. #endif
  10921. soap->linger_time = 0;
  10922. soap->maxlevel = SOAP_MAXLEVEL;
  10923. soap->maxlength = SOAP_MAXLENGTH;
  10924. soap->maxoccurs = SOAP_MAXOCCURS;
  10925. soap->http_version = "1.1";
  10926. soap->proxy_http_version = "1.0";
  10927. soap->http_content = NULL;
  10928. soap->http_extra_header = NULL;
  10929. soap->actor = NULL;
  10930. soap->lang = "en";
  10931. soap->keep_alive = 0;
  10932. soap->tcp_keep_alive = 0;
  10933. soap->tcp_keep_idle = 0;
  10934. soap->tcp_keep_intvl = 0;
  10935. soap->tcp_keep_cnt = 0;
  10936. soap->max_keep_alive = SOAP_MAXKEEPALIVE;
  10937. soap->ip = 0;
  10938. soap->ip6[0] = 0;
  10939. soap->ip6[1] = 0;
  10940. soap->ip6[2] = 0;
  10941. soap->ip6[3] = 0;
  10942. soap->labbuf = NULL;
  10943. soap->lablen = 0;
  10944. soap->labidx = 0;
  10945. soap->encodingStyle = NULL;
  10946. #ifndef WITH_NONAMESPACES
  10947. soap->namespaces = namespaces;
  10948. #else
  10949. soap->namespaces = NULL;
  10950. #endif
  10951. soap->local_namespaces = NULL;
  10952. soap->nlist = NULL;
  10953. soap->blist = NULL;
  10954. soap->clist = NULL;
  10955. soap->alist = NULL;
  10956. soap->shaky = 0;
  10957. soap->attributes = NULL;
  10958. soap->header = NULL;
  10959. soap->fault = NULL;
  10960. soap->master = SOAP_INVALID_SOCKET;
  10961. soap->socket = SOAP_INVALID_SOCKET;
  10962. soap->sendsk = SOAP_INVALID_SOCKET;
  10963. soap->recvsk = SOAP_INVALID_SOCKET;
  10964. soap->os = NULL;
  10965. soap->is = NULL;
  10966. #ifndef WITH_LEANER
  10967. soap->dom = NULL;
  10968. soap->dime.list = NULL;
  10969. soap->dime.first = NULL;
  10970. soap->dime.last = NULL;
  10971. soap->mime.list = NULL;
  10972. soap->mime.first = NULL;
  10973. soap->mime.last = NULL;
  10974. soap->mime.boundary = NULL;
  10975. soap->mime.start = NULL;
  10976. soap->xlist = NULL;
  10977. #endif
  10978. #ifndef UNDER_CE
  10979. soap->recvfd = 0;
  10980. soap->sendfd = 1;
  10981. #else
  10982. soap->recvfd = stdin;
  10983. soap->sendfd = stdout;
  10984. #endif
  10985. soap->tag[0] = '\0';
  10986. soap->id[0] = '\0';
  10987. soap->href[0] = '\0';
  10988. soap->type[0] = '\0';
  10989. soap->arrayType[0] = '\0';
  10990. soap->arraySize[0] = '\0';
  10991. soap->arrayOffset[0] = '\0';
  10992. soap->endpoint[0] = '\0';
  10993. soap->host[0] = '\0';
  10994. soap->path[0] = '\0';
  10995. soap->port = 0;
  10996. soap->override_host = NULL;
  10997. soap->override_port = 0;
  10998. soap->action = NULL;
  10999. soap->proxy_host = NULL;
  11000. soap->proxy_port = 8080;
  11001. soap->proxy_userid = NULL;
  11002. soap->proxy_passwd = NULL;
  11003. soap->proxy_from = NULL;
  11004. soap->origin = NULL;
  11005. soap->cors_origin = NULL;
  11006. soap->cors_allow = "*";
  11007. soap->cors_method = NULL;
  11008. soap->cors_header = NULL;
  11009. soap->cors_methods = NULL;
  11010. soap->cors_headers = NULL;
  11011. soap->x_frame_options = "SAMEORIGIN";
  11012. soap->prolog = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
  11013. soap->zlib_state = SOAP_ZLIB_NONE;
  11014. soap->zlib_in = SOAP_ZLIB_NONE;
  11015. soap->zlib_out = SOAP_ZLIB_NONE;
  11016. soap->d_stream = NULL;
  11017. soap->z_buf = NULL;
  11018. soap->z_level = 6;
  11019. soap->z_dict = NULL;
  11020. soap->z_dict_len = 0;
  11021. #ifndef WITH_LEAN
  11022. soap->wsuid = NULL;
  11023. soap->c14nexclude = NULL;
  11024. soap->c14ninclude = NULL;
  11025. soap->cookies = NULL;
  11026. soap->cookie_domain = NULL;
  11027. soap->cookie_path = NULL;
  11028. soap->cookie_max = 32;
  11029. #endif
  11030. #ifdef WMW_RPM_IO
  11031. soap->rpmreqid = NULL;
  11032. #endif
  11033. #ifndef WITH_NOIDREF
  11034. soap_init_iht(soap);
  11035. #endif
  11036. soap_init_pht(soap);
  11037. #ifdef WITH_OPENSSL
  11038. if (!soap_ssl_init_done)
  11039. soap_ssl_init();
  11040. soap->fsslauth = ssl_auth_init;
  11041. soap->fsslverify = NULL;
  11042. soap->bio = NULL;
  11043. soap->ssl = NULL;
  11044. soap->ctx = NULL;
  11045. soap->session = NULL;
  11046. soap->session_host[0] = '\0';
  11047. soap->session_port = 443;
  11048. soap->ssl_flags = SOAP_SSL_DEFAULT;
  11049. soap->keyfile = NULL;
  11050. soap->keyid = NULL;
  11051. soap->password = NULL;
  11052. soap->cafile = NULL;
  11053. soap->capath = NULL;
  11054. soap->crlfile = NULL;
  11055. soap->dhfile = NULL;
  11056. soap->randfile = NULL;
  11057. #endif
  11058. #ifdef WITH_GNUTLS
  11059. if (!soap_ssl_init_done)
  11060. soap_ssl_init();
  11061. soap->fsslauth = ssl_auth_init;
  11062. soap->fsslverify = NULL;
  11063. soap->xcred = NULL;
  11064. soap->acred = NULL;
  11065. soap->cache = NULL;
  11066. soap->session = NULL;
  11067. soap->ssl_flags = SOAP_SSL_DEFAULT;
  11068. soap->keyfile = NULL;
  11069. soap->keyid = NULL;
  11070. soap->password = NULL;
  11071. soap->cafile = NULL;
  11072. soap->capath = NULL;
  11073. soap->crlfile = NULL;
  11074. soap->dh_params = NULL;
  11075. soap->rsa_params = NULL;
  11076. #endif
  11077. #ifdef WITH_SYSTEMSSL
  11078. soap->fsslauth = ssl_auth_init;
  11079. soap->fsslverify = NULL;
  11080. soap->bio = NULL;
  11081. soap->ssl = (gsk_handle)NULL;
  11082. soap->ctx = (gsk_handle)NULL;
  11083. soap->session = NULL;
  11084. soap->ssl_flags = SOAP_SSL_DEFAULT;
  11085. soap->keyfile = NULL;
  11086. soap->keyid = NULL;
  11087. soap->password = NULL;
  11088. soap->cafile = NULL;
  11089. soap->capath = NULL;
  11090. soap->crlfile = NULL;
  11091. soap->dhfile = NULL;
  11092. soap->randfile = NULL;
  11093. #endif
  11094. soap->c_locale = NULL;
  11095. soap->buflen = 0;
  11096. soap->bufidx = 0;
  11097. #ifndef WITH_LEANER
  11098. soap->dime.chunksize = 0;
  11099. soap->dime.buflen = 0;
  11100. #endif
  11101. soap->other = 0;
  11102. soap->root = -1;
  11103. soap->null = 0;
  11104. soap->position = 0;
  11105. soap->encoding = 0;
  11106. soap->mustUnderstand = 0;
  11107. soap->ns = 0;
  11108. soap->part = SOAP_END;
  11109. soap->event = 0;
  11110. soap->evlev = 0;
  11111. soap->alloced = 0;
  11112. soap->count = 0;
  11113. soap->length = 0;
  11114. soap->cdata = 0;
  11115. soap->peeked = 0;
  11116. soap->ahead = 0;
  11117. soap->idnum = 0;
  11118. soap->level = 0;
  11119. soap->status = 0;
  11120. soap->error = SOAP_OK;
  11121. soap->errmode = 0;
  11122. soap->errnum = 0;
  11123. }
  11124. /******************************************************************************/
  11125. SOAP_FMAC1
  11126. void
  11127. SOAP_FMAC2
  11128. soap_begin(struct soap *soap) {
  11129. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Clean up for input/output\n"));
  11130. soap->error = SOAP_OK;
  11131. if (!soap->keep_alive) {
  11132. soap->buflen = 0;
  11133. soap->bufidx = 0;
  11134. }
  11135. soap->encoding = 0;
  11136. soap->mode = 0;
  11137. soap->part = SOAP_END;
  11138. soap->peeked = 0;
  11139. soap->ahead = 0;
  11140. soap->level = 0;
  11141. *soap->endpoint = '\0';
  11142. soap->encodingStyle = SOAP_STR_EOS;
  11143. soap_free_temp(soap);
  11144. }
  11145. /******************************************************************************/
  11146. SOAP_FMAC1
  11147. void
  11148. SOAP_FMAC2
  11149. soap_end(struct soap *soap) {
  11150. if (soap_check_state(soap))
  11151. return;
  11152. soap_free_temp(soap);
  11153. soap_dealloc(soap, NULL);
  11154. while (soap->clist) {
  11155. struct soap_clist *cp = soap->clist->next;
  11156. SOAP_FREE(soap, soap->clist);
  11157. soap->clist = cp;
  11158. }
  11159. soap_closesock(soap);
  11160. #ifdef SOAP_DEBUG
  11161. soap_close_logfiles(soap);
  11162. #endif
  11163. }
  11164. /******************************************************************************/
  11165. SOAP_FMAC1
  11166. void
  11167. SOAP_FMAC2
  11168. soap_set_version(struct soap *soap, short version) {
  11169. soap_set_local_namespaces(soap);
  11170. if (soap->version != version && soap->local_namespaces && soap->local_namespaces[0].id &&
  11171. soap->local_namespaces[1].id) {
  11172. if (version == 1) {
  11173. soap->local_namespaces[0].ns = soap_env1;
  11174. soap->local_namespaces[1].ns = soap_enc1;
  11175. } else if (version == 2) {
  11176. soap->local_namespaces[0].ns = soap_env2;
  11177. soap->local_namespaces[1].ns = soap_enc2;
  11178. }
  11179. soap->version = version;
  11180. }
  11181. if (version == 0)
  11182. soap->encodingStyle = SOAP_STR_EOS;
  11183. else
  11184. soap->encodingStyle = NULL;
  11185. }
  11186. /******************************************************************************/
  11187. static void
  11188. soap_version(struct soap *soap) {
  11189. struct Namespace *p = soap->local_namespaces;
  11190. if (p) {
  11191. const char *ns = p[0].out;
  11192. if (!ns)
  11193. ns = p[0].ns;
  11194. if (ns) {
  11195. if (!strcmp(ns, soap_env1)) {
  11196. soap->version = 1; /* make sure we use SOAP 1.1 */
  11197. if (p[1].out)
  11198. SOAP_FREE(soap, p[1].out);
  11199. p[1].out = (char *) SOAP_MALLOC(soap, sizeof(soap_enc1));
  11200. if (p[1].out)
  11201. (void) soap_memcpy(p[1].out, sizeof(soap_enc1), soap_enc1, sizeof(soap_enc1));
  11202. } else if (!strcmp(ns, soap_env2)) {
  11203. soap->version = 2; /* make sure we use SOAP 1.2 */
  11204. if (p[1].out)
  11205. SOAP_FREE(soap, p[1].out);
  11206. p[1].out = (char *) SOAP_MALLOC(soap, sizeof(soap_enc2));
  11207. if (p[1].out)
  11208. (void) soap_memcpy(p[1].out, sizeof(soap_enc2), soap_enc2, sizeof(soap_enc2));
  11209. }
  11210. }
  11211. }
  11212. }
  11213. /******************************************************************************/
  11214. SOAP_FMAC1
  11215. int
  11216. SOAP_FMAC2
  11217. soap_set_namespaces(struct soap *soap, const struct Namespace *p) {
  11218. struct Namespace *ns = soap->local_namespaces;
  11219. struct soap_nlist *np, *nq, *nr;
  11220. unsigned int level = soap->level;
  11221. soap->namespaces = p;
  11222. soap->local_namespaces = NULL;
  11223. soap_set_local_namespaces(soap);
  11224. /* reverse the namespace list */
  11225. np = soap->nlist;
  11226. soap->nlist = NULL;
  11227. if (np) {
  11228. nq = np->next;
  11229. np->next = NULL;
  11230. while (nq) {
  11231. nr = nq->next;
  11232. nq->next = np;
  11233. np = nq;
  11234. nq = nr;
  11235. }
  11236. }
  11237. /* then push on new stack */
  11238. while (np) {
  11239. const char *s;
  11240. soap->level = np->level; /* preserve element nesting level */
  11241. s = np->ns;
  11242. if (!s && np->index >= 0 && ns) {
  11243. s = ns[np->index].out;
  11244. if (!s)
  11245. s = ns[np->index].ns;
  11246. }
  11247. if (s)
  11248. (void) soap_push_namespace(soap, np->id, s);
  11249. nq = np;
  11250. np = np->next;
  11251. SOAP_FREE(soap, nq);
  11252. }
  11253. if (ns) {
  11254. int i;
  11255. for (i = 0; ns[i].id; i++) {
  11256. if (ns[i].out) {
  11257. SOAP_FREE(soap, ns[i].out);
  11258. ns[i].out = NULL;
  11259. }
  11260. }
  11261. SOAP_FREE(soap, ns);
  11262. }
  11263. soap->level = level; /* restore level */
  11264. return SOAP_OK;
  11265. }
  11266. /******************************************************************************/
  11267. SOAP_FMAC1
  11268. void
  11269. SOAP_FMAC2
  11270. soap_set_local_namespaces(struct soap *soap) {
  11271. if (soap->namespaces && !soap->local_namespaces) {
  11272. const struct Namespace *ns1;
  11273. struct Namespace *ns2;
  11274. size_t n = 1;
  11275. for (ns1 = soap->namespaces; ns1->id; ns1++)
  11276. n++;
  11277. n *= sizeof(struct Namespace);
  11278. ns2 = (struct Namespace *) SOAP_MALLOC(soap, n);
  11279. if (ns2) {
  11280. (void) soap_memcpy((void *) ns2, n, (const void *) soap->namespaces, n);
  11281. if (ns2[0].ns) {
  11282. if (!strcmp(ns2[0].ns, soap_env1))
  11283. soap->version = 1;
  11284. else if (!strcmp(ns2[0].ns, soap_env2))
  11285. soap->version = 2;
  11286. }
  11287. soap->local_namespaces = ns2;
  11288. for (; ns2->id; ns2++)
  11289. ns2->out = NULL;
  11290. }
  11291. }
  11292. }
  11293. /******************************************************************************/
  11294. #ifndef WITH_LEAN
  11295. SOAP_FMAC1
  11296. const char *
  11297. SOAP_FMAC2
  11298. soap_tagsearch(const char *big, const char *little) {
  11299. if (big && little) {
  11300. size_t n = strlen(little);
  11301. const char *s = big;
  11302. while (s) {
  11303. const char *t = s;
  11304. size_t i;
  11305. for (i = 0; i < n; i++, t++) {
  11306. if (*t != little[i])
  11307. break;
  11308. }
  11309. if (*t == '\0' || *t == ' ') {
  11310. if (i == n || (i > 0 && little[i - 1] == ':'))
  11311. return s;
  11312. }
  11313. s = strchr(t, ' ');
  11314. if (s)
  11315. s++;
  11316. }
  11317. }
  11318. return NULL;
  11319. }
  11320. #endif
  11321. /******************************************************************************/
  11322. SOAP_FMAC1
  11323. struct soap_nlist *
  11324. SOAP_FMAC2
  11325. soap_lookup_ns(struct soap *soap, const char *tag, size_t n) {
  11326. struct soap_nlist *np;
  11327. for (np = soap->nlist; np; np = np->next)
  11328. if (!strncmp(np->id, tag, n) && !np->id[n])
  11329. return np;
  11330. return NULL;
  11331. }
  11332. /******************************************************************************/
  11333. #ifndef WITH_LEAN
  11334. static struct soap_nlist *
  11335. soap_push_ns(struct soap *soap, const char *id, const char *ns, short utilized, short isearly) {
  11336. struct soap_nlist *np = NULL;
  11337. size_t n, k;
  11338. unsigned int level = soap->level + isearly;
  11339. if (soap_tagsearch(soap->c14nexclude, id))
  11340. return NULL;
  11341. if (!utilized) {
  11342. for (np = soap->nlist; np; np = np->next) {
  11343. if (!strcmp(np->id, id) && ((!np->ns && *id) || (np->ns && !strcmp(np->ns, ns))))
  11344. break;
  11345. }
  11346. if (np) {
  11347. if ((np->level < level || (!np->ns && *id)) && np->index == 1)
  11348. utilized = 1;
  11349. else
  11350. return NULL;
  11351. }
  11352. }
  11353. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Adding namespace binding (level=%u) '%s' '%s' utilized=%d\n", level, id,
  11354. ns ? ns : "(null)", utilized));
  11355. n = strlen(id);
  11356. if (ns)
  11357. k = strlen(ns) + 1;
  11358. else
  11359. k = 0;
  11360. if (sizeof(struct soap_nlist) + n + k > n &&
  11361. (SOAP_MAXALLOCSIZE <= 0 || sizeof(struct soap_nlist) + n + k <= SOAP_MAXALLOCSIZE))
  11362. np = (struct soap_nlist *) SOAP_MALLOC(soap, sizeof(struct soap_nlist) + n + k);
  11363. if (!np) {
  11364. soap->error = SOAP_EOM;
  11365. return NULL;
  11366. }
  11367. np->next = soap->nlist;
  11368. soap->nlist = np;
  11369. soap_strcpy((char *) np->id, n + 1, id);
  11370. if (ns) {
  11371. np->ns = np->id + n + 1;
  11372. soap_strcpy((char *) np->ns, k, ns);
  11373. } else {
  11374. np->ns = NULL;
  11375. }
  11376. np->level = level;
  11377. np->index = utilized;
  11378. return np;
  11379. }
  11380. #endif
  11381. /******************************************************************************/
  11382. #ifndef WITH_LEAN
  11383. static void
  11384. soap_utilize_ns(struct soap *soap, const char *tag, short isearly) {
  11385. struct soap_nlist *np;
  11386. size_t n = 0;
  11387. if (!strncmp(tag, "xmlns:", 6)) {
  11388. tag += 6;
  11389. n = strlen(tag);
  11390. } else {
  11391. const char *t = strchr(tag, ':');
  11392. if (t)
  11393. n = t - tag;
  11394. }
  11395. np = soap_lookup_ns(soap, tag, n);
  11396. if (np) {
  11397. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Utilizing namespace of '%s' at level %u utilized=%d at level=%u\n", tag,
  11398. soap->level + isearly, np->index, np->level));
  11399. if (np->index <= 0) {
  11400. if (np->level == soap->level + isearly)
  11401. np->index = 1;
  11402. else
  11403. (void) soap_push_ns(soap, np->id, np->ns, 1, isearly);
  11404. }
  11405. } else if (strncmp(tag, "xml", 3)) {
  11406. DBGLOG(TEST,
  11407. SOAP_MESSAGE(fdebug, "Utilizing default namespace of '%s' at level %u\n", tag, soap->level + isearly));
  11408. (void) soap_strncpy(soap->tag, sizeof(soap->tag), tag, n);
  11409. (void) soap_push_ns(soap, soap->tag, NULL, 1, isearly);
  11410. }
  11411. }
  11412. #endif
  11413. /******************************************************************************/
  11414. SOAP_FMAC1
  11415. int
  11416. SOAP_FMAC2
  11417. soap_element(struct soap *soap, const char *tag, int id, const char *type) {
  11418. #ifndef WITH_LEAN
  11419. const char *s;
  11420. #endif
  11421. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Element begin tag='%s' level='%u' id='%d' type='%s'\n", tag, soap->level, id,
  11422. type ? type : SOAP_STR_EOS));
  11423. #ifdef WITH_DOM
  11424. #ifndef WITH_LEAN
  11425. if (soap_tagsearch(soap->wsuid, tag))
  11426. {
  11427. size_t i;
  11428. for (s = tag, i = 0; *s && i < sizeof(soap->href) - 1; s++, i++)
  11429. soap->href[i] = *s == ':' ? '-' : *s;
  11430. soap->href[i] = '\0';
  11431. if (soap_set_attr(soap, "wsu:Id", soap->href, 1))
  11432. return soap->error;
  11433. }
  11434. #endif
  11435. #endif
  11436. soap->level++;
  11437. if (soap->level > soap->maxlevel)
  11438. return soap->error = SOAP_LEVEL;
  11439. #ifdef WITH_DOM
  11440. #ifndef WITH_LEAN
  11441. if ((soap->mode & SOAP_XML_CANONICAL) && !(soap->mode & SOAP_DOM_ASIS))
  11442. {
  11443. if (soap->evlev >= soap->level)
  11444. soap->evlev = 0;
  11445. if (soap->event == SOAP_SEC_BEGIN && !soap->evlev)
  11446. {
  11447. struct soap_nlist *np;
  11448. /* non-nested wsu:Id found: clear xmlns, re-emit them for exc-c14n */
  11449. for (np = soap->nlist; np; np = np->next)
  11450. {
  11451. int p = soap->c14ninclude ? *soap->c14ninclude == '+' || soap_tagsearch(soap->c14ninclude, np->id) != NULL : 0;
  11452. if (np->index == 2 || p)
  11453. {
  11454. struct soap_nlist *np1 = soap_push_ns(soap, np->id, np->ns, 1, 0);
  11455. if (np1 && !p)
  11456. np1->index = 0;
  11457. }
  11458. }
  11459. soap->evlev = soap->level;
  11460. }
  11461. }
  11462. #endif
  11463. if ((soap->mode & SOAP_XML_DOM))
  11464. {
  11465. struct soap_dom_element *elt = (struct soap_dom_element*)soap_malloc(soap, sizeof(struct soap_dom_element));
  11466. if (!elt)
  11467. return soap->error = SOAP_EOM;
  11468. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Adding DOM element tag='%s' %p (parent='%s' %p)\n", tag, elt, soap->dom ? soap->dom->name : "(null)", soap->dom));
  11469. elt->soap = soap;
  11470. elt->next = NULL;
  11471. elt->prnt = soap->dom;
  11472. elt->elts = NULL;
  11473. elt->atts = NULL;
  11474. elt->nstr = NULL;
  11475. elt->name = soap_strdup(soap, tag);
  11476. elt->lead = NULL;
  11477. elt->text = NULL;
  11478. elt->code = NULL;
  11479. elt->tail = NULL;
  11480. elt->node = NULL;
  11481. elt->type = 0;
  11482. if (soap->dom)
  11483. {
  11484. struct soap_dom_element *p = soap->dom->elts;
  11485. if (p)
  11486. {
  11487. while (p->next)
  11488. p = p->next;
  11489. p->next = elt;
  11490. }
  11491. else
  11492. {
  11493. soap->dom->elts = elt;
  11494. }
  11495. }
  11496. soap->dom = elt;
  11497. if (!elt->name)
  11498. return soap->error = SOAP_EOM;
  11499. }
  11500. else
  11501. {
  11502. #endif
  11503. #ifndef WITH_LEAN
  11504. if (!soap->ns) {
  11505. if (!(soap->mode & SOAP_XML_CANONICAL) && soap_send(soap, soap->prolog))
  11506. return soap->error;
  11507. } else if ((soap->mode & SOAP_XML_INDENT)) {
  11508. if (soap->ns == 1 &&
  11509. soap_send_raw(soap, soap_indent, soap->level < sizeof(soap_indent) ? soap->level : sizeof(soap_indent) - 1))
  11510. return soap->error;
  11511. soap->body = 1;
  11512. }
  11513. if ((soap->mode & SOAP_XML_DEFAULTNS)) {
  11514. size_t n = 0;
  11515. s = strchr(tag, ':');
  11516. if (s)
  11517. n = s++ - tag;
  11518. else
  11519. s = tag;
  11520. if (soap_send_raw(soap, "<", 1)
  11521. || soap_send(soap, s))
  11522. return soap->error;
  11523. if (n) {
  11524. struct Namespace *ns = soap->local_namespaces;
  11525. for (; ns && ns->id; ns++) {
  11526. if (*ns->id && ns->ns && !strncmp(ns->id, tag, n) && !ns->id[n]) {
  11527. if (!soap->nlist || *soap->nlist->id || (soap->nlist->ns && strcmp(soap->nlist->ns, ns->ns))) {
  11528. (void) soap_push_ns(soap, SOAP_STR_EOS, ns->out ? ns->out : ns->ns, 0, 0);
  11529. if (soap_attribute(soap, "xmlns", ns->out ? ns->out : ns->ns))
  11530. return soap->error;
  11531. }
  11532. break;
  11533. }
  11534. }
  11535. }
  11536. #ifndef WITH_NOEMPTYNAMESPACES
  11537. else if (!soap->nlist || *soap->nlist->id || (soap->nlist->ns && *soap->nlist->ns)) {
  11538. (void) soap_push_ns(soap, SOAP_STR_EOS, SOAP_STR_EOS, 0, 0);
  11539. if (soap_attribute(soap, "xmlns", SOAP_STR_EOS))
  11540. return soap->error;
  11541. }
  11542. #endif
  11543. } else
  11544. #endif
  11545. if (soap_send_raw(soap, "<", 1)
  11546. || soap_send(soap, tag))
  11547. return soap->error;
  11548. #ifdef WITH_DOM
  11549. }
  11550. #endif
  11551. if (!soap->ns) {
  11552. struct Namespace *ns = soap->local_namespaces;
  11553. int k = -1;
  11554. if (ns) {
  11555. while (k-- && ns->id) {
  11556. const char *t = ns->out;
  11557. if (!t)
  11558. t = ns->ns;
  11559. if (*ns->id && t && *t) {
  11560. (SOAP_SNPRINTF(soap->tmpbuf, sizeof(soap->tmpbuf), strlen(ns->id) + 6), "xmlns:%s", ns->id);
  11561. if (soap_attribute(soap, soap->tmpbuf, t))
  11562. return soap->error;
  11563. }
  11564. ns++;
  11565. }
  11566. }
  11567. }
  11568. soap->ns = 1; /* namespace table control: ns = 0 or 2 to start, then 1 to stop dumping the table */
  11569. #ifndef WITH_LEAN
  11570. if ((soap->mode & SOAP_XML_CANONICAL)) {
  11571. if ((soap->mode & SOAP_XML_DEFAULTNS))
  11572. soap_utilize_ns(soap, SOAP_STR_EOS, 0);
  11573. else
  11574. soap_utilize_ns(soap, tag, 0);
  11575. }
  11576. #endif
  11577. if (id > 0) {
  11578. (SOAP_SNPRINTF(soap->tmpbuf, sizeof(soap->tmpbuf), sizeof(SOAP_BASEREFNAME) + 20), SOAP_BASEREFNAME "%d", id);
  11579. if (soap->version == 2) {
  11580. if (soap_attribute(soap, "SOAP-ENC:id", soap->tmpbuf))
  11581. return soap->error;
  11582. } else if (soap_attribute(soap, "id", soap->tmpbuf)) {
  11583. return soap->error;
  11584. }
  11585. }
  11586. if (type && *type && !(soap->mode & SOAP_XML_NOTYPE)) {
  11587. #ifndef WITH_LEAN
  11588. if ((soap->mode & SOAP_XML_CANONICAL) && !(soap->mode & SOAP_XML_CANONICAL_NA))
  11589. soap_utilize_ns(soap, type, 0);
  11590. #endif
  11591. if (soap_attribute(soap, "xsi:type", type))
  11592. return soap->error;
  11593. }
  11594. if (soap->null && soap->position > 0 && soap->version == 1) {
  11595. int i;
  11596. (SOAP_SNPRINTF(soap->tmpbuf, sizeof(soap->tmpbuf) - 1, 20), "[%d", soap->positions[0]);
  11597. for (i = 1; i < soap->position; i++) {
  11598. size_t l = strlen(soap->tmpbuf);
  11599. (SOAP_SNPRINTF(soap->tmpbuf + l, sizeof(soap->tmpbuf) - l - 1, 20), ",%d", soap->positions[i]);
  11600. }
  11601. soap_strcat(soap->tmpbuf, sizeof(soap->tmpbuf), "]");
  11602. if (soap_attribute(soap, "SOAP-ENC:position", soap->tmpbuf))
  11603. return soap->error;
  11604. }
  11605. if (soap->mustUnderstand) {
  11606. if (soap->actor && *soap->actor) {
  11607. if (soap_attribute(soap, soap->version == 2 ? "SOAP-ENV:role" : "SOAP-ENV:actor", soap->actor))
  11608. return soap->error;
  11609. }
  11610. if (soap_attribute(soap, "SOAP-ENV:mustUnderstand", soap->version == 2 ? "true" : "1"))
  11611. return soap->error;
  11612. soap->mustUnderstand = 0;
  11613. }
  11614. if (soap->encoding) {
  11615. if (soap->encodingStyle && soap->local_namespaces && soap->local_namespaces[0].id &&
  11616. soap->local_namespaces[1].id) {
  11617. if (!*soap->encodingStyle) {
  11618. if (soap->local_namespaces[1].out)
  11619. soap->encodingStyle = soap->local_namespaces[1].out;
  11620. else
  11621. soap->encodingStyle = soap->local_namespaces[1].ns;
  11622. }
  11623. if (soap->encodingStyle && soap_attribute(soap, "SOAP-ENV:encodingStyle", soap->encodingStyle))
  11624. return soap->error;
  11625. } else {
  11626. soap->encodingStyle = NULL;
  11627. }
  11628. soap->encoding = 0;
  11629. }
  11630. soap->null = 0;
  11631. soap->position = 0;
  11632. return SOAP_OK;
  11633. }
  11634. /******************************************************************************/
  11635. SOAP_FMAC1
  11636. int
  11637. SOAP_FMAC2
  11638. soap_element_begin_out(struct soap *soap, const char *tag, int id, const char *type) {
  11639. if (*tag == '-')
  11640. return SOAP_OK;
  11641. #ifdef WITH_DOM
  11642. if (soap->feltbegout)
  11643. return soap->error = soap->feltbegout(soap, tag, id, type);
  11644. #endif
  11645. if (soap_element(soap, tag, id, type))
  11646. return soap->error;
  11647. return soap_element_start_end_out(soap, NULL);
  11648. }
  11649. /******************************************************************************/
  11650. #if _MSC_VER < 1400 && !defined(HAVE_STRLCAT)
  11651. /* concat string (truncating the result, strings must not be NULL) */
  11652. SOAP_FMAC1
  11653. void
  11654. SOAP_FMAC2
  11655. soap_strcat(char *t, size_t n, const char *s) {
  11656. size_t k = strlen(t);
  11657. if (k < n) {
  11658. t += k;
  11659. n -= k;
  11660. while (--n > 0 && *s)
  11661. *t++ = *s++;
  11662. *t = '\0';
  11663. }
  11664. }
  11665. #endif
  11666. /******************************************************************************/
  11667. #if _MSC_VER < 1400
  11668. /* concat string up to m chars (leaves destination intact on overrun and returns nonzero, zero if OK) */
  11669. SOAP_FMAC1
  11670. int
  11671. SOAP_FMAC2
  11672. soap_strncat(char *t, size_t n, const char *s, size_t m) {
  11673. size_t k;
  11674. if (!t || !s)
  11675. return 1;
  11676. k = strlen(t);
  11677. if (n <= k + m)
  11678. return 1;
  11679. t += k;
  11680. n -= k;
  11681. while (--n > 0 && *s)
  11682. *t++ = *s++;
  11683. *t = '\0';
  11684. return 0;
  11685. }
  11686. #endif
  11687. /******************************************************************************/
  11688. #ifndef HAVE_STRRCHR
  11689. SOAP_FMAC1
  11690. char*
  11691. SOAP_FMAC2
  11692. soap_strrchr(const char *s, int t)
  11693. {
  11694. char *r = NULL;
  11695. while (*s)
  11696. if (*s++ == t)
  11697. r = (char*)s - 1;
  11698. return r;
  11699. }
  11700. #endif
  11701. /******************************************************************************/
  11702. #ifndef HAVE_STRTOL
  11703. SOAP_FMAC1
  11704. long
  11705. SOAP_FMAC2
  11706. soap_strtol(const char *s, char **t, int b)
  11707. {
  11708. long n = 0;
  11709. int c;
  11710. while (*s > 0 && *s <= 32)
  11711. s++;
  11712. if (b == 10)
  11713. {
  11714. short neg = 0;
  11715. if (*s == '-')
  11716. {
  11717. s++;
  11718. neg = 1;
  11719. }
  11720. else if (*s == '+')
  11721. {
  11722. s++;
  11723. }
  11724. while ((c = *s) && c >= '0' && c <= '9')
  11725. {
  11726. if (n >= 214748364 && (n > 214748364 || c >= '8'))
  11727. {
  11728. if (neg && n == 214748364 && c == '8')
  11729. {
  11730. if (t)
  11731. *t = (char*)(s + 1);
  11732. return -2147483648;
  11733. }
  11734. break;
  11735. }
  11736. n *= 10;
  11737. n += c - '0';
  11738. s++;
  11739. }
  11740. if (neg)
  11741. n = -n;
  11742. }
  11743. else /* assume b == 16 and value is always positive */
  11744. {
  11745. while ((c = *s))
  11746. {
  11747. if (c >= '0' && c <= '9')
  11748. c -= '0';
  11749. else if (c >= 'A' && c <= 'F')
  11750. c -= 'A' - 10;
  11751. else if (c >= 'a' && c <= 'f')
  11752. c -= 'a' - 10;
  11753. if (n > 0x07FFFFFF)
  11754. break;
  11755. n <<= 4;
  11756. n += c;
  11757. s++;
  11758. }
  11759. }
  11760. if (t)
  11761. *t = (char*)s;
  11762. return n;
  11763. }
  11764. #endif
  11765. /******************************************************************************/
  11766. #ifndef HAVE_STRTOUL
  11767. SOAP_FMAC1
  11768. unsigned long
  11769. SOAP_FMAC2
  11770. soap_strtoul(const char *s, char **t, int b)
  11771. {
  11772. unsigned long n = 0;
  11773. int c;
  11774. while (*s > 0 && *s <= 32)
  11775. s++;
  11776. if (b == 10)
  11777. {
  11778. short neg = 0;
  11779. if (*s == '-')
  11780. {
  11781. s++;
  11782. neg = 1;
  11783. }
  11784. else if (*s == '+')
  11785. {
  11786. s++;
  11787. }
  11788. while ((c = *s) && c >= '0' && c <= '9')
  11789. {
  11790. if (n >= 429496729 && (n > 429496729 || c >= '6'))
  11791. break;
  11792. n *= 10;
  11793. n += c - '0';
  11794. s++;
  11795. }
  11796. if (neg && n > 0)
  11797. s--;
  11798. }
  11799. else /* b == 16 */
  11800. {
  11801. while ((c = *s))
  11802. {
  11803. if (c >= '0' && c <= '9')
  11804. c -= '0';
  11805. else if (c >= 'A' && c <= 'F')
  11806. c -= 'A' - 10;
  11807. else if (c >= 'a' && c <= 'f')
  11808. c -= 'a' - 10;
  11809. if (n > 0x0FFFFFFF)
  11810. break;
  11811. n <<= 4;
  11812. n += c;
  11813. s++;
  11814. }
  11815. }
  11816. if (t)
  11817. *t = (char*)s;
  11818. return n;
  11819. }
  11820. #endif
  11821. /******************************************************************************/
  11822. #ifndef soap_strtoll
  11823. SOAP_FMAC1
  11824. LONG64
  11825. SOAP_FMAC2
  11826. soap_strtoll(const char *s, char **t, int b)
  11827. {
  11828. LONG64 n = 0LL;
  11829. int c;
  11830. while (*s > 0 && *s <= 32)
  11831. s++;
  11832. if (b == 10)
  11833. {
  11834. short neg = 0;
  11835. if (*s == '-')
  11836. {
  11837. s++;
  11838. neg = 1;
  11839. }
  11840. else if (*s == '+')
  11841. {
  11842. s++;
  11843. }
  11844. while ((c = *s) && c >= '0' && c <= '9')
  11845. {
  11846. if (n >= 922337203685477580LL && (n > 922337203685477580LL || c >= '8'))
  11847. {
  11848. if (neg && n == 922337203685477580LL && c == '8')
  11849. {
  11850. if (t)
  11851. *t = (char*)(s + 1);
  11852. return -9223372036854775807LL - 1LL; /* appease compilers that complain */
  11853. }
  11854. break;
  11855. }
  11856. n *= 10LL;
  11857. n += c - '0';
  11858. s++;
  11859. }
  11860. if (neg)
  11861. n = -n;
  11862. }
  11863. else /* assume b == 16 and value is always positive */
  11864. {
  11865. while ((c = *s))
  11866. {
  11867. if (c >= '0' && c <= '9')
  11868. c -= '0';
  11869. else if (c >= 'A' && c <= 'F')
  11870. c -= 'A' - 10;
  11871. else if (c >= 'a' && c <= 'f')
  11872. c -= 'a' - 10;
  11873. if (n > 0x07FFFFFFFFFFFFFFLL)
  11874. break;
  11875. n <<= 4;
  11876. n += c;
  11877. s++;
  11878. }
  11879. }
  11880. if (t)
  11881. *t = (char*)s;
  11882. return n;
  11883. }
  11884. #endif
  11885. /******************************************************************************/
  11886. #ifndef soap_strtoull
  11887. SOAP_FMAC1
  11888. ULONG64
  11889. SOAP_FMAC2
  11890. soap_strtoull(const char *s, char **t, int b)
  11891. {
  11892. ULONG64 n = 0UL;
  11893. int c;
  11894. while (*s > 0 && *s <= 32)
  11895. s++;
  11896. if (b == 10)
  11897. {
  11898. short neg = 0;
  11899. if (*s == '-')
  11900. {
  11901. s++;
  11902. neg = 1;
  11903. }
  11904. else if (*s == '+')
  11905. {
  11906. s++;
  11907. }
  11908. while ((c = *s) && c >= '0' && c <= '9')
  11909. {
  11910. if (n >= 1844674407370955161ULL)
  11911. break;
  11912. n *= 10UL;
  11913. n += c - '0';
  11914. s++;
  11915. }
  11916. if (neg && n > 0UL)
  11917. s--;
  11918. }
  11919. else /* b == 16 */
  11920. {
  11921. while ((c = *s))
  11922. {
  11923. if (c >= '0' && c <= '9')
  11924. c -= '0';
  11925. else if (c >= 'A' && c <= 'F')
  11926. c -= 'A' - 10;
  11927. else if (c >= 'a' && c <= 'f')
  11928. c -= 'a' - 10;
  11929. if (n > 0x0FFFFFFFFFFFFFFFULL)
  11930. break;
  11931. n <<= 4;
  11932. n += c;
  11933. s++;
  11934. }
  11935. }
  11936. if (t)
  11937. *t = (char*)s;
  11938. return n;
  11939. }
  11940. #endif
  11941. /******************************************************************************/
  11942. SOAP_FMAC1
  11943. int
  11944. SOAP_FMAC2
  11945. soap_array_begin_out(struct soap *soap, const char *tag, int id, const char *type, const char *offset) {
  11946. if (!type || !*type || soap->version == 0)
  11947. return soap_element_begin_out(soap, tag, id, NULL);
  11948. if (soap_element(soap, tag, id, NULL))
  11949. return soap->error;
  11950. if (soap->version == 1) {
  11951. if (offset && soap_attribute(soap, "SOAP-ENC:offset", offset))
  11952. return soap->error;
  11953. if (soap_attribute(soap, "SOAP-ENC:arrayType", type))
  11954. return soap->error;
  11955. } else {
  11956. const char *s;
  11957. s = strchr(type, '[');
  11958. if (s && (size_t) (s - type) < sizeof(soap->tmpbuf)) {
  11959. (void) soap_strncpy(soap->tmpbuf, sizeof(soap->tmpbuf), type, s - type);
  11960. if (soap_attribute(soap, "SOAP-ENC:itemType", soap->tmpbuf))
  11961. return soap->error;
  11962. s++;
  11963. if (*s && *s != ']') {
  11964. soap_strcpy(soap->tmpbuf, sizeof(soap->tmpbuf), s);
  11965. soap->tmpbuf[strlen(soap->tmpbuf) - 1] = '\0';
  11966. if (soap_attribute(soap, "SOAP-ENC:arraySize", soap->tmpbuf))
  11967. return soap->error;
  11968. }
  11969. }
  11970. }
  11971. #ifndef WITH_LEAN
  11972. if ((soap->mode & SOAP_XML_CANONICAL) && !(soap->mode & SOAP_XML_CANONICAL_NA))
  11973. soap_utilize_ns(soap, type, 0);
  11974. #endif
  11975. return soap_element_start_end_out(soap, NULL);
  11976. }
  11977. /******************************************************************************/
  11978. SOAP_FMAC1
  11979. int
  11980. SOAP_FMAC2
  11981. soap_element_start_end_out(struct soap *soap, const char *tag) {
  11982. struct soap_attribute *tp;
  11983. #ifndef WITH_LEAN
  11984. if ((soap->mode & SOAP_XML_CANONICAL)) {
  11985. struct soap_nlist *np;
  11986. for (tp = soap->attributes; tp; tp = tp->next) {
  11987. if (tp->visible && *tp->name && strchr(tp->name, ':'))
  11988. soap_utilize_ns(soap, tp->name, 0);
  11989. }
  11990. if (soap->event == SOAP_SEC_BEGIN) {
  11991. for (np = soap->nlist; np; np = np->next)
  11992. if (soap_tagsearch(soap->c14ninclude, np->id))
  11993. (void) soap_push_ns(soap, np->id, np->ns, 1, 0);
  11994. soap->event = 0;
  11995. soap->evlev = 0;
  11996. }
  11997. for (np = soap->nlist; np; np = np->next) {
  11998. if (np->ns && np->index == 1) {
  11999. if (*np->id)
  12000. (SOAP_SNPRINTF(soap->tmpbuf, sizeof(soap->tmpbuf), strlen(np->id) + 6), "xmlns:%s", np->id);
  12001. else
  12002. soap_strcpy(soap->tmpbuf, sizeof(soap->tmpbuf), "xmlns");
  12003. DBGLOG(TEST, SOAP_MESSAGE(fdebug,
  12004. "Enabling utilized binding (level=%u) %s='%s' SEC-BEGIN=%d c14ninclude='%s'\n",
  12005. np->level, soap->tmpbuf, np->ns, soap->event == SOAP_SEC_BEGIN,
  12006. soap->c14ninclude ? soap->c14ninclude : "(null)"));
  12007. np->index = 2;
  12008. soap->level--;
  12009. if (*np->id || *np->ns || soap->level > 1)
  12010. if (soap_set_attr(soap, soap->tmpbuf, np->ns, 1))
  12011. return soap->error;
  12012. soap->level++;
  12013. } else {
  12014. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Binding (level=%u) %s='%s' utilized=%d\n", np->level, np->id, np->ns,
  12015. np->index));
  12016. }
  12017. }
  12018. }
  12019. #endif
  12020. #ifdef WITH_DOM
  12021. if ((soap->mode & SOAP_XML_DOM) && soap->dom)
  12022. {
  12023. struct soap_dom_attribute **att;
  12024. att = &soap->dom->atts;
  12025. for (tp = soap->attributes; tp; tp = tp->next)
  12026. {
  12027. if (tp->visible)
  12028. {
  12029. *att = (struct soap_dom_attribute*)soap_malloc(soap, sizeof(struct soap_dom_attribute));
  12030. if (!*att)
  12031. return soap->error;
  12032. (*att)->next = NULL;
  12033. (*att)->nstr = NULL;
  12034. (*att)->name = soap_strdup(soap, tp->name);
  12035. (*att)->text = soap_strdup(soap, tp->value);
  12036. (*att)->soap = soap;
  12037. if (!(*att)->name || (tp->value && !(*att)->text))
  12038. return soap->error = SOAP_EOM;
  12039. att = &(*att)->next;
  12040. tp->visible = 0;
  12041. }
  12042. }
  12043. return SOAP_OK;
  12044. }
  12045. #endif
  12046. for (tp = soap->attributes; tp; tp = tp->next) {
  12047. if (tp->visible) {
  12048. if (soap_send_raw(soap, " ", 1) || soap_send(soap, tp->name))
  12049. return soap->error;
  12050. if (tp->visible == 2 && tp->value) {
  12051. if (soap_send_raw(soap, "=\"", 2)
  12052. || soap_string_out(soap, tp->value, tp->flag)
  12053. || soap_send_raw(soap, "\"", 1))
  12054. return soap->error;
  12055. } else {
  12056. if (soap_send_raw(soap, "=\"\"", 3))
  12057. return soap->error;
  12058. }
  12059. tp->visible = 0;
  12060. }
  12061. }
  12062. if (tag) {
  12063. #ifndef WITH_LEAN
  12064. if ((soap->mode & SOAP_XML_CANONICAL)) {
  12065. if (soap_send_raw(soap, ">", 1)
  12066. || soap_element_end_out(soap, tag))
  12067. return soap->error;
  12068. return SOAP_OK;
  12069. }
  12070. #endif
  12071. if (soap->nlist)
  12072. soap_pop_namespace(soap);
  12073. soap->level--; /* decrement level just before /> */
  12074. soap->body = 0;
  12075. return soap_send_raw(soap, "/>", 2);
  12076. }
  12077. return soap_send_raw(soap, ">", 1);
  12078. }
  12079. /******************************************************************************/
  12080. SOAP_FMAC1
  12081. int
  12082. SOAP_FMAC2
  12083. soap_element_end_out(struct soap *soap, const char *tag) {
  12084. if (*tag == '-')
  12085. return SOAP_OK;
  12086. #ifdef WITH_DOM
  12087. if (soap->feltendout)
  12088. return soap->error = soap->feltendout(soap, tag);
  12089. #endif
  12090. return soap_element_end(soap, tag);
  12091. }
  12092. /******************************************************************************/
  12093. SOAP_FMAC1
  12094. int
  12095. SOAP_FMAC2
  12096. soap_element_end(struct soap *soap, const char *tag) {
  12097. #ifndef WITH_LEAN
  12098. const char *s;
  12099. #endif
  12100. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Element ending tag='%s'\n", tag));
  12101. #ifdef WITH_DOM
  12102. if ((soap->mode & SOAP_XML_DOM) && soap->dom)
  12103. {
  12104. if (soap->dom->prnt)
  12105. soap->dom = soap->dom->prnt;
  12106. return SOAP_OK;
  12107. }
  12108. #endif
  12109. #ifndef WITH_LEAN
  12110. if (soap->nlist)
  12111. soap_pop_namespace(soap);
  12112. if ((soap->mode & SOAP_XML_INDENT)) {
  12113. if (!soap->body) {
  12114. if (soap_send_raw(soap, soap_indent,
  12115. soap->level < sizeof(soap_indent) ? soap->level : sizeof(soap_indent) - 1))
  12116. return soap->error;
  12117. }
  12118. soap->body = 0;
  12119. }
  12120. if ((soap->mode & SOAP_XML_DEFAULTNS) && (s = strchr(tag, ':')) != NULL)
  12121. tag = s + 1;
  12122. #endif
  12123. if (soap_send_raw(soap, "</", 2)
  12124. || soap_send(soap, tag))
  12125. return soap->error;
  12126. soap->level--; /* decrement level just before > */
  12127. return soap_send_raw(soap, ">", 1);
  12128. }
  12129. /******************************************************************************/
  12130. SOAP_FMAC1
  12131. int
  12132. SOAP_FMAC2
  12133. soap_element_ref(struct soap *soap, const char *tag, int id, int href) {
  12134. const char *s = "ref";
  12135. int n = 1;
  12136. if (soap->version == 1) {
  12137. s = "href";
  12138. n = 0;
  12139. } else if (soap->version == 2) {
  12140. s = "SOAP-ENC:ref";
  12141. }
  12142. (SOAP_SNPRINTF(soap->href, sizeof(soap->href), sizeof(SOAP_BASEREFNAME) + 21), "#" SOAP_BASEREFNAME "%d", href);
  12143. return soap_element_href(soap, tag, id, s, soap->href + n);
  12144. }
  12145. /******************************************************************************/
  12146. SOAP_FMAC1
  12147. int
  12148. SOAP_FMAC2
  12149. soap_element_href(struct soap *soap, const char *tag, int id, const char *ref, const char *val) {
  12150. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Element '%s' reference %s='%s'\n", tag, ref, val));
  12151. if (soap_element(soap, tag, id, NULL)
  12152. || soap_attribute(soap, ref, val)
  12153. || soap_element_start_end_out(soap, tag))
  12154. return soap->error;
  12155. soap->body = 0;
  12156. return SOAP_OK;
  12157. }
  12158. /******************************************************************************/
  12159. SOAP_FMAC1
  12160. int
  12161. SOAP_FMAC2
  12162. soap_element_null(struct soap *soap, const char *tag, int id, const char *type) {
  12163. struct soap_attribute *tp = NULL;
  12164. for (tp = soap->attributes; tp; tp = tp->next)
  12165. if (tp->visible)
  12166. break;
  12167. if (tp || (soap->version == 2 && soap->position > 0) || id > 0 || (soap->mode & SOAP_XML_NIL)) {
  12168. if (soap_element(soap, tag, id, type)
  12169. || (!tp && soap_attribute(soap, "xsi:nil", "true"))
  12170. || soap_element_start_end_out(soap, tag))
  12171. return soap->error;
  12172. soap->body = 0;
  12173. } else {
  12174. soap->null = 1;
  12175. soap->position = 0;
  12176. soap->mustUnderstand = 0;
  12177. }
  12178. return SOAP_OK;
  12179. }
  12180. /******************************************************************************/
  12181. SOAP_FMAC1
  12182. int
  12183. SOAP_FMAC2
  12184. soap_element_empty(struct soap *soap, const char *tag) {
  12185. if (soap_element(soap, tag, -1, NULL))
  12186. return soap->error;
  12187. return soap_element_start_end_out(soap, tag);
  12188. }
  12189. /******************************************************************************/
  12190. SOAP_FMAC1
  12191. int
  12192. SOAP_FMAC2
  12193. soap_element_nil(struct soap *soap, const char *tag) {
  12194. if (soap_element(soap, tag, -1, NULL)
  12195. || (soap_attribute(soap, "xsi:nil", "true")))
  12196. return soap->error;
  12197. return soap_element_start_end_out(soap, tag);
  12198. }
  12199. /******************************************************************************/
  12200. SOAP_FMAC1
  12201. int
  12202. SOAP_FMAC2
  12203. soap_element_id(struct soap *soap, const char *tag, int id, const void *p, const void *a, int n, const char *type,
  12204. int t, char **mark) {
  12205. (void) a;
  12206. (void) n;
  12207. if (!p) {
  12208. soap->error = soap_element_null(soap, tag, id, type);
  12209. return -1;
  12210. }
  12211. #ifndef WITH_NOIDREF
  12212. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Element_id %p type=%d id=%d\n", p, t, id));
  12213. if ((!soap->encodingStyle && !(soap->mode & SOAP_XML_GRAPH)) || (soap->mode & SOAP_XML_TREE))
  12214. return soap_check_and_mark(soap, p, t, mark);
  12215. if (mark)
  12216. *mark = NULL;
  12217. if (id < -1)
  12218. return soap_embed(soap, p, a, n, t);
  12219. else if (id <= 0) {
  12220. struct soap_plist *pp;
  12221. if (a)
  12222. id = soap_array_pointer_lookup(soap, p, a, n, t, &pp);
  12223. else
  12224. id = soap_pointer_lookup(soap, p, t, &pp);
  12225. if (id) {
  12226. if (soap_is_embedded(soap, pp)) {
  12227. soap_element_ref(soap, tag, 0, id);
  12228. return -1;
  12229. }
  12230. if (soap_is_single(soap, pp))
  12231. return 0;
  12232. soap_set_embedded(soap, pp);
  12233. }
  12234. }
  12235. return id;
  12236. #else
  12237. return soap_check_and_mark(soap, p, t, mark);
  12238. #endif
  12239. }
  12240. /******************************************************************************/
  12241. SOAP_FMAC1
  12242. int
  12243. SOAP_FMAC2
  12244. soap_check_and_mark(struct soap *soap, const void *p, int t, char **mark) {
  12245. if (mark) {
  12246. struct soap_plist *pp;
  12247. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Check %p and mark %p\n", p, (void *) mark));
  12248. if (!soap_pointer_lookup(soap, p, t, &pp))
  12249. if (!soap_pointer_enter(soap, p, NULL, 0, t, &pp))
  12250. return -1;
  12251. if ((soap->mode & SOAP_IO_LENGTH)) {
  12252. if (pp->mark1 > 0)
  12253. return -1;
  12254. pp->mark1 = 1;
  12255. *mark = &pp->mark1;
  12256. } else {
  12257. if (pp->mark2 > 0)
  12258. return -1;
  12259. pp->mark2 = 1;
  12260. *mark = &pp->mark2;
  12261. }
  12262. }
  12263. return 0;
  12264. }
  12265. /******************************************************************************/
  12266. SOAP_FMAC1
  12267. void *
  12268. SOAP_FMAC2
  12269. soap_mark_lookup(struct soap *soap, const void *p, int t, struct soap_plist **ppp, char **mark) {
  12270. if (!soap)
  12271. return NULL;
  12272. if (mark || !(soap->mode & SOAP_XML_TREE)) {
  12273. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Mark lookup %p type=%d\n", p, t));
  12274. if (!soap_pointer_lookup(soap, p, t, ppp)) {
  12275. if (!soap_pointer_enter(soap, p, NULL, 0, t, ppp))
  12276. return NULL;
  12277. } else if (!(soap->mode & SOAP_XML_TREE)) {
  12278. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Mark found %p\n", (*ppp)->dup));
  12279. return (*ppp)->dup;
  12280. }
  12281. if (mark) {
  12282. if ((*ppp)->mark1 > 0)
  12283. (*ppp)->mark1 = 2; /* cycle */
  12284. else
  12285. (*ppp)->mark1 = 1; /* cycle detection */
  12286. *mark = &(*ppp)->mark1;
  12287. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Mark cycle %d\n", (*ppp)->mark1));
  12288. }
  12289. }
  12290. return NULL;
  12291. }
  12292. /******************************************************************************/
  12293. SOAP_FMAC1
  12294. int
  12295. SOAP_FMAC2
  12296. soap_mark_cycle(struct soap *soap, struct soap_plist *pp) {
  12297. (void) soap;
  12298. return pp && pp->mark1 == 2 && (soap->mode & SOAP_XML_TREE);
  12299. }
  12300. /******************************************************************************/
  12301. SOAP_FMAC1
  12302. void
  12303. SOAP_FMAC2
  12304. soap_mark_dup(struct soap *soap, void *a, struct soap_plist *pp) {
  12305. (void) soap;
  12306. if (pp)
  12307. pp->dup = a;
  12308. }
  12309. /******************************************************************************/
  12310. SOAP_FMAC1
  12311. void
  12312. SOAP_FMAC2
  12313. soap_unmark(struct soap *soap, char *mark) {
  12314. (void) soap;
  12315. if (mark)
  12316. *mark = 0; /* release detection */
  12317. }
  12318. /******************************************************************************/
  12319. SOAP_FMAC1
  12320. int
  12321. SOAP_FMAC2
  12322. soap_element_result(struct soap *soap, const char *tag) {
  12323. if (soap->version == 2 && soap->encodingStyle) {
  12324. if (soap_element(soap, "SOAP-RPC:result", 0, NULL)
  12325. || soap_attribute(soap, "xmlns:SOAP-RPC", soap_rpc)
  12326. || soap_element_start_end_out(soap, NULL)
  12327. || soap_string_out(soap, tag, 0)
  12328. || soap_element_end_out(soap, "SOAP-RPC:result"))
  12329. return soap->error;
  12330. }
  12331. return SOAP_OK;
  12332. }
  12333. /******************************************************************************/
  12334. SOAP_FMAC1
  12335. void
  12336. SOAP_FMAC2
  12337. soap_check_result(struct soap *soap, const char *tag) {
  12338. (void) tag;
  12339. if (soap->version == 2 && soap->encodingStyle) {
  12340. soap_instring(soap, ":result", NULL, NULL, 0, 2, -1, -1, NULL);
  12341. /* just ignore content for compliance reasons, but should compare tag to element's QName value? */
  12342. }
  12343. }
  12344. /******************************************************************************/
  12345. SOAP_FMAC1
  12346. int
  12347. SOAP_FMAC2
  12348. soap_attribute(struct soap *soap, const char *name, const char *value) {
  12349. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Attribute '%s'='%s'\n", name, value));
  12350. #ifdef WITH_DOM
  12351. if ((soap->mode & SOAP_XML_DOM) && !(soap->mode & SOAP_XML_CANONICAL) && soap->dom)
  12352. {
  12353. struct soap_dom_attribute *a = (struct soap_dom_attribute*)soap_malloc(soap, sizeof(struct soap_dom_attribute));
  12354. if (!a)
  12355. return soap->error;
  12356. a->next = soap->dom->atts;
  12357. a->nstr = NULL;
  12358. a->name = soap_strdup(soap, name);
  12359. a->text = soap_strdup(soap, value);
  12360. a->soap = soap;
  12361. soap->dom->atts = a;
  12362. if (!a->name || (value && !a->text))
  12363. return soap->error = SOAP_EOM;
  12364. return SOAP_OK;
  12365. }
  12366. #endif
  12367. #ifndef WITH_LEAN
  12368. if ((soap->mode & SOAP_XML_CANONICAL)) {
  12369. /* push namespace */
  12370. if (!strncmp(name, "xmlns", 5) && ((name[5] == ':') || name[5] == '\0')) {
  12371. if (name[5] == ':' && soap->c14ninclude &&
  12372. ((*soap->c14ninclude == '*' || soap_tagsearch(soap->c14ninclude, name + 6))))
  12373. soap_utilize_ns(soap, name, 0);
  12374. (void) soap_push_ns(soap, name + 5 + (name[5] == ':'), value, 0, 0);
  12375. } else {
  12376. soap->level--;
  12377. if (soap_set_attr(soap, name, value, 1))
  12378. return soap->error;
  12379. soap->level++;
  12380. }
  12381. } else
  12382. #endif
  12383. {
  12384. if (soap_send_raw(soap, " ", 1)
  12385. || soap_send(soap, name))
  12386. return soap->error;
  12387. if (value)
  12388. if (soap_send_raw(soap, "=\"", 2)
  12389. || soap_string_out(soap, value, 1)
  12390. || soap_send_raw(soap, "\"", 1))
  12391. return soap->error;
  12392. }
  12393. return SOAP_OK;
  12394. }
  12395. /******************************************************************************/
  12396. SOAP_FMAC1
  12397. int
  12398. SOAP_FMAC2
  12399. soap_element_begin_in(struct soap *soap, const char *tag, int nillable, const char *type) {
  12400. if (!soap_peek_element(soap)) {
  12401. if (soap->other)
  12402. return soap->error = SOAP_TAG_MISMATCH;
  12403. if (tag && *tag == '-')
  12404. return SOAP_OK;
  12405. soap->error = soap_match_tag(soap, soap->tag, tag);
  12406. if (!soap->error) {
  12407. if (type && *soap->type && soap_match_tag(soap, soap->type, type))
  12408. return soap->error = SOAP_TYPE;
  12409. soap->peeked = 0;
  12410. if (!nillable && soap->null && (soap->mode & SOAP_XML_STRICT))
  12411. return soap->error = SOAP_NULL;
  12412. if (soap->body) {
  12413. soap->level++;
  12414. if (soap->level > soap->maxlevel)
  12415. return soap->error = SOAP_LEVEL;
  12416. }
  12417. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Begin tag found (level=%u) '%s'='%s'\n", soap->level, soap->tag,
  12418. tag ? tag : SOAP_STR_EOS));
  12419. soap->error = SOAP_OK;
  12420. }
  12421. } else if (soap->error == SOAP_NO_TAG && tag && *tag == '-') {
  12422. soap->error = SOAP_OK;
  12423. }
  12424. return soap->error;
  12425. }
  12426. /******************************************************************************/
  12427. SOAP_FMAC1
  12428. int
  12429. SOAP_FMAC2
  12430. soap_element_end_in(struct soap *soap, const char *tag) {
  12431. soap_wchar c;
  12432. char *s = NULL;
  12433. int n = 0;
  12434. if (tag && *tag == '-')
  12435. return SOAP_OK;
  12436. if (soap->error == SOAP_NO_TAG)
  12437. soap->error = SOAP_OK;
  12438. #ifdef WITH_DOM
  12439. /* this whitespace or mixed content is significant for DOM "as-is" */
  12440. if ((soap->mode & SOAP_XML_DOM) && soap->dom)
  12441. {
  12442. const char *t = soap->dom->code; /* save XML code */
  12443. s = soap_string_in(soap, -1, -1, -1, NULL);
  12444. if (!soap->peeked && !s)
  12445. return soap->error;
  12446. if (soap->dom->prnt)
  12447. soap->dom = soap->dom->prnt;
  12448. if (s && (soap->mode & SOAP_XML_STRICT))
  12449. {
  12450. for (; *s; s++)
  12451. if (!soap_coblank((soap_wchar)*s))
  12452. return soap->error = SOAP_END_TAG; /* reject mixed content before ending tag */
  12453. }
  12454. soap->dom->code = t; /* restore XML code */
  12455. }
  12456. #endif
  12457. if (soap->peeked) {
  12458. if (*soap->tag)
  12459. n++;
  12460. soap->peeked = 0;
  12461. }
  12462. do {
  12463. while (((c = soap_get(soap)) != SOAP_TT)) {
  12464. if ((int) c == EOF)
  12465. return soap->error = SOAP_CHK_EOF;
  12466. if (!soap_coblank(c)) {
  12467. if ((soap->mode & SOAP_XML_STRICT))
  12468. return soap->error = SOAP_END_TAG; /* reject mixed content before ending tag */
  12469. if (c == SOAP_LT)
  12470. n++;
  12471. else if (c == '/') {
  12472. c = soap_get(soap);
  12473. if (c == SOAP_GT && n > 0)
  12474. n--;
  12475. else
  12476. soap_unget(soap, c);
  12477. }
  12478. }
  12479. }
  12480. } while (n-- > 0);
  12481. s = soap->tag;
  12482. n = sizeof(soap->tag);
  12483. while ((c = soap_get(soap)) > 32) {
  12484. if (n > 1) {
  12485. *s++ = (char) c;
  12486. n--;
  12487. }
  12488. }
  12489. *s = '\0';
  12490. if ((int) c == EOF)
  12491. return soap->error = SOAP_CHK_EOF;
  12492. while (soap_coblank(c))
  12493. c = soap_get(soap);
  12494. if (c != SOAP_GT)
  12495. return soap->error = SOAP_SYNTAX_ERROR;
  12496. #ifndef WITH_LEAN
  12497. #ifdef WITH_DOM
  12498. if (soap->feltendin)
  12499. {
  12500. int err = soap->error;
  12501. soap->error = soap->feltendin(soap, soap->tag, tag);
  12502. if (soap->error)
  12503. return soap->error;
  12504. if (err)
  12505. return soap->error = err; /* restore error */
  12506. }
  12507. #endif
  12508. #endif
  12509. if (tag && (soap->mode & SOAP_XML_STRICT)) {
  12510. soap_pop_namespace(soap);
  12511. if (soap_match_tag(soap, soap->tag, tag)) {
  12512. DBGLOG(TEST,
  12513. SOAP_MESSAGE(fdebug, "End tag '%s' does not match '%s'\n", soap->tag, tag ? tag : SOAP_STR_EOS));
  12514. return soap->error = SOAP_SYNTAX_ERROR;
  12515. }
  12516. }
  12517. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "End tag found (level=%u) '%s'='%s'\n", soap->level, soap->tag,
  12518. tag ? tag : SOAP_STR_EOS));
  12519. soap->level--;
  12520. return SOAP_OK;
  12521. }
  12522. /******************************************************************************/
  12523. SOAP_FMAC1
  12524. const char *
  12525. SOAP_FMAC2
  12526. soap_attr_value(struct soap *soap, const char *name, int flag, int occurs) {
  12527. struct soap_attribute *tp;
  12528. if (*name == '-')
  12529. return SOAP_STR_EOS;
  12530. for (tp = soap->attributes; tp; tp = tp->next) {
  12531. if (tp->visible == 2 && !soap_match_att(soap, tp->name, name))
  12532. break;
  12533. }
  12534. if (tp) {
  12535. if (occurs == 4 || (occurs == 2 && (soap->mode & SOAP_XML_STRICT)))
  12536. soap->error = SOAP_PROHIBITED;
  12537. else if (flag >= 4)
  12538. return soap_collapse(soap, tp->value, flag, 1);
  12539. else
  12540. return tp->value;
  12541. } else if (occurs == 3 || (occurs == 1 && (soap->mode & SOAP_XML_STRICT))) {
  12542. soap->error = SOAP_REQUIRED;
  12543. } else {
  12544. soap->error = SOAP_OK;
  12545. }
  12546. return NULL;
  12547. }
  12548. /******************************************************************************/
  12549. SOAP_FMAC1
  12550. int
  12551. SOAP_FMAC2
  12552. soap_set_attr(struct soap *soap, const char *name, const char *value, int flag) {
  12553. struct soap_attribute *tp, *tq;
  12554. if (*name == '-')
  12555. return SOAP_OK;
  12556. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Set attribute %s='%s'\n", name, value ? value : SOAP_STR_EOS));
  12557. tq = NULL;
  12558. for (tp = soap->attributes; tp; tq = tp, tp = tp->next) {
  12559. if (!strcmp(tp->name, name))
  12560. break;
  12561. }
  12562. if (!tp) {
  12563. size_t l = strlen(name);
  12564. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Allocate attribute %s\n", name));
  12565. if (sizeof(struct soap_attribute) + l > l &&
  12566. (SOAP_MAXALLOCSIZE <= 0 || sizeof(struct soap_attribute) + l <= SOAP_MAXALLOCSIZE))
  12567. tp = (struct soap_attribute *) SOAP_MALLOC(soap, sizeof(struct soap_attribute) + l);
  12568. if (!tp)
  12569. return soap->error = SOAP_EOM;
  12570. tp->ns = NULL;
  12571. #ifndef WITH_LEAN
  12572. if ((soap->mode & SOAP_XML_CANONICAL)) {
  12573. struct soap_attribute **tpp = &soap->attributes;
  12574. const char *s = strchr(name, ':');
  12575. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Inserting attribute %s for c14n\n", name));
  12576. if (!strncmp(name, "xmlns", 5)) {
  12577. for (; *tpp; tpp = &(*tpp)->next)
  12578. if (strncmp((*tpp)->name, "xmlns", 5) || strcmp((*tpp)->name + 5, name + 5) > 0)
  12579. break;
  12580. } else if (!s) {
  12581. for (; *tpp; tpp = &(*tpp)->next)
  12582. if (strncmp((*tpp)->name, "xmlns", 5) && ((*tpp)->ns || strcmp((*tpp)->name, name) > 0))
  12583. break;
  12584. } else {
  12585. struct soap_nlist *np = soap_lookup_ns(soap, name, s - name);
  12586. if (np) {
  12587. tp->ns = np->ns;
  12588. } else {
  12589. struct soap_attribute *tq;
  12590. for (tq = soap->attributes; tq; tq = tq->next) {
  12591. if (!strncmp(tq->name, "xmlns:", 6) && !strncmp(tq->name + 6, name, s - name) &&
  12592. !tq->name[6 + s - name]) {
  12593. tp->ns = tq->ns;
  12594. break;
  12595. }
  12596. }
  12597. }
  12598. for (; *tpp; tpp = &(*tpp)->next) {
  12599. int k;
  12600. if (strncmp((*tpp)->name, "xmlns", 5) && (*tpp)->ns && tp->ns &&
  12601. ((k = strcmp((*tpp)->ns, tp->ns)) > 0 || (!k && strcmp((*tpp)->name, name) > 0)))
  12602. break;
  12603. }
  12604. }
  12605. tp->next = *tpp;
  12606. *tpp = tp;
  12607. } else
  12608. #endif
  12609. if (tq) {
  12610. tq->next = tp;
  12611. tp->next = NULL;
  12612. } else {
  12613. tp->next = soap->attributes;
  12614. soap->attributes = tp;
  12615. }
  12616. soap_strcpy((char *) tp->name, l + 1, name);
  12617. tp->value = NULL;
  12618. } else if (tp->visible) {
  12619. return SOAP_OK;
  12620. } else if (value && tp->value && tp->size <= strlen(value)) {
  12621. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Free attribute value of %s (free %p)\n", name, (void *) tp->value));
  12622. SOAP_FREE(soap, tp->value);
  12623. tp->value = NULL;
  12624. tp->ns = NULL;
  12625. }
  12626. if (value) {
  12627. if (!tp->value) {
  12628. tp->size = strlen(value) + 1;
  12629. if (SOAP_MAXALLOCSIZE <= 0 || tp->size <= SOAP_MAXALLOCSIZE)
  12630. tp->value = (char *) SOAP_MALLOC(soap, tp->size);
  12631. if (!tp->value)
  12632. return soap->error = SOAP_EOM;
  12633. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Allocate attribute value for %s (%p)\n", tp->name, (void *) tp->value));
  12634. }
  12635. soap_strcpy(tp->value, tp->size, value);
  12636. if (!strncmp(tp->name, "xmlns:", 6))
  12637. tp->ns = tp->value;
  12638. tp->visible = 2;
  12639. tp->flag = (short) flag;
  12640. #ifndef WITH_LEAN
  12641. if (!strcmp(name, "wsu:Id")) {
  12642. soap->event = SOAP_SEC_BEGIN;
  12643. soap_strcpy(soap->id, sizeof(soap->id), value);
  12644. }
  12645. if ((soap->mode & SOAP_XML_CANONICAL) && !(soap->mode & SOAP_XML_CANONICAL_NA)) {
  12646. const char *s = strchr(name, ':');
  12647. if (s && strchr(value, ':')) {
  12648. struct soap_nlist *np = soap_lookup_ns(soap, name, s - name);
  12649. if (np && np->ns && soap->local_namespaces) {
  12650. if ((!strcmp(s + 1, "type") && !strcmp(np->ns, soap->local_namespaces[2].ns)) /* xsi:type QName */
  12651. || ((!strcmp(s + 1, "arrayType") || !strcmp(s + 1, "itemType")) && !strcmp(np->ns,
  12652. soap->local_namespaces[1].ns))) /* SOAP-ENC:arrayType and SOAP-ENC:itemType QName */
  12653. soap_utilize_ns(soap, value, 1);
  12654. }
  12655. }
  12656. }
  12657. #endif
  12658. } else {
  12659. tp->visible = 1;
  12660. }
  12661. return SOAP_OK;
  12662. }
  12663. /******************************************************************************/
  12664. SOAP_FMAC1
  12665. void
  12666. SOAP_FMAC2
  12667. soap_clr_attr(struct soap *soap) {
  12668. struct soap_attribute *tp;
  12669. #ifndef WITH_LEAN
  12670. if ((soap->mode & SOAP_XML_CANONICAL)) {
  12671. while (soap->attributes) {
  12672. tp = soap->attributes->next;
  12673. if (soap->attributes->value)
  12674. SOAP_FREE(soap, soap->attributes->value);
  12675. SOAP_FREE(soap, soap->attributes);
  12676. soap->attributes = tp;
  12677. }
  12678. } else
  12679. #endif
  12680. {
  12681. for (tp = soap->attributes; tp; tp = tp->next)
  12682. tp->visible = 0;
  12683. }
  12684. }
  12685. /******************************************************************************/
  12686. static int
  12687. soap_getattrval(struct soap *soap, char *s, size_t *n, soap_wchar d) {
  12688. char buf[8];
  12689. size_t i;
  12690. size_t k = *n;
  12691. size_t m = 0;
  12692. char *t = buf;
  12693. for (i = 0; i < k; i++) {
  12694. soap_wchar c;
  12695. if (m) {
  12696. *s++ = *t++;
  12697. m--;
  12698. continue;
  12699. }
  12700. if ((soap->mode & SOAP_C_UTFSTRING)) {
  12701. c = soap_get(soap);
  12702. if ((c & 0x80000000) && c >= -0x7FFFFF80 && c < SOAP_AP) {
  12703. t = buf;
  12704. c &= 0x7FFFFFFF;
  12705. if (c < 0x0800)
  12706. *t++ = (char) (0xC0 | ((c >> 6) & 0x1F));
  12707. else {
  12708. #ifdef WITH_REPLACE_ILLEGAL_UTF8
  12709. if (!((c >= 0x80 && c <= 0xD7FF) || (c >= 0xE000 && c <= 0xFFFD) || (c >= 0x10000 && c <= 0x10FFFF)))
  12710. c = SOAP_UNKNOWN_UNICODE_CHAR;
  12711. #endif
  12712. if (c < 0x010000) {
  12713. *t++ = (char) (0xE0 | ((c >> 12) & 0x0F));
  12714. } else {
  12715. if (c < 0x200000) {
  12716. *t++ = (char) (0xF0 | ((c >> 18) & 0x07));
  12717. } else {
  12718. if (c < 0x04000000) {
  12719. *t++ = (char) (0xF8 | ((c >> 24) & 0x03));
  12720. } else {
  12721. *t++ = (char) (0xFC | ((c >> 30) & 0x01));
  12722. *t++ = (char) (0x80 | ((c >> 24) & 0x3F));
  12723. }
  12724. *t++ = (char) (0x80 | ((c >> 18) & 0x3F));
  12725. }
  12726. *t++ = (char) (0x80 | ((c >> 12) & 0x3F));
  12727. }
  12728. *t++ = (char) (0x80 | ((c >> 6) & 0x3F));
  12729. }
  12730. *t++ = (char) (0x80 | (c & 0x3F));
  12731. m = t - buf - 1;
  12732. if (i + m >= k) {
  12733. soap_unget(soap, c | 0x80000000);
  12734. *n = i;
  12735. return soap->error = SOAP_EOM;
  12736. }
  12737. t = buf;
  12738. *s++ = *t++;
  12739. continue;
  12740. }
  12741. } else {
  12742. c = soap_getutf8(soap);
  12743. }
  12744. switch (c) {
  12745. case SOAP_TT:
  12746. *s++ = '<';
  12747. soap_unget(soap, '/');
  12748. break;
  12749. case SOAP_LT:
  12750. *s++ = '<';
  12751. break;
  12752. case SOAP_GT:
  12753. if (d == ' ') {
  12754. soap_unget(soap, c);
  12755. *s = '\0';
  12756. *n = i + 1;
  12757. return SOAP_OK;
  12758. }
  12759. *s++ = '>';
  12760. break;
  12761. case SOAP_QT:
  12762. if (c == d) {
  12763. *s = '\0';
  12764. *n = i + 1;
  12765. return SOAP_OK;
  12766. }
  12767. *s++ = '"';
  12768. break;
  12769. case SOAP_AP:
  12770. if (c == d) {
  12771. *s = '\0';
  12772. *n = i + 1;
  12773. return SOAP_OK;
  12774. }
  12775. *s++ = '\'';
  12776. break;
  12777. case '\t':
  12778. case '\n':
  12779. case '\r':
  12780. case ' ':
  12781. case '/':
  12782. if (d == ' ') {
  12783. soap_unget(soap, c);
  12784. *s = '\0';
  12785. *n = i + 1;
  12786. return SOAP_OK;
  12787. }
  12788. *s++ = (char) c;
  12789. break;
  12790. default:
  12791. if ((int) c == EOF) {
  12792. *s = '\0';
  12793. *n = i + 1;
  12794. return soap->error = SOAP_CHK_EOF;
  12795. }
  12796. *s++ = (char) c;
  12797. }
  12798. }
  12799. return soap->error = SOAP_EOM;
  12800. }
  12801. /******************************************************************************/
  12802. #ifdef WITH_FAST
  12803. SOAP_FMAC1
  12804. int
  12805. SOAP_FMAC2
  12806. soap_store_lab(struct soap *soap, const char *s, size_t n) {
  12807. soap->labidx = 0;
  12808. return soap_append_lab(soap, s, n);
  12809. }
  12810. #endif
  12811. /******************************************************************************/
  12812. #ifdef WITH_FAST
  12813. SOAP_FMAC1
  12814. int
  12815. SOAP_FMAC2
  12816. soap_append_lab(struct soap *soap, const char *s, size_t n) {
  12817. if (soap->labidx + n < soap->labidx)
  12818. return soap->error = SOAP_EOM;
  12819. if (soap->labidx + n >= soap->lablen) {
  12820. char *t = soap->labbuf;
  12821. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Enlarging look-aside buffer to append data, size=%lu\n",
  12822. (unsigned long) soap->lablen));
  12823. if (soap->lablen == 0)
  12824. soap->lablen = SOAP_LABLEN;
  12825. while (soap->labidx + n >= soap->lablen) {
  12826. if (soap->lablen << 1 < soap->lablen)
  12827. return soap->error = SOAP_EOM;
  12828. soap->lablen <<= 1;
  12829. }
  12830. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "New look-aside buffer size=%lu\n", (unsigned long) soap->lablen));
  12831. if (SOAP_MAXALLOCSIZE > 0 && soap->lablen > SOAP_MAXALLOCSIZE)
  12832. return soap->error = SOAP_EOM;
  12833. soap->labbuf = (char *) SOAP_MALLOC(soap, soap->lablen);
  12834. if (!soap->labbuf) {
  12835. if (t)
  12836. SOAP_FREE(soap, t);
  12837. return soap->error = SOAP_EOM;
  12838. }
  12839. if (t) {
  12840. (void) soap_memcpy((void *) soap->labbuf, soap->lablen, (const void *) t, soap->labidx);
  12841. SOAP_FREE(soap, t);
  12842. }
  12843. }
  12844. if (s) {
  12845. (void) soap_memcpy((void *) (soap->labbuf + soap->labidx), soap->lablen - soap->labidx, (const void *) s, n);
  12846. soap->labidx += n;
  12847. }
  12848. return SOAP_OK;
  12849. }
  12850. #endif
  12851. /******************************************************************************/
  12852. SOAP_FMAC1
  12853. int
  12854. SOAP_FMAC2
  12855. soap_peek_element(struct soap *soap) {
  12856. #ifdef WITH_DOM
  12857. struct soap_dom_attribute **att = NULL;
  12858. char *lead = NULL;
  12859. #endif
  12860. struct soap_attribute *tp, *tq = NULL;
  12861. const char *t;
  12862. char *s;
  12863. soap_wchar c;
  12864. int i;
  12865. if (soap->peeked) {
  12866. if (!*soap->tag)
  12867. return soap->error = SOAP_NO_TAG;
  12868. return SOAP_OK;
  12869. }
  12870. soap->peeked = 1;
  12871. soap->id[0] = '\0';
  12872. soap->href[0] = '\0';
  12873. soap->type[0] = '\0';
  12874. soap->arrayType[0] = '\0';
  12875. soap->arraySize[0] = '\0';
  12876. soap->arrayOffset[0] = '\0';
  12877. soap->other = 0;
  12878. soap->root = -1;
  12879. soap->position = 0;
  12880. soap->null = 0;
  12881. soap->mustUnderstand = 0;
  12882. /* UTF-8 BOM? */
  12883. c = soap_getchar(soap);
  12884. if (c == 0xEF && soap_get0(soap) == 0xBB) {
  12885. soap_get1(soap);
  12886. c = soap_get1(soap);
  12887. if (c == 0xBF)
  12888. soap->mode &= ~SOAP_ENC_LATIN;
  12889. else
  12890. soap_unget(soap, (0x0F << 12) | (0xBB << 6) | (c & 0x3F)); /* UTF-8 */
  12891. } else if ((c == 0xFE && soap_get0(soap) == 0xFF) /* UTF-16 BE */
  12892. || (c == 0xFF && soap_get0(soap) == 0xFE)) /* UTF-16 LE */
  12893. {
  12894. return soap->error = SOAP_UTF_ERROR;
  12895. } else {
  12896. soap_unget(soap, c);
  12897. }
  12898. c = soap_get(soap);
  12899. #ifdef WITH_DOM
  12900. /* whitespace leading up to the start tag is significant for DOM as-is (but comments and PIs are removed from this lead) */
  12901. if (soap_coblank(c))
  12902. {
  12903. soap->labidx = 0;
  12904. do
  12905. {
  12906. if (soap_append_lab(soap, NULL, 0))
  12907. return soap->error;
  12908. s = soap->labbuf + soap->labidx;
  12909. i = soap->lablen - soap->labidx;
  12910. soap->labidx = soap->lablen;
  12911. while (soap_coblank(c) && i-- > 0)
  12912. {
  12913. *s++ = c;
  12914. c = soap_get(soap);
  12915. }
  12916. } while (soap_coblank(c) || i == 0);
  12917. *s = '\0';
  12918. lead = soap->labbuf;
  12919. }
  12920. #else
  12921. /* skip space */
  12922. while (soap_coblank(c))
  12923. c = soap_get(soap);
  12924. #endif
  12925. if (c != SOAP_LT) {
  12926. *soap->tag = '\0';
  12927. if ((int) c == EOF)
  12928. return soap->error = SOAP_CHK_EOF;
  12929. soap_unget(soap, c);
  12930. #ifdef WITH_DOM
  12931. /* whitespace leading up to the end tag is significant for DOM as-is */
  12932. if ((soap->mode & SOAP_XML_DOM) && soap->dom)
  12933. {
  12934. if (lead && *lead)
  12935. soap->dom->tail = soap_strdup(soap, lead);
  12936. else
  12937. soap->dom->tail = SOAP_STR_EOS; /* body with closing tag instead of <tag/> */
  12938. }
  12939. #endif
  12940. return soap->error = SOAP_NO_TAG;
  12941. }
  12942. do {
  12943. c = soap_get1(soap);
  12944. } while (soap_coblank(c));
  12945. s = soap->tag;
  12946. i = sizeof(soap->tag);
  12947. while (c != '>' && c != '/' && c > 32 && (int) c != EOF) {
  12948. if (i > 1) {
  12949. *s++ = (char) c;
  12950. i--;
  12951. }
  12952. c = soap_get1(soap);
  12953. }
  12954. *s = '\0';
  12955. while (soap_coblank(c))
  12956. c = soap_get1(soap);
  12957. #ifdef WITH_DOM
  12958. if ((soap->mode & SOAP_XML_DOM))
  12959. {
  12960. struct soap_dom_element *elt;
  12961. elt = (struct soap_dom_element*)soap_malloc(soap, sizeof(struct soap_dom_element));
  12962. if (!elt)
  12963. return soap->error;
  12964. elt->next = NULL;
  12965. elt->prnt = soap->dom;
  12966. elt->elts = NULL;
  12967. elt->atts = NULL;
  12968. elt->nstr = NULL;
  12969. elt->name = soap_strdup(soap, soap->tag);
  12970. elt->text = NULL;
  12971. elt->code = NULL;
  12972. elt->tail = NULL;
  12973. elt->node = NULL;
  12974. elt->type = 0;
  12975. if (lead && *lead)
  12976. elt->lead = soap_strdup(soap, lead);
  12977. else
  12978. elt->lead = NULL;
  12979. elt->soap = soap;
  12980. if (soap->dom)
  12981. {
  12982. struct soap_dom_element *p = soap->dom->elts;
  12983. if (p)
  12984. {
  12985. while (p->next)
  12986. p = p->next;
  12987. p->next = elt;
  12988. }
  12989. else
  12990. {
  12991. soap->dom->elts = elt;
  12992. }
  12993. }
  12994. soap->dom = elt;
  12995. att = &elt->atts;
  12996. if (!elt->name)
  12997. return soap->error = SOAP_EOM;
  12998. }
  12999. #endif
  13000. soap_pop_namespace(soap);
  13001. for (tp = soap->attributes; tp; tp = tp->next)
  13002. tp->visible = 0;
  13003. while ((int) c != EOF && c != '>' && c != '/') {
  13004. s = soap->tmpbuf;
  13005. i = sizeof(soap->tmpbuf);
  13006. while (c != '=' && c != '>' && c != '/' && c > 32 && (int) c != EOF) {
  13007. if (i > 1) {
  13008. *s++ = (char) c;
  13009. i--;
  13010. }
  13011. c = soap_get1(soap);
  13012. }
  13013. *s = '\0';
  13014. if (i == sizeof(soap->tmpbuf))
  13015. return soap->error = SOAP_SYNTAX_ERROR;
  13016. #ifdef WITH_DOM
  13017. /* add attribute name to dom */
  13018. if (att)
  13019. {
  13020. *att = (struct soap_dom_attribute*)soap_malloc(soap, sizeof(struct soap_dom_attribute));
  13021. if (!*att)
  13022. return soap->error;
  13023. (*att)->next = NULL;
  13024. (*att)->nstr = NULL;
  13025. (*att)->name = soap_strdup(soap, soap->tmpbuf);
  13026. (*att)->text = NULL;
  13027. (*att)->soap = soap;
  13028. if (!(*att)->name)
  13029. return soap->error = SOAP_EOM;
  13030. }
  13031. #endif
  13032. if (!strncmp(soap->tmpbuf, "xmlns", 5)) {
  13033. if (soap->tmpbuf[5] == ':')
  13034. t = soap->tmpbuf + 6;
  13035. else if (soap->tmpbuf[5])
  13036. t = NULL;
  13037. else
  13038. t = SOAP_STR_EOS;
  13039. } else {
  13040. t = NULL;
  13041. }
  13042. tq = NULL;
  13043. for (tp = soap->attributes; tp; tq = tp, tp = tp->next) {
  13044. if (!SOAP_STRCMP(tp->name, soap->tmpbuf))
  13045. break;
  13046. }
  13047. if (!tp) {
  13048. size_t l = strlen(soap->tmpbuf);
  13049. tp = (struct soap_attribute *) SOAP_MALLOC(soap, sizeof(struct soap_attribute) + l);
  13050. if (!tp)
  13051. return soap->error = SOAP_EOM;
  13052. (void) soap_memcpy((char *) tp->name, l + 1, soap->tmpbuf, l + 1);
  13053. tp->value = NULL;
  13054. tp->size = 0;
  13055. tp->ns = NULL;
  13056. tp->visible = 0;
  13057. /* append attribute to the end of the list */
  13058. if (tq) {
  13059. tq->next = tp;
  13060. tp->next = NULL;
  13061. } else {
  13062. tp->next = soap->attributes;
  13063. soap->attributes = tp;
  13064. }
  13065. }
  13066. while (soap_coblank(c))
  13067. c = soap_get1(soap);
  13068. if (c == '=') {
  13069. size_t k;
  13070. do {
  13071. c = soap_getutf8(soap);
  13072. } while (soap_coblank(c));
  13073. if (c != SOAP_QT && c != SOAP_AP) {
  13074. soap_unget(soap, c);
  13075. c = ' '; /* blank delimiter */
  13076. }
  13077. k = tp->size;
  13078. if (soap_getattrval(soap, tp->value, &k, c)) {
  13079. #ifdef WITH_FAST
  13080. if (soap->error != SOAP_EOM)
  13081. return soap->error;
  13082. soap->error = SOAP_OK;
  13083. if (soap_store_lab(soap, tp->value, k))
  13084. return soap->error;
  13085. if (tp->value)
  13086. SOAP_FREE(soap, tp->value);
  13087. tp->value = NULL;
  13088. for (;;) {
  13089. k = soap->lablen - soap->labidx;
  13090. if (soap_getattrval(soap, soap->labbuf + soap->labidx, &k, c)) {
  13091. if (soap->error != SOAP_EOM)
  13092. return soap->error;
  13093. soap->error = SOAP_OK;
  13094. soap->labidx = soap->lablen;
  13095. if (soap_append_lab(soap, NULL, 0))
  13096. return soap->error;
  13097. } else {
  13098. break;
  13099. }
  13100. }
  13101. if (soap->labidx) {
  13102. tp->size = soap->lablen;
  13103. } else {
  13104. tp->size = strlen(soap->labbuf) + 1;
  13105. if (tp->size < SOAP_LABLEN)
  13106. tp->size = SOAP_LABLEN;
  13107. }
  13108. tp->value = (char *) SOAP_MALLOC(soap, tp->size);
  13109. if (!tp->value)
  13110. return soap->error = SOAP_EOM;
  13111. soap_strcpy(tp->value, tp->size, soap->labbuf);
  13112. #else
  13113. tp->size = k;
  13114. if (soap->error != SOAP_EOM)
  13115. return soap->error;
  13116. soap->error = SOAP_OK;
  13117. if (soap_alloc_block(soap) == NULL)
  13118. return soap->error;
  13119. for (;;)
  13120. {
  13121. s = (char*)soap_push_block(soap, NULL, SOAP_BLKLEN);
  13122. if (!s)
  13123. return soap->error;
  13124. k = SOAP_BLKLEN;
  13125. if (soap_getattrval(soap, s, &k, c))
  13126. {
  13127. if (soap->error != SOAP_EOM)
  13128. return soap->error;
  13129. soap->error = SOAP_OK;
  13130. soap_size_block(soap, NULL, k);
  13131. }
  13132. else
  13133. {
  13134. break;
  13135. }
  13136. }
  13137. k = tp->size + soap->blist->size;
  13138. if (SOAP_MAXALLOCSIZE > 0 && k > SOAP_MAXALLOCSIZE)
  13139. return soap->error = SOAP_EOM;
  13140. s = (char*)SOAP_MALLOC(soap, k);
  13141. if (!s)
  13142. return soap->error = SOAP_EOM;
  13143. if (tp->value)
  13144. {
  13145. (void)soap_memcpy((void*)s, k, (const void*)tp->value, tp->size);
  13146. SOAP_FREE(soap, tp->value);
  13147. }
  13148. soap_save_block(soap, NULL, s + tp->size, 0);
  13149. tp->value = s;
  13150. tp->size = k;
  13151. #endif
  13152. }
  13153. if (tp->visible) {
  13154. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Duplicate attribute in %s\n", soap->tag));
  13155. return soap->error = SOAP_SYNTAX_ERROR; /* redefined (duplicate) attribute */
  13156. }
  13157. tp->visible = 2; /* seen this attribute w/ value */
  13158. do {
  13159. c = soap_get1(soap);
  13160. } while (soap_coblank(c));
  13161. #ifdef WITH_DOM
  13162. if (att && tp->value)
  13163. {
  13164. (*att)->text = soap_strdup(soap, tp->value);
  13165. if (!(*att)->text)
  13166. return soap->error = SOAP_EOM;
  13167. }
  13168. #endif
  13169. } else {
  13170. tp->visible = 1; /* seen this attribute w/o value */
  13171. }
  13172. #ifdef WITH_DOM
  13173. if (att)
  13174. att = &(*att)->next;
  13175. #endif
  13176. if (t && tp->value) {
  13177. if (soap_push_namespace(soap, t, tp->value) == NULL)
  13178. return soap->error;
  13179. }
  13180. }
  13181. #ifdef WITH_DOM
  13182. if (att)
  13183. {
  13184. soap->dom->nstr = soap_current_namespace_tag(soap, soap->tag);
  13185. for (att = &soap->dom->atts; *att; att = &(*att)->next)
  13186. (*att)->nstr = soap_current_namespace_att(soap, (*att)->name);
  13187. }
  13188. #endif
  13189. if ((int) c == EOF)
  13190. return soap->error = SOAP_CHK_EOF;
  13191. soap->body = (c != '/');
  13192. if (!soap->body) {
  13193. do {
  13194. c = soap_get1(soap);
  13195. } while (soap_coblank(c));
  13196. }
  13197. #ifdef WITH_DOM
  13198. if ((soap->mode & SOAP_XML_DOM))
  13199. {
  13200. if (!soap->body && soap->dom->prnt)
  13201. soap->dom = soap->dom->prnt;
  13202. }
  13203. #endif
  13204. for (tp = soap->attributes; tp; tp = tp->next) {
  13205. if (tp->visible && tp->value) {
  13206. #ifndef WITH_NOIDREF
  13207. if (!strcmp(tp->name, "id")) {
  13208. if ((soap->version > 0 && !(soap->imode & SOAP_XML_TREE))
  13209. || (soap->mode & SOAP_XML_GRAPH)) {
  13210. *soap->id = '#';
  13211. soap_strcpy(soap->id + 1, sizeof(soap->id) - 1, tp->value);
  13212. }
  13213. } else if (!strcmp(tp->name, "href")) {
  13214. if ((soap->version == 1 && !(soap->imode & SOAP_XML_TREE))
  13215. || (soap->mode & SOAP_XML_GRAPH)
  13216. || ((soap->mode & (SOAP_ENC_MTOM | SOAP_ENC_DIME)) && *tp->value != '#'))
  13217. soap_strcpy(soap->href, sizeof(soap->href), tp->value);
  13218. } else if (!strcmp(tp->name, "ref")) {
  13219. if ((soap->version == 2 && !(soap->imode & SOAP_XML_TREE))
  13220. || (soap->mode & SOAP_XML_GRAPH)) {
  13221. *soap->href = '#';
  13222. soap_strcpy(soap->href + (*tp->value != '#'), sizeof(soap->href) - 1, tp->value);
  13223. }
  13224. }
  13225. #else
  13226. if (!strcmp(tp->name, "href"))
  13227. {
  13228. if ((soap->mode & (SOAP_ENC_MTOM | SOAP_ENC_DIME)) && *tp->value != '#')
  13229. soap_strcpy(soap->href, sizeof(soap->href), tp->value);
  13230. }
  13231. #endif
  13232. else if (!soap_match_tag(soap, tp->name, "xsi:type")) {
  13233. soap_strcpy(soap->type, sizeof(soap->type), tp->value);
  13234. } else if ((!soap_match_tag(soap, tp->name, "xsi:null")
  13235. || !soap_match_tag(soap, tp->name, "xsi:nil"))
  13236. && (!strcmp(tp->value, "1")
  13237. || !strcmp(tp->value, "true"))) {
  13238. soap->null = 1;
  13239. } else if (!soap_match_tag(soap, tp->name, "SOAP-ENV:encodingStyle")) {
  13240. if (!soap->encodingStyle)
  13241. soap->encodingStyle = SOAP_STR_EOS;
  13242. soap_version(soap);
  13243. } else if (soap->version == 1) {
  13244. if (!soap_match_tag(soap, tp->name, "SOAP-ENC:arrayType")) {
  13245. s = soap_strrchr(tp->value, '[');
  13246. if (s && (size_t) (s - tp->value) < sizeof(soap->arrayType)) {
  13247. (void) soap_strncpy(soap->arrayType, sizeof(soap->arrayType), tp->value, s - tp->value);
  13248. soap_strcpy(soap->arraySize, sizeof(soap->arraySize), s);
  13249. } else
  13250. soap_strcpy(soap->arrayType, sizeof(soap->arrayType), tp->value);
  13251. } else if (!soap_match_tag(soap, tp->name, "SOAP-ENC:offset")) {
  13252. soap_strcpy(soap->arrayOffset, sizeof(soap->arrayOffset), tp->value);
  13253. } else if (!soap_match_tag(soap, tp->name, "SOAP-ENC:position")) {
  13254. soap->position = soap_getposition(tp->value, soap->positions);
  13255. } else if (!soap_match_tag(soap, tp->name, "SOAP-ENC:root")) {
  13256. soap->root = ((!strcmp(tp->value, "1") || !strcmp(tp->value, "true")));
  13257. } else if (!soap_match_tag(soap, tp->name, "SOAP-ENV:mustUnderstand")
  13258. && (!strcmp(tp->value, "1") || !strcmp(tp->value, "true"))) {
  13259. soap->mustUnderstand = 1;
  13260. } else if (!soap_match_tag(soap, tp->name, "SOAP-ENV:actor")) {
  13261. if ((!soap->actor || strcmp(soap->actor, tp->value))
  13262. && strcmp(tp->value, "http://schemas.xmlsoap.org/soap/actor/next"))
  13263. soap->other = 1;
  13264. }
  13265. } else if (soap->version == 2) {
  13266. #ifndef WITH_NOIDREF
  13267. if (!soap_match_tag(soap, tp->name, "SOAP-ENC:id")) {
  13268. *soap->id = '#';
  13269. soap_strcpy(soap->id + 1, sizeof(soap->id) - 1, tp->value);
  13270. } else if (!soap_match_tag(soap, tp->name, "SOAP-ENC:ref")) {
  13271. *soap->href = '#';
  13272. soap_strcpy(soap->href + (*tp->value != '#'), sizeof(soap->href) - 1, tp->value);
  13273. } else
  13274. #endif
  13275. if (!soap_match_tag(soap, tp->name, "SOAP-ENC:itemType")) {
  13276. soap_strcpy(soap->arrayType, sizeof(soap->arrayType), tp->value);
  13277. } else if (!soap_match_tag(soap, tp->name, "SOAP-ENC:arraySize")) {
  13278. soap_strcpy(soap->arraySize, sizeof(soap->arraySize), tp->value);
  13279. } else if (!soap_match_tag(soap, tp->name, "SOAP-ENV:mustUnderstand")
  13280. && (!strcmp(tp->value, "1") || !strcmp(tp->value, "true"))) {
  13281. soap->mustUnderstand = 1;
  13282. } else if (!soap_match_tag(soap, tp->name, "SOAP-ENV:role")) {
  13283. if ((!soap->actor || strcmp(soap->actor, tp->value))
  13284. && strcmp(tp->value, "http://www.w3.org/2003/05/soap-envelope/role/next"))
  13285. soap->other = 1;
  13286. }
  13287. } else {
  13288. if (!soap_match_tag(soap, tp->name, "wsdl:required") && !strcmp(tp->value, "true"))
  13289. soap->mustUnderstand = 1;
  13290. }
  13291. }
  13292. }
  13293. #ifdef WITH_DOM
  13294. if (soap->feltbegin)
  13295. return soap->error = soap->feltbegin(soap, soap->tag);
  13296. #endif
  13297. return soap->error = SOAP_OK;
  13298. }
  13299. /******************************************************************************/
  13300. SOAP_FMAC1
  13301. void
  13302. SOAP_FMAC2
  13303. soap_retry(struct soap *soap) {
  13304. soap->error = SOAP_OK;
  13305. soap_revert(soap);
  13306. }
  13307. /******************************************************************************/
  13308. SOAP_FMAC1
  13309. void
  13310. SOAP_FMAC2
  13311. soap_revert(struct soap *soap) {
  13312. if (!soap->peeked) {
  13313. soap->peeked = 1;
  13314. if (soap->body)
  13315. soap->level--;
  13316. }
  13317. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Reverting to last element '%s' (level=%u)\n", soap->tag, soap->level));
  13318. }
  13319. /******************************************************************************/
  13320. SOAP_FMAC1
  13321. int
  13322. SOAP_FMAC2
  13323. soap_ignore(struct soap *soap) {
  13324. int n = 0;
  13325. soap_wchar c;
  13326. soap->level++;
  13327. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Ignoring XML content at level=%u\n", soap->level));
  13328. #ifdef WITH_DOM
  13329. if ((soap->mode & SOAP_XML_DOM) && soap->dom)
  13330. {
  13331. if (!soap_string_in(soap, -1, -1, -1, NULL))
  13332. return soap->error;
  13333. }
  13334. else
  13335. #endif
  13336. {
  13337. for (;;) {
  13338. c = soap_get(soap);
  13339. switch (c) {
  13340. case SOAP_TT:
  13341. if (n == 0)
  13342. goto end;
  13343. n--;
  13344. break;
  13345. case SOAP_LT:
  13346. n++;
  13347. break;
  13348. case '/':
  13349. if (n > 0) {
  13350. c = soap_get0(soap);
  13351. if (c == '>')
  13352. n--;
  13353. }
  13354. break;
  13355. default:
  13356. if ((int) c == EOF)
  13357. return soap->error = SOAP_EOF;
  13358. }
  13359. }
  13360. end:
  13361. soap_unget(soap, c);
  13362. }
  13363. return soap_element_end_in(soap, NULL);
  13364. }
  13365. /******************************************************************************/
  13366. SOAP_FMAC1
  13367. int
  13368. SOAP_FMAC2
  13369. soap_string_out(struct soap *soap, const char *s, int flag) {
  13370. const char *t;
  13371. soap_wchar c;
  13372. soap_wchar mask = (soap_wchar) 0xFFFFFF80UL;
  13373. #ifdef WITH_DOM
  13374. if ((soap->mode & SOAP_XML_DOM) && soap->dom)
  13375. {
  13376. soap->dom->text = soap_strdup(soap, s);
  13377. if (!soap->dom->text)
  13378. return soap->error = SOAP_EOM;
  13379. return SOAP_OK;
  13380. }
  13381. #endif
  13382. if (flag == 2 || (soap->mode & SOAP_C_UTFSTRING))
  13383. mask = 0;
  13384. t = s;
  13385. while ((c = *t++)) {
  13386. switch (c) {
  13387. case 0x09:
  13388. if (flag) {
  13389. if (soap_send_raw(soap, s, t - s - 1) || soap_send_raw(soap, "&#x9;", 5))
  13390. return soap->error;
  13391. s = t;
  13392. }
  13393. break;
  13394. case 0x0A:
  13395. if (flag || !(soap->mode & SOAP_XML_CANONICAL)) {
  13396. if (soap_send_raw(soap, s, t - s - 1) || soap_send_raw(soap, "&#xA;", 5))
  13397. return soap->error;
  13398. s = t;
  13399. }
  13400. break;
  13401. case '&':
  13402. if (soap_send_raw(soap, s, t - s - 1) || soap_send_raw(soap, "&amp;", 5))
  13403. return soap->error;
  13404. s = t;
  13405. break;
  13406. case '<':
  13407. if (soap_send_raw(soap, s, t - s - 1) || soap_send_raw(soap, "&lt;", 4))
  13408. return soap->error;
  13409. s = t;
  13410. break;
  13411. case '>':
  13412. if (!flag) {
  13413. if (soap_send_raw(soap, s, t - s - 1) || soap_send_raw(soap, "&gt;", 4))
  13414. return soap->error;
  13415. s = t;
  13416. }
  13417. break;
  13418. case '"':
  13419. if (flag) {
  13420. if (soap_send_raw(soap, s, t - s - 1) || soap_send_raw(soap, "&quot;", 6))
  13421. return soap->error;
  13422. s = t;
  13423. }
  13424. break;
  13425. case 0x7F:
  13426. if (soap_send_raw(soap, s, t - s - 1) || soap_send_raw(soap, "&#x7F;", 6))
  13427. return soap->error;
  13428. s = t;
  13429. break;
  13430. default:
  13431. #ifndef WITH_LEANER
  13432. #ifdef HAVE_MBTOWC
  13433. if ((soap->mode & SOAP_C_MBSTRING)) {
  13434. wchar_t wc;
  13435. int m = mbtowc(&wc, t - 1, MB_CUR_MAX);
  13436. if (m > 0 && !((soap_wchar) wc == c && m == 1 && c < 0x80)) {
  13437. if (soap_send_raw(soap, s, t - s - 1) || soap_pututf8(soap, (unsigned long) wc))
  13438. return soap->error;
  13439. s = t += m - 1;
  13440. continue;
  13441. }
  13442. }
  13443. #endif
  13444. #endif
  13445. #ifndef WITH_NOSTRINGTOUTF8
  13446. if ((c & mask) || !(c & 0xFFFFFFE0UL)) {
  13447. if (soap_send_raw(soap, s, t - s - 1) || soap_pututf8(soap, (unsigned char) c))
  13448. return soap->error;
  13449. s = t;
  13450. }
  13451. #endif
  13452. }
  13453. }
  13454. return soap_send_raw(soap, s, t - s - 1);
  13455. }
  13456. /******************************************************************************/
  13457. SOAP_FMAC1
  13458. char *
  13459. SOAP_FMAC2
  13460. soap_string_in(struct soap *soap, int flag, long minlen, long maxlen, const char *pattern) {
  13461. char *s;
  13462. char *t = NULL;
  13463. size_t i;
  13464. ULONG64 l = 0;
  13465. int n = 0, f = 0, m = 0;
  13466. soap_wchar c;
  13467. #if !defined(WITH_LEANER) && defined(HAVE_WCTOMB)
  13468. char buf[MB_LEN_MAX > 8 ? MB_LEN_MAX : 8];
  13469. #else
  13470. char buf[8];
  13471. #endif
  13472. if (maxlen < 0 && soap->maxlength > 0)
  13473. maxlen = soap->maxlength;
  13474. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Reading string content, flag=%d\n", flag));
  13475. if (flag <= 0 && soap->peeked && *soap->tag) {
  13476. #ifndef WITH_LEAN
  13477. struct soap_attribute *tp;
  13478. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "String content includes tag '%s' and attributes\n", soap->tag));
  13479. t = soap->tmpbuf;
  13480. *t = '<';
  13481. soap_strcpy(t + 1, sizeof(soap->tmpbuf) - 1, soap->tag);
  13482. t += strlen(t);
  13483. for (tp = soap->attributes; tp; tp = tp->next) {
  13484. if (tp->visible) {
  13485. size_t k = strlen(tp->name);
  13486. if (t + k + 1 >= soap->tmpbuf + sizeof(soap->tmpbuf))
  13487. break; /* too many or attribute values to large */
  13488. *t++ = ' ';
  13489. (void) soap_strncpy(t, sizeof(soap->tmpbuf) - (t - soap->tmpbuf), tp->name, k);
  13490. t += k;
  13491. if (tp->value) {
  13492. k = strlen(tp->value);
  13493. if (t + k + 3 >= soap->tmpbuf + sizeof(soap->tmpbuf))
  13494. break; /* too many or attribute values to large */
  13495. *t++ = '=';
  13496. *t++ = '"';
  13497. (void) soap_strncpy(t, sizeof(soap->tmpbuf) - (t - soap->tmpbuf), tp->value, k);
  13498. t += k;
  13499. *t++ = '"';
  13500. }
  13501. }
  13502. }
  13503. if (!soap->body)
  13504. *t++ = '/';
  13505. *t++ = '>';
  13506. *t = '\0';
  13507. t = soap->tmpbuf;
  13508. m = (int) strlen(soap->tmpbuf);
  13509. #endif
  13510. if (soap->body)
  13511. n = 1;
  13512. f = 1;
  13513. soap->peeked = 0;
  13514. }
  13515. #ifdef WITH_CDATA
  13516. if (flag <= 0) {
  13517. int state = 0;
  13518. #ifdef WITH_FAST
  13519. soap->labidx = 0; /* use look-aside buffer */
  13520. #else
  13521. if (soap_alloc_block(soap) == NULL)
  13522. return NULL;
  13523. #endif
  13524. for (;;) {
  13525. #ifdef WITH_FAST
  13526. size_t k;
  13527. if (soap_append_lab(soap, NULL, 0)) /* allocate more space in look-aside buffer if necessary */
  13528. return NULL;
  13529. s = soap->labbuf + soap->labidx; /* space to populate */
  13530. k = soap->lablen - soap->labidx; /* number of bytes available */
  13531. soap->labidx = soap->lablen; /* claim this space */
  13532. #else
  13533. size_t k = SOAP_BLKLEN;
  13534. s = (char*)soap_push_block(soap, NULL, k);
  13535. if (!s)
  13536. return NULL;
  13537. #endif
  13538. for (i = 0; i < k; i++) {
  13539. if (m > 0) {
  13540. *s++ = *t++; /* copy multibyte characters */
  13541. m--;
  13542. continue;
  13543. }
  13544. c = soap_getchar(soap);
  13545. if ((int) c == EOF)
  13546. goto end;
  13547. if ((c >= 0x80 || c < SOAP_AP) && state != 1 && !(soap->mode & SOAP_ENC_LATIN)) {
  13548. if ((c & 0x7FFFFFFF) >= 0x80) {
  13549. soap_unget(soap, c);
  13550. c = soap_getutf8(soap);
  13551. }
  13552. if ((c & 0x7FFFFFFF) >= 0x80 && (flag <= 0 || (soap->mode & SOAP_C_UTFSTRING))) {
  13553. c &= 0x7FFFFFFF;
  13554. t = buf;
  13555. if (c < 0x0800)
  13556. *t++ = (char) (0xC0 | ((c >> 6) & 0x1F));
  13557. else {
  13558. #ifdef WITH_REPLACE_ILLEGAL_UTF8
  13559. if (!((c >= 0x80 && c <= 0xD7FF) || (c >= 0xE000 && c <= 0xFFFD) || (c >= 0x10000 && c <= 0x10FFFF)))
  13560. c = SOAP_UNKNOWN_UNICODE_CHAR;
  13561. #endif
  13562. if (c < 0x010000) {
  13563. *t++ = (char) (0xE0 | ((c >> 12) & 0x0F));
  13564. } else {
  13565. if (c < 0x200000) {
  13566. *t++ = (char) (0xF0 | ((c >> 18) & 0x07));
  13567. } else {
  13568. if (c < 0x04000000) {
  13569. *t++ = (char) (0xF8 | ((c >> 24) & 0x03));
  13570. } else {
  13571. *t++ = (char) (0xFC | ((c >> 30) & 0x01));
  13572. *t++ = (char) (0x80 | ((c >> 24) & 0x3F));
  13573. }
  13574. *t++ = (char) (0x80 | ((c >> 18) & 0x3F));
  13575. }
  13576. *t++ = (char) (0x80 | ((c >> 12) & 0x3F));
  13577. }
  13578. *t++ = (char) (0x80 | ((c >> 6) & 0x3F));
  13579. }
  13580. *t++ = (char) (0x80 | (c & 0x3F));
  13581. m = (int) (t - buf) - 1;
  13582. t = buf;
  13583. *s++ = *t++;
  13584. continue;
  13585. }
  13586. }
  13587. switch (state) {
  13588. case 1:
  13589. if (c == ']')
  13590. state = 4;
  13591. *s++ = (char) c;
  13592. continue;
  13593. case 2:
  13594. if (c == '-')
  13595. state = 6;
  13596. *s++ = (char) c;
  13597. continue;
  13598. case 3:
  13599. if (c == '?')
  13600. state = 8;
  13601. *s++ = (char) c;
  13602. continue;
  13603. /* CDATA */
  13604. case 4:
  13605. if (c == ']')
  13606. state = 5;
  13607. else
  13608. state = 1;
  13609. *s++ = (char) c;
  13610. continue;
  13611. case 5:
  13612. if (c == '>')
  13613. state = 0;
  13614. else if (c != ']')
  13615. state = 1;
  13616. *s++ = (char) c;
  13617. continue;
  13618. /* comment */
  13619. case 6:
  13620. if (c == '-')
  13621. state = 7;
  13622. else
  13623. state = 2;
  13624. *s++ = (char) c;
  13625. continue;
  13626. case 7:
  13627. if (c == '>')
  13628. state = 0;
  13629. else if (c != '-')
  13630. state = 2;
  13631. *s++ = (char) c;
  13632. continue;
  13633. /* PI */
  13634. case 8:
  13635. if (c == '>')
  13636. state = 0;
  13637. else if (c != '?')
  13638. state = 3;
  13639. *s++ = (char) c;
  13640. continue;
  13641. }
  13642. switch (c) {
  13643. case SOAP_TT:
  13644. if (n == 0)
  13645. goto end;
  13646. n--;
  13647. *s++ = '<';
  13648. t = (char *) "/";
  13649. m = 1;
  13650. break;
  13651. case SOAP_LT:
  13652. if (flag == 3 || (f && n == 0))
  13653. goto end;
  13654. n++;
  13655. *s++ = '<';
  13656. break;
  13657. case SOAP_GT:
  13658. *s++ = '>';
  13659. break;
  13660. case SOAP_QT:
  13661. *s++ = '"';
  13662. break;
  13663. case SOAP_AP:
  13664. *s++ = '\'';
  13665. break;
  13666. case '/':
  13667. if (n > 0) {
  13668. c = soap_getchar(soap);
  13669. if (c == '>')
  13670. n--;
  13671. soap_unget(soap, c);
  13672. }
  13673. *s++ = '/';
  13674. break;
  13675. case '<':
  13676. c = soap_getchar(soap);
  13677. if (c == '/') {
  13678. if (n == 0) {
  13679. c = SOAP_TT;
  13680. goto end;
  13681. }
  13682. n--;
  13683. } else if (c == '!') {
  13684. c = soap_getchar(soap);
  13685. if (c == '[') {
  13686. do {
  13687. c = soap_getchar(soap);
  13688. } while ((int) c != EOF && c != '[');
  13689. if ((int) c == EOF)
  13690. goto end;
  13691. t = (char *) "![CDATA[";
  13692. m = 8;
  13693. state = 1;
  13694. } else if (c == '-') {
  13695. c = soap_getchar(soap);
  13696. if (c == '-')
  13697. state = 2;
  13698. t = (char *) "!-";
  13699. m = 2;
  13700. soap_unget(soap, c);
  13701. } else {
  13702. t = (char *) "!";
  13703. m = 1;
  13704. soap_unget(soap, c);
  13705. }
  13706. *s++ = '<';
  13707. break;
  13708. } else if (c == '?') {
  13709. state = 3;
  13710. } else if (flag == 3 || (f && n == 0)) {
  13711. soap_revget1(soap);
  13712. c = '<';
  13713. goto end;
  13714. } else
  13715. n++;
  13716. soap_unget(soap, c);
  13717. *s++ = '<';
  13718. break;
  13719. case '>':
  13720. *s++ = '>';
  13721. break;
  13722. case '"':
  13723. *s++ = '"';
  13724. break;
  13725. default:
  13726. #ifndef WITH_LEANER
  13727. #ifdef HAVE_WCTOMB
  13728. if ((soap->mode & SOAP_C_MBSTRING)) {
  13729. #if defined(WIN32) && !defined(CYGWIN) && !defined(__MINGW32__) && !defined(__MINGW64__) && !defined(__BORLANDC__)
  13730. m = 0;
  13731. wctomb_s(&m, buf, sizeof(buf), (wchar_t)(c & 0x7FFFFFFF));
  13732. #else
  13733. m = wctomb(buf, (wchar_t) (c & 0x7FFFFFFF));
  13734. #endif
  13735. if (m >= 1 && m <= (int) MB_CUR_MAX) {
  13736. t = buf;
  13737. *s++ = *t++;
  13738. m--;
  13739. } else {
  13740. *s++ = SOAP_UNKNOWN_CHAR;
  13741. m = 0;
  13742. }
  13743. } else
  13744. #endif
  13745. #endif
  13746. *s++ = (char) (c & 0xFF);
  13747. }
  13748. l++;
  13749. if (maxlen >= 0 && l > (size_t) maxlen) {
  13750. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "String too long: maxlen=%ld\n", maxlen));
  13751. soap->error = SOAP_LENGTH;
  13752. return NULL;
  13753. }
  13754. }
  13755. }
  13756. }
  13757. #endif
  13758. #ifdef WITH_FAST
  13759. soap->labidx = 0; /* use look-aside buffer */
  13760. #else
  13761. if (soap_alloc_block(soap) == NULL)
  13762. return NULL;
  13763. #endif
  13764. for (;;) {
  13765. #ifdef WITH_FAST
  13766. size_t k;
  13767. if (soap_append_lab(soap, NULL, 0)) /* allocate more space in look-aside buffer if necessary */
  13768. return NULL;
  13769. s = soap->labbuf + soap->labidx; /* space to populate */
  13770. k = soap->lablen - soap->labidx; /* number of bytes available */
  13771. soap->labidx = soap->lablen; /* claim this space */
  13772. #else
  13773. size_t k = SOAP_BLKLEN;
  13774. s = (char*)soap_push_block(soap, NULL, k);
  13775. if (!s)
  13776. return NULL;
  13777. #endif
  13778. for (i = 0; i < k; i++) {
  13779. if (m > 0) {
  13780. *s++ = *t++; /* copy multibyte characters */
  13781. m--;
  13782. continue;
  13783. }
  13784. #ifndef WITH_CDATA
  13785. if (flag <= 0)
  13786. c = soap_getchar(soap);
  13787. else
  13788. #endif
  13789. {
  13790. c = soap_getutf8(soap);
  13791. if ((soap->mode & SOAP_C_UTFSTRING)) {
  13792. if (c >= 0x80 || (c < SOAP_AP && c >= -0x7FFFFF80)) {
  13793. c &= 0x7FFFFFFF;
  13794. t = buf;
  13795. if (c < 0x0800) {
  13796. *t++ = (char) (0xC0 | ((c >> 6) & 0x1F));
  13797. } else {
  13798. #ifdef WITH_REPLACE_ILLEGAL_UTF8
  13799. if (!((c >= 0x80 && c <= 0xD7FF) || (c >= 0xE000 && c <= 0xFFFD) || (c >= 0x10000 && c <= 0x10FFFF)))
  13800. c = SOAP_UNKNOWN_UNICODE_CHAR;
  13801. #endif
  13802. if (c < 0x010000) {
  13803. *t++ = (char) (0xE0 | ((c >> 12) & 0x0F));
  13804. } else {
  13805. if (c < 0x200000) {
  13806. *t++ = (char) (0xF0 | ((c >> 18) & 0x07));
  13807. } else {
  13808. if (c < 0x04000000) {
  13809. *t++ = (char) (0xF8 | ((c >> 24) & 0x03));
  13810. } else {
  13811. *t++ = (char) (0xFC | ((c >> 30) & 0x01));
  13812. *t++ = (char) (0x80 | ((c >> 24) & 0x3F));
  13813. }
  13814. *t++ = (char) (0x80 | ((c >> 18) & 0x3F));
  13815. }
  13816. *t++ = (char) (0x80 | ((c >> 12) & 0x3F));
  13817. }
  13818. *t++ = (char) (0x80 | ((c >> 6) & 0x3F));
  13819. }
  13820. *t++ = (char) (0x80 | (c & 0x3F));
  13821. m = (int) (t - buf) - 1;
  13822. t = buf;
  13823. *s++ = *t++;
  13824. l++;
  13825. if (maxlen >= 0 && l > (size_t) maxlen) {
  13826. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "String too long: maxlen=%ld\n", maxlen));
  13827. soap->error = SOAP_LENGTH;
  13828. return NULL;
  13829. }
  13830. continue;
  13831. }
  13832. }
  13833. }
  13834. switch (c) {
  13835. case SOAP_TT:
  13836. if (n == 0)
  13837. goto end;
  13838. n--;
  13839. *s++ = '<';
  13840. t = (char *) "/";
  13841. m = 1;
  13842. break;
  13843. case SOAP_LT:
  13844. if (flag == 3 || (f && n == 0))
  13845. goto end;
  13846. n++;
  13847. *s++ = '<';
  13848. break;
  13849. case SOAP_GT:
  13850. *s++ = '>';
  13851. break;
  13852. case SOAP_QT:
  13853. *s++ = '"';
  13854. break;
  13855. case SOAP_AP:
  13856. *s++ = '\'';
  13857. break;
  13858. case '/':
  13859. if (n > 0) {
  13860. if (flag > 0) {
  13861. c = soap_get(soap);
  13862. if (c == SOAP_GT)
  13863. n--;
  13864. } else {
  13865. c = soap_getchar(soap);
  13866. if (c == '>')
  13867. n--;
  13868. }
  13869. soap_unget(soap, c);
  13870. }
  13871. *s++ = '/';
  13872. break;
  13873. case (soap_wchar) ('<' | 0x80000000):
  13874. if (flag > 0) {
  13875. *s++ = '<';
  13876. } else {
  13877. *s++ = '&';
  13878. t = (char *) "lt;";
  13879. m = 3;
  13880. }
  13881. break;
  13882. case (soap_wchar) ('>' | 0x80000000):
  13883. if (flag > 0) {
  13884. *s++ = '>';
  13885. } else {
  13886. *s++ = '&';
  13887. t = (char *) "gt;";
  13888. m = 3;
  13889. }
  13890. break;
  13891. case (soap_wchar) ('&' | 0x80000000):
  13892. if (flag > 0) {
  13893. *s++ = '&';
  13894. } else {
  13895. *s++ = '&';
  13896. t = (char *) "amp;";
  13897. m = 4;
  13898. }
  13899. break;
  13900. case (soap_wchar) ('"' | 0x80000000):
  13901. if (flag > 0) {
  13902. *s++ = '"';
  13903. } else {
  13904. *s++ = '&';
  13905. t = (char *) "quot;";
  13906. m = 5;
  13907. }
  13908. break;
  13909. case (soap_wchar) ('\'' | 0x80000000):
  13910. if (flag > 0) {
  13911. *s++ = '\'';
  13912. } else {
  13913. *s++ = '&';
  13914. t = (char *) "apos;";
  13915. m = 5;
  13916. }
  13917. break;
  13918. default:
  13919. if ((int) c == EOF)
  13920. goto end;
  13921. #ifndef WITH_CDATA
  13922. if (c == '<')
  13923. {
  13924. c = soap_getchar(soap);
  13925. soap_unget(soap, c);
  13926. if (c == '/')
  13927. {
  13928. c = SOAP_TT;
  13929. if (n == 0)
  13930. goto end;
  13931. n--;
  13932. }
  13933. else
  13934. {
  13935. n++;
  13936. }
  13937. *s++ = '<';
  13938. }
  13939. else
  13940. #endif
  13941. #ifndef WITH_LEANER
  13942. #ifdef HAVE_WCTOMB
  13943. if ((soap->mode & SOAP_C_MBSTRING)) {
  13944. #if defined(WIN32) && !defined(CYGWIN) && !defined(__MINGW32__) && !defined(__MINGW64__) && !defined(__BORLANDC__)
  13945. m = 0;
  13946. wctomb_s(&m, buf, sizeof(buf), (wchar_t)(c & 0x7FFFFFFF));
  13947. #else
  13948. m = wctomb(buf, (wchar_t) (c & 0x7FFFFFFF));
  13949. #endif
  13950. if (m >= 1 && m <= (int) MB_CUR_MAX) {
  13951. t = buf;
  13952. *s++ = *t++;
  13953. m--;
  13954. } else {
  13955. *s++ = SOAP_UNKNOWN_CHAR;
  13956. m = 0;
  13957. }
  13958. } else
  13959. #endif
  13960. #endif
  13961. *s++ = (char) (c & 0xFF);
  13962. }
  13963. l++;
  13964. if (maxlen >= 0 && l > (size_t) maxlen) {
  13965. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "String too long: maxlen=%ld\n", maxlen));
  13966. soap->error = SOAP_LENGTH;
  13967. return NULL;
  13968. }
  13969. }
  13970. }
  13971. end:
  13972. soap_unget(soap, c);
  13973. *s = '\0';
  13974. #ifdef WITH_FAST
  13975. t = soap_strdup(soap, soap->labbuf);
  13976. if (!t)
  13977. return NULL;
  13978. #else
  13979. soap_size_block(soap, NULL, i + 1);
  13980. t = soap_save_block(soap, NULL, NULL, 0);
  13981. #endif
  13982. if (minlen > 0 && l < (size_t) minlen) {
  13983. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "String too short: %lu chars, minlen=%ld\n", (unsigned long) l, minlen));
  13984. soap->error = SOAP_LENGTH;
  13985. return NULL;
  13986. }
  13987. #ifdef WITH_DOM
  13988. if ((soap->mode & SOAP_XML_DOM) && soap->dom && *t)
  13989. {
  13990. if (flag > 0)
  13991. soap->dom->text = t;
  13992. else
  13993. soap->dom->code = t;
  13994. }
  13995. #endif
  13996. if (flag == 2) {
  13997. if (soap_s2QName(soap, t, &t, minlen, maxlen, pattern))
  13998. return NULL;
  13999. } else if (flag >= 4 && t) {
  14000. t = soap_collapse(soap, t, flag, 1);
  14001. }
  14002. #ifndef WITH_LEANER
  14003. else if (pattern && soap->fsvalidate) {
  14004. soap->error = soap->fsvalidate(soap, pattern, t);
  14005. if (soap->error)
  14006. return NULL;
  14007. }
  14008. #endif
  14009. return t;
  14010. }
  14011. /******************************************************************************/
  14012. #ifndef WITH_LEANER
  14013. SOAP_FMAC1
  14014. int
  14015. SOAP_FMAC2
  14016. soap_wstring_out(struct soap *soap, const wchar_t *s, int flag) {
  14017. const char *t;
  14018. char tmp;
  14019. soap_wchar c;
  14020. #ifdef WITH_DOM
  14021. if ((soap->mode & SOAP_XML_DOM) && soap->dom)
  14022. {
  14023. soap->dom->text = soap_wchar2s(soap, s);
  14024. return SOAP_OK;
  14025. }
  14026. #endif
  14027. while ((c = *s++)) {
  14028. switch (c) {
  14029. case 0x09:
  14030. if (flag)
  14031. t = "&#x9;";
  14032. else
  14033. t = "\t";
  14034. break;
  14035. case 0x0A:
  14036. if (flag || !(soap->mode & SOAP_XML_CANONICAL))
  14037. t = "&#xA;";
  14038. else
  14039. t = "\n";
  14040. break;
  14041. case 0x0D:
  14042. t = "&#xD;";
  14043. break;
  14044. case '&':
  14045. t = "&amp;";
  14046. break;
  14047. case '<':
  14048. t = "&lt;";
  14049. break;
  14050. case '>':
  14051. if (flag)
  14052. t = ">";
  14053. else
  14054. t = "&gt;";
  14055. break;
  14056. case '"':
  14057. if (flag)
  14058. t = "&quot;";
  14059. else
  14060. t = "\"";
  14061. break;
  14062. default:
  14063. if (c >= 0x20 && c < 0x80) {
  14064. tmp = (char) c;
  14065. if (soap_send_raw(soap, &tmp, 1))
  14066. return soap->error;
  14067. } else {
  14068. /* check for UTF16 encoding when wchar_t is too small to hold UCS */
  14069. if (sizeof(wchar_t) < 4 && (c & 0xFC00) == 0xD800) {
  14070. soap_wchar d = *s;
  14071. if ((d & 0xFC00) == 0xDC00) {
  14072. c = ((c - 0xD800) << 10) + (d - 0xDC00) + 0x10000;
  14073. s++;
  14074. }
  14075. #ifdef WITH_REPLACE_ILLEGAL_UTF8
  14076. else
  14077. {
  14078. c = SOAP_UNKNOWN_UNICODE_CHAR; /* Malformed UTF-16 */
  14079. }
  14080. #endif
  14081. }
  14082. if (soap_pututf8(soap, (unsigned long) c))
  14083. return soap->error;
  14084. }
  14085. continue;
  14086. }
  14087. if (soap_send(soap, t))
  14088. return soap->error;
  14089. }
  14090. return SOAP_OK;
  14091. }
  14092. #endif
  14093. /******************************************************************************/
  14094. #ifndef WITH_LEANER
  14095. SOAP_FMAC1
  14096. wchar_t *
  14097. SOAP_FMAC2
  14098. soap_wstring_in(struct soap *soap, int flag, long minlen, long maxlen, const char *pattern) {
  14099. wchar_t *s;
  14100. int i, n = 0, f = 0;
  14101. ULONG64 l = 0;
  14102. soap_wchar c;
  14103. char *t = NULL;
  14104. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Reading wide string content\n"));
  14105. if (maxlen < 0 && soap->maxlength > 0)
  14106. maxlen = soap->maxlength;
  14107. if (flag <= 0 && soap->peeked && *soap->tag) {
  14108. #ifndef WITH_LEAN
  14109. struct soap_attribute *tp;
  14110. t = soap->tmpbuf;
  14111. *t = '<';
  14112. soap_strcpy(t + 1, sizeof(soap->tmpbuf) - 1, soap->tag);
  14113. t += strlen(t);
  14114. for (tp = soap->attributes; tp; tp = tp->next) {
  14115. if (tp->visible) {
  14116. size_t k = strlen(tp->name);
  14117. if (t + k + 1 >= soap->tmpbuf + sizeof(soap->tmpbuf))
  14118. break;
  14119. *t++ = ' ';
  14120. (void) soap_strncpy(t, sizeof(soap->tmpbuf) - (t - soap->tmpbuf), tp->name, k);
  14121. t += k;
  14122. if (tp->value) {
  14123. k = strlen(tp->value);
  14124. if (t + k + 3 >= soap->tmpbuf + sizeof(soap->tmpbuf))
  14125. break;
  14126. *t++ = '=';
  14127. *t++ = '"';
  14128. (void) soap_strncpy(t, sizeof(soap->tmpbuf) - (t - soap->tmpbuf), tp->value, k);
  14129. t += k;
  14130. *t++ = '"';
  14131. }
  14132. }
  14133. }
  14134. if (!soap->body)
  14135. *t++ = '/';
  14136. *t++ = '>';
  14137. *t = '\0';
  14138. t = soap->tmpbuf;
  14139. #endif
  14140. if (soap->body)
  14141. n = 1;
  14142. f = 1;
  14143. soap->peeked = 0;
  14144. }
  14145. if (soap_alloc_block(soap) == NULL)
  14146. return NULL;
  14147. for (;;) {
  14148. s = (wchar_t *) soap_push_block(soap, NULL, sizeof(wchar_t) * SOAP_BLKLEN);
  14149. if (!s)
  14150. return NULL;
  14151. for (i = 0; i < SOAP_BLKLEN; i++) {
  14152. if (t) {
  14153. *s++ = (wchar_t) *t++;
  14154. if (!*t)
  14155. t = NULL;
  14156. continue;
  14157. }
  14158. c = soap_getutf8(soap);
  14159. switch (c) {
  14160. case SOAP_TT:
  14161. if (n == 0)
  14162. goto end;
  14163. n--;
  14164. *s++ = L'<';
  14165. soap_unget(soap, '/');
  14166. break;
  14167. case SOAP_LT:
  14168. if (flag == 3 || (f && n == 0))
  14169. goto end;
  14170. n++;
  14171. *s++ = L'<';
  14172. break;
  14173. case SOAP_GT:
  14174. *s++ = L'>';
  14175. break;
  14176. case SOAP_QT:
  14177. *s++ = L'"';
  14178. break;
  14179. case SOAP_AP:
  14180. *s++ = L'\'';
  14181. break;
  14182. case '/':
  14183. if (n > 0) {
  14184. c = soap_getutf8(soap);
  14185. if (c == SOAP_GT)
  14186. n--;
  14187. soap_unget(soap, c);
  14188. }
  14189. *s++ = L'/';
  14190. break;
  14191. case '<':
  14192. if (flag > 0) {
  14193. *s++ = L'<';
  14194. } else {
  14195. *s++ = L'&';
  14196. t = (char *) "lt;";
  14197. }
  14198. break;
  14199. case '>':
  14200. if (flag > 0) {
  14201. *s++ = L'>';
  14202. } else {
  14203. *s++ = (wchar_t) '&';
  14204. t = (char *) "gt;";
  14205. }
  14206. break;
  14207. case '"':
  14208. if (flag > 0) {
  14209. *s++ = L'"';
  14210. } else {
  14211. *s++ = L'&';
  14212. t = (char *) "quot;";
  14213. }
  14214. break;
  14215. default:
  14216. if ((int) c == EOF)
  14217. goto end;
  14218. /* use UTF16 encoding when wchar_t is too small to hold UCS */
  14219. if (sizeof(wchar_t) < 4 && c > 0xFFFF) {
  14220. soap_wchar c1, c2;
  14221. c1 = 0xD800 - (0x10000 >> 10) + (c >> 10);
  14222. c2 = 0xDC00 + (c & 0x3FF);
  14223. c = c1;
  14224. soap_unget(soap, c2);
  14225. }
  14226. *s++ = (wchar_t) (c & 0x7FFFFFFF);
  14227. }
  14228. l++;
  14229. if (maxlen >= 0 && l > (size_t) maxlen) {
  14230. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "String too long: maxlen=%ld\n", maxlen));
  14231. soap->error = SOAP_LENGTH;
  14232. return NULL;
  14233. }
  14234. }
  14235. }
  14236. end:
  14237. soap_unget(soap, c);
  14238. *s = L'\0';
  14239. soap_size_block(soap, NULL, sizeof(wchar_t) * (i + 1));
  14240. if (minlen > 0 && l < (size_t) minlen) {
  14241. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "String too short: %lu chars, minlen=%ld\n", (unsigned long) l, minlen));
  14242. soap->error = SOAP_LENGTH;
  14243. return NULL;
  14244. }
  14245. s = (wchar_t *) soap_save_block(soap, NULL, NULL, 0);
  14246. #ifndef WITH_LEAN
  14247. if (flag >= 4 && s)
  14248. s = soap_wcollapse(soap, s, flag, 1);
  14249. #endif
  14250. #ifndef WITH_LEANER
  14251. if (pattern && soap->fwvalidate) {
  14252. soap->error = soap->fwvalidate(soap, pattern, s);
  14253. if (soap->error)
  14254. return NULL;
  14255. }
  14256. #endif
  14257. #ifdef WITH_DOM
  14258. if ((soap->mode & SOAP_XML_DOM) && soap->dom)
  14259. soap->dom->text = soap_wchar2s(soap, s);
  14260. #endif
  14261. return s;
  14262. }
  14263. #endif
  14264. /******************************************************************************/
  14265. SOAP_FMAC1
  14266. const char *
  14267. SOAP_FMAC2
  14268. soap_int2s(struct soap *soap, int n) {
  14269. return soap_long2s(soap, (long) n);
  14270. }
  14271. /******************************************************************************/
  14272. SOAP_FMAC1
  14273. int
  14274. SOAP_FMAC2
  14275. soap_outint(struct soap *soap, const char *tag, int id, const int *p, const char *type, int n) {
  14276. if (soap_element_begin_out(soap, tag, soap_embedded_id(soap, id, p, n), type)
  14277. || soap_string_out(soap, soap_long2s(soap, (long) *p), 0))
  14278. return soap->error;
  14279. return soap_element_end_out(soap, tag);
  14280. }
  14281. /******************************************************************************/
  14282. SOAP_FMAC1
  14283. int
  14284. SOAP_FMAC2
  14285. soap_s2int(struct soap *soap, const char *s, int *p) {
  14286. if (s) {
  14287. long n;
  14288. char *r;
  14289. if (!*s)
  14290. return soap->error = SOAP_EMPTY;
  14291. #ifndef WITH_NOIO
  14292. #ifndef WITH_LEAN
  14293. soap_reset_errno;
  14294. #endif
  14295. #endif
  14296. n = soap_strtol(s, &r, 10);
  14297. if (s == r || *r
  14298. #ifndef WITH_LEAN
  14299. || n != (int) n
  14300. #endif
  14301. #ifndef WITH_NOIO
  14302. #ifndef WITH_LEAN
  14303. || soap_errno == SOAP_ERANGE
  14304. #endif
  14305. #endif
  14306. )
  14307. soap->error = SOAP_TYPE;
  14308. *p = (int) n;
  14309. }
  14310. return soap->error;
  14311. }
  14312. /******************************************************************************/
  14313. SOAP_FMAC1
  14314. int *
  14315. SOAP_FMAC2
  14316. soap_inint(struct soap *soap, const char *tag, int *p, const char *type, int t) {
  14317. if (soap_element_begin_in(soap, tag, 0, NULL))
  14318. return NULL;
  14319. #ifndef WITH_LEAN
  14320. if (*soap->type
  14321. && soap_match_tag(soap, soap->type, type)
  14322. && soap_match_tag(soap, soap->type, ":int")
  14323. && soap_match_tag(soap, soap->type, ":short")
  14324. && soap_match_tag(soap, soap->type, ":byte")) {
  14325. soap->error = SOAP_TYPE;
  14326. soap_revert(soap);
  14327. return NULL;
  14328. }
  14329. #else
  14330. (void)type;
  14331. #endif
  14332. p = (int *) soap_id_enter(soap, soap->id, p, t, sizeof(int), NULL, NULL, NULL, NULL);
  14333. if (!p)
  14334. return NULL;
  14335. if (*soap->href != '#') {
  14336. int err = soap_s2int(soap, soap_value(soap), p);
  14337. if ((soap->body && soap_element_end_in(soap, tag)) || err)
  14338. return NULL;
  14339. } else {
  14340. p = (int *) soap_id_forward(soap, soap->href, p, 0, t, t, sizeof(int), 0, NULL, NULL);
  14341. if (soap->body && soap_element_end_in(soap, tag))
  14342. return NULL;
  14343. }
  14344. return p;
  14345. }
  14346. /******************************************************************************/
  14347. SOAP_FMAC1
  14348. const char *
  14349. SOAP_FMAC2
  14350. soap_long2s(struct soap *soap, long n) {
  14351. (SOAP_SNPRINTF(soap->tmpbuf, sizeof(soap->tmpbuf), 20), "%ld", n);
  14352. return soap->tmpbuf;
  14353. }
  14354. /******************************************************************************/
  14355. SOAP_FMAC1
  14356. int
  14357. SOAP_FMAC2
  14358. soap_outlong(struct soap *soap, const char *tag, int id, const long *p, const char *type, int n) {
  14359. if (soap_element_begin_out(soap, tag, soap_embedded_id(soap, id, p, n), type)
  14360. || soap_string_out(soap, soap_long2s(soap, *p), 0))
  14361. return soap->error;
  14362. return soap_element_end_out(soap, tag);
  14363. }
  14364. /******************************************************************************/
  14365. SOAP_FMAC1
  14366. int
  14367. SOAP_FMAC2
  14368. soap_s2long(struct soap *soap, const char *s, long *p) {
  14369. if (s) {
  14370. char *r;
  14371. if (!*s)
  14372. return soap->error = SOAP_EMPTY;
  14373. #ifndef WITH_NOIO
  14374. #ifndef WITH_LEAN
  14375. soap_reset_errno;
  14376. #endif
  14377. #endif
  14378. *p = soap_strtol(s, &r, 10);
  14379. if (s == r || *r
  14380. #ifndef WITH_NOIO
  14381. #ifndef WITH_LEAN
  14382. || soap_errno == SOAP_ERANGE
  14383. #endif
  14384. #endif
  14385. )
  14386. soap->error = SOAP_TYPE;
  14387. }
  14388. return soap->error;
  14389. }
  14390. /******************************************************************************/
  14391. SOAP_FMAC1
  14392. long *
  14393. SOAP_FMAC2
  14394. soap_inlong(struct soap *soap, const char *tag, long *p, const char *type, int t) {
  14395. if (soap_element_begin_in(soap, tag, 0, NULL))
  14396. return NULL;
  14397. #ifndef WITH_LEAN
  14398. if (*soap->type
  14399. && soap_match_tag(soap, soap->type, type)
  14400. && soap_match_tag(soap, soap->type, ":int")
  14401. && soap_match_tag(soap, soap->type, ":short")
  14402. && soap_match_tag(soap, soap->type, ":byte")) {
  14403. soap->error = SOAP_TYPE;
  14404. soap_revert(soap);
  14405. return NULL;
  14406. }
  14407. #else
  14408. (void)type;
  14409. #endif
  14410. p = (long *) soap_id_enter(soap, soap->id, p, t, sizeof(long), NULL, NULL, NULL, NULL);
  14411. if (!p)
  14412. return NULL;
  14413. if (*soap->href != '#') {
  14414. int err = soap_s2long(soap, soap_value(soap), p);
  14415. if ((soap->body && soap_element_end_in(soap, tag)) || err)
  14416. return NULL;
  14417. } else {
  14418. p = (long *) soap_id_forward(soap, soap->href, p, 0, t, t, sizeof(long), 0, NULL, NULL);
  14419. if (soap->body && soap_element_end_in(soap, tag))
  14420. return NULL;
  14421. }
  14422. return p;
  14423. }
  14424. /******************************************************************************/
  14425. SOAP_FMAC1
  14426. const char *
  14427. SOAP_FMAC2
  14428. soap_LONG642s(struct soap *soap, LONG64 n) {
  14429. (SOAP_SNPRINTF(soap->tmpbuf, sizeof(soap->tmpbuf), 20), SOAP_LONG_FORMAT, n);
  14430. return soap->tmpbuf;
  14431. }
  14432. /******************************************************************************/
  14433. SOAP_FMAC1
  14434. int
  14435. SOAP_FMAC2
  14436. soap_outLONG64(struct soap *soap, const char *tag, int id, const LONG64 *p, const char *type, int n) {
  14437. if (soap_element_begin_out(soap, tag, soap_embedded_id(soap, id, p, n), type)
  14438. || soap_string_out(soap, soap_LONG642s(soap, *p), 0))
  14439. return soap->error;
  14440. return soap_element_end_out(soap, tag);
  14441. }
  14442. /******************************************************************************/
  14443. SOAP_FMAC1
  14444. int
  14445. SOAP_FMAC2
  14446. soap_s2LONG64(struct soap *soap, const char *s, LONG64 *p) {
  14447. if (s) {
  14448. char *r;
  14449. if (!*s)
  14450. return soap->error = SOAP_EMPTY;
  14451. #ifndef WITH_NOIO
  14452. #ifndef WITH_LEAN
  14453. soap_reset_errno;
  14454. #endif
  14455. #endif
  14456. *p = soap_strtoll(s, &r, 10);
  14457. if (s == r || *r
  14458. #ifndef WITH_NOIO
  14459. #ifndef WITH_LEAN
  14460. || soap_errno == SOAP_ERANGE
  14461. #endif
  14462. #endif
  14463. )
  14464. soap->error = SOAP_TYPE;
  14465. }
  14466. return soap->error;
  14467. }
  14468. /******************************************************************************/
  14469. SOAP_FMAC1
  14470. LONG64 *
  14471. SOAP_FMAC2
  14472. soap_inLONG64(struct soap *soap, const char *tag, LONG64 *p, const char *type, int t) {
  14473. if (soap_element_begin_in(soap, tag, 0, NULL))
  14474. return NULL;
  14475. #ifndef WITH_LEAN
  14476. if (*soap->type
  14477. && soap_match_tag(soap, soap->type, type)
  14478. && soap_match_tag(soap, soap->type, ":integer")
  14479. && soap_match_tag(soap, soap->type, ":positiveInteger")
  14480. && soap_match_tag(soap, soap->type, ":negativeInteger")
  14481. && soap_match_tag(soap, soap->type, ":nonPositiveInteger")
  14482. && soap_match_tag(soap, soap->type, ":nonNegativeInteger")
  14483. && soap_match_tag(soap, soap->type, ":long")
  14484. && soap_match_tag(soap, soap->type, ":int")
  14485. && soap_match_tag(soap, soap->type, ":short")
  14486. && soap_match_tag(soap, soap->type, ":byte")) {
  14487. soap->error = SOAP_TYPE;
  14488. soap_revert(soap);
  14489. return NULL;
  14490. }
  14491. #else
  14492. (void)type;
  14493. #endif
  14494. p = (LONG64 *) soap_id_enter(soap, soap->id, p, t, sizeof(LONG64), NULL, NULL, NULL, NULL);
  14495. if (!p)
  14496. return NULL;
  14497. if (*soap->href != '#') {
  14498. int err = soap_s2LONG64(soap, soap_value(soap), p);
  14499. if ((soap->body && soap_element_end_in(soap, tag)) || err)
  14500. return NULL;
  14501. } else {
  14502. p = (LONG64 *) soap_id_forward(soap, soap->href, p, 0, t, t, sizeof(LONG64), 0, NULL, NULL);
  14503. if (soap->body && soap_element_end_in(soap, tag))
  14504. return NULL;
  14505. }
  14506. return p;
  14507. }
  14508. /******************************************************************************/
  14509. SOAP_FMAC1
  14510. const char *
  14511. SOAP_FMAC2
  14512. soap_byte2s(struct soap *soap, char n) {
  14513. return soap_long2s(soap, (long) n);
  14514. }
  14515. /******************************************************************************/
  14516. SOAP_FMAC1
  14517. int
  14518. SOAP_FMAC2
  14519. soap_outbyte(struct soap *soap, const char *tag, int id, const char *p, const char *type, int n) {
  14520. if (soap_element_begin_out(soap, tag, soap_embedded_id(soap, id, p, n), type)
  14521. || soap_string_out(soap, soap_long2s(soap, (long) *p), 0))
  14522. return soap->error;
  14523. return soap_element_end_out(soap, tag);
  14524. }
  14525. /******************************************************************************/
  14526. SOAP_FMAC1
  14527. int
  14528. SOAP_FMAC2
  14529. soap_s2byte(struct soap *soap, const char *s, char *p) {
  14530. if (s) {
  14531. long n;
  14532. char *r;
  14533. if (!*s)
  14534. return soap->error = SOAP_EMPTY;
  14535. n = soap_strtol(s, &r, 10);
  14536. if (s == r || *r || n < -128 || n > 127)
  14537. soap->error = SOAP_TYPE;
  14538. *p = (char) n;
  14539. }
  14540. return soap->error;
  14541. }
  14542. /******************************************************************************/
  14543. SOAP_FMAC1
  14544. char *
  14545. SOAP_FMAC2
  14546. soap_inbyte(struct soap *soap, const char *tag, char *p, const char *type, int t) {
  14547. if (soap_element_begin_in(soap, tag, 0, NULL))
  14548. return NULL;
  14549. #ifndef WITH_LEAN
  14550. if (*soap->type
  14551. && soap_match_tag(soap, soap->type, type)
  14552. && soap_match_tag(soap, soap->type, ":byte")) {
  14553. soap->error = SOAP_TYPE;
  14554. soap_revert(soap);
  14555. return NULL;
  14556. }
  14557. #else
  14558. (void)type;
  14559. #endif
  14560. p = (char *) soap_id_enter(soap, soap->id, p, t, sizeof(char), NULL, NULL, NULL, NULL);
  14561. if (!p)
  14562. return NULL;
  14563. if (*soap->href != '#') {
  14564. int err = soap_s2byte(soap, soap_value(soap), p);
  14565. if ((soap->body && soap_element_end_in(soap, tag)) || err)
  14566. return NULL;
  14567. } else {
  14568. p = (char *) soap_id_forward(soap, soap->href, p, 0, t, t, sizeof(char), 0, NULL, NULL);
  14569. if (soap->body && soap_element_end_in(soap, tag))
  14570. return NULL;
  14571. }
  14572. return p;
  14573. }
  14574. /******************************************************************************/
  14575. SOAP_FMAC1
  14576. const char *
  14577. SOAP_FMAC2
  14578. soap_short2s(struct soap *soap, short n) {
  14579. return soap_long2s(soap, (long) n);
  14580. }
  14581. /******************************************************************************/
  14582. SOAP_FMAC1
  14583. int
  14584. SOAP_FMAC2
  14585. soap_outshort(struct soap *soap, const char *tag, int id, const short *p, const char *type, int n) {
  14586. if (soap_element_begin_out(soap, tag, soap_embedded_id(soap, id, p, n), type)
  14587. || soap_string_out(soap, soap_long2s(soap, (long) *p), 0))
  14588. return soap->error;
  14589. return soap_element_end_out(soap, tag);
  14590. }
  14591. /******************************************************************************/
  14592. SOAP_FMAC1
  14593. int
  14594. SOAP_FMAC2
  14595. soap_s2short(struct soap *soap, const char *s, short *p) {
  14596. if (s) {
  14597. long n;
  14598. char *r;
  14599. if (!*s)
  14600. return soap->error = SOAP_EMPTY;
  14601. n = soap_strtol(s, &r, 10);
  14602. if (s == r || *r || n < -32768 || n > 32767)
  14603. soap->error = SOAP_TYPE;
  14604. *p = (short) n;
  14605. }
  14606. return soap->error;
  14607. }
  14608. /******************************************************************************/
  14609. SOAP_FMAC1
  14610. short *
  14611. SOAP_FMAC2
  14612. soap_inshort(struct soap *soap, const char *tag, short *p, const char *type, int t) {
  14613. if (soap_element_begin_in(soap, tag, 0, NULL))
  14614. return NULL;
  14615. #ifndef WITH_LEAN
  14616. if (*soap->type
  14617. && soap_match_tag(soap, soap->type, type)
  14618. && soap_match_tag(soap, soap->type, ":short")
  14619. && soap_match_tag(soap, soap->type, ":byte")) {
  14620. soap->error = SOAP_TYPE;
  14621. soap_revert(soap);
  14622. return NULL;
  14623. }
  14624. #else
  14625. (void)type;
  14626. #endif
  14627. p = (short *) soap_id_enter(soap, soap->id, p, t, sizeof(short), NULL, NULL, NULL, NULL);
  14628. if (!p)
  14629. return NULL;
  14630. if (*soap->href != '#') {
  14631. int err = soap_s2short(soap, soap_value(soap), p);
  14632. if ((soap->body && soap_element_end_in(soap, tag)) || err)
  14633. return NULL;
  14634. } else {
  14635. p = (short *) soap_id_forward(soap, soap->href, p, 0, t, t, sizeof(short), 0, NULL, NULL);
  14636. if (soap->body && soap_element_end_in(soap, tag))
  14637. return NULL;
  14638. }
  14639. return p;
  14640. }
  14641. /******************************************************************************/
  14642. SOAP_FMAC1
  14643. const char *
  14644. SOAP_FMAC2
  14645. soap_float2s(struct soap *soap, float n) {
  14646. #if defined(WITH_C_LOCALE)
  14647. # if !defined(WIN32)
  14648. SOAP_LOCALE_T locale;
  14649. # endif
  14650. #else
  14651. char *s;
  14652. #endif
  14653. if (soap_isnan((double) n))
  14654. return "NaN";
  14655. if (soap_ispinff(n))
  14656. return "INF";
  14657. if (soap_isninff(n))
  14658. return "-INF";
  14659. #if defined(WITH_C_LOCALE)
  14660. # ifdef WIN32
  14661. _sprintf_s_l(soap->tmpbuf, _countof(soap->tmpbuf), soap->float_format, SOAP_LOCALE(soap), n);
  14662. # else
  14663. locale = uselocale(SOAP_LOCALE(soap));
  14664. (SOAP_SNPRINTF(soap->tmpbuf, sizeof(soap->tmpbuf), 20), soap->float_format, n);
  14665. uselocale(locale);
  14666. # endif
  14667. #else
  14668. (SOAP_SNPRINTF(soap->tmpbuf, sizeof(soap->tmpbuf), 20), soap->float_format, n);
  14669. s = strchr(soap->tmpbuf, ','); /* convert decimal comma to DP */
  14670. if (s)
  14671. *s = '.';
  14672. #endif
  14673. return soap->tmpbuf;
  14674. }
  14675. /******************************************************************************/
  14676. SOAP_FMAC1
  14677. int
  14678. SOAP_FMAC2
  14679. soap_outfloat(struct soap *soap, const char *tag, int id, const float *p, const char *type, int n) {
  14680. if (soap_element_begin_out(soap, tag, soap_embedded_id(soap, id, p, n), type)
  14681. || soap_string_out(soap, soap_float2s(soap, *p), 0))
  14682. return soap->error;
  14683. return soap_element_end_out(soap, tag);
  14684. }
  14685. /******************************************************************************/
  14686. SOAP_FMAC1
  14687. int
  14688. SOAP_FMAC2
  14689. soap_s2float(struct soap *soap, const char *s, float *p) {
  14690. if (s) {
  14691. if (!*s)
  14692. return soap->error = SOAP_EMPTY;
  14693. if (!soap_tag_cmp(s, "INF")) {
  14694. *p = FLT_PINFTY;
  14695. } else if (!soap_tag_cmp(s, "+INF")) {
  14696. *p = FLT_PINFTY;
  14697. } else if (!soap_tag_cmp(s, "-INF")) {
  14698. *p = FLT_NINFTY;
  14699. } else if (!soap_tag_cmp(s, "NaN")) {
  14700. *p = FLT_NAN;
  14701. } else {
  14702. /* On some systems strtof requires -std=c99 or does not even link: so we try strtod first */
  14703. #if defined(WITH_C_LOCALE)
  14704. # if defined(HAVE_STRTOD_L)
  14705. char *r;
  14706. # ifdef WIN32
  14707. *p = (float)_strtod_l(s, &r, SOAP_LOCALE(soap));
  14708. # else
  14709. *p = (float) strtod_l(s, &r, SOAP_LOCALE(soap));
  14710. # endif
  14711. if (*r)
  14712. soap->error = SOAP_TYPE;
  14713. # elif defined(HAVE_STRTOF_L)
  14714. char *r;
  14715. *p = strtof_l((char*)s, &r, SOAP_LOCALE(soap));
  14716. if (*r)
  14717. soap->error = SOAP_TYPE;
  14718. # elif defined(HAVE_SSCANF_L)
  14719. double n;
  14720. if (sscanf_l(s, SOAP_LOCALE(soap), "%lf", &n) != 1)
  14721. soap->error = SOAP_TYPE;
  14722. *p = (float)n;
  14723. # elif defined(HAVE_STRTOD)
  14724. char *r;
  14725. SOAP_LOCALE_T locale = uselocale(SOAP_LOCALE(soap));
  14726. *p = (float)strtod((char*)s, &r);
  14727. uselocale(locale);
  14728. if (*r)
  14729. soap->error = SOAP_TYPE;
  14730. # elif defined(HAVE_STRTOF)
  14731. char *r;
  14732. SOAP_LOCALE_T locale = uselocale(SOAP_LOCALE(soap));
  14733. *p = strtof((char*)s, &r);
  14734. uselocale(locale);
  14735. if (*r)
  14736. soap->error = SOAP_TYPE;
  14737. # elif defined(HAVE_SSCANF)
  14738. double n;
  14739. SOAP_LOCALE_T locale = uselocale(SOAP_LOCALE(soap));
  14740. if (sscanf(s, "%lf", &n) != 1)
  14741. soap->error = SOAP_TYPE;
  14742. uselocale(locale);
  14743. *p = (float)n;
  14744. # else
  14745. soap->error = SOAP_TYPE;
  14746. # endif
  14747. #elif defined(HAVE_STRTOD)
  14748. char *r;
  14749. *p = (float)strtod(s, &r);
  14750. if (*r)
  14751. soap->error = SOAP_TYPE;
  14752. #elif defined(HAVE_STRTOF)
  14753. char *r;
  14754. *p = strtof((char*)s, &r);
  14755. if (*r)
  14756. soap->error = SOAP_TYPE;
  14757. #elif defined(HAVE_SSCANF)
  14758. double n;
  14759. if (sscanf(s, "%lf", &n) != 1)
  14760. soap->error = SOAP_TYPE;
  14761. *p = (float)n;
  14762. #else
  14763. soap->error = SOAP_TYPE;
  14764. #endif
  14765. }
  14766. }
  14767. return soap->error;
  14768. }
  14769. /******************************************************************************/
  14770. #ifndef WITH_LEAN
  14771. static int soap_isnumeric(struct soap *soap, const char *type) {
  14772. if (soap_match_tag(soap, soap->type, type)
  14773. && soap_match_tag(soap, soap->type, ":float")
  14774. && soap_match_tag(soap, soap->type, ":double")
  14775. && soap_match_tag(soap, soap->type, ":decimal")
  14776. && soap_match_tag(soap, soap->type, ":integer")
  14777. && soap_match_tag(soap, soap->type, ":positiveInteger")
  14778. && soap_match_tag(soap, soap->type, ":negativeInteger")
  14779. && soap_match_tag(soap, soap->type, ":nonPositiveInteger")
  14780. && soap_match_tag(soap, soap->type, ":nonNegativeInteger")
  14781. && soap_match_tag(soap, soap->type, ":long")
  14782. && soap_match_tag(soap, soap->type, ":int")
  14783. && soap_match_tag(soap, soap->type, ":short")
  14784. && soap_match_tag(soap, soap->type, ":byte")
  14785. && soap_match_tag(soap, soap->type, ":unsignedLong")
  14786. && soap_match_tag(soap, soap->type, ":unsignedInt")
  14787. && soap_match_tag(soap, soap->type, ":unsignedShort")
  14788. && soap_match_tag(soap, soap->type, ":unsignedByte")) {
  14789. soap->error = SOAP_TYPE;
  14790. soap_revert(soap);
  14791. return SOAP_ERR;
  14792. }
  14793. return SOAP_OK;
  14794. }
  14795. #endif
  14796. /******************************************************************************/
  14797. SOAP_FMAC1
  14798. float *
  14799. SOAP_FMAC2
  14800. soap_infloat(struct soap *soap, const char *tag, float *p, const char *type, int t) {
  14801. if (soap_element_begin_in(soap, tag, 0, NULL))
  14802. return NULL;
  14803. #ifndef WITH_LEAN
  14804. if (*soap->type != '\0' && soap_isnumeric(soap, type))
  14805. return NULL;
  14806. #else
  14807. (void)type;
  14808. #endif
  14809. p = (float *) soap_id_enter(soap, soap->id, p, t, sizeof(float), NULL, NULL, NULL, NULL);
  14810. if (!p)
  14811. return NULL;
  14812. if (*soap->href != '#') {
  14813. int err = soap_s2float(soap, soap_value(soap), p);
  14814. if ((soap->body && soap_element_end_in(soap, tag)) || err)
  14815. return NULL;
  14816. } else {
  14817. p = (float *) soap_id_forward(soap, soap->href, p, 0, t, t, sizeof(float), 0, NULL, NULL);
  14818. if (soap->body && soap_element_end_in(soap, tag))
  14819. return NULL;
  14820. }
  14821. return p;
  14822. }
  14823. /******************************************************************************/
  14824. SOAP_FMAC1
  14825. const char *
  14826. SOAP_FMAC2
  14827. soap_double2s(struct soap *soap, double n) {
  14828. #if defined(WITH_C_LOCALE)
  14829. # if !defined(WIN32)
  14830. SOAP_LOCALE_T locale;
  14831. # endif
  14832. #else
  14833. char *s;
  14834. #endif
  14835. if (soap_isnan(n))
  14836. return "NaN";
  14837. if (soap_ispinfd(n))
  14838. return "INF";
  14839. if (soap_isninfd(n))
  14840. return "-INF";
  14841. #if defined(WITH_C_LOCALE)
  14842. # ifdef WIN32
  14843. _sprintf_s_l(soap->tmpbuf, _countof(soap->tmpbuf), soap->double_format, SOAP_LOCALE(soap), n);
  14844. # else
  14845. locale = uselocale(SOAP_LOCALE(soap));
  14846. (SOAP_SNPRINTF(soap->tmpbuf, sizeof(soap->tmpbuf), 40), soap->double_format, n);
  14847. uselocale(locale);
  14848. # endif
  14849. #else
  14850. (SOAP_SNPRINTF(soap->tmpbuf, sizeof(soap->tmpbuf), 40), soap->double_format, n);
  14851. s = strchr(soap->tmpbuf, ','); /* convert decimal comma to DP */
  14852. if (s)
  14853. *s = '.';
  14854. #endif
  14855. return soap->tmpbuf;
  14856. }
  14857. /******************************************************************************/
  14858. SOAP_FMAC1
  14859. int
  14860. SOAP_FMAC2
  14861. soap_outdouble(struct soap *soap, const char *tag, int id, const double *p, const char *type, int n) {
  14862. if (soap_element_begin_out(soap, tag, soap_embedded_id(soap, id, p, n), type)
  14863. || soap_string_out(soap, soap_double2s(soap, *p), 0))
  14864. return soap->error;
  14865. return soap_element_end_out(soap, tag);
  14866. }
  14867. /******************************************************************************/
  14868. SOAP_FMAC1
  14869. int
  14870. SOAP_FMAC2
  14871. soap_s2double(struct soap *soap, const char *s, double *p) {
  14872. if (s) {
  14873. if (!*s)
  14874. return soap->error = SOAP_EMPTY;
  14875. if (!soap_tag_cmp(s, "INF")) {
  14876. *p = DBL_PINFTY;
  14877. } else if (!soap_tag_cmp(s, "+INF")) {
  14878. *p = DBL_PINFTY;
  14879. } else if (!soap_tag_cmp(s, "-INF")) {
  14880. *p = DBL_NINFTY;
  14881. } else if (!soap_tag_cmp(s, "NaN")) {
  14882. *p = DBL_NAN;
  14883. } else {
  14884. #if defined(WITH_C_LOCALE)
  14885. # if defined(HAVE_STRTOD_L)
  14886. char *r;
  14887. # ifdef WIN32
  14888. *p = _strtod_l(s, &r, SOAP_LOCALE(soap));
  14889. # else
  14890. *p = strtod_l(s, &r, SOAP_LOCALE(soap));
  14891. # endif
  14892. if (*r)
  14893. soap->error = SOAP_TYPE;
  14894. # elif defined(HAVE_STRTOD)
  14895. char *r;
  14896. SOAP_LOCALE_T locale = uselocale(SOAP_LOCALE(soap));
  14897. *p = strtod(s, &r);
  14898. uselocale(locale);
  14899. if (*r)
  14900. soap->error = SOAP_TYPE;
  14901. # elif defined(HAVE_SSCANF_L)
  14902. SOAP_LOCALE_T locale = uselocale(SOAP_LOCALE(soap));
  14903. if (sscanf_l(s, SOAP_LOCALE(soap), "%lf", p) != 1)
  14904. soap->error = SOAP_TYPE;
  14905. uselocale(locale);
  14906. # else
  14907. soap->error = SOAP_TYPE;
  14908. # endif
  14909. #elif defined(HAVE_STRTOD)
  14910. char *r;
  14911. *p = strtod(s, &r);
  14912. if (*r)
  14913. soap->error = SOAP_TYPE;
  14914. #elif defined(HAVE_SSCANF)
  14915. if (sscanf(s, "%lf", p) != 1)
  14916. soap->error = SOAP_TYPE;
  14917. #else
  14918. soap->error = SOAP_TYPE;
  14919. #endif
  14920. }
  14921. }
  14922. return soap->error;
  14923. }
  14924. /******************************************************************************/
  14925. SOAP_FMAC1
  14926. double *
  14927. SOAP_FMAC2
  14928. soap_indouble(struct soap *soap, const char *tag, double *p, const char *type, int t) {
  14929. if (soap_element_begin_in(soap, tag, 0, NULL))
  14930. return NULL;
  14931. #ifndef WITH_LEAN
  14932. if (*soap->type != '\0' && soap_isnumeric(soap, type))
  14933. return NULL;
  14934. #else
  14935. (void)type;
  14936. #endif
  14937. p = (double *) soap_id_enter(soap, soap->id, p, t, sizeof(double), NULL, NULL, NULL, NULL);
  14938. if (!p)
  14939. return NULL;
  14940. if (*soap->href != '#') {
  14941. int err = soap_s2double(soap, soap_value(soap), p);
  14942. if ((soap->body && soap_element_end_in(soap, tag)) || err)
  14943. return NULL;
  14944. } else {
  14945. p = (double *) soap_id_forward(soap, soap->href, p, 0, t, t, sizeof(double), 0, NULL, NULL);
  14946. if (soap->body && soap_element_end_in(soap, tag))
  14947. return NULL;
  14948. }
  14949. return p;
  14950. }
  14951. /******************************************************************************/
  14952. SOAP_FMAC1
  14953. const char *
  14954. SOAP_FMAC2
  14955. soap_unsignedByte2s(struct soap *soap, unsigned char n) {
  14956. return soap_unsignedLong2s(soap, (unsigned long) n);
  14957. }
  14958. /******************************************************************************/
  14959. SOAP_FMAC1
  14960. int
  14961. SOAP_FMAC2
  14962. soap_outunsignedByte(struct soap *soap, const char *tag, int id, const unsigned char *p, const char *type, int n) {
  14963. if (soap_element_begin_out(soap, tag, soap_embedded_id(soap, id, p, n), type)
  14964. || soap_string_out(soap, soap_unsignedLong2s(soap, (unsigned long) *p), 0))
  14965. return soap->error;
  14966. return soap_element_end_out(soap, tag);
  14967. }
  14968. /******************************************************************************/
  14969. SOAP_FMAC1
  14970. int
  14971. SOAP_FMAC2
  14972. soap_s2unsignedByte(struct soap *soap, const char *s, unsigned char *p) {
  14973. if (s) {
  14974. long n;
  14975. char *r;
  14976. if (!*s)
  14977. return soap->error = SOAP_EMPTY;
  14978. n = soap_strtol(s, &r, 10);
  14979. if (s == r || *r || n < 0 || n > 255)
  14980. soap->error = SOAP_TYPE;
  14981. *p = (unsigned char) n;
  14982. }
  14983. return soap->error;
  14984. }
  14985. /******************************************************************************/
  14986. SOAP_FMAC1
  14987. unsigned char *
  14988. SOAP_FMAC2
  14989. soap_inunsignedByte(struct soap *soap, const char *tag, unsigned char *p, const char *type, int t) {
  14990. if (soap_element_begin_in(soap, tag, 0, NULL))
  14991. return NULL;
  14992. #ifndef WITH_LEAN
  14993. if (*soap->type
  14994. && soap_match_tag(soap, soap->type, type)
  14995. && soap_match_tag(soap, soap->type, ":unsignedByte")) {
  14996. soap->error = SOAP_TYPE;
  14997. soap_revert(soap);
  14998. return NULL;
  14999. }
  15000. #else
  15001. (void)type;
  15002. #endif
  15003. p = (unsigned char *) soap_id_enter(soap, soap->id, p, t, sizeof(unsigned char), NULL, NULL, NULL, NULL);
  15004. if (!p)
  15005. return NULL;
  15006. if (*soap->href != '#') {
  15007. int err = soap_s2unsignedByte(soap, soap_value(soap), p);
  15008. if ((soap->body && soap_element_end_in(soap, tag)) || err)
  15009. return NULL;
  15010. } else {
  15011. p = (unsigned char *) soap_id_forward(soap, soap->href, p, 0, t, t, sizeof(unsigned char), 0, NULL, NULL);
  15012. if (soap->body && soap_element_end_in(soap, tag))
  15013. return NULL;
  15014. }
  15015. return p;
  15016. }
  15017. /******************************************************************************/
  15018. SOAP_FMAC1
  15019. const char *
  15020. SOAP_FMAC2
  15021. soap_unsignedShort2s(struct soap *soap, unsigned short n) {
  15022. return soap_unsignedLong2s(soap, (unsigned long) n);
  15023. }
  15024. /******************************************************************************/
  15025. SOAP_FMAC1
  15026. int
  15027. SOAP_FMAC2
  15028. soap_outunsignedShort(struct soap *soap, const char *tag, int id, const unsigned short *p, const char *type, int n) {
  15029. if (soap_element_begin_out(soap, tag, soap_embedded_id(soap, id, p, n), type)
  15030. || soap_string_out(soap, soap_unsignedLong2s(soap, (unsigned long) *p), 0))
  15031. return soap->error;
  15032. return soap_element_end_out(soap, tag);
  15033. }
  15034. /******************************************************************************/
  15035. SOAP_FMAC1
  15036. int
  15037. SOAP_FMAC2
  15038. soap_s2unsignedShort(struct soap *soap, const char *s, unsigned short *p) {
  15039. if (s) {
  15040. long n;
  15041. char *r;
  15042. if (!*s)
  15043. return soap->error = SOAP_EMPTY;
  15044. n = soap_strtol(s, &r, 10);
  15045. if (s == r || *r || n < 0 || n > 65535)
  15046. soap->error = SOAP_TYPE;
  15047. *p = (unsigned short) n;
  15048. }
  15049. return soap->error;
  15050. }
  15051. /******************************************************************************/
  15052. SOAP_FMAC1
  15053. unsigned short *
  15054. SOAP_FMAC2
  15055. soap_inunsignedShort(struct soap *soap, const char *tag, unsigned short *p, const char *type, int t) {
  15056. if (soap_element_begin_in(soap, tag, 0, NULL))
  15057. return NULL;
  15058. #ifndef WITH_LEAN
  15059. if (*soap->type
  15060. && soap_match_tag(soap, soap->type, type)
  15061. && soap_match_tag(soap, soap->type, ":unsignedShort")
  15062. && soap_match_tag(soap, soap->type, ":unsignedByte")) {
  15063. soap->error = SOAP_TYPE;
  15064. soap_revert(soap);
  15065. return NULL;
  15066. }
  15067. #else
  15068. (void)type;
  15069. #endif
  15070. p = (unsigned short *) soap_id_enter(soap, soap->id, p, t, sizeof(unsigned short), NULL, NULL, NULL, NULL);
  15071. if (!p)
  15072. return NULL;
  15073. if (*soap->href != '#') {
  15074. int err = soap_s2unsignedShort(soap, soap_value(soap), p);
  15075. if ((soap->body && soap_element_end_in(soap, tag)) || err)
  15076. return NULL;
  15077. } else {
  15078. p = (unsigned short *) soap_id_forward(soap, soap->href, p, 0, t, t, sizeof(unsigned short), 0, NULL, NULL);
  15079. if (soap->body && soap_element_end_in(soap, tag))
  15080. return NULL;
  15081. }
  15082. return p;
  15083. }
  15084. /******************************************************************************/
  15085. SOAP_FMAC1
  15086. const char *
  15087. SOAP_FMAC2
  15088. soap_unsignedInt2s(struct soap *soap, unsigned int n) {
  15089. return soap_unsignedLong2s(soap, (unsigned long) n);
  15090. }
  15091. /******************************************************************************/
  15092. SOAP_FMAC1
  15093. int
  15094. SOAP_FMAC2
  15095. soap_outunsignedInt(struct soap *soap, const char *tag, int id, const unsigned int *p, const char *type, int n) {
  15096. if (soap_element_begin_out(soap, tag, soap_embedded_id(soap, id, p, n), type)
  15097. || soap_string_out(soap, soap_unsignedLong2s(soap, (unsigned long) *p), 0))
  15098. return soap->error;
  15099. return soap_element_end_out(soap, tag);
  15100. }
  15101. /******************************************************************************/
  15102. SOAP_FMAC1
  15103. int
  15104. SOAP_FMAC2
  15105. soap_s2unsignedInt(struct soap *soap, const char *s, unsigned int *p) {
  15106. if (s) {
  15107. char *r;
  15108. if (!*s)
  15109. return soap->error = SOAP_EMPTY;
  15110. #ifndef WITH_NOIO
  15111. #ifndef WITH_LEAN
  15112. soap_reset_errno;
  15113. #endif
  15114. #endif
  15115. *p = (unsigned int) soap_strtoul(s, &r, 10);
  15116. if (s == r || *r
  15117. #ifndef WITH_NOIO
  15118. #ifndef WITH_LEAN
  15119. || soap_errno == SOAP_ERANGE
  15120. #endif
  15121. #endif
  15122. )
  15123. soap->error = SOAP_TYPE;
  15124. #ifdef HAVE_STRTOUL
  15125. if (*p > 0 && strchr(s, '-'))
  15126. return soap->error = SOAP_TYPE;
  15127. #endif
  15128. }
  15129. return soap->error;
  15130. }
  15131. /******************************************************************************/
  15132. SOAP_FMAC1
  15133. unsigned int *
  15134. SOAP_FMAC2
  15135. soap_inunsignedInt(struct soap *soap, const char *tag, unsigned int *p, const char *type, int t) {
  15136. if (soap_element_begin_in(soap, tag, 0, NULL))
  15137. return NULL;
  15138. #ifndef WITH_LEAN
  15139. if (*soap->type
  15140. && soap_match_tag(soap, soap->type, type)
  15141. && soap_match_tag(soap, soap->type, ":unsignedInt")
  15142. && soap_match_tag(soap, soap->type, ":unsignedShort")
  15143. && soap_match_tag(soap, soap->type, ":unsignedByte")) {
  15144. soap->error = SOAP_TYPE;
  15145. soap_revert(soap);
  15146. return NULL;
  15147. }
  15148. #else
  15149. (void)type;
  15150. #endif
  15151. p = (unsigned int *) soap_id_enter(soap, soap->id, p, t, sizeof(unsigned int), NULL, NULL, NULL, NULL);
  15152. if (!p)
  15153. return NULL;
  15154. if (*soap->href != '#') {
  15155. int err = soap_s2unsignedInt(soap, soap_value(soap), p);
  15156. if ((soap->body && soap_element_end_in(soap, tag)) || err)
  15157. return NULL;
  15158. } else {
  15159. p = (unsigned int *) soap_id_forward(soap, soap->href, p, 0, t, t, sizeof(unsigned int), 0, NULL, NULL);
  15160. if (soap->body && soap_element_end_in(soap, tag))
  15161. return NULL;
  15162. }
  15163. return p;
  15164. }
  15165. /******************************************************************************/
  15166. SOAP_FMAC1
  15167. const char *
  15168. SOAP_FMAC2
  15169. soap_unsignedLong2s(struct soap *soap, unsigned long n) {
  15170. (SOAP_SNPRINTF(soap->tmpbuf, sizeof(soap->tmpbuf), 20), "%lu", n);
  15171. return soap->tmpbuf;
  15172. }
  15173. /******************************************************************************/
  15174. SOAP_FMAC1
  15175. int
  15176. SOAP_FMAC2
  15177. soap_outunsignedLong(struct soap *soap, const char *tag, int id, const unsigned long *p, const char *type, int n) {
  15178. if (soap_element_begin_out(soap, tag, soap_embedded_id(soap, id, p, n), type)
  15179. || soap_string_out(soap, soap_unsignedLong2s(soap, *p), 0))
  15180. return soap->error;
  15181. return soap_element_end_out(soap, tag);
  15182. }
  15183. /******************************************************************************/
  15184. SOAP_FMAC1
  15185. int
  15186. SOAP_FMAC2
  15187. soap_s2unsignedLong(struct soap *soap, const char *s, unsigned long *p) {
  15188. if (s) {
  15189. char *r;
  15190. if (!*s)
  15191. return soap->error = SOAP_EMPTY;
  15192. #ifndef WITH_NOIO
  15193. #ifndef WITH_LEAN
  15194. soap_reset_errno;
  15195. #endif
  15196. #endif
  15197. *p = soap_strtoul(s, &r, 10);
  15198. if (s == r || *r
  15199. #ifndef WITH_NOIO
  15200. #ifndef WITH_LEAN
  15201. || soap_errno == SOAP_ERANGE
  15202. #endif
  15203. #endif
  15204. )
  15205. soap->error = SOAP_TYPE;
  15206. #ifdef HAVE_STRTOUL
  15207. if (*p > 0 && strchr(s, '-'))
  15208. return soap->error = SOAP_TYPE;
  15209. #endif
  15210. }
  15211. return soap->error;
  15212. }
  15213. /******************************************************************************/
  15214. SOAP_FMAC1
  15215. unsigned long *
  15216. SOAP_FMAC2
  15217. soap_inunsignedLong(struct soap *soap, const char *tag, unsigned long *p, const char *type, int t) {
  15218. if (soap_element_begin_in(soap, tag, 0, NULL))
  15219. return NULL;
  15220. #ifndef WITH_LEAN
  15221. if (*soap->type
  15222. && soap_match_tag(soap, soap->type, type)
  15223. && soap_match_tag(soap, soap->type, ":unsignedInt")
  15224. && soap_match_tag(soap, soap->type, ":unsignedShort")
  15225. && soap_match_tag(soap, soap->type, ":unsignedByte")) {
  15226. soap->error = SOAP_TYPE;
  15227. soap_revert(soap);
  15228. return NULL;
  15229. }
  15230. #else
  15231. (void)type;
  15232. #endif
  15233. p = (unsigned long *) soap_id_enter(soap, soap->id, p, t, sizeof(unsigned long), NULL, NULL, NULL, NULL);
  15234. if (!p)
  15235. return NULL;
  15236. if (*soap->href != '#') {
  15237. int err = soap_s2unsignedLong(soap, soap_value(soap), p);
  15238. if ((soap->body && soap_element_end_in(soap, tag)) || err)
  15239. return NULL;
  15240. } else {
  15241. p = (unsigned long *) soap_id_forward(soap, soap->href, p, 0, t, t, sizeof(unsigned long), 0, NULL, NULL);
  15242. if (soap->body && soap_element_end_in(soap, tag))
  15243. return NULL;
  15244. }
  15245. return p;
  15246. }
  15247. /******************************************************************************/
  15248. SOAP_FMAC1
  15249. const char *
  15250. SOAP_FMAC2
  15251. soap_ULONG642s(struct soap *soap, ULONG64 n) {
  15252. (SOAP_SNPRINTF(soap->tmpbuf, sizeof(soap->tmpbuf), 20), SOAP_ULONG_FORMAT, n);
  15253. return soap->tmpbuf;
  15254. }
  15255. /******************************************************************************/
  15256. SOAP_FMAC1
  15257. int
  15258. SOAP_FMAC2
  15259. soap_outULONG64(struct soap *soap, const char *tag, int id, const ULONG64 *p, const char *type, int n) {
  15260. if (soap_element_begin_out(soap, tag, soap_embedded_id(soap, id, p, n), type)
  15261. || soap_string_out(soap, soap_ULONG642s(soap, *p), 0))
  15262. return soap->error;
  15263. return soap_element_end_out(soap, tag);
  15264. }
  15265. /******************************************************************************/
  15266. SOAP_FMAC1
  15267. int
  15268. SOAP_FMAC2
  15269. soap_s2ULONG64(struct soap *soap, const char *s, ULONG64 *p) {
  15270. if (s) {
  15271. char *r;
  15272. if (!*s)
  15273. return soap->error = SOAP_EMPTY;
  15274. #ifndef WITH_NOIO
  15275. #ifndef WITH_LEAN
  15276. soap_reset_errno;
  15277. #endif
  15278. #endif
  15279. *p = soap_strtoull(s, &r, 10);
  15280. if (s == r || *r
  15281. #ifndef WITH_NOIO
  15282. #ifndef WITH_LEAN
  15283. || soap_errno == SOAP_ERANGE
  15284. #endif
  15285. #endif
  15286. )
  15287. soap->error = SOAP_TYPE;
  15288. if (*p > 0 && strchr(s, '-'))
  15289. return soap->error = SOAP_TYPE;
  15290. }
  15291. return soap->error;
  15292. }
  15293. /******************************************************************************/
  15294. SOAP_FMAC1
  15295. ULONG64 *
  15296. SOAP_FMAC2
  15297. soap_inULONG64(struct soap *soap, const char *tag, ULONG64 *p, const char *type, int t) {
  15298. if (soap_element_begin_in(soap, tag, 0, NULL))
  15299. return NULL;
  15300. #ifndef WITH_LEAN
  15301. if (*soap->type
  15302. && soap_match_tag(soap, soap->type, type)
  15303. && soap_match_tag(soap, soap->type, ":positiveInteger")
  15304. && soap_match_tag(soap, soap->type, ":nonNegativeInteger")
  15305. && soap_match_tag(soap, soap->type, ":unsignedLong")
  15306. && soap_match_tag(soap, soap->type, ":unsignedInt")
  15307. && soap_match_tag(soap, soap->type, ":unsignedShort")
  15308. && soap_match_tag(soap, soap->type, ":unsignedByte")) {
  15309. soap->error = SOAP_TYPE;
  15310. soap_revert(soap);
  15311. return NULL;
  15312. }
  15313. #else
  15314. (void)type;
  15315. #endif
  15316. p = (ULONG64 *) soap_id_enter(soap, soap->id, p, t, sizeof(ULONG64), NULL, NULL, NULL, NULL);
  15317. if (!p)
  15318. return NULL;
  15319. if (*soap->href != '#') {
  15320. int err = soap_s2ULONG64(soap, soap_value(soap), p);
  15321. if ((soap->body && soap_element_end_in(soap, tag)) || err)
  15322. return NULL;
  15323. } else {
  15324. p = (ULONG64 *) soap_id_forward(soap, soap->href, p, 0, t, t, sizeof(ULONG64), 0, NULL, NULL);
  15325. if (soap->body && soap_element_end_in(soap, tag))
  15326. return NULL;
  15327. }
  15328. return p;
  15329. }
  15330. /******************************************************************************/
  15331. SOAP_FMAC1
  15332. int
  15333. SOAP_FMAC2
  15334. soap_s2char(struct soap *soap, const char *s, char **t, int flag, long minlen, long maxlen, const char *pattern) {
  15335. if (s) {
  15336. const char *r = soap_string(soap, s, flag, minlen, maxlen, pattern);
  15337. if (r && (*t = soap_strdup(soap, r)) == NULL)
  15338. return soap->error = SOAP_EOM;
  15339. }
  15340. return soap->error;
  15341. }
  15342. /******************************************************************************/
  15343. #ifndef WITH_COMPAT
  15344. #ifdef __cplusplus
  15345. SOAP_FMAC1
  15346. int
  15347. SOAP_FMAC2
  15348. soap_s2stdchar(struct soap *soap, const char *s, std::string *t, int flag, long minlen, long maxlen,
  15349. const char *pattern) {
  15350. if (s) {
  15351. const char *r = soap_string(soap, s, flag, minlen, maxlen, pattern);
  15352. if (r)
  15353. t->assign(r);
  15354. }
  15355. return soap->error;
  15356. }
  15357. #endif
  15358. #endif
  15359. /******************************************************************************/
  15360. static const char *
  15361. soap_string(struct soap *soap, const char *s, int flag, long minlen, long maxlen, const char *pattern) {
  15362. if (s) {
  15363. if (maxlen < 0 && soap->maxlength > 0)
  15364. maxlen = soap->maxlength;
  15365. if (minlen > 0 || maxlen >= 0) {
  15366. size_t l;
  15367. if ((soap->mode & SOAP_C_UTFSTRING))
  15368. l = soap_utf8len(s);
  15369. else
  15370. l = strlen(s);
  15371. if ((maxlen >= 0 && l > (size_t) maxlen) || (minlen > 0 && l < (size_t) minlen)) {
  15372. soap->error = SOAP_LENGTH;
  15373. return NULL;
  15374. }
  15375. }
  15376. if (flag >= 4)
  15377. s = soap_collapse(soap, (char *) s, flag, 0);
  15378. #ifndef WITH_LEANER
  15379. if (pattern && soap->fsvalidate) {
  15380. soap->error = soap->fsvalidate(soap, pattern, s);
  15381. if (soap->error)
  15382. return NULL;
  15383. }
  15384. #else
  15385. (void)pattern;
  15386. #endif
  15387. }
  15388. return s;
  15389. }
  15390. /******************************************************************************/
  15391. static char *
  15392. soap_collapse(struct soap *soap, char *s, int flag, int insitu) {
  15393. /* flag 4=normalizedString (replace), 5=token (collapse) */
  15394. char *t;
  15395. size_t n;
  15396. if (!s)
  15397. return NULL;
  15398. if (flag == 4) {
  15399. for (t = s; *t && (!soap_coblank((soap_wchar) *t) || *t == 32); t++)
  15400. continue;
  15401. if (*t) {
  15402. /* replace white space and control chars by blanks */
  15403. if (!insitu)
  15404. s = soap_strdup(soap, s);
  15405. for (t = s; *t; t++)
  15406. if (soap_coblank((soap_wchar) *t))
  15407. *t = ' ';
  15408. }
  15409. return s;
  15410. }
  15411. /* collapse white space */
  15412. for (t = s; *t && soap_coblank((soap_wchar) *t); t++)
  15413. continue;
  15414. n = strlen(t);
  15415. if (insitu && s < t)
  15416. (void) soap_memmove(s, n + 1, t, n + 1);
  15417. else
  15418. s = t;
  15419. if (n > 0) {
  15420. if (!soap_coblank((soap_wchar) s[n - 1])) {
  15421. for (t = s; (*t && !soap_coblank((soap_wchar) *t)) ||
  15422. (*t == 32 && (!t[1] || !soap_coblank((soap_wchar) t[1]))); t++)
  15423. continue;
  15424. if (!*t)
  15425. return s;
  15426. }
  15427. if (!insitu)
  15428. s = soap_strdup(soap, s);
  15429. for (t = s; *t; t++) {
  15430. if (soap_coblank((soap_wchar) *t)) {
  15431. char *r;
  15432. *t = ' ';
  15433. for (r = t + 1; *r && soap_coblank((soap_wchar) *r); r++)
  15434. continue;
  15435. if (r > t + 1)
  15436. (void) soap_memmove(t + 1, n - (t - s), r, n - (r - s) + 1);
  15437. }
  15438. }
  15439. t--;
  15440. if (t >= s && *t == 32)
  15441. *t = '\0';
  15442. }
  15443. return s;
  15444. }
  15445. /******************************************************************************/
  15446. SOAP_FMAC1
  15447. int
  15448. SOAP_FMAC2
  15449. soap_s2QName(struct soap *soap, const char *s, char **t, long minlen, long maxlen, const char *pattern) {
  15450. *t = NULL;
  15451. if (s) {
  15452. const char *r = soap_QName(soap, s, minlen, maxlen, pattern);
  15453. if (r && (*t = soap_strdup(soap, r)) == NULL)
  15454. return soap->error = SOAP_EOM;
  15455. }
  15456. return soap->error;
  15457. }
  15458. /******************************************************************************/
  15459. #ifndef WITH_COMPAT
  15460. #ifdef __cplusplus
  15461. SOAP_FMAC1
  15462. int
  15463. SOAP_FMAC2
  15464. soap_s2stdQName(struct soap *soap, const char *s, std::string *t, long minlen, long maxlen, const char *pattern) {
  15465. t->clear();
  15466. if (s) {
  15467. const char *r = soap_QName(soap, s, minlen, maxlen, pattern);
  15468. if (r)
  15469. t->assign(r);
  15470. }
  15471. return soap->error;
  15472. }
  15473. #endif
  15474. #endif
  15475. /******************************************************************************/
  15476. static const char *
  15477. soap_QName(struct soap *soap, const char *s, long minlen, long maxlen, const char *pattern) {
  15478. if (s) {
  15479. char *b;
  15480. if (maxlen < 0 && soap->maxlength > 0)
  15481. maxlen = soap->maxlength;
  15482. if (minlen > 0 || maxlen >= 0) {
  15483. size_t l;
  15484. if ((soap->mode & SOAP_C_UTFSTRING))
  15485. l = soap_utf8len(s);
  15486. else
  15487. l = strlen(s);
  15488. if ((maxlen >= 0 && l > (size_t) maxlen) || (minlen > 0 && l < (size_t) minlen)) {
  15489. soap->error = SOAP_LENGTH;
  15490. return NULL;
  15491. }
  15492. }
  15493. #ifdef WITH_FAST
  15494. soap->labidx = 0;
  15495. #else
  15496. if (soap_alloc_block(soap) == NULL)
  15497. return NULL;
  15498. #endif
  15499. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Normalized namespace(s) of QNames '%s'", s));
  15500. /* convert (by prefix normalize prefix) all QNames in s */
  15501. for (;;) {
  15502. size_t n;
  15503. struct soap_nlist *np;
  15504. const char *p = NULL;
  15505. short flag = 0;
  15506. const char *r = NULL;
  15507. size_t m = 0;
  15508. #ifndef WITH_FAST
  15509. size_t k = 0;
  15510. #endif
  15511. /* skip blanks */
  15512. while (*s && soap_coblank((soap_wchar) *s))
  15513. s++;
  15514. if (!*s)
  15515. break;
  15516. /* find next QName */
  15517. n = 1;
  15518. while (s[n] && !soap_coblank((soap_wchar) s[n]))
  15519. n++;
  15520. np = soap->nlist;
  15521. /* if there is no namespace stack, or prefix is "#" or "xml" then copy string */
  15522. if (!np || *s == '#' || !strncmp(s, "xml:", 4)) {
  15523. r = s;
  15524. m = n;
  15525. } else /* we normalize the QName by replacing its prefix */
  15526. {
  15527. const char *q;
  15528. for (p = s; *p && p < s + n; p++)
  15529. if (*p == ':')
  15530. break;
  15531. if (*p == ':') {
  15532. size_t k = p - s;
  15533. while (np && (strncmp(np->id, s, k) || np->id[k]))
  15534. np = np->next;
  15535. p++;
  15536. } else {
  15537. while (np && *np->id)
  15538. np = np->next;
  15539. p = s;
  15540. }
  15541. /* replace prefix */
  15542. if (np) {
  15543. if (np->index >= 0 && soap->local_namespaces &&
  15544. (q = soap->local_namespaces[np->index].id) != NULL) {
  15545. size_t k = strlen(q);
  15546. if (q[k - 1] != '_') {
  15547. r = q;
  15548. m = k;
  15549. } else {
  15550. flag = 1;
  15551. r = soap->local_namespaces[np->index].ns;
  15552. m = strlen(r);
  15553. }
  15554. } else if (np->ns) {
  15555. flag = 1;
  15556. r = np->ns;
  15557. m = strlen(r);
  15558. } else {
  15559. DBGLOG(TEST,
  15560. SOAP_MESSAGE(fdebug, "\nNamespace prefix of '%s' not defined (index=%d, URI='%s')\n", s,
  15561. np->index, np->ns ? np->ns : SOAP_STR_EOS));
  15562. soap->error = SOAP_NAMESPACE;
  15563. return NULL;
  15564. }
  15565. } else if (s[n]) /* no namespace, part of string */
  15566. {
  15567. r = s;
  15568. m = n;
  15569. } else /* no namespace: assume "" namespace */
  15570. {
  15571. flag = 1;
  15572. }
  15573. }
  15574. #ifdef WITH_FAST
  15575. if ((flag && soap_append_lab(soap, "\"", 1))
  15576. || (m && soap_append_lab(soap, r, m))
  15577. || (flag && soap_append_lab(soap, "\"", 1))
  15578. || (p && (soap_append_lab(soap, ":", 1) || soap_append_lab(soap, p, n - (p - s)))))
  15579. return NULL;
  15580. #else
  15581. k = 2*flag + m + (p ? n - (p-s) + 1 : 0) + (s[n] != '\0');
  15582. b = (char*)soap_push_block(soap, NULL, k);
  15583. if (!b)
  15584. return NULL;
  15585. if (flag)
  15586. *b++ = '"';
  15587. if (m)
  15588. {
  15589. if (soap_memcpy((void*)b, k, (const void*)r, m))
  15590. {
  15591. soap->error = SOAP_EOM;
  15592. return NULL;
  15593. }
  15594. b += m;
  15595. }
  15596. if (flag)
  15597. *b++ = '"';
  15598. if (p)
  15599. {
  15600. *b++ = ':';
  15601. if (soap_memcpy((void*)b, k - m - flag - 1, (const void*)p, n - (p-s)))
  15602. {
  15603. soap->error = SOAP_EOM;
  15604. return NULL;
  15605. }
  15606. b += n - (p-s);
  15607. }
  15608. #endif
  15609. /* advance to next and add spacing */
  15610. s += n;
  15611. while (*s && soap_coblank(*s))
  15612. s++;
  15613. if (*s) {
  15614. #ifdef WITH_FAST
  15615. if (soap_append_lab(soap, " ", 1))
  15616. return NULL;
  15617. #else
  15618. *b = ' ';
  15619. #endif
  15620. }
  15621. }
  15622. #ifdef WITH_FAST
  15623. if (soap_append_lab(soap, SOAP_STR_EOS, 1))
  15624. return NULL;
  15625. b = soap->labbuf;
  15626. #else
  15627. b = (char*)soap_push_block(soap, NULL, 1);
  15628. if (!b)
  15629. return NULL;
  15630. *b = '\0';
  15631. b = (char*)soap_save_block(soap, NULL, NULL, 0);
  15632. #endif
  15633. #ifndef WITH_LEANER
  15634. if (pattern && soap->fsvalidate) {
  15635. soap->error = soap->fsvalidate(soap, pattern, b);
  15636. if (soap->error)
  15637. return NULL;
  15638. }
  15639. #else
  15640. (void)pattern;
  15641. #endif
  15642. return b;
  15643. }
  15644. return NULL;
  15645. }
  15646. /******************************************************************************/
  15647. SOAP_FMAC1
  15648. const char *
  15649. SOAP_FMAC2
  15650. soap_QName2s(struct soap *soap, const char *s) {
  15651. const char *t = NULL;
  15652. if (s) {
  15653. #ifdef WITH_FAST
  15654. soap_store_lab(soap, SOAP_STR_EOS, 1);
  15655. soap->labidx = 0;
  15656. #else
  15657. char *b = NULL;
  15658. if (soap_alloc_block(soap) == NULL)
  15659. return NULL;
  15660. #endif
  15661. for (;;) {
  15662. size_t n;
  15663. const char *q = NULL;
  15664. const char *r = NULL;
  15665. size_t m = 0;
  15666. #ifndef WITH_FAST
  15667. size_t k = 0;
  15668. #endif
  15669. /* skip blanks */
  15670. while (*s && soap_coblank((soap_wchar) *s))
  15671. s++;
  15672. if (!*s) {
  15673. #ifdef WITH_FAST
  15674. soap->labbuf[soap->labidx > 0 ? soap->labidx - 1 : 0] = '\0';
  15675. #else
  15676. if (!b)
  15677. return soap_strdup(soap, SOAP_STR_EOS);
  15678. --b;
  15679. *b = '\0';
  15680. #endif
  15681. break;
  15682. }
  15683. /* find next QName */
  15684. n = 0;
  15685. while (s[n] && !soap_coblank((soap_wchar) s[n])) {
  15686. if (s[n] == ':')
  15687. r = s;
  15688. n++;
  15689. }
  15690. if (*s != '"') /* non-quoted: pass string as is */
  15691. {
  15692. #ifndef WITH_LEAN
  15693. if (r && (soap->mode & SOAP_XML_CANONICAL) && !(soap->mode & SOAP_XML_CANONICAL_NA))
  15694. soap_utilize_ns(soap, s, 1);
  15695. #endif
  15696. r = s;
  15697. m = n + 1;
  15698. } else /* prefix quoted URI-based string */
  15699. {
  15700. q = strchr(s + 1, '"');
  15701. if (q) {
  15702. struct Namespace *p = soap->local_namespaces;
  15703. if (p) {
  15704. for (; p->id; p++) {
  15705. if (p->ns)
  15706. if (!soap_tag_cmp(s + 1, p->ns))
  15707. break;
  15708. if (p->in)
  15709. if (!soap_tag_cmp(s + 1, p->in))
  15710. break;
  15711. }
  15712. }
  15713. q++;
  15714. /* URL is in the namespace table? */
  15715. if (p && p->id) {
  15716. r = p->id;
  15717. m = strlen(r);
  15718. } else /* not in namespace table: create xmlns binding */
  15719. {
  15720. char *x = soap_strdup(soap, s + 1);
  15721. if (!x)
  15722. return NULL;
  15723. x[q - s - 2] = '\0';
  15724. (SOAP_SNPRINTF(soap->tmpbuf, sizeof(soap->tmpbuf), 27), "xmlns:_%d", soap->idnum++);
  15725. soap_set_attr(soap, soap->tmpbuf, x, 1);
  15726. r = soap->tmpbuf + 6;
  15727. m = strlen(r);
  15728. }
  15729. }
  15730. }
  15731. /* copy normalized QName into buffer, including the ending blank or NUL */
  15732. #ifdef WITH_FAST
  15733. if ((m && soap_append_lab(soap, r, m))
  15734. || (q && soap_append_lab(soap, q, n - (q - s) + 1)))
  15735. return NULL;
  15736. #else
  15737. k = m + (q ? n - (q - s) + 1 : 0);
  15738. b = (char*)soap_push_block(soap, NULL, k);
  15739. if (!b)
  15740. {
  15741. soap->error = SOAP_EOM;
  15742. return NULL;
  15743. }
  15744. if (soap_memcpy((void*)b, k, (const void*)r, m))
  15745. {
  15746. soap->error = SOAP_EOM;
  15747. return NULL;
  15748. }
  15749. b += m;
  15750. if (q)
  15751. {
  15752. if (soap_memcpy((void*)b, k - m, (const void*)q, n - (q - s) + 1))
  15753. {
  15754. soap->error = SOAP_EOM;
  15755. return NULL;
  15756. }
  15757. b += n - (q - s) + 1;
  15758. }
  15759. #endif
  15760. /* advance to next */
  15761. s += n;
  15762. }
  15763. #ifdef WITH_FAST
  15764. t = soap_strdup(soap, soap->labbuf);
  15765. if (!t)
  15766. soap->error = SOAP_EOM;
  15767. #else
  15768. t = (char*)soap_save_block(soap, NULL, NULL, 0);
  15769. #endif
  15770. }
  15771. return t;
  15772. }
  15773. /******************************************************************************/
  15774. #ifndef WITH_LEAN
  15775. SOAP_FMAC1
  15776. int
  15777. SOAP_FMAC2
  15778. soap_s2wchar(struct soap *soap, const char *s, wchar_t **t, int flag, long minlen, long maxlen, const char *pattern) {
  15779. if (s) {
  15780. const wchar_t *r = soap_wstring(soap, s, flag, minlen, maxlen, pattern);
  15781. if (r && (*t = soap_wstrdup(soap, r)) == NULL)
  15782. return soap->error = SOAP_EOM;
  15783. }
  15784. return soap->error;
  15785. }
  15786. #endif
  15787. /******************************************************************************/
  15788. #ifndef WITH_COMPAT
  15789. #ifdef __cplusplus
  15790. #ifndef WITH_LEAN
  15791. SOAP_FMAC1
  15792. int
  15793. SOAP_FMAC2
  15794. soap_s2stdwchar(struct soap *soap, const char *s, std::wstring *t, int flag, long minlen, long maxlen,
  15795. const char *pattern) {
  15796. if (s) {
  15797. const wchar_t *r = soap_wstring(soap, s, flag, minlen, maxlen, pattern);
  15798. if (r)
  15799. t->assign(r);
  15800. }
  15801. return soap->error;
  15802. }
  15803. #endif
  15804. #endif
  15805. #endif
  15806. /******************************************************************************/
  15807. #ifndef WITH_LEAN
  15808. static const wchar_t *
  15809. soap_wstring(struct soap *soap, const char *s, int flag, long minlen, long maxlen, const char *pattern) {
  15810. if (s) {
  15811. size_t l;
  15812. soap_wchar c;
  15813. wchar_t *t;
  15814. if (maxlen < 0 && soap->maxlength > 0)
  15815. maxlen = soap->maxlength;
  15816. soap->labidx = 0;
  15817. if ((soap->mode & SOAP_ENC_LATIN)) {
  15818. wchar_t *r;
  15819. if (soap_append_lab(soap, NULL, sizeof(wchar_t) * (strlen(s) + 1)))
  15820. return NULL;
  15821. r = (wchar_t *) soap->labbuf;
  15822. while (*s)
  15823. *r++ = (wchar_t) *s++;
  15824. } else {
  15825. /* Convert UTF8 to wchar_t */
  15826. while (*s) {
  15827. c = (unsigned char) *s++;
  15828. if (c >= 0x80) {
  15829. #ifdef WITH_REPLACE_ILLEGAL_UTF8
  15830. soap_wchar c1, c2, c3;
  15831. c1 = (unsigned char)*s;
  15832. if (c <= 0xC1 || (c1 & 0xC0) != 0x80)
  15833. {
  15834. c = SOAP_UNKNOWN_UNICODE_CHAR;
  15835. }
  15836. else
  15837. {
  15838. ++s;
  15839. c1 &= 0x3F;
  15840. if (c < 0xE0)
  15841. {
  15842. c = (((c & 0x1F) << 6) | c1);
  15843. }
  15844. else
  15845. {
  15846. c2 = (unsigned char)*s;
  15847. if ((c == 0xE0 && c1 < 0x20) || (c2 & 0xC0) != 0x80)
  15848. {
  15849. c = SOAP_UNKNOWN_UNICODE_CHAR;
  15850. }
  15851. else
  15852. {
  15853. ++s;
  15854. c2 &= 0x3F;
  15855. if (c < 0xF0)
  15856. {
  15857. c = (((c & 0x0F) << 12) | (c1 << 6) | c2);
  15858. }
  15859. else
  15860. {
  15861. c3 = (unsigned char)*s;
  15862. if ((c == 0xF0 && c1 < 0x10) || (c == 0xF4 && c1 >= 0x10) || c >= 0xF5 || (c3 & 0xC0) != 0x80)
  15863. {
  15864. c = SOAP_UNKNOWN_UNICODE_CHAR;
  15865. }
  15866. else
  15867. {
  15868. ++s;
  15869. c = (((c & 0x07) << 18) | (c1 << 12) | (c2 << 6) | (c3 & 0x3F));
  15870. }
  15871. }
  15872. }
  15873. }
  15874. }
  15875. #else
  15876. soap_wchar c1, c2, c3, c4;
  15877. c1 = (unsigned char) *s;
  15878. if (c1) {
  15879. s++;
  15880. c1 &= 0x3F;
  15881. if (c < 0xE0) {
  15882. c = (wchar_t) (((soap_wchar) (c & 0x1F) << 6) | c1);
  15883. } else {
  15884. c2 = (unsigned char) *s;
  15885. if (c2) {
  15886. s++;
  15887. c2 &= 0x3F;
  15888. if (c < 0xF0) {
  15889. c = (wchar_t) (((soap_wchar) (c & 0x0F) << 12) | (c1 << 6) | c2);
  15890. } else {
  15891. c3 = (unsigned char) *s;
  15892. if (c3) {
  15893. s++;
  15894. c3 &= 0x3F;
  15895. if (c < 0xF8) {
  15896. c = (wchar_t) (((soap_wchar) (c & 0x07) << 18) | (c1 << 12) | (c2 << 6) |
  15897. c3);
  15898. } else {
  15899. c4 = (unsigned char) *s;
  15900. if (c4) {
  15901. s++;
  15902. c4 &= 0x3F;
  15903. if (c < 0xFC) {
  15904. c = (wchar_t) (((soap_wchar) (c & 0x03) << 24) | (c1 << 18) |
  15905. (c2 << 12) | (c3 << 6) | c4);
  15906. } else {
  15907. c = (wchar_t) (((soap_wchar) (c & 0x01) << 30) | (c1 << 24) |
  15908. (c2 << 18) | (c3 << 12) | (c4 << 6) |
  15909. (unsigned char) (*s & 0x3F));
  15910. if (*s)
  15911. s++;
  15912. }
  15913. }
  15914. }
  15915. }
  15916. }
  15917. }
  15918. }
  15919. }
  15920. #endif
  15921. }
  15922. /* use UTF16 encoding when wchar_t is too small to hold UCS */
  15923. if (sizeof(wchar_t) < 4 && c > 0xFFFF) {
  15924. wchar_t c1, c2;
  15925. c1 = 0xD800 - (0x10000 >> 10) + (c >> 10);
  15926. c2 = 0xDC00 + (c & 0x3FF);
  15927. if (soap_append_lab(soap, (const char *) &c1, sizeof(wchar_t)) ||
  15928. soap_append_lab(soap, (const char *) &c2, sizeof(wchar_t)))
  15929. return NULL;
  15930. } else if (soap_append_lab(soap, (const char *) &c, sizeof(wchar_t))) {
  15931. return NULL;
  15932. }
  15933. }
  15934. }
  15935. l = soap->labidx / sizeof(wchar_t);
  15936. c = L'\0';
  15937. if (soap_append_lab(soap, (const char *) &c, sizeof(wchar_t)))
  15938. return NULL;
  15939. if ((maxlen >= 0 && l > (size_t) maxlen) || (minlen > 0 && l < (size_t) minlen)) {
  15940. soap->error = SOAP_LENGTH;
  15941. return NULL;
  15942. }
  15943. t = (wchar_t *) soap->labbuf;
  15944. #ifndef WITH_LEAN
  15945. if (flag >= 4 && t)
  15946. t = soap_wcollapse(soap, t, flag, 1);
  15947. #endif
  15948. #ifndef WITH_LEANER
  15949. if (pattern && soap->fwvalidate) {
  15950. soap->error = soap->fwvalidate(soap, pattern, t);
  15951. if (soap->error)
  15952. return NULL;
  15953. }
  15954. #endif
  15955. return t;
  15956. }
  15957. return NULL;
  15958. }
  15959. #endif
  15960. /******************************************************************************/
  15961. #ifndef WITH_LEAN
  15962. static wchar_t *
  15963. soap_wcollapse(struct soap *soap, wchar_t *s, int flag, int insitu) {
  15964. /* flag 4=normalizedString (replace), 5=token (collapse) */
  15965. wchar_t *t;
  15966. size_t n;
  15967. if (!s)
  15968. return NULL;
  15969. if (flag == 4) {
  15970. for (t = s; *t && (!soap_coblank((soap_wchar) *t) || *t == 32); t++)
  15971. continue;
  15972. if (*t) {
  15973. /* replace blanks and control char by space */
  15974. if (!insitu)
  15975. s = soap_wstrdup(soap, s);
  15976. if (s)
  15977. for (t = s; *t; t++)
  15978. if (soap_coblank((soap_wchar) *t))
  15979. *t = L' ';
  15980. }
  15981. return s;
  15982. }
  15983. /* collapse white space */
  15984. for (t = s; *t && soap_coblank((soap_wchar) *t); t++)
  15985. continue;
  15986. n = 0;
  15987. while (t[n])
  15988. n++;
  15989. if (insitu && s < t)
  15990. (void) soap_memmove(s, n + 1, t, n + 1);
  15991. else
  15992. s = t;
  15993. if (n > 0) {
  15994. if (!soap_coblank((soap_wchar) s[n - 1])) {
  15995. for (t = s; (*t && !soap_coblank((soap_wchar) *t)) ||
  15996. (*t == 32 && (!t[1] || !soap_coblank((soap_wchar) t[1]))); t++)
  15997. continue;
  15998. if (!*t)
  15999. return s;
  16000. }
  16001. if (!insitu)
  16002. s = soap_wstrdup(soap, s);
  16003. if (s) {
  16004. for (t = s; *t; t++) {
  16005. if (soap_coblank((soap_wchar) *t)) {
  16006. wchar_t *r;
  16007. *t = L' ';
  16008. for (r = t + 1; *r && soap_coblank((soap_wchar) *r); r++)
  16009. continue;
  16010. if (r > t + 1)
  16011. (void) soap_memmove(t + 1, sizeof(wchar_t) * (n - (t - s)), r,
  16012. sizeof(wchar_t) * (n - (r - s) + 1));
  16013. }
  16014. }
  16015. t--;
  16016. if (t >= s && *t == 32)
  16017. *t = L'\0';
  16018. }
  16019. }
  16020. return s;
  16021. }
  16022. #endif
  16023. /******************************************************************************/
  16024. #ifndef WITH_LEAN
  16025. SOAP_FMAC1
  16026. const char *
  16027. SOAP_FMAC2
  16028. soap_wchar2s(struct soap *soap, const wchar_t *s) {
  16029. soap_wchar c;
  16030. char *r, *t;
  16031. const wchar_t *q = s;
  16032. size_t n = 0;
  16033. if (!s)
  16034. return NULL;
  16035. while ((c = *q++)) {
  16036. if (c > 0 && c < 0x80)
  16037. n++;
  16038. else
  16039. #ifdef WITH_REPLACE_ILLEGAL_UTF8
  16040. n += 4;
  16041. #else
  16042. n += 6;
  16043. #endif
  16044. }
  16045. r = t = (char *) soap_malloc(soap, n + 1);
  16046. if (r) {
  16047. /* Convert wchar to UTF8 (chars above U+10FFFF are silently converted, but should not be used) */
  16048. while ((c = *s++)) {
  16049. if (c > 0 && c < 0x80) {
  16050. *t++ = (char) c;
  16051. } else {
  16052. /* check for UTF16 encoding when wchar_t is too small to hold UCS */
  16053. if (sizeof(wchar_t) < 4 && (c & 0xFC00) == 0xD800) {
  16054. soap_wchar d = *s;
  16055. if ((d & 0xFC00) == 0xDC00) {
  16056. c = ((c - 0xD800) << 10) + (d - 0xDC00) + 0x10000;
  16057. s++;
  16058. }
  16059. #ifdef WITH_REPLACE_ILLEGAL_UTF8
  16060. else
  16061. {
  16062. c = SOAP_UNKNOWN_UNICODE_CHAR; /* Malformed UTF-16 */
  16063. }
  16064. #endif
  16065. }
  16066. if (c < 0x0800) {
  16067. *t++ = (char) (0xC0 | ((c >> 6) & 0x1F));
  16068. } else {
  16069. #ifdef WITH_REPLACE_ILLEGAL_UTF8
  16070. if (!((c >= 0x80 && c <= 0xD7FF) || (c >= 0xE000 && c <= 0xFFFD) || (c >= 0x10000 && c <= 0x10FFFF)))
  16071. c = SOAP_UNKNOWN_UNICODE_CHAR;
  16072. #endif
  16073. if (c < 0x010000) {
  16074. *t++ = (char) (0xE0 | ((c >> 12) & 0x0F));
  16075. } else {
  16076. if (c < 0x200000) {
  16077. *t++ = (char) (0xF0 | ((c >> 18) & 0x07));
  16078. } else {
  16079. if (c < 0x04000000) {
  16080. *t++ = (char) (0xF8 | ((c >> 24) & 0x03));
  16081. } else {
  16082. *t++ = (char) (0xFC | ((c >> 30) & 0x01));
  16083. *t++ = (char) (0x80 | ((c >> 24) & 0x3F));
  16084. }
  16085. *t++ = (char) (0x80 | ((c >> 18) & 0x3F));
  16086. }
  16087. *t++ = (char) (0x80 | ((c >> 12) & 0x3F));
  16088. }
  16089. *t++ = (char) (0x80 | ((c >> 6) & 0x3F));
  16090. }
  16091. *t++ = (char) (0x80 | (c & 0x3F));
  16092. }
  16093. }
  16094. *t = '\0';
  16095. }
  16096. return r;
  16097. }
  16098. #endif
  16099. /******************************************************************************/
  16100. SOAP_FMAC1
  16101. int
  16102. SOAP_FMAC2
  16103. soap_outstring(struct soap *soap, const char *tag, int id, char *const *p, const char *type, int n) {
  16104. id = soap_element_id(soap, tag, id, *p, NULL, 0, type, n, NULL);
  16105. if (id < 0)
  16106. return soap->error;
  16107. if (!**p && (soap->mode & SOAP_C_NILSTRING))
  16108. return soap_element_null(soap, tag, id, type);
  16109. if (soap_element_begin_out(soap, tag, id, type)
  16110. || soap_string_out(soap, *p, 0)
  16111. || soap_element_end_out(soap, tag))
  16112. return soap->error;
  16113. return SOAP_OK;
  16114. }
  16115. /******************************************************************************/
  16116. SOAP_FMAC1
  16117. char **
  16118. SOAP_FMAC2
  16119. soap_instring(struct soap *soap, const char *tag, char **p, const char *type, int t, int flag, long minlen, long maxlen,
  16120. const char *pattern) {
  16121. (void) type;
  16122. if (soap_element_begin_in(soap, tag, 1, NULL)) {
  16123. if (!tag || *tag != '-' || soap->error != SOAP_NO_TAG)
  16124. return NULL;
  16125. soap->error = SOAP_OK;
  16126. }
  16127. if (!p) {
  16128. p = (char **) soap_malloc(soap, sizeof(char *));
  16129. if (!p)
  16130. return NULL;
  16131. }
  16132. if (soap->null) {
  16133. *p = NULL;
  16134. } else if (soap->body) {
  16135. *p = soap_string_in(soap, flag, minlen, maxlen, pattern);
  16136. if (!*p || !(char *) soap_id_enter(soap, soap->id, *p, t, sizeof(char *), NULL, NULL, NULL, NULL))
  16137. return NULL;
  16138. if (!**p && tag && *tag == '-') {
  16139. soap->error = SOAP_NO_TAG;
  16140. return NULL;
  16141. }
  16142. } else if (tag && *tag == '-') {
  16143. soap->error = SOAP_NO_TAG;
  16144. return NULL;
  16145. } else if (*soap->href != '#') {
  16146. if (minlen > 0) {
  16147. soap->error = SOAP_LENGTH;
  16148. return NULL;
  16149. }
  16150. *p = soap_strdup(soap, SOAP_STR_EOS);
  16151. if (!*p)
  16152. return NULL;
  16153. }
  16154. if (*soap->href == '#')
  16155. p = (char **) soap_id_lookup(soap, soap->href, (void **) p, t, sizeof(char **), 0, NULL);
  16156. if (soap->body && soap_element_end_in(soap, tag))
  16157. return NULL;
  16158. return p;
  16159. }
  16160. /******************************************************************************/
  16161. #ifndef WITH_LEANER
  16162. SOAP_FMAC1
  16163. int
  16164. SOAP_FMAC2
  16165. soap_outwstring(struct soap *soap, const char *tag, int id, wchar_t *const *p, const char *type, int n) {
  16166. id = soap_element_id(soap, tag, id, *p, NULL, 0, type, n, NULL);
  16167. if (id < 0)
  16168. return soap->error;
  16169. if (!**p && (soap->mode & SOAP_C_NILSTRING))
  16170. return soap_element_null(soap, tag, id, type);
  16171. if (soap_element_begin_out(soap, tag, id, type)
  16172. || soap_wstring_out(soap, *p, 0)
  16173. || soap_element_end_out(soap, tag))
  16174. return soap->error;
  16175. return SOAP_OK;
  16176. }
  16177. #endif
  16178. /******************************************************************************/
  16179. #ifndef WITH_LEANER
  16180. SOAP_FMAC1
  16181. wchar_t **
  16182. SOAP_FMAC2
  16183. soap_inwstring(struct soap *soap, const char *tag, wchar_t **p, const char *type, int t, int flag, long minlen,
  16184. long maxlen, const char *pattern) {
  16185. (void) type;
  16186. if (soap_element_begin_in(soap, tag, 1, NULL)) {
  16187. if (!tag || *tag != '-' || soap->error != SOAP_NO_TAG)
  16188. return NULL;
  16189. soap->error = SOAP_OK;
  16190. }
  16191. if (!p) {
  16192. p = (wchar_t **) soap_malloc(soap, sizeof(wchar_t *));
  16193. if (!p)
  16194. return NULL;
  16195. }
  16196. if (soap->null) {
  16197. *p = NULL;
  16198. } else if (soap->body) {
  16199. *p = soap_wstring_in(soap, flag, minlen, maxlen, pattern);
  16200. if (!*p || !(wchar_t *) soap_id_enter(soap, soap->id, *p, t, sizeof(wchar_t *), NULL, NULL, NULL, NULL))
  16201. return NULL;
  16202. if (!**p && tag && *tag == '-') {
  16203. soap->error = SOAP_NO_TAG;
  16204. return NULL;
  16205. }
  16206. } else if (tag && *tag == '-') {
  16207. soap->error = SOAP_NO_TAG;
  16208. return NULL;
  16209. } else if (*soap->href != '#') {
  16210. if (minlen > 0) {
  16211. soap->error = SOAP_LENGTH;
  16212. return NULL;
  16213. }
  16214. *p = soap_wstrdup(soap, L"");
  16215. }
  16216. if (*soap->href == '#')
  16217. p = (wchar_t **) soap_id_lookup(soap, soap->href, (void **) p, t, sizeof(wchar_t **), 0, NULL);
  16218. if (soap->body && soap_element_end_in(soap, tag))
  16219. return NULL;
  16220. return p;
  16221. }
  16222. #endif
  16223. /******************************************************************************/
  16224. #ifndef WITH_LEAN
  16225. #ifdef UNDER_CE
  16226. /* WinCE mktime (based on the mingw-runtime, public domain) */
  16227. #define __FILETIME_to_ll(f) ((long long)(f).dwHighDateTime << 32 | (long long)(f).dwLowDateTime)
  16228. static time_t
  16229. mktime(struct tm *pt)
  16230. {
  16231. SYSTEMTIME s, s1, s2;
  16232. FILETIME f, f1, f2;
  16233. long long diff;
  16234. GetSystemTime(&s1);
  16235. GetLocalTime(&s2);
  16236. SystemTimeToFileTime(&s1, &f1);
  16237. SystemTimeToFileTime(&s2, &f2);
  16238. diff = (__FILETIME_to_ll(f2) - __FILETIME_to_ll(f1)) / 10000000LL;
  16239. s.wYear = pt->tm_year + 1900;
  16240. s.wMonth = pt->tm_mon + 1;
  16241. s.wDayOfWeek = pt->tm_wday;
  16242. s.wDay = pt->tm_mday;
  16243. s.wHour = pt->tm_hour;
  16244. s.wMinute = pt->tm_min;
  16245. s.wSecond = pt->tm_sec;
  16246. s.wMilliseconds = 0;
  16247. SystemTimeToFileTime(&s, &f);
  16248. return (time_t)((__FILETIME_to_ll(f) - 116444736000000000LL) / 10000000LL) - (time_t)diff;
  16249. }
  16250. #endif
  16251. #endif
  16252. /******************************************************************************/
  16253. #ifndef WITH_LEAN
  16254. #ifdef UNDER_CE
  16255. /* WinCE gmtime_r (based on the mingw-runtime, public domain) */
  16256. #define HAVE_GMTIME_R
  16257. static struct tm*
  16258. gmtime_r(const time_t *t, struct tm *pt)
  16259. {
  16260. FILETIME f, f1, f2;
  16261. SYSTEMTIME s, s1 = {0};
  16262. long long time = (long long)(*t) * 10000000LL + 116444736000000000LL;
  16263. f.dwHighDateTime = (DWORD)((time >> 32) & 0x00000000FFFFFFFF);
  16264. f.dwLowDateTime = (DWORD)(time & 0x00000000FFFFFFFF);
  16265. FileTimeToSystemTime(&f, &s);
  16266. pt->tm_year = s.wYear - 1900;
  16267. pt->tm_mon = s.wMonth - 1;
  16268. pt->tm_wday = s.wDayOfWeek;
  16269. pt->tm_mday = s.wDay;
  16270. s1.wYear = s.wYear;
  16271. s1.wMonth = 1;
  16272. s1.wDayOfWeek = 1;
  16273. s1.wDay = 1;
  16274. SystemTimeToFileTime(&s1, &f1);
  16275. SystemTimeToFileTime(&s, &f2);
  16276. pt->tm_yday = (((__FILETIME_to_ll(f2) - __FILETIME_to_ll(f1)) / 10000000LL) / (60 * 60 * 24));
  16277. pt->tm_hour = s.wHour;
  16278. pt->tm_min = s.wMinute;
  16279. pt->tm_sec = s.wSecond;
  16280. pt->tm_isdst = 0;
  16281. return pt;
  16282. }
  16283. #endif
  16284. #endif
  16285. /******************************************************************************/
  16286. #ifndef WITH_LEAN
  16287. #ifdef UNDER_CE
  16288. /* WinCE very simple strftime for format "%Y-%m-%dT%H:%M:%SZ", note: %F and %T not supported by MS */
  16289. static size_t
  16290. strftime(char *buf, size_t len, const char *format, const struct tm *pT)
  16291. {
  16292. (void)len; (void)format;
  16293. #ifndef WITH_NOZONE
  16294. (SOAP_SNPRINTF(buf, len, 20), "%04d-%02d-%02dT%02d:%02d:%02dZ", pT->tm_year + 1900, pT->tm_mon + 1, pT->tm_mday, pT->tm_hour, pT->tm_min, pT->tm_sec);
  16295. #else
  16296. (SOAP_SNPRINTF(buf, len, 20), "%04d-%02d-%02dT%02d:%02d:%02d", pT->tm_year + 1900, pT->tm_mon + 1, pT->tm_mday, pT->tm_hour, pT->tm_min, pT->tm_sec);
  16297. #endif
  16298. return len;
  16299. }
  16300. #endif
  16301. #endif
  16302. /******************************************************************************/
  16303. #if !defined(WITH_LEAN) || defined(WITH_COOKIES)
  16304. SOAP_FMAC1
  16305. time_t
  16306. SOAP_FMAC2
  16307. soap_timegm(struct tm *T) {
  16308. #if defined(HAVE_TIMEGM)
  16309. return timegm(T);
  16310. #else
  16311. time_t t, g, z;
  16312. struct tm tm;
  16313. #ifndef HAVE_GMTIME_R
  16314. struct tm *tp;
  16315. #endif
  16316. t = mktime(T);
  16317. if (t == (time_t)-1)
  16318. return (time_t)-1;
  16319. #ifdef HAVE_GMTIME_R
  16320. if (gmtime_r(&t, &tm) == SOAP_FUNC_R_ERR)
  16321. return (time_t)-1;
  16322. #else
  16323. tp = gmtime(&t);
  16324. if (!tp)
  16325. return (time_t)-1;
  16326. tm = *tp;
  16327. #endif
  16328. tm.tm_isdst = 0;
  16329. g = mktime(&tm);
  16330. if (g == (time_t)-1)
  16331. return (time_t)-1;
  16332. z = g - t;
  16333. return t - z;
  16334. #endif
  16335. }
  16336. #endif
  16337. /******************************************************************************/
  16338. #ifndef WITH_LEAN
  16339. SOAP_FMAC1
  16340. const char *
  16341. SOAP_FMAC2
  16342. soap_dateTime2s(struct soap *soap, time_t n) {
  16343. struct tm T, *pT = &T;
  16344. size_t l = 0;
  16345. #if defined(HAVE_GMTIME_R) && !defined(WITH_NOZONE)
  16346. if (gmtime_r(&n, pT) != SOAP_FUNC_R_ERR)
  16347. l = strftime(soap->tmpbuf, sizeof(soap->tmpbuf), "%Y-%m-%dT%H:%M:%SZ", pT);
  16348. #elif defined(HAVE_GMTIME) && !defined(WITH_NOZONE)
  16349. pT = gmtime(&n);
  16350. if (pT)
  16351. l = strftime(soap->tmpbuf, sizeof(soap->tmpbuf), "%Y-%m-%dT%H:%M:%SZ", pT);
  16352. #elif (defined(HAVE_TM_GMTOFF) || defined(HAVE_STRUCT_TM_TM_GMTOFF) || defined(HAVE_STRUCT_TM___TM_GMTOFF)) && !defined(WITH_NOZONE)
  16353. #if defined(HAVE_LOCALTIME_R)
  16354. if (localtime_r(&n, pT) != SOAP_FUNC_R_ERR)
  16355. {
  16356. l = strftime(soap->tmpbuf, sizeof(soap->tmpbuf), "%Y-%m-%dT%H:%M:%S%z", pT);
  16357. if (l)
  16358. {
  16359. (void)soap_memmove(soap->tmpbuf + 23, sizeof(soap->tmpbuf) - 23, soap->tmpbuf + 22, 3); /* 2000-03-01T02:00:00+0300 */
  16360. soap->tmpbuf[22] = ':'; /* 2000-03-01T02:00:00+03:00 */
  16361. }
  16362. }
  16363. #else
  16364. pT = localtime(&n);
  16365. if (pT)
  16366. {
  16367. l = strftime(soap->tmpbuf, sizeof(soap->tmpbuf), "%Y-%m-%dT%H:%M:%S%z", pT);
  16368. if (l)
  16369. {
  16370. (void)soap_memmove(soap->tmpbuf + 23, sizeof(soap->tmpbuf) - 23, soap->tmpbuf + 22, 3); /* 2000-03-01T02:00:00+0300 */
  16371. soap->tmpbuf[22] = ':'; /* 2000-03-01T02:00:00+03:00 */
  16372. }
  16373. }
  16374. #endif
  16375. #elif defined(HAVE_GETTIMEOFDAY) && !defined(WITH_NOZONE)
  16376. #if defined(HAVE_LOCALTIME_R)
  16377. if (localtime_r(&n, pT) != SOAP_FUNC_R_ERR)
  16378. {
  16379. struct timeval tv;
  16380. struct timezone tz;
  16381. memset((void*)&tz, 0, sizeof(tz));
  16382. gettimeofday(&tv, &tz);
  16383. l = strftime(soap->tmpbuf, sizeof(soap->tmpbuf), "%Y-%m-%dT%H:%M:%S", pT);
  16384. if (l)
  16385. (SOAP_SNPRINTF(soap->tmpbuf + l, sizeof(soap->tmpbuf) - l, 7), "%+03d:%02d", -tz.tz_minuteswest/60+(pT->tm_isdst!=0), abs(tz.tz_minuteswest)%60);
  16386. }
  16387. #else
  16388. pT = localtime(&n);
  16389. if (pT)
  16390. {
  16391. struct timeval tv;
  16392. struct timezone tz;
  16393. memset((void*)&tz, 0, sizeof(tz));
  16394. gettimeofday(&tv, &tz);
  16395. l = strftime(soap->tmpbuf, sizeof(soap->tmpbuf), "%Y-%m-%dT%H:%M:%S", pT);
  16396. if (l)
  16397. (SOAP_SNPRINTF(soap->tmpbuf + l, sizeof(soap->tmpbuf) - l, 7), "%+03d:%02d", -tz.tz_minuteswest/60+(pT->tm_isdst!=0), abs(tz.tz_minuteswest)%60);
  16398. }
  16399. #endif
  16400. #elif defined(HAVE_FTIME) && !defined(WITH_NOZONE)
  16401. #if defined(HAVE_LOCALTIME_R)
  16402. if (localtime_r(&n, pT) != SOAP_FUNC_R_ERR)
  16403. {
  16404. struct timeb t;
  16405. memset((void*)&t, 0, sizeof(t));
  16406. #ifdef __BORLANDC__
  16407. ::ftime(&t);
  16408. #else
  16409. ftime(&t);
  16410. #endif
  16411. l = strftime(soap->tmpbuf, sizeof(soap->tmpbuf), "%Y-%m-%dT%H:%M:%S", pT);
  16412. if (l)
  16413. (SOAP_SNPRINTF(soap->tmpbuf + l, sizeof(soap->tmpbuf) - l, 7), "%+03d:%02d", -t.timezone/60+(pT->tm_isdst!=0), abs(t.timezone)%60);
  16414. }
  16415. #else
  16416. pT = localtime(&n);
  16417. if (pT)
  16418. {
  16419. struct timeb t;
  16420. memset((void*)&t, 0, sizeof(t));
  16421. #ifdef __BORLANDC__
  16422. ::ftime(&t);
  16423. #else
  16424. ftime(&t);
  16425. #endif
  16426. l = strftime(soap->tmpbuf, sizeof(soap->tmpbuf), "%Y-%m-%dT%H:%M:%S", pT);
  16427. if (l)
  16428. (SOAP_SNPRINTF(soap->tmpbuf + l, sizeof(soap->tmpbuf) - l, 7), "%+03d:%02d", -t.timezone/60+(pT->tm_isdst!=0), abs(t.timezone)%60);
  16429. }
  16430. #endif
  16431. #elif defined(HAVE_LOCALTIME_R)
  16432. if (localtime_r(&n, pT) != SOAP_FUNC_R_ERR)
  16433. l = strftime(soap->tmpbuf, sizeof(soap->tmpbuf), "%Y-%m-%dT%H:%M:%S", pT);
  16434. #else
  16435. pT = localtime(&n);
  16436. if (pT)
  16437. l = strftime(soap->tmpbuf, sizeof(soap->tmpbuf), "%Y-%m-%dT%H:%M:%S", pT);
  16438. #endif
  16439. if (!l)
  16440. soap_strcpy(soap->tmpbuf, sizeof(soap->tmpbuf), "1969-12-31T23:59:59Z");
  16441. return soap->tmpbuf;
  16442. }
  16443. #endif
  16444. /******************************************************************************/
  16445. #ifndef WITH_LEAN
  16446. SOAP_FMAC1
  16447. int
  16448. SOAP_FMAC2
  16449. soap_outdateTime(struct soap *soap, const char *tag, int id, const time_t *p, const char *type, int n) {
  16450. if (soap_element_begin_out(soap, tag, soap_embedded_id(soap, id, p, n), type)
  16451. || soap_string_out(soap, soap_dateTime2s(soap, *p), 0))
  16452. return soap->error;
  16453. return soap_element_end_out(soap, tag);
  16454. }
  16455. #endif
  16456. /******************************************************************************/
  16457. #ifndef WITH_LEAN
  16458. SOAP_FMAC1
  16459. int
  16460. SOAP_FMAC2
  16461. soap_s2dateTime(struct soap *soap, const char *s, time_t *p) {
  16462. *p = 0;
  16463. if (s) {
  16464. char *t;
  16465. unsigned long d;
  16466. struct tm T;
  16467. if (!*s)
  16468. return soap->error = SOAP_EMPTY;
  16469. memset((void *) &T, 0, sizeof(T));
  16470. d = soap_strtoul(s, &t, 10);
  16471. if (*t == '-') {
  16472. /* YYYY-MM-DD */
  16473. T.tm_year = (int) d;
  16474. T.tm_mon = (int) soap_strtoul(t + 1, &t, 10);
  16475. T.tm_mday = (int) soap_strtoul(t + 1, &t, 10);
  16476. } else if (!(soap->mode & SOAP_XML_STRICT)) {
  16477. /* YYYYMMDD */
  16478. T.tm_year = (int) (d / 10000);
  16479. T.tm_mon = (int) (d / 100 % 100);
  16480. T.tm_mday = (int) (d % 100);
  16481. } else {
  16482. return soap->error = SOAP_TYPE;
  16483. }
  16484. if (*t == 'T' || ((*t == 't' || *t == ' ') && !(soap->mode & SOAP_XML_STRICT))) {
  16485. d = soap_strtoul(t + 1, &t, 10);
  16486. if (*t == ':') {
  16487. /* Thh:mm:ss */
  16488. T.tm_hour = (int) d;
  16489. T.tm_min = (int) soap_strtoul(t + 1, &t, 10);
  16490. T.tm_sec = (int) soap_strtoul(t + 1, &t, 10);
  16491. } else if (!(soap->mode & SOAP_XML_STRICT)) {
  16492. /* Thhmmss */
  16493. T.tm_hour = (int) (d / 10000);
  16494. T.tm_min = (int) (d / 100 % 100);
  16495. T.tm_sec = (int) (d % 100);
  16496. } else {
  16497. return soap->error = SOAP_TYPE;
  16498. }
  16499. }
  16500. if (T.tm_year == 1)
  16501. T.tm_year = 70;
  16502. else
  16503. T.tm_year -= 1900;
  16504. T.tm_mon--;
  16505. if (*t == '.') {
  16506. for (t++; *t; t++)
  16507. if (*t < '0' || *t > '9')
  16508. break;
  16509. }
  16510. if (*t == ' ' && !(soap->mode & SOAP_XML_STRICT))
  16511. t++;
  16512. if (*t) {
  16513. #ifndef WITH_NOZONE
  16514. if (*t == '+' || *t == '-') {
  16515. int h, m;
  16516. m = (int) soap_strtol(t, &t, 10);
  16517. if (*t == ':') {
  16518. /* +hh:mm */
  16519. h = m;
  16520. m = (int) soap_strtol(t + 1, &t, 10);
  16521. if (h < 0)
  16522. m = -m;
  16523. } else if (!(soap->mode & SOAP_XML_STRICT)) {
  16524. /* +hhmm */
  16525. h = m / 100;
  16526. m = m % 100;
  16527. } else {
  16528. /* +hh */
  16529. h = m;
  16530. m = 0;
  16531. }
  16532. if (*t)
  16533. return soap->error = SOAP_TYPE;
  16534. T.tm_min -= m;
  16535. T.tm_hour -= h;
  16536. /* put hour and min in range */
  16537. T.tm_hour += T.tm_min / 60;
  16538. T.tm_min %= 60;
  16539. if (T.tm_min < 0) {
  16540. T.tm_min += 60;
  16541. T.tm_hour--;
  16542. }
  16543. T.tm_mday += T.tm_hour / 24;
  16544. T.tm_hour %= 24;
  16545. if (T.tm_hour < 0) {
  16546. T.tm_hour += 24;
  16547. T.tm_mday--;
  16548. }
  16549. /* note: day of the month may be out of range, timegm() handles it */
  16550. } else if (*t != 'Z') {
  16551. return soap->error = SOAP_TYPE;
  16552. }
  16553. #endif
  16554. *p = soap_timegm(&T);
  16555. } else /* no UTC or timezone, so assume we got a localtime */
  16556. {
  16557. T.tm_isdst = -1;
  16558. *p = mktime(&T);
  16559. }
  16560. }
  16561. return soap->error;
  16562. }
  16563. #endif
  16564. /******************************************************************************/
  16565. #ifndef WITH_LEAN
  16566. SOAP_FMAC1
  16567. time_t *
  16568. SOAP_FMAC2
  16569. soap_indateTime(struct soap *soap, const char *tag, time_t *p, const char *type, int t) {
  16570. if (soap_element_begin_in(soap, tag, 0, NULL))
  16571. return NULL;
  16572. if (*soap->type
  16573. && soap_match_tag(soap, soap->type, type)
  16574. && soap_match_tag(soap, soap->type, ":dateTime")) {
  16575. soap->error = SOAP_TYPE;
  16576. soap_revert(soap);
  16577. return NULL;
  16578. }
  16579. p = (time_t *) soap_id_enter(soap, soap->id, p, t, sizeof(time_t), NULL, NULL, NULL, NULL);
  16580. if (!p)
  16581. return NULL;
  16582. if (*soap->href != '#') {
  16583. int err = soap_s2dateTime(soap, soap_value(soap), p);
  16584. if ((soap->body && soap_element_end_in(soap, tag)) || err)
  16585. return NULL;
  16586. } else {
  16587. p = (time_t *) soap_id_forward(soap, soap->href, p, 0, t, t, sizeof(time_t), 0, NULL, NULL);
  16588. if (soap->body && soap_element_end_in(soap, tag))
  16589. return NULL;
  16590. }
  16591. return p;
  16592. }
  16593. #endif
  16594. /******************************************************************************/
  16595. SOAP_FMAC1
  16596. int
  16597. SOAP_FMAC2
  16598. soap_outliteral(struct soap *soap, const char *tag, char *const *p, const char *type) {
  16599. if (tag && *tag != '-')
  16600. if (soap_element_begin_out(soap, tag, 0, type))
  16601. return soap->error;
  16602. if (p && *p)
  16603. if (soap_send(soap, *p)) /* send as-is */
  16604. return soap->error;
  16605. if (tag && *tag != '-')
  16606. return soap_element_end_out(soap, tag);
  16607. return SOAP_OK;
  16608. }
  16609. /******************************************************************************/
  16610. SOAP_FMAC1
  16611. char **
  16612. SOAP_FMAC2
  16613. soap_inliteral(struct soap *soap, const char *tag, char **p) {
  16614. if (soap_element_begin_in(soap, tag, 1, NULL)) {
  16615. if (soap->error != SOAP_NO_TAG || soap_peek(soap) == SOAP_TT)
  16616. return NULL;
  16617. soap->error = SOAP_OK;
  16618. }
  16619. if (!p) {
  16620. p = (char **) soap_malloc(soap, sizeof(char *));
  16621. if (!p)
  16622. return NULL;
  16623. }
  16624. if (soap->body || (tag && *tag == '-')) {
  16625. if (tag && *tag != '-')
  16626. *p = soap_string_in(soap, -1, -1, -1, NULL);
  16627. else
  16628. *p = soap_string_in(soap, 0, -1, -1, NULL);
  16629. if (!*p)
  16630. return NULL;
  16631. if (!**p && tag && *tag == '-') {
  16632. soap->error = SOAP_NO_TAG;
  16633. return NULL;
  16634. }
  16635. } else if (soap->null) {
  16636. *p = NULL;
  16637. } else {
  16638. *p = soap_strdup(soap, SOAP_STR_EOS);
  16639. }
  16640. if (soap->body && soap_element_end_in(soap, tag))
  16641. return NULL;
  16642. return p;
  16643. }
  16644. /******************************************************************************/
  16645. #ifndef WITH_LEANER
  16646. SOAP_FMAC1
  16647. int
  16648. SOAP_FMAC2
  16649. soap_outwliteral(struct soap *soap, const char *tag, wchar_t *const *p, const char *type) {
  16650. if (tag && *tag != '-')
  16651. if (soap_element_begin_out(soap, tag, 0, type))
  16652. return soap->error;
  16653. if (p) {
  16654. wchar_t c;
  16655. const wchar_t *s = *p;
  16656. while ((c = *s++)) {
  16657. if (soap_pututf8(soap, (unsigned long) c)) /* send as-is in UTF8 */
  16658. return soap->error;
  16659. }
  16660. }
  16661. if (tag && *tag != '-')
  16662. return soap_element_end_out(soap, tag);
  16663. return SOAP_OK;
  16664. }
  16665. #endif
  16666. /******************************************************************************/
  16667. #ifndef WITH_LEANER
  16668. SOAP_FMAC1
  16669. wchar_t **
  16670. SOAP_FMAC2
  16671. soap_inwliteral(struct soap *soap, const char *tag, wchar_t **p) {
  16672. if (soap_element_begin_in(soap, tag, 1, NULL)) {
  16673. if (soap->error != SOAP_NO_TAG || soap_peek(soap) == SOAP_TT)
  16674. return NULL;
  16675. soap->error = SOAP_OK;
  16676. }
  16677. if (!p) {
  16678. p = (wchar_t **) soap_malloc(soap, sizeof(wchar_t *));
  16679. if (!p)
  16680. return NULL;
  16681. }
  16682. if (soap->body) {
  16683. if (tag && *tag != '-')
  16684. *p = soap_wstring_in(soap, -1, -1, -1, NULL);
  16685. else
  16686. *p = soap_wstring_in(soap, 0, -1, -1, NULL);
  16687. if (!*p)
  16688. return NULL;
  16689. if (!**p && tag && *tag == '-') {
  16690. soap->error = SOAP_NO_TAG;
  16691. return NULL;
  16692. }
  16693. } else if (tag && *tag == '-') {
  16694. soap->error = SOAP_NO_TAG;
  16695. return NULL;
  16696. } else if (soap->null) {
  16697. *p = NULL;
  16698. } else {
  16699. *p = soap_wstrdup(soap, L"");
  16700. }
  16701. if (soap->body && soap_element_end_in(soap, tag))
  16702. return NULL;
  16703. return p;
  16704. }
  16705. #endif
  16706. /******************************************************************************/
  16707. SOAP_FMAC1
  16708. const char *
  16709. SOAP_FMAC2
  16710. soap_value(struct soap *soap) {
  16711. size_t i;
  16712. soap_wchar c = 0;
  16713. char *s = soap->tmpbuf;
  16714. if (!soap->body)
  16715. return SOAP_STR_EOS;
  16716. do {
  16717. c = soap_get(soap);
  16718. } while (soap_coblank(c));
  16719. for (i = 0; i < sizeof(soap->tmpbuf) - 1; i++) {
  16720. if (c == SOAP_TT || c == SOAP_LT || (int) c == EOF)
  16721. break;
  16722. *s++ = (char) c;
  16723. c = soap_get(soap);
  16724. }
  16725. for (s--; i > 0; i--, s--) {
  16726. if (!soap_coblank((soap_wchar) *s))
  16727. break;
  16728. }
  16729. s[1] = '\0';
  16730. soap->tmpbuf[sizeof(soap->tmpbuf) - 1] = '\0'; /* appease */
  16731. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Element content value='%s'\n", soap->tmpbuf));
  16732. if (c == SOAP_TT || c == SOAP_LT || (int) c == EOF) {
  16733. soap_unget(soap, c);
  16734. } else {
  16735. soap->error = SOAP_LENGTH;
  16736. return NULL;
  16737. }
  16738. #ifdef WITH_DOM
  16739. if ((soap->mode & SOAP_XML_DOM) && soap->dom)
  16740. {
  16741. soap->dom->text = soap_strdup(soap, soap->tmpbuf);
  16742. if (!soap->dom->text)
  16743. return NULL;
  16744. }
  16745. #endif
  16746. return soap->tmpbuf; /* return non-null pointer */
  16747. }
  16748. /******************************************************************************/
  16749. #if !defined(WITH_LEANER) || !defined(WITH_NOHTTP)
  16750. SOAP_FMAC1
  16751. int
  16752. SOAP_FMAC2
  16753. soap_getline(struct soap *soap, char *buf, int len) {
  16754. char *s = buf;
  16755. int i = len;
  16756. soap_wchar c = 0;
  16757. for (;;) {
  16758. while (i > 1) {
  16759. c = soap_getchar(soap);
  16760. if (c == '\r' || c == '\n')
  16761. break;
  16762. if ((int) c == EOF)
  16763. return soap->error = SOAP_CHK_EOF;
  16764. *s++ = (char) c;
  16765. i--;
  16766. }
  16767. *s = '\0';
  16768. if (c != '\n')
  16769. c = soap_getchar(soap); /* got \r or something else, now get \n */
  16770. if (c == '\n') {
  16771. if (i == len) /* empty line: end of HTTP/MIME header */
  16772. break;
  16773. c = soap_get0(soap);
  16774. if (c != ' ' && c != '\t') /* HTTP line continuation? */
  16775. break;
  16776. } else if ((int) c == EOF) {
  16777. return soap->error = SOAP_CHK_EOF;
  16778. } else if (i <= 1) {
  16779. return soap->error = SOAP_HDR;
  16780. }
  16781. }
  16782. return SOAP_OK;
  16783. }
  16784. #endif
  16785. /******************************************************************************/
  16786. static ULONG64
  16787. soap_count_attachments(struct soap *soap) {
  16788. #ifndef WITH_LEANER
  16789. struct soap_multipart *content;
  16790. ULONG64 count = soap->count;
  16791. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Calculating the message size with attachments, current count="
  16792. SOAP_ULONG_FORMAT
  16793. "\n", count));
  16794. if ((soap->mode & SOAP_ENC_DIME) && !(soap->mode & SOAP_ENC_MTOM)) {
  16795. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Calculating the size of DIME attachments\n"));
  16796. for (content = soap->dime.first; content; content = content->next) {
  16797. count += 12 + ((content->size + 3) & (~3));
  16798. if (content->id)
  16799. count += ((strlen(content->id) + 3) & (~3));
  16800. if (content->type)
  16801. count += ((strlen(content->type) + 3) & (~3));
  16802. if (content->options)
  16803. count += ((((unsigned char) content->options[2] << 8) | ((unsigned char) content->options[3])) + 7) &
  16804. (~3);
  16805. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Size of DIME attachment content is %lu bytes\n",
  16806. (unsigned long) content->size));
  16807. }
  16808. }
  16809. if ((soap->mode & SOAP_ENC_MIME) && soap->mime.boundary) {
  16810. size_t n = strlen(soap->mime.boundary);
  16811. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Calculating the size of MIME attachments\n"));
  16812. for (content = soap->mime.first; content; content = content->next) {
  16813. const char *s;
  16814. /* count \r\n--boundary\r\n */
  16815. count += 6 + n;
  16816. /* count Content-Type: ...\r\n */
  16817. if (content->type)
  16818. count += 16 + strlen(content->type);
  16819. /* count Content-Transfer-Encoding: ...\r\n */
  16820. s = soap_code_str(mime_codes, content->encoding);
  16821. if (s)
  16822. count += 29 + strlen(s);
  16823. /* count Content-ID: ...\r\n */
  16824. if (content->id)
  16825. count += 14 + strlen(content->id);
  16826. /* count Content-Location: ...\r\n */
  16827. if (content->location)
  16828. count += 20 + strlen(content->location);
  16829. /* count Content-Description: ...\r\n */
  16830. if (content->description)
  16831. count += 23 + strlen(content->description);
  16832. /* count \r\n...content */
  16833. count += 2 + content->size;
  16834. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Size of MIME attachment content is %lu bytes\n",
  16835. (unsigned long) content->size));
  16836. }
  16837. /* count \r\n--boundary-- */
  16838. count += 6 + n;
  16839. }
  16840. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "New count="
  16841. SOAP_ULONG_FORMAT
  16842. "\n", count));
  16843. return count;
  16844. #else
  16845. return soap->count;
  16846. #endif
  16847. }
  16848. /******************************************************************************/
  16849. #ifndef WITH_LEANER
  16850. static int
  16851. soap_putdimefield(struct soap *soap, const char *s, size_t n) {
  16852. if (soap_send_raw(soap, s, n))
  16853. return soap->error;
  16854. return soap_send_raw(soap, SOAP_STR_PADDING, -(long) n & 3);
  16855. }
  16856. #endif
  16857. /******************************************************************************/
  16858. #ifndef WITH_LEANER
  16859. SOAP_FMAC1
  16860. char *
  16861. SOAP_FMAC2
  16862. soap_dime_option(struct soap *soap, unsigned short optype, const char *option) {
  16863. size_t n;
  16864. char *s = NULL;
  16865. if (option) {
  16866. n = strlen(option);
  16867. s = (char *) soap_malloc(soap, n + 5);
  16868. if (s) {
  16869. s[0] = (char) (optype >> 8);
  16870. s[1] = (char) (optype & 0xFF);
  16871. s[2] = (char) (n >> 8);
  16872. s[3] = (char) (n & 0xFF);
  16873. soap_strcpy(s + 4, n + 1, option);
  16874. }
  16875. }
  16876. return s;
  16877. }
  16878. #endif
  16879. /******************************************************************************/
  16880. #ifndef WITH_LEANER
  16881. SOAP_FMAC1
  16882. int
  16883. SOAP_FMAC2
  16884. soap_putdimehdr(struct soap *soap) {
  16885. unsigned char tmp[12];
  16886. size_t optlen = 0, idlen = 0, typelen = 0;
  16887. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Put DIME header id='%s'\n", soap->dime.id ? soap->dime.id : SOAP_STR_EOS));
  16888. if (soap->dime.options)
  16889. optlen = (((unsigned char) soap->dime.options[2] << 8) | ((unsigned char) soap->dime.options[3])) + 4;
  16890. if (soap->dime.id) {
  16891. idlen = strlen(soap->dime.id);
  16892. if (idlen > 0x0000FFFF)
  16893. idlen = 0x0000FFFF;
  16894. }
  16895. if (soap->dime.type) {
  16896. typelen = strlen(soap->dime.type);
  16897. if (typelen > 0x0000FFFF)
  16898. typelen = 0x0000FFFF;
  16899. }
  16900. tmp[0] = SOAP_DIME_VERSION | (soap->dime.flags & 0x7);
  16901. tmp[1] = soap->dime.flags & 0xF0;
  16902. tmp[2] = (char) (optlen >> 8);
  16903. tmp[3] = (char) (optlen & 0xFF);
  16904. tmp[4] = (char) (idlen >> 8);
  16905. tmp[5] = (char) (idlen & 0xFF);
  16906. tmp[6] = (char) (typelen >> 8);
  16907. tmp[7] = (char) (typelen & 0xFF);
  16908. tmp[8] = (char) (soap->dime.size >> 24);
  16909. tmp[9] = (char) ((soap->dime.size >> 16) & 0xFF);
  16910. tmp[10] = (char) ((soap->dime.size >> 8) & 0xFF);
  16911. tmp[11] = (char) (soap->dime.size & 0xFF);
  16912. if (soap_send_raw(soap, (char *) tmp, 12)
  16913. || soap_putdimefield(soap, soap->dime.options, optlen)
  16914. || soap_putdimefield(soap, soap->dime.id, idlen)
  16915. || soap_putdimefield(soap, soap->dime.type, typelen))
  16916. return soap->error;
  16917. return SOAP_OK;
  16918. }
  16919. #endif
  16920. /******************************************************************************/
  16921. #ifndef WITH_LEANER
  16922. SOAP_FMAC1
  16923. int
  16924. SOAP_FMAC2
  16925. soap_putdime(struct soap *soap) {
  16926. struct soap_multipart *content;
  16927. if (!(soap->mode & SOAP_ENC_DIME))
  16928. return SOAP_OK;
  16929. for (content = soap->dime.first; content; content = content->next) {
  16930. void *handle;
  16931. soap->dime.size = content->size;
  16932. soap->dime.id = content->id;
  16933. soap->dime.type = content->type;
  16934. soap->dime.options = content->options;
  16935. soap->dime.flags = SOAP_DIME_VERSION | SOAP_DIME_MEDIA;
  16936. if (soap->fdimereadopen &&
  16937. ((handle = soap->fdimereadopen(soap, (void *) content->ptr, content->id, content->type,
  16938. content->options)) != NULL || soap->error)) {
  16939. size_t size = content->size;
  16940. if (!handle) {
  16941. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "fdimereadopen failed\n"));
  16942. return soap->error;
  16943. }
  16944. if (!size && ((soap->mode & SOAP_ENC_PLAIN) || (soap->mode & SOAP_IO) == SOAP_IO_CHUNK ||
  16945. (soap->mode & SOAP_IO) == SOAP_IO_STORE)) {
  16946. size_t chunksize = sizeof(soap->tmpbuf);
  16947. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Chunked streaming DIME\n"));
  16948. do {
  16949. size = soap->fdimeread(soap, handle, soap->tmpbuf, chunksize);
  16950. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "fdimeread returned %lu bytes\n", (unsigned long) size));
  16951. if (size < chunksize) {
  16952. soap->dime.flags &= ~SOAP_DIME_CF;
  16953. if (!content->next)
  16954. soap->dime.flags |= SOAP_DIME_ME;
  16955. } else {
  16956. soap->dime.flags |= SOAP_DIME_CF;
  16957. }
  16958. soap->dime.size = size;
  16959. if (soap_putdimehdr(soap)
  16960. || soap_putdimefield(soap, soap->tmpbuf, size))
  16961. break;
  16962. if (soap->dime.id) {
  16963. soap->dime.flags &= ~(SOAP_DIME_MB | SOAP_DIME_MEDIA);
  16964. soap->dime.id = NULL;
  16965. soap->dime.type = NULL;
  16966. soap->dime.options = NULL;
  16967. }
  16968. } while (size >= chunksize);
  16969. } else {
  16970. if (!content->next)
  16971. soap->dime.flags |= SOAP_DIME_ME;
  16972. if (soap_putdimehdr(soap))
  16973. return soap->error;
  16974. do {
  16975. size_t bufsize;
  16976. if (size < sizeof(soap->tmpbuf))
  16977. bufsize = size;
  16978. else
  16979. bufsize = sizeof(soap->tmpbuf);
  16980. bufsize = soap->fdimeread(soap, handle, soap->tmpbuf, bufsize);
  16981. if (!bufsize) {
  16982. DBGLOG(TEST, SOAP_MESSAGE(fdebug,
  16983. "fdimeread failed: insufficient data (%lu bytes remaining from %lu bytes)\n",
  16984. (unsigned long) size, (unsigned long) content->size));
  16985. soap->error = SOAP_CHK_EOF;
  16986. break;
  16987. }
  16988. if (soap_send_raw(soap, soap->tmpbuf, bufsize))
  16989. break;
  16990. size -= bufsize;
  16991. } while (size);
  16992. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "fdimereadclose\n"));
  16993. if (soap_send_raw(soap, SOAP_STR_PADDING, -(long) soap->dime.size & 3))
  16994. return soap->error;
  16995. }
  16996. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "fdimereadclose\n"));
  16997. if (soap->fdimereadclose)
  16998. soap->fdimereadclose(soap, handle);
  16999. } else {
  17000. if (!content->next)
  17001. soap->dime.flags |= SOAP_DIME_ME;
  17002. if (soap_putdimehdr(soap)
  17003. || soap_putdimefield(soap, (char *) content->ptr, content->size))
  17004. return soap->error;
  17005. }
  17006. }
  17007. return SOAP_OK;
  17008. }
  17009. #endif
  17010. /******************************************************************************/
  17011. #ifndef WITH_LEANER
  17012. static char *
  17013. soap_getdimefield(struct soap *soap, size_t n) {
  17014. char *p = NULL;
  17015. if (n > 0) {
  17016. p = (char *) soap_malloc(soap, n + 1 > n ? n + 1 : n);
  17017. if (p) {
  17018. char *s = p;
  17019. size_t i;
  17020. for (i = n; i > 0; i--) {
  17021. soap_wchar c = soap_get1(soap);
  17022. if ((int) c == EOF) {
  17023. soap->error = SOAP_CHK_EOF;
  17024. return NULL;
  17025. }
  17026. *s++ = (char) c;
  17027. }
  17028. if (n + 1 > n)
  17029. *s = '\0'; /* force NUL terminated */
  17030. soap->error = soap_move(soap, (size_t) (-(long) n & 3));
  17031. if (soap->error)
  17032. return NULL;
  17033. } else {
  17034. soap->error = SOAP_EOM;
  17035. }
  17036. }
  17037. return p;
  17038. }
  17039. #endif
  17040. /******************************************************************************/
  17041. #ifndef WITH_LEANER
  17042. SOAP_FMAC1
  17043. int
  17044. SOAP_FMAC2
  17045. soap_getdimehdr(struct soap *soap) {
  17046. soap_wchar c;
  17047. char *s;
  17048. int i;
  17049. unsigned char tmp[12];
  17050. size_t optlen, idlen, typelen;
  17051. if (!(soap->mode & SOAP_ENC_DIME))
  17052. return soap->error = SOAP_DIME_END;
  17053. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Get DIME header\n"));
  17054. s = (char *) tmp;
  17055. for (i = 12; i > 0; i--) {
  17056. c = soap_getchar(soap);
  17057. if ((int) c == EOF)
  17058. return soap->error = SOAP_CHK_EOF;
  17059. *s++ = (char) c;
  17060. }
  17061. if ((tmp[0] & 0xF8) != SOAP_DIME_VERSION)
  17062. return soap->error = SOAP_DIME_MISMATCH;
  17063. soap->dime.flags = (tmp[0] & 0x7) | (tmp[1] & 0xF0);
  17064. optlen = (tmp[2] << 8) | tmp[3];
  17065. idlen = (tmp[4] << 8) | tmp[5];
  17066. typelen = (tmp[6] << 8) | tmp[7];
  17067. soap->dime.size = ((size_t) tmp[8] << 24) | ((size_t) tmp[9] << 16) | ((size_t) tmp[10] << 8) | ((size_t) tmp[11]);
  17068. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "DIME size=%lu flags=0x%X\n", (unsigned long) soap->dime.size, soap->dime.flags));
  17069. soap->dime.options = soap_getdimefield(soap, optlen);
  17070. if (!soap->dime.options && soap->error)
  17071. return soap->error;
  17072. soap->dime.id = soap_getdimefield(soap, idlen);
  17073. if (!soap->dime.id && soap->error)
  17074. return soap->error;
  17075. soap->dime.type = soap_getdimefield(soap, typelen);
  17076. if (!soap->dime.type && soap->error)
  17077. return soap->error;
  17078. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "DIME flags=%x id='%s', type='%s', options='%s'\n", soap->dime.flags,
  17079. soap->dime.id ? soap->dime.id : SOAP_STR_EOS, soap->dime.type ? soap->dime.type : "",
  17080. soap->dime.options ? soap->dime.options + 4 : SOAP_STR_EOS));
  17081. if ((soap->dime.flags & SOAP_DIME_ME))
  17082. soap->mode &= ~SOAP_ENC_DIME;
  17083. return SOAP_OK;
  17084. }
  17085. #endif
  17086. /******************************************************************************/
  17087. #ifndef WITH_LEANER
  17088. SOAP_FMAC1
  17089. int
  17090. SOAP_FMAC2
  17091. soap_getdime(struct soap *soap) {
  17092. if (soap->dime.buflen || soap->dime.chunksize) {
  17093. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Skip remainder of SOAP in DIME (%u bytes or %u bytes in chunk left)\n",
  17094. (unsigned int) soap->dime.buflen, (unsigned int) soap->dime.chunksize));
  17095. do
  17096. if (soap_get1(soap) == (int) EOF)
  17097. return soap->error = SOAP_CHK_EOF;
  17098. while (soap->dime.buflen || soap->dime.chunksize);
  17099. if (soap_move(soap, (size_t) (-(long) soap->dime.size & 3)))
  17100. return soap->error = SOAP_CHK_EOF;
  17101. if (!(soap->mode & SOAP_ENC_DIME))
  17102. return SOAP_OK;
  17103. } else {
  17104. if (soap_move(soap, (size_t) (((soap->dime.size + 3) & (~3)) - soap_tell(soap))))
  17105. return soap->error = SOAP_CHK_EOF;
  17106. }
  17107. for (;;) {
  17108. struct soap_multipart *content;
  17109. if (soap_getdimehdr(soap))
  17110. break;
  17111. if (soap->fdimewriteopen &&
  17112. ((soap->dime.ptr = (char *) soap->fdimewriteopen(soap, soap->dime.id, soap->dime.type,
  17113. soap->dime.options)) != NULL || soap->error)) {
  17114. const char *id, *type, *options;
  17115. size_t size, n;
  17116. if (!soap->dime.ptr)
  17117. return soap->error;
  17118. id = soap->dime.id;
  17119. type = soap->dime.type;
  17120. options = soap->dime.options;
  17121. for (;;) {
  17122. size = soap->dime.size;
  17123. for (;;) {
  17124. n = soap->buflen - soap->bufidx;
  17125. if (size < n)
  17126. n = size;
  17127. soap->error = soap->fdimewrite(soap, (void *) soap->dime.ptr, soap->buf + soap->bufidx, n);
  17128. if (soap->error)
  17129. break;
  17130. size -= n;
  17131. if (!size) {
  17132. soap->bufidx += n;
  17133. break;
  17134. }
  17135. if (soap_recv(soap)) {
  17136. soap->error = SOAP_EOF;
  17137. goto end;
  17138. }
  17139. }
  17140. if (soap_move(soap, (size_t) (-(long) soap->dime.size & 3))) {
  17141. soap->error = SOAP_EOF;
  17142. break;
  17143. }
  17144. if (!(soap->dime.flags & SOAP_DIME_CF))
  17145. break;
  17146. if (soap_getdimehdr(soap))
  17147. break;
  17148. }
  17149. end:
  17150. if (soap->fdimewriteclose)
  17151. soap->fdimewriteclose(soap, (void *) soap->dime.ptr);
  17152. soap->dime.size = 0;
  17153. soap->dime.id = id;
  17154. soap->dime.type = type;
  17155. soap->dime.options = options;
  17156. } else if ((soap->dime.flags & SOAP_DIME_CF)) {
  17157. const char *id, *type, *options;
  17158. id = soap->dime.id;
  17159. type = soap->dime.type;
  17160. options = soap->dime.options;
  17161. if (soap_alloc_block(soap) == NULL)
  17162. return soap->error = SOAP_EOM;
  17163. for (;;) {
  17164. soap_wchar c;
  17165. size_t i;
  17166. char *s;
  17167. if (soap->dime.size > SOAP_MAXDIMESIZE) {
  17168. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "DIME size=%lu exceeds SOAP_MAXDIMESIZE=%lu\n",
  17169. (unsigned long) soap->dime.size, (unsigned long) SOAP_MAXDIMESIZE));
  17170. return soap->error = SOAP_DIME_ERROR;
  17171. }
  17172. s = (char *) soap_push_block(soap, NULL, soap->dime.size);
  17173. if (!s)
  17174. return soap->error = SOAP_EOM;
  17175. for (i = soap->dime.size; i > 0; i--) {
  17176. c = soap_get1(soap);
  17177. if ((int) c == EOF)
  17178. return soap->error = SOAP_EOF;
  17179. *s++ = (char) c;
  17180. }
  17181. if (soap_move(soap, (size_t) (-(long) soap->dime.size & 3)))
  17182. return soap->error = SOAP_EOF;
  17183. if (!(soap->dime.flags & SOAP_DIME_CF))
  17184. break;
  17185. if (soap_getdimehdr(soap))
  17186. return soap->error;
  17187. }
  17188. soap->dime.size = soap->blist->size;
  17189. if (soap->dime.size + 1 > soap->dime.size)
  17190. soap->blist->size++; /* allocate one more byte in blist for the terminating '\0' */
  17191. soap->dime.ptr = soap_save_block(soap, NULL, NULL, 0);
  17192. if (!soap->dime.ptr)
  17193. return soap->error;
  17194. if (soap->dime.size + 1 > soap->dime.size)
  17195. soap->dime.ptr[soap->dime.size] = '\0'; /* make 0-terminated, just in case even though this is binary data */
  17196. soap->dime.id = id;
  17197. soap->dime.type = type;
  17198. soap->dime.options = options;
  17199. } else {
  17200. soap->dime.ptr = soap_getdimefield(soap, soap->dime.size);
  17201. }
  17202. content = soap_alloc_multipart(soap, &soap->dime.first, &soap->dime.last, soap->dime.ptr, soap->dime.size);
  17203. if (!content)
  17204. return soap->error = SOAP_EOM;
  17205. content->id = soap->dime.id;
  17206. content->type = soap->dime.type;
  17207. content->options = soap->dime.options;
  17208. if (soap->error)
  17209. return soap->error;
  17210. soap_resolve_attachment(soap, content);
  17211. }
  17212. if (soap->error != SOAP_DIME_END)
  17213. return soap->error;
  17214. return soap->error = SOAP_OK;
  17215. }
  17216. #endif
  17217. /******************************************************************************/
  17218. #ifndef WITH_LEANER
  17219. SOAP_FMAC1
  17220. int
  17221. SOAP_FMAC2
  17222. soap_getmimehdr(struct soap *soap) {
  17223. struct soap_multipart *content;
  17224. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Get MIME header\n"));
  17225. do {
  17226. if (soap_getline(soap, soap->msgbuf, sizeof(soap->msgbuf)))
  17227. return soap->error;
  17228. } while (!*soap->msgbuf);
  17229. if (soap->msgbuf[0] == '-' && soap->msgbuf[1] == '-') {
  17230. char *s = soap->msgbuf + strlen(soap->msgbuf) - 1;
  17231. /* remove white space */
  17232. while (soap_coblank((soap_wchar) *s))
  17233. s--;
  17234. s[1] = '\0';
  17235. if (soap->mime.boundary) {
  17236. if (strcmp(soap->msgbuf + 2, soap->mime.boundary))
  17237. return soap->error = SOAP_MIME_ERROR;
  17238. } else {
  17239. soap->mime.boundary = soap_strdup(soap, soap->msgbuf + 2);
  17240. if (!soap->mime.boundary)
  17241. return soap->error = SOAP_EOM;
  17242. }
  17243. if (soap_getline(soap, soap->msgbuf, sizeof(soap->msgbuf)))
  17244. return soap->error;
  17245. }
  17246. if (soap_set_mime_attachment(soap, NULL, 0, SOAP_MIME_NONE, NULL, NULL, NULL, NULL))
  17247. return soap->error = SOAP_EOM;
  17248. content = soap->mime.last;
  17249. for (;;) {
  17250. char *key = soap->msgbuf;
  17251. char *val;
  17252. if (!*key)
  17253. break;
  17254. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "MIME header: %s\n", key));
  17255. val = strchr(soap->msgbuf, ':');
  17256. if (val) {
  17257. *val = '\0';
  17258. do {
  17259. val++;
  17260. } while (*val && *val <= 32);
  17261. if (!soap_tag_cmp(key, "Content-ID"))
  17262. content->id = soap_strdup(soap, val);
  17263. else if (!soap_tag_cmp(key, "Content-Location"))
  17264. content->location = soap_strdup(soap, val);
  17265. else if (!content->id && !soap_tag_cmp(key, "Content-Disposition"))
  17266. content->id = soap_strdup(soap, soap_http_header_attribute(soap, val, "name"));
  17267. else if (!soap_tag_cmp(key, "Content-Type"))
  17268. content->type = soap_strdup(soap, val);
  17269. else if (!soap_tag_cmp(key, "Content-Description"))
  17270. content->description = soap_strdup(soap, val);
  17271. else if (!soap_tag_cmp(key, "Content-Transfer-Encoding"))
  17272. content->encoding = (enum soap_mime_encoding) soap_code_int(mime_codes, val, (LONG64) SOAP_MIME_NONE);
  17273. }
  17274. if (soap_getline(soap, key, sizeof(soap->msgbuf)))
  17275. return soap->error;
  17276. }
  17277. return SOAP_OK;
  17278. }
  17279. #endif
  17280. /******************************************************************************/
  17281. #ifndef WITH_LEANER
  17282. SOAP_FMAC1
  17283. int
  17284. SOAP_FMAC2
  17285. soap_getmime(struct soap *soap) {
  17286. while (soap_recv_mime_attachment(soap, NULL))
  17287. continue;
  17288. return soap->error;
  17289. }
  17290. #endif
  17291. /******************************************************************************/
  17292. #ifndef WITH_LEANER
  17293. SOAP_FMAC1
  17294. void
  17295. SOAP_FMAC2
  17296. soap_post_check_mime_attachments(struct soap *soap) {
  17297. soap->imode |= SOAP_MIME_POSTCHECK;
  17298. }
  17299. #endif
  17300. /******************************************************************************/
  17301. #ifndef WITH_LEANER
  17302. SOAP_FMAC1
  17303. int
  17304. SOAP_FMAC2
  17305. soap_check_mime_attachments(struct soap *soap) {
  17306. if ((soap->mode & SOAP_MIME_POSTCHECK))
  17307. return soap_recv_mime_attachment(soap, NULL) != NULL;
  17308. return SOAP_OK;
  17309. }
  17310. #endif
  17311. /******************************************************************************/
  17312. #ifndef WITH_LEANER
  17313. SOAP_FMAC1
  17314. struct soap_multipart *
  17315. SOAP_FMAC2
  17316. soap_recv_mime_attachment(struct soap *soap, void *handle) {
  17317. soap_wchar c = 0;
  17318. size_t i, m = 0;
  17319. char *s, *t = NULL;
  17320. struct soap_multipart *content;
  17321. short flag = 0;
  17322. if (!(soap->mode & SOAP_ENC_MIME))
  17323. return NULL;
  17324. content = soap->mime.last;
  17325. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Get MIME (%p)\n", (void *) content));
  17326. if (!content) {
  17327. if (soap_getmimehdr(soap))
  17328. return NULL;
  17329. content = soap->mime.last;
  17330. } else if (content != soap->mime.first) {
  17331. if (soap->fmimewriteopen &&
  17332. ((content->ptr = (char *) soap->fmimewriteopen(soap, (void *) handle, content->id, content->type,
  17333. content->description, content->encoding)) != NULL ||
  17334. soap->error)) {
  17335. if (!content->ptr)
  17336. return NULL;
  17337. }
  17338. }
  17339. DBGLOG(TEST,
  17340. SOAP_MESSAGE(fdebug, "Parsing MIME content id='%s' type='%s'\n", content->id ? content->id : SOAP_STR_EOS,
  17341. content->type ? content->type : SOAP_STR_EOS));
  17342. if (!content->ptr && soap_alloc_block(soap) == NULL) {
  17343. soap->error = SOAP_EOM;
  17344. return NULL;
  17345. }
  17346. for (;;) {
  17347. if (content->ptr) {
  17348. s = soap->tmpbuf;
  17349. } else {
  17350. s = (char *) soap_push_block(soap, NULL, sizeof(soap->tmpbuf));
  17351. if (!s) {
  17352. soap->error = SOAP_EOM;
  17353. return NULL;
  17354. }
  17355. }
  17356. for (i = 0; i < sizeof(soap->tmpbuf); i++) {
  17357. if (m > 0) {
  17358. *s++ = *t++;
  17359. m--;
  17360. } else {
  17361. if (!flag) {
  17362. c = soap_getchar(soap);
  17363. if ((int) c == EOF) {
  17364. if (content->ptr && soap->fmimewriteclose)
  17365. soap->fmimewriteclose(soap, (void *) content->ptr);
  17366. soap->error = SOAP_CHK_EOF;
  17367. return NULL;
  17368. }
  17369. }
  17370. if (flag || c == '\r') {
  17371. memset((void *) soap->msgbuf, 0, sizeof(soap->msgbuf));
  17372. soap_strcpy(soap->msgbuf, sizeof(soap->msgbuf), "\n--");
  17373. if (soap->mime.boundary) {
  17374. if (soap_strncat(soap->msgbuf, sizeof(soap->msgbuf), soap->mime.boundary,
  17375. sizeof(soap->msgbuf) - 4)) {
  17376. soap->error = SOAP_MIME_ERROR;
  17377. return NULL;
  17378. }
  17379. }
  17380. t = soap->msgbuf;
  17381. do {
  17382. c = soap_getchar(soap);
  17383. } while (c == *t++);
  17384. if ((int) c == EOF) {
  17385. if (content->ptr && soap->fmimewriteclose)
  17386. soap->fmimewriteclose(soap, (void *) content->ptr);
  17387. soap->error = SOAP_CHK_EOF;
  17388. return NULL;
  17389. }
  17390. if (!*--t)
  17391. goto end;
  17392. *t = (char) c;
  17393. flag = (c == '\r');
  17394. m = t - soap->msgbuf + 1 - flag;
  17395. t = soap->msgbuf;
  17396. c = '\r';
  17397. }
  17398. *s++ = (char) c;
  17399. }
  17400. }
  17401. if (content->ptr && soap->fmimewrite) {
  17402. soap->error = soap->fmimewrite(soap, (void *) content->ptr, soap->tmpbuf, i);
  17403. if (soap->error)
  17404. break;
  17405. }
  17406. }
  17407. end:
  17408. if (content->ptr) {
  17409. if (!soap->error && soap->fmimewrite)
  17410. soap->error = soap->fmimewrite(soap, (void *) content->ptr, soap->tmpbuf, i);
  17411. if (soap->fmimewriteclose)
  17412. soap->fmimewriteclose(soap, (void *) content->ptr);
  17413. if (soap->error)
  17414. return NULL;
  17415. } else {
  17416. *s = '\0'; /* make 0-terminated, just in case even though this is binary data */
  17417. content->size = soap_size_block(soap, NULL, i + 1) - 1; /* last block with '\0' */
  17418. content->ptr = soap_save_block(soap, NULL, NULL, 0);
  17419. }
  17420. soap_resolve_attachment(soap, content);
  17421. if (c == '-' && soap_getchar(soap) == '-') {
  17422. soap->mode &= ~SOAP_ENC_MIME;
  17423. if ((soap->mode & SOAP_MIME_POSTCHECK) && soap_end_recv(soap)) {
  17424. if (soap->keep_alive == -2) /* special case to keep alive */
  17425. soap->keep_alive = 0;
  17426. soap_closesock(soap);
  17427. return NULL;
  17428. }
  17429. } else {
  17430. while (c != '\r' && (int) c != EOF && soap_coblank(c))
  17431. c = soap_getchar(soap);
  17432. if (c != '\r' || soap_getchar(soap) != '\n') {
  17433. soap->error = SOAP_MIME_ERROR;
  17434. return NULL;
  17435. }
  17436. if (soap_getmimehdr(soap))
  17437. return NULL;
  17438. }
  17439. return content;
  17440. }
  17441. #endif
  17442. /******************************************************************************/
  17443. #ifndef WITH_LEANER
  17444. SOAP_FMAC1
  17445. int
  17446. SOAP_FMAC2
  17447. soap_match_cid(struct soap *soap, const char *s, const char *t) {
  17448. size_t n;
  17449. if (!s)
  17450. return 1;
  17451. if (!strcmp(s, t))
  17452. return 0;
  17453. if (!strncmp(s, "cid:", 4))
  17454. s += 4;
  17455. n = strlen(t);
  17456. if (*t == '<') {
  17457. t++;
  17458. n -= 2;
  17459. }
  17460. if (!strncmp(s, t, n) && !s[n])
  17461. return 0;
  17462. (void) soap_decode(soap->tmpbuf, sizeof(soap->tmpbuf), s, SOAP_STR_EOS);
  17463. if (!strncmp(soap->tmpbuf, t, n) && !soap->tmpbuf[n])
  17464. return 0;
  17465. return 1;
  17466. }
  17467. #endif
  17468. /******************************************************************************/
  17469. /* return UUID "<prefix>xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx" in a temporary buffer */
  17470. SOAP_FMAC1
  17471. const char *
  17472. SOAP_FMAC2
  17473. soap_rand_uuid(struct soap *soap, const char *prefix) {
  17474. int r1, r2, r3, r4;
  17475. #ifdef WITH_OPENSSL
  17476. r1 = soap_random;
  17477. r2 = soap_random;
  17478. #else
  17479. size_t i;
  17480. static int k = 0xFACEB00C;
  17481. int lo = k % 127773;
  17482. int hi = k / 127773;
  17483. # if defined(HAVE_GETTIMEOFDAY)
  17484. struct timeval tv;
  17485. gettimeofday(&tv, NULL);
  17486. r1 = 10000000 * tv.tv_sec + tv.tv_usec;
  17487. # elif defined(UNDER_CE)
  17488. r1 = (int)Random();
  17489. # elif !defined(WITH_LEAN)
  17490. r1 = (int)time(NULL);
  17491. # else
  17492. r1 = k;
  17493. # endif
  17494. k = 16807 * lo - 2836 * hi;
  17495. if (k <= 0)
  17496. k += 0x7FFFFFFF;
  17497. r2 = k;
  17498. /* k &= 0x8FFFFFFF; */
  17499. for (i = 0; i < (sizeof(soap->buf) < 16UL ? sizeof(soap->buf) : 16UL); i++)
  17500. r2 += soap->buf[i];
  17501. #endif
  17502. r3 = soap_random;
  17503. r4 = soap_random;
  17504. (SOAP_SNPRINTF(soap->tmpbuf, sizeof(soap->tmpbuf), prefix ? strlen(prefix) + 37 : 37),
  17505. "%s%8.8x-%4.4hx-4%3.3hx-%4.4hx-%4.4hx%8.8x", prefix ? prefix : SOAP_STR_EOS, r1, (short) (r2 >> 16),
  17506. (short) (((short) r2 >> 4) & 0x0FFF), (short) (((short) (r3 >> 16) & 0x3FFF) | 0x8000), (short) r3, r4);
  17507. return soap->tmpbuf;
  17508. }
  17509. /******************************************************************************/
  17510. #ifndef WITH_LEANER
  17511. static void
  17512. soap_resolve_attachment(struct soap *soap, struct soap_multipart *content) {
  17513. if (content->id) {
  17514. struct soap_xlist **xp = &soap->xlist;
  17515. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Resolving attachment data for id='%s'\n", content->id));
  17516. while (*xp) {
  17517. struct soap_xlist *xq = *xp;
  17518. if (!soap_match_cid(soap, xq->id, content->id)) {
  17519. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Found matching attachment id='%s' for content id='%s'\n", xq->id,
  17520. content->id));
  17521. *xp = xq->next;
  17522. *xq->ptr = (unsigned char *) content->ptr;
  17523. *xq->size = (int) content->size;
  17524. *xq->type = (char *) content->type;
  17525. if (content->options)
  17526. *xq->options = (char *) content->options;
  17527. else
  17528. *xq->options = (char *) content->description;
  17529. SOAP_FREE(soap, xq);
  17530. } else {
  17531. xp = &(*xp)->next;
  17532. }
  17533. }
  17534. }
  17535. }
  17536. #endif
  17537. /******************************************************************************/
  17538. #ifndef WITH_LEANER
  17539. SOAP_FMAC1
  17540. int
  17541. SOAP_FMAC2
  17542. soap_putmimehdr(struct soap *soap, struct soap_multipart *content) {
  17543. const char *s;
  17544. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "MIME attachment type='%s'\n", content->type ? content->type : SOAP_STR_EOS));
  17545. if (soap_send3(soap, "\r\n--", soap->mime.boundary, "\r\n"))
  17546. return soap->error;
  17547. if (content->type && soap_send3(soap, "Content-Type: ", content->type, "\r\n"))
  17548. return soap->error;
  17549. s = soap_code_str(mime_codes, content->encoding);
  17550. if (s && soap_send3(soap, "Content-Transfer-Encoding: ", s, "\r\n"))
  17551. return soap->error;
  17552. if (content->id && soap_send3(soap, "Content-ID: ", content->id, "\r\n"))
  17553. return soap->error;
  17554. if (content->location && soap_send3(soap, "Content-Location: ", content->location, "\r\n"))
  17555. return soap->error;
  17556. if (content->description && soap_send3(soap, "Content-Description: ", content->description, "\r\n"))
  17557. return soap->error;
  17558. return soap_send_raw(soap, "\r\n", 2);
  17559. }
  17560. #endif
  17561. /******************************************************************************/
  17562. #ifndef WITH_LEANER
  17563. SOAP_FMAC1
  17564. int
  17565. SOAP_FMAC2
  17566. soap_putmime(struct soap *soap) {
  17567. struct soap_multipart *content;
  17568. if (!(soap->mode & SOAP_ENC_MIME) || !soap->mime.boundary)
  17569. return SOAP_OK;
  17570. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Sending MIME attachments\n"));
  17571. for (content = soap->mime.first; content; content = content->next) {
  17572. void *handle;
  17573. if (soap->fmimereadopen &&
  17574. ((handle = soap->fmimereadopen(soap, (void *) content->ptr, content->id, content->type,
  17575. content->description)) != NULL || soap->error)) {
  17576. size_t size = content->size;
  17577. if (!handle) {
  17578. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "fmimereadopen failed\n"));
  17579. return soap->error;
  17580. }
  17581. if (soap_putmimehdr(soap, content))
  17582. return soap->error;
  17583. if (!size) {
  17584. if ((soap->mode & SOAP_ENC_PLAIN) || (soap->mode & SOAP_IO) == SOAP_IO_CHUNK ||
  17585. (soap->mode & SOAP_IO) == SOAP_IO_STORE) {
  17586. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Chunked streaming MIME\n"));
  17587. do {
  17588. size = soap->fmimeread(soap, handle, soap->tmpbuf, sizeof(soap->tmpbuf));
  17589. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "fmimeread returned %lu bytes\n", (unsigned long) size));
  17590. if (soap_send_raw(soap, soap->tmpbuf, size))
  17591. break;
  17592. } while (size);
  17593. } else {
  17594. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Error: cannot chunk streaming MIME (no HTTP chunking)\n"));
  17595. }
  17596. } else {
  17597. do {
  17598. size_t bufsize;
  17599. if (size < sizeof(soap->tmpbuf))
  17600. bufsize = size;
  17601. else
  17602. bufsize = sizeof(soap->tmpbuf);
  17603. bufsize = soap->fmimeread(soap, handle, soap->tmpbuf, bufsize);
  17604. if (!bufsize) {
  17605. DBGLOG(TEST, SOAP_MESSAGE(fdebug,
  17606. "fmimeread failed: insufficient data (%lu bytes remaining from %lu bytes)\n",
  17607. (unsigned long) size, (unsigned long) content->size));
  17608. soap->error = SOAP_EOF;
  17609. break;
  17610. }
  17611. if (soap_send_raw(soap, soap->tmpbuf, bufsize))
  17612. break;
  17613. size -= bufsize;
  17614. } while (size);
  17615. }
  17616. if (soap->fmimereadclose)
  17617. soap->fmimereadclose(soap, handle);
  17618. } else {
  17619. if (soap_putmimehdr(soap, content)
  17620. || soap_send_raw(soap, content->ptr, content->size))
  17621. return soap->error;
  17622. }
  17623. }
  17624. return soap_send3(soap, "\r\n--", soap->mime.boundary, "--");
  17625. }
  17626. #endif
  17627. /******************************************************************************/
  17628. #ifndef WITH_LEANER
  17629. SOAP_FMAC1
  17630. void
  17631. SOAP_FMAC2
  17632. soap_set_dime(struct soap *soap) {
  17633. soap->omode |= SOAP_ENC_DIME;
  17634. soap->dime.first = NULL;
  17635. soap->dime.last = NULL;
  17636. }
  17637. #endif
  17638. /******************************************************************************/
  17639. #ifndef WITH_LEANER
  17640. SOAP_FMAC1
  17641. void
  17642. SOAP_FMAC2
  17643. soap_set_mime(struct soap *soap, const char *boundary, const char *start) {
  17644. soap->omode |= SOAP_ENC_MIME;
  17645. soap->mime.first = NULL;
  17646. soap->mime.last = NULL;
  17647. soap->mime.boundary = soap_strdup(soap, boundary);
  17648. soap->mime.start = soap_strdup(soap, start);
  17649. }
  17650. #endif
  17651. /******************************************************************************/
  17652. #ifndef WITH_LEANER
  17653. SOAP_FMAC1
  17654. void
  17655. SOAP_FMAC2
  17656. soap_clr_dime(struct soap *soap) {
  17657. soap->omode &= ~SOAP_ENC_DIME;
  17658. soap->dime.first = NULL;
  17659. soap->dime.last = NULL;
  17660. }
  17661. #endif
  17662. /******************************************************************************/
  17663. #ifndef WITH_LEANER
  17664. SOAP_FMAC1
  17665. void
  17666. SOAP_FMAC2
  17667. soap_clr_mime(struct soap *soap) {
  17668. soap->omode &= ~SOAP_ENC_MIME;
  17669. soap->mime.first = NULL;
  17670. soap->mime.last = NULL;
  17671. soap->mime.boundary = NULL;
  17672. soap->mime.start = NULL;
  17673. }
  17674. #endif
  17675. /******************************************************************************/
  17676. #ifndef WITH_LEANER
  17677. static int
  17678. soap_begin_attachments(struct soap *soap) {
  17679. if ((soap->mode & SOAP_ENC_MIME) && soap->mime.boundary && soap->mime.start) {
  17680. const char *s;
  17681. if (strlen(soap->mime.boundary) + strlen(soap->mime.start) + 140 > sizeof(soap->tmpbuf))
  17682. return soap->error = SOAP_EOM;
  17683. if ((soap->mode & SOAP_ENC_DIME) && !(soap->mode & SOAP_ENC_MTOM)) {
  17684. s = "application/dime";
  17685. } else if (soap->version == 2) {
  17686. if ((soap->mode & SOAP_ENC_MTOM))
  17687. s = "application/xop+xml; charset=utf-8; type=\"application/soap+xml\"";
  17688. else
  17689. s = "application/soap+xml; charset=utf-8";
  17690. } else if ((soap->mode & SOAP_ENC_MTOM)) {
  17691. s = "application/xop+xml; charset=utf-8; type=\"text/xml\"";
  17692. } else {
  17693. s = "text/xml; charset=utf-8";
  17694. }
  17695. (SOAP_SNPRINTF_SAFE(soap->tmpbuf, sizeof(soap->tmpbuf)),
  17696. "--%s\r\nContent-Type: %s\r\nContent-Transfer-Encoding: binary\r\nContent-ID: %s\r\n\r\n",
  17697. soap->mime.boundary, s, soap->mime.start);
  17698. if (soap_send(soap, soap->tmpbuf))
  17699. return soap->error;
  17700. }
  17701. if ((soap->mode & SOAP_IO_LENGTH))
  17702. soap->dime.size = (size_t) soap->count; /* DIME in MIME correction, soap->count is small */
  17703. if (!(soap->mode & SOAP_IO_LENGTH) && (soap->mode & SOAP_ENC_DIME)) {
  17704. if (soap_putdimehdr(soap))
  17705. return soap->error;
  17706. }
  17707. return SOAP_OK;
  17708. }
  17709. #endif
  17710. /******************************************************************************/
  17711. #ifndef WITH_LEANER
  17712. static int
  17713. soap_end_attachments(struct soap *soap) {
  17714. if ((soap->mode & SOAP_IO_LENGTH) && (soap->mode & SOAP_ENC_DIME) && !(soap->mode & SOAP_ENC_MTOM)) {
  17715. if (soap->count > 0xFFFFFFFF)
  17716. return soap->error = SOAP_DIME_ERROR;
  17717. soap->dime.size = (size_t) soap->count - soap->dime.size; /* DIME in MIME correction */
  17718. (SOAP_SNPRINTF(soap->id, sizeof(soap->id), strlen(soap->dime_id_format) + 20), soap->dime_id_format, 0);
  17719. soap->dime.id = soap->id;
  17720. if (soap->local_namespaces && soap->local_namespaces[0].id) {
  17721. if (soap->local_namespaces[0].out)
  17722. soap->dime.type = (char *) soap->local_namespaces[0].out;
  17723. else
  17724. soap->dime.type = (char *) soap->local_namespaces[0].ns;
  17725. }
  17726. soap->dime.options = NULL;
  17727. soap->dime.flags = SOAP_DIME_MB | SOAP_DIME_ABSURI;
  17728. if (!soap->dime.first)
  17729. soap->dime.flags |= SOAP_DIME_ME;
  17730. soap->count += 12 + ((strlen(soap->dime.id) + 3) & (~3)) +
  17731. (soap->dime.type ? ((strlen(soap->dime.type) + 3) & (~3)) : 0);
  17732. }
  17733. if ((soap->mode & SOAP_ENC_DIME) && !(soap->mode & SOAP_ENC_MTOM))
  17734. return soap_send_raw(soap, SOAP_STR_PADDING, -(long) soap->dime.size & 3);
  17735. return SOAP_OK;
  17736. }
  17737. #endif
  17738. /******************************************************************************/
  17739. #ifndef WITH_LEANER
  17740. static struct soap_multipart *
  17741. soap_alloc_multipart(struct soap *soap, struct soap_multipart **first, struct soap_multipart **last, const char *ptr,
  17742. size_t size) {
  17743. struct soap_multipart *content;
  17744. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "New DIME/MIME attachment %p (%lu)\n", (void *) ptr, (unsigned long) size));
  17745. content = (struct soap_multipart *) soap_malloc(soap, sizeof(struct soap_multipart));
  17746. if (content) {
  17747. content->next = NULL;
  17748. content->ptr = ptr;
  17749. content->size = size;
  17750. content->id = NULL;
  17751. content->type = NULL;
  17752. content->options = NULL;
  17753. content->encoding = SOAP_MIME_NONE;
  17754. content->location = NULL;
  17755. content->description = NULL;
  17756. if (!*first)
  17757. *first = content;
  17758. if (*last)
  17759. (*last)->next = content;
  17760. *last = content;
  17761. }
  17762. return content;
  17763. }
  17764. #endif
  17765. /******************************************************************************/
  17766. #ifndef WITH_LEANER
  17767. SOAP_FMAC1
  17768. int
  17769. SOAP_FMAC2
  17770. soap_set_dime_attachment(struct soap *soap, const char *ptr, size_t size, const char *type, const char *id,
  17771. unsigned short optype, const char *option) {
  17772. struct soap_multipart *content = soap_alloc_multipart(soap, &soap->dime.first, &soap->dime.last, ptr, size);
  17773. if (!content)
  17774. return SOAP_EOM;
  17775. content->id = soap_strdup(soap, id);
  17776. content->type = soap_strdup(soap, type);
  17777. content->options = soap_dime_option(soap, optype, option);
  17778. return SOAP_OK;
  17779. }
  17780. #endif
  17781. /******************************************************************************/
  17782. #ifndef WITH_LEANER
  17783. SOAP_FMAC1
  17784. int
  17785. SOAP_FMAC2
  17786. soap_set_mime_attachment(struct soap *soap, const char *ptr, size_t size, enum soap_mime_encoding encoding,
  17787. const char *type, const char *id, const char *location, const char *description) {
  17788. struct soap_multipart *content = soap_alloc_multipart(soap, &soap->mime.first, &soap->mime.last, ptr, size);
  17789. if (!content)
  17790. return SOAP_EOM;
  17791. content->id = soap_strdup(soap, id);
  17792. content->type = soap_strdup(soap, type);
  17793. content->encoding = encoding;
  17794. content->location = soap_strdup(soap, location);
  17795. content->description = soap_strdup(soap, description);
  17796. return SOAP_OK;
  17797. }
  17798. #endif
  17799. /******************************************************************************/
  17800. #ifndef WITH_LEANER
  17801. SOAP_FMAC1
  17802. struct soap_multipart *
  17803. SOAP_FMAC2
  17804. soap_next_multipart(struct soap_multipart *content) {
  17805. if (content)
  17806. return content->next;
  17807. return NULL;
  17808. }
  17809. #endif
  17810. /******************************************************************************/
  17811. #ifndef WITH_LEANER
  17812. static void
  17813. soap_select_mime_boundary(struct soap *soap) {
  17814. while (!soap->mime.boundary || soap_valid_mime_boundary(soap)) {
  17815. char *s = soap->mime.boundary;
  17816. size_t n = 0;
  17817. if (s)
  17818. n = strlen(s);
  17819. if (n < 16) {
  17820. n = 64;
  17821. s = soap->mime.boundary = (char *) soap_malloc(soap, n + 1);
  17822. if (!s)
  17823. return;
  17824. }
  17825. *s++ = '=';
  17826. *s++ = '=';
  17827. n -= 4;
  17828. while (n) {
  17829. *s++ = soap_base64o[soap_random & 0x3F];
  17830. n--;
  17831. }
  17832. *s++ = '=';
  17833. *s++ = '=';
  17834. *s = '\0';
  17835. }
  17836. if (!soap->mime.start)
  17837. soap->mime.start = "<SOAP-ENV:Envelope>";
  17838. }
  17839. #endif
  17840. /******************************************************************************/
  17841. #ifndef WITH_LEANER
  17842. static int
  17843. soap_valid_mime_boundary(struct soap *soap) {
  17844. struct soap_multipart *content;
  17845. size_t k;
  17846. if (soap->fmimeread)
  17847. return SOAP_OK;
  17848. k = strlen(soap->mime.boundary);
  17849. for (content = soap->mime.first; content; content = content->next) {
  17850. if (content->ptr && content->size >= k) {
  17851. const char *p = (const char *) content->ptr;
  17852. size_t i;
  17853. for (i = 0; i < content->size - k; i++, p++) {
  17854. if (!strncmp(p, soap->mime.boundary, k))
  17855. return SOAP_ERR;
  17856. }
  17857. }
  17858. }
  17859. return SOAP_OK;
  17860. }
  17861. #endif
  17862. /******************************************************************************/
  17863. #ifdef WITH_GZIP
  17864. static int
  17865. soap_getgziphdr(struct soap *soap)
  17866. {
  17867. int i;
  17868. soap_wchar c = 0, f = 0;
  17869. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Get gzip header\n"));
  17870. for (i = 0; i < 9; i++)
  17871. {
  17872. c = soap_get1(soap);
  17873. if (i == 1 && c == 8)
  17874. soap->z_dict = 0;
  17875. if (i == 2)
  17876. f = c;
  17877. }
  17878. if (f & 0x04) /* FEXTRA */
  17879. {
  17880. i = soap_get1(soap);
  17881. i |= soap_get1(soap) << 8;
  17882. while (i-- > 0)
  17883. {
  17884. if ((int)soap_get1(soap) == EOF)
  17885. return soap->error = SOAP_ZLIB_ERROR;
  17886. }
  17887. }
  17888. if (f & 0x08) /* skip FNAME */
  17889. {
  17890. do
  17891. {
  17892. c = soap_get1(soap);
  17893. } while (c && (int)c != EOF);
  17894. }
  17895. if ((int)c != EOF && (f & 0x10)) /* skip FCOMMENT */
  17896. {
  17897. do
  17898. {
  17899. c = soap_get1(soap);
  17900. } while (c && (int)c != EOF);
  17901. }
  17902. if ((int)c != EOF && (f & 0x02)) /* skip FHCRC (CRC32 is used) */
  17903. {
  17904. c = soap_get1(soap);
  17905. if ((int)c != EOF)
  17906. c = soap_get1(soap);
  17907. }
  17908. if ((int)c == EOF)
  17909. return soap->error = SOAP_ZLIB_ERROR;
  17910. return SOAP_OK;
  17911. }
  17912. #endif
  17913. /******************************************************************************/
  17914. SOAP_FMAC1
  17915. int
  17916. SOAP_FMAC2
  17917. soap_begin_serve(struct soap *soap) {
  17918. #ifdef WITH_FASTCGI
  17919. if (FCGI_Accept() < 0)
  17920. {
  17921. soap->error = SOAP_EOF;
  17922. return soap_send_fault(soap);
  17923. }
  17924. #endif
  17925. soap_begin(soap);
  17926. if (soap_begin_recv(soap)
  17927. || soap_envelope_begin_in(soap)
  17928. || soap_recv_header(soap)
  17929. || soap_body_begin_in(soap)) {
  17930. if (soap->error < SOAP_STOP) {
  17931. #ifdef WITH_FASTCGI
  17932. (void)soap_send_fault(soap);
  17933. #else
  17934. return soap_send_fault(soap);
  17935. #endif
  17936. }
  17937. return soap_closesock(soap);
  17938. }
  17939. return SOAP_OK;
  17940. }
  17941. /******************************************************************************/
  17942. SOAP_FMAC1
  17943. int
  17944. SOAP_FMAC2
  17945. soap_begin_recv(struct soap *soap) {
  17946. // std::cerr<<"Recieved data Sassan2: "<<soap_getchar(soap)<<std::endl;
  17947. soap_wchar c;
  17948. DBGLOG(TEST,
  17949. SOAP_MESSAGE(fdebug, "Initializing for input from socket=%d/fd=%d\n", (int) soap->socket, soap->recvfd));
  17950. soap->error = SOAP_OK;
  17951. #ifndef WITH_LEANER
  17952. soap->recverror = SOAP_OK;
  17953. #endif
  17954. soap_free_temp(soap);
  17955. soap_set_local_namespaces(soap);
  17956. soap->version = 0; /* don't assume we're parsing SOAP content by default */
  17957. #ifndef WITH_NOIDREF
  17958. soap_free_iht(soap);
  17959. #endif
  17960. if ((soap->imode & SOAP_IO) == SOAP_IO_CHUNK) {
  17961. soap->imode &= ~SOAP_IO;
  17962. soap->omode = (soap->omode & ~SOAP_IO) | SOAP_IO_CHUNK;
  17963. }
  17964. soap->imode &= ~(SOAP_ENC_DIME | SOAP_ENC_MIME | SOAP_ENC_MTOM | SOAP_ENC_ZLIB);
  17965. soap->mode = soap->imode;
  17966. if (!(soap->mode & SOAP_IO_KEEPALIVE))
  17967. soap->keep_alive = 0;
  17968. if (!soap->keep_alive)
  17969. soap->buflen = soap->bufidx = 0;
  17970. soap->null = 0;
  17971. soap->position = 0;
  17972. soap->mustUnderstand = 0;
  17973. soap->shaky = 0;
  17974. soap->ahead = 0;
  17975. soap->peeked = 0;
  17976. soap->level = 0;
  17977. soap->part = SOAP_BEGIN_RECV;
  17978. soap->count = 0;
  17979. soap->length = 0;
  17980. soap->cdata = 0;
  17981. *soap->endpoint = '\0';
  17982. soap->action = NULL;
  17983. soap->header = NULL;
  17984. soap->fault = NULL;
  17985. soap->status = 0;
  17986. soap->fform = NULL;
  17987. soap->body = 1;
  17988. #ifndef WITH_LEANER
  17989. soap->dom = NULL;
  17990. soap->dime.count = 0;
  17991. soap->dime.chunksize = 0;
  17992. soap->dime.buflen = 0;
  17993. soap->dime.list = NULL;
  17994. soap->dime.first = NULL;
  17995. soap->dime.last = NULL;
  17996. soap->mime.list = NULL;
  17997. soap->mime.first = NULL;
  17998. soap->mime.last = NULL;
  17999. soap->mime.boundary = NULL;
  18000. soap->mime.start = NULL;
  18001. #endif
  18002. #ifdef WIN32
  18003. #ifndef UNDER_CE
  18004. #ifndef WITH_FASTCGI
  18005. if (!soap_valid_socket(soap->socket) && !soap->is && soap->recvfd >= 0) /* Set win32 stdin or soap->recvfd to BINARY, e.g. to support DIME */
  18006. #ifdef __BORLANDC__
  18007. setmode(soap->recvfd, _O_BINARY);
  18008. #else
  18009. _setmode(soap->recvfd, _O_BINARY);
  18010. #endif
  18011. #endif
  18012. #endif
  18013. #endif
  18014. #ifdef WITH_ZLIB
  18015. soap->zlib_in = SOAP_ZLIB_NONE;
  18016. soap->zlib_out = SOAP_ZLIB_NONE;
  18017. if (!soap->d_stream)
  18018. {
  18019. soap->d_stream = (z_stream*)SOAP_MALLOC(soap, sizeof(z_stream));
  18020. if (!soap->d_stream)
  18021. return soap->error = SOAP_EOM;
  18022. soap->d_stream->zalloc = Z_NULL;
  18023. soap->d_stream->zfree = Z_NULL;
  18024. soap->d_stream->opaque = Z_NULL;
  18025. soap->d_stream->next_in = Z_NULL;
  18026. soap->d_stream->msg = Z_NULL;
  18027. }
  18028. soap->d_stream->avail_in = 0;
  18029. soap->d_stream->next_out = (Byte*)soap->buf;
  18030. soap->d_stream->avail_out = sizeof(soap->buf);
  18031. soap->z_ratio_in = 1.0;
  18032. #endif
  18033. #ifdef WITH_OPENSSL
  18034. if (soap->ssl)
  18035. ERR_clear_error();
  18036. #endif
  18037. #ifndef WITH_LEAN
  18038. soap->start = (ULONG64) time(NULL);
  18039. #endif
  18040. #ifndef WITH_LEANER
  18041. if (soap->fprepareinitrecv && (soap->error = soap->fprepareinitrecv(soap)) != SOAP_OK)
  18042. return soap->error;
  18043. #endif
  18044. c = soap_getchar(soap);
  18045. #ifdef WITH_GZIP
  18046. if (c == 0x1F)
  18047. {
  18048. if (soap_getgziphdr(soap))
  18049. return soap->error;
  18050. if (inflateInit2(soap->d_stream, -MAX_WBITS) != Z_OK)
  18051. return soap->error = SOAP_ZLIB_ERROR;
  18052. if (soap->z_dict)
  18053. {
  18054. if (inflateSetDictionary(soap->d_stream, (const Bytef*)soap->z_dict, soap->z_dict_len) != Z_OK)
  18055. return soap->error = SOAP_ZLIB_ERROR;
  18056. }
  18057. soap->zlib_state = SOAP_ZLIB_INFLATE;
  18058. soap->mode |= SOAP_ENC_ZLIB;
  18059. soap->zlib_in = SOAP_ZLIB_GZIP;
  18060. soap->z_crc = crc32(0L, NULL, 0);
  18061. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "gzip initialized\n"));
  18062. if (!soap->z_buf)
  18063. soap->z_buf = (char*)SOAP_MALLOC(soap, sizeof(soap->buf));
  18064. (void)soap_memcpy((void*)soap->z_buf, sizeof(soap->buf), (const void*)soap->buf, sizeof(soap->buf));
  18065. /* should not chunk over plain transport, so why bother to check? */
  18066. /* if ((soap->mode & SOAP_IO) == SOAP_IO_CHUNK) */
  18067. /* soap->z_buflen = soap->bufidx; */
  18068. /* else */
  18069. soap->d_stream->next_in = (Byte*)(soap->z_buf + soap->bufidx);
  18070. soap->d_stream->avail_in = (unsigned int)(soap->buflen - soap->bufidx);
  18071. soap->z_buflen = soap->buflen;
  18072. soap->buflen = soap->bufidx;
  18073. c = ' ';
  18074. }
  18075. #endif
  18076. while (soap_coblank(c))
  18077. c = soap_getchar(soap);
  18078. #ifndef WITH_LEANER
  18079. if (c == '-' && soap_get0(soap) == '-') {
  18080. soap->mode |= SOAP_ENC_MIME;
  18081. } else if ((c & 0xFFFC) == (SOAP_DIME_VERSION | SOAP_DIME_MB) && (soap_get0(soap) & 0xFFF0) == 0x20) {
  18082. soap->mode |= SOAP_ENC_DIME;
  18083. } else
  18084. #endif
  18085. {
  18086. /* skip BOM */
  18087. if (c == 0xEF && soap_get0(soap) == 0xBB) {
  18088. soap_get1(soap);
  18089. c = soap_get1(soap);
  18090. if (c == 0xBF) {
  18091. soap->mode &= ~SOAP_ENC_LATIN;
  18092. c = soap_getchar(soap);
  18093. } else {
  18094. c = (0x0F << 12) | (0xBB << 6) | (c & 0x3F); /* UTF-8 */
  18095. }
  18096. } else if ((c == 0xFE && soap_get0(soap) == 0xFF) /* UTF-16 BE */
  18097. || (c == 0xFF && soap_get0(soap) == 0xFE)) /* UTF-16 LE */
  18098. {
  18099. return soap->error = SOAP_UTF_ERROR;
  18100. }
  18101. /* skip space */
  18102. while (soap_coblank(c))
  18103. c = soap_getchar(soap);
  18104. }
  18105. if ((int) c == EOF)
  18106. return soap->error = SOAP_CHK_EOF;
  18107. soap_unget(soap, c);
  18108. #ifndef WITH_NOHTTP
  18109. /* if not XML/MIME/DIME/ZLIB, assume HTTP method or status line */
  18110. if (((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')) &&
  18111. !(soap->mode & (SOAP_ENC_MIME | SOAP_ENC_DIME | SOAP_ENC_ZLIB | SOAP_ENC_PLAIN))) {
  18112. soap_mode m = soap->imode;
  18113. soap->error = soap->fparse(soap);
  18114. soap->mode = soap->imode; /* if imode is changed, effectuate */
  18115. soap->imode = m; /* restore imode */
  18116. if ((soap->mode & SOAP_IO) == SOAP_IO_CHUNK) {
  18117. soap->chunkbuflen = soap->buflen;
  18118. soap->buflen = soap->bufidx;
  18119. soap->chunksize = 0;
  18120. }
  18121. #ifdef WITH_ZLIB
  18122. soap->mode &= ~SOAP_ENC_ZLIB;
  18123. if (soap->zlib_in != SOAP_ZLIB_NONE)
  18124. {
  18125. #ifdef WITH_GZIP
  18126. if (soap->zlib_in != SOAP_ZLIB_DEFLATE)
  18127. {
  18128. c = soap_get1(soap);
  18129. if (c == (int)EOF)
  18130. return soap->error = SOAP_EOF;
  18131. if (c == 0x1F)
  18132. {
  18133. if (soap_getgziphdr(soap))
  18134. return soap->error;
  18135. if (inflateInit2(soap->d_stream, -MAX_WBITS) != Z_OK)
  18136. return soap->error = SOAP_ZLIB_ERROR;
  18137. soap->z_crc = crc32(0L, NULL, 0);
  18138. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "gzip initialized\n"));
  18139. }
  18140. else
  18141. {
  18142. soap_revget1(soap);
  18143. if (inflateInit(soap->d_stream) != Z_OK)
  18144. return soap->error = SOAP_ZLIB_ERROR;
  18145. soap->zlib_in = SOAP_ZLIB_DEFLATE;
  18146. }
  18147. }
  18148. else
  18149. #endif
  18150. if (inflateInit(soap->d_stream) != Z_OK)
  18151. return soap->error = SOAP_ZLIB_ERROR;
  18152. if (soap->z_dict)
  18153. {
  18154. if (inflateSetDictionary(soap->d_stream, (const Bytef*)soap->z_dict, soap->z_dict_len) != Z_OK)
  18155. return soap->error = SOAP_ZLIB_ERROR;
  18156. }
  18157. soap->zlib_state = SOAP_ZLIB_INFLATE;
  18158. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Inflate initialized\n"));
  18159. soap->mode |= SOAP_ENC_ZLIB;
  18160. if (!soap->z_buf)
  18161. soap->z_buf = (char*)SOAP_MALLOC(soap, sizeof(soap->buf));
  18162. (void)soap_memcpy((void*)soap->z_buf, sizeof(soap->buf), (const void*)soap->buf, sizeof(soap->buf));
  18163. soap->d_stream->next_in = (Byte*)(soap->z_buf + soap->bufidx);
  18164. soap->d_stream->avail_in = (unsigned int)(soap->buflen - soap->bufidx);
  18165. soap->z_buflen = soap->buflen;
  18166. soap->buflen = soap->bufidx;
  18167. }
  18168. #endif
  18169. #ifndef WITH_LEANER
  18170. if (soap->fpreparerecv && (soap->mode & SOAP_IO) != SOAP_IO_CHUNK && soap->buflen > soap->bufidx) {
  18171. int r;
  18172. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Invoking fpreparerecv\n"));
  18173. r = soap->fpreparerecv(soap, soap->buf + soap->bufidx, soap->buflen - soap->bufidx);
  18174. if (r)
  18175. return soap->error = r;
  18176. }
  18177. #endif
  18178. if (soap->error && soap->error < SOAP_STOP) {
  18179. if (soap->status >= 200 && soap->status < 600) {
  18180. const char *s = soap_http_get_body(soap, NULL);
  18181. (void) soap_end_recv(soap);
  18182. if (soap->status >= 300)
  18183. soap->keep_alive = 0; /* to force close */
  18184. return soap_set_receiver_error(soap, "HTTP Error", s, soap->status);
  18185. }
  18186. return soap->error;
  18187. }
  18188. if (!soap->body && soap->status >= 200 && soap->status < 600)
  18189. return soap->error = soap->status; /* client side received HTTP status code */
  18190. if (soap->status > SOAP_POST) {
  18191. soap->fform = NULL;
  18192. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Invoking http method handler\n"));
  18193. switch (soap->status) {
  18194. case SOAP_GET:
  18195. if (soap_http_skip_body(soap) || soap_end_recv(soap))
  18196. return soap->error;
  18197. soap->error = soap->fget(soap);
  18198. break;
  18199. case SOAP_PUT:
  18200. soap->error = soap->fput(soap);
  18201. break;
  18202. case SOAP_PATCH:
  18203. soap->error = soap->fpatch(soap);
  18204. break;
  18205. case SOAP_DEL:
  18206. if (soap_http_skip_body(soap) || soap_end_recv(soap))
  18207. return soap->error;
  18208. soap->error = soap->fdel(soap);
  18209. break;
  18210. case SOAP_HEAD:
  18211. if (soap_http_skip_body(soap) || soap_end_recv(soap))
  18212. return soap->error;
  18213. soap->error = soap->fhead(soap);
  18214. break;
  18215. case SOAP_OPTIONS:
  18216. if (soap_http_skip_body(soap) || soap_end_recv(soap))
  18217. return soap->error;
  18218. soap->error = soap->fopt(soap);
  18219. break;
  18220. default:
  18221. if (soap_http_skip_body(soap) || soap_end_recv(soap))
  18222. return soap->error;
  18223. return 405;
  18224. }
  18225. if (soap->error == SOAP_OK)
  18226. return soap->error = SOAP_STOP; /* prevents further processing */
  18227. if (soap->error != SOAP_FORM || !soap->fform) /* continue if handler returned SOAP_FORM */
  18228. return soap->error;
  18229. soap->error = SOAP_OK;
  18230. }
  18231. if (soap->fform) {
  18232. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Invoking http form handler\n"));
  18233. soap->error = soap->fform(soap);
  18234. if (soap->error == SOAP_OK)
  18235. return soap->error = SOAP_STOP; /* prevents further processing */
  18236. if (soap->status != SOAP_POST ||
  18237. soap->error != 404) /* continue with POST if handler returned HTTP not found */
  18238. return soap->error;
  18239. soap->error = SOAP_OK;
  18240. }
  18241. if (!soap->body)//Sassan No_Data
  18242. {
  18243. std::cout << "No Content Recieved at " << time(NULL) << " for " << soap->host <<" Calling CheckQueue..."<< std::endl;
  18244. return soap->error =20400 ;
  18245. }
  18246. }
  18247. #endif
  18248. #ifndef WITH_LEANER
  18249. if ((soap->mode & SOAP_ENC_MIME)) {
  18250. do /* skip preamble */
  18251. {
  18252. c = soap_getchar(soap);
  18253. if ((int) c == EOF)
  18254. return soap->error = SOAP_CHK_EOF;
  18255. } while (c != '-' || soap_get0(soap) != '-');
  18256. soap_unget(soap, c);
  18257. if (soap_getmimehdr(soap))
  18258. return soap->error;
  18259. if (soap->mime.start) {
  18260. do {
  18261. if (!soap->mime.last->id)
  18262. break;
  18263. if (!soap_match_cid(soap, soap->mime.start, soap->mime.last->id))
  18264. break;
  18265. } while (soap_recv_mime_attachment(soap, NULL));
  18266. }
  18267. if (soap_http_header_attribute(soap, soap->mime.first->type, "application/dime"))
  18268. soap->mode |= SOAP_ENC_DIME;
  18269. }
  18270. if ((soap->mode & SOAP_ENC_DIME)) {
  18271. if (soap_getdimehdr(soap))
  18272. return soap->error;
  18273. if ((soap->dime.flags & SOAP_DIME_CF)) {
  18274. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Chunked SOAP in DIME message\n"));
  18275. soap->dime.chunksize = soap->dime.size;
  18276. if (soap->buflen - soap->bufidx >= soap->dime.chunksize) {
  18277. soap->dime.buflen = soap->buflen;
  18278. soap->buflen = soap->bufidx + soap->dime.chunksize;
  18279. } else {
  18280. soap->dime.chunksize -= soap->buflen - soap->bufidx;
  18281. }
  18282. }
  18283. soap->count = soap->buflen - soap->bufidx;
  18284. if (soap->recv_maxlength && soap->count > soap->recv_maxlength)
  18285. return soap->error = SOAP_EOF;
  18286. }
  18287. #endif
  18288. return SOAP_OK;
  18289. }
  18290. /******************************************************************************/
  18291. SOAP_FMAC1
  18292. int
  18293. SOAP_FMAC2
  18294. soap_envelope_begin_out(struct soap *soap) {
  18295. if (soap->version == 0)
  18296. return SOAP_OK;
  18297. soap->part = SOAP_IN_ENVELOPE;
  18298. return soap_element_begin_out(soap, "SOAP-ENV:Envelope", 0, NULL);
  18299. }
  18300. /******************************************************************************/
  18301. SOAP_FMAC1
  18302. int
  18303. SOAP_FMAC2
  18304. soap_envelope_end_out(struct soap *soap) {
  18305. if (soap->version == 0)
  18306. return SOAP_OK;
  18307. if (soap_element_end_out(soap, "SOAP-ENV:Envelope")
  18308. || soap_send_raw(soap, "\r\n", 2)) /* 2.8: always emit \r\n */
  18309. return soap->error;
  18310. soap->part = SOAP_END_ENVELOPE;
  18311. return SOAP_OK;
  18312. }
  18313. /******************************************************************************/
  18314. SOAP_FMAC1
  18315. int
  18316. SOAP_FMAC2
  18317. soap_http_has_body(struct soap *soap) {
  18318. return soap->length || (soap->mode & SOAP_ENC_ZLIB) || (soap->mode & SOAP_IO) == SOAP_IO_CHUNK;
  18319. }
  18320. /******************************************************************************/
  18321. SOAP_FMAC1
  18322. int
  18323. SOAP_FMAC2
  18324. soap_http_skip_body(struct soap *soap) {
  18325. ULONG64 k = soap->length;
  18326. /* check HTTP body, return "" if none */
  18327. if (!k && !(soap->mode & SOAP_ENC_ZLIB) && (soap->mode & SOAP_IO) != SOAP_IO_CHUNK)
  18328. return SOAP_OK;
  18329. /* do not consume DIME or MIME attachments, leave this to soap_end_recv */
  18330. if ((soap->mode & SOAP_ENC_DIME) || (soap->mode & SOAP_ENC_MIME))
  18331. return SOAP_OK;
  18332. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Skipping HTTP body (mode=0x%x)\n", soap->mode));
  18333. if (k && !(soap->mode & SOAP_ENC_ZLIB)) {
  18334. size_t i;
  18335. soap->length = 0;
  18336. for (i = 0; i < k; i++) {
  18337. soap_wchar c = soap_get1(soap);
  18338. if ((int) c == EOF)
  18339. break;
  18340. }
  18341. } else {
  18342. for (;;) {
  18343. soap_wchar c = soap_get1(soap);
  18344. if ((int) c == EOF)
  18345. break;
  18346. }
  18347. }
  18348. return SOAP_OK;
  18349. }
  18350. /******************************************************************************/
  18351. SOAP_FMAC1
  18352. char *
  18353. SOAP_FMAC2
  18354. soap_http_get_body(struct soap *soap, size_t *len) {
  18355. return soap_http_get_body_prefix(soap, len, NULL);
  18356. }
  18357. /******************************************************************************/
  18358. SOAP_FMAC1
  18359. char *
  18360. SOAP_FMAC2
  18361. soap_http_get_form(struct soap *soap) {
  18362. return soap_http_get_body_prefix(soap, NULL, "?");
  18363. }
  18364. /******************************************************************************/
  18365. SOAP_FMAC1
  18366. char *
  18367. SOAP_FMAC2
  18368. soap_http_get_body_prefix(struct soap *soap, size_t *len, const char *prefix) {
  18369. char *s;
  18370. ULONG64 k = soap->length;
  18371. size_t n = 0;
  18372. if (!prefix)
  18373. prefix = SOAP_STR_EOS;
  18374. else
  18375. n = strlen(prefix);
  18376. if (len)
  18377. *len = 0;
  18378. /* check HTTP body, return "" if none */
  18379. if (!k && !(soap->mode & SOAP_ENC_ZLIB) && (soap->mode & SOAP_IO) != SOAP_IO_CHUNK)
  18380. return soap_strdup(soap, prefix);
  18381. /* do not consume DIME or MIME attachments, leave this to soap_end_recv */
  18382. if ((soap->mode & SOAP_ENC_DIME) || (soap->mode & SOAP_ENC_MIME))
  18383. return soap_strdup(soap, prefix);
  18384. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Parsing HTTP body, prefixed with '%s' (mode=0x%x)\n", prefix, soap->mode));
  18385. if (k && !(soap->mode & SOAP_ENC_ZLIB)) {
  18386. char *t;
  18387. soap->length = 0;
  18388. /* http content length != 0 and uncompressed body */
  18389. if ((SOAP_MAXALLOCSIZE != 0 && n + k > SOAP_MAXALLOCSIZE) || n + k > (ULONG64) ((size_t) -2)) {
  18390. soap->error = SOAP_EOM;
  18391. return NULL;
  18392. }
  18393. s = t = (char *) soap_malloc(soap, (size_t) k + n + 1);
  18394. if (s) {
  18395. size_t i;
  18396. soap_strcpy(t, n + 1, prefix);
  18397. t += n;
  18398. for (i = 0; i < k; i++) {
  18399. soap_wchar c = soap_get1(soap);
  18400. if ((int) c == EOF)
  18401. break;
  18402. *t++ = (char) (c & 0xFF);
  18403. }
  18404. *t = '\0';
  18405. if (len)
  18406. *len = n + i;
  18407. } else {
  18408. soap->error = SOAP_EOM;
  18409. return NULL;
  18410. }
  18411. } else {
  18412. size_t i, l = 0;
  18413. if (soap_alloc_block(soap) == NULL)
  18414. return NULL;
  18415. if (n) {
  18416. s = (char *) soap_push_block(soap, NULL, n);
  18417. if (!s)
  18418. return NULL;
  18419. soap_strcpy(s, n + 1, prefix);
  18420. l += n;
  18421. }
  18422. for (;;) {
  18423. size_t k = SOAP_BLKLEN;
  18424. s = (char *) soap_push_block(soap, NULL, k);
  18425. if (!s)
  18426. return NULL;
  18427. for (i = 0; i < k; i++) {
  18428. soap_wchar c;
  18429. l++;
  18430. if (l == 0) {
  18431. soap->error = SOAP_EOM;
  18432. return NULL;
  18433. }
  18434. c = soap_get1(soap);
  18435. if ((int) c == EOF)
  18436. goto end;
  18437. *s++ = (char) (c & 0xFF);
  18438. }
  18439. }
  18440. end:
  18441. *s = '\0';
  18442. if (len)
  18443. *len = l - 1;
  18444. soap_size_block(soap, NULL, i + 1);
  18445. s = soap_save_block(soap, NULL, NULL, 0);
  18446. }
  18447. return s;
  18448. }
  18449. /******************************************************************************/
  18450. SOAP_FMAC1
  18451. int
  18452. SOAP_FMAC2
  18453. soap_envelope_begin_in(struct soap *soap) {
  18454. soap->part = SOAP_IN_ENVELOPE;
  18455. if (soap_element_begin_in(soap, "SOAP-ENV:Envelope", 0, NULL)) {
  18456. if (soap->error == SOAP_TAG_MISMATCH) {
  18457. if (!soap_element_begin_in(soap, "Envelope", 0, NULL))
  18458. soap->error = SOAP_VERSIONMISMATCH;
  18459. else if (soap->status == 0
  18460. || (soap->status >= 200 && soap->status <= 299)
  18461. || soap->status == 400
  18462. || soap->status == 500)
  18463. return SOAP_OK; /* allow non-SOAP (REST) XML content to be captured */
  18464. soap->error = soap->status;
  18465. } else if (soap->status) {
  18466. soap->error = soap->status;
  18467. }
  18468. return soap->error;
  18469. }
  18470. soap_version(soap);
  18471. return SOAP_OK;
  18472. }
  18473. /******************************************************************************/
  18474. SOAP_FMAC1
  18475. int
  18476. SOAP_FMAC2
  18477. soap_envelope_end_in(struct soap *soap) {
  18478. if (soap->version == 0)
  18479. return SOAP_OK;
  18480. soap->part = SOAP_END_ENVELOPE;
  18481. return soap_element_end_in(soap, "SOAP-ENV:Envelope");
  18482. }
  18483. /******************************************************************************/
  18484. SOAP_FMAC1
  18485. int
  18486. SOAP_FMAC2
  18487. soap_body_begin_out(struct soap *soap) {
  18488. if (soap->version == 1)
  18489. soap->encoding = 1;
  18490. #ifndef WITH_LEAN
  18491. if ((soap->mode & SOAP_SEC_WSUID) && soap_set_attr(soap, "wsu:Id", "Body", 1))
  18492. return soap->error;
  18493. #endif
  18494. if (soap->version == 0)
  18495. return SOAP_OK;
  18496. soap->part = SOAP_IN_BODY;
  18497. return soap_element_begin_out(soap, "SOAP-ENV:Body", 0, NULL);
  18498. }
  18499. /******************************************************************************/
  18500. SOAP_FMAC1
  18501. int
  18502. SOAP_FMAC2
  18503. soap_body_end_out(struct soap *soap) {
  18504. if (soap->version == 0)
  18505. return SOAP_OK;
  18506. if (soap_element_end_out(soap, "SOAP-ENV:Body"))
  18507. return soap->error;
  18508. soap->part = SOAP_END_BODY;
  18509. return SOAP_OK;
  18510. }
  18511. /******************************************************************************/
  18512. SOAP_FMAC1
  18513. int
  18514. SOAP_FMAC2
  18515. soap_body_begin_in(struct soap *soap) {
  18516. if (soap->version == 0)
  18517. return SOAP_OK;
  18518. soap->part = SOAP_IN_BODY;
  18519. if (soap_element_begin_in(soap, "SOAP-ENV:Body", 0, NULL))
  18520. return soap->error;
  18521. if (!soap->body) {
  18522. return SOAP_NO_DATA;
  18523. }
  18524. return SOAP_OK;
  18525. }
  18526. /******************************************************************************/
  18527. SOAP_FMAC1
  18528. int
  18529. SOAP_FMAC2
  18530. soap_body_end_in(struct soap *soap) {
  18531. if (soap->version == 0)
  18532. return SOAP_OK;
  18533. if (soap->part == SOAP_NO_BODY)
  18534. return soap->error = SOAP_OK;
  18535. soap->part = SOAP_END_BODY;
  18536. return soap_element_end_in(soap, "SOAP-ENV:Body");
  18537. }
  18538. /******************************************************************************/
  18539. SOAP_FMAC1
  18540. int
  18541. SOAP_FMAC2
  18542. soap_recv_header(struct soap *soap) {
  18543. if (soap_getheader(soap) && soap->error == SOAP_TAG_MISMATCH)
  18544. soap->error = SOAP_OK;
  18545. if (soap->error == SOAP_OK && soap->fheader)
  18546. soap->error = soap->fheader(soap);
  18547. return soap->error;
  18548. }
  18549. /******************************************************************************/
  18550. SOAP_FMAC1
  18551. void
  18552. SOAP_FMAC2
  18553. soap_set_endpoint(struct soap *soap, const char *endpoint) {
  18554. const char *s, *t;
  18555. size_t i, n;
  18556. soap->endpoint[0] = '\0';
  18557. soap->host[0] = '\0';
  18558. soap->path[0] = '/';
  18559. soap->path[1] = '\0';
  18560. soap->port = 80;
  18561. if (!endpoint || !*endpoint)
  18562. return;
  18563. #ifdef WITH_OPENSSL
  18564. if (!soap_tag_cmp(endpoint, "https:*"))
  18565. soap->port = 443;
  18566. #endif
  18567. soap_strcpy(soap->endpoint, sizeof(soap->endpoint), endpoint);
  18568. s = strchr(endpoint, ':');
  18569. if (s && s[1] == '/' && s[2] == '/')
  18570. s += 3;
  18571. else
  18572. s = endpoint;
  18573. #if !defined(WITH_NOHTTP) || !defined(WITH_LEANER)
  18574. t = strchr(s, '@');
  18575. if (t && *s != ':' && *s != '@') {
  18576. size_t l = t - s + 1;
  18577. char *r = (char *) soap_malloc(soap, l);
  18578. n = s - endpoint;
  18579. if (r) {
  18580. s = soap_decode(r, l, s, ":@");
  18581. soap->userid = r;
  18582. soap->passwd = SOAP_STR_EOS;
  18583. if (*s == ':') {
  18584. s++;
  18585. if (*s != '@') {
  18586. l = t - s + 1;
  18587. r = r + strlen(r) + 1;
  18588. s = soap_decode(r, l, s, "@");
  18589. soap->passwd = r;
  18590. }
  18591. }
  18592. }
  18593. s++;
  18594. soap_strcpy(soap->endpoint + n, sizeof(soap->endpoint) - n, s);
  18595. }
  18596. #endif
  18597. n = strlen(s);
  18598. if (n >= sizeof(soap->host))
  18599. n = sizeof(soap->host) - 1;
  18600. #ifdef WITH_IPV6
  18601. if (s[0] == '[')
  18602. {
  18603. s++;
  18604. for (i = 0; i < n; i++)
  18605. {
  18606. if (s[i] == ']')
  18607. {
  18608. s++;
  18609. --n;
  18610. break;
  18611. }
  18612. soap->host[i] = s[i];
  18613. }
  18614. }
  18615. else
  18616. {
  18617. for (i = 0; i < n; i++)
  18618. {
  18619. soap->host[i] = s[i];
  18620. if (s[i] == '/' || s[i] == ':' || s[i] == '?')
  18621. break;
  18622. }
  18623. }
  18624. #else
  18625. for (i = 0; i < n; i++) {
  18626. soap->host[i] = s[i];
  18627. if (s[i] == '/' || s[i] == ':' || s[i] == '?')
  18628. break;
  18629. }
  18630. #endif
  18631. soap->host[i] = '\0';
  18632. if (s[i] == ':') {
  18633. soap->port = (int) soap_strtol(s + i + 1, NULL, 10);
  18634. for (i++; i < n; i++)
  18635. if (s[i] == '/')
  18636. break;
  18637. }
  18638. if (i < n && s[i])
  18639. soap_strcpy(soap->path, sizeof(soap->path), s + i);
  18640. if (soap->override_host && *soap->override_host) {
  18641. soap_strcpy(soap->host, sizeof(soap->host), soap->override_host);
  18642. if (soap->override_port)
  18643. soap->port = soap->override_port;
  18644. }
  18645. if (soap->userid && !soap->authrealm)
  18646. soap->authrealm = soap->host;
  18647. }
  18648. /******************************************************************************/
  18649. #ifndef WITH_NOHTTP
  18650. SOAP_FMAC1
  18651. int
  18652. SOAP_FMAC2
  18653. soap_GET(struct soap *soap, const char *endpoint, const char *action) {
  18654. return soap_connect_command(soap, SOAP_GET, endpoint, action);
  18655. }
  18656. #endif
  18657. /******************************************************************************/
  18658. #ifndef WITH_NOHTTP
  18659. SOAP_FMAC1
  18660. int
  18661. SOAP_FMAC2
  18662. soap_PUT(struct soap *soap, const char *endpoint, const char *action, const char *type) {
  18663. soap->http_content = type;
  18664. if ((soap->omode & SOAP_IO) != SOAP_IO_CHUNK) {
  18665. soap->omode &= ~SOAP_IO;
  18666. soap->omode |= SOAP_IO_STORE;
  18667. }
  18668. return soap_connect_command(soap, SOAP_PUT, endpoint, action);
  18669. }
  18670. #endif
  18671. /******************************************************************************/
  18672. #ifndef WITH_NOHTTP
  18673. SOAP_FMAC1
  18674. int
  18675. SOAP_FMAC2
  18676. soap_POST(struct soap *soap, const char *endpoint, const char *action, const char *type) {
  18677. soap->http_content = type;
  18678. if ((soap->omode & SOAP_IO) != SOAP_IO_CHUNK) {
  18679. soap->omode &= ~SOAP_IO;
  18680. soap->omode |= SOAP_IO_STORE;
  18681. }
  18682. return soap_connect_command(soap, SOAP_POST_FILE, endpoint, action);
  18683. }
  18684. #endif
  18685. /******************************************************************************/
  18686. #ifndef WITH_NOHTTP
  18687. SOAP_FMAC1
  18688. int
  18689. SOAP_FMAC2
  18690. soap_PATCH(struct soap *soap, const char *endpoint, const char *action, const char *type) {
  18691. soap->http_content = type;
  18692. if ((soap->omode & SOAP_IO) != SOAP_IO_CHUNK) {
  18693. soap->omode &= ~SOAP_IO;
  18694. soap->omode |= SOAP_IO_STORE;
  18695. }
  18696. return soap_connect_command(soap, SOAP_PATCH, endpoint, action);
  18697. }
  18698. #endif
  18699. /******************************************************************************/
  18700. #ifndef WITH_NOHTTP
  18701. SOAP_FMAC1
  18702. int
  18703. SOAP_FMAC2
  18704. soap_DELETE(struct soap *soap, const char *endpoint) {
  18705. if (soap_connect_command(soap, SOAP_DEL, endpoint, NULL)
  18706. || soap_recv_empty_response(soap))
  18707. return soap_closesock(soap);
  18708. return SOAP_OK;
  18709. }
  18710. #endif
  18711. /******************************************************************************/
  18712. SOAP_FMAC1
  18713. int
  18714. SOAP_FMAC2
  18715. soap_connect(struct soap *soap, const char *endpoint, const char *action) {
  18716. return soap_connect_command(soap, SOAP_POST, endpoint, action);
  18717. }
  18718. /******************************************************************************/
  18719. SOAP_FMAC1
  18720. int
  18721. SOAP_FMAC2
  18722. soap_connect_command(struct soap *soap, int http_command, const char *endpoints, const char *action) {
  18723. if (endpoints) {
  18724. const char *s;
  18725. s = strchr(endpoints, ' ');
  18726. if (s) {
  18727. size_t l = strlen(endpoints);
  18728. char *endpoint = NULL;
  18729. if (SOAP_MAXALLOCSIZE == 0 || l <= SOAP_MAXALLOCSIZE)
  18730. endpoint = (char *) SOAP_MALLOC(soap, l + 1);
  18731. if (!endpoint)
  18732. return soap->error = SOAP_EOM;
  18733. for (;;) {
  18734. (void) soap_strncpy(endpoint, l + 1, endpoints, s - endpoints);
  18735. endpoint[s - endpoints] = '\0';
  18736. if (soap_try_connect_command(soap, http_command, endpoint, action) != SOAP_TCP_ERROR)
  18737. break;
  18738. if (!*s)
  18739. break;
  18740. soap->error = SOAP_OK;
  18741. while (*s == ' ')
  18742. s++;
  18743. endpoints = s;
  18744. s = strchr(endpoints, ' ');
  18745. if (!s)
  18746. s = endpoints + strlen(endpoints);
  18747. }
  18748. SOAP_FREE(soap, endpoint);
  18749. } else {
  18750. soap_try_connect_command(soap, http_command, endpoints, action);
  18751. }
  18752. }
  18753. return soap->error;
  18754. }
  18755. /******************************************************************************/
  18756. static int
  18757. soap_try_connect_command(struct soap *soap, int http_command, const char *endpoint, const char *action) {
  18758. char host[sizeof(soap->host)];
  18759. int port;
  18760. ULONG64 count;
  18761. soap->error = SOAP_OK;
  18762. soap_memcpy(host, sizeof(host), soap->host, sizeof(soap->host)); /* save previous host name: if != then reconnect */
  18763. port = soap->port; /* save previous port to compare */
  18764. soap->status = http_command;
  18765. soap_set_endpoint(soap, endpoint);
  18766. soap->action = soap_strdup(soap, action);
  18767. #ifndef WITH_LEANER
  18768. if (soap->fconnect) {
  18769. soap->error = soap->fconnect(soap, endpoint, soap->host, soap->port);
  18770. if (soap->error)
  18771. return soap->error;
  18772. } else
  18773. #endif
  18774. if (soap->fopen && *soap->host) {
  18775. if (!soap->keep_alive || !soap_valid_socket(soap->socket) || strcmp(soap->host, host) || soap->port != port ||
  18776. !soap->fpoll || soap->fpoll(soap)) {
  18777. soap->error = SOAP_OK;
  18778. #ifndef WITH_LEAN
  18779. if (!strncmp(endpoint, "soap.udp:", 9) || !strncmp(endpoint, "udp:", 4)) {
  18780. soap->omode |= SOAP_IO_UDP;
  18781. } else
  18782. #endif
  18783. {
  18784. soap->keep_alive = 0; /* to force close */
  18785. soap->omode &= ~SOAP_IO_UDP; /* to force close */
  18786. }
  18787. soap_closesock(soap);
  18788. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Connect/reconnect to '%s' host='%s' path='%s' port=%d\n",
  18789. endpoint ? endpoint : "(null)", soap->host, soap->path, soap->port));
  18790. if (!soap->keep_alive || !soap_valid_socket(soap->socket)) {
  18791. soap->socket = soap->fopen(soap, endpoint, soap->host, soap->port);
  18792. if (!soap_valid_socket(soap->socket) || soap->error) {
  18793. if (soap->error)
  18794. return soap->error;
  18795. return soap->error = SOAP_TCP_ERROR;
  18796. }
  18797. soap->keep_alive = -((soap->omode & SOAP_IO_KEEPALIVE) != 0);
  18798. }
  18799. }
  18800. }
  18801. #ifdef WITH_NTLM
  18802. if (soap_ntlm_handshake(soap, SOAP_GET, endpoint, soap->host, soap->port))
  18803. return soap->error;
  18804. #endif
  18805. count = soap_count_attachments(soap);
  18806. if (soap_init_send(soap))
  18807. return soap->error;
  18808. #ifndef WITH_NOHTTP
  18809. if (http_command == SOAP_GET || http_command == SOAP_DEL || http_command == SOAP_HEAD ||
  18810. http_command == SOAP_OPTIONS) {
  18811. soap->mode &= ~SOAP_IO;
  18812. soap->mode |= SOAP_IO_BUFFER;
  18813. }
  18814. if ((soap->mode & SOAP_IO) != SOAP_IO_STORE && !(soap->mode & SOAP_ENC_PLAIN) && endpoint) {
  18815. soap_mode k = soap->mode;
  18816. soap->mode &= ~(SOAP_IO | SOAP_ENC_ZLIB);
  18817. if ((k & SOAP_IO) != SOAP_IO_FLUSH)
  18818. soap->mode |= SOAP_IO_BUFFER;
  18819. soap->error = soap->fpost(soap, endpoint, soap->host, soap->port, soap->path, action, count);
  18820. if (soap->error)
  18821. return soap->error;
  18822. if ((k & SOAP_IO) == SOAP_IO_CHUNK) {
  18823. if (soap_flush(soap))
  18824. return soap->error;
  18825. }
  18826. soap->mode = k;
  18827. }
  18828. if (http_command == SOAP_GET || http_command == SOAP_DEL || http_command == SOAP_HEAD ||
  18829. http_command == SOAP_OPTIONS)
  18830. return soap_end_send_flush(soap);
  18831. #endif
  18832. #ifndef WITH_LEANER
  18833. if (soap_begin_attachments(soap))
  18834. return soap->error;
  18835. #endif
  18836. return SOAP_OK;
  18837. }
  18838. /******************************************************************************/
  18839. #ifdef WITH_NTLM
  18840. static int
  18841. soap_ntlm_handshake(struct soap *soap, int command, const char *endpoint, const char *host, int port)
  18842. {
  18843. /* requires libntlm from http://www.nongnu.org/libntlm/ */
  18844. const char *userid = (soap->proxy_userid ? soap->proxy_userid : soap->userid);
  18845. const char *passwd = (soap->proxy_passwd ? soap->proxy_passwd : soap->passwd);
  18846. struct SOAP_ENV__Header *oldheader;
  18847. if (soap->ntlm_challenge && userid && passwd && soap->authrealm)
  18848. {
  18849. tSmbNtlmAuthRequest req;
  18850. tSmbNtlmAuthResponse res;
  18851. tSmbNtlmAuthChallenge ch;
  18852. int k = soap->keep_alive;
  18853. ULONG64 l = soap->length;
  18854. ULONG64 c = soap->count;
  18855. soap_mode m = soap->mode, o = soap->omode;
  18856. int s = soap->status;
  18857. char *a = soap->action;
  18858. short v = soap->version;
  18859. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "NTLM '%s'\n", soap->ntlm_challenge));
  18860. if (!*soap->ntlm_challenge)
  18861. {
  18862. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "NTLM S->C Type 1: received NTLM authentication challenge from server\n"));
  18863. /* S -> C 401 Unauthorized
  18864. WWW-Authenticate: NTLM
  18865. */
  18866. buildSmbNtlmAuthRequest(&req, userid, soap->authrealm);
  18867. soap->ntlm_challenge = soap_s2base64(soap, (unsigned char*)(void*)&req, NULL, SmbLength(&req));
  18868. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "NTLM C->S Type 2: sending NTLM authorization to server\nAuthorization: NTLM %s\n", soap->ntlm_challenge));
  18869. /* C -> S GET ...
  18870. Authorization: NTLM TlRMTVNTUAABAAAAA7IAAAoACgApAAAACQAJACAAAABMSUdIVENJVFlVUlNBLU1JTk9S
  18871. */
  18872. soap->omode = SOAP_IO_BUFFER;
  18873. if (soap_init_send(soap))
  18874. return soap->error;
  18875. if (!soap->keep_alive)
  18876. soap->keep_alive = -1; /* client keep alive */
  18877. soap->status = command;
  18878. if (soap->fpost(soap, endpoint, host, port, soap->path, soap->action, 0)
  18879. || soap_end_send_flush(soap))
  18880. return soap->error;
  18881. soap->mode = m;
  18882. soap->keep_alive = k;
  18883. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "NTLM S->C Type 2: waiting on server NTLM response\n"));
  18884. oldheader = soap->header;
  18885. if (soap_begin_recv(soap))
  18886. if (soap->error == SOAP_EOF)
  18887. return soap->error;
  18888. (void)soap_end_recv(soap);
  18889. soap->header = oldheader;
  18890. soap->length = l;
  18891. if (soap->status != 401 && soap->status != 407)
  18892. return soap->error = SOAP_NTLM_ERROR;
  18893. soap->error = SOAP_OK;
  18894. }
  18895. /* S -> C 401 Unauthorized
  18896. WWW-Authenticate: NTLM TlRMTVNTUAACAAAAAAAAACgAAAABggAAU3J2Tm9uY2UAAAAAAAAAAA==
  18897. */
  18898. soap_base642s(soap, soap->ntlm_challenge, (char*)&ch, sizeof(tSmbNtlmAuthChallenge), NULL);
  18899. buildSmbNtlmAuthResponse(&ch, &res, userid, passwd);
  18900. soap->ntlm_challenge = soap_s2base64(soap, (unsigned char*)(void*)&res, NULL, SmbLength(&res));
  18901. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "NTLM C->S Type 3: sending NTLM authorization to server\nAuthorization: NTLM %s\n", soap->ntlm_challenge));
  18902. /* C -> S GET ...
  18903. Authorization: NTLM TlRMTVNTUAADAAAAGAAYAHIAAAAYABgAigAAABQAFABAAAAADAAMAFQAAAASABIAYAAAAAAAAACiAAAAAYIAAFUAUgBTAEEALQBNAEkATgBPAFIAWgBhAHAAaABvAGQATABJAEcASABUAEMASQBUAFkArYfKbe/jRoW5xDxHeoxC1gBmfWiS5+iX4OAN4xBKG/IFPwfH3agtPEia6YnhsADT
  18904. */
  18905. soap->userid = NULL;
  18906. soap->passwd = NULL;
  18907. soap->proxy_userid = NULL;
  18908. soap->proxy_passwd = NULL;
  18909. soap->keep_alive = k;
  18910. soap->length = l;
  18911. soap->count = c;
  18912. soap->mode = m;
  18913. soap->omode = o;
  18914. soap->status = s;
  18915. soap->action = a;
  18916. soap->version = v;
  18917. }
  18918. return SOAP_OK;
  18919. }
  18920. #endif
  18921. /******************************************************************************/
  18922. SOAP_FMAC1
  18923. char *
  18924. SOAP_FMAC2
  18925. soap_s2base64(struct soap *soap, const unsigned char *s, char *t, int n) {
  18926. int i;
  18927. unsigned long m;
  18928. char *p;
  18929. if (!t)
  18930. t = (char *) soap_malloc(soap, (n + 2) / 3 * 4 + 1);
  18931. if (!t)
  18932. return NULL;
  18933. p = t;
  18934. t[0] = '\0';
  18935. if (!s)
  18936. return p;
  18937. for (; n > 2; n -= 3, s += 3) {
  18938. m = s[0];
  18939. m = (m << 8) | s[1];
  18940. m = (m << 8) | s[2];
  18941. for (i = 4; i > 0; m >>= 6)
  18942. t[--i] = soap_base64o[m & 0x3F];
  18943. t += 4;
  18944. }
  18945. t[0] = '\0';
  18946. if (n > 0) /* 0 < n <= 2 implies that t[0..4] is allocated (base64 scaling formula) */
  18947. {
  18948. m = 0;
  18949. for (i = 0; i < n; i++)
  18950. m = (m << 8) | *s++;
  18951. for (; i < 3; i++)
  18952. m <<= 8;
  18953. for (i = 4; i > 0; m >>= 6)
  18954. t[--i] = soap_base64o[m & 0x3F];
  18955. for (i = 3; i > n; i--)
  18956. t[i] = '=';
  18957. t[4] = '\0';
  18958. }
  18959. return p;
  18960. }
  18961. /******************************************************************************/
  18962. SOAP_FMAC1
  18963. const char *
  18964. SOAP_FMAC2
  18965. soap_base642s(struct soap *soap, const char *s, char *t, size_t l, int *n) {
  18966. size_t i, j;
  18967. soap_wchar c;
  18968. unsigned long m;
  18969. const char *p;
  18970. if (!s || !*s) {
  18971. if (n)
  18972. *n = 0;
  18973. if (soap->error)
  18974. return NULL;
  18975. return SOAP_NON_NULL;
  18976. }
  18977. if (!t) {
  18978. l = (strlen(s) + 3) / 4 * 3 + 1; /* space for raw binary and \0 */
  18979. t = (char *) soap_malloc(soap, l);
  18980. }
  18981. if (!t)
  18982. return NULL;
  18983. p = t;
  18984. if (n)
  18985. *n = 0;
  18986. for (i = 0;; i += 3, l -= 3) {
  18987. m = 0;
  18988. j = 0;
  18989. while (j < 4) {
  18990. c = *s++;
  18991. if (c == '=' || !c) {
  18992. if (l >= j - 1) {
  18993. switch (j) {
  18994. case 2:
  18995. *t++ = (char) ((m >> 4) & 0xFF);
  18996. i++;
  18997. l--;
  18998. break;
  18999. case 3:
  19000. *t++ = (char) ((m >> 10) & 0xFF);
  19001. *t++ = (char) ((m >> 2) & 0xFF);
  19002. i += 2;
  19003. l -= 2;
  19004. }
  19005. }
  19006. if (n)
  19007. *n = (int) i;
  19008. if (l)
  19009. *t = '\0';
  19010. return p;
  19011. }
  19012. c -= '+';
  19013. if (c >= 0 && c <= 79) {
  19014. int b = soap_base64i[c];
  19015. if (b >= 64) {
  19016. soap->error = SOAP_TYPE;
  19017. return NULL;
  19018. }
  19019. m = (m << 6) + b;
  19020. j++;
  19021. } else if (!soap_coblank(c + '+')) {
  19022. soap->error = SOAP_TYPE;
  19023. return NULL;
  19024. }
  19025. }
  19026. if (l < 3) {
  19027. if (n)
  19028. *n = (int) i;
  19029. if (l)
  19030. *t = '\0';
  19031. return p;
  19032. }
  19033. *t++ = (char) ((m >> 16) & 0xFF);
  19034. *t++ = (char) ((m >> 8) & 0xFF);
  19035. *t++ = (char) (m & 0xFF);
  19036. }
  19037. }
  19038. /******************************************************************************/
  19039. SOAP_FMAC1
  19040. char *
  19041. SOAP_FMAC2
  19042. soap_s2hex(struct soap *soap, const unsigned char *s, char *t, int n) {
  19043. char *p;
  19044. if (!t)
  19045. t = (char *) soap_malloc(soap, 2 * n + 1);
  19046. if (!t)
  19047. return NULL;
  19048. p = t;
  19049. t[0] = '\0';
  19050. if (s) {
  19051. for (; n > 0; n--) {
  19052. int m = *s++;
  19053. *t++ = (char) ((m >> 4) + (m > 159 ? 'a' - 10 : '0'));
  19054. m &= 0x0F;
  19055. *t++ = (char) (m + (m > 9 ? 'a' - 10 : '0'));
  19056. }
  19057. }
  19058. *t++ = '\0';
  19059. return p;
  19060. }
  19061. /******************************************************************************/
  19062. SOAP_FMAC1
  19063. const char *
  19064. SOAP_FMAC2
  19065. soap_hex2s(struct soap *soap, const char *s, char *t, size_t l, int *n) {
  19066. const char *p;
  19067. if (!s || !*s) {
  19068. if (n)
  19069. *n = 0;
  19070. if (soap->error)
  19071. return NULL;
  19072. return SOAP_NON_NULL;
  19073. }
  19074. if (!t) {
  19075. l = strlen(s) / 2 + 1; /* make sure enough space for \0 */
  19076. t = (char *) soap_malloc(soap, l);
  19077. }
  19078. if (!t)
  19079. return NULL;
  19080. p = t;
  19081. while (l) {
  19082. int d1, d2;
  19083. d1 = *s++;
  19084. if (!d1)
  19085. break;
  19086. d2 = *s++;
  19087. if (!d2)
  19088. break;
  19089. *t++ = (char) (((d1 >= 'A' ? (d1 & 0x7) + 9 : d1 - '0') << 4) + (d2 >= 'A' ? (d2 & 0x7) + 9 : d2 - '0'));
  19090. l--;
  19091. }
  19092. if (n)
  19093. *n = (int) (t - p);
  19094. if (l)
  19095. *t = '\0';
  19096. return p;
  19097. }
  19098. /******************************************************************************/
  19099. #ifndef WITH_NOHTTP
  19100. SOAP_FMAC1
  19101. const char *
  19102. SOAP_FMAC2
  19103. soap_http_content_type(struct soap *soap, int status) {
  19104. if (soap->status != SOAP_GET && soap->status != SOAP_DEL && soap->status != SOAP_CONNECT) {
  19105. const char *s = "text/xml; charset=utf-8";
  19106. #ifndef WITH_LEANER
  19107. const char *r = NULL;
  19108. size_t n;
  19109. #endif
  19110. if (((status >= SOAP_FILE && status < SOAP_FILE + 600) || soap->status == SOAP_PUT ||
  19111. soap->status == SOAP_POST_FILE || soap->status == SOAP_PATCH) && soap->http_content &&
  19112. *soap->http_content && !strchr(soap->http_content, 10) && !strchr(soap->http_content, 13))
  19113. s = soap->http_content;
  19114. else if (status == SOAP_HTML)
  19115. s = "text/html; charset=utf-8";
  19116. else if (soap->version == 2)
  19117. s = "application/soap+xml; charset=utf-8";
  19118. soap->http_content = NULL; /* use http_content once (assign new value before each call) */
  19119. #ifndef WITH_LEANER
  19120. if (soap->mode & (SOAP_ENC_DIME | SOAP_ENC_MTOM)) {
  19121. if ((soap->mode & SOAP_ENC_MTOM)) {
  19122. if (soap->version == 2)
  19123. r = "application/soap+xml";
  19124. else
  19125. r = "text/xml";
  19126. s = "application/xop+xml";
  19127. } else {
  19128. s = "application/dime";
  19129. }
  19130. }
  19131. if ((soap->mode & SOAP_ENC_MIME) && soap->mime.boundary) {
  19132. const char *t;
  19133. size_t l;
  19134. (SOAP_SNPRINTF(soap->tmpbuf, sizeof(soap->tmpbuf), strlen(soap->mime.boundary) + 53),
  19135. "multipart/related; charset=utf-8; boundary=\"%s\"; type=\"", soap->mime.boundary);
  19136. t = strchr(s, ';');
  19137. if (t)
  19138. n = t - s;
  19139. else
  19140. n = strlen(s);
  19141. l = strlen(soap->tmpbuf);
  19142. if (sizeof(soap->tmpbuf) > l + n)
  19143. (void) soap_strncpy(soap->tmpbuf + l, sizeof(soap->tmpbuf) - l, s, n);
  19144. if (soap->mime.start) {
  19145. l = strlen(soap->tmpbuf);
  19146. (SOAP_SNPRINTF(soap->tmpbuf + l, sizeof(soap->tmpbuf) - l, strlen(soap->mime.start) + 10),
  19147. "\"; start=\"%s", soap->mime.start);
  19148. }
  19149. if (r) {
  19150. l = strlen(soap->tmpbuf);
  19151. (SOAP_SNPRINTF(soap->tmpbuf + l, sizeof(soap->tmpbuf) - l, strlen(r) + 15), "\"; start-info=\"%s", r);
  19152. }
  19153. l = strlen(soap->tmpbuf);
  19154. if (sizeof(soap->tmpbuf) > l)
  19155. soap_strcpy(soap->tmpbuf + l, sizeof(soap->tmpbuf) - l, "\"");
  19156. } else {
  19157. soap_strcpy(soap->tmpbuf, sizeof(soap->tmpbuf), s);
  19158. }
  19159. if (status == SOAP_OK && soap->version == 2 && soap->action) {
  19160. size_t l = strlen(soap->tmpbuf);
  19161. n = strlen(soap->action);
  19162. (SOAP_SNPRINTF(soap->tmpbuf + l, sizeof(soap->tmpbuf) - l, n + 11), "; action=\"%s\"", soap->action);
  19163. }
  19164. #else
  19165. soap_strcpy(soap->tmpbuf, sizeof(soap->tmpbuf), s);
  19166. #endif
  19167. return soap->tmpbuf;
  19168. }
  19169. return NULL;
  19170. }
  19171. #endif
  19172. /******************************************************************************/
  19173. #ifndef WITH_NOHTTP
  19174. SOAP_FMAC1
  19175. int
  19176. SOAP_FMAC2
  19177. soap_puthttphdr(struct soap *soap, int status, ULONG64 count) {
  19178. int err = SOAP_OK;
  19179. if (soap_http_content_type(soap, status)) {
  19180. err = soap->fposthdr(soap, "Content-Type", soap->tmpbuf);
  19181. if (err)
  19182. return err;
  19183. #ifdef WITH_ZLIB
  19184. if ((soap->omode & SOAP_ENC_ZLIB))
  19185. {
  19186. #ifdef WITH_GZIP
  19187. err = soap->fposthdr(soap, "Content-Encoding", soap->zlib_out == SOAP_ZLIB_DEFLATE ? "deflate" : "gzip");
  19188. #else
  19189. err = soap->fposthdr(soap, "Content-Encoding", "deflate");
  19190. #endif
  19191. if (err)
  19192. return err;
  19193. }
  19194. #endif
  19195. #ifndef WITH_LEANER
  19196. if ((soap->omode & SOAP_IO) == SOAP_IO_CHUNK) {
  19197. err = soap->fposthdr(soap, "Transfer-Encoding", "chunked");
  19198. } else
  19199. #endif
  19200. {
  19201. (SOAP_SNPRINTF(soap->tmpbuf, sizeof(soap->tmpbuf), 20), SOAP_ULONG_FORMAT, count);
  19202. err = soap->fposthdr(soap, "Content-Length", soap->tmpbuf);
  19203. }
  19204. if (err)
  19205. return err;
  19206. }
  19207. if (soap->http_extra_header) {
  19208. err = soap_send(soap, soap->http_extra_header);
  19209. soap->http_extra_header = NULL; /* use http_extra_header once (assign new value before each call) */
  19210. if (err)
  19211. return err;
  19212. err = soap_send_raw(soap, "\r\n", 2);
  19213. if (err)
  19214. return err;
  19215. }
  19216. if (soap->keep_alive) {
  19217. if (soap->keep_alive > 0 && soap->recv_timeout) {
  19218. int t = soap->recv_timeout;
  19219. if (t < 0)
  19220. t = 1;
  19221. (SOAP_SNPRINTF(soap->tmpbuf, sizeof(soap->tmpbuf), 20), "timeout=%d, max=%d", soap->recv_timeout,
  19222. soap->keep_alive);
  19223. err = soap->fposthdr(soap, "Keep-Alive", soap->tmpbuf);
  19224. if (err)
  19225. return err;
  19226. }
  19227. return soap->fposthdr(soap, "Connection", "keep-alive");
  19228. }
  19229. return soap->fposthdr(soap, "Connection", "close");
  19230. }
  19231. #endif
  19232. /******************************************************************************/
  19233. #ifndef WITH_LEAN
  19234. static const char *
  19235. soap_set_validation_fault(struct soap *soap, const char *s, const char *t) {
  19236. if (!t)
  19237. t = SOAP_STR_EOS;
  19238. if (*soap->tag)
  19239. (SOAP_SNPRINTF(soap->msgbuf, sizeof(soap->msgbuf), strlen(s) + strlen(t) + strlen(soap->tag) + 47),
  19240. "Validation constraint violation: %s%s in element '%s'", s, t, soap->tag);
  19241. else
  19242. (SOAP_SNPRINTF(soap->msgbuf, sizeof(soap->msgbuf), strlen(s) + strlen(t) + 33),
  19243. "Validation constraint violation: %s%s", s, t);
  19244. return soap->msgbuf;
  19245. }
  19246. #endif
  19247. /******************************************************************************/
  19248. SOAP_FMAC1
  19249. void
  19250. SOAP_FMAC2
  19251. soap_set_fault(struct soap *soap) {
  19252. const char **c;
  19253. const char **s;
  19254. if (soap->version == 0)
  19255. soap_version(soap);
  19256. c = soap_faultcode(soap);
  19257. s = soap_faultstring(soap);
  19258. if (soap->fseterror)
  19259. soap->fseterror(soap, c, s);
  19260. if (!*c) {
  19261. if (soap->version == 2)
  19262. *c = "SOAP-ENV:Sender";
  19263. else if (soap->version == 1)
  19264. *c = "SOAP-ENV:Client";
  19265. else
  19266. *c = "";
  19267. }
  19268. if (*s)
  19269. return;
  19270. if (soap->error >= SOAP_POST)
  19271. soap->error = SOAP_HTTP_METHOD;
  19272. switch (soap->error) {
  19273. #ifndef WITH_LEAN
  19274. case SOAP_CLI_FAULT:
  19275. *s = "Client fault";
  19276. break;
  19277. case SOAP_SVR_FAULT:
  19278. *s = "Server fault";
  19279. break;
  19280. case SOAP_TAG_MISMATCH:
  19281. *s = soap_set_validation_fault(soap, "tag name or namespace mismatch", NULL);
  19282. break;
  19283. case SOAP_TYPE:
  19284. if (*soap->type)
  19285. *s = soap_set_validation_fault(soap, "type mismatch ", soap->type);
  19286. else if (*soap->arrayType)
  19287. *s = soap_set_validation_fault(soap, "array type mismatch", NULL);
  19288. else
  19289. *s = soap_set_validation_fault(soap, "invalid value", NULL);
  19290. break;
  19291. case SOAP_SYNTAX_ERROR:
  19292. *s = soap_set_validation_fault(soap, "syntax error", NULL);
  19293. break;
  19294. case SOAP_NO_TAG:
  19295. if (soap->version == 0 && soap->level == 0)
  19296. *s = soap_set_validation_fault(soap, "root element expected", NULL);
  19297. else if (soap->level == 0)
  19298. *s = soap_set_validation_fault(soap, "SOAP message expected", NULL);
  19299. else
  19300. *s = soap_set_validation_fault(soap, "element tag expected", NULL);
  19301. break;
  19302. case SOAP_END_TAG:
  19303. *s = soap_set_validation_fault(soap, "closing tag expected", NULL);
  19304. break;
  19305. case SOAP_MUSTUNDERSTAND:
  19306. *c = "SOAP-ENV:MustUnderstand";
  19307. (SOAP_SNPRINTF(soap->msgbuf, sizeof(soap->msgbuf), strlen(soap->tag) + 65),
  19308. "The data in element '%s' must be understood but cannot be processed, Sassan %s", soap->tag, debug);
  19309. *s = soap->msgbuf;
  19310. break;
  19311. case SOAP_VERSIONMISMATCH:
  19312. *c = "SOAP-ENV:VersionMismatch";
  19313. *s = "Invalid SOAP message or SOAP version mismatch";
  19314. break;
  19315. case SOAP_DATAENCODINGUNKNOWN:
  19316. *c = "SOAP-ENV:DataEncodingUnknown";
  19317. *s = "Unsupported SOAP data encoding";
  19318. break;
  19319. case SOAP_NAMESPACE:
  19320. *s = soap_set_validation_fault(soap, "namespace error", NULL);
  19321. break;
  19322. case SOAP_USER_ERROR:
  19323. *s = "User data access error";
  19324. break;
  19325. case SOAP_NO_METHOD:
  19326. (SOAP_SNPRINTF(soap->msgbuf, sizeof(soap->msgbuf), strlen(soap->tag) + 66),
  19327. "Method '%s' not implemented: method name or namespace not recognized", soap->tag);
  19328. *s = soap->msgbuf;
  19329. break;
  19330. case SOAP_NO_DATA:
  19331. *s = "Data required for operation";
  19332. break;
  19333. case SOAP_GET_METHOD:
  19334. *s = "HTTP GET method not implemented";
  19335. break;
  19336. case SOAP_PUT_METHOD:
  19337. *s = "HTTP PUT method not implemented";
  19338. break;
  19339. case SOAP_PATCH_METHOD:
  19340. *s = "HTTP PATCH method not implemented";
  19341. break;
  19342. case SOAP_DEL_METHOD:
  19343. *s = "HTTP DELETE method not implemented";
  19344. break;
  19345. case SOAP_HTTP_METHOD:
  19346. *s = "HTTP method error";
  19347. break;
  19348. case SOAP_EOM:
  19349. *s = "Out of memory";
  19350. break;
  19351. case SOAP_MOE:
  19352. *s = "Memory overflow or memory corruption error";
  19353. break;
  19354. case SOAP_HDR:
  19355. *s = "Header line too long";
  19356. break;
  19357. case SOAP_IOB:
  19358. *s = "Array index out of bounds";
  19359. break;
  19360. case SOAP_NULL:
  19361. *s = soap_set_validation_fault(soap, "nil not allowed", NULL);
  19362. break;
  19363. case SOAP_DUPLICATE_ID:
  19364. *s = soap_set_validation_fault(soap, "multiple elements (use the SOAP_XML_TREE flag) with duplicate id ",
  19365. soap->id);
  19366. if (soap->version == 2)
  19367. *soap_faultsubcode(soap) = "SOAP-ENC:DuplicateID";
  19368. break;
  19369. case SOAP_MISSING_ID:
  19370. *s = soap_set_validation_fault(soap, "missing id for ref ", soap->id);
  19371. if (soap->version == 2)
  19372. *soap_faultsubcode(soap) = "SOAP-ENC:MissingID";
  19373. break;
  19374. case SOAP_HREF:
  19375. *s = soap_set_validation_fault(soap, "incompatible object type id-ref ", soap->id);
  19376. break;
  19377. case SOAP_FAULT:
  19378. break;
  19379. #ifndef WITH_NOIO
  19380. case SOAP_UDP_ERROR:
  19381. *s = "Message too large for UDP packet";
  19382. break;
  19383. case SOAP_TCP_ERROR:
  19384. *s = tcp_error(soap);
  19385. break;
  19386. #endif
  19387. case SOAP_HTTP_ERROR:
  19388. *s = "An HTTP processing error occurred";
  19389. break;
  19390. case SOAP_NTLM_ERROR:
  19391. *s = "An HTTP NTLM authentication error occurred";
  19392. break;
  19393. case SOAP_SSL_ERROR:
  19394. #ifdef WITH_OPENSSL
  19395. *s = "SSL/TLS error";
  19396. #else
  19397. *s = "OpenSSL not installed: recompile with -DWITH_OPENSSL";
  19398. #endif
  19399. break;
  19400. case SOAP_PLUGIN_ERROR:
  19401. *s = "Plugin registry error";
  19402. break;
  19403. case SOAP_DIME_ERROR:
  19404. *s = "DIME format error or max DIME size exceeds SOAP_MAXDIMESIZE currently set to " SOAP_XSTRINGIFY(
  19405. SOAP_MAXDIMESIZE);
  19406. break;
  19407. case SOAP_DIME_HREF:
  19408. *s = "DIME href to missing attachment";
  19409. break;
  19410. case SOAP_DIME_MISMATCH:
  19411. *s = "DIME version/transmission error";
  19412. break;
  19413. case SOAP_DIME_END:
  19414. *s = "End of DIME error";
  19415. break;
  19416. case SOAP_MIME_ERROR:
  19417. *s = "MIME format error";
  19418. break;
  19419. case SOAP_MIME_HREF:
  19420. *s = "MIME href to missing attachment";
  19421. break;
  19422. case SOAP_MIME_END:
  19423. *s = "End of MIME error";
  19424. break;
  19425. case SOAP_ZLIB_ERROR:
  19426. #ifdef WITH_ZLIB
  19427. (SOAP_SNPRINTF(soap->msgbuf, sizeof(soap->msgbuf), (soap->d_stream && soap->d_stream->msg ? strlen(soap->d_stream->msg) : 0) + 19), "Zlib/gzip error: '%s'", soap->d_stream && soap->d_stream->msg ? soap->d_stream->msg : SOAP_STR_EOS);
  19428. *s = soap->msgbuf;
  19429. #else
  19430. *s = "Zlib/gzip not installed for (de)compression: recompile with -DWITH_GZIP";
  19431. #endif
  19432. break;
  19433. case SOAP_REQUIRED:
  19434. *s = soap_set_validation_fault(soap, "missing required attribute", NULL);
  19435. break;
  19436. case SOAP_PROHIBITED:
  19437. *s = soap_set_validation_fault(soap, "prohibited attribute present", NULL);
  19438. break;
  19439. case SOAP_LEVEL:
  19440. *s = "Maximum XML nesting depth level exceeded: increase maxlevel";
  19441. break;
  19442. case SOAP_LENGTH:
  19443. *s = soap_set_validation_fault(soap, "value range or content length violation", NULL);
  19444. break;
  19445. case SOAP_OCCURS:
  19446. *s = soap_set_validation_fault(soap, "occurrence constraint violation", NULL);
  19447. break;
  19448. case SOAP_FIXED:
  19449. *s = soap_set_validation_fault(soap, "value does not match the fixed value required", NULL);
  19450. break;
  19451. case SOAP_EMPTY:
  19452. *s = soap_set_validation_fault(soap, "empty value provided where a value is required", NULL);
  19453. break;
  19454. case SOAP_FD_EXCEEDED:
  19455. *s = "Maximum number of open connections was reached: increase FD_SETSIZE or define HAVE_POLL";
  19456. break;
  19457. case SOAP_UTF_ERROR:
  19458. *s = "UTF content encoding error";
  19459. break;
  19460. case SOAP_STOP:
  19461. *s = "Stopped: service request already handled by plugin (informative)";
  19462. break;
  19463. #endif
  19464. case SOAP_EOF:
  19465. #ifndef WITH_NOIO
  19466. *s = soap_strerror(soap); /* *s = soap->msgbuf */
  19467. #ifndef WITH_LEAN
  19468. if (strlen(soap->msgbuf) + 25 < sizeof(soap->msgbuf)) {
  19469. (void) soap_memmove((void *) (soap->msgbuf + 25), sizeof(soap->tmpbuf) - 25,
  19470. (const void *) soap->msgbuf, strlen(soap->msgbuf) + 1);
  19471. if (soap->is)
  19472. #if defined(__cplusplus) && !defined(WITH_COMPAT)
  19473. (void) soap_memcpy((void *) soap->msgbuf, sizeof(soap->msgbuf),
  19474. (const void *) "End or bad std::istream: ", 25);
  19475. #else
  19476. (void)soap_memcpy((void*)soap->msgbuf, sizeof(soap->msgbuf), (const void*)"End at NUL buffer input: ", 25);
  19477. #endif
  19478. else
  19479. (void) soap_memcpy((void *) soap->msgbuf, sizeof(soap->msgbuf),
  19480. (const void *) "End of file or no input: ", 25);
  19481. }
  19482. #endif
  19483. break;
  19484. #else
  19485. *s = "End of file or no input";
  19486. break;
  19487. #endif
  19488. case SOAP_ERR:
  19489. *s = "An unspecified error occurred";
  19490. break;
  19491. default:
  19492. #ifndef WITH_NOHTTP
  19493. #ifndef WITH_LEAN
  19494. if (soap->error >= 200 && soap->error < 600) {
  19495. const char *t = http_error(soap, soap->error);
  19496. (SOAP_SNPRINTF(soap->msgbuf, sizeof(soap->msgbuf), strlen(t) + 54), "Error %d: HTTP %d %s", soap->error,
  19497. soap->error, t);
  19498. *s = soap->msgbuf;
  19499. } else
  19500. #endif
  19501. #endif
  19502. {
  19503. (SOAP_SNPRINTF(soap->msgbuf, sizeof(soap->msgbuf), 26), "Error %d", soap->error);
  19504. *s = soap->msgbuf;
  19505. }
  19506. }
  19507. }
  19508. /******************************************************************************/
  19509. SOAP_FMAC1
  19510. int
  19511. SOAP_FMAC2
  19512. soap_send_fault(struct soap *soap) {
  19513. int status = soap->error;
  19514. if (status == SOAP_OK || status == SOAP_STOP)
  19515. return soap_closesock(soap);
  19516. #ifndef WITH_NOHTTP
  19517. if (status >= 200 && status <= 299)
  19518. return soap_send_empty_response(soap, status);
  19519. #endif
  19520. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Sending back fault struct for error code %d\n", soap->error));
  19521. soap->keep_alive = 0; /* error: close connection later by disabling keep-alive here */
  19522. soap_set_fault(soap);
  19523. if (soap->error < 200 && soap->error != SOAP_FAULT)
  19524. soap->header = NULL;
  19525. if (status != SOAP_EOF || (!soap->recv_timeout && !soap->send_timeout)) {
  19526. int r = 1;
  19527. #ifndef WITH_NOIO
  19528. if (soap->fpoll && soap->fpoll(soap)) {
  19529. r = 0;
  19530. }
  19531. #ifndef WITH_LEAN
  19532. else if (soap_valid_socket(soap->socket)) {
  19533. r = tcp_select(soap, soap->socket, SOAP_TCP_SELECT_RCV | SOAP_TCP_SELECT_SND, 0);
  19534. if (r > 0) {
  19535. int t;
  19536. if (!(r & SOAP_TCP_SELECT_SND)
  19537. || ((r & SOAP_TCP_SELECT_RCV)
  19538. && recv(soap->socket, (char *) &t, 1, MSG_PEEK) < 0))
  19539. r = 0;
  19540. }
  19541. }
  19542. #endif
  19543. #endif
  19544. if (r > 0) {
  19545. soap->error = SOAP_OK;
  19546. if (soap->version > 0) {
  19547. soap->encodingStyle = NULL; /* no encodingStyle in Faults */
  19548. soap_serializeheader(soap);
  19549. soap_serializefault(soap);
  19550. (void) soap_begin_count(soap);
  19551. if ((soap->mode & SOAP_IO_LENGTH)) {
  19552. if (soap_envelope_begin_out(soap)
  19553. || soap_putheader(soap)
  19554. || soap_body_begin_out(soap)
  19555. || soap_putfault(soap)
  19556. || soap_body_end_out(soap)
  19557. || soap_envelope_end_out(soap))
  19558. return soap_closesock(soap);
  19559. }
  19560. (void) soap_end_count(soap);
  19561. if (soap_response(soap, status)
  19562. || soap_envelope_begin_out(soap)
  19563. || soap_putheader(soap)
  19564. || soap_body_begin_out(soap)
  19565. || soap_putfault(soap)
  19566. || soap_body_end_out(soap)
  19567. || soap_envelope_end_out(soap)
  19568. || soap_end_send(soap))
  19569. return soap_closesock(soap);
  19570. } else {
  19571. const char *s = *soap_faultstring(soap);
  19572. const char **d = soap_faultdetail(soap);
  19573. (void) soap_begin_count(soap);
  19574. if ((soap->mode & SOAP_IO_LENGTH))
  19575. if (soap_element_begin_out(soap, "fault", 0, NULL)
  19576. || soap_outstring(soap, "reason", 0, (char *const *) &s, NULL, 0)
  19577. || soap_outliteral(soap, "detail", (char *const *) d, NULL)
  19578. || soap_element_end_out(soap, "fault"))
  19579. return soap_closesock(soap);
  19580. (void) soap_end_count(soap);
  19581. if (soap_response(soap, status)
  19582. || soap_element_begin_out(soap, "fault", 0, NULL)
  19583. || soap_outstring(soap, "reason", 0, (char *const *) &s, NULL, 0)
  19584. || soap_outliteral(soap, "detail", (char *const *) d, NULL)
  19585. || soap_element_end_out(soap, "fault")
  19586. || soap_end_send(soap))
  19587. return soap_closesock(soap);
  19588. }
  19589. }
  19590. }
  19591. soap->error = status;
  19592. return soap_closesock(soap);
  19593. }
  19594. /******************************************************************************/
  19595. SOAP_FMAC1
  19596. int
  19597. SOAP_FMAC2
  19598. soap_recv_fault(struct soap *soap, int check) {
  19599. int status = soap->status;
  19600. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Check (%d) if receiving SOAP Fault (status = %d)\n", check, status));
  19601. if (!check) {
  19602. /* try getfault when no tag or tag mismatched at level 2, otherwise close and return SOAP_TAG_MISMATCH or HTTP error code */
  19603. if (soap->error != SOAP_NO_TAG && (soap->error != SOAP_TAG_MISMATCH || soap->level != 2)) {
  19604. if (soap->error == SOAP_TAG_MISMATCH && soap->level == 0) {
  19605. soap->error = SOAP_OK;
  19606. if (!soap_element_begin_in(soap, "fault", 0, NULL)) {
  19607. char *s = NULL, *d = NULL;
  19608. (void) soap_instring(soap, "reason", &s, NULL, 0, 1, 0, -1, NULL);
  19609. (void) soap_inliteral(soap, "detail", &d);
  19610. if (!soap_element_end_in(soap, "fault") && !soap_end_recv(soap)) {
  19611. *soap_faultstring(soap) = s;
  19612. *soap_faultdetail(soap) = d;
  19613. if (status)
  19614. soap->error = status;
  19615. else
  19616. soap->error = SOAP_FAULT;
  19617. soap_set_fault(soap);
  19618. return soap_closesock(soap);
  19619. }
  19620. }
  19621. soap->error = SOAP_TAG_MISMATCH;
  19622. }
  19623. if (status && (status < 200 || status > 299))
  19624. soap->error = status;
  19625. return soap_closesock(soap);
  19626. }
  19627. }
  19628. soap->error = SOAP_OK;
  19629. if (soap_getfault(soap)) {
  19630. /* if check>0 and no SOAP Fault is present and no HTTP error then just return to parse request */
  19631. if (check
  19632. && (status == 0 || (status >= 200 && status <= 299))
  19633. && ((soap->error == SOAP_TAG_MISMATCH && soap->level == 2) || soap->error == SOAP_NO_TAG))
  19634. return soap->error = SOAP_OK;
  19635. /* if check=0 and empty SOAP Body and encodingStyle is NULL and no HTTP error then just return */
  19636. if (!check
  19637. && (status == 0 || (status >= 200 && status <= 299))
  19638. && !soap->encodingStyle
  19639. && (soap->error == SOAP_NO_TAG && soap->level <= 2))
  19640. return soap->error = SOAP_OK;
  19641. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Error: soap_get_soapfault() failed with error %d at level %u tag '%s'\n",
  19642. soap->error, soap->level, soap->tag));
  19643. *soap_faultcode(soap) = (soap->version == 2 ? "SOAP-ENV:Sender" : "SOAP-ENV:Client");
  19644. if (status)
  19645. soap->error = status;
  19646. else
  19647. soap->error = status = SOAP_NO_DATA;
  19648. soap_set_fault(soap);
  19649. } else {
  19650. const char *s = *soap_faultcode(soap);
  19651. if (!soap_match_tag(soap, s, "SOAP-ENV:Server")
  19652. || !soap_match_tag(soap, s, "SOAP-ENV:Receiver")) {
  19653. status = SOAP_SVR_FAULT;
  19654. } else if (!soap_match_tag(soap, s, "SOAP-ENV:Client")
  19655. || !soap_match_tag(soap, s, "SOAP-ENV:Sender")) {
  19656. status = SOAP_CLI_FAULT;
  19657. } else if (!soap_match_tag(soap, s, "SOAP-ENV:MustUnderstand")) {
  19658. status = SOAP_MUSTUNDERSTAND;
  19659. } else if (!soap_match_tag(soap, s, "SOAP-ENV:VersionMismatch")) {
  19660. status = SOAP_VERSIONMISMATCH;
  19661. } else {
  19662. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Received SOAP Fault code %s\n", s));
  19663. status = SOAP_FAULT;
  19664. }
  19665. if (!soap_body_end_in(soap))
  19666. soap_envelope_end_in(soap);
  19667. }
  19668. (void) soap_end_recv(soap);
  19669. soap->error = status;
  19670. return soap_closesock(soap);
  19671. }
  19672. /******************************************************************************/
  19673. #ifndef WITH_NOHTTP
  19674. SOAP_FMAC1
  19675. int
  19676. SOAP_FMAC2
  19677. soap_send_empty_response(struct soap *soap, int httpstatuscode) {
  19678. soap_mode m = soap->omode;
  19679. if (!(m & SOAP_IO_UDP)) {
  19680. soap->count = 0;
  19681. if ((m & SOAP_IO) == SOAP_IO_CHUNK)
  19682. soap->omode = (m & ~SOAP_IO) | SOAP_IO_BUFFER;
  19683. (void) soap_response(soap, httpstatuscode);
  19684. (void) soap_end_send(soap); /* force end of sends */
  19685. soap->error = SOAP_STOP; /* stops the server from returning another response */
  19686. soap->omode = m;
  19687. }
  19688. return soap_closesock(soap);
  19689. }
  19690. #endif
  19691. /******************************************************************************/
  19692. #ifndef WITH_NOHTTP
  19693. SOAP_FMAC1
  19694. int
  19695. SOAP_FMAC2
  19696. soap_recv_empty_response(struct soap *soap) {
  19697. soap->error = SOAP_OK;
  19698. if (!(soap->omode & SOAP_IO_UDP) && !(soap->omode & SOAP_ENC_PLAIN)) {
  19699. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Receiving empty response\n"));
  19700. if (soap_begin_recv(soap) == SOAP_OK) {
  19701. if (soap_http_skip_body(soap) || soap_end_recv(soap))
  19702. return soap_closesock(soap);
  19703. } else if (soap->error == 200 || soap->error == 201 || soap->error == 202) {
  19704. soap->error = SOAP_OK;
  19705. }
  19706. }
  19707. #ifndef WITH_LEANER
  19708. else if ((soap->fprepareinitrecv && (soap->error = soap->fprepareinitrecv(soap)))
  19709. || (soap->fpreparefinalrecv && (soap->error = soap->fpreparefinalrecv(soap)))) {
  19710. return soap->error;
  19711. }
  19712. #endif
  19713. return soap_closesock(soap);
  19714. }
  19715. #endif
  19716. /******************************************************************************/
  19717. #ifndef WITH_NOIO
  19718. static const char *
  19719. soap_strerror(struct soap *soap) {
  19720. int err = soap->errnum;
  19721. *soap->msgbuf = '\0';
  19722. if (err) {
  19723. #ifndef WIN32
  19724. # ifdef HAVE_STRERROR_R
  19725. # if (!_GNU_SOURCE && !defined(_POSIX_C_SOURCE) && !defined(_XOPEN_SOURCE)) || _POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600 || defined(__ANDROID__) || !defined(__GLIBC__)
  19726. strerror_r(err, soap->msgbuf, sizeof(soap->msgbuf)); /* XSI-compliant */
  19727. # else
  19728. return strerror_r(err, soap->msgbuf, sizeof(soap->msgbuf)); /* GNU-specific */
  19729. # endif
  19730. # else
  19731. return strerror(err);
  19732. # endif
  19733. #else
  19734. #ifndef UNDER_CE
  19735. DWORD len;
  19736. *soap->msgbuf = '\0';
  19737. len = FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)soap->msgbuf, (DWORD)sizeof(soap->msgbuf), NULL);
  19738. #else
  19739. DWORD i, len;
  19740. *soap->msgbuf = '\0';
  19741. len = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, err, 0, (LPTSTR)soap->msgbuf, (DWORD)(sizeof(soap->msgbuf)/sizeof(TCHAR)), NULL);
  19742. for (i = 0; i <= len; i++)
  19743. {
  19744. if (((TCHAR*)soap->msgbuf)[i] < 0x80)
  19745. soap->msgbuf[i] = (char)((TCHAR*)soap->msgbuf)[i];
  19746. else
  19747. soap->msgbuf[i] = '?';
  19748. }
  19749. #endif
  19750. #endif
  19751. } else {
  19752. if (soap->recv_maxlength && soap->count > soap->recv_maxlength) {
  19753. soap_strcpy(soap->msgbuf, sizeof(soap->msgbuf), "max message length exceeded");
  19754. } else {
  19755. int tt = soap->transfer_timeout, rt = soap->recv_timeout, st = soap->send_timeout;
  19756. #ifndef WITH_LEAN
  19757. int tu = ' ', ru = ' ', su = ' ';
  19758. #endif
  19759. soap_strcpy(soap->msgbuf, sizeof(soap->msgbuf), "message transfer interrupted");
  19760. if (tt | rt || st)
  19761. soap_strcpy(soap->msgbuf + 28, sizeof(soap->msgbuf) - 28, " or timed out");
  19762. #ifndef WITH_LEAN
  19763. if (tt < 0) {
  19764. tt = -tt;
  19765. tu = 'u';
  19766. }
  19767. if (rt < 0) {
  19768. rt = -rt;
  19769. ru = 'u';
  19770. }
  19771. if (st < 0) {
  19772. st = -st;
  19773. su = 'u';
  19774. }
  19775. if (tt) {
  19776. size_t l = strlen(soap->msgbuf);
  19777. (SOAP_SNPRINTF(soap->msgbuf + l, sizeof(soap->msgbuf) - l, 43), " (%d%csec max transfer time)", tt, tu);
  19778. }
  19779. if (rt) {
  19780. size_t l = strlen(soap->msgbuf);
  19781. (SOAP_SNPRINTF(soap->msgbuf + l, sizeof(soap->msgbuf) - l, 40), " (%d%csec max recv delay)", rt, ru);
  19782. }
  19783. if (st) {
  19784. size_t l = strlen(soap->msgbuf);
  19785. (SOAP_SNPRINTF(soap->msgbuf + l, sizeof(soap->msgbuf) - l, 40), " (%d%csec max send delay)", st, su);
  19786. }
  19787. #endif
  19788. }
  19789. }
  19790. return soap->msgbuf;
  19791. }
  19792. #endif
  19793. /******************************************************************************/
  19794. static int
  19795. soap_set_error(struct soap *soap, const char *faultcode, const char *faultsubcodeQName, const char *faultstring,
  19796. const char *faultdetailXML, int soaperror) {
  19797. *soap_faultcode(soap) = faultcode;
  19798. if (faultsubcodeQName)
  19799. *soap_faultsubcode(soap) = faultsubcodeQName;
  19800. *soap_faultstring(soap) = faultstring;
  19801. if (faultdetailXML && *faultdetailXML) {
  19802. const char **s = soap_faultdetail(soap);
  19803. if (s)
  19804. *s = faultdetailXML;
  19805. }
  19806. return soap->error = soaperror;
  19807. }
  19808. /******************************************************************************/
  19809. SOAP_FMAC1
  19810. int
  19811. SOAP_FMAC2
  19812. soap_set_sender_error(struct soap *soap, const char *faultstring, const char *faultdetailXML, int soaperror) {
  19813. return soap_set_error(soap,
  19814. soap->version == 2 ? "SOAP-ENV:Sender" : soap->version == 1 ? "SOAP-ENV:Client" : "at sender",
  19815. NULL, faultstring, faultdetailXML, soaperror);
  19816. }
  19817. /******************************************************************************/
  19818. SOAP_FMAC1
  19819. int
  19820. SOAP_FMAC2
  19821. soap_set_receiver_error(struct soap *soap, const char *faultstring, const char *faultdetailXML, int soaperror) {
  19822. return soap_set_error(soap, soap->version == 2 ? "SOAP-ENV:Receiver" : soap->version == 1 ? "SOAP-ENV:Server"
  19823. : "detected", NULL,
  19824. faultstring, faultdetailXML, soaperror);
  19825. }
  19826. /******************************************************************************/
  19827. static int
  19828. soap_copy_fault(struct soap *soap, const char *faultcode, const char *faultsubcodeQName, const char *faultstring,
  19829. const char *faultdetailXML) {
  19830. char *r = NULL, *s = NULL, *t = NULL;
  19831. DBGFUN2("soap_copy_fault", "code=%s", faultcode ? faultcode : "(null)", "string=%s",
  19832. faultstring ? faultstring : "(null)")
  19833. if (faultsubcodeQName)
  19834. r = soap_strdup(soap, faultsubcodeQName);
  19835. if (faultstring)
  19836. s = soap_strdup(soap, faultstring);
  19837. if (faultdetailXML)
  19838. t = soap_strdup(soap, faultdetailXML);
  19839. return soap_set_error(soap, faultcode, r, s, t, SOAP_FAULT);
  19840. }
  19841. /******************************************************************************/
  19842. SOAP_FMAC1
  19843. int
  19844. SOAP_FMAC2
  19845. soap_sender_fault(struct soap *soap, const char *faultstring, const char *faultdetailXML) {
  19846. return soap_sender_fault_subcode(soap, NULL, faultstring, faultdetailXML);
  19847. }
  19848. /******************************************************************************/
  19849. SOAP_FMAC1
  19850. int
  19851. SOAP_FMAC2
  19852. soap_sender_fault_subcode(struct soap *soap, const char *faultsubcodeQName, const char *faultstring,
  19853. const char *faultdetailXML) {
  19854. return soap_copy_fault(soap, soap->version == 2 ? "SOAP-ENV:Sender" : soap->version == 1 ? "SOAP-ENV:Client"
  19855. : "at source",
  19856. faultsubcodeQName, faultstring, faultdetailXML);
  19857. }
  19858. /******************************************************************************/
  19859. SOAP_FMAC1
  19860. int
  19861. SOAP_FMAC2
  19862. soap_receiver_fault(struct soap *soap, const char *faultstring, const char *faultdetailXML) {
  19863. return soap_receiver_fault_subcode(soap, NULL, faultstring, faultdetailXML);
  19864. }
  19865. /******************************************************************************/
  19866. SOAP_FMAC1
  19867. int
  19868. SOAP_FMAC2
  19869. soap_receiver_fault_subcode(struct soap *soap, const char *faultsubcodeQName, const char *faultstring,
  19870. const char *faultdetailXML) {
  19871. return soap_copy_fault(soap, soap->version == 2 ? "SOAP-ENV:Receiver" : soap->version == 1 ? "SOAP-ENV:Server"
  19872. : "is internal",
  19873. faultsubcodeQName, faultstring, faultdetailXML);
  19874. }
  19875. /******************************************************************************/
  19876. #ifndef WITH_NOSTDLIB
  19877. SOAP_FMAC1
  19878. void
  19879. SOAP_FMAC2
  19880. soap_print_fault(struct soap *soap, FILE *fd) {
  19881. if (soap_check_state(soap)) {
  19882. fprintf(fd, "Error: soap struct state not initialized\n");
  19883. } else if (soap->error) {
  19884. const char **c, *v = NULL, *s, *d;
  19885. c = soap_faultcode(soap);
  19886. if (!*c) {
  19887. soap_set_fault(soap);
  19888. c = soap_faultcode(soap);
  19889. }
  19890. if (soap->version == 2)
  19891. v = soap_fault_subcode(soap);
  19892. s = soap_fault_string(soap);
  19893. d = soap_fault_detail(soap);
  19894. fprintf(fd, "%s%d fault %s [%s]\n\"%s\"\nDetail: %s\n", soap->version ? "SOAP 1." : "Error ",
  19895. soap->version ? (int) soap->version : soap->error, *c, v ? v : "no subcode", s ? s : "[no reason]",
  19896. d ? d : "[no detail]");
  19897. }
  19898. }
  19899. #endif
  19900. /******************************************************************************/
  19901. #ifdef __cplusplus
  19902. #ifndef WITH_LEAN
  19903. #ifndef WITH_NOSTDLIB
  19904. #ifndef WITH_COMPAT
  19905. SOAP_FMAC1
  19906. void
  19907. SOAP_FMAC2
  19908. soap_stream_fault(struct soap *soap, std::ostream &os) {
  19909. if (soap_check_state(soap)) {
  19910. os << "Error: soap struct state not initialized\n";
  19911. } else if (soap->error) {
  19912. const char **c, *v = NULL, *s, *d;
  19913. c = soap_faultcode(soap);
  19914. if (!*c) {
  19915. soap_set_fault(soap);
  19916. c = soap_faultcode(soap);
  19917. }
  19918. if (soap->version == 2)
  19919. v = soap_fault_subcode(soap);
  19920. s = soap_fault_string(soap);
  19921. d = soap_fault_detail(soap);
  19922. os << (soap->version ? "SOAP 1." : "Error ")
  19923. << (soap->version ? (int) soap->version : soap->error)
  19924. << " fault " << *c
  19925. << "[" << (v ? v : "no subcode") << "]"
  19926. << std::endl
  19927. << "\"" << (s ? s : "[no reason]") << "\""
  19928. << std::endl
  19929. << "Detail: " << (d ? d : "[no detail]")
  19930. << std::endl;
  19931. }
  19932. }
  19933. #endif
  19934. #endif
  19935. #endif
  19936. #endif
  19937. /******************************************************************************/
  19938. #ifndef WITH_LEAN
  19939. #ifndef WITH_NOSTDLIB
  19940. SOAP_FMAC1
  19941. char *
  19942. SOAP_FMAC2
  19943. soap_sprint_fault(struct soap *soap, char *buf, size_t len) {
  19944. if (soap_check_state(soap)) {
  19945. soap_strcpy(buf, len, "Error: soap struct not initialized");
  19946. } else if (soap->error) {
  19947. const char **c, *v = NULL, *s, *d;
  19948. c = soap_faultcode(soap);
  19949. if (!*c) {
  19950. soap_set_fault(soap);
  19951. c = soap_faultcode(soap);
  19952. }
  19953. if (soap->version == 2)
  19954. v = soap_fault_subcode(soap);
  19955. if (!v)
  19956. v = "no subcode";
  19957. s = soap_fault_string(soap);
  19958. if (!s)
  19959. s = "[no reason]";
  19960. d = soap_fault_detail(soap);
  19961. if (!d)
  19962. d = "[no detail]";
  19963. (SOAP_SNPRINTF(buf, len, strlen(*c) + strlen(v) + strlen(s) + strlen(d) + 72),
  19964. "%s%d fault %s [%s]\n\"%s\"\nDetail: %s\n", soap->version ? "SOAP 1." : "Error ",
  19965. soap->version ? (int) soap->version : soap->error, *c, v, s, d);
  19966. } else if (len > 0) {
  19967. *buf = '\0';
  19968. }
  19969. return buf;
  19970. }
  19971. #endif
  19972. #endif
  19973. /******************************************************************************/
  19974. #ifndef WITH_NOSTDLIB
  19975. SOAP_FMAC1
  19976. void
  19977. SOAP_FMAC2
  19978. soap_print_fault_location(struct soap *soap, FILE *fd) {
  19979. #ifndef WITH_LEAN
  19980. int i, j, c1, c2;
  19981. if (soap_check_state(soap) == SOAP_OK && soap->error && soap->error != SOAP_STOP && soap->bufidx <= soap->buflen &&
  19982. soap->buflen > 0 && soap->buflen <= sizeof(soap->buf)) {
  19983. i = (int) soap->bufidx - 1;
  19984. if (i <= 0)
  19985. i = 0;
  19986. c1 = soap->buf[i];
  19987. soap->buf[i] = '\0';
  19988. if ((int) soap->buflen >= i + 1024)
  19989. j = i + 1023;
  19990. else
  19991. j = (int) soap->buflen - 1;
  19992. c2 = soap->buf[j];
  19993. soap->buf[j] = '\0';
  19994. fprintf(fd, "%s%c\n<!-- ** HERE ** -->\n", soap->buf, c1);
  19995. if (soap->bufidx < soap->buflen)
  19996. fprintf(fd, "%s\n", soap->buf + soap->bufidx);
  19997. soap->buf[i] = (char) c1;
  19998. soap->buf[j] = (char) c2;
  19999. }
  20000. #else
  20001. (void)soap;
  20002. (void)fd;
  20003. #endif
  20004. }
  20005. #endif
  20006. /******************************************************************************/
  20007. #ifdef __cplusplus
  20008. #ifndef WITH_LEAN
  20009. #ifndef WITH_NOSTDLIB
  20010. #ifndef WITH_COMPAT
  20011. SOAP_FMAC1
  20012. void
  20013. SOAP_FMAC2
  20014. soap_stream_fault_location(struct soap *soap, std::ostream &os) {
  20015. int i, j, c1, c2;
  20016. if (soap_check_state(soap) == SOAP_OK && soap->error && soap->error != SOAP_STOP && soap->bufidx <= soap->buflen &&
  20017. soap->buflen > 0 && soap->buflen <= sizeof(soap->buf)) {
  20018. i = (int) soap->bufidx - 1;
  20019. if (i <= 0)
  20020. i = 0;
  20021. c1 = soap->buf[i];
  20022. soap->buf[i] = '\0';
  20023. if ((int) soap->buflen >= i + 1024)
  20024. j = i + 1023;
  20025. else
  20026. j = (int) soap->buflen - 1;
  20027. c2 = soap->buf[j];
  20028. soap->buf[j] = '\0';
  20029. os << soap->buf << (char) c1 << std::endl << "<!-- ** HERE ** -->" << std::endl;
  20030. if (soap->bufidx < soap->buflen)
  20031. os << soap->buf + soap->bufidx << std::endl;
  20032. soap->buf[i] = (char) c1;
  20033. soap->buf[j] = (char) c2;
  20034. }
  20035. }
  20036. #endif
  20037. #endif
  20038. #endif
  20039. #endif
  20040. /******************************************************************************/
  20041. SOAP_FMAC1
  20042. int
  20043. SOAP_FMAC2
  20044. soap_register_plugin_arg(struct soap *soap, int (*fcreate)(struct soap *, struct soap_plugin *, void *), void *arg) {
  20045. struct soap_plugin *p;
  20046. int err;
  20047. p = (struct soap_plugin *) SOAP_MALLOC(soap, sizeof(struct soap_plugin));
  20048. if (!p)
  20049. return soap->error = SOAP_EOM;
  20050. p->id = NULL;
  20051. p->data = NULL;
  20052. p->fcopy = NULL;
  20053. p->fdelete = NULL;
  20054. err = fcreate(soap, p, arg);
  20055. if (!err && p->fdelete && p->id) {
  20056. if (!soap_lookup_plugin(soap, p->id)) {
  20057. p->next = soap->plugins;
  20058. soap->plugins = p;
  20059. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Registered '%s' plugin\n", p->id));
  20060. return SOAP_OK;
  20061. }
  20062. DBGLOG(TEST,
  20063. SOAP_MESSAGE(fdebug, "Could not register plugin '%s': plugin with the same ID already registered\n",
  20064. p->id));
  20065. SOAP_FREE(soap, p);
  20066. return SOAP_OK;
  20067. }
  20068. DBGLOG(TEST, SOAP_MESSAGE(fdebug,
  20069. "Could not register plugin '%s': plugin returned error %d or plugin ID not set or fdelete callback not set\n",
  20070. p->id ? p->id : "plugin ID not set", err));
  20071. SOAP_FREE(soap, p);
  20072. soap->error = err ? err : SOAP_PLUGIN_ERROR;
  20073. return soap->error;
  20074. }
  20075. /******************************************************************************/
  20076. static void *
  20077. fplugin(struct soap *soap, const char *id) {
  20078. struct soap_plugin *p;
  20079. for (p = soap->plugins; p; p = p->next)
  20080. if (p->id == id || !strcmp(p->id, id))
  20081. return p->data;
  20082. return NULL;
  20083. }
  20084. /******************************************************************************/
  20085. SOAP_FMAC1
  20086. void *
  20087. SOAP_FMAC2
  20088. soap_lookup_plugin(struct soap *soap, const char *id) {
  20089. return soap->fplugin(soap, id);
  20090. }
  20091. /******************************************************************************/
  20092. #ifdef __cplusplus
  20093. }
  20094. #endif
  20095. /******************************************************************************\
  20096. *
  20097. * C++ soap struct methods
  20098. *
  20099. \******************************************************************************/
  20100. #ifdef __cplusplus
  20101. soap::soap() {
  20102. soap_init(this);
  20103. /* no logs to prevent DEBUG mode leaks when the user calls a soap_init() on this context */
  20104. soap_set_test_logfile(this, NULL);
  20105. soap_set_sent_logfile(this, NULL);
  20106. soap_set_recv_logfile(this, NULL);
  20107. }
  20108. #endif
  20109. /******************************************************************************/
  20110. #ifdef __cplusplus
  20111. soap::soap(soap_mode m) {
  20112. soap_init1(this, m);
  20113. }
  20114. #endif
  20115. /******************************************************************************/
  20116. #ifdef __cplusplus
  20117. soap::soap(soap_mode im, soap_mode om) {
  20118. soap_init2(this, im, om);
  20119. }
  20120. #endif
  20121. /******************************************************************************/
  20122. #ifdef __cplusplus
  20123. soap::soap(const struct soap &soap) {
  20124. soap_copy_context(this, &soap);
  20125. }
  20126. #endif
  20127. /******************************************************************************/
  20128. #ifdef __cplusplus
  20129. struct soap &soap::operator=(const struct soap &soap) {
  20130. soap_done(this);
  20131. soap_copy_context(this, &soap);
  20132. return *this;
  20133. }
  20134. #endif
  20135. /******************************************************************************/
  20136. #ifdef __cplusplus
  20137. void soap::destroy() {
  20138. soap_destroy(this);
  20139. soap_end(this);
  20140. }
  20141. #endif
  20142. /******************************************************************************/
  20143. #ifdef __cplusplus
  20144. soap::~soap() {
  20145. soap_done(this);
  20146. }
  20147. #endif
  20148. /******************************************************************************/