8
8
9
9
package rfc2045
10
10
import "io"
11
- import "fmt"
12
- import "mime"
11
+ import "bytes"
13
12
import "strings"
13
+ import "encoding/base64"
14
14
import "mime/quotedprintable"
15
15
16
16
// DecodeB decodes Base64 encoded text.
@@ -20,15 +20,11 @@ import "mime/quotedprintable"
20
20
// Returns:
21
21
// - (string): Decoded text
22
22
func DecodeB (argv0 string , argv1 string ) (string , error ) {
23
- if len (argv0 ) < 8 { return argv0 , nil }
24
- if len (argv1 ) == 0 { argv1 = "utf-8" }
25
-
26
- decodingif := new (mime.WordDecoder )
27
- base64text := strings .Join (strings .Split (strings .TrimSpace (argv0 ), "\n " ), "" )
28
- base64text = fmt .Sprintf ("=?%s?B?%s?=" , argv1 , base64text )
23
+ if len (argv0 ) < 8 { return argv0 , nil }
29
24
30
- plain , nyaan := decodingif .Decode (base64text ); if nyaan != nil { return "" , nyaan }
31
- return plain , nil
25
+ base64text := strings .ReplaceAll (strings .TrimSpace (argv0 ), "\n " , "" )
26
+ cv , nyaan := base64 .StdEncoding .DecodeString (base64text ); if nyaan != nil { return "" , nyaan }
27
+ return string (cv ), nil
32
28
}
33
29
34
30
// DecodeQ() decodes Quoted-Pritable encdoed text
@@ -38,14 +34,11 @@ func DecodeB(argv0 string, argv1 string) (string, error) {
38
34
// - (string): Decoded text
39
35
// - (error): Decoding error
40
36
func DecodeQ (argv0 string ) (string , error ) {
41
- readstring := strings .NewReader (argv0 )
42
- decodingif := quotedprintable .NewReader (readstring )
43
- plainvalue := ""
44
-
45
- // Failed to decode the quoted-printable text
46
- plain , nyaan := io .ReadAll (decodingif ); if nyaan != nil { plainvalue = argv0 }
47
- if len (plain ) > 0 { plainvalue = string (plain ) }
37
+ if len (argv0 ) < 8 { return argv0 , nil }
38
+ decodingif := quotedprintable .NewReader (bytes .NewReader ([]byte (argv0 )))
48
39
49
- return plainvalue , nyaan
40
+ var readbuffer bytes.Buffer
41
+ _ , nyaan := io .Copy (& readbuffer , decodingif ); if nyaan != nil { return "" , nyaan }
42
+ return readbuffer .String (), nil
50
43
}
51
44
0 commit comments