Skip to content

Commit

Permalink
Added basic extract file functions
Browse files Browse the repository at this point in the history
  • Loading branch information
shawwwn committed May 15, 2014
1 parent 66a8ae7 commit 8f4431a
Show file tree
Hide file tree
Showing 12 changed files with 187 additions and 26 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,6 @@ packages/
*.dep
*.pch
*.ncb
*.exp
*.exp

[Ss]ample/
32 changes: 32 additions & 0 deletions H2OExtractor.dsp

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

80 changes: 72 additions & 8 deletions Source/Archive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,8 @@ uint64_t Archive::readComment(uint64_t streamPos)

// Read Header
m_hH2O.read(m_Comment.szHeader, 8);
DB::debugLog("- ArchiveHeader: ", m_Comment.szHeader, NULL);

// Read VersionInfo
m_hH2O.read((char*)&m_Comment.VersionInfo, 4);
DB::debugLog("- VersionInfo: ", m_Comment.VersionInfo, NULL);

m_hH2O.read((char*)&m_Comment.VersionInfo, 4);
// Read Comments
uint64_t beg = m_hH2O.tellg();
char curByte = 0x0;
Expand All @@ -42,31 +38,99 @@ uint64_t Archive::readComment(uint64_t streamPos)
m_Comment.szComments = new char[length];
memset(m_Comment.szComments, 0, length);
m_hH2O.read(m_Comment.szComments, length-1);

//#if _DEBUG
DB::debugLog("- ArchiveHeader: ", m_Comment.szHeader, NULL);
DB::debugLog("- VersionInfo: ", m_Comment.VersionInfo, NULL);
DB::debugLog("- Comments: ", m_Comment.szComments, NULL);
//#endif
return end;
}

uint64_t Archive::readHeader(uint64_t streamPos)
{
m_hH2O.seekg(streamPos, std::ios::beg);
m_hH2O.read((char*)&m_Header, sizeof(ArchiveHeader));
DB::debugLog("size: ", sizeof(m_Header.CompressedSize), NULL);

//#if _DEBUG
DB::debugLog("- Version: ", m_Header.Version, NULL);
DB::debugLog("- FileCount: ", m_Header.FileCount, NULL);
DB::debugLog("- CompressedSize: ", m_Header.CompressedSize, NULL);
DB::debugLog("- RawSize: ", m_Header.RawSize, NULL);
return 0;
//#endif
return m_hH2O.tellg();
}

uint64_t Archive::readFileList(uint64_t streamPos)
{
m_hH2O.seekg(streamPos, std::ios::beg);
ArchiveFile curFile;
for (int i=0; i<m_Header.FileCount; i++)
{
m_hH2O.read((char*)&curFile, sizeof(ArchiveFile));
m_FileList.push_back(curFile);

#if _DEBUG
DB::debugLog("- CompressionTag: ", curFile.CompressionTag, NULL);
DB::debugLog("- FolderNameIndex: ", (uint32_t)curFile.FolderNameIndex, NULL);
DB::debugLog("- FileNameIndex: ", (uint32_t)curFile.FileNameIndex, NULL);
DB::debugLog("- FileId: ", curFile.FileId, NULL);
DB::debugLog("- RawSize: ", curFile.RawSize, NULL);
DB::debugLog("- CompressedSize: ", curFile.CompressedSize, NULL);
DB::debugLog("- Offest: ", curFile.Offest, NULL);
DB::debugLog("- CRC32[hex]: ", curFile.CRC32, NULL, true);
DB::debugLog("- UnknownConstant: ", curFile.UnknownConstant, NULL);
DB::debugLog("-------------------", "", NULL);
#endif
}
return m_hH2O.tellg();
}

void Archive::open(char* szFilePath)
{
m_hH2O.open("Cursors.H2O", std::ifstream::binary);
m_hH2O.open(szFilePath, std::ifstream::binary);

uint64_t curPos = 0;
DB::debugLog("Comment Section: ", "", NULL);
curPos = readComment(curPos);
DB::debugLog("Header Section: ", "", NULL);
curPos = readHeader(curPos);
DB::debugLog("\n", "", NULL);
DB::debugLog("File Section: ", "", NULL);
curPos = readFileList(curPos);
}

void Archive::extractByIndex(uint32_t index)
{
ArchiveFile& rFile = m_FileList[index];
char* buffer = new char[rFile.CompressedSize];
m_hH2O.seekg(rFile.Offest, std::ios::beg);
m_hH2O.read(buffer, rFile.CompressedSize);

// generate file name and create directory
const char* dirPath = "output/";
if (checkPathType(dirPath)!=TYPE_DIR)
mkdir(dirPath);
char fileName[16];
char fullPath[64];
getFileName(index, ".wav", fileName);
getFullPath("output/", fileName, fullPath);
std::ofstream destFile;

// write file
destFile.open(fullPath, std::ofstream::binary);
destFile.write(buffer, rFile.CompressedSize);
destFile.close();

delete[] buffer;
}

void Archive::extractAll()
{
for (int i=0; i<m_Header.FileCount; i++)
{
extractByIndex(i);
}
}

void Archive::close()
Expand Down
8 changes: 7 additions & 1 deletion Source/Archive.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@
#include <fstream>
#include <sstream>
#include <vector>
#include <direct.h>

#include "Types.h"
#include "FileUtils.h"
#include "StringUtils.h"
#include "DebugUtils.h"
#include "ArchiveComment.h"
#include "ArchiveHeader.h"
Expand All @@ -24,13 +27,16 @@ class Archive

uint64_t readComment(uint64_t streamPos);
uint64_t readHeader(uint64_t streamPos);
uint64_t readFileList(uint64_t streamPos);

void open(char* szFilePath);
void extractByIndex(uint32_t index);
void extractAll();
void close();

ArchiveComment m_Comment;
ArchiveHeader m_Header;

std::vector<ArchiveFile> m_FileList;


protected:
Expand Down
2 changes: 1 addition & 1 deletion Source/ArchiveFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ struct ArchiveFile
uint32_t FileId;
uint32_t RawSize;
uint32_t CompressedSize;
uint32_t Offest;
uint64_t Offest;
char CRC32[4];
uint32_t UnknownConstant;
};
Expand Down
19 changes: 9 additions & 10 deletions Source/DebugUtils.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* DebugUtils.cpp */
#include "DebugUtils.h"

namespace DB {
Expand All @@ -12,34 +13,33 @@ namespace DB {
}
fflush(stdout);
}
void debugLog(char* szDesc, char* szString, char* szComment)
void debugLog(char* szDesc, char* szString, char* szComment, bool isHex)
{
#ifdef _DEBUG
szDesc = (szDesc==NULL) ? "" : szDesc;
szString = (szString==NULL) ? "" : szString;
szComment = (szComment==NULL) ? "" : szComment;
std::cout << szDesc << szString << szComment << std::endl;
#endif
if (isHex)
{
// Display in big endian order
std::cout << szDesc << std::hex << std::setfill ('0') << std::setw(8) << *(uint32_t*)szString << std::dec << szComment << std::endl;
}
else
std::cout << szDesc << szString << szComment << std::endl;
}
void debugLog(char* szDesc, float szFloat, char* szComment)
{
#ifdef _DEBUG
szDesc = (szDesc==NULL) ? "" : szDesc;
szComment = (szComment==NULL) ? "" : szComment;
std::cout << szDesc << std::fixed << std::setprecision(2) << szFloat << szComment << std::endl;
#endif
}
void debugLog(char* szDesc, uint32_t szUint, char* szComment)
{
#ifdef _DEBUG
szDesc = (szDesc==NULL) ? "" : szDesc;
szComment = (szComment==NULL) ? "" : szComment;
std::cout << szDesc << szUint << szComment << std::endl;
#endif
}
void debugLog(char* szDesc, uint64_t szUint, char* szComment)
{
#ifdef _DEBUG
szDesc = (szDesc==NULL) ? "" : szDesc;
szComment = (szComment==NULL) ? "" : szComment;
#if (_MSC_VER < 1300) // VC6 doesn't support cout 64bit integer
Expand All @@ -49,6 +49,5 @@ namespace DB {
#else
std::cout << szDesc << szUint << szComment << std::endl;
#endif
#endif
}
}
5 changes: 3 additions & 2 deletions Source/DebugUtils.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
/* DebugUtils.hpp */
/* DebugUtils.h */
#pragma once

#include <iostream>
#include <iomanip>
#include "Types.h"

namespace DB {
void printint64(uint64_t i);
void debugLog(char* szDesc, char* szString, char* szComment);
void debugLog(char* szDesc, char* szString, char* szComment, bool isHex=false);
void debugLog(char* szDesc, float szFloat, char* szComment);
void debugLog(char* szDesc, uint32_t szUint32, char* szComment);
void debugLog(char* szDesc, uint64_t szUint64, char* szComment);
Expand Down
16 changes: 16 additions & 0 deletions Source/FileUtils.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/* FileUtils.cpp */
#include "FileUtils.h"

PathType checkPathType(const char* dirPath)
{
if (access(dirPath, 0)==0)
{
struct stat status;
stat(dirPath, &status);
if ( status.st_mode & S_IFDIR )
return TYPE_DIR;
else
return TYPE_FILE;
}
return TYPE_NOT_EXIST;
}
10 changes: 10 additions & 0 deletions Source/FileUtils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/* FileUtils.h */
#pragma once

#include <io.h> // For access()
#include <sys/types.h> // For stat()
#include <sys/stat.h> // For stat()
#include "Types.h"

enum PathType { TYPE_DIR, TYPE_FILE, TYPE_NOT_EXIST };
PathType checkPathType(const char* dirPath);
8 changes: 5 additions & 3 deletions Source/H2OExtractor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
int main(int argc, char* argv[])
{
printf("H2OExtractor\n============================\n");
Archive h2o("Cursors.H2O");
return 0;
}
Archive h2o("Input.H2O");

DB::debugLog("\nExtracting files...", "\n", NULL);
h2o.extractAll();
return 0;
}
20 changes: 20 additions & 0 deletions Source/StringUtils.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/* StringUtils.cpp */
#include "StringUtils.h"

// TODO: Use native char array

void getFileName(uint32_t index, const char* postfix, char* outputBuffer)
{
memset(outputBuffer, 0, 16);
char szIndex[8];
itoa (index, szIndex, 10);
strcpy(outputBuffer, szIndex);
strcat(outputBuffer, postfix);
}

void getFullPath(const char* directoryPath, char* fileName, char* outputBuffer)
{
memset(outputBuffer, 0, 64);
strcpy(outputBuffer, directoryPath);
strcat(outputBuffer, fileName);
}
9 changes: 9 additions & 0 deletions Source/StringUtils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/* StringUtils.h */
#pragma once

#include <string.h>
#include <stdlib.h>
#include "Types.h"

void getFileName(uint32_t index, const char* postfix, char* outputBuffer);
void getFullPath(const char* directoryPath, char* fileName, char* outputBuffer);

0 comments on commit 8f4431a

Please sign in to comment.