/
cgf_access.go
523 lines (367 loc) · 10.5 KB
/
cgf_access.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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
//package main
package cgf
import "fmt"
//import "./dlug"
import "github.com/abeconnelly/dlug"
func CGFMagic(cgf_bytes []byte) uint64 {
magic := byte2uint64(cgf_bytes[0:8])
return magic
}
func CGFVersion(cgf_bytes []byte) string {
s,_ := byte2string(cgf_bytes[8:])
return s
}
func CGFLibraryVersion(cgf_bytes []byte) string {
n:=8
_,dn := byte2string(cgf_bytes[n:])
n+=dn
lv,dn := byte2string(cgf_bytes[n:])
return lv
}
func CGFPathCount(cgf_bytes []byte) int {
return 0
}
func CGFTilemapBytes(cgf_bytes []byte) ([]byte, error) {
cgf := CGF{}
CGFFillHeader(&cgf, cgf_bytes)
// if e!=nil { return e }
return cgf.TileMap, nil
}
func CGFPathBytes(cgf_bytes []byte, path int) ([]byte, error) {
cgf := CGF{}
CGFFillHeader(&cgf, cgf_bytes)
if path>=len(cgf.PathOffset) { return nil, fmt.Errorf("path does not exist in CGF") }
be := cgf.PathOffset[path] + cgf.PathByteOffset
en := cgf.PathOffset[path+1] + cgf.PathByteOffset
return cgf_bytes[be:en], nil
}
type PathOverflowStruct struct {
Length uint64
Stride uint64
Offset []uint64
Position []uint64
Map []byte
IntMap []int
}
func debug_print_path_overflow(po *PathOverflowStruct) {
fmt.Printf("Length: %d\n", po.Length)
fmt.Printf("Stride: %d\n", po.Stride)
fmt.Printf("Offset: %v\n", po.Offset)
fmt.Printf("Position: %v\n", po.Position)
fmt.Printf("Map: %v\n", po.Map)
fmt.Printf("IntMap: %v\n", po.IntMap)
}
func GetPathOverflow(path_bytes []byte) PathOverflowStruct {
st:=0
name,dn := byte2string(path_bytes[st:]) ; _ = name
st+=dn
vec_len := byte2uint64(path_bytes[st:st+8])
st+=8
st+=8*int(vec_len)
po := PathOverflowStruct{}
po.Length = byte2uint64(path_bytes[st:st+8])
st+=8
po.Stride = byte2uint64(path_bytes[st:st+8])
st+=8
ofs_len := (po.Length + po.Stride - 1) / po.Stride
fmt.Printf("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! %d\n", ofs_len)
if (po.Length % po.Stride) != 0 { ofs_len ++ }
fmt.Printf("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! %d\n", ofs_len)
po.Offset = make([]uint64, ofs_len)
for i:=(0); i<len(po.Offset); i++ {
po.Offset[i] = byte2uint64(path_bytes[st:st+8])
st+=8
}
pos_len := (po.Length + po.Stride - 1) / po.Stride
po.Position = make([]uint64, pos_len)
for i:=(0); i<len(po.Position); i++ {
po.Position[i] = byte2uint64(path_bytes[st:st+8])
st+=8
}
start_map := st
po.IntMap = make([]int, po.Length)
for i:=uint64(0); i<po.Length; i++ {
u,dn := dlug.ConvertUint64(path_bytes[st:])
st+=dn
po.IntMap[i] = int(u)
}
po.Map = path_bytes[start_map:st]
return po
}
func PathOverflowBytes(path_bytes []byte, path int) ([]byte, error) {
return nil,nil
}
func CGFVectorUint64(cgf_bytes []byte, path int) ([]uint64, error) {
path_bytes,e := CGFPathBytes(cgf_bytes, path)
if e!=nil { return nil, e }
fmt.Printf(">>>>>> %x %x %x\n", path_bytes[0], path_bytes[1], path_bytes[2])
st:=0
path_name,dn := byte2string(path_bytes[st:])
st+=dn
fmt.Printf(">>> %s\n", path_name)
vec_len := byte2uint64(path_bytes[st:])
st+=8
vec := make([]uint64, vec_len)
for i:=0; i<int(vec_len); i++ {
vec[i] = byte2uint64(path_bytes[st+i*8:st+(i+1)*8])
}
fmt.Printf(">>> veclen %d\n", vec_len)
return vec, nil
}
func PathOverflowOffsetUint64(path_bytes []byte) ([]uint64) {
st:=0
n_rec := byte2uint64(path_bytes[st:st+8]) ; _ = n_rec
st+=8
stride := byte2uint64(path_bytes[st:st+8]) ; _ = stride
st+=8
offset := make([]uint64, n_rec) ; _ = offset
for i:=uint64(0); i<n_rec; i++ {
offset[i] = byte2uint64(path_bytes[st:st+8])
st+=8
}
return offset
}
func PathOverflowPositionUint64(path_bytes []byte) ([]uint64) {
st:=0
n_rec := byte2uint64(path_bytes[st:st+8]) ; _ = n_rec
st+=8
stride := byte2uint64(path_bytes[st:st+8]) ; _ = stride
st+=8
offset := make([]uint64, n_rec) ; _ = offset
for i:=uint64(0); i<n_rec; i++ {
offset[i] = byte2uint64(path_bytes[st:st+8])
st+=8
}
position := make([]uint64, n_rec) ; _ = position
for i:=uint64(0); i<n_rec; i++ {
position[i] = byte2uint64(path_bytes[st:st+8])
st+=8
}
return position
}
// Count number of actual overflow entries. This is needed
// for the overflow tables.
//
// Entries that appear in the local hexit cache won't be counted
// unless they're greater than 0xd.
//
func CountOverflowVectorUint64(vec []uint64, start_step, end_step int) int {
dn_step := end_step - start_step ; _ = dn_step
ovf_count := 0
hexit_pos := uint(0)
vec_pos := start_step/32
vec_ofs := start_step%32
// update hexit_pos to skip over
// hexits before start_step.
//
if vec_ofs != 0 {
for i:=(0); i<vec_ofs; i++ {
if (vec[vec_pos] & (1<<(32+uint(i)))) == 0 { continue }
if hexit_pos >= 8 { continue }
hexit := vec[vec_pos] & (0xf<<(4*hexit_pos))
hexit_pos++
if hexit == 0 { continue }
if hexit < 0xd { continue }
if hexit == 0xe { //?????
continue
}
}
}
cur_step := start_step
for cur_step < end_step {
for i:=uint8(vec_ofs); i<32; i++ {
if (vec[vec_pos] & (1<<(32+i))) != 0 {
if hexit_pos < 8 {
hexit := (vec[vec_pos] & (0xf<<(4*hexit_pos))) >> (4*hexit_pos)
hexit_pos++
if hexit >= 0xd {
ovf_count++
}
} else {
ovf_count++
}
}
cur_step++
if cur_step >= end_step { return ovf_count }
}
vec_pos++
vec_ofs = 0
hexit_pos=0
}
return ovf_count
}
func CountOverflow(cgf_bytes []byte, path, start_step, end_step int) (int,error) {
vec,e := CGFVectorUint64(cgf_bytes, path)
if e!=nil { return -1,e }
return CountOverflowVectorUint64(vec, start_step, end_step), nil
dn_step := end_step - start_step ; _ = dn_step
ovf_count := 0
hexit_pos := uint(0)
vec_pos := start_step/32
vec_ofs := start_step%32
// update hexit_pos to skip over
// hexits before start_step.
//
if vec_ofs != 0 {
for i:=(0); i<vec_ofs; i++ {
if (vec[vec_pos] & (1<<(32+uint(i)))) == 0 { continue }
if hexit_pos >= 8 { continue }
hexit := vec[vec_pos] & (0xf<<(4*hexit_pos))
hexit_pos++
if hexit == 0 { continue }
if hexit < 0xd { continue }
if hexit == 0xe { //?????
continue
}
}
}
cur_step := start_step
for cur_step < end_step {
for i:=uint8(vec_ofs); i<32; i++ {
if (vec[vec_pos] & (1<<(32+i))) != 0 {
if hexit_pos < 8 {
hexit := (vec[vec_pos] & (0xf<<(4*hexit_pos))) >> (4*hexit_pos)
hexit_pos++
if hexit >= 0xd {
ovf_count++
}
} else {
ovf_count++
}
}
cur_step++
if cur_step >= end_step { return ovf_count, nil }
}
vec_pos++
vec_ofs = 0
hexit_pos=0
}
return ovf_count, nil
}
// start_step is inclusive, end step is exclusive
//
func CountOverflow_old(cgf_bytes []byte, path, start_step, step int) (int,error) {
vec,e := CGFVectorUint64(cgf_bytes, path)
if e!=nil { return -1,e }
dn_step := step - start_step ; _ = dn_step
ovf_count := 0
hexit_pos := uint(0)
vec_pos := start_step/32
vec_ofs := start_step%32
if vec_ofs != 0 {
for i:=(0); i<vec_ofs; i++ {
if (vec[vec_pos] & (1<<(32+uint(i)))) == 0 { continue }
if hexit_pos >= 8 { continue }
hexit := vec[vec_pos] & (0xf<<(4*hexit_pos))
hexit_pos++
if hexit == 0 { continue }
if hexit < 0xd { continue }
if hexit == 0xe { //?????
continue
}
}
}
cur_step := start_step
for cur_step < step {
for i:=uint8(vec_ofs); i<32; i++ {
if (vec[vec_pos] & (1<<(32+i))) != 0 {
if hexit_pos < 8 {
hexit := vec[vec_pos] & (0xf<<(4*hexit_pos))
hexit_pos++
if hexit >= 0xd { ovf_count++ }
} else {
ovf_count++
}
}
cur_step++
if cur_step >= step { return ovf_count, nil }
}
vec_pos++
vec_ofs = 0
hexit_pos=0
}
return ovf_count, nil
}
func LookupTileMap(cgf_bytes []byte, path, ver, step int) (int, error) {
path_bytes,e := CGFPathBytes(cgf_bytes, path)
if e!=nil { return -1, e }
st:=0
path_name,dn := byte2string(path_bytes[st:]) ; _ = path_name
st+=dn
vec_len := byte2uint64(path_bytes[st:])
st+=8
vec_entry := step/32
vecbits := byte2uint64(path_bytes[st + vec_entry*8:st + (vec_entry+1)*8])
st+=int(vec_len)*8
m := uint8(step%32)
//DEBUG
fmt.Printf(">>>>>> step %d (%d)\n", step, m)
// Canonical, return 0
//
if (vecbits&(1<<(32+m))) == 0 { return 0,nil }
//DEBUG
fmt.Printf(">>>>>> vecbits %8x\n", vecbits)
// See if the value is stored in the hexit
//
hexit_shift := uint(0)
for i:=uint8(0); i<m; i++ {
if (vecbits & (1<<(32+i))) != 0 { hexit_shift++ }
}
//DEBUG
fmt.Printf(">>>>>> hexitpos %d\n", hexit_shift)
// Return with the hexit if we can.
//
loq_flag := false ; _ = loq_flag
if hexit_shift < (32/8) {
hexit := (vecbits&(0xf<<(4*hexit_shift))) >> (4*hexit_shift)
if hexit == 0 { return -2,nil }
if hexit < 0xd { return int(hexit),nil }
if hexit == 0xe { loq_flag = true }
}
// Find it in the Overflow bytes
//
po := GetPathOverflow(path_bytes)
debug_print_path_overflow(&po)
start_idx := 0
start_step := po.Position[0]
for i:=1; i<len(po.Position); i++ {
if step >= int(po.Position[i-1]) { break }
start_idx = i
start_step = po.Position[i]
}
fmt.Printf(">>> start_idx %d, start_step %d\n", start_idx, start_step)
n_ovf,e := CountOverflow(cgf_bytes, path, int(start_step), step) ; _ = n_ovf
if e!=nil { return -1, nil }
n_ovf++
fmt.Printf(">> n_ovf %d\n", n_ovf)
tme:=0
seen_rec := 0
byte_offset := po.Offset[start_idx]
for p:=int(byte_offset); (p<len(po.Map)) && (seen_rec<n_ovf); seen_rec++ {
u,dn := dlug.ConvertUint64(po.Map[p:])
p+=dn
fmt.Printf("????? %d\n", u)
tme = int(u)
}
fmt.Printf(">>>> seen_rec %d, n_ovf %d\n", seen_rec, n_ovf)
fmt.Printf(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> tme %d\n", tme)
return tme, nil
return -1,nil
}
func LookupTileMapEntry(cgf_bytes []byte, path, ver, step int) (TileMapEntry, error) {
tme := TileMapEntry{}
tm,e := LookupTileMap(cgf_bytes, path, ver, step)
if e!=nil { return tme, e }
tme.TileMap = tm
if tm>=0 {
tilemap_bytes,e := CGFTilemapBytes(cgf_bytes)
if e!=nil { return tme, e }
//tile_map := unpack_tilemap(tilemap_bytes)
tile_map := UnpackTileMap(tilemap_bytes)
tme := tile_map[tm]
tme.Variant = tme.Variant
tme.Span = tme.Span
return tme,nil
}
return tme,fmt.Errorf("could not find entry")
}