FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
go-gitdiff/gitdiff/binary_reader.go at master · bluekeyes/go-gitdiff · GitHub
bluekeyes
/
go-gitdiff
Public
Notifications
You must be signed in to change notification settings
Fork
30
Star
154
Code
Issues
5
Pull requests
2
Actions
Security and quality
3
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Security and quality
Insights
Expand file tree
Breadcrumbs
go-gitdiff
/
gitdiff
/
binary_reader.go
Copy path
More file actions
More file actions
Latest commit
History
History
History
56 lines (51 loc) · 1.47 KB
Breadcrumbs
go-gitdiff
/
gitdiff
/
binary_reader.go
Copy path
File metadata and controls
56 lines (51 loc) · 1.47 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
package
gitdiff
import
(
"bytes"
"compress/zlib"
"fmt"
"io"
)
// binaryReader reads the compressed raw data in a binary fragment,
// decompressing it and checking it against the expected size. Clients should
// read until it returns an error or io.EOF.
type
binaryReader
struct
{
r
*
io.
LimitedReader
c
io.
Closer
size
int64
raw
[]
byte
}
func
newBinaryReader
(
f
*
BinaryFragment
) io.
Reader
{
return
&
binaryReader
{
raw
:
f
.
RawData
,
size
:
f
.
Size
,
}
}
func
(
r
*
binaryReader
)
Read
(
p
[]
byte
) (
int
,
error
) {
// Defer initialization of the zlib reader so that we report any header
// errors from Read() instead of when creating the binaryReader. This
// allows clients to use io.ReadAll() directly with BinaryFragment.Data().
if
r
.
r
==
nil
{
zr
,
err
:=
zlib
.
NewReader
(
bytes
.
NewReader
(
r
.
raw
))
if
err
!=
nil
{
return
0
,
err
}
r
.
r
=
&
io.
LimitedReader
{
R
:
zr
,
N
:
r
.
size
+
1
}
r
.
c
=
zr
}
n
,
err
:=
r
.
r
.
Read
(
p
)
if
err
==
io
.
EOF
{
// If we reached the "end", first check that we read the correct amount
// of data. On an exact read, the limit reader has one byte remaining.
switch
{
case
r
.
r
.
N
>
1
:
return
n
,
fmt
.
Errorf
(
"incorrect inflated size: expected %d bytes, received %d"
,
r
.
size
,
r
.
size
-
(
r
.
r
.
N
-
1
))
case
r
.
r
.
N
<
1
:
return
n
,
fmt
.
Errorf
(
"incorrect inflated size: expected %d bytes, received more"
,
r
.
size
)
}
// Then check that the zlib checksum is valid by closing the reader
if
err
:=
r
.
c
.
Close
();
err
!=
nil
{
return
n
,
err
}
}
return
n
,
err
}
Back
|
FazBrowse Home
|
New Git URL