是否有可能拦截matlab save()字节流 [英] Is it possible to intercept a matlab save() bytestream

查看:73
本文介绍了是否有可能拦截matlab save()字节流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在matlab中,可以使用matlab save()调用将matlab对象甚至整个工作区写入文件.我想截取字节流并在将其转到文件之前对其进行后处理,这可能吗?或者,可以指定写入字节流的文件描述符,而不是通常将save()调用中用作参数的文件名.

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 postprocess it before it goes to a file, is this possible? Alternatively, is it possible to specify the filedescriptor that the bytestream is written to instead of the filename that usually goes into the save() call as an argument.

请注意,我不是在寻找在matlab中写入文件的替代方法,我知道我可以fopen()文件并写入所需的内容,但是重点是我想(重新)使用对象保存调用内部的序列化,而无需再次发明自己的序列化.

Note that I'm not looking for an alternative way to write a file in matlab, I know I can fopen() a file and write whatever I want, but the point is that I want to (re)use the object serialization that is internal to the save call, not invent my own again.

对于load()调用,当然会出现一个模拟问题,但在那种情况下,在字节流进入反序列化过程之前就对其进行了拦截,但是我猜想save()是否有可能解决load()的问题问题自然会随之而来.

An analog question would of course arise for the load() call, but in that case intercepting the bytestream before it goes into the deserialization process, but I guess if it is possible for save() the solution to the load() problem will follow naturally.

一些说明:

  1. 我不是在寻找一种序列化matlab数据的新方法,它已经存在,整个练习的重点是在save()调用中使用现有的序列化,因此1)我不需要在更新版本的Matlab中开始为新类型的对象更新序列化代码,否则天堂禁止人们开始使用自定义OOP对象,并且2)我仍然可以轻松地使用现有代码来读取mat文件,例如scipy的支持用于mat文件.

  1. I'm not looking at a new way to serialize matlab data, it already exists and the whole point of the exercise is to use the existing serialization in the save() call so that 1) I don't need to start updating the serialization code for new types of objects in newer versions of matlab, or heaven forbid people start using custom OOP objects, and 2) I can still easily use existing code to read in mat files, such as for example scipy's support for mat files.

在进行后处理之前,流不能流到文件或其他任何东西,这是出于安全性的考虑,将流简单地写到文件中完全破坏了该目的.

The stream must not get out to a file or anything before post-processing, the idea is encryption for security, writing the stream out plain to a file completely undermines that purpose.

并发症:

  • 似乎在matlab的save函数中使用的功能不只是常规的顺序写入.检查库的目标代码,似乎使用matPutVariable(以前称为matPutArray)实现了保存功能,该功能将类型为mxArray*的给定变量写到以matOpen打开的类型为MATFile*的文件中.这里的问题是matPutVariable的描述中的以下文本:

  • It seems that the functionality used in the save function in matlab isn't just a regular sequential write. Examining the object code of the libraries it seems that the save function is implemented using matPutVariable (previously called matPutArray) which writes a given variable of type mxArray* out to a file of type MATFile* opened with matOpen. The problem here is the following text in the description of matPutVariable:

如果MAT文件中不存在mxArray,则该函数将其追加到末尾.如果文件中存在具有相同名称的mxArray,则该函数将通过重写文件来用新的mxArray替换现有的mxArray.

If the mxArray does not exist in the MAT-file, the function appends it to the end. If an mxArray with the same name exists in the file, the function replaces the existing mxArray with the new mxArray by rewriting the file.

这意味着matPutVariable函数将必须在文件中进行查找,显然在使用管道时将无法进行查找,因此,在使用此现有的序列化功能时,无法使用管道来实现我们对字节流的处理.

This means that the matPutVariable function will have to seek through the file, obviously seeking will not be possible when pipes are used, so using pipes to implement our processing of the bytestream is not possible when using this existing serialization functionality.

推荐答案

仔细研究了几个月之后,我要说,不,这是不可能的.至少,并非没有不可移植的二进制/ELF黑客攻击.

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.

这篇关于是否有可能拦截matlab save()字节流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆