Skip to content

Commit

Permalink
Replace most returns with CHECK_ASSERTION
Browse files Browse the repository at this point in the history
Also fix some Seek methods return types being unsigned, while returning
negative errors.

Added the CHECK_ASSERTION macro checks in a couple more places.

Simplified CHECK_ASSERTION macro usage.
  • Loading branch information
tambry committed Nov 9, 2015
1 parent 9c2f48c commit 5d5a4f8
Show file tree
Hide file tree
Showing 14 changed files with 159 additions and 394 deletions.
8 changes: 1 addition & 7 deletions Utilities/File.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -327,13 +327,7 @@ int OSCopyFile(const char* source, const char* destination, bool overwrite)
//sendfile will work with non-socket output (i.e. regular file) on Linux 2.6.33+
off_t bytesCopied = 0;
struct stat fileinfo = { 0 };

s32 ret = fstat(input, &fileinfo);
if (ret < 0)
{
return -1;
}

fstat(input, &fileinfo);
int result = sendfile(output, input, &bytesCopied, fileinfo.st_size) == -1 ? -1 : 0;
#endif

Expand Down
42 changes: 8 additions & 34 deletions Utilities/VirtualMemory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,55 +17,29 @@ namespace memory_helper
{
#ifdef _WIN32
void* ret = VirtualAlloc(NULL, size, MEM_RESERVE, PAGE_NOACCESS);
if (ret == NULL)
{
LOG_ERROR(HLE, "reserve_memory VirtualAlloc failed.");
return (void*)VM_FAILURE;
}
CHECK_ASSERTION(ret != NULL);
#else
void* ret = mmap(nullptr, size, PROT_NONE, MAP_ANON | MAP_PRIVATE, -1, 0);
if (ret == (void*)VM_FAILURE)
{
LOG_ERROR(HLE, "reserve_memory mmap failed.");
}
CHECK_ASSERTION(ret != 0);
#endif
return ret;
}

s32 commit_page_memory(void* pointer, size_t page_size)
void commit_page_memory(void* pointer, size_t page_size)
{
#ifdef _WIN32
if (VirtualAlloc((u8*)pointer, page_size, MEM_COMMIT, PAGE_READWRITE) == NULL)
{
LOG_ERROR(HLE, "commit_page_memory VirtualAlloc failed.");
return VM_FAILURE;
}
CHECK_ASSERTION(VirtualAlloc((u8*)pointer, page_size, MEM_COMMIT, PAGE_READWRITE) != NULL);
#else
s32 ret = mprotect((u8*)pointer, page_size, PROT_READ | PROT_WRITE);
if (ret < VM_SUCCESS)
{
LOG_ERROR(HLE, "commit_page_memory mprotect failed. (%d)", ret);
return VM_FAILURE;
}
CHECK_ASSERTION(mprotect((u8*)pointer, page_size, PROT_READ | PROT_WRITE) != -1);
#endif
return VM_SUCCESS;
}

s32 free_reserved_memory(void* pointer, size_t size)
void free_reserved_memory(void* pointer, size_t size)
{
#ifdef _WIN32
if (VirtualFree(pointer, 0, MEM_RELEASE) == 0)
{
LOG_ERROR(HLE, "free_reserved_memory VirtualFree failed.");
return VM_FAILURE;
}
CHECK_ASSERTION(VirtualFree(pointer, 0, MEM_RELEASE) != 0);
#else
if (munmap(pointer, size) != VM_SUCCESS)
{
LOG_ERROR(HLE, "free_reserved_memory munmap failed.");
return VM_FAILURE;
}
CHECK_ASSERTION(munmap(pointer, size) == 0);
#endif
return VM_SUCCESS;
}
}
20 changes: 2 additions & 18 deletions Utilities/VirtualMemory.h
Original file line number Diff line number Diff line change
@@ -1,38 +1,22 @@
#pragma once

// Failure codes for the functions
enum
{
VM_SUCCESS = 0,
VM_FAILURE = -1,
};

namespace memory_helper
{
/**
* Reserve size bytes of virtual memory and returns it.
* The memory should be commited before usage.
*
* Returns the base address of the allocated region of pages, if successful.
* Returns (void*)VM_FAILURE, if unsuccessful.
*/
void* reserve_memory(size_t size);

/**
* Commit page_size bytes of virtual memory starting at pointer.
* That is, bake reserved memory with physical memory.
* pointer should belong to a range of reserved memory.
*
* Returns VM_SUCCESS, if successful.
* Returns VM_FAILURE, if unsuccessful.
*/
s32 commit_page_memory(void* pointer, size_t page_size);
void commit_page_memory(void* pointer, size_t page_size);

/**
* Free memory alloced via reserve_memory.
*
* Returns VM_SUCCESS, if successful.
* Returns VM_FAILURE, if unsuccessful.
*/
s32 free_reserved_memory(void* pointer, size_t size);
void free_reserved_memory(void* pointer, size_t size);
}
10 changes: 3 additions & 7 deletions Utilities/rPlatform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

#ifndef _WIN32
#include <dirent.h>
#include <errno.h>
#endif

#include "rPlatform.h"
Expand Down Expand Up @@ -57,14 +58,9 @@ std::string rPlatform::getConfigDir()
dir = "./config";
dir = dir + "/rpcs3/";

s32 ret = mkdir(dir.c_str(), 0777);
if (ret == EEXIST)
if (mkdir(dir.c_str(), 0777) == -1)
{
LOG_WARNING(HLE, "Configuration directory already exists. (%s)", dir);
}
else if (ret < 0)
{
LOG_ERROR(HLE, "An error occured during the creation of the configuration directory. (%d)", ret);
printf("An error occured during the creation of the configuration directory. (%d)", errno);
}
#endif
}
Expand Down
38 changes: 12 additions & 26 deletions rpcs3/Crypto/unedat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,12 +172,8 @@ int decrypt_data(const fs::file* in, const fs::file* out, EDAT_HEADER *edat, NPD
{
metadata_sec_offset = metadata_offset + (unsigned long long) i * metadata_section_size;

if (in->seek(metadata_sec_offset) < 0)
{
LOG_ERROR(LOADER, "EDAT: Seeking medata section offset at %u failed.", metadata_sec_offset);
return 1;
}

CHECK_ASSERTION(in->seek(metadata_sec_offset) != -1);

unsigned char metadata[0x20];
memset(metadata, 0, 0x20);
in->read(metadata, 0x20);
Expand Down Expand Up @@ -205,7 +201,7 @@ int decrypt_data(const fs::file* in, const fs::file* out, EDAT_HEADER *edat, NPD
{
// If FLAG 0x20, the metadata precedes each data block.
metadata_sec_offset = metadata_offset + (unsigned long long) i * (metadata_section_size + length);
in->seek(metadata_sec_offset);
CHECK_ASSERTION(in->seek(metadata_sec_offset) != -1);

unsigned char metadata[0x20];
memset(metadata, 0, 0x20);
Expand All @@ -226,7 +222,7 @@ int decrypt_data(const fs::file* in, const fs::file* out, EDAT_HEADER *edat, NPD
else
{
metadata_sec_offset = metadata_offset + (unsigned long long) i * metadata_section_size;
in->seek(metadata_sec_offset);
CHECK_ASSERTION(in->seek(metadata_sec_offset) != -1);

in->read(hash_result, 0x10);
offset = metadata_offset + (unsigned long long) i * edat->block_size + (unsigned long long) block_num * metadata_section_size;
Expand All @@ -248,11 +244,7 @@ int decrypt_data(const fs::file* in, const fs::file* out, EDAT_HEADER *edat, NPD
memset(hash, 0, 0x10);
memset(key_result, 0, 0x10);

if (in->seek(offset) < 0)
{
LOG_ERROR(LOADER, "EDAT: Seeking offset at %u failed.", offset);
return 1;
}
CHECK_ASSERTION(in->seek(offset) != -1);

in->read(enc_data, length);

Expand Down Expand Up @@ -357,7 +349,7 @@ int decrypt_data(const fs::file* in, const fs::file* out, EDAT_HEADER *edat, NPD

int check_data(unsigned char *key, EDAT_HEADER *edat, NPD_HEADER *npd, const fs::file* f, bool verbose)
{
f->seek(0);
CHECK_ASSERTION(f->seek(0) != -1);
unsigned char header[0xA0];
unsigned char empty_header[0xA0];
unsigned char header_hash[0x10];
Expand Down Expand Up @@ -402,10 +394,7 @@ int check_data(unsigned char *key, EDAT_HEADER *edat, NPD_HEADER *npd, const fs:
f->read(header, 0xA0);

// Read in the header and metadata section hashes.
if (f->seek(0x90) < 0)
{
LOG_ERROR(LOADER, "EDAT: Seeking header at 0x90 failed.");
}
CHECK_ASSERTION(f->seek(0x90) != -1);

f->read(metadata_hash, 0x10);
f->read(header_hash, 0x10);
Expand Down Expand Up @@ -462,10 +451,7 @@ int check_data(unsigned char *key, EDAT_HEADER *edat, NPD_HEADER *npd, const fs:
while (bytes_to_read > 0)
{
// Locate the metadata blocks.
if (f->seek(metadata_section_offset) < 0)
{
LOG_ERROR(LOADER, "EDAT: Seeking metadata blocks at %u failed.", metadata_section_offset);
}
CHECK_ASSERTION(f->seek(metadata_section_offset) != -1);

// Read in the metadata.
f->read(metadata + bytes_read, metadata_section_size);
Expand Down Expand Up @@ -512,9 +498,9 @@ int check_data(unsigned char *key, EDAT_HEADER *edat, NPD_HEADER *npd, const fs:


// Read in the metadata and header signatures.
f->seek(0xB0);
CHECK_ASSERTION(f->seek(0xB0) != -1);
f->read(metadata_signature, 0x28);
f->seek(0xD8);
CHECK_ASSERTION(f->seek(0xD8) != -1);
f->read(header_signature, 0x28);

// Checking metadata signature.
Expand All @@ -530,7 +516,7 @@ int check_data(unsigned char *key, EDAT_HEADER *edat, NPD_HEADER *npd, const fs:
{
int metadata_buf_size = block_num * 0x10;
unsigned char *metadata_buf = new unsigned char[metadata_buf_size];
f->seek(metadata_offset);
CHECK_ASSERTION(f->seek(metadata_offset) != -1);
f->read(metadata_buf, metadata_buf_size);
sha1(metadata_buf, metadata_buf_size, signature_hash);
delete[] metadata_buf;
Expand Down Expand Up @@ -563,7 +549,7 @@ int check_data(unsigned char *key, EDAT_HEADER *edat, NPD_HEADER *npd, const fs:
// Setup header signature hash.
memset(signature_hash, 0, 20);
unsigned char *header_buf = new unsigned char[0xD8];
f->seek(0x00);
CHECK_ASSERTION(f->seek(0x00) != -1);
f->read(header_buf, 0xD8);
sha1(header_buf, 0xD8, signature_hash );
delete[] header_buf;
Expand Down
6 changes: 1 addition & 5 deletions rpcs3/Crypto/unpkg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,7 @@ bool UnpackPKG(const fs::file& pkg_f, const std::string& dir, volatile f64& prog
// Define decryption subfunction (`psp` arg selects the key for specific block)
auto decrypt = [&](u64 offset, u64 size, bool psp) -> u64
{
if (pkg_f.seek(start_offset + header.data_offset + offset) < 0)
{
LOG_ERROR(LOADER, "PKG: Package file seek to %u failed.", start_offset + header.data_offset + offset);
return -1;
}
CHECK_ASSERTION(pkg_f.seek(start_offset + header.data_offset + offset) != -1);

// Read the data and set available size
const u64 read = pkg_f.read(buf.get(), size);
Expand Down
Loading

0 comments on commit 5d5a4f8

Please sign in to comment.