Documentation

QedShareCli.Utf8Decode

Reading share text with characters that are not ASCII #

No share has a character outside ASCII, but text given as a share can. decodeAscii only reads ASCII bytes. decodeUtf8 reads any other text without a list as long as the text:

decodeUtf8_eq proves that it returns exactly what decodeShare returns.

Replacing characters that are not ASCII #

c if it is ASCII, ~ otherwise.

Equations
Instances For

    substChar gives an ASCII character.

    substChar keeps ASCII characters.

    theorem QedShare.Cli.substChar_of_ne (c : Char) (h : substChar c ≠ '~') :

    A character that does not become ~ is kept.

    theorem QedShare.Cli.substChar_eq_iff (c d : Char) (hd : d.val.toNat ≤ 127) (hd' : d ≠ '~') :
    substChar c = d ↔ c = d

    substChar c is an ASCII character other than ~ only if c is that character.

    substChar keeps whitespace, and makes none.

    theorem QedShare.Cli.not_le_ascii (c d : Char) (h : ¬c.val.toNat ≤ 127) (hd : d.val.toNat ≤ 127) :
    ¬c ≤ d

    A character that is not ASCII is above every ASCII character.

    substChar keeps hex digits, and makes none.

    Hex-decoding does not see the replacement.

    theorem QedShare.Cli.map_substChar_eq_iff (t L : List Char) (hL : ∀ (c : Char), c ∈ L → c.val.toNat ≤ 127 ∧ c ≠ '~') :

    Text is replaced into ASCII text without ~ only if it is that text.

    The tag does not see the replacement.

    Decoding does not see the replacement: the text with every character that is not ASCII replaced by ~ decodes to the same record, or fails with the same error.

    looksHex does not see the replacement.

    decodeShare does not see the replacement in hex text.

    theorem QedShare.Cli.decodeShare_tooLong (label s : String) (h1 : looksHex (trimWs s) = false) (h2 : (trimWs s).length > maxWordShareChars) :
    decodeShare label s = Except.error (toString label ++ toString ": too long to be a word share")

    Text that is not hex and too long is not a word share.

    The replacement on bytes #

    What the replacement does to one byte of UTF-8: an ASCII byte is kept, the first byte of a longer character becomes ~, and its other bytes go away.

    Equations
    Instances For
      theorem QedShare.Cli.substByte_lead (x : Nat) :
      substByte (UInt8.ofNat (x % 32 + 192)) = some 126 ∧ substByte (UInt8.ofNat (x % 16 + 224)) = some 126 ∧ substByte (UInt8.ofNat (x % 8 + 240)) = some 126

      The first byte of a character of two, three or four bytes becomes ~.

      The other bytes of such a character go away.

      substByte on the bytes of a character gives the byte of the replaced character.

      substByte on the bytes of text gives the bytes of the replaced text.

      Loop for substBytes.

      Equations
      Instances For

        The bytes of the text with every character that is not ASCII replaced by ~ (substBytes_toUTF8).

        Equations
        Instances For

          substLoop is List.filterMap on the bytes it reads.

          theorem QedShare.Cli.substBytes_ascii (bs : ByteArray) (b : UInt8) :
          b ∈ (substBytes bs).data.toList → b < 128

          substBytes gives ASCII bytes.

          substBytes on the bytes of a text gives the bytes of the replaced text.

          Trimming on the bytes #

          theorem QedShare.Cli.trimWs_pad (pre m post : List Char) (hpre : ∀ (c : Char), c ∈ pre → c.isWhitespace = true) (hpost : ∀ (c : Char), c ∈ post → c.isWhitespace = true) :

          Whitespace around text does not change what trimWs gives.

          theorem QedShare.Cli.readerSpec_congr (label s m : String) (h : trimWs s = trimWs m) :
          readerSpec label s = readerSpec label m

          A reader only looks at the trimmed text.

          theorem QedShare.Cli.isWsByte_ascii (b : UInt8) :
          isWsByte b = true → b < 128

          Whitespace bytes are ASCII.

          Whitespace bytes are the UTF-8 bytes of whitespace characters.

          theorem QedShare.Cli.trimWs_extract (bs : ByteArray) (h : bs.IsValidUTF8) (i j : Nat) (hij : i ≤ j) (hjn : j ≤ bs.size) (hpre : (segB bs 0 i).all isWsByte = true) (hpost : (segB bs j bs.size).all isWsByte = true) (hv : (bs.extract i j).IsValidUTF8) :
          trimWs { toByteArray := bs, isValidUTF8 := h } = trimWs { toByteArray := bs.extract i j, isValidUTF8 := hv }

          Cutting whitespace bytes off both ends of a text does not change what trimWs gives.

          The list-based reader on the text without the whitespace around it: the ends found by skipWs and skipWsEnd are checked, and the text between them is checked to be UTF-8. Reads what the list-based reader reads on the whole text (trimmedReader_eq).

          Equations
          • One or more equations did not get rendered due to their size.
          Instances For
            theorem QedShare.Cli.trimmedReader_eq (label : String) (bs : ByteArray) (h : bs.IsValidUTF8) :
            trimmedReader label bs h = readerSpec label { toByteArray := bs, isValidUTF8 := h }

            trimmedReader reads what the list-based reader reads.

            The reader #

            looksHex on the trimmed text does not see the replacement.

            The length of the trimmed text does not see the replacement.

            Read one share from the bytes of any text without a list as long as the text. Returns what looksHex and decodeShare return (decodeUtf8_eq).

            Short text goes to the list-based reader. In longer text, every character that is not ASCII is replaced by ~ and the result is read by decodeAscii: for hex text that is the answer (decodeShare_subst). Text that is not hex is too long to be a word share if its trimmed part has more than maxWordShareChars characters (the replaced text has as many); shorter than that, the trimmed part goes to the list-based reader (trimmedReader).

            Equations
            • One or more equations did not get rendered due to their size.
            Instances For
              theorem QedShare.Cli.decodeUtf8_eq (label : String) (bs : ByteArray) (h : bs.IsValidUTF8) :
              decodeUtf8 label bs h = readerSpec label { toByteArray := bs, isValidUTF8 := h }

              decodeUtf8 reads what decodeShare reads. For the bytes of any text, it returns whether the text is in the hex form (looksHex), and the packed record decodeShare returns, or the same error.

              The reader for text that is not a well-formed hex share: decodeAscii for ASCII text, decodeUtf8 for the rest.

              Equations
              Instances For
                theorem QedShare.Cli.decodeOther_eq (label s : String) :
                decodeOther label s = readerSpec label s

                decodeOther reads what decodeShare reads, packed.

                Read one share, hex or words, into a packed record, and say whether it is in the hex form. Every text is read from its bytes; only text that is short once trimmed goes through the lists of decodeShare. Returns exactly what looksHex and decodeShare return, the record packed (decodePackedForm_eq).

                Equations
                Instances For

                  Read one share, hex or words, into a packed record. Returns exactly what decodeShare returns, packed (decodePacked_eq).

                  Equations
                  Instances For

                    decodePackedForm reads what decodeShare reads, packed. For every label and text, it returns whether the text is in the hex form (looksHex), and the packed record of the record decodeShare returns, or the same error.

                    decodePacked reads what decodeShare reads, packed. For every label and text, decodePacked returns the packed record of the record decodeShare returns, or the same error.

                    decodePackedForm on the UTF-8 bytes of the text: what the executable calls, so that the text is never copied into a String (decodeBytes_eq). Only short text is made into a string, for decodeShare.

                    Equations
                    • One or more equations did not get rendered due to their size.
                    Instances For
                      theorem QedShare.Cli.decodeBytes_eq (label : String) (bs : ByteArray) (h : bs.IsValidUTF8) :
                      decodeBytes label bs h = readerSpec label { toByteArray := bs, isValidUTF8 := h }

                      decodeBytes is decodePackedForm on the text with those bytes, so it returns what looksHex and decodeShare return for that text.

                      Whether the text with these bytes is in the hex form: looksHex on the trimmed text, read from the bytes (looksHexFast_eq). ASCII text is looked at directly (the ends found by skipWs and skipWsEnd are checked); for any other text the reader says it. Used to decide how a file is cut into shares.

                      Equations
                      • One or more equations did not get rendered due to their size.
                      Instances For
                        theorem QedShare.Cli.looksHexFast_eq (bs : ByteArray) (h : bs.IsValidUTF8) :
                        looksHexFast bs h = looksHex (trimWs { toByteArray := bs, isValidUTF8 := h })

                        looksHexFast is looksHex on the trimmed text.