-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathstream_reader.go
96 lines (86 loc) · 2.29 KB
/
stream_reader.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
package ole2
import (
"io"
"log"
)
var DEBUG = false
type StreamReader struct {
sat []uint32
start uint32
reader io.ReadSeeker
offset_of_sector uint32
offset_in_sector uint32
size_sector uint32
size int64
offset int64
sector_pos func(uint32, uint32) uint32
}
func (r *StreamReader) Read(p []byte) (n int, err error) {
if r.offset_of_sector == ENDOFCHAIN {
return 0, io.EOF
}
pos := r.sector_pos(r.offset_of_sector, r.size_sector) + r.offset_in_sector
r.reader.Seek(int64(pos), 0)
readed := uint32(0)
for remainLen := uint32(len(p)) - readed; remainLen > r.size_sector-r.offset_in_sector; remainLen = uint32(len(p)) - readed {
if n, err := r.reader.Read(p[readed : readed+r.size_sector-r.offset_in_sector]); err != nil {
return int(readed) + n, err
} else {
readed += uint32(n)
r.offset_in_sector = 0
if r.offset_of_sector >= uint32(len(r.sat)) {
log.Fatal(`
THIS SHOULD NOT HAPPEN, IF YOUR PROGRAM BREAK,
COMMENT THIS LINE TO CONTINUE AND MAIL ME XLS FILE
TO TEST, THANKS`)
return int(readed), io.EOF
} else {
r.offset_of_sector = r.sat[r.offset_of_sector]
}
if r.offset_of_sector == ENDOFCHAIN {
return int(readed), io.EOF
}
pos := r.sector_pos(r.offset_of_sector, r.size_sector) + r.offset_in_sector
r.reader.Seek(int64(pos), 0)
}
}
if n, err := r.reader.Read(p[readed:len(p)]); err == nil {
r.offset_in_sector += uint32(n)
if DEBUG {
log.Printf("pos:%x,bit:% X", r.offset_of_sector, p)
}
return len(p), nil
} else {
return int(readed) + n, err
}
}
func (r *StreamReader) Seek(offset int64, whence int) (offset_result int64, err error) {
if whence == 0 {
r.offset_of_sector = r.start
r.offset_in_sector = 0
r.offset = offset
} else {
r.offset += offset
}
if r.offset_of_sector == ENDOFCHAIN {
return r.offset, io.EOF
}
for offset >= int64(r.size_sector-r.offset_in_sector) {
r.offset_of_sector = r.sat[r.offset_of_sector]
offset -= int64(r.size_sector - r.offset_in_sector)
r.offset_in_sector = 0
if r.offset_of_sector == ENDOFCHAIN {
err = io.EOF
goto return_res
}
}
if r.size <= r.offset {
err = io.EOF
r.offset = r.size
} else {
r.offset_in_sector += uint32(offset)
}
return_res:
offset_result = r.offset
return
}