-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add FileIo class and change read_elf to use it.
- Loading branch information
Showing
8 changed files
with
422 additions
and
197 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
#ifndef NAKEN_ASM_VERSION_H | ||
#define NAKEN_ASM_VERSION_H | ||
|
||
#define VERSION "January 28, 2024" | ||
#define VERSION "February 6, 2024" | ||
|
||
#endif | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -101,6 +101,7 @@ COMMON_OBJS=" | |
Var.o" | ||
|
||
FILEIO_OBJS=" | ||
FileIo.o | ||
file.o | ||
read_amiga.o | ||
read_bin.o | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,234 @@ | ||
/** | ||
* naken_asm assembler. | ||
* Author: Michael Kohn | ||
* Email: [email protected] | ||
* Web: https://www.mikekohn.net/ | ||
* License: GPLv3 | ||
* | ||
* Copyright 2010-2024 by Michael Kohn | ||
* | ||
*/ | ||
|
||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <stdint.h> | ||
|
||
#include "FileIo.h" | ||
|
||
FileIo::FileIo() : fp(NULL) | ||
{ | ||
set_endian(false); | ||
} | ||
|
||
FileIo::~FileIo() | ||
{ | ||
close_file(); | ||
} | ||
|
||
int FileIo::open_for_writing(const char *filename) | ||
{ | ||
fp = fopen(filename, "wb"); | ||
if (fp == NULL) { return -1; } | ||
|
||
return 0; | ||
} | ||
|
||
int FileIo::open_for_reading(const char *filename) | ||
{ | ||
fp = fopen(filename, "rb"); | ||
if (fp == NULL) { return -1; } | ||
|
||
return 0; | ||
} | ||
|
||
void FileIo::close_file() | ||
{ | ||
if (fp != NULL) { fclose(fp); } | ||
fp = NULL; | ||
} | ||
|
||
void FileIo::set_endian(int value) | ||
{ | ||
if (value == FILE_ENDIAN_BIG) | ||
{ | ||
get_int16_func = get_int16_be; | ||
get_int32_func = get_int32_be; | ||
get_int64_func = get_int64_be; | ||
|
||
write_int16_func = write_int16_be; | ||
write_int32_func = write_int32_be; | ||
write_int64_func = write_int64_be; | ||
} | ||
else | ||
{ | ||
get_int16_func = get_int16_le; | ||
get_int32_func = get_int32_le; | ||
get_int64_func = get_int64_le; | ||
|
||
write_int16_func = write_int16_le; | ||
write_int32_func = write_int32_le; | ||
write_int64_func = write_int64_le; | ||
} | ||
} | ||
|
||
int FileIo::get_string_at_offset(char *data, int length, uint64_t offset) | ||
{ | ||
long marker = ftell(fp); | ||
int ptr = 0; | ||
|
||
fseek(fp, offset, SEEK_SET); | ||
|
||
while (true) | ||
{ | ||
int ch = getc(fp); | ||
if (ch == 0) { break; } | ||
data[ptr++] = ch; | ||
if (ptr == length - 1) { break; } | ||
} | ||
|
||
data[ptr] = 0; | ||
|
||
fseek(fp, marker, SEEK_SET); | ||
|
||
return 0; | ||
} | ||
|
||
int FileIo::get_bytes_at_offset(uint8_t *data, int length, uint64_t offset) | ||
{ | ||
long marker = ftell(fp); | ||
|
||
fseek(fp, offset, SEEK_SET); | ||
int n = fread(data, 1, length, fp); | ||
fseek(fp, marker, SEEK_SET); | ||
|
||
return n == length ? 0 : 1; | ||
} | ||
|
||
uint32_t FileIo::get_int16_le(FILE *in) | ||
{ | ||
uint32_t i; | ||
|
||
i = getc(in); | ||
i |= (getc(in) << 8); | ||
|
||
return i; | ||
} | ||
|
||
uint32_t FileIo::get_int32_le(FILE *in) | ||
{ | ||
uint32_t i; | ||
|
||
i = getc(in); | ||
i |= (getc(in) << 8); | ||
i |= (getc(in) << 16); | ||
i |= (getc(in) << 24); | ||
|
||
return i; | ||
} | ||
|
||
uint64_t FileIo::get_int64_le(FILE *in) | ||
{ | ||
uint64_t i; | ||
|
||
i = (uint64_t)getc(in); | ||
i |= ((uint64_t)getc(in) << 8); | ||
i |= ((uint64_t)getc(in) << 16); | ||
i |= ((uint64_t)getc(in) << 24); | ||
i |= ((uint64_t)getc(in) << 32); | ||
i |= ((uint64_t)getc(in) << 40); | ||
i |= ((uint64_t)getc(in) << 48); | ||
i |= ((uint64_t)getc(in) << 56); | ||
|
||
return i; | ||
} | ||
|
||
uint32_t FileIo::get_int16_be(FILE *in) | ||
{ | ||
uint32_t i; | ||
|
||
i = (getc(in) << 8); | ||
i |= getc(in); | ||
|
||
return i; | ||
} | ||
|
||
uint32_t FileIo::get_int32_be(FILE *in) | ||
{ | ||
uint32_t i; | ||
|
||
i = (getc(in) << 24); | ||
i |= (getc(in) << 16); | ||
i |= (getc(in) << 8); | ||
i |= getc(in); | ||
|
||
return i; | ||
} | ||
|
||
uint64_t FileIo::get_int64_be(FILE *in) | ||
{ | ||
uint32_t i; | ||
|
||
i = ((uint64_t)getc(in) << 56); | ||
i |= ((uint64_t)getc(in) << 48); | ||
i |= ((uint64_t)getc(in) << 40); | ||
i |= ((uint64_t)getc(in) << 32); | ||
i |= ((uint64_t)getc(in) << 24); | ||
i |= ((uint64_t)getc(in) << 16); | ||
i |= ((uint64_t)getc(in) << 8); | ||
i |= (uint64_t)getc(in); | ||
|
||
return i; | ||
} | ||
|
||
void FileIo::write_int16_le(FILE *out, uint32_t n) | ||
{ | ||
putc(n & 0xff, out); | ||
putc((n >> 8) & 0xff, out); | ||
} | ||
|
||
void FileIo::write_int32_le(FILE *out, uint32_t n) | ||
{ | ||
putc(n & 0xff, out); | ||
putc((n >> 8) & 0xff, out); | ||
putc((n >> 16) & 0xff, out); | ||
putc((n >> 24) & 0xff, out); | ||
} | ||
|
||
void FileIo::write_int64_le(FILE *out, uint64_t n) | ||
{ | ||
putc(n & 0xff, out); | ||
putc((n >> 8) & 0xff, out); | ||
putc((n >> 16) & 0xff, out); | ||
putc((n >> 24) & 0xff, out); | ||
putc((n >> 32) & 0xff, out); | ||
putc((n >> 40) & 0xff, out); | ||
putc((n >> 48) & 0xff, out); | ||
putc((n >> 56) & 0xff, out); | ||
} | ||
|
||
void FileIo::write_int16_be(FILE *out, uint32_t n) | ||
{ | ||
putc((n >> 8) & 0xff, out); | ||
putc(n & 0xff, out); | ||
} | ||
|
||
void FileIo::write_int32_be(FILE *out, uint32_t n) | ||
{ | ||
putc((n >> 24) & 0xff, out); | ||
putc((n >> 16) & 0xff, out); | ||
putc((n >> 8) & 0xff, out); | ||
putc(n & 0xff, out); | ||
} | ||
|
||
void FileIo::write_int64_be(FILE *out, uint64_t n) | ||
{ | ||
putc((n >> 56) & 0xff, out); | ||
putc((n >> 48) & 0xff, out); | ||
putc((n >> 40) & 0xff, out); | ||
putc((n >> 32) & 0xff, out); | ||
putc((n >> 24) & 0xff, out); | ||
putc((n >> 16) & 0xff, out); | ||
putc((n >> 8) & 0xff, out); | ||
putc(n & 0xff, out); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
/** | ||
* naken_asm assembler. | ||
* Author: Michael Kohn | ||
* Email: [email protected] | ||
* Web: https://www.mikekohn.net/ | ||
* License: GPLv3 | ||
* | ||
* Copyright 2010-2024 by Michael Kohn | ||
* | ||
*/ | ||
|
||
#ifndef NAKEN_ASM_FILE_IO_H | ||
#define NAKEN_ASM_FILE_IO_H | ||
|
||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <stdint.h> | ||
|
||
class FileIo | ||
{ | ||
public: | ||
FileIo(); | ||
~FileIo(); | ||
|
||
int open_for_writing(const char *filename); | ||
int open_for_reading(const char *filename); | ||
|
||
void close_file(); | ||
|
||
void set_endian(int value); | ||
|
||
int get_int8() { return getc(fp); } | ||
uint32_t get_int16() { return get_int16_func(fp); } | ||
uint32_t get_int32() { return get_int32_func(fp); } | ||
uint64_t get_int64() { return get_int64_func(fp); } | ||
|
||
void write_int16(uint32_t n) { write_int16_func(fp, n); } | ||
void write_int32(uint32_t n) { write_int32_func(fp, n); } | ||
void write_int64(uint64_t n) { write_int64_func(fp, n); } | ||
|
||
int get_bytes(uint8_t *data, int length) | ||
{ | ||
return fread(data, 1, length, fp); | ||
} | ||
|
||
int write_bytes(uint8_t *data, int length) | ||
{ | ||
return fwrite(data, 1, length, fp); | ||
} | ||
|
||
long tell() { return ftell(fp); } | ||
void set(long offset) { fseek(fp, offset, SEEK_SET); } | ||
void seek(long offset, int whence) { fseek(fp, offset, whence); } | ||
|
||
int get_string_at_offset(char *data, int length, uint64_t offset); | ||
int get_bytes_at_offset(uint8_t *data, int length, uint64_t offset); | ||
|
||
enum | ||
{ | ||
FILE_ENDIAN_LITTLE = 0, | ||
FILE_ENDIAN_BIG = 1 | ||
}; | ||
|
||
private: | ||
FILE *fp; | ||
|
||
typedef uint32_t (*get_int16_t)(FILE *); | ||
typedef uint32_t (*get_int32_t)(FILE *); | ||
typedef uint64_t (*get_int64_t)(FILE *); | ||
|
||
typedef void (*write_int16_t)(FILE *, uint32_t); | ||
typedef void (*write_int32_t)(FILE *, uint32_t); | ||
typedef void (*write_int64_t)(FILE *, uint64_t); | ||
|
||
get_int16_t get_int16_func; | ||
get_int32_t get_int32_func; | ||
get_int64_t get_int64_func; | ||
|
||
write_int16_t write_int16_func; | ||
write_int32_t write_int32_func; | ||
write_int64_t write_int64_func; | ||
|
||
static uint32_t get_int16_le(FILE *in); | ||
static uint32_t get_int32_le(FILE *in); | ||
static uint64_t get_int64_le(FILE *in); | ||
|
||
static uint32_t get_int16_be(FILE *in); | ||
static uint32_t get_int32_be(FILE *in); | ||
static uint64_t get_int64_be(FILE *in); | ||
|
||
static void write_int16_le(FILE *out, uint32_t n); | ||
static void write_int32_le(FILE *out, uint32_t n); | ||
static void write_int64_le(FILE *out, uint64_t n); | ||
|
||
static void write_int16_be(FILE *out, uint32_t n); | ||
static void write_int32_be(FILE *out, uint32_t n); | ||
static void write_int64_be(FILE *out, uint64_t n); | ||
}; | ||
|
||
#endif | ||
|
Oops, something went wrong.