pngrutil.c 153 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976
  1. /* pngrutil.c - utilities to read a PNG file
  2. *
  3. * Copyright (c) 2018-2022 Cosmin Truta
  4. * Copyright (c) 1998-2002,2004,2006-2018 Glenn Randers-Pehrson
  5. * Copyright (c) 1996-1997 Andreas Dilger
  6. * Copyright (c) 1995-1996 Guy Eric Schalnat, Group 42, Inc.
  7. *
  8. * This code is released under the libpng license.
  9. * For conditions of distribution and use, see the disclaimer
  10. * and license in png.h
  11. *
  12. * This file contains routines that are only called from within
  13. * libpng itself during the course of reading an image.
  14. */
  15. #include "pngpriv.h"
  16. #ifdef PNG_READ_SUPPORTED
  17. png_uint_32 PNGAPI
  18. png_get_uint_31(png_const_structrp png_ptr, png_const_bytep buf)
  19. {
  20. png_uint_32 uval = png_get_uint_32(buf);
  21. if (uval > PNG_UINT_31_MAX)
  22. png_error(png_ptr, "PNG unsigned integer out of range");
  23. return (uval);
  24. }
  25. #if defined(PNG_READ_gAMA_SUPPORTED) || defined(PNG_READ_cHRM_SUPPORTED)
  26. /* The following is a variation on the above for use with the fixed
  27. * point values used for gAMA and cHRM. Instead of png_error it
  28. * issues a warning and returns (-1) - an invalid value because both
  29. * gAMA and cHRM use *unsigned* integers for fixed point values.
  30. */
  31. #define PNG_FIXED_ERROR (-1)
  32. static png_fixed_point /* PRIVATE */
  33. png_get_fixed_point(png_structrp png_ptr, png_const_bytep buf)
  34. {
  35. png_uint_32 uval = png_get_uint_32(buf);
  36. if (uval <= PNG_UINT_31_MAX)
  37. return (png_fixed_point)uval; /* known to be in range */
  38. /* The caller can turn off the warning by passing NULL. */
  39. if (png_ptr != NULL)
  40. png_warning(png_ptr, "PNG fixed point integer out of range");
  41. return PNG_FIXED_ERROR;
  42. }
  43. #endif
  44. #ifdef PNG_READ_INT_FUNCTIONS_SUPPORTED
  45. /* NOTE: the read macros will obscure these definitions, so that if
  46. * PNG_USE_READ_MACROS is set the library will not use them internally,
  47. * but the APIs will still be available externally.
  48. *
  49. * The parentheses around "PNGAPI function_name" in the following three
  50. * functions are necessary because they allow the macros to co-exist with
  51. * these (unused but exported) functions.
  52. */
  53. /* Grab an unsigned 32-bit integer from a buffer in big-endian format. */
  54. png_uint_32 (PNGAPI
  55. png_get_uint_32)(png_const_bytep buf)
  56. {
  57. png_uint_32 uval =
  58. ((png_uint_32)(*(buf )) << 24) +
  59. ((png_uint_32)(*(buf + 1)) << 16) +
  60. ((png_uint_32)(*(buf + 2)) << 8) +
  61. ((png_uint_32)(*(buf + 3)) ) ;
  62. return uval;
  63. }
  64. /* Grab a signed 32-bit integer from a buffer in big-endian format. The
  65. * data is stored in the PNG file in two's complement format and there
  66. * is no guarantee that a 'png_int_32' is exactly 32 bits, therefore
  67. * the following code does a two's complement to native conversion.
  68. */
  69. png_int_32 (PNGAPI
  70. png_get_int_32)(png_const_bytep buf)
  71. {
  72. png_uint_32 uval = png_get_uint_32(buf);
  73. if ((uval & 0x80000000) == 0) /* non-negative */
  74. return (png_int_32)uval;
  75. uval = (uval ^ 0xffffffff) + 1; /* 2's complement: -x = ~x+1 */
  76. if ((uval & 0x80000000) == 0) /* no overflow */
  77. return -(png_int_32)uval;
  78. /* The following has to be safe; this function only gets called on PNG data
  79. * and if we get here that data is invalid. 0 is the most safe value and
  80. * if not then an attacker would surely just generate a PNG with 0 instead.
  81. */
  82. return 0;
  83. }
  84. /* Grab an unsigned 16-bit integer from a buffer in big-endian format. */
  85. png_uint_16 (PNGAPI
  86. png_get_uint_16)(png_const_bytep buf)
  87. {
  88. /* ANSI-C requires an int value to accommodate at least 16 bits so this
  89. * works and allows the compiler not to worry about possible narrowing
  90. * on 32-bit systems. (Pre-ANSI systems did not make integers smaller
  91. * than 16 bits either.)
  92. */
  93. unsigned int val =
  94. ((unsigned int)(*buf) << 8) +
  95. ((unsigned int)(*(buf + 1)));
  96. return (png_uint_16)val;
  97. }
  98. #endif /* READ_INT_FUNCTIONS */
  99. /* Read and check the PNG file signature */
  100. void /* PRIVATE */
  101. png_read_sig(png_structrp png_ptr, png_inforp info_ptr)
  102. {
  103. size_t num_checked, num_to_check;
  104. /* Exit if the user application does not expect a signature. */
  105. if (png_ptr->sig_bytes >= 8)
  106. return;
  107. num_checked = png_ptr->sig_bytes;
  108. num_to_check = 8 - num_checked;
  109. #ifdef PNG_IO_STATE_SUPPORTED
  110. png_ptr->io_state = PNG_IO_READING | PNG_IO_SIGNATURE;
  111. #endif
  112. /* The signature must be serialized in a single I/O call. */
  113. png_read_data(png_ptr, &(info_ptr->signature[num_checked]), num_to_check);
  114. png_ptr->sig_bytes = 8;
  115. if (png_sig_cmp(info_ptr->signature, num_checked, num_to_check) != 0)
  116. {
  117. if (num_checked < 4 &&
  118. png_sig_cmp(info_ptr->signature, num_checked, num_to_check - 4))
  119. png_error(png_ptr, "Not a PNG file");
  120. else
  121. png_error(png_ptr, "PNG file corrupted by ASCII conversion");
  122. }
  123. if (num_checked < 3)
  124. png_ptr->mode |= PNG_HAVE_PNG_SIGNATURE;
  125. }
  126. /* Read the chunk header (length + type name).
  127. * Put the type name into png_ptr->chunk_name, and return the length.
  128. */
  129. png_uint_32 /* PRIVATE */
  130. png_read_chunk_header(png_structrp png_ptr)
  131. {
  132. png_byte buf[8];
  133. png_uint_32 length;
  134. #ifdef PNG_IO_STATE_SUPPORTED
  135. png_ptr->io_state = PNG_IO_READING | PNG_IO_CHUNK_HDR;
  136. #endif
  137. /* Read the length and the chunk name.
  138. * This must be performed in a single I/O call.
  139. */
  140. png_read_data(png_ptr, buf, 8);
  141. length = png_get_uint_31(png_ptr, buf);
  142. /* Put the chunk name into png_ptr->chunk_name. */
  143. png_ptr->chunk_name = PNG_CHUNK_FROM_STRING(buf+4);
  144. png_debug2(0, "Reading %lx chunk, length = %lu",
  145. (unsigned long)png_ptr->chunk_name, (unsigned long)length);
  146. /* Reset the crc and run it over the chunk name. */
  147. png_reset_crc(png_ptr);
  148. png_calculate_crc(png_ptr, buf + 4, 4);
  149. /* Check to see if chunk name is valid. */
  150. png_check_chunk_name(png_ptr, png_ptr->chunk_name);
  151. /* Check for too-large chunk length */
  152. png_check_chunk_length(png_ptr, length);
  153. #ifdef PNG_IO_STATE_SUPPORTED
  154. png_ptr->io_state = PNG_IO_READING | PNG_IO_CHUNK_DATA;
  155. #endif
  156. return length;
  157. }
  158. /* Read data, and (optionally) run it through the CRC. */
  159. void /* PRIVATE */
  160. png_crc_read(png_structrp png_ptr, png_bytep buf, png_uint_32 length)
  161. {
  162. if (png_ptr == NULL)
  163. return;
  164. png_read_data(png_ptr, buf, length);
  165. png_calculate_crc(png_ptr, buf, length);
  166. }
  167. /* Optionally skip data and then check the CRC. Depending on whether we
  168. * are reading an ancillary or critical chunk, and how the program has set
  169. * things up, we may calculate the CRC on the data and print a message.
  170. * Returns '1' if there was a CRC error, '0' otherwise.
  171. */
  172. int /* PRIVATE */
  173. png_crc_finish(png_structrp png_ptr, png_uint_32 skip)
  174. {
  175. /* The size of the local buffer for inflate is a good guess as to a
  176. * reasonable size to use for buffering reads from the application.
  177. */
  178. while (skip > 0)
  179. {
  180. png_uint_32 len;
  181. png_byte tmpbuf[PNG_INFLATE_BUF_SIZE];
  182. len = (sizeof tmpbuf);
  183. if (len > skip)
  184. len = skip;
  185. skip -= len;
  186. png_crc_read(png_ptr, tmpbuf, len);
  187. }
  188. if (png_crc_error(png_ptr) != 0)
  189. {
  190. if (PNG_CHUNK_ANCILLARY(png_ptr->chunk_name) != 0 ?
  191. (png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN) == 0 :
  192. (png_ptr->flags & PNG_FLAG_CRC_CRITICAL_USE) != 0)
  193. {
  194. png_chunk_warning(png_ptr, "CRC error");
  195. }
  196. else
  197. png_chunk_error(png_ptr, "CRC error");
  198. return (1);
  199. }
  200. return (0);
  201. }
  202. /* Compare the CRC stored in the PNG file with that calculated by libpng from
  203. * the data it has read thus far.
  204. */
  205. int /* PRIVATE */
  206. png_crc_error(png_structrp png_ptr)
  207. {
  208. png_byte crc_bytes[4];
  209. png_uint_32 crc;
  210. int need_crc = 1;
  211. if (PNG_CHUNK_ANCILLARY(png_ptr->chunk_name) != 0)
  212. {
  213. if ((png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_MASK) ==
  214. (PNG_FLAG_CRC_ANCILLARY_USE | PNG_FLAG_CRC_ANCILLARY_NOWARN))
  215. need_crc = 0;
  216. }
  217. else /* critical */
  218. {
  219. if ((png_ptr->flags & PNG_FLAG_CRC_CRITICAL_IGNORE) != 0)
  220. need_crc = 0;
  221. }
  222. #ifdef PNG_IO_STATE_SUPPORTED
  223. png_ptr->io_state = PNG_IO_READING | PNG_IO_CHUNK_CRC;
  224. #endif
  225. /* The chunk CRC must be serialized in a single I/O call. */
  226. png_read_data(png_ptr, crc_bytes, 4);
  227. if (need_crc != 0)
  228. {
  229. crc = png_get_uint_32(crc_bytes);
  230. return ((int)(crc != png_ptr->crc));
  231. }
  232. else
  233. return (0);
  234. }
  235. #if defined(PNG_READ_iCCP_SUPPORTED) || defined(PNG_READ_iTXt_SUPPORTED) ||\
  236. defined(PNG_READ_pCAL_SUPPORTED) || defined(PNG_READ_sCAL_SUPPORTED) ||\
  237. defined(PNG_READ_sPLT_SUPPORTED) || defined(PNG_READ_tEXt_SUPPORTED) ||\
  238. defined(PNG_READ_zTXt_SUPPORTED) || defined(PNG_SEQUENTIAL_READ_SUPPORTED)
  239. /* Manage the read buffer; this simply reallocates the buffer if it is not small
  240. * enough (or if it is not allocated). The routine returns a pointer to the
  241. * buffer; if an error occurs and 'warn' is set the routine returns NULL, else
  242. * it will call png_error (via png_malloc) on failure. (warn == 2 means
  243. * 'silent').
  244. */
  245. static png_bytep
  246. png_read_buffer(png_structrp png_ptr, png_alloc_size_t new_size, int warn)
  247. {
  248. png_bytep buffer = png_ptr->read_buffer;
  249. if (buffer != NULL && new_size > png_ptr->read_buffer_size)
  250. {
  251. png_ptr->read_buffer = NULL;
  252. png_ptr->read_buffer_size = 0;
  253. png_free(png_ptr, buffer);
  254. buffer = NULL;
  255. }
  256. if (buffer == NULL)
  257. {
  258. buffer = png_voidcast(png_bytep, png_malloc_base(png_ptr, new_size));
  259. if (buffer != NULL)
  260. {
  261. memset(buffer, 0, new_size); /* just in case */
  262. png_ptr->read_buffer = buffer;
  263. png_ptr->read_buffer_size = new_size;
  264. }
  265. else if (warn < 2) /* else silent */
  266. {
  267. if (warn != 0)
  268. png_chunk_warning(png_ptr, "insufficient memory to read chunk");
  269. else
  270. png_chunk_error(png_ptr, "insufficient memory to read chunk");
  271. }
  272. }
  273. return buffer;
  274. }
  275. #endif /* READ_iCCP|iTXt|pCAL|sCAL|sPLT|tEXt|zTXt|SEQUENTIAL_READ */
  276. /* png_inflate_claim: claim the zstream for some nefarious purpose that involves
  277. * decompression. Returns Z_OK on success, else a zlib error code. It checks
  278. * the owner but, in final release builds, just issues a warning if some other
  279. * chunk apparently owns the stream. Prior to release it does a png_error.
  280. */
  281. static int
  282. png_inflate_claim(png_structrp png_ptr, png_uint_32 owner)
  283. {
  284. if (png_ptr->zowner != 0)
  285. {
  286. char msg[64];
  287. PNG_STRING_FROM_CHUNK(msg, png_ptr->zowner);
  288. /* So the message that results is "<chunk> using zstream"; this is an
  289. * internal error, but is very useful for debugging. i18n requirements
  290. * are minimal.
  291. */
  292. (void)png_safecat(msg, (sizeof msg), 4, " using zstream");
  293. #if PNG_RELEASE_BUILD
  294. png_chunk_warning(png_ptr, msg);
  295. png_ptr->zowner = 0;
  296. #else
  297. png_chunk_error(png_ptr, msg);
  298. #endif
  299. }
  300. /* Implementation note: unlike 'png_deflate_claim' this internal function
  301. * does not take the size of the data as an argument. Some efficiency could
  302. * be gained by using this when it is known *if* the zlib stream itself does
  303. * not record the number; however, this is an illusion: the original writer
  304. * of the PNG may have selected a lower window size, and we really must
  305. * follow that because, for systems with with limited capabilities, we
  306. * would otherwise reject the application's attempts to use a smaller window
  307. * size (zlib doesn't have an interface to say "this or lower"!).
  308. *
  309. * inflateReset2 was added to zlib 1.2.4; before this the window could not be
  310. * reset, therefore it is necessary to always allocate the maximum window
  311. * size with earlier zlibs just in case later compressed chunks need it.
  312. */
  313. {
  314. int ret; /* zlib return code */
  315. #if ZLIB_VERNUM >= 0x1240
  316. int window_bits = 0;
  317. # if defined(PNG_SET_OPTION_SUPPORTED) && defined(PNG_MAXIMUM_INFLATE_WINDOW)
  318. if (((png_ptr->options >> PNG_MAXIMUM_INFLATE_WINDOW) & 3) ==
  319. PNG_OPTION_ON)
  320. {
  321. window_bits = 15;
  322. png_ptr->zstream_start = 0; /* fixed window size */
  323. }
  324. else
  325. {
  326. png_ptr->zstream_start = 1;
  327. }
  328. # endif
  329. #endif /* ZLIB_VERNUM >= 0x1240 */
  330. /* Set this for safety, just in case the previous owner left pointers to
  331. * memory allocations.
  332. */
  333. png_ptr->zstream.next_in = NULL;
  334. png_ptr->zstream.avail_in = 0;
  335. png_ptr->zstream.next_out = NULL;
  336. png_ptr->zstream.avail_out = 0;
  337. if ((png_ptr->flags & PNG_FLAG_ZSTREAM_INITIALIZED) != 0)
  338. {
  339. #if ZLIB_VERNUM >= 0x1240
  340. ret = inflateReset2(&png_ptr->zstream, window_bits);
  341. #else
  342. ret = inflateReset(&png_ptr->zstream);
  343. #endif
  344. }
  345. else
  346. {
  347. #if ZLIB_VERNUM >= 0x1240
  348. ret = inflateInit2(&png_ptr->zstream, window_bits);
  349. #else
  350. ret = inflateInit(&png_ptr->zstream);
  351. #endif
  352. if (ret == Z_OK)
  353. png_ptr->flags |= PNG_FLAG_ZSTREAM_INITIALIZED;
  354. }
  355. #if ZLIB_VERNUM >= 0x1290 && \
  356. defined(PNG_SET_OPTION_SUPPORTED) && defined(PNG_IGNORE_ADLER32)
  357. if (((png_ptr->options >> PNG_IGNORE_ADLER32) & 3) == PNG_OPTION_ON)
  358. /* Turn off validation of the ADLER32 checksum in IDAT chunks */
  359. ret = inflateValidate(&png_ptr->zstream, 0);
  360. #endif
  361. if (ret == Z_OK)
  362. png_ptr->zowner = owner;
  363. else
  364. png_zstream_error(png_ptr, ret);
  365. return ret;
  366. }
  367. #ifdef window_bits
  368. # undef window_bits
  369. #endif
  370. }
  371. #if ZLIB_VERNUM >= 0x1240
  372. /* Handle the start of the inflate stream if we called inflateInit2(strm,0);
  373. * in this case some zlib versions skip validation of the CINFO field and, in
  374. * certain circumstances, libpng may end up displaying an invalid image, in
  375. * contrast to implementations that call zlib in the normal way (e.g. libpng
  376. * 1.5).
  377. */
  378. int /* PRIVATE */
  379. png_zlib_inflate(png_structrp png_ptr, int flush)
  380. {
  381. if (png_ptr->zstream_start && png_ptr->zstream.avail_in > 0)
  382. {
  383. if ((*png_ptr->zstream.next_in >> 4) > 7)
  384. {
  385. png_ptr->zstream.msg = "invalid window size (libpng)";
  386. return Z_DATA_ERROR;
  387. }
  388. png_ptr->zstream_start = 0;
  389. }
  390. return inflate(&png_ptr->zstream, flush);
  391. }
  392. #endif /* Zlib >= 1.2.4 */
  393. #ifdef PNG_READ_COMPRESSED_TEXT_SUPPORTED
  394. #if defined(PNG_READ_zTXt_SUPPORTED) || defined (PNG_READ_iTXt_SUPPORTED)
  395. /* png_inflate now returns zlib error codes including Z_OK and Z_STREAM_END to
  396. * allow the caller to do multiple calls if required. If the 'finish' flag is
  397. * set Z_FINISH will be passed to the final inflate() call and Z_STREAM_END must
  398. * be returned or there has been a problem, otherwise Z_SYNC_FLUSH is used and
  399. * Z_OK or Z_STREAM_END will be returned on success.
  400. *
  401. * The input and output sizes are updated to the actual amounts of data consumed
  402. * or written, not the amount available (as in a z_stream). The data pointers
  403. * are not changed, so the next input is (data+input_size) and the next
  404. * available output is (output+output_size).
  405. */
  406. static int
  407. png_inflate(png_structrp png_ptr, png_uint_32 owner, int finish,
  408. /* INPUT: */ png_const_bytep input, png_uint_32p input_size_ptr,
  409. /* OUTPUT: */ png_bytep output, png_alloc_size_t *output_size_ptr)
  410. {
  411. if (png_ptr->zowner == owner) /* Else not claimed */
  412. {
  413. int ret;
  414. png_alloc_size_t avail_out = *output_size_ptr;
  415. png_uint_32 avail_in = *input_size_ptr;
  416. /* zlib can't necessarily handle more than 65535 bytes at once (i.e. it
  417. * can't even necessarily handle 65536 bytes) because the type uInt is
  418. * "16 bits or more". Consequently it is necessary to chunk the input to
  419. * zlib. This code uses ZLIB_IO_MAX, from pngpriv.h, as the maximum (the
  420. * maximum value that can be stored in a uInt.) It is possible to set
  421. * ZLIB_IO_MAX to a lower value in pngpriv.h and this may sometimes have
  422. * a performance advantage, because it reduces the amount of data accessed
  423. * at each step and that may give the OS more time to page it in.
  424. */
  425. png_ptr->zstream.next_in = PNGZ_INPUT_CAST(input);
  426. /* avail_in and avail_out are set below from 'size' */
  427. png_ptr->zstream.avail_in = 0;
  428. png_ptr->zstream.avail_out = 0;
  429. /* Read directly into the output if it is available (this is set to
  430. * a local buffer below if output is NULL).
  431. */
  432. if (output != NULL)
  433. png_ptr->zstream.next_out = output;
  434. do
  435. {
  436. uInt avail;
  437. Byte local_buffer[PNG_INFLATE_BUF_SIZE];
  438. /* zlib INPUT BUFFER */
  439. /* The setting of 'avail_in' used to be outside the loop; by setting it
  440. * inside it is possible to chunk the input to zlib and simply rely on
  441. * zlib to advance the 'next_in' pointer. This allows arbitrary
  442. * amounts of data to be passed through zlib at the unavoidable cost of
  443. * requiring a window save (memcpy of up to 32768 output bytes)
  444. * every ZLIB_IO_MAX input bytes.
  445. */
  446. avail_in += png_ptr->zstream.avail_in; /* not consumed last time */
  447. avail = ZLIB_IO_MAX;
  448. if (avail_in < avail)
  449. avail = (uInt)avail_in; /* safe: < than ZLIB_IO_MAX */
  450. avail_in -= avail;
  451. png_ptr->zstream.avail_in = avail;
  452. /* zlib OUTPUT BUFFER */
  453. avail_out += png_ptr->zstream.avail_out; /* not written last time */
  454. avail = ZLIB_IO_MAX; /* maximum zlib can process */
  455. if (output == NULL)
  456. {
  457. /* Reset the output buffer each time round if output is NULL and
  458. * make available the full buffer, up to 'remaining_space'
  459. */
  460. png_ptr->zstream.next_out = local_buffer;
  461. if ((sizeof local_buffer) < avail)
  462. avail = (sizeof local_buffer);
  463. }
  464. if (avail_out < avail)
  465. avail = (uInt)avail_out; /* safe: < ZLIB_IO_MAX */
  466. png_ptr->zstream.avail_out = avail;
  467. avail_out -= avail;
  468. /* zlib inflate call */
  469. /* In fact 'avail_out' may be 0 at this point, that happens at the end
  470. * of the read when the final LZ end code was not passed at the end of
  471. * the previous chunk of input data. Tell zlib if we have reached the
  472. * end of the output buffer.
  473. */
  474. ret = PNG_INFLATE(png_ptr, avail_out > 0 ? Z_NO_FLUSH :
  475. (finish ? Z_FINISH : Z_SYNC_FLUSH));
  476. } while (ret == Z_OK);
  477. /* For safety kill the local buffer pointer now */
  478. if (output == NULL)
  479. png_ptr->zstream.next_out = NULL;
  480. /* Claw back the 'size' and 'remaining_space' byte counts. */
  481. avail_in += png_ptr->zstream.avail_in;
  482. avail_out += png_ptr->zstream.avail_out;
  483. /* Update the input and output sizes; the updated values are the amount
  484. * consumed or written, effectively the inverse of what zlib uses.
  485. */
  486. if (avail_out > 0)
  487. *output_size_ptr -= avail_out;
  488. if (avail_in > 0)
  489. *input_size_ptr -= avail_in;
  490. /* Ensure png_ptr->zstream.msg is set (even in the success case!) */
  491. png_zstream_error(png_ptr, ret);
  492. return ret;
  493. }
  494. else
  495. {
  496. /* This is a bad internal error. The recovery assigns to the zstream msg
  497. * pointer, which is not owned by the caller, but this is safe; it's only
  498. * used on errors!
  499. */
  500. png_ptr->zstream.msg = PNGZ_MSG_CAST("zstream unclaimed");
  501. return Z_STREAM_ERROR;
  502. }
  503. }
  504. /*
  505. * Decompress trailing data in a chunk. The assumption is that read_buffer
  506. * points at an allocated area holding the contents of a chunk with a
  507. * trailing compressed part. What we get back is an allocated area
  508. * holding the original prefix part and an uncompressed version of the
  509. * trailing part (the malloc area passed in is freed).
  510. */
  511. static int
  512. png_decompress_chunk(png_structrp png_ptr,
  513. png_uint_32 chunklength, png_uint_32 prefix_size,
  514. png_alloc_size_t *newlength /* must be initialized to the maximum! */,
  515. int terminate /*add a '\0' to the end of the uncompressed data*/)
  516. {
  517. /* TODO: implement different limits for different types of chunk.
  518. *
  519. * The caller supplies *newlength set to the maximum length of the
  520. * uncompressed data, but this routine allocates space for the prefix and
  521. * maybe a '\0' terminator too. We have to assume that 'prefix_size' is
  522. * limited only by the maximum chunk size.
  523. */
  524. png_alloc_size_t limit = PNG_SIZE_MAX;
  525. # ifdef PNG_SET_USER_LIMITS_SUPPORTED
  526. if (png_ptr->user_chunk_malloc_max > 0 &&
  527. png_ptr->user_chunk_malloc_max < limit)
  528. limit = png_ptr->user_chunk_malloc_max;
  529. # elif PNG_USER_CHUNK_MALLOC_MAX > 0
  530. if (PNG_USER_CHUNK_MALLOC_MAX < limit)
  531. limit = PNG_USER_CHUNK_MALLOC_MAX;
  532. # endif
  533. if (limit >= prefix_size + (terminate != 0))
  534. {
  535. int ret;
  536. limit -= prefix_size + (terminate != 0);
  537. if (limit < *newlength)
  538. *newlength = limit;
  539. /* Now try to claim the stream. */
  540. ret = png_inflate_claim(png_ptr, png_ptr->chunk_name);
  541. if (ret == Z_OK)
  542. {
  543. png_uint_32 lzsize = chunklength - prefix_size;
  544. ret = png_inflate(png_ptr, png_ptr->chunk_name, 1/*finish*/,
  545. /* input: */ png_ptr->read_buffer + prefix_size, &lzsize,
  546. /* output: */ NULL, newlength);
  547. if (ret == Z_STREAM_END)
  548. {
  549. /* Use 'inflateReset' here, not 'inflateReset2' because this
  550. * preserves the previously decided window size (otherwise it would
  551. * be necessary to store the previous window size.) In practice
  552. * this doesn't matter anyway, because png_inflate will call inflate
  553. * with Z_FINISH in almost all cases, so the window will not be
  554. * maintained.
  555. */
  556. if (inflateReset(&png_ptr->zstream) == Z_OK)
  557. {
  558. /* Because of the limit checks above we know that the new,
  559. * expanded, size will fit in a size_t (let alone an
  560. * png_alloc_size_t). Use png_malloc_base here to avoid an
  561. * extra OOM message.
  562. */
  563. png_alloc_size_t new_size = *newlength;
  564. png_alloc_size_t buffer_size = prefix_size + new_size +
  565. (terminate != 0);
  566. png_bytep text = png_voidcast(png_bytep, png_malloc_base(png_ptr,
  567. buffer_size));
  568. if (text != NULL)
  569. {
  570. memset(text, 0, buffer_size);
  571. ret = png_inflate(png_ptr, png_ptr->chunk_name, 1/*finish*/,
  572. png_ptr->read_buffer + prefix_size, &lzsize,
  573. text + prefix_size, newlength);
  574. if (ret == Z_STREAM_END)
  575. {
  576. if (new_size == *newlength)
  577. {
  578. if (terminate != 0)
  579. text[prefix_size + *newlength] = 0;
  580. if (prefix_size > 0)
  581. memcpy(text, png_ptr->read_buffer, prefix_size);
  582. {
  583. png_bytep old_ptr = png_ptr->read_buffer;
  584. png_ptr->read_buffer = text;
  585. png_ptr->read_buffer_size = buffer_size;
  586. text = old_ptr; /* freed below */
  587. }
  588. }
  589. else
  590. {
  591. /* The size changed on the second read, there can be no
  592. * guarantee that anything is correct at this point.
  593. * The 'msg' pointer has been set to "unexpected end of
  594. * LZ stream", which is fine, but return an error code
  595. * that the caller won't accept.
  596. */
  597. ret = PNG_UNEXPECTED_ZLIB_RETURN;
  598. }
  599. }
  600. else if (ret == Z_OK)
  601. ret = PNG_UNEXPECTED_ZLIB_RETURN; /* for safety */
  602. /* Free the text pointer (this is the old read_buffer on
  603. * success)
  604. */
  605. png_free(png_ptr, text);
  606. /* This really is very benign, but it's still an error because
  607. * the extra space may otherwise be used as a Trojan Horse.
  608. */
  609. if (ret == Z_STREAM_END &&
  610. chunklength - prefix_size != lzsize)
  611. png_chunk_benign_error(png_ptr, "extra compressed data");
  612. }
  613. else
  614. {
  615. /* Out of memory allocating the buffer */
  616. ret = Z_MEM_ERROR;
  617. png_zstream_error(png_ptr, Z_MEM_ERROR);
  618. }
  619. }
  620. else
  621. {
  622. /* inflateReset failed, store the error message */
  623. png_zstream_error(png_ptr, ret);
  624. ret = PNG_UNEXPECTED_ZLIB_RETURN;
  625. }
  626. }
  627. else if (ret == Z_OK)
  628. ret = PNG_UNEXPECTED_ZLIB_RETURN;
  629. /* Release the claimed stream */
  630. png_ptr->zowner = 0;
  631. }
  632. else /* the claim failed */ if (ret == Z_STREAM_END) /* impossible! */
  633. ret = PNG_UNEXPECTED_ZLIB_RETURN;
  634. return ret;
  635. }
  636. else
  637. {
  638. /* Application/configuration limits exceeded */
  639. png_zstream_error(png_ptr, Z_MEM_ERROR);
  640. return Z_MEM_ERROR;
  641. }
  642. }
  643. #endif /* READ_zTXt || READ_iTXt */
  644. #endif /* READ_COMPRESSED_TEXT */
  645. #ifdef PNG_READ_iCCP_SUPPORTED
  646. /* Perform a partial read and decompress, producing 'avail_out' bytes and
  647. * reading from the current chunk as required.
  648. */
  649. static int
  650. png_inflate_read(png_structrp png_ptr, png_bytep read_buffer, uInt read_size,
  651. png_uint_32p chunk_bytes, png_bytep next_out, png_alloc_size_t *out_size,
  652. int finish)
  653. {
  654. if (png_ptr->zowner == png_ptr->chunk_name)
  655. {
  656. int ret;
  657. /* next_in and avail_in must have been initialized by the caller. */
  658. png_ptr->zstream.next_out = next_out;
  659. png_ptr->zstream.avail_out = 0; /* set in the loop */
  660. do
  661. {
  662. if (png_ptr->zstream.avail_in == 0)
  663. {
  664. if (read_size > *chunk_bytes)
  665. read_size = (uInt)*chunk_bytes;
  666. *chunk_bytes -= read_size;
  667. if (read_size > 0)
  668. png_crc_read(png_ptr, read_buffer, read_size);
  669. png_ptr->zstream.next_in = read_buffer;
  670. png_ptr->zstream.avail_in = read_size;
  671. }
  672. if (png_ptr->zstream.avail_out == 0)
  673. {
  674. uInt avail = ZLIB_IO_MAX;
  675. if (avail > *out_size)
  676. avail = (uInt)*out_size;
  677. *out_size -= avail;
  678. png_ptr->zstream.avail_out = avail;
  679. }
  680. /* Use Z_SYNC_FLUSH when there is no more chunk data to ensure that all
  681. * the available output is produced; this allows reading of truncated
  682. * streams.
  683. */
  684. ret = PNG_INFLATE(png_ptr, *chunk_bytes > 0 ?
  685. Z_NO_FLUSH : (finish ? Z_FINISH : Z_SYNC_FLUSH));
  686. }
  687. while (ret == Z_OK && (*out_size > 0 || png_ptr->zstream.avail_out > 0));
  688. *out_size += png_ptr->zstream.avail_out;
  689. png_ptr->zstream.avail_out = 0; /* Should not be required, but is safe */
  690. /* Ensure the error message pointer is always set: */
  691. png_zstream_error(png_ptr, ret);
  692. return ret;
  693. }
  694. else
  695. {
  696. png_ptr->zstream.msg = PNGZ_MSG_CAST("zstream unclaimed");
  697. return Z_STREAM_ERROR;
  698. }
  699. }
  700. #endif /* READ_iCCP */
  701. /* Read and check the IDHR chunk */
  702. void /* PRIVATE */
  703. png_handle_IHDR(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
  704. {
  705. png_byte buf[13];
  706. png_uint_32 width, height;
  707. int bit_depth, color_type, compression_type, filter_type;
  708. int interlace_type;
  709. png_debug(1, "in png_handle_IHDR");
  710. if ((png_ptr->mode & PNG_HAVE_IHDR) != 0)
  711. png_chunk_error(png_ptr, "out of place");
  712. /* Check the length */
  713. if (length != 13)
  714. png_chunk_error(png_ptr, "invalid");
  715. png_ptr->mode |= PNG_HAVE_IHDR;
  716. png_crc_read(png_ptr, buf, 13);
  717. png_crc_finish(png_ptr, 0);
  718. width = png_get_uint_31(png_ptr, buf);
  719. height = png_get_uint_31(png_ptr, buf + 4);
  720. bit_depth = buf[8];
  721. color_type = buf[9];
  722. compression_type = buf[10];
  723. filter_type = buf[11];
  724. interlace_type = buf[12];
  725. #ifdef PNG_READ_APNG_SUPPORTED
  726. png_ptr->first_frame_width = width;
  727. png_ptr->first_frame_height = height;
  728. #endif
  729. /* Set internal variables */
  730. png_ptr->width = width;
  731. png_ptr->height = height;
  732. png_ptr->bit_depth = (png_byte)bit_depth;
  733. png_ptr->interlaced = (png_byte)interlace_type;
  734. png_ptr->color_type = (png_byte)color_type;
  735. #ifdef PNG_MNG_FEATURES_SUPPORTED
  736. png_ptr->filter_type = (png_byte)filter_type;
  737. #endif
  738. png_ptr->compression_type = (png_byte)compression_type;
  739. /* Find number of channels */
  740. switch (png_ptr->color_type)
  741. {
  742. default: /* invalid, png_set_IHDR calls png_error */
  743. case PNG_COLOR_TYPE_GRAY:
  744. case PNG_COLOR_TYPE_PALETTE:
  745. png_ptr->channels = 1;
  746. break;
  747. case PNG_COLOR_TYPE_RGB:
  748. png_ptr->channels = 3;
  749. break;
  750. case PNG_COLOR_TYPE_GRAY_ALPHA:
  751. png_ptr->channels = 2;
  752. break;
  753. case PNG_COLOR_TYPE_RGB_ALPHA:
  754. png_ptr->channels = 4;
  755. break;
  756. }
  757. /* Set up other useful info */
  758. png_ptr->pixel_depth = (png_byte)(png_ptr->bit_depth * png_ptr->channels);
  759. png_ptr->rowbytes = PNG_ROWBYTES(png_ptr->pixel_depth, png_ptr->width);
  760. png_debug1(3, "bit_depth = %d", png_ptr->bit_depth);
  761. png_debug1(3, "channels = %d", png_ptr->channels);
  762. png_debug1(3, "rowbytes = %lu", (unsigned long)png_ptr->rowbytes);
  763. png_set_IHDR(png_ptr, info_ptr, width, height, bit_depth,
  764. color_type, interlace_type, compression_type, filter_type);
  765. }
  766. /* Read and check the palette */
  767. void /* PRIVATE */
  768. png_handle_PLTE(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
  769. {
  770. png_color palette[PNG_MAX_PALETTE_LENGTH];
  771. int max_palette_length, num, i;
  772. #ifdef PNG_POINTER_INDEXING_SUPPORTED
  773. png_colorp pal_ptr;
  774. #endif
  775. png_debug(1, "in png_handle_PLTE");
  776. if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
  777. png_chunk_error(png_ptr, "missing IHDR");
  778. /* Moved to before the 'after IDAT' check below because otherwise duplicate
  779. * PLTE chunks are potentially ignored (the spec says there shall not be more
  780. * than one PLTE, the error is not treated as benign, so this check trumps
  781. * the requirement that PLTE appears before IDAT.)
  782. */
  783. else if ((png_ptr->mode & PNG_HAVE_PLTE) != 0)
  784. png_chunk_error(png_ptr, "duplicate");
  785. else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
  786. {
  787. /* This is benign because the non-benign error happened before, when an
  788. * IDAT was encountered in a color-mapped image with no PLTE.
  789. */
  790. png_crc_finish(png_ptr, length);
  791. png_chunk_benign_error(png_ptr, "out of place");
  792. return;
  793. }
  794. png_ptr->mode |= PNG_HAVE_PLTE;
  795. if ((png_ptr->color_type & PNG_COLOR_MASK_COLOR) == 0)
  796. {
  797. png_crc_finish(png_ptr, length);
  798. png_chunk_benign_error(png_ptr, "ignored in grayscale PNG");
  799. return;
  800. }
  801. #ifndef PNG_READ_OPT_PLTE_SUPPORTED
  802. if (png_ptr->color_type != PNG_COLOR_TYPE_PALETTE)
  803. {
  804. png_crc_finish(png_ptr, length);
  805. return;
  806. }
  807. #endif
  808. if (length > 3*PNG_MAX_PALETTE_LENGTH || length % 3)
  809. {
  810. png_crc_finish(png_ptr, length);
  811. if (png_ptr->color_type != PNG_COLOR_TYPE_PALETTE)
  812. png_chunk_benign_error(png_ptr, "invalid");
  813. else
  814. png_chunk_error(png_ptr, "invalid");
  815. return;
  816. }
  817. /* The cast is safe because 'length' is less than 3*PNG_MAX_PALETTE_LENGTH */
  818. num = (int)length / 3;
  819. /* If the palette has 256 or fewer entries but is too large for the bit
  820. * depth, we don't issue an error, to preserve the behavior of previous
  821. * libpng versions. We silently truncate the unused extra palette entries
  822. * here.
  823. */
  824. if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
  825. max_palette_length = (1 << png_ptr->bit_depth);
  826. else
  827. max_palette_length = PNG_MAX_PALETTE_LENGTH;
  828. if (num > max_palette_length)
  829. num = max_palette_length;
  830. #ifdef PNG_POINTER_INDEXING_SUPPORTED
  831. for (i = 0, pal_ptr = palette; i < num; i++, pal_ptr++)
  832. {
  833. png_byte buf[3];
  834. png_crc_read(png_ptr, buf, 3);
  835. pal_ptr->red = buf[0];
  836. pal_ptr->green = buf[1];
  837. pal_ptr->blue = buf[2];
  838. }
  839. #else
  840. for (i = 0; i < num; i++)
  841. {
  842. png_byte buf[3];
  843. png_crc_read(png_ptr, buf, 3);
  844. /* Don't depend upon png_color being any order */
  845. palette[i].red = buf[0];
  846. palette[i].green = buf[1];
  847. palette[i].blue = buf[2];
  848. }
  849. #endif
  850. /* If we actually need the PLTE chunk (ie for a paletted image), we do
  851. * whatever the normal CRC configuration tells us. However, if we
  852. * have an RGB image, the PLTE can be considered ancillary, so
  853. * we will act as though it is.
  854. */
  855. #ifndef PNG_READ_OPT_PLTE_SUPPORTED
  856. if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
  857. #endif
  858. {
  859. png_crc_finish(png_ptr, (png_uint_32) (length - (unsigned int)num * 3));
  860. }
  861. #ifndef PNG_READ_OPT_PLTE_SUPPORTED
  862. else if (png_crc_error(png_ptr) != 0) /* Only if we have a CRC error */
  863. {
  864. /* If we don't want to use the data from an ancillary chunk,
  865. * we have two options: an error abort, or a warning and we
  866. * ignore the data in this chunk (which should be OK, since
  867. * it's considered ancillary for a RGB or RGBA image).
  868. *
  869. * IMPLEMENTATION NOTE: this is only here because png_crc_finish uses the
  870. * chunk type to determine whether to check the ancillary or the critical
  871. * flags.
  872. */
  873. if ((png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_USE) == 0)
  874. {
  875. if ((png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN) != 0)
  876. return;
  877. else
  878. png_chunk_error(png_ptr, "CRC error");
  879. }
  880. /* Otherwise, we (optionally) emit a warning and use the chunk. */
  881. else if ((png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN) == 0)
  882. png_chunk_warning(png_ptr, "CRC error");
  883. }
  884. #endif
  885. /* TODO: png_set_PLTE has the side effect of setting png_ptr->palette to its
  886. * own copy of the palette. This has the side effect that when png_start_row
  887. * is called (this happens after any call to png_read_update_info) the
  888. * info_ptr palette gets changed. This is extremely unexpected and
  889. * confusing.
  890. *
  891. * Fix this by not sharing the palette in this way.
  892. */
  893. png_set_PLTE(png_ptr, info_ptr, palette, num);
  894. /* The three chunks, bKGD, hIST and tRNS *must* appear after PLTE and before
  895. * IDAT. Prior to 1.6.0 this was not checked; instead the code merely
  896. * checked the apparent validity of a tRNS chunk inserted before PLTE on a
  897. * palette PNG. 1.6.0 attempts to rigorously follow the standard and
  898. * therefore does a benign error if the erroneous condition is detected *and*
  899. * cancels the tRNS if the benign error returns. The alternative is to
  900. * amend the standard since it would be rather hypocritical of the standards
  901. * maintainers to ignore it.
  902. */
  903. #ifdef PNG_READ_tRNS_SUPPORTED
  904. if (png_ptr->num_trans > 0 ||
  905. (info_ptr != NULL && (info_ptr->valid & PNG_INFO_tRNS) != 0))
  906. {
  907. /* Cancel this because otherwise it would be used if the transforms
  908. * require it. Don't cancel the 'valid' flag because this would prevent
  909. * detection of duplicate chunks.
  910. */
  911. png_ptr->num_trans = 0;
  912. if (info_ptr != NULL)
  913. info_ptr->num_trans = 0;
  914. png_chunk_benign_error(png_ptr, "tRNS must be after");
  915. }
  916. #endif
  917. #ifdef PNG_READ_hIST_SUPPORTED
  918. if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_hIST) != 0)
  919. png_chunk_benign_error(png_ptr, "hIST must be after");
  920. #endif
  921. #ifdef PNG_READ_bKGD_SUPPORTED
  922. if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_bKGD) != 0)
  923. png_chunk_benign_error(png_ptr, "bKGD must be after");
  924. #endif
  925. }
  926. void /* PRIVATE */
  927. png_handle_IEND(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
  928. {
  929. png_debug(1, "in png_handle_IEND");
  930. if ((png_ptr->mode & PNG_HAVE_IHDR) == 0 ||
  931. (png_ptr->mode & PNG_HAVE_IDAT) == 0)
  932. png_chunk_error(png_ptr, "out of place");
  933. png_ptr->mode |= (PNG_AFTER_IDAT | PNG_HAVE_IEND);
  934. png_crc_finish(png_ptr, length);
  935. if (length != 0)
  936. png_chunk_benign_error(png_ptr, "invalid");
  937. PNG_UNUSED(info_ptr)
  938. }
  939. #ifdef PNG_READ_gAMA_SUPPORTED
  940. void /* PRIVATE */
  941. png_handle_gAMA(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
  942. {
  943. png_fixed_point igamma;
  944. png_byte buf[4];
  945. png_debug(1, "in png_handle_gAMA");
  946. if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
  947. png_chunk_error(png_ptr, "missing IHDR");
  948. else if ((png_ptr->mode & (PNG_HAVE_IDAT|PNG_HAVE_PLTE)) != 0)
  949. {
  950. png_crc_finish(png_ptr, length);
  951. png_chunk_benign_error(png_ptr, "out of place");
  952. return;
  953. }
  954. if (length != 4)
  955. {
  956. png_crc_finish(png_ptr, length);
  957. png_chunk_benign_error(png_ptr, "invalid");
  958. return;
  959. }
  960. png_crc_read(png_ptr, buf, 4);
  961. if (png_crc_finish(png_ptr, 0) != 0)
  962. return;
  963. igamma = png_get_fixed_point(NULL, buf);
  964. png_colorspace_set_gamma(png_ptr, &png_ptr->colorspace, igamma);
  965. png_colorspace_sync(png_ptr, info_ptr);
  966. }
  967. #endif
  968. #ifdef PNG_READ_sBIT_SUPPORTED
  969. void /* PRIVATE */
  970. png_handle_sBIT(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
  971. {
  972. unsigned int truelen, i;
  973. png_byte sample_depth;
  974. png_byte buf[4];
  975. png_debug(1, "in png_handle_sBIT");
  976. if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
  977. png_chunk_error(png_ptr, "missing IHDR");
  978. else if ((png_ptr->mode & (PNG_HAVE_IDAT|PNG_HAVE_PLTE)) != 0)
  979. {
  980. png_crc_finish(png_ptr, length);
  981. png_chunk_benign_error(png_ptr, "out of place");
  982. return;
  983. }
  984. if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_sBIT) != 0)
  985. {
  986. png_crc_finish(png_ptr, length);
  987. png_chunk_benign_error(png_ptr, "duplicate");
  988. return;
  989. }
  990. if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
  991. {
  992. truelen = 3;
  993. sample_depth = 8;
  994. }
  995. else
  996. {
  997. truelen = png_ptr->channels;
  998. sample_depth = png_ptr->bit_depth;
  999. }
  1000. if (length != truelen || length > 4)
  1001. {
  1002. png_chunk_benign_error(png_ptr, "invalid");
  1003. png_crc_finish(png_ptr, length);
  1004. return;
  1005. }
  1006. buf[0] = buf[1] = buf[2] = buf[3] = sample_depth;
  1007. png_crc_read(png_ptr, buf, truelen);
  1008. if (png_crc_finish(png_ptr, 0) != 0)
  1009. return;
  1010. for (i=0; i<truelen; ++i)
  1011. {
  1012. if (buf[i] == 0 || buf[i] > sample_depth)
  1013. {
  1014. png_chunk_benign_error(png_ptr, "invalid");
  1015. return;
  1016. }
  1017. }
  1018. if ((png_ptr->color_type & PNG_COLOR_MASK_COLOR) != 0)
  1019. {
  1020. png_ptr->sig_bit.red = buf[0];
  1021. png_ptr->sig_bit.green = buf[1];
  1022. png_ptr->sig_bit.blue = buf[2];
  1023. png_ptr->sig_bit.alpha = buf[3];
  1024. }
  1025. else
  1026. {
  1027. png_ptr->sig_bit.gray = buf[0];
  1028. png_ptr->sig_bit.red = buf[0];
  1029. png_ptr->sig_bit.green = buf[0];
  1030. png_ptr->sig_bit.blue = buf[0];
  1031. png_ptr->sig_bit.alpha = buf[1];
  1032. }
  1033. png_set_sBIT(png_ptr, info_ptr, &(png_ptr->sig_bit));
  1034. }
  1035. #endif
  1036. #ifdef PNG_READ_cHRM_SUPPORTED
  1037. void /* PRIVATE */
  1038. png_handle_cHRM(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
  1039. {
  1040. png_byte buf[32];
  1041. png_xy xy;
  1042. png_debug(1, "in png_handle_cHRM");
  1043. if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
  1044. png_chunk_error(png_ptr, "missing IHDR");
  1045. else if ((png_ptr->mode & (PNG_HAVE_IDAT|PNG_HAVE_PLTE)) != 0)
  1046. {
  1047. png_crc_finish(png_ptr, length);
  1048. png_chunk_benign_error(png_ptr, "out of place");
  1049. return;
  1050. }
  1051. if (length != 32)
  1052. {
  1053. png_crc_finish(png_ptr, length);
  1054. png_chunk_benign_error(png_ptr, "invalid");
  1055. return;
  1056. }
  1057. png_crc_read(png_ptr, buf, 32);
  1058. if (png_crc_finish(png_ptr, 0) != 0)
  1059. return;
  1060. xy.whitex = png_get_fixed_point(NULL, buf);
  1061. xy.whitey = png_get_fixed_point(NULL, buf + 4);
  1062. xy.redx = png_get_fixed_point(NULL, buf + 8);
  1063. xy.redy = png_get_fixed_point(NULL, buf + 12);
  1064. xy.greenx = png_get_fixed_point(NULL, buf + 16);
  1065. xy.greeny = png_get_fixed_point(NULL, buf + 20);
  1066. xy.bluex = png_get_fixed_point(NULL, buf + 24);
  1067. xy.bluey = png_get_fixed_point(NULL, buf + 28);
  1068. if (xy.whitex == PNG_FIXED_ERROR ||
  1069. xy.whitey == PNG_FIXED_ERROR ||
  1070. xy.redx == PNG_FIXED_ERROR ||
  1071. xy.redy == PNG_FIXED_ERROR ||
  1072. xy.greenx == PNG_FIXED_ERROR ||
  1073. xy.greeny == PNG_FIXED_ERROR ||
  1074. xy.bluex == PNG_FIXED_ERROR ||
  1075. xy.bluey == PNG_FIXED_ERROR)
  1076. {
  1077. png_chunk_benign_error(png_ptr, "invalid values");
  1078. return;
  1079. }
  1080. /* If a colorspace error has already been output skip this chunk */
  1081. if ((png_ptr->colorspace.flags & PNG_COLORSPACE_INVALID) != 0)
  1082. return;
  1083. if ((png_ptr->colorspace.flags & PNG_COLORSPACE_FROM_cHRM) != 0)
  1084. {
  1085. png_ptr->colorspace.flags |= PNG_COLORSPACE_INVALID;
  1086. png_colorspace_sync(png_ptr, info_ptr);
  1087. png_chunk_benign_error(png_ptr, "duplicate");
  1088. return;
  1089. }
  1090. png_ptr->colorspace.flags |= PNG_COLORSPACE_FROM_cHRM;
  1091. (void)png_colorspace_set_chromaticities(png_ptr, &png_ptr->colorspace, &xy,
  1092. 1/*prefer cHRM values*/);
  1093. png_colorspace_sync(png_ptr, info_ptr);
  1094. }
  1095. #endif
  1096. #ifdef PNG_READ_sRGB_SUPPORTED
  1097. void /* PRIVATE */
  1098. png_handle_sRGB(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
  1099. {
  1100. png_byte intent;
  1101. png_debug(1, "in png_handle_sRGB");
  1102. if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
  1103. png_chunk_error(png_ptr, "missing IHDR");
  1104. else if ((png_ptr->mode & (PNG_HAVE_IDAT|PNG_HAVE_PLTE)) != 0)
  1105. {
  1106. png_crc_finish(png_ptr, length);
  1107. png_chunk_benign_error(png_ptr, "out of place");
  1108. return;
  1109. }
  1110. if (length != 1)
  1111. {
  1112. png_crc_finish(png_ptr, length);
  1113. png_chunk_benign_error(png_ptr, "invalid");
  1114. return;
  1115. }
  1116. png_crc_read(png_ptr, &intent, 1);
  1117. if (png_crc_finish(png_ptr, 0) != 0)
  1118. return;
  1119. /* If a colorspace error has already been output skip this chunk */
  1120. if ((png_ptr->colorspace.flags & PNG_COLORSPACE_INVALID) != 0)
  1121. return;
  1122. /* Only one sRGB or iCCP chunk is allowed, use the HAVE_INTENT flag to detect
  1123. * this.
  1124. */
  1125. if ((png_ptr->colorspace.flags & PNG_COLORSPACE_HAVE_INTENT) != 0)
  1126. {
  1127. png_ptr->colorspace.flags |= PNG_COLORSPACE_INVALID;
  1128. png_colorspace_sync(png_ptr, info_ptr);
  1129. png_chunk_benign_error(png_ptr, "too many profiles");
  1130. return;
  1131. }
  1132. (void)png_colorspace_set_sRGB(png_ptr, &png_ptr->colorspace, intent);
  1133. png_colorspace_sync(png_ptr, info_ptr);
  1134. }
  1135. #endif /* READ_sRGB */
  1136. #ifdef PNG_READ_iCCP_SUPPORTED
  1137. void /* PRIVATE */
  1138. png_handle_iCCP(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
  1139. /* Note: this does not properly handle profiles that are > 64K under DOS */
  1140. {
  1141. png_const_charp errmsg = NULL; /* error message output, or no error */
  1142. int finished = 0; /* crc checked */
  1143. png_debug(1, "in png_handle_iCCP");
  1144. if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
  1145. png_chunk_error(png_ptr, "missing IHDR");
  1146. else if ((png_ptr->mode & (PNG_HAVE_IDAT|PNG_HAVE_PLTE)) != 0)
  1147. {
  1148. png_crc_finish(png_ptr, length);
  1149. png_chunk_benign_error(png_ptr, "out of place");
  1150. return;
  1151. }
  1152. /* Consistent with all the above colorspace handling an obviously *invalid*
  1153. * chunk is just ignored, so does not invalidate the color space. An
  1154. * alternative is to set the 'invalid' flags at the start of this routine
  1155. * and only clear them in they were not set before and all the tests pass.
  1156. */
  1157. /* The keyword must be at least one character and there is a
  1158. * terminator (0) byte and the compression method byte, and the
  1159. * 'zlib' datastream is at least 11 bytes.
  1160. */
  1161. if (length < 14)
  1162. {
  1163. png_crc_finish(png_ptr, length);
  1164. png_chunk_benign_error(png_ptr, "too short");
  1165. return;
  1166. }
  1167. /* If a colorspace error has already been output skip this chunk */
  1168. if ((png_ptr->colorspace.flags & PNG_COLORSPACE_INVALID) != 0)
  1169. {
  1170. png_crc_finish(png_ptr, length);
  1171. return;
  1172. }
  1173. /* Only one sRGB or iCCP chunk is allowed, use the HAVE_INTENT flag to detect
  1174. * this.
  1175. */
  1176. if ((png_ptr->colorspace.flags & PNG_COLORSPACE_HAVE_INTENT) == 0)
  1177. {
  1178. uInt read_length, keyword_length;
  1179. char keyword[81];
  1180. /* Find the keyword; the keyword plus separator and compression method
  1181. * bytes can be at most 81 characters long.
  1182. */
  1183. read_length = 81; /* maximum */
  1184. if (read_length > length)
  1185. read_length = (uInt)length;
  1186. png_crc_read(png_ptr, (png_bytep)keyword, read_length);
  1187. length -= read_length;
  1188. /* The minimum 'zlib' stream is assumed to be just the 2 byte header,
  1189. * 5 bytes minimum 'deflate' stream, and the 4 byte checksum.
  1190. */
  1191. if (length < 11)
  1192. {
  1193. png_crc_finish(png_ptr, length);
  1194. png_chunk_benign_error(png_ptr, "too short");
  1195. return;
  1196. }
  1197. keyword_length = 0;
  1198. while (keyword_length < 80 && keyword_length < read_length &&
  1199. keyword[keyword_length] != 0)
  1200. ++keyword_length;
  1201. /* TODO: make the keyword checking common */
  1202. if (keyword_length >= 1 && keyword_length <= 79)
  1203. {
  1204. /* We only understand '0' compression - deflate - so if we get a
  1205. * different value we can't safely decode the chunk.
  1206. */
  1207. if (keyword_length+1 < read_length &&
  1208. keyword[keyword_length+1] == PNG_COMPRESSION_TYPE_BASE)
  1209. {
  1210. read_length -= keyword_length+2;
  1211. if (png_inflate_claim(png_ptr, png_iCCP) == Z_OK)
  1212. {
  1213. Byte profile_header[132]={0};
  1214. Byte local_buffer[PNG_INFLATE_BUF_SIZE];
  1215. png_alloc_size_t size = (sizeof profile_header);
  1216. png_ptr->zstream.next_in = (Bytef*)keyword + (keyword_length+2);
  1217. png_ptr->zstream.avail_in = read_length;
  1218. (void)png_inflate_read(png_ptr, local_buffer,
  1219. (sizeof local_buffer), &length, profile_header, &size,
  1220. 0/*finish: don't, because the output is too small*/);
  1221. if (size == 0)
  1222. {
  1223. /* We have the ICC profile header; do the basic header checks.
  1224. */
  1225. png_uint_32 profile_length = png_get_uint_32(profile_header);
  1226. if (png_icc_check_length(png_ptr, &png_ptr->colorspace,
  1227. keyword, profile_length) != 0)
  1228. {
  1229. /* The length is apparently ok, so we can check the 132
  1230. * byte header.
  1231. */
  1232. if (png_icc_check_header(png_ptr, &png_ptr->colorspace,
  1233. keyword, profile_length, profile_header,
  1234. png_ptr->color_type) != 0)
  1235. {
  1236. /* Now read the tag table; a variable size buffer is
  1237. * needed at this point, allocate one for the whole
  1238. * profile. The header check has already validated
  1239. * that none of this stuff will overflow.
  1240. */
  1241. png_uint_32 tag_count =
  1242. png_get_uint_32(profile_header + 128);
  1243. png_bytep profile = png_read_buffer(png_ptr,
  1244. profile_length, 2/*silent*/);
  1245. if (profile != NULL)
  1246. {
  1247. memcpy(profile, profile_header,
  1248. (sizeof profile_header));
  1249. size = 12 * tag_count;
  1250. (void)png_inflate_read(png_ptr, local_buffer,
  1251. (sizeof local_buffer), &length,
  1252. profile + (sizeof profile_header), &size, 0);
  1253. /* Still expect a buffer error because we expect
  1254. * there to be some tag data!
  1255. */
  1256. if (size == 0)
  1257. {
  1258. if (png_icc_check_tag_table(png_ptr,
  1259. &png_ptr->colorspace, keyword, profile_length,
  1260. profile) != 0)
  1261. {
  1262. /* The profile has been validated for basic
  1263. * security issues, so read the whole thing in.
  1264. */
  1265. size = profile_length - (sizeof profile_header)
  1266. - 12 * tag_count;
  1267. (void)png_inflate_read(png_ptr, local_buffer,
  1268. (sizeof local_buffer), &length,
  1269. profile + (sizeof profile_header) +
  1270. 12 * tag_count, &size, 1/*finish*/);
  1271. if (length > 0 && !(png_ptr->flags &
  1272. PNG_FLAG_BENIGN_ERRORS_WARN))
  1273. errmsg = "extra compressed data";
  1274. /* But otherwise allow extra data: */
  1275. else if (size == 0)
  1276. {
  1277. if (length > 0)
  1278. {
  1279. /* This can be handled completely, so
  1280. * keep going.
  1281. */
  1282. png_chunk_warning(png_ptr,
  1283. "extra compressed data");
  1284. }
  1285. png_crc_finish(png_ptr, length);
  1286. finished = 1;
  1287. # if defined(PNG_sRGB_SUPPORTED) && PNG_sRGB_PROFILE_CHECKS >= 0
  1288. /* Check for a match against sRGB */
  1289. png_icc_set_sRGB(png_ptr,
  1290. &png_ptr->colorspace, profile,
  1291. png_ptr->zstream.adler);
  1292. # endif
  1293. /* Steal the profile for info_ptr. */
  1294. if (info_ptr != NULL)
  1295. {
  1296. png_free_data(png_ptr, info_ptr,
  1297. PNG_FREE_ICCP, 0);
  1298. info_ptr->iccp_name = png_voidcast(char*,
  1299. png_malloc_base(png_ptr,
  1300. keyword_length+1));
  1301. if (info_ptr->iccp_name != NULL)
  1302. {
  1303. memcpy(info_ptr->iccp_name, keyword,
  1304. keyword_length+1);
  1305. info_ptr->iccp_proflen =
  1306. profile_length;
  1307. info_ptr->iccp_profile = profile;
  1308. png_ptr->read_buffer = NULL; /*steal*/
  1309. info_ptr->free_me |= PNG_FREE_ICCP;
  1310. info_ptr->valid |= PNG_INFO_iCCP;
  1311. }
  1312. else
  1313. {
  1314. png_ptr->colorspace.flags |=
  1315. PNG_COLORSPACE_INVALID;
  1316. errmsg = "out of memory";
  1317. }
  1318. }
  1319. /* else the profile remains in the read
  1320. * buffer which gets reused for subsequent
  1321. * chunks.
  1322. */
  1323. if (info_ptr != NULL)
  1324. png_colorspace_sync(png_ptr, info_ptr);
  1325. if (errmsg == NULL)
  1326. {
  1327. png_ptr->zowner = 0;
  1328. return;
  1329. }
  1330. }
  1331. if (errmsg == NULL)
  1332. errmsg = png_ptr->zstream.msg;
  1333. }
  1334. /* else png_icc_check_tag_table output an error */
  1335. }
  1336. else /* profile truncated */
  1337. errmsg = png_ptr->zstream.msg;
  1338. }
  1339. else
  1340. errmsg = "out of memory";
  1341. }
  1342. /* else png_icc_check_header output an error */
  1343. }
  1344. /* else png_icc_check_length output an error */
  1345. }
  1346. else /* profile truncated */
  1347. errmsg = png_ptr->zstream.msg;
  1348. /* Release the stream */
  1349. png_ptr->zowner = 0;
  1350. }
  1351. else /* png_inflate_claim failed */
  1352. errmsg = png_ptr->zstream.msg;
  1353. }
  1354. else
  1355. errmsg = "bad compression method"; /* or missing */
  1356. }
  1357. else
  1358. errmsg = "bad keyword";
  1359. }
  1360. else
  1361. errmsg = "too many profiles";
  1362. /* Failure: the reason is in 'errmsg' */
  1363. if (finished == 0)
  1364. png_crc_finish(png_ptr, length);
  1365. png_ptr->colorspace.flags |= PNG_COLORSPACE_INVALID;
  1366. png_colorspace_sync(png_ptr, info_ptr);
  1367. if (errmsg != NULL) /* else already output */
  1368. png_chunk_benign_error(png_ptr, errmsg);
  1369. }
  1370. #endif /* READ_iCCP */
  1371. #ifdef PNG_READ_sPLT_SUPPORTED
  1372. void /* PRIVATE */
  1373. png_handle_sPLT(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
  1374. /* Note: this does not properly handle chunks that are > 64K under DOS */
  1375. {
  1376. png_bytep entry_start, buffer;
  1377. png_sPLT_t new_palette;
  1378. png_sPLT_entryp pp;
  1379. png_uint_32 data_length;
  1380. int entry_size, i;
  1381. png_uint_32 skip = 0;
  1382. png_uint_32 dl;
  1383. size_t max_dl;
  1384. png_debug(1, "in png_handle_sPLT");
  1385. #ifdef PNG_USER_LIMITS_SUPPORTED
  1386. if (png_ptr->user_chunk_cache_max != 0)
  1387. {
  1388. if (png_ptr->user_chunk_cache_max == 1)
  1389. {
  1390. png_crc_finish(png_ptr, length);
  1391. return;
  1392. }
  1393. if (--png_ptr->user_chunk_cache_max == 1)
  1394. {
  1395. png_warning(png_ptr, "No space in chunk cache for sPLT");
  1396. png_crc_finish(png_ptr, length);
  1397. return;
  1398. }
  1399. }
  1400. #endif
  1401. if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
  1402. png_chunk_error(png_ptr, "missing IHDR");
  1403. else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
  1404. {
  1405. png_crc_finish(png_ptr, length);
  1406. png_chunk_benign_error(png_ptr, "out of place");
  1407. return;
  1408. }
  1409. #ifdef PNG_MAX_MALLOC_64K
  1410. if (length > 65535U)
  1411. {
  1412. png_crc_finish(png_ptr, length);
  1413. png_chunk_benign_error(png_ptr, "too large to fit in memory");
  1414. return;
  1415. }
  1416. #endif
  1417. buffer = png_read_buffer(png_ptr, length+1, 2/*silent*/);
  1418. if (buffer == NULL)
  1419. {
  1420. png_crc_finish(png_ptr, length);
  1421. png_chunk_benign_error(png_ptr, "out of memory");
  1422. return;
  1423. }
  1424. /* WARNING: this may break if size_t is less than 32 bits; it is assumed
  1425. * that the PNG_MAX_MALLOC_64K test is enabled in this case, but this is a
  1426. * potential breakage point if the types in pngconf.h aren't exactly right.
  1427. */
  1428. png_crc_read(png_ptr, buffer, length);
  1429. if (png_crc_finish(png_ptr, skip) != 0)
  1430. return;
  1431. buffer[length] = 0;
  1432. for (entry_start = buffer; *entry_start; entry_start++)
  1433. /* Empty loop to find end of name */ ;
  1434. ++entry_start;
  1435. /* A sample depth should follow the separator, and we should be on it */
  1436. if (length < 2U || entry_start > buffer + (length - 2U))
  1437. {
  1438. png_warning(png_ptr, "malformed sPLT chunk");
  1439. return;
  1440. }
  1441. new_palette.depth = *entry_start++;
  1442. entry_size = (new_palette.depth == 8 ? 6 : 10);
  1443. /* This must fit in a png_uint_32 because it is derived from the original
  1444. * chunk data length.
  1445. */
  1446. data_length = length - (png_uint_32)(entry_start - buffer);
  1447. /* Integrity-check the data length */
  1448. if ((data_length % (unsigned int)entry_size) != 0)
  1449. {
  1450. png_warning(png_ptr, "sPLT chunk has bad length");
  1451. return;
  1452. }
  1453. dl = (png_uint_32)(data_length / (unsigned int)entry_size);
  1454. max_dl = PNG_SIZE_MAX / (sizeof (png_sPLT_entry));
  1455. if (dl > max_dl)
  1456. {
  1457. png_warning(png_ptr, "sPLT chunk too long");
  1458. return;
  1459. }
  1460. new_palette.nentries = (png_int_32)(data_length / (unsigned int)entry_size);
  1461. new_palette.entries = (png_sPLT_entryp)png_malloc_warn(png_ptr,
  1462. (png_alloc_size_t) new_palette.nentries * (sizeof (png_sPLT_entry)));
  1463. if (new_palette.entries == NULL)
  1464. {
  1465. png_warning(png_ptr, "sPLT chunk requires too much memory");
  1466. return;
  1467. }
  1468. #ifdef PNG_POINTER_INDEXING_SUPPORTED
  1469. for (i = 0; i < new_palette.nentries; i++)
  1470. {
  1471. pp = new_palette.entries + i;
  1472. if (new_palette.depth == 8)
  1473. {
  1474. pp->red = *entry_start++;
  1475. pp->green = *entry_start++;
  1476. pp->blue = *entry_start++;
  1477. pp->alpha = *entry_start++;
  1478. }
  1479. else
  1480. {
  1481. pp->red = png_get_uint_16(entry_start); entry_start += 2;
  1482. pp->green = png_get_uint_16(entry_start); entry_start += 2;
  1483. pp->blue = png_get_uint_16(entry_start); entry_start += 2;
  1484. pp->alpha = png_get_uint_16(entry_start); entry_start += 2;
  1485. }
  1486. pp->frequency = png_get_uint_16(entry_start); entry_start += 2;
  1487. }
  1488. #else
  1489. pp = new_palette.entries;
  1490. for (i = 0; i < new_palette.nentries; i++)
  1491. {
  1492. if (new_palette.depth == 8)
  1493. {
  1494. pp[i].red = *entry_start++;
  1495. pp[i].green = *entry_start++;
  1496. pp[i].blue = *entry_start++;
  1497. pp[i].alpha = *entry_start++;
  1498. }
  1499. else
  1500. {
  1501. pp[i].red = png_get_uint_16(entry_start); entry_start += 2;
  1502. pp[i].green = png_get_uint_16(entry_start); entry_start += 2;
  1503. pp[i].blue = png_get_uint_16(entry_start); entry_start += 2;
  1504. pp[i].alpha = png_get_uint_16(entry_start); entry_start += 2;
  1505. }
  1506. pp[i].frequency = png_get_uint_16(entry_start); entry_start += 2;
  1507. }
  1508. #endif
  1509. /* Discard all chunk data except the name and stash that */
  1510. new_palette.name = (png_charp)buffer;
  1511. png_set_sPLT(png_ptr, info_ptr, &new_palette, 1);
  1512. png_free(png_ptr, new_palette.entries);
  1513. }
  1514. #endif /* READ_sPLT */
  1515. #ifdef PNG_READ_tRNS_SUPPORTED
  1516. void /* PRIVATE */
  1517. png_handle_tRNS(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
  1518. {
  1519. png_byte readbuf[PNG_MAX_PALETTE_LENGTH];
  1520. png_debug(1, "in png_handle_tRNS");
  1521. if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
  1522. png_chunk_error(png_ptr, "missing IHDR");
  1523. else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
  1524. {
  1525. png_crc_finish(png_ptr, length);
  1526. png_chunk_benign_error(png_ptr, "out of place");
  1527. return;
  1528. }
  1529. else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_tRNS) != 0)
  1530. {
  1531. png_crc_finish(png_ptr, length);
  1532. png_chunk_benign_error(png_ptr, "duplicate");
  1533. return;
  1534. }
  1535. if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY)
  1536. {
  1537. png_byte buf[2];
  1538. if (length != 2)
  1539. {
  1540. png_crc_finish(png_ptr, length);
  1541. png_chunk_benign_error(png_ptr, "invalid");
  1542. return;
  1543. }
  1544. png_crc_read(png_ptr, buf, 2);
  1545. png_ptr->num_trans = 1;
  1546. png_ptr->trans_color.gray = png_get_uint_16(buf);
  1547. }
  1548. else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB)
  1549. {
  1550. png_byte buf[6];
  1551. if (length != 6)
  1552. {
  1553. png_crc_finish(png_ptr, length);
  1554. png_chunk_benign_error(png_ptr, "invalid");
  1555. return;
  1556. }
  1557. png_crc_read(png_ptr, buf, length);
  1558. png_ptr->num_trans = 1;
  1559. png_ptr->trans_color.red = png_get_uint_16(buf);
  1560. png_ptr->trans_color.green = png_get_uint_16(buf + 2);
  1561. png_ptr->trans_color.blue = png_get_uint_16(buf + 4);
  1562. }
  1563. else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
  1564. {
  1565. if ((png_ptr->mode & PNG_HAVE_PLTE) == 0)
  1566. {
  1567. /* TODO: is this actually an error in the ISO spec? */
  1568. png_crc_finish(png_ptr, length);
  1569. png_chunk_benign_error(png_ptr, "out of place");
  1570. return;
  1571. }
  1572. if (length > (unsigned int) png_ptr->num_palette ||
  1573. length > (unsigned int) PNG_MAX_PALETTE_LENGTH ||
  1574. length == 0)
  1575. {
  1576. png_crc_finish(png_ptr, length);
  1577. png_chunk_benign_error(png_ptr, "invalid");
  1578. return;
  1579. }
  1580. png_crc_read(png_ptr, readbuf, length);
  1581. png_ptr->num_trans = (png_uint_16)length;
  1582. }
  1583. else
  1584. {
  1585. png_crc_finish(png_ptr, length);
  1586. png_chunk_benign_error(png_ptr, "invalid with alpha channel");
  1587. return;
  1588. }
  1589. if (png_crc_finish(png_ptr, 0) != 0)
  1590. {
  1591. png_ptr->num_trans = 0;
  1592. return;
  1593. }
  1594. /* TODO: this is a horrible side effect in the palette case because the
  1595. * png_struct ends up with a pointer to the tRNS buffer owned by the
  1596. * png_info. Fix this.
  1597. */
  1598. png_set_tRNS(png_ptr, info_ptr, readbuf, png_ptr->num_trans,
  1599. &(png_ptr->trans_color));
  1600. }
  1601. #endif
  1602. #ifdef PNG_READ_bKGD_SUPPORTED
  1603. void /* PRIVATE */
  1604. png_handle_bKGD(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
  1605. {
  1606. unsigned int truelen;
  1607. png_byte buf[6];
  1608. png_color_16 background;
  1609. png_debug(1, "in png_handle_bKGD");
  1610. if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
  1611. png_chunk_error(png_ptr, "missing IHDR");
  1612. else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0 ||
  1613. (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
  1614. (png_ptr->mode & PNG_HAVE_PLTE) == 0))
  1615. {
  1616. png_crc_finish(png_ptr, length);
  1617. png_chunk_benign_error(png_ptr, "out of place");
  1618. return;
  1619. }
  1620. else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_bKGD) != 0)
  1621. {
  1622. png_crc_finish(png_ptr, length);
  1623. png_chunk_benign_error(png_ptr, "duplicate");
  1624. return;
  1625. }
  1626. if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
  1627. truelen = 1;
  1628. else if ((png_ptr->color_type & PNG_COLOR_MASK_COLOR) != 0)
  1629. truelen = 6;
  1630. else
  1631. truelen = 2;
  1632. if (length != truelen)
  1633. {
  1634. png_crc_finish(png_ptr, length);
  1635. png_chunk_benign_error(png_ptr, "invalid");
  1636. return;
  1637. }
  1638. png_crc_read(png_ptr, buf, truelen);
  1639. if (png_crc_finish(png_ptr, 0) != 0)
  1640. return;
  1641. /* We convert the index value into RGB components so that we can allow
  1642. * arbitrary RGB values for background when we have transparency, and
  1643. * so it is easy to determine the RGB values of the background color
  1644. * from the info_ptr struct.
  1645. */
  1646. if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
  1647. {
  1648. background.index = buf[0];
  1649. if (info_ptr != NULL && info_ptr->num_palette != 0)
  1650. {
  1651. if (buf[0] >= info_ptr->num_palette)
  1652. {
  1653. png_chunk_benign_error(png_ptr, "invalid index");
  1654. return;
  1655. }
  1656. background.red = (png_uint_16)png_ptr->palette[buf[0]].red;
  1657. background.green = (png_uint_16)png_ptr->palette[buf[0]].green;
  1658. background.blue = (png_uint_16)png_ptr->palette[buf[0]].blue;
  1659. }
  1660. else
  1661. background.red = background.green = background.blue = 0;
  1662. background.gray = 0;
  1663. }
  1664. else if ((png_ptr->color_type & PNG_COLOR_MASK_COLOR) == 0) /* GRAY */
  1665. {
  1666. if (png_ptr->bit_depth <= 8)
  1667. {
  1668. if (buf[0] != 0 || buf[1] >= (unsigned int)(1 << png_ptr->bit_depth))
  1669. {
  1670. png_chunk_benign_error(png_ptr, "invalid gray level");
  1671. return;
  1672. }
  1673. }
  1674. background.index = 0;
  1675. background.red =
  1676. background.green =
  1677. background.blue =
  1678. background.gray = png_get_uint_16(buf);
  1679. }
  1680. else
  1681. {
  1682. if (png_ptr->bit_depth <= 8)
  1683. {
  1684. if (buf[0] != 0 || buf[2] != 0 || buf[4] != 0)
  1685. {
  1686. png_chunk_benign_error(png_ptr, "invalid color");
  1687. return;
  1688. }
  1689. }
  1690. background.index = 0;
  1691. background.red = png_get_uint_16(buf);
  1692. background.green = png_get_uint_16(buf + 2);
  1693. background.blue = png_get_uint_16(buf + 4);
  1694. background.gray = 0;
  1695. }
  1696. png_set_bKGD(png_ptr, info_ptr, &background);
  1697. }
  1698. #endif
  1699. #ifdef PNG_READ_eXIf_SUPPORTED
  1700. void /* PRIVATE */
  1701. png_handle_eXIf(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
  1702. {
  1703. unsigned int i;
  1704. png_debug(1, "in png_handle_eXIf");
  1705. if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
  1706. png_chunk_error(png_ptr, "missing IHDR");
  1707. if (length < 2)
  1708. {
  1709. png_crc_finish(png_ptr, length);
  1710. png_chunk_benign_error(png_ptr, "too short");
  1711. return;
  1712. }
  1713. else if (info_ptr == NULL || (info_ptr->valid & PNG_INFO_eXIf) != 0)
  1714. {
  1715. png_crc_finish(png_ptr, length);
  1716. png_chunk_benign_error(png_ptr, "duplicate");
  1717. return;
  1718. }
  1719. info_ptr->free_me |= PNG_FREE_EXIF;
  1720. info_ptr->eXIf_buf = png_voidcast(png_bytep,
  1721. png_malloc_warn(png_ptr, length));
  1722. if (info_ptr->eXIf_buf == NULL)
  1723. {
  1724. png_crc_finish(png_ptr, length);
  1725. png_chunk_benign_error(png_ptr, "out of memory");
  1726. return;
  1727. }
  1728. for (i = 0; i < length; i++)
  1729. {
  1730. png_byte buf[1];
  1731. png_crc_read(png_ptr, buf, 1);
  1732. info_ptr->eXIf_buf[i] = buf[0];
  1733. if (i == 1)
  1734. {
  1735. if ((buf[0] != 'M' && buf[0] != 'I') ||
  1736. (info_ptr->eXIf_buf[0] != buf[0]))
  1737. {
  1738. png_crc_finish(png_ptr, length - 2);
  1739. png_chunk_benign_error(png_ptr, "incorrect byte-order specifier");
  1740. png_free(png_ptr, info_ptr->eXIf_buf);
  1741. info_ptr->eXIf_buf = NULL;
  1742. return;
  1743. }
  1744. }
  1745. }
  1746. if (png_crc_finish(png_ptr, 0) == 0)
  1747. png_set_eXIf_1(png_ptr, info_ptr, length, info_ptr->eXIf_buf);
  1748. png_free(png_ptr, info_ptr->eXIf_buf);
  1749. info_ptr->eXIf_buf = NULL;
  1750. }
  1751. #endif
  1752. #ifdef PNG_READ_hIST_SUPPORTED
  1753. void /* PRIVATE */
  1754. png_handle_hIST(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
  1755. {
  1756. unsigned int num, i;
  1757. png_uint_16 readbuf[PNG_MAX_PALETTE_LENGTH];
  1758. png_debug(1, "in png_handle_hIST");
  1759. if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
  1760. png_chunk_error(png_ptr, "missing IHDR");
  1761. else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0 ||
  1762. (png_ptr->mode & PNG_HAVE_PLTE) == 0)
  1763. {
  1764. png_crc_finish(png_ptr, length);
  1765. png_chunk_benign_error(png_ptr, "out of place");
  1766. return;
  1767. }
  1768. else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_hIST) != 0)
  1769. {
  1770. png_crc_finish(png_ptr, length);
  1771. png_chunk_benign_error(png_ptr, "duplicate");
  1772. return;
  1773. }
  1774. num = length / 2 ;
  1775. if (length != num * 2 ||
  1776. num != (unsigned int)png_ptr->num_palette ||
  1777. num > (unsigned int)PNG_MAX_PALETTE_LENGTH)
  1778. {
  1779. png_crc_finish(png_ptr, length);
  1780. png_chunk_benign_error(png_ptr, "invalid");
  1781. return;
  1782. }
  1783. for (i = 0; i < num; i++)
  1784. {
  1785. png_byte buf[2];
  1786. png_crc_read(png_ptr, buf, 2);
  1787. readbuf[i] = png_get_uint_16(buf);
  1788. }
  1789. if (png_crc_finish(png_ptr, 0) != 0)
  1790. return;
  1791. png_set_hIST(png_ptr, info_ptr, readbuf);
  1792. }
  1793. #endif
  1794. #ifdef PNG_READ_pHYs_SUPPORTED
  1795. void /* PRIVATE */
  1796. png_handle_pHYs(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
  1797. {
  1798. png_byte buf[9];
  1799. png_uint_32 res_x, res_y;
  1800. int unit_type;
  1801. png_debug(1, "in png_handle_pHYs");
  1802. if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
  1803. png_chunk_error(png_ptr, "missing IHDR");
  1804. else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
  1805. {
  1806. png_crc_finish(png_ptr, length);
  1807. png_chunk_benign_error(png_ptr, "out of place");
  1808. return;
  1809. }
  1810. else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_pHYs) != 0)
  1811. {
  1812. png_crc_finish(png_ptr, length);
  1813. png_chunk_benign_error(png_ptr, "duplicate");
  1814. return;
  1815. }
  1816. if (length != 9)
  1817. {
  1818. png_crc_finish(png_ptr, length);
  1819. png_chunk_benign_error(png_ptr, "invalid");
  1820. return;
  1821. }
  1822. png_crc_read(png_ptr, buf, 9);
  1823. if (png_crc_finish(png_ptr, 0) != 0)
  1824. return;
  1825. res_x = png_get_uint_32(buf);
  1826. res_y = png_get_uint_32(buf + 4);
  1827. unit_type = buf[8];
  1828. png_set_pHYs(png_ptr, info_ptr, res_x, res_y, unit_type);
  1829. }
  1830. #endif
  1831. #ifdef PNG_READ_oFFs_SUPPORTED
  1832. void /* PRIVATE */
  1833. png_handle_oFFs(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
  1834. {
  1835. png_byte buf[9];
  1836. png_int_32 offset_x, offset_y;
  1837. int unit_type;
  1838. png_debug(1, "in png_handle_oFFs");
  1839. if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
  1840. png_chunk_error(png_ptr, "missing IHDR");
  1841. else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
  1842. {
  1843. png_crc_finish(png_ptr, length);
  1844. png_chunk_benign_error(png_ptr, "out of place");
  1845. return;
  1846. }
  1847. else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_oFFs) != 0)
  1848. {
  1849. png_crc_finish(png_ptr, length);
  1850. png_chunk_benign_error(png_ptr, "duplicate");
  1851. return;
  1852. }
  1853. if (length != 9)
  1854. {
  1855. png_crc_finish(png_ptr, length);
  1856. png_chunk_benign_error(png_ptr, "invalid");
  1857. return;
  1858. }
  1859. png_crc_read(png_ptr, buf, 9);
  1860. if (png_crc_finish(png_ptr, 0) != 0)
  1861. return;
  1862. offset_x = png_get_int_32(buf);
  1863. offset_y = png_get_int_32(buf + 4);
  1864. unit_type = buf[8];
  1865. png_set_oFFs(png_ptr, info_ptr, offset_x, offset_y, unit_type);
  1866. }
  1867. #endif
  1868. #ifdef PNG_READ_pCAL_SUPPORTED
  1869. /* Read the pCAL chunk (described in the PNG Extensions document) */
  1870. void /* PRIVATE */
  1871. png_handle_pCAL(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
  1872. {
  1873. png_int_32 X0, X1;
  1874. png_byte type, nparams;
  1875. png_bytep buffer, buf, units, endptr;
  1876. png_charpp params;
  1877. int i;
  1878. png_debug(1, "in png_handle_pCAL");
  1879. if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
  1880. png_chunk_error(png_ptr, "missing IHDR");
  1881. else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
  1882. {
  1883. png_crc_finish(png_ptr, length);
  1884. png_chunk_benign_error(png_ptr, "out of place");
  1885. return;
  1886. }
  1887. else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_pCAL) != 0)
  1888. {
  1889. png_crc_finish(png_ptr, length);
  1890. png_chunk_benign_error(png_ptr, "duplicate");
  1891. return;
  1892. }
  1893. png_debug1(2, "Allocating and reading pCAL chunk data (%u bytes)",
  1894. length + 1);
  1895. buffer = png_read_buffer(png_ptr, length+1, 2/*silent*/);
  1896. if (buffer == NULL)
  1897. {
  1898. png_crc_finish(png_ptr, length);
  1899. png_chunk_benign_error(png_ptr, "out of memory");
  1900. return;
  1901. }
  1902. png_crc_read(png_ptr, buffer, length);
  1903. if (png_crc_finish(png_ptr, 0) != 0)
  1904. return;
  1905. buffer[length] = 0; /* Null terminate the last string */
  1906. png_debug(3, "Finding end of pCAL purpose string");
  1907. for (buf = buffer; *buf; buf++)
  1908. /* Empty loop */ ;
  1909. endptr = buffer + length;
  1910. /* We need to have at least 12 bytes after the purpose string
  1911. * in order to get the parameter information.
  1912. */
  1913. if (endptr - buf <= 12)
  1914. {
  1915. png_chunk_benign_error(png_ptr, "invalid");
  1916. return;
  1917. }
  1918. png_debug(3, "Reading pCAL X0, X1, type, nparams, and units");
  1919. X0 = png_get_int_32((png_bytep)buf+1);
  1920. X1 = png_get_int_32((png_bytep)buf+5);
  1921. type = buf[9];
  1922. nparams = buf[10];
  1923. units = buf + 11;
  1924. png_debug(3, "Checking pCAL equation type and number of parameters");
  1925. /* Check that we have the right number of parameters for known
  1926. * equation types.
  1927. */
  1928. if ((type == PNG_EQUATION_LINEAR && nparams != 2) ||
  1929. (type == PNG_EQUATION_BASE_E && nparams != 3) ||
  1930. (type == PNG_EQUATION_ARBITRARY && nparams != 3) ||
  1931. (type == PNG_EQUATION_HYPERBOLIC && nparams != 4))
  1932. {
  1933. png_chunk_benign_error(png_ptr, "invalid parameter count");
  1934. return;
  1935. }
  1936. else if (type >= PNG_EQUATION_LAST)
  1937. {
  1938. png_chunk_benign_error(png_ptr, "unrecognized equation type");
  1939. }
  1940. for (buf = units; *buf; buf++)
  1941. /* Empty loop to move past the units string. */ ;
  1942. png_debug(3, "Allocating pCAL parameters array");
  1943. params = png_voidcast(png_charpp, png_malloc_warn(png_ptr,
  1944. nparams * (sizeof (png_charp))));
  1945. if (params == NULL)
  1946. {
  1947. png_chunk_benign_error(png_ptr, "out of memory");
  1948. return;
  1949. }
  1950. /* Get pointers to the start of each parameter string. */
  1951. for (i = 0; i < nparams; i++)
  1952. {
  1953. buf++; /* Skip the null string terminator from previous parameter. */
  1954. png_debug1(3, "Reading pCAL parameter %d", i);
  1955. for (params[i] = (png_charp)buf; buf <= endptr && *buf != 0; buf++)
  1956. /* Empty loop to move past each parameter string */ ;
  1957. /* Make sure we haven't run out of data yet */
  1958. if (buf > endptr)
  1959. {
  1960. png_free(png_ptr, params);
  1961. png_chunk_benign_error(png_ptr, "invalid data");
  1962. return;
  1963. }
  1964. }
  1965. png_set_pCAL(png_ptr, info_ptr, (png_charp)buffer, X0, X1, type, nparams,
  1966. (png_charp)units, params);
  1967. png_free(png_ptr, params);
  1968. }
  1969. #endif
  1970. #ifdef PNG_READ_sCAL_SUPPORTED
  1971. /* Read the sCAL chunk */
  1972. void /* PRIVATE */
  1973. png_handle_sCAL(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
  1974. {
  1975. png_bytep buffer;
  1976. size_t i;
  1977. int state;
  1978. png_debug(1, "in png_handle_sCAL");
  1979. if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
  1980. png_chunk_error(png_ptr, "missing IHDR");
  1981. else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
  1982. {
  1983. png_crc_finish(png_ptr, length);
  1984. png_chunk_benign_error(png_ptr, "out of place");
  1985. return;
  1986. }
  1987. else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_sCAL) != 0)
  1988. {
  1989. png_crc_finish(png_ptr, length);
  1990. png_chunk_benign_error(png_ptr, "duplicate");
  1991. return;
  1992. }
  1993. /* Need unit type, width, \0, height: minimum 4 bytes */
  1994. else if (length < 4)
  1995. {
  1996. png_crc_finish(png_ptr, length);
  1997. png_chunk_benign_error(png_ptr, "invalid");
  1998. return;
  1999. }
  2000. png_debug1(2, "Allocating and reading sCAL chunk data (%u bytes)",
  2001. length + 1);
  2002. buffer = png_read_buffer(png_ptr, length+1, 2/*silent*/);
  2003. if (buffer == NULL)
  2004. {
  2005. png_chunk_benign_error(png_ptr, "out of memory");
  2006. png_crc_finish(png_ptr, length);
  2007. return;
  2008. }
  2009. png_crc_read(png_ptr, buffer, length);
  2010. buffer[length] = 0; /* Null terminate the last string */
  2011. if (png_crc_finish(png_ptr, 0) != 0)
  2012. return;
  2013. /* Validate the unit. */
  2014. if (buffer[0] != 1 && buffer[0] != 2)
  2015. {
  2016. png_chunk_benign_error(png_ptr, "invalid unit");
  2017. return;
  2018. }
  2019. /* Validate the ASCII numbers, need two ASCII numbers separated by
  2020. * a '\0' and they need to fit exactly in the chunk data.
  2021. */
  2022. i = 1;
  2023. state = 0;
  2024. if (png_check_fp_number((png_const_charp)buffer, length, &state, &i) == 0 ||
  2025. i >= length || buffer[i++] != 0)
  2026. png_chunk_benign_error(png_ptr, "bad width format");
  2027. else if (PNG_FP_IS_POSITIVE(state) == 0)
  2028. png_chunk_benign_error(png_ptr, "non-positive width");
  2029. else
  2030. {
  2031. size_t heighti = i;
  2032. state = 0;
  2033. if (png_check_fp_number((png_const_charp)buffer, length,
  2034. &state, &i) == 0 || i != length)
  2035. png_chunk_benign_error(png_ptr, "bad height format");
  2036. else if (PNG_FP_IS_POSITIVE(state) == 0)
  2037. png_chunk_benign_error(png_ptr, "non-positive height");
  2038. else
  2039. /* This is the (only) success case. */
  2040. png_set_sCAL_s(png_ptr, info_ptr, buffer[0],
  2041. (png_charp)buffer+1, (png_charp)buffer+heighti);
  2042. }
  2043. }
  2044. #endif
  2045. #ifdef PNG_READ_tIME_SUPPORTED
  2046. void /* PRIVATE */
  2047. png_handle_tIME(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
  2048. {
  2049. png_byte buf[7];
  2050. png_time mod_time;
  2051. png_debug(1, "in png_handle_tIME");
  2052. if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
  2053. png_chunk_error(png_ptr, "missing IHDR");
  2054. else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_tIME) != 0)
  2055. {
  2056. png_crc_finish(png_ptr, length);
  2057. png_chunk_benign_error(png_ptr, "duplicate");
  2058. return;
  2059. }
  2060. if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
  2061. png_ptr->mode |= PNG_AFTER_IDAT;
  2062. if (length != 7)
  2063. {
  2064. png_crc_finish(png_ptr, length);
  2065. png_chunk_benign_error(png_ptr, "invalid");
  2066. return;
  2067. }
  2068. png_crc_read(png_ptr, buf, 7);
  2069. if (png_crc_finish(png_ptr, 0) != 0)
  2070. return;
  2071. mod_time.second = buf[6];
  2072. mod_time.minute = buf[5];
  2073. mod_time.hour = buf[4];
  2074. mod_time.day = buf[3];
  2075. mod_time.month = buf[2];
  2076. mod_time.year = png_get_uint_16(buf);
  2077. png_set_tIME(png_ptr, info_ptr, &mod_time);
  2078. }
  2079. #endif
  2080. #ifdef PNG_READ_tEXt_SUPPORTED
  2081. /* Note: this does not properly handle chunks that are > 64K under DOS */
  2082. void /* PRIVATE */
  2083. png_handle_tEXt(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
  2084. {
  2085. png_text text_info;
  2086. png_bytep buffer;
  2087. png_charp key;
  2088. png_charp text;
  2089. png_uint_32 skip = 0;
  2090. png_debug(1, "in png_handle_tEXt");
  2091. #ifdef PNG_USER_LIMITS_SUPPORTED
  2092. if (png_ptr->user_chunk_cache_max != 0)
  2093. {
  2094. if (png_ptr->user_chunk_cache_max == 1)
  2095. {
  2096. png_crc_finish(png_ptr, length);
  2097. return;
  2098. }
  2099. if (--png_ptr->user_chunk_cache_max == 1)
  2100. {
  2101. png_crc_finish(png_ptr, length);
  2102. png_chunk_benign_error(png_ptr, "no space in chunk cache");
  2103. return;
  2104. }
  2105. }
  2106. #endif
  2107. if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
  2108. png_chunk_error(png_ptr, "missing IHDR");
  2109. if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
  2110. png_ptr->mode |= PNG_AFTER_IDAT;
  2111. #ifdef PNG_MAX_MALLOC_64K
  2112. if (length > 65535U)
  2113. {
  2114. png_crc_finish(png_ptr, length);
  2115. png_chunk_benign_error(png_ptr, "too large to fit in memory");
  2116. return;
  2117. }
  2118. #endif
  2119. buffer = png_read_buffer(png_ptr, length+1, 1/*warn*/);
  2120. if (buffer == NULL)
  2121. {
  2122. png_chunk_benign_error(png_ptr, "out of memory");
  2123. return;
  2124. }
  2125. png_crc_read(png_ptr, buffer, length);
  2126. if (png_crc_finish(png_ptr, skip) != 0)
  2127. return;
  2128. key = (png_charp)buffer;
  2129. key[length] = 0;
  2130. for (text = key; *text; text++)
  2131. /* Empty loop to find end of key */ ;
  2132. if (text != key + length)
  2133. text++;
  2134. text_info.compression = PNG_TEXT_COMPRESSION_NONE;
  2135. text_info.key = key;
  2136. text_info.lang = NULL;
  2137. text_info.lang_key = NULL;
  2138. text_info.itxt_length = 0;
  2139. text_info.text = text;
  2140. text_info.text_length = strlen(text);
  2141. if (png_set_text_2(png_ptr, info_ptr, &text_info, 1) != 0)
  2142. png_warning(png_ptr, "Insufficient memory to process text chunk");
  2143. }
  2144. #endif
  2145. #ifdef PNG_READ_zTXt_SUPPORTED
  2146. /* Note: this does not correctly handle chunks that are > 64K under DOS */
  2147. void /* PRIVATE */
  2148. png_handle_zTXt(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
  2149. {
  2150. png_const_charp errmsg = NULL;
  2151. png_bytep buffer;
  2152. png_uint_32 keyword_length;
  2153. png_debug(1, "in png_handle_zTXt");
  2154. #ifdef PNG_USER_LIMITS_SUPPORTED
  2155. if (png_ptr->user_chunk_cache_max != 0)
  2156. {
  2157. if (png_ptr->user_chunk_cache_max == 1)
  2158. {
  2159. png_crc_finish(png_ptr, length);
  2160. return;
  2161. }
  2162. if (--png_ptr->user_chunk_cache_max == 1)
  2163. {
  2164. png_crc_finish(png_ptr, length);
  2165. png_chunk_benign_error(png_ptr, "no space in chunk cache");
  2166. return;
  2167. }
  2168. }
  2169. #endif
  2170. if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
  2171. png_chunk_error(png_ptr, "missing IHDR");
  2172. if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
  2173. png_ptr->mode |= PNG_AFTER_IDAT;
  2174. /* Note, "length" is sufficient here; we won't be adding
  2175. * a null terminator later.
  2176. */
  2177. buffer = png_read_buffer(png_ptr, length, 2/*silent*/);
  2178. if (buffer == NULL)
  2179. {
  2180. png_crc_finish(png_ptr, length);
  2181. png_chunk_benign_error(png_ptr, "out of memory");
  2182. return;
  2183. }
  2184. png_crc_read(png_ptr, buffer, length);
  2185. if (png_crc_finish(png_ptr, 0) != 0)
  2186. return;
  2187. /* TODO: also check that the keyword contents match the spec! */
  2188. for (keyword_length = 0;
  2189. keyword_length < length && buffer[keyword_length] != 0;
  2190. ++keyword_length)
  2191. /* Empty loop to find end of name */ ;
  2192. if (keyword_length > 79 || keyword_length < 1)
  2193. errmsg = "bad keyword";
  2194. /* zTXt must have some LZ data after the keyword, although it may expand to
  2195. * zero bytes; we need a '\0' at the end of the keyword, the compression type
  2196. * then the LZ data:
  2197. */
  2198. else if (keyword_length + 3 > length)
  2199. errmsg = "truncated";
  2200. else if (buffer[keyword_length+1] != PNG_COMPRESSION_TYPE_BASE)
  2201. errmsg = "unknown compression type";
  2202. else
  2203. {
  2204. png_alloc_size_t uncompressed_length = PNG_SIZE_MAX;
  2205. /* TODO: at present png_decompress_chunk imposes a single application
  2206. * level memory limit, this should be split to different values for iCCP
  2207. * and text chunks.
  2208. */
  2209. if (png_decompress_chunk(png_ptr, length, keyword_length+2,
  2210. &uncompressed_length, 1/*terminate*/) == Z_STREAM_END)
  2211. {
  2212. png_text text;
  2213. if (png_ptr->read_buffer == NULL)
  2214. errmsg="Read failure in png_handle_zTXt";
  2215. else
  2216. {
  2217. /* It worked; png_ptr->read_buffer now looks like a tEXt chunk
  2218. * except for the extra compression type byte and the fact that
  2219. * it isn't necessarily '\0' terminated.
  2220. */
  2221. buffer = png_ptr->read_buffer;
  2222. buffer[uncompressed_length+(keyword_length+2)] = 0;
  2223. text.compression = PNG_TEXT_COMPRESSION_zTXt;
  2224. text.key = (png_charp)buffer;
  2225. text.text = (png_charp)(buffer + keyword_length+2);
  2226. text.text_length = uncompressed_length;
  2227. text.itxt_length = 0;
  2228. text.lang = NULL;
  2229. text.lang_key = NULL;
  2230. if (png_set_text_2(png_ptr, info_ptr, &text, 1) != 0)
  2231. errmsg = "insufficient memory";
  2232. }
  2233. }
  2234. else
  2235. errmsg = png_ptr->zstream.msg;
  2236. }
  2237. if (errmsg != NULL)
  2238. png_chunk_benign_error(png_ptr, errmsg);
  2239. }
  2240. #endif
  2241. #ifdef PNG_READ_iTXt_SUPPORTED
  2242. /* Note: this does not correctly handle chunks that are > 64K under DOS */
  2243. void /* PRIVATE */
  2244. png_handle_iTXt(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
  2245. {
  2246. png_const_charp errmsg = NULL;
  2247. png_bytep buffer;
  2248. png_uint_32 prefix_length;
  2249. png_debug(1, "in png_handle_iTXt");
  2250. #ifdef PNG_USER_LIMITS_SUPPORTED
  2251. if (png_ptr->user_chunk_cache_max != 0)
  2252. {
  2253. if (png_ptr->user_chunk_cache_max == 1)
  2254. {
  2255. png_crc_finish(png_ptr, length);
  2256. return;
  2257. }
  2258. if (--png_ptr->user_chunk_cache_max == 1)
  2259. {
  2260. png_crc_finish(png_ptr, length);
  2261. png_chunk_benign_error(png_ptr, "no space in chunk cache");
  2262. return;
  2263. }
  2264. }
  2265. #endif
  2266. if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
  2267. png_chunk_error(png_ptr, "missing IHDR");
  2268. if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
  2269. png_ptr->mode |= PNG_AFTER_IDAT;
  2270. buffer = png_read_buffer(png_ptr, length+1, 1/*warn*/);
  2271. if (buffer == NULL)
  2272. {
  2273. png_crc_finish(png_ptr, length);
  2274. png_chunk_benign_error(png_ptr, "out of memory");
  2275. return;
  2276. }
  2277. png_crc_read(png_ptr, buffer, length);
  2278. if (png_crc_finish(png_ptr, 0) != 0)
  2279. return;
  2280. /* First the keyword. */
  2281. for (prefix_length=0;
  2282. prefix_length < length && buffer[prefix_length] != 0;
  2283. ++prefix_length)
  2284. /* Empty loop */ ;
  2285. /* Perform a basic check on the keyword length here. */
  2286. if (prefix_length > 79 || prefix_length < 1)
  2287. errmsg = "bad keyword";
  2288. /* Expect keyword, compression flag, compression type, language, translated
  2289. * keyword (both may be empty but are 0 terminated) then the text, which may
  2290. * be empty.
  2291. */
  2292. else if (prefix_length + 5 > length)
  2293. errmsg = "truncated";
  2294. else if (buffer[prefix_length+1] == 0 ||
  2295. (buffer[prefix_length+1] == 1 &&
  2296. buffer[prefix_length+2] == PNG_COMPRESSION_TYPE_BASE))
  2297. {
  2298. int compressed = buffer[prefix_length+1] != 0;
  2299. png_uint_32 language_offset, translated_keyword_offset;
  2300. png_alloc_size_t uncompressed_length = 0;
  2301. /* Now the language tag */
  2302. prefix_length += 3;
  2303. language_offset = prefix_length;
  2304. for (; prefix_length < length && buffer[prefix_length] != 0;
  2305. ++prefix_length)
  2306. /* Empty loop */ ;
  2307. /* WARNING: the length may be invalid here, this is checked below. */
  2308. translated_keyword_offset = ++prefix_length;
  2309. for (; prefix_length < length && buffer[prefix_length] != 0;
  2310. ++prefix_length)
  2311. /* Empty loop */ ;
  2312. /* prefix_length should now be at the trailing '\0' of the translated
  2313. * keyword, but it may already be over the end. None of this arithmetic
  2314. * can overflow because chunks are at most 2^31 bytes long, but on 16-bit
  2315. * systems the available allocation may overflow.
  2316. */
  2317. ++prefix_length;
  2318. if (compressed == 0 && prefix_length <= length)
  2319. uncompressed_length = length - prefix_length;
  2320. else if (compressed != 0 && prefix_length < length)
  2321. {
  2322. uncompressed_length = PNG_SIZE_MAX;
  2323. /* TODO: at present png_decompress_chunk imposes a single application
  2324. * level memory limit, this should be split to different values for
  2325. * iCCP and text chunks.
  2326. */
  2327. if (png_decompress_chunk(png_ptr, length, prefix_length,
  2328. &uncompressed_length, 1/*terminate*/) == Z_STREAM_END)
  2329. buffer = png_ptr->read_buffer;
  2330. else
  2331. errmsg = png_ptr->zstream.msg;
  2332. }
  2333. else
  2334. errmsg = "truncated";
  2335. if (errmsg == NULL)
  2336. {
  2337. png_text text;
  2338. buffer[uncompressed_length+prefix_length] = 0;
  2339. if (compressed == 0)
  2340. text.compression = PNG_ITXT_COMPRESSION_NONE;
  2341. else
  2342. text.compression = PNG_ITXT_COMPRESSION_zTXt;
  2343. text.key = (png_charp)buffer;
  2344. text.lang = (png_charp)buffer + language_offset;
  2345. text.lang_key = (png_charp)buffer + translated_keyword_offset;
  2346. text.text = (png_charp)buffer + prefix_length;
  2347. text.text_length = 0;
  2348. text.itxt_length = uncompressed_length;
  2349. if (png_set_text_2(png_ptr, info_ptr, &text, 1) != 0)
  2350. errmsg = "insufficient memory";
  2351. }
  2352. }
  2353. else
  2354. errmsg = "bad compression info";
  2355. if (errmsg != NULL)
  2356. png_chunk_benign_error(png_ptr, errmsg);
  2357. }
  2358. #endif
  2359. #ifdef PNG_READ_APNG_SUPPORTED
  2360. void /* PRIVATE */
  2361. png_handle_acTL(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  2362. {
  2363. png_byte data[8];
  2364. png_uint_32 num_frames;
  2365. png_uint_32 num_plays;
  2366. png_uint_32 didSet;
  2367. png_debug(1, "in png_handle_acTL");
  2368. if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
  2369. {
  2370. png_error(png_ptr, "Missing IHDR before acTL");
  2371. }
  2372. else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
  2373. {
  2374. png_warning(png_ptr, "Invalid acTL after IDAT skipped");
  2375. png_crc_finish(png_ptr, length);
  2376. return;
  2377. }
  2378. else if ((png_ptr->mode & PNG_HAVE_acTL) != 0)
  2379. {
  2380. png_warning(png_ptr, "Duplicate acTL skipped");
  2381. png_crc_finish(png_ptr, length);
  2382. return;
  2383. }
  2384. else if (length != 8)
  2385. {
  2386. png_warning(png_ptr, "acTL with invalid length skipped");
  2387. png_crc_finish(png_ptr, length);
  2388. return;
  2389. }
  2390. png_crc_read(png_ptr, data, 8);
  2391. png_crc_finish(png_ptr, 0);
  2392. num_frames = png_get_uint_31(png_ptr, data);
  2393. num_plays = png_get_uint_31(png_ptr, data + 4);
  2394. /* the set function will do error checking on num_frames */
  2395. didSet = png_set_acTL(png_ptr, info_ptr, num_frames, num_plays);
  2396. if (didSet != 0)
  2397. png_ptr->mode |= PNG_HAVE_acTL;
  2398. }
  2399. void /* PRIVATE */
  2400. png_handle_fcTL(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  2401. {
  2402. png_byte data[22];
  2403. png_uint_32 width;
  2404. png_uint_32 height;
  2405. png_uint_32 x_offset;
  2406. png_uint_32 y_offset;
  2407. png_uint_16 delay_num;
  2408. png_uint_16 delay_den;
  2409. png_byte dispose_op;
  2410. png_byte blend_op;
  2411. png_debug(1, "in png_handle_fcTL");
  2412. png_ensure_sequence_number(png_ptr, length);
  2413. if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
  2414. {
  2415. png_error(png_ptr, "Missing IHDR before fcTL");
  2416. }
  2417. else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
  2418. {
  2419. /* for any frames other then the first this message may be misleading,
  2420. * but correct. PNG_HAVE_IDAT is unset before the frame head is read
  2421. * i can't think of a better message */
  2422. png_warning(png_ptr, "Invalid fcTL after IDAT skipped");
  2423. png_crc_finish(png_ptr, length-4);
  2424. return;
  2425. }
  2426. else if ((png_ptr->mode & PNG_HAVE_fcTL) != 0)
  2427. {
  2428. png_warning(png_ptr, "Duplicate fcTL within one frame skipped");
  2429. png_crc_finish(png_ptr, length-4);
  2430. return;
  2431. }
  2432. else if (length != 26)
  2433. {
  2434. png_warning(png_ptr, "fcTL with invalid length skipped");
  2435. png_crc_finish(png_ptr, length-4);
  2436. return;
  2437. }
  2438. png_crc_read(png_ptr, data, 22);
  2439. png_crc_finish(png_ptr, 0);
  2440. width = png_get_uint_31(png_ptr, data);
  2441. height = png_get_uint_31(png_ptr, data + 4);
  2442. x_offset = png_get_uint_31(png_ptr, data + 8);
  2443. y_offset = png_get_uint_31(png_ptr, data + 12);
  2444. delay_num = png_get_uint_16(data + 16);
  2445. delay_den = png_get_uint_16(data + 18);
  2446. dispose_op = data[20];
  2447. blend_op = data[21];
  2448. if (png_ptr->num_frames_read == 0 && (x_offset != 0 || y_offset != 0))
  2449. {
  2450. png_warning(png_ptr, "fcTL for the first frame must have zero offset");
  2451. return;
  2452. }
  2453. if (info_ptr != NULL)
  2454. {
  2455. if (png_ptr->num_frames_read == 0 &&
  2456. (width != info_ptr->width || height != info_ptr->height))
  2457. {
  2458. png_warning(png_ptr, "size in first frame's fcTL must match "
  2459. "the size in IHDR");
  2460. return;
  2461. }
  2462. /* The set function will do more error checking */
  2463. png_set_next_frame_fcTL(png_ptr, info_ptr, width, height,
  2464. x_offset, y_offset, delay_num, delay_den,
  2465. dispose_op, blend_op);
  2466. png_read_reinit(png_ptr, info_ptr);
  2467. png_ptr->mode |= PNG_HAVE_fcTL;
  2468. }
  2469. }
  2470. void /* PRIVATE */
  2471. png_have_info(png_structp png_ptr, png_infop info_ptr)
  2472. {
  2473. if ((info_ptr->valid & PNG_INFO_acTL) != 0 &&
  2474. (info_ptr->valid & PNG_INFO_fcTL) == 0)
  2475. {
  2476. png_ptr->apng_flags |= PNG_FIRST_FRAME_HIDDEN;
  2477. info_ptr->num_frames++;
  2478. }
  2479. }
  2480. void /* PRIVATE */
  2481. png_handle_fdAT(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  2482. {
  2483. png_ensure_sequence_number(png_ptr, length);
  2484. /* This function is only called from png_read_end(), png_read_info(),
  2485. * and png_push_read_chunk() which means that:
  2486. * - the user doesn't want to read this frame
  2487. * - or this is an out-of-place fdAT
  2488. * in either case it is safe to ignore the chunk with a warning */
  2489. png_warning(png_ptr, "ignoring fdAT chunk");
  2490. png_crc_finish(png_ptr, length - 4);
  2491. PNG_UNUSED(info_ptr)
  2492. }
  2493. void /* PRIVATE */
  2494. png_ensure_sequence_number(png_structp png_ptr, png_uint_32 length)
  2495. {
  2496. png_byte data[4];
  2497. png_uint_32 sequence_number;
  2498. if (length < 4)
  2499. png_error(png_ptr, "invalid fcTL or fdAT chunk found");
  2500. png_crc_read(png_ptr, data, 4);
  2501. sequence_number = png_get_uint_31(png_ptr, data);
  2502. if (sequence_number != png_ptr->next_seq_num)
  2503. png_error(png_ptr, "fcTL or fdAT chunk with out-of-order sequence "
  2504. "number found");
  2505. png_ptr->next_seq_num++;
  2506. }
  2507. #endif /* READ_APNG */
  2508. #ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED
  2509. /* Utility function for png_handle_unknown; set up png_ptr::unknown_chunk */
  2510. static int
  2511. png_cache_unknown_chunk(png_structrp png_ptr, png_uint_32 length)
  2512. {
  2513. png_alloc_size_t limit = PNG_SIZE_MAX;
  2514. if (png_ptr->unknown_chunk.data != NULL)
  2515. {
  2516. png_free(png_ptr, png_ptr->unknown_chunk.data);
  2517. png_ptr->unknown_chunk.data = NULL;
  2518. }
  2519. # ifdef PNG_SET_USER_LIMITS_SUPPORTED
  2520. if (png_ptr->user_chunk_malloc_max > 0 &&
  2521. png_ptr->user_chunk_malloc_max < limit)
  2522. limit = png_ptr->user_chunk_malloc_max;
  2523. # elif PNG_USER_CHUNK_MALLOC_MAX > 0
  2524. if (PNG_USER_CHUNK_MALLOC_MAX < limit)
  2525. limit = PNG_USER_CHUNK_MALLOC_MAX;
  2526. # endif
  2527. if (length <= limit)
  2528. {
  2529. PNG_CSTRING_FROM_CHUNK(png_ptr->unknown_chunk.name, png_ptr->chunk_name);
  2530. /* The following is safe because of the PNG_SIZE_MAX init above */
  2531. png_ptr->unknown_chunk.size = (size_t)length/*SAFE*/;
  2532. /* 'mode' is a flag array, only the bottom four bits matter here */
  2533. png_ptr->unknown_chunk.location = (png_byte)png_ptr->mode/*SAFE*/;
  2534. if (length == 0)
  2535. png_ptr->unknown_chunk.data = NULL;
  2536. else
  2537. {
  2538. /* Do a 'warn' here - it is handled below. */
  2539. png_ptr->unknown_chunk.data = png_voidcast(png_bytep,
  2540. png_malloc_warn(png_ptr, length));
  2541. }
  2542. }
  2543. if (png_ptr->unknown_chunk.data == NULL && length > 0)
  2544. {
  2545. /* This is benign because we clean up correctly */
  2546. png_crc_finish(png_ptr, length);
  2547. png_chunk_benign_error(png_ptr, "unknown chunk exceeds memory limits");
  2548. return 0;
  2549. }
  2550. else
  2551. {
  2552. if (length > 0)
  2553. png_crc_read(png_ptr, png_ptr->unknown_chunk.data, length);
  2554. png_crc_finish(png_ptr, 0);
  2555. return 1;
  2556. }
  2557. }
  2558. #endif /* READ_UNKNOWN_CHUNKS */
  2559. /* Handle an unknown, or known but disabled, chunk */
  2560. void /* PRIVATE */
  2561. png_handle_unknown(png_structrp png_ptr, png_inforp info_ptr,
  2562. png_uint_32 length, int keep)
  2563. {
  2564. int handled = 0; /* the chunk was handled */
  2565. png_debug(1, "in png_handle_unknown");
  2566. #ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED
  2567. /* NOTE: this code is based on the code in libpng-1.4.12 except for fixing
  2568. * the bug which meant that setting a non-default behavior for a specific
  2569. * chunk would be ignored (the default was always used unless a user
  2570. * callback was installed).
  2571. *
  2572. * 'keep' is the value from the png_chunk_unknown_handling, the setting for
  2573. * this specific chunk_name, if PNG_HANDLE_AS_UNKNOWN_SUPPORTED, if not it
  2574. * will always be PNG_HANDLE_CHUNK_AS_DEFAULT and it needs to be set here.
  2575. * This is just an optimization to avoid multiple calls to the lookup
  2576. * function.
  2577. */
  2578. # ifndef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
  2579. # ifdef PNG_SET_UNKNOWN_CHUNKS_SUPPORTED
  2580. keep = png_chunk_unknown_handling(png_ptr, png_ptr->chunk_name);
  2581. # endif
  2582. # endif
  2583. /* One of the following methods will read the chunk or skip it (at least one
  2584. * of these is always defined because this is the only way to switch on
  2585. * PNG_READ_UNKNOWN_CHUNKS_SUPPORTED)
  2586. */
  2587. # ifdef PNG_READ_USER_CHUNKS_SUPPORTED
  2588. /* The user callback takes precedence over the chunk keep value, but the
  2589. * keep value is still required to validate a save of a critical chunk.
  2590. */
  2591. if (png_ptr->read_user_chunk_fn != NULL)
  2592. {
  2593. if (png_cache_unknown_chunk(png_ptr, length) != 0)
  2594. {
  2595. /* Callback to user unknown chunk handler */
  2596. int ret = (*(png_ptr->read_user_chunk_fn))(png_ptr,
  2597. &png_ptr->unknown_chunk);
  2598. /* ret is:
  2599. * negative: An error occurred; png_chunk_error will be called.
  2600. * zero: The chunk was not handled, the chunk will be discarded
  2601. * unless png_set_keep_unknown_chunks has been used to set
  2602. * a 'keep' behavior for this particular chunk, in which
  2603. * case that will be used. A critical chunk will cause an
  2604. * error at this point unless it is to be saved.
  2605. * positive: The chunk was handled, libpng will ignore/discard it.
  2606. */
  2607. if (ret < 0)
  2608. png_chunk_error(png_ptr, "error in user chunk");
  2609. else if (ret == 0)
  2610. {
  2611. /* If the keep value is 'default' or 'never' override it, but
  2612. * still error out on critical chunks unless the keep value is
  2613. * 'always' While this is weird it is the behavior in 1.4.12.
  2614. * A possible improvement would be to obey the value set for the
  2615. * chunk, but this would be an API change that would probably
  2616. * damage some applications.
  2617. *
  2618. * The png_app_warning below catches the case that matters, where
  2619. * the application has not set specific save or ignore for this
  2620. * chunk or global save or ignore.
  2621. */
  2622. if (keep < PNG_HANDLE_CHUNK_IF_SAFE)
  2623. {
  2624. # ifdef PNG_SET_UNKNOWN_CHUNKS_SUPPORTED
  2625. if (png_ptr->unknown_default < PNG_HANDLE_CHUNK_IF_SAFE)
  2626. {
  2627. png_chunk_warning(png_ptr, "Saving unknown chunk:");
  2628. png_app_warning(png_ptr,
  2629. "forcing save of an unhandled chunk;"
  2630. " please call png_set_keep_unknown_chunks");
  2631. /* with keep = PNG_HANDLE_CHUNK_IF_SAFE */
  2632. }
  2633. # endif
  2634. keep = PNG_HANDLE_CHUNK_IF_SAFE;
  2635. }
  2636. }
  2637. else /* chunk was handled */
  2638. {
  2639. handled = 1;
  2640. /* Critical chunks can be safely discarded at this point. */
  2641. keep = PNG_HANDLE_CHUNK_NEVER;
  2642. }
  2643. }
  2644. else
  2645. keep = PNG_HANDLE_CHUNK_NEVER; /* insufficient memory */
  2646. }
  2647. else
  2648. /* Use the SAVE_UNKNOWN_CHUNKS code or skip the chunk */
  2649. # endif /* READ_USER_CHUNKS */
  2650. # ifdef PNG_SAVE_UNKNOWN_CHUNKS_SUPPORTED
  2651. {
  2652. /* keep is currently just the per-chunk setting, if there was no
  2653. * setting change it to the global default now (not that this may
  2654. * still be AS_DEFAULT) then obtain the cache of the chunk if required,
  2655. * if not simply skip the chunk.
  2656. */
  2657. if (keep == PNG_HANDLE_CHUNK_AS_DEFAULT)
  2658. keep = png_ptr->unknown_default;
  2659. if (keep == PNG_HANDLE_CHUNK_ALWAYS ||
  2660. (keep == PNG_HANDLE_CHUNK_IF_SAFE &&
  2661. PNG_CHUNK_ANCILLARY(png_ptr->chunk_name)))
  2662. {
  2663. if (png_cache_unknown_chunk(png_ptr, length) == 0)
  2664. keep = PNG_HANDLE_CHUNK_NEVER;
  2665. }
  2666. else
  2667. png_crc_finish(png_ptr, length);
  2668. }
  2669. # else
  2670. # ifndef PNG_READ_USER_CHUNKS_SUPPORTED
  2671. # error no method to support READ_UNKNOWN_CHUNKS
  2672. # endif
  2673. {
  2674. /* If here there is no read callback pointer set and no support is
  2675. * compiled in to just save the unknown chunks, so simply skip this
  2676. * chunk. If 'keep' is something other than AS_DEFAULT or NEVER then
  2677. * the app has erroneously asked for unknown chunk saving when there
  2678. * is no support.
  2679. */
  2680. if (keep > PNG_HANDLE_CHUNK_NEVER)
  2681. png_app_error(png_ptr, "no unknown chunk support available");
  2682. png_crc_finish(png_ptr, length);
  2683. }
  2684. # endif
  2685. # ifdef PNG_STORE_UNKNOWN_CHUNKS_SUPPORTED
  2686. /* Now store the chunk in the chunk list if appropriate, and if the limits
  2687. * permit it.
  2688. */
  2689. if (keep == PNG_HANDLE_CHUNK_ALWAYS ||
  2690. (keep == PNG_HANDLE_CHUNK_IF_SAFE &&
  2691. PNG_CHUNK_ANCILLARY(png_ptr->chunk_name)))
  2692. {
  2693. # ifdef PNG_USER_LIMITS_SUPPORTED
  2694. switch (png_ptr->user_chunk_cache_max)
  2695. {
  2696. case 2:
  2697. png_ptr->user_chunk_cache_max = 1;
  2698. png_chunk_benign_error(png_ptr, "no space in chunk cache");
  2699. /* FALLTHROUGH */
  2700. case 1:
  2701. /* NOTE: prior to 1.6.0 this case resulted in an unknown critical
  2702. * chunk being skipped, now there will be a hard error below.
  2703. */
  2704. break;
  2705. default: /* not at limit */
  2706. --(png_ptr->user_chunk_cache_max);
  2707. /* FALLTHROUGH */
  2708. case 0: /* no limit */
  2709. # endif /* USER_LIMITS */
  2710. /* Here when the limit isn't reached or when limits are compiled
  2711. * out; store the chunk.
  2712. */
  2713. png_set_unknown_chunks(png_ptr, info_ptr,
  2714. &png_ptr->unknown_chunk, 1);
  2715. handled = 1;
  2716. # ifdef PNG_USER_LIMITS_SUPPORTED
  2717. break;
  2718. }
  2719. # endif
  2720. }
  2721. # else /* no store support: the chunk must be handled by the user callback */
  2722. PNG_UNUSED(info_ptr)
  2723. # endif
  2724. /* Regardless of the error handling below the cached data (if any) can be
  2725. * freed now. Notice that the data is not freed if there is a png_error, but
  2726. * it will be freed by destroy_read_struct.
  2727. */
  2728. if (png_ptr->unknown_chunk.data != NULL)
  2729. png_free(png_ptr, png_ptr->unknown_chunk.data);
  2730. png_ptr->unknown_chunk.data = NULL;
  2731. #else /* !PNG_READ_UNKNOWN_CHUNKS_SUPPORTED */
  2732. /* There is no support to read an unknown chunk, so just skip it. */
  2733. png_crc_finish(png_ptr, length);
  2734. PNG_UNUSED(info_ptr)
  2735. PNG_UNUSED(keep)
  2736. #endif /* !READ_UNKNOWN_CHUNKS */
  2737. /* Check for unhandled critical chunks */
  2738. if (handled == 0 && PNG_CHUNK_CRITICAL(png_ptr->chunk_name))
  2739. png_chunk_error(png_ptr, "unhandled critical chunk");
  2740. }
  2741. /* This function is called to verify that a chunk name is valid.
  2742. * This function can't have the "critical chunk check" incorporated
  2743. * into it, since in the future we will need to be able to call user
  2744. * functions to handle unknown critical chunks after we check that
  2745. * the chunk name itself is valid.
  2746. */
  2747. /* Bit hacking: the test for an invalid byte in the 4 byte chunk name is:
  2748. *
  2749. * ((c) < 65 || (c) > 122 || ((c) > 90 && (c) < 97))
  2750. */
  2751. void /* PRIVATE */
  2752. png_check_chunk_name(png_const_structrp png_ptr, png_uint_32 chunk_name)
  2753. {
  2754. int i;
  2755. png_uint_32 cn=chunk_name;
  2756. png_debug(1, "in png_check_chunk_name");
  2757. for (i=1; i<=4; ++i)
  2758. {
  2759. int c = cn & 0xff;
  2760. if (c < 65 || c > 122 || (c > 90 && c < 97))
  2761. png_chunk_error(png_ptr, "invalid chunk type");
  2762. cn >>= 8;
  2763. }
  2764. }
  2765. void /* PRIVATE */
  2766. png_check_chunk_length(png_const_structrp png_ptr, png_uint_32 length)
  2767. {
  2768. png_alloc_size_t limit = PNG_UINT_31_MAX;
  2769. # ifdef PNG_SET_USER_LIMITS_SUPPORTED
  2770. if (png_ptr->user_chunk_malloc_max > 0 &&
  2771. png_ptr->user_chunk_malloc_max < limit)
  2772. limit = png_ptr->user_chunk_malloc_max;
  2773. # elif PNG_USER_CHUNK_MALLOC_MAX > 0
  2774. if (PNG_USER_CHUNK_MALLOC_MAX < limit)
  2775. limit = PNG_USER_CHUNK_MALLOC_MAX;
  2776. # endif
  2777. #ifdef PNG_READ_APNG_SUPPORTED
  2778. if (png_ptr->chunk_name == png_IDAT || png_ptr->chunk_name == png_fdAT)
  2779. #else
  2780. if (png_ptr->chunk_name == png_IDAT)
  2781. #endif
  2782. {
  2783. png_alloc_size_t idat_limit = PNG_UINT_31_MAX;
  2784. size_t row_factor =
  2785. (size_t)png_ptr->width
  2786. * (size_t)png_ptr->channels
  2787. * (png_ptr->bit_depth > 8? 2: 1)
  2788. + 1
  2789. + (png_ptr->interlaced? 6: 0);
  2790. if (png_ptr->height > PNG_UINT_32_MAX/row_factor)
  2791. idat_limit = PNG_UINT_31_MAX;
  2792. else
  2793. idat_limit = png_ptr->height * row_factor;
  2794. row_factor = row_factor > 32566? 32566 : row_factor;
  2795. idat_limit += 6 + 5*(idat_limit/row_factor+1); /* zlib+deflate overhead */
  2796. idat_limit=idat_limit < PNG_UINT_31_MAX? idat_limit : PNG_UINT_31_MAX;
  2797. limit = limit < idat_limit? idat_limit : limit;
  2798. }
  2799. if (length > limit)
  2800. {
  2801. png_debug2(0," length = %lu, limit = %lu",
  2802. (unsigned long)length,(unsigned long)limit);
  2803. png_benign_error(png_ptr, "chunk data is too large");
  2804. }
  2805. }
  2806. /* Combines the row recently read in with the existing pixels in the row. This
  2807. * routine takes care of alpha and transparency if requested. This routine also
  2808. * handles the two methods of progressive display of interlaced images,
  2809. * depending on the 'display' value; if 'display' is true then the whole row
  2810. * (dp) is filled from the start by replicating the available pixels. If
  2811. * 'display' is false only those pixels present in the pass are filled in.
  2812. */
  2813. void /* PRIVATE */
  2814. png_combine_row(png_const_structrp png_ptr, png_bytep dp, int display)
  2815. {
  2816. unsigned int pixel_depth = png_ptr->transformed_pixel_depth;
  2817. png_const_bytep sp = png_ptr->row_buf + 1;
  2818. png_alloc_size_t row_width = png_ptr->width;
  2819. unsigned int pass = png_ptr->pass;
  2820. png_bytep end_ptr = 0;
  2821. png_byte end_byte = 0;
  2822. unsigned int end_mask;
  2823. png_debug(1, "in png_combine_row");
  2824. /* Added in 1.5.6: it should not be possible to enter this routine until at
  2825. * least one row has been read from the PNG data and transformed.
  2826. */
  2827. if (pixel_depth == 0)
  2828. png_error(png_ptr, "internal row logic error");
  2829. /* Added in 1.5.4: the pixel depth should match the information returned by
  2830. * any call to png_read_update_info at this point. Do not continue if we got
  2831. * this wrong.
  2832. */
  2833. if (png_ptr->info_rowbytes != 0 && png_ptr->info_rowbytes !=
  2834. PNG_ROWBYTES(pixel_depth, row_width))
  2835. png_error(png_ptr, "internal row size calculation error");
  2836. /* Don't expect this to ever happen: */
  2837. if (row_width == 0)
  2838. png_error(png_ptr, "internal row width error");
  2839. /* Preserve the last byte in cases where only part of it will be overwritten,
  2840. * the multiply below may overflow, we don't care because ANSI-C guarantees
  2841. * we get the low bits.
  2842. */
  2843. end_mask = (pixel_depth * row_width) & 7;
  2844. if (end_mask != 0)
  2845. {
  2846. /* end_ptr == NULL is a flag to say do nothing */
  2847. end_ptr = dp + PNG_ROWBYTES(pixel_depth, row_width) - 1;
  2848. end_byte = *end_ptr;
  2849. # ifdef PNG_READ_PACKSWAP_SUPPORTED
  2850. if ((png_ptr->transformations & PNG_PACKSWAP) != 0)
  2851. /* little-endian byte */
  2852. end_mask = (unsigned int)(0xff << end_mask);
  2853. else /* big-endian byte */
  2854. # endif
  2855. end_mask = 0xff >> end_mask;
  2856. /* end_mask is now the bits to *keep* from the destination row */
  2857. }
  2858. /* For non-interlaced images this reduces to a memcpy(). A memcpy()
  2859. * will also happen if interlacing isn't supported or if the application
  2860. * does not call png_set_interlace_handling(). In the latter cases the
  2861. * caller just gets a sequence of the unexpanded rows from each interlace
  2862. * pass.
  2863. */
  2864. #ifdef PNG_READ_INTERLACING_SUPPORTED
  2865. if (png_ptr->interlaced != 0 &&
  2866. (png_ptr->transformations & PNG_INTERLACE) != 0 &&
  2867. pass < 6 && (display == 0 ||
  2868. /* The following copies everything for 'display' on passes 0, 2 and 4. */
  2869. (display == 1 && (pass & 1) != 0)))
  2870. {
  2871. /* Narrow images may have no bits in a pass; the caller should handle
  2872. * this, but this test is cheap:
  2873. */
  2874. if (row_width <= PNG_PASS_START_COL(pass))
  2875. return;
  2876. if (pixel_depth < 8)
  2877. {
  2878. /* For pixel depths up to 4 bpp the 8-pixel mask can be expanded to fit
  2879. * into 32 bits, then a single loop over the bytes using the four byte
  2880. * values in the 32-bit mask can be used. For the 'display' option the
  2881. * expanded mask may also not require any masking within a byte. To
  2882. * make this work the PACKSWAP option must be taken into account - it
  2883. * simply requires the pixels to be reversed in each byte.
  2884. *
  2885. * The 'regular' case requires a mask for each of the first 6 passes,
  2886. * the 'display' case does a copy for the even passes in the range
  2887. * 0..6. This has already been handled in the test above.
  2888. *
  2889. * The masks are arranged as four bytes with the first byte to use in
  2890. * the lowest bits (little-endian) regardless of the order (PACKSWAP or
  2891. * not) of the pixels in each byte.
  2892. *
  2893. * NOTE: the whole of this logic depends on the caller of this function
  2894. * only calling it on rows appropriate to the pass. This function only
  2895. * understands the 'x' logic; the 'y' logic is handled by the caller.
  2896. *
  2897. * The following defines allow generation of compile time constant bit
  2898. * masks for each pixel depth and each possibility of swapped or not
  2899. * swapped bytes. Pass 'p' is in the range 0..6; 'x', a pixel index,
  2900. * is in the range 0..7; and the result is 1 if the pixel is to be
  2901. * copied in the pass, 0 if not. 'S' is for the sparkle method, 'B'
  2902. * for the block method.
  2903. *
  2904. * With some compilers a compile time expression of the general form:
  2905. *
  2906. * (shift >= 32) ? (a >> (shift-32)) : (b >> shift)
  2907. *
  2908. * Produces warnings with values of 'shift' in the range 33 to 63
  2909. * because the right hand side of the ?: expression is evaluated by
  2910. * the compiler even though it isn't used. Microsoft Visual C (various
  2911. * versions) and the Intel C compiler are known to do this. To avoid
  2912. * this the following macros are used in 1.5.6. This is a temporary
  2913. * solution to avoid destabilizing the code during the release process.
  2914. */
  2915. # if PNG_USE_COMPILE_TIME_MASKS
  2916. # define PNG_LSR(x,s) ((x)>>((s) & 0x1f))
  2917. # define PNG_LSL(x,s) ((x)<<((s) & 0x1f))
  2918. # else
  2919. # define PNG_LSR(x,s) ((x)>>(s))
  2920. # define PNG_LSL(x,s) ((x)<<(s))
  2921. # endif
  2922. # define S_COPY(p,x) (((p)<4 ? PNG_LSR(0x80088822,(3-(p))*8+(7-(x))) :\
  2923. PNG_LSR(0xaa55ff00,(7-(p))*8+(7-(x)))) & 1)
  2924. # define B_COPY(p,x) (((p)<4 ? PNG_LSR(0xff0fff33,(3-(p))*8+(7-(x))) :\
  2925. PNG_LSR(0xff55ff00,(7-(p))*8+(7-(x)))) & 1)
  2926. /* Return a mask for pass 'p' pixel 'x' at depth 'd'. The mask is
  2927. * little endian - the first pixel is at bit 0 - however the extra
  2928. * parameter 's' can be set to cause the mask position to be swapped
  2929. * within each byte, to match the PNG format. This is done by XOR of
  2930. * the shift with 7, 6 or 4 for bit depths 1, 2 and 4.
  2931. */
  2932. # define PIXEL_MASK(p,x,d,s) \
  2933. (PNG_LSL(((PNG_LSL(1U,(d)))-1),(((x)*(d))^((s)?8-(d):0))))
  2934. /* Hence generate the appropriate 'block' or 'sparkle' pixel copy mask.
  2935. */
  2936. # define S_MASKx(p,x,d,s) (S_COPY(p,x)?PIXEL_MASK(p,x,d,s):0)
  2937. # define B_MASKx(p,x,d,s) (B_COPY(p,x)?PIXEL_MASK(p,x,d,s):0)
  2938. /* Combine 8 of these to get the full mask. For the 1-bpp and 2-bpp
  2939. * cases the result needs replicating, for the 4-bpp case the above
  2940. * generates a full 32 bits.
  2941. */
  2942. # define MASK_EXPAND(m,d) ((m)*((d)==1?0x01010101:((d)==2?0x00010001:1)))
  2943. # define S_MASK(p,d,s) MASK_EXPAND(S_MASKx(p,0,d,s) + S_MASKx(p,1,d,s) +\
  2944. S_MASKx(p,2,d,s) + S_MASKx(p,3,d,s) + S_MASKx(p,4,d,s) +\
  2945. S_MASKx(p,5,d,s) + S_MASKx(p,6,d,s) + S_MASKx(p,7,d,s), d)
  2946. # define B_MASK(p,d,s) MASK_EXPAND(B_MASKx(p,0,d,s) + B_MASKx(p,1,d,s) +\
  2947. B_MASKx(p,2,d,s) + B_MASKx(p,3,d,s) + B_MASKx(p,4,d,s) +\
  2948. B_MASKx(p,5,d,s) + B_MASKx(p,6,d,s) + B_MASKx(p,7,d,s), d)
  2949. #if PNG_USE_COMPILE_TIME_MASKS
  2950. /* Utility macros to construct all the masks for a depth/swap
  2951. * combination. The 's' parameter says whether the format is PNG
  2952. * (big endian bytes) or not. Only the three odd-numbered passes are
  2953. * required for the display/block algorithm.
  2954. */
  2955. # define S_MASKS(d,s) { S_MASK(0,d,s), S_MASK(1,d,s), S_MASK(2,d,s),\
  2956. S_MASK(3,d,s), S_MASK(4,d,s), S_MASK(5,d,s) }
  2957. # define B_MASKS(d,s) { B_MASK(1,d,s), B_MASK(3,d,s), B_MASK(5,d,s) }
  2958. # define DEPTH_INDEX(d) ((d)==1?0:((d)==2?1:2))
  2959. /* Hence the pre-compiled masks indexed by PACKSWAP (or not), depth and
  2960. * then pass:
  2961. */
  2962. static const png_uint_32 row_mask[2/*PACKSWAP*/][3/*depth*/][6] =
  2963. {
  2964. /* Little-endian byte masks for PACKSWAP */
  2965. { S_MASKS(1,0), S_MASKS(2,0), S_MASKS(4,0) },
  2966. /* Normal (big-endian byte) masks - PNG format */
  2967. { S_MASKS(1,1), S_MASKS(2,1), S_MASKS(4,1) }
  2968. };
  2969. /* display_mask has only three entries for the odd passes, so index by
  2970. * pass>>1.
  2971. */
  2972. static const png_uint_32 display_mask[2][3][3] =
  2973. {
  2974. /* Little-endian byte masks for PACKSWAP */
  2975. { B_MASKS(1,0), B_MASKS(2,0), B_MASKS(4,0) },
  2976. /* Normal (big-endian byte) masks - PNG format */
  2977. { B_MASKS(1,1), B_MASKS(2,1), B_MASKS(4,1) }
  2978. };
  2979. # define MASK(pass,depth,display,png)\
  2980. ((display)?display_mask[png][DEPTH_INDEX(depth)][pass>>1]:\
  2981. row_mask[png][DEPTH_INDEX(depth)][pass])
  2982. #else /* !PNG_USE_COMPILE_TIME_MASKS */
  2983. /* This is the runtime alternative: it seems unlikely that this will
  2984. * ever be either smaller or faster than the compile time approach.
  2985. */
  2986. # define MASK(pass,depth,display,png)\
  2987. ((display)?B_MASK(pass,depth,png):S_MASK(pass,depth,png))
  2988. #endif /* !USE_COMPILE_TIME_MASKS */
  2989. /* Use the appropriate mask to copy the required bits. In some cases
  2990. * the byte mask will be 0 or 0xff; optimize these cases. row_width is
  2991. * the number of pixels, but the code copies bytes, so it is necessary
  2992. * to special case the end.
  2993. */
  2994. png_uint_32 pixels_per_byte = 8 / pixel_depth;
  2995. png_uint_32 mask;
  2996. # ifdef PNG_READ_PACKSWAP_SUPPORTED
  2997. if ((png_ptr->transformations & PNG_PACKSWAP) != 0)
  2998. mask = MASK(pass, pixel_depth, display, 0);
  2999. else
  3000. # endif
  3001. mask = MASK(pass, pixel_depth, display, 1);
  3002. for (;;)
  3003. {
  3004. png_uint_32 m;
  3005. /* It doesn't matter in the following if png_uint_32 has more than
  3006. * 32 bits because the high bits always match those in m<<24; it is,
  3007. * however, essential to use OR here, not +, because of this.
  3008. */
  3009. m = mask;
  3010. mask = (m >> 8) | (m << 24); /* rotate right to good compilers */
  3011. m &= 0xff;
  3012. if (m != 0) /* something to copy */
  3013. {
  3014. if (m != 0xff)
  3015. *dp = (png_byte)((*dp & ~m) | (*sp & m));
  3016. else
  3017. *dp = *sp;
  3018. }
  3019. /* NOTE: this may overwrite the last byte with garbage if the image
  3020. * is not an exact number of bytes wide; libpng has always done
  3021. * this.
  3022. */
  3023. if (row_width <= pixels_per_byte)
  3024. break; /* May need to restore part of the last byte */
  3025. row_width -= pixels_per_byte;
  3026. ++dp;
  3027. ++sp;
  3028. }
  3029. }
  3030. else /* pixel_depth >= 8 */
  3031. {
  3032. unsigned int bytes_to_copy, bytes_to_jump;
  3033. /* Validate the depth - it must be a multiple of 8 */
  3034. if (pixel_depth & 7)
  3035. png_error(png_ptr, "invalid user transform pixel depth");
  3036. pixel_depth >>= 3; /* now in bytes */
  3037. row_width *= pixel_depth;
  3038. /* Regardless of pass number the Adam 7 interlace always results in a
  3039. * fixed number of pixels to copy then to skip. There may be a
  3040. * different number of pixels to skip at the start though.
  3041. */
  3042. {
  3043. unsigned int offset = PNG_PASS_START_COL(pass) * pixel_depth;
  3044. row_width -= offset;
  3045. dp += offset;
  3046. sp += offset;
  3047. }
  3048. /* Work out the bytes to copy. */
  3049. if (display != 0)
  3050. {
  3051. /* When doing the 'block' algorithm the pixel in the pass gets
  3052. * replicated to adjacent pixels. This is why the even (0,2,4,6)
  3053. * passes are skipped above - the entire expanded row is copied.
  3054. */
  3055. bytes_to_copy = (1<<((6-pass)>>1)) * pixel_depth;
  3056. /* But don't allow this number to exceed the actual row width. */
  3057. if (bytes_to_copy > row_width)
  3058. bytes_to_copy = (unsigned int)/*SAFE*/row_width;
  3059. }
  3060. else /* normal row; Adam7 only ever gives us one pixel to copy. */
  3061. bytes_to_copy = pixel_depth;
  3062. /* In Adam7 there is a constant offset between where the pixels go. */
  3063. bytes_to_jump = PNG_PASS_COL_OFFSET(pass) * pixel_depth;
  3064. /* And simply copy these bytes. Some optimization is possible here,
  3065. * depending on the value of 'bytes_to_copy'. Special case the low
  3066. * byte counts, which we know to be frequent.
  3067. *
  3068. * Notice that these cases all 'return' rather than 'break' - this
  3069. * avoids an unnecessary test on whether to restore the last byte
  3070. * below.
  3071. */
  3072. switch (bytes_to_copy)
  3073. {
  3074. case 1:
  3075. for (;;)
  3076. {
  3077. *dp = *sp;
  3078. if (row_width <= bytes_to_jump)
  3079. return;
  3080. dp += bytes_to_jump;
  3081. sp += bytes_to_jump;
  3082. row_width -= bytes_to_jump;
  3083. }
  3084. case 2:
  3085. /* There is a possibility of a partial copy at the end here; this
  3086. * slows the code down somewhat.
  3087. */
  3088. do
  3089. {
  3090. dp[0] = sp[0]; dp[1] = sp[1];
  3091. if (row_width <= bytes_to_jump)
  3092. return;
  3093. sp += bytes_to_jump;
  3094. dp += bytes_to_jump;
  3095. row_width -= bytes_to_jump;
  3096. }
  3097. while (row_width > 1);
  3098. /* And there can only be one byte left at this point: */
  3099. *dp = *sp;
  3100. return;
  3101. case 3:
  3102. /* This can only be the RGB case, so each copy is exactly one
  3103. * pixel and it is not necessary to check for a partial copy.
  3104. */
  3105. for (;;)
  3106. {
  3107. dp[0] = sp[0]; dp[1] = sp[1]; dp[2] = sp[2];
  3108. if (row_width <= bytes_to_jump)
  3109. return;
  3110. sp += bytes_to_jump;
  3111. dp += bytes_to_jump;
  3112. row_width -= bytes_to_jump;
  3113. }
  3114. default:
  3115. #if PNG_ALIGN_TYPE != PNG_ALIGN_NONE
  3116. /* Check for double byte alignment and, if possible, use a
  3117. * 16-bit copy. Don't attempt this for narrow images - ones that
  3118. * are less than an interlace panel wide. Don't attempt it for
  3119. * wide bytes_to_copy either - use the memcpy there.
  3120. */
  3121. if (bytes_to_copy < 16 /*else use memcpy*/ &&
  3122. png_isaligned(dp, png_uint_16) &&
  3123. png_isaligned(sp, png_uint_16) &&
  3124. bytes_to_copy % (sizeof (png_uint_16)) == 0 &&
  3125. bytes_to_jump % (sizeof (png_uint_16)) == 0)
  3126. {
  3127. /* Everything is aligned for png_uint_16 copies, but try for
  3128. * png_uint_32 first.
  3129. */
  3130. if (png_isaligned(dp, png_uint_32) &&
  3131. png_isaligned(sp, png_uint_32) &&
  3132. bytes_to_copy % (sizeof (png_uint_32)) == 0 &&
  3133. bytes_to_jump % (sizeof (png_uint_32)) == 0)
  3134. {
  3135. png_uint_32p dp32 = png_aligncast(png_uint_32p,dp);
  3136. png_const_uint_32p sp32 = png_aligncastconst(
  3137. png_const_uint_32p, sp);
  3138. size_t skip = (bytes_to_jump-bytes_to_copy) /
  3139. (sizeof (png_uint_32));
  3140. do
  3141. {
  3142. size_t c = bytes_to_copy;
  3143. do
  3144. {
  3145. *dp32++ = *sp32++;
  3146. c -= (sizeof (png_uint_32));
  3147. }
  3148. while (c > 0);
  3149. if (row_width <= bytes_to_jump)
  3150. return;
  3151. dp32 += skip;
  3152. sp32 += skip;
  3153. row_width -= bytes_to_jump;
  3154. }
  3155. while (bytes_to_copy <= row_width);
  3156. /* Get to here when the row_width truncates the final copy.
  3157. * There will be 1-3 bytes left to copy, so don't try the
  3158. * 16-bit loop below.
  3159. */
  3160. dp = (png_bytep)dp32;
  3161. sp = (png_const_bytep)sp32;
  3162. do
  3163. *dp++ = *sp++;
  3164. while (--row_width > 0);
  3165. return;
  3166. }
  3167. /* Else do it in 16-bit quantities, but only if the size is
  3168. * not too large.
  3169. */
  3170. else
  3171. {
  3172. png_uint_16p dp16 = png_aligncast(png_uint_16p, dp);
  3173. png_const_uint_16p sp16 = png_aligncastconst(
  3174. png_const_uint_16p, sp);
  3175. size_t skip = (bytes_to_jump-bytes_to_copy) /
  3176. (sizeof (png_uint_16));
  3177. do
  3178. {
  3179. size_t c = bytes_to_copy;
  3180. do
  3181. {
  3182. *dp16++ = *sp16++;
  3183. c -= (sizeof (png_uint_16));
  3184. }
  3185. while (c > 0);
  3186. if (row_width <= bytes_to_jump)
  3187. return;
  3188. dp16 += skip;
  3189. sp16 += skip;
  3190. row_width -= bytes_to_jump;
  3191. }
  3192. while (bytes_to_copy <= row_width);
  3193. /* End of row - 1 byte left, bytes_to_copy > row_width: */
  3194. dp = (png_bytep)dp16;
  3195. sp = (png_const_bytep)sp16;
  3196. do
  3197. *dp++ = *sp++;
  3198. while (--row_width > 0);
  3199. return;
  3200. }
  3201. }
  3202. #endif /* ALIGN_TYPE code */
  3203. /* The true default - use a memcpy: */
  3204. for (;;)
  3205. {
  3206. memcpy(dp, sp, bytes_to_copy);
  3207. if (row_width <= bytes_to_jump)
  3208. return;
  3209. sp += bytes_to_jump;
  3210. dp += bytes_to_jump;
  3211. row_width -= bytes_to_jump;
  3212. if (bytes_to_copy > row_width)
  3213. bytes_to_copy = (unsigned int)/*SAFE*/row_width;
  3214. }
  3215. }
  3216. /* NOT REACHED*/
  3217. } /* pixel_depth >= 8 */
  3218. /* Here if pixel_depth < 8 to check 'end_ptr' below. */
  3219. }
  3220. else
  3221. #endif /* READ_INTERLACING */
  3222. /* If here then the switch above wasn't used so just memcpy the whole row
  3223. * from the temporary row buffer (notice that this overwrites the end of the
  3224. * destination row if it is a partial byte.)
  3225. */
  3226. memcpy(dp, sp, PNG_ROWBYTES(pixel_depth, row_width));
  3227. /* Restore the overwritten bits from the last byte if necessary. */
  3228. if (end_ptr != NULL)
  3229. *end_ptr = (png_byte)((end_byte & end_mask) | (*end_ptr & ~end_mask));
  3230. }
  3231. #ifdef PNG_READ_INTERLACING_SUPPORTED
  3232. void /* PRIVATE */
  3233. png_do_read_interlace(png_row_infop row_info, png_bytep row, int pass,
  3234. png_uint_32 transformations /* Because these may affect the byte layout */)
  3235. {
  3236. /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
  3237. /* Offset to next interlace block */
  3238. static const unsigned int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
  3239. png_debug(1, "in png_do_read_interlace");
  3240. if (row != NULL && row_info != NULL)
  3241. {
  3242. png_uint_32 final_width;
  3243. final_width = row_info->width * png_pass_inc[pass];
  3244. switch (row_info->pixel_depth)
  3245. {
  3246. case 1:
  3247. {
  3248. png_bytep sp = row + (size_t)((row_info->width - 1) >> 3);
  3249. png_bytep dp = row + (size_t)((final_width - 1) >> 3);
  3250. unsigned int sshift, dshift;
  3251. unsigned int s_start, s_end;
  3252. int s_inc;
  3253. int jstop = (int)png_pass_inc[pass];
  3254. png_byte v;
  3255. png_uint_32 i;
  3256. int j;
  3257. #ifdef PNG_READ_PACKSWAP_SUPPORTED
  3258. if ((transformations & PNG_PACKSWAP) != 0)
  3259. {
  3260. sshift = ((row_info->width + 7) & 0x07);
  3261. dshift = ((final_width + 7) & 0x07);
  3262. s_start = 7;
  3263. s_end = 0;
  3264. s_inc = -1;
  3265. }
  3266. else
  3267. #endif
  3268. {
  3269. sshift = 7 - ((row_info->width + 7) & 0x07);
  3270. dshift = 7 - ((final_width + 7) & 0x07);
  3271. s_start = 0;
  3272. s_end = 7;
  3273. s_inc = 1;
  3274. }
  3275. for (i = 0; i < row_info->width; i++)
  3276. {
  3277. v = (png_byte)((*sp >> sshift) & 0x01);
  3278. for (j = 0; j < jstop; j++)
  3279. {
  3280. unsigned int tmp = *dp & (0x7f7f >> (7 - dshift));
  3281. tmp |= (unsigned int)(v << dshift);
  3282. *dp = (png_byte)(tmp & 0xff);
  3283. if (dshift == s_end)
  3284. {
  3285. dshift = s_start;
  3286. dp--;
  3287. }
  3288. else
  3289. dshift = (unsigned int)((int)dshift + s_inc);
  3290. }
  3291. if (sshift == s_end)
  3292. {
  3293. sshift = s_start;
  3294. sp--;
  3295. }
  3296. else
  3297. sshift = (unsigned int)((int)sshift + s_inc);
  3298. }
  3299. break;
  3300. }
  3301. case 2:
  3302. {
  3303. png_bytep sp = row + (png_uint_32)((row_info->width - 1) >> 2);
  3304. png_bytep dp = row + (png_uint_32)((final_width - 1) >> 2);
  3305. unsigned int sshift, dshift;
  3306. unsigned int s_start, s_end;
  3307. int s_inc;
  3308. int jstop = (int)png_pass_inc[pass];
  3309. png_uint_32 i;
  3310. #ifdef PNG_READ_PACKSWAP_SUPPORTED
  3311. if ((transformations & PNG_PACKSWAP) != 0)
  3312. {
  3313. sshift = (((row_info->width + 3) & 0x03) << 1);
  3314. dshift = (((final_width + 3) & 0x03) << 1);
  3315. s_start = 6;
  3316. s_end = 0;
  3317. s_inc = -2;
  3318. }
  3319. else
  3320. #endif
  3321. {
  3322. sshift = ((3 - ((row_info->width + 3) & 0x03)) << 1);
  3323. dshift = ((3 - ((final_width + 3) & 0x03)) << 1);
  3324. s_start = 0;
  3325. s_end = 6;
  3326. s_inc = 2;
  3327. }
  3328. for (i = 0; i < row_info->width; i++)
  3329. {
  3330. png_byte v;
  3331. int j;
  3332. v = (png_byte)((*sp >> sshift) & 0x03);
  3333. for (j = 0; j < jstop; j++)
  3334. {
  3335. unsigned int tmp = *dp & (0x3f3f >> (6 - dshift));
  3336. tmp |= (unsigned int)(v << dshift);
  3337. *dp = (png_byte)(tmp & 0xff);
  3338. if (dshift == s_end)
  3339. {
  3340. dshift = s_start;
  3341. dp--;
  3342. }
  3343. else
  3344. dshift = (unsigned int)((int)dshift + s_inc);
  3345. }
  3346. if (sshift == s_end)
  3347. {
  3348. sshift = s_start;
  3349. sp--;
  3350. }
  3351. else
  3352. sshift = (unsigned int)((int)sshift + s_inc);
  3353. }
  3354. break;
  3355. }
  3356. case 4:
  3357. {
  3358. png_bytep sp = row + (size_t)((row_info->width - 1) >> 1);
  3359. png_bytep dp = row + (size_t)((final_width - 1) >> 1);
  3360. unsigned int sshift, dshift;
  3361. unsigned int s_start, s_end;
  3362. int s_inc;
  3363. png_uint_32 i;
  3364. int jstop = (int)png_pass_inc[pass];
  3365. #ifdef PNG_READ_PACKSWAP_SUPPORTED
  3366. if ((transformations & PNG_PACKSWAP) != 0)
  3367. {
  3368. sshift = (((row_info->width + 1) & 0x01) << 2);
  3369. dshift = (((final_width + 1) & 0x01) << 2);
  3370. s_start = 4;
  3371. s_end = 0;
  3372. s_inc = -4;
  3373. }
  3374. else
  3375. #endif
  3376. {
  3377. sshift = ((1 - ((row_info->width + 1) & 0x01)) << 2);
  3378. dshift = ((1 - ((final_width + 1) & 0x01)) << 2);
  3379. s_start = 0;
  3380. s_end = 4;
  3381. s_inc = 4;
  3382. }
  3383. for (i = 0; i < row_info->width; i++)
  3384. {
  3385. png_byte v = (png_byte)((*sp >> sshift) & 0x0f);
  3386. int j;
  3387. for (j = 0; j < jstop; j++)
  3388. {
  3389. unsigned int tmp = *dp & (0xf0f >> (4 - dshift));
  3390. tmp |= (unsigned int)(v << dshift);
  3391. *dp = (png_byte)(tmp & 0xff);
  3392. if (dshift == s_end)
  3393. {
  3394. dshift = s_start;
  3395. dp--;
  3396. }
  3397. else
  3398. dshift = (unsigned int)((int)dshift + s_inc);
  3399. }
  3400. if (sshift == s_end)
  3401. {
  3402. sshift = s_start;
  3403. sp--;
  3404. }
  3405. else
  3406. sshift = (unsigned int)((int)sshift + s_inc);
  3407. }
  3408. break;
  3409. }
  3410. default:
  3411. {
  3412. size_t pixel_bytes = (row_info->pixel_depth >> 3);
  3413. png_bytep sp = row + (size_t)(row_info->width - 1)
  3414. * pixel_bytes;
  3415. png_bytep dp = row + (size_t)(final_width - 1) * pixel_bytes;
  3416. int jstop = (int)png_pass_inc[pass];
  3417. png_uint_32 i;
  3418. for (i = 0; i < row_info->width; i++)
  3419. {
  3420. png_byte v[8]; /* SAFE; pixel_depth does not exceed 64 */
  3421. int j;
  3422. memcpy(v, sp, pixel_bytes);
  3423. for (j = 0; j < jstop; j++)
  3424. {
  3425. memcpy(dp, v, pixel_bytes);
  3426. dp -= pixel_bytes;
  3427. }
  3428. sp -= pixel_bytes;
  3429. }
  3430. break;
  3431. }
  3432. }
  3433. row_info->width = final_width;
  3434. row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, final_width);
  3435. }
  3436. #ifndef PNG_READ_PACKSWAP_SUPPORTED
  3437. PNG_UNUSED(transformations) /* Silence compiler warning */
  3438. #endif
  3439. }
  3440. #endif /* READ_INTERLACING */
  3441. static void
  3442. png_read_filter_row_sub(png_row_infop row_info, png_bytep row,
  3443. png_const_bytep prev_row)
  3444. {
  3445. size_t i;
  3446. size_t istop = row_info->rowbytes;
  3447. unsigned int bpp = (row_info->pixel_depth + 7) >> 3;
  3448. png_bytep rp = row + bpp;
  3449. PNG_UNUSED(prev_row)
  3450. for (i = bpp; i < istop; i++)
  3451. {
  3452. *rp = (png_byte)(((int)(*rp) + (int)(*(rp-bpp))) & 0xff);
  3453. rp++;
  3454. }
  3455. }
  3456. static void
  3457. png_read_filter_row_up(png_row_infop row_info, png_bytep row,
  3458. png_const_bytep prev_row)
  3459. {
  3460. size_t i;
  3461. size_t istop = row_info->rowbytes;
  3462. png_bytep rp = row;
  3463. png_const_bytep pp = prev_row;
  3464. for (i = 0; i < istop; i++)
  3465. {
  3466. *rp = (png_byte)(((int)(*rp) + (int)(*pp++)) & 0xff);
  3467. rp++;
  3468. }
  3469. }
  3470. static void
  3471. png_read_filter_row_avg(png_row_infop row_info, png_bytep row,
  3472. png_const_bytep prev_row)
  3473. {
  3474. size_t i;
  3475. png_bytep rp = row;
  3476. png_const_bytep pp = prev_row;
  3477. unsigned int bpp = (row_info->pixel_depth + 7) >> 3;
  3478. size_t istop = row_info->rowbytes - bpp;
  3479. for (i = 0; i < bpp; i++)
  3480. {
  3481. *rp = (png_byte)(((int)(*rp) +
  3482. ((int)(*pp++) / 2 )) & 0xff);
  3483. rp++;
  3484. }
  3485. for (i = 0; i < istop; i++)
  3486. {
  3487. *rp = (png_byte)(((int)(*rp) +
  3488. (int)(*pp++ + *(rp-bpp)) / 2 ) & 0xff);
  3489. rp++;
  3490. }
  3491. }
  3492. static void
  3493. png_read_filter_row_paeth_1byte_pixel(png_row_infop row_info, png_bytep row,
  3494. png_const_bytep prev_row)
  3495. {
  3496. png_bytep rp_end = row + row_info->rowbytes;
  3497. int a, c;
  3498. /* First pixel/byte */
  3499. c = *prev_row++;
  3500. a = *row + c;
  3501. *row++ = (png_byte)a;
  3502. /* Remainder */
  3503. while (row < rp_end)
  3504. {
  3505. int b, pa, pb, pc, p;
  3506. a &= 0xff; /* From previous iteration or start */
  3507. b = *prev_row++;
  3508. p = b - c;
  3509. pc = a - c;
  3510. #ifdef PNG_USE_ABS
  3511. pa = abs(p);
  3512. pb = abs(pc);
  3513. pc = abs(p + pc);
  3514. #else
  3515. pa = p < 0 ? -p : p;
  3516. pb = pc < 0 ? -pc : pc;
  3517. pc = (p + pc) < 0 ? -(p + pc) : p + pc;
  3518. #endif
  3519. /* Find the best predictor, the least of pa, pb, pc favoring the earlier
  3520. * ones in the case of a tie.
  3521. */
  3522. if (pb < pa)
  3523. {
  3524. pa = pb; a = b;
  3525. }
  3526. if (pc < pa) a = c;
  3527. /* Calculate the current pixel in a, and move the previous row pixel to c
  3528. * for the next time round the loop
  3529. */
  3530. c = b;
  3531. a += *row;
  3532. *row++ = (png_byte)a;
  3533. }
  3534. }
  3535. static void
  3536. png_read_filter_row_paeth_multibyte_pixel(png_row_infop row_info, png_bytep row,
  3537. png_const_bytep prev_row)
  3538. {
  3539. unsigned int bpp = (row_info->pixel_depth + 7) >> 3;
  3540. png_bytep rp_end = row + bpp;
  3541. /* Process the first pixel in the row completely (this is the same as 'up'
  3542. * because there is only one candidate predictor for the first row).
  3543. */
  3544. while (row < rp_end)
  3545. {
  3546. int a = *row + *prev_row++;
  3547. *row++ = (png_byte)a;
  3548. }
  3549. /* Remainder */
  3550. rp_end = rp_end + (row_info->rowbytes - bpp);
  3551. while (row < rp_end)
  3552. {
  3553. int a, b, c, pa, pb, pc, p;
  3554. c = *(prev_row - bpp);
  3555. a = *(row - bpp);
  3556. b = *prev_row++;
  3557. p = b - c;
  3558. pc = a - c;
  3559. #ifdef PNG_USE_ABS
  3560. pa = abs(p);
  3561. pb = abs(pc);
  3562. pc = abs(p + pc);
  3563. #else
  3564. pa = p < 0 ? -p : p;
  3565. pb = pc < 0 ? -pc : pc;
  3566. pc = (p + pc) < 0 ? -(p + pc) : p + pc;
  3567. #endif
  3568. if (pb < pa)
  3569. {
  3570. pa = pb; a = b;
  3571. }
  3572. if (pc < pa) a = c;
  3573. a += *row;
  3574. *row++ = (png_byte)a;
  3575. }
  3576. }
  3577. static void
  3578. png_init_filter_functions(png_structrp pp)
  3579. /* This function is called once for every PNG image (except for PNG images
  3580. * that only use PNG_FILTER_VALUE_NONE for all rows) to set the
  3581. * implementations required to reverse the filtering of PNG rows. Reversing
  3582. * the filter is the first transformation performed on the row data. It is
  3583. * performed in place, therefore an implementation can be selected based on
  3584. * the image pixel format. If the implementation depends on image width then
  3585. * take care to ensure that it works correctly if the image is interlaced -
  3586. * interlacing causes the actual row width to vary.
  3587. */
  3588. {
  3589. unsigned int bpp = (pp->pixel_depth + 7) >> 3;
  3590. pp->read_filter[PNG_FILTER_VALUE_SUB-1] = png_read_filter_row_sub;
  3591. pp->read_filter[PNG_FILTER_VALUE_UP-1] = png_read_filter_row_up;
  3592. pp->read_filter[PNG_FILTER_VALUE_AVG-1] = png_read_filter_row_avg;
  3593. if (bpp == 1)
  3594. pp->read_filter[PNG_FILTER_VALUE_PAETH-1] =
  3595. png_read_filter_row_paeth_1byte_pixel;
  3596. else
  3597. pp->read_filter[PNG_FILTER_VALUE_PAETH-1] =
  3598. png_read_filter_row_paeth_multibyte_pixel;
  3599. #ifdef PNG_FILTER_OPTIMIZATIONS
  3600. /* To use this define PNG_FILTER_OPTIMIZATIONS as the name of a function to
  3601. * call to install hardware optimizations for the above functions; simply
  3602. * replace whatever elements of the pp->read_filter[] array with a hardware
  3603. * specific (or, for that matter, generic) optimization.
  3604. *
  3605. * To see an example of this examine what configure.ac does when
  3606. * --enable-arm-neon is specified on the command line.
  3607. */
  3608. PNG_FILTER_OPTIMIZATIONS(pp, bpp);
  3609. #endif
  3610. }
  3611. void /* PRIVATE */
  3612. png_read_filter_row(png_structrp pp, png_row_infop row_info, png_bytep row,
  3613. png_const_bytep prev_row, int filter)
  3614. {
  3615. /* OPTIMIZATION: DO NOT MODIFY THIS FUNCTION, instead #define
  3616. * PNG_FILTER_OPTIMIZATIONS to a function that overrides the generic
  3617. * implementations. See png_init_filter_functions above.
  3618. */
  3619. if (filter > PNG_FILTER_VALUE_NONE && filter < PNG_FILTER_VALUE_LAST)
  3620. {
  3621. if (pp->read_filter[0] == NULL)
  3622. png_init_filter_functions(pp);
  3623. pp->read_filter[filter-1](row_info, row, prev_row);
  3624. }
  3625. }
  3626. #ifdef PNG_SEQUENTIAL_READ_SUPPORTED
  3627. void /* PRIVATE */
  3628. png_read_IDAT_data(png_structrp png_ptr, png_bytep output,
  3629. png_alloc_size_t avail_out)
  3630. {
  3631. /* Loop reading IDATs and decompressing the result into output[avail_out] */
  3632. png_ptr->zstream.next_out = output;
  3633. png_ptr->zstream.avail_out = 0; /* safety: set below */
  3634. if (output == NULL)
  3635. avail_out = 0;
  3636. do
  3637. {
  3638. int ret;
  3639. png_byte tmpbuf[PNG_INFLATE_BUF_SIZE];
  3640. if (png_ptr->zstream.avail_in == 0)
  3641. {
  3642. uInt avail_in;
  3643. png_bytep buffer;
  3644. #ifdef PNG_READ_APNG_SUPPORTED
  3645. png_uint_32 bytes_to_skip = 0;
  3646. while (png_ptr->idat_size == 0 || bytes_to_skip != 0)
  3647. {
  3648. png_crc_finish(png_ptr, bytes_to_skip);
  3649. bytes_to_skip = 0;
  3650. png_ptr->idat_size = png_read_chunk_header(png_ptr);
  3651. if (png_ptr->num_frames_read == 0)
  3652. {
  3653. if (png_ptr->chunk_name != png_IDAT)
  3654. png_error(png_ptr, "Not enough image data");
  3655. }
  3656. else
  3657. {
  3658. if (png_ptr->chunk_name == png_IEND)
  3659. png_error(png_ptr, "Not enough image data");
  3660. if (png_ptr->chunk_name != png_fdAT)
  3661. {
  3662. png_warning(png_ptr, "Skipped (ignored) a chunk "
  3663. "between APNG chunks");
  3664. bytes_to_skip = png_ptr->idat_size;
  3665. continue;
  3666. }
  3667. png_ensure_sequence_number(png_ptr, png_ptr->idat_size);
  3668. png_ptr->idat_size -= 4;
  3669. }
  3670. }
  3671. #else
  3672. while (png_ptr->idat_size == 0)
  3673. {
  3674. png_crc_finish(png_ptr, 0);
  3675. png_ptr->idat_size = png_read_chunk_header(png_ptr);
  3676. /* This is an error even in the 'check' case because the code just
  3677. * consumed a non-IDAT header.
  3678. */
  3679. if (png_ptr->chunk_name != png_IDAT)
  3680. png_error(png_ptr, "Not enough image data");
  3681. }
  3682. #endif /* READ_APNG */
  3683. avail_in = png_ptr->IDAT_read_size;
  3684. if (avail_in > png_ptr->idat_size)
  3685. avail_in = (uInt)png_ptr->idat_size;
  3686. /* A PNG with a gradually increasing IDAT size will defeat this attempt
  3687. * to minimize memory usage by causing lots of re-allocs, but
  3688. * realistically doing IDAT_read_size re-allocs is not likely to be a
  3689. * big problem.
  3690. */
  3691. buffer = png_read_buffer(png_ptr, avail_in, 0/*error*/);
  3692. png_crc_read(png_ptr, buffer, avail_in);
  3693. png_ptr->idat_size -= avail_in;
  3694. png_ptr->zstream.next_in = buffer;
  3695. png_ptr->zstream.avail_in = avail_in;
  3696. }
  3697. /* And set up the output side. */
  3698. if (output != NULL) /* standard read */
  3699. {
  3700. uInt out = ZLIB_IO_MAX;
  3701. if (out > avail_out)
  3702. out = (uInt)avail_out;
  3703. avail_out -= out;
  3704. png_ptr->zstream.avail_out = out;
  3705. }
  3706. else /* after last row, checking for end */
  3707. {
  3708. png_ptr->zstream.next_out = tmpbuf;
  3709. png_ptr->zstream.avail_out = (sizeof tmpbuf);
  3710. }
  3711. /* Use NO_FLUSH; this gives zlib the maximum opportunity to optimize the
  3712. * process. If the LZ stream is truncated the sequential reader will
  3713. * terminally damage the stream, above, by reading the chunk header of the
  3714. * following chunk (it then exits with png_error).
  3715. *
  3716. * TODO: deal more elegantly with truncated IDAT lists.
  3717. */
  3718. ret = PNG_INFLATE(png_ptr, Z_NO_FLUSH);
  3719. /* Take the unconsumed output back. */
  3720. if (output != NULL)
  3721. avail_out += png_ptr->zstream.avail_out;
  3722. else /* avail_out counts the extra bytes */
  3723. avail_out += (sizeof tmpbuf) - png_ptr->zstream.avail_out;
  3724. png_ptr->zstream.avail_out = 0;
  3725. if (ret == Z_STREAM_END)
  3726. {
  3727. /* Do this for safety; we won't read any more into this row. */
  3728. png_ptr->zstream.next_out = NULL;
  3729. png_ptr->mode |= PNG_AFTER_IDAT;
  3730. png_ptr->flags |= PNG_FLAG_ZSTREAM_ENDED;
  3731. #ifdef PNG_READ_APNG_SUPPORTED
  3732. png_ptr->num_frames_read++;
  3733. #endif
  3734. if (png_ptr->zstream.avail_in > 0 || png_ptr->idat_size > 0)
  3735. png_chunk_benign_error(png_ptr, "Extra compressed data");
  3736. break;
  3737. }
  3738. if (ret != Z_OK)
  3739. {
  3740. png_zstream_error(png_ptr, ret);
  3741. if (output != NULL)
  3742. png_chunk_error(png_ptr, png_ptr->zstream.msg);
  3743. else /* checking */
  3744. {
  3745. png_chunk_benign_error(png_ptr, png_ptr->zstream.msg);
  3746. return;
  3747. }
  3748. }
  3749. } while (avail_out > 0);
  3750. if (avail_out > 0)
  3751. {
  3752. /* The stream ended before the image; this is the same as too few IDATs so
  3753. * should be handled the same way.
  3754. */
  3755. if (output != NULL)
  3756. png_error(png_ptr, "Not enough image data");
  3757. else /* the deflate stream contained extra data */
  3758. png_chunk_benign_error(png_ptr, "Too much image data");
  3759. }
  3760. }
  3761. void /* PRIVATE */
  3762. png_read_finish_IDAT(png_structrp png_ptr)
  3763. {
  3764. /* We don't need any more data and the stream should have ended, however the
  3765. * LZ end code may actually not have been processed. In this case we must
  3766. * read it otherwise stray unread IDAT data or, more likely, an IDAT chunk
  3767. * may still remain to be consumed.
  3768. */
  3769. if ((png_ptr->flags & PNG_FLAG_ZSTREAM_ENDED) == 0)
  3770. {
  3771. /* The NULL causes png_read_IDAT_data to swallow any remaining bytes in
  3772. * the compressed stream, but the stream may be damaged too, so even after
  3773. * this call we may need to terminate the zstream ownership.
  3774. */
  3775. png_read_IDAT_data(png_ptr, NULL, 0);
  3776. png_ptr->zstream.next_out = NULL; /* safety */
  3777. /* Now clear everything out for safety; the following may not have been
  3778. * done.
  3779. */
  3780. if ((png_ptr->flags & PNG_FLAG_ZSTREAM_ENDED) == 0)
  3781. {
  3782. png_ptr->mode |= PNG_AFTER_IDAT;
  3783. png_ptr->flags |= PNG_FLAG_ZSTREAM_ENDED;
  3784. }
  3785. }
  3786. /* If the zstream has not been released do it now *and* terminate the reading
  3787. * of the final IDAT chunk.
  3788. */
  3789. if (png_ptr->zowner == png_IDAT)
  3790. {
  3791. /* Always do this; the pointers otherwise point into the read buffer. */
  3792. png_ptr->zstream.next_in = NULL;
  3793. png_ptr->zstream.avail_in = 0;
  3794. /* Now we no longer own the zstream. */
  3795. png_ptr->zowner = 0;
  3796. /* The slightly weird semantics of the sequential IDAT reading is that we
  3797. * are always in or at the end of an IDAT chunk, so we always need to do a
  3798. * crc_finish here. If idat_size is non-zero we also need to read the
  3799. * spurious bytes at the end of the chunk now.
  3800. */
  3801. (void)png_crc_finish(png_ptr, png_ptr->idat_size);
  3802. }
  3803. }
  3804. void /* PRIVATE */
  3805. png_read_finish_row(png_structrp png_ptr)
  3806. {
  3807. /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
  3808. /* Start of interlace block */
  3809. static const png_byte png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
  3810. /* Offset to next interlace block */
  3811. static const png_byte png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
  3812. /* Start of interlace block in the y direction */
  3813. static const png_byte png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
  3814. /* Offset to next interlace block in the y direction */
  3815. static const png_byte png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
  3816. png_debug(1, "in png_read_finish_row");
  3817. png_ptr->row_number++;
  3818. if (png_ptr->row_number < png_ptr->num_rows)
  3819. return;
  3820. if (png_ptr->interlaced != 0)
  3821. {
  3822. png_ptr->row_number = 0;
  3823. /* TO DO: don't do this if prev_row isn't needed (requires
  3824. * read-ahead of the next row's filter byte.
  3825. */
  3826. memset(png_ptr->prev_row, 0, png_ptr->rowbytes + 1);
  3827. do
  3828. {
  3829. png_ptr->pass++;
  3830. if (png_ptr->pass >= 7)
  3831. break;
  3832. png_ptr->iwidth = (png_ptr->width +
  3833. png_pass_inc[png_ptr->pass] - 1 -
  3834. png_pass_start[png_ptr->pass]) /
  3835. png_pass_inc[png_ptr->pass];
  3836. if ((png_ptr->transformations & PNG_INTERLACE) == 0)
  3837. {
  3838. png_ptr->num_rows = (png_ptr->height +
  3839. png_pass_yinc[png_ptr->pass] - 1 -
  3840. png_pass_ystart[png_ptr->pass]) /
  3841. png_pass_yinc[png_ptr->pass];
  3842. }
  3843. else /* if (png_ptr->transformations & PNG_INTERLACE) */
  3844. break; /* libpng deinterlacing sees every row */
  3845. } while (png_ptr->num_rows == 0 || png_ptr->iwidth == 0);
  3846. if (png_ptr->pass < 7)
  3847. return;
  3848. }
  3849. /* Here after at the end of the last row of the last pass. */
  3850. png_read_finish_IDAT(png_ptr);
  3851. }
  3852. #endif /* SEQUENTIAL_READ */
  3853. void /* PRIVATE */
  3854. png_read_start_row(png_structrp png_ptr)
  3855. {
  3856. /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
  3857. /* Start of interlace block */
  3858. static const png_byte png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
  3859. /* Offset to next interlace block */
  3860. static const png_byte png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
  3861. /* Start of interlace block in the y direction */
  3862. static const png_byte png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
  3863. /* Offset to next interlace block in the y direction */
  3864. static const png_byte png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
  3865. unsigned int max_pixel_depth;
  3866. size_t row_bytes;
  3867. png_debug(1, "in png_read_start_row");
  3868. #ifdef PNG_READ_TRANSFORMS_SUPPORTED
  3869. png_init_read_transformations(png_ptr);
  3870. #endif
  3871. if (png_ptr->interlaced != 0)
  3872. {
  3873. if ((png_ptr->transformations & PNG_INTERLACE) == 0)
  3874. png_ptr->num_rows = (png_ptr->height + png_pass_yinc[0] - 1 -
  3875. png_pass_ystart[0]) / png_pass_yinc[0];
  3876. else
  3877. png_ptr->num_rows = png_ptr->height;
  3878. png_ptr->iwidth = (png_ptr->width +
  3879. png_pass_inc[png_ptr->pass] - 1 -
  3880. png_pass_start[png_ptr->pass]) /
  3881. png_pass_inc[png_ptr->pass];
  3882. }
  3883. else
  3884. {
  3885. png_ptr->num_rows = png_ptr->height;
  3886. png_ptr->iwidth = png_ptr->width;
  3887. }
  3888. max_pixel_depth = (unsigned int)png_ptr->pixel_depth;
  3889. /* WARNING: * png_read_transform_info (pngrtran.c) performs a simpler set of
  3890. * calculations to calculate the final pixel depth, then
  3891. * png_do_read_transforms actually does the transforms. This means that the
  3892. * code which effectively calculates this value is actually repeated in three
  3893. * separate places. They must all match. Innocent changes to the order of
  3894. * transformations can and will break libpng in a way that causes memory
  3895. * overwrites.
  3896. *
  3897. * TODO: fix this.
  3898. */
  3899. #ifdef PNG_READ_PACK_SUPPORTED
  3900. if ((png_ptr->transformations & PNG_PACK) != 0 && png_ptr->bit_depth < 8)
  3901. max_pixel_depth = 8;
  3902. #endif
  3903. #ifdef PNG_READ_EXPAND_SUPPORTED
  3904. if ((png_ptr->transformations & PNG_EXPAND) != 0)
  3905. {
  3906. if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
  3907. {
  3908. if (png_ptr->num_trans != 0)
  3909. max_pixel_depth = 32;
  3910. else
  3911. max_pixel_depth = 24;
  3912. }
  3913. else if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY)
  3914. {
  3915. if (max_pixel_depth < 8)
  3916. max_pixel_depth = 8;
  3917. if (png_ptr->num_trans != 0)
  3918. max_pixel_depth *= 2;
  3919. }
  3920. else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB)
  3921. {
  3922. if (png_ptr->num_trans != 0)
  3923. {
  3924. max_pixel_depth *= 4;
  3925. max_pixel_depth /= 3;
  3926. }
  3927. }
  3928. }
  3929. #endif
  3930. #ifdef PNG_READ_EXPAND_16_SUPPORTED
  3931. if ((png_ptr->transformations & PNG_EXPAND_16) != 0)
  3932. {
  3933. # ifdef PNG_READ_EXPAND_SUPPORTED
  3934. /* In fact it is an error if it isn't supported, but checking is
  3935. * the safe way.
  3936. */
  3937. if ((png_ptr->transformations & PNG_EXPAND) != 0)
  3938. {
  3939. if (png_ptr->bit_depth < 16)
  3940. max_pixel_depth *= 2;
  3941. }
  3942. else
  3943. # endif
  3944. png_ptr->transformations &= ~PNG_EXPAND_16;
  3945. }
  3946. #endif
  3947. #ifdef PNG_READ_FILLER_SUPPORTED
  3948. if ((png_ptr->transformations & (PNG_FILLER)) != 0)
  3949. {
  3950. if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY)
  3951. {
  3952. if (max_pixel_depth <= 8)
  3953. max_pixel_depth = 16;
  3954. else
  3955. max_pixel_depth = 32;
  3956. }
  3957. else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB ||
  3958. png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
  3959. {
  3960. if (max_pixel_depth <= 32)
  3961. max_pixel_depth = 32;
  3962. else
  3963. max_pixel_depth = 64;
  3964. }
  3965. }
  3966. #endif
  3967. #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
  3968. if ((png_ptr->transformations & PNG_GRAY_TO_RGB) != 0)
  3969. {
  3970. if (
  3971. #ifdef PNG_READ_EXPAND_SUPPORTED
  3972. (png_ptr->num_trans != 0 &&
  3973. (png_ptr->transformations & PNG_EXPAND) != 0) ||
  3974. #endif
  3975. #ifdef PNG_READ_FILLER_SUPPORTED
  3976. (png_ptr->transformations & (PNG_FILLER)) != 0 ||
  3977. #endif
  3978. png_ptr->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
  3979. {
  3980. if (max_pixel_depth <= 16)
  3981. max_pixel_depth = 32;
  3982. else
  3983. max_pixel_depth = 64;
  3984. }
  3985. else
  3986. {
  3987. if (max_pixel_depth <= 8)
  3988. {
  3989. if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
  3990. max_pixel_depth = 32;
  3991. else
  3992. max_pixel_depth = 24;
  3993. }
  3994. else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
  3995. max_pixel_depth = 64;
  3996. else
  3997. max_pixel_depth = 48;
  3998. }
  3999. }
  4000. #endif
  4001. #if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) && \
  4002. defined(PNG_USER_TRANSFORM_PTR_SUPPORTED)
  4003. if ((png_ptr->transformations & PNG_USER_TRANSFORM) != 0)
  4004. {
  4005. unsigned int user_pixel_depth = png_ptr->user_transform_depth *
  4006. png_ptr->user_transform_channels;
  4007. if (user_pixel_depth > max_pixel_depth)
  4008. max_pixel_depth = user_pixel_depth;
  4009. }
  4010. #endif
  4011. /* This value is stored in png_struct and double checked in the row read
  4012. * code.
  4013. */
  4014. png_ptr->maximum_pixel_depth = (png_byte)max_pixel_depth;
  4015. png_ptr->transformed_pixel_depth = 0; /* calculated on demand */
  4016. /* Align the width on the next larger 8 pixels. Mainly used
  4017. * for interlacing
  4018. */
  4019. row_bytes = ((png_ptr->width + 7) & ~((png_uint_32)7));
  4020. /* Calculate the maximum bytes needed, adding a byte and a pixel
  4021. * for safety's sake
  4022. */
  4023. row_bytes = PNG_ROWBYTES(max_pixel_depth, row_bytes) +
  4024. 1 + ((max_pixel_depth + 7) >> 3U);
  4025. #ifdef PNG_MAX_MALLOC_64K
  4026. if (row_bytes > (png_uint_32)65536L)
  4027. png_error(png_ptr, "This image requires a row greater than 64KB");
  4028. #endif
  4029. if (row_bytes + 48 > png_ptr->old_big_row_buf_size)
  4030. {
  4031. png_free(png_ptr, png_ptr->big_row_buf);
  4032. png_free(png_ptr, png_ptr->big_prev_row);
  4033. if (png_ptr->interlaced != 0)
  4034. png_ptr->big_row_buf = (png_bytep)png_calloc(png_ptr,
  4035. row_bytes + 48);
  4036. else
  4037. png_ptr->big_row_buf = (png_bytep)png_malloc(png_ptr, row_bytes + 48);
  4038. png_ptr->big_prev_row = (png_bytep)png_malloc(png_ptr, row_bytes + 48);
  4039. #ifdef PNG_ALIGNED_MEMORY_SUPPORTED
  4040. /* Use 16-byte aligned memory for row_buf with at least 16 bytes
  4041. * of padding before and after row_buf; treat prev_row similarly.
  4042. * NOTE: the alignment is to the start of the pixels, one beyond the start
  4043. * of the buffer, because of the filter byte. Prior to libpng 1.5.6 this
  4044. * was incorrect; the filter byte was aligned, which had the exact
  4045. * opposite effect of that intended.
  4046. */
  4047. {
  4048. png_bytep temp = png_ptr->big_row_buf + 32;
  4049. size_t extra = (size_t)temp & 0x0f;
  4050. png_ptr->row_buf = temp - extra - 1/*filter byte*/;
  4051. temp = png_ptr->big_prev_row + 32;
  4052. extra = (size_t)temp & 0x0f;
  4053. png_ptr->prev_row = temp - extra - 1/*filter byte*/;
  4054. }
  4055. #else
  4056. /* Use 31 bytes of padding before and 17 bytes after row_buf. */
  4057. png_ptr->row_buf = png_ptr->big_row_buf + 31;
  4058. png_ptr->prev_row = png_ptr->big_prev_row + 31;
  4059. #endif
  4060. png_ptr->old_big_row_buf_size = row_bytes + 48;
  4061. }
  4062. #ifdef PNG_MAX_MALLOC_64K
  4063. if (png_ptr->rowbytes > 65535)
  4064. png_error(png_ptr, "This image requires a row greater than 64KB");
  4065. #endif
  4066. if (png_ptr->rowbytes > (PNG_SIZE_MAX - 1))
  4067. png_error(png_ptr, "Row has too many bytes to allocate in memory");
  4068. memset(png_ptr->prev_row, 0, png_ptr->rowbytes + 1);
  4069. png_debug1(3, "width = %u,", png_ptr->width);
  4070. png_debug1(3, "height = %u,", png_ptr->height);
  4071. png_debug1(3, "iwidth = %u,", png_ptr->iwidth);
  4072. png_debug1(3, "num_rows = %u,", png_ptr->num_rows);
  4073. png_debug1(3, "rowbytes = %lu,", (unsigned long)png_ptr->rowbytes);
  4074. png_debug1(3, "irowbytes = %lu",
  4075. (unsigned long)PNG_ROWBYTES(png_ptr->pixel_depth, png_ptr->iwidth) + 1);
  4076. /* The sequential reader needs a buffer for IDAT, but the progressive reader
  4077. * does not, so free the read buffer now regardless; the sequential reader
  4078. * reallocates it on demand.
  4079. */
  4080. if (png_ptr->read_buffer != NULL)
  4081. {
  4082. png_bytep buffer = png_ptr->read_buffer;
  4083. png_ptr->read_buffer_size = 0;
  4084. png_ptr->read_buffer = NULL;
  4085. png_free(png_ptr, buffer);
  4086. }
  4087. /* Finally claim the zstream for the inflate of the IDAT data, use the bits
  4088. * value from the stream (note that this will result in a fatal error if the
  4089. * IDAT stream has a bogus deflate header window_bits value, but this should
  4090. * not be happening any longer!)
  4091. */
  4092. if (png_inflate_claim(png_ptr, png_IDAT) != Z_OK)
  4093. png_error(png_ptr, png_ptr->zstream.msg);
  4094. png_ptr->flags |= PNG_FLAG_ROW_INIT;
  4095. }
  4096. #ifdef PNG_READ_APNG_SUPPORTED
  4097. /* This function is to be called after the main IDAT set has been read and
  4098. * before a new IDAT is read. It resets some parts of png_ptr
  4099. * to make them usable by the read functions again */
  4100. void /* PRIVATE */
  4101. png_read_reset(png_structp png_ptr)
  4102. {
  4103. png_ptr->mode &= ~PNG_HAVE_IDAT;
  4104. png_ptr->mode &= ~PNG_AFTER_IDAT;
  4105. png_ptr->row_number = 0;
  4106. png_ptr->pass = 0;
  4107. }
  4108. void /* PRIVATE */
  4109. png_read_reinit(png_structp png_ptr, png_infop info_ptr)
  4110. {
  4111. png_ptr->width = info_ptr->next_frame_width;
  4112. png_ptr->height = info_ptr->next_frame_height;
  4113. png_ptr->rowbytes = PNG_ROWBYTES(png_ptr->pixel_depth,png_ptr->width);
  4114. png_ptr->info_rowbytes = PNG_ROWBYTES(info_ptr->pixel_depth,
  4115. png_ptr->width);
  4116. if (png_ptr->prev_row != NULL)
  4117. memset(png_ptr->prev_row, 0, png_ptr->rowbytes + 1);
  4118. }
  4119. #ifdef PNG_PROGRESSIVE_READ_SUPPORTED
  4120. /* same as png_read_reset() but for the progressive reader */
  4121. void /* PRIVATE */
  4122. png_progressive_read_reset(png_structp png_ptr)
  4123. {
  4124. #ifdef PNG_READ_INTERLACING_SUPPORTED
  4125. /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
  4126. /* Start of interlace block */
  4127. static const png_byte png_pass_start[] = {0, 4, 0, 2, 0, 1, 0};
  4128. /* Offset to next interlace block */
  4129. static const png_byte png_pass_inc[] = {8, 8, 4, 4, 2, 2, 1};
  4130. /* Start of interlace block in the y direction */
  4131. static const png_byte png_pass_ystart[] = {0, 0, 4, 0, 2, 0, 1};
  4132. /* Offset to next interlace block in the y direction */
  4133. static const png_byte png_pass_yinc[] = {8, 8, 8, 4, 4, 2, 2};
  4134. if (png_ptr->interlaced != 0)
  4135. {
  4136. if ((png_ptr->transformations & PNG_INTERLACE) == 0)
  4137. png_ptr->num_rows = (png_ptr->height + png_pass_yinc[0] - 1 -
  4138. png_pass_ystart[0]) / png_pass_yinc[0];
  4139. else
  4140. png_ptr->num_rows = png_ptr->height;
  4141. png_ptr->iwidth = (png_ptr->width +
  4142. png_pass_inc[png_ptr->pass] - 1 -
  4143. png_pass_start[png_ptr->pass]) /
  4144. png_pass_inc[png_ptr->pass];
  4145. }
  4146. else
  4147. #endif /* READ_INTERLACING */
  4148. {
  4149. png_ptr->num_rows = png_ptr->height;
  4150. png_ptr->iwidth = png_ptr->width;
  4151. }
  4152. png_ptr->flags &= ~PNG_FLAG_ZSTREAM_ENDED;
  4153. if (inflateReset(&(png_ptr->zstream)) != Z_OK)
  4154. png_error(png_ptr, "inflateReset failed");
  4155. png_ptr->zstream.avail_in = 0;
  4156. png_ptr->zstream.next_in = 0;
  4157. png_ptr->zstream.next_out = png_ptr->row_buf;
  4158. png_ptr->zstream.avail_out = (uInt)PNG_ROWBYTES(png_ptr->pixel_depth,
  4159. png_ptr->iwidth) + 1;
  4160. }
  4161. #endif /* PROGRESSIVE_READ */
  4162. #endif /* READ_APNG */
  4163. #endif /* READ */