从原始文件重建文件,在RAM中重建差异文件 [英] Rebuild file from original file and differences file in RAM

查看:119
本文介绍了从原始文件重建文件,在RAM中重建差异文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的朋友们,我正在尝试实现一个重建二进制文件的程序,从旧文件和差异文件开始。这项工作基于巧妙的论文Olin Percival,可执行代码的Naive差异,http://www.daemonology.net/bsdiff/,2003。我想在微控制器ARM M4上使用rebuilder(bspatch)。我试图通过对简单向量的操作来消除磁盘访问。操作并非无足轻重,因为Olin程序很复杂!当然除了文件指针之外,Olin程序还使用函数来访问文件,例如必须替换的fgetc FSEEK。首先,我在Visual Studio中使用C语言,当程序在PC上工作时,我将在ARM M4上移动它。

首先,我添加char的静态向量,只要文件在输入中,然后我将文件复制粘贴到矢量中。从这时起,我不再想要进行磁盘访问。但是,该程序不断进行不必要的磁盘访问。



有没有人用char矢量替换文件指针只使用RAM?



查看Olin的原始程序(在链接中找到)有一个Visual Studio项目。



谢谢你,快乐幸福快乐新年

Stefano Fredella



Dear friends, I am trying to implement a program that reconstructs a binary file, starting from the old file and a file of differences. The work is based on the ingenious thesis Olin Percival, Naive differences of executable code, http://www.daemonology.net/bsdiff/ , 2003. I want to use the rebuilder (bspatch) on a microcontroller ARM M4. I'm trying to eliminate disk access with operations on simple vectors. The operation is not trivial because the Olin program is complicated! Of course in addition to the file pointer, the Olin program uses functions to access the file, such as fgetc FSEEK that must be replaced. To start I am working in C in visual studio and when the program will work on the PC i will move it on ARM M4.
To begin with, I add static vectors of char as long as the files in input,then I copy-paste the files in to the vectors. From this point on I no longer want to make disk accesses. However, the program keeps making unwanted disk access.

Has anyone ever replaced a file pointer with a vector of char to use only RAM?

Look the original program of Olin (found in the link) there is a Visual Studio project.

Thank You and Happy Happy Happy New Year
Stefano Fredella

Ok, I try to explain one specific problem:
 
I have these two functions and i do not want pass FILE* f. I want pass a chars vector 
How can I change that?
 
<pre lang="c++">
typedef void BZFILE;
 
BZFILE* BZ_API(BZ2_bzReadOpen) 
                   ( int*  bzerror, 
                     FILE* f, 
                     int   verbosity,
                     int   small,
                     void* unused,
                     int   nUnused )
{
   bzFile* bzf = NULL;
   int     ret;
 
   BZ_SETERR(BZ_OK);
 
   if (f == NULL || 
       (small != 0 && small != 1) ||
       (verbosity < 0 || verbosity > 4) ||
       (unused == NULL && nUnused != 0) ||
       (unused != NULL && (nUnused < 0 || nUnused > BZ_MAX_UNUSED)))
      { BZ_SETERR(BZ_PARAM_ERROR); return NULL; };
 
   if (ferror(f))
      { BZ_SETERR(BZ_IO_ERROR); return NULL; };
 
   bzf = malloc ( sizeof(bzFile) );
   if (bzf == NULL) 
      { BZ_SETERR(BZ_MEM_ERROR); return NULL; };
 
   BZ_SETERR(BZ_OK);
 
   bzf->initialisedOk = False;
   bzf->handle        = f;
   bzf->bufN          = 0;
   bzf->writing       = False;
   bzf->strm.bzalloc  = NULL;
   bzf->strm.bzfree   = NULL;
   bzf->strm.opaque   = NULL;
 
   while (nUnused > 0) {
      bzf->buf[bzf->bufN] = *((UChar*)(unused)); bzf->bufN++;
      unused = ((void*)( 1 + ((UChar*)(unused))  ));
      nUnused--;
   }
 
   ret = BZ2_bzDecompressInit ( &(bzf->strm), verbosity, small );
   if (ret != BZ_OK)
      { BZ_SETERR(ret); free(bzf); return NULL; };
 
   bzf->strm.avail_in = bzf->bufN;
   bzf->strm.next_in  = bzf->buf;
 
   bzf->initialisedOk = True;
   return bzf;   
}
 
int BZ_API(BZ2_bzRead) 
           ( int*    bzerror, 
             BZFILE* b, 
             void*   buf, 
             int     len )
{
   Int32   n, ret;
   bzFile* bzf = (bzFile*)b;
 
   BZ_SETERR(BZ_OK);
 
   if (bzf == NULL || buf == NULL || len < 0)
      { BZ_SETERR(BZ_PARAM_ERROR); return 0; };
 
   if (bzf->writing)
      { BZ_SETERR(BZ_SEQUENCE_ERROR); return 0; };
 
   if (len == 0)
      { BZ_SETERR(BZ_OK); return 0; };
 
   bzf->strm.avail_out = len;
   bzf->strm.next_out = buf;
 
   while (True) {
 
      if (ferror(bzf->handle)) 
         { BZ_SETERR(BZ_IO_ERROR); return 0; };
 
      if (bzf->strm.avail_in == 0 && !myfeof(bzf->handle)) {
         n = fread ( bzf->buf, sizeof(UChar), 
                     BZ_MAX_UNUSED, bzf->handle );
         if (ferror(bzf->handle))
            { BZ_SETERR(BZ_IO_ERROR); return 0; };
         bzf->bufN = n;
         bzf->strm.avail_in = bzf->bufN;
         bzf->strm.next_in = bzf->buf;
      }
 
      ret = BZ2_bzDecompress ( &(bzf->strm) );
 
      if (ret != BZ_OK && ret != BZ_STREAM_END)
         { BZ_SETERR(ret); return 0; };
 
      if (ret == BZ_OK && myfeof(bzf->handle) && 
          bzf->strm.avail_in == 0 && bzf->strm.avail_out > 0)
         { BZ_SETERR(BZ_UNEXPECTED_EOF); return 0; };
 
      if (ret == BZ_STREAM_END)
         { BZ_SETERR(BZ_STREAM_END);
           return len - bzf->strm.avail_out; };
      if (bzf->strm.avail_out == 0)
         { BZ_SETERR(BZ_OK); return len; };
 
   }
 
   return 0; /*not reached*/
}
static Bool myfeof ( FILE* f )
{
   Int32 c = fgetc ( f );
   if (c == EOF) return True;
   ungetc ( c, f );
   return False;
}
</pre>

推荐答案

抽象。



你在代码和实际数据(文件或内存)之间需要一个抽象层。您需要编写此抽象层的两个实现 - 一个用于文件,另一个用于内存。您的评论表明需要随机访问,因此该层需要支持搜索。



这在C ++中更容易完成。如果你只限于C,请使用带有函数指针成员的结构 - 一个用于打开,关闭,读取,写入和查找。



Abstraction.

You need an abstraction layer between the code and the actual data (file or memory). You'll need to write two implementations of this abstraction layer - one for a file and the other for memory. Your comments state the need for random access so this layer will need to support seek.

This is much more easily done in C++. If you are limited to C, use a struct with function pointer members - one each for open, close, read, write, and seek.

typedef void *handle_t;

struct indirect
{
    bool (*open)(const char *name, handle_t &handle);
    void (*close)(handle_t handle);
    bool (*read)(handle_t handle, unsigned char *data, size_t size, size_t *copied);
    bool (*write)(handle_t handle, const unsigned char *data, size_t size, size_t *copied);
    bool (*seek)(handle_t handle, long seek);
};





接下来,您将编写五个调用标准C FILE * API的函数。让它首先工作,因为它将是验证你没有破坏任何东西的最简单方法。



最后,编写五个使用内存的函数 - 可能是堆 - 要做相同的。



Next, you'd write five functions that call the standard C FILE * API. Get that working first as it will be the easiest way to verify you have not broken anything.

Finally, write five functions that use memory - probably the heap - to do the same.


您是否解决了上述问题?
hi did you resolved above issue ?


这篇关于从原始文件重建文件,在RAM中重建差异文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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