vstruct2 icon indicating copy to clipboard operation
vstruct2 copied to clipboard

proposal: add vsCast method

Open williballenthin opened this issue 10 years ago • 1 comments

via: https://github.com/vivisect/dissect/pull/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)

williballenthin avatar Feb 24 '16 15:02 williballenthin

Hadn't even thought of having it use a "shared backing". Really dig it. Maybe we also need the equiv of a vsMemCopy(dest), but named something better ( so there's a syntax that does the exact thing that sparked the conversation :) )

For the "shared backing" vsCast syntax, i think of the "cast" operation as applying to the old object so maybe something like

src.vsCast( dst )

or the longer...

dst.vsCastFrom( src )

or naming it something that makes the second use case the implied direction?

dst.vsPointTo()

invisig0th avatar Feb 24 '16 15:02 invisig0th