• Top
    • Documentation
    • Books
    • Recursion-and-induction
    • Boolean-reasoning
    • Debugging
    • Projects
    • Std
    • Proof-automation
    • Macro-libraries
    • ACL2
    • Interfacing-tools
    • Hardware-verification
    • Software-verification
      • Kestrel-books
      • X86isa
        • Program-execution
        • Introduction
        • X86isa-build-instructions
        • Publications
        • Contributors
        • Machine
          • Syscalls
          • Cpuid
          • X86isa-state
          • Linear-memory
          • Rflag-specifications
          • Characterizing-undefined-behavior
          • Top-level-memory
          • App-view
          • X86-decoder
          • Physical-memory
          • Decoding-and-spec-utils
          • Instructions
          • X86-modes
          • Segmentation
          • Register-readers-and-writers
          • Other-non-deterministic-computations
          • Environment
            • Components-of-the-environment-field
            • Reading-memory-as-strings-or-bytes
            • Converting-between-strings-and-bytes
              • Writing-strings-or-bytes-to-memory
            • Paging
          • Implemented-opcodes
          • Proof-utilities
          • To-do
          • Concrete-simulation-examples
          • Model-validation
          • Utils
          • Debugging-code-proofs
        • Axe
        • Execloader
      • Math
      • Testing-utilities
    • Environment

    Converting-between-strings-and-bytes

    Definitions and Theorems

    Function: bytes-to-charlist

    (defun bytes-to-charlist (bytes)
      (declare (xargs :guard (byte-listp bytes)))
      (let ((__function__ 'bytes-to-charlist))
        (declare (ignorable __function__))
        (if (endp bytes)
            nil
          (cons (code-char (car bytes))
                (bytes-to-charlist (cdr bytes))))))

    Theorem: character-listp-of-bytes-to-charlist

    (defthm character-listp-of-bytes-to-charlist
      (implies (force (byte-listp bytes))
               (b* ((lst (bytes-to-charlist bytes)))
                 (character-listp lst)))
      :rule-classes :type-prescription)

    Function: charlist-to-bytes

    (defun charlist-to-bytes (charlist)
      (declare (xargs :guard (character-listp charlist)))
      (let ((__function__ 'charlist-to-bytes))
        (declare (ignorable __function__))
        (if (endp charlist)
            nil
          (cons (char-code (car charlist))
                (charlist-to-bytes (cdr charlist))))))

    Theorem: byte-listp-of-charlist-to-bytes

    (defthm byte-listp-of-charlist-to-bytes
      (implies (force (character-listp bytes))
               (b* ((lst (charlist-to-bytes charlist)))
                 (byte-listp lst)))
      :rule-classes :type-prescription)

    Theorem: same-length-of-byte-and-character-lists

    (defthm same-length-of-byte-and-character-lists
      (implies (character-listp charlist)
               (equal (len (charlist-to-bytes charlist))
                      (len charlist))))

    Function: string-to-bytes

    (defun string-to-bytes (str)
      (declare (xargs :guard (stringp str)))
      (let ((__function__ 'string-to-bytes))
        (declare (ignorable __function__))
        (charlist-to-bytes (coerce str 'list))))

    Theorem: byte-listp-of-string-to-bytes

    (defthm byte-listp-of-string-to-bytes
      (implies (force (stringp str))
               (b* ((lst (string-to-bytes str)))
                 (byte-listp lst)))
      :rule-classes :type-prescription)

    Function: bytes-to-string

    (defun bytes-to-string (bytes)
      (declare (xargs :guard (byte-listp bytes)))
      (let ((__function__ 'bytes-to-string))
        (declare (ignorable __function__))
        (coerce (bytes-to-charlist bytes)
                'string)))

    Theorem: stringp-of-bytes-to-string

    (defthm stringp-of-bytes-to-string
      (implies (force (byte-listp bytes))
               (b* ((lst (bytes-to-string bytes)))
                 (stringp lst)))
      :rule-classes :type-prescription)

    Function: read-n-bytes-from-string

    (defun read-n-bytes-from-string (n str)
      (declare (xargs :guard (and (natp n) (stringp str))))
      (let ((__function__ 'read-n-bytes-from-string))
        (declare (ignorable __function__))
        (let* ((bytes (string-to-bytes str))
               (n-bytes (take n bytes)))
          n-bytes)))

    Theorem: take-byte-listp

    (defthm take-byte-listp
      (implies (and (byte-listp xs)
                    (natp n)
                    (<= n (len xs)))
               (byte-listp (take n xs))))

    Theorem: byte-listp-read-n-bytes-from-string

    (defthm byte-listp-read-n-bytes-from-string
      (implies (and (natp n)
                    (< n (len (string-to-bytes str)))
                    (stringp str))
               (byte-listp (read-n-bytes-from-string n str)))
      :rule-classes :type-prescription)

    Function: read-n-bytes-from-string-as-string

    (defun read-n-bytes-from-string-as-string (n str)
      (declare (xargs :guard (and (natp n) (stringp str))))
      (let ((__function__ 'read-n-bytes-from-string-as-string))
        (declare (ignorable __function__))
        (b* ((n-bytes (read-n-bytes-from-string n str))
             ((when (not (byte-listp n-bytes))) nil)
             (new-charlist (bytes-to-charlist n-bytes))
             (new-str (coerce new-charlist 'string)))
          new-str)))

    Theorem: stringp-read-n-bytes-from-string-as-string

    (defthm stringp-read-n-bytes-from-string-as-string
      (implies (and (natp n)
                    (< n (len (string-to-bytes str)))
                    (stringp str))
               (stringp (read-n-bytes-from-string-as-string n str)))
      :rule-classes :type-prescription)