Skip to content

Commit e33b502

Browse files
committed
Fix #755
1 parent e3358c4 commit e33b502

File tree

8 files changed

+80
-29
lines changed

8 files changed

+80
-29
lines changed

pkg/pdfcpu/crypto.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1087,7 +1087,7 @@ func decryptBytes(b []byte, objNr, genNr int, encKey []byte, needAES bool, r int
10871087
// decryptString decrypts s using RC4 or AES.
10881088
func decryptString(s string, objNr, genNr int, key []byte, needAES bool, r int) ([]byte, error) {
10891089

1090-
bb, err := types.Unescape(s, true)
1090+
bb, err := types.Unescape(s)
10911091
if err != nil {
10921092
return nil, err
10931093
}

pkg/pdfcpu/model/dereference.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ func (xRefTable *XRefTable) DereferenceStringEntryBytes(d types.Dict, key string
377377

378378
switch o := o.(type) {
379379
case types.StringLiteral:
380-
bb, err := types.Unescape(o.Value(), false)
380+
bb, err := types.Unescape(o.Value())
381381
if err != nil {
382382
return nil, err
383383
}

pkg/pdfcpu/model/xreftable.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1563,7 +1563,7 @@ func (xRefTable *XRefTable) IDFirstElement() (id []byte, err error) {
15631563
return nil, errors.New("pdfcpu: ID must contain hex literals or string literals")
15641564
}
15651565

1566-
bb, err := types.Unescape(sl.Value(), false)
1566+
bb, err := types.Unescape(sl.Value())
15671567
if err != nil {
15681568
return nil, err
15691569
}

pkg/pdfcpu/types/dict.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -561,7 +561,7 @@ func (d Dict) StringEntryBytes(key string) ([]byte, error) {
561561

562562
s := d.StringLiteralEntry(key)
563563
if s != nil {
564-
bb, err := Unescape(s.Value(), false)
564+
bb, err := Unescape(s.Value())
565565
if err != nil {
566566
return nil, err
567567
}

pkg/pdfcpu/types/string.go

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,7 @@ func regularChar(c byte, esc bool) bool {
107107
}
108108

109109
// Unescape resolves all escape sequences of s.
110-
func Unescape(s string, enc bool) ([]byte, error) {
111-
112-
// TODO remove "enc" parameter.
113-
110+
func Unescape(s string) ([]byte, error) {
114111
var esc bool
115112
var longEol bool
116113
var octalCode string
@@ -129,6 +126,21 @@ func Unescape(s string, enc bool) ([]byte, error) {
129126
}
130127
}
131128

129+
if len(octalCode) > 0 {
130+
if strings.ContainsRune("01234567", rune(c)) {
131+
octalCode = octalCode + string(c)
132+
if len(octalCode) == 3 {
133+
b.WriteByte(ByteForOctalString(octalCode))
134+
octalCode = ""
135+
esc = false
136+
}
137+
continue
138+
}
139+
b.WriteByte(ByteForOctalString(octalCode))
140+
octalCode = ""
141+
esc = false
142+
}
143+
132144
if regularChar(c, esc) {
133145
b.WriteByte(c)
134146
continue
@@ -149,19 +161,6 @@ func Unescape(s string, enc bool) ([]byte, error) {
149161

150162
// escaped = true && any other than \
151163

152-
if len(octalCode) > 0 {
153-
if !strings.ContainsRune("01234567", rune(c)) {
154-
return nil, errors.Errorf("Unescape: illegal octal sequence detected %X", octalCode)
155-
}
156-
octalCode = octalCode + string(c)
157-
if len(octalCode) == 3 {
158-
b.WriteByte(ByteForOctalString(octalCode))
159-
octalCode = ""
160-
esc = false
161-
}
162-
continue
163-
}
164-
165164
// Ignore \eol line breaks.
166165
if c == 0x0A {
167166
esc = false
@@ -189,6 +188,10 @@ func Unescape(s string, enc bool) ([]byte, error) {
189188
esc = false
190189
}
191190

191+
if len(octalCode) > 0 {
192+
b.WriteByte(ByteForOctalString(octalCode))
193+
}
194+
192195
return b.Bytes(), nil
193196
}
194197

pkg/pdfcpu/types/string_test.go

Lines changed: 54 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package types
1818

1919
import (
20+
"bytes"
2021
"testing"
2122
)
2223

@@ -52,9 +53,56 @@ func TestByteForOctalString(t *testing.T) {
5253
}
5354
for _, test := range tests {
5455
t.Run(test.input, func(t *testing.T) {
55-
actual := ByteForOctalString(test.input)
56-
if actual != test.expected {
57-
t.Errorf("got %x; want %x", test.expected, actual)
56+
got := ByteForOctalString(test.input)
57+
if got != test.expected {
58+
t.Errorf("got %x; want %x", got, test.expected)
59+
}
60+
})
61+
}
62+
}
63+
64+
func TestUnescapeStringWithOctal(t *testing.T) {
65+
tests := []struct {
66+
input string
67+
expected []byte
68+
}{
69+
{
70+
"\\5",
71+
[]byte{0x05},
72+
},
73+
{
74+
"\\5a",
75+
[]byte{0x05, 'a'},
76+
},
77+
{
78+
"\\5\\5",
79+
[]byte{0x05, 0x05},
80+
},
81+
{
82+
"\\53",
83+
[]byte{'+'},
84+
},
85+
{
86+
"\\53a",
87+
[]byte{'+', 'a'},
88+
},
89+
{
90+
"\\053",
91+
[]byte{'+'},
92+
},
93+
{
94+
"\\0053",
95+
[]byte{0x05, '3'},
96+
},
97+
}
98+
for _, test := range tests {
99+
t.Run(test.input, func(t *testing.T) {
100+
got, err := Unescape(test.input)
101+
if err != nil {
102+
t.Fail()
103+
}
104+
if !bytes.Equal(got, test.expected) {
105+
t.Errorf("got %x; want %x", got, test.expected)
58106
}
59107
})
60108
}
@@ -116,12 +164,12 @@ func TestDecodeName(t *testing.T) {
116164
}
117165
for _, test := range tests {
118166
t.Run(test.input, func(t *testing.T) {
119-
actual, err := DecodeName(test.input)
167+
got, err := DecodeName(test.input)
120168
if err != nil {
121169
t.Fail()
122170
}
123-
if actual != test.expected {
124-
t.Errorf("got %x; want %x", test.expected, actual)
171+
if got != test.expected {
172+
t.Errorf("got %x; want %x", got, test.expected)
125173
}
126174
})
127175
}

pkg/pdfcpu/types/utf16.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ func EscapeUTF16String(s string) (*string, error) {
127127

128128
// StringLiteralToString returns the best possible string rep for a string literal.
129129
func StringLiteralToString(sl StringLiteral) (string, error) {
130-
bb, err := Unescape(sl.Value(), false)
130+
bb, err := Unescape(sl.Value())
131131
if err != nil {
132132
return "", err
133133
}

pkg/pdfcpu/writeImage.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ func colorLookupTable(xRefTable *model.XRefTable, o types.Object) ([]byte, error
127127
switch o := o.(type) {
128128

129129
case types.StringLiteral:
130-
return types.Unescape(o.Value(), false)
130+
return types.Unescape(o.Value())
131131

132132
case types.HexLiteral:
133133
return o.Bytes()

0 commit comments

Comments
 (0)