All Projects → scodec → Scodec

scodec / Scodec

Licence: bsd-3-clause
Scala combinator library for working with binary data

Programming Languages

scala
5932 projects

Projects that are alternatives of or similar to Scodec

Bitmatch
A Rust crate that allows you to match, bind, and pack the individual bits of integers.
Stars: ✭ 82 (-88.43%)
Mutual labels:  encoding, binary, decoding
sirdez
Glorious Binary Serialization and Deserialization for TypeScript.
Stars: ✭ 20 (-97.18%)
Mutual labels:  encoding, binary, decoding
Binary
Generic and fast binary serializer for Go
Stars: ✭ 86 (-87.87%)
Mutual labels:  encoding, binary, decoding
Pbf
A low-level, lightweight protocol buffers implementation in JavaScript.
Stars: ✭ 618 (-12.83%)
Mutual labels:  encoding, binary, decoding
Phpasn1
A PHP library to encode and decode arbitrary ASN.1 structures using ITU-T X.690 encoding rules.
Stars: ✭ 136 (-80.82%)
Mutual labels:  encoding, binary, decoding
go-fixedwidth
Encoding and decoding for fixed-width formatted data
Stars: ✭ 64 (-90.97%)
Mutual labels:  encoding, decoding
avro ex
An Avro Library that emphasizes testability and ease of use.
Stars: ✭ 47 (-93.37%)
Mutual labels:  encoding, decoding
Flac
Free Lossless Audio Codec
Stars: ✭ 593 (-16.36%)
Mutual labels:  encoding, decoding
scure-base
Secure, audited & 0-deps implementation of bech32, base64, base32, base16 & base58
Stars: ✭ 27 (-96.19%)
Mutual labels:  encoding, decoding
universal-base64
Small universal base64 functions for node.js and browsers
Stars: ✭ 25 (-96.47%)
Mutual labels:  encoding, decoding
velvet-video
Java library for encoding / decoding / muxing / demuxing video and audio in various formats
Stars: ✭ 32 (-95.49%)
Mutual labels:  encoding, decoding
AnimatedGif
📼 A high performance .NET library for reading and creating animated GIFs
Stars: ✭ 106 (-85.05%)
Mutual labels:  encoding, decoding
cpp-bencoding
A C++ bencoding library supporting both decoding and encoding.
Stars: ✭ 20 (-97.18%)
Mutual labels:  encoding, decoding
Decodify
Detect and decode encoded strings, recursively.
Stars: ✭ 670 (-5.5%)
Mutual labels:  encoding, decoding
content inspector
Fast inspection of binary buffers to guess/determine the type of content
Stars: ✭ 28 (-96.05%)
Mutual labels:  encoding, binary
sia
Sia - Binary serialisation and deserialisation
Stars: ✭ 52 (-92.67%)
Mutual labels:  encoding, binary
Serpent
A protocol to serialize Swift structs and classes for encoding and decoding.
Stars: ✭ 281 (-60.37%)
Mutual labels:  encoding, decoding
Libmorton
C++ header-only library with methods to efficiently encode/decode Morton codes in/from 2D/3D coordinates
Stars: ✭ 373 (-47.39%)
Mutual labels:  encoding, decoding
Hashids.net
A small .NET package to generate YouTube-like hashes from one or many numbers. Use hashids when you do not want to expose your database ids to the user.
Stars: ✭ 470 (-33.71%)
Mutual labels:  encoding, decoding
d3coder
Chrome extension for encoding/decoding and hashing text on websites
Stars: ✭ 26 (-96.33%)
Mutual labels:  encoding, decoding

scodec

Gitter

Scala combinator library for working with binary data.

Design Constraints

This library focuses on contract-first and pure functional encoding and decoding of binary data. The following design constraints are considered:

  • Binary structure should mirror protocol definitions and be self-evident under casual reading
  • Mapping binary structures to types should be statically verified
  • Encoding and decoding should be purely functional
  • Failures in encoding and decoding should provide descriptive errors
  • Compiler plugin should not be used

As a result, the library is implemented as a combinator based DSL. Performance is considered but yields to the above design constraints.

Acknowledgements

Scodec 1.x used Shapeless and is heavily influenced by scala.util.parsing.combinator. As of Scodec 2.x, the library only depends on the standard library.

Administrative

This project is licensed under a 3-clause BSD license.

The scodec channel on Gitter is a good place to go for help. Also consider using the scodec tag on StackOverflow.

Introduction

The primary abstraction is a Codec[A], which supports encoding a value of type A to a BitVector and decoding a BitVector to a value of type A.

The codecs objects provides a number of predefined codecs and combinators.

    import scodec.*
    import scodec.bits.*
    import scodec.codecs.*

    // Create a codec for an 8-bit unsigned int followed by an 8-bit unsigned int followed by a 16-bit unsigned int
    val firstCodec = uint8 :: uint8 :: uint16

    // Decode a bit vector using that codec
    val result: Attempt[DecodeResult[(Int, Int, Int)]] = firstCodec.decode(hex"102a03ff".bits)
    // Successful(DecodeResult(((16, 42), 1023), BitVector(empty)))

    // Sum the result
    val add3 = (_: Int) + (_: Int) + (_: Int)
    val sum: Attempt[DecodeResult[Int]] = result.map(_.map(add3))
    // Successful(DecodeResult(1081, BitVector(empty)))

Automatic case class binding is supported via tuples:

    case class Point(x: Int, y: Int, z: Int)

    val pointCodec: Codec[Point] = (int8 :: int8 :: int8).as[Point]

    val encoded: Attempt[BitVector] = pointCodec.encode(Point(-5, 10, 1))
    // Successful(BitVector(24 bits, 0xfb0a01))

    val decoded: Attempt[DecodeResult[Point]] = pointCodec.decode(0xfb0a01)
    // Successful(DecodeResult(Point(-5, 10, 1), BitVector(empty)))

Codecs can also be derived, resulting in usage like:

    case class Point(x: Int, y: Int, z: Int) derives Codec

    val encoded: Attempt[BitVector] = Codec.encode(Point(-5, 10, 1))
    // Successful(BitVector(96 bits, 0x000000fb0000000a00000001))

    val decoded: Attempt[DecodeResult[Point]] = Codec.decode[Point](0x000000fb0000000a00000001)
    // Successful(DecodeResult(Point(-5, 10, 1), BitVector(empty)))

New codecs can be created by either implementing the Codec trait though typically new codecs are created by applying one or more combinators to existing codecs.

See the guide for detailed documentation. Also, see ScalaDoc. Especially:

Ecosystem

Many libraries have support for scodec:

Examples

There are various examples in the test directory, including codecs for:

The scodec-protocols has production quality codecs for the above examples.

The bmsg library has a codec for the Bitcoin Cash and Bitcoin Core network protocol.

The scodec-msgpack library provides codecs for MessagePack.

The fs2-http project uses FS2, scodec, and shapeless to implement a minimal HTTP client and server.

The scodec-bson library implements BSON codecs and combinators.

Testing Your Own Codecs

If you're creating your own Codec instances scodec publishes some of its own test tooling in the scodec-testkit module.

Getting Binaries

See the releases page on the website.

Building

This project uses sbt and requires node.js to be installed in order to run Scala.js tests. To build, run sbt publish-local.

Code of Conduct

See the Code of Conduct.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].