;;; -*- Mode: LISP; Package: :cl-user; BASE: 10; Syntax: ANSI-Common-Lisp; -*- ;;; ;;; Touched: Wed Sep 13 07:29:15 2006 +0530 ;;; Time-stamp: <2007-04-30 06:44:05 madhu> ;;; Status: Experimental. DO NOT REDISTRIBUTE. ;;; Copyright (C) 2006 Madhu. All rights reserved. ;;; (defpackage "ABPLFK-YENCBIN" (:use "CL")) (in-package "ABPLFK-YENCBIN") (defconstant +element-type+ '(unsigned-byte 8)) (defconstant +yenc-decoding-vector+ #(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 0 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) "(make-array 256 :element-type +element-type+ :initial-contents (loop for c below 256 collect (mod (- c 42) 256)))") (defconstant +default-initial-buffer-size+ 1024) (defconstant Newline 10 "(char-code #\Newline)") (defconstant Return 13 "(char-code #\Return)") (defconstant Equal 61 "(char-code #\=)") (defconstant Tab 09 "(char-code #\Tab)") (defconstant Space 32 "(char-code #\Space)") (defconstant +yenc-begin-magic+ #(61 121 98 101 103 105 110) "(map 'vector 'char-code \"=ybegin\")") (defconstant +yenc-part-magic+ #(61 121 112 97 114 116) "(map 'vector 'char-code \"=ypart\")") (defconstant +yenc-end-magic+ #(61 121 101 110 100) "(map 'vector 'char-code \"=yend\")") (defun yenc-decode-region (buf start end &optional (output (make-array +default-initial-buffer-size+ :element-type +element-type+ :fill-pointer 0 :adjustable t))) "Return a sequence of unsigned-bytes." (loop (unless (< start end) (return output)) (let ((char (aref buf start))) (cond ((or (= char Newline) (= char Return))) ((= char Equal) (setq char (aref buf (incf start))) (vector-push-extend (mod (- char 106) 256) output)) (t (vector-push-extend (aref +yenc-decoding-vector+ char) output)))) (incf start))) (defun yenc-parse-keyvalue (keystring line &key (start 0) end (terminators (list Newline Return Tab Space))) "Returns a string. Second return value (if any) indicates the position after the parse terminated." (let ((keylength (length keystring)) (key (map 'vector 'char-code keystring))) (assert (eql #\= (char keystring (1- keylength)))) (let ((pos (search key line :start2 start :end2 end))) (when pos (let* ((pbeg (+ pos keylength)) (pend (position-if (lambda (c) (find c terminators)) line :start pbeg :end end))) (map `string `code-char (if pend (values (subseq line pbeg pend) pend) (subseq line pbeg)))))))) (defun yenc-next-line (line &key (start 0) end) "Returns position of the start of the next line (if any)." (let ((pos (position-if (lambda (c) (or (= c Newline) (= c Return))) line :start start :end end))) (when pos (loop while (< (incf pos) (or end (length line))) unless (case (aref line pos) ((Newline Return) t)) return pos)))) ;;; ---------------------------------------------------------------------- ;;; JUNK AT EOF ;;; #+nil (defmacro safe-let* (bindings &body body) (loop for x in bindings for (var val) = (if (consp x) x (list x)) collect var into vars when val collect `(unless (setq ,var ,val) (format t "Error setting ~A" ',var) (return-from YENC-SAVE-PART nil)) into forms finally (return `(let ,vars ,@forms ,@body)))) ;; XXX SLIME BUG reported on 20050227 (defmacro with-pathname-fu ((pathname-var) &body body) "On encountering a FILE-ERROR arrange during execution of BODY arrange for a restart to interactively change the value of PATHNAME-VAR. Typically BODY is expected to be a WITH-OPEN-FILE." `(prog () loop (restart-case (return (multiple-value-prog1 (progn ,@body))) (delete-it () ; XXX :test (lambda (c) (typep c 'file-error)) :report (lambda (stream) (format stream "delete ~S" ,pathname-var)) (cond ((delete-file ,pathname-var) (warn "deleted ~S" ,pathname-var)) (t (warn "deleting ~S failed" ,pathname-var))) (go loop)) (choose-new-pathname () ; XXX :test (lambda (c) (typep c 'file-error)) :report (lambda (stream) (format stream "Use a different pathname.")) (format t "Enter a new pathname for #'READ: ") (let (*read-eval*) (setq ,pathname-var (read))) (go loop))))) (defvar $buffer ; XXX (user::ensure-buffer-with-capacity +default-initial-buffer-size+ :element-type '(unsigned-byte 8))) (defvar $decoded-buffer ; XXX (user::ensure-buffer-with-capacity +default-initial-buffer-size+ :element-type '(unsigned-byte 8))) (defun yenc-decoded-buffer () ; XXX (setf (fill-pointer $decoded-buffer) 0) $decoded-buffer) (defun yenc-slurp-file (pathname) ; XXX (user::slurp-file pathname $buffer)) (defun yenc-save-part (pathname output-directory) (macrolet ((safe-let* (bindings &body body) (loop for x in bindings for (var val) = (if (consp x) x (list x)) collect var into vars when val collect `(unless (setq ,var ,val) (warn "Error decoding yenc file: ~A" ',var) (return-from YENC-SAVE-PART nil)) into forms finally (return `(let ,vars ,@forms ,@body))))) (safe-let* ((file-buf (YENC-SLURP-FILE pathname)) (pos-beg (search +yenc-begin-magic+ file-buf)) (part-beg (or (search +yenc-part-magic+ file-buf :start2 pos-beg) pos-beg)) ; XXX (decoded-file-size (parse-integer (yenc-parse-keyvalue "size=" file-buf :start pos-beg :end ; XXX (if (/= part-beg pos-beg) part-beg)))) (decoded-file-name (yenc-parse-keyvalue "name=" file-buf :start pos-beg ;;;madhu 070422 :terminators (list Newline Return))) (decoded-region-begin (yenc-next-line file-buf :start part-beg)) (decoded-region-end (1- (search +yenc-end-magic+ file-buf :start2 decoded-region-begin))) (output-pathname (merge-pathnames decoded-file-name output-directory nil)) (decoded-array (yenc-decode-region file-buf decoded-region-begin decoded-region-end (YENC-DECODED-BUFFER)))) (unless (= decoded-file-size (length decoded-array)) (warn "~A: Mismatch in file size: expected ~D Got ~D." (file-namestring pathname) decoded-file-size (length decoded-array)) (return-from yenc-save-part nil)) (when (probe-file output-pathname) (warn "~A: FILE EXISTS: ~A" (file-namestring pathname) output-pathname)) (with-pathname-fu (output-pathname) ; XXX (with-open-file (output-stream output-pathname :element-type +element-type+ ;XXX :if-exists :supersede :direction :output) (write-sequence decoded-array output-stream) decoded-file-size)))))