pngrutil.c 140 KB

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