代码拉取完成,页面将自动刷新
#if 0
/*
* main.c
*
* Created on: 2023锟斤拷4锟斤拷14锟斤拷
* Author: hello
*/
#include "bsdiff.h"
#include "bspatch.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int getfilesize(const char* fname);
int loadfile(const char* filename, uint8_t* buf);
int storefile(const char* filename, uint8_t* buf, uint32_t len);
int readfile(const char* fname, uint32_t offset, void* buf, uint32_t len);
int writefile(const char* fname, uint32_t offset, void* buf, uint32_t len);
#define debug(fmt,...) do{printf(fmt, ##__VA_ARGS__);fflush(stdout);}while(0)
#include <stdio.h>
#include <stdlib.h>
static int writepatch(struct bsdiff_stream* stream, const void* buffer, int size)
{
FILE* fp = (FILE*)stream->opaque;
if(fwrite(buffer, 1, size, fp) != size)
return -1;
return 0;
}
void genpatch()
{
uint8_t *old,*new;
off_t oldsize,newsize;
uint8_t buf[8];
FILE * pf;
struct bsdiff_stream stream;
stream.malloc = malloc;
stream.free = free;
stream.write = writepatch;
oldsize = getfilesize("old.bin");
newsize = getfilesize("new.bin");
old = malloc(oldsize + 1);
new = malloc(newsize + 1);
if(loadfile("old.bin", old) < 0 || loadfile("new.bin", new) < 0)
{
debug("Failed to load file \n");
goto __exit;
}
/* Create the patch file */
pf = fopen("diff.bin", "w+");
if(!pf)
{
debug("Failed to create patch file \n");
goto __exit;
}
/* Write header (signature+newsize)*/
offtout(newsize, buf);
if (fwrite("ENDSLEY/BSDIFF43", 16, 1, pf) != 1 || fwrite(buf, sizeof(buf), 1, pf) != 1)
{
debug("Failed to write header \n");
goto __exit;
}
stream.opaque = pf;
if (bsdiff(old, oldsize, new, newsize, &stream))
{
debug("Failed to bsdiff \n");
goto __exit;
}
debug(" generate patch successfully ! \n");
__exit:
fclose(pf);
/* Free the memory we used */
free(old);
free(new);
}
static int readpatch(const struct bspatch_stream* stream, void* buffer, int length)
{
FILE* fp = (FILE*)stream->opaque;
if(fread(buffer, 1, length, fp) != length)
return -1;
return 0;
}
void restorenew()
{
FILE * f;
uint8_t header[24];
uint8_t *old, *new;
int64_t oldsize, newsize;
struct bspatch_stream stream;
/* Open patch file */
if ((f = fopen("diff.bin", "r")) == NULL)
{
debug(" Failed to open patch file ");
goto __exit;
}
/* Read header */
if (fread(header, 1, 24, f) != 24)
{
debug(" Failed to read patch header ");
goto __exit;
}
/* Check for appropriate magic */
if (memcmp(header, "ENDSLEY/BSDIFF43", 16) != 0)
{
debug(" Corrupt patch file ");
goto __exit;
}
/* Read lengths from header */
newsize=offtin(header+16);
if(newsize<0)
{
debug(" Corrupt patch file ");
goto __exit;
}
oldsize = getfilesize("old.bin");
if(oldsize < 0)
{
debug("Failed to get old file size \n");
goto __exit;
}
old = malloc(oldsize + 1);
new = malloc(newsize + 1);
if(loadfile("old.bin", old) < 0)
{
debug("Failed to read old file \n");
goto __exit;
}
stream.read = readpatch;
stream.opaque = f;
if (bspatch(old, oldsize, new, newsize, &stream))
{
debug(" Failed to bspatch \n");
goto __exit;
}
if(storefile("new-restore.bin", new, newsize) < 0)
{
debug(" Failed to save new file \n");
goto __exit;
}
debug(" restore new file successfully ! \n");
__exit:
fclose(f);
free(new);
free(old);
}
int main(int argc,char *argv[])
{
genpatch();
restorenew();
}
int loadfile(const char* filename, uint8_t* buf)
{
int filesize = 0, nread = 0;
FILE* fp = NULL;
fp = fopen(filename, "rb");
if(!fp)
return -1;
fseek(fp, 0, SEEK_END);
filesize = ftell(fp);
fseek(fp, 0, SEEK_SET);
if(filesize > 0)
nread = fread(buf, 1, filesize, fp);
fclose(fp);
return nread;
}
int storefile(const char* filename, uint8_t* buf, uint32_t len)
{
int nwrite = 0;
FILE* fp = NULL;
fp = fopen(filename, "wb+");
if(!fp)
{
return -1;
}
nwrite = fwrite(buf, 1, len, fp);
fclose(fp);
return nwrite;
}
int readfile(const char* fname, uint32_t offset, void* buf, uint32_t len)
{
int nread = 0;
FILE* fp = NULL;
fp = fopen(fname, "rb");
if(!fp)
return -1;
fseek(fp, offset, SEEK_SET);
nread = fread(buf, 1, len, fp);
fclose(fp);
return nread;
}
int writefile(const char* fname, uint32_t offset, void* buf, uint32_t len)
{
int nwrite = 0;
FILE* fp = NULL;
fp = fopen(fname, "ab");
if(!fp)
return -1;
fseek(fp, offset, SEEK_SET);
nwrite = fwrite(buf, 1, len, fp);
fclose(fp);
return nwrite;
}
int getfilesize(const char* fname)
{
int len = 0;
FILE* fp = NULL;
fp = fopen(fname, "r");
if(!fp)
return -1;
fseek(fp, 0, SEEK_END);
len = ftell(fp);
fclose(fp);
return len;
}
#endif
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。