HexDecode.c 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /*
  2. * The Python Imaging Library.
  3. * $Id$
  4. *
  5. * decoder for hex encoded image data
  6. *
  7. * history:
  8. * 96-05-16 fl Created
  9. *
  10. * Copyright (c) Fredrik Lundh 1996.
  11. * Copyright (c) Secret Labs AB 1997.
  12. *
  13. * See the README file for information on usage and redistribution.
  14. */
  15. #include "Imaging.h"
  16. #define HEX(v) ((v >= '0' && v <= '9') ? v - '0' :\
  17. (v >= 'a' && v <= 'f') ? v - 'a' + 10 :\
  18. (v >= 'A' && v <= 'F') ? v - 'A' + 10 : -1)
  19. int
  20. ImagingHexDecode(Imaging im, ImagingCodecState state, UINT8* buf, Py_ssize_t bytes)
  21. {
  22. UINT8* ptr;
  23. int a, b;
  24. ptr = buf;
  25. for (;;) {
  26. if (bytes < 2)
  27. return ptr - buf;
  28. a = HEX(ptr[0]);
  29. b = HEX(ptr[1]);
  30. if (a < 0 || b < 0) {
  31. ptr++;
  32. bytes--;
  33. } else {
  34. ptr += 2;
  35. bytes -= 2;
  36. state->buffer[state->x] = (a<<4) + b;
  37. if (++state->x >= state->bytes) {
  38. /* Got a full line, unpack it */
  39. state->shuffle((UINT8*) im->image[state->y], state->buffer,
  40. state->xsize);
  41. state->x = 0;
  42. if (++state->y >= state->ysize) {
  43. /* End of file (errcode = 0) */
  44. return -1;
  45. }
  46. }
  47. }
  48. }
  49. }