stellar / js-xdr

Read/write XDR encoded data structures (RFC 4506)
Apache License 2.0
25 stars 31 forks source link

Opaque data type without a length header? #52

Open evanpurkhiser opened 4 years ago

evanpurkhiser commented 4 years ago

Is your feature request related to a problem? Please describe. I'm implementing the ONC-RPC protocol using this XDR library. Part of the spec has structs that represent headers for arbitrary data requests and responses, thus I need a way to specify a type that basically says 'the rest of the data in the buffer', like varOpaque but without the length header.

For example, the struct:

struct accepted_reply {
   opaque_auth verf;
   union switch (accept_stat stat) {
   case SUCCESS:
      opaque results[0];
      /*
       * procedure-specific results start here
       */
    case PROG_MISMATCH:
       struct {
          unsigned int low;
          unsigned int high;
       } mismatch_info;
    default:
       void;
    } reply_data;
};

The procedure-specific results start here is where the rest response of the rpc call lives. But how can I access that after using fromXDR?

Describe the solution you'd like

Here's what I've hacked together: https://github.com/EvanPurkhiser/prolink-typescript/blob/957918208b0399ceaa1d6978ae23c7d3415b8436/src/nfs/xdr.ts#L3-L18

Then the union definition looks like this

https://github.com/EvanPurkhiser/prolink-typescript/blob/957918208b0399ceaa1d6978ae23c7d3415b8436/src/nfs/xdr.ts#L84-L95

Describe alternatives you've considered Alternatively some access to the cursor could potentially be OK, but it would be nice to get the data back as part of the result object

bartekn commented 4 years ago

I think you can try to modify https://github.com/stellar/js-xdr/blob/c58bbdc2b1c376d447208ba1ff847846b6c369e6/src/io-mixin.js#L18 a bit, like:

const cursor = new Cursor(buffer); // js-xdr Cursor
const result = acceptedReply.read(cursor);
const pos = cursor.tell() // bytes read
// if SUCCESS:
procedureSpecific = buffer.slice(pos)