dec_uint32.c 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. // If we have native uint32's, pick off 4 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 a zero at the end of the output, ensure
  4. // that there are at least 2 valid bytes of input data remaining to close the
  5. // gap. 4 + 2 + 2 = 8 bytes:
  6. while (srclen >= 8)
  7. {
  8. uint32_t str, res, dec;
  9. // Load string:
  10. //str = *(uint32_t *)c;
  11. memcpy(&str, c, sizeof(str));
  12. // Shuffle bytes to 32-bit bigendian:
  13. str = cpu_to_be32(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 = plain32_base64_table_dec[str >> 24]) > 63) {
  17. break;
  18. }
  19. res = dec << 26;
  20. if ((dec = plain32_base64_table_dec[(str >> 16) & 0xFF]) > 63) {
  21. break;
  22. }
  23. res |= dec << 20;
  24. if ((dec = plain32_base64_table_dec[(str >> 8) & 0xFF]) > 63) {
  25. break;
  26. }
  27. res |= dec << 14;
  28. if ((dec = plain32_base64_table_dec[str & 0xFF]) > 63) {
  29. break;
  30. }
  31. res |= dec << 8;
  32. // Reshuffle and repack into 3-byte output format:
  33. res = be32_to_cpu(res);
  34. // Store back:
  35. //*(uint32_t *)o = res;
  36. memcpy(o, &res, sizeof(res));
  37. c += 4;
  38. o += 3;
  39. outl += 3;
  40. srclen -= 4;
  41. }