Pillow/src/libImaging/HexDecode.c

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

65 lines
1.4 KiB
C
Raw Normal View History

2010-07-31 06:52:47 +04:00
/*
* The Python Imaging Library.
* $Id$
*
* decoder for hex encoded image data
*
* history:
2020-05-01 15:08:57 +03:00
* 96-05-16 fl Created
2010-07-31 06:52:47 +04:00
*
* Copyright (c) Fredrik Lundh 1996.
* Copyright (c) Secret Labs AB 1997.
*
* See the README file for information on usage and redistribution.
*/
#include "Imaging.h"
2020-05-01 15:08:57 +03:00
#define HEX(v) \
((v >= '0' && v <= '9') ? v - '0' \
: (v >= 'a' && v <= 'f') ? v - 'a' + 10 \
: (v >= 'A' && v <= 'F') ? v - 'A' + 10 \
: -1)
2010-07-31 06:52:47 +04:00
int
ImagingHexDecode(Imaging im, ImagingCodecState state, UINT8 *buf, Py_ssize_t bytes) {
2010-07-31 06:52:47 +04:00
UINT8 *ptr;
int a, b;
ptr = buf;
for (;;) {
2020-05-10 12:56:36 +03:00
if (bytes < 2) {
2020-05-01 15:08:57 +03:00
return ptr - buf;
2020-05-10 12:56:36 +03:00
}
2010-07-31 06:52:47 +04:00
2020-05-01 15:08:57 +03:00
a = HEX(ptr[0]);
b = HEX(ptr[1]);
2010-07-31 06:52:47 +04:00
2020-05-01 15:08:57 +03:00
if (a < 0 || b < 0) {
ptr++;
bytes--;
2010-07-31 06:52:47 +04:00
2020-05-01 15:08:57 +03:00
} else {
ptr += 2;
bytes -= 2;
2010-07-31 06:52:47 +04:00
2020-05-01 15:08:57 +03:00
state->buffer[state->x] = (a << 4) + b;
2010-07-31 06:52:47 +04:00
2020-05-01 15:08:57 +03:00
if (++state->x >= state->bytes) {
/* Got a full line, unpack it */
state->shuffle(
(UINT8 *)im->image[state->y], state->buffer, state->xsize
);
2010-07-31 06:52:47 +04:00
2020-05-01 15:08:57 +03:00
state->x = 0;
2010-07-31 06:52:47 +04:00
2020-05-01 15:08:57 +03:00
if (++state->y >= state->ysize) {
/* End of file (errcode = 0) */
return -1;
}
}
}
2010-07-31 06:52:47 +04:00
}
}