*** empty log message ***
[bpt/emacs.git] / lisp / net / tramp-uu.el
1 ;;; -*- coding: iso-2022-7bit; -*-
2 ;;; tramp-uu.el --- uuencode in Lisp
3
4 ;; Copyright (C) 2002, 2003, 2004, 2005, 2006,
5 ;; 2007 Free Software Foundation, Inc.
6
7 ;; Author: Kai Gro\e,A_\e(Bjohann <kai.grossjohann@gmx.net>
8 ;; Keywords: comm, terminals
9
10 ;; This file is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 3, or (at your option)
13 ;; any later version.
14
15 ;; This file is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to
22 ;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23 ;; Boston, MA 02110-1301, USA.
24
25 ;;; Commentary:
26
27 ;; An implementation of "uuencode" in Lisp. Uses the function
28 ;; base64-encode-region which is built-in to modern Emacsen.
29
30 ;;; Code:
31
32 (defvar tramp-uu-b64-alphabet
33 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
34 "Mapping from base64-encoded character to the byte it represents.")
35
36 (defvar tramp-uu-b64-char-to-byte
37 (let ((i 0))
38 (mapcar (lambda (c)
39 (prog1
40 (cons c i)
41 (setq i (1+ i))))
42 tramp-uu-b64-alphabet))
43 "Alist of mapping from base64 character to its byte.")
44
45 (defun tramp-uu-byte-to-uu-char (byte)
46 "Return the character encoding BYTE."
47 (if (zerop byte) ?` (+ byte 32)))
48
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)))
52
53 (defun tramp-uuencode-region (beg end)
54 "UU-encode the region between BEG and END."
55 ;; First we base64 encode the region, then we transmogrify that into
56 ;; uu encoding.
57 (let ((len (base64-encode-region beg end t))
58 (padding 0)
59 i c)
60 (save-excursion
61 (goto-char beg)
62 (setq i 0)
63 (while (< i len)
64 (setq c (char-after (point)))
65 (delete-char 1)
66 (if (equal c ?=)
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)))
70 (setq i (1+ i)))
71 ;; Every 60 characters, add "M" at beginning of line (as
72 ;; length byte) and insert a newline.
73 (when (zerop (% i 60))
74 (save-excursion
75 (beginning-of-line)
76 (insert (char-to-string (+ 32 (/ (* 3 60) 4)))))
77 (insert "\n")))
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))
81 (save-excursion
82 (beginning-of-line)
83 (insert (char-to-string (+ 32 (% (- end beg) 45)))))
84 (insert "\n"))
85 ;; Why is there always a "`" line at the end?
86 (insert "`\nend\n")
87 (goto-char beg)
88 (insert "begin 600 xxx\n"))))
89
90 (provide 'tramp-uu)
91
92 ;;; arch-tag: 7153f2c6-8be5-4cd2-8c06-0fbcf5190ef6
93 ;;; tramp-uu.el ends here