ModelicaMatIO.c 729 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966179671796817969179701797117972179731797417975179761797717978179791798017981179821798317984179851798617987179881798917990179911799217993179941799517996179971799817999180001800118002180031800418005180061800718008180091801018011180121801318014180151801618017180181801918020180211802218023180241802518026180271802818029180301803118032180331803418035180361803718038180391804018041180421804318044180451804618047180481804918050180511805218053180541805518056180571805818059180601806118062180631806418065180661806718068180691807018071180721807318074180751807618077180781807918080180811808218083180841808518086180871808818089180901809118092180931809418095180961809718098180991810018101181021810318104181051810618107181081810918110181111811218113181141811518116181171811818119181201812118122181231812418125181261812718128181291813018131181321813318134181351813618137181381813918140181411814218143181441814518146181471814818149181501815118152181531815418155181561815718158181591816018161181621816318164181651816618167181681816918170181711817218173181741817518176181771817818179181801818118182181831818418185181861818718188181891819018191181921819318194181951819618197181981819918200182011820218203182041820518206182071820818209182101821118212182131821418215182161821718218182191822018221182221822318224182251822618227182281822918230182311823218233182341823518236182371823818239182401824118242182431824418245182461824718248182491825018251182521825318254182551825618257182581825918260182611826218263182641826518266182671826818269182701827118272182731827418275182761827718278182791828018281182821828318284182851828618287182881828918290182911829218293182941829518296182971829818299183001830118302183031830418305183061830718308183091831018311183121831318314183151831618317183181831918320183211832218323183241832518326183271832818329183301833118332183331833418335183361833718338183391834018341183421834318344183451834618347183481834918350183511835218353183541835518356183571835818359183601836118362183631836418365183661836718368183691837018371183721837318374183751837618377183781837918380183811838218383183841838518386183871838818389183901839118392183931839418395183961839718398183991840018401184021840318404184051840618407184081840918410184111841218413184141841518416184171841818419184201842118422184231842418425184261842718428184291843018431184321843318434184351843618437184381843918440184411844218443184441844518446184471844818449184501845118452184531845418455184561845718458184591846018461184621846318464184651846618467184681846918470184711847218473184741847518476184771847818479184801848118482184831848418485184861848718488184891849018491184921849318494184951849618497184981849918500185011850218503185041850518506185071850818509185101851118512185131851418515185161851718518185191852018521185221852318524185251852618527185281852918530185311853218533185341853518536185371853818539185401854118542185431854418545185461854718548185491855018551185521855318554185551855618557185581855918560185611856218563185641856518566185671856818569185701857118572185731857418575185761857718578185791858018581185821858318584185851858618587185881858918590185911859218593185941859518596185971859818599186001860118602186031860418605186061860718608186091861018611186121861318614186151861618617186181861918620186211862218623186241862518626186271862818629186301863118632186331863418635186361863718638186391864018641186421864318644186451864618647186481864918650186511865218653186541865518656186571865818659186601866118662186631866418665186661866718668186691867018671186721867318674186751867618677186781867918680186811868218683186841868518686186871868818689186901869118692186931869418695186961869718698186991870018701187021870318704187051870618707187081870918710187111871218713187141871518716187171871818719187201872118722187231872418725187261872718728187291873018731187321873318734187351873618737187381873918740187411874218743187441874518746187471874818749187501875118752187531875418755187561875718758187591876018761187621876318764187651876618767187681876918770187711877218773187741877518776187771877818779187801878118782187831878418785187861878718788187891879018791187921879318794187951879618797187981879918800188011880218803188041880518806188071880818809188101881118812188131881418815188161881718818188191882018821188221882318824188251882618827188281882918830188311883218833188341883518836188371883818839188401884118842188431884418845188461884718848188491885018851188521885318854188551885618857188581885918860188611886218863188641886518866188671886818869188701887118872188731887418875188761887718878188791888018881188821888318884188851888618887188881888918890188911889218893188941889518896188971889818899189001890118902189031890418905189061890718908189091891018911189121891318914189151891618917189181891918920189211892218923189241892518926189271892818929189301893118932189331893418935189361893718938189391894018941189421894318944189451894618947189481894918950189511895218953189541895518956189571895818959189601896118962189631896418965189661896718968189691897018971189721897318974189751897618977189781897918980189811898218983189841898518986189871898818989189901899118992189931899418995189961899718998189991900019001190021900319004190051900619007190081900919010190111901219013190141901519016190171901819019190201902119022190231902419025190261902719028190291903019031190321903319034190351903619037190381903919040190411904219043190441904519046190471904819049190501905119052190531905419055190561905719058190591906019061190621906319064190651906619067190681906919070190711907219073190741907519076190771907819079190801908119082190831908419085190861908719088190891909019091190921909319094190951909619097190981909919100191011910219103191041910519106191071910819109191101911119112191131911419115191161911719118191191912019121191221912319124191251912619127191281912919130191311913219133191341913519136191371913819139191401914119142191431914419145191461914719148191491915019151191521915319154191551915619157191581915919160191611916219163191641916519166191671916819169191701917119172191731917419175191761917719178191791918019181191821918319184191851918619187191881918919190191911919219193191941919519196191971919819199192001920119202192031920419205192061920719208192091921019211192121921319214192151921619217192181921919220192211922219223192241922519226192271922819229192301923119232192331923419235192361923719238192391924019241192421924319244192451924619247192481924919250192511925219253192541925519256192571925819259192601926119262192631926419265192661926719268192691927019271192721927319274192751927619277192781927919280192811928219283192841928519286192871928819289192901929119292192931929419295192961929719298192991930019301193021930319304193051930619307193081930919310193111931219313193141931519316193171931819319193201932119322193231932419325193261932719328193291933019331193321933319334193351933619337193381933919340193411934219343193441934519346193471934819349193501935119352193531935419355193561935719358193591936019361193621936319364193651936619367193681936919370193711937219373193741937519376193771937819379193801938119382193831938419385193861938719388193891939019391193921939319394193951939619397193981939919400194011940219403194041940519406194071940819409194101941119412194131941419415194161941719418194191942019421194221942319424194251942619427194281942919430194311943219433194341943519436194371943819439194401944119442194431944419445194461944719448194491945019451194521945319454194551945619457194581945919460194611946219463194641946519466194671946819469194701947119472194731947419475194761947719478194791948019481194821948319484194851948619487194881948919490194911949219493194941949519496194971949819499195001950119502195031950419505195061950719508195091951019511195121951319514195151951619517195181951919520195211952219523195241952519526195271952819529195301953119532195331953419535195361953719538195391954019541195421954319544195451954619547195481954919550195511955219553195541955519556195571955819559195601956119562195631956419565195661956719568195691957019571195721957319574195751957619577195781957919580195811958219583195841958519586195871958819589195901959119592195931959419595195961959719598195991960019601196021960319604196051960619607196081960919610196111961219613196141961519616196171961819619196201962119622196231962419625196261962719628196291963019631196321963319634196351963619637196381963919640196411964219643196441964519646196471964819649196501965119652196531965419655196561965719658196591966019661196621966319664196651966619667196681966919670196711967219673196741967519676196771967819679196801968119682196831968419685196861968719688196891969019691196921969319694196951969619697196981969919700197011970219703197041970519706197071970819709197101971119712197131971419715197161971719718197191972019721197221972319724197251972619727197281972919730197311973219733197341973519736197371973819739197401974119742197431974419745197461974719748197491975019751197521975319754197551975619757197581975919760197611976219763197641976519766197671976819769197701977119772197731977419775197761977719778197791978019781197821978319784197851978619787197881978919790197911979219793197941979519796197971979819799198001980119802198031980419805198061980719808198091981019811198121981319814198151981619817198181981919820198211982219823198241982519826198271982819829198301983119832198331983419835198361983719838198391984019841198421984319844198451984619847198481984919850198511985219853198541985519856198571985819859198601986119862198631986419865198661986719868198691987019871198721987319874198751987619877198781987919880198811988219883198841988519886198871988819889198901989119892198931989419895198961989719898198991990019901199021990319904199051990619907199081990919910199111991219913199141991519916199171991819919199201992119922199231992419925199261992719928199291993019931199321993319934199351993619937199381993919940199411994219943199441994519946199471994819949199501995119952199531995419955199561995719958199591996019961199621996319964199651996619967199681996919970199711997219973199741997519976199771997819979199801998119982199831998419985199861998719988199891999019991199921999319994199951999619997199981999920000200012000220003200042000520006200072000820009200102001120012200132001420015200162001720018200192002020021200222002320024200252002620027200282002920030200312003220033200342003520036200372003820039200402004120042200432004420045200462004720048200492005020051200522005320054200552005620057200582005920060200612006220063200642006520066200672006820069200702007120072200732007420075200762007720078200792008020081200822008320084200852008620087200882008920090200912009220093200942009520096200972009820099201002010120102201032010420105201062010720108201092011020111201122011320114201152011620117201182011920120201212012220123201242012520126201272012820129201302013120132201332013420135201362013720138201392014020141201422014320144201452014620147201482014920150201512015220153201542015520156201572015820159201602016120162201632016420165201662016720168201692017020171201722017320174201752017620177201782017920180201812018220183201842018520186201872018820189201902019120192201932019420195201962019720198201992020020201202022020320204202052020620207202082020920210202112021220213202142021520216202172021820219202202022120222202232022420225202262022720228202292023020231202322023320234202352023620237202382023920240202412024220243202442024520246202472024820249202502025120252202532025420255202562025720258202592026020261202622026320264202652026620267202682026920270202712027220273202742027520276202772027820279202802028120282202832028420285202862028720288202892029020291202922029320294202952029620297202982029920300203012030220303203042030520306203072030820309203102031120312203132031420315203162031720318203192032020321203222032320324203252032620327203282032920330203312033220333203342033520336203372033820339203402034120342203432034420345203462034720348203492035020351203522035320354203552035620357203582035920360203612036220363203642036520366203672036820369203702037120372203732037420375203762037720378203792038020381203822038320384203852038620387203882038920390203912039220393203942039520396203972039820399204002040120402204032040420405204062040720408204092041020411204122041320414204152041620417204182041920420204212042220423204242042520426204272042820429204302043120432204332043420435204362043720438204392044020441204422044320444204452044620447204482044920450204512045220453204542045520456204572045820459204602046120462204632046420465204662046720468204692047020471204722047320474204752047620477204782047920480204812048220483204842048520486204872048820489204902049120492204932049420495204962049720498204992050020501205022050320504205052050620507205082050920510205112051220513205142051520516205172051820519205202052120522205232052420525205262052720528205292053020531205322053320534205352053620537205382053920540205412054220543205442054520546205472054820549205502055120552205532055420555205562055720558205592056020561205622056320564205652056620567205682056920570205712057220573205742057520576205772057820579205802058120582205832058420585205862058720588205892059020591205922059320594205952059620597205982059920600206012060220603206042060520606206072060820609206102061120612206132061420615206162061720618206192062020621206222062320624206252062620627206282062920630206312063220633206342063520636206372063820639206402064120642206432064420645206462064720648206492065020651206522065320654206552065620657206582065920660206612066220663206642066520666206672066820669206702067120672206732067420675206762067720678206792068020681206822068320684206852068620687206882068920690206912069220693206942069520696206972069820699207002070120702207032070420705207062070720708207092071020711207122071320714207152071620717207182071920720207212072220723207242072520726207272072820729207302073120732207332073420735207362073720738207392074020741207422074320744207452074620747207482074920750207512075220753207542075520756207572075820759207602076120762207632076420765207662076720768207692077020771207722077320774207752077620777207782077920780207812078220783207842078520786207872078820789207902079120792207932079420795207962079720798207992080020801208022080320804208052080620807208082080920810208112081220813208142081520816208172081820819208202082120822208232082420825208262082720828208292083020831208322083320834208352083620837208382083920840208412084220843208442084520846208472084820849208502085120852208532085420855208562085720858208592086020861208622086320864208652086620867208682086920870208712087220873208742087520876208772087820879208802088120882208832088420885208862088720888208892089020891208922089320894208952089620897208982089920900209012090220903209042090520906209072090820909209102091120912209132091420915209162091720918209192092020921209222092320924209252092620927209282092920930209312093220933209342093520936209372093820939209402094120942209432094420945209462094720948209492095020951209522095320954209552095620957209582095920960209612096220963209642096520966209672096820969209702097120972209732097420975209762097720978209792098020981209822098320984209852098620987209882098920990209912099220993209942099520996209972099820999210002100121002210032100421005210062100721008210092101021011210122101321014210152101621017210182101921020210212102221023210242102521026210272102821029210302103121032210332103421035210362103721038210392104021041210422104321044210452104621047210482104921050210512105221053210542105521056210572105821059210602106121062210632106421065210662106721068210692107021071210722107321074210752107621077210782107921080210812108221083210842108521086210872108821089210902109121092210932109421095210962109721098210992110021101211022110321104211052110621107211082110921110211112111221113211142111521116211172111821119211202112121122211232112421125211262112721128211292113021131211322113321134211352113621137211382113921140211412114221143211442114521146211472114821149211502115121152211532115421155211562115721158211592116021161211622116321164211652116621167211682116921170211712117221173211742117521176211772117821179211802118121182211832118421185211862118721188211892119021191211922119321194211952119621197211982119921200212012120221203212042120521206212072120821209212102121121212212132121421215212162121721218212192122021221212222122321224212252122621227212282122921230212312123221233212342123521236212372123821239212402124121242212432124421245212462124721248212492125021251212522125321254212552125621257212582125921260212612126221263212642126521266212672126821269212702127121272212732127421275212762127721278212792128021281212822128321284212852128621287212882128921290212912129221293212942129521296212972129821299213002130121302213032130421305213062130721308213092131021311213122131321314213152131621317213182131921320213212132221323213242132521326213272132821329213302133121332213332133421335213362133721338213392134021341213422134321344213452134621347213482134921350213512135221353213542135521356213572135821359213602136121362213632136421365213662136721368213692137021371213722137321374213752137621377213782137921380213812138221383213842138521386213872138821389213902139121392213932139421395213962139721398213992140021401214022140321404214052140621407214082140921410214112141221413214142141521416214172141821419214202142121422214232142421425
  1. /* ModelicaMatIO.c - MAT file I/O functions
  2. Copyright (C) 2013-2016, Christopher C. Hulbert, Modelica Association, and ITI GmbH
  3. Copyright (C) 2005-2013, Christopher C. Hulbert
  4. All rights reserved.
  5. Redistribution and use in source and binary forms, with or without
  6. modification, are permitted provided that the following conditions are met:
  7. 1. Redistributions of source code must retain the above copyright notice,
  8. this list of conditions and the following disclaimer.
  9. 2. Redistributions in binary form must reproduce the above copyright
  10. notice, this list of conditions and the following disclaimer in the
  11. documentation and/or other materials provided with the distribution.
  12. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  13. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  14. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  15. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  16. FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  17. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  18. SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  19. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  20. OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  21. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  22. */
  23. /*
  24. This file was created by concatenation of the following C source files of the
  25. MAT file I/O library from <http://sourceforge.net/projects/matio/>:
  26. endian.c
  27. inflate.c
  28. io.c
  29. read_data.c
  30. snprintf.c
  31. mat.c
  32. mat4.c
  33. mat5.c
  34. mat73.c
  35. matvar_cell.c
  36. matvar_struct.c
  37. */
  38. /*
  39. By default v4 and v6 MAT-files are supported. The v7 and v7.3 MAT-file
  40. formats require additional preprocessor options and third-party libraries.
  41. The following #define's are available.
  42. NO_FILE_SYSTEM: A file system is not present (e.g. on dSPACE or xPC).
  43. HAVE_ZLIB=1 : Enables the support of v7 MAT-files
  44. The zlib (>= v1.2.3) library is required.
  45. HAVE_HDF5=1 : Enables the support of v7.3 MAT-files
  46. The hdf5 (>= v1.8) and szip libraries are required.
  47. */
  48. #include "ModelicaUtilities.h"
  49. #if !defined(NO_FILE_SYSTEM)
  50. #if defined(__gnu_linux__)
  51. #define _GNU_SOURCE 1
  52. #elif defined(__MINGW32__) && !defined(__USE_MINGW_ANSI_STDIO)
  53. #define __USE_MINGW_ANSI_STDIO 1
  54. #endif
  55. #include <stdarg.h>
  56. /* -------------------------------
  57. * ---------- endian.c
  58. * -------------------------------
  59. */
  60. /** @file endian.c
  61. * @brief Functions to handle endian specifics
  62. */
  63. #include <stdlib.h>
  64. #ifndef MATIO_PRIVATE_H
  65. #define MATIO_PRIVATE_H
  66. /* Extended sparse matrix data types */
  67. #define EXTENDED_SPARSE 1
  68. /* Define to 1 if you have the `asprintf' function. */
  69. #if defined(__CYGWIN__)
  70. #define HAVE_ASPRINTF 1
  71. #else
  72. #undef HAVE_ASPRINTF
  73. #endif
  74. /* Define to 1 if the system has the type `intmax_t'. */
  75. #if defined(_WIN32)
  76. #if defined(_MSC_VER) && _MSC_VER >= 1600
  77. #define HAVE_INTMAX_T 1
  78. #elif defined(__WATCOMC__) || defined(__MINGW32__) || defined(__CYGWIN__)
  79. #define HAVE_INTMAX_T 1
  80. #else
  81. #undef HAVE_INTMAX_T
  82. #endif
  83. #elif defined(__GNUC__) && !defined(__VXWORKS__)
  84. #define HAVE_INTMAX_T 1
  85. #else
  86. #undef HAVE_INTMAX_T
  87. #endif
  88. /* Define to 1 if you have the <inttypes.h> header file. */
  89. #if defined(_WIN32)
  90. #if defined(_MSC_VER) && _MSC_VER >= 1800
  91. #define HAVE_INTTYPES_H 1
  92. #elif defined(__WATCOMC__) || defined(__MINGW32__) || defined(__CYGWIN__)
  93. #define HAVE_INTTYPES_H 1
  94. #else
  95. #undef HAVE_INTTYPES_H
  96. #endif
  97. #elif defined(__GNUC__)
  98. #define HAVE_INTTYPES_H 1
  99. #else
  100. #undef HAVE_INTTYPES_H
  101. #endif
  102. /* Define to 1 if you have the `localeconv' function. */
  103. #define HAVE_LOCALECONV 1
  104. /* Define to 1 if you have the <locale.h> header file. */
  105. #define HAVE_LOCALE_H 1
  106. /* Define to 1 if the system has the type `long double'. */
  107. #if defined (_WIN32)
  108. #if defined(__WATCOMC__) || (defined(_MSC_VER) && _MSC_VER >= 1300)
  109. #define HAVE_LONG_DOUBLE 1
  110. #endif
  111. #endif
  112. #if defined(__GNUC__)
  113. #define HAVE_LONG_DOUBLE 1
  114. #endif
  115. /* Define to 1 if the system has the type `long long int'. */
  116. #if defined (_WIN32)
  117. #if defined(__WATCOMC__) || (defined(_MSC_VER) && _MSC_VER >= 1300)
  118. #define HAVE_LONG_LONG_INT 1
  119. #endif
  120. #endif
  121. #if defined(__GNUC__) && __STDC_VERSION__ >= 199901L
  122. #define HAVE_LONG_LONG_INT 1
  123. #endif
  124. /* Define to 1 if the system has the type `ptrdiff_t'. */
  125. #define HAVE_PTRDIFF_T 1
  126. /* Define to 1 if you have a C99 compliant `snprintf' function. */
  127. #if defined(STDC99)
  128. #define HAVE_SNPRINTF 1
  129. #elif defined(__MINGW32__) || defined(__CYGWIN__)
  130. #if __STDC_VERSION__ >= 199901L
  131. #define HAVE_SNPRINTF 1
  132. #endif
  133. #elif defined(__WATCOMC__)
  134. #define HAVE_SNPRINTF 1
  135. #elif defined(__TURBOC__) && __TURBOC__ >= 0x550
  136. #define HAVE_SNPRINTF 1
  137. #elif defined(MSDOS) && defined(__BORLANDC__) && (BORLANDC > 0x410)
  138. #define HAVE_SNPRINTF 1
  139. #elif defined(_MSC_VER) && _MSC_VER >= 1900
  140. #define HAVE_SNPRINTF 1
  141. #else
  142. #undef HAVE_SNPRINTF
  143. #endif
  144. /* Define to 1 if you have the <stdarg.h> header file. */
  145. #define HAVE_STDARG_H 1
  146. /* Define to 1 if you have the <stddef.h> header file. */
  147. #define HAVE_STDDEF_H 1
  148. /* Define to 1 if you have the <stdint.h> header file. */
  149. #if defined(_WIN32)
  150. #if defined(_MSC_VER) && _MSC_VER >= 1600
  151. #define HAVE_STDINT_H 1
  152. #elif defined(__WATCOMC__) || defined(__MINGW32__) || defined(__CYGWIN__)
  153. #define HAVE_STDINT_H 1
  154. #else
  155. #undef HAVE_STDINT_H
  156. #endif
  157. #elif defined(__GNUC__) && !defined(__VXWORKS__)
  158. #define HAVE_STDINT_H 1
  159. #else
  160. #undef HAVE_STDINT_H
  161. #endif
  162. /* Define to 1 if you have the <stdlib.h> header file. */
  163. #define HAVE_STDLIB_H 1
  164. /* Define to 1 if `decimal_point' is member of `struct lconv'. */
  165. #define HAVE_STRUCT_LCONV_DECIMAL_POINT 1
  166. /* Define to 1 if `thousands_sep' is member of `struct lconv'. */
  167. #define HAVE_STRUCT_LCONV_THOUSANDS_SEP 1
  168. /* Define to 1 if the system has the type `uintmax_t'. */
  169. #if defined(_WIN32)
  170. #if defined(_MSC_VER) && _MSC_VER >= 1600
  171. #define HAVE_UINTMAX_T 1
  172. #elif defined(__WATCOMC__) || defined(__MINGW32__) || defined(__CYGWIN__)
  173. #define HAVE_UINTMAX_T 1
  174. #else
  175. #undef HAVE_UINTMAX_T
  176. #endif
  177. #elif defined(__GNUC__) && !defined(__VXWORKS__)
  178. #define HAVE_UINTMAX_T 1
  179. #else
  180. #undef HAVE_UINTMAX_T
  181. #endif
  182. /* Define to 1 if the system has the type `uintptr_t'. */
  183. #if defined(__LCC__) || (defined(_MSC_VER) && _MSC_VER <= 1200)
  184. #undef HAVE_UINTPTR_T
  185. #else
  186. #define HAVE_UINTPTR_T 1
  187. #endif
  188. /* Define to 1 if the system has the type `unsigned long long int'. */
  189. #if defined (_WIN32)
  190. #if defined(__WATCOMC__) || (defined(_MSC_VER) && _MSC_VER >= 1300)
  191. #define HAVE_UNSIGNED_LONG_LONG_INT 1
  192. #endif
  193. #endif
  194. #if defined(__GNUC__) && __STDC_VERSION__ >= 199901L
  195. #define HAVE_UNSIGNED_LONG_LONG_INT 1
  196. #endif
  197. /* Define to 1 if you have the `vasprintf' function. */
  198. #if defined(__CYGWIN__)
  199. #define HAVE_VASPRINTF 1
  200. #else
  201. #undef HAVE_VASPRINTF
  202. #endif
  203. /* Define to 1 if you have the `va_copy' function or macro. */
  204. #if defined(__GNUC__) && __STDC_VERSION__ >= 199901L
  205. #define HAVE_VA_COPY 1
  206. #elif defined(_MSC_VER) && _MSC_VER >= 1800
  207. #define HAVE_VA_COPY 1
  208. #elif defined(__WATCOMC__)
  209. #define HAVE_VA_COPY 1
  210. #else
  211. #undef HAVE_VA_COPY
  212. #endif
  213. /* Define to 1 if you have a C99 compliant `vsnprintf' function. */
  214. #if defined(STDC99)
  215. #define HAVE_VSNPRINTF 1
  216. #elif defined(__MINGW32__) || defined(__CYGWIN__)
  217. #if __STDC_VERSION__ >= 199901L
  218. #define HAVE_VSNPRINTF 1
  219. #endif
  220. #elif defined(__WATCOMC__)
  221. #define HAVE_VSNPRINTF 1
  222. #elif defined(__TURBOC__) && __TURBOC__ >= 0x550
  223. #define HAVE_VSNPRINTF 1
  224. #elif defined(MSDOS) && defined(__BORLANDC__) && (BORLANDC > 0x410)
  225. #define HAVE_VSNPRINTF 1
  226. #elif defined(_MSC_VER) && _MSC_VER >= 1900
  227. #define HAVE_VSNPRINTF 1
  228. #else
  229. #undef HAVE_VSNPRINTF
  230. #endif
  231. /* Define to 1 if you have the `__va_copy' function or macro. */
  232. #if defined(__GNUC__)
  233. #define HAVE___VA_COPY 1
  234. #elif defined(__WATCOMC__)
  235. #define HAVE___VA_COPY 1
  236. #else
  237. #undef HAVE___VA_COPY
  238. #endif
  239. /* Platform */
  240. #if defined(_MSC_VER) && defined(_WIN64)
  241. #define MATIO_PLATFORM "x86_64-pc-windows"
  242. #elif defined(_MSC_VER) && defined(_WIN32)
  243. #define MATIO_PLATFORM "i686-pc-windows"
  244. #else
  245. #define MATIO_PLATFORM "UNKNOWN"
  246. #endif
  247. /* Define to 1 if you have the ANSI C header files. */
  248. #define STDC_HEADERS 1
  249. /* Z prefix */
  250. #undef Z_PREFIX
  251. #include "ModelicaMatIO.h"
  252. #if HAVE_INTTYPES_H
  253. # define __STDC_FORMAT_MACROS
  254. #endif
  255. #if defined(HAVE_ZLIB)
  256. # include <zlib.h>
  257. #endif
  258. #if defined(HAVE_HDF5)
  259. # include <hdf5.h>
  260. #else
  261. # define hobj_ref_t int
  262. # define hid_t int
  263. #endif
  264. #if defined(HAVE_ZLIB) && HAVE_ZLIB
  265. # define ZLIB_BYTE_PTR(a) ((Bytef *)(a))
  266. #endif
  267. /** @if mat_devman
  268. * @brief Matlab MAT File information
  269. *
  270. * Contains information about a Matlab MAT file
  271. * @ingroup mat_internal
  272. * @endif
  273. */
  274. struct _mat_t {
  275. void *fp; /**< File pointer for the MAT file */
  276. char *header; /**< MAT File header string */
  277. char *subsys_offset; /**< Offset */
  278. char *filename; /**< Filename of the MAT file */
  279. int version; /**< MAT File version */
  280. int byteswap; /**< 1 if byte swapping is required, 0 otherwise */
  281. int mode; /**< Access mode */
  282. int bof; /**< Beginning of file not including any header */
  283. int next_index; /**< Index/File position of next variable to read */
  284. int num_datasets; /**< Number of datasets in the file */
  285. hid_t refs_id; /**< Id of the /#refs# group in HDF5 */
  286. };
  287. /** @if mat_devman
  288. * @brief internal structure for MAT variables
  289. * @ingroup mat_internal
  290. * @endif
  291. */
  292. struct matvar_internal {
  293. char *hdf5_name; /**< Name */
  294. hobj_ref_t hdf5_ref; /**< Reference */
  295. hid_t id; /**< Id */
  296. int fpos; /**< Offset from the beginning of the MAT file to the variable */
  297. int datapos; /**< Offset from the beginning of the MAT file to the data */
  298. mat_t *fp; /**< Pointer to the MAT file structure (mat_t) */
  299. unsigned num_fields; /**< Number of fields */
  300. char **fieldnames; /**< Pointer to fieldnames */
  301. #if defined(HAVE_ZLIB)
  302. z_streamp z; /**< zlib compression state */
  303. void *data; /**< Inflated data array */
  304. #endif
  305. };
  306. /* snprintf.c */
  307. #if !HAVE_VSNPRINTF
  308. int rpl_vsnprintf(char *, size_t, const char *, va_list);
  309. #define mat_vsnprintf rpl_vsnprintf
  310. #else
  311. #define mat_vsnprintf vsnprintf
  312. #endif /* !HAVE_VSNPRINTF */
  313. #if !HAVE_SNPRINTF
  314. int rpl_snprintf(char *, size_t, const char *, ...);
  315. #define mat_snprintf rpl_snprintf
  316. #else
  317. #define mat_snprintf snprintf
  318. #endif /* !HAVE_SNPRINTF */
  319. #if !HAVE_VASPRINTF
  320. int rpl_vasprintf(char **, const char *, va_list);
  321. #define mat_vasprintf rpl_vasprintf
  322. #else
  323. #define mat_vasprintf vasprintf
  324. #endif /* !HAVE_VASPRINTF */
  325. #if !HAVE_ASPRINTF
  326. int rpl_asprintf(char **, const char *, ...);
  327. #define mat_asprintf rpl_asprintf
  328. #else
  329. #define mat_asprintf asprintf
  330. #endif /* !HAVE_ASPRINTF */
  331. /* endian.c */
  332. static double Mat_doubleSwap(double *a);
  333. static float Mat_floatSwap(float *a);
  334. #ifdef HAVE_MATIO_INT64_T
  335. static mat_int64_t Mat_int64Swap(mat_int64_t *a);
  336. #endif /* HAVE_MATIO_INT64_T */
  337. #ifdef HAVE_MATIO_UINT64_T
  338. static mat_uint64_t Mat_uint64Swap(mat_uint64_t *a);
  339. #endif /* HAVE_MATIO_UINT64_T */
  340. static mat_int32_t Mat_int32Swap(mat_int32_t *a);
  341. static mat_uint32_t Mat_uint32Swap(mat_uint32_t *a);
  342. static mat_int16_t Mat_int16Swap(mat_int16_t *a);
  343. static mat_uint16_t Mat_uint16Swap(mat_uint16_t *a);
  344. /* read_data.c */
  345. static int ReadDoubleData(mat_t *mat,double *data,enum matio_types data_type,
  346. int len);
  347. static int ReadSingleData(mat_t *mat,float *data,enum matio_types data_type,
  348. int len);
  349. #ifdef HAVE_MATIO_INT64_T
  350. static int ReadInt64Data (mat_t *mat,mat_int64_t *data,
  351. enum matio_types data_type,int len);
  352. #endif /* HAVE_MATIO_INT64_T */
  353. #ifdef HAVE_MATIO_UINT64_T
  354. static int ReadUInt64Data(mat_t *mat,mat_uint64_t *data,
  355. enum matio_types data_type,int len);
  356. #endif /* HAVE_MATIO_UINT64_T */
  357. static int ReadInt32Data (mat_t *mat,mat_int32_t *data,
  358. enum matio_types data_type,int len);
  359. static int ReadUInt32Data(mat_t *mat,mat_uint32_t *data,
  360. enum matio_types data_type,int len);
  361. static int ReadInt16Data (mat_t *mat,mat_int16_t *data,
  362. enum matio_types data_type,int len);
  363. static int ReadUInt16Data(mat_t *mat,mat_uint16_t *data,
  364. enum matio_types data_type,int len);
  365. static int ReadInt8Data (mat_t *mat,mat_int8_t *data,
  366. enum matio_types data_type,int len);
  367. static int ReadUInt8Data (mat_t *mat,mat_uint8_t *data,
  368. enum matio_types data_type,int len);
  369. static int ReadCharData (mat_t *mat,char *data,enum matio_types data_type,
  370. int len);
  371. static int ReadDataSlab1(mat_t *mat,void *data,enum matio_classes class_type,
  372. enum matio_types data_type,int start,int stride,int edge);
  373. static int ReadDataSlab2(mat_t *mat,void *data,enum matio_classes class_type,
  374. enum matio_types data_type,size_t *dims,int *start,int *stride,
  375. int *edge);
  376. static int ReadDataSlabN(mat_t *mat,void *data,enum matio_classes class_type,
  377. enum matio_types data_type,int rank,size_t *dims,int *start,
  378. int *stride,int *edge);
  379. #if defined(HAVE_ZLIB)
  380. static int ReadCompressedDoubleData(mat_t *mat,z_streamp z,double *data,
  381. enum matio_types data_type,int len);
  382. static int ReadCompressedSingleData(mat_t *mat,z_streamp z,float *data,
  383. enum matio_types data_type,int len);
  384. #ifdef HAVE_MATIO_INT64_T
  385. static int ReadCompressedInt64Data(mat_t *mat,z_streamp z,mat_int64_t *data,
  386. enum matio_types data_type,int len);
  387. #endif /* HAVE_MATIO_INT64_T */
  388. #ifdef HAVE_MATIO_UINT64_T
  389. static int ReadCompressedUInt64Data(mat_t *mat,z_streamp z,mat_uint64_t *data,
  390. enum matio_types data_type,int len);
  391. #endif /* HAVE_MATIO_UINT64_T */
  392. static int ReadCompressedInt32Data(mat_t *mat,z_streamp z,mat_int32_t *data,
  393. enum matio_types data_type,int len);
  394. static int ReadCompressedUInt32Data(mat_t *mat,z_streamp z,mat_uint32_t *data,
  395. enum matio_types data_type,int len);
  396. static int ReadCompressedInt16Data(mat_t *mat,z_streamp z,mat_int16_t *data,
  397. enum matio_types data_type,int len);
  398. static int ReadCompressedUInt16Data(mat_t *mat,z_streamp z,mat_uint16_t *data,
  399. enum matio_types data_type,int len);
  400. static int ReadCompressedInt8Data(mat_t *mat,z_streamp z,mat_int8_t *data,
  401. enum matio_types data_type,int len);
  402. static int ReadCompressedUInt8Data(mat_t *mat,z_streamp z,mat_uint8_t *data,
  403. enum matio_types data_type,int len);
  404. static int ReadCompressedCharData(mat_t *mat,z_streamp z,char *data,
  405. enum matio_types data_type,int len);
  406. static int ReadCompressedDataSlab1(mat_t *mat,z_streamp z,void *data,
  407. enum matio_classes class_type,enum matio_types data_type,
  408. int start,int stride,int edge);
  409. static int ReadCompressedDataSlab2(mat_t *mat,z_streamp z,void *data,
  410. enum matio_classes class_type,enum matio_types data_type,
  411. size_t *dims,int *start,int *stride,int *edge);
  412. static int ReadCompressedDataSlabN(mat_t *mat,z_streamp z,void *data,
  413. enum matio_classes class_type,enum matio_types data_type,
  414. int rank,size_t *dims,int *start,int *stride,int *edge);
  415. /* inflate.c */
  416. static size_t InflateSkip(mat_t *mat, z_streamp z, int nbytes);
  417. static size_t InflateSkip2(mat_t *mat, matvar_t *matvar, int nbytes);
  418. static size_t InflateSkipData(mat_t *mat,z_streamp z,enum matio_types data_type,int len);
  419. static size_t InflateVarTag(mat_t *mat, matvar_t *matvar, void *buf);
  420. static size_t InflateArrayFlags(mat_t *mat, matvar_t *matvar, void *buf);
  421. static size_t InflateDimensions(mat_t *mat, matvar_t *matvar, void *buf);
  422. static size_t InflateVarNameTag(mat_t *mat, matvar_t *matvar, void *buf);
  423. static size_t InflateVarName(mat_t *mat,matvar_t *matvar,void *buf,int N);
  424. static size_t InflateDataTag(mat_t *mat, matvar_t *matvar, void *buf);
  425. static size_t InflateDataType(mat_t *mat, z_stream *matvar, void *buf);
  426. static size_t InflateData(mat_t *mat, z_streamp z, void *buf, int nBytes);
  427. static size_t InflateFieldNameLength(mat_t *mat,matvar_t *matvar,void *buf);
  428. static size_t InflateFieldNamesTag(mat_t *mat,matvar_t *matvar,void *buf);
  429. static size_t InflateFieldNames(mat_t *mat,matvar_t *matvar,void *buf,int nfields,
  430. int fieldname_length,int padding);
  431. #endif
  432. #endif
  433. /** @brief swap the bytes @c a and @c b
  434. * @ingroup mat_internal
  435. */
  436. #define swap(a,b) a^=b;b^=a;a^=b
  437. #ifdef HAVE_MATIO_INT64_T
  438. /** @brief swap the bytes of a 64-bit signed integer
  439. * @ingroup mat_internal
  440. * @param a pointer to integer to swap
  441. * @return the swapped integer
  442. */
  443. static mat_int64_t
  444. Mat_int64Swap( mat_int64_t *a )
  445. {
  446. union {
  447. mat_int8_t i1[8];
  448. mat_int64_t i8;
  449. } tmp;
  450. tmp.i8 = *a;
  451. swap( tmp.i1[0], tmp.i1[7] );
  452. swap( tmp.i1[1], tmp.i1[6] );
  453. swap( tmp.i1[2], tmp.i1[5] );
  454. swap( tmp.i1[3], tmp.i1[4] );
  455. *a = tmp.i8;
  456. return *a;
  457. }
  458. #endif /* HAVE_MATIO_INT64_T */
  459. #ifdef HAVE_MATIO_UINT64_T
  460. /** @brief swap the bytes of a 64-bit unsigned integer
  461. * @ingroup mat_internal
  462. * @param a pointer to integer to swap
  463. * @return the swapped integer
  464. */
  465. static mat_uint64_t
  466. Mat_uint64Swap( mat_uint64_t *a )
  467. {
  468. union {
  469. mat_uint8_t i1[8];
  470. mat_uint64_t i8;
  471. } tmp;
  472. tmp.i8 = *a;
  473. swap( tmp.i1[0], tmp.i1[7] );
  474. swap( tmp.i1[1], tmp.i1[6] );
  475. swap( tmp.i1[2], tmp.i1[5] );
  476. swap( tmp.i1[3], tmp.i1[4] );
  477. *a = tmp.i8;
  478. return *a;
  479. }
  480. #endif /* HAVE_MATIO_UINT64_T */
  481. /** @brief swap the bytes of a 32-bit signed integer
  482. * @ingroup mat_internal
  483. * @param a pointer to integer to swap
  484. * @return the swapped integer
  485. */
  486. static mat_int32_t
  487. Mat_int32Swap( mat_int32_t *a )
  488. {
  489. union {
  490. mat_int8_t i1[4];
  491. mat_int32_t i4;
  492. } tmp;
  493. tmp.i4 = *a;
  494. swap( tmp.i1[0], tmp.i1[3] );
  495. swap( tmp.i1[1], tmp.i1[2] );
  496. *a = tmp.i4;
  497. return *a;
  498. }
  499. /** @brief swap the bytes of a 32-bit unsigned integer
  500. * @ingroup mat_internal
  501. * @param a pointer to integer to swap
  502. * @return the swapped integer
  503. */
  504. static mat_uint32_t
  505. Mat_uint32Swap( mat_uint32_t *a )
  506. {
  507. union {
  508. mat_uint8_t i1[4];
  509. mat_uint32_t i4;
  510. } tmp;
  511. tmp.i4 = *a;
  512. swap( tmp.i1[0], tmp.i1[3] );
  513. swap( tmp.i1[1], tmp.i1[2] );
  514. *a = tmp.i4;
  515. return *a;
  516. }
  517. /** @brief swap the bytes of a 16-bit signed integer
  518. * @ingroup mat_internal
  519. * @param a pointer to integer to swap
  520. * @return the swapped integer
  521. */
  522. static mat_int16_t
  523. Mat_int16Swap( mat_int16_t *a )
  524. {
  525. union {
  526. mat_int8_t i1[2];
  527. mat_int16_t i2;
  528. } tmp;
  529. tmp.i2 = *a;
  530. swap( tmp.i1[0], tmp.i1[1] );
  531. *a = tmp.i2;
  532. return *a;
  533. }
  534. /** @brief swap the bytes of a 16-bit unsigned integer
  535. * @ingroup mat_internal
  536. * @param a pointer to integer to swap
  537. * @return the swapped integer
  538. */
  539. static mat_uint16_t
  540. Mat_uint16Swap( mat_uint16_t *a )
  541. {
  542. union {
  543. mat_uint8_t i1[2];
  544. mat_uint16_t i2;
  545. } tmp;
  546. tmp.i2 = *a;
  547. swap( tmp.i1[0], tmp.i1[1] );
  548. *a = tmp.i2;
  549. return *a;
  550. }
  551. /** @brief swap the bytes of a 4 byte single-precision float
  552. * @ingroup mat_internal
  553. * @param a pointer to integer to swap
  554. * @return the swapped integer
  555. */
  556. static float
  557. Mat_floatSwap( float *a )
  558. {
  559. union {
  560. char i1[4];
  561. float r4;
  562. } tmp;
  563. tmp.r4 = *a;
  564. swap( tmp.i1[0], tmp.i1[3] );
  565. swap( tmp.i1[1], tmp.i1[2] );
  566. *a = tmp.r4;
  567. return *a;
  568. }
  569. /** @brief swap the bytes of a 4 or 8 byte double-precision float
  570. * @ingroup mat_internal
  571. * @param a pointer to integer to swap
  572. * @return the swapped integer
  573. */
  574. static double
  575. Mat_doubleSwap( double *a )
  576. {
  577. #ifndef SIZEOF_DOUBLE
  578. #define SIZEOF_DOUBLE 8
  579. #endif
  580. union {
  581. char a[SIZEOF_DOUBLE];
  582. double b;
  583. } tmp;
  584. tmp.b = *a;
  585. #if SIZEOF_DOUBLE == 4
  586. swap( tmp.a[0], tmp.a[3] );
  587. swap( tmp.a[1], tmp.a[2] );
  588. #elif SIZEOF_DOUBLE == 8
  589. swap( tmp.a[0], tmp.a[7] );
  590. swap( tmp.a[1], tmp.a[6] );
  591. swap( tmp.a[2], tmp.a[5] );
  592. swap( tmp.a[3], tmp.a[4] );
  593. #elif SIZEOF_DOUBLE == 16
  594. swap( tmp.a[0], tmp.a[15] );
  595. swap( tmp.a[1], tmp.a[14] );
  596. swap( tmp.a[2], tmp.a[13] );
  597. swap( tmp.a[3], tmp.a[12] );
  598. swap( tmp.a[4], tmp.a[11] );
  599. swap( tmp.a[5], tmp.a[10] );
  600. swap( tmp.a[6], tmp.a[9] );
  601. swap( tmp.a[7], tmp.a[8] );
  602. #endif
  603. *a = tmp.b;
  604. return *a;
  605. }
  606. /* -------------------------------
  607. * ---------- inflate.c
  608. * -------------------------------
  609. */
  610. /** @file inflate.c
  611. * @brief Functions to inflate data/tags
  612. * @ingroup MAT
  613. */
  614. #include <stdlib.h>
  615. #if HAVE_ZLIB
  616. /** @cond mat_devman */
  617. /** @brief Inflate the data until @c nbytes of uncompressed data has been
  618. * inflated
  619. *
  620. * @ingroup mat_internal
  621. * @param mat Pointer to the MAT file
  622. * @param z zlib compression stream
  623. * @param nbytes Number of uncompressed bytes to skip
  624. * @return Number of bytes read from the file
  625. */
  626. static size_t
  627. InflateSkip(mat_t *mat, z_streamp z, int nbytes)
  628. {
  629. mat_uint8_t comp_buf[512],uncomp_buf[512];
  630. int n, err, cnt = 0;
  631. size_t bytesread = 0;
  632. if ( nbytes < 1 )
  633. return 0;
  634. n = (nbytes<512) ? nbytes : 512;
  635. if ( !z->avail_in ) {
  636. z->next_in = comp_buf;
  637. z->avail_in += fread(comp_buf,1,n,(FILE*)mat->fp);
  638. bytesread += z->avail_in;
  639. }
  640. z->avail_out = n;
  641. z->next_out = uncomp_buf;
  642. err = inflate(z,Z_FULL_FLUSH);
  643. if ( err == Z_STREAM_END ) {
  644. return bytesread;
  645. } else if ( err != Z_OK ) {
  646. Mat_Critical("InflateSkip: inflate returned %s",zError(err == Z_NEED_DICT ? Z_DATA_ERROR : err));
  647. return bytesread;
  648. }
  649. if ( !z->avail_out ) {
  650. cnt += n;
  651. n = ((nbytes-cnt)<512) ? nbytes-cnt : 512;
  652. z->avail_out = n;
  653. z->next_out = uncomp_buf;
  654. }
  655. while ( cnt < nbytes ) {
  656. if ( !z->avail_in ) {
  657. z->next_in = comp_buf;
  658. z->avail_in += fread(comp_buf,1,n,(FILE*)mat->fp);
  659. bytesread += z->avail_in;
  660. }
  661. err = inflate(z,Z_FULL_FLUSH);
  662. if ( err == Z_STREAM_END ) {
  663. break;
  664. } else if ( err != Z_OK ) {
  665. Mat_Critical("InflateSkip: inflate returned %s",zError(err == Z_NEED_DICT ? Z_DATA_ERROR : err));
  666. break;
  667. }
  668. if ( !z->avail_out ) {
  669. cnt += n;
  670. n = ((nbytes-cnt)<512) ? nbytes-cnt : 512;
  671. z->avail_out = n;
  672. z->next_out = uncomp_buf;
  673. }
  674. }
  675. if ( z->avail_in ) {
  676. int offset = -(long)z->avail_in;
  677. (void)fseek((FILE*)mat->fp,offset,SEEK_CUR);
  678. bytesread -= z->avail_in;
  679. z->avail_in = 0;
  680. }
  681. return bytesread;
  682. }
  683. /** @brief Inflate the data until @c nbytes of compressed data has been
  684. * inflated
  685. *
  686. * @ingroup mat_internal
  687. * @param mat Pointer to the MAT file
  688. * @param z zlib compression stream
  689. * @param nbytes Number of uncompressed bytes to skip
  690. * @return Number of bytes read from the file
  691. */
  692. static size_t
  693. InflateSkip2(mat_t *mat, matvar_t *matvar, int nbytes)
  694. {
  695. mat_uint8_t comp_buf[32],uncomp_buf[32];
  696. int err, cnt = 0;
  697. size_t bytesread = 0;
  698. if ( !matvar->internal->z->avail_in ) {
  699. matvar->internal->z->avail_in = 1;
  700. matvar->internal->z->next_in = comp_buf;
  701. bytesread += fread(comp_buf,1,1,(FILE*)mat->fp);
  702. }
  703. matvar->internal->z->avail_out = 1;
  704. matvar->internal->z->next_out = uncomp_buf;
  705. err = inflate(matvar->internal->z,Z_NO_FLUSH);
  706. if ( err != Z_OK ) {
  707. Mat_Critical("InflateSkip2: %s - inflate returned %s",matvar->name,zError(err == Z_NEED_DICT ? Z_DATA_ERROR : err));
  708. return bytesread;
  709. }
  710. if ( !matvar->internal->z->avail_out ) {
  711. matvar->internal->z->avail_out = 1;
  712. matvar->internal->z->next_out = uncomp_buf;
  713. }
  714. while ( cnt < nbytes ) {
  715. if ( !matvar->internal->z->avail_in ) {
  716. matvar->internal->z->avail_in = 1;
  717. matvar->internal->z->next_in = comp_buf;
  718. bytesread += fread(comp_buf,1,1,(FILE*)mat->fp);
  719. cnt++;
  720. }
  721. err = inflate(matvar->internal->z,Z_NO_FLUSH);
  722. if ( err != Z_OK ) {
  723. Mat_Critical("InflateSkip2: %s - inflate returned %s",matvar->name,zError(err == Z_NEED_DICT ? Z_DATA_ERROR : err));
  724. return bytesread;
  725. }
  726. if ( !matvar->internal->z->avail_out ) {
  727. matvar->internal->z->avail_out = 1;
  728. matvar->internal->z->next_out = uncomp_buf;
  729. }
  730. }
  731. if ( matvar->internal->z->avail_in ) {
  732. (void)fseek((FILE*)mat->fp,-(int)matvar->internal->z->avail_in,SEEK_CUR);
  733. bytesread -= matvar->internal->z->avail_in;
  734. matvar->internal->z->avail_in = 0;
  735. }
  736. return bytesread;
  737. }
  738. /** @brief Inflate the data until @c len elements of compressed data with data
  739. * type @c data_type has been inflated
  740. *
  741. * @ingroup mat_internal
  742. * @param mat Pointer to the MAT file
  743. * @param z zlib compression stream
  744. * @param data_type Data type (matio_types enumerations)
  745. * @param len Number of elements of datatype @c data_type to skip
  746. * @return Number of bytes read from the file
  747. */
  748. static size_t
  749. InflateSkipData(mat_t *mat,z_streamp z,enum matio_types data_type,int len)
  750. {
  751. int data_size = 0;
  752. if ( (mat == NULL) || (z == NULL) )
  753. return 0;
  754. else if ( len < 1 )
  755. return 0;
  756. switch ( data_type ) {
  757. case MAT_T_DOUBLE:
  758. data_size = sizeof(double);
  759. break;
  760. case MAT_T_SINGLE:
  761. data_size = sizeof(float);
  762. break;
  763. #ifdef HAVE_MATIO_INT64_T
  764. case MAT_T_INT64:
  765. data_size = sizeof(mat_int64_t);
  766. break;
  767. #endif /* HAVE_MATIO_INT64_T */
  768. #ifdef HAVE_MATIO_UINT64_T
  769. case MAT_T_UINT64:
  770. data_size = sizeof(mat_uint64_t);
  771. break;
  772. #endif /* HAVE_MATIO_UINT64_T */
  773. case MAT_T_INT32:
  774. data_size = sizeof(mat_int32_t);
  775. break;
  776. case MAT_T_UINT32:
  777. data_size = sizeof(mat_uint32_t);
  778. break;
  779. case MAT_T_INT16:
  780. data_size = sizeof(mat_int16_t);
  781. break;
  782. case MAT_T_UINT16:
  783. data_size = sizeof(mat_uint16_t);
  784. break;
  785. case MAT_T_UINT8:
  786. data_size = sizeof(mat_uint8_t);
  787. break;
  788. case MAT_T_INT8:
  789. data_size = sizeof(mat_int8_t);
  790. break;
  791. default:
  792. return 0;
  793. }
  794. InflateSkip(mat,z,len*data_size);
  795. return len;
  796. }
  797. /** @brief Inflates the variable's tag.
  798. *
  799. * @c buf must hold at least 8 bytes
  800. * @ingroup mat_internal
  801. * @param mat Pointer to the MAT file
  802. * @param matvar Pointer to the MAT variable
  803. * @param buf Pointer to store the 8-byte variable tag
  804. * @return Number of bytes read from the file
  805. */
  806. static size_t
  807. InflateVarTag(mat_t *mat, matvar_t *matvar, void *buf)
  808. {
  809. mat_uint8_t comp_buf[32];
  810. int err;
  811. size_t bytesread = 0;
  812. if (buf == NULL)
  813. return 0;
  814. if ( !matvar->internal->z->avail_in ) {
  815. matvar->internal->z->avail_in = 1;
  816. matvar->internal->z->next_in = comp_buf;
  817. bytesread += fread(comp_buf,1,1,(FILE*)mat->fp);
  818. }
  819. matvar->internal->z->avail_out = 8;
  820. matvar->internal->z->next_out = (Bytef*)buf;
  821. err = inflate(matvar->internal->z,Z_NO_FLUSH);
  822. if ( err != Z_OK ) {
  823. Mat_Critical("InflateVarTag: inflate returned %s",zError(err == Z_NEED_DICT ? Z_DATA_ERROR : err));
  824. return bytesread;
  825. }
  826. while ( matvar->internal->z->avail_out && !matvar->internal->z->avail_in ) {
  827. matvar->internal->z->avail_in = 1;
  828. matvar->internal->z->next_in = comp_buf;
  829. bytesread += fread(comp_buf,1,1,(FILE*)mat->fp);
  830. err = inflate(matvar->internal->z,Z_NO_FLUSH);
  831. if ( err != Z_OK ) {
  832. Mat_Critical("InflateVarTag: inflate returned %s",zError(err == Z_NEED_DICT ? Z_DATA_ERROR : err));
  833. return bytesread;
  834. }
  835. }
  836. if ( matvar->internal->z->avail_in ) {
  837. (void)fseek((FILE*)mat->fp,-(int)matvar->internal->z->avail_in,SEEK_CUR);
  838. bytesread -= matvar->internal->z->avail_in;
  839. matvar->internal->z->avail_in = 0;
  840. }
  841. return bytesread;
  842. }
  843. /** @brief Inflates the Array Flags Tag and the Array Flags data.
  844. *
  845. * @c buf must hold at least 16 bytes
  846. * @ingroup mat_internal
  847. * @param mat Pointer to the MAT file
  848. * @param matvar Pointer to the MAT variable
  849. * @param buf Pointer to store the 16-byte array flags tag and data
  850. * @return Number of bytes read from the file
  851. */
  852. static size_t
  853. InflateArrayFlags(mat_t *mat, matvar_t *matvar, void *buf)
  854. {
  855. mat_uint8_t comp_buf[32];
  856. int err;
  857. size_t bytesread = 0;
  858. if (buf == NULL) return 0;
  859. if ( !matvar->internal->z->avail_in ) {
  860. matvar->internal->z->avail_in = 1;
  861. matvar->internal->z->next_in = comp_buf;
  862. bytesread += fread(comp_buf,1,1,(FILE*)mat->fp);
  863. }
  864. matvar->internal->z->avail_out = 16;
  865. matvar->internal->z->next_out = (Bytef*)buf;
  866. err = inflate(matvar->internal->z,Z_NO_FLUSH);
  867. if ( err != Z_OK ) {
  868. Mat_Critical("InflateArrayFlags: inflate returned %s",zError(err == Z_NEED_DICT ? Z_DATA_ERROR : err));
  869. return bytesread;
  870. }
  871. while ( matvar->internal->z->avail_out && !matvar->internal->z->avail_in ) {
  872. matvar->internal->z->avail_in = 1;
  873. matvar->internal->z->next_in = comp_buf;
  874. bytesread += fread(comp_buf,1,1,(FILE*)mat->fp);
  875. err = inflate(matvar->internal->z,Z_NO_FLUSH);
  876. if ( err != Z_OK ) {
  877. Mat_Critical("InflateArrayFlags: inflate returned %s",zError(err == Z_NEED_DICT ? Z_DATA_ERROR : err));
  878. return bytesread;
  879. }
  880. }
  881. if ( matvar->internal->z->avail_in ) {
  882. (void)fseek((FILE*)mat->fp,-(int)matvar->internal->z->avail_in,SEEK_CUR);
  883. bytesread -= matvar->internal->z->avail_in;
  884. matvar->internal->z->avail_in = 0;
  885. }
  886. return bytesread;
  887. }
  888. /** @brief Inflates the dimensions tag and the dimensions data
  889. *
  890. * @c buf must hold at least (8+4*rank) bytes where rank is the number of
  891. * dimensions. If the end of the dimensions data is not aligned on an 8-byte
  892. * boundary, this function eats up those bytes and stores then in @c buf.
  893. * @ingroup mat_internal
  894. * @param mat Pointer to the MAT file
  895. * @param matvar Pointer to the MAT variable
  896. * @param buf Pointer to store the dimensions flag and data
  897. * @return Number of bytes read from the file
  898. */
  899. static size_t
  900. InflateDimensions(mat_t *mat, matvar_t *matvar, void *buf)
  901. {
  902. mat_uint8_t comp_buf[32];
  903. mat_int32_t tag[2];
  904. int err, rank, i;
  905. size_t bytesread = 0;
  906. if ( buf == NULL )
  907. return 0;
  908. if ( !matvar->internal->z->avail_in ) {
  909. matvar->internal->z->avail_in = 1;
  910. matvar->internal->z->next_in = comp_buf;
  911. bytesread += fread(comp_buf,1,1,(FILE*)mat->fp);
  912. }
  913. matvar->internal->z->avail_out = 8;
  914. matvar->internal->z->next_out = (Bytef*)buf;
  915. err = inflate(matvar->internal->z,Z_NO_FLUSH);
  916. if ( err != Z_OK ) {
  917. Mat_Critical("InflateDimensions: inflate returned %s",zError(err == Z_NEED_DICT ? Z_DATA_ERROR : err));
  918. return bytesread;
  919. }
  920. while ( matvar->internal->z->avail_out && !matvar->internal->z->avail_in ) {
  921. matvar->internal->z->avail_in = 1;
  922. matvar->internal->z->next_in = comp_buf;
  923. bytesread += fread(comp_buf,1,1,(FILE*)mat->fp);
  924. err = inflate(matvar->internal->z,Z_NO_FLUSH);
  925. if ( err != Z_OK ) {
  926. Mat_Critical("InflateDimensions: inflate returned %s",zError(err == Z_NEED_DICT ? Z_DATA_ERROR : err));
  927. return bytesread;
  928. }
  929. }
  930. tag[0] = *(int *)buf;
  931. tag[1] = *((int *)buf+1);
  932. if ( mat->byteswap ) {
  933. Mat_int32Swap(tag);
  934. Mat_int32Swap(tag+1);
  935. }
  936. if ( (tag[0] & 0x0000ffff) != MAT_T_INT32 ) {
  937. Mat_Critical("InflateDimensions: Reading dimensions expected type MAT_T_INT32");
  938. return bytesread;
  939. }
  940. rank = tag[1];
  941. if ( rank % 8 != 0 )
  942. i = 8-(rank %8);
  943. else
  944. i = 0;
  945. rank+=i;
  946. if ( !matvar->internal->z->avail_in ) {
  947. matvar->internal->z->avail_in = 1;
  948. matvar->internal->z->next_in = comp_buf;
  949. bytesread += fread(comp_buf,1,1,(FILE*)mat->fp);
  950. }
  951. matvar->internal->z->avail_out = rank;
  952. matvar->internal->z->next_out = (Bytef*)((mat_int32_t *)buf+2);
  953. err = inflate(matvar->internal->z,Z_NO_FLUSH);
  954. if ( err != Z_OK ) {
  955. Mat_Critical("InflateDimensions: inflate returned %s",zError(err == Z_NEED_DICT ? Z_DATA_ERROR : err));
  956. return bytesread;
  957. }
  958. while ( matvar->internal->z->avail_out && !matvar->internal->z->avail_in ) {
  959. matvar->internal->z->avail_in = 1;
  960. matvar->internal->z->next_in = comp_buf;
  961. bytesread += fread(comp_buf,1,1,(FILE*)mat->fp);
  962. err = inflate(matvar->internal->z,Z_NO_FLUSH);
  963. if ( err != Z_OK ) {
  964. Mat_Critical("InflateDimensions: inflate returned %s",zError(err == Z_NEED_DICT ? Z_DATA_ERROR : err));
  965. return bytesread;
  966. }
  967. }
  968. if ( matvar->internal->z->avail_in ) {
  969. (void)fseek((FILE*)mat->fp,-(int)matvar->internal->z->avail_in,SEEK_CUR);
  970. bytesread -= matvar->internal->z->avail_in;
  971. matvar->internal->z->avail_in = 0;
  972. }
  973. return bytesread;
  974. }
  975. /** @brief Inflates the variable name tag
  976. *
  977. * @ingroup mat_internal
  978. * @param mat Pointer to the MAT file
  979. * @param matvar Pointer to the MAT variable
  980. * @param buf Pointer to store the variables name tag
  981. * @return Number of bytes read from the file
  982. */
  983. static size_t
  984. InflateVarNameTag(mat_t *mat, matvar_t *matvar, void *buf)
  985. {
  986. mat_uint8_t comp_buf[32];
  987. int err;
  988. size_t bytesread = 0;
  989. if ( buf == NULL )
  990. return 0;
  991. if ( !matvar->internal->z->avail_in ) {
  992. matvar->internal->z->avail_in = 1;
  993. matvar->internal->z->next_in = comp_buf;
  994. bytesread += fread(comp_buf,1,1,(FILE*)mat->fp);
  995. }
  996. matvar->internal->z->avail_out = 8;
  997. matvar->internal->z->next_out = (Bytef*)buf;
  998. err = inflate(matvar->internal->z,Z_NO_FLUSH);
  999. if ( err != Z_OK ) {
  1000. Mat_Critical("InflateVarNameTag: inflate returned %s",zError(err == Z_NEED_DICT ? Z_DATA_ERROR : err));
  1001. return bytesread;
  1002. }
  1003. while ( matvar->internal->z->avail_out && !matvar->internal->z->avail_in ) {
  1004. matvar->internal->z->avail_in = 1;
  1005. matvar->internal->z->next_in = comp_buf;
  1006. bytesread += fread(comp_buf,1,1,(FILE*)mat->fp);
  1007. err = inflate(matvar->internal->z,Z_NO_FLUSH);
  1008. if ( err != Z_OK ) {
  1009. Mat_Critical("InflateVarNameTag: inflate returned %s",zError(err == Z_NEED_DICT ? Z_DATA_ERROR : err));
  1010. return bytesread;
  1011. }
  1012. }
  1013. if ( matvar->internal->z->avail_in ) {
  1014. (void)fseek((FILE*)mat->fp,-(int)matvar->internal->z->avail_in,SEEK_CUR);
  1015. bytesread -= matvar->internal->z->avail_in;
  1016. matvar->internal->z->avail_in = 0;
  1017. }
  1018. return bytesread;
  1019. }
  1020. /** @brief Inflates the variable name
  1021. *
  1022. * @ingroup mat_internal
  1023. * @param mat Pointer to the MAT file
  1024. * @param matvar Pointer to the MAT variable
  1025. * @param buf Pointer to store the variables name
  1026. * @param N Number of characters in the name
  1027. * @return Number of bytes read from the file
  1028. */
  1029. static size_t
  1030. InflateVarName(mat_t *mat, matvar_t *matvar, void *buf, int N)
  1031. {
  1032. mat_uint8_t comp_buf[32];
  1033. int err;
  1034. size_t bytesread = 0;
  1035. if ( buf == NULL )
  1036. return 0;
  1037. if ( !matvar->internal->z->avail_in ) {
  1038. matvar->internal->z->avail_in = 1;
  1039. matvar->internal->z->next_in = comp_buf;
  1040. bytesread += fread(comp_buf,1,1,(FILE*)mat->fp);
  1041. }
  1042. matvar->internal->z->avail_out = N;
  1043. matvar->internal->z->next_out = (Bytef*)buf;
  1044. err = inflate(matvar->internal->z,Z_NO_FLUSH);
  1045. if ( err != Z_OK ) {
  1046. Mat_Critical("InflateVarName: inflate returned %s",zError(err == Z_NEED_DICT ? Z_DATA_ERROR : err));
  1047. return bytesread;
  1048. }
  1049. while ( matvar->internal->z->avail_out && !matvar->internal->z->avail_in ) {
  1050. matvar->internal->z->avail_in = 1;
  1051. matvar->internal->z->next_in = comp_buf;
  1052. bytesread += fread(comp_buf,1,1,(FILE*)mat->fp);
  1053. err = inflate(matvar->internal->z,Z_NO_FLUSH);
  1054. if ( err != Z_OK ) {
  1055. Mat_Critical("InflateVarName: inflate returned %s",zError(err == Z_NEED_DICT ? Z_DATA_ERROR : err));
  1056. return bytesread;
  1057. }
  1058. }
  1059. if ( matvar->internal->z->avail_in ) {
  1060. (void)fseek((FILE*)mat->fp,-(int)matvar->internal->z->avail_in,SEEK_CUR);
  1061. bytesread -= matvar->internal->z->avail_in;
  1062. matvar->internal->z->avail_in = 0;
  1063. }
  1064. return bytesread;
  1065. }
  1066. /** @brief Inflates the data's tag
  1067. *
  1068. * buf must hold at least 8 bytes
  1069. * @ingroup mat_internal
  1070. * @param mat Pointer to the MAT file
  1071. * @param matvar Pointer to the MAT variable
  1072. * @param buf Pointer to store the data tag
  1073. * @return Number of bytes read from the file
  1074. */
  1075. static size_t
  1076. InflateDataTag(mat_t *mat, matvar_t *matvar, void *buf)
  1077. {
  1078. mat_uint8_t comp_buf[32];
  1079. int err;
  1080. size_t bytesread = 0;
  1081. if ( buf == NULL )
  1082. return 0;
  1083. if ( !matvar->internal->z->avail_in ) {
  1084. matvar->internal->z->avail_in = 1;
  1085. matvar->internal->z->next_in = comp_buf;
  1086. bytesread += fread(comp_buf,1,1,(FILE*)mat->fp);
  1087. }
  1088. matvar->internal->z->avail_out = 8;
  1089. matvar->internal->z->next_out = (Bytef*)buf;
  1090. err = inflate(matvar->internal->z,Z_NO_FLUSH);
  1091. if ( err == Z_STREAM_END ) {
  1092. return bytesread;
  1093. } else if ( err != Z_OK ) {
  1094. Mat_Critical("InflateDataTag: %s - inflate returned %s",matvar->name,zError(err == Z_NEED_DICT ? Z_DATA_ERROR : err));
  1095. return bytesread;
  1096. }
  1097. while ( matvar->internal->z->avail_out && !matvar->internal->z->avail_in ) {
  1098. matvar->internal->z->avail_in = 1;
  1099. matvar->internal->z->next_in = comp_buf;
  1100. bytesread += fread(comp_buf,1,1,(FILE*)mat->fp);
  1101. err = inflate(matvar->internal->z,Z_NO_FLUSH);
  1102. if ( err == Z_STREAM_END ) {
  1103. break;
  1104. } else if ( err != Z_OK ) {
  1105. Mat_Critical("InflateDataTag: %s - inflate returned %s",matvar->name,zError(err == Z_NEED_DICT ? Z_DATA_ERROR : err));
  1106. return bytesread;
  1107. }
  1108. }
  1109. if ( matvar->internal->z->avail_in ) {
  1110. (void)fseek((FILE*)mat->fp,-(int)matvar->internal->z->avail_in,SEEK_CUR);
  1111. bytesread -= matvar->internal->z->avail_in;
  1112. matvar->internal->z->avail_in = 0;
  1113. }
  1114. return bytesread;
  1115. }
  1116. /** @brief Inflates the data's type
  1117. *
  1118. * buf must hold at least 4 bytes
  1119. * @ingroup mat_internal
  1120. * @param mat Pointer to the MAT file
  1121. * @param matvar Pointer to the MAT variable
  1122. * @param buf Pointer to store the data type
  1123. * @return Number of bytes read from the file
  1124. */
  1125. static size_t
  1126. InflateDataType(mat_t *mat, z_streamp z, void *buf)
  1127. {
  1128. mat_uint8_t comp_buf[32];
  1129. int err;
  1130. size_t bytesread = 0;
  1131. if ( buf == NULL )
  1132. return 0;
  1133. if ( !z->avail_in ) {
  1134. z->avail_in = 1;
  1135. z->next_in = comp_buf;
  1136. bytesread += fread(comp_buf,1,1,(FILE*)mat->fp);
  1137. }
  1138. z->avail_out = 4;
  1139. z->next_out = (Bytef*)buf;
  1140. err = inflate(z,Z_NO_FLUSH);
  1141. if ( err != Z_OK ) {
  1142. Mat_Critical("InflateDataType: inflate returned %s",zError(err == Z_NEED_DICT ? Z_DATA_ERROR : err));
  1143. return bytesread;
  1144. }
  1145. while ( z->avail_out && !z->avail_in ) {
  1146. z->avail_in = 1;
  1147. z->next_in = comp_buf;
  1148. bytesread += fread(comp_buf,1,1,(FILE*)mat->fp);
  1149. err = inflate(z,Z_NO_FLUSH);
  1150. if ( err != Z_OK ) {
  1151. Mat_Critical("InflateDataType: inflate returned %s",zError(err == Z_NEED_DICT ? Z_DATA_ERROR : err));
  1152. return bytesread;
  1153. }
  1154. }
  1155. if ( z->avail_in ) {
  1156. (void)fseek((FILE*)mat->fp,-(int)z->avail_in,SEEK_CUR);
  1157. bytesread -= z->avail_in;
  1158. z->avail_in = 0;
  1159. }
  1160. return bytesread;
  1161. }
  1162. /** @brief Inflates the data
  1163. *
  1164. * buf must hold at least @c nBytes bytes
  1165. * @ingroup mat_internal
  1166. * @param mat Pointer to the MAT file
  1167. * @param z zlib compression stream
  1168. * @param buf Pointer to store the data type
  1169. * @param nBytes Number of bytes to inflate
  1170. * @return Number of bytes read from the file
  1171. */
  1172. static size_t
  1173. InflateData(mat_t *mat, z_streamp z, void *buf, int nBytes)
  1174. {
  1175. mat_uint8_t comp_buf[1024];
  1176. int err;
  1177. size_t bytesread = 0;
  1178. if ( buf == NULL )
  1179. return 0;
  1180. if ( nBytes < 1 ) {
  1181. return bytesread;
  1182. }
  1183. if ( !z->avail_in ) {
  1184. if ( nBytes > 1024 ) {
  1185. z->avail_in = fread(comp_buf,1,1024,(FILE*)mat->fp);
  1186. } else {
  1187. z->avail_in = fread(comp_buf,1,nBytes,(FILE*)mat->fp);
  1188. }
  1189. bytesread += z->avail_in;
  1190. z->next_in = comp_buf;
  1191. }
  1192. z->avail_out = nBytes;
  1193. z->next_out = (Bytef*)buf;
  1194. err = inflate(z,Z_FULL_FLUSH);
  1195. if ( err == Z_STREAM_END ) {
  1196. return bytesread;
  1197. } else if ( err != Z_OK ) {
  1198. Mat_Critical("InflateData: inflate returned %s",zError( err == Z_NEED_DICT ? Z_DATA_ERROR : err ));
  1199. return bytesread;
  1200. }
  1201. while ( z->avail_out && !z->avail_in ) {
  1202. if ( nBytes > 1024 + bytesread ) {
  1203. z->avail_in = fread(comp_buf,1,1024,(FILE*)mat->fp);
  1204. } else if ( nBytes < 1 + bytesread ) { /* Read a byte at a time */
  1205. z->avail_in = fread(comp_buf,1,1,(FILE*)mat->fp);
  1206. } else {
  1207. z->avail_in = fread(comp_buf,1,nBytes-bytesread,(FILE*)mat->fp);
  1208. }
  1209. bytesread += z->avail_in;
  1210. z->next_in = comp_buf;
  1211. err = inflate(z,Z_FULL_FLUSH);
  1212. if ( err == Z_STREAM_END ) {
  1213. break;
  1214. } else if ( err != Z_OK && err != Z_BUF_ERROR ) {
  1215. Mat_Critical("InflateData: inflate returned %s",zError(err == Z_NEED_DICT ? Z_DATA_ERROR : err));
  1216. break;
  1217. }
  1218. }
  1219. if ( z->avail_in ) {
  1220. int offset = -(long)z->avail_in;
  1221. (void)fseek((FILE*)mat->fp,offset,SEEK_CUR);
  1222. bytesread -= z->avail_in;
  1223. z->avail_in = 0;
  1224. }
  1225. return bytesread;
  1226. }
  1227. /** @brief Inflates the structure's fieldname length
  1228. *
  1229. * buf must hold at least 8 bytes
  1230. * @ingroup mat_internal
  1231. * @param mat Pointer to the MAT file
  1232. * @param matvar Pointer to the MAT variable
  1233. * @param buf Pointer to store the fieldname length
  1234. * @return Number of bytes read from the file
  1235. */
  1236. static size_t
  1237. InflateFieldNameLength(mat_t *mat, matvar_t *matvar, void *buf)
  1238. {
  1239. mat_uint8_t comp_buf[32];
  1240. int err;
  1241. size_t bytesread = 0;
  1242. if ( buf == NULL )
  1243. return 0;
  1244. if ( !matvar->internal->z->avail_in ) {
  1245. matvar->internal->z->avail_in = 1;
  1246. matvar->internal->z->next_in = comp_buf;
  1247. bytesread += fread(comp_buf,1,1,(FILE*)mat->fp);
  1248. }
  1249. matvar->internal->z->avail_out = 8;
  1250. matvar->internal->z->next_out = (Bytef*)buf;
  1251. err = inflate(matvar->internal->z,Z_NO_FLUSH);
  1252. if ( err != Z_OK ) {
  1253. Mat_Critical("InflateFieldNameLength: inflate returned %s",zError(err == Z_NEED_DICT ? Z_DATA_ERROR : err));
  1254. return bytesread;
  1255. }
  1256. while ( matvar->internal->z->avail_out && !matvar->internal->z->avail_in ) {
  1257. matvar->internal->z->avail_in = 1;
  1258. matvar->internal->z->next_in = comp_buf;
  1259. bytesread += fread(comp_buf,1,1,(FILE*)mat->fp);
  1260. err = inflate(matvar->internal->z,Z_NO_FLUSH);
  1261. if ( err != Z_OK ) {
  1262. Mat_Critical("InflateFieldNameLength: inflate returned %s",zError(err == Z_NEED_DICT ? Z_DATA_ERROR : err));
  1263. return bytesread;
  1264. }
  1265. }
  1266. if ( matvar->internal->z->avail_in ) {
  1267. (void)fseek((FILE*)mat->fp,-(int)matvar->internal->z->avail_in,SEEK_CUR);
  1268. bytesread -= matvar->internal->z->avail_in;
  1269. matvar->internal->z->avail_in = 0;
  1270. }
  1271. return bytesread;
  1272. }
  1273. /** @brief Inflates the structure's fieldname tag
  1274. *
  1275. * buf must hold at least 8 bytes
  1276. * @ingroup mat_internal
  1277. * @param mat Pointer to the MAT file
  1278. * @param matvar Pointer to the MAT variable
  1279. * @param buf Pointer to store the fieldname tag
  1280. * @return Number of bytes read from the file
  1281. */
  1282. static size_t
  1283. InflateFieldNamesTag(mat_t *mat, matvar_t *matvar, void *buf)
  1284. {
  1285. mat_uint8_t comp_buf[32];
  1286. int err;
  1287. size_t bytesread = 0;
  1288. if ( buf == NULL )
  1289. return 0;
  1290. if ( !matvar->internal->z->avail_in ) {
  1291. matvar->internal->z->avail_in = 1;
  1292. matvar->internal->z->next_in = comp_buf;
  1293. bytesread += fread(comp_buf,1,1,(FILE*)mat->fp);
  1294. }
  1295. matvar->internal->z->avail_out = 8;
  1296. matvar->internal->z->next_out = (Bytef*)buf;
  1297. err = inflate(matvar->internal->z,Z_NO_FLUSH);
  1298. if ( err != Z_OK ) {
  1299. Mat_Critical("InflateFieldNamesTag: inflate returned %s",zError(err == Z_NEED_DICT ? Z_DATA_ERROR : err));
  1300. return bytesread;
  1301. }
  1302. while ( matvar->internal->z->avail_out && !matvar->internal->z->avail_in ) {
  1303. matvar->internal->z->avail_in = 1;
  1304. matvar->internal->z->next_in = comp_buf;
  1305. bytesread += fread(comp_buf,1,1,(FILE*)mat->fp);
  1306. err = inflate(matvar->internal->z,Z_NO_FLUSH);
  1307. if ( err != Z_OK ) {
  1308. Mat_Critical("InflateFieldNamesTag: inflate returned %s",zError(err == Z_NEED_DICT ? Z_DATA_ERROR : err));
  1309. return bytesread;
  1310. }
  1311. }
  1312. if ( matvar->internal->z->avail_in ) {
  1313. (void)fseek((FILE*)mat->fp,-(int)matvar->internal->z->avail_in,SEEK_CUR);
  1314. bytesread -= matvar->internal->z->avail_in;
  1315. matvar->internal->z->avail_in = 0;
  1316. }
  1317. return bytesread;
  1318. }
  1319. /*
  1320. * Inflates the structure's fieldname length. buf must hold at least
  1321. * nfields*fieldname_length bytes
  1322. */
  1323. /** @brief Inflates the structure's fieldnames
  1324. *
  1325. * buf must hold at least @c nfields * @c fieldname_length bytes
  1326. * @ingroup mat_internal
  1327. * @param mat Pointer to the MAT file
  1328. * @param matvar Pointer to the MAT variable
  1329. * @param buf Pointer to store the fieldnames
  1330. * @param nfields Number of fields
  1331. * @param fieldname_length Maximum length in bytes of each field
  1332. * @param padding Number of padding bytes
  1333. * @return Number of bytes read from the file
  1334. */
  1335. static size_t
  1336. InflateFieldNames(mat_t *mat,matvar_t *matvar,void *buf,int nfields,
  1337. int fieldname_length,int padding)
  1338. {
  1339. mat_uint8_t comp_buf[32];
  1340. int err;
  1341. size_t bytesread = 0;
  1342. if ( buf == NULL )
  1343. return 0;
  1344. if ( !matvar->internal->z->avail_in ) {
  1345. matvar->internal->z->avail_in = 1;
  1346. matvar->internal->z->next_in = comp_buf;
  1347. bytesread += fread(comp_buf,1,1,(FILE*)mat->fp);
  1348. }
  1349. matvar->internal->z->avail_out = nfields*fieldname_length+padding;
  1350. matvar->internal->z->next_out = (Bytef*)buf;
  1351. err = inflate(matvar->internal->z,Z_NO_FLUSH);
  1352. if ( err != Z_OK ) {
  1353. Mat_Critical("InflateFieldNames: inflate returned %s",zError(err == Z_NEED_DICT ? Z_DATA_ERROR : err));
  1354. return bytesread;
  1355. }
  1356. while ( matvar->internal->z->avail_out && !matvar->internal->z->avail_in ) {
  1357. matvar->internal->z->avail_in = 1;
  1358. matvar->internal->z->next_in = comp_buf;
  1359. bytesread += fread(comp_buf,1,1,(FILE*)mat->fp);
  1360. err = inflate(matvar->internal->z,Z_NO_FLUSH);
  1361. if ( err != Z_OK ) {
  1362. Mat_Critical("InflateFieldNames: inflate returned %s",zError(err == Z_NEED_DICT ? Z_DATA_ERROR : err));
  1363. return bytesread;
  1364. }
  1365. }
  1366. if ( matvar->internal->z->avail_in ) {
  1367. (void)fseek((FILE*)mat->fp,-(int)matvar->internal->z->avail_in,SEEK_CUR);
  1368. bytesread -= matvar->internal->z->avail_in;
  1369. matvar->internal->z->avail_in = 0;
  1370. }
  1371. return bytesread;
  1372. }
  1373. /** @endcond */
  1374. #endif
  1375. /* -------------------------------
  1376. * ---------- io.c
  1377. * -------------------------------
  1378. */
  1379. /** @file io.c
  1380. * MAT File I/O Utility Functions
  1381. */
  1382. #include <stdlib.h>
  1383. #include <stdarg.h>
  1384. #include <stdio.h>
  1385. #include <string.h>
  1386. #if !defined(HAVE_VA_COPY) && !defined(va_copy) && defined(HAVE___VA_COPY)
  1387. # define va_copy(d,s) __va_copy(d,s)
  1388. #elif !defined(HAVE_VA_COPY) && !defined(va_copy)
  1389. # define va_copy(d,s) memcpy(&(d),&(s),sizeof(va_list))
  1390. #endif
  1391. /** @brief Allocates and prints to a new string
  1392. *
  1393. * @ingroup mat_util
  1394. * @param format format string
  1395. * @param ap variable argument list
  1396. * @return Newly allocated string with format printed to it
  1397. */
  1398. char *
  1399. strdup_vprintf(const char* format, va_list ap)
  1400. {
  1401. va_list ap2;
  1402. int size;
  1403. char* buffer;
  1404. va_copy(ap2, ap);
  1405. size = mat_vsnprintf(NULL, 0, format, ap2)+1;
  1406. va_end(ap2);
  1407. buffer = (char*)malloc(size+1);
  1408. if ( !buffer )
  1409. return NULL;
  1410. mat_vsnprintf(buffer, size, format, ap);
  1411. return buffer;
  1412. }
  1413. /** @brief Allocates and prints to a new string using printf format
  1414. *
  1415. * @ingroup mat_util
  1416. * @param format format string
  1417. * @return Pointer to resulting string, or NULL if there was an error
  1418. */
  1419. char *
  1420. strdup_printf(const char* format, ...)
  1421. {
  1422. char* buffer;
  1423. va_list ap;
  1424. va_start(ap, format);
  1425. buffer = strdup_vprintf(format, ap);
  1426. va_end(ap);
  1427. return buffer;
  1428. }
  1429. /** @brief Logs a Critical message and aborts the program
  1430. *
  1431. * Logs an Error message and aborts
  1432. * @ingroup mat_util
  1433. * @param format format string identical to printf format
  1434. * @param ... arguments to the format string
  1435. */
  1436. void Mat_Critical(const char *format, ... )
  1437. {
  1438. va_list ap;
  1439. va_start(ap, format);
  1440. ModelicaVFormatError(format, ap);
  1441. va_end(ap);
  1442. }
  1443. /** @brief Prints a warning message
  1444. *
  1445. * Logs a warning message then returns
  1446. * @ingroup mat_util
  1447. * @param format format string identical to printf format
  1448. * @param ... arguments to the format string
  1449. */
  1450. void
  1451. Mat_Warning(const char *format, ... )
  1452. {
  1453. va_list ap;
  1454. va_start(ap, format);
  1455. ModelicaVFormatMessage(format, ap);
  1456. va_end(ap);
  1457. }
  1458. /** @brief Calculate the size of MAT data types
  1459. *
  1460. * @ingroup mat_util
  1461. * @param data_type Data type enumeration
  1462. * @return size of the data type in bytes
  1463. */
  1464. size_t
  1465. Mat_SizeOf(enum matio_types data_type)
  1466. {
  1467. switch (data_type) {
  1468. case MAT_T_DOUBLE:
  1469. return sizeof(double);
  1470. case MAT_T_SINGLE:
  1471. return sizeof(float);
  1472. #ifdef HAVE_MATIO_INT64_T
  1473. case MAT_T_INT64:
  1474. return sizeof(mat_int64_t);
  1475. #endif
  1476. #ifdef HAVE_MATIO_UINT64_T
  1477. case MAT_T_UINT64:
  1478. return sizeof(mat_uint64_t);
  1479. #endif
  1480. case MAT_T_INT32:
  1481. return sizeof(mat_int32_t);
  1482. case MAT_T_UINT32:
  1483. return sizeof(mat_uint32_t);
  1484. case MAT_T_INT16:
  1485. return sizeof(mat_int16_t);
  1486. case MAT_T_UINT16:
  1487. return sizeof(mat_uint16_t);
  1488. case MAT_T_INT8:
  1489. return sizeof(mat_int8_t);
  1490. case MAT_T_UINT8:
  1491. return sizeof(mat_uint8_t);
  1492. case MAT_T_UTF8:
  1493. return 1;
  1494. case MAT_T_UTF16:
  1495. return 2;
  1496. case MAT_T_UTF32:
  1497. return 4;
  1498. default:
  1499. return 0;
  1500. }
  1501. }
  1502. /* -------------------------------
  1503. * ---------- read_data.c
  1504. * -------------------------------
  1505. */
  1506. /** @file read_data.c
  1507. * Matlab MAT version 5 file functions
  1508. * @ingroup MAT
  1509. */
  1510. /* FIXME: Implement Unicode support */
  1511. #include <stdlib.h>
  1512. #include <string.h>
  1513. #include <stdio.h>
  1514. #include <math.h>
  1515. #include <time.h>
  1516. /*
  1517. * --------------------------------------------------------------------------
  1518. * Routines to read data of any type into arrays of a specific type
  1519. * --------------------------------------------------------------------------
  1520. */
  1521. /** @cond mat_devman */
  1522. /** @brief Reads data of type @c data_type into a double type
  1523. *
  1524. * Reads from the MAT file @c len elements of data type @c data_type storing
  1525. * them as double's in @c data.
  1526. * @ingroup mat_internal
  1527. * @param mat MAT file pointer
  1528. * @param data Pointer to store the output double values (len*sizeof(double))
  1529. * @param data_type one of the @c matio_types enumerations which is the source
  1530. * data type in the file
  1531. * @param len Number of elements of type @c data_type to read from the file
  1532. * @retval Number of bytes read from the file
  1533. */
  1534. static int
  1535. ReadDoubleData(mat_t *mat,double *data,enum matio_types data_type,int len)
  1536. {
  1537. int bytesread = 0, data_size = 0, i;
  1538. if ( (mat == NULL) || (data == NULL) || (mat->fp == NULL) )
  1539. return 0;
  1540. switch ( data_type ) {
  1541. case MAT_T_DOUBLE:
  1542. {
  1543. data_size = sizeof(double);
  1544. if ( mat->byteswap ) {
  1545. bytesread += fread(data,data_size,len,(FILE*)mat->fp);
  1546. for ( i = 0; i < len; i++ ) {
  1547. (void)Mat_doubleSwap(data+i);
  1548. }
  1549. } else {
  1550. bytesread += fread(data,data_size,len,(FILE*)mat->fp);
  1551. }
  1552. break;
  1553. }
  1554. case MAT_T_SINGLE:
  1555. {
  1556. float f;
  1557. data_size = sizeof(float);
  1558. if ( mat->byteswap ) {
  1559. for ( i = 0; i < len; i++ ) {
  1560. bytesread += fread(&f,data_size,1,(FILE*)mat->fp);
  1561. data[i] = Mat_floatSwap(&f);
  1562. }
  1563. } else {
  1564. for ( i = 0; i < len; i++ ) {
  1565. bytesread += fread(&f,data_size,1,(FILE*)mat->fp);
  1566. data[i] = f;
  1567. }
  1568. }
  1569. break;
  1570. }
  1571. case MAT_T_INT32:
  1572. {
  1573. mat_int32_t i32;
  1574. data_size = sizeof(mat_int32_t);
  1575. if ( mat->byteswap ) {
  1576. for ( i = 0; i < len; i++ ) {
  1577. bytesread += fread(&i32,data_size,1,(FILE*)mat->fp);
  1578. data[i] = Mat_int32Swap(&i32);
  1579. }
  1580. } else {
  1581. for ( i = 0; i < len; i++ ) {
  1582. bytesread += fread(&i32,data_size,1,(FILE*)mat->fp);
  1583. data[i] = i32;
  1584. }
  1585. }
  1586. break;
  1587. }
  1588. case MAT_T_UINT32:
  1589. {
  1590. mat_uint32_t ui32;
  1591. data_size = sizeof(mat_uint32_t);
  1592. if ( mat->byteswap ) {
  1593. for ( i = 0; i < len; i++ ) {
  1594. bytesread += fread(&ui32,data_size,1,(FILE*)mat->fp);
  1595. data[i] = Mat_uint32Swap(&ui32);
  1596. }
  1597. } else {
  1598. for ( i = 0; i < len; i++ ) {
  1599. bytesread += fread(&ui32,data_size,1,(FILE*)mat->fp);
  1600. data[i] = ui32;
  1601. }
  1602. }
  1603. break;
  1604. }
  1605. case MAT_T_INT16:
  1606. {
  1607. mat_int16_t i16;
  1608. data_size = sizeof(mat_int16_t);
  1609. if ( mat->byteswap ) {
  1610. for ( i = 0; i < len; i++ ) {
  1611. bytesread += fread(&i16,data_size,1,(FILE*)mat->fp);
  1612. data[i] = Mat_int16Swap(&i16);
  1613. }
  1614. } else {
  1615. for ( i = 0; i < len; i++ ) {
  1616. bytesread += fread(&i16,data_size,1,(FILE*)mat->fp);
  1617. data[i] = i16;
  1618. }
  1619. }
  1620. break;
  1621. }
  1622. case MAT_T_UINT16:
  1623. {
  1624. mat_uint16_t ui16;
  1625. data_size = sizeof(mat_uint16_t);
  1626. if ( mat->byteswap ) {
  1627. for ( i = 0; i < len; i++ ) {
  1628. bytesread += fread(&ui16,data_size,1,(FILE*)mat->fp);
  1629. data[i] = Mat_uint16Swap(&ui16);
  1630. }
  1631. } else {
  1632. for ( i = 0; i < len; i++ ) {
  1633. bytesread += fread(&ui16,data_size,1,(FILE*)mat->fp);
  1634. data[i] = ui16;
  1635. }
  1636. }
  1637. break;
  1638. }
  1639. case MAT_T_INT8:
  1640. {
  1641. mat_int8_t i8;
  1642. data_size = sizeof(mat_int8_t);
  1643. for ( i = 0; i < len; i++ ) {
  1644. bytesread += fread(&i8,data_size,1,(FILE*)mat->fp);
  1645. data[i] = i8;
  1646. }
  1647. break;
  1648. }
  1649. case MAT_T_UINT8:
  1650. {
  1651. mat_uint8_t ui8;
  1652. data_size = sizeof(mat_uint8_t);
  1653. for ( i = 0; i < len; i++ ) {
  1654. bytesread += fread(&ui8,data_size,1,(FILE*)mat->fp);
  1655. data[i] = ui8;
  1656. }
  1657. break;
  1658. }
  1659. default:
  1660. return 0;
  1661. }
  1662. bytesread *= data_size;
  1663. return bytesread;
  1664. }
  1665. #if defined(HAVE_ZLIB)
  1666. /** @brief Reads data of type @c data_type into a double type
  1667. *
  1668. * Reads from the MAT file @c len compressed elements of data type @c data_type
  1669. * storing them as double's in @c data.
  1670. * @ingroup mat_internal
  1671. * @param mat MAT file pointer
  1672. * @param z Pointer to the zlib stream for inflation
  1673. * @param data Pointer to store the output double values (len*sizeof(double))
  1674. * @param data_type one of the @c matio_types enumerations which is the source
  1675. * data type in the file
  1676. * @param len Number of elements of type @c data_type to read from the file
  1677. * @retval Number of bytes read from the file
  1678. */
  1679. static int
  1680. ReadCompressedDoubleData(mat_t *mat,z_streamp z,double *data,
  1681. enum matio_types data_type,int len)
  1682. {
  1683. int nBytes = 0, data_size = 0, i;
  1684. union _buf {
  1685. #if SIZEOF_DOUBLE == 8
  1686. double d[128];
  1687. #elif SIZEOF_DOUBLE == 16
  1688. double d[64];
  1689. #endif
  1690. float f[256];
  1691. mat_int32_t i32[256];
  1692. mat_uint32_t ui32[256];
  1693. mat_int16_t i16[512];
  1694. mat_uint16_t ui16[512];
  1695. mat_int8_t i8[1024];
  1696. mat_uint8_t ui8[1024];
  1697. } buf;
  1698. switch ( data_type ) {
  1699. case MAT_T_DOUBLE:
  1700. {
  1701. data_size = sizeof(double);
  1702. if ( mat->byteswap ) {
  1703. InflateData(mat,z,data,len*data_size);
  1704. for ( i = 0; i < len; i++ )
  1705. (void)Mat_doubleSwap(data+i);
  1706. } else {
  1707. InflateData(mat,z,data,len*data_size);
  1708. }
  1709. break;
  1710. }
  1711. case MAT_T_INT32:
  1712. {
  1713. data_size = sizeof(mat_int32_t);
  1714. if ( mat->byteswap ) {
  1715. if ( len <= 256 ){
  1716. InflateData(mat,z,buf.i32,len*data_size);
  1717. for ( i = 0; i < len; i++ )
  1718. data[i] = Mat_int32Swap(buf.i32+i);
  1719. } else {
  1720. int j;
  1721. len -= 256;
  1722. for ( i = 0; i < len; i+=256 ) {
  1723. InflateData(mat,z,buf.i32,256*data_size);
  1724. for ( j = 0; j < 256; j++ )
  1725. data[i+j] = Mat_int32Swap(buf.i32+j);
  1726. }
  1727. len = len-(i-256);
  1728. InflateData(mat,z,buf.i32,len*data_size);
  1729. for ( j = 0; j < len; j++ )
  1730. data[i+j] = Mat_int32Swap(buf.i32+j);
  1731. }
  1732. } else {
  1733. if ( len <= 256 ){
  1734. InflateData(mat,z,buf.i32,len*data_size);
  1735. for ( i = 0; i < len; i++ )
  1736. data[i] = buf.i32[i];
  1737. } else {
  1738. int j;
  1739. len -= 256;
  1740. for ( i = 0; i < len; i+=256 ) {
  1741. InflateData(mat,z,buf.i32,256*data_size);
  1742. for ( j = 0; j < 256; j++ )
  1743. data[i+j] = buf.i32[j];
  1744. }
  1745. len = len-(i-256);
  1746. InflateData(mat,z,buf.i32,len*data_size);
  1747. for ( j = 0; j < len; j++ )
  1748. data[i+j] = buf.i32[j];
  1749. }
  1750. }
  1751. break;
  1752. }
  1753. case MAT_T_UINT32:
  1754. {
  1755. data_size = sizeof(mat_uint32_t);
  1756. if ( mat->byteswap ) {
  1757. if ( len <= 256 ){
  1758. InflateData(mat,z,buf.ui32,len*data_size);
  1759. for ( i = 0; i < len; i++ )
  1760. data[i] = Mat_uint32Swap(buf.ui32+i);
  1761. } else {
  1762. int j;
  1763. len -= 256;
  1764. for ( i = 0; i < len; i+=256 ) {
  1765. InflateData(mat,z,buf.ui32,256*data_size);
  1766. for ( j = 0; j < 256; j++ )
  1767. data[i+j] = Mat_uint32Swap(buf.ui32+j);
  1768. }
  1769. len = len-(i-256);
  1770. InflateData(mat,z,buf.ui32,len*data_size);
  1771. for ( j = 0; j < len; j++ )
  1772. data[i+j] = Mat_uint32Swap(buf.ui32+j);
  1773. }
  1774. } else {
  1775. if ( len <= 256 ) {
  1776. InflateData(mat,z,buf.ui32,len*data_size);
  1777. for ( i = 0; i < len; i++ )
  1778. data[i] = buf.ui32[i];
  1779. } else {
  1780. int j;
  1781. len -= 256;
  1782. for ( i = 0; i < len; i+=256 ) {
  1783. InflateData(mat,z,buf.ui32,256*data_size);
  1784. for ( j = 0; j < 256; j++ )
  1785. data[i+j] = buf.ui32[j];
  1786. }
  1787. len = len-(i-256);
  1788. InflateData(mat,z,buf.ui32,len*data_size);
  1789. for ( j = 0; j < len; j++ )
  1790. data[i+j] = buf.ui32[j];
  1791. }
  1792. }
  1793. break;
  1794. }
  1795. case MAT_T_INT16:
  1796. {
  1797. data_size = sizeof(mat_int16_t);
  1798. if ( mat->byteswap ) {
  1799. if ( len <= 512 ){
  1800. InflateData(mat,z,buf.i16,len*data_size);
  1801. for ( i = 0; i < len; i++ )
  1802. data[i] = Mat_int16Swap(buf.i16+i);
  1803. } else {
  1804. int j;
  1805. len -= 512;
  1806. for ( i = 0; i < len; i+=512 ) {
  1807. InflateData(mat,z,buf.i16,512*data_size);
  1808. for ( j = 0; j < 512; j++ )
  1809. data[i+j] = Mat_int16Swap(buf.i16+j);
  1810. }
  1811. len = len-(i-512);
  1812. InflateData(mat,z,buf.i16,len*data_size);
  1813. for ( j = 0; j < len; j++ )
  1814. data[i+j] = Mat_int16Swap(buf.i16+j);
  1815. }
  1816. } else {
  1817. if ( len <= 512 ) {
  1818. InflateData(mat,z,buf.i16,len*data_size);
  1819. for ( i = 0; i < len; i++ )
  1820. data[i] = buf.i16[i];
  1821. } else {
  1822. int j;
  1823. len -= 512;
  1824. for ( i = 0; i < len; i+=512 ) {
  1825. InflateData(mat,z,buf.i16,512*data_size);
  1826. for ( j = 0; j < 512; j++ )
  1827. data[i+j] = buf.i16[j];
  1828. }
  1829. len = len-(i-512);
  1830. InflateData(mat,z,buf.i16,len*data_size);
  1831. for ( j = 0; j < len; j++ )
  1832. data[i+j] = buf.i16[j];
  1833. }
  1834. }
  1835. break;
  1836. }
  1837. case MAT_T_UINT16:
  1838. {
  1839. data_size = sizeof(mat_uint16_t);
  1840. if ( mat->byteswap ) {
  1841. if ( len <= 512 ){
  1842. InflateData(mat,z,buf.ui16,len*data_size);
  1843. for ( i = 0; i < len; i++ )
  1844. data[i] = Mat_uint16Swap(buf.ui16+i);
  1845. } else {
  1846. int j;
  1847. len -= 512;
  1848. for ( i = 0; i < len; i+=512 ) {
  1849. InflateData(mat,z,buf.ui16,512*data_size);
  1850. for ( j = 0; j < 512; j++ )
  1851. data[i+j] = Mat_uint16Swap(buf.ui16+j);
  1852. }
  1853. len = len-(i-512);
  1854. InflateData(mat,z,buf.ui16,len*data_size);
  1855. for ( j = 0; j < len; j++ )
  1856. data[i+j] = Mat_uint16Swap(buf.ui16+j);
  1857. }
  1858. } else {
  1859. if ( len <= 512 ) {
  1860. InflateData(mat,z,buf.ui16,len*data_size);
  1861. for ( i = 0; i < len; i++ )
  1862. data[i] = buf.ui16[i];
  1863. } else {
  1864. int j;
  1865. len -= 512;
  1866. for ( i = 0; i < len; i+=512 ) {
  1867. InflateData(mat,z,buf.ui16,512*data_size);
  1868. for ( j = 0; j < 512; j++ )
  1869. data[i+j] = buf.ui16[j];
  1870. }
  1871. len = len-(i-512);
  1872. InflateData(mat,z,buf.ui16,len*data_size);
  1873. for ( j = 0; j < len; j++ )
  1874. data[i+j] = buf.ui16[j];
  1875. }
  1876. }
  1877. break;
  1878. }
  1879. case MAT_T_UINT8:
  1880. {
  1881. data_size = sizeof(mat_uint8_t);
  1882. if ( len <= 1024 ) {
  1883. InflateData(mat,z,buf.ui8,len*data_size);
  1884. for ( i = 0; i < len; i++ )
  1885. data[i] = buf.ui8[i];
  1886. } else {
  1887. int j;
  1888. len -= 1024;
  1889. for ( i = 0; i < len; i+=1024 ) {
  1890. InflateData(mat,z,buf.ui8,1024*data_size);
  1891. for ( j = 0; j < 1024; j++ )
  1892. data[i+j] = buf.ui8[j];
  1893. }
  1894. len = len-(i-1024);
  1895. InflateData(mat,z,buf.ui8,len*data_size);
  1896. for ( j = 0; j < len; j++ )
  1897. data[i+j] = buf.ui8[j];
  1898. }
  1899. break;
  1900. }
  1901. case MAT_T_INT8:
  1902. {
  1903. data_size = sizeof(mat_int8_t);
  1904. if ( len <= 1024 ) {
  1905. InflateData(mat,z,buf.i8,len*data_size);
  1906. for ( i = 0; i < len; i++ )
  1907. data[i] = buf.i8[i];
  1908. } else {
  1909. int j;
  1910. len -= 1024;
  1911. for ( i = 0; i < len; i+=1024 ) {
  1912. InflateData(mat,z,buf.i8,1024*data_size);
  1913. for ( j = 0; j < 1024; j++ )
  1914. data[i+j] = buf.i8[j];
  1915. }
  1916. len = len-(i-1024);
  1917. InflateData(mat,z,buf.i8,len*data_size);
  1918. for ( j = 0; j < len; j++ )
  1919. data[i+j] = buf.i8[j];
  1920. }
  1921. break;
  1922. }
  1923. default:
  1924. return 0;
  1925. }
  1926. nBytes = len*data_size;
  1927. return nBytes;
  1928. }
  1929. #endif
  1930. /** @brief Reads data of type @c data_type into a float type
  1931. *
  1932. * Reads from the MAT file @c len elements of data type @c data_type storing
  1933. * them as float's in @c data.
  1934. * @ingroup mat_internal
  1935. * @param mat MAT file pointer
  1936. * @param data Pointer to store the output float values (len*sizeof(float))
  1937. * @param data_type one of the @c matio_types enumerations which is the source
  1938. * data type in the file
  1939. * @param len Number of elements of type @c data_type to read from the file
  1940. * @retval Number of bytes read from the file
  1941. */
  1942. static int
  1943. ReadSingleData(mat_t *mat,float *data,enum matio_types data_type,int len)
  1944. {
  1945. int bytesread = 0, data_size = 0, i;
  1946. if ( (mat == NULL) || (data == NULL) || (mat->fp == NULL) )
  1947. return 0;
  1948. switch ( data_type ) {
  1949. case MAT_T_DOUBLE:
  1950. {
  1951. double d;
  1952. data_size = sizeof(double);
  1953. if ( mat->byteswap ) {
  1954. for ( i = 0; i < len; i++ ) {
  1955. bytesread += fread(&d,data_size,1,(FILE*)mat->fp);
  1956. data[i] = Mat_doubleSwap(&d);
  1957. }
  1958. } else {
  1959. for ( i = 0; i < len; i++ ) {
  1960. bytesread += fread(&d,data_size,1,(FILE*)mat->fp);
  1961. data[i] = d;
  1962. }
  1963. }
  1964. break;
  1965. }
  1966. case MAT_T_SINGLE:
  1967. {
  1968. float f;
  1969. data_size = sizeof(float);
  1970. if ( mat->byteswap ) {
  1971. for ( i = 0; i < len; i++ ) {
  1972. bytesread += fread(&f,data_size,1,(FILE*)mat->fp);
  1973. data[i] = Mat_floatSwap(&f);
  1974. }
  1975. } else {
  1976. for ( i = 0; i < len; i++ ) {
  1977. bytesread += fread(&f,data_size,1,(FILE*)mat->fp);
  1978. data[i] = f;
  1979. }
  1980. }
  1981. break;
  1982. }
  1983. case MAT_T_INT32:
  1984. {
  1985. mat_int32_t i32;
  1986. data_size = sizeof(mat_int32_t);
  1987. if ( mat->byteswap ) {
  1988. for ( i = 0; i < len; i++ ) {
  1989. bytesread += fread(&i32,data_size,1,(FILE*)mat->fp);
  1990. data[i] = Mat_int32Swap(&i32);
  1991. }
  1992. } else {
  1993. for ( i = 0; i < len; i++ ) {
  1994. bytesread += fread(&i32,data_size,1,(FILE*)mat->fp);
  1995. data[i] = i32;
  1996. }
  1997. }
  1998. break;
  1999. }
  2000. case MAT_T_UINT32:
  2001. {
  2002. mat_uint32_t ui32;
  2003. data_size = sizeof(mat_uint32_t);
  2004. if ( mat->byteswap ) {
  2005. for ( i = 0; i < len; i++ ) {
  2006. bytesread += fread(&ui32,data_size,1,(FILE*)mat->fp);
  2007. data[i] = Mat_uint32Swap(&ui32);
  2008. }
  2009. } else {
  2010. for ( i = 0; i < len; i++ ) {
  2011. bytesread += fread(&ui32,data_size,1,(FILE*)mat->fp);
  2012. data[i] = ui32;
  2013. }
  2014. }
  2015. break;
  2016. }
  2017. case MAT_T_INT16:
  2018. {
  2019. mat_int16_t i16;
  2020. data_size = sizeof(mat_int16_t);
  2021. if ( mat->byteswap ) {
  2022. for ( i = 0; i < len; i++ ) {
  2023. bytesread += fread(&i16,data_size,1,(FILE*)mat->fp);
  2024. data[i] = Mat_int16Swap(&i16);
  2025. }
  2026. } else {
  2027. for ( i = 0; i < len; i++ ) {
  2028. bytesread += fread(&i16,data_size,1,(FILE*)mat->fp);
  2029. data[i] = i16;
  2030. }
  2031. }
  2032. break;
  2033. }
  2034. case MAT_T_UINT16:
  2035. {
  2036. mat_uint16_t ui16;
  2037. data_size = sizeof(mat_uint16_t);
  2038. if ( mat->byteswap ) {
  2039. for ( i = 0; i < len; i++ ) {
  2040. bytesread += fread(&ui16,data_size,1,(FILE*)mat->fp);
  2041. data[i] = Mat_uint16Swap(&ui16);
  2042. }
  2043. } else {
  2044. for ( i = 0; i < len; i++ ) {
  2045. bytesread += fread(&ui16,data_size,1,(FILE*)mat->fp);
  2046. data[i] = ui16;
  2047. }
  2048. }
  2049. break;
  2050. }
  2051. case MAT_T_INT8:
  2052. {
  2053. mat_int8_t i8;
  2054. data_size = sizeof(mat_int8_t);
  2055. for ( i = 0; i < len; i++ ) {
  2056. bytesread += fread(&i8,data_size,1,(FILE*)mat->fp);
  2057. data[i] = i8;
  2058. }
  2059. break;
  2060. }
  2061. case MAT_T_UINT8:
  2062. {
  2063. mat_uint8_t ui8;
  2064. data_size = sizeof(mat_uint8_t);
  2065. for ( i = 0; i < len; i++ ) {
  2066. bytesread += fread(&ui8,data_size,1,(FILE*)mat->fp);
  2067. data[i] = ui8;
  2068. }
  2069. break;
  2070. }
  2071. default:
  2072. return 0;
  2073. }
  2074. bytesread *= data_size;
  2075. return bytesread;
  2076. }
  2077. #if defined(HAVE_ZLIB)
  2078. /** @brief Reads data of type @c data_type into a float type
  2079. *
  2080. * Reads from the MAT file @c len compressed elements of data type @c data_type
  2081. * storing them as float's in @c data.
  2082. * @ingroup mat_internal
  2083. * @param mat MAT file pointer
  2084. * @param z Pointer to the zlib stream for inflation
  2085. * @param data Pointer to store the output float values (len*sizeof(float))
  2086. * @param data_type one of the @c matio_types enumerations which is the source
  2087. * data type in the file
  2088. * @param len Number of elements of type @c data_type to read from the file
  2089. * @retval Number of bytes read from the file
  2090. */
  2091. static int
  2092. ReadCompressedSingleData(mat_t *mat,z_streamp z,float *data,
  2093. enum matio_types data_type,int len)
  2094. {
  2095. int nBytes = 0, data_size = 0, i;
  2096. if ( (mat == NULL) || (data == NULL) || (z == NULL) )
  2097. return 0;
  2098. switch ( data_type ) {
  2099. case MAT_T_DOUBLE:
  2100. {
  2101. double d;
  2102. data_size = sizeof(double);
  2103. if ( mat->byteswap ) {
  2104. for ( i = 0; i < len; i++ ) {
  2105. InflateData(mat,z,&d,data_size);
  2106. data[i] = Mat_doubleSwap(&d);
  2107. }
  2108. } else {
  2109. for ( i = 0; i < len; i++ ) {
  2110. InflateData(mat,z,&d,data_size);
  2111. data[i] = d;
  2112. }
  2113. }
  2114. break;
  2115. }
  2116. case MAT_T_SINGLE:
  2117. {
  2118. float f;
  2119. data_size = sizeof(float);
  2120. if ( mat->byteswap ) {
  2121. for ( i = 0; i < len; i++ ) {
  2122. InflateData(mat,z,&f,data_size);
  2123. data[i] = Mat_floatSwap(&f);
  2124. }
  2125. } else {
  2126. for ( i = 0; i < len; i++ ) {
  2127. InflateData(mat,z,data+i,data_size);
  2128. }
  2129. }
  2130. break;
  2131. }
  2132. case MAT_T_INT32:
  2133. {
  2134. mat_int32_t i32;
  2135. data_size = sizeof(mat_int32_t);
  2136. if ( mat->byteswap ) {
  2137. for ( i = 0; i < len; i++ ) {
  2138. InflateData(mat,z,&i32,data_size);
  2139. data[i] = Mat_int32Swap(&i32);
  2140. }
  2141. } else {
  2142. for ( i = 0; i < len; i++ ) {
  2143. InflateData(mat,z,&i32,data_size);
  2144. data[i] = i32;
  2145. }
  2146. }
  2147. break;
  2148. }
  2149. case MAT_T_UINT32:
  2150. {
  2151. mat_uint32_t ui32;
  2152. data_size = sizeof(mat_uint32_t);
  2153. if ( mat->byteswap ) {
  2154. for ( i = 0; i < len; i++ ) {
  2155. InflateData(mat,z,&ui32,data_size);
  2156. data[i] = Mat_uint32Swap(&ui32);
  2157. }
  2158. } else {
  2159. for ( i = 0; i < len; i++ ) {
  2160. InflateData(mat,z,&ui32,data_size);
  2161. data[i] = ui32;
  2162. }
  2163. }
  2164. break;
  2165. }
  2166. case MAT_T_INT16:
  2167. {
  2168. mat_int16_t i16;
  2169. data_size = sizeof(mat_int16_t);
  2170. if ( mat->byteswap ) {
  2171. for ( i = 0; i < len; i++ ) {
  2172. InflateData(mat,z,&i16,data_size);
  2173. data[i] = Mat_int16Swap(&i16);
  2174. }
  2175. } else {
  2176. for ( i = 0; i < len; i++ ) {
  2177. InflateData(mat,z,&i16,data_size);
  2178. data[i] = i16;
  2179. }
  2180. }
  2181. break;
  2182. }
  2183. case MAT_T_UINT16:
  2184. {
  2185. mat_uint16_t ui16;
  2186. data_size = sizeof(mat_uint16_t);
  2187. if ( mat->byteswap ) {
  2188. for ( i = 0; i < len; i++ ) {
  2189. InflateData(mat,z,&ui16,data_size);
  2190. data[i] = Mat_uint16Swap(&ui16);
  2191. }
  2192. } else {
  2193. for ( i = 0; i < len; i++ ) {
  2194. InflateData(mat,z,&ui16,data_size);
  2195. data[i] = ui16;
  2196. }
  2197. }
  2198. break;
  2199. }
  2200. case MAT_T_UINT8:
  2201. {
  2202. mat_uint8_t ui8;
  2203. data_size = sizeof(mat_uint8_t);
  2204. for ( i = 0; i < len; i++ ) {
  2205. InflateData(mat,z,&ui8,data_size);
  2206. data[i] = ui8;
  2207. }
  2208. break;
  2209. }
  2210. case MAT_T_INT8:
  2211. {
  2212. mat_int8_t i8;
  2213. data_size = sizeof(mat_int8_t);
  2214. for ( i = 0; i < len; i++ ) {
  2215. InflateData(mat,z,&i8,data_size);
  2216. data[i] = i8;
  2217. }
  2218. break;
  2219. }
  2220. default:
  2221. return 0;
  2222. }
  2223. nBytes = len*data_size;
  2224. return nBytes;
  2225. }
  2226. #endif
  2227. #ifdef HAVE_MATIO_INT64_T
  2228. /** @brief Reads data of type @c data_type into a signed 64-bit integer type
  2229. *
  2230. * Reads from the MAT file @c len elements of data type @c data_type storing
  2231. * them as signed 64-bit integers in @c data.
  2232. * @ingroup mat_internal
  2233. * @param mat MAT file pointer
  2234. * @param data Pointer to store the output signed 64-bit integer values
  2235. * (len*sizeof(mat_int64_t))
  2236. * @param data_type one of the @c matio_types enumerations which is the source
  2237. * data type in the file
  2238. * @param len Number of elements of type @c data_type to read from the file
  2239. * @retval Number of bytes read from the file
  2240. */
  2241. static int
  2242. ReadInt64Data(mat_t *mat,mat_int64_t *data,enum matio_types data_type,int len)
  2243. {
  2244. int bytesread = 0, data_size = 0, i;
  2245. if ( (mat == NULL) || (data == NULL) || (mat->fp == NULL) )
  2246. return 0;
  2247. switch ( data_type ) {
  2248. case MAT_T_DOUBLE:
  2249. {
  2250. double d;
  2251. data_size = sizeof(double);
  2252. if ( mat->byteswap ) {
  2253. for ( i = 0; i < len; i++ ) {
  2254. bytesread += fread(&d,data_size,1,(FILE*)mat->fp);
  2255. data[i] = Mat_doubleSwap(&d);
  2256. }
  2257. } else {
  2258. for ( i = 0; i < len; i++ ) {
  2259. bytesread += fread(&d,data_size,1,(FILE*)mat->fp);
  2260. data[i] = d;
  2261. }
  2262. }
  2263. break;
  2264. }
  2265. case MAT_T_SINGLE:
  2266. {
  2267. float f;
  2268. data_size = sizeof(float);
  2269. if ( mat->byteswap ) {
  2270. for ( i = 0; i < len; i++ ) {
  2271. bytesread += fread(&f,data_size,1,(FILE*)mat->fp);
  2272. data[i] = Mat_floatSwap(&f);
  2273. }
  2274. } else {
  2275. for ( i = 0; i < len; i++ ) {
  2276. bytesread += fread(&f,data_size,1,(FILE*)mat->fp);
  2277. data[i] = f;
  2278. }
  2279. }
  2280. break;
  2281. }
  2282. case MAT_T_INT64:
  2283. {
  2284. mat_int64_t i64;
  2285. data_size = sizeof(mat_int64_t);
  2286. if ( mat->byteswap ) {
  2287. for ( i = 0; i < len; i++ ) {
  2288. bytesread += fread(&i64,data_size,1,(FILE*)mat->fp);
  2289. data[i] = Mat_int64Swap(&i64);
  2290. }
  2291. } else {
  2292. for ( i = 0; i < len; i++ ) {
  2293. bytesread += fread(&i64,data_size,1,(FILE*)mat->fp);
  2294. data[i] = i64;
  2295. }
  2296. }
  2297. break;
  2298. }
  2299. #ifdef HAVE_MATIO_UINT64_T
  2300. case MAT_T_UINT64:
  2301. {
  2302. mat_uint64_t ui64;
  2303. data_size = sizeof(mat_uint64_t);
  2304. if ( mat->byteswap ) {
  2305. for ( i = 0; i < len; i++ ) {
  2306. bytesread += fread(&ui64,data_size,1,(FILE*)mat->fp);
  2307. data[i] = Mat_uint64Swap(&ui64);
  2308. }
  2309. } else {
  2310. for ( i = 0; i < len; i++ ) {
  2311. bytesread += fread(&ui64,data_size,1,(FILE*)mat->fp);
  2312. data[i] = ui64;
  2313. }
  2314. }
  2315. break;
  2316. }
  2317. #endif /* HAVE_MATIO_UINT64_T */
  2318. case MAT_T_INT32:
  2319. {
  2320. mat_int32_t i32;
  2321. data_size = sizeof(mat_int32_t);
  2322. if ( mat->byteswap ) {
  2323. for ( i = 0; i < len; i++ ) {
  2324. bytesread += fread(&i32,data_size,1,(FILE*)mat->fp);
  2325. data[i] = Mat_int32Swap(&i32);
  2326. }
  2327. } else {
  2328. for ( i = 0; i < len; i++ ) {
  2329. bytesread += fread(&i32,data_size,1,(FILE*)mat->fp);
  2330. data[i] = i32;
  2331. }
  2332. }
  2333. break;
  2334. }
  2335. case MAT_T_UINT32:
  2336. {
  2337. mat_uint32_t ui32;
  2338. data_size = sizeof(mat_uint32_t);
  2339. if ( mat->byteswap ) {
  2340. for ( i = 0; i < len; i++ ) {
  2341. bytesread += fread(&ui32,data_size,1,(FILE*)mat->fp);
  2342. data[i] = Mat_uint32Swap(&ui32);
  2343. }
  2344. } else {
  2345. for ( i = 0; i < len; i++ ) {
  2346. bytesread += fread(&ui32,data_size,1,(FILE*)mat->fp);
  2347. data[i] = ui32;
  2348. }
  2349. }
  2350. break;
  2351. }
  2352. case MAT_T_INT16:
  2353. {
  2354. mat_int16_t i16;
  2355. data_size = sizeof(mat_int16_t);
  2356. if ( mat->byteswap ) {
  2357. for ( i = 0; i < len; i++ ) {
  2358. bytesread += fread(&i16,data_size,1,(FILE*)mat->fp);
  2359. data[i] = Mat_int16Swap(&i16);
  2360. }
  2361. } else {
  2362. for ( i = 0; i < len; i++ ) {
  2363. bytesread += fread(&i16,data_size,1,(FILE*)mat->fp);
  2364. data[i] = i16;
  2365. }
  2366. }
  2367. break;
  2368. }
  2369. case MAT_T_UINT16:
  2370. {
  2371. mat_uint16_t ui16;
  2372. data_size = sizeof(mat_uint16_t);
  2373. if ( mat->byteswap ) {
  2374. for ( i = 0; i < len; i++ ) {
  2375. bytesread += fread(&ui16,data_size,1,(FILE*)mat->fp);
  2376. data[i] = Mat_uint16Swap(&ui16);
  2377. }
  2378. } else {
  2379. for ( i = 0; i < len; i++ ) {
  2380. bytesread += fread(&ui16,data_size,1,(FILE*)mat->fp);
  2381. data[i] = ui16;
  2382. }
  2383. }
  2384. break;
  2385. }
  2386. case MAT_T_INT8:
  2387. {
  2388. mat_int8_t i8;
  2389. data_size = sizeof(mat_int8_t);
  2390. for ( i = 0; i < len; i++ ) {
  2391. bytesread += fread(&i8,data_size,1,(FILE*)mat->fp);
  2392. data[i] = i8;
  2393. }
  2394. break;
  2395. }
  2396. case MAT_T_UINT8:
  2397. {
  2398. mat_uint8_t ui8;
  2399. data_size = sizeof(mat_uint8_t);
  2400. for ( i = 0; i < len; i++ ) {
  2401. bytesread += fread(&ui8,data_size,1,(FILE*)mat->fp);
  2402. data[i] = ui8;
  2403. }
  2404. break;
  2405. }
  2406. default:
  2407. return 0;
  2408. }
  2409. bytesread *= data_size;
  2410. return bytesread;
  2411. }
  2412. #if defined(HAVE_ZLIB)
  2413. /** @brief Reads data of type @c data_type into a signed 64-bit integer type
  2414. *
  2415. * Reads from the MAT file @c len compressed elements of data type @c data_type
  2416. * storing them as signed 64-bit integers in @c data.
  2417. * @ingroup mat_internal
  2418. * @param mat MAT file pointer
  2419. * @param z Pointer to the zlib stream for inflation
  2420. * @param data Pointer to store the output signed 64-bit integer values
  2421. * (len*sizeof(mat_int64_t))
  2422. * @param data_type one of the @c matio_types enumerations which is the source
  2423. * data type in the file
  2424. * @param len Number of elements of type @c data_type to read from the file
  2425. * @retval Number of bytes read from the file
  2426. */
  2427. static int
  2428. ReadCompressedInt64Data(mat_t *mat,z_streamp z,mat_int64_t *data,
  2429. enum matio_types data_type,int len)
  2430. {
  2431. int nBytes = 0, data_size = 0, i;
  2432. if ( (mat == NULL) || (data == NULL) || (z == NULL) )
  2433. return 0;
  2434. switch ( data_type ) {
  2435. case MAT_T_DOUBLE:
  2436. {
  2437. double d;
  2438. data_size = sizeof(double);
  2439. if ( mat->byteswap ) {
  2440. for ( i = 0; i < len; i++ ) {
  2441. InflateData(mat,z,&d,data_size);
  2442. data[i] = Mat_doubleSwap(&d);
  2443. }
  2444. } else {
  2445. for ( i = 0; i < len; i++ ) {
  2446. InflateData(mat,z,&d,data_size);
  2447. data[i] = d;
  2448. }
  2449. }
  2450. break;
  2451. }
  2452. case MAT_T_SINGLE:
  2453. {
  2454. float f;
  2455. data_size = sizeof(float);
  2456. if ( mat->byteswap ) {
  2457. for ( i = 0; i < len; i++ ) {
  2458. InflateData(mat,z,&f,data_size);
  2459. data[i] = Mat_floatSwap(&f);
  2460. }
  2461. } else {
  2462. for ( i = 0; i < len; i++ ) {
  2463. InflateData(mat,z,&f,data_size);
  2464. data[i] = f;
  2465. }
  2466. }
  2467. break;
  2468. }
  2469. case MAT_T_INT64:
  2470. {
  2471. mat_int64_t i64;
  2472. data_size = sizeof(mat_int64_t);
  2473. if ( mat->byteswap ) {
  2474. for ( i = 0; i < len; i++ ) {
  2475. InflateData(mat,z,&i64,data_size);
  2476. data[i] = Mat_int64Swap(&i64);
  2477. }
  2478. } else {
  2479. for ( i = 0; i < len; i++ ) {
  2480. InflateData(mat,z,&i64,data_size);
  2481. data[i] = i64;
  2482. }
  2483. }
  2484. break;
  2485. }
  2486. case MAT_T_UINT64:
  2487. {
  2488. mat_uint64_t ui64;
  2489. data_size = sizeof(mat_uint64_t);
  2490. if ( mat->byteswap ) {
  2491. for ( i = 0; i < len; i++ ) {
  2492. InflateData(mat,z,&ui64,data_size);
  2493. data[i] = Mat_uint64Swap(&ui64);
  2494. }
  2495. } else {
  2496. for ( i = 0; i < len; i++ ) {
  2497. InflateData(mat,z,&ui64,data_size);
  2498. data[i] = ui64;
  2499. }
  2500. }
  2501. break;
  2502. }
  2503. case MAT_T_INT32:
  2504. {
  2505. mat_int32_t i32;
  2506. data_size = sizeof(mat_int32_t);
  2507. if ( mat->byteswap ) {
  2508. for ( i = 0; i < len; i++ ) {
  2509. InflateData(mat,z,&i32,data_size);
  2510. data[i] = Mat_int32Swap(&i32);
  2511. }
  2512. } else {
  2513. for ( i = 0; i < len; i++ ) {
  2514. InflateData(mat,z,&i32,data_size);
  2515. data[i] = i32;
  2516. }
  2517. }
  2518. break;
  2519. }
  2520. case MAT_T_UINT32:
  2521. {
  2522. mat_uint32_t ui32;
  2523. data_size = sizeof(mat_uint32_t);
  2524. if ( mat->byteswap ) {
  2525. for ( i = 0; i < len; i++ ) {
  2526. InflateData(mat,z,&ui32,data_size);
  2527. data[i] = Mat_uint32Swap(&ui32);
  2528. }
  2529. } else {
  2530. for ( i = 0; i < len; i++ ) {
  2531. InflateData(mat,z,&ui32,data_size);
  2532. data[i] = ui32;
  2533. }
  2534. }
  2535. break;
  2536. }
  2537. case MAT_T_INT16:
  2538. {
  2539. mat_int16_t i16;
  2540. data_size = sizeof(mat_int16_t);
  2541. if ( mat->byteswap ) {
  2542. for ( i = 0; i < len; i++ ) {
  2543. InflateData(mat,z,&i16,data_size);
  2544. data[i] = Mat_int16Swap(&i16);
  2545. }
  2546. } else {
  2547. for ( i = 0; i < len; i++ ) {
  2548. InflateData(mat,z,&i16,data_size);
  2549. data[i] = i16;
  2550. }
  2551. }
  2552. break;
  2553. }
  2554. case MAT_T_UINT16:
  2555. {
  2556. mat_uint16_t ui16;
  2557. data_size = sizeof(mat_uint16_t);
  2558. if ( mat->byteswap ) {
  2559. for ( i = 0; i < len; i++ ) {
  2560. InflateData(mat,z,&ui16,data_size);
  2561. data[i] = Mat_uint16Swap(&ui16);
  2562. }
  2563. } else {
  2564. for ( i = 0; i < len; i++ ) {
  2565. InflateData(mat,z,&ui16,data_size);
  2566. data[i] = ui16;
  2567. }
  2568. }
  2569. break;
  2570. }
  2571. case MAT_T_UINT8:
  2572. {
  2573. mat_uint8_t ui8;
  2574. data_size = sizeof(mat_uint8_t);
  2575. for ( i = 0; i < len; i++ ) {
  2576. InflateData(mat,z,&ui8,data_size);
  2577. data[i] = ui8;
  2578. }
  2579. break;
  2580. }
  2581. case MAT_T_INT8:
  2582. {
  2583. mat_int8_t i8;
  2584. data_size = sizeof(mat_int8_t);
  2585. for ( i = 0; i < len; i++ ) {
  2586. InflateData(mat,z,&i8,data_size);
  2587. data[i] = i8;
  2588. }
  2589. break;
  2590. }
  2591. default:
  2592. return 0;
  2593. }
  2594. nBytes = len*data_size;
  2595. return nBytes;
  2596. }
  2597. #endif
  2598. #endif /* HAVE_MATIO_INT64_T */
  2599. #ifdef HAVE_MATIO_UINT64_T
  2600. /** @brief Reads data of type @c data_type into an unsigned 64-bit integer type
  2601. *
  2602. * Reads from the MAT file @c len elements of data type @c data_type storing
  2603. * them as unsigned 64-bit integers in @c data.
  2604. * @ingroup mat_internal
  2605. * @param mat MAT file pointer
  2606. * @param data Pointer to store the output unsigned 64-bit integer values
  2607. * (len*sizeof(mat_uint64_t))
  2608. * @param data_type one of the @c matio_types enumerations which is the source
  2609. * data type in the file
  2610. * @param len Number of elements of type @c data_type to read from the file
  2611. * @retval Number of bytes read from the file
  2612. */
  2613. static int
  2614. ReadUInt64Data(mat_t *mat,mat_uint64_t *data,enum matio_types data_type,int len)
  2615. {
  2616. int bytesread = 0, data_size = 0, i;
  2617. if ( (mat == NULL) || (data == NULL) || (mat->fp == NULL) )
  2618. return 0;
  2619. switch ( data_type ) {
  2620. case MAT_T_DOUBLE:
  2621. {
  2622. double d;
  2623. data_size = sizeof(double);
  2624. if ( mat->byteswap ) {
  2625. for ( i = 0; i < len; i++ ) {
  2626. bytesread += fread(&d,data_size,1,(FILE*)mat->fp);
  2627. data[i] = Mat_doubleSwap(&d);
  2628. }
  2629. } else {
  2630. for ( i = 0; i < len; i++ ) {
  2631. bytesread += fread(&d,data_size,1,(FILE*)mat->fp);
  2632. data[i] = d;
  2633. }
  2634. }
  2635. break;
  2636. }
  2637. case MAT_T_SINGLE:
  2638. {
  2639. float f;
  2640. data_size = sizeof(float);
  2641. if ( mat->byteswap ) {
  2642. for ( i = 0; i < len; i++ ) {
  2643. bytesread += fread(&f,data_size,1,(FILE*)mat->fp);
  2644. data[i] = Mat_floatSwap(&f);
  2645. }
  2646. } else {
  2647. for ( i = 0; i < len; i++ ) {
  2648. bytesread += fread(&f,data_size,1,(FILE*)mat->fp);
  2649. data[i] = f;
  2650. }
  2651. }
  2652. break;
  2653. }
  2654. #ifdef HAVE_MATIO_INT64_T
  2655. case MAT_T_INT64:
  2656. {
  2657. mat_int64_t i64;
  2658. data_size = sizeof(mat_int64_t);
  2659. if ( mat->byteswap ) {
  2660. for ( i = 0; i < len; i++ ) {
  2661. bytesread += fread(&i64,data_size,1,(FILE*)mat->fp);
  2662. data[i] = Mat_int64Swap(&i64);
  2663. }
  2664. } else {
  2665. for ( i = 0; i < len; i++ ) {
  2666. bytesread += fread(&i64,data_size,1,(FILE*)mat->fp);
  2667. data[i] = i64;
  2668. }
  2669. }
  2670. break;
  2671. }
  2672. #endif /* HAVE_MATIO_INT64_T */
  2673. case MAT_T_UINT64:
  2674. {
  2675. mat_uint64_t ui64;
  2676. data_size = sizeof(mat_uint64_t);
  2677. if ( mat->byteswap ) {
  2678. for ( i = 0; i < len; i++ ) {
  2679. bytesread += fread(&ui64,data_size,1,(FILE*)mat->fp);
  2680. data[i] = Mat_uint64Swap(&ui64);
  2681. }
  2682. } else {
  2683. for ( i = 0; i < len; i++ ) {
  2684. bytesread += fread(&ui64,data_size,1,(FILE*)mat->fp);
  2685. data[i] = ui64;
  2686. }
  2687. }
  2688. break;
  2689. }
  2690. case MAT_T_INT32:
  2691. {
  2692. mat_int32_t i32;
  2693. data_size = sizeof(mat_int32_t);
  2694. if ( mat->byteswap ) {
  2695. for ( i = 0; i < len; i++ ) {
  2696. bytesread += fread(&i32,data_size,1,(FILE*)mat->fp);
  2697. data[i] = Mat_int32Swap(&i32);
  2698. }
  2699. } else {
  2700. for ( i = 0; i < len; i++ ) {
  2701. bytesread += fread(&i32,data_size,1,(FILE*)mat->fp);
  2702. data[i] = i32;
  2703. }
  2704. }
  2705. break;
  2706. }
  2707. case MAT_T_UINT32:
  2708. {
  2709. mat_uint32_t ui32;
  2710. data_size = sizeof(mat_uint32_t);
  2711. if ( mat->byteswap ) {
  2712. for ( i = 0; i < len; i++ ) {
  2713. bytesread += fread(&ui32,data_size,1,(FILE*)mat->fp);
  2714. data[i] = Mat_uint32Swap(&ui32);
  2715. }
  2716. } else {
  2717. for ( i = 0; i < len; i++ ) {
  2718. bytesread += fread(&ui32,data_size,1,(FILE*)mat->fp);
  2719. data[i] = ui32;
  2720. }
  2721. }
  2722. break;
  2723. }
  2724. case MAT_T_INT16:
  2725. {
  2726. mat_int16_t i16;
  2727. data_size = sizeof(mat_int16_t);
  2728. if ( mat->byteswap ) {
  2729. for ( i = 0; i < len; i++ ) {
  2730. bytesread += fread(&i16,data_size,1,(FILE*)mat->fp);
  2731. data[i] = Mat_int16Swap(&i16);
  2732. }
  2733. } else {
  2734. for ( i = 0; i < len; i++ ) {
  2735. bytesread += fread(&i16,data_size,1,(FILE*)mat->fp);
  2736. data[i] = i16;
  2737. }
  2738. }
  2739. break;
  2740. }
  2741. case MAT_T_UINT16:
  2742. {
  2743. mat_uint16_t ui16;
  2744. data_size = sizeof(mat_uint16_t);
  2745. if ( mat->byteswap ) {
  2746. for ( i = 0; i < len; i++ ) {
  2747. bytesread += fread(&ui16,data_size,1,(FILE*)mat->fp);
  2748. data[i] = Mat_uint16Swap(&ui16);
  2749. }
  2750. } else {
  2751. for ( i = 0; i < len; i++ ) {
  2752. bytesread += fread(&ui16,data_size,1,(FILE*)mat->fp);
  2753. data[i] = ui16;
  2754. }
  2755. }
  2756. break;
  2757. }
  2758. case MAT_T_INT8:
  2759. {
  2760. mat_int8_t i8;
  2761. data_size = sizeof(mat_int8_t);
  2762. for ( i = 0; i < len; i++ ) {
  2763. bytesread += fread(&i8,data_size,1,(FILE*)mat->fp);
  2764. data[i] = i8;
  2765. }
  2766. break;
  2767. }
  2768. case MAT_T_UINT8:
  2769. {
  2770. mat_uint8_t ui8;
  2771. data_size = sizeof(mat_uint8_t);
  2772. for ( i = 0; i < len; i++ ) {
  2773. bytesread += fread(&ui8,data_size,1,(FILE*)mat->fp);
  2774. data[i] = ui8;
  2775. }
  2776. break;
  2777. }
  2778. default:
  2779. return 0;
  2780. }
  2781. bytesread *= data_size;
  2782. return bytesread;
  2783. }
  2784. #if defined(HAVE_ZLIB)
  2785. /** @brief Reads data of type @c data_type into an unsigned 64-bit integer type
  2786. *
  2787. * Reads from the MAT file @c len compressed elements of data type @c data_type
  2788. * storing them as unsigned 64-bit integers in @c data.
  2789. * @ingroup mat_internal
  2790. * @param mat MAT file pointer
  2791. * @param z Pointer to the zlib stream for inflation
  2792. * @param data Pointer to store the output unsigned 64-bit integer values
  2793. * (len*sizeof(mat_uint64_t))
  2794. * @param data_type one of the @c matio_types enumerations which is the source
  2795. * data type in the file
  2796. * @param len Number of elements of type @c data_type to read from the file
  2797. * @retval Number of bytes read from the file
  2798. */
  2799. static int
  2800. ReadCompressedUInt64Data(mat_t *mat,z_streamp z,mat_uint64_t *data,
  2801. enum matio_types data_type,int len)
  2802. {
  2803. int nBytes = 0, data_size = 0, i;
  2804. if ( (mat == NULL) || (data == NULL) || (z == NULL) )
  2805. return 0;
  2806. switch ( data_type ) {
  2807. case MAT_T_DOUBLE:
  2808. {
  2809. double d;
  2810. data_size = sizeof(double);
  2811. if ( mat->byteswap ) {
  2812. for ( i = 0; i < len; i++ ) {
  2813. InflateData(mat,z,&d,data_size);
  2814. data[i] = Mat_doubleSwap(&d);
  2815. }
  2816. } else {
  2817. for ( i = 0; i < len; i++ ) {
  2818. InflateData(mat,z,&d,data_size);
  2819. data[i] = d;
  2820. }
  2821. }
  2822. break;
  2823. }
  2824. case MAT_T_SINGLE:
  2825. {
  2826. float f;
  2827. data_size = sizeof(float);
  2828. if ( mat->byteswap ) {
  2829. for ( i = 0; i < len; i++ ) {
  2830. InflateData(mat,z,&f,data_size);
  2831. data[i] = Mat_floatSwap(&f);
  2832. }
  2833. } else {
  2834. for ( i = 0; i < len; i++ ) {
  2835. InflateData(mat,z,&f,data_size);
  2836. data[i] = f;
  2837. }
  2838. }
  2839. break;
  2840. }
  2841. case MAT_T_INT64:
  2842. {
  2843. mat_int64_t i64;
  2844. data_size = sizeof(mat_int64_t);
  2845. if ( mat->byteswap ) {
  2846. for ( i = 0; i < len; i++ ) {
  2847. InflateData(mat,z,&i64,data_size);
  2848. data[i] = Mat_int64Swap(&i64);
  2849. }
  2850. } else {
  2851. for ( i = 0; i < len; i++ ) {
  2852. InflateData(mat,z,&i64,data_size);
  2853. data[i] = i64;
  2854. }
  2855. }
  2856. break;
  2857. }
  2858. case MAT_T_UINT64:
  2859. {
  2860. mat_uint64_t ui64;
  2861. data_size = sizeof(mat_uint64_t);
  2862. if ( mat->byteswap ) {
  2863. for ( i = 0; i < len; i++ ) {
  2864. InflateData(mat,z,&ui64,data_size);
  2865. data[i] = Mat_uint64Swap(&ui64);
  2866. }
  2867. } else {
  2868. for ( i = 0; i < len; i++ ) {
  2869. InflateData(mat,z,&ui64,data_size);
  2870. data[i] = ui64;
  2871. }
  2872. }
  2873. break;
  2874. }
  2875. case MAT_T_INT32:
  2876. {
  2877. mat_int32_t i32;
  2878. data_size = sizeof(mat_int32_t);
  2879. if ( mat->byteswap ) {
  2880. for ( i = 0; i < len; i++ ) {
  2881. InflateData(mat,z,&i32,data_size);
  2882. data[i] = Mat_int32Swap(&i32);
  2883. }
  2884. } else {
  2885. for ( i = 0; i < len; i++ ) {
  2886. InflateData(mat,z,&i32,data_size);
  2887. data[i] = i32;
  2888. }
  2889. }
  2890. break;
  2891. }
  2892. case MAT_T_UINT32:
  2893. {
  2894. mat_uint32_t ui32;
  2895. data_size = sizeof(mat_uint32_t);
  2896. if ( mat->byteswap ) {
  2897. for ( i = 0; i < len; i++ ) {
  2898. InflateData(mat,z,&ui32,data_size);
  2899. data[i] = Mat_uint32Swap(&ui32);
  2900. }
  2901. } else {
  2902. for ( i = 0; i < len; i++ ) {
  2903. InflateData(mat,z,&ui32,data_size);
  2904. data[i] = ui32;
  2905. }
  2906. }
  2907. break;
  2908. }
  2909. case MAT_T_INT16:
  2910. {
  2911. mat_int16_t i16;
  2912. data_size = sizeof(mat_int16_t);
  2913. if ( mat->byteswap ) {
  2914. for ( i = 0; i < len; i++ ) {
  2915. InflateData(mat,z,&i16,data_size);
  2916. data[i] = Mat_int16Swap(&i16);
  2917. }
  2918. } else {
  2919. for ( i = 0; i < len; i++ ) {
  2920. InflateData(mat,z,&i16,data_size);
  2921. data[i] = i16;
  2922. }
  2923. }
  2924. break;
  2925. }
  2926. case MAT_T_UINT16:
  2927. {
  2928. mat_uint16_t ui16;
  2929. data_size = sizeof(mat_uint16_t);
  2930. if ( mat->byteswap ) {
  2931. for ( i = 0; i < len; i++ ) {
  2932. InflateData(mat,z,&ui16,data_size);
  2933. data[i] = Mat_uint16Swap(&ui16);
  2934. }
  2935. } else {
  2936. for ( i = 0; i < len; i++ ) {
  2937. InflateData(mat,z,&ui16,data_size);
  2938. data[i] = ui16;
  2939. }
  2940. }
  2941. break;
  2942. }
  2943. case MAT_T_UINT8:
  2944. {
  2945. mat_uint8_t ui8;
  2946. data_size = sizeof(mat_uint8_t);
  2947. for ( i = 0; i < len; i++ ) {
  2948. InflateData(mat,z,&ui8,data_size);
  2949. data[i] = ui8;
  2950. }
  2951. break;
  2952. }
  2953. case MAT_T_INT8:
  2954. {
  2955. mat_int8_t i8;
  2956. data_size = sizeof(mat_int8_t);
  2957. for ( i = 0; i < len; i++ ) {
  2958. InflateData(mat,z,&i8,data_size);
  2959. data[i] = i8;
  2960. }
  2961. break;
  2962. }
  2963. default:
  2964. return 0;
  2965. }
  2966. nBytes = len*data_size;
  2967. return nBytes;
  2968. }
  2969. #endif /* HAVE_ZLIB */
  2970. #endif /* HAVE_MATIO_UINT64_T */
  2971. /** @brief Reads data of type @c data_type into a signed 32-bit integer type
  2972. *
  2973. * Reads from the MAT file @c len elements of data type @c data_type storing
  2974. * them as signed 32-bit integers in @c data.
  2975. * @ingroup mat_internal
  2976. * @param mat MAT file pointer
  2977. * @param data Pointer to store the output signed 32-bit integer values
  2978. * (len*sizeof(mat_int32_t))
  2979. * @param data_type one of the @c matio_types enumerations which is the source
  2980. * data type in the file
  2981. * @param len Number of elements of type @c data_type to read from the file
  2982. * @retval Number of bytes read from the file
  2983. */
  2984. static int
  2985. ReadInt32Data(mat_t *mat,mat_int32_t *data,enum matio_types data_type,int len)
  2986. {
  2987. int bytesread = 0, data_size = 0, i;
  2988. if ( (mat == NULL) || (data == NULL) || (mat->fp == NULL) )
  2989. return 0;
  2990. switch ( data_type ) {
  2991. case MAT_T_DOUBLE:
  2992. {
  2993. double d;
  2994. data_size = sizeof(double);
  2995. if ( mat->byteswap ) {
  2996. for ( i = 0; i < len; i++ ) {
  2997. bytesread += fread(&d,data_size,1,(FILE*)mat->fp);
  2998. data[i] = Mat_doubleSwap(&d);
  2999. }
  3000. } else {
  3001. for ( i = 0; i < len; i++ ) {
  3002. bytesread += fread(&d,data_size,1,(FILE*)mat->fp);
  3003. data[i] = d;
  3004. }
  3005. }
  3006. break;
  3007. }
  3008. case MAT_T_SINGLE:
  3009. {
  3010. float f;
  3011. data_size = sizeof(float);
  3012. if ( mat->byteswap ) {
  3013. for ( i = 0; i < len; i++ ) {
  3014. bytesread += fread(&f,data_size,1,(FILE*)mat->fp);
  3015. data[i] = Mat_floatSwap(&f);
  3016. }
  3017. } else {
  3018. for ( i = 0; i < len; i++ ) {
  3019. bytesread += fread(&f,data_size,1,(FILE*)mat->fp);
  3020. data[i] = f;
  3021. }
  3022. }
  3023. break;
  3024. }
  3025. case MAT_T_INT32:
  3026. {
  3027. mat_int32_t i32;
  3028. data_size = sizeof(mat_int32_t);
  3029. if ( mat->byteswap ) {
  3030. for ( i = 0; i < len; i++ ) {
  3031. bytesread += fread(&i32,data_size,1,(FILE*)mat->fp);
  3032. data[i] = Mat_int32Swap(&i32);
  3033. }
  3034. } else {
  3035. for ( i = 0; i < len; i++ ) {
  3036. bytesread += fread(&i32,data_size,1,(FILE*)mat->fp);
  3037. data[i] = i32;
  3038. }
  3039. }
  3040. break;
  3041. }
  3042. case MAT_T_UINT32:
  3043. {
  3044. mat_uint32_t ui32;
  3045. data_size = sizeof(mat_uint32_t);
  3046. if ( mat->byteswap ) {
  3047. for ( i = 0; i < len; i++ ) {
  3048. bytesread += fread(&ui32,data_size,1,(FILE*)mat->fp);
  3049. data[i] = Mat_uint32Swap(&ui32);
  3050. }
  3051. } else {
  3052. for ( i = 0; i < len; i++ ) {
  3053. bytesread += fread(&ui32,data_size,1,(FILE*)mat->fp);
  3054. data[i] = ui32;
  3055. }
  3056. }
  3057. break;
  3058. }
  3059. case MAT_T_INT16:
  3060. {
  3061. mat_int16_t i16;
  3062. data_size = sizeof(mat_int16_t);
  3063. if ( mat->byteswap ) {
  3064. for ( i = 0; i < len; i++ ) {
  3065. bytesread += fread(&i16,data_size,1,(FILE*)mat->fp);
  3066. data[i] = Mat_int16Swap(&i16);
  3067. }
  3068. } else {
  3069. for ( i = 0; i < len; i++ ) {
  3070. bytesread += fread(&i16,data_size,1,(FILE*)mat->fp);
  3071. data[i] = i16;
  3072. }
  3073. }
  3074. break;
  3075. }
  3076. case MAT_T_UINT16:
  3077. {
  3078. mat_uint16_t ui16;
  3079. data_size = sizeof(mat_uint16_t);
  3080. if ( mat->byteswap ) {
  3081. for ( i = 0; i < len; i++ ) {
  3082. bytesread += fread(&ui16,data_size,1,(FILE*)mat->fp);
  3083. data[i] = Mat_uint16Swap(&ui16);
  3084. }
  3085. } else {
  3086. for ( i = 0; i < len; i++ ) {
  3087. bytesread += fread(&ui16,data_size,1,(FILE*)mat->fp);
  3088. data[i] = ui16;
  3089. }
  3090. }
  3091. break;
  3092. }
  3093. case MAT_T_INT8:
  3094. {
  3095. mat_int8_t i8;
  3096. data_size = sizeof(mat_int8_t);
  3097. for ( i = 0; i < len; i++ ) {
  3098. bytesread += fread(&i8,data_size,1,(FILE*)mat->fp);
  3099. data[i] = i8;
  3100. }
  3101. break;
  3102. }
  3103. case MAT_T_UINT8:
  3104. {
  3105. mat_uint8_t ui8;
  3106. data_size = sizeof(mat_uint8_t);
  3107. for ( i = 0; i < len; i++ ) {
  3108. bytesread += fread(&ui8,data_size,1,(FILE*)mat->fp);
  3109. data[i] = ui8;
  3110. }
  3111. break;
  3112. }
  3113. default:
  3114. return 0;
  3115. }
  3116. bytesread *= data_size;
  3117. return bytesread;
  3118. }
  3119. #if defined(HAVE_ZLIB)
  3120. /** @brief Reads data of type @c data_type into a signed 32-bit integer type
  3121. *
  3122. * Reads from the MAT file @c len compressed elements of data type @c data_type
  3123. * storing them as signed 32-bit integers in @c data.
  3124. * @ingroup mat_internal
  3125. * @param mat MAT file pointer
  3126. * @param z Pointer to the zlib stream for inflation
  3127. * @param data Pointer to store the output signed 32-bit integer values
  3128. * (len*sizeof(mat_int32_t))
  3129. * @param data_type one of the @c matio_types enumerations which is the source
  3130. * data type in the file
  3131. * @param len Number of elements of type @c data_type to read from the file
  3132. * @retval Number of bytes read from the file
  3133. */
  3134. static int
  3135. ReadCompressedInt32Data(mat_t *mat,z_streamp z,mat_int32_t *data,
  3136. enum matio_types data_type,int len)
  3137. {
  3138. int nBytes = 0, data_size = 0, i;
  3139. if ( (mat == NULL) || (data == NULL) || (z == NULL) )
  3140. return 0;
  3141. switch ( data_type ) {
  3142. case MAT_T_DOUBLE:
  3143. {
  3144. double d;
  3145. data_size = sizeof(double);
  3146. if ( mat->byteswap ) {
  3147. for ( i = 0; i < len; i++ ) {
  3148. InflateData(mat,z,&d,data_size);
  3149. data[i] = Mat_doubleSwap(&d);
  3150. }
  3151. } else {
  3152. for ( i = 0; i < len; i++ ) {
  3153. InflateData(mat,z,&d,data_size);
  3154. data[i] = d;
  3155. }
  3156. }
  3157. break;
  3158. }
  3159. case MAT_T_SINGLE:
  3160. {
  3161. float f;
  3162. data_size = sizeof(float);
  3163. if ( mat->byteswap ) {
  3164. for ( i = 0; i < len; i++ ) {
  3165. InflateData(mat,z,&f,data_size);
  3166. data[i] = Mat_floatSwap(&f);
  3167. }
  3168. } else {
  3169. for ( i = 0; i < len; i++ ) {
  3170. InflateData(mat,z,&f,data_size);
  3171. data[i] = f;
  3172. }
  3173. }
  3174. break;
  3175. }
  3176. case MAT_T_INT32:
  3177. {
  3178. mat_int32_t i32;
  3179. data_size = sizeof(mat_int32_t);
  3180. if ( mat->byteswap ) {
  3181. for ( i = 0; i < len; i++ ) {
  3182. InflateData(mat,z,&i32,data_size);
  3183. data[i] = Mat_int32Swap(&i32);
  3184. }
  3185. } else {
  3186. for ( i = 0; i < len; i++ ) {
  3187. InflateData(mat,z,&i32,data_size);
  3188. data[i] = i32;
  3189. }
  3190. }
  3191. break;
  3192. }
  3193. case MAT_T_UINT32:
  3194. {
  3195. mat_uint32_t ui32;
  3196. data_size = sizeof(mat_uint32_t);
  3197. if ( mat->byteswap ) {
  3198. for ( i = 0; i < len; i++ ) {
  3199. InflateData(mat,z,&ui32,data_size);
  3200. data[i] = Mat_uint32Swap(&ui32);
  3201. }
  3202. } else {
  3203. for ( i = 0; i < len; i++ ) {
  3204. InflateData(mat,z,&ui32,data_size);
  3205. data[i] = ui32;
  3206. }
  3207. }
  3208. break;
  3209. }
  3210. case MAT_T_INT16:
  3211. {
  3212. mat_int16_t i16;
  3213. data_size = sizeof(mat_int16_t);
  3214. if ( mat->byteswap ) {
  3215. for ( i = 0; i < len; i++ ) {
  3216. InflateData(mat,z,&i16,data_size);
  3217. data[i] = Mat_int16Swap(&i16);
  3218. }
  3219. } else {
  3220. for ( i = 0; i < len; i++ ) {
  3221. InflateData(mat,z,&i16,data_size);
  3222. data[i] = i16;
  3223. }
  3224. }
  3225. break;
  3226. }
  3227. case MAT_T_UINT16:
  3228. {
  3229. mat_uint16_t ui16;
  3230. data_size = sizeof(mat_uint16_t);
  3231. if ( mat->byteswap ) {
  3232. for ( i = 0; i < len; i++ ) {
  3233. InflateData(mat,z,&ui16,data_size);
  3234. data[i] = Mat_uint16Swap(&ui16);
  3235. }
  3236. } else {
  3237. for ( i = 0; i < len; i++ ) {
  3238. InflateData(mat,z,&ui16,data_size);
  3239. data[i] = ui16;
  3240. }
  3241. }
  3242. break;
  3243. }
  3244. case MAT_T_UINT8:
  3245. {
  3246. mat_uint8_t ui8;
  3247. data_size = sizeof(mat_uint8_t);
  3248. for ( i = 0; i < len; i++ ) {
  3249. InflateData(mat,z,&ui8,data_size);
  3250. data[i] = ui8;
  3251. }
  3252. break;
  3253. }
  3254. case MAT_T_INT8:
  3255. {
  3256. mat_int8_t i8;
  3257. data_size = sizeof(mat_int8_t);
  3258. for ( i = 0; i < len; i++ ) {
  3259. InflateData(mat,z,&i8,data_size);
  3260. data[i] = i8;
  3261. }
  3262. break;
  3263. }
  3264. default:
  3265. return 0;
  3266. }
  3267. nBytes = len*data_size;
  3268. return nBytes;
  3269. }
  3270. #endif
  3271. /** @brief Reads data of type @c data_type into an unsigned 32-bit integer type
  3272. *
  3273. * Reads from the MAT file @c len elements of data type @c data_type storing
  3274. * them as unsigned 32-bit integers in @c data.
  3275. * @ingroup mat_internal
  3276. * @param mat MAT file pointer
  3277. * @param data Pointer to store the output unsigned 32-bit integer values
  3278. * (len*sizeof(mat_uint32_t))
  3279. * @param data_type one of the @c matio_types enumerations which is the source
  3280. * data type in the file
  3281. * @param len Number of elements of type @c data_type to read from the file
  3282. * @retval Number of bytes read from the file
  3283. */
  3284. static int
  3285. ReadUInt32Data(mat_t *mat,mat_uint32_t *data,enum matio_types data_type,int len)
  3286. {
  3287. int bytesread = 0, data_size = 0, i;
  3288. if ( (mat == NULL) || (data == NULL) || (mat->fp == NULL) )
  3289. return 0;
  3290. switch ( data_type ) {
  3291. case MAT_T_DOUBLE:
  3292. {
  3293. double d;
  3294. data_size = sizeof(double);
  3295. if ( mat->byteswap ) {
  3296. for ( i = 0; i < len; i++ ) {
  3297. bytesread += fread(&d,data_size,1,(FILE*)mat->fp);
  3298. data[i] = Mat_doubleSwap(&d);
  3299. }
  3300. } else {
  3301. for ( i = 0; i < len; i++ ) {
  3302. bytesread += fread(&d,data_size,1,(FILE*)mat->fp);
  3303. data[i] = d;
  3304. }
  3305. }
  3306. break;
  3307. }
  3308. case MAT_T_SINGLE:
  3309. {
  3310. float f;
  3311. data_size = sizeof(float);
  3312. if ( mat->byteswap ) {
  3313. for ( i = 0; i < len; i++ ) {
  3314. bytesread += fread(&f,data_size,1,(FILE*)mat->fp);
  3315. data[i] = Mat_floatSwap(&f);
  3316. }
  3317. } else {
  3318. for ( i = 0; i < len; i++ ) {
  3319. bytesread += fread(&f,data_size,1,(FILE*)mat->fp);
  3320. data[i] = f;
  3321. }
  3322. }
  3323. break;
  3324. }
  3325. case MAT_T_INT32:
  3326. {
  3327. mat_int32_t i32;
  3328. data_size = sizeof(mat_int32_t);
  3329. if ( mat->byteswap ) {
  3330. for ( i = 0; i < len; i++ ) {
  3331. bytesread += fread(&i32,data_size,1,(FILE*)mat->fp);
  3332. data[i] = Mat_int32Swap(&i32);
  3333. }
  3334. } else {
  3335. for ( i = 0; i < len; i++ ) {
  3336. bytesread += fread(&i32,data_size,1,(FILE*)mat->fp);
  3337. data[i] = i32;
  3338. }
  3339. }
  3340. break;
  3341. }
  3342. case MAT_T_UINT32:
  3343. {
  3344. mat_uint32_t ui32;
  3345. data_size = sizeof(mat_uint32_t);
  3346. if ( mat->byteswap ) {
  3347. for ( i = 0; i < len; i++ ) {
  3348. bytesread += fread(&ui32,data_size,1,(FILE*)mat->fp);
  3349. data[i] = Mat_uint32Swap(&ui32);
  3350. }
  3351. } else {
  3352. for ( i = 0; i < len; i++ ) {
  3353. bytesread += fread(&ui32,data_size,1,(FILE*)mat->fp);
  3354. data[i] = ui32;
  3355. }
  3356. }
  3357. break;
  3358. }
  3359. case MAT_T_INT16:
  3360. {
  3361. mat_int16_t i16;
  3362. data_size = sizeof(mat_int16_t);
  3363. if ( mat->byteswap ) {
  3364. for ( i = 0; i < len; i++ ) {
  3365. bytesread += fread(&i16,data_size,1,(FILE*)mat->fp);
  3366. data[i] = Mat_int16Swap(&i16);
  3367. }
  3368. } else {
  3369. for ( i = 0; i < len; i++ ) {
  3370. bytesread += fread(&i16,data_size,1,(FILE*)mat->fp);
  3371. data[i] = i16;
  3372. }
  3373. }
  3374. break;
  3375. }
  3376. case MAT_T_UINT16:
  3377. {
  3378. mat_uint16_t ui16;
  3379. data_size = sizeof(mat_uint16_t);
  3380. if ( mat->byteswap ) {
  3381. for ( i = 0; i < len; i++ ) {
  3382. bytesread += fread(&ui16,data_size,1,(FILE*)mat->fp);
  3383. data[i] = Mat_uint16Swap(&ui16);
  3384. }
  3385. } else {
  3386. for ( i = 0; i < len; i++ ) {
  3387. bytesread += fread(&ui16,data_size,1,(FILE*)mat->fp);
  3388. data[i] = ui16;
  3389. }
  3390. }
  3391. break;
  3392. }
  3393. case MAT_T_INT8:
  3394. {
  3395. mat_int8_t i8;
  3396. data_size = sizeof(mat_int8_t);
  3397. for ( i = 0; i < len; i++ ) {
  3398. bytesread += fread(&i8,data_size,1,(FILE*)mat->fp);
  3399. data[i] = i8;
  3400. }
  3401. break;
  3402. }
  3403. case MAT_T_UINT8:
  3404. {
  3405. mat_uint8_t ui8;
  3406. data_size = sizeof(mat_uint8_t);
  3407. for ( i = 0; i < len; i++ ) {
  3408. bytesread += fread(&ui8,data_size,1,(FILE*)mat->fp);
  3409. data[i] = ui8;
  3410. }
  3411. break;
  3412. }
  3413. default:
  3414. return 0;
  3415. }
  3416. bytesread *= data_size;
  3417. return bytesread;
  3418. }
  3419. #if defined(HAVE_ZLIB)
  3420. /** @brief Reads data of type @c data_type into an unsigned 32-bit integer type
  3421. *
  3422. * Reads from the MAT file @c len compressed elements of data type @c data_type
  3423. * storing them as unsigned 32-bit integers in @c data.
  3424. * @ingroup mat_internal
  3425. * @param mat MAT file pointer
  3426. * @param z Pointer to the zlib stream for inflation
  3427. * @param data Pointer to store the output unsigned 32-bit integer values
  3428. * (len*sizeof(mat_uint32_t))
  3429. * @param data_type one of the @c matio_types enumerations which is the source
  3430. * data type in the file
  3431. * @param len Number of elements of type @c data_type to read from the file
  3432. * @retval Number of bytes read from the file
  3433. */
  3434. static int
  3435. ReadCompressedUInt32Data(mat_t *mat,z_streamp z,mat_uint32_t *data,
  3436. enum matio_types data_type,int len)
  3437. {
  3438. int nBytes = 0, data_size = 0, i;
  3439. if ( (mat == NULL) || (data == NULL) || (z == NULL) )
  3440. return 0;
  3441. switch ( data_type ) {
  3442. case MAT_T_DOUBLE:
  3443. {
  3444. double d;
  3445. data_size = sizeof(double);
  3446. if ( mat->byteswap ) {
  3447. for ( i = 0; i < len; i++ ) {
  3448. InflateData(mat,z,&d,data_size);
  3449. data[i] = Mat_doubleSwap(&d);
  3450. }
  3451. } else {
  3452. for ( i = 0; i < len; i++ ) {
  3453. InflateData(mat,z,&d,data_size);
  3454. data[i] = d;
  3455. }
  3456. }
  3457. break;
  3458. }
  3459. case MAT_T_SINGLE:
  3460. {
  3461. float f;
  3462. data_size = sizeof(float);
  3463. if ( mat->byteswap ) {
  3464. for ( i = 0; i < len; i++ ) {
  3465. InflateData(mat,z,&f,data_size);
  3466. data[i] = Mat_floatSwap(&f);
  3467. }
  3468. } else {
  3469. for ( i = 0; i < len; i++ ) {
  3470. InflateData(mat,z,&f,data_size);
  3471. data[i] = f;
  3472. }
  3473. }
  3474. break;
  3475. }
  3476. case MAT_T_INT32:
  3477. {
  3478. mat_int32_t i32;
  3479. data_size = sizeof(mat_int32_t);
  3480. if ( mat->byteswap ) {
  3481. for ( i = 0; i < len; i++ ) {
  3482. InflateData(mat,z,&i32,data_size);
  3483. data[i] = Mat_int32Swap(&i32);
  3484. }
  3485. } else {
  3486. for ( i = 0; i < len; i++ ) {
  3487. InflateData(mat,z,&i32,data_size);
  3488. data[i] = i32;
  3489. }
  3490. }
  3491. break;
  3492. }
  3493. case MAT_T_UINT32:
  3494. {
  3495. mat_uint32_t ui32;
  3496. data_size = sizeof(mat_uint32_t);
  3497. if ( mat->byteswap ) {
  3498. for ( i = 0; i < len; i++ ) {
  3499. InflateData(mat,z,&ui32,data_size);
  3500. data[i] = Mat_uint32Swap(&ui32);
  3501. }
  3502. } else {
  3503. for ( i = 0; i < len; i++ ) {
  3504. InflateData(mat,z,&ui32,data_size);
  3505. data[i] = ui32;
  3506. }
  3507. }
  3508. break;
  3509. }
  3510. case MAT_T_INT16:
  3511. {
  3512. mat_int16_t i16;
  3513. data_size = sizeof(mat_int16_t);
  3514. if ( mat->byteswap ) {
  3515. for ( i = 0; i < len; i++ ) {
  3516. InflateData(mat,z,&i16,data_size);
  3517. data[i] = Mat_int16Swap(&i16);
  3518. }
  3519. } else {
  3520. for ( i = 0; i < len; i++ ) {
  3521. InflateData(mat,z,&i16,data_size);
  3522. data[i] = i16;
  3523. }
  3524. }
  3525. break;
  3526. }
  3527. case MAT_T_UINT16:
  3528. {
  3529. mat_uint16_t ui16;
  3530. data_size = sizeof(mat_uint16_t);
  3531. if ( mat->byteswap ) {
  3532. for ( i = 0; i < len; i++ ) {
  3533. InflateData(mat,z,&ui16,data_size);
  3534. data[i] = Mat_uint16Swap(&ui16);
  3535. }
  3536. } else {
  3537. for ( i = 0; i < len; i++ ) {
  3538. InflateData(mat,z,&ui16,data_size);
  3539. data[i] = ui16;
  3540. }
  3541. }
  3542. break;
  3543. }
  3544. case MAT_T_UINT8:
  3545. {
  3546. mat_uint8_t ui8;
  3547. data_size = sizeof(mat_uint8_t);
  3548. for ( i = 0; i < len; i++ ) {
  3549. InflateData(mat,z,&ui8,data_size);
  3550. data[i] = ui8;
  3551. }
  3552. break;
  3553. }
  3554. case MAT_T_INT8:
  3555. {
  3556. mat_int8_t i8;
  3557. data_size = sizeof(mat_int8_t);
  3558. for ( i = 0; i < len; i++ ) {
  3559. InflateData(mat,z,&i8,data_size);
  3560. data[i] = i8;
  3561. }
  3562. break;
  3563. }
  3564. default:
  3565. return 0;
  3566. }
  3567. nBytes = len*data_size;
  3568. return nBytes;
  3569. }
  3570. #endif
  3571. /** @brief Reads data of type @c data_type into a signed 16-bit integer type
  3572. *
  3573. * Reads from the MAT file @c len elements of data type @c data_type storing
  3574. * them as signed 16-bit integers in @c data.
  3575. * @ingroup mat_internal
  3576. * @param mat MAT file pointer
  3577. * @param data Pointer to store the output signed 16-bit integer values
  3578. * (len*sizeof(mat_int16_t))
  3579. * @param data_type one of the @c matio_types enumerations which is the source
  3580. * data type in the file
  3581. * @param len Number of elements of type @c data_type to read from the file
  3582. * @retval Number of bytes read from the file
  3583. */
  3584. static int
  3585. ReadInt16Data(mat_t *mat,mat_int16_t *data,enum matio_types data_type,int len)
  3586. {
  3587. int bytesread = 0, data_size = 0, i;
  3588. if ( (mat == NULL) || (data == NULL) || (mat->fp == NULL) )
  3589. return 0;
  3590. switch ( data_type ) {
  3591. case MAT_T_DOUBLE:
  3592. {
  3593. double d;
  3594. data_size = sizeof(double);
  3595. if ( mat->byteswap ) {
  3596. for ( i = 0; i < len; i++ ) {
  3597. bytesread += fread(&d,data_size,1,(FILE*)mat->fp);
  3598. data[i] = Mat_doubleSwap(&d);
  3599. }
  3600. } else {
  3601. for ( i = 0; i < len; i++ ) {
  3602. bytesread += fread(&d,data_size,1,(FILE*)mat->fp);
  3603. data[i] = d;
  3604. }
  3605. }
  3606. break;
  3607. }
  3608. case MAT_T_SINGLE:
  3609. {
  3610. float f;
  3611. data_size = sizeof(float);
  3612. if ( mat->byteswap ) {
  3613. for ( i = 0; i < len; i++ ) {
  3614. bytesread += fread(&f,data_size,1,(FILE*)mat->fp);
  3615. data[i] = Mat_floatSwap(&f);
  3616. }
  3617. } else {
  3618. for ( i = 0; i < len; i++ ) {
  3619. bytesread += fread(&f,data_size,1,(FILE*)mat->fp);
  3620. data[i] = f;
  3621. }
  3622. }
  3623. break;
  3624. }
  3625. case MAT_T_INT32:
  3626. {
  3627. mat_int32_t i32;
  3628. data_size = sizeof(mat_int32_t);
  3629. if ( mat->byteswap ) {
  3630. for ( i = 0; i < len; i++ ) {
  3631. bytesread += fread(&i32,data_size,1,(FILE*)mat->fp);
  3632. data[i] = Mat_int32Swap(&i32);
  3633. }
  3634. } else {
  3635. for ( i = 0; i < len; i++ ) {
  3636. bytesread += fread(&i32,data_size,1,(FILE*)mat->fp);
  3637. data[i] = i32;
  3638. }
  3639. }
  3640. break;
  3641. }
  3642. case MAT_T_UINT32:
  3643. {
  3644. mat_uint32_t ui32;
  3645. data_size = sizeof(mat_uint32_t);
  3646. if ( mat->byteswap ) {
  3647. for ( i = 0; i < len; i++ ) {
  3648. bytesread += fread(&ui32,data_size,1,(FILE*)mat->fp);
  3649. data[i] = Mat_uint32Swap(&ui32);
  3650. }
  3651. } else {
  3652. for ( i = 0; i < len; i++ ) {
  3653. bytesread += fread(&ui32,data_size,1,(FILE*)mat->fp);
  3654. data[i] = ui32;
  3655. }
  3656. }
  3657. break;
  3658. }
  3659. case MAT_T_INT16:
  3660. {
  3661. mat_int16_t i16;
  3662. data_size = sizeof(mat_int16_t);
  3663. if ( mat->byteswap ) {
  3664. for ( i = 0; i < len; i++ ) {
  3665. bytesread += fread(&i16,data_size,1,(FILE*)mat->fp);
  3666. data[i] = Mat_int16Swap(&i16);
  3667. }
  3668. } else {
  3669. for ( i = 0; i < len; i++ ) {
  3670. bytesread += fread(&i16,data_size,1,(FILE*)mat->fp);
  3671. data[i] = i16;
  3672. }
  3673. }
  3674. break;
  3675. }
  3676. case MAT_T_UINT16:
  3677. {
  3678. mat_uint16_t ui16;
  3679. data_size = sizeof(mat_uint16_t);
  3680. if ( mat->byteswap ) {
  3681. for ( i = 0; i < len; i++ ) {
  3682. bytesread += fread(&ui16,data_size,1,(FILE*)mat->fp);
  3683. data[i] = Mat_uint16Swap(&ui16);
  3684. }
  3685. } else {
  3686. for ( i = 0; i < len; i++ ) {
  3687. bytesread += fread(&ui16,data_size,1,(FILE*)mat->fp);
  3688. data[i] = ui16;
  3689. }
  3690. }
  3691. break;
  3692. }
  3693. case MAT_T_INT8:
  3694. {
  3695. mat_int8_t i8;
  3696. data_size = sizeof(mat_int8_t);
  3697. for ( i = 0; i < len; i++ ) {
  3698. bytesread += fread(&i8,data_size,1,(FILE*)mat->fp);
  3699. data[i] = i8;
  3700. }
  3701. break;
  3702. }
  3703. case MAT_T_UINT8:
  3704. {
  3705. mat_uint8_t ui8;
  3706. data_size = sizeof(mat_uint8_t);
  3707. for ( i = 0; i < len; i++ ) {
  3708. bytesread += fread(&ui8,data_size,1,(FILE*)mat->fp);
  3709. data[i] = ui8;
  3710. }
  3711. break;
  3712. }
  3713. default:
  3714. return 0;
  3715. }
  3716. bytesread *= data_size;
  3717. return bytesread;
  3718. }
  3719. #if defined(HAVE_ZLIB)
  3720. /** @brief Reads data of type @c data_type into a signed 16-bit integer type
  3721. *
  3722. * Reads from the MAT file @c len compressed elements of data type @c data_type
  3723. * storing them as signed 16-bit integers in @c data.
  3724. * @ingroup mat_internal
  3725. * @param mat MAT file pointer
  3726. * @param z Pointer to the zlib stream for inflation
  3727. * @param data Pointer to store the output signed 16-bit integer values
  3728. * (len*sizeof(mat_int16_t))
  3729. * @param data_type one of the @c matio_types enumerations which is the source
  3730. * data type in the file
  3731. * @param len Number of elements of type @c data_type to read from the file
  3732. * @retval Number of bytes read from the file
  3733. */
  3734. static int
  3735. ReadCompressedInt16Data(mat_t *mat,z_streamp z,mat_int16_t *data,
  3736. enum matio_types data_type,int len)
  3737. {
  3738. int nBytes = 0, data_size = 0, i;
  3739. if ( (mat == NULL) || (data == NULL) || (z == NULL) )
  3740. return 0;
  3741. switch ( data_type ) {
  3742. case MAT_T_DOUBLE:
  3743. {
  3744. double d;
  3745. data_size = sizeof(double);
  3746. if ( mat->byteswap ) {
  3747. for ( i = 0; i < len; i++ ) {
  3748. InflateData(mat,z,&d,data_size);
  3749. data[i] = Mat_doubleSwap(&d);
  3750. }
  3751. } else {
  3752. for ( i = 0; i < len; i++ ) {
  3753. InflateData(mat,z,&d,data_size);
  3754. data[i] = d;
  3755. }
  3756. }
  3757. break;
  3758. }
  3759. case MAT_T_SINGLE:
  3760. {
  3761. float f;
  3762. data_size = sizeof(float);
  3763. if ( mat->byteswap ) {
  3764. for ( i = 0; i < len; i++ ) {
  3765. InflateData(mat,z,&f,data_size);
  3766. data[i] = Mat_floatSwap(&f);
  3767. }
  3768. } else {
  3769. for ( i = 0; i < len; i++ ) {
  3770. InflateData(mat,z,&f,data_size);
  3771. data[i] = f;
  3772. }
  3773. }
  3774. break;
  3775. }
  3776. case MAT_T_INT32:
  3777. {
  3778. mat_int32_t i32;
  3779. data_size = sizeof(mat_int32_t);
  3780. if ( mat->byteswap ) {
  3781. for ( i = 0; i < len; i++ ) {
  3782. InflateData(mat,z,&i32,data_size);
  3783. data[i] = Mat_int32Swap(&i32);
  3784. }
  3785. } else {
  3786. for ( i = 0; i < len; i++ ) {
  3787. InflateData(mat,z,&i32,data_size);
  3788. data[i] = i32;
  3789. }
  3790. }
  3791. break;
  3792. }
  3793. case MAT_T_UINT32:
  3794. {
  3795. mat_uint32_t ui32;
  3796. data_size = sizeof(mat_uint32_t);
  3797. if ( mat->byteswap ) {
  3798. for ( i = 0; i < len; i++ ) {
  3799. InflateData(mat,z,&ui32,data_size);
  3800. data[i] = Mat_uint32Swap(&ui32);
  3801. }
  3802. } else {
  3803. for ( i = 0; i < len; i++ ) {
  3804. InflateData(mat,z,&ui32,data_size);
  3805. data[i] = ui32;
  3806. }
  3807. }
  3808. break;
  3809. }
  3810. case MAT_T_INT16:
  3811. {
  3812. mat_int16_t i16;
  3813. data_size = sizeof(mat_int16_t);
  3814. if ( mat->byteswap ) {
  3815. for ( i = 0; i < len; i++ ) {
  3816. InflateData(mat,z,&i16,data_size);
  3817. data[i] = Mat_int16Swap(&i16);
  3818. }
  3819. } else {
  3820. for ( i = 0; i < len; i++ ) {
  3821. InflateData(mat,z,&i16,data_size);
  3822. data[i] = i16;
  3823. }
  3824. }
  3825. break;
  3826. }
  3827. case MAT_T_UINT16:
  3828. {
  3829. mat_uint16_t ui16;
  3830. data_size = sizeof(mat_uint16_t);
  3831. if ( mat->byteswap ) {
  3832. for ( i = 0; i < len; i++ ) {
  3833. InflateData(mat,z,&ui16,data_size);
  3834. data[i] = Mat_uint16Swap(&ui16);
  3835. }
  3836. } else {
  3837. for ( i = 0; i < len; i++ ) {
  3838. InflateData(mat,z,&ui16,data_size);
  3839. data[i] = ui16;
  3840. }
  3841. }
  3842. break;
  3843. }
  3844. case MAT_T_UINT8:
  3845. {
  3846. mat_uint8_t ui8;
  3847. data_size = sizeof(mat_uint8_t);
  3848. for ( i = 0; i < len; i++ ) {
  3849. InflateData(mat,z,&ui8,data_size);
  3850. data[i] = ui8;
  3851. }
  3852. break;
  3853. }
  3854. case MAT_T_INT8:
  3855. {
  3856. mat_int8_t i8;
  3857. data_size = sizeof(mat_int8_t);
  3858. for ( i = 0; i < len; i++ ) {
  3859. InflateData(mat,z,&i8,data_size);
  3860. data[i] = i8;
  3861. }
  3862. break;
  3863. }
  3864. default:
  3865. return 0;
  3866. }
  3867. nBytes = len*data_size;
  3868. return nBytes;
  3869. }
  3870. #endif
  3871. /** @brief Reads data of type @c data_type into an unsigned 16-bit integer type
  3872. *
  3873. * Reads from the MAT file @c len elements of data type @c data_type storing
  3874. * them as unsigned 16-bit integers in @c data.
  3875. * @ingroup mat_internal
  3876. * @param mat MAT file pointer
  3877. * @param data Pointer to store the output unsigned 16-bit integer values
  3878. * (len*sizeof(mat_uint16_t))
  3879. * @param data_type one of the @c matio_types enumerations which is the source
  3880. * data type in the file
  3881. * @param len Number of elements of type @c data_type to read from the file
  3882. * @retval Number of bytes read from the file
  3883. */
  3884. static int
  3885. ReadUInt16Data(mat_t *mat,mat_uint16_t *data,enum matio_types data_type,int len)
  3886. {
  3887. int bytesread = 0, data_size = 0, i;
  3888. if ( (mat == NULL) || (data == NULL) || (mat->fp == NULL) )
  3889. return 0;
  3890. switch ( data_type ) {
  3891. case MAT_T_DOUBLE:
  3892. {
  3893. double d;
  3894. data_size = sizeof(double);
  3895. if ( mat->byteswap ) {
  3896. for ( i = 0; i < len; i++ ) {
  3897. bytesread += fread(&d,data_size,1,(FILE*)mat->fp);
  3898. data[i] = Mat_doubleSwap(&d);
  3899. }
  3900. } else {
  3901. for ( i = 0; i < len; i++ ) {
  3902. bytesread += fread(&d,data_size,1,(FILE*)mat->fp);
  3903. data[i] = d;
  3904. }
  3905. }
  3906. break;
  3907. }
  3908. case MAT_T_SINGLE:
  3909. {
  3910. float f;
  3911. data_size = sizeof(float);
  3912. if ( mat->byteswap ) {
  3913. for ( i = 0; i < len; i++ ) {
  3914. bytesread += fread(&f,data_size,1,(FILE*)mat->fp);
  3915. data[i] = Mat_floatSwap(&f);
  3916. }
  3917. } else {
  3918. for ( i = 0; i < len; i++ ) {
  3919. bytesread += fread(&f,data_size,1,(FILE*)mat->fp);
  3920. data[i] = f;
  3921. }
  3922. }
  3923. break;
  3924. }
  3925. case MAT_T_INT32:
  3926. {
  3927. mat_int32_t i32;
  3928. data_size = sizeof(mat_int32_t);
  3929. if ( mat->byteswap ) {
  3930. for ( i = 0; i < len; i++ ) {
  3931. bytesread += fread(&i32,data_size,1,(FILE*)mat->fp);
  3932. data[i] = Mat_int32Swap(&i32);
  3933. }
  3934. } else {
  3935. for ( i = 0; i < len; i++ ) {
  3936. bytesread += fread(&i32,data_size,1,(FILE*)mat->fp);
  3937. data[i] = i32;
  3938. }
  3939. }
  3940. break;
  3941. }
  3942. case MAT_T_UINT32:
  3943. {
  3944. mat_uint32_t ui32;
  3945. data_size = sizeof(mat_uint32_t);
  3946. if ( mat->byteswap ) {
  3947. for ( i = 0; i < len; i++ ) {
  3948. bytesread += fread(&ui32,data_size,1,(FILE*)mat->fp);
  3949. data[i] = Mat_uint32Swap(&ui32);
  3950. }
  3951. } else {
  3952. for ( i = 0; i < len; i++ ) {
  3953. bytesread += fread(&ui32,data_size,1,(FILE*)mat->fp);
  3954. data[i] = ui32;
  3955. }
  3956. }
  3957. break;
  3958. }
  3959. case MAT_T_INT16:
  3960. {
  3961. mat_int16_t i16;
  3962. data_size = sizeof(mat_int16_t);
  3963. if ( mat->byteswap ) {
  3964. for ( i = 0; i < len; i++ ) {
  3965. bytesread += fread(&i16,data_size,1,(FILE*)mat->fp);
  3966. data[i] = Mat_int16Swap(&i16);
  3967. }
  3968. } else {
  3969. for ( i = 0; i < len; i++ ) {
  3970. bytesread += fread(&i16,data_size,1,(FILE*)mat->fp);
  3971. data[i] = i16;
  3972. }
  3973. }
  3974. break;
  3975. }
  3976. case MAT_T_UINT16:
  3977. {
  3978. mat_uint16_t ui16;
  3979. data_size = sizeof(mat_uint16_t);
  3980. if ( mat->byteswap ) {
  3981. for ( i = 0; i < len; i++ ) {
  3982. bytesread += fread(&ui16,data_size,1,(FILE*)mat->fp);
  3983. data[i] = Mat_uint16Swap(&ui16);
  3984. }
  3985. } else {
  3986. for ( i = 0; i < len; i++ ) {
  3987. bytesread += fread(&ui16,data_size,1,(FILE*)mat->fp);
  3988. data[i] = ui16;
  3989. }
  3990. }
  3991. break;
  3992. }
  3993. case MAT_T_INT8:
  3994. {
  3995. mat_int8_t i8;
  3996. data_size = sizeof(mat_int8_t);
  3997. for ( i = 0; i < len; i++ ) {
  3998. bytesread += fread(&i8,data_size,1,(FILE*)mat->fp);
  3999. data[i] = i8;
  4000. }
  4001. break;
  4002. }
  4003. case MAT_T_UINT8:
  4004. {
  4005. mat_uint8_t ui8;
  4006. data_size = sizeof(mat_uint8_t);
  4007. for ( i = 0; i < len; i++ ) {
  4008. bytesread += fread(&ui8,data_size,1,(FILE*)mat->fp);
  4009. data[i] = ui8;
  4010. }
  4011. break;
  4012. }
  4013. default:
  4014. return 0;
  4015. }
  4016. bytesread *= data_size;
  4017. return bytesread;
  4018. }
  4019. #if defined(HAVE_ZLIB)
  4020. /** @brief Reads data of type @c data_type into an unsigned 16-bit integer type
  4021. *
  4022. * Reads from the MAT file @c len compressed elements of data type @c data_type
  4023. * storing them as unsigned 16-bit integers in @c data.
  4024. * @ingroup mat_internal
  4025. * @param mat MAT file pointer
  4026. * @param z Pointer to the zlib stream for inflation
  4027. * @param data Pointer to store the output n unsigned 16-bit integer values
  4028. * (len*sizeof(mat_uint16_t))
  4029. * @param data_type one of the @c matio_types enumerations which is the source
  4030. * data type in the file
  4031. * @param len Number of elements of type @c data_type to read from the file
  4032. * @retval Number of bytes read from the file
  4033. */
  4034. static int
  4035. ReadCompressedUInt16Data(mat_t *mat,z_streamp z,mat_uint16_t *data,
  4036. enum matio_types data_type,int len)
  4037. {
  4038. int nBytes = 0, data_size = 0, i;
  4039. if ( (mat == NULL) || (data == NULL) || (z == NULL) )
  4040. return 0;
  4041. switch ( data_type ) {
  4042. case MAT_T_DOUBLE:
  4043. {
  4044. double d;
  4045. data_size = sizeof(double);
  4046. if ( mat->byteswap ) {
  4047. for ( i = 0; i < len; i++ ) {
  4048. InflateData(mat,z,&d,data_size);
  4049. data[i] = Mat_doubleSwap(&d);
  4050. }
  4051. } else {
  4052. for ( i = 0; i < len; i++ ) {
  4053. InflateData(mat,z,&d,data_size);
  4054. data[i] = d;
  4055. }
  4056. }
  4057. break;
  4058. }
  4059. case MAT_T_SINGLE:
  4060. {
  4061. float f;
  4062. data_size = sizeof(float);
  4063. if ( mat->byteswap ) {
  4064. for ( i = 0; i < len; i++ ) {
  4065. InflateData(mat,z,&f,data_size);
  4066. data[i] = Mat_floatSwap(&f);
  4067. }
  4068. } else {
  4069. for ( i = 0; i < len; i++ ) {
  4070. InflateData(mat,z,&f,data_size);
  4071. data[i] = f;
  4072. }
  4073. }
  4074. break;
  4075. }
  4076. case MAT_T_INT32:
  4077. {
  4078. mat_int32_t i32;
  4079. data_size = sizeof(mat_int32_t);
  4080. if ( mat->byteswap ) {
  4081. for ( i = 0; i < len; i++ ) {
  4082. InflateData(mat,z,&i32,data_size);
  4083. data[i] = Mat_int32Swap(&i32);
  4084. }
  4085. } else {
  4086. for ( i = 0; i < len; i++ ) {
  4087. InflateData(mat,z,&i32,data_size);
  4088. data[i] = i32;
  4089. }
  4090. }
  4091. break;
  4092. }
  4093. case MAT_T_UINT32:
  4094. {
  4095. mat_uint32_t ui32;
  4096. data_size = sizeof(mat_uint32_t);
  4097. if ( mat->byteswap ) {
  4098. for ( i = 0; i < len; i++ ) {
  4099. InflateData(mat,z,&ui32,data_size);
  4100. data[i] = Mat_uint32Swap(&ui32);
  4101. }
  4102. } else {
  4103. for ( i = 0; i < len; i++ ) {
  4104. InflateData(mat,z,&ui32,data_size);
  4105. data[i] = ui32;
  4106. }
  4107. }
  4108. break;
  4109. }
  4110. case MAT_T_INT16:
  4111. {
  4112. mat_int16_t i16;
  4113. data_size = sizeof(mat_int16_t);
  4114. if ( mat->byteswap ) {
  4115. for ( i = 0; i < len; i++ ) {
  4116. InflateData(mat,z,&i16,data_size);
  4117. data[i] = Mat_int16Swap(&i16);
  4118. }
  4119. } else {
  4120. for ( i = 0; i < len; i++ ) {
  4121. InflateData(mat,z,&i16,data_size);
  4122. data[i] = i16;
  4123. }
  4124. }
  4125. break;
  4126. }
  4127. case MAT_T_UINT16:
  4128. {
  4129. mat_uint16_t ui16;
  4130. data_size = sizeof(mat_uint16_t);
  4131. if ( mat->byteswap ) {
  4132. for ( i = 0; i < len; i++ ) {
  4133. InflateData(mat,z,&ui16,data_size);
  4134. data[i] = Mat_uint16Swap(&ui16);
  4135. }
  4136. } else {
  4137. for ( i = 0; i < len; i++ ) {
  4138. InflateData(mat,z,&ui16,data_size);
  4139. data[i] = ui16;
  4140. }
  4141. }
  4142. break;
  4143. }
  4144. case MAT_T_UINT8:
  4145. {
  4146. mat_uint8_t ui8;
  4147. data_size = sizeof(mat_uint8_t);
  4148. for ( i = 0; i < len; i++ ) {
  4149. InflateData(mat,z,&ui8,data_size);
  4150. data[i] = ui8;
  4151. }
  4152. break;
  4153. }
  4154. case MAT_T_INT8:
  4155. {
  4156. mat_int8_t i8;
  4157. data_size = sizeof(mat_int8_t);
  4158. for ( i = 0; i < len; i++ ) {
  4159. InflateData(mat,z,&i8,data_size);
  4160. data[i] = i8;
  4161. }
  4162. break;
  4163. }
  4164. default:
  4165. return 0;
  4166. }
  4167. nBytes = len*data_size;
  4168. return nBytes;
  4169. }
  4170. #endif
  4171. /** @brief Reads data of type @c data_type into a signed 8-bit integer type
  4172. *
  4173. * Reads from the MAT file @c len elements of data type @c data_type storing
  4174. * them as signed 8-bit integers in @c data.
  4175. * @ingroup mat_internal
  4176. * @param mat MAT file pointer
  4177. * @param data Pointer to store the output signed 8-bit integer values
  4178. * (len*sizeof(mat_int8_t))
  4179. * @param data_type one of the @c matio_types enumerations which is the source
  4180. * data type in the file
  4181. * @param len Number of elements of type @c data_type to read from the file
  4182. * @retval Number of bytes read from the file
  4183. */
  4184. static int
  4185. ReadInt8Data(mat_t *mat,mat_int8_t *data,enum matio_types data_type,int len)
  4186. {
  4187. int bytesread = 0, data_size = 0, i;
  4188. if ( (mat == NULL) || (data == NULL) || (mat->fp == NULL) )
  4189. return 0;
  4190. switch ( data_type ) {
  4191. case MAT_T_DOUBLE:
  4192. {
  4193. double d;
  4194. data_size = sizeof(double);
  4195. if ( mat->byteswap ) {
  4196. for ( i = 0; i < len; i++ ) {
  4197. bytesread += fread(&d,data_size,1,(FILE*)mat->fp);
  4198. data[i] = Mat_doubleSwap(&d);
  4199. }
  4200. } else {
  4201. for ( i = 0; i < len; i++ ) {
  4202. bytesread += fread(&d,data_size,1,(FILE*)mat->fp);
  4203. data[i] = d;
  4204. }
  4205. }
  4206. break;
  4207. }
  4208. case MAT_T_SINGLE:
  4209. {
  4210. float f;
  4211. data_size = sizeof(float);
  4212. if ( mat->byteswap ) {
  4213. for ( i = 0; i < len; i++ ) {
  4214. bytesread += fread(&f,data_size,1,(FILE*)mat->fp);
  4215. data[i] = Mat_floatSwap(&f);
  4216. }
  4217. } else {
  4218. for ( i = 0; i < len; i++ ) {
  4219. bytesread += fread(&f,data_size,1,(FILE*)mat->fp);
  4220. data[i] = f;
  4221. }
  4222. }
  4223. break;
  4224. }
  4225. case MAT_T_INT32:
  4226. {
  4227. mat_int32_t i32;
  4228. data_size = sizeof(mat_int32_t);
  4229. if ( mat->byteswap ) {
  4230. for ( i = 0; i < len; i++ ) {
  4231. bytesread += fread(&i32,data_size,1,(FILE*)mat->fp);
  4232. data[i] = Mat_int32Swap(&i32);
  4233. }
  4234. } else {
  4235. for ( i = 0; i < len; i++ ) {
  4236. bytesread += fread(&i32,data_size,1,(FILE*)mat->fp);
  4237. data[i] = i32;
  4238. }
  4239. }
  4240. break;
  4241. }
  4242. case MAT_T_UINT32:
  4243. {
  4244. mat_uint32_t ui32;
  4245. data_size = sizeof(mat_uint32_t);
  4246. if ( mat->byteswap ) {
  4247. for ( i = 0; i < len; i++ ) {
  4248. bytesread += fread(&ui32,data_size,1,(FILE*)mat->fp);
  4249. data[i] = Mat_uint32Swap(&ui32);
  4250. }
  4251. } else {
  4252. for ( i = 0; i < len; i++ ) {
  4253. bytesread += fread(&ui32,data_size,1,(FILE*)mat->fp);
  4254. data[i] = ui32;
  4255. }
  4256. }
  4257. break;
  4258. }
  4259. case MAT_T_INT16:
  4260. {
  4261. mat_int16_t i16;
  4262. data_size = sizeof(mat_int16_t);
  4263. if ( mat->byteswap ) {
  4264. for ( i = 0; i < len; i++ ) {
  4265. bytesread += fread(&i16,data_size,1,(FILE*)mat->fp);
  4266. data[i] = Mat_int16Swap(&i16);
  4267. }
  4268. } else {
  4269. for ( i = 0; i < len; i++ ) {
  4270. bytesread += fread(&i16,data_size,1,(FILE*)mat->fp);
  4271. data[i] = i16;
  4272. }
  4273. }
  4274. break;
  4275. }
  4276. case MAT_T_UINT16:
  4277. {
  4278. mat_uint16_t ui16;
  4279. data_size = sizeof(mat_uint16_t);
  4280. if ( mat->byteswap ) {
  4281. for ( i = 0; i < len; i++ ) {
  4282. bytesread += fread(&ui16,data_size,1,(FILE*)mat->fp);
  4283. data[i] = Mat_uint16Swap(&ui16);
  4284. }
  4285. } else {
  4286. for ( i = 0; i < len; i++ ) {
  4287. bytesread += fread(&ui16,data_size,1,(FILE*)mat->fp);
  4288. data[i] = ui16;
  4289. }
  4290. }
  4291. break;
  4292. }
  4293. case MAT_T_INT8:
  4294. {
  4295. mat_int8_t i8;
  4296. data_size = sizeof(mat_int8_t);
  4297. for ( i = 0; i < len; i++ ) {
  4298. bytesread += fread(&i8,data_size,1,(FILE*)mat->fp);
  4299. data[i] = i8;
  4300. }
  4301. break;
  4302. }
  4303. case MAT_T_UINT8:
  4304. {
  4305. mat_uint8_t ui8;
  4306. data_size = sizeof(mat_uint8_t);
  4307. for ( i = 0; i < len; i++ ) {
  4308. bytesread += fread(&ui8,data_size,1,(FILE*)mat->fp);
  4309. data[i] = ui8;
  4310. }
  4311. break;
  4312. }
  4313. default:
  4314. return 0;
  4315. }
  4316. bytesread *= data_size;
  4317. return bytesread;
  4318. }
  4319. #if defined(HAVE_ZLIB)
  4320. /** @brief Reads data of type @c data_type into a signed 8-bit integer type
  4321. *
  4322. * Reads from the MAT file @c len compressed elements of data type @c data_type
  4323. * storing them as signed 8-bit integers in @c data.
  4324. * @ingroup mat_internal
  4325. * @param mat MAT file pointer
  4326. * @param z Pointer to the zlib stream for inflation
  4327. * @param data Pointer to store the output signed 8-bit integer values
  4328. * (len*sizeof(mat_int8_t))
  4329. * @param data_type one of the @c matio_types enumerations which is the source
  4330. * data type in the file
  4331. * @param len Number of elements of type @c data_type to read from the file
  4332. * @retval Number of bytes read from the file
  4333. */
  4334. static int
  4335. ReadCompressedInt8Data(mat_t *mat,z_streamp z,mat_int8_t *data,
  4336. enum matio_types data_type,int len)
  4337. {
  4338. int nBytes = 0, data_size = 0, i;
  4339. if ( (mat == NULL) || (data == NULL) || (z == NULL) )
  4340. return 0;
  4341. switch ( data_type ) {
  4342. case MAT_T_DOUBLE:
  4343. {
  4344. double d;
  4345. data_size = sizeof(double);
  4346. if ( mat->byteswap ) {
  4347. for ( i = 0; i < len; i++ ) {
  4348. InflateData(mat,z,&d,data_size);
  4349. data[i] = Mat_doubleSwap(&d);
  4350. }
  4351. } else {
  4352. for ( i = 0; i < len; i++ ) {
  4353. InflateData(mat,z,&d,data_size);
  4354. data[i] = d;
  4355. }
  4356. }
  4357. break;
  4358. }
  4359. case MAT_T_SINGLE:
  4360. {
  4361. float f;
  4362. data_size = sizeof(float);
  4363. if ( mat->byteswap ) {
  4364. for ( i = 0; i < len; i++ ) {
  4365. InflateData(mat,z,&f,data_size);
  4366. data[i] = Mat_floatSwap(&f);
  4367. }
  4368. } else {
  4369. for ( i = 0; i < len; i++ ) {
  4370. InflateData(mat,z,&f,data_size);
  4371. data[i] = f;
  4372. }
  4373. }
  4374. break;
  4375. }
  4376. case MAT_T_INT32:
  4377. {
  4378. mat_int32_t i32;
  4379. data_size = sizeof(mat_int32_t);
  4380. if ( mat->byteswap ) {
  4381. for ( i = 0; i < len; i++ ) {
  4382. InflateData(mat,z,&i32,data_size);
  4383. data[i] = Mat_int32Swap(&i32);
  4384. }
  4385. } else {
  4386. for ( i = 0; i < len; i++ ) {
  4387. InflateData(mat,z,&i32,data_size);
  4388. data[i] = i32;
  4389. }
  4390. }
  4391. break;
  4392. }
  4393. case MAT_T_UINT32:
  4394. {
  4395. mat_uint32_t ui32;
  4396. data_size = sizeof(mat_uint32_t);
  4397. if ( mat->byteswap ) {
  4398. for ( i = 0; i < len; i++ ) {
  4399. InflateData(mat,z,&ui32,data_size);
  4400. data[i] = Mat_uint32Swap(&ui32);
  4401. }
  4402. } else {
  4403. for ( i = 0; i < len; i++ ) {
  4404. InflateData(mat,z,&ui32,data_size);
  4405. data[i] = ui32;
  4406. }
  4407. }
  4408. break;
  4409. }
  4410. case MAT_T_INT16:
  4411. {
  4412. mat_int16_t i16;
  4413. data_size = sizeof(mat_int16_t);
  4414. if ( mat->byteswap ) {
  4415. for ( i = 0; i < len; i++ ) {
  4416. InflateData(mat,z,&i16,data_size);
  4417. data[i] = Mat_int16Swap(&i16);
  4418. }
  4419. } else {
  4420. for ( i = 0; i < len; i++ ) {
  4421. InflateData(mat,z,&i16,data_size);
  4422. data[i] = i16;
  4423. }
  4424. }
  4425. break;
  4426. }
  4427. case MAT_T_UINT16:
  4428. {
  4429. mat_uint16_t ui16;
  4430. data_size = sizeof(mat_uint16_t);
  4431. if ( mat->byteswap ) {
  4432. for ( i = 0; i < len; i++ ) {
  4433. InflateData(mat,z,&ui16,data_size);
  4434. data[i] = Mat_uint16Swap(&ui16);
  4435. }
  4436. } else {
  4437. for ( i = 0; i < len; i++ ) {
  4438. InflateData(mat,z,&ui16,data_size);
  4439. data[i] = ui16;
  4440. }
  4441. }
  4442. break;
  4443. }
  4444. case MAT_T_UINT8:
  4445. {
  4446. mat_uint8_t ui8;
  4447. data_size = sizeof(mat_uint8_t);
  4448. for ( i = 0; i < len; i++ ) {
  4449. InflateData(mat,z,&ui8,data_size);
  4450. data[i] = ui8;
  4451. }
  4452. break;
  4453. }
  4454. case MAT_T_INT8:
  4455. {
  4456. mat_int8_t i8;
  4457. data_size = sizeof(mat_int8_t);
  4458. for ( i = 0; i < len; i++ ) {
  4459. InflateData(mat,z,&i8,data_size);
  4460. data[i] = i8;
  4461. }
  4462. break;
  4463. }
  4464. default:
  4465. return 0;
  4466. }
  4467. nBytes = len*data_size;
  4468. return nBytes;
  4469. }
  4470. #endif
  4471. /** @brief Reads data of type @c data_type into an unsigned 8-bit integer type
  4472. *
  4473. * Reads from the MAT file @c len elements of data type @c data_type storing
  4474. * them as unsigned 8-bit integers in @c data.
  4475. * @ingroup mat_internal
  4476. * @param mat MAT file pointer
  4477. * @param data Pointer to store the output unsigned 8-bit integer values
  4478. * (len*sizeof(mat_uint8_t))
  4479. * @param data_type one of the @c matio_types enumerations which is the source
  4480. * data type in the file
  4481. * @param len Number of elements of type @c data_type to read from the file
  4482. * @retval Number of bytes read from the file
  4483. */
  4484. static int
  4485. ReadUInt8Data(mat_t *mat,mat_uint8_t *data,enum matio_types data_type,int len)
  4486. {
  4487. int bytesread = 0, data_size = 0, i;
  4488. if ( (mat == NULL) || (data == NULL) || (mat->fp == NULL) )
  4489. return 0;
  4490. switch ( data_type ) {
  4491. case MAT_T_DOUBLE:
  4492. {
  4493. double d;
  4494. data_size = sizeof(double);
  4495. if ( mat->byteswap ) {
  4496. for ( i = 0; i < len; i++ ) {
  4497. bytesread += fread(&d,data_size,1,(FILE*)mat->fp);
  4498. data[i] = Mat_doubleSwap(&d);
  4499. }
  4500. } else {
  4501. for ( i = 0; i < len; i++ ) {
  4502. bytesread += fread(&d,data_size,1,(FILE*)mat->fp);
  4503. data[i] = d;
  4504. }
  4505. }
  4506. break;
  4507. }
  4508. case MAT_T_SINGLE:
  4509. {
  4510. float f;
  4511. data_size = sizeof(float);
  4512. if ( mat->byteswap ) {
  4513. for ( i = 0; i < len; i++ ) {
  4514. bytesread += fread(&f,data_size,1,(FILE*)mat->fp);
  4515. data[i] = Mat_floatSwap(&f);
  4516. }
  4517. } else {
  4518. for ( i = 0; i < len; i++ ) {
  4519. bytesread += fread(&f,data_size,1,(FILE*)mat->fp);
  4520. data[i] = f;
  4521. }
  4522. }
  4523. break;
  4524. }
  4525. case MAT_T_INT32:
  4526. {
  4527. mat_int32_t i32;
  4528. data_size = sizeof(mat_int32_t);
  4529. if ( mat->byteswap ) {
  4530. for ( i = 0; i < len; i++ ) {
  4531. bytesread += fread(&i32,data_size,1,(FILE*)mat->fp);
  4532. data[i] = Mat_int32Swap(&i32);
  4533. }
  4534. } else {
  4535. for ( i = 0; i < len; i++ ) {
  4536. bytesread += fread(&i32,data_size,1,(FILE*)mat->fp);
  4537. data[i] = i32;
  4538. }
  4539. }
  4540. break;
  4541. }
  4542. case MAT_T_UINT32:
  4543. {
  4544. mat_uint32_t ui32;
  4545. data_size = sizeof(mat_uint32_t);
  4546. if ( mat->byteswap ) {
  4547. for ( i = 0; i < len; i++ ) {
  4548. bytesread += fread(&ui32,data_size,1,(FILE*)mat->fp);
  4549. data[i] = Mat_uint32Swap(&ui32);
  4550. }
  4551. } else {
  4552. for ( i = 0; i < len; i++ ) {
  4553. bytesread += fread(&ui32,data_size,1,(FILE*)mat->fp);
  4554. data[i] = ui32;
  4555. }
  4556. }
  4557. break;
  4558. }
  4559. case MAT_T_INT16:
  4560. {
  4561. mat_int16_t i16;
  4562. data_size = sizeof(mat_int16_t);
  4563. if ( mat->byteswap ) {
  4564. for ( i = 0; i < len; i++ ) {
  4565. bytesread += fread(&i16,data_size,1,(FILE*)mat->fp);
  4566. data[i] = Mat_int16Swap(&i16);
  4567. }
  4568. } else {
  4569. for ( i = 0; i < len; i++ ) {
  4570. bytesread += fread(&i16,data_size,1,(FILE*)mat->fp);
  4571. data[i] = i16;
  4572. }
  4573. }
  4574. break;
  4575. }
  4576. case MAT_T_UINT16:
  4577. {
  4578. mat_uint16_t ui16;
  4579. data_size = sizeof(mat_uint16_t);
  4580. if ( mat->byteswap ) {
  4581. for ( i = 0; i < len; i++ ) {
  4582. bytesread += fread(&ui16,data_size,1,(FILE*)mat->fp);
  4583. data[i] = Mat_uint16Swap(&ui16);
  4584. }
  4585. } else {
  4586. for ( i = 0; i < len; i++ ) {
  4587. bytesread += fread(&ui16,data_size,1,(FILE*)mat->fp);
  4588. data[i] = ui16;
  4589. }
  4590. }
  4591. break;
  4592. }
  4593. case MAT_T_INT8:
  4594. {
  4595. mat_int8_t i8;
  4596. data_size = sizeof(mat_int8_t);
  4597. for ( i = 0; i < len; i++ ) {
  4598. bytesread += fread(&i8,data_size,1,(FILE*)mat->fp);
  4599. data[i] = i8;
  4600. }
  4601. break;
  4602. }
  4603. case MAT_T_UINT8:
  4604. {
  4605. mat_uint8_t ui8;
  4606. data_size = sizeof(mat_uint8_t);
  4607. for ( i = 0; i < len; i++ ) {
  4608. bytesread += fread(&ui8,data_size,1,(FILE*)mat->fp);
  4609. data[i] = ui8;
  4610. }
  4611. break;
  4612. }
  4613. default:
  4614. return 0;
  4615. }
  4616. bytesread *= data_size;
  4617. return bytesread;
  4618. }
  4619. #if defined(HAVE_ZLIB)
  4620. /** @brief Reads data of type @c data_type into an unsigned 8-bit integer type
  4621. *
  4622. * Reads from the MAT file @c len compressed elements of data type @c data_type
  4623. * storing them as unsigned 8-bit integers in @c data.
  4624. * @ingroup mat_internal
  4625. * @param mat MAT file pointer
  4626. * @param z Pointer to the zlib stream for inflation
  4627. * @param data Pointer to store the output 8-bit integer values
  4628. * (len*sizeof(mat_uint8_t))
  4629. * @param data_type one of the @c matio_types enumerations which is the source
  4630. * data type in the file
  4631. * @param len Number of elements of type @c data_type to read from the file
  4632. * @retval Number of bytes read from the file
  4633. */
  4634. static int
  4635. ReadCompressedUInt8Data(mat_t *mat,z_streamp z,mat_uint8_t *data,
  4636. enum matio_types data_type,int len)
  4637. {
  4638. int nBytes = 0, data_size = 0, i;
  4639. if ( (mat == NULL) || (data == NULL) || (z == NULL) )
  4640. return 0;
  4641. switch ( data_type ) {
  4642. case MAT_T_DOUBLE:
  4643. {
  4644. double d;
  4645. data_size = sizeof(double);
  4646. if ( mat->byteswap ) {
  4647. for ( i = 0; i < len; i++ ) {
  4648. InflateData(mat,z,&d,data_size);
  4649. data[i] = Mat_doubleSwap(&d);
  4650. }
  4651. } else {
  4652. for ( i = 0; i < len; i++ ) {
  4653. InflateData(mat,z,&d,data_size);
  4654. data[i] = d;
  4655. }
  4656. }
  4657. break;
  4658. }
  4659. case MAT_T_SINGLE:
  4660. {
  4661. float f;
  4662. data_size = sizeof(float);
  4663. if ( mat->byteswap ) {
  4664. for ( i = 0; i < len; i++ ) {
  4665. InflateData(mat,z,&f,data_size);
  4666. data[i] = Mat_floatSwap(&f);
  4667. }
  4668. } else {
  4669. for ( i = 0; i < len; i++ ) {
  4670. InflateData(mat,z,&f,data_size);
  4671. data[i] = f;
  4672. }
  4673. }
  4674. break;
  4675. }
  4676. case MAT_T_INT32:
  4677. {
  4678. mat_int32_t i32;
  4679. data_size = sizeof(mat_int32_t);
  4680. if ( mat->byteswap ) {
  4681. for ( i = 0; i < len; i++ ) {
  4682. InflateData(mat,z,&i32,data_size);
  4683. data[i] = Mat_int32Swap(&i32);
  4684. }
  4685. } else {
  4686. for ( i = 0; i < len; i++ ) {
  4687. InflateData(mat,z,&i32,data_size);
  4688. data[i] = i32;
  4689. }
  4690. }
  4691. break;
  4692. }
  4693. case MAT_T_UINT32:
  4694. {
  4695. mat_uint32_t ui32;
  4696. data_size = sizeof(mat_uint32_t);
  4697. if ( mat->byteswap ) {
  4698. for ( i = 0; i < len; i++ ) {
  4699. InflateData(mat,z,&ui32,data_size);
  4700. data[i] = Mat_uint32Swap(&ui32);
  4701. }
  4702. } else {
  4703. for ( i = 0; i < len; i++ ) {
  4704. InflateData(mat,z,&ui32,data_size);
  4705. data[i] = ui32;
  4706. }
  4707. }
  4708. break;
  4709. }
  4710. case MAT_T_INT16:
  4711. {
  4712. mat_int16_t i16;
  4713. data_size = sizeof(mat_int16_t);
  4714. if ( mat->byteswap ) {
  4715. for ( i = 0; i < len; i++ ) {
  4716. InflateData(mat,z,&i16,data_size);
  4717. data[i] = Mat_int16Swap(&i16);
  4718. }
  4719. } else {
  4720. for ( i = 0; i < len; i++ ) {
  4721. InflateData(mat,z,&i16,data_size);
  4722. data[i] = i16;
  4723. }
  4724. }
  4725. break;
  4726. }
  4727. case MAT_T_UINT16:
  4728. {
  4729. mat_uint16_t ui16;
  4730. data_size = sizeof(mat_uint16_t);
  4731. if ( mat->byteswap ) {
  4732. for ( i = 0; i < len; i++ ) {
  4733. InflateData(mat,z,&ui16,data_size);
  4734. data[i] = Mat_uint16Swap(&ui16);
  4735. }
  4736. } else {
  4737. for ( i = 0; i < len; i++ ) {
  4738. InflateData(mat,z,&ui16,data_size);
  4739. data[i] = ui16;
  4740. }
  4741. }
  4742. break;
  4743. }
  4744. case MAT_T_UINT8:
  4745. {
  4746. mat_uint8_t ui8;
  4747. data_size = sizeof(mat_uint8_t);
  4748. for ( i = 0; i < len; i++ ) {
  4749. InflateData(mat,z,&ui8,data_size);
  4750. data[i] = ui8;
  4751. }
  4752. break;
  4753. }
  4754. case MAT_T_INT8:
  4755. {
  4756. mat_int8_t i8;
  4757. data_size = sizeof(mat_int8_t);
  4758. for ( i = 0; i < len; i++ ) {
  4759. InflateData(mat,z,&i8,data_size);
  4760. data[i] = i8;
  4761. }
  4762. break;
  4763. }
  4764. default:
  4765. return 0;
  4766. }
  4767. nBytes = len*data_size;
  4768. return nBytes;
  4769. }
  4770. #endif
  4771. #if defined(HAVE_ZLIB)
  4772. /** @brief Reads data of type @c data_type into a char type
  4773. *
  4774. * Reads from the MAT file @c len compressed elements of data type @c data_type
  4775. * storing them as char's in @c data.
  4776. * @ingroup mat_internal
  4777. * @param mat MAT file pointer
  4778. * @param z Pointer to the zlib stream for inflation
  4779. * @param data Pointer to store the output char values (len*sizeof(char))
  4780. * @param data_type one of the @c matio_types enumerations which is the source
  4781. * data type in the file
  4782. * @param len Number of elements of type @c data_type to read from the file
  4783. * @retval Number of bytes read from the file
  4784. */
  4785. static int
  4786. ReadCompressedCharData(mat_t *mat,z_streamp z,char *data,
  4787. enum matio_types data_type,int len)
  4788. {
  4789. int nBytes = 0, data_size = 0, i;
  4790. if ( (mat == NULL) || (data == NULL) || (mat->fp == NULL) )
  4791. return 0;
  4792. switch ( data_type ) {
  4793. case MAT_T_UTF8:
  4794. data_size = 1;
  4795. for ( i = 0; i < len; i++ )
  4796. InflateData(mat,z,data+i,data_size);
  4797. break;
  4798. case MAT_T_INT8:
  4799. case MAT_T_UINT8:
  4800. data_size = 1;
  4801. for ( i = 0; i < len; i++ )
  4802. InflateData(mat,z,data+i,data_size);
  4803. break;
  4804. case MAT_T_INT16:
  4805. case MAT_T_UINT16:
  4806. {
  4807. mat_uint16_t i16;
  4808. data_size = 2;
  4809. if ( mat->byteswap ) {
  4810. for ( i = 0; i < len; i++ ) {
  4811. InflateData(mat,z,&i16,data_size);
  4812. data[i] = Mat_uint16Swap(&i16);
  4813. }
  4814. } else {
  4815. for ( i = 0; i < len; i++ ) {
  4816. InflateData(mat,z,&i16,data_size);
  4817. data[i] = i16;
  4818. }
  4819. }
  4820. break;
  4821. }
  4822. default:
  4823. printf("Character data not supported type: %d",data_type);
  4824. break;
  4825. }
  4826. nBytes = len*data_size;
  4827. return nBytes;
  4828. }
  4829. #endif
  4830. static int
  4831. ReadCharData(mat_t *mat,char *data,enum matio_types data_type,int len)
  4832. {
  4833. int bytesread = 0, data_size = 0, i;
  4834. if ( (mat == NULL) || (data == NULL) || (mat->fp == NULL) )
  4835. return 0;
  4836. switch ( data_type ) {
  4837. case MAT_T_UTF8:
  4838. for ( i = 0; i < len; i++ )
  4839. bytesread += fread(data+i,1,1,(FILE*)mat->fp);
  4840. break;
  4841. case MAT_T_INT8:
  4842. case MAT_T_UINT8:
  4843. for ( i = 0; i < len; i++ )
  4844. bytesread += fread(data+i,1,1,(FILE*)mat->fp);
  4845. break;
  4846. case MAT_T_INT16:
  4847. case MAT_T_UINT16:
  4848. {
  4849. mat_uint16_t i16;
  4850. if ( mat->byteswap ) {
  4851. for ( i = 0; i < len; i++ ) {
  4852. bytesread += fread(&i16,2,1,(FILE*)mat->fp);
  4853. data[i] = Mat_uint16Swap(&i16);
  4854. }
  4855. } else {
  4856. for ( i = 0; i < len; i++ ) {
  4857. bytesread += fread(&i16,2,1,(FILE*)mat->fp);
  4858. data[i] = i16;
  4859. }
  4860. }
  4861. break;
  4862. }
  4863. default:
  4864. printf("Character data not supported type: %d",data_type);
  4865. break;
  4866. }
  4867. bytesread *= data_size;
  4868. return bytesread;
  4869. }
  4870. /*
  4871. *-------------------------------------------------------------------
  4872. * Routines to read "slabs" of data
  4873. *-------------------------------------------------------------------
  4874. */
  4875. #define READ_DATA_SLABN_RANK_LOOP \
  4876. do { \
  4877. for ( j = 1; j < rank; j++ ) { \
  4878. cnt[j]++; \
  4879. if ( (cnt[j] % edge[j]) == 0 ) { \
  4880. cnt[j] = 0; \
  4881. if ( (I % dimp[j]) != 0 ) { \
  4882. (void)fseek((FILE*)mat->fp,data_size*(dimp[j]-(I % dimp[j]) + dimp[j-1]*start[j]),SEEK_CUR); \
  4883. I += dimp[j]-(I % dimp[j]) + dimp[j-1]*start[j]; \
  4884. } else if ( start[j] ) { \
  4885. (void)fseek((FILE*)mat->fp,data_size*(dimp[j-1]*start[j]),SEEK_CUR); \
  4886. I += dimp[j-1]*start[j]; \
  4887. } \
  4888. } else { \
  4889. I += inc[j]; \
  4890. (void)fseek((FILE*)mat->fp,data_size*inc[j],SEEK_CUR); \
  4891. break; \
  4892. } \
  4893. } \
  4894. } while (0)
  4895. #define READ_DATA_SLABN(ReadDataFunc) \
  4896. do { \
  4897. inc[0] = stride[0]-1; \
  4898. dimp[0] = dims[0]; \
  4899. N = edge[0]; \
  4900. I = 0; /* start[0]; */ \
  4901. for ( i = 1; i < rank; i++ ) { \
  4902. inc[i] = stride[i]-1; \
  4903. dimp[i] = dims[i-1]; \
  4904. for ( j = i; j--; ) { \
  4905. inc[i] *= dims[j]; \
  4906. dimp[i] *= dims[j+1]; \
  4907. } \
  4908. N *= edge[i]; \
  4909. I += dimp[i-1]*start[i]; \
  4910. } \
  4911. (void)fseek((FILE*)mat->fp,I*data_size,SEEK_CUR); \
  4912. if ( stride[0] == 1 ) { \
  4913. for ( i = 0; i < N; i+=edge[0] ) { \
  4914. if ( start[0] ) { \
  4915. (void)fseek((FILE*)mat->fp,start[0]*data_size,SEEK_CUR); \
  4916. I += start[0]; \
  4917. } \
  4918. ReadDataFunc(mat,ptr+i,data_type,edge[0]); \
  4919. I += dims[0]-start[0]; \
  4920. (void)fseek((FILE*)mat->fp,data_size*(dims[0]-edge[0]-start[0]), \
  4921. SEEK_CUR); \
  4922. READ_DATA_SLABN_RANK_LOOP; \
  4923. } \
  4924. } else { \
  4925. for ( i = 0; i < N; i+=edge[0] ) { \
  4926. if ( start[0] ) { \
  4927. (void)fseek((FILE*)mat->fp,start[0]*data_size,SEEK_CUR); \
  4928. I += start[0]; \
  4929. } \
  4930. for ( j = 0; j < edge[0]; j++ ) { \
  4931. ReadDataFunc(mat,ptr+i+j,data_type,1); \
  4932. (void)fseek((FILE*)mat->fp,data_size*(stride[0]-1),SEEK_CUR); \
  4933. I += stride[0]; \
  4934. } \
  4935. I += dims[0]-edge[0]*stride[0]-start[0]; \
  4936. (void)fseek((FILE*)mat->fp,data_size* \
  4937. (dims[0]-edge[0]*stride[0]-start[0]),SEEK_CUR); \
  4938. READ_DATA_SLABN_RANK_LOOP; \
  4939. } \
  4940. } \
  4941. } while (0)
  4942. /** @brief Reads data of type @c data_type by user-defined dimensions
  4943. *
  4944. * @ingroup mat_internal
  4945. * @param mat MAT file pointer
  4946. * @param data Pointer to store the output data
  4947. * @param class_type Type of data class (matio_classes enumerations)
  4948. * @param data_type Datatype of the stored data (matio_types enumerations)
  4949. * @param rank Number of dimensions in the data
  4950. * @param dims Dimensions of the data
  4951. * @param start Index to start reading data in each dimension
  4952. * @param stride Read every @c stride elements in each dimension
  4953. * @param edge Number of elements to read in each dimension
  4954. * @retval Number of bytes read from the file, or -1 on error
  4955. */
  4956. static int
  4957. ReadDataSlabN(mat_t *mat,void *data,enum matio_classes class_type,
  4958. enum matio_types data_type,int rank,size_t *dims,int *start,int *stride,
  4959. int *edge)
  4960. {
  4961. int nBytes = 0, i, j, N, I = 0;
  4962. int inc[10] = {0,}, cnt[10] = {0,}, dimp[10] = {0,};
  4963. size_t data_size;
  4964. if ( (mat == NULL) || (data == NULL) || (mat->fp == NULL) ||
  4965. (start == NULL) || (stride == NULL) || (edge == NULL) ) {
  4966. return -1;
  4967. } else if ( rank > 10 ) {
  4968. return -1;
  4969. }
  4970. data_size = Mat_SizeOf(data_type);
  4971. switch ( class_type ) {
  4972. case MAT_C_DOUBLE:
  4973. {
  4974. double *ptr = (double*)data;
  4975. READ_DATA_SLABN(ReadDoubleData);
  4976. break;
  4977. }
  4978. case MAT_C_SINGLE:
  4979. {
  4980. float *ptr = (float*)data;
  4981. READ_DATA_SLABN(ReadSingleData);
  4982. break;
  4983. }
  4984. #ifdef HAVE_MATIO_INT64_T
  4985. case MAT_C_INT64:
  4986. {
  4987. mat_int64_t *ptr = (mat_int64_t*)data;
  4988. READ_DATA_SLABN(ReadInt64Data);
  4989. break;
  4990. }
  4991. #endif /* HAVE_MATIO_INT64_T */
  4992. #ifdef HAVE_MATIO_UINT64_T
  4993. case MAT_C_UINT64:
  4994. {
  4995. mat_uint64_t *ptr = (mat_uint64_t*)data;
  4996. READ_DATA_SLABN(ReadUInt64Data);
  4997. break;
  4998. }
  4999. #endif /* HAVE_MATIO_UINT64_T */
  5000. case MAT_C_INT32:
  5001. {
  5002. mat_int32_t *ptr = (mat_int32_t*)data;
  5003. READ_DATA_SLABN(ReadInt32Data);
  5004. break;
  5005. }
  5006. case MAT_C_UINT32:
  5007. {
  5008. mat_uint32_t *ptr = (mat_uint32_t*)data;
  5009. READ_DATA_SLABN(ReadUInt32Data);
  5010. break;
  5011. }
  5012. case MAT_C_INT16:
  5013. {
  5014. mat_int16_t *ptr = (mat_int16_t*)data;
  5015. READ_DATA_SLABN(ReadInt16Data);
  5016. break;
  5017. }
  5018. case MAT_C_UINT16:
  5019. {
  5020. mat_uint16_t *ptr = (mat_uint16_t*)data;
  5021. READ_DATA_SLABN(ReadUInt16Data);
  5022. break;
  5023. }
  5024. case MAT_C_INT8:
  5025. {
  5026. mat_int8_t *ptr = (mat_int8_t*)data;
  5027. READ_DATA_SLABN(ReadInt8Data);
  5028. break;
  5029. }
  5030. case MAT_C_UINT8:
  5031. {
  5032. mat_uint8_t *ptr = (mat_uint8_t*)data;
  5033. READ_DATA_SLABN(ReadUInt8Data);
  5034. break;
  5035. }
  5036. default:
  5037. nBytes = 0;
  5038. }
  5039. return nBytes;
  5040. }
  5041. #undef READ_DATA_SLABN
  5042. #undef READ_DATA_SLABN_RANK_LOOP
  5043. #if defined(HAVE_ZLIB)
  5044. #define READ_COMPRESSED_DATA_SLABN_RANK_LOOP \
  5045. do { \
  5046. for ( j = 1; j < rank; j++ ) { \
  5047. cnt[j]++; \
  5048. if ( (cnt[j] % edge[j]) == 0 ) { \
  5049. cnt[j] = 0; \
  5050. if ( (I % dimp[j]) != 0 ) { \
  5051. InflateSkipData(mat,&z_copy,data_type, dimp[j]-(I % dimp[j]) + dimp[j-1]*start[j]); \
  5052. I += dimp[j]-(I % dimp[j]) + dimp[j-1]*start[j]; \
  5053. } else if ( start[j] ) { \
  5054. InflateSkipData(mat,&z_copy,data_type, dimp[j-1]*start[j]); \
  5055. I += dimp[j-1]*start[j]; \
  5056. } \
  5057. } else { \
  5058. if ( inc[j] ) { \
  5059. I += inc[j]; \
  5060. InflateSkipData(mat,&z_copy,data_type,inc[j]); \
  5061. } \
  5062. break; \
  5063. } \
  5064. } \
  5065. } while (0)
  5066. #define READ_COMPRESSED_DATA_SLABN(ReadDataFunc) \
  5067. do { \
  5068. inc[0] = stride[0]-1; \
  5069. dimp[0] = dims[0]; \
  5070. N = edge[0]; \
  5071. I = 0; \
  5072. for ( i = 1; i < rank; i++ ) { \
  5073. inc[i] = stride[i]-1; \
  5074. dimp[i] = dims[i-1]; \
  5075. for ( j = i; j--; ) { \
  5076. inc[i] *= dims[j]; \
  5077. dimp[i] *= dims[j+1]; \
  5078. } \
  5079. N *= edge[i]; \
  5080. I += dimp[i-1]*start[i]; \
  5081. } \
  5082. /* Skip all data to the starting indices */ \
  5083. InflateSkipData(mat,&z_copy,data_type,I); \
  5084. if ( stride[0] == 1 ) { \
  5085. for ( i = 0; i < N; i+=edge[0] ) { \
  5086. if ( start[0] ) { \
  5087. InflateSkipData(mat,&z_copy,data_type,start[0]); \
  5088. I += start[0]; \
  5089. } \
  5090. ReadDataFunc(mat,&z_copy,ptr+i,data_type,edge[0]); \
  5091. InflateSkipData(mat,&z_copy,data_type,dims[0]-start[0]-edge[0]); \
  5092. I += dims[0]-start[0]; \
  5093. READ_COMPRESSED_DATA_SLABN_RANK_LOOP; \
  5094. } \
  5095. } else { \
  5096. for ( i = 0; i < N; i+=edge[0] ) { \
  5097. if ( start[0] ) { \
  5098. InflateSkipData(mat,&z_copy,data_type,start[0]); \
  5099. I += start[0]; \
  5100. } \
  5101. for ( j = 0; j < edge[0]-1; j++ ) { \
  5102. ReadDataFunc(mat,&z_copy,ptr+i+j,data_type,1); \
  5103. InflateSkipData(mat,&z_copy,data_type,(stride[0]-1)); \
  5104. I += stride[0]; \
  5105. } \
  5106. ReadDataFunc(mat,&z_copy,ptr+i+j,data_type,1); \
  5107. I += dims[0]-(edge[0]-1)*stride[0]-start[0]; \
  5108. InflateSkipData(mat,&z_copy,data_type,dims[0]-(edge[0]-1)*stride[0]-start[0]-1); \
  5109. READ_COMPRESSED_DATA_SLABN_RANK_LOOP; \
  5110. } \
  5111. } \
  5112. } while (0)
  5113. /** @brief Reads data of type @c data_type by user-defined dimensions
  5114. *
  5115. * @ingroup mat_internal
  5116. * @param mat MAT file pointer
  5117. * @param z zlib compression stream
  5118. * @param data Pointer to store the output data
  5119. * @param class_type Type of data class (matio_classes enumerations)
  5120. * @param data_type Datatype of the stored data (matio_types enumerations)
  5121. * @param rank Number of dimensions in the data
  5122. * @param dims Dimensions of the data
  5123. * @param start Index to start reading data in each dimension
  5124. * @param stride Read every @c stride elements in each dimension
  5125. * @param edge Number of elements to read in each dimension
  5126. * @retval Number of bytes read from the file, or -1 on error
  5127. */
  5128. static int
  5129. ReadCompressedDataSlabN(mat_t *mat,z_streamp z,void *data,
  5130. enum matio_classes class_type,enum matio_types data_type,int rank,
  5131. size_t *dims,int *start,int *stride,int *edge)
  5132. {
  5133. int nBytes = 0, i, j, N, I = 0;
  5134. int inc[10] = {0,}, cnt[10] = {0,}, dimp[10] = {0,};
  5135. z_stream z_copy = {0,};
  5136. if ( (mat == NULL) || (data == NULL) || (mat->fp == NULL) ||
  5137. (start == NULL) || (stride == NULL) || (edge == NULL) ) {
  5138. return 1;
  5139. } else if ( rank > 10 ) {
  5140. return 1;
  5141. }
  5142. i = inflateCopy(&z_copy,z);
  5143. switch ( class_type ) {
  5144. case MAT_C_DOUBLE:
  5145. {
  5146. double *ptr = (double*)data;
  5147. READ_COMPRESSED_DATA_SLABN(ReadCompressedDoubleData);
  5148. break;
  5149. }
  5150. case MAT_C_SINGLE:
  5151. {
  5152. float *ptr = (float*)data;
  5153. READ_COMPRESSED_DATA_SLABN(ReadCompressedSingleData);
  5154. break;
  5155. }
  5156. #ifdef HAVE_MATIO_INT64_T
  5157. case MAT_C_INT64:
  5158. {
  5159. mat_int64_t *ptr = (mat_int64_t*)data;
  5160. READ_COMPRESSED_DATA_SLABN(ReadCompressedInt64Data);
  5161. break;
  5162. }
  5163. #endif /* HAVE_MATIO_INT64_T */
  5164. #ifdef HAVE_MATIO_UINT64_T
  5165. case MAT_C_UINT64:
  5166. {
  5167. mat_uint64_t *ptr = (mat_uint64_t*)data;
  5168. READ_COMPRESSED_DATA_SLABN(ReadCompressedUInt64Data);
  5169. break;
  5170. }
  5171. #endif /* HAVE_MATIO_UINT64_T */
  5172. case MAT_C_INT32:
  5173. {
  5174. mat_int32_t *ptr = (mat_int32_t*)data;
  5175. READ_COMPRESSED_DATA_SLABN(ReadCompressedInt32Data);
  5176. break;
  5177. }
  5178. case MAT_C_UINT32:
  5179. {
  5180. mat_uint32_t *ptr = (mat_uint32_t*)data;
  5181. READ_COMPRESSED_DATA_SLABN(ReadCompressedUInt32Data);
  5182. break;
  5183. }
  5184. case MAT_C_INT16:
  5185. {
  5186. mat_int16_t *ptr = (mat_int16_t*)data;
  5187. READ_COMPRESSED_DATA_SLABN(ReadCompressedInt16Data);
  5188. break;
  5189. }
  5190. case MAT_C_UINT16:
  5191. {
  5192. mat_uint16_t *ptr = (mat_uint16_t*)data;
  5193. READ_COMPRESSED_DATA_SLABN(ReadCompressedUInt16Data);
  5194. break;
  5195. }
  5196. case MAT_C_INT8:
  5197. {
  5198. mat_int8_t *ptr = (mat_int8_t*)data;
  5199. READ_COMPRESSED_DATA_SLABN(ReadCompressedInt8Data);
  5200. break;
  5201. }
  5202. case MAT_C_UINT8:
  5203. {
  5204. mat_uint8_t *ptr = (mat_uint8_t*)data;
  5205. READ_COMPRESSED_DATA_SLABN(ReadCompressedUInt8Data);
  5206. break;
  5207. }
  5208. default:
  5209. nBytes = 0;
  5210. }
  5211. inflateEnd(&z_copy);
  5212. return nBytes;
  5213. }
  5214. #undef READ_COMPRESSED_DATA_SLABN
  5215. #undef READ_COMPRESSED_DATA_SLABN_RANK_LOOP
  5216. #endif
  5217. #define READ_DATA_SLAB1(ReadDataFunc) \
  5218. do { \
  5219. if ( !stride ) { \
  5220. bytesread+=ReadDataFunc(mat,ptr,data_type,edge); \
  5221. } else { \
  5222. for ( i = 0; i < edge; i++ ) { \
  5223. bytesread+=ReadDataFunc(mat,ptr+i,data_type,1); \
  5224. (void)fseek((FILE*)mat->fp,stride,SEEK_CUR); \
  5225. } \
  5226. } \
  5227. } while (0)
  5228. /** @brief Reads data of type @c data_type by user-defined dimensions for 1-D
  5229. * data
  5230. *
  5231. * @ingroup mat_internal
  5232. * @param mat MAT file pointer
  5233. * @param data Pointer to store the output data
  5234. * @param class_type Type of data class (matio_classes enumerations)
  5235. * @param data_type Datatype of the stored data (matio_types enumerations)
  5236. * @param start Index to start reading data
  5237. * @param stride Read every @c stride elements
  5238. * @param edge Number of elements to read
  5239. * @return Number of bytes read from the file, or -1 on error
  5240. */
  5241. static int
  5242. ReadDataSlab1(mat_t *mat,void *data,enum matio_classes class_type,
  5243. enum matio_types data_type,int start,int stride,int edge)
  5244. {
  5245. int i;
  5246. size_t data_size;
  5247. int bytesread = 0;
  5248. data_size = Mat_SizeOf(data_type);
  5249. (void)fseek((FILE*)mat->fp,start*data_size,SEEK_CUR);
  5250. stride = data_size*(stride-1);
  5251. switch ( class_type ) {
  5252. case MAT_C_DOUBLE:
  5253. {
  5254. double *ptr = (double*)data;
  5255. READ_DATA_SLAB1(ReadDoubleData);
  5256. break;
  5257. }
  5258. case MAT_C_SINGLE:
  5259. {
  5260. float *ptr = (float*)data;
  5261. READ_DATA_SLAB1(ReadSingleData);
  5262. break;
  5263. }
  5264. #ifdef HAVE_MATIO_INT64_T
  5265. case MAT_C_INT64:
  5266. {
  5267. mat_int64_t *ptr = (mat_int64_t*)data;
  5268. READ_DATA_SLAB1(ReadInt64Data);
  5269. break;
  5270. }
  5271. #endif /* HAVE_MATIO_INT64_T */
  5272. #ifdef HAVE_MATIO_UINT64_T
  5273. case MAT_C_UINT64:
  5274. {
  5275. mat_uint64_t *ptr = (mat_uint64_t*)data;
  5276. READ_DATA_SLAB1(ReadUInt64Data);
  5277. break;
  5278. }
  5279. #endif /* HAVE_MATIO_UINT64_T */
  5280. case MAT_C_INT32:
  5281. {
  5282. mat_int32_t *ptr = (mat_int32_t*)data;
  5283. READ_DATA_SLAB1(ReadInt32Data);
  5284. break;
  5285. }
  5286. case MAT_C_UINT32:
  5287. {
  5288. mat_uint32_t *ptr = (mat_uint32_t*)data;
  5289. READ_DATA_SLAB1(ReadUInt32Data);
  5290. break;
  5291. }
  5292. case MAT_C_INT16:
  5293. {
  5294. mat_int16_t *ptr = (mat_int16_t*)data;
  5295. READ_DATA_SLAB1(ReadInt16Data);
  5296. break;
  5297. }
  5298. case MAT_C_UINT16:
  5299. {
  5300. mat_uint16_t *ptr = (mat_uint16_t*)data;
  5301. READ_DATA_SLAB1(ReadUInt16Data);
  5302. break;
  5303. }
  5304. case MAT_C_INT8:
  5305. {
  5306. mat_int8_t *ptr = (mat_int8_t*)data;
  5307. READ_DATA_SLAB1(ReadInt8Data);
  5308. break;
  5309. }
  5310. case MAT_C_UINT8:
  5311. {
  5312. mat_uint8_t *ptr = (mat_uint8_t*)data;
  5313. READ_DATA_SLAB1(ReadUInt8Data);
  5314. break;
  5315. }
  5316. default:
  5317. return 0;
  5318. }
  5319. return bytesread;
  5320. }
  5321. #undef READ_DATA_SLAB1
  5322. #define READ_DATA_SLAB2(ReadDataFunc) \
  5323. do { \
  5324. /* If stride[0] is 1 and stride[1] is 1, we are reading all of the */ \
  5325. /* data so get rid of the loops. */ \
  5326. if ( (stride[0] == 1 && edge[0] == dims[0]) && \
  5327. (stride[1] == 1) ) { \
  5328. ReadDataFunc(mat,ptr,data_type,edge[0]*edge[1]); \
  5329. } else { \
  5330. row_stride = (stride[0]-1)*data_size; \
  5331. col_stride = stride[1]*dims[0]*data_size; \
  5332. pos = ftell((FILE*)mat->fp); \
  5333. if ( pos == -1L ) { \
  5334. Mat_Critical("Couldn't determine file position"); \
  5335. return -1; \
  5336. } \
  5337. (void)fseek((FILE*)mat->fp,start[1]*dims[0]*data_size,SEEK_CUR); \
  5338. for ( i = 0; i < edge[1]; i++ ) { \
  5339. pos = ftell((FILE*)mat->fp); \
  5340. if ( pos == -1L ) { \
  5341. Mat_Critical("Couldn't determine file position"); \
  5342. return -1; \
  5343. } \
  5344. (void)fseek((FILE*)mat->fp,start[0]*data_size,SEEK_CUR); \
  5345. for ( j = 0; j < edge[0]; j++ ) { \
  5346. ReadDataFunc(mat,ptr++,data_type,1); \
  5347. (void)fseek((FILE*)mat->fp,row_stride,SEEK_CUR); \
  5348. } \
  5349. pos2 = ftell((FILE*)mat->fp); \
  5350. if ( pos2 == -1L ) { \
  5351. Mat_Critical("Couldn't determine file position"); \
  5352. return -1; \
  5353. } \
  5354. pos +=col_stride-pos2; \
  5355. (void)fseek((FILE*)mat->fp,pos,SEEK_CUR); \
  5356. } \
  5357. } \
  5358. } while (0)
  5359. /** @brief Reads data of type @c data_type by user-defined dimensions for 2-D
  5360. * data
  5361. *
  5362. * @ingroup mat_internal
  5363. * @param mat MAT file pointer
  5364. * @param data Pointer to store the output data
  5365. * @param class_type Type of data class (matio_classes enumerations)
  5366. * @param data_type Datatype of the stored data (matio_types enumerations)
  5367. * @param dims Dimensions of the data
  5368. * @param start Index to start reading data in each dimension
  5369. * @param stride Read every @c stride elements in each dimension
  5370. * @param edge Number of elements to read in each dimension
  5371. * @retval Number of bytes read from the file, or -1 on error
  5372. */
  5373. static int
  5374. ReadDataSlab2(mat_t *mat,void *data,enum matio_classes class_type,
  5375. enum matio_types data_type,size_t *dims,int *start,int *stride,int *edge)
  5376. {
  5377. int nBytes = 0, data_size, i, j;
  5378. int pos, row_stride, col_stride, pos2;
  5379. if ( (mat == NULL) || (data == NULL) || (mat->fp == NULL) ||
  5380. (start == NULL) || (stride == NULL) || (edge == NULL) ) {
  5381. return 0;
  5382. }
  5383. data_size = Mat_SizeOf(data_type);
  5384. switch ( class_type ) {
  5385. case MAT_C_DOUBLE:
  5386. {
  5387. double *ptr = (double*)data;
  5388. READ_DATA_SLAB2(ReadDoubleData);
  5389. break;
  5390. }
  5391. case MAT_C_SINGLE:
  5392. {
  5393. float *ptr = (float*)data;
  5394. READ_DATA_SLAB2(ReadSingleData);
  5395. break;
  5396. }
  5397. #ifdef HAVE_MATIO_INT64_T
  5398. case MAT_C_INT64:
  5399. {
  5400. mat_int64_t *ptr = (mat_int64_t*)data;
  5401. READ_DATA_SLAB2(ReadInt64Data);
  5402. break;
  5403. }
  5404. #endif /* HAVE_MATIO_INT64_T */
  5405. #ifdef HAVE_MATIO_UINT64_T
  5406. case MAT_C_UINT64:
  5407. {
  5408. mat_uint64_t *ptr = (mat_uint64_t*)data;
  5409. READ_DATA_SLAB2(ReadUInt64Data);
  5410. break;
  5411. }
  5412. #endif /* HAVE_MATIO_UINT64_T */
  5413. case MAT_C_INT32:
  5414. {
  5415. mat_int32_t *ptr = (mat_int32_t*)data;
  5416. READ_DATA_SLAB2(ReadInt32Data);
  5417. break;
  5418. }
  5419. case MAT_C_UINT32:
  5420. {
  5421. mat_uint32_t *ptr = (mat_uint32_t*)data;
  5422. READ_DATA_SLAB2(ReadUInt32Data);
  5423. break;
  5424. }
  5425. case MAT_C_INT16:
  5426. {
  5427. mat_int16_t *ptr = (mat_int16_t*)data;
  5428. READ_DATA_SLAB2(ReadInt16Data);
  5429. break;
  5430. }
  5431. case MAT_C_UINT16:
  5432. {
  5433. mat_uint16_t *ptr = (mat_uint16_t*)data;
  5434. READ_DATA_SLAB2(ReadUInt16Data);
  5435. break;
  5436. }
  5437. case MAT_C_INT8:
  5438. {
  5439. mat_int8_t *ptr = (mat_int8_t*)data;
  5440. READ_DATA_SLAB2(ReadInt8Data);
  5441. break;
  5442. }
  5443. case MAT_C_UINT8:
  5444. {
  5445. mat_uint8_t *ptr = (mat_uint8_t*)data;
  5446. READ_DATA_SLAB2(ReadUInt8Data);
  5447. break;
  5448. }
  5449. default:
  5450. nBytes = 0;
  5451. }
  5452. return nBytes;
  5453. }
  5454. #undef READ_DATA_SLAB2
  5455. #if defined(HAVE_ZLIB)
  5456. #define READ_COMPRESSED_DATA_SLAB1(ReadDataFunc) \
  5457. do { \
  5458. if ( !stride ) { \
  5459. nBytes+=ReadDataFunc(mat,&z_copy,ptr,data_type,edge); \
  5460. } else { \
  5461. for ( i = 0; i < edge; i++ ) { \
  5462. nBytes+=ReadDataFunc(mat,&z_copy,ptr+i,data_type,1); \
  5463. InflateSkipData(mat,&z_copy,data_type,stride); \
  5464. } \
  5465. } \
  5466. } while (0)
  5467. /** @brief Reads data of type @c data_type by user-defined dimensions for 1-D
  5468. * data
  5469. *
  5470. * @ingroup mat_internal
  5471. * @param mat MAT file pointer
  5472. * @param z zlib compression stream
  5473. * @param data Pointer to store the output data
  5474. * @param class_type Type of data class (matio_classes enumerations)
  5475. * @param data_type Datatype of the stored data (matio_types enumerations)
  5476. * @param dims Dimensions of the data
  5477. * @param start Index to start reading data in each dimension
  5478. * @param stride Read every @c stride elements in each dimension
  5479. * @param edge Number of elements to read in each dimension
  5480. * @retval Number of bytes read from the file, or -1 on error
  5481. */
  5482. static int
  5483. ReadCompressedDataSlab1(mat_t *mat,z_streamp z,void *data,
  5484. enum matio_classes class_type,enum matio_types data_type,int start,
  5485. int stride,int edge)
  5486. {
  5487. int nBytes = 0, i, err;
  5488. z_stream z_copy = {0,};
  5489. if ( (mat == NULL) || (data == NULL) || (mat->fp == NULL) )
  5490. return 0;
  5491. stride--;
  5492. err = inflateCopy(&z_copy,z);
  5493. InflateSkipData(mat,&z_copy,data_type,start);
  5494. switch ( class_type ) {
  5495. case MAT_C_DOUBLE:
  5496. {
  5497. double *ptr = (double*)data;
  5498. READ_COMPRESSED_DATA_SLAB1(ReadCompressedDoubleData);
  5499. break;
  5500. }
  5501. case MAT_C_SINGLE:
  5502. {
  5503. float *ptr = (float*)data;
  5504. READ_COMPRESSED_DATA_SLAB1(ReadCompressedSingleData);
  5505. break;
  5506. }
  5507. #ifdef HAVE_MATIO_INT64_T
  5508. case MAT_C_INT64:
  5509. {
  5510. mat_int64_t *ptr = (mat_int64_t*)data;
  5511. READ_COMPRESSED_DATA_SLAB1(ReadCompressedInt64Data);
  5512. break;
  5513. }
  5514. #endif /* HAVE_MATIO_INT64_T */
  5515. #ifdef HAVE_MATIO_UINT64_T
  5516. case MAT_C_UINT64:
  5517. {
  5518. mat_uint64_t *ptr = (mat_uint64_t*)data;
  5519. READ_COMPRESSED_DATA_SLAB1(ReadCompressedUInt64Data);
  5520. break;
  5521. }
  5522. #endif /* HAVE_MATIO_UINT64_T */
  5523. case MAT_C_INT32:
  5524. {
  5525. mat_int32_t *ptr = (mat_int32_t*)data;
  5526. READ_COMPRESSED_DATA_SLAB1(ReadCompressedInt32Data);
  5527. break;
  5528. }
  5529. case MAT_C_UINT32:
  5530. {
  5531. mat_uint32_t *ptr = (mat_uint32_t*)data;
  5532. READ_COMPRESSED_DATA_SLAB1(ReadCompressedUInt32Data);
  5533. break;
  5534. }
  5535. case MAT_C_INT16:
  5536. {
  5537. mat_int16_t *ptr = (mat_int16_t*)data;
  5538. READ_COMPRESSED_DATA_SLAB1(ReadCompressedInt16Data);
  5539. break;
  5540. }
  5541. case MAT_C_UINT16:
  5542. {
  5543. mat_uint16_t *ptr = (mat_uint16_t*)data;
  5544. READ_COMPRESSED_DATA_SLAB1(ReadCompressedUInt16Data);
  5545. break;
  5546. }
  5547. case MAT_C_INT8:
  5548. {
  5549. mat_int8_t *ptr = (mat_int8_t*)data;
  5550. READ_COMPRESSED_DATA_SLAB1(ReadCompressedInt8Data);
  5551. break;
  5552. }
  5553. case MAT_C_UINT8:
  5554. {
  5555. mat_uint8_t *ptr = (mat_uint8_t*)data;
  5556. READ_COMPRESSED_DATA_SLAB1(ReadCompressedUInt8Data);
  5557. break;
  5558. }
  5559. default:
  5560. break;
  5561. }
  5562. inflateEnd(&z_copy);
  5563. return nBytes;
  5564. }
  5565. #undef READ_COMPRESSED_DATA_SLAB1
  5566. #define READ_COMPRESSED_DATA_SLAB2(ReadDataFunc) \
  5567. do {\
  5568. row_stride = (stride[0]-1); \
  5569. col_stride = (stride[1]-1)*dims[0]; \
  5570. InflateSkipData(mat,&z_copy,data_type,start[1]*dims[0]); \
  5571. /* If stride[0] is 1 and stride[1] is 1, we are reading all of the */ \
  5572. /* data so get rid of the loops. If stride[0] is 1 and stride[1] */ \
  5573. /* is not 0, we are reading whole columns, so get rid of inner loop */ \
  5574. /* to speed up the code */ \
  5575. if ( (stride[0] == 1 && edge[0] == dims[0]) && \
  5576. (stride[1] == 1) ) { \
  5577. ReadDataFunc(mat,&z_copy,ptr,data_type,edge[0]*edge[1]); \
  5578. } else if ( stride[0] == 1 ) { \
  5579. for ( i = 0; i < edge[1]; i++ ) { \
  5580. InflateSkipData(mat,&z_copy,data_type,start[0]); \
  5581. ReadDataFunc(mat,&z_copy,ptr,data_type,edge[0]); \
  5582. ptr += edge[0]; \
  5583. pos = dims[0]-(edge[0]-1)*stride[0]-1-start[0] + col_stride; \
  5584. InflateSkipData(mat,&z_copy,data_type,pos); \
  5585. } \
  5586. } else { \
  5587. for ( i = 0; i < edge[1]; i++ ) { \
  5588. InflateSkipData(mat,&z_copy,data_type,start[0]); \
  5589. for ( j = 0; j < edge[0]-1; j++ ) { \
  5590. ReadDataFunc(mat,&z_copy,ptr++,data_type,1); \
  5591. InflateSkipData(mat,&z_copy,data_type,row_stride); \
  5592. } \
  5593. ReadDataFunc(mat,&z_copy,ptr++,data_type,1); \
  5594. pos = dims[0]-(edge[0]-1)*stride[0]-1-start[0] + col_stride; \
  5595. InflateSkipData(mat,&z_copy,data_type,pos); \
  5596. } \
  5597. } \
  5598. } while (0)
  5599. /** @brief Reads data of type @c data_type by user-defined dimensions for 2-D
  5600. * data
  5601. *
  5602. * @ingroup mat_internal
  5603. * @param mat MAT file pointer
  5604. * @param z zlib compression stream
  5605. * @param data Pointer to store the output data
  5606. * @param class_type Type of data class (matio_classes enumerations)
  5607. * @param data_type Datatype of the stored data (matio_types enumerations)
  5608. * @param dims Dimensions of the data
  5609. * @param start Index to start reading data in each dimension
  5610. * @param stride Read every @c stride elements in each dimension
  5611. * @param edge Number of elements to read in each dimension
  5612. * @retval Number of bytes read from the file, or -1 on error
  5613. */
  5614. static int
  5615. ReadCompressedDataSlab2(mat_t *mat,z_streamp z,void *data,
  5616. enum matio_classes class_type,enum matio_types data_type,size_t *dims,
  5617. int *start,int *stride,int *edge)
  5618. {
  5619. int nBytes = 0, i, j, err;
  5620. int pos, row_stride, col_stride;
  5621. z_stream z_copy = {0,};
  5622. if ( (mat == NULL) || (data == NULL) || (mat->fp == NULL) ||
  5623. (start == NULL) || (stride == NULL) || (edge == NULL) ) {
  5624. return 0;
  5625. }
  5626. err = inflateCopy(&z_copy,z);
  5627. switch ( class_type ) {
  5628. case MAT_C_DOUBLE:
  5629. {
  5630. double *ptr = (double*)data;
  5631. READ_COMPRESSED_DATA_SLAB2(ReadCompressedDoubleData);
  5632. break;
  5633. }
  5634. case MAT_C_SINGLE:
  5635. {
  5636. float *ptr = (float*)data;
  5637. READ_COMPRESSED_DATA_SLAB2(ReadCompressedSingleData);
  5638. break;
  5639. }
  5640. #ifdef HAVE_MATIO_INT64_T
  5641. case MAT_C_INT64:
  5642. {
  5643. mat_int64_t *ptr = (mat_int64_t*)data;
  5644. READ_COMPRESSED_DATA_SLAB2(ReadCompressedInt64Data);
  5645. break;
  5646. }
  5647. #endif /* HAVE_MATIO_INT64_T */
  5648. #ifdef HAVE_MATIO_UINT64_T
  5649. case MAT_C_UINT64:
  5650. {
  5651. mat_uint64_t *ptr = (mat_uint64_t*)data;
  5652. READ_COMPRESSED_DATA_SLAB2(ReadCompressedUInt64Data);
  5653. break;
  5654. }
  5655. #endif /* HAVE_MATIO_UINT64_T */
  5656. case MAT_C_INT32:
  5657. {
  5658. mat_int32_t *ptr = (mat_int32_t*)data;
  5659. READ_COMPRESSED_DATA_SLAB2(ReadCompressedInt32Data);
  5660. break;
  5661. }
  5662. case MAT_C_UINT32:
  5663. {
  5664. mat_uint32_t *ptr = (mat_uint32_t*)data;
  5665. READ_COMPRESSED_DATA_SLAB2(ReadCompressedUInt32Data);
  5666. break;
  5667. }
  5668. case MAT_C_INT16:
  5669. {
  5670. mat_int16_t *ptr = (mat_int16_t*)data;
  5671. READ_COMPRESSED_DATA_SLAB2(ReadCompressedInt16Data);
  5672. break;
  5673. }
  5674. case MAT_C_UINT16:
  5675. {
  5676. mat_uint16_t *ptr = (mat_uint16_t*)data;
  5677. READ_COMPRESSED_DATA_SLAB2(ReadCompressedUInt16Data);
  5678. break;
  5679. }
  5680. case MAT_C_INT8:
  5681. {
  5682. mat_int8_t *ptr = (mat_int8_t*)data;
  5683. READ_COMPRESSED_DATA_SLAB2(ReadCompressedInt8Data);
  5684. break;
  5685. }
  5686. case MAT_C_UINT8:
  5687. {
  5688. mat_uint8_t *ptr = (mat_uint8_t*)data;
  5689. READ_COMPRESSED_DATA_SLAB2(ReadCompressedUInt8Data);
  5690. break;
  5691. }
  5692. default:
  5693. nBytes = 0;
  5694. }
  5695. inflateEnd(&z_copy);
  5696. return nBytes;
  5697. }
  5698. #undef READ_COMPRESSED_DATA_SLAB2
  5699. #endif
  5700. /** @endcond */
  5701. /* -------------------------------
  5702. * ---------- snprintf.c
  5703. * -------------------------------
  5704. */
  5705. /*
  5706. * Copyright (c) 1995 Patrick Powell.
  5707. *
  5708. * This code is based on code written by Patrick Powell <papowell@astart.com>.
  5709. * It may be used for any purpose as long as this notice remains intact on all
  5710. * source code distributions.
  5711. */
  5712. /*
  5713. * Copyright (c) 2008 Holger Weiss.
  5714. *
  5715. * This version of the code is maintained by Holger Weiss <holger@jhweiss.de>.
  5716. * My changes to the code may freely be used, modified and/or redistributed for
  5717. * any purpose. It would be nice if additions and fixes to this file (including
  5718. * trivial code cleanups) would be sent back in order to let me include them in
  5719. * the version available at <http://www.jhweiss.de/software/snprintf.html>.
  5720. * However, this is not a requirement for using or redistributing (possibly
  5721. * modified) versions of this file, nor is leaving this notice intact mandatory.
  5722. */
  5723. /*
  5724. * History
  5725. *
  5726. * 2008-01-20 Holger Weiss <holger@jhweiss.de> for C99-snprintf 1.1:
  5727. *
  5728. * Fixed the detection of infinite floating point values on IRIX (and
  5729. * possibly other systems) and applied another few minor cleanups.
  5730. *
  5731. * 2008-01-06 Holger Weiss <holger@jhweiss.de> for C99-snprintf 1.0:
  5732. *
  5733. * Added a lot of new features, fixed many bugs, and incorporated various
  5734. * improvements done by Andrew Tridgell <tridge@samba.org>, Russ Allbery
  5735. * <rra@stanford.edu>, Hrvoje Niksic <hniksic@xemacs.org>, Damien Miller
  5736. * <djm@mindrot.org>, and others for the Samba, INN, Wget, and OpenSSH
  5737. * projects. The additions include: support the "e", "E", "g", "G", and
  5738. * "F" conversion specifiers (and use conversion style "f" or "F" for the
  5739. * still unsupported "a" and "A" specifiers); support the "hh", "ll", "j",
  5740. * "t", and "z" length modifiers; support the "#" flag and the (non-C99)
  5741. * "'" flag; use localeconv(3) (if available) to get both the current
  5742. * locale's decimal point character and the separator between groups of
  5743. * digits; fix the handling of various corner cases of field width and
  5744. * precision specifications; fix various floating point conversion bugs;
  5745. * handle infinite and NaN floating point values; don't attempt to write to
  5746. * the output buffer (which may be NULL) if a size of zero was specified;
  5747. * check for integer overflow of the field width, precision, and return
  5748. * values and during the floating point conversion; use the OUTCHAR() macro
  5749. * instead of a function for better performance; provide asprintf(3) and
  5750. * vasprintf(3) functions; add new test cases. The replacement functions
  5751. * have been renamed to use an "rpl_" prefix, the function calls in the
  5752. * main project (and in this file) must be redefined accordingly for each
  5753. * replacement function which is needed (by using Autoconf or other means).
  5754. * Various other minor improvements have been applied and the coding style
  5755. * was cleaned up for consistency.
  5756. *
  5757. * 2007-07-23 Holger Weiss <holger@jhweiss.de> for Mutt 1.5.13:
  5758. *
  5759. * C99 compliant snprintf(3) and vsnprintf(3) functions return the number
  5760. * of characters that would have been written to a sufficiently sized
  5761. * buffer (excluding the '\0'). The original code simply returned the
  5762. * length of the resulting output string, so that's been fixed.
  5763. *
  5764. * 1998-03-05 Michael Elkins <me@mutt.org> for Mutt 0.90.8:
  5765. *
  5766. * The original code assumed that both snprintf(3) and vsnprintf(3) were
  5767. * missing. Some systems only have snprintf(3) but not vsnprintf(3), so
  5768. * the code is now broken down under HAVE_SNPRINTF and HAVE_VSNPRINTF.
  5769. *
  5770. * 1998-01-27 Thomas Roessler <roessler@does-not-exist.org> for Mutt 0.89i:
  5771. *
  5772. * The PGP code was using unsigned hexadecimal formats. Unfortunately,
  5773. * unsigned formats simply didn't work.
  5774. *
  5775. * 1997-10-22 Brandon Long <blong@fiction.net> for Mutt 0.87.1:
  5776. *
  5777. * Ok, added some minimal floating point support, which means this probably
  5778. * requires libm on most operating systems. Don't yet support the exponent
  5779. * (e,E) and sigfig (g,G). Also, fmtint() was pretty badly broken, it just
  5780. * wasn't being exercised in ways which showed it, so that's been fixed.
  5781. * Also, formatted the code to Mutt conventions, and removed dead code left
  5782. * over from the original. Also, there is now a builtin-test, run with:
  5783. * gcc -DTEST_SNPRINTF -o snprintf snprintf.c -lm && ./snprintf
  5784. *
  5785. * 2996-09-15 Brandon Long <blong@fiction.net> for Mutt 0.43:
  5786. *
  5787. * This was ugly. It is still ugly. I opted out of floating point
  5788. * numbers, but the formatter understands just about everything from the
  5789. * normal C string format, at least as far as I can tell from the Solaris
  5790. * 2.5 printf(3S) man page.
  5791. */
  5792. /*
  5793. * ToDo
  5794. *
  5795. * - Add wide character support.
  5796. * - Add support for "%a" and "%A" conversions.
  5797. * - Create test routines which predefine the expected results. Our test cases
  5798. * usually expose bugs in system implementations rather than in ours :-)
  5799. */
  5800. /*
  5801. * Usage
  5802. *
  5803. * 1) The following preprocessor macros should be defined to 1 if the feature or
  5804. * file in question is available on the target system (by using Autoconf or
  5805. * other means), though basic functionality should be available as long as
  5806. * HAVE_STDARG_H and HAVE_STDLIB_H are defined correctly:
  5807. *
  5808. * HAVE_VSNPRINTF
  5809. * HAVE_SNPRINTF
  5810. * HAVE_VASPRINTF
  5811. * HAVE_ASPRINTF
  5812. * HAVE_STDARG_H
  5813. * HAVE_STDDEF_H
  5814. * HAVE_STDINT_H
  5815. * HAVE_STDLIB_H
  5816. * HAVE_INTTYPES_H
  5817. * HAVE_LOCALE_H
  5818. * HAVE_LOCALECONV
  5819. * HAVE_LCONV_DECIMAL_POINT
  5820. * HAVE_LCONV_THOUSANDS_SEP
  5821. * HAVE_LONG_DOUBLE
  5822. * HAVE_LONG_LONG_INT
  5823. * HAVE_UNSIGNED_LONG_LONG_INT
  5824. * HAVE_INTMAX_T
  5825. * HAVE_UINTMAX_T
  5826. * HAVE_UINTPTR_T
  5827. * HAVE_PTRDIFF_T
  5828. * HAVE_VA_COPY
  5829. * HAVE___VA_COPY
  5830. *
  5831. * 2) The calls to the functions which should be replaced must be redefined
  5832. * throughout the project files (by using Autoconf or other means):
  5833. *
  5834. * #define vsnprintf rpl_vsnprintf
  5835. * #define snprintf rpl_snprintf
  5836. * #define vasprintf rpl_vasprintf
  5837. * #define asprintf rpl_asprintf
  5838. *
  5839. * 3) The required replacement functions should be declared in some header file
  5840. * included throughout the project files:
  5841. *
  5842. * #if HAVE_CONFIG_H
  5843. * #include <config.h>
  5844. * #endif
  5845. * #if HAVE_STDARG_H
  5846. * #include <stdarg.h>
  5847. * #if !HAVE_VSNPRINTF
  5848. * int rpl_vsnprintf(char *, size_t, const char *, va_list);
  5849. * #endif
  5850. * #if !HAVE_SNPRINTF
  5851. * int rpl_snprintf(char *, size_t, const char *, ...);
  5852. * #endif
  5853. * #if !HAVE_VASPRINTF
  5854. * int rpl_vasprintf(char **, const char *, va_list);
  5855. * #endif
  5856. * #if !HAVE_ASPRINTF
  5857. * int rpl_asprintf(char **, const char *, ...);
  5858. * #endif
  5859. * #endif
  5860. *
  5861. * Autoconf macros for handling step 1 and step 2 are available at
  5862. * <http://www.jhweiss.de/software/snprintf.html>.
  5863. */
  5864. #if !HAVE_SNPRINTF || !HAVE_VSNPRINTF || !HAVE_ASPRINTF || !HAVE_VASPRINTF
  5865. #include <stdio.h> /* For NULL, size_t, vsnprintf(3), and vasprintf(3). */
  5866. #ifdef VA_START
  5867. #undef VA_START
  5868. #endif /* defined(VA_START) */
  5869. #ifdef VA_SHIFT
  5870. #undef VA_SHIFT
  5871. #endif /* defined(VA_SHIFT) */
  5872. #if HAVE_STDARG_H
  5873. #include <stdarg.h>
  5874. #define VA_START(ap, last) va_start(ap, last)
  5875. #define VA_SHIFT(ap, value, type) /* No-op for ANSI C. */
  5876. #else /* Assume <varargs.h> is available. */
  5877. #include <varargs.h>
  5878. #define VA_START(ap, last) va_start(ap) /* "last" is ignored. */
  5879. #define VA_SHIFT(ap, value, type) value = va_arg(ap, type)
  5880. #endif /* HAVE_STDARG_H */
  5881. #if !HAVE_VASPRINTF
  5882. #if HAVE_STDLIB_H
  5883. #include <stdlib.h> /* For malloc(3). */
  5884. #endif /* HAVE_STDLIB_H */
  5885. #ifdef VA_COPY
  5886. #undef VA_COPY
  5887. #endif /* defined(VA_COPY) */
  5888. #ifdef VA_END_COPY
  5889. #undef VA_END_COPY
  5890. #endif /* defined(VA_END_COPY) */
  5891. #if HAVE_VA_COPY
  5892. #define VA_COPY(dest, src) va_copy(dest, src)
  5893. #define VA_END_COPY(ap) va_end(ap)
  5894. #elif HAVE___VA_COPY
  5895. #define VA_COPY(dest, src) __va_copy(dest, src)
  5896. #define VA_END_COPY(ap) va_end(ap)
  5897. #else
  5898. #define VA_COPY(dest, src) (void)mymemcpy(&dest, &src, sizeof(va_list))
  5899. #define VA_END_COPY(ap) /* No-op. */
  5900. #define NEED_MYMEMCPY 1
  5901. static void *mymemcpy(void *, void *, size_t);
  5902. #endif /* HAVE_VA_COPY */
  5903. #endif /* !HAVE_VASPRINTF */
  5904. #if !HAVE_VSNPRINTF
  5905. #include <errno.h> /* For ERANGE and errno. */
  5906. #include <limits.h> /* For *_MAX. */
  5907. #if HAVE_INTTYPES_H
  5908. #include <inttypes.h> /* For intmax_t (if not defined in <stdint.h>). */
  5909. #endif /* HAVE_INTTYPES_H */
  5910. #if HAVE_LOCALE_H
  5911. #include <locale.h> /* For localeconv(3). */
  5912. #endif /* HAVE_LOCALE_H */
  5913. #if HAVE_STDDEF_H
  5914. #include <stddef.h> /* For ptrdiff_t. */
  5915. #endif /* HAVE_STDDEF_H */
  5916. #if HAVE_STDINT_H
  5917. #include <stdint.h> /* For intmax_t. */
  5918. #endif /* HAVE_STDINT_H */
  5919. /* Support for unsigned long long int. We may also need ULLONG_MAX. */
  5920. #ifndef ULONG_MAX /* We may need ULONG_MAX as a fallback. */
  5921. #ifdef UINT_MAX
  5922. #define ULONG_MAX UINT_MAX
  5923. #else
  5924. #define ULONG_MAX INT_MAX
  5925. #endif /* defined(UINT_MAX) */
  5926. #endif /* !defined(ULONG_MAX) */
  5927. #ifdef ULLONG
  5928. #undef ULLONG
  5929. #endif /* defined(ULLONG) */
  5930. #if HAVE_UNSIGNED_LONG_LONG_INT
  5931. #define ULLONG unsigned long long int
  5932. #ifndef ULLONG_MAX
  5933. #define ULLONG_MAX ULONG_MAX
  5934. #endif /* !defined(ULLONG_MAX) */
  5935. #else
  5936. #define ULLONG unsigned long int
  5937. #ifdef ULLONG_MAX
  5938. #undef ULLONG_MAX
  5939. #endif /* defined(ULLONG_MAX) */
  5940. #define ULLONG_MAX ULONG_MAX
  5941. #endif /* HAVE_LONG_LONG_INT */
  5942. /* Support for uintmax_t. We also need UINTMAX_MAX. */
  5943. #ifdef UINTMAX_T
  5944. #undef UINTMAX_T
  5945. #endif /* defined(UINTMAX_T) */
  5946. #if HAVE_UINTMAX_T || defined(uintmax_t)
  5947. #define UINTMAX_T uintmax_t
  5948. #ifndef UINTMAX_MAX
  5949. #define UINTMAX_MAX ULLONG_MAX
  5950. #endif /* !defined(UINTMAX_MAX) */
  5951. #else
  5952. #define UINTMAX_T ULLONG
  5953. #ifdef UINTMAX_MAX
  5954. #undef UINTMAX_MAX
  5955. #endif /* defined(UINTMAX_MAX) */
  5956. #define UINTMAX_MAX ULLONG_MAX
  5957. #endif /* HAVE_UINTMAX_T || defined(uintmax_t) */
  5958. /* Support for long double. */
  5959. #ifndef LDOUBLE
  5960. #if HAVE_LONG_DOUBLE
  5961. #define LDOUBLE long double
  5962. #else
  5963. #define LDOUBLE double
  5964. #endif /* HAVE_LONG_DOUBLE */
  5965. #endif /* !defined(LDOUBLE) */
  5966. /* Support for long long int. */
  5967. #ifndef LLONG
  5968. #if HAVE_LONG_LONG_INT
  5969. #define LLONG long long int
  5970. #else
  5971. #define LLONG long int
  5972. #endif /* HAVE_LONG_LONG_INT */
  5973. #endif /* !defined(LLONG) */
  5974. /* Support for intmax_t. */
  5975. #ifndef INTMAX_T
  5976. #if HAVE_INTMAX_T || defined(intmax_t)
  5977. #define INTMAX_T intmax_t
  5978. #else
  5979. #define INTMAX_T LLONG
  5980. #endif /* HAVE_INTMAX_T || defined(intmax_t) */
  5981. #endif /* !defined(INTMAX_T) */
  5982. /* Support for uintptr_t. */
  5983. #ifndef UINTPTR_T
  5984. #if HAVE_UINTPTR_T || defined(uintptr_t)
  5985. #define UINTPTR_T uintptr_t
  5986. #else
  5987. #define UINTPTR_T unsigned long int
  5988. #endif /* HAVE_UINTPTR_T || defined(uintptr_t) */
  5989. #endif /* !defined(UINTPTR_T) */
  5990. /* Support for ptrdiff_t. */
  5991. #ifndef PTRDIFF_T
  5992. #if HAVE_PTRDIFF_T || defined(ptrdiff_t)
  5993. #define PTRDIFF_T ptrdiff_t
  5994. #else
  5995. #define PTRDIFF_T long int
  5996. #endif /* HAVE_PTRDIFF_T || defined(ptrdiff_t) */
  5997. #endif /* !defined(PTRDIFF_T) */
  5998. /*
  5999. * We need an unsigned integer type corresponding to ptrdiff_t (cf. C99:
  6000. * 7.19.6.1, 7). However, we'll simply use PTRDIFF_T and convert it to an
  6001. * unsigned type if necessary. This should work just fine in practice.
  6002. */
  6003. #ifndef UPTRDIFF_T
  6004. #define UPTRDIFF_T PTRDIFF_T
  6005. #endif /* !defined(UPTRDIFF_T) */
  6006. /*
  6007. * We need a signed integer type corresponding to size_t (cf. C99: 7.19.6.1, 7).
  6008. * However, we'll simply use size_t and convert it to a signed type if
  6009. * necessary. This should work just fine in practice.
  6010. */
  6011. #ifndef SSIZE_T
  6012. /* MinGW uses typedef __SIZE_TYPE__ size_t so #define SSIZE_T size_t clashes */
  6013. #ifndef __MINGW32__
  6014. #define SSIZE_T size_t
  6015. #endif /* !defined(__MINGW32__) */
  6016. #endif /* !defined(SSIZE_T) */
  6017. /* Either ERANGE or E2BIG should be available everywhere. */
  6018. #ifndef ERANGE
  6019. #define ERANGE E2BIG
  6020. #endif /* !defined(ERANGE) */
  6021. #ifndef EOVERFLOW
  6022. #define EOVERFLOW ERANGE
  6023. #endif /* !defined(EOVERFLOW) */
  6024. /*
  6025. * Buffer size to hold the octal string representation of UINT128_MAX without
  6026. * nul-termination ("3777777777777777777777777777777777777777777").
  6027. */
  6028. #ifdef MAX_CONVERT_LENGTH
  6029. #undef MAX_CONVERT_LENGTH
  6030. #endif /* defined(MAX_CONVERT_LENGTH) */
  6031. #define MAX_CONVERT_LENGTH 43
  6032. /* Format read states. */
  6033. #define PRINT_S_DEFAULT 0
  6034. #define PRINT_S_FLAGS 1
  6035. #define PRINT_S_WIDTH 2
  6036. #define PRINT_S_DOT 3
  6037. #define PRINT_S_PRECISION 4
  6038. #define PRINT_S_MOD 5
  6039. #define PRINT_S_CONV 6
  6040. /* Format flags. */
  6041. #define PRINT_F_MINUS (1 << 0)
  6042. #define PRINT_F_PLUS (1 << 1)
  6043. #define PRINT_F_SPACE (1 << 2)
  6044. #define PRINT_F_NUM (1 << 3)
  6045. #define PRINT_F_ZERO (1 << 4)
  6046. #define PRINT_F_QUOTE (1 << 5)
  6047. #define PRINT_F_UP (1 << 6)
  6048. #define PRINT_F_UNSIGNED (1 << 7)
  6049. #define PRINT_F_TYPE_G (1 << 8)
  6050. #define PRINT_F_TYPE_E (1 << 9)
  6051. /* Conversion flags. */
  6052. #define PRINT_C_CHAR 1
  6053. #define PRINT_C_SHORT 2
  6054. #define PRINT_C_LONG 3
  6055. #define PRINT_C_LLONG 4
  6056. #define PRINT_C_LDOUBLE 5
  6057. #define PRINT_C_SIZE 6
  6058. #define PRINT_C_PTRDIFF 7
  6059. #define PRINT_C_INTMAX 8
  6060. #ifndef MAX
  6061. #define MAX(x, y) ((x >= y) ? x : y)
  6062. #endif /* !defined(MAX) */
  6063. #ifndef CHARTOINT
  6064. #define CHARTOINT(ch) (ch - '0')
  6065. #endif /* !defined(CHARTOINT) */
  6066. #ifndef ISDIGIT
  6067. #define ISDIGIT(ch) ('0' <= (unsigned char)ch && (unsigned char)ch <= '9')
  6068. #endif /* !defined(ISDIGIT) */
  6069. #ifndef ISNAN
  6070. #define ISNAN(x) (x != x)
  6071. #endif /* !defined(ISNAN) */
  6072. #ifndef ISINF
  6073. #define ISINF(x) (x != 0.0 && x + x == x)
  6074. #endif /* !defined(ISINF) */
  6075. #ifdef OUTCHAR
  6076. #undef OUTCHAR
  6077. #endif /* defined(OUTCHAR) */
  6078. #define OUTCHAR(str, len, size, ch) \
  6079. do { \
  6080. if (len + 1 < size) \
  6081. str[len] = ch; \
  6082. (len)++; \
  6083. } while (/* CONSTCOND */ 0)
  6084. static void fmtstr(char *, size_t *, size_t, const char *, int, int, int);
  6085. static void fmtint(char *, size_t *, size_t, INTMAX_T, int, int, int, int);
  6086. static void fmtflt(char *, size_t *, size_t, LDOUBLE, int, int, int, int *);
  6087. static void printsep(char *, size_t *, size_t);
  6088. static int getnumsep(int);
  6089. static int getexponent(LDOUBLE);
  6090. static int convert(UINTMAX_T, char *, size_t, int, int);
  6091. static UINTMAX_T cast(LDOUBLE);
  6092. static UINTMAX_T myround(LDOUBLE);
  6093. static LDOUBLE mypow10(int);
  6094. int
  6095. rpl_vsnprintf(char *str, size_t size, const char *format, va_list args)
  6096. {
  6097. LDOUBLE fvalue;
  6098. INTMAX_T value;
  6099. unsigned char cvalue;
  6100. const char *strvalue;
  6101. INTMAX_T *intmaxptr;
  6102. PTRDIFF_T *ptrdiffptr;
  6103. size_t *sizeptr;
  6104. LLONG *llongptr;
  6105. int *longptr;
  6106. int *intptr;
  6107. short int *shortptr;
  6108. signed char *charptr;
  6109. size_t len = 0;
  6110. int overflow = 0;
  6111. int base = 0;
  6112. int cflags = 0;
  6113. int flags = 0;
  6114. int width = 0;
  6115. int precision = -1;
  6116. int state = PRINT_S_DEFAULT;
  6117. char ch = *format++;
  6118. /*
  6119. * C99 says: "If `n' is zero, nothing is written, and `s' may be a null
  6120. * pointer." (7.19.6.5, 2) We're forgiving and allow a NULL pointer
  6121. * even if a size larger than zero was specified. At least NetBSD's
  6122. * snprintf(3) does the same, as well as other versions of this file.
  6123. * (Though some of these versions will write to a non-NULL buffer even
  6124. * if a size of zero was specified, which violates the standard.)
  6125. */
  6126. if (str == NULL && size != 0)
  6127. size = 0;
  6128. while (ch != '\0')
  6129. switch (state) {
  6130. case PRINT_S_DEFAULT:
  6131. if (ch == '%')
  6132. state = PRINT_S_FLAGS;
  6133. else
  6134. OUTCHAR(str, len, size, ch);
  6135. ch = *format++;
  6136. break;
  6137. case PRINT_S_FLAGS:
  6138. switch (ch) {
  6139. case '-':
  6140. flags |= PRINT_F_MINUS;
  6141. ch = *format++;
  6142. break;
  6143. case '+':
  6144. flags |= PRINT_F_PLUS;
  6145. ch = *format++;
  6146. break;
  6147. case ' ':
  6148. flags |= PRINT_F_SPACE;
  6149. ch = *format++;
  6150. break;
  6151. case '#':
  6152. flags |= PRINT_F_NUM;
  6153. ch = *format++;
  6154. break;
  6155. case '0':
  6156. flags |= PRINT_F_ZERO;
  6157. ch = *format++;
  6158. break;
  6159. case '\'': /* SUSv2 flag (not in C99). */
  6160. flags |= PRINT_F_QUOTE;
  6161. ch = *format++;
  6162. break;
  6163. default:
  6164. state = PRINT_S_WIDTH;
  6165. break;
  6166. }
  6167. break;
  6168. case PRINT_S_WIDTH:
  6169. if (ISDIGIT(ch)) {
  6170. ch = CHARTOINT(ch);
  6171. if (width > (INT_MAX - ch) / 10) {
  6172. overflow = 1;
  6173. goto out;
  6174. }
  6175. width = 10 * width + ch;
  6176. ch = *format++;
  6177. } else if (ch == '*') {
  6178. /*
  6179. * C99 says: "A negative field width argument is
  6180. * taken as a `-' flag followed by a positive
  6181. * field width." (7.19.6.1, 5)
  6182. */
  6183. if ((width = va_arg(args, int)) < 0) {
  6184. flags |= PRINT_F_MINUS;
  6185. width = -width;
  6186. }
  6187. ch = *format++;
  6188. state = PRINT_S_DOT;
  6189. } else
  6190. state = PRINT_S_DOT;
  6191. break;
  6192. case PRINT_S_DOT:
  6193. if (ch == '.') {
  6194. state = PRINT_S_PRECISION;
  6195. ch = *format++;
  6196. } else
  6197. state = PRINT_S_MOD;
  6198. break;
  6199. case PRINT_S_PRECISION:
  6200. if (precision == -1)
  6201. precision = 0;
  6202. if (ISDIGIT(ch)) {
  6203. ch = CHARTOINT(ch);
  6204. if (precision > (INT_MAX - ch) / 10) {
  6205. overflow = 1;
  6206. goto out;
  6207. }
  6208. precision = 10 * precision + ch;
  6209. ch = *format++;
  6210. } else if (ch == '*') {
  6211. /*
  6212. * C99 says: "A negative precision argument is
  6213. * taken as if the precision were omitted."
  6214. * (7.19.6.1, 5)
  6215. */
  6216. if ((precision = va_arg(args, int)) < 0)
  6217. precision = -1;
  6218. ch = *format++;
  6219. state = PRINT_S_MOD;
  6220. } else
  6221. state = PRINT_S_MOD;
  6222. break;
  6223. case PRINT_S_MOD:
  6224. switch (ch) {
  6225. case 'h':
  6226. ch = *format++;
  6227. if (ch == 'h') { /* It's a char. */
  6228. ch = *format++;
  6229. cflags = PRINT_C_CHAR;
  6230. } else
  6231. cflags = PRINT_C_SHORT;
  6232. break;
  6233. case 'l':
  6234. ch = *format++;
  6235. if (ch == 'l') { /* It's a long long. */
  6236. ch = *format++;
  6237. cflags = PRINT_C_LLONG;
  6238. } else
  6239. cflags = PRINT_C_LONG;
  6240. break;
  6241. case 'L':
  6242. cflags = PRINT_C_LDOUBLE;
  6243. ch = *format++;
  6244. break;
  6245. case 'j':
  6246. cflags = PRINT_C_INTMAX;
  6247. ch = *format++;
  6248. break;
  6249. case 't':
  6250. cflags = PRINT_C_PTRDIFF;
  6251. ch = *format++;
  6252. break;
  6253. case 'z':
  6254. cflags = PRINT_C_SIZE;
  6255. ch = *format++;
  6256. break;
  6257. }
  6258. state = PRINT_S_CONV;
  6259. break;
  6260. case PRINT_S_CONV:
  6261. switch (ch) {
  6262. case 'd':
  6263. /* FALLTHROUGH */
  6264. case 'i':
  6265. switch (cflags) {
  6266. case PRINT_C_CHAR:
  6267. value = (signed char)va_arg(args, int);
  6268. break;
  6269. case PRINT_C_SHORT:
  6270. value = (short int)va_arg(args, int);
  6271. break;
  6272. case PRINT_C_LONG:
  6273. value = va_arg(args, int);
  6274. break;
  6275. case PRINT_C_LLONG:
  6276. value = va_arg(args, LLONG);
  6277. break;
  6278. case PRINT_C_SIZE:
  6279. value = va_arg(args, size_t);
  6280. break;
  6281. case PRINT_C_INTMAX:
  6282. value = va_arg(args, INTMAX_T);
  6283. break;
  6284. case PRINT_C_PTRDIFF:
  6285. value = va_arg(args, PTRDIFF_T);
  6286. break;
  6287. default:
  6288. value = va_arg(args, int);
  6289. break;
  6290. }
  6291. fmtint(str, &len, size, value, 10, width,
  6292. precision, flags);
  6293. break;
  6294. case 'X':
  6295. flags |= PRINT_F_UP;
  6296. /* FALLTHROUGH */
  6297. case 'x':
  6298. base = 16;
  6299. /* FALLTHROUGH */
  6300. case 'o':
  6301. if (base == 0)
  6302. base = 8;
  6303. /* FALLTHROUGH */
  6304. case 'u':
  6305. if (base == 0)
  6306. base = 10;
  6307. flags |= PRINT_F_UNSIGNED;
  6308. switch (cflags) {
  6309. case PRINT_C_CHAR:
  6310. value = (unsigned char)va_arg(args,
  6311. unsigned int);
  6312. break;
  6313. case PRINT_C_SHORT:
  6314. value = (unsigned short int)va_arg(args,
  6315. unsigned int);
  6316. break;
  6317. case PRINT_C_LONG:
  6318. value = va_arg(args, unsigned int);
  6319. break;
  6320. case PRINT_C_LLONG:
  6321. value = va_arg(args, ULLONG);
  6322. break;
  6323. case PRINT_C_SIZE:
  6324. value = va_arg(args, size_t);
  6325. break;
  6326. case PRINT_C_INTMAX:
  6327. value = va_arg(args, UINTMAX_T);
  6328. break;
  6329. case PRINT_C_PTRDIFF:
  6330. value = va_arg(args, UPTRDIFF_T);
  6331. break;
  6332. default:
  6333. value = va_arg(args, unsigned int);
  6334. break;
  6335. }
  6336. fmtint(str, &len, size, value, base, width,
  6337. precision, flags);
  6338. break;
  6339. case 'A':
  6340. /* Not yet supported, we'll use "%F". */
  6341. /* FALLTHROUGH */
  6342. case 'F':
  6343. flags |= PRINT_F_UP;
  6344. case 'a':
  6345. /* Not yet supported, we'll use "%f". */
  6346. /* FALLTHROUGH */
  6347. case 'f':
  6348. if (cflags == PRINT_C_LDOUBLE)
  6349. fvalue = va_arg(args, LDOUBLE);
  6350. else
  6351. fvalue = va_arg(args, double);
  6352. fmtflt(str, &len, size, fvalue, width,
  6353. precision, flags, &overflow);
  6354. if (overflow)
  6355. goto out;
  6356. break;
  6357. case 'E':
  6358. flags |= PRINT_F_UP;
  6359. /* FALLTHROUGH */
  6360. case 'e':
  6361. flags |= PRINT_F_TYPE_E;
  6362. if (cflags == PRINT_C_LDOUBLE)
  6363. fvalue = va_arg(args, LDOUBLE);
  6364. else
  6365. fvalue = va_arg(args, double);
  6366. fmtflt(str, &len, size, fvalue, width,
  6367. precision, flags, &overflow);
  6368. if (overflow)
  6369. goto out;
  6370. break;
  6371. case 'G':
  6372. flags |= PRINT_F_UP;
  6373. /* FALLTHROUGH */
  6374. case 'g':
  6375. flags |= PRINT_F_TYPE_G;
  6376. if (cflags == PRINT_C_LDOUBLE)
  6377. fvalue = va_arg(args, LDOUBLE);
  6378. else
  6379. fvalue = va_arg(args, double);
  6380. /*
  6381. * If the precision is zero, it is treated as
  6382. * one (cf. C99: 7.19.6.1, 8).
  6383. */
  6384. if (precision == 0)
  6385. precision = 1;
  6386. fmtflt(str, &len, size, fvalue, width,
  6387. precision, flags, &overflow);
  6388. if (overflow)
  6389. goto out;
  6390. break;
  6391. case 'c':
  6392. cvalue = va_arg(args, int);
  6393. OUTCHAR(str, len, size, cvalue);
  6394. break;
  6395. case 's':
  6396. strvalue = va_arg(args, char *);
  6397. fmtstr(str, &len, size, strvalue, width,
  6398. precision, flags);
  6399. break;
  6400. case 'p':
  6401. /*
  6402. * C99 says: "The value of the pointer is
  6403. * converted to a sequence of printing
  6404. * characters, in an implementation-defined
  6405. * manner." (C99: 7.19.6.1, 8)
  6406. */
  6407. if ((strvalue = (const char*)va_arg(args, void *)) == NULL)
  6408. /*
  6409. * We use the glibc format. BSD prints
  6410. * "0x0", SysV "0".
  6411. */
  6412. fmtstr(str, &len, size, "(nil)", width,
  6413. -1, flags);
  6414. else {
  6415. /*
  6416. * We use the BSD/glibc format. SysV
  6417. * omits the "0x" prefix (which we emit
  6418. * using the PRINT_F_NUM flag).
  6419. */
  6420. flags |= PRINT_F_NUM;
  6421. flags |= PRINT_F_UNSIGNED;
  6422. fmtint(str, &len, size,
  6423. (UINTPTR_T)strvalue, 16, width,
  6424. precision, flags);
  6425. }
  6426. break;
  6427. case 'n':
  6428. switch (cflags) {
  6429. case PRINT_C_CHAR:
  6430. charptr = va_arg(args, signed char *);
  6431. *charptr = len;
  6432. break;
  6433. case PRINT_C_SHORT:
  6434. shortptr = va_arg(args, short int *);
  6435. *shortptr = len;
  6436. break;
  6437. case PRINT_C_LONG:
  6438. longptr = va_arg(args, int *);
  6439. *longptr = len;
  6440. break;
  6441. case PRINT_C_LLONG:
  6442. llongptr = va_arg(args, LLONG *);
  6443. *llongptr = len;
  6444. break;
  6445. case PRINT_C_SIZE:
  6446. /*
  6447. * C99 says that with the "z" length
  6448. * modifier, "a following `n' conversion
  6449. * specifier applies to a pointer to a
  6450. * signed integer type corresponding to
  6451. * size_t argument." (7.19.6.1, 7)
  6452. */
  6453. sizeptr = va_arg(args, size_t *);
  6454. *sizeptr = len;
  6455. break;
  6456. case PRINT_C_INTMAX:
  6457. intmaxptr = va_arg(args, INTMAX_T *);
  6458. *intmaxptr = len;
  6459. break;
  6460. case PRINT_C_PTRDIFF:
  6461. ptrdiffptr = va_arg(args, PTRDIFF_T *);
  6462. *ptrdiffptr = len;
  6463. break;
  6464. default:
  6465. intptr = va_arg(args, int *);
  6466. *intptr = len;
  6467. break;
  6468. }
  6469. break;
  6470. case '%': /* Print a "%" character verbatim. */
  6471. OUTCHAR(str, len, size, ch);
  6472. break;
  6473. default: /* Skip other characters. */
  6474. break;
  6475. }
  6476. ch = *format++;
  6477. state = PRINT_S_DEFAULT;
  6478. base = cflags = flags = width = 0;
  6479. precision = -1;
  6480. break;
  6481. }
  6482. out:
  6483. if (len < size)
  6484. str[len] = '\0';
  6485. else if (size > 0)
  6486. str[size - 1] = '\0';
  6487. if (overflow || len >= INT_MAX) {
  6488. errno = overflow ? EOVERFLOW : ERANGE;
  6489. return -1;
  6490. }
  6491. return (int)len;
  6492. }
  6493. static void
  6494. fmtstr(char *str, size_t *len, size_t size, const char *value, int width,
  6495. int precision, int flags)
  6496. {
  6497. int padlen, strln; /* Amount to pad. */
  6498. int noprecision = (precision == -1);
  6499. if (value == NULL) /* We're forgiving. */
  6500. value = "(null)";
  6501. /* If a precision was specified, don't read the string past it. */
  6502. for (strln = 0; value[strln] != '\0' &&
  6503. (noprecision || strln < precision); strln++)
  6504. continue;
  6505. if ((padlen = width - strln) < 0)
  6506. padlen = 0;
  6507. if (flags & PRINT_F_MINUS) /* Left justify. */
  6508. padlen = -padlen;
  6509. while (padlen > 0) { /* Leading spaces. */
  6510. OUTCHAR(str, *len, size, ' ');
  6511. padlen--;
  6512. }
  6513. while (*value != '\0' && (noprecision || precision-- > 0)) {
  6514. OUTCHAR(str, *len, size, *value);
  6515. value++;
  6516. }
  6517. while (padlen < 0) { /* Trailing spaces. */
  6518. OUTCHAR(str, *len, size, ' ');
  6519. padlen++;
  6520. }
  6521. }
  6522. static void
  6523. fmtint(char *str, size_t *len, size_t size, INTMAX_T value, int base, int width,
  6524. int precision, int flags)
  6525. {
  6526. UINTMAX_T uvalue;
  6527. char iconvert[MAX_CONVERT_LENGTH];
  6528. char sign = 0;
  6529. char hexprefix = 0;
  6530. int spadlen = 0; /* Amount to space pad. */
  6531. int zpadlen = 0; /* Amount to zero pad. */
  6532. int pos;
  6533. int separators = (flags & PRINT_F_QUOTE);
  6534. int noprecision = (precision == -1);
  6535. if (flags & PRINT_F_UNSIGNED)
  6536. uvalue = value;
  6537. else {
  6538. uvalue = (value >= 0) ? value : -value;
  6539. if (value < 0)
  6540. sign = '-';
  6541. else if (flags & PRINT_F_PLUS) /* Do a sign. */
  6542. sign = '+';
  6543. else if (flags & PRINT_F_SPACE)
  6544. sign = ' ';
  6545. }
  6546. pos = convert(uvalue, iconvert, sizeof(iconvert), base,
  6547. flags & PRINT_F_UP);
  6548. if (flags & PRINT_F_NUM && uvalue != 0) {
  6549. /*
  6550. * C99 says: "The result is converted to an `alternative form'.
  6551. * For `o' conversion, it increases the precision, if and only
  6552. * if necessary, to force the first digit of the result to be a
  6553. * zero (if the value and precision are both 0, a single 0 is
  6554. * printed). For `x' (or `X') conversion, a nonzero result has
  6555. * `0x' (or `0X') prefixed to it." (7.19.6.1, 6)
  6556. */
  6557. switch (base) {
  6558. case 8:
  6559. if (precision <= pos)
  6560. precision = pos + 1;
  6561. break;
  6562. case 16:
  6563. hexprefix = (flags & PRINT_F_UP) ? 'X' : 'x';
  6564. break;
  6565. }
  6566. }
  6567. if (separators) /* Get the number of group separators we'll print. */
  6568. separators = getnumsep(pos);
  6569. zpadlen = precision - pos - separators;
  6570. spadlen = width /* Minimum field width. */
  6571. - separators /* Number of separators. */
  6572. - MAX(precision, pos) /* Number of integer digits. */
  6573. - ((sign != 0) ? 1 : 0) /* Will we print a sign? */
  6574. - ((hexprefix != 0) ? 2 : 0); /* Will we print a prefix? */
  6575. if (zpadlen < 0)
  6576. zpadlen = 0;
  6577. if (spadlen < 0)
  6578. spadlen = 0;
  6579. /*
  6580. * C99 says: "If the `0' and `-' flags both appear, the `0' flag is
  6581. * ignored. For `d', `i', `o', `u', `x', and `X' conversions, if a
  6582. * precision is specified, the `0' flag is ignored." (7.19.6.1, 6)
  6583. */
  6584. if (flags & PRINT_F_MINUS) /* Left justify. */
  6585. spadlen = -spadlen;
  6586. else if (flags & PRINT_F_ZERO && noprecision) {
  6587. zpadlen += spadlen;
  6588. spadlen = 0;
  6589. }
  6590. while (spadlen > 0) { /* Leading spaces. */
  6591. OUTCHAR(str, *len, size, ' ');
  6592. spadlen--;
  6593. }
  6594. if (sign != 0) /* Sign. */
  6595. OUTCHAR(str, *len, size, sign);
  6596. if (hexprefix != 0) { /* A "0x" or "0X" prefix. */
  6597. OUTCHAR(str, *len, size, '0');
  6598. OUTCHAR(str, *len, size, hexprefix);
  6599. }
  6600. while (zpadlen > 0) { /* Leading zeros. */
  6601. OUTCHAR(str, *len, size, '0');
  6602. zpadlen--;
  6603. }
  6604. while (pos > 0) { /* The actual digits. */
  6605. pos--;
  6606. OUTCHAR(str, *len, size, iconvert[pos]);
  6607. if (separators > 0 && pos > 0 && pos % 3 == 0)
  6608. printsep(str, len, size);
  6609. }
  6610. while (spadlen < 0) { /* Trailing spaces. */
  6611. OUTCHAR(str, *len, size, ' ');
  6612. spadlen++;
  6613. }
  6614. }
  6615. static void
  6616. fmtflt(char *str, size_t *len, size_t size, LDOUBLE fvalue, int width,
  6617. int precision, int flags, int *overflow)
  6618. {
  6619. LDOUBLE ufvalue;
  6620. UINTMAX_T intpart;
  6621. UINTMAX_T fracpart;
  6622. UINTMAX_T mask;
  6623. const char *infnan = NULL;
  6624. char iconvert[MAX_CONVERT_LENGTH];
  6625. char fconvert[MAX_CONVERT_LENGTH];
  6626. char econvert[4]; /* "e-12" (without nul-termination). */
  6627. char esign = 0;
  6628. char sign = 0;
  6629. int leadfraczeros = 0;
  6630. int exponent = 0;
  6631. int emitpoint = 0;
  6632. int omitzeros = 0;
  6633. int omitcount = 0;
  6634. int padlen = 0;
  6635. int epos = 0;
  6636. int fpos = 0;
  6637. int ipos = 0;
  6638. int separators = (flags & PRINT_F_QUOTE);
  6639. int estyle = (flags & PRINT_F_TYPE_E);
  6640. #if HAVE_LOCALECONV && HAVE_LCONV_DECIMAL_POINT
  6641. struct lconv *lc = localeconv();
  6642. #endif /* HAVE_LOCALECONV && HAVE_LCONV_DECIMAL_POINT */
  6643. /*
  6644. * AIX' man page says the default is 0, but C99 and at least Solaris'
  6645. * and NetBSD's man pages say the default is 6, and sprintf(3) on AIX
  6646. * defaults to 6.
  6647. */
  6648. if (precision == -1)
  6649. precision = 6;
  6650. if (fvalue < 0.0)
  6651. sign = '-';
  6652. else if (flags & PRINT_F_PLUS) /* Do a sign. */
  6653. sign = '+';
  6654. else if (flags & PRINT_F_SPACE)
  6655. sign = ' ';
  6656. if (ISNAN(fvalue))
  6657. infnan = (flags & PRINT_F_UP) ? "NAN" : "nan";
  6658. else if (ISINF(fvalue))
  6659. infnan = (flags & PRINT_F_UP) ? "INF" : "inf";
  6660. if (infnan != NULL) {
  6661. if (sign != 0)
  6662. iconvert[ipos++] = sign;
  6663. while (*infnan != '\0')
  6664. iconvert[ipos++] = *infnan++;
  6665. fmtstr(str, len, size, iconvert, width, ipos, flags);
  6666. return;
  6667. }
  6668. /* "%e" (or "%E") or "%g" (or "%G") conversion. */
  6669. if (flags & PRINT_F_TYPE_E || flags & PRINT_F_TYPE_G) {
  6670. if (flags & PRINT_F_TYPE_G) {
  6671. /*
  6672. * For "%g" (and "%G") conversions, the precision
  6673. * specifies the number of significant digits, which
  6674. * includes the digits in the integer part. The
  6675. * conversion will or will not be using "e-style" (like
  6676. * "%e" or "%E" conversions) depending on the precision
  6677. * and on the exponent. However, the exponent can be
  6678. * affected by rounding the converted value, so we'll
  6679. * leave this decision for later. Until then, we'll
  6680. * assume that we're going to do an "e-style" conversion
  6681. * (in order to get the exponent calculated). For
  6682. * "e-style", the precision must be decremented by one.
  6683. */
  6684. precision--;
  6685. /*
  6686. * For "%g" (and "%G") conversions, trailing zeros are
  6687. * removed from the fractional portion of the result
  6688. * unless the "#" flag was specified.
  6689. */
  6690. if (!(flags & PRINT_F_NUM))
  6691. omitzeros = 1;
  6692. }
  6693. exponent = getexponent(fvalue);
  6694. estyle = 1;
  6695. }
  6696. again:
  6697. /*
  6698. * Sorry, we only support 9, 19, or 38 digits (that is, the number of
  6699. * digits of the 32-bit, the 64-bit, or the 128-bit UINTMAX_MAX value
  6700. * minus one) past the decimal point due to our conversion method.
  6701. */
  6702. switch (sizeof(UINTMAX_T)) {
  6703. case 16:
  6704. if (precision > 38)
  6705. precision = 38;
  6706. break;
  6707. case 8:
  6708. if (precision > 19)
  6709. precision = 19;
  6710. break;
  6711. default:
  6712. if (precision > 9)
  6713. precision = 9;
  6714. break;
  6715. }
  6716. ufvalue = (fvalue >= 0.0) ? fvalue : -fvalue;
  6717. if (estyle) /* We want exactly one integer digit. */
  6718. ufvalue /= mypow10(exponent);
  6719. if ((intpart = cast(ufvalue)) == UINTMAX_MAX) {
  6720. *overflow = 1;
  6721. return;
  6722. }
  6723. /*
  6724. * Factor of ten with the number of digits needed for the fractional
  6725. * part. For example, if the precision is 3, the mask will be 1000.
  6726. */
  6727. mask = mypow10(precision);
  6728. /*
  6729. * We "cheat" by converting the fractional part to integer by
  6730. * multiplying by a factor of ten.
  6731. */
  6732. if ((fracpart = myround(mask * (ufvalue - intpart))) >= mask) {
  6733. /*
  6734. * For example, ufvalue = 2.99962, intpart = 2, and mask = 1000
  6735. * (because precision = 3). Now, myround(1000 * 0.99962) will
  6736. * return 1000. So, the integer part must be incremented by one
  6737. * and the fractional part must be set to zero.
  6738. */
  6739. intpart++;
  6740. fracpart = 0;
  6741. if (estyle && intpart == 10) {
  6742. /*
  6743. * The value was rounded up to ten, but we only want one
  6744. * integer digit if using "e-style". So, the integer
  6745. * part must be set to one and the exponent must be
  6746. * incremented by one.
  6747. */
  6748. intpart = 1;
  6749. exponent++;
  6750. }
  6751. }
  6752. /*
  6753. * Now that we know the real exponent, we can check whether or not to
  6754. * use "e-style" for "%g" (and "%G") conversions. If we don't need
  6755. * "e-style", the precision must be adjusted and the integer and
  6756. * fractional parts must be recalculated from the original value.
  6757. *
  6758. * C99 says: "Let P equal the precision if nonzero, 6 if the precision
  6759. * is omitted, or 1 if the precision is zero. Then, if a conversion
  6760. * with style `E' would have an exponent of X:
  6761. *
  6762. * - if P > X >= -4, the conversion is with style `f' (or `F') and
  6763. * precision P - (X + 1).
  6764. *
  6765. * - otherwise, the conversion is with style `e' (or `E') and precision
  6766. * P - 1." (7.19.6.1, 8)
  6767. *
  6768. * Note that we had decremented the precision by one.
  6769. */
  6770. if (flags & PRINT_F_TYPE_G && estyle &&
  6771. precision + 1 > exponent && exponent >= -4) {
  6772. precision -= exponent;
  6773. estyle = 0;
  6774. goto again;
  6775. }
  6776. if (estyle) {
  6777. if (exponent < 0) {
  6778. exponent = -exponent;
  6779. esign = '-';
  6780. } else
  6781. esign = '+';
  6782. /*
  6783. * Convert the exponent. The sizeof(econvert) is 4. So, the
  6784. * econvert buffer can hold e.g. "e+99" and "e-99". We don't
  6785. * support an exponent which contains more than two digits.
  6786. * Therefore, the following stores are safe.
  6787. */
  6788. epos = convert(exponent, econvert, 2, 10, 0);
  6789. /*
  6790. * C99 says: "The exponent always contains at least two digits,
  6791. * and only as many more digits as necessary to represent the
  6792. * exponent." (7.19.6.1, 8)
  6793. */
  6794. if (epos == 1)
  6795. econvert[epos++] = '0';
  6796. econvert[epos++] = esign;
  6797. econvert[epos++] = (flags & PRINT_F_UP) ? 'E' : 'e';
  6798. }
  6799. /* Convert the integer part and the fractional part. */
  6800. ipos = convert(intpart, iconvert, sizeof(iconvert), 10, 0);
  6801. if (fracpart != 0) /* convert() would return 1 if fracpart == 0. */
  6802. fpos = convert(fracpart, fconvert, sizeof(fconvert), 10, 0);
  6803. leadfraczeros = precision - fpos;
  6804. if (omitzeros) {
  6805. if (fpos > 0) /* Omit trailing fractional part zeros. */
  6806. while (omitcount < fpos && fconvert[omitcount] == '0')
  6807. omitcount++;
  6808. else { /* The fractional part is zero, omit it completely. */
  6809. omitcount = precision;
  6810. leadfraczeros = 0;
  6811. }
  6812. precision -= omitcount;
  6813. }
  6814. /*
  6815. * Print a decimal point if either the fractional part is non-zero
  6816. * and/or the "#" flag was specified.
  6817. */
  6818. if (precision > 0 || flags & PRINT_F_NUM)
  6819. emitpoint = 1;
  6820. if (separators) /* Get the number of group separators we'll print. */
  6821. separators = getnumsep(ipos);
  6822. padlen = width /* Minimum field width. */
  6823. - ipos /* Number of integer digits. */
  6824. - epos /* Number of exponent characters. */
  6825. - precision /* Number of fractional digits. */
  6826. - separators /* Number of group separators. */
  6827. - (emitpoint ? 1 : 0) /* Will we print a decimal point? */
  6828. - ((sign != 0) ? 1 : 0); /* Will we print a sign character? */
  6829. if (padlen < 0)
  6830. padlen = 0;
  6831. /*
  6832. * C99 says: "If the `0' and `-' flags both appear, the `0' flag is
  6833. * ignored." (7.19.6.1, 6)
  6834. */
  6835. if (flags & PRINT_F_MINUS) /* Left justifty. */
  6836. padlen = -padlen;
  6837. else if (flags & PRINT_F_ZERO && padlen > 0) {
  6838. if (sign != 0) { /* Sign. */
  6839. OUTCHAR(str, *len, size, sign);
  6840. sign = 0;
  6841. }
  6842. while (padlen > 0) { /* Leading zeros. */
  6843. OUTCHAR(str, *len, size, '0');
  6844. padlen--;
  6845. }
  6846. }
  6847. while (padlen > 0) { /* Leading spaces. */
  6848. OUTCHAR(str, *len, size, ' ');
  6849. padlen--;
  6850. }
  6851. if (sign != 0) /* Sign. */
  6852. OUTCHAR(str, *len, size, sign);
  6853. while (ipos > 0) { /* Integer part. */
  6854. ipos--;
  6855. OUTCHAR(str, *len, size, iconvert[ipos]);
  6856. if (separators > 0 && ipos > 0 && ipos % 3 == 0)
  6857. printsep(str, len, size);
  6858. }
  6859. if (emitpoint) { /* Decimal point. */
  6860. #if HAVE_LOCALECONV && HAVE_LCONV_DECIMAL_POINT
  6861. if (lc->decimal_point != NULL && *lc->decimal_point != '\0')
  6862. OUTCHAR(str, *len, size, *lc->decimal_point);
  6863. else /* We'll always print some decimal point character. */
  6864. #endif /* HAVE_LOCALECONV && HAVE_LCONV_DECIMAL_POINT */
  6865. OUTCHAR(str, *len, size, '.');
  6866. }
  6867. while (leadfraczeros > 0) { /* Leading fractional part zeros. */
  6868. OUTCHAR(str, *len, size, '0');
  6869. leadfraczeros--;
  6870. }
  6871. while (fpos > omitcount) { /* The remaining fractional part. */
  6872. fpos--;
  6873. OUTCHAR(str, *len, size, fconvert[fpos]);
  6874. }
  6875. while (epos > 0) { /* Exponent. */
  6876. epos--;
  6877. OUTCHAR(str, *len, size, econvert[epos]);
  6878. }
  6879. while (padlen < 0) { /* Trailing spaces. */
  6880. OUTCHAR(str, *len, size, ' ');
  6881. padlen++;
  6882. }
  6883. }
  6884. static void
  6885. printsep(char *str, size_t *len, size_t size)
  6886. {
  6887. #if HAVE_LOCALECONV && HAVE_LCONV_THOUSANDS_SEP
  6888. struct lconv *lc = localeconv();
  6889. int i;
  6890. if (lc->thousands_sep != NULL)
  6891. for (i = 0; lc->thousands_sep[i] != '\0'; i++)
  6892. OUTCHAR(str, *len, size, lc->thousands_sep[i]);
  6893. else
  6894. #endif /* HAVE_LOCALECONV && HAVE_LCONV_THOUSANDS_SEP */
  6895. OUTCHAR(str, *len, size, ',');
  6896. }
  6897. static int
  6898. getnumsep(int digits)
  6899. {
  6900. int separators = (digits - ((digits % 3 == 0) ? 1 : 0)) / 3;
  6901. #if HAVE_LOCALECONV && HAVE_LCONV_THOUSANDS_SEP
  6902. int strln;
  6903. struct lconv *lc = localeconv();
  6904. /* We support an arbitrary separator length (including zero). */
  6905. if (lc->thousands_sep != NULL) {
  6906. for (strln = 0; lc->thousands_sep[strln] != '\0'; strln++)
  6907. continue;
  6908. separators *= strln;
  6909. }
  6910. #endif /* HAVE_LOCALECONV && HAVE_LCONV_THOUSANDS_SEP */
  6911. return separators;
  6912. }
  6913. static int
  6914. getexponent(LDOUBLE value)
  6915. {
  6916. LDOUBLE tmp = (value >= 0.0) ? value : -value;
  6917. int exponent = 0;
  6918. /*
  6919. * We check for 99 > exponent > -99 in order to work around possible
  6920. * endless loops which could happen (at least) in the second loop (at
  6921. * least) if we're called with an infinite value. However, we checked
  6922. * for infinity before calling this function using our ISINF() macro, so
  6923. * this might be somewhat paranoid.
  6924. */
  6925. while (tmp < 1.0 && tmp > 0.0 && --exponent > -99)
  6926. tmp *= 10;
  6927. while (tmp >= 10.0 && ++exponent < 99)
  6928. tmp /= 10;
  6929. return exponent;
  6930. }
  6931. static int
  6932. convert(UINTMAX_T value, char *buf, size_t size, int base, int caps)
  6933. {
  6934. const char *digits = caps ? "0123456789ABCDEF" : "0123456789abcdef";
  6935. size_t pos = 0;
  6936. /* We return an unterminated buffer with the digits in reverse order. */
  6937. do {
  6938. buf[pos++] = digits[value % base];
  6939. value /= base;
  6940. } while (value != 0 && pos < size);
  6941. return (int)pos;
  6942. }
  6943. static UINTMAX_T
  6944. cast(LDOUBLE value)
  6945. {
  6946. UINTMAX_T result;
  6947. /*
  6948. * We check for ">=" and not for ">" because if UINTMAX_MAX cannot be
  6949. * represented exactly as an LDOUBLE value (but is less than LDBL_MAX),
  6950. * it may be increased to the nearest higher representable value for the
  6951. * comparison (cf. C99: 6.3.1.4, 2). It might then equal the LDOUBLE
  6952. * value although converting the latter to UINTMAX_T would overflow.
  6953. */
  6954. if (value >= UINTMAX_MAX)
  6955. return UINTMAX_MAX;
  6956. result = value;
  6957. /*
  6958. * At least on NetBSD/sparc64 3.0.2 and 4.99.30, casting long double to
  6959. * an integer type converts e.g. 1.9 to 2 instead of 1 (which violates
  6960. * the standard). Sigh.
  6961. */
  6962. return (result <= value) ? result : result - 1;
  6963. }
  6964. static UINTMAX_T
  6965. myround(LDOUBLE value)
  6966. {
  6967. UINTMAX_T intpart = cast(value);
  6968. return ((value -= intpart) < 0.5) ? intpart : intpart + 1;
  6969. }
  6970. static LDOUBLE
  6971. mypow10(int exponent)
  6972. {
  6973. LDOUBLE result = 1;
  6974. while (exponent > 0) {
  6975. result *= 10;
  6976. exponent--;
  6977. }
  6978. while (exponent < 0) {
  6979. result /= 10;
  6980. exponent++;
  6981. }
  6982. return result;
  6983. }
  6984. #endif /* !HAVE_VSNPRINTF */
  6985. #if !HAVE_VASPRINTF
  6986. #if NEED_MYMEMCPY
  6987. static void *
  6988. mymemcpy(void *dst, void *src, size_t len)
  6989. {
  6990. const char *from = (const char *)src;
  6991. char *to = (char*)dst;
  6992. /* No need for optimization, we use this only to replace va_copy(3). */
  6993. while (len-- > 0)
  6994. *to++ = *from++;
  6995. return dst;
  6996. }
  6997. #endif /* NEED_MYMEMCPY */
  6998. int
  6999. rpl_vasprintf(char **ret, const char *format, va_list ap)
  7000. {
  7001. size_t size;
  7002. int len;
  7003. va_list aq;
  7004. VA_COPY(aq, ap);
  7005. #if !HAVE_VSNPRINTF
  7006. len = rpl_vsnprintf(NULL, 0, format, aq);
  7007. #else
  7008. len = vsnprintf(NULL, 0, format, aq);
  7009. #endif
  7010. VA_END_COPY(aq);
  7011. if (len < 0 || (*ret = (char*)malloc(size = len + 1)) == NULL)
  7012. return -1;
  7013. #if !HAVE_VSNPRINTF
  7014. return rpl_vsnprintf(*ret, size, format, ap);
  7015. #else
  7016. return vsnprintf(*ret, size, format, ap);
  7017. #endif
  7018. }
  7019. #endif /* !HAVE_VASPRINTF */
  7020. #if !HAVE_SNPRINTF
  7021. #if HAVE_STDARG_H
  7022. int
  7023. rpl_snprintf(char *str, size_t size, const char *format, ...)
  7024. #else
  7025. int
  7026. rpl_snprintf(va_alist) va_dcl
  7027. #endif /* HAVE_STDARG_H */
  7028. {
  7029. #if !HAVE_STDARG_H
  7030. char *str;
  7031. size_t size;
  7032. char *format;
  7033. #endif /* HAVE_STDARG_H */
  7034. va_list ap;
  7035. int len;
  7036. VA_START(ap, format);
  7037. VA_SHIFT(ap, str, char *);
  7038. VA_SHIFT(ap, size, size_t);
  7039. VA_SHIFT(ap, format, const char *);
  7040. #if !HAVE_VSNPRINTF
  7041. len = rpl_vsnprintf(str, size, format, ap);
  7042. #else
  7043. len = vsnprintf(str, size, format, ap);
  7044. #endif
  7045. va_end(ap);
  7046. return len;
  7047. }
  7048. #endif /* !HAVE_SNPRINTF */
  7049. #if !HAVE_ASPRINTF
  7050. #if HAVE_STDARG_H
  7051. int
  7052. rpl_asprintf(char **ret, const char *format, ...)
  7053. #else
  7054. int
  7055. rpl_asprintf(va_alist) va_dcl
  7056. #endif /* HAVE_STDARG_H */
  7057. {
  7058. #if !HAVE_STDARG_H
  7059. char **ret;
  7060. char *format;
  7061. #endif /* HAVE_STDARG_H */
  7062. va_list ap;
  7063. int len;
  7064. VA_START(ap, format);
  7065. VA_SHIFT(ap, ret, char **);
  7066. VA_SHIFT(ap, format, const char *);
  7067. #if !HAVE_VASPRINTF
  7068. len = rpl_vasprintf(ret, format, ap);
  7069. #else
  7070. len = vasprintf(ret, format, ap);
  7071. #endif
  7072. va_end(ap);
  7073. return len;
  7074. }
  7075. #endif /* !HAVE_ASPRINTF */
  7076. #endif /* !HAVE_SNPRINTF || !HAVE_VSNPRINTF || !HAVE_ASPRINTF || [...] */
  7077. static char* mat_strdup(const char *s)
  7078. {
  7079. size_t len = strlen(s) + 1;
  7080. char *d = (char*)malloc(len);
  7081. return d ? (char*)memcpy(d, s, len) : NULL;
  7082. }
  7083. /* -------------------------------
  7084. * ---------- mat.c
  7085. * -------------------------------
  7086. */
  7087. /** @file mat.c
  7088. * Matlab MAT version 5 file functions
  7089. * @ingroup MAT
  7090. */
  7091. /* FIXME: Implement Unicode support */
  7092. #include <stdlib.h>
  7093. #include <string.h>
  7094. #include <stdio.h>
  7095. #include <math.h>
  7096. #include <time.h>
  7097. #ifndef MAT5_H
  7098. #define MAT5_H
  7099. static size_t GetStructFieldBufSize(matvar_t *matvar);
  7100. static size_t GetCellArrayFieldBufSize(matvar_t *matvar);
  7101. #if defined(HAVE_ZLIB)
  7102. static size_t GetMatrixMaxBufSize(matvar_t *matvar);
  7103. static size_t GetEmptyMatrixMaxBufSize(const char *name,int rank);
  7104. #endif
  7105. static size_t WriteCharData(mat_t *mat, void *data, int N,enum matio_types data_type);
  7106. static size_t WriteEmptyCharData(mat_t *mat, int N, enum matio_types data_type);
  7107. static size_t WriteEmptyData(mat_t *mat,int N,enum matio_types data_type);
  7108. static size_t ReadNextCell( mat_t *mat, matvar_t *matvar );
  7109. static size_t ReadNextStructField( mat_t *mat, matvar_t *matvar );
  7110. static size_t ReadNextFunctionHandle(mat_t *mat, matvar_t *matvar);
  7111. static int WriteCellArrayFieldInfo(mat_t *mat,matvar_t *matvar);
  7112. static int WriteCellArrayField(mat_t *mat,matvar_t *matvar );
  7113. static int WriteStructField(mat_t *mat,matvar_t *matvar);
  7114. static size_t Mat_WriteEmptyVariable5(mat_t *mat,const char *name,int rank,
  7115. size_t *dims);
  7116. #if defined(HAVE_ZLIB)
  7117. static size_t WriteCompressedCharData(mat_t *mat,z_streamp z,void *data,int N,
  7118. enum matio_types data_type);
  7119. #if 0
  7120. static size_t WriteCompressedEmptyData(mat_t *mat,z_streamp z,int N,
  7121. enum matio_types data_type);
  7122. #endif
  7123. static size_t WriteCompressedData(mat_t *mat,z_streamp z,void *data,int N,
  7124. enum matio_types data_type);
  7125. static size_t WriteCompressedCellArrayField(mat_t *mat,matvar_t *matvar,
  7126. z_streamp z);
  7127. static size_t WriteCompressedStructField(mat_t *mat,matvar_t *matvar,
  7128. z_streamp z);
  7129. static size_t Mat_WriteCompressedEmptyVariable5(mat_t *mat,const char *name,
  7130. int rank,size_t *dims,z_streamp z);
  7131. #endif
  7132. /* mat5.c */
  7133. static mat_t *Mat_Create5(const char *matname,const char *hdr_str);
  7134. static matvar_t *Mat_VarReadNextInfo5( mat_t *mat );
  7135. static void Read5(mat_t *mat, matvar_t *matvar);
  7136. static int ReadData5(mat_t *mat,matvar_t *matvar,void *data,
  7137. int *start,int *stride,int *edge);
  7138. static int Mat_VarReadDataLinear5(mat_t *mat,matvar_t *matvar,void *data,
  7139. int start,int stride,int edge);
  7140. static int Mat_VarWrite5(mat_t *mat,matvar_t *matvar,int compress);
  7141. static int WriteCharDataSlab2(mat_t *mat,void *data,enum matio_types data_type,
  7142. size_t *dims,int *start,int *stride,int *edge);
  7143. static int WriteData(mat_t *mat,void *data,int N,enum matio_types data_type);
  7144. static int WriteDataSlab2(mat_t *mat,void *data,enum matio_types data_type,
  7145. size_t *dims,int *start,int *stride,int *edge);
  7146. static void WriteInfo5(mat_t *mat, matvar_t *matvar);
  7147. #endif
  7148. #ifndef MAT4_H
  7149. #define MAT4_H
  7150. static mat_t *Mat_Create4(const char* matname);
  7151. static int Mat_VarWrite4(mat_t *mat,matvar_t *matvar);
  7152. static void Read4(mat_t *mat, matvar_t *matvar);
  7153. static int ReadData4(mat_t *mat,matvar_t *matvar,void *data,
  7154. int *start,int *stride,int *edge);
  7155. static int Mat_VarReadDataLinear4(mat_t *mat,matvar_t *matvar,void *data,
  7156. int start,int stride,int edge);
  7157. static matvar_t *Mat_VarReadNextInfo4(mat_t *mat);
  7158. #endif
  7159. #if defined(HAVE_HDF5)
  7160. #ifndef MAT73_H
  7161. #define MAT73_H
  7162. static mat_t *Mat_Create73(const char *matname,const char *hdr_str);
  7163. static void Mat_VarRead73(mat_t *mat,matvar_t *matvar);
  7164. static int Mat_VarReadData73(mat_t *mat,matvar_t *matvar,void *data,
  7165. int *start,int *stride,int *edge);
  7166. static int Mat_VarReadDataLinear73(mat_t *mat,matvar_t *matvar,void *data,
  7167. int start,int stride,int edge);
  7168. static matvar_t *Mat_VarReadNextInfo73(mat_t *mat);
  7169. static int Mat_VarWrite73(mat_t *mat,matvar_t *matvar,int compress);
  7170. #endif
  7171. #endif
  7172. #if HAVE_INTTYPES_H
  7173. # include <inttypes.h>
  7174. #endif
  7175. #if defined(_WIN32)
  7176. # include <io.h>
  7177. # define mktemp _mktemp
  7178. #endif
  7179. #if defined(_MSC_VER) || defined(__MINGW32__)
  7180. # define SIZE_T_FMTSTR "Iu"
  7181. #elif defined(__GNUC__) && __STDC_VERSION__ < 199901L
  7182. # define SIZE_T_FMTSTR "lu"
  7183. #else
  7184. # define SIZE_T_FMTSTR "zu"
  7185. #endif
  7186. static void
  7187. ReadData(mat_t *mat, matvar_t *matvar)
  7188. {
  7189. if ( mat == NULL || matvar == NULL || mat->fp == NULL )
  7190. return;
  7191. else if ( mat->version == MAT_FT_MAT5 )
  7192. Read5(mat,matvar);
  7193. #if defined(HAVE_HDF5)
  7194. else if ( mat->version == MAT_FT_MAT73 )
  7195. Mat_VarRead73(mat,matvar);
  7196. #endif
  7197. else if ( mat->version == MAT_FT_MAT4 )
  7198. Read4(mat,matvar);
  7199. return;
  7200. }
  7201. static void
  7202. Mat_PrintNumber(enum matio_types type, void *data)
  7203. {
  7204. switch ( type ) {
  7205. case MAT_T_DOUBLE:
  7206. printf("%g",*(double*)data);
  7207. break;
  7208. case MAT_T_SINGLE:
  7209. printf("%g",*(float*)data);
  7210. break;
  7211. #ifdef HAVE_MATIO_INT64_T
  7212. case MAT_T_INT64:
  7213. #if HAVE_INTTYPES_H
  7214. printf("%" PRIi64,*(mat_int64_t*)data);
  7215. #elif defined(_MSC_VER) && _MSC_VER >= 1200
  7216. printf("%I64i",*(mat_int64_t*)data);
  7217. #elif defined(HAVE_LONG_LONG_INT)
  7218. printf("%lld",(long long)(*(mat_int64_t*)data));
  7219. #else
  7220. printf("%ld",(long)(*(mat_int64_t*)data));
  7221. #endif
  7222. break;
  7223. #endif
  7224. #ifdef HAVE_MATIO_UINT64_T
  7225. case MAT_T_UINT64:
  7226. #if HAVE_INTTYPES_H
  7227. printf("%" PRIu64,*(mat_uint64_t*)data);
  7228. #elif defined(_MSC_VER) && _MSC_VER >= 1200
  7229. printf("%I64u",*(mat_uint64_t*)data);
  7230. #elif defined(HAVE_UNSIGNED_LONG_LONG_INT)
  7231. printf("%llu",(unsigned long long)(*(mat_uint64_t*)data));
  7232. #else
  7233. printf("%lu",(unsigned long)(*(mat_uint64_t*)data));
  7234. #endif
  7235. break;
  7236. #endif
  7237. case MAT_T_INT32:
  7238. printf("%d",*(mat_int32_t*)data);
  7239. break;
  7240. case MAT_T_UINT32:
  7241. printf("%u",*(mat_uint32_t*)data);
  7242. break;
  7243. case MAT_T_INT16:
  7244. printf("%hd",*(mat_int16_t*)data);
  7245. break;
  7246. case MAT_T_UINT16:
  7247. printf("%hu",*(mat_uint16_t*)data);
  7248. break;
  7249. case MAT_T_INT8:
  7250. #if defined(__GNUC__) && __STDC_VERSION__ >= 199901L
  7251. printf("%hhd",*(mat_int8_t*)data);
  7252. #else
  7253. printf("%hd",(mat_int16_t)(*(mat_int8_t*)data));
  7254. #endif
  7255. break;
  7256. case MAT_T_UINT8:
  7257. #if defined(__GNUC__) && __STDC_VERSION__ >= 199901L
  7258. printf("%hhu",*(mat_uint8_t*)data);
  7259. #else
  7260. printf("%hu",(mat_uint16_t)(*(mat_uint8_t*)data));
  7261. #endif
  7262. break;
  7263. default:
  7264. break;
  7265. }
  7266. }
  7267. /*
  7268. *====================================================================
  7269. * Public Functions
  7270. *====================================================================
  7271. */
  7272. /** @brief Get the version of the library
  7273. *
  7274. * Gets the version number of the library
  7275. * @param major Pointer to store the library major version number
  7276. * @param minor Pointer to store the library minor version number
  7277. * @param release Pointer to store the library release version number
  7278. */
  7279. void
  7280. Mat_GetLibraryVersion(int *major,int *minor,int *release)
  7281. {
  7282. if ( NULL != major )
  7283. *major = MATIO_MAJOR_VERSION;
  7284. if ( NULL != minor )
  7285. *minor = MATIO_MINOR_VERSION;
  7286. if ( NULL != release )
  7287. *release = MATIO_RELEASE_LEVEL;
  7288. }
  7289. /** @brief Creates a new Matlab MAT file
  7290. *
  7291. * Tries to create a new Matlab MAT file with the given name and optional
  7292. * header string. If no header string is given, the default string
  7293. * is used containing the software, version, and date in it. If a header
  7294. * string is given, at most the first 116 characters is written to the file.
  7295. * The given header string need not be the full 116 characters, but MUST be
  7296. * NULL terminated.
  7297. * @ingroup MAT
  7298. * @param matname Name of MAT file to create
  7299. * @param hdr_str Optional header string, NULL to use default
  7300. * @param mat_file_ver MAT file version to create
  7301. * @return A pointer to the MAT file or NULL if it failed. This is not a
  7302. * simple FILE * and should not be used as one.
  7303. */
  7304. mat_t *
  7305. Mat_CreateVer(const char *matname,const char *hdr_str,enum mat_ft mat_file_ver)
  7306. {
  7307. mat_t *mat = NULL;
  7308. switch ( mat_file_ver ) {
  7309. case MAT_FT_MAT4:
  7310. mat = Mat_Create4(matname);
  7311. break;
  7312. case MAT_FT_MAT5:
  7313. mat = Mat_Create5(matname,hdr_str);
  7314. break;
  7315. case MAT_FT_MAT73:
  7316. #if defined(HAVE_HDF5)
  7317. mat = Mat_Create73(matname,hdr_str);
  7318. #endif
  7319. break;
  7320. default:
  7321. break;
  7322. }
  7323. return mat;
  7324. }
  7325. /** @brief Opens an existing Matlab MAT file
  7326. *
  7327. * Tries to open a Matlab MAT file with the given name
  7328. * @ingroup MAT
  7329. * @param matname Name of MAT file to open
  7330. * @param mode File access mode (MAT_ACC_RDONLY,MAT_ACC_RDWR,etc).
  7331. * @return A pointer to the MAT file or NULL if it failed. This is not a
  7332. * simple FILE * and should not be used as one.
  7333. */
  7334. mat_t *
  7335. Mat_Open(const char *matname,int mode)
  7336. {
  7337. FILE *fp = NULL;
  7338. mat_int16_t tmp, tmp2;
  7339. mat_t *mat = NULL;
  7340. size_t bytesread = 0;
  7341. if ( (mode & 0x01) == MAT_ACC_RDONLY ) {
  7342. fp = fopen( matname, "rb" );
  7343. if ( !fp )
  7344. return NULL;
  7345. } else if ( (mode & 0x01) == MAT_ACC_RDWR ) {
  7346. fp = fopen( matname, "r+b" );
  7347. if ( !fp ) {
  7348. mat = Mat_CreateVer(matname,NULL,(enum mat_ft)(mode&0xfffffffe));
  7349. return mat;
  7350. }
  7351. } else {
  7352. Mat_Critical("Invalid file open mode");
  7353. return NULL;
  7354. }
  7355. mat = (mat_t*)malloc(sizeof(*mat));
  7356. if ( NULL == mat ) {
  7357. fclose(fp);
  7358. Mat_Critical("Couldn't allocate memory for the MAT file");
  7359. return NULL;
  7360. }
  7361. mat->fp = fp;
  7362. mat->header = (char*)calloc(128,sizeof(char));
  7363. if ( NULL == mat->header ) {
  7364. free(mat);
  7365. fclose(fp);
  7366. Mat_Critical("Couldn't allocate memory for the MAT file header");
  7367. return NULL;
  7368. }
  7369. mat->subsys_offset = (char*)calloc(8,sizeof(char));
  7370. if ( NULL == mat->subsys_offset ) {
  7371. free(mat->header);
  7372. free(mat);
  7373. fclose(fp);
  7374. Mat_Critical("Couldn't allocate memory for the MAT file subsys offset");
  7375. return NULL;
  7376. }
  7377. mat->filename = NULL;
  7378. mat->byteswap = 0;
  7379. mat->version = 0;
  7380. mat->refs_id = -1;
  7381. bytesread += fread(mat->header,1,116,fp);
  7382. mat->header[116] = '\0';
  7383. bytesread += fread(mat->subsys_offset,1,8,fp);
  7384. bytesread += 2*fread(&tmp2,2,1,fp);
  7385. bytesread += fread(&tmp,1,2,fp);
  7386. if ( 128 == bytesread ) {
  7387. /* v5 and v7.3 files have at least 128 byte header */
  7388. mat->byteswap = -1;
  7389. if (tmp == 0x4d49)
  7390. mat->byteswap = 0;
  7391. else if (tmp == 0x494d) {
  7392. mat->byteswap = 1;
  7393. Mat_int16Swap(&tmp2);
  7394. }
  7395. mat->version = (int)tmp2;
  7396. if ( (mat->version == 0x0100 || mat->version == 0x0200) &&
  7397. -1 != mat->byteswap ) {
  7398. mat->bof = ftell((FILE*)mat->fp);
  7399. if ( mat->bof == -1L ) {
  7400. free(mat->header);
  7401. free(mat->subsys_offset);
  7402. free(mat);
  7403. fclose(fp);
  7404. Mat_Critical("Couldn't determine file position");
  7405. return NULL;
  7406. }
  7407. mat->next_index = 0;
  7408. } else {
  7409. mat->version = 0;
  7410. }
  7411. }
  7412. if ( 0 == mat->version ) {
  7413. /* Maybe a V4 MAT file */
  7414. matvar_t *var;
  7415. free(mat->header);
  7416. free(mat->subsys_offset);
  7417. mat->header = NULL;
  7418. mat->subsys_offset = NULL;
  7419. mat->fp = fp;
  7420. mat->version = MAT_FT_MAT4;
  7421. mat->byteswap = 0;
  7422. mat->mode = mode;
  7423. mat->bof = 0;
  7424. mat->next_index = 0;
  7425. mat->refs_id = -1;
  7426. Mat_Rewind(mat);
  7427. var = Mat_VarReadNextInfo4(mat);
  7428. if ( NULL == var &&
  7429. bytesread != 0 ) { /* Accept 0 bytes files as a valid V4 file */
  7430. /* Does not seem to be a valid V4 file */
  7431. Mat_Close(mat);
  7432. mat = NULL;
  7433. Mat_Critical("\"%s\" does not seem to be a valid MAT file",matname);
  7434. } else {
  7435. Mat_VarFree(var);
  7436. Mat_Rewind(mat);
  7437. }
  7438. }
  7439. if ( NULL == mat )
  7440. return mat;
  7441. mat->filename = strdup_printf("%s",matname);
  7442. mat->mode = mode;
  7443. if ( mat->version == 0x0200 ) {
  7444. fclose((FILE*)mat->fp);
  7445. #if defined(HAVE_HDF5)
  7446. mat->fp = malloc(sizeof(hid_t));
  7447. if ( (mode & 0x01) == MAT_ACC_RDONLY )
  7448. *(hid_t*)mat->fp=H5Fopen(mat->filename,H5F_ACC_RDONLY,H5P_DEFAULT);
  7449. else if ( (mode & 0x01) == MAT_ACC_RDWR )
  7450. *(hid_t*)mat->fp=H5Fopen(mat->filename,H5F_ACC_RDWR,H5P_DEFAULT);
  7451. if ( -1 < *(hid_t*)mat->fp ) {
  7452. H5G_info_t group_info;
  7453. H5Gget_info(*(hid_t*)mat->fp, &group_info);
  7454. mat->num_datasets = group_info.nlinks;
  7455. mat->refs_id = -1;
  7456. }
  7457. #else
  7458. mat->fp = NULL;
  7459. Mat_Close(mat);
  7460. mat = NULL;
  7461. Mat_Critical("No HDF5 support which is required to read the v7.3 "
  7462. "MAT file \"%s\"",matname);
  7463. #endif
  7464. }
  7465. return mat;
  7466. }
  7467. /** @brief Closes an open Matlab MAT file
  7468. *
  7469. * Closes the given Matlab MAT file and frees any memory with it.
  7470. * @ingroup MAT
  7471. * @param mat Pointer to the MAT file
  7472. * @retval 0
  7473. */
  7474. int
  7475. Mat_Close( mat_t *mat )
  7476. {
  7477. if ( NULL != mat ) {
  7478. #if defined(HAVE_HDF5)
  7479. if ( mat->version == 0x0200 ) {
  7480. if ( mat->refs_id > -1 )
  7481. H5Gclose(mat->refs_id);
  7482. H5Fclose(*(hid_t*)mat->fp);
  7483. free(mat->fp);
  7484. mat->fp = NULL;
  7485. }
  7486. #endif
  7487. if ( mat->fp )
  7488. fclose((FILE*)mat->fp);
  7489. if ( mat->header )
  7490. free(mat->header);
  7491. if ( mat->subsys_offset )
  7492. free(mat->subsys_offset);
  7493. if ( mat->filename )
  7494. free(mat->filename);
  7495. free(mat);
  7496. }
  7497. return 0;
  7498. }
  7499. /** @brief Gets the filename for the given MAT file
  7500. *
  7501. * Gets the filename for the given MAT file
  7502. * @ingroup MAT
  7503. * @param matfp Pointer to the MAT file
  7504. * @return MAT filename
  7505. */
  7506. const char *
  7507. Mat_GetFilename(mat_t *matfp)
  7508. {
  7509. const char *filename = NULL;
  7510. if ( NULL != matfp )
  7511. filename = matfp->filename;
  7512. return filename;
  7513. }
  7514. /** @brief Gets the version of the given MAT file
  7515. *
  7516. * Gets the version of the given MAT file
  7517. * @ingroup MAT
  7518. * @param matfp Pointer to the MAT file
  7519. * @return MAT file version
  7520. */
  7521. enum mat_ft
  7522. Mat_GetVersion(mat_t *matfp)
  7523. {
  7524. enum mat_ft file_type = MAT_FT_UNDEFINED;
  7525. if ( NULL != matfp )
  7526. file_type = (enum mat_ft)matfp->version;
  7527. return file_type;
  7528. }
  7529. /** @brief Rewinds a Matlab MAT file to the first variable
  7530. *
  7531. * Rewinds a Matlab MAT file to the first variable
  7532. * @ingroup MAT
  7533. * @param mat Pointer to the MAT file
  7534. * @retval 0 on success
  7535. */
  7536. int
  7537. Mat_Rewind( mat_t *mat )
  7538. {
  7539. int err = 0;
  7540. switch ( mat->version ) {
  7541. case MAT_FT_MAT73:
  7542. mat->next_index = 0;
  7543. break;
  7544. case MAT_FT_MAT5:
  7545. (void)fseek((FILE*)mat->fp,128L,SEEK_SET);
  7546. break;
  7547. case MAT_FT_MAT4:
  7548. (void)fseek((FILE*)mat->fp,0L,SEEK_SET);
  7549. break;
  7550. default:
  7551. err = -1;
  7552. break;
  7553. }
  7554. return err;
  7555. }
  7556. /** @brief Returns the size of a Matlab Class
  7557. *
  7558. * Returns the size (in bytes) of the matlab class class_type
  7559. * @ingroup MAT
  7560. * @param class_type Matlab class type (MAT_C_*)
  7561. * @returns Size of the class
  7562. */
  7563. size_t
  7564. Mat_SizeOfClass(int class_type)
  7565. {
  7566. switch (class_type) {
  7567. case MAT_C_DOUBLE:
  7568. return sizeof(double);
  7569. case MAT_C_SINGLE:
  7570. return sizeof(float);
  7571. #ifdef HAVE_MATIO_INT64_T
  7572. case MAT_C_INT64:
  7573. return sizeof(mat_int64_t);
  7574. #endif
  7575. #ifdef HAVE_MATIO_UINT64_T
  7576. case MAT_C_UINT64:
  7577. return sizeof(mat_uint64_t);
  7578. #endif
  7579. case MAT_C_INT32:
  7580. return sizeof(mat_int32_t);
  7581. case MAT_C_UINT32:
  7582. return sizeof(mat_uint32_t);
  7583. case MAT_C_INT16:
  7584. return sizeof(mat_int16_t);
  7585. case MAT_C_UINT16:
  7586. return sizeof(mat_uint16_t);
  7587. case MAT_C_INT8:
  7588. return sizeof(mat_int8_t);
  7589. case MAT_C_UINT8:
  7590. return sizeof(mat_uint8_t);
  7591. case MAT_C_CHAR:
  7592. return sizeof(mat_int16_t);
  7593. default:
  7594. return 0;
  7595. }
  7596. }
  7597. /*
  7598. *===================================================================
  7599. * MAT Variable Functions
  7600. *===================================================================
  7601. */
  7602. /** @brief Allocates memory for a new matvar_t and initializes all the fields
  7603. *
  7604. * @ingroup MAT
  7605. * @return A newly allocated matvar_t
  7606. */
  7607. matvar_t *
  7608. Mat_VarCalloc(void)
  7609. {
  7610. matvar_t *matvar;
  7611. matvar = (matvar_t*)malloc(sizeof(*matvar));
  7612. if ( NULL != matvar ) {
  7613. matvar->nbytes = 0;
  7614. matvar->rank = 0;
  7615. matvar->data_type = MAT_T_UNKNOWN;
  7616. matvar->data_size = 0;
  7617. matvar->class_type = MAT_C_EMPTY;
  7618. matvar->isComplex = 0;
  7619. matvar->isGlobal = 0;
  7620. matvar->isLogical = 0;
  7621. matvar->dims = NULL;
  7622. matvar->name = NULL;
  7623. matvar->data = NULL;
  7624. matvar->mem_conserve = 0;
  7625. matvar->compression = MAT_COMPRESSION_NONE;
  7626. matvar->internal = (struct matvar_internal*)malloc(sizeof(*matvar->internal));
  7627. if ( NULL == matvar->internal ) {
  7628. free(matvar);
  7629. matvar = NULL;
  7630. } else {
  7631. matvar->internal->hdf5_name = NULL;
  7632. matvar->internal->hdf5_ref = 0;
  7633. matvar->internal->id = -1;
  7634. matvar->internal->fp = NULL;
  7635. matvar->internal->fpos = 0;
  7636. matvar->internal->datapos = 0;
  7637. matvar->internal->fieldnames = NULL;
  7638. matvar->internal->num_fields = 0;
  7639. #if defined(HAVE_ZLIB)
  7640. matvar->internal->z = NULL;
  7641. matvar->internal->data = NULL;
  7642. #endif
  7643. }
  7644. }
  7645. return matvar;
  7646. }
  7647. /** @brief Creates a MAT Variable with the given name and (optionally) data
  7648. *
  7649. * Creates a MAT variable that can be written to a Matlab MAT file with the
  7650. * given name, data type, dimensions and data. Rank should always be 2 or more.
  7651. * i.e. Scalar values would have rank=2 and dims[2] = {1,1}. Data type is
  7652. * one of the MAT_T types. MAT adds MAT_T_STRUCT and MAT_T_CELL to create
  7653. * Structures and Cell Arrays respectively. For MAT_T_STRUCT, data should be a
  7654. * NULL terminated array of matvar_t * variables (i.e. for a 3x2 structure with
  7655. * 10 fields, there should be 61 matvar_t * variables where the last one is
  7656. * NULL). For cell arrays, the NULL termination isn't necessary. So to create
  7657. * a cell array of size 3x2, data would be the address of an array of 6
  7658. * matvar_t * variables.
  7659. *
  7660. * EXAMPLE:
  7661. * To create a struct of size 3x2 with 3 fields:
  7662. * @code
  7663. * int rank=2, dims[2] = {3,2}, nfields = 3;
  7664. * matvar_t **vars;
  7665. *
  7666. * vars = malloc((3*2*nfields+1)*sizeof(matvar_t *));
  7667. * vars[0] = Mat_VarCreate(...);
  7668. * :
  7669. * vars[3*2*nfields-1] = Mat_VarCreate(...);
  7670. * vars[3*2*nfields] = NULL;
  7671. * @endcode
  7672. *
  7673. * EXAMPLE:
  7674. * To create a cell array of size 3x2:
  7675. * @code
  7676. * int rank=2, dims[2] = {3,2};
  7677. * matvar_t **vars;
  7678. *
  7679. * vars = malloc(3*2*sizeof(matvar_t *));
  7680. * vars[0] = Mat_VarCreate(...);
  7681. * :
  7682. * vars[5] = Mat_VarCreate(...);
  7683. * @endcode
  7684. *
  7685. * @ingroup MAT
  7686. * @param name Name of the variable to create
  7687. * @param class_type class type of the variable in Matlab(one of the mx Classes)
  7688. * @param data_type data type of the variable (one of the MAT_T_ Types)
  7689. * @param rank Rank of the variable
  7690. * @param dims array of dimensions of the variable of size rank
  7691. * @param data pointer to the data
  7692. * @param opt 0, or bitwise or of the following options:
  7693. * - MAT_F_DONT_COPY_DATA to just use the pointer to the data and not copy the
  7694. * data itself. Note that the pointer should not be freed until you are
  7695. * done with the mat variable. The Mat_VarFree function will NOT free
  7696. * data that was created with MAT_F_DONT_COPY_DATA, so free it yourself.
  7697. * - MAT_F_COMPLEX to specify that the data is complex. The data variable
  7698. * should be a pointer to a mat_complex_split_t type.
  7699. * - MAT_F_GLOBAL to assign the variable as a global variable
  7700. * - MAT_F_LOGICAL to specify that it is a logical variable
  7701. * @return A MAT variable that can be written to a file or otherwise used
  7702. */
  7703. matvar_t *
  7704. Mat_VarCreate(const char *name,enum matio_classes class_type,
  7705. enum matio_types data_type,int rank,size_t *dims,void *data,int opt)
  7706. {
  7707. size_t i, nmemb = 1, nfields = 0, data_size;
  7708. matvar_t *matvar = NULL;
  7709. if (dims == NULL) return NULL;
  7710. matvar = Mat_VarCalloc();
  7711. if ( NULL == matvar )
  7712. return NULL;
  7713. matvar->compression = MAT_COMPRESSION_NONE;
  7714. matvar->isComplex = opt & MAT_F_COMPLEX;
  7715. matvar->isGlobal = opt & MAT_F_GLOBAL;
  7716. matvar->isLogical = opt & MAT_F_LOGICAL;
  7717. if ( name )
  7718. matvar->name = strdup_printf("%s",name);
  7719. matvar->rank = rank;
  7720. matvar->dims = (size_t*)malloc(matvar->rank*sizeof(*matvar->dims));
  7721. for ( i = 0; i < matvar->rank; i++ ) {
  7722. matvar->dims[i] = dims[i];
  7723. nmemb *= dims[i];
  7724. }
  7725. matvar->class_type = class_type;
  7726. matvar->data_type = data_type;
  7727. switch ( data_type ) {
  7728. case MAT_T_INT8:
  7729. data_size = 1;
  7730. break;
  7731. case MAT_T_UINT8:
  7732. data_size = 1;
  7733. break;
  7734. case MAT_T_INT16:
  7735. data_size = 2;
  7736. break;
  7737. case MAT_T_UINT16:
  7738. data_size = 2;
  7739. break;
  7740. case MAT_T_INT64:
  7741. data_size = 8;
  7742. break;
  7743. case MAT_T_UINT64:
  7744. data_size = 8;
  7745. break;
  7746. case MAT_T_INT32:
  7747. data_size = 4;
  7748. break;
  7749. case MAT_T_UINT32:
  7750. data_size = 4;
  7751. break;
  7752. case MAT_T_SINGLE:
  7753. data_size = sizeof(float);
  7754. break;
  7755. case MAT_T_DOUBLE:
  7756. data_size = sizeof(double);
  7757. break;
  7758. case MAT_T_UTF8:
  7759. data_size = 1;
  7760. break;
  7761. case MAT_T_UTF16:
  7762. data_size = 2;
  7763. break;
  7764. case MAT_T_UTF32:
  7765. data_size = 4;
  7766. break;
  7767. case MAT_T_CELL:
  7768. data_size = sizeof(matvar_t **);
  7769. break;
  7770. case MAT_T_STRUCT:
  7771. {
  7772. matvar_t **fields;
  7773. data_size = sizeof(matvar_t **);
  7774. if ( data != NULL ) {
  7775. fields = (matvar_t**)data;
  7776. nfields = 0;
  7777. while ( fields[nfields] != NULL )
  7778. nfields++;
  7779. if ( nmemb )
  7780. nfields = nfields / nmemb;
  7781. matvar->internal->num_fields = nfields;
  7782. if ( nfields ) {
  7783. matvar->internal->fieldnames =
  7784. (char**)calloc(nfields,sizeof(*matvar->internal->fieldnames));
  7785. for ( i = 0; i < nfields; i++ )
  7786. matvar->internal->fieldnames[i] = mat_strdup(fields[i]->name);
  7787. nmemb *= nfields;
  7788. }
  7789. }
  7790. break;
  7791. }
  7792. default:
  7793. Mat_VarFree(matvar);
  7794. Mat_Critical("Unrecognized data_type");
  7795. return NULL;
  7796. }
  7797. if ( matvar->class_type == MAT_C_SPARSE ) {
  7798. matvar->data_size = sizeof(mat_sparse_t);
  7799. matvar->nbytes = matvar->data_size;
  7800. } else {
  7801. matvar->data_size = data_size;
  7802. matvar->nbytes = nmemb*matvar->data_size;
  7803. }
  7804. if ( data == NULL ) {
  7805. if ( MAT_C_CELL == matvar->class_type && nmemb > 0 )
  7806. matvar->data = calloc(nmemb,sizeof(matvar_t*));
  7807. else
  7808. matvar->data = NULL;
  7809. } else if ( opt & MAT_F_DONT_COPY_DATA ) {
  7810. matvar->data = data;
  7811. matvar->mem_conserve = 1;
  7812. } else if ( MAT_C_SPARSE == matvar->class_type ) {
  7813. mat_sparse_t *sparse_data, *sparse_data_in;
  7814. sparse_data_in = (mat_sparse_t*)data;
  7815. sparse_data = (mat_sparse_t*)malloc(sizeof(mat_sparse_t));
  7816. if ( NULL != sparse_data ) {
  7817. sparse_data->nzmax = sparse_data_in->nzmax;
  7818. sparse_data->nir = sparse_data_in->nir;
  7819. sparse_data->njc = sparse_data_in->njc;
  7820. sparse_data->ndata = sparse_data_in->ndata;
  7821. sparse_data->ir = (int*)malloc(sparse_data->nir*sizeof(*sparse_data->ir));
  7822. if ( NULL != sparse_data->ir )
  7823. memcpy(sparse_data->ir,sparse_data_in->ir,
  7824. sparse_data->nir*sizeof(*sparse_data->ir));
  7825. sparse_data->jc = (int*)malloc(sparse_data->njc*sizeof(*sparse_data->jc));
  7826. if ( NULL != sparse_data->jc )
  7827. memcpy(sparse_data->jc,sparse_data_in->jc,
  7828. sparse_data->njc*sizeof(*sparse_data->jc));
  7829. if ( matvar->isComplex ) {
  7830. sparse_data->data = malloc(sizeof(mat_complex_split_t));
  7831. if ( NULL != sparse_data->data ) {
  7832. mat_complex_split_t *complex_data,*complex_data_in;
  7833. complex_data = (mat_complex_split_t*)sparse_data->data;
  7834. complex_data_in = (mat_complex_split_t*)sparse_data_in->data;
  7835. complex_data->Re = malloc(sparse_data->ndata*data_size);
  7836. complex_data->Im = malloc(sparse_data->ndata*data_size);
  7837. if ( NULL != complex_data->Re )
  7838. memcpy(complex_data->Re,complex_data_in->Re,
  7839. sparse_data->ndata*data_size);
  7840. if ( NULL != complex_data->Im )
  7841. memcpy(complex_data->Im,complex_data_in->Im,
  7842. sparse_data->ndata*data_size);
  7843. }
  7844. } else {
  7845. sparse_data->data = malloc(sparse_data->ndata*data_size);
  7846. if ( NULL != sparse_data->data )
  7847. memcpy(sparse_data->data,sparse_data_in->data,
  7848. sparse_data->ndata*data_size);
  7849. }
  7850. }
  7851. matvar->data = sparse_data;
  7852. } else {
  7853. if ( matvar->isComplex ) {
  7854. matvar->data = malloc(sizeof(mat_complex_split_t));
  7855. if ( NULL != matvar->data && matvar->nbytes > 0 ) {
  7856. mat_complex_split_t *complex_data = (mat_complex_split_t*)matvar->data;
  7857. mat_complex_split_t *complex_data_in = (mat_complex_split_t*)data;
  7858. complex_data->Re = malloc(matvar->nbytes);
  7859. complex_data->Im = malloc(matvar->nbytes);
  7860. if ( NULL != complex_data->Re )
  7861. memcpy(complex_data->Re,complex_data_in->Re,matvar->nbytes);
  7862. if ( NULL != complex_data->Im )
  7863. memcpy(complex_data->Im,complex_data_in->Im,matvar->nbytes);
  7864. }
  7865. } else if ( matvar->nbytes > 0 ) {
  7866. matvar->data = malloc(matvar->nbytes);
  7867. if ( NULL != matvar->data )
  7868. memcpy(matvar->data,data,matvar->nbytes);
  7869. }
  7870. matvar->mem_conserve = 0;
  7871. }
  7872. return matvar;
  7873. }
  7874. /** @brief Copies a file
  7875. *
  7876. * @param src source file path
  7877. * @param dst destination file path
  7878. * @retval 0 on success
  7879. */
  7880. static int
  7881. mat_copy(const char* src, const char* dst)
  7882. {
  7883. size_t len;
  7884. char buf[BUFSIZ] = {'\0'};
  7885. FILE* in;
  7886. FILE* out;
  7887. in = fopen(src, "rb");
  7888. if (in == NULL) {
  7889. Mat_Critical("Cannot open file \"%s\" for reading.", src);
  7890. return -1;
  7891. }
  7892. out = fopen(dst, "wb");
  7893. if (out == NULL) {
  7894. fclose(in);
  7895. Mat_Critical("Cannot open file \"%s\" for writing.", dst);
  7896. return -1;
  7897. }
  7898. while ((len = fread(buf, sizeof(char), BUFSIZ, in)) > 0) {
  7899. if (len != fwrite(buf, sizeof(char), len, out)) {
  7900. fclose(in);
  7901. fclose(out);
  7902. Mat_Critical("Error writing to file \"%s\".", dst);
  7903. return -1;
  7904. }
  7905. }
  7906. fclose(in);
  7907. fclose(out);
  7908. return 0;
  7909. }
  7910. /** @brief Deletes a variable from a file
  7911. *
  7912. * @ingroup MAT
  7913. * @param mat Pointer to the mat_t file structure
  7914. * @param name Name of the variable to delete
  7915. * @returns 0 on success
  7916. */
  7917. int
  7918. Mat_VarDelete(mat_t *mat, const char *name)
  7919. {
  7920. int err = 1;
  7921. char *tmp_name;
  7922. char temp[7] = "XXXXXX";
  7923. if ( NULL == mat || NULL == name )
  7924. return err;
  7925. if ( (tmp_name = mktemp(temp)) != NULL ) {
  7926. enum mat_ft mat_file_ver = MAT_FT_DEFAULT;
  7927. mat_t *tmp;
  7928. switch ( mat->version ) {
  7929. case 0x0200:
  7930. mat_file_ver = MAT_FT_MAT73;
  7931. break;
  7932. case 0x0100:
  7933. mat_file_ver = MAT_FT_MAT5;
  7934. break;
  7935. case 0x0010:
  7936. mat_file_ver = MAT_FT_MAT4;
  7937. break;
  7938. }
  7939. tmp = Mat_CreateVer(tmp_name,mat->header,mat_file_ver);
  7940. if ( tmp != NULL ) {
  7941. matvar_t *matvar;
  7942. while ( NULL != (matvar = Mat_VarReadNext(mat)) ) {
  7943. if ( strcmp(matvar->name,name) )
  7944. Mat_VarWrite(tmp,matvar,matvar->compression);
  7945. else
  7946. err = 0;
  7947. Mat_VarFree(matvar);
  7948. }
  7949. Mat_Close(tmp);
  7950. if (err == 0) {
  7951. char *new_name = strdup_printf("%s",mat->filename);
  7952. #if defined(HAVE_HDF5)
  7953. if ( mat_file_ver == MAT_FT_MAT73 ) {
  7954. if ( mat->refs_id > -1 )
  7955. H5Gclose(mat->refs_id);
  7956. H5Fclose(*(hid_t*)mat->fp);
  7957. free(mat->fp);
  7958. mat->fp = NULL;
  7959. }
  7960. #endif
  7961. if ( mat->fp ) {
  7962. fclose((FILE*)mat->fp);
  7963. mat->fp = NULL;
  7964. }
  7965. if ( (err = mat_copy(tmp_name,new_name)) == -1 ) {
  7966. Mat_Critical("Cannot copy file from \"%s\" to \"%s\".",
  7967. tmp_name, new_name);
  7968. } else if ( (err = remove(tmp_name)) == -1 ) {
  7969. Mat_Critical("Cannot remove file \"%s\".",tmp_name);
  7970. } else {
  7971. tmp = Mat_Open(new_name,mat->mode);
  7972. if ( NULL != tmp ) {
  7973. if ( mat->header )
  7974. free(mat->header);
  7975. if ( mat->subsys_offset )
  7976. free(mat->subsys_offset);
  7977. if ( mat->filename )
  7978. free(mat->filename);
  7979. memcpy(mat,tmp,sizeof(mat_t));
  7980. free(tmp);
  7981. } else {
  7982. Mat_Critical("Cannot open file \"%s\".",new_name);
  7983. }
  7984. }
  7985. free(new_name);
  7986. } else if ( (err = remove(tmp_name)) == -1 ) {
  7987. Mat_Critical("Cannot remove file \"%s\".",tmp_name);
  7988. }
  7989. }
  7990. } else {
  7991. Mat_Critical("Cannot create a unique file name.");
  7992. }
  7993. return err;
  7994. }
  7995. /** @brief Duplicates a matvar_t structure
  7996. *
  7997. * Provides a clean function for duplicating a matvar_t structure.
  7998. * @ingroup MAT
  7999. * @param in pointer to the matvar_t structure to be duplicated
  8000. * @param opt 0 does a shallow duplicate and only assigns the data pointer to
  8001. * the duplicated array. 1 will do a deep duplicate and actually
  8002. * duplicate the contents of the data. Warning: If you do a shallow
  8003. * copy and free both structures, the data will be freed twice and
  8004. * memory will be corrupted. This may be fixed in a later release.
  8005. * @returns Pointer to the duplicated matvar_t structure.
  8006. */
  8007. matvar_t *
  8008. Mat_VarDuplicate(const matvar_t *in, int opt)
  8009. {
  8010. matvar_t *out;
  8011. int i;
  8012. out = Mat_VarCalloc();
  8013. if ( out == NULL )
  8014. return NULL;
  8015. out->nbytes = in->nbytes;
  8016. out->rank = in->rank;
  8017. out->data_type = in->data_type;
  8018. out->data_size = in->data_size;
  8019. out->class_type = in->class_type;
  8020. out->isComplex = in->isComplex;
  8021. out->isGlobal = in->isGlobal;
  8022. out->isLogical = in->isLogical;
  8023. out->mem_conserve = in->mem_conserve;
  8024. out->compression = in->compression;
  8025. out->name = NULL;
  8026. out->dims = NULL;
  8027. out->data = NULL;
  8028. if ( NULL != in->internal->hdf5_name )
  8029. out->internal->hdf5_name = mat_strdup(in->internal->hdf5_name);
  8030. out->internal->hdf5_ref = in->internal->hdf5_ref;
  8031. out->internal->id = in->internal->id;
  8032. out->internal->fpos = in->internal->fpos;
  8033. out->internal->datapos = in->internal->datapos;
  8034. #if defined(HAVE_ZLIB)
  8035. out->internal->z = NULL;
  8036. out->internal->data = NULL;
  8037. #endif
  8038. out->internal->num_fields = in->internal->num_fields;
  8039. if ( NULL != in->internal->fieldnames && in->internal->num_fields > 0 ) {
  8040. out->internal->fieldnames = (char**)calloc(in->internal->num_fields,
  8041. sizeof(*in->internal->fieldnames));
  8042. for ( i = 0; i < in->internal->num_fields; i++ ) {
  8043. if ( NULL != in->internal->fieldnames[i] )
  8044. out->internal->fieldnames[i] =
  8045. mat_strdup(in->internal->fieldnames[i]);
  8046. }
  8047. }
  8048. if (in->name != NULL && (NULL != (out->name = (char*)malloc(strlen(in->name)+1))))
  8049. memcpy(out->name,in->name,strlen(in->name)+1);
  8050. out->dims = (size_t*)malloc(in->rank*sizeof(*out->dims));
  8051. if ( out->dims != NULL )
  8052. memcpy(out->dims,in->dims,in->rank*sizeof(*out->dims));
  8053. #if defined(HAVE_ZLIB)
  8054. if ( (in->internal->z != NULL) && (NULL != (out->internal->z = (z_streamp)malloc(sizeof(z_stream)))) )
  8055. inflateCopy(out->internal->z,in->internal->z);
  8056. if ( in->internal->data != NULL ) {
  8057. if ( in->class_type == MAT_C_SPARSE ) {
  8058. out->internal->data = malloc(sizeof(mat_sparse_t));
  8059. if ( out->internal->data != NULL ) {
  8060. mat_sparse_t *out_sparse = (mat_sparse_t*)out->internal->data;
  8061. mat_sparse_t *in_sparse = (mat_sparse_t*)in->internal->data;
  8062. out_sparse->nzmax = in_sparse->nzmax;
  8063. out_sparse->nir = in_sparse->nir;
  8064. out_sparse->ir = (int*)malloc(in_sparse->nir*sizeof(int));
  8065. if ( out_sparse->ir != NULL )
  8066. memcpy(out_sparse->ir, in_sparse->ir, in_sparse->nir*sizeof(int));
  8067. out_sparse->njc = in_sparse->njc;
  8068. out_sparse->jc = (int*)malloc(in_sparse->njc*sizeof(int));
  8069. if ( out_sparse->jc != NULL )
  8070. memcpy(out_sparse->jc, in_sparse->jc, in_sparse->njc*sizeof(int));
  8071. out_sparse->ndata = in_sparse->ndata;
  8072. if ( out->isComplex && NULL != in_sparse->data ) {
  8073. out_sparse->data = malloc(sizeof(mat_complex_split_t));
  8074. if ( out_sparse->data != NULL ) {
  8075. mat_complex_split_t *out_data = (mat_complex_split_t*)out_sparse->data;
  8076. mat_complex_split_t *in_data = (mat_complex_split_t*)in_sparse->data;
  8077. out_data->Re = malloc(
  8078. in_sparse->ndata*Mat_SizeOf(in->data_type));
  8079. if ( NULL != out_data->Re )
  8080. memcpy(out_data->Re,in_data->Re,
  8081. in_sparse->ndata*Mat_SizeOf(in->data_type));
  8082. out_data->Im = malloc(
  8083. in_sparse->ndata*Mat_SizeOf(in->data_type));
  8084. if ( NULL != out_data->Im )
  8085. memcpy(out_data->Im,in_data->Im,
  8086. in_sparse->ndata*Mat_SizeOf(in->data_type));
  8087. }
  8088. } else if ( in_sparse->data != NULL ) {
  8089. out_sparse->data = malloc(in_sparse->ndata*Mat_SizeOf(in->data_type));
  8090. if ( NULL != out_sparse->data )
  8091. memcpy(out_sparse->data, in_sparse->data,
  8092. in_sparse->ndata*Mat_SizeOf(in->data_type));
  8093. }
  8094. }
  8095. } else if ( out->isComplex ) {
  8096. out->internal->data = malloc(sizeof(mat_complex_split_t));
  8097. if ( out->internal->data != NULL ) {
  8098. mat_complex_split_t *out_data = (mat_complex_split_t*)out->internal->data;
  8099. mat_complex_split_t *in_data = (mat_complex_split_t*)in->internal->data;
  8100. out_data->Re = malloc(out->nbytes);
  8101. if ( NULL != out_data->Re )
  8102. memcpy(out_data->Re,in_data->Re,out->nbytes);
  8103. out_data->Im = malloc(out->nbytes);
  8104. if ( NULL != out_data->Im )
  8105. memcpy(out_data->Im,in_data->Im,out->nbytes);
  8106. }
  8107. } else if ( NULL != (out->internal->data = malloc(in->nbytes)) ) {
  8108. memcpy(out->internal->data, in->internal->data, in->nbytes);
  8109. }
  8110. }
  8111. #endif
  8112. if ( !opt ) {
  8113. out->data = in->data;
  8114. } else if ( (in->data != NULL) && (in->class_type == MAT_C_STRUCT) ) {
  8115. matvar_t **infields, **outfields;
  8116. int nfields = 0;
  8117. out->data = malloc(in->nbytes);
  8118. if ( out->data != NULL && in->data_size > 0 ) {
  8119. nfields = in->nbytes / in->data_size;
  8120. infields = (matvar_t **)in->data;
  8121. outfields = (matvar_t **)out->data;
  8122. for ( i = 0; i < nfields; i++ ) {
  8123. outfields[i] = Mat_VarDuplicate(infields[i],opt);
  8124. }
  8125. }
  8126. } else if ( (in->data != NULL) && (in->class_type == MAT_C_CELL) ) {
  8127. matvar_t **incells, **outcells;
  8128. int ncells = 0;
  8129. out->data = malloc(in->nbytes);
  8130. if ( out->data != NULL && in->data_size > 0 ) {
  8131. ncells = in->nbytes / in->data_size;
  8132. incells = (matvar_t **)in->data;
  8133. outcells = (matvar_t **)out->data;
  8134. for ( i = 0; i < ncells; i++ ) {
  8135. outcells[i] = Mat_VarDuplicate(incells[i],opt);
  8136. }
  8137. }
  8138. } else if ( (in->data != NULL) && (in->class_type == MAT_C_SPARSE) ) {
  8139. out->data = malloc(sizeof(mat_sparse_t));
  8140. if ( out->data != NULL ) {
  8141. mat_sparse_t *out_sparse = (mat_sparse_t*)out->data;
  8142. mat_sparse_t *in_sparse = (mat_sparse_t*)in->data;
  8143. out_sparse->nzmax = in_sparse->nzmax;
  8144. out_sparse->nir = in_sparse->nir;
  8145. out_sparse->ir = (int*)malloc(in_sparse->nir*sizeof(int));
  8146. if ( out_sparse->ir != NULL )
  8147. memcpy(out_sparse->ir, in_sparse->ir, in_sparse->nir*sizeof(int));
  8148. out_sparse->njc = in_sparse->njc;
  8149. out_sparse->jc = (int*)malloc(in_sparse->njc*sizeof(int));
  8150. if ( out_sparse->jc != NULL )
  8151. memcpy(out_sparse->jc, in_sparse->jc, in_sparse->njc*sizeof(int));
  8152. out_sparse->ndata = in_sparse->ndata;
  8153. if ( out->isComplex && NULL != in_sparse->data ) {
  8154. out_sparse->data = malloc(sizeof(mat_complex_split_t));
  8155. if ( out_sparse->data != NULL ) {
  8156. mat_complex_split_t *out_data = (mat_complex_split_t*)out_sparse->data;
  8157. mat_complex_split_t *in_data = (mat_complex_split_t*)in_sparse->data;
  8158. out_data->Re = malloc(in_sparse->ndata*Mat_SizeOf(in->data_type));
  8159. if ( NULL != out_data->Re )
  8160. memcpy(out_data->Re,in_data->Re,in_sparse->ndata*Mat_SizeOf(in->data_type));
  8161. out_data->Im = malloc(in_sparse->ndata*Mat_SizeOf(in->data_type));
  8162. if ( NULL != out_data->Im )
  8163. memcpy(out_data->Im,in_data->Im,in_sparse->ndata*Mat_SizeOf(in->data_type));
  8164. }
  8165. } else if ( in_sparse->data != NULL ) {
  8166. out_sparse->data = malloc(in_sparse->ndata*Mat_SizeOf(in->data_type));
  8167. if ( NULL != out_sparse->data )
  8168. memcpy(out_sparse->data, in_sparse->data, in_sparse->ndata*Mat_SizeOf(in->data_type));
  8169. }
  8170. }
  8171. } else if ( in->data != NULL ) {
  8172. if ( out->isComplex ) {
  8173. out->data = malloc(sizeof(mat_complex_split_t));
  8174. if ( out->data != NULL ) {
  8175. mat_complex_split_t *out_data = (mat_complex_split_t*)out->data;
  8176. mat_complex_split_t *in_data = (mat_complex_split_t*)in->data;
  8177. out_data->Re = malloc(out->nbytes);
  8178. if ( NULL != out_data->Re )
  8179. memcpy(out_data->Re,in_data->Re,out->nbytes);
  8180. out_data->Im = malloc(out->nbytes);
  8181. if ( NULL != out_data->Im )
  8182. memcpy(out_data->Im,in_data->Im,out->nbytes);
  8183. }
  8184. } else {
  8185. out->data = malloc(in->nbytes);
  8186. if ( out->data != NULL )
  8187. memcpy(out->data,in->data,in->nbytes);
  8188. }
  8189. }
  8190. return out;
  8191. }
  8192. /** @brief Frees all the allocated memory associated with the structure
  8193. *
  8194. * Frees memory used by a MAT variable. Frees the data associated with a
  8195. * MAT variable if it's non-NULL and MAT_F_DONT_COPY_DATA was not used.
  8196. * @ingroup MAT
  8197. * @param matvar Pointer to the matvar_t structure
  8198. */
  8199. void
  8200. Mat_VarFree(matvar_t *matvar)
  8201. {
  8202. size_t nmemb = 0, i;
  8203. if ( !matvar )
  8204. return;
  8205. if ( matvar->dims ) {
  8206. nmemb = 1;
  8207. for ( i = 0; i < matvar->rank; i++ )
  8208. nmemb *= matvar->dims[i];
  8209. free(matvar->dims);
  8210. }
  8211. if ( matvar->name )
  8212. free(matvar->name);
  8213. if ( matvar->data != NULL) {
  8214. switch (matvar->class_type ) {
  8215. case MAT_C_STRUCT:
  8216. if ( !matvar->mem_conserve ) {
  8217. matvar_t **fields = (matvar_t**)matvar->data;
  8218. int nfields = matvar->internal->num_fields;
  8219. for ( i = 0; i < nmemb*nfields; i++ )
  8220. Mat_VarFree(fields[i]);
  8221. free(matvar->data);
  8222. break;
  8223. }
  8224. case MAT_C_CELL:
  8225. if ( !matvar->mem_conserve ) {
  8226. matvar_t **cells = (matvar_t**)matvar->data;
  8227. for ( i = 0; i < nmemb; i++ )
  8228. Mat_VarFree(cells[i]);
  8229. free(matvar->data);
  8230. }
  8231. break;
  8232. case MAT_C_SPARSE:
  8233. if ( !matvar->mem_conserve ) {
  8234. mat_sparse_t *sparse;
  8235. sparse = (mat_sparse_t*)matvar->data;
  8236. if ( sparse->ir != NULL )
  8237. free(sparse->ir);
  8238. if ( sparse->jc != NULL )
  8239. free(sparse->jc);
  8240. if ( matvar->isComplex && NULL != sparse->data ) {
  8241. mat_complex_split_t *complex_data = (mat_complex_split_t*)sparse->data;
  8242. free(complex_data->Re);
  8243. free(complex_data->Im);
  8244. free(complex_data);
  8245. } else if ( sparse->data != NULL ) {
  8246. free(sparse->data);
  8247. }
  8248. free(sparse);
  8249. }
  8250. break;
  8251. case MAT_C_DOUBLE:
  8252. case MAT_C_SINGLE:
  8253. case MAT_C_INT64:
  8254. case MAT_C_UINT64:
  8255. case MAT_C_INT32:
  8256. case MAT_C_UINT32:
  8257. case MAT_C_INT16:
  8258. case MAT_C_UINT16:
  8259. case MAT_C_INT8:
  8260. case MAT_C_UINT8:
  8261. case MAT_C_CHAR:
  8262. if ( !matvar->mem_conserve ) {
  8263. if ( matvar->isComplex ) {
  8264. mat_complex_split_t *complex_data = (mat_complex_split_t*)matvar->data;
  8265. free(complex_data->Re);
  8266. free(complex_data->Im);
  8267. free(complex_data);
  8268. } else {
  8269. free(matvar->data);
  8270. }
  8271. }
  8272. break;
  8273. case MAT_C_EMPTY:
  8274. case MAT_C_OBJECT:
  8275. case MAT_C_FUNCTION:
  8276. break;
  8277. }
  8278. }
  8279. if ( NULL != matvar->internal ) {
  8280. #if defined(HAVE_ZLIB)
  8281. if ( matvar->compression == MAT_COMPRESSION_ZLIB ) {
  8282. inflateEnd(matvar->internal->z);
  8283. free(matvar->internal->z);
  8284. if ( (matvar->internal->data != NULL) && (matvar->class_type == MAT_C_SPARSE) ) {
  8285. mat_sparse_t *sparse;
  8286. sparse = (mat_sparse_t*)matvar->internal->data;
  8287. if ( sparse->ir != NULL )
  8288. free(sparse->ir);
  8289. if ( sparse->jc != NULL )
  8290. free(sparse->jc);
  8291. if ( matvar->isComplex && NULL != sparse->data ) {
  8292. mat_complex_split_t *complex_data = (mat_complex_split_t*)sparse->data;
  8293. free(complex_data->Re);
  8294. free(complex_data->Im);
  8295. free(complex_data);
  8296. } else if ( sparse->data != NULL ) {
  8297. free(sparse->data);
  8298. }
  8299. free(sparse);
  8300. }
  8301. else if ( (matvar->internal->data != NULL) && matvar->isComplex ) {
  8302. mat_complex_split_t *complex_data =
  8303. (mat_complex_split_t*)matvar->internal->data;
  8304. free(complex_data->Re);
  8305. free(complex_data->Im);
  8306. free(complex_data);
  8307. } else if ( NULL != matvar->internal->data ) {
  8308. free(matvar->internal->data);
  8309. }
  8310. }
  8311. #endif
  8312. #if defined(HAVE_HDF5)
  8313. if ( -1 < matvar->internal->id ) {
  8314. switch ( H5Iget_type(matvar->internal->id) ) {
  8315. case H5I_GROUP:
  8316. H5Gclose(matvar->internal->id);
  8317. matvar->internal->id = -1;
  8318. break;
  8319. case H5I_DATASET:
  8320. H5Dclose(matvar->internal->id);
  8321. matvar->internal->id = -1;
  8322. break;
  8323. default:
  8324. break;
  8325. }
  8326. }
  8327. if ( 0 < matvar->internal->hdf5_ref ) {
  8328. switch ( H5Iget_type(matvar->internal->id) ) {
  8329. case H5I_GROUP:
  8330. H5Gclose(matvar->internal->id);
  8331. matvar->internal->hdf5_ref = -1;
  8332. break;
  8333. case H5I_DATASET:
  8334. H5Dclose(matvar->internal->id);
  8335. matvar->internal->hdf5_ref = -1;
  8336. break;
  8337. default:
  8338. break;
  8339. }
  8340. }
  8341. if ( NULL != matvar->internal->hdf5_name ) {
  8342. free(matvar->internal->hdf5_name);
  8343. matvar->internal->hdf5_name = NULL;
  8344. }
  8345. #endif
  8346. if ( NULL != matvar->internal->fieldnames &&
  8347. matvar->internal->num_fields > 0 ) {
  8348. size_t i;
  8349. for ( i = 0; i < matvar->internal->num_fields; i++ ) {
  8350. if ( NULL != matvar->internal->fieldnames[i] )
  8351. free(matvar->internal->fieldnames[i]);
  8352. }
  8353. free(matvar->internal->fieldnames);
  8354. }
  8355. free(matvar->internal);
  8356. matvar->internal = NULL;
  8357. }
  8358. /* FIXME: Why does this cause a SEGV? */
  8359. #if 0
  8360. memset(matvar,0,sizeof(matvar_t));
  8361. #endif
  8362. free(matvar);
  8363. }
  8364. /** @brief Calculate a single subscript from a set of subscript values
  8365. *
  8366. * Calculates a single linear subscript (0-relative) given a 1-relative
  8367. * subscript for each dimension. The calculation uses the formula below where
  8368. * index is the linear index, s is an array of length RANK where each element
  8369. * is the subscript for the corresponding dimension, D is an array whose
  8370. * elements are the dimensions of the variable.
  8371. * \f[
  8372. * index = \sum\limits_{k=0}^{RANK-1} [(s_k - 1) \prod\limits_{l=0}^{k} D_l ]
  8373. * \f]
  8374. * @ingroup MAT
  8375. * @param rank Rank of the variable
  8376. * @param dims Dimensions of the variable
  8377. * @param subs Array of dimension subscripts
  8378. * @return Single (linear) subscript
  8379. */
  8380. int
  8381. Mat_CalcSingleSubscript(int rank,int *dims,int *subs)
  8382. {
  8383. int index = 0, i, j, k, err = 0;
  8384. for ( i = 0; i < rank; i++ ) {
  8385. k = subs[i];
  8386. if ( k > dims[i] ) {
  8387. err = 1;
  8388. Mat_Critical("Mat_CalcSingleSubscript: index out of bounds");
  8389. break;
  8390. } else if ( k < 1 ) {
  8391. err = 1;
  8392. break;
  8393. }
  8394. k--;
  8395. for ( j = i; j--; )
  8396. k *= dims[j];
  8397. index += k;
  8398. }
  8399. if ( err )
  8400. index = -1;
  8401. return index;
  8402. }
  8403. /** @brief Calculate a single subscript from a set of subscript values
  8404. *
  8405. * Calculates a single linear subscript (0-relative) given a 1-relative
  8406. * subscript for each dimension. The calculation uses the formula below where
  8407. * index is the linear index, s is an array of length RANK where each element
  8408. * is the subscript for the corresponding dimension, D is an array whose
  8409. * elements are the dimensions of the variable.
  8410. * \f[
  8411. * index = \sum\limits_{k=0}^{RANK-1} [(s_k - 1) \prod\limits_{l=0}^{k} D_l ]
  8412. * \f]
  8413. * @ingroup MAT
  8414. * @param rank Rank of the variable
  8415. * @param dims Dimensions of the variable
  8416. * @param subs Array of dimension subscripts
  8417. * @param[out] index Single (linear) subscript
  8418. * @retval 0 on success
  8419. */
  8420. int
  8421. Mat_CalcSingleSubscript2(int rank,size_t *dims,size_t *subs,size_t *index)
  8422. {
  8423. int i, err = 0;
  8424. for ( i = 0; i < rank; i++ ) {
  8425. int j;
  8426. size_t k = subs[i];
  8427. if ( k > dims[i] ) {
  8428. err = 1;
  8429. Mat_Critical("Mat_CalcSingleSubscript2: index out of bounds");
  8430. break;
  8431. } else if ( k < 1 ) {
  8432. err = 1;
  8433. break;
  8434. }
  8435. k--;
  8436. for ( j = i; j--; )
  8437. k *= dims[j];
  8438. *index += k;
  8439. }
  8440. return err;
  8441. }
  8442. /** @brief Calculate a set of subscript values from a single(linear) subscript
  8443. *
  8444. * Calculates 1-relative subscripts for each dimension given a 0-relative
  8445. * linear index. Subscripts are calculated as follows where s is the array
  8446. * of dimension subscripts, D is the array of dimensions, and index is the
  8447. * linear index.
  8448. * \f[
  8449. * s_k = \lfloor\frac{1}{L} \prod\limits_{l = 0}^{k} D_l\rfloor + 1
  8450. * \f]
  8451. * \f[
  8452. * L = index - \sum\limits_{l = k}^{RANK - 1} s_k \prod\limits_{m = 0}^{k} D_m
  8453. * \f]
  8454. * @ingroup MAT
  8455. * @param rank Rank of the variable
  8456. * @param dims Dimensions of the variable
  8457. * @param index Linear index
  8458. * @return Array of dimension subscripts
  8459. */
  8460. int *
  8461. Mat_CalcSubscripts(int rank,int *dims,int index)
  8462. {
  8463. int i, j, k, *subs;
  8464. double l;
  8465. subs = (int*)malloc(rank*sizeof(int));
  8466. l = index;
  8467. for ( i = rank; i--; ) {
  8468. k = 1;
  8469. for ( j = i; j--; )
  8470. k *= dims[j];
  8471. subs[i] = floor(l / (double)k);
  8472. l -= subs[i]*k;
  8473. subs[i]++;
  8474. }
  8475. return subs;
  8476. }
  8477. /** @brief Calculate a set of subscript values from a single(linear) subscript
  8478. *
  8479. * Calculates 1-relative subscripts for each dimension given a 0-relative
  8480. * linear index. Subscripts are calculated as follows where s is the array
  8481. * of dimension subscripts, D is the array of dimensions, and index is the
  8482. * linear index.
  8483. * \f[
  8484. * s_k = \lfloor\frac{1}{L} \prod\limits_{l = 0}^{k} D_l\rfloor + 1
  8485. * \f]
  8486. * \f[
  8487. * L = index - \sum\limits_{l = k}^{RANK - 1} s_k \prod\limits_{m = 0}^{k} D_m
  8488. * \f]
  8489. * @ingroup MAT
  8490. * @param rank Rank of the variable
  8491. * @param dims Dimensions of the variable
  8492. * @param index Linear index
  8493. * @return Array of dimension subscripts
  8494. */
  8495. size_t *
  8496. Mat_CalcSubscripts2(int rank,size_t *dims,size_t index)
  8497. {
  8498. int i;
  8499. size_t *subs;
  8500. double l;
  8501. subs = (size_t*)malloc(rank*sizeof(size_t));
  8502. l = (double)index;
  8503. for ( i = rank; i--; ) {
  8504. int j;
  8505. size_t k = 1;
  8506. for ( j = i; j--; )
  8507. k *= dims[j];
  8508. subs[i] = (size_t)floor(l / (double)k);
  8509. l -= subs[i]*k;
  8510. subs[i]++;
  8511. }
  8512. return subs;
  8513. }
  8514. /** @brief Calculates the size of a matlab variable in bytes
  8515. *
  8516. * @ingroup MAT
  8517. * @param matvar matlab variable
  8518. * @returns size of the variable in bytes
  8519. */
  8520. size_t
  8521. Mat_VarGetSize(matvar_t *matvar)
  8522. {
  8523. int nmemb, i;
  8524. size_t bytes = 0;
  8525. if ( matvar->class_type == MAT_C_STRUCT ) {
  8526. int nfields;
  8527. matvar_t **fields;
  8528. nmemb = 1;
  8529. for ( i = 0; i < matvar->rank; i++ )
  8530. nmemb *= matvar->dims[i];
  8531. nfields = matvar->internal->num_fields;
  8532. if ( nmemb*nfields > 0 ) {
  8533. fields = (matvar_t**)matvar->data;
  8534. if ( NULL != fields )
  8535. for ( i = 0; i < nmemb*nfields; i++ )
  8536. bytes += Mat_VarGetSize(fields[i]);
  8537. }
  8538. } else if ( matvar->class_type == MAT_C_CELL ) {
  8539. int ncells;
  8540. matvar_t **cells;
  8541. ncells = matvar->nbytes / matvar->data_size;
  8542. cells = (matvar_t**)matvar->data;
  8543. if ( NULL != cells )
  8544. for ( i = 0; i < ncells; i++ )
  8545. bytes += Mat_VarGetSize(cells[i]);
  8546. } else {
  8547. nmemb = 1;
  8548. for ( i = 0; i < matvar->rank; i++ )
  8549. nmemb *= matvar->dims[i];
  8550. bytes += nmemb*Mat_SizeOfClass(matvar->class_type);
  8551. }
  8552. return bytes;
  8553. }
  8554. /** @brief Prints the variable information
  8555. *
  8556. * Prints to stdout the values of the @ref matvar_t structure
  8557. * @ingroup MAT
  8558. * @param matvar Pointer to the matvar_t structure
  8559. * @param printdata set to 1 if the Variables data should be printed, else 0
  8560. */
  8561. void
  8562. Mat_VarPrint( matvar_t *matvar, int printdata )
  8563. {
  8564. size_t nmemb;
  8565. int i, j;
  8566. const char *class_type_desc[16] = {"Undefined","Cell Array","Structure",
  8567. "Object","Character Array","Sparse Array","Double Precision Array",
  8568. "Single Precision Array", "8-bit, signed integer array",
  8569. "8-bit, unsigned integer array","16-bit, signed integer array",
  8570. "16-bit, unsigned integer array","32-bit, signed integer array",
  8571. "32-bit, unsigned integer array","64-bit, signed integer array",
  8572. "64-bit, unsigned integer array"};
  8573. const char *data_type_desc[23] = {"Unknown","8-bit, signed integer",
  8574. "8-bit, unsigned integer","16-bit, signed integer",
  8575. "16-bit, unsigned integer","32-bit, signed integer",
  8576. "32-bit, unsigned integer","IEEE 754 single-precision","RESERVED",
  8577. "IEEE 754 double-precision","RESERVED","RESERVED",
  8578. "64-bit, signed integer","64-bit, unsigned integer", "Matlab Array",
  8579. "Compressed Data","Unicode UTF-8 Encoded Character Data",
  8580. "Unicode UTF-16 Encoded Character Data",
  8581. "Unicode UTF-32 Encoded Character Data","","String","Cell Array",
  8582. "Structure"};
  8583. if ( matvar == NULL )
  8584. return;
  8585. if ( matvar->name )
  8586. printf(" Name: %s\n", matvar->name);
  8587. printf(" Rank: %d\n", matvar->rank);
  8588. if ( matvar->rank == 0 )
  8589. return;
  8590. printf("Dimensions: %" SIZE_T_FMTSTR,matvar->dims[0]);
  8591. nmemb = matvar->dims[0];
  8592. for ( i = 1; i < matvar->rank; i++ ) {
  8593. printf(" x %" SIZE_T_FMTSTR,matvar->dims[i]);
  8594. nmemb *= matvar->dims[i];
  8595. }
  8596. printf("\n");
  8597. printf("Class Type: %s",class_type_desc[matvar->class_type]);
  8598. if ( matvar->isComplex )
  8599. printf(" (complex)");
  8600. else if ( matvar->isLogical )
  8601. printf(" (logical)");
  8602. printf("\n");
  8603. if ( matvar->data_type )
  8604. printf(" Data Type: %s\n", data_type_desc[matvar->data_type]);
  8605. if ( MAT_C_STRUCT == matvar->class_type ) {
  8606. matvar_t **fields = (matvar_t **)matvar->data;
  8607. int nfields = matvar->internal->num_fields;
  8608. if ( nmemb*nfields > 0 ) {
  8609. printf("Fields[%" SIZE_T_FMTSTR "] {\n", nfields*nmemb);
  8610. for ( i = 0; i < nfields*nmemb; i++ ) {
  8611. if ( NULL == fields[i] ) {
  8612. printf(" Name: %s\n Rank: %d\n",
  8613. matvar->internal->fieldnames[i%nfields],0);
  8614. } else {
  8615. Mat_VarPrint(fields[i],printdata);
  8616. }
  8617. }
  8618. printf("}\n");
  8619. } else {
  8620. printf("Fields[%d] {\n", nfields);
  8621. for ( i = 0; i < nfields; i++ )
  8622. printf(" Name: %s\n Rank: %d\n",
  8623. matvar->internal->fieldnames[i],0);
  8624. printf("}\n");
  8625. }
  8626. return;
  8627. } else if ( matvar->data == NULL || matvar->data_size < 1 ) {
  8628. return;
  8629. } else if ( MAT_C_CELL == matvar->class_type ) {
  8630. matvar_t **cells = (matvar_t **)matvar->data;
  8631. int ncells = matvar->nbytes / matvar->data_size;
  8632. printf("{\n");
  8633. for ( i = 0; i < ncells; i++ )
  8634. Mat_VarPrint(cells[i],printdata);
  8635. printf("}\n");
  8636. return;
  8637. } else if ( !printdata ) {
  8638. return;
  8639. }
  8640. printf("{\n");
  8641. if ( matvar->rank > 2 ) {
  8642. printf("I can't print more than 2 dimensions\n");
  8643. } else if ( matvar->rank == 1 && matvar->dims[0] > 15 ) {
  8644. printf("I won't print more than 15 elements in a vector\n");
  8645. } else if ( matvar->rank==2 ) {
  8646. switch( matvar->class_type ) {
  8647. case MAT_C_DOUBLE:
  8648. case MAT_C_SINGLE:
  8649. #ifdef HAVE_MATIO_INT64_T
  8650. case MAT_C_INT64:
  8651. #endif
  8652. #ifdef HAVE_MATIO_UINT64_T
  8653. case MAT_C_UINT64:
  8654. #endif
  8655. case MAT_C_INT32:
  8656. case MAT_C_UINT32:
  8657. case MAT_C_INT16:
  8658. case MAT_C_UINT16:
  8659. case MAT_C_INT8:
  8660. case MAT_C_UINT8:
  8661. {
  8662. size_t stride = Mat_SizeOf(matvar->data_type);
  8663. if ( matvar->isComplex ) {
  8664. mat_complex_split_t *complex_data = (mat_complex_split_t*)matvar->data;
  8665. char *rp = (char*)complex_data->Re;
  8666. char *ip = (char*)complex_data->Im;
  8667. for ( i = 0; i < matvar->dims[0] && i < 15; i++ ) {
  8668. for ( j = 0; j < matvar->dims[1] && j < 15; j++ ) {
  8669. size_t idx = matvar->dims[0]*j+i;
  8670. Mat_PrintNumber(matvar->data_type,rp+idx*stride);
  8671. printf(" + ");
  8672. Mat_PrintNumber(matvar->data_type,ip+idx*stride);
  8673. printf("i ");
  8674. }
  8675. if ( j < matvar->dims[1] )
  8676. printf("...");
  8677. printf("\n");
  8678. }
  8679. if ( i < matvar->dims[0] )
  8680. printf(".\n.\n.\n");
  8681. } else {
  8682. char *data = (char*)matvar->data;
  8683. for ( i = 0; i < matvar->dims[0] && i < 15; i++ ) {
  8684. for ( j = 0; j < matvar->dims[1] && j < 15; j++ ) {
  8685. size_t idx = matvar->dims[0]*j+i;
  8686. Mat_PrintNumber(matvar->data_type,
  8687. data+idx*stride);
  8688. printf(" ");
  8689. }
  8690. if ( j < matvar->dims[1] )
  8691. printf("...");
  8692. printf("\n");
  8693. }
  8694. if ( i < matvar->dims[0] )
  8695. printf(".\n.\n.\n");
  8696. }
  8697. break;
  8698. }
  8699. case MAT_C_CHAR:
  8700. {
  8701. char *data = (char*)matvar->data;
  8702. for ( i = 0; i < matvar->dims[0]; i++ ) {
  8703. for ( j = 0; j < matvar->dims[1]; j++ )
  8704. printf("%c",data[j*matvar->dims[0]+i]);
  8705. printf("\n");
  8706. }
  8707. break;
  8708. }
  8709. case MAT_C_SPARSE:
  8710. {
  8711. mat_sparse_t *sparse;
  8712. size_t stride = Mat_SizeOf(matvar->data_type);
  8713. #if !defined(EXTENDED_SPARSE)
  8714. if ( MAT_T_DOUBLE != matvar->data_type )
  8715. break;
  8716. #endif
  8717. sparse = (mat_sparse_t*)matvar->data;
  8718. if ( matvar->isComplex ) {
  8719. mat_complex_split_t *complex_data = (mat_complex_split_t*)sparse->data;
  8720. char *re = (char*)complex_data->Re;
  8721. char *im = (char*)complex_data->Im;
  8722. for ( i = 0; i < sparse->njc-1; i++ ) {
  8723. for ( j = sparse->jc[i];
  8724. j < sparse->jc[i+1] && j < sparse->ndata; j++ ) {
  8725. printf(" (%d,%d) ",sparse->ir[j]+1,i+1);
  8726. Mat_PrintNumber(matvar->data_type,re+j*stride);
  8727. printf(" + ");
  8728. Mat_PrintNumber(matvar->data_type,im+j*stride);
  8729. printf("i\n");
  8730. }
  8731. }
  8732. } else {
  8733. char *data = (char*)sparse->data;
  8734. for ( i = 0; i < sparse->njc-1; i++ ) {
  8735. for ( j = sparse->jc[i];
  8736. j < sparse->jc[i+1] && j < sparse->ndata; j++ ) {
  8737. printf(" (%d,%d) ",sparse->ir[j]+1,i+1);
  8738. Mat_PrintNumber(matvar->data_type,data+j*stride);
  8739. printf("\n");
  8740. }
  8741. }
  8742. }
  8743. break;
  8744. } /* case MAT_C_SPARSE: */
  8745. default:
  8746. break;
  8747. } /* switch( matvar->class_type ) */
  8748. }
  8749. printf("}\n");
  8750. return;
  8751. }
  8752. /** @brief Reads MAT variable data from a file
  8753. *
  8754. * Reads data from a MAT variable. The variable must have been read by
  8755. * Mat_VarReadInfo.
  8756. * @ingroup MAT
  8757. * @param mat MAT file to read data from
  8758. * @param matvar MAT variable information
  8759. * @param data pointer to store data in (must be pre-allocated)
  8760. * @param start array of starting indices
  8761. * @param stride stride of data
  8762. * @param edge array specifying the number to read in each direction
  8763. * @retval 0 on success
  8764. */
  8765. int
  8766. Mat_VarReadData(mat_t *mat,matvar_t *matvar,void *data,
  8767. int *start,int *stride,int *edge)
  8768. {
  8769. int err = 0;
  8770. switch ( matvar->class_type ) {
  8771. case MAT_C_DOUBLE:
  8772. case MAT_C_SINGLE:
  8773. case MAT_C_INT64:
  8774. case MAT_C_UINT64:
  8775. case MAT_C_INT32:
  8776. case MAT_C_UINT32:
  8777. case MAT_C_INT16:
  8778. case MAT_C_UINT16:
  8779. case MAT_C_INT8:
  8780. case MAT_C_UINT8:
  8781. break;
  8782. default:
  8783. return -1;
  8784. }
  8785. switch ( mat->version ) {
  8786. case MAT_FT_MAT73:
  8787. #if defined(HAVE_HDF5)
  8788. err = Mat_VarReadData73(mat,matvar,data,start,stride,edge);
  8789. #else
  8790. err = 1;
  8791. #endif
  8792. break;
  8793. case MAT_FT_MAT5:
  8794. err = ReadData5(mat,matvar,data,start,stride,edge);
  8795. break;
  8796. case MAT_FT_MAT4:
  8797. err = ReadData4(mat,matvar,data,start,stride,edge);
  8798. break;
  8799. }
  8800. return err;
  8801. }
  8802. /** @brief Reads all the data for a matlab variable
  8803. *
  8804. * Allocates memory for an reads the data for a given matlab variable.
  8805. * @ingroup MAT
  8806. * @param mat Matlab MAT file structure pointer
  8807. * @param matvar Variable whose data is to be read
  8808. * @returns non-zero on error
  8809. */
  8810. int
  8811. Mat_VarReadDataAll(mat_t *mat,matvar_t *matvar)
  8812. {
  8813. int err = 0;
  8814. if ( (mat == NULL) || (matvar == NULL) )
  8815. err = 1;
  8816. else
  8817. ReadData(mat,matvar);
  8818. return err;
  8819. }
  8820. /** @brief Reads a subset of a MAT variable using a 1-D indexing
  8821. *
  8822. * Reads data from a MAT variable using a linear (1-D) indexing mode. The
  8823. * variable must have been read by Mat_VarReadInfo.
  8824. * @ingroup MAT
  8825. * @param mat MAT file to read data from
  8826. * @param matvar MAT variable information
  8827. * @param data pointer to store data in (must be pre-allocated)
  8828. * @param start starting index
  8829. * @param stride stride of data
  8830. * @param edge number of elements to read
  8831. * @retval 0 on success
  8832. */
  8833. int
  8834. Mat_VarReadDataLinear(mat_t *mat,matvar_t *matvar,void *data,int start,
  8835. int stride,int edge)
  8836. {
  8837. int err = 0;
  8838. switch ( matvar->class_type ) {
  8839. case MAT_C_DOUBLE:
  8840. case MAT_C_SINGLE:
  8841. case MAT_C_INT64:
  8842. case MAT_C_UINT64:
  8843. case MAT_C_INT32:
  8844. case MAT_C_UINT32:
  8845. case MAT_C_INT16:
  8846. case MAT_C_UINT16:
  8847. case MAT_C_INT8:
  8848. case MAT_C_UINT8:
  8849. break;
  8850. default:
  8851. return -1;
  8852. }
  8853. switch ( mat->version ) {
  8854. case MAT_FT_MAT73:
  8855. #if defined(HAVE_HDF5)
  8856. err = Mat_VarReadDataLinear73(mat,matvar,data,start,stride,edge);
  8857. #else
  8858. err = 1;
  8859. #endif
  8860. break;
  8861. case MAT_FT_MAT5:
  8862. err = Mat_VarReadDataLinear5(mat,matvar,data,start,stride,edge);
  8863. break;
  8864. case MAT_FT_MAT4:
  8865. err = Mat_VarReadDataLinear4(mat,matvar,data,start,stride,edge);
  8866. break;
  8867. }
  8868. return err;
  8869. }
  8870. /** @brief Reads the information of the next variable in a MAT file
  8871. *
  8872. * Reads the next variable's information (class,flags-complex/global/logical,
  8873. * rank,dimensions, name, etc) from the Matlab MAT file. After reading, the MAT
  8874. * file is positioned past the current variable.
  8875. * @ingroup MAT
  8876. * @param mat Pointer to the MAT file
  8877. * @return Pointer to the @ref matvar_t structure containing the MAT
  8878. * variable information
  8879. */
  8880. matvar_t *
  8881. Mat_VarReadNextInfo( mat_t *mat )
  8882. {
  8883. matvar_t *matvar = NULL;
  8884. if( mat == NULL )
  8885. return NULL;
  8886. switch ( mat->version ) {
  8887. case MAT_FT_MAT5:
  8888. matvar = Mat_VarReadNextInfo5(mat);
  8889. break;
  8890. case MAT_FT_MAT73:
  8891. #if defined(HAVE_HDF5)
  8892. matvar = Mat_VarReadNextInfo73(mat);
  8893. #endif
  8894. break;
  8895. case MAT_FT_MAT4:
  8896. matvar = Mat_VarReadNextInfo4(mat);
  8897. break;
  8898. }
  8899. return matvar;
  8900. }
  8901. /** @brief Reads the information of a variable with the given name from a MAT file
  8902. *
  8903. * Reads the named variable (or the next variable if name is NULL) information
  8904. * (class,flags-complex/global/logical,rank,dimensions,and name) from the
  8905. * Matlab MAT file
  8906. * @ingroup MAT
  8907. * @param mat Pointer to the MAT file
  8908. * @param name Name of the variable to read
  8909. * @return Pointer to the @ref matvar_t structure containing the MAT
  8910. * variable information
  8911. */
  8912. matvar_t *
  8913. Mat_VarReadInfo( mat_t *mat, const char *name )
  8914. {
  8915. int fpos;
  8916. matvar_t *matvar = NULL;
  8917. if ( (mat == NULL) || (name == NULL) )
  8918. return NULL;
  8919. if ( mat->version == MAT_FT_MAT73 ) {
  8920. fpos = mat->next_index;
  8921. mat->next_index = 0;
  8922. do {
  8923. matvar = Mat_VarReadNextInfo(mat);
  8924. if ( matvar != NULL ) {
  8925. if ( matvar->name == NULL || strcmp(matvar->name,name) ) {
  8926. Mat_VarFree(matvar);
  8927. matvar = NULL;
  8928. }
  8929. } else {
  8930. Mat_Critical("An error occurred in reading the MAT file");
  8931. break;
  8932. }
  8933. } while ( NULL == matvar && mat->next_index < mat->num_datasets );
  8934. mat->next_index = fpos;
  8935. } else {
  8936. fpos = ftell((FILE*)mat->fp);
  8937. if ( fpos != -1L ) {
  8938. (void)fseek((FILE*)mat->fp,mat->bof,SEEK_SET);
  8939. do {
  8940. matvar = Mat_VarReadNextInfo(mat);
  8941. if ( matvar != NULL ) {
  8942. if ( matvar->name == NULL || strcmp(matvar->name,name) ) {
  8943. Mat_VarFree(matvar);
  8944. matvar = NULL;
  8945. }
  8946. } else if (!feof((FILE *)mat->fp)) {
  8947. Mat_Critical("An error occurred in reading the MAT file");
  8948. break;
  8949. }
  8950. } while ( NULL == matvar && !feof((FILE *)mat->fp) );
  8951. (void)fseek((FILE*)mat->fp,fpos,SEEK_SET);
  8952. } else {
  8953. Mat_Critical("Couldn't determine file position");
  8954. }
  8955. }
  8956. return matvar;
  8957. }
  8958. /** @brief Reads the variable with the given name from a MAT file
  8959. *
  8960. * Reads the next variable in the Matlab MAT file
  8961. * @ingroup MAT
  8962. * @param mat Pointer to the MAT file
  8963. * @param name Name of the variable to read
  8964. * @return Pointer to the @ref matvar_t structure containing the MAT
  8965. * variable information
  8966. */
  8967. matvar_t *
  8968. Mat_VarRead( mat_t *mat, const char *name )
  8969. {
  8970. int fpos;
  8971. matvar_t *matvar = NULL;
  8972. if ( (mat == NULL) || (name == NULL) )
  8973. return NULL;
  8974. if ( MAT_FT_MAT73 != mat->version ) {
  8975. fpos = ftell((FILE*)mat->fp);
  8976. if ( fpos == -1L ) {
  8977. Mat_Critical("Couldn't determine file position");
  8978. return NULL;
  8979. }
  8980. } else {
  8981. fpos = mat->next_index;
  8982. mat->next_index = 0;
  8983. }
  8984. matvar = Mat_VarReadInfo(mat,name);
  8985. if ( matvar )
  8986. ReadData(mat,matvar);
  8987. if ( MAT_FT_MAT73 != mat->version ) {
  8988. (void)fseek((FILE*)mat->fp,fpos,SEEK_SET);
  8989. } else {
  8990. mat->next_index = fpos;
  8991. }
  8992. return matvar;
  8993. }
  8994. /** @brief Reads the next variable in a MAT file
  8995. *
  8996. * Reads the next variable in the Matlab MAT file
  8997. * @ingroup MAT
  8998. * @param mat Pointer to the MAT file
  8999. * @return Pointer to the @ref matvar_t structure containing the MAT
  9000. * variable information
  9001. */
  9002. matvar_t *
  9003. Mat_VarReadNext( mat_t *mat )
  9004. {
  9005. int fpos = 0;
  9006. matvar_t *matvar = NULL;
  9007. if ( mat->version != MAT_FT_MAT73 ) {
  9008. if ( feof((FILE *)mat->fp) )
  9009. return NULL;
  9010. /* Read position so we can reset the file position if an error occurs */
  9011. fpos = ftell((FILE*)mat->fp);
  9012. if ( fpos == -1L ) {
  9013. Mat_Critical("Couldn't determine file position");
  9014. return NULL;
  9015. }
  9016. }
  9017. matvar = Mat_VarReadNextInfo(mat);
  9018. if ( matvar ) {
  9019. ReadData(mat,matvar);
  9020. } else if (mat->version != MAT_FT_MAT73 ) {
  9021. (void)fseek((FILE*)mat->fp,fpos,SEEK_SET);
  9022. }
  9023. return matvar;
  9024. }
  9025. /** @brief Writes the given MAT variable to a MAT file
  9026. *
  9027. * Writes the MAT variable information stored in matvar to the given MAT file.
  9028. * The variable will be written to the end of the file.
  9029. * @ingroup MAT
  9030. * @param mat MAT file to write to
  9031. * @param matvar MAT variable information to write
  9032. * @retval 0 on success
  9033. */
  9034. int
  9035. Mat_VarWriteInfo(mat_t *mat, matvar_t *matvar )
  9036. {
  9037. int err = 0;
  9038. if ( mat == NULL || matvar == NULL || mat->fp == NULL )
  9039. return -1;
  9040. else if ( mat->version == MAT_FT_MAT5 )
  9041. WriteInfo5(mat,matvar);
  9042. else
  9043. err = 1;
  9044. return err;
  9045. }
  9046. /** @brief Writes the given data to the MAT variable
  9047. *
  9048. * Writes data to a MAT variable. The variable must have previously been
  9049. * written with Mat_VarWriteInfo.
  9050. * @ingroup MAT
  9051. * @param mat MAT file to write to
  9052. * @param matvar MAT variable information to write
  9053. * @param data pointer to the data to write
  9054. * @param start array of starting indices
  9055. * @param stride stride of data
  9056. * @param edge array specifying the number to read in each direction
  9057. * @retval 0 on success
  9058. */
  9059. int
  9060. Mat_VarWriteData(mat_t *mat,matvar_t *matvar,void *data,
  9061. int *start,int *stride,int *edge)
  9062. {
  9063. int err = 0, k, N = 1;
  9064. if ( mat == NULL || matvar == NULL )
  9065. return -1;
  9066. (void)fseek((FILE*)mat->fp,matvar->internal->datapos+8,SEEK_SET);
  9067. if ( data == NULL ) {
  9068. err = -1;
  9069. } else if ( start == NULL && stride == NULL && edge == NULL ) {
  9070. for ( k = 0; k < matvar->rank; k++ )
  9071. N *= matvar->dims[k];
  9072. if ( matvar->compression == MAT_COMPRESSION_NONE )
  9073. WriteData(mat,data,N,matvar->data_type);
  9074. #if 0
  9075. else if ( matvar->compression == MAT_COMPRESSION_ZLIB ) {
  9076. WriteCompressedData(mat,matvar->internal->z,data,N,matvar->data_type);
  9077. (void)deflateEnd(matvar->internal->z);
  9078. free(matvar->internal->z);
  9079. matvar->internal->z = NULL;
  9080. }
  9081. #endif
  9082. } else if ( start == NULL || stride == NULL || edge == NULL ) {
  9083. err = 1;
  9084. } else if ( matvar->rank == 2 ) {
  9085. if ( stride[0]*(edge[0]-1)+start[0]+1 > matvar->dims[0] ) {
  9086. err = 1;
  9087. } else if ( stride[1]*(edge[1]-1)+start[1]+1 > matvar->dims[1] ) {
  9088. err = 1;
  9089. } else {
  9090. switch ( matvar->class_type ) {
  9091. case MAT_C_DOUBLE:
  9092. case MAT_C_SINGLE:
  9093. case MAT_C_INT64:
  9094. case MAT_C_UINT64:
  9095. case MAT_C_INT32:
  9096. case MAT_C_UINT32:
  9097. case MAT_C_INT16:
  9098. case MAT_C_UINT16:
  9099. case MAT_C_INT8:
  9100. case MAT_C_UINT8:
  9101. WriteDataSlab2(mat,data,matvar->data_type,matvar->dims,
  9102. start,stride,edge);
  9103. break;
  9104. case MAT_C_CHAR:
  9105. WriteCharDataSlab2(mat,data,matvar->data_type,matvar->dims,
  9106. start,stride,edge);
  9107. break;
  9108. default:
  9109. break;
  9110. }
  9111. }
  9112. }
  9113. return err;
  9114. }
  9115. /** @brief Writes the given MAT variable to a MAT file
  9116. *
  9117. * Writes the MAT variable information stored in matvar to the given MAT file.
  9118. * The variable will be written to the end of the file.
  9119. * @ingroup MAT
  9120. * @param mat MAT file to write to
  9121. * @param matvar MAT variable information to write
  9122. * @param compress Whether or not to compress the data
  9123. * (Only valid for version 5 MAT files and variables with numeric data)
  9124. * @retval 0 on success
  9125. */
  9126. int
  9127. Mat_VarWrite(mat_t *mat,matvar_t *matvar,enum matio_compression compress)
  9128. {
  9129. if ( mat == NULL || matvar == NULL )
  9130. return -1;
  9131. else if ( mat->version == MAT_FT_MAT4 )
  9132. return Mat_VarWrite4(mat,matvar);
  9133. else if ( mat->version == MAT_FT_MAT5 )
  9134. return Mat_VarWrite5(mat,matvar,compress);
  9135. #if defined(HAVE_HDF5)
  9136. else if ( mat->version == MAT_FT_MAT73 )
  9137. return Mat_VarWrite73(mat,matvar,compress);
  9138. #endif
  9139. return 1;
  9140. }
  9141. /* -------------------------------
  9142. * ---------- mat4.c
  9143. * -------------------------------
  9144. */
  9145. /** @file mat4.c
  9146. * Matlab MAT version 4 file functions
  9147. * @ingroup MAT
  9148. */
  9149. #include <stdlib.h>
  9150. #include <string.h>
  9151. #include <stdio.h>
  9152. #include <math.h>
  9153. #include <limits.h>
  9154. #if defined(__GLIBC__)
  9155. #include <endian.h>
  9156. #endif
  9157. /** @if mat_devman
  9158. * @brief Creates a new Matlab MAT version 4 file
  9159. *
  9160. * Tries to create a new Matlab MAT file with the given name.
  9161. * @ingroup MAT
  9162. * @param matname Name of MAT file to create
  9163. * @return A pointer to the MAT file or NULL if it failed. This is not a
  9164. * simple FILE * and should not be used as one.
  9165. * @endif
  9166. */
  9167. static mat_t *
  9168. Mat_Create4(const char* matname)
  9169. {
  9170. FILE *fp = NULL;
  9171. mat_t *mat = NULL;
  9172. int byteswap;
  9173. #if defined(__GLIBC__)
  9174. #if (__BYTE_ORDER == __LITTLE_ENDIAN)
  9175. byteswap = 0;
  9176. #elif (__BYTE_ORDER == __BIG_ENDIAN)
  9177. byteswap = 1;
  9178. #else
  9179. return NULL;
  9180. #endif
  9181. #elif defined(_BIG_ENDIAN) && !defined(_LITTLE_ENDIAN)
  9182. byteswap = 1;
  9183. #elif defined(_LITTLE_ENDIAN) && !defined(_BIG_ENDIAN)
  9184. byteswap = 0;
  9185. #elif defined(__sparc) || defined(__sparc__) || defined(_POWER) || defined(__powerpc__) || \
  9186. defined(__ppc__) || defined(__hpux) || defined(_MIPSEB) || defined(_POWER) || defined(__s390__)
  9187. byteswap = 1;
  9188. #elif defined(__i386__) || defined(__alpha__) || defined(__ia64) || defined(__ia64__) || \
  9189. defined(_M_IX86) || defined(_M_IA64) || defined(_M_ALPHA) || defined(__amd64) || \
  9190. defined(__amd64__) || defined(_M_AMD64) || defined(__x86_64) || defined(__x86_64__) || \
  9191. defined(_M_X64) || defined(__bfin__)
  9192. byteswap = 0;
  9193. #else
  9194. return NULL;
  9195. #endif
  9196. fp = fopen(matname,"wb");
  9197. if ( !fp )
  9198. return NULL;
  9199. mat = (mat_t*)malloc(sizeof(*mat));
  9200. if ( NULL == mat ) {
  9201. fclose(fp);
  9202. Mat_Critical("Couldn't allocate memory for the MAT file");
  9203. return NULL;
  9204. }
  9205. mat->header = NULL;
  9206. mat->subsys_offset = NULL;
  9207. mat->fp = fp;
  9208. mat->version = MAT_FT_MAT4;
  9209. mat->byteswap = byteswap;
  9210. mat->bof = 0;
  9211. mat->next_index = 0;
  9212. mat->refs_id = -1;
  9213. mat->filename = strdup_printf("%s",matname);
  9214. mat->mode = 0;
  9215. Mat_Rewind(mat);
  9216. return mat;
  9217. }
  9218. /** @if mat_devman
  9219. * @brief Writes a matlab variable to a version 4 matlab file
  9220. *
  9221. * @ingroup mat_internal
  9222. * @param mat MAT file pointer
  9223. * @param matvar pointer to the mat variable
  9224. * @retval 0 on success
  9225. * @endif
  9226. */
  9227. static int
  9228. Mat_VarWrite4(mat_t *mat,matvar_t *matvar)
  9229. {
  9230. typedef struct {
  9231. mat_int32_t type;
  9232. mat_int32_t mrows;
  9233. mat_int32_t ncols;
  9234. mat_int32_t imagf;
  9235. mat_int32_t namelen;
  9236. } Fmatrix;
  9237. mat_int32_t nmemb = 1, i;
  9238. Fmatrix x;
  9239. if ( NULL == mat || NULL == matvar || NULL == matvar->name || matvar->rank != 2 )
  9240. return -1;
  9241. /* FIXME: SEEK_END is not Guaranteed by the C standard */
  9242. (void)fseek((FILE*)mat->fp,0,SEEK_END); /* Always write at end of file */
  9243. switch ( matvar->data_type ) {
  9244. case MAT_T_DOUBLE:
  9245. x.type = 0;
  9246. break;
  9247. case MAT_T_SINGLE:
  9248. x.type = 10;
  9249. break;
  9250. case MAT_T_INT32:
  9251. x.type = 20;
  9252. break;
  9253. case MAT_T_INT16:
  9254. x.type = 30;
  9255. break;
  9256. case MAT_T_UINT16:
  9257. x.type = 40;
  9258. break;
  9259. case MAT_T_UINT8:
  9260. x.type = 50;
  9261. break;
  9262. default:
  9263. return 2;
  9264. }
  9265. if ( mat->byteswap )
  9266. x.type += 1000;
  9267. x.namelen = (mat_int32_t)strlen(matvar->name) + 1;
  9268. switch ( matvar->class_type ) {
  9269. case MAT_C_CHAR:
  9270. x.type++;
  9271. /* Fall through */
  9272. case MAT_C_DOUBLE:
  9273. case MAT_C_SINGLE:
  9274. case MAT_C_INT32:
  9275. case MAT_C_INT16:
  9276. case MAT_C_UINT16:
  9277. case MAT_C_UINT8:
  9278. for ( i = 0; i < matvar->rank; i++ ) {
  9279. mat_int32_t dim;
  9280. dim = (mat_int32_t)matvar->dims[i];
  9281. nmemb *= dim;
  9282. }
  9283. x.mrows = (mat_int32_t)matvar->dims[0];
  9284. x.ncols = (mat_int32_t)matvar->dims[1];
  9285. x.imagf = matvar->isComplex ? 1 : 0;
  9286. fwrite(&x, sizeof(Fmatrix), 1, (FILE*)mat->fp);
  9287. fwrite(matvar->name, sizeof(char), x.namelen, (FILE*)mat->fp);
  9288. if (matvar->isComplex) {
  9289. mat_complex_split_t *complex_data;
  9290. complex_data = (mat_complex_split_t*)matvar->data;
  9291. fwrite(complex_data->Re, matvar->data_size, nmemb, (FILE*)mat->fp);
  9292. fwrite(complex_data->Im, matvar->data_size, nmemb, (FILE*)mat->fp);
  9293. }
  9294. else {
  9295. fwrite(matvar->data, matvar->data_size, nmemb, (FILE*)mat->fp);
  9296. }
  9297. break;
  9298. case MAT_C_SPARSE:
  9299. {
  9300. mat_sparse_t* sparse;
  9301. double tmp;
  9302. int i, j;
  9303. size_t stride = Mat_SizeOf(matvar->data_type);
  9304. #if !defined(EXTENDED_SPARSE)
  9305. if ( MAT_T_DOUBLE != matvar->data_type )
  9306. break;
  9307. #endif
  9308. sparse = (mat_sparse_t*)matvar->data;
  9309. x.type += 2;
  9310. x.mrows = sparse->njc > 0 ? sparse->jc[sparse->njc - 1] + 1 : 1;
  9311. x.ncols = matvar->isComplex ? 4 : 3;
  9312. x.imagf = 0;
  9313. fwrite(&x, sizeof(Fmatrix), 1, (FILE*)mat->fp);
  9314. fwrite(matvar->name, sizeof(char), x.namelen, (FILE*)mat->fp);
  9315. for ( i = 0; i < sparse->njc - 1; i++ ) {
  9316. for ( j = sparse->jc[i];
  9317. j < sparse->jc[i + 1] && j < sparse->ndata; j++ ) {
  9318. tmp = sparse->ir[j] + 1;
  9319. fwrite(&tmp, sizeof(double), 1, (FILE*)mat->fp);
  9320. }
  9321. }
  9322. tmp = matvar->dims[0];
  9323. fwrite(&tmp, sizeof(double), 1, (FILE*)mat->fp);
  9324. for ( i = 0; i < sparse->njc - 1; i++ ) {
  9325. for ( j = sparse->jc[i];
  9326. j < sparse->jc[i + 1] && j < sparse->ndata; j++ ) {
  9327. tmp = i + 1;
  9328. fwrite(&tmp, sizeof(double), 1, (FILE*)mat->fp);
  9329. }
  9330. }
  9331. tmp = matvar->dims[1];
  9332. fwrite(&tmp, sizeof(double), 1, (FILE*)mat->fp);
  9333. tmp = 0.;
  9334. if ( matvar->isComplex ) {
  9335. mat_complex_split_t *complex_data;
  9336. char* re, *im;
  9337. complex_data = (mat_complex_split_t*)sparse->data;
  9338. re = (char*)complex_data->Re;
  9339. im = (char*)complex_data->Im;
  9340. for ( i = 0; i < sparse->njc - 1; i++ ) {
  9341. for ( j = sparse->jc[i];
  9342. j < sparse->jc[i + 1] && j < sparse->ndata; j++ ) {
  9343. fwrite(re + j*stride, stride, 1, (FILE*)mat->fp);
  9344. }
  9345. }
  9346. fwrite(&tmp, stride, 1, (FILE*)mat->fp);
  9347. for ( i = 0; i < sparse->njc - 1; i++ ) {
  9348. for ( j = sparse->jc[i];
  9349. j < sparse->jc[i + 1] && j < sparse->ndata; j++ ) {
  9350. fwrite(im + j*stride, stride, 1, (FILE*)mat->fp);
  9351. }
  9352. }
  9353. } else {
  9354. char *data = (char*)sparse->data;
  9355. for ( i = 0; i < sparse->njc - 1; i++ ) {
  9356. for ( j = sparse->jc[i];
  9357. j < sparse->jc[i + 1] && j < sparse->ndata; j++ ) {
  9358. fwrite(data + j*stride, stride, 1, (FILE*)mat->fp);
  9359. }
  9360. }
  9361. }
  9362. fwrite(&tmp, stride, 1, (FILE*)mat->fp);
  9363. break;
  9364. }
  9365. default:
  9366. break;
  9367. }
  9368. return 0;
  9369. }
  9370. /** @if mat_devman
  9371. * @brief Reads the data of a version 4 MAT file variable
  9372. *
  9373. * @ingroup mat_internal
  9374. * @param mat MAT file pointer
  9375. * @param matvar MAT variable pointer to read the data
  9376. * @endif
  9377. */
  9378. static void
  9379. Read4(mat_t *mat,matvar_t *matvar)
  9380. {
  9381. unsigned int N;
  9382. (void)fseek((FILE*)mat->fp,matvar->internal->datapos,SEEK_SET);
  9383. N = matvar->dims[0]*matvar->dims[1];
  9384. switch ( matvar->class_type ) {
  9385. case MAT_C_DOUBLE:
  9386. matvar->data_size = sizeof(double);
  9387. matvar->nbytes = N*matvar->data_size;
  9388. if ( matvar->isComplex ) {
  9389. mat_complex_split_t *complex_data;
  9390. complex_data = (mat_complex_split_t*)malloc(sizeof(*complex_data));
  9391. if ( complex_data != NULL ) {
  9392. complex_data->Re = malloc(matvar->nbytes);
  9393. complex_data->Im = malloc(matvar->nbytes);
  9394. matvar->data = complex_data;
  9395. if ( complex_data->Re != NULL && complex_data->Im != NULL ) {
  9396. ReadDoubleData(mat, (double*)complex_data->Re, matvar->data_type, N);
  9397. ReadDoubleData(mat, (double*)complex_data->Im, matvar->data_type, N);
  9398. }
  9399. }
  9400. } else {
  9401. matvar->data = malloc(matvar->nbytes);
  9402. if ( matvar->data != NULL )
  9403. ReadDoubleData(mat, (double*)matvar->data, matvar->data_type, N);
  9404. }
  9405. /* Update data type to match format of matvar->data */
  9406. matvar->data_type = MAT_T_DOUBLE;
  9407. break;
  9408. case MAT_C_CHAR:
  9409. matvar->data_size = 1;
  9410. matvar->nbytes = N;
  9411. matvar->data = malloc(matvar->nbytes);
  9412. if ( NULL == matvar->data )
  9413. Mat_Critical("Memory allocation failure");
  9414. else
  9415. ReadUInt8Data(mat,(mat_uint8_t*)matvar->data,matvar->data_type,N);
  9416. matvar->data_type = MAT_T_UINT8;
  9417. break;
  9418. case MAT_C_SPARSE:
  9419. matvar->data_size = sizeof(mat_sparse_t);
  9420. matvar->data = malloc(matvar->data_size);
  9421. if ( matvar->data == NULL )
  9422. Mat_Critical("Memory allocation failure");
  9423. else {
  9424. double tmp;
  9425. int i;
  9426. mat_sparse_t* sparse;
  9427. int fpos;
  9428. enum matio_types data_type = MAT_T_DOUBLE;
  9429. /* matvar->dims[1] either is 3 for real or 4 for complex sparse */
  9430. matvar->isComplex = matvar->dims[1] == 4 ? 1 : 0;
  9431. sparse = (mat_sparse_t*)matvar->data;
  9432. sparse->nir = matvar->dims[0] - 1;
  9433. sparse->nzmax = sparse->nir;
  9434. sparse->ir = (mat_int32_t*)malloc(sparse->nir*sizeof(mat_int32_t));
  9435. if ( sparse->ir != NULL ) {
  9436. ReadInt32Data(mat, sparse->ir, data_type, sparse->nir);
  9437. for ( i = 0; i < sparse->nir; i++ )
  9438. sparse->ir[i] = sparse->ir[i] - 1;
  9439. } else {
  9440. free(matvar->data);
  9441. matvar->data = NULL;
  9442. Mat_Critical("Memory allocation failure");
  9443. return;
  9444. }
  9445. ReadDoubleData(mat, &tmp, data_type, 1);
  9446. matvar->dims[0] = tmp;
  9447. fpos = ftell((FILE*)mat->fp);
  9448. if ( fpos == -1L ) {
  9449. free(sparse->ir);
  9450. free(matvar->data);
  9451. matvar->data = NULL;
  9452. Mat_Critical("Couldn't determine file position");
  9453. return;
  9454. }
  9455. (void)fseek((FILE*)mat->fp,sparse->nir*Mat_SizeOf(data_type),
  9456. SEEK_CUR);
  9457. ReadDoubleData(mat, &tmp, data_type, 1);
  9458. if ( tmp > INT_MAX-1 || tmp < 0 ) {
  9459. free(sparse->ir);
  9460. free(matvar->data);
  9461. matvar->data = NULL;
  9462. Mat_Critical("Invalid column dimension for sparse matrix");
  9463. return;
  9464. }
  9465. matvar->dims[1] = tmp < 0 ? 0 : ( tmp > INT_MAX-1 ? INT_MAX-1 : (size_t)tmp );
  9466. (void)fseek((FILE*)mat->fp,fpos,SEEK_SET);
  9467. if ( matvar->dims[1] > INT_MAX-1 ) {
  9468. free(sparse->ir);
  9469. free(matvar->data);
  9470. matvar->data = NULL;
  9471. Mat_Critical("Invalid column dimension for sparse matrix");
  9472. return;
  9473. }
  9474. sparse->njc = (int)matvar->dims[1] + 1;
  9475. sparse->jc = (mat_int32_t*)malloc(sparse->njc*sizeof(mat_int32_t));
  9476. if ( sparse->jc != NULL ) {
  9477. mat_int32_t *jc;
  9478. jc = (mat_int32_t*)malloc(sparse->nir*sizeof(mat_int32_t));
  9479. if ( jc != NULL ) {
  9480. int j = 0;
  9481. sparse->jc[0] = 0;
  9482. ReadInt32Data(mat, jc, data_type, sparse->nir);
  9483. for ( i = 1; i < sparse->njc-1; i++ ) {
  9484. while ( j < sparse->nir && jc[j] <= i )
  9485. j++;
  9486. sparse->jc[i] = j;
  9487. }
  9488. free(jc);
  9489. /* terminating nnz */
  9490. sparse->jc[sparse->njc-1] = sparse->nir;
  9491. } else {
  9492. free(sparse->jc);
  9493. free(sparse->ir);
  9494. free(matvar->data);
  9495. matvar->data = NULL;
  9496. Mat_Critical("Memory allocation failure");
  9497. return;
  9498. }
  9499. } else {
  9500. free(sparse->ir);
  9501. free(matvar->data);
  9502. matvar->data = NULL;
  9503. Mat_Critical("Memory allocation failure");
  9504. return;
  9505. }
  9506. ReadDoubleData(mat, &tmp, data_type, 1);
  9507. sparse->ndata = sparse->nir;
  9508. data_type = matvar->data_type;
  9509. if ( matvar->isComplex ) {
  9510. mat_complex_split_t *complex_data;
  9511. complex_data = (mat_complex_split_t*)malloc(sizeof(*complex_data));
  9512. if ( complex_data != NULL ) {
  9513. complex_data->Re = malloc(sparse->ndata*Mat_SizeOf(data_type));
  9514. complex_data->Im = malloc(sparse->ndata*Mat_SizeOf(data_type));
  9515. sparse->data = complex_data;
  9516. if ( complex_data->Re != NULL && complex_data->Im != NULL ) {
  9517. #if defined(EXTENDED_SPARSE)
  9518. switch ( data_type ) {
  9519. case MAT_T_DOUBLE:
  9520. ReadDoubleData(mat, (double*)complex_data->Re,
  9521. data_type, sparse->ndata);
  9522. ReadDoubleData(mat, &tmp, data_type, 1);
  9523. ReadDoubleData(mat, (double*)complex_data->Im,
  9524. data_type, sparse->ndata);
  9525. ReadDoubleData(mat, &tmp, data_type, 1);
  9526. break;
  9527. case MAT_T_SINGLE:
  9528. {
  9529. float tmp2;
  9530. ReadSingleData(mat, (float*)complex_data->Re,
  9531. data_type, sparse->ndata);
  9532. ReadSingleData(mat, &tmp2, data_type, 1);
  9533. ReadSingleData(mat, (float*)complex_data->Im,
  9534. data_type, sparse->ndata);
  9535. ReadSingleData(mat, &tmp2, data_type, 1);
  9536. break;
  9537. }
  9538. case MAT_T_INT32:
  9539. {
  9540. mat_int32_t tmp2;
  9541. ReadInt32Data(mat, (mat_int32_t*)complex_data->Re,
  9542. data_type, sparse->ndata);
  9543. ReadInt32Data(mat, &tmp2, data_type, 1);
  9544. ReadInt32Data(mat, (mat_int32_t*)complex_data->Im,
  9545. data_type, sparse->ndata);
  9546. ReadInt32Data(mat, &tmp2, data_type, 1);
  9547. break;
  9548. }
  9549. case MAT_T_INT16:
  9550. {
  9551. mat_int16_t tmp2;
  9552. ReadInt16Data(mat, (mat_int16_t*)complex_data->Re,
  9553. data_type, sparse->ndata);
  9554. ReadInt16Data(mat, &tmp2, data_type, 1);
  9555. ReadInt16Data(mat, (mat_int16_t*)complex_data->Im,
  9556. data_type, sparse->ndata);
  9557. ReadInt16Data(mat, &tmp2, data_type, 1);
  9558. break;
  9559. }
  9560. case MAT_T_UINT16:
  9561. {
  9562. mat_uint16_t tmp2;
  9563. ReadUInt16Data(mat, (mat_uint16_t*)complex_data->Re,
  9564. data_type, sparse->ndata);
  9565. ReadUInt16Data(mat, &tmp2, data_type, 1);
  9566. ReadUInt16Data(mat, (mat_uint16_t*)complex_data->Im,
  9567. data_type, sparse->ndata);
  9568. ReadUInt16Data(mat, &tmp2, data_type, 1);
  9569. break;
  9570. }
  9571. case MAT_T_UINT8:
  9572. {
  9573. mat_uint8_t tmp2;
  9574. ReadUInt8Data(mat, (mat_uint8_t*)complex_data->Re,
  9575. data_type, sparse->ndata);
  9576. ReadUInt8Data(mat, &tmp2, data_type, 1);
  9577. ReadUInt8Data(mat, (mat_uint8_t*)complex_data->Im,
  9578. data_type, sparse->ndata);
  9579. ReadUInt8Data(mat, &tmp2, data_type, 1);
  9580. break;
  9581. }
  9582. default:
  9583. free(complex_data->Re);
  9584. free(complex_data->Im);
  9585. free(complex_data);
  9586. free(sparse->jc);
  9587. free(sparse->ir);
  9588. free(matvar->data);
  9589. matvar->data = NULL;
  9590. Mat_Critical("Read4: %d is not a supported data type for ",
  9591. "extended sparse", data_type);
  9592. return;
  9593. }
  9594. #else
  9595. ReadDoubleData(mat, (double*)complex_data->Re,
  9596. data_type, sparse->ndata);
  9597. ReadDoubleData(mat, &tmp, data_type, 1);
  9598. ReadDoubleData(mat, (double*)complex_data->Im,
  9599. data_type, sparse->ndata);
  9600. ReadDoubleData(mat, &tmp, data_type, 1);
  9601. #endif
  9602. }
  9603. }
  9604. } else {
  9605. sparse->data = malloc(sparse->ndata*Mat_SizeOf(data_type));
  9606. if ( sparse->data != NULL ) {
  9607. #if defined(EXTENDED_SPARSE)
  9608. switch ( data_type ) {
  9609. case MAT_T_DOUBLE:
  9610. ReadDoubleData(mat, (double*)sparse->data,
  9611. data_type, sparse->ndata);
  9612. ReadDoubleData(mat, &tmp, data_type, 1);
  9613. break;
  9614. case MAT_T_SINGLE:
  9615. {
  9616. float tmp2;
  9617. ReadSingleData(mat, (float*)sparse->data,
  9618. data_type, sparse->ndata);
  9619. ReadSingleData(mat, &tmp2, data_type, 1);
  9620. break;
  9621. }
  9622. case MAT_T_INT32:
  9623. {
  9624. mat_int32_t tmp2;
  9625. ReadInt32Data(mat, (mat_int32_t*)sparse->data,
  9626. data_type, sparse->ndata);
  9627. ReadInt32Data(mat, &tmp2, data_type, 1);
  9628. break;
  9629. }
  9630. case MAT_T_INT16:
  9631. {
  9632. mat_int16_t tmp2;
  9633. ReadInt16Data(mat, (mat_int16_t*)sparse->data,
  9634. data_type, sparse->ndata);
  9635. ReadInt16Data(mat, &tmp2, data_type, 1);
  9636. break;
  9637. }
  9638. case MAT_T_UINT16:
  9639. {
  9640. mat_uint16_t tmp2;
  9641. ReadUInt16Data(mat, (mat_uint16_t*)sparse->data,
  9642. data_type, sparse->ndata);
  9643. ReadUInt16Data(mat, &tmp2, data_type, 1);
  9644. break;
  9645. }
  9646. case MAT_T_UINT8:
  9647. {
  9648. mat_uint8_t tmp2;
  9649. ReadUInt8Data(mat, (mat_uint8_t*)sparse->data,
  9650. data_type, sparse->ndata);
  9651. ReadUInt8Data(mat, &tmp2, data_type, 1);
  9652. break;
  9653. }
  9654. default:
  9655. free(sparse->data);
  9656. free(sparse->jc);
  9657. free(sparse->ir);
  9658. free(matvar->data);
  9659. matvar->data = NULL;
  9660. Mat_Critical("Read4: %d is not a supported data type for ",
  9661. "extended sparse", data_type);
  9662. return;
  9663. }
  9664. #else
  9665. ReadDoubleData(mat, (double*)sparse->data, data_type, sparse->ndata);
  9666. ReadDoubleData(mat, &tmp, data_type, 1);
  9667. #endif
  9668. } else {
  9669. free(sparse->jc);
  9670. free(sparse->ir);
  9671. free(matvar->data);
  9672. matvar->data = NULL;
  9673. Mat_Critical("Memory allocation failure");
  9674. return;
  9675. }
  9676. }
  9677. break;
  9678. }
  9679. default:
  9680. Mat_Critical("MAT V4 data type error");
  9681. return;
  9682. }
  9683. return;
  9684. }
  9685. /** @if mat_devman
  9686. * @brief Reads a slab of data from a version 4 MAT file for the @c matvar variable
  9687. *
  9688. * @ingroup mat_internal
  9689. * @param mat Version 4 MAT file pointer
  9690. * @param matvar pointer to the mat variable
  9691. * @param data pointer to store the read data in (must be of size
  9692. * edge[0]*...edge[rank-1]*Mat_SizeOfClass(matvar->class_type))
  9693. * @param start index to start reading data in each dimension
  9694. * @param stride write data every @c stride elements in each dimension
  9695. * @param edge number of elements to read in each dimension
  9696. * @retval 0 on success
  9697. * @endif
  9698. */
  9699. static int
  9700. ReadData4(mat_t *mat,matvar_t *matvar,void *data,
  9701. int *start,int *stride,int *edge)
  9702. {
  9703. int err = 0;
  9704. enum matio_classes class_type = MAT_C_EMPTY;
  9705. (void)fseek((FILE*)mat->fp,matvar->internal->datapos,SEEK_SET);
  9706. switch( matvar->data_type ) {
  9707. case MAT_T_DOUBLE:
  9708. class_type = MAT_C_DOUBLE;
  9709. break;
  9710. case MAT_T_SINGLE:
  9711. class_type = MAT_C_SINGLE;
  9712. break;
  9713. case MAT_T_INT32:
  9714. class_type = MAT_C_INT32;
  9715. break;
  9716. case MAT_T_INT16:
  9717. class_type = MAT_C_INT16;
  9718. break;
  9719. case MAT_T_UINT16:
  9720. class_type = MAT_C_UINT16;
  9721. break;
  9722. case MAT_T_UINT8:
  9723. class_type = MAT_C_UINT8;
  9724. break;
  9725. default:
  9726. return 1;
  9727. }
  9728. if ( matvar->rank == 2 ) {
  9729. if ( stride[0]*(edge[0]-1)+start[0]+1 > matvar->dims[0] )
  9730. err = 1;
  9731. else if ( stride[1]*(edge[1]-1)+start[1]+1 > matvar->dims[1] )
  9732. err = 1;
  9733. if ( matvar->isComplex ) {
  9734. mat_complex_split_t *cdata = (mat_complex_split_t*)data;
  9735. int nbytes = edge[0]*edge[1]*Mat_SizeOf(matvar->data_type);
  9736. ReadDataSlab2(mat,cdata->Re,class_type,matvar->data_type,
  9737. matvar->dims,start,stride,edge);
  9738. (void)fseek((FILE*)mat->fp,matvar->internal->datapos+nbytes,SEEK_SET);
  9739. ReadDataSlab2(mat,cdata->Im,class_type,
  9740. matvar->data_type,matvar->dims,start,stride,edge);
  9741. } else {
  9742. ReadDataSlab2(mat,data,class_type,matvar->data_type,
  9743. matvar->dims,start,stride,edge);
  9744. }
  9745. } else if ( matvar->isComplex ) {
  9746. int i;
  9747. mat_complex_split_t *cdata = (mat_complex_split_t*)data;
  9748. int nbytes = Mat_SizeOf(matvar->data_type);
  9749. for ( i = 0; i < matvar->rank; i++ )
  9750. nbytes *= edge[i];
  9751. ReadDataSlabN(mat,cdata->Re,class_type,matvar->data_type,
  9752. matvar->rank,matvar->dims,start,stride,edge);
  9753. (void)fseek((FILE*)mat->fp,matvar->internal->datapos+nbytes,SEEK_SET);
  9754. ReadDataSlabN(mat,cdata->Im,class_type,matvar->data_type,
  9755. matvar->rank,matvar->dims,start,stride,edge);
  9756. } else {
  9757. ReadDataSlabN(mat,data,class_type,matvar->data_type,
  9758. matvar->rank,matvar->dims,start,stride,edge);
  9759. }
  9760. return err;
  9761. }
  9762. /** @brief Reads a subset of a MAT variable using a 1-D indexing
  9763. *
  9764. * Reads data from a MAT variable using a linear (1-D) indexing mode. The
  9765. * variable must have been read by Mat_VarReadInfo.
  9766. * @ingroup MAT
  9767. * @param mat MAT file to read data from
  9768. * @param matvar MAT variable information
  9769. * @param data pointer to store data in (must be pre-allocated)
  9770. * @param start starting index
  9771. * @param stride stride of data
  9772. * @param edge number of elements to read
  9773. * @retval 0 on success
  9774. */
  9775. static int
  9776. Mat_VarReadDataLinear4(mat_t *mat,matvar_t *matvar,void *data,int start,
  9777. int stride,int edge)
  9778. {
  9779. size_t i, nmemb = 1;
  9780. int err = 0;
  9781. (void)fseek((FILE*)mat->fp,matvar->internal->datapos,SEEK_SET);
  9782. matvar->data_size = Mat_SizeOf(matvar->data_type);
  9783. for ( i = 0; i < matvar->rank; i++ )
  9784. nmemb *= matvar->dims[i];
  9785. if ( stride*(edge-1)+start+1 > nmemb ) {
  9786. return 1;
  9787. }
  9788. if ( matvar->isComplex ) {
  9789. mat_complex_split_t *complex_data = (mat_complex_split_t*)data;
  9790. int nbytes = nmemb*matvar->data_size;
  9791. ReadDataSlab1(mat,complex_data->Re,matvar->class_type,
  9792. matvar->data_type,start,stride,edge);
  9793. (void)fseek((FILE*)mat->fp,matvar->internal->datapos+nbytes,SEEK_SET);
  9794. ReadDataSlab1(mat,complex_data->Im,matvar->class_type,
  9795. matvar->data_type,start,stride,edge);
  9796. } else {
  9797. ReadDataSlab1(mat,data,matvar->class_type,matvar->data_type,start,
  9798. stride,edge);
  9799. }
  9800. return err;
  9801. }
  9802. /** @if mat_devman
  9803. * @brief Reads the header information for the next MAT variable in a version 4 MAT file
  9804. *
  9805. * @ingroup mat_internal
  9806. * @param mat MAT file pointer
  9807. * @return pointer to the MAT variable or NULL
  9808. * @endif
  9809. */
  9810. static matvar_t *
  9811. Mat_VarReadNextInfo4(mat_t *mat)
  9812. {
  9813. int tmp,M,O,data_type,class_type;
  9814. int nBytes;
  9815. size_t err;
  9816. matvar_t *matvar = NULL;
  9817. union {
  9818. mat_uint32_t u;
  9819. mat_uint8_t c[4];
  9820. } endian;
  9821. if ( mat == NULL || mat->fp == NULL )
  9822. return NULL;
  9823. else if ( NULL == (matvar = Mat_VarCalloc()) )
  9824. return NULL;
  9825. matvar->internal->fp = mat;
  9826. matvar->internal->fpos = ftell((FILE*)mat->fp);
  9827. if ( matvar->internal->fpos == -1L ) {
  9828. Mat_VarFree(matvar);
  9829. Mat_Critical("Couldn't determine file position");
  9830. return NULL;
  9831. }
  9832. err = fread(&tmp,sizeof(int),1,(FILE*)mat->fp);
  9833. if ( !err ) {
  9834. Mat_VarFree(matvar);
  9835. return NULL;
  9836. }
  9837. endian.u = 0x01020304;
  9838. /* See if MOPT may need byteswapping */
  9839. if ( tmp < 0 || tmp > 4052 ) {
  9840. if ( Mat_int32Swap(&tmp) > 4052 ) {
  9841. Mat_VarFree(matvar);
  9842. return NULL;
  9843. }
  9844. }
  9845. M = floor(tmp / 1000.0);
  9846. tmp -= M*1000;
  9847. O = floor(tmp / 100.0);
  9848. tmp -= O*100;
  9849. data_type = floor(tmp / 10.0);
  9850. tmp -= data_type*10;
  9851. class_type = floor(tmp / 1.0);
  9852. switch ( M ) {
  9853. case 0:
  9854. /* IEEE little endian */
  9855. mat->byteswap = (endian.c[0] != 4);
  9856. break;
  9857. case 1:
  9858. /* IEEE big endian */
  9859. mat->byteswap = (endian.c[0] != 1);
  9860. break;
  9861. default:
  9862. /* VAX, Cray, or bogus */
  9863. Mat_VarFree(matvar);
  9864. return NULL;
  9865. }
  9866. /* O must be zero */
  9867. if ( 0 != O ) {
  9868. Mat_VarFree(matvar);
  9869. return NULL;
  9870. }
  9871. /* Convert the V4 data type */
  9872. switch ( data_type ) {
  9873. case 0:
  9874. matvar->data_type = MAT_T_DOUBLE;
  9875. break;
  9876. case 1:
  9877. matvar->data_type = MAT_T_SINGLE;
  9878. break;
  9879. case 2:
  9880. matvar->data_type = MAT_T_INT32;
  9881. break;
  9882. case 3:
  9883. matvar->data_type = MAT_T_INT16;
  9884. break;
  9885. case 4:
  9886. matvar->data_type = MAT_T_UINT16;
  9887. break;
  9888. case 5:
  9889. matvar->data_type = MAT_T_UINT8;
  9890. break;
  9891. default:
  9892. Mat_VarFree(matvar);
  9893. return NULL;
  9894. }
  9895. switch ( class_type ) {
  9896. case 0:
  9897. matvar->class_type = MAT_C_DOUBLE;
  9898. break;
  9899. case 1:
  9900. matvar->class_type = MAT_C_CHAR;
  9901. break;
  9902. case 2:
  9903. matvar->class_type = MAT_C_SPARSE;
  9904. break;
  9905. default:
  9906. Mat_VarFree(matvar);
  9907. return NULL;
  9908. }
  9909. matvar->rank = 2;
  9910. matvar->dims = (size_t*)malloc(2*sizeof(*matvar->dims));
  9911. if ( NULL == matvar->dims ) {
  9912. Mat_VarFree(matvar);
  9913. return NULL;
  9914. }
  9915. err = fread(&tmp,sizeof(int),1,(FILE*)mat->fp);
  9916. if ( mat->byteswap )
  9917. Mat_int32Swap(&tmp);
  9918. matvar->dims[0] = tmp;
  9919. if ( !err ) {
  9920. Mat_VarFree(matvar);
  9921. return NULL;
  9922. }
  9923. err = fread(&tmp,sizeof(int),1,(FILE*)mat->fp);
  9924. if ( mat->byteswap )
  9925. Mat_int32Swap(&tmp);
  9926. matvar->dims[1] = tmp;
  9927. if ( !err ) {
  9928. Mat_VarFree(matvar);
  9929. return NULL;
  9930. }
  9931. err = fread(&(matvar->isComplex),sizeof(int),1,(FILE*)mat->fp);
  9932. if ( !err ) {
  9933. Mat_VarFree(matvar);
  9934. return NULL;
  9935. }
  9936. err = fread(&tmp,sizeof(int),1,(FILE*)mat->fp);
  9937. if ( !err ) {
  9938. Mat_VarFree(matvar);
  9939. return NULL;
  9940. }
  9941. if ( mat->byteswap )
  9942. Mat_int32Swap(&tmp);
  9943. /* Check that the length of the variable name is at least 1 */
  9944. if ( tmp < 1 ) {
  9945. Mat_VarFree(matvar);
  9946. return NULL;
  9947. }
  9948. matvar->name = (char*)malloc(tmp);
  9949. if ( NULL == matvar->name ) {
  9950. Mat_VarFree(matvar);
  9951. return NULL;
  9952. }
  9953. err = fread(matvar->name,1,tmp,(FILE*)mat->fp);
  9954. if ( !err ) {
  9955. Mat_VarFree(matvar);
  9956. return NULL;
  9957. }
  9958. matvar->internal->datapos = ftell((FILE*)mat->fp);
  9959. if ( matvar->internal->datapos == -1L ) {
  9960. Mat_VarFree(matvar);
  9961. Mat_Critical("Couldn't determine file position");
  9962. return NULL;
  9963. }
  9964. nBytes = matvar->dims[0]*matvar->dims[1]*Mat_SizeOf(matvar->data_type);
  9965. if ( matvar->isComplex )
  9966. nBytes *= 2;
  9967. (void)fseek((FILE*)mat->fp,nBytes,SEEK_CUR);
  9968. return matvar;
  9969. }
  9970. /* -------------------------------
  9971. * ---------- mat5.c
  9972. * -------------------------------
  9973. */
  9974. /** @file mat5.c
  9975. * Matlab MAT version 5 file functions
  9976. * @ingroup MAT
  9977. */
  9978. /* FIXME: Implement Unicode support */
  9979. #include <stdlib.h>
  9980. #include <string.h>
  9981. #include <stdio.h>
  9982. #include <math.h>
  9983. #include <time.h>
  9984. /** Get type from tag */
  9985. #define TYPE_FROM_TAG(a) (enum matio_types)((a) & 0x000000ff)
  9986. /** Get class from array flag */
  9987. #define CLASS_FROM_ARRAY_FLAGS(a) (enum matio_classes)((a) & 0x000000ff)
  9988. /** Class type mask */
  9989. #define CLASS_TYPE_MASK 0x000000ff
  9990. static mat_complex_split_t null_complex_data = {NULL,NULL};
  9991. /*
  9992. * -------------------------------------------------------------
  9993. * Private Functions
  9994. * -------------------------------------------------------------
  9995. */
  9996. /** @brief determines the number of bytes needed to store the given struct field
  9997. *
  9998. * @ingroup mat_internal
  9999. * @param matvar field of a structure
  10000. * @return the number of bytes needed to store the struct field
  10001. */
  10002. static size_t
  10003. GetStructFieldBufSize(matvar_t *matvar)
  10004. {
  10005. size_t nBytes = 0, data_bytes = 0;
  10006. size_t tag_size = 8, array_flags_size = 8;
  10007. int nmemb = 1, i;
  10008. if ( matvar == NULL )
  10009. return nBytes;
  10010. /* Add the Array Flags tag and space to the number of bytes */
  10011. nBytes += tag_size + array_flags_size;
  10012. /* In a struct field, the name is just a tag with 0 bytes */
  10013. nBytes += tag_size;
  10014. /* Add rank and dimensions, padded to an 8 byte block */
  10015. for ( i = 0; i < matvar->rank; i++ )
  10016. nmemb *= matvar->dims[i];
  10017. if ( matvar->rank % 2 )
  10018. nBytes += tag_size + matvar->rank*4 + 4;
  10019. else
  10020. nBytes += tag_size + matvar->rank*4;
  10021. switch ( matvar->class_type ) {
  10022. case MAT_C_STRUCT:
  10023. {
  10024. matvar_t **fields = (matvar_t**)matvar->data;
  10025. int i, nfields = 0;
  10026. size_t maxlen = 0;
  10027. nfields = matvar->internal->num_fields;
  10028. for ( i = 0; i < nfields; i++ ) {
  10029. char *fieldname = matvar->internal->fieldnames[i];
  10030. if ( NULL != fieldname && strlen(fieldname) > maxlen )
  10031. maxlen = strlen(fieldname);
  10032. }
  10033. maxlen++;
  10034. while ( nfields*maxlen % 8 != 0 )
  10035. maxlen++;
  10036. nBytes += tag_size + tag_size + maxlen*nfields;
  10037. /* FIXME: Add bytes for the fieldnames */
  10038. if ( NULL != fields && nfields > 0 ) {
  10039. for ( i = 0; i < nfields*nmemb; i++ )
  10040. nBytes += tag_size + GetStructFieldBufSize(fields[i]);
  10041. }
  10042. break;
  10043. }
  10044. case MAT_C_CELL:
  10045. {
  10046. matvar_t **cells = (matvar_t**)matvar->data;
  10047. int i, ncells;
  10048. if ( matvar->nbytes == 0 || matvar->data_size == 0 )
  10049. break;
  10050. ncells = matvar->nbytes / matvar->data_size;
  10051. if ( NULL != cells && ncells > 0 ) {
  10052. for ( i = 0; i < ncells; i++ )
  10053. nBytes += tag_size + GetCellArrayFieldBufSize(cells[i]);
  10054. }
  10055. break;
  10056. }
  10057. case MAT_C_SPARSE:
  10058. {
  10059. mat_sparse_t *sparse = (mat_sparse_t*)matvar->data;
  10060. data_bytes = sparse->nir*sizeof(mat_int32_t);
  10061. if ( data_bytes % 8 )
  10062. data_bytes += (8 - (data_bytes % 8));
  10063. nBytes += tag_size + data_bytes;
  10064. data_bytes = sparse->njc*sizeof(mat_int32_t);
  10065. if ( data_bytes % 8 )
  10066. data_bytes += (8 - (data_bytes % 8));
  10067. nBytes += tag_size + data_bytes;
  10068. data_bytes = sparse->ndata*Mat_SizeOf(matvar->data_type);
  10069. if ( data_bytes % 8 )
  10070. data_bytes += (8 - (data_bytes % 8));
  10071. nBytes += tag_size + data_bytes;
  10072. if ( matvar->isComplex )
  10073. nBytes += tag_size + data_bytes;
  10074. break;
  10075. }
  10076. case MAT_C_CHAR:
  10077. if ( MAT_T_UINT8 == matvar->data_type ||
  10078. MAT_T_INT8 == matvar->data_type )
  10079. data_bytes = nmemb*Mat_SizeOf(MAT_T_UINT16);
  10080. else
  10081. data_bytes = nmemb*Mat_SizeOf(matvar->data_type);
  10082. if ( data_bytes % 8 )
  10083. data_bytes += (8 - (data_bytes % 8));
  10084. nBytes += tag_size + data_bytes;
  10085. if ( matvar->isComplex )
  10086. nBytes += tag_size + data_bytes;
  10087. break;
  10088. default:
  10089. data_bytes = nmemb*Mat_SizeOf(matvar->data_type);
  10090. if ( data_bytes % 8 )
  10091. data_bytes += (8 - (data_bytes % 8));
  10092. nBytes += tag_size + data_bytes;
  10093. if ( matvar->isComplex )
  10094. nBytes += tag_size + data_bytes;
  10095. } /* switch ( matvar->class_type ) */
  10096. return nBytes;
  10097. }
  10098. /** @brief determines the number of bytes needed to store the cell array element
  10099. *
  10100. * @ingroup mat_internal
  10101. * @param matvar MAT variable
  10102. * @return the number of bytes needed to store the variable
  10103. */
  10104. static size_t
  10105. GetCellArrayFieldBufSize(matvar_t *matvar)
  10106. {
  10107. size_t nBytes = 0, data_bytes;
  10108. size_t tag_size = 8, array_flags_size = 8;
  10109. int nmemb = 1, i;
  10110. if ( matvar == NULL )
  10111. return nBytes;
  10112. /* Add the Array Flags tag and space to the number of bytes */
  10113. nBytes += tag_size + array_flags_size;
  10114. /* In an element of a cell array, the name is just a tag with 0 bytes */
  10115. nBytes += tag_size;
  10116. /* Add rank and dimensions, padded to an 8 byte block */
  10117. for ( i = 0; i < matvar->rank; i++ )
  10118. nmemb *= matvar->dims[i];
  10119. if ( matvar->rank % 2 )
  10120. nBytes += tag_size + matvar->rank*4 + 4;
  10121. else
  10122. nBytes += tag_size + matvar->rank*4;
  10123. switch ( matvar->class_type ) {
  10124. case MAT_C_STRUCT:
  10125. {
  10126. matvar_t **fields = (matvar_t**)matvar->data;
  10127. int i, nfields = 0;
  10128. size_t maxlen = 0;
  10129. nfields = matvar->internal->num_fields;
  10130. for ( i = 0; i < nfields; i++ ) {
  10131. char *fieldname = matvar->internal->fieldnames[i];
  10132. if ( NULL != fieldname && strlen(fieldname) > maxlen )
  10133. maxlen = strlen(fieldname);
  10134. }
  10135. maxlen++;
  10136. while ( nfields*maxlen % 8 != 0 )
  10137. maxlen++;
  10138. nBytes += tag_size + tag_size + maxlen*nfields;
  10139. if ( NULL != fields && nfields > 0 ) {
  10140. for ( i = 0; i < nfields*nmemb; i++ )
  10141. nBytes += tag_size + GetStructFieldBufSize(fields[i]);
  10142. }
  10143. break;
  10144. }
  10145. case MAT_C_CELL:
  10146. {
  10147. matvar_t **cells = (matvar_t**)matvar->data;
  10148. int i, ncells;
  10149. if ( matvar->nbytes == 0 || matvar->data_size == 0 )
  10150. break;
  10151. ncells = matvar->nbytes / matvar->data_size;
  10152. if ( NULL != cells && ncells > 0 ) {
  10153. for ( i = 0; i < ncells; i++ )
  10154. nBytes += tag_size + GetCellArrayFieldBufSize(cells[i]);
  10155. }
  10156. break;
  10157. }
  10158. case MAT_C_SPARSE:
  10159. {
  10160. mat_sparse_t *sparse = (mat_sparse_t*)matvar->data;
  10161. data_bytes = sparse->nir*sizeof(mat_int32_t);
  10162. if ( data_bytes % 8 )
  10163. data_bytes += (8 - (data_bytes % 8));
  10164. nBytes += tag_size + data_bytes;
  10165. data_bytes = sparse->njc*sizeof(mat_int32_t);
  10166. if ( data_bytes % 8 )
  10167. data_bytes += (8 - (data_bytes % 8));
  10168. nBytes += tag_size + data_bytes;
  10169. data_bytes = sparse->ndata*Mat_SizeOf(matvar->data_type);
  10170. if ( data_bytes % 8 )
  10171. data_bytes += (8 - (data_bytes % 8));
  10172. nBytes += tag_size + data_bytes;
  10173. if ( matvar->isComplex )
  10174. nBytes += tag_size + data_bytes;
  10175. break;
  10176. }
  10177. case MAT_C_CHAR:
  10178. if ( MAT_T_UINT8 == matvar->data_type ||
  10179. MAT_T_INT8 == matvar->data_type )
  10180. data_bytes = nmemb*Mat_SizeOf(MAT_T_UINT16);
  10181. else
  10182. data_bytes = nmemb*Mat_SizeOf(matvar->data_type);
  10183. if ( data_bytes % 8 )
  10184. data_bytes += (8 - (data_bytes % 8));
  10185. nBytes += tag_size + data_bytes;
  10186. if ( matvar->isComplex )
  10187. nBytes += tag_size + data_bytes;
  10188. break;
  10189. default:
  10190. data_bytes = nmemb*Mat_SizeOf(matvar->data_type);
  10191. if ( data_bytes % 8 )
  10192. data_bytes += (8 - (data_bytes % 8));
  10193. nBytes += tag_size + data_bytes;
  10194. if ( matvar->isComplex )
  10195. nBytes += tag_size + data_bytes;
  10196. } /* switch ( matvar->class_type ) */
  10197. return nBytes;
  10198. }
  10199. #if defined(HAVE_ZLIB)
  10200. /** @brief determines the number of bytes needed to store the given variable
  10201. *
  10202. * @ingroup mat_internal
  10203. * @param matvar MAT variable
  10204. * @return the number of bytes needed to store the variable
  10205. */
  10206. static size_t
  10207. GetEmptyMatrixMaxBufSize(const char *name,int rank)
  10208. {
  10209. size_t nBytes = 0, len;
  10210. size_t tag_size = 8, array_flags_size = 8;
  10211. /* Add the Array Flags tag and space to the number of bytes */
  10212. nBytes += tag_size + array_flags_size;
  10213. /* Get size of variable name, pad it to an 8 byte block, and add it to nBytes */
  10214. if ( NULL != name )
  10215. len = strlen(name);
  10216. else
  10217. len = 4;
  10218. if ( len <= 4 ) {
  10219. nBytes += tag_size;
  10220. } else {
  10221. if ( len % 8 )
  10222. len = len + (8 - len % 8);
  10223. nBytes += tag_size + len;
  10224. }
  10225. /* Add rank and dimensions, padded to an 8 byte block */
  10226. if ( rank % 2 )
  10227. nBytes += tag_size + rank*4 + 4;
  10228. else
  10229. nBytes += tag_size + rank*4;
  10230. /* Data tag */
  10231. nBytes += tag_size;
  10232. return nBytes;
  10233. }
  10234. /** @brief determines the number of bytes needed to store the given variable
  10235. *
  10236. * @ingroup mat_internal
  10237. * @param matvar MAT variable
  10238. * @return the number of bytes needed to store the variable
  10239. */
  10240. static size_t
  10241. GetMatrixMaxBufSize(matvar_t *matvar)
  10242. {
  10243. size_t nBytes = 0, len, data_bytes;
  10244. size_t tag_size = 8, array_flags_size = 8;
  10245. int nmemb = 1, i;
  10246. if ( matvar == NULL )
  10247. return nBytes;
  10248. /* Add the Array Flags tag and space to the number of bytes */
  10249. nBytes += tag_size + array_flags_size;
  10250. /* Get size of variable name, pad it to an 8 byte block, and add it to nBytes */
  10251. if ( NULL != matvar->name )
  10252. len = strlen(matvar->name);
  10253. else
  10254. len=4;
  10255. if ( len <= 4 ) {
  10256. nBytes += tag_size;
  10257. } else {
  10258. if ( len % 8 )
  10259. len = len + (8 - len % 8);
  10260. nBytes += tag_size + len;
  10261. }
  10262. /* Add rank and dimensions, padded to an 8 byte block */
  10263. for ( i = 0, len = 0; i < matvar->rank; i++ )
  10264. nmemb *= matvar->dims[i];
  10265. if ( matvar->rank % 2 )
  10266. nBytes += tag_size + matvar->rank*4 + 4;
  10267. else
  10268. nBytes += tag_size + matvar->rank*4;
  10269. switch ( matvar->class_type ) {
  10270. case MAT_C_STRUCT:
  10271. {
  10272. matvar_t **fields = (matvar_t**)matvar->data;
  10273. int i, nfields = 0;
  10274. size_t maxlen = 0;
  10275. nfields = matvar->internal->num_fields;
  10276. for ( i = 0; i < nfields; i++ ) {
  10277. char *fieldname = matvar->internal->fieldnames[i];
  10278. if ( NULL != fieldname && strlen(fieldname) > maxlen )
  10279. maxlen = strlen(fieldname);
  10280. }
  10281. maxlen++;
  10282. while ( nfields*maxlen % 8 != 0 )
  10283. maxlen++;
  10284. nBytes += tag_size + tag_size + maxlen*nfields;
  10285. /* FIXME: Add bytes for the fieldnames */
  10286. if ( NULL != fields && nfields > 0 ) {
  10287. for ( i = 0; i < nfields*nmemb; i++ )
  10288. nBytes += tag_size + GetStructFieldBufSize(fields[i]);
  10289. }
  10290. break;
  10291. }
  10292. case MAT_C_CELL:
  10293. {
  10294. matvar_t **cells = (matvar_t**)matvar->data;
  10295. int i, ncells;
  10296. if ( matvar->nbytes == 0 || matvar->data_size == 0 )
  10297. break;
  10298. ncells = matvar->nbytes / matvar->data_size;
  10299. if ( NULL != cells && ncells > 0 ) {
  10300. for ( i = 0; i < ncells; i++ )
  10301. nBytes += tag_size + GetCellArrayFieldBufSize(cells[i]);
  10302. }
  10303. break;
  10304. }
  10305. case MAT_C_SPARSE:
  10306. {
  10307. mat_sparse_t *sparse = (mat_sparse_t*)matvar->data;
  10308. data_bytes = sparse->nir*sizeof(mat_int32_t);
  10309. if ( data_bytes % 8 )
  10310. data_bytes += (8 - (data_bytes % 8));
  10311. nBytes += tag_size + data_bytes;
  10312. data_bytes = sparse->njc*sizeof(mat_int32_t);
  10313. if ( data_bytes % 8 )
  10314. data_bytes += (8 - (data_bytes % 8));
  10315. nBytes += tag_size + data_bytes;
  10316. data_bytes = sparse->ndata*Mat_SizeOf(matvar->data_type);
  10317. if ( data_bytes % 8 )
  10318. data_bytes += (8 - (data_bytes % 8));
  10319. nBytes += tag_size + data_bytes;
  10320. if ( matvar->isComplex )
  10321. nBytes += tag_size + data_bytes;
  10322. break;
  10323. }
  10324. case MAT_C_CHAR:
  10325. if ( MAT_T_UINT8 == matvar->data_type ||
  10326. MAT_T_INT8 == matvar->data_type )
  10327. data_bytes = nmemb*Mat_SizeOf(MAT_T_UINT16);
  10328. else
  10329. data_bytes = nmemb*Mat_SizeOf(matvar->data_type);
  10330. if ( data_bytes % 8 )
  10331. data_bytes += (8 - (data_bytes % 8));
  10332. nBytes += tag_size + data_bytes;
  10333. if ( matvar->isComplex )
  10334. nBytes += tag_size + data_bytes;
  10335. break;
  10336. default:
  10337. data_bytes = nmemb*Mat_SizeOf(matvar->data_type);
  10338. if ( data_bytes % 8 )
  10339. data_bytes += (8 - (data_bytes % 8));
  10340. nBytes += tag_size + data_bytes;
  10341. if ( matvar->isComplex )
  10342. nBytes += tag_size + data_bytes;
  10343. } /* switch ( matvar->class_type ) */
  10344. return nBytes;
  10345. }
  10346. #endif
  10347. /** @if mat_devman
  10348. * @brief Creates a new Matlab MAT version 5 file
  10349. *
  10350. * Tries to create a new Matlab MAT file with the given name and optional
  10351. * header string. If no header string is given, the default string
  10352. * is used containing the software, version, and date in it. If a header
  10353. * string is given, at most the first 116 characters is written to the file.
  10354. * The given header string need not be the full 116 characters, but MUST be
  10355. * NULL terminated.
  10356. * @ingroup MAT
  10357. * @param matname Name of MAT file to create
  10358. * @param hdr_str Optional header string, NULL to use default
  10359. * @return A pointer to the MAT file or NULL if it failed. This is not a
  10360. * simple FILE * and should not be used as one.
  10361. * @endif
  10362. */
  10363. static mat_t *
  10364. Mat_Create5(const char *matname,const char *hdr_str)
  10365. {
  10366. FILE *fp = NULL;
  10367. mat_int16_t endian = 0, version;
  10368. mat_t *mat = NULL;
  10369. size_t err;
  10370. time_t t;
  10371. fp = fopen(matname,"wb");
  10372. if ( !fp )
  10373. return NULL;
  10374. mat = (mat_t*)malloc(sizeof(*mat));
  10375. if ( mat == NULL ) {
  10376. fclose(fp);
  10377. return NULL;
  10378. }
  10379. mat->fp = NULL;
  10380. mat->header = NULL;
  10381. mat->subsys_offset = NULL;
  10382. mat->filename = NULL;
  10383. mat->version = 0;
  10384. mat->byteswap = 0;
  10385. mat->mode = 0;
  10386. mat->bof = 0;
  10387. mat->next_index = 0;
  10388. t = time(NULL);
  10389. mat->fp = fp;
  10390. mat->filename = strdup_printf("%s",matname);
  10391. mat->mode = MAT_ACC_RDWR;
  10392. mat->byteswap = 0;
  10393. mat->header = (char*)malloc(128*sizeof(char));
  10394. mat->subsys_offset = (char*)malloc(8*sizeof(char));
  10395. memset(mat->header,' ',128);
  10396. if ( hdr_str == NULL ) {
  10397. err = mat_snprintf(mat->header,116,"MATLAB 5.0 MAT-file, Platform: %s, "
  10398. "Created by: libmatio v%d.%d.%d on %s", MATIO_PLATFORM,
  10399. MATIO_MAJOR_VERSION, MATIO_MINOR_VERSION, MATIO_RELEASE_LEVEL,
  10400. ctime(&t));
  10401. } else {
  10402. err = mat_snprintf(mat->header,116,"%s",hdr_str);
  10403. }
  10404. if ( err >= 116 )
  10405. mat->header[115] = '\0'; /* Just to make sure it's NULL terminated */
  10406. memset(mat->subsys_offset,' ',8);
  10407. mat->version = (int)0x0100;
  10408. endian = 0x4d49;
  10409. version = 0x0100;
  10410. err = fwrite(mat->header,1,116,(FILE*)mat->fp);
  10411. err = fwrite(mat->subsys_offset,1,8,(FILE*)mat->fp);
  10412. err = fwrite(&version,2,1,(FILE*)mat->fp);
  10413. err = fwrite(&endian,2,1,(FILE*)mat->fp);
  10414. return mat;
  10415. }
  10416. /** @if mat_devman
  10417. * @brief Writes @c data as character data
  10418. *
  10419. * This function uses the knowledge that the data is part of a character class
  10420. * to avoid some pitfalls with Matlab listed below.
  10421. * @li Matlab character data cannot be unsigned 8-bit integers, it needs at
  10422. * least unsigned 16-bit integers
  10423. *
  10424. * @ingroup mat_internal
  10425. * @param mat MAT file pointer
  10426. * @param data character data to write
  10427. * @param N Number of elements to write
  10428. * @param data_type character data type (enum matio_types)
  10429. * @return number of bytes written
  10430. * @endif
  10431. */
  10432. static size_t
  10433. WriteCharData(mat_t *mat, void *data, int N,enum matio_types data_type)
  10434. {
  10435. int nBytes = 0, i;
  10436. size_t byteswritten = 0;
  10437. mat_int8_t pad1 = 0;
  10438. switch ( data_type ) {
  10439. case MAT_T_UINT16:
  10440. {
  10441. nBytes = N*2;
  10442. fwrite(&data_type,4,1,(FILE*)mat->fp);
  10443. fwrite(&nBytes,4,1,(FILE*)mat->fp);
  10444. if ( NULL != data && N > 0 )
  10445. fwrite(data,2,N,(FILE*)mat->fp);
  10446. if ( nBytes % 8 )
  10447. for ( i = nBytes % 8; i < 8; i++ )
  10448. fwrite(&pad1,1,1,(FILE*)mat->fp);
  10449. break;
  10450. }
  10451. case MAT_T_INT8:
  10452. case MAT_T_UINT8:
  10453. {
  10454. mat_uint8_t *ptr;
  10455. mat_uint16_t c;
  10456. /* Matlab can't read MAT_C_CHAR as uint8, needs uint16 */
  10457. nBytes = N*2;
  10458. data_type = MAT_T_UINT16;
  10459. fwrite(&data_type,4,1,(FILE*)mat->fp);
  10460. fwrite(&nBytes,4,1,(FILE*)mat->fp);
  10461. ptr = (mat_uint8_t*)data;
  10462. if ( NULL == ptr )
  10463. break;
  10464. for ( i = 0; i < N; i++ ) {
  10465. c = (mat_uint16_t)*(char *)ptr;
  10466. fwrite(&c,2,1,(FILE*)mat->fp);
  10467. ptr++;
  10468. }
  10469. if ( nBytes % 8 )
  10470. for ( i = nBytes % 8; i < 8; i++ )
  10471. fwrite(&pad1,1,1,(FILE*)mat->fp);
  10472. break;
  10473. }
  10474. case MAT_T_UTF8:
  10475. {
  10476. mat_uint8_t *ptr;
  10477. nBytes = N;
  10478. fwrite(&data_type,4,1,(FILE*)mat->fp);
  10479. fwrite(&nBytes,4,1,(FILE*)mat->fp);
  10480. ptr = (mat_uint8_t*)data;
  10481. if ( NULL != ptr && nBytes > 0 )
  10482. fwrite(ptr,1,nBytes,(FILE*)mat->fp);
  10483. if ( nBytes % 8 )
  10484. for ( i = nBytes % 8; i < 8; i++ )
  10485. fwrite(&pad1,1,1,(FILE*)mat->fp);
  10486. break;
  10487. }
  10488. case MAT_T_UNKNOWN:
  10489. {
  10490. /* Sometimes empty char data will have MAT_T_UNKNOWN, so just write
  10491. * a data tag
  10492. */
  10493. nBytes = N*2;
  10494. data_type = MAT_T_UINT16;
  10495. fwrite(&data_type,4,1,(FILE*)mat->fp);
  10496. fwrite(&nBytes,4,1,(FILE*)mat->fp);
  10497. break;
  10498. }
  10499. default:
  10500. break;
  10501. }
  10502. byteswritten+=nBytes;
  10503. return byteswritten;
  10504. }
  10505. #if defined(HAVE_ZLIB)
  10506. /** @brief Writes @c data as compressed character data
  10507. *
  10508. * This function uses the knowledge that the data is part of a character class
  10509. * to avoid some pitfalls with Matlab listed below.
  10510. * @li Matlab character data cannot be unsigned 8-bit integers, it needs at
  10511. * least unsigned 16-bit integers
  10512. *
  10513. * @ingroup mat_internal
  10514. * @param mat MAT file pointer
  10515. * @param z pointer to the zlib compression stream
  10516. * @param data character data to write
  10517. * @param N Number of elements to write
  10518. * @param data_type character data type (enum matio_types)
  10519. * @return number of bytes written
  10520. */
  10521. static size_t
  10522. WriteCompressedCharData(mat_t *mat,z_streamp z,void *data,int N,
  10523. enum matio_types data_type)
  10524. {
  10525. int data_size, data_tag[2], byteswritten = 0;
  10526. int buf_size = 1024, i;
  10527. mat_uint8_t buf[1024], pad[8] = {0,};
  10528. if ((mat == NULL) || (mat->fp == NULL))
  10529. return 0;
  10530. switch ( data_type ) {
  10531. case MAT_T_UINT16:
  10532. {
  10533. data_size = 2;
  10534. data_tag[0] = MAT_T_UINT16;
  10535. data_tag[1] = N*data_size;
  10536. z->next_in = ZLIB_BYTE_PTR(data_tag);
  10537. z->avail_in = 8;
  10538. do {
  10539. z->next_out = buf;
  10540. z->avail_out = buf_size;
  10541. deflate(z,Z_NO_FLUSH);
  10542. byteswritten += fwrite(buf,1,buf_size-z->avail_out,(FILE*)mat->fp);
  10543. } while ( z->avail_out == 0 );
  10544. /* exit early if this is a empty data */
  10545. if ( NULL == data || N < 1 )
  10546. break;
  10547. z->next_in = (Bytef*)data;
  10548. z->avail_in = data_size*N;
  10549. do {
  10550. z->next_out = buf;
  10551. z->avail_out = buf_size;
  10552. deflate(z,Z_NO_FLUSH);
  10553. byteswritten += fwrite(buf,1,buf_size-z->avail_out,(FILE*)mat->fp);
  10554. } while ( z->avail_out == 0 );
  10555. /* Add/Compress padding to pad to 8-byte boundary */
  10556. if ( N*data_size % 8 ) {
  10557. z->next_in = pad;
  10558. z->avail_in = 8 - (N*data_size % 8);
  10559. do {
  10560. z->next_out = buf;
  10561. z->avail_out = buf_size;
  10562. deflate(z,Z_NO_FLUSH);
  10563. byteswritten += fwrite(buf,1,buf_size-z->avail_out,(FILE*)mat->fp);
  10564. } while ( z->avail_out == 0 );
  10565. }
  10566. break;
  10567. }
  10568. case MAT_T_INT8:
  10569. case MAT_T_UINT8:
  10570. {
  10571. mat_uint8_t *ptr;
  10572. mat_uint16_t c;
  10573. /* Matlab can't read MAT_C_CHAR as uint8, needs uint16 */
  10574. data_size = 2;
  10575. data_tag[0] = MAT_T_UINT16;
  10576. data_tag[1] = N*data_size;
  10577. z->next_in = ZLIB_BYTE_PTR(data_tag);
  10578. z->avail_in = 8;
  10579. do {
  10580. z->next_out = buf;
  10581. z->avail_out = buf_size;
  10582. deflate(z,Z_NO_FLUSH);
  10583. byteswritten += fwrite(buf,1,buf_size-z->avail_out,(FILE*)mat->fp);
  10584. } while ( z->avail_out == 0 );
  10585. /* exit early if this is a empty data */
  10586. if ( NULL == data || N < 1 )
  10587. break;
  10588. z->next_in = (Bytef*)data;
  10589. z->avail_in = data_size*N;
  10590. ptr = (mat_uint8_t*)data;
  10591. for ( i = 0; i < N; i++ ) {
  10592. c = (mat_uint16_t)*(char *)ptr;
  10593. z->next_in = ZLIB_BYTE_PTR(&c);
  10594. z->avail_in = 2;
  10595. do {
  10596. z->next_out = buf;
  10597. z->avail_out = buf_size;
  10598. deflate(z,Z_NO_FLUSH);
  10599. byteswritten += fwrite(buf,1,buf_size-z->avail_out,(FILE*)mat->fp);
  10600. } while ( z->avail_out == 0 );
  10601. ptr++;
  10602. }
  10603. /* Add/Compress padding to pad to 8-byte boundary */
  10604. if ( N*data_size % 8 ) {
  10605. z->next_in = pad;
  10606. z->avail_in = 8 - (N*data_size % 8);
  10607. do {
  10608. z->next_out = buf;
  10609. z->avail_out = buf_size;
  10610. deflate(z,Z_NO_FLUSH);
  10611. byteswritten += fwrite(buf,1,buf_size-z->avail_out,(FILE*)mat->fp);
  10612. } while ( z->avail_out == 0 );
  10613. }
  10614. break;
  10615. }
  10616. case MAT_T_UTF8:
  10617. {
  10618. data_size = 1;
  10619. data_tag[0] = MAT_T_UTF8;
  10620. data_tag[1] = N*data_size;
  10621. z->next_in = ZLIB_BYTE_PTR(data_tag);
  10622. z->avail_in = 8;
  10623. do {
  10624. z->next_out = buf;
  10625. z->avail_out = buf_size;
  10626. deflate(z,Z_NO_FLUSH);
  10627. byteswritten += fwrite(buf,1,buf_size-z->avail_out,(FILE*)mat->fp);
  10628. } while ( z->avail_out == 0 );
  10629. /* exit early if this is a empty data */
  10630. if ( NULL == data || N < 1 )
  10631. break;
  10632. z->next_in = (Bytef*)data;
  10633. z->avail_in = data_size*N;
  10634. do {
  10635. z->next_out = buf;
  10636. z->avail_out = buf_size;
  10637. deflate(z,Z_NO_FLUSH);
  10638. byteswritten += fwrite(buf,1,buf_size-z->avail_out,(FILE*)mat->fp);
  10639. } while ( z->avail_out == 0 );
  10640. /* Add/Compress padding to pad to 8-byte boundary */
  10641. if ( N*data_size % 8 ) {
  10642. z->next_in = pad;
  10643. z->avail_in = 8 - (N*data_size % 8);
  10644. do {
  10645. z->next_out = buf;
  10646. z->avail_out = buf_size;
  10647. deflate(z,Z_NO_FLUSH);
  10648. byteswritten += fwrite(buf,1,buf_size-z->avail_out,(FILE*)mat->fp);
  10649. } while ( z->avail_out == 0 );
  10650. }
  10651. break;
  10652. }
  10653. case MAT_T_UNKNOWN:
  10654. {
  10655. /* Sometimes empty char data will have MAT_T_UNKNOWN, so just write
  10656. * a data tag
  10657. */
  10658. data_size = 2;
  10659. data_tag[0] = MAT_T_UINT16;
  10660. data_tag[1] = N*data_size;
  10661. z->next_in = ZLIB_BYTE_PTR(data_tag);
  10662. z->avail_in = 8;
  10663. do {
  10664. z->next_out = buf;
  10665. z->avail_out = buf_size;
  10666. deflate(z,Z_NO_FLUSH);
  10667. byteswritten += fwrite(buf,1,buf_size-z->avail_out,(FILE*)mat->fp);
  10668. } while ( z->avail_out == 0 );
  10669. }
  10670. default:
  10671. break;
  10672. }
  10673. return byteswritten;
  10674. }
  10675. #endif
  10676. /** @if mat_devman
  10677. * @brief Writes empty characters to the MAT file
  10678. *
  10679. * This function uses the knowledge that the data is part of a character class
  10680. * to avoid some pitfalls with Matlab listed below.
  10681. * @li Matlab character data cannot be unsigned 8-bit integers, it needs at
  10682. * least unsigned 16-bit integers
  10683. * @ingroup mat_internal
  10684. * @param mat MAT file pointer
  10685. * @param data character data to write
  10686. * @param N Number of elements to write
  10687. * @param data_type character data type (enum matio_types)
  10688. * @return number of bytes written
  10689. * @endif
  10690. */
  10691. static size_t
  10692. WriteEmptyCharData(mat_t *mat, int N, enum matio_types data_type)
  10693. {
  10694. int nBytes = 0, i;
  10695. size_t byteswritten = 0;
  10696. mat_int8_t pad1 = 0;
  10697. switch ( data_type ) {
  10698. case MAT_T_UINT8:
  10699. case MAT_T_INT8:
  10700. data_type = MAT_T_UINT16;
  10701. /* Fall through: Matlab MAT_C_CHAR needs uint16 */
  10702. case MAT_T_UINT16:
  10703. {
  10704. mat_uint16_t u16 = 0;
  10705. nBytes = N*sizeof(mat_uint16_t);
  10706. fwrite(&data_type,sizeof(mat_int32_t),1,(FILE*)mat->fp);
  10707. fwrite(&nBytes,sizeof(mat_int32_t),1,(FILE*)mat->fp);
  10708. for ( i = 0; i < N; i++ )
  10709. fwrite(&u16,sizeof(mat_uint16_t),1,(FILE*)mat->fp);
  10710. if ( nBytes % 8 )
  10711. for ( i = nBytes % 8; i < 8; i++ )
  10712. fwrite(&pad1,1,1,(FILE*)mat->fp);
  10713. break;
  10714. }
  10715. case MAT_T_UTF8:
  10716. {
  10717. mat_uint8_t u8 = 0;
  10718. nBytes = N;
  10719. fwrite(&data_type,sizeof(mat_int32_t),1,(FILE*)mat->fp);
  10720. fwrite(&nBytes,sizeof(mat_int32_t),1,(FILE*)mat->fp);
  10721. for ( i = 0; i < N; i++ )
  10722. fwrite(&u8,sizeof(mat_uint8_t),1,(FILE*)mat->fp);
  10723. if ( nBytes % 8 )
  10724. for ( i = nBytes % 8; i < 8; i++ )
  10725. fwrite(&pad1,1,1,(FILE*)mat->fp);
  10726. break;
  10727. }
  10728. default:
  10729. break;
  10730. }
  10731. byteswritten+=nBytes;
  10732. return byteswritten;
  10733. }
  10734. /** @if mat_devman
  10735. * @brief Writes the data tags and empty data to the file
  10736. *
  10737. * Writes the data tags and empty data to the file to save space for the
  10738. * variable when the actual data is written
  10739. * @ingroup mat_internal
  10740. * @param mat MAT file pointer
  10741. * @param N number of elements to write
  10742. * @param data_type data type to write
  10743. * @return Number of bytes written
  10744. * @endif
  10745. */
  10746. static size_t
  10747. WriteEmptyData(mat_t *mat,int N,enum matio_types data_type)
  10748. {
  10749. int nBytes = 0, data_size, i;
  10750. if ( (mat == NULL) || (mat->fp == NULL) )
  10751. return 0;
  10752. switch ( data_type ) {
  10753. case MAT_T_DOUBLE:
  10754. {
  10755. double d = 0.0;
  10756. data_size = sizeof(double);
  10757. nBytes = N*data_size;
  10758. fwrite(&data_type,4,1,(FILE*)mat->fp);
  10759. fwrite(&nBytes,4,1,(FILE*)mat->fp);
  10760. for ( i = 0; i < N; i++ )
  10761. fwrite(&d,data_size,1,(FILE*)mat->fp);
  10762. break;
  10763. }
  10764. case MAT_T_SINGLE:
  10765. {
  10766. float f = 0.0;
  10767. data_size = sizeof(float);
  10768. nBytes = N*data_size;
  10769. fwrite(&data_type,4,1,(FILE*)mat->fp);
  10770. fwrite(&nBytes,4,1,(FILE*)mat->fp);
  10771. for ( i = 0; i < N; i++ )
  10772. fwrite(&f,data_size,1,(FILE*)mat->fp);
  10773. break;
  10774. }
  10775. case MAT_T_INT8:
  10776. {
  10777. mat_int8_t i8 = 0;
  10778. data_size = sizeof(mat_int8_t);
  10779. nBytes = N*data_size;
  10780. fwrite(&data_type,4,1,(FILE*)mat->fp);
  10781. fwrite(&nBytes,4,1,(FILE*)mat->fp);
  10782. for ( i = 0; i < N; i++ )
  10783. fwrite(&i8,data_size,1,(FILE*)mat->fp);
  10784. break;
  10785. }
  10786. case MAT_T_UINT8:
  10787. {
  10788. mat_uint8_t ui8 = 0;
  10789. data_size = sizeof(mat_uint8_t);
  10790. nBytes = N*data_size;
  10791. fwrite(&data_type,4,1,(FILE*)mat->fp);
  10792. fwrite(&nBytes,4,1,(FILE*)mat->fp);
  10793. for ( i = 0; i < N; i++ )
  10794. fwrite(&ui8,data_size,1,(FILE*)mat->fp);
  10795. break;
  10796. }
  10797. case MAT_T_INT16:
  10798. {
  10799. mat_int16_t i16 = 0;
  10800. data_size = sizeof(mat_int16_t);
  10801. nBytes = N*data_size;
  10802. fwrite(&data_type,4,1,(FILE*)mat->fp);
  10803. fwrite(&nBytes,4,1,(FILE*)mat->fp);
  10804. for ( i = 0; i < N; i++ )
  10805. fwrite(&i16,data_size,1,(FILE*)mat->fp);
  10806. break;
  10807. }
  10808. case MAT_T_UINT16:
  10809. {
  10810. mat_uint16_t ui16 = 0;
  10811. data_size = sizeof(mat_uint16_t);
  10812. nBytes = N*data_size;
  10813. fwrite(&data_type,4,1,(FILE*)mat->fp);
  10814. fwrite(&nBytes,4,1,(FILE*)mat->fp);
  10815. for ( i = 0; i < N; i++ )
  10816. fwrite(&ui16,data_size,1,(FILE*)mat->fp);
  10817. break;
  10818. }
  10819. case MAT_T_INT32:
  10820. {
  10821. mat_int32_t i32 = 0;
  10822. data_size = sizeof(mat_int32_t);
  10823. nBytes = N*data_size;
  10824. fwrite(&data_type,4,1,(FILE*)mat->fp);
  10825. fwrite(&nBytes,4,1,(FILE*)mat->fp);
  10826. for ( i = 0; i < N; i++ )
  10827. fwrite(&i32,data_size,1,(FILE*)mat->fp);
  10828. break;
  10829. }
  10830. case MAT_T_UINT32:
  10831. {
  10832. mat_uint32_t ui32 = 0;
  10833. data_size = sizeof(mat_uint32_t);
  10834. nBytes = N*data_size;
  10835. fwrite(&data_type,4,1,(FILE*)mat->fp);
  10836. fwrite(&nBytes,4,1,(FILE*)mat->fp);
  10837. for ( i = 0; i < N; i++ )
  10838. fwrite(&ui32,data_size,1,(FILE*)mat->fp);
  10839. break;
  10840. }
  10841. #ifdef HAVE_MATIO_INT64_T
  10842. case MAT_T_INT64:
  10843. {
  10844. mat_int64_t i64 = 0;
  10845. data_size = sizeof(mat_int64_t);
  10846. nBytes = N*data_size;
  10847. fwrite(&data_type,4,1,(FILE*)mat->fp);
  10848. fwrite(&nBytes,4,1,(FILE*)mat->fp);
  10849. for ( i = 0; i < N; i++ )
  10850. fwrite(&i64,data_size,1,(FILE*)mat->fp);
  10851. break;
  10852. }
  10853. #endif
  10854. #ifdef HAVE_MATIO_UINT64_T
  10855. case MAT_T_UINT64:
  10856. {
  10857. mat_uint64_t ui64 = 0;
  10858. data_size = sizeof(mat_uint64_t);
  10859. nBytes = N*data_size;
  10860. fwrite(&data_type,4,1,(FILE*)mat->fp);
  10861. fwrite(&nBytes,4,1,(FILE*)mat->fp);
  10862. for ( i = 0; i < N; i++ )
  10863. fwrite(&ui64,data_size,1,(FILE*)mat->fp);
  10864. break;
  10865. }
  10866. #endif
  10867. default:
  10868. nBytes = 0;
  10869. }
  10870. return nBytes;
  10871. }
  10872. #if defined(HAVE_ZLIB)
  10873. #if 0
  10874. static size_t
  10875. WriteCompressedEmptyData(mat_t *mat,z_streamp z,int N,
  10876. enum matio_types data_type)
  10877. {
  10878. int nBytes = 0, data_size, i;
  10879. size_t byteswritten = 0;
  10880. if ( (mat == NULL) || (mat->fp == NULL) )
  10881. return 0;
  10882. switch ( data_type ) {
  10883. case MAT_T_DOUBLE:
  10884. {
  10885. mat_uint32_t uncomp_buf[32] = {0,};
  10886. mat_uint32_t comp_buf[32] = {0,};
  10887. double data_uncomp_buf[4] = {0.0,};
  10888. data_size = sizeof(double);
  10889. nBytes = N*data_size;
  10890. uncomp_buf[0] = data_type;
  10891. uncomp_buf[1] = 0;
  10892. z->next_in = ZLIB_BYTE_PTR(uncomp_buf);
  10893. z->avail_in = 8;
  10894. do {
  10895. z->next_out = ZLIB_BYTE_PTR(comp_buf);
  10896. z->avail_out = 32*sizeof(*comp_buf);
  10897. deflate(z,Z_NO_FLUSH);
  10898. byteswritten += fwrite(comp_buf,1,32*sizeof(*comp_buf)-z->avail_out,(FILE*)mat->fp);
  10899. } while ( z->avail_out == 0 );
  10900. for ( i = 0; i < N; i++ ) {
  10901. z->next_in = ZLIB_BYTE_PTR(data_uncomp_buf);
  10902. z->avail_in = 8;
  10903. do {
  10904. z->next_out = ZLIB_BYTE_PTR(comp_buf);
  10905. z->avail_out = 32*sizeof(*comp_buf);
  10906. deflate(z,Z_NO_FLUSH);
  10907. byteswritten += fwrite(comp_buf,32*sizeof(*comp_buf)-z->avail_out,1,(FILE*)mat->fp);
  10908. } while ( z->avail_out == 0 );
  10909. }
  10910. break;
  10911. }
  10912. case MAT_T_SINGLE:
  10913. {
  10914. float f = 0.0;
  10915. data_size = sizeof(float);
  10916. nBytes = N*data_size;
  10917. fwrite(&data_type,4,1,(FILE*)mat->fp);
  10918. fwrite(&nBytes,4,1,(FILE*)mat->fp);
  10919. for ( i = 0; i < N; i++ )
  10920. fwrite(&f,data_size,1,(FILE*)mat->fp);
  10921. break;
  10922. }
  10923. case MAT_T_INT8:
  10924. {
  10925. mat_int8_t i8 = 0;
  10926. data_size = sizeof(mat_int8_t);
  10927. nBytes = N*data_size;
  10928. fwrite(&data_type,4,1,(FILE*)mat->fp);
  10929. fwrite(&nBytes,4,1,(FILE*)mat->fp);
  10930. for ( i = 0; i < N; i++ )
  10931. fwrite(&i8,data_size,1,(FILE*)mat->fp);
  10932. break;
  10933. }
  10934. case MAT_T_UINT8:
  10935. {
  10936. mat_uint8_t ui8 = 0;
  10937. data_size = sizeof(mat_uint8_t);
  10938. nBytes = N*data_size;
  10939. fwrite(&data_type,4,1,(FILE*)mat->fp);
  10940. fwrite(&nBytes,4,1,(FILE*)mat->fp);
  10941. for ( i = 0; i < N; i++ )
  10942. fwrite(&ui8,data_size,1,(FILE*)mat->fp);
  10943. break;
  10944. }
  10945. case MAT_T_INT16:
  10946. {
  10947. mat_int16_t i16 = 0;
  10948. data_size = sizeof(mat_int16_t);
  10949. nBytes = N*data_size;
  10950. fwrite(&data_type,4,1,(FILE*)mat->fp);
  10951. fwrite(&nBytes,4,1,(FILE*)mat->fp);
  10952. for ( i = 0; i < N; i++ )
  10953. fwrite(&i16,data_size,1,(FILE*)mat->fp);
  10954. break;
  10955. }
  10956. case MAT_T_UINT16:
  10957. {
  10958. mat_uint16_t ui16 = 0;
  10959. data_size = sizeof(mat_uint16_t);
  10960. nBytes = N*data_size;
  10961. fwrite(&data_type,4,1,(FILE*)mat->fp);
  10962. fwrite(&nBytes,4,1,(FILE*)mat->fp);
  10963. for ( i = 0; i < N; i++ )
  10964. fwrite(&ui16,data_size,1,(FILE*)mat->fp);
  10965. break;
  10966. }
  10967. case MAT_T_INT32:
  10968. {
  10969. mat_int32_t i32 = 0;
  10970. data_size = sizeof(mat_int32_t);
  10971. nBytes = N*data_size;
  10972. fwrite(&data_type,4,1,(FILE*)mat->fp);
  10973. fwrite(&nBytes,4,1,(FILE*)mat->fp);
  10974. for ( i = 0; i < N; i++ )
  10975. fwrite(&i32,data_size,1,(FILE*)mat->fp);
  10976. break;
  10977. }
  10978. case MAT_T_UINT32:
  10979. {
  10980. mat_uint32_t ui32 = 0;
  10981. data_size = sizeof(mat_uint32_t);
  10982. nBytes = N*data_size;
  10983. fwrite(&data_type,4,1,(FILE*)mat->fp);
  10984. fwrite(&nBytes,4,1,(FILE*)mat->fp);
  10985. for ( i = 0; i < N; i++ )
  10986. fwrite(&ui32,data_size,1,(FILE*)mat->fp);
  10987. break;
  10988. }
  10989. #ifdef HAVE_MATIO_INT64_T
  10990. case MAT_T_INT64:
  10991. {
  10992. mat_int64_t i64 = 0;
  10993. data_size = sizeof(mat_int64_t);
  10994. nBytes = N*data_size;
  10995. fwrite(&data_type,4,1,(FILE*)mat->fp);
  10996. fwrite(&nBytes,4,1,(FILE*)mat->fp);
  10997. for ( i = 0; i < N; i++ )
  10998. fwrite(&i64,data_size,1,(FILE*)mat->fp);
  10999. break;
  11000. }
  11001. #endif
  11002. #ifdef HAVE_MATIO_UINT64_T
  11003. case MAT_T_UINT64:
  11004. {
  11005. mat_uint64_t ui64 = 0;
  11006. data_size = sizeof(mat_uint64_t);
  11007. nBytes = N*data_size;
  11008. fwrite(&data_type,4,1,(FILE*)mat->fp);
  11009. fwrite(&nBytes,4,1,(FILE*)mat->fp);
  11010. for ( i = 0; i < N; i++ )
  11011. fwrite(&ui64,data_size,1,(FILE*)mat->fp);
  11012. break;
  11013. }
  11014. #endif
  11015. default:
  11016. nBytes = 0;
  11017. }
  11018. return byteswritten;
  11019. }
  11020. #endif
  11021. #endif
  11022. /** @if mat_devman
  11023. * @param Writes a 2-D slab of data to the MAT file
  11024. *
  11025. * @ingroup mat_internal
  11026. * @fixme should return the number of bytes written, but currently returns 0
  11027. * @param mat MAT file pointer
  11028. * @param data pointer to the slab of data
  11029. * @param data_type data type of the data (enum matio_types)
  11030. * @param dims dimensions of the dataset
  11031. * @param start index to start writing the data in each dimension
  11032. * @param stride write data every @c stride elements
  11033. * @param edge number of elements to write in each dimension
  11034. * @return number of byteswritten, or -1 on error
  11035. * @endif
  11036. */
  11037. static int
  11038. WriteDataSlab2(mat_t *mat,void *data,enum matio_types data_type,size_t *dims,
  11039. int *start,int *stride,int *edge)
  11040. {
  11041. int nBytes = 0, data_size, i, j;
  11042. int pos, row_stride, col_stride, pos2;
  11043. if ( (mat == NULL) || (data == NULL) || (mat->fp == NULL) ||
  11044. (start == NULL) || (stride == NULL) || (edge == NULL) ) {
  11045. return 0;
  11046. }
  11047. switch ( data_type ) {
  11048. case MAT_T_DOUBLE:
  11049. {
  11050. double *ptr;
  11051. data_size = sizeof(double);
  11052. ptr = (double *)data;
  11053. row_stride = (stride[0]-1)*data_size;
  11054. col_stride = stride[1]*dims[0]*data_size;
  11055. (void)fseek((FILE*)mat->fp,start[1]*dims[0]*data_size,SEEK_CUR);
  11056. for ( i = 0; i < edge[1]; i++ ) {
  11057. pos = ftell((FILE*)mat->fp);
  11058. if ( pos == -1L ) {
  11059. Mat_Critical("Couldn't determine file position");
  11060. return -1;
  11061. }
  11062. (void)fseek((FILE*)mat->fp,start[0]*data_size,SEEK_CUR);
  11063. for ( j = 0; j < edge[0]; j++ ) {
  11064. fwrite(ptr++,data_size,1,(FILE*)mat->fp);
  11065. (void)fseek((FILE*)mat->fp,row_stride,SEEK_CUR);
  11066. }
  11067. pos2 = ftell((FILE*)mat->fp);
  11068. if ( pos2 == -1L ) {
  11069. Mat_Critical("Couldn't determine file position");
  11070. return -1;
  11071. }
  11072. pos +=col_stride-pos2;
  11073. (void)fseek((FILE*)mat->fp,pos,SEEK_CUR);
  11074. }
  11075. break;
  11076. }
  11077. case MAT_T_SINGLE:
  11078. {
  11079. float *ptr;
  11080. data_size = sizeof(float);
  11081. ptr = (float *)data;
  11082. row_stride = (stride[0]-1)*data_size;
  11083. col_stride = stride[1]*dims[0]*data_size;
  11084. (void)fseek((FILE*)mat->fp,start[1]*dims[0]*data_size,SEEK_CUR);
  11085. for ( i = 0; i < edge[1]; i++ ) {
  11086. pos = ftell((FILE*)mat->fp);
  11087. if ( pos == -1L ) {
  11088. Mat_Critical("Couldn't determine file position");
  11089. return -1;
  11090. }
  11091. (void)fseek((FILE*)mat->fp,start[0]*data_size,SEEK_CUR);
  11092. for ( j = 0; j < edge[0]; j++ ) {
  11093. fwrite(ptr++,data_size,1,(FILE*)mat->fp);
  11094. (void)fseek((FILE*)mat->fp,row_stride,SEEK_CUR);
  11095. }
  11096. pos2 = ftell((FILE*)mat->fp);
  11097. if ( pos2 == -1L ) {
  11098. Mat_Critical("Couldn't determine file position");
  11099. return -1;
  11100. }
  11101. pos +=col_stride-pos2;
  11102. (void)fseek((FILE*)mat->fp,pos,SEEK_CUR);
  11103. }
  11104. break;
  11105. }
  11106. #ifdef HAVE_MATIO_INT64_T
  11107. case MAT_T_INT64:
  11108. {
  11109. mat_int64_t *ptr;
  11110. data_size = sizeof(mat_int64_t);
  11111. ptr = (mat_int64_t *)data;
  11112. row_stride = (stride[0]-1)*data_size;
  11113. col_stride = stride[1]*dims[0]*data_size;
  11114. (void)fseek((FILE*)mat->fp,start[1]*dims[0]*data_size,SEEK_CUR);
  11115. for ( i = 0; i < edge[1]; i++ ) {
  11116. pos = ftell((FILE*)mat->fp);
  11117. if ( pos == -1L ) {
  11118. Mat_Critical("Couldn't determine file position");
  11119. return -1;
  11120. }
  11121. (void)fseek((FILE*)mat->fp,start[0]*data_size,SEEK_CUR);
  11122. for ( j = 0; j < edge[0]; j++ ) {
  11123. fwrite(ptr++,data_size,1,(FILE*)mat->fp);
  11124. (void)fseek((FILE*)mat->fp,row_stride,SEEK_CUR);
  11125. }
  11126. pos2 = ftell((FILE*)mat->fp);
  11127. if ( pos2 == -1L ) {
  11128. Mat_Critical("Couldn't determine file position");
  11129. return -1;
  11130. }
  11131. pos +=col_stride-pos2;
  11132. (void)fseek((FILE*)mat->fp,pos,SEEK_CUR);
  11133. }
  11134. break;
  11135. }
  11136. #endif
  11137. #ifdef HAVE_MATIO_UINT64_T
  11138. case MAT_T_UINT64:
  11139. {
  11140. mat_uint64_t *ptr;
  11141. data_size = sizeof(mat_uint64_t);
  11142. ptr = (mat_uint64_t *)data;
  11143. row_stride = (stride[0]-1)*data_size;
  11144. col_stride = stride[1]*dims[0]*data_size;
  11145. (void)fseek((FILE*)mat->fp,start[1]*dims[0]*data_size,SEEK_CUR);
  11146. for ( i = 0; i < edge[1]; i++ ) {
  11147. pos = ftell((FILE*)mat->fp);
  11148. if ( pos == -1L ) {
  11149. Mat_Critical("Couldn't determine file position");
  11150. return -1;
  11151. }
  11152. (void)fseek((FILE*)mat->fp,start[0]*data_size,SEEK_CUR);
  11153. for ( j = 0; j < edge[0]; j++ ) {
  11154. fwrite(ptr++,data_size,1,(FILE*)mat->fp);
  11155. (void)fseek((FILE*)mat->fp,row_stride,SEEK_CUR);
  11156. }
  11157. pos2 = ftell((FILE*)mat->fp);
  11158. if ( pos2 == -1L ) {
  11159. Mat_Critical("Couldn't determine file position");
  11160. return -1;
  11161. }
  11162. pos +=col_stride-pos2;
  11163. (void)fseek((FILE*)mat->fp,pos,SEEK_CUR);
  11164. }
  11165. break;
  11166. }
  11167. #endif
  11168. case MAT_T_INT32:
  11169. {
  11170. mat_int32_t *ptr;
  11171. data_size = sizeof(mat_int32_t);
  11172. ptr = (mat_int32_t *)data;
  11173. row_stride = (stride[0]-1)*data_size;
  11174. col_stride = stride[1]*dims[0]*data_size;
  11175. (void)fseek((FILE*)mat->fp,start[1]*dims[0]*data_size,SEEK_CUR);
  11176. for ( i = 0; i < edge[1]; i++ ) {
  11177. pos = ftell((FILE*)mat->fp);
  11178. if ( pos == -1L ) {
  11179. Mat_Critical("Couldn't determine file position");
  11180. return -1;
  11181. }
  11182. (void)fseek((FILE*)mat->fp,start[0]*data_size,SEEK_CUR);
  11183. for ( j = 0; j < edge[0]; j++ ) {
  11184. fwrite(ptr++,data_size,1,(FILE*)mat->fp);
  11185. (void)fseek((FILE*)mat->fp,row_stride,SEEK_CUR);
  11186. }
  11187. pos2 = ftell((FILE*)mat->fp);
  11188. if ( pos2 == -1L ) {
  11189. Mat_Critical("Couldn't determine file position");
  11190. return -1;
  11191. }
  11192. pos +=col_stride-pos2;
  11193. (void)fseek((FILE*)mat->fp,pos,SEEK_CUR);
  11194. }
  11195. break;
  11196. }
  11197. case MAT_T_UINT32:
  11198. {
  11199. mat_uint32_t *ptr;
  11200. data_size = sizeof(mat_uint32_t);
  11201. ptr = (mat_uint32_t *)data;
  11202. row_stride = (stride[0]-1)*data_size;
  11203. col_stride = stride[1]*dims[0]*data_size;
  11204. (void)fseek((FILE*)mat->fp,start[1]*dims[0]*data_size,SEEK_CUR);
  11205. for ( i = 0; i < edge[1]; i++ ) {
  11206. pos = ftell((FILE*)mat->fp);
  11207. if ( pos == -1L ) {
  11208. Mat_Critical("Couldn't determine file position");
  11209. return -1;
  11210. }
  11211. (void)fseek((FILE*)mat->fp,start[0]*data_size,SEEK_CUR);
  11212. for ( j = 0; j < edge[0]; j++ ) {
  11213. fwrite(ptr++,data_size,1,(FILE*)mat->fp);
  11214. (void)fseek((FILE*)mat->fp,row_stride,SEEK_CUR);
  11215. }
  11216. pos2 = ftell((FILE*)mat->fp);
  11217. if ( pos2 == -1L ) {
  11218. Mat_Critical("Couldn't determine file position");
  11219. return -1;
  11220. }
  11221. pos +=col_stride-pos2;
  11222. (void)fseek((FILE*)mat->fp,pos,SEEK_CUR);
  11223. }
  11224. break;
  11225. }
  11226. case MAT_T_INT16:
  11227. {
  11228. mat_int16_t *ptr;
  11229. data_size = sizeof(mat_int16_t);
  11230. ptr = (mat_int16_t *)data;
  11231. row_stride = (stride[0]-1)*data_size;
  11232. col_stride = stride[1]*dims[0]*data_size;
  11233. (void)fseek((FILE*)mat->fp,start[1]*dims[0]*data_size,SEEK_CUR);
  11234. for ( i = 0; i < edge[1]; i++ ) {
  11235. pos = ftell((FILE*)mat->fp);
  11236. if ( pos == -1L ) {
  11237. Mat_Critical("Couldn't determine file position");
  11238. return -1;
  11239. }
  11240. (void)fseek((FILE*)mat->fp,start[0]*data_size,SEEK_CUR);
  11241. for ( j = 0; j < edge[0]; j++ ) {
  11242. fwrite(ptr++,data_size,1,(FILE*)mat->fp);
  11243. (void)fseek((FILE*)mat->fp,row_stride,SEEK_CUR);
  11244. }
  11245. pos2 = ftell((FILE*)mat->fp);
  11246. if ( pos2 == -1L ) {
  11247. Mat_Critical("Couldn't determine file position");
  11248. return -1;
  11249. }
  11250. pos +=col_stride-pos2;
  11251. (void)fseek((FILE*)mat->fp,pos,SEEK_CUR);
  11252. }
  11253. break;
  11254. }
  11255. case MAT_T_UINT16:
  11256. {
  11257. mat_uint16_t *ptr;
  11258. data_size = sizeof(mat_uint16_t);
  11259. ptr = (mat_uint16_t *)data;
  11260. row_stride = (stride[0]-1)*data_size;
  11261. col_stride = stride[1]*dims[0]*data_size;
  11262. (void)fseek((FILE*)mat->fp,start[1]*dims[0]*data_size,SEEK_CUR);
  11263. for ( i = 0; i < edge[1]; i++ ) {
  11264. pos = ftell((FILE*)mat->fp);
  11265. if ( pos == -1L ) {
  11266. Mat_Critical("Couldn't determine file position");
  11267. return -1;
  11268. }
  11269. (void)fseek((FILE*)mat->fp,start[0]*data_size,SEEK_CUR);
  11270. for ( j = 0; j < edge[0]; j++ ) {
  11271. fwrite(ptr++,data_size,1,(FILE*)mat->fp);
  11272. (void)fseek((FILE*)mat->fp,row_stride,SEEK_CUR);
  11273. }
  11274. pos2 = ftell((FILE*)mat->fp);
  11275. if ( pos2 == -1L ) {
  11276. Mat_Critical("Couldn't determine file position");
  11277. return -1;
  11278. }
  11279. pos +=col_stride-pos2;
  11280. (void)fseek((FILE*)mat->fp,pos,SEEK_CUR);
  11281. }
  11282. break;
  11283. }
  11284. case MAT_T_INT8:
  11285. {
  11286. mat_int8_t *ptr;
  11287. data_size = sizeof(mat_int8_t);
  11288. ptr = (mat_int8_t *)data;
  11289. row_stride = (stride[0]-1)*data_size;
  11290. col_stride = stride[1]*dims[0]*data_size;
  11291. (void)fseek((FILE*)mat->fp,start[1]*dims[0]*data_size,SEEK_CUR);
  11292. for ( i = 0; i < edge[1]; i++ ) {
  11293. pos = ftell((FILE*)mat->fp);
  11294. if ( pos == -1L ) {
  11295. Mat_Critical("Couldn't determine file position");
  11296. return -1;
  11297. }
  11298. (void)fseek((FILE*)mat->fp,start[0]*data_size,SEEK_CUR);
  11299. for ( j = 0; j < edge[0]; j++ ) {
  11300. fwrite(ptr++,data_size,1,(FILE*)mat->fp);
  11301. (void)fseek((FILE*)mat->fp,row_stride,SEEK_CUR);
  11302. }
  11303. pos2 = ftell((FILE*)mat->fp);
  11304. if ( pos2 == -1L ) {
  11305. Mat_Critical("Couldn't determine file position");
  11306. return -1;
  11307. }
  11308. pos +=col_stride-pos2;
  11309. (void)fseek((FILE*)mat->fp,pos,SEEK_CUR);
  11310. }
  11311. break;
  11312. }
  11313. case MAT_T_UINT8:
  11314. {
  11315. mat_uint8_t *ptr;
  11316. data_size = sizeof(mat_uint8_t);
  11317. ptr = (mat_uint8_t *)data;
  11318. row_stride = (stride[0]-1)*data_size;
  11319. col_stride = stride[1]*dims[0]*data_size;
  11320. (void)fseek((FILE*)mat->fp,start[1]*dims[0]*data_size,SEEK_CUR);
  11321. for ( i = 0; i < edge[1]; i++ ) {
  11322. pos = ftell((FILE*)mat->fp);
  11323. if ( pos == -1L ) {
  11324. Mat_Critical("Couldn't determine file position");
  11325. return -1;
  11326. }
  11327. (void)fseek((FILE*)mat->fp,start[0]*data_size,SEEK_CUR);
  11328. for ( j = 0; j < edge[0]; j++ ) {
  11329. fwrite(ptr++,data_size,1,(FILE*)mat->fp);
  11330. (void)fseek((FILE*)mat->fp,row_stride,SEEK_CUR);
  11331. }
  11332. pos2 = ftell((FILE*)mat->fp);
  11333. if ( pos2 == -1L ) {
  11334. Mat_Critical("Couldn't determine file position");
  11335. return -1;
  11336. }
  11337. pos +=col_stride-pos2;
  11338. (void)fseek((FILE*)mat->fp,pos,SEEK_CUR);
  11339. }
  11340. break;
  11341. }
  11342. default:
  11343. nBytes = 0;
  11344. }
  11345. return nBytes;
  11346. }
  11347. /** @if mat_devman
  11348. * @param Writes a 2-D slab of character data to the MAT file
  11349. *
  11350. * This function uses the knowledge that the data is part of a character class
  11351. * to avoid some pitfalls with Matlab listed below.
  11352. * @li Matlab character data cannot be unsigned 8-bit integers, it needs at
  11353. * least unsigned 16-bit integers
  11354. * @ingroup mat_internal
  11355. * @fixme should return the number of bytes written, but currently returns 0
  11356. * @param mat MAT file pointer
  11357. * @param data pointer to the slab of data
  11358. * @param data_type data type of the data (enum matio_types)
  11359. * @param dims dimensions of the dataset
  11360. * @param start index to start writing the data in each dimension
  11361. * @param stride write data every @c stride elements
  11362. * @param edge number of elements to write in each dimension
  11363. * @return number of byteswritten, or -1 on error
  11364. * @endif
  11365. */
  11366. static int
  11367. WriteCharDataSlab2(mat_t *mat,void *data,enum matio_types data_type,
  11368. size_t *dims,int *start,int *stride,int *edge)
  11369. {
  11370. int nBytes = 0, data_size, i, j;
  11371. int pos, row_stride, col_stride, pos2;
  11372. if ( (mat == NULL) || (data == NULL) || (mat->fp == NULL) ||
  11373. (start == NULL) || (stride == NULL) || (edge == NULL) ) {
  11374. return 0;
  11375. }
  11376. switch ( data_type ) {
  11377. case MAT_T_UINT16:
  11378. {
  11379. mat_uint16_t *ptr;
  11380. data_size = sizeof(mat_uint16_t);
  11381. ptr = (mat_uint16_t*)data;
  11382. row_stride = (stride[0]-1)*data_size;
  11383. col_stride = stride[1]*dims[0]*data_size;
  11384. (void)fseek((FILE*)mat->fp,start[1]*dims[0]*data_size,SEEK_CUR);
  11385. for ( i = 0; i < edge[1]; i++ ) {
  11386. pos = ftell((FILE*)mat->fp);
  11387. if ( pos == -1L ) {
  11388. Mat_Critical("Couldn't determine file position");
  11389. return -1;
  11390. }
  11391. (void)fseek((FILE*)mat->fp,start[0]*data_size,SEEK_CUR);
  11392. for ( j = 0; j < edge[0]; j++ ) {
  11393. fwrite(ptr++,data_size,1,(FILE*)mat->fp);
  11394. (void)fseek((FILE*)mat->fp,row_stride,SEEK_CUR);
  11395. }
  11396. pos2 = ftell((FILE*)mat->fp);
  11397. if ( pos2 == -1L ) {
  11398. Mat_Critical("Couldn't determine file position");
  11399. return -1;
  11400. }
  11401. pos +=col_stride-pos2;
  11402. (void)fseek((FILE*)mat->fp,pos,SEEK_CUR);
  11403. }
  11404. break;
  11405. }
  11406. case MAT_T_INT8:
  11407. case MAT_T_UINT8:
  11408. {
  11409. /* Matlab can't read MAT_C_CHAR as uint8, needs uint16 */
  11410. mat_uint8_t *ptr;
  11411. mat_uint16_t c;
  11412. data_size = sizeof(mat_uint16_t);
  11413. ptr = (mat_uint8_t*)data;
  11414. row_stride = (stride[0]-1)*data_size;
  11415. col_stride = stride[1]*dims[0]*data_size;
  11416. (void)fseek((FILE*)mat->fp,start[1]*dims[0]*data_size,SEEK_CUR);
  11417. for ( i = 0; i < edge[1]; i++ ) {
  11418. pos = ftell((FILE*)mat->fp);
  11419. if ( pos == -1L ) {
  11420. Mat_Critical("Couldn't determine file position");
  11421. return -1;
  11422. }
  11423. (void)fseek((FILE*)mat->fp,start[0]*data_size,SEEK_CUR);
  11424. for ( j = 0; j < edge[0]; j++,ptr++ ) {
  11425. c = *ptr;
  11426. fwrite(&c,data_size,1,(FILE*)mat->fp);
  11427. (void)fseek((FILE*)mat->fp,row_stride,SEEK_CUR);
  11428. }
  11429. pos2 = ftell((FILE*)mat->fp);
  11430. if ( pos2 == -1L ) {
  11431. Mat_Critical("Couldn't determine file position");
  11432. return -1;
  11433. }
  11434. pos +=col_stride-pos2;
  11435. (void)fseek((FILE*)mat->fp,pos,SEEK_CUR);
  11436. }
  11437. break;
  11438. }
  11439. case MAT_T_UTF8:
  11440. {
  11441. mat_uint8_t *ptr;
  11442. data_size = sizeof(mat_uint8_t);
  11443. ptr = (mat_uint8_t*)data;
  11444. row_stride = (stride[0]-1)*data_size;
  11445. col_stride = stride[1]*dims[0]*data_size;
  11446. (void)fseek((FILE*)mat->fp,start[1]*dims[0]*data_size,SEEK_CUR);
  11447. for ( i = 0; i < edge[1]; i++ ) {
  11448. pos = ftell((FILE*)mat->fp);
  11449. if ( pos == -1L ) {
  11450. Mat_Critical("Couldn't determine file position");
  11451. return -1;
  11452. }
  11453. (void)fseek((FILE*)mat->fp,start[0]*data_size,SEEK_CUR);
  11454. for ( j = 0; j < edge[0]; j++,ptr++ ) {
  11455. fwrite(ptr,data_size,1,(FILE*)mat->fp);
  11456. (void)fseek((FILE*)mat->fp,row_stride,SEEK_CUR);
  11457. }
  11458. pos2 = ftell((FILE*)mat->fp);
  11459. if ( pos2 == -1L ) {
  11460. Mat_Critical("Couldn't determine file position");
  11461. return -1;
  11462. }
  11463. pos +=col_stride-pos2;
  11464. (void)fseek((FILE*)mat->fp,pos,SEEK_CUR);
  11465. }
  11466. break;
  11467. }
  11468. default:
  11469. nBytes = 0;
  11470. }
  11471. return nBytes;
  11472. }
  11473. /** @brief Writes the data buffer to the file
  11474. *
  11475. * @param mat MAT file pointer
  11476. * @param data pointer to the data to write
  11477. * @param N number of elements to write
  11478. * @param data_type data type of the data
  11479. * @return number of bytes written
  11480. */
  11481. static int
  11482. WriteData(mat_t *mat,void *data,int N,enum matio_types data_type)
  11483. {
  11484. int nBytes = 0, data_size;
  11485. if ((mat == NULL) || (mat->fp == NULL) )
  11486. return 0;
  11487. data_size = Mat_SizeOf(data_type);
  11488. nBytes = N*data_size;
  11489. fwrite(&data_type,4,1,(FILE*)mat->fp);
  11490. fwrite(&nBytes,4,1,(FILE*)mat->fp);
  11491. if ( data != NULL && N > 0 )
  11492. fwrite(data,data_size,N,(FILE*)mat->fp);
  11493. return nBytes;
  11494. }
  11495. #if defined(HAVE_ZLIB)
  11496. /* Compresses the data buffer and writes it to the file */
  11497. static size_t
  11498. WriteCompressedData(mat_t *mat,z_streamp z,void *data,int N,
  11499. enum matio_types data_type)
  11500. {
  11501. int nBytes = 0, data_size, data_tag[2], byteswritten = 0;
  11502. int buf_size = 1024;
  11503. mat_uint8_t buf[1024], pad[8] = {0,};
  11504. if ((mat == NULL) || (mat->fp == NULL))
  11505. return 0;
  11506. data_size = Mat_SizeOf(data_type);
  11507. data_tag[0] = data_type;
  11508. data_tag[1] = data_size*N;
  11509. z->next_in = ZLIB_BYTE_PTR(data_tag);
  11510. z->avail_in = 8;
  11511. do {
  11512. z->next_out = buf;
  11513. z->avail_out = buf_size;
  11514. deflate(z,Z_NO_FLUSH);
  11515. byteswritten += fwrite(buf,1,buf_size-z->avail_out,(FILE*)mat->fp);
  11516. } while ( z->avail_out == 0 );
  11517. /* exit early if this is a empty data */
  11518. if ( NULL == data || N < 1 )
  11519. return byteswritten;
  11520. z->next_in = (Bytef*)data;
  11521. z->avail_in = N*data_size;
  11522. do {
  11523. z->next_out = buf;
  11524. z->avail_out = buf_size;
  11525. deflate(z,Z_NO_FLUSH);
  11526. byteswritten += fwrite(buf,1,buf_size-z->avail_out,(FILE*)mat->fp);
  11527. } while ( z->avail_out == 0 );
  11528. /* Add/Compress padding to pad to 8-byte boundary */
  11529. if ( N*data_size % 8 ) {
  11530. z->next_in = pad;
  11531. z->avail_in = 8 - (N*data_size % 8);
  11532. do {
  11533. z->next_out = buf;
  11534. z->avail_out = buf_size;
  11535. deflate(z,Z_NO_FLUSH);
  11536. byteswritten += fwrite(buf,1,buf_size-z->avail_out,(FILE*)mat->fp);
  11537. } while ( z->avail_out == 0 );
  11538. }
  11539. nBytes = byteswritten;
  11540. return nBytes;
  11541. }
  11542. #endif
  11543. /** @brief Reads the next cell of the cell array in @c matvar
  11544. *
  11545. * @ingroup mat_internal
  11546. * @param mat MAT file pointer
  11547. * @param matvar MAT variable pointer
  11548. * @return Number of bytes read
  11549. */
  11550. static size_t
  11551. ReadNextCell( mat_t *mat, matvar_t *matvar )
  11552. {
  11553. size_t bytesread = 0;
  11554. int ncells, i;
  11555. matvar_t **cells = NULL;
  11556. ncells = 1;
  11557. for ( i = 0; i < matvar->rank; i++ )
  11558. ncells *= matvar->dims[i];
  11559. matvar->data_size = sizeof(matvar_t *);
  11560. matvar->nbytes = ncells*matvar->data_size;
  11561. matvar->data = malloc(matvar->nbytes);
  11562. if ( !matvar->data ) {
  11563. Mat_Critical("Couldn't allocate memory for %s->data",matvar->name);
  11564. return bytesread;
  11565. }
  11566. cells = (matvar_t **)matvar->data;
  11567. if ( matvar->compression ) {
  11568. #if defined(HAVE_ZLIB)
  11569. mat_uint32_t uncomp_buf[16] = {0,};
  11570. int nbytes;
  11571. mat_uint32_t array_flags;
  11572. int err;
  11573. for ( i = 0; i < ncells; i++ ) {
  11574. cells[i] = Mat_VarCalloc();
  11575. if ( NULL == cells[i] ) {
  11576. Mat_Critical("Couldn't allocate memory for cell %d", i);
  11577. continue;
  11578. }
  11579. cells[i]->internal->fpos = ftell((FILE*)mat->fp);
  11580. if ( cells[i]->internal->fpos == -1L ) {
  11581. Mat_Critical("Couldn't determine file position");
  11582. continue;
  11583. } else {
  11584. cells[i]->internal->fpos -= matvar->internal->z->avail_in;
  11585. }
  11586. /* Read variable tag for cell */
  11587. uncomp_buf[0] = 0;
  11588. uncomp_buf[1] = 0;
  11589. bytesread += InflateVarTag(mat,matvar,uncomp_buf);
  11590. if ( mat->byteswap ) {
  11591. (void)Mat_uint32Swap(uncomp_buf);
  11592. (void)Mat_uint32Swap(uncomp_buf+1);
  11593. }
  11594. nbytes = uncomp_buf[1];
  11595. if ( !nbytes ) {
  11596. /* empty cell */
  11597. continue;
  11598. } else if ( uncomp_buf[0] != MAT_T_MATRIX ) {
  11599. Mat_VarFree(cells[i]);
  11600. cells[i] = NULL;
  11601. Mat_Critical("cells[%d], Uncompressed type not MAT_T_MATRIX",i);
  11602. break;
  11603. }
  11604. cells[i]->compression = MAT_COMPRESSION_ZLIB;
  11605. bytesread += InflateArrayFlags(mat,matvar,uncomp_buf);
  11606. nbytes -= 16;
  11607. if ( mat->byteswap ) {
  11608. (void)Mat_uint32Swap(uncomp_buf);
  11609. (void)Mat_uint32Swap(uncomp_buf+1);
  11610. (void)Mat_uint32Swap(uncomp_buf+2);
  11611. (void)Mat_uint32Swap(uncomp_buf+3);
  11612. }
  11613. /* Array Flags */
  11614. if ( uncomp_buf[0] == MAT_T_UINT32 ) {
  11615. array_flags = uncomp_buf[2];
  11616. cells[i]->class_type = CLASS_FROM_ARRAY_FLAGS(array_flags);
  11617. cells[i]->isComplex = (array_flags & MAT_F_COMPLEX);
  11618. cells[i]->isGlobal = (array_flags & MAT_F_GLOBAL);
  11619. cells[i]->isLogical = (array_flags & MAT_F_LOGICAL);
  11620. if ( cells[i]->class_type == MAT_C_SPARSE ) {
  11621. /* Need to find a more appropriate place to store nzmax */
  11622. cells[i]->nbytes = uncomp_buf[3];
  11623. }
  11624. } else {
  11625. Mat_Critical("Expected MAT_T_UINT32 for Array Tags, got %d",
  11626. uncomp_buf[0]);
  11627. bytesread+=InflateSkip(mat,matvar->internal->z,nbytes);
  11628. }
  11629. bytesread += InflateDimensions(mat,matvar,uncomp_buf);
  11630. nbytes -= 8;
  11631. if ( mat->byteswap ) {
  11632. (void)Mat_uint32Swap(uncomp_buf);
  11633. (void)Mat_uint32Swap(uncomp_buf+1);
  11634. }
  11635. /* Rank and Dimension */
  11636. if ( uncomp_buf[0] == MAT_T_INT32 ) {
  11637. int j = 0;
  11638. cells[i]->rank = uncomp_buf[1];
  11639. nbytes -= cells[i]->rank;
  11640. cells[i]->rank /= 4;
  11641. cells[i]->dims = (size_t*)malloc(cells[i]->rank*sizeof(*cells[i]->dims));
  11642. if ( mat->byteswap ) {
  11643. for ( j = 0; j < cells[i]->rank; j++ )
  11644. cells[i]->dims[j] = Mat_uint32Swap(uncomp_buf+2+j);
  11645. } else {
  11646. for ( j = 0; j < cells[i]->rank; j++ )
  11647. cells[i]->dims[j] = uncomp_buf[2+j];
  11648. }
  11649. if ( cells[i]->rank % 2 != 0 )
  11650. nbytes -= 4;
  11651. }
  11652. bytesread += InflateVarNameTag(mat,matvar,uncomp_buf);
  11653. nbytes -= 8;
  11654. if ( mat->byteswap ) {
  11655. (void)Mat_uint32Swap(uncomp_buf);
  11656. (void)Mat_uint32Swap(uncomp_buf+1);
  11657. }
  11658. /* Handle cell elements written with a variable name */
  11659. if ( uncomp_buf[1] > 0 ) {
  11660. /* Name of variable */
  11661. int len = 0;
  11662. if ( uncomp_buf[0] == MAT_T_INT8 ) { /* Name not in tag */
  11663. len = uncomp_buf[1];
  11664. if ( len % 8 > 0 )
  11665. len = len+(8-(len % 8));
  11666. cells[i]->name = (char*)malloc(len+1);
  11667. /* Inflate variable name */
  11668. bytesread += InflateVarName(mat,matvar,cells[i]->name,len);
  11669. cells[i]->name[len] = '\0';
  11670. nbytes -= len;
  11671. } else if ( ((uncomp_buf[0] & 0x0000ffff) == MAT_T_INT8) &&
  11672. ((uncomp_buf[0] & 0xffff0000) != 0x00) ) {
  11673. /* Name packed in tag */
  11674. len = (uncomp_buf[0] & 0xffff0000) >> 16;
  11675. cells[i]->name = (char*)malloc(len+1);
  11676. memcpy(cells[i]->name,uncomp_buf+1,len);
  11677. cells[i]->name[len] = '\0';
  11678. }
  11679. }
  11680. cells[i]->internal->z = (z_streamp)calloc(1,sizeof(z_stream));
  11681. if ( cells[i]->internal->z != NULL ) {
  11682. err = inflateCopy(cells[i]->internal->z,matvar->internal->z);
  11683. if ( err == Z_OK ) {
  11684. cells[i]->internal->datapos = ftell((FILE*)mat->fp);
  11685. if ( cells[i]->internal->datapos != -1L ) {
  11686. cells[i]->internal->datapos -= matvar->internal->z->avail_in;
  11687. if ( cells[i]->class_type == MAT_C_STRUCT )
  11688. bytesread+=ReadNextStructField(mat,cells[i]);
  11689. else if ( cells[i]->class_type == MAT_C_CELL )
  11690. bytesread+=ReadNextCell(mat,cells[i]);
  11691. else if ( nbytes <= (1 << MAX_WBITS) ) {
  11692. /* Memory optimization: Read data if less in size
  11693. than the zlib inflate state (approximately) */
  11694. cells[i]->internal->fp = mat;
  11695. Read5(mat,cells[i]);
  11696. cells[i]->internal->data = cells[i]->data;
  11697. cells[i]->data = NULL;
  11698. }
  11699. (void)fseek((FILE*)mat->fp,cells[i]->internal->datapos,SEEK_SET);
  11700. } else {
  11701. Mat_Critical("Couldn't determine file position");
  11702. }
  11703. if ( cells[i]->internal->data != NULL ||
  11704. cells[i]->class_type == MAT_C_STRUCT ||
  11705. cells[i]->class_type == MAT_C_CELL ) {
  11706. /* Memory optimization: Free inflate state */
  11707. inflateEnd(cells[i]->internal->z);
  11708. free(cells[i]->internal->z);
  11709. cells[i]->internal->z = NULL;
  11710. }
  11711. } else {
  11712. Mat_Critical("inflateCopy returned error %s",zError(err));
  11713. }
  11714. } else {
  11715. Mat_Critical("Couldn't allocate memory");
  11716. }
  11717. bytesread+=InflateSkip(mat,matvar->internal->z,nbytes);
  11718. }
  11719. #else
  11720. Mat_Critical("Not compiled with zlib support");
  11721. #endif
  11722. } else {
  11723. mat_uint32_t buf[16];
  11724. int nbytes,nBytes;
  11725. mat_uint32_t array_flags;
  11726. for ( i = 0; i < ncells; i++ ) {
  11727. int cell_bytes_read,name_len;
  11728. cells[i] = Mat_VarCalloc();
  11729. if ( !cells[i] ) {
  11730. Mat_Critical("Couldn't allocate memory for cell %d", i);
  11731. continue;
  11732. }
  11733. cells[i]->internal->fpos = ftell((FILE*)mat->fp);
  11734. if ( cells[i]->internal->fpos == -1L ) {
  11735. Mat_Critical("Couldn't determine file position");
  11736. continue;
  11737. }
  11738. /* Read variable tag for cell */
  11739. cell_bytes_read = fread(buf,4,2,(FILE*)mat->fp);
  11740. /* Empty cells at the end of a file may cause an EOF */
  11741. if ( !cell_bytes_read )
  11742. continue;
  11743. bytesread += cell_bytes_read;
  11744. if ( mat->byteswap ) {
  11745. (void)Mat_uint32Swap(buf);
  11746. (void)Mat_uint32Swap(buf+1);
  11747. }
  11748. nBytes = buf[1];
  11749. if ( !nBytes ) {
  11750. /* empty cell */
  11751. continue;
  11752. } else if ( buf[0] != MAT_T_MATRIX ) {
  11753. Mat_VarFree(cells[i]);
  11754. cells[i] = NULL;
  11755. Mat_Critical("cells[%d] not MAT_T_MATRIX, fpos = %ld",i,
  11756. ftell((FILE*)mat->fp));
  11757. break;
  11758. }
  11759. cells[i]->compression = MAT_COMPRESSION_NONE;
  11760. #if defined(HAVE_ZLIB)
  11761. cells[i]->internal->z = NULL;
  11762. #endif
  11763. /* Read Array Flags and The Dimensions Tag */
  11764. bytesread += fread(buf,4,6,(FILE*)mat->fp);
  11765. if ( mat->byteswap ) {
  11766. (void)Mat_uint32Swap(buf);
  11767. (void)Mat_uint32Swap(buf+1);
  11768. (void)Mat_uint32Swap(buf+2);
  11769. (void)Mat_uint32Swap(buf+3);
  11770. (void)Mat_uint32Swap(buf+4);
  11771. (void)Mat_uint32Swap(buf+5);
  11772. }
  11773. nBytes-=24;
  11774. /* Array Flags */
  11775. if ( buf[0] == MAT_T_UINT32 ) {
  11776. array_flags = buf[2];
  11777. cells[i]->class_type = CLASS_FROM_ARRAY_FLAGS(array_flags);
  11778. cells[i]->isComplex = (array_flags & MAT_F_COMPLEX);
  11779. cells[i]->isGlobal = (array_flags & MAT_F_GLOBAL);
  11780. cells[i]->isLogical = (array_flags & MAT_F_LOGICAL);
  11781. if ( cells[i]->class_type == MAT_C_SPARSE ) {
  11782. /* Need to find a more appropriate place to store nzmax */
  11783. cells[i]->nbytes = buf[3];
  11784. }
  11785. }
  11786. /* Rank and Dimension */
  11787. if ( buf[4] == MAT_T_INT32 ) {
  11788. int j;
  11789. nbytes = buf[5];
  11790. nBytes-=nbytes;
  11791. cells[i]->rank = nbytes / 4;
  11792. cells[i]->dims = (size_t*)malloc(cells[i]->rank*sizeof(*cells[i]->dims));
  11793. /* Assumes rank <= 16 */
  11794. if ( cells[i]->rank % 2 != 0 ) {
  11795. bytesread+=fread(buf,4,cells[i]->rank+1,(FILE*)mat->fp);
  11796. nBytes-=4;
  11797. } else
  11798. bytesread+=fread(buf,4,cells[i]->rank,(FILE*)mat->fp);
  11799. if ( mat->byteswap ) {
  11800. for ( j = 0; j < cells[i]->rank; j++ )
  11801. cells[i]->dims[j] = Mat_uint32Swap(buf+j);
  11802. } else {
  11803. for ( j = 0; j < cells[i]->rank; j++ )
  11804. cells[i]->dims[j] = buf[j];
  11805. }
  11806. }
  11807. /* Variable Name Tag */
  11808. bytesread+=fread(buf,1,8,(FILE*)mat->fp);
  11809. nBytes-=8;
  11810. if ( mat->byteswap ) {
  11811. (void)Mat_uint32Swap(buf);
  11812. (void)Mat_uint32Swap(buf+1);
  11813. }
  11814. name_len = 0;
  11815. if ( buf[1] > 0 ) {
  11816. /* Name of variable */
  11817. if ( buf[0] == MAT_T_INT8 ) { /* Name not in tag */
  11818. name_len = buf[1];
  11819. if ( name_len % 8 > 0 )
  11820. name_len = name_len+(8-(name_len % 8));
  11821. nBytes -= name_len;
  11822. (void)fseek((FILE*)mat->fp,name_len,SEEK_CUR);
  11823. }
  11824. }
  11825. cells[i]->internal->datapos = ftell((FILE*)mat->fp);
  11826. if ( cells[i]->internal->datapos != -1L ) {
  11827. if ( cells[i]->class_type == MAT_C_STRUCT )
  11828. bytesread+=ReadNextStructField(mat,cells[i]);
  11829. if ( cells[i]->class_type == MAT_C_CELL )
  11830. bytesread+=ReadNextCell(mat,cells[i]);
  11831. (void)fseek((FILE*)mat->fp,cells[i]->internal->datapos+nBytes,SEEK_SET);
  11832. } else {
  11833. Mat_Critical("Couldn't determine file position");
  11834. }
  11835. }
  11836. }
  11837. return bytesread;
  11838. }
  11839. /** @brief Reads the next struct field of the structure in @c matvar
  11840. *
  11841. * Reads the next struct fields (fieldname length,names,data headers for all
  11842. * the fields
  11843. * @ingroup mat_internal
  11844. * @param mat MAT file pointer
  11845. * @param matvar MAT variable pointer
  11846. * @return Number of bytes read
  11847. */
  11848. static size_t
  11849. ReadNextStructField( mat_t *mat, matvar_t *matvar )
  11850. {
  11851. int fieldname_size,nfields, nmemb = 1, i;
  11852. size_t bytesread = 0;
  11853. matvar_t **fields = NULL;
  11854. for ( i = 0; i < matvar->rank; i++ )
  11855. nmemb *= matvar->dims[i];
  11856. if ( matvar->compression ) {
  11857. #if defined(HAVE_ZLIB)
  11858. char *ptr;
  11859. mat_uint32_t uncomp_buf[16] = {0,};
  11860. int nbytes, j;
  11861. mat_uint32_t array_flags;
  11862. int err;
  11863. /* Inflate Field name length */
  11864. bytesread += InflateFieldNameLength(mat,matvar,uncomp_buf);
  11865. if ( mat->byteswap ) {
  11866. (void)Mat_uint32Swap(uncomp_buf);
  11867. (void)Mat_uint32Swap(uncomp_buf+1);
  11868. }
  11869. if ( (uncomp_buf[0] & 0x0000ffff) == MAT_T_INT32 ) {
  11870. fieldname_size = uncomp_buf[1];
  11871. } else {
  11872. Mat_Warning("Error getting fieldname size");
  11873. return bytesread;
  11874. }
  11875. bytesread += InflateFieldNamesTag(mat,matvar,uncomp_buf);
  11876. if ( mat->byteswap ) {
  11877. (void)Mat_uint32Swap(uncomp_buf);
  11878. (void)Mat_uint32Swap(uncomp_buf+1);
  11879. }
  11880. nfields = uncomp_buf[1];
  11881. nfields = nfields / fieldname_size;
  11882. matvar->data_size = sizeof(matvar_t *);
  11883. if ( nfields*fieldname_size % 8 != 0 )
  11884. i = 8-(nfields*fieldname_size % 8);
  11885. else
  11886. i = 0;
  11887. if ( nfields ) {
  11888. ptr = (char*)malloc(nfields*fieldname_size+i);
  11889. bytesread += InflateFieldNames(mat,matvar,ptr,nfields,fieldname_size,i);
  11890. matvar->internal->num_fields = nfields;
  11891. matvar->internal->fieldnames =
  11892. (char**)calloc(nfields,sizeof(*matvar->internal->fieldnames));
  11893. for ( i = 0; i < nfields; i++ ) {
  11894. matvar->internal->fieldnames[i] = (char*)malloc(fieldname_size);
  11895. memcpy(matvar->internal->fieldnames[i],ptr+i*fieldname_size,
  11896. fieldname_size);
  11897. matvar->internal->fieldnames[i][fieldname_size-1] = '\0';
  11898. }
  11899. free(ptr);
  11900. } else {
  11901. matvar->internal->num_fields = 0;
  11902. matvar->internal->fieldnames = NULL;
  11903. }
  11904. matvar->nbytes = nmemb*nfields*matvar->data_size;
  11905. if ( !matvar->nbytes )
  11906. return bytesread;
  11907. matvar->data = malloc(matvar->nbytes);
  11908. if ( !matvar->data )
  11909. return bytesread;
  11910. fields = (matvar_t**)matvar->data;
  11911. for ( i = 0; i < nmemb; i++ ) {
  11912. for ( j = 0; j < nfields; j++ ) {
  11913. fields[i*nfields+j] = Mat_VarCalloc();
  11914. fields[i*nfields+j]->name = mat_strdup(matvar->internal->fieldnames[j]);
  11915. }
  11916. }
  11917. for ( i = 0; i < nmemb*nfields; i++ ) {
  11918. fields[i]->internal->fpos = ftell((FILE*)mat->fp);
  11919. if ( fields[i]->internal->fpos == -1L ) {
  11920. Mat_Critical("Couldn't determine file position");
  11921. continue;
  11922. } else {
  11923. fields[i]->internal->fpos -= matvar->internal->z->avail_in;
  11924. }
  11925. /* Read variable tag for struct field */
  11926. bytesread += InflateVarTag(mat,matvar,uncomp_buf);
  11927. if ( mat->byteswap ) {
  11928. (void)Mat_uint32Swap(uncomp_buf);
  11929. (void)Mat_uint32Swap(uncomp_buf+1);
  11930. }
  11931. nbytes = uncomp_buf[1];
  11932. if ( uncomp_buf[0] != MAT_T_MATRIX ) {
  11933. Mat_VarFree(fields[i]);
  11934. fields[i] = NULL;
  11935. Mat_Critical("fields[%d], Uncompressed type not MAT_T_MATRIX",i);
  11936. continue;
  11937. } else if ( nbytes == 0 ) {
  11938. fields[i]->rank = 0;
  11939. continue;
  11940. }
  11941. fields[i]->compression = MAT_COMPRESSION_ZLIB;
  11942. bytesread += InflateArrayFlags(mat,matvar,uncomp_buf);
  11943. nbytes -= 16;
  11944. if ( mat->byteswap ) {
  11945. (void)Mat_uint32Swap(uncomp_buf);
  11946. (void)Mat_uint32Swap(uncomp_buf+1);
  11947. (void)Mat_uint32Swap(uncomp_buf+2);
  11948. (void)Mat_uint32Swap(uncomp_buf+3);
  11949. }
  11950. /* Array Flags */
  11951. if ( uncomp_buf[0] == MAT_T_UINT32 ) {
  11952. array_flags = uncomp_buf[2];
  11953. fields[i]->class_type = CLASS_FROM_ARRAY_FLAGS(array_flags);
  11954. fields[i]->isComplex = (array_flags & MAT_F_COMPLEX);
  11955. fields[i]->isGlobal = (array_flags & MAT_F_GLOBAL);
  11956. fields[i]->isLogical = (array_flags & MAT_F_LOGICAL);
  11957. if ( fields[i]->class_type == MAT_C_SPARSE ) {
  11958. /* Need to find a more appropriate place to store nzmax */
  11959. fields[i]->nbytes = uncomp_buf[3];
  11960. }
  11961. } else {
  11962. Mat_Critical("Expected MAT_T_UINT32 for Array Tags, got %d",
  11963. uncomp_buf[0]);
  11964. bytesread+=InflateSkip(mat,matvar->internal->z,nbytes);
  11965. }
  11966. bytesread += InflateDimensions(mat,matvar,uncomp_buf);
  11967. nbytes -= 8;
  11968. if ( mat->byteswap ) {
  11969. (void)Mat_uint32Swap(uncomp_buf);
  11970. (void)Mat_uint32Swap(uncomp_buf+1);
  11971. }
  11972. /* Rank and Dimension */
  11973. if ( uncomp_buf[0] == MAT_T_INT32 ) {
  11974. int j = 0;
  11975. fields[i]->rank = uncomp_buf[1];
  11976. nbytes -= fields[i]->rank;
  11977. fields[i]->rank /= 4;
  11978. fields[i]->dims = (size_t*)malloc(fields[i]->rank*
  11979. sizeof(*fields[i]->dims));
  11980. if ( mat->byteswap ) {
  11981. for ( j = 0; j < fields[i]->rank; j++ )
  11982. fields[i]->dims[j] = Mat_uint32Swap(uncomp_buf+2+j);
  11983. } else {
  11984. for ( j = 0; j < fields[i]->rank; j++ )
  11985. fields[i]->dims[j] = uncomp_buf[2+j];
  11986. }
  11987. if ( fields[i]->rank % 2 != 0 )
  11988. nbytes -= 4;
  11989. }
  11990. bytesread += InflateVarNameTag(mat,matvar,uncomp_buf);
  11991. nbytes -= 8;
  11992. fields[i]->internal->z = (z_streamp)calloc(1,sizeof(z_stream));
  11993. if ( fields[i]->internal->z != NULL ) {
  11994. err = inflateCopy(fields[i]->internal->z,matvar->internal->z);
  11995. if ( err == Z_OK ) {
  11996. fields[i]->internal->datapos = ftell((FILE*)mat->fp);
  11997. if ( fields[i]->internal->datapos != -1L ) {
  11998. fields[i]->internal->datapos -= matvar->internal->z->avail_in;
  11999. if ( fields[i]->class_type == MAT_C_STRUCT )
  12000. bytesread+=ReadNextStructField(mat,fields[i]);
  12001. else if ( fields[i]->class_type == MAT_C_CELL )
  12002. bytesread+=ReadNextCell(mat,fields[i]);
  12003. else if ( nbytes <= (1 << MAX_WBITS) ) {
  12004. /* Memory optimization: Read data if less in size
  12005. than the zlib inflate state (approximately) */
  12006. fields[i]->internal->fp = mat;
  12007. Read5(mat,fields[i]);
  12008. fields[i]->internal->data = fields[i]->data;
  12009. fields[i]->data = NULL;
  12010. }
  12011. (void)fseek((FILE*)mat->fp,fields[i]->internal->datapos,SEEK_SET);
  12012. } else {
  12013. Mat_Critical("Couldn't determine file position");
  12014. }
  12015. if ( fields[i]->internal->data != NULL ||
  12016. fields[i]->class_type == MAT_C_STRUCT ||
  12017. fields[i]->class_type == MAT_C_CELL ) {
  12018. /* Memory optimization: Free inflate state */
  12019. inflateEnd(fields[i]->internal->z);
  12020. free(fields[i]->internal->z);
  12021. fields[i]->internal->z = NULL;
  12022. }
  12023. } else {
  12024. Mat_Critical("inflateCopy returned error %s",zError(err));
  12025. }
  12026. } else {
  12027. Mat_Critical("Couldn't allocate memory");
  12028. }
  12029. bytesread+=InflateSkip(mat,matvar->internal->z,nbytes);
  12030. }
  12031. #else
  12032. Mat_Critical("Not compiled with zlib support");
  12033. #endif
  12034. } else {
  12035. mat_uint32_t buf[16] = {0,};
  12036. int nbytes,nBytes,j;
  12037. mat_uint32_t array_flags;
  12038. bytesread+=fread(buf,4,2,(FILE*)mat->fp);
  12039. if ( mat->byteswap ) {
  12040. (void)Mat_uint32Swap(buf);
  12041. (void)Mat_uint32Swap(buf+1);
  12042. }
  12043. if ( (buf[0] & 0x0000ffff) == MAT_T_INT32 ) {
  12044. fieldname_size = buf[1];
  12045. } else {
  12046. Mat_Warning("Error getting fieldname size");
  12047. return bytesread;
  12048. }
  12049. bytesread+=fread(buf,4,2,(FILE*)mat->fp);
  12050. if ( mat->byteswap ) {
  12051. (void)Mat_uint32Swap(buf);
  12052. (void)Mat_uint32Swap(buf+1);
  12053. }
  12054. nfields = buf[1];
  12055. nfields = nfields / fieldname_size;
  12056. matvar->data_size = sizeof(matvar_t *);
  12057. if ( nfields ) {
  12058. matvar->internal->num_fields = nfields;
  12059. matvar->internal->fieldnames =
  12060. (char**)calloc(nfields,sizeof(*matvar->internal->fieldnames));
  12061. for ( i = 0; i < nfields; i++ ) {
  12062. matvar->internal->fieldnames[i] = (char*)malloc(fieldname_size);
  12063. bytesread+=fread(matvar->internal->fieldnames[i],1,fieldname_size,(FILE*)mat->fp);
  12064. matvar->internal->fieldnames[i][fieldname_size-1] = '\0';
  12065. }
  12066. } else {
  12067. matvar->internal->num_fields = 0;
  12068. matvar->internal->fieldnames = NULL;
  12069. }
  12070. if ( (nfields*fieldname_size) % 8 ) {
  12071. (void)fseek((FILE*)mat->fp,8-((nfields*fieldname_size) % 8),SEEK_CUR);
  12072. bytesread+=8-((nfields*fieldname_size) % 8);
  12073. }
  12074. matvar->nbytes = nmemb*nfields*matvar->data_size;
  12075. if ( !matvar->nbytes )
  12076. return bytesread;
  12077. matvar->data = malloc(matvar->nbytes);
  12078. if ( !matvar->data )
  12079. return bytesread;
  12080. fields = (matvar_t**)matvar->data;
  12081. for ( i = 0; i < nmemb; i++ ) {
  12082. for ( j = 0; j < nfields; j++ ) {
  12083. fields[i*nfields+j] = Mat_VarCalloc();
  12084. fields[i*nfields+j]->name = mat_strdup(matvar->internal->fieldnames[j]);
  12085. }
  12086. }
  12087. for ( i = 0; i < nmemb*nfields; i++ ) {
  12088. fields[i]->internal->fpos = ftell((FILE*)mat->fp);
  12089. if ( fields[i]->internal->fpos == -1L ) {
  12090. Mat_Critical("Couldn't determine file position");
  12091. continue;
  12092. }
  12093. /* Read variable tag for struct field */
  12094. bytesread += fread(buf,4,2,(FILE*)mat->fp);
  12095. if ( mat->byteswap ) {
  12096. (void)Mat_uint32Swap(buf);
  12097. (void)Mat_uint32Swap(buf+1);
  12098. }
  12099. nBytes = buf[1];
  12100. if ( buf[0] != MAT_T_MATRIX ) {
  12101. Mat_VarFree(fields[i]);
  12102. fields[i] = NULL;
  12103. Mat_Critical("fields[%d] not MAT_T_MATRIX, fpos = %ld",i,
  12104. ftell((FILE*)mat->fp));
  12105. return bytesread;
  12106. } else if ( nBytes == 0 ) {
  12107. fields[i]->rank = 0;
  12108. continue;
  12109. }
  12110. fields[i]->compression = MAT_COMPRESSION_NONE;
  12111. #if defined(HAVE_ZLIB)
  12112. fields[i]->internal->z = NULL;
  12113. #endif
  12114. /* Read Array Flags and The Dimensions Tag */
  12115. bytesread += fread(buf,4,6,(FILE*)mat->fp);
  12116. if ( mat->byteswap ) {
  12117. (void)Mat_uint32Swap(buf);
  12118. (void)Mat_uint32Swap(buf+1);
  12119. (void)Mat_uint32Swap(buf+2);
  12120. (void)Mat_uint32Swap(buf+3);
  12121. (void)Mat_uint32Swap(buf+4);
  12122. (void)Mat_uint32Swap(buf+5);
  12123. }
  12124. nBytes-=24;
  12125. /* Array Flags */
  12126. if ( buf[0] == MAT_T_UINT32 ) {
  12127. array_flags = buf[2];
  12128. fields[i]->class_type = CLASS_FROM_ARRAY_FLAGS(array_flags);
  12129. fields[i]->isComplex = (array_flags & MAT_F_COMPLEX);
  12130. fields[i]->isGlobal = (array_flags & MAT_F_GLOBAL);
  12131. fields[i]->isLogical = (array_flags & MAT_F_LOGICAL);
  12132. if ( fields[i]->class_type == MAT_C_SPARSE ) {
  12133. /* Need to find a more appropriate place to store nzmax */
  12134. fields[i]->nbytes = buf[3];
  12135. }
  12136. }
  12137. /* Rank and Dimension */
  12138. if ( buf[4] == MAT_T_INT32 ) {
  12139. int j;
  12140. nbytes = buf[5];
  12141. nBytes-=nbytes;
  12142. fields[i]->rank = nbytes / 4;
  12143. fields[i]->dims = (size_t*)malloc(fields[i]->rank*
  12144. sizeof(*fields[i]->dims));
  12145. /* Assumes rank <= 16 */
  12146. if ( fields[i]->rank % 2 != 0 ) {
  12147. bytesread+=fread(buf,4,fields[i]->rank+1,(FILE*)mat->fp);
  12148. nBytes-=4;
  12149. } else
  12150. bytesread+=fread(buf,4,fields[i]->rank,(FILE*)mat->fp);
  12151. if ( mat->byteswap ) {
  12152. for ( j = 0; j < fields[i]->rank; j++ )
  12153. fields[i]->dims[j] = Mat_uint32Swap(buf+j);
  12154. } else {
  12155. for ( j = 0; j < fields[i]->rank; j++ )
  12156. fields[i]->dims[j] = buf[j];
  12157. }
  12158. }
  12159. /* Variable Name Tag */
  12160. bytesread+=fread(buf,1,8,(FILE*)mat->fp);
  12161. nBytes-=8;
  12162. fields[i]->internal->datapos = ftell((FILE*)mat->fp);
  12163. if ( fields[i]->internal->datapos != -1L ) {
  12164. if ( fields[i]->class_type == MAT_C_STRUCT )
  12165. bytesread+=ReadNextStructField(mat,fields[i]);
  12166. else if ( fields[i]->class_type == MAT_C_CELL )
  12167. bytesread+=ReadNextCell(mat,fields[i]);
  12168. (void)fseek((FILE*)mat->fp,fields[i]->internal->datapos+nBytes,SEEK_SET);
  12169. } else {
  12170. Mat_Critical("Couldn't determine file position");
  12171. }
  12172. }
  12173. }
  12174. return bytesread;
  12175. }
  12176. /** @brief Reads the function handle data of the function handle in @c matvar
  12177. *
  12178. * @ingroup mat_internal
  12179. * @param mat MAT file pointer
  12180. * @param matvar MAT variable pointer
  12181. * @return Number of bytes read
  12182. */
  12183. static size_t
  12184. ReadNextFunctionHandle(mat_t *mat, matvar_t *matvar)
  12185. {
  12186. int nfunctions = 1, i;
  12187. size_t bytesread = 0;
  12188. matvar_t **functions = NULL;
  12189. for ( i = 0; i < matvar->rank; i++ )
  12190. nfunctions *= matvar->dims[i];
  12191. matvar->data = malloc(nfunctions*sizeof(matvar_t *));
  12192. if ( matvar->data != NULL ) {
  12193. matvar->data_size = sizeof(matvar_t *);
  12194. matvar->nbytes = nfunctions*matvar->data_size;
  12195. functions = (matvar_t**)matvar->data;
  12196. for ( i = 0; i < nfunctions; i++ )
  12197. functions[i] = Mat_VarReadNextInfo(mat);
  12198. } else {
  12199. bytesread = 0;
  12200. matvar->data_size = 0;
  12201. matvar->nbytes = 0;
  12202. }
  12203. return bytesread;
  12204. }
  12205. /** @brief Writes the header and blank data for a cell array
  12206. *
  12207. * @ingroup mat_internal
  12208. * @param mat MAT file pointer
  12209. * @param matvar pointer to the mat variable
  12210. * @return number of bytes written
  12211. */
  12212. static int
  12213. WriteCellArrayFieldInfo(mat_t *mat,matvar_t *matvar)
  12214. {
  12215. mat_uint32_t array_flags = 0x0;
  12216. mat_int16_t array_name_type = MAT_T_INT8;
  12217. int array_flags_type = MAT_T_UINT32, dims_array_type = MAT_T_INT32;
  12218. int array_flags_size = 8, pad4 = 0, matrix_type = MAT_T_MATRIX;
  12219. mat_int8_t pad1 = 0;
  12220. int nBytes, i, nmemb = 1;
  12221. int start = 0, end = 0;
  12222. if ((matvar == NULL) || (mat == NULL))
  12223. return 0;
  12224. #if 0
  12225. nBytes = GetMatrixMaxBufSize(matvar);
  12226. #endif
  12227. fwrite(&matrix_type,4,1,(FILE*)mat->fp);
  12228. fwrite(&pad4,4,1,(FILE*)mat->fp);
  12229. start = ftell((FILE*)mat->fp);
  12230. /* Array Flags */
  12231. array_flags = matvar->class_type & CLASS_TYPE_MASK;
  12232. if ( matvar->isComplex )
  12233. array_flags |= MAT_F_COMPLEX;
  12234. if ( matvar->isGlobal )
  12235. array_flags |= MAT_F_GLOBAL;
  12236. if ( matvar->isLogical )
  12237. array_flags |= MAT_F_LOGICAL;
  12238. if ( mat->byteswap )
  12239. array_flags = Mat_int32Swap((mat_int32_t*)&array_flags);
  12240. fwrite(&array_flags_type,4,1,(FILE*)mat->fp);
  12241. fwrite(&array_flags_size,4,1,(FILE*)mat->fp);
  12242. fwrite(&array_flags,4,1,(FILE*)mat->fp);
  12243. fwrite(&pad4,4,1,(FILE*)mat->fp);
  12244. /* Rank and Dimension */
  12245. nBytes = matvar->rank * 4;
  12246. fwrite(&dims_array_type,4,1,(FILE*)mat->fp);
  12247. fwrite(&nBytes,4,1,(FILE*)mat->fp);
  12248. for ( i = 0; i < matvar->rank; i++ ) {
  12249. mat_int32_t dim;
  12250. dim = matvar->dims[i];
  12251. nmemb *= dim;
  12252. fwrite(&dim,4,1,(FILE*)mat->fp);
  12253. }
  12254. if ( matvar->rank % 2 != 0 )
  12255. fwrite(&pad4,4,1,(FILE*)mat->fp);
  12256. /* Name of variable */
  12257. if ( !matvar->name ) {
  12258. fwrite(&array_name_type,2,1,(FILE*)mat->fp);
  12259. fwrite(&pad1,1,1,(FILE*)mat->fp);
  12260. fwrite(&pad1,1,1,(FILE*)mat->fp);
  12261. fwrite(&pad4,4,1,(FILE*)mat->fp);
  12262. } else if ( strlen(matvar->name) <= 4 ) {
  12263. mat_int16_t array_name_len = (mat_int16_t)strlen(matvar->name);
  12264. mat_int8_t pad1 = 0;
  12265. fwrite(&array_name_type,2,1,(FILE*)mat->fp);
  12266. fwrite(&array_name_len,2,1,(FILE*)mat->fp);
  12267. fwrite(matvar->name,1,array_name_len,(FILE*)mat->fp);
  12268. for ( i = array_name_len; i < 4; i++ )
  12269. fwrite(&pad1,1,1,(FILE*)mat->fp);
  12270. } else {
  12271. mat_int32_t array_name_len = (mat_int32_t)strlen(matvar->name);
  12272. mat_int8_t pad1 = 0;
  12273. fwrite(&array_name_type,2,1,(FILE*)mat->fp);
  12274. fwrite(&pad1,1,1,(FILE*)mat->fp);
  12275. fwrite(&pad1,1,1,(FILE*)mat->fp);
  12276. fwrite(&array_name_len,4,1,(FILE*)mat->fp);
  12277. fwrite(matvar->name,1,array_name_len,(FILE*)mat->fp);
  12278. if ( array_name_len % 8 )
  12279. for ( i = array_name_len % 8; i < 8; i++ )
  12280. fwrite(&pad1,1,1,(FILE*)mat->fp);
  12281. }
  12282. matvar->internal->datapos = ftell((FILE*)mat->fp);
  12283. if ( matvar->internal->datapos == -1L ) {
  12284. Mat_Critical("Couldn't determine file position");
  12285. }
  12286. switch ( matvar->class_type ) {
  12287. case MAT_C_DOUBLE:
  12288. case MAT_C_SINGLE:
  12289. case MAT_C_INT64:
  12290. case MAT_C_UINT64:
  12291. case MAT_C_INT32:
  12292. case MAT_C_UINT32:
  12293. case MAT_C_INT16:
  12294. case MAT_C_UINT16:
  12295. case MAT_C_INT8:
  12296. case MAT_C_UINT8:
  12297. nBytes = WriteEmptyData(mat,nmemb,matvar->data_type);
  12298. if ( nBytes % 8 )
  12299. for ( i = nBytes % 8; i < 8; i++ )
  12300. fwrite(&pad1,1,1,(FILE*)mat->fp);
  12301. if ( matvar->isComplex ) {
  12302. nBytes = WriteEmptyData(mat,nmemb,matvar->data_type);
  12303. if ( nBytes % 8 )
  12304. for ( i = nBytes % 8; i < 8; i++ )
  12305. fwrite(&pad1,1,1,(FILE*)mat->fp);
  12306. }
  12307. break;
  12308. case MAT_C_CHAR:
  12309. {
  12310. WriteEmptyCharData(mat,nmemb,matvar->data_type);
  12311. break;
  12312. }
  12313. case MAT_C_CELL:
  12314. {
  12315. int ncells;
  12316. matvar_t **cells = (matvar_t **)matvar->data;
  12317. /* Check for an empty cell array */
  12318. if ( matvar->nbytes == 0 || matvar->data_size == 0 ||
  12319. matvar->data == NULL )
  12320. break;
  12321. ncells = matvar->nbytes / matvar->data_size;
  12322. for ( i = 0; i < ncells; i++ )
  12323. WriteCellArrayFieldInfo(mat,cells[i]);
  12324. break;
  12325. }
  12326. /* FIXME: Structures */
  12327. case MAT_C_STRUCT:
  12328. case MAT_C_SPARSE:
  12329. case MAT_C_FUNCTION:
  12330. case MAT_C_OBJECT:
  12331. case MAT_C_EMPTY:
  12332. break;
  12333. }
  12334. end = ftell((FILE*)mat->fp);
  12335. if ( start != -1L && end != -1L ) {
  12336. nBytes = (int)(end-start);
  12337. (void)fseek((FILE*)mat->fp,(long)-(nBytes+4),SEEK_CUR);
  12338. fwrite(&nBytes,4,1,(FILE*)mat->fp);
  12339. (void)fseek((FILE*)mat->fp,end,SEEK_SET);
  12340. } else {
  12341. Mat_Critical("Couldn't determine file position");
  12342. }
  12343. return 0;
  12344. }
  12345. /** @brief Writes the header and data for an element of a cell array
  12346. *
  12347. * @ingroup mat_internal
  12348. * @param mat MAT file pointer
  12349. * @param matvar pointer to the mat variable
  12350. * @retval 0 on success
  12351. */
  12352. static int
  12353. WriteCellArrayField(mat_t *mat,matvar_t *matvar )
  12354. {
  12355. mat_uint32_t array_flags = 0x0;
  12356. mat_int16_t array_name_type = MAT_T_INT8,fieldname_type = MAT_T_INT32,fieldname_data_size=4;
  12357. int array_flags_type = MAT_T_UINT32, dims_array_type = MAT_T_INT32;
  12358. int array_flags_size = 8, pad4 = 0, matrix_type = MAT_T_MATRIX;
  12359. mat_int8_t pad1 = 0;
  12360. int nBytes, i, nmemb = 1, nzmax = 0;
  12361. int start = 0, end = 0;
  12362. if ((matvar == NULL) || (mat == NULL))
  12363. return 1;
  12364. #if 0
  12365. nBytes = GetMatrixMaxBufSize(matvar);
  12366. #endif
  12367. fwrite(&matrix_type,4,1,(FILE*)mat->fp);
  12368. fwrite(&pad4,4,1,(FILE*)mat->fp);
  12369. start = ftell((FILE*)mat->fp);
  12370. /* Array Flags */
  12371. array_flags = matvar->class_type & CLASS_TYPE_MASK;
  12372. if ( matvar->isComplex )
  12373. array_flags |= MAT_F_COMPLEX;
  12374. if ( matvar->isGlobal )
  12375. array_flags |= MAT_F_GLOBAL;
  12376. if ( matvar->isLogical )
  12377. array_flags |= MAT_F_LOGICAL;
  12378. if ( matvar->class_type == MAT_C_SPARSE )
  12379. nzmax = ((mat_sparse_t *)matvar->data)->nzmax;
  12380. if ( mat->byteswap )
  12381. array_flags = Mat_int32Swap((mat_int32_t*)&array_flags);
  12382. fwrite(&array_flags_type,4,1,(FILE*)mat->fp);
  12383. fwrite(&array_flags_size,4,1,(FILE*)mat->fp);
  12384. fwrite(&array_flags,4,1,(FILE*)mat->fp);
  12385. fwrite(&nzmax,4,1,(FILE*)mat->fp);
  12386. /* Rank and Dimension */
  12387. nBytes = matvar->rank * 4;
  12388. fwrite(&dims_array_type,4,1,(FILE*)mat->fp);
  12389. fwrite(&nBytes,4,1,(FILE*)mat->fp);
  12390. for ( i = 0; i < matvar->rank; i++ ) {
  12391. mat_int32_t dim;
  12392. dim = matvar->dims[i];
  12393. nmemb *= dim;
  12394. fwrite(&dim,4,1,(FILE*)mat->fp);
  12395. }
  12396. if ( matvar->rank % 2 != 0 )
  12397. fwrite(&pad4,4,1,(FILE*)mat->fp);
  12398. /* Name of variable */
  12399. if ( !matvar->name ) {
  12400. fwrite(&array_name_type,2,1,(FILE*)mat->fp);
  12401. fwrite(&pad1,1,1,(FILE*)mat->fp);
  12402. fwrite(&pad1,1,1,(FILE*)mat->fp);
  12403. fwrite(&pad4,4,1,(FILE*)mat->fp);
  12404. } else if ( strlen(matvar->name) <= 4 ) {
  12405. mat_int16_t array_name_len = (mat_int16_t)strlen(matvar->name);
  12406. mat_int8_t pad1 = 0;
  12407. fwrite(&array_name_type,2,1,(FILE*)mat->fp);
  12408. fwrite(&array_name_len,2,1,(FILE*)mat->fp);
  12409. fwrite(matvar->name,1,array_name_len,(FILE*)mat->fp);
  12410. for ( i = array_name_len; i < 4; i++ )
  12411. fwrite(&pad1,1,1,(FILE*)mat->fp);
  12412. } else {
  12413. mat_int32_t array_name_len = (mat_int32_t)strlen(matvar->name);
  12414. mat_int8_t pad1 = 0;
  12415. fwrite(&array_name_type,2,1,(FILE*)mat->fp);
  12416. fwrite(&pad1,1,1,(FILE*)mat->fp);
  12417. fwrite(&pad1,1,1,(FILE*)mat->fp);
  12418. fwrite(&array_name_len,4,1,(FILE*)mat->fp);
  12419. fwrite(matvar->name,1,array_name_len,(FILE*)mat->fp);
  12420. if ( array_name_len % 8 )
  12421. for ( i = array_name_len % 8; i < 8; i++ )
  12422. fwrite(&pad1,1,1,(FILE*)mat->fp);
  12423. }
  12424. switch ( matvar->class_type ) {
  12425. case MAT_C_DOUBLE:
  12426. case MAT_C_SINGLE:
  12427. case MAT_C_INT64:
  12428. case MAT_C_UINT64:
  12429. case MAT_C_INT32:
  12430. case MAT_C_UINT32:
  12431. case MAT_C_INT16:
  12432. case MAT_C_UINT16:
  12433. case MAT_C_INT8:
  12434. case MAT_C_UINT8:
  12435. {
  12436. if ( matvar->isComplex ) {
  12437. mat_complex_split_t *complex_data = (mat_complex_split_t*)matvar->data;
  12438. if ( NULL == matvar->data )
  12439. complex_data = &null_complex_data;
  12440. nBytes=WriteData(mat,complex_data->Re,nmemb,matvar->data_type);
  12441. if ( nBytes % 8 )
  12442. for ( i = nBytes % 8; i < 8; i++ )
  12443. fwrite(&pad1,1,1,(FILE*)mat->fp);
  12444. nBytes=WriteData(mat,complex_data->Im,nmemb,matvar->data_type);
  12445. if ( nBytes % 8 )
  12446. for ( i = nBytes % 8; i < 8; i++ )
  12447. fwrite(&pad1,1,1,(FILE*)mat->fp);
  12448. } else {
  12449. nBytes = WriteData(mat,matvar->data,nmemb,matvar->data_type);
  12450. if ( nBytes % 8 )
  12451. for ( i = nBytes % 8; i < 8; i++ )
  12452. fwrite(&pad1,1,1,(FILE*)mat->fp);
  12453. }
  12454. break;
  12455. }
  12456. case MAT_C_CHAR:
  12457. WriteCharData(mat,matvar->data,nmemb,matvar->data_type);
  12458. break;
  12459. case MAT_C_CELL:
  12460. {
  12461. int ncells;
  12462. matvar_t **cells = (matvar_t **)matvar->data;
  12463. /* Check for an empty cell array */
  12464. if ( matvar->nbytes == 0 || matvar->data_size == 0 ||
  12465. matvar->data == NULL )
  12466. break;
  12467. ncells = matvar->nbytes / matvar->data_size;
  12468. for ( i = 0; i < ncells; i++ )
  12469. WriteCellArrayField(mat,cells[i]);
  12470. break;
  12471. }
  12472. case MAT_C_STRUCT:
  12473. {
  12474. char *padzero;
  12475. int fieldname_size, nfields;
  12476. size_t maxlen = 0;
  12477. matvar_t **fields = (matvar_t **)matvar->data;
  12478. unsigned fieldname;
  12479. nfields = matvar->internal->num_fields;
  12480. for ( i = 0; i < nfields; i++ ) {
  12481. size_t len = strlen(matvar->internal->fieldnames[i]);
  12482. if ( len > maxlen )
  12483. maxlen = len;
  12484. }
  12485. maxlen++;
  12486. fieldname_size = maxlen;
  12487. while ( nfields*fieldname_size % 8 != 0 )
  12488. fieldname_size++;
  12489. #if 0
  12490. fwrite(&fieldname_type,2,1,(FILE*)mat->fp);
  12491. fwrite(&fieldname_data_size,2,1,(FILE*)mat->fp);
  12492. #else
  12493. fieldname = (fieldname_data_size<<16) | fieldname_type;
  12494. fwrite(&fieldname,4,1,(FILE*)mat->fp);
  12495. #endif
  12496. fwrite(&fieldname_size,4,1,(FILE*)mat->fp);
  12497. fwrite(&array_name_type,2,1,(FILE*)mat->fp);
  12498. fwrite(&pad1,1,1,(FILE*)mat->fp);
  12499. fwrite(&pad1,1,1,(FILE*)mat->fp);
  12500. nBytes = nfields*fieldname_size;
  12501. fwrite(&nBytes,4,1,(FILE*)mat->fp);
  12502. padzero = (char*)calloc(fieldname_size,1);
  12503. for ( i = 0; i < nfields; i++ ) {
  12504. size_t len = strlen(matvar->internal->fieldnames[i]);
  12505. fwrite(matvar->internal->fieldnames[i],1,len,(FILE*)mat->fp);
  12506. fwrite(padzero,1,fieldname_size-len,(FILE*)mat->fp);
  12507. }
  12508. free(padzero);
  12509. for ( i = 0; i < nmemb*nfields; i++ )
  12510. WriteStructField(mat,fields[i]);
  12511. break;
  12512. }
  12513. case MAT_C_SPARSE:
  12514. {
  12515. mat_sparse_t *sparse = (mat_sparse_t*)matvar->data;
  12516. nBytes = WriteData(mat,sparse->ir,sparse->nir,MAT_T_INT32);
  12517. if ( nBytes % 8 )
  12518. for ( i = nBytes % 8; i < 8; i++ )
  12519. fwrite(&pad1,1,1,(FILE*)mat->fp);
  12520. nBytes = WriteData(mat,sparse->jc,sparse->njc,MAT_T_INT32);
  12521. if ( nBytes % 8 )
  12522. for ( i = nBytes % 8; i < 8; i++ )
  12523. fwrite(&pad1,1,1,(FILE*)mat->fp);
  12524. if ( matvar->isComplex ) {
  12525. mat_complex_split_t *complex_data = (mat_complex_split_t*)sparse->data;
  12526. nBytes = WriteData(mat,complex_data->Re,sparse->ndata,
  12527. matvar->data_type);
  12528. if ( nBytes % 8 )
  12529. for ( i = nBytes % 8; i < 8; i++ )
  12530. fwrite(&pad1,1,1,(FILE*)mat->fp);
  12531. nBytes = WriteData(mat,complex_data->Im,sparse->ndata,
  12532. matvar->data_type);
  12533. if ( nBytes % 8 )
  12534. for ( i = nBytes % 8; i < 8; i++ )
  12535. fwrite(&pad1,1,1,(FILE*)mat->fp);
  12536. } else {
  12537. nBytes = WriteData(mat,sparse->data,sparse->ndata,
  12538. matvar->data_type);
  12539. if ( nBytes % 8 )
  12540. for ( i = nBytes % 8; i < 8; i++ )
  12541. fwrite(&pad1,1,1,(FILE*)mat->fp);
  12542. }
  12543. }
  12544. case MAT_C_FUNCTION:
  12545. case MAT_C_OBJECT:
  12546. case MAT_C_EMPTY:
  12547. break;
  12548. }
  12549. end = ftell((FILE*)mat->fp);
  12550. if ( start != -1L && end != -1L ) {
  12551. nBytes = (int)(end-start);
  12552. (void)fseek((FILE*)mat->fp,(long)-(nBytes+4),SEEK_CUR);
  12553. fwrite(&nBytes,4,1,(FILE*)mat->fp);
  12554. (void)fseek((FILE*)mat->fp,end,SEEK_SET);
  12555. } else {
  12556. Mat_Critical("Couldn't determine file position");
  12557. }
  12558. return 0;
  12559. }
  12560. #if defined(HAVE_ZLIB)
  12561. /** @brief Writes the header and data for a field of a compressed cell array
  12562. *
  12563. * @ingroup mat_internal
  12564. * @param mat MAT file pointer
  12565. * @param matvar pointer to the mat variable
  12566. * @return number of bytes written to the MAT file
  12567. */
  12568. static size_t
  12569. WriteCompressedCellArrayField(mat_t *mat,matvar_t *matvar,z_streamp z)
  12570. {
  12571. mat_uint32_t array_flags = 0x0;
  12572. mat_int16_t array_name_type = MAT_T_INT8;
  12573. mat_int16_t fieldname_type = MAT_T_INT32;
  12574. mat_int16_t fieldname_data_size = 4;
  12575. int array_flags_type = MAT_T_UINT32, dims_array_type = MAT_T_INT32;
  12576. int array_flags_size = 8, pad4 = 0;
  12577. int nBytes, i, nmemb = 1, nzmax = 0;
  12578. int start = 0;
  12579. mat_uint32_t comp_buf[512];
  12580. mat_uint32_t uncomp_buf[512] = {0,};
  12581. int buf_size = 512;
  12582. size_t byteswritten = 0;
  12583. if ( NULL == matvar || NULL == mat || NULL == z)
  12584. return 0;
  12585. start = ftell((FILE*)mat->fp);
  12586. /* Array Flags */
  12587. array_flags = matvar->class_type & CLASS_TYPE_MASK;
  12588. if ( matvar->isComplex )
  12589. array_flags |= MAT_F_COMPLEX;
  12590. if ( matvar->isGlobal )
  12591. array_flags |= MAT_F_GLOBAL;
  12592. if ( matvar->isLogical )
  12593. array_flags |= MAT_F_LOGICAL;
  12594. if ( matvar->class_type == MAT_C_SPARSE )
  12595. nzmax = ((mat_sparse_t *)matvar->data)->nzmax;
  12596. uncomp_buf[0] = MAT_T_MATRIX;
  12597. uncomp_buf[1] = (int)GetCellArrayFieldBufSize(matvar);
  12598. z->next_in = ZLIB_BYTE_PTR(uncomp_buf);
  12599. z->avail_in = 8;
  12600. do {
  12601. z->next_out = ZLIB_BYTE_PTR(comp_buf);
  12602. z->avail_out = buf_size*sizeof(*comp_buf);
  12603. deflate(z,Z_NO_FLUSH);
  12604. byteswritten += fwrite(comp_buf,1,buf_size*sizeof(*comp_buf)-z->avail_out,
  12605. (FILE*)mat->fp);
  12606. } while ( z->avail_out == 0 );
  12607. uncomp_buf[0] = array_flags_type;
  12608. uncomp_buf[1] = array_flags_size;
  12609. uncomp_buf[2] = array_flags;
  12610. uncomp_buf[3] = nzmax;
  12611. /* Rank and Dimension */
  12612. nBytes = matvar->rank * 4;
  12613. uncomp_buf[4] = dims_array_type;
  12614. uncomp_buf[5] = nBytes;
  12615. for ( i = 0; i < matvar->rank; i++ ) {
  12616. mat_int32_t dim;
  12617. dim = matvar->dims[i];
  12618. nmemb *= dim;
  12619. uncomp_buf[6+i] = dim;
  12620. }
  12621. if ( matvar->rank % 2 != 0 ) {
  12622. uncomp_buf[6+i] = pad4;
  12623. i++;
  12624. }
  12625. z->next_in = ZLIB_BYTE_PTR(uncomp_buf);
  12626. z->avail_in = (6+i)*sizeof(*uncomp_buf);
  12627. do {
  12628. z->next_out = ZLIB_BYTE_PTR(comp_buf);
  12629. z->avail_out = buf_size*sizeof(*comp_buf);
  12630. deflate(z,Z_NO_FLUSH);
  12631. byteswritten += fwrite(comp_buf,1,buf_size*sizeof(*comp_buf)-z->avail_out,
  12632. (FILE*)mat->fp);
  12633. } while ( z->avail_out == 0 );
  12634. /* Name of variable */
  12635. uncomp_buf[0] = array_name_type;
  12636. uncomp_buf[1] = 0;
  12637. z->next_in = ZLIB_BYTE_PTR(uncomp_buf);
  12638. z->avail_in = 8;
  12639. do {
  12640. z->next_out = ZLIB_BYTE_PTR(comp_buf);
  12641. z->avail_out = buf_size*sizeof(*comp_buf);
  12642. deflate(z,Z_NO_FLUSH);
  12643. byteswritten += fwrite(comp_buf,1,buf_size*sizeof(*comp_buf)-z->avail_out,
  12644. (FILE*)mat->fp);
  12645. } while ( z->avail_out == 0 );
  12646. matvar->internal->datapos = ftell((FILE*)mat->fp);
  12647. if ( matvar->internal->datapos == -1L ) {
  12648. Mat_Critical("Couldn't determine file position");
  12649. }
  12650. switch ( matvar->class_type ) {
  12651. case MAT_C_DOUBLE:
  12652. case MAT_C_SINGLE:
  12653. case MAT_C_INT64:
  12654. case MAT_C_UINT64:
  12655. case MAT_C_INT32:
  12656. case MAT_C_UINT32:
  12657. case MAT_C_INT16:
  12658. case MAT_C_UINT16:
  12659. case MAT_C_INT8:
  12660. case MAT_C_UINT8:
  12661. {
  12662. /* WriteCompressedData makes sure uncompressed data is aligned
  12663. * on an 8-byte boundary */
  12664. if ( matvar->isComplex ) {
  12665. mat_complex_split_t *complex_data = (mat_complex_split_t*)matvar->data;
  12666. if ( NULL == matvar->data )
  12667. complex_data = &null_complex_data;
  12668. byteswritten += WriteCompressedData(mat,z,
  12669. complex_data->Re,nmemb,matvar->data_type);
  12670. byteswritten += WriteCompressedData(mat,z,
  12671. complex_data->Im,nmemb,matvar->data_type);
  12672. } else {
  12673. byteswritten += WriteCompressedData(mat,z,
  12674. matvar->data,nmemb,matvar->data_type);
  12675. }
  12676. break;
  12677. }
  12678. case MAT_C_CHAR:
  12679. {
  12680. byteswritten += WriteCompressedCharData(mat,z,matvar->data,
  12681. nmemb,matvar->data_type);
  12682. break;
  12683. }
  12684. case MAT_C_CELL:
  12685. {
  12686. int ncells;
  12687. matvar_t **cells = (matvar_t **)matvar->data;
  12688. /* Check for an empty cell array */
  12689. if ( matvar->nbytes == 0 || matvar->data_size == 0 ||
  12690. matvar->data == NULL )
  12691. break;
  12692. ncells = matvar->nbytes / matvar->data_size;
  12693. for ( i = 0; i < ncells; i++ )
  12694. WriteCompressedCellArrayField(mat,cells[i],z);
  12695. break;
  12696. }
  12697. case MAT_C_STRUCT:
  12698. {
  12699. unsigned char *padzero;
  12700. int fieldname_size, nfields;
  12701. size_t maxlen = 0;
  12702. mat_int32_t array_name_type = MAT_T_INT8;
  12703. matvar_t **fields = (matvar_t **)matvar->data;
  12704. nfields = matvar->internal->num_fields;
  12705. /* Check for a structure with no fields */
  12706. if ( nfields < 1 ) {
  12707. fieldname_size = 1;
  12708. uncomp_buf[0] = (fieldname_data_size << 16) |
  12709. fieldname_type;
  12710. uncomp_buf[1] = fieldname_size;
  12711. uncomp_buf[2] = array_name_type;
  12712. uncomp_buf[3] = 0;
  12713. z->next_in = ZLIB_BYTE_PTR(uncomp_buf);
  12714. z->avail_in = 16;
  12715. do {
  12716. z->next_out = ZLIB_BYTE_PTR(comp_buf);
  12717. z->avail_out = buf_size*sizeof(*comp_buf);
  12718. deflate(z,Z_NO_FLUSH);
  12719. byteswritten += fwrite(comp_buf,1,buf_size*
  12720. sizeof(*comp_buf)-z->avail_out,(FILE*)mat->fp);
  12721. } while ( z->avail_out == 0 );
  12722. break;
  12723. }
  12724. for ( i = 0; i < nfields; i++ ) {
  12725. size_t len = strlen(matvar->internal->fieldnames[i]);
  12726. if ( len > maxlen )
  12727. maxlen = len;
  12728. }
  12729. maxlen++;
  12730. fieldname_size = maxlen;
  12731. while ( nfields*fieldname_size % 8 != 0 )
  12732. fieldname_size++;
  12733. uncomp_buf[0] = (fieldname_data_size << 16) | fieldname_type;
  12734. uncomp_buf[1] = fieldname_size;
  12735. uncomp_buf[2] = array_name_type;
  12736. uncomp_buf[3] = nfields*fieldname_size;
  12737. padzero = (unsigned char*)calloc(fieldname_size,1);
  12738. z->next_in = ZLIB_BYTE_PTR(uncomp_buf);
  12739. z->avail_in = 16;
  12740. do {
  12741. z->next_out = ZLIB_BYTE_PTR(comp_buf);
  12742. z->avail_out = buf_size*sizeof(*comp_buf);
  12743. deflate(z,Z_NO_FLUSH);
  12744. byteswritten += fwrite(comp_buf,1,
  12745. buf_size*sizeof(*comp_buf)-z->avail_out,(FILE*)mat->fp);
  12746. } while ( z->avail_out == 0 );
  12747. for ( i = 0; i < nfields; i++ ) {
  12748. memset(padzero,'\0',fieldname_size);
  12749. memcpy(padzero,matvar->internal->fieldnames[i],
  12750. strlen(matvar->internal->fieldnames[i]));
  12751. z->next_in = ZLIB_BYTE_PTR(padzero);
  12752. z->avail_in = fieldname_size;
  12753. do {
  12754. z->next_out = ZLIB_BYTE_PTR(comp_buf);
  12755. z->avail_out = buf_size*sizeof(*comp_buf);
  12756. deflate(z,Z_NO_FLUSH);
  12757. byteswritten += fwrite(comp_buf,1,
  12758. buf_size*sizeof(*comp_buf)-z->avail_out,(FILE*)mat->fp);
  12759. } while ( z->avail_out == 0 );
  12760. }
  12761. free(padzero);
  12762. for ( i = 0; i < nmemb*nfields; i++ )
  12763. byteswritten +=
  12764. WriteCompressedStructField(mat,fields[i],z);
  12765. break;
  12766. }
  12767. case MAT_C_SPARSE:
  12768. {
  12769. mat_sparse_t *sparse = (mat_sparse_t*)matvar->data;
  12770. byteswritten += WriteCompressedData(mat,z,sparse->ir,
  12771. sparse->nir,MAT_T_INT32);
  12772. byteswritten += WriteCompressedData(mat,z,sparse->jc,
  12773. sparse->njc,MAT_T_INT32);
  12774. if ( matvar->isComplex ) {
  12775. mat_complex_split_t *complex_data = (mat_complex_split_t*)sparse->data;
  12776. byteswritten += WriteCompressedData(mat,z,
  12777. complex_data->Re,sparse->ndata,matvar->data_type);
  12778. byteswritten += WriteCompressedData(mat,z,
  12779. complex_data->Im,sparse->ndata,matvar->data_type);
  12780. } else {
  12781. byteswritten += WriteCompressedData(mat,z,
  12782. sparse->data,sparse->ndata,matvar->data_type);
  12783. }
  12784. break;
  12785. }
  12786. case MAT_C_FUNCTION:
  12787. case MAT_C_OBJECT:
  12788. case MAT_C_EMPTY:
  12789. break;
  12790. }
  12791. return byteswritten;
  12792. }
  12793. #endif
  12794. /** @brief Writes the header and data for a field of a struct array
  12795. *
  12796. * @ingroup mat_internal
  12797. * @param mat MAT file pointer
  12798. * @param matvar pointer to the mat variable
  12799. * @retval 0 on success
  12800. */
  12801. static int
  12802. WriteStructField(mat_t *mat,matvar_t *matvar)
  12803. {
  12804. mat_uint32_t array_flags = 0x0;
  12805. mat_int16_t fieldname_type = MAT_T_INT32,fieldname_data_size=4;
  12806. mat_int32_t array_name_type = MAT_T_INT8;
  12807. int array_flags_type = MAT_T_UINT32, dims_array_type = MAT_T_INT32;
  12808. int array_flags_size = 8, pad4 = 0, matrix_type = MAT_T_MATRIX;
  12809. mat_int8_t pad1 = 0;
  12810. int nBytes, i, nmemb = 1, nzmax = 0;
  12811. int start = 0, end = 0;
  12812. if ( mat == NULL )
  12813. return 1;
  12814. if ( NULL == matvar ) {
  12815. size_t dims[2] = {0,0};
  12816. Mat_WriteEmptyVariable5(mat, NULL, 2, dims);
  12817. return 0;
  12818. }
  12819. fwrite(&matrix_type,4,1,(FILE*)mat->fp);
  12820. fwrite(&pad4,4,1,(FILE*)mat->fp);
  12821. start = ftell((FILE*)mat->fp);
  12822. /* Array Flags */
  12823. array_flags = matvar->class_type & CLASS_TYPE_MASK;
  12824. if ( matvar->isComplex )
  12825. array_flags |= MAT_F_COMPLEX;
  12826. if ( matvar->isGlobal )
  12827. array_flags |= MAT_F_GLOBAL;
  12828. if ( matvar->isLogical )
  12829. array_flags |= MAT_F_LOGICAL;
  12830. if ( matvar->class_type == MAT_C_SPARSE )
  12831. nzmax = ((mat_sparse_t *)matvar->data)->nzmax;
  12832. if ( mat->byteswap )
  12833. array_flags = Mat_int32Swap((mat_int32_t*)&array_flags);
  12834. fwrite(&array_flags_type,4,1,(FILE*)mat->fp);
  12835. fwrite(&array_flags_size,4,1,(FILE*)mat->fp);
  12836. fwrite(&array_flags,4,1,(FILE*)mat->fp);
  12837. fwrite(&nzmax,4,1,(FILE*)mat->fp);
  12838. /* Rank and Dimension */
  12839. nBytes = matvar->rank * 4;
  12840. fwrite(&dims_array_type,4,1,(FILE*)mat->fp);
  12841. fwrite(&nBytes,4,1,(FILE*)mat->fp);
  12842. for ( i = 0; i < matvar->rank; i++ ) {
  12843. mat_int32_t dim;
  12844. dim = matvar->dims[i];
  12845. nmemb *= dim;
  12846. fwrite(&dim,4,1,(FILE*)mat->fp);
  12847. }
  12848. if ( matvar->rank % 2 != 0 )
  12849. fwrite(&pad4,4,1,(FILE*)mat->fp);
  12850. /* Name of variable */
  12851. fwrite(&array_name_type,4,1,(FILE*)mat->fp);
  12852. fwrite(&pad4,4,1,(FILE*)mat->fp);
  12853. switch ( matvar->class_type ) {
  12854. case MAT_C_DOUBLE:
  12855. case MAT_C_SINGLE:
  12856. case MAT_C_INT64:
  12857. case MAT_C_UINT64:
  12858. case MAT_C_INT32:
  12859. case MAT_C_UINT32:
  12860. case MAT_C_INT16:
  12861. case MAT_C_UINT16:
  12862. case MAT_C_INT8:
  12863. case MAT_C_UINT8:
  12864. {
  12865. if ( matvar->isComplex ) {
  12866. mat_complex_split_t *complex_data = (mat_complex_split_t*)matvar->data;
  12867. if ( NULL == matvar->data )
  12868. complex_data = &null_complex_data;
  12869. nBytes=WriteData(mat,complex_data->Re,nmemb,matvar->data_type);
  12870. if ( nBytes % 8 )
  12871. for ( i = nBytes % 8; i < 8; i++ )
  12872. fwrite(&pad1,1,1,(FILE*)mat->fp);
  12873. nBytes=WriteData(mat,complex_data->Im,nmemb,matvar->data_type);
  12874. if ( nBytes % 8 )
  12875. for ( i = nBytes % 8; i < 8; i++ )
  12876. fwrite(&pad1,1,1,(FILE*)mat->fp);
  12877. } else {
  12878. nBytes=WriteData(mat,matvar->data,nmemb,matvar->data_type);
  12879. if ( nBytes % 8 )
  12880. for ( i = nBytes % 8; i < 8; i++ )
  12881. fwrite(&pad1,1,1,(FILE*)mat->fp);
  12882. }
  12883. break;
  12884. }
  12885. case MAT_C_CHAR:
  12886. nBytes=WriteCharData(mat,matvar->data,nmemb,matvar->data_type);
  12887. break;
  12888. case MAT_C_CELL:
  12889. {
  12890. int ncells;
  12891. matvar_t **cells = (matvar_t **)matvar->data;
  12892. /* Check for an empty cell array */
  12893. if ( matvar->nbytes == 0 || matvar->data_size == 0 ||
  12894. matvar->data == NULL )
  12895. break;
  12896. ncells = matvar->nbytes / matvar->data_size;
  12897. for ( i = 0; i < ncells; i++ )
  12898. WriteCellArrayField(mat,cells[i]);
  12899. break;
  12900. }
  12901. case MAT_C_STRUCT:
  12902. {
  12903. char *padzero;
  12904. int fieldname_size, nfields = 0;
  12905. size_t maxlen = 0;
  12906. matvar_t **fields = (matvar_t **)matvar->data;
  12907. unsigned fieldname;
  12908. /* nmemb*matvar->data_size can be zero when saving a struct that
  12909. * contains an empty struct in one of its fields
  12910. * (e.g. x.y = struct('z', {})). If it's zero, we would divide
  12911. * by zero.
  12912. */
  12913. nfields = matvar->internal->num_fields;
  12914. for ( i = 0; i < nfields; i++ ) {
  12915. size_t len = strlen(matvar->internal->fieldnames[i]);
  12916. if ( len > maxlen )
  12917. maxlen = len;
  12918. }
  12919. maxlen++;
  12920. fieldname_size = maxlen;
  12921. while ( nfields*fieldname_size % 8 != 0 )
  12922. fieldname_size++;
  12923. #if 0
  12924. fwrite(&fieldname_type,2,1,(FILE*)mat->fp);
  12925. fwrite(&fieldname_data_size,2,1,(FILE*)mat->fp);
  12926. #else
  12927. fieldname = (fieldname_data_size<<16) | fieldname_type;
  12928. fwrite(&fieldname,4,1,(FILE*)mat->fp);
  12929. #endif
  12930. fwrite(&fieldname_size,4,1,(FILE*)mat->fp);
  12931. fwrite(&array_name_type,4,1,(FILE*)mat->fp);
  12932. nBytes = nfields*fieldname_size;
  12933. fwrite(&nBytes,4,1,(FILE*)mat->fp);
  12934. padzero = (char*)calloc(fieldname_size,1);
  12935. for ( i = 0; i < nfields; i++ ) {
  12936. size_t len = strlen(matvar->internal->fieldnames[i]);
  12937. fwrite(matvar->internal->fieldnames[i],1,len,(FILE*)mat->fp);
  12938. fwrite(padzero,1,fieldname_size-len,(FILE*)mat->fp);
  12939. }
  12940. free(padzero);
  12941. for ( i = 0; i < nmemb*nfields; i++ )
  12942. WriteStructField(mat,fields[i]);
  12943. break;
  12944. }
  12945. case MAT_C_SPARSE:
  12946. {
  12947. mat_sparse_t *sparse = (mat_sparse_t*)matvar->data;
  12948. nBytes = WriteData(mat,sparse->ir,sparse->nir,MAT_T_INT32);
  12949. if ( nBytes % 8 )
  12950. for ( i = nBytes % 8; i < 8; i++ )
  12951. fwrite(&pad1,1,1,(FILE*)mat->fp);
  12952. nBytes = WriteData(mat,sparse->jc,sparse->njc,MAT_T_INT32);
  12953. if ( nBytes % 8 )
  12954. for ( i = nBytes % 8; i < 8; i++ )
  12955. fwrite(&pad1,1,1,(FILE*)mat->fp);
  12956. if ( matvar->isComplex ) {
  12957. mat_complex_split_t *complex_data = (mat_complex_split_t*)sparse->data;
  12958. nBytes = WriteData(mat,complex_data->Re,sparse->ndata,
  12959. matvar->data_type);
  12960. if ( nBytes % 8 )
  12961. for ( i = nBytes % 8; i < 8; i++ )
  12962. fwrite(&pad1,1,1,(FILE*)mat->fp);
  12963. nBytes = WriteData(mat,complex_data->Im,sparse->ndata,
  12964. matvar->data_type);
  12965. if ( nBytes % 8 )
  12966. for ( i = nBytes % 8; i < 8; i++ )
  12967. fwrite(&pad1,1,1,(FILE*)mat->fp);
  12968. } else {
  12969. nBytes = WriteData(mat,sparse->data,sparse->ndata,
  12970. matvar->data_type);
  12971. if ( nBytes % 8 )
  12972. for ( i = nBytes % 8; i < 8; i++ )
  12973. fwrite(&pad1,1,1,(FILE*)mat->fp);
  12974. }
  12975. }
  12976. case MAT_C_FUNCTION:
  12977. case MAT_C_OBJECT:
  12978. case MAT_C_EMPTY:
  12979. break;
  12980. }
  12981. end = ftell((FILE*)mat->fp);
  12982. if ( start != -1L && end != -1L ) {
  12983. nBytes = (int)(end-start);
  12984. (void)fseek((FILE*)mat->fp,(long)-(nBytes+4),SEEK_CUR);
  12985. fwrite(&nBytes,4,1,(FILE*)mat->fp);
  12986. (void)fseek((FILE*)mat->fp,end,SEEK_SET);
  12987. } else {
  12988. Mat_Critical("Couldn't determine file position");
  12989. }
  12990. return 0;
  12991. }
  12992. #if defined(HAVE_ZLIB)
  12993. /** @brief Writes the header and data for a field of a compressed struct array
  12994. *
  12995. * @ingroup mat_internal
  12996. * @fixme Currently does not work for cell arrays or sparse data
  12997. * @param mat MAT file pointer
  12998. * @param matvar pointer to the mat variable
  12999. * @return number of bytes written to the MAT file
  13000. */
  13001. static size_t
  13002. WriteCompressedStructField(mat_t *mat,matvar_t *matvar,z_streamp z)
  13003. {
  13004. mat_uint32_t array_flags = 0x0;
  13005. mat_int16_t array_name_type = MAT_T_INT8;
  13006. mat_int16_t fieldname_type = MAT_T_INT32;
  13007. mat_int16_t fieldname_data_size = 4;
  13008. int array_flags_type = MAT_T_UINT32, dims_array_type = MAT_T_INT32;
  13009. int array_flags_size = 8, pad4 = 0;
  13010. int nBytes, i, nmemb = 1, nzmax = 0;
  13011. int start = 0;
  13012. mat_uint32_t comp_buf[512];
  13013. mat_uint32_t uncomp_buf[512] = {0,};
  13014. int buf_size = 512;
  13015. size_t byteswritten = 0;
  13016. if ( NULL == mat || NULL == z)
  13017. return 1;
  13018. if ( NULL == matvar ) {
  13019. size_t dims[2] = {0,0};
  13020. byteswritten = Mat_WriteCompressedEmptyVariable5(mat, NULL, 2, dims, z);
  13021. return byteswritten;
  13022. }
  13023. start = ftell((FILE*)mat->fp);
  13024. /* Array Flags */
  13025. array_flags = matvar->class_type & CLASS_TYPE_MASK;
  13026. if ( matvar->isComplex )
  13027. array_flags |= MAT_F_COMPLEX;
  13028. if ( matvar->isGlobal )
  13029. array_flags |= MAT_F_GLOBAL;
  13030. if ( matvar->isLogical )
  13031. array_flags |= MAT_F_LOGICAL;
  13032. if ( matvar->class_type == MAT_C_SPARSE )
  13033. nzmax = ((mat_sparse_t *)matvar->data)->nzmax;
  13034. uncomp_buf[0] = MAT_T_MATRIX;
  13035. uncomp_buf[1] = (int)GetStructFieldBufSize(matvar);
  13036. z->next_in = ZLIB_BYTE_PTR(uncomp_buf);
  13037. z->avail_in = 8;
  13038. do {
  13039. z->next_out = ZLIB_BYTE_PTR(comp_buf);
  13040. z->avail_out = buf_size*sizeof(*comp_buf);
  13041. deflate(z,Z_NO_FLUSH);
  13042. byteswritten += fwrite(comp_buf,1,buf_size*sizeof(*comp_buf)-z->avail_out,
  13043. (FILE*)mat->fp);
  13044. } while ( z->avail_out == 0 );
  13045. uncomp_buf[0] = array_flags_type;
  13046. uncomp_buf[1] = array_flags_size;
  13047. uncomp_buf[2] = array_flags;
  13048. uncomp_buf[3] = nzmax;
  13049. /* Rank and Dimension */
  13050. nBytes = matvar->rank * 4;
  13051. uncomp_buf[4] = dims_array_type;
  13052. uncomp_buf[5] = nBytes;
  13053. for ( i = 0; i < matvar->rank; i++ ) {
  13054. mat_int32_t dim;
  13055. dim = matvar->dims[i];
  13056. nmemb *= dim;
  13057. uncomp_buf[6+i] = dim;
  13058. }
  13059. if ( matvar->rank % 2 != 0 ) {
  13060. uncomp_buf[6+i] = pad4;
  13061. i++;
  13062. }
  13063. z->next_in = ZLIB_BYTE_PTR(uncomp_buf);
  13064. z->avail_in = (6+i)*sizeof(*uncomp_buf);
  13065. do {
  13066. z->next_out = ZLIB_BYTE_PTR(comp_buf);
  13067. z->avail_out = buf_size*sizeof(*comp_buf);
  13068. deflate(z,Z_NO_FLUSH);
  13069. byteswritten += fwrite(comp_buf,1,buf_size*sizeof(*comp_buf)-z->avail_out,
  13070. (FILE*)mat->fp);
  13071. } while ( z->avail_out == 0 );
  13072. /* Name of variable */
  13073. uncomp_buf[0] = array_name_type;
  13074. uncomp_buf[1] = 0;
  13075. z->next_in = ZLIB_BYTE_PTR(uncomp_buf);
  13076. z->avail_in = 8;
  13077. do {
  13078. z->next_out = ZLIB_BYTE_PTR(comp_buf);
  13079. z->avail_out = buf_size*sizeof(*comp_buf);
  13080. deflate(z,Z_NO_FLUSH);
  13081. byteswritten += fwrite(comp_buf,1,buf_size*sizeof(*comp_buf)-z->avail_out,
  13082. (FILE*)mat->fp);
  13083. } while ( z->avail_out == 0 );
  13084. matvar->internal->datapos = ftell((FILE*)mat->fp);
  13085. if ( matvar->internal->datapos == -1L ) {
  13086. Mat_Critical("Couldn't determine file position");
  13087. }
  13088. switch ( matvar->class_type ) {
  13089. case MAT_C_DOUBLE:
  13090. case MAT_C_SINGLE:
  13091. case MAT_C_INT64:
  13092. case MAT_C_UINT64:
  13093. case MAT_C_INT32:
  13094. case MAT_C_UINT32:
  13095. case MAT_C_INT16:
  13096. case MAT_C_UINT16:
  13097. case MAT_C_INT8:
  13098. case MAT_C_UINT8:
  13099. {
  13100. /* WriteCompressedData makes sure uncompressed data is aligned
  13101. * on an 8-byte boundary */
  13102. if ( matvar->isComplex ) {
  13103. mat_complex_split_t *complex_data = (mat_complex_split_t*)matvar->data;
  13104. if ( NULL == matvar->data )
  13105. complex_data = &null_complex_data;
  13106. byteswritten += WriteCompressedData(mat,z,
  13107. complex_data->Re,nmemb,matvar->data_type);
  13108. byteswritten += WriteCompressedData(mat,z,
  13109. complex_data->Im,nmemb,matvar->data_type);
  13110. } else {
  13111. byteswritten += WriteCompressedData(mat,z,
  13112. matvar->data,nmemb,matvar->data_type);
  13113. }
  13114. break;
  13115. }
  13116. case MAT_C_CHAR:
  13117. {
  13118. byteswritten += WriteCompressedCharData(mat,z,matvar->data,
  13119. nmemb,matvar->data_type);
  13120. break;
  13121. }
  13122. case MAT_C_CELL:
  13123. {
  13124. int ncells;
  13125. matvar_t **cells = (matvar_t **)matvar->data;
  13126. /* Check for an empty cell array */
  13127. if ( matvar->nbytes == 0 || matvar->data_size == 0 ||
  13128. matvar->data == NULL )
  13129. break;
  13130. ncells = matvar->nbytes / matvar->data_size;
  13131. for ( i = 0; i < ncells; i++ )
  13132. WriteCompressedCellArrayField(mat,cells[i],z);
  13133. break;
  13134. }
  13135. case MAT_C_STRUCT:
  13136. {
  13137. unsigned char *padzero;
  13138. int fieldname_size, nfields;
  13139. size_t maxlen = 0;
  13140. mat_int32_t array_name_type = MAT_T_INT8;
  13141. matvar_t **fields = (matvar_t **)matvar->data;
  13142. nfields = matvar->internal->num_fields;
  13143. /* Check for a structure with no fields */
  13144. if ( nfields < 1 ) {
  13145. fieldname_size = 1;
  13146. uncomp_buf[0] = (fieldname_data_size << 16) |
  13147. fieldname_type;
  13148. uncomp_buf[1] = fieldname_size;
  13149. uncomp_buf[2] = array_name_type;
  13150. uncomp_buf[3] = 0;
  13151. z->next_in = ZLIB_BYTE_PTR(uncomp_buf);
  13152. z->avail_in = 16;
  13153. do {
  13154. z->next_out = ZLIB_BYTE_PTR(comp_buf);
  13155. z->avail_out = buf_size*sizeof(*comp_buf);
  13156. deflate(z,Z_NO_FLUSH);
  13157. byteswritten += fwrite(comp_buf,1,buf_size*
  13158. sizeof(*comp_buf)-z->avail_out,(FILE*)mat->fp);
  13159. } while ( z->avail_out == 0 );
  13160. break;
  13161. }
  13162. for ( i = 0; i < nfields; i++ ) {
  13163. size_t len = strlen(matvar->internal->fieldnames[i]);
  13164. if ( len > maxlen )
  13165. maxlen = len;
  13166. }
  13167. maxlen++;
  13168. fieldname_size = maxlen;
  13169. while ( nfields*fieldname_size % 8 != 0 )
  13170. fieldname_size++;
  13171. uncomp_buf[0] = (fieldname_data_size << 16) | fieldname_type;
  13172. uncomp_buf[1] = fieldname_size;
  13173. uncomp_buf[2] = array_name_type;
  13174. uncomp_buf[3] = nfields*fieldname_size;
  13175. padzero = (unsigned char*)calloc(fieldname_size,1);
  13176. z->next_in = ZLIB_BYTE_PTR(uncomp_buf);
  13177. z->avail_in = 16;
  13178. do {
  13179. z->next_out = ZLIB_BYTE_PTR(comp_buf);
  13180. z->avail_out = buf_size*sizeof(*comp_buf);
  13181. deflate(z,Z_NO_FLUSH);
  13182. byteswritten += fwrite(comp_buf,1,
  13183. buf_size*sizeof(*comp_buf)-z->avail_out,(FILE*)mat->fp);
  13184. } while ( z->avail_out == 0 );
  13185. for ( i = 0; i < nfields; i++ ) {
  13186. size_t len = strlen(matvar->internal->fieldnames[i]);
  13187. memset(padzero,'\0',fieldname_size);
  13188. memcpy(padzero,matvar->internal->fieldnames[i],len);
  13189. z->next_in = ZLIB_BYTE_PTR(padzero);
  13190. z->avail_in = fieldname_size;
  13191. do {
  13192. z->next_out = ZLIB_BYTE_PTR(comp_buf);
  13193. z->avail_out = buf_size*sizeof(*comp_buf);
  13194. deflate(z,Z_NO_FLUSH);
  13195. byteswritten += fwrite(comp_buf,1,
  13196. buf_size*sizeof(*comp_buf)-z->avail_out,(FILE*)mat->fp);
  13197. } while ( z->avail_out == 0 );
  13198. }
  13199. free(padzero);
  13200. for ( i = 0; i < nmemb*nfields; i++ )
  13201. byteswritten +=
  13202. WriteCompressedStructField(mat,fields[i],z);
  13203. break;
  13204. }
  13205. case MAT_C_SPARSE:
  13206. {
  13207. mat_sparse_t *sparse = (mat_sparse_t*)matvar->data;
  13208. byteswritten += WriteCompressedData(mat,z,sparse->ir,
  13209. sparse->nir,MAT_T_INT32);
  13210. byteswritten += WriteCompressedData(mat,z,sparse->jc,
  13211. sparse->njc,MAT_T_INT32);
  13212. if ( matvar->isComplex ) {
  13213. mat_complex_split_t *complex_data = (mat_complex_split_t*)sparse->data;
  13214. byteswritten += WriteCompressedData(mat,z,
  13215. complex_data->Re,sparse->ndata,matvar->data_type);
  13216. byteswritten += WriteCompressedData(mat,z,
  13217. complex_data->Im,sparse->ndata,matvar->data_type);
  13218. } else {
  13219. byteswritten += WriteCompressedData(mat,z,
  13220. sparse->data,sparse->ndata,matvar->data_type);
  13221. }
  13222. break;
  13223. }
  13224. case MAT_C_FUNCTION:
  13225. case MAT_C_OBJECT:
  13226. case MAT_C_EMPTY:
  13227. break;
  13228. }
  13229. return byteswritten;
  13230. }
  13231. #endif
  13232. static size_t
  13233. Mat_WriteEmptyVariable5(mat_t *mat,const char *name,int rank,size_t *dims)
  13234. {
  13235. mat_uint32_t array_flags = 0x0;
  13236. mat_int32_t array_name_type = MAT_T_INT8, matrix_type = MAT_T_MATRIX;
  13237. int array_flags_type = MAT_T_UINT32, dims_array_type = MAT_T_INT32;
  13238. int array_flags_size = 8, pad4 = 0, nBytes, i, nmemb = 1;
  13239. mat_int8_t pad1 = 0;
  13240. size_t byteswritten = 0;
  13241. int start = 0, end = 0;
  13242. fwrite(&matrix_type,4,1,(FILE*)mat->fp);
  13243. fwrite(&pad4,4,1,(FILE*)mat->fp);
  13244. start = ftell((FILE*)mat->fp);
  13245. /* Array Flags */
  13246. array_flags = MAT_C_DOUBLE;
  13247. if ( mat->byteswap )
  13248. array_flags = Mat_int32Swap((mat_int32_t*)&array_flags);
  13249. byteswritten += fwrite(&array_flags_type,4,1,(FILE*)mat->fp);
  13250. byteswritten += fwrite(&array_flags_size,4,1,(FILE*)mat->fp);
  13251. byteswritten += fwrite(&array_flags,4,1,(FILE*)mat->fp);
  13252. byteswritten += fwrite(&pad4,4,1,(FILE*)mat->fp);
  13253. /* Rank and Dimension */
  13254. nBytes = rank * 4;
  13255. byteswritten += fwrite(&dims_array_type,4,1,(FILE*)mat->fp);
  13256. byteswritten += fwrite(&nBytes,4,1,(FILE*)mat->fp);
  13257. for ( i = 0; i < rank; i++ ) {
  13258. mat_int32_t dim;
  13259. dim = dims[i];
  13260. nmemb *= dim;
  13261. byteswritten += fwrite(&dim,4,1,(FILE*)mat->fp);
  13262. }
  13263. if ( rank % 2 != 0 )
  13264. byteswritten += fwrite(&pad4,4,1,(FILE*)mat->fp);
  13265. if ( NULL == name ) {
  13266. /* Name of variable */
  13267. byteswritten += fwrite(&array_name_type,4,1,(FILE*)mat->fp);
  13268. byteswritten += fwrite(&pad4,4,1,(FILE*)mat->fp);
  13269. } else {
  13270. mat_int32_t array_name_type = MAT_T_INT8;
  13271. mat_int32_t array_name_len = strlen(name);
  13272. /* Name of variable */
  13273. if ( array_name_len <= 4 ) {
  13274. mat_int8_t pad1 = 0;
  13275. array_name_type = (array_name_len << 16) | array_name_type;
  13276. byteswritten += fwrite(&array_name_type,4,1,(FILE*)mat->fp);
  13277. byteswritten += fwrite(name,1,array_name_len,(FILE*)mat->fp);
  13278. for ( i = array_name_len; i < 4; i++ )
  13279. byteswritten += fwrite(&pad1,1,1,(FILE*)mat->fp);
  13280. } else {
  13281. byteswritten += fwrite(&array_name_type,4,1,(FILE*)mat->fp);
  13282. byteswritten += fwrite(&array_name_len,4,1,(FILE*)mat->fp);
  13283. byteswritten += fwrite(name,1,array_name_len,(FILE*)mat->fp);
  13284. if ( array_name_len % 8 )
  13285. for ( i = array_name_len % 8; i < 8; i++ )
  13286. byteswritten += fwrite(&pad1,1,1,(FILE*)mat->fp);
  13287. }
  13288. }
  13289. nBytes = WriteData(mat,NULL,0,MAT_T_DOUBLE);
  13290. byteswritten += nBytes;
  13291. if ( nBytes % 8 )
  13292. for ( i = nBytes % 8; i < 8; i++ )
  13293. byteswritten += fwrite(&pad1,1,1,(FILE*)mat->fp);
  13294. end = ftell((FILE*)mat->fp);
  13295. if ( start != -1L && end != -1L ) {
  13296. nBytes = (int)(end-start);
  13297. (void)fseek((FILE*)mat->fp,(long)-(nBytes+4),SEEK_CUR);
  13298. fwrite(&nBytes,4,1,(FILE*)mat->fp);
  13299. (void)fseek((FILE*)mat->fp,end,SEEK_SET);
  13300. } else {
  13301. Mat_Critical("Couldn't determine file position");
  13302. }
  13303. return byteswritten;
  13304. }
  13305. #if defined(HAVE_ZLIB)
  13306. static size_t
  13307. Mat_WriteCompressedEmptyVariable5(mat_t *mat,const char *name,int rank,
  13308. size_t *dims,z_streamp z)
  13309. {
  13310. mat_uint32_t array_flags = 0x0;
  13311. mat_int16_t array_name_type = MAT_T_INT8;
  13312. int array_flags_type = MAT_T_UINT32, dims_array_type = MAT_T_INT32;
  13313. int array_flags_size = 8, pad4 = 0;
  13314. int nBytes, i, nmemb = 1;
  13315. mat_uint32_t comp_buf[512];
  13316. mat_uint32_t uncomp_buf[512] = {0,};
  13317. int buf_size = 512;
  13318. size_t byteswritten = 0, buf_size_bytes;
  13319. if ( NULL == mat || NULL == z)
  13320. return 1;
  13321. buf_size_bytes = buf_size*sizeof(*comp_buf);
  13322. /* Array Flags */
  13323. array_flags = MAT_C_DOUBLE;
  13324. uncomp_buf[0] = MAT_T_MATRIX;
  13325. uncomp_buf[1] = (int)GetEmptyMatrixMaxBufSize(name,rank);
  13326. z->next_in = ZLIB_BYTE_PTR(uncomp_buf);
  13327. z->avail_in = 8;
  13328. do {
  13329. z->next_out = ZLIB_BYTE_PTR(comp_buf);
  13330. z->avail_out = buf_size_bytes;
  13331. deflate(z,Z_NO_FLUSH);
  13332. byteswritten += fwrite(comp_buf,1,buf_size_bytes-z->avail_out,(FILE*)mat->fp);
  13333. } while ( z->avail_out == 0 );
  13334. uncomp_buf[0] = array_flags_type;
  13335. uncomp_buf[1] = array_flags_size;
  13336. uncomp_buf[2] = array_flags;
  13337. uncomp_buf[3] = 0;
  13338. /* Rank and Dimension */
  13339. nBytes = rank * 4;
  13340. uncomp_buf[4] = dims_array_type;
  13341. uncomp_buf[5] = nBytes;
  13342. for ( i = 0; i < rank; i++ ) {
  13343. mat_int32_t dim;
  13344. dim = dims[i];
  13345. nmemb *= dim;
  13346. uncomp_buf[6+i] = dim;
  13347. }
  13348. if ( rank % 2 != 0 ) {
  13349. uncomp_buf[6+i] = pad4;
  13350. i++;
  13351. }
  13352. z->next_in = ZLIB_BYTE_PTR(uncomp_buf);
  13353. z->avail_in = (6+i)*sizeof(*uncomp_buf);
  13354. do {
  13355. z->next_out = ZLIB_BYTE_PTR(comp_buf);
  13356. z->avail_out = buf_size_bytes;
  13357. deflate(z,Z_NO_FLUSH);
  13358. byteswritten += fwrite(comp_buf,1,buf_size_bytes-z->avail_out,(FILE*)mat->fp);
  13359. } while ( z->avail_out == 0 );
  13360. /* Name of variable */
  13361. if ( NULL == name ) {
  13362. uncomp_buf[0] = array_name_type;
  13363. uncomp_buf[1] = 0;
  13364. z->next_in = ZLIB_BYTE_PTR(uncomp_buf);
  13365. z->avail_in = 8;
  13366. do {
  13367. z->next_out = ZLIB_BYTE_PTR(comp_buf);
  13368. z->avail_out = buf_size_bytes;
  13369. deflate(z,Z_NO_FLUSH);
  13370. byteswritten += fwrite(comp_buf,1,buf_size_bytes-z->avail_out,(FILE*)mat->fp);
  13371. } while ( z->avail_out == 0 );
  13372. } else {
  13373. if ( strlen(name) <= 4 ) {
  13374. mat_int16_t array_name_len = (mat_int16_t)strlen(name);
  13375. mat_int16_t array_name_type = MAT_T_INT8;
  13376. memset(uncomp_buf,0,8);
  13377. uncomp_buf[0] = (array_name_len << 16) | array_name_type;
  13378. memcpy(uncomp_buf+1,name,array_name_len);
  13379. if ( array_name_len % 4 )
  13380. array_name_len += 4-(array_name_len % 4);
  13381. z->next_in = ZLIB_BYTE_PTR(uncomp_buf);
  13382. z->avail_in = 8;
  13383. do {
  13384. z->next_out = ZLIB_BYTE_PTR(comp_buf);
  13385. z->avail_out = buf_size_bytes;
  13386. deflate(z,Z_NO_FLUSH);
  13387. byteswritten += fwrite(comp_buf,1,buf_size_bytes-z->avail_out,
  13388. (FILE*)mat->fp);
  13389. } while ( z->avail_out == 0 );
  13390. } else {
  13391. mat_int32_t array_name_len = (mat_int32_t)strlen(name);
  13392. mat_int32_t array_name_type = MAT_T_INT8;
  13393. memset(uncomp_buf,0,buf_size*sizeof(*uncomp_buf));
  13394. uncomp_buf[0] = array_name_type;
  13395. uncomp_buf[1] = array_name_len;
  13396. memcpy(uncomp_buf+2,name,array_name_len);
  13397. if ( array_name_len % 8 )
  13398. array_name_len += 8-(array_name_len % 8);
  13399. z->next_in = ZLIB_BYTE_PTR(uncomp_buf);
  13400. z->avail_in = 8+array_name_len;
  13401. do {
  13402. z->next_out = ZLIB_BYTE_PTR(comp_buf);
  13403. z->avail_out = buf_size_bytes;
  13404. deflate(z,Z_NO_FLUSH);
  13405. byteswritten += fwrite(comp_buf,1,buf_size_bytes-z->avail_out,
  13406. (FILE*)mat->fp);
  13407. } while ( z->avail_out == 0 );
  13408. }
  13409. }
  13410. byteswritten += WriteCompressedData(mat,z,NULL,0,MAT_T_DOUBLE);
  13411. return byteswritten;
  13412. }
  13413. #endif
  13414. /** @if mat_devman
  13415. * @brief Reads a data element including tag and data
  13416. *
  13417. * @ingroup mat_internal
  13418. * @param mat MAT file pointer
  13419. * @param matvar MAT variable pointer
  13420. * @param data Pointer to store the data
  13421. * @param N number of data elements allocated for the pointer
  13422. * @endif
  13423. */
  13424. void
  13425. Mat_VarReadNumeric5(mat_t *mat,matvar_t *matvar,void *data,size_t N)
  13426. {
  13427. int nBytes = 0, data_in_tag = 0;
  13428. enum matio_types packed_type = MAT_T_UNKNOWN;
  13429. mat_uint32_t tag[2];
  13430. if ( matvar->compression ) {
  13431. #if defined(HAVE_ZLIB)
  13432. matvar->internal->z->avail_in = 0;
  13433. InflateDataType(mat,matvar->internal->z,tag);
  13434. if ( mat->byteswap )
  13435. (void)Mat_uint32Swap(tag);
  13436. packed_type = TYPE_FROM_TAG(tag[0]);
  13437. if ( tag[0] & 0xffff0000 ) { /* Data is in the tag */
  13438. data_in_tag = 1;
  13439. nBytes = (tag[0] & 0xffff0000) >> 16;
  13440. } else {
  13441. data_in_tag = 0;
  13442. InflateDataType(mat,matvar->internal->z,tag+1);
  13443. if ( mat->byteswap )
  13444. (void)Mat_uint32Swap(tag+1);
  13445. nBytes = tag[1];
  13446. }
  13447. #endif
  13448. } else {
  13449. size_t bytesread = fread(tag,4,1,(FILE*)mat->fp);
  13450. if ( mat->byteswap )
  13451. (void)Mat_uint32Swap(tag);
  13452. packed_type = TYPE_FROM_TAG(tag[0]);
  13453. if ( tag[0] & 0xffff0000 ) { /* Data is in the tag */
  13454. data_in_tag = 1;
  13455. nBytes = (tag[0] & 0xffff0000) >> 16;
  13456. } else {
  13457. data_in_tag = 0;
  13458. bytesread += fread(tag+1,4,1,(FILE*)mat->fp);
  13459. if ( mat->byteswap )
  13460. (void)Mat_uint32Swap(tag+1);
  13461. nBytes = tag[1];
  13462. }
  13463. }
  13464. if ( nBytes == 0 ) {
  13465. matvar->nbytes = 0;
  13466. return;
  13467. }
  13468. if ( matvar->compression == MAT_COMPRESSION_NONE) {
  13469. switch ( matvar->class_type ) {
  13470. case MAT_C_DOUBLE:
  13471. nBytes = ReadDoubleData(mat,(double*)data,packed_type,N);
  13472. break;
  13473. case MAT_C_SINGLE:
  13474. nBytes = ReadSingleData(mat,(float*)data,packed_type,N);
  13475. break;
  13476. case MAT_C_INT64:
  13477. #ifdef HAVE_MATIO_INT64_T
  13478. nBytes = ReadInt64Data(mat,(mat_int64_t*)data,packed_type,N);
  13479. #endif
  13480. break;
  13481. case MAT_C_UINT64:
  13482. #ifdef HAVE_MATIO_UINT64_T
  13483. nBytes = ReadUInt64Data(mat,(mat_uint64_t*)data,packed_type,N);
  13484. #endif
  13485. break;
  13486. case MAT_C_INT32:
  13487. nBytes = ReadInt32Data(mat,(mat_int32_t*)data,packed_type,N);
  13488. break;
  13489. case MAT_C_UINT32:
  13490. nBytes = ReadUInt32Data(mat,(mat_uint32_t*)data,packed_type,N);
  13491. break;
  13492. case MAT_C_INT16:
  13493. nBytes = ReadInt16Data(mat,(mat_int16_t*)data,packed_type,N);
  13494. break;
  13495. case MAT_C_UINT16:
  13496. nBytes = ReadUInt16Data(mat,(mat_uint16_t*)data,packed_type,N);
  13497. break;
  13498. case MAT_C_INT8:
  13499. nBytes = ReadInt8Data(mat,(mat_int8_t*)data,packed_type,N);
  13500. break;
  13501. case MAT_C_UINT8:
  13502. nBytes = ReadUInt8Data(mat,(mat_uint8_t*)data,packed_type,N);
  13503. break;
  13504. default:
  13505. break;
  13506. }
  13507. /*
  13508. * If the data was in the tag we started on a 4-byte
  13509. * boundary so add 4 to make it an 8-byte
  13510. */
  13511. if ( data_in_tag )
  13512. nBytes+=4;
  13513. if ( (nBytes % 8) != 0 )
  13514. (void)fseek((FILE*)mat->fp,8-(nBytes % 8),SEEK_CUR);
  13515. #if defined(HAVE_ZLIB)
  13516. } else if ( matvar->compression == MAT_COMPRESSION_ZLIB ) {
  13517. switch ( matvar->class_type ) {
  13518. case MAT_C_DOUBLE:
  13519. nBytes = ReadCompressedDoubleData(mat,matvar->internal->z,(double*)data,
  13520. packed_type,N);
  13521. break;
  13522. case MAT_C_SINGLE:
  13523. nBytes = ReadCompressedSingleData(mat,matvar->internal->z,(float*)data,
  13524. packed_type,N);
  13525. break;
  13526. case MAT_C_INT64:
  13527. #ifdef HAVE_MATIO_INT64_T
  13528. nBytes = ReadCompressedInt64Data(mat,matvar->internal->z,(mat_int64_t*)data,
  13529. packed_type,N);
  13530. #endif
  13531. break;
  13532. case MAT_C_UINT64:
  13533. #ifdef HAVE_MATIO_UINT64_T
  13534. nBytes = ReadCompressedUInt64Data(mat,matvar->internal->z,(mat_uint64_t*)data,
  13535. packed_type,N);
  13536. #endif
  13537. break;
  13538. case MAT_C_INT32:
  13539. nBytes = ReadCompressedInt32Data(mat,matvar->internal->z,(mat_int32_t*)data,
  13540. packed_type,N);
  13541. break;
  13542. case MAT_C_UINT32:
  13543. nBytes = ReadCompressedUInt32Data(mat,matvar->internal->z,(mat_uint32_t*)data,
  13544. packed_type,N);
  13545. break;
  13546. case MAT_C_INT16:
  13547. nBytes = ReadCompressedInt16Data(mat,matvar->internal->z,(mat_int16_t*)data,
  13548. packed_type,N);
  13549. break;
  13550. case MAT_C_UINT16:
  13551. nBytes = ReadCompressedUInt16Data(mat,matvar->internal->z,(mat_uint16_t*)data,
  13552. packed_type,N);
  13553. break;
  13554. case MAT_C_INT8:
  13555. nBytes = ReadCompressedInt8Data(mat,matvar->internal->z,(mat_int8_t*)data,
  13556. packed_type,N);
  13557. break;
  13558. case MAT_C_UINT8:
  13559. nBytes = ReadCompressedUInt8Data(mat,matvar->internal->z,(mat_uint8_t*)data,
  13560. packed_type,N);
  13561. break;
  13562. default:
  13563. break;
  13564. }
  13565. /*
  13566. * If the data was in the tag we started on a 4-byte
  13567. * boundary so add 4 to make it an 8-byte
  13568. */
  13569. if ( data_in_tag )
  13570. nBytes+=4;
  13571. if ( (nBytes % 8) != 0 )
  13572. InflateSkip(mat,matvar->internal->z,8-(nBytes % 8));
  13573. #endif
  13574. }
  13575. }
  13576. /** @if mat_devman
  13577. * @brief Reads the data of a version 5 MAT variable
  13578. *
  13579. * @ingroup mat_internal
  13580. * @param mat MAT file pointer
  13581. * @param matvar MAT variable pointer to read the data
  13582. * @endif
  13583. */
  13584. static void
  13585. Read5(mat_t *mat, matvar_t *matvar)
  13586. {
  13587. int nBytes = 0, len = 1, i, byteswap, data_in_tag = 0;
  13588. enum matio_types packed_type = MAT_T_UNKNOWN;
  13589. int fpos;
  13590. mat_uint32_t tag[2];
  13591. size_t bytesread = 0;
  13592. if ( matvar == NULL )
  13593. return;
  13594. else if ( matvar->rank == 0 ) /* An empty data set */
  13595. return;
  13596. #if defined(HAVE_ZLIB)
  13597. else if ( NULL != matvar->internal->data ) {
  13598. /* Data already read in ReadNextStructField or ReadNextCell */
  13599. matvar->data = matvar->internal->data;
  13600. matvar->internal->data = NULL;
  13601. return;
  13602. }
  13603. #endif
  13604. fpos = ftell((FILE*)mat->fp);
  13605. if ( fpos == -1L ) {
  13606. Mat_Critical("Couldn't determine file position");
  13607. return;
  13608. }
  13609. len = 1;
  13610. byteswap = mat->byteswap;
  13611. for ( i = 0; i < matvar->rank; i++ )
  13612. len *= matvar->dims[i];
  13613. switch ( matvar->class_type ) {
  13614. case MAT_C_EMPTY:
  13615. matvar->nbytes = 0;
  13616. matvar->data_size = sizeof(double);
  13617. matvar->data_type = MAT_T_DOUBLE;
  13618. matvar->class_type = MAT_C_EMPTY;
  13619. matvar->rank = 2;
  13620. matvar->dims = (size_t*)malloc(matvar->rank*sizeof(*(matvar->dims)));
  13621. matvar->dims[0] = 0;
  13622. matvar->dims[1] = 0;
  13623. break;
  13624. case MAT_C_DOUBLE:
  13625. (void)fseek((FILE*)mat->fp,matvar->internal->datapos,SEEK_SET);
  13626. matvar->data_size = sizeof(double);
  13627. matvar->data_type = MAT_T_DOUBLE;
  13628. if ( matvar->isComplex ) {
  13629. mat_complex_split_t *complex_data;
  13630. matvar->nbytes = len*matvar->data_size;
  13631. complex_data = (mat_complex_split_t*)malloc(sizeof(*complex_data));
  13632. if ( NULL == complex_data ) {
  13633. Mat_Critical("Failed to allocate %d bytes",sizeof(*complex_data));
  13634. break;
  13635. }
  13636. complex_data->Re = malloc(matvar->nbytes);
  13637. complex_data->Im = malloc(matvar->nbytes);
  13638. if ( NULL == complex_data->Re || NULL == complex_data->Im ) {
  13639. if ( NULL != complex_data->Re )
  13640. free(complex_data->Re);
  13641. if ( NULL != complex_data->Im )
  13642. free(complex_data->Im);
  13643. free(complex_data);
  13644. Mat_Critical("Failed to allocate %d bytes",2*matvar->nbytes);
  13645. break;
  13646. }
  13647. Mat_VarReadNumeric5(mat,matvar,complex_data->Re,len);
  13648. Mat_VarReadNumeric5(mat,matvar,complex_data->Im,len);
  13649. matvar->data = complex_data;
  13650. } else {
  13651. matvar->nbytes = len*matvar->data_size;
  13652. matvar->data = malloc(matvar->nbytes);
  13653. if ( !matvar->data ) {
  13654. Mat_Critical("Failed to allocate %d bytes",matvar->nbytes);
  13655. break;
  13656. }
  13657. Mat_VarReadNumeric5(mat,matvar,matvar->data,len);
  13658. }
  13659. break;
  13660. case MAT_C_SINGLE:
  13661. (void)fseek((FILE*)mat->fp,matvar->internal->datapos,SEEK_SET);
  13662. matvar->data_size = sizeof(float);
  13663. matvar->data_type = MAT_T_SINGLE;
  13664. if ( matvar->isComplex ) {
  13665. mat_complex_split_t *complex_data;
  13666. matvar->nbytes = len*matvar->data_size;
  13667. complex_data = (mat_complex_split_t*)malloc(sizeof(*complex_data));
  13668. if ( NULL == complex_data ) {
  13669. Mat_Critical("Failed to allocate %d bytes",sizeof(*complex_data));
  13670. break;
  13671. }
  13672. complex_data->Re = malloc(matvar->nbytes);
  13673. complex_data->Im = malloc(matvar->nbytes);
  13674. if ( NULL == complex_data->Re || NULL == complex_data->Im ) {
  13675. if ( NULL != complex_data->Re )
  13676. free(complex_data->Re);
  13677. if ( NULL != complex_data->Im )
  13678. free(complex_data->Im);
  13679. free(complex_data);
  13680. Mat_Critical("Failed to allocate %d bytes",2*matvar->nbytes);
  13681. break;
  13682. }
  13683. Mat_VarReadNumeric5(mat,matvar,complex_data->Re,len);
  13684. Mat_VarReadNumeric5(mat,matvar,complex_data->Im,len);
  13685. matvar->data = complex_data;
  13686. } else {
  13687. matvar->nbytes = len*matvar->data_size;
  13688. matvar->data = malloc(matvar->nbytes);
  13689. if ( !matvar->data ) {
  13690. Mat_Critical("Failed to allocate %d bytes",matvar->nbytes);
  13691. break;
  13692. }
  13693. Mat_VarReadNumeric5(mat,matvar,matvar->data,len);
  13694. }
  13695. break;
  13696. case MAT_C_INT64:
  13697. #ifdef HAVE_MATIO_INT64_T
  13698. (void)fseek((FILE*)mat->fp,matvar->internal->datapos,SEEK_SET);
  13699. matvar->data_size = sizeof(mat_int64_t);
  13700. matvar->data_type = MAT_T_INT64;
  13701. if ( matvar->isComplex ) {
  13702. mat_complex_split_t *complex_data;
  13703. matvar->nbytes = len*matvar->data_size;
  13704. complex_data = (mat_complex_split_t*)malloc(sizeof(*complex_data));
  13705. if ( NULL == complex_data ) {
  13706. Mat_Critical("Failed to allocate %d bytes",sizeof(*complex_data));
  13707. break;
  13708. }
  13709. complex_data->Re = malloc(matvar->nbytes);
  13710. complex_data->Im = malloc(matvar->nbytes);
  13711. if ( NULL == complex_data->Re || NULL == complex_data->Im ) {
  13712. if ( NULL != complex_data->Re )
  13713. free(complex_data->Re);
  13714. if ( NULL != complex_data->Im )
  13715. free(complex_data->Im);
  13716. free(complex_data);
  13717. Mat_Critical("Failed to allocate %d bytes",2*matvar->nbytes);
  13718. break;
  13719. }
  13720. Mat_VarReadNumeric5(mat,matvar,complex_data->Re,len);
  13721. Mat_VarReadNumeric5(mat,matvar,complex_data->Im,len);
  13722. matvar->data = complex_data;
  13723. } else {
  13724. matvar->nbytes = len*matvar->data_size;
  13725. matvar->data = malloc(matvar->nbytes);
  13726. if ( !matvar->data ) {
  13727. Mat_Critical("Failed to allocate %d bytes",matvar->nbytes);
  13728. break;
  13729. }
  13730. Mat_VarReadNumeric5(mat,matvar,matvar->data,len);
  13731. }
  13732. #endif
  13733. break;
  13734. case MAT_C_UINT64:
  13735. #ifdef HAVE_MATIO_UINT64_T
  13736. (void)fseek((FILE*)mat->fp,matvar->internal->datapos,SEEK_SET);
  13737. matvar->data_size = sizeof(mat_uint64_t);
  13738. matvar->data_type = MAT_T_UINT64;
  13739. if ( matvar->isComplex ) {
  13740. mat_complex_split_t *complex_data;
  13741. matvar->nbytes = len*matvar->data_size;
  13742. complex_data = (mat_complex_split_t*)malloc(sizeof(*complex_data));
  13743. if ( NULL == complex_data ) {
  13744. Mat_Critical("Failed to allocate %d bytes",sizeof(*complex_data));
  13745. break;
  13746. }
  13747. complex_data->Re = malloc(matvar->nbytes);
  13748. complex_data->Im = malloc(matvar->nbytes);
  13749. if ( NULL == complex_data->Re || NULL == complex_data->Im ) {
  13750. if ( NULL != complex_data->Re )
  13751. free(complex_data->Re);
  13752. if ( NULL != complex_data->Im )
  13753. free(complex_data->Im);
  13754. free(complex_data);
  13755. Mat_Critical("Failed to allocate %d bytes",2*matvar->nbytes);
  13756. break;
  13757. }
  13758. Mat_VarReadNumeric5(mat,matvar,complex_data->Re,len);
  13759. Mat_VarReadNumeric5(mat,matvar,complex_data->Im,len);
  13760. matvar->data = complex_data;
  13761. } else {
  13762. matvar->nbytes = len*matvar->data_size;
  13763. matvar->data = malloc(matvar->nbytes);
  13764. if ( !matvar->data ) {
  13765. Mat_Critical("Failed to allocate %d bytes",matvar->nbytes);
  13766. break;
  13767. }
  13768. Mat_VarReadNumeric5(mat,matvar,matvar->data,len);
  13769. }
  13770. #endif
  13771. break;
  13772. case MAT_C_INT32:
  13773. (void)fseek((FILE*)mat->fp,matvar->internal->datapos,SEEK_SET);
  13774. matvar->data_size = sizeof(mat_int32_t);
  13775. matvar->data_type = MAT_T_INT32;
  13776. if ( matvar->isComplex ) {
  13777. mat_complex_split_t *complex_data;
  13778. matvar->nbytes = len*matvar->data_size;
  13779. complex_data = (mat_complex_split_t*)malloc(sizeof(*complex_data));
  13780. if ( NULL == complex_data ) {
  13781. Mat_Critical("Failed to allocate %d bytes",sizeof(*complex_data));
  13782. break;
  13783. }
  13784. complex_data->Re = malloc(matvar->nbytes);
  13785. complex_data->Im = malloc(matvar->nbytes);
  13786. if ( NULL == complex_data->Re || NULL == complex_data->Im ) {
  13787. if ( NULL != complex_data->Re )
  13788. free(complex_data->Re);
  13789. if ( NULL != complex_data->Im )
  13790. free(complex_data->Im);
  13791. free(complex_data);
  13792. Mat_Critical("Failed to allocate %d bytes",2*matvar->nbytes);
  13793. break;
  13794. }
  13795. Mat_VarReadNumeric5(mat,matvar,complex_data->Re,len);
  13796. Mat_VarReadNumeric5(mat,matvar,complex_data->Im,len);
  13797. matvar->data = complex_data;
  13798. } else {
  13799. matvar->nbytes = len*matvar->data_size;
  13800. matvar->data = malloc(matvar->nbytes);
  13801. if ( !matvar->data ) {
  13802. Mat_Critical("Failed to allocate %d bytes",matvar->nbytes);
  13803. break;
  13804. }
  13805. Mat_VarReadNumeric5(mat,matvar,matvar->data,len);
  13806. }
  13807. break;
  13808. case MAT_C_UINT32:
  13809. (void)fseek((FILE*)mat->fp,matvar->internal->datapos,SEEK_SET);
  13810. matvar->data_size = sizeof(mat_uint32_t);
  13811. matvar->data_type = MAT_T_UINT32;
  13812. if ( matvar->isComplex ) {
  13813. mat_complex_split_t *complex_data;
  13814. matvar->nbytes = len*matvar->data_size;
  13815. complex_data = (mat_complex_split_t*)malloc(sizeof(*complex_data));
  13816. if ( NULL == complex_data ) {
  13817. Mat_Critical("Failed to allocate %d bytes",sizeof(*complex_data));
  13818. break;
  13819. }
  13820. complex_data->Re = malloc(matvar->nbytes);
  13821. complex_data->Im = malloc(matvar->nbytes);
  13822. if ( NULL == complex_data->Re || NULL == complex_data->Im ) {
  13823. if ( NULL != complex_data->Re )
  13824. free(complex_data->Re);
  13825. if ( NULL != complex_data->Im )
  13826. free(complex_data->Im);
  13827. free(complex_data);
  13828. Mat_Critical("Failed to allocate %d bytes",2*matvar->nbytes);
  13829. break;
  13830. }
  13831. Mat_VarReadNumeric5(mat,matvar,complex_data->Re,len);
  13832. Mat_VarReadNumeric5(mat,matvar,complex_data->Im,len);
  13833. matvar->data = complex_data;
  13834. } else {
  13835. matvar->nbytes = len*matvar->data_size;
  13836. matvar->data = malloc(matvar->nbytes);
  13837. if ( !matvar->data ) {
  13838. Mat_Critical("Failed to allocate %d bytes",matvar->nbytes);
  13839. break;
  13840. }
  13841. Mat_VarReadNumeric5(mat,matvar,matvar->data,len);
  13842. }
  13843. break;
  13844. case MAT_C_INT16:
  13845. (void)fseek((FILE*)mat->fp,matvar->internal->datapos,SEEK_SET);
  13846. matvar->data_size = sizeof(mat_int16_t);
  13847. matvar->data_type = MAT_T_INT16;
  13848. if ( matvar->isComplex ) {
  13849. mat_complex_split_t *complex_data;
  13850. matvar->nbytes = len*matvar->data_size;
  13851. complex_data = (mat_complex_split_t*)malloc(sizeof(*complex_data));
  13852. if ( NULL == complex_data ) {
  13853. Mat_Critical("Failed to allocate %d bytes",sizeof(*complex_data));
  13854. break;
  13855. }
  13856. complex_data->Re = malloc(matvar->nbytes);
  13857. complex_data->Im = malloc(matvar->nbytes);
  13858. if ( NULL == complex_data->Re || NULL == complex_data->Im ) {
  13859. if ( NULL != complex_data->Re )
  13860. free(complex_data->Re);
  13861. if ( NULL != complex_data->Im )
  13862. free(complex_data->Im);
  13863. free(complex_data);
  13864. Mat_Critical("Failed to allocate %d bytes",2*matvar->nbytes);
  13865. break;
  13866. }
  13867. Mat_VarReadNumeric5(mat,matvar,complex_data->Re,len);
  13868. Mat_VarReadNumeric5(mat,matvar,complex_data->Im,len);
  13869. matvar->data = complex_data;
  13870. } else {
  13871. matvar->nbytes = len*matvar->data_size;
  13872. matvar->data = malloc(matvar->nbytes);
  13873. if ( !matvar->data ) {
  13874. Mat_Critical("Failed to allocate %d bytes",matvar->nbytes);
  13875. break;
  13876. }
  13877. Mat_VarReadNumeric5(mat,matvar,matvar->data,len);
  13878. }
  13879. break;
  13880. case MAT_C_UINT16:
  13881. (void)fseek((FILE*)mat->fp,matvar->internal->datapos,SEEK_SET);
  13882. matvar->data_size = sizeof(mat_uint16_t);
  13883. matvar->data_type = MAT_T_UINT16;
  13884. if ( matvar->isComplex ) {
  13885. mat_complex_split_t *complex_data;
  13886. matvar->nbytes = len*matvar->data_size;
  13887. complex_data = (mat_complex_split_t*)malloc(sizeof(*complex_data));
  13888. if ( NULL == complex_data ) {
  13889. Mat_Critical("Failed to allocate %d bytes",sizeof(*complex_data));
  13890. break;
  13891. }
  13892. complex_data->Re = malloc(matvar->nbytes);
  13893. complex_data->Im = malloc(matvar->nbytes);
  13894. if ( NULL == complex_data->Re || NULL == complex_data->Im ) {
  13895. if ( NULL != complex_data->Re )
  13896. free(complex_data->Re);
  13897. if ( NULL != complex_data->Im )
  13898. free(complex_data->Im);
  13899. free(complex_data);
  13900. Mat_Critical("Failed to allocate %d bytes",2*matvar->nbytes);
  13901. break;
  13902. }
  13903. Mat_VarReadNumeric5(mat,matvar,complex_data->Re,len);
  13904. Mat_VarReadNumeric5(mat,matvar,complex_data->Im,len);
  13905. matvar->data = complex_data;
  13906. } else {
  13907. matvar->nbytes = len*matvar->data_size;
  13908. matvar->data = malloc(matvar->nbytes);
  13909. if ( !matvar->data ) {
  13910. Mat_Critical("Failed to allocate %d bytes",matvar->nbytes);
  13911. break;
  13912. }
  13913. Mat_VarReadNumeric5(mat,matvar,matvar->data,len);
  13914. }
  13915. break;
  13916. case MAT_C_INT8:
  13917. (void)fseek((FILE*)mat->fp,matvar->internal->datapos,SEEK_SET);
  13918. matvar->data_size = sizeof(mat_int8_t);
  13919. matvar->data_type = MAT_T_INT8;
  13920. if ( matvar->isComplex ) {
  13921. mat_complex_split_t *complex_data;
  13922. matvar->nbytes = len*matvar->data_size;
  13923. complex_data = (mat_complex_split_t*)malloc(sizeof(*complex_data));
  13924. if ( NULL == complex_data ) {
  13925. Mat_Critical("Failed to allocate %d bytes",sizeof(*complex_data));
  13926. break;
  13927. }
  13928. complex_data->Re = malloc(matvar->nbytes);
  13929. complex_data->Im = malloc(matvar->nbytes);
  13930. if ( NULL == complex_data->Re || NULL == complex_data->Im ) {
  13931. if ( NULL != complex_data->Re )
  13932. free(complex_data->Re);
  13933. if ( NULL != complex_data->Im )
  13934. free(complex_data->Im);
  13935. free(complex_data);
  13936. Mat_Critical("Failed to allocate %d bytes",2*matvar->nbytes);
  13937. break;
  13938. }
  13939. Mat_VarReadNumeric5(mat,matvar,complex_data->Re,len);
  13940. Mat_VarReadNumeric5(mat,matvar,complex_data->Im,len);
  13941. matvar->data = complex_data;
  13942. } else {
  13943. matvar->nbytes = len*matvar->data_size;
  13944. matvar->data = malloc(matvar->nbytes);
  13945. if ( !matvar->data ) {
  13946. Mat_Critical("Failed to allocate %d bytes",matvar->nbytes);
  13947. break;
  13948. }
  13949. Mat_VarReadNumeric5(mat,matvar,matvar->data,len);
  13950. }
  13951. break;
  13952. case MAT_C_UINT8:
  13953. (void)fseek((FILE*)mat->fp,matvar->internal->datapos,SEEK_SET);
  13954. matvar->data_size = sizeof(mat_uint8_t);
  13955. matvar->data_type = MAT_T_UINT8;
  13956. if ( matvar->isComplex ) {
  13957. mat_complex_split_t *complex_data;
  13958. matvar->nbytes = len*matvar->data_size;
  13959. complex_data = (mat_complex_split_t*)malloc(sizeof(*complex_data));
  13960. if ( NULL == complex_data ) {
  13961. Mat_Critical("Failed to allocate %d bytes",sizeof(*complex_data));
  13962. break;
  13963. }
  13964. complex_data->Re = malloc(matvar->nbytes);
  13965. complex_data->Im = malloc(matvar->nbytes);
  13966. if ( NULL == complex_data->Re || NULL == complex_data->Im ) {
  13967. if ( NULL != complex_data->Re )
  13968. free(complex_data->Re);
  13969. if ( NULL != complex_data->Im )
  13970. free(complex_data->Im);
  13971. free(complex_data);
  13972. Mat_Critical("Failed to allocate %d bytes",2*matvar->nbytes);
  13973. break;
  13974. }
  13975. Mat_VarReadNumeric5(mat,matvar,complex_data->Re,len);
  13976. Mat_VarReadNumeric5(mat,matvar,complex_data->Im,len);
  13977. matvar->data = complex_data;
  13978. } else {
  13979. matvar->nbytes = len*matvar->data_size;
  13980. matvar->data = malloc(matvar->nbytes);
  13981. if ( !matvar->data ) {
  13982. Mat_Critical("Failed to allocate %d bytes",matvar->nbytes);
  13983. break;
  13984. }
  13985. Mat_VarReadNumeric5(mat,matvar,matvar->data,len);
  13986. }
  13987. break;
  13988. case MAT_C_CHAR:
  13989. if ( matvar->compression ) {
  13990. #if defined(HAVE_ZLIB)
  13991. (void)fseek((FILE*)mat->fp,matvar->internal->datapos,SEEK_SET);
  13992. matvar->internal->z->avail_in = 0;
  13993. InflateDataType(mat,matvar->internal->z,tag);
  13994. if ( byteswap )
  13995. (void)Mat_uint32Swap(tag);
  13996. packed_type = TYPE_FROM_TAG(tag[0]);
  13997. if ( tag[0] & 0xffff0000 ) { /* Data is in the tag */
  13998. data_in_tag = 1;
  13999. nBytes = (tag[0] & 0xffff0000) >> 16;
  14000. } else {
  14001. data_in_tag = 0;
  14002. InflateDataType(mat,matvar->internal->z,tag+1);
  14003. if ( byteswap )
  14004. (void)Mat_uint32Swap(tag+1);
  14005. nBytes = tag[1];
  14006. }
  14007. #endif
  14008. } else {
  14009. (void)fseek((FILE*)mat->fp,matvar->internal->datapos,SEEK_SET);
  14010. bytesread += fread(tag,4,1,(FILE*)mat->fp);
  14011. if ( byteswap )
  14012. (void)Mat_uint32Swap(tag);
  14013. packed_type = TYPE_FROM_TAG(tag[0]);
  14014. if ( tag[0] & 0xffff0000 ) { /* Data is in the tag */
  14015. data_in_tag = 1;
  14016. nBytes = (tag[0] & 0xffff0000) >> 16;
  14017. } else {
  14018. data_in_tag = 0;
  14019. bytesread += fread(tag+1,4,1,(FILE*)mat->fp);
  14020. if ( byteswap )
  14021. (void)Mat_uint32Swap(tag+1);
  14022. nBytes = tag[1];
  14023. }
  14024. }
  14025. if ( nBytes == 0 ) {
  14026. matvar->nbytes = 0;
  14027. matvar->data = calloc(0,1);
  14028. break;
  14029. }
  14030. matvar->data_size = sizeof(char);
  14031. /* FIXME: */
  14032. matvar->data_type = MAT_T_UINT8;
  14033. matvar->nbytes = len*matvar->data_size;
  14034. matvar->data = calloc(matvar->nbytes+1,1);
  14035. if ( !matvar->data ) {
  14036. Mat_Critical("Failed to allocate %d bytes",matvar->nbytes);
  14037. break;
  14038. }
  14039. if ( matvar->compression == MAT_COMPRESSION_NONE) {
  14040. nBytes = ReadCharData(mat,(char*)matvar->data,packed_type,len);
  14041. /*
  14042. * If the data was in the tag we started on a 4-byte
  14043. * boundary so add 4 to make it an 8-byte
  14044. */
  14045. if ( data_in_tag )
  14046. nBytes+=4;
  14047. if ( (nBytes % 8) != 0 )
  14048. (void)fseek((FILE*)mat->fp,8-(nBytes % 8),SEEK_CUR);
  14049. #if defined(HAVE_ZLIB)
  14050. } else if ( matvar->compression == MAT_COMPRESSION_ZLIB) {
  14051. nBytes = ReadCompressedCharData(mat,matvar->internal->z,
  14052. (char*)matvar->data,packed_type,len);
  14053. /*
  14054. * If the data was in the tag we started on a 4-byte
  14055. * boundary so add 4 to make it an 8-byte
  14056. */
  14057. if ( data_in_tag )
  14058. nBytes+=4;
  14059. if ( (nBytes % 8) != 0 )
  14060. InflateSkip(mat,matvar->internal->z,8-(nBytes % 8));
  14061. #endif
  14062. }
  14063. break;
  14064. case MAT_C_STRUCT:
  14065. {
  14066. matvar_t **fields;
  14067. int nfields = 0;
  14068. matvar->data_type = MAT_T_STRUCT;
  14069. if ( !matvar->nbytes || !matvar->data_size || NULL == matvar->data )
  14070. break;
  14071. nfields = matvar->internal->num_fields;
  14072. fields = (matvar_t **)matvar->data;
  14073. for ( i = 0; i < len*nfields; i++ ) {
  14074. fields[i]->internal->fp = mat;
  14075. Read5(mat,fields[i]);
  14076. }
  14077. break;
  14078. }
  14079. case MAT_C_CELL:
  14080. {
  14081. matvar_t **cells;
  14082. if ( !matvar->data ) {
  14083. Mat_Critical("Data is NULL for Cell Array %s",matvar->name);
  14084. break;
  14085. }
  14086. cells = (matvar_t **)matvar->data;
  14087. for ( i = 0; i < len; i++ ) {
  14088. cells[i]->internal->fp = mat;
  14089. Read5(mat,cells[i]);
  14090. }
  14091. /* FIXME: */
  14092. matvar->data_type = MAT_T_CELL;
  14093. break;
  14094. }
  14095. case MAT_C_SPARSE:
  14096. {
  14097. int N = 0;
  14098. mat_sparse_t *data;
  14099. matvar->data_size = sizeof(mat_sparse_t);
  14100. matvar->data = malloc(matvar->data_size);
  14101. if ( matvar->data == NULL ) {
  14102. Mat_Critical("ReadData: Allocation of data pointer failed");
  14103. break;
  14104. }
  14105. data = (mat_sparse_t*)matvar->data;
  14106. data->nzmax = matvar->nbytes;
  14107. (void)fseek((FILE*)mat->fp,matvar->internal->datapos,SEEK_SET);
  14108. /* Read ir */
  14109. if ( matvar->compression ) {
  14110. #if defined(HAVE_ZLIB)
  14111. matvar->internal->z->avail_in = 0;
  14112. InflateDataType(mat,matvar->internal->z,tag);
  14113. if ( mat->byteswap )
  14114. (void)Mat_uint32Swap(tag);
  14115. packed_type = TYPE_FROM_TAG(tag[0]);
  14116. if ( tag[0] & 0xffff0000 ) { /* Data is in the tag */
  14117. data_in_tag = 1;
  14118. N = (tag[0] & 0xffff0000) >> 16;
  14119. } else {
  14120. data_in_tag = 0;
  14121. (void)ReadCompressedInt32Data(mat,matvar->internal->z,
  14122. (mat_int32_t*)&N,MAT_T_INT32,1);
  14123. }
  14124. #endif
  14125. } else {
  14126. bytesread += fread(tag,4,1,(FILE*)mat->fp);
  14127. if ( mat->byteswap )
  14128. (void)Mat_uint32Swap(tag);
  14129. packed_type = TYPE_FROM_TAG(tag[0]);
  14130. if ( tag[0] & 0xffff0000 ) { /* Data is in the tag */
  14131. data_in_tag = 1;
  14132. N = (tag[0] & 0xffff0000) >> 16;
  14133. } else {
  14134. data_in_tag = 0;
  14135. bytesread += fread(&N,4,1,(FILE*)mat->fp);
  14136. if ( mat->byteswap )
  14137. Mat_int32Swap(&N);
  14138. }
  14139. }
  14140. data->nir = N / 4;
  14141. data->ir = (mat_int32_t*)malloc(data->nir*sizeof(mat_int32_t));
  14142. if ( data->ir != NULL ) {
  14143. if ( matvar->compression == MAT_COMPRESSION_NONE) {
  14144. nBytes = ReadInt32Data(mat,data->ir,packed_type,data->nir);
  14145. /*
  14146. * If the data was in the tag we started on a 4-byte
  14147. * boundary so add 4 to make it an 8-byte
  14148. */
  14149. if ( data_in_tag )
  14150. nBytes+=4;
  14151. if ( (nBytes % 8) != 0 )
  14152. (void)fseek((FILE*)mat->fp,8-(nBytes % 8),SEEK_CUR);
  14153. #if defined(HAVE_ZLIB)
  14154. } else if ( matvar->compression == MAT_COMPRESSION_ZLIB) {
  14155. nBytes = ReadCompressedInt32Data(mat,matvar->internal->z,
  14156. data->ir,packed_type,data->nir);
  14157. /*
  14158. * If the data was in the tag we started on a 4-byte
  14159. * boundary so add 4 to make it an 8-byte
  14160. */
  14161. if ( data_in_tag )
  14162. nBytes+=4;
  14163. if ( (nBytes % 8) != 0 )
  14164. InflateSkip(mat,matvar->internal->z,8-(nBytes % 8));
  14165. #endif
  14166. }
  14167. } else {
  14168. Mat_Critical("ReadData: Allocation of ir pointer failed");
  14169. break;
  14170. }
  14171. /* Read jc */
  14172. if ( matvar->compression ) {
  14173. #if defined(HAVE_ZLIB)
  14174. matvar->internal->z->avail_in = 0;
  14175. InflateDataType(mat,matvar->internal->z,tag);
  14176. if ( mat->byteswap )
  14177. Mat_uint32Swap(tag);
  14178. packed_type = TYPE_FROM_TAG(tag[0]);
  14179. if ( tag[0] & 0xffff0000 ) { /* Data is in the tag */
  14180. data_in_tag = 1;
  14181. N = (tag[0] & 0xffff0000) >> 16;
  14182. } else {
  14183. data_in_tag = 0;
  14184. (void)ReadCompressedInt32Data(mat,matvar->internal->z,
  14185. (mat_int32_t*)&N,MAT_T_INT32,1);
  14186. }
  14187. #endif
  14188. } else {
  14189. bytesread += fread(tag,4,1,(FILE*)mat->fp);
  14190. if ( mat->byteswap )
  14191. Mat_uint32Swap(tag);
  14192. packed_type = TYPE_FROM_TAG(tag[0]);
  14193. if ( tag[0] & 0xffff0000 ) { /* Data is in the tag */
  14194. data_in_tag = 1;
  14195. N = (tag[0] & 0xffff0000) >> 16;
  14196. } else {
  14197. data_in_tag = 0;
  14198. bytesread += fread(&N,4,1,(FILE*)mat->fp);
  14199. if ( mat->byteswap )
  14200. Mat_int32Swap(&N);
  14201. }
  14202. }
  14203. data->njc = N / 4;
  14204. data->jc = (mat_int32_t*)malloc(data->njc*sizeof(mat_int32_t));
  14205. if ( data->jc != NULL ) {
  14206. if ( matvar->compression == MAT_COMPRESSION_NONE) {
  14207. nBytes = ReadInt32Data(mat,data->jc,packed_type,data->njc);
  14208. /*
  14209. * If the data was in the tag we started on a 4-byte
  14210. * boundary so add 4 to make it an 8-byte
  14211. */
  14212. if ( data_in_tag )
  14213. nBytes+=4;
  14214. if ( (nBytes % 8) != 0 )
  14215. (void)fseek((FILE*)mat->fp,8-(nBytes % 8),SEEK_CUR);
  14216. #if defined(HAVE_ZLIB)
  14217. } else if ( matvar->compression == MAT_COMPRESSION_ZLIB) {
  14218. nBytes = ReadCompressedInt32Data(mat,matvar->internal->z,
  14219. data->jc,packed_type,data->njc);
  14220. /*
  14221. * If the data was in the tag we started on a 4-byte
  14222. * boundary so add 4 to make it an 8-byte
  14223. */
  14224. if ( data_in_tag )
  14225. nBytes+=4;
  14226. if ( (nBytes % 8) != 0 )
  14227. InflateSkip(mat,matvar->internal->z,8-(nBytes % 8));
  14228. #endif
  14229. }
  14230. } else {
  14231. Mat_Critical("ReadData: Allocation of jc pointer failed");
  14232. break;
  14233. }
  14234. /* Read data */
  14235. if ( matvar->compression ) {
  14236. #if defined(HAVE_ZLIB)
  14237. matvar->internal->z->avail_in = 0;
  14238. InflateDataType(mat,matvar->internal->z,tag);
  14239. if ( mat->byteswap )
  14240. Mat_uint32Swap(tag);
  14241. packed_type = TYPE_FROM_TAG(tag[0]);
  14242. if ( tag[0] & 0xffff0000 ) { /* Data is in the tag */
  14243. data_in_tag = 1;
  14244. N = (tag[0] & 0xffff0000) >> 16;
  14245. } else {
  14246. data_in_tag = 0;
  14247. (void)ReadCompressedInt32Data(mat,matvar->internal->z,
  14248. (mat_int32_t*)&N,MAT_T_INT32,1);
  14249. }
  14250. #endif
  14251. } else {
  14252. bytesread += fread(tag,4,1,(FILE*)mat->fp);
  14253. if ( mat->byteswap )
  14254. Mat_uint32Swap(tag);
  14255. packed_type = TYPE_FROM_TAG(tag[0]);
  14256. if ( tag[0] & 0xffff0000 ) { /* Data is in the tag */
  14257. data_in_tag = 1;
  14258. N = (tag[0] & 0xffff0000) >> 16;
  14259. } else {
  14260. data_in_tag = 0;
  14261. bytesread += fread(&N,4,1,(FILE*)mat->fp);
  14262. if ( mat->byteswap )
  14263. Mat_int32Swap(&N);
  14264. }
  14265. }
  14266. if ( matvar->isLogical && packed_type == MAT_T_DOUBLE ) {
  14267. /* For some reason, MAT says the data type is a double,
  14268. * but it appears to be written as 8-bit integer.
  14269. */
  14270. packed_type = MAT_T_UINT8;
  14271. }
  14272. #if defined(EXTENDED_SPARSE)
  14273. matvar->data_type = packed_type;
  14274. #else
  14275. matvar->data_type = MAT_T_DOUBLE;
  14276. #endif
  14277. {
  14278. size_t s_type = Mat_SizeOf(packed_type);
  14279. if ( s_type == 0 )
  14280. break;
  14281. data->ndata = N / s_type;
  14282. }
  14283. if ( matvar->isComplex ) {
  14284. mat_complex_split_t *complex_data;
  14285. complex_data = (mat_complex_split_t*)malloc(sizeof(*complex_data));
  14286. if ( NULL == complex_data ) {
  14287. Mat_Critical("Failed to allocate %d bytes",sizeof(*complex_data));
  14288. break;
  14289. }
  14290. complex_data->Re = malloc(data->ndata*
  14291. Mat_SizeOf(matvar->data_type));
  14292. complex_data->Im = malloc(data->ndata*
  14293. Mat_SizeOf(matvar->data_type));
  14294. if ( NULL == complex_data->Re || NULL == complex_data->Im ) {
  14295. if ( NULL != complex_data->Re )
  14296. free(complex_data->Re);
  14297. if ( NULL != complex_data->Im )
  14298. free(complex_data->Im);
  14299. free(complex_data);
  14300. Mat_Critical("Failed to allocate %d bytes",
  14301. data->ndata* Mat_SizeOf(matvar->data_type));
  14302. break;
  14303. }
  14304. if ( matvar->compression == MAT_COMPRESSION_NONE) {
  14305. #if defined(EXTENDED_SPARSE)
  14306. switch ( matvar->data_type ) {
  14307. case MAT_T_DOUBLE:
  14308. nBytes = ReadDoubleData(mat,(double*)complex_data->Re,
  14309. packed_type,data->ndata);
  14310. break;
  14311. case MAT_T_SINGLE:
  14312. nBytes = ReadSingleData(mat,(float*)complex_data->Re,
  14313. packed_type,data->ndata);
  14314. break;
  14315. case MAT_T_INT64:
  14316. #ifdef HAVE_MATIO_INT64_T
  14317. nBytes = ReadInt64Data(mat,(mat_int64_t*)complex_data->Re,
  14318. packed_type,data->ndata);
  14319. #endif
  14320. break;
  14321. case MAT_T_UINT64:
  14322. #ifdef HAVE_MATIO_UINT64_T
  14323. nBytes = ReadUInt64Data(mat,(mat_uint64_t*)complex_data->Re,
  14324. packed_type,data->ndata);
  14325. #endif
  14326. break;
  14327. case MAT_T_INT32:
  14328. nBytes = ReadInt32Data(mat,(mat_int32_t*)complex_data->Re,
  14329. packed_type,data->ndata);
  14330. break;
  14331. case MAT_T_UINT32:
  14332. nBytes = ReadUInt32Data(mat,(mat_uint32_t*)complex_data->Re,
  14333. packed_type,data->ndata);
  14334. break;
  14335. case MAT_T_INT16:
  14336. nBytes = ReadInt16Data(mat,(mat_int16_t*)complex_data->Re,
  14337. packed_type,data->ndata);
  14338. break;
  14339. case MAT_T_UINT16:
  14340. nBytes = ReadUInt16Data(mat,(mat_uint16_t*)complex_data->Re,
  14341. packed_type,data->ndata);
  14342. break;
  14343. case MAT_T_INT8:
  14344. nBytes = ReadInt8Data(mat,(mat_int8_t*)complex_data->Re,
  14345. packed_type,data->ndata);
  14346. break;
  14347. case MAT_T_UINT8:
  14348. nBytes = ReadUInt8Data(mat,(mat_uint8_t*)complex_data->Re,
  14349. packed_type,data->ndata);
  14350. break;
  14351. default:
  14352. break;
  14353. }
  14354. #else
  14355. nBytes = ReadDoubleData(mat,complex_data->Re,
  14356. packed_type,data->ndata);
  14357. #endif
  14358. if ( data_in_tag )
  14359. nBytes+=4;
  14360. if ( (nBytes % 8) != 0 )
  14361. (void)fseek((FILE*)mat->fp,8-(nBytes % 8),SEEK_CUR);
  14362. /* Complex Data Tag */
  14363. bytesread += fread(tag,4,1,(FILE*)mat->fp);
  14364. if ( byteswap )
  14365. (void)Mat_uint32Swap(tag);
  14366. packed_type = TYPE_FROM_TAG(tag[0]);
  14367. if ( tag[0] & 0xffff0000 ) { /* Data is in the tag */
  14368. data_in_tag = 1;
  14369. nBytes = (tag[0] & 0xffff0000) >> 16;
  14370. } else {
  14371. data_in_tag = 0;
  14372. bytesread += fread(tag+1,4,1,(FILE*)mat->fp);
  14373. if ( byteswap )
  14374. (void)Mat_uint32Swap(tag+1);
  14375. nBytes = tag[1];
  14376. }
  14377. #if defined(EXTENDED_SPARSE)
  14378. switch ( matvar->data_type ) {
  14379. case MAT_T_DOUBLE:
  14380. nBytes = ReadDoubleData(mat,(double*)complex_data->Im,
  14381. packed_type,data->ndata);
  14382. break;
  14383. case MAT_T_SINGLE:
  14384. nBytes = ReadSingleData(mat,(float*)complex_data->Im,
  14385. packed_type,data->ndata);
  14386. break;
  14387. case MAT_T_INT64:
  14388. #ifdef HAVE_MATIO_INT64_T
  14389. nBytes = ReadInt64Data(mat,(mat_int64_t*)complex_data->Im,
  14390. packed_type,data->ndata);
  14391. #endif
  14392. break;
  14393. case MAT_T_UINT64:
  14394. #ifdef HAVE_MATIO_UINT64_T
  14395. nBytes = ReadUInt64Data(mat,(mat_uint64_t*)complex_data->Im,
  14396. packed_type,data->ndata);
  14397. #endif
  14398. break;
  14399. case MAT_T_INT32:
  14400. nBytes = ReadInt32Data(mat,(mat_int32_t*)complex_data->Im,
  14401. packed_type,data->ndata);
  14402. break;
  14403. case MAT_T_UINT32:
  14404. nBytes = ReadUInt32Data(mat,(mat_uint32_t*)complex_data->Im,
  14405. packed_type,data->ndata);
  14406. break;
  14407. case MAT_T_INT16:
  14408. nBytes = ReadInt16Data(mat,(mat_int16_t*)complex_data->Im,
  14409. packed_type,data->ndata);
  14410. break;
  14411. case MAT_T_UINT16:
  14412. nBytes = ReadUInt16Data(mat,(mat_uint16_t*)complex_data->Im,
  14413. packed_type,data->ndata);
  14414. break;
  14415. case MAT_T_INT8:
  14416. nBytes = ReadInt8Data(mat,(mat_int8_t*)complex_data->Im,
  14417. packed_type,data->ndata);
  14418. break;
  14419. case MAT_T_UINT8:
  14420. nBytes = ReadUInt8Data(mat,(mat_uint8_t*)complex_data->Im,
  14421. packed_type,data->ndata);
  14422. break;
  14423. default:
  14424. break;
  14425. }
  14426. #else /* EXTENDED_SPARSE */
  14427. nBytes = ReadDoubleData(mat,complex_data->Im,
  14428. packed_type,data->ndata);
  14429. #endif /* EXTENDED_SPARSE */
  14430. if ( data_in_tag )
  14431. nBytes+=4;
  14432. if ( (nBytes % 8) != 0 )
  14433. (void)fseek((FILE*)mat->fp,8-(nBytes % 8),SEEK_CUR);
  14434. #if defined(HAVE_ZLIB)
  14435. } else if ( matvar->compression == MAT_COMPRESSION_ZLIB ) {
  14436. #if defined(EXTENDED_SPARSE)
  14437. switch ( matvar->data_type ) {
  14438. case MAT_T_DOUBLE:
  14439. nBytes = ReadCompressedDoubleData(mat,matvar->internal->z,
  14440. (double*)complex_data->Re,packed_type,data->ndata);
  14441. break;
  14442. case MAT_T_SINGLE:
  14443. nBytes = ReadCompressedSingleData(mat,matvar->internal->z,
  14444. (float*)complex_data->Re,packed_type,data->ndata);
  14445. break;
  14446. case MAT_T_INT64:
  14447. #ifdef HAVE_MATIO_INT64_T
  14448. nBytes = ReadCompressedInt64Data(mat,
  14449. matvar->internal->z,(mat_int64_t*)complex_data->Re,
  14450. packed_type,data->ndata);
  14451. #endif
  14452. break;
  14453. case MAT_T_UINT64:
  14454. #ifdef HAVE_MATIO_UINT64_T
  14455. nBytes = ReadCompressedUInt64Data(mat,
  14456. matvar->internal->z,(mat_uint64_t*)complex_data->Re,
  14457. packed_type,data->ndata);
  14458. #endif
  14459. break;
  14460. case MAT_T_INT32:
  14461. nBytes = ReadCompressedInt32Data(mat,matvar->internal->z,
  14462. (mat_int32_t*)complex_data->Re,packed_type,data->ndata);
  14463. break;
  14464. case MAT_T_UINT32:
  14465. nBytes = ReadCompressedUInt32Data(mat,matvar->internal->z,
  14466. (mat_uint32_t*)complex_data->Re,packed_type,data->ndata);
  14467. break;
  14468. case MAT_T_INT16:
  14469. nBytes = ReadCompressedInt16Data(mat,matvar->internal->z,
  14470. (mat_int16_t*)complex_data->Re,packed_type,data->ndata);
  14471. break;
  14472. case MAT_T_UINT16:
  14473. nBytes = ReadCompressedUInt16Data(mat,matvar->internal->z,
  14474. (mat_uint16_t*)complex_data->Re,packed_type,data->ndata);
  14475. break;
  14476. case MAT_T_INT8:
  14477. nBytes = ReadCompressedInt8Data(mat,matvar->internal->z,
  14478. (mat_int8_t*)complex_data->Re,packed_type,data->ndata);
  14479. break;
  14480. case MAT_T_UINT8:
  14481. nBytes = ReadCompressedUInt8Data(mat,matvar->internal->z,
  14482. (mat_uint8_t*)complex_data->Re,packed_type,data->ndata);
  14483. break;
  14484. default:
  14485. break;
  14486. }
  14487. #else /* EXTENDED_SPARSE */
  14488. nBytes = ReadCompressedDoubleData(mat,matvar->internal->z,
  14489. (double*)complex_data->Re,packed_type,data->ndata);
  14490. #endif /* EXTENDED_SPARSE */
  14491. if ( data_in_tag )
  14492. nBytes+=4;
  14493. if ( (nBytes % 8) != 0 )
  14494. InflateSkip(mat,matvar->internal->z,8-(nBytes % 8));
  14495. /* Complex Data Tag */
  14496. InflateDataType(mat,matvar->internal->z,tag);
  14497. if ( byteswap )
  14498. (void)Mat_uint32Swap(tag);
  14499. packed_type = TYPE_FROM_TAG(tag[0]);
  14500. if ( tag[0] & 0xffff0000 ) { /* Data is in the tag */
  14501. data_in_tag = 1;
  14502. nBytes = (tag[0] & 0xffff0000) >> 16;
  14503. } else {
  14504. data_in_tag = 0;
  14505. InflateDataType(mat,matvar->internal->z,tag+1);
  14506. if ( byteswap )
  14507. (void)Mat_uint32Swap(tag+1);
  14508. nBytes = tag[1];
  14509. }
  14510. #if defined(EXTENDED_SPARSE)
  14511. switch ( matvar->data_type ) {
  14512. case MAT_T_DOUBLE:
  14513. nBytes = ReadCompressedDoubleData(mat,matvar->internal->z,
  14514. (double*)complex_data->Im,packed_type,data->ndata);
  14515. break;
  14516. case MAT_T_SINGLE:
  14517. nBytes = ReadCompressedSingleData(mat,matvar->internal->z,
  14518. (float*)complex_data->Im,packed_type,data->ndata);
  14519. break;
  14520. case MAT_T_INT64:
  14521. #ifdef HAVE_MATIO_INT64_T
  14522. nBytes = ReadCompressedInt64Data(mat,
  14523. matvar->internal->z,(mat_int64_t*)complex_data->Im,
  14524. packed_type,data->ndata);
  14525. #endif
  14526. break;
  14527. case MAT_T_UINT64:
  14528. #ifdef HAVE_MATIO_UINT64_T
  14529. nBytes = ReadCompressedUInt64Data(mat,
  14530. matvar->internal->z,(mat_uint64_t*)complex_data->Im,
  14531. packed_type,data->ndata);
  14532. #endif
  14533. break;
  14534. case MAT_T_INT32:
  14535. nBytes = ReadCompressedInt32Data(mat,matvar->internal->z,
  14536. (mat_int32_t*)complex_data->Im,packed_type,data->ndata);
  14537. break;
  14538. case MAT_T_UINT32:
  14539. nBytes = ReadCompressedUInt32Data(mat,matvar->internal->z,
  14540. (mat_uint32_t*)complex_data->Im,packed_type,data->ndata);
  14541. break;
  14542. case MAT_T_INT16:
  14543. nBytes = ReadCompressedInt16Data(mat,matvar->internal->z,
  14544. (mat_int16_t*)complex_data->Im,packed_type,data->ndata);
  14545. break;
  14546. case MAT_T_UINT16:
  14547. nBytes = ReadCompressedUInt16Data(mat,matvar->internal->z,
  14548. (mat_uint16_t*)complex_data->Im,packed_type,data->ndata);
  14549. break;
  14550. case MAT_T_INT8:
  14551. nBytes = ReadCompressedInt8Data(mat,matvar->internal->z,
  14552. (mat_int8_t*)complex_data->Im,packed_type,data->ndata);
  14553. break;
  14554. case MAT_T_UINT8:
  14555. nBytes = ReadCompressedUInt8Data(mat,matvar->internal->z,
  14556. (mat_uint8_t*)complex_data->Im,packed_type,data->ndata);
  14557. break;
  14558. default:
  14559. break;
  14560. }
  14561. #else /* EXTENDED_SPARSE */
  14562. nBytes = ReadCompressedDoubleData(mat,matvar->internal->z,
  14563. (double*)complex_data->Im,packed_type,data->ndata);
  14564. #endif /* EXTENDED_SPARSE */
  14565. if ( data_in_tag )
  14566. nBytes+=4;
  14567. if ( (nBytes % 8) != 0 )
  14568. InflateSkip(mat,matvar->internal->z,8-(nBytes % 8));
  14569. #endif /* HAVE_ZLIB */
  14570. }
  14571. data->data = complex_data;
  14572. } else { /* isComplex */
  14573. data->data = malloc(data->ndata*Mat_SizeOf(matvar->data_type));
  14574. if ( data->data == NULL ) {
  14575. Mat_Critical("Failed to allocate %d bytes",
  14576. data->ndata*Mat_SizeOf(MAT_T_DOUBLE));
  14577. break;
  14578. }
  14579. if ( matvar->compression == MAT_COMPRESSION_NONE) {
  14580. #if defined(EXTENDED_SPARSE)
  14581. switch ( matvar->data_type ) {
  14582. case MAT_T_DOUBLE:
  14583. nBytes = ReadDoubleData(mat,(double*)data->data,
  14584. packed_type,data->ndata);
  14585. break;
  14586. case MAT_T_SINGLE:
  14587. nBytes = ReadSingleData(mat,(float*)data->data,
  14588. packed_type,data->ndata);
  14589. break;
  14590. case MAT_T_INT64:
  14591. #ifdef HAVE_MATIO_INT64_T
  14592. nBytes = ReadInt64Data(mat,(mat_int64_t*)data->data,
  14593. packed_type,data->ndata);
  14594. #endif
  14595. break;
  14596. case MAT_T_UINT64:
  14597. #ifdef HAVE_MATIO_UINT64_T
  14598. nBytes = ReadUInt64Data(mat,(mat_uint64_t*)data->data,
  14599. packed_type,data->ndata);
  14600. #endif
  14601. break;
  14602. case MAT_T_INT32:
  14603. nBytes = ReadInt32Data(mat,(mat_int32_t*)data->data,
  14604. packed_type,data->ndata);
  14605. break;
  14606. case MAT_T_UINT32:
  14607. nBytes = ReadUInt32Data(mat,(mat_uint32_t*)data->data,
  14608. packed_type,data->ndata);
  14609. break;
  14610. case MAT_T_INT16:
  14611. nBytes = ReadInt16Data(mat,(mat_int16_t*)data->data,
  14612. packed_type,data->ndata);
  14613. break;
  14614. case MAT_T_UINT16:
  14615. nBytes = ReadUInt16Data(mat,(mat_uint16_t*)data->data,
  14616. packed_type,data->ndata);
  14617. break;
  14618. case MAT_T_INT8:
  14619. nBytes = ReadInt8Data(mat,(mat_int8_t*)data->data,
  14620. packed_type,data->ndata);
  14621. break;
  14622. case MAT_T_UINT8:
  14623. nBytes = ReadUInt8Data(mat,(mat_uint8_t*)data->data,
  14624. packed_type,data->ndata);
  14625. break;
  14626. default:
  14627. break;
  14628. }
  14629. #else
  14630. nBytes = ReadDoubleData(mat,(double*)data->data,packed_type,
  14631. data->ndata);
  14632. #endif
  14633. if ( data_in_tag )
  14634. nBytes+=4;
  14635. if ( (nBytes % 8) != 0 )
  14636. (void)fseek((FILE*)mat->fp,8-(nBytes % 8),SEEK_CUR);
  14637. #if defined(HAVE_ZLIB)
  14638. } else if ( matvar->compression == MAT_COMPRESSION_ZLIB) {
  14639. #if defined(EXTENDED_SPARSE)
  14640. switch ( matvar->data_type ) {
  14641. case MAT_T_DOUBLE:
  14642. nBytes = ReadCompressedDoubleData(mat,matvar->internal->z,
  14643. (double*)data->data,packed_type,data->ndata);
  14644. break;
  14645. case MAT_T_SINGLE:
  14646. nBytes = ReadCompressedSingleData(mat,matvar->internal->z,
  14647. (float*)data->data,packed_type,data->ndata);
  14648. break;
  14649. case MAT_T_INT64:
  14650. #ifdef HAVE_MATIO_INT64_T
  14651. nBytes = ReadCompressedInt64Data(mat,
  14652. matvar->internal->z,(mat_int64_t*)data->data,packed_type,
  14653. data->ndata);
  14654. #endif
  14655. break;
  14656. case MAT_T_UINT64:
  14657. #ifdef HAVE_MATIO_UINT64_T
  14658. nBytes = ReadCompressedUInt64Data(mat,
  14659. matvar->internal->z,(mat_uint64_t*)data->data,packed_type,
  14660. data->ndata);
  14661. #endif
  14662. break;
  14663. case MAT_T_INT32:
  14664. nBytes = ReadCompressedInt32Data(mat,matvar->internal->z,
  14665. (mat_int32_t*)data->data,packed_type,data->ndata);
  14666. break;
  14667. case MAT_T_UINT32:
  14668. nBytes = ReadCompressedUInt32Data(mat,matvar->internal->z,
  14669. (mat_uint32_t*)data->data,packed_type,data->ndata);
  14670. break;
  14671. case MAT_T_INT16:
  14672. nBytes = ReadCompressedInt16Data(mat,matvar->internal->z,
  14673. (mat_int16_t*)data->data,packed_type,data->ndata);
  14674. break;
  14675. case MAT_T_UINT16:
  14676. nBytes = ReadCompressedUInt16Data(mat,matvar->internal->z,
  14677. (mat_uint16_t*)data->data,packed_type,data->ndata);
  14678. break;
  14679. case MAT_T_INT8:
  14680. nBytes = ReadCompressedInt8Data(mat,matvar->internal->z,
  14681. (mat_int8_t*)data->data,packed_type,data->ndata);
  14682. break;
  14683. case MAT_T_UINT8:
  14684. nBytes = ReadCompressedUInt8Data(mat,matvar->internal->z,
  14685. (mat_uint8_t*)data->data,packed_type,data->ndata);
  14686. break;
  14687. default:
  14688. break;
  14689. }
  14690. #else /* EXTENDED_SPARSE */
  14691. nBytes = ReadCompressedDoubleData(mat,matvar->internal->z,
  14692. (double*)data->data,packed_type,data->ndata);
  14693. #endif /* EXTENDED_SPARSE */
  14694. if ( data_in_tag )
  14695. nBytes+=4;
  14696. if ( (nBytes % 8) != 0 )
  14697. InflateSkip(mat,matvar->internal->z,8-(nBytes % 8));
  14698. #endif /* HAVE_ZLIB */
  14699. }
  14700. }
  14701. break;
  14702. }
  14703. case MAT_C_FUNCTION:
  14704. {
  14705. matvar_t **functions;
  14706. int nfunctions = 0;
  14707. if ( !matvar->nbytes || !matvar->data_size )
  14708. break;
  14709. nfunctions = matvar->nbytes / matvar->data_size;
  14710. functions = (matvar_t **)matvar->data;
  14711. if ( NULL != functions ) {
  14712. for ( i = 0; i < nfunctions; i++ ) {
  14713. functions[i]->internal->fp = mat;
  14714. Read5(mat,functions[i]);
  14715. }
  14716. }
  14717. /* FIXME: */
  14718. matvar->data_type = MAT_T_FUNCTION;
  14719. break;
  14720. }
  14721. default:
  14722. Mat_Critical("Read5: %d is not a supported class", matvar->class_type);
  14723. }
  14724. (void)fseek((FILE*)mat->fp,fpos,SEEK_SET);
  14725. return;
  14726. }
  14727. #if defined(HAVE_ZLIB)
  14728. #define GET_DATA_SLABN_RANK_LOOP \
  14729. do { \
  14730. for ( j = 1; j < rank; j++ ) { \
  14731. cnt[j]++; \
  14732. if ( (cnt[j] % edge[j]) == 0 ) { \
  14733. cnt[j] = 0; \
  14734. if ( (I % dimp[j]) != 0 ) { \
  14735. ptr_in += dimp[j]-(I % dimp[j])+dimp[j-1]*start[j]; \
  14736. I += dimp[j]-(I % dimp[j]) + dimp[j-1]*start[j]; \
  14737. } else if ( start[j] ) { \
  14738. ptr_in += dimp[j-1]*start[j]; \
  14739. I += dimp[j-1]*start[j]; \
  14740. } \
  14741. } else { \
  14742. I += inc[j]; \
  14743. ptr_in += inc[j]; \
  14744. break; \
  14745. } \
  14746. } \
  14747. } while (0)
  14748. #define GET_DATA_SLAB2 \
  14749. do { \
  14750. ptr_in += start[1]*dims[0] + start[0]; \
  14751. for ( i = 0; i < edge[1]; i++ ) { \
  14752. for ( j = 0; j < edge[0]; j++ ) \
  14753. memcpy(ptr++, ptr_in+j*stride[0], data_size); \
  14754. ptr_in += stride[1]*dims[0]; \
  14755. } \
  14756. } while (0)
  14757. #define GET_DATA_SLABN \
  14758. do { \
  14759. inc[0] = stride[0]-1; \
  14760. dimp[0] = dims[0]; \
  14761. N = edge[0]; \
  14762. I = 0; /* start[0]; */ \
  14763. for ( i = 1; i < rank; i++ ) { \
  14764. inc[i] = stride[i]-1; \
  14765. dimp[i] = dims[i-1]; \
  14766. for ( j = i; j--; ) { \
  14767. inc[i] *= dims[j]; \
  14768. dimp[i] *= dims[j+1]; \
  14769. } \
  14770. N *= edge[i]; \
  14771. I += dimp[i-1]*start[i]; \
  14772. } \
  14773. ptr_in += I; \
  14774. if ( stride[0] == 1 ) { \
  14775. for ( i = 0; i < N; i+=edge[0] ) { \
  14776. if ( start[0] ) { \
  14777. ptr_in += start[0]; \
  14778. I += start[0]; \
  14779. } \
  14780. memcpy(ptr+i, ptr_in, edge[0]*data_size); \
  14781. I += dims[0]-start[0]; \
  14782. ptr_in += dims[0]-start[0]; \
  14783. GET_DATA_SLABN_RANK_LOOP; \
  14784. } \
  14785. } else { \
  14786. for ( i = 0; i < N; i+=edge[0] ) { \
  14787. if ( start[0] ) { \
  14788. ptr_in += start[0]; \
  14789. I += start[0]; \
  14790. } \
  14791. for ( j = 0; j < edge[0]; j++ ) { \
  14792. memcpy(ptr+i+j, ptr_in, data_size); \
  14793. ptr_in += stride[0]; \
  14794. I += stride[0]; \
  14795. } \
  14796. I += dims[0]-edge[0]*stride[0]-start[0]; \
  14797. ptr_in += dims[0]-edge[0]*stride[0]-start[0]; \
  14798. GET_DATA_SLABN_RANK_LOOP; \
  14799. } \
  14800. } \
  14801. } while (0)
  14802. static int
  14803. GetDataSlab(void *data_in, void *data_out, enum matio_classes class_type,
  14804. enum matio_types data_type, size_t *dims, int *start, int *stride, int *edge,
  14805. int rank, size_t nbytes)
  14806. {
  14807. int err = 0;
  14808. int data_size = Mat_SizeOf(data_type);
  14809. if ( rank == 2 ) {
  14810. if ( stride[0]*(edge[0]-1)+start[0]+1 > dims[0] )
  14811. err = 1;
  14812. else if ( stride[1]*(edge[1]-1)+start[1]+1 > dims[1] )
  14813. err = 1;
  14814. else if ( (stride[0] == 1 && edge[0] == dims[0]) &&
  14815. (stride[1] == 1) )
  14816. memcpy(data_out, data_in, nbytes);
  14817. else {
  14818. int i, j;
  14819. switch ( class_type ) {
  14820. case MAT_C_DOUBLE:
  14821. {
  14822. double *ptr = (double *)data_out;
  14823. double *ptr_in = (double *)data_in;
  14824. GET_DATA_SLAB2;
  14825. break;
  14826. }
  14827. case MAT_C_SINGLE:
  14828. {
  14829. float *ptr = (float *)data_out;
  14830. float *ptr_in = (float *)data_in;
  14831. GET_DATA_SLAB2;
  14832. break;
  14833. }
  14834. #ifdef HAVE_MATIO_INT64_T
  14835. case MAT_C_INT64:
  14836. {
  14837. mat_int64_t *ptr = (mat_int64_t *)data_out;
  14838. mat_int64_t *ptr_in = (mat_int64_t *)data_in;
  14839. GET_DATA_SLAB2;
  14840. break;
  14841. }
  14842. #endif /* HAVE_MATIO_INT64_T */
  14843. #ifdef HAVE_MATIO_UINT64_T
  14844. case MAT_C_UINT64:
  14845. {
  14846. mat_uint64_t *ptr = (mat_uint64_t *)data_out;
  14847. mat_uint64_t *ptr_in = (mat_uint64_t *)data_in;
  14848. GET_DATA_SLAB2;
  14849. break;
  14850. }
  14851. #endif /* HAVE_MATIO_UINT64_T */
  14852. case MAT_C_INT32:
  14853. {
  14854. mat_int32_t *ptr = (mat_int32_t *)data_out;
  14855. mat_int32_t *ptr_in = (mat_int32_t *)data_in;
  14856. GET_DATA_SLAB2;
  14857. break;
  14858. }
  14859. case MAT_C_UINT32:
  14860. {
  14861. mat_uint32_t *ptr = (mat_uint32_t *)data_out;
  14862. mat_uint32_t *ptr_in = (mat_uint32_t *)data_in;
  14863. GET_DATA_SLAB2;
  14864. break;
  14865. }
  14866. case MAT_C_INT16:
  14867. {
  14868. mat_int16_t *ptr = (mat_int16_t *)data_out;
  14869. mat_int16_t *ptr_in = (mat_int16_t *)data_in;
  14870. GET_DATA_SLAB2;
  14871. break;
  14872. }
  14873. case MAT_C_UINT16:
  14874. {
  14875. mat_uint16_t *ptr = (mat_uint16_t *)data_out;
  14876. mat_uint16_t *ptr_in = (mat_uint16_t *)data_in;
  14877. GET_DATA_SLAB2;
  14878. break;
  14879. }
  14880. case MAT_C_INT8:
  14881. {
  14882. mat_int8_t *ptr = (mat_int8_t *)data_out;
  14883. mat_int8_t *ptr_in = (mat_int8_t *)data_in;
  14884. GET_DATA_SLAB2;
  14885. break;
  14886. }
  14887. case MAT_C_UINT8:
  14888. {
  14889. mat_uint8_t *ptr = (mat_uint8_t *)data_out;
  14890. mat_uint8_t *ptr_in = (mat_uint8_t *)data_in;
  14891. GET_DATA_SLAB2;
  14892. break;
  14893. }
  14894. default:
  14895. err = 1;
  14896. break;
  14897. }
  14898. }
  14899. } else {
  14900. int nBytes = 0, i, j, N, I = 0;
  14901. int inc[10] = {0,}, cnt[10] = {0,}, dimp[10] = {0,};
  14902. switch ( class_type ) {
  14903. case MAT_C_DOUBLE:
  14904. {
  14905. double *ptr = (double *)data_out;
  14906. double *ptr_in = (double *)data_in;
  14907. GET_DATA_SLABN;
  14908. break;
  14909. }
  14910. case MAT_C_SINGLE:
  14911. {
  14912. float *ptr = (float *)data_out;
  14913. float *ptr_in = (float *)data_in;
  14914. GET_DATA_SLABN;
  14915. break;
  14916. }
  14917. #ifdef HAVE_MATIO_INT64_T
  14918. case MAT_C_INT64:
  14919. {
  14920. mat_int64_t *ptr = (mat_int64_t *)data_out;
  14921. mat_int64_t *ptr_in = (mat_int64_t *)data_in;
  14922. GET_DATA_SLABN;
  14923. break;
  14924. }
  14925. #endif /* HAVE_MATIO_INT64_T */
  14926. #ifdef HAVE_MATIO_UINT64_T
  14927. case MAT_C_UINT64:
  14928. {
  14929. mat_uint64_t *ptr = (mat_uint64_t *)data_out;
  14930. mat_uint64_t *ptr_in = (mat_uint64_t *)data_in;
  14931. GET_DATA_SLABN;
  14932. break;
  14933. }
  14934. #endif /* HAVE_MATIO_UINT64_T */
  14935. case MAT_C_INT32:
  14936. {
  14937. mat_int32_t *ptr = (mat_int32_t *)data_out;
  14938. mat_int32_t *ptr_in = (mat_int32_t *)data_in;
  14939. GET_DATA_SLABN;
  14940. break;
  14941. }
  14942. case MAT_C_UINT32:
  14943. {
  14944. mat_uint32_t *ptr = (mat_uint32_t *)data_out;
  14945. mat_uint32_t *ptr_in = (mat_uint32_t *)data_in;
  14946. GET_DATA_SLABN;
  14947. break;
  14948. }
  14949. case MAT_C_INT16:
  14950. {
  14951. mat_int16_t *ptr = (mat_int16_t *)data_out;
  14952. mat_int16_t *ptr_in = (mat_int16_t *)data_in;
  14953. GET_DATA_SLABN;
  14954. break;
  14955. }
  14956. case MAT_C_UINT16:
  14957. {
  14958. mat_uint16_t *ptr = (mat_uint16_t *)data_out;
  14959. mat_uint16_t *ptr_in = (mat_uint16_t *)data_in;
  14960. GET_DATA_SLABN;
  14961. break;
  14962. }
  14963. case MAT_C_INT8:
  14964. {
  14965. mat_int8_t *ptr = (mat_int8_t *)data_out;
  14966. mat_int8_t *ptr_in = (mat_int8_t *)data_in;
  14967. GET_DATA_SLABN;
  14968. break;
  14969. }
  14970. case MAT_C_UINT8:
  14971. {
  14972. mat_uint8_t *ptr = (mat_uint8_t *)data_out;
  14973. mat_uint8_t *ptr_in = (mat_uint8_t *)data_in;
  14974. GET_DATA_SLABN;
  14975. break;
  14976. }
  14977. default:
  14978. err = 1;
  14979. break;
  14980. }
  14981. }
  14982. return err;
  14983. }
  14984. #undef GET_DATA_SLABN
  14985. #undef GET_DATA_SLAB2
  14986. #undef GET_DATA_SLABN_RANK_LOOP
  14987. #define GET_DATA_LINEAR \
  14988. do { \
  14989. ptr_in += start; \
  14990. if ( !stride ) { \
  14991. memcpy(ptr, ptr_in, edge*data_size); \
  14992. } else { \
  14993. int i; \
  14994. for ( i = 0; i < edge; i++ ) \
  14995. memcpy(ptr++, ptr_in+i*stride, data_size); \
  14996. } \
  14997. } while (0)
  14998. static int
  14999. GetDataLinear(void *data_in, void *data_out, enum matio_classes class_type,
  15000. enum matio_types data_type, int start, int stride, int edge)
  15001. {
  15002. int err = 0;
  15003. int data_size = Mat_SizeOf(data_type);
  15004. switch ( class_type ) {
  15005. case MAT_C_DOUBLE:
  15006. {
  15007. double *ptr = (double *)data_out;
  15008. double *ptr_in = (double*)data_in;
  15009. GET_DATA_LINEAR;
  15010. break;
  15011. }
  15012. case MAT_C_SINGLE:
  15013. {
  15014. float *ptr = (float *)data_out;
  15015. float *ptr_in = (float*)data_in;
  15016. GET_DATA_LINEAR;
  15017. break;
  15018. }
  15019. #ifdef HAVE_MATIO_INT64_T
  15020. case MAT_C_INT64:
  15021. {
  15022. mat_int64_t *ptr = (mat_int64_t *)data_out;
  15023. mat_int64_t *ptr_in = (mat_int64_t*)data_in;
  15024. GET_DATA_LINEAR;
  15025. break;
  15026. }
  15027. #endif /* HAVE_MATIO_INT64_T */
  15028. #ifdef HAVE_MATIO_UINT64_T
  15029. case MAT_C_UINT64:
  15030. {
  15031. mat_uint64_t *ptr = (mat_uint64_t *)data_out;
  15032. mat_uint64_t *ptr_in = (mat_uint64_t*)data_in;
  15033. GET_DATA_LINEAR;
  15034. break;
  15035. }
  15036. #endif /* HAVE_MATIO_UINT64_T */
  15037. case MAT_C_INT32:
  15038. {
  15039. mat_int32_t *ptr = (mat_int32_t *)data_out;
  15040. mat_int32_t *ptr_in = (mat_int32_t*)data_in;
  15041. GET_DATA_LINEAR;
  15042. break;
  15043. }
  15044. case MAT_C_UINT32:
  15045. {
  15046. mat_uint32_t *ptr = (mat_uint32_t *)data_out;
  15047. mat_uint32_t *ptr_in = (mat_uint32_t*)data_in;
  15048. GET_DATA_LINEAR;
  15049. break;
  15050. }
  15051. case MAT_C_INT16:
  15052. {
  15053. mat_int16_t *ptr = (mat_int16_t *)data_out;
  15054. mat_int16_t *ptr_in = (mat_int16_t*)data_in;
  15055. GET_DATA_LINEAR;
  15056. break;
  15057. }
  15058. case MAT_C_UINT16:
  15059. {
  15060. mat_uint16_t *ptr = (mat_uint16_t *)data_out;
  15061. mat_uint16_t *ptr_in = (mat_uint16_t*)data_in;
  15062. GET_DATA_LINEAR;
  15063. break;
  15064. }
  15065. case MAT_C_INT8:
  15066. {
  15067. mat_int8_t *ptr = (mat_int8_t *)data_out;
  15068. mat_int8_t *ptr_in = (mat_int8_t*)data_in;
  15069. GET_DATA_LINEAR;
  15070. break;
  15071. }
  15072. case MAT_C_UINT8:
  15073. {
  15074. mat_uint8_t *ptr = (mat_uint8_t *)data_out;
  15075. mat_uint8_t *ptr_in = (mat_uint8_t*)data_in;
  15076. GET_DATA_LINEAR;
  15077. break;
  15078. }
  15079. default:
  15080. err = 1;
  15081. break;
  15082. }
  15083. return err;
  15084. }
  15085. #undef GET_DATA_LINEAR
  15086. #endif
  15087. /** @if mat_devman
  15088. * @brief Reads a slab of data from the mat variable @c matvar
  15089. *
  15090. * @ingroup mat_internal
  15091. * @param mat MAT file pointer
  15092. * @param matvar pointer to the mat variable
  15093. * @param data pointer to store the read data in (must be of size
  15094. * edge[0]*...edge[rank-1]*Mat_SizeOfClass(matvar->class_type))
  15095. * @param start index to start reading data in each dimension
  15096. * @param stride write data every @c stride elements in each dimension
  15097. * @param edge number of elements to read in each dimension
  15098. * @retval 0 on success
  15099. * @endif
  15100. */
  15101. static int
  15102. ReadData5(mat_t *mat,matvar_t *matvar,void *data,
  15103. int *start,int *stride,int *edge)
  15104. {
  15105. int err = 0,real_bytes = 0;
  15106. mat_int32_t tag[2];
  15107. size_t bytesread = 0;
  15108. #if defined(HAVE_ZLIB)
  15109. z_stream z;
  15110. #endif
  15111. (void)fseek((FILE*)mat->fp,matvar->internal->datapos,SEEK_SET);
  15112. if ( matvar->compression == MAT_COMPRESSION_NONE ) {
  15113. bytesread += fread(tag,4,2,(FILE*)mat->fp);
  15114. if ( mat->byteswap ) {
  15115. Mat_int32Swap(tag);
  15116. Mat_int32Swap(tag+1);
  15117. }
  15118. matvar->data_type = TYPE_FROM_TAG(tag[0]);
  15119. if ( tag[0] & 0xffff0000 ) { /* Data is packed in the tag */
  15120. (void)fseek((FILE*)mat->fp,-4,SEEK_CUR);
  15121. real_bytes = 4+(tag[0] >> 16);
  15122. } else {
  15123. real_bytes = 8+tag[1];
  15124. }
  15125. #if defined(HAVE_ZLIB)
  15126. } else if ( matvar->compression == MAT_COMPRESSION_ZLIB ) {
  15127. if ( NULL != matvar->internal->data ) {
  15128. /* Data already read in ReadNextStructField or ReadNextCell */
  15129. if ( matvar->isComplex ) {
  15130. mat_complex_split_t *ci, *co;
  15131. co = (mat_complex_split_t*)data;
  15132. ci = (mat_complex_split_t*)matvar->internal->data;
  15133. err = GetDataSlab(ci->Re, co->Re, matvar->class_type,
  15134. matvar->data_type, matvar->dims, start, stride, edge,
  15135. matvar->rank, matvar->nbytes);
  15136. if ( err == 0 )
  15137. err = GetDataSlab(ci->Im, co->Im, matvar->class_type,
  15138. matvar->data_type, matvar->dims, start, stride, edge,
  15139. matvar->rank, matvar->nbytes);
  15140. return err;
  15141. } else {
  15142. return GetDataSlab(matvar->internal->data, data, matvar->class_type,
  15143. matvar->data_type, matvar->dims, start, stride, edge,
  15144. matvar->rank, matvar->nbytes);
  15145. }
  15146. }
  15147. err = inflateCopy(&z,matvar->internal->z);
  15148. if ( err != Z_OK ) {
  15149. Mat_Critical("inflateCopy returned error %s",zError(err));
  15150. return -1;
  15151. }
  15152. z.avail_in = 0;
  15153. InflateDataType(mat,&z,tag);
  15154. if ( mat->byteswap ) {
  15155. Mat_int32Swap(tag);
  15156. }
  15157. matvar->data_type = TYPE_FROM_TAG(tag[0]);
  15158. if ( !(tag[0] & 0xffff0000) ) {/* Data is NOT packed in the tag */
  15159. /* We're cheating, but InflateDataType just inflates 4 bytes */
  15160. InflateDataType(mat,&z,tag+1);
  15161. if ( mat->byteswap ) {
  15162. Mat_int32Swap(tag+1);
  15163. }
  15164. real_bytes = 8+tag[1];
  15165. } else {
  15166. real_bytes = 4+(tag[0] >> 16);
  15167. }
  15168. #endif
  15169. }
  15170. if ( real_bytes % 8 )
  15171. real_bytes += (8-(real_bytes % 8));
  15172. if ( matvar->rank == 2 ) {
  15173. if ( stride[0]*(edge[0]-1)+start[0]+1 > matvar->dims[0] )
  15174. err = 1;
  15175. else if ( stride[1]*(edge[1]-1)+start[1]+1 > matvar->dims[1] )
  15176. err = 1;
  15177. else if ( matvar->compression == MAT_COMPRESSION_NONE ) {
  15178. if ( matvar->isComplex ) {
  15179. mat_complex_split_t *complex_data = (mat_complex_split_t*)data;
  15180. ReadDataSlab2(mat,complex_data->Re,matvar->class_type,
  15181. matvar->data_type,matvar->dims,start,stride,edge);
  15182. (void)fseek((FILE*)mat->fp,matvar->internal->datapos+real_bytes,SEEK_SET);
  15183. bytesread += fread(tag,4,2,(FILE*)mat->fp);
  15184. if ( mat->byteswap ) {
  15185. Mat_int32Swap(tag);
  15186. Mat_int32Swap(tag+1);
  15187. }
  15188. matvar->data_type = TYPE_FROM_TAG(tag[0]);
  15189. if ( tag[0] & 0xffff0000 ) { /* Data is packed in the tag */
  15190. (void)fseek((FILE*)mat->fp,-4,SEEK_CUR);
  15191. }
  15192. ReadDataSlab2(mat,complex_data->Im,matvar->class_type,
  15193. matvar->data_type,matvar->dims,start,stride,edge);
  15194. } else {
  15195. ReadDataSlab2(mat,data,matvar->class_type,
  15196. matvar->data_type,matvar->dims,start,stride,edge);
  15197. }
  15198. }
  15199. #if defined(HAVE_ZLIB)
  15200. else if ( matvar->compression == MAT_COMPRESSION_ZLIB ) {
  15201. if ( matvar->isComplex ) {
  15202. mat_complex_split_t *complex_data = (mat_complex_split_t*)data;
  15203. ReadCompressedDataSlab2(mat,&z,complex_data->Re,
  15204. matvar->class_type,matvar->data_type,matvar->dims,
  15205. start,stride,edge);
  15206. (void)fseek((FILE*)mat->fp,matvar->internal->datapos,SEEK_SET);
  15207. /* Reset zlib knowledge to before reading real tag */
  15208. inflateEnd(&z);
  15209. err = inflateCopy(&z,matvar->internal->z);
  15210. if ( err != Z_OK ) {
  15211. Mat_Critical("inflateCopy returned error %s",zError(err));
  15212. }
  15213. InflateSkip(mat,&z,real_bytes);
  15214. z.avail_in = 0;
  15215. InflateDataType(mat,&z,tag);
  15216. if ( mat->byteswap ) {
  15217. Mat_int32Swap(tag);
  15218. }
  15219. matvar->data_type = TYPE_FROM_TAG(tag[0]);
  15220. if ( !(tag[0] & 0xffff0000) ) {/*Data is NOT packed in the tag*/
  15221. InflateSkip(mat,&z,4);
  15222. }
  15223. ReadCompressedDataSlab2(mat,&z,complex_data->Im,
  15224. matvar->class_type,matvar->data_type,matvar->dims,
  15225. start,stride,edge);
  15226. } else {
  15227. ReadCompressedDataSlab2(mat,&z,data,matvar->class_type,
  15228. matvar->data_type,matvar->dims,start,stride,edge);
  15229. }
  15230. inflateEnd(&z);
  15231. }
  15232. #endif
  15233. } else {
  15234. if ( matvar->compression == MAT_COMPRESSION_NONE ) {
  15235. if ( matvar->isComplex ) {
  15236. mat_complex_split_t *complex_data = (mat_complex_split_t*)data;
  15237. ReadDataSlabN(mat,complex_data->Re,matvar->class_type,
  15238. matvar->data_type,matvar->rank,matvar->dims,
  15239. start,stride,edge);
  15240. (void)fseek((FILE*)mat->fp,matvar->internal->datapos+real_bytes,SEEK_SET);
  15241. bytesread += fread(tag,4,2,(FILE*)mat->fp);
  15242. if ( mat->byteswap ) {
  15243. Mat_int32Swap(tag);
  15244. Mat_int32Swap(tag+1);
  15245. }
  15246. matvar->data_type = TYPE_FROM_TAG(tag[0]);
  15247. if ( tag[0] & 0xffff0000 ) { /* Data is packed in the tag */
  15248. (void)fseek((FILE*)mat->fp,-4,SEEK_CUR);
  15249. }
  15250. ReadDataSlabN(mat,complex_data->Im,matvar->class_type,
  15251. matvar->data_type,matvar->rank,matvar->dims,
  15252. start,stride,edge);
  15253. } else {
  15254. ReadDataSlabN(mat,data,matvar->class_type,matvar->data_type,
  15255. matvar->rank,matvar->dims,start,stride,edge);
  15256. }
  15257. }
  15258. #if defined(HAVE_ZLIB)
  15259. else if ( matvar->compression == MAT_COMPRESSION_ZLIB ) {
  15260. if ( matvar->isComplex ) {
  15261. mat_complex_split_t *complex_data = (mat_complex_split_t*)data;
  15262. ReadCompressedDataSlabN(mat,&z,complex_data->Re,
  15263. matvar->class_type,matvar->data_type,matvar->rank,
  15264. matvar->dims,start,stride,edge);
  15265. (void)fseek((FILE*)mat->fp,matvar->internal->datapos,SEEK_SET);
  15266. /* Reset zlib knowledge to before reading real tag */
  15267. inflateEnd(&z);
  15268. err = inflateCopy(&z,matvar->internal->z);
  15269. if ( err != Z_OK ) {
  15270. Mat_Critical("inflateCopy returned error %s",zError(err));
  15271. }
  15272. InflateSkip(mat,&z,real_bytes);
  15273. z.avail_in = 0;
  15274. InflateDataType(mat,&z,tag);
  15275. if ( mat->byteswap ) {
  15276. Mat_int32Swap(tag);
  15277. }
  15278. matvar->data_type = TYPE_FROM_TAG(tag[0]);
  15279. if ( !(tag[0] & 0xffff0000) ) {/*Data is NOT packed in the tag*/
  15280. InflateSkip(mat,&z,4);
  15281. }
  15282. ReadCompressedDataSlabN(mat,&z,complex_data->Im,
  15283. matvar->class_type,matvar->data_type,matvar->rank,
  15284. matvar->dims,start,stride,edge);
  15285. } else {
  15286. ReadCompressedDataSlabN(mat,&z,data,matvar->class_type,
  15287. matvar->data_type,matvar->rank,matvar->dims,
  15288. start,stride,edge);
  15289. }
  15290. inflateEnd(&z);
  15291. }
  15292. #endif
  15293. }
  15294. if ( err )
  15295. return err;
  15296. switch ( matvar->class_type ) {
  15297. case MAT_C_DOUBLE:
  15298. matvar->data_type = MAT_T_DOUBLE;
  15299. matvar->data_size = sizeof(double);
  15300. break;
  15301. case MAT_C_SINGLE:
  15302. matvar->data_type = MAT_T_SINGLE;
  15303. matvar->data_size = sizeof(float);
  15304. break;
  15305. #ifdef HAVE_MATIO_INT64_T
  15306. case MAT_C_INT64:
  15307. matvar->data_type = MAT_T_INT64;
  15308. matvar->data_size = sizeof(mat_int64_t);
  15309. break;
  15310. #endif /* HAVE_MATIO_INT64_T */
  15311. #ifdef HAVE_MATIO_UINT64_T
  15312. case MAT_C_UINT64:
  15313. matvar->data_type = MAT_T_UINT64;
  15314. matvar->data_size = sizeof(mat_uint64_t);
  15315. break;
  15316. #endif /* HAVE_MATIO_UINT64_T */
  15317. case MAT_C_INT32:
  15318. matvar->data_type = MAT_T_INT32;
  15319. matvar->data_size = sizeof(mat_int32_t);
  15320. break;
  15321. case MAT_C_UINT32:
  15322. matvar->data_type = MAT_T_UINT32;
  15323. matvar->data_size = sizeof(mat_uint32_t);
  15324. break;
  15325. case MAT_C_INT16:
  15326. matvar->data_type = MAT_T_INT16;
  15327. matvar->data_size = sizeof(mat_int16_t);
  15328. break;
  15329. case MAT_C_UINT16:
  15330. matvar->data_type = MAT_T_UINT16;
  15331. matvar->data_size = sizeof(mat_uint16_t);
  15332. break;
  15333. case MAT_C_INT8:
  15334. matvar->data_type = MAT_T_INT8;
  15335. matvar->data_size = sizeof(mat_int8_t);
  15336. break;
  15337. case MAT_C_UINT8:
  15338. matvar->data_type = MAT_T_UINT8;
  15339. matvar->data_size = sizeof(mat_uint8_t);
  15340. break;
  15341. default:
  15342. break;
  15343. }
  15344. return err;
  15345. }
  15346. /** @brief Reads a subset of a MAT variable using a 1-D indexing
  15347. *
  15348. * Reads data from a MAT variable using a linear (1-D) indexing mode. The
  15349. * variable must have been read by Mat_VarReadInfo.
  15350. * @ingroup MAT
  15351. * @param mat MAT file to read data from
  15352. * @param matvar MAT variable information
  15353. * @param data pointer to store data in (must be pre-allocated)
  15354. * @param start starting index
  15355. * @param stride stride of data
  15356. * @param edge number of elements to read
  15357. * @retval 0 on success
  15358. */
  15359. static int
  15360. Mat_VarReadDataLinear5(mat_t *mat,matvar_t *matvar,void *data,int start,
  15361. int stride,int edge)
  15362. {
  15363. int err = 0, nmemb = 1, i, real_bytes = 0;
  15364. mat_int32_t tag[2];
  15365. size_t bytesread = 0;
  15366. #if defined(HAVE_ZLIB)
  15367. z_stream z;
  15368. #endif
  15369. if ( mat->version == MAT_FT_MAT4 )
  15370. return -1;
  15371. (void)fseek((FILE*)mat->fp,matvar->internal->datapos,SEEK_SET);
  15372. if ( matvar->compression == MAT_COMPRESSION_NONE ) {
  15373. bytesread += fread(tag,4,2,(FILE*)mat->fp);
  15374. if ( mat->byteswap ) {
  15375. Mat_int32Swap(tag);
  15376. Mat_int32Swap(tag+1);
  15377. }
  15378. matvar->data_type = (enum matio_types)(tag[0] & 0x000000ff);
  15379. if ( tag[0] & 0xffff0000 ) { /* Data is packed in the tag */
  15380. (void)fseek((FILE*)mat->fp,-4,SEEK_CUR);
  15381. real_bytes = 4+(tag[0] >> 16);
  15382. } else {
  15383. real_bytes = 8+tag[1];
  15384. }
  15385. #if defined(HAVE_ZLIB)
  15386. } else if ( matvar->compression == MAT_COMPRESSION_ZLIB ) {
  15387. if ( NULL != matvar->internal->data ) {
  15388. /* Data already read in ReadNextStructField or ReadNextCell */
  15389. if ( matvar->isComplex ) {
  15390. mat_complex_split_t *ci, *co;
  15391. co = (mat_complex_split_t*)data;
  15392. ci = (mat_complex_split_t*)matvar->internal->data;
  15393. err = GetDataLinear(ci->Re, co->Re, matvar->class_type,
  15394. matvar->data_type, start, stride, edge);
  15395. if ( err == 0 )
  15396. err = GetDataLinear(ci->Im, co->Im, matvar->class_type,
  15397. matvar->data_type, start, stride, edge);
  15398. return err;
  15399. } else {
  15400. return GetDataLinear(matvar->internal->data, data, matvar->class_type,
  15401. matvar->data_type, start, stride, edge);
  15402. }
  15403. }
  15404. matvar->internal->z->avail_in = 0;
  15405. err = inflateCopy(&z,matvar->internal->z);
  15406. if ( err != Z_OK ) {
  15407. Mat_Critical("inflateCopy returned error %s",zError(err));
  15408. return -1;
  15409. }
  15410. InflateDataType(mat,&z,tag);
  15411. if ( mat->byteswap ) {
  15412. Mat_int32Swap(tag);
  15413. Mat_int32Swap(tag+1);
  15414. }
  15415. matvar->data_type = (enum matio_types)(tag[0] & 0x000000ff);
  15416. if ( !(tag[0] & 0xffff0000) ) {/* Data is NOT packed in the tag */
  15417. /* We're cheating, but InflateDataType just inflates 4 bytes */
  15418. InflateDataType(mat,&z,tag+1);
  15419. if ( mat->byteswap ) {
  15420. Mat_int32Swap(tag+1);
  15421. }
  15422. real_bytes = 8+tag[1];
  15423. } else {
  15424. real_bytes = 4+(tag[0] >> 16);
  15425. }
  15426. #endif
  15427. }
  15428. if ( real_bytes % 8 )
  15429. real_bytes += (8-(real_bytes % 8));
  15430. for ( i = 0; i < matvar->rank; i++ )
  15431. nmemb *= matvar->dims[i];
  15432. if ( stride*(edge-1)+start+1 > nmemb ) {
  15433. err = 1;
  15434. } else if ( matvar->compression == MAT_COMPRESSION_NONE ) {
  15435. if ( matvar->isComplex ) {
  15436. mat_complex_split_t *complex_data = (mat_complex_split_t*)data;
  15437. ReadDataSlab1(mat,complex_data->Re,matvar->class_type,
  15438. matvar->data_type,start,stride,edge);
  15439. (void)fseek((FILE*)mat->fp,matvar->internal->datapos+real_bytes,SEEK_SET);
  15440. bytesread += fread(tag,4,2,(FILE*)mat->fp);
  15441. if ( mat->byteswap ) {
  15442. Mat_int32Swap(tag);
  15443. Mat_int32Swap(tag+1);
  15444. }
  15445. matvar->data_type = (enum matio_types)(tag[0] & 0x000000ff);
  15446. if ( tag[0] & 0xffff0000 ) { /* Data is packed in the tag */
  15447. (void)fseek((FILE*)mat->fp,-4,SEEK_CUR);
  15448. }
  15449. ReadDataSlab1(mat,complex_data->Im,matvar->class_type,
  15450. matvar->data_type,start,stride,edge);
  15451. } else {
  15452. ReadDataSlab1(mat,data,matvar->class_type,
  15453. matvar->data_type,start,stride,edge);
  15454. }
  15455. #if defined(HAVE_ZLIB)
  15456. } else if ( matvar->compression == MAT_COMPRESSION_ZLIB ) {
  15457. if ( matvar->isComplex ) {
  15458. mat_complex_split_t *complex_data = (mat_complex_split_t*)data;
  15459. ReadCompressedDataSlab1(mat,&z,complex_data->Re,
  15460. matvar->class_type,matvar->data_type,start,stride,edge);
  15461. (void)fseek((FILE*)mat->fp,matvar->internal->datapos,SEEK_SET);
  15462. /* Reset zlib knowledge to before reading real tag */
  15463. inflateEnd(&z);
  15464. err = inflateCopy(&z,matvar->internal->z);
  15465. if ( err != Z_OK ) {
  15466. Mat_Critical("inflateCopy returned error %s",zError(err));
  15467. }
  15468. InflateSkip(mat,&z,real_bytes);
  15469. z.avail_in = 0;
  15470. InflateDataType(mat,&z,tag);
  15471. if ( mat->byteswap ) {
  15472. Mat_int32Swap(tag);
  15473. }
  15474. matvar->data_type = (enum matio_types)(tag[0] & 0x000000ff);
  15475. if ( !(tag[0] & 0xffff0000) ) {/*Data is NOT packed in the tag*/
  15476. InflateSkip(mat,&z,4);
  15477. }
  15478. ReadCompressedDataSlab1(mat,&z,complex_data->Im,
  15479. matvar->class_type,matvar->data_type,start,stride,edge);
  15480. } else {
  15481. ReadCompressedDataSlab1(mat,&z,data,matvar->class_type,
  15482. matvar->data_type,start,stride,edge);
  15483. }
  15484. inflateEnd(&z);
  15485. #endif
  15486. }
  15487. switch(matvar->class_type) {
  15488. case MAT_C_DOUBLE:
  15489. matvar->data_type = MAT_T_DOUBLE;
  15490. matvar->data_size = sizeof(double);
  15491. break;
  15492. case MAT_C_SINGLE:
  15493. matvar->data_type = MAT_T_SINGLE;
  15494. matvar->data_size = sizeof(float);
  15495. break;
  15496. #ifdef HAVE_MATIO_INT64_T
  15497. case MAT_C_INT64:
  15498. matvar->data_type = MAT_T_INT64;
  15499. matvar->data_size = sizeof(mat_int64_t);
  15500. break;
  15501. #endif /* HAVE_MATIO_INT64_T */
  15502. #ifdef HAVE_MATIO_UINT64_T
  15503. case MAT_C_UINT64:
  15504. matvar->data_type = MAT_T_UINT64;
  15505. matvar->data_size = sizeof(mat_uint64_t);
  15506. break;
  15507. #endif /* HAVE_MATIO_UINT64_T */
  15508. case MAT_C_INT32:
  15509. matvar->data_type = MAT_T_INT32;
  15510. matvar->data_size = sizeof(mat_int32_t);
  15511. break;
  15512. case MAT_C_UINT32:
  15513. matvar->data_type = MAT_T_UINT32;
  15514. matvar->data_size = sizeof(mat_uint32_t);
  15515. break;
  15516. case MAT_C_INT16:
  15517. matvar->data_type = MAT_T_INT16;
  15518. matvar->data_size = sizeof(mat_int16_t);
  15519. break;
  15520. case MAT_C_UINT16:
  15521. matvar->data_type = MAT_T_UINT16;
  15522. matvar->data_size = sizeof(mat_uint16_t);
  15523. break;
  15524. case MAT_C_INT8:
  15525. matvar->data_type = MAT_T_INT8;
  15526. matvar->data_size = sizeof(mat_int8_t);
  15527. break;
  15528. case MAT_C_UINT8:
  15529. matvar->data_type = MAT_T_UINT8;
  15530. matvar->data_size = sizeof(mat_uint8_t);
  15531. break;
  15532. default:
  15533. break;
  15534. }
  15535. return err;
  15536. }
  15537. /** @if mat_devman
  15538. * @brief Writes a matlab variable to a version 5 matlab file
  15539. *
  15540. * @ingroup mat_internal
  15541. * @param mat MAT file pointer
  15542. * @param matvar pointer to the mat variable
  15543. * @param compress option to compress the variable
  15544. * (only works for numeric types)
  15545. * @retval 0 on success
  15546. * @endif
  15547. */
  15548. static int
  15549. Mat_VarWrite5(mat_t *mat,matvar_t *matvar,int compress)
  15550. {
  15551. mat_uint32_t array_flags = 0x0;
  15552. mat_int16_t fieldname_type = MAT_T_INT32,fieldname_data_size=4;
  15553. mat_int8_t pad1 = 0;
  15554. int array_flags_type = MAT_T_UINT32, dims_array_type = MAT_T_INT32;
  15555. int array_flags_size = 8, pad4 = 0, matrix_type = MAT_T_MATRIX;
  15556. int nBytes, i, nmemb = 1,nzmax = 0;
  15557. int start = 0, end = 0;
  15558. if ( NULL == mat )
  15559. return -1;
  15560. /* FIXME: SEEK_END is not Guaranteed by the C standard */
  15561. (void)fseek((FILE*)mat->fp,0,SEEK_END); /* Always write at end of file */
  15562. if ( NULL == matvar || NULL == matvar->name )
  15563. return -1;
  15564. #if !defined(HAVE_ZLIB)
  15565. compress = MAT_COMPRESSION_NONE;
  15566. #endif
  15567. if ( compress == MAT_COMPRESSION_NONE ) {
  15568. fwrite(&matrix_type,4,1,(FILE*)mat->fp);
  15569. fwrite(&pad4,4,1,(FILE*)mat->fp);
  15570. start = ftell((FILE*)mat->fp);
  15571. /* Array Flags */
  15572. array_flags = matvar->class_type & CLASS_TYPE_MASK;
  15573. if ( matvar->isComplex )
  15574. array_flags |= MAT_F_COMPLEX;
  15575. if ( matvar->isGlobal )
  15576. array_flags |= MAT_F_GLOBAL;
  15577. if ( matvar->isLogical )
  15578. array_flags |= MAT_F_LOGICAL;
  15579. if ( matvar->class_type == MAT_C_SPARSE )
  15580. nzmax = ((mat_sparse_t *)matvar->data)->nzmax;
  15581. fwrite(&array_flags_type,4,1,(FILE*)mat->fp);
  15582. fwrite(&array_flags_size,4,1,(FILE*)mat->fp);
  15583. fwrite(&array_flags,4,1,(FILE*)mat->fp);
  15584. fwrite(&nzmax,4,1,(FILE*)mat->fp);
  15585. /* Rank and Dimension */
  15586. nBytes = matvar->rank * 4;
  15587. fwrite(&dims_array_type,4,1,(FILE*)mat->fp);
  15588. fwrite(&nBytes,4,1,(FILE*)mat->fp);
  15589. for ( i = 0; i < matvar->rank; i++ ) {
  15590. mat_int32_t dim;
  15591. dim = matvar->dims[i];
  15592. nmemb *= dim;
  15593. fwrite(&dim,4,1,(FILE*)mat->fp);
  15594. }
  15595. if ( matvar->rank % 2 != 0 )
  15596. fwrite(&pad4,4,1,(FILE*)mat->fp);
  15597. /* Name of variable */
  15598. if ( strlen(matvar->name) <= 4 ) {
  15599. mat_int32_t array_name_type = MAT_T_INT8;
  15600. mat_int32_t array_name_len = strlen(matvar->name);
  15601. mat_int8_t pad1 = 0;
  15602. #if 0
  15603. fwrite(&array_name_type,2,1,(FILE*)mat->fp);
  15604. fwrite(&array_name_len,2,1,(FILE*)mat->fp);
  15605. #else
  15606. array_name_type = (array_name_len << 16) | array_name_type;
  15607. fwrite(&array_name_type,4,1,(FILE*)mat->fp);
  15608. #endif
  15609. fwrite(matvar->name,1,array_name_len,(FILE*)mat->fp);
  15610. for ( i = array_name_len; i < 4; i++ )
  15611. fwrite(&pad1,1,1,(FILE*)mat->fp);
  15612. } else {
  15613. mat_int32_t array_name_type = MAT_T_INT8;
  15614. mat_int32_t array_name_len = (mat_int32_t)strlen(matvar->name);
  15615. mat_int8_t pad1 = 0;
  15616. fwrite(&array_name_type,4,1,(FILE*)mat->fp);
  15617. fwrite(&array_name_len,4,1,(FILE*)mat->fp);
  15618. fwrite(matvar->name,1,array_name_len,(FILE*)mat->fp);
  15619. if ( array_name_len % 8 )
  15620. for ( i = array_name_len % 8; i < 8; i++ )
  15621. fwrite(&pad1,1,1,(FILE*)mat->fp);
  15622. }
  15623. matvar->internal->datapos = ftell((FILE*)mat->fp);
  15624. if ( matvar->internal->datapos == -1L ) {
  15625. Mat_Critical("Couldn't determine file position");
  15626. }
  15627. switch ( matvar->class_type ) {
  15628. case MAT_C_DOUBLE:
  15629. case MAT_C_SINGLE:
  15630. case MAT_C_INT64:
  15631. case MAT_C_UINT64:
  15632. case MAT_C_INT32:
  15633. case MAT_C_UINT32:
  15634. case MAT_C_INT16:
  15635. case MAT_C_UINT16:
  15636. case MAT_C_INT8:
  15637. case MAT_C_UINT8:
  15638. {
  15639. if ( matvar->isComplex ) {
  15640. mat_complex_split_t *complex_data = (mat_complex_split_t*)matvar->data;
  15641. if ( NULL == complex_data )
  15642. complex_data = &null_complex_data;
  15643. nBytes = WriteData(mat,complex_data->Re,nmemb,
  15644. matvar->data_type);
  15645. if ( nBytes % 8 )
  15646. for ( i = nBytes % 8; i < 8; i++ )
  15647. fwrite(&pad1,1,1,(FILE*)mat->fp);
  15648. nBytes = WriteData(mat,complex_data->Im,nmemb,
  15649. matvar->data_type);
  15650. if ( nBytes % 8 )
  15651. for ( i = nBytes % 8; i < 8; i++ )
  15652. fwrite(&pad1,1,1,(FILE*)mat->fp);
  15653. } else {
  15654. nBytes=WriteData(mat,matvar->data,nmemb,matvar->data_type);
  15655. if ( nBytes % 8 )
  15656. for ( i = nBytes % 8; i < 8; i++ )
  15657. fwrite(&pad1,1,1,(FILE*)mat->fp);
  15658. }
  15659. break;
  15660. }
  15661. case MAT_C_CHAR:
  15662. {
  15663. WriteCharData(mat,matvar->data,nmemb,matvar->data_type);
  15664. break;
  15665. }
  15666. case MAT_C_CELL:
  15667. {
  15668. int ncells;
  15669. matvar_t **cells = (matvar_t **)matvar->data;
  15670. /* Check for an empty cell array */
  15671. if ( matvar->nbytes == 0 || matvar->data_size == 0 ||
  15672. matvar->data == NULL )
  15673. break;
  15674. ncells = matvar->nbytes / matvar->data_size;
  15675. for ( i = 0; i < ncells; i++ )
  15676. WriteCellArrayField(mat,cells[i]);
  15677. break;
  15678. }
  15679. case MAT_C_STRUCT:
  15680. {
  15681. char *padzero;
  15682. int fieldname_size, nfields;
  15683. size_t maxlen = 0;
  15684. matvar_t **fields = (matvar_t **)matvar->data;
  15685. mat_int32_t array_name_type = MAT_T_INT8;
  15686. unsigned fieldname;
  15687. nfields = matvar->internal->num_fields;
  15688. /* Check for a structure with no fields */
  15689. if ( nfields < 1 ) {
  15690. #if 0
  15691. fwrite(&fieldname_type,2,1,(FILE*)mat->fp);
  15692. fwrite(&fieldname_data_size,2,1,(FILE*)mat->fp);
  15693. #else
  15694. fieldname = (fieldname_data_size<<16) | fieldname_type;
  15695. fwrite(&fieldname,4,1,(FILE*)mat->fp);
  15696. #endif
  15697. fieldname_size = 1;
  15698. fwrite(&fieldname_size,4,1,(FILE*)mat->fp);
  15699. fwrite(&array_name_type,4,1,(FILE*)mat->fp);
  15700. nBytes = 0;
  15701. fwrite(&nBytes,4,1,(FILE*)mat->fp);
  15702. break;
  15703. }
  15704. for ( i = 0; i < nfields; i++ ) {
  15705. size_t len = strlen(matvar->internal->fieldnames[i]);
  15706. if ( len > maxlen )
  15707. maxlen = len;
  15708. }
  15709. maxlen++;
  15710. fieldname_size = maxlen;
  15711. while ( nfields*fieldname_size % 8 != 0 )
  15712. fieldname_size++;
  15713. #if 0
  15714. fwrite(&fieldname_type,2,1,(FILE*)mat->fp);
  15715. fwrite(&fieldname_data_size,2,1,(FILE*)mat->fp);
  15716. #else
  15717. fieldname = (fieldname_data_size<<16) | fieldname_type;
  15718. fwrite(&fieldname,4,1,(FILE*)mat->fp);
  15719. #endif
  15720. fwrite(&fieldname_size,4,1,(FILE*)mat->fp);
  15721. fwrite(&array_name_type,4,1,(FILE*)mat->fp);
  15722. nBytes = nfields*fieldname_size;
  15723. fwrite(&nBytes,4,1,(FILE*)mat->fp);
  15724. padzero = (char*)calloc(fieldname_size,1);
  15725. for ( i = 0; i < nfields; i++ ) {
  15726. size_t len = strlen(matvar->internal->fieldnames[i]);
  15727. fwrite(matvar->internal->fieldnames[i],1,len,(FILE*)mat->fp);
  15728. fwrite(padzero,1,fieldname_size-len,(FILE*)mat->fp);
  15729. }
  15730. free(padzero);
  15731. for ( i = 0; i < nmemb*nfields; i++ )
  15732. WriteStructField(mat,fields[i]);
  15733. break;
  15734. }
  15735. case MAT_C_SPARSE:
  15736. {
  15737. mat_sparse_t *sparse = (mat_sparse_t*)matvar->data;
  15738. nBytes = WriteData(mat,sparse->ir,sparse->nir,MAT_T_INT32);
  15739. if ( nBytes % 8 )
  15740. for ( i = nBytes % 8; i < 8; i++ )
  15741. fwrite(&pad1,1,1,(FILE*)mat->fp);
  15742. nBytes = WriteData(mat,sparse->jc,sparse->njc,MAT_T_INT32);
  15743. if ( nBytes % 8 )
  15744. for ( i = nBytes % 8; i < 8; i++ )
  15745. fwrite(&pad1,1,1,(FILE*)mat->fp);
  15746. if ( matvar->isComplex ) {
  15747. mat_complex_split_t *complex_data = (mat_complex_split_t*)sparse->data;
  15748. nBytes = WriteData(mat,complex_data->Re,sparse->ndata,
  15749. matvar->data_type);
  15750. if ( nBytes % 8 )
  15751. for ( i = nBytes % 8; i < 8; i++ )
  15752. fwrite(&pad1,1,1,(FILE*)mat->fp);
  15753. nBytes = WriteData(mat,complex_data->Im,sparse->ndata,
  15754. matvar->data_type);
  15755. if ( nBytes % 8 )
  15756. for ( i = nBytes % 8; i < 8; i++ )
  15757. fwrite(&pad1,1,1,(FILE*)mat->fp);
  15758. } else {
  15759. nBytes = WriteData(mat,sparse->data,sparse->ndata,matvar->data_type);
  15760. if ( nBytes % 8 )
  15761. for ( i = nBytes % 8; i < 8; i++ )
  15762. fwrite(&pad1,1,1,(FILE*)mat->fp);
  15763. }
  15764. }
  15765. case MAT_C_EMPTY:
  15766. case MAT_C_FUNCTION:
  15767. case MAT_C_OBJECT:
  15768. break;
  15769. }
  15770. #if defined(HAVE_ZLIB)
  15771. } else if ( compress == MAT_COMPRESSION_ZLIB ) {
  15772. mat_uint32_t comp_buf[512];
  15773. mat_uint32_t uncomp_buf[512] = {0,};
  15774. int buf_size = 512, err;
  15775. size_t byteswritten = 0;
  15776. if (matvar->internal->z != NULL) {
  15777. inflateEnd(matvar->internal->z);
  15778. free(matvar->internal->z);
  15779. }
  15780. matvar->internal->z = (z_streamp)calloc(1,sizeof(*matvar->internal->z));
  15781. err = deflateInit(matvar->internal->z,Z_DEFAULT_COMPRESSION);
  15782. if ( err != Z_OK ) {
  15783. free(matvar->internal->z);
  15784. matvar->internal->z = NULL;
  15785. Mat_Critical("deflateInit returned %s",zError(err));
  15786. return -1;
  15787. }
  15788. matrix_type = MAT_T_COMPRESSED;
  15789. fwrite(&matrix_type,4,1,(FILE*)mat->fp);
  15790. fwrite(&pad4,4,1,(FILE*)mat->fp);
  15791. start = ftell((FILE*)mat->fp);
  15792. /* Array Flags */
  15793. array_flags = matvar->class_type & CLASS_TYPE_MASK;
  15794. if ( matvar->isComplex )
  15795. array_flags |= MAT_F_COMPLEX;
  15796. if ( matvar->isGlobal )
  15797. array_flags |= MAT_F_GLOBAL;
  15798. if ( matvar->isLogical )
  15799. array_flags |= MAT_F_LOGICAL;
  15800. if ( matvar->class_type == MAT_C_SPARSE )
  15801. nzmax = ((mat_sparse_t *)matvar->data)->nzmax;
  15802. uncomp_buf[0] = MAT_T_MATRIX;
  15803. uncomp_buf[1] = (int)GetMatrixMaxBufSize(matvar);
  15804. matvar->internal->z->next_in = ZLIB_BYTE_PTR(uncomp_buf);
  15805. matvar->internal->z->avail_in = 8;
  15806. do {
  15807. matvar->internal->z->next_out = ZLIB_BYTE_PTR(comp_buf);
  15808. matvar->internal->z->avail_out = buf_size*sizeof(*comp_buf);
  15809. deflate(matvar->internal->z,Z_NO_FLUSH);
  15810. byteswritten += fwrite(comp_buf,1,
  15811. buf_size*sizeof(*comp_buf)-matvar->internal->z->avail_out,(FILE*)mat->fp);
  15812. } while ( matvar->internal->z->avail_out == 0 );
  15813. uncomp_buf[0] = array_flags_type;
  15814. uncomp_buf[1] = array_flags_size;
  15815. uncomp_buf[2] = array_flags;
  15816. uncomp_buf[3] = nzmax;
  15817. /* Rank and Dimension */
  15818. nBytes = matvar->rank * 4;
  15819. uncomp_buf[4] = dims_array_type;
  15820. uncomp_buf[5] = nBytes;
  15821. for ( i = 0; i < matvar->rank; i++ ) {
  15822. mat_int32_t dim;
  15823. dim = matvar->dims[i];
  15824. nmemb *= dim;
  15825. uncomp_buf[6+i] = dim;
  15826. }
  15827. if ( matvar->rank % 2 != 0 ) {
  15828. uncomp_buf[6+i] = pad4;
  15829. i++;
  15830. }
  15831. matvar->internal->z->next_in = ZLIB_BYTE_PTR(uncomp_buf);
  15832. matvar->internal->z->avail_in = (6+i)*sizeof(*uncomp_buf);
  15833. do {
  15834. matvar->internal->z->next_out = ZLIB_BYTE_PTR(comp_buf);
  15835. matvar->internal->z->avail_out = buf_size*sizeof(*comp_buf);
  15836. deflate(matvar->internal->z,Z_NO_FLUSH);
  15837. byteswritten += fwrite(comp_buf,1,
  15838. buf_size*sizeof(*comp_buf)-matvar->internal->z->avail_out,(FILE*)mat->fp);
  15839. } while ( matvar->internal->z->avail_out == 0 );
  15840. /* Name of variable */
  15841. if ( strlen(matvar->name) <= 4 ) {
  15842. mat_int16_t array_name_len = (mat_int16_t)strlen(matvar->name);
  15843. mat_int16_t array_name_type = MAT_T_INT8;
  15844. memset(uncomp_buf,0,8);
  15845. uncomp_buf[0] = (array_name_len << 16) | array_name_type;
  15846. memcpy(uncomp_buf+1,matvar->name,array_name_len);
  15847. if ( array_name_len % 4 )
  15848. array_name_len += 4-(array_name_len % 4);
  15849. matvar->internal->z->next_in = ZLIB_BYTE_PTR(uncomp_buf);
  15850. matvar->internal->z->avail_in = 8;
  15851. do {
  15852. matvar->internal->z->next_out = ZLIB_BYTE_PTR(comp_buf);
  15853. matvar->internal->z->avail_out = buf_size*sizeof(*comp_buf);
  15854. deflate(matvar->internal->z,Z_NO_FLUSH);
  15855. byteswritten += fwrite(comp_buf,1,
  15856. buf_size*sizeof(*comp_buf)-matvar->internal->z->avail_out,(FILE*)mat->fp);
  15857. } while ( matvar->internal->z->avail_out == 0 );
  15858. } else {
  15859. mat_int32_t array_name_len = (mat_int32_t)strlen(matvar->name);
  15860. mat_int32_t array_name_type = MAT_T_INT8;
  15861. memset(uncomp_buf,0,buf_size*sizeof(*uncomp_buf));
  15862. uncomp_buf[0] = array_name_type;
  15863. uncomp_buf[1] = array_name_len;
  15864. memcpy(uncomp_buf+2,matvar->name,array_name_len);
  15865. if ( array_name_len % 8 )
  15866. array_name_len += 8-(array_name_len % 8);
  15867. matvar->internal->z->next_in = ZLIB_BYTE_PTR(uncomp_buf);
  15868. matvar->internal->z->avail_in = 8+array_name_len;
  15869. do {
  15870. matvar->internal->z->next_out = ZLIB_BYTE_PTR(comp_buf);
  15871. matvar->internal->z->avail_out = buf_size*sizeof(*comp_buf);
  15872. deflate(matvar->internal->z,Z_NO_FLUSH);
  15873. byteswritten += fwrite(comp_buf,1,
  15874. buf_size*sizeof(*comp_buf)-matvar->internal->z->avail_out,(FILE*)mat->fp);
  15875. } while ( matvar->internal->z->avail_out == 0 );
  15876. }
  15877. matvar->internal->datapos = ftell((FILE*)mat->fp);
  15878. if ( matvar->internal->datapos == -1L ) {
  15879. Mat_Critical("Couldn't determine file position");
  15880. }
  15881. switch ( matvar->class_type ) {
  15882. case MAT_C_DOUBLE:
  15883. case MAT_C_SINGLE:
  15884. case MAT_C_INT64:
  15885. case MAT_C_UINT64:
  15886. case MAT_C_INT32:
  15887. case MAT_C_UINT32:
  15888. case MAT_C_INT16:
  15889. case MAT_C_UINT16:
  15890. case MAT_C_INT8:
  15891. case MAT_C_UINT8:
  15892. {
  15893. /* WriteCompressedData makes sure uncompressed data is aligned
  15894. * on an 8-byte boundary */
  15895. if ( matvar->isComplex ) {
  15896. mat_complex_split_t *complex_data = (mat_complex_split_t*)matvar->data;
  15897. if ( NULL == matvar->data )
  15898. complex_data = &null_complex_data;
  15899. byteswritten += WriteCompressedData(mat,matvar->internal->z,
  15900. complex_data->Re,nmemb,matvar->data_type);
  15901. byteswritten += WriteCompressedData(mat,matvar->internal->z,
  15902. complex_data->Im,nmemb,matvar->data_type);
  15903. } else {
  15904. byteswritten += WriteCompressedData(mat,matvar->internal->z,
  15905. matvar->data,nmemb,matvar->data_type);
  15906. }
  15907. break;
  15908. }
  15909. case MAT_C_CHAR:
  15910. {
  15911. byteswritten += WriteCompressedCharData(mat,matvar->internal->z,
  15912. matvar->data,nmemb,matvar->data_type);
  15913. break;
  15914. }
  15915. case MAT_C_CELL:
  15916. {
  15917. int ncells;
  15918. matvar_t **cells = (matvar_t **)matvar->data;
  15919. /* Check for an empty cell array */
  15920. if ( matvar->nbytes == 0 || matvar->data_size == 0 ||
  15921. matvar->data == NULL )
  15922. break;
  15923. ncells = matvar->nbytes / matvar->data_size;
  15924. for ( i = 0; i < ncells; i++ )
  15925. WriteCompressedCellArrayField(mat,cells[i],matvar->internal->z);
  15926. break;
  15927. }
  15928. case MAT_C_STRUCT:
  15929. {
  15930. unsigned char *padzero;
  15931. int fieldname_size, nfields;
  15932. size_t maxlen = 0;
  15933. mat_int32_t array_name_type = MAT_T_INT8;
  15934. matvar_t **fields = (matvar_t **)matvar->data;
  15935. nfields = matvar->internal->num_fields;
  15936. /* Check for a structure with no fields */
  15937. if ( nfields < 1 ) {
  15938. fieldname_size = 1;
  15939. uncomp_buf[0] = (fieldname_data_size << 16) |
  15940. fieldname_type;
  15941. uncomp_buf[1] = fieldname_size;
  15942. uncomp_buf[2] = array_name_type;
  15943. uncomp_buf[3] = 0;
  15944. matvar->internal->z->next_in = ZLIB_BYTE_PTR(uncomp_buf);
  15945. matvar->internal->z->avail_in = 16;
  15946. do {
  15947. matvar->internal->z->next_out = ZLIB_BYTE_PTR(comp_buf);
  15948. matvar->internal->z->avail_out = buf_size*sizeof(*comp_buf);
  15949. deflate(matvar->internal->z,Z_NO_FLUSH);
  15950. byteswritten += fwrite(comp_buf,1,buf_size*
  15951. sizeof(*comp_buf)-matvar->internal->z->avail_out,(FILE*)mat->fp);
  15952. } while ( matvar->internal->z->avail_out == 0 );
  15953. break;
  15954. }
  15955. for ( i = 0; i < nfields; i++ ) {
  15956. size_t len = strlen(matvar->internal->fieldnames[i]);
  15957. if ( len > maxlen )
  15958. maxlen = len;
  15959. }
  15960. maxlen++;
  15961. fieldname_size = maxlen;
  15962. while ( nfields*fieldname_size % 8 != 0 )
  15963. fieldname_size++;
  15964. uncomp_buf[0] = (fieldname_data_size << 16) | fieldname_type;
  15965. uncomp_buf[1] = fieldname_size;
  15966. uncomp_buf[2] = array_name_type;
  15967. uncomp_buf[3] = nfields*fieldname_size;
  15968. padzero = (unsigned char*)calloc(fieldname_size,1);
  15969. matvar->internal->z->next_in = ZLIB_BYTE_PTR(uncomp_buf);
  15970. matvar->internal->z->avail_in = 16;
  15971. do {
  15972. matvar->internal->z->next_out = ZLIB_BYTE_PTR(comp_buf);
  15973. matvar->internal->z->avail_out = buf_size*sizeof(*comp_buf);
  15974. deflate(matvar->internal->z,Z_NO_FLUSH);
  15975. byteswritten += fwrite(comp_buf,1,
  15976. buf_size*sizeof(*comp_buf)-matvar->internal->z->avail_out,(FILE*)mat->fp);
  15977. } while ( matvar->internal->z->avail_out == 0 );
  15978. for ( i = 0; i < nfields; i++ ) {
  15979. size_t len = strlen(matvar->internal->fieldnames[i]);
  15980. memset(padzero,'\0',fieldname_size);
  15981. memcpy(padzero,matvar->internal->fieldnames[i],len);
  15982. matvar->internal->z->next_in = ZLIB_BYTE_PTR(padzero);
  15983. matvar->internal->z->avail_in = fieldname_size;
  15984. do {
  15985. matvar->internal->z->next_out = ZLIB_BYTE_PTR(comp_buf);
  15986. matvar->internal->z->avail_out = buf_size*sizeof(*comp_buf);
  15987. deflate(matvar->internal->z,Z_NO_FLUSH);
  15988. byteswritten += fwrite(comp_buf,1,
  15989. buf_size*sizeof(*comp_buf)-matvar->internal->z->avail_out,
  15990. (FILE*)mat->fp);
  15991. } while ( matvar->internal->z->avail_out == 0 );
  15992. }
  15993. free(padzero);
  15994. for ( i = 0; i < nmemb*nfields; i++ )
  15995. byteswritten +=
  15996. WriteCompressedStructField(mat,fields[i],matvar->internal->z);
  15997. break;
  15998. }
  15999. case MAT_C_SPARSE:
  16000. {
  16001. mat_sparse_t *sparse = (mat_sparse_t*)matvar->data;
  16002. byteswritten += WriteCompressedData(mat,matvar->internal->z,sparse->ir,
  16003. sparse->nir,MAT_T_INT32);
  16004. byteswritten += WriteCompressedData(mat,matvar->internal->z,sparse->jc,
  16005. sparse->njc,MAT_T_INT32);
  16006. if ( matvar->isComplex ) {
  16007. mat_complex_split_t *complex_data = (mat_complex_split_t*)sparse->data;
  16008. byteswritten += WriteCompressedData(mat,matvar->internal->z,
  16009. complex_data->Re,sparse->ndata,matvar->data_type);
  16010. byteswritten += WriteCompressedData(mat,matvar->internal->z,
  16011. complex_data->Im,sparse->ndata,matvar->data_type);
  16012. } else {
  16013. byteswritten += WriteCompressedData(mat,matvar->internal->z,
  16014. sparse->data,sparse->ndata,matvar->data_type);
  16015. }
  16016. break;
  16017. }
  16018. case MAT_C_EMPTY:
  16019. case MAT_C_FUNCTION:
  16020. case MAT_C_OBJECT:
  16021. break;
  16022. }
  16023. matvar->internal->z->next_in = NULL;
  16024. matvar->internal->z->avail_in = 0;
  16025. do {
  16026. matvar->internal->z->next_out = ZLIB_BYTE_PTR(comp_buf);
  16027. matvar->internal->z->avail_out = buf_size*sizeof(*comp_buf);
  16028. err = deflate(matvar->internal->z,Z_FINISH);
  16029. byteswritten += fwrite(comp_buf,1,
  16030. buf_size*sizeof(*comp_buf)-matvar->internal->z->avail_out,(FILE*)mat->fp);
  16031. } while ( err != Z_STREAM_END && matvar->internal->z->avail_out == 0 );
  16032. /* End the compression and set to NULL so Mat_VarFree doesn't try
  16033. * to free matvar->internal->z with inflateEnd
  16034. */
  16035. #if 0
  16036. if ( byteswritten % 8 )
  16037. for ( i = 0; i < 8-(byteswritten % 8); i++ )
  16038. fwrite(&pad1,1,1,(FILE*)mat->fp);
  16039. #endif
  16040. (void)deflateEnd(matvar->internal->z);
  16041. free(matvar->internal->z);
  16042. matvar->internal->z = NULL;
  16043. #endif
  16044. }
  16045. end = ftell((FILE*)mat->fp);
  16046. if ( start != -1L && end != -1L ) {
  16047. nBytes = (int)(end-start);
  16048. (void)fseek((FILE*)mat->fp,(long)-(nBytes+4),SEEK_CUR);
  16049. fwrite(&nBytes,4,1,(FILE*)mat->fp);
  16050. (void)fseek((FILE*)mat->fp,end,SEEK_SET);
  16051. } else {
  16052. Mat_Critical("Couldn't determine file position");
  16053. }
  16054. return 0;
  16055. }
  16056. /** @if mat_devman
  16057. * @brief Writes the variable information and empty data
  16058. *
  16059. * @ingroup mat_internal
  16060. * @param mat MAT file pointer
  16061. * @param matvar pointer to the mat variable
  16062. * @endif
  16063. */
  16064. static void
  16065. WriteInfo5(mat_t *mat, matvar_t *matvar)
  16066. {
  16067. mat_uint32_t array_flags = 0x0;
  16068. mat_int16_t fieldname_type = MAT_T_INT32,fieldname_data_size=4;
  16069. mat_int8_t pad1 = 0;
  16070. int array_flags_type = MAT_T_UINT32, dims_array_type = MAT_T_INT32;
  16071. int array_flags_size = 8, pad4 = 0, matrix_type = MAT_T_MATRIX;
  16072. int nBytes, i, nmemb = 1,nzmax = 0;
  16073. int start = 0, end = 0;
  16074. /* FIXME: SEEK_END is not Guaranteed by the C standard */
  16075. (void)fseek((FILE*)mat->fp,0,SEEK_END); /* Always write at end of file */
  16076. if ( matvar->compression == MAT_COMPRESSION_NONE ) {
  16077. fwrite(&matrix_type,4,1,(FILE*)mat->fp);
  16078. fwrite(&pad4,4,1,(FILE*)mat->fp);
  16079. start = ftell((FILE*)mat->fp);
  16080. /* Array Flags */
  16081. array_flags = matvar->class_type & CLASS_TYPE_MASK;
  16082. if ( matvar->isComplex )
  16083. array_flags |= MAT_F_COMPLEX;
  16084. if ( matvar->isGlobal )
  16085. array_flags |= MAT_F_GLOBAL;
  16086. if ( matvar->isLogical )
  16087. array_flags |= MAT_F_LOGICAL;
  16088. if ( matvar->class_type == MAT_C_SPARSE )
  16089. nzmax = ((mat_sparse_t *)matvar->data)->nzmax;
  16090. fwrite(&array_flags_type,4,1,(FILE*)mat->fp);
  16091. fwrite(&array_flags_size,4,1,(FILE*)mat->fp);
  16092. fwrite(&array_flags,4,1,(FILE*)mat->fp);
  16093. fwrite(&nzmax,4,1,(FILE*)mat->fp);
  16094. /* Rank and Dimension */
  16095. nBytes = matvar->rank * 4;
  16096. fwrite(&dims_array_type,4,1,(FILE*)mat->fp);
  16097. fwrite(&nBytes,4,1,(FILE*)mat->fp);
  16098. for ( i = 0; i < matvar->rank; i++ ) {
  16099. mat_int32_t dim;
  16100. dim = matvar->dims[i];
  16101. nmemb *= dim;
  16102. fwrite(&dim,4,1,(FILE*)mat->fp);
  16103. }
  16104. if ( matvar->rank % 2 != 0 )
  16105. fwrite(&pad4,4,1,(FILE*)mat->fp);
  16106. /* Name of variable */
  16107. if ( strlen(matvar->name) <= 4 ) {
  16108. mat_int16_t array_name_len = (mat_int16_t)strlen(matvar->name);
  16109. mat_int8_t pad1 = 0;
  16110. mat_int16_t array_name_type = MAT_T_INT8;
  16111. fwrite(&array_name_type,2,1,(FILE*)mat->fp);
  16112. fwrite(&array_name_len,2,1,(FILE*)mat->fp);
  16113. fwrite(matvar->name,1,array_name_len,(FILE*)mat->fp);
  16114. for ( i = array_name_len; i < 4; i++ )
  16115. fwrite(&pad1,1,1,(FILE*)mat->fp);
  16116. } else {
  16117. mat_int32_t array_name_len = (mat_int32_t)strlen(matvar->name);
  16118. mat_int8_t pad1 = 0;
  16119. mat_int32_t array_name_type = MAT_T_INT8;
  16120. fwrite(&array_name_type,4,1,(FILE*)mat->fp);
  16121. fwrite(&array_name_len,4,1,(FILE*)mat->fp);
  16122. fwrite(matvar->name,1,array_name_len,(FILE*)mat->fp);
  16123. if ( array_name_len % 8 )
  16124. for ( i = array_name_len % 8; i < 8; i++ )
  16125. fwrite(&pad1,1,1,(FILE*)mat->fp);
  16126. }
  16127. matvar->internal->datapos = ftell((FILE*)mat->fp);
  16128. if ( matvar->internal->datapos == -1L ) {
  16129. Mat_Critical("Couldn't determine file position");
  16130. }
  16131. switch ( matvar->class_type ) {
  16132. case MAT_C_DOUBLE:
  16133. case MAT_C_SINGLE:
  16134. case MAT_C_INT64:
  16135. case MAT_C_UINT64:
  16136. case MAT_C_INT32:
  16137. case MAT_C_UINT32:
  16138. case MAT_C_INT16:
  16139. case MAT_C_UINT16:
  16140. case MAT_C_INT8:
  16141. case MAT_C_UINT8:
  16142. nBytes = WriteEmptyData(mat,nmemb,matvar->data_type);
  16143. if ( nBytes % 8 )
  16144. for ( i = nBytes % 8; i < 8; i++ )
  16145. fwrite(&pad1,1,1,(FILE*)mat->fp);
  16146. if ( matvar->isComplex ) {
  16147. nBytes = WriteEmptyData(mat,nmemb,matvar->data_type);
  16148. if ( nBytes % 8 )
  16149. for ( i = nBytes % 8; i < 8; i++ )
  16150. fwrite(&pad1,1,1,(FILE*)mat->fp);
  16151. }
  16152. break;
  16153. case MAT_C_CHAR:
  16154. {
  16155. WriteEmptyCharData(mat,nmemb,matvar->data_type);
  16156. break;
  16157. }
  16158. case MAT_C_CELL:
  16159. {
  16160. int ncells;
  16161. matvar_t **cells = (matvar_t **)matvar->data;
  16162. /* Check for an empty cell array */
  16163. if ( matvar->nbytes == 0 || matvar->data_size == 0 ||
  16164. matvar->data == NULL )
  16165. break;
  16166. ncells = matvar->nbytes / matvar->data_size;
  16167. for ( i = 0; i < ncells; i++ )
  16168. WriteCellArrayFieldInfo(mat,cells[i]);
  16169. break;
  16170. }
  16171. case MAT_C_STRUCT:
  16172. {
  16173. char *padzero;
  16174. int maxlen = 0, fieldname_size;
  16175. int nfields = matvar->internal->num_fields;
  16176. matvar_t **fields = (matvar_t **)matvar->data;
  16177. mat_int32_t array_name_type = MAT_T_INT8;
  16178. unsigned fieldname;
  16179. for ( i = 0; i < nfields; i++ ) {
  16180. size_t len = strlen(matvar->internal->fieldnames[i]);
  16181. if ( len > maxlen )
  16182. maxlen = len;
  16183. }
  16184. maxlen++;
  16185. fieldname_size = maxlen;
  16186. while ( nfields*fieldname_size % 8 != 0 )
  16187. fieldname_size++;
  16188. #if 0
  16189. fwrite(&fieldname_type,2,1,(FILE*)mat->fp);
  16190. fwrite(&fieldname_data_size,2,1,(FILE*)mat->fp);
  16191. #else
  16192. fieldname = (fieldname_data_size<<16) | fieldname_type;
  16193. fwrite(&fieldname,4,1,(FILE*)mat->fp);
  16194. #endif
  16195. fwrite(&fieldname_size,4,1,(FILE*)mat->fp);
  16196. fwrite(&array_name_type,4,1,(FILE*)mat->fp);
  16197. nBytes = nfields*fieldname_size;
  16198. fwrite(&nBytes,4,1,(FILE*)mat->fp);
  16199. padzero = (char*)calloc(fieldname_size,1);
  16200. for ( i = 0; i < nfields; i++ ) {
  16201. size_t len = strlen(matvar->internal->fieldnames[i]);
  16202. fwrite(matvar->internal->fieldnames[i],1,len,(FILE*)mat->fp);
  16203. fwrite(padzero,1,fieldname_size-len,(FILE*)mat->fp);
  16204. }
  16205. free(padzero);
  16206. for ( i = 0; i < nfields; i++ )
  16207. WriteInfo5(mat,fields[i]);
  16208. break;
  16209. }
  16210. case MAT_C_SPARSE:
  16211. case MAT_C_EMPTY:
  16212. case MAT_C_FUNCTION:
  16213. case MAT_C_OBJECT:
  16214. break;
  16215. }
  16216. /* Does not work.
  16217. * Can write empty data, but how to go back and add the real data?
  16218. */
  16219. #if 0
  16220. } else if ( matvar->compression == MAT_COMPRESSION_ZLIB ) {
  16221. #if defined(HAVE_ZLIB)
  16222. mat_uint32_t comp_buf[512];
  16223. mat_uint32_t uncomp_buf[512] = {0,};
  16224. int buf_size = 512, err;
  16225. size_t byteswritten = 0;
  16226. matvar->internal->z = (z_streamp)calloc(1,sizeof(*matvar->internal->z));
  16227. err = deflateInit(matvar->internal->z,Z_DEFAULT_COMPRESSION);
  16228. if ( err != Z_OK ) {
  16229. free(matvar->internal->z);
  16230. matvar->internal->z = NULL;
  16231. Mat_Critical("deflateInit returned %s",zError(err));
  16232. return;
  16233. }
  16234. matrix_type = MAT_T_COMPRESSED;
  16235. fwrite(&matrix_type,4,1,(FILE*)mat->fp);
  16236. fwrite(&pad4,4,1,(FILE*)mat->fp);
  16237. start = ftell((FILE*)mat->fp);
  16238. /* Array Flags */
  16239. array_flags = matvar->class_type & MAT_F_CLASS_T;
  16240. if ( matvar->isComplex )
  16241. array_flags |= MAT_F_COMPLEX;
  16242. if ( matvar->isGlobal )
  16243. array_flags |= MAT_F_GLOBAL;
  16244. if ( matvar->isLogical )
  16245. array_flags |= MAT_F_LOGICAL;
  16246. uncomp_buf[0] = MAT_T_MATRIX;
  16247. uncomp_buf[1] = 448;
  16248. matvar->internal->z->next_in = uncomp_buf;
  16249. matvar->internal->z->avail_in = 8;
  16250. do {
  16251. matvar->internal->z->next_out = comp_buf;
  16252. matvar->internal->z->avail_out = buf_size*sizeof(*comp_buf);
  16253. deflate(matvar->internal->z,Z_NO_FLUSH);
  16254. byteswritten += fwrite(comp_buf,1,
  16255. buf_size*sizeof(*comp_buf)-matvar->internal->z->avail_out,
  16256. (FILE*)mat->fp);
  16257. } while ( matvar->internal->z->avail_out == 0 );
  16258. uncomp_buf[0] = array_flags_type;
  16259. uncomp_buf[1] = array_flags_size;
  16260. uncomp_buf[2] = array_flags;
  16261. uncomp_buf[3] = 0;
  16262. /* Rank and Dimension */
  16263. nBytes = matvar->rank * 4;
  16264. uncomp_buf[4] = dims_array_type;
  16265. uncomp_buf[5] = nBytes;
  16266. for ( i = 0; i < matvar->rank; i++ ) {
  16267. mat_int32_t dim;
  16268. dim = matvar->dims[i];
  16269. nmemb *= dim;
  16270. uncomp_buf[6+i] = dim;
  16271. }
  16272. if ( matvar->rank % 2 != 0 )
  16273. uncomp_buf[6+i] = pad4;
  16274. matvar->internal->z->next_in = uncomp_buf;
  16275. matvar->internal->z->avail_in = (6+i)*sizeof(*uncomp_buf);
  16276. do {
  16277. matvar->internal->z->next_out = comp_buf;
  16278. matvar->internal->z->avail_out = buf_size*sizeof(*comp_buf);
  16279. deflate(matvar->internal->z,Z_NO_FLUSH);
  16280. byteswritten += fwrite(comp_buf,1,
  16281. buf_size*sizeof(*comp_buf)-matvar->internal->z->avail_out,
  16282. (FILE*)mat->fp);
  16283. } while ( matvar->internal->z->avail_out == 0 );
  16284. /* Name of variable */
  16285. if ( strlen(matvar->name) <= 4 ) {
  16286. #if 0
  16287. mat_int16_t array_name_len = (mat_int16_t)strlen(matvar->name);
  16288. mat_int8_t pad1 = 0;
  16289. uncomp_buf[0] = (array_name_type << 16) | array_name_len;
  16290. memcpy(uncomp_buf+1,matvar->name,array_name_len);
  16291. matvar->internal->z->next_in = uncomp_buf;
  16292. matvar->internal->z->avail_in = 8;
  16293. do {
  16294. matvar->internal->z->next_out = comp_buf;
  16295. matvar->internal->z->avail_out = buf_size*sizeof(*comp_buf);
  16296. deflate(matvar->internal->z,Z_NO_FLUSH);
  16297. byteswritten += fwrite(comp_buf,1,
  16298. buf_size*sizeof(*comp_buf)-matvar->internal->z->avail_out,
  16299. (FILE*)mat->fp);
  16300. } while ( matvar->internal->z->avail_out == 0 );
  16301. } else {
  16302. #endif
  16303. mat_int32_t array_name_len = (mat_int32_t)strlen(matvar->name);
  16304. memset(uncomp_buf,0,buf_size*sizeof(*uncomp_buf));
  16305. uncomp_buf[0] = array_name_type;
  16306. uncomp_buf[1] = array_name_len;
  16307. memcpy(uncomp_buf+2,matvar->name,array_name_len);
  16308. if ( array_name_len % 8 )
  16309. array_name_len += array_name_len % 8;
  16310. matvar->internal->z->next_in = uncomp_buf;
  16311. matvar->internal->z->avail_in = 8+array_name_len;
  16312. do {
  16313. matvar->internal->z->next_out = comp_buf;
  16314. matvar->internal->z->avail_out = buf_size*sizeof(*comp_buf);
  16315. deflate(matvar->internal->z,Z_NO_FLUSH);
  16316. byteswritten += fwrite(comp_buf,1,
  16317. buf_size*sizeof(*comp_buf)-matvar->internal->z->avail_out,(FILE*)mat->fp);
  16318. } while ( matvar->internal->z->avail_out == 0 );
  16319. }
  16320. matvar->internal->datapos = ftell((FILE*)mat->fp);
  16321. if ( matvar->internal->datapos == -1L ) {
  16322. Mat_Critical("Couldn't determine file position");
  16323. }
  16324. deflateCopy(&z_save,matvar->internal->z);
  16325. switch ( matvar->class_type ) {
  16326. case MAT_C_DOUBLE:
  16327. case MAT_C_SINGLE:
  16328. case MAT_C_INT32:
  16329. case MAT_C_UINT32:
  16330. case MAT_C_INT16:
  16331. case MAT_C_UINT16:
  16332. case MAT_C_INT8:
  16333. case MAT_C_UINT8:
  16334. byteswritten += WriteCompressedEmptyData(mat,matvar->internal->z,nmemb,matvar->data_type);
  16335. #if 0
  16336. if ( nBytes % 8 )
  16337. for ( i = nBytes % 8; i < 8; i++ )
  16338. fwrite(&pad1,1,1,(FILE*)mat->fp);
  16339. if ( matvar->isComplex ) {
  16340. nBytes = WriteEmptyData(mat,nmemb,matvar->data_type);
  16341. if ( nBytes % 8 )
  16342. for ( i = nBytes % 8; i < 8; i++ )
  16343. fwrite(&pad1,1,1,(FILE*)mat->fp);
  16344. }
  16345. #endif
  16346. break;
  16347. }
  16348. matvar->internal->z->next_in = NULL;
  16349. matvar->internal->z->avail_in = 0;
  16350. do {
  16351. matvar->internal->z->next_out = comp_buf;
  16352. matvar->internal->z->avail_out = buf_size*sizeof(*comp_buf);
  16353. err = deflate(matvar->internal->z,Z_FINISH);
  16354. byteswritten += fwrite(comp_buf,1,
  16355. buf_size*sizeof(*comp_buf)-matvar->internal->z->avail_out,(FILE*)mat->fp);
  16356. } while ( err != Z_STREAM_END && matvar->internal->z->avail_out == 0 );
  16357. if ( byteswritten % 8 )
  16358. for ( i = byteswritten % 8; i < 8; i++ )
  16359. fwrite(&pad1,1,1,(FILE*)mat->fp);
  16360. Mat_Critical("deflate with Z_FINISH returned %s, byteswritten = %u",zError(err),byteswritten);
  16361. #if 1
  16362. (void)deflateEnd(matvar->internal->z);
  16363. free(matvar->internal->z);
  16364. matvar->internal->z = NULL;
  16365. #else
  16366. memcpy(matvar->internal->z,&z_save,sizeof(*matvar->internal->z));
  16367. #endif
  16368. #endif
  16369. #endif
  16370. }
  16371. end = ftell((FILE*)mat->fp);
  16372. if ( start != -1L && end != -1L ) {
  16373. nBytes = (int)(end-start);
  16374. (void)fseek((FILE*)mat->fp,(long)-(nBytes+4),SEEK_CUR);
  16375. fwrite(&nBytes,4,1,(FILE*)mat->fp);
  16376. (void)fseek((FILE*)mat->fp,end,SEEK_SET);
  16377. } else {
  16378. Mat_Critical("Couldn't determine file position");
  16379. }
  16380. }
  16381. /** @if mat_devman
  16382. * @brief Reads the header information for the next MAT variable
  16383. *
  16384. * @ingroup mat_internal
  16385. * @param mat MAT file pointer
  16386. * @return pointer to the MAT variable or NULL
  16387. * @endif
  16388. */
  16389. static matvar_t *
  16390. Mat_VarReadNextInfo5( mat_t *mat )
  16391. {
  16392. int err, data_type, nBytes, i;
  16393. int fpos;
  16394. matvar_t *matvar = NULL;
  16395. mat_uint32_t array_flags;
  16396. if( mat == NULL )
  16397. return NULL;
  16398. fpos = ftell((FILE*)mat->fp);
  16399. if ( fpos == -1L ) {
  16400. Mat_Critical("Couldn't determine file position");
  16401. return NULL;
  16402. }
  16403. err = fread(&data_type,4,1,(FILE*)mat->fp);
  16404. if ( err == 0 )
  16405. return NULL;
  16406. err = fread(&nBytes,4,1,(FILE*)mat->fp);
  16407. if ( mat->byteswap ) {
  16408. Mat_int32Swap(&data_type);
  16409. Mat_int32Swap(&nBytes);
  16410. }
  16411. switch ( data_type ) {
  16412. case MAT_T_COMPRESSED:
  16413. {
  16414. #if defined(HAVE_ZLIB)
  16415. mat_uint32_t uncomp_buf[16] = {0,};
  16416. int nbytes;
  16417. int bytesread = 0;
  16418. matvar = Mat_VarCalloc();
  16419. matvar->name = NULL;
  16420. matvar->data = NULL;
  16421. matvar->dims = NULL;
  16422. matvar->nbytes = 0;
  16423. matvar->data_type = MAT_T_UNKNOWN;
  16424. matvar->class_type = MAT_C_EMPTY;
  16425. matvar->data_size = 0;
  16426. matvar->mem_conserve = 0;
  16427. matvar->compression = MAT_COMPRESSION_ZLIB;
  16428. matvar->internal->fp = mat;
  16429. matvar->internal->fpos = fpos;
  16430. matvar->internal->z = (z_streamp)calloc(1,sizeof(z_stream));
  16431. err = inflateInit(matvar->internal->z);
  16432. if ( err != Z_OK ) {
  16433. Mat_VarFree(matvar);
  16434. matvar = NULL;
  16435. Mat_Critical("inflateInit returned %s",zError(err));
  16436. break;
  16437. }
  16438. /* Read Variable tag */
  16439. bytesread += InflateVarTag(mat,matvar,uncomp_buf);
  16440. if ( mat->byteswap ) {
  16441. (void)Mat_uint32Swap(uncomp_buf);
  16442. (void)Mat_uint32Swap(uncomp_buf+1);
  16443. }
  16444. nbytes = uncomp_buf[1];
  16445. if ( uncomp_buf[0] != MAT_T_MATRIX ) {
  16446. (void)fseek((FILE*)mat->fp,nBytes-bytesread,SEEK_CUR);
  16447. Mat_VarFree(matvar);
  16448. matvar = NULL;
  16449. Mat_Critical("Uncompressed type not MAT_T_MATRIX");
  16450. break;
  16451. }
  16452. /* Inflate Array Flags */
  16453. bytesread += InflateArrayFlags(mat,matvar,uncomp_buf);
  16454. if ( mat->byteswap ) {
  16455. (void)Mat_uint32Swap(uncomp_buf);
  16456. (void)Mat_uint32Swap(uncomp_buf+2);
  16457. (void)Mat_uint32Swap(uncomp_buf+3);
  16458. }
  16459. /* Array Flags */
  16460. if ( uncomp_buf[0] == MAT_T_UINT32 ) {
  16461. array_flags = uncomp_buf[2];
  16462. matvar->class_type = CLASS_FROM_ARRAY_FLAGS(array_flags);
  16463. matvar->isComplex = (array_flags & MAT_F_COMPLEX);
  16464. matvar->isGlobal = (array_flags & MAT_F_GLOBAL);
  16465. matvar->isLogical = (array_flags & MAT_F_LOGICAL);
  16466. if ( matvar->class_type == MAT_C_SPARSE ) {
  16467. /* Need to find a more appropriate place to store nzmax */
  16468. matvar->nbytes = uncomp_buf[3];
  16469. }
  16470. }
  16471. /* Inflate Dimensions */
  16472. bytesread += InflateDimensions(mat,matvar,uncomp_buf);
  16473. if ( mat->byteswap ) {
  16474. (void)Mat_uint32Swap(uncomp_buf);
  16475. (void)Mat_uint32Swap(uncomp_buf+1);
  16476. }
  16477. /* Rank and Dimension */
  16478. if ( uncomp_buf[0] == MAT_T_INT32 ) {
  16479. nbytes = uncomp_buf[1];
  16480. matvar->rank = nbytes / 4;
  16481. matvar->dims = (size_t*)malloc(matvar->rank*sizeof(*matvar->dims));
  16482. if ( mat->byteswap ) {
  16483. for ( i = 0; i < matvar->rank; i++ )
  16484. matvar->dims[i] = Mat_uint32Swap(&(uncomp_buf[2+i]));
  16485. } else {
  16486. for ( i = 0; i < matvar->rank; i++ )
  16487. matvar->dims[i] = uncomp_buf[2+i];
  16488. }
  16489. }
  16490. /* Inflate variable name tag */
  16491. bytesread += InflateVarNameTag(mat,matvar,uncomp_buf);
  16492. if ( mat->byteswap )
  16493. (void)Mat_uint32Swap(uncomp_buf);
  16494. /* Name of variable */
  16495. if ( uncomp_buf[0] == MAT_T_INT8 ) { /* Name not in tag */
  16496. int len;
  16497. if ( mat->byteswap )
  16498. len = Mat_uint32Swap(uncomp_buf+1);
  16499. else
  16500. len = uncomp_buf[1];
  16501. if ( len % 8 == 0 )
  16502. i = len;
  16503. else
  16504. i = len+(8-(len % 8));
  16505. matvar->name = (char*)malloc(i+1);
  16506. /* Inflate variable name */
  16507. bytesread += InflateVarName(mat,matvar,matvar->name,i);
  16508. matvar->name[len] = '\0';
  16509. } else if ( ((uncomp_buf[0] & 0x0000ffff) == MAT_T_INT8) &&
  16510. ((uncomp_buf[0] & 0xffff0000) != 0x00) ) {
  16511. /* Name packed in tag */
  16512. int len;
  16513. len = (uncomp_buf[0] & 0xffff0000) >> 16;
  16514. matvar->name = (char*)malloc(len+1);
  16515. memcpy(matvar->name,uncomp_buf+1,len);
  16516. matvar->name[len] = '\0';
  16517. }
  16518. if ( matvar->class_type == MAT_C_STRUCT )
  16519. (void)ReadNextStructField(mat,matvar);
  16520. else if ( matvar->class_type == MAT_C_CELL )
  16521. (void)ReadNextCell(mat,matvar);
  16522. (void)fseek((FILE*)mat->fp,-(int)matvar->internal->z->avail_in,SEEK_CUR);
  16523. matvar->internal->datapos = ftell((FILE*)mat->fp);
  16524. if ( matvar->internal->datapos == -1L ) {
  16525. Mat_Critical("Couldn't determine file position");
  16526. }
  16527. (void)fseek((FILE*)mat->fp,nBytes+8+fpos,SEEK_SET);
  16528. break;
  16529. #else
  16530. Mat_Critical("Compressed variable found in \"%s\", but matio was "
  16531. "built without zlib support",mat->filename);
  16532. (void)fseek((FILE*)mat->fp,nBytes+8+fpos,SEEK_SET);
  16533. return NULL;
  16534. #endif
  16535. }
  16536. case MAT_T_MATRIX:
  16537. {
  16538. int nbytes;
  16539. mat_uint32_t buf[32];
  16540. size_t bytesread = 0;
  16541. matvar = Mat_VarCalloc();
  16542. matvar->internal->fpos = fpos;
  16543. matvar->internal->fp = mat;
  16544. /* Read Array Flags and The Dimensions Tag */
  16545. bytesread += fread(buf,4,6,(FILE*)mat->fp);
  16546. if ( mat->byteswap ) {
  16547. (void)Mat_uint32Swap(buf);
  16548. (void)Mat_uint32Swap(buf+1);
  16549. (void)Mat_uint32Swap(buf+2);
  16550. (void)Mat_uint32Swap(buf+3);
  16551. (void)Mat_uint32Swap(buf+4);
  16552. (void)Mat_uint32Swap(buf+5);
  16553. }
  16554. /* Array Flags */
  16555. if ( buf[0] == MAT_T_UINT32 ) {
  16556. array_flags = buf[2];
  16557. matvar->class_type = CLASS_FROM_ARRAY_FLAGS(array_flags);
  16558. matvar->isComplex = (array_flags & MAT_F_COMPLEX);
  16559. matvar->isGlobal = (array_flags & MAT_F_GLOBAL);
  16560. matvar->isLogical = (array_flags & MAT_F_LOGICAL);
  16561. if ( matvar->class_type == MAT_C_SPARSE ) {
  16562. /* Need to find a more appropriate place to store nzmax */
  16563. matvar->nbytes = buf[3];
  16564. }
  16565. }
  16566. /* Rank and Dimension */
  16567. if ( buf[4] == MAT_T_INT32 ) {
  16568. nbytes = buf[5];
  16569. matvar->rank = nbytes / 4;
  16570. matvar->dims = (size_t*)malloc(matvar->rank*sizeof(*matvar->dims));
  16571. /* Assumes rank <= 16 */
  16572. if ( matvar->rank % 2 != 0 )
  16573. bytesread+=fread(buf,4,matvar->rank+1,(FILE*)mat->fp);
  16574. else
  16575. bytesread+=fread(buf,4,matvar->rank,(FILE*)mat->fp);
  16576. if ( mat->byteswap ) {
  16577. for ( i = 0; i < matvar->rank; i++ )
  16578. matvar->dims[i] = Mat_uint32Swap(buf+i);
  16579. } else {
  16580. for ( i = 0; i < matvar->rank; i++ )
  16581. matvar->dims[i] = buf[i];
  16582. }
  16583. }
  16584. /* Variable Name Tag */
  16585. bytesread+=fread(buf,4,2,(FILE*)mat->fp);
  16586. if ( mat->byteswap )
  16587. (void)Mat_uint32Swap(buf);
  16588. /* Name of variable */
  16589. if ( buf[0] == MAT_T_INT8 ) { /* Name not in tag */
  16590. int len;
  16591. if ( mat->byteswap )
  16592. len = Mat_uint32Swap(buf+1);
  16593. else
  16594. len = buf[1];
  16595. if ( len % 8 == 0 )
  16596. i = len;
  16597. else
  16598. i = len+(8-(len % 8));
  16599. bytesread+=fread(buf,1,i,(FILE*)mat->fp);
  16600. matvar->name = (char*)malloc(len+1);
  16601. memcpy(matvar->name,buf,len);
  16602. matvar->name[len] = '\0';
  16603. } else if ( ((buf[0] & 0x0000ffff) == MAT_T_INT8) &&
  16604. ((buf[0] & 0xffff0000) != 0x00) ) {
  16605. /* Name packed in the tag */
  16606. int len;
  16607. len = (buf[0] & 0xffff0000) >> 16;
  16608. matvar->name = (char*)malloc(len+1);
  16609. memcpy(matvar->name,buf+1,len);
  16610. matvar->name[len] = '\0';
  16611. }
  16612. if ( matvar->class_type == MAT_C_STRUCT )
  16613. (void)ReadNextStructField(mat,matvar);
  16614. else if ( matvar->class_type == MAT_C_CELL )
  16615. (void)ReadNextCell(mat,matvar);
  16616. else if ( matvar->class_type == MAT_C_FUNCTION )
  16617. (void)ReadNextFunctionHandle(mat,matvar);
  16618. matvar->internal->datapos = ftell((FILE*)mat->fp);
  16619. if ( matvar->internal->datapos == -1L ) {
  16620. Mat_Critical("Couldn't determine file position");
  16621. }
  16622. (void)fseek((FILE*)mat->fp,nBytes+8+fpos,SEEK_SET);
  16623. break;
  16624. }
  16625. default:
  16626. Mat_Critical("Not possible to read compressed v7 MAT file \"%s\"",
  16627. mat->filename);
  16628. return NULL;
  16629. }
  16630. return matvar;
  16631. }
  16632. /* -------------------------------
  16633. * ---------- mat73.c
  16634. * -------------------------------
  16635. */
  16636. /** @file mat73.c
  16637. * Matlab MAT version 7.3 file functions
  16638. * @ingroup MAT
  16639. */
  16640. #include <stdlib.h>
  16641. #include <string.h>
  16642. #include <stdio.h>
  16643. #include <math.h>
  16644. #include <time.h>
  16645. #if defined(HAVE_HDF5)
  16646. static const char *Mat_class_names[] = {
  16647. "",
  16648. "cell",
  16649. "struct",
  16650. "object",
  16651. "char",
  16652. "sparse",
  16653. "double",
  16654. "single",
  16655. "int8",
  16656. "uint8",
  16657. "int16",
  16658. "uint16",
  16659. "int32",
  16660. "uint32",
  16661. "int64",
  16662. "uint64",
  16663. "function"
  16664. };
  16665. struct mat_read_next_iter_data {
  16666. mat_t *mat;
  16667. matvar_t *matvar;
  16668. };
  16669. struct h5_read_group_info_iter_data {
  16670. hsize_t nfields;
  16671. matvar_t *matvar;
  16672. };
  16673. /*===========================================================================
  16674. * Private functions
  16675. *===========================================================================
  16676. */
  16677. static enum matio_classes Mat_class_str_to_id(const char *name);
  16678. static hid_t Mat_class_type_to_hid_t(enum matio_classes class_type);
  16679. static hid_t Mat_data_type_to_hid_t(enum matio_types data_type);
  16680. static hid_t Mat_dims_type_to_hid_t(void);
  16681. static void Mat_H5GetChunkSize(size_t rank,hsize_t *dims,hsize_t *chunk_dims);
  16682. static void Mat_H5ReadClassType(matvar_t *matvar,hid_t dset_id);
  16683. static void Mat_H5ReadDatasetInfo(mat_t *mat,matvar_t *matvar,hid_t dset_id);
  16684. static void Mat_H5ReadGroupInfo(mat_t *mat,matvar_t *matvar,hid_t dset_id);
  16685. static void Mat_H5ReadNextReferenceInfo(hid_t ref_id,matvar_t *matvar,mat_t *mat);
  16686. static void Mat_H5ReadNextReferenceData(hid_t ref_id,matvar_t *matvar,mat_t *mat);
  16687. static int Mat_VarWriteCell73(hid_t id,matvar_t *matvar,const char *name,
  16688. hid_t *refs_id);
  16689. static int Mat_VarWriteChar73(hid_t id,matvar_t *matvar,const char *name);
  16690. static int Mat_WriteEmptyVariable73(hid_t id,const char *name,hsize_t rank,
  16691. size_t *dims);
  16692. static int Mat_VarWriteNumeric73(hid_t id,matvar_t *matvar,const char *name);
  16693. static int Mat_VarWriteStruct73(hid_t id,matvar_t *matvar,const char *name,
  16694. hid_t *refs_id);
  16695. static int Mat_VarWriteNext73(hid_t id,matvar_t *matvar,const char *name,
  16696. hid_t *refs_id);
  16697. static herr_t Mat_VarReadNextInfoIterate(hid_t id, const char *name,
  16698. const H5L_info_t *info, void *op_data);
  16699. static herr_t Mat_H5ReadGroupInfoIterate(hid_t dset_id, const char *name,
  16700. const H5L_info_t *info, void *op_data);
  16701. static enum matio_classes
  16702. Mat_class_str_to_id(const char *name)
  16703. {
  16704. enum matio_classes id = MAT_C_EMPTY;
  16705. if ( NULL != name ) {
  16706. int k;
  16707. for ( k = 1; k < 17; k++ ) {
  16708. if ( !strcmp(name,Mat_class_names[k]) ) {
  16709. id = (enum matio_classes)k;
  16710. break;
  16711. }
  16712. }
  16713. }
  16714. return id;
  16715. }
  16716. static enum matio_types
  16717. Mat_ClassToType73(enum matio_classes class_type)
  16718. {
  16719. enum matio_types type;
  16720. switch ( class_type ) {
  16721. case MAT_C_DOUBLE:
  16722. type = MAT_T_DOUBLE;
  16723. break;
  16724. case MAT_C_SINGLE:
  16725. type = MAT_T_SINGLE;
  16726. break;
  16727. case MAT_C_INT64:
  16728. type = MAT_T_INT64;
  16729. break;
  16730. case MAT_C_UINT64:
  16731. type = MAT_T_UINT64;
  16732. break;
  16733. case MAT_C_INT32:
  16734. type = MAT_T_INT32;
  16735. break;
  16736. case MAT_C_UINT32:
  16737. type = MAT_T_UINT32;
  16738. break;
  16739. case MAT_C_INT16:
  16740. type = MAT_T_INT16;
  16741. break;
  16742. case MAT_C_UINT16:
  16743. type = MAT_T_UINT16;
  16744. break;
  16745. case MAT_C_INT8:
  16746. type = MAT_T_INT8;
  16747. break;
  16748. case MAT_C_CHAR:
  16749. type = MAT_T_UINT8;
  16750. break;
  16751. case MAT_C_UINT8:
  16752. type = MAT_T_UINT8;
  16753. break;
  16754. case MAT_C_CELL:
  16755. type = MAT_T_CELL;
  16756. break;
  16757. case MAT_C_STRUCT:
  16758. type = MAT_T_STRUCT;
  16759. break;
  16760. default:
  16761. type = MAT_T_UNKNOWN;
  16762. break;
  16763. }
  16764. return type;
  16765. }
  16766. static enum matio_classes
  16767. Mat_TypeToClass73(enum matio_types type)
  16768. {
  16769. enum matio_classes class_type = MAT_C_EMPTY;
  16770. switch ( type ) {
  16771. case MAT_T_DOUBLE:
  16772. class_type = MAT_C_DOUBLE;
  16773. break;
  16774. case MAT_T_SINGLE:
  16775. class_type = MAT_C_SINGLE;
  16776. break;
  16777. case MAT_T_INT64:
  16778. class_type = MAT_C_INT64;
  16779. break;
  16780. case MAT_T_UINT64:
  16781. class_type = MAT_C_UINT64;
  16782. break;
  16783. case MAT_T_INT32:
  16784. class_type = MAT_C_INT32;
  16785. break;
  16786. case MAT_T_UINT32:
  16787. class_type = MAT_C_UINT32;
  16788. break;
  16789. case MAT_T_INT16:
  16790. class_type = MAT_C_INT16;
  16791. break;
  16792. case MAT_T_UINT16:
  16793. class_type = MAT_C_UINT16;
  16794. break;
  16795. case MAT_T_INT8:
  16796. class_type = MAT_C_INT8;
  16797. break;
  16798. case MAT_T_UINT8:
  16799. class_type = MAT_C_UINT8;
  16800. break;
  16801. default:
  16802. class_type = MAT_C_EMPTY;
  16803. break;
  16804. }
  16805. return class_type;
  16806. }
  16807. static hid_t
  16808. Mat_class_type_to_hid_t(enum matio_classes class_type)
  16809. {
  16810. switch ( class_type ) {
  16811. case MAT_C_DOUBLE:
  16812. return H5T_NATIVE_DOUBLE;
  16813. case MAT_C_SINGLE:
  16814. return H5T_NATIVE_FLOAT;
  16815. case MAT_C_INT64:
  16816. if ( CHAR_BIT*sizeof(long long) == 64 )
  16817. return H5T_NATIVE_LLONG;
  16818. else if ( CHAR_BIT*sizeof(long) == 64 )
  16819. return H5T_NATIVE_LONG;
  16820. else if ( CHAR_BIT*sizeof(int) == 64 )
  16821. return H5T_NATIVE_INT;
  16822. else if ( CHAR_BIT*sizeof(short) == 64 )
  16823. return H5T_NATIVE_SHORT;
  16824. else
  16825. return -1;
  16826. case MAT_C_UINT64:
  16827. if ( CHAR_BIT*sizeof(long long) == 64 )
  16828. return H5T_NATIVE_ULLONG;
  16829. else if ( CHAR_BIT*sizeof(long) == 64 )
  16830. return H5T_NATIVE_ULONG;
  16831. else if ( CHAR_BIT*sizeof(int) == 64 )
  16832. return H5T_NATIVE_UINT;
  16833. if ( CHAR_BIT*sizeof(short) == 64 )
  16834. return H5T_NATIVE_USHORT;
  16835. else
  16836. return -1;
  16837. case MAT_C_INT32:
  16838. if ( CHAR_BIT*sizeof(int) == 32 )
  16839. return H5T_NATIVE_INT;
  16840. else if ( CHAR_BIT*sizeof(short) == 32 )
  16841. return H5T_NATIVE_SHORT;
  16842. else if ( CHAR_BIT*sizeof(long) == 32 )
  16843. return H5T_NATIVE_LONG;
  16844. else if ( CHAR_BIT*sizeof(long long) == 32 )
  16845. return H5T_NATIVE_LLONG;
  16846. else if ( CHAR_BIT == 32 )
  16847. return H5T_NATIVE_SCHAR;
  16848. else
  16849. return -1;
  16850. case MAT_C_UINT32:
  16851. if ( CHAR_BIT*sizeof(int) == 32 )
  16852. return H5T_NATIVE_UINT;
  16853. else if ( CHAR_BIT*sizeof(short) == 32 )
  16854. return H5T_NATIVE_USHORT;
  16855. else if ( CHAR_BIT*sizeof(long) == 32 )
  16856. return H5T_NATIVE_ULONG;
  16857. else if ( CHAR_BIT*sizeof(long long) == 32 )
  16858. return H5T_NATIVE_ULLONG;
  16859. else if ( CHAR_BIT == 32 )
  16860. return H5T_NATIVE_UCHAR;
  16861. else
  16862. return -1;
  16863. case MAT_C_INT16:
  16864. if ( CHAR_BIT*sizeof(short) == 16 )
  16865. return H5T_NATIVE_SHORT;
  16866. else if ( CHAR_BIT*sizeof(int) == 16 )
  16867. return H5T_NATIVE_INT;
  16868. else if ( CHAR_BIT*sizeof(long) == 16 )
  16869. return H5T_NATIVE_LONG;
  16870. else if ( CHAR_BIT*sizeof(long long) == 16 )
  16871. return H5T_NATIVE_LLONG;
  16872. else if ( CHAR_BIT == 16 )
  16873. return H5T_NATIVE_SCHAR;
  16874. else
  16875. return -1;
  16876. case MAT_C_UINT16:
  16877. if ( CHAR_BIT*sizeof(short) == 16 )
  16878. return H5T_NATIVE_USHORT;
  16879. else if ( CHAR_BIT*sizeof(int) == 16 )
  16880. return H5T_NATIVE_UINT;
  16881. else if ( CHAR_BIT*sizeof(long) == 16 )
  16882. return H5T_NATIVE_ULONG;
  16883. else if ( CHAR_BIT*sizeof(long long) == 16 )
  16884. return H5T_NATIVE_ULLONG;
  16885. else if ( CHAR_BIT == 16 )
  16886. return H5T_NATIVE_UCHAR;
  16887. else
  16888. return -1;
  16889. case MAT_C_INT8:
  16890. if ( CHAR_BIT == 8 )
  16891. return H5T_NATIVE_SCHAR;
  16892. else if ( CHAR_BIT*sizeof(short) == 8 )
  16893. return H5T_NATIVE_SHORT;
  16894. else if ( CHAR_BIT*sizeof(int) == 8 )
  16895. return H5T_NATIVE_INT;
  16896. else if ( CHAR_BIT*sizeof(long) == 8 )
  16897. return H5T_NATIVE_LONG;
  16898. else if ( CHAR_BIT*sizeof(long long) == 8 )
  16899. return H5T_NATIVE_LLONG;
  16900. else
  16901. return -1;
  16902. case MAT_C_UINT8:
  16903. if ( CHAR_BIT == 8 )
  16904. return H5T_NATIVE_UCHAR;
  16905. else if ( CHAR_BIT*sizeof(short) == 8 )
  16906. return H5T_NATIVE_USHORT;
  16907. else if ( CHAR_BIT*sizeof(int) == 8 )
  16908. return H5T_NATIVE_UINT;
  16909. else if ( CHAR_BIT*sizeof(long) == 8 )
  16910. return H5T_NATIVE_ULONG;
  16911. else if ( CHAR_BIT*sizeof(long long) == 8 )
  16912. return H5T_NATIVE_ULLONG;
  16913. else
  16914. return -1;
  16915. default:
  16916. return -1;
  16917. }
  16918. }
  16919. static hid_t
  16920. Mat_data_type_to_hid_t(enum matio_types data_type)
  16921. {
  16922. switch ( data_type ) {
  16923. case MAT_T_DOUBLE:
  16924. return H5T_NATIVE_DOUBLE;
  16925. case MAT_T_SINGLE:
  16926. return H5T_NATIVE_FLOAT;
  16927. case MAT_T_INT64:
  16928. if ( CHAR_BIT*sizeof(long long) == 64 )
  16929. return H5T_NATIVE_LLONG;
  16930. else if ( CHAR_BIT*sizeof(long) == 64 )
  16931. return H5T_NATIVE_LONG;
  16932. else if ( CHAR_BIT*sizeof(int) == 64 )
  16933. return H5T_NATIVE_INT;
  16934. else if ( CHAR_BIT*sizeof(short) == 64 )
  16935. return H5T_NATIVE_SHORT;
  16936. else
  16937. return -1;
  16938. case MAT_T_UINT64:
  16939. if ( CHAR_BIT*sizeof(long long) == 64 )
  16940. return H5T_NATIVE_ULLONG;
  16941. else if ( CHAR_BIT*sizeof(long) == 64 )
  16942. return H5T_NATIVE_ULONG;
  16943. else if ( CHAR_BIT*sizeof(int) == 64 )
  16944. return H5T_NATIVE_UINT;
  16945. else if ( CHAR_BIT*sizeof(short) == 64 )
  16946. return H5T_NATIVE_USHORT;
  16947. else
  16948. return -1;
  16949. case MAT_T_INT32:
  16950. if ( CHAR_BIT*sizeof(int) == 32 )
  16951. return H5T_NATIVE_INT;
  16952. else if ( CHAR_BIT*sizeof(short) == 32 )
  16953. return H5T_NATIVE_SHORT;
  16954. else if ( CHAR_BIT*sizeof(long) == 32 )
  16955. return H5T_NATIVE_LONG;
  16956. else if ( CHAR_BIT*sizeof(long long) == 32 )
  16957. return H5T_NATIVE_LLONG;
  16958. else if ( CHAR_BIT == 32 )
  16959. return H5T_NATIVE_SCHAR;
  16960. else
  16961. return -1;
  16962. case MAT_T_UINT32:
  16963. if ( CHAR_BIT*sizeof(int) == 32 )
  16964. return H5T_NATIVE_UINT;
  16965. else if ( CHAR_BIT*sizeof(short) == 32 )
  16966. return H5T_NATIVE_USHORT;
  16967. else if ( CHAR_BIT*sizeof(long) == 32 )
  16968. return H5T_NATIVE_ULONG;
  16969. else if ( CHAR_BIT*sizeof(long long) == 32 )
  16970. return H5T_NATIVE_ULLONG;
  16971. else if ( CHAR_BIT == 32 )
  16972. return H5T_NATIVE_UCHAR;
  16973. else
  16974. return -1;
  16975. case MAT_T_INT16:
  16976. if ( CHAR_BIT*sizeof(short) == 16 )
  16977. return H5T_NATIVE_SHORT;
  16978. else if ( CHAR_BIT*sizeof(int) == 16 )
  16979. return H5T_NATIVE_INT;
  16980. else if ( CHAR_BIT*sizeof(long) == 16 )
  16981. return H5T_NATIVE_LONG;
  16982. else if ( CHAR_BIT*sizeof(long long) == 16 )
  16983. return H5T_NATIVE_LLONG;
  16984. else if ( CHAR_BIT == 16 )
  16985. return H5T_NATIVE_SCHAR;
  16986. else
  16987. return -1;
  16988. case MAT_T_UINT16:
  16989. if ( CHAR_BIT*sizeof(short) == 16 )
  16990. return H5T_NATIVE_USHORT;
  16991. else if ( CHAR_BIT*sizeof(int) == 16 )
  16992. return H5T_NATIVE_UINT;
  16993. else if ( CHAR_BIT*sizeof(long) == 16 )
  16994. return H5T_NATIVE_ULONG;
  16995. else if ( CHAR_BIT*sizeof(long long) == 16 )
  16996. return H5T_NATIVE_ULLONG;
  16997. else if ( CHAR_BIT == 16 )
  16998. return H5T_NATIVE_UCHAR;
  16999. else
  17000. return -1;
  17001. case MAT_T_INT8:
  17002. if ( CHAR_BIT == 8 )
  17003. return H5T_NATIVE_SCHAR;
  17004. else if ( CHAR_BIT*sizeof(short) == 8 )
  17005. return H5T_NATIVE_SHORT;
  17006. else if ( CHAR_BIT*sizeof(int) == 8 )
  17007. return H5T_NATIVE_INT;
  17008. else if ( CHAR_BIT*sizeof(long) == 8 )
  17009. return H5T_NATIVE_LONG;
  17010. else if ( CHAR_BIT*sizeof(long long) == 8 )
  17011. return H5T_NATIVE_LLONG;
  17012. else
  17013. return -1;
  17014. case MAT_T_UINT8:
  17015. if ( CHAR_BIT == 8 )
  17016. return H5T_NATIVE_UCHAR;
  17017. else if ( CHAR_BIT*sizeof(short) == 8 )
  17018. return H5T_NATIVE_USHORT;
  17019. else if ( CHAR_BIT*sizeof(int) == 8 )
  17020. return H5T_NATIVE_UINT;
  17021. else if ( CHAR_BIT*sizeof(long) == 8 )
  17022. return H5T_NATIVE_ULONG;
  17023. else if ( CHAR_BIT*sizeof(long long) == 8 )
  17024. return H5T_NATIVE_ULLONG;
  17025. else
  17026. return -1;
  17027. case MAT_T_UTF8:
  17028. return H5T_NATIVE_CHAR;
  17029. default:
  17030. return -1;
  17031. }
  17032. }
  17033. static hid_t
  17034. Mat_dims_type_to_hid_t(void)
  17035. {
  17036. if ( sizeof(size_t) == H5Tget_size(H5T_NATIVE_HSIZE) )
  17037. return H5T_NATIVE_HSIZE;
  17038. else if ( sizeof(size_t) == H5Tget_size(H5T_NATIVE_ULLONG) )
  17039. return H5T_NATIVE_ULLONG;
  17040. else if ( sizeof(size_t) == H5Tget_size(H5T_NATIVE_ULONG) )
  17041. return H5T_NATIVE_ULONG;
  17042. else if ( sizeof(size_t) == H5Tget_size(H5T_NATIVE_UINT) )
  17043. return H5T_NATIVE_UINT;
  17044. else if ( sizeof(size_t) == H5Tget_size(H5T_NATIVE_USHORT) )
  17045. return H5T_NATIVE_USHORT;
  17046. else
  17047. return -1;
  17048. }
  17049. static void
  17050. Mat_H5GetChunkSize(size_t rank,hsize_t *dims,hsize_t *chunk_dims)
  17051. {
  17052. hsize_t i, j, chunk_size = 1;
  17053. for ( i = 0; i < rank; i++ ) {
  17054. chunk_dims[i] = 1;
  17055. for ( j = 4096/chunk_size; j > 1; j >>= 1 ) {
  17056. if ( dims[i] >= j ) {
  17057. chunk_dims[i] = j;
  17058. break;
  17059. }
  17060. }
  17061. chunk_size *= chunk_dims[i];
  17062. }
  17063. }
  17064. static void
  17065. Mat_H5ReadClassType(matvar_t *matvar,hid_t dset_id)
  17066. {
  17067. hid_t attr_id, type_id;
  17068. attr_id = H5Aopen_by_name(dset_id,".","MATLAB_class",H5P_DEFAULT,H5P_DEFAULT);
  17069. type_id = H5Aget_type(attr_id);
  17070. if ( H5T_STRING == H5Tget_class(type_id) ) {
  17071. char *class_str = (char*)calloc(H5Tget_size(type_id)+1,1);
  17072. if ( NULL != class_str ) {
  17073. hid_t class_id = H5Tcopy(H5T_C_S1);
  17074. H5Tset_size(class_id,H5Tget_size(type_id));
  17075. H5Aread(attr_id,class_id,class_str);
  17076. H5Tclose(class_id);
  17077. matvar->class_type = Mat_class_str_to_id(class_str);
  17078. if ( MAT_C_EMPTY == matvar->class_type ) {
  17079. /* Check if this is a logical variable */
  17080. if ( !strcmp(class_str, "logical") ) {
  17081. int_decode = 0;
  17082. hid_t attr_id2;
  17083. matvar->isLogical = MAT_F_LOGICAL;
  17084. if ( H5Aexists_by_name(dset_id,".","MATLAB_int_decode",H5P_DEFAULT) ) {
  17085. attr_id2 = H5Aopen_by_name(dset_id,".","MATLAB_int_decode",H5P_DEFAULT,H5P_DEFAULT);
  17086. /* FIXME: Check that dataspace is scalar */
  17087. H5Aread(attr_id2,H5T_NATIVE_INT,&int_decode);
  17088. H5Aclose(attr_id2);
  17089. }
  17090. switch (int_decode) {
  17091. case 1:
  17092. matvar->class_type = MAT_C_UINT8;
  17093. break;
  17094. case 2:
  17095. matvar->class_type = MAT_C_UINT16;
  17096. break;
  17097. case 4:
  17098. matvar->class_type = MAT_C_UINT32;
  17099. break;
  17100. default:
  17101. break;
  17102. }
  17103. }
  17104. }
  17105. matvar->data_type = Mat_ClassToType73(matvar->class_type);
  17106. free(class_str);
  17107. }
  17108. }
  17109. H5Tclose(type_id);
  17110. H5Aclose(attr_id);
  17111. }
  17112. static void
  17113. Mat_H5ReadDatasetInfo(mat_t *mat,matvar_t *matvar,hid_t dset_id)
  17114. {
  17115. ssize_t name_len;
  17116. /* FIXME */
  17117. hsize_t dims[10];
  17118. hid_t attr_id,type_id,space_id;
  17119. #if 0
  17120. matvar->fp = mat;
  17121. name_len = H5Gget_objname_by_idx(fid,mat->next_index,NULL,0);
  17122. matvar->name = malloc(1+name_len);
  17123. if ( matvar->name ) {
  17124. name_len = H5Gget_objname_by_idx(fid,mat->next_index,
  17125. matvar->name,1+name_len);
  17126. matvar->name[name_len] = '\0';
  17127. }
  17128. dset_id = H5Dopen(fid,matvar->name);
  17129. #endif
  17130. /* Get the HDF5 name of the variable */
  17131. name_len = H5Iget_name(dset_id,NULL,0);
  17132. if ( name_len > 0 ) {
  17133. matvar->internal->hdf5_name = (char*)malloc(name_len+1);
  17134. (void)H5Iget_name(dset_id,matvar->internal->hdf5_name,name_len+1);
  17135. } else {
  17136. /* Can not get an internal name, so leave the identifier open */
  17137. matvar->internal->id = dset_id;
  17138. }
  17139. space_id = H5Dget_space(dset_id);
  17140. matvar->rank = H5Sget_simple_extent_ndims(space_id);
  17141. matvar->dims = (size_t*)malloc(matvar->rank*sizeof(*matvar->dims));
  17142. if ( NULL != matvar->dims ) {
  17143. int k;
  17144. H5Sget_simple_extent_dims(space_id,dims,NULL);
  17145. for ( k = 0; k < matvar->rank; k++ )
  17146. matvar->dims[k] = dims[matvar->rank - k - 1];
  17147. H5Sclose(space_id);
  17148. } else {
  17149. H5Sclose(space_id);
  17150. Mat_Critical("Error allocating memory for matvar->dims");
  17151. return;
  17152. }
  17153. Mat_H5ReadClassType(matvar, dset_id);
  17154. if ( H5Aexists_by_name(dset_id,".","MATLAB_global",H5P_DEFAULT) ) {
  17155. attr_id = H5Aopen_by_name(dset_id,".","MATLAB_global",H5P_DEFAULT,H5P_DEFAULT);
  17156. /* FIXME: Check that dataspace is scalar */
  17157. H5Aread(attr_id,H5T_NATIVE_INT,&matvar->isGlobal);
  17158. H5Aclose(attr_id);
  17159. }
  17160. /* Check for attribute that indicates an empty array */
  17161. if ( H5Aexists_by_name(dset_id,".","MATLAB_empty",H5P_DEFAULT) ) {
  17162. int empty = 0;
  17163. attr_id = H5Aopen_by_name(dset_id,".","MATLAB_empty",H5P_DEFAULT,H5P_DEFAULT);
  17164. /* FIXME: Check that dataspace is scalar */
  17165. H5Aread(attr_id,H5T_NATIVE_INT,&empty);
  17166. H5Aclose(attr_id);
  17167. if ( empty ) {
  17168. matvar->rank = matvar->dims[0];
  17169. free(matvar->dims);
  17170. matvar->dims = (size_t*)calloc(matvar->rank,sizeof(*matvar->dims));
  17171. H5Dread(dset_id,Mat_dims_type_to_hid_t(),H5S_ALL,H5S_ALL,
  17172. H5P_DEFAULT,matvar->dims);
  17173. }
  17174. }
  17175. /* Test if dataset type is compound and if so if it's complex */
  17176. type_id = H5Dget_type(dset_id);
  17177. if ( H5T_COMPOUND == H5Tget_class(type_id) ) {
  17178. /* FIXME: Any more checks? */
  17179. matvar->isComplex = MAT_F_COMPLEX;
  17180. }
  17181. H5Tclose(type_id);
  17182. /* If the dataset is a cell array read the info of the cells */
  17183. if ( MAT_C_CELL == matvar->class_type ) {
  17184. matvar_t **cells;
  17185. int i,ncells = 1;
  17186. hobj_ref_t *ref_ids;
  17187. for ( i = 0; i < matvar->rank; i++ )
  17188. ncells *= matvar->dims[i];
  17189. matvar->data_size = sizeof(matvar_t**);
  17190. matvar->nbytes = ncells*matvar->data_size;
  17191. matvar->data = malloc(matvar->nbytes);
  17192. cells = (matvar_t**)matvar->data;
  17193. if ( ncells ) {
  17194. ref_ids = (hobj_ref_t*)malloc(ncells*sizeof(*ref_ids));
  17195. H5Dread(dset_id,H5T_STD_REF_OBJ,H5S_ALL,H5S_ALL,H5P_DEFAULT,
  17196. ref_ids);
  17197. for ( i = 0; i < ncells; i++ ) {
  17198. hid_t ref_id;
  17199. cells[i] = Mat_VarCalloc();
  17200. cells[i]->internal->hdf5_ref = ref_ids[i];
  17201. /* Closing of ref_id is done in Mat_H5ReadNextReferenceInfo */
  17202. ref_id = H5Rdereference(dset_id,H5R_OBJECT,ref_ids+i);
  17203. cells[i]->internal->id = ref_id;
  17204. cells[i]->internal->fp = matvar->internal->fp;
  17205. Mat_H5ReadNextReferenceInfo(ref_id,cells[i],mat);
  17206. }
  17207. free(ref_ids);
  17208. }
  17209. } else if ( MAT_C_STRUCT == matvar->class_type ) {
  17210. /* Empty structures can be a dataset */
  17211. /* Check if the structure defines its fields in MATLAB_fields */
  17212. if ( H5Aexists_by_name(dset_id,".","MATLAB_fields",H5P_DEFAULT) ) {
  17213. int i;
  17214. hid_t field_id;
  17215. hsize_t nfields;
  17216. hvl_t *fieldnames_vl;
  17217. attr_id = H5Aopen_by_name(dset_id,".","MATLAB_fields",H5P_DEFAULT,H5P_DEFAULT);
  17218. space_id = H5Aget_space(attr_id);
  17219. (void)H5Sget_simple_extent_dims(space_id,&nfields,NULL);
  17220. field_id = H5Aget_type(attr_id);
  17221. fieldnames_vl = (hvl_t*)malloc(nfields*sizeof(*fieldnames_vl));
  17222. H5Aread(attr_id,field_id,fieldnames_vl);
  17223. matvar->internal->num_fields = nfields;
  17224. matvar->internal->fieldnames =
  17225. (char**)calloc(nfields,sizeof(*matvar->internal->fieldnames));
  17226. for ( i = 0; i < nfields; i++ ) {
  17227. matvar->internal->fieldnames[i] =
  17228. (char*)calloc(fieldnames_vl[i].len+1,1);
  17229. memcpy(matvar->internal->fieldnames[i],fieldnames_vl[i].p,
  17230. fieldnames_vl[i].len);
  17231. }
  17232. H5Dvlen_reclaim(field_id,space_id,H5P_DEFAULT,
  17233. fieldnames_vl);
  17234. H5Sclose(space_id);
  17235. H5Tclose(field_id);
  17236. H5Aclose(attr_id);
  17237. free(fieldnames_vl);
  17238. }
  17239. }
  17240. }
  17241. static void
  17242. Mat_H5ReadGroupInfo(mat_t *mat,matvar_t *matvar,hid_t dset_id)
  17243. {
  17244. ssize_t name_len;
  17245. int k, fields_are_variables = 1;
  17246. /* FIXME */
  17247. hsize_t dims[10],nfields=0,numel;
  17248. hid_t attr_id,type_id,space_id,field_id,field_type_id;
  17249. matvar_t **fields;
  17250. H5O_type_t obj_type;
  17251. #if 0
  17252. matvar->fp = mat;
  17253. name_len = H5Gget_objname_by_idx(fid,mat->next_index,NULL,0);
  17254. matvar->name = malloc(1+name_len);
  17255. if ( matvar->name ) {
  17256. name_len = H5Gget_objname_by_idx(fid,mat->next_index,
  17257. matvar->name,1+name_len);
  17258. matvar->name[name_len] = '\0';
  17259. }
  17260. dset_id = H5Gopen(fid,matvar->name);
  17261. #endif
  17262. /* Get the HDF5 name of the variable */
  17263. name_len = H5Iget_name(dset_id,NULL,0);
  17264. if ( name_len > 0 ) {
  17265. matvar->internal->hdf5_name = (char*)malloc(name_len+1);
  17266. (void)H5Iget_name(dset_id,matvar->internal->hdf5_name,name_len+1);
  17267. } else {
  17268. /* Can not get an internal name, so leave the identifier open */
  17269. matvar->internal->id = dset_id;
  17270. }
  17271. Mat_H5ReadClassType(matvar,dset_id);
  17272. /* Check if the variable is global */
  17273. if ( H5Aexists_by_name(dset_id,".","MATLAB_global",H5P_DEFAULT) ) {
  17274. attr_id = H5Aopen_by_name(dset_id,".","MATLAB_global",H5P_DEFAULT,H5P_DEFAULT);
  17275. /* FIXME: Check that dataspace is scalar */
  17276. H5Aread(attr_id,H5T_NATIVE_INT,&matvar->isGlobal);
  17277. H5Aclose(attr_id);
  17278. }
  17279. /* Check if the variable is sparse */
  17280. if ( H5Aexists_by_name(dset_id,".","MATLAB_sparse",H5P_DEFAULT) ) {
  17281. hid_t sparse_dset_id;
  17282. unsigned nrows = 0;
  17283. attr_id = H5Aopen_by_name(dset_id,".","MATLAB_sparse",H5P_DEFAULT,H5P_DEFAULT);
  17284. H5Aread(attr_id,H5T_NATIVE_UINT,&nrows);
  17285. H5Aclose(attr_id);
  17286. matvar->class_type = MAT_C_SPARSE;
  17287. matvar->rank = 2;
  17288. matvar->dims = (size_t*)malloc(matvar->rank*sizeof(*matvar->dims));
  17289. if (matvar->dims == NULL) {
  17290. Mat_Critical("Error allocating memory for matvar->dims");
  17291. return;
  17292. }
  17293. matvar->dims[0] = nrows;
  17294. if ( H5Lexists(dset_id,"jc",H5P_DEFAULT) ) {
  17295. sparse_dset_id = H5Dopen(dset_id,"jc",H5P_DEFAULT);
  17296. space_id = H5Dget_space(sparse_dset_id);
  17297. (void)H5Sget_simple_extent_dims(space_id,dims,NULL);
  17298. matvar->dims[1] = dims[0] - 1;
  17299. H5Sclose(space_id);
  17300. H5Dclose(sparse_dset_id);
  17301. }
  17302. /* Test if dataset type is compound and if so if it's complex */
  17303. if ( H5Lexists(dset_id,"data",H5P_DEFAULT) ) {
  17304. sparse_dset_id = H5Dopen(dset_id,"data",H5P_DEFAULT);
  17305. type_id = H5Dget_type(sparse_dset_id);
  17306. if ( H5T_COMPOUND == H5Tget_class(type_id) ) {
  17307. /* FIXME: Any more checks? */
  17308. matvar->isComplex = MAT_F_COMPLEX;
  17309. }
  17310. H5Tclose(type_id);
  17311. H5Dclose(sparse_dset_id);
  17312. }
  17313. return;
  17314. }
  17315. /* Check if the structure defines its fields in MATLAB_fields */
  17316. if ( H5Aexists_by_name(dset_id,".","MATLAB_fields",H5P_DEFAULT) ) {
  17317. hvl_t *fieldnames_vl;
  17318. attr_id = H5Aopen_by_name(dset_id,".","MATLAB_fields",H5P_DEFAULT,H5P_DEFAULT);
  17319. space_id = H5Aget_space(attr_id);
  17320. (void)H5Sget_simple_extent_dims(space_id,&nfields,NULL);
  17321. field_id = H5Aget_type(attr_id);
  17322. fieldnames_vl = (hvl_t*)malloc(nfields*sizeof(*fieldnames_vl));
  17323. H5Aread(attr_id,field_id,fieldnames_vl);
  17324. matvar->internal->num_fields = nfields;
  17325. matvar->internal->fieldnames =
  17326. (char**)malloc(nfields*sizeof(*matvar->internal->fieldnames));
  17327. for ( k = 0; k < nfields; k++ ) {
  17328. matvar->internal->fieldnames[k] = (char*)calloc(fieldnames_vl[k].len+1,1);
  17329. memcpy(matvar->internal->fieldnames[k],fieldnames_vl[k].p,
  17330. fieldnames_vl[k].len);
  17331. }
  17332. H5Dvlen_reclaim(field_id,space_id,H5P_DEFAULT,fieldnames_vl);
  17333. H5Sclose(space_id);
  17334. H5Tclose(field_id);
  17335. H5Aclose(attr_id);
  17336. free(fieldnames_vl);
  17337. } else {
  17338. H5G_info_t group_info;
  17339. matvar->internal->num_fields = 0;
  17340. H5Gget_info(dset_id, &group_info);
  17341. if ( group_info.nlinks > 0 ) {
  17342. struct h5_read_group_info_iter_data group_data = {0};
  17343. herr_t herr;
  17344. /* First iteration to retrieve number of relevant links */
  17345. herr = H5Literate_by_name(dset_id, matvar->internal->hdf5_name, H5_INDEX_NAME,
  17346. H5_ITER_NATIVE, NULL, Mat_H5ReadGroupInfoIterate,
  17347. (void *)&group_data, H5P_DEFAULT);
  17348. if ( herr > 0 && group_data.nfields > 0 ) {
  17349. matvar->internal->fieldnames =
  17350. (char**)calloc(group_data.nfields,sizeof(*matvar->internal->fieldnames));
  17351. group_data.nfields = 0;
  17352. group_data.matvar = matvar;
  17353. if ( matvar->internal->fieldnames != NULL ) {
  17354. /* Second iteration to fill fieldnames */
  17355. H5Literate_by_name(dset_id, matvar->internal->hdf5_name, H5_INDEX_NAME,
  17356. H5_ITER_NATIVE, NULL, Mat_H5ReadGroupInfoIterate,
  17357. (void *)&group_data, H5P_DEFAULT);
  17358. }
  17359. matvar->internal->num_fields = (unsigned)group_data.nfields;
  17360. nfields = group_data.nfields;
  17361. }
  17362. }
  17363. }
  17364. if ( nfields > 0 ) {
  17365. H5O_info_t object_info;
  17366. H5Oget_info_by_name(dset_id, matvar->internal->fieldnames[0], &object_info, H5P_DEFAULT);
  17367. obj_type = object_info.type;
  17368. } else {
  17369. obj_type = H5O_TYPE_UNKNOWN;
  17370. }
  17371. if ( obj_type == H5O_TYPE_DATASET ) {
  17372. field_id = H5Dopen(dset_id,matvar->internal->fieldnames[0],H5P_DEFAULT);
  17373. field_type_id = H5Dget_type(field_id);
  17374. if ( H5T_REFERENCE == H5Tget_class(field_type_id) ) {
  17375. /* Check if the field has the MATLAB_class attribute. If so, it
  17376. * means the structure is a scalar. Otherwise, the dimensions of
  17377. * the field dataset is the dimensions of the structure
  17378. */
  17379. if ( H5Aexists_by_name(field_id,".","MATLAB_class",H5P_DEFAULT) ) {
  17380. attr_id = H5Aopen_by_name(field_id,".","MATLAB_class",H5P_DEFAULT,H5P_DEFAULT);
  17381. H5Aclose(attr_id);
  17382. matvar->rank = 2;
  17383. matvar->dims = (size_t*)malloc(2*sizeof(*matvar->dims));
  17384. if (matvar->dims == NULL) {
  17385. H5Tclose(field_type_id);
  17386. H5Dclose(field_id);
  17387. Mat_Critical("Error allocating memory for matvar->dims");
  17388. return;
  17389. }
  17390. matvar->dims[0] = 1;
  17391. matvar->dims[1] = 1;
  17392. numel = 1;
  17393. } else {
  17394. space_id = H5Dget_space(field_id);
  17395. matvar->rank = H5Sget_simple_extent_ndims(space_id);
  17396. matvar->dims = (size_t*)malloc(matvar->rank*sizeof(*matvar->dims));
  17397. if (matvar->dims == NULL) {
  17398. H5Tclose(field_type_id);
  17399. H5Dclose(field_id);
  17400. H5Sclose(space_id);
  17401. Mat_Critical("Error allocating memory for matvar->dims");
  17402. return;
  17403. }
  17404. (void)H5Sget_simple_extent_dims(space_id,dims,NULL);
  17405. numel = 1;
  17406. for ( k = 0; k < matvar->rank; k++ ) {
  17407. matvar->dims[k] = dims[matvar->rank - k - 1];
  17408. numel *= matvar->dims[k];
  17409. }
  17410. H5Sclose(space_id);
  17411. fields_are_variables = 0;
  17412. }
  17413. } else {
  17414. /* Structure should be a scalar */
  17415. matvar->rank = 2;
  17416. matvar->dims = (size_t*)malloc(2*sizeof(*matvar->dims));
  17417. if (matvar->dims == NULL) {
  17418. H5Tclose(field_type_id);
  17419. H5Dclose(field_id);
  17420. Mat_Critical("Error allocating memory for matvar->dims");
  17421. return;
  17422. }
  17423. matvar->dims[0] = 1;
  17424. matvar->dims[1] = 1;
  17425. numel = 1;
  17426. }
  17427. H5Tclose(field_type_id);
  17428. H5Dclose(field_id);
  17429. } else {
  17430. /* Structure should be a scalar */
  17431. numel = 1;
  17432. matvar->rank = 2;
  17433. matvar->dims = (size_t*)malloc(2*sizeof(*matvar->dims));
  17434. if (matvar->dims == NULL) {
  17435. Mat_Critical("Error allocating memory for matvar->dims");
  17436. return;
  17437. }
  17438. matvar->dims[0] = 1;
  17439. matvar->dims[1] = 1;
  17440. }
  17441. if ( numel < 1 || nfields < 1 )
  17442. return;
  17443. fields = (matvar_t**)malloc(nfields*numel*sizeof(*fields));
  17444. matvar->data = fields;
  17445. matvar->data_size = sizeof(*fields);
  17446. matvar->nbytes = nfields*numel*matvar->data_size;
  17447. if ( NULL != fields ) {
  17448. for ( k = 0; k < nfields; k++ ) {
  17449. int l;
  17450. H5O_info_t object_info;
  17451. fields[k] = NULL;
  17452. H5Oget_info_by_name(dset_id, matvar->internal->fieldnames[k], &object_info, H5P_DEFAULT);
  17453. if ( object_info.type == H5O_TYPE_DATASET ) {
  17454. field_id = H5Dopen(dset_id,matvar->internal->fieldnames[k],
  17455. H5P_DEFAULT);
  17456. if ( !fields_are_variables ) {
  17457. hobj_ref_t *ref_ids = (hobj_ref_t*)malloc(numel*sizeof(*ref_ids));
  17458. H5Dread(field_id,H5T_STD_REF_OBJ,H5S_ALL,H5S_ALL,
  17459. H5P_DEFAULT,ref_ids);
  17460. for ( l = 0; l < numel; l++ ) {
  17461. hid_t ref_id;
  17462. fields[l*nfields+k] = Mat_VarCalloc();
  17463. fields[l*nfields+k]->name =
  17464. mat_strdup(matvar->internal->fieldnames[k]);
  17465. fields[l*nfields+k]->internal->hdf5_ref=ref_ids[l];
  17466. /* Get the HDF5 name of the variable */
  17467. name_len = H5Iget_name(field_id,NULL,0);
  17468. if ( name_len > 0 ) {
  17469. fields[l*nfields+k]->internal->hdf5_name =
  17470. (char*)malloc(name_len+1);
  17471. (void)H5Iget_name(field_id,
  17472. fields[l*nfields+k]->internal->hdf5_name,
  17473. name_len+1);
  17474. }
  17475. /* Closing of ref_id is done in
  17476. * Mat_H5ReadNextReferenceInfo
  17477. */
  17478. ref_id = H5Rdereference(field_id,H5R_OBJECT,
  17479. ref_ids+l);
  17480. fields[l*nfields+k]->internal->id=ref_id;
  17481. Mat_H5ReadNextReferenceInfo(ref_id,fields[l*nfields+k],mat);
  17482. }
  17483. free(ref_ids);
  17484. } else {
  17485. fields[k] = Mat_VarCalloc();
  17486. fields[k]->internal->fp = mat;
  17487. fields[k]->name =
  17488. mat_strdup(matvar->internal->fieldnames[k]);
  17489. Mat_H5ReadDatasetInfo(mat,fields[k],field_id);
  17490. }
  17491. H5Dclose(field_id);
  17492. } else if ( object_info.type == H5O_TYPE_GROUP ) {
  17493. field_id = H5Gopen(dset_id,matvar->internal->fieldnames[k],
  17494. H5P_DEFAULT);
  17495. if ( -1 < field_id ) {
  17496. fields[k] = Mat_VarCalloc();
  17497. fields[k]->internal->fp = mat;
  17498. fields[k]->name = mat_strdup(matvar->internal->fieldnames[k]);
  17499. Mat_H5ReadGroupInfo(mat,fields[k],field_id);
  17500. H5Gclose(field_id);
  17501. }
  17502. }
  17503. }
  17504. }
  17505. }
  17506. static herr_t
  17507. Mat_H5ReadGroupInfoIterate(hid_t dset_id, const char *name, const H5L_info_t *info, void *op_data)
  17508. {
  17509. matvar_t *matvar;
  17510. H5O_info_t object_info;
  17511. struct h5_read_group_info_iter_data *group_data;
  17512. /* FIXME: follow symlinks, datatypes? */
  17513. H5Oget_info_by_name(dset_id, name, &object_info, H5P_DEFAULT);
  17514. if ( H5O_TYPE_DATASET != object_info.type && H5O_TYPE_GROUP != object_info.type )
  17515. return 0;
  17516. group_data = (struct h5_read_group_info_iter_data *)op_data;
  17517. if ( group_data == NULL )
  17518. return -1;
  17519. matvar = group_data->matvar;
  17520. switch ( object_info.type ) {
  17521. case H5O_TYPE_DATASET:
  17522. {
  17523. if ( matvar != NULL ) {
  17524. matvar->internal->fieldnames[group_data->nfields] =
  17525. (char*)calloc(strlen(name)+1,sizeof(char));
  17526. strcpy(matvar->internal->fieldnames[group_data->nfields], name);
  17527. }
  17528. group_data->nfields++;
  17529. break;
  17530. }
  17531. case H5O_TYPE_GROUP:
  17532. {
  17533. /* Check that this is not the /#refs# group */
  17534. if ( 0 == strcmp(name,"#refs#") )
  17535. return 0;
  17536. if ( matvar != NULL ) {
  17537. matvar->internal->fieldnames[group_data->nfields] =
  17538. (char*)calloc(strlen(name)+1,sizeof(char));
  17539. strcpy(matvar->internal->fieldnames[group_data->nfields], name);
  17540. }
  17541. group_data->nfields++;
  17542. break;
  17543. }
  17544. default:
  17545. break;
  17546. }
  17547. return 1;
  17548. }
  17549. static void
  17550. Mat_H5ReadNextReferenceInfo(hid_t ref_id,matvar_t *matvar,mat_t *mat)
  17551. {
  17552. if( ref_id < 0 || matvar == NULL)
  17553. return;
  17554. switch ( H5Iget_type(ref_id) ) {
  17555. case H5I_DATASET:
  17556. {
  17557. /* FIXME */
  17558. hsize_t dims[10];
  17559. hid_t attr_id,type_id,dset_id,space_id;
  17560. /* matvar->fp = mat; */
  17561. dset_id = ref_id;
  17562. #if 0
  17563. /* Get the HDF5 name of the variable */
  17564. name_len = H5Iget_name(dset_id,NULL,0);
  17565. matvar->hdf5_name = malloc(name_len+1);
  17566. (void)H5Iget_name(dset_id,matvar->hdf5_name,name_len);
  17567. printf("%s\n",matvar->hdf5_name);
  17568. #endif
  17569. /* Get the rank and dimensions of the data */
  17570. space_id = H5Dget_space(dset_id);
  17571. matvar->rank = H5Sget_simple_extent_ndims(space_id);
  17572. matvar->dims = (size_t*)malloc(matvar->rank*sizeof(*matvar->dims));
  17573. if ( NULL == matvar->dims ) {
  17574. H5Sclose(space_id);
  17575. break;
  17576. } else {
  17577. int k;
  17578. H5Sget_simple_extent_dims(space_id,dims,NULL);
  17579. for ( k = 0; k < matvar->rank; k++ )
  17580. matvar->dims[k] = dims[matvar->rank - k - 1];
  17581. }
  17582. H5Sclose(space_id);
  17583. Mat_H5ReadClassType(matvar,dset_id);
  17584. if ( H5Aexists_by_name(dset_id,".","MATLAB_global",H5P_DEFAULT) ) {
  17585. attr_id = H5Aopen_by_name(dset_id,".","MATLAB_global",H5P_DEFAULT,H5P_DEFAULT);
  17586. /* FIXME: Check that dataspace is scalar */
  17587. H5Aread(attr_id,H5T_NATIVE_INT,&matvar->isGlobal);
  17588. H5Aclose(attr_id);
  17589. }
  17590. /* Check for attribute that indicates an empty array */
  17591. if ( H5Aexists_by_name(dset_id,".","MATLAB_empty",H5P_DEFAULT) ) {
  17592. int empty = 0;
  17593. attr_id = H5Aopen_by_name(dset_id,".","MATLAB_empty",H5P_DEFAULT,H5P_DEFAULT);
  17594. /* FIXME: Check that dataspace is scalar */
  17595. H5Aread(attr_id,H5T_NATIVE_INT,&empty);
  17596. H5Aclose(attr_id);
  17597. if ( empty ) {
  17598. matvar->rank = matvar->dims[0];
  17599. free(matvar->dims);
  17600. matvar->dims = (size_t*)calloc(matvar->rank,sizeof(*matvar->dims));
  17601. H5Dread(dset_id,Mat_dims_type_to_hid_t(),H5S_ALL,H5S_ALL,
  17602. H5P_DEFAULT,matvar->dims);
  17603. }
  17604. }
  17605. /* Test if dataset type is compound and if so if it's complex */
  17606. type_id = H5Dget_type(dset_id);
  17607. if ( H5T_COMPOUND == H5Tget_class(type_id) ) {
  17608. /* FIXME: Any more checks? */
  17609. matvar->isComplex = MAT_F_COMPLEX;
  17610. }
  17611. H5Tclose(type_id);
  17612. /* If the dataset is a cell array read the info of the cells */
  17613. if ( MAT_C_CELL == matvar->class_type ) {
  17614. matvar_t **cells;
  17615. int i,ncells = 1;
  17616. for ( i = 0; i < matvar->rank; i++ )
  17617. ncells *= matvar->dims[i];
  17618. matvar->data_size = sizeof(matvar_t**);
  17619. matvar->nbytes = ncells*matvar->data_size;
  17620. matvar->data = malloc(matvar->nbytes);
  17621. cells = (matvar_t**)matvar->data;
  17622. if ( ncells > 0 ) {
  17623. hobj_ref_t *ref_ids;
  17624. ref_ids = (hobj_ref_t*)malloc(ncells*sizeof(*ref_ids));
  17625. H5Dread(dset_id,H5T_STD_REF_OBJ,H5S_ALL,H5S_ALL,H5P_DEFAULT,
  17626. ref_ids);
  17627. for ( i = 0; i < ncells; i++ ) {
  17628. hid_t ref_id;
  17629. cells[i] = Mat_VarCalloc();
  17630. cells[i]->internal->hdf5_ref = ref_ids[i];
  17631. /* Closing of ref_id is done in Mat_H5ReadNextReferenceInfo */
  17632. ref_id = H5Rdereference(dset_id,H5R_OBJECT,ref_ids+i);
  17633. cells[i]->internal->id=ref_id;
  17634. cells[i]->internal->fp=matvar->internal->fp;
  17635. Mat_H5ReadNextReferenceInfo(ref_id,cells[i],mat);
  17636. }
  17637. free(ref_ids);
  17638. }
  17639. } else if ( MAT_C_STRUCT == matvar->class_type ) {
  17640. /* Empty structures can be a dataset */
  17641. /* Check if the structure defines its fields in MATLAB_fields */
  17642. if ( H5Aexists_by_name(dset_id,".","MATLAB_fields",H5P_DEFAULT) ) {
  17643. int i;
  17644. hid_t field_id;
  17645. hsize_t nfields;
  17646. hvl_t *fieldnames_vl;
  17647. attr_id = H5Aopen_by_name(dset_id,".","MATLAB_fields",H5P_DEFAULT,H5P_DEFAULT);
  17648. space_id = H5Aget_space(attr_id);
  17649. (void)H5Sget_simple_extent_dims(space_id,&nfields,NULL);
  17650. field_id = H5Aget_type(attr_id);
  17651. fieldnames_vl = (hvl_t*)malloc(nfields*sizeof(*fieldnames_vl));
  17652. H5Aread(attr_id,field_id,fieldnames_vl);
  17653. matvar->internal->num_fields = nfields;
  17654. matvar->internal->fieldnames =
  17655. (char**)malloc(nfields*sizeof(*matvar->internal->fieldnames));
  17656. for ( i = 0; i < nfields; i++ ) {
  17657. matvar->internal->fieldnames[i] =
  17658. (char*)calloc(fieldnames_vl[i].len+1,1);
  17659. memcpy(matvar->internal->fieldnames[i],
  17660. fieldnames_vl[i].p,fieldnames_vl[i].len);
  17661. }
  17662. H5Dvlen_reclaim(field_id,space_id,H5P_DEFAULT,
  17663. fieldnames_vl);
  17664. H5Sclose(space_id);
  17665. H5Tclose(field_id);
  17666. H5Aclose(attr_id);
  17667. free(fieldnames_vl);
  17668. }
  17669. }
  17670. if ( matvar->internal->id != dset_id ) {
  17671. /* Close dataset and increment count */
  17672. H5Dclose(dset_id);
  17673. }
  17674. /*H5Dclose(dset_id);*/
  17675. break;
  17676. }
  17677. case H5I_GROUP:
  17678. {
  17679. Mat_H5ReadGroupInfo(mat,matvar,ref_id);
  17680. break;
  17681. }
  17682. default:
  17683. break;
  17684. }
  17685. return;
  17686. }
  17687. static void
  17688. Mat_H5ReadNextReferenceData(hid_t ref_id,matvar_t *matvar,mat_t *mat)
  17689. {
  17690. int k;
  17691. size_t numel;
  17692. hid_t dset_id;
  17693. if( ref_id < 0 || matvar == NULL)
  17694. return;
  17695. /* If the datatype with references is a cell, we've already read info into
  17696. * the variable data, so just loop over each cell element and call
  17697. * Mat_H5ReadNextReferenceData on it.
  17698. */
  17699. if ( MAT_C_CELL == matvar->class_type ) {
  17700. matvar_t **cells = (matvar_t**)matvar->data;
  17701. numel = 1;
  17702. for ( k = 0; k < matvar->rank; k++ )
  17703. numel *= matvar->dims[k];
  17704. for ( k = 0; k < numel; k++ )
  17705. Mat_H5ReadNextReferenceData(cells[k]->internal->id,cells[k],mat);
  17706. return;
  17707. }
  17708. switch ( H5Iget_type(ref_id) ) {
  17709. case H5I_DATASET:
  17710. {
  17711. hid_t data_type_id;
  17712. numel = 1;
  17713. for ( k = 0; k < matvar->rank; k++ )
  17714. numel *= matvar->dims[k];
  17715. if ( MAT_C_CHAR == matvar->class_type ) {
  17716. matvar->data_type = MAT_T_UINT8;
  17717. matvar->data_size = Mat_SizeOf(MAT_T_UINT8);
  17718. data_type_id = Mat_data_type_to_hid_t(MAT_T_UINT8);
  17719. } else if ( MAT_C_STRUCT == matvar->class_type ) {
  17720. /* Empty structure array */
  17721. break;
  17722. } else {
  17723. matvar->data_size = Mat_SizeOfClass(matvar->class_type);
  17724. data_type_id = Mat_class_type_to_hid_t(matvar->class_type);
  17725. }
  17726. matvar->nbytes = numel*matvar->data_size;
  17727. if ( matvar->nbytes < 1 ) {
  17728. H5Dclose(ref_id);
  17729. break;
  17730. }
  17731. dset_id = ref_id;
  17732. if ( !matvar->isComplex ) {
  17733. matvar->data = malloc(matvar->nbytes);
  17734. if ( NULL != matvar->data ) {
  17735. H5Dread(dset_id,data_type_id,H5S_ALL,H5S_ALL,H5P_DEFAULT,
  17736. matvar->data);
  17737. }
  17738. } else {
  17739. mat_complex_split_t *complex_data;
  17740. hid_t h5_complex_base,h5_complex;
  17741. complex_data = (mat_complex_split_t*)malloc(sizeof(*complex_data));
  17742. complex_data->Re = malloc(matvar->nbytes);
  17743. complex_data->Im = malloc(matvar->nbytes);
  17744. h5_complex_base = data_type_id;
  17745. h5_complex = H5Tcreate(H5T_COMPOUND,
  17746. H5Tget_size(h5_complex_base));
  17747. H5Tinsert(h5_complex,"real",0,h5_complex_base);
  17748. H5Dread(dset_id,h5_complex,H5S_ALL,H5S_ALL,H5P_DEFAULT,
  17749. complex_data->Re);
  17750. H5Tclose(h5_complex);
  17751. h5_complex = H5Tcreate(H5T_COMPOUND,
  17752. H5Tget_size(h5_complex_base));
  17753. H5Tinsert(h5_complex,"imag",0,h5_complex_base);
  17754. H5Dread(dset_id,h5_complex,H5S_ALL,H5S_ALL,H5P_DEFAULT,
  17755. complex_data->Im);
  17756. H5Tclose(h5_complex);
  17757. matvar->data = complex_data;
  17758. }
  17759. H5Dclose(dset_id);
  17760. break;
  17761. }
  17762. case H5I_GROUP:
  17763. {
  17764. matvar_t **fields;
  17765. int i,nfields = 0;
  17766. if ( MAT_C_SPARSE == matvar->class_type ) {
  17767. Mat_VarRead73(mat,matvar);
  17768. } else {
  17769. if ( !matvar->nbytes || !matvar->data_size || NULL == matvar->data )
  17770. break;
  17771. nfields = matvar->nbytes / matvar->data_size;
  17772. fields = (matvar_t**)matvar->data;
  17773. for ( i = 0; i < nfields; i++ ) {
  17774. if ( 0 < fields[i]->internal->hdf5_ref &&
  17775. -1 < fields[i]->internal->id ) {
  17776. /* Dataset of references */
  17777. Mat_H5ReadNextReferenceData(fields[i]->internal->id,fields[i],mat);
  17778. } else {
  17779. Mat_VarRead73(mat,fields[i]);
  17780. }
  17781. }
  17782. }
  17783. break;
  17784. }
  17785. default:
  17786. break;
  17787. }
  17788. return;
  17789. }
  17790. /** @if mat_devman
  17791. * @brief Writes a cell array matlab variable to the specified HDF id with the
  17792. * given name
  17793. *
  17794. * @ingroup mat_internal
  17795. * @param id HDF id of the parent object
  17796. * @param matvar pointer to the cell array variable
  17797. * @param name Name of the HDF dataset
  17798. * @retval 0 on success
  17799. * @endif
  17800. */
  17801. static int
  17802. Mat_VarWriteCell73(hid_t id,matvar_t *matvar,const char *name,hid_t *refs_id)
  17803. {
  17804. unsigned k;
  17805. hid_t str_type_id,mspace_id,dset_id,attr_type_id,attr_id,aspace_id;
  17806. hsize_t nmemb;
  17807. matvar_t **cells;
  17808. int is_ref, err = -1;
  17809. char id_name[128] = {'\0',};
  17810. hsize_t perm_dims[10];
  17811. cells = (matvar_t**)matvar->data;
  17812. nmemb = matvar->dims[0];
  17813. for ( k = 1; k < matvar->rank; k++ )
  17814. nmemb *= matvar->dims[k];
  17815. if ( 0 == nmemb || NULL == matvar->data ) {
  17816. hsize_t rank = matvar->rank;
  17817. unsigned empty = 1;
  17818. mspace_id = H5Screate_simple(1,&rank,NULL);
  17819. dset_id = H5Dcreate(id,name,H5T_NATIVE_HSIZE,mspace_id,
  17820. H5P_DEFAULT,H5P_DEFAULT,H5P_DEFAULT);
  17821. attr_type_id = H5Tcopy(H5T_C_S1);
  17822. H5Tset_size(attr_type_id,
  17823. strlen(Mat_class_names[matvar->class_type])+1);
  17824. aspace_id = H5Screate(H5S_SCALAR);
  17825. attr_id = H5Acreate(dset_id,"MATLAB_class",attr_type_id,
  17826. aspace_id,H5P_DEFAULT,H5P_DEFAULT);
  17827. H5Awrite(attr_id,attr_type_id,
  17828. Mat_class_names[matvar->class_type]);
  17829. H5Sclose(aspace_id);
  17830. H5Aclose(attr_id);
  17831. H5Tclose(attr_type_id);
  17832. /* Write the empty attribute */
  17833. aspace_id = H5Screate(H5S_SCALAR);
  17834. attr_id = H5Acreate(dset_id,"MATLAB_empty",H5T_NATIVE_UINT,
  17835. aspace_id,H5P_DEFAULT,H5P_DEFAULT);
  17836. H5Awrite(attr_id,H5T_NATIVE_UINT,&empty);
  17837. H5Sclose(aspace_id);
  17838. H5Aclose(attr_id);
  17839. /* Write the dimensions as the data */
  17840. H5Dwrite(dset_id,Mat_dims_type_to_hid_t(),H5S_ALL,H5S_ALL,
  17841. H5P_DEFAULT,matvar->dims);
  17842. H5Dclose(dset_id);
  17843. H5Sclose(mspace_id);
  17844. err = 0;
  17845. } else {
  17846. (void)H5Iget_name(id,id_name,127);
  17847. is_ref = !strcmp(id_name,"/#refs#");
  17848. if ( *refs_id < 0 ) {
  17849. if ( H5Lexists(id,"/#refs#",H5P_DEFAULT) ) {
  17850. *refs_id = H5Gopen(id,"/#refs#",H5P_DEFAULT);
  17851. } else {
  17852. *refs_id = H5Gcreate(id,"/#refs#",H5P_DEFAULT,
  17853. H5P_DEFAULT,H5P_DEFAULT);
  17854. }
  17855. }
  17856. if ( *refs_id > -1 ) {
  17857. char obj_name[64];
  17858. hobj_ref_t *refs;
  17859. for ( k = 0; k < matvar->rank; k++ )
  17860. perm_dims[k] = matvar->dims[matvar->rank-k-1];
  17861. refs = (hobj_ref_t*)malloc(nmemb*sizeof(*refs));
  17862. mspace_id=H5Screate_simple(matvar->rank,perm_dims,NULL);
  17863. dset_id = H5Dcreate(id,name,H5T_STD_REF_OBJ,mspace_id,
  17864. H5P_DEFAULT,H5P_DEFAULT,H5P_DEFAULT);
  17865. for ( k = 0; k < nmemb; k++ ) {
  17866. H5G_info_t group_info;
  17867. H5Gget_info(*refs_id, &group_info);
  17868. sprintf(obj_name,"%lld",group_info.nlinks);
  17869. if ( NULL != cells[k] )
  17870. cells[k]->compression = matvar->compression;
  17871. Mat_VarWriteNext73(*refs_id,cells[k],obj_name,refs_id);
  17872. sprintf(obj_name,"/#refs#/%lld",group_info.nlinks);
  17873. H5Rcreate(refs+k,id,obj_name,H5R_OBJECT,-1);
  17874. }
  17875. H5Dwrite(dset_id,H5T_STD_REF_OBJ,H5S_ALL,H5S_ALL,
  17876. H5P_DEFAULT,refs);
  17877. str_type_id = H5Tcopy(H5T_C_S1);
  17878. H5Tset_size(str_type_id,7);
  17879. aspace_id = H5Screate(H5S_SCALAR);
  17880. attr_id = H5Acreate(dset_id,"MATLAB_class",str_type_id,
  17881. aspace_id,H5P_DEFAULT,H5P_DEFAULT);
  17882. H5Awrite(attr_id,str_type_id,"cell");
  17883. H5Aclose(attr_id);
  17884. H5Sclose(aspace_id);
  17885. H5Tclose(str_type_id);
  17886. H5Dclose(dset_id);
  17887. free(refs);
  17888. H5Sclose(mspace_id);
  17889. err = 0;
  17890. }
  17891. }
  17892. return err;
  17893. }
  17894. /** @if mat_devman
  17895. * @brief Writes a character matlab variable to the specified HDF id with the
  17896. * given name
  17897. *
  17898. * @ingroup mat_internal
  17899. * @param id HDF id of the parent object
  17900. * @param matvar pointer to the character variable
  17901. * @param name Name of the HDF dataset
  17902. * @retval 0 on success
  17903. * @endif
  17904. */
  17905. static int
  17906. Mat_VarWriteChar73(hid_t id,matvar_t *matvar,const char *name)
  17907. {
  17908. int err = -1;
  17909. unsigned int k,numel;
  17910. hid_t mspace_id,dset_id,attr_type_id,attr_id,aspace_id;
  17911. hsize_t perm_dims[10];
  17912. numel = 1;
  17913. for ( k = 0; k < matvar->rank; k++ ) {
  17914. perm_dims[k] = matvar->dims[matvar->rank-k-1];
  17915. numel *= perm_dims[k];
  17916. }
  17917. if ( 0 == numel || NULL == matvar->data ) {
  17918. hsize_t rank = matvar->rank;
  17919. unsigned empty = 1;
  17920. mspace_id = H5Screate_simple(1,&rank,NULL);
  17921. dset_id = H5Dcreate(id,name,H5T_NATIVE_HSIZE,mspace_id,
  17922. H5P_DEFAULT,H5P_DEFAULT,H5P_DEFAULT);
  17923. attr_type_id = H5Tcopy(H5T_C_S1);
  17924. H5Tset_size(attr_type_id,
  17925. strlen(Mat_class_names[matvar->class_type])+1);
  17926. aspace_id = H5Screate(H5S_SCALAR);
  17927. attr_id = H5Acreate(dset_id,"MATLAB_class",attr_type_id,
  17928. aspace_id,H5P_DEFAULT,H5P_DEFAULT);
  17929. H5Awrite(attr_id,attr_type_id,
  17930. Mat_class_names[matvar->class_type]);
  17931. H5Sclose(aspace_id);
  17932. H5Aclose(attr_id);
  17933. H5Tclose(attr_type_id);
  17934. /* Write the empty attribute */
  17935. aspace_id = H5Screate(H5S_SCALAR);
  17936. attr_id = H5Acreate(dset_id,"MATLAB_empty",H5T_NATIVE_UINT,
  17937. aspace_id,H5P_DEFAULT,H5P_DEFAULT);
  17938. H5Awrite(attr_id,H5T_NATIVE_UINT,&empty);
  17939. H5Sclose(aspace_id);
  17940. H5Aclose(attr_id);
  17941. /* Write the dimensions as the data */
  17942. H5Dwrite(dset_id,Mat_dims_type_to_hid_t(),H5S_ALL,H5S_ALL,
  17943. H5P_DEFAULT,matvar->dims);
  17944. H5Dclose(dset_id);
  17945. H5Sclose(mspace_id);
  17946. err = 0;
  17947. } else {
  17948. int matlab_int_decode = 2;
  17949. mspace_id = H5Screate_simple(matvar->rank,perm_dims,NULL);
  17950. switch ( matvar->data_type ) {
  17951. case MAT_T_UTF32:
  17952. case MAT_T_INT32:
  17953. case MAT_T_UINT32:
  17954. /* Not sure matlab will actually handle this */
  17955. dset_id = H5Dcreate(id,name,
  17956. Mat_class_type_to_hid_t(MAT_C_UINT32),
  17957. mspace_id,H5P_DEFAULT,H5P_DEFAULT,
  17958. H5P_DEFAULT);
  17959. break;
  17960. case MAT_T_UTF16:
  17961. case MAT_T_UTF8:
  17962. case MAT_T_INT16:
  17963. case MAT_T_UINT16:
  17964. case MAT_T_INT8:
  17965. case MAT_T_UINT8:
  17966. dset_id = H5Dcreate(id,name,
  17967. Mat_class_type_to_hid_t(MAT_C_UINT16),
  17968. mspace_id,H5P_DEFAULT,H5P_DEFAULT,
  17969. H5P_DEFAULT);
  17970. break;
  17971. default:
  17972. H5Sclose(mspace_id);
  17973. return err;
  17974. }
  17975. attr_type_id = H5Tcopy(H5T_C_S1);
  17976. H5Tset_size(attr_type_id,
  17977. strlen(Mat_class_names[matvar->class_type])+1);
  17978. aspace_id = H5Screate(H5S_SCALAR);
  17979. attr_id = H5Acreate(dset_id,"MATLAB_class",attr_type_id,
  17980. aspace_id,H5P_DEFAULT,H5P_DEFAULT);
  17981. H5Awrite(attr_id,attr_type_id,Mat_class_names[matvar->class_type]);
  17982. H5Aclose(attr_id);
  17983. H5Tclose(attr_type_id);
  17984. attr_type_id = H5Tcopy(H5T_NATIVE_INT);
  17985. attr_id = H5Acreate(dset_id,"MATLAB_int_decode",attr_type_id,
  17986. aspace_id,H5P_DEFAULT,H5P_DEFAULT);
  17987. H5Awrite(attr_id,attr_type_id,&matlab_int_decode);
  17988. H5Aclose(attr_id);
  17989. H5Tclose(attr_type_id);
  17990. H5Sclose(aspace_id);
  17991. H5Dwrite(dset_id,Mat_data_type_to_hid_t(matvar->data_type),
  17992. H5S_ALL,H5S_ALL,H5P_DEFAULT,matvar->data);
  17993. H5Dclose(dset_id);
  17994. H5Sclose(mspace_id);
  17995. err = 0;
  17996. }
  17997. return err;
  17998. }
  17999. static int
  18000. Mat_WriteEmptyVariable73(hid_t id,const char *name,hsize_t rank,size_t *dims)
  18001. {
  18002. int err = -1;
  18003. unsigned empty = 1;
  18004. hid_t mspace_id,dset_id,attr_type_id,attr_id,aspace_id;
  18005. mspace_id = H5Screate_simple(1,&rank,NULL);
  18006. dset_id = H5Dcreate(id,name,H5T_NATIVE_HSIZE,mspace_id,
  18007. H5P_DEFAULT,H5P_DEFAULT,H5P_DEFAULT);
  18008. if ( dset_id > -1 ) {
  18009. attr_type_id = H5Tcopy(H5T_C_S1);
  18010. H5Tset_size(attr_type_id,7);
  18011. aspace_id = H5Screate(H5S_SCALAR);
  18012. attr_id = H5Acreate(dset_id,"MATLAB_class",attr_type_id,
  18013. aspace_id,H5P_DEFAULT,H5P_DEFAULT);
  18014. H5Awrite(attr_id,attr_type_id,"double");
  18015. H5Sclose(aspace_id);
  18016. H5Aclose(attr_id);
  18017. H5Tclose(attr_type_id);
  18018. aspace_id = H5Screate(H5S_SCALAR);
  18019. attr_id = H5Acreate(dset_id,"MATLAB_empty",H5T_NATIVE_UINT,
  18020. aspace_id,H5P_DEFAULT,H5P_DEFAULT);
  18021. H5Awrite(attr_id,H5T_NATIVE_UINT,&empty);
  18022. H5Sclose(aspace_id);
  18023. H5Aclose(attr_id);
  18024. /* Write the dimensions as the data */
  18025. H5Dwrite(dset_id,Mat_dims_type_to_hid_t(),H5S_ALL,H5S_ALL,
  18026. H5P_DEFAULT,dims);
  18027. err = 0;
  18028. H5Dclose(dset_id);
  18029. }
  18030. H5Sclose(mspace_id);
  18031. return err;
  18032. }
  18033. /** @if mat_devman
  18034. * @brief Writes a logical matlab variable to the specified HDF id with the
  18035. * given name
  18036. *
  18037. * @ingroup mat_internal
  18038. * @param id HDF id of the parent object
  18039. * @param matvar pointer to the logical variable
  18040. * @param name Name of the HDF dataset
  18041. * @retval 0 on success
  18042. * @endif
  18043. */
  18044. static int
  18045. Mat_VarWriteLogical73(hid_t id,matvar_t *matvar,const char *name)
  18046. {
  18047. int err = -1;
  18048. unsigned int k,numel;
  18049. hid_t mspace_id,dset_id,attr_type_id,attr_id,aspace_id,plist;
  18050. hsize_t perm_dims[10];
  18051. herr_t herr;
  18052. int_decode = 1;
  18053. numel = 1;
  18054. for ( k = 0; k < matvar->rank; k++ ) {
  18055. perm_dims[k] = matvar->dims[matvar->rank-k-1];
  18056. numel *= perm_dims[k];
  18057. }
  18058. if ( matvar->compression ) {
  18059. hsize_t chunk_dims[10];
  18060. Mat_H5GetChunkSize(matvar->rank, perm_dims,chunk_dims);
  18061. plist = H5Pcreate(H5P_DATASET_CREATE);
  18062. herr = H5Pset_chunk(plist, matvar->rank, chunk_dims);
  18063. herr = H5Pset_deflate(plist, 9);
  18064. } else {
  18065. plist = H5P_DEFAULT;
  18066. }
  18067. if ( 0 == numel || NULL == matvar->data ) {
  18068. hsize_t rank = matvar->rank;
  18069. unsigned empty = 1;
  18070. mspace_id = H5Screate_simple(1,&rank,NULL);
  18071. dset_id = H5Dcreate(id,name,H5T_NATIVE_HSIZE,mspace_id,
  18072. H5P_DEFAULT,H5P_DEFAULT,H5P_DEFAULT);
  18073. attr_type_id = H5Tcopy(H5T_C_S1);
  18074. H5Tset_size(attr_type_id,7);
  18075. aspace_id = H5Screate(H5S_SCALAR);
  18076. attr_id = H5Acreate(dset_id,"MATLAB_class",attr_type_id,
  18077. aspace_id,H5P_DEFAULT,H5P_DEFAULT);
  18078. H5Awrite(attr_id,attr_type_id,"logical");
  18079. H5Sclose(aspace_id);
  18080. H5Aclose(attr_id);
  18081. H5Tclose(attr_type_id);
  18082. /* Write the MATLAB_int_decode attribute */
  18083. aspace_id = H5Screate(H5S_SCALAR);
  18084. attr_id = H5Acreate(dset_id,"MATLAB_int_decode",H5T_NATIVE_INT,
  18085. aspace_id,H5P_DEFAULT,H5P_DEFAULT);
  18086. H5Awrite(attr_id,H5T_NATIVE_INT,&int_decode);
  18087. H5Sclose(aspace_id);
  18088. H5Aclose(attr_id);
  18089. /* Write the empty attribute */
  18090. aspace_id = H5Screate(H5S_SCALAR);
  18091. attr_id = H5Acreate(dset_id,"MATLAB_empty",H5T_NATIVE_UINT,
  18092. aspace_id,H5P_DEFAULT,H5P_DEFAULT);
  18093. H5Awrite(attr_id,H5T_NATIVE_UINT,&empty);
  18094. H5Sclose(aspace_id);
  18095. H5Aclose(attr_id);
  18096. /* Write the dimensions as the data */
  18097. H5Dwrite(dset_id,Mat_dims_type_to_hid_t(),H5S_ALL,H5S_ALL,
  18098. H5P_DEFAULT,matvar->dims);
  18099. H5Dclose(dset_id);
  18100. H5Sclose(mspace_id);
  18101. err = 0;
  18102. } else {
  18103. mspace_id = H5Screate_simple(matvar->rank,perm_dims,NULL);
  18104. /* Note that MATLAB only recognizes uint8 as logical */
  18105. dset_id = H5Dcreate(id,name,
  18106. Mat_class_type_to_hid_t(MAT_C_UINT8),
  18107. mspace_id,H5P_DEFAULT,plist,H5P_DEFAULT);
  18108. attr_type_id = H5Tcopy(H5T_C_S1);
  18109. H5Tset_size(attr_type_id,7);
  18110. aspace_id = H5Screate(H5S_SCALAR);
  18111. attr_id = H5Acreate(dset_id,"MATLAB_class",attr_type_id,
  18112. aspace_id,H5P_DEFAULT,H5P_DEFAULT);
  18113. H5Awrite(attr_id,attr_type_id,"logical");
  18114. H5Sclose(aspace_id);
  18115. H5Aclose(attr_id);
  18116. H5Tclose(attr_type_id);
  18117. /* Write the MATLAB_int_decode attribute */
  18118. aspace_id = H5Screate(H5S_SCALAR);
  18119. attr_id = H5Acreate(dset_id,"MATLAB_int_decode",H5T_NATIVE_INT,
  18120. aspace_id,H5P_DEFAULT,H5P_DEFAULT);
  18121. H5Awrite(attr_id,H5T_NATIVE_INT,&int_decode);
  18122. H5Sclose(aspace_id);
  18123. H5Aclose(attr_id);
  18124. H5Dwrite(dset_id,Mat_data_type_to_hid_t(matvar->data_type),
  18125. H5S_ALL,H5S_ALL,H5P_DEFAULT,matvar->data);
  18126. H5Dclose(dset_id);
  18127. H5Sclose(mspace_id);
  18128. err = 0;
  18129. }
  18130. if ( H5P_DEFAULT != plist )
  18131. H5Pclose(plist);
  18132. return err;
  18133. }
  18134. /** @if mat_devman
  18135. * @brief Writes a numeric matlab variable to the specified HDF id with the
  18136. * given name
  18137. *
  18138. * @ingroup mat_internal
  18139. * @param id HDF id of the parent object
  18140. * @param matvar pointer to the numeric variable
  18141. * @param name Name of the HDF dataset
  18142. * @retval 0 on success
  18143. * @endif
  18144. */
  18145. static int
  18146. Mat_VarWriteNumeric73(hid_t id,matvar_t *matvar,const char *name)
  18147. {
  18148. int err = -1;
  18149. unsigned int k,numel;
  18150. hid_t mspace_id,dset_id,attr_type_id,attr_id,aspace_id,plist;
  18151. hsize_t perm_dims[10];
  18152. herr_t herr;
  18153. numel = 1;
  18154. for ( k = 0; k < matvar->rank; k++ ) {
  18155. perm_dims[k] = matvar->dims[matvar->rank-k-1];
  18156. numel *= perm_dims[k];
  18157. }
  18158. if ( matvar->compression ) {
  18159. hsize_t chunk_dims[10];
  18160. Mat_H5GetChunkSize(matvar->rank, perm_dims,chunk_dims);
  18161. plist = H5Pcreate(H5P_DATASET_CREATE);
  18162. herr = H5Pset_chunk(plist, matvar->rank, chunk_dims);
  18163. herr = H5Pset_deflate(plist, 9);
  18164. } else {
  18165. plist = H5P_DEFAULT;
  18166. }
  18167. if ( 0 == numel || NULL == matvar->data ) {
  18168. hsize_t rank = matvar->rank;
  18169. unsigned empty = 1;
  18170. mspace_id = H5Screate_simple(1,&rank,NULL);
  18171. dset_id = H5Dcreate(id,name,H5T_NATIVE_HSIZE,mspace_id,
  18172. H5P_DEFAULT,H5P_DEFAULT,H5P_DEFAULT);
  18173. attr_type_id = H5Tcopy(H5T_C_S1);
  18174. H5Tset_size(attr_type_id,
  18175. strlen(Mat_class_names[matvar->class_type])+1);
  18176. aspace_id = H5Screate(H5S_SCALAR);
  18177. attr_id = H5Acreate(dset_id,"MATLAB_class",attr_type_id,
  18178. aspace_id,H5P_DEFAULT,H5P_DEFAULT);
  18179. H5Awrite(attr_id,attr_type_id,
  18180. Mat_class_names[matvar->class_type]);
  18181. H5Sclose(aspace_id);
  18182. H5Aclose(attr_id);
  18183. H5Tclose(attr_type_id);
  18184. /* Write the empty attribute */
  18185. aspace_id = H5Screate(H5S_SCALAR);
  18186. attr_id = H5Acreate(dset_id,"MATLAB_empty",H5T_NATIVE_UINT,
  18187. aspace_id,H5P_DEFAULT,H5P_DEFAULT);
  18188. H5Awrite(attr_id,H5T_NATIVE_UINT,&empty);
  18189. H5Sclose(aspace_id);
  18190. H5Aclose(attr_id);
  18191. /* Write the dimensions as the data */
  18192. H5Dwrite(dset_id,Mat_dims_type_to_hid_t(),H5S_ALL,H5S_ALL,
  18193. H5P_DEFAULT,matvar->dims);
  18194. H5Dclose(dset_id);
  18195. H5Sclose(mspace_id);
  18196. err = 0;
  18197. } else if ( matvar->isComplex ) {
  18198. hid_t h5_complex,h5_complex_base;
  18199. h5_complex_base = Mat_class_type_to_hid_t(matvar->class_type);
  18200. h5_complex = H5Tcreate(H5T_COMPOUND,
  18201. 2*H5Tget_size(h5_complex_base));
  18202. H5Tinsert(h5_complex,"real",0,h5_complex_base);
  18203. H5Tinsert(h5_complex,"imag",H5Tget_size(h5_complex_base),
  18204. h5_complex_base);
  18205. mspace_id = H5Screate_simple(matvar->rank,perm_dims,NULL);
  18206. dset_id = H5Dcreate(id,name,h5_complex,mspace_id,H5P_DEFAULT,
  18207. plist,H5P_DEFAULT);
  18208. attr_type_id = H5Tcopy(H5T_C_S1);
  18209. H5Tset_size(attr_type_id,
  18210. strlen(Mat_class_names[matvar->class_type])+1);
  18211. aspace_id = H5Screate(H5S_SCALAR);
  18212. attr_id = H5Acreate(dset_id,"MATLAB_class",attr_type_id,
  18213. aspace_id,H5P_DEFAULT,H5P_DEFAULT);
  18214. H5Awrite(attr_id,attr_type_id,
  18215. Mat_class_names[matvar->class_type]);
  18216. H5Sclose(aspace_id);
  18217. H5Aclose(attr_id);
  18218. H5Tclose(attr_type_id);
  18219. H5Tclose(h5_complex);
  18220. /* Write real part of dataset */
  18221. h5_complex = H5Tcreate(H5T_COMPOUND,
  18222. H5Tget_size(h5_complex_base));
  18223. H5Tinsert(h5_complex,"real",0,h5_complex_base);
  18224. H5Dwrite(dset_id,h5_complex,H5S_ALL,H5S_ALL,H5P_DEFAULT,
  18225. ((mat_complex_split_t*)matvar->data)->Re);
  18226. H5Tclose(h5_complex);
  18227. /* Write imaginary part of dataset */
  18228. h5_complex = H5Tcreate(H5T_COMPOUND,
  18229. H5Tget_size(h5_complex_base));
  18230. H5Tinsert(h5_complex,"imag",0,h5_complex_base);
  18231. H5Dwrite(dset_id,h5_complex,H5S_ALL,H5S_ALL,H5P_DEFAULT,
  18232. ((mat_complex_split_t*)matvar->data)->Im);
  18233. H5Tclose(h5_complex);
  18234. H5Dclose(dset_id);
  18235. H5Sclose(mspace_id);
  18236. err = 0;
  18237. } else { /* matvar->isComplex */
  18238. mspace_id = H5Screate_simple(matvar->rank,perm_dims,NULL);
  18239. dset_id = H5Dcreate(id,name,
  18240. Mat_class_type_to_hid_t(matvar->class_type),
  18241. mspace_id,H5P_DEFAULT,plist,H5P_DEFAULT);
  18242. attr_type_id = H5Tcopy(H5T_C_S1);
  18243. H5Tset_size(attr_type_id,
  18244. strlen(Mat_class_names[matvar->class_type])+1);
  18245. aspace_id = H5Screate(H5S_SCALAR);
  18246. attr_id = H5Acreate(dset_id,"MATLAB_class",attr_type_id,
  18247. aspace_id,H5P_DEFAULT,H5P_DEFAULT);
  18248. H5Awrite(attr_id,attr_type_id,
  18249. Mat_class_names[matvar->class_type]);
  18250. H5Sclose(aspace_id);
  18251. H5Aclose(attr_id);
  18252. H5Tclose(attr_type_id);
  18253. H5Dwrite(dset_id,Mat_data_type_to_hid_t(matvar->data_type),
  18254. H5S_ALL,H5S_ALL,H5P_DEFAULT,matvar->data);
  18255. H5Dclose(dset_id);
  18256. H5Sclose(mspace_id);
  18257. err = 0;
  18258. }
  18259. if ( H5P_DEFAULT != plist )
  18260. H5Pclose(plist);
  18261. return err;
  18262. }
  18263. /** @if mat_devman
  18264. * @brief Writes a sparse matrix variable to the specified HDF id with the
  18265. * given name
  18266. *
  18267. * @ingroup mat_internal
  18268. * @param id HDF id of the parent object
  18269. * @param matvar pointer to the structure variable
  18270. * @param name Name of the HDF dataset
  18271. * @retval 0 on success
  18272. * @endif
  18273. */
  18274. static int
  18275. Mat_VarWriteSparse73(hid_t id,matvar_t *matvar,const char *name)
  18276. {
  18277. int err = -1;
  18278. unsigned k;
  18279. hid_t sparse_id,mspace_id,dset_id,attr_type_id,attr_id,aspace_id;
  18280. hsize_t nmemb;
  18281. hsize_t perm_dims[10];
  18282. nmemb = matvar->dims[0];
  18283. for ( k = 1; k < matvar->rank; k++ )
  18284. nmemb *= matvar->dims[k];
  18285. sparse_id = H5Gcreate(id,name,H5P_DEFAULT,H5P_DEFAULT,H5P_DEFAULT);
  18286. if ( sparse_id < 0 ) {
  18287. Mat_Critical("Error creating group for sparse array %s",
  18288. matvar->name);
  18289. } else {
  18290. hid_t size_type_id,data_type_id;
  18291. mat_sparse_t *sparse;
  18292. hsize_t rank, nir, njc, ndata;
  18293. mat_uint64_t sparse_attr_value;
  18294. enum matio_classes class_type;
  18295. sparse = (mat_sparse_t*)matvar->data;
  18296. rank = matvar->rank;
  18297. class_type = Mat_TypeToClass73(matvar->data_type);
  18298. attr_type_id = H5Tcopy(H5T_C_S1);
  18299. H5Tset_size(attr_type_id,
  18300. strlen(Mat_class_names[class_type])+1);
  18301. aspace_id = H5Screate(H5S_SCALAR);
  18302. attr_id = H5Acreate(sparse_id,"MATLAB_class",attr_type_id,
  18303. aspace_id,H5P_DEFAULT,H5P_DEFAULT);
  18304. H5Awrite(attr_id,attr_type_id,
  18305. Mat_class_names[class_type]);
  18306. H5Sclose(aspace_id);
  18307. H5Aclose(attr_id);
  18308. H5Tclose(attr_type_id);
  18309. sparse_attr_value = matvar->dims[0];
  18310. size_type_id = Mat_class_type_to_hid_t(MAT_C_UINT64);
  18311. aspace_id = H5Screate(H5S_SCALAR);
  18312. attr_id = H5Acreate(sparse_id,"MATLAB_sparse",size_type_id,
  18313. aspace_id,H5P_DEFAULT,H5P_DEFAULT);
  18314. H5Awrite(attr_id,size_type_id,&sparse_attr_value);
  18315. H5Sclose(aspace_id);
  18316. H5Aclose(attr_id);
  18317. ndata = sparse->ndata;
  18318. data_type_id = Mat_data_type_to_hid_t(matvar->data_type);
  18319. if ( matvar->isComplex ) {
  18320. hid_t h5_complex;
  18321. mat_complex_split_t *complex_data;
  18322. complex_data = (mat_complex_split_t*)sparse->data;
  18323. /* Create dataset datatype as compound with real and
  18324. * imaginary fields
  18325. */
  18326. h5_complex = H5Tcreate(H5T_COMPOUND,
  18327. 2*H5Tget_size(data_type_id));
  18328. H5Tinsert(h5_complex,"real",0,data_type_id);
  18329. H5Tinsert(h5_complex,"imag",H5Tget_size(data_type_id),
  18330. data_type_id);
  18331. /* Create dataset */
  18332. perm_dims[0] = ndata;
  18333. mspace_id = H5Screate_simple(1,perm_dims,NULL);
  18334. dset_id = H5Dcreate(sparse_id,"data",h5_complex,mspace_id,
  18335. H5P_DEFAULT,H5P_DEFAULT,H5P_DEFAULT);
  18336. H5Tclose(h5_complex);
  18337. /* Write real part of dataset */
  18338. h5_complex = H5Tcreate(H5T_COMPOUND,
  18339. H5Tget_size(data_type_id));
  18340. H5Tinsert(h5_complex,"real",0,data_type_id);
  18341. H5Dwrite(dset_id,h5_complex,H5S_ALL,H5S_ALL,H5P_DEFAULT,
  18342. complex_data->Re);
  18343. H5Tclose(h5_complex);
  18344. /* Write imaginary part of dataset */
  18345. h5_complex = H5Tcreate(H5T_COMPOUND,H5Tget_size(data_type_id));
  18346. H5Tinsert(h5_complex,"imag",0,data_type_id);
  18347. H5Dwrite(dset_id,h5_complex,H5S_ALL,H5S_ALL,H5P_DEFAULT,
  18348. complex_data->Im);
  18349. H5Tclose(h5_complex);
  18350. H5Dclose(dset_id);
  18351. H5Sclose(mspace_id);
  18352. } else { /* if ( matvar->isComplex ) */
  18353. mspace_id = H5Screate_simple(1,&ndata,NULL);
  18354. dset_id = H5Dcreate(sparse_id,"data",data_type_id,mspace_id,
  18355. H5P_DEFAULT,H5P_DEFAULT,H5P_DEFAULT);
  18356. H5Dwrite(dset_id,data_type_id,H5S_ALL,H5S_ALL,H5P_DEFAULT,
  18357. sparse->data);
  18358. H5Dclose(dset_id);
  18359. H5Sclose(mspace_id);
  18360. }
  18361. nir = sparse->nir;
  18362. mspace_id = H5Screate_simple(1,&nir,NULL);
  18363. dset_id = H5Dcreate(sparse_id,"ir",size_type_id,mspace_id,
  18364. H5P_DEFAULT,H5P_DEFAULT,H5P_DEFAULT);
  18365. H5Dwrite(dset_id,H5T_NATIVE_INT,H5S_ALL,H5S_ALL,H5P_DEFAULT,
  18366. sparse->ir);
  18367. H5Dclose(dset_id);
  18368. H5Sclose(mspace_id);
  18369. njc = sparse->njc;
  18370. mspace_id = H5Screate_simple(1,&njc,NULL);
  18371. dset_id = H5Dcreate(sparse_id,"jc",size_type_id,mspace_id,
  18372. H5P_DEFAULT,H5P_DEFAULT,H5P_DEFAULT);
  18373. H5Dwrite(dset_id,H5T_NATIVE_INT,H5S_ALL,H5S_ALL,H5P_DEFAULT,
  18374. sparse->jc);
  18375. H5Dclose(dset_id);
  18376. H5Sclose(mspace_id);
  18377. H5Gclose(sparse_id);
  18378. err = 0;
  18379. }
  18380. return err;
  18381. }
  18382. /** @if mat_devman
  18383. * @brief Writes a structure matlab variable to the specified HDF id with the
  18384. * given name
  18385. *
  18386. * @ingroup mat_internal
  18387. * @param id HDF id of the parent object
  18388. * @param matvar pointer to the structure variable
  18389. * @param name Name of the HDF dataset
  18390. * @retval 0 on success
  18391. * @endif
  18392. */
  18393. static int
  18394. Mat_VarWriteStruct73(hid_t id,matvar_t *matvar,const char *name,hid_t *refs_id)
  18395. {
  18396. int err = -1;
  18397. unsigned k;
  18398. hid_t mspace_id,dset_id,attr_type_id,attr_id,aspace_id;
  18399. hid_t struct_id,str_type_id,fieldnames_id;
  18400. hsize_t nfields,nmemb;
  18401. matvar_t **fields;
  18402. hvl_t *fieldnames;
  18403. char id_name[128] = {'\0',};
  18404. int is_ref;
  18405. hsize_t perm_dims[10];
  18406. nmemb = 1;
  18407. for ( k = 0; k < matvar->rank; k++ )
  18408. nmemb *= matvar->dims[k];
  18409. if ( 0 == nmemb || NULL == matvar->data ) {
  18410. hsize_t rank = matvar->rank;
  18411. unsigned empty = 1;
  18412. mspace_id = H5Screate_simple(1,&rank,NULL);
  18413. dset_id = H5Dcreate(id,name,H5T_NATIVE_HSIZE,mspace_id,
  18414. H5P_DEFAULT,H5P_DEFAULT,H5P_DEFAULT);
  18415. attr_type_id = H5Tcopy(H5T_C_S1);
  18416. H5Tset_size(attr_type_id,
  18417. strlen(Mat_class_names[matvar->class_type])+1);
  18418. aspace_id = H5Screate(H5S_SCALAR);
  18419. attr_id = H5Acreate(dset_id,"MATLAB_class",attr_type_id,
  18420. aspace_id,H5P_DEFAULT,H5P_DEFAULT);
  18421. H5Awrite(attr_id,attr_type_id,
  18422. Mat_class_names[matvar->class_type]);
  18423. H5Sclose(aspace_id);
  18424. H5Aclose(attr_id);
  18425. H5Tclose(attr_type_id);
  18426. /* Write the empty attribute */
  18427. aspace_id = H5Screate(H5S_SCALAR);
  18428. attr_id = H5Acreate(dset_id,"MATLAB_empty",H5T_NATIVE_UINT,
  18429. aspace_id,H5P_DEFAULT,H5P_DEFAULT);
  18430. H5Awrite(attr_id,H5T_NATIVE_UINT,&empty);
  18431. H5Sclose(aspace_id);
  18432. H5Aclose(attr_id);
  18433. nfields = matvar->internal->num_fields;
  18434. if ( nfields ) {
  18435. str_type_id = H5Tcopy(H5T_C_S1);
  18436. fieldnames = (hvl_t*)malloc(nfields*sizeof(*fieldnames));
  18437. fields = (matvar_t**)matvar->data;
  18438. for ( k = 0; k < nfields; k++ ) {
  18439. fieldnames[k].len =
  18440. strlen(matvar->internal->fieldnames[k]);
  18441. fieldnames[k].p = matvar->internal->fieldnames[k];
  18442. }
  18443. H5Tset_size(str_type_id,1);
  18444. fieldnames_id = H5Tvlen_create(str_type_id);
  18445. aspace_id = H5Screate_simple(1,&nfields,NULL);
  18446. attr_id = H5Acreate(dset_id,"MATLAB_fields",fieldnames_id,
  18447. aspace_id,H5P_DEFAULT,H5P_DEFAULT);
  18448. H5Awrite(attr_id,fieldnames_id,fieldnames);
  18449. H5Aclose(attr_id);
  18450. H5Sclose(aspace_id);
  18451. H5Tclose(fieldnames_id);
  18452. H5Tclose(str_type_id);
  18453. free(fieldnames);
  18454. }
  18455. /* Write the dimensions as the data */
  18456. H5Dwrite(dset_id,Mat_dims_type_to_hid_t(),H5S_ALL,H5S_ALL,
  18457. H5P_DEFAULT,matvar->dims);
  18458. H5Dclose(dset_id);
  18459. H5Sclose(mspace_id);
  18460. } else {
  18461. (void)H5Iget_name(id,id_name,127);
  18462. is_ref = !strcmp(id_name,"/#refs#");
  18463. struct_id = H5Gcreate(id,name,H5P_DEFAULT,H5P_DEFAULT,
  18464. H5P_DEFAULT);
  18465. if ( struct_id < 0 ) {
  18466. Mat_Critical("Error creating group for struct %s",name);
  18467. } else {
  18468. str_type_id = H5Tcopy(H5T_C_S1);
  18469. H5Tset_size(str_type_id,7);
  18470. aspace_id = H5Screate(H5S_SCALAR);
  18471. attr_id = H5Acreate(struct_id,"MATLAB_class",str_type_id,
  18472. aspace_id,H5P_DEFAULT,H5P_DEFAULT);
  18473. H5Awrite(attr_id,str_type_id,"struct");
  18474. H5Aclose(attr_id);
  18475. H5Sclose(aspace_id);
  18476. nfields = matvar->internal->num_fields;
  18477. /* Structure with no fields */
  18478. if ( nfields == 0 ) {
  18479. H5Gclose(struct_id);
  18480. H5Tclose(str_type_id);
  18481. return 0;
  18482. }
  18483. fieldnames = (hvl_t*)malloc(nfields*sizeof(*fieldnames));
  18484. fields = (matvar_t**)matvar->data;
  18485. for ( k = 0; k < nfields; k++ ) {
  18486. fieldnames[k].len =
  18487. strlen(matvar->internal->fieldnames[k]);
  18488. fieldnames[k].p = matvar->internal->fieldnames[k];
  18489. }
  18490. H5Tset_size(str_type_id,1);
  18491. fieldnames_id = H5Tvlen_create(str_type_id);
  18492. aspace_id = H5Screate_simple(1,&nfields,NULL);
  18493. attr_id = H5Acreate(struct_id,"MATLAB_fields",fieldnames_id,
  18494. aspace_id,H5P_DEFAULT,H5P_DEFAULT);
  18495. H5Awrite(attr_id,fieldnames_id,fieldnames);
  18496. H5Aclose(attr_id);
  18497. H5Sclose(aspace_id);
  18498. H5Tclose(fieldnames_id);
  18499. H5Tclose(str_type_id);
  18500. free(fieldnames);
  18501. if ( 1 == nmemb ) {
  18502. for ( k = 0; k < nfields; k++ ) {
  18503. if ( NULL != fields[k] )
  18504. fields[k]->compression = matvar->compression;
  18505. Mat_VarWriteNext73(struct_id,fields[k],
  18506. matvar->internal->fieldnames[k],refs_id);
  18507. }
  18508. } else {
  18509. if ( *refs_id < 0 ) {
  18510. if ( H5Lexists(id,"/#refs#",H5P_DEFAULT) ) {
  18511. *refs_id = H5Gopen(id,"/#refs#",H5P_DEFAULT);
  18512. } else {
  18513. *refs_id = H5Gcreate(id,"/#refs#",H5P_DEFAULT,
  18514. H5P_DEFAULT,H5P_DEFAULT);
  18515. }
  18516. }
  18517. if ( *refs_id > -1 ) {
  18518. char name[64];
  18519. hobj_ref_t **refs;
  18520. int l;
  18521. refs = (hobj_ref_t**)malloc(nfields*sizeof(*refs));
  18522. for ( l = 0; l < nfields; l++ )
  18523. refs[l] = (hobj_ref_t*)malloc(nmemb*sizeof(*refs[l]));
  18524. for ( k = 0; k < nmemb; k++ ) {
  18525. for ( l = 0; l < nfields; l++ ) {
  18526. H5G_info_t group_info;
  18527. H5Gget_info(*refs_id, &group_info);
  18528. sprintf(name,"%lld",group_info.nlinks);
  18529. if ( NULL != fields[k*nfields+l] )
  18530. fields[k*nfields+l]->compression =
  18531. matvar->compression;
  18532. Mat_VarWriteNext73(*refs_id,fields[k*nfields+l],
  18533. name,refs_id);
  18534. sprintf(name,"/#refs#/%lld",group_info.nlinks);
  18535. H5Rcreate(refs[l]+k,id,name,H5R_OBJECT,-1);
  18536. }
  18537. }
  18538. for ( k = 0; k < matvar->rank; k++ )
  18539. perm_dims[k] = matvar->dims[matvar->rank-k-1];
  18540. mspace_id=H5Screate_simple(matvar->rank,perm_dims,NULL);
  18541. for ( l = 0; l < nfields; l++ ) {
  18542. dset_id = H5Dcreate(struct_id,
  18543. matvar->internal->fieldnames[l],
  18544. H5T_STD_REF_OBJ,mspace_id,
  18545. H5P_DEFAULT,H5P_DEFAULT,H5P_DEFAULT);
  18546. H5Dwrite(dset_id,H5T_STD_REF_OBJ,H5S_ALL,
  18547. H5S_ALL,H5P_DEFAULT,refs[l]);
  18548. H5Dclose(dset_id);
  18549. free(refs[l]);
  18550. }
  18551. free(refs);
  18552. H5Sclose(mspace_id);
  18553. }
  18554. }
  18555. }
  18556. H5Gclose(struct_id);
  18557. }
  18558. return err;
  18559. }
  18560. static int
  18561. Mat_VarWriteNext73(hid_t id,matvar_t *matvar,const char *name,hid_t *refs_id)
  18562. {
  18563. int err = -1;
  18564. if ( NULL == matvar ) {
  18565. size_t dims[2] = {0,0};
  18566. return Mat_WriteEmptyVariable73(id,name,2,dims);
  18567. } else if ( matvar->isLogical ) {
  18568. return Mat_VarWriteLogical73(id,matvar,name);
  18569. }
  18570. switch ( matvar->class_type ) {
  18571. case MAT_C_DOUBLE:
  18572. case MAT_C_SINGLE:
  18573. case MAT_C_INT64:
  18574. case MAT_C_UINT64:
  18575. case MAT_C_INT32:
  18576. case MAT_C_UINT32:
  18577. case MAT_C_INT16:
  18578. case MAT_C_UINT16:
  18579. case MAT_C_INT8:
  18580. case MAT_C_UINT8:
  18581. err = Mat_VarWriteNumeric73(id,matvar,name);
  18582. break;
  18583. case MAT_C_CHAR:
  18584. err = Mat_VarWriteChar73(id,matvar,name);
  18585. break;
  18586. case MAT_C_STRUCT:
  18587. err = Mat_VarWriteStruct73(id,matvar,name,refs_id);
  18588. break;
  18589. case MAT_C_CELL:
  18590. err = Mat_VarWriteCell73(id,matvar,name,refs_id);
  18591. break;
  18592. case MAT_C_SPARSE:
  18593. err = Mat_VarWriteSparse73(id,matvar,name);
  18594. break;
  18595. case MAT_C_EMPTY:
  18596. case MAT_C_FUNCTION:
  18597. case MAT_C_OBJECT:
  18598. break;
  18599. }
  18600. return err;
  18601. }
  18602. /** @if mat_devman
  18603. * @brief Creates a new Matlab MAT version 7.3 file
  18604. *
  18605. * Tries to create a new Matlab MAT file with the given name and optional
  18606. * header string. If no header string is given, the default string
  18607. * is used containing the software, version, and date in it. If a header
  18608. * string is given, at most the first 116 characters is written to the file.
  18609. * The given header string need not be the full 116 characters, but MUST be
  18610. * NULL terminated.
  18611. * @ingroup mat_internal
  18612. * @param matname Name of MAT file to create
  18613. * @param hdr_str Optional header string, NULL to use default
  18614. * @return A pointer to the MAT file or NULL if it failed. This is not a
  18615. * simple FILE * and should not be used as one.
  18616. * @endif
  18617. */
  18618. mat_t *
  18619. Mat_Create73(const char *matname,const char *hdr_str)
  18620. {
  18621. FILE *fp = NULL;
  18622. mat_int16_t endian = 0, version;
  18623. mat_t *mat = NULL;
  18624. size_t err;
  18625. time_t t;
  18626. hid_t plist_id,fid;
  18627. plist_id = H5Pcreate(H5P_FILE_CREATE);
  18628. H5Pset_userblock(plist_id,512);
  18629. fid = H5Fcreate(matname,H5F_ACC_TRUNC,plist_id,H5P_DEFAULT);
  18630. H5Fclose(fid);
  18631. H5Pclose(plist_id);
  18632. fp = fopen(matname,"r+b");
  18633. if ( !fp )
  18634. return NULL;
  18635. (void)fseek(fp,0,SEEK_SET);
  18636. mat = (mat_t*)malloc(sizeof(*mat));
  18637. if ( mat == NULL ) {
  18638. fclose(fp);
  18639. return NULL;
  18640. }
  18641. mat->fp = NULL;
  18642. mat->header = NULL;
  18643. mat->subsys_offset = NULL;
  18644. mat->filename = NULL;
  18645. mat->version = 0;
  18646. mat->byteswap = 0;
  18647. mat->mode = 0;
  18648. mat->bof = 0;
  18649. mat->next_index = 0;
  18650. mat->refs_id = -1;
  18651. t = time(NULL);
  18652. mat->filename = strdup_printf("%s",matname);
  18653. mat->mode = MAT_ACC_RDWR;
  18654. mat->byteswap = 0;
  18655. mat->header = (char*)malloc(128*sizeof(char));
  18656. mat->subsys_offset = (char*)malloc(8*sizeof(char));
  18657. memset(mat->header,' ',128);
  18658. if ( hdr_str == NULL ) {
  18659. err = mat_snprintf(mat->header,116,"MATLAB 7.3 MAT-file, Platform: %s, "
  18660. "Created by: libmatio v%d.%d.%d on %s HDF5 schema 0.5",
  18661. MATIO_PLATFORM, MATIO_MAJOR_VERSION, MATIO_MINOR_VERSION,
  18662. MATIO_RELEASE_LEVEL, ctime(&t));
  18663. } else {
  18664. err = mat_snprintf(mat->header,116,"%s",hdr_str);
  18665. }
  18666. if ( err >= 116 )
  18667. mat->header[115] = '\0'; /* Just to make sure it's NULL terminated */
  18668. memset(mat->subsys_offset,' ',8);
  18669. mat->version = (int)0x0200;
  18670. endian = 0x4d49;
  18671. version = 0x0200;
  18672. err = fwrite(mat->header,1,116,fp);
  18673. err = fwrite(mat->subsys_offset,1,8,fp);
  18674. err = fwrite(&version,2,1,fp);
  18675. err = fwrite(&endian,2,1,fp);
  18676. fclose(fp);
  18677. fid = H5Fopen(matname,H5F_ACC_RDWR,H5P_DEFAULT);
  18678. mat->fp = malloc(sizeof(hid_t));
  18679. *(hid_t*)mat->fp = fid;
  18680. return mat;
  18681. }
  18682. /** @if mat_devman
  18683. * @brief Reads the MAT variable identified by matvar
  18684. *
  18685. * @ingroup mat_internal
  18686. * @param mat MAT file pointer
  18687. * @param matvar MAT variable pointer
  18688. * @endif
  18689. */
  18690. void
  18691. Mat_VarRead73(mat_t *mat,matvar_t *matvar)
  18692. {
  18693. int k;
  18694. size_t numel;
  18695. hid_t fid,dset_id,ref_id;
  18696. if ( NULL == mat || NULL == matvar )
  18697. return;
  18698. else if (NULL == matvar->internal->hdf5_name && 0 > matvar->internal->id)
  18699. return;
  18700. fid = *(hid_t*)mat->fp;
  18701. switch (matvar->class_type) {
  18702. case MAT_C_DOUBLE:
  18703. case MAT_C_SINGLE:
  18704. case MAT_C_INT64:
  18705. case MAT_C_UINT64:
  18706. case MAT_C_INT32:
  18707. case MAT_C_UINT32:
  18708. case MAT_C_INT16:
  18709. case MAT_C_UINT16:
  18710. case MAT_C_INT8:
  18711. case MAT_C_UINT8:
  18712. numel = 1;
  18713. for ( k = 0; k < matvar->rank; k++ )
  18714. numel *= matvar->dims[k];
  18715. matvar->data_size = Mat_SizeOfClass(matvar->class_type);
  18716. matvar->nbytes = numel*matvar->data_size;
  18717. if ( numel < 1 )
  18718. break;
  18719. if ( NULL != matvar->internal->hdf5_name ) {
  18720. ref_id = H5Dopen(fid,matvar->internal->hdf5_name,H5P_DEFAULT);
  18721. } else {
  18722. ref_id = matvar->internal->id;
  18723. H5Iinc_ref(ref_id);
  18724. }
  18725. if ( 0 < matvar->internal->hdf5_ref ) {
  18726. dset_id = H5Rdereference(ref_id,H5R_OBJECT,&matvar->internal->hdf5_ref);
  18727. } else {
  18728. dset_id = ref_id;
  18729. H5Iinc_ref(dset_id);
  18730. }
  18731. if ( !matvar->isComplex ) {
  18732. matvar->data = malloc(matvar->nbytes);
  18733. if ( NULL != matvar->data ) {
  18734. H5Dread(dset_id,Mat_class_type_to_hid_t(matvar->class_type),
  18735. H5S_ALL,H5S_ALL,H5P_DEFAULT,matvar->data);
  18736. }
  18737. } else {
  18738. mat_complex_split_t *complex_data;
  18739. hid_t h5_complex_base,h5_complex;
  18740. complex_data = (mat_complex_split_t*)malloc(sizeof(*complex_data));
  18741. complex_data->Re = malloc(matvar->nbytes);
  18742. complex_data->Im = malloc(matvar->nbytes);
  18743. h5_complex_base = Mat_class_type_to_hid_t(matvar->class_type);
  18744. h5_complex = H5Tcreate(H5T_COMPOUND,
  18745. H5Tget_size(h5_complex_base));
  18746. H5Tinsert(h5_complex,"real",0,h5_complex_base);
  18747. H5Dread(dset_id,h5_complex,H5S_ALL,H5S_ALL,H5P_DEFAULT,
  18748. complex_data->Re);
  18749. H5Tclose(h5_complex);
  18750. h5_complex = H5Tcreate(H5T_COMPOUND,
  18751. H5Tget_size(h5_complex_base));
  18752. H5Tinsert(h5_complex,"imag",0,h5_complex_base);
  18753. H5Dread(dset_id,h5_complex,H5S_ALL,H5S_ALL,H5P_DEFAULT,
  18754. complex_data->Im);
  18755. H5Tclose(h5_complex);
  18756. matvar->data = complex_data;
  18757. }
  18758. H5Dclose(dset_id);
  18759. H5Dclose(ref_id);
  18760. break;
  18761. case MAT_C_CHAR:
  18762. numel = 1;
  18763. for ( k = 0; k < matvar->rank; k++ )
  18764. numel *= matvar->dims[k];
  18765. matvar->data_type = MAT_T_UINT8;
  18766. matvar->data_size = 1;
  18767. matvar->nbytes = numel*matvar->data_size;
  18768. if ( NULL != matvar->internal->hdf5_name ) {
  18769. dset_id = H5Dopen(fid,matvar->internal->hdf5_name,H5P_DEFAULT);
  18770. } else {
  18771. dset_id = matvar->internal->id;
  18772. H5Iinc_ref(dset_id);
  18773. }
  18774. matvar->data = malloc(matvar->nbytes);
  18775. if ( NULL != matvar->data ) {
  18776. H5Dread(dset_id,Mat_data_type_to_hid_t(matvar->data_type),
  18777. H5S_ALL,H5S_ALL,H5P_DEFAULT,matvar->data);
  18778. }
  18779. break;
  18780. case MAT_C_STRUCT:
  18781. {
  18782. matvar_t **fields;
  18783. int i,nfields = 0;
  18784. if ( !matvar->internal->num_fields || NULL == matvar->data )
  18785. break;
  18786. numel = 1;
  18787. for ( k = 0; k < matvar->rank; k++ )
  18788. numel *= matvar->dims[k];
  18789. nfields = matvar->internal->num_fields;
  18790. fields = (matvar_t**)matvar->data;
  18791. for ( i = 0; i < nfields*numel; i++ ) {
  18792. if ( 0 < fields[i]->internal->hdf5_ref &&
  18793. -1 < fields[i]->internal->id ) {
  18794. /* Dataset of references */
  18795. Mat_H5ReadNextReferenceData(fields[i]->internal->id,fields[i],mat);
  18796. } else {
  18797. Mat_VarRead73(mat,fields[i]);
  18798. }
  18799. }
  18800. break;
  18801. }
  18802. case MAT_C_CELL:
  18803. {
  18804. matvar_t **cells;
  18805. int i,ncells = 0;
  18806. if ( NULL != matvar->internal->hdf5_name ) {
  18807. dset_id = H5Dopen(fid,matvar->internal->hdf5_name,H5P_DEFAULT);
  18808. } else {
  18809. dset_id = matvar->internal->id;
  18810. H5Iinc_ref(dset_id);
  18811. }
  18812. ncells = matvar->nbytes / matvar->data_size;
  18813. cells = (matvar_t**)matvar->data;
  18814. for ( i = 0; i < ncells; i++ )
  18815. Mat_H5ReadNextReferenceData(cells[i]->internal->id,cells[i],mat);
  18816. break;
  18817. }
  18818. case MAT_C_SPARSE:
  18819. {
  18820. hid_t sparse_dset_id, space_id;
  18821. hsize_t dims[2] = {0,};
  18822. mat_sparse_t *sparse_data = (mat_sparse_t *)calloc(1,
  18823. sizeof(*sparse_data));
  18824. if ( NULL != matvar->internal->hdf5_name ) {
  18825. dset_id = H5Gopen(fid,matvar->internal->hdf5_name,H5P_DEFAULT);
  18826. } else {
  18827. dset_id = matvar->internal->id;
  18828. H5Iinc_ref(dset_id);
  18829. }
  18830. if ( H5Lexists(dset_id,"ir",H5P_DEFAULT) ) {
  18831. sparse_dset_id = H5Dopen(dset_id,"ir",H5P_DEFAULT);
  18832. space_id = H5Dget_space(sparse_dset_id);
  18833. H5Sget_simple_extent_dims(space_id,dims,NULL);
  18834. sparse_data->nir = dims[0];
  18835. sparse_data->ir = (int*)malloc(sparse_data->nir*
  18836. sizeof(*sparse_data->ir));
  18837. H5Dread(sparse_dset_id,H5T_NATIVE_INT,
  18838. H5S_ALL,H5S_ALL,H5P_DEFAULT,sparse_data->ir);
  18839. H5Sclose(space_id);
  18840. H5Dclose(sparse_dset_id);
  18841. }
  18842. if ( H5Lexists(dset_id,"jc",H5P_DEFAULT) ) {
  18843. sparse_dset_id = H5Dopen(dset_id,"jc",H5P_DEFAULT);
  18844. space_id = H5Dget_space(sparse_dset_id);
  18845. H5Sget_simple_extent_dims(space_id,dims,NULL);
  18846. sparse_data->njc = dims[0];
  18847. sparse_data->jc = (int*)malloc(sparse_data->njc*
  18848. sizeof(*sparse_data->jc));
  18849. H5Dread(sparse_dset_id,H5T_NATIVE_INT,
  18850. H5S_ALL,H5S_ALL,H5P_DEFAULT,sparse_data->jc);
  18851. H5Sclose(space_id);
  18852. H5Dclose(sparse_dset_id);
  18853. }
  18854. if ( H5Lexists(dset_id,"data",H5P_DEFAULT) ) {
  18855. size_t ndata_bytes;
  18856. sparse_dset_id = H5Dopen(dset_id,"data",H5P_DEFAULT);
  18857. space_id = H5Dget_space(sparse_dset_id);
  18858. H5Sget_simple_extent_dims(space_id,dims,NULL);
  18859. sparse_data->nzmax = dims[0];
  18860. sparse_data->ndata = dims[0];
  18861. matvar->data_size = sizeof(mat_sparse_t);
  18862. matvar->nbytes = matvar->data_size;
  18863. ndata_bytes = sparse_data->nzmax*Mat_SizeOf(matvar->data_type);
  18864. if ( !matvar->isComplex ) {
  18865. sparse_data->data = malloc(ndata_bytes);
  18866. if ( NULL != sparse_data->data ) {
  18867. H5Dread(sparse_dset_id,
  18868. Mat_data_type_to_hid_t(matvar->data_type),
  18869. H5S_ALL,H5S_ALL,H5P_DEFAULT,sparse_data->data);
  18870. }
  18871. } else {
  18872. mat_complex_split_t *complex_data;
  18873. hid_t h5_complex_base,h5_complex;
  18874. complex_data = (mat_complex_split_t*)malloc(sizeof(*complex_data));
  18875. complex_data->Re = malloc(ndata_bytes);
  18876. complex_data->Im = malloc(ndata_bytes);
  18877. h5_complex_base = Mat_data_type_to_hid_t(matvar->data_type);
  18878. h5_complex = H5Tcreate(H5T_COMPOUND,
  18879. H5Tget_size(h5_complex_base));
  18880. H5Tinsert(h5_complex,"real",0,h5_complex_base);
  18881. H5Dread(sparse_dset_id,h5_complex,H5S_ALL,H5S_ALL,
  18882. H5P_DEFAULT,complex_data->Re);
  18883. H5Tclose(h5_complex);
  18884. h5_complex = H5Tcreate(H5T_COMPOUND,
  18885. H5Tget_size(h5_complex_base));
  18886. H5Tinsert(h5_complex,"imag",0,h5_complex_base);
  18887. H5Dread(sparse_dset_id,h5_complex,H5S_ALL,H5S_ALL,
  18888. H5P_DEFAULT,complex_data->Im);
  18889. H5Tclose(h5_complex);
  18890. sparse_data->data = complex_data;
  18891. }
  18892. H5Sclose(space_id);
  18893. H5Dclose(sparse_dset_id);
  18894. }
  18895. H5Gclose(dset_id);
  18896. matvar->data = sparse_data;
  18897. break;
  18898. }
  18899. case MAT_C_EMPTY:
  18900. case MAT_C_FUNCTION:
  18901. case MAT_C_OBJECT:
  18902. break;
  18903. }
  18904. }
  18905. /** @if mat_devman
  18906. * @brief Reads a slab of data from the mat variable @c matvar
  18907. *
  18908. * @ingroup mat_internal
  18909. * @param mat MAT file pointer
  18910. * @param matvar pointer to the mat variable
  18911. * @param data pointer to store the read data in (must be of size
  18912. * edge[0]*...edge[rank-1]*Mat_SizeOfClass(matvar->class_type))
  18913. * @param start index to start reading data in each dimension
  18914. * @param stride write data every @c stride elements in each dimension
  18915. * @param edge number of elements to read in each dimension
  18916. * @retval 0 on success
  18917. * @endif
  18918. */
  18919. int
  18920. Mat_VarReadData73(mat_t *mat,matvar_t *matvar,void *data,
  18921. int *start,int *stride,int *edge)
  18922. {
  18923. int err = -1;
  18924. int k;
  18925. hid_t fid,dset_id,ref_id,dset_space,mem_space;
  18926. hsize_t dset_start[10],dset_stride[10],dset_edge[10];
  18927. if ( NULL == mat || NULL == matvar || NULL == data || NULL == start ||
  18928. NULL == stride || NULL == edge )
  18929. return err;
  18930. else if (NULL == matvar->internal->hdf5_name && 0 > matvar->internal->id)
  18931. return err;
  18932. fid = *(hid_t*)mat->fp;
  18933. for ( k = 0; k < matvar->rank; k++ ) {
  18934. dset_start[k] = start[matvar->rank-k-1];
  18935. dset_stride[k] = stride[matvar->rank-k-1];
  18936. dset_edge[k] = edge[matvar->rank-k-1];
  18937. }
  18938. mem_space = H5Screate_simple(matvar->rank, dset_edge, NULL);
  18939. switch (matvar->class_type) {
  18940. case MAT_C_DOUBLE:
  18941. case MAT_C_SINGLE:
  18942. case MAT_C_INT64:
  18943. case MAT_C_UINT64:
  18944. case MAT_C_INT32:
  18945. case MAT_C_UINT32:
  18946. case MAT_C_INT16:
  18947. case MAT_C_UINT16:
  18948. case MAT_C_INT8:
  18949. case MAT_C_UINT8:
  18950. if ( NULL != matvar->internal->hdf5_name ) {
  18951. ref_id = H5Dopen(fid,matvar->internal->hdf5_name,H5P_DEFAULT);
  18952. } else {
  18953. ref_id = matvar->internal->id;
  18954. H5Iinc_ref(ref_id);
  18955. }
  18956. if ( 0 < matvar->internal->hdf5_ref ) {
  18957. dset_id = H5Rdereference(ref_id,H5R_OBJECT,&matvar->internal->hdf5_ref);
  18958. } else {
  18959. dset_id = ref_id;
  18960. H5Iinc_ref(dset_id);
  18961. }
  18962. dset_space = H5Dget_space(dset_id);
  18963. H5Sselect_hyperslab(dset_space, H5S_SELECT_SET, dset_start,
  18964. dset_stride, dset_edge, NULL);
  18965. if ( !matvar->isComplex ) {
  18966. H5Dread(dset_id,Mat_class_type_to_hid_t(matvar->class_type),
  18967. mem_space,dset_space,H5P_DEFAULT,data);
  18968. } else {
  18969. mat_complex_split_t *complex_data = (mat_complex_split_t*)data;
  18970. hid_t h5_complex_base,h5_complex;
  18971. h5_complex_base = Mat_class_type_to_hid_t(matvar->class_type);
  18972. h5_complex = H5Tcreate(H5T_COMPOUND,
  18973. H5Tget_size(h5_complex_base));
  18974. H5Tinsert(h5_complex,"real",0,h5_complex_base);
  18975. H5Dread(dset_id,h5_complex,mem_space,dset_space,H5P_DEFAULT,
  18976. complex_data->Re);
  18977. H5Tclose(h5_complex);
  18978. h5_complex = H5Tcreate(H5T_COMPOUND,
  18979. H5Tget_size(h5_complex_base));
  18980. H5Tinsert(h5_complex,"imag",0,h5_complex_base);
  18981. H5Dread(dset_id,h5_complex,mem_space,dset_space,H5P_DEFAULT,
  18982. complex_data->Im);
  18983. H5Tclose(h5_complex);
  18984. }
  18985. H5Sclose(dset_space);
  18986. H5Dclose(dset_id);
  18987. H5Dclose(ref_id);
  18988. err = 0;
  18989. break;
  18990. default:
  18991. break;
  18992. }
  18993. H5Sclose(mem_space);
  18994. return err;
  18995. }
  18996. /** @if mat_devman
  18997. * @brief Reads a subset of a MAT variable using a 1-D indexing
  18998. *
  18999. * Reads data from a MAT variable using a linear (1-D) indexing mode. The
  19000. * variable must have been read by Mat_VarReadInfo.
  19001. * @ingroup mat_internal
  19002. * @param mat MAT file pointer
  19003. * @param matvar pointer to the mat variable
  19004. * @param data pointer to store the read data in (must be of size
  19005. * edge*Mat_SizeOfClass(matvar->class_type))
  19006. * @param start starting index
  19007. * @param stride stride of data
  19008. * @param edge number of elements to read
  19009. * @retval 0 on success
  19010. * @endif
  19011. */
  19012. int
  19013. Mat_VarReadDataLinear73(mat_t *mat,matvar_t *matvar,void *data,
  19014. int start,int stride,int edge)
  19015. {
  19016. int err = -1;
  19017. hid_t fid,dset_id,dset_space,mem_space;
  19018. hsize_t dset_start,dset_stride,dset_edge;
  19019. hsize_t *points, k, dimp[10];
  19020. if ( NULL == mat || NULL == matvar || NULL == data )
  19021. return err;
  19022. else if (NULL == matvar->internal->hdf5_name && 0 > matvar->internal->id)
  19023. return err;
  19024. fid = *(hid_t*)mat->fp;
  19025. dset_start = start;
  19026. dset_stride = stride;
  19027. dset_edge = edge;
  19028. mem_space = H5Screate_simple(1, &dset_edge, NULL);
  19029. switch (matvar->class_type) {
  19030. case MAT_C_DOUBLE:
  19031. case MAT_C_SINGLE:
  19032. case MAT_C_INT64:
  19033. case MAT_C_UINT64:
  19034. case MAT_C_INT32:
  19035. case MAT_C_UINT32:
  19036. case MAT_C_INT16:
  19037. case MAT_C_UINT16:
  19038. case MAT_C_INT8:
  19039. case MAT_C_UINT8:
  19040. if ( NULL != matvar->internal->hdf5_name ) {
  19041. dset_id = H5Dopen(fid,matvar->internal->hdf5_name,H5P_DEFAULT);
  19042. } else {
  19043. dset_id = matvar->internal->id;
  19044. H5Iinc_ref(dset_id);
  19045. }
  19046. points = (hsize_t*)malloc(matvar->rank*dset_edge*sizeof(*points));
  19047. if ( NULL == points ) {
  19048. err = -2;
  19049. break;
  19050. }
  19051. dimp[0] = 1;
  19052. for ( k = 1; k < matvar->rank; k++ )
  19053. dimp[k] = dimp[k-1]*matvar->dims[k-1];
  19054. for ( k = 0; k < dset_edge; k++ ) {
  19055. size_t l, coord, idx;
  19056. coord = start + k*stride;
  19057. for ( l = matvar->rank; l--; ) {
  19058. idx = coord / dimp[l];
  19059. points[matvar->rank*(k+1)-1-l] = idx;
  19060. coord -= idx*dimp[l];
  19061. }
  19062. }
  19063. dset_space = H5Dget_space(dset_id);
  19064. H5Sselect_elements(dset_space,H5S_SELECT_SET,dset_edge,points);
  19065. if ( !matvar->isComplex ) {
  19066. H5Dread(dset_id,Mat_class_type_to_hid_t(matvar->class_type),
  19067. mem_space,dset_space,H5P_DEFAULT,data);
  19068. } else {
  19069. mat_complex_split_t *complex_data = (mat_complex_split_t*)data;
  19070. hid_t h5_complex_base,h5_complex;
  19071. h5_complex_base = Mat_class_type_to_hid_t(matvar->class_type);
  19072. h5_complex = H5Tcreate(H5T_COMPOUND,
  19073. H5Tget_size(h5_complex_base));
  19074. H5Tinsert(h5_complex,"real",0,h5_complex_base);
  19075. H5Dread(dset_id,h5_complex,mem_space,dset_space,H5P_DEFAULT,
  19076. complex_data->Re);
  19077. H5Tclose(h5_complex);
  19078. h5_complex = H5Tcreate(H5T_COMPOUND,
  19079. H5Tget_size(h5_complex_base));
  19080. H5Tinsert(h5_complex,"imag",0,h5_complex_base);
  19081. H5Dread(dset_id,h5_complex,mem_space,dset_space,H5P_DEFAULT,
  19082. complex_data->Im);
  19083. H5Tclose(h5_complex);
  19084. }
  19085. H5Sclose(dset_space);
  19086. H5Dclose(dset_id);
  19087. free(points);
  19088. err = 0;
  19089. break;
  19090. default:
  19091. break;
  19092. }
  19093. H5Sclose(mem_space);
  19094. return err;
  19095. }
  19096. /** @if mat_devman
  19097. * @brief Reads the header information for the next MAT variable
  19098. *
  19099. * @ingroup mat_internal
  19100. * @param mat MAT file pointer
  19101. * @return pointer to the MAT variable or NULL
  19102. * @endif
  19103. */
  19104. matvar_t *
  19105. Mat_VarReadNextInfo73( mat_t *mat )
  19106. {
  19107. hid_t fid;
  19108. hsize_t idx;
  19109. herr_t herr;
  19110. struct mat_read_next_iter_data mat_data;
  19111. if( mat == NULL )
  19112. return NULL;
  19113. if ( mat->next_index >= mat->num_datasets )
  19114. return NULL;
  19115. fid = *(hid_t*)mat->fp;
  19116. idx = (hsize_t)mat->next_index;
  19117. mat_data.mat = mat;
  19118. mat_data.matvar = NULL;
  19119. herr = H5Literate(fid, H5_INDEX_NAME, H5_ITER_NATIVE, &idx, Mat_VarReadNextInfoIterate, (void*)&mat_data);
  19120. if ( herr > 0 )
  19121. mat->next_index = (long)idx;
  19122. return mat_data.matvar;
  19123. }
  19124. static herr_t
  19125. Mat_VarReadNextInfoIterate(hid_t fid, const char *name, const H5L_info_t *info, void *op_data)
  19126. {
  19127. mat_t *mat;
  19128. matvar_t *matvar;
  19129. H5O_info_t object_info;
  19130. struct mat_read_next_iter_data *mat_data;
  19131. /* FIXME: follow symlinks, datatypes? */
  19132. /* Check that this is not the /#refs# group */
  19133. if ( 0 == strcmp(name, "#refs#") )
  19134. return 0;
  19135. H5Oget_info_by_name(fid, name, &object_info, H5P_DEFAULT);
  19136. if ( H5O_TYPE_DATASET != object_info.type && H5O_TYPE_GROUP != object_info.type )
  19137. return 0;
  19138. mat_data = (struct mat_read_next_iter_data *)op_data;
  19139. if (mat_data == NULL )
  19140. return -1;
  19141. mat = mat_data->mat;
  19142. matvar = mat_data->matvar;
  19143. if ( NULL == (matvar = Mat_VarCalloc()) )
  19144. return -1;
  19145. matvar->internal->fp = mat;
  19146. matvar->name = (char*)malloc(1 + strlen(name));
  19147. if ( matvar->name == NULL ) {
  19148. Mat_VarFree(matvar);
  19149. return -1;
  19150. }
  19151. strcpy(matvar->name, name);
  19152. switch ( object_info.type ) {
  19153. case H5O_TYPE_DATASET:
  19154. {
  19155. ssize_t name_len;
  19156. /* FIXME */
  19157. hsize_t dims[10];
  19158. hid_t attr_id,type_id,dset_id,space_id;
  19159. dset_id = H5Dopen(fid,matvar->name,H5P_DEFAULT);
  19160. /* Get the HDF5 name of the variable */
  19161. name_len = H5Iget_name(dset_id,NULL,0);
  19162. if ( name_len > 0 ) {
  19163. matvar->internal->hdf5_name = (char*)malloc(name_len+1);
  19164. (void)H5Iget_name(dset_id,matvar->internal->hdf5_name,
  19165. name_len+1);
  19166. } else {
  19167. /* Can not get an internal name, so leave the identifier open */
  19168. matvar->internal->id = dset_id;
  19169. }
  19170. space_id = H5Dget_space(dset_id);
  19171. matvar->rank = H5Sget_simple_extent_ndims(space_id);
  19172. matvar->dims = (size_t*)malloc(matvar->rank*sizeof(*matvar->dims));
  19173. if ( NULL != matvar->dims ) {
  19174. int k;
  19175. H5Sget_simple_extent_dims(space_id,dims,NULL);
  19176. for ( k = 0; k < matvar->rank; k++ )
  19177. matvar->dims[k] = dims[matvar->rank - k - 1];
  19178. } else {
  19179. H5Sclose(space_id);
  19180. Mat_VarFree(matvar);
  19181. Mat_Critical("Error allocating memory for matvar->dims");
  19182. return -1;
  19183. }
  19184. H5Sclose(space_id);
  19185. Mat_H5ReadClassType(matvar,dset_id);
  19186. if ( H5Aexists_by_name(dset_id,".","MATLAB_global",H5P_DEFAULT) ) {
  19187. attr_id = H5Aopen_by_name(dset_id,".","MATLAB_global",H5P_DEFAULT,H5P_DEFAULT);
  19188. /* FIXME: Check that dataspace is scalar */
  19189. H5Aread(attr_id,H5T_NATIVE_INT,&matvar->isGlobal);
  19190. H5Aclose(attr_id);
  19191. }
  19192. /* Check for attribute that indicates an empty array */
  19193. if ( H5Aexists_by_name(dset_id,".","MATLAB_empty",H5P_DEFAULT) ) {
  19194. int empty = 0;
  19195. attr_id = H5Aopen_by_name(dset_id,".","MATLAB_empty",H5P_DEFAULT,H5P_DEFAULT);
  19196. /* FIXME: Check that dataspace is scalar */
  19197. H5Aread(attr_id,H5T_NATIVE_INT,&empty);
  19198. H5Aclose(attr_id);
  19199. if ( empty ) {
  19200. matvar->rank = matvar->dims[0];
  19201. free(matvar->dims);
  19202. matvar->dims = (size_t*)calloc(matvar->rank,sizeof(*matvar->dims));
  19203. H5Dread(dset_id,Mat_dims_type_to_hid_t(),H5S_ALL,H5S_ALL,
  19204. H5P_DEFAULT,matvar->dims);
  19205. }
  19206. }
  19207. /* Test if dataset type is compound and if so if it's complex */
  19208. type_id = H5Dget_type(dset_id);
  19209. if ( H5T_COMPOUND == H5Tget_class(type_id) ) {
  19210. /* FIXME: Any more checks? */
  19211. matvar->isComplex = MAT_F_COMPLEX;
  19212. }
  19213. H5Tclose(type_id);
  19214. /* If the dataset is a cell array read the info of the cells */
  19215. if ( MAT_C_CELL == matvar->class_type ) {
  19216. matvar_t **cells;
  19217. int i,ncells = 1;
  19218. hobj_ref_t *ref_ids;
  19219. for ( i = 0; i < matvar->rank; i++ )
  19220. ncells *= matvar->dims[i];
  19221. matvar->data_size = sizeof(matvar_t**);
  19222. matvar->nbytes = ncells*matvar->data_size;
  19223. matvar->data = malloc(matvar->nbytes);
  19224. cells = (matvar_t**)matvar->data;
  19225. if ( ncells ) {
  19226. ref_ids = (hobj_ref_t*)malloc(ncells*sizeof(*ref_ids));
  19227. H5Dread(dset_id,H5T_STD_REF_OBJ,H5S_ALL,H5S_ALL,H5P_DEFAULT,
  19228. ref_ids);
  19229. for ( i = 0; i < ncells; i++ ) {
  19230. hid_t ref_id;
  19231. cells[i] = Mat_VarCalloc();
  19232. cells[i]->internal->hdf5_ref = ref_ids[i];
  19233. /* Closing of ref_id is done in
  19234. * Mat_H5ReadNextReferenceInfo
  19235. */
  19236. ref_id = H5Rdereference(dset_id,H5R_OBJECT,ref_ids+i);
  19237. cells[i]->internal->id=ref_id;
  19238. cells[i]->internal->fp=matvar->internal->fp;
  19239. Mat_H5ReadNextReferenceInfo(ref_id,cells[i],mat);
  19240. }
  19241. free(ref_ids);
  19242. }
  19243. } else if ( MAT_C_STRUCT == matvar->class_type ) {
  19244. /* Empty structures can be a dataset */
  19245. /* Check if the structure defines its fields in MATLAB_fields */
  19246. if ( H5Aexists_by_name(dset_id,".","MATLAB_fields",H5P_DEFAULT) ) {
  19247. int i;
  19248. hid_t field_id;
  19249. hsize_t nfields;
  19250. hvl_t *fieldnames_vl;
  19251. attr_id = H5Aopen_by_name(dset_id,".","MATLAB_fields",H5P_DEFAULT,H5P_DEFAULT);
  19252. space_id = H5Aget_space(attr_id);
  19253. (void)H5Sget_simple_extent_dims(space_id,&nfields,NULL);
  19254. field_id = H5Aget_type(attr_id);
  19255. fieldnames_vl = (hvl_t*)malloc(nfields*sizeof(*fieldnames_vl));
  19256. H5Aread(attr_id,field_id,fieldnames_vl);
  19257. matvar->internal->num_fields = nfields;
  19258. matvar->internal->fieldnames =
  19259. (char**)calloc(nfields,sizeof(*matvar->internal->fieldnames));
  19260. for ( i = 0; i < nfields; i++ ) {
  19261. matvar->internal->fieldnames[i] =
  19262. (char*)calloc(fieldnames_vl[i].len+1,1);
  19263. memcpy(matvar->internal->fieldnames[i],fieldnames_vl[i].p,
  19264. fieldnames_vl[i].len);
  19265. }
  19266. H5Dvlen_reclaim(field_id,space_id,H5P_DEFAULT,fieldnames_vl);
  19267. H5Sclose(space_id);
  19268. H5Tclose(field_id);
  19269. H5Aclose(attr_id);
  19270. free(fieldnames_vl);
  19271. }
  19272. }
  19273. if ( matvar->internal->id != dset_id ) {
  19274. /* Close dataset and increment count */
  19275. H5Dclose(dset_id);
  19276. }
  19277. mat_data->matvar = matvar;
  19278. break;
  19279. }
  19280. case H5O_TYPE_GROUP:
  19281. {
  19282. hid_t dset_id;
  19283. dset_id = H5Gopen(fid,matvar->name,H5P_DEFAULT);
  19284. Mat_H5ReadGroupInfo(mat,matvar,dset_id);
  19285. H5Gclose(dset_id);
  19286. mat_data->matvar = matvar;
  19287. break;
  19288. }
  19289. default:
  19290. break;
  19291. }
  19292. return 1;
  19293. }
  19294. /** @if mat_devman
  19295. * @brief Writes a matlab variable to a version 7.3 matlab file
  19296. *
  19297. * @ingroup mat_internal
  19298. * @param mat MAT file pointer
  19299. * @param matvar pointer to the mat variable
  19300. * @param compress option to compress the variable
  19301. * (only works for numeric types)
  19302. * @retval 0 on success
  19303. * @endif
  19304. */
  19305. int
  19306. Mat_VarWrite73(mat_t *mat,matvar_t *matvar,int compress)
  19307. {
  19308. hid_t id;
  19309. if ( NULL == mat || NULL == matvar )
  19310. return -1;
  19311. matvar->compression = (enum matio_compression)compress;
  19312. id = *(hid_t*)mat->fp;
  19313. return Mat_VarWriteNext73(id,matvar,matvar->name,&(mat->refs_id));
  19314. }
  19315. #endif
  19316. /* -------------------------------
  19317. * ---------- matvar_cell.c
  19318. * -------------------------------
  19319. */
  19320. #include <stdlib.h>
  19321. #include <string.h>
  19322. /** @brief Returns a pointer to the Cell array at a specific index
  19323. *
  19324. * Returns a pointer to the Cell Array Field at the given 1-relative index.
  19325. * MAT file must be a version 5 matlab file.
  19326. * @ingroup MAT
  19327. * @param matvar Pointer to the Cell Array MAT variable
  19328. * @param index linear index of cell to return
  19329. * @return Pointer to the Cell Array Field on success, NULL on error
  19330. */
  19331. matvar_t *
  19332. Mat_VarGetCell(matvar_t *matvar,int index)
  19333. {
  19334. int nmemb = 1, i;
  19335. matvar_t *cell = NULL;
  19336. if ( matvar == NULL )
  19337. return NULL;
  19338. for ( i = 0; i < matvar->rank; i++ )
  19339. nmemb *= matvar->dims[i];
  19340. if ( index < nmemb )
  19341. cell = *((matvar_t **)matvar->data + index);
  19342. return cell;
  19343. }
  19344. /** @brief Indexes a cell array
  19345. *
  19346. * Finds cells of a cell array given a start, stride, and edge for each.
  19347. * dimension. The cells are placed in a pointer array. The cells should not
  19348. * be freed, but the array of pointers should be. If copies are needed,
  19349. * use Mat_VarDuplicate on each cell.
  19350. *
  19351. * Note that this function is limited to structure arrays with a rank less than
  19352. * 10.
  19353. *
  19354. * @ingroup MAT
  19355. * @param matvar Cell Array matlab variable
  19356. * @param start vector of length rank with 0-relative starting coordinates for
  19357. * each dimension.
  19358. * @param stride vector of length rank with strides for each dimension.
  19359. * @param edge vector of length rank with the number of elements to read in
  19360. * each dimension.
  19361. * @returns an array of pointers to the cells
  19362. */
  19363. matvar_t **
  19364. Mat_VarGetCells(matvar_t *matvar,int *start,int *stride,int *edge)
  19365. {
  19366. int i, j, N, I;
  19367. size_t idx[10] = {0,}, cnt[10] = {0,}, dimp[10] = {0,};
  19368. matvar_t **cells;
  19369. if ( (matvar == NULL) || (start == NULL) || (stride == NULL) ||
  19370. (edge == NULL) ) {
  19371. return NULL;
  19372. } else if ( matvar->rank > 9 ) {
  19373. return NULL;
  19374. }
  19375. dimp[0] = matvar->dims[0];
  19376. N = edge[0];
  19377. I = start[0];
  19378. idx[0] = start[0];
  19379. for ( i = 1; i < matvar->rank; i++ ) {
  19380. idx[i] = start[i];
  19381. dimp[i] = dimp[i-1]*matvar->dims[i];
  19382. N *= edge[i];
  19383. I += start[i]*dimp[i-1];
  19384. }
  19385. cells = (matvar_t**)malloc(N*sizeof(matvar_t *));
  19386. for ( i = 0; i < N; i+=edge[0] ) {
  19387. for ( j = 0; j < edge[0]; j++ ) {
  19388. cells[i+j] = *((matvar_t **)matvar->data + I);
  19389. I += stride[0];
  19390. }
  19391. idx[0] = start[0];
  19392. I = idx[0];
  19393. cnt[1]++;
  19394. idx[1] += stride[1];
  19395. for ( j = 1; j < matvar->rank; j++ ) {
  19396. if ( cnt[j] == edge[j] ) {
  19397. cnt[j] = 0;
  19398. idx[j] = start[j];
  19399. cnt[j+1]++;
  19400. idx[j+1] += stride[j+1];
  19401. }
  19402. I += idx[j]*dimp[j-1];
  19403. }
  19404. }
  19405. return cells;
  19406. }
  19407. /** @brief Indexes a cell array
  19408. *
  19409. * Finds cells of a cell array given a linear indexed start, stride, and edge.
  19410. * The cells are placed in a pointer array. The cells themself should not
  19411. * be freed as they are part of the original cell array, but the pointer array
  19412. * should be. If copies are needed, use Mat_VarDuplicate on each of the cells.
  19413. * MAT file version must be 5.
  19414. * @ingroup MAT
  19415. * @param matvar Cell Array matlab variable
  19416. * @param start starting index
  19417. * @param stride stride
  19418. * @param edge Number of cells to get
  19419. * @returns an array of pointers to the cells
  19420. */
  19421. matvar_t **
  19422. Mat_VarGetCellsLinear(matvar_t *matvar,int start,int stride,int edge)
  19423. {
  19424. int i, I;
  19425. matvar_t **cells = NULL;
  19426. if ( matvar != NULL ) {
  19427. cells = (matvar_t**)malloc(edge*sizeof(matvar_t *));
  19428. I = start;
  19429. for ( i = 0; i < edge; i++ ) {
  19430. cells[i] = *((matvar_t **)matvar->data + I);
  19431. I += stride;
  19432. }
  19433. }
  19434. return cells;
  19435. }
  19436. /** @brief Sets the element of the cell array at the specific index
  19437. *
  19438. * Sets the element of the cell array at the given 0-relative index to @c cell.
  19439. * @ingroup MAT
  19440. * @param matvar Pointer to the cell array variable
  19441. * @param index 0-relative linear index of the cell to set
  19442. * @param cell Pointer to the cell to set
  19443. * @return Pointer to the previous cell element, or NULL if there was no
  19444. * previous cell element or error.
  19445. */
  19446. matvar_t *
  19447. Mat_VarSetCell(matvar_t *matvar,int index,matvar_t *cell)
  19448. {
  19449. int nmemb = 1, i;
  19450. matvar_t **cells, *old_cell = NULL;
  19451. if ( matvar == NULL || matvar->rank < 1 )
  19452. return NULL;
  19453. for ( i = 0; i < matvar->rank; i++ )
  19454. nmemb *= matvar->dims[i];
  19455. cells = (matvar_t**)matvar->data;
  19456. if ( index < nmemb ) {
  19457. old_cell = cells[index];
  19458. cells[index] = cell;
  19459. }
  19460. return old_cell;
  19461. }
  19462. /* -------------------------------
  19463. * ---------- matvar_struct.c
  19464. * -------------------------------
  19465. */
  19466. #include <stdlib.h>
  19467. #include <string.h>
  19468. /** @brief Creates a structure MATLAB variable with the given name and fields
  19469. *
  19470. * @ingroup MAT
  19471. * @param name Name of the structure variable to create
  19472. * @param rank Rank of the variable
  19473. * @param dims array of dimensions of the variable of size rank
  19474. * @param fields Array of @c nfields fieldnames
  19475. * @param nfields Number of fields in the structure
  19476. * @return Pointer to the new structure MATLAB variable on success, NULL on error
  19477. */
  19478. matvar_t *
  19479. Mat_VarCreateStruct(const char *name,int rank,size_t *dims,const char **fields,
  19480. unsigned nfields)
  19481. {
  19482. int i, nmemb = 1;
  19483. matvar_t *matvar;
  19484. if ( NULL == dims )
  19485. return NULL;
  19486. matvar = Mat_VarCalloc();
  19487. if ( NULL == matvar )
  19488. return NULL;
  19489. matvar->compression = MAT_COMPRESSION_NONE;
  19490. if ( NULL != name )
  19491. matvar->name = mat_strdup(name);
  19492. matvar->rank = rank;
  19493. matvar->dims = (size_t*)malloc(matvar->rank*sizeof(*matvar->dims));
  19494. for ( i = 0; i < matvar->rank; i++ ) {
  19495. matvar->dims[i] = dims[i];
  19496. nmemb *= dims[i];
  19497. }
  19498. matvar->class_type = MAT_C_STRUCT;
  19499. matvar->data_type = MAT_T_STRUCT;
  19500. matvar->data_size = sizeof(matvar_t *);
  19501. if ( nfields ) {
  19502. matvar->internal->num_fields = nfields;
  19503. matvar->internal->fieldnames =
  19504. (char**)malloc(nfields*sizeof(*matvar->internal->fieldnames));
  19505. if ( NULL == matvar->internal->fieldnames ) {
  19506. Mat_VarFree(matvar);
  19507. matvar = NULL;
  19508. } else {
  19509. for ( i = 0; i < nfields; i++ ) {
  19510. if ( NULL == fields[i] ) {
  19511. Mat_VarFree(matvar);
  19512. matvar = NULL;
  19513. break;
  19514. } else {
  19515. matvar->internal->fieldnames[i] = mat_strdup(fields[i]);
  19516. }
  19517. }
  19518. }
  19519. if ( NULL != matvar && nmemb > 0 && nfields > 0 ) {
  19520. matvar_t **field_vars;
  19521. matvar->nbytes = nmemb*nfields*matvar->data_size;
  19522. matvar->data = malloc(matvar->nbytes);
  19523. field_vars = (matvar_t**)matvar->data;
  19524. for ( i = 0; i < nfields*nmemb; i++ )
  19525. field_vars[i] = NULL;
  19526. }
  19527. }
  19528. return matvar;
  19529. }
  19530. /** @brief Adds a field to a structure
  19531. *
  19532. * Adds the given field to the structure. fields should be an array of matvar_t
  19533. * pointers of the same size as the structure (i.e. 1 field per structure
  19534. * element).
  19535. * @ingroup MAT
  19536. * @param matvar Pointer to the Structure MAT variable
  19537. * @param fieldname Name of field to be added
  19538. * @retval 0 on success
  19539. */
  19540. int
  19541. Mat_VarAddStructField(matvar_t *matvar,const char *fieldname)
  19542. {
  19543. int i, f, nfields, nmemb, cnt = 0;
  19544. matvar_t **new_data, **old_data;
  19545. if ( matvar == NULL || fieldname == NULL )
  19546. return -1;
  19547. nmemb = 1;
  19548. for ( i = 0; i < matvar->rank; i++ )
  19549. nmemb *= matvar->dims[i];
  19550. nfields = matvar->internal->num_fields+1;
  19551. matvar->internal->num_fields = nfields;
  19552. matvar->internal->fieldnames =
  19553. (char**)realloc(matvar->internal->fieldnames,
  19554. nfields*sizeof(*matvar->internal->fieldnames));
  19555. matvar->internal->fieldnames[nfields-1] = mat_strdup(fieldname);
  19556. new_data = (matvar_t**)malloc(nfields*nmemb*sizeof(*new_data));
  19557. if ( new_data == NULL )
  19558. return -1;
  19559. old_data = (matvar_t**)matvar->data;
  19560. for ( i = 0; i < nmemb; i++ ) {
  19561. for ( f = 0; f < nfields-1; f++ )
  19562. new_data[cnt++] = old_data[i*(nfields-1)+f];
  19563. new_data[cnt++] = NULL;
  19564. }
  19565. free(matvar->data);
  19566. matvar->data = new_data;
  19567. matvar->nbytes = nfields*nmemb*sizeof(*new_data);
  19568. return 0;
  19569. }
  19570. /** @brief Returns the number of fields in a structure variable
  19571. *
  19572. * Returns the number of fields in the given structure.
  19573. * @ingroup MAT
  19574. * @param matvar Structure matlab variable
  19575. * @returns Number of fields
  19576. */
  19577. unsigned
  19578. Mat_VarGetNumberOfFields(matvar_t *matvar)
  19579. {
  19580. int nfields;
  19581. if ( matvar == NULL || matvar->class_type != MAT_C_STRUCT ||
  19582. NULL == matvar->internal ) {
  19583. nfields = 0;
  19584. } else {
  19585. nfields = matvar->internal->num_fields;
  19586. }
  19587. return nfields;
  19588. }
  19589. /** @brief Returns the fieldnames of a structure variable
  19590. *
  19591. * Returns the fieldnames for the given structure. The returned pointers are
  19592. * internal to the structure and should not be free'd.
  19593. * @ingroup MAT
  19594. * @param matvar Structure matlab variable
  19595. * @returns Array of fieldnames
  19596. */
  19597. char * const *
  19598. Mat_VarGetStructFieldnames(const matvar_t *matvar)
  19599. {
  19600. if ( matvar == NULL || matvar->class_type != MAT_C_STRUCT ||
  19601. NULL == matvar->internal ) {
  19602. return NULL;
  19603. } else {
  19604. return matvar->internal->fieldnames;
  19605. }
  19606. }
  19607. /** @brief Finds a field of a structure by the field's index
  19608. *
  19609. * Returns a pointer to the structure field at the given 0-relative index.
  19610. * @ingroup MAT
  19611. * @param matvar Pointer to the Structure MAT variable
  19612. * @param field_index 0-relative index of the field.
  19613. * @param index linear index of the structure array
  19614. * @return Pointer to the structure field on success, NULL on error
  19615. */
  19616. matvar_t *
  19617. Mat_VarGetStructFieldByIndex(matvar_t *matvar,size_t field_index,size_t index)
  19618. {
  19619. int i, nfields;
  19620. matvar_t *field = NULL;
  19621. size_t nmemb;
  19622. if ( matvar == NULL || matvar->class_type != MAT_C_STRUCT ||
  19623. matvar->data_size == 0 )
  19624. return field;
  19625. nmemb = 1;
  19626. for ( i = 0; i < matvar->rank; i++ )
  19627. nmemb *= matvar->dims[i];
  19628. nfields = matvar->internal->num_fields;
  19629. if ( nmemb > 0 && index >= nmemb ) {
  19630. Mat_Critical("Mat_VarGetStructField: structure index out of bounds");
  19631. } else if ( nfields > 0 ) {
  19632. if ( field_index > nfields ) {
  19633. Mat_Critical("Mat_VarGetStructField: field index out of bounds");
  19634. } else {
  19635. field = *((matvar_t **)matvar->data+index*nfields+field_index);
  19636. }
  19637. }
  19638. return field;
  19639. }
  19640. /** @brief Finds a field of a structure by the field's name
  19641. *
  19642. * Returns a pointer to the structure field at the given 0-relative index.
  19643. * @ingroup MAT
  19644. * @param matvar Pointer to the Structure MAT variable
  19645. * @param field_name Name of the structure field
  19646. * @param index linear index of the structure array
  19647. * @return Pointer to the structure field on success, NULL on error
  19648. */
  19649. matvar_t *
  19650. Mat_VarGetStructFieldByName(matvar_t *matvar,const char *field_name,
  19651. size_t index)
  19652. {
  19653. int i, nfields, field_index;
  19654. matvar_t *field = NULL;
  19655. size_t nmemb;
  19656. if ( matvar == NULL || matvar->class_type != MAT_C_STRUCT ||
  19657. matvar->data_size == 0 )
  19658. return field;
  19659. nmemb = 1;
  19660. for ( i = 0; i < matvar->rank; i++ )
  19661. nmemb *= matvar->dims[i];
  19662. nfields = matvar->internal->num_fields;
  19663. field_index = -1;
  19664. for ( i = 0; i < nfields; i++ ) {
  19665. if ( !strcmp(matvar->internal->fieldnames[i],field_name) ) {
  19666. field_index = i;
  19667. break;
  19668. }
  19669. }
  19670. if ( index >= nmemb ) {
  19671. Mat_Critical("Mat_VarGetStructField: structure index out of bounds");
  19672. } else if ( field_index >= 0 ) {
  19673. field = *((matvar_t **)matvar->data+index*nfields+field_index);
  19674. }
  19675. return field;
  19676. }
  19677. /** @brief Finds a field of a structure
  19678. *
  19679. * Returns a pointer to the structure field at the given 0-relative index.
  19680. * @ingroup MAT
  19681. * @param matvar Pointer to the Structure MAT variable
  19682. * @param name_or_index Name of the field, or the 1-relative index of the field
  19683. * If the index is used, it should be the address of an integer variable whose
  19684. * value is the index number.
  19685. * @param opt MAT_BY_NAME if the name_or_index is the name or MAT_BY_INDEX if
  19686. * the index was passed.
  19687. * @param index linear index of the structure to find the field of
  19688. * @return Pointer to the Structure Field on success, NULL on error
  19689. */
  19690. matvar_t *
  19691. Mat_VarGetStructField(matvar_t *matvar,void *name_or_index,int opt,int index)
  19692. {
  19693. int i, err = 0, nfields, nmemb;
  19694. matvar_t *field = NULL;
  19695. nmemb = 1;
  19696. for ( i = 0; i < matvar->rank; i++ )
  19697. nmemb *= matvar->dims[i];
  19698. nfields = matvar->internal->num_fields;
  19699. if ( index < 0 || (nmemb > 0 && index >= nmemb ))
  19700. err = 1;
  19701. else if ( nfields < 1 )
  19702. err = 1;
  19703. if ( !err && (opt == MAT_BY_INDEX) ) {
  19704. size_t field_index = *(int *)name_or_index;
  19705. if ( field_index > 0 )
  19706. field = Mat_VarGetStructFieldByIndex(matvar,field_index-1,index);
  19707. } else if ( !err && (opt == MAT_BY_NAME) ) {
  19708. field = Mat_VarGetStructFieldByName(matvar,(const char*)name_or_index,index);
  19709. }
  19710. return field;
  19711. }
  19712. /** @brief Indexes a structure
  19713. *
  19714. * Finds structures of a structure array given a start, stride, and edge for
  19715. * each dimension. The structures are placed in a new structure array. If
  19716. * copy_fields is non-zero, the indexed structures are copied and should be
  19717. * freed, but if copy_fields is zero, the indexed structures are pointers to
  19718. * the original, but should still be freed. The structures have a flag set
  19719. * so that the structure fields are not freed.
  19720. *
  19721. * Note that this function is limited to structure arrays with a rank less than
  19722. * 10.
  19723. *
  19724. * @ingroup MAT
  19725. * @param matvar Structure matlab variable
  19726. * @param start vector of length rank with 0-relative starting coordinates for
  19727. * each dimension.
  19728. * @param stride vector of length rank with strides for each dimension.
  19729. * @param edge vector of length rank with the number of elements to read in
  19730. * each dimension.
  19731. * @param copy_fields 1 to copy the fields, 0 to just set pointers to them.
  19732. * @returns A new structure array with fields indexed from @c matvar.
  19733. */
  19734. matvar_t *
  19735. Mat_VarGetStructs(matvar_t *matvar,int *start,int *stride,int *edge,
  19736. int copy_fields)
  19737. {
  19738. size_t i,j,N,I,nfields,field,idx[10] = {0,},cnt[10] = {0,},dimp[10] = {0,};
  19739. matvar_t **fields, *struct_slab;
  19740. if ( (matvar == NULL) || (start == NULL) || (stride == NULL) ||
  19741. (edge == NULL) ) {
  19742. return NULL;
  19743. } else if ( matvar->rank > 9 ) {
  19744. return NULL;
  19745. } else if ( matvar->class_type != MAT_C_STRUCT ) {
  19746. return NULL;
  19747. }
  19748. struct_slab = Mat_VarDuplicate(matvar,0);
  19749. if ( !copy_fields )
  19750. struct_slab->mem_conserve = 1;
  19751. nfields = matvar->internal->num_fields;
  19752. dimp[0] = matvar->dims[0];
  19753. N = edge[0];
  19754. I = start[0];
  19755. struct_slab->dims[0] = edge[0];
  19756. idx[0] = start[0];
  19757. for ( i = 1; i < matvar->rank; i++ ) {
  19758. idx[i] = start[i];
  19759. dimp[i] = dimp[i-1]*matvar->dims[i];
  19760. N *= edge[i];
  19761. I += start[i]*dimp[i-1];
  19762. struct_slab->dims[i] = edge[i];
  19763. }
  19764. I *= nfields;
  19765. struct_slab->nbytes = N*nfields*sizeof(matvar_t *);
  19766. struct_slab->data = malloc(struct_slab->nbytes);
  19767. if ( struct_slab->data == NULL ) {
  19768. Mat_VarFree(struct_slab);
  19769. return NULL;
  19770. }
  19771. fields = (matvar_t**)struct_slab->data;
  19772. for ( i = 0; i < N; i+=edge[0] ) {
  19773. for ( j = 0; j < edge[0]; j++ ) {
  19774. for ( field = 0; field < nfields; field++ ) {
  19775. if ( copy_fields )
  19776. fields[(i+j)*nfields+field] =
  19777. Mat_VarDuplicate(*((matvar_t **)matvar->data + I),1);
  19778. else
  19779. fields[(i+j)*nfields+field] =
  19780. *((matvar_t **)matvar->data + I);
  19781. I++;
  19782. }
  19783. if ( stride != 0 )
  19784. I += (stride[0]-1)*nfields;
  19785. }
  19786. idx[0] = start[0];
  19787. I = idx[0];
  19788. cnt[1]++;
  19789. idx[1] += stride[1];
  19790. for ( j = 1; j < matvar->rank; j++ ) {
  19791. if ( cnt[j] == edge[j] ) {
  19792. cnt[j] = 0;
  19793. idx[j] = start[j];
  19794. cnt[j+1]++;
  19795. idx[j+1] += stride[j+1];
  19796. }
  19797. I += idx[j]*dimp[j-1];
  19798. }
  19799. I *= nfields;
  19800. }
  19801. return struct_slab;
  19802. }
  19803. /** @brief Indexes a structure
  19804. *
  19805. * Finds structures of a structure array given a single (linear)start, stride,
  19806. * and edge. The structures are placed in a new structure array. If
  19807. * copy_fields is non-zero, the indexed structures are copied and should be
  19808. * freed, but if copy_fields is zero, the indexed structures are pointers to
  19809. * the original, but should still be freed since the mem_conserve flag is set
  19810. * so that the structures are not freed.
  19811. * MAT File version must be 5.
  19812. * @ingroup MAT
  19813. * @param matvar Structure matlab variable
  19814. * @param start starting index (0-relative)
  19815. * @param stride stride (1 reads consecutive elements)
  19816. * @param edge Number of elements to read
  19817. * @param copy_fields 1 to copy the fields, 0 to just set pointers to them.
  19818. * @returns A new structure with fields indexed from matvar
  19819. */
  19820. matvar_t *
  19821. Mat_VarGetStructsLinear(matvar_t *matvar,int start,int stride,int edge,
  19822. int copy_fields)
  19823. {
  19824. int i, I, field, nfields;
  19825. matvar_t *struct_slab, **fields;
  19826. /* FIXME: Check allocations */
  19827. if ( matvar == NULL || matvar->rank > 10 ) {
  19828. struct_slab = NULL;
  19829. } else {
  19830. struct_slab = Mat_VarDuplicate(matvar,0);
  19831. if ( !copy_fields )
  19832. struct_slab->mem_conserve = 1;
  19833. nfields = matvar->internal->num_fields;
  19834. struct_slab->nbytes = edge*nfields*sizeof(matvar_t *);
  19835. struct_slab->data = malloc(struct_slab->nbytes);
  19836. struct_slab->dims[0] = edge;
  19837. struct_slab->dims[1] = 1;
  19838. fields = (matvar_t**)struct_slab->data;
  19839. I = start*nfields;
  19840. for ( i = 0; i < edge; i++ ) {
  19841. if ( copy_fields ) {
  19842. for ( field = 0; field < nfields; field++ ) {
  19843. fields[i*nfields+field] =
  19844. Mat_VarDuplicate(*((matvar_t **)matvar->data+I),1);
  19845. I++;
  19846. }
  19847. } else {
  19848. for ( field = 0; field < nfields; field++ ) {
  19849. fields[i*nfields+field] = *((matvar_t **)matvar->data + I);
  19850. I++;
  19851. }
  19852. }
  19853. I += (stride-1)*nfields;
  19854. }
  19855. }
  19856. return struct_slab;
  19857. }
  19858. /** @brief Sets the structure field to the given variable
  19859. *
  19860. * Sets the structure field specified by the 0-relative field index
  19861. * @c field_index for the given 0-relative structure index @c index to
  19862. * @c field.
  19863. * @ingroup MAT
  19864. * @param matvar Pointer to the structure MAT variable
  19865. * @param field_index 0-relative index of the field.
  19866. * @param index linear index of the structure array
  19867. * @param field New field variable
  19868. * @return Pointer to the previous field (NULL if no previous field)
  19869. */
  19870. matvar_t *
  19871. Mat_VarSetStructFieldByIndex(matvar_t *matvar,size_t field_index,size_t index,
  19872. matvar_t *field)
  19873. {
  19874. int i, nfields;
  19875. matvar_t *old_field = NULL;
  19876. size_t nmemb;
  19877. if ( matvar == NULL || matvar->class_type != MAT_C_STRUCT ||
  19878. matvar->data == NULL )
  19879. return old_field;
  19880. nmemb = 1;
  19881. for ( i = 0; i < matvar->rank; i++ )
  19882. nmemb *= matvar->dims[i];
  19883. nfields = matvar->internal->num_fields;
  19884. if ( index < nmemb && field_index < nfields ) {
  19885. matvar_t **fields = (matvar_t**)matvar->data;
  19886. old_field = fields[index*nfields+field_index];
  19887. fields[index*nfields+field_index] = field;
  19888. if ( NULL != field->name ) {
  19889. free(field->name);
  19890. }
  19891. field->name = mat_strdup(matvar->internal->fieldnames[field_index]);
  19892. }
  19893. return old_field;
  19894. }
  19895. /** @brief Sets the structure field to the given variable
  19896. *
  19897. * Sets the specified structure fieldname at the given 0-relative @c index to
  19898. * @c field.
  19899. * @ingroup MAT
  19900. * @param matvar Pointer to the Structure MAT variable
  19901. * @param field_name Name of the structure field
  19902. * @param index linear index of the structure array
  19903. * @param field New field variable
  19904. * @return Pointer to the previous field (NULL if no previous field)
  19905. */
  19906. matvar_t *
  19907. Mat_VarSetStructFieldByName(matvar_t *matvar,const char *field_name,
  19908. size_t index,matvar_t *field)
  19909. {
  19910. int i, nfields, field_index;
  19911. matvar_t *old_field = NULL;
  19912. size_t nmemb;
  19913. if ( matvar == NULL || matvar->class_type != MAT_C_STRUCT ||
  19914. matvar->data == NULL )
  19915. return old_field;
  19916. nmemb = 1;
  19917. for ( i = 0; i < matvar->rank; i++ )
  19918. nmemb *= matvar->dims[i];
  19919. nfields = matvar->internal->num_fields;
  19920. field_index = -1;
  19921. for ( i = 0; i < nfields; i++ ) {
  19922. if ( !strcmp(matvar->internal->fieldnames[i],field_name) ) {
  19923. field_index = i;
  19924. break;
  19925. }
  19926. }
  19927. if ( index < nmemb && field_index >= 0 ) {
  19928. matvar_t **fields = (matvar_t**)matvar->data;
  19929. old_field = fields[index*nfields+field_index];
  19930. fields[index*nfields+field_index] = field;
  19931. if ( NULL != field->name ) {
  19932. free(field->name);
  19933. }
  19934. field->name = mat_strdup(matvar->internal->fieldnames[field_index]);
  19935. }
  19936. return old_field;
  19937. }
  19938. #endif /* NO_FILE_SYSTEM */