hexparse.c 558 B

12345678910111213141516171819202122232425
  1. #include "hexparse.h"
  2. static int hexdigit(char x)
  3. {
  4. if (x >= '0' && x <= '9') return x - '0';
  5. if (x >= 'a' && x <= 'f') return 10 + (x - 'a');
  6. if (x >= 'A' && x <= 'F') return 10 + (x - 'A');
  7. return -1;
  8. }
  9. int hexparse(unsigned char *y,long long len,const char *x)
  10. {
  11. if (!x) return 0;
  12. while (len > 0) {
  13. int digit0;
  14. int digit1;
  15. digit0 = hexdigit(x[0]); if (digit0 == -1) return 0;
  16. digit1 = hexdigit(x[1]); if (digit1 == -1) return 0;
  17. *y++ = digit1 + 16 * digit0;
  18. --len;
  19. x += 2;
  20. }
  21. if (x[0]) return 0;
  22. return 1;
  23. }