Skip to content

Commit

Permalink
new feature: ascii85 decoder
Browse files Browse the repository at this point in the history
decoding pdf files encoded with ascii85, including adobe ending
  • Loading branch information
victron committed Feb 8, 2019
1 parent 1406190 commit a561fbf
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
53 changes: 53 additions & 0 deletions ascii85.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// file with help function for ascii85 decoder
// later if new decoders is going to add it reasonable to rename file and add them here
// also create interfaces to switch between them (like in unidoc)

package pdf

import (
"io"
)

type alphaReader struct {
reader io.Reader
}

func newAlphaReader(reader io.Reader) *alphaReader {
return &alphaReader{reader: reader}
}

func checkAscii85(r byte) byte {
if r >= '!' && r <= 'u' { // 33 <= ascii85 <=117
return r
}
if r == '~' {
return 1 // for marking possible end of data
}
return 0 // if non-ascii85
}

func (a *alphaReader) Read(p []byte) (int, error) {
n, err := a.reader.Read(p)
if err == io.EOF {
}
if err != nil {
return n, err
}
buf := make([]byte, n)
tilda := false
for i := 0; i < n; i++ {
char := checkAscii85(p[i])
if char == '>' && tilda { // end of data
break
}
if char > 1 {
buf[i] = char
}
if char == 1 {
tilda = true // possible end of data
}
}

copy(p, buf)
return n, nil
}
11 changes: 11 additions & 0 deletions read.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ import (
"crypto/cipher"
"crypto/md5"
"crypto/rc4"
"encoding/ascii85"
"fmt"
"io"
"io/ioutil"
Expand Down Expand Up @@ -840,6 +841,16 @@ func applyFilter(rd io.Reader, name string, param Value) io.Reader {
case 12:
return &pngUpReader{r: zr, hist: make([]byte, 1+columns), tmp: make([]byte, 1+columns)}
}
case "ASCII85Decode":
cleanAscii85 := newAlphaReader(rd)
decoder := ascii85.NewDecoder(cleanAscii85)

if param.Keys() == nil {
return decoder
} else {
fmt.Println("param=", param)
panic("not expected DecodeParms for ascii85")
}
}
}

Expand Down

0 comments on commit a561fbf

Please sign in to comment.