enc_uint64.c 1.1 KB

12345678910111213141516171819202122232425262728293031
  1. // If we have 64-bit ints, pick off 6 bytes at a time for as long as we can,
  2. // but ensure that there are at least 8 bytes available to avoid segfaulting:
  3. while (srclen >= 8)
  4. {
  5. // Load string:
  6. //uint64_t str = *(uint64_t *)c;
  7. uint64_t str;
  8. memcpy(&str, c, sizeof(str));
  9. // Reorder to 64-bit big-endian, if not already in that format. The
  10. // workset must be in big-endian, otherwise the shifted bits do not
  11. // carry over properly among adjacent bytes:
  12. str = cpu_to_be64(str);
  13. // Shift input by 6 bytes each round and mask in only the lower 6 bits;
  14. // look up the character in the Base64 encoding table and write it to
  15. // the output location:
  16. *o++ = plain64_base64_table_enc[(str >> 58) & 0x3F];
  17. *o++ = plain64_base64_table_enc[(str >> 52) & 0x3F];
  18. *o++ = plain64_base64_table_enc[(str >> 46) & 0x3F];
  19. *o++ = plain64_base64_table_enc[(str >> 40) & 0x3F];
  20. *o++ = plain64_base64_table_enc[(str >> 34) & 0x3F];
  21. *o++ = plain64_base64_table_enc[(str >> 28) & 0x3F];
  22. *o++ = plain64_base64_table_enc[(str >> 22) & 0x3F];
  23. *o++ = plain64_base64_table_enc[(str >> 16) & 0x3F];
  24. c += 6; // 6 bytes of input
  25. outl += 8; // 8 bytes of output
  26. srclen -= 6;
  27. }