GithubHelp home page GithubHelp logo

tianking / raw Goto Github PK

View Code? Open in Web Editor NEW

This project forked from hacker1024/raw

0.0 0.0 0.0 102 KB

Dart package for writing, reading, and debugging binary data.

License: Apache License 2.0

Dart 100.00%

raw's Introduction

Pub Package

Introduction

A package for writing, reading, and debugging binary data. Licensed under the Apache License 2.0.

Development happens at: github.com/terrier989/dart-raw

Features

Key classes

  • RawWriter:
    • Writes bytes to a buffer.
    • Automatically expands the buffer when isExpanding is true.
  • RawReader:
    • Reads bytes from a buffer.
    • If reading fails, throw descriptive exceptions.
  • SelfEncoder
    • Classes that know how to encode state using RawWriter.
  • SelfDecoder
    • Classes that know how to decode state using RawReader.
  • SelfCodec
    • Classes that implement both SelfEncoder and SelfDecoder.

A typical implementation of SelfCodec looks like this:

class MyStruct extends SelfCodec {
  int intField = 0;
  String stringField = "";
  
  @override
  void encodeSelf(RawWriter writer) {
    writer.writeInt32(intField);
    writer.writeUint32(stringField.length, Endian.little);
    writer.writeUtf8(stringField);
  }
  
  @override
  void decodeSelf(RawReader reader) {
    intField = reader.readInt32();
    final stringLength = reader.readUint32(Endian.little);
    stringField = reader.readUtf8(stringLength);
  }
}

Supported primitives

  • Numeric types
    • Unsigned/signed integers
      • Uint8 / Int8
      • Uint16 / Int16
      • Uint32 / Int32
      • FixInt64 / FixInt64 (Int64 from fixnum)
    • Floating-point values
      • Float32
      • Float64
    • Variable-length integers (identical with Protocol Buffers encoding)
      • VarUint
      • VarInt
  • Sequence types
    • Uint8List
    • ByteData
    • Strings (UTF-8)
    • Zeroes

Helpers for testing

import 'package:test/test.dart';
import 'package:raw/raw_test.dart';

void main() {
  test("an example", () {
    final value = [9,8,7];
    final expected = [1,2,3];
    expect(value, byteListEquals(expected));
    
    // Output:
    // 0x0000: 0908 07
    //     (0) ^^^^ ^^
    //         0102 03
  });
}

If your class implements SelfEncoder, use selfEncoderEquals:

class MyStruct extends SelfCodec {
  // ...
}

void main() {
  test("an example", () {
    // ...
    expect(myStruct, selfEncoderEquals(expected));
  });
}

Converting hex formats to bytes

DebugHexDecoder is able to import hex formats such as:

  • "0000000: 0123 4567 89ab cdef 0123 4567 89ab cdef ................"
  • "0000010: 012345678 ............ # comment"
  • "012345678abcdef // no prefix"

Converting bytes to hex-based descriptions

DebugHexEncoder converts bytes to the following format:

0x0000: 0123 4567 89ab cdef  0123 4567 89ab cdef
    (0)
0x0010: 0123 4567 89ab cdef  0123 4567 89ab cdef
   (16)

If expected byte list is specified, bytes are converted to the following format:

0x0000: 0123 5555 89ab cdef  0123 4567 89ab cdef
    (0)      ^ ^^                                 <-- index of the first problem: 0x02 (decimal: 2)
             4 67
0x0010: 0123 4567 89ab cdef  0123 4567 89
   (16)                                  ^^ ^^^^  <-- index of the first problem: 0x0D (decimal: 13)
                                         ab cdef

raw's People

Contributors

hacker1024 avatar terrier989 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.