diff options
author | Bryan Newbold <bnewbold@robocracy.org> | 2017-02-20 00:05:28 -0800 |
---|---|---|
committer | Bryan Newbold <bnewbold@robocracy.org> | 2017-02-20 00:05:28 -0800 |
commit | 87b82b5822ca54228cfa6df29be3ad9d4bc47d16 (patch) | |
tree | 1eb4f87abd38bea56e08335d939e8171d5e7bfc7 /makcrc.scm | |
parent | bd9733926076885e3417b74de76e4c9c7bc56254 (diff) | |
download | slib-87b82b5822ca54228cfa6df29be3ad9d4bc47d16.tar.gz slib-87b82b5822ca54228cfa6df29be3ad9d4bc47d16.zip |
Import Upstream version 2d2upstream/2d2
Diffstat (limited to 'makcrc.scm')
-rw-r--r-- | makcrc.scm | 49 |
1 files changed, 29 insertions, 20 deletions
@@ -1,9 +1,9 @@ ;;;; "makcrc.scm" Compute Cyclic Checksums -;;; Copyright (C) 1995, 1996, 1997 Aubrey Jaffer. +;;; Copyright (C) 1995, 1996, 1997, 2001 Aubrey Jaffer ; -;Permission to copy this software, to redistribute it, and to use it -;for any purpose is granted, subject to the following restrictions and -;understandings. +;Permission to copy this software, to modify it, to redistribute it, +;to distribute modified versions, and to use it for any purpose is +;granted, subject to the following restrictions and understandings. ; ;1. Any copy made of this software must include this copyright notice ;in full. @@ -17,27 +17,39 @@ ;promotional, or sales literature without prior written consent in ;each case. +(require 'byte) (require 'logical) -;;;(define crc (eval (make-port-crc 16 #o010013))) -;;;(define crc (eval (make-port-crc 08 #o053))) -;;;(define (file-check-sum file) (call-with-input-file file crc32)) - (define (make-port-crc . margs) (define (make-mask hibit) (+ (ash (+ -1 (ash 1 (+ 1 (- hibit 2)))) 1) 1)) - (define accum-bits 32) (define chunk-bits (integer-length (+ -1 char-code-limit))) + (define accum-bits #f) (define generator #f) - (cond ((pair? margs) - (set! accum-bits (car margs)) - (cond ((pair? (cdr margs)) - (set! generator (cadr margs)))))) + (case (length margs) + ((0) #t) + ((1) (if (< (car margs) 128) + (set! accum-bits (car margs)) + (set! generator (car margs)))) + ((2) + (set! accum-bits (car margs)) + (set! generator (cadr margs))) + (else (slib:error 'make-port-crc 'args margs))) (cond ((not generator) (case accum-bits - ((32) (set! generator #b00000100110000010001110110110111)) + ((#f 32) (set! accum-bits 32) + (set! generator #b00000100110000010001110110110111)) ; CRC-32 + ((16) (set! generator #b0001000000001011)) ; CRC-16 + ;;((16) (set! generator #b0001000000100001)) ; CRC-CCIT + ;;((08) (set! generator #b101011)) (else (slib:error 'make-port-crc "no default polynomial for" - accum-bits "bits"))))) + accum-bits "bits")))) + ((not accum-bits) + (set! accum-bits (+ -1 (integer-length generator))))) + (set! generator (logand generator (lognot (ash 1 accum-bits)))) + (cond ((>= (integer-length generator) accum-bits) + (slib:error 'make-port-crc + "generator longer than" accum-bits "bits"))) (let* ((chunk-mask (make-mask chunk-bits)) (crctab (make-vector (+ 1 chunk-mask)))) (define (accum src) @@ -69,17 +81,14 @@ (do ((i 0 (+ 1 i))) ((> i chunk-mask)) (vector-set! crctab i (remd i))))) - (cond ((>= (integer-length generator) accum-bits) - (slib:error 'make-port-crc - "generator longer than" accum-bits "bits"))) (make-crc-table) `(lambda (port) (define crc 0) (define byte-count 0) (define crctab ',crctab) - (do ((ci (read-char port) (read-char port))) + (do ((ci (read-byte port) (read-byte port))) ((eof-object? ci)) - ,(accum '(char->integer ci)) + ,(accum 'ci) (set! byte-count (+ 1 byte-count))) (do ((byte-count byte-count (ash byte-count ,(- chunk-bits)))) ((zero? byte-count)) |