* Fix Wii/GC Compiling

* Finish initial FileInfo/Dir APIs
* Get rid of type punning warnings
This commit is contained in:
Phillip Stephens 2015-05-20 21:57:27 -07:00
parent 29a203522d
commit 011496db8b
25 changed files with 2909 additions and 75 deletions

View File

@ -61,3 +61,5 @@ win32 {
headerFiles.files = $$PWD/include/*
INSTALLS += libFiles headerFiles
}

View File

@ -23,7 +23,8 @@ SOURCES += \
$$PWD/src/LZ77/LZType11.cpp \
$$PWD/src/LZ77/LZBase.cpp \
$$PWD/src/Athena/FileInfo.cpp \
$$PWD/src/Athena/Dir.cpp
$$PWD/src/Athena/Dir.cpp \
$$PWD/src/gekko_support.c
win32:SOURCES += \
$$PWD/src/win32_largefilewrapper.c
@ -60,11 +61,11 @@ HEADERS += \
$$PWD/include/utf8/core.h \
$$PWD/include/utf8/unchecked.h \
$$PWD/include/Athena/FileInfo.hpp \
$$PWD/include/Athena/Dir.hpp
$$PWD/include/Athena/Dir.hpp \
$$PWD/include/gekko_support.h
win32:HEADERS += \
$$PWD/include/win32_largefilewrapper.h
mac:HEADERS += \
$$PWD/include/osx_largefilewrapper.h

2413
Doxyfile Normal file

File diff suppressed because it is too large Load Diff

View File

@ -3,10 +3,6 @@ SOURCES += \
$$PWD/compress.c \
$$PWD/crc32.c \
$$PWD/deflate.c \
$$PWD/gzclose.c \
$$PWD/gzlib.c \
$$PWD/gzread.c \
$$PWD/gzwrite.c \
$$PWD/infback.c \
$$PWD/inffast.c \
$$PWD/inflate.c \

View File

@ -2,6 +2,7 @@
#define DIR_HPP
#include "Athena/FileInfo.hpp"
#include <stdio.h>
namespace Athena
{
@ -22,6 +23,7 @@ public:
bool cd(const std::string& path);
bool rm(const std::string& path);
bool touch();
static bool mkdir(const std::string& dir, mode_t mode = 0755);
static bool mkpath(const std::string& path, mode_t mode = 0755);
private:

View File

@ -3,7 +3,7 @@
#include <string>
#include <Athena/Global.hpp>
#include "Athena/Global.hpp"
namespace Athena
{

View File

@ -18,7 +18,7 @@
#include "Athena/IStreamReader.hpp"
#include <string>
#include <cstdio>
#include <stdio.h>
namespace Athena
{

View File

@ -17,6 +17,7 @@
#define FILEWRITER_HPP
#include "Athena/IStreamWriter.hpp"
#include <stdio.h>
namespace Athena
{
@ -35,7 +36,6 @@ public:
void open(bool overwrite = true);
void close();
bool isOpen() const;
bool save();
void seek(atInt64 pos, SeekOrigin origin = SeekOrigin::Current);
inline void seekAlign32() {seek(ROUND_UP_32(position()), SeekOrigin::Begin);}
bool atEnd() const;

View File

@ -40,6 +40,15 @@
# endif
#endif
#ifdef GEKKO
#include "gekko_support.h"
typedef struct stat stat64_t;
#define stat64 stat
#else
typedef struct stat64 stat64_t;
#endif
#ifndef aDebug
#define aDebug() \
std::cout << __FILE__ << "(" << __LINE__ << ") " << AT_PRETTY_FUNCTION << ": "

View File

@ -75,13 +75,13 @@ inline atInt64 swap64(atInt64 val)
inline atUint64 swapU64(atUint64 val) {return (atUint64)swap64(val);}
inline float swapFloat(float val)
{
atInt32 ival = swap32(*((atInt32*)(&val)));
return *((float*)(&ival));
atInt32 ival = swap64(static_cast<atInt32>(val));
return static_cast<float>(ival);
}
inline double swapDouble(double val)
{
atInt64 ival = swap64(*((atInt64*)(&val)));
return *((double*)(&ival));
atInt64 ival = swap64(static_cast<atInt64>(val));
return static_cast<double>(ival);
}
inline atInt16 LittleInt16(atInt16& val)
{
@ -203,6 +203,8 @@ inline double BigDouble(double& val)
return val;
}
atUint64 rand64();
void fillRandom(atUint8* rndArea, atUint64 count);
std::vector<std::string> split(const std::string& s, char delim);
std::string join(const std::vector<std::string>& elems, const std::string& delims);

View File

@ -166,19 +166,61 @@ public:
*/
bool isFile() const;
/*!
* \brief addChild
* \param file
*/
void addChild(WiiFile* file);
/*!
* \brief children
* \return
*/
std::vector<WiiFile*> children();
/*!
* \brief child
* \param name
* \return
*/
WiiFile* child(const std::string& name);
/*!
* \brief removeChild
* \param name
*/
void removeChild(const std::string& name);
/*!
* \brief removeChild
* \param file
*/
void removeChild(WiiFile* file);
/*!
* \brief parent
* \return
*/
WiiFile* parent();
/*!
* \brief setParent
* \param parent
*/
void setParent(WiiFile* parent);
/*!
* \brief fileCount
* \return
*/
atUint32 fileCount();
/*!
* \brief allChildren
* \return
*/
std::vector<WiiFile*> allChildren();
/*!
* \brief fullpath
* \return
*/
std::string fullpath();
protected:
private:

21
include/gekko_support.h Normal file
View File

@ -0,0 +1,21 @@
#ifndef GEKKO_SUPPORT_H
#define GEKKO_SUPPORT_H
#ifdef GEKKO
#include <stdarg.h>
#include <stddef.h>
#ifdef __cplusplus
extern "C" {
#endif
char *
realpath(const char *path, char *resolved);
int
vsnprintf (char *s, size_t n, const char *format, va_list ap);
#ifdef __cplusplus
}
#endif
#endif
#endif // GEKKO_SUPPORT_H

View File

@ -14,10 +14,10 @@
// along with libAthena. If not, see <http://www.gnu.org/licenses/>
#include "Athena/Checksums.hpp"
#include "Athena/FileReader.hpp"
#include <iostream>
#include <iomanip>
namespace Athena
{
namespace Checksums

View File

@ -358,32 +358,18 @@ atUint32 simpleEnc(const atUint8* src, atInt32 size, atInt32 pos, atUint32* pMat
atUint32 decompressLZ77(const atUint8* src, atUint32 srcLen, atUint8** dst)
{
LZBase* lzCodec;
if (*(atUint8*)src == 0x11)
lzCodec = new LZType11;
else
lzCodec = new LZType10;
return LZType11().decompress(src, dst, srcLen);
atUint32 retLength = lzCodec->decompress(src, dst, srcLen);
delete lzCodec;
return retLength;
return LZType10(2).decompress(src, dst, srcLen);
}
atUint32 compressLZ77(const atUint8* src, atUint32 srcLen, atUint8** dst, bool extended)
{
LZBase* lzCodec;
if (extended)
lzCodec = new LZType11;
else
lzCodec = new LZType10(2);
return LZType11().compress(src, dst, srcLen);
atUint32 retLength = lzCodec->compress(src, dst, srcLen);
delete lzCodec;
return retLength;
return LZType10(2).compress(src, dst, srcLen);
}
} // Compression

View File

@ -2,7 +2,10 @@
#include <dirent.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdlib.h>
#include <limits.h>
#define __STDC_FORMAT_MACROS
#include <inttypes.h>
#ifdef _MSC_VER
#define stat64 __stat64
@ -23,7 +26,7 @@ std::string Dir::absolutePath() const
bool Dir::isDir() const
{
struct stat64 st;
stat64_t st;
int e = stat64(m_path.c_str(), &st);
if (e < 0)
return false;
@ -48,6 +51,17 @@ bool Dir::rm(const std::string& path)
return !(remove((m_path + "/" + path).c_str()) < 0);
}
bool Dir::touch()
{
srand(time(NULL));
atUint64 tmp = utility::rand64();
std::string tmpFile = utility::sprintf("%" PRIX64 ".tmp", tmp);
bool ret = FileInfo(m_path + "/" + tmpFile).touch();
if (ret)
return rm(tmpFile);
return false;
}
bool Dir::mkdir(const std::string& dir, mode_t mode)
{
return !(::mkdir(dir.c_str(), mode) < 0);

View File

@ -4,12 +4,16 @@
#include "Athena/FileReader.hpp"
#include <sys/time.h>
#include <time.h>
#include <fcntl.h>
#include <stdio.h>
#include <dirent.h>
#include <sys/stat.h>
#include <unistd.h>
#include <limits.h>
#include <stdlib.h>
#if !(defined(HW_DOL) || defined(HW_RVL) || defined(_WIN32))
#include <fcntl.h>
#endif
#ifdef _WIN32
#include <windows.h>
@ -76,8 +80,14 @@ atUint64 FileInfo::size() const
bool FileInfo::exists() const
{
#ifdef GEKKO
struct stat st;
int e = stat(m_path.c_str(), &st);
#else
struct stat64 st;
int e = stat64(m_path.c_str(), &st);
#endif
if (e < 0)
return false;
@ -86,8 +96,13 @@ bool FileInfo::exists() const
bool FileInfo::isLink() const
{
#ifdef GEKKO
struct stat st;
int e = stat(m_path.c_str(), &st);
#else
struct stat64 st;
int e = stat64(m_path.c_str(), &st);
#endif
if (e < 0)
return false;
@ -96,7 +111,7 @@ bool FileInfo::isLink() const
bool FileInfo::isFile() const
{
struct stat64 st;
stat64_t st;
int e = stat64(m_path.c_str(), &st);
if (e < 0)
return false;
@ -106,25 +121,16 @@ bool FileInfo::isFile() const
bool FileInfo::touch() const
{
#ifndef _WIN32
struct stat st;
struct timespec newTimes[2];
if (stat(m_path.c_str(), &st) < 0) {
#if defined(__GNUC__) && !(defined(HW_DOL) || defined(HW_RVL) || defined(GEKKO))
stat64_t st;
if (stat64(m_path.c_str(), &st) < 0) {
(void)Athena::io::FileWriter(m_path);
return true;
}
/* keep atime unchanged */
newTimes[0] = st.st_atim;
/* set mtime to current time */
clock_gettime(CLOCK_REALTIME, &newTimes[1]);
if (utimensat(AT_FDCWD, m_path.c_str(), newTimes, 0) < 0) {
if (utimensat(AT_FDCWD, m_path.c_str(), NULL, 0) < 0) {
return false;
}
#else
#elif defined(_WIN32)
FILETIME modtime;
SYSTEMTIME st;
HANDLE fh;
@ -170,6 +176,29 @@ bool FileInfo::touch() const
}
CloseHandle(fh);
#elif (defined(HW_RVL) || defined(HW_DOL)) && defined(GEKKO)
// Generic portable version, not extremely reliable but does work
atUint64 val = 0xCDCDCDCDCD;
try
{
Athena::io::FileReader reader(m_path.c_str());
val = reader.readUint64();
}
catch(...)
{
}
try
{
Athena::io::FileWriter writer(m_path, false);
if (val != 0xCDCDCDCDCD)
writer.writeUint64(val);
}
catch(...)
{
return false;
}
#endif
return true;

View File

@ -97,11 +97,6 @@ bool FileWriter::isOpen() const
return m_fileHandle != NULL;
}
bool FileWriter::save()
{
return true;
}
void FileWriter::seek(atInt64 pos, SeekOrigin origin)
{
if (fseeko64(m_fileHandle, pos, (int)origin) != 0)

View File

@ -499,11 +499,7 @@ void MemoryReader::loadData()
rewind(in);
length = utility::fileSize(m_filepath);
#ifdef HW_RVL
m_data = (Uint8*)memalign(32, length);
#else
m_data = new atUint8[length];
#endif
atUint64 done = 0;
atUint64 blocksize = BLOCKSZ;

View File

@ -495,12 +495,7 @@ void MemoryWriter::resize(atUint64 newSize)
THROW_INVALID_OPERATION_EXCEPTION("Stream::resize() -> New size cannot be less to the old size.");
// Allocate and copy new buffer
#ifdef HW_RVL
Uint8* newArray = (Uint8*)memalign(32, newSize);
#else
atUint8* newArray = new atUint8[newSize];
#endif
memset(newArray, 0, newSize);
if (m_data)
@ -508,11 +503,7 @@ void MemoryWriter::resize(atUint64 newSize)
memcpy(newArray, m_data, m_length);
// Delete the old one
#ifdef HW_RVL
free(m_data);
#else
delete[] m_data;
#endif
}
// Swap the pointer and size out for the new ones.

View File

@ -19,11 +19,13 @@
#include <stdlib.h>
#include <sstream>
#include <algorithm>
#include <cstdarg>
#include <stdarg.h>
#include <iterator>
#include <cstdio>
#include <sys/types.h>
#include <sys/stat.h>
#include <Athena/Exception.hpp>
#include <random>
#ifdef _MSC_VER
#include <functional>
@ -171,7 +173,7 @@ int countChar(const std::string& str, const char chr, int* lastOccur)
atUint64 fileSize(const std::string& filename)
{
struct stat64 st;
stat64_t st;
stat64(filename.c_str(), &st);
return st.st_size;
}
@ -203,5 +205,15 @@ std::string& trim(std::string& s)
return s;
}
atUint64 rand64()
{
// Combine 4 parts of low 16-bit of each rand()
atUint64 r0 = (atUint64)rand() << 48;
atUint64 r1 = (atUint64)rand() << 48 >> 16;
atUint64 r2 = (atUint64)rand() << 48 >> 32;
atUint64 r3 = (atUint64)rand() << 48 >> 48;
return r0 | r1 | r2 | r3;
}
} // utility
} // Athena

View File

@ -23,6 +23,7 @@
#include "Athena/IOException.hpp"
#include "Athena/InvalidOperationException.hpp"
#include "Athena/InvalidDataException.hpp"
#include "Athena/FileWriter.hpp"
#include "md5.h"
#include "aes.h"
#include "ec.h"
@ -74,7 +75,7 @@ WiiSave* WiiSaveReader::readSave()
if (bkMagic != 0x426B0001)
THROW_INVALID_DATA_EXCEPTION_RETURN(nullptr, "Invalid BacKup header magic");
atUint32 ngId = base::readUint32();
/*atUint32 ngId =*/ base::readUint32();
atUint32 numFiles = base::readUint32();
/*int fileSize =*/ base::readUint32();
@ -124,13 +125,14 @@ WiiBanner* WiiSaveReader::readBanner()
atUint8 permissions;
atUint8 md5[16];
atUint8 md5Calc[16];
atUint8 tmpIV[26];
atUint8 tmpIV[16];
memcpy(tmpIV, SD_IV, 16);
std::cout << "Decrypting: banner.bin...";
aes_set_key(SD_KEY);
aes_decrypt(tmpIV, data, dec, 0xF0C0);
std::cout << "done" << std::endl;
memset(md5, 0, 16);
memset(md5Calc, 0, 16);
// Read in the MD5 sum
@ -163,6 +165,7 @@ WiiBanner* WiiSaveReader::readBanner()
THROW_INVALID_DATA_EXCEPTION_RETURN(nullptr, "MD5 Mismatch");
}
// Set the binary reader buffer;
base::setData(dec, 0xF0C0);
// Start reading the header
@ -328,7 +331,7 @@ void WiiSaveReader::readCerts(atUint32 totalSize)
WiiFile* WiiSaveReader::buildTree(std::vector<WiiFile*> files)
{
// This is simply a virtual root that will contain all the other nodes
WiiFile* root = new WiiFile("/");
WiiFile* root = new WiiFile("");
root->setType(WiiFile::Directory);
for (WiiFile* f : files)

View File

@ -1,7 +1,7 @@
#include "LZ77/LZLookupTable.hpp"
#include "LZ77/LZType10.hpp"
#include <Athena/MemoryWriter.hpp>
#include <memory.h>
#include <string.h>
LZType10::LZType10(atInt32 MinimumOffset, atInt32 SlidingWindow, atInt32 MinimumMatch, atInt32 BlockSize)
: LZBase(MinimumOffset, SlidingWindow, MinimumMatch, BlockSize)

View File

@ -1,7 +1,7 @@
#include "LZ77/LZLookupTable.hpp"
#include "LZ77/LZType11.hpp"
#include <Athena/MemoryWriter.hpp>
#include <memory.h>
#include <string.h>
LZType11::LZType11(atInt32 minimumOffset, atInt32 slidingWindow, atInt32 minimumMatch, atInt32 blockSize)

317
src/gekko_support.c Normal file
View File

@ -0,0 +1,317 @@
#if defined(GEKKO)
#include "gekko_support.h"
#define SYMLOOP_MAX 8
#include <sys/stat.h>
#include <sys/param.h>
#include <stddef.h>
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <unistd.h>
#include <limits.h>
/* A slightly modified copy of this file exists in libexec/ld.so */
/*
* char *realpath(const char *path, char resolved[PATH_MAX]);
*
* Find the real name of path, by removing all ".", ".." and symlink
* components. Returns (resolved) on success, or (NULL) on failure,
* in which case the path which caused trouble is left in (resolved).
*/
char *
realpath(const char *path, char *resolved)
{
struct stat sb;
char *p, *q, *s;
size_t left_len, resolved_len;
unsigned symlinks;
int serrno, slen, mem_allocated;
char left[PATH_MAX], next_token[PATH_MAX], symlink[PATH_MAX];
if (path[0] == '\0') {
errno = ENOENT;
return (NULL);
}
serrno = errno;
if (resolved == NULL) {
resolved = malloc(PATH_MAX);
if (resolved == NULL)
return (NULL);
mem_allocated = 1;
} else
mem_allocated = 0;
symlinks = 0;
if (path[0] == '/') {
resolved[0] = '/';
resolved[1] = '\0';
if (path[1] == '\0')
return (resolved);
resolved_len = 1;
left_len = strlcpy(left, path + 1, sizeof(left));
} else {
if (getcwd(resolved, PATH_MAX) == NULL) {
if (mem_allocated)
free(resolved);
else
strlcpy(resolved, ".", PATH_MAX);
return (NULL);
}
resolved_len = strlen(resolved);
left_len = strlcpy(left, path, sizeof(left));
}
if (left_len >= sizeof(left) || resolved_len >= PATH_MAX) {
errno = ENAMETOOLONG;
goto err;
}
/*
* Iterate over path components in `left'.
*/
while (left_len != 0) {
/*
* Extract the next path component and adjust `left'
* and its length.
*/
p = strchr(left, '/');
s = p ? p : left + left_len;
if (s - left >= sizeof(next_token)) {
errno = ENAMETOOLONG;
goto err;
}
memcpy(next_token, left, s - left);
next_token[s - left] = '\0';
left_len -= s - left;
if (p != NULL)
memmove(left, s + 1, left_len + 1);
if (resolved[resolved_len - 1] != '/') {
if (resolved_len + 1 >= PATH_MAX) {
errno = ENAMETOOLONG;
goto err;
}
resolved[resolved_len++] = '/';
resolved[resolved_len] = '\0';
}
if (next_token[0] == '\0')
continue;
else if (strcmp(next_token, ".") == 0)
continue;
else if (strcmp(next_token, "..") == 0) {
/*
* Strip the last path component except when we have
* single "/"
*/
if (resolved_len > 1) {
resolved[resolved_len - 1] = '\0';
q = strrchr(resolved, '/') + 1;
*q = '\0';
resolved_len = q - resolved;
}
continue;
}
/*
* Append the next path component and lstat() it. If
* lstat() fails we still can return successfully if
* there are no more path components left.
*/
resolved_len = strlcat(resolved, next_token, PATH_MAX);
if (resolved_len >= PATH_MAX) {
errno = ENAMETOOLONG;
goto err;
}
if (lstat(resolved, &sb) != 0) {
if (errno == ENOENT && p == NULL) {
errno = serrno;
return (resolved);
}
goto err;
}
if (S_ISLNK(sb.st_mode)) {
if (symlinks++ > SYMLOOP_MAX) {
errno = ELOOP;
goto err;
}
slen = readlink(resolved, symlink, sizeof(symlink) - 1);
if (slen < 0)
goto err;
symlink[slen] = '\0';
if (symlink[0] == '/') {
resolved[1] = 0;
resolved_len = 1;
} else if (resolved_len > 1) {
/* Strip the last path component. */
resolved[resolved_len - 1] = '\0';
q = strrchr(resolved, '/') + 1;
*q = '\0';
resolved_len = q - resolved;
}
/*
* If there are any path components left, then
* append them to symlink. The result is placed
* in `left'.
*/
if (p != NULL) {
if (symlink[slen - 1] != '/') {
if (slen + 1 >= sizeof(symlink)) {
errno = ENAMETOOLONG;
goto err;
}
symlink[slen] = '/';
symlink[slen + 1] = 0;
}
left_len = strlcat(symlink, left, sizeof(symlink));
if (left_len >= sizeof(left)) {
errno = ENAMETOOLONG;
goto err;
}
}
left_len = strlcpy(left, symlink, sizeof(left));
}
}
/*
* Remove trailing slash except when the resolved pathname
* is a single "/".
*/
if (resolved_len > 1 && resolved[resolved_len - 1] == '/')
resolved[resolved_len - 1] = '\0';
return (resolved);
err:
if (mem_allocated)
free(resolved);
return (NULL);
}
static int
int_vasprintf (result, format, args)
char **result;
const char *format;
va_list *args;
{
const char *p = format;
/* Add one to make sure that it is never zero, which might cause malloc
to return NULL. */
int total_width = strlen (format) + 1;
va_list ap;
memcpy ((void*) &ap, (void*) args, sizeof (va_list));
while (*p != '\0')
{
if (*p++ == '%')
{
while (strchr ("-+ #0", *p))
++p;
if (*p == '*')
{
++p;
total_width += abs (va_arg (ap, int));
}
else
total_width += strtoul (p, (char**)&p, 10);
if (*p == '.')
{
++p;
if (*p == '*')
{
++p;
total_width += abs (va_arg (ap, int));
}
else
total_width += strtoul (p, (char**)&p, 10);
}
while (strchr ("hlL", *p))
++p;
/* Should be big enough for any format specifier except %s and floats. */
total_width += 30;
switch (*p)
{
case 'd':
case 'i':
case 'o':
case 'u':
case 'x':
case 'X':
case 'c':
(void) va_arg (ap, int);
break;
case 'f':
case 'e':
case 'E':
case 'g':
case 'G':
(void) va_arg (ap, double);
/* Since an ieee double can have an exponent of 307, we'll
make the buffer wide enough to cover the gross case. */
total_width += 307;
break;
case 's':
total_width += strlen (va_arg (ap, char *));
break;
case 'p':
case 'n':
(void) va_arg (ap, char *);
break;
}
}
}
#ifdef TEST
global_total_width = total_width;
#endif
*result = malloc (total_width);
if (*result != NULL)
return vsprintf (*result, format, *args);
else
return 0;
}
int
vasprintf (result, format, args)
char **result;
const char *format;
#if defined (_BSD_VA_LIST_) && defined (__FreeBSD__)
_BSD_VA_LIST_ args;
#else
va_list args;
#endif
{
return int_vasprintf (result, format, &args);
}
int
vsnprintf (char *s, size_t n, const char *format, va_list ap)
{
char *buf = 0;
int result = vasprintf (&buf, format, ap);
if (!buf)
return -1;
if (result < 0)
{
free (buf);
return -1;
}
result = strlen (buf);
if (n > 0)
{
if ((long) n > result)
memcpy (s, buf, result+1);
else
{
memcpy (s, buf, n-1);
s[n - 1] = 0;
}
}
free (buf);
return result;
}
#endif

View File

@ -1,3 +1,4 @@
#ifdef __APPLE__
#include "osx_largefilewrapper.h"
#include <stdio.h>
@ -10,4 +11,6 @@ off_t ftello64(FILE* fp)
{
return ftello(fp);
}
#endif