-
Notifications
You must be signed in to change notification settings - Fork 0
/
merge.go
328 lines (234 loc) · 7.67 KB
/
merge.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/*
Copyright 2018 The pdfcpu Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package pdflite
import "fmt"
func patchIndRef(ir *IndirectRef, lookup map[int]int) {
i := ir.ObjectNumber.Value()
ir.ObjectNumber = Integer(lookup[i])
}
func patchObject(o Object, lookup map[int]int) Object {
fmt.Printf("patchObject before: %v\n", o)
var ob Object
switch obj := o.(type) {
case IndirectRef:
patchIndRef(&obj, lookup)
ob = obj
case Dict:
patchDict(obj, lookup)
ob = obj
case StreamDict:
patchDict(obj.Dict, lookup)
ob = obj
case ObjectStreamDict:
patchDict(obj.Dict, lookup)
ob = obj
case XRefStreamDict:
patchDict(obj.Dict, lookup)
ob = obj
case Array:
patchArray(obj, lookup)
ob = obj
}
fmt.Printf("patchObject end: %v\n", ob)
return ob
}
func patchDict(d Dict, lookup map[int]int) {
fmt.Printf("patchDict before: %v\n", d)
for k, obj := range d {
o := patchObject(obj, lookup)
if o != nil {
d[k] = o
}
}
fmt.Printf("patchDict after: %v\n", d)
}
func patchArray(a Array, lookup map[int]int) {
fmt.Printf("patchArray begin: %v\n", a)
for i, obj := range a {
o := patchObject(obj, lookup)
if o != nil {
a[i] = o
}
}
fmt.Printf("patchArray end: %v\n", a)
}
func objNrsIntSet(ctx *Context) IntSet {
objNrs := IntSet{}
for k := range ctx.Table {
if k == 0 {
// obj#0 is always the head of the freelist.
continue
}
objNrs[k] = true
}
return objNrs
}
func lookupTable(keys IntSet, i int) map[int]int {
m := map[int]int{}
for k := range keys {
m[k] = i
i++
}
return m
}
// Patch an IntSet of objNrs using lookup.
func patchObjects(s IntSet, lookup map[int]int) IntSet {
t := IntSet{}
for k, v := range s {
if v {
t[lookup[k]] = v
}
}
return t
}
func patchSourceObjectNumbers(ctxSource, ctxDest *Context) {
fmt.Printf("patchSourceObjectNumbers: ctxSource: xRefTableSize:%d trailer.Size:%d - %s\n", len(ctxSource.Table), *ctxSource.Size, ctxSource.Read.FileName)
fmt.Printf("patchSourceObjectNumbers: ctxDest: xRefTableSize:%d trailer.Size:%d - %s\n", len(ctxDest.Table), *ctxDest.Size, ctxDest.Read.FileName)
// Patch source xref tables obj numbers which are essentially the keys.
//logInfoMerge.Printf("Source XRefTable before:\n%s\n", ctxSource)
objNrs := objNrsIntSet(ctxSource)
// Create lookup table for object numbers.
// The first number is the successor of the last number in ctxDest.
lookup := lookupTable(objNrs, *ctxDest.Size)
// Patch pointer to root object
patchIndRef(ctxSource.Root, lookup)
// Patch pointer to info object
if ctxSource.Info != nil {
patchIndRef(ctxSource.Info, lookup)
}
// Patch free object zero
entry := ctxSource.Table[0]
off := int(*entry.Offset)
if off != 0 {
i := int64(lookup[off])
entry.Offset = &i
}
// Patch all indRefs for xref table entries.
for k := range objNrs {
//logDebugMerge.Printf("patching obj #%d\n", k)
entry := ctxSource.Table[k]
if entry.Free {
fmt.Printf("patch free entry: old offset:%d\n", *entry.Offset)
off := int(*entry.Offset)
if off == 0 {
continue
}
i := int64(lookup[off])
entry.Offset = &i
fmt.Printf("patch free entry: new offset:%d\n", *entry.Offset)
continue
}
patchObject(entry.Object, lookup)
}
// Patch xref entry object numbers.
m := make(map[int]*XRefTableEntry, *ctxSource.Size)
for k, v := range lookup {
m[v] = ctxSource.Table[k]
}
m[0] = ctxSource.Table[0]
ctxSource.Table = m
// Patch DuplicateInfo object numbers.
ctxSource.Optimize.DuplicateInfoObjects = patchObjects(ctxSource.Optimize.DuplicateInfoObjects, lookup)
// Patch Linearization object numbers.
ctxSource.LinearizationObjs = patchObjects(ctxSource.LinearizationObjs, lookup)
// Patch XRefStream objects numbers.
ctxSource.Read.XRefStreams = patchObjects(ctxSource.Read.XRefStreams, lookup)
// Patch object stream object numbers.
ctxSource.Read.ObjectStreams = patchObjects(ctxSource.Read.ObjectStreams, lookup)
fmt.Printf("patchSourceObjectNumbers end")
}
func appendSourcePageTreeToDestPageTree(ctxSource, ctxDest *Context) error {
fmt.Println("appendSourcePageTreeToDestPageTree begin")
indRefPageTreeRootDictSource, err := ctxSource.Pages()
if err != nil {
return err
}
pageTreeRootDictSource, _ := ctxSource.XRefTable.DereferenceDict(*indRefPageTreeRootDictSource)
pageCountSource := pageTreeRootDictSource.IntEntry("Count")
indRefPageTreeRootDictDest, err := ctxDest.Pages()
if err != nil {
return err
}
pageTreeRootDictDest, _ := ctxDest.XRefTable.DereferenceDict(*indRefPageTreeRootDictDest)
pageCountDest := pageTreeRootDictDest.IntEntry("Count")
a := pageTreeRootDictDest.ArrayEntry("Kids")
fmt.Printf("Kids before: %v\n", a)
pageTreeRootDictSource.Insert("Parent", *indRefPageTreeRootDictDest)
// The source page tree gets appended on to the dest page tree.
a = append(a, *indRefPageTreeRootDictSource)
fmt.Printf("Kids after: %v\n", a)
pageTreeRootDictDest.Update("Count", Integer(*pageCountDest+*pageCountSource))
pageTreeRootDictDest.Update("Kids", a)
ctxDest.PageCount += ctxSource.PageCount
fmt.Println("appendSourcePageTreeToDestPageTree end")
return nil
}
func appendSourceObjectsToDest(ctxSource, ctxDest *Context) {
fmt.Println("appendSourceObjectsToDest begin")
for objNr, entry := range ctxSource.Table {
// Do not copy free list head.
if objNr == 0 {
continue
}
fmt.Printf("adding obj %d from src to dest\n", objNr)
ctxDest.Table[objNr] = entry
*ctxDest.Size++
}
fmt.Println("appendSourceObjectsToDest end")
}
// merge two disjunct IntSets
func mergeIntSets(src, dest IntSet) {
for k := range src {
dest[k] = true
}
}
func mergeDuplicateObjNumberIntSets(ctxSource, ctxDest *Context) {
fmt.Println("mergeDuplicateObjNumberIntSets begin")
mergeIntSets(ctxSource.Optimize.DuplicateInfoObjects, ctxDest.Optimize.DuplicateInfoObjects)
mergeIntSets(ctxSource.LinearizationObjs, ctxDest.LinearizationObjs)
mergeIntSets(ctxSource.Read.XRefStreams, ctxDest.Read.XRefStreams)
mergeIntSets(ctxSource.Read.ObjectStreams, ctxDest.Read.ObjectStreams)
fmt.Println("mergeDuplicateObjNumberIntSets end")
}
// MergeXRefTables merges Context ctxSource into ctxDest by appending its page tree.
func MergeXRefTables(ctxSource, ctxDest *Context) (err error) {
// Sweep over ctxSource cross ref table and ensure valid object numbers in ctxDest's space.
patchSourceObjectNumbers(ctxSource, ctxDest)
// Append ctxSource pageTree to ctxDest pageTree.
fmt.Println("appendSourcePageTreeToDestPageTree")
err = appendSourcePageTreeToDestPageTree(ctxSource, ctxDest)
if err != nil {
return err
}
// Append ctxSource objects to ctxDest
fmt.Println("appendSourceObjectsToDest")
appendSourceObjectsToDest(ctxSource, ctxDest)
// Mark source's root object as free.
err = ctxDest.DeleteObject(int(ctxSource.Root.ObjectNumber))
if err != nil {
return
}
// Mark source's info object as free.
// Note: Any indRefs this info object depends on are missed.
if ctxSource.Info != nil {
err = ctxDest.DeleteObject(int(ctxSource.Info.ObjectNumber))
if err != nil {
return
}
}
// Merge all IntSets containing redundant object numbers.
fmt.Println("mergeDuplicateObjNumberIntSets")
mergeDuplicateObjNumberIntSets(ctxSource, ctxDest)
fmt.Printf("Dest XRefTable after merge:\n%s\n", ctxDest)
return nil
}