1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969
|
;; Processing Unicode Files with ACL2
;; Copyright (C) 2005-2006 by Jared Davis <jared@cs.utexas.edu>
;;
;; This program is free software; you can redistribute it and/or modify it
;; under the terms of the GNU General Public License as published by the Free
;; Software Foundation; either version 2 of the License, or (at your option)
;; any later version.
;;
;; This program is distributed in the hope that it will be useful but WITHOUT
;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
;; FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
;; more details.
;;
;; You should have received a copy of the GNU General Public License along with
;; this program; if not, write to the Free Software Foundation, Inc., 59 Temple
;; Place - Suite 330, Boston, MA 02111-1307, USA.
(in-package "ACL2")
(set-verify-guards-eagerness 2)
(set-state-ok t)
(include-book "read-byte")
(local (include-book "unsigned-byte-listp"))
(local (include-book "signed-byte-listp"))
(include-book "sign-byte")
(include-book "combine")
(local (include-book "arithmetic-3/bind-free/top" :dir :system))
;; Below are many similar functions, which we have aggregated into the
;; following macro:
;;
;; (read-bytes$ channel [:bytes bytes]
;; [:signed signed]
;; [:end end])
;;
;; If you provide :bytes, it must be either 1, 2, or 4. (default is 1)
;; If you provide :signed, it must be either t or nil. (default is nil)
;; If you provide :end, it must be either big or little. (default is big)
;;
;; The macro simply expands into the appropriate function from the table
;; below. (For the 1-byte readers, :end does not matter.)
;;
;; Name Bytes Read Value Range Endian-ness
;; read-byte$ 1 [0, 2^8-1] N/A
;; read-8s 1 [-2^7, 2^7-1] N/A
;; read-16ube 2 [0, 2^16-1] Big Endian
;; read-16ule 2 [0, 2^16-1] Little Endian
;; read-16sbe 2 [-2^15, 2^15-1] Big Endian
;; read-16sle 2 [-2^15, 2^15-1] Little Endian
;; read-32ube 4 [0, 2^32-1] Big Endian
;; read-32ule 4 [0, 2^32-1] Little Endian
;; read-32sbe 4 [-2^32, 2^32-1] Big Endian
;; read-32sle 4 [-2^32, 2^32-1] Little Endian
;;
;; We also provide another macro which allows you to read a fixed sequence of
;; same-length quantifies. The interface is very similar to the above:
;;
;; (read-bytes$-n n channel [:bytes bytes]
;; [:signed signed]
;; [:end end])
;;
;; Here n must be a natural number, while :bytes, :signed, and :end must
;; conform to the same restrictions as before. The macro expands to a list
;; version of the appropriate function, which is logically a simple recursion
;; until n reaches zero, but under the hood is implemented in a tail recursive
;; manner for greater efficiency.
;;
;; These functions return fail if EOF is reached before (n * bytes) are read
;; from the stream. Otherwise, they return an n-element list where every
;; member is constructed in accordance with the table above.
(local (defthm rationalp-when-integerp
(implies (integerp x)
(rationalp x))))
;; read-8s
(defun read-8s (channel state)
"Read a signed byte from an input channel."
(declare (xargs :guard (and (state-p state)
(symbolp channel)
(open-input-channel-p channel :byte state))
:verify-guards nil))
(mv-let (byte state)
(read-byte$ channel state)
(if byte
(mv (mbe :logic (sign-byte byte)
:exec (if (< (the-fixnum byte) 128)
byte
(the-fixnum
(- (the-fixnum byte) 256))))
state)
(mv nil state))))
(verify-guards read-8s
:hints(("Goal" :in-theory (enable sign-byte signed-byte-p))))
(defthm read-8s-signed-byte
(implies (and (force (state-p1 state))
(force (open-input-channel-p1 channel :byte state))
(force (symbolp channel))
(car (read-8s channel state)))
(signed-byte-p 8 (car (read-8s channel state)))))
(defthm read-8s-integer
(implies (and (force (state-p1 state))
(force (open-input-channel-p1 channel :byte state))
(force (symbolp channel))
(car (read-8s channel state)))
(integerp (car (read-8s channel state)))))
(defthm read-8s-range
(implies (and (force (state-p1 state))
(force (open-input-channel-p1 channel :byte state))
(force (symbolp channel))
(car (read-8s channel state)))
(and (<= -128 (car (read-8s channel state)))
(<= (- (expt 2 7)) (car (read-8s channel state)))
(< (car (read-8s channel state)) 128)
(< (car (read-8s channel state)) (expt 2 7))))
:rule-classes :linear
:hints(("Goal"
:in-theory (enable signed-byte-p)
:use (:instance read-8s-signed-byte))))
(defthm read-8s-state
(implies (and (force (state-p1 state))
(force (open-input-channel-p1 channel :byte state))
(force (symbolp channel)))
(state-p1 (mv-nth 1 (read-8s channel state)))))
(defthm read-8s-open-input-channel-p1
(implies (and (force (state-p1 state))
(force (open-input-channel-p1 channel :byte state))
(force (symbolp channel)))
(open-input-channel-p1 channel
:byte
(mv-nth 1 (read-8s channel state)))))
(in-theory (disable read-8s))
;; read-16ube
(defun read-16ube (channel state)
(declare (xargs :guard (and (state-p state)
(symbolp channel)
(open-input-channel-p channel :byte state))
:guard-hints(("Goal" :in-theory (enable combine16u)))))
(mv-let (x1 state) (read-byte$ channel state)
(mv-let (x2 state) (read-byte$ channel state)
(if (null x1)
(mv nil state)
(if (null x2)
(mv 'fail state)
(mv (mbe :logic (combine16u x1 x2)
:exec (logior
(the (unsigned-byte 16)
(ash (the (unsigned-byte 8) x1) 8))
(the (unsigned-byte 8)
x2)))
state))))))
(defthm read-16ube-unsigned-byte
(implies (and (force (state-p1 state))
(force (open-input-channel-p1 channel :byte state))
(force (symbolp channel))
(car (read-16ube channel state))
(not (equal (car (read-16ube channel state)) 'fail)))
(unsigned-byte-p 16 (car (read-16ube channel state)))))
(defthm read-16ube-integer
(implies (and (force (state-p1 state))
(force (open-input-channel-p1 channel :byte state))
(force (symbolp channel))
(car (read-16ube channel state))
(not (equal (car (read-16ube channel state)) 'fail)))
(integerp (car (read-16ube channel state)))))
(defthm read-16ube-range
(implies (and (force (state-p1 state))
(force (open-input-channel-p1 channel :byte state))
(force (symbolp channel))
(car (read-16ube channel state))
(not (equal (car (read-16ube channel state)) 'fail)))
(and (<= 0 (car (read-16ube channel state)))
(< (car (read-16ube channel state)) 65536)
(< (car (read-16ube channel state)) (expt 2 16))))
:rule-classes :linear
:hints(("Goal"
:in-theory (enable unsigned-byte-p)
:use (:instance read-16ube-unsigned-byte))))
(defthm read-16ube-state
(implies (and (force (state-p1 state))
(force (open-input-channel-p1 channel :byte state))
(force (symbolp channel)))
(state-p1 (mv-nth 1 (read-16ube channel state)))))
(defthm read-16ube-open-input-channel-p1
(implies (and (force (state-p1 state))
(force (open-input-channel-p1 channel :byte state))
(force (symbolp channel)))
(open-input-channel-p1 channel
:byte
(mv-nth 1 (read-16ube channel state)))))
(in-theory (disable read-16ube))
;; read-16ule
(defun read-16ule (channel state)
(declare (xargs :guard (and (state-p state)
(symbolp channel)
(open-input-channel-p channel :byte state))
:guard-hints(("Goal" :in-theory (enable combine16u)))))
(mv-let (x1 state) (read-byte$ channel state)
(mv-let (x2 state) (read-byte$ channel state)
(if (null x1)
(mv nil state)
(if (null x2)
(mv 'fail state)
(mv (mbe :logic (combine16u x2 x1)
:exec (logior
(the (unsigned-byte 16)
(ash (the (unsigned-byte 8) x2) 8))
(the (unsigned-byte 8)
x1)))
state))))))
(defthm read-16ule-unsigned-byte
(implies (and (force (state-p1 state))
(force (open-input-channel-p1 channel :byte state))
(force (symbolp channel))
(car (read-16ule channel state))
(not (equal (car (read-16ule channel state)) 'fail)))
(unsigned-byte-p 16 (car (read-16ule channel state)))))
(defthm read-16ule-integer
(implies (and (force (state-p1 state))
(force (open-input-channel-p1 channel :byte state))
(force (symbolp channel))
(car (read-16ule channel state))
(not (equal (car (read-16ule channel state)) 'fail)))
(integerp (car (read-16ule channel state)))))
(defthm read-16ule-range
(implies (and (force (state-p1 state))
(force (open-input-channel-p1 channel :byte state))
(force (symbolp channel))
(car (read-16ule channel state))
(not (equal (car (read-16ule channel state)) 'fail)))
(and (<= 0 (car (read-16ule channel state)))
(< (car (read-16ube channel state)) 65536)
(< (car (read-16ube channel state)) (expt 2 16))))
:rule-classes :linear
:hints(("Goal"
:in-theory (enable unsigned-byte-p)
:use (:instance read-16ule-unsigned-byte))))
(defthm read-16ule-state
(implies (and (force (state-p1 state))
(force (open-input-channel-p1 channel :byte state))
(force (symbolp channel)))
(state-p1 (mv-nth 1 (read-16ule channel state)))))
(defthm read-16ule-open-input-channel-p1
(implies (and (force (state-p1 state))
(force (open-input-channel-p1 channel :byte state))
(force (symbolp channel)))
(open-input-channel-p1 channel
:byte
(mv-nth 1 (read-16ule channel state)))))
(in-theory (disable read-16ule))
;; read-16sbe
(defun read-16sbe (channel state)
(declare (xargs :guard (and (state-p state)
(symbolp channel)
(open-input-channel-p channel :byte state))
:guard-hints(("Goal" :in-theory (enable combine16s sign-byte)))))
(mv-let (x1 state) (read-byte$ channel state)
(mv-let (x2 state) (read-byte$ channel state)
(if (null x1)
(mv nil state)
(if (null x2)
(mv 'fail state)
(mv (mbe :logic (combine16s x1 x2)
:exec (logior (the (signed-byte 16)
(ash (the (signed-byte 8)
(if (< (the-fixnum x1) 128)
(the-fixnum x1)
(the-fixnum
(- (the-fixnum x1) 256))))
8))
(the (unsigned-byte 8)
x2)))
state))))))
(defthm read-16sbe-signed-byte
(implies (and (force (state-p1 state))
(force (open-input-channel-p1 channel :byte state))
(force (symbolp channel))
(car (read-16sbe channel state))
(not (equal (car (read-16sbe channel state)) 'fail)))
(signed-byte-p 16 (car (read-16sbe channel state)))))
(defthm read-16sbe-integer
(implies (and (force (state-p1 state))
(force (open-input-channel-p1 channel :byte state))
(force (symbolp channel))
(car (read-16sbe channel state))
(not (equal (car (read-16sbe channel state)) 'fail)))
(integerp (car (read-16sbe channel state)))))
(defthm read-16sbe-range
(implies (and (force (state-p1 state))
(force (open-input-channel-p1 channel :byte state))
(force (symbolp channel))
(car (read-16sbe channel state))
(not (equal (car (read-16sbe channel state)) 'fail)))
(and (<= -32768 (car (read-16sbe channel state)))
(<= (- (expt 2 15)) (car (read-16sbe channel state)))
(< (car (read-16sbe channel state)) 32768)
(< (car (read-16sbe channel state)) (expt 2 15))))
:rule-classes :linear
:hints(("Goal"
:in-theory (enable signed-byte-p)
:use (:instance read-16sbe-signed-byte))))
(defthm read-16sbe-state
(implies (and (force (state-p1 state))
(force (open-input-channel-p1 channel :byte state))
(force (symbolp channel)))
(state-p1 (mv-nth 1 (read-16sbe channel state)))))
(defthm read-16sbe-open-input-channel-p1
(implies (and (force (state-p1 state))
(force (open-input-channel-p1 channel :byte state))
(force (symbolp channel)))
(open-input-channel-p1 channel
:byte
(mv-nth 1 (read-16sbe channel state)))))
(in-theory (disable read-16sbe))
;; read-16sle
(defun read-16sle (channel state)
(declare (xargs :guard (and (state-p state)
(symbolp channel)
(open-input-channel-p channel :byte state))
:guard-hints(("Goal" :in-theory (enable combine16s sign-byte)))))
(mv-let (x1 state) (read-byte$ channel state)
(mv-let (x2 state) (read-byte$ channel state)
(if (null x1)
(mv nil state)
(if (null x2)
(mv 'fail state)
(mv (mbe :logic (combine16s x2 x1)
:exec (logior (the (signed-byte 16)
(ash (the (signed-byte 8)
(if (< (the-fixnum x2) 128)
(the-fixnum x2)
(the-fixnum
(- (the-fixnum x2) 256))))
8))
(the (unsigned-byte 8)
x1)))
state))))))
(defthm read-16sle-signed-byte
(implies (and (force (state-p1 state))
(force (open-input-channel-p1 channel :byte state))
(force (symbolp channel))
(car (read-16sle channel state))
(not (equal (car (read-16sle channel state)) 'fail)))
(signed-byte-p 16 (car (read-16sle channel state)))))
(defthm read-16sle-integer
(implies (and (force (state-p1 state))
(force (open-input-channel-p1 channel :byte state))
(force (symbolp channel))
(car (read-16sle channel state))
(not (equal (car (read-16sle channel state)) 'fail)))
(integerp (car (read-16sle channel state)))))
(defthm read-16sle-range
(implies (and (force (state-p1 state))
(force (open-input-channel-p1 channel :byte state))
(force (symbolp channel))
(car (read-16sle channel state))
(not (equal (car (read-16sle channel state)) 'fail)))
(and (<= -32768 (car (read-16sle channel state)))
(<= (- (expt 2 15)) (car (read-16sle channel state)))
(< (car (read-16sle channel state)) 32768)
(< (car (read-16sle channel state)) (expt 2 15))))
:rule-classes :linear
:hints(("Goal"
:in-theory (enable signed-byte-p)
:use (:instance read-16sle-signed-byte))))
(defthm read-16sle-state
(implies (and (force (state-p1 state))
(force (open-input-channel-p1 channel :byte state))
(force (symbolp channel)))
(state-p1 (mv-nth 1 (read-16sle channel state)))))
(defthm read-16sle-open-input-channel-p1
(implies (and (force (state-p1 state))
(force (open-input-channel-p1 channel :byte state))
(force (symbolp channel)))
(open-input-channel-p1 channel
:byte
(mv-nth 1 (read-16sle channel state)))))
(in-theory (disable read-16sle))
;; read-32ube
(defun read-32ube (channel state)
(declare (xargs :guard (and (state-p state)
(symbolp channel)
(open-input-channel-p channel :byte state))
:guard-hints(("Goal" :in-theory (enable combine32u)))))
(mv-let (x1 state) (read-byte$ channel state)
(mv-let (x2 state) (read-byte$ channel state)
(mv-let (x3 state) (read-byte$ channel state)
(mv-let (x4 state) (read-byte$ channel state)
(if (null x1)
(mv nil state)
(if (or (null x2) (null x3) (null x4))
(mv 'fail state)
(mv (mbe :logic (combine32u x1 x2 x3 x4)
:exec (logior (the (unsigned-byte 32)
(ash (the (unsigned-byte 8) x1) 24))
(the (unsigned-byte 24)
(logior
(the (unsigned-byte 24)
(ash (the (unsigned-byte 8) x2) 16))
(the (unsigned-byte 16)
(logior
(the (unsigned-byte 16)
(ash (the (unsigned-byte 8) x3) 8))
(the (unsigned-byte 8)
x4)))))))
state))))))))
(defthm read-32ube-unsigned-byte
(implies (and (force (state-p1 state))
(force (open-input-channel-p1 channel :byte state))
(force (symbolp channel))
(car (read-32ube channel state))
(not (equal (car (read-32ube channel state)) 'fail)))
(unsigned-byte-p 32 (car (read-32ube channel state)))))
(defthm read-32ube-integer
(implies (and (force (state-p1 state))
(force (open-input-channel-p1 channel :byte state))
(force (symbolp channel))
(car (read-32ube channel state))
(not (equal (car (read-32ube channel state)) 'fail)))
(integerp (car (read-32ube channel state)))))
(defthm read-32ube-range
(implies (and (force (state-p1 state))
(force (open-input-channel-p1 channel :byte state))
(force (symbolp channel))
(car (read-32ube channel state))
(not (equal (car (read-32ube channel state)) 'fail)))
(and (<= 0 (car (read-32ube channel state)))
(< (car (read-32ube channel state)) 4294967296)
(< (car (read-32ube channel state)) (expt 2 32))))
:rule-classes :linear
:hints(("Goal"
:in-theory (enable unsigned-byte-p)
:use (:instance read-32ube-unsigned-byte))))
(defthm read-32ube-state
(implies (and (force (state-p1 state))
(force (open-input-channel-p1 channel :byte state))
(force (symbolp channel)))
(state-p1 (mv-nth 1 (read-32ube channel state)))))
(defthm read-32ube-open-input-channel-p1
(implies (and (force (state-p1 state))
(force (open-input-channel-p1 channel :byte state))
(force (symbolp channel)))
(open-input-channel-p1 channel
:byte
(mv-nth 1 (read-32ube channel state)))))
(in-theory (disable read-32ube))
;; read-32ule
(defun read-32ule (channel state)
(declare (xargs :guard (and (state-p state)
(symbolp channel)
(open-input-channel-p channel :byte state))
:guard-hints(("Goal" :in-theory (enable combine32u)))))
(mv-let (x1 state) (read-byte$ channel state)
(mv-let (x2 state) (read-byte$ channel state)
(mv-let (x3 state) (read-byte$ channel state)
(mv-let (x4 state) (read-byte$ channel state)
(if (null x1)
(mv nil state)
(if (or (null x2) (null x3) (null x4))
(mv 'fail state)
(mv (mbe :logic (combine32u x4 x3 x2 x1)
:exec (logior (the (unsigned-byte 32)
(ash (the (unsigned-byte 8) x4) 24))
(the (unsigned-byte 24)
(logior
(the (unsigned-byte 24)
(ash (the (unsigned-byte 8) x3) 16))
(the (unsigned-byte 16)
(logior
(the (unsigned-byte 16)
(ash (the (unsigned-byte 8) x2) 8))
(the (unsigned-byte 8)
x1)))))))
state))))))))
(defthm read-32ule-unsigned-byte
(implies (and (force (state-p1 state))
(force (open-input-channel-p1 channel :byte state))
(force (symbolp channel))
(car (read-32ule channel state))
(not (equal (car (read-32ule channel state)) 'fail)))
(unsigned-byte-p 32 (car (read-32ule channel state)))))
(defthm read-32ule-integer
(implies (and (force (state-p1 state))
(force (open-input-channel-p1 channel :byte state))
(force (symbolp channel))
(car (read-32ule channel state))
(not (equal (car (read-32ule channel state)) 'fail)))
(integerp (car (read-32ule channel state)))))
(defthm read-32ule-range
(implies (and (force (state-p1 state))
(force (open-input-channel-p1 channel :byte state))
(force (symbolp channel))
(car (read-32ule channel state))
(not (equal (car (read-32ule channel state)) 'fail)))
(and (<= 0 (car (read-32ule channel state)))
(< (car (read-32ule channel state)) 4294967296)
(< (car (read-32ule channel state)) (expt 2 32))))
:rule-classes :linear
:hints(("Goal"
:in-theory (enable unsigned-byte-p)
:use (:instance read-32ule-unsigned-byte))))
(defthm read-32ule-state
(implies (and (force (state-p1 state))
(force (open-input-channel-p1 channel :byte state))
(force (symbolp channel)))
(state-p1 (mv-nth 1 (read-32ule channel state)))))
(defthm read-32ule-open-input-channel-p1
(implies (and (force (state-p1 state))
(force (open-input-channel-p1 channel :byte state))
(force (symbolp channel)))
(open-input-channel-p1 channel
:byte
(mv-nth 1 (read-32ule channel state)))))
(in-theory (disable read-32ule))
;; read-32sbe
(defun read-32sbe (channel state)
(declare (xargs :guard (and (state-p state)
(symbolp channel)
(open-input-channel-p channel :byte state))
:guard-hints(("Goal" :in-theory (enable combine32s sign-byte)))))
(mv-let (x1 state) (read-byte$ channel state)
(mv-let (x2 state) (read-byte$ channel state)
(mv-let (x3 state) (read-byte$ channel state)
(mv-let (x4 state) (read-byte$ channel state)
(if (null x1)
(mv nil state)
(if (or (null x2) (null x3) (null x4))
(mv 'fail state)
(mv (mbe :logic (combine32s x1 x2 x3 x4)
:exec (logior (the (signed-byte 32)
(ash (the (signed-byte 8)
(if (< (the-fixnum x1) 128)
x1
(the-fixnum
(- (the-fixnum x1) 256))))
24))
(the (unsigned-byte 24)
(logior
(the (unsigned-byte 24)
(ash (the (unsigned-byte 8) x2) 16))
(the (unsigned-byte 16)
(logior
(the (unsigned-byte 16)
(ash (the (unsigned-byte 8) x3) 8))
(the (unsigned-byte 8)
x4)))))))
state))))))))
(defthm read-32sbe-signed-byte
(implies (and (force (state-p1 state))
(force (open-input-channel-p1 channel :byte state))
(force (symbolp channel))
(car (read-32sbe channel state))
(not (equal (car (read-32sbe channel state)) 'fail)))
(signed-byte-p 32 (car (read-32sbe channel state)))))
(defthm read-32sbe-integer
(implies (and (force (state-p1 state))
(force (open-input-channel-p1 channel :byte state))
(force (symbolp channel))
(car (read-32sbe channel state))
(not (equal (car (read-32sbe channel state)) 'fail)))
(integerp (car (read-32sbe channel state)))))
(defthm read-32sbe-range
(implies (and (force (state-p1 state))
(force (open-input-channel-p1 channel :byte state))
(force (symbolp channel))
(car (read-32sbe channel state))
(not (equal (car (read-32sbe channel state)) 'fail)))
(and (<= -2147483648 (car (read-32sbe channel state)))
(<= (- (expt 2 31)) (car (read-32sbe channel state)))
(< (car (read-32sbe channel state)) 2147483648)
(< (car (read-32sbe channel state)) (expt 2 31))))
:rule-classes :linear
:hints(("Goal"
:in-theory (enable signed-byte-p)
:use (:instance read-32sbe-signed-byte))))
(defthm read-32sbe-state
(implies (and (force (state-p1 state))
(force (open-input-channel-p1 channel :byte state))
(force (symbolp channel)))
(state-p1 (mv-nth 1 (read-32sbe channel state)))))
(defthm read-32sbe-open-input-channel-p1
(implies (and (force (state-p1 state))
(force (open-input-channel-p1 channel :byte state))
(force (symbolp channel)))
(open-input-channel-p1 channel
:byte
(mv-nth 1 (read-32sbe channel state)))))
(in-theory (disable read-32sbe))
;; read-32sle
(defun read-32sle (channel state)
(declare (xargs :guard (and (state-p state)
(symbolp channel)
(open-input-channel-p channel :byte state))
:guard-hints(("Goal" :in-theory (enable combine32s sign-byte)))))
(mv-let (x1 state) (read-byte$ channel state)
(mv-let (x2 state) (read-byte$ channel state)
(mv-let (x3 state) (read-byte$ channel state)
(mv-let (x4 state) (read-byte$ channel state)
(if (null x1)
(mv nil state)
(if (or (null x2) (null x3) (null x4))
(mv 'fail state)
(mv (mbe :logic (combine32s x4 x3 x2 x1)
:exec (logior (the (signed-byte 32)
(ash (the (signed-byte 8)
(if (< (the-fixnum x4) 128)
x4
(the-fixnum
(- (the-fixnum x4) 256))))
24))
(the (unsigned-byte 24)
(logior
(the (unsigned-byte 24)
(ash (the (unsigned-byte 8) x3) 16))
(the (unsigned-byte 16)
(logior
(the (unsigned-byte 16)
(ash (the (unsigned-byte 8) x2) 8))
(the (unsigned-byte 8)
x1)))))))
state))))))))
(defthm read-32sle-signed-byte
(implies (and (force (state-p1 state))
(force (open-input-channel-p1 channel :byte state))
(force (symbolp channel))
(car (read-32sle channel state))
(not (equal (car (read-32sle channel state)) 'fail)))
(signed-byte-p 32 (car (read-32sle channel state)))))
(defthm read-32sle-integer
(implies (and (force (state-p1 state))
(force (open-input-channel-p1 channel :byte state))
(force (symbolp channel))
(car (read-32sle channel state))
(not (equal (car (read-32sle channel state)) 'fail)))
(integerp (car (read-32sle channel state)))))
(defthm read-32sle-range
(implies (and (force (state-p1 state))
(force (open-input-channel-p1 channel :byte state))
(force (symbolp channel))
(car (read-32sle channel state))
(not (equal (car (read-32sle channel state)) 'fail)))
(and (<= -2147483648 (car (read-32sle channel state)))
(<= (- (expt 2 31)) (car (read-32sle channel state)))
(< (car (read-32sle channel state)) 2147483648)
(< (car (read-32sle channel state)) (expt 2 31))))
:rule-classes :linear
:hints(("Goal"
:in-theory (enable signed-byte-p)
:use (:instance read-32sle-signed-byte))))
(defthm read-32sle-state
(implies (and (force (state-p1 state))
(force (open-input-channel-p1 channel :byte state))
(force (symbolp channel)))
(state-p1 (mv-nth 1 (read-32sle channel state)))))
(defthm read-32sle-open-input-channel-p1
(implies (and (force (state-p1 state))
(force (open-input-channel-p1 channel :byte state))
(force (symbolp channel)))
(open-input-channel-p1 channel
:byte
(mv-nth 1 (read-32sle channel state)))))
(in-theory (disable read-32sle))
;; read-bytes$ macro
(defmacro read-bytes$ (channel &key (bytes '1)
(signed 'nil)
(end 'big))
(declare (xargs :guard (and (symbolp channel)
(booleanp signed)
(or (equal bytes 1)
(equal bytes 2)
(equal bytes 4))
(or (equal end 'little)
(equal end 'big)))))
(case end
('big (if signed
(case bytes
(1 `(read-8s ,channel state))
(2 `(read-16sbe ,channel state))
(4 `(read-32sbe ,channel state))
(t (er hard 'read-bytes$ "Bad case in read-bytes$.")))
(case bytes
(1 `(read-byte$ ,channel state))
(2 `(read-16ube ,channel state))
(4 `(read-32ube ,channel state))
(t (er hard 'read-bytes$ "Bad case in read-bytes$.")))))
('little (if signed
(case bytes
(1 `(read-8s ,channel state))
(2 `(read-16sle ,channel state))
(4 `(read-32sle ,channel state))
(t (er hard 'read-bytes$ "Bad case in read-bytes$.")))
(case bytes
(1 `(read-byte$ ,channel state))
(2 `(read-16ule ,channel state))
(4 `(read-32ule ,channel state))
(t (er hard 'read-bytes$ "Bad case in read-bytes$.")))))
(t (er hard 'read-bytes$ "Bad case in read-bytes$."))))
;; reading sequences of data
(defmacro create-n-reader (read-1 fails?)
(declare (xargs :guard (and (symbolp read-1)
(booleanp fails?))))
(let ((tr-read-n (intern-in-package-of-symbol
(concatenate 'string "TR-" (symbol-name read-1) "-N")
read-1))
(read-n (intern-in-package-of-symbol
(concatenate 'string (symbol-name read-1) "-N")
read-1))
(read-n-state (intern-in-package-of-symbol
(concatenate 'string (symbol-name read-1) "-N-STATE")
read-1))
(read-n-open-input-channel (intern-in-package-of-symbol
(concatenate 'string (symbol-name read-1)
"-N-OPEN-INPUT-CHANNEL")
read-1))
(read-n-data (intern-in-package-of-symbol
(concatenate 'string (symbol-name read-1) "-N-DATA")
read-1)))
`(encapsulate
nil
(defun ,tr-read-n (n channel state acc)
(declare (xargs :guard (and (natp n)
(state-p state)
(symbolp channel)
(open-input-channel-p channel :byte state)
(true-listp acc))))
(if (mbe :logic (zp n) :exec (= 0 n))
(mv (reverse acc) state)
(mv-let (byte state)
(,read-1 channel state)
(cond ((eq byte nil)
(mv 'fail state))
,@(if fails?
`(((eq byte 'fail)
(mv 'fail state)))
nil)
(t (,tr-read-n (1- n) channel state
(cons byte acc)))))))
(defun ,read-n (n channel state)
(declare (xargs :guard (and (natp n)
(state-p state)
(symbolp channel)
(open-input-channel-p channel :byte state))
:verify-guards nil))
(mbe :logic (if (zp n)
(mv nil state)
(mv-let (byte state)
(,read-1 channel state)
(cond ((eq byte nil)
(mv 'fail state))
,@(if fails?
`(((eq byte 'fail)
(mv 'fail state)))
nil)
(t (mv-let (rest state)
(,read-n (1- n) channel state)
(mv (if (eq rest 'fail)
'fail
(cons byte rest))
state))))))
:exec (,tr-read-n n channel state nil)))
(local (defthm lemma-decompose-impl
(equal (,tr-read-n n channel state acc)
(list (car (,tr-read-n n channel state acc))
(mv-nth 1 (,tr-read-n n channel state acc))))
:rule-classes nil))
(local (defthm lemma-decompose-spec
(equal (,read-n n channel state)
(list (car (,read-n n channel state))
(mv-nth 1 (,read-n n channel state))))
:rule-classes nil))
(local (defthm lemma-data-equiv-1
(implies (equal (car (,read-n n channel state)) 'fail)
(equal (car (,tr-read-n n channel state acc)) 'fail))))
(local (defthm lemma-data-equiv-2
(implies (and (true-listp acc)
(not (equal (car (,read-n n channel state)) 'fail)))
(equal (car (,tr-read-n n channel state acc))
(revappend acc (car (,read-n n channel state)))))))
(local (defthm lemma-state-equiv
(equal (mv-nth 1 (,tr-read-n n channel state acc))
(mv-nth 1 (,read-n n channel state)))))
(local (defthm lemma-equiv
(equal (,tr-read-n n channel state nil)
(,read-n n channel state))
:hints(("Goal" :in-theory (disable ,tr-read-n ,read-n)
:use ((:instance lemma-decompose-impl (acc nil))
(:instance lemma-decompose-spec)
(:instance lemma-data-equiv-2 (acc nil)))))))
(verify-guards ,read-n)
(defthm ,read-n-state
(implies (and (force (state-p1 state))
(force (symbolp channel))
(force (open-input-channel-p1 channel :byte state)))
(state-p1 (mv-nth 1 (,read-n n channel state)))))
(defthm ,read-n-open-input-channel
(implies (and (force (state-p1 state))
(force (symbolp channel))
(force (open-input-channel-p1 channel :byte state)))
(open-input-channel-p1 channel
:byte
(mv-nth 1 (,read-n n channel state)))))
(defthm ,read-n-data
(implies (not (equal (car (,read-n n channel state)) 'fail))
(and (true-listp (car (,read-n n channel state)))
(equal (len (car (,read-n n channel state)))
(nfix n)))))
)))
(create-n-reader read-byte$ nil)
(create-n-reader read-8s nil)
(create-n-reader read-16ube t)
(create-n-reader read-16ule t)
(create-n-reader read-16sbe t)
(create-n-reader read-16sle t)
(create-n-reader read-32ube t)
(create-n-reader read-32ule t)
(create-n-reader read-32sbe t)
(create-n-reader read-32sle t)
(defmacro read-bytes$-n (n channel &key (bytes '1) (signed 'nil) (end 'big))
(declare (xargs :guard (and (symbolp channel)
(booleanp signed)
(or (equal bytes 1)
(equal bytes 2)
(equal bytes 4))
(or (equal end 'little)
(equal end 'big)))))
(case end
('big (if signed
(case bytes
(1 `(read-8s-n ,n ,channel state))
(2 `(read-16sbe-n ,n ,channel state))
(4 `(read-32sbe-n ,n ,channel state))
(t (er hard 'read-bytes$-n "Bad case in read-bytes$-n.")))
(case bytes
(1 `(read-byte$-n ,n ,channel state))
(2 `(read-16ube-n ,n ,channel state))
(4 `(read-32ube-n ,n ,channel state))
(t (er hard 'read-bytes$-n "Bad case in read-bytes$-n.")))))
('little (if signed
(case bytes
(1 `(read-8s-n ,n ,channel state))
(2 `(read-16sle-n ,n ,channel state))
(4 `(read-32sle-n ,n ,channel state))
(t (er hard 'read-bytes$-n "Bad case in read-bytes$-n.")))
(case bytes
(1 `(read-byte$-n ,n ,channel state))
(2 `(read-16ule-n ,n ,channel state))
(4 `(read-32ule-n ,n ,channel state))
(t (er hard 'read-bytes$-n "Bad case in read-bytes$-n.")))))
(t (er hard 'read-bytes$-n "Bad case in read-bytes$-n."))))
|