hex.cpp 913 B

123456789101112131415161718192021222324252627282930
  1. #include "hex.h"
  2. #include "output.h"
  3. #include <util/string/hex.h>
  4. void HexEncode(const void* in, size_t len, IOutputStream& out) {
  5. static const size_t NUM_OF_BYTES = 32;
  6. char buffer[NUM_OF_BYTES * 2];
  7. auto current = static_cast<const char*>(in);
  8. for (size_t take = 0; len; current += take, len -= take) {
  9. take = Min(NUM_OF_BYTES, len);
  10. HexEncode(current, take, buffer);
  11. out.Write(buffer, take * 2);
  12. }
  13. }
  14. void HexDecode(const void* in, size_t len, IOutputStream& out) {
  15. Y_ENSURE(!(len & 1), TStringBuf("Odd buffer length passed to HexDecode"));
  16. static const size_t NUM_OF_BYTES = 32;
  17. char buffer[NUM_OF_BYTES];
  18. auto current = static_cast<const char*>(in);
  19. for (size_t take = 0; len; current += take, len -= take) {
  20. take = Min(NUM_OF_BYTES * 2, len);
  21. HexDecode(current, take, buffer);
  22. out.Write(buffer, take / 2);
  23. }
  24. }