Skip to content

Commit

Permalink
Merge pull request #3 from asdetycv1zzc/master
Browse files Browse the repository at this point in the history
  • Loading branch information
ryank231231 authored Jun 9, 2022
2 parents 9efc0cc + df08f1d commit f80c008
Show file tree
Hide file tree
Showing 10 changed files with 393 additions and 167 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
################################################################################
# 此 .gitignore 文件已由 Microsoft(R) Visual Studio 自动创建。
################################################################################

/.vs
/out/build
4 changes: 1 addition & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
cmake_minimum_required (VERSION 3.11)
project (pfs_upk)

set(CMAKE_BUILD_TYPE Release)

set(CMAKE_CXX_STANDARD 17)
add_definitions(-DUNICODE -D_UNICODE)

add_executable(pfs_upk pfs_upk.cpp pack.cpp unpack.cpp xorcrypt.cpp sha1.c)
add_executable(pfs_upk pfs_upk.cpp pack.cpp unpack.cpp xorcrypt.cpp sha1.c MemoryStream.cpp csharp_types.h MemoryStream.h)

if (CMAKE_SYSTEM_NAME MATCHES "Linux")
target_link_libraries(pfs_upk stdc++fs)
Expand Down
26 changes: 26 additions & 0 deletions CMakeSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"configurations": [
{
"name": "x64-Debug",
"generator": "Ninja",
"configurationType": "Debug",
"inheritEnvironments": [ "msvc_x64_x64" ],
"buildRoot": "${projectDir}\\out\\build\\${name}",
"installRoot": "${projectDir}\\out\\install\\${name}",
"cmakeCommandArgs": "",
"buildCommandArgs": "",
"ctestCommandArgs": ""
},
{
"name": "x64-Release",
"generator": "Ninja",
"configurationType": "Release",
"buildRoot": "${projectDir}\\out\\build\\${name}",
"installRoot": "${projectDir}\\out\\install\\${name}",
"cmakeCommandArgs": "",
"buildCommandArgs": "",
"ctestCommandArgs": "",
"inheritEnvironments": [ "msvc_x64_x64" ]
}
]
}
135 changes: 135 additions & 0 deletions MemoryStream.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
#include<vector>
#include<memory>
#include"MemoryStream.h"

using namespace std;

BYTE* MemoryStream::GetBase()
{
return _base.get();
}
const uint MemoryStream::GetPosition() const
{
return Position;
}
const size_t MemoryStream::GetSize() const
{
return Length;
}
const BYTE* MemoryStream::GetBase() const
{
return _base.get();
}
BYTE* MemoryStream::GetPtr()
{
return _base.get() + Position;
}
const BYTE* MemoryStream::GetPtr() const
{
return _base.get() + Position;
}

void MemoryStream::Close()
{
_base.reset();
}
void MemoryStream::Dispose()
{
Close();
}
long MemoryStream::Seek(unsigned int _offset, int _Mode)
{
switch (_Mode)
{
case MEM_BEG:
{
Position = _offset;
break;
}
case MEM_CUR:
{
Position += _offset;
break;
}
case MEM_END:
{
Position = Length - _offset;
break;
}
default:
{
break;
}
}
return Position;
}
long MemoryStream::Read(vector<BYTE>& _buffer, unsigned int _size)
{
Read(_buffer.data(), _size);
return _size;
}
long MemoryStream::Read(void* _buffer, unsigned int _size)
{
memcpy(_buffer, _base.get() + Position, _size);
Position += _size;
return _size;
}
template<typename _Ty>
void MemoryStream::Write(const vector<_Ty>& _source)
{
Write(_source.data(), _source.size());
}
template<typename _Ty>
void MemoryStream::Write(size_t _Pos, const _Ty* _Src, size_t _Size)
{
if (Position + _Size * sizeof(_Ty) / sizeof(BYTE) > Length) return;
memcpy(_base.get() + _Pos, _Src, _Size * sizeof(_Ty) / sizeof(BYTE));
Position += _Size;
}
template<typename _Ty>
void MemoryStream::Write(const _Ty* _Src, size_t _Size)
{
Write(Position, (BYTE*)_Src, _Size);
}
void MemoryStream::Write(const MemoryStream& _Src)
{
Write(Position, _Src.GetBase(), _Src.Length);
}
void MemoryStream::Write(const MemoryStream* _Src)
{
Write(*(_Src));
}
void MemoryStream::Write(const char* _Src, size_t _Size)
{
Write(Position, (BYTE*)_Src, _Size);
}
void MemoryStream::Write(const unsigned int* _Src, size_t _Size)
{
Write(Position, (BYTE*)_Src, _Size);
}
void MemoryStream::Write(const int _Val, size_t _Size)
{
const BYTE* _Ptr = (BYTE*)&_Val;
for (size_t i = 0; i < _Size; i++)
{
Write(_Ptr, sizeof(BYTE));
}
}
MemoryStream::MemoryStream(BYTE* _Ptr, unsigned int _size)
{
Position = 0;
Length = _size;
_base = std::unique_ptr<BYTE>(_Ptr);
}
MemoryStream::MemoryStream(const vector<BYTE>& _source)
{
std::unique_ptr<BYTE> _real(new BYTE[_source.size()]);
Length = _source.size();
Position = 0;
memcpy(_real.get(), _source.data(), _source.size() * sizeof(BYTE));
_base = std::move(_real);
}
MemoryStream::~MemoryStream()
{
this->Dispose();
}
53 changes: 53 additions & 0 deletions MemoryStream.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#pragma once
#define MEM_BEG 0
#define MEM_CUR 1
#define MEM_END 2
#include<iostream>
#include<vector>
#include<memory>
#include"csharp_types.h"


class MemoryStream
{
private:
std::unique_ptr<BYTE> _base;
public:
uint Position = 0;
uint Length = 0;

BYTE* GetBase();
const BYTE* GetBase() const;
const uint GetPosition() const;
const size_t GetSize() const;
BYTE* GetPtr();
const BYTE* GetPtr() const;

long Seek(unsigned int _offset, int _Mode = MEM_BEG);

long Read(std::vector<BYTE>& _buffer, unsigned int _size);
long Read(void* _buffer, unsigned int _size);

template<typename _Ty>
void Write(const std::vector<_Ty>& _source);
template<typename _Ty>
void Write(size_t _Pos, const _Ty* _Src, size_t _Size);
template<typename _Ty>
void Write(const _Ty* _Src, size_t _Size);
void Write(const MemoryStream& _Src); //Similar with CopyTo();
void Write(const MemoryStream* _Src); //Similar with CopyTo();
void Write(const char* _Src, size_t _Size); //Special Judgement, don't know why doesn't work with template
void Write(const unsigned int* _Src, size_t _Size);
void Write(const int _Val, size_t _Size); //Similar with memset(), Param _Val only receives int and will be converted to BYTE;

void Insert(const size_t _offset, const std::vector<BYTE>& _source);
void Insert(const size_t _offset, const void* _source, size_t _Size);

void Erase(const size_t _offset, size_t _Size);

MemoryStream(BYTE* _Ptr, unsigned int _size);
MemoryStream(const std::vector<BYTE>& _source);
~MemoryStream();
void Close();
void Dispose();
};
10 changes: 10 additions & 0 deletions csharp_types.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#pragma once
#include<iostream>
#include<fstream>
#include<vector>
#include<map>
#include<Windows.h>

typedef unsigned int uint;
typedef unsigned long ulong;
typedef unsigned short ushort;
Loading

0 comments on commit f80c008

Please sign in to comment.