enc_uint32.c 954 B

123456789101112131415161718192021222324252627
  1. // If we have 32-bit ints, pick off 3 bytes at a time for as long as we can,
  2. // but ensure that there are at least 4 bytes available to avoid segfaulting:
  3. while (srclen >= 4)
  4. {
  5. // Load string:
  6. //uint32_t str = *(uint32_t *)c;
  7. uint32_t str;
  8. memcpy(&str, c, sizeof(str));
  9. // Reorder to 32-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_be32(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++ = plain32_base64_table_enc[(str >> 26) & 0x3F];
  17. *o++ = plain32_base64_table_enc[(str >> 20) & 0x3F];
  18. *o++ = plain32_base64_table_enc[(str >> 14) & 0x3F];
  19. *o++ = plain32_base64_table_enc[(str >> 8) & 0x3F];
  20. c += 3; // 3 bytes of input
  21. outl += 4; // 4 bytes of output
  22. srclen -= 3;
  23. }