#include "unit_specifiers.h" #include namespace unit_specifiers { namespace detail { bool take_utf8(unsigned char, unsigned&, unsigned&, unsigned&, unsigned&); } namespace { bool safe(unsigned char byte) { return (byte >= 'a' && byte <= 'z') || (byte >= 'A' && byte <= 'Z') || (byte >= '0' && byte <= '9') || byte == '_' || byte == ':' || byte == '.'; } int hex_value(unsigned char byte) { if (byte >= '0' && byte <= '9') return byte - '0'; if (byte >= 'a' && byte <= 'f') return byte - 'a' + 10; if (byte >= 'A' && byte <= 'F') return byte - 'A' + 10; return -1; } } // namespace struct Stream::Impl { Mode mode = Mode::escape; Phase phase = Phase::open; std::size_t capacity = 16384; std::size_t input_bytes = 0; unsigned remaining = 0; unsigned received = 0; unsigned lower = 0x80; unsigned upper = 0xbf; unsigned escape_pending = 0; unsigned hex_byte = 0; std::string converted; void clear_progress() { input_bytes = 0; remaining = 0; received = 0; lower = 0x80; upper = 0xbf; escape_pending = 0; hex_byte = 0; converted.clear(); } Error fail() { phase = Phase::invalid; clear_progress(); return Error::invalid; } bool decoded_byte(unsigned char byte) { if (!detail::take_utf8(byte, remaining, received, lower, upper)) return false; converted.push_back(static_cast(byte)); return true; } bool encoded_byte(unsigned char byte) { if (!detail::take_utf8(byte, remaining, received, lower, upper)) return false; if (safe(byte) && !(input_bytes == 0 && byte == '.')) { converted.push_back(static_cast(byte)); } else { static constexpr char digits[] = "0123456789abcdef"; converted.push_back('\\'); converted.push_back('x'); converted.push_back(digits[byte >> 4]); converted.push_back(digits[byte & 15]); } return true; } bool unescape_byte(unsigned char byte) { if (escape_pending == 0) { if (safe(byte)) return decoded_byte(byte); if (byte == '\\') { escape_pending = 1; return true; } return false; } if (escape_pending == 1) { if (byte != 'x') return false; escape_pending = 2; return true; } const int nibble = hex_value(byte); if (nibble < 0) return false; if (escape_pending == 2) { hex_byte = static_cast(nibble) << 4; escape_pending = 3; return true; } const unsigned value = hex_byte | static_cast(nibble); escape_pending = 0; hex_byte = 0; return decoded_byte(static_cast(value)); } }; Stream::Stream() : impl_(std::make_unique()) {} Stream::~Stream() = default; Error Stream::reset(Mode mode, std::size_t capacity) { if ((mode != Mode::escape && mode != Mode::unescape) || capacity > 16384) return Error::invalid; impl_->clear_progress(); impl_->mode = mode; impl_->capacity = capacity; impl_->phase = Phase::open; return Error::none; } Error Stream::feed(std::string_view chunk) { if (impl_->phase != Phase::open) return Error::invalid; const std::size_t limit = impl_->mode == Mode::escape ? 4096 : 16384; if (chunk.size() > limit - impl_->input_bytes) return impl_->fail(); for (unsigned char byte : chunk) { const bool okay = impl_->mode == Mode::escape ? impl_->encoded_byte(byte) : impl_->unescape_byte(byte); if (!okay) return impl_->fail(); ++impl_->input_bytes; } return Error::none; } Error Stream::finish() { if (impl_->phase == Phase::finished) return Error::none; if (impl_->phase == Phase::invalid) return Error::invalid; if (impl_->remaining != 0 || impl_->escape_pending != 0 || impl_->converted.size() > impl_->capacity) return impl_->fail(); impl_->phase = Phase::finished; return Error::none; } StreamView Stream::view() const { StreamView output; output.mode = impl_->mode; output.phase = impl_->phase; output.capacity = impl_->capacity; output.input_bytes = impl_->input_bytes; output.utf8_pending = impl_->received; output.escape_pending = impl_->escape_pending; if (impl_->phase == Phase::finished) output.result = impl_->converted; return output; } namespace { Error transform(std::string_view input, Mode mode, std::string& output) { output.clear(); Stream stream; if (stream.reset(mode, 16384) != Error::none || stream.feed(input) != Error::none || stream.finish() != Error::none) return Error::invalid; output = stream.view().result; return Error::none; } } // namespace Error escape(std::string_view text, std::string& output) { return transform(text, Mode::escape, output); } Error unescape(std::string_view encoded, std::string& output) { return transform(encoded, Mode::unescape, output); } } // namespace unit_specifiers