SgiRleDecode.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /*
  2. * The Python Imaging Library.
  3. * $Id$
  4. *
  5. * decoder for Sgi RLE data.
  6. *
  7. * history:
  8. * 2017-07-28 mb fixed for images larger than 64KB
  9. * 2017-07-20 mb created
  10. *
  11. * Copyright (c) Mickael Bonfill 2017.
  12. *
  13. * See the README file for information on usage and redistribution.
  14. */
  15. #include "Imaging.h"
  16. #include "Sgi.h"
  17. #define SGI_HEADER_SIZE 512
  18. #define RLE_COPY_FLAG 0x80
  19. #define RLE_MAX_RUN 0x7f
  20. static void
  21. read4B(UINT32 *dest, UINT8 *buf) {
  22. *dest = (UINT32)((buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3]);
  23. }
  24. /*
  25. SgiRleDecoding is done in a single channel row oriented set of RLE chunks.
  26. * The file is arranged as
  27. - SGI Header
  28. - Rle Offset Table
  29. - Rle Length Table
  30. - Scanline Data
  31. * Each RLE atom is c->bpc bytes wide (1 or 2)
  32. * Each RLE Chunk is [specifier atom] [ 1 or n data atoms ]
  33. * Copy Atoms are a byte with the high bit set, and the low 7 are
  34. the number of bytes to copy from the source to the
  35. destination. e.g.
  36. CBBBBBBBB or 0CHLHLHLHLHLHL (B=byte, H/L = Hi low bytes)
  37. * Run atoms do not have the high bit set, and the low 7 bits are
  38. the number of copies of the next atom to copy to the
  39. destination. e.g.:
  40. RB -> BBBBB or RHL -> HLHLHLHLHL
  41. The upshot of this is, there is no way to determine the required
  42. length of the input buffer from reloffset and rlelength without
  43. going through the data at that scan line.
  44. Furthermore, there's no requirement that individual scan lines
  45. pointed to from the rleoffset table are in any sort of order or
  46. used only once, or even disjoint. There's also no requirement that
  47. all of the data in the scan line area of the image file be used
  48. */
  49. static int
  50. expandrow(UINT8 *dest, UINT8 *src, int n, int z, int xsize, UINT8 *end_of_buffer) {
  51. /*
  52. * n here is the number of rlechunks
  53. * z is the number of channels, for calculating the interleave
  54. * offset to go to RGBA style pixels
  55. * xsize is the row width
  56. * end_of_buffer is the address of the end of the input buffer
  57. */
  58. UINT8 pixel, count;
  59. int x = 0;
  60. for (; n > 0; n--) {
  61. if (src > end_of_buffer) {
  62. return -1;
  63. }
  64. pixel = *src++;
  65. if (n == 1 && pixel != 0) {
  66. return n;
  67. }
  68. count = pixel & RLE_MAX_RUN;
  69. if (!count) {
  70. return count;
  71. }
  72. if (x + count > xsize) {
  73. return -1;
  74. }
  75. x += count;
  76. if (pixel & RLE_COPY_FLAG) {
  77. if (src + count > end_of_buffer) {
  78. return -1;
  79. }
  80. while (count--) {
  81. *dest = *src++;
  82. dest += z;
  83. }
  84. } else {
  85. if (src > end_of_buffer) {
  86. return -1;
  87. }
  88. pixel = *src++;
  89. while (count--) {
  90. *dest = pixel;
  91. dest += z;
  92. }
  93. }
  94. }
  95. return 0;
  96. }
  97. static int
  98. expandrow2(UINT8 *dest, const UINT8 *src, int n, int z, int xsize, UINT8 *end_of_buffer) {
  99. UINT8 pixel, count;
  100. int x = 0;
  101. for (; n > 0; n--) {
  102. if (src + 1 > end_of_buffer) {
  103. return -1;
  104. }
  105. pixel = src[1];
  106. src += 2;
  107. if (n == 1 && pixel != 0) {
  108. return n;
  109. }
  110. count = pixel & RLE_MAX_RUN;
  111. if (!count) {
  112. return count;
  113. }
  114. if (x + count > xsize) {
  115. return -1;
  116. }
  117. x += count;
  118. if (pixel & RLE_COPY_FLAG) {
  119. if (src + 2 * count > end_of_buffer) {
  120. return -1;
  121. }
  122. while (count--) {
  123. memcpy(dest, src, 2);
  124. src += 2;
  125. dest += z * 2;
  126. }
  127. } else {
  128. if (src + 2 > end_of_buffer) {
  129. return -1;
  130. }
  131. while (count--) {
  132. memcpy(dest, src, 2);
  133. dest += z * 2;
  134. }
  135. src += 2;
  136. }
  137. }
  138. return 0;
  139. }
  140. int
  141. ImagingSgiRleDecode(Imaging im, ImagingCodecState state, UINT8 *buf, Py_ssize_t bytes) {
  142. UINT8 *ptr;
  143. SGISTATE *c;
  144. int err = 0;
  145. int status;
  146. /* size check */
  147. if (im->xsize > INT_MAX / im->bands || im->ysize > INT_MAX / im->bands) {
  148. state->errcode = IMAGING_CODEC_MEMORY;
  149. return -1;
  150. }
  151. /* Get all data from File descriptor */
  152. c = (SGISTATE *)state->context;
  153. _imaging_seek_pyFd(state->fd, 0L, SEEK_END);
  154. c->bufsize = _imaging_tell_pyFd(state->fd);
  155. c->bufsize -= SGI_HEADER_SIZE;
  156. c->tablen = im->bands * im->ysize;
  157. /* below, we populate the starttab and lentab into the bufsize,
  158. each with 4 bytes per element of tablen
  159. Check here before we allocate any memory
  160. */
  161. if (c->bufsize < 8 * c->tablen) {
  162. state->errcode = IMAGING_CODEC_OVERRUN;
  163. return -1;
  164. }
  165. ptr = malloc(sizeof(UINT8) * c->bufsize);
  166. if (!ptr) {
  167. state->errcode = IMAGING_CODEC_MEMORY;
  168. return -1;
  169. }
  170. _imaging_seek_pyFd(state->fd, SGI_HEADER_SIZE, SEEK_SET);
  171. if (_imaging_read_pyFd(state->fd, (char *)ptr, c->bufsize) != c->bufsize) {
  172. state->errcode = IMAGING_CODEC_UNKNOWN;
  173. return -1;
  174. }
  175. /* decoder initialization */
  176. state->count = 0;
  177. state->y = 0;
  178. if (state->ystep < 0) {
  179. state->y = im->ysize - 1;
  180. } else {
  181. state->ystep = 1;
  182. }
  183. /* Allocate memory for RLE tables and rows */
  184. free(state->buffer);
  185. state->buffer = NULL;
  186. /* malloc overflow check above */
  187. state->buffer = calloc(im->xsize * im->bands, sizeof(UINT8) * 2);
  188. c->starttab = calloc(c->tablen, sizeof(UINT32));
  189. c->lengthtab = calloc(c->tablen, sizeof(UINT32));
  190. if (!state->buffer || !c->starttab || !c->lengthtab) {
  191. err = IMAGING_CODEC_MEMORY;
  192. goto sgi_finish_decode;
  193. }
  194. /* populate offsets table */
  195. for (c->tabindex = 0, c->bufindex = 0; c->tabindex < c->tablen;
  196. c->tabindex++, c->bufindex += 4) {
  197. read4B(&c->starttab[c->tabindex], &ptr[c->bufindex]);
  198. }
  199. /* populate lengths table */
  200. for (c->tabindex = 0, c->bufindex = c->tablen * sizeof(UINT32);
  201. c->tabindex < c->tablen;
  202. c->tabindex++, c->bufindex += 4) {
  203. read4B(&c->lengthtab[c->tabindex], &ptr[c->bufindex]);
  204. }
  205. /* read compressed rows */
  206. for (c->rowno = 0; c->rowno < im->ysize; c->rowno++, state->y += state->ystep) {
  207. for (c->channo = 0; c->channo < im->bands; c->channo++) {
  208. c->rleoffset = c->starttab[c->rowno + c->channo * im->ysize];
  209. c->rlelength = c->lengthtab[c->rowno + c->channo * im->ysize];
  210. // Check for underflow of rleoffset-SGI_HEADER_SIZE
  211. if (c->rleoffset < SGI_HEADER_SIZE) {
  212. state->errcode = IMAGING_CODEC_OVERRUN;
  213. goto sgi_finish_decode;
  214. }
  215. c->rleoffset -= SGI_HEADER_SIZE;
  216. /* row decompression */
  217. if (c->bpc == 1) {
  218. status = expandrow(
  219. &state->buffer[c->channo],
  220. &ptr[c->rleoffset],
  221. c->rlelength,
  222. im->bands,
  223. im->xsize,
  224. &ptr[c->bufsize-1]);
  225. } else {
  226. status = expandrow2(
  227. &state->buffer[c->channo * 2],
  228. &ptr[c->rleoffset],
  229. c->rlelength,
  230. im->bands,
  231. im->xsize,
  232. &ptr[c->bufsize-1]);
  233. }
  234. if (status == -1) {
  235. state->errcode = IMAGING_CODEC_OVERRUN;
  236. goto sgi_finish_decode;
  237. } else if (status == 1) {
  238. goto sgi_finish_decode;
  239. }
  240. }
  241. /* store decompressed data in image */
  242. state->shuffle((UINT8 *)im->image[state->y], state->buffer, im->xsize);
  243. }
  244. sgi_finish_decode:;
  245. free(c->starttab);
  246. free(c->lengthtab);
  247. free(ptr);
  248. if (err != 0) {
  249. state->errcode = err;
  250. return -1;
  251. }
  252. return 0;
  253. }