Skip to content

Commit

Permalink
Add FileIo class and change read_elf to use it.
Browse files Browse the repository at this point in the history
  • Loading branch information
mikeakohn committed Feb 6, 2024
1 parent 39d1d55 commit 9a4537b
Show file tree
Hide file tree
Showing 8 changed files with 422 additions and 197 deletions.
8 changes: 4 additions & 4 deletions common/naken_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* Web: https://www.mikekohn.net/
* License: GPLv3
*
* Copyright 2010-2023 by Michael Kohn
* Copyright 2010-2024 by Michael Kohn
*
*/

Expand Down Expand Up @@ -76,9 +76,9 @@ static void print_usage()
" -powerpc (PowerPC)\n"
" -propeller (Parallax Propeller)\n"
" -propeller2 (Parallax Propeller2)\n"
" -ps2ee (Playstation 2 EE)\n"
" -ps2ee_vu0 (Playstation 2 VU0)\n"
" -ps2ee_vu1 (Playstation 2 VU1)\n"
" -ps2_ee (Playstation 2 EE)\n"
" -ps2_ee_vu0 (Playstation 2 VU0)\n"
" -ps2_ee_vu1 (Playstation 2 VU1)\n"
" -riscv (RISCV)\n"
" -sh4 (SH4)\n"
" -stm8 (STM8)\n"
Expand Down
2 changes: 1 addition & 1 deletion common/version.h
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

1 change: 1 addition & 0 deletions configure
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ COMMON_OBJS="
Var.o"

FILEIO_OBJS="
FileIo.o
file.o
read_amiga.o
read_bin.o
Expand Down
234 changes: 234 additions & 0 deletions fileio/FileIo.cpp
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);
}

101 changes: 101 additions & 0 deletions fileio/FileIo.h
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

Loading

0 comments on commit 9a4537b

Please sign in to comment.