From 2aa4a82499d4becd2284cdb482213d541b8804dd Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 28 Apr 2024 16:29:10 +0200 Subject: Adding upstream version 86.0.1. Signed-off-by: Daniel Baumann --- dom/script/ScriptDecoding.h | 96 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 dom/script/ScriptDecoding.h (limited to 'dom/script/ScriptDecoding.h') diff --git a/dom/script/ScriptDecoding.h b/dom/script/ScriptDecoding.h new file mode 100644 index 0000000000..a54d120240 --- /dev/null +++ b/dom/script/ScriptDecoding.h @@ -0,0 +1,96 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set ts=8 sts=2 et sw=2 tw=80: */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +/* Script decoding templates for processing byte data as UTF-8 or UTF-16. */ + +#ifndef mozilla_dom_ScriptDecoding_h +#define mozilla_dom_ScriptDecoding_h + +#include "mozilla/Assertions.h" // MOZ_ASSERT +#include "mozilla/CheckedInt.h" // mozilla::CheckedInt +#include "mozilla/Encoding.h" // mozilla::Decoder +#include "mozilla/Span.h" // mozilla::Span +#include "mozilla/Tuple.h" // mozilla::Tie +#include "mozilla/UniquePtr.h" // mozilla::UniquePtr +#include "mozilla/Unused.h" // mozilla::Unused + +#include // size_t +#include // uint8_t, uint32_t +#include // std::is_same + +namespace mozilla { +namespace dom { + +template +struct ScriptDecoding { + static_assert(std::is_same::value || + std::is_same::value, + "must be either UTF-8 or UTF-16"); +}; + +template <> +struct ScriptDecoding { + static CheckedInt MaxBufferLength(const UniquePtr& aDecoder, + uint32_t aByteLength) { + return aDecoder->MaxUTF16BufferLength(aByteLength); + } + + static size_t DecodeInto(const UniquePtr& aDecoder, + const Span& aSrc, + Span aDest, bool aEndOfSource) { + uint32_t result; + size_t read; + size_t written; + bool hadErrors; + Tie(result, read, written, hadErrors) = + aDecoder->DecodeToUTF16(aSrc, aDest, aEndOfSource); + MOZ_ASSERT(result == kInputEmpty); + MOZ_ASSERT(read == aSrc.Length()); + MOZ_ASSERT(written <= aDest.Length()); + Unused << hadErrors; + + return written; + } +}; + +template <> +struct ScriptDecoding { + static CheckedInt MaxBufferLength(const UniquePtr& aDecoder, + uint32_t aByteLength) { + return aDecoder->MaxUTF8BufferLength(aByteLength); + } + + static size_t DecodeInto(const UniquePtr& aDecoder, + const Span& aSrc, + Span aDest, bool aEndOfSource) { + uint32_t result; + size_t read; + size_t written; + bool hadErrors; + // Until C++ char8_t happens, our decoder APIs deal in |uint8_t| while + // |Utf8Unit| internally deals with |char|, so there's inevitable impedance + // mismatch between |aDest| as |Utf8Unit| and |AsWritableBytes(aDest)| as + // |Span|. :-( The written memory will be interpreted through + // |char Utf8Unit::mValue| which is *permissible* because any object's + // memory can be interpreted as |char|. Unfortunately, until + // twos-complement is mandated, we have to play fast and loose and *hope* + // interpreting memory storing |uint8_t| as |char| will pick up the desired + // wrapped-around value. ¯\_(ツ)_/¯ + Tie(result, read, written, hadErrors) = + aDecoder->DecodeToUTF8(aSrc, AsWritableBytes(aDest), aEndOfSource); + MOZ_ASSERT(result == kInputEmpty); + MOZ_ASSERT(read == aSrc.Length()); + MOZ_ASSERT(written <= aDest.Length()); + Unused << hadErrors; + + return written; + } +}; + +} // namespace dom +} // namespace mozilla + +#endif // mozilla_dom_ScriptDecoding_h -- cgit v1.2.3