1 ;;; tramp-uu.el --- uuencode in Lisp
3 ;; Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007,
4 ;; 2008 Free Software Foundation, Inc.
6 ;; Author: Kai Großjohann <kai.grossjohann@gmx.net>
7 ;; Keywords: comm, terminals
9 ;; This file is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 3, or (at your option)
14 ;; This file is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING. If not, see
21 ;; <http://www.gnu.org/licenses/>.
25 ;; An implementation of "uuencode" in Lisp. Uses the function
26 ;; base64-encode-region which is built-in to modern Emacsen.
30 (defvar tramp-uu-b64-alphabet
31 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
32 "Mapping from base64-encoded character to the byte it represents.")
34 (defvar tramp-uu-b64-char-to-byte
40 tramp-uu-b64-alphabet
))
41 "Alist of mapping from base64 character to its byte.")
43 (defun tramp-uu-byte-to-uu-char (byte)
44 "Return the character encoding BYTE."
45 (if (zerop byte
) ?
` (+ byte
32)))
47 (defun tramp-uu-b64-char-to-byte (char)
48 "Return the byte that is encoded as CHAR."
49 (cdr (assq char tramp-uu-b64-char-to-byte
)))
51 (defun tramp-uuencode-region (beg end
)
52 "UU-encode the region between BEG and END."
53 ;; First we base64 encode the region, then we transmogrify that into
55 (let ((len (base64-encode-region beg end t
))
62 (setq c
(char-after (point)))
65 ;; "=" means padding. Insert "`" instead. Not counted for length.
66 (progn (insert "`") (setq len
(1- len
)))
67 (insert (tramp-uu-byte-to-uu-char (tramp-uu-b64-char-to-byte c
)))
69 ;; Every 60 characters, add "M" at beginning of line (as
70 ;; length byte) and insert a newline.
71 (when (zerop (% i
60))
74 (insert (char-to-string (+ 32 (/ (* 3 60) 4)))))
76 ;; If there is something leftover, we compute the length byte
77 ;; for that stuff and insert it and a trailing newline.
78 (unless (zerop (% i
60))
81 (insert (char-to-string (+ 32 (%
(- end beg
) 45)))))
83 ;; Why is there always a "`" line at the end?
86 (insert "begin 600 xxx\n"))))
90 ;; arch-tag: 7153f2c6-8be5-4cd2-8c06-0fbcf5190ef6
91 ;;; tramp-uu.el ends here