x-gzip64 decoder for FLIM on Emacs24

火曜日, 12月 29 2015 @ 11:35 午前 JST

投稿者: kazuhiro

Emacs24で実装された、zlibサポートを利用したx-gzip64エンコードのデコーダです。 今更FLIMに入れるのもどうかという気もしますので、ここで公開しておきます。

;; x-gzip64 decoder
(when (and (fboundp 'zlib-available-p) (zlib-available-p))
  (eval-after-load "mel-g"
    '(progn
       (mel-define-method mime-decode-region (start end (nil "x-gzip64"))
         (save-excursion
           (save-restriction
             (narrow-to-region start end)
             (let ((mb enable-multibyte-characters))
               (set-buffer-multibyte nil)
               (base64-decode-region (point-min) (point-max))
               (zlib-decompress-region (point-min) (point-max))
               (set-buffer-multibyte mb)))))

       (mel-define-method mime-decode-string (string (nil "x-gzip64"))
         (with-temp-buffer
           (set-buffer-multibyte nil)
           (insert string)
           (base64-decode-region (point-min) (point-max))
           (zlib-decompress-region (point-min) (point-max))
           (buffer-string)))

       (mel-define-method mime-write-decoded-region (start end filename
                                                           (nil "x-gzip64"))
         "Decode and write current region encoded by gzip64 into FILENAME.
START and END are buffer positions."
         (interactive "*r\nFWrite decoded region to file: ")
         (let ((coding-system-for-read 'binary)
               (coding-system-for-write 'binary)
               (buffer (current-buffer)))
           (with-temp-buffer
             (set-buffer-multibyte nil)
             (insert-buffer-substring buffer start end)
             (base64-decode-region (point-min) (point-max))
             (zlib-decompress-region (point-min) (point-max))
             (write-region (point-min) (point-max) filename)))))))

タグ: Emacs Wanderlust

コメント (0件)


Kazuhiro's blog
http://ikazuhiro.s206.xrea.com/article.php/20151229113543633