Skip to content

Commit 9ed161f

Browse files
committed
Fix for #99. Also removed some code redundant now.
1 parent 7be2036 commit 9ed161f

File tree

7 files changed

+52
-23
lines changed

7 files changed

+52
-23
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ test/*.pdf
3131
test/*.res
3232
test/pvt
3333
test/PDFTest*
34+
test/pdftest*
3435
file.txt
3536
data/fonts/Arial.afm
3637
test/sample01.pem

Project.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ julia = "1.6"
3131
[extras]
3232
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
3333
ZipFile = "a5390f91-8eb1-5f08-bee0-b1d1ffed6cea"
34+
Downloads = "f43a241f-c20a-4ad4-852c-f6b1247861c6"
3435

3536
[targets]
36-
test = ["Test", "ZipFile"]
37+
test = ["Test", "ZipFile", "Downloads"]

src/Inflate.jl

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,7 @@ const Z_MEM_ERROR = -4
4545
const Z_BUF_ERROR = -5
4646
const Z_VERSION_ERROR = -6
4747

48-
@static if Base.VERSION > v"1.3-"
49-
using Zlib_jll: libz
50-
else
51-
isfile(joinpath(dirname(@__FILE__),"..","deps","deps.jl")) ||
52-
error("PDFIO not properly installed. Please run Pkg.build(\"PDFIO\")")
53-
include("../deps/deps.jl")
54-
end
48+
using Zlib_jll: libz
5549

5650
_zlibVersion() = ccall((:zlibVersion, libz), Ptr{Cstring}, ())
5751

src/LibCrypto.jl

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
1-
@static if Base.VERSION > v"1.3-"
2-
using OpenSSL_jll: libcrypto
3-
else
4-
isfile(joinpath(dirname(@__FILE__), "..", "deps", "deps.jl")) ||
5-
error("PDFIO not properly installed. Please run Pkg.build(\"PDFIO\")")
6-
include("../deps/deps.jl")
7-
end
1+
using OpenSSL_jll: libcrypto
82

93
using Base: SecretBuffer, SecretBuffer!
104
import Base: copy

src/PDFonts.jl

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ cmap_command(b::Vector{UInt8}) =
335335
nothing : Symbol(String(b))
336336

337337
function on_cmap_command!(stm::IO, command::Symbol,
338-
params::Vector{CosInt}, cmap::CMap)
338+
params::Vector{CosInt}, cmap::CMap)
339339
n = get(pop!(params))
340340
o1, o2, o3 = CosNull, CosNull, CosNull
341341
for i = 1:n
@@ -358,12 +358,30 @@ function on_cmap_command!(stm::IO, command::Symbol,
358358
end
359359
else
360360
l = length(d1)
361+
@assert (d1[1] <= d2[1]) E_INVALID_CODESPACERANGE
361362
if l == 1
362363
cmap.code_space[Interval(d1[1], d2[1])] = CosNull
363364
else
364-
imap = IntervalTree{UInt8, CosNullType}()
365-
imap[Interval(d1[2], d2[2])] = CosNull
366-
cmap.code_space[Interval(d1[1], d2[1])] = imap
365+
if d1[2] < d2[2]
366+
imap = IntervalTree{UInt8, CosNullType}()
367+
imap[Interval(d1[2], d2[2])] = CosNull
368+
cmap.code_space[Interval(d1[1], d2[1])] = imap
369+
else
370+
@warn "Corrupt CMap file. Repairing... Some encodings may not map properly."
371+
imap = IntervalTree{UInt8, CosNullType}()
372+
imap[Interval(d1[2], 0xff)] = CosNull
373+
cmap.code_space[Interval(d1[1], d1[1])] = imap
374+
375+
imap = IntervalTree{UInt8, CosNullType}()
376+
imap[Interval(0x00, d2[2])] = CosNull
377+
cmap.code_space[Interval(d2[1], d2[1])] = imap
378+
379+
if d2[1] - d1[1] > 1
380+
imap = IntervalTree{UInt8, CosNullType}()
381+
imap[Interval(0x00, 0xff)] = CosNull
382+
cmap.code_space[Interval(d1[1]+1, d2[1]-1)] = imap
383+
end
384+
end
367385
end
368386
end
369387
end

src/errors.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ export E_EXPECTED_EOF, E_UNEXPECTED_EOF, E_UNEXPECTED_CHAR, E_BAD_KEY,
22
E_BAD_ESCAPE, E_BAD_CONTROL, E_LEADING_ZERO, E_BAD_NUMBER, E_BAD_HEADER,
33
E_BAD_TRAILER, E_BAD_TYPE, E_NOT_IMPLEMENTED,
44
E_INVALID_OBJECT, E_INVALID_PAGE_NUMBER, E_INVALID_PAGE_LABEL,
5-
E_NOT_TAGGED_PDF, E_INVALID_PASSWORD
5+
E_NOT_TAGGED_PDF, E_INVALID_PASSWORD, E_INVALID_CODESPACERANGE
66

77
# The following errors may be thrown by the reader
88
const E_EXPECTED_EOF = "Expected end of input"
@@ -28,3 +28,4 @@ const E_INVALID_PASSWORD = "The password supplied to open the document is inv
2828
const E_INVALID_CRYPT = "The crypt handler is not supported"
2929
const E_NOT_TAGGED_PDF = "PDF file is not tagged"
3030
const E_NOT_IMPLEMENTED = "Not Implemented"
31+
const E_INVALID_CODESPACERANGE = "Invalid code space range in CMap"

test/runtests.jl

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,16 @@ using PDFIO.Cos
55
using PDFIO.Common
66
using ZipFile
77
using AbstractTrees
8+
using Downloads
89

910
# Internal methods for testing only
1011
using PDFIO.Cos: parse_indirect_ref, decode_ascii85, CosXString, parse_value
11-
using PDFIO.PD: openssl_error
12+
using PDFIO.PD: openssl_error, read_cmap, get_encoded_string
1213
using PDFIO.Common: read_pkcs12
1314

1415
include("debugIO.jl")
1516

16-
pdftest_ver = "0.0.9"
17+
pdftest_ver = "0.0.10"
1718
pdftest_link = "https://github.com/sambitdash/PDFTest/archive/v"*pdftest_ver
1819

1920
zipfile = "pdftest-"*pdftest_ver
@@ -22,7 +23,7 @@ zipfile *= ".zip"
2223
pdftest_dir="PDFTest-"*pdftest_ver*"/"
2324

2425
if !isdir(pdftest_dir)
25-
isfile(zipfile) || download(pdftest_link, zipfile)
26+
isfile(zipfile) || Downloads.download(pdftest_link, zipfile)
2627
r = ZipFile.Reader(zipfile)
2728
buf = Vector{UInt8}(undef, 64*1024)
2829
for f in r.files
@@ -57,6 +58,8 @@ function local_testfiles(filename, filesdir="files")
5758
joinpath(@__DIR__, pdftest_dir, filesdir, filename))
5859
end
5960

61+
local_files(filename, filesdir="files") = joinpath(@__DIR__, pdftest_dir, filesdir, filename)
62+
6063
@testset "PDFIO tests" begin
6164
@testset "Miscellaneous" begin
6265
@test_throws ErrorException skipv(IOBuffer([UInt8(65), UInt8(66)]),
@@ -416,6 +419,23 @@ end
416419
@test files_equal(resfile, template)
417420
length(utilPrintOpenFiles()) == 0
418421
end
422+
423+
@test begin
424+
filename="bad.cmap"
425+
DEBUG && println(filename)
426+
path = local_files(filename)
427+
io = util_open(path, "r")
428+
cmap = nothing
429+
try
430+
cmap = read_cmap(io)
431+
finally
432+
util_close(io)
433+
end
434+
@test cmap !== nothing
435+
@test all(get_encoded_string(UInt8[0x00, 0xff], cmap).== [Char(0x0111)])
436+
@test all(get_encoded_string(UInt8[0x01, 0x08], cmap).== [Char(0x0110)])
437+
all(get_encoded_string(UInt8[0x00, 0xfb, 0x00, 0xfe], cmap) .== [Char(0x0106), Char(0x010D)])
438+
end
419439
end
420440

421441
@testset "Corrupt File" begin

0 commit comments

Comments
 (0)