Giter Site home page Giter Site logo

bitstream's Introduction

What is a bitstream?

It is a utility for reading, writing, and interpreting data that is not always byte-aligned. This module provides a Reader and Writer bitstream implementation.

How can I use a bitstream.Reader?

Suppose you have a binary file with the following format:

  • First 6 bits is an unsigned integer for the major version
  • following 7 bits is padded 0's
  • there will be three 4-byte strings, each with 4 bits of zero padding

The file could be parsed in this way:

package main

import (
	"github.com/gravestench/bitstream"
)

func main() {
	r := bitstream.NewReader().FromBytes(fileBytes)

	var err error

	version, err := r.Next(6).Bits().AsUInt()
	if err != nil {
		// handle it
	}

	// check the zero pad
	pad, err := r.Next(7).Bits().AsInt()
	if pad > 0 || err != nil {
		// handle it
	}

	strings := make([]string, 3)

	for strIdx := range strings {
		chars := make([]byte, 4)

		// read 4 characters
		for charIdx := range chars {
			chars[charIdx], err = r.Next(1).Bytes().AsByte()
			if err != nil {
				// handle it
			}
		}

		// check the zero pad
		pad, err := r.Next(4).Bits().AsInt()
		if pad > 0 || err != nil {
			// handle it
		}

		// cast the 4 characters as a string
		strings[strIdx] = (string)(chars)
	}
}

How can I use a bitstream.Writer?

Assuming the same file format as above:

package main

import (
	"github.com/gravestench/bitstream"
)

type example struct {
	version uint8
	strings [4]string
}

func main() {
	e := example{
		version: 5,
		strings: [4]string{
			"abcd",
			"efgh",
			"ijkl",
		},
	}

	w := &bitstream.Writer{}
	bits := bitstream.BitsFromByte(e.version)[:6]

	if bitsWritten, err := w.Write(bits); err != nil {
		// handle it
	} else if bitsWritten != 6 {
		// handle it
	}

	pad7 := bitstream.BitsFromByte(0)[:7]

	if bitsWritten, err := w.Write(pad7); err != nil {
		// handle it
	} else if bitsWritten != 7 {
		// handle it
	}

	pad4 := bitstream.BitsFromByte(0)[:4]

	for idx := range e.strings {
		bytes := ([]byte)(e.strings[idx])[:4]
		w.Write(bytes, pad4)
	}
}

bitstream's People

Contributors

dknuth-tti avatar gravestench avatar gucio321 avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.