bamiaux / iobit

Package iobit provides primitives for reading & writing bits
MIT License
17 stars 5 forks source link

Buffer #1

Closed sbowman closed 10 years ago

sbowman commented 10 years ago

I love the library; perfect for helping parse a bit stream. But I'm having a problem with setting up the Reader.

I decode a Base64 string (a SCTE 35 signal) into a byte array and send it into iobit.NewReader(), but the binary that comes out doesn't look right.

If I parse through the byte array from the base64 string directly, bit by bit, I get a string of bits like this:

1111110001010101000000000101110100000001100100111101011101111100111111101111011000011101010110001011001001100000110000101000011100010011111111101111100111101111100011000000100110100110000101001100100000110000111001011100111110101011010100111010101010010101111110100100100010010111100001100011001101101001110001011001010100011010110101010101111010010110101001101110011110010010011001010111100010010000101101000100111011010111101100101001011001001111010110000111001110100001100111101010111010110011000100

But when I create a new reader from the decoded base64 string's byte array and loop through calling iobit.IsBit(), I get a string of bits like this:

11111100000000000101010100000000000000000000000000000000000000000000000000000000000000000000000000000101000001101000000001100100111101011101111100000000000000000011111100000010001111010100001101010101010001010100100101000001010000100100001101000100011111111101111100000000000000001111011100110001010000000000100100101001010000100100110001000001010000110100101101001111010101010101010000111010010101000101011101110100010010000100101100110000001100010100110101010011010001010100101001000110010110100101010101110100010110100100110100110011011001000100110001010111010001000100001001101000010011100110101101101100010100100110010001111010001100000011100100110100000000000000000001100111101010111010110011000100

When I try to parse the binary, I obviously get lots of 0 values that really shouldn't be 0.

Any idea what I'm doing wrong?

bamiaux commented 10 years ago

I wasn't able to reproduce your bug

func TestBug1(t *testing.T) {
    src := []byte{0xFC, 0x55, 0x00, 0x5D, 0x01, 0x93, 0xD7, 0x7C}
    r := NewReader(src[:])
    dst := make([]byte, len(src))
    w := NewWriter(dst[:])
    for i := 0; i < len(src)*8; i++ {
        v := uint32(0)
        if r.IsBit() {
            v = 1
        }
        w.PutUint32(1, v)
    }
    flushCheck(t, w)
    compare(t, src, dst)
}

In this test case, the first 8 bytes seems to be decoded fine, bit by bit, while you're suggesting they're not. Without any code, It'll be hard to say where the bug is.

sbowman commented 10 years ago

Maybe I'm doing something wrong. Something with the bytes produced by encoding/base64? I'll take a closer look at that.

I posted my test code as a Gist:

https://gist.github.com/sbowman/9512354

bamiaux commented 10 years ago

Your showBinary function is broken, you need to pad the printed value. Here's a modified version which give the same result manually and with iobit. I didn't check the whole string, but the first few bytes were correctly decoded

https://gist.github.com/bamiaux/9890754

sbowman commented 10 years ago

Damn. I'm an idiot. Sorry for that, and thanks for catching it. And thanks again for iobit.

On Mar 31, 2014, at 5:59 AM, Benoît Amiaux notifications@github.com wrote:

Your showBinary function is broken, you need to pad the printed value. Here's a modified version which give the same result manually and with iobit. I didn't check the whole string, but the first few bytes were correctly decoded

https://gist.github.com/bamiaux/9890754

— Reply to this email directly or view it on GitHub.

bamiaux commented 10 years ago

No worry, bugs can happen !