Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
pedroalbanese committed Oct 3, 2022
1 parent ef44bbe commit 27a678a
Show file tree
Hide file tree
Showing 5 changed files with 530 additions and 2 deletions.
20 changes: 20 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Copyright (c) 2020 Blake Williams <[email protected]>
Copyright (c) 2022 Pedro Albanese <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
19 changes: 17 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,17 @@
# uuencode
UUEncoder is a tool that converts to and from uuencoding
# UUE
UUEncode is a tool that converts to and from uuencoding

## Usage
<pre>
Usage of uuencode:
-d Decode instead of Encode
-f string
Target file
-o string
Output file</pre>

## License
This project is licensed under the MIT License.

**Copyright (c) 2020 Blake Williams <[email protected]>**
**Copyright (c) 2022 Pedro Albanese <[email protected]>**
73 changes: 73 additions & 0 deletions cmd/uuencode/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package main

import (
"flag"
"fmt"
"io"
"log"
"os"

"github.com/pedroalbanese/uuencode"
)

var (
dec = flag.Bool("d", false, "Decode instead of Encode")
ifile = flag.String("f", "", "Target file")
ofile = flag.String("o", "", "Output file")
)

func main() {
flag.Parse()

if len(os.Args) < 2 {
fmt.Fprintln(os.Stderr, "Usage of", os.Args[0]+":")
flag.PrintDefaults()
os.Exit(1)
}

if *dec == false {
var err error
infile, err := os.Open(*ifile)
if err != nil {
log.Println(err)
}
var outfile *os.File
if *ofile == "-" || *ofile == "" {
outfile = os.Stdout
} else {
outfile, err = os.Create(*ofile)
if err != nil {
log.Println(err)
}
}
uw := uuencode.NewWriter(outfile, *ifile, 0664)
if _, err = io.Copy(uw, infile); err != nil {
return
}
if err := uw.Flush(); err != nil {
return
}
} else {
file, err := os.Open(*ifile)
if err != nil {
log.Println(err)
}
var outfile *os.File
if *ofile == "-" || *ofile == "" {
outfile = os.Stdout
} else {
outfile, err = os.Create(*ofile)
if err != nil {
log.Println(err)
}
}
ur := uuencode.NewReader(file, nil)
_, err = io.Copy(outfile, ur)
if err != nil {
log.Fatal(err)
}
f, _ := ur.File()
m, _ := ur.Mode()
fmt.Fprintf(os.Stderr, "file: %s, mode: %03o\n", f, m)
}
}
241 changes: 241 additions & 0 deletions reader.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,241 @@
package uuencode

import (
"bytes"
"fmt"
"io"
"os"
"strconv"
)

const (
readBegin = iota
readLineLength
readLine
readEnd
readDone
)

var (
tokMagic = []byte("begin ")
tokReadEnd = []byte("end")
tokReadEmptyLine = []byte("`")
)

type Reader struct {
inner io.Reader
state int
file string
mode os.FileMode
scratch []byte
buf []byte
line []byte
lineRem []byte
linesz int
}

func NewReader(rdr io.Reader, scratch []byte) *Reader {
if len(scratch) < 2048 {
scratch = make([]byte, 2048)
}
return &Reader{
inner: rdr,
scratch: scratch,
line: make([]byte, 63), // line can never have more than 63 chars of decoded data
}
}

func (r *Reader) File() (name string, ok bool) {
if r.state < readLineLength {
return "", false
}
return r.file, true
}

func (r *Reader) Mode() (mode os.FileMode, ok bool) {
if r.state < readLineLength {
return 0, false
}
return r.mode, true
}

func (r *Reader) pull() (err error) {
newsz := copy(r.scratch, r.buf)
n, err := r.inner.Read(r.scratch[newsz:])
n += newsz
r.buf = r.scratch[:n]

if n == 0 && err == io.EOF {
if r.state != readDone {
return r.fail("premature end of data stream")
}
r.state = readDone

} else if err == io.EOF {
err = nil
}
return err
}

func (r *Reader) fail(msg string) (err error) {
r.state = readDone
return fmt.Errorf("uudecode: %s", msg)
}

func (r *Reader) Read(b []byte) (n int, err error) {
bsz := len(b)

if len(r.lineRem) > 0 {
n += copy(b, r.lineRem)
r.lineRem = r.lineRem[n:]
if n <= bsz {
return n, nil
}
}

for {
switch r.state {
case readBegin:
if err := r.pull(); err != nil {
return n, err
}
nl := bytes.IndexByte(r.scratch, '\n')
if nl < 0 {
return n, r.fail("header missing delimiter")
}
if !bytes.HasPrefix(r.scratch, tokMagic) {
return n, r.fail("header missing magic")
}
pos := len(tokMagic)

// tokMagic includes first space, so spc is the mode string's end delimiter:
spc := bytes.IndexByte(r.scratch[pos:], ' ')
if spc < 0 {
return n, r.fail("missing file name")
}

modestr := string(r.scratch[pos : pos+spc])
mode, err := strconv.ParseInt(modestr, 8, 16)
if err != nil {
return n, r.fail("invalid file mode")
}

r.mode = os.FileMode(mode)
r.file = string(r.scratch[pos+spc+1 : nl])

r.state = readLineLength
r.buf = r.buf[nl+1:]

case readLineLength:
if len(r.buf) < 1 {
if err := r.pull(); err != nil {
return n, err
}
if len(r.buf) < 1 {
return n, r.fail("missing line length")
}
}

// This also works if the zero is a '`' as (96 - ' ') & 63 == 0
r.linesz = int((r.buf[0] - ' ') & 63)
r.buf = r.buf[1:]
if r.linesz == 0 {
r.state = readEnd
} else {
r.state = readLine
}

case readLine:
nl := bytes.IndexByte(r.buf, '\n')
if nl < 0 {
if err := r.pull(); err != nil {
return n, err
}
nl = bytes.IndexByte(r.buf, '\n')
if nl < 0 {
return n, r.fail("missing line ending")
}
}

end := nl
if nl > 0 && r.buf[nl-1] == '\r' {
nl--
}

if nl&3 != 0 {
return n, r.fail("encoded line length must be divisible by 4")
}
if nl > 64 {
return n, r.fail("encoded line too long")
}

expectedDec := (nl / 4) * 3
if expectedDec < r.linesz {
return n, r.fail("unexpected decoded size for line")
}

var lenc, ldec []byte = nil, r.line
var ienc, idec int
lenc, r.buf = r.buf[:nl], r.buf[end+1:]
r.state = readLineLength

for ienc < nl {
e0, e1, e2, e3 := lenc[ienc]-' ', lenc[ienc+1]-' ', lenc[ienc+2]-' ', lenc[ienc+3]-' '
if e0 > 64 || e1 > 64 || e2 > 64 || e3 > 64 {
return n, r.fail("unexpected encoded byte")
}
// bytes may be '64' instead of '0':
e0, e1, e2, e3 = e0&63, e1&63, e2&63, e3&63

ldec[idec+0] = e0<<2 | e1>>4 // dec: 111111 112222 222233 333333
ldec[idec+1] = e1<<4 | e2>>2 // enc: 111111 222222 333333 444444
ldec[idec+2] = e2<<6 | e3
ienc, idec = ienc+4, idec+3
}

ldec = ldec[:r.linesz]
cp := copy(b[n:], ldec)
n += cp
r.lineRem = ldec[cp:]
if n == bsz {
return n, nil
}

case readEnd:
nl := bytes.IndexByte(r.buf, '\n')
if nl < 0 {
if err := r.pull(); err != nil {
return n, err
}
nl = bytes.IndexByte(r.buf, '\n')
if nl < 0 {
return n, r.fail("missing line ending")
}
}
end := nl
if nl > 0 && r.buf[nl-1] == '\r' {
nl--
}
if nl == 0 {
r.buf = r.buf[end+1:]
continue
}
if bytes.Equal(r.buf[:nl], tokReadEmptyLine) {
r.buf = r.buf[end+1:]
continue
}
trimmed := bytes.TrimRight(r.buf, "\r\n")
if !bytes.Equal(trimmed, tokReadEnd) {
return n, r.fail("unexpected end")
}

r.state = readDone

case readDone:
return n, io.EOF

default:
panic("unknown state")
}
}
}
Loading

0 comments on commit 27a678a

Please sign in to comment.