dec_uint64.c 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. // If we have native uint64's, pick off 8 bytes at a time for as long as we
  2. // can, but make sure that we quit before seeing any == markers at the end of
  3. // the string. Also, because we write two zeroes at the end of the output,
  4. // ensure that there are at least 3 valid bytes of input data remaining to
  5. // close the gap. 8 + 2 + 3 = 13 bytes:
  6. while (srclen >= 13)
  7. {
  8. uint64_t str, res, dec;
  9. // Load string:
  10. //str = *(uint64_t *)c;
  11. memcpy(&str, c, sizeof(str));
  12. // Shuffle bytes to 64-bit bigendian:
  13. str = cpu_to_be64(str);
  14. // Lookup each byte in the decoding table; if we encounter any
  15. // "invalid" values, fall back on the bytewise code:
  16. if ((dec = plain64_base64_table_dec[str >> 56]) > 63) {
  17. break;
  18. }
  19. res = dec << 58;
  20. if ((dec = plain64_base64_table_dec[(str >> 48) & 0xFF]) > 63) {
  21. break;
  22. }
  23. res |= dec << 52;
  24. if ((dec = plain64_base64_table_dec[(str >> 40) & 0xFF]) > 63) {
  25. break;
  26. }
  27. res |= dec << 46;
  28. if ((dec = plain64_base64_table_dec[(str >> 32) & 0xFF]) > 63) {
  29. break;
  30. }
  31. res |= dec << 40;
  32. if ((dec = plain64_base64_table_dec[(str >> 24) & 0xFF]) > 63) {
  33. break;
  34. }
  35. res |= dec << 34;
  36. if ((dec = plain64_base64_table_dec[(str >> 16) & 0xFF]) > 63) {
  37. break;
  38. }
  39. res |= dec << 28;
  40. if ((dec = plain64_base64_table_dec[(str >> 8) & 0xFF]) > 63) {
  41. break;
  42. }
  43. res |= dec << 22;
  44. if ((dec = plain64_base64_table_dec[str & 0xFF]) > 63) {
  45. break;
  46. }
  47. res |= dec << 16;
  48. // Reshuffle and repack into 6-byte output format:
  49. res = be64_to_cpu(res);
  50. // Store back:
  51. //*(uint64_t *)o = res;
  52. memcpy(o, &res, sizeof(res));
  53. c += 8;
  54. o += 6;
  55. outl += 6;
  56. srclen -= 8;
  57. }