Skip to content

Commit

Permalink
fix 2G limit bug
Browse files Browse the repository at this point in the history
  • Loading branch information
wuyongzheng committed Aug 15, 2012
1 parent 9f07279 commit e98d732
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 213 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
map-*.img
*.img
*.o
*.obj
*.exe
8 changes: 4 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
CC = gcc
CFLAGS = -Wall
CFLAGS = -Wall -D_FILE_OFFSET_BITS=64
GIMGLIB_SOURCES = gimglib.c util.c sf_typ.c sf_mps.c sf_tre.c sf_rgn.c sf_lbl.c sf_net.c sf_nod.c sf_gmp.c
GIMGLIB_OBJS = $(GIMGLIB_SOURCES:.c=.o)

Expand All @@ -12,11 +12,11 @@ gimgfixcmd: gimgfixcmd.o cmdlib.o $(GIMGLIB_OBJS)

gimgxor: gimgxor.o

gimgunlock: gimgunlock.o
gimgunlock: gimgunlock.o util_indep.o

gimgch: gimgch.o
gimgch: gimgch.o util_indep.o

gimgextract: gimgextract.o
gimgextract: gimgextract.o util_indep.o

cmdc: cmdc.o
$(CC) -o $@ $< -lm
Expand Down
69 changes: 7 additions & 62 deletions gimgch.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@
#include <assert.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include "util_indep.h"

struct header_struct {
const char *imgpath;
char subfile[16];
off_t subfile_offset; /* abs offset */
int subfile_size;
int header_rel_offset; /* 0 if it's OF */
int header_size;
int subfile_offset; /* abs offset */
int subfile_size;
unsigned char *header;
char id[4];
};
Expand All @@ -21,56 +22,6 @@ static struct header_struct *headers[MAX_HEADERS];
static int header_num = 0;


static int read_byte_at (FILE *fp, unsigned long offset)
{
if (fseek(fp, offset, SEEK_SET)) {
perror(NULL);
exit(1);
}
return getc(fp);
}

static int read_2byte_at (FILE *fp, unsigned long offset)
{
int n = 0;
if (fseek(fp, offset, SEEK_SET)) {
perror(NULL);
exit(1);
}
if (fread(&n, 2, 1, fp) != 1) {
perror(NULL);
exit(1);
}
return n;
}

static unsigned int read_4byte_at (FILE *fp, unsigned long offset)
{
int n = 0;
if (fseek(fp, offset, SEEK_SET)) {
perror(NULL);
exit(1);
}
if (fread(&n, 4, 1, fp) != 1) {
perror(NULL);
exit(1);
}
return n;
}

static void read_bytes_at (FILE *fp, unsigned long offset,
unsigned char *buffer, int size)
{
if (fseek(fp, offset, SEEK_SET)) {
perror(NULL);
exit(1);
}
if (fread(buffer, size, 1, fp) != 1) {
perror(NULL);
exit(1);
}
}

static void display_headers (int line_columns)
{
int i, ptr, bytes_pre_line;
Expand Down Expand Up @@ -166,7 +117,7 @@ static void display_headers (int line_columns)
}

static int add_header(FILE *fp, const char *imgpath, const char *sf_name,
int subfile_offset, int subfile_size, int header_rel_offset)
off_t subfile_offset, int subfile_size, int header_rel_offset)
{
struct header_struct *header =
(struct header_struct *)malloc(sizeof(struct header_struct));
Expand Down Expand Up @@ -195,13 +146,6 @@ static int add_header(FILE *fp, const char *imgpath, const char *sf_name,
return 0;
}

#define errexit(...) \
do { \
printf(__VA_ARGS__); \
if (fp) \
fclose(fp); \
return 1; \
} while (0)
static int read_header (const char *imgpath, const char *subfile_name_pattern,
int match_maximum)
{
Expand All @@ -223,7 +167,7 @@ static int read_header (const char *imgpath, const char *subfile_name_pattern,

fatstart = read_byte_at(fp, 0x40) == 0 ? 3 : read_byte_at(fp, 0x40);
if (read_4byte_at(fp, 0x40c) == 0) { // use root dir. assume it's the first file
unsigned long offset = fatstart * 512;
off_t offset = fatstart * 512;
if (read_byte_at(fp, offset) != 1 ||
read_byte_at(fp, offset + 0x1) != ' ' ||
read_byte_at(fp, offset + 0x9) != ' ')
Expand All @@ -240,7 +184,8 @@ static int read_header (const char *imgpath, const char *subfile_name_pattern,
}

for (fatcount = fatstart; fatcount < fatend; fatcount ++) {
unsigned long offset = fatcount * 512, subfile_offset, subfile_size;
off_t offset = fatcount * 512, subfile_offset;
int subfile_size;
char sf_name[16];

if (read_byte_at(fp, offset) != 1)
Expand Down
57 changes: 4 additions & 53 deletions gimgextract.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,57 +2,7 @@
#include <stdlib.h>
#include <string.h>
#include <assert.h>

int read_byte_at (FILE *fp, unsigned long offset)
{
if (fseek(fp, offset, SEEK_SET)) {
perror(NULL);
exit(1);
}
return getc(fp);
}

int read_2byte_at (FILE *fp, unsigned long offset)
{
int n = 0;
if (fseek(fp, offset, SEEK_SET)) {
perror(NULL);
exit(1);
}
if (fread(&n, 2, 1, fp) != 1) {
perror(NULL);
exit(1);
}
return n;
}

unsigned int read_4byte_at (FILE *fp, unsigned long offset)
{
int n = 0;
if (fseek(fp, offset, SEEK_SET)) {
perror(NULL);
exit(1);
}
if (fread(&n, 4, 1, fp) != 1) {
perror(NULL);
exit(1);
}
return n;
}

void read_bytes_at (FILE *fp, unsigned long offset, unsigned char *buffer, int size)
{
if (fseek(fp, offset, SEEK_SET)) {
perror(NULL);
exit(1);
}
if (fread(buffer, size, 1, fp) != 1) {
perror(NULL);
exit(1);
}
}

#define errexit(...) do {printf(__VA_ARGS__); return 1;} while (0)
#include "util_indep.h"

int main (int argc, char *argv[])
{
Expand All @@ -78,7 +28,7 @@ int main (int argc, char *argv[])

fatstart = read_byte_at(infp, 0x40) == 0 ? 3 : read_byte_at(infp, 0x40);
if (read_4byte_at(infp, 0x40c) == 0) { // use root dir. assume it's the first file
unsigned long offset = fatstart * 512;
off_t offset = fatstart * 512;
if (read_byte_at(infp, offset) != 1 ||
read_byte_at(infp, offset + 0x1) != ' ' ||
read_byte_at(infp, offset + 0x9) != ' ')
Expand All @@ -93,7 +43,8 @@ int main (int argc, char *argv[])
}

for (fatcount = fatstart; fatcount < fatend; fatcount ++) {
unsigned long offset = fatcount * 512, sf_offset, sf_size;
off_t offset = fatcount * 512, sf_offset;
int sf_size;
char sf_name[16];
FILE *outfp;

Expand Down
80 changes: 9 additions & 71 deletions gimgunlock.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,16 @@
#include <stdlib.h>
#include <string.h>
#include <assert.h>

#define errexit(...) do {printf(__VA_ARGS__); exit(1);} while (0)
#include "util_indep.h"

struct patch_struct {
unsigned long offset;
unsigned long size;
unsigned char *data;
struct patch_struct *next;
off_t offset;
unsigned long size;
};


void hexdump (const unsigned char *data, int size)
{
while (size -- > 0)
printf("%02x", *(data ++));
printf("\n");
}

void unlockml (unsigned char *dst, const unsigned char *src,
int size, unsigned int key)
{
Expand Down Expand Up @@ -53,70 +45,16 @@ struct patch_struct *prepend_patch (struct patch_struct *patch_list, unsigned lo
{
struct patch_struct *new_patch =
(struct patch_struct *)malloc(sizeof(struct patch_struct) + size);
if (new_patch == NULL)
errexit("out of memory\n");
assert(new_patch != NULL);
memset(new_patch, 0, sizeof(struct patch_struct) + size);
new_patch->size = size;
new_patch->data = (unsigned char *)new_patch + sizeof(struct patch_struct);
new_patch->next = patch_list;
return new_patch;
}

unsigned int read_byte_at (FILE *fp, unsigned long offset)
{
int c;
if (fseek(fp, offset, SEEK_SET)) {
perror(NULL);
exit(1);
}
c = getc(fp);
if (c == EOF)
errexit("Unexpected EOF\n");
return c;
}

unsigned int read_2byte_at (FILE *fp, unsigned long offset)
{
unsigned char buff[2];
if (fseek(fp, offset, SEEK_SET)) {
perror(NULL);
exit(1);
}
if (fread(&buff, 2, 1, fp) != 1) {
perror(NULL);
exit(1);
}
return (buff[1] << 8) | buff[0];
}

unsigned int read_4byte_at (FILE *fp, unsigned long offset)
{
unsigned char buff[4];
if (fseek(fp, offset, SEEK_SET)) {
perror(NULL);
exit(1);
}
if (fread(&buff, 4, 1, fp) != 1) {
perror(NULL);
exit(1);
}
return (buff[3] << 24) | (buff[2] << 16) | (buff[1] << 8) | buff[0];
}

void read_bytes_at (FILE *fp, unsigned long offset, unsigned char *buffer, int size)
{
if (fseek(fp, offset, SEEK_SET)) {
perror(NULL);
exit(1);
}
if (fread(buffer, size, 1, fp) != 1) {
perror(NULL);
exit(1);
}
}

struct patch_struct *unlock_tre (FILE *fp, struct patch_struct *patch,
unsigned long base, unsigned long header)
off_t base, off_t header)
{
unsigned int key, mloff, mlsize;
unsigned char encml[64]; /* at most 16 levels */
Expand Down Expand Up @@ -169,7 +107,7 @@ struct patch_struct *create_patch (FILE *fp)

fatstart = read_byte_at(fp, 0x40) == 0 ? 3 : read_byte_at(fp, 0x40);
if (read_4byte_at(fp, 0x40c) == 0) { // use root dir. assume it's the first file
unsigned long offset = fatstart * 512;
off_t offset = fatstart * 512;
if (read_byte_at(fp, offset) != 1 ||
read_byte_at(fp, offset + 0x1) != ' ' ||
read_byte_at(fp, offset + 0x9) != ' ')
Expand All @@ -186,7 +124,7 @@ struct patch_struct *create_patch (FILE *fp)
}

for (fatcount = fatstart; fatcount < fatend; fatcount ++) {
unsigned long offset = fatcount * 512;
off_t offset = fatcount * 512;
char subfile_name[16];

if (read_byte_at(fp, offset) != 1)
Expand All @@ -205,7 +143,7 @@ struct patch_struct *create_patch (FILE *fp)
if (read_byte_at(fp, offset + 0x9) == 'G' &&
read_byte_at(fp, offset + 0xa) == 'M' &&
read_byte_at(fp, offset + 0xb) == 'P') {
unsigned long tre_offset;
off_t tre_offset;
offset = read_2byte_at(fp, offset + 0x20) * block_size;
if (read_byte_at(fp, offset + 0x2) != 'G' ||
read_byte_at(fp, offset + 0x9) != 'G' ||
Expand Down Expand Up @@ -262,7 +200,7 @@ void apply_patch (FILE *fp, struct patch_struct *patch_list)
struct patch_struct *patch;

for (patch = patch_list; patch != NULL; patch = patch->next) {
if (fseek(fp, patch->offset, SEEK_SET)) {
if (myfseek64(fp, patch->offset)) {
perror(NULL);
return;
}
Expand Down
27 changes: 5 additions & 22 deletions makefile.nmake
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,14 @@ gimgxor.exe: gimgxor.c
$(CC) $(CFLAGS) gimgxor.c

gimgunlock.exe: gimgunlock.c
$(CC) $(CFLAGS) gimgunlock.c
$(CC) $(CFLAGS) gimgunlock.c util_indep.c

gimgdh.exe: gimgdh.c
$(CC) $(CFLAGS) gimgdh.c

gimginfo.obj:
$(CC) $(CFLAGS) /c gimginfo.c

gimglib.obj:
$(CC) $(CFLAGS) /c gimglib.c

util.obj:
$(CC) $(CFLAGS) /c util.c

sf_tre.obj:
$(CC) $(CFLAGS) /c sf_tre.c

sf_typ.obj:
$(CC) $(CFLAGS) /c sf_typ.c

sf_mps.obj:
$(CC) $(CFLAGS) /c sf_mps.c
gimgch.exe: gimgch.c
$(CC) $(CFLAGS) gimgch.c util_indep.c

clean:
-del /F $(OBJS) gimginfo.exe
-del /F gimgxor.exe gimgxor.obj
-del /F gimgunlock.exe gimgunlock.obj
-del /F gimgdh.exe gimgdh.obj
-del /F gimgch.exe gimgch.obj
-del /F util_indep.obj
Loading

0 comments on commit e98d732

Please sign in to comment.