Mirror of :pserver:cvs@cvs.fefe.de:/cvs libowfat https://www.fefe.de/libowfat/
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.
 
 
 
 

39 lines
1.1 KiB

  1. #include "scan.h"
  2. #ifdef UNITTEST
  3. #undef UNITTEST
  4. #include "scan_fromhex.c"
  5. #define UNITTEST
  6. #endif
  7. size_t scan_xlongn(const char *src,size_t n,unsigned long *dest) {
  8. register const char *tmp=src;
  9. register unsigned long l=0;
  10. register unsigned char c;
  11. while (n-->0 && (l>>(sizeof(l)*8-4))==0 && (c=(unsigned char)scan_fromhex((unsigned char)*tmp))<16) {
  12. l=(l<<4)+c;
  13. ++tmp;
  14. }
  15. *dest=l;
  16. return (size_t)(tmp-src);
  17. }
  18. #ifdef UNITTEST
  19. #include <assert.h>
  20. int main() {
  21. unsigned long i;
  22. assert(scan_xlongn("fefe",4,&i)==4 && i==0xfefe);
  23. assert(scan_xlongn("0xfefe",6,&i)==1); // 0x not supported, will scan the 0
  24. assert(scan_xlongn("+fefe",5,&i)==0);
  25. assert(scan_xlongn("fefec0de",7,&i)==7 && i==0xfefec0d);
  26. assert(scan_xlongn("fefec0de",8,&i)==8 && i==0xfefec0de);
  27. assert(scan_xlongn("fefec0de",9,&i)==8 && i==0xfefec0de);
  28. if (sizeof(i)==4)
  29. assert(scan_xlongn("fefec0debaad",14,&i)==8 && i==0xfefec0de); // test truncation
  30. else if (sizeof(i)==8)
  31. assert(scan_xlongn("fefec0debaadc0debl06",40,&i)==16 && i==0xfefec0debaadc0de); // test truncation
  32. return 0;
  33. }
  34. #endif