pngrutil.c 138 KB

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