Is it possible to intercept a matlab save() bytestream

前端 未结 10 2335
暖寄归人
暖寄归人 2020-12-09 22:50

In matlab it is possible to write matlab objects, or even the entire workspace, to a file using the matlab save() call. I would like to intercept the bytestream and postproc

相关标签:
10条回答
  • 2020-12-09 23:09

    Couldn't you encrypt the content of variables instead ?

    With whos, you get a list of all your variables in alphabetic order. For each one, you generate a mask of the same size with your encryption algorithm and you replace the "true" value by itself XOR the mask. To finish, you save the encrypted variables using save. The name and size of your variables are visible but that's probably not critical (if necessary, you can crypt names too).

    Proceed the same way to load.

    0 讨论(0)
  • 2020-12-09 23:14

    How about using a virtual filesystem? On Windows there is a commercial library called BoxedAPP SDK that allows you to create a virtual file that is only visible to the creating process (possibly children also). You would probably have to make a MEX to interface the library. First you would create the virtual file and then you could use the save command in matlab with the same filename. Then you can read the serialized .mat bytestream using normal fopen/fread functions in the matlab and do what ever you wish with it. This would at least prevent the file getting created on the hard disk. I'm not sure though if the file or parts of it could get to the swap file in some situation as the file is actually created to the memory.

    There seems to be also undocumented functions mxSerialize and mxDeserialize in libmx that you could use eg. by loadlibrary/calllib functions directly from matlab or by a wrapper mex. A bit of Googling revealed that the signature for these functions should be

    mxArray* mxSerialize(const mxArray*);
    mxArray* mxDeserialize(const void*, size_t);
    

    and some tests revealed that mxSerialize() gets the matlab variable as argument and returns a serialized bytes as uint8 array. The mxDeserialize() transforms this uint8 array (1st argument) back to matlab object as return value. The 2nd argument for mxDeserialize seems to be the number of elements in the 1st argument. Using these undocumented functions is not though guaranteed to work in future because TMW might change the API.

    0 讨论(0)
  • 2020-12-09 23:16

    I am also interested on this problem. I found some things, but nothing works:

    • matlab save stdio you find this hidden feature, but it doesn't work
    • engGetArray/engPutArray "This routine allows you to copy a variable out of the workspace."

    Look at MAT files specification, maybe we can reproduce matlab serialization with a Mex file:

    • http://www.eiscat.se/groups/Documentation/UserGuides/matlab4.pdf

    Update:

    I found something very interesting: run in Matlab console this command

    edit([matlabroot '/extern/examples/eng_mat/matcreat.c']);
    

    or this

    edit([matlabroot '/extern/examples/eng_mat/matcreat.cpp']);
    

    This is the documentation, how to compile it: http://www.mathworks.com/help/techdoc/matlab_external/f14500.html

    In my opinion it should be feasible to use STDOUT in pmat = matOpen(file, "w"); command.

    0 讨论(0)
  • 2020-12-09 23:19

    For HG objects, you can intercept the save processing via the internal (modifiable) *.m files that are explained here: http://undocumentedmatlab.com/blog/handle2struct-struct2handle-and-matlab-8/

    0 讨论(0)
  • 2020-12-09 23:21

    EDIT: (based on comments) Hmm, I guess my old answer doesn't help much then. I don't know how you would go about intercepting the bytestream, but I suppose one option you have (which is admittedly a little bit of a kludge) is to just let the SAVE function create the file then immediately read the data from the file byte-wise, process it, and write it back to the file. Something like:

    save('workspace.mat');
    fid = fopen('workspace.mat','r');
    byteData = fread(fid,inf,'*uint8');
    fclose(fid);
    %# ... Process byteData here ...
    fid = fopen('workspace.mat','w');
    fwrite(fid,byteData,'uint8');
    fclose(fid);
    

    Old answer:

    For user-defined class objects, I believe what you're looking for is embodied in the overloaded SAVEOBJ and LOADOBJ methods, which are called on an object before saving it to or loading it from a file. When saving or loading objects to or from .MAT files, you can use these methods to modify the save/load process so that the objects can be formatted in different ways. However, I don't think you can do this for built-in data types, only for user-defined objects.

    0 讨论(0)
  • 2020-12-09 23:22

    After brooding on this for several months, I'm going to say, no, this is not possible. At least, not without hardcore non-portable binary/ELF hacking.

    0 讨论(0)
提交回复
热议问题