enc_uint32.c 898 B

123456789101112131415161718192021222324
  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. // Reorder to 32-bit big-endian, if not already in that format. The
  8. // workset must be in big-endian, otherwise the shifted bits do not
  9. // carry over properly among adjacent bytes:
  10. str = cpu_to_be32(str);
  11. // Shift input by 6 bytes each round and mask in only the lower 6 bits;
  12. // look up the character in the Base64 encoding table and write it to
  13. // the output location:
  14. *o++ = neon32_base64_table_enc[(str >> 26) & 0x3F];
  15. *o++ = neon32_base64_table_enc[(str >> 20) & 0x3F];
  16. *o++ = neon32_base64_table_enc[(str >> 14) & 0x3F];
  17. *o++ = neon32_base64_table_enc[(str >> 8) & 0x3F];
  18. c += 3; // 3 bytes of input
  19. outl += 4; // 4 bytes of output
  20. srclen -= 3;
  21. }