[질문] 왜 파일이 수정이 안 될까요?
글쓴이: naisr00t / 작성시간: 목, 2004/05/20 - 5:15오후
몇 일 째 고생입니다.
파일을 카피 후, 파일의 내용을 수정하려고 합니다.
그런데, 디버깅 때는, 수정한 내용이 보여서 완전히 수정되었다고 생각했습니다.
그런데, 실제로 파일을 열어보니, 그대로입니다.
소스파일을 첨부합니다.
아울러 소스의 내용은 다음과 같습니다.
개발환경은 VC++ 입니다. 수정해야 할 파일은 01.jar 파일입니다.
디렉토리 엔트리 부분에 OK 라고 시작되는 부분을 PK 라고 수정해야 되는 것인데...
#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <io.h>
#include "MHtypes.h"
#define PERMS 0666
#define BUFSIZE 512
#define SIZEOFCENTRALEND 22
#define CENTRALENDSIGNATURE 0x06054b50
/*
* Macros for getting central directory header (CEN) fields
*/
#define CENHOW 10 /* compression method */
#define CENTIM 12 /* file modification time (DOS format) */
#define CENSIZ 20 /* compressed size */
#define CENLEN 24 /* uncompressed size */
#define CENNAM 28 /* length of filename */
#define CENEXT 30 /* length of extra field */
#define CENCOM 32 /* file comment length */
#define CENOFF 42 /* offset of local header */
#define CENHDRSIZ 46
#define READ16(ptr, pos) (M_Int16)(*((M_Uint8*)ptr+pos) | (*((M_Uint8*)ptr+pos+1) << 8))
#define READ32(ptr, pos) (M_Int32)(*((M_Uint8*)ptr+pos) | (*((M_Uint8*)ptr+pos+1) << 8) | (*((M_Uint8*)ptr+pos+2) << 16) | (*((M_Uint8*)ptr+pos+3) << 24))
//error print
void fatal(char *error_name)
{
perror(error_name);
exit(1);
}
int JarFile_getJarInfo(char* testFile1, char* testFile2, int* offset,
int* compressed_size, int* uncompressd_size, int* compress_meth)
{
char buf[SIZEOFCENTRALEND]; //SIZEOFCENTRALEND = 22
char fileName[256];
char* cBuf;
int cDirSize;
int cDirOff;
int entryCount;
int i;
int res = 0;
int fnLen;
int fp;
int fd;
int cnt = 0;
int srcFD, destFD;
int readNumber;
char buff[BUFSIZE];
//MV_DECLARE_REWIND_VAR(rwnPos);
//rewind(fd);
if ((srcFD = _open(testFile1, _O_RDONLY | _O_BINARY)) < 0) {
fatal("Read Opend\n");
}
if ((destFD = _open(testFile2, _O_RDWR | _O_CREAT | _O_TRUNC | _O_BINARY, 0666)) < 0) {
fatal("Write Opened\n");
}
while ((readNumber = _read(srcFD, buff, BUFSIZE)) > 0) {
if (_write(destFD, buff, readNumber) < readNumber) {
fatal("Success Writed");
}
}
printf("%s close success - #%d\n", testFile1, _close(srcFD));
fd = destFD;
if ((fp = lseek(fd, -SIZEOFCENTRALEND, SEEK_END)) < 0 ) {
return(-5);
}
res = _read(fd, buf, SIZEOFCENTRALEND);
//readFD = read(fd, buf, SIZEOFCENTRALEND);
if (READ32(buf, 0) != CENTRALENDSIGNATURE ) {
return(-6);
}
entryCount = READ16(buf, 10);
cDirSize = READ32(buf, 12);
cDirOff = READ32(buf, 16);
//MV_MEM_SET_REWIND_POS(rwnPos);
//lseek(fd, 0L, 0);
lseek(fd, cDirOff, SEEK_SET);
//cBuf = memCheckCalloc(cDirSize);
cBuf = (char *)malloc(cDirSize);
if (cBuf == 0) {
printf("Out of Memory\n");
return(-7); // outOfMemory
}
memset(cBuf,0x0,cDirSize);
_read(fd, cBuf, cDirSize);
/*
if ((res = read(fd, cBuf, cDirSize)) != cDirSize) {
printf("res = %d\n", res);
MV_MEM_REWIND(rwnPos);
lseek(fd, 0L, 0); //rewind
return(-1);
}
*/
for (i = 0; i < entryCount; i++) {
*compress_meth = READ16(cBuf, CENHOW); //10
*compressed_size = READ32(cBuf, CENSIZ); //22
*uncompressd_size = READ32(cBuf, CENLEN); //24
if ( *uncompressd_size != 0 ) {
fnLen = READ16(cBuf, CENNAM);
memcpy(fileName, cBuf+CENHDRSIZ, fnLen);
fileName[fnLen] = 0;
*offset = READ32(cBuf, CENOFF);
/*
if (!strcmp(name,fileName)) {
//MV_MEM_REWIND(rwnPos);
return(0);
}
*/
}
cBuf += CENHDRSIZ + READ16(cBuf, CENNAM) + READ16(cBuf, CENEXT) + READ16(cBuf, CENCOM);
if (cBuf[0] == 'O' && cBuf[0 +1] == 'K') {
cBuf[0] = 'P';
//cnt++;
//printf("Bingo: count = %d\n", cnt);
}
}
//MV_MEM_REWIND(rwnPos);
//lseek(fd, cDirOff, SEEK_SET);
//버퍼에 바뀐 내용을 다시 쓰기 위해 파일 포인터를 다시 엔트리 부분에 놓는다.
if ((fp = lseek(fd, -SIZEOFCENTRALEND, SEEK_END)) < 0 ) {
return(-5);
}
res = _read(fd, buf, SIZEOFCENTRALEND);
if (READ32(buf, 0) != CENTRALENDSIGNATURE ) {
return(-6);
}
entryCount = READ16(buf, 10);
cDirSize = READ32(buf, 12);
cDirOff = READ32(buf, 16);
lseek(fd, cDirOff, SEEK_SET);
while ((readNumber = _read(destFD, cBuf, (int)cBuf)) > 0) {
if (_write(destFD, cBuf, readNumber) < readNumber) {
fatal("Success Writed");
}
}
printf("%s close success - #%d\n", testFile2, _close(fd));
return (1);
}
//copy program TEST
int main(int argc, char* argv[])
{
int srcFD = 0;
int destFD = 0;
int fd = 0;
int readNumber = 0;
int offset = 0;
int compressed_size = 0;
int uncompressd_size = 0;
int compress_meth = 0;
int res = 0;
//char buff[BUFSIZE];
void fatal(char*);
//Test File;
char* testFile1 = "01.jar";
char* testFile2 = "02.jar";
///*
if (argc < 2) {
printf("Usage: Command file1.jar file2.jar\n");
exit(0);
}
//*/
#ifdef WIN32
printf("Compiling ..... [WIN32]\n");
#endif
//test
//res = JarFile_getJarInfo(testFile1, testFile2, &offset, &compressed_size, &uncompressd_size, &compress_meth);
res = JarFile_getJarInfo(argv[1], argv[2], &offset, &compressed_size, &uncompressd_size, &compress_meth);
if (1 == res) {
printf("Copy Completed: Conversion Success\n", res);
} else {
printf("check number - #%d\n", res);
}
return(0);
}
File attachments:
| 첨부 | 파일 크기 |
|---|---|
| 411.81 KB |
Forums:


* 참고1) man lseek2) lseek()예제를 많이 본다.
* 참고
1) man lseek
2) lseek()예제를 많이 본다.
3) 테스트도 해본다
4) 또해본다.
------------------ P.S. --------------
지식은 오픈해서 검증받아야 산지식이된다고 동네 아저씨가 그러더라.
댓글 달기