1 ;;; tramp-uu.el --- uuencode in Lisp -*- lexical-binding:t -*-
3 ;; Copyright (C) 2002-2017 Free Software Foundation, Inc.
5 ;; Author: Kai Großjohann <kai.grossjohann@gmx.net>
6 ;; Maintainer: Michael Albinus <michael.albinus@gmx.de>
7 ;; Keywords: comm, terminals
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
27 ;; An implementation of "uuencode" in Lisp. Uses the function
28 ;; base64-encode-region which is built-in to modern Emacsen.
32 (defconst tramp-uu-b64-alphabet
33 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
34 "Mapping from base64-encoded character to the byte it represents.")
36 (defconst tramp-uu-b64-char-to-byte
42 tramp-uu-b64-alphabet
))
43 "Alist of mapping from base64 character to its byte.")
45 (defun tramp-uu-byte-to-uu-char (byte)
46 "Return the character encoding BYTE."
47 (if (zerop byte
) ?
` (+ byte
32)))
49 (defun tramp-uu-b64-char-to-byte (char)
50 "Return the byte that is encoded as CHAR."
51 (cdr (assq char tramp-uu-b64-char-to-byte
)))
54 (defun tramp-uuencode-region (beg end
)
55 "UU-encode the region between BEG and END."
56 ;; First we base64 encode the region, then we transmogrify that into
58 (let ((len (base64-encode-region beg end t
))
64 (setq c
(char-after (point)))
67 ;; "=" means padding. Insert "`" instead. Not counted for length.
68 (progn (insert "`") (setq len
(1- len
)))
69 (insert (tramp-uu-byte-to-uu-char (tramp-uu-b64-char-to-byte c
)))
71 ;; Every 60 characters, add "M" at beginning of line (as
72 ;; length byte) and insert a newline.
73 (when (zerop (% i
60))
76 (insert (char-to-string (+ 32 (/ (* 3 60) 4)))))
78 ;; If there is something leftover, we compute the length byte
79 ;; for that stuff and insert it and a trailing newline.
80 (unless (zerop (% i
60))
83 (insert (char-to-string (+ 32 (%
(- end beg
) 45)))))
85 ;; Why is there always a "`" line at the end?
88 (insert "begin 600 xxx\n"))))
90 (add-hook 'tramp-unload-hook
92 (unload-feature 'tramp-uu
'force
)))
96 ;;; tramp-uu.el ends here