|
| 1 | +package uuencode |
| 2 | + |
| 3 | +import ( |
| 4 | + "bufio" |
| 5 | + "bytes" |
| 6 | + "encoding/base64" |
| 7 | + "fmt" |
| 8 | + "io" |
| 9 | + "strings" |
| 10 | +) |
| 11 | + |
| 12 | +const ( |
| 13 | + // StandardCharset is the standard charset for uuencoded files: ASCII characters 32 - 95. |
| 14 | + StandardCharset = " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_" |
| 15 | + |
| 16 | + // AlternateCharset is the same as the standard charset, except that the space character is replaced by backtick. |
| 17 | + // This encoding is non-standard but used occasionally. (Like in the BSD uuencode implementation). |
| 18 | + AlternateCharset = "`!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_" |
| 19 | +) |
| 20 | + |
| 21 | +// Decoder encapsulates functionality for decoding uuencoded content. |
| 22 | +// To create a Decoder, use the helper functions NewStandardDecoder or NewDecoder(charset). |
| 23 | +type Decoder struct { |
| 24 | + // encoding is used to decode individual lines within the encoded text. |
| 25 | + encoding *base64.Encoding |
| 26 | + |
| 27 | + // paddingChar is used to pad lines that have had their padding chopped off for one reason or another. |
| 28 | + paddingChar string |
| 29 | +} |
| 30 | + |
| 31 | +// NewStandardDecoder returns a Decoder that uses the StandardCharset. |
| 32 | +func NewStandardDecoder() Decoder { |
| 33 | + return NewDecoder(StandardCharset) |
| 34 | +} |
| 35 | + |
| 36 | +// NewDecoder returns a decoder using the given charset. |
| 37 | +// See StandardCharset and AlternateCharset for common values. |
| 38 | +// Note: the provided charset must be a valid base64 charset, otherwise attempts to Decode may panic. |
| 39 | +func NewDecoder(charset string) Decoder { |
| 40 | + return Decoder{ |
| 41 | + encoding: base64.NewEncoding(charset).WithPadding(base64.NoPadding), |
| 42 | + paddingChar: string(charset[0]), // Padding char is just the first character in the charset |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +// DecodeToBytes is a convenience function for decoding a reader when you just want all the decoded contents in memory in a byte slice. |
| 47 | +// See Decode for more info. |
| 48 | +func (d Decoder) DecodeToBytes(reader io.Reader) ([]byte, error) { |
| 49 | + var buf bytes.Buffer |
| 50 | + if err := d.Decode(reader, &buf); err != nil { |
| 51 | + return nil, err |
| 52 | + } |
| 53 | + |
| 54 | + return buf.Bytes(), nil |
| 55 | +} |
| 56 | + |
| 57 | +// Decode decodes the uuencoded contents (as described here: https://en.wikipedia.org/wiki/Uuencoding#Encoded_format) |
| 58 | +// of reader and writes the decoded bytes to the given output writer. |
| 59 | +// This function assumes there is only one encoded file in the reader, it will ignore anything past the end of the first encoded file. |
| 60 | +func (d Decoder) Decode(reader io.Reader, output io.Writer) error { |
| 61 | + scanner := bufio.NewScanner(reader) |
| 62 | + |
| 63 | + lineNumber := 0 |
| 64 | + for scanner.Scan() { |
| 65 | + lineNumber++ |
| 66 | + |
| 67 | + if scanner.Err() != nil { |
| 68 | + return fmt.Errorf("error while scanner reader: %w", scanner.Err()) |
| 69 | + } |
| 70 | + |
| 71 | + line := scanner.Text() |
| 72 | + |
| 73 | + // We don't care about the begin line, we also don't care about empty lines |
| 74 | + if strings.HasPrefix(line, "begin") || line == "" { |
| 75 | + continue |
| 76 | + } |
| 77 | + |
| 78 | + // When we find the first end line, we're done. |
| 79 | + if line == "end" { |
| 80 | + return nil |
| 81 | + } |
| 82 | + |
| 83 | + // If it's not a begin or end line, first check the line length character. |
| 84 | + // If it's the special character backtick (`), the line is empty and we should skip it |
| 85 | + lengthChar := line[0] |
| 86 | + if lengthChar == '`' { |
| 87 | + continue |
| 88 | + } |
| 89 | + |
| 90 | + // uuencoding adds 32 to the lengthChar so its a printable character |
| 91 | + decodedLen := lengthChar - 32 |
| 92 | + |
| 93 | + // Some encoding schemes don't use the special character for empty lines. |
| 94 | + if decodedLen == 0 { |
| 95 | + continue |
| 96 | + } |
| 97 | + |
| 98 | + // The formatted characters are everything after the length char. |
| 99 | + // Sometimes padding is omitted from the line, so we have to make sure we add it back before decoding. |
| 100 | + expectedLen := d.encoding.EncodedLen(int(decodedLen)) |
| 101 | + encodedCharacters := d.padContentLine(line[1:], expectedLen) |
| 102 | + |
| 103 | + decoded, err := d.encoding.DecodeString(encodedCharacters) |
| 104 | + if err != nil { |
| 105 | + return fmt.Errorf("error decoding line %d: %w", lineNumber, err) |
| 106 | + } |
| 107 | + |
| 108 | + // Write the decoded bytes to the output writer |
| 109 | + if _, err := output.Write(decoded[:decodedLen]); err != nil { |
| 110 | + return fmt.Errorf("error writing decoded bytes to writer: %w", err) |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + // If we made it out of the loop, it means we never saw the 'end' line |
| 115 | + return fmt.Errorf("malformed input; missing 'end' line") |
| 116 | +} |
| 117 | + |
| 118 | +func (d Decoder) padContentLine(line string, expectedLen int) string { |
| 119 | + for len(line) < expectedLen { |
| 120 | + line += d.paddingChar |
| 121 | + } |
| 122 | + |
| 123 | + return line |
| 124 | +} |
0 commit comments