stdsoap2.cpp 706 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966179671796817969179701797117972179731797417975179761797717978179791798017981179821798317984179851798617987179881798917990179911799217993179941799517996179971799817999180001800118002180031800418005180061800718008180091801018011180121801318014180151801618017180181801918020180211802218023180241802518026180271802818029180301803118032180331803418035180361803718038180391804018041180421804318044180451804618047180481804918050180511805218053180541805518056180571805818059180601806118062180631806418065180661806718068180691807018071180721807318074180751807618077180781807918080180811808218083180841808518086180871808818089180901809118092180931809418095180961809718098180991810018101181021810318104181051810618107181081810918110181111811218113181141811518116181171811818119181201812118122181231812418125181261812718128181291813018131181321813318134181351813618137181381813918140181411814218143181441814518146181471814818149181501815118152181531815418155181561815718158181591816018161181621816318164181651816618167181681816918170181711817218173181741817518176181771817818179181801818118182181831818418185181861818718188181891819018191181921819318194181951819618197181981819918200182011820218203182041820518206182071820818209182101821118212182131821418215182161821718218182191822018221182221822318224182251822618227182281822918230182311823218233182341823518236182371823818239182401824118242182431824418245182461824718248182491825018251182521825318254182551825618257182581825918260182611826218263182641826518266182671826818269182701827118272182731827418275182761827718278182791828018281182821828318284182851828618287182881828918290182911829218293182941829518296182971829818299183001830118302183031830418305183061830718308183091831018311183121831318314183151831618317183181831918320183211832218323183241832518326183271832818329183301833118332183331833418335183361833718338183391834018341183421834318344183451834618347183481834918350183511835218353183541835518356183571835818359183601836118362183631836418365183661836718368183691837018371183721837318374183751837618377183781837918380183811838218383183841838518386183871838818389183901839118392183931839418395183961839718398183991840018401184021840318404184051840618407184081840918410184111841218413184141841518416184171841818419184201842118422184231842418425184261842718428184291843018431184321843318434184351843618437184381843918440184411844218443184441844518446184471844818449184501845118452184531845418455184561845718458184591846018461184621846318464184651846618467184681846918470184711847218473184741847518476184771847818479184801848118482184831848418485184861848718488184891849018491184921849318494184951849618497184981849918500185011850218503185041850518506185071850818509185101851118512185131851418515185161851718518185191852018521185221852318524185251852618527185281852918530185311853218533185341853518536185371853818539185401854118542185431854418545185461854718548185491855018551185521855318554185551855618557185581855918560185611856218563185641856518566185671856818569185701857118572185731857418575185761857718578185791858018581185821858318584185851858618587185881858918590185911859218593185941859518596185971859818599186001860118602186031860418605186061860718608186091861018611186121861318614186151861618617186181861918620186211862218623186241862518626186271862818629186301863118632186331863418635186361863718638186391864018641186421864318644186451864618647186481864918650186511865218653186541865518656186571865818659186601866118662186631866418665186661866718668186691867018671186721867318674186751867618677186781867918680186811868218683186841868518686186871868818689186901869118692186931869418695186961869718698186991870018701187021870318704187051870618707187081870918710187111871218713187141871518716187171871818719187201872118722187231872418725187261872718728187291873018731187321873318734187351873618737187381873918740187411874218743187441874518746187471874818749187501875118752187531875418755187561875718758187591876018761187621876318764187651876618767187681876918770187711877218773187741877518776187771877818779187801878118782187831878418785187861878718788187891879018791187921879318794187951879618797187981879918800188011880218803188041880518806188071880818809188101881118812188131881418815188161881718818188191882018821188221882318824188251882618827188281882918830188311883218833188341883518836188371883818839188401884118842188431884418845188461884718848188491885018851188521885318854188551885618857188581885918860188611886218863188641886518866188671886818869188701887118872188731887418875188761887718878188791888018881188821888318884188851888618887188881888918890188911889218893188941889518896188971889818899189001890118902189031890418905189061890718908189091891018911189121891318914189151891618917189181891918920189211892218923189241892518926189271892818929189301893118932189331893418935189361893718938189391894018941189421894318944189451894618947189481894918950189511895218953189541895518956189571895818959189601896118962189631896418965189661896718968189691897018971189721897318974189751897618977189781897918980189811898218983189841898518986189871898818989189901899118992189931899418995189961899718998189991900019001190021900319004190051900619007190081900919010190111901219013190141901519016190171901819019190201902119022190231902419025190261902719028190291903019031190321903319034190351903619037190381903919040190411904219043190441904519046190471904819049190501905119052190531905419055190561905719058190591906019061190621906319064190651906619067190681906919070190711907219073190741907519076190771907819079190801908119082190831908419085190861908719088190891909019091190921909319094190951909619097190981909919100191011910219103191041910519106191071910819109191101911119112191131911419115191161911719118191191912019121191221912319124191251912619127191281912919130191311913219133191341913519136191371913819139191401914119142191431914419145191461914719148191491915019151191521915319154191551915619157191581915919160191611916219163191641916519166191671916819169191701917119172191731917419175191761917719178191791918019181191821918319184191851918619187191881918919190191911919219193191941919519196191971919819199192001920119202192031920419205192061920719208192091921019211192121921319214192151921619217192181921919220192211922219223192241922519226192271922819229192301923119232192331923419235192361923719238192391924019241192421924319244192451924619247192481924919250192511925219253192541925519256192571925819259192601926119262192631926419265192661926719268192691927019271192721927319274192751927619277192781927919280192811928219283192841928519286192871928819289192901929119292192931929419295192961929719298192991930019301193021930319304193051930619307193081930919310193111931219313193141931519316193171931819319193201932119322193231932419325193261932719328193291933019331193321933319334193351933619337193381933919340193411934219343193441934519346193471934819349193501935119352193531935419355193561935719358193591936019361193621936319364193651936619367193681936919370193711937219373193741937519376193771937819379193801938119382193831938419385193861938719388193891939019391193921939319394193951939619397193981939919400194011940219403194041940519406194071940819409194101941119412194131941419415194161941719418194191942019421194221942319424194251942619427194281942919430194311943219433194341943519436194371943819439194401944119442194431944419445194461944719448194491945019451194521945319454194551945619457194581945919460194611946219463194641946519466194671946819469194701947119472194731947419475194761947719478194791948019481194821948319484194851948619487194881948919490194911949219493194941949519496194971949819499195001950119502195031950419505195061950719508195091951019511195121951319514195151951619517195181951919520195211952219523195241952519526195271952819529195301953119532195331953419535195361953719538195391954019541195421954319544195451954619547195481954919550195511955219553195541955519556195571955819559195601956119562195631956419565195661956719568195691957019571195721957319574195751957619577195781957919580195811958219583195841958519586195871958819589195901959119592195931959419595195961959719598195991960019601196021960319604196051960619607196081960919610196111961219613196141961519616196171961819619196201962119622196231962419625196261962719628196291963019631196321963319634196351963619637196381963919640196411964219643196441964519646196471964819649196501965119652196531965419655196561965719658196591966019661196621966319664196651966619667196681966919670196711967219673196741967519676196771967819679196801968119682196831968419685196861968719688196891969019691196921969319694196951969619697196981969919700197011970219703197041970519706197071970819709197101971119712197131971419715197161971719718197191972019721197221972319724197251972619727197281972919730197311973219733197341973519736197371973819739197401974119742197431974419745197461974719748197491975019751197521975319754197551975619757197581975919760197611976219763197641976519766197671976819769197701977119772197731977419775197761977719778197791978019781197821978319784197851978619787197881978919790197911979219793197941979519796197971979819799198001980119802198031980419805198061980719808198091981019811198121981319814198151981619817198181981919820198211982219823198241982519826198271982819829198301983119832198331983419835198361983719838198391984019841198421984319844198451984619847198481984919850198511985219853198541985519856198571985819859198601986119862198631986419865198661986719868198691987019871198721987319874198751987619877198781987919880198811988219883198841988519886198871988819889198901989119892198931989419895198961989719898198991990019901199021990319904199051990619907199081990919910199111991219913199141991519916199171991819919199201992119922199231992419925199261992719928199291993019931199321993319934199351993619937199381993919940199411994219943199441994519946199471994819949199501995119952199531995419955199561995719958199591996019961199621996319964199651996619967199681996919970199711997219973199741997519976199771997819979199801998119982199831998419985199861998719988199891999019991199921999319994199951999619997199981999920000200012000220003200042000520006200072000820009200102001120012200132001420015200162001720018200192002020021200222002320024200252002620027200282002920030200312003220033200342003520036200372003820039200402004120042200432004420045200462004720048200492005020051200522005320054200552005620057200582005920060200612006220063200642006520066200672006820069200702007120072200732007420075200762007720078200792008020081200822008320084200852008620087200882008920090200912009220093200942009520096200972009820099201002010120102201032010420105201062010720108201092011020111201122011320114201152011620117201182011920120201212012220123201242012520126201272012820129201302013120132201332013420135201362013720138201392014020141201422014320144201452014620147201482014920150201512015220153201542015520156201572015820159201602016120162201632016420165201662016720168201692017020171201722017320174201752017620177201782017920180201812018220183201842018520186201872018820189201902019120192201932019420195201962019720198201992020020201202022020320204202052020620207202082020920210202112021220213202142021520216202172021820219202202022120222202232022420225202262022720228202292023020231202322023320234202352023620237202382023920240202412024220243202442024520246202472024820249202502025120252202532025420255202562025720258202592026020261202622026320264202652026620267202682026920270202712027220273202742027520276202772027820279202802028120282202832028420285202862028720288202892029020291202922029320294202952029620297202982029920300203012030220303203042030520306203072030820309203102031120312203132031420315203162031720318203192032020321203222032320324203252032620327203282032920330203312033220333203342033520336203372033820339203402034120342203432034420345203462034720348203492035020351203522035320354203552035620357203582035920360203612036220363203642036520366203672036820369203702037120372203732037420375203762037720378203792038020381203822038320384203852038620387203882038920390203912039220393203942039520396203972039820399204002040120402204032040420405204062040720408204092041020411204122041320414204152041620417204182041920420204212042220423204242042520426204272042820429204302043120432204332043420435204362043720438204392044020441204422044320444204452044620447204482044920450204512045220453204542045520456204572045820459204602046120462204632046420465204662046720468204692047020471204722047320474204752047620477204782047920480204812048220483204842048520486204872048820489204902049120492204932049420495204962049720498204992050020501205022050320504205052050620507205082050920510205112051220513205142051520516205172051820519205202052120522205232052420525205262052720528205292053020531205322053320534205352053620537205382053920540205412054220543205442054520546205472054820549205502055120552205532055420555205562055720558205592056020561205622056320564205652056620567205682056920570205712057220573205742057520576205772057820579205802058120582205832058420585205862058720588205892059020591205922059320594205952059620597205982059920600206012060220603206042060520606206072060820609206102061120612206132061420615206162061720618206192062020621206222062320624206252062620627206282062920630206312063220633206342063520636206372063820639206402064120642206432064420645206462064720648206492065020651206522065320654206552065620657206582065920660206612066220663206642066520666206672066820669206702067120672206732067420675206762067720678206792068020681206822068320684206852068620687206882068920690206912069220693206942069520696206972069820699207002070120702207032070420705207062070720708207092071020711207122071320714207152071620717207182071920720207212072220723207242072520726207272072820729207302073120732207332073420735207362073720738207392074020741207422074320744207452074620747207482074920750207512075220753207542075520756207572075820759207602076120762207632076420765207662076720768207692077020771207722077320774207752077620777207782077920780207812078220783207842078520786207872078820789207902079120792207932079420795207962079720798207992080020801208022080320804208052080620807208082080920810208112081220813208142081520816208172081820819208202082120822208232082420825208262082720828208292083020831208322083320834208352083620837208382083920840208412084220843208442084520846208472084820849208502085120852208532085420855208562085720858208592086020861208622086320864208652086620867208682086920870208712087220873208742087520876208772087820879208802088120882208832088420885208862088720888208892089020891208922089320894208952089620897208982089920900209012090220903209042090520906209072090820909209102091120912209132091420915209162091720918209192092020921209222092320924209252092620927209282092920930209312093220933209342093520936209372093820939209402094120942209432094420945209462094720948209492095020951209522095320954209552095620957209582095920960209612096220963209642096520966209672096820969209702097120972209732097420975209762097720978209792098020981209822098320984209852098620987209882098920990209912099220993209942099520996209972099820999210002100121002210032100421005210062100721008210092101021011210122101321014210152101621017210182101921020210212102221023210242102521026210272102821029210302103121032210332103421035210362103721038210392104021041210422104321044210452104621047210482104921050210512105221053210542105521056210572105821059210602106121062210632106421065210662106721068210692107021071210722107321074210752107621077210782107921080210812108221083210842108521086210872108821089210902109121092210932109421095210962109721098210992110021101211022110321104
  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. soap->error = status;
  6485. return soap->error = status;
  6486. }
  6487. /******************************************************************************/
  6488. SOAP_FMAC1
  6489. int
  6490. SOAP_FMAC2
  6491. soap_force_closesock(struct soap *soap) {
  6492. soap->keep_alive = 0;
  6493. if (soap_valid_socket(soap->socket) && soap->fclosesocket) {
  6494. soap->fclosesocket(soap, soap->socket);
  6495. soap->socket = SOAP_INVALID_SOCKET;
  6496. }
  6497. return soap->error;
  6498. }
  6499. /******************************************************************************/
  6500. #ifdef WITH_SELF_PIPE
  6501. SOAP_FMAC1
  6502. void
  6503. SOAP_FMAC2
  6504. soap_close_connection(struct soap *soap)
  6505. {
  6506. if (soap_valid_socket(soap->socket))
  6507. write(soap->pipe_fd[1], "1", 1);
  6508. }
  6509. #endif
  6510. /******************************************************************************/
  6511. #ifndef WITH_NOIO
  6512. SOAP_FMAC1
  6513. void
  6514. SOAP_FMAC2
  6515. soap_cleanup(struct soap *soap) {
  6516. soap_done(soap);
  6517. #ifdef WIN32
  6518. if (!tcp_done)
  6519. return;
  6520. tcp_done = 0;
  6521. WSACleanup();
  6522. #endif
  6523. }
  6524. #endif
  6525. /******************************************************************************/
  6526. SOAP_FMAC1
  6527. void
  6528. SOAP_FMAC2
  6529. soap_done(struct soap *soap) {
  6530. #ifdef SOAP_DEBUG
  6531. int i;
  6532. #endif
  6533. if (soap_check_state(soap))
  6534. return;
  6535. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Done with context%s\n", soap->state == SOAP_COPY ? " copy" : ""));
  6536. soap_free_temp(soap);
  6537. while (soap->clist) {
  6538. struct soap_clist *p = soap->clist->next;
  6539. SOAP_FREE(soap, soap->clist);
  6540. soap->clist = p;
  6541. }
  6542. if (soap->state == SOAP_INIT)
  6543. soap->omode &= ~SOAP_IO_UDP; /* to force close the socket */
  6544. soap->keep_alive = 0; /* to force close the socket */
  6545. if (soap->master == soap->socket) /* do not close twice */
  6546. soap->master = SOAP_INVALID_SOCKET;
  6547. soap_closesock(soap);
  6548. #ifdef WITH_COOKIES
  6549. soap_free_cookies(soap);
  6550. #endif
  6551. while (soap->plugins) {
  6552. struct soap_plugin *p = soap->plugins->next;
  6553. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Removing plugin '%s'\n", soap->plugins->id));
  6554. if (soap->plugins->fcopy || soap->state == SOAP_INIT)
  6555. soap->plugins->fdelete(soap, soap->plugins);
  6556. SOAP_FREE(soap, soap->plugins);
  6557. soap->plugins = p;
  6558. }
  6559. soap->fplugin = fplugin;
  6560. #ifndef WITH_NOHTTP
  6561. soap->fpost = http_post;
  6562. soap->fget = http_get;
  6563. soap->fput = http_put;
  6564. soap->fpatch = http_patch;
  6565. soap->fdel = http_del;
  6566. soap->fopt = http_200;
  6567. soap->fhead = http_200;
  6568. soap->fform = NULL;
  6569. soap->fposthdr = http_post_header;
  6570. soap->fresponse = http_response;
  6571. soap->fparse = http_parse;
  6572. soap->fparsehdr = http_parse_header;
  6573. #endif
  6574. soap->fheader = NULL;
  6575. #ifndef WITH_NOIO
  6576. #ifndef WITH_IPV6
  6577. soap->fresolve = tcp_gethost;
  6578. #else
  6579. soap->fresolve = NULL;
  6580. #endif
  6581. soap->faccept = tcp_accept;
  6582. soap->fopen = tcp_connect;
  6583. soap->fclose = tcp_disconnect;
  6584. soap->fclosesocket = tcp_closesocket;
  6585. soap->fshutdownsocket = tcp_shutdownsocket;
  6586. soap->fsend = fsend;
  6587. soap->frecv = frecv;
  6588. soap->fpoll = soap_poll;
  6589. #else
  6590. soap->fopen = NULL;
  6591. soap->fclose = NULL;
  6592. soap->fpoll = NULL;
  6593. #endif
  6594. #ifndef WITH_LEANER
  6595. soap->fsvalidate = NULL;
  6596. soap->fwvalidate = NULL;
  6597. soap->feltbegin = NULL;
  6598. soap->feltendin = NULL;
  6599. soap->feltbegout = NULL;
  6600. soap->feltendout = NULL;
  6601. soap->fprepareinitsend = NULL;
  6602. soap->fprepareinitrecv = NULL;
  6603. soap->fpreparesend = NULL;
  6604. soap->fpreparerecv = NULL;
  6605. soap->fpreparefinalsend = NULL;
  6606. soap->fpreparefinalrecv = NULL;
  6607. soap->ffiltersend = NULL;
  6608. soap->ffilterrecv = NULL;
  6609. #endif
  6610. soap->fseterror = NULL;
  6611. soap->fignore = NULL;
  6612. soap->fserveloop = NULL;
  6613. #ifdef WITH_OPENSSL
  6614. if (soap->session)
  6615. {
  6616. SSL_SESSION_free(soap->session);
  6617. soap->session = NULL;
  6618. }
  6619. #endif
  6620. if (soap->state == SOAP_INIT) {
  6621. if (soap_valid_socket(soap->master)) {
  6622. soap->fclosesocket(soap, soap->master);
  6623. soap->master = SOAP_INVALID_SOCKET;
  6624. }
  6625. }
  6626. #ifdef WITH_OPENSSL
  6627. if (soap->ssl)
  6628. {
  6629. SSL_free(soap->ssl);
  6630. soap->ssl = NULL;
  6631. }
  6632. if (soap->state == SOAP_INIT)
  6633. {
  6634. if (soap->ctx)
  6635. {
  6636. SSL_CTX_free(soap->ctx);
  6637. soap->ctx = NULL;
  6638. }
  6639. }
  6640. ERR_clear_error();
  6641. # if OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(LIBRESSL_VERSION_NUMBER)
  6642. /* OpenSSL libraries handle thread init and deinit */
  6643. # elif OPENSSL_VERSION_NUMBER >= 0x10000000L
  6644. ERR_remove_thread_state(NULL);
  6645. # else
  6646. ERR_remove_state(0);
  6647. # endif
  6648. #endif
  6649. #ifdef WITH_GNUTLS
  6650. if (soap->state == SOAP_INIT)
  6651. {
  6652. if (soap->xcred)
  6653. {
  6654. gnutls_certificate_free_credentials(soap->xcred);
  6655. soap->xcred = NULL;
  6656. }
  6657. if (soap->acred)
  6658. {
  6659. gnutls_anon_free_client_credentials(soap->acred);
  6660. soap->acred = NULL;
  6661. }
  6662. if (soap->cache)
  6663. {
  6664. gnutls_priority_deinit(soap->cache);
  6665. soap->cache = NULL;
  6666. }
  6667. if (soap->dh_params)
  6668. {
  6669. gnutls_dh_params_deinit(soap->dh_params);
  6670. soap->dh_params = NULL;
  6671. }
  6672. # if GNUTLS_VERSION_NUMBER < 0x030300
  6673. if (soap->rsa_params)
  6674. {
  6675. gnutls_rsa_params_deinit(soap->rsa_params);
  6676. soap->rsa_params = NULL;
  6677. }
  6678. #endif
  6679. }
  6680. if (soap->session)
  6681. {
  6682. gnutls_deinit(soap->session);
  6683. soap->session = NULL;
  6684. }
  6685. #endif
  6686. #ifdef WITH_SYSTEMSSL
  6687. if (soap->ssl)
  6688. gsk_secure_socket_close(&soap->ssl);
  6689. if (soap->state == SOAP_INIT)
  6690. if (soap->ctx)
  6691. gsk_environment_close(&soap->ctx);
  6692. #endif
  6693. #ifdef WITH_C_LOCALE
  6694. SOAP_FREELOCALE(soap);
  6695. #endif
  6696. #ifdef WITH_ZLIB
  6697. if (soap->d_stream)
  6698. {
  6699. SOAP_FREE(soap, soap->d_stream);
  6700. soap->d_stream = NULL;
  6701. }
  6702. if (soap->z_buf)
  6703. {
  6704. SOAP_FREE(soap, soap->z_buf);
  6705. soap->z_buf = NULL;
  6706. }
  6707. #endif
  6708. #ifdef SOAP_DEBUG
  6709. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Free logfiles\n"));
  6710. for (i = 0; i < SOAP_MAXLOGS; i++)
  6711. {
  6712. soap_close_logfile(soap, i);
  6713. if (soap->logfile[i])
  6714. {
  6715. SOAP_FREE_UNMANAGED(soap->logfile[i]);
  6716. soap->logfile[i] = NULL;
  6717. }
  6718. }
  6719. #endif
  6720. #ifdef WITH_SELF_PIPE
  6721. close(soap->pipe_fd[0]);
  6722. close(soap->pipe_fd[1]);
  6723. #endif
  6724. #ifdef SOAP_MEM_DEBUG
  6725. soap_free_mht(soap);
  6726. #endif
  6727. soap->state = SOAP_NONE;
  6728. }
  6729. /******************************************************************************\
  6730. *
  6731. * HTTP
  6732. *
  6733. \******************************************************************************/
  6734. #ifndef WITH_NOHTTP
  6735. static int
  6736. http_parse(struct soap *soap) {
  6737. char header[SOAP_HDRLEN], *s;
  6738. int err = SOAP_OK;
  6739. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Waiting for HTTP request/response...\n"));
  6740. soap->fform = NULL;
  6741. *soap->endpoint = '\0';
  6742. soap->bearer = NULL;
  6743. #ifdef WITH_NTLM
  6744. if (!soap->ntlm_challenge)
  6745. #endif
  6746. {
  6747. soap->userid = NULL;
  6748. soap->passwd = NULL;
  6749. soap->authrealm = NULL;
  6750. }
  6751. #ifdef WITH_NTLM
  6752. soap->ntlm_challenge = NULL;
  6753. #endif
  6754. soap->proxy_from = NULL;
  6755. soap->cors_origin = NULL;
  6756. soap->cors_method = NULL;
  6757. soap->cors_header = NULL;
  6758. do {
  6759. soap->length = 0;
  6760. soap->http_content = NULL;
  6761. soap->action = NULL;
  6762. soap->status = 0;
  6763. soap->body = 1;
  6764. if (soap_getline(soap, soap->msgbuf, sizeof(soap->msgbuf))) {
  6765. if (soap->error == SOAP_EOF)
  6766. return SOAP_EOF;
  6767. return soap->error = 414;
  6768. }
  6769. s = strchr(soap->msgbuf, ' ');
  6770. if (s) {
  6771. soap->status = (unsigned short) soap_strtoul(s, &s, 10);
  6772. if (!soap_coblank((soap_wchar) *s))
  6773. soap->status = 0;
  6774. }
  6775. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "HTTP status: %s\n", soap->msgbuf));
  6776. for (;;) {
  6777. if (soap_getline(soap, header, SOAP_HDRLEN)) {
  6778. if (soap->error == SOAP_EOF) {
  6779. soap->error = SOAP_OK;
  6780. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "EOF in HTTP header, try to continue anyway\n"));
  6781. break;
  6782. }
  6783. return soap->error;
  6784. }
  6785. if (!*header)
  6786. break;
  6787. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "HTTP header: %s\n", header));
  6788. s = strchr(header, ':');
  6789. if (s) {
  6790. char *t;
  6791. *s = '\0';
  6792. do {
  6793. s++;
  6794. } while (*s && *s <= 32);
  6795. if (*s == '"')
  6796. s++;
  6797. t = s + strlen(s) - 1;
  6798. while (t > s && *t <= 32)
  6799. t--;
  6800. if (t >= s && *t == '"')
  6801. t--;
  6802. t[1] = '\0';
  6803. soap->error = soap->fparsehdr(soap, header, s);
  6804. if (soap->error) {
  6805. if (soap->error < SOAP_STOP)
  6806. return soap->error;
  6807. err = soap->error;
  6808. soap->error = SOAP_OK;
  6809. }
  6810. }
  6811. }
  6812. } while (soap->status == 100);
  6813. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Finished HTTP header parsing, status = %d\n", soap->status));
  6814. s = strstr(soap->msgbuf, "HTTP/");
  6815. if (s && s[5] == '1' && s[6] == '.' && s[7] == '0') {
  6816. soap->keep_alive = 0; /* HTTP 1.0 does not support keep-alive */
  6817. if (soap->status == 0 && (soap->omode & SOAP_IO) == SOAP_IO_CHUNK) /* soap->status == 0 for HTTP request */
  6818. soap->omode = (soap->omode & ~SOAP_IO) | SOAP_IO_STORE; /* HTTP 1.0 does not support chunked transfers */
  6819. }
  6820. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Keep alive connection = %d\n", soap->keep_alive));
  6821. if (soap->status == 0) {
  6822. size_t l = 0;
  6823. if (s) {
  6824. if (!strncmp(soap->msgbuf, "POST ", l = 5))
  6825. soap->status = SOAP_POST;
  6826. else if (!strncmp(soap->msgbuf, "GET ", l = 4))
  6827. soap->status = SOAP_GET;
  6828. else if (!strncmp(soap->msgbuf, "PUT ", l = 4))
  6829. soap->status = SOAP_PUT;
  6830. else if (!strncmp(soap->msgbuf, "PATCH ", l = 4))
  6831. soap->status = SOAP_PATCH;
  6832. else if (!strncmp(soap->msgbuf, "DELETE ", l = 7))
  6833. soap->status = SOAP_DEL;
  6834. else if (!strncmp(soap->msgbuf, "HEAD ", l = 5))
  6835. soap->status = SOAP_HEAD;
  6836. else if (!strncmp(soap->msgbuf, "OPTIONS ", l = 8))
  6837. soap->status = SOAP_OPTIONS;
  6838. }
  6839. if (s && soap->status) {
  6840. size_t m, n, k;
  6841. int r;
  6842. while (soap->msgbuf[l] && soap_coblank((soap_wchar) soap->msgbuf[l]))
  6843. l++;
  6844. m = strlen(soap->endpoint);
  6845. n = m + (s - soap->msgbuf) - l - 1;
  6846. if (n >= sizeof(soap->endpoint))
  6847. n = sizeof(soap->endpoint) - 1;
  6848. if (m > n)
  6849. m = n;
  6850. k = n - m + 1;
  6851. if (k >= sizeof(soap->path))
  6852. k = sizeof(soap->path) - 1;
  6853. while (k > 0 && soap_coblank((soap_wchar) soap->msgbuf[l + k - 1]))
  6854. k--;
  6855. if (soap_strncpy(soap->path, sizeof(soap->path), soap->msgbuf + l, k))
  6856. return soap->error = 414;
  6857. if (*soap->path && *soap->path != '/')
  6858. r = soap_strncpy(soap->endpoint, sizeof(soap->endpoint), soap->path, k);
  6859. else
  6860. r = soap_strncat(soap->endpoint, sizeof(soap->endpoint), soap->path, k);
  6861. if (r)
  6862. return soap->error = 414;
  6863. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Target endpoint='%s' path='%s'\n", soap->endpoint, soap->path));
  6864. if (err)
  6865. return soap->error = err;
  6866. } else if (err) {
  6867. return soap->error = err;
  6868. } else if (s) {
  6869. return soap->error = 405;
  6870. }
  6871. return SOAP_OK;
  6872. }
  6873. if ((soap->status >= 200 && soap->status <= 299) /* OK, Accepted, etc */
  6874. || soap->status == 400 /* Bad Request */
  6875. || soap->status == 500) /* Internal Server Error */
  6876. return soap->error = SOAP_OK;
  6877. return soap->error = soap->status;
  6878. }
  6879. #endif
  6880. /******************************************************************************/
  6881. #ifndef WITH_NOHTTP
  6882. static int
  6883. http_parse_header(struct soap *soap, const char *key, const char *val) {
  6884. if (!soap_tag_cmp(key, "Host")) {
  6885. #if defined(WITH_OPENSSL) || defined(WITH_GNUTLS)
  6886. if ((soap->imode & SOAP_ENC_SSL))
  6887. soap_strcpy(soap->endpoint, sizeof(soap->endpoint), "https://");
  6888. else
  6889. #endif
  6890. soap_strcpy(soap->endpoint, sizeof(soap->endpoint), "http://");
  6891. if (soap_strncat(soap->endpoint, sizeof(soap->endpoint), val, sizeof(soap->endpoint) - 9))
  6892. return soap->error = SOAP_HDR;
  6893. }
  6894. #ifndef WITH_LEANER
  6895. else if (!soap_tag_cmp(key, "Content-Type")) {
  6896. const char *action;
  6897. soap->http_content = soap_strdup(soap, val);
  6898. if (soap_http_header_attribute(soap, val, "application/dime"))
  6899. soap->imode |= SOAP_ENC_DIME;
  6900. else if (soap_http_header_attribute(soap, val, "multipart/related")
  6901. || soap_http_header_attribute(soap, val, "multipart/form-data")) {
  6902. const char *type;
  6903. soap->mime.boundary = soap_strdup(soap, soap_http_header_attribute(soap, val, "boundary"));
  6904. soap->mime.start = soap_strdup(soap, soap_http_header_attribute(soap, val, "start"));
  6905. soap->imode |= SOAP_ENC_MIME;
  6906. type = soap_http_header_attribute(soap, val, "type");
  6907. if (type && !strcmp(type, "application/xop+xml"))
  6908. soap->imode |= SOAP_ENC_MTOM;
  6909. }
  6910. action = soap_http_header_attribute(soap, val, "action");
  6911. if (action) {
  6912. if (*action == '"') {
  6913. soap->action = soap_strdup(soap, action + 1);
  6914. if (soap->action && *soap->action)
  6915. soap->action[strlen(soap->action) - 1] = '\0';
  6916. } else
  6917. soap->action = soap_strdup(soap, action);
  6918. }
  6919. }
  6920. #endif
  6921. else if (!soap_tag_cmp(key, "Content-Length")) {
  6922. soap->length = soap_strtoull(val, NULL, 10);
  6923. if (soap->length == 0)
  6924. soap->body = 0;
  6925. } else if (!soap_tag_cmp(key, "Content-Encoding")) {
  6926. if (!soap_tag_cmp(val, "deflate"))
  6927. #ifdef WITH_ZLIB
  6928. soap->zlib_in = SOAP_ZLIB_DEFLATE;
  6929. #else
  6930. return SOAP_ZLIB_ERROR;
  6931. #endif
  6932. else if (!soap_tag_cmp(val, "gzip"))
  6933. #ifdef WITH_GZIP
  6934. soap->zlib_in = SOAP_ZLIB_GZIP;
  6935. #else
  6936. return SOAP_ZLIB_ERROR;
  6937. #endif
  6938. }
  6939. #ifdef WITH_ZLIB
  6940. else if (!soap_tag_cmp(key, "Accept-Encoding"))
  6941. {
  6942. #ifdef WITH_GZIP
  6943. if (strchr(val, '*') || soap_http_header_attribute(soap, val, "gzip"))
  6944. soap->zlib_out = SOAP_ZLIB_GZIP;
  6945. else
  6946. #endif
  6947. if (strchr(val, '*') || soap_http_header_attribute(soap, val, "deflate"))
  6948. soap->zlib_out = SOAP_ZLIB_DEFLATE;
  6949. else
  6950. soap->zlib_out = SOAP_ZLIB_NONE;
  6951. }
  6952. #endif
  6953. else if (!soap_tag_cmp(key, "Transfer-Encoding")) {
  6954. soap->imode &= ~SOAP_IO;
  6955. if (!soap_tag_cmp(val, "chunked"))
  6956. soap->imode |= SOAP_IO_CHUNK;
  6957. } else if (!soap_tag_cmp(key, "Connection")) {
  6958. if (!soap_tag_cmp(val, "close"))
  6959. soap->keep_alive = 0;
  6960. }
  6961. #if !defined(WITH_LEAN) || defined(WITH_NTLM)
  6962. else if (!soap_tag_cmp(key, "Authorization") || !soap_tag_cmp(key, "Proxy-Authorization")) {
  6963. #ifdef WITH_NTLM
  6964. if (!soap_tag_cmp(val, "NTLM*"))
  6965. {
  6966. soap->ntlm_challenge = soap_strdup(soap, val + 4);
  6967. }
  6968. else
  6969. #endif
  6970. if (!soap_tag_cmp(val, "Bearer *")) {
  6971. soap->bearer = soap_strdup(soap, val + 7);
  6972. } else if (!soap_tag_cmp(val, "Basic *")) {
  6973. int n;
  6974. char *s;
  6975. soap_base642s(soap, val + 6, soap->tmpbuf, sizeof(soap->tmpbuf) - 1, &n);
  6976. soap->tmpbuf[n] = '\0';
  6977. s = strchr(soap->tmpbuf, ':');
  6978. if (s) {
  6979. *s = '\0';
  6980. soap->userid = soap_strdup(soap, soap->tmpbuf);
  6981. soap->passwd = soap_strdup(soap, s + 1);
  6982. }
  6983. }
  6984. } else if (!soap_tag_cmp(key, "WWW-Authenticate") || !soap_tag_cmp(key, "Proxy-Authenticate")) {
  6985. #ifdef WITH_NTLM
  6986. if (!soap_tag_cmp(val, "NTLM*"))
  6987. soap->ntlm_challenge = soap_strdup(soap, val + 4);
  6988. else
  6989. #endif
  6990. soap->authrealm = soap_strdup(soap, soap_http_header_attribute(soap, val + 6, "realm"));
  6991. } else if (!soap_tag_cmp(key, "Expect")) {
  6992. if (!soap_tag_cmp(val, "100-continue")) {
  6993. if ((soap->error = soap->fposthdr(soap, "HTTP/1.1 100 Continue", NULL)) != SOAP_OK
  6994. || (soap->error = soap->fposthdr(soap, NULL, NULL)) != SOAP_OK)
  6995. return soap->error;
  6996. }
  6997. }
  6998. #endif
  6999. else if (!soap_tag_cmp(key, "SOAPAction")) {
  7000. if (*val == '"') {
  7001. soap->action = soap_strdup(soap, val + 1);
  7002. if (*soap->action)
  7003. soap->action[strlen(soap->action) - 1] = '\0';
  7004. } else
  7005. soap->action = soap_strdup(soap, val);
  7006. } else if (!soap_tag_cmp(key, "Location")) {
  7007. soap_strcpy(soap->endpoint, sizeof(soap->endpoint), val);
  7008. } else if (!soap_tag_cmp(key, "X-Forwarded-For")) {
  7009. soap->proxy_from = soap_strdup(soap, val);
  7010. } else if (!soap_tag_cmp(key, "Origin")) {
  7011. soap->origin = soap_strdup(soap, val);
  7012. soap->cors_origin = soap->cors_allow;
  7013. } else if (!soap_tag_cmp(key, "Access-Control-Request-Method")) {
  7014. soap->cors_method = soap_strdup(soap, val);
  7015. } else if (!soap_tag_cmp(key, "Access-Control-Request-Headers")) {
  7016. soap->cors_header = soap_strdup(soap, val);
  7017. }
  7018. #ifdef WITH_COOKIES
  7019. else if (!soap_tag_cmp(key, "Cookie")
  7020. || !soap_tag_cmp(key, "Cookie2")
  7021. || !soap_tag_cmp(key, "Set-Cookie")
  7022. || !soap_tag_cmp(key, "Set-Cookie2"))
  7023. {
  7024. soap_getcookies(soap, val);
  7025. }
  7026. #endif
  7027. return SOAP_OK;
  7028. }
  7029. #endif
  7030. /******************************************************************************/
  7031. #if !defined(WITH_NOHTTP) || !defined(WITH_LEANER)
  7032. SOAP_FMAC1
  7033. const char *
  7034. SOAP_FMAC2
  7035. soap_http_header_attribute(struct soap *soap, const char *line, const char *key) {
  7036. const char *s = line;
  7037. if (s) {
  7038. while (*s) {
  7039. short flag;
  7040. s = soap_decode_key(soap->tmpbuf, sizeof(soap->tmpbuf), s);
  7041. flag = soap_tag_cmp(soap->tmpbuf, key);
  7042. s = soap_decode_val(soap->tmpbuf, sizeof(soap->tmpbuf), s);
  7043. if (!flag)
  7044. return soap->tmpbuf;
  7045. }
  7046. }
  7047. return NULL;
  7048. }
  7049. #endif
  7050. /******************************************************************************/
  7051. #if !defined(WITH_NOHTTP) || !defined(WITH_LEANER)
  7052. SOAP_FMAC1
  7053. const char *
  7054. SOAP_FMAC2
  7055. soap_decode_key(char *buf, size_t len, const char *val) {
  7056. return soap_decode(buf, len, val, "=,;");
  7057. }
  7058. #endif
  7059. /******************************************************************************/
  7060. #if !defined(WITH_NOHTTP) || !defined(WITH_LEANER)
  7061. SOAP_FMAC1
  7062. const char *
  7063. SOAP_FMAC2
  7064. soap_decode_val(char *buf, size_t len, const char *val) {
  7065. if (*val != '=') {
  7066. *buf = '\0';
  7067. return val;
  7068. }
  7069. return soap_decode(buf, len, val + 1, ",;");
  7070. }
  7071. #endif
  7072. /******************************************************************************/
  7073. #if !defined(WITH_NOHTTP) || !defined(WITH_LEANER)
  7074. static const char *
  7075. soap_decode(char *buf, size_t len, const char *val, const char *sep) {
  7076. const char *s;
  7077. char *t = buf;
  7078. size_t i = len;
  7079. for (s = val; *s; s++)
  7080. if (*s != ' ' && *s != '\t' && !strchr(sep, *s))
  7081. break;
  7082. if (len > 0) {
  7083. if (*s == '"') {
  7084. s++;
  7085. while (*s && *s != '"' && --i)
  7086. *t++ = *s++;
  7087. } else {
  7088. while (*s && !strchr(sep, *s) && --i) {
  7089. if (*s == '%' && s[1] && s[2]) {
  7090. *t++ = ((s[1] >= 'A' ? (s[1] & 0x7) + 9 : s[1] - '0') << 4)
  7091. + (s[2] >= 'A' ? (s[2] & 0x7) + 9 : s[2] - '0');
  7092. s += 3;
  7093. } else
  7094. *t++ = *s++;
  7095. }
  7096. }
  7097. buf[len - 1] = '\0'; /* appease static checkers that get confused */
  7098. }
  7099. *t = '\0';
  7100. while (*s && !strchr(sep, *s))
  7101. s++;
  7102. return s;
  7103. }
  7104. #endif
  7105. /******************************************************************************/
  7106. #ifndef WITH_NOHTTP
  7107. static const char *
  7108. http_error(struct soap *soap, int status) {
  7109. const char *msg = SOAP_STR_EOS;
  7110. (void) soap;
  7111. (void) status;
  7112. #ifndef WITH_LEAN
  7113. msg = soap_code_str(h_http_error_codes, status);
  7114. if (!msg)
  7115. msg = SOAP_STR_EOS;
  7116. #endif
  7117. return msg;
  7118. }
  7119. #endif
  7120. /******************************************************************************/
  7121. #ifndef WITH_NOHTTP
  7122. static int
  7123. http_get(struct soap *soap) {
  7124. (void) soap;
  7125. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "HTTP GET request\n"));
  7126. return SOAP_GET_METHOD;
  7127. }
  7128. #endif
  7129. /******************************************************************************/
  7130. #ifndef WITH_NOHTTP
  7131. static int
  7132. http_put(struct soap *soap) {
  7133. (void) soap;
  7134. return SOAP_PUT_METHOD;
  7135. }
  7136. #endif
  7137. /******************************************************************************/
  7138. #ifndef WITH_NOHTTP
  7139. static int
  7140. http_patch(struct soap *soap) {
  7141. (void) soap;
  7142. return SOAP_PATCH_METHOD;
  7143. }
  7144. #endif
  7145. /******************************************************************************/
  7146. #ifndef WITH_NOHTTP
  7147. static int
  7148. http_del(struct soap *soap) {
  7149. (void) soap;
  7150. return SOAP_DEL_METHOD;
  7151. }
  7152. #endif
  7153. /******************************************************************************/
  7154. #ifndef WITH_NOHTTP
  7155. static int
  7156. http_200(struct soap *soap) {
  7157. if (soap->origin && soap->cors_method) /* CORS Origin and Access-Control-Request-Method headers */
  7158. {
  7159. soap->cors_origin = soap->cors_allow; /* modify this code or hook your own soap->fopt() callback with logic */
  7160. soap->cors_methods = "GET, PUT, PATCH, POST, HEAD, OPTIONS";
  7161. soap->cors_headers = soap->cors_header;
  7162. }
  7163. return soap_send_empty_response(soap, 200);
  7164. }
  7165. #endif
  7166. /******************************************************************************/
  7167. #ifndef WITH_NOHTTP
  7168. static int
  7169. http_post(struct soap *soap, const char *endpoint, const char *host, int port, const char *path, const char *action,
  7170. ULONG64 count) {
  7171. const char *s;
  7172. int err;
  7173. size_t l;
  7174. switch (soap->status) {
  7175. case SOAP_GET:
  7176. s = "GET";
  7177. break;
  7178. case SOAP_PUT:
  7179. s = "PUT";
  7180. break;
  7181. case SOAP_PATCH:
  7182. s = "PATCH";
  7183. break;
  7184. case SOAP_DEL:
  7185. s = "DELETE";
  7186. break;
  7187. case SOAP_CONNECT:
  7188. s = "CONNECT";
  7189. break;
  7190. case SOAP_HEAD:
  7191. s = "HEAD";
  7192. break;
  7193. case SOAP_OPTIONS:
  7194. s = "OPTIONS";
  7195. break;
  7196. default:
  7197. s = "POST";
  7198. }
  7199. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "HTTP %s to %s\n", s, endpoint ? endpoint : "(null)"));
  7200. if (!endpoint ||
  7201. (soap_tag_cmp(endpoint, "http:*") && soap_tag_cmp(endpoint, "https:*") && soap_tag_cmp(endpoint, "httpg:*")))
  7202. return SOAP_OK;
  7203. /* set l to prevent overruns ('host' and 'soap->host' are substrings of 'endpoint') */
  7204. l = strlen(endpoint) + strlen(soap->http_version) + 80;
  7205. if (l > sizeof(soap->tmpbuf))
  7206. return soap->error = SOAP_EOM;
  7207. if (soap->status == SOAP_CONNECT)
  7208. (SOAP_SNPRINTF(soap->tmpbuf, sizeof(soap->tmpbuf), l), "%s %s:%d HTTP/%s", s, soap->host, soap->port,
  7209. soap->http_version);
  7210. else if (soap->proxy_host && endpoint)
  7211. (SOAP_SNPRINTF(soap->tmpbuf, sizeof(soap->tmpbuf), l), "%s %s HTTP/%s", s, endpoint, soap->http_version);
  7212. else
  7213. (SOAP_SNPRINTF(soap->tmpbuf, sizeof(soap->tmpbuf), l), "%s /%s HTTP/%s", s, (*path == '/' ? path + 1 : path),
  7214. soap->http_version);
  7215. err = soap->fposthdr(soap, soap->tmpbuf, NULL);
  7216. if (err)
  7217. return err;
  7218. #ifdef WITH_OPENSSL
  7219. if ((soap->ssl && port != 443) || (!soap->ssl && port != 80))
  7220. #else
  7221. if (port != 80)
  7222. #endif
  7223. {
  7224. #ifdef WITH_IPV6
  7225. if (*host != '[' && strchr(host, ':'))
  7226. (SOAP_SNPRINTF(soap->tmpbuf, sizeof(soap->tmpbuf), l), "[%s]:%d", host, port); /* RFC 2732 */
  7227. else
  7228. #endif
  7229. (SOAP_SNPRINTF(soap->tmpbuf, sizeof(soap->tmpbuf), l), "%s:%d", host, port);
  7230. } else {
  7231. #ifdef WITH_IPV6
  7232. if (*host != '[' && strchr(host, ':'))
  7233. (SOAP_SNPRINTF(soap->tmpbuf, sizeof(soap->tmpbuf), l), "[%s]", host); /* RFC 2732 */
  7234. else
  7235. #endif
  7236. soap_strcpy(soap->tmpbuf, sizeof(soap->tmpbuf), host);
  7237. }
  7238. err = soap->fposthdr(soap, "Host", soap->tmpbuf);
  7239. if (err)
  7240. return err;
  7241. err = soap->fposthdr(soap, "User-Agent", "gSOAP/2.8");
  7242. if (err)
  7243. return err;
  7244. if (soap->origin) {
  7245. err = soap->fposthdr(soap, "Origin", soap->origin);
  7246. if (err)
  7247. return err;
  7248. if (soap->status == SOAP_OPTIONS) {
  7249. err = soap->fposthdr(soap, "Access-Control-Request-Method", soap->cors_method ? soap->cors_method : "POST");
  7250. if (err)
  7251. return err;
  7252. if (soap->cors_header) {
  7253. err = soap->fposthdr(soap, "Access-Control-Request-Headers", soap->cors_header);
  7254. if (err)
  7255. return err;
  7256. }
  7257. }
  7258. }
  7259. err = soap_puthttphdr(soap, SOAP_OK, count);
  7260. if (err)
  7261. return err;
  7262. #ifndef WITH_LEANER
  7263. if ((soap->imode & SOAP_ENC_MTOM)) {
  7264. err = soap->fposthdr(soap, "Accept", "multipart/related,application/xop+xml,*/*;q=0.8");
  7265. if (err)
  7266. return err;
  7267. }
  7268. #endif
  7269. #ifdef WITH_ZLIB
  7270. #ifdef WITH_GZIP
  7271. err = soap->fposthdr(soap, "Accept-Encoding", "gzip,deflate");
  7272. #else
  7273. err = soap->fposthdr(soap, "Accept-Encoding", "deflate");
  7274. #endif
  7275. if (err)
  7276. return err;
  7277. #endif
  7278. #if !defined(WITH_LEAN) || defined(WITH_NTLM)
  7279. if (soap->bearer) {
  7280. l = strlen(soap->bearer);
  7281. (SOAP_SNPRINTF(soap->tmpbuf, sizeof(soap->tmpbuf), l + 1), "Bearer %s", soap->bearer);
  7282. err = soap->fposthdr(soap, "Authorization", soap->tmpbuf);
  7283. if (err)
  7284. return err;
  7285. }
  7286. #ifdef WITH_NTLM
  7287. if (soap->ntlm_challenge)
  7288. {
  7289. l = strlen(soap->ntlm_challenge);
  7290. if (l)
  7291. {
  7292. (SOAP_SNPRINTF(soap->tmpbuf, sizeof(soap->tmpbuf), l + 5), "NTLM %s", soap->ntlm_challenge);
  7293. if (soap->proxy_host)
  7294. {
  7295. err = soap->fposthdr(soap, "Proxy-Authorization", soap->tmpbuf);
  7296. if (err)
  7297. return err;
  7298. }
  7299. else
  7300. {
  7301. err = soap->fposthdr(soap, "Authorization", soap->tmpbuf);
  7302. if (err)
  7303. return err;
  7304. }
  7305. }
  7306. }
  7307. else
  7308. {
  7309. #endif
  7310. if (soap->userid && soap->passwd) {
  7311. soap_strcpy(soap->tmpbuf, sizeof(soap->tmpbuf), "Basic ");
  7312. (SOAP_SNPRINTF(soap->tmpbuf + 262, sizeof(soap->tmpbuf) - 262, strlen(soap->userid) + strlen(soap->passwd) + 1),
  7313. "%s:%s", soap->userid, soap->passwd);
  7314. soap_s2base64(soap, (const unsigned char *) (soap->tmpbuf + 262), soap->tmpbuf + 6,
  7315. (int) strlen(soap->tmpbuf + 262));
  7316. err = soap->fposthdr(soap, "Authorization", soap->tmpbuf);
  7317. if (err)
  7318. return err;
  7319. }
  7320. if (soap->proxy_userid && soap->proxy_passwd) {
  7321. soap_strcpy(soap->tmpbuf, sizeof(soap->tmpbuf), "Basic ");
  7322. (SOAP_SNPRINTF(soap->tmpbuf + 262, sizeof(soap->tmpbuf) - 262,
  7323. strlen(soap->proxy_userid) + strlen(soap->proxy_passwd) + 1), "%s:%s", soap->proxy_userid,
  7324. soap->proxy_passwd);
  7325. soap_s2base64(soap, (const unsigned char *) (soap->tmpbuf + 262), soap->tmpbuf + 6,
  7326. (int) strlen(soap->tmpbuf + 262));
  7327. err = soap->fposthdr(soap, "Proxy-Authorization", soap->tmpbuf);
  7328. if (err)
  7329. return err;
  7330. }
  7331. #ifdef WITH_NTLM
  7332. }
  7333. #endif
  7334. #endif
  7335. #ifdef WITH_COOKIES
  7336. #ifdef WITH_OPENSSL
  7337. if (soap_putcookies(soap, host, path, soap->ssl != NULL))
  7338. return soap->error;
  7339. #else
  7340. if (soap_putcookies(soap, host, path, 0))
  7341. return soap->error;
  7342. #endif
  7343. #endif
  7344. if (action && soap->status != SOAP_GET && soap->status != SOAP_DEL) {
  7345. (SOAP_SNPRINTF(soap->tmpbuf, sizeof(soap->tmpbuf), strlen(action) + 2), "\"%s\"", action);
  7346. err = soap->fposthdr(soap, "SOAPAction", soap->tmpbuf);
  7347. if (err)
  7348. return err;
  7349. }
  7350. return soap->fposthdr(soap, NULL, NULL);
  7351. }
  7352. #endif
  7353. /******************************************************************************/
  7354. #ifndef WITH_NOHTTP
  7355. static int
  7356. http_send_header(struct soap *soap, const char *s) {
  7357. const char *t;
  7358. do {
  7359. t = strchr(s, '\n'); /* disallow \n in HTTP headers */
  7360. if (!t)
  7361. t = s + strlen(s);
  7362. if (soap_send_raw(soap, s, t - s))
  7363. return soap->error;
  7364. s = t + 1;
  7365. } while (*t);
  7366. return SOAP_OK;
  7367. }
  7368. #endif
  7369. /******************************************************************************/
  7370. #ifndef WITH_NOHTTP
  7371. static int
  7372. http_post_header(struct soap *soap, const char *key, const char *val) {
  7373. if (key) {
  7374. if (http_send_header(soap, key))
  7375. return soap->error;
  7376. if (val && (soap_send_raw(soap, ": ", 2) || http_send_header(soap, val)))
  7377. return soap->error;
  7378. }
  7379. return soap_send_raw(soap, "\r\n", 2);
  7380. }
  7381. #endif
  7382. /******************************************************************************/
  7383. #ifndef WITH_NOHTTP
  7384. static int
  7385. http_response(struct soap *soap, int status, ULONG64 count) {
  7386. int err;
  7387. char http[32];
  7388. int code = status;
  7389. const char *line;
  7390. #ifdef WMW_RPM_IO
  7391. if (soap->rpmreqid)
  7392. httpOutputEnable(soap->rpmreqid);
  7393. if (soap->rpmreqid
  7394. || soap_valid_socket(soap->master)
  7395. || soap_valid_socket(soap->socket)
  7396. || soap->recvfd != 0
  7397. || soap->sendfd != 1
  7398. || soap->os) /* RPM behaves as if standalone */
  7399. #else
  7400. if (soap_valid_socket(soap->master)
  7401. || soap_valid_socket(soap->socket)
  7402. #ifndef UNDER_CE
  7403. || soap->recvfd != 0
  7404. || soap->sendfd != 1
  7405. #else
  7406. || soap->recvfd != stdin
  7407. || soap->sendfd != stdout
  7408. #endif
  7409. || soap->os) /* standalone server application (over sockets), not CGI (over stdin/out) */
  7410. #endif
  7411. (SOAP_SNPRINTF(http, sizeof(http), strlen(soap->http_version) + 5), "HTTP/%s", soap->http_version);
  7412. else
  7413. soap_strcpy(http, sizeof(http), "Status:");
  7414. if (status >= SOAP_FILE && status < SOAP_FILE + 600) {
  7415. code = status - SOAP_FILE;
  7416. if (code == 0)
  7417. code = 200;
  7418. } else if (!status || status == SOAP_HTML) {
  7419. if (count || ((soap->omode & SOAP_IO) == SOAP_IO_CHUNK))
  7420. code = 200;
  7421. else
  7422. code = 202;
  7423. } else if (status < 200 || status >= 600) {
  7424. const char *s = *soap_faultcode(soap);
  7425. if (status >= SOAP_GET_METHOD && status <= SOAP_HTTP_METHOD)
  7426. code = 405;
  7427. else if (soap->version == 2 && (!s || !strcmp(s, "SOAP-ENV:Sender")))
  7428. code = 400;
  7429. else
  7430. code = 500;
  7431. }
  7432. line = http_error(soap, code);
  7433. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "HTTP Status = %d %s\n", code, line));
  7434. (SOAP_SNPRINTF(soap->tmpbuf, sizeof(soap->tmpbuf), sizeof(http) + 22 + strlen(line)), "%s %d %s", http, code, line);
  7435. err = soap->fposthdr(soap, soap->tmpbuf, NULL);
  7436. if (err)
  7437. return err;
  7438. #ifndef WITH_LEAN
  7439. if (status == 401) {
  7440. (SOAP_SNPRINTF_SAFE(soap->tmpbuf, sizeof(soap->tmpbuf)), "Basic realm=\"%s\"", (soap->authrealm &&
  7441. strlen(soap->authrealm) + 14 <
  7442. sizeof(soap->tmpbuf))
  7443. ? soap->authrealm
  7444. : "gSOAP Web Service");
  7445. err = soap->fposthdr(soap, "WWW-Authenticate", soap->tmpbuf);
  7446. if (err)
  7447. return err;
  7448. } else if ((status >= 301 && status <= 303) || status == 307) {
  7449. err = soap->fposthdr(soap, "Location", soap->endpoint);
  7450. if (err)
  7451. return err;
  7452. }
  7453. #endif
  7454. err = soap->fposthdr(soap, "Server", "ACS/1.1.1 By Sassan");
  7455. // err = soap->fposthdr(soap, "Server", "gSOAP/2.8");
  7456. if (err)
  7457. return err;
  7458. if (soap->cors_origin) {
  7459. err = soap->fposthdr(soap, "Access-Control-Allow-Origin", soap->cors_origin);
  7460. if (err)
  7461. return err;
  7462. err = soap->fposthdr(soap, "Access-Control-Allow-Credentials", "true");
  7463. if (err)
  7464. return err;
  7465. if (soap->cors_methods) {
  7466. err = soap->fposthdr(soap, "Access-Control-Allow-Methods", soap->cors_methods);
  7467. if (err)
  7468. return err;
  7469. if (soap->cors_headers) {
  7470. err = soap->fposthdr(soap, "Access-Control-Allow-Headers", soap->cors_headers);
  7471. if (err)
  7472. return err;
  7473. }
  7474. }
  7475. }
  7476. if (soap->x_frame_options) {
  7477. err = soap->fposthdr(soap, "X-Frame-Options", soap->x_frame_options);
  7478. if (err)
  7479. return err;
  7480. }
  7481. soap->cors_origin = NULL;
  7482. soap->cors_methods = NULL;
  7483. soap->cors_headers = NULL;
  7484. err = soap_puthttphdr(soap, status, count);
  7485. if (err)
  7486. return err;
  7487. #ifdef WITH_COOKIES
  7488. if (soap_putsetcookies(soap))
  7489. return soap->error;
  7490. soap_free_cookies(soap);
  7491. #endif
  7492. return soap->fposthdr(soap, NULL, NULL);
  7493. }
  7494. #endif
  7495. /******************************************************************************/
  7496. SOAP_FMAC1
  7497. int
  7498. SOAP_FMAC2
  7499. soap_response(struct soap *soap, int status) {
  7500. ULONG64 count;
  7501. if (!(soap->omode & (SOAP_ENC_PLAIN | SOAP_IO_STORE /* this tests for chunking too */))
  7502. && (status == SOAP_HTML || (status >= SOAP_FILE && status < SOAP_FILE + 600)))
  7503. soap->omode = (soap->omode & ~SOAP_IO) | SOAP_IO_STORE;
  7504. soap->status = status;
  7505. count = soap_count_attachments(soap);
  7506. if (soap_init_send(soap))
  7507. return soap->error;
  7508. #ifndef WITH_NOHTTP
  7509. if ((soap->mode & SOAP_IO) != SOAP_IO_STORE && !(soap->mode & SOAP_ENC_PLAIN)) {
  7510. int k = soap->mode;
  7511. soap->mode &= ~(SOAP_IO | SOAP_ENC_ZLIB);
  7512. if ((k & SOAP_IO) != SOAP_IO_FLUSH)
  7513. soap->mode |= SOAP_IO_BUFFER;
  7514. soap->error = soap->fresponse(soap, status, count);
  7515. if (soap->error)
  7516. return soap->error;
  7517. #ifndef WITH_LEANER
  7518. if ((k & SOAP_IO) == SOAP_IO_CHUNK) {
  7519. if (soap_flush(soap))
  7520. return soap->error;
  7521. }
  7522. #endif
  7523. soap->mode = k;
  7524. }
  7525. #endif
  7526. #ifndef WITH_LEANER
  7527. if (soap_begin_attachments(soap))
  7528. return soap->error;
  7529. #endif
  7530. return SOAP_OK;
  7531. }
  7532. /******************************************************************************/
  7533. SOAP_FMAC1
  7534. const char *
  7535. SOAP_FMAC2
  7536. soap_extend_url(struct soap *soap, const char *s, const char *t) {
  7537. if (s)
  7538. soap_strcpy(soap->msgbuf, sizeof(soap->msgbuf), s);
  7539. else
  7540. *soap->msgbuf = '\0';
  7541. if (t && (*t == '/' || *t == '?')) {
  7542. char *r = strchr(soap->msgbuf, '?');
  7543. if (r) {
  7544. if (*t == '?') {
  7545. soap_strcat(soap->msgbuf, sizeof(soap->msgbuf), "&");
  7546. soap_strcat(soap->msgbuf, sizeof(soap->msgbuf), t + 1);
  7547. } else /* *t == '/' */
  7548. {
  7549. size_t l = r - soap->msgbuf;
  7550. *r = '\0';
  7551. soap_strcat(soap->msgbuf, sizeof(soap->msgbuf), t);
  7552. if (s)
  7553. soap_strcat(soap->msgbuf, sizeof(soap->msgbuf), s + l);
  7554. }
  7555. } else {
  7556. soap_strcat(soap->msgbuf, sizeof(soap->msgbuf), t);
  7557. }
  7558. }
  7559. return soap->msgbuf;
  7560. }
  7561. /******************************************************************************/
  7562. SOAP_FMAC1
  7563. const char *
  7564. SOAP_FMAC2
  7565. soap_extend_url_query(struct soap *soap, const char *s, const char *t) {
  7566. (void) soap_extend_url(soap, s, t); /* fills and returns soap->msgbuf */
  7567. if (strchr(soap->msgbuf, '?'))
  7568. soap_strcat(soap->msgbuf, sizeof(soap->msgbuf), "&");
  7569. else
  7570. soap_strcat(soap->msgbuf, sizeof(soap->msgbuf), "?");
  7571. return soap->msgbuf;
  7572. }
  7573. /******************************************************************************/
  7574. SOAP_FMAC1
  7575. void
  7576. SOAP_FMAC2
  7577. soap_url_query(struct soap *soap, const char *s, const char *t) {
  7578. size_t n = strlen(s);
  7579. if (n) {
  7580. char *r = soap->msgbuf;
  7581. size_t k = n - (s[n - 1] == '=');
  7582. while ((r = strchr(r, '{')) != NULL)
  7583. if (!strncmp(++r, s, k) && r[k] == '}')
  7584. break;
  7585. if (r) {
  7586. size_t m = t ? strlen(t) : 0;
  7587. (void) soap_memmove(r + m - 1, soap->msgbuf + sizeof(soap->msgbuf) - (r + n + 1), r + k + 1,
  7588. strlen(r + k + 1) + 1);
  7589. if (m)
  7590. (void) soap_memmove(r - 1, soap->msgbuf + sizeof(soap->msgbuf) - (r - 1), t, m);
  7591. } else {
  7592. soap_strcat(soap->msgbuf, sizeof(soap->msgbuf), s);
  7593. if (t) {
  7594. int m = (int) strlen(soap->msgbuf); /* msgbuf length is max SOAP_TMPLEN or just 1024 bytes */
  7595. (void) soap_encode_url(t, soap->msgbuf + m, (int) sizeof(soap->msgbuf) - m);
  7596. }
  7597. soap_strcat(soap->msgbuf, sizeof(soap->msgbuf), "&");
  7598. }
  7599. }
  7600. }
  7601. /******************************************************************************/
  7602. SOAP_FMAC1
  7603. int
  7604. SOAP_FMAC2
  7605. soap_encode_url(const char *s, char *t, int len) {
  7606. int c;
  7607. int n = len;
  7608. if (s && n > 0) {
  7609. while ((c = *s++) && --n > 0) {
  7610. if (c == '-'
  7611. || c == '.'
  7612. || (c >= '0' && c <= '9')
  7613. || (c >= 'A' && c <= 'Z')
  7614. || c == '_'
  7615. || (c >= 'a' && c <= 'z')
  7616. || c == '~') {
  7617. *t++ = c;
  7618. } else if (n > 2) {
  7619. *t++ = '%';
  7620. *t++ = (c >> 4) + (c > 159 ? '7' : '0');
  7621. c &= 0xF;
  7622. *t++ = c + (c > 9 ? '7' : '0');
  7623. n -= 2;
  7624. } else {
  7625. break;
  7626. }
  7627. }
  7628. *t = '\0';
  7629. }
  7630. return len - n;
  7631. }
  7632. /******************************************************************************/
  7633. SOAP_FMAC1
  7634. const char *
  7635. SOAP_FMAC2
  7636. soap_encode_url_string(struct soap *soap, const char *s) {
  7637. if (s) {
  7638. int n = 3 * (int) strlen(s) + 1;
  7639. char *t = (char *) soap_malloc(soap, n);
  7640. if (t) {
  7641. (void) soap_encode_url(s, t, n);
  7642. return t;
  7643. }
  7644. }
  7645. return SOAP_STR_EOS;
  7646. }
  7647. /******************************************************************************\
  7648. *
  7649. * HTTP Cookies RFC 6265
  7650. *
  7651. \******************************************************************************/
  7652. #ifdef WITH_COOKIES
  7653. SOAP_FMAC1
  7654. struct soap_cookie*
  7655. SOAP_FMAC2
  7656. soap_cookie(struct soap *soap, const char *name, const char *domain, const char *path)
  7657. {
  7658. return soap_cookie_env(soap, name, domain, path, 0);
  7659. }
  7660. /******************************************************************************/
  7661. SOAP_FMAC1
  7662. struct soap_cookie*
  7663. SOAP_FMAC2
  7664. soap_cookie_env(struct soap *soap, const char *name, const char *domain, const char *path, short env)
  7665. {
  7666. struct soap_cookie *p;
  7667. if (!domain && !env)
  7668. domain = soap->cookie_domain;
  7669. if (!path)
  7670. path = soap->cookie_path;
  7671. if (!path)
  7672. path = SOAP_STR_EOS;
  7673. else if (*path == '/')
  7674. path++;
  7675. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Search cookie='%s' domain='%s' path='%s' env=%hd\n", name, domain ? domain : "(null)", path ? path : "(null)", env));
  7676. for (p = soap->cookies; p; p = p->next)
  7677. {
  7678. 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));
  7679. if ((!env || p->env)
  7680. && !strcmp(p->name, name)
  7681. && (!domain || (domain && p->domain && !strcmp(p->domain, domain)))
  7682. && (!path || (path && p->path && !strncmp(p->path, path, strlen(p->path)))))
  7683. break;
  7684. }
  7685. return p;
  7686. }
  7687. /******************************************************************************/
  7688. SOAP_FMAC1
  7689. struct soap_cookie*
  7690. SOAP_FMAC2
  7691. soap_set_cookie(struct soap *soap, const char *name, const char *value, const char *domain, const char *path)
  7692. {
  7693. struct soap_cookie **p, *q;
  7694. int n;
  7695. if (!domain)
  7696. domain = soap->cookie_domain;
  7697. if (!path)
  7698. path = soap->cookie_path;
  7699. if (!path)
  7700. path = SOAP_STR_EOS;
  7701. else if (*path == '/')
  7702. path++;
  7703. q = soap_cookie(soap, name, domain, path);
  7704. 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)"));
  7705. if (!q)
  7706. {
  7707. q = (struct soap_cookie*)SOAP_MALLOC(soap, sizeof(struct soap_cookie));
  7708. if (q)
  7709. {
  7710. size_t l = strlen(name) + 1;
  7711. q->name = NULL;
  7712. if (SOAP_MAXALLOCSIZE <= 0 || l <= SOAP_MAXALLOCSIZE)
  7713. q->name = (char*)SOAP_MALLOC(soap, l);
  7714. if (q->name)
  7715. (void)soap_memcpy(q->name, l, name, l);
  7716. q->value = NULL;
  7717. q->domain = NULL;
  7718. q->path = NULL;
  7719. q->expire = 0;
  7720. q->maxage = -1;
  7721. q->version = 1;
  7722. q->secure = 0;
  7723. q->modified = 0;
  7724. for (p = &soap->cookies, n = soap->cookie_max; *p && n; p = &(*p)->next, n--)
  7725. if (!strcmp((*p)->name, name) && (*p)->path && path && strcmp((*p)->path, path) < 0)
  7726. break;
  7727. if (n)
  7728. {
  7729. q->next = *p;
  7730. *p = q;
  7731. }
  7732. else
  7733. {
  7734. SOAP_FREE(soap, q->name);
  7735. SOAP_FREE(soap, q);
  7736. q = NULL;
  7737. }
  7738. }
  7739. }
  7740. else
  7741. {
  7742. q->modified = 1;
  7743. }
  7744. if (q)
  7745. {
  7746. if (q->value)
  7747. {
  7748. if (!value || strcmp(value, q->value))
  7749. {
  7750. SOAP_FREE(soap, q->value);
  7751. q->value = NULL;
  7752. }
  7753. }
  7754. if (value && *value && !q->value)
  7755. {
  7756. size_t l = strlen(value) + 1;
  7757. q->value = NULL;
  7758. if (SOAP_MAXALLOCSIZE <= 0 || l <= SOAP_MAXALLOCSIZE)
  7759. q->value = (char*)SOAP_MALLOC(soap, l);
  7760. if (q->value)
  7761. soap_strcpy(q->value, l, value);
  7762. }
  7763. if (q->domain)
  7764. {
  7765. if (!domain || strcmp(domain, q->domain))
  7766. {
  7767. SOAP_FREE(soap, q->domain);
  7768. q->domain = NULL;
  7769. }
  7770. }
  7771. if (domain && !q->domain)
  7772. {
  7773. size_t l = strlen(domain) + 1;
  7774. q->domain = NULL;
  7775. if (SOAP_MAXALLOCSIZE <= 0 || l <= SOAP_MAXALLOCSIZE)
  7776. q->domain = (char*)SOAP_MALLOC(soap, l);
  7777. if (q->domain)
  7778. soap_strcpy(q->domain, l, domain);
  7779. }
  7780. if (q->path)
  7781. {
  7782. if (!path || strncmp(path, q->path, strlen(q->path)))
  7783. {
  7784. SOAP_FREE(soap, q->path);
  7785. q->path = NULL;
  7786. }
  7787. }
  7788. if (path && !q->path)
  7789. {
  7790. size_t l = strlen(path) + 1;
  7791. q->path = NULL;
  7792. if (SOAP_MAXALLOCSIZE <= 0 || l <= SOAP_MAXALLOCSIZE)
  7793. q->path = (char*)SOAP_MALLOC(soap, l);
  7794. if (q->path)
  7795. soap_strcpy(q->path, l, path);
  7796. }
  7797. q->session = 1;
  7798. q->env = 0;
  7799. }
  7800. return q;
  7801. }
  7802. /******************************************************************************/
  7803. SOAP_FMAC1
  7804. void
  7805. SOAP_FMAC2
  7806. soap_clr_cookie(struct soap *soap, const char *name, const char *domain, const char *path)
  7807. {
  7808. struct soap_cookie **p, *q;
  7809. if (!domain)
  7810. domain = soap->cookie_domain;
  7811. if (!domain)
  7812. {
  7813. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Error in clear cookie='%s': cookie domain not set\n", name ? name : "(null)"));
  7814. return;
  7815. }
  7816. if (!path)
  7817. path = soap->cookie_path;
  7818. if (!path)
  7819. path = SOAP_STR_EOS;
  7820. else if (*path == '/')
  7821. path++;
  7822. for (p = &soap->cookies, q = *p; q; q = *p)
  7823. {
  7824. if (!strcmp(q->name, name) && (!q->domain || !strcmp(q->domain, domain)) && (!q->path || !strncmp(q->path, path, strlen(q->path))))
  7825. {
  7826. SOAP_FREE(soap, q->name);
  7827. if (q->value)
  7828. SOAP_FREE(soap, q->value);
  7829. if (q->domain)
  7830. SOAP_FREE(soap, q->domain);
  7831. if (q->path)
  7832. SOAP_FREE(soap, q->path);
  7833. *p = q->next;
  7834. SOAP_FREE(soap, q);
  7835. }
  7836. else
  7837. p = &q->next;
  7838. }
  7839. }
  7840. /******************************************************************************/
  7841. SOAP_FMAC1
  7842. const char *
  7843. SOAP_FMAC2
  7844. soap_cookie_value(struct soap *soap, const char *name, const char *domain, const char *path)
  7845. {
  7846. struct soap_cookie *p;
  7847. p = soap_cookie(soap, name, domain, path);
  7848. if (p)
  7849. return p->value;
  7850. return NULL;
  7851. }
  7852. /******************************************************************************/
  7853. SOAP_FMAC1
  7854. const char *
  7855. SOAP_FMAC2
  7856. soap_env_cookie_value(struct soap *soap, const char *name, const char *domain, const char *path)
  7857. {
  7858. struct soap_cookie *p;
  7859. p = soap_cookie(soap, name, domain, path);
  7860. if (p && p->env)
  7861. return p->value;
  7862. return NULL;
  7863. }
  7864. /******************************************************************************/
  7865. SOAP_FMAC1
  7866. time_t
  7867. SOAP_FMAC2
  7868. soap_cookie_expire(struct soap *soap, const char *name, const char *domain, const char *path)
  7869. {
  7870. struct soap_cookie *p;
  7871. p = soap_cookie(soap, name, domain, path);
  7872. if (p)
  7873. return (time_t)p->expire;
  7874. return -1;
  7875. }
  7876. /******************************************************************************/
  7877. SOAP_FMAC1
  7878. int
  7879. SOAP_FMAC2
  7880. soap_set_cookie_expire(struct soap *soap, const char *name, long maxage, const char *domain, const char *path)
  7881. {
  7882. struct soap_cookie *p;
  7883. 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)"));
  7884. p = soap_cookie(soap, name, domain, path);
  7885. if (p)
  7886. {
  7887. p->maxage = maxage;
  7888. p->modified = 1;
  7889. return SOAP_OK;
  7890. }
  7891. return SOAP_ERR;
  7892. }
  7893. /******************************************************************************/
  7894. SOAP_FMAC1
  7895. int
  7896. SOAP_FMAC2
  7897. soap_set_cookie_secure(struct soap *soap, const char *name, const char *domain, const char *path)
  7898. {
  7899. struct soap_cookie *p;
  7900. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Set cookie secure: cookie='%s' domain='%s' path='%s'\n", name, domain ? domain : "(null)", path ? path : "(null)"));
  7901. p = soap_cookie(soap, name, domain, path);
  7902. if (p)
  7903. {
  7904. p->secure = 1;
  7905. p->modified = 1;
  7906. return SOAP_OK;
  7907. }
  7908. return SOAP_ERR;
  7909. }
  7910. /******************************************************************************/
  7911. SOAP_FMAC1
  7912. int
  7913. SOAP_FMAC2
  7914. soap_set_cookie_session(struct soap *soap, const char *name, const char *domain, const char *path)
  7915. {
  7916. struct soap_cookie *p;
  7917. p = soap_cookie(soap, name, domain, path);
  7918. if (p)
  7919. {
  7920. p->session = 1;
  7921. p->modified = 1;
  7922. return SOAP_OK;
  7923. }
  7924. return SOAP_ERR;
  7925. }
  7926. /******************************************************************************/
  7927. SOAP_FMAC1
  7928. int
  7929. SOAP_FMAC2
  7930. soap_clr_cookie_session(struct soap *soap, const char *name, const char *domain, const char *path)
  7931. {
  7932. struct soap_cookie *p;
  7933. p = soap_cookie(soap, name, domain, path);
  7934. if (p)
  7935. {
  7936. p->session = 0;
  7937. p->modified = 1;
  7938. return SOAP_OK;
  7939. }
  7940. return SOAP_ERR;
  7941. }
  7942. /******************************************************************************/
  7943. SOAP_FMAC1
  7944. int
  7945. SOAP_FMAC2
  7946. soap_putsetcookies(struct soap *soap)
  7947. {
  7948. struct soap_cookie *p;
  7949. char *s, tmp[4096];
  7950. const char *t;
  7951. for (p = soap->cookies; p; p = p->next)
  7952. {
  7953. if ((p->modified
  7954. #ifdef WITH_OPENSSL
  7955. || (!p->env && !soap->ssl == !p->secure)
  7956. #endif
  7957. ) && p->name && p->value && *p->name && *p->value)
  7958. {
  7959. s = tmp;
  7960. s += soap_encode_url(p->name, s, 3967);
  7961. *s++ = '=';
  7962. s += soap_encode_url(p->value, s, 3968 - (int)(s-tmp));
  7963. t = p->domain ? p->domain : soap->cookie_domain;
  7964. if (t && (int)strlen(t) < 3968 - (int)(s-tmp))
  7965. {
  7966. soap_strcpy(s, 4096 - (s-tmp), ";Domain=");
  7967. s += 8;
  7968. soap_strcpy(s, 4096 - (s-tmp), t);
  7969. s += strlen(s);
  7970. }
  7971. t = p->path ? p->path : soap->cookie_path;
  7972. if (t && (int)strlen(t) < 3976 - (int)(s-tmp))
  7973. {
  7974. soap_strcpy(s, 4096 - (s-tmp), ";Path=/");
  7975. s += 7;
  7976. if (*t == '/')
  7977. t++;
  7978. if (strchr(t, '%')) /* already URL encoded? */
  7979. {
  7980. soap_strcpy(s, 4096 - (s-tmp), t);
  7981. s += strlen(s);
  7982. }
  7983. else
  7984. {
  7985. s += soap_encode_url(t, s, 4096 - (int)(s-tmp));
  7986. }
  7987. }
  7988. if (p->version > 0 && s-tmp < 3983)
  7989. {
  7990. (SOAP_SNPRINTF(s, 4096 - (s-tmp), 29), ";Version=%u", p->version);
  7991. s += strlen(s);
  7992. }
  7993. if (p->maxage >= 0 && s-tmp < 4012)
  7994. {
  7995. (SOAP_SNPRINTF(s, 4096 - (s-tmp), 29), ";Max-Age=%ld", p->maxage);
  7996. s += strlen(s);
  7997. }
  7998. #if !defined(WITH_LEAN)
  7999. #if defined(HAVE_GMTIME_R) || defined(HAVE_GMTIME)
  8000. if (p->maxage >= 0 && s-tmp < 4041)
  8001. {
  8002. time_t n = time(NULL) + p->maxage;
  8003. struct tm T, *pT = &T;
  8004. size_t l = 0;
  8005. /* format is Wed, 09 Jun 2021 10:18:14 GMT */
  8006. #if defined(HAVE_GMTIME_R)
  8007. if (gmtime_r(&n, pT) != SOAP_FUNC_R_ERR)
  8008. l = strftime(s, 4096 - (s-tmp), ";Expires=%a, %d %b %Y %H:%M:%S GMT", pT);
  8009. #else
  8010. pT = gmtime(&n);
  8011. if (pT)
  8012. l = strftime(s, 4096 - (s-tmp), ";Expires=%a, %d %b %Y %H:%M:%S GMT", pT);
  8013. #endif
  8014. s += l;
  8015. }
  8016. #endif
  8017. #endif
  8018. if (s-tmp < 4079
  8019. && (p->secure
  8020. #ifdef WITH_OPENSSL
  8021. || soap->ssl
  8022. #endif
  8023. ))
  8024. {
  8025. soap_strcpy(s, 4096 - (s-tmp), ";Secure");
  8026. s += strlen(s);
  8027. }
  8028. if (s-tmp < 4086)
  8029. soap_strcpy(s, 4096 - (s-tmp), ";HttpOnly");
  8030. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Set-Cookie: %s\n", tmp));
  8031. soap->error = soap->fposthdr(soap, "Set-Cookie", tmp);
  8032. if (soap->error)
  8033. return soap->error;
  8034. }
  8035. }
  8036. return SOAP_OK;
  8037. }
  8038. /******************************************************************************/
  8039. SOAP_FMAC1
  8040. int
  8041. SOAP_FMAC2
  8042. soap_putcookies(struct soap *soap, const char *domain, const char *path, int secure)
  8043. {
  8044. struct soap_cookie **p, *q;
  8045. char *s, tmp[4096];
  8046. unsigned int version = 0;
  8047. time_t now = time(NULL);
  8048. if (!domain || !path)
  8049. return SOAP_OK;
  8050. s = tmp;
  8051. p = &soap->cookies;
  8052. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Sending cookies for domain='%s' path='%s'\n", domain, path));
  8053. if (*path == '/')
  8054. path++;
  8055. while ((q = *p))
  8056. {
  8057. if (q->expire && now >= (time_t)q->expire)
  8058. {
  8059. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Cookie %s expired\n", q->name));
  8060. SOAP_FREE(soap, q->name);
  8061. if (q->value)
  8062. SOAP_FREE(soap, q->value);
  8063. if (q->domain)
  8064. SOAP_FREE(soap, q->domain);
  8065. if (q->path)
  8066. SOAP_FREE(soap, q->path);
  8067. *p = q->next;
  8068. SOAP_FREE(soap, q);
  8069. }
  8070. else
  8071. {
  8072. int flag;
  8073. char *t = q->domain;
  8074. size_t n = 0;
  8075. if (!t)
  8076. flag = 1;
  8077. else
  8078. {
  8079. const char *r = strchr(t, ':');
  8080. if (r)
  8081. n = r - t;
  8082. else
  8083. n = strlen(t);
  8084. flag = !strncmp(t, domain, n);
  8085. }
  8086. /* domain-level cookies, cannot compile when WITH_NOIO set */
  8087. #ifndef WITH_NOIO
  8088. if (!flag)
  8089. {
  8090. struct hostent hostent;
  8091. if (!tcp_gethostbyname(soap, (char*)domain, &hostent, NULL))
  8092. {
  8093. const char *r = hostent.h_name;
  8094. if (*t == '.')
  8095. {
  8096. size_t k = strlen(hostent.h_name);
  8097. if (k >= n)
  8098. r = hostent.h_name + k - n;
  8099. }
  8100. flag = !strncmp(t, r, n);
  8101. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Domain cookie %s host %s (match=%d)\n", t, r, flag));
  8102. }
  8103. }
  8104. #endif
  8105. if (flag
  8106. && (!q->path || !strncmp(q->path, path, strlen(q->path)))
  8107. #ifndef WITH_INSECURE_COOKIES
  8108. && (!q->secure || secure)
  8109. #endif
  8110. )
  8111. {
  8112. size_t n = 12;
  8113. if (q->name)
  8114. n += 3*strlen(q->name);
  8115. if (q->value && *q->value)
  8116. n += 3*strlen(q->value) + 1;
  8117. if (q->path && *q->path)
  8118. n += strlen(q->path) + 9;
  8119. if (q->domain)
  8120. n += strlen(q->domain) + 11;
  8121. if (s + n >= tmp + sizeof(tmp))
  8122. {
  8123. if (s == tmp)
  8124. return SOAP_OK; /* header too big, cannot split */
  8125. /* split up HTTP header */
  8126. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Cookie: %s\n", tmp));
  8127. soap->error = soap->fposthdr(soap, "Cookie", tmp);
  8128. if (soap->error)
  8129. return soap->error;
  8130. s = tmp;
  8131. }
  8132. else if (s != tmp)
  8133. {
  8134. *s++ = ';';
  8135. }
  8136. 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)"));
  8137. if (q->version != version && (s-tmp) + (size_t)36 < sizeof(tmp))
  8138. {
  8139. (SOAP_SNPRINTF_SAFE(s, sizeof(tmp) - (s-tmp)), "$Version=%u;", q->version);
  8140. version = q->version;
  8141. s += strlen(s);
  8142. }
  8143. if (q->name && (s-tmp) + strlen(q->name) + (size_t)15 < sizeof(tmp))
  8144. {
  8145. s += soap_encode_url(q->name, s, (int)(tmp+sizeof(tmp)-s)-15);
  8146. }
  8147. if (q->value && *q->value && (s-tmp) + strlen(q->value) + (size_t)16 < sizeof(tmp))
  8148. {
  8149. *s++ = '=';
  8150. s += soap_encode_url(q->value, s, (int)(tmp+sizeof(tmp)-s)-16);
  8151. }
  8152. if (q->path && (s-tmp) + strlen(q->path) + (size_t)36 < sizeof(tmp))
  8153. {
  8154. (SOAP_SNPRINTF_SAFE(s, sizeof(tmp) - (s-tmp)), ";$Path=\"/%s\"", (*q->path == '/' ? q->path + 1 : q->path));
  8155. s += strlen(s);
  8156. }
  8157. if (q->domain && (s-tmp) + strlen(q->domain) + (size_t)36 < sizeof(tmp))
  8158. {
  8159. (SOAP_SNPRINTF_SAFE(s, sizeof(tmp) - (s-tmp)), ";$Domain=\"%s\"", q->domain);
  8160. s += strlen(s);
  8161. }
  8162. }
  8163. p = &q->next;
  8164. }
  8165. }
  8166. if (s != tmp)
  8167. {
  8168. soap->error = soap->fposthdr(soap, "Cookie", tmp);
  8169. if (soap->error)
  8170. return soap->error;
  8171. }
  8172. return SOAP_OK;
  8173. }
  8174. /******************************************************************************/
  8175. SOAP_FMAC1
  8176. void
  8177. SOAP_FMAC2
  8178. soap_getcookies(struct soap *soap, const char *val)
  8179. {
  8180. struct soap_cookie *p = NULL, *q;
  8181. const char *s;
  8182. char *t, tmp[4096]; /* cookie size is up to 4096 bytes [RFC2109] */
  8183. char *domain = NULL;
  8184. char *path = NULL;
  8185. unsigned int version = 0;
  8186. time_t now = time(NULL);
  8187. if (!val)
  8188. return;
  8189. s = val;
  8190. while (*s)
  8191. {
  8192. s = soap_decode_key(tmp, sizeof(tmp), s);
  8193. if (!soap_tag_cmp(tmp, "$Version"))
  8194. {
  8195. s = soap_decode_val(tmp, sizeof(tmp), s);
  8196. if (s)
  8197. {
  8198. if (p)
  8199. p->version = (int)soap_strtol(tmp, NULL, 10);
  8200. else
  8201. version = (int)soap_strtol(tmp, NULL, 10);
  8202. }
  8203. }
  8204. else if (!soap_tag_cmp(tmp, "$Path"))
  8205. {
  8206. s = soap_decode_val(tmp, sizeof(tmp), s);
  8207. if (*tmp)
  8208. {
  8209. size_t l = strlen(tmp) + 1;
  8210. t = NULL;
  8211. if (SOAP_MAXALLOCSIZE <= 0 || l <= SOAP_MAXALLOCSIZE)
  8212. t = (char*)SOAP_MALLOC(soap, l);
  8213. if (t)
  8214. (void)soap_memcpy((void*)t, l, (const void*)tmp, l);
  8215. }
  8216. else
  8217. {
  8218. t = NULL;
  8219. }
  8220. if (p)
  8221. {
  8222. if (p->path)
  8223. SOAP_FREE(soap, p->path);
  8224. p->path = t;
  8225. }
  8226. else
  8227. {
  8228. if (path)
  8229. SOAP_FREE(soap, path);
  8230. path = t;
  8231. }
  8232. }
  8233. else if (!soap_tag_cmp(tmp, "$Domain"))
  8234. {
  8235. s = soap_decode_val(tmp, sizeof(tmp), s);
  8236. if (*tmp)
  8237. {
  8238. size_t l = strlen(tmp) + 1;
  8239. t = NULL;
  8240. if (SOAP_MAXALLOCSIZE <= 0 || l <= SOAP_MAXALLOCSIZE)
  8241. t = (char*)SOAP_MALLOC(soap, l);
  8242. if (t)
  8243. (void)soap_memcpy((void*)t, l, (const void*)tmp, l);
  8244. }
  8245. else
  8246. {
  8247. t = NULL;
  8248. }
  8249. if (p)
  8250. {
  8251. if (p->domain)
  8252. SOAP_FREE(soap, p->domain);
  8253. p->domain = t;
  8254. }
  8255. else
  8256. {
  8257. if (domain)
  8258. SOAP_FREE(soap, domain);
  8259. domain = t;
  8260. }
  8261. }
  8262. else if (p && !soap_tag_cmp(tmp, "Path"))
  8263. {
  8264. if (p->path)
  8265. SOAP_FREE(soap, p->path);
  8266. s = soap_decode_val(tmp, sizeof(tmp), s);
  8267. if (*tmp)
  8268. {
  8269. size_t l = strlen(tmp) + 1;
  8270. p->path = NULL;
  8271. if (SOAP_MAXALLOCSIZE <= 0 || l <= SOAP_MAXALLOCSIZE)
  8272. p->path = (char*)SOAP_MALLOC(soap, l);
  8273. if (p->path)
  8274. (void)soap_memcpy((void*)p->path, l, (const void*)tmp, l);
  8275. }
  8276. else
  8277. {
  8278. p->path = NULL;
  8279. }
  8280. }
  8281. else if (p && !soap_tag_cmp(tmp, "Domain"))
  8282. {
  8283. if (p->domain)
  8284. SOAP_FREE(soap, p->domain);
  8285. s = soap_decode_val(tmp, sizeof(tmp), s);
  8286. if (*tmp)
  8287. {
  8288. size_t l = strlen(tmp) + 1;
  8289. p->domain = NULL;
  8290. if (SOAP_MAXALLOCSIZE <= 0 || l <= SOAP_MAXALLOCSIZE)
  8291. p->domain = (char*)SOAP_MALLOC(soap, l);
  8292. if (p->domain)
  8293. (void)soap_memcpy((void*)p->domain, l, (const void*)tmp, l);
  8294. }
  8295. else
  8296. {
  8297. p->domain = NULL;
  8298. }
  8299. }
  8300. else if (p && !soap_tag_cmp(tmp, "Version"))
  8301. {
  8302. s = soap_decode_val(tmp, sizeof(tmp), s);
  8303. p->version = (unsigned int)soap_strtoul(tmp, NULL, 10);
  8304. }
  8305. else if (p && !soap_tag_cmp(tmp, "Max-Age"))
  8306. {
  8307. s = soap_decode_val(tmp, sizeof(tmp), s);
  8308. p->expire = (ULONG64)(now + soap_strtol(tmp, NULL, 10));
  8309. }
  8310. else if (p && !soap_tag_cmp(tmp, "Expires"))
  8311. {
  8312. if (*s == '=')
  8313. {
  8314. s = soap_decode(tmp, sizeof(tmp), s + 1, ";");
  8315. if (!p->expire && strlen(tmp) >= 23)
  8316. {
  8317. char a[3];
  8318. struct tm T;
  8319. static const char mns[] = "anebarprayunulugepctovec";
  8320. const char *t = strchr(tmp, ' ');
  8321. if (t)
  8322. {
  8323. a[2] = '\0';
  8324. memset((void*)&T, 0, sizeof(T));
  8325. if (t[1] >= 'A')
  8326. {
  8327. /* format is Sun Nov 6 08:49:37 94 */
  8328. a[0] = t[2];
  8329. a[1] = t[3];
  8330. T.tm_mon = (int)(strstr(mns, a) - mns) / 2;
  8331. a[0] = t[5];
  8332. a[1] = t[6];
  8333. T.tm_mday = (int)soap_strtol(a, NULL, 10);
  8334. if (t[17] && t[18] && t[19] != ' ')
  8335. t += 2; /* format is Sun Nov 6 08:49:37 2017 - ANSI-C */
  8336. a[0] = t[17];
  8337. a[1] = t[18];
  8338. T.tm_year = 100 + (int)soap_strtol(a, NULL, 10);
  8339. t += 6;
  8340. }
  8341. else
  8342. {
  8343. /* format is Sunday, 06-Nov-17 08:49:37 GMT - RFC 850 */
  8344. a[0] = t[1];
  8345. a[1] = t[2];
  8346. T.tm_mday = (int)soap_strtol(a, NULL, 10);
  8347. a[0] = t[5];
  8348. a[1] = t[6];
  8349. T.tm_mon = (int)(strstr(mns, a) - mns) / 2;
  8350. if (t[10] != ' ')
  8351. t += 2; /* format is Wed, 09 Jun 2021 10:18:14 GMT - RFC 822 */
  8352. a[0] = t[8];
  8353. a[1] = t[9];
  8354. T.tm_year = 100 + (int)soap_strtol(a, NULL, 10);
  8355. t += 11;
  8356. }
  8357. a[0] = t[0];
  8358. a[1] = t[1];
  8359. T.tm_hour = (int)soap_strtol(a, NULL, 10);
  8360. a[0] = t[3];
  8361. a[1] = t[4];
  8362. T.tm_min = (int)soap_strtol(a, NULL, 10);
  8363. a[0] = t[6];
  8364. a[1] = t[7];
  8365. T.tm_sec = (int)soap_strtol(a, NULL, 10);
  8366. p->expire = (ULONG64)soap_timegm(&T);
  8367. }
  8368. }
  8369. }
  8370. }
  8371. else if (p && !soap_tag_cmp(tmp, "Secure"))
  8372. {
  8373. p->secure = 1;
  8374. s = soap_decode_val(tmp, sizeof(tmp), s);
  8375. }
  8376. else if (p && !soap_tag_cmp(tmp, "HttpOnly"))
  8377. {
  8378. s = soap_decode_val(tmp, sizeof(tmp), s);
  8379. }
  8380. else if (p && !soap_tag_cmp(tmp, "Comment"))
  8381. {
  8382. s = soap_decode_val(tmp, sizeof(tmp), s);
  8383. }
  8384. else if (*tmp)
  8385. {
  8386. if (p)
  8387. {
  8388. 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));
  8389. q = soap_set_cookie(soap, p->name, p->value, p->domain, p->path);
  8390. if (q)
  8391. {
  8392. q->version = p->version;
  8393. q->expire = p->expire;
  8394. q->secure = p->secure;
  8395. q->env = 1;
  8396. }
  8397. if (p->name)
  8398. SOAP_FREE(soap, p->name);
  8399. if (p->value)
  8400. SOAP_FREE(soap, p->value);
  8401. if (p->domain)
  8402. SOAP_FREE(soap, p->domain);
  8403. if (p->path)
  8404. SOAP_FREE(soap, p->path);
  8405. SOAP_FREE(soap, p);
  8406. }
  8407. p = (struct soap_cookie*)SOAP_MALLOC(soap, sizeof(struct soap_cookie));
  8408. if (p)
  8409. {
  8410. size_t l = strlen(tmp) + 1;
  8411. p->name = NULL;
  8412. if (SOAP_MAXALLOCSIZE <= 0 || l <= SOAP_MAXALLOCSIZE)
  8413. p->name = (char*)SOAP_MALLOC(soap, l);
  8414. if (p->name)
  8415. (void)soap_memcpy(p->name, l, tmp, l);
  8416. s = soap_decode_val(tmp, sizeof(tmp), s);
  8417. if (*tmp)
  8418. {
  8419. l = strlen(tmp) + 1;
  8420. p->value = NULL;
  8421. if (SOAP_MAXALLOCSIZE <= 0 || l <= SOAP_MAXALLOCSIZE)
  8422. p->value = (char*)SOAP_MALLOC(soap, l);
  8423. if (p->value)
  8424. (void)soap_memcpy((void*)p->value, l, (const void*)tmp, l);
  8425. }
  8426. else
  8427. {
  8428. p->value = NULL;
  8429. }
  8430. if (domain)
  8431. {
  8432. p->domain = domain;
  8433. }
  8434. else
  8435. {
  8436. p->domain = NULL;
  8437. }
  8438. if (path)
  8439. {
  8440. p->path = path;
  8441. }
  8442. else if (*soap->path)
  8443. {
  8444. l = strlen(soap->path) + 1;
  8445. p->path = NULL;
  8446. if (SOAP_MAXALLOCSIZE <= 0 || l <= SOAP_MAXALLOCSIZE)
  8447. p->path = (char*)SOAP_MALLOC(soap, l);
  8448. if (p->path)
  8449. (void)soap_memcpy((void*)p->path, l, (const void*)soap->path, l);
  8450. }
  8451. else
  8452. {
  8453. p->path = (char*)SOAP_MALLOC(soap, 2);
  8454. if (p->path)
  8455. (void)soap_memcpy((void*)p->path, 2, (const void*)"/", 2);
  8456. }
  8457. p->expire = 0;
  8458. p->secure = 0;
  8459. p->version = version;
  8460. }
  8461. }
  8462. }
  8463. if (p)
  8464. {
  8465. 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));
  8466. q = soap_set_cookie(soap, p->name, p->value, p->domain, p->path);
  8467. if (q)
  8468. {
  8469. q->version = p->version;
  8470. q->expire = p->expire;
  8471. q->secure = p->secure;
  8472. q->env = 1;
  8473. }
  8474. if (p->name)
  8475. SOAP_FREE(soap, p->name);
  8476. if (p->value)
  8477. SOAP_FREE(soap, p->value);
  8478. if (p->domain)
  8479. SOAP_FREE(soap, p->domain);
  8480. if (p->path)
  8481. SOAP_FREE(soap, p->path);
  8482. SOAP_FREE(soap, p);
  8483. }
  8484. if (domain)
  8485. SOAP_FREE(soap, domain);
  8486. if (path)
  8487. SOAP_FREE(soap, path);
  8488. }
  8489. /******************************************************************************/
  8490. SOAP_FMAC1
  8491. int
  8492. SOAP_FMAC2
  8493. soap_getenv_cookies(struct soap *soap)
  8494. {
  8495. struct soap_cookie *p;
  8496. const char *s;
  8497. char key[4096], val[4096]; /* cookie size is up to 4096 bytes [RFC2109] */
  8498. s = getenv("HTTP_COOKIE");
  8499. if (!s)
  8500. return SOAP_ERR;
  8501. do
  8502. {
  8503. s = soap_decode_key(key, sizeof(key), s);
  8504. s = soap_decode_val(val, sizeof(val), s);
  8505. p = soap_set_cookie(soap, key, val, NULL, NULL);
  8506. if (p)
  8507. p->env = 1;
  8508. } while (*s);
  8509. return SOAP_OK;
  8510. }
  8511. /******************************************************************************/
  8512. SOAP_FMAC1
  8513. struct soap_cookie*
  8514. SOAP_FMAC2
  8515. soap_copy_cookies(struct soap *copy, const struct soap *soap)
  8516. {
  8517. struct soap_cookie *p, **q, *r;
  8518. (void)copy;
  8519. q = &r;
  8520. for (p = soap->cookies; p; p = p->next)
  8521. {
  8522. *q = (struct soap_cookie*)SOAP_MALLOC(copy, sizeof(struct soap_cookie));
  8523. if (!*q)
  8524. return r;
  8525. **q = *p;
  8526. if (p->name)
  8527. {
  8528. size_t l = strlen(p->name) + 1;
  8529. (*q)->name = NULL;
  8530. if (SOAP_MAXALLOCSIZE <= 0 || l <= SOAP_MAXALLOCSIZE)
  8531. (*q)->name = (char*)SOAP_MALLOC(copy, l);
  8532. if ((*q)->name)
  8533. (void)soap_memcpy((*q)->name, l, p->name, l);
  8534. }
  8535. if (p->value)
  8536. {
  8537. size_t l = strlen(p->value) + 1;
  8538. (*q)->value = NULL;
  8539. if (SOAP_MAXALLOCSIZE <= 0 || l <= SOAP_MAXALLOCSIZE)
  8540. (*q)->value = (char*)SOAP_MALLOC(copy, l);
  8541. if ((*q)->value)
  8542. (void)soap_memcpy((*q)->value, l, p->value, l);
  8543. }
  8544. if (p->domain)
  8545. {
  8546. size_t l = strlen(p->domain) + 1;
  8547. (*q)->domain = NULL;
  8548. if (SOAP_MAXALLOCSIZE <= 0 || l <= SOAP_MAXALLOCSIZE)
  8549. (*q)->domain = (char*)SOAP_MALLOC(copy, l);
  8550. if ((*q)->domain)
  8551. (void)soap_memcpy((*q)->domain, l, p->domain, l);
  8552. }
  8553. if (p->path)
  8554. {
  8555. size_t l = strlen(p->path) + 1;
  8556. (*q)->path = NULL;
  8557. if (SOAP_MAXALLOCSIZE <= 0 || l <= SOAP_MAXALLOCSIZE)
  8558. (*q)->path = (char*)SOAP_MALLOC(copy, l);
  8559. if ((*q)->path)
  8560. (void)soap_memcpy((*q)->path, l, p->path, l);
  8561. }
  8562. q = &(*q)->next;
  8563. }
  8564. *q = NULL;
  8565. return r;
  8566. }
  8567. /******************************************************************************/
  8568. SOAP_FMAC1
  8569. void
  8570. SOAP_FMAC2
  8571. soap_free_cookies(struct soap *soap)
  8572. {
  8573. struct soap_cookie *p;
  8574. for (p = soap->cookies; p; p = soap->cookies)
  8575. {
  8576. soap->cookies = p->next;
  8577. SOAP_FREE(soap, p->name);
  8578. if (p->value)
  8579. SOAP_FREE(soap, p->value);
  8580. if (p->domain)
  8581. SOAP_FREE(soap, p->domain);
  8582. if (p->path)
  8583. SOAP_FREE(soap, p->path);
  8584. SOAP_FREE(soap, p);
  8585. }
  8586. }
  8587. /******************************************************************************/
  8588. #endif /* WITH_COOKIES */
  8589. /******************************************************************************/
  8590. SOAP_FMAC1
  8591. size_t
  8592. SOAP_FMAC2
  8593. soap_hash(const char *s) {
  8594. size_t h = 0;
  8595. while (*s)
  8596. h = 65599 * h + *s++;
  8597. return h % SOAP_IDHASH;
  8598. }
  8599. /******************************************************************************/
  8600. static void
  8601. soap_init_pht(struct soap *soap) {
  8602. int i;
  8603. soap->pblk = NULL;
  8604. soap->pidx = 0;
  8605. for (i = 0; i < (int) SOAP_PTRHASH; i++)
  8606. soap->pht[i] = NULL;
  8607. }
  8608. /******************************************************************************/
  8609. SOAP_FMAC1
  8610. struct soap *
  8611. SOAP_FMAC2
  8612. soap_versioning(soap_new)(soap_mode imode, soap_mode omode) {
  8613. struct soap *soap;
  8614. #ifdef __cplusplus
  8615. soap = SOAP_NEW_UNMANAGED(struct soap);
  8616. #else
  8617. soap = (struct soap*)SOAP_MALLOC_UNMANAGED(sizeof(struct soap));
  8618. #endif
  8619. if (soap)
  8620. soap_versioning(soap_init)(soap, imode, omode);
  8621. return soap;
  8622. }
  8623. /******************************************************************************/
  8624. SOAP_FMAC1
  8625. void
  8626. SOAP_FMAC2
  8627. soap_free(struct soap *soap) {
  8628. soap_done(soap);
  8629. #ifdef __cplusplus
  8630. SOAP_DELETE_UNMANAGED(soap);
  8631. #else
  8632. SOAP_FREE_UNMANAGED(soap);
  8633. #endif
  8634. }
  8635. /******************************************************************************/
  8636. SOAP_FMAC1
  8637. void
  8638. SOAP_FMAC2
  8639. soap_del(struct soap *soap) {
  8640. free(soap);
  8641. }
  8642. /******************************************************************************/
  8643. static void
  8644. soap_free_pht(struct soap *soap) {
  8645. struct soap_pblk *pb, *next;
  8646. int i;
  8647. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Free pointer hashtable\n"));
  8648. for (pb = soap->pblk; pb; pb = next) {
  8649. next = pb->next;
  8650. SOAP_FREE(soap, pb);
  8651. }
  8652. soap->pblk = NULL;
  8653. soap->pidx = 0;
  8654. for (i = 0; i < (int) SOAP_PTRHASH; i++)
  8655. soap->pht[i] = NULL;
  8656. }
  8657. /******************************************************************************/
  8658. #ifndef WITH_NOIDREF
  8659. SOAP_FMAC1
  8660. int
  8661. SOAP_FMAC2
  8662. soap_embed(struct soap *soap, const void *p, const void *a, int n, int t) {
  8663. int id;
  8664. struct soap_plist *pp;
  8665. if (soap->version == 2)
  8666. soap->encoding = 1;
  8667. if (!p || (!soap->encodingStyle && !(soap->mode & SOAP_XML_GRAPH)) || (soap->mode & SOAP_XML_TREE))
  8668. return 0;
  8669. if (a)
  8670. id = soap_array_pointer_lookup(soap, p, a, n, t, &pp);
  8671. else
  8672. id = soap_pointer_lookup(soap, p, t, &pp);
  8673. if (id) {
  8674. if (soap_is_embedded(soap, pp)
  8675. || soap_is_single(soap, pp))
  8676. return 0;
  8677. soap_set_embedded(soap, pp);
  8678. }
  8679. return id;
  8680. }
  8681. #endif
  8682. /******************************************************************************/
  8683. SOAP_FMAC1
  8684. int
  8685. SOAP_FMAC2
  8686. soap_pointer_lookup(struct soap *soap, const void *p, int type, struct soap_plist **ppp) {
  8687. struct soap_plist *pp;
  8688. *ppp = NULL;
  8689. if (p) {
  8690. for (pp = soap->pht[soap_hash_ptr(p)]; pp; pp = pp->next) {
  8691. if (pp->ptr == p && pp->type == type) {
  8692. *ppp = pp;
  8693. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Lookup location=%p type=%d id=%d\n", p, type, pp->id));
  8694. return pp->id;
  8695. }
  8696. }
  8697. }
  8698. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Lookup location=%p type=%d: not found\n", p, type));
  8699. return 0;
  8700. }
  8701. /******************************************************************************/
  8702. SOAP_FMAC1
  8703. int
  8704. SOAP_FMAC2
  8705. soap_pointer_enter(struct soap *soap, const void *p, const void *a, int n, int type, struct soap_plist **ppp) {
  8706. size_t h;
  8707. struct soap_plist *pp;
  8708. (void) n;
  8709. if (!soap->pblk || soap->pidx >= SOAP_PTRBLK) {
  8710. struct soap_pblk *pb = (struct soap_pblk *) SOAP_MALLOC(soap, sizeof(struct soap_pblk));
  8711. if (!pb) {
  8712. soap->error = SOAP_EOM;
  8713. return 0;
  8714. }
  8715. pb->next = soap->pblk;
  8716. soap->pblk = pb;
  8717. soap->pidx = 0;
  8718. }
  8719. *ppp = pp = &soap->pblk->plist[soap->pidx++];
  8720. if (a)
  8721. h = soap_hash_ptr(a);
  8722. else
  8723. h = soap_hash_ptr(p);
  8724. DBGLOG(TEST,
  8725. SOAP_MESSAGE(fdebug, "Pointer enter location=%p array=%p size=%lu type=%d id=%d\n", p, a, (unsigned long) n,
  8726. type, soap->idnum + 1));
  8727. pp->next = soap->pht[h];
  8728. pp->type = type;
  8729. pp->mark1 = 0;
  8730. pp->mark2 = 0;
  8731. pp->ptr = p;
  8732. pp->dup = NULL;
  8733. pp->array = a;
  8734. pp->size = n;
  8735. soap->pht[h] = pp;
  8736. pp->id = ++soap->idnum;
  8737. return pp->id;
  8738. }
  8739. /******************************************************************************/
  8740. SOAP_FMAC1
  8741. int
  8742. SOAP_FMAC2
  8743. soap_array_pointer_lookup(struct soap *soap, const void *p, const void *a, int n, int type, struct soap_plist **ppp) {
  8744. struct soap_plist *pp;
  8745. *ppp = NULL;
  8746. if (!p || !a)
  8747. return 0;
  8748. for (pp = soap->pht[soap_hash_ptr(a)]; pp; pp = pp->next) {
  8749. if (pp->type == type && pp->array == a && pp->size == n) {
  8750. *ppp = pp;
  8751. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Array lookup location=%p type=%d id=%d\n", a, type, pp->id));
  8752. return pp->id;
  8753. }
  8754. }
  8755. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Array lookup location=%p type=%d: not found\n", a, type));
  8756. return 0;
  8757. }
  8758. /******************************************************************************/
  8759. SOAP_FMAC1
  8760. int
  8761. SOAP_FMAC2
  8762. soap_begin_count(struct soap *soap) {
  8763. soap_free_ns(soap);
  8764. soap->error = SOAP_OK;
  8765. #ifndef WITH_LEANER
  8766. if ((soap->mode & SOAP_ENC_DIME) || (soap->omode & SOAP_ENC_DIME)) {
  8767. soap->mode = soap->omode | SOAP_IO_LENGTH | SOAP_ENC_DIME;
  8768. } else
  8769. #endif
  8770. {
  8771. soap->mode = soap->omode;
  8772. if ((soap->mode & SOAP_IO_UDP)) {
  8773. soap->mode &= SOAP_IO;
  8774. soap->mode |= SOAP_IO_BUFFER | SOAP_ENC_PLAIN;
  8775. }
  8776. if ((soap->mode & SOAP_IO) == SOAP_IO_STORE
  8777. || (((soap->mode & SOAP_IO) == SOAP_IO_CHUNK || (soap->mode & SOAP_ENC_PLAIN))
  8778. #ifndef WITH_LEANER
  8779. && !soap->fpreparesend
  8780. #endif
  8781. ))
  8782. soap->mode &= ~SOAP_IO_LENGTH;
  8783. else
  8784. soap->mode |= SOAP_IO_LENGTH;
  8785. }
  8786. #ifdef WITH_ZLIB
  8787. if ((soap->mode & SOAP_ENC_ZLIB) && (soap->mode & SOAP_IO) == SOAP_IO_FLUSH)
  8788. {
  8789. if (!(soap->mode & SOAP_ENC_DIME))
  8790. soap->mode &= ~SOAP_IO_LENGTH;
  8791. if ((soap->mode & SOAP_ENC_PLAIN))
  8792. soap->mode |= SOAP_IO_BUFFER;
  8793. else
  8794. soap->mode |= SOAP_IO_STORE;
  8795. }
  8796. #endif
  8797. #ifndef WITH_LEANER
  8798. if ((soap->mode & SOAP_ENC_MTOM) && (soap->mode & SOAP_ENC_DIME))
  8799. soap->mode |= SOAP_ENC_MIME;
  8800. else if (!(soap->mode & SOAP_ENC_MIME))
  8801. soap->mode &= ~SOAP_ENC_MTOM;
  8802. if ((soap->mode & SOAP_ENC_MIME))
  8803. soap_select_mime_boundary(soap);
  8804. soap->dime.list = soap->dime.last; /* keep track of last DIME attachment */
  8805. #endif
  8806. soap->count = 0;
  8807. soap->ns = 0;
  8808. soap->null = 0;
  8809. soap->position = 0;
  8810. soap->mustUnderstand = 0;
  8811. soap->encoding = 0;
  8812. soap->part = SOAP_BEGIN_SEND;
  8813. soap->event = 0;
  8814. soap->evlev = 0;
  8815. soap->idnum = 0;
  8816. soap->body = 1;
  8817. soap->level = 0;
  8818. soap_clr_attr(soap);
  8819. soap_set_local_namespaces(soap);
  8820. #ifndef WITH_LEANER
  8821. soap->dime.size = 0; /* accumulate total size of attachments */
  8822. if (soap->fprepareinitsend && (soap->mode & SOAP_IO) != SOAP_IO_STORE &&
  8823. (soap->error = soap->fprepareinitsend(soap)) != SOAP_OK)
  8824. return soap->error;
  8825. #endif
  8826. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Begin count phase (socket=%d mode=0x%x count="
  8827. SOAP_ULONG_FORMAT
  8828. ")\n", (int) soap->socket, (unsigned int) soap->mode, soap->count));
  8829. #ifndef WITH_LEANER
  8830. if ((soap->mode & SOAP_IO_LENGTH))
  8831. return soap_begin_attachments(soap);
  8832. #endif
  8833. return SOAP_OK;
  8834. }
  8835. /******************************************************************************/
  8836. SOAP_FMAC1
  8837. int
  8838. SOAP_FMAC2
  8839. soap_end_count(struct soap *soap) {
  8840. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "End of count phase\n"));
  8841. #ifndef WITH_LEANER
  8842. if ((soap->mode & SOAP_IO_LENGTH)) {
  8843. if (soap_end_attachments(soap))
  8844. return soap->error;
  8845. if (soap->fpreparefinalsend && (soap->error = soap->fpreparefinalsend(soap)) != SOAP_OK)
  8846. return soap->error;
  8847. }
  8848. #else
  8849. (void)soap;
  8850. #endif
  8851. return SOAP_OK;
  8852. }
  8853. /******************************************************************************/
  8854. static int
  8855. soap_init_send(struct soap *soap) {
  8856. DBGLOG(TEST,
  8857. SOAP_MESSAGE(fdebug, "Initializing for output to socket=%d/fd=%d\n", (int) soap->socket, soap->sendfd));
  8858. *soap->tag = '\0';
  8859. soap_free_ns(soap);
  8860. soap->error = SOAP_OK;
  8861. soap->mode = soap->omode | (soap->mode & (SOAP_IO_LENGTH | SOAP_ENC_DIME));
  8862. #ifndef WITH_LEAN
  8863. if ((soap->mode & SOAP_IO_UDP)) {
  8864. soap->mode &= ~SOAP_IO;
  8865. soap->mode |= SOAP_IO_BUFFER | SOAP_ENC_PLAIN;
  8866. if ((soap->mode & SOAP_IO_LENGTH) && soap->count > sizeof(soap->buf))
  8867. return soap->error = SOAP_UDP_ERROR;
  8868. }
  8869. #endif
  8870. #ifdef WITH_ZLIB
  8871. if ((soap->mode & SOAP_ENC_ZLIB) && (soap->mode & SOAP_IO) == SOAP_IO_FLUSH)
  8872. {
  8873. if ((soap->mode & SOAP_ENC_PLAIN))
  8874. soap->mode |= SOAP_IO_BUFFER;
  8875. else
  8876. soap->mode |= SOAP_IO_STORE;
  8877. }
  8878. #endif
  8879. #if !defined(__cplusplus) || defined(WITH_COMPAT)
  8880. if (soap->os)
  8881. {
  8882. *soap->os = NULL;
  8883. soap->mode = (soap->mode & ~SOAP_IO) | SOAP_IO_STORE;
  8884. }
  8885. else
  8886. #endif
  8887. if ((soap->mode & SOAP_IO) == SOAP_IO_FLUSH && soap_valid_socket(soap->socket)) {
  8888. if ((soap->mode & SOAP_IO_LENGTH) || (soap->mode & SOAP_ENC_PLAIN))
  8889. soap->mode |= SOAP_IO_BUFFER;
  8890. else
  8891. soap->mode |= SOAP_IO_STORE;
  8892. }
  8893. soap->mode &= ~SOAP_IO_LENGTH;
  8894. if ((soap->mode & SOAP_IO) == SOAP_IO_STORE && soap_alloc_block(soap) == NULL)
  8895. return soap->error;
  8896. if (!(soap->mode & SOAP_IO_KEEPALIVE))
  8897. soap->keep_alive = 0;
  8898. #ifndef WITH_LEANER
  8899. if ((soap->mode & SOAP_ENC_MTOM) && (soap->mode & SOAP_ENC_DIME)) {
  8900. soap->mode |= SOAP_ENC_MIME;
  8901. soap->mode &= ~SOAP_ENC_DIME;
  8902. } else if (!(soap->mode & SOAP_ENC_MIME)) {
  8903. soap->mode &= ~SOAP_ENC_MTOM;
  8904. }
  8905. if ((soap->mode & SOAP_ENC_MIME))
  8906. soap_select_mime_boundary(soap);
  8907. #ifdef WIN32
  8908. #ifndef UNDER_CE
  8909. #ifndef WITH_FASTCGI
  8910. if (!soap_valid_socket(soap->socket) && !soap->os && soap->sendfd >= 0) /* Set win32 stdout or soap->sendfd to BINARY, e.g. to support DIME */
  8911. #ifdef __BORLANDC__
  8912. setmode(soap->sendfd, _O_BINARY);
  8913. #else
  8914. _setmode(soap->sendfd, _O_BINARY);
  8915. #endif
  8916. #endif
  8917. #endif
  8918. #endif
  8919. #endif
  8920. if ((soap->mode & SOAP_IO))
  8921. soap->buflen = soap->bufidx = 0;
  8922. soap->chunksize = 0;
  8923. soap->ns = 0;
  8924. soap->null = 0;
  8925. soap->position = 0;
  8926. soap->mustUnderstand = 0;
  8927. soap->encoding = 0;
  8928. soap->event = 0;
  8929. soap->evlev = 0;
  8930. soap->idnum = 0;
  8931. soap->body = 1;
  8932. soap->level = 0;
  8933. soap_clr_attr(soap);
  8934. soap_set_local_namespaces(soap);
  8935. #ifdef WITH_ZLIB
  8936. soap->z_ratio_out = 1.0;
  8937. if ((soap->mode & SOAP_ENC_ZLIB) && soap->zlib_state != SOAP_ZLIB_DEFLATE)
  8938. {
  8939. if (!soap->d_stream)
  8940. {
  8941. soap->d_stream = (z_stream*)SOAP_MALLOC(soap, sizeof(z_stream));
  8942. if (!soap->d_stream)
  8943. return soap->error = SOAP_EOM;
  8944. soap->d_stream->zalloc = Z_NULL;
  8945. soap->d_stream->zfree = Z_NULL;
  8946. soap->d_stream->opaque = Z_NULL;
  8947. soap->d_stream->next_in = Z_NULL;
  8948. }
  8949. if (!soap->z_buf)
  8950. soap->z_buf = (char*)SOAP_MALLOC(soap, sizeof(soap->buf));
  8951. if (!soap->z_buf)
  8952. return soap->error = SOAP_EOM;
  8953. soap->d_stream->next_out = (Byte*)soap->z_buf;
  8954. soap->d_stream->avail_out = sizeof(soap->buf);
  8955. #ifdef WITH_GZIP
  8956. if (soap->zlib_out != SOAP_ZLIB_DEFLATE)
  8957. {
  8958. (void)soap_memcpy((void*)soap->z_buf, sizeof(soap->buf), (const void*)"\37\213\10\0\0\0\0\0\0\377", 10);
  8959. soap->d_stream->next_out = (Byte*)soap->z_buf + 10;
  8960. soap->d_stream->avail_out = sizeof(soap->buf) - 10;
  8961. soap->z_crc = crc32(0L, NULL, 0);
  8962. soap->zlib_out = SOAP_ZLIB_GZIP;
  8963. if (soap->z_dict)
  8964. *((Byte*)soap->z_buf + 2) = 0xff;
  8965. if (deflateInit2(soap->d_stream, soap->z_level, Z_DEFLATED, -MAX_WBITS, 8, Z_DEFAULT_STRATEGY) != Z_OK)
  8966. return soap->error = SOAP_ZLIB_ERROR;
  8967. }
  8968. else
  8969. #endif
  8970. if (deflateInit(soap->d_stream, soap->z_level) != Z_OK)
  8971. return soap->error = SOAP_ZLIB_ERROR;
  8972. if (soap->z_dict)
  8973. {
  8974. if (deflateSetDictionary(soap->d_stream, (const Bytef*)soap->z_dict, soap->z_dict_len) != Z_OK)
  8975. return soap->error = SOAP_ZLIB_ERROR;
  8976. }
  8977. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Deflate initialized\n"));
  8978. soap->zlib_state = SOAP_ZLIB_DEFLATE;
  8979. }
  8980. #endif
  8981. #ifdef WITH_OPENSSL
  8982. if (soap->ssl)
  8983. ERR_clear_error();
  8984. #endif
  8985. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Begin send phase (socket=%d mode=0x%x count="
  8986. SOAP_ULONG_FORMAT
  8987. ")\n", (int) soap->socket, soap->mode, soap->count));
  8988. soap->part = SOAP_BEGIN_SEND;
  8989. #ifndef WITH_LEANER
  8990. if (soap->fprepareinitsend && (soap->mode & SOAP_IO) == SOAP_IO_STORE &&
  8991. (soap->error = soap->fprepareinitsend(soap)) != SOAP_OK)
  8992. return soap->error;
  8993. #endif
  8994. #ifndef WITH_LEAN
  8995. soap->start = (ULONG64) time(NULL);
  8996. #endif
  8997. return SOAP_OK;
  8998. }
  8999. /******************************************************************************/
  9000. SOAP_FMAC1
  9001. int
  9002. SOAP_FMAC2
  9003. soap_begin_send(struct soap *soap) {
  9004. #ifndef WITH_LEANER
  9005. if (soap_init_send(soap))
  9006. return soap->error;
  9007. return soap_begin_attachments(soap);
  9008. #else
  9009. return soap_init_send(soap);
  9010. #endif
  9011. }
  9012. /******************************************************************************/
  9013. #ifndef WITH_NOIDREF
  9014. SOAP_FMAC1
  9015. void
  9016. SOAP_FMAC2
  9017. soap_embedded(struct soap *soap, const void *p, int t) {
  9018. struct soap_plist *pp;
  9019. if (soap_pointer_lookup(soap, p, t, &pp)) {
  9020. pp->mark1 = 1;
  9021. pp->mark2 = 1;
  9022. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Embedded %p type=%d mark set to 1\n", p, t));
  9023. }
  9024. }
  9025. #endif
  9026. /******************************************************************************/
  9027. #ifndef WITH_NOIDREF
  9028. SOAP_FMAC1
  9029. int
  9030. SOAP_FMAC2
  9031. soap_reference(struct soap *soap, const void *p, int t) {
  9032. struct soap_plist *pp;
  9033. if (!p ||
  9034. (!soap->encodingStyle && !(soap->omode & (SOAP_ENC_DIME | SOAP_ENC_MIME | SOAP_ENC_MTOM | SOAP_XML_GRAPH))) ||
  9035. (soap->omode & SOAP_XML_TREE))
  9036. return 1;
  9037. if (soap_pointer_lookup(soap, p, t, &pp)) {
  9038. if (pp->mark1 == 0) {
  9039. pp->mark1 = 2;
  9040. pp->mark2 = 2;
  9041. }
  9042. } else if (!soap_pointer_enter(soap, p, NULL, 0, t, &pp)) {
  9043. return 1;
  9044. }
  9045. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Reference %p type=%d (%d %d)\n", p, t, (int) pp->mark1, (int) pp->mark2));
  9046. return pp->mark1;
  9047. }
  9048. #endif
  9049. /******************************************************************************/
  9050. #ifndef WITH_NOIDREF
  9051. SOAP_FMAC1
  9052. int
  9053. SOAP_FMAC2
  9054. soap_array_reference(struct soap *soap, const void *p, const void *a, int n, int t) {
  9055. struct soap_plist *pp;
  9056. if (!p || !a || (!soap->encodingStyle && !(soap->omode & SOAP_XML_GRAPH)) || (soap->omode & SOAP_XML_TREE))
  9057. return 1;
  9058. if (soap_array_pointer_lookup(soap, p, a, n, t, &pp)) {
  9059. if (pp->mark1 == 0) {
  9060. pp->mark1 = 2;
  9061. pp->mark2 = 2;
  9062. }
  9063. } else if (!soap_pointer_enter(soap, p, a, n, t, &pp)) {
  9064. return 1;
  9065. }
  9066. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Array reference %p ptr=%p n=%lu t=%d (%d %d)\n", p, a, (unsigned long) n, t,
  9067. (int) pp->mark1, (int) pp->mark2));
  9068. return pp->mark1;
  9069. }
  9070. #endif
  9071. /******************************************************************************/
  9072. #ifndef WITH_NOIDREF
  9073. SOAP_FMAC1
  9074. int
  9075. SOAP_FMAC2
  9076. soap_attachment_reference(struct soap *soap, const void *p, const void *a, int n, int t, const char *id,
  9077. const char *type) {
  9078. struct soap_plist *pp;
  9079. if (!p || !a || (!soap->encodingStyle && !(soap->omode & SOAP_XML_GRAPH) && !id && !type) ||
  9080. (soap->omode & SOAP_XML_TREE))
  9081. return 1;
  9082. if (soap_array_pointer_lookup(soap, p, a, n, t, &pp)) {
  9083. if (pp->mark1 == 0) {
  9084. pp->mark1 = 2;
  9085. pp->mark2 = 2;
  9086. }
  9087. } else if (!soap_pointer_enter(soap, p, a, n, t, &pp)) {
  9088. return 1;
  9089. }
  9090. if (id || type)
  9091. soap->mode |= SOAP_ENC_DIME;
  9092. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Attachment reference %p ptr=%p n=%lu t=%d (%d %d)\n", p, a, (unsigned long) n, t,
  9093. (int) pp->mark1, (int) pp->mark2));
  9094. return pp->mark1;
  9095. }
  9096. #endif
  9097. /******************************************************************************/
  9098. #ifndef WITH_NOIDREF
  9099. SOAP_FMAC1
  9100. int
  9101. SOAP_FMAC2
  9102. soap_embedded_id(struct soap *soap, int id, const void *p, int t) {
  9103. struct soap_plist *pp = NULL;
  9104. if (id >= 0 || (!soap->encodingStyle && !(soap->omode & SOAP_XML_GRAPH)) || (soap->omode & SOAP_XML_TREE))
  9105. return id;
  9106. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Embedded_id %p type=%d id=%d\n", p, t, id));
  9107. if (id < -1)
  9108. return soap_embed(soap, p, NULL, 0, t);
  9109. if (id < 0) {
  9110. id = soap_pointer_lookup(soap, p, t, &pp);
  9111. if (soap->version == 1 && soap->part != SOAP_IN_HEADER) {
  9112. if (id) {
  9113. if ((soap->mode & SOAP_IO_LENGTH))
  9114. pp->mark1 = 2;
  9115. else
  9116. pp->mark2 = 2;
  9117. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Embedded_id multiref id=%d %p type=%d = (%d %d)\n", id, p, t,
  9118. (int) pp->mark1, (int) pp->mark2));
  9119. }
  9120. return -1;
  9121. } else if (id) {
  9122. if ((soap->mode & SOAP_IO_LENGTH))
  9123. pp->mark1 = 1;
  9124. else
  9125. pp->mark2 = 1;
  9126. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Embedded_id embedded ref id=%d %p type=%d = (%d %d)\n", id, p, t,
  9127. (int) pp->mark1, (int) pp->mark2));
  9128. }
  9129. }
  9130. return id;
  9131. }
  9132. #endif
  9133. /******************************************************************************/
  9134. #ifndef WITH_NOIDREF
  9135. SOAP_FMAC1
  9136. int
  9137. SOAP_FMAC2
  9138. soap_is_embedded(struct soap *soap, struct soap_plist *pp) {
  9139. if (!pp)
  9140. return 0;
  9141. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Is embedded? %d %d\n", (int) pp->mark1, (int) pp->mark2));
  9142. if (soap->version == 1 && soap->encodingStyle && !(soap->omode & SOAP_XML_GRAPH) && soap->part != SOAP_IN_HEADER) {
  9143. if ((soap->mode & SOAP_IO_LENGTH))
  9144. return pp->mark1 != 0;
  9145. return pp->mark2 != 0;
  9146. }
  9147. if ((soap->mode & SOAP_IO_LENGTH))
  9148. return pp->mark1 == 1;
  9149. return pp->mark2 == 1;
  9150. }
  9151. #endif
  9152. /******************************************************************************/
  9153. #ifndef WITH_NOIDREF
  9154. SOAP_FMAC1
  9155. int
  9156. SOAP_FMAC2
  9157. soap_is_single(struct soap *soap, struct soap_plist *pp) {
  9158. if (soap->part == SOAP_IN_HEADER)
  9159. return 1;
  9160. if (!pp)
  9161. return 0;
  9162. if ((soap->mode & SOAP_IO_LENGTH))
  9163. return pp->mark1 == 0;
  9164. return pp->mark2 == 0;
  9165. }
  9166. #endif
  9167. /******************************************************************************/
  9168. #ifndef WITH_NOIDREF
  9169. SOAP_FMAC1
  9170. void
  9171. SOAP_FMAC2
  9172. soap_set_embedded(struct soap *soap, struct soap_plist *pp) {
  9173. if (!pp)
  9174. return;
  9175. if ((soap->mode & SOAP_IO_LENGTH))
  9176. pp->mark1 = 1;
  9177. else
  9178. pp->mark2 = 1;
  9179. }
  9180. #endif
  9181. /******************************************************************************/
  9182. #ifndef WITH_LEANER
  9183. SOAP_FMAC1
  9184. int
  9185. SOAP_FMAC2
  9186. soap_attachment(struct soap *soap, const char *tag, int id, const void *p, const void *a, int n, const char *aid,
  9187. const char *atype, const char *aoptions, const char *type, int t) {
  9188. struct soap_plist *pp;
  9189. int i;
  9190. if (!p || !a || (!aid && !atype) ||
  9191. (!soap->encodingStyle && !(soap->omode & (SOAP_ENC_DIME | SOAP_ENC_MIME | SOAP_ENC_MTOM | SOAP_XML_GRAPH))) ||
  9192. (soap->omode & SOAP_XML_TREE))
  9193. return soap_element_id(soap, tag, id, p, a, n, type, t, NULL);
  9194. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Attachment tag='%s' id='%s' (%d) type='%s'\n", tag, aid ? aid : SOAP_STR_EOS, id,
  9195. atype ? atype : SOAP_STR_EOS));
  9196. i = soap_array_pointer_lookup(soap, p, a, n, t, &pp);
  9197. if (!i) {
  9198. i = soap_pointer_enter(soap, p, a, n, t, &pp);
  9199. if (!i) {
  9200. soap->error = SOAP_EOM;
  9201. return -1;
  9202. }
  9203. }
  9204. if (id <= 0)
  9205. id = i;
  9206. if (!aid) {
  9207. (SOAP_SNPRINTF(soap->tmpbuf, sizeof(soap->tmpbuf), strlen(soap->dime_id_format) + 20), soap->dime_id_format,
  9208. id);
  9209. aid = soap_strdup(soap, soap->tmpbuf);
  9210. if (!aid)
  9211. return -1;
  9212. }
  9213. /* Add MTOM xop:Include element when necessary */
  9214. /* TODO: this code to be obsoleted with new import/xop.h conventions */
  9215. if ((soap->omode & SOAP_ENC_MTOM) && strcmp(tag, "xop:Include")) {
  9216. if (soap_element_begin_out(soap, tag, 0, type)
  9217. ||
  9218. soap_element_href(soap, "xop:Include", 0, "xmlns:xop=\"http://www.w3.org/2004/08/xop/include\" href", aid)
  9219. || soap_element_end_out(soap, tag))
  9220. return soap->error;
  9221. } else if (soap_element_href(soap, tag, 0, "href", aid)) {
  9222. return soap->error;
  9223. }
  9224. if ((soap->mode & SOAP_IO_LENGTH)) {
  9225. if (pp->mark1 != 3) {
  9226. struct soap_multipart *content;
  9227. if ((soap->omode & SOAP_ENC_MTOM))
  9228. content = soap_alloc_multipart(soap, &soap->mime.first, &soap->mime.last, (const char *) a, n);
  9229. else
  9230. content = soap_alloc_multipart(soap, &soap->dime.first, &soap->dime.last, (const char *) a, n);
  9231. if (!content) {
  9232. soap->error = SOAP_EOM;
  9233. return -1;
  9234. }
  9235. if (!strncmp(aid, "cid:", 4)) /* RFC 2111 */
  9236. {
  9237. if ((soap->omode & SOAP_ENC_MTOM)) {
  9238. size_t l = strlen(aid) - 1;
  9239. char *s = (char *) soap_malloc(soap, l);
  9240. if (s) {
  9241. s[0] = '<';
  9242. (void) soap_strncpy(s + 1, l - 1, aid + 4, l - 3);
  9243. s[l - 2] = '>';
  9244. s[l - 1] = '\0';
  9245. content->id = s;
  9246. }
  9247. } else {
  9248. content->id = aid + 4;
  9249. }
  9250. } else {
  9251. content->id = aid;
  9252. }
  9253. content->type = atype;
  9254. content->options = aoptions;
  9255. content->encoding = SOAP_MIME_BINARY;
  9256. pp->mark1 = 3;
  9257. }
  9258. } else {
  9259. pp->mark2 = 3;
  9260. }
  9261. return -1;
  9262. }
  9263. #endif
  9264. /******************************************************************************/
  9265. #ifndef WITH_NOIDREF
  9266. static void
  9267. soap_init_iht(struct soap *soap) {
  9268. int i;
  9269. for (i = 0; i < SOAP_IDHASH; i++)
  9270. soap->iht[i] = NULL;
  9271. }
  9272. #endif
  9273. /******************************************************************************/
  9274. #ifndef WITH_NOIDREF
  9275. static void
  9276. soap_free_iht(struct soap *soap) {
  9277. int i;
  9278. struct soap_ilist *ip = NULL, *p = NULL;
  9279. struct soap_flist *fp = NULL, *fq = NULL;
  9280. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Free ID hashtable\n"));
  9281. for (i = 0; i < SOAP_IDHASH; i++) {
  9282. for (ip = soap->iht[i]; ip; ip = p) {
  9283. for (fp = ip->flist; fp; fp = fq) {
  9284. fq = fp->next;
  9285. SOAP_FREE(soap, fp);
  9286. }
  9287. p = ip->next;
  9288. SOAP_FREE(soap, ip);
  9289. }
  9290. soap->iht[i] = NULL;
  9291. }
  9292. }
  9293. #endif
  9294. /******************************************************************************/
  9295. #ifndef WITH_NOIDREF
  9296. SOAP_FMAC1
  9297. struct soap_ilist *
  9298. SOAP_FMAC2
  9299. soap_lookup(struct soap *soap, const char *id) {
  9300. struct soap_ilist *ip = NULL;
  9301. for (ip = soap->iht[soap_hash(id)]; ip; ip = ip->next)
  9302. if (!strcmp(ip->id, id))
  9303. return ip;
  9304. return NULL;
  9305. }
  9306. #endif
  9307. /******************************************************************************/
  9308. #ifndef WITH_NOIDREF
  9309. SOAP_FMAC1
  9310. struct soap_ilist *
  9311. SOAP_FMAC2
  9312. soap_enter(struct soap *soap, const char *id, int t, size_t n) {
  9313. size_t h;
  9314. struct soap_ilist *ip = NULL;
  9315. size_t l = strlen(id);
  9316. if (sizeof(struct soap_ilist) + l > l &&
  9317. (SOAP_MAXALLOCSIZE <= 0 || sizeof(struct soap_ilist) + l <= SOAP_MAXALLOCSIZE))
  9318. ip = (struct soap_ilist *) SOAP_MALLOC(soap, sizeof(struct soap_ilist) + l);
  9319. if (ip) {
  9320. ip->type = t;
  9321. ip->size = n;
  9322. ip->ptr = NULL;
  9323. ip->spine = NULL;
  9324. ip->link = NULL;
  9325. ip->copy = NULL;
  9326. ip->flist = NULL;
  9327. ip->smart = NULL;
  9328. ip->shaky = 0;
  9329. (void) soap_memcpy((char *) ip->id, l + 1, id, l + 1);
  9330. h = soap_hash(id); /* h = (HASH(id) % SOAP_IDHASH) so soap->iht[h] is safe */
  9331. ip->next = soap->iht[h];
  9332. soap->iht[h] = ip;
  9333. }
  9334. return ip;
  9335. }
  9336. #endif
  9337. /******************************************************************************/
  9338. SOAP_FMAC1
  9339. void *
  9340. SOAP_FMAC2
  9341. soap_malloc(struct soap *soap, size_t n) {
  9342. char *p;
  9343. size_t k = n;
  9344. if (SOAP_MAXALLOCSIZE > 0 && n > SOAP_MAXALLOCSIZE) {
  9345. soap->error = SOAP_EOM;
  9346. return NULL;
  9347. }
  9348. if (!soap)
  9349. return SOAP_MALLOC(soap, n);
  9350. n += sizeof(short);
  9351. n += (~n + 1) & (sizeof(void *) - 1); /* align at 4-, 8- or 16-byte boundary by rounding up */
  9352. if (n + sizeof(void *) + sizeof(size_t) < k) {
  9353. soap->error = SOAP_EOM;
  9354. return NULL;
  9355. }
  9356. p = (char *) SOAP_MALLOC(soap, n + sizeof(void *) + sizeof(size_t));
  9357. if (!p) {
  9358. soap->error = SOAP_EOM;
  9359. return NULL;
  9360. }
  9361. /* set a canary word to detect memory overruns and data corruption */
  9362. *(unsigned short *) (p + n - sizeof(unsigned short)) = (unsigned short) SOAP_CANARY;
  9363. /* keep chain of alloced cells for destruction */
  9364. *(void **) (p + n) = soap->alist;
  9365. *(size_t *) (p + n + sizeof(void *)) = n;
  9366. soap->alist = p + n;
  9367. return p;
  9368. }
  9369. /******************************************************************************/
  9370. #ifdef SOAP_MEM_DEBUG
  9371. static void
  9372. soap_init_mht(struct soap *soap) {
  9373. int i;
  9374. for (i = 0; i < (int) SOAP_PTRHASH; i++)
  9375. soap->mht[i] = NULL;
  9376. }
  9377. #endif
  9378. /******************************************************************************/
  9379. #ifdef SOAP_MEM_DEBUG
  9380. static void
  9381. soap_free_mht(struct soap *soap) {
  9382. int i;
  9383. struct soap_mlist *mp, *mq;
  9384. for (i = 0; i < (int) SOAP_PTRHASH; i++) {
  9385. for (mp = soap->mht[i]; mp; mp = mq) {
  9386. mq = mp->next;
  9387. if (mp->live)
  9388. fprintf(stderr, "%s(%d): malloc() = %p not freed (memory leak or forgot to call soap_end()?)\n",
  9389. mp->file, mp->line, mp->ptr);
  9390. free(mp);
  9391. }
  9392. soap->mht[i] = NULL;
  9393. }
  9394. }
  9395. #endif
  9396. /******************************************************************************/
  9397. #ifdef SOAP_MEM_DEBUG
  9398. SOAP_FMAC1
  9399. void *
  9400. SOAP_FMAC2
  9401. soap_track_malloc(struct soap *soap, const char *file, int line, size_t size) {
  9402. void *p = malloc(size);
  9403. if (soap) {
  9404. size_t h = soap_hash_ptr(p);
  9405. struct soap_mlist *mp = (struct soap_mlist *) malloc(sizeof(struct soap_mlist));
  9406. if (soap->fdebug[SOAP_INDEX_TEST]) {
  9407. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "%s(%d): malloc(%lu) = %p\n", file, line, (unsigned long) size, p));
  9408. }
  9409. mp->next = soap->mht[h];
  9410. mp->ptr = p;
  9411. mp->file = file;
  9412. mp->line = line;
  9413. mp->live = 1;
  9414. soap->mht[h] = mp;
  9415. }
  9416. return p;
  9417. }
  9418. #endif
  9419. /******************************************************************************/
  9420. #ifdef SOAP_MEM_DEBUG
  9421. SOAP_FMAC1
  9422. void
  9423. SOAP_FMAC2
  9424. soap_track_free(struct soap *soap, const char *file, int line, void *p) {
  9425. if (!soap) {
  9426. free(p);
  9427. } else {
  9428. size_t h = soap_hash_ptr(p);
  9429. struct soap_mlist *mp;
  9430. for (mp = soap->mht[h]; mp; mp = mp->next)
  9431. if (mp->ptr == p)
  9432. break;
  9433. if (mp) {
  9434. if (mp->live) {
  9435. if (soap->fdebug[SOAP_INDEX_TEST]) {
  9436. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "%s(%d): free(%p)\n", file, line, p));
  9437. }
  9438. free(p);
  9439. mp->live = 0;
  9440. } else {
  9441. fprintf(stderr, "%s(%d): free(%p) double free of pointer malloced at %s(%d)\n", file, line, p, mp->file,
  9442. mp->line);
  9443. }
  9444. } else {
  9445. fprintf(stderr, "%s(%d): free(%p) pointer not malloced\n", file, line, p);
  9446. }
  9447. }
  9448. }
  9449. #endif
  9450. /******************************************************************************/
  9451. #ifdef SOAP_MEM_DEBUG
  9452. static void
  9453. soap_track_unlink(struct soap *soap, const void *p) {
  9454. size_t h = soap_hash_ptr(p);
  9455. struct soap_mlist *mp;
  9456. for (mp = soap->mht[h]; mp; mp = mp->next)
  9457. if (mp->ptr == p)
  9458. break;
  9459. if (mp)
  9460. mp->live = 0;
  9461. }
  9462. #endif
  9463. /******************************************************************************/
  9464. SOAP_FMAC1
  9465. void
  9466. SOAP_FMAC2
  9467. soap_dealloc(struct soap *soap, void *p) {
  9468. if (soap_check_state(soap))
  9469. return;
  9470. if (p) {
  9471. char **q;
  9472. for (q = (char **) (void *) &soap->alist; *q; q = *(char ***) q) {
  9473. if (*(unsigned short *) (char *) (*q - sizeof(unsigned short)) != (unsigned short) SOAP_CANARY) {
  9474. #ifdef SOAP_MEM_DEBUG
  9475. fprintf(stderr, "Data corruption in dynamic allocation (see logs)\n");
  9476. #endif
  9477. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Data corruption:\n"));
  9478. DBGHEX(TEST, *q - 200, 200);
  9479. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "\n"));
  9480. soap->error = SOAP_MOE;
  9481. return;
  9482. }
  9483. if (p == (void *) (*q - *(size_t *) (*q + sizeof(void *)))) {
  9484. *q = **(char ***) q;
  9485. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Freed data at %p\n", p));
  9486. SOAP_FREE(soap, p);
  9487. return;
  9488. }
  9489. }
  9490. soap_delete(soap, p);
  9491. } else {
  9492. char *q;
  9493. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Free all soap_malloc() data\n"));
  9494. while (soap->alist) {
  9495. q = (char *) soap->alist;
  9496. if (*(unsigned short *) (char *) (q - sizeof(unsigned short)) != (unsigned short) SOAP_CANARY) {
  9497. #ifdef SOAP_MEM_DEBUG
  9498. fprintf(stderr, "Data corruption in dynamic allocation (see logs)\n");
  9499. #endif
  9500. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Data corruption:\n"));
  9501. DBGHEX(TEST, q - 200, 200);
  9502. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "\n"));
  9503. soap->error = SOAP_MOE;
  9504. return;
  9505. }
  9506. soap->alist = *(void **) q;
  9507. q -= *(size_t *) (q + sizeof(void *));
  9508. SOAP_FREE(soap, q);
  9509. }
  9510. /* assume these were deallocated: */
  9511. soap->http_content = NULL;
  9512. soap->action = NULL;
  9513. soap->fault = NULL;
  9514. soap->header = NULL;
  9515. soap->bearer = NULL;
  9516. soap->userid = NULL;
  9517. soap->passwd = NULL;
  9518. soap->authrealm = NULL;
  9519. #ifdef WITH_NTLM
  9520. soap->ntlm_challenge = NULL;
  9521. #endif
  9522. #ifndef WITH_LEANER
  9523. soap_clr_mime(soap);
  9524. #endif
  9525. }
  9526. }
  9527. /******************************************************************************/
  9528. SOAP_FMAC1
  9529. void
  9530. SOAP_FMAC2
  9531. soap_delete(struct soap *soap, void *p) {
  9532. struct soap_clist **cp;
  9533. if (soap_check_state(soap))
  9534. return;
  9535. cp = &soap->clist;
  9536. if (p) {
  9537. while (*cp) {
  9538. if (p == (*cp)->ptr) {
  9539. struct soap_clist *q = *cp;
  9540. *cp = q->next;
  9541. if (q->fdelete(soap, q)) {
  9542. DBGLOG(TEST, SOAP_MESSAGE(fdebug,
  9543. "Could not dealloc data %p: deletion callback failed for object type=%d\n",
  9544. q->ptr, q->type));
  9545. #ifdef SOAP_MEM_DEBUG
  9546. fprintf(stderr, "new(object type=%d) = %p not freed: deletion callback failed\n", q->type, q->ptr);
  9547. #endif
  9548. }
  9549. SOAP_FREE(soap, q);
  9550. return;
  9551. }
  9552. cp = &(*cp)->next;
  9553. }
  9554. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Could not dealloc data %p: address not in list\n", p));
  9555. } else {
  9556. while (*cp) {
  9557. struct soap_clist *q = *cp;
  9558. *cp = q->next;
  9559. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Delete %p type=%d (cp=%p)\n", q->ptr, q->type, (void *) q));
  9560. if (q->fdelete(soap, q)) {
  9561. DBGLOG(TEST,
  9562. SOAP_MESSAGE(fdebug, "Could not dealloc data %p: deletion callback failed for object type=%d\n",
  9563. q->ptr, q->type));
  9564. #ifdef SOAP_MEM_DEBUG
  9565. fprintf(stderr, "new(object type=%d) = %p not freed: deletion callback failed\n", q->type, q->ptr);
  9566. #endif
  9567. }
  9568. SOAP_FREE(soap, q);
  9569. }
  9570. }
  9571. soap->fault = NULL; /* assume this was deallocated */
  9572. soap->header = NULL; /* assume this was deallocated */
  9573. }
  9574. /******************************************************************************/
  9575. SOAP_FMAC1
  9576. void
  9577. SOAP_FMAC2
  9578. soap_delegate_deletion(struct soap *soap, struct soap *soap_to) {
  9579. struct soap_clist *cp;
  9580. char **q;
  9581. #ifdef SOAP_MEM_DEBUG
  9582. void *p;
  9583. struct soap_mlist **mp, *mq;
  9584. size_t h;
  9585. #endif
  9586. for (q = (char **) (void *) &soap->alist; *q; q = *(char ***) q) {
  9587. if (*(unsigned short *) (char *) (*q - sizeof(unsigned short)) != (unsigned short) SOAP_CANARY) {
  9588. #ifdef SOAP_MEM_DEBUG
  9589. fprintf(stderr, "Data corruption in dynamic allocation (see logs)\n");
  9590. #endif
  9591. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Data corruption:\n"));
  9592. DBGHEX(TEST, *q - 200, 200);
  9593. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "\n"));
  9594. soap->error = SOAP_MOE;
  9595. return;
  9596. }
  9597. #ifdef SOAP_MEM_DEBUG
  9598. p = (void *) (*q - *(size_t *) (*q + sizeof(void *)));
  9599. h = soap_hash_ptr(p);
  9600. for (mp = &soap->mht[h]; *mp; mp = &(*mp)->next) {
  9601. if ((*mp)->ptr == p) {
  9602. mq = *mp;
  9603. *mp = mq->next;
  9604. mq->next = soap_to->mht[h];
  9605. soap_to->mht[h] = mq;
  9606. break;
  9607. }
  9608. }
  9609. #endif
  9610. }
  9611. *q = (char *) soap_to->alist;
  9612. soap_to->alist = soap->alist;
  9613. soap->alist = NULL;
  9614. #ifdef SOAP_MEM_DEBUG
  9615. cp = soap->clist;
  9616. while (cp) {
  9617. h = soap_hash_ptr(cp);
  9618. for (mp = &soap->mht[h]; *mp; mp = &(*mp)->next) {
  9619. if ((*mp)->ptr == cp) {
  9620. mq = *mp;
  9621. *mp = mq->next;
  9622. mq->next = soap_to->mht[h];
  9623. soap_to->mht[h] = mq;
  9624. break;
  9625. }
  9626. }
  9627. cp = cp->next;
  9628. }
  9629. #endif
  9630. cp = soap_to->clist;
  9631. if (cp) {
  9632. while (cp->next)
  9633. cp = cp->next;
  9634. cp->next = soap->clist;
  9635. } else {
  9636. soap_to->clist = soap->clist;
  9637. }
  9638. soap->clist = NULL;
  9639. }
  9640. /******************************************************************************/
  9641. SOAP_FMAC1
  9642. struct soap_clist *
  9643. SOAP_FMAC2
  9644. soap_link(struct soap *soap, int t, int n, int (*fdelete)(struct soap *, struct soap_clist *)) {
  9645. struct soap_clist *cp = NULL;
  9646. if (soap) {
  9647. if (n != SOAP_NO_LINK_TO_DELETE) {
  9648. cp = (struct soap_clist *) SOAP_MALLOC(soap, sizeof(struct soap_clist));
  9649. if (!cp) {
  9650. soap->error = SOAP_EOM;
  9651. } else {
  9652. cp->next = soap->clist;
  9653. cp->type = t;
  9654. cp->size = n;
  9655. cp->ptr = NULL;
  9656. cp->fdelete = fdelete;
  9657. soap->clist = cp;
  9658. }
  9659. }
  9660. soap->alloced = t;
  9661. }
  9662. return cp;
  9663. }
  9664. /******************************************************************************/
  9665. SOAP_FMAC1
  9666. int
  9667. SOAP_FMAC2
  9668. soap_unlink(struct soap *soap, const void *p) {
  9669. char **q;
  9670. struct soap_clist **cp;
  9671. if (soap && p) {
  9672. for (q = (char **) (void *) &soap->alist; *q; q = *(char ***) q) {
  9673. if (p == (void *) (*q - *(size_t *) (*q + sizeof(void *)))) {
  9674. *q = **(char ***) q;
  9675. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Unlinked data %p\n", p));
  9676. #ifdef SOAP_MEM_DEBUG
  9677. soap_track_unlink(soap, p);
  9678. #endif
  9679. return SOAP_OK; /* found and removed from dealloc chain */
  9680. }
  9681. }
  9682. for (cp = &soap->clist; *cp; cp = &(*cp)->next) {
  9683. if (p == (*cp)->ptr) {
  9684. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Unlinked class instance %p\n", p));
  9685. q = (char **) *cp;
  9686. *cp = (*cp)->next;
  9687. SOAP_FREE(soap, q);
  9688. return SOAP_OK; /* found and removed from dealloc chain */
  9689. }
  9690. }
  9691. }
  9692. return SOAP_ERR;
  9693. }
  9694. /******************************************************************************/
  9695. #ifndef WITH_NOIDREF
  9696. SOAP_FMAC1
  9697. int
  9698. SOAP_FMAC2
  9699. soap_lookup_type(struct soap *soap, const char *id) {
  9700. struct soap_ilist *ip;
  9701. if (id && *id) {
  9702. ip = soap_lookup(soap, id);
  9703. if (ip) {
  9704. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Lookup id='%s' type=%d\n", id, ip->type));
  9705. return ip->type;
  9706. }
  9707. }
  9708. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "lookup type id='%s' NOT FOUND! Need to get it from xsi:type\n", id));
  9709. return 0;
  9710. }
  9711. #endif
  9712. /******************************************************************************/
  9713. #ifndef WITH_NOIDREF
  9714. SOAP_FMAC1
  9715. short
  9716. SOAP_FMAC2
  9717. soap_begin_shaky(struct soap *soap) {
  9718. short f = soap->shaky;
  9719. soap->shaky = 1;
  9720. return f;
  9721. }
  9722. #endif
  9723. /******************************************************************************/
  9724. #ifndef WITH_NOIDREF
  9725. SOAP_FMAC1
  9726. void
  9727. SOAP_FMAC2
  9728. soap_end_shaky(struct soap *soap, short f) {
  9729. soap->shaky = f;
  9730. }
  9731. #endif
  9732. /******************************************************************************/
  9733. #ifndef WITH_NOIDREF
  9734. static int
  9735. soap_is_shaky(struct soap *soap, void *p) {
  9736. (void) p;
  9737. if (!soap->blist && !soap->shaky)
  9738. return 0;
  9739. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Shaky %p\n", p));
  9740. return 1;
  9741. }
  9742. #endif
  9743. /******************************************************************************/
  9744. #ifndef WITH_NOIDREF
  9745. SOAP_FMAC1
  9746. void *
  9747. SOAP_FMAC2
  9748. soap_id_lookup(struct soap *soap, const char *id, void **p, int t, size_t n, unsigned int k, int (*fbase)(int, int)) {
  9749. struct soap_ilist *ip;
  9750. if (!p || !id || !*id)
  9751. return p;
  9752. ip = soap_lookup(soap, id); /* lookup pointer to hash table entry for string id */
  9753. if (!ip) {
  9754. ip = soap_enter(soap, id, t, n); /* new hash table entry for string id */
  9755. if (!ip)
  9756. return NULL;
  9757. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Forwarding first href='%s' type=%d location=%p (%u bytes) level=%u\n", id, t,
  9758. (void *) p, (unsigned int) n, k));
  9759. *p = NULL;
  9760. if (k) {
  9761. int i;
  9762. if (k > SOAP_MAXPTRS)
  9763. return NULL;
  9764. ip->spine = (void **) soap_malloc(soap, SOAP_MAXPTRS * sizeof(void *));
  9765. if (!ip->spine)
  9766. return NULL;
  9767. ip->spine[0] = NULL;
  9768. for (i = 1; i < SOAP_MAXPTRS; i++)
  9769. ip->spine[i] = &ip->spine[i - 1];
  9770. *p = &ip->spine[k - 1];
  9771. } else {
  9772. ip->link = p;
  9773. ip->shaky = soap_is_shaky(soap, (void *) p);
  9774. }
  9775. } else if (ip->type != t && (!fbase || !fbase(ip->type, t)) &&
  9776. (!fbase || !fbase(t, ip->type) || soap_type_punned(soap, ip))) {
  9777. DBGLOG(TEST,
  9778. SOAP_MESSAGE(fdebug, "Lookup type incompatibility: ref='%s' id-type=%d ref-type=%d\n", id, ip->type, t));
  9779. (void) soap_id_nullify(soap, id);
  9780. return NULL;
  9781. } else if (k == 0 && ip->ptr && !ip->shaky) /* when block lists are in use, ip->ptr will change */
  9782. {
  9783. DBGLOG(TEST,
  9784. SOAP_MESSAGE(fdebug, "Resolved href='%s' type=%d location=%p (%u bytes) level=%u\n", id, t, ip->ptr,
  9785. (unsigned int) n, k));
  9786. *p = ip->ptr;
  9787. } else {
  9788. DBGLOG(TEST,
  9789. SOAP_MESSAGE(fdebug, "Forwarded href='%s' type=%d location=%p (%u bytes) level=%u\n", id, t, (void *) p,
  9790. (unsigned int) n, k));
  9791. if (fbase && fbase(t, ip->type) && !soap_type_punned(soap, ip)) {
  9792. ip->type = t;
  9793. ip->size = n;
  9794. }
  9795. *p = NULL;
  9796. if (k) {
  9797. if (!ip->spine) {
  9798. int i;
  9799. if (k > SOAP_MAXPTRS)
  9800. return NULL;
  9801. ip->spine = (void **) soap_malloc(soap, SOAP_MAXPTRS * sizeof(void *));
  9802. if (!ip->spine)
  9803. return NULL;
  9804. ip->spine[0] = NULL;
  9805. for (i = 1; i < SOAP_MAXPTRS; i++)
  9806. ip->spine[i] = &ip->spine[i - 1];
  9807. }
  9808. *p = &ip->spine[k - 1];
  9809. if (ip->ptr && !ip->shaky)
  9810. ip->spine[0] = ip->ptr;
  9811. } else {
  9812. void *q = ip->link;
  9813. ip->link = p;
  9814. ip->shaky = soap_is_shaky(soap, (void *) p);
  9815. *p = q;
  9816. }
  9817. }
  9818. return p;
  9819. }
  9820. #endif
  9821. /******************************************************************************/
  9822. #ifndef WITH_NOIDREF
  9823. SOAP_FMAC1
  9824. void *
  9825. SOAP_FMAC2
  9826. soap_id_forward(struct soap *soap, const char *href, void *p, size_t i, int t, int tt, size_t n, unsigned int k,
  9827. void (*finsert)(struct soap *, int, int, void *, size_t, const void *, void **),
  9828. int (*fbase)(int, int)) {
  9829. struct soap_ilist *ip;
  9830. if (!p || !href || !*href)
  9831. return p;
  9832. ip = soap_lookup(soap, href); /* lookup pointer to hash table entry for string id */
  9833. if (!ip) {
  9834. ip = soap_enter(soap, href, t, n); /* new hash table entry for string id */
  9835. if (!ip)
  9836. return NULL;
  9837. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "New entry href='%s' type=%d size=%lu level=%d location=%p\n", href, t,
  9838. (unsigned long) n, k, p));
  9839. } else if ((ip->type != t || ip->size != n) && k == 0) {
  9840. DBGLOG(TEST, SOAP_MESSAGE(fdebug,
  9841. "Forward type incompatibility id='%s' expect type=%d size=%lu level=%u got type=%d size=%lu\n",
  9842. href, ip->type, (unsigned long) ip->size, k, t, (unsigned long) n));
  9843. (void) soap_id_nullify(soap, href);
  9844. return NULL;
  9845. }
  9846. if (finsert || n < sizeof(void *)) {
  9847. struct soap_flist *fp = (struct soap_flist *) SOAP_MALLOC(soap, sizeof(struct soap_flist));
  9848. if (!fp) {
  9849. soap->error = SOAP_EOM;
  9850. return NULL;
  9851. }
  9852. if (fbase && fbase(t, ip->type) && !soap_type_punned(soap, ip)) {
  9853. ip->type = t;
  9854. ip->size = n;
  9855. }
  9856. if ((ip->type != t || ip->size != n) && (!fbase || !fbase(ip->type, t))) {
  9857. DBGLOG(TEST, SOAP_MESSAGE(fdebug,
  9858. "Forward type incompatibility id='%s' expect type=%d size=%lu level=%u got type=%d size=%lu\n",
  9859. href, ip->type, (unsigned long) ip->size, k, t, (unsigned long) n));
  9860. SOAP_FREE(soap, fp);
  9861. (void) soap_id_nullify(soap, href);
  9862. return NULL;
  9863. }
  9864. fp->next = ip->flist;
  9865. fp->type = tt;
  9866. fp->ptr = p;
  9867. fp->level = k;
  9868. fp->index = i;
  9869. fp->finsert = finsert;
  9870. ip->flist = fp;
  9871. DBGLOG(TEST, SOAP_MESSAGE(fdebug,
  9872. "Forwarding type=%d (target type=%d) size=%lu location=%p level=%u index=%lu href='%s'\n",
  9873. t, tt, (unsigned long) n, p, k, (unsigned long) i, href));
  9874. } else {
  9875. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Forwarding copying address %p for type=%d href='%s'\n", p, t, href));
  9876. *(void **) p = ip->copy;
  9877. ip->copy = p;
  9878. }
  9879. ip->shaky = soap_is_shaky(soap, p);
  9880. return p;
  9881. }
  9882. #endif
  9883. /******************************************************************************/
  9884. SOAP_FMAC1
  9885. void *
  9886. SOAP_FMAC2
  9887. soap_id_enter(struct soap *soap, const char *id, void *p, int t, size_t n, const char *type, const char *arrayType,
  9888. void *(*finstantiate)(struct soap *, int, const char *, const char *, size_t *), int (*fbase)(int, int)) {
  9889. #ifndef WITH_NOIDREF
  9890. struct soap_ilist *ip;
  9891. #endif
  9892. (void) id;
  9893. (void) fbase;
  9894. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Enter id='%s' type=%d location=%p size=%lu\n", id, t, p, (unsigned long) n));
  9895. soap->alloced = 0;
  9896. if (!p) {
  9897. if (finstantiate) {
  9898. p = finstantiate(soap, t, type, arrayType, &n); /* soap->alloced is set in soap_link() */
  9899. t = soap->alloced;
  9900. } else {
  9901. p = soap_malloc(soap, n);
  9902. soap->alloced = t;
  9903. }
  9904. }
  9905. #ifndef WITH_NOIDREF
  9906. if (!id || !*id)
  9907. #endif
  9908. return p;
  9909. #ifndef WITH_NOIDREF
  9910. ip = soap_lookup(soap, id); /* lookup pointer to hash table entry for string id */
  9911. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Lookup entry id='%s' for location=%p type=%d\n", id, p, t));
  9912. if (!ip) {
  9913. ip = soap_enter(soap, id, t, n); /* new hash table entry for string id */
  9914. if (!ip)
  9915. return NULL;
  9916. ip->ptr = p;
  9917. DBGLOG(TEST,
  9918. SOAP_MESSAGE(fdebug, "New entry id='%s' type=%d size=%lu location=%p\n", id, t, (unsigned long) n, p));
  9919. if (!soap->alloced)
  9920. ip->shaky = soap_is_shaky(soap, p);
  9921. } else if (ip->ptr) {
  9922. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Multiply defined id='%s'\n", id));
  9923. soap_strcpy(soap->id, sizeof(soap->id), id);
  9924. soap->error = SOAP_DUPLICATE_ID;
  9925. return NULL;
  9926. } else if ((ip->type != t && (!fbase || !fbase(t, ip->type) || soap_type_punned(soap, ip)))
  9927. || (ip->type == t && ip->size != n && soap_type_punned(soap, ip))) {
  9928. DBGLOG(TEST,
  9929. SOAP_MESSAGE(fdebug, "Enter type incompatibility id='%s' expect type=%d size=%lu got type=%d size=%lu\n",
  9930. id, ip->type, (unsigned long) ip->size, t, (unsigned long) n));
  9931. (void) soap_id_nullify(soap, id);
  9932. return NULL;
  9933. } else {
  9934. ip->type = t;
  9935. ip->size = n;
  9936. ip->ptr = p;
  9937. if (!soap->alloced)
  9938. ip->shaky = soap_is_shaky(soap, p);
  9939. if (soap->alloced || !ip->shaky) {
  9940. void **q; /* ptr will not change later, so resolve links now */
  9941. if (ip->spine)
  9942. ip->spine[0] = p;
  9943. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Traversing link chain to resolve id='%s' type=%d\n", ip->id, ip->type));
  9944. q = (void **) ip->link;
  9945. while (q) {
  9946. void *r = *q;
  9947. *q = p;
  9948. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "... link %p -> %p\n", (void *) q, p));
  9949. q = (void **) r;
  9950. }
  9951. ip->link = NULL;
  9952. }
  9953. }
  9954. return ip->ptr;
  9955. #endif
  9956. }
  9957. /******************************************************************************/
  9958. SOAP_FMAC1
  9959. void **
  9960. SOAP_FMAC2
  9961. soap_id_smart(struct soap *soap, const char *id, int t, size_t n) {
  9962. (void) soap;
  9963. (void) id;
  9964. (void) t;
  9965. (void) n;
  9966. #ifndef WITH_NOIDREF
  9967. if (id && *id) {
  9968. struct soap_ilist *ip = soap_lookup(soap, id); /* lookup pointer to hash table entry for string id */
  9969. if (!ip) {
  9970. ip = soap_enter(soap, id, t, n); /* new hash table entry for string id */
  9971. if (!ip)
  9972. return NULL;
  9973. }
  9974. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "New smart shared pointer entry id='%s' type=%d size=%lu smart=%p\n", id, t,
  9975. (unsigned long) n, ip->smart));
  9976. return &ip->smart;
  9977. }
  9978. #endif
  9979. return NULL;
  9980. }
  9981. /******************************************************************************/
  9982. #ifndef WITH_NOIDREF
  9983. static int
  9984. soap_type_punned(struct soap *soap, const struct soap_ilist *ip) {
  9985. const struct soap_flist *fp;
  9986. (void) soap;
  9987. if (ip->ptr || ip->copy)
  9988. return 1;
  9989. for (fp = ip->flist; fp; fp = fp->next)
  9990. if (fp->level == 0)
  9991. return 1;
  9992. return 0;
  9993. }
  9994. #endif
  9995. /******************************************************************************/
  9996. #ifndef WITH_NOIDREF
  9997. SOAP_FMAC1
  9998. int
  9999. SOAP_FMAC2
  10000. soap_id_nullify(struct soap *soap, const char *id) {
  10001. int i;
  10002. for (i = 0; i < SOAP_IDHASH; i++) {
  10003. struct soap_ilist *ip;
  10004. for (ip = soap->iht[i]; ip; ip = ip->next) {
  10005. void *p, *q;
  10006. for (p = ip->link; p; p = q) {
  10007. q = *(void **) p;
  10008. *(void **) p = NULL;
  10009. }
  10010. ip->link = NULL;
  10011. }
  10012. }
  10013. soap_strcpy(soap->id, sizeof(soap->id), id);
  10014. return soap->error = SOAP_HREF;
  10015. }
  10016. #endif
  10017. /******************************************************************************/
  10018. SOAP_FMAC1
  10019. int
  10020. SOAP_FMAC2
  10021. soap_end_send(struct soap *soap) {
  10022. #ifndef WITH_LEANER
  10023. int err;
  10024. err = soap_end_attachments(soap);
  10025. if (soap->dime.list) {
  10026. /* SOAP body referenced attachments must appear first */
  10027. soap->dime.last->next = soap->dime.first;
  10028. soap->dime.first = soap->dime.list->next;
  10029. soap->dime.list->next = NULL;
  10030. soap->dime.last = soap->dime.list;
  10031. }
  10032. if (!err)
  10033. err = soap_putdime(soap);
  10034. if (!err)
  10035. err = soap_putmime(soap);
  10036. soap->mime.list = NULL;
  10037. soap->mime.first = NULL;
  10038. soap->mime.last = NULL;
  10039. soap->dime.list = NULL;
  10040. soap->dime.first = NULL;
  10041. soap->dime.last = NULL;
  10042. if (err)
  10043. return err;
  10044. #endif
  10045. return soap_end_send_flush(soap);
  10046. }
  10047. /******************************************************************************/
  10048. SOAP_FMAC1
  10049. int
  10050. SOAP_FMAC2
  10051. soap_end_send_flush(struct soap *soap) {
  10052. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "End send mode=0x%x\n", soap->mode));
  10053. if ((soap->mode & SOAP_IO)) /* need to flush the remaining data in buffer */
  10054. {
  10055. if (soap_flush(soap))
  10056. #ifdef WITH_ZLIB
  10057. {
  10058. if ((soap->mode & SOAP_ENC_ZLIB) && soap->zlib_state == SOAP_ZLIB_DEFLATE)
  10059. {
  10060. soap->zlib_state = SOAP_ZLIB_NONE;
  10061. deflateEnd(soap->d_stream);
  10062. }
  10063. return soap->error;
  10064. }
  10065. #else
  10066. return soap->error;
  10067. #endif
  10068. #ifdef WITH_ZLIB
  10069. if ((soap->mode & SOAP_ENC_ZLIB) && soap->d_stream)
  10070. {
  10071. int r;
  10072. soap->d_stream->avail_in = 0;
  10073. do
  10074. {
  10075. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Deflating remainder\n"));
  10076. r = deflate(soap->d_stream, Z_FINISH);
  10077. if (soap->d_stream->avail_out != sizeof(soap->buf))
  10078. {
  10079. if (soap_flush_raw(soap, soap->z_buf, sizeof(soap->buf) - soap->d_stream->avail_out))
  10080. {
  10081. soap->zlib_state = SOAP_ZLIB_NONE;
  10082. deflateEnd(soap->d_stream);
  10083. return soap->error;
  10084. }
  10085. soap->d_stream->next_out = (Byte*)soap->z_buf;
  10086. soap->d_stream->avail_out = sizeof(soap->buf);
  10087. }
  10088. } while (r == Z_OK);
  10089. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Deflated total %lu->%lu bytes\n", soap->d_stream->total_in, soap->d_stream->total_out));
  10090. soap->z_ratio_out = (float)soap->d_stream->total_out / (float)soap->d_stream->total_in;
  10091. soap->mode &= ~SOAP_ENC_ZLIB;
  10092. soap->zlib_state = SOAP_ZLIB_NONE;
  10093. if (deflateEnd(soap->d_stream) != Z_OK || r != Z_STREAM_END)
  10094. {
  10095. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Unable to end deflate: %s\n", soap->d_stream->msg ? soap->d_stream->msg : SOAP_STR_EOS));
  10096. return soap->error = SOAP_ZLIB_ERROR;
  10097. }
  10098. #ifdef WITH_GZIP
  10099. if (soap->zlib_out != SOAP_ZLIB_DEFLATE)
  10100. {
  10101. soap->z_buf[0] = soap->z_crc & 0xFF;
  10102. soap->z_buf[1] = (soap->z_crc >> 8) & 0xFF;
  10103. soap->z_buf[2] = (soap->z_crc >> 16) & 0xFF;
  10104. soap->z_buf[3] = (soap->z_crc >> 24) & 0xFF;
  10105. soap->z_buf[4] = soap->d_stream->total_in & 0xFF;
  10106. soap->z_buf[5] = (soap->d_stream->total_in >> 8) & 0xFF;
  10107. soap->z_buf[6] = (soap->d_stream->total_in >> 16) & 0xFF;
  10108. soap->z_buf[7] = (soap->d_stream->total_in >> 24) & 0xFF;
  10109. if (soap_flush_raw(soap, soap->z_buf, 8))
  10110. return soap->error;
  10111. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "gzip crc32=%lu\n", (unsigned long)soap->z_crc));
  10112. }
  10113. #endif
  10114. }
  10115. #endif
  10116. if ((soap->mode & SOAP_IO) == SOAP_IO_STORE) {
  10117. #if !defined(__cplusplus) || defined(WITH_COMPAT)
  10118. if (soap->os)
  10119. {
  10120. char *b = (char*)soap_push_block(soap, NULL, 1);
  10121. if (b)
  10122. {
  10123. *b = '\0';
  10124. *soap->os = soap_save_block(soap, NULL, NULL, 0);
  10125. }
  10126. }
  10127. else
  10128. #endif
  10129. {
  10130. char *p;
  10131. #ifndef WITH_NOHTTP
  10132. if (!(soap->mode & SOAP_ENC_PLAIN)) {
  10133. soap->mode--;
  10134. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Sending buffered message of length %u\n",
  10135. (unsigned int) soap->blist->size));
  10136. if (soap->status >= SOAP_POST)
  10137. soap->error = soap->fpost(soap, soap->endpoint, soap->host, soap->port, soap->path,
  10138. soap->action, soap->blist->size);
  10139. else if (soap->status != SOAP_STOP)
  10140. soap->error = soap->fresponse(soap, soap->status, soap->blist->size);
  10141. if (soap->error || soap_flush(soap))
  10142. return soap->error;
  10143. soap->mode++;
  10144. }
  10145. #endif
  10146. for (p = soap_first_block(soap, NULL); p; p = soap_next_block(soap, NULL)) {
  10147. DBGMSG(SENT, p, soap_block_size(soap, NULL));
  10148. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Send %u bytes to socket=%d/fd=%d\n",
  10149. (unsigned int) soap_block_size(soap, NULL), (int) soap->socket,
  10150. soap->sendfd));
  10151. soap->error = soap->fsend(soap, p, soap_block_size(soap, NULL));
  10152. if (soap->error) {
  10153. soap_end_block(soap, NULL);
  10154. return soap->error;
  10155. }
  10156. }
  10157. soap_end_block(soap, NULL);
  10158. }
  10159. #ifndef WITH_LEANER
  10160. if (soap->fpreparefinalsend && (soap->error = soap->fpreparefinalsend(soap)) != SOAP_OK)
  10161. return soap->error;
  10162. #endif
  10163. if ((soap->omode & SOAP_IO) == SOAP_IO_STORE && (soap->imode & SOAP_IO) != SOAP_IO_STORE)
  10164. soap->omode = (soap->omode & ~SOAP_IO) | (soap->imode & SOAP_IO);
  10165. }
  10166. #ifndef WITH_LEANER
  10167. else if ((soap->mode & SOAP_IO) == SOAP_IO_CHUNK) {
  10168. DBGMSG(SENT, "\r\n0\r\n\r\n", 7);
  10169. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Send 7 bytes to socket=%d/fd=%d\n", (int) soap->socket, soap->sendfd));
  10170. soap->error = soap->fsend(soap, "\r\n0\r\n\r\n", 7);
  10171. if (soap->error)
  10172. return soap->error;
  10173. }
  10174. #endif
  10175. }
  10176. #ifdef WITH_TCPFIN
  10177. #if defined(WITH_OPENSSL) || defined(WITH_SYSTEMSSL)
  10178. if (!soap->ssl)
  10179. #endif
  10180. if (soap_valid_socket(soap->socket) && !soap->keep_alive && !(soap->omode & SOAP_IO_UDP))
  10181. soap->fshutdownsocket(soap, soap->socket, SOAP_SHUT_WR); /* Send TCP FIN */
  10182. #endif
  10183. #if defined(__cplusplus) && !defined(WITH_COMPAT)
  10184. if (soap->os)
  10185. soap->os->flush();
  10186. #endif
  10187. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "End of send phase\n"));
  10188. soap->omode &= ~SOAP_SEC_WSUID;
  10189. soap->count = 0;
  10190. soap->part = SOAP_END;
  10191. return SOAP_OK;
  10192. }
  10193. /******************************************************************************/
  10194. SOAP_FMAC1
  10195. int
  10196. SOAP_FMAC2
  10197. soap_end_recv(struct soap *soap) {
  10198. soap->part = SOAP_END;
  10199. #ifndef WITH_LEAN
  10200. soap->wsuid = NULL; /* reset before next send */
  10201. soap->c14nexclude = NULL; /* reset before next send */
  10202. soap->c14ninclude = NULL; /* reset before next send */
  10203. #endif
  10204. #ifndef WITH_LEANER
  10205. soap->ffilterrecv = NULL;
  10206. if ((soap->mode & SOAP_ENC_DIME) && soap_getdime(soap)) {
  10207. soap->dime.first = NULL;
  10208. soap->dime.last = NULL;
  10209. return soap->error;
  10210. }
  10211. soap->dime.list = soap->dime.first;
  10212. soap->dime.first = NULL;
  10213. soap->dime.last = NULL;
  10214. /* Check if MIME attachments and mime-post-check flag is set, if so call soap_resolve() and return */
  10215. if ((soap->mode & SOAP_ENC_MIME)) {
  10216. if ((soap->mode & SOAP_MIME_POSTCHECK)) {
  10217. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Post checking MIME attachments\n"));
  10218. if (!soap->keep_alive)
  10219. soap->keep_alive = -2; /* special case to keep alive */
  10220. #ifndef WITH_NOIDREF
  10221. soap_resolve(soap);
  10222. #endif
  10223. return SOAP_OK;
  10224. }
  10225. if (soap_getmime(soap))
  10226. return soap->error;
  10227. }
  10228. soap->mime.list = soap->mime.first;
  10229. soap->mime.first = NULL;
  10230. soap->mime.last = NULL;
  10231. soap->mime.boundary = NULL;
  10232. if (soap->xlist) {
  10233. struct soap_multipart *content;
  10234. for (content = soap->mime.list; content; content = content->next)
  10235. soap_resolve_attachment(soap, content);
  10236. }
  10237. #endif
  10238. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "End of receive message ok\n"));
  10239. #ifdef WITH_ZLIB
  10240. if ((soap->mode & SOAP_ENC_ZLIB) && soap->d_stream)
  10241. {
  10242. /* Make sure end of compressed content is reached */
  10243. while (soap->d_stream->next_out != Z_NULL)
  10244. if ((int)soap_get1(soap) == EOF)
  10245. break;
  10246. soap->mode &= ~SOAP_ENC_ZLIB;
  10247. (void)soap_memcpy((void*)soap->buf, sizeof(soap->buf), (const void*)soap->z_buf, sizeof(soap->buf));
  10248. soap->bufidx = (char*)soap->d_stream->next_in - soap->z_buf;
  10249. soap->buflen = soap->z_buflen;
  10250. soap->zlib_state = SOAP_ZLIB_NONE;
  10251. if (inflateEnd(soap->d_stream) != Z_OK)
  10252. return soap->error = SOAP_ZLIB_ERROR;
  10253. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Inflate end ok\n"));
  10254. #ifdef WITH_GZIP
  10255. if (soap->zlib_in == SOAP_ZLIB_GZIP)
  10256. {
  10257. soap_wchar c;
  10258. short i;
  10259. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Inflate gzip crc check\n"));
  10260. for (i = 0; i < 8; i++)
  10261. {
  10262. if ((int)(c = soap_get1(soap)) == EOF)
  10263. {
  10264. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Gzip error: unable to read crc value\n"));
  10265. return soap->error = SOAP_ZLIB_ERROR;
  10266. }
  10267. soap->z_buf[i] = (char)c;
  10268. }
  10269. 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)))
  10270. {
  10271. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Gzip inflate error: crc check failed, message corrupted? (crc32=%lu)\n", (unsigned long)soap->z_crc));
  10272. return soap->error = SOAP_ZLIB_ERROR;
  10273. }
  10274. 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)))
  10275. {
  10276. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Gzip inflate error: incorrect message length\n"));
  10277. return soap->error = SOAP_ZLIB_ERROR;
  10278. }
  10279. }
  10280. soap->zlib_in = SOAP_ZLIB_NONE;
  10281. #endif
  10282. }
  10283. #endif
  10284. if ((soap->mode & SOAP_IO) == SOAP_IO_CHUNK)
  10285. while ((int) soap->ahead != EOF && !soap_recv_raw(soap))
  10286. continue;
  10287. #ifndef WITH_NOIDREF
  10288. if (soap_resolve(soap))
  10289. return soap->error;
  10290. #endif
  10291. #ifndef WITH_LEANER
  10292. if (soap->xlist) {
  10293. if ((soap->mode & SOAP_ENC_MTOM))
  10294. return soap->error = SOAP_MIME_HREF;
  10295. return soap->error = SOAP_DIME_HREF;
  10296. }
  10297. #endif
  10298. soap_free_ns(soap);
  10299. #ifndef WITH_LEANER
  10300. if (soap->fpreparefinalrecv)
  10301. return soap->error = soap->fpreparefinalrecv(soap);
  10302. #endif
  10303. return SOAP_OK;
  10304. }
  10305. /******************************************************************************/
  10306. SOAP_FMAC1
  10307. void
  10308. SOAP_FMAC2
  10309. soap_free_temp(struct soap *soap) {
  10310. struct soap_attribute *tp, *tq;
  10311. struct Namespace *ns;
  10312. soap_free_ns(soap);
  10313. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Free any remaining temp blocks\n"));
  10314. while (soap->blist)
  10315. soap_end_block(soap, NULL);
  10316. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Free attribute storage\n"));
  10317. for (tp = soap->attributes; tp; tp = tq) {
  10318. tq = tp->next;
  10319. if (tp->value)
  10320. SOAP_FREE(soap, tp->value);
  10321. SOAP_FREE(soap, tp);
  10322. }
  10323. soap->attributes = NULL;
  10324. #ifdef WITH_FAST
  10325. if (soap->labbuf)
  10326. SOAP_FREE(soap, soap->labbuf);
  10327. soap->labbuf = NULL;
  10328. soap->lablen = 0;
  10329. soap->labidx = 0;
  10330. #endif
  10331. ns = soap->local_namespaces;
  10332. if (ns) {
  10333. for (; ns->id; ns++) {
  10334. if (ns->out) {
  10335. SOAP_FREE(soap, ns->out);
  10336. ns->out = NULL;
  10337. }
  10338. }
  10339. SOAP_FREE(soap, soap->local_namespaces);
  10340. soap->local_namespaces = NULL;
  10341. }
  10342. #ifndef WITH_LEANER
  10343. while (soap->xlist) {
  10344. struct soap_xlist *xp = soap->xlist->next;
  10345. SOAP_FREE(soap, soap->xlist);
  10346. soap->xlist = xp;
  10347. }
  10348. #endif
  10349. #ifndef WITH_NOIDREF
  10350. soap_free_iht(soap);
  10351. #endif
  10352. soap_free_pht(soap);
  10353. }
  10354. /******************************************************************************/
  10355. static void
  10356. soap_free_ns(struct soap *soap) {
  10357. struct soap_nlist *np, *nq;
  10358. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Free namespace stack\n"));
  10359. for (np = soap->nlist; np; np = nq) {
  10360. nq = np->next;
  10361. SOAP_FREE(soap, np);
  10362. }
  10363. soap->nlist = NULL;
  10364. }
  10365. /******************************************************************************/
  10366. #ifdef SOAP_DEBUG
  10367. static void
  10368. soap_init_logs(struct soap *soap)
  10369. {
  10370. int i;
  10371. for (i = 0; i < SOAP_MAXLOGS; i++)
  10372. {
  10373. soap->logfile[i] = NULL;
  10374. soap->fdebug[i] = NULL;
  10375. }
  10376. }
  10377. #endif
  10378. /******************************************************************************/
  10379. #ifdef SOAP_DEBUG
  10380. SOAP_FMAC1
  10381. void
  10382. SOAP_FMAC2
  10383. soap_open_logfile(struct soap *soap, int i)
  10384. {
  10385. if (soap->logfile[i])
  10386. soap->fdebug[i] = fopen(soap->logfile[i], i < 2 ? "ab" : "a");
  10387. }
  10388. #endif
  10389. /******************************************************************************/
  10390. #ifdef SOAP_DEBUG
  10391. static void
  10392. soap_close_logfile(struct soap *soap, int i)
  10393. {
  10394. if (soap->fdebug[i])
  10395. {
  10396. fclose(soap->fdebug[i]);
  10397. soap->fdebug[i] = NULL;
  10398. }
  10399. }
  10400. #endif
  10401. /******************************************************************************/
  10402. #ifdef SOAP_DEBUG
  10403. SOAP_FMAC1
  10404. void
  10405. SOAP_FMAC2
  10406. soap_close_logfiles(struct soap *soap)
  10407. {
  10408. int i;
  10409. for (i = 0; i < SOAP_MAXLOGS; i++)
  10410. soap_close_logfile(soap, i);
  10411. }
  10412. #endif
  10413. /******************************************************************************/
  10414. #ifdef SOAP_DEBUG
  10415. static void
  10416. soap_set_logfile(struct soap *soap, int i, const char *logfile)
  10417. {
  10418. const char *s;
  10419. char *t = NULL;
  10420. soap_close_logfile(soap, i);
  10421. s = soap->logfile[i];
  10422. if (s)
  10423. SOAP_FREE_UNMANAGED(s);
  10424. if (logfile)
  10425. {
  10426. size_t l = strlen(logfile) + 1;
  10427. t = (char*)SOAP_MALLOC_UNMANAGED(l);
  10428. if (t)
  10429. (void)soap_memcpy((void*)t, l, (const void*)logfile, l);
  10430. }
  10431. soap->logfile[i] = t;
  10432. }
  10433. #endif
  10434. /******************************************************************************/
  10435. SOAP_FMAC1
  10436. void
  10437. SOAP_FMAC2
  10438. soap_set_recv_logfile(struct soap *soap, const char *logfile) {
  10439. (void) soap;
  10440. (void) logfile;
  10441. #ifdef SOAP_DEBUG
  10442. soap_set_logfile(soap, SOAP_INDEX_RECV, logfile);
  10443. #endif
  10444. }
  10445. /******************************************************************************/
  10446. SOAP_FMAC1
  10447. void
  10448. SOAP_FMAC2
  10449. soap_set_sent_logfile(struct soap *soap, const char *logfile) {
  10450. (void) soap;
  10451. (void) logfile;
  10452. #ifdef SOAP_DEBUG
  10453. soap_set_logfile(soap, SOAP_INDEX_SENT, logfile);
  10454. #endif
  10455. }
  10456. /******************************************************************************/
  10457. SOAP_FMAC1
  10458. void
  10459. SOAP_FMAC2
  10460. soap_set_test_logfile(struct soap *soap, const char *logfile) {
  10461. (void) soap;
  10462. (void) logfile;
  10463. #ifdef SOAP_DEBUG
  10464. soap_set_logfile(soap, SOAP_INDEX_TEST, logfile);
  10465. #endif
  10466. }
  10467. /******************************************************************************/
  10468. SOAP_FMAC1
  10469. struct soap *
  10470. SOAP_FMAC2
  10471. soap_copy(const struct soap *soap) {
  10472. struct soap *copy = soap_versioning(soap_new)(SOAP_IO_DEFAULT, SOAP_IO_DEFAULT);
  10473. soap_done(copy);
  10474. if (soap_copy_context(copy, soap) != NULL)
  10475. return copy;
  10476. soap_free(copy);
  10477. return NULL;
  10478. }
  10479. /******************************************************************************/
  10480. SOAP_FMAC1
  10481. struct soap *
  10482. SOAP_FMAC2
  10483. soap_copy_context(struct soap *copy, const struct soap *soap) {
  10484. if (copy == soap)
  10485. return copy;
  10486. if (soap_check_state(soap))
  10487. return NULL;
  10488. if (copy) {
  10489. struct soap_plugin *p = NULL;
  10490. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Copying context\n"));
  10491. (void) soap_memcpy((void *) copy, sizeof(struct soap), (const void *) soap, sizeof(struct soap));
  10492. copy->state = SOAP_COPY;
  10493. copy->error = SOAP_OK;
  10494. copy->bearer = NULL;
  10495. copy->userid = NULL;
  10496. copy->passwd = NULL;
  10497. #ifdef WITH_NTLM
  10498. copy->ntlm_challenge = NULL;
  10499. #endif
  10500. copy->nlist = NULL;
  10501. copy->blist = NULL;
  10502. copy->clist = NULL;
  10503. copy->alist = NULL;
  10504. copy->attributes = NULL;
  10505. copy->labbuf = NULL;
  10506. copy->lablen = 0;
  10507. copy->labidx = 0;
  10508. #ifdef SOAP_MEM_DEBUG
  10509. soap_init_mht(copy);
  10510. #endif
  10511. #ifdef SOAP_DEBUG
  10512. soap_init_logs(copy);
  10513. soap_set_test_logfile(copy, soap->logfile[SOAP_INDEX_TEST]);
  10514. soap_set_sent_logfile(copy, soap->logfile[SOAP_INDEX_SENT]);
  10515. soap_set_recv_logfile(copy, soap->logfile[SOAP_INDEX_RECV]);
  10516. #endif
  10517. copy->namespaces = soap->local_namespaces;
  10518. copy->local_namespaces = NULL;
  10519. soap_set_local_namespaces(copy); /* copy content of soap->local_namespaces */
  10520. copy->namespaces = soap->namespaces; /* point to shared read-only namespaces table */
  10521. copy->c_locale = NULL;
  10522. #ifdef WITH_OPENSSL
  10523. copy->bio = NULL;
  10524. copy->ssl = NULL;
  10525. copy->session = NULL;
  10526. copy->session_host[0] = '\0';
  10527. copy->session_port = 443;
  10528. #endif
  10529. #ifdef WITH_GNUTLS
  10530. copy->session = NULL;
  10531. #endif
  10532. #ifdef WITH_ZLIB
  10533. copy->d_stream = NULL;
  10534. copy->z_buf = NULL;
  10535. #endif
  10536. #ifndef WITH_NOIDREF
  10537. soap_init_iht(copy);
  10538. #endif
  10539. soap_init_pht(copy);
  10540. copy->header = NULL;
  10541. copy->fault = NULL;
  10542. copy->action = NULL;
  10543. #ifndef WITH_LEAN
  10544. #ifdef WITH_COOKIES
  10545. copy->cookies = soap_copy_cookies(copy, soap);
  10546. #else
  10547. copy->cookies = NULL;
  10548. #endif
  10549. #endif
  10550. copy->plugins = NULL;
  10551. for (p = soap->plugins; p; p = p->next) {
  10552. struct soap_plugin *q = (struct soap_plugin *) SOAP_MALLOC(copy, sizeof(struct soap_plugin));
  10553. if (!q) {
  10554. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Could not allocate plugin '%s'\n", p->id));
  10555. soap_end(copy);
  10556. soap_done(copy);
  10557. return NULL;
  10558. }
  10559. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Copying plugin '%s'\n", p->id));
  10560. *q = *p;
  10561. if (p->fcopy && (copy->error = p->fcopy(copy, q, p)) != SOAP_OK) {
  10562. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Could not copy plugin '%s' error = %d\n", p->id, copy->error));
  10563. SOAP_FREE(copy, q);
  10564. soap_end(copy);
  10565. soap_done(copy);
  10566. return NULL;
  10567. }
  10568. q->next = copy->plugins;
  10569. copy->plugins = q;
  10570. }
  10571. }
  10572. #ifdef WITH_SELF_PIPE
  10573. pipe(copy->pipe_fd);
  10574. SOAP_SOCKNONBLOCK(copy->pipe_fd[0])
  10575. SOAP_SOCKNONBLOCK(copy->pipe_fd[1])
  10576. #endif
  10577. return copy;
  10578. }
  10579. /******************************************************************************/
  10580. SOAP_FMAC1
  10581. void
  10582. SOAP_FMAC2
  10583. soap_copy_stream(struct soap *copy, struct soap *soap) {
  10584. struct soap_attribute *tp = NULL, *tq;
  10585. if (copy == soap)
  10586. return;
  10587. copy->header = soap->header;
  10588. copy->mode = soap->mode;
  10589. copy->imode = soap->imode;
  10590. copy->omode = soap->omode;
  10591. copy->socket = soap->socket;
  10592. copy->sendsk = soap->sendsk;
  10593. copy->recvsk = soap->recvsk;
  10594. copy->transfer_timeout = soap->transfer_timeout;
  10595. copy->recv_maxlength = soap->recv_maxlength;
  10596. copy->recv_timeout = soap->recv_timeout;
  10597. copy->send_timeout = soap->send_timeout;
  10598. copy->connect_timeout = soap->connect_timeout;
  10599. copy->accept_timeout = soap->accept_timeout;
  10600. copy->socket_flags = soap->socket_flags;
  10601. copy->connect_flags = soap->connect_flags;
  10602. copy->bind_flags = soap->bind_flags;
  10603. copy->bind_inet6 = soap->bind_inet6;
  10604. copy->bind_v6only = soap->bind_v6only;
  10605. copy->accept_flags = soap->accept_flags;
  10606. copy->sndbuf = soap->sndbuf;
  10607. copy->rcvbuf = soap->rcvbuf;
  10608. copy->linger_time = soap->linger_time;
  10609. copy->maxlevel = soap->maxlevel;
  10610. copy->maxlength = soap->maxlength;
  10611. copy->maxoccurs = soap->maxoccurs;
  10612. copy->os = soap->os;
  10613. copy->is = soap->is;
  10614. copy->sendfd = soap->sendfd;
  10615. copy->recvfd = soap->recvfd;
  10616. copy->bufidx = soap->bufidx;
  10617. copy->buflen = soap->buflen;
  10618. copy->ahead = soap->ahead;
  10619. copy->cdata = soap->cdata;
  10620. copy->chunksize = soap->chunksize;
  10621. copy->chunkbuflen = soap->chunkbuflen;
  10622. copy->keep_alive = soap->keep_alive;
  10623. copy->tcp_keep_alive = soap->tcp_keep_alive;
  10624. copy->tcp_keep_idle = soap->tcp_keep_idle;
  10625. copy->tcp_keep_intvl = soap->tcp_keep_intvl;
  10626. copy->tcp_keep_cnt = soap->tcp_keep_cnt;
  10627. copy->max_keep_alive = soap->max_keep_alive;
  10628. #ifndef WITH_NOIO
  10629. copy->peer = soap->peer;
  10630. copy->peerlen = soap->peerlen;
  10631. copy->ip = soap->ip;
  10632. copy->ip6[0] = soap->ip6[0];
  10633. copy->ip6[1] = soap->ip6[1];
  10634. copy->ip6[2] = soap->ip6[2];
  10635. copy->ip6[3] = soap->ip6[3];
  10636. copy->port = soap->port;
  10637. (void) soap_memcpy((void *) copy->host, sizeof(copy->host), (const void *) soap->host, sizeof(soap->host));
  10638. (void) soap_memcpy((void *) copy->endpoint, sizeof(copy->endpoint), (const void *) soap->endpoint,
  10639. sizeof(soap->endpoint));
  10640. #endif
  10641. #ifdef WITH_OPENSSL
  10642. copy->bio = soap->bio;
  10643. copy->ctx = soap->ctx;
  10644. copy->ssl = soap->ssl;
  10645. #endif
  10646. #ifdef WITH_GNUTLS
  10647. copy->session = soap->session;
  10648. #endif
  10649. #ifdef WITH_SYSTEMSSL
  10650. copy->ctx = soap->ctx;
  10651. copy->ssl = soap->ssl;
  10652. #endif
  10653. #ifdef WITH_ZLIB
  10654. copy->zlib_state = soap->zlib_state;
  10655. copy->zlib_in = soap->zlib_in;
  10656. copy->zlib_out = soap->zlib_out;
  10657. if (soap->d_stream && soap->zlib_state != SOAP_ZLIB_NONE)
  10658. {
  10659. if (!copy->d_stream)
  10660. copy->d_stream = (z_stream*)SOAP_MALLOC(copy, sizeof(z_stream));
  10661. if (copy->d_stream)
  10662. (void)soap_memcpy((void*)copy->d_stream, sizeof(z_stream), (const void*)soap->d_stream, sizeof(z_stream));
  10663. }
  10664. copy->z_crc = soap->z_crc;
  10665. copy->z_ratio_in = soap->z_ratio_in;
  10666. copy->z_ratio_out = soap->z_ratio_out;
  10667. copy->z_buf = NULL;
  10668. copy->z_buflen = soap->z_buflen;
  10669. copy->z_level = soap->z_level;
  10670. if (soap->z_buf && soap->zlib_state != SOAP_ZLIB_NONE)
  10671. {
  10672. copy->z_buf = (char*)SOAP_MALLOC(copy, sizeof(soap->buf));
  10673. if (copy->z_buf)
  10674. (void)soap_memcpy((void*)copy->z_buf, sizeof(soap->buf), (const void*)soap->z_buf, sizeof(soap->buf));
  10675. }
  10676. copy->z_dict = soap->z_dict;
  10677. copy->z_dict_len = soap->z_dict_len;
  10678. #endif
  10679. (void) soap_memcpy((void *) copy->buf, sizeof(copy->buf), (const void *) soap->buf, sizeof(soap->buf));
  10680. /* copy XML parser state */
  10681. soap_free_ns(copy);
  10682. soap_set_local_namespaces(copy);
  10683. copy->version = soap->version;
  10684. if (soap->nlist && soap->local_namespaces) {
  10685. struct soap_nlist *np = NULL, *nq;
  10686. /* copy reversed nlist */
  10687. for (nq = soap->nlist; nq; nq = nq->next) {
  10688. struct soap_nlist *nr = np;
  10689. size_t n = sizeof(struct soap_nlist) + strlen(nq->id);
  10690. np = (struct soap_nlist *) SOAP_MALLOC(copy, n);
  10691. if (!np) {
  10692. np = nr;
  10693. break;
  10694. }
  10695. (void) soap_memcpy((void *) np, n, (const void *) nq, n);
  10696. np->next = nr;
  10697. }
  10698. while (np) {
  10699. const char *s = np->ns;
  10700. copy->level = np->level; /* preserve element nesting level */
  10701. if (!s && np->index >= 0) {
  10702. s = soap->local_namespaces[np->index].out;
  10703. if (!s)
  10704. s = soap->local_namespaces[np->index].ns;
  10705. }
  10706. if (s)
  10707. (void) soap_push_namespace(copy, np->id, s);
  10708. nq = np;
  10709. np = np->next;
  10710. SOAP_FREE(copy, nq);
  10711. }
  10712. }
  10713. (void) soap_memcpy((void *) copy->tag, sizeof(copy->tag), (const void *) soap->tag, sizeof(soap->tag));
  10714. (void) soap_memcpy((void *) copy->id, sizeof(copy->id), (const void *) soap->id, sizeof(soap->id));
  10715. (void) soap_memcpy((void *) copy->href, sizeof(copy->href), (const void *) soap->href, sizeof(soap->href));
  10716. (void) soap_memcpy((void *) copy->type, sizeof(copy->type), (const void *) soap->type, sizeof(soap->type));
  10717. copy->other = soap->other;
  10718. copy->root = soap->root;
  10719. copy->null = soap->null;
  10720. copy->body = soap->body;
  10721. copy->part = soap->part;
  10722. copy->mustUnderstand = soap->mustUnderstand;
  10723. copy->level = soap->level;
  10724. copy->peeked = soap->peeked;
  10725. /* copy attributes */
  10726. for (tq = soap->attributes; tq; tq = tq->next) {
  10727. struct soap_attribute *tr = tp;
  10728. size_t n = sizeof(struct soap_attribute) + strlen(tq->name);
  10729. tp = (struct soap_attribute *) SOAP_MALLOC(copy, n);
  10730. (void) soap_memcpy((void *) tp, n, (const void *) tq, n);
  10731. if (tp->size) {
  10732. tp->value = (char *) SOAP_MALLOC(copy, tp->size);
  10733. if (tp->value)
  10734. (void) soap_memcpy((void *) tp->value, tp->size, (const void *) tq->value, tp->size);
  10735. }
  10736. tp->ns = NULL;
  10737. tp->next = tr;
  10738. }
  10739. copy->attributes = tp;
  10740. }
  10741. /******************************************************************************/
  10742. SOAP_FMAC1
  10743. void
  10744. SOAP_FMAC2
  10745. soap_free_stream(struct soap *soap) {
  10746. soap->socket = SOAP_INVALID_SOCKET;
  10747. soap->sendsk = SOAP_INVALID_SOCKET;
  10748. soap->recvsk = SOAP_INVALID_SOCKET;
  10749. #ifdef WITH_OPENSSL
  10750. soap->bio = NULL;
  10751. soap->ctx = NULL;
  10752. soap->ssl = NULL;
  10753. #endif
  10754. #ifdef WITH_GNUTLS
  10755. soap->xcred = NULL;
  10756. soap->acred = NULL;
  10757. soap->cache = NULL;
  10758. soap->session = NULL;
  10759. soap->dh_params = NULL;
  10760. soap->rsa_params = NULL;
  10761. #endif
  10762. #ifdef WITH_SYSTEMSSL
  10763. soap->ctx = (gsk_handle)NULL;
  10764. soap->ssl = (gsk_handle)NULL;
  10765. #endif
  10766. #ifdef WITH_ZLIB
  10767. if (soap->z_buf)
  10768. SOAP_FREE(soap, soap->z_buf);
  10769. soap->z_buf = NULL;
  10770. #endif
  10771. }
  10772. /******************************************************************************/
  10773. SOAP_FMAC1
  10774. void
  10775. SOAP_FMAC2
  10776. soap_initialize(struct soap *soap) {
  10777. soap_versioning(soap_init)(soap, SOAP_IO_DEFAULT, SOAP_IO_DEFAULT);
  10778. }
  10779. /******************************************************************************/
  10780. SOAP_FMAC1
  10781. void
  10782. SOAP_FMAC2
  10783. soap_versioning(soap_init)(struct soap *soap, soap_mode imode, soap_mode omode) {
  10784. size_t i;
  10785. soap->state = SOAP_INIT;
  10786. #ifdef SOAP_MEM_DEBUG
  10787. soap_init_mht(soap);
  10788. #endif
  10789. #ifdef SOAP_DEBUG
  10790. soap_init_logs(soap);
  10791. #endif
  10792. #ifdef TANDEM_NONSTOP
  10793. soap_set_test_logfile(soap, "TESTLOG");
  10794. soap_set_sent_logfile(soap, "SENTLOG");
  10795. soap_set_recv_logfile(soap, "RECVLOG");
  10796. #else
  10797. soap_set_test_logfile(soap, "TEST.log");
  10798. soap_set_sent_logfile(soap, "SENT.log");
  10799. soap_set_recv_logfile(soap, "RECV.log");
  10800. #endif
  10801. #ifdef WITH_SELF_PIPE
  10802. pipe(soap->pipe_fd);
  10803. SOAP_SOCKNONBLOCK(soap->pipe_fd[0])
  10804. SOAP_SOCKNONBLOCK(soap->pipe_fd[1])
  10805. #endif
  10806. soap->version = 0;
  10807. soap->imode = imode;
  10808. soap->omode = omode;
  10809. soap->mode = imode;
  10810. soap->plugins = NULL;
  10811. soap->user = NULL;
  10812. for (i = 0; i < sizeof(soap->data) / sizeof(*soap->data); i++)
  10813. soap->data[i] = NULL;
  10814. soap->bearer = NULL;
  10815. soap->userid = NULL;
  10816. soap->passwd = NULL;
  10817. soap->authrealm = NULL;
  10818. #ifdef WITH_NTLM
  10819. soap->ntlm_challenge = NULL;
  10820. #endif
  10821. #ifndef WITH_NOHTTP
  10822. soap->fpost = http_post;
  10823. soap->fget = http_get;
  10824. soap->fput = http_put;
  10825. soap->fpatch = http_patch;
  10826. soap->fdel = http_del;
  10827. soap->fopt = http_200;
  10828. soap->fhead = http_200;
  10829. soap->fform = NULL;
  10830. soap->fposthdr = http_post_header;
  10831. soap->fresponse = http_response;
  10832. soap->fparse = http_parse;
  10833. soap->fparsehdr = http_parse_header;
  10834. #endif
  10835. soap->fheader = NULL;
  10836. soap->fconnect = NULL;
  10837. soap->fdisconnect = NULL;
  10838. #ifndef WITH_NOIO
  10839. soap->ipv6_multicast_if = 0; /* in_addr_t value */
  10840. soap->ipv4_multicast_if = NULL; /* points to struct in_addr or in_addr_t */
  10841. soap->ipv4_multicast_ttl = 0; /* 0: use default */
  10842. soap->client_port = -1; /* client port to bind, -1 for none */
  10843. soap->client_interface = NULL; /* client interface address, NULL for none */
  10844. #ifndef WITH_IPV6
  10845. soap->fresolve = tcp_gethost;
  10846. #else
  10847. soap->fresolve = NULL;
  10848. #endif
  10849. soap->faccept = tcp_accept;
  10850. soap->fopen = tcp_connect;
  10851. soap->fclose = tcp_disconnect;
  10852. soap->fclosesocket = tcp_closesocket;
  10853. soap->fshutdownsocket = tcp_shutdownsocket;
  10854. soap->fsend = fsend;
  10855. soap->frecv = frecv;
  10856. soap->fpoll = soap_poll;
  10857. #else
  10858. soap->fopen = NULL;
  10859. soap->fclose = NULL;
  10860. soap->fpoll = NULL;
  10861. #endif
  10862. soap->fseterror = NULL;
  10863. soap->fignore = NULL;
  10864. soap->fserveloop = NULL;
  10865. soap->fplugin = fplugin;
  10866. #ifndef WITH_LEANER
  10867. soap->fsvalidate = NULL;
  10868. soap->fwvalidate = NULL;
  10869. soap->feltbegin = NULL;
  10870. soap->feltendin = NULL;
  10871. soap->feltbegout = NULL;
  10872. soap->feltendout = NULL;
  10873. soap->fprepareinitsend = NULL;
  10874. soap->fprepareinitrecv = NULL;
  10875. soap->fpreparesend = NULL;
  10876. soap->fpreparerecv = NULL;
  10877. soap->fpreparefinalsend = NULL;
  10878. soap->fpreparefinalrecv = NULL;
  10879. soap->ffiltersend = NULL;
  10880. soap->ffilterrecv = NULL;
  10881. soap->fdimereadopen = NULL;
  10882. soap->fdimewriteopen = NULL;
  10883. soap->fdimereadclose = NULL;
  10884. soap->fdimewriteclose = NULL;
  10885. soap->fdimeread = NULL;
  10886. soap->fdimewrite = NULL;
  10887. soap->fmimereadopen = NULL;
  10888. soap->fmimewriteopen = NULL;
  10889. soap->fmimereadclose = NULL;
  10890. soap->fmimewriteclose = NULL;
  10891. soap->fmimeread = NULL;
  10892. soap->fmimewrite = NULL;
  10893. #endif
  10894. soap->float_format = "%.9G"; /* Alternative: use "%G" */
  10895. soap->double_format = "%.17lG"; /* Alternative: use "%lG" */
  10896. soap->long_double_format = NULL; /* Defined in custom serializer custom/long_double.c */
  10897. soap->dime_id_format = "cid:id%d"; /* default DIME id format for int id index */
  10898. soap->recv_maxlength = 0x7FFFFFFF; /* default max length of messages received (2GB) */
  10899. soap->recv_timeout = 0;
  10900. soap->send_timeout = 0;
  10901. soap->transfer_timeout = 0;
  10902. soap->connect_timeout = 0;
  10903. soap->accept_timeout = 0;
  10904. soap->socket_flags = 0;
  10905. soap->connect_flags = 0;
  10906. soap->bind_flags = 0;
  10907. #ifdef WITH_IPV6_V6ONLY
  10908. soap->bind_inet6 = 1;
  10909. soap->bind_v6only = 1;
  10910. #else
  10911. soap->bind_inet6 = 0;
  10912. soap->bind_v6only = 0;
  10913. #endif
  10914. soap->accept_flags = 0;
  10915. #ifdef WIN32
  10916. soap->sndbuf = SOAP_BUFLEN + 1; /* this size speeds up windows xfer */
  10917. soap->rcvbuf = SOAP_BUFLEN + 1;
  10918. #else
  10919. soap->sndbuf = SOAP_BUFLEN;
  10920. soap->rcvbuf = SOAP_BUFLEN;
  10921. #endif
  10922. soap->linger_time = 0;
  10923. soap->maxlevel = SOAP_MAXLEVEL;
  10924. soap->maxlength = SOAP_MAXLENGTH;
  10925. soap->maxoccurs = SOAP_MAXOCCURS;
  10926. soap->http_version = "1.1";
  10927. soap->proxy_http_version = "1.0";
  10928. soap->http_content = NULL;
  10929. soap->http_extra_header = NULL;
  10930. soap->actor = NULL;
  10931. soap->lang = "en";
  10932. soap->keep_alive = 0;
  10933. soap->tcp_keep_alive = 0;
  10934. soap->tcp_keep_idle = 0;
  10935. soap->tcp_keep_intvl = 0;
  10936. soap->tcp_keep_cnt = 0;
  10937. soap->max_keep_alive = SOAP_MAXKEEPALIVE;
  10938. soap->ip = 0;
  10939. soap->ip6[0] = 0;
  10940. soap->ip6[1] = 0;
  10941. soap->ip6[2] = 0;
  10942. soap->ip6[3] = 0;
  10943. soap->labbuf = NULL;
  10944. soap->lablen = 0;
  10945. soap->labidx = 0;
  10946. soap->encodingStyle = NULL;
  10947. #ifndef WITH_NONAMESPACES
  10948. soap->namespaces = namespaces;
  10949. #else
  10950. soap->namespaces = NULL;
  10951. #endif
  10952. soap->local_namespaces = NULL;
  10953. soap->nlist = NULL;
  10954. soap->blist = NULL;
  10955. soap->clist = NULL;
  10956. soap->alist = NULL;
  10957. soap->shaky = 0;
  10958. soap->attributes = NULL;
  10959. soap->header = NULL;
  10960. soap->fault = NULL;
  10961. soap->master = SOAP_INVALID_SOCKET;
  10962. soap->socket = SOAP_INVALID_SOCKET;
  10963. soap->sendsk = SOAP_INVALID_SOCKET;
  10964. soap->recvsk = SOAP_INVALID_SOCKET;
  10965. soap->os = NULL;
  10966. soap->is = NULL;
  10967. #ifndef WITH_LEANER
  10968. soap->dom = NULL;
  10969. soap->dime.list = NULL;
  10970. soap->dime.first = NULL;
  10971. soap->dime.last = NULL;
  10972. soap->mime.list = NULL;
  10973. soap->mime.first = NULL;
  10974. soap->mime.last = NULL;
  10975. soap->mime.boundary = NULL;
  10976. soap->mime.start = NULL;
  10977. soap->xlist = NULL;
  10978. #endif
  10979. #ifndef UNDER_CE
  10980. soap->recvfd = 0;
  10981. soap->sendfd = 1;
  10982. #else
  10983. soap->recvfd = stdin;
  10984. soap->sendfd = stdout;
  10985. #endif
  10986. soap->tag[0] = '\0';
  10987. soap->id[0] = '\0';
  10988. soap->href[0] = '\0';
  10989. soap->type[0] = '\0';
  10990. soap->arrayType[0] = '\0';
  10991. soap->arraySize[0] = '\0';
  10992. soap->arrayOffset[0] = '\0';
  10993. soap->endpoint[0] = '\0';
  10994. soap->host[0] = '\0';
  10995. soap->path[0] = '\0';
  10996. soap->port = 0;
  10997. soap->override_host = NULL;
  10998. soap->override_port = 0;
  10999. soap->action = NULL;
  11000. soap->proxy_host = NULL;
  11001. soap->proxy_port = 8080;
  11002. soap->proxy_userid = NULL;
  11003. soap->proxy_passwd = NULL;
  11004. soap->proxy_from = NULL;
  11005. soap->origin = NULL;
  11006. soap->cors_origin = NULL;
  11007. soap->cors_allow = "*";
  11008. soap->cors_method = NULL;
  11009. soap->cors_header = NULL;
  11010. soap->cors_methods = NULL;
  11011. soap->cors_headers = NULL;
  11012. soap->x_frame_options = "SAMEORIGIN";
  11013. soap->prolog = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
  11014. soap->zlib_state = SOAP_ZLIB_NONE;
  11015. soap->zlib_in = SOAP_ZLIB_NONE;
  11016. soap->zlib_out = SOAP_ZLIB_NONE;
  11017. soap->d_stream = NULL;
  11018. soap->z_buf = NULL;
  11019. soap->z_level = 6;
  11020. soap->z_dict = NULL;
  11021. soap->z_dict_len = 0;
  11022. #ifndef WITH_LEAN
  11023. soap->wsuid = NULL;
  11024. soap->c14nexclude = NULL;
  11025. soap->c14ninclude = NULL;
  11026. soap->cookies = NULL;
  11027. soap->cookie_domain = NULL;
  11028. soap->cookie_path = NULL;
  11029. soap->cookie_max = 32;
  11030. #endif
  11031. #ifdef WMW_RPM_IO
  11032. soap->rpmreqid = NULL;
  11033. #endif
  11034. #ifndef WITH_NOIDREF
  11035. soap_init_iht(soap);
  11036. #endif
  11037. soap_init_pht(soap);
  11038. #ifdef WITH_OPENSSL
  11039. if (!soap_ssl_init_done)
  11040. soap_ssl_init();
  11041. soap->fsslauth = ssl_auth_init;
  11042. soap->fsslverify = NULL;
  11043. soap->bio = NULL;
  11044. soap->ssl = NULL;
  11045. soap->ctx = NULL;
  11046. soap->session = NULL;
  11047. soap->session_host[0] = '\0';
  11048. soap->session_port = 443;
  11049. soap->ssl_flags = SOAP_SSL_DEFAULT;
  11050. soap->keyfile = NULL;
  11051. soap->keyid = NULL;
  11052. soap->password = NULL;
  11053. soap->cafile = NULL;
  11054. soap->capath = NULL;
  11055. soap->crlfile = NULL;
  11056. soap->dhfile = NULL;
  11057. soap->randfile = NULL;
  11058. #endif
  11059. #ifdef WITH_GNUTLS
  11060. if (!soap_ssl_init_done)
  11061. soap_ssl_init();
  11062. soap->fsslauth = ssl_auth_init;
  11063. soap->fsslverify = NULL;
  11064. soap->xcred = NULL;
  11065. soap->acred = NULL;
  11066. soap->cache = NULL;
  11067. soap->session = NULL;
  11068. soap->ssl_flags = SOAP_SSL_DEFAULT;
  11069. soap->keyfile = NULL;
  11070. soap->keyid = NULL;
  11071. soap->password = NULL;
  11072. soap->cafile = NULL;
  11073. soap->capath = NULL;
  11074. soap->crlfile = NULL;
  11075. soap->dh_params = NULL;
  11076. soap->rsa_params = NULL;
  11077. #endif
  11078. #ifdef WITH_SYSTEMSSL
  11079. soap->fsslauth = ssl_auth_init;
  11080. soap->fsslverify = NULL;
  11081. soap->bio = NULL;
  11082. soap->ssl = (gsk_handle)NULL;
  11083. soap->ctx = (gsk_handle)NULL;
  11084. soap->session = NULL;
  11085. soap->ssl_flags = SOAP_SSL_DEFAULT;
  11086. soap->keyfile = NULL;
  11087. soap->keyid = NULL;
  11088. soap->password = NULL;
  11089. soap->cafile = NULL;
  11090. soap->capath = NULL;
  11091. soap->crlfile = NULL;
  11092. soap->dhfile = NULL;
  11093. soap->randfile = NULL;
  11094. #endif
  11095. soap->c_locale = NULL;
  11096. soap->buflen = 0;
  11097. soap->bufidx = 0;
  11098. #ifndef WITH_LEANER
  11099. soap->dime.chunksize = 0;
  11100. soap->dime.buflen = 0;
  11101. #endif
  11102. soap->other = 0;
  11103. soap->root = -1;
  11104. soap->null = 0;
  11105. soap->position = 0;
  11106. soap->encoding = 0;
  11107. soap->mustUnderstand = 0;
  11108. soap->ns = 0;
  11109. soap->part = SOAP_END;
  11110. soap->event = 0;
  11111. soap->evlev = 0;
  11112. soap->alloced = 0;
  11113. soap->count = 0;
  11114. soap->length = 0;
  11115. soap->cdata = 0;
  11116. soap->peeked = 0;
  11117. soap->ahead = 0;
  11118. soap->idnum = 0;
  11119. soap->level = 0;
  11120. soap->status = 0;
  11121. soap->error = SOAP_OK;
  11122. soap->errmode = 0;
  11123. soap->errnum = 0;
  11124. }
  11125. /******************************************************************************/
  11126. SOAP_FMAC1
  11127. void
  11128. SOAP_FMAC2
  11129. soap_begin(struct soap *soap) {
  11130. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Clean up for input/output\n"));
  11131. soap->error = SOAP_OK;
  11132. if (!soap->keep_alive) {
  11133. soap->buflen = 0;
  11134. soap->bufidx = 0;
  11135. }
  11136. soap->encoding = 0;
  11137. soap->mode = 0;
  11138. soap->part = SOAP_END;
  11139. soap->peeked = 0;
  11140. soap->ahead = 0;
  11141. soap->level = 0;
  11142. *soap->endpoint = '\0';
  11143. soap->encodingStyle = SOAP_STR_EOS;
  11144. soap_free_temp(soap);
  11145. }
  11146. /******************************************************************************/
  11147. SOAP_FMAC1
  11148. void
  11149. SOAP_FMAC2
  11150. soap_end(struct soap *soap) {
  11151. if (soap_check_state(soap))
  11152. return;
  11153. soap_free_temp(soap);
  11154. soap_dealloc(soap, NULL);
  11155. while (soap->clist) {
  11156. struct soap_clist *cp = soap->clist->next;
  11157. SOAP_FREE(soap, soap->clist);
  11158. soap->clist = cp;
  11159. }
  11160. soap_closesock(soap);
  11161. #ifdef SOAP_DEBUG
  11162. soap_close_logfiles(soap);
  11163. #endif
  11164. }
  11165. /******************************************************************************/
  11166. SOAP_FMAC1
  11167. void
  11168. SOAP_FMAC2
  11169. soap_set_version(struct soap *soap, short version) {
  11170. soap_set_local_namespaces(soap);
  11171. if (soap->version != version && soap->local_namespaces && soap->local_namespaces[0].id &&
  11172. soap->local_namespaces[1].id) {
  11173. if (version == 1) {
  11174. soap->local_namespaces[0].ns = soap_env1;
  11175. soap->local_namespaces[1].ns = soap_enc1;
  11176. } else if (version == 2) {
  11177. soap->local_namespaces[0].ns = soap_env2;
  11178. soap->local_namespaces[1].ns = soap_enc2;
  11179. }
  11180. soap->version = version;
  11181. }
  11182. if (version == 0)
  11183. soap->encodingStyle = SOAP_STR_EOS;
  11184. else
  11185. soap->encodingStyle = NULL;
  11186. }
  11187. /******************************************************************************/
  11188. static void
  11189. soap_version(struct soap *soap) {
  11190. struct Namespace *p = soap->local_namespaces;
  11191. if (p) {
  11192. const char *ns = p[0].out;
  11193. if (!ns)
  11194. ns = p[0].ns;
  11195. if (ns) {
  11196. if (!strcmp(ns, soap_env1)) {
  11197. soap->version = 1; /* make sure we use SOAP 1.1 */
  11198. if (p[1].out)
  11199. SOAP_FREE(soap, p[1].out);
  11200. p[1].out = (char *) SOAP_MALLOC(soap, sizeof(soap_enc1));
  11201. if (p[1].out)
  11202. (void) soap_memcpy(p[1].out, sizeof(soap_enc1), soap_enc1, sizeof(soap_enc1));
  11203. } else if (!strcmp(ns, soap_env2)) {
  11204. soap->version = 2; /* make sure we use SOAP 1.2 */
  11205. if (p[1].out)
  11206. SOAP_FREE(soap, p[1].out);
  11207. p[1].out = (char *) SOAP_MALLOC(soap, sizeof(soap_enc2));
  11208. if (p[1].out)
  11209. (void) soap_memcpy(p[1].out, sizeof(soap_enc2), soap_enc2, sizeof(soap_enc2));
  11210. }
  11211. }
  11212. }
  11213. }
  11214. /******************************************************************************/
  11215. SOAP_FMAC1
  11216. int
  11217. SOAP_FMAC2
  11218. soap_set_namespaces(struct soap *soap, const struct Namespace *p) {
  11219. struct Namespace *ns = soap->local_namespaces;
  11220. struct soap_nlist *np, *nq, *nr;
  11221. unsigned int level = soap->level;
  11222. soap->namespaces = p;
  11223. soap->local_namespaces = NULL;
  11224. soap_set_local_namespaces(soap);
  11225. /* reverse the namespace list */
  11226. np = soap->nlist;
  11227. soap->nlist = NULL;
  11228. if (np) {
  11229. nq = np->next;
  11230. np->next = NULL;
  11231. while (nq) {
  11232. nr = nq->next;
  11233. nq->next = np;
  11234. np = nq;
  11235. nq = nr;
  11236. }
  11237. }
  11238. /* then push on new stack */
  11239. while (np) {
  11240. const char *s;
  11241. soap->level = np->level; /* preserve element nesting level */
  11242. s = np->ns;
  11243. if (!s && np->index >= 0 && ns) {
  11244. s = ns[np->index].out;
  11245. if (!s)
  11246. s = ns[np->index].ns;
  11247. }
  11248. if (s)
  11249. (void) soap_push_namespace(soap, np->id, s);
  11250. nq = np;
  11251. np = np->next;
  11252. SOAP_FREE(soap, nq);
  11253. }
  11254. if (ns) {
  11255. int i;
  11256. for (i = 0; ns[i].id; i++) {
  11257. if (ns[i].out) {
  11258. SOAP_FREE(soap, ns[i].out);
  11259. ns[i].out = NULL;
  11260. }
  11261. }
  11262. SOAP_FREE(soap, ns);
  11263. }
  11264. soap->level = level; /* restore level */
  11265. return SOAP_OK;
  11266. }
  11267. /******************************************************************************/
  11268. SOAP_FMAC1
  11269. void
  11270. SOAP_FMAC2
  11271. soap_set_local_namespaces(struct soap *soap) {
  11272. if (soap->namespaces && !soap->local_namespaces) {
  11273. const struct Namespace *ns1;
  11274. struct Namespace *ns2;
  11275. size_t n = 1;
  11276. for (ns1 = soap->namespaces; ns1->id; ns1++)
  11277. n++;
  11278. n *= sizeof(struct Namespace);
  11279. ns2 = (struct Namespace *) SOAP_MALLOC(soap, n);
  11280. if (ns2) {
  11281. (void) soap_memcpy((void *) ns2, n, (const void *) soap->namespaces, n);
  11282. if (ns2[0].ns) {
  11283. if (!strcmp(ns2[0].ns, soap_env1))
  11284. soap->version = 1;
  11285. else if (!strcmp(ns2[0].ns, soap_env2))
  11286. soap->version = 2;
  11287. }
  11288. soap->local_namespaces = ns2;
  11289. for (; ns2->id; ns2++)
  11290. ns2->out = NULL;
  11291. }
  11292. }
  11293. }
  11294. /******************************************************************************/
  11295. #ifndef WITH_LEAN
  11296. SOAP_FMAC1
  11297. const char *
  11298. SOAP_FMAC2
  11299. soap_tagsearch(const char *big, const char *little) {
  11300. if (big && little) {
  11301. size_t n = strlen(little);
  11302. const char *s = big;
  11303. while (s) {
  11304. const char *t = s;
  11305. size_t i;
  11306. for (i = 0; i < n; i++, t++) {
  11307. if (*t != little[i])
  11308. break;
  11309. }
  11310. if (*t == '\0' || *t == ' ') {
  11311. if (i == n || (i > 0 && little[i - 1] == ':'))
  11312. return s;
  11313. }
  11314. s = strchr(t, ' ');
  11315. if (s)
  11316. s++;
  11317. }
  11318. }
  11319. return NULL;
  11320. }
  11321. #endif
  11322. /******************************************************************************/
  11323. SOAP_FMAC1
  11324. struct soap_nlist *
  11325. SOAP_FMAC2
  11326. soap_lookup_ns(struct soap *soap, const char *tag, size_t n) {
  11327. struct soap_nlist *np;
  11328. for (np = soap->nlist; np; np = np->next)
  11329. if (!strncmp(np->id, tag, n) && !np->id[n])
  11330. return np;
  11331. return NULL;
  11332. }
  11333. /******************************************************************************/
  11334. #ifndef WITH_LEAN
  11335. static struct soap_nlist *
  11336. soap_push_ns(struct soap *soap, const char *id, const char *ns, short utilized, short isearly) {
  11337. struct soap_nlist *np = NULL;
  11338. size_t n, k;
  11339. unsigned int level = soap->level + isearly;
  11340. if (soap_tagsearch(soap->c14nexclude, id))
  11341. return NULL;
  11342. if (!utilized) {
  11343. for (np = soap->nlist; np; np = np->next) {
  11344. if (!strcmp(np->id, id) && ((!np->ns && *id) || (np->ns && !strcmp(np->ns, ns))))
  11345. break;
  11346. }
  11347. if (np) {
  11348. if ((np->level < level || (!np->ns && *id)) && np->index == 1)
  11349. utilized = 1;
  11350. else
  11351. return NULL;
  11352. }
  11353. }
  11354. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Adding namespace binding (level=%u) '%s' '%s' utilized=%d\n", level, id,
  11355. ns ? ns : "(null)", utilized));
  11356. n = strlen(id);
  11357. if (ns)
  11358. k = strlen(ns) + 1;
  11359. else
  11360. k = 0;
  11361. if (sizeof(struct soap_nlist) + n + k > n &&
  11362. (SOAP_MAXALLOCSIZE <= 0 || sizeof(struct soap_nlist) + n + k <= SOAP_MAXALLOCSIZE))
  11363. np = (struct soap_nlist *) SOAP_MALLOC(soap, sizeof(struct soap_nlist) + n + k);
  11364. if (!np) {
  11365. soap->error = SOAP_EOM;
  11366. return NULL;
  11367. }
  11368. np->next = soap->nlist;
  11369. soap->nlist = np;
  11370. soap_strcpy((char *) np->id, n + 1, id);
  11371. if (ns) {
  11372. np->ns = np->id + n + 1;
  11373. soap_strcpy((char *) np->ns, k, ns);
  11374. } else {
  11375. np->ns = NULL;
  11376. }
  11377. np->level = level;
  11378. np->index = utilized;
  11379. return np;
  11380. }
  11381. #endif
  11382. /******************************************************************************/
  11383. #ifndef WITH_LEAN
  11384. static void
  11385. soap_utilize_ns(struct soap *soap, const char *tag, short isearly) {
  11386. struct soap_nlist *np;
  11387. size_t n = 0;
  11388. if (!strncmp(tag, "xmlns:", 6)) {
  11389. tag += 6;
  11390. n = strlen(tag);
  11391. } else {
  11392. const char *t = strchr(tag, ':');
  11393. if (t)
  11394. n = t - tag;
  11395. }
  11396. np = soap_lookup_ns(soap, tag, n);
  11397. if (np) {
  11398. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Utilizing namespace of '%s' at level %u utilized=%d at level=%u\n", tag,
  11399. soap->level + isearly, np->index, np->level));
  11400. if (np->index <= 0) {
  11401. if (np->level == soap->level + isearly)
  11402. np->index = 1;
  11403. else
  11404. (void) soap_push_ns(soap, np->id, np->ns, 1, isearly);
  11405. }
  11406. } else if (strncmp(tag, "xml", 3)) {
  11407. DBGLOG(TEST,
  11408. SOAP_MESSAGE(fdebug, "Utilizing default namespace of '%s' at level %u\n", tag, soap->level + isearly));
  11409. (void) soap_strncpy(soap->tag, sizeof(soap->tag), tag, n);
  11410. (void) soap_push_ns(soap, soap->tag, NULL, 1, isearly);
  11411. }
  11412. }
  11413. #endif
  11414. /******************************************************************************/
  11415. SOAP_FMAC1
  11416. int
  11417. SOAP_FMAC2
  11418. soap_element(struct soap *soap, const char *tag, int id, const char *type) {
  11419. #ifndef WITH_LEAN
  11420. const char *s;
  11421. #endif
  11422. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Element begin tag='%s' level='%u' id='%d' type='%s'\n", tag, soap->level, id,
  11423. type ? type : SOAP_STR_EOS));
  11424. #ifdef WITH_DOM
  11425. #ifndef WITH_LEAN
  11426. if (soap_tagsearch(soap->wsuid, tag))
  11427. {
  11428. size_t i;
  11429. for (s = tag, i = 0; *s && i < sizeof(soap->href) - 1; s++, i++)
  11430. soap->href[i] = *s == ':' ? '-' : *s;
  11431. soap->href[i] = '\0';
  11432. if (soap_set_attr(soap, "wsu:Id", soap->href, 1))
  11433. return soap->error;
  11434. }
  11435. #endif
  11436. #endif
  11437. soap->level++;
  11438. if (soap->level > soap->maxlevel)
  11439. return soap->error = SOAP_LEVEL;
  11440. #ifdef WITH_DOM
  11441. #ifndef WITH_LEAN
  11442. if ((soap->mode & SOAP_XML_CANONICAL) && !(soap->mode & SOAP_DOM_ASIS))
  11443. {
  11444. if (soap->evlev >= soap->level)
  11445. soap->evlev = 0;
  11446. if (soap->event == SOAP_SEC_BEGIN && !soap->evlev)
  11447. {
  11448. struct soap_nlist *np;
  11449. /* non-nested wsu:Id found: clear xmlns, re-emit them for exc-c14n */
  11450. for (np = soap->nlist; np; np = np->next)
  11451. {
  11452. int p = soap->c14ninclude ? *soap->c14ninclude == '+' || soap_tagsearch(soap->c14ninclude, np->id) != NULL : 0;
  11453. if (np->index == 2 || p)
  11454. {
  11455. struct soap_nlist *np1 = soap_push_ns(soap, np->id, np->ns, 1, 0);
  11456. if (np1 && !p)
  11457. np1->index = 0;
  11458. }
  11459. }
  11460. soap->evlev = soap->level;
  11461. }
  11462. }
  11463. #endif
  11464. if ((soap->mode & SOAP_XML_DOM))
  11465. {
  11466. struct soap_dom_element *elt = (struct soap_dom_element*)soap_malloc(soap, sizeof(struct soap_dom_element));
  11467. if (!elt)
  11468. return soap->error = SOAP_EOM;
  11469. 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));
  11470. elt->soap = soap;
  11471. elt->next = NULL;
  11472. elt->prnt = soap->dom;
  11473. elt->elts = NULL;
  11474. elt->atts = NULL;
  11475. elt->nstr = NULL;
  11476. elt->name = soap_strdup(soap, tag);
  11477. elt->lead = NULL;
  11478. elt->text = NULL;
  11479. elt->code = NULL;
  11480. elt->tail = NULL;
  11481. elt->node = NULL;
  11482. elt->type = 0;
  11483. if (soap->dom)
  11484. {
  11485. struct soap_dom_element *p = soap->dom->elts;
  11486. if (p)
  11487. {
  11488. while (p->next)
  11489. p = p->next;
  11490. p->next = elt;
  11491. }
  11492. else
  11493. {
  11494. soap->dom->elts = elt;
  11495. }
  11496. }
  11497. soap->dom = elt;
  11498. if (!elt->name)
  11499. return soap->error = SOAP_EOM;
  11500. }
  11501. else
  11502. {
  11503. #endif
  11504. #ifndef WITH_LEAN
  11505. if (!soap->ns) {
  11506. if (!(soap->mode & SOAP_XML_CANONICAL) && soap_send(soap, soap->prolog))
  11507. return soap->error;
  11508. } else if ((soap->mode & SOAP_XML_INDENT)) {
  11509. if (soap->ns == 1 &&
  11510. soap_send_raw(soap, soap_indent, soap->level < sizeof(soap_indent) ? soap->level : sizeof(soap_indent) - 1))
  11511. return soap->error;
  11512. soap->body = 1;
  11513. }
  11514. if ((soap->mode & SOAP_XML_DEFAULTNS)) {
  11515. size_t n = 0;
  11516. s = strchr(tag, ':');
  11517. if (s)
  11518. n = s++ - tag;
  11519. else
  11520. s = tag;
  11521. if (soap_send_raw(soap, "<", 1)
  11522. || soap_send(soap, s))
  11523. return soap->error;
  11524. if (n) {
  11525. struct Namespace *ns = soap->local_namespaces;
  11526. for (; ns && ns->id; ns++) {
  11527. if (*ns->id && ns->ns && !strncmp(ns->id, tag, n) && !ns->id[n]) {
  11528. if (!soap->nlist || *soap->nlist->id || (soap->nlist->ns && strcmp(soap->nlist->ns, ns->ns))) {
  11529. (void) soap_push_ns(soap, SOAP_STR_EOS, ns->out ? ns->out : ns->ns, 0, 0);
  11530. if (soap_attribute(soap, "xmlns", ns->out ? ns->out : ns->ns))
  11531. return soap->error;
  11532. }
  11533. break;
  11534. }
  11535. }
  11536. }
  11537. #ifndef WITH_NOEMPTYNAMESPACES
  11538. else if (!soap->nlist || *soap->nlist->id || (soap->nlist->ns && *soap->nlist->ns)) {
  11539. (void) soap_push_ns(soap, SOAP_STR_EOS, SOAP_STR_EOS, 0, 0);
  11540. if (soap_attribute(soap, "xmlns", SOAP_STR_EOS))
  11541. return soap->error;
  11542. }
  11543. #endif
  11544. } else
  11545. #endif
  11546. if (soap_send_raw(soap, "<", 1)
  11547. || soap_send(soap, tag))
  11548. return soap->error;
  11549. #ifdef WITH_DOM
  11550. }
  11551. #endif
  11552. if (!soap->ns) {
  11553. struct Namespace *ns = soap->local_namespaces;
  11554. int k = -1;
  11555. if (ns) {
  11556. while (k-- && ns->id) {
  11557. const char *t = ns->out;
  11558. if (!t)
  11559. t = ns->ns;
  11560. if (*ns->id && t && *t) {
  11561. (SOAP_SNPRINTF(soap->tmpbuf, sizeof(soap->tmpbuf), strlen(ns->id) + 6), "xmlns:%s", ns->id);
  11562. if (soap_attribute(soap, soap->tmpbuf, t))
  11563. return soap->error;
  11564. }
  11565. ns++;
  11566. }
  11567. }
  11568. }
  11569. soap->ns = 1; /* namespace table control: ns = 0 or 2 to start, then 1 to stop dumping the table */
  11570. #ifndef WITH_LEAN
  11571. if ((soap->mode & SOAP_XML_CANONICAL)) {
  11572. if ((soap->mode & SOAP_XML_DEFAULTNS))
  11573. soap_utilize_ns(soap, SOAP_STR_EOS, 0);
  11574. else
  11575. soap_utilize_ns(soap, tag, 0);
  11576. }
  11577. #endif
  11578. if (id > 0) {
  11579. (SOAP_SNPRINTF(soap->tmpbuf, sizeof(soap->tmpbuf), sizeof(SOAP_BASEREFNAME) + 20), SOAP_BASEREFNAME "%d", id);
  11580. if (soap->version == 2) {
  11581. if (soap_attribute(soap, "SOAP-ENC:id", soap->tmpbuf))
  11582. return soap->error;
  11583. } else if (soap_attribute(soap, "id", soap->tmpbuf)) {
  11584. return soap->error;
  11585. }
  11586. }
  11587. if (type && *type && !(soap->mode & SOAP_XML_NOTYPE)) {
  11588. #ifndef WITH_LEAN
  11589. if ((soap->mode & SOAP_XML_CANONICAL) && !(soap->mode & SOAP_XML_CANONICAL_NA))
  11590. soap_utilize_ns(soap, type, 0);
  11591. #endif
  11592. if (soap_attribute(soap, "xsi:type", type))
  11593. return soap->error;
  11594. }
  11595. if (soap->null && soap->position > 0 && soap->version == 1) {
  11596. int i;
  11597. (SOAP_SNPRINTF(soap->tmpbuf, sizeof(soap->tmpbuf) - 1, 20), "[%d", soap->positions[0]);
  11598. for (i = 1; i < soap->position; i++) {
  11599. size_t l = strlen(soap->tmpbuf);
  11600. (SOAP_SNPRINTF(soap->tmpbuf + l, sizeof(soap->tmpbuf) - l - 1, 20), ",%d", soap->positions[i]);
  11601. }
  11602. soap_strcat(soap->tmpbuf, sizeof(soap->tmpbuf), "]");
  11603. if (soap_attribute(soap, "SOAP-ENC:position", soap->tmpbuf))
  11604. return soap->error;
  11605. }
  11606. if (soap->mustUnderstand) {
  11607. if (soap->actor && *soap->actor) {
  11608. if (soap_attribute(soap, soap->version == 2 ? "SOAP-ENV:role" : "SOAP-ENV:actor", soap->actor))
  11609. return soap->error;
  11610. }
  11611. if (soap_attribute(soap, "SOAP-ENV:mustUnderstand", soap->version == 2 ? "true" : "1"))
  11612. return soap->error;
  11613. soap->mustUnderstand = 0;
  11614. }
  11615. if (soap->encoding) {
  11616. if (soap->encodingStyle && soap->local_namespaces && soap->local_namespaces[0].id &&
  11617. soap->local_namespaces[1].id) {
  11618. if (!*soap->encodingStyle) {
  11619. if (soap->local_namespaces[1].out)
  11620. soap->encodingStyle = soap->local_namespaces[1].out;
  11621. else
  11622. soap->encodingStyle = soap->local_namespaces[1].ns;
  11623. }
  11624. if (soap->encodingStyle && soap_attribute(soap, "SOAP-ENV:encodingStyle", soap->encodingStyle))
  11625. return soap->error;
  11626. } else {
  11627. soap->encodingStyle = NULL;
  11628. }
  11629. soap->encoding = 0;
  11630. }
  11631. soap->null = 0;
  11632. soap->position = 0;
  11633. return SOAP_OK;
  11634. }
  11635. /******************************************************************************/
  11636. SOAP_FMAC1
  11637. int
  11638. SOAP_FMAC2
  11639. soap_element_begin_out(struct soap *soap, const char *tag, int id, const char *type) {
  11640. if (*tag == '-')
  11641. return SOAP_OK;
  11642. #ifdef WITH_DOM
  11643. if (soap->feltbegout)
  11644. return soap->error = soap->feltbegout(soap, tag, id, type);
  11645. #endif
  11646. if (soap_element(soap, tag, id, type))
  11647. return soap->error;
  11648. return soap_element_start_end_out(soap, NULL);
  11649. }
  11650. /******************************************************************************/
  11651. #if _MSC_VER < 1400 && !defined(HAVE_STRLCAT)
  11652. /* concat string (truncating the result, strings must not be NULL) */
  11653. SOAP_FMAC1
  11654. void
  11655. SOAP_FMAC2
  11656. soap_strcat(char *t, size_t n, const char *s) {
  11657. size_t k = strlen(t);
  11658. if (k < n) {
  11659. t += k;
  11660. n -= k;
  11661. while (--n > 0 && *s)
  11662. *t++ = *s++;
  11663. *t = '\0';
  11664. }
  11665. }
  11666. #endif
  11667. /******************************************************************************/
  11668. #if _MSC_VER < 1400
  11669. /* concat string up to m chars (leaves destination intact on overrun and returns nonzero, zero if OK) */
  11670. SOAP_FMAC1
  11671. int
  11672. SOAP_FMAC2
  11673. soap_strncat(char *t, size_t n, const char *s, size_t m) {
  11674. size_t k;
  11675. if (!t || !s)
  11676. return 1;
  11677. k = strlen(t);
  11678. if (n <= k + m)
  11679. return 1;
  11680. t += k;
  11681. n -= k;
  11682. while (--n > 0 && *s)
  11683. *t++ = *s++;
  11684. *t = '\0';
  11685. return 0;
  11686. }
  11687. #endif
  11688. /******************************************************************************/
  11689. #ifndef HAVE_STRRCHR
  11690. SOAP_FMAC1
  11691. char*
  11692. SOAP_FMAC2
  11693. soap_strrchr(const char *s, int t)
  11694. {
  11695. char *r = NULL;
  11696. while (*s)
  11697. if (*s++ == t)
  11698. r = (char*)s - 1;
  11699. return r;
  11700. }
  11701. #endif
  11702. /******************************************************************************/
  11703. #ifndef HAVE_STRTOL
  11704. SOAP_FMAC1
  11705. long
  11706. SOAP_FMAC2
  11707. soap_strtol(const char *s, char **t, int b)
  11708. {
  11709. long n = 0;
  11710. int c;
  11711. while (*s > 0 && *s <= 32)
  11712. s++;
  11713. if (b == 10)
  11714. {
  11715. short neg = 0;
  11716. if (*s == '-')
  11717. {
  11718. s++;
  11719. neg = 1;
  11720. }
  11721. else if (*s == '+')
  11722. {
  11723. s++;
  11724. }
  11725. while ((c = *s) && c >= '0' && c <= '9')
  11726. {
  11727. if (n >= 214748364 && (n > 214748364 || c >= '8'))
  11728. {
  11729. if (neg && n == 214748364 && c == '8')
  11730. {
  11731. if (t)
  11732. *t = (char*)(s + 1);
  11733. return -2147483648;
  11734. }
  11735. break;
  11736. }
  11737. n *= 10;
  11738. n += c - '0';
  11739. s++;
  11740. }
  11741. if (neg)
  11742. n = -n;
  11743. }
  11744. else /* assume b == 16 and value is always positive */
  11745. {
  11746. while ((c = *s))
  11747. {
  11748. if (c >= '0' && c <= '9')
  11749. c -= '0';
  11750. else if (c >= 'A' && c <= 'F')
  11751. c -= 'A' - 10;
  11752. else if (c >= 'a' && c <= 'f')
  11753. c -= 'a' - 10;
  11754. if (n > 0x07FFFFFF)
  11755. break;
  11756. n <<= 4;
  11757. n += c;
  11758. s++;
  11759. }
  11760. }
  11761. if (t)
  11762. *t = (char*)s;
  11763. return n;
  11764. }
  11765. #endif
  11766. /******************************************************************************/
  11767. #ifndef HAVE_STRTOUL
  11768. SOAP_FMAC1
  11769. unsigned long
  11770. SOAP_FMAC2
  11771. soap_strtoul(const char *s, char **t, int b)
  11772. {
  11773. unsigned long n = 0;
  11774. int c;
  11775. while (*s > 0 && *s <= 32)
  11776. s++;
  11777. if (b == 10)
  11778. {
  11779. short neg = 0;
  11780. if (*s == '-')
  11781. {
  11782. s++;
  11783. neg = 1;
  11784. }
  11785. else if (*s == '+')
  11786. {
  11787. s++;
  11788. }
  11789. while ((c = *s) && c >= '0' && c <= '9')
  11790. {
  11791. if (n >= 429496729 && (n > 429496729 || c >= '6'))
  11792. break;
  11793. n *= 10;
  11794. n += c - '0';
  11795. s++;
  11796. }
  11797. if (neg && n > 0)
  11798. s--;
  11799. }
  11800. else /* b == 16 */
  11801. {
  11802. while ((c = *s))
  11803. {
  11804. if (c >= '0' && c <= '9')
  11805. c -= '0';
  11806. else if (c >= 'A' && c <= 'F')
  11807. c -= 'A' - 10;
  11808. else if (c >= 'a' && c <= 'f')
  11809. c -= 'a' - 10;
  11810. if (n > 0x0FFFFFFF)
  11811. break;
  11812. n <<= 4;
  11813. n += c;
  11814. s++;
  11815. }
  11816. }
  11817. if (t)
  11818. *t = (char*)s;
  11819. return n;
  11820. }
  11821. #endif
  11822. /******************************************************************************/
  11823. #ifndef soap_strtoll
  11824. SOAP_FMAC1
  11825. LONG64
  11826. SOAP_FMAC2
  11827. soap_strtoll(const char *s, char **t, int b)
  11828. {
  11829. LONG64 n = 0LL;
  11830. int c;
  11831. while (*s > 0 && *s <= 32)
  11832. s++;
  11833. if (b == 10)
  11834. {
  11835. short neg = 0;
  11836. if (*s == '-')
  11837. {
  11838. s++;
  11839. neg = 1;
  11840. }
  11841. else if (*s == '+')
  11842. {
  11843. s++;
  11844. }
  11845. while ((c = *s) && c >= '0' && c <= '9')
  11846. {
  11847. if (n >= 922337203685477580LL && (n > 922337203685477580LL || c >= '8'))
  11848. {
  11849. if (neg && n == 922337203685477580LL && c == '8')
  11850. {
  11851. if (t)
  11852. *t = (char*)(s + 1);
  11853. return -9223372036854775807LL - 1LL; /* appease compilers that complain */
  11854. }
  11855. break;
  11856. }
  11857. n *= 10LL;
  11858. n += c - '0';
  11859. s++;
  11860. }
  11861. if (neg)
  11862. n = -n;
  11863. }
  11864. else /* assume b == 16 and value is always positive */
  11865. {
  11866. while ((c = *s))
  11867. {
  11868. if (c >= '0' && c <= '9')
  11869. c -= '0';
  11870. else if (c >= 'A' && c <= 'F')
  11871. c -= 'A' - 10;
  11872. else if (c >= 'a' && c <= 'f')
  11873. c -= 'a' - 10;
  11874. if (n > 0x07FFFFFFFFFFFFFFLL)
  11875. break;
  11876. n <<= 4;
  11877. n += c;
  11878. s++;
  11879. }
  11880. }
  11881. if (t)
  11882. *t = (char*)s;
  11883. return n;
  11884. }
  11885. #endif
  11886. /******************************************************************************/
  11887. #ifndef soap_strtoull
  11888. SOAP_FMAC1
  11889. ULONG64
  11890. SOAP_FMAC2
  11891. soap_strtoull(const char *s, char **t, int b)
  11892. {
  11893. ULONG64 n = 0UL;
  11894. int c;
  11895. while (*s > 0 && *s <= 32)
  11896. s++;
  11897. if (b == 10)
  11898. {
  11899. short neg = 0;
  11900. if (*s == '-')
  11901. {
  11902. s++;
  11903. neg = 1;
  11904. }
  11905. else if (*s == '+')
  11906. {
  11907. s++;
  11908. }
  11909. while ((c = *s) && c >= '0' && c <= '9')
  11910. {
  11911. if (n >= 1844674407370955161ULL)
  11912. break;
  11913. n *= 10UL;
  11914. n += c - '0';
  11915. s++;
  11916. }
  11917. if (neg && n > 0UL)
  11918. s--;
  11919. }
  11920. else /* b == 16 */
  11921. {
  11922. while ((c = *s))
  11923. {
  11924. if (c >= '0' && c <= '9')
  11925. c -= '0';
  11926. else if (c >= 'A' && c <= 'F')
  11927. c -= 'A' - 10;
  11928. else if (c >= 'a' && c <= 'f')
  11929. c -= 'a' - 10;
  11930. if (n > 0x0FFFFFFFFFFFFFFFULL)
  11931. break;
  11932. n <<= 4;
  11933. n += c;
  11934. s++;
  11935. }
  11936. }
  11937. if (t)
  11938. *t = (char*)s;
  11939. return n;
  11940. }
  11941. #endif
  11942. /******************************************************************************/
  11943. SOAP_FMAC1
  11944. int
  11945. SOAP_FMAC2
  11946. soap_array_begin_out(struct soap *soap, const char *tag, int id, const char *type, const char *offset) {
  11947. if (!type || !*type || soap->version == 0)
  11948. return soap_element_begin_out(soap, tag, id, NULL);
  11949. if (soap_element(soap, tag, id, NULL))
  11950. return soap->error;
  11951. if (soap->version == 1) {
  11952. if (offset && soap_attribute(soap, "SOAP-ENC:offset", offset))
  11953. return soap->error;
  11954. if (soap_attribute(soap, "SOAP-ENC:arrayType", type))
  11955. return soap->error;
  11956. } else {
  11957. const char *s;
  11958. s = strchr(type, '[');
  11959. if (s && (size_t) (s - type) < sizeof(soap->tmpbuf)) {
  11960. (void) soap_strncpy(soap->tmpbuf, sizeof(soap->tmpbuf), type, s - type);
  11961. if (soap_attribute(soap, "SOAP-ENC:itemType", soap->tmpbuf))
  11962. return soap->error;
  11963. s++;
  11964. if (*s && *s != ']') {
  11965. soap_strcpy(soap->tmpbuf, sizeof(soap->tmpbuf), s);
  11966. soap->tmpbuf[strlen(soap->tmpbuf) - 1] = '\0';
  11967. if (soap_attribute(soap, "SOAP-ENC:arraySize", soap->tmpbuf))
  11968. return soap->error;
  11969. }
  11970. }
  11971. }
  11972. #ifndef WITH_LEAN
  11973. if ((soap->mode & SOAP_XML_CANONICAL) && !(soap->mode & SOAP_XML_CANONICAL_NA))
  11974. soap_utilize_ns(soap, type, 0);
  11975. #endif
  11976. return soap_element_start_end_out(soap, NULL);
  11977. }
  11978. /******************************************************************************/
  11979. SOAP_FMAC1
  11980. int
  11981. SOAP_FMAC2
  11982. soap_element_start_end_out(struct soap *soap, const char *tag) {
  11983. struct soap_attribute *tp;
  11984. #ifndef WITH_LEAN
  11985. if ((soap->mode & SOAP_XML_CANONICAL)) {
  11986. struct soap_nlist *np;
  11987. for (tp = soap->attributes; tp; tp = tp->next) {
  11988. if (tp->visible && *tp->name && strchr(tp->name, ':'))
  11989. soap_utilize_ns(soap, tp->name, 0);
  11990. }
  11991. if (soap->event == SOAP_SEC_BEGIN) {
  11992. for (np = soap->nlist; np; np = np->next)
  11993. if (soap_tagsearch(soap->c14ninclude, np->id))
  11994. (void) soap_push_ns(soap, np->id, np->ns, 1, 0);
  11995. soap->event = 0;
  11996. soap->evlev = 0;
  11997. }
  11998. for (np = soap->nlist; np; np = np->next) {
  11999. if (np->ns && np->index == 1) {
  12000. if (*np->id)
  12001. (SOAP_SNPRINTF(soap->tmpbuf, sizeof(soap->tmpbuf), strlen(np->id) + 6), "xmlns:%s", np->id);
  12002. else
  12003. soap_strcpy(soap->tmpbuf, sizeof(soap->tmpbuf), "xmlns");
  12004. DBGLOG(TEST, SOAP_MESSAGE(fdebug,
  12005. "Enabling utilized binding (level=%u) %s='%s' SEC-BEGIN=%d c14ninclude='%s'\n",
  12006. np->level, soap->tmpbuf, np->ns, soap->event == SOAP_SEC_BEGIN,
  12007. soap->c14ninclude ? soap->c14ninclude : "(null)"));
  12008. np->index = 2;
  12009. soap->level--;
  12010. if (*np->id || *np->ns || soap->level > 1)
  12011. if (soap_set_attr(soap, soap->tmpbuf, np->ns, 1))
  12012. return soap->error;
  12013. soap->level++;
  12014. } else {
  12015. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Binding (level=%u) %s='%s' utilized=%d\n", np->level, np->id, np->ns,
  12016. np->index));
  12017. }
  12018. }
  12019. }
  12020. #endif
  12021. #ifdef WITH_DOM
  12022. if ((soap->mode & SOAP_XML_DOM) && soap->dom)
  12023. {
  12024. struct soap_dom_attribute **att;
  12025. att = &soap->dom->atts;
  12026. for (tp = soap->attributes; tp; tp = tp->next)
  12027. {
  12028. if (tp->visible)
  12029. {
  12030. *att = (struct soap_dom_attribute*)soap_malloc(soap, sizeof(struct soap_dom_attribute));
  12031. if (!*att)
  12032. return soap->error;
  12033. (*att)->next = NULL;
  12034. (*att)->nstr = NULL;
  12035. (*att)->name = soap_strdup(soap, tp->name);
  12036. (*att)->text = soap_strdup(soap, tp->value);
  12037. (*att)->soap = soap;
  12038. if (!(*att)->name || (tp->value && !(*att)->text))
  12039. return soap->error = SOAP_EOM;
  12040. att = &(*att)->next;
  12041. tp->visible = 0;
  12042. }
  12043. }
  12044. return SOAP_OK;
  12045. }
  12046. #endif
  12047. for (tp = soap->attributes; tp; tp = tp->next) {
  12048. if (tp->visible) {
  12049. if (soap_send_raw(soap, " ", 1) || soap_send(soap, tp->name))
  12050. return soap->error;
  12051. if (tp->visible == 2 && tp->value) {
  12052. if (soap_send_raw(soap, "=\"", 2)
  12053. || soap_string_out(soap, tp->value, tp->flag)
  12054. || soap_send_raw(soap, "\"", 1))
  12055. return soap->error;
  12056. } else {
  12057. if (soap_send_raw(soap, "=\"\"", 3))
  12058. return soap->error;
  12059. }
  12060. tp->visible = 0;
  12061. }
  12062. }
  12063. if (tag) {
  12064. #ifndef WITH_LEAN
  12065. if ((soap->mode & SOAP_XML_CANONICAL)) {
  12066. if (soap_send_raw(soap, ">", 1)
  12067. || soap_element_end_out(soap, tag))
  12068. return soap->error;
  12069. return SOAP_OK;
  12070. }
  12071. #endif
  12072. if (soap->nlist)
  12073. soap_pop_namespace(soap);
  12074. soap->level--; /* decrement level just before /> */
  12075. soap->body = 0;
  12076. return soap_send_raw(soap, "/>", 2);
  12077. }
  12078. return soap_send_raw(soap, ">", 1);
  12079. }
  12080. /******************************************************************************/
  12081. SOAP_FMAC1
  12082. int
  12083. SOAP_FMAC2
  12084. soap_element_end_out(struct soap *soap, const char *tag) {
  12085. if (*tag == '-')
  12086. return SOAP_OK;
  12087. #ifdef WITH_DOM
  12088. if (soap->feltendout)
  12089. return soap->error = soap->feltendout(soap, tag);
  12090. #endif
  12091. return soap_element_end(soap, tag);
  12092. }
  12093. /******************************************************************************/
  12094. SOAP_FMAC1
  12095. int
  12096. SOAP_FMAC2
  12097. soap_element_end(struct soap *soap, const char *tag) {
  12098. #ifndef WITH_LEAN
  12099. const char *s;
  12100. #endif
  12101. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Element ending tag='%s'\n", tag));
  12102. #ifdef WITH_DOM
  12103. if ((soap->mode & SOAP_XML_DOM) && soap->dom)
  12104. {
  12105. if (soap->dom->prnt)
  12106. soap->dom = soap->dom->prnt;
  12107. return SOAP_OK;
  12108. }
  12109. #endif
  12110. #ifndef WITH_LEAN
  12111. if (soap->nlist)
  12112. soap_pop_namespace(soap);
  12113. if ((soap->mode & SOAP_XML_INDENT)) {
  12114. if (!soap->body) {
  12115. if (soap_send_raw(soap, soap_indent,
  12116. soap->level < sizeof(soap_indent) ? soap->level : sizeof(soap_indent) - 1))
  12117. return soap->error;
  12118. }
  12119. soap->body = 0;
  12120. }
  12121. if ((soap->mode & SOAP_XML_DEFAULTNS) && (s = strchr(tag, ':')) != NULL)
  12122. tag = s + 1;
  12123. #endif
  12124. if (soap_send_raw(soap, "</", 2)
  12125. || soap_send(soap, tag))
  12126. return soap->error;
  12127. soap->level--; /* decrement level just before > */
  12128. return soap_send_raw(soap, ">", 1);
  12129. }
  12130. /******************************************************************************/
  12131. SOAP_FMAC1
  12132. int
  12133. SOAP_FMAC2
  12134. soap_element_ref(struct soap *soap, const char *tag, int id, int href) {
  12135. const char *s = "ref";
  12136. int n = 1;
  12137. if (soap->version == 1) {
  12138. s = "href";
  12139. n = 0;
  12140. } else if (soap->version == 2) {
  12141. s = "SOAP-ENC:ref";
  12142. }
  12143. (SOAP_SNPRINTF(soap->href, sizeof(soap->href), sizeof(SOAP_BASEREFNAME) + 21), "#" SOAP_BASEREFNAME "%d", href);
  12144. return soap_element_href(soap, tag, id, s, soap->href + n);
  12145. }
  12146. /******************************************************************************/
  12147. SOAP_FMAC1
  12148. int
  12149. SOAP_FMAC2
  12150. soap_element_href(struct soap *soap, const char *tag, int id, const char *ref, const char *val) {
  12151. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Element '%s' reference %s='%s'\n", tag, ref, val));
  12152. if (soap_element(soap, tag, id, NULL)
  12153. || soap_attribute(soap, ref, val)
  12154. || soap_element_start_end_out(soap, tag))
  12155. return soap->error;
  12156. soap->body = 0;
  12157. return SOAP_OK;
  12158. }
  12159. /******************************************************************************/
  12160. SOAP_FMAC1
  12161. int
  12162. SOAP_FMAC2
  12163. soap_element_null(struct soap *soap, const char *tag, int id, const char *type) {
  12164. struct soap_attribute *tp = NULL;
  12165. for (tp = soap->attributes; tp; tp = tp->next)
  12166. if (tp->visible)
  12167. break;
  12168. if (tp || (soap->version == 2 && soap->position > 0) || id > 0 || (soap->mode & SOAP_XML_NIL)) {
  12169. if (soap_element(soap, tag, id, type)
  12170. || (!tp && soap_attribute(soap, "xsi:nil", "true"))
  12171. || soap_element_start_end_out(soap, tag))
  12172. return soap->error;
  12173. soap->body = 0;
  12174. } else {
  12175. soap->null = 1;
  12176. soap->position = 0;
  12177. soap->mustUnderstand = 0;
  12178. }
  12179. return SOAP_OK;
  12180. }
  12181. /******************************************************************************/
  12182. SOAP_FMAC1
  12183. int
  12184. SOAP_FMAC2
  12185. soap_element_empty(struct soap *soap, const char *tag) {
  12186. if (soap_element(soap, tag, -1, NULL))
  12187. return soap->error;
  12188. return soap_element_start_end_out(soap, tag);
  12189. }
  12190. /******************************************************************************/
  12191. SOAP_FMAC1
  12192. int
  12193. SOAP_FMAC2
  12194. soap_element_nil(struct soap *soap, const char *tag) {
  12195. if (soap_element(soap, tag, -1, NULL)
  12196. || (soap_attribute(soap, "xsi:nil", "true")))
  12197. return soap->error;
  12198. return soap_element_start_end_out(soap, tag);
  12199. }
  12200. /******************************************************************************/
  12201. SOAP_FMAC1
  12202. int
  12203. SOAP_FMAC2
  12204. soap_element_id(struct soap *soap, const char *tag, int id, const void *p, const void *a, int n, const char *type,
  12205. int t, char **mark) {
  12206. (void) a;
  12207. (void) n;
  12208. if (!p) {
  12209. soap->error = soap_element_null(soap, tag, id, type);
  12210. return -1;
  12211. }
  12212. #ifndef WITH_NOIDREF
  12213. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Element_id %p type=%d id=%d\n", p, t, id));
  12214. if ((!soap->encodingStyle && !(soap->mode & SOAP_XML_GRAPH)) || (soap->mode & SOAP_XML_TREE))
  12215. return soap_check_and_mark(soap, p, t, mark);
  12216. if (mark)
  12217. *mark = NULL;
  12218. if (id < -1)
  12219. return soap_embed(soap, p, a, n, t);
  12220. else if (id <= 0) {
  12221. struct soap_plist *pp;
  12222. if (a)
  12223. id = soap_array_pointer_lookup(soap, p, a, n, t, &pp);
  12224. else
  12225. id = soap_pointer_lookup(soap, p, t, &pp);
  12226. if (id) {
  12227. if (soap_is_embedded(soap, pp)) {
  12228. soap_element_ref(soap, tag, 0, id);
  12229. return -1;
  12230. }
  12231. if (soap_is_single(soap, pp))
  12232. return 0;
  12233. soap_set_embedded(soap, pp);
  12234. }
  12235. }
  12236. return id;
  12237. #else
  12238. return soap_check_and_mark(soap, p, t, mark);
  12239. #endif
  12240. }
  12241. /******************************************************************************/
  12242. SOAP_FMAC1
  12243. int
  12244. SOAP_FMAC2
  12245. soap_check_and_mark(struct soap *soap, const void *p, int t, char **mark) {
  12246. if (mark) {
  12247. struct soap_plist *pp;
  12248. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Check %p and mark %p\n", p, (void *) mark));
  12249. if (!soap_pointer_lookup(soap, p, t, &pp))
  12250. if (!soap_pointer_enter(soap, p, NULL, 0, t, &pp))
  12251. return -1;
  12252. if ((soap->mode & SOAP_IO_LENGTH)) {
  12253. if (pp->mark1 > 0)
  12254. return -1;
  12255. pp->mark1 = 1;
  12256. *mark = &pp->mark1;
  12257. } else {
  12258. if (pp->mark2 > 0)
  12259. return -1;
  12260. pp->mark2 = 1;
  12261. *mark = &pp->mark2;
  12262. }
  12263. }
  12264. return 0;
  12265. }
  12266. /******************************************************************************/
  12267. SOAP_FMAC1
  12268. void *
  12269. SOAP_FMAC2
  12270. soap_mark_lookup(struct soap *soap, const void *p, int t, struct soap_plist **ppp, char **mark) {
  12271. if (!soap)
  12272. return NULL;
  12273. if (mark || !(soap->mode & SOAP_XML_TREE)) {
  12274. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Mark lookup %p type=%d\n", p, t));
  12275. if (!soap_pointer_lookup(soap, p, t, ppp)) {
  12276. if (!soap_pointer_enter(soap, p, NULL, 0, t, ppp))
  12277. return NULL;
  12278. } else if (!(soap->mode & SOAP_XML_TREE)) {
  12279. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Mark found %p\n", (*ppp)->dup));
  12280. return (*ppp)->dup;
  12281. }
  12282. if (mark) {
  12283. if ((*ppp)->mark1 > 0)
  12284. (*ppp)->mark1 = 2; /* cycle */
  12285. else
  12286. (*ppp)->mark1 = 1; /* cycle detection */
  12287. *mark = &(*ppp)->mark1;
  12288. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Mark cycle %d\n", (*ppp)->mark1));
  12289. }
  12290. }
  12291. return NULL;
  12292. }
  12293. /******************************************************************************/
  12294. SOAP_FMAC1
  12295. int
  12296. SOAP_FMAC2
  12297. soap_mark_cycle(struct soap *soap, struct soap_plist *pp) {
  12298. (void) soap;
  12299. return pp && pp->mark1 == 2 && (soap->mode & SOAP_XML_TREE);
  12300. }
  12301. /******************************************************************************/
  12302. SOAP_FMAC1
  12303. void
  12304. SOAP_FMAC2
  12305. soap_mark_dup(struct soap *soap, void *a, struct soap_plist *pp) {
  12306. (void) soap;
  12307. if (pp)
  12308. pp->dup = a;
  12309. }
  12310. /******************************************************************************/
  12311. SOAP_FMAC1
  12312. void
  12313. SOAP_FMAC2
  12314. soap_unmark(struct soap *soap, char *mark) {
  12315. (void) soap;
  12316. if (mark)
  12317. *mark = 0; /* release detection */
  12318. }
  12319. /******************************************************************************/
  12320. SOAP_FMAC1
  12321. int
  12322. SOAP_FMAC2
  12323. soap_element_result(struct soap *soap, const char *tag) {
  12324. if (soap->version == 2 && soap->encodingStyle) {
  12325. if (soap_element(soap, "SOAP-RPC:result", 0, NULL)
  12326. || soap_attribute(soap, "xmlns:SOAP-RPC", soap_rpc)
  12327. || soap_element_start_end_out(soap, NULL)
  12328. || soap_string_out(soap, tag, 0)
  12329. || soap_element_end_out(soap, "SOAP-RPC:result"))
  12330. return soap->error;
  12331. }
  12332. return SOAP_OK;
  12333. }
  12334. /******************************************************************************/
  12335. SOAP_FMAC1
  12336. void
  12337. SOAP_FMAC2
  12338. soap_check_result(struct soap *soap, const char *tag) {
  12339. (void) tag;
  12340. if (soap->version == 2 && soap->encodingStyle) {
  12341. soap_instring(soap, ":result", NULL, NULL, 0, 2, -1, -1, NULL);
  12342. /* just ignore content for compliance reasons, but should compare tag to element's QName value? */
  12343. }
  12344. }
  12345. /******************************************************************************/
  12346. SOAP_FMAC1
  12347. int
  12348. SOAP_FMAC2
  12349. soap_attribute(struct soap *soap, const char *name, const char *value) {
  12350. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Attribute '%s'='%s'\n", name, value));
  12351. #ifdef WITH_DOM
  12352. if ((soap->mode & SOAP_XML_DOM) && !(soap->mode & SOAP_XML_CANONICAL) && soap->dom)
  12353. {
  12354. struct soap_dom_attribute *a = (struct soap_dom_attribute*)soap_malloc(soap, sizeof(struct soap_dom_attribute));
  12355. if (!a)
  12356. return soap->error;
  12357. a->next = soap->dom->atts;
  12358. a->nstr = NULL;
  12359. a->name = soap_strdup(soap, name);
  12360. a->text = soap_strdup(soap, value);
  12361. a->soap = soap;
  12362. soap->dom->atts = a;
  12363. if (!a->name || (value && !a->text))
  12364. return soap->error = SOAP_EOM;
  12365. return SOAP_OK;
  12366. }
  12367. #endif
  12368. #ifndef WITH_LEAN
  12369. if ((soap->mode & SOAP_XML_CANONICAL)) {
  12370. /* push namespace */
  12371. if (!strncmp(name, "xmlns", 5) && ((name[5] == ':') || name[5] == '\0')) {
  12372. if (name[5] == ':' && soap->c14ninclude &&
  12373. ((*soap->c14ninclude == '*' || soap_tagsearch(soap->c14ninclude, name + 6))))
  12374. soap_utilize_ns(soap, name, 0);
  12375. (void) soap_push_ns(soap, name + 5 + (name[5] == ':'), value, 0, 0);
  12376. } else {
  12377. soap->level--;
  12378. if (soap_set_attr(soap, name, value, 1))
  12379. return soap->error;
  12380. soap->level++;
  12381. }
  12382. } else
  12383. #endif
  12384. {
  12385. if (soap_send_raw(soap, " ", 1)
  12386. || soap_send(soap, name))
  12387. return soap->error;
  12388. if (value)
  12389. if (soap_send_raw(soap, "=\"", 2)
  12390. || soap_string_out(soap, value, 1)
  12391. || soap_send_raw(soap, "\"", 1))
  12392. return soap->error;
  12393. }
  12394. return SOAP_OK;
  12395. }
  12396. /******************************************************************************/
  12397. SOAP_FMAC1
  12398. int
  12399. SOAP_FMAC2
  12400. soap_element_begin_in(struct soap *soap, const char *tag, int nillable, const char *type) {
  12401. if (!soap_peek_element(soap)) {
  12402. if (soap->other)
  12403. return soap->error = SOAP_TAG_MISMATCH;
  12404. if (tag && *tag == '-')
  12405. return SOAP_OK;
  12406. soap->error = soap_match_tag(soap, soap->tag, tag);
  12407. if (!soap->error) {
  12408. if (type && *soap->type && soap_match_tag(soap, soap->type, type))
  12409. return soap->error = SOAP_TYPE;
  12410. soap->peeked = 0;
  12411. if (!nillable && soap->null && (soap->mode & SOAP_XML_STRICT))
  12412. return soap->error = SOAP_NULL;
  12413. if (soap->body) {
  12414. soap->level++;
  12415. if (soap->level > soap->maxlevel)
  12416. return soap->error = SOAP_LEVEL;
  12417. }
  12418. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Begin tag found (level=%u) '%s'='%s'\n", soap->level, soap->tag,
  12419. tag ? tag : SOAP_STR_EOS));
  12420. soap->error = SOAP_OK;
  12421. }
  12422. } else if (soap->error == SOAP_NO_TAG && tag && *tag == '-') {
  12423. soap->error = SOAP_OK;
  12424. }
  12425. return soap->error;
  12426. }
  12427. /******************************************************************************/
  12428. SOAP_FMAC1
  12429. int
  12430. SOAP_FMAC2
  12431. soap_element_end_in(struct soap *soap, const char *tag) {
  12432. soap_wchar c;
  12433. char *s = NULL;
  12434. int n = 0;
  12435. if (tag && *tag == '-')
  12436. return SOAP_OK;
  12437. if (soap->error == SOAP_NO_TAG)
  12438. soap->error = SOAP_OK;
  12439. #ifdef WITH_DOM
  12440. /* this whitespace or mixed content is significant for DOM "as-is" */
  12441. if ((soap->mode & SOAP_XML_DOM) && soap->dom)
  12442. {
  12443. const char *t = soap->dom->code; /* save XML code */
  12444. s = soap_string_in(soap, -1, -1, -1, NULL);
  12445. if (!soap->peeked && !s)
  12446. return soap->error;
  12447. if (soap->dom->prnt)
  12448. soap->dom = soap->dom->prnt;
  12449. if (s && (soap->mode & SOAP_XML_STRICT))
  12450. {
  12451. for (; *s; s++)
  12452. if (!soap_coblank((soap_wchar)*s))
  12453. return soap->error = SOAP_END_TAG; /* reject mixed content before ending tag */
  12454. }
  12455. soap->dom->code = t; /* restore XML code */
  12456. }
  12457. #endif
  12458. if (soap->peeked) {
  12459. if (*soap->tag)
  12460. n++;
  12461. soap->peeked = 0;
  12462. }
  12463. do {
  12464. while (((c = soap_get(soap)) != SOAP_TT)) {
  12465. if ((int) c == EOF)
  12466. return soap->error = SOAP_CHK_EOF;
  12467. if (!soap_coblank(c)) {
  12468. if ((soap->mode & SOAP_XML_STRICT))
  12469. return soap->error = SOAP_END_TAG; /* reject mixed content before ending tag */
  12470. if (c == SOAP_LT)
  12471. n++;
  12472. else if (c == '/') {
  12473. c = soap_get(soap);
  12474. if (c == SOAP_GT && n > 0)
  12475. n--;
  12476. else
  12477. soap_unget(soap, c);
  12478. }
  12479. }
  12480. }
  12481. } while (n-- > 0);
  12482. s = soap->tag;
  12483. n = sizeof(soap->tag);
  12484. while ((c = soap_get(soap)) > 32) {
  12485. if (n > 1) {
  12486. *s++ = (char) c;
  12487. n--;
  12488. }
  12489. }
  12490. *s = '\0';
  12491. if ((int) c == EOF)
  12492. return soap->error = SOAP_CHK_EOF;
  12493. while (soap_coblank(c))
  12494. c = soap_get(soap);
  12495. if (c != SOAP_GT)
  12496. return soap->error = SOAP_SYNTAX_ERROR;
  12497. #ifndef WITH_LEAN
  12498. #ifdef WITH_DOM
  12499. if (soap->feltendin)
  12500. {
  12501. int err = soap->error;
  12502. soap->error = soap->feltendin(soap, soap->tag, tag);
  12503. if (soap->error)
  12504. return soap->error;
  12505. if (err)
  12506. return soap->error = err; /* restore error */
  12507. }
  12508. #endif
  12509. #endif
  12510. if (tag && (soap->mode & SOAP_XML_STRICT)) {
  12511. soap_pop_namespace(soap);
  12512. if (soap_match_tag(soap, soap->tag, tag)) {
  12513. DBGLOG(TEST,
  12514. SOAP_MESSAGE(fdebug, "End tag '%s' does not match '%s'\n", soap->tag, tag ? tag : SOAP_STR_EOS));
  12515. return soap->error = SOAP_SYNTAX_ERROR;
  12516. }
  12517. }
  12518. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "End tag found (level=%u) '%s'='%s'\n", soap->level, soap->tag,
  12519. tag ? tag : SOAP_STR_EOS));
  12520. soap->level--;
  12521. return SOAP_OK;
  12522. }
  12523. /******************************************************************************/
  12524. SOAP_FMAC1
  12525. const char *
  12526. SOAP_FMAC2
  12527. soap_attr_value(struct soap *soap, const char *name, int flag, int occurs) {
  12528. struct soap_attribute *tp;
  12529. if (*name == '-')
  12530. return SOAP_STR_EOS;
  12531. for (tp = soap->attributes; tp; tp = tp->next) {
  12532. if (tp->visible == 2 && !soap_match_att(soap, tp->name, name))
  12533. break;
  12534. }
  12535. if (tp) {
  12536. if (occurs == 4 || (occurs == 2 && (soap->mode & SOAP_XML_STRICT)))
  12537. soap->error = SOAP_PROHIBITED;
  12538. else if (flag >= 4)
  12539. return soap_collapse(soap, tp->value, flag, 1);
  12540. else
  12541. return tp->value;
  12542. } else if (occurs == 3 || (occurs == 1 && (soap->mode & SOAP_XML_STRICT))) {
  12543. soap->error = SOAP_REQUIRED;
  12544. } else {
  12545. soap->error = SOAP_OK;
  12546. }
  12547. return NULL;
  12548. }
  12549. /******************************************************************************/
  12550. SOAP_FMAC1
  12551. int
  12552. SOAP_FMAC2
  12553. soap_set_attr(struct soap *soap, const char *name, const char *value, int flag) {
  12554. struct soap_attribute *tp, *tq;
  12555. if (*name == '-')
  12556. return SOAP_OK;
  12557. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Set attribute %s='%s'\n", name, value ? value : SOAP_STR_EOS));
  12558. tq = NULL;
  12559. for (tp = soap->attributes; tp; tq = tp, tp = tp->next) {
  12560. if (!strcmp(tp->name, name))
  12561. break;
  12562. }
  12563. if (!tp) {
  12564. size_t l = strlen(name);
  12565. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Allocate attribute %s\n", name));
  12566. if (sizeof(struct soap_attribute) + l > l &&
  12567. (SOAP_MAXALLOCSIZE <= 0 || sizeof(struct soap_attribute) + l <= SOAP_MAXALLOCSIZE))
  12568. tp = (struct soap_attribute *) SOAP_MALLOC(soap, sizeof(struct soap_attribute) + l);
  12569. if (!tp)
  12570. return soap->error = SOAP_EOM;
  12571. tp->ns = NULL;
  12572. #ifndef WITH_LEAN
  12573. if ((soap->mode & SOAP_XML_CANONICAL)) {
  12574. struct soap_attribute **tpp = &soap->attributes;
  12575. const char *s = strchr(name, ':');
  12576. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Inserting attribute %s for c14n\n", name));
  12577. if (!strncmp(name, "xmlns", 5)) {
  12578. for (; *tpp; tpp = &(*tpp)->next)
  12579. if (strncmp((*tpp)->name, "xmlns", 5) || strcmp((*tpp)->name + 5, name + 5) > 0)
  12580. break;
  12581. } else if (!s) {
  12582. for (; *tpp; tpp = &(*tpp)->next)
  12583. if (strncmp((*tpp)->name, "xmlns", 5) && ((*tpp)->ns || strcmp((*tpp)->name, name) > 0))
  12584. break;
  12585. } else {
  12586. struct soap_nlist *np = soap_lookup_ns(soap, name, s - name);
  12587. if (np) {
  12588. tp->ns = np->ns;
  12589. } else {
  12590. struct soap_attribute *tq;
  12591. for (tq = soap->attributes; tq; tq = tq->next) {
  12592. if (!strncmp(tq->name, "xmlns:", 6) && !strncmp(tq->name + 6, name, s - name) &&
  12593. !tq->name[6 + s - name]) {
  12594. tp->ns = tq->ns;
  12595. break;
  12596. }
  12597. }
  12598. }
  12599. for (; *tpp; tpp = &(*tpp)->next) {
  12600. int k;
  12601. if (strncmp((*tpp)->name, "xmlns", 5) && (*tpp)->ns && tp->ns &&
  12602. ((k = strcmp((*tpp)->ns, tp->ns)) > 0 || (!k && strcmp((*tpp)->name, name) > 0)))
  12603. break;
  12604. }
  12605. }
  12606. tp->next = *tpp;
  12607. *tpp = tp;
  12608. } else
  12609. #endif
  12610. if (tq) {
  12611. tq->next = tp;
  12612. tp->next = NULL;
  12613. } else {
  12614. tp->next = soap->attributes;
  12615. soap->attributes = tp;
  12616. }
  12617. soap_strcpy((char *) tp->name, l + 1, name);
  12618. tp->value = NULL;
  12619. } else if (tp->visible) {
  12620. return SOAP_OK;
  12621. } else if (value && tp->value && tp->size <= strlen(value)) {
  12622. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Free attribute value of %s (free %p)\n", name, (void *) tp->value));
  12623. SOAP_FREE(soap, tp->value);
  12624. tp->value = NULL;
  12625. tp->ns = NULL;
  12626. }
  12627. if (value) {
  12628. if (!tp->value) {
  12629. tp->size = strlen(value) + 1;
  12630. if (SOAP_MAXALLOCSIZE <= 0 || tp->size <= SOAP_MAXALLOCSIZE)
  12631. tp->value = (char *) SOAP_MALLOC(soap, tp->size);
  12632. if (!tp->value)
  12633. return soap->error = SOAP_EOM;
  12634. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Allocate attribute value for %s (%p)\n", tp->name, (void *) tp->value));
  12635. }
  12636. soap_strcpy(tp->value, tp->size, value);
  12637. if (!strncmp(tp->name, "xmlns:", 6))
  12638. tp->ns = tp->value;
  12639. tp->visible = 2;
  12640. tp->flag = (short) flag;
  12641. #ifndef WITH_LEAN
  12642. if (!strcmp(name, "wsu:Id")) {
  12643. soap->event = SOAP_SEC_BEGIN;
  12644. soap_strcpy(soap->id, sizeof(soap->id), value);
  12645. }
  12646. if ((soap->mode & SOAP_XML_CANONICAL) && !(soap->mode & SOAP_XML_CANONICAL_NA)) {
  12647. const char *s = strchr(name, ':');
  12648. if (s && strchr(value, ':')) {
  12649. struct soap_nlist *np = soap_lookup_ns(soap, name, s - name);
  12650. if (np && np->ns && soap->local_namespaces) {
  12651. if ((!strcmp(s + 1, "type") && !strcmp(np->ns, soap->local_namespaces[2].ns)) /* xsi:type QName */
  12652. || ((!strcmp(s + 1, "arrayType") || !strcmp(s + 1, "itemType")) && !strcmp(np->ns,
  12653. soap->local_namespaces[1].ns))) /* SOAP-ENC:arrayType and SOAP-ENC:itemType QName */
  12654. soap_utilize_ns(soap, value, 1);
  12655. }
  12656. }
  12657. }
  12658. #endif
  12659. } else {
  12660. tp->visible = 1;
  12661. }
  12662. return SOAP_OK;
  12663. }
  12664. /******************************************************************************/
  12665. SOAP_FMAC1
  12666. void
  12667. SOAP_FMAC2
  12668. soap_clr_attr(struct soap *soap) {
  12669. struct soap_attribute *tp;
  12670. #ifndef WITH_LEAN
  12671. if ((soap->mode & SOAP_XML_CANONICAL)) {
  12672. while (soap->attributes) {
  12673. tp = soap->attributes->next;
  12674. if (soap->attributes->value)
  12675. SOAP_FREE(soap, soap->attributes->value);
  12676. SOAP_FREE(soap, soap->attributes);
  12677. soap->attributes = tp;
  12678. }
  12679. } else
  12680. #endif
  12681. {
  12682. for (tp = soap->attributes; tp; tp = tp->next)
  12683. tp->visible = 0;
  12684. }
  12685. }
  12686. /******************************************************************************/
  12687. static int
  12688. soap_getattrval(struct soap *soap, char *s, size_t *n, soap_wchar d) {
  12689. char buf[8];
  12690. size_t i;
  12691. size_t k = *n;
  12692. size_t m = 0;
  12693. char *t = buf;
  12694. for (i = 0; i < k; i++) {
  12695. soap_wchar c;
  12696. if (m) {
  12697. *s++ = *t++;
  12698. m--;
  12699. continue;
  12700. }
  12701. if ((soap->mode & SOAP_C_UTFSTRING)) {
  12702. c = soap_get(soap);
  12703. if ((c & 0x80000000) && c >= -0x7FFFFF80 && c < SOAP_AP) {
  12704. t = buf;
  12705. c &= 0x7FFFFFFF;
  12706. if (c < 0x0800)
  12707. *t++ = (char) (0xC0 | ((c >> 6) & 0x1F));
  12708. else {
  12709. #ifdef WITH_REPLACE_ILLEGAL_UTF8
  12710. if (!((c >= 0x80 && c <= 0xD7FF) || (c >= 0xE000 && c <= 0xFFFD) || (c >= 0x10000 && c <= 0x10FFFF)))
  12711. c = SOAP_UNKNOWN_UNICODE_CHAR;
  12712. #endif
  12713. if (c < 0x010000) {
  12714. *t++ = (char) (0xE0 | ((c >> 12) & 0x0F));
  12715. } else {
  12716. if (c < 0x200000) {
  12717. *t++ = (char) (0xF0 | ((c >> 18) & 0x07));
  12718. } else {
  12719. if (c < 0x04000000) {
  12720. *t++ = (char) (0xF8 | ((c >> 24) & 0x03));
  12721. } else {
  12722. *t++ = (char) (0xFC | ((c >> 30) & 0x01));
  12723. *t++ = (char) (0x80 | ((c >> 24) & 0x3F));
  12724. }
  12725. *t++ = (char) (0x80 | ((c >> 18) & 0x3F));
  12726. }
  12727. *t++ = (char) (0x80 | ((c >> 12) & 0x3F));
  12728. }
  12729. *t++ = (char) (0x80 | ((c >> 6) & 0x3F));
  12730. }
  12731. *t++ = (char) (0x80 | (c & 0x3F));
  12732. m = t - buf - 1;
  12733. if (i + m >= k) {
  12734. soap_unget(soap, c | 0x80000000);
  12735. *n = i;
  12736. return soap->error = SOAP_EOM;
  12737. }
  12738. t = buf;
  12739. *s++ = *t++;
  12740. continue;
  12741. }
  12742. } else {
  12743. c = soap_getutf8(soap);
  12744. }
  12745. switch (c) {
  12746. case SOAP_TT:
  12747. *s++ = '<';
  12748. soap_unget(soap, '/');
  12749. break;
  12750. case SOAP_LT:
  12751. *s++ = '<';
  12752. break;
  12753. case SOAP_GT:
  12754. if (d == ' ') {
  12755. soap_unget(soap, c);
  12756. *s = '\0';
  12757. *n = i + 1;
  12758. return SOAP_OK;
  12759. }
  12760. *s++ = '>';
  12761. break;
  12762. case SOAP_QT:
  12763. if (c == d) {
  12764. *s = '\0';
  12765. *n = i + 1;
  12766. return SOAP_OK;
  12767. }
  12768. *s++ = '"';
  12769. break;
  12770. case SOAP_AP:
  12771. if (c == d) {
  12772. *s = '\0';
  12773. *n = i + 1;
  12774. return SOAP_OK;
  12775. }
  12776. *s++ = '\'';
  12777. break;
  12778. case '\t':
  12779. case '\n':
  12780. case '\r':
  12781. case ' ':
  12782. case '/':
  12783. if (d == ' ') {
  12784. soap_unget(soap, c);
  12785. *s = '\0';
  12786. *n = i + 1;
  12787. return SOAP_OK;
  12788. }
  12789. *s++ = (char) c;
  12790. break;
  12791. default:
  12792. if ((int) c == EOF) {
  12793. *s = '\0';
  12794. *n = i + 1;
  12795. return soap->error = SOAP_CHK_EOF;
  12796. }
  12797. *s++ = (char) c;
  12798. }
  12799. }
  12800. return soap->error = SOAP_EOM;
  12801. }
  12802. /******************************************************************************/
  12803. #ifdef WITH_FAST
  12804. SOAP_FMAC1
  12805. int
  12806. SOAP_FMAC2
  12807. soap_store_lab(struct soap *soap, const char *s, size_t n) {
  12808. soap->labidx = 0;
  12809. return soap_append_lab(soap, s, n);
  12810. }
  12811. #endif
  12812. /******************************************************************************/
  12813. #ifdef WITH_FAST
  12814. SOAP_FMAC1
  12815. int
  12816. SOAP_FMAC2
  12817. soap_append_lab(struct soap *soap, const char *s, size_t n) {
  12818. if (soap->labidx + n < soap->labidx)
  12819. return soap->error = SOAP_EOM;
  12820. if (soap->labidx + n >= soap->lablen) {
  12821. char *t = soap->labbuf;
  12822. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Enlarging look-aside buffer to append data, size=%lu\n",
  12823. (unsigned long) soap->lablen));
  12824. if (soap->lablen == 0)
  12825. soap->lablen = SOAP_LABLEN;
  12826. while (soap->labidx + n >= soap->lablen) {
  12827. if (soap->lablen << 1 < soap->lablen)
  12828. return soap->error = SOAP_EOM;
  12829. soap->lablen <<= 1;
  12830. }
  12831. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "New look-aside buffer size=%lu\n", (unsigned long) soap->lablen));
  12832. if (SOAP_MAXALLOCSIZE > 0 && soap->lablen > SOAP_MAXALLOCSIZE)
  12833. return soap->error = SOAP_EOM;
  12834. soap->labbuf = (char *) SOAP_MALLOC(soap, soap->lablen);
  12835. if (!soap->labbuf) {
  12836. if (t)
  12837. SOAP_FREE(soap, t);
  12838. return soap->error = SOAP_EOM;
  12839. }
  12840. if (t) {
  12841. (void) soap_memcpy((void *) soap->labbuf, soap->lablen, (const void *) t, soap->labidx);
  12842. SOAP_FREE(soap, t);
  12843. }
  12844. }
  12845. if (s) {
  12846. (void) soap_memcpy((void *) (soap->labbuf + soap->labidx), soap->lablen - soap->labidx, (const void *) s, n);
  12847. soap->labidx += n;
  12848. }
  12849. return SOAP_OK;
  12850. }
  12851. #endif
  12852. /******************************************************************************/
  12853. SOAP_FMAC1
  12854. int
  12855. SOAP_FMAC2
  12856. soap_peek_element(struct soap *soap) {
  12857. #ifdef WITH_DOM
  12858. struct soap_dom_attribute **att = NULL;
  12859. char *lead = NULL;
  12860. #endif
  12861. struct soap_attribute *tp, *tq = NULL;
  12862. const char *t;
  12863. char *s;
  12864. soap_wchar c;
  12865. int i;
  12866. if (soap->peeked) {
  12867. if (!*soap->tag)
  12868. return soap->error = SOAP_NO_TAG;
  12869. return SOAP_OK;
  12870. }
  12871. soap->peeked = 1;
  12872. soap->id[0] = '\0';
  12873. soap->href[0] = '\0';
  12874. soap->type[0] = '\0';
  12875. soap->arrayType[0] = '\0';
  12876. soap->arraySize[0] = '\0';
  12877. soap->arrayOffset[0] = '\0';
  12878. soap->other = 0;
  12879. soap->root = -1;
  12880. soap->position = 0;
  12881. soap->null = 0;
  12882. soap->mustUnderstand = 0;
  12883. /* UTF-8 BOM? */
  12884. c = soap_getchar(soap);
  12885. if (c == 0xEF && soap_get0(soap) == 0xBB) {
  12886. soap_get1(soap);
  12887. c = soap_get1(soap);
  12888. if (c == 0xBF)
  12889. soap->mode &= ~SOAP_ENC_LATIN;
  12890. else
  12891. soap_unget(soap, (0x0F << 12) | (0xBB << 6) | (c & 0x3F)); /* UTF-8 */
  12892. } else if ((c == 0xFE && soap_get0(soap) == 0xFF) /* UTF-16 BE */
  12893. || (c == 0xFF && soap_get0(soap) == 0xFE)) /* UTF-16 LE */
  12894. {
  12895. return soap->error = SOAP_UTF_ERROR;
  12896. } else {
  12897. soap_unget(soap, c);
  12898. }
  12899. c = soap_get(soap);
  12900. #ifdef WITH_DOM
  12901. /* whitespace leading up to the start tag is significant for DOM as-is (but comments and PIs are removed from this lead) */
  12902. if (soap_coblank(c))
  12903. {
  12904. soap->labidx = 0;
  12905. do
  12906. {
  12907. if (soap_append_lab(soap, NULL, 0))
  12908. return soap->error;
  12909. s = soap->labbuf + soap->labidx;
  12910. i = soap->lablen - soap->labidx;
  12911. soap->labidx = soap->lablen;
  12912. while (soap_coblank(c) && i-- > 0)
  12913. {
  12914. *s++ = c;
  12915. c = soap_get(soap);
  12916. }
  12917. } while (soap_coblank(c) || i == 0);
  12918. *s = '\0';
  12919. lead = soap->labbuf;
  12920. }
  12921. #else
  12922. /* skip space */
  12923. while (soap_coblank(c)) {
  12924. c = soap_get(soap);
  12925. }
  12926. #endif
  12927. if (c != SOAP_LT) {
  12928. *soap->tag = '\0';
  12929. if ((int) c == EOF)
  12930. return soap->error = SOAP_CHK_EOF;
  12931. soap_unget(soap, c);
  12932. #ifdef WITH_DOM
  12933. /* whitespace leading up to the end tag is significant for DOM as-is */
  12934. if ((soap->mode & SOAP_XML_DOM) && soap->dom)
  12935. {
  12936. if (lead && *lead)
  12937. soap->dom->tail = soap_strdup(soap, lead);
  12938. else
  12939. soap->dom->tail = SOAP_STR_EOS; /* body with closing tag instead of <tag/> */
  12940. }
  12941. #endif
  12942. return soap->error = SOAP_NO_TAG;
  12943. }
  12944. do {
  12945. c = soap_get1(soap);
  12946. } while (soap_coblank(c));
  12947. s = soap->tag;
  12948. i = sizeof(soap->tag);
  12949. while (c != '>' && c != '/' && c > 32 && (int) c != EOF) {
  12950. if (i > 1) {
  12951. *s++ = (char) c;
  12952. i--;
  12953. }
  12954. c = soap_get1(soap);
  12955. }
  12956. *s = '\0';
  12957. while (soap_coblank(c))
  12958. c = soap_get1(soap);
  12959. #ifdef WITH_DOM
  12960. if ((soap->mode & SOAP_XML_DOM))
  12961. {
  12962. struct soap_dom_element *elt;
  12963. elt = (struct soap_dom_element*)soap_malloc(soap, sizeof(struct soap_dom_element));
  12964. if (!elt)
  12965. return soap->error;
  12966. elt->next = NULL;
  12967. elt->prnt = soap->dom;
  12968. elt->elts = NULL;
  12969. elt->atts = NULL;
  12970. elt->nstr = NULL;
  12971. elt->name = soap_strdup(soap, soap->tag);
  12972. elt->text = NULL;
  12973. elt->code = NULL;
  12974. elt->tail = NULL;
  12975. elt->node = NULL;
  12976. elt->type = 0;
  12977. if (lead && *lead)
  12978. elt->lead = soap_strdup(soap, lead);
  12979. else
  12980. elt->lead = NULL;
  12981. elt->soap = soap;
  12982. if (soap->dom)
  12983. {
  12984. struct soap_dom_element *p = soap->dom->elts;
  12985. if (p)
  12986. {
  12987. while (p->next)
  12988. p = p->next;
  12989. p->next = elt;
  12990. }
  12991. else
  12992. {
  12993. soap->dom->elts = elt;
  12994. }
  12995. }
  12996. soap->dom = elt;
  12997. att = &elt->atts;
  12998. if (!elt->name)
  12999. return soap->error = SOAP_EOM;
  13000. }
  13001. #endif
  13002. soap_pop_namespace(soap);
  13003. for (tp = soap->attributes; tp; tp = tp->next)
  13004. tp->visible = 0;
  13005. while ((int) c != EOF && c != '>' && c != '/') {
  13006. s = soap->tmpbuf;
  13007. i = sizeof(soap->tmpbuf);
  13008. while (c != '=' && c != '>' && c != '/' && c > 32 && (int) c != EOF) {
  13009. if (i > 1) {
  13010. *s++ = (char) c;
  13011. i--;
  13012. }
  13013. c = soap_get1(soap);
  13014. }
  13015. *s = '\0';
  13016. if (i == sizeof(soap->tmpbuf))
  13017. return soap->error = SOAP_SYNTAX_ERROR;
  13018. #ifdef WITH_DOM
  13019. /* add attribute name to dom */
  13020. if (att)
  13021. {
  13022. *att = (struct soap_dom_attribute*)soap_malloc(soap, sizeof(struct soap_dom_attribute));
  13023. if (!*att)
  13024. return soap->error;
  13025. (*att)->next = NULL;
  13026. (*att)->nstr = NULL;
  13027. (*att)->name = soap_strdup(soap, soap->tmpbuf);
  13028. (*att)->text = NULL;
  13029. (*att)->soap = soap;
  13030. if (!(*att)->name)
  13031. return soap->error = SOAP_EOM;
  13032. }
  13033. #endif
  13034. if (!strncmp(soap->tmpbuf, "xmlns", 5)) {
  13035. if (soap->tmpbuf[5] == ':')
  13036. t = soap->tmpbuf + 6;
  13037. else if (soap->tmpbuf[5])
  13038. t = NULL;
  13039. else
  13040. t = SOAP_STR_EOS;
  13041. } else {
  13042. t = NULL;
  13043. }
  13044. tq = NULL;
  13045. for (tp = soap->attributes; tp; tq = tp, tp = tp->next) {
  13046. if (!SOAP_STRCMP(tp->name, soap->tmpbuf))
  13047. break;
  13048. }
  13049. if (!tp) {
  13050. size_t l = strlen(soap->tmpbuf);
  13051. tp = (struct soap_attribute *) SOAP_MALLOC(soap, sizeof(struct soap_attribute) + l);
  13052. if (!tp)
  13053. return soap->error = SOAP_EOM;
  13054. (void) soap_memcpy((char *) tp->name, l + 1, soap->tmpbuf, l + 1);
  13055. tp->value = NULL;
  13056. tp->size = 0;
  13057. tp->ns = NULL;
  13058. tp->visible = 0;
  13059. /* append attribute to the end of the list */
  13060. if (tq) {
  13061. tq->next = tp;
  13062. tp->next = NULL;
  13063. } else {
  13064. tp->next = soap->attributes;
  13065. soap->attributes = tp;
  13066. }
  13067. }
  13068. while (soap_coblank(c))
  13069. c = soap_get1(soap);
  13070. if (c == '=') {
  13071. size_t k;
  13072. do {
  13073. c = soap_getutf8(soap);
  13074. } while (soap_coblank(c));
  13075. if (c != SOAP_QT && c != SOAP_AP) {
  13076. soap_unget(soap, c);
  13077. c = ' '; /* blank delimiter */
  13078. }
  13079. k = tp->size;
  13080. if (soap_getattrval(soap, tp->value, &k, c)) {
  13081. #ifdef WITH_FAST
  13082. if (soap->error != SOAP_EOM)
  13083. return soap->error;
  13084. soap->error = SOAP_OK;
  13085. if (soap_store_lab(soap, tp->value, k))
  13086. return soap->error;
  13087. if (tp->value)
  13088. SOAP_FREE(soap, tp->value);
  13089. tp->value = NULL;
  13090. for (;;) {
  13091. k = soap->lablen - soap->labidx;
  13092. if (soap_getattrval(soap, soap->labbuf + soap->labidx, &k, c)) {
  13093. if (soap->error != SOAP_EOM)
  13094. return soap->error;
  13095. soap->error = SOAP_OK;
  13096. soap->labidx = soap->lablen;
  13097. if (soap_append_lab(soap, NULL, 0))
  13098. return soap->error;
  13099. } else {
  13100. break;
  13101. }
  13102. }
  13103. if (soap->labidx) {
  13104. tp->size = soap->lablen;
  13105. } else {
  13106. tp->size = strlen(soap->labbuf) + 1;
  13107. if (tp->size < SOAP_LABLEN)
  13108. tp->size = SOAP_LABLEN;
  13109. }
  13110. tp->value = (char *) SOAP_MALLOC(soap, tp->size);
  13111. if (!tp->value)
  13112. return soap->error = SOAP_EOM;
  13113. soap_strcpy(tp->value, tp->size, soap->labbuf);
  13114. #else
  13115. tp->size = k;
  13116. if (soap->error != SOAP_EOM)
  13117. return soap->error;
  13118. soap->error = SOAP_OK;
  13119. if (soap_alloc_block(soap) == NULL)
  13120. return soap->error;
  13121. for (;;)
  13122. {
  13123. s = (char*)soap_push_block(soap, NULL, SOAP_BLKLEN);
  13124. if (!s)
  13125. return soap->error;
  13126. k = SOAP_BLKLEN;
  13127. if (soap_getattrval(soap, s, &k, c))
  13128. {
  13129. if (soap->error != SOAP_EOM)
  13130. return soap->error;
  13131. soap->error = SOAP_OK;
  13132. soap_size_block(soap, NULL, k);
  13133. }
  13134. else
  13135. {
  13136. break;
  13137. }
  13138. }
  13139. k = tp->size + soap->blist->size;
  13140. if (SOAP_MAXALLOCSIZE > 0 && k > SOAP_MAXALLOCSIZE)
  13141. return soap->error = SOAP_EOM;
  13142. s = (char*)SOAP_MALLOC(soap, k);
  13143. if (!s)
  13144. return soap->error = SOAP_EOM;
  13145. if (tp->value)
  13146. {
  13147. (void)soap_memcpy((void*)s, k, (const void*)tp->value, tp->size);
  13148. SOAP_FREE(soap, tp->value);
  13149. }
  13150. soap_save_block(soap, NULL, s + tp->size, 0);
  13151. tp->value = s;
  13152. tp->size = k;
  13153. #endif
  13154. }
  13155. if (tp->visible) {
  13156. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Duplicate attribute in %s\n", soap->tag));
  13157. return soap->error = SOAP_SYNTAX_ERROR; /* redefined (duplicate) attribute */
  13158. }
  13159. tp->visible = 2; /* seen this attribute w/ value */
  13160. do {
  13161. c = soap_get1(soap);
  13162. } while (soap_coblank(c));
  13163. #ifdef WITH_DOM
  13164. if (att && tp->value)
  13165. {
  13166. (*att)->text = soap_strdup(soap, tp->value);
  13167. if (!(*att)->text)
  13168. return soap->error = SOAP_EOM;
  13169. }
  13170. #endif
  13171. } else {
  13172. tp->visible = 1; /* seen this attribute w/o value */
  13173. }
  13174. #ifdef WITH_DOM
  13175. if (att)
  13176. att = &(*att)->next;
  13177. #endif
  13178. if (t && tp->value) {
  13179. if (soap_push_namespace(soap, t, tp->value) == NULL)
  13180. return soap->error;
  13181. }
  13182. }
  13183. #ifdef WITH_DOM
  13184. if (att)
  13185. {
  13186. soap->dom->nstr = soap_current_namespace_tag(soap, soap->tag);
  13187. for (att = &soap->dom->atts; *att; att = &(*att)->next)
  13188. (*att)->nstr = soap_current_namespace_att(soap, (*att)->name);
  13189. }
  13190. #endif
  13191. if ((int) c == EOF)
  13192. return soap->error = SOAP_CHK_EOF;
  13193. soap->body = (c != '/');
  13194. if (!soap->body) {
  13195. do {
  13196. c = soap_get1(soap);
  13197. } while (soap_coblank(c));
  13198. }
  13199. #ifdef WITH_DOM
  13200. if ((soap->mode & SOAP_XML_DOM))
  13201. {
  13202. if (!soap->body && soap->dom->prnt)
  13203. soap->dom = soap->dom->prnt;
  13204. }
  13205. #endif
  13206. for (tp = soap->attributes; tp; tp = tp->next) {
  13207. if (tp->visible && tp->value) {
  13208. #ifndef WITH_NOIDREF
  13209. if (!strcmp(tp->name, "id")) {
  13210. if ((soap->version > 0 && !(soap->imode & SOAP_XML_TREE))
  13211. || (soap->mode & SOAP_XML_GRAPH)) {
  13212. *soap->id = '#';
  13213. soap_strcpy(soap->id + 1, sizeof(soap->id) - 1, tp->value);
  13214. }
  13215. } else if (!strcmp(tp->name, "href")) {
  13216. if ((soap->version == 1 && !(soap->imode & SOAP_XML_TREE))
  13217. || (soap->mode & SOAP_XML_GRAPH)
  13218. || ((soap->mode & (SOAP_ENC_MTOM | SOAP_ENC_DIME)) && *tp->value != '#'))
  13219. soap_strcpy(soap->href, sizeof(soap->href), tp->value);
  13220. } else if (!strcmp(tp->name, "ref")) {
  13221. if ((soap->version == 2 && !(soap->imode & SOAP_XML_TREE))
  13222. || (soap->mode & SOAP_XML_GRAPH)) {
  13223. *soap->href = '#';
  13224. soap_strcpy(soap->href + (*tp->value != '#'), sizeof(soap->href) - 1, tp->value);
  13225. }
  13226. }
  13227. #else
  13228. if (!strcmp(tp->name, "href"))
  13229. {
  13230. if ((soap->mode & (SOAP_ENC_MTOM | SOAP_ENC_DIME)) && *tp->value != '#')
  13231. soap_strcpy(soap->href, sizeof(soap->href), tp->value);
  13232. }
  13233. #endif
  13234. else if (!soap_match_tag(soap, tp->name, "xsi:type")) {
  13235. soap_strcpy(soap->type, sizeof(soap->type), tp->value);
  13236. } else if ((!soap_match_tag(soap, tp->name, "xsi:null")
  13237. || !soap_match_tag(soap, tp->name, "xsi:nil"))
  13238. && (!strcmp(tp->value, "1")
  13239. || !strcmp(tp->value, "true"))) {
  13240. soap->null = 1;
  13241. } else if (!soap_match_tag(soap, tp->name, "SOAP-ENV:encodingStyle")) {
  13242. if (!soap->encodingStyle)
  13243. soap->encodingStyle = SOAP_STR_EOS;
  13244. soap_version(soap);
  13245. } else if (soap->version == 1) {
  13246. if (!soap_match_tag(soap, tp->name, "SOAP-ENC:arrayType")) {
  13247. s = soap_strrchr(tp->value, '[');
  13248. if (s && (size_t) (s - tp->value) < sizeof(soap->arrayType)) {
  13249. (void) soap_strncpy(soap->arrayType, sizeof(soap->arrayType), tp->value, s - tp->value);
  13250. soap_strcpy(soap->arraySize, sizeof(soap->arraySize), s);
  13251. } else
  13252. soap_strcpy(soap->arrayType, sizeof(soap->arrayType), tp->value);
  13253. } else if (!soap_match_tag(soap, tp->name, "SOAP-ENC:offset")) {
  13254. soap_strcpy(soap->arrayOffset, sizeof(soap->arrayOffset), tp->value);
  13255. } else if (!soap_match_tag(soap, tp->name, "SOAP-ENC:position")) {
  13256. soap->position = soap_getposition(tp->value, soap->positions);
  13257. } else if (!soap_match_tag(soap, tp->name, "SOAP-ENC:root")) {
  13258. soap->root = ((!strcmp(tp->value, "1") || !strcmp(tp->value, "true")));
  13259. } else if (!soap_match_tag(soap, tp->name, "SOAP-ENV:mustUnderstand")
  13260. && (!strcmp(tp->value, "1") || !strcmp(tp->value, "true"))) {
  13261. soap->mustUnderstand = 1;
  13262. } else if (!soap_match_tag(soap, tp->name, "SOAP-ENV:actor")) {
  13263. if ((!soap->actor || strcmp(soap->actor, tp->value))
  13264. && strcmp(tp->value, "http://schemas.xmlsoap.org/soap/actor/next"))
  13265. soap->other = 1;
  13266. }
  13267. } else if (soap->version == 2) {
  13268. #ifndef WITH_NOIDREF
  13269. if (!soap_match_tag(soap, tp->name, "SOAP-ENC:id")) {
  13270. *soap->id = '#';
  13271. soap_strcpy(soap->id + 1, sizeof(soap->id) - 1, tp->value);
  13272. } else if (!soap_match_tag(soap, tp->name, "SOAP-ENC:ref")) {
  13273. *soap->href = '#';
  13274. soap_strcpy(soap->href + (*tp->value != '#'), sizeof(soap->href) - 1, tp->value);
  13275. } else
  13276. #endif
  13277. if (!soap_match_tag(soap, tp->name, "SOAP-ENC:itemType")) {
  13278. soap_strcpy(soap->arrayType, sizeof(soap->arrayType), tp->value);
  13279. } else if (!soap_match_tag(soap, tp->name, "SOAP-ENC:arraySize")) {
  13280. soap_strcpy(soap->arraySize, sizeof(soap->arraySize), tp->value);
  13281. } else if (!soap_match_tag(soap, tp->name, "SOAP-ENV:mustUnderstand")
  13282. && (!strcmp(tp->value, "1") || !strcmp(tp->value, "true"))) {
  13283. soap->mustUnderstand = 1;
  13284. } else if (!soap_match_tag(soap, tp->name, "SOAP-ENV:role")) {
  13285. if ((!soap->actor || strcmp(soap->actor, tp->value))
  13286. && strcmp(tp->value, "http://www.w3.org/2003/05/soap-envelope/role/next"))
  13287. soap->other = 1;
  13288. }
  13289. } else {
  13290. if (!soap_match_tag(soap, tp->name, "wsdl:required") && !strcmp(tp->value, "true"))
  13291. soap->mustUnderstand = 1;
  13292. }
  13293. }
  13294. }
  13295. #ifdef WITH_DOM
  13296. if (soap->feltbegin)
  13297. return soap->error = soap->feltbegin(soap, soap->tag);
  13298. #endif
  13299. return soap->error = SOAP_OK;
  13300. }
  13301. /******************************************************************************/
  13302. SOAP_FMAC1
  13303. void
  13304. SOAP_FMAC2
  13305. soap_retry(struct soap *soap) {
  13306. soap->error = SOAP_OK;
  13307. soap_revert(soap);
  13308. }
  13309. /******************************************************************************/
  13310. SOAP_FMAC1
  13311. void
  13312. SOAP_FMAC2
  13313. soap_revert(struct soap *soap) {
  13314. if (!soap->peeked) {
  13315. soap->peeked = 1;
  13316. if (soap->body)
  13317. soap->level--;
  13318. }
  13319. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Reverting to last element '%s' (level=%u)\n", soap->tag, soap->level));
  13320. }
  13321. /******************************************************************************/
  13322. SOAP_FMAC1
  13323. int
  13324. SOAP_FMAC2
  13325. soap_ignore(struct soap *soap) {
  13326. int n = 0;
  13327. soap_wchar c;
  13328. soap->level++;
  13329. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Ignoring XML content at level=%u\n", soap->level));
  13330. #ifdef WITH_DOM
  13331. if ((soap->mode & SOAP_XML_DOM) && soap->dom)
  13332. {
  13333. if (!soap_string_in(soap, -1, -1, -1, NULL))
  13334. return soap->error;
  13335. }
  13336. else
  13337. #endif
  13338. {
  13339. for (;;) {
  13340. c = soap_get(soap);
  13341. switch (c) {
  13342. case SOAP_TT:
  13343. if (n == 0)
  13344. goto end;
  13345. n--;
  13346. break;
  13347. case SOAP_LT:
  13348. n++;
  13349. break;
  13350. case '/':
  13351. if (n > 0) {
  13352. c = soap_get0(soap);
  13353. if (c == '>')
  13354. n--;
  13355. }
  13356. break;
  13357. default:
  13358. if ((int) c == EOF)
  13359. return soap->error = SOAP_EOF;
  13360. }
  13361. }
  13362. end:
  13363. soap_unget(soap, c);
  13364. }
  13365. return soap_element_end_in(soap, NULL);
  13366. }
  13367. /******************************************************************************/
  13368. SOAP_FMAC1
  13369. int
  13370. SOAP_FMAC2
  13371. soap_string_out(struct soap *soap, const char *s, int flag) {
  13372. const char *t;
  13373. soap_wchar c;
  13374. soap_wchar mask = (soap_wchar) 0xFFFFFF80UL;
  13375. #ifdef WITH_DOM
  13376. if ((soap->mode & SOAP_XML_DOM) && soap->dom)
  13377. {
  13378. soap->dom->text = soap_strdup(soap, s);
  13379. if (!soap->dom->text)
  13380. return soap->error = SOAP_EOM;
  13381. return SOAP_OK;
  13382. }
  13383. #endif
  13384. if (flag == 2 || (soap->mode & SOAP_C_UTFSTRING))
  13385. mask = 0;
  13386. t = s;
  13387. while ((c = *t++)) {
  13388. switch (c) {
  13389. case 0x09:
  13390. if (flag) {
  13391. if (soap_send_raw(soap, s, t - s - 1) || soap_send_raw(soap, "&#x9;", 5))
  13392. return soap->error;
  13393. s = t;
  13394. }
  13395. break;
  13396. case 0x0A:
  13397. if (flag || !(soap->mode & SOAP_XML_CANONICAL)) {
  13398. if (soap_send_raw(soap, s, t - s - 1) || soap_send_raw(soap, "&#xA;", 5))
  13399. return soap->error;
  13400. s = t;
  13401. }
  13402. break;
  13403. case '&':
  13404. if (soap_send_raw(soap, s, t - s - 1) || soap_send_raw(soap, "&amp;", 5))
  13405. return soap->error;
  13406. s = t;
  13407. break;
  13408. case '<':
  13409. if (soap_send_raw(soap, s, t - s - 1) || soap_send_raw(soap, "&lt;", 4))
  13410. return soap->error;
  13411. s = t;
  13412. break;
  13413. case '>':
  13414. if (!flag) {
  13415. if (soap_send_raw(soap, s, t - s - 1) || soap_send_raw(soap, "&gt;", 4))
  13416. return soap->error;
  13417. s = t;
  13418. }
  13419. break;
  13420. case '"':
  13421. if (flag) {
  13422. if (soap_send_raw(soap, s, t - s - 1) || soap_send_raw(soap, "&quot;", 6))
  13423. return soap->error;
  13424. s = t;
  13425. }
  13426. break;
  13427. case 0x7F:
  13428. if (soap_send_raw(soap, s, t - s - 1) || soap_send_raw(soap, "&#x7F;", 6))
  13429. return soap->error;
  13430. s = t;
  13431. break;
  13432. default:
  13433. #ifndef WITH_LEANER
  13434. #ifdef HAVE_MBTOWC
  13435. if ((soap->mode & SOAP_C_MBSTRING)) {
  13436. wchar_t wc;
  13437. int m = mbtowc(&wc, t - 1, MB_CUR_MAX);
  13438. if (m > 0 && !((soap_wchar) wc == c && m == 1 && c < 0x80)) {
  13439. if (soap_send_raw(soap, s, t - s - 1) || soap_pututf8(soap, (unsigned long) wc))
  13440. return soap->error;
  13441. s = t += m - 1;
  13442. continue;
  13443. }
  13444. }
  13445. #endif
  13446. #endif
  13447. #ifndef WITH_NOSTRINGTOUTF8
  13448. if ((c & mask) || !(c & 0xFFFFFFE0UL)) {
  13449. if (soap_send_raw(soap, s, t - s - 1) || soap_pututf8(soap, (unsigned char) c))
  13450. return soap->error;
  13451. s = t;
  13452. }
  13453. #endif
  13454. }
  13455. }
  13456. return soap_send_raw(soap, s, t - s - 1);
  13457. }
  13458. /******************************************************************************/
  13459. SOAP_FMAC1
  13460. char *
  13461. SOAP_FMAC2
  13462. soap_string_in(struct soap *soap, int flag, long minlen, long maxlen, const char *pattern) {
  13463. char *s;
  13464. char *t = NULL;
  13465. size_t i;
  13466. ULONG64 l = 0;
  13467. int n = 0, f = 0, m = 0;
  13468. soap_wchar c;
  13469. #if !defined(WITH_LEANER) && defined(HAVE_WCTOMB)
  13470. char buf[MB_LEN_MAX > 8 ? MB_LEN_MAX : 8];
  13471. #else
  13472. char buf[8];
  13473. #endif
  13474. if (maxlen < 0 && soap->maxlength > 0)
  13475. maxlen = soap->maxlength;
  13476. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Reading string content, flag=%d\n", flag));
  13477. if (flag <= 0 && soap->peeked && *soap->tag) {
  13478. #ifndef WITH_LEAN
  13479. struct soap_attribute *tp;
  13480. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "String content includes tag '%s' and attributes\n", soap->tag));
  13481. t = soap->tmpbuf;
  13482. *t = '<';
  13483. soap_strcpy(t + 1, sizeof(soap->tmpbuf) - 1, soap->tag);
  13484. t += strlen(t);
  13485. for (tp = soap->attributes; tp; tp = tp->next) {
  13486. if (tp->visible) {
  13487. size_t k = strlen(tp->name);
  13488. if (t + k + 1 >= soap->tmpbuf + sizeof(soap->tmpbuf))
  13489. break; /* too many or attribute values to large */
  13490. *t++ = ' ';
  13491. (void) soap_strncpy(t, sizeof(soap->tmpbuf) - (t - soap->tmpbuf), tp->name, k);
  13492. t += k;
  13493. if (tp->value) {
  13494. k = strlen(tp->value);
  13495. if (t + k + 3 >= soap->tmpbuf + sizeof(soap->tmpbuf))
  13496. break; /* too many or attribute values to large */
  13497. *t++ = '=';
  13498. *t++ = '"';
  13499. (void) soap_strncpy(t, sizeof(soap->tmpbuf) - (t - soap->tmpbuf), tp->value, k);
  13500. t += k;
  13501. *t++ = '"';
  13502. }
  13503. }
  13504. }
  13505. if (!soap->body)
  13506. *t++ = '/';
  13507. *t++ = '>';
  13508. *t = '\0';
  13509. t = soap->tmpbuf;
  13510. m = (int) strlen(soap->tmpbuf);
  13511. #endif
  13512. if (soap->body)
  13513. n = 1;
  13514. f = 1;
  13515. soap->peeked = 0;
  13516. }
  13517. #ifdef WITH_CDATA
  13518. if (flag <= 0) {
  13519. int state = 0;
  13520. #ifdef WITH_FAST
  13521. soap->labidx = 0; /* use look-aside buffer */
  13522. #else
  13523. if (soap_alloc_block(soap) == NULL)
  13524. return NULL;
  13525. #endif
  13526. for (;;) {
  13527. #ifdef WITH_FAST
  13528. size_t k;
  13529. if (soap_append_lab(soap, NULL, 0)) /* allocate more space in look-aside buffer if necessary */
  13530. return NULL;
  13531. s = soap->labbuf + soap->labidx; /* space to populate */
  13532. k = soap->lablen - soap->labidx; /* number of bytes available */
  13533. soap->labidx = soap->lablen; /* claim this space */
  13534. #else
  13535. size_t k = SOAP_BLKLEN;
  13536. s = (char*)soap_push_block(soap, NULL, k);
  13537. if (!s)
  13538. return NULL;
  13539. #endif
  13540. for (i = 0; i < k; i++) {
  13541. if (m > 0) {
  13542. *s++ = *t++; /* copy multibyte characters */
  13543. m--;
  13544. continue;
  13545. }
  13546. c = soap_getchar(soap);
  13547. if ((int) c == EOF)
  13548. goto end;
  13549. if ((c >= 0x80 || c < SOAP_AP) && state != 1 && !(soap->mode & SOAP_ENC_LATIN)) {
  13550. if ((c & 0x7FFFFFFF) >= 0x80) {
  13551. soap_unget(soap, c);
  13552. c = soap_getutf8(soap);
  13553. }
  13554. if ((c & 0x7FFFFFFF) >= 0x80 && (flag <= 0 || (soap->mode & SOAP_C_UTFSTRING))) {
  13555. c &= 0x7FFFFFFF;
  13556. t = buf;
  13557. if (c < 0x0800)
  13558. *t++ = (char) (0xC0 | ((c >> 6) & 0x1F));
  13559. else {
  13560. #ifdef WITH_REPLACE_ILLEGAL_UTF8
  13561. if (!((c >= 0x80 && c <= 0xD7FF) || (c >= 0xE000 && c <= 0xFFFD) || (c >= 0x10000 && c <= 0x10FFFF)))
  13562. c = SOAP_UNKNOWN_UNICODE_CHAR;
  13563. #endif
  13564. if (c < 0x010000) {
  13565. *t++ = (char) (0xE0 | ((c >> 12) & 0x0F));
  13566. } else {
  13567. if (c < 0x200000) {
  13568. *t++ = (char) (0xF0 | ((c >> 18) & 0x07));
  13569. } else {
  13570. if (c < 0x04000000) {
  13571. *t++ = (char) (0xF8 | ((c >> 24) & 0x03));
  13572. } else {
  13573. *t++ = (char) (0xFC | ((c >> 30) & 0x01));
  13574. *t++ = (char) (0x80 | ((c >> 24) & 0x3F));
  13575. }
  13576. *t++ = (char) (0x80 | ((c >> 18) & 0x3F));
  13577. }
  13578. *t++ = (char) (0x80 | ((c >> 12) & 0x3F));
  13579. }
  13580. *t++ = (char) (0x80 | ((c >> 6) & 0x3F));
  13581. }
  13582. *t++ = (char) (0x80 | (c & 0x3F));
  13583. m = (int) (t - buf) - 1;
  13584. t = buf;
  13585. *s++ = *t++;
  13586. continue;
  13587. }
  13588. }
  13589. switch (state) {
  13590. case 1:
  13591. if (c == ']')
  13592. state = 4;
  13593. *s++ = (char) c;
  13594. continue;
  13595. case 2:
  13596. if (c == '-')
  13597. state = 6;
  13598. *s++ = (char) c;
  13599. continue;
  13600. case 3:
  13601. if (c == '?')
  13602. state = 8;
  13603. *s++ = (char) c;
  13604. continue;
  13605. /* CDATA */
  13606. case 4:
  13607. if (c == ']')
  13608. state = 5;
  13609. else
  13610. state = 1;
  13611. *s++ = (char) c;
  13612. continue;
  13613. case 5:
  13614. if (c == '>')
  13615. state = 0;
  13616. else if (c != ']')
  13617. state = 1;
  13618. *s++ = (char) c;
  13619. continue;
  13620. /* comment */
  13621. case 6:
  13622. if (c == '-')
  13623. state = 7;
  13624. else
  13625. state = 2;
  13626. *s++ = (char) c;
  13627. continue;
  13628. case 7:
  13629. if (c == '>')
  13630. state = 0;
  13631. else if (c != '-')
  13632. state = 2;
  13633. *s++ = (char) c;
  13634. continue;
  13635. /* PI */
  13636. case 8:
  13637. if (c == '>')
  13638. state = 0;
  13639. else if (c != '?')
  13640. state = 3;
  13641. *s++ = (char) c;
  13642. continue;
  13643. }
  13644. switch (c) {
  13645. case SOAP_TT:
  13646. if (n == 0)
  13647. goto end;
  13648. n--;
  13649. *s++ = '<';
  13650. t = (char *) "/";
  13651. m = 1;
  13652. break;
  13653. case SOAP_LT:
  13654. if (flag == 3 || (f && n == 0))
  13655. goto end;
  13656. n++;
  13657. *s++ = '<';
  13658. break;
  13659. case SOAP_GT:
  13660. *s++ = '>';
  13661. break;
  13662. case SOAP_QT:
  13663. *s++ = '"';
  13664. break;
  13665. case SOAP_AP:
  13666. *s++ = '\'';
  13667. break;
  13668. case '/':
  13669. if (n > 0) {
  13670. c = soap_getchar(soap);
  13671. if (c == '>')
  13672. n--;
  13673. soap_unget(soap, c);
  13674. }
  13675. *s++ = '/';
  13676. break;
  13677. case '<':
  13678. c = soap_getchar(soap);
  13679. if (c == '/') {
  13680. if (n == 0) {
  13681. c = SOAP_TT;
  13682. goto end;
  13683. }
  13684. n--;
  13685. } else if (c == '!') {
  13686. c = soap_getchar(soap);
  13687. if (c == '[') {
  13688. do {
  13689. c = soap_getchar(soap);
  13690. } while ((int) c != EOF && c != '[');
  13691. if ((int) c == EOF)
  13692. goto end;
  13693. t = (char *) "![CDATA[";
  13694. m = 8;
  13695. state = 1;
  13696. } else if (c == '-') {
  13697. c = soap_getchar(soap);
  13698. if (c == '-')
  13699. state = 2;
  13700. t = (char *) "!-";
  13701. m = 2;
  13702. soap_unget(soap, c);
  13703. } else {
  13704. t = (char *) "!";
  13705. m = 1;
  13706. soap_unget(soap, c);
  13707. }
  13708. *s++ = '<';
  13709. break;
  13710. } else if (c == '?') {
  13711. state = 3;
  13712. } else if (flag == 3 || (f && n == 0)) {
  13713. soap_revget1(soap);
  13714. c = '<';
  13715. goto end;
  13716. } else
  13717. n++;
  13718. soap_unget(soap, c);
  13719. *s++ = '<';
  13720. break;
  13721. case '>':
  13722. *s++ = '>';
  13723. break;
  13724. case '"':
  13725. *s++ = '"';
  13726. break;
  13727. default:
  13728. #ifndef WITH_LEANER
  13729. #ifdef HAVE_WCTOMB
  13730. if ((soap->mode & SOAP_C_MBSTRING)) {
  13731. #if defined(WIN32) && !defined(CYGWIN) && !defined(__MINGW32__) && !defined(__MINGW64__) && !defined(__BORLANDC__)
  13732. m = 0;
  13733. wctomb_s(&m, buf, sizeof(buf), (wchar_t)(c & 0x7FFFFFFF));
  13734. #else
  13735. m = wctomb(buf, (wchar_t) (c & 0x7FFFFFFF));
  13736. #endif
  13737. if (m >= 1 && m <= (int) MB_CUR_MAX) {
  13738. t = buf;
  13739. *s++ = *t++;
  13740. m--;
  13741. } else {
  13742. *s++ = SOAP_UNKNOWN_CHAR;
  13743. m = 0;
  13744. }
  13745. } else
  13746. #endif
  13747. #endif
  13748. *s++ = (char) (c & 0xFF);
  13749. }
  13750. l++;
  13751. if (maxlen >= 0 && l > (size_t) maxlen) {
  13752. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "String too long: maxlen=%ld\n", maxlen));
  13753. soap->error = SOAP_LENGTH;
  13754. return NULL;
  13755. }
  13756. }
  13757. }
  13758. }
  13759. #endif
  13760. #ifdef WITH_FAST
  13761. soap->labidx = 0; /* use look-aside buffer */
  13762. #else
  13763. if (soap_alloc_block(soap) == NULL)
  13764. return NULL;
  13765. #endif
  13766. for (;;) {
  13767. #ifdef WITH_FAST
  13768. size_t k;
  13769. if (soap_append_lab(soap, NULL, 0)) /* allocate more space in look-aside buffer if necessary */
  13770. return NULL;
  13771. s = soap->labbuf + soap->labidx; /* space to populate */
  13772. k = soap->lablen - soap->labidx; /* number of bytes available */
  13773. soap->labidx = soap->lablen; /* claim this space */
  13774. #else
  13775. size_t k = SOAP_BLKLEN;
  13776. s = (char*)soap_push_block(soap, NULL, k);
  13777. if (!s)
  13778. return NULL;
  13779. #endif
  13780. for (i = 0; i < k; i++) {
  13781. if (m > 0) {
  13782. *s++ = *t++; /* copy multibyte characters */
  13783. m--;
  13784. continue;
  13785. }
  13786. #ifndef WITH_CDATA
  13787. if (flag <= 0)
  13788. c = soap_getchar(soap);
  13789. else
  13790. #endif
  13791. {
  13792. c = soap_getutf8(soap);
  13793. if ((soap->mode & SOAP_C_UTFSTRING)) {
  13794. if (c >= 0x80 || (c < SOAP_AP && c >= -0x7FFFFF80)) {
  13795. c &= 0x7FFFFFFF;
  13796. t = buf;
  13797. if (c < 0x0800) {
  13798. *t++ = (char) (0xC0 | ((c >> 6) & 0x1F));
  13799. } else {
  13800. #ifdef WITH_REPLACE_ILLEGAL_UTF8
  13801. if (!((c >= 0x80 && c <= 0xD7FF) || (c >= 0xE000 && c <= 0xFFFD) || (c >= 0x10000 && c <= 0x10FFFF)))
  13802. c = SOAP_UNKNOWN_UNICODE_CHAR;
  13803. #endif
  13804. if (c < 0x010000) {
  13805. *t++ = (char) (0xE0 | ((c >> 12) & 0x0F));
  13806. } else {
  13807. if (c < 0x200000) {
  13808. *t++ = (char) (0xF0 | ((c >> 18) & 0x07));
  13809. } else {
  13810. if (c < 0x04000000) {
  13811. *t++ = (char) (0xF8 | ((c >> 24) & 0x03));
  13812. } else {
  13813. *t++ = (char) (0xFC | ((c >> 30) & 0x01));
  13814. *t++ = (char) (0x80 | ((c >> 24) & 0x3F));
  13815. }
  13816. *t++ = (char) (0x80 | ((c >> 18) & 0x3F));
  13817. }
  13818. *t++ = (char) (0x80 | ((c >> 12) & 0x3F));
  13819. }
  13820. *t++ = (char) (0x80 | ((c >> 6) & 0x3F));
  13821. }
  13822. *t++ = (char) (0x80 | (c & 0x3F));
  13823. m = (int) (t - buf) - 1;
  13824. t = buf;
  13825. *s++ = *t++;
  13826. l++;
  13827. if (maxlen >= 0 && l > (size_t) maxlen) {
  13828. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "String too long: maxlen=%ld\n", maxlen));
  13829. soap->error = SOAP_LENGTH;
  13830. return NULL;
  13831. }
  13832. continue;
  13833. }
  13834. }
  13835. }
  13836. switch (c) {
  13837. case SOAP_TT:
  13838. if (n == 0)
  13839. goto end;
  13840. n--;
  13841. *s++ = '<';
  13842. t = (char *) "/";
  13843. m = 1;
  13844. break;
  13845. case SOAP_LT:
  13846. if (flag == 3 || (f && n == 0))
  13847. goto end;
  13848. n++;
  13849. *s++ = '<';
  13850. break;
  13851. case SOAP_GT:
  13852. *s++ = '>';
  13853. break;
  13854. case SOAP_QT:
  13855. *s++ = '"';
  13856. break;
  13857. case SOAP_AP:
  13858. *s++ = '\'';
  13859. break;
  13860. case '/':
  13861. if (n > 0) {
  13862. if (flag > 0) {
  13863. c = soap_get(soap);
  13864. if (c == SOAP_GT)
  13865. n--;
  13866. } else {
  13867. c = soap_getchar(soap);
  13868. if (c == '>')
  13869. n--;
  13870. }
  13871. soap_unget(soap, c);
  13872. }
  13873. *s++ = '/';
  13874. break;
  13875. case (soap_wchar) ('<' | 0x80000000):
  13876. if (flag > 0) {
  13877. *s++ = '<';
  13878. } else {
  13879. *s++ = '&';
  13880. t = (char *) "lt;";
  13881. m = 3;
  13882. }
  13883. break;
  13884. case (soap_wchar) ('>' | 0x80000000):
  13885. if (flag > 0) {
  13886. *s++ = '>';
  13887. } else {
  13888. *s++ = '&';
  13889. t = (char *) "gt;";
  13890. m = 3;
  13891. }
  13892. break;
  13893. case (soap_wchar) ('&' | 0x80000000):
  13894. if (flag > 0) {
  13895. *s++ = '&';
  13896. } else {
  13897. *s++ = '&';
  13898. t = (char *) "amp;";
  13899. m = 4;
  13900. }
  13901. break;
  13902. case (soap_wchar) ('"' | 0x80000000):
  13903. if (flag > 0) {
  13904. *s++ = '"';
  13905. } else {
  13906. *s++ = '&';
  13907. t = (char *) "quot;";
  13908. m = 5;
  13909. }
  13910. break;
  13911. case (soap_wchar) ('\'' | 0x80000000):
  13912. if (flag > 0) {
  13913. *s++ = '\'';
  13914. } else {
  13915. *s++ = '&';
  13916. t = (char *) "apos;";
  13917. m = 5;
  13918. }
  13919. break;
  13920. default:
  13921. if ((int) c == EOF)
  13922. goto end;
  13923. #ifndef WITH_CDATA
  13924. if (c == '<')
  13925. {
  13926. c = soap_getchar(soap);
  13927. soap_unget(soap, c);
  13928. if (c == '/')
  13929. {
  13930. c = SOAP_TT;
  13931. if (n == 0)
  13932. goto end;
  13933. n--;
  13934. }
  13935. else
  13936. {
  13937. n++;
  13938. }
  13939. *s++ = '<';
  13940. }
  13941. else
  13942. #endif
  13943. #ifndef WITH_LEANER
  13944. #ifdef HAVE_WCTOMB
  13945. if ((soap->mode & SOAP_C_MBSTRING)) {
  13946. #if defined(WIN32) && !defined(CYGWIN) && !defined(__MINGW32__) && !defined(__MINGW64__) && !defined(__BORLANDC__)
  13947. m = 0;
  13948. wctomb_s(&m, buf, sizeof(buf), (wchar_t)(c & 0x7FFFFFFF));
  13949. #else
  13950. m = wctomb(buf, (wchar_t) (c & 0x7FFFFFFF));
  13951. #endif
  13952. if (m >= 1 && m <= (int) MB_CUR_MAX) {
  13953. t = buf;
  13954. *s++ = *t++;
  13955. m--;
  13956. } else {
  13957. *s++ = SOAP_UNKNOWN_CHAR;
  13958. m = 0;
  13959. }
  13960. } else
  13961. #endif
  13962. #endif
  13963. *s++ = (char) (c & 0xFF);
  13964. }
  13965. l++;
  13966. if (maxlen >= 0 && l > (size_t) maxlen) {
  13967. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "String too long: maxlen=%ld\n", maxlen));
  13968. soap->error = SOAP_LENGTH;
  13969. return NULL;
  13970. }
  13971. }
  13972. }
  13973. end:
  13974. soap_unget(soap, c);
  13975. *s = '\0';
  13976. #ifdef WITH_FAST
  13977. t = soap_strdup(soap, soap->labbuf);
  13978. if (!t)
  13979. return NULL;
  13980. #else
  13981. soap_size_block(soap, NULL, i + 1);
  13982. t = soap_save_block(soap, NULL, NULL, 0);
  13983. #endif
  13984. if (minlen > 0 && l < (size_t) minlen) {
  13985. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "String too short: %lu chars, minlen=%ld\n", (unsigned long) l, minlen));
  13986. soap->error = SOAP_LENGTH;
  13987. return NULL;
  13988. }
  13989. #ifdef WITH_DOM
  13990. if ((soap->mode & SOAP_XML_DOM) && soap->dom && *t)
  13991. {
  13992. if (flag > 0)
  13993. soap->dom->text = t;
  13994. else
  13995. soap->dom->code = t;
  13996. }
  13997. #endif
  13998. if (flag == 2) {
  13999. if (soap_s2QName(soap, t, &t, minlen, maxlen, pattern))
  14000. return NULL;
  14001. } else if (flag >= 4 && t) {
  14002. t = soap_collapse(soap, t, flag, 1);
  14003. }
  14004. #ifndef WITH_LEANER
  14005. else if (pattern && soap->fsvalidate) {
  14006. soap->error = soap->fsvalidate(soap, pattern, t);
  14007. if (soap->error)
  14008. return NULL;
  14009. }
  14010. #endif
  14011. return t;
  14012. }
  14013. /******************************************************************************/
  14014. #ifndef WITH_LEANER
  14015. SOAP_FMAC1
  14016. int
  14017. SOAP_FMAC2
  14018. soap_wstring_out(struct soap *soap, const wchar_t *s, int flag) {
  14019. const char *t;
  14020. char tmp;
  14021. soap_wchar c;
  14022. #ifdef WITH_DOM
  14023. if ((soap->mode & SOAP_XML_DOM) && soap->dom)
  14024. {
  14025. soap->dom->text = soap_wchar2s(soap, s);
  14026. return SOAP_OK;
  14027. }
  14028. #endif
  14029. while ((c = *s++)) {
  14030. switch (c) {
  14031. case 0x09:
  14032. if (flag)
  14033. t = "&#x9;";
  14034. else
  14035. t = "\t";
  14036. break;
  14037. case 0x0A:
  14038. if (flag || !(soap->mode & SOAP_XML_CANONICAL))
  14039. t = "&#xA;";
  14040. else
  14041. t = "\n";
  14042. break;
  14043. case 0x0D:
  14044. t = "&#xD;";
  14045. break;
  14046. case '&':
  14047. t = "&amp;";
  14048. break;
  14049. case '<':
  14050. t = "&lt;";
  14051. break;
  14052. case '>':
  14053. if (flag)
  14054. t = ">";
  14055. else
  14056. t = "&gt;";
  14057. break;
  14058. case '"':
  14059. if (flag)
  14060. t = "&quot;";
  14061. else
  14062. t = "\"";
  14063. break;
  14064. default:
  14065. if (c >= 0x20 && c < 0x80) {
  14066. tmp = (char) c;
  14067. if (soap_send_raw(soap, &tmp, 1))
  14068. return soap->error;
  14069. } else {
  14070. /* check for UTF16 encoding when wchar_t is too small to hold UCS */
  14071. if (sizeof(wchar_t) < 4 && (c & 0xFC00) == 0xD800) {
  14072. soap_wchar d = *s;
  14073. if ((d & 0xFC00) == 0xDC00) {
  14074. c = ((c - 0xD800) << 10) + (d - 0xDC00) + 0x10000;
  14075. s++;
  14076. }
  14077. #ifdef WITH_REPLACE_ILLEGAL_UTF8
  14078. else
  14079. {
  14080. c = SOAP_UNKNOWN_UNICODE_CHAR; /* Malformed UTF-16 */
  14081. }
  14082. #endif
  14083. }
  14084. if (soap_pututf8(soap, (unsigned long) c))
  14085. return soap->error;
  14086. }
  14087. continue;
  14088. }
  14089. if (soap_send(soap, t))
  14090. return soap->error;
  14091. }
  14092. return SOAP_OK;
  14093. }
  14094. #endif
  14095. /******************************************************************************/
  14096. #ifndef WITH_LEANER
  14097. SOAP_FMAC1
  14098. wchar_t *
  14099. SOAP_FMAC2
  14100. soap_wstring_in(struct soap *soap, int flag, long minlen, long maxlen, const char *pattern) {
  14101. wchar_t *s;
  14102. int i, n = 0, f = 0;
  14103. ULONG64 l = 0;
  14104. soap_wchar c;
  14105. char *t = NULL;
  14106. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Reading wide string content\n"));
  14107. if (maxlen < 0 && soap->maxlength > 0)
  14108. maxlen = soap->maxlength;
  14109. if (flag <= 0 && soap->peeked && *soap->tag) {
  14110. #ifndef WITH_LEAN
  14111. struct soap_attribute *tp;
  14112. t = soap->tmpbuf;
  14113. *t = '<';
  14114. soap_strcpy(t + 1, sizeof(soap->tmpbuf) - 1, soap->tag);
  14115. t += strlen(t);
  14116. for (tp = soap->attributes; tp; tp = tp->next) {
  14117. if (tp->visible) {
  14118. size_t k = strlen(tp->name);
  14119. if (t + k + 1 >= soap->tmpbuf + sizeof(soap->tmpbuf))
  14120. break;
  14121. *t++ = ' ';
  14122. (void) soap_strncpy(t, sizeof(soap->tmpbuf) - (t - soap->tmpbuf), tp->name, k);
  14123. t += k;
  14124. if (tp->value) {
  14125. k = strlen(tp->value);
  14126. if (t + k + 3 >= soap->tmpbuf + sizeof(soap->tmpbuf))
  14127. break;
  14128. *t++ = '=';
  14129. *t++ = '"';
  14130. (void) soap_strncpy(t, sizeof(soap->tmpbuf) - (t - soap->tmpbuf), tp->value, k);
  14131. t += k;
  14132. *t++ = '"';
  14133. }
  14134. }
  14135. }
  14136. if (!soap->body)
  14137. *t++ = '/';
  14138. *t++ = '>';
  14139. *t = '\0';
  14140. t = soap->tmpbuf;
  14141. #endif
  14142. if (soap->body)
  14143. n = 1;
  14144. f = 1;
  14145. soap->peeked = 0;
  14146. }
  14147. if (soap_alloc_block(soap) == NULL)
  14148. return NULL;
  14149. for (;;) {
  14150. s = (wchar_t *) soap_push_block(soap, NULL, sizeof(wchar_t) * SOAP_BLKLEN);
  14151. if (!s)
  14152. return NULL;
  14153. for (i = 0; i < SOAP_BLKLEN; i++) {
  14154. if (t) {
  14155. *s++ = (wchar_t) *t++;
  14156. if (!*t)
  14157. t = NULL;
  14158. continue;
  14159. }
  14160. c = soap_getutf8(soap);
  14161. switch (c) {
  14162. case SOAP_TT:
  14163. if (n == 0)
  14164. goto end;
  14165. n--;
  14166. *s++ = L'<';
  14167. soap_unget(soap, '/');
  14168. break;
  14169. case SOAP_LT:
  14170. if (flag == 3 || (f && n == 0))
  14171. goto end;
  14172. n++;
  14173. *s++ = L'<';
  14174. break;
  14175. case SOAP_GT:
  14176. *s++ = L'>';
  14177. break;
  14178. case SOAP_QT:
  14179. *s++ = L'"';
  14180. break;
  14181. case SOAP_AP:
  14182. *s++ = L'\'';
  14183. break;
  14184. case '/':
  14185. if (n > 0) {
  14186. c = soap_getutf8(soap);
  14187. if (c == SOAP_GT)
  14188. n--;
  14189. soap_unget(soap, c);
  14190. }
  14191. *s++ = L'/';
  14192. break;
  14193. case '<':
  14194. if (flag > 0) {
  14195. *s++ = L'<';
  14196. } else {
  14197. *s++ = L'&';
  14198. t = (char *) "lt;";
  14199. }
  14200. break;
  14201. case '>':
  14202. if (flag > 0) {
  14203. *s++ = L'>';
  14204. } else {
  14205. *s++ = (wchar_t) '&';
  14206. t = (char *) "gt;";
  14207. }
  14208. break;
  14209. case '"':
  14210. if (flag > 0) {
  14211. *s++ = L'"';
  14212. } else {
  14213. *s++ = L'&';
  14214. t = (char *) "quot;";
  14215. }
  14216. break;
  14217. default:
  14218. if ((int) c == EOF)
  14219. goto end;
  14220. /* use UTF16 encoding when wchar_t is too small to hold UCS */
  14221. if (sizeof(wchar_t) < 4 && c > 0xFFFF) {
  14222. soap_wchar c1, c2;
  14223. c1 = 0xD800 - (0x10000 >> 10) + (c >> 10);
  14224. c2 = 0xDC00 + (c & 0x3FF);
  14225. c = c1;
  14226. soap_unget(soap, c2);
  14227. }
  14228. *s++ = (wchar_t) (c & 0x7FFFFFFF);
  14229. }
  14230. l++;
  14231. if (maxlen >= 0 && l > (size_t) maxlen) {
  14232. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "String too long: maxlen=%ld\n", maxlen));
  14233. soap->error = SOAP_LENGTH;
  14234. return NULL;
  14235. }
  14236. }
  14237. }
  14238. end:
  14239. soap_unget(soap, c);
  14240. *s = L'\0';
  14241. soap_size_block(soap, NULL, sizeof(wchar_t) * (i + 1));
  14242. if (minlen > 0 && l < (size_t) minlen) {
  14243. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "String too short: %lu chars, minlen=%ld\n", (unsigned long) l, minlen));
  14244. soap->error = SOAP_LENGTH;
  14245. return NULL;
  14246. }
  14247. s = (wchar_t *) soap_save_block(soap, NULL, NULL, 0);
  14248. #ifndef WITH_LEAN
  14249. if (flag >= 4 && s)
  14250. s = soap_wcollapse(soap, s, flag, 1);
  14251. #endif
  14252. #ifndef WITH_LEANER
  14253. if (pattern && soap->fwvalidate) {
  14254. soap->error = soap->fwvalidate(soap, pattern, s);
  14255. if (soap->error)
  14256. return NULL;
  14257. }
  14258. #endif
  14259. #ifdef WITH_DOM
  14260. if ((soap->mode & SOAP_XML_DOM) && soap->dom)
  14261. soap->dom->text = soap_wchar2s(soap, s);
  14262. #endif
  14263. return s;
  14264. }
  14265. #endif
  14266. /******************************************************************************/
  14267. SOAP_FMAC1
  14268. const char *
  14269. SOAP_FMAC2
  14270. soap_int2s(struct soap *soap, int n) {
  14271. return soap_long2s(soap, (long) n);
  14272. }
  14273. /******************************************************************************/
  14274. SOAP_FMAC1
  14275. int
  14276. SOAP_FMAC2
  14277. soap_outint(struct soap *soap, const char *tag, int id, const int *p, const char *type, int n) {
  14278. if (soap_element_begin_out(soap, tag, soap_embedded_id(soap, id, p, n), type)
  14279. || soap_string_out(soap, soap_long2s(soap, (long) *p), 0))
  14280. return soap->error;
  14281. return soap_element_end_out(soap, tag);
  14282. }
  14283. /******************************************************************************/
  14284. SOAP_FMAC1
  14285. int
  14286. SOAP_FMAC2
  14287. soap_s2int(struct soap *soap, const char *s, int *p) {
  14288. if (s) {
  14289. long n;
  14290. char *r;
  14291. if (!*s)
  14292. return soap->error = SOAP_EMPTY;
  14293. #ifndef WITH_NOIO
  14294. #ifndef WITH_LEAN
  14295. soap_reset_errno;
  14296. #endif
  14297. #endif
  14298. n = soap_strtol(s, &r, 10);
  14299. if (s == r || *r
  14300. #ifndef WITH_LEAN
  14301. || n != (int) n
  14302. #endif
  14303. #ifndef WITH_NOIO
  14304. #ifndef WITH_LEAN
  14305. || soap_errno == SOAP_ERANGE
  14306. #endif
  14307. #endif
  14308. )
  14309. soap->error = SOAP_TYPE;
  14310. *p = (int) n;
  14311. }
  14312. return soap->error;
  14313. }
  14314. /******************************************************************************/
  14315. SOAP_FMAC1
  14316. int *
  14317. SOAP_FMAC2
  14318. soap_inint(struct soap *soap, const char *tag, int *p, const char *type, int t) {
  14319. if (soap_element_begin_in(soap, tag, 0, NULL))
  14320. return NULL;
  14321. #ifndef WITH_LEAN
  14322. if (*soap->type
  14323. && soap_match_tag(soap, soap->type, type)
  14324. && soap_match_tag(soap, soap->type, ":int")
  14325. && soap_match_tag(soap, soap->type, ":short")
  14326. && soap_match_tag(soap, soap->type, ":byte")) {
  14327. soap->error = SOAP_TYPE;
  14328. soap_revert(soap);
  14329. return NULL;
  14330. }
  14331. #else
  14332. (void)type;
  14333. #endif
  14334. p = (int *) soap_id_enter(soap, soap->id, p, t, sizeof(int), NULL, NULL, NULL, NULL);
  14335. if (!p)
  14336. return NULL;
  14337. if (*soap->href != '#') {
  14338. int err = soap_s2int(soap, soap_value(soap), p);
  14339. if ((soap->body && soap_element_end_in(soap, tag)) || err)
  14340. return NULL;
  14341. } else {
  14342. p = (int *) soap_id_forward(soap, soap->href, p, 0, t, t, sizeof(int), 0, NULL, NULL);
  14343. if (soap->body && soap_element_end_in(soap, tag))
  14344. return NULL;
  14345. }
  14346. return p;
  14347. }
  14348. /******************************************************************************/
  14349. SOAP_FMAC1
  14350. const char *
  14351. SOAP_FMAC2
  14352. soap_long2s(struct soap *soap, long n) {
  14353. (SOAP_SNPRINTF(soap->tmpbuf, sizeof(soap->tmpbuf), 20), "%ld", n);
  14354. return soap->tmpbuf;
  14355. }
  14356. /******************************************************************************/
  14357. SOAP_FMAC1
  14358. int
  14359. SOAP_FMAC2
  14360. soap_outlong(struct soap *soap, const char *tag, int id, const long *p, const char *type, int n) {
  14361. if (soap_element_begin_out(soap, tag, soap_embedded_id(soap, id, p, n), type)
  14362. || soap_string_out(soap, soap_long2s(soap, *p), 0))
  14363. return soap->error;
  14364. return soap_element_end_out(soap, tag);
  14365. }
  14366. /******************************************************************************/
  14367. SOAP_FMAC1
  14368. int
  14369. SOAP_FMAC2
  14370. soap_s2long(struct soap *soap, const char *s, long *p) {
  14371. if (s) {
  14372. char *r;
  14373. if (!*s)
  14374. return soap->error = SOAP_EMPTY;
  14375. #ifndef WITH_NOIO
  14376. #ifndef WITH_LEAN
  14377. soap_reset_errno;
  14378. #endif
  14379. #endif
  14380. *p = soap_strtol(s, &r, 10);
  14381. if (s == r || *r
  14382. #ifndef WITH_NOIO
  14383. #ifndef WITH_LEAN
  14384. || soap_errno == SOAP_ERANGE
  14385. #endif
  14386. #endif
  14387. )
  14388. soap->error = SOAP_TYPE;
  14389. }
  14390. return soap->error;
  14391. }
  14392. /******************************************************************************/
  14393. SOAP_FMAC1
  14394. long *
  14395. SOAP_FMAC2
  14396. soap_inlong(struct soap *soap, const char *tag, long *p, const char *type, int t) {
  14397. if (soap_element_begin_in(soap, tag, 0, NULL))
  14398. return NULL;
  14399. #ifndef WITH_LEAN
  14400. if (*soap->type
  14401. && soap_match_tag(soap, soap->type, type)
  14402. && soap_match_tag(soap, soap->type, ":int")
  14403. && soap_match_tag(soap, soap->type, ":short")
  14404. && soap_match_tag(soap, soap->type, ":byte")) {
  14405. soap->error = SOAP_TYPE;
  14406. soap_revert(soap);
  14407. return NULL;
  14408. }
  14409. #else
  14410. (void)type;
  14411. #endif
  14412. p = (long *) soap_id_enter(soap, soap->id, p, t, sizeof(long), NULL, NULL, NULL, NULL);
  14413. if (!p)
  14414. return NULL;
  14415. if (*soap->href != '#') {
  14416. int err = soap_s2long(soap, soap_value(soap), p);
  14417. if ((soap->body && soap_element_end_in(soap, tag)) || err)
  14418. return NULL;
  14419. } else {
  14420. p = (long *) soap_id_forward(soap, soap->href, p, 0, t, t, sizeof(long), 0, NULL, NULL);
  14421. if (soap->body && soap_element_end_in(soap, tag))
  14422. return NULL;
  14423. }
  14424. return p;
  14425. }
  14426. /******************************************************************************/
  14427. SOAP_FMAC1
  14428. const char *
  14429. SOAP_FMAC2
  14430. soap_LONG642s(struct soap *soap, LONG64 n) {
  14431. (SOAP_SNPRINTF(soap->tmpbuf, sizeof(soap->tmpbuf), 20), SOAP_LONG_FORMAT, n);
  14432. return soap->tmpbuf;
  14433. }
  14434. /******************************************************************************/
  14435. SOAP_FMAC1
  14436. int
  14437. SOAP_FMAC2
  14438. soap_outLONG64(struct soap *soap, const char *tag, int id, const LONG64 *p, const char *type, int n) {
  14439. if (soap_element_begin_out(soap, tag, soap_embedded_id(soap, id, p, n), type)
  14440. || soap_string_out(soap, soap_LONG642s(soap, *p), 0))
  14441. return soap->error;
  14442. return soap_element_end_out(soap, tag);
  14443. }
  14444. /******************************************************************************/
  14445. SOAP_FMAC1
  14446. int
  14447. SOAP_FMAC2
  14448. soap_s2LONG64(struct soap *soap, const char *s, LONG64 *p) {
  14449. if (s) {
  14450. char *r;
  14451. if (!*s)
  14452. return soap->error = SOAP_EMPTY;
  14453. #ifndef WITH_NOIO
  14454. #ifndef WITH_LEAN
  14455. soap_reset_errno;
  14456. #endif
  14457. #endif
  14458. *p = soap_strtoll(s, &r, 10);
  14459. if (s == r || *r
  14460. #ifndef WITH_NOIO
  14461. #ifndef WITH_LEAN
  14462. || soap_errno == SOAP_ERANGE
  14463. #endif
  14464. #endif
  14465. )
  14466. soap->error = SOAP_TYPE;
  14467. }
  14468. return soap->error;
  14469. }
  14470. /******************************************************************************/
  14471. SOAP_FMAC1
  14472. LONG64 *
  14473. SOAP_FMAC2
  14474. soap_inLONG64(struct soap *soap, const char *tag, LONG64 *p, const char *type, int t) {
  14475. if (soap_element_begin_in(soap, tag, 0, NULL))
  14476. return NULL;
  14477. #ifndef WITH_LEAN
  14478. if (*soap->type
  14479. && soap_match_tag(soap, soap->type, type)
  14480. && soap_match_tag(soap, soap->type, ":integer")
  14481. && soap_match_tag(soap, soap->type, ":positiveInteger")
  14482. && soap_match_tag(soap, soap->type, ":negativeInteger")
  14483. && soap_match_tag(soap, soap->type, ":nonPositiveInteger")
  14484. && soap_match_tag(soap, soap->type, ":nonNegativeInteger")
  14485. && soap_match_tag(soap, soap->type, ":long")
  14486. && soap_match_tag(soap, soap->type, ":int")
  14487. && soap_match_tag(soap, soap->type, ":short")
  14488. && soap_match_tag(soap, soap->type, ":byte")) {
  14489. soap->error = SOAP_TYPE;
  14490. soap_revert(soap);
  14491. return NULL;
  14492. }
  14493. #else
  14494. (void)type;
  14495. #endif
  14496. p = (LONG64 *) soap_id_enter(soap, soap->id, p, t, sizeof(LONG64), NULL, NULL, NULL, NULL);
  14497. if (!p)
  14498. return NULL;
  14499. if (*soap->href != '#') {
  14500. int err = soap_s2LONG64(soap, soap_value(soap), p);
  14501. if ((soap->body && soap_element_end_in(soap, tag)) || err)
  14502. return NULL;
  14503. } else {
  14504. p = (LONG64 *) soap_id_forward(soap, soap->href, p, 0, t, t, sizeof(LONG64), 0, NULL, NULL);
  14505. if (soap->body && soap_element_end_in(soap, tag))
  14506. return NULL;
  14507. }
  14508. return p;
  14509. }
  14510. /******************************************************************************/
  14511. SOAP_FMAC1
  14512. const char *
  14513. SOAP_FMAC2
  14514. soap_byte2s(struct soap *soap, char n) {
  14515. return soap_long2s(soap, (long) n);
  14516. }
  14517. /******************************************************************************/
  14518. SOAP_FMAC1
  14519. int
  14520. SOAP_FMAC2
  14521. soap_outbyte(struct soap *soap, const char *tag, int id, const char *p, const char *type, int n) {
  14522. if (soap_element_begin_out(soap, tag, soap_embedded_id(soap, id, p, n), type)
  14523. || soap_string_out(soap, soap_long2s(soap, (long) *p), 0))
  14524. return soap->error;
  14525. return soap_element_end_out(soap, tag);
  14526. }
  14527. /******************************************************************************/
  14528. SOAP_FMAC1
  14529. int
  14530. SOAP_FMAC2
  14531. soap_s2byte(struct soap *soap, const char *s, char *p) {
  14532. if (s) {
  14533. long n;
  14534. char *r;
  14535. if (!*s)
  14536. return soap->error = SOAP_EMPTY;
  14537. n = soap_strtol(s, &r, 10);
  14538. if (s == r || *r || n < -128 || n > 127)
  14539. soap->error = SOAP_TYPE;
  14540. *p = (char) n;
  14541. }
  14542. return soap->error;
  14543. }
  14544. /******************************************************************************/
  14545. SOAP_FMAC1
  14546. char *
  14547. SOAP_FMAC2
  14548. soap_inbyte(struct soap *soap, const char *tag, char *p, const char *type, int t) {
  14549. if (soap_element_begin_in(soap, tag, 0, NULL))
  14550. return NULL;
  14551. #ifndef WITH_LEAN
  14552. if (*soap->type
  14553. && soap_match_tag(soap, soap->type, type)
  14554. && soap_match_tag(soap, soap->type, ":byte")) {
  14555. soap->error = SOAP_TYPE;
  14556. soap_revert(soap);
  14557. return NULL;
  14558. }
  14559. #else
  14560. (void)type;
  14561. #endif
  14562. p = (char *) soap_id_enter(soap, soap->id, p, t, sizeof(char), NULL, NULL, NULL, NULL);
  14563. if (!p)
  14564. return NULL;
  14565. if (*soap->href != '#') {
  14566. int err = soap_s2byte(soap, soap_value(soap), p);
  14567. if ((soap->body && soap_element_end_in(soap, tag)) || err)
  14568. return NULL;
  14569. } else {
  14570. p = (char *) soap_id_forward(soap, soap->href, p, 0, t, t, sizeof(char), 0, NULL, NULL);
  14571. if (soap->body && soap_element_end_in(soap, tag))
  14572. return NULL;
  14573. }
  14574. return p;
  14575. }
  14576. /******************************************************************************/
  14577. SOAP_FMAC1
  14578. const char *
  14579. SOAP_FMAC2
  14580. soap_short2s(struct soap *soap, short n) {
  14581. return soap_long2s(soap, (long) n);
  14582. }
  14583. /******************************************************************************/
  14584. SOAP_FMAC1
  14585. int
  14586. SOAP_FMAC2
  14587. soap_outshort(struct soap *soap, const char *tag, int id, const short *p, const char *type, int n) {
  14588. if (soap_element_begin_out(soap, tag, soap_embedded_id(soap, id, p, n), type)
  14589. || soap_string_out(soap, soap_long2s(soap, (long) *p), 0))
  14590. return soap->error;
  14591. return soap_element_end_out(soap, tag);
  14592. }
  14593. /******************************************************************************/
  14594. SOAP_FMAC1
  14595. int
  14596. SOAP_FMAC2
  14597. soap_s2short(struct soap *soap, const char *s, short *p) {
  14598. if (s) {
  14599. long n;
  14600. char *r;
  14601. if (!*s)
  14602. return soap->error = SOAP_EMPTY;
  14603. n = soap_strtol(s, &r, 10);
  14604. if (s == r || *r || n < -32768 || n > 32767)
  14605. soap->error = SOAP_TYPE;
  14606. *p = (short) n;
  14607. }
  14608. return soap->error;
  14609. }
  14610. /******************************************************************************/
  14611. SOAP_FMAC1
  14612. short *
  14613. SOAP_FMAC2
  14614. soap_inshort(struct soap *soap, const char *tag, short *p, const char *type, int t) {
  14615. if (soap_element_begin_in(soap, tag, 0, NULL))
  14616. return NULL;
  14617. #ifndef WITH_LEAN
  14618. if (*soap->type
  14619. && soap_match_tag(soap, soap->type, type)
  14620. && soap_match_tag(soap, soap->type, ":short")
  14621. && soap_match_tag(soap, soap->type, ":byte")) {
  14622. soap->error = SOAP_TYPE;
  14623. soap_revert(soap);
  14624. return NULL;
  14625. }
  14626. #else
  14627. (void)type;
  14628. #endif
  14629. p = (short *) soap_id_enter(soap, soap->id, p, t, sizeof(short), NULL, NULL, NULL, NULL);
  14630. if (!p)
  14631. return NULL;
  14632. if (*soap->href != '#') {
  14633. int err = soap_s2short(soap, soap_value(soap), p);
  14634. if ((soap->body && soap_element_end_in(soap, tag)) || err)
  14635. return NULL;
  14636. } else {
  14637. p = (short *) soap_id_forward(soap, soap->href, p, 0, t, t, sizeof(short), 0, NULL, NULL);
  14638. if (soap->body && soap_element_end_in(soap, tag))
  14639. return NULL;
  14640. }
  14641. return p;
  14642. }
  14643. /******************************************************************************/
  14644. SOAP_FMAC1
  14645. const char *
  14646. SOAP_FMAC2
  14647. soap_float2s(struct soap *soap, float n) {
  14648. #if defined(WITH_C_LOCALE)
  14649. # if !defined(WIN32)
  14650. SOAP_LOCALE_T locale;
  14651. # endif
  14652. #else
  14653. char *s;
  14654. #endif
  14655. if (soap_isnan((double) n))
  14656. return "NaN";
  14657. if (soap_ispinff(n))
  14658. return "INF";
  14659. if (soap_isninff(n))
  14660. return "-INF";
  14661. #if defined(WITH_C_LOCALE)
  14662. # ifdef WIN32
  14663. _sprintf_s_l(soap->tmpbuf, _countof(soap->tmpbuf), soap->float_format, SOAP_LOCALE(soap), n);
  14664. # else
  14665. locale = uselocale(SOAP_LOCALE(soap));
  14666. (SOAP_SNPRINTF(soap->tmpbuf, sizeof(soap->tmpbuf), 20), soap->float_format, n);
  14667. uselocale(locale);
  14668. # endif
  14669. #else
  14670. (SOAP_SNPRINTF(soap->tmpbuf, sizeof(soap->tmpbuf), 20), soap->float_format, n);
  14671. s = strchr(soap->tmpbuf, ','); /* convert decimal comma to DP */
  14672. if (s)
  14673. *s = '.';
  14674. #endif
  14675. return soap->tmpbuf;
  14676. }
  14677. /******************************************************************************/
  14678. SOAP_FMAC1
  14679. int
  14680. SOAP_FMAC2
  14681. soap_outfloat(struct soap *soap, const char *tag, int id, const float *p, const char *type, int n) {
  14682. if (soap_element_begin_out(soap, tag, soap_embedded_id(soap, id, p, n), type)
  14683. || soap_string_out(soap, soap_float2s(soap, *p), 0))
  14684. return soap->error;
  14685. return soap_element_end_out(soap, tag);
  14686. }
  14687. /******************************************************************************/
  14688. SOAP_FMAC1
  14689. int
  14690. SOAP_FMAC2
  14691. soap_s2float(struct soap *soap, const char *s, float *p) {
  14692. if (s) {
  14693. if (!*s)
  14694. return soap->error = SOAP_EMPTY;
  14695. if (!soap_tag_cmp(s, "INF")) {
  14696. *p = FLT_PINFTY;
  14697. } else if (!soap_tag_cmp(s, "+INF")) {
  14698. *p = FLT_PINFTY;
  14699. } else if (!soap_tag_cmp(s, "-INF")) {
  14700. *p = FLT_NINFTY;
  14701. } else if (!soap_tag_cmp(s, "NaN")) {
  14702. *p = FLT_NAN;
  14703. } else {
  14704. /* On some systems strtof requires -std=c99 or does not even link: so we try strtod first */
  14705. #if defined(WITH_C_LOCALE)
  14706. # if defined(HAVE_STRTOD_L)
  14707. char *r;
  14708. # ifdef WIN32
  14709. *p = (float)_strtod_l(s, &r, SOAP_LOCALE(soap));
  14710. # else
  14711. *p = (float) strtod_l(s, &r, SOAP_LOCALE(soap));
  14712. # endif
  14713. if (*r)
  14714. soap->error = SOAP_TYPE;
  14715. # elif defined(HAVE_STRTOF_L)
  14716. char *r;
  14717. *p = strtof_l((char*)s, &r, SOAP_LOCALE(soap));
  14718. if (*r)
  14719. soap->error = SOAP_TYPE;
  14720. # elif defined(HAVE_SSCANF_L)
  14721. double n;
  14722. if (sscanf_l(s, SOAP_LOCALE(soap), "%lf", &n) != 1)
  14723. soap->error = SOAP_TYPE;
  14724. *p = (float)n;
  14725. # elif defined(HAVE_STRTOD)
  14726. char *r;
  14727. SOAP_LOCALE_T locale = uselocale(SOAP_LOCALE(soap));
  14728. *p = (float)strtod((char*)s, &r);
  14729. uselocale(locale);
  14730. if (*r)
  14731. soap->error = SOAP_TYPE;
  14732. # elif defined(HAVE_STRTOF)
  14733. char *r;
  14734. SOAP_LOCALE_T locale = uselocale(SOAP_LOCALE(soap));
  14735. *p = strtof((char*)s, &r);
  14736. uselocale(locale);
  14737. if (*r)
  14738. soap->error = SOAP_TYPE;
  14739. # elif defined(HAVE_SSCANF)
  14740. double n;
  14741. SOAP_LOCALE_T locale = uselocale(SOAP_LOCALE(soap));
  14742. if (sscanf(s, "%lf", &n) != 1)
  14743. soap->error = SOAP_TYPE;
  14744. uselocale(locale);
  14745. *p = (float)n;
  14746. # else
  14747. soap->error = SOAP_TYPE;
  14748. # endif
  14749. #elif defined(HAVE_STRTOD)
  14750. char *r;
  14751. *p = (float)strtod(s, &r);
  14752. if (*r)
  14753. soap->error = SOAP_TYPE;
  14754. #elif defined(HAVE_STRTOF)
  14755. char *r;
  14756. *p = strtof((char*)s, &r);
  14757. if (*r)
  14758. soap->error = SOAP_TYPE;
  14759. #elif defined(HAVE_SSCANF)
  14760. double n;
  14761. if (sscanf(s, "%lf", &n) != 1)
  14762. soap->error = SOAP_TYPE;
  14763. *p = (float)n;
  14764. #else
  14765. soap->error = SOAP_TYPE;
  14766. #endif
  14767. }
  14768. }
  14769. return soap->error;
  14770. }
  14771. /******************************************************************************/
  14772. #ifndef WITH_LEAN
  14773. static int soap_isnumeric(struct soap *soap, const char *type) {
  14774. if (soap_match_tag(soap, soap->type, type)
  14775. && soap_match_tag(soap, soap->type, ":float")
  14776. && soap_match_tag(soap, soap->type, ":double")
  14777. && soap_match_tag(soap, soap->type, ":decimal")
  14778. && soap_match_tag(soap, soap->type, ":integer")
  14779. && soap_match_tag(soap, soap->type, ":positiveInteger")
  14780. && soap_match_tag(soap, soap->type, ":negativeInteger")
  14781. && soap_match_tag(soap, soap->type, ":nonPositiveInteger")
  14782. && soap_match_tag(soap, soap->type, ":nonNegativeInteger")
  14783. && soap_match_tag(soap, soap->type, ":long")
  14784. && soap_match_tag(soap, soap->type, ":int")
  14785. && soap_match_tag(soap, soap->type, ":short")
  14786. && soap_match_tag(soap, soap->type, ":byte")
  14787. && soap_match_tag(soap, soap->type, ":unsignedLong")
  14788. && soap_match_tag(soap, soap->type, ":unsignedInt")
  14789. && soap_match_tag(soap, soap->type, ":unsignedShort")
  14790. && soap_match_tag(soap, soap->type, ":unsignedByte")) {
  14791. soap->error = SOAP_TYPE;
  14792. soap_revert(soap);
  14793. return SOAP_ERR;
  14794. }
  14795. return SOAP_OK;
  14796. }
  14797. #endif
  14798. /******************************************************************************/
  14799. SOAP_FMAC1
  14800. float *
  14801. SOAP_FMAC2
  14802. soap_infloat(struct soap *soap, const char *tag, float *p, const char *type, int t) {
  14803. if (soap_element_begin_in(soap, tag, 0, NULL))
  14804. return NULL;
  14805. #ifndef WITH_LEAN
  14806. if (*soap->type != '\0' && soap_isnumeric(soap, type))
  14807. return NULL;
  14808. #else
  14809. (void)type;
  14810. #endif
  14811. p = (float *) soap_id_enter(soap, soap->id, p, t, sizeof(float), NULL, NULL, NULL, NULL);
  14812. if (!p)
  14813. return NULL;
  14814. if (*soap->href != '#') {
  14815. int err = soap_s2float(soap, soap_value(soap), p);
  14816. if ((soap->body && soap_element_end_in(soap, tag)) || err)
  14817. return NULL;
  14818. } else {
  14819. p = (float *) soap_id_forward(soap, soap->href, p, 0, t, t, sizeof(float), 0, NULL, NULL);
  14820. if (soap->body && soap_element_end_in(soap, tag))
  14821. return NULL;
  14822. }
  14823. return p;
  14824. }
  14825. /******************************************************************************/
  14826. SOAP_FMAC1
  14827. const char *
  14828. SOAP_FMAC2
  14829. soap_double2s(struct soap *soap, double n) {
  14830. #if defined(WITH_C_LOCALE)
  14831. # if !defined(WIN32)
  14832. SOAP_LOCALE_T locale;
  14833. # endif
  14834. #else
  14835. char *s;
  14836. #endif
  14837. if (soap_isnan(n))
  14838. return "NaN";
  14839. if (soap_ispinfd(n))
  14840. return "INF";
  14841. if (soap_isninfd(n))
  14842. return "-INF";
  14843. #if defined(WITH_C_LOCALE)
  14844. # ifdef WIN32
  14845. _sprintf_s_l(soap->tmpbuf, _countof(soap->tmpbuf), soap->double_format, SOAP_LOCALE(soap), n);
  14846. # else
  14847. locale = uselocale(SOAP_LOCALE(soap));
  14848. (SOAP_SNPRINTF(soap->tmpbuf, sizeof(soap->tmpbuf), 40), soap->double_format, n);
  14849. uselocale(locale);
  14850. # endif
  14851. #else
  14852. (SOAP_SNPRINTF(soap->tmpbuf, sizeof(soap->tmpbuf), 40), soap->double_format, n);
  14853. s = strchr(soap->tmpbuf, ','); /* convert decimal comma to DP */
  14854. if (s)
  14855. *s = '.';
  14856. #endif
  14857. return soap->tmpbuf;
  14858. }
  14859. /******************************************************************************/
  14860. SOAP_FMAC1
  14861. int
  14862. SOAP_FMAC2
  14863. soap_outdouble(struct soap *soap, const char *tag, int id, const double *p, const char *type, int n) {
  14864. if (soap_element_begin_out(soap, tag, soap_embedded_id(soap, id, p, n), type)
  14865. || soap_string_out(soap, soap_double2s(soap, *p), 0))
  14866. return soap->error;
  14867. return soap_element_end_out(soap, tag);
  14868. }
  14869. /******************************************************************************/
  14870. SOAP_FMAC1
  14871. int
  14872. SOAP_FMAC2
  14873. soap_s2double(struct soap *soap, const char *s, double *p) {
  14874. if (s) {
  14875. if (!*s)
  14876. return soap->error = SOAP_EMPTY;
  14877. if (!soap_tag_cmp(s, "INF")) {
  14878. *p = DBL_PINFTY;
  14879. } else if (!soap_tag_cmp(s, "+INF")) {
  14880. *p = DBL_PINFTY;
  14881. } else if (!soap_tag_cmp(s, "-INF")) {
  14882. *p = DBL_NINFTY;
  14883. } else if (!soap_tag_cmp(s, "NaN")) {
  14884. *p = DBL_NAN;
  14885. } else {
  14886. #if defined(WITH_C_LOCALE)
  14887. # if defined(HAVE_STRTOD_L)
  14888. char *r;
  14889. # ifdef WIN32
  14890. *p = _strtod_l(s, &r, SOAP_LOCALE(soap));
  14891. # else
  14892. *p = strtod_l(s, &r, SOAP_LOCALE(soap));
  14893. # endif
  14894. if (*r)
  14895. soap->error = SOAP_TYPE;
  14896. # elif defined(HAVE_STRTOD)
  14897. char *r;
  14898. SOAP_LOCALE_T locale = uselocale(SOAP_LOCALE(soap));
  14899. *p = strtod(s, &r);
  14900. uselocale(locale);
  14901. if (*r)
  14902. soap->error = SOAP_TYPE;
  14903. # elif defined(HAVE_SSCANF_L)
  14904. SOAP_LOCALE_T locale = uselocale(SOAP_LOCALE(soap));
  14905. if (sscanf_l(s, SOAP_LOCALE(soap), "%lf", p) != 1)
  14906. soap->error = SOAP_TYPE;
  14907. uselocale(locale);
  14908. # else
  14909. soap->error = SOAP_TYPE;
  14910. # endif
  14911. #elif defined(HAVE_STRTOD)
  14912. char *r;
  14913. *p = strtod(s, &r);
  14914. if (*r)
  14915. soap->error = SOAP_TYPE;
  14916. #elif defined(HAVE_SSCANF)
  14917. if (sscanf(s, "%lf", p) != 1)
  14918. soap->error = SOAP_TYPE;
  14919. #else
  14920. soap->error = SOAP_TYPE;
  14921. #endif
  14922. }
  14923. }
  14924. return soap->error;
  14925. }
  14926. /******************************************************************************/
  14927. SOAP_FMAC1
  14928. double *
  14929. SOAP_FMAC2
  14930. soap_indouble(struct soap *soap, const char *tag, double *p, const char *type, int t) {
  14931. if (soap_element_begin_in(soap, tag, 0, NULL))
  14932. return NULL;
  14933. #ifndef WITH_LEAN
  14934. if (*soap->type != '\0' && soap_isnumeric(soap, type))
  14935. return NULL;
  14936. #else
  14937. (void)type;
  14938. #endif
  14939. p = (double *) soap_id_enter(soap, soap->id, p, t, sizeof(double), NULL, NULL, NULL, NULL);
  14940. if (!p)
  14941. return NULL;
  14942. if (*soap->href != '#') {
  14943. int err = soap_s2double(soap, soap_value(soap), p);
  14944. if ((soap->body && soap_element_end_in(soap, tag)) || err)
  14945. return NULL;
  14946. } else {
  14947. p = (double *) soap_id_forward(soap, soap->href, p, 0, t, t, sizeof(double), 0, NULL, NULL);
  14948. if (soap->body && soap_element_end_in(soap, tag))
  14949. return NULL;
  14950. }
  14951. return p;
  14952. }
  14953. /******************************************************************************/
  14954. SOAP_FMAC1
  14955. const char *
  14956. SOAP_FMAC2
  14957. soap_unsignedByte2s(struct soap *soap, unsigned char n) {
  14958. return soap_unsignedLong2s(soap, (unsigned long) n);
  14959. }
  14960. /******************************************************************************/
  14961. SOAP_FMAC1
  14962. int
  14963. SOAP_FMAC2
  14964. soap_outunsignedByte(struct soap *soap, const char *tag, int id, const unsigned char *p, const char *type, int n) {
  14965. if (soap_element_begin_out(soap, tag, soap_embedded_id(soap, id, p, n), type)
  14966. || soap_string_out(soap, soap_unsignedLong2s(soap, (unsigned long) *p), 0))
  14967. return soap->error;
  14968. return soap_element_end_out(soap, tag);
  14969. }
  14970. /******************************************************************************/
  14971. SOAP_FMAC1
  14972. int
  14973. SOAP_FMAC2
  14974. soap_s2unsignedByte(struct soap *soap, const char *s, unsigned char *p) {
  14975. if (s) {
  14976. long n;
  14977. char *r;
  14978. if (!*s)
  14979. return soap->error = SOAP_EMPTY;
  14980. n = soap_strtol(s, &r, 10);
  14981. if (s == r || *r || n < 0 || n > 255)
  14982. soap->error = SOAP_TYPE;
  14983. *p = (unsigned char) n;
  14984. }
  14985. return soap->error;
  14986. }
  14987. /******************************************************************************/
  14988. SOAP_FMAC1
  14989. unsigned char *
  14990. SOAP_FMAC2
  14991. soap_inunsignedByte(struct soap *soap, const char *tag, unsigned char *p, const char *type, int t) {
  14992. if (soap_element_begin_in(soap, tag, 0, NULL))
  14993. return NULL;
  14994. #ifndef WITH_LEAN
  14995. if (*soap->type
  14996. && soap_match_tag(soap, soap->type, type)
  14997. && soap_match_tag(soap, soap->type, ":unsignedByte")) {
  14998. soap->error = SOAP_TYPE;
  14999. soap_revert(soap);
  15000. return NULL;
  15001. }
  15002. #else
  15003. (void)type;
  15004. #endif
  15005. p = (unsigned char *) soap_id_enter(soap, soap->id, p, t, sizeof(unsigned char), NULL, NULL, NULL, NULL);
  15006. if (!p)
  15007. return NULL;
  15008. if (*soap->href != '#') {
  15009. int err = soap_s2unsignedByte(soap, soap_value(soap), p);
  15010. if ((soap->body && soap_element_end_in(soap, tag)) || err)
  15011. return NULL;
  15012. } else {
  15013. p = (unsigned char *) soap_id_forward(soap, soap->href, p, 0, t, t, sizeof(unsigned char), 0, NULL, NULL);
  15014. if (soap->body && soap_element_end_in(soap, tag))
  15015. return NULL;
  15016. }
  15017. return p;
  15018. }
  15019. /******************************************************************************/
  15020. SOAP_FMAC1
  15021. const char *
  15022. SOAP_FMAC2
  15023. soap_unsignedShort2s(struct soap *soap, unsigned short n) {
  15024. return soap_unsignedLong2s(soap, (unsigned long) n);
  15025. }
  15026. /******************************************************************************/
  15027. SOAP_FMAC1
  15028. int
  15029. SOAP_FMAC2
  15030. soap_outunsignedShort(struct soap *soap, const char *tag, int id, const unsigned short *p, const char *type, int n) {
  15031. if (soap_element_begin_out(soap, tag, soap_embedded_id(soap, id, p, n), type)
  15032. || soap_string_out(soap, soap_unsignedLong2s(soap, (unsigned long) *p), 0))
  15033. return soap->error;
  15034. return soap_element_end_out(soap, tag);
  15035. }
  15036. /******************************************************************************/
  15037. SOAP_FMAC1
  15038. int
  15039. SOAP_FMAC2
  15040. soap_s2unsignedShort(struct soap *soap, const char *s, unsigned short *p) {
  15041. if (s) {
  15042. long n;
  15043. char *r;
  15044. if (!*s)
  15045. return soap->error = SOAP_EMPTY;
  15046. n = soap_strtol(s, &r, 10);
  15047. if (s == r || *r || n < 0 || n > 65535)
  15048. soap->error = SOAP_TYPE;
  15049. *p = (unsigned short) n;
  15050. }
  15051. return soap->error;
  15052. }
  15053. /******************************************************************************/
  15054. SOAP_FMAC1
  15055. unsigned short *
  15056. SOAP_FMAC2
  15057. soap_inunsignedShort(struct soap *soap, const char *tag, unsigned short *p, const char *type, int t) {
  15058. if (soap_element_begin_in(soap, tag, 0, NULL))
  15059. return NULL;
  15060. #ifndef WITH_LEAN
  15061. if (*soap->type
  15062. && soap_match_tag(soap, soap->type, type)
  15063. && soap_match_tag(soap, soap->type, ":unsignedShort")
  15064. && soap_match_tag(soap, soap->type, ":unsignedByte")) {
  15065. soap->error = SOAP_TYPE;
  15066. soap_revert(soap);
  15067. return NULL;
  15068. }
  15069. #else
  15070. (void)type;
  15071. #endif
  15072. p = (unsigned short *) soap_id_enter(soap, soap->id, p, t, sizeof(unsigned short), NULL, NULL, NULL, NULL);
  15073. if (!p)
  15074. return NULL;
  15075. if (*soap->href != '#') {
  15076. int err = soap_s2unsignedShort(soap, soap_value(soap), p);
  15077. if ((soap->body && soap_element_end_in(soap, tag)) || err)
  15078. return NULL;
  15079. } else {
  15080. p = (unsigned short *) soap_id_forward(soap, soap->href, p, 0, t, t, sizeof(unsigned short), 0, NULL, NULL);
  15081. if (soap->body && soap_element_end_in(soap, tag))
  15082. return NULL;
  15083. }
  15084. return p;
  15085. }
  15086. /******************************************************************************/
  15087. SOAP_FMAC1
  15088. const char *
  15089. SOAP_FMAC2
  15090. soap_unsignedInt2s(struct soap *soap, unsigned int n) {
  15091. return soap_unsignedLong2s(soap, (unsigned long) n);
  15092. }
  15093. /******************************************************************************/
  15094. SOAP_FMAC1
  15095. int
  15096. SOAP_FMAC2
  15097. soap_outunsignedInt(struct soap *soap, const char *tag, int id, const unsigned int *p, const char *type, int n) {
  15098. if (soap_element_begin_out(soap, tag, soap_embedded_id(soap, id, p, n), type)
  15099. || soap_string_out(soap, soap_unsignedLong2s(soap, (unsigned long) *p), 0))
  15100. return soap->error;
  15101. return soap_element_end_out(soap, tag);
  15102. }
  15103. /******************************************************************************/
  15104. SOAP_FMAC1
  15105. int
  15106. SOAP_FMAC2
  15107. soap_s2unsignedInt(struct soap *soap, const char *s, unsigned int *p) {
  15108. if (s) {
  15109. char *r;
  15110. if (!*s)
  15111. return soap->error = SOAP_EMPTY;
  15112. #ifndef WITH_NOIO
  15113. #ifndef WITH_LEAN
  15114. soap_reset_errno;
  15115. #endif
  15116. #endif
  15117. *p = (unsigned int) soap_strtoul(s, &r, 10);
  15118. if (s == r || *r
  15119. #ifndef WITH_NOIO
  15120. #ifndef WITH_LEAN
  15121. || soap_errno == SOAP_ERANGE
  15122. #endif
  15123. #endif
  15124. )
  15125. soap->error = SOAP_TYPE;
  15126. #ifdef HAVE_STRTOUL
  15127. if (*p > 0 && strchr(s, '-'))
  15128. return soap->error = SOAP_TYPE;
  15129. #endif
  15130. }
  15131. return soap->error;
  15132. }
  15133. /******************************************************************************/
  15134. SOAP_FMAC1
  15135. unsigned int *
  15136. SOAP_FMAC2
  15137. soap_inunsignedInt(struct soap *soap, const char *tag, unsigned int *p, const char *type, int t) {
  15138. if (soap_element_begin_in(soap, tag, 0, NULL))
  15139. return NULL;
  15140. #ifndef WITH_LEAN
  15141. if (*soap->type
  15142. && soap_match_tag(soap, soap->type, type)
  15143. && soap_match_tag(soap, soap->type, ":unsignedInt")
  15144. && soap_match_tag(soap, soap->type, ":unsignedShort")
  15145. && soap_match_tag(soap, soap->type, ":unsignedByte")) {
  15146. soap->error = SOAP_TYPE;
  15147. soap_revert(soap);
  15148. return NULL;
  15149. }
  15150. #else
  15151. (void)type;
  15152. #endif
  15153. p = (unsigned int *) soap_id_enter(soap, soap->id, p, t, sizeof(unsigned int), NULL, NULL, NULL, NULL);
  15154. if (!p)
  15155. return NULL;
  15156. if (*soap->href != '#') {
  15157. int err = soap_s2unsignedInt(soap, soap_value(soap), p);
  15158. if ((soap->body && soap_element_end_in(soap, tag)) || err)
  15159. return NULL;
  15160. } else {
  15161. p = (unsigned int *) soap_id_forward(soap, soap->href, p, 0, t, t, sizeof(unsigned int), 0, NULL, NULL);
  15162. if (soap->body && soap_element_end_in(soap, tag))
  15163. return NULL;
  15164. }
  15165. return p;
  15166. }
  15167. /******************************************************************************/
  15168. SOAP_FMAC1
  15169. const char *
  15170. SOAP_FMAC2
  15171. soap_unsignedLong2s(struct soap *soap, unsigned long n) {
  15172. (SOAP_SNPRINTF(soap->tmpbuf, sizeof(soap->tmpbuf), 20), "%lu", n);
  15173. return soap->tmpbuf;
  15174. }
  15175. /******************************************************************************/
  15176. SOAP_FMAC1
  15177. int
  15178. SOAP_FMAC2
  15179. soap_outunsignedLong(struct soap *soap, const char *tag, int id, const unsigned long *p, const char *type, int n) {
  15180. if (soap_element_begin_out(soap, tag, soap_embedded_id(soap, id, p, n), type)
  15181. || soap_string_out(soap, soap_unsignedLong2s(soap, *p), 0))
  15182. return soap->error;
  15183. return soap_element_end_out(soap, tag);
  15184. }
  15185. /******************************************************************************/
  15186. SOAP_FMAC1
  15187. int
  15188. SOAP_FMAC2
  15189. soap_s2unsignedLong(struct soap *soap, const char *s, unsigned long *p) {
  15190. if (s) {
  15191. char *r;
  15192. if (!*s)
  15193. return soap->error = SOAP_EMPTY;
  15194. #ifndef WITH_NOIO
  15195. #ifndef WITH_LEAN
  15196. soap_reset_errno;
  15197. #endif
  15198. #endif
  15199. *p = soap_strtoul(s, &r, 10);
  15200. if (s == r || *r
  15201. #ifndef WITH_NOIO
  15202. #ifndef WITH_LEAN
  15203. || soap_errno == SOAP_ERANGE
  15204. #endif
  15205. #endif
  15206. )
  15207. soap->error = SOAP_TYPE;
  15208. #ifdef HAVE_STRTOUL
  15209. if (*p > 0 && strchr(s, '-'))
  15210. return soap->error = SOAP_TYPE;
  15211. #endif
  15212. }
  15213. return soap->error;
  15214. }
  15215. /******************************************************************************/
  15216. SOAP_FMAC1
  15217. unsigned long *
  15218. SOAP_FMAC2
  15219. soap_inunsignedLong(struct soap *soap, const char *tag, unsigned long *p, const char *type, int t) {
  15220. if (soap_element_begin_in(soap, tag, 0, NULL))
  15221. return NULL;
  15222. #ifndef WITH_LEAN
  15223. if (*soap->type
  15224. && soap_match_tag(soap, soap->type, type)
  15225. && soap_match_tag(soap, soap->type, ":unsignedInt")
  15226. && soap_match_tag(soap, soap->type, ":unsignedShort")
  15227. && soap_match_tag(soap, soap->type, ":unsignedByte")) {
  15228. soap->error = SOAP_TYPE;
  15229. soap_revert(soap);
  15230. return NULL;
  15231. }
  15232. #else
  15233. (void)type;
  15234. #endif
  15235. p = (unsigned long *) soap_id_enter(soap, soap->id, p, t, sizeof(unsigned long), NULL, NULL, NULL, NULL);
  15236. if (!p)
  15237. return NULL;
  15238. if (*soap->href != '#') {
  15239. int err = soap_s2unsignedLong(soap, soap_value(soap), p);
  15240. if ((soap->body && soap_element_end_in(soap, tag)) || err)
  15241. return NULL;
  15242. } else {
  15243. p = (unsigned long *) soap_id_forward(soap, soap->href, p, 0, t, t, sizeof(unsigned long), 0, NULL, NULL);
  15244. if (soap->body && soap_element_end_in(soap, tag))
  15245. return NULL;
  15246. }
  15247. return p;
  15248. }
  15249. /******************************************************************************/
  15250. SOAP_FMAC1
  15251. const char *
  15252. SOAP_FMAC2
  15253. soap_ULONG642s(struct soap *soap, ULONG64 n) {
  15254. (SOAP_SNPRINTF(soap->tmpbuf, sizeof(soap->tmpbuf), 20), SOAP_ULONG_FORMAT, n);
  15255. return soap->tmpbuf;
  15256. }
  15257. /******************************************************************************/
  15258. SOAP_FMAC1
  15259. int
  15260. SOAP_FMAC2
  15261. soap_outULONG64(struct soap *soap, const char *tag, int id, const ULONG64 *p, const char *type, int n) {
  15262. if (soap_element_begin_out(soap, tag, soap_embedded_id(soap, id, p, n), type)
  15263. || soap_string_out(soap, soap_ULONG642s(soap, *p), 0))
  15264. return soap->error;
  15265. return soap_element_end_out(soap, tag);
  15266. }
  15267. /******************************************************************************/
  15268. SOAP_FMAC1
  15269. int
  15270. SOAP_FMAC2
  15271. soap_s2ULONG64(struct soap *soap, const char *s, ULONG64 *p) {
  15272. if (s) {
  15273. char *r;
  15274. if (!*s)
  15275. return soap->error = SOAP_EMPTY;
  15276. #ifndef WITH_NOIO
  15277. #ifndef WITH_LEAN
  15278. soap_reset_errno;
  15279. #endif
  15280. #endif
  15281. *p = soap_strtoull(s, &r, 10);
  15282. if (s == r || *r
  15283. #ifndef WITH_NOIO
  15284. #ifndef WITH_LEAN
  15285. || soap_errno == SOAP_ERANGE
  15286. #endif
  15287. #endif
  15288. )
  15289. soap->error = SOAP_TYPE;
  15290. if (*p > 0 && strchr(s, '-'))
  15291. return soap->error = SOAP_TYPE;
  15292. }
  15293. return soap->error;
  15294. }
  15295. /******************************************************************************/
  15296. SOAP_FMAC1
  15297. ULONG64 *
  15298. SOAP_FMAC2
  15299. soap_inULONG64(struct soap *soap, const char *tag, ULONG64 *p, const char *type, int t) {
  15300. if (soap_element_begin_in(soap, tag, 0, NULL))
  15301. return NULL;
  15302. #ifndef WITH_LEAN
  15303. if (*soap->type
  15304. && soap_match_tag(soap, soap->type, type)
  15305. && soap_match_tag(soap, soap->type, ":positiveInteger")
  15306. && soap_match_tag(soap, soap->type, ":nonNegativeInteger")
  15307. && soap_match_tag(soap, soap->type, ":unsignedLong")
  15308. && soap_match_tag(soap, soap->type, ":unsignedInt")
  15309. && soap_match_tag(soap, soap->type, ":unsignedShort")
  15310. && soap_match_tag(soap, soap->type, ":unsignedByte")) {
  15311. soap->error = SOAP_TYPE;
  15312. soap_revert(soap);
  15313. return NULL;
  15314. }
  15315. #else
  15316. (void)type;
  15317. #endif
  15318. p = (ULONG64 *) soap_id_enter(soap, soap->id, p, t, sizeof(ULONG64), NULL, NULL, NULL, NULL);
  15319. if (!p)
  15320. return NULL;
  15321. if (*soap->href != '#') {
  15322. int err = soap_s2ULONG64(soap, soap_value(soap), p);
  15323. if ((soap->body && soap_element_end_in(soap, tag)) || err)
  15324. return NULL;
  15325. } else {
  15326. p = (ULONG64 *) soap_id_forward(soap, soap->href, p, 0, t, t, sizeof(ULONG64), 0, NULL, NULL);
  15327. if (soap->body && soap_element_end_in(soap, tag))
  15328. return NULL;
  15329. }
  15330. return p;
  15331. }
  15332. /******************************************************************************/
  15333. SOAP_FMAC1
  15334. int
  15335. SOAP_FMAC2
  15336. soap_s2char(struct soap *soap, const char *s, char **t, int flag, long minlen, long maxlen, const char *pattern) {
  15337. if (s) {
  15338. const char *r = soap_string(soap, s, flag, minlen, maxlen, pattern);
  15339. if (r && (*t = soap_strdup(soap, r)) == NULL)
  15340. return soap->error = SOAP_EOM;
  15341. }
  15342. return soap->error;
  15343. }
  15344. /******************************************************************************/
  15345. #ifndef WITH_COMPAT
  15346. #ifdef __cplusplus
  15347. SOAP_FMAC1
  15348. int
  15349. SOAP_FMAC2
  15350. soap_s2stdchar(struct soap *soap, const char *s, std::string *t, int flag, long minlen, long maxlen,
  15351. const char *pattern) {
  15352. if (s) {
  15353. const char *r = soap_string(soap, s, flag, minlen, maxlen, pattern);
  15354. if (r)
  15355. t->assign(r);
  15356. }
  15357. return soap->error;
  15358. }
  15359. #endif
  15360. #endif
  15361. /******************************************************************************/
  15362. static const char *
  15363. soap_string(struct soap *soap, const char *s, int flag, long minlen, long maxlen, const char *pattern) {
  15364. if (s) {
  15365. if (maxlen < 0 && soap->maxlength > 0)
  15366. maxlen = soap->maxlength;
  15367. if (minlen > 0 || maxlen >= 0) {
  15368. size_t l;
  15369. if ((soap->mode & SOAP_C_UTFSTRING))
  15370. l = soap_utf8len(s);
  15371. else
  15372. l = strlen(s);
  15373. if ((maxlen >= 0 && l > (size_t) maxlen) || (minlen > 0 && l < (size_t) minlen)) {
  15374. soap->error = SOAP_LENGTH;
  15375. return NULL;
  15376. }
  15377. }
  15378. if (flag >= 4)
  15379. s = soap_collapse(soap, (char *) s, flag, 0);
  15380. #ifndef WITH_LEANER
  15381. if (pattern && soap->fsvalidate) {
  15382. soap->error = soap->fsvalidate(soap, pattern, s);
  15383. if (soap->error)
  15384. return NULL;
  15385. }
  15386. #else
  15387. (void)pattern;
  15388. #endif
  15389. }
  15390. return s;
  15391. }
  15392. /******************************************************************************/
  15393. static char *
  15394. soap_collapse(struct soap *soap, char *s, int flag, int insitu) {
  15395. /* flag 4=normalizedString (replace), 5=token (collapse) */
  15396. char *t;
  15397. size_t n;
  15398. if (!s)
  15399. return NULL;
  15400. if (flag == 4) {
  15401. for (t = s; *t && (!soap_coblank((soap_wchar) *t) || *t == 32); t++)
  15402. continue;
  15403. if (*t) {
  15404. /* replace white space and control chars by blanks */
  15405. if (!insitu)
  15406. s = soap_strdup(soap, s);
  15407. for (t = s; *t; t++)
  15408. if (soap_coblank((soap_wchar) *t))
  15409. *t = ' ';
  15410. }
  15411. return s;
  15412. }
  15413. /* collapse white space */
  15414. for (t = s; *t && soap_coblank((soap_wchar) *t); t++)
  15415. continue;
  15416. n = strlen(t);
  15417. if (insitu && s < t)
  15418. (void) soap_memmove(s, n + 1, t, n + 1);
  15419. else
  15420. s = t;
  15421. if (n > 0) {
  15422. if (!soap_coblank((soap_wchar) s[n - 1])) {
  15423. for (t = s; (*t && !soap_coblank((soap_wchar) *t)) ||
  15424. (*t == 32 && (!t[1] || !soap_coblank((soap_wchar) t[1]))); t++)
  15425. continue;
  15426. if (!*t)
  15427. return s;
  15428. }
  15429. if (!insitu)
  15430. s = soap_strdup(soap, s);
  15431. for (t = s; *t; t++) {
  15432. if (soap_coblank((soap_wchar) *t)) {
  15433. char *r;
  15434. *t = ' ';
  15435. for (r = t + 1; *r && soap_coblank((soap_wchar) *r); r++)
  15436. continue;
  15437. if (r > t + 1)
  15438. (void) soap_memmove(t + 1, n - (t - s), r, n - (r - s) + 1);
  15439. }
  15440. }
  15441. t--;
  15442. if (t >= s && *t == 32)
  15443. *t = '\0';
  15444. }
  15445. return s;
  15446. }
  15447. /******************************************************************************/
  15448. SOAP_FMAC1
  15449. int
  15450. SOAP_FMAC2
  15451. soap_s2QName(struct soap *soap, const char *s, char **t, long minlen, long maxlen, const char *pattern) {
  15452. *t = NULL;
  15453. if (s) {
  15454. const char *r = soap_QName(soap, s, minlen, maxlen, pattern);
  15455. if (r && (*t = soap_strdup(soap, r)) == NULL)
  15456. return soap->error = SOAP_EOM;
  15457. }
  15458. return soap->error;
  15459. }
  15460. /******************************************************************************/
  15461. #ifndef WITH_COMPAT
  15462. #ifdef __cplusplus
  15463. SOAP_FMAC1
  15464. int
  15465. SOAP_FMAC2
  15466. soap_s2stdQName(struct soap *soap, const char *s, std::string *t, long minlen, long maxlen, const char *pattern) {
  15467. t->clear();
  15468. if (s) {
  15469. const char *r = soap_QName(soap, s, minlen, maxlen, pattern);
  15470. if (r)
  15471. t->assign(r);
  15472. }
  15473. return soap->error;
  15474. }
  15475. #endif
  15476. #endif
  15477. /******************************************************************************/
  15478. static const char *
  15479. soap_QName(struct soap *soap, const char *s, long minlen, long maxlen, const char *pattern) {
  15480. if (s) {
  15481. char *b;
  15482. if (maxlen < 0 && soap->maxlength > 0)
  15483. maxlen = soap->maxlength;
  15484. if (minlen > 0 || maxlen >= 0) {
  15485. size_t l;
  15486. if ((soap->mode & SOAP_C_UTFSTRING))
  15487. l = soap_utf8len(s);
  15488. else
  15489. l = strlen(s);
  15490. if ((maxlen >= 0 && l > (size_t) maxlen) || (minlen > 0 && l < (size_t) minlen)) {
  15491. soap->error = SOAP_LENGTH;
  15492. return NULL;
  15493. }
  15494. }
  15495. #ifdef WITH_FAST
  15496. soap->labidx = 0;
  15497. #else
  15498. if (soap_alloc_block(soap) == NULL)
  15499. return NULL;
  15500. #endif
  15501. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Normalized namespace(s) of QNames '%s'", s));
  15502. /* convert (by prefix normalize prefix) all QNames in s */
  15503. for (;;) {
  15504. size_t n;
  15505. struct soap_nlist *np;
  15506. const char *p = NULL;
  15507. short flag = 0;
  15508. const char *r = NULL;
  15509. size_t m = 0;
  15510. #ifndef WITH_FAST
  15511. size_t k = 0;
  15512. #endif
  15513. /* skip blanks */
  15514. while (*s && soap_coblank((soap_wchar) *s))
  15515. s++;
  15516. if (!*s)
  15517. break;
  15518. /* find next QName */
  15519. n = 1;
  15520. while (s[n] && !soap_coblank((soap_wchar) s[n]))
  15521. n++;
  15522. np = soap->nlist;
  15523. /* if there is no namespace stack, or prefix is "#" or "xml" then copy string */
  15524. if (!np || *s == '#' || !strncmp(s, "xml:", 4)) {
  15525. r = s;
  15526. m = n;
  15527. } else /* we normalize the QName by replacing its prefix */
  15528. {
  15529. const char *q;
  15530. for (p = s; *p && p < s + n; p++)
  15531. if (*p == ':')
  15532. break;
  15533. if (*p == ':') {
  15534. size_t k = p - s;
  15535. while (np && (strncmp(np->id, s, k) || np->id[k]))
  15536. np = np->next;
  15537. p++;
  15538. } else {
  15539. while (np && *np->id)
  15540. np = np->next;
  15541. p = s;
  15542. }
  15543. /* replace prefix */
  15544. if (np) {
  15545. if (np->index >= 0 && soap->local_namespaces &&
  15546. (q = soap->local_namespaces[np->index].id) != NULL) {
  15547. size_t k = strlen(q);
  15548. if (q[k - 1] != '_') {
  15549. r = q;
  15550. m = k;
  15551. } else {
  15552. flag = 1;
  15553. r = soap->local_namespaces[np->index].ns;
  15554. m = strlen(r);
  15555. }
  15556. } else if (np->ns) {
  15557. flag = 1;
  15558. r = np->ns;
  15559. m = strlen(r);
  15560. } else {
  15561. DBGLOG(TEST,
  15562. SOAP_MESSAGE(fdebug, "\nNamespace prefix of '%s' not defined (index=%d, URI='%s')\n", s,
  15563. np->index, np->ns ? np->ns : SOAP_STR_EOS));
  15564. soap->error = SOAP_NAMESPACE;
  15565. return NULL;
  15566. }
  15567. } else if (s[n]) /* no namespace, part of string */
  15568. {
  15569. r = s;
  15570. m = n;
  15571. } else /* no namespace: assume "" namespace */
  15572. {
  15573. flag = 1;
  15574. }
  15575. }
  15576. #ifdef WITH_FAST
  15577. if ((flag && soap_append_lab(soap, "\"", 1))
  15578. || (m && soap_append_lab(soap, r, m))
  15579. || (flag && soap_append_lab(soap, "\"", 1))
  15580. || (p && (soap_append_lab(soap, ":", 1) || soap_append_lab(soap, p, n - (p - s)))))
  15581. return NULL;
  15582. #else
  15583. k = 2*flag + m + (p ? n - (p-s) + 1 : 0) + (s[n] != '\0');
  15584. b = (char*)soap_push_block(soap, NULL, k);
  15585. if (!b)
  15586. return NULL;
  15587. if (flag)
  15588. *b++ = '"';
  15589. if (m)
  15590. {
  15591. if (soap_memcpy((void*)b, k, (const void*)r, m))
  15592. {
  15593. soap->error = SOAP_EOM;
  15594. return NULL;
  15595. }
  15596. b += m;
  15597. }
  15598. if (flag)
  15599. *b++ = '"';
  15600. if (p)
  15601. {
  15602. *b++ = ':';
  15603. if (soap_memcpy((void*)b, k - m - flag - 1, (const void*)p, n - (p-s)))
  15604. {
  15605. soap->error = SOAP_EOM;
  15606. return NULL;
  15607. }
  15608. b += n - (p-s);
  15609. }
  15610. #endif
  15611. /* advance to next and add spacing */
  15612. s += n;
  15613. while (*s && soap_coblank(*s))
  15614. s++;
  15615. if (*s) {
  15616. #ifdef WITH_FAST
  15617. if (soap_append_lab(soap, " ", 1))
  15618. return NULL;
  15619. #else
  15620. *b = ' ';
  15621. #endif
  15622. }
  15623. }
  15624. #ifdef WITH_FAST
  15625. if (soap_append_lab(soap, SOAP_STR_EOS, 1))
  15626. return NULL;
  15627. b = soap->labbuf;
  15628. #else
  15629. b = (char*)soap_push_block(soap, NULL, 1);
  15630. if (!b)
  15631. return NULL;
  15632. *b = '\0';
  15633. b = (char*)soap_save_block(soap, NULL, NULL, 0);
  15634. #endif
  15635. #ifndef WITH_LEANER
  15636. if (pattern && soap->fsvalidate) {
  15637. soap->error = soap->fsvalidate(soap, pattern, b);
  15638. if (soap->error)
  15639. return NULL;
  15640. }
  15641. #else
  15642. (void)pattern;
  15643. #endif
  15644. return b;
  15645. }
  15646. return NULL;
  15647. }
  15648. /******************************************************************************/
  15649. SOAP_FMAC1
  15650. const char *
  15651. SOAP_FMAC2
  15652. soap_QName2s(struct soap *soap, const char *s) {
  15653. const char *t = NULL;
  15654. if (s) {
  15655. #ifdef WITH_FAST
  15656. soap_store_lab(soap, SOAP_STR_EOS, 1);
  15657. soap->labidx = 0;
  15658. #else
  15659. char *b = NULL;
  15660. if (soap_alloc_block(soap) == NULL)
  15661. return NULL;
  15662. #endif
  15663. for (;;) {
  15664. size_t n;
  15665. const char *q = NULL;
  15666. const char *r = NULL;
  15667. size_t m = 0;
  15668. #ifndef WITH_FAST
  15669. size_t k = 0;
  15670. #endif
  15671. /* skip blanks */
  15672. while (*s && soap_coblank((soap_wchar) *s))
  15673. s++;
  15674. if (!*s) {
  15675. #ifdef WITH_FAST
  15676. soap->labbuf[soap->labidx > 0 ? soap->labidx - 1 : 0] = '\0';
  15677. #else
  15678. if (!b)
  15679. return soap_strdup(soap, SOAP_STR_EOS);
  15680. --b;
  15681. *b = '\0';
  15682. #endif
  15683. break;
  15684. }
  15685. /* find next QName */
  15686. n = 0;
  15687. while (s[n] && !soap_coblank((soap_wchar) s[n])) {
  15688. if (s[n] == ':')
  15689. r = s;
  15690. n++;
  15691. }
  15692. if (*s != '"') /* non-quoted: pass string as is */
  15693. {
  15694. #ifndef WITH_LEAN
  15695. if (r && (soap->mode & SOAP_XML_CANONICAL) && !(soap->mode & SOAP_XML_CANONICAL_NA))
  15696. soap_utilize_ns(soap, s, 1);
  15697. #endif
  15698. r = s;
  15699. m = n + 1;
  15700. } else /* prefix quoted URI-based string */
  15701. {
  15702. q = strchr(s + 1, '"');
  15703. if (q) {
  15704. struct Namespace *p = soap->local_namespaces;
  15705. if (p) {
  15706. for (; p->id; p++) {
  15707. if (p->ns)
  15708. if (!soap_tag_cmp(s + 1, p->ns))
  15709. break;
  15710. if (p->in)
  15711. if (!soap_tag_cmp(s + 1, p->in))
  15712. break;
  15713. }
  15714. }
  15715. q++;
  15716. /* URL is in the namespace table? */
  15717. if (p && p->id) {
  15718. r = p->id;
  15719. m = strlen(r);
  15720. } else /* not in namespace table: create xmlns binding */
  15721. {
  15722. char *x = soap_strdup(soap, s + 1);
  15723. if (!x)
  15724. return NULL;
  15725. x[q - s - 2] = '\0';
  15726. (SOAP_SNPRINTF(soap->tmpbuf, sizeof(soap->tmpbuf), 27), "xmlns:_%d", soap->idnum++);
  15727. soap_set_attr(soap, soap->tmpbuf, x, 1);
  15728. r = soap->tmpbuf + 6;
  15729. m = strlen(r);
  15730. }
  15731. }
  15732. }
  15733. /* copy normalized QName into buffer, including the ending blank or NUL */
  15734. #ifdef WITH_FAST
  15735. if ((m && soap_append_lab(soap, r, m))
  15736. || (q && soap_append_lab(soap, q, n - (q - s) + 1)))
  15737. return NULL;
  15738. #else
  15739. k = m + (q ? n - (q - s) + 1 : 0);
  15740. b = (char*)soap_push_block(soap, NULL, k);
  15741. if (!b)
  15742. {
  15743. soap->error = SOAP_EOM;
  15744. return NULL;
  15745. }
  15746. if (soap_memcpy((void*)b, k, (const void*)r, m))
  15747. {
  15748. soap->error = SOAP_EOM;
  15749. return NULL;
  15750. }
  15751. b += m;
  15752. if (q)
  15753. {
  15754. if (soap_memcpy((void*)b, k - m, (const void*)q, n - (q - s) + 1))
  15755. {
  15756. soap->error = SOAP_EOM;
  15757. return NULL;
  15758. }
  15759. b += n - (q - s) + 1;
  15760. }
  15761. #endif
  15762. /* advance to next */
  15763. s += n;
  15764. }
  15765. #ifdef WITH_FAST
  15766. t = soap_strdup(soap, soap->labbuf);
  15767. if (!t)
  15768. soap->error = SOAP_EOM;
  15769. #else
  15770. t = (char*)soap_save_block(soap, NULL, NULL, 0);
  15771. #endif
  15772. }
  15773. return t;
  15774. }
  15775. /******************************************************************************/
  15776. #ifndef WITH_LEAN
  15777. SOAP_FMAC1
  15778. int
  15779. SOAP_FMAC2
  15780. soap_s2wchar(struct soap *soap, const char *s, wchar_t **t, int flag, long minlen, long maxlen, const char *pattern) {
  15781. if (s) {
  15782. const wchar_t *r = soap_wstring(soap, s, flag, minlen, maxlen, pattern);
  15783. if (r && (*t = soap_wstrdup(soap, r)) == NULL)
  15784. return soap->error = SOAP_EOM;
  15785. }
  15786. return soap->error;
  15787. }
  15788. #endif
  15789. /******************************************************************************/
  15790. #ifndef WITH_COMPAT
  15791. #ifdef __cplusplus
  15792. #ifndef WITH_LEAN
  15793. SOAP_FMAC1
  15794. int
  15795. SOAP_FMAC2
  15796. soap_s2stdwchar(struct soap *soap, const char *s, std::wstring *t, int flag, long minlen, long maxlen,
  15797. const char *pattern) {
  15798. if (s) {
  15799. const wchar_t *r = soap_wstring(soap, s, flag, minlen, maxlen, pattern);
  15800. if (r)
  15801. t->assign(r);
  15802. }
  15803. return soap->error;
  15804. }
  15805. #endif
  15806. #endif
  15807. #endif
  15808. /******************************************************************************/
  15809. #ifndef WITH_LEAN
  15810. static const wchar_t *
  15811. soap_wstring(struct soap *soap, const char *s, int flag, long minlen, long maxlen, const char *pattern) {
  15812. if (s) {
  15813. size_t l;
  15814. soap_wchar c;
  15815. wchar_t *t;
  15816. if (maxlen < 0 && soap->maxlength > 0)
  15817. maxlen = soap->maxlength;
  15818. soap->labidx = 0;
  15819. if ((soap->mode & SOAP_ENC_LATIN)) {
  15820. wchar_t *r;
  15821. if (soap_append_lab(soap, NULL, sizeof(wchar_t) * (strlen(s) + 1)))
  15822. return NULL;
  15823. r = (wchar_t *) soap->labbuf;
  15824. while (*s)
  15825. *r++ = (wchar_t) *s++;
  15826. } else {
  15827. /* Convert UTF8 to wchar_t */
  15828. while (*s) {
  15829. c = (unsigned char) *s++;
  15830. if (c >= 0x80) {
  15831. #ifdef WITH_REPLACE_ILLEGAL_UTF8
  15832. soap_wchar c1, c2, c3;
  15833. c1 = (unsigned char)*s;
  15834. if (c <= 0xC1 || (c1 & 0xC0) != 0x80)
  15835. {
  15836. c = SOAP_UNKNOWN_UNICODE_CHAR;
  15837. }
  15838. else
  15839. {
  15840. ++s;
  15841. c1 &= 0x3F;
  15842. if (c < 0xE0)
  15843. {
  15844. c = (((c & 0x1F) << 6) | c1);
  15845. }
  15846. else
  15847. {
  15848. c2 = (unsigned char)*s;
  15849. if ((c == 0xE0 && c1 < 0x20) || (c2 & 0xC0) != 0x80)
  15850. {
  15851. c = SOAP_UNKNOWN_UNICODE_CHAR;
  15852. }
  15853. else
  15854. {
  15855. ++s;
  15856. c2 &= 0x3F;
  15857. if (c < 0xF0)
  15858. {
  15859. c = (((c & 0x0F) << 12) | (c1 << 6) | c2);
  15860. }
  15861. else
  15862. {
  15863. c3 = (unsigned char)*s;
  15864. if ((c == 0xF0 && c1 < 0x10) || (c == 0xF4 && c1 >= 0x10) || c >= 0xF5 || (c3 & 0xC0) != 0x80)
  15865. {
  15866. c = SOAP_UNKNOWN_UNICODE_CHAR;
  15867. }
  15868. else
  15869. {
  15870. ++s;
  15871. c = (((c & 0x07) << 18) | (c1 << 12) | (c2 << 6) | (c3 & 0x3F));
  15872. }
  15873. }
  15874. }
  15875. }
  15876. }
  15877. #else
  15878. soap_wchar c1, c2, c3, c4;
  15879. c1 = (unsigned char) *s;
  15880. if (c1) {
  15881. s++;
  15882. c1 &= 0x3F;
  15883. if (c < 0xE0) {
  15884. c = (wchar_t) (((soap_wchar) (c & 0x1F) << 6) | c1);
  15885. } else {
  15886. c2 = (unsigned char) *s;
  15887. if (c2) {
  15888. s++;
  15889. c2 &= 0x3F;
  15890. if (c < 0xF0) {
  15891. c = (wchar_t) (((soap_wchar) (c & 0x0F) << 12) | (c1 << 6) | c2);
  15892. } else {
  15893. c3 = (unsigned char) *s;
  15894. if (c3) {
  15895. s++;
  15896. c3 &= 0x3F;
  15897. if (c < 0xF8) {
  15898. c = (wchar_t) (((soap_wchar) (c & 0x07) << 18) | (c1 << 12) | (c2 << 6) |
  15899. c3);
  15900. } else {
  15901. c4 = (unsigned char) *s;
  15902. if (c4) {
  15903. s++;
  15904. c4 &= 0x3F;
  15905. if (c < 0xFC) {
  15906. c = (wchar_t) (((soap_wchar) (c & 0x03) << 24) | (c1 << 18) |
  15907. (c2 << 12) | (c3 << 6) | c4);
  15908. } else {
  15909. c = (wchar_t) (((soap_wchar) (c & 0x01) << 30) | (c1 << 24) |
  15910. (c2 << 18) | (c3 << 12) | (c4 << 6) |
  15911. (unsigned char) (*s & 0x3F));
  15912. if (*s)
  15913. s++;
  15914. }
  15915. }
  15916. }
  15917. }
  15918. }
  15919. }
  15920. }
  15921. }
  15922. #endif
  15923. }
  15924. /* use UTF16 encoding when wchar_t is too small to hold UCS */
  15925. if (sizeof(wchar_t) < 4 && c > 0xFFFF) {
  15926. wchar_t c1, c2;
  15927. c1 = 0xD800 - (0x10000 >> 10) + (c >> 10);
  15928. c2 = 0xDC00 + (c & 0x3FF);
  15929. if (soap_append_lab(soap, (const char *) &c1, sizeof(wchar_t)) ||
  15930. soap_append_lab(soap, (const char *) &c2, sizeof(wchar_t)))
  15931. return NULL;
  15932. } else if (soap_append_lab(soap, (const char *) &c, sizeof(wchar_t))) {
  15933. return NULL;
  15934. }
  15935. }
  15936. }
  15937. l = soap->labidx / sizeof(wchar_t);
  15938. c = L'\0';
  15939. if (soap_append_lab(soap, (const char *) &c, sizeof(wchar_t)))
  15940. return NULL;
  15941. if ((maxlen >= 0 && l > (size_t) maxlen) || (minlen > 0 && l < (size_t) minlen)) {
  15942. soap->error = SOAP_LENGTH;
  15943. return NULL;
  15944. }
  15945. t = (wchar_t *) soap->labbuf;
  15946. #ifndef WITH_LEAN
  15947. if (flag >= 4 && t)
  15948. t = soap_wcollapse(soap, t, flag, 1);
  15949. #endif
  15950. #ifndef WITH_LEANER
  15951. if (pattern && soap->fwvalidate) {
  15952. soap->error = soap->fwvalidate(soap, pattern, t);
  15953. if (soap->error)
  15954. return NULL;
  15955. }
  15956. #endif
  15957. return t;
  15958. }
  15959. return NULL;
  15960. }
  15961. #endif
  15962. /******************************************************************************/
  15963. #ifndef WITH_LEAN
  15964. static wchar_t *
  15965. soap_wcollapse(struct soap *soap, wchar_t *s, int flag, int insitu) {
  15966. /* flag 4=normalizedString (replace), 5=token (collapse) */
  15967. wchar_t *t;
  15968. size_t n;
  15969. if (!s)
  15970. return NULL;
  15971. if (flag == 4) {
  15972. for (t = s; *t && (!soap_coblank((soap_wchar) *t) || *t == 32); t++)
  15973. continue;
  15974. if (*t) {
  15975. /* replace blanks and control char by space */
  15976. if (!insitu)
  15977. s = soap_wstrdup(soap, s);
  15978. if (s)
  15979. for (t = s; *t; t++)
  15980. if (soap_coblank((soap_wchar) *t))
  15981. *t = L' ';
  15982. }
  15983. return s;
  15984. }
  15985. /* collapse white space */
  15986. for (t = s; *t && soap_coblank((soap_wchar) *t); t++)
  15987. continue;
  15988. n = 0;
  15989. while (t[n])
  15990. n++;
  15991. if (insitu && s < t)
  15992. (void) soap_memmove(s, n + 1, t, n + 1);
  15993. else
  15994. s = t;
  15995. if (n > 0) {
  15996. if (!soap_coblank((soap_wchar) s[n - 1])) {
  15997. for (t = s; (*t && !soap_coblank((soap_wchar) *t)) ||
  15998. (*t == 32 && (!t[1] || !soap_coblank((soap_wchar) t[1]))); t++)
  15999. continue;
  16000. if (!*t)
  16001. return s;
  16002. }
  16003. if (!insitu)
  16004. s = soap_wstrdup(soap, s);
  16005. if (s) {
  16006. for (t = s; *t; t++) {
  16007. if (soap_coblank((soap_wchar) *t)) {
  16008. wchar_t *r;
  16009. *t = L' ';
  16010. for (r = t + 1; *r && soap_coblank((soap_wchar) *r); r++)
  16011. continue;
  16012. if (r > t + 1)
  16013. (void) soap_memmove(t + 1, sizeof(wchar_t) * (n - (t - s)), r,
  16014. sizeof(wchar_t) * (n - (r - s) + 1));
  16015. }
  16016. }
  16017. t--;
  16018. if (t >= s && *t == 32)
  16019. *t = L'\0';
  16020. }
  16021. }
  16022. return s;
  16023. }
  16024. #endif
  16025. /******************************************************************************/
  16026. #ifndef WITH_LEAN
  16027. SOAP_FMAC1
  16028. const char *
  16029. SOAP_FMAC2
  16030. soap_wchar2s(struct soap *soap, const wchar_t *s) {
  16031. soap_wchar c;
  16032. char *r, *t;
  16033. const wchar_t *q = s;
  16034. size_t n = 0;
  16035. if (!s)
  16036. return NULL;
  16037. while ((c = *q++)) {
  16038. if (c > 0 && c < 0x80)
  16039. n++;
  16040. else
  16041. #ifdef WITH_REPLACE_ILLEGAL_UTF8
  16042. n += 4;
  16043. #else
  16044. n += 6;
  16045. #endif
  16046. }
  16047. r = t = (char *) soap_malloc(soap, n + 1);
  16048. if (r) {
  16049. /* Convert wchar to UTF8 (chars above U+10FFFF are silently converted, but should not be used) */
  16050. while ((c = *s++)) {
  16051. if (c > 0 && c < 0x80) {
  16052. *t++ = (char) c;
  16053. } else {
  16054. /* check for UTF16 encoding when wchar_t is too small to hold UCS */
  16055. if (sizeof(wchar_t) < 4 && (c & 0xFC00) == 0xD800) {
  16056. soap_wchar d = *s;
  16057. if ((d & 0xFC00) == 0xDC00) {
  16058. c = ((c - 0xD800) << 10) + (d - 0xDC00) + 0x10000;
  16059. s++;
  16060. }
  16061. #ifdef WITH_REPLACE_ILLEGAL_UTF8
  16062. else
  16063. {
  16064. c = SOAP_UNKNOWN_UNICODE_CHAR; /* Malformed UTF-16 */
  16065. }
  16066. #endif
  16067. }
  16068. if (c < 0x0800) {
  16069. *t++ = (char) (0xC0 | ((c >> 6) & 0x1F));
  16070. } else {
  16071. #ifdef WITH_REPLACE_ILLEGAL_UTF8
  16072. if (!((c >= 0x80 && c <= 0xD7FF) || (c >= 0xE000 && c <= 0xFFFD) || (c >= 0x10000 && c <= 0x10FFFF)))
  16073. c = SOAP_UNKNOWN_UNICODE_CHAR;
  16074. #endif
  16075. if (c < 0x010000) {
  16076. *t++ = (char) (0xE0 | ((c >> 12) & 0x0F));
  16077. } else {
  16078. if (c < 0x200000) {
  16079. *t++ = (char) (0xF0 | ((c >> 18) & 0x07));
  16080. } else {
  16081. if (c < 0x04000000) {
  16082. *t++ = (char) (0xF8 | ((c >> 24) & 0x03));
  16083. } else {
  16084. *t++ = (char) (0xFC | ((c >> 30) & 0x01));
  16085. *t++ = (char) (0x80 | ((c >> 24) & 0x3F));
  16086. }
  16087. *t++ = (char) (0x80 | ((c >> 18) & 0x3F));
  16088. }
  16089. *t++ = (char) (0x80 | ((c >> 12) & 0x3F));
  16090. }
  16091. *t++ = (char) (0x80 | ((c >> 6) & 0x3F));
  16092. }
  16093. *t++ = (char) (0x80 | (c & 0x3F));
  16094. }
  16095. }
  16096. *t = '\0';
  16097. }
  16098. return r;
  16099. }
  16100. #endif
  16101. /******************************************************************************/
  16102. SOAP_FMAC1
  16103. int
  16104. SOAP_FMAC2
  16105. soap_outstring(struct soap *soap, const char *tag, int id, char *const *p, const char *type, int n) {
  16106. id = soap_element_id(soap, tag, id, *p, NULL, 0, type, n, NULL);
  16107. if (id < 0)
  16108. return soap->error;
  16109. if (!**p && (soap->mode & SOAP_C_NILSTRING))
  16110. return soap_element_null(soap, tag, id, type);
  16111. if (soap_element_begin_out(soap, tag, id, type)
  16112. || soap_string_out(soap, *p, 0)
  16113. || soap_element_end_out(soap, tag))
  16114. return soap->error;
  16115. return SOAP_OK;
  16116. }
  16117. /******************************************************************************/
  16118. SOAP_FMAC1
  16119. char **
  16120. SOAP_FMAC2
  16121. soap_instring(struct soap *soap, const char *tag, char **p, const char *type, int t, int flag, long minlen, long maxlen,
  16122. const char *pattern) {
  16123. (void) type;
  16124. if (soap_element_begin_in(soap, tag, 1, NULL)) {
  16125. if (!tag || *tag != '-' || soap->error != SOAP_NO_TAG)
  16126. return NULL;
  16127. soap->error = SOAP_OK;
  16128. }
  16129. if (!p) {
  16130. p = (char **) soap_malloc(soap, sizeof(char *));
  16131. if (!p)
  16132. return NULL;
  16133. }
  16134. if (soap->null) {
  16135. *p = NULL;
  16136. } else if (soap->body) {
  16137. *p = soap_string_in(soap, flag, minlen, maxlen, pattern);
  16138. if (!*p || !(char *) soap_id_enter(soap, soap->id, *p, t, sizeof(char *), NULL, NULL, NULL, NULL))
  16139. return NULL;
  16140. if (!**p && tag && *tag == '-') {
  16141. soap->error = SOAP_NO_TAG;
  16142. return NULL;
  16143. }
  16144. } else if (tag && *tag == '-') {
  16145. soap->error = SOAP_NO_TAG;
  16146. return NULL;
  16147. } else if (*soap->href != '#') {
  16148. if (minlen > 0) {
  16149. soap->error = SOAP_LENGTH;
  16150. return NULL;
  16151. }
  16152. *p = soap_strdup(soap, SOAP_STR_EOS);
  16153. if (!*p)
  16154. return NULL;
  16155. }
  16156. if (*soap->href == '#')
  16157. p = (char **) soap_id_lookup(soap, soap->href, (void **) p, t, sizeof(char **), 0, NULL);
  16158. if (soap->body && soap_element_end_in(soap, tag))
  16159. return NULL;
  16160. return p;
  16161. }
  16162. /******************************************************************************/
  16163. #ifndef WITH_LEANER
  16164. SOAP_FMAC1
  16165. int
  16166. SOAP_FMAC2
  16167. soap_outwstring(struct soap *soap, const char *tag, int id, wchar_t *const *p, const char *type, int n) {
  16168. id = soap_element_id(soap, tag, id, *p, NULL, 0, type, n, NULL);
  16169. if (id < 0)
  16170. return soap->error;
  16171. if (!**p && (soap->mode & SOAP_C_NILSTRING))
  16172. return soap_element_null(soap, tag, id, type);
  16173. if (soap_element_begin_out(soap, tag, id, type)
  16174. || soap_wstring_out(soap, *p, 0)
  16175. || soap_element_end_out(soap, tag))
  16176. return soap->error;
  16177. return SOAP_OK;
  16178. }
  16179. #endif
  16180. /******************************************************************************/
  16181. #ifndef WITH_LEANER
  16182. SOAP_FMAC1
  16183. wchar_t **
  16184. SOAP_FMAC2
  16185. soap_inwstring(struct soap *soap, const char *tag, wchar_t **p, const char *type, int t, int flag, long minlen,
  16186. long maxlen, const char *pattern) {
  16187. (void) type;
  16188. if (soap_element_begin_in(soap, tag, 1, NULL)) {
  16189. if (!tag || *tag != '-' || soap->error != SOAP_NO_TAG)
  16190. return NULL;
  16191. soap->error = SOAP_OK;
  16192. }
  16193. if (!p) {
  16194. p = (wchar_t **) soap_malloc(soap, sizeof(wchar_t *));
  16195. if (!p)
  16196. return NULL;
  16197. }
  16198. if (soap->null) {
  16199. *p = NULL;
  16200. } else if (soap->body) {
  16201. *p = soap_wstring_in(soap, flag, minlen, maxlen, pattern);
  16202. if (!*p || !(wchar_t *) soap_id_enter(soap, soap->id, *p, t, sizeof(wchar_t *), NULL, NULL, NULL, NULL))
  16203. return NULL;
  16204. if (!**p && tag && *tag == '-') {
  16205. soap->error = SOAP_NO_TAG;
  16206. return NULL;
  16207. }
  16208. } else if (tag && *tag == '-') {
  16209. soap->error = SOAP_NO_TAG;
  16210. return NULL;
  16211. } else if (*soap->href != '#') {
  16212. if (minlen > 0) {
  16213. soap->error = SOAP_LENGTH;
  16214. return NULL;
  16215. }
  16216. *p = soap_wstrdup(soap, L"");
  16217. }
  16218. if (*soap->href == '#')
  16219. p = (wchar_t **) soap_id_lookup(soap, soap->href, (void **) p, t, sizeof(wchar_t **), 0, NULL);
  16220. if (soap->body && soap_element_end_in(soap, tag))
  16221. return NULL;
  16222. return p;
  16223. }
  16224. #endif
  16225. /******************************************************************************/
  16226. #ifndef WITH_LEAN
  16227. #ifdef UNDER_CE
  16228. /* WinCE mktime (based on the mingw-runtime, public domain) */
  16229. #define __FILETIME_to_ll(f) ((long long)(f).dwHighDateTime << 32 | (long long)(f).dwLowDateTime)
  16230. static time_t
  16231. mktime(struct tm *pt)
  16232. {
  16233. SYSTEMTIME s, s1, s2;
  16234. FILETIME f, f1, f2;
  16235. long long diff;
  16236. GetSystemTime(&s1);
  16237. GetLocalTime(&s2);
  16238. SystemTimeToFileTime(&s1, &f1);
  16239. SystemTimeToFileTime(&s2, &f2);
  16240. diff = (__FILETIME_to_ll(f2) - __FILETIME_to_ll(f1)) / 10000000LL;
  16241. s.wYear = pt->tm_year + 1900;
  16242. s.wMonth = pt->tm_mon + 1;
  16243. s.wDayOfWeek = pt->tm_wday;
  16244. s.wDay = pt->tm_mday;
  16245. s.wHour = pt->tm_hour;
  16246. s.wMinute = pt->tm_min;
  16247. s.wSecond = pt->tm_sec;
  16248. s.wMilliseconds = 0;
  16249. SystemTimeToFileTime(&s, &f);
  16250. return (time_t)((__FILETIME_to_ll(f) - 116444736000000000LL) / 10000000LL) - (time_t)diff;
  16251. }
  16252. #endif
  16253. #endif
  16254. /******************************************************************************/
  16255. #ifndef WITH_LEAN
  16256. #ifdef UNDER_CE
  16257. /* WinCE gmtime_r (based on the mingw-runtime, public domain) */
  16258. #define HAVE_GMTIME_R
  16259. static struct tm*
  16260. gmtime_r(const time_t *t, struct tm *pt)
  16261. {
  16262. FILETIME f, f1, f2;
  16263. SYSTEMTIME s, s1 = {0};
  16264. long long time = (long long)(*t) * 10000000LL + 116444736000000000LL;
  16265. f.dwHighDateTime = (DWORD)((time >> 32) & 0x00000000FFFFFFFF);
  16266. f.dwLowDateTime = (DWORD)(time & 0x00000000FFFFFFFF);
  16267. FileTimeToSystemTime(&f, &s);
  16268. pt->tm_year = s.wYear - 1900;
  16269. pt->tm_mon = s.wMonth - 1;
  16270. pt->tm_wday = s.wDayOfWeek;
  16271. pt->tm_mday = s.wDay;
  16272. s1.wYear = s.wYear;
  16273. s1.wMonth = 1;
  16274. s1.wDayOfWeek = 1;
  16275. s1.wDay = 1;
  16276. SystemTimeToFileTime(&s1, &f1);
  16277. SystemTimeToFileTime(&s, &f2);
  16278. pt->tm_yday = (((__FILETIME_to_ll(f2) - __FILETIME_to_ll(f1)) / 10000000LL) / (60 * 60 * 24));
  16279. pt->tm_hour = s.wHour;
  16280. pt->tm_min = s.wMinute;
  16281. pt->tm_sec = s.wSecond;
  16282. pt->tm_isdst = 0;
  16283. return pt;
  16284. }
  16285. #endif
  16286. #endif
  16287. /******************************************************************************/
  16288. #ifndef WITH_LEAN
  16289. #ifdef UNDER_CE
  16290. /* WinCE very simple strftime for format "%Y-%m-%dT%H:%M:%SZ", note: %F and %T not supported by MS */
  16291. static size_t
  16292. strftime(char *buf, size_t len, const char *format, const struct tm *pT)
  16293. {
  16294. (void)len; (void)format;
  16295. #ifndef WITH_NOZONE
  16296. (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);
  16297. #else
  16298. (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);
  16299. #endif
  16300. return len;
  16301. }
  16302. #endif
  16303. #endif
  16304. /******************************************************************************/
  16305. #if !defined(WITH_LEAN) || defined(WITH_COOKIES)
  16306. SOAP_FMAC1
  16307. time_t
  16308. SOAP_FMAC2
  16309. soap_timegm(struct tm *T) {
  16310. #if defined(HAVE_TIMEGM)
  16311. return timegm(T);
  16312. #else
  16313. time_t t, g, z;
  16314. struct tm tm;
  16315. #ifndef HAVE_GMTIME_R
  16316. struct tm *tp;
  16317. #endif
  16318. t = mktime(T);
  16319. if (t == (time_t)-1)
  16320. return (time_t)-1;
  16321. #ifdef HAVE_GMTIME_R
  16322. if (gmtime_r(&t, &tm) == SOAP_FUNC_R_ERR)
  16323. return (time_t)-1;
  16324. #else
  16325. tp = gmtime(&t);
  16326. if (!tp)
  16327. return (time_t)-1;
  16328. tm = *tp;
  16329. #endif
  16330. tm.tm_isdst = 0;
  16331. g = mktime(&tm);
  16332. if (g == (time_t)-1)
  16333. return (time_t)-1;
  16334. z = g - t;
  16335. return t - z;
  16336. #endif
  16337. }
  16338. #endif
  16339. /******************************************************************************/
  16340. #ifndef WITH_LEAN
  16341. SOAP_FMAC1
  16342. const char *
  16343. SOAP_FMAC2
  16344. soap_dateTime2s(struct soap *soap, time_t n) {
  16345. struct tm T, *pT = &T;
  16346. size_t l = 0;
  16347. #if defined(HAVE_GMTIME_R) && !defined(WITH_NOZONE)
  16348. if (gmtime_r(&n, pT) != SOAP_FUNC_R_ERR)
  16349. l = strftime(soap->tmpbuf, sizeof(soap->tmpbuf), "%Y-%m-%dT%H:%M:%SZ", pT);
  16350. #elif defined(HAVE_GMTIME) && !defined(WITH_NOZONE)
  16351. pT = gmtime(&n);
  16352. if (pT)
  16353. l = strftime(soap->tmpbuf, sizeof(soap->tmpbuf), "%Y-%m-%dT%H:%M:%SZ", pT);
  16354. #elif (defined(HAVE_TM_GMTOFF) || defined(HAVE_STRUCT_TM_TM_GMTOFF) || defined(HAVE_STRUCT_TM___TM_GMTOFF)) && !defined(WITH_NOZONE)
  16355. #if defined(HAVE_LOCALTIME_R)
  16356. if (localtime_r(&n, pT) != SOAP_FUNC_R_ERR)
  16357. {
  16358. l = strftime(soap->tmpbuf, sizeof(soap->tmpbuf), "%Y-%m-%dT%H:%M:%S%z", pT);
  16359. if (l)
  16360. {
  16361. (void)soap_memmove(soap->tmpbuf + 23, sizeof(soap->tmpbuf) - 23, soap->tmpbuf + 22, 3); /* 2000-03-01T02:00:00+0300 */
  16362. soap->tmpbuf[22] = ':'; /* 2000-03-01T02:00:00+03:00 */
  16363. }
  16364. }
  16365. #else
  16366. pT = localtime(&n);
  16367. if (pT)
  16368. {
  16369. l = strftime(soap->tmpbuf, sizeof(soap->tmpbuf), "%Y-%m-%dT%H:%M:%S%z", pT);
  16370. if (l)
  16371. {
  16372. (void)soap_memmove(soap->tmpbuf + 23, sizeof(soap->tmpbuf) - 23, soap->tmpbuf + 22, 3); /* 2000-03-01T02:00:00+0300 */
  16373. soap->tmpbuf[22] = ':'; /* 2000-03-01T02:00:00+03:00 */
  16374. }
  16375. }
  16376. #endif
  16377. #elif defined(HAVE_GETTIMEOFDAY) && !defined(WITH_NOZONE)
  16378. #if defined(HAVE_LOCALTIME_R)
  16379. if (localtime_r(&n, pT) != SOAP_FUNC_R_ERR)
  16380. {
  16381. struct timeval tv;
  16382. struct timezone tz;
  16383. memset((void*)&tz, 0, sizeof(tz));
  16384. gettimeofday(&tv, &tz);
  16385. l = strftime(soap->tmpbuf, sizeof(soap->tmpbuf), "%Y-%m-%dT%H:%M:%S", pT);
  16386. if (l)
  16387. (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);
  16388. }
  16389. #else
  16390. pT = localtime(&n);
  16391. if (pT)
  16392. {
  16393. struct timeval tv;
  16394. struct timezone tz;
  16395. memset((void*)&tz, 0, sizeof(tz));
  16396. gettimeofday(&tv, &tz);
  16397. l = strftime(soap->tmpbuf, sizeof(soap->tmpbuf), "%Y-%m-%dT%H:%M:%S", pT);
  16398. if (l)
  16399. (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);
  16400. }
  16401. #endif
  16402. #elif defined(HAVE_FTIME) && !defined(WITH_NOZONE)
  16403. #if defined(HAVE_LOCALTIME_R)
  16404. if (localtime_r(&n, pT) != SOAP_FUNC_R_ERR)
  16405. {
  16406. struct timeb t;
  16407. memset((void*)&t, 0, sizeof(t));
  16408. #ifdef __BORLANDC__
  16409. ::ftime(&t);
  16410. #else
  16411. ftime(&t);
  16412. #endif
  16413. l = strftime(soap->tmpbuf, sizeof(soap->tmpbuf), "%Y-%m-%dT%H:%M:%S", pT);
  16414. if (l)
  16415. (SOAP_SNPRINTF(soap->tmpbuf + l, sizeof(soap->tmpbuf) - l, 7), "%+03d:%02d", -t.timezone/60+(pT->tm_isdst!=0), abs(t.timezone)%60);
  16416. }
  16417. #else
  16418. pT = localtime(&n);
  16419. if (pT)
  16420. {
  16421. struct timeb t;
  16422. memset((void*)&t, 0, sizeof(t));
  16423. #ifdef __BORLANDC__
  16424. ::ftime(&t);
  16425. #else
  16426. ftime(&t);
  16427. #endif
  16428. l = strftime(soap->tmpbuf, sizeof(soap->tmpbuf), "%Y-%m-%dT%H:%M:%S", pT);
  16429. if (l)
  16430. (SOAP_SNPRINTF(soap->tmpbuf + l, sizeof(soap->tmpbuf) - l, 7), "%+03d:%02d", -t.timezone/60+(pT->tm_isdst!=0), abs(t.timezone)%60);
  16431. }
  16432. #endif
  16433. #elif defined(HAVE_LOCALTIME_R)
  16434. if (localtime_r(&n, pT) != SOAP_FUNC_R_ERR)
  16435. l = strftime(soap->tmpbuf, sizeof(soap->tmpbuf), "%Y-%m-%dT%H:%M:%S", pT);
  16436. #else
  16437. pT = localtime(&n);
  16438. if (pT)
  16439. l = strftime(soap->tmpbuf, sizeof(soap->tmpbuf), "%Y-%m-%dT%H:%M:%S", pT);
  16440. #endif
  16441. if (!l)
  16442. soap_strcpy(soap->tmpbuf, sizeof(soap->tmpbuf), "1969-12-31T23:59:59Z");
  16443. return soap->tmpbuf;
  16444. }
  16445. #endif
  16446. /******************************************************************************/
  16447. #ifndef WITH_LEAN
  16448. SOAP_FMAC1
  16449. int
  16450. SOAP_FMAC2
  16451. soap_outdateTime(struct soap *soap, const char *tag, int id, const time_t *p, const char *type, int n) {
  16452. if (soap_element_begin_out(soap, tag, soap_embedded_id(soap, id, p, n), type)
  16453. || soap_string_out(soap, soap_dateTime2s(soap, *p), 0))
  16454. return soap->error;
  16455. return soap_element_end_out(soap, tag);
  16456. }
  16457. #endif
  16458. /******************************************************************************/
  16459. #ifndef WITH_LEAN
  16460. SOAP_FMAC1
  16461. int
  16462. SOAP_FMAC2
  16463. soap_s2dateTime(struct soap *soap, const char *s, time_t *p) {
  16464. *p = 0;
  16465. if (s) {
  16466. char *t;
  16467. unsigned long d;
  16468. struct tm T;
  16469. if (!*s)
  16470. return soap->error = SOAP_EMPTY;
  16471. memset((void *) &T, 0, sizeof(T));
  16472. d = soap_strtoul(s, &t, 10);
  16473. if (*t == '-') {
  16474. /* YYYY-MM-DD */
  16475. T.tm_year = (int) d;
  16476. T.tm_mon = (int) soap_strtoul(t + 1, &t, 10);
  16477. T.tm_mday = (int) soap_strtoul(t + 1, &t, 10);
  16478. } else if (!(soap->mode & SOAP_XML_STRICT)) {
  16479. /* YYYYMMDD */
  16480. T.tm_year = (int) (d / 10000);
  16481. T.tm_mon = (int) (d / 100 % 100);
  16482. T.tm_mday = (int) (d % 100);
  16483. } else {
  16484. return soap->error = SOAP_TYPE;
  16485. }
  16486. if (*t == 'T' || ((*t == 't' || *t == ' ') && !(soap->mode & SOAP_XML_STRICT))) {
  16487. d = soap_strtoul(t + 1, &t, 10);
  16488. if (*t == ':') {
  16489. /* Thh:mm:ss */
  16490. T.tm_hour = (int) d;
  16491. T.tm_min = (int) soap_strtoul(t + 1, &t, 10);
  16492. T.tm_sec = (int) soap_strtoul(t + 1, &t, 10);
  16493. } else if (!(soap->mode & SOAP_XML_STRICT)) {
  16494. /* Thhmmss */
  16495. T.tm_hour = (int) (d / 10000);
  16496. T.tm_min = (int) (d / 100 % 100);
  16497. T.tm_sec = (int) (d % 100);
  16498. } else {
  16499. return soap->error = SOAP_TYPE;
  16500. }
  16501. }
  16502. if (T.tm_year == 1)
  16503. T.tm_year = 70;
  16504. else
  16505. T.tm_year -= 1900;
  16506. T.tm_mon--;
  16507. if (*t == '.') {
  16508. for (t++; *t; t++)
  16509. if (*t < '0' || *t > '9')
  16510. break;
  16511. }
  16512. if (*t == ' ' && !(soap->mode & SOAP_XML_STRICT))
  16513. t++;
  16514. if (*t) {
  16515. #ifndef WITH_NOZONE
  16516. if (*t == '+' || *t == '-') {
  16517. int h, m;
  16518. m = (int) soap_strtol(t, &t, 10);
  16519. if (*t == ':') {
  16520. /* +hh:mm */
  16521. h = m;
  16522. m = (int) soap_strtol(t + 1, &t, 10);
  16523. if (h < 0)
  16524. m = -m;
  16525. } else if (!(soap->mode & SOAP_XML_STRICT)) {
  16526. /* +hhmm */
  16527. h = m / 100;
  16528. m = m % 100;
  16529. } else {
  16530. /* +hh */
  16531. h = m;
  16532. m = 0;
  16533. }
  16534. if (*t)
  16535. return soap->error = SOAP_TYPE;
  16536. T.tm_min -= m;
  16537. T.tm_hour -= h;
  16538. /* put hour and min in range */
  16539. T.tm_hour += T.tm_min / 60;
  16540. T.tm_min %= 60;
  16541. if (T.tm_min < 0) {
  16542. T.tm_min += 60;
  16543. T.tm_hour--;
  16544. }
  16545. T.tm_mday += T.tm_hour / 24;
  16546. T.tm_hour %= 24;
  16547. if (T.tm_hour < 0) {
  16548. T.tm_hour += 24;
  16549. T.tm_mday--;
  16550. }
  16551. /* note: day of the month may be out of range, timegm() handles it */
  16552. } else if (*t != 'Z') {
  16553. return soap->error = SOAP_TYPE;
  16554. }
  16555. #endif
  16556. *p = soap_timegm(&T);
  16557. } else /* no UTC or timezone, so assume we got a localtime */
  16558. {
  16559. T.tm_isdst = -1;
  16560. *p = mktime(&T);
  16561. }
  16562. }
  16563. return soap->error;
  16564. }
  16565. #endif
  16566. /******************************************************************************/
  16567. #ifndef WITH_LEAN
  16568. SOAP_FMAC1
  16569. time_t *
  16570. SOAP_FMAC2
  16571. soap_indateTime(struct soap *soap, const char *tag, time_t *p, const char *type, int t) {
  16572. if (soap_element_begin_in(soap, tag, 0, NULL))
  16573. return NULL;
  16574. if (*soap->type
  16575. && soap_match_tag(soap, soap->type, type)
  16576. && soap_match_tag(soap, soap->type, ":dateTime")) {
  16577. soap->error = SOAP_TYPE;
  16578. soap_revert(soap);
  16579. return NULL;
  16580. }
  16581. p = (time_t *) soap_id_enter(soap, soap->id, p, t, sizeof(time_t), NULL, NULL, NULL, NULL);
  16582. if (!p)
  16583. return NULL;
  16584. if (*soap->href != '#') {
  16585. int err = soap_s2dateTime(soap, soap_value(soap), p);
  16586. if ((soap->body && soap_element_end_in(soap, tag)) || err)
  16587. return NULL;
  16588. } else {
  16589. p = (time_t *) soap_id_forward(soap, soap->href, p, 0, t, t, sizeof(time_t), 0, NULL, NULL);
  16590. if (soap->body && soap_element_end_in(soap, tag))
  16591. return NULL;
  16592. }
  16593. return p;
  16594. }
  16595. #endif
  16596. /******************************************************************************/
  16597. SOAP_FMAC1
  16598. int
  16599. SOAP_FMAC2
  16600. soap_outliteral(struct soap *soap, const char *tag, char *const *p, const char *type) {
  16601. if (tag && *tag != '-')
  16602. if (soap_element_begin_out(soap, tag, 0, type))
  16603. return soap->error;
  16604. if (p && *p)
  16605. if (soap_send(soap, *p)) /* send as-is */
  16606. return soap->error;
  16607. if (tag && *tag != '-')
  16608. return soap_element_end_out(soap, tag);
  16609. return SOAP_OK;
  16610. }
  16611. /******************************************************************************/
  16612. SOAP_FMAC1
  16613. char **
  16614. SOAP_FMAC2
  16615. soap_inliteral(struct soap *soap, const char *tag, char **p) {
  16616. if (soap_element_begin_in(soap, tag, 1, NULL)) {
  16617. if (soap->error != SOAP_NO_TAG || soap_peek(soap) == SOAP_TT)
  16618. return NULL;
  16619. soap->error = SOAP_OK;
  16620. }
  16621. if (!p) {
  16622. p = (char **) soap_malloc(soap, sizeof(char *));
  16623. if (!p)
  16624. return NULL;
  16625. }
  16626. if (soap->body || (tag && *tag == '-')) {
  16627. if (tag && *tag != '-')
  16628. *p = soap_string_in(soap, -1, -1, -1, NULL);
  16629. else
  16630. *p = soap_string_in(soap, 0, -1, -1, NULL);
  16631. if (!*p)
  16632. return NULL;
  16633. if (!**p && tag && *tag == '-') {
  16634. soap->error = SOAP_NO_TAG;
  16635. return NULL;
  16636. }
  16637. } else if (soap->null) {
  16638. *p = NULL;
  16639. } else {
  16640. *p = soap_strdup(soap, SOAP_STR_EOS);
  16641. }
  16642. if (soap->body && soap_element_end_in(soap, tag))
  16643. return NULL;
  16644. return p;
  16645. }
  16646. /******************************************************************************/
  16647. #ifndef WITH_LEANER
  16648. SOAP_FMAC1
  16649. int
  16650. SOAP_FMAC2
  16651. soap_outwliteral(struct soap *soap, const char *tag, wchar_t *const *p, const char *type) {
  16652. if (tag && *tag != '-')
  16653. if (soap_element_begin_out(soap, tag, 0, type))
  16654. return soap->error;
  16655. if (p) {
  16656. wchar_t c;
  16657. const wchar_t *s = *p;
  16658. while ((c = *s++)) {
  16659. if (soap_pututf8(soap, (unsigned long) c)) /* send as-is in UTF8 */
  16660. return soap->error;
  16661. }
  16662. }
  16663. if (tag && *tag != '-')
  16664. return soap_element_end_out(soap, tag);
  16665. return SOAP_OK;
  16666. }
  16667. #endif
  16668. /******************************************************************************/
  16669. #ifndef WITH_LEANER
  16670. SOAP_FMAC1
  16671. wchar_t **
  16672. SOAP_FMAC2
  16673. soap_inwliteral(struct soap *soap, const char *tag, wchar_t **p) {
  16674. if (soap_element_begin_in(soap, tag, 1, NULL)) {
  16675. if (soap->error != SOAP_NO_TAG || soap_peek(soap) == SOAP_TT)
  16676. return NULL;
  16677. soap->error = SOAP_OK;
  16678. }
  16679. if (!p) {
  16680. p = (wchar_t **) soap_malloc(soap, sizeof(wchar_t *));
  16681. if (!p)
  16682. return NULL;
  16683. }
  16684. if (soap->body) {
  16685. if (tag && *tag != '-')
  16686. *p = soap_wstring_in(soap, -1, -1, -1, NULL);
  16687. else
  16688. *p = soap_wstring_in(soap, 0, -1, -1, NULL);
  16689. if (!*p)
  16690. return NULL;
  16691. if (!**p && tag && *tag == '-') {
  16692. soap->error = SOAP_NO_TAG;
  16693. return NULL;
  16694. }
  16695. } else if (tag && *tag == '-') {
  16696. soap->error = SOAP_NO_TAG;
  16697. return NULL;
  16698. } else if (soap->null) {
  16699. *p = NULL;
  16700. } else {
  16701. *p = soap_wstrdup(soap, L"");
  16702. }
  16703. if (soap->body && soap_element_end_in(soap, tag))
  16704. return NULL;
  16705. return p;
  16706. }
  16707. #endif
  16708. /******************************************************************************/
  16709. SOAP_FMAC1
  16710. const char *
  16711. SOAP_FMAC2
  16712. soap_value(struct soap *soap) {
  16713. size_t i;
  16714. soap_wchar c = 0;
  16715. char *s = soap->tmpbuf;
  16716. if (!soap->body)
  16717. return SOAP_STR_EOS;
  16718. do {
  16719. c = soap_get(soap);
  16720. } while (soap_coblank(c));
  16721. for (i = 0; i < sizeof(soap->tmpbuf) - 1; i++) {
  16722. if (c == SOAP_TT || c == SOAP_LT || (int) c == EOF)
  16723. break;
  16724. *s++ = (char) c;
  16725. c = soap_get(soap);
  16726. }
  16727. for (s--; i > 0; i--, s--) {
  16728. if (!soap_coblank((soap_wchar) *s))
  16729. break;
  16730. }
  16731. s[1] = '\0';
  16732. soap->tmpbuf[sizeof(soap->tmpbuf) - 1] = '\0'; /* appease */
  16733. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Element content value='%s'\n", soap->tmpbuf));
  16734. if (c == SOAP_TT || c == SOAP_LT || (int) c == EOF) {
  16735. soap_unget(soap, c);
  16736. } else {
  16737. soap->error = SOAP_LENGTH;
  16738. return NULL;
  16739. }
  16740. #ifdef WITH_DOM
  16741. if ((soap->mode & SOAP_XML_DOM) && soap->dom)
  16742. {
  16743. soap->dom->text = soap_strdup(soap, soap->tmpbuf);
  16744. if (!soap->dom->text)
  16745. return NULL;
  16746. }
  16747. #endif
  16748. return soap->tmpbuf; /* return non-null pointer */
  16749. }
  16750. /******************************************************************************/
  16751. #if !defined(WITH_LEANER) || !defined(WITH_NOHTTP)
  16752. SOAP_FMAC1
  16753. int
  16754. SOAP_FMAC2
  16755. soap_getline(struct soap *soap, char *buf, int len) {
  16756. char *s = buf;
  16757. int i = len;
  16758. soap_wchar c = 0;
  16759. for (;;) {
  16760. while (i > 1) {
  16761. c = soap_getchar(soap);
  16762. if (c == '\r' || c == '\n')
  16763. break;
  16764. if ((int) c == EOF)
  16765. return soap->error = SOAP_CHK_EOF;
  16766. *s++ = (char) c;
  16767. i--;
  16768. }
  16769. *s = '\0';
  16770. if (c != '\n')
  16771. c = soap_getchar(soap); /* got \r or something else, now get \n */
  16772. if (c == '\n') {
  16773. if (i == len) /* empty line: end of HTTP/MIME header */
  16774. break;
  16775. c = soap_get0(soap);
  16776. if (c != ' ' && c != '\t') /* HTTP line continuation? */
  16777. break;
  16778. } else if ((int) c == EOF) {
  16779. return soap->error = SOAP_CHK_EOF;
  16780. } else if (i <= 1) {
  16781. return soap->error = SOAP_HDR;
  16782. }
  16783. }
  16784. return SOAP_OK;
  16785. }
  16786. #endif
  16787. /******************************************************************************/
  16788. static ULONG64
  16789. soap_count_attachments(struct soap *soap) {
  16790. #ifndef WITH_LEANER
  16791. struct soap_multipart *content;
  16792. ULONG64 count = soap->count;
  16793. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Calculating the message size with attachments, current count="
  16794. SOAP_ULONG_FORMAT
  16795. "\n", count));
  16796. if ((soap->mode & SOAP_ENC_DIME) && !(soap->mode & SOAP_ENC_MTOM)) {
  16797. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Calculating the size of DIME attachments\n"));
  16798. for (content = soap->dime.first; content; content = content->next) {
  16799. count += 12 + ((content->size + 3) & (~3));
  16800. if (content->id)
  16801. count += ((strlen(content->id) + 3) & (~3));
  16802. if (content->type)
  16803. count += ((strlen(content->type) + 3) & (~3));
  16804. if (content->options)
  16805. count += ((((unsigned char) content->options[2] << 8) | ((unsigned char) content->options[3])) + 7) &
  16806. (~3);
  16807. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Size of DIME attachment content is %lu bytes\n",
  16808. (unsigned long) content->size));
  16809. }
  16810. }
  16811. if ((soap->mode & SOAP_ENC_MIME) && soap->mime.boundary) {
  16812. size_t n = strlen(soap->mime.boundary);
  16813. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Calculating the size of MIME attachments\n"));
  16814. for (content = soap->mime.first; content; content = content->next) {
  16815. const char *s;
  16816. /* count \r\n--boundary\r\n */
  16817. count += 6 + n;
  16818. /* count Content-Type: ...\r\n */
  16819. if (content->type)
  16820. count += 16 + strlen(content->type);
  16821. /* count Content-Transfer-Encoding: ...\r\n */
  16822. s = soap_code_str(mime_codes, content->encoding);
  16823. if (s)
  16824. count += 29 + strlen(s);
  16825. /* count Content-ID: ...\r\n */
  16826. if (content->id)
  16827. count += 14 + strlen(content->id);
  16828. /* count Content-Location: ...\r\n */
  16829. if (content->location)
  16830. count += 20 + strlen(content->location);
  16831. /* count Content-Description: ...\r\n */
  16832. if (content->description)
  16833. count += 23 + strlen(content->description);
  16834. /* count \r\n...content */
  16835. count += 2 + content->size;
  16836. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Size of MIME attachment content is %lu bytes\n",
  16837. (unsigned long) content->size));
  16838. }
  16839. /* count \r\n--boundary-- */
  16840. count += 6 + n;
  16841. }
  16842. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "New count="
  16843. SOAP_ULONG_FORMAT
  16844. "\n", count));
  16845. return count;
  16846. #else
  16847. return soap->count;
  16848. #endif
  16849. }
  16850. /******************************************************************************/
  16851. #ifndef WITH_LEANER
  16852. static int
  16853. soap_putdimefield(struct soap *soap, const char *s, size_t n) {
  16854. if (soap_send_raw(soap, s, n))
  16855. return soap->error;
  16856. return soap_send_raw(soap, SOAP_STR_PADDING, -(long) n & 3);
  16857. }
  16858. #endif
  16859. /******************************************************************************/
  16860. #ifndef WITH_LEANER
  16861. SOAP_FMAC1
  16862. char *
  16863. SOAP_FMAC2
  16864. soap_dime_option(struct soap *soap, unsigned short optype, const char *option) {
  16865. size_t n;
  16866. char *s = NULL;
  16867. if (option) {
  16868. n = strlen(option);
  16869. s = (char *) soap_malloc(soap, n + 5);
  16870. if (s) {
  16871. s[0] = (char) (optype >> 8);
  16872. s[1] = (char) (optype & 0xFF);
  16873. s[2] = (char) (n >> 8);
  16874. s[3] = (char) (n & 0xFF);
  16875. soap_strcpy(s + 4, n + 1, option);
  16876. }
  16877. }
  16878. return s;
  16879. }
  16880. #endif
  16881. /******************************************************************************/
  16882. #ifndef WITH_LEANER
  16883. SOAP_FMAC1
  16884. int
  16885. SOAP_FMAC2
  16886. soap_putdimehdr(struct soap *soap) {
  16887. unsigned char tmp[12];
  16888. size_t optlen = 0, idlen = 0, typelen = 0;
  16889. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Put DIME header id='%s'\n", soap->dime.id ? soap->dime.id : SOAP_STR_EOS));
  16890. if (soap->dime.options)
  16891. optlen = (((unsigned char) soap->dime.options[2] << 8) | ((unsigned char) soap->dime.options[3])) + 4;
  16892. if (soap->dime.id) {
  16893. idlen = strlen(soap->dime.id);
  16894. if (idlen > 0x0000FFFF)
  16895. idlen = 0x0000FFFF;
  16896. }
  16897. if (soap->dime.type) {
  16898. typelen = strlen(soap->dime.type);
  16899. if (typelen > 0x0000FFFF)
  16900. typelen = 0x0000FFFF;
  16901. }
  16902. tmp[0] = SOAP_DIME_VERSION | (soap->dime.flags & 0x7);
  16903. tmp[1] = soap->dime.flags & 0xF0;
  16904. tmp[2] = (char) (optlen >> 8);
  16905. tmp[3] = (char) (optlen & 0xFF);
  16906. tmp[4] = (char) (idlen >> 8);
  16907. tmp[5] = (char) (idlen & 0xFF);
  16908. tmp[6] = (char) (typelen >> 8);
  16909. tmp[7] = (char) (typelen & 0xFF);
  16910. tmp[8] = (char) (soap->dime.size >> 24);
  16911. tmp[9] = (char) ((soap->dime.size >> 16) & 0xFF);
  16912. tmp[10] = (char) ((soap->dime.size >> 8) & 0xFF);
  16913. tmp[11] = (char) (soap->dime.size & 0xFF);
  16914. if (soap_send_raw(soap, (char *) tmp, 12)
  16915. || soap_putdimefield(soap, soap->dime.options, optlen)
  16916. || soap_putdimefield(soap, soap->dime.id, idlen)
  16917. || soap_putdimefield(soap, soap->dime.type, typelen))
  16918. return soap->error;
  16919. return SOAP_OK;
  16920. }
  16921. #endif
  16922. /******************************************************************************/
  16923. #ifndef WITH_LEANER
  16924. SOAP_FMAC1
  16925. int
  16926. SOAP_FMAC2
  16927. soap_putdime(struct soap *soap) {
  16928. struct soap_multipart *content;
  16929. if (!(soap->mode & SOAP_ENC_DIME))
  16930. return SOAP_OK;
  16931. for (content = soap->dime.first; content; content = content->next) {
  16932. void *handle;
  16933. soap->dime.size = content->size;
  16934. soap->dime.id = content->id;
  16935. soap->dime.type = content->type;
  16936. soap->dime.options = content->options;
  16937. soap->dime.flags = SOAP_DIME_VERSION | SOAP_DIME_MEDIA;
  16938. if (soap->fdimereadopen &&
  16939. ((handle = soap->fdimereadopen(soap, (void *) content->ptr, content->id, content->type,
  16940. content->options)) != NULL || soap->error)) {
  16941. size_t size = content->size;
  16942. if (!handle) {
  16943. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "fdimereadopen failed\n"));
  16944. return soap->error;
  16945. }
  16946. if (!size && ((soap->mode & SOAP_ENC_PLAIN) || (soap->mode & SOAP_IO) == SOAP_IO_CHUNK ||
  16947. (soap->mode & SOAP_IO) == SOAP_IO_STORE)) {
  16948. size_t chunksize = sizeof(soap->tmpbuf);
  16949. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Chunked streaming DIME\n"));
  16950. do {
  16951. size = soap->fdimeread(soap, handle, soap->tmpbuf, chunksize);
  16952. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "fdimeread returned %lu bytes\n", (unsigned long) size));
  16953. if (size < chunksize) {
  16954. soap->dime.flags &= ~SOAP_DIME_CF;
  16955. if (!content->next)
  16956. soap->dime.flags |= SOAP_DIME_ME;
  16957. } else {
  16958. soap->dime.flags |= SOAP_DIME_CF;
  16959. }
  16960. soap->dime.size = size;
  16961. if (soap_putdimehdr(soap)
  16962. || soap_putdimefield(soap, soap->tmpbuf, size))
  16963. break;
  16964. if (soap->dime.id) {
  16965. soap->dime.flags &= ~(SOAP_DIME_MB | SOAP_DIME_MEDIA);
  16966. soap->dime.id = NULL;
  16967. soap->dime.type = NULL;
  16968. soap->dime.options = NULL;
  16969. }
  16970. } while (size >= chunksize);
  16971. } else {
  16972. if (!content->next)
  16973. soap->dime.flags |= SOAP_DIME_ME;
  16974. if (soap_putdimehdr(soap))
  16975. return soap->error;
  16976. do {
  16977. size_t bufsize;
  16978. if (size < sizeof(soap->tmpbuf))
  16979. bufsize = size;
  16980. else
  16981. bufsize = sizeof(soap->tmpbuf);
  16982. bufsize = soap->fdimeread(soap, handle, soap->tmpbuf, bufsize);
  16983. if (!bufsize) {
  16984. DBGLOG(TEST, SOAP_MESSAGE(fdebug,
  16985. "fdimeread failed: insufficient data (%lu bytes remaining from %lu bytes)\n",
  16986. (unsigned long) size, (unsigned long) content->size));
  16987. soap->error = SOAP_CHK_EOF;
  16988. break;
  16989. }
  16990. if (soap_send_raw(soap, soap->tmpbuf, bufsize))
  16991. break;
  16992. size -= bufsize;
  16993. } while (size);
  16994. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "fdimereadclose\n"));
  16995. if (soap_send_raw(soap, SOAP_STR_PADDING, -(long) soap->dime.size & 3))
  16996. return soap->error;
  16997. }
  16998. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "fdimereadclose\n"));
  16999. if (soap->fdimereadclose)
  17000. soap->fdimereadclose(soap, handle);
  17001. } else {
  17002. if (!content->next)
  17003. soap->dime.flags |= SOAP_DIME_ME;
  17004. if (soap_putdimehdr(soap)
  17005. || soap_putdimefield(soap, (char *) content->ptr, content->size))
  17006. return soap->error;
  17007. }
  17008. }
  17009. return SOAP_OK;
  17010. }
  17011. #endif
  17012. /******************************************************************************/
  17013. #ifndef WITH_LEANER
  17014. static char *
  17015. soap_getdimefield(struct soap *soap, size_t n) {
  17016. char *p = NULL;
  17017. if (n > 0) {
  17018. p = (char *) soap_malloc(soap, n + 1 > n ? n + 1 : n);
  17019. if (p) {
  17020. char *s = p;
  17021. size_t i;
  17022. for (i = n; i > 0; i--) {
  17023. soap_wchar c = soap_get1(soap);
  17024. if ((int) c == EOF) {
  17025. soap->error = SOAP_CHK_EOF;
  17026. return NULL;
  17027. }
  17028. *s++ = (char) c;
  17029. }
  17030. if (n + 1 > n)
  17031. *s = '\0'; /* force NUL terminated */
  17032. soap->error = soap_move(soap, (size_t) (-(long) n & 3));
  17033. if (soap->error)
  17034. return NULL;
  17035. } else {
  17036. soap->error = SOAP_EOM;
  17037. }
  17038. }
  17039. return p;
  17040. }
  17041. #endif
  17042. /******************************************************************************/
  17043. #ifndef WITH_LEANER
  17044. SOAP_FMAC1
  17045. int
  17046. SOAP_FMAC2
  17047. soap_getdimehdr(struct soap *soap) {
  17048. soap_wchar c;
  17049. char *s;
  17050. int i;
  17051. unsigned char tmp[12];
  17052. size_t optlen, idlen, typelen;
  17053. if (!(soap->mode & SOAP_ENC_DIME))
  17054. return soap->error = SOAP_DIME_END;
  17055. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Get DIME header\n"));
  17056. s = (char *) tmp;
  17057. for (i = 12; i > 0; i--) {
  17058. c = soap_getchar(soap);
  17059. if ((int) c == EOF)
  17060. return soap->error = SOAP_CHK_EOF;
  17061. *s++ = (char) c;
  17062. }
  17063. if ((tmp[0] & 0xF8) != SOAP_DIME_VERSION)
  17064. return soap->error = SOAP_DIME_MISMATCH;
  17065. soap->dime.flags = (tmp[0] & 0x7) | (tmp[1] & 0xF0);
  17066. optlen = (tmp[2] << 8) | tmp[3];
  17067. idlen = (tmp[4] << 8) | tmp[5];
  17068. typelen = (tmp[6] << 8) | tmp[7];
  17069. soap->dime.size = ((size_t) tmp[8] << 24) | ((size_t) tmp[9] << 16) | ((size_t) tmp[10] << 8) | ((size_t) tmp[11]);
  17070. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "DIME size=%lu flags=0x%X\n", (unsigned long) soap->dime.size, soap->dime.flags));
  17071. soap->dime.options = soap_getdimefield(soap, optlen);
  17072. if (!soap->dime.options && soap->error)
  17073. return soap->error;
  17074. soap->dime.id = soap_getdimefield(soap, idlen);
  17075. if (!soap->dime.id && soap->error)
  17076. return soap->error;
  17077. soap->dime.type = soap_getdimefield(soap, typelen);
  17078. if (!soap->dime.type && soap->error)
  17079. return soap->error;
  17080. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "DIME flags=%x id='%s', type='%s', options='%s'\n", soap->dime.flags,
  17081. soap->dime.id ? soap->dime.id : SOAP_STR_EOS, soap->dime.type ? soap->dime.type : "",
  17082. soap->dime.options ? soap->dime.options + 4 : SOAP_STR_EOS));
  17083. if ((soap->dime.flags & SOAP_DIME_ME))
  17084. soap->mode &= ~SOAP_ENC_DIME;
  17085. return SOAP_OK;
  17086. }
  17087. #endif
  17088. /******************************************************************************/
  17089. #ifndef WITH_LEANER
  17090. SOAP_FMAC1
  17091. int
  17092. SOAP_FMAC2
  17093. soap_getdime(struct soap *soap) {
  17094. if (soap->dime.buflen || soap->dime.chunksize) {
  17095. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Skip remainder of SOAP in DIME (%u bytes or %u bytes in chunk left)\n",
  17096. (unsigned int) soap->dime.buflen, (unsigned int) soap->dime.chunksize));
  17097. do
  17098. if (soap_get1(soap) == (int) EOF)
  17099. return soap->error = SOAP_CHK_EOF;
  17100. while (soap->dime.buflen || soap->dime.chunksize);
  17101. if (soap_move(soap, (size_t) (-(long) soap->dime.size & 3)))
  17102. return soap->error = SOAP_CHK_EOF;
  17103. if (!(soap->mode & SOAP_ENC_DIME))
  17104. return SOAP_OK;
  17105. } else {
  17106. if (soap_move(soap, (size_t) (((soap->dime.size + 3) & (~3)) - soap_tell(soap))))
  17107. return soap->error = SOAP_CHK_EOF;
  17108. }
  17109. for (;;) {
  17110. struct soap_multipart *content;
  17111. if (soap_getdimehdr(soap))
  17112. break;
  17113. if (soap->fdimewriteopen &&
  17114. ((soap->dime.ptr = (char *) soap->fdimewriteopen(soap, soap->dime.id, soap->dime.type,
  17115. soap->dime.options)) != NULL || soap->error)) {
  17116. const char *id, *type, *options;
  17117. size_t size, n;
  17118. if (!soap->dime.ptr)
  17119. return soap->error;
  17120. id = soap->dime.id;
  17121. type = soap->dime.type;
  17122. options = soap->dime.options;
  17123. for (;;) {
  17124. size = soap->dime.size;
  17125. for (;;) {
  17126. n = soap->buflen - soap->bufidx;
  17127. if (size < n)
  17128. n = size;
  17129. soap->error = soap->fdimewrite(soap, (void *) soap->dime.ptr, soap->buf + soap->bufidx, n);
  17130. if (soap->error)
  17131. break;
  17132. size -= n;
  17133. if (!size) {
  17134. soap->bufidx += n;
  17135. break;
  17136. }
  17137. if (soap_recv(soap)) {
  17138. soap->error = SOAP_EOF;
  17139. goto end;
  17140. }
  17141. }
  17142. if (soap_move(soap, (size_t) (-(long) soap->dime.size & 3))) {
  17143. soap->error = SOAP_EOF;
  17144. break;
  17145. }
  17146. if (!(soap->dime.flags & SOAP_DIME_CF))
  17147. break;
  17148. if (soap_getdimehdr(soap))
  17149. break;
  17150. }
  17151. end:
  17152. if (soap->fdimewriteclose)
  17153. soap->fdimewriteclose(soap, (void *) soap->dime.ptr);
  17154. soap->dime.size = 0;
  17155. soap->dime.id = id;
  17156. soap->dime.type = type;
  17157. soap->dime.options = options;
  17158. } else if ((soap->dime.flags & SOAP_DIME_CF)) {
  17159. const char *id, *type, *options;
  17160. id = soap->dime.id;
  17161. type = soap->dime.type;
  17162. options = soap->dime.options;
  17163. if (soap_alloc_block(soap) == NULL)
  17164. return soap->error = SOAP_EOM;
  17165. for (;;) {
  17166. soap_wchar c;
  17167. size_t i;
  17168. char *s;
  17169. if (soap->dime.size > SOAP_MAXDIMESIZE) {
  17170. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "DIME size=%lu exceeds SOAP_MAXDIMESIZE=%lu\n",
  17171. (unsigned long) soap->dime.size, (unsigned long) SOAP_MAXDIMESIZE));
  17172. return soap->error = SOAP_DIME_ERROR;
  17173. }
  17174. s = (char *) soap_push_block(soap, NULL, soap->dime.size);
  17175. if (!s)
  17176. return soap->error = SOAP_EOM;
  17177. for (i = soap->dime.size; i > 0; i--) {
  17178. c = soap_get1(soap);
  17179. if ((int) c == EOF)
  17180. return soap->error = SOAP_EOF;
  17181. *s++ = (char) c;
  17182. }
  17183. if (soap_move(soap, (size_t) (-(long) soap->dime.size & 3)))
  17184. return soap->error = SOAP_EOF;
  17185. if (!(soap->dime.flags & SOAP_DIME_CF))
  17186. break;
  17187. if (soap_getdimehdr(soap))
  17188. return soap->error;
  17189. }
  17190. soap->dime.size = soap->blist->size;
  17191. if (soap->dime.size + 1 > soap->dime.size)
  17192. soap->blist->size++; /* allocate one more byte in blist for the terminating '\0' */
  17193. soap->dime.ptr = soap_save_block(soap, NULL, NULL, 0);
  17194. if (!soap->dime.ptr)
  17195. return soap->error;
  17196. if (soap->dime.size + 1 > soap->dime.size)
  17197. soap->dime.ptr[soap->dime.size] = '\0'; /* make 0-terminated, just in case even though this is binary data */
  17198. soap->dime.id = id;
  17199. soap->dime.type = type;
  17200. soap->dime.options = options;
  17201. } else {
  17202. soap->dime.ptr = soap_getdimefield(soap, soap->dime.size);
  17203. }
  17204. content = soap_alloc_multipart(soap, &soap->dime.first, &soap->dime.last, soap->dime.ptr, soap->dime.size);
  17205. if (!content)
  17206. return soap->error = SOAP_EOM;
  17207. content->id = soap->dime.id;
  17208. content->type = soap->dime.type;
  17209. content->options = soap->dime.options;
  17210. if (soap->error)
  17211. return soap->error;
  17212. soap_resolve_attachment(soap, content);
  17213. }
  17214. if (soap->error != SOAP_DIME_END)
  17215. return soap->error;
  17216. return soap->error = SOAP_OK;
  17217. }
  17218. #endif
  17219. /******************************************************************************/
  17220. #ifndef WITH_LEANER
  17221. SOAP_FMAC1
  17222. int
  17223. SOAP_FMAC2
  17224. soap_getmimehdr(struct soap *soap) {
  17225. struct soap_multipart *content;
  17226. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Get MIME header\n"));
  17227. do {
  17228. if (soap_getline(soap, soap->msgbuf, sizeof(soap->msgbuf)))
  17229. return soap->error;
  17230. } while (!*soap->msgbuf);
  17231. if (soap->msgbuf[0] == '-' && soap->msgbuf[1] == '-') {
  17232. char *s = soap->msgbuf + strlen(soap->msgbuf) - 1;
  17233. /* remove white space */
  17234. while (soap_coblank((soap_wchar) *s))
  17235. s--;
  17236. s[1] = '\0';
  17237. if (soap->mime.boundary) {
  17238. if (strcmp(soap->msgbuf + 2, soap->mime.boundary))
  17239. return soap->error = SOAP_MIME_ERROR;
  17240. } else {
  17241. soap->mime.boundary = soap_strdup(soap, soap->msgbuf + 2);
  17242. if (!soap->mime.boundary)
  17243. return soap->error = SOAP_EOM;
  17244. }
  17245. if (soap_getline(soap, soap->msgbuf, sizeof(soap->msgbuf)))
  17246. return soap->error;
  17247. }
  17248. if (soap_set_mime_attachment(soap, NULL, 0, SOAP_MIME_NONE, NULL, NULL, NULL, NULL))
  17249. return soap->error = SOAP_EOM;
  17250. content = soap->mime.last;
  17251. for (;;) {
  17252. char *key = soap->msgbuf;
  17253. char *val;
  17254. if (!*key)
  17255. break;
  17256. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "MIME header: %s\n", key));
  17257. val = strchr(soap->msgbuf, ':');
  17258. if (val) {
  17259. *val = '\0';
  17260. do {
  17261. val++;
  17262. } while (*val && *val <= 32);
  17263. if (!soap_tag_cmp(key, "Content-ID"))
  17264. content->id = soap_strdup(soap, val);
  17265. else if (!soap_tag_cmp(key, "Content-Location"))
  17266. content->location = soap_strdup(soap, val);
  17267. else if (!content->id && !soap_tag_cmp(key, "Content-Disposition"))
  17268. content->id = soap_strdup(soap, soap_http_header_attribute(soap, val, "name"));
  17269. else if (!soap_tag_cmp(key, "Content-Type"))
  17270. content->type = soap_strdup(soap, val);
  17271. else if (!soap_tag_cmp(key, "Content-Description"))
  17272. content->description = soap_strdup(soap, val);
  17273. else if (!soap_tag_cmp(key, "Content-Transfer-Encoding"))
  17274. content->encoding = (enum soap_mime_encoding) soap_code_int(mime_codes, val, (LONG64) SOAP_MIME_NONE);
  17275. }
  17276. if (soap_getline(soap, key, sizeof(soap->msgbuf)))
  17277. return soap->error;
  17278. }
  17279. return SOAP_OK;
  17280. }
  17281. #endif
  17282. /******************************************************************************/
  17283. #ifndef WITH_LEANER
  17284. SOAP_FMAC1
  17285. int
  17286. SOAP_FMAC2
  17287. soap_getmime(struct soap *soap) {
  17288. while (soap_recv_mime_attachment(soap, NULL))
  17289. continue;
  17290. return soap->error;
  17291. }
  17292. #endif
  17293. /******************************************************************************/
  17294. #ifndef WITH_LEANER
  17295. SOAP_FMAC1
  17296. void
  17297. SOAP_FMAC2
  17298. soap_post_check_mime_attachments(struct soap *soap) {
  17299. soap->imode |= SOAP_MIME_POSTCHECK;
  17300. }
  17301. #endif
  17302. /******************************************************************************/
  17303. #ifndef WITH_LEANER
  17304. SOAP_FMAC1
  17305. int
  17306. SOAP_FMAC2
  17307. soap_check_mime_attachments(struct soap *soap) {
  17308. if ((soap->mode & SOAP_MIME_POSTCHECK))
  17309. return soap_recv_mime_attachment(soap, NULL) != NULL;
  17310. return SOAP_OK;
  17311. }
  17312. #endif
  17313. /******************************************************************************/
  17314. #ifndef WITH_LEANER
  17315. SOAP_FMAC1
  17316. struct soap_multipart *
  17317. SOAP_FMAC2
  17318. soap_recv_mime_attachment(struct soap *soap, void *handle) {
  17319. soap_wchar c = 0;
  17320. size_t i, m = 0;
  17321. char *s, *t = NULL;
  17322. struct soap_multipart *content;
  17323. short flag = 0;
  17324. if (!(soap->mode & SOAP_ENC_MIME))
  17325. return NULL;
  17326. content = soap->mime.last;
  17327. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Get MIME (%p)\n", (void *) content));
  17328. if (!content) {
  17329. if (soap_getmimehdr(soap))
  17330. return NULL;
  17331. content = soap->mime.last;
  17332. } else if (content != soap->mime.first) {
  17333. if (soap->fmimewriteopen &&
  17334. ((content->ptr = (char *) soap->fmimewriteopen(soap, (void *) handle, content->id, content->type,
  17335. content->description, content->encoding)) != NULL ||
  17336. soap->error)) {
  17337. if (!content->ptr)
  17338. return NULL;
  17339. }
  17340. }
  17341. DBGLOG(TEST,
  17342. SOAP_MESSAGE(fdebug, "Parsing MIME content id='%s' type='%s'\n", content->id ? content->id : SOAP_STR_EOS,
  17343. content->type ? content->type : SOAP_STR_EOS));
  17344. if (!content->ptr && soap_alloc_block(soap) == NULL) {
  17345. soap->error = SOAP_EOM;
  17346. return NULL;
  17347. }
  17348. for (;;) {
  17349. if (content->ptr) {
  17350. s = soap->tmpbuf;
  17351. } else {
  17352. s = (char *) soap_push_block(soap, NULL, sizeof(soap->tmpbuf));
  17353. if (!s) {
  17354. soap->error = SOAP_EOM;
  17355. return NULL;
  17356. }
  17357. }
  17358. for (i = 0; i < sizeof(soap->tmpbuf); i++) {
  17359. if (m > 0) {
  17360. *s++ = *t++;
  17361. m--;
  17362. } else {
  17363. if (!flag) {
  17364. c = soap_getchar(soap);
  17365. if ((int) c == EOF) {
  17366. if (content->ptr && soap->fmimewriteclose)
  17367. soap->fmimewriteclose(soap, (void *) content->ptr);
  17368. soap->error = SOAP_CHK_EOF;
  17369. return NULL;
  17370. }
  17371. }
  17372. if (flag || c == '\r') {
  17373. memset((void *) soap->msgbuf, 0, sizeof(soap->msgbuf));
  17374. soap_strcpy(soap->msgbuf, sizeof(soap->msgbuf), "\n--");
  17375. if (soap->mime.boundary) {
  17376. if (soap_strncat(soap->msgbuf, sizeof(soap->msgbuf), soap->mime.boundary,
  17377. sizeof(soap->msgbuf) - 4)) {
  17378. soap->error = SOAP_MIME_ERROR;
  17379. return NULL;
  17380. }
  17381. }
  17382. t = soap->msgbuf;
  17383. do {
  17384. c = soap_getchar(soap);
  17385. } while (c == *t++);
  17386. if ((int) c == EOF) {
  17387. if (content->ptr && soap->fmimewriteclose)
  17388. soap->fmimewriteclose(soap, (void *) content->ptr);
  17389. soap->error = SOAP_CHK_EOF;
  17390. return NULL;
  17391. }
  17392. if (!*--t)
  17393. goto end;
  17394. *t = (char) c;
  17395. flag = (c == '\r');
  17396. m = t - soap->msgbuf + 1 - flag;
  17397. t = soap->msgbuf;
  17398. c = '\r';
  17399. }
  17400. *s++ = (char) c;
  17401. }
  17402. }
  17403. if (content->ptr && soap->fmimewrite) {
  17404. soap->error = soap->fmimewrite(soap, (void *) content->ptr, soap->tmpbuf, i);
  17405. if (soap->error)
  17406. break;
  17407. }
  17408. }
  17409. end:
  17410. if (content->ptr) {
  17411. if (!soap->error && soap->fmimewrite)
  17412. soap->error = soap->fmimewrite(soap, (void *) content->ptr, soap->tmpbuf, i);
  17413. if (soap->fmimewriteclose)
  17414. soap->fmimewriteclose(soap, (void *) content->ptr);
  17415. if (soap->error)
  17416. return NULL;
  17417. } else {
  17418. *s = '\0'; /* make 0-terminated, just in case even though this is binary data */
  17419. content->size = soap_size_block(soap, NULL, i + 1) - 1; /* last block with '\0' */
  17420. content->ptr = soap_save_block(soap, NULL, NULL, 0);
  17421. }
  17422. soap_resolve_attachment(soap, content);
  17423. if (c == '-' && soap_getchar(soap) == '-') {
  17424. soap->mode &= ~SOAP_ENC_MIME;
  17425. if ((soap->mode & SOAP_MIME_POSTCHECK) && soap_end_recv(soap)) {
  17426. if (soap->keep_alive == -2) /* special case to keep alive */
  17427. soap->keep_alive = 0;
  17428. soap_closesock(soap);
  17429. return NULL;
  17430. }
  17431. } else {
  17432. while (c != '\r' && (int) c != EOF && soap_coblank(c))
  17433. c = soap_getchar(soap);
  17434. if (c != '\r' || soap_getchar(soap) != '\n') {
  17435. soap->error = SOAP_MIME_ERROR;
  17436. return NULL;
  17437. }
  17438. if (soap_getmimehdr(soap))
  17439. return NULL;
  17440. }
  17441. return content;
  17442. }
  17443. #endif
  17444. /******************************************************************************/
  17445. #ifndef WITH_LEANER
  17446. SOAP_FMAC1
  17447. int
  17448. SOAP_FMAC2
  17449. soap_match_cid(struct soap *soap, const char *s, const char *t) {
  17450. size_t n;
  17451. if (!s)
  17452. return 1;
  17453. if (!strcmp(s, t))
  17454. return 0;
  17455. if (!strncmp(s, "cid:", 4))
  17456. s += 4;
  17457. n = strlen(t);
  17458. if (*t == '<') {
  17459. t++;
  17460. n -= 2;
  17461. }
  17462. if (!strncmp(s, t, n) && !s[n])
  17463. return 0;
  17464. (void) soap_decode(soap->tmpbuf, sizeof(soap->tmpbuf), s, SOAP_STR_EOS);
  17465. if (!strncmp(soap->tmpbuf, t, n) && !soap->tmpbuf[n])
  17466. return 0;
  17467. return 1;
  17468. }
  17469. #endif
  17470. /******************************************************************************/
  17471. /* return UUID "<prefix>xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx" in a temporary buffer */
  17472. SOAP_FMAC1
  17473. const char *
  17474. SOAP_FMAC2
  17475. soap_rand_uuid(struct soap *soap, const char *prefix) {
  17476. int r1, r2, r3, r4;
  17477. #ifdef WITH_OPENSSL
  17478. r1 = soap_random;
  17479. r2 = soap_random;
  17480. #else
  17481. size_t i;
  17482. static int k = 0xFACEB00C;
  17483. int lo = k % 127773;
  17484. int hi = k / 127773;
  17485. # if defined(HAVE_GETTIMEOFDAY)
  17486. struct timeval tv;
  17487. gettimeofday(&tv, NULL);
  17488. r1 = 10000000 * tv.tv_sec + tv.tv_usec;
  17489. # elif defined(UNDER_CE)
  17490. r1 = (int)Random();
  17491. # elif !defined(WITH_LEAN)
  17492. r1 = (int)time(NULL);
  17493. # else
  17494. r1 = k;
  17495. # endif
  17496. k = 16807 * lo - 2836 * hi;
  17497. if (k <= 0)
  17498. k += 0x7FFFFFFF;
  17499. r2 = k;
  17500. /* k &= 0x8FFFFFFF; */
  17501. for (i = 0; i < (sizeof(soap->buf) < 16UL ? sizeof(soap->buf) : 16UL); i++)
  17502. r2 += soap->buf[i];
  17503. #endif
  17504. r3 = soap_random;
  17505. r4 = soap_random;
  17506. (SOAP_SNPRINTF(soap->tmpbuf, sizeof(soap->tmpbuf), prefix ? strlen(prefix) + 37 : 37),
  17507. "%s%8.8x-%4.4hx-4%3.3hx-%4.4hx-%4.4hx%8.8x", prefix ? prefix : SOAP_STR_EOS, r1, (short) (r2 >> 16),
  17508. (short) (((short) r2 >> 4) & 0x0FFF), (short) (((short) (r3 >> 16) & 0x3FFF) | 0x8000), (short) r3, r4);
  17509. return soap->tmpbuf;
  17510. }
  17511. /******************************************************************************/
  17512. #ifndef WITH_LEANER
  17513. static void
  17514. soap_resolve_attachment(struct soap *soap, struct soap_multipart *content) {
  17515. if (content->id) {
  17516. struct soap_xlist **xp = &soap->xlist;
  17517. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Resolving attachment data for id='%s'\n", content->id));
  17518. while (*xp) {
  17519. struct soap_xlist *xq = *xp;
  17520. if (!soap_match_cid(soap, xq->id, content->id)) {
  17521. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Found matching attachment id='%s' for content id='%s'\n", xq->id,
  17522. content->id));
  17523. *xp = xq->next;
  17524. *xq->ptr = (unsigned char *) content->ptr;
  17525. *xq->size = (int) content->size;
  17526. *xq->type = (char *) content->type;
  17527. if (content->options)
  17528. *xq->options = (char *) content->options;
  17529. else
  17530. *xq->options = (char *) content->description;
  17531. SOAP_FREE(soap, xq);
  17532. } else {
  17533. xp = &(*xp)->next;
  17534. }
  17535. }
  17536. }
  17537. }
  17538. #endif
  17539. /******************************************************************************/
  17540. #ifndef WITH_LEANER
  17541. SOAP_FMAC1
  17542. int
  17543. SOAP_FMAC2
  17544. soap_putmimehdr(struct soap *soap, struct soap_multipart *content) {
  17545. const char *s;
  17546. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "MIME attachment type='%s'\n", content->type ? content->type : SOAP_STR_EOS));
  17547. if (soap_send3(soap, "\r\n--", soap->mime.boundary, "\r\n"))
  17548. return soap->error;
  17549. if (content->type && soap_send3(soap, "Content-Type: ", content->type, "\r\n"))
  17550. return soap->error;
  17551. s = soap_code_str(mime_codes, content->encoding);
  17552. if (s && soap_send3(soap, "Content-Transfer-Encoding: ", s, "\r\n"))
  17553. return soap->error;
  17554. if (content->id && soap_send3(soap, "Content-ID: ", content->id, "\r\n"))
  17555. return soap->error;
  17556. if (content->location && soap_send3(soap, "Content-Location: ", content->location, "\r\n"))
  17557. return soap->error;
  17558. if (content->description && soap_send3(soap, "Content-Description: ", content->description, "\r\n"))
  17559. return soap->error;
  17560. return soap_send_raw(soap, "\r\n", 2);
  17561. }
  17562. #endif
  17563. /******************************************************************************/
  17564. #ifndef WITH_LEANER
  17565. SOAP_FMAC1
  17566. int
  17567. SOAP_FMAC2
  17568. soap_putmime(struct soap *soap) {
  17569. struct soap_multipart *content;
  17570. if (!(soap->mode & SOAP_ENC_MIME) || !soap->mime.boundary)
  17571. return SOAP_OK;
  17572. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Sending MIME attachments\n"));
  17573. for (content = soap->mime.first; content; content = content->next) {
  17574. void *handle;
  17575. if (soap->fmimereadopen &&
  17576. ((handle = soap->fmimereadopen(soap, (void *) content->ptr, content->id, content->type,
  17577. content->description)) != NULL || soap->error)) {
  17578. size_t size = content->size;
  17579. if (!handle) {
  17580. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "fmimereadopen failed\n"));
  17581. return soap->error;
  17582. }
  17583. if (soap_putmimehdr(soap, content))
  17584. return soap->error;
  17585. if (!size) {
  17586. if ((soap->mode & SOAP_ENC_PLAIN) || (soap->mode & SOAP_IO) == SOAP_IO_CHUNK ||
  17587. (soap->mode & SOAP_IO) == SOAP_IO_STORE) {
  17588. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Chunked streaming MIME\n"));
  17589. do {
  17590. size = soap->fmimeread(soap, handle, soap->tmpbuf, sizeof(soap->tmpbuf));
  17591. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "fmimeread returned %lu bytes\n", (unsigned long) size));
  17592. if (soap_send_raw(soap, soap->tmpbuf, size))
  17593. break;
  17594. } while (size);
  17595. } else {
  17596. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Error: cannot chunk streaming MIME (no HTTP chunking)\n"));
  17597. }
  17598. } else {
  17599. do {
  17600. size_t bufsize;
  17601. if (size < sizeof(soap->tmpbuf))
  17602. bufsize = size;
  17603. else
  17604. bufsize = sizeof(soap->tmpbuf);
  17605. bufsize = soap->fmimeread(soap, handle, soap->tmpbuf, bufsize);
  17606. if (!bufsize) {
  17607. DBGLOG(TEST, SOAP_MESSAGE(fdebug,
  17608. "fmimeread failed: insufficient data (%lu bytes remaining from %lu bytes)\n",
  17609. (unsigned long) size, (unsigned long) content->size));
  17610. soap->error = SOAP_EOF;
  17611. break;
  17612. }
  17613. if (soap_send_raw(soap, soap->tmpbuf, bufsize))
  17614. break;
  17615. size -= bufsize;
  17616. } while (size);
  17617. }
  17618. if (soap->fmimereadclose)
  17619. soap->fmimereadclose(soap, handle);
  17620. } else {
  17621. if (soap_putmimehdr(soap, content)
  17622. || soap_send_raw(soap, content->ptr, content->size))
  17623. return soap->error;
  17624. }
  17625. }
  17626. return soap_send3(soap, "\r\n--", soap->mime.boundary, "--");
  17627. }
  17628. #endif
  17629. /******************************************************************************/
  17630. #ifndef WITH_LEANER
  17631. SOAP_FMAC1
  17632. void
  17633. SOAP_FMAC2
  17634. soap_set_dime(struct soap *soap) {
  17635. soap->omode |= SOAP_ENC_DIME;
  17636. soap->dime.first = NULL;
  17637. soap->dime.last = NULL;
  17638. }
  17639. #endif
  17640. /******************************************************************************/
  17641. #ifndef WITH_LEANER
  17642. SOAP_FMAC1
  17643. void
  17644. SOAP_FMAC2
  17645. soap_set_mime(struct soap *soap, const char *boundary, const char *start) {
  17646. soap->omode |= SOAP_ENC_MIME;
  17647. soap->mime.first = NULL;
  17648. soap->mime.last = NULL;
  17649. soap->mime.boundary = soap_strdup(soap, boundary);
  17650. soap->mime.start = soap_strdup(soap, start);
  17651. }
  17652. #endif
  17653. /******************************************************************************/
  17654. #ifndef WITH_LEANER
  17655. SOAP_FMAC1
  17656. void
  17657. SOAP_FMAC2
  17658. soap_clr_dime(struct soap *soap) {
  17659. soap->omode &= ~SOAP_ENC_DIME;
  17660. soap->dime.first = NULL;
  17661. soap->dime.last = NULL;
  17662. }
  17663. #endif
  17664. /******************************************************************************/
  17665. #ifndef WITH_LEANER
  17666. SOAP_FMAC1
  17667. void
  17668. SOAP_FMAC2
  17669. soap_clr_mime(struct soap *soap) {
  17670. soap->omode &= ~SOAP_ENC_MIME;
  17671. soap->mime.first = NULL;
  17672. soap->mime.last = NULL;
  17673. soap->mime.boundary = NULL;
  17674. soap->mime.start = NULL;
  17675. }
  17676. #endif
  17677. /******************************************************************************/
  17678. #ifndef WITH_LEANER
  17679. static int
  17680. soap_begin_attachments(struct soap *soap) {
  17681. if ((soap->mode & SOAP_ENC_MIME) && soap->mime.boundary && soap->mime.start) {
  17682. const char *s;
  17683. if (strlen(soap->mime.boundary) + strlen(soap->mime.start) + 140 > sizeof(soap->tmpbuf))
  17684. return soap->error = SOAP_EOM;
  17685. if ((soap->mode & SOAP_ENC_DIME) && !(soap->mode & SOAP_ENC_MTOM)) {
  17686. s = "application/dime";
  17687. } else if (soap->version == 2) {
  17688. if ((soap->mode & SOAP_ENC_MTOM))
  17689. s = "application/xop+xml; charset=utf-8; type=\"application/soap+xml\"";
  17690. else
  17691. s = "application/soap+xml; charset=utf-8";
  17692. } else if ((soap->mode & SOAP_ENC_MTOM)) {
  17693. s = "application/xop+xml; charset=utf-8; type=\"text/xml\"";
  17694. } else {
  17695. s = "text/xml; charset=utf-8";
  17696. }
  17697. (SOAP_SNPRINTF_SAFE(soap->tmpbuf, sizeof(soap->tmpbuf)),
  17698. "--%s\r\nContent-Type: %s\r\nContent-Transfer-Encoding: binary\r\nContent-ID: %s\r\n\r\n",
  17699. soap->mime.boundary, s, soap->mime.start);
  17700. if (soap_send(soap, soap->tmpbuf))
  17701. return soap->error;
  17702. }
  17703. if ((soap->mode & SOAP_IO_LENGTH))
  17704. soap->dime.size = (size_t) soap->count; /* DIME in MIME correction, soap->count is small */
  17705. if (!(soap->mode & SOAP_IO_LENGTH) && (soap->mode & SOAP_ENC_DIME)) {
  17706. if (soap_putdimehdr(soap))
  17707. return soap->error;
  17708. }
  17709. return SOAP_OK;
  17710. }
  17711. #endif
  17712. /******************************************************************************/
  17713. #ifndef WITH_LEANER
  17714. static int
  17715. soap_end_attachments(struct soap *soap) {
  17716. if ((soap->mode & SOAP_IO_LENGTH) && (soap->mode & SOAP_ENC_DIME) && !(soap->mode & SOAP_ENC_MTOM)) {
  17717. if (soap->count > 0xFFFFFFFF)
  17718. return soap->error = SOAP_DIME_ERROR;
  17719. soap->dime.size = (size_t) soap->count - soap->dime.size; /* DIME in MIME correction */
  17720. (SOAP_SNPRINTF(soap->id, sizeof(soap->id), strlen(soap->dime_id_format) + 20), soap->dime_id_format, 0);
  17721. soap->dime.id = soap->id;
  17722. if (soap->local_namespaces && soap->local_namespaces[0].id) {
  17723. if (soap->local_namespaces[0].out)
  17724. soap->dime.type = (char *) soap->local_namespaces[0].out;
  17725. else
  17726. soap->dime.type = (char *) soap->local_namespaces[0].ns;
  17727. }
  17728. soap->dime.options = NULL;
  17729. soap->dime.flags = SOAP_DIME_MB | SOAP_DIME_ABSURI;
  17730. if (!soap->dime.first)
  17731. soap->dime.flags |= SOAP_DIME_ME;
  17732. soap->count += 12 + ((strlen(soap->dime.id) + 3) & (~3)) +
  17733. (soap->dime.type ? ((strlen(soap->dime.type) + 3) & (~3)) : 0);
  17734. }
  17735. if ((soap->mode & SOAP_ENC_DIME) && !(soap->mode & SOAP_ENC_MTOM))
  17736. return soap_send_raw(soap, SOAP_STR_PADDING, -(long) soap->dime.size & 3);
  17737. return SOAP_OK;
  17738. }
  17739. #endif
  17740. /******************************************************************************/
  17741. #ifndef WITH_LEANER
  17742. static struct soap_multipart *
  17743. soap_alloc_multipart(struct soap *soap, struct soap_multipart **first, struct soap_multipart **last, const char *ptr,
  17744. size_t size) {
  17745. struct soap_multipart *content;
  17746. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "New DIME/MIME attachment %p (%lu)\n", (void *) ptr, (unsigned long) size));
  17747. content = (struct soap_multipart *) soap_malloc(soap, sizeof(struct soap_multipart));
  17748. if (content) {
  17749. content->next = NULL;
  17750. content->ptr = ptr;
  17751. content->size = size;
  17752. content->id = NULL;
  17753. content->type = NULL;
  17754. content->options = NULL;
  17755. content->encoding = SOAP_MIME_NONE;
  17756. content->location = NULL;
  17757. content->description = NULL;
  17758. if (!*first)
  17759. *first = content;
  17760. if (*last)
  17761. (*last)->next = content;
  17762. *last = content;
  17763. }
  17764. return content;
  17765. }
  17766. #endif
  17767. /******************************************************************************/
  17768. #ifndef WITH_LEANER
  17769. SOAP_FMAC1
  17770. int
  17771. SOAP_FMAC2
  17772. soap_set_dime_attachment(struct soap *soap, const char *ptr, size_t size, const char *type, const char *id,
  17773. unsigned short optype, const char *option) {
  17774. struct soap_multipart *content = soap_alloc_multipart(soap, &soap->dime.first, &soap->dime.last, ptr, size);
  17775. if (!content)
  17776. return SOAP_EOM;
  17777. content->id = soap_strdup(soap, id);
  17778. content->type = soap_strdup(soap, type);
  17779. content->options = soap_dime_option(soap, optype, option);
  17780. return SOAP_OK;
  17781. }
  17782. #endif
  17783. /******************************************************************************/
  17784. #ifndef WITH_LEANER
  17785. SOAP_FMAC1
  17786. int
  17787. SOAP_FMAC2
  17788. soap_set_mime_attachment(struct soap *soap, const char *ptr, size_t size, enum soap_mime_encoding encoding,
  17789. const char *type, const char *id, const char *location, const char *description) {
  17790. struct soap_multipart *content = soap_alloc_multipart(soap, &soap->mime.first, &soap->mime.last, ptr, size);
  17791. if (!content)
  17792. return SOAP_EOM;
  17793. content->id = soap_strdup(soap, id);
  17794. content->type = soap_strdup(soap, type);
  17795. content->encoding = encoding;
  17796. content->location = soap_strdup(soap, location);
  17797. content->description = soap_strdup(soap, description);
  17798. return SOAP_OK;
  17799. }
  17800. #endif
  17801. /******************************************************************************/
  17802. #ifndef WITH_LEANER
  17803. SOAP_FMAC1
  17804. struct soap_multipart *
  17805. SOAP_FMAC2
  17806. soap_next_multipart(struct soap_multipart *content) {
  17807. if (content)
  17808. return content->next;
  17809. return NULL;
  17810. }
  17811. #endif
  17812. /******************************************************************************/
  17813. #ifndef WITH_LEANER
  17814. static void
  17815. soap_select_mime_boundary(struct soap *soap) {
  17816. while (!soap->mime.boundary || soap_valid_mime_boundary(soap)) {
  17817. char *s = soap->mime.boundary;
  17818. size_t n = 0;
  17819. if (s)
  17820. n = strlen(s);
  17821. if (n < 16) {
  17822. n = 64;
  17823. s = soap->mime.boundary = (char *) soap_malloc(soap, n + 1);
  17824. if (!s)
  17825. return;
  17826. }
  17827. *s++ = '=';
  17828. *s++ = '=';
  17829. n -= 4;
  17830. while (n) {
  17831. *s++ = soap_base64o[soap_random & 0x3F];
  17832. n--;
  17833. }
  17834. *s++ = '=';
  17835. *s++ = '=';
  17836. *s = '\0';
  17837. }
  17838. if (!soap->mime.start)
  17839. soap->mime.start = "<SOAP-ENV:Envelope>";
  17840. }
  17841. #endif
  17842. /******************************************************************************/
  17843. #ifndef WITH_LEANER
  17844. static int
  17845. soap_valid_mime_boundary(struct soap *soap) {
  17846. struct soap_multipart *content;
  17847. size_t k;
  17848. if (soap->fmimeread)
  17849. return SOAP_OK;
  17850. k = strlen(soap->mime.boundary);
  17851. for (content = soap->mime.first; content; content = content->next) {
  17852. if (content->ptr && content->size >= k) {
  17853. const char *p = (const char *) content->ptr;
  17854. size_t i;
  17855. for (i = 0; i < content->size - k; i++, p++) {
  17856. if (!strncmp(p, soap->mime.boundary, k))
  17857. return SOAP_ERR;
  17858. }
  17859. }
  17860. }
  17861. return SOAP_OK;
  17862. }
  17863. #endif
  17864. /******************************************************************************/
  17865. #ifdef WITH_GZIP
  17866. static int
  17867. soap_getgziphdr(struct soap *soap)
  17868. {
  17869. int i;
  17870. soap_wchar c = 0, f = 0;
  17871. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Get gzip header\n"));
  17872. for (i = 0; i < 9; i++)
  17873. {
  17874. c = soap_get1(soap);
  17875. if (i == 1 && c == 8)
  17876. soap->z_dict = 0;
  17877. if (i == 2)
  17878. f = c;
  17879. }
  17880. if (f & 0x04) /* FEXTRA */
  17881. {
  17882. i = soap_get1(soap);
  17883. i |= soap_get1(soap) << 8;
  17884. while (i-- > 0)
  17885. {
  17886. if ((int)soap_get1(soap) == EOF)
  17887. return soap->error = SOAP_ZLIB_ERROR;
  17888. }
  17889. }
  17890. if (f & 0x08) /* skip FNAME */
  17891. {
  17892. do
  17893. {
  17894. c = soap_get1(soap);
  17895. } while (c && (int)c != EOF);
  17896. }
  17897. if ((int)c != EOF && (f & 0x10)) /* skip FCOMMENT */
  17898. {
  17899. do
  17900. {
  17901. c = soap_get1(soap);
  17902. } while (c && (int)c != EOF);
  17903. }
  17904. if ((int)c != EOF && (f & 0x02)) /* skip FHCRC (CRC32 is used) */
  17905. {
  17906. c = soap_get1(soap);
  17907. if ((int)c != EOF)
  17908. c = soap_get1(soap);
  17909. }
  17910. if ((int)c == EOF)
  17911. return soap->error = SOAP_ZLIB_ERROR;
  17912. return SOAP_OK;
  17913. }
  17914. #endif
  17915. /******************************************************************************/
  17916. SOAP_FMAC1
  17917. int
  17918. SOAP_FMAC2
  17919. soap_begin_serve(struct soap *soap) {
  17920. #ifdef WITH_FASTCGI
  17921. if (FCGI_Accept() < 0)
  17922. {
  17923. soap->error = SOAP_EOF;
  17924. return soap_send_fault(soap);
  17925. }
  17926. #endif
  17927. soap_begin(soap);
  17928. if (soap_begin_recv(soap)
  17929. || soap_envelope_begin_in(soap)
  17930. || soap_recv_header(soap)
  17931. || soap_body_begin_in(soap)) {
  17932. if (soap->error < SOAP_STOP) {
  17933. #ifdef WITH_FASTCGI
  17934. (void)soap_send_fault(soap);
  17935. #else
  17936. return soap_send_fault(soap);
  17937. #endif
  17938. }
  17939. return soap_closesock(soap);
  17940. }
  17941. return SOAP_OK;
  17942. }
  17943. /******************************************************************************/
  17944. SOAP_FMAC1
  17945. int
  17946. SOAP_FMAC2
  17947. soap_begin_recv(struct soap *soap) {
  17948. // std::cerr<<"Recieved data Sassan2: "<<soap_getchar(soap)<<std::endl;
  17949. soap_wchar c;
  17950. DBGLOG(TEST,
  17951. SOAP_MESSAGE(fdebug, "Initializing for input from socket=%d/fd=%d\n", (int) soap->socket, soap->recvfd));
  17952. soap->error = SOAP_OK;
  17953. #ifndef WITH_LEANER
  17954. soap->recverror = SOAP_OK;
  17955. #endif
  17956. soap_free_temp(soap);
  17957. soap_set_local_namespaces(soap);
  17958. soap->version = 0; /* don't assume we're parsing SOAP content by default */
  17959. #ifndef WITH_NOIDREF
  17960. soap_free_iht(soap);
  17961. #endif
  17962. if ((soap->imode & SOAP_IO) == SOAP_IO_CHUNK) {
  17963. soap->imode &= ~SOAP_IO;
  17964. soap->omode = (soap->omode & ~SOAP_IO) | SOAP_IO_CHUNK;
  17965. }
  17966. soap->imode &= ~(SOAP_ENC_DIME | SOAP_ENC_MIME | SOAP_ENC_MTOM | SOAP_ENC_ZLIB);
  17967. soap->mode = soap->imode;
  17968. if (!(soap->mode & SOAP_IO_KEEPALIVE))
  17969. soap->keep_alive = 0;
  17970. if (!soap->keep_alive)
  17971. soap->buflen = soap->bufidx = 0;
  17972. soap->null = 0;
  17973. soap->position = 0;
  17974. soap->mustUnderstand = 0;
  17975. soap->shaky = 0;
  17976. soap->ahead = 0;
  17977. soap->peeked = 0;
  17978. soap->level = 0;
  17979. soap->part = SOAP_BEGIN_RECV;
  17980. soap->count = 0;
  17981. soap->length = 0;
  17982. soap->cdata = 0;
  17983. *soap->endpoint = '\0';
  17984. soap->action = NULL;
  17985. soap->header = NULL;
  17986. soap->fault = NULL;
  17987. soap->status = 0;
  17988. soap->fform = NULL;
  17989. soap->body = 1;
  17990. #ifndef WITH_LEANER
  17991. soap->dom = NULL;
  17992. soap->dime.count = 0;
  17993. soap->dime.chunksize = 0;
  17994. soap->dime.buflen = 0;
  17995. soap->dime.list = NULL;
  17996. soap->dime.first = NULL;
  17997. soap->dime.last = NULL;
  17998. soap->mime.list = NULL;
  17999. soap->mime.first = NULL;
  18000. soap->mime.last = NULL;
  18001. soap->mime.boundary = NULL;
  18002. soap->mime.start = NULL;
  18003. #endif
  18004. #ifdef WIN32
  18005. #ifndef UNDER_CE
  18006. #ifndef WITH_FASTCGI
  18007. if (!soap_valid_socket(soap->socket) && !soap->is && soap->recvfd >= 0) /* Set win32 stdin or soap->recvfd to BINARY, e.g. to support DIME */
  18008. #ifdef __BORLANDC__
  18009. setmode(soap->recvfd, _O_BINARY);
  18010. #else
  18011. _setmode(soap->recvfd, _O_BINARY);
  18012. #endif
  18013. #endif
  18014. #endif
  18015. #endif
  18016. #ifdef WITH_ZLIB
  18017. soap->zlib_in = SOAP_ZLIB_NONE;
  18018. soap->zlib_out = SOAP_ZLIB_NONE;
  18019. if (!soap->d_stream)
  18020. {
  18021. soap->d_stream = (z_stream*)SOAP_MALLOC(soap, sizeof(z_stream));
  18022. if (!soap->d_stream)
  18023. return soap->error = SOAP_EOM;
  18024. soap->d_stream->zalloc = Z_NULL;
  18025. soap->d_stream->zfree = Z_NULL;
  18026. soap->d_stream->opaque = Z_NULL;
  18027. soap->d_stream->next_in = Z_NULL;
  18028. soap->d_stream->msg = Z_NULL;
  18029. }
  18030. soap->d_stream->avail_in = 0;
  18031. soap->d_stream->next_out = (Byte*)soap->buf;
  18032. soap->d_stream->avail_out = sizeof(soap->buf);
  18033. soap->z_ratio_in = 1.0;
  18034. #endif
  18035. #ifdef WITH_OPENSSL
  18036. if (soap->ssl)
  18037. ERR_clear_error();
  18038. #endif
  18039. #ifndef WITH_LEAN
  18040. soap->start = (ULONG64) time(NULL);
  18041. #endif
  18042. #ifndef WITH_LEANER
  18043. if (soap->fprepareinitrecv && (soap->error = soap->fprepareinitrecv(soap)) != SOAP_OK)
  18044. return soap->error;
  18045. #endif
  18046. c = soap_getchar(soap);
  18047. #ifdef WITH_GZIP
  18048. if (c == 0x1F)
  18049. {
  18050. if (soap_getgziphdr(soap))
  18051. return soap->error;
  18052. if (inflateInit2(soap->d_stream, -MAX_WBITS) != Z_OK)
  18053. return soap->error = SOAP_ZLIB_ERROR;
  18054. if (soap->z_dict)
  18055. {
  18056. if (inflateSetDictionary(soap->d_stream, (const Bytef*)soap->z_dict, soap->z_dict_len) != Z_OK)
  18057. return soap->error = SOAP_ZLIB_ERROR;
  18058. }
  18059. soap->zlib_state = SOAP_ZLIB_INFLATE;
  18060. soap->mode |= SOAP_ENC_ZLIB;
  18061. soap->zlib_in = SOAP_ZLIB_GZIP;
  18062. soap->z_crc = crc32(0L, NULL, 0);
  18063. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "gzip initialized\n"));
  18064. if (!soap->z_buf)
  18065. soap->z_buf = (char*)SOAP_MALLOC(soap, sizeof(soap->buf));
  18066. (void)soap_memcpy((void*)soap->z_buf, sizeof(soap->buf), (const void*)soap->buf, sizeof(soap->buf));
  18067. /* should not chunk over plain transport, so why bother to check? */
  18068. /* if ((soap->mode & SOAP_IO) == SOAP_IO_CHUNK) */
  18069. /* soap->z_buflen = soap->bufidx; */
  18070. /* else */
  18071. soap->d_stream->next_in = (Byte*)(soap->z_buf + soap->bufidx);
  18072. soap->d_stream->avail_in = (unsigned int)(soap->buflen - soap->bufidx);
  18073. soap->z_buflen = soap->buflen;
  18074. soap->buflen = soap->bufidx;
  18075. c = ' ';
  18076. }
  18077. #endif
  18078. while (soap_coblank(c))
  18079. c = soap_getchar(soap);
  18080. #ifndef WITH_LEANER
  18081. if (c == '-' && soap_get0(soap) == '-') {
  18082. soap->mode |= SOAP_ENC_MIME;
  18083. } else if ((c & 0xFFFC) == (SOAP_DIME_VERSION | SOAP_DIME_MB) && (soap_get0(soap) & 0xFFF0) == 0x20) {
  18084. soap->mode |= SOAP_ENC_DIME;
  18085. } else
  18086. #endif
  18087. {
  18088. /* skip BOM */
  18089. if (c == 0xEF && soap_get0(soap) == 0xBB) {
  18090. soap_get1(soap);
  18091. c = soap_get1(soap);
  18092. if (c == 0xBF) {
  18093. soap->mode &= ~SOAP_ENC_LATIN;
  18094. c = soap_getchar(soap);
  18095. } else {
  18096. c = (0x0F << 12) | (0xBB << 6) | (c & 0x3F); /* UTF-8 */
  18097. }
  18098. } else if ((c == 0xFE && soap_get0(soap) == 0xFF) /* UTF-16 BE */
  18099. || (c == 0xFF && soap_get0(soap) == 0xFE)) /* UTF-16 LE */
  18100. {
  18101. return soap->error = SOAP_UTF_ERROR;
  18102. }
  18103. /* skip space */
  18104. while (soap_coblank(c))
  18105. c = soap_getchar(soap);
  18106. }
  18107. if ((int) c == EOF)
  18108. return soap->error = SOAP_CHK_EOF;
  18109. soap_unget(soap, c);
  18110. #ifndef WITH_NOHTTP
  18111. /* if not XML/MIME/DIME/ZLIB, assume HTTP method or status line */
  18112. if (((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')) &&
  18113. !(soap->mode & (SOAP_ENC_MIME | SOAP_ENC_DIME | SOAP_ENC_ZLIB | SOAP_ENC_PLAIN))) {
  18114. soap_mode m = soap->imode;
  18115. soap->error = soap->fparse(soap);
  18116. soap->mode = soap->imode; /* if imode is changed, effectuate */
  18117. soap->imode = m; /* restore imode */
  18118. if ((soap->mode & SOAP_IO) == SOAP_IO_CHUNK) {
  18119. soap->chunkbuflen = soap->buflen;
  18120. soap->buflen = soap->bufidx;
  18121. soap->chunksize = 0;
  18122. }
  18123. #ifdef WITH_ZLIB
  18124. soap->mode &= ~SOAP_ENC_ZLIB;
  18125. if (soap->zlib_in != SOAP_ZLIB_NONE)
  18126. {
  18127. #ifdef WITH_GZIP
  18128. if (soap->zlib_in != SOAP_ZLIB_DEFLATE)
  18129. {
  18130. c = soap_get1(soap);
  18131. if (c == (int)EOF)
  18132. return soap->error = SOAP_EOF;
  18133. if (c == 0x1F)
  18134. {
  18135. if (soap_getgziphdr(soap))
  18136. return soap->error;
  18137. if (inflateInit2(soap->d_stream, -MAX_WBITS) != Z_OK)
  18138. return soap->error = SOAP_ZLIB_ERROR;
  18139. soap->z_crc = crc32(0L, NULL, 0);
  18140. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "gzip initialized\n"));
  18141. }
  18142. else
  18143. {
  18144. soap_revget1(soap);
  18145. if (inflateInit(soap->d_stream) != Z_OK)
  18146. return soap->error = SOAP_ZLIB_ERROR;
  18147. soap->zlib_in = SOAP_ZLIB_DEFLATE;
  18148. }
  18149. }
  18150. else
  18151. #endif
  18152. if (inflateInit(soap->d_stream) != Z_OK)
  18153. return soap->error = SOAP_ZLIB_ERROR;
  18154. if (soap->z_dict)
  18155. {
  18156. if (inflateSetDictionary(soap->d_stream, (const Bytef*)soap->z_dict, soap->z_dict_len) != Z_OK)
  18157. return soap->error = SOAP_ZLIB_ERROR;
  18158. }
  18159. soap->zlib_state = SOAP_ZLIB_INFLATE;
  18160. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Inflate initialized\n"));
  18161. soap->mode |= SOAP_ENC_ZLIB;
  18162. if (!soap->z_buf)
  18163. soap->z_buf = (char*)SOAP_MALLOC(soap, sizeof(soap->buf));
  18164. (void)soap_memcpy((void*)soap->z_buf, sizeof(soap->buf), (const void*)soap->buf, sizeof(soap->buf));
  18165. soap->d_stream->next_in = (Byte*)(soap->z_buf + soap->bufidx);
  18166. soap->d_stream->avail_in = (unsigned int)(soap->buflen - soap->bufidx);
  18167. soap->z_buflen = soap->buflen;
  18168. soap->buflen = soap->bufidx;
  18169. }
  18170. #endif
  18171. #ifndef WITH_LEANER
  18172. if (soap->fpreparerecv && (soap->mode & SOAP_IO) != SOAP_IO_CHUNK && soap->buflen > soap->bufidx) {
  18173. int r;
  18174. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Invoking fpreparerecv\n"));
  18175. r = soap->fpreparerecv(soap, soap->buf + soap->bufidx, soap->buflen - soap->bufidx);
  18176. if (r)
  18177. return soap->error = r;
  18178. }
  18179. #endif
  18180. if (soap->error && soap->error < SOAP_STOP) {
  18181. if (soap->status >= 200 && soap->status < 600) {
  18182. const char *s = soap_http_get_body(soap, NULL);
  18183. (void) soap_end_recv(soap);
  18184. if (soap->status >= 300)
  18185. soap->keep_alive = 0; /* to force close */
  18186. return soap_set_receiver_error(soap, "HTTP Error", s, soap->status);
  18187. }
  18188. return soap->error;
  18189. }
  18190. if (!soap->body && soap->status >= 200 && soap->status < 600)
  18191. return soap->error = soap->status; /* client side received HTTP status code */
  18192. if (soap->status > SOAP_POST) {
  18193. soap->fform = NULL;
  18194. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Invoking http method handler\n"));
  18195. switch (soap->status) {
  18196. case SOAP_GET:
  18197. if (soap_http_skip_body(soap) || soap_end_recv(soap))
  18198. return soap->error;
  18199. soap->error = soap->fget(soap);
  18200. break;
  18201. case SOAP_PUT:
  18202. soap->error = soap->fput(soap);
  18203. break;
  18204. case SOAP_PATCH:
  18205. soap->error = soap->fpatch(soap);
  18206. break;
  18207. case SOAP_DEL:
  18208. if (soap_http_skip_body(soap) || soap_end_recv(soap))
  18209. return soap->error;
  18210. soap->error = soap->fdel(soap);
  18211. break;
  18212. case SOAP_HEAD:
  18213. if (soap_http_skip_body(soap) || soap_end_recv(soap))
  18214. return soap->error;
  18215. soap->error = soap->fhead(soap);
  18216. break;
  18217. case SOAP_OPTIONS:
  18218. if (soap_http_skip_body(soap) || soap_end_recv(soap))
  18219. return soap->error;
  18220. soap->error = soap->fopt(soap);
  18221. break;
  18222. default:
  18223. if (soap_http_skip_body(soap) || soap_end_recv(soap))
  18224. return soap->error;
  18225. return 405;
  18226. }
  18227. if (soap->error == SOAP_OK)
  18228. return soap->error = SOAP_STOP; /* prevents further processing */
  18229. if (soap->error != SOAP_FORM || !soap->fform) /* continue if handler returned SOAP_FORM */
  18230. return soap->error;
  18231. soap->error = SOAP_OK;
  18232. }
  18233. if (soap->fform) {
  18234. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Invoking http form handler\n"));
  18235. soap->error = soap->fform(soap);
  18236. if (soap->error == SOAP_OK)
  18237. return soap->error = SOAP_STOP; /* prevents further processing */
  18238. if (soap->status != SOAP_POST ||
  18239. soap->error != 404) /* continue with POST if handler returned HTTP not found */
  18240. return soap->error;
  18241. soap->error = SOAP_OK;
  18242. }
  18243. if (!soap->body)//Sassan No_Data
  18244. {
  18245. std::cout << "No Content Recieved at " << time(NULL) << " for " << soap->host <<" Calling CheckQueue..."<< std::endl;
  18246. return soap->error =20400 ;
  18247. }
  18248. }
  18249. #endif
  18250. #ifndef WITH_LEANER
  18251. if ((soap->mode & SOAP_ENC_MIME)) {
  18252. do /* skip preamble */
  18253. {
  18254. c = soap_getchar(soap);
  18255. if ((int) c == EOF)
  18256. return soap->error = SOAP_CHK_EOF;
  18257. } while (c != '-' || soap_get0(soap) != '-');
  18258. soap_unget(soap, c);
  18259. if (soap_getmimehdr(soap))
  18260. return soap->error;
  18261. if (soap->mime.start) {
  18262. do {
  18263. if (!soap->mime.last->id)
  18264. break;
  18265. if (!soap_match_cid(soap, soap->mime.start, soap->mime.last->id))
  18266. break;
  18267. } while (soap_recv_mime_attachment(soap, NULL));
  18268. }
  18269. if (soap_http_header_attribute(soap, soap->mime.first->type, "application/dime"))
  18270. soap->mode |= SOAP_ENC_DIME;
  18271. }
  18272. if ((soap->mode & SOAP_ENC_DIME)) {
  18273. if (soap_getdimehdr(soap))
  18274. return soap->error;
  18275. if ((soap->dime.flags & SOAP_DIME_CF)) {
  18276. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Chunked SOAP in DIME message\n"));
  18277. soap->dime.chunksize = soap->dime.size;
  18278. if (soap->buflen - soap->bufidx >= soap->dime.chunksize) {
  18279. soap->dime.buflen = soap->buflen;
  18280. soap->buflen = soap->bufidx + soap->dime.chunksize;
  18281. } else {
  18282. soap->dime.chunksize -= soap->buflen - soap->bufidx;
  18283. }
  18284. }
  18285. soap->count = soap->buflen - soap->bufidx;
  18286. if (soap->recv_maxlength && soap->count > soap->recv_maxlength)
  18287. return soap->error = SOAP_EOF;
  18288. }
  18289. #endif
  18290. return SOAP_OK;
  18291. }
  18292. /******************************************************************************/
  18293. SOAP_FMAC1
  18294. int
  18295. SOAP_FMAC2
  18296. soap_envelope_begin_out(struct soap *soap) {
  18297. if (soap->version == 0)
  18298. return SOAP_OK;
  18299. soap->part = SOAP_IN_ENVELOPE;
  18300. return soap_element_begin_out(soap, "SOAP-ENV:Envelope", 0, NULL);
  18301. }
  18302. /******************************************************************************/
  18303. SOAP_FMAC1
  18304. int
  18305. SOAP_FMAC2
  18306. soap_envelope_end_out(struct soap *soap) {
  18307. if (soap->version == 0)
  18308. return SOAP_OK;
  18309. if (soap_element_end_out(soap, "SOAP-ENV:Envelope")
  18310. || soap_send_raw(soap, "\r\n", 2)) /* 2.8: always emit \r\n */
  18311. return soap->error;
  18312. soap->part = SOAP_END_ENVELOPE;
  18313. return SOAP_OK;
  18314. }
  18315. /******************************************************************************/
  18316. SOAP_FMAC1
  18317. int
  18318. SOAP_FMAC2
  18319. soap_http_has_body(struct soap *soap) {
  18320. return soap->length || (soap->mode & SOAP_ENC_ZLIB) || (soap->mode & SOAP_IO) == SOAP_IO_CHUNK;
  18321. }
  18322. /******************************************************************************/
  18323. SOAP_FMAC1
  18324. int
  18325. SOAP_FMAC2
  18326. soap_http_skip_body(struct soap *soap) {
  18327. ULONG64 k = soap->length;
  18328. /* check HTTP body, return "" if none */
  18329. if (!k && !(soap->mode & SOAP_ENC_ZLIB) && (soap->mode & SOAP_IO) != SOAP_IO_CHUNK)
  18330. return SOAP_OK;
  18331. /* do not consume DIME or MIME attachments, leave this to soap_end_recv */
  18332. if ((soap->mode & SOAP_ENC_DIME) || (soap->mode & SOAP_ENC_MIME))
  18333. return SOAP_OK;
  18334. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Skipping HTTP body (mode=0x%x)\n", soap->mode));
  18335. if (k && !(soap->mode & SOAP_ENC_ZLIB)) {
  18336. size_t i;
  18337. soap->length = 0;
  18338. for (i = 0; i < k; i++) {
  18339. soap_wchar c = soap_get1(soap);
  18340. if ((int) c == EOF)
  18341. break;
  18342. }
  18343. } else {
  18344. for (;;) {
  18345. soap_wchar c = soap_get1(soap);
  18346. if ((int) c == EOF)
  18347. break;
  18348. }
  18349. }
  18350. return SOAP_OK;
  18351. }
  18352. /******************************************************************************/
  18353. SOAP_FMAC1
  18354. char *
  18355. SOAP_FMAC2
  18356. soap_http_get_body(struct soap *soap, size_t *len) {
  18357. return soap_http_get_body_prefix(soap, len, NULL);
  18358. }
  18359. /******************************************************************************/
  18360. SOAP_FMAC1
  18361. char *
  18362. SOAP_FMAC2
  18363. soap_http_get_form(struct soap *soap) {
  18364. return soap_http_get_body_prefix(soap, NULL, "?");
  18365. }
  18366. /******************************************************************************/
  18367. SOAP_FMAC1
  18368. char *
  18369. SOAP_FMAC2
  18370. soap_http_get_body_prefix(struct soap *soap, size_t *len, const char *prefix) {
  18371. char *s;
  18372. ULONG64 k = soap->length;
  18373. size_t n = 0;
  18374. if (!prefix)
  18375. prefix = SOAP_STR_EOS;
  18376. else
  18377. n = strlen(prefix);
  18378. if (len)
  18379. *len = 0;
  18380. /* check HTTP body, return "" if none */
  18381. if (!k && !(soap->mode & SOAP_ENC_ZLIB) && (soap->mode & SOAP_IO) != SOAP_IO_CHUNK)
  18382. return soap_strdup(soap, prefix);
  18383. /* do not consume DIME or MIME attachments, leave this to soap_end_recv */
  18384. if ((soap->mode & SOAP_ENC_DIME) || (soap->mode & SOAP_ENC_MIME))
  18385. return soap_strdup(soap, prefix);
  18386. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Parsing HTTP body, prefixed with '%s' (mode=0x%x)\n", prefix, soap->mode));
  18387. if (k && !(soap->mode & SOAP_ENC_ZLIB)) {
  18388. char *t;
  18389. soap->length = 0;
  18390. /* http content length != 0 and uncompressed body */
  18391. if ((SOAP_MAXALLOCSIZE != 0 && n + k > SOAP_MAXALLOCSIZE) || n + k > (ULONG64) ((size_t) -2)) {
  18392. soap->error = SOAP_EOM;
  18393. return NULL;
  18394. }
  18395. s = t = (char *) soap_malloc(soap, (size_t) k + n + 1);
  18396. if (s) {
  18397. size_t i;
  18398. soap_strcpy(t, n + 1, prefix);
  18399. t += n;
  18400. for (i = 0; i < k; i++) {
  18401. soap_wchar c = soap_get1(soap);
  18402. if ((int) c == EOF)
  18403. break;
  18404. *t++ = (char) (c & 0xFF);
  18405. }
  18406. *t = '\0';
  18407. if (len)
  18408. *len = n + i;
  18409. } else {
  18410. soap->error = SOAP_EOM;
  18411. return NULL;
  18412. }
  18413. } else {
  18414. size_t i, l = 0;
  18415. if (soap_alloc_block(soap) == NULL)
  18416. return NULL;
  18417. if (n) {
  18418. s = (char *) soap_push_block(soap, NULL, n);
  18419. if (!s)
  18420. return NULL;
  18421. soap_strcpy(s, n + 1, prefix);
  18422. l += n;
  18423. }
  18424. for (;;) {
  18425. size_t k = SOAP_BLKLEN;
  18426. s = (char *) soap_push_block(soap, NULL, k);
  18427. if (!s)
  18428. return NULL;
  18429. for (i = 0; i < k; i++) {
  18430. soap_wchar c;
  18431. l++;
  18432. if (l == 0) {
  18433. soap->error = SOAP_EOM;
  18434. return NULL;
  18435. }
  18436. c = soap_get1(soap);
  18437. if ((int) c == EOF)
  18438. goto end;
  18439. *s++ = (char) (c & 0xFF);
  18440. }
  18441. }
  18442. end:
  18443. *s = '\0';
  18444. if (len)
  18445. *len = l - 1;
  18446. soap_size_block(soap, NULL, i + 1);
  18447. s = soap_save_block(soap, NULL, NULL, 0);
  18448. }
  18449. return s;
  18450. }
  18451. /******************************************************************************/
  18452. SOAP_FMAC1
  18453. int
  18454. SOAP_FMAC2
  18455. soap_envelope_begin_in(struct soap *soap) {
  18456. soap->part = SOAP_IN_ENVELOPE;
  18457. if (soap_element_begin_in(soap, "SOAP-ENV:Envelope", 0, NULL)) {
  18458. if (soap->error == SOAP_TAG_MISMATCH) {
  18459. if (!soap_element_begin_in(soap, "Envelope", 0, NULL))
  18460. soap->error = SOAP_VERSIONMISMATCH;
  18461. else if (soap->status == 0
  18462. || (soap->status >= 200 && soap->status <= 299)
  18463. || soap->status == 400
  18464. || soap->status == 500)
  18465. return SOAP_OK; /* allow non-SOAP (REST) XML content to be captured */
  18466. soap->error = soap->status;
  18467. } else if (soap->status) {
  18468. soap->error = soap->status;
  18469. }
  18470. return soap->error;
  18471. }
  18472. soap_version(soap);
  18473. return SOAP_OK;
  18474. }
  18475. /******************************************************************************/
  18476. SOAP_FMAC1
  18477. int
  18478. SOAP_FMAC2
  18479. soap_envelope_end_in(struct soap *soap) {
  18480. if (soap->version == 0)
  18481. return SOAP_OK;
  18482. soap->part = SOAP_END_ENVELOPE;
  18483. return soap_element_end_in(soap, "SOAP-ENV:Envelope");
  18484. }
  18485. /******************************************************************************/
  18486. SOAP_FMAC1
  18487. int
  18488. SOAP_FMAC2
  18489. soap_body_begin_out(struct soap *soap) {
  18490. if (soap->version == 1)
  18491. soap->encoding = 1;
  18492. #ifndef WITH_LEAN
  18493. if ((soap->mode & SOAP_SEC_WSUID) && soap_set_attr(soap, "wsu:Id", "Body", 1))
  18494. return soap->error;
  18495. #endif
  18496. if (soap->version == 0)
  18497. return SOAP_OK;
  18498. soap->part = SOAP_IN_BODY;
  18499. return soap_element_begin_out(soap, "SOAP-ENV:Body", 0, NULL);
  18500. }
  18501. /******************************************************************************/
  18502. SOAP_FMAC1
  18503. int
  18504. SOAP_FMAC2
  18505. soap_body_end_out(struct soap *soap) {
  18506. if (soap->version == 0)
  18507. return SOAP_OK;
  18508. if (soap_element_end_out(soap, "SOAP-ENV:Body"))
  18509. return soap->error;
  18510. soap->part = SOAP_END_BODY;
  18511. return SOAP_OK;
  18512. }
  18513. /******************************************************************************/
  18514. SOAP_FMAC1
  18515. int
  18516. SOAP_FMAC2
  18517. soap_body_begin_in(struct soap *soap) {
  18518. if (soap->version == 0)
  18519. return SOAP_OK;
  18520. soap->part = SOAP_IN_BODY;
  18521. if (soap_element_begin_in(soap, "SOAP-ENV:Body", 0, NULL))
  18522. return soap->error;
  18523. if (!soap->body) {
  18524. return SOAP_NO_DATA;
  18525. }
  18526. return SOAP_OK;
  18527. }
  18528. /******************************************************************************/
  18529. SOAP_FMAC1
  18530. int
  18531. SOAP_FMAC2
  18532. soap_body_end_in(struct soap *soap) {
  18533. if (soap->version == 0)
  18534. return SOAP_OK;
  18535. if (soap->part == SOAP_NO_BODY)
  18536. return soap->error = SOAP_OK;
  18537. soap->part = SOAP_END_BODY;
  18538. return soap_element_end_in(soap, "SOAP-ENV:Body");
  18539. }
  18540. /******************************************************************************/
  18541. SOAP_FMAC1
  18542. int
  18543. SOAP_FMAC2
  18544. soap_recv_header(struct soap *soap) {
  18545. if (soap_getheader(soap) && soap->error == SOAP_TAG_MISMATCH)
  18546. soap->error = SOAP_OK;
  18547. if (soap->error == SOAP_OK && soap->fheader)
  18548. soap->error = soap->fheader(soap);
  18549. return soap->error;
  18550. }
  18551. /******************************************************************************/
  18552. SOAP_FMAC1
  18553. void
  18554. SOAP_FMAC2
  18555. soap_set_endpoint(struct soap *soap, const char *endpoint) {
  18556. const char *s, *t;
  18557. size_t i, n;
  18558. soap->endpoint[0] = '\0';
  18559. soap->host[0] = '\0';
  18560. soap->path[0] = '/';
  18561. soap->path[1] = '\0';
  18562. soap->port = 80;
  18563. if (!endpoint || !*endpoint)
  18564. return;
  18565. #ifdef WITH_OPENSSL
  18566. if (!soap_tag_cmp(endpoint, "https:*"))
  18567. soap->port = 443;
  18568. #endif
  18569. soap_strcpy(soap->endpoint, sizeof(soap->endpoint), endpoint);
  18570. s = strchr(endpoint, ':');
  18571. if (s && s[1] == '/' && s[2] == '/')
  18572. s += 3;
  18573. else
  18574. s = endpoint;
  18575. #if !defined(WITH_NOHTTP) || !defined(WITH_LEANER)
  18576. t = strchr(s, '@');
  18577. if (t && *s != ':' && *s != '@') {
  18578. size_t l = t - s + 1;
  18579. char *r = (char *) soap_malloc(soap, l);
  18580. n = s - endpoint;
  18581. if (r) {
  18582. s = soap_decode(r, l, s, ":@");
  18583. soap->userid = r;
  18584. soap->passwd = SOAP_STR_EOS;
  18585. if (*s == ':') {
  18586. s++;
  18587. if (*s != '@') {
  18588. l = t - s + 1;
  18589. r = r + strlen(r) + 1;
  18590. s = soap_decode(r, l, s, "@");
  18591. soap->passwd = r;
  18592. }
  18593. }
  18594. }
  18595. s++;
  18596. soap_strcpy(soap->endpoint + n, sizeof(soap->endpoint) - n, s);
  18597. }
  18598. #endif
  18599. n = strlen(s);
  18600. if (n >= sizeof(soap->host))
  18601. n = sizeof(soap->host) - 1;
  18602. #ifdef WITH_IPV6
  18603. if (s[0] == '[')
  18604. {
  18605. s++;
  18606. for (i = 0; i < n; i++)
  18607. {
  18608. if (s[i] == ']')
  18609. {
  18610. s++;
  18611. --n;
  18612. break;
  18613. }
  18614. soap->host[i] = s[i];
  18615. }
  18616. }
  18617. else
  18618. {
  18619. for (i = 0; i < n; i++)
  18620. {
  18621. soap->host[i] = s[i];
  18622. if (s[i] == '/' || s[i] == ':' || s[i] == '?')
  18623. break;
  18624. }
  18625. }
  18626. #else
  18627. for (i = 0; i < n; i++) {
  18628. soap->host[i] = s[i];
  18629. if (s[i] == '/' || s[i] == ':' || s[i] == '?')
  18630. break;
  18631. }
  18632. #endif
  18633. soap->host[i] = '\0';
  18634. if (s[i] == ':') {
  18635. soap->port = (int) soap_strtol(s + i + 1, NULL, 10);
  18636. for (i++; i < n; i++)
  18637. if (s[i] == '/')
  18638. break;
  18639. }
  18640. if (i < n && s[i])
  18641. soap_strcpy(soap->path, sizeof(soap->path), s + i);
  18642. if (soap->override_host && *soap->override_host) {
  18643. soap_strcpy(soap->host, sizeof(soap->host), soap->override_host);
  18644. if (soap->override_port)
  18645. soap->port = soap->override_port;
  18646. }
  18647. if (soap->userid && !soap->authrealm)
  18648. soap->authrealm = soap->host;
  18649. }
  18650. /******************************************************************************/
  18651. #ifndef WITH_NOHTTP
  18652. SOAP_FMAC1
  18653. int
  18654. SOAP_FMAC2
  18655. soap_GET(struct soap *soap, const char *endpoint, const char *action) {
  18656. return soap_connect_command(soap, SOAP_GET, endpoint, action);
  18657. }
  18658. #endif
  18659. /******************************************************************************/
  18660. #ifndef WITH_NOHTTP
  18661. SOAP_FMAC1
  18662. int
  18663. SOAP_FMAC2
  18664. soap_PUT(struct soap *soap, const char *endpoint, const char *action, const char *type) {
  18665. soap->http_content = type;
  18666. if ((soap->omode & SOAP_IO) != SOAP_IO_CHUNK) {
  18667. soap->omode &= ~SOAP_IO;
  18668. soap->omode |= SOAP_IO_STORE;
  18669. }
  18670. return soap_connect_command(soap, SOAP_PUT, endpoint, action);
  18671. }
  18672. #endif
  18673. /******************************************************************************/
  18674. #ifndef WITH_NOHTTP
  18675. SOAP_FMAC1
  18676. int
  18677. SOAP_FMAC2
  18678. soap_POST(struct soap *soap, const char *endpoint, const char *action, const char *type) {
  18679. soap->http_content = type;
  18680. if ((soap->omode & SOAP_IO) != SOAP_IO_CHUNK) {
  18681. soap->omode &= ~SOAP_IO;
  18682. soap->omode |= SOAP_IO_STORE;
  18683. }
  18684. return soap_connect_command(soap, SOAP_POST_FILE, endpoint, action);
  18685. }
  18686. #endif
  18687. /******************************************************************************/
  18688. #ifndef WITH_NOHTTP
  18689. SOAP_FMAC1
  18690. int
  18691. SOAP_FMAC2
  18692. soap_PATCH(struct soap *soap, const char *endpoint, const char *action, const char *type) {
  18693. soap->http_content = type;
  18694. if ((soap->omode & SOAP_IO) != SOAP_IO_CHUNK) {
  18695. soap->omode &= ~SOAP_IO;
  18696. soap->omode |= SOAP_IO_STORE;
  18697. }
  18698. return soap_connect_command(soap, SOAP_PATCH, endpoint, action);
  18699. }
  18700. #endif
  18701. /******************************************************************************/
  18702. #ifndef WITH_NOHTTP
  18703. SOAP_FMAC1
  18704. int
  18705. SOAP_FMAC2
  18706. soap_DELETE(struct soap *soap, const char *endpoint) {
  18707. if (soap_connect_command(soap, SOAP_DEL, endpoint, NULL)
  18708. || soap_recv_empty_response(soap))
  18709. return soap_closesock(soap);
  18710. return SOAP_OK;
  18711. }
  18712. #endif
  18713. /******************************************************************************/
  18714. SOAP_FMAC1
  18715. int
  18716. SOAP_FMAC2
  18717. soap_connect(struct soap *soap, const char *endpoint, const char *action) {
  18718. return soap_connect_command(soap, SOAP_POST, endpoint, action);
  18719. }
  18720. /******************************************************************************/
  18721. SOAP_FMAC1
  18722. int
  18723. SOAP_FMAC2
  18724. soap_connect_command(struct soap *soap, int http_command, const char *endpoints, const char *action) {
  18725. if (endpoints) {
  18726. const char *s;
  18727. s = strchr(endpoints, ' ');
  18728. if (s) {
  18729. size_t l = strlen(endpoints);
  18730. char *endpoint = NULL;
  18731. if (SOAP_MAXALLOCSIZE == 0 || l <= SOAP_MAXALLOCSIZE)
  18732. endpoint = (char *) SOAP_MALLOC(soap, l + 1);
  18733. if (!endpoint)
  18734. return soap->error = SOAP_EOM;
  18735. for (;;) {
  18736. (void) soap_strncpy(endpoint, l + 1, endpoints, s - endpoints);
  18737. endpoint[s - endpoints] = '\0';
  18738. if (soap_try_connect_command(soap, http_command, endpoint, action) != SOAP_TCP_ERROR)
  18739. break;
  18740. if (!*s)
  18741. break;
  18742. soap->error = SOAP_OK;
  18743. while (*s == ' ')
  18744. s++;
  18745. endpoints = s;
  18746. s = strchr(endpoints, ' ');
  18747. if (!s)
  18748. s = endpoints + strlen(endpoints);
  18749. }
  18750. SOAP_FREE(soap, endpoint);
  18751. } else {
  18752. soap_try_connect_command(soap, http_command, endpoints, action);
  18753. }
  18754. }
  18755. return soap->error;
  18756. }
  18757. /******************************************************************************/
  18758. static int
  18759. soap_try_connect_command(struct soap *soap, int http_command, const char *endpoint, const char *action) {
  18760. char host[sizeof(soap->host)];
  18761. int port;
  18762. ULONG64 count;
  18763. soap->error = SOAP_OK;
  18764. soap_memcpy(host, sizeof(host), soap->host, sizeof(soap->host)); /* save previous host name: if != then reconnect */
  18765. port = soap->port; /* save previous port to compare */
  18766. soap->status = http_command;
  18767. soap_set_endpoint(soap, endpoint);
  18768. soap->action = soap_strdup(soap, action);
  18769. #ifndef WITH_LEANER
  18770. if (soap->fconnect) {
  18771. soap->error = soap->fconnect(soap, endpoint, soap->host, soap->port);
  18772. if (soap->error)
  18773. return soap->error;
  18774. } else
  18775. #endif
  18776. if (soap->fopen && *soap->host) {
  18777. if (!soap->keep_alive || !soap_valid_socket(soap->socket) || strcmp(soap->host, host) || soap->port != port ||
  18778. !soap->fpoll || soap->fpoll(soap)) {
  18779. soap->error = SOAP_OK;
  18780. #ifndef WITH_LEAN
  18781. if (!strncmp(endpoint, "soap.udp:", 9) || !strncmp(endpoint, "udp:", 4)) {
  18782. soap->omode |= SOAP_IO_UDP;
  18783. } else
  18784. #endif
  18785. {
  18786. soap->keep_alive = 0; /* to force close */
  18787. soap->omode &= ~SOAP_IO_UDP; /* to force close */
  18788. }
  18789. soap_closesock(soap);
  18790. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Connect/reconnect to '%s' host='%s' path='%s' port=%d\n",
  18791. endpoint ? endpoint : "(null)", soap->host, soap->path, soap->port));
  18792. if (!soap->keep_alive || !soap_valid_socket(soap->socket)) {
  18793. soap->socket = soap->fopen(soap, endpoint, soap->host, soap->port);
  18794. if (!soap_valid_socket(soap->socket) || soap->error) {
  18795. if (soap->error)
  18796. return soap->error;
  18797. return soap->error = SOAP_TCP_ERROR;
  18798. }
  18799. soap->keep_alive = -((soap->omode & SOAP_IO_KEEPALIVE) != 0);
  18800. }
  18801. }
  18802. }
  18803. #ifdef WITH_NTLM
  18804. if (soap_ntlm_handshake(soap, SOAP_GET, endpoint, soap->host, soap->port))
  18805. return soap->error;
  18806. #endif
  18807. count = soap_count_attachments(soap);
  18808. if (soap_init_send(soap))
  18809. return soap->error;
  18810. #ifndef WITH_NOHTTP
  18811. if (http_command == SOAP_GET || http_command == SOAP_DEL || http_command == SOAP_HEAD ||
  18812. http_command == SOAP_OPTIONS) {
  18813. soap->mode &= ~SOAP_IO;
  18814. soap->mode |= SOAP_IO_BUFFER;
  18815. }
  18816. if ((soap->mode & SOAP_IO) != SOAP_IO_STORE && !(soap->mode & SOAP_ENC_PLAIN) && endpoint) {
  18817. soap_mode k = soap->mode;
  18818. soap->mode &= ~(SOAP_IO | SOAP_ENC_ZLIB);
  18819. if ((k & SOAP_IO) != SOAP_IO_FLUSH)
  18820. soap->mode |= SOAP_IO_BUFFER;
  18821. soap->error = soap->fpost(soap, endpoint, soap->host, soap->port, soap->path, action, count);
  18822. if (soap->error)
  18823. return soap->error;
  18824. if ((k & SOAP_IO) == SOAP_IO_CHUNK) {
  18825. if (soap_flush(soap))
  18826. return soap->error;
  18827. }
  18828. soap->mode = k;
  18829. }
  18830. if (http_command == SOAP_GET || http_command == SOAP_DEL || http_command == SOAP_HEAD ||
  18831. http_command == SOAP_OPTIONS)
  18832. return soap_end_send_flush(soap);
  18833. #endif
  18834. #ifndef WITH_LEANER
  18835. if (soap_begin_attachments(soap))
  18836. return soap->error;
  18837. #endif
  18838. return SOAP_OK;
  18839. }
  18840. /******************************************************************************/
  18841. #ifdef WITH_NTLM
  18842. static int
  18843. soap_ntlm_handshake(struct soap *soap, int command, const char *endpoint, const char *host, int port)
  18844. {
  18845. /* requires libntlm from http://www.nongnu.org/libntlm/ */
  18846. const char *userid = (soap->proxy_userid ? soap->proxy_userid : soap->userid);
  18847. const char *passwd = (soap->proxy_passwd ? soap->proxy_passwd : soap->passwd);
  18848. struct SOAP_ENV__Header *oldheader;
  18849. if (soap->ntlm_challenge && userid && passwd && soap->authrealm)
  18850. {
  18851. tSmbNtlmAuthRequest req;
  18852. tSmbNtlmAuthResponse res;
  18853. tSmbNtlmAuthChallenge ch;
  18854. int k = soap->keep_alive;
  18855. ULONG64 l = soap->length;
  18856. ULONG64 c = soap->count;
  18857. soap_mode m = soap->mode, o = soap->omode;
  18858. int s = soap->status;
  18859. char *a = soap->action;
  18860. short v = soap->version;
  18861. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "NTLM '%s'\n", soap->ntlm_challenge));
  18862. if (!*soap->ntlm_challenge)
  18863. {
  18864. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "NTLM S->C Type 1: received NTLM authentication challenge from server\n"));
  18865. /* S -> C 401 Unauthorized
  18866. WWW-Authenticate: NTLM
  18867. */
  18868. buildSmbNtlmAuthRequest(&req, userid, soap->authrealm);
  18869. soap->ntlm_challenge = soap_s2base64(soap, (unsigned char*)(void*)&req, NULL, SmbLength(&req));
  18870. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "NTLM C->S Type 2: sending NTLM authorization to server\nAuthorization: NTLM %s\n", soap->ntlm_challenge));
  18871. /* C -> S GET ...
  18872. Authorization: NTLM TlRMTVNTUAABAAAAA7IAAAoACgApAAAACQAJACAAAABMSUdIVENJVFlVUlNBLU1JTk9S
  18873. */
  18874. soap->omode = SOAP_IO_BUFFER;
  18875. if (soap_init_send(soap))
  18876. return soap->error;
  18877. if (!soap->keep_alive)
  18878. soap->keep_alive = -1; /* client keep alive */
  18879. soap->status = command;
  18880. if (soap->fpost(soap, endpoint, host, port, soap->path, soap->action, 0)
  18881. || soap_end_send_flush(soap))
  18882. return soap->error;
  18883. soap->mode = m;
  18884. soap->keep_alive = k;
  18885. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "NTLM S->C Type 2: waiting on server NTLM response\n"));
  18886. oldheader = soap->header;
  18887. if (soap_begin_recv(soap))
  18888. if (soap->error == SOAP_EOF)
  18889. return soap->error;
  18890. (void)soap_end_recv(soap);
  18891. soap->header = oldheader;
  18892. soap->length = l;
  18893. if (soap->status != 401 && soap->status != 407)
  18894. return soap->error = SOAP_NTLM_ERROR;
  18895. soap->error = SOAP_OK;
  18896. }
  18897. /* S -> C 401 Unauthorized
  18898. WWW-Authenticate: NTLM TlRMTVNTUAACAAAAAAAAACgAAAABggAAU3J2Tm9uY2UAAAAAAAAAAA==
  18899. */
  18900. soap_base642s(soap, soap->ntlm_challenge, (char*)&ch, sizeof(tSmbNtlmAuthChallenge), NULL);
  18901. buildSmbNtlmAuthResponse(&ch, &res, userid, passwd);
  18902. soap->ntlm_challenge = soap_s2base64(soap, (unsigned char*)(void*)&res, NULL, SmbLength(&res));
  18903. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "NTLM C->S Type 3: sending NTLM authorization to server\nAuthorization: NTLM %s\n", soap->ntlm_challenge));
  18904. /* C -> S GET ...
  18905. Authorization: NTLM TlRMTVNTUAADAAAAGAAYAHIAAAAYABgAigAAABQAFABAAAAADAAMAFQAAAASABIAYAAAAAAAAACiAAAAAYIAAFUAUgBTAEEALQBNAEkATgBPAFIAWgBhAHAAaABvAGQATABJAEcASABUAEMASQBUAFkArYfKbe/jRoW5xDxHeoxC1gBmfWiS5+iX4OAN4xBKG/IFPwfH3agtPEia6YnhsADT
  18906. */
  18907. soap->userid = NULL;
  18908. soap->passwd = NULL;
  18909. soap->proxy_userid = NULL;
  18910. soap->proxy_passwd = NULL;
  18911. soap->keep_alive = k;
  18912. soap->length = l;
  18913. soap->count = c;
  18914. soap->mode = m;
  18915. soap->omode = o;
  18916. soap->status = s;
  18917. soap->action = a;
  18918. soap->version = v;
  18919. }
  18920. return SOAP_OK;
  18921. }
  18922. #endif
  18923. /******************************************************************************/
  18924. SOAP_FMAC1
  18925. char *
  18926. SOAP_FMAC2
  18927. soap_s2base64(struct soap *soap, const unsigned char *s, char *t, int n) {
  18928. int i;
  18929. unsigned long m;
  18930. char *p;
  18931. if (!t)
  18932. t = (char *) soap_malloc(soap, (n + 2) / 3 * 4 + 1);
  18933. if (!t)
  18934. return NULL;
  18935. p = t;
  18936. t[0] = '\0';
  18937. if (!s)
  18938. return p;
  18939. for (; n > 2; n -= 3, s += 3) {
  18940. m = s[0];
  18941. m = (m << 8) | s[1];
  18942. m = (m << 8) | s[2];
  18943. for (i = 4; i > 0; m >>= 6)
  18944. t[--i] = soap_base64o[m & 0x3F];
  18945. t += 4;
  18946. }
  18947. t[0] = '\0';
  18948. if (n > 0) /* 0 < n <= 2 implies that t[0..4] is allocated (base64 scaling formula) */
  18949. {
  18950. m = 0;
  18951. for (i = 0; i < n; i++)
  18952. m = (m << 8) | *s++;
  18953. for (; i < 3; i++)
  18954. m <<= 8;
  18955. for (i = 4; i > 0; m >>= 6)
  18956. t[--i] = soap_base64o[m & 0x3F];
  18957. for (i = 3; i > n; i--)
  18958. t[i] = '=';
  18959. t[4] = '\0';
  18960. }
  18961. return p;
  18962. }
  18963. /******************************************************************************/
  18964. SOAP_FMAC1
  18965. const char *
  18966. SOAP_FMAC2
  18967. soap_base642s(struct soap *soap, const char *s, char *t, size_t l, int *n) {
  18968. size_t i, j;
  18969. soap_wchar c;
  18970. unsigned long m;
  18971. const char *p;
  18972. if (!s || !*s) {
  18973. if (n)
  18974. *n = 0;
  18975. if (soap->error)
  18976. return NULL;
  18977. return SOAP_NON_NULL;
  18978. }
  18979. if (!t) {
  18980. l = (strlen(s) + 3) / 4 * 3 + 1; /* space for raw binary and \0 */
  18981. t = (char *) soap_malloc(soap, l);
  18982. }
  18983. if (!t)
  18984. return NULL;
  18985. p = t;
  18986. if (n)
  18987. *n = 0;
  18988. for (i = 0;; i += 3, l -= 3) {
  18989. m = 0;
  18990. j = 0;
  18991. while (j < 4) {
  18992. c = *s++;
  18993. if (c == '=' || !c) {
  18994. if (l >= j - 1) {
  18995. switch (j) {
  18996. case 2:
  18997. *t++ = (char) ((m >> 4) & 0xFF);
  18998. i++;
  18999. l--;
  19000. break;
  19001. case 3:
  19002. *t++ = (char) ((m >> 10) & 0xFF);
  19003. *t++ = (char) ((m >> 2) & 0xFF);
  19004. i += 2;
  19005. l -= 2;
  19006. }
  19007. }
  19008. if (n)
  19009. *n = (int) i;
  19010. if (l)
  19011. *t = '\0';
  19012. return p;
  19013. }
  19014. c -= '+';
  19015. if (c >= 0 && c <= 79) {
  19016. int b = soap_base64i[c];
  19017. if (b >= 64) {
  19018. soap->error = SOAP_TYPE;
  19019. return NULL;
  19020. }
  19021. m = (m << 6) + b;
  19022. j++;
  19023. } else if (!soap_coblank(c + '+')) {
  19024. soap->error = SOAP_TYPE;
  19025. return NULL;
  19026. }
  19027. }
  19028. if (l < 3) {
  19029. if (n)
  19030. *n = (int) i;
  19031. if (l)
  19032. *t = '\0';
  19033. return p;
  19034. }
  19035. *t++ = (char) ((m >> 16) & 0xFF);
  19036. *t++ = (char) ((m >> 8) & 0xFF);
  19037. *t++ = (char) (m & 0xFF);
  19038. }
  19039. }
  19040. /******************************************************************************/
  19041. SOAP_FMAC1
  19042. char *
  19043. SOAP_FMAC2
  19044. soap_s2hex(struct soap *soap, const unsigned char *s, char *t, int n) {
  19045. char *p;
  19046. if (!t)
  19047. t = (char *) soap_malloc(soap, 2 * n + 1);
  19048. if (!t)
  19049. return NULL;
  19050. p = t;
  19051. t[0] = '\0';
  19052. if (s) {
  19053. for (; n > 0; n--) {
  19054. int m = *s++;
  19055. *t++ = (char) ((m >> 4) + (m > 159 ? 'a' - 10 : '0'));
  19056. m &= 0x0F;
  19057. *t++ = (char) (m + (m > 9 ? 'a' - 10 : '0'));
  19058. }
  19059. }
  19060. *t++ = '\0';
  19061. return p;
  19062. }
  19063. /******************************************************************************/
  19064. SOAP_FMAC1
  19065. const char *
  19066. SOAP_FMAC2
  19067. soap_hex2s(struct soap *soap, const char *s, char *t, size_t l, int *n) {
  19068. const char *p;
  19069. if (!s || !*s) {
  19070. if (n)
  19071. *n = 0;
  19072. if (soap->error)
  19073. return NULL;
  19074. return SOAP_NON_NULL;
  19075. }
  19076. if (!t) {
  19077. l = strlen(s) / 2 + 1; /* make sure enough space for \0 */
  19078. t = (char *) soap_malloc(soap, l);
  19079. }
  19080. if (!t)
  19081. return NULL;
  19082. p = t;
  19083. while (l) {
  19084. int d1, d2;
  19085. d1 = *s++;
  19086. if (!d1)
  19087. break;
  19088. d2 = *s++;
  19089. if (!d2)
  19090. break;
  19091. *t++ = (char) (((d1 >= 'A' ? (d1 & 0x7) + 9 : d1 - '0') << 4) + (d2 >= 'A' ? (d2 & 0x7) + 9 : d2 - '0'));
  19092. l--;
  19093. }
  19094. if (n)
  19095. *n = (int) (t - p);
  19096. if (l)
  19097. *t = '\0';
  19098. return p;
  19099. }
  19100. /******************************************************************************/
  19101. #ifndef WITH_NOHTTP
  19102. SOAP_FMAC1
  19103. const char *
  19104. SOAP_FMAC2
  19105. soap_http_content_type(struct soap *soap, int status) {
  19106. if (soap->status != SOAP_GET && soap->status != SOAP_DEL && soap->status != SOAP_CONNECT) {
  19107. const char *s = "text/xml; charset=utf-8";
  19108. #ifndef WITH_LEANER
  19109. const char *r = NULL;
  19110. size_t n;
  19111. #endif
  19112. if (((status >= SOAP_FILE && status < SOAP_FILE + 600) || soap->status == SOAP_PUT ||
  19113. soap->status == SOAP_POST_FILE || soap->status == SOAP_PATCH) && soap->http_content &&
  19114. *soap->http_content && !strchr(soap->http_content, 10) && !strchr(soap->http_content, 13))
  19115. s = soap->http_content;
  19116. else if (status == SOAP_HTML)
  19117. s = "text/html; charset=utf-8";
  19118. else if (soap->version == 2)
  19119. s = "application/soap+xml; charset=utf-8";
  19120. soap->http_content = NULL; /* use http_content once (assign new value before each call) */
  19121. #ifndef WITH_LEANER
  19122. if (soap->mode & (SOAP_ENC_DIME | SOAP_ENC_MTOM)) {
  19123. if ((soap->mode & SOAP_ENC_MTOM)) {
  19124. if (soap->version == 2)
  19125. r = "application/soap+xml";
  19126. else
  19127. r = "text/xml";
  19128. s = "application/xop+xml";
  19129. } else {
  19130. s = "application/dime";
  19131. }
  19132. }
  19133. if ((soap->mode & SOAP_ENC_MIME) && soap->mime.boundary) {
  19134. const char *t;
  19135. size_t l;
  19136. (SOAP_SNPRINTF(soap->tmpbuf, sizeof(soap->tmpbuf), strlen(soap->mime.boundary) + 53),
  19137. "multipart/related; charset=utf-8; boundary=\"%s\"; type=\"", soap->mime.boundary);
  19138. t = strchr(s, ';');
  19139. if (t)
  19140. n = t - s;
  19141. else
  19142. n = strlen(s);
  19143. l = strlen(soap->tmpbuf);
  19144. if (sizeof(soap->tmpbuf) > l + n)
  19145. (void) soap_strncpy(soap->tmpbuf + l, sizeof(soap->tmpbuf) - l, s, n);
  19146. if (soap->mime.start) {
  19147. l = strlen(soap->tmpbuf);
  19148. (SOAP_SNPRINTF(soap->tmpbuf + l, sizeof(soap->tmpbuf) - l, strlen(soap->mime.start) + 10),
  19149. "\"; start=\"%s", soap->mime.start);
  19150. }
  19151. if (r) {
  19152. l = strlen(soap->tmpbuf);
  19153. (SOAP_SNPRINTF(soap->tmpbuf + l, sizeof(soap->tmpbuf) - l, strlen(r) + 15), "\"; start-info=\"%s", r);
  19154. }
  19155. l = strlen(soap->tmpbuf);
  19156. if (sizeof(soap->tmpbuf) > l)
  19157. soap_strcpy(soap->tmpbuf + l, sizeof(soap->tmpbuf) - l, "\"");
  19158. } else {
  19159. soap_strcpy(soap->tmpbuf, sizeof(soap->tmpbuf), s);
  19160. }
  19161. if (status == SOAP_OK && soap->version == 2 && soap->action) {
  19162. size_t l = strlen(soap->tmpbuf);
  19163. n = strlen(soap->action);
  19164. (SOAP_SNPRINTF(soap->tmpbuf + l, sizeof(soap->tmpbuf) - l, n + 11), "; action=\"%s\"", soap->action);
  19165. }
  19166. #else
  19167. soap_strcpy(soap->tmpbuf, sizeof(soap->tmpbuf), s);
  19168. #endif
  19169. return soap->tmpbuf;
  19170. }
  19171. return NULL;
  19172. }
  19173. #endif
  19174. /******************************************************************************/
  19175. #ifndef WITH_NOHTTP
  19176. SOAP_FMAC1
  19177. int
  19178. SOAP_FMAC2
  19179. soap_puthttphdr(struct soap *soap, int status, ULONG64 count) {
  19180. int err = SOAP_OK;
  19181. if (soap_http_content_type(soap, status)) {
  19182. err = soap->fposthdr(soap, "Content-Type", soap->tmpbuf);
  19183. if (err)
  19184. return err;
  19185. #ifdef WITH_ZLIB
  19186. if ((soap->omode & SOAP_ENC_ZLIB))
  19187. {
  19188. #ifdef WITH_GZIP
  19189. err = soap->fposthdr(soap, "Content-Encoding", soap->zlib_out == SOAP_ZLIB_DEFLATE ? "deflate" : "gzip");
  19190. #else
  19191. err = soap->fposthdr(soap, "Content-Encoding", "deflate");
  19192. #endif
  19193. if (err)
  19194. return err;
  19195. }
  19196. #endif
  19197. #ifndef WITH_LEANER
  19198. if ((soap->omode & SOAP_IO) == SOAP_IO_CHUNK) {
  19199. err = soap->fposthdr(soap, "Transfer-Encoding", "chunked");
  19200. } else
  19201. #endif
  19202. {
  19203. (SOAP_SNPRINTF(soap->tmpbuf, sizeof(soap->tmpbuf), 20), SOAP_ULONG_FORMAT, count);
  19204. err = soap->fposthdr(soap, "Content-Length", soap->tmpbuf);
  19205. }
  19206. if (err)
  19207. return err;
  19208. }
  19209. if (soap->http_extra_header) {
  19210. err = soap_send(soap, soap->http_extra_header);
  19211. soap->http_extra_header = NULL; /* use http_extra_header once (assign new value before each call) */
  19212. if (err)
  19213. return err;
  19214. err = soap_send_raw(soap, "\r\n", 2);
  19215. if (err)
  19216. return err;
  19217. }
  19218. if (soap->keep_alive) {
  19219. if (soap->keep_alive > 0 && soap->recv_timeout) {
  19220. int t = soap->recv_timeout;
  19221. if (t < 0)
  19222. t = 1;
  19223. (SOAP_SNPRINTF(soap->tmpbuf, sizeof(soap->tmpbuf), 20), "timeout=%d, max=%d", soap->recv_timeout,
  19224. soap->keep_alive);
  19225. err = soap->fposthdr(soap, "Keep-Alive", soap->tmpbuf);
  19226. if (err)
  19227. return err;
  19228. }
  19229. return soap->fposthdr(soap, "Connection", "keep-alive");
  19230. }
  19231. return soap->fposthdr(soap, "Connection", "close");
  19232. }
  19233. #endif
  19234. /******************************************************************************/
  19235. #ifndef WITH_LEAN
  19236. static const char *
  19237. soap_set_validation_fault(struct soap *soap, const char *s, const char *t) {
  19238. if (!t)
  19239. t = SOAP_STR_EOS;
  19240. if (*soap->tag)
  19241. (SOAP_SNPRINTF(soap->msgbuf, sizeof(soap->msgbuf), strlen(s) + strlen(t) + strlen(soap->tag) + 47),
  19242. "Validation constraint violation: %s%s in element '%s'", s, t, soap->tag);
  19243. else
  19244. (SOAP_SNPRINTF(soap->msgbuf, sizeof(soap->msgbuf), strlen(s) + strlen(t) + 33),
  19245. "Validation constraint violation: %s%s", s, t);
  19246. return soap->msgbuf;
  19247. }
  19248. #endif
  19249. /******************************************************************************/
  19250. SOAP_FMAC1
  19251. void
  19252. SOAP_FMAC2
  19253. soap_set_fault(struct soap *soap) {
  19254. const char **c;
  19255. const char **s;
  19256. if (soap->version == 0)
  19257. soap_version(soap);
  19258. c = soap_faultcode(soap);
  19259. s = soap_faultstring(soap);
  19260. if (soap->fseterror)
  19261. soap->fseterror(soap, c, s);
  19262. if (!*c) {
  19263. if (soap->version == 2)
  19264. *c = "SOAP-ENV:Sender";
  19265. else if (soap->version == 1)
  19266. *c = "SOAP-ENV:Client";
  19267. else
  19268. *c = "";
  19269. }
  19270. if (*s)
  19271. return;
  19272. if (soap->error >= SOAP_POST)
  19273. soap->error = SOAP_HTTP_METHOD;
  19274. switch (soap->error) {
  19275. #ifndef WITH_LEAN
  19276. case SOAP_CLI_FAULT:
  19277. *s = "Client fault";
  19278. break;
  19279. case SOAP_SVR_FAULT:
  19280. *s = "Server fault";
  19281. break;
  19282. case SOAP_TAG_MISMATCH:
  19283. *s = soap_set_validation_fault(soap, "tag name or namespace mismatch", NULL);
  19284. break;
  19285. case SOAP_TYPE:
  19286. if (*soap->type)
  19287. *s = soap_set_validation_fault(soap, "type mismatch ", soap->type);
  19288. else if (*soap->arrayType)
  19289. *s = soap_set_validation_fault(soap, "array type mismatch", NULL);
  19290. else
  19291. *s = soap_set_validation_fault(soap, "invalid value", NULL);
  19292. break;
  19293. case SOAP_SYNTAX_ERROR:
  19294. *s = soap_set_validation_fault(soap, "syntax error", NULL);
  19295. break;
  19296. case SOAP_NO_TAG:
  19297. if (soap->version == 0 && soap->level == 0)
  19298. *s = soap_set_validation_fault(soap, "root element expected", NULL);
  19299. else if (soap->level == 0)
  19300. *s = soap_set_validation_fault(soap, "SOAP message expected", NULL);
  19301. else
  19302. *s = soap_set_validation_fault(soap, "element tag expected", NULL);
  19303. break;
  19304. case SOAP_END_TAG:
  19305. *s = soap_set_validation_fault(soap, "closing tag expected", NULL);
  19306. break;
  19307. case SOAP_MUSTUNDERSTAND:
  19308. *c = "SOAP-ENV:MustUnderstand";
  19309. (SOAP_SNPRINTF(soap->msgbuf, sizeof(soap->msgbuf), strlen(soap->tag) + 65),
  19310. "The data in element '%s' must be understood but cannot be processed, Sassan %s", soap->tag, debug);
  19311. *s = soap->msgbuf;
  19312. break;
  19313. case SOAP_VERSIONMISMATCH:
  19314. *c = "SOAP-ENV:VersionMismatch";
  19315. *s = "Invalid SOAP message or SOAP version mismatch";
  19316. break;
  19317. case SOAP_DATAENCODINGUNKNOWN:
  19318. *c = "SOAP-ENV:DataEncodingUnknown";
  19319. *s = "Unsupported SOAP data encoding";
  19320. break;
  19321. case SOAP_NAMESPACE:
  19322. *s = soap_set_validation_fault(soap, "namespace error", NULL);
  19323. break;
  19324. case SOAP_USER_ERROR:
  19325. *s = "User data access error";
  19326. break;
  19327. case SOAP_NO_METHOD:
  19328. (SOAP_SNPRINTF(soap->msgbuf, sizeof(soap->msgbuf), strlen(soap->tag) + 66),
  19329. "Method '%s' not implemented: method name or namespace not recognized", soap->tag);
  19330. *s = soap->msgbuf;
  19331. break;
  19332. case SOAP_NO_DATA:
  19333. *s = "Data required for operation";
  19334. break;
  19335. case SOAP_GET_METHOD:
  19336. *s = "HTTP GET method not implemented";
  19337. break;
  19338. case SOAP_PUT_METHOD:
  19339. *s = "HTTP PUT method not implemented";
  19340. break;
  19341. case SOAP_PATCH_METHOD:
  19342. *s = "HTTP PATCH method not implemented";
  19343. break;
  19344. case SOAP_DEL_METHOD:
  19345. *s = "HTTP DELETE method not implemented";
  19346. break;
  19347. case SOAP_HTTP_METHOD:
  19348. *s = "HTTP method error";
  19349. break;
  19350. case SOAP_EOM:
  19351. *s = "Out of memory";
  19352. break;
  19353. case SOAP_MOE:
  19354. *s = "Memory overflow or memory corruption error";
  19355. break;
  19356. case SOAP_HDR:
  19357. *s = "Header line too long";
  19358. break;
  19359. case SOAP_IOB:
  19360. *s = "Array index out of bounds";
  19361. break;
  19362. case SOAP_NULL:
  19363. *s = soap_set_validation_fault(soap, "nil not allowed", NULL);
  19364. break;
  19365. case SOAP_DUPLICATE_ID:
  19366. *s = soap_set_validation_fault(soap, "multiple elements (use the SOAP_XML_TREE flag) with duplicate id ",
  19367. soap->id);
  19368. if (soap->version == 2)
  19369. *soap_faultsubcode(soap) = "SOAP-ENC:DuplicateID";
  19370. break;
  19371. case SOAP_MISSING_ID:
  19372. *s = soap_set_validation_fault(soap, "missing id for ref ", soap->id);
  19373. if (soap->version == 2)
  19374. *soap_faultsubcode(soap) = "SOAP-ENC:MissingID";
  19375. break;
  19376. case SOAP_HREF:
  19377. *s = soap_set_validation_fault(soap, "incompatible object type id-ref ", soap->id);
  19378. break;
  19379. case SOAP_FAULT:
  19380. break;
  19381. #ifndef WITH_NOIO
  19382. case SOAP_UDP_ERROR:
  19383. *s = "Message too large for UDP packet";
  19384. break;
  19385. case SOAP_TCP_ERROR:
  19386. *s = tcp_error(soap);
  19387. break;
  19388. #endif
  19389. case SOAP_HTTP_ERROR:
  19390. *s = "An HTTP processing error occurred";
  19391. break;
  19392. case SOAP_NTLM_ERROR:
  19393. *s = "An HTTP NTLM authentication error occurred";
  19394. break;
  19395. case SOAP_SSL_ERROR:
  19396. #ifdef WITH_OPENSSL
  19397. *s = "SSL/TLS error";
  19398. #else
  19399. *s = "OpenSSL not installed: recompile with -DWITH_OPENSSL";
  19400. #endif
  19401. break;
  19402. case SOAP_PLUGIN_ERROR:
  19403. *s = "Plugin registry error";
  19404. break;
  19405. case SOAP_DIME_ERROR:
  19406. *s = "DIME format error or max DIME size exceeds SOAP_MAXDIMESIZE currently set to " SOAP_XSTRINGIFY(
  19407. SOAP_MAXDIMESIZE);
  19408. break;
  19409. case SOAP_DIME_HREF:
  19410. *s = "DIME href to missing attachment";
  19411. break;
  19412. case SOAP_DIME_MISMATCH:
  19413. *s = "DIME version/transmission error";
  19414. break;
  19415. case SOAP_DIME_END:
  19416. *s = "End of DIME error";
  19417. break;
  19418. case SOAP_MIME_ERROR:
  19419. *s = "MIME format error";
  19420. break;
  19421. case SOAP_MIME_HREF:
  19422. *s = "MIME href to missing attachment";
  19423. break;
  19424. case SOAP_MIME_END:
  19425. *s = "End of MIME error";
  19426. break;
  19427. case SOAP_ZLIB_ERROR:
  19428. #ifdef WITH_ZLIB
  19429. (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);
  19430. *s = soap->msgbuf;
  19431. #else
  19432. *s = "Zlib/gzip not installed for (de)compression: recompile with -DWITH_GZIP";
  19433. #endif
  19434. break;
  19435. case SOAP_REQUIRED:
  19436. *s = soap_set_validation_fault(soap, "missing required attribute", NULL);
  19437. break;
  19438. case SOAP_PROHIBITED:
  19439. *s = soap_set_validation_fault(soap, "prohibited attribute present", NULL);
  19440. break;
  19441. case SOAP_LEVEL:
  19442. *s = "Maximum XML nesting depth level exceeded: increase maxlevel";
  19443. break;
  19444. case SOAP_LENGTH:
  19445. *s = soap_set_validation_fault(soap, "value range or content length violation", NULL);
  19446. break;
  19447. case SOAP_OCCURS:
  19448. *s = soap_set_validation_fault(soap, "occurrence constraint violation", NULL);
  19449. break;
  19450. case SOAP_FIXED:
  19451. *s = soap_set_validation_fault(soap, "value does not match the fixed value required", NULL);
  19452. break;
  19453. case SOAP_EMPTY:
  19454. *s = soap_set_validation_fault(soap, "empty value provided where a value is required", NULL);
  19455. break;
  19456. case SOAP_FD_EXCEEDED:
  19457. *s = "Maximum number of open connections was reached: increase FD_SETSIZE or define HAVE_POLL";
  19458. break;
  19459. case SOAP_UTF_ERROR:
  19460. *s = "UTF content encoding error";
  19461. break;
  19462. case SOAP_STOP:
  19463. *s = "Stopped: service request already handled by plugin (informative)";
  19464. break;
  19465. #endif
  19466. case SOAP_EOF:
  19467. #ifndef WITH_NOIO
  19468. *s = soap_strerror(soap); /* *s = soap->msgbuf */
  19469. #ifndef WITH_LEAN
  19470. if (strlen(soap->msgbuf) + 25 < sizeof(soap->msgbuf)) {
  19471. (void) soap_memmove((void *) (soap->msgbuf + 25), sizeof(soap->tmpbuf) - 25,
  19472. (const void *) soap->msgbuf, strlen(soap->msgbuf) + 1);
  19473. if (soap->is)
  19474. #if defined(__cplusplus) && !defined(WITH_COMPAT)
  19475. (void) soap_memcpy((void *) soap->msgbuf, sizeof(soap->msgbuf),
  19476. (const void *) "End or bad std::istream: ", 25);
  19477. #else
  19478. (void)soap_memcpy((void*)soap->msgbuf, sizeof(soap->msgbuf), (const void*)"End at NUL buffer input: ", 25);
  19479. #endif
  19480. else
  19481. (void) soap_memcpy((void *) soap->msgbuf, sizeof(soap->msgbuf),
  19482. (const void *) "End of file or no input: ", 25);
  19483. }
  19484. #endif
  19485. break;
  19486. #else
  19487. *s = "End of file or no input";
  19488. break;
  19489. #endif
  19490. case SOAP_ERR:
  19491. *s = "An unspecified error occurred";
  19492. break;
  19493. default:
  19494. #ifndef WITH_NOHTTP
  19495. #ifndef WITH_LEAN
  19496. if (soap->error >= 200 && soap->error < 600) {
  19497. const char *t = http_error(soap, soap->error);
  19498. (SOAP_SNPRINTF(soap->msgbuf, sizeof(soap->msgbuf), strlen(t) + 54), "Error %d: HTTP %d %s", soap->error,
  19499. soap->error, t);
  19500. *s = soap->msgbuf;
  19501. } else
  19502. #endif
  19503. #endif
  19504. {
  19505. (SOAP_SNPRINTF(soap->msgbuf, sizeof(soap->msgbuf), 26), "Error %d", soap->error);
  19506. *s = soap->msgbuf;
  19507. }
  19508. }
  19509. }
  19510. /******************************************************************************/
  19511. SOAP_FMAC1
  19512. int
  19513. SOAP_FMAC2
  19514. soap_send_fault(struct soap *soap) {
  19515. int status = soap->error;
  19516. if (status == SOAP_OK || status == SOAP_STOP)
  19517. return soap_closesock(soap);
  19518. #ifndef WITH_NOHTTP
  19519. if (status >= 200 && status <= 299)
  19520. return soap_send_empty_response(soap, status);
  19521. #endif
  19522. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Sending back fault struct for error code %d\n", soap->error));
  19523. soap->keep_alive = 0; /* error: close connection later by disabling keep-alive here */
  19524. soap_set_fault(soap);
  19525. if (soap->error < 200 && soap->error != SOAP_FAULT)
  19526. soap->header = NULL;
  19527. if (status != SOAP_EOF || (!soap->recv_timeout && !soap->send_timeout)) {
  19528. int r = 1;
  19529. #ifndef WITH_NOIO
  19530. if (soap->fpoll && soap->fpoll(soap)) {
  19531. r = 0;
  19532. }
  19533. #ifndef WITH_LEAN
  19534. else if (soap_valid_socket(soap->socket)) {
  19535. r = tcp_select(soap, soap->socket, SOAP_TCP_SELECT_RCV | SOAP_TCP_SELECT_SND, 0);
  19536. if (r > 0) {
  19537. int t;
  19538. if (!(r & SOAP_TCP_SELECT_SND)
  19539. || ((r & SOAP_TCP_SELECT_RCV)
  19540. && recv(soap->socket, (char *) &t, 1, MSG_PEEK) < 0))
  19541. r = 0;
  19542. }
  19543. }
  19544. #endif
  19545. #endif
  19546. if (r > 0) {
  19547. soap->error = SOAP_OK;
  19548. if (soap->version > 0) {
  19549. soap->encodingStyle = NULL; /* no encodingStyle in Faults */
  19550. soap_serializeheader(soap);
  19551. soap_serializefault(soap);
  19552. (void) soap_begin_count(soap);
  19553. if ((soap->mode & SOAP_IO_LENGTH)) {
  19554. if (soap_envelope_begin_out(soap)
  19555. || soap_putheader(soap)
  19556. || soap_body_begin_out(soap)
  19557. || soap_putfault(soap)
  19558. || soap_body_end_out(soap)
  19559. || soap_envelope_end_out(soap))
  19560. return soap_closesock(soap);
  19561. }
  19562. (void) soap_end_count(soap);
  19563. if (soap_response(soap, status)
  19564. || soap_envelope_begin_out(soap)
  19565. || soap_putheader(soap)
  19566. || soap_body_begin_out(soap)
  19567. || soap_putfault(soap)
  19568. || soap_body_end_out(soap)
  19569. || soap_envelope_end_out(soap)
  19570. || soap_end_send(soap))
  19571. return soap_closesock(soap);
  19572. } else {
  19573. const char *s = *soap_faultstring(soap);
  19574. const char **d = soap_faultdetail(soap);
  19575. (void) soap_begin_count(soap);
  19576. if ((soap->mode & SOAP_IO_LENGTH))
  19577. if (soap_element_begin_out(soap, "fault", 0, NULL)
  19578. || soap_outstring(soap, "reason", 0, (char *const *) &s, NULL, 0)
  19579. || soap_outliteral(soap, "detail", (char *const *) d, NULL)
  19580. || soap_element_end_out(soap, "fault"))
  19581. return soap_closesock(soap);
  19582. (void) soap_end_count(soap);
  19583. if (soap_response(soap, status)
  19584. || soap_element_begin_out(soap, "fault", 0, NULL)
  19585. || soap_outstring(soap, "reason", 0, (char *const *) &s, NULL, 0)
  19586. || soap_outliteral(soap, "detail", (char *const *) d, NULL)
  19587. || soap_element_end_out(soap, "fault")
  19588. || soap_end_send(soap))
  19589. return soap_closesock(soap);
  19590. }
  19591. }
  19592. }
  19593. soap->error = status;
  19594. return soap_closesock(soap);
  19595. }
  19596. /******************************************************************************/
  19597. SOAP_FMAC1
  19598. int
  19599. SOAP_FMAC2
  19600. soap_recv_fault(struct soap *soap, int check) {
  19601. int status = soap->status;
  19602. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Check (%d) if receiving SOAP Fault (status = %d)\n", check, status));
  19603. if (!check) {
  19604. /* try getfault when no tag or tag mismatched at level 2, otherwise close and return SOAP_TAG_MISMATCH or HTTP error code */
  19605. if (soap->error != SOAP_NO_TAG && (soap->error != SOAP_TAG_MISMATCH || soap->level != 2)) {
  19606. if (soap->error == SOAP_TAG_MISMATCH && soap->level == 0) {
  19607. soap->error = SOAP_OK;
  19608. if (!soap_element_begin_in(soap, "fault", 0, NULL)) {
  19609. char *s = NULL, *d = NULL;
  19610. (void) soap_instring(soap, "reason", &s, NULL, 0, 1, 0, -1, NULL);
  19611. (void) soap_inliteral(soap, "detail", &d);
  19612. if (!soap_element_end_in(soap, "fault") && !soap_end_recv(soap)) {
  19613. *soap_faultstring(soap) = s;
  19614. *soap_faultdetail(soap) = d;
  19615. if (status)
  19616. soap->error = status;
  19617. else
  19618. soap->error = SOAP_FAULT;
  19619. soap_set_fault(soap);
  19620. return soap_closesock(soap);
  19621. }
  19622. }
  19623. soap->error = SOAP_TAG_MISMATCH;
  19624. }
  19625. if (status && (status < 200 || status > 299))
  19626. soap->error = status;
  19627. return soap_closesock(soap);
  19628. }
  19629. }
  19630. soap->error = SOAP_OK;
  19631. if (soap_getfault(soap)) {
  19632. /* if check>0 and no SOAP Fault is present and no HTTP error then just return to parse request */
  19633. if (check
  19634. && (status == 0 || (status >= 200 && status <= 299))
  19635. && ((soap->error == SOAP_TAG_MISMATCH && soap->level == 2) || soap->error == SOAP_NO_TAG))
  19636. return soap->error = SOAP_OK;
  19637. /* if check=0 and empty SOAP Body and encodingStyle is NULL and no HTTP error then just return */
  19638. if (!check
  19639. && (status == 0 || (status >= 200 && status <= 299))
  19640. && !soap->encodingStyle
  19641. && (soap->error == SOAP_NO_TAG && soap->level <= 2))
  19642. return soap->error = SOAP_OK;
  19643. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Error: soap_get_soapfault() failed with error %d at level %u tag '%s'\n",
  19644. soap->error, soap->level, soap->tag));
  19645. *soap_faultcode(soap) = (soap->version == 2 ? "SOAP-ENV:Sender" : "SOAP-ENV:Client");
  19646. if (status)
  19647. soap->error = status;
  19648. else
  19649. soap->error = status = SOAP_NO_DATA;
  19650. soap_set_fault(soap);
  19651. } else {
  19652. const char *s = *soap_faultcode(soap);
  19653. if (!soap_match_tag(soap, s, "SOAP-ENV:Server")
  19654. || !soap_match_tag(soap, s, "SOAP-ENV:Receiver")) {
  19655. status = SOAP_SVR_FAULT;
  19656. } else if (!soap_match_tag(soap, s, "SOAP-ENV:Client")
  19657. || !soap_match_tag(soap, s, "SOAP-ENV:Sender")) {
  19658. status = SOAP_CLI_FAULT;
  19659. } else if (!soap_match_tag(soap, s, "SOAP-ENV:MustUnderstand")) {
  19660. status = SOAP_MUSTUNDERSTAND;
  19661. } else if (!soap_match_tag(soap, s, "SOAP-ENV:VersionMismatch")) {
  19662. status = SOAP_VERSIONMISMATCH;
  19663. } else {
  19664. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Received SOAP Fault code %s\n", s));
  19665. status = SOAP_FAULT;
  19666. }
  19667. if (!soap_body_end_in(soap))
  19668. soap_envelope_end_in(soap);
  19669. }
  19670. (void) soap_end_recv(soap);
  19671. soap->error = status;
  19672. return soap_closesock(soap);
  19673. }
  19674. /******************************************************************************/
  19675. #ifndef WITH_NOHTTP
  19676. SOAP_FMAC1
  19677. int
  19678. SOAP_FMAC2
  19679. soap_send_empty_response(struct soap *soap, int httpstatuscode) {
  19680. soap_mode m = soap->omode;
  19681. if (!(m & SOAP_IO_UDP)) {
  19682. soap->count = 0;
  19683. if ((m & SOAP_IO) == SOAP_IO_CHUNK)
  19684. soap->omode = (m & ~SOAP_IO) | SOAP_IO_BUFFER;
  19685. (void) soap_response(soap, httpstatuscode);
  19686. (void) soap_end_send(soap); /* force end of sends */
  19687. soap->error = SOAP_STOP; /* stops the server from returning another response */
  19688. soap->omode = m;
  19689. }
  19690. return soap_closesock(soap);
  19691. }
  19692. #endif
  19693. /******************************************************************************/
  19694. #ifndef WITH_NOHTTP
  19695. SOAP_FMAC1
  19696. int
  19697. SOAP_FMAC2
  19698. soap_recv_empty_response(struct soap *soap) {
  19699. soap->error = SOAP_OK;
  19700. if (!(soap->omode & SOAP_IO_UDP) && !(soap->omode & SOAP_ENC_PLAIN)) {
  19701. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Receiving empty response\n"));
  19702. if (soap_begin_recv(soap) == SOAP_OK) {
  19703. if (soap_http_skip_body(soap) || soap_end_recv(soap))
  19704. return soap_closesock(soap);
  19705. } else if (soap->error == 200 || soap->error == 201 || soap->error == 202) {
  19706. soap->error = SOAP_OK;
  19707. }
  19708. }
  19709. #ifndef WITH_LEANER
  19710. else if ((soap->fprepareinitrecv && (soap->error = soap->fprepareinitrecv(soap)))
  19711. || (soap->fpreparefinalrecv && (soap->error = soap->fpreparefinalrecv(soap)))) {
  19712. return soap->error;
  19713. }
  19714. #endif
  19715. return soap_closesock(soap);
  19716. }
  19717. #endif
  19718. /******************************************************************************/
  19719. #ifndef WITH_NOIO
  19720. static const char *
  19721. soap_strerror(struct soap *soap) {
  19722. int err = soap->errnum;
  19723. *soap->msgbuf = '\0';
  19724. if (err) {
  19725. #ifndef WIN32
  19726. # ifdef HAVE_STRERROR_R
  19727. # if (!_GNU_SOURCE && !defined(_POSIX_C_SOURCE) && !defined(_XOPEN_SOURCE)) || _POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600 || defined(__ANDROID__) || !defined(__GLIBC__)
  19728. strerror_r(err, soap->msgbuf, sizeof(soap->msgbuf)); /* XSI-compliant */
  19729. # else
  19730. return strerror_r(err, soap->msgbuf, sizeof(soap->msgbuf)); /* GNU-specific */
  19731. # endif
  19732. # else
  19733. return strerror(err);
  19734. # endif
  19735. #else
  19736. #ifndef UNDER_CE
  19737. DWORD len;
  19738. *soap->msgbuf = '\0';
  19739. 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);
  19740. #else
  19741. DWORD i, len;
  19742. *soap->msgbuf = '\0';
  19743. len = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, err, 0, (LPTSTR)soap->msgbuf, (DWORD)(sizeof(soap->msgbuf)/sizeof(TCHAR)), NULL);
  19744. for (i = 0; i <= len; i++)
  19745. {
  19746. if (((TCHAR*)soap->msgbuf)[i] < 0x80)
  19747. soap->msgbuf[i] = (char)((TCHAR*)soap->msgbuf)[i];
  19748. else
  19749. soap->msgbuf[i] = '?';
  19750. }
  19751. #endif
  19752. #endif
  19753. } else {
  19754. if (soap->recv_maxlength && soap->count > soap->recv_maxlength) {
  19755. soap_strcpy(soap->msgbuf, sizeof(soap->msgbuf), "max message length exceeded");
  19756. } else {
  19757. int tt = soap->transfer_timeout, rt = soap->recv_timeout, st = soap->send_timeout;
  19758. #ifndef WITH_LEAN
  19759. int tu = ' ', ru = ' ', su = ' ';
  19760. #endif
  19761. soap_strcpy(soap->msgbuf, sizeof(soap->msgbuf), "message transfer interrupted");
  19762. if (tt | rt || st)
  19763. soap_strcpy(soap->msgbuf + 28, sizeof(soap->msgbuf) - 28, " or timed out");
  19764. #ifndef WITH_LEAN
  19765. if (tt < 0) {
  19766. tt = -tt;
  19767. tu = 'u';
  19768. }
  19769. if (rt < 0) {
  19770. rt = -rt;
  19771. ru = 'u';
  19772. }
  19773. if (st < 0) {
  19774. st = -st;
  19775. su = 'u';
  19776. }
  19777. if (tt) {
  19778. size_t l = strlen(soap->msgbuf);
  19779. (SOAP_SNPRINTF(soap->msgbuf + l, sizeof(soap->msgbuf) - l, 43), " (%d%csec max transfer time)", tt, tu);
  19780. }
  19781. if (rt) {
  19782. size_t l = strlen(soap->msgbuf);
  19783. (SOAP_SNPRINTF(soap->msgbuf + l, sizeof(soap->msgbuf) - l, 40), " (%d%csec max recv delay)", rt, ru);
  19784. }
  19785. if (st) {
  19786. size_t l = strlen(soap->msgbuf);
  19787. (SOAP_SNPRINTF(soap->msgbuf + l, sizeof(soap->msgbuf) - l, 40), " (%d%csec max send delay)", st, su);
  19788. }
  19789. #endif
  19790. }
  19791. }
  19792. return soap->msgbuf;
  19793. }
  19794. #endif
  19795. /******************************************************************************/
  19796. static int
  19797. soap_set_error(struct soap *soap, const char *faultcode, const char *faultsubcodeQName, const char *faultstring,
  19798. const char *faultdetailXML, int soaperror) {
  19799. *soap_faultcode(soap) = faultcode;
  19800. if (faultsubcodeQName)
  19801. *soap_faultsubcode(soap) = faultsubcodeQName;
  19802. *soap_faultstring(soap) = faultstring;
  19803. if (faultdetailXML && *faultdetailXML) {
  19804. const char **s = soap_faultdetail(soap);
  19805. if (s)
  19806. *s = faultdetailXML;
  19807. }
  19808. return soap->error = soaperror;
  19809. }
  19810. /******************************************************************************/
  19811. SOAP_FMAC1
  19812. int
  19813. SOAP_FMAC2
  19814. soap_set_sender_error(struct soap *soap, const char *faultstring, const char *faultdetailXML, int soaperror) {
  19815. return soap_set_error(soap,
  19816. soap->version == 2 ? "SOAP-ENV:Sender" : soap->version == 1 ? "SOAP-ENV:Client" : "at sender",
  19817. NULL, faultstring, faultdetailXML, soaperror);
  19818. }
  19819. /******************************************************************************/
  19820. SOAP_FMAC1
  19821. int
  19822. SOAP_FMAC2
  19823. soap_set_receiver_error(struct soap *soap, const char *faultstring, const char *faultdetailXML, int soaperror) {
  19824. return soap_set_error(soap, soap->version == 2 ? "SOAP-ENV:Receiver" : soap->version == 1 ? "SOAP-ENV:Server"
  19825. : "detected", NULL,
  19826. faultstring, faultdetailXML, soaperror);
  19827. }
  19828. /******************************************************************************/
  19829. static int
  19830. soap_copy_fault(struct soap *soap, const char *faultcode, const char *faultsubcodeQName, const char *faultstring,
  19831. const char *faultdetailXML) {
  19832. char *r = NULL, *s = NULL, *t = NULL;
  19833. DBGFUN2("soap_copy_fault", "code=%s", faultcode ? faultcode : "(null)", "string=%s",
  19834. faultstring ? faultstring : "(null)")
  19835. if (faultsubcodeQName)
  19836. r = soap_strdup(soap, faultsubcodeQName);
  19837. if (faultstring)
  19838. s = soap_strdup(soap, faultstring);
  19839. if (faultdetailXML)
  19840. t = soap_strdup(soap, faultdetailXML);
  19841. return soap_set_error(soap, faultcode, r, s, t, SOAP_FAULT);
  19842. }
  19843. /******************************************************************************/
  19844. SOAP_FMAC1
  19845. int
  19846. SOAP_FMAC2
  19847. soap_sender_fault(struct soap *soap, const char *faultstring, const char *faultdetailXML) {
  19848. return soap_sender_fault_subcode(soap, NULL, faultstring, faultdetailXML);
  19849. }
  19850. /******************************************************************************/
  19851. SOAP_FMAC1
  19852. int
  19853. SOAP_FMAC2
  19854. soap_sender_fault_subcode(struct soap *soap, const char *faultsubcodeQName, const char *faultstring,
  19855. const char *faultdetailXML) {
  19856. return soap_copy_fault(soap, soap->version == 2 ? "SOAP-ENV:Sender" : soap->version == 1 ? "SOAP-ENV:Client"
  19857. : "at source",
  19858. faultsubcodeQName, faultstring, faultdetailXML);
  19859. }
  19860. /******************************************************************************/
  19861. SOAP_FMAC1
  19862. int
  19863. SOAP_FMAC2
  19864. soap_receiver_fault(struct soap *soap, const char *faultstring, const char *faultdetailXML) {
  19865. return soap_receiver_fault_subcode(soap, NULL, faultstring, faultdetailXML);
  19866. }
  19867. /******************************************************************************/
  19868. SOAP_FMAC1
  19869. int
  19870. SOAP_FMAC2
  19871. soap_receiver_fault_subcode(struct soap *soap, const char *faultsubcodeQName, const char *faultstring,
  19872. const char *faultdetailXML) {
  19873. return soap_copy_fault(soap, soap->version == 2 ? "SOAP-ENV:Receiver" : soap->version == 1 ? "SOAP-ENV:Server"
  19874. : "is internal",
  19875. faultsubcodeQName, faultstring, faultdetailXML);
  19876. }
  19877. /******************************************************************************/
  19878. #ifndef WITH_NOSTDLIB
  19879. SOAP_FMAC1
  19880. void
  19881. SOAP_FMAC2
  19882. soap_print_fault(struct soap *soap, FILE *fd) {
  19883. if (soap_check_state(soap)) {
  19884. fprintf(fd, "Error: soap struct state not initialized\n");
  19885. } else if (soap->error) {
  19886. const char **c, *v = NULL, *s, *d;
  19887. c = soap_faultcode(soap);
  19888. if (!*c) {
  19889. soap_set_fault(soap);
  19890. c = soap_faultcode(soap);
  19891. }
  19892. if (soap->version == 2)
  19893. v = soap_fault_subcode(soap);
  19894. s = soap_fault_string(soap);
  19895. d = soap_fault_detail(soap);
  19896. fprintf(fd, "%s%d fault %s [%s]\n\"%s\"\nDetail: %s\n", soap->version ? "SOAP 1." : "Error ",
  19897. soap->version ? (int) soap->version : soap->error, *c, v ? v : "no subcode", s ? s : "[no reason]",
  19898. d ? d : "[no detail]");
  19899. }
  19900. }
  19901. #endif
  19902. /******************************************************************************/
  19903. #ifdef __cplusplus
  19904. #ifndef WITH_LEAN
  19905. #ifndef WITH_NOSTDLIB
  19906. #ifndef WITH_COMPAT
  19907. SOAP_FMAC1
  19908. void
  19909. SOAP_FMAC2
  19910. soap_stream_fault(struct soap *soap, std::ostream &os) {
  19911. if (soap_check_state(soap)) {
  19912. os << "Error: soap struct state not initialized\n";
  19913. } else if (soap->error) {
  19914. const char **c, *v = NULL, *s, *d;
  19915. c = soap_faultcode(soap);
  19916. if (!*c) {
  19917. soap_set_fault(soap);
  19918. c = soap_faultcode(soap);
  19919. }
  19920. if (soap->version == 2)
  19921. v = soap_fault_subcode(soap);
  19922. s = soap_fault_string(soap);
  19923. d = soap_fault_detail(soap);
  19924. os << (soap->version ? "SOAP 1." : "Error ")
  19925. << (soap->version ? (int) soap->version : soap->error)
  19926. << " fault " << *c
  19927. << "[" << (v ? v : "no subcode") << "]"
  19928. << std::endl
  19929. << "\"" << (s ? s : "[no reason]") << "\""
  19930. << std::endl
  19931. << "Detail: " << (d ? d : "[no detail]")
  19932. << std::endl;
  19933. }
  19934. }
  19935. #endif
  19936. #endif
  19937. #endif
  19938. #endif
  19939. /******************************************************************************/
  19940. #ifndef WITH_LEAN
  19941. #ifndef WITH_NOSTDLIB
  19942. SOAP_FMAC1
  19943. char *
  19944. SOAP_FMAC2
  19945. soap_sprint_fault(struct soap *soap, char *buf, size_t len) {
  19946. if (soap_check_state(soap)) {
  19947. soap_strcpy(buf, len, "Error: soap struct not initialized");
  19948. } else if (soap->error) {
  19949. const char **c, *v = NULL, *s, *d;
  19950. c = soap_faultcode(soap);
  19951. if (!*c) {
  19952. soap_set_fault(soap);
  19953. c = soap_faultcode(soap);
  19954. }
  19955. if (soap->version == 2)
  19956. v = soap_fault_subcode(soap);
  19957. if (!v)
  19958. v = "no subcode";
  19959. s = soap_fault_string(soap);
  19960. if (!s)
  19961. s = "[no reason]";
  19962. d = soap_fault_detail(soap);
  19963. if (!d)
  19964. d = "[no detail]";
  19965. (SOAP_SNPRINTF(buf, len, strlen(*c) + strlen(v) + strlen(s) + strlen(d) + 72),
  19966. "%s%d fault %s [%s]\n\"%s\"\nDetail: %s\n", soap->version ? "SOAP 1." : "Error ",
  19967. soap->version ? (int) soap->version : soap->error, *c, v, s, d);
  19968. } else if (len > 0) {
  19969. *buf = '\0';
  19970. }
  19971. return buf;
  19972. }
  19973. #endif
  19974. #endif
  19975. /******************************************************************************/
  19976. #ifndef WITH_NOSTDLIB
  19977. SOAP_FMAC1
  19978. void
  19979. SOAP_FMAC2
  19980. soap_print_fault_location(struct soap *soap, FILE *fd) {
  19981. #ifndef WITH_LEAN
  19982. int i, j, c1, c2;
  19983. if (soap_check_state(soap) == SOAP_OK && soap->error && soap->error != SOAP_STOP && soap->bufidx <= soap->buflen &&
  19984. soap->buflen > 0 && soap->buflen <= sizeof(soap->buf)) {
  19985. i = (int) soap->bufidx - 1;
  19986. if (i <= 0)
  19987. i = 0;
  19988. c1 = soap->buf[i];
  19989. soap->buf[i] = '\0';
  19990. if ((int) soap->buflen >= i + 1024)
  19991. j = i + 1023;
  19992. else
  19993. j = (int) soap->buflen - 1;
  19994. c2 = soap->buf[j];
  19995. soap->buf[j] = '\0';
  19996. fprintf(fd, "%s%c\n<!-- ** HERE ** -->\n", soap->buf, c1);
  19997. if (soap->bufidx < soap->buflen)
  19998. fprintf(fd, "%s\n", soap->buf + soap->bufidx);
  19999. soap->buf[i] = (char) c1;
  20000. soap->buf[j] = (char) c2;
  20001. }
  20002. #else
  20003. (void)soap;
  20004. (void)fd;
  20005. #endif
  20006. }
  20007. #endif
  20008. /******************************************************************************/
  20009. #ifdef __cplusplus
  20010. #ifndef WITH_LEAN
  20011. #ifndef WITH_NOSTDLIB
  20012. #ifndef WITH_COMPAT
  20013. SOAP_FMAC1
  20014. void
  20015. SOAP_FMAC2
  20016. soap_stream_fault_location(struct soap *soap, std::ostream &os) {
  20017. int i, j, c1, c2;
  20018. if (soap_check_state(soap) == SOAP_OK && soap->error && soap->error != SOAP_STOP && soap->bufidx <= soap->buflen &&
  20019. soap->buflen > 0 && soap->buflen <= sizeof(soap->buf)) {
  20020. i = (int) soap->bufidx - 1;
  20021. if (i <= 0)
  20022. i = 0;
  20023. c1 = soap->buf[i];
  20024. soap->buf[i] = '\0';
  20025. if ((int) soap->buflen >= i + 1024)
  20026. j = i + 1023;
  20027. else
  20028. j = (int) soap->buflen - 1;
  20029. c2 = soap->buf[j];
  20030. soap->buf[j] = '\0';
  20031. os << soap->buf << (char) c1 << std::endl << "<!-- ** HERE ** -->" << std::endl;
  20032. if (soap->bufidx < soap->buflen)
  20033. os << soap->buf + soap->bufidx << std::endl;
  20034. soap->buf[i] = (char) c1;
  20035. soap->buf[j] = (char) c2;
  20036. }
  20037. }
  20038. #endif
  20039. #endif
  20040. #endif
  20041. #endif
  20042. /******************************************************************************/
  20043. SOAP_FMAC1
  20044. int
  20045. SOAP_FMAC2
  20046. soap_register_plugin_arg(struct soap *soap, int (*fcreate)(struct soap *, struct soap_plugin *, void *), void *arg) {
  20047. struct soap_plugin *p;
  20048. int err;
  20049. p = (struct soap_plugin *) SOAP_MALLOC(soap, sizeof(struct soap_plugin));
  20050. if (!p)
  20051. return soap->error = SOAP_EOM;
  20052. p->id = NULL;
  20053. p->data = NULL;
  20054. p->fcopy = NULL;
  20055. p->fdelete = NULL;
  20056. err = fcreate(soap, p, arg);
  20057. if (!err && p->fdelete && p->id) {
  20058. if (!soap_lookup_plugin(soap, p->id)) {
  20059. p->next = soap->plugins;
  20060. soap->plugins = p;
  20061. DBGLOG(TEST, SOAP_MESSAGE(fdebug, "Registered '%s' plugin\n", p->id));
  20062. return SOAP_OK;
  20063. }
  20064. DBGLOG(TEST,
  20065. SOAP_MESSAGE(fdebug, "Could not register plugin '%s': plugin with the same ID already registered\n",
  20066. p->id));
  20067. SOAP_FREE(soap, p);
  20068. return SOAP_OK;
  20069. }
  20070. DBGLOG(TEST, SOAP_MESSAGE(fdebug,
  20071. "Could not register plugin '%s': plugin returned error %d or plugin ID not set or fdelete callback not set\n",
  20072. p->id ? p->id : "plugin ID not set", err));
  20073. SOAP_FREE(soap, p);
  20074. soap->error = err ? err : SOAP_PLUGIN_ERROR;
  20075. return soap->error;
  20076. }
  20077. /******************************************************************************/
  20078. static void *
  20079. fplugin(struct soap *soap, const char *id) {
  20080. struct soap_plugin *p;
  20081. for (p = soap->plugins; p; p = p->next)
  20082. if (p->id == id || !strcmp(p->id, id))
  20083. return p->data;
  20084. return NULL;
  20085. }
  20086. /******************************************************************************/
  20087. SOAP_FMAC1
  20088. void *
  20089. SOAP_FMAC2
  20090. soap_lookup_plugin(struct soap *soap, const char *id) {
  20091. return soap->fplugin(soap, id);
  20092. }
  20093. /******************************************************************************/
  20094. #ifdef __cplusplus
  20095. }
  20096. #endif
  20097. /******************************************************************************\
  20098. *
  20099. * C++ soap struct methods
  20100. *
  20101. \******************************************************************************/
  20102. #ifdef __cplusplus
  20103. soap::soap() {
  20104. soap_init(this);
  20105. /* no logs to prevent DEBUG mode leaks when the user calls a soap_init() on this context */
  20106. soap_set_test_logfile(this, NULL);
  20107. soap_set_sent_logfile(this, NULL);
  20108. soap_set_recv_logfile(this, NULL);
  20109. }
  20110. #endif
  20111. /******************************************************************************/
  20112. #ifdef __cplusplus
  20113. soap::soap(soap_mode m) {
  20114. soap_init1(this, m);
  20115. }
  20116. #endif
  20117. /******************************************************************************/
  20118. #ifdef __cplusplus
  20119. soap::soap(soap_mode im, soap_mode om) {
  20120. soap_init2(this, im, om);
  20121. }
  20122. #endif
  20123. /******************************************************************************/
  20124. #ifdef __cplusplus
  20125. soap::soap(const struct soap &soap) {
  20126. soap_copy_context(this, &soap);
  20127. }
  20128. #endif
  20129. /******************************************************************************/
  20130. #ifdef __cplusplus
  20131. struct soap &soap::operator=(const struct soap &soap) {
  20132. soap_done(this);
  20133. soap_copy_context(this, &soap);
  20134. return *this;
  20135. }
  20136. #endif
  20137. /******************************************************************************/
  20138. #ifdef __cplusplus
  20139. void soap::destroy() {
  20140. soap_destroy(this);
  20141. soap_end(this);
  20142. }
  20143. #endif
  20144. /******************************************************************************/
  20145. #ifdef __cplusplus
  20146. soap::~soap() {
  20147. soap_done(this);
  20148. }
  20149. #endif
  20150. /******************************************************************************/