pngrutil.c 142 KB

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