[mew-dist 26735] Re: MIME decode error (**B ENCODING ERROR**))

Kazu Yamamoto ( 山本和彦 ) kazu at example.com
2005年 12月 15日 (木) 16:45:30 JST


> > こういうやりかたが OK なら、「B decode 時のエラー条件緩和」で非
> > 常に甘くなった mew-base64-decode-string() はもともとの strict な
> > コードに戻して、mew-header-decode() でチェックして warning も出
> > す。というのがよいかもです。
> 
> というのをやってみました。mew-base64-decode-string() はなにもし
> ないで、全部 mew-header-decode() に任せています。

遅くなってすいません。アイディアを世襲しつつ、僕風にプログラミングして
みました。こんなのでどうでしょうか?

--かず

Index: mew-bq.el
===================================================================
RCS file: /cvsroot/mew/mew/mew-bq.el,v
retrieving revision 1.37
diff -c -r1.37 mew-bq.el
*** mew-bq.el	26 May 2005 06:15:47 -0000	1.37
--- mew-bq.el	15 Dec 2005 07:44:18 -0000
***************
*** 11,32 ****
    '(("B" . mew-base64-encode-string)
      ("Q" . mew-q-encode-string)))
  
- (defvar mew-header-decode-switch
-   '(("B" . mew-base64-decode-string)
-     ("Q" . mew-q-decode-string)))
- 
- (defvar mew-header-decode-error-switch
-   `(("B" . ,mew-error-invalid-b-encoding)
-     ("Q" . ,mew-error-invalid-q-encoding)))
- 
  (defsubst mew-header-encode-get-func (b-or-q)
    (cdr (mew-assoc-case-equal b-or-q mew-header-encode-switch 0)))
  
  (defsubst mew-header-decode-get-func (b-or-q)
!   (cdr (mew-assoc-case-equal b-or-q mew-header-decode-switch 0)))
  
  (defsubst mew-header-decode-get-error-str (b-or-q)
!   (cdr (mew-assoc-case-equal b-or-q mew-header-decode-error-switch 0)))
  
  (defconst mew-header-decode-regex
    "=\\?\\([^? \t]+\\)\\?\\(.\\)\\?\\([^? \t]+\\)\\?=")
--- 11,38 ----
    '(("B" . mew-base64-encode-string)
      ("Q" . mew-q-encode-string)))
  
  (defsubst mew-header-encode-get-func (b-or-q)
    (cdr (mew-assoc-case-equal b-or-q mew-header-encode-switch 0)))
  
+ 
+ (defvar mew-header-decode-switch
+   `(("B"
+      mew-base64-decode-string
+      mew-base64-decode-error-string
+      ,mew-error-invalid-b-encoding)
+     ("Q"
+      mew-q-decode-string
+      nil
+      ,mew-error-invalid-q-encoding)))
+ 
  (defsubst mew-header-decode-get-func (b-or-q)
!   (nth 1 (mew-assoc-case-equal b-or-q mew-header-decode-switch 0)))
! 
! (defsubst mew-header-decode-get-error-func (b-or-q)
!   (nth 2 (mew-assoc-case-equal b-or-q mew-header-decode-switch 0)))
  
  (defsubst mew-header-decode-get-error-str (b-or-q)
!   (nth 3 (mew-assoc-case-equal b-or-q mew-header-decode-switch 0)))
  
  (defconst mew-header-decode-regex
    "=\\?\\([^? \t]+\\)\\?\\(.\\)\\?\\([^? \t]+\\)\\?=")
***************
*** 81,87 ****
    (let* ((estr (mew-cs-encode-string str hcs)))
      (concat "=?" charset "?" b-or-q "?" (funcall fun estr) "?=")))
  
! (defun mew-header-decode (charset b-or-q estr)
    (let* ((fun (mew-header-decode-get-func b-or-q))
  	 (cs (mew-charset-to-cs charset))
  	 str)
--- 87,93 ----
    (let* ((estr (mew-cs-encode-string str hcs)))
      (concat "=?" charset "?" b-or-q "?" (funcall fun estr) "?=")))
  
! (defun mew-header-decode (charset b-or-q estr &optional key)
    (let* ((fun (mew-header-decode-get-func b-or-q))
  	 (cs (mew-charset-to-cs charset))
  	 str)
***************
*** 93,98 ****
--- 99,105 ----
        mew-error-unknown-charset)
       (fun ;; if cs is nil, mew-cs-decode-string does not cs-decode.
        (setq str (funcall fun estr))
+       (unless str (setq str (mew-header-decode-error key b-or-q estr)))
        (if (null str)
  	  (mew-header-decode-get-error-str b-or-q)
  	(when (and mew-decode-broken
***************
*** 107,112 ****
--- 114,141 ----
  	  (mew-cs-decode-string str cs))))
       (t estr))))
  
+ (defun mew-header-decode-error (key b-or-q estr)
+   (let ((func (mew-header-decode-get-error-func b-or-q)))
+     (if func (funcall func key b-or-q estr))))
+ 
+ (defun mew-base64-decode-error-string (key b-or-q estr)
+   (let ((str estr) strlen r ret)
+     (when (string-match "^\\([^=]*\\)=*$" str)
+       (setq str (mew-match-string 1 str)))
+     (setq strlen (length str))
+     (setq r (% strlen 4))
+     (cond
+      ((= r 0)
+       (setq ret (mew-base64-decode-string str)))
+      ((= r 1)
+       )
+      ((= r 2)
+       (setq ret (mew-base64-decode-string (concat str "=="))))
+      ((= r 3)
+       (setq ret (mew-base64-decode-string (concat str "=")))))
+     (if ret (mew-decode-warning-fields key 'padding))
+     ret))
+ 
  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  ;;;
  ;;; Quoted-printable encoding
***************
*** 610,616 ****
  	(end (match-end 0))
  	(css (mew-header-decode (mew-match-string 1)
  				(mew-match-string 2)
! 				(mew-match-string 3))))
      (setq css (mew-header-sanity-check-string2 css key))
      (delete-region beg end)
      (insert css)))
--- 639,646 ----
  	(end (match-end 0))
  	(css (mew-header-decode (mew-match-string 1)
  				(mew-match-string 2)
! 				(mew-match-string 3)
! 				key)))
      (setq css (mew-header-sanity-check-string2 css key))
      (delete-region beg end)
      (insert css)))
***************
*** 650,655 ****
--- 680,688 ----
         ((eq err 'raw)
  	(setq level 2)
  	(setq wmsg (concat key " has raw text strings.\n")))
+        ((eq err 'padding)
+ 	(setq level 2)
+ 	(setq wmsg (concat key " has illegal padding in encoded text.\n")))
         ((eq err 'quoted)
  	(setq level 2)
  	(setq wmsg (concat key " has encoded-words in quoted text.\n"))))
Index: mew-env.el
===================================================================
RCS file: /cvsroot/mew/mew/mew-env.el,v
retrieving revision 1.43
diff -c -r1.43 mew-env.el
*** mew-env.el	7 Jan 2005 04:30:12 -0000	1.43
--- mew-env.el	15 Dec 2005 07:44:18 -0000
***************
*** 314,333 ****
   ((fboundp 'base64-decode-string)
    (defun mew-base64-decode-string (str64)
      (condition-case nil
! 	(let ((r (% (length str64) 4)))
! 	  (cond
! 	   ((= r 1)
! 	    (if (string-match "=$" str64)
! 		(setq str64 (substring str64 0 (match-beginning 0)))))
! 	   ((= r 2)
! 	    (if (string-match "==$" str64)
! 		(setq str64 (substring str64 0 (match-beginning 0)))
! 	      (setq str64 (concat str64 "=="))))
! 	   ((= r 3)
! 	    (if (string-match "===$" str64)
! 		(setq str64 (substring str64 0 (match-beginning 0)))
! 	      (setq str64 (concat str64 "=")))))
! 	  (base64-decode-string str64))
        (error nil))))
   (t
    (defconst mew-base64-char256
--- 314,320 ----
   ((fboundp 'base64-decode-string)
    (defun mew-base64-decode-string (str64)
      (condition-case nil
! 	(base64-decode-string str64)
        (error nil))))
   (t
    (defconst mew-base64-char256



Mew-dist メーリングリストの案内