Browse Source

Add auto erase leading .\, Add oendianness.h, Add readSignature function

master
Jake Wakeling 1 month ago
parent
commit
4a9217f626
8 changed files with 105 additions and 52 deletions
  1. 35
    0
      include/oendianness.h
  2. 3
    0
      src/arg.cpp
  3. 0
    6
      src/arg.h
  4. 9
    0
      src/main.cpp
  5. 0
    9
      src/main.h
  6. 41
    26
      src/oar.cpp
  7. 3
    11
      src/oar.h
  8. 14
    0
      src/version.h

+ 35
- 0
include/oendianness.h View File

@@ -0,0 +1,35 @@
// oendianness.h, version 1.0
// OmegaLib endianness functions
// Copyright (C) 2019, Jakob Wakeling
// All rights reserved.

#ifndef OENDIANNESS_H
#define OENDIANNESS_H

#include <stdint.h>

/*
Swap endianness of 16 bits of memory
*/
uint16_t swap16(uint16_t x) {
return (x << 8) | (x >> 8);
}

/*
Swap endianness of 32 bits of memory
*/
uint32_t swap32(uint32_t x) {
x = ((x & 0x0000FFFF) << 16) | ((x & 0xFFFF0000) >> 16);
return ((x & 0x00FF00FF) << 8) | ((x & 0xFF00FF00) >> 8);
}

/*
Swap endianness of 64 bits of memory
*/
uint64_t swap64(uint64_t x) {
x = ((x & 0x00000000FFFFFFFF) << 32) | ((x & 0xFFFFFFFF00000000) >> 32);
x = ((x & 0x0000FFFF0000FFFF) << 16) | ((x & 0xFFFF0000FFFF0000) >> 16);
return ((x & 0x00FF00FF00FF00FF) << 8) | ((x & 0xFF00FF00FF00FF00) >> 8);
}

#endif // OENDIANNESS_H

+ 3
- 0
src/arg.cpp View File

@@ -3,7 +3,10 @@
// Copyright (C) 2019, Jakob Wakeling
// All rights reserved.

#include <cxxopts.hpp>

#include "arg.h"
#include "version.h"

/*
Parse program arguments and return result

+ 0
- 6
src/arg.h View File

@@ -6,12 +6,6 @@
#ifndef ARG_H
#define ARG_H

#define PROGRAM_NAME "oar"
#define PROGRAM_DESC "OmegaARc, Version 0.1.0"
#define OAR_H_VERSION 0x4F415200

#include <cxxopts.hpp>

class Arg {
public:
static cxxopts::ParseResult Arg::parse(int argc, char *argv[]);

+ 9
- 0
src/main.cpp View File

@@ -3,7 +3,16 @@
// Copyright (C) 2019, Jakob Wakeling
// All rights reserved.

#include <ctime>
#include <cxxopts.hpp>
#include <iostream>
#include <fstream>
#include <filesystem>

#include "arg.h"
#include "header.h"
#include "main.h"
#include "oar.h"

namespace fs = std::filesystem;


+ 0
- 9
src/main.h View File

@@ -6,15 +6,6 @@
#ifndef MAIN_H
#define MAIN_H

#define PROGRAM_NAME "oar"

#include <ctime>
#include <iostream>
#include <fstream>
#include <filesystem>

#include "arg.h"
#include "header.h"
#include "oar.h"

#endif

+ 41
- 26
src/oar.cpp View File

@@ -3,7 +3,16 @@
// Copyright (C) 2019, Jakob Wakeling
// All rights reserved.

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <oendianness.h>


#include "header.h"
#include "oar.h"
#include "version.h"

/*
Compress files into OmegaARc archive
@@ -15,7 +24,7 @@ int Oar::compress(std::string oarPath, std::vector<std::string> files) {
std::ofstream oarFile;
oarFile.open(oarPath, std::ios::binary | std::ios::out); // Open OAR archive for writing
if (!oarFile) { // Check for file open error
fprintf(stderr, "OARx0A10: Failed to open %s for writing\n", oarPath.c_str());
fprintf(stderr, "0x0A10: Failed to open %s for writing\n", oarPath.c_str());
return 0x0A10;
}

@@ -40,12 +49,13 @@ int Oar::extract(std::string oarPath, std::string extractPath) {
std::ifstream oarFile;
oarFile.open(oarPath, std::ios::binary | std::ios::in); // Open OAR archive for reading
if (!oarFile) { // Check for file open error
fprintf(stderr, "OARx0A20: Failed to open %s for reading\n", oarPath.c_str());
fprintf(stderr, "0x0A20: Failed to open %s for reading\n", oarPath.c_str());
return 0x0A20;
}

// TODO: Implement OAR signature checking, not currently necesary as there is only one
oarFile.seekg(4); // Skip OAR signature
if (readSignature(&oarFile) != OAR_H_VERSION) { // Check OAR signature
fprintf(stderr, "OAR signature does not match, ignoring");
}

for (;;) {
Header *header = fileDecode(&oarFile); // Decode next file header
@@ -55,7 +65,7 @@ int Oar::extract(std::string oarPath, std::string extractPath) {
}

if (header == nullptr) { // Check for header read error
fprintf(stderr, "OARx0A21: Failed to read OAR header\n");
fprintf(stderr, "0x0A21: Failed to read OAR header\n");
delete header;
return 0x0A21;
}
@@ -87,8 +97,9 @@ int Oar::list(std::string oarPath, std::vector<std::string> *files) {
return 0x0A20;
}

// TODO: Implement OAR signature checking, not currently necesary as there is only one
oarFile.seekg(4); // Skip OAR signature
if (readSignature(&oarFile) != OAR_H_VERSION) { // Check OAR signature
fprintf(stderr, "OAR signature does not match, ignoring\n");
}

for (;;) {
Header *header = fileDecode(&oarFile); // Decode next file header
@@ -121,6 +132,11 @@ int Oar::list(std::string oarPath, std::vector<std::string> *files) {
0x0A11: Failed to open input file for reading
*/
int Oar::fileAdd(std::ofstream *oarFile, std::string filePath) {
// TODO: This could theoretically be made more efficient
if (filePath.at(0) == '.' && filePath.at(1) == '\\') { // Erase any leading .\ in file path
filePath.erase(0, 2);
}

Header *header = new Header(filePath); // Construct header with path

std::ifstream file;
@@ -165,25 +181,6 @@ int Oar::fileAdd(std::ofstream *oarFile, std::string filePath) {
return 0x0000;
}

/*
Swap endianness of unsigned 32-bit integer
*/
uint32_t swap32(uint32_t integer) {
return ((integer >> 24) & 0x000000FF) | ((integer << 8) & 0x00FF0000) |
((integer >> 8) & 0x0000FF00) | ((integer << 24) & 0xFF000000);
}

/*
Write OAR signature to file
TODO: Add error checking
*/
void Oar::WriteSignature(std::ofstream *oarFile) {
uint32_t headerVersion = OAR_H_VERSION; // Load header version
headerVersion = swap32(headerVersion); // Swap header version
oarFile->write((char *)& headerVersion, 4); // Write header version
return;
}

/*
Extract file

@@ -228,6 +225,24 @@ int Oar::fileExtract(std::istream *oarFile, std::string path, size_t fileSize) {
return 0x0000;
}

/*
Write OAR signature to file
*/
void Oar::WriteSignature(std::ofstream *oarFile) {
uint32_t headerVersion = swap32(OAR_H_VERSION); // Load and swap header version
oarFile->write((char *)&headerVersion, 4); // Write header version
return;
}

/*
Read OAR signature from file
*/
uint32_t Oar::readSignature(std::ifstream *oarFile) {
uint32_t headerVersion;
oarFile->read((char *)&headerVersion, 4); // Read header version
return swap32(headerVersion);
}

/*
Decode file header
TODO: Add error checking and clean up

+ 3
- 11
src/oar.h View File

@@ -6,24 +6,16 @@
#ifndef OAR_H
#define OAR_H

#define OAR_H_VERSION 0x4F415200

#include <iostream>
#include <fstream>
#include <string>
#include <vector>

#include "header.h"

namespace Oar {
int compress(std::string oarPath, std::vector<std::string> files);
int extract(std::string oarPath, std::string extractPath);
int list(std::string oarPath, std::vector<std::string> *files);

int fileAdd(std::ofstream *oarFile, std::string filePath);
void WriteSignature(std::ofstream *oarFile);

int fileExtract(std::istream *oarFile, std::string file, size_t fileSize);

void WriteSignature(std::ofstream *oarFile);
uint32_t readSignature(std::ifstream *oarFile);
Header *fileDecode(std::ifstream *oarFile);
};


+ 14
- 0
src/version.h View File

@@ -0,0 +1,14 @@
// version.h
// Version information file for OmegaARc
// Copyright (C) 2019, Jakob Wakeling
// All rights reserved.

#ifndef VERSION_H
#define VERSION_H

#define PROGRAM_NAME "oar"
#define PROGRAM_DESC "OmegaARc, Version 0.1.0"
#define OAR_VERSION "0.1.0"
#define OAR_H_VERSION 0x4F415200

#endif // VERSION_H

Loading…
Cancel
Save