Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

pngpread.c 37KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291
  1. /* pngpread.c - read a png file in push mode
  2. *
  3. * Last changed in libpng 1.6.0 [February 14, 2013]
  4. * Copyright (c) 1998-2013 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. #include "pngpriv.h"
  13. #ifdef PNG_PROGRESSIVE_READ_SUPPORTED
  14. /* Push model modes */
  15. #define PNG_READ_SIG_MODE 0
  16. #define PNG_READ_CHUNK_MODE 1
  17. #define PNG_READ_IDAT_MODE 2
  18. #define PNG_SKIP_MODE 3
  19. #define PNG_READ_tEXt_MODE 4
  20. #define PNG_READ_zTXt_MODE 5
  21. #define PNG_READ_DONE_MODE 6
  22. #define PNG_READ_iTXt_MODE 7
  23. #define PNG_ERROR_MODE 8
  24. void PNGAPI
  25. png_process_data(png_structrp png_ptr, png_inforp info_ptr,
  26. png_bytep buffer, png_size_t buffer_size)
  27. {
  28. if (png_ptr == NULL || info_ptr == NULL)
  29. return;
  30. png_push_restore_buffer(png_ptr, buffer, buffer_size);
  31. while (png_ptr->buffer_size)
  32. {
  33. png_process_some_data(png_ptr, info_ptr);
  34. }
  35. }
  36. png_size_t PNGAPI
  37. png_process_data_pause(png_structrp png_ptr, int save)
  38. {
  39. if (png_ptr != NULL)
  40. {
  41. /* It's easiest for the caller if we do the save, then the caller doesn't
  42. * have to supply the same data again:
  43. */
  44. if (save)
  45. png_push_save_buffer(png_ptr);
  46. else
  47. {
  48. /* This includes any pending saved bytes: */
  49. png_size_t remaining = png_ptr->buffer_size;
  50. png_ptr->buffer_size = 0;
  51. /* So subtract the saved buffer size, unless all the data
  52. * is actually 'saved', in which case we just return 0
  53. */
  54. if (png_ptr->save_buffer_size < remaining)
  55. return remaining - png_ptr->save_buffer_size;
  56. }
  57. }
  58. return 0;
  59. }
  60. png_uint_32 PNGAPI
  61. png_process_data_skip(png_structrp png_ptr)
  62. {
  63. png_uint_32 remaining = 0;
  64. if (png_ptr != NULL && png_ptr->process_mode == PNG_SKIP_MODE &&
  65. png_ptr->skip_length > 0)
  66. {
  67. /* At the end of png_process_data the buffer size must be 0 (see the loop
  68. * above) so we can detect a broken call here:
  69. */
  70. if (png_ptr->buffer_size != 0)
  71. png_error(png_ptr,
  72. "png_process_data_skip called inside png_process_data");
  73. /* If is impossible for there to be a saved buffer at this point -
  74. * otherwise we could not be in SKIP mode. This will also happen if
  75. * png_process_skip is called inside png_process_data (but only very
  76. * rarely.)
  77. */
  78. if (png_ptr->save_buffer_size != 0)
  79. png_error(png_ptr, "png_process_data_skip called with saved data");
  80. remaining = png_ptr->skip_length;
  81. png_ptr->skip_length = 0;
  82. png_ptr->process_mode = PNG_READ_CHUNK_MODE;
  83. }
  84. return remaining;
  85. }
  86. /* What we do with the incoming data depends on what we were previously
  87. * doing before we ran out of data...
  88. */
  89. void /* PRIVATE */
  90. png_process_some_data(png_structrp png_ptr, png_inforp info_ptr)
  91. {
  92. if (png_ptr == NULL)
  93. return;
  94. switch (png_ptr->process_mode)
  95. {
  96. case PNG_READ_SIG_MODE:
  97. {
  98. png_push_read_sig(png_ptr, info_ptr);
  99. break;
  100. }
  101. case PNG_READ_CHUNK_MODE:
  102. {
  103. png_push_read_chunk(png_ptr, info_ptr);
  104. break;
  105. }
  106. case PNG_READ_IDAT_MODE:
  107. {
  108. png_push_read_IDAT(png_ptr);
  109. break;
  110. }
  111. case PNG_SKIP_MODE:
  112. {
  113. png_push_crc_finish(png_ptr);
  114. break;
  115. }
  116. default:
  117. {
  118. png_ptr->buffer_size = 0;
  119. break;
  120. }
  121. }
  122. }
  123. /* Read any remaining signature bytes from the stream and compare them with
  124. * the correct PNG signature. It is possible that this routine is called
  125. * with bytes already read from the signature, either because they have been
  126. * checked by the calling application, or because of multiple calls to this
  127. * routine.
  128. */
  129. void /* PRIVATE */
  130. png_push_read_sig(png_structrp png_ptr, png_inforp info_ptr)
  131. {
  132. png_size_t num_checked = png_ptr->sig_bytes,
  133. num_to_check = 8 - num_checked;
  134. if (png_ptr->buffer_size < num_to_check)
  135. {
  136. num_to_check = png_ptr->buffer_size;
  137. }
  138. png_push_fill_buffer(png_ptr, &(info_ptr->signature[num_checked]),
  139. num_to_check);
  140. png_ptr->sig_bytes = (png_byte)(png_ptr->sig_bytes + num_to_check);
  141. if (png_sig_cmp(info_ptr->signature, num_checked, num_to_check))
  142. {
  143. if (num_checked < 4 &&
  144. png_sig_cmp(info_ptr->signature, num_checked, num_to_check - 4))
  145. png_error(png_ptr, "Not a PNG file");
  146. else
  147. png_error(png_ptr, "PNG file corrupted by ASCII conversion");
  148. }
  149. else
  150. {
  151. if (png_ptr->sig_bytes >= 8)
  152. {
  153. png_ptr->process_mode = PNG_READ_CHUNK_MODE;
  154. }
  155. }
  156. }
  157. void /* PRIVATE */
  158. png_push_read_chunk(png_structrp png_ptr, png_inforp info_ptr)
  159. {
  160. png_uint_32 chunk_name;
  161. #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
  162. int keep; /* unknown handling method */
  163. #endif
  164. /* First we make sure we have enough data for the 4 byte chunk name
  165. * and the 4 byte chunk length before proceeding with decoding the
  166. * chunk data. To fully decode each of these chunks, we also make
  167. * sure we have enough data in the buffer for the 4 byte CRC at the
  168. * end of every chunk (except IDAT, which is handled separately).
  169. */
  170. if (!(png_ptr->mode & PNG_HAVE_CHUNK_HEADER))
  171. {
  172. png_byte chunk_length[4];
  173. png_byte chunk_tag[4];
  174. if (png_ptr->buffer_size < 8)
  175. {
  176. png_push_save_buffer(png_ptr);
  177. return;
  178. }
  179. png_push_fill_buffer(png_ptr, chunk_length, 4);
  180. png_ptr->push_length = png_get_uint_31(png_ptr, chunk_length);
  181. png_reset_crc(png_ptr);
  182. png_crc_read(png_ptr, chunk_tag, 4);
  183. png_ptr->chunk_name = PNG_CHUNK_FROM_STRING(chunk_tag);
  184. png_check_chunk_name(png_ptr, png_ptr->chunk_name);
  185. png_ptr->mode |= PNG_HAVE_CHUNK_HEADER;
  186. }
  187. chunk_name = png_ptr->chunk_name;
  188. if (chunk_name == png_IDAT)
  189. {
  190. if (png_ptr->mode & PNG_AFTER_IDAT)
  191. png_ptr->mode |= PNG_HAVE_CHUNK_AFTER_IDAT;
  192. /* If we reach an IDAT chunk, this means we have read all of the
  193. * header chunks, and we can start reading the image (or if this
  194. * is called after the image has been read - we have an error).
  195. */
  196. if (!(png_ptr->mode & PNG_HAVE_IHDR))
  197. png_error(png_ptr, "Missing IHDR before IDAT");
  198. else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
  199. !(png_ptr->mode & PNG_HAVE_PLTE))
  200. png_error(png_ptr, "Missing PLTE before IDAT");
  201. png_ptr->mode |= PNG_HAVE_IDAT;
  202. if (!(png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT))
  203. if (png_ptr->push_length == 0)
  204. return;
  205. if (png_ptr->mode & PNG_AFTER_IDAT)
  206. png_benign_error(png_ptr, "Too many IDATs found");
  207. }
  208. if (chunk_name == png_IHDR)
  209. {
  210. if (png_ptr->push_length != 13)
  211. png_error(png_ptr, "Invalid IHDR length");
  212. if (png_ptr->push_length + 4 > png_ptr->buffer_size)
  213. {
  214. png_push_save_buffer(png_ptr);
  215. return;
  216. }
  217. png_handle_IHDR(png_ptr, info_ptr, png_ptr->push_length);
  218. }
  219. else if (chunk_name == png_IEND)
  220. {
  221. if (png_ptr->push_length + 4 > png_ptr->buffer_size)
  222. {
  223. png_push_save_buffer(png_ptr);
  224. return;
  225. }
  226. png_handle_IEND(png_ptr, info_ptr, png_ptr->push_length);
  227. png_ptr->process_mode = PNG_READ_DONE_MODE;
  228. png_push_have_end(png_ptr, info_ptr);
  229. }
  230. #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
  231. else if ((keep = png_chunk_unknown_handling(png_ptr, chunk_name)) != 0)
  232. {
  233. if (png_ptr->push_length + 4 > png_ptr->buffer_size)
  234. {
  235. png_push_save_buffer(png_ptr);
  236. return;
  237. }
  238. png_handle_unknown(png_ptr, info_ptr, png_ptr->push_length, keep);
  239. if (chunk_name == png_PLTE)
  240. png_ptr->mode |= PNG_HAVE_PLTE;
  241. }
  242. #endif
  243. else if (chunk_name == png_PLTE)
  244. {
  245. if (png_ptr->push_length + 4 > png_ptr->buffer_size)
  246. {
  247. png_push_save_buffer(png_ptr);
  248. return;
  249. }
  250. png_handle_PLTE(png_ptr, info_ptr, png_ptr->push_length);
  251. }
  252. else if (chunk_name == png_IDAT)
  253. {
  254. png_ptr->idat_size = png_ptr->push_length;
  255. png_ptr->process_mode = PNG_READ_IDAT_MODE;
  256. png_push_have_info(png_ptr, info_ptr);
  257. png_ptr->zstream.avail_out =
  258. (uInt) PNG_ROWBYTES(png_ptr->pixel_depth,
  259. png_ptr->iwidth) + 1;
  260. png_ptr->zstream.next_out = png_ptr->row_buf;
  261. return;
  262. }
  263. #ifdef PNG_READ_gAMA_SUPPORTED
  264. else if (png_ptr->chunk_name == png_gAMA)
  265. {
  266. if (png_ptr->push_length + 4 > png_ptr->buffer_size)
  267. {
  268. png_push_save_buffer(png_ptr);
  269. return;
  270. }
  271. png_handle_gAMA(png_ptr, info_ptr, png_ptr->push_length);
  272. }
  273. #endif
  274. #ifdef PNG_READ_sBIT_SUPPORTED
  275. else if (png_ptr->chunk_name == png_sBIT)
  276. {
  277. if (png_ptr->push_length + 4 > png_ptr->buffer_size)
  278. {
  279. png_push_save_buffer(png_ptr);
  280. return;
  281. }
  282. png_handle_sBIT(png_ptr, info_ptr, png_ptr->push_length);
  283. }
  284. #endif
  285. #ifdef PNG_READ_cHRM_SUPPORTED
  286. else if (png_ptr->chunk_name == png_cHRM)
  287. {
  288. if (png_ptr->push_length + 4 > png_ptr->buffer_size)
  289. {
  290. png_push_save_buffer(png_ptr);
  291. return;
  292. }
  293. png_handle_cHRM(png_ptr, info_ptr, png_ptr->push_length);
  294. }
  295. #endif
  296. #ifdef PNG_READ_sRGB_SUPPORTED
  297. else if (chunk_name == png_sRGB)
  298. {
  299. if (png_ptr->push_length + 4 > png_ptr->buffer_size)
  300. {
  301. png_push_save_buffer(png_ptr);
  302. return;
  303. }
  304. png_handle_sRGB(png_ptr, info_ptr, png_ptr->push_length);
  305. }
  306. #endif
  307. #ifdef PNG_READ_iCCP_SUPPORTED
  308. else if (png_ptr->chunk_name == png_iCCP)
  309. {
  310. if (png_ptr->push_length + 4 > png_ptr->buffer_size)
  311. {
  312. png_push_save_buffer(png_ptr);
  313. return;
  314. }
  315. png_handle_iCCP(png_ptr, info_ptr, png_ptr->push_length);
  316. }
  317. #endif
  318. #ifdef PNG_READ_sPLT_SUPPORTED
  319. else if (chunk_name == png_sPLT)
  320. {
  321. if (png_ptr->push_length + 4 > png_ptr->buffer_size)
  322. {
  323. png_push_save_buffer(png_ptr);
  324. return;
  325. }
  326. png_handle_sPLT(png_ptr, info_ptr, png_ptr->push_length);
  327. }
  328. #endif
  329. #ifdef PNG_READ_tRNS_SUPPORTED
  330. else if (chunk_name == png_tRNS)
  331. {
  332. if (png_ptr->push_length + 4 > png_ptr->buffer_size)
  333. {
  334. png_push_save_buffer(png_ptr);
  335. return;
  336. }
  337. png_handle_tRNS(png_ptr, info_ptr, png_ptr->push_length);
  338. }
  339. #endif
  340. #ifdef PNG_READ_bKGD_SUPPORTED
  341. else if (chunk_name == png_bKGD)
  342. {
  343. if (png_ptr->push_length + 4 > png_ptr->buffer_size)
  344. {
  345. png_push_save_buffer(png_ptr);
  346. return;
  347. }
  348. png_handle_bKGD(png_ptr, info_ptr, png_ptr->push_length);
  349. }
  350. #endif
  351. #ifdef PNG_READ_hIST_SUPPORTED
  352. else if (chunk_name == png_hIST)
  353. {
  354. if (png_ptr->push_length + 4 > png_ptr->buffer_size)
  355. {
  356. png_push_save_buffer(png_ptr);
  357. return;
  358. }
  359. png_handle_hIST(png_ptr, info_ptr, png_ptr->push_length);
  360. }
  361. #endif
  362. #ifdef PNG_READ_pHYs_SUPPORTED
  363. else if (chunk_name == png_pHYs)
  364. {
  365. if (png_ptr->push_length + 4 > png_ptr->buffer_size)
  366. {
  367. png_push_save_buffer(png_ptr);
  368. return;
  369. }
  370. png_handle_pHYs(png_ptr, info_ptr, png_ptr->push_length);
  371. }
  372. #endif
  373. #ifdef PNG_READ_oFFs_SUPPORTED
  374. else if (chunk_name == png_oFFs)
  375. {
  376. if (png_ptr->push_length + 4 > png_ptr->buffer_size)
  377. {
  378. png_push_save_buffer(png_ptr);
  379. return;
  380. }
  381. png_handle_oFFs(png_ptr, info_ptr, png_ptr->push_length);
  382. }
  383. #endif
  384. #ifdef PNG_READ_pCAL_SUPPORTED
  385. else if (chunk_name == png_pCAL)
  386. {
  387. if (png_ptr->push_length + 4 > png_ptr->buffer_size)
  388. {
  389. png_push_save_buffer(png_ptr);
  390. return;
  391. }
  392. png_handle_pCAL(png_ptr, info_ptr, png_ptr->push_length);
  393. }
  394. #endif
  395. #ifdef PNG_READ_sCAL_SUPPORTED
  396. else if (chunk_name == png_sCAL)
  397. {
  398. if (png_ptr->push_length + 4 > png_ptr->buffer_size)
  399. {
  400. png_push_save_buffer(png_ptr);
  401. return;
  402. }
  403. png_handle_sCAL(png_ptr, info_ptr, png_ptr->push_length);
  404. }
  405. #endif
  406. #ifdef PNG_READ_tIME_SUPPORTED
  407. else if (chunk_name == png_tIME)
  408. {
  409. if (png_ptr->push_length + 4 > png_ptr->buffer_size)
  410. {
  411. png_push_save_buffer(png_ptr);
  412. return;
  413. }
  414. png_handle_tIME(png_ptr, info_ptr, png_ptr->push_length);
  415. }
  416. #endif
  417. #ifdef PNG_READ_tEXt_SUPPORTED
  418. else if (chunk_name == png_tEXt)
  419. {
  420. if (png_ptr->push_length + 4 > png_ptr->buffer_size)
  421. {
  422. png_push_save_buffer(png_ptr);
  423. return;
  424. }
  425. png_handle_tEXt(png_ptr, info_ptr, png_ptr->push_length);
  426. }
  427. #endif
  428. #ifdef PNG_READ_zTXt_SUPPORTED
  429. else if (chunk_name == png_zTXt)
  430. {
  431. if (png_ptr->push_length + 4 > png_ptr->buffer_size)
  432. {
  433. png_push_save_buffer(png_ptr);
  434. return;
  435. }
  436. png_handle_zTXt(png_ptr, info_ptr, png_ptr->push_length);
  437. }
  438. #endif
  439. #ifdef PNG_READ_iTXt_SUPPORTED
  440. else if (chunk_name == png_iTXt)
  441. {
  442. if (png_ptr->push_length + 4 > png_ptr->buffer_size)
  443. {
  444. png_push_save_buffer(png_ptr);
  445. return;
  446. }
  447. png_handle_iTXt(png_ptr, info_ptr, png_ptr->push_length);
  448. }
  449. #endif
  450. else
  451. {
  452. if (png_ptr->push_length + 4 > png_ptr->buffer_size)
  453. {
  454. png_push_save_buffer(png_ptr);
  455. return;
  456. }
  457. png_handle_unknown(png_ptr, info_ptr, png_ptr->push_length,
  458. PNG_HANDLE_CHUNK_AS_DEFAULT);
  459. }
  460. png_ptr->mode &= ~PNG_HAVE_CHUNK_HEADER;
  461. }
  462. void /* PRIVATE */
  463. png_push_crc_skip(png_structrp png_ptr, png_uint_32 skip)
  464. {
  465. png_ptr->process_mode = PNG_SKIP_MODE;
  466. png_ptr->skip_length = skip;
  467. }
  468. void /* PRIVATE */
  469. png_push_crc_finish(png_structrp png_ptr)
  470. {
  471. if (png_ptr->skip_length && png_ptr->save_buffer_size)
  472. {
  473. png_size_t save_size = png_ptr->save_buffer_size;
  474. png_uint_32 skip_length = png_ptr->skip_length;
  475. /* We want the smaller of 'skip_length' and 'save_buffer_size', but
  476. * they are of different types and we don't know which variable has the
  477. * fewest bits. Carefully select the smaller and cast it to the type of
  478. * the larger - this cannot overflow. Do not cast in the following test
  479. * - it will break on either 16 or 64 bit platforms.
  480. */
  481. if (skip_length < save_size)
  482. save_size = (png_size_t)skip_length;
  483. else
  484. skip_length = (png_uint_32)save_size;
  485. png_calculate_crc(png_ptr, png_ptr->save_buffer_ptr, save_size);
  486. png_ptr->skip_length -= skip_length;
  487. png_ptr->buffer_size -= save_size;
  488. png_ptr->save_buffer_size -= save_size;
  489. png_ptr->save_buffer_ptr += save_size;
  490. }
  491. if (png_ptr->skip_length && png_ptr->current_buffer_size)
  492. {
  493. png_size_t save_size = png_ptr->current_buffer_size;
  494. png_uint_32 skip_length = png_ptr->skip_length;
  495. /* We want the smaller of 'skip_length' and 'current_buffer_size', here,
  496. * the same problem exists as above and the same solution.
  497. */
  498. if (skip_length < save_size)
  499. save_size = (png_size_t)skip_length;
  500. else
  501. skip_length = (png_uint_32)save_size;
  502. png_calculate_crc(png_ptr, png_ptr->current_buffer_ptr, save_size);
  503. png_ptr->skip_length -= skip_length;
  504. png_ptr->buffer_size -= save_size;
  505. png_ptr->current_buffer_size -= save_size;
  506. png_ptr->current_buffer_ptr += save_size;
  507. }
  508. if (!png_ptr->skip_length)
  509. {
  510. if (png_ptr->buffer_size < 4)
  511. {
  512. png_push_save_buffer(png_ptr);
  513. return;
  514. }
  515. png_crc_finish(png_ptr, 0);
  516. png_ptr->process_mode = PNG_READ_CHUNK_MODE;
  517. }
  518. }
  519. void PNGCBAPI
  520. png_push_fill_buffer(png_structp png_ptr, png_bytep buffer, png_size_t length)
  521. {
  522. png_bytep ptr;
  523. if (png_ptr == NULL)
  524. return;
  525. ptr = buffer;
  526. if (png_ptr->save_buffer_size)
  527. {
  528. png_size_t save_size;
  529. if (length < png_ptr->save_buffer_size)
  530. save_size = length;
  531. else
  532. save_size = png_ptr->save_buffer_size;
  533. memcpy(ptr, png_ptr->save_buffer_ptr, save_size);
  534. length -= save_size;
  535. ptr += save_size;
  536. png_ptr->buffer_size -= save_size;
  537. png_ptr->save_buffer_size -= save_size;
  538. png_ptr->save_buffer_ptr += save_size;
  539. }
  540. if (length && png_ptr->current_buffer_size)
  541. {
  542. png_size_t save_size;
  543. if (length < png_ptr->current_buffer_size)
  544. save_size = length;
  545. else
  546. save_size = png_ptr->current_buffer_size;
  547. memcpy(ptr, png_ptr->current_buffer_ptr, save_size);
  548. png_ptr->buffer_size -= save_size;
  549. png_ptr->current_buffer_size -= save_size;
  550. png_ptr->current_buffer_ptr += save_size;
  551. }
  552. }
  553. void /* PRIVATE */
  554. png_push_save_buffer(png_structrp png_ptr)
  555. {
  556. if (png_ptr->save_buffer_size)
  557. {
  558. if (png_ptr->save_buffer_ptr != png_ptr->save_buffer)
  559. {
  560. png_size_t i, istop;
  561. png_bytep sp;
  562. png_bytep dp;
  563. istop = png_ptr->save_buffer_size;
  564. for (i = 0, sp = png_ptr->save_buffer_ptr, dp = png_ptr->save_buffer;
  565. i < istop; i++, sp++, dp++)
  566. {
  567. *dp = *sp;
  568. }
  569. }
  570. }
  571. if (png_ptr->save_buffer_size + png_ptr->current_buffer_size >
  572. png_ptr->save_buffer_max)
  573. {
  574. png_size_t new_max;
  575. png_bytep old_buffer;
  576. if (png_ptr->save_buffer_size > PNG_SIZE_MAX -
  577. (png_ptr->current_buffer_size + 256))
  578. {
  579. png_error(png_ptr, "Potential overflow of save_buffer");
  580. }
  581. new_max = png_ptr->save_buffer_size + png_ptr->current_buffer_size + 256;
  582. old_buffer = png_ptr->save_buffer;
  583. png_ptr->save_buffer = (png_bytep)png_malloc_warn(png_ptr,
  584. (png_size_t)new_max);
  585. if (png_ptr->save_buffer == NULL)
  586. {
  587. png_free(png_ptr, old_buffer);
  588. png_error(png_ptr, "Insufficient memory for save_buffer");
  589. }
  590. memcpy(png_ptr->save_buffer, old_buffer, png_ptr->save_buffer_size);
  591. png_free(png_ptr, old_buffer);
  592. png_ptr->save_buffer_max = new_max;
  593. }
  594. if (png_ptr->current_buffer_size)
  595. {
  596. memcpy(png_ptr->save_buffer + png_ptr->save_buffer_size,
  597. png_ptr->current_buffer_ptr, png_ptr->current_buffer_size);
  598. png_ptr->save_buffer_size += png_ptr->current_buffer_size;
  599. png_ptr->current_buffer_size = 0;
  600. }
  601. png_ptr->save_buffer_ptr = png_ptr->save_buffer;
  602. png_ptr->buffer_size = 0;
  603. }
  604. void /* PRIVATE */
  605. png_push_restore_buffer(png_structrp png_ptr, png_bytep buffer,
  606. png_size_t buffer_length)
  607. {
  608. png_ptr->current_buffer = buffer;
  609. png_ptr->current_buffer_size = buffer_length;
  610. png_ptr->buffer_size = buffer_length + png_ptr->save_buffer_size;
  611. png_ptr->current_buffer_ptr = png_ptr->current_buffer;
  612. }
  613. void /* PRIVATE */
  614. png_push_read_IDAT(png_structrp png_ptr)
  615. {
  616. if (!(png_ptr->mode & PNG_HAVE_CHUNK_HEADER))
  617. {
  618. png_byte chunk_length[4];
  619. png_byte chunk_tag[4];
  620. /* TODO: this code can be commoned up with the same code in push_read */
  621. if (png_ptr->buffer_size < 8)
  622. {
  623. png_push_save_buffer(png_ptr);
  624. return;
  625. }
  626. png_push_fill_buffer(png_ptr, chunk_length, 4);
  627. png_ptr->push_length = png_get_uint_31(png_ptr, chunk_length);
  628. png_reset_crc(png_ptr);
  629. png_crc_read(png_ptr, chunk_tag, 4);
  630. png_ptr->chunk_name = PNG_CHUNK_FROM_STRING(chunk_tag);
  631. png_ptr->mode |= PNG_HAVE_CHUNK_HEADER;
  632. if (png_ptr->chunk_name != png_IDAT)
  633. {
  634. png_ptr->process_mode = PNG_READ_CHUNK_MODE;
  635. if (!(png_ptr->flags & PNG_FLAG_ZSTREAM_ENDED))
  636. png_error(png_ptr, "Not enough compressed data");
  637. return;
  638. }
  639. png_ptr->idat_size = png_ptr->push_length;
  640. }
  641. if (png_ptr->idat_size && png_ptr->save_buffer_size)
  642. {
  643. png_size_t save_size = png_ptr->save_buffer_size;
  644. png_uint_32 idat_size = png_ptr->idat_size;
  645. /* We want the smaller of 'idat_size' and 'current_buffer_size', but they
  646. * are of different types and we don't know which variable has the fewest
  647. * bits. Carefully select the smaller and cast it to the type of the
  648. * larger - this cannot overflow. Do not cast in the following test - it
  649. * will break on either 16 or 64 bit platforms.
  650. */
  651. if (idat_size < save_size)
  652. save_size = (png_size_t)idat_size;
  653. else
  654. idat_size = (png_uint_32)save_size;
  655. png_calculate_crc(png_ptr, png_ptr->save_buffer_ptr, save_size);
  656. png_process_IDAT_data(png_ptr, png_ptr->save_buffer_ptr, save_size);
  657. png_ptr->idat_size -= idat_size;
  658. png_ptr->buffer_size -= save_size;
  659. png_ptr->save_buffer_size -= save_size;
  660. png_ptr->save_buffer_ptr += save_size;
  661. }
  662. if (png_ptr->idat_size && png_ptr->current_buffer_size)
  663. {
  664. png_size_t save_size = png_ptr->current_buffer_size;
  665. png_uint_32 idat_size = png_ptr->idat_size;
  666. /* We want the smaller of 'idat_size' and 'current_buffer_size', but they
  667. * are of different types and we don't know which variable has the fewest
  668. * bits. Carefully select the smaller and cast it to the type of the
  669. * larger - this cannot overflow.
  670. */
  671. if (idat_size < save_size)
  672. save_size = (png_size_t)idat_size;
  673. else
  674. idat_size = (png_uint_32)save_size;
  675. png_calculate_crc(png_ptr, png_ptr->current_buffer_ptr, save_size);
  676. png_process_IDAT_data(png_ptr, png_ptr->current_buffer_ptr, save_size);
  677. png_ptr->idat_size -= idat_size;
  678. png_ptr->buffer_size -= save_size;
  679. png_ptr->current_buffer_size -= save_size;
  680. png_ptr->current_buffer_ptr += save_size;
  681. }
  682. if (!png_ptr->idat_size)
  683. {
  684. if (png_ptr->buffer_size < 4)
  685. {
  686. png_push_save_buffer(png_ptr);
  687. return;
  688. }
  689. png_crc_finish(png_ptr, 0);
  690. png_ptr->mode &= ~PNG_HAVE_CHUNK_HEADER;
  691. png_ptr->mode |= PNG_AFTER_IDAT;
  692. png_ptr->zowner = 0;
  693. }
  694. }
  695. void /* PRIVATE */
  696. png_process_IDAT_data(png_structrp png_ptr, png_bytep buffer,
  697. png_size_t buffer_length)
  698. {
  699. /* The caller checks for a non-zero buffer length. */
  700. if (!(buffer_length > 0) || buffer == NULL)
  701. png_error(png_ptr, "No IDAT data (internal error)");
  702. /* This routine must process all the data it has been given
  703. * before returning, calling the row callback as required to
  704. * handle the uncompressed results.
  705. */
  706. png_ptr->zstream.next_in = buffer;
  707. /* TODO: WARNING: TRUNCATION ERROR: DANGER WILL ROBINSON: */
  708. png_ptr->zstream.avail_in = (uInt)buffer_length;
  709. /* Keep going until the decompressed data is all processed
  710. * or the stream marked as finished.
  711. */
  712. while (png_ptr->zstream.avail_in > 0 &&
  713. !(png_ptr->flags & PNG_FLAG_ZSTREAM_ENDED))
  714. {
  715. int ret;
  716. /* We have data for zlib, but we must check that zlib
  717. * has someplace to put the results. It doesn't matter
  718. * if we don't expect any results -- it may be the input
  719. * data is just the LZ end code.
  720. */
  721. if (!(png_ptr->zstream.avail_out > 0))
  722. {
  723. /* TODO: WARNING: TRUNCATION ERROR: DANGER WILL ROBINSON: */
  724. png_ptr->zstream.avail_out = (uInt)(PNG_ROWBYTES(png_ptr->pixel_depth,
  725. png_ptr->iwidth) + 1);
  726. png_ptr->zstream.next_out = png_ptr->row_buf;
  727. }
  728. /* Using Z_SYNC_FLUSH here means that an unterminated
  729. * LZ stream (a stream with a missing end code) can still
  730. * be handled, otherwise (Z_NO_FLUSH) a future zlib
  731. * implementation might defer output and therefore
  732. * change the current behavior (see comments in inflate.c
  733. * for why this doesn't happen at present with zlib 1.2.5).
  734. */
  735. ret = inflate(&png_ptr->zstream, Z_SYNC_FLUSH);
  736. /* Check for any failure before proceeding. */
  737. if (ret != Z_OK && ret != Z_STREAM_END)
  738. {
  739. /* Terminate the decompression. */
  740. png_ptr->flags |= PNG_FLAG_ZSTREAM_ENDED;
  741. png_ptr->zowner = 0;
  742. /* This may be a truncated stream (missing or
  743. * damaged end code). Treat that as a warning.
  744. */
  745. if (png_ptr->row_number >= png_ptr->num_rows ||
  746. png_ptr->pass > 6)
  747. png_warning(png_ptr, "Truncated compressed data in IDAT");
  748. else
  749. png_error(png_ptr, "Decompression error in IDAT");
  750. /* Skip the check on unprocessed input */
  751. return;
  752. }
  753. /* Did inflate output any data? */
  754. if (png_ptr->zstream.next_out != png_ptr->row_buf)
  755. {
  756. /* Is this unexpected data after the last row?
  757. * If it is, artificially terminate the LZ output
  758. * here.
  759. */
  760. if (png_ptr->row_number >= png_ptr->num_rows ||
  761. png_ptr->pass > 6)
  762. {
  763. /* Extra data. */
  764. png_warning(png_ptr, "Extra compressed data in IDAT");
  765. png_ptr->flags |= PNG_FLAG_ZSTREAM_ENDED;
  766. png_ptr->zowner = 0;
  767. /* Do no more processing; skip the unprocessed
  768. * input check below.
  769. */
  770. return;
  771. }
  772. /* Do we have a complete row? */
  773. if (png_ptr->zstream.avail_out == 0)
  774. png_push_process_row(png_ptr);
  775. }
  776. /* And check for the end of the stream. */
  777. if (ret == Z_STREAM_END)
  778. png_ptr->flags |= PNG_FLAG_ZSTREAM_ENDED;
  779. }
  780. /* All the data should have been processed, if anything
  781. * is left at this point we have bytes of IDAT data
  782. * after the zlib end code.
  783. */
  784. if (png_ptr->zstream.avail_in > 0)
  785. png_warning(png_ptr, "Extra compression data in IDAT");
  786. }
  787. void /* PRIVATE */
  788. png_push_process_row(png_structrp png_ptr)
  789. {
  790. /* 1.5.6: row_info moved out of png_struct to a local here. */
  791. png_row_info row_info;
  792. row_info.width = png_ptr->iwidth; /* NOTE: width of current interlaced row */
  793. row_info.color_type = png_ptr->color_type;
  794. row_info.bit_depth = png_ptr->bit_depth;
  795. row_info.channels = png_ptr->channels;
  796. row_info.pixel_depth = png_ptr->pixel_depth;
  797. row_info.rowbytes = PNG_ROWBYTES(row_info.pixel_depth, row_info.width);
  798. if (png_ptr->row_buf[0] > PNG_FILTER_VALUE_NONE)
  799. {
  800. if (png_ptr->row_buf[0] < PNG_FILTER_VALUE_LAST)
  801. png_read_filter_row(png_ptr, &row_info, png_ptr->row_buf + 1,
  802. png_ptr->prev_row + 1, png_ptr->row_buf[0]);
  803. else
  804. png_error(png_ptr, "bad adaptive filter value");
  805. }
  806. /* libpng 1.5.6: the following line was copying png_ptr->rowbytes before
  807. * 1.5.6, while the buffer really is this big in current versions of libpng
  808. * it may not be in the future, so this was changed just to copy the
  809. * interlaced row count:
  810. */
  811. memcpy(png_ptr->prev_row, png_ptr->row_buf, row_info.rowbytes + 1);
  812. #ifdef PNG_READ_TRANSFORMS_SUPPORTED
  813. if (png_ptr->transformations)
  814. png_do_read_transformations(png_ptr, &row_info);
  815. #endif
  816. /* The transformed pixel depth should match the depth now in row_info. */
  817. if (png_ptr->transformed_pixel_depth == 0)
  818. {
  819. png_ptr->transformed_pixel_depth = row_info.pixel_depth;
  820. if (row_info.pixel_depth > png_ptr->maximum_pixel_depth)
  821. png_error(png_ptr, "progressive row overflow");
  822. }
  823. else if (png_ptr->transformed_pixel_depth != row_info.pixel_depth)
  824. png_error(png_ptr, "internal progressive row size calculation error");
  825. #ifdef PNG_READ_INTERLACING_SUPPORTED
  826. /* Blow up interlaced rows to full size */
  827. if (png_ptr->interlaced && (png_ptr->transformations & PNG_INTERLACE))
  828. {
  829. if (png_ptr->pass < 6)
  830. png_do_read_interlace(&row_info, png_ptr->row_buf + 1, png_ptr->pass,
  831. png_ptr->transformations);
  832. switch (png_ptr->pass)
  833. {
  834. case 0:
  835. {
  836. int i;
  837. for (i = 0; i < 8 && png_ptr->pass == 0; i++)
  838. {
  839. png_push_have_row(png_ptr, png_ptr->row_buf + 1);
  840. png_read_push_finish_row(png_ptr); /* Updates png_ptr->pass */
  841. }
  842. if (png_ptr->pass == 2) /* Pass 1 might be empty */
  843. {
  844. for (i = 0; i < 4 && png_ptr->pass == 2; i++)
  845. {
  846. png_push_have_row(png_ptr, NULL);
  847. png_read_push_finish_row(png_ptr);
  848. }
  849. }
  850. if (png_ptr->pass == 4 && png_ptr->height <= 4)
  851. {
  852. for (i = 0; i < 2 && png_ptr->pass == 4; i++)
  853. {
  854. png_push_have_row(png_ptr, NULL);
  855. png_read_push_finish_row(png_ptr);
  856. }
  857. }
  858. if (png_ptr->pass == 6 && png_ptr->height <= 4)
  859. {
  860. png_push_have_row(png_ptr, NULL);
  861. png_read_push_finish_row(png_ptr);
  862. }
  863. break;
  864. }
  865. case 1:
  866. {
  867. int i;
  868. for (i = 0; i < 8 && png_ptr->pass == 1; i++)
  869. {
  870. png_push_have_row(png_ptr, png_ptr->row_buf + 1);
  871. png_read_push_finish_row(png_ptr);
  872. }
  873. if (png_ptr->pass == 2) /* Skip top 4 generated rows */
  874. {
  875. for (i = 0; i < 4 && png_ptr->pass == 2; i++)
  876. {
  877. png_push_have_row(png_ptr, NULL);
  878. png_read_push_finish_row(png_ptr);
  879. }
  880. }
  881. break;
  882. }
  883. case 2:
  884. {
  885. int i;
  886. for (i = 0; i < 4 && png_ptr->pass == 2; i++)
  887. {
  888. png_push_have_row(png_ptr, png_ptr->row_buf + 1);
  889. png_read_push_finish_row(png_ptr);
  890. }
  891. for (i = 0; i < 4 && png_ptr->pass == 2; i++)
  892. {
  893. png_push_have_row(png_ptr, NULL);
  894. png_read_push_finish_row(png_ptr);
  895. }
  896. if (png_ptr->pass == 4) /* Pass 3 might be empty */
  897. {
  898. for (i = 0; i < 2 && png_ptr->pass == 4; i++)
  899. {
  900. png_push_have_row(png_ptr, NULL);
  901. png_read_push_finish_row(png_ptr);
  902. }
  903. }
  904. break;
  905. }
  906. case 3:
  907. {
  908. int i;
  909. for (i = 0; i < 4 && png_ptr->pass == 3; i++)
  910. {
  911. png_push_have_row(png_ptr, png_ptr->row_buf + 1);
  912. png_read_push_finish_row(png_ptr);
  913. }
  914. if (png_ptr->pass == 4) /* Skip top two generated rows */
  915. {
  916. for (i = 0; i < 2 && png_ptr->pass == 4; i++)
  917. {
  918. png_push_have_row(png_ptr, NULL);
  919. png_read_push_finish_row(png_ptr);
  920. }
  921. }
  922. break;
  923. }
  924. case 4:
  925. {
  926. int i;
  927. for (i = 0; i < 2 && png_ptr->pass == 4; i++)
  928. {
  929. png_push_have_row(png_ptr, png_ptr->row_buf + 1);
  930. png_read_push_finish_row(png_ptr);
  931. }
  932. for (i = 0; i < 2 && png_ptr->pass == 4; i++)
  933. {
  934. png_push_have_row(png_ptr, NULL);
  935. png_read_push_finish_row(png_ptr);
  936. }
  937. if (png_ptr->pass == 6) /* Pass 5 might be empty */
  938. {
  939. png_push_have_row(png_ptr, NULL);
  940. png_read_push_finish_row(png_ptr);
  941. }
  942. break;
  943. }
  944. case 5:
  945. {
  946. int i;
  947. for (i = 0; i < 2 && png_ptr->pass == 5; i++)
  948. {
  949. png_push_have_row(png_ptr, png_ptr->row_buf + 1);
  950. png_read_push_finish_row(png_ptr);
  951. }
  952. if (png_ptr->pass == 6) /* Skip top generated row */
  953. {
  954. png_push_have_row(png_ptr, NULL);
  955. png_read_push_finish_row(png_ptr);
  956. }
  957. break;
  958. }
  959. default:
  960. case 6:
  961. {
  962. png_push_have_row(png_ptr, png_ptr->row_buf + 1);
  963. png_read_push_finish_row(png_ptr);
  964. if (png_ptr->pass != 6)
  965. break;
  966. png_push_have_row(png_ptr, NULL);
  967. png_read_push_finish_row(png_ptr);
  968. }
  969. }
  970. }
  971. else
  972. #endif
  973. {
  974. png_push_have_row(png_ptr, png_ptr->row_buf + 1);
  975. png_read_push_finish_row(png_ptr);
  976. }
  977. }
  978. void /* PRIVATE */
  979. png_read_push_finish_row(png_structrp png_ptr)
  980. {
  981. #ifdef PNG_READ_INTERLACING_SUPPORTED
  982. /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
  983. /* Start of interlace block */
  984. static PNG_CONST png_byte png_pass_start[] = {0, 4, 0, 2, 0, 1, 0};
  985. /* Offset to next interlace block */
  986. static PNG_CONST png_byte png_pass_inc[] = {8, 8, 4, 4, 2, 2, 1};
  987. /* Start of interlace block in the y direction */
  988. static PNG_CONST png_byte png_pass_ystart[] = {0, 0, 4, 0, 2, 0, 1};
  989. /* Offset to next interlace block in the y direction */
  990. static PNG_CONST png_byte png_pass_yinc[] = {8, 8, 8, 4, 4, 2, 2};
  991. /* Height of interlace block. This is not currently used - if you need
  992. * it, uncomment it here and in png.h
  993. static PNG_CONST png_byte png_pass_height[] = {8, 8, 4, 4, 2, 2, 1};
  994. */
  995. #endif
  996. png_ptr->row_number++;
  997. if (png_ptr->row_number < png_ptr->num_rows)
  998. return;
  999. #ifdef PNG_READ_INTERLACING_SUPPORTED
  1000. if (png_ptr->interlaced)
  1001. {
  1002. png_ptr->row_number = 0;
  1003. memset(png_ptr->prev_row, 0, png_ptr->rowbytes + 1);
  1004. do
  1005. {
  1006. png_ptr->pass++;
  1007. if ((png_ptr->pass == 1 && png_ptr->width < 5) ||
  1008. (png_ptr->pass == 3 && png_ptr->width < 3) ||
  1009. (png_ptr->pass == 5 && png_ptr->width < 2))
  1010. png_ptr->pass++;
  1011. if (png_ptr->pass > 7)
  1012. png_ptr->pass--;
  1013. if (png_ptr->pass >= 7)
  1014. break;
  1015. png_ptr->iwidth = (png_ptr->width +
  1016. png_pass_inc[png_ptr->pass] - 1 -
  1017. png_pass_start[png_ptr->pass]) /
  1018. png_pass_inc[png_ptr->pass];
  1019. if (png_ptr->transformations & PNG_INTERLACE)
  1020. break;
  1021. png_ptr->num_rows = (png_ptr->height +
  1022. png_pass_yinc[png_ptr->pass] - 1 -
  1023. png_pass_ystart[png_ptr->pass]) /
  1024. png_pass_yinc[png_ptr->pass];
  1025. } while (png_ptr->iwidth == 0 || png_ptr->num_rows == 0);
  1026. }
  1027. #endif /* PNG_READ_INTERLACING_SUPPORTED */
  1028. }
  1029. void /* PRIVATE */
  1030. png_push_have_info(png_structrp png_ptr, png_inforp info_ptr)
  1031. {
  1032. if (png_ptr->info_fn != NULL)
  1033. (*(png_ptr->info_fn))(png_ptr, info_ptr);
  1034. }
  1035. void /* PRIVATE */
  1036. png_push_have_end(png_structrp png_ptr, png_inforp info_ptr)
  1037. {
  1038. if (png_ptr->end_fn != NULL)
  1039. (*(png_ptr->end_fn))(png_ptr, info_ptr);
  1040. }
  1041. void /* PRIVATE */
  1042. png_push_have_row(png_structrp png_ptr, png_bytep row)
  1043. {
  1044. if (png_ptr->row_fn != NULL)
  1045. (*(png_ptr->row_fn))(png_ptr, row, png_ptr->row_number,
  1046. (int)png_ptr->pass);
  1047. }
  1048. #ifdef PNG_READ_INTERLACING_SUPPORTED
  1049. void PNGAPI
  1050. png_progressive_combine_row(png_const_structrp png_ptr, png_bytep old_row,
  1051. png_const_bytep new_row)
  1052. {
  1053. if (png_ptr == NULL)
  1054. return;
  1055. /* new_row is a flag here - if it is NULL then the app callback was called
  1056. * from an empty row (see the calls to png_struct::row_fn below), otherwise
  1057. * it must be png_ptr->row_buf+1
  1058. */
  1059. if (new_row != NULL)
  1060. png_combine_row(png_ptr, old_row, 1/*display*/);
  1061. }
  1062. #endif /* PNG_READ_INTERLACING_SUPPORTED */
  1063. void PNGAPI
  1064. png_set_progressive_read_fn(png_structrp png_ptr, png_voidp progressive_ptr,
  1065. png_progressive_info_ptr info_fn, png_progressive_row_ptr row_fn,
  1066. png_progressive_end_ptr end_fn)
  1067. {
  1068. if (png_ptr == NULL)
  1069. return;
  1070. png_ptr->info_fn = info_fn;
  1071. png_ptr->row_fn = row_fn;
  1072. png_ptr->end_fn = end_fn;
  1073. png_set_read_fn(png_ptr, progressive_ptr, png_push_fill_buffer);
  1074. }
  1075. png_voidp PNGAPI
  1076. png_get_progressive_ptr(png_const_structrp png_ptr)
  1077. {
  1078. if (png_ptr == NULL)
  1079. return (NULL);
  1080. return png_ptr->io_ptr;
  1081. }
  1082. #endif /* PNG_PROGRESSIVE_READ_SUPPORTED */