pngrutil.c 144 KB

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