Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #144 Fix - JBIG2 - Changed integer variables types #148

Merged
merged 7 commits into from
Aug 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 27 additions & 26 deletions internal/jbig2/decoder/arithmetic/arithmetic.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ var (
{0x5601, 46, 46, 0},
}

qeTable = []int{
qeTable = []uint32{
0x56010000, 0x34010000, 0x18010000, 0x0AC10000,
0x05210000, 0x02210000, 0x56010000, 0x54010000,
0x48010000, 0x38010000, 0x30010000, 0x24010000,
Expand All @@ -50,20 +50,20 @@ var (
0x00050000, 0x00010000, 0x56010000,
}

nmpsTable = []int{
nmpsTable = []uint32{
1, 2, 3, 4, 5, 38, 7, 8, 9, 10, 11, 12, 13, 29, 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, 45, 46,
}

nlpsTable = []int{
nlpsTable = []uint32{
1, 6, 9, 12, 29, 33, 6, 14, 14, 14, 17, 18, 20, 21, 14, 14, 15, 16, 17, 18, 19,
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, 46,
}

switchTable = []int{
switchTable = []uint32{
1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
Expand All @@ -73,25 +73,25 @@ var (
// Decoder is the arithmetic Decoder structure, used to decode the jbig2 Segments.
type Decoder struct {
// ContextSize is the current decoder context size
ContextSize []int
ReferedToContextSize []int
ContextSize []uint32
ReferedToContextSize []uint32

r reader.StreamReader
b int
b uint8
c uint64
a uint32
previous int64
ct int
prvCtr int
ct int32
prvCtr int32
streamPosition int64
}

// New creates new arithmetic Decoder.
func New(r reader.StreamReader) (*Decoder, error) {
d := &Decoder{
r: r,
ContextSize: []int{16, 13, 10, 10},
ReferedToContextSize: []int{13, 10},
ContextSize: []uint32{16, 13, 10, 10},
ReferedToContextSize: []uint32{13, 10},
}

// initialize the decoder from the reader
Expand All @@ -107,7 +107,7 @@ func (d *Decoder) DecodeBit(stats *DecoderStats) (int, error) {
var (
bit int
qeValue = qe[stats.cx()][0]
icx = int(stats.cx())
icx = int32(stats.cx())
)

defer func() {
Expand Down Expand Up @@ -138,10 +138,11 @@ func (d *Decoder) DecodeBit(stats *DecoderStats) (int, error) {
}

// DecodeInt decodes the Integer from the arithmetic Decoder for the provided DecoderStats.
func (d *Decoder) DecodeInt(stats *DecoderStats) (int, error) {
func (d *Decoder) DecodeInt(stats *DecoderStats) (int32, error) {
var (
value, bit, s, bitsToRead, offset int
err error
value, offset int32
bit, s, bitsToRead int
err error
)
if stats == nil {
stats = NewStats(512, 1)
Expand Down Expand Up @@ -222,16 +223,16 @@ func (d *Decoder) DecodeInt(stats *DecoderStats) (int, error) {
if err != nil {
return 0, err
}
value = (value << 1) | bit
value = (value << 1) | int32(bit)
}
value += offset

if s == 0 {
return int(value), nil
return value, nil
} else if s == 1 && value > 0 {
return int(-value), nil
return -value, nil
}
return math.MaxInt64, nil
return math.MaxInt32, nil
}

// DecodeIAID decodes the IAID procedure, Annex A.3.
Expand All @@ -242,7 +243,7 @@ func (d *Decoder) DecodeIAID(codeLen uint64, stats *DecoderStats) (int64, error)

// A.3 2)
for i = 0; i < codeLen; i++ {
stats.SetIndex(int(d.previous))
stats.SetIndex(int32(d.previous))
bit, err := d.DecodeBit(stats)
if err != nil {
return 0, err
Expand All @@ -264,7 +265,7 @@ func (d *Decoder) init() error {
return err
}

d.b = int(b)
d.b = b
d.c = (uint64(b) << 16)

if err = d.readByte(); err != nil {
Expand All @@ -291,7 +292,7 @@ func (d *Decoder) readByte() error {
return err
}

d.b = int(b)
d.b = b

if d.b == 0xFF {
b1, err := d.r.ReadByte()
Expand All @@ -314,7 +315,7 @@ func (d *Decoder) readByte() error {
if err != nil {
return err
}
d.b = int(b)
d.b = b

d.c += uint64(d.b) << 8
d.ct = 8
Expand Down Expand Up @@ -345,7 +346,7 @@ func (d *Decoder) renormalize() error {
}

func (d *Decoder) decodeIntBit(stats *DecoderStats) (int, error) {
stats.SetIndex(int(d.previous))
stats.SetIndex(int32(d.previous))
bit, err := d.DecodeBit(stats)
if err != nil {
common.Log.Debug("ArithmeticDecoder 'decodeIntBit'-> DecodeBit failed. %v", err)
Expand All @@ -360,7 +361,7 @@ func (d *Decoder) decodeIntBit(stats *DecoderStats) (int, error) {
return bit, nil
}

func (d *Decoder) mpsExchange(stats *DecoderStats, icx int) int {
func (d *Decoder) mpsExchange(stats *DecoderStats, icx int32) int {
mps := stats.mps[stats.index]

if d.a < qe[icx][0] {
Expand All @@ -376,7 +377,7 @@ func (d *Decoder) mpsExchange(stats *DecoderStats, icx int) int {

}

func (d *Decoder) lpsExchange(stats *DecoderStats, icx int, qeValue uint32) int {
func (d *Decoder) lpsExchange(stats *DecoderStats, icx int32, qeValue uint32) int {
mps := stats.getMps()
if d.a < qeValue {
stats.setEntry(int(qe[icx][1]))
Expand Down
10 changes: 5 additions & 5 deletions internal/jbig2/decoder/arithmetic/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ import (

// DecoderStats is the structure that contains arithmetic decode context.
type DecoderStats struct {
index int
contextSize int
index int32
contextSize int32
codingContextTable []byte
mps []byte
}

// NewStats creates new DecoderStats of size 'contextSize'.
func NewStats(contextSize int, index int) *DecoderStats {
func NewStats(contextSize int32, index int32) *DecoderStats {
return &DecoderStats{
index: index,
contextSize: contextSize,
Expand Down Expand Up @@ -59,8 +59,8 @@ func (d *DecoderStats) Reset() {
}

// SetIndex sets current decoder stats 'index'.
func (d *DecoderStats) SetIndex(index int) {
d.index = int(uint(index))
func (d *DecoderStats) SetIndex(index int32) {
d.index = index
}

// String implements Stringer interface.
Expand Down
12 changes: 6 additions & 6 deletions internal/jbig2/decoder/huffman/encoded_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func (e *EncodedTable) String() string {
func (e *EncodedTable) parseTable() error {
var (
codeTable []*Code
prefLen, rangeLen, rangeLow int
prefLen, rangeLen, rangeLow int32
temp uint64
err error
)
Expand All @@ -77,13 +77,13 @@ func (e *EncodedTable) parseTable() error {
if err != nil {
return err
}
prefLen = int(temp)
prefLen = int32(temp)

temp, err = r.ReadBits(byte(e.HtRS()))
if err != nil {
return err
}
rangeLen = int(temp)
rangeLen = int32(temp)

codeTable = append(codeTable, NewCode(prefLen, rangeLen, rangeLow, false))
curRangeLow += (1 << uint(rangeLen))
Expand All @@ -94,7 +94,7 @@ func (e *EncodedTable) parseTable() error {
if err != nil {
return err
}
prefLen = int(temp)
prefLen = int32(temp)

// Annex B.2 7)
rangeLen = 32
Expand All @@ -106,7 +106,7 @@ func (e *EncodedTable) parseTable() error {
if err != nil {
return err
}
prefLen = int(temp)
prefLen = int32(temp)

// Annex B.2 9)
rangeLen = 32
Expand All @@ -119,7 +119,7 @@ func (e *EncodedTable) parseTable() error {
if err != nil {
return err
}
prefLen = int(temp)
prefLen = int32(temp)
codeTable = append(codeTable, NewCode(prefLen, -1, -1, false))
}

Expand Down
12 changes: 6 additions & 6 deletions internal/jbig2/decoder/huffman/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type OutOfBandNode struct{}
var _ Node = &OutOfBandNode{}

// Decode implements Node interface.
// NOTE: Decodes the out of band node by returning max int64 value.
// Decodes the out of band node by returning max int64 value.
func (o *OutOfBandNode) Decode(r reader.StreamReader) (int64, error) {
return int64(math.MaxInt64), nil
}
Expand All @@ -43,8 +43,8 @@ func newOufOfBandNode(c *Code) *OutOfBandNode {

// ValueNode represents a value node in a huffman tree. It is a leaf of a tree.
type ValueNode struct {
rangeLen int
rangeLow int
rangeLen int32
rangeLow int32
isLowerRange bool
}

Expand Down Expand Up @@ -82,7 +82,7 @@ func newValueNode(c *Code) *ValueNode {
// It is defined as a pair of two child nodes 'zero' and 'one' and a 'depth' level.
// Implements Node interface.
type InternalNode struct {
depth int
depth int32
zero Node
one Node
}
Expand Down Expand Up @@ -175,12 +175,12 @@ func (i *InternalNode) append(c *Code) error {
}

func (i *InternalNode) pad(sb *strings.Builder) {
for j := 0; j < i.depth; j++ {
for j := int32(0); j < i.depth; j++ {
sb.WriteString(" ")
}
}

// newInternalNode creates new internal node.
func newInternalNode(depth int) *InternalNode {
func newInternalNode(depth int32) *InternalNode {
return &InternalNode{depth: depth}
}
4 changes: 2 additions & 2 deletions internal/jbig2/decoder/huffman/standard_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func GetStandardTable(number int) (Tabler, error) {
return table, nil
}

func newStandardTable(table [][]int) (*StandardTable, error) {
func newStandardTable(table [][]int32) (*StandardTable, error) {
var codeTable []*Code

for i := 0; i < len(table); i++ {
Expand All @@ -88,7 +88,7 @@ func newStandardTable(table [][]int) (*StandardTable, error) {
return s, nil
}

var tables = [][][]int{
var tables = [][][]int32{
// B1
{
{1, 4, 0},
Expand Down
4 changes: 1 addition & 3 deletions internal/jbig2/decoder/huffman/standard_table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ func TestGetStandardTable(t *testing.T) {
table, err := GetStandardTable(1)
require.NoError(t, err)

if assert.NotNil(t, table.RootNode()) {
t.Logf(table.String())
}
assert.NotNil(t, table.RootNode())
})

t.Run("OutOfRange", func(t *testing.T) {
Expand Down
Loading