diff --git a/doc/allocators.adoc b/doc/allocators.adoc index 2783c96..d208320 100644 --- a/doc/allocators.adoc +++ b/doc/allocators.adoc @@ -29,7 +29,7 @@ codecs allocates through it and through nothing else. | `over` with a static bound | nothing -| `over` with a bound that is not static +| `over` with a bound that is not static, a decode under a decode bound of at most 4096 bytes aside | its allocator, for the intermediate | the stream transformers of the native formats, `pipe`, `drain`, and `each` and `buffered` with `codecs::capacity` @@ -103,6 +103,11 @@ adds no handling. * The allocator of `over` is for its intermediate alone. The allocator of a token is for its result alone. Neither is the other's. +* The allocator of `over` is not used for a decode whose value codec states + a `max_decode_input_size` of at most 4096 bytes: its intermediate is in place, + whatever the text's type, and an input past the bound is `input_too_long` before + anything is made. A bound above 4096 bytes takes the intermediate, of the exact + size written, from the allocator. * A `std::pmr::monotonic_buffer_resource` never gives memory back: an encode of a megabyte through an arena of a kilobyte throws once, and a loop that reuses the arena must release it. diff --git a/doc/changelog.adoc b/doc/changelog.adoc index f36b04a..d21c456 100644 --- a/doc/changelog.adoc +++ b/doc/changelog.adoc @@ -16,7 +16,7 @@ of every version are on webcpp's index page. === Unreleased // tag::unreleased[] -* Added codecs.hpp and config.hpp: encode, decode, tokens, results, secrecy and over, errors as values. +* Added codecs.hpp and config.hpp: encode, decode, tokens, results, secrecy and over, errors as values, value_formats for a library's value types, and max_decode_input_size as a value codec's bound on every path. * Added the stream contract: one-shot and stream transformers, pipe, drain, each, buffered, encoder and decoder. * Added hex.hpp: hex, constant time, streaming natively and decoding in place. * Added base32.hpp: base32 and base32hex of RFC 4648, padding an option, constant time, streaming natively and decoding in place. @@ -24,6 +24,6 @@ of every version are on webcpp's index page. * Added base58.hpp: basic_base58 with a bound, an alphabet and a checksum as parameters, constant time when secret, streaming through buffered. * Added bech32.hpp: bech32 and bech32m with a maximum length, words and bytes data, constant time and streaming natively. * Added percent.hpp: percent-encoding over Boost.URL with any CharSet, and the target percent, streaming natively. -* Added armor.hpp: RFC 7468's strict armor, composing over any bytes format, its payload constant time, streaming natively. +* Added armor.hpp: RFC 7468's strict armor, composing over any bytes format, its payload constant time, streaming natively, with basic_armor for a label in the type. * Added the guide, with its examples built and run on every target. // end::unreleased[] diff --git a/doc/composition.adoc b/doc/composition.adoc index 6e92758..2b48421 100644 --- a/doc/composition.adoc +++ b/doc/composition.adoc @@ -40,16 +40,39 @@ The intermediate, the bytes between the two links, is held: `std::span` of a static extent, a `secure::secret`, `text`, `bytes`) or the value codec states `max_encoded_size`. There is no cap: the cost is the stack, an array of the middle bound, the inner link's bound on the input's size - for an encode and the outer link's bound on the text's size for a decode, in - every level of a chain of chains; -* *on the heap*, from the allocator of `over`, for any other input. The third + for an encode and, for a decode, the smaller of the outer link's bound on the + text's size and the decode bound, in every level of a chain of chains; +* *in place, under a decode bound*, when the inner link is a value codec that + states `max_decode_input_size`, the longest input its `decode` accepts, and it + is at most 4096 bytes: the decode of a value then holds its intermediate in + place whatever the text's type, a `std::string_view` included, and allocates + nothing. The intermediate is an array of the smaller of that bound and the + outer link's bound on a text of a static size, when the text has one, and it + is refused past the bound with `input_too_long` before it is made (see + "Errors"). The program below is a record whose value codec states 12 bytes + encoded and 16 accepted, through the armor, decoded from a `std::string`; +* *on the heap*, from the allocator of `over`, for any other input: a decode + whose value codec states no decode bound, or one above 4096 bytes, takes it + from the allocator, of exactly the written size. The third template parameter of `over` is that allocator, `std::allocator` by default, deduced from `over{outer, inner, allocator}`. A program that passes an input of a static size and wants the intermediate on the heap passes it as a view of a - dynamic extent, a `std::span` or a `std::string_view`. + dynamic extent, a `std::span` or a `std::string_view`, + unless a decode bound of at most 4096 bytes holds it in place. + +[source,cpp] +---- +include::{examples}/over_value.cpp[tag=example,indent=0] +---- + +[listing] +---- +include::{examples}/over_value.expected[] +---- The allocator of a result or a token is not the intermediate's, and the -allocator of `over` is used for nothing else. A heap intermediate makes the exact +allocator of `over` is used for nothing else, and not for a decode under a +decode bound of at most 4096 bytes. A heap intermediate makes the exact `encoded_size` and `decoded_size`, which run the inner link into it, and `encode` and `decode` allocate: those forms are not `noexcept`, an allocator's exception reaches the caller, and on a secret chain every buffer is wiped @@ -71,6 +94,17 @@ every form reports it before it reads any data, in either link: a chain with refuses every form with `invalid_prefix`. A data error of either link is the chain's: the decode of `414` in the program is the outer link's, hex, odd length. +A decode of a value reports its first error in this order: a fault of a link's +configuration; the outer link's own error, found over the whole text before +anything is written; then, for an exact size of the intermediate past the value +codec's `max_decode_input_size`, `input_too_long`, the error a direct decode of +that input gives, with nothing written, allocated or wiped and the value codec +not called; then the value codec's own error. A text whose payload is 17 bytes, +under a bound of 16, is `input_too_long` when its characters are valid, and the +outer link's `invalid_character` when one is not. `input_too_long` is the error of an +input past a value codec's bound whatever the path, the typed decode of bytes, the +typed decode of text and the call `over` makes of the value codec alike. + A fault a link's encode has (bech32 with no human-readable part, the armor with no label, the format's `encode_fault`) is the chain's too, before any token is prepared: the inner link's in every form, and the outer link's on the @@ -86,6 +120,11 @@ own error. of chains has one in each level's frame. A program that puts a large array through `over` passes a view of a dynamic extent to move the intermediate to the heap. +* A chain of chains is in place for a text of a dynamic size only at the level + whose inner link is a value codec that states a decode bound: the value codec + of `over` itself states none, because a link may accept a text longer than the + longest it writes (the armor's CRLF line ends), so the levels above it take + their intermediate from the allocator, unless the text's size is static. * The inner link's output is bytes, and the outer reads them as bytes, whatever the inner's text was. The armor over hex, as in the program of <>, armors the hex text; a program that wants the armor of diff --git a/doc/formats.adoc b/doc/formats.adoc index 0c23808..8c585d2 100644 --- a/doc/formats.adoc +++ b/doc/formats.adoc @@ -385,6 +385,24 @@ A label given to the call that is longer than 64 characters or outside the grammar is a fault of the configuration, `invalid_prefix` before anything is read. +*A label that is in the type.* When the label follows from what the bytes are, as +a public key's does, a program names it in the type and holds no value that could +be wrong: `formats::basic_armor