请帮忙修改个程序,RAW文件转换成Bin文件

visio 2011-08-03 01:27:26
这是从网上Down下来的程序,不过只支持256K文件大小的转换
请大大们帮忙修改为文件大小不限制
谢谢

/*
raw2bin:
This program is used to convert a 256KB rom raw file into a bin format file only.
Please note, the program only convert the raw file and locate it to the address 0x83BC0000!!
Also, the converted bin file will be filled the end of file with 0xFF to the 4096 boundary, this is a special need

*/

#include <stdio.h>
#include <stdlib.h>

unsigned char memory[32768L*1024L];

int main(int argc,char *argv[])
{
FILE *fp;
FILE *rp1;
unsigned int i,j;
unsigned int len1,len2,len3;
unsigned int checksum;

unsigned int tmp;
unsigned int block_num,block_base;

if (argc != 3)
{
printf("Usage: %s input_raw_file output_bin_file\n",argv[0]);
return 1;
}

fp = fopen(argv[2],"wb");
if (fp==NULL)
{
printf("Can't open bin file %s to write!\n",argv[2]);
return 1;
}


rp1 = fopen(argv[1],"rb");
if (rp1==NULL)
{
printf("Can't open raw file %s to read!\n",argv[1]);
fclose(fp);
return 1;
}

fseek(rp1,0,SEEK_END);

len1 = ftell(rp1);

printf("len1=%d\n",len1);

if (len1%131072!=0)
printf("Warning! Input raw file not align at 4KB address....\n");

fseek(rp1,0,SEEK_SET);

fprintf(fp,"B000FF\n");

tmp = 0x83BC0000;
fwrite(&tmp,1,sizeof(unsigned int),fp);
tmp = len1;
fwrite(&tmp,1,sizeof(unsigned int),fp);

// write the blocks

block_num = len1 / 131072;
block_base = 0x83BC0000;

for (i=0;i<block_num;i++)
{
tmp = block_base + i*131072;
fwrite(&tmp,1,sizeof(unsigned int),fp);
tmp = 131072;
fwrite(&tmp,1,sizeof(unsigned int),fp);

fread(memory,1,131072,rp1);

checksum = 0;
for (j=0;j<131072;j++)
{
checksum = checksum + memory[j];
}

fwrite(&checksum,1,sizeof(unsigned int),fp);
fwrite(memory,1,131072,fp);

}

// write the last block

tmp = 0x0;
fwrite(&tmp,1,sizeof(unsigned int),fp);
tmp = 0x0;
fwrite(&tmp,1,sizeof(unsigned int),fp);
tmp = 0x0;
fwrite(&tmp,1,sizeof(unsigned int),fp);


// fill the end of file with 0xFF to the 4096 boundary
len1 = ftell(fp);

len2 = len1 % 4096;

if (len2 != 0)
{
len3 = 4096 - len2;

for (i=0;i<len3+1;i++)
memory[i] = 0xFF;

fwrite(memory,1,len3,fp);
}


fclose(rp1);
fclose(fp);

return 0;
}
...全文
341 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2011-08-04
  • 打赏
  • 举报
回复
仅供参考
#include <sys\stat.h>
#include <io.h>
#include <fcntl.h>
#include <share.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <string.h>
#define MAX_CLU_BYTES 65536
FILE *fo;
int fh;
__int64 offs,offs1;
__int64 rvi64;
int rv,wrv;
unsigned char buf[MAX_CLU_BYTES];
char ofn[_MAX_PATH];
char offstr[80];
void strcpybutcomma(char *t,char *s) {
char c;

while (1) {
c = *s++;
if (','!=c) *t++ = c;
if (0==c) break;
}
}
void main(int argc,char **argv) {
if (argc<3) {
printf("Copy File Tail.\n");
printf("Usage:\n");
printf(" cft filename.ext offset_begin[-offset_end]\n");
printf("Copy filename.ext offset_begin[-offset_end] to offset_begin[-offset_end]-filename.ext\n");
printf("Note: Byte at offset_end is NOT included.\n");
printf("Example:\n");
printf(" cft abc.rar 12345\n");
printf("Copy abc.rar offset 12345-end to 12345-abc.rar\n");
printf(" cft abc.rar 123-12345\n");
printf("Copy abc.rar offset 123-12345 to 123-12345-abc.rar\n");
printf(" cft abc.rar 0xAB-0xCD\n");
printf("Copy abc.rar offset 0xAB-0xCD to 0xAB-0xCD-abc.rar\n");
return;
}
strcpybutcomma(offstr,argv[2]);
rv=sscanf(offstr,"%I64i-%I64i",&offs,&offs1);
if (rv==0) {
printf("offset %s is not number\n",argv[2]);
return;
}
fh=_sopen(argv[1],_O_BINARY|_O_RDONLY|_O_RANDOM,_SH_DENYWR);
if (fh==-1) {
printf("_sopen %s errno=%d\n",argv[1],errno);
return;
}
if (rv==1) {
offs1=_filelengthi64(fh);
if (offs1==-1i64) {
printf("%I64=_filelengthi64 errno=%d\n",offs1,errno);
_close(fh);
return;
}
} else {//rv==2
if (offs1<offs) {
printf("%s offset_begin>offset_end error\n",argv[2]);
_close(fh);
return;
}
}
rvi64=_lseeki64(fh,offs,SEEK_SET);
if (rvi64!=offs) {
printf("%I64u=_lseeki64 %I64u errno=%d\n",rvi64,offs,errno);
_close(fh);
return;
}
sprintf(ofn,"%s-",offstr);
strcat(ofn,argv[1]);
fo=fopen(ofn,"wb");
if (fo==NULL) {
_close(fh);
printf("fopen %s error\n",ofn);
return;
}
cprintf("\n%I64u\r",offs);
while (1) {
rv=_read(fh,buf,(unsigned int)__min(offs1-offs,MAX_CLU_BYTES));
if (rv==0) break;//
if (rv<0) {
fclose(fo);
_close(fh);
printf("_read %s offset %I64u error\n",argv[1],offs);
return;
}
wrv=fwrite(buf,1,rv,fo);
if (wrv!=rv) {
fclose(fo);
_close(fh);
printf("fwrite %s error\n",ofn);
return;
} else {
offs+=rv;
cprintf("%I64u\r",offs);
if (offs>=offs1) break;//
}
}
fclose(fo);
_close(fh);
printf("Copy %s offset %s to %s OK.\n",argv[1],argv[2],ofn);
}
visio 2011-08-04
  • 打赏
  • 举报
回复
请高手出山
visio 2011-08-03
  • 打赏
  • 举报
回复
有没有不用分拆直接转换的办法
我不清楚拆分之后会对被转换文件有什么影响
多一个或少一个字节CRC都过不了了
無_1024 2011-08-03
  • 打赏
  • 举报
回复
用一个循环 吧源文件切分成256K大小的块 再把你现在的程序写成一个函数 每一块调用一次你这个函数
可以参考SHA1的那个字节切分 就是那些加解密的切分源字符串的操作
visio 2011-08-03
  • 打赏
  • 举报
回复
回楼上
这个理论上是可以
不过我不会:(
大熊猫侯佩 2011-08-03
  • 打赏
  • 举报
回复
你把被转换文件分拆成256kb的块,然后组合,可以吗?

69,373

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧