home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / gnu / emacs / bug / 1213 < prev    next >
Encoding:
Text File  |  1992-08-25  |  19.8 KB  |  503 lines

  1. Newsgroups: gnu.emacs.bug
  2. Path: sparky!uunet!convex!darwin.sura.net!zaphod.mps.ohio-state.edu!cis.ohio-state.edu!dadhb1.ti.COM!adam
  3. From: adam@dadhb1.ti.COM (Adam Hudd)
  4. Subject: Re: emacs should be able to load compressed files,
  5. Message-ID: <ADAM.92Aug25090402@node_4914a.dadhb1.ti.com>
  6. Sender: gnulists@ai.mit.edu
  7. Organization: Texas Instruments, Inc., Houston, TX
  8. References: adam@dadhb1.ti.COM (Adam Hudd)
  9. Distribution: gnu
  10. Date: Tue, 25 Aug 1992 14:04:02 GMT
  11. Approved: bug-gnu-emacs@prep.ai.mit.edu
  12. Lines: 489
  13.  
  14.  
  15.   Paul> uncompressing them on the fly.  Being able to autoload such files
  16.   Paul> would make my .elc library collection quite a bit smaller.  Maybe it
  17.   Paul> could notice a .Z or .elz extension.
  18.  
  19.   Paul> Better implement this before someone patents it!  :-)
  20.  
  21. ;;; Compaction, compression and encryption for GNU Emacs
  22. ;;; Copyright (C) 1988, 1989, 1990 Kyle E. Jones
  23. ;;;
  24. ;;; This program is free software; you can redistribute it and/or modify
  25. ;;; it under the terms of the GNU General Public License as published by
  26. ;;; the Free Software Foundation; either version 1, or (at your option)
  27. ;;; any later version.
  28. ;;;
  29. ;;; This program is distributed in the hope that it will be useful,
  30. ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  31. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  32. ;;; GNU General Public License for more details.
  33. ;;;
  34. ;;; A copy of the GNU General Public License can be obtained from this
  35. ;;; program's author (send electronic mail to kyle@cs.odu.edu) or from
  36. ;;; the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA
  37. ;;; 02139, USA.
  38. ;;;
  39. ;;; Send bug reports to kyle@cs.odu.edu.
  40.  
  41. ;; To use this package, put it in a file called "crypt.el" in a Lisp
  42. ;; directory that Emacs knows about, byte-compile it, and put the line:
  43. ;;    (require 'crypt)
  44. ;; in your .emacs file or in the file default.el in the "lisp" directory
  45. ;; of the Emacs distribution.  Don't bother trying to autoload this file;
  46. ;; this package uses a find-file hook and thus should be loaded the
  47. ;; first time you visit any sort of file.
  48. ;;
  49. ;; The basic purpose of this package of Lisp functions is to automatically
  50. ;; recognize encrypted, compacted or compressed files when they are first
  51. ;; visited and decode the file's BUFFER before it is presented to the user.
  52. ;; The file itself is unchanged.  When the buffer is subsequently saved to
  53. ;; disk, a hook function re-encodes the buffer before the actual disk write
  54. ;; takes place.
  55. ;;
  56. ;; This package recognizes compacted and compressed files by a magic number at
  57. ;; the beginning of these files, but a heuristic is used to detect encrypted
  58. ;; files.  If you are asked for an encryption key for a file that is in fact
  59. ;; not encrypted, just hit RET and the file will be accepted as is, and the
  60. ;; crypt minor mode will not be entered.
  61.  
  62. (provide 'crypt)
  63.  
  64. (defvar auto-decode-buffer t
  65.   "*Non-nil value means that the buffers associated with encoded files will
  66. be decoded automatically, without requesting confirmation from the user.
  67. Nil means to ask before doing the decoding.")
  68.  
  69. (defvar buffer-save-encrypted nil
  70.   "*Non-nil means that when this buffer is saved it will be written out
  71. encrypted, as with the UNIX crypt(1) command.  Automatically local to all
  72. buffers.")
  73. (make-variable-buffer-local 'buffer-save-encrypted)
  74.  
  75. (defvar buffer-save-compacted nil
  76.   "*Non-nil means that when this buffer is saved it will be written out
  77. compacted, as with the UNIX compact(1) command.  Automatically local to all
  78. buffers.")
  79. (make-variable-buffer-local 'buffer-save-compacted)
  80.  
  81. (defvar buffer-save-compressed nil
  82.   "*Non-nil means that when this buffer is saved it will be written out
  83. compressed, as with the UNIX compress(1) command.  Automatically local to all
  84. buffers.")
  85. (make-variable-buffer-local 'buffer-save-compressed)
  86.  
  87. (defvar buffer-encryption-key nil
  88.   "*Key to use when encrypting the current buffer, prior to saving it.
  89. Automatically local to all buffers.")
  90. (make-variable-buffer-local 'buffer-encryption-key)
  91.  
  92. (defconst compact-magic-regexp "\377\037"
  93.   "Regexp that matches the magic number at the beginning of files created
  94. by the compact(1) command.")
  95.  
  96. (defconst compress-magic-regexp "\037\235\220"
  97.   "Regexp that matches the magic number at the beginning of files created
  98. by the compress(1) command.")
  99.  
  100. ;; Encrypted files have no magic number, so we have to hack a way of
  101. ;; determining which new buffers start in crypt mode.  The current setup is
  102. ;; that we use only buffers that have a non-ASCII character very close to
  103. ;; beginning of buffer and that do NOT match crypt-magic-regexp-inverse.
  104. ;; Currently crypt-magic-regexp-inverse will match Sun OS, 4.x BSD, and
  105. ;; Ultrix executable magic numbers, so binaries can still be edited (heh)
  106. ;; without headaches.
  107.  
  108. (defconst crypt-magic-regexp-inverse
  109.   "\\(..\\)?\\([\007\010\013]\001\\|\001[\007\010\013]\\)"
  110.   "Regexp that must NOT match the beginning of an encrypted buffer.")
  111.  
  112. (defmacro save-point (&rest body)
  113.   "Save value of point, evalutes FORMS and restore value of point.
  114. If the saved value of point is no longer valid go to (point-max).
  115. This macro exists because, save-excursion loses track of point during
  116. some types of deletions."
  117.   (let ((var (make-symbol "saved-point")))
  118.     (list 'let (list (list var '(point)))
  119.       (list 'unwind-protect
  120.         (cons 'progn body)
  121.         (list 'goto-char var)))))
  122.  
  123.  
  124. (defun find-crypt-file-hook ()
  125.   (save-point
  126.     (save-restriction
  127.       (widen)
  128.       (goto-char (point-min))
  129.       (let ((buffer-file-name buffer-file-name)
  130.         (old-buffer-file-name buffer-file-name)
  131.         (old-buffer-modified-p (buffer-modified-p))
  132.         encrypted compressed compacted case-fold-search buffer-read-only)
  133.     ;; We can reasonably assume that either compaction or compression will
  134.     ;; be used, or neither, but not both.
  135.     (cond ((and (looking-at compact-magic-regexp)
  136.             (or auto-decode-buffer
  137.             (y-or-n-p (format "Uncompact buffer %s? "
  138.                       (buffer-name)))))
  139.            (message "Uncompacting %s..." (buffer-name))
  140.            (compact-buffer (current-buffer) t)
  141.            ;; We can't actually go into compact mode yet because the major
  142.            ;; mode may change later on and blow away all local variables
  143.            ;; (and thus the minor modes).  So we make a note to go into
  144.            ;; compact mode later.
  145.            (setq compacted t)
  146.            ;; here we strip the compacted file's .C extension so that later
  147.            ;; we can set the buffer's major mode based on this modified
  148.            ;; name instead of the name with the .C extension.
  149.            (if (string-match "\\(\\.C\\)$" buffer-file-name)
  150.            (setq buffer-file-name
  151.              (substring buffer-file-name 0 (match-beginning 1))))
  152.            (if (not (input-pending-p))
  153.            (message "Uncompacting %s... done" (buffer-name))))
  154.           ((and (looking-at compress-magic-regexp)
  155.             (or auto-decode-buffer
  156.             (y-or-n-p (format "Uncompress buffer %s? "
  157.                       (buffer-name)))))
  158.            (message "Uncompressing %s..." (buffer-name))
  159.            (compress-buffer (current-buffer) t)
  160.            (setq compressed t)
  161.            (if (string-match "\\(\\.Z\\)$" buffer-file-name)
  162.            (setq buffer-file-name
  163.              (substring buffer-file-name 0 (match-beginning 1))))
  164.            (if (not (input-pending-p))
  165.            (message "Uncompressing %s... done" (buffer-name)))))
  166.     ;; Now peek at the file and see if it still looks like a binary file.
  167.     ;; If so, try the crypt-magic-regexp-inverse against it and if it FAILS
  168.     ;; we assume that this is an encrypted buffer.
  169.     (cond ((and (not (eobp))
  170.             (re-search-forward "[\200-\377]" (min (point-max) 15) t)
  171.             (goto-char (point-min))
  172.             (not (looking-at crypt-magic-regexp-inverse)))
  173.            (if (not buffer-encryption-key)
  174.            (call-interactively 'set-encryption-key))
  175.            ;; if user did not enter a key, turn off crypt mode.
  176.            ;; good for binaries that crypt-magic-regexp-inverse
  177.            ;; doesn't recognize.
  178.            ;; -- thanx to Paul Dworkin (paul@media-lab.media.mit.edu)
  179.            (if (equal buffer-encryption-key "")
  180.            (message "No key given, buffer %s assumed normal."
  181.                 (buffer-name))
  182.          (message "Decrypting %s..." (buffer-name))
  183.          (crypt-buffer buffer-encryption-key)
  184.          ;; Tuck the key away for safe keeping since setting the major
  185.          ;; mode may well blow it away.
  186.          (setq encrypted buffer-encryption-key)
  187.          (if (not (input-pending-p))
  188.              (message "Decrypting %s... done" (buffer-name))))))
  189.     ;; OK, if any changes have been made to the buffer we need to rerun the
  190.     ;; code the does automatic selection of major mode.
  191.     (cond ((or compressed compacted encrypted)
  192.            (set-auto-mode)
  193.            (hack-local-variables)
  194.            ;; Now set our minor modes.
  195.            (if compressed (compress-mode 1))
  196.            (if compacted (compact-mode 1))
  197.            (if encrypted
  198.            (progn (crypt-mode 1)
  199.               (setq buffer-encryption-key encrypted)))
  200.            ;; Restore buffer file name now, so that lock file entry is
  201.            ;; removed properly.
  202.            (setq buffer-file-name old-buffer-file-name)
  203.            ;; Restore buffer modified flag to its previous value.
  204.            ;; This will also remove the lock file entry for the buffer
  205.            ;; if the previous value was nil; this is why buffer-file-name
  206.            ;; had to be manually restored above.
  207.            (set-buffer-modified-p old-buffer-modified-p)))))))
  208.  
  209. ;; This function should be called ONLY as a write-file hook.
  210. ;; Odd things will happen if it is called elsewhere.
  211. (defun write-crypt-file-hook ()
  212.   (cond
  213.    ((or buffer-save-encrypted buffer-save-compacted buffer-save-compressed)
  214.     (save-excursion
  215.       (save-restriction
  216.     (let ((copy-buffer (get-buffer-create " *crypt copy buffer*"))
  217.           (selective-display selective-display)
  218.           (buffer-read-only))
  219.       (copy-to-buffer copy-buffer 1 (1+ (buffer-size)))
  220.       (narrow-to-region (point) (point))
  221.       (unwind-protect
  222.           (progn
  223.         (insert-buffer-substring copy-buffer)
  224.         (kill-buffer copy-buffer)
  225.         ;; selective-display non-nil means we must convert carriage
  226.         ;; returns to newlines now, and set selective-display
  227.         ;; temporarily to nil.
  228.         (cond (selective-display
  229.                (goto-char (point-min))
  230.                (subst-char-in-region (point-min) (point-max) ?\r ?\n)
  231.                (setq selective-display nil)))
  232.         (cond
  233.          (buffer-save-encrypted
  234.           (if (null buffer-encryption-key)
  235.               (error "No encryption key set for buffer %s"
  236.                  (buffer-name)))
  237.           (if (not (stringp buffer-encryption-key))
  238.               (error "Encryption key is not a string"))
  239.           (message "Encrypting %s..." (buffer-name))
  240.           (crypt-buffer buffer-encryption-key)))
  241.         (cond
  242.          ((and buffer-save-compacted buffer-save-compressed)
  243.           (error "Cannot compact and compress buffer %s"
  244.              (buffer-name)))
  245.          (buffer-save-compacted
  246.           (message "Compacting %s..." (buffer-name))
  247.           (compact-buffer))
  248.          (buffer-save-compressed
  249.           (message "Compressing %s..." (buffer-name))
  250.           (compress-buffer)))
  251.         (write-region (point-min) (point-max) buffer-file-name nil t)
  252.         (delete-region (point-min) (point-max))
  253.         (set-buffer-modified-p nil)
  254.         ;; return t so that basic-save-buffer will
  255.         ;; know that the save has already been done.
  256.         t )
  257.         ;; unwind...
  258.         ;; If the crypted stuff has already been removed
  259.         ;; then this is a no-op.
  260.         (delete-region (point-min) (point-max)))))))))
  261.  
  262. (defun crypt-region (start end key)
  263.   "Encrypt/decrypt the text in the region.
  264. >From a program, this function takes three args: START, END and KEY.
  265. When called interactively START and END default to point and mark
  266. \(START being the lesser of the two), KEY is prompted for."
  267.   (interactive
  268.    (progn
  269.      (barf-if-buffer-read-only)
  270.      (list
  271.       (region-beginning)
  272.       (region-end)
  273.       (read-string-no-echo "Crypt region using key: "))))
  274.   (save-point
  275.    (let ((opoint-max (point-max)))
  276.      (call-process-region start end shell-file-name t t nil "-c"
  277.               (concat "crypt \"" key "\""))
  278.      (if (not (= opoint-max (point-max)))
  279.      (error "crypt command failed!")))))
  280.  
  281. (defun crypt-buffer (key &optional buffer)
  282.   "Using KEY, encrypt/decrypt BUFFER.
  283. BUFFER defaults to the current buffer."
  284.   (interactive
  285.    (progn
  286.      (barf-if-buffer-read-only)
  287.      (list (read-string-no-echo "Crypt buffer using key: "))))
  288.   (or buffer (setq buffer (current-buffer)))
  289.   (save-excursion
  290.     (set-buffer buffer)
  291.     (crypt-region (point-min) (point-max) key)))
  292.  
  293. (defun compact-region (start end &optional undo)
  294.   "Compact the text in the region.
  295. >From a program, this function takes three args: START, END and UNDO.
  296. When called interactively START and END default to point and mark
  297. \(START being the lesser of the two).
  298. Prefix arg (or optional second arg non-nil) UNDO means uncompact."
  299.   (interactive "*r\nP")
  300.   (save-point
  301.    (call-process-region start end shell-file-name t t nil "-c"
  302.             (if undo "uncompact" "compact"))
  303.    (cond ((not undo)
  304.       (goto-char start)
  305.       (let (case-fold-search)
  306.         (if (not (looking-at compact-magic-regexp))
  307.         (error "%s failed!" (if undo
  308.                     "Uncompaction"
  309.                       "Compaction"))))))))
  310.  
  311. (defun compact-buffer (&optional buffer undo)
  312.   "Compact BUFFER.
  313. BUFFER defaults to the current buffer.
  314. Prefix arg (or second arg non-nil from a program) UNDO means uncompact."
  315.   (interactive (list (current-buffer) current-prefix-arg))
  316.   (or buffer (setq buffer (current-buffer)))
  317.   (save-excursion
  318.     (set-buffer buffer)
  319.     (compact-region (point-min) (point-max) undo)))
  320.  
  321. (defun compress-region (start end &optional undo)
  322.   "Compress the text in the region.
  323. >From a program, this function takes three args: START, END and UNDO.
  324. When called interactively START and END default to point and mark
  325. \(START being the lesser of the two).
  326. Prefix arg (or optional second arg non-nil) UNDO means uncompress."
  327.   (interactive "*r\nP")
  328.   (save-point
  329.    (call-process-region start end shell-file-name t t nil "-c"
  330.             (if undo "compress -d" "compress"))
  331.    (cond ((not undo)
  332.       (goto-char start)
  333.       (let (case-fold-search)
  334.         (if (not (looking-at compress-magic-regexp))
  335.         (error "%s failed!" (if undo
  336.                     "Uncompression"
  337.                       "Compression"))))))))
  338.  
  339. (defun compress-buffer (&optional buffer undo)
  340.   "Compress BUFFER.
  341. BUFFER defaults to the current buffer.
  342. Prefix arg (or second arg non-nil from a program) UNDO means uncompress."
  343.   (interactive (list (current-buffer) current-prefix-arg))
  344.   (or buffer (setq buffer (current-buffer)))
  345.   (save-excursion
  346.     (set-buffer buffer)
  347.     (compress-region (point-min) (point-max) undo)))
  348.  
  349. (defun set-encryption-key (key &optional buffer)
  350.   "Set the encryption KEY for BUFFER.
  351. KEY should be a string.
  352. BUFFER should be a buffer or the name of one;
  353. it defaults to the current buffer.  If BUFFER is in crypt mode, then it is
  354. also marked as modified, since it needs to be saved with the new key."
  355.   (interactive
  356.    (progn
  357.      (barf-if-buffer-read-only)
  358.      (list
  359.       (read-string-no-echo
  360.        (format "Set encryption key for buffer %s: " (buffer-name))))))
  361.   (or buffer (setq buffer (current-buffer)))
  362.   (save-excursion
  363.     (set-buffer buffer)
  364.     (if (equal key buffer-encryption-key)
  365.     (message "Key is identical to original, no change.")
  366.       (setq buffer-encryption-key key)
  367.       ;; don't touch the modify flag unless we're in crypt-mode.
  368.       (if buffer-save-encrypted
  369.       (set-buffer-modified-p t)))))
  370.  
  371. (defun crypt-mode (&optional arg)
  372.   "Toggle crypt mode.
  373. With arg, turn crypt mode on iff arg is positive, otherwise turn it off.
  374. In crypt mode, buffers are automatically encrypted before being written.
  375. If crypt mode is toggled and a key has been set for the current buffer, then
  376. the current buffer is marked modified, since it needs to be rewritten
  377. with (or without) encryption.
  378.  
  379. Use \\[set-encryption-key] to set the encryption key for the current buffer.
  380.  
  381. Entering crypt mode causes auto-saving to be turned off in the current buffer,
  382. as there is no way (in Emacs Lisp) to force auto save files to be encrypted."
  383.   (interactive "P")
  384.   (let ((oldval buffer-save-encrypted))
  385.     (setq buffer-save-encrypted
  386.       (if arg (> arg 0) (not buffer-save-encrypted)))
  387.     (if buffer-save-encrypted
  388.     (auto-save-mode 0)
  389.       (auto-save-mode (if (and auto-save-default buffer-file-name) 1 0)))
  390.     (if buffer-encryption-key
  391.     (set-buffer-modified-p
  392.      (not (eq oldval buffer-save-encrypted))))))
  393.  
  394. (defun compact-mode (&optional arg)
  395.   "Toggle compact mode.
  396. With arg, turn compact mode on iff arg is positive, otherwise turn it off.
  397. In compact mode, buffers are automatically compacted before being written.
  398. If compact mode is toggled, the current buffer is marked modified, since
  399. it needs to be written with (or without) compaction.
  400.  
  401. Entering compact mode causes auto-saving to be turned off in the current
  402. buffer, as there is no way (in Emacs Lisp) to force auto save files to be
  403. compacted."
  404.   (interactive "P")
  405.   (let ((oldval buffer-save-compacted))
  406.     (setq buffer-save-compacted
  407.       (if arg (> arg 0) (not buffer-save-compacted)))
  408.     (if buffer-save-compacted
  409.     (auto-save-mode 0)
  410.       (auto-save-mode (if (and auto-save-default buffer-file-name) 1 0)))
  411.     (set-buffer-modified-p (not (eq oldval buffer-save-compacted)))))
  412.  
  413. (defun compress-mode (&optional arg)
  414.   "Toggle compress mode.
  415. With arg, turn compress mode on iff arg is positive, otherwise turn it off.
  416. In compress mode, buffers are automatically compressed before being written.
  417. If compress mode is toggled, the current buffer is marked modified, since
  418. it needs to be written with (or without) compression.
  419.  
  420. Entering compress mode causes auto-saving to be turned off in the current
  421. buffer, as there is no way (in Emacs Lisp) to force auto save files to be
  422. compressed."
  423.   (interactive "P")
  424.   (let ((oldval buffer-save-compressed))
  425.     (setq buffer-save-compressed
  426.       (if arg (> arg 0) (not buffer-save-compressed)))
  427.     (if buffer-save-compressed
  428.     (auto-save-mode 0)
  429.       (auto-save-mode (if (and auto-save-default buffer-file-name) 1 0)))
  430.     (set-buffer-modified-p (not (eq oldval buffer-save-compressed)))))
  431.  
  432. (defun read-string-no-echo (prompt &optional confirm)
  433.   "Read a string from the minibuffer, prompting with PROMPT.
  434. Optional second argument CONFIRM non-nil means that the user will be asked
  435.   to type the string a second time for confirmation and if there is a
  436.   mismatch, the process is repeated.
  437.  
  438. Line editing keys are:
  439.   C-h, DEL    rubout
  440.   C-u, C-x      line kill
  441.   C-q, C-v      literal next"
  442.   (catch 'return-value
  443.     (save-excursion
  444.       (let ((input-buffer (get-buffer-create " *password*"))
  445.         (cursor-in-echo-area t)
  446.         (echo-keystrokes 0)
  447.         char string help-form done kill-ring)
  448.     (set-buffer input-buffer)
  449.     (unwind-protect
  450.         (while t
  451.           (erase-buffer)
  452.           (message prompt)
  453.           (while (not (memq (setq char (read-char)) '(?\C-m ?\C-j)))
  454.         (if (setq form
  455.              (cdr
  456.               (assq char
  457.                 '((?\C-h . (delete-char -1))
  458.                   (?\C-? . (delete-char -1))
  459.                   (?\C-u . (delete-region 1 (point)))
  460.                   (?\C-x . (delete-region 1 (point)))
  461.                   (?\C-q . (quoted-insert 1))
  462.                   (?\C-v . (quoted-insert 1))))))
  463.             (condition-case error-data
  464.             (eval form)
  465.               (error t))
  466.           (insert char))
  467.         (message prompt))
  468.           (cond ((and confirm string)
  469.              (cond ((not (string= string (buffer-string)))
  470.                 (message
  471.                  (concat prompt "[Mismatch... try again.]"))
  472.                 (ding)
  473.                 (sit-for 2)
  474.                 (setq string nil))
  475.                (t (throw 'return-value string))))
  476.             (confirm
  477.              (setq string (buffer-string))
  478.              (message (concat prompt "[Retype to confirm...]"))
  479.              (sit-for 2))
  480.             (t (throw 'return-value (buffer-string)))))
  481.       (set-buffer-modified-p nil)
  482.       (kill-buffer input-buffer))))))
  483.  
  484. ;; Install the hooks, then add the mode indicators to
  485. ;; the minor mode alist.
  486. (cond
  487.  ((not (memq 'write-crypt-file-hook write-file-hooks))
  488.   ;; make this hook last on purpose
  489.   (setq write-file-hooks (append write-file-hooks
  490.                  (list 'write-crypt-file-hook))
  491.     find-file-hooks (cons 'find-crypt-file-hook find-file-hooks)
  492.     find-file-not-found-hooks (cons 'find-crypt-file-hook
  493.                     find-file-not-found-hooks)
  494.     minor-mode-alist (nconc (list '(buffer-save-encrypted " Crypt")
  495.                       '(buffer-save-compacted " Compact")
  496.                       '(buffer-save-compressed " Compress"))
  497.                 minor-mode-alist))))
  498. --
  499. Adam Hudd               adam@dadhb1.ti.com             __o
  500. Texas Instruments Inc,  spy@ti.co.uk                  -\<,
  501. Houston, TX             spy@timsg.ti.com        .....O / O
  502.  
  503.