Giter Site home page Giter Site logo

vstruct2's Issues

vbytes default size is 0

This issue can serve as a discussion point for the following behavior, which I was surprised by, and find inconsistent.

An uninitialized vbytes instance emits 0 bytes, while an uninitialized number instance emits a non-zero number of bytes. For example:

import vstruct.types as v_types

# initialized vbytes should have `size` bytes emitted
a = v_types.vbytes(size=8)
a.vsParse(b"\x00" * 8)
assert(len(a.vsEmit()) == 8)  # --> OK

# uninitialized vbytes should have `size` bytes emitted???
b = v_types.vbytes(size=8)
assert(len(b.vsEmit()) == 8)  # --> AssertionError: the size is 0

# initialized uint64 should have 8 bytes emitted
c = v_types.uint64()
c.vsParse(b"\x00" * 8)
assert(len(c.vsEmit()) == 8)  # --> OK

# uninitialized uint64 should have 8 bytes emitted
d = v_types.uint64()
assert(len(d.vsEmit()) == 8)  # --> OK

This surprised me while writing parsers because if I created a new instance of a VStruct that contained both number and vbytes members, I only had to explicitly initialize some of them. (Actually, I started with vstructs that only contained numbers, and didn't have to initialize the struct to emit a buffer of correct length. This changed when I added a vbytes member.)

I think the API would be more consistent if vbytes emitted size 0x00 bytes if its not been initialized; however, I can also understand the argument that structs should be initialized or undefined behavior results.

What are your thoughts?

proposal: add vsCast method

via: vivisect/dissect#1

i had used the following function to cast the bytes from one vstruct to another:

def cast_vstruct(src_instance, dst_instance):
    '''
    load the bytes that back `src_instance` into `dst_instance`.
    note that subsequent changes made to `dst_instance` are not reflected in `src_instance`.
    so this is useful in 'by-value' situations, but not for situations expecting writeback support.
    modifies `dst_instance`. does not modify src_instance.
    type src_instance: v_types.VStruct
    type dst_instance: v_types.VStruct
    '''
    d = src_instance.vsEmit()
    dst_instance.vsParse(d)

a vsCast method on vstruct might enable the passing of the fd/bytes and offset to the destination object, and therefore:

  1. be higher performant, since the serialization/deserialization is avoided.
  2. support writeback on the destination instance (though this may result in sync issues)

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.