~dricottone/digestion

ref: 62eccd92f6655185139d9015022c8b9953a51521 digestion/decoder/encodings.go -rw-r--r-- 1.0 KiB
62eccd92Dominic Ricottone Refactored into subdirectories; Added common decoders 4 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package decoder

import (
	"strings"
)

const (
	UTF8            = "UTF8"
	Base64          = "Base64"
	QuotedPrintable = "QuotedPrintable"
	Unknown         = "Unknown"
)

func determine_encoding(encoding string) string {
	if strings.Contains(encoding, "base64") {
		return Base64
	} else if strings.Contains(encoding, "quoted-printable") {
		return QuotedPrintable
	} else if strings.Contains(encoding, "utf-8") {
		return UTF8
	} else {
		return Unknown
	}
}

// Translate an encoding label into a numeric value according to preference
// of use in processing. Preference tiers are:
//  1. UTF-8
//  2. base64, quoted-printable
func EvaluateEncoding(encoding string) int {
	switch determine_encoding(encoding) {
	case UTF8:
		return 0
	case Base64:
		return 1
	case QuotedPrintable:
		return 1
	default:
		return 10
	}
}

func DecodeArray(lines []string, encoding string) ([]string, error) {
	switch determine_encoding(encoding) {
	case Base64:
		return decode_base64(lines)
	case QuotedPrintable:
		return decode_quotedprintable(lines)
	default:
		return lines, nil
	}
}