;;; eexec.el --- hex and eexec encoder / decoder ;; Copyright (C) 1999 Roger Willcocks ;; rogerw@centipede.co.uk ;; ;; 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. ;;; Commentary: ;; commands to turn a region from binary to hex and vice-versa ;; commands to eexec encode and decode a region ;;; Code: (defun eexec-region () "ascii to PS" (interactive) (let ((from (region-beginning)) (to (region-end)) (i 0) (j 0) (k 0) (ival 0) (R 55665) str len cval) (setq str (buffer-substring from to)) (setq len (length str)) (goto-char from) (kill-region from to) (while (< i len) (setq cval (aref str i)) (setq i (1+ i)) (progn (setq ival (logxor cval (/ R 256))) (setq R (logand (+ 22719 (* 52845 (+ ival R))) 65535)) (insert (aref "0123456789abcdef" (logand 15 (/ ival 16)))) (insert (aref "0123456789abcdef" (logand 15 ival))) (setq k (1+ k)) ) (if (eq 0 (% k 30)) (insert 10) ) ) ) ) (defun uneexec-region () "PS to ascii" (interactive) (let ((from (region-beginning)) (to (region-end)) (i 0) (j 0) (R 55665) str len cval ival) (setq str (buffer-substring from to)) (setq len (length str)) (setq ival 0) (goto-char from) (kill-region from to) (while (< i len) (setq cval (aref str i)) (if (and (>= cval ?0) (<= cval ?9)) (progn (setq ival (+ (* 16 ival) (- cval ?0))) (setq j (1+ j)))) (if (and (>= cval ?a) (<= cval ?f)) (progn (setq ival (+ (* 16 ival) (+ 10 (- cval ?a)))) (setq j (1+ j)))) (if (and (>= cval ?A) (<= cval ?F)) (progn (setq ival (+ (* 16 ival) (+ 10 (- cval ?A)))) (setq j (1+ j)))) (setq i (1+ i)) (if (eq j 2) (progn (insert (logand 255 (logxor ival (/ R 256)))) (setq R (logand (+ 22719 (* 52845 (+ ival R))) 65535)) (setq ival 0) (setq j 0) ) ) ) ) ) (defun beexec-region () "bin to PS" (interactive) (let ((from (region-beginning)) (to (region-end)) (i 0) (j 0) (R 55665) str len cval ival) (setq str (buffer-substring from to)) (setq len (length str)) (setq ival 0) (goto-char from) (kill-region from to) (while (< i len) (setq cval (aref str i)) (setq i (1+ i)) (setq ival (logxor cval (/ R 256))) (setq R (logand (+ 22719 (* 52845 (+ ival R))) 65535)) (insert (logand 255 ival)) ) ) ) (defun buneexec-region () "PS to bin" (interactive) (let ((from (region-beginning)) (to (region-end)) (i 0) (j 0) (R 55665) str len cval ival) (setq str (buffer-substring from to)) (setq len (length str)) (setq ival 0) (goto-char from) (kill-region from to) (while (< i len) (setq ival (aref str i)) (setq i (1+ i)) (insert (logand 255 (logxor ival (/ R 256)))) (setq R (logand (+ 22719 (* 52845 (+ ival R))) 65535)) ) ) ) (defun hex-region () "char to hex" (interactive) (let ((from (region-beginning)) (to (region-end)) (i 0) (j 0) (k 0) str len cval ival) (setq str (buffer-substring from to)) (setq len (length str)) (goto-char from) (kill-region from to) (while (< i len) (setq cval (aref str i)) (setq i (1+ i)) (progn (insert (aref "0123456789abcdef" (logand 15 (/ cval 16)))) (insert (aref "0123456789abcdef" (logand 15 cval))) (setq k (1+ k)) ) (if (eq 0 (% k 30)) (insert 10) ) ) ) ) (defun unhex-region () "hex to char" (interactive) (let ((from (region-beginning)) (to (region-end)) (i 0) (j 0) (ival 0) str len cval) (setq str (buffer-substring from to)) (setq len (length str)) (goto-char from) (kill-region from to) (while (< i len) (setq cval (aref str i)) (if (and (>= cval ?0) (<= cval ?9)) (progn (setq ival (+ (* 16 ival) (- cval ?0))) (setq j (1+ j)))) (if (and (>= cval ?a) (<= cval ?f)) (progn (setq ival (+ (* 16 ival) (+ 10 (- cval ?a)))) (setq j (1+ j)))) (if (and (>= cval ?A) (<= cval ?F)) (progn (setq ival (+ (* 16 ival) (+ 10 (- cval ?A)))) (setq j (1+ j)))) (setq i (1+ i)) (if (eq j 2) (progn (insert ival) (setq ival 0) (setq j 0) ) ) ) ) ) ;;; eexec.el ends here