pngrutil.c 140 KB

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