예제 #1
0
파일: ps.go 프로젝트: MattDavisRV/pdfreader
func String(s []byte) []byte {
	if s[0] == '<' {
		r := hex.Decode(string(s[1 : len(s)-1]))
		return r
	}
	if s[0] != '(' {
		return s
	}
	r := make([]byte, len(s))
	q := 0
	for p := 1; p < len(s)-1; p++ {
		if s[p] == '\\' {
			p++
			switch s[p] {
			case 13:
				if s[p+1] == 10 {
					p++
				}
				q--
			case 10:
				q--
			case 'n':
				r[q] = 10
			case 'r':
				r[q] = 13
			case 't':
				r[q] = 9
			case 'b':
				r[q] = 8
			case 'f':
				r[q] = 12
			case '0', '1', '2', '3', '4', '5', '6', '7':
				a := s[p] - '0'
				if s[p+1] >= '0' && s[p+1] <= '7' {
					p++
					a = (a << 3) + (s[p] - '0')
					if s[p+1] >= '0' && s[p+1] <= '7' {
						p++
						a = (a << 3) + (s[p] - '0')
					}
				}
				r[q] = a
			default:
				r[q] = s[p]
			}
		} else {
			r[q] = s[p]
		}
		q++
	}
	return r[0:q]
}
예제 #2
0
// pd.DecodedStream() returns decoded contents of a stream.
func (pd *PdfReaderT) DecodedStream(reference []byte) (DictionaryT, []byte) {
	dic, data := pd.Stream(reference)
	if f, ok := dic["/Filter"]; ok {
		filter := pd.ForcedArray(f)
		var decos [][]byte
		if d, ok := dic["/DecodeParams"]; ok {
			decos = pd.ForcedArray(d)
		} else {
			decos = make([][]byte, len(filter))
		}
		for ff := range filter {
			deco := pd.Dic(decos[ff])
			switch string(filter[ff]) {
			case "/FlateDecode":
				data = fancy.ReadAndClose(zlib.NewInflater(fancy.SliceReader(data)))
			case "/LZWDecode":
				early := true
				if deco != nil {
					if s, ok := deco["/EarlyChange"]; ok {
						early = pd.Num(s) == 1
					}
				}
				data = lzw.Decode(data, early)
			case "/ASCII85Decode":
				ds := data
				for len(ds) > 1 && ds[len(ds)-1] < 33 {
					ds = ds[0 : len(ds)-1]
				}
				if len(ds) >= 2 && ds[len(ds)-1] == '>' && ds[len(ds)-2] == '~' {
					ds = ds[0 : len(ds)-2]
				}
				data = fancy.ReadAll(ascii85.NewDecoder(fancy.SliceReader(ds)))
			case "/ASCIIHexDecode":
				data = hex.Decode(string(data))
			default:
				data = []byte{}
			}
		}
	}
	return dic, data
}