Example #1
0
func unrolledBitPack1(input []int64, offset int, len int, w io.ByteWriter) error {
	numHops := 8
	remainder := len % numHops
	endOffset := offset + len
	endUnroll := endOffset - remainder
	val := 0
	for i := offset; i < endUnroll; i = i + numHops {
		val = (val | (int(input[i]&1) << 7) |
			(int(input[i+1]&1) << 6) |
			(int(input[i+2]&1) << 5) |
			(int(input[i+3]&1) << 4) |
			(int(input[i+4]&1) << 3) |
			(int(input[i+5]&1) << 2) |
			(int(input[i+6]&1) << 1) |
			int(input[i+7])&1)
		err := w.WriteByte(byte(val))
		if err != nil {
			return err
		}
		val = 0
	}
	if remainder > 0 {
		startShift := 7
		for i := endUnroll; i < endOffset; i++ {
			val = (val | int(input[i]&1)<<uint64(startShift))
			startShift--
		}
		err := w.WriteByte(byte(val))
		if err != nil {
			return err
		}
	}
	return nil
}
Example #2
0
func unrolledBitPack4(input []int64, offset int, len int, w io.ByteWriter) error {
	numHops := 2
	remainder := len % numHops
	endOffset := offset + len
	endUnroll := endOffset - remainder
	val := 0
	for i := offset; i < endUnroll; i = i + numHops {
		val = (val | (int(input[i]&15) << 4) | int(input[i+1])&15)
		err := w.WriteByte(byte(val))
		if err != nil {
			return err
		}
		val = 0
	}

	if remainder > 0 {
		startShift := 4
		for i := endUnroll; i < endOffset; i++ {
			val = (val | int(input[i]&15)<<uint64(startShift))
			startShift -= 4
		}
		err := w.WriteByte(byte(val))
		if err != nil {
			return err
		}
	}
	return nil
}
Example #3
0
// WriteBytes writes an encoded []byte to buf, returning the number of bytes
// written, and any write error encountered.
func WriteBytes(buf io.ByteWriter, data []byte) (n int, err error) {
	wb := func(b byte) (err error) {
		if err = buf.WriteByte(b); err == nil {
			n++
		}
		return
	}

	acc := byte(0)
	for i := 0; i < len(data); i++ {
		m := uint(i % 7)
		b := data[i]
		if err = wb(acc | 1 | ((b & (0xff << (m + 1))) >> m)); err != nil {
			return
		}
		acc = (b << (7 - m))
		if m == 6 {
			if err = wb(acc | 1); err != nil {
				return
			}
			acc = 0
		}
	}
	err = wb(acc)
	return
}
Example #4
0
File: util.go Project: dsnet/golib
// Allows multiple bytes to be written to a buffer like io.Write.
func ByteWriteFull(wr io.ByteWriter, buf []byte) (cnt int, err error) {
	for cnt = 0; cnt < len(buf); cnt++ {
		err = wr.WriteByte(buf[cnt])
		if err != nil {
			return
		}
	}
	return
}
Example #5
0
// encodeInt writes an encoded int64 to the passed io.ByteWriter.
func encodeInt(v int64, w io.ByteWriter) {
	if v < 0 {
		v = ^(v << 1)
	} else {
		v <<= 1
	}
	for v >= 0x20 {
		w.WriteByte((0x20 | (byte(v) & 0x1f)) + 63)
		v >>= 5
	}
	w.WriteByte(byte(v) + 63)
}
Example #6
0
File: util.go Project: dsnet/golib
func ByteCopy(dst io.ByteWriter, src io.ByteReader) (cnt int64, err error) {
	var val byte
	for cnt = 0; true; cnt++ {
		if val, err = src.ReadByte(); err != nil {
			break
		}
		if err = dst.WriteByte(val); err != nil {
			break
		}
	}
	if err == io.EOF { // This is expected
		err = nil
	}
	return
}
Example #7
0
File: util.go Project: dsnet/golib
func ByteCopyN(dst io.ByteWriter, src io.ByteReader, num int64) (cnt int64, err error) {
	var val byte
	for cnt = 0; cnt < num; cnt++ {
		if val, err = src.ReadByte(); err != nil {
			break
		}
		if err = dst.WriteByte(val); err != nil {
			break
		}
	}
	if cnt == num { // This is expected
		err = nil
	}
	return
}
Example #8
0
func writeVulong(w io.ByteWriter, value int64) error {
	for {
		if (value & ^0x7f) == 0 {
			err := w.WriteByte(byte(value))
			if err != nil {
				return err
			}
			return nil
		}
		err := w.WriteByte(byte(0x80 | (value & 0x7f)))
		if err != nil {
			return err
		}
		value = int64(uint64(value) >> 7)
	}
}
Example #9
0
func writeRemainingLongs(w io.ByteWriter, offset int, input []int64, remainder int, numBytes int) error {
	numHops := remainder
	idx := 0
	writeBuffer := make([]byte, 64, 64)
	switch numBytes {
	case 1:
		for remainder > 0 {
			writeBuffer[idx] = byte(input[offset+idx] & 255)
			remainder--
			idx++
		}
	case 2:
		for remainder > 0 {
			writeLongBE2(writeBuffer, input[offset+idx], idx*2)
			remainder--
			idx++
		}
	case 3:
		for remainder > 0 {
			writeLongBE3(writeBuffer, input[offset+idx], idx*3)
			remainder--
			idx++
		}
	case 4:
		for remainder > 0 {
			writeLongBE4(writeBuffer, input[offset+idx], idx*4)
			remainder--
			idx++
		}

	case 5:
		for remainder > 0 {
			writeLongBE5(writeBuffer, input[offset+idx], idx*5)
			remainder--
			idx++
		}
	case 6:
		for remainder > 0 {
			writeLongBE6(writeBuffer, input[offset+idx], idx*6)
			remainder--
			idx++
		}
	case 7:
		for remainder > 0 {
			writeLongBE7(writeBuffer, input[offset+idx], idx*7)
			remainder--
			idx++
		}
	case 8:
		for remainder > 0 {
			writeLongBE8(writeBuffer, input[offset+idx], idx*8)
			remainder--
			idx++
		}
	}

	toWrite := numHops * numBytes
	for j := 0; j < toWrite; j++ {
		err := w.WriteByte(writeBuffer[j])
		if err != nil {
			return err
		}
	}
	return nil
}
Example #10
0
func writeLongBE(w io.ByteWriter, input []int64, offset int, numHops int, numBytes int) error {
	writeBuffer := make([]byte, BufferSize, BufferSize)
	switch numBytes {
	case 1:
		writeBuffer[0] = byte(input[offset+0] & 255)
		writeBuffer[1] = byte(input[offset+1] & 255)
		writeBuffer[2] = byte(input[offset+2] & 255)
		writeBuffer[3] = byte(input[offset+3] & 255)
		writeBuffer[4] = byte(input[offset+4] & 255)
		writeBuffer[5] = byte(input[offset+5] & 255)
		writeBuffer[6] = byte(input[offset+6] & 255)
		writeBuffer[7] = byte(input[offset+7] & 255)
	case 2:
		writeLongBE2(writeBuffer, input[offset+0], 0)
		writeLongBE2(writeBuffer, input[offset+1], 2)
		writeLongBE2(writeBuffer, input[offset+2], 4)
		writeLongBE2(writeBuffer, input[offset+3], 6)
		writeLongBE2(writeBuffer, input[offset+4], 8)
		writeLongBE2(writeBuffer, input[offset+5], 10)
		writeLongBE2(writeBuffer, input[offset+6], 12)
		writeLongBE2(writeBuffer, input[offset+7], 14)
	case 3:
		writeLongBE3(writeBuffer, input[offset+0], 0)
		writeLongBE3(writeBuffer, input[offset+1], 3)
		writeLongBE3(writeBuffer, input[offset+2], 6)
		writeLongBE3(writeBuffer, input[offset+3], 9)
		writeLongBE3(writeBuffer, input[offset+4], 12)
		writeLongBE3(writeBuffer, input[offset+5], 15)
		writeLongBE3(writeBuffer, input[offset+6], 18)
		writeLongBE3(writeBuffer, input[offset+7], 21)
	case 4:
		writeLongBE4(writeBuffer, input[offset+0], 0)
		writeLongBE4(writeBuffer, input[offset+1], 4)
		writeLongBE4(writeBuffer, input[offset+2], 8)
		writeLongBE4(writeBuffer, input[offset+3], 12)
		writeLongBE4(writeBuffer, input[offset+4], 16)
		writeLongBE4(writeBuffer, input[offset+5], 20)
		writeLongBE4(writeBuffer, input[offset+6], 24)
		writeLongBE4(writeBuffer, input[offset+7], 28)
	case 5:
		writeLongBE5(writeBuffer, input[offset+0], 0)
		writeLongBE5(writeBuffer, input[offset+1], 5)
		writeLongBE5(writeBuffer, input[offset+2], 10)
		writeLongBE5(writeBuffer, input[offset+3], 15)
		writeLongBE5(writeBuffer, input[offset+4], 20)
		writeLongBE5(writeBuffer, input[offset+5], 25)
		writeLongBE5(writeBuffer, input[offset+6], 30)
		writeLongBE5(writeBuffer, input[offset+7], 35)
	case 6:
		writeLongBE6(writeBuffer, input[offset+0], 0)
		writeLongBE6(writeBuffer, input[offset+1], 6)
		writeLongBE6(writeBuffer, input[offset+2], 12)
		writeLongBE6(writeBuffer, input[offset+3], 18)
		writeLongBE6(writeBuffer, input[offset+4], 24)
		writeLongBE6(writeBuffer, input[offset+5], 30)
		writeLongBE6(writeBuffer, input[offset+6], 36)
		writeLongBE6(writeBuffer, input[offset+7], 42)
	case 7:
		writeLongBE7(writeBuffer, input[offset+0], 0)
		writeLongBE7(writeBuffer, input[offset+1], 7)
		writeLongBE7(writeBuffer, input[offset+2], 14)
		writeLongBE7(writeBuffer, input[offset+3], 21)
		writeLongBE7(writeBuffer, input[offset+4], 28)
		writeLongBE7(writeBuffer, input[offset+5], 35)
		writeLongBE7(writeBuffer, input[offset+6], 42)
		writeLongBE7(writeBuffer, input[offset+7], 49)
	case 8:
		writeLongBE8(writeBuffer, input[offset+0], 0)
		writeLongBE8(writeBuffer, input[offset+1], 8)
		writeLongBE8(writeBuffer, input[offset+2], 16)
		writeLongBE8(writeBuffer, input[offset+3], 24)
		writeLongBE8(writeBuffer, input[offset+4], 32)
		writeLongBE8(writeBuffer, input[offset+5], 40)
		writeLongBE8(writeBuffer, input[offset+6], 48)
		writeLongBE8(writeBuffer, input[offset+7], 56)
	}

	toWrite := numHops * numBytes
	for j := 0; j < toWrite; j++ {
		err := w.WriteByte(writeBuffer[j])
		if err != nil {
			return err
		}
	}
	return nil
}
Example #11
0
func writeInts(input []int64, offset int, l int, bitSize int, w io.ByteWriter) error {
	if input == nil || len(input) < 1 || offset < 0 || l < 1 || bitSize < 1 {
		return nil
	}

	switch bitSize {
	case 1:
		return unrolledBitPack1(input, offset, l, w)
	case 2:
		return unrolledBitPack2(input, offset, l, w)
	case 4:
		return unrolledBitPack4(input, offset, l, w)
	case 8:
		return unrolledBitPack8(input, offset, l, w)
	case 16:
		return unrolledBitPack16(input, offset, l, w)
	case 24:
		return unrolledBitPack24(input, offset, l, w)
	case 32:
		return unrolledBitPack32(input, offset, l, w)
	case 40:
		return unrolledBitPack40(input, offset, l, w)
	case 48:
		return unrolledBitPack48(input, offset, l, w)
	case 56:
		return unrolledBitPack56(input, offset, l, w)
	case 64:
		return unrolledBitPack64(input, offset, l, w)
	}

	bitsLeft := 8
	current := byte(0x00)
	for i := offset; i < (offset + l); i++ {
		value := input[i]
		bitsToWrite := bitSize
		for bitsToWrite > bitsLeft {
			// add the bits to the bottom of the current word
			current |= uint8(uint64(value) >> uint64(bitsToWrite-bitsLeft))
			// subtract out the bits we just added
			bitsToWrite -= bitsLeft
			// zero out the bits above bitsToWrite
			value &= (1 << uint64(bitsToWrite)) - 1
			err := w.WriteByte(current)
			if err != nil {
				return err
			}
			current = 0
			bitsLeft = 8
		}
		bitsLeft -= bitsToWrite
		current |= uint8(value << uint64(bitsLeft))
		if bitsLeft == 0 {
			err := w.WriteByte(current)
			if err != nil {
				return err
			}
			current = 0
			bitsLeft = 8
		}
	}

	// flush
	if bitsLeft != 8 {
		err := w.WriteByte(current)
		if err != nil {
			return err
		}
		current = 0
		bitsLeft = 8
	}

	return nil
}
Example #12
0
func decodeSingle(input io.ByteReader, output io.ByteWriter) (noMoreInput bool,
	endOfKey bool, err error) {
	byteOne, err := input.ReadByte()
	if err != nil {
		// No more to read
		return true, false, nil
	}
	if byteOne == 0x00 {
		// Maybe special case
		nextByte, err := input.ReadByte()
		if err != nil {
			// End reached, no special case
			err = output.WriteByte(0x00)
			if err != nil {
				return false, false, err
			}
			return true, false, nil
		}
		if nextByte == 0x00 {
			// End of key here, nothing to write
			return false, true, nil
		}
		if nextByte == 0x01 {
			// Escape, read the next byte
			escapedByte, err := input.ReadByte()
			if err != nil {
				return true, false, nil //TODO: Is this error?
			}
			err = output.WriteByte(0x00)
			if err != nil {
				return false, false, err
			}
			if !(escapedByte == 0x00 || escapedByte == 0x01) {
				// Error, wrong escaped byte
				err = errors.New(fmt.Sprintf("Wrong escape sequence, expecting byte 0 or 1 "+
					"but got %v", escapedByte))
				return false, false, err
			}
			err = output.WriteByte(escapedByte)
			if err != nil {
				return false, false, err
			}
			// Done here with unescaping
			return false, false, nil
		}
		// Nope, no special case
		err = output.WriteByte(0x00)
		if err != nil {
			return false, false, err
		}
		err = output.WriteByte(nextByte)
		if err != nil {
			return false, false, err
		}
	} else {
		// Standard case
		err = output.WriteByte(byteOne)
		if err != nil {
			return false, false, err
		}
	}
	return false, false, nil
}
Example #13
0
func encodeSingle(input io.ByteReader, output io.ByteWriter) (noMoreInput bool,
	err error) {
	byteOne, err := input.ReadByte()
	if err != nil {
		// No more to read
		return true, nil
	}
	if byteOne == 0x00 {
		// Special cases
		// Maybe special case
		nextByte, err := input.ReadByte()
		if err != nil {
			err = output.WriteByte(0x00)
			if err != nil {
				return false, err
			}
			return true, nil
		}
		if nextByte == 0x00 || nextByte == 0x01 {
			// Need to escape that
			err = output.WriteByte(0x00)
			if err != nil {
				return false, err
			}
			err = output.WriteByte(0x01)
			if err != nil {
				return false, err
			}
			err = output.WriteByte(nextByte)
			if err != nil {
				return false, err
			}
		} else {
			// Ah, no, no special case
			err = output.WriteByte(0x00)
			if err != nil {
				return false, err
			}
			err = output.WriteByte(nextByte)
			if err != nil {
				return false, err
			}
		}
	} else {
		// Standard case
		err = output.WriteByte(byteOne)
		if err != nil {
			return false, err
		}
	}
	return false, nil
}
Example #14
0
func (i InstWriteToOutput) Eval(t Tape, in io.ByteReader, out io.ByteWriter) {
	b := t.GetByte()
	out.WriteByte(b)
}