pngrutil.c 153 KB

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