-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
351 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package bra | ||
|
||
import ( | ||
"encoding/binary" | ||
"io" | ||
) | ||
|
||
const armAlignment = 4 | ||
|
||
type arm struct { | ||
ip uint32 | ||
} | ||
|
||
func (c *arm) Size() int { return armAlignment } | ||
|
||
func (c *arm) Convert(b []byte, encoding bool) int { | ||
if len(b) < c.Size() { | ||
return 0 | ||
} | ||
|
||
if c.ip == 0 { | ||
c.ip += armAlignment | ||
} | ||
|
||
var i int | ||
|
||
for i = 0; i < len(b) & ^(armAlignment-1); i += armAlignment { | ||
v := binary.LittleEndian.Uint32(b[i:]) | ||
|
||
c.ip += uint32(armAlignment) | ||
|
||
if b[i+3] == 0xeb { | ||
v <<= 2 | ||
|
||
if encoding { | ||
v += c.ip | ||
} else { | ||
v -= c.ip | ||
} | ||
|
||
v >>= 2 | ||
v &= 0x00ffffff | ||
v |= 0xeb000000 | ||
} | ||
|
||
binary.LittleEndian.PutUint32(b[i:], v) | ||
} | ||
|
||
return i | ||
} | ||
|
||
// NewARMReader returns a new ARM io.ReadCloser. | ||
func NewARMReader(_ []byte, _ uint64, readers []io.ReadCloser) (io.ReadCloser, error) { | ||
return newReader(readers, new(arm)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package bra | ||
|
||
import ( | ||
"encoding/binary" | ||
"io" | ||
) | ||
|
||
const bcjLookAhead = 4 | ||
|
||
type bcj struct { | ||
ip, state uint32 | ||
} | ||
|
||
func (c *bcj) Size() int { return bcjLookAhead + 1 } | ||
|
||
func test86MSByte(b byte) bool { | ||
return (b+1)&0xfe == 0 | ||
} | ||
|
||
//nolint:cyclop,funlen,gocognit | ||
func (c *bcj) Convert(b []byte, encoding bool) int { | ||
if len(b) < c.Size() { | ||
return 0 | ||
} | ||
|
||
var ( | ||
pos int | ||
mask = c.state & 7 | ||
) | ||
|
||
for { | ||
p := pos | ||
for ; p < len(b)-bcjLookAhead; p++ { | ||
if b[p]&0xfe == 0xe8 { | ||
break | ||
} | ||
} | ||
|
||
d := p - pos | ||
pos = p | ||
|
||
if p >= len(b)-bcjLookAhead { | ||
if d > 2 { | ||
c.state = 0 | ||
} else { | ||
c.state = mask >> d | ||
} | ||
|
||
c.ip += uint32(pos) | ||
|
||
return pos | ||
} | ||
|
||
if d > 2 { | ||
mask = 0 | ||
} else { | ||
mask >>= d | ||
if mask != 0 && (mask > 4 || mask == 3 || test86MSByte(b[p+int(mask>>1)+1])) { | ||
mask = (mask >> 1) | 4 | ||
pos++ | ||
|
||
continue | ||
} | ||
} | ||
|
||
//nolint:nestif | ||
if test86MSByte(b[p+4]) { | ||
v := binary.LittleEndian.Uint32(b[p+1:]) | ||
cur := c.ip + uint32(c.Size()+pos) | ||
pos += c.Size() | ||
|
||
if encoding { | ||
v += cur | ||
} else { | ||
v -= cur | ||
} | ||
|
||
if mask != 0 { | ||
sh := mask & 6 << 2 | ||
if test86MSByte(byte(v >> sh)) { | ||
v ^= (uint32(0x100) << sh) - 1 | ||
if encoding { | ||
v += cur | ||
} else { | ||
v -= cur | ||
} | ||
} | ||
|
||
mask = 0 | ||
} | ||
|
||
binary.LittleEndian.PutUint32(b[p+1:], v) | ||
b[p+4] = 0 - b[p+4]&1 | ||
} else { | ||
mask = (mask >> 1) | 4 | ||
pos++ | ||
} | ||
} | ||
} | ||
|
||
// NewBCJReader returns a new BCJ io.ReadCloser. | ||
func NewBCJReader(_ []byte, _ uint64, readers []io.ReadCloser) (io.ReadCloser, error) { | ||
return newReader(readers, new(bcj)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package bra | ||
|
||
type converter interface { | ||
Size() int | ||
Convert([]byte, bool) int | ||
} | ||
|
||
func max(x, y int) int { | ||
if x > y { | ||
return x | ||
} | ||
|
||
return y | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package bra | ||
|
||
import ( | ||
"encoding/binary" | ||
"io" | ||
) | ||
|
||
const ppcAlignment = 4 | ||
|
||
type ppc struct { | ||
ip uint32 | ||
} | ||
|
||
func (c *ppc) Size() int { return ppcAlignment } | ||
|
||
func (c *ppc) Convert(b []byte, encoding bool) int { | ||
if len(b) < c.Size() { | ||
return 0 | ||
} | ||
|
||
var i int | ||
|
||
for i = 0; i < len(b) & ^(ppcAlignment-1); i += ppcAlignment { | ||
v := binary.BigEndian.Uint32(b[i:]) | ||
|
||
if b[i+0]&0xfc == 0x48 && b[i+3]&3 == 1 { | ||
if encoding { | ||
v += c.ip | ||
} else { | ||
v -= c.ip | ||
} | ||
|
||
v &= 0x03ffffff | ||
v |= 0x48000000 | ||
} | ||
|
||
c.ip += uint32(ppcAlignment) | ||
|
||
binary.BigEndian.PutUint32(b[i:], v) | ||
} | ||
|
||
return i | ||
} | ||
|
||
// NewPPCReader returns a new PPC io.ReadCloser. | ||
func NewPPCReader(_ []byte, _ uint64, readers []io.ReadCloser) (io.ReadCloser, error) { | ||
return newReader(readers, new(ppc)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package bra | ||
|
||
import ( | ||
"bytes" | ||
"errors" | ||
"io" | ||
) | ||
|
||
type readCloser struct { | ||
rc io.ReadCloser | ||
buf bytes.Buffer | ||
conv converter | ||
} | ||
|
||
func (rc *readCloser) Close() (err error) { | ||
if rc.rc != nil { | ||
err = rc.rc.Close() | ||
rc.rc = nil | ||
} | ||
|
||
return | ||
} | ||
|
||
func (rc *readCloser) Read(p []byte) (int, error) { | ||
if rc.rc == nil { | ||
return 0, errors.New("bra: Read after Close") | ||
} | ||
|
||
if _, err := io.CopyN(&rc.buf, rc.rc, int64(max(len(p), rc.conv.Size())-rc.buf.Len())); err != nil { | ||
if !errors.Is(err, io.EOF) { | ||
return 0, err | ||
} | ||
} | ||
|
||
if n := rc.conv.Convert(rc.buf.Bytes(), false); n > 0 { | ||
return rc.buf.Read(p[:n]) | ||
} | ||
|
||
return rc.buf.Read(p) | ||
} | ||
|
||
func newReader(readers []io.ReadCloser, conv converter) (io.ReadCloser, error) { | ||
if len(readers) != 1 { | ||
return nil, errors.New("bra: need exactly one reader") | ||
} | ||
|
||
return &readCloser{ | ||
rc: readers[0], | ||
conv: conv, | ||
}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package bra | ||
|
||
import ( | ||
"encoding/binary" | ||
"io" | ||
) | ||
|
||
const sparcAlignment = 4 | ||
|
||
type sparc struct { | ||
ip uint32 | ||
} | ||
|
||
func (c *sparc) Size() int { return sparcAlignment } | ||
|
||
func (c *sparc) Convert(b []byte, encoding bool) int { | ||
if len(b) < c.Size() { | ||
return 0 | ||
} | ||
|
||
var i int | ||
|
||
for i = 0; i < len(b) & ^(sparcAlignment-1); i += sparcAlignment { | ||
v := binary.BigEndian.Uint32(b[i:]) | ||
|
||
if (b[i+0] == 0x40 && b[i+1]&0xc0 == 0) || (b[i+0] == 0x7f && b[i+1] >= 0xc0) { | ||
v <<= 2 | ||
|
||
if encoding { | ||
v += c.ip | ||
} else { | ||
v -= c.ip | ||
} | ||
|
||
v &= 0x01ffffff | ||
v -= uint32(1) << 24 | ||
v ^= 0xff000000 | ||
v >>= 2 | ||
v |= 0x40000000 | ||
} | ||
|
||
c.ip += uint32(sparcAlignment) | ||
|
||
binary.BigEndian.PutUint32(b[i:], v) | ||
} | ||
|
||
return i | ||
} | ||
|
||
// NewSPARCReader returns a new SPARC io.ReadCloser. | ||
func NewSPARCReader(_ []byte, _ uint64, readers []io.ReadCloser) (io.ReadCloser, error) { | ||
return newReader(readers, new(sparc)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.