Browse Source

stylistic cleanup

master
despair 5 years ago
parent
commit
2a1e7e42f2
  1. 21
      src/CFGFileParser.cpp
  2. 10
      src/CFGFileParser.h
  3. 28
      src/TLSFAlloc.h
  4. 3
      src/graphics/components/AnimeComponent.cpp
  5. 6
      src/main.cpp
  6. 45
      src/networking/HTTPSRequest.cpp
  7. 1
      src/networking/HTTPSRequest.h
  8. 2
      src/pnm.cpp
  9. 10
      src/pnm.h
  10. 5
      src/tlsf-winnt.h
  11. 2
      src/tlsf.c

21
src/CFGFileParser.cpp

@ -1,7 +1,6 @@ @@ -1,7 +1,6 @@
#include "CFGFileParser.h"
CFGFileParser::CFGFileParser(const char* filename)
{
CFGFileParser::CFGFileParser(const char* filename){
// Open the config file, get its size,
// allocate the buffer, read it into
// the buffer, close the file
@ -12,18 +11,15 @@ CFGFileParser::CFGFileParser(const char* filename) @@ -12,18 +11,15 @@ CFGFileParser::CFGFileParser(const char* filename)
fclose(cfg_file);
}
CFGFileParser::~CFGFileParser()
{
CFGFileParser::~CFGFileParser(){
// clean up!
tlsf_free(buffer);
}
namespace ntr{
size_t string_hash(const fast_string &str)
{
size_t string_hash(const fast_string &str){
size_t out = 0;
// instead of tying this to i386/amd64
// because someone is eventually going to
// instead of tying this to i386/amd64, because someone is eventually going to
// port this to ARM or some RISC nonsense
#if UINTPTR_MAX == 0xffffffff
MurmurHash3_x86_128 ( &str, sizeof(str), 7904542L, &out );
@ -34,12 +30,13 @@ namespace ntr{ @@ -34,12 +30,13 @@ namespace ntr{
return out;
}
bool fast_string_compare(fast_string t1, fast_string t2)
{
bool fast_string_compare(fast_string t1, fast_string t2){
int i = strcmp(t1.c_str(),t2.c_str());
if (!i)
if (!i){
return true;
else
}
else{
return false;
}
}
}

10
src/CFGFileParser.h

@ -39,8 +39,14 @@ class CFGFileParser { @@ -39,8 +39,14 @@ class CFGFileParser {
public:
CFGFileParser(const char* filename);
~CFGFileParser();
static void* operator new(std::size_t n){ if (void *mem = tlsf_malloc(n)) return mem; throw std::bad_alloc {}; }
static void operator delete(void *p){tlsf_free(p);}
static void* operator new(std::size_t n){
if (void *mem = tlsf_malloc(n)){
return mem;
} throw std::bad_alloc {};
}
static void operator delete(void *p){
tlsf_free(p);
}
private:
FILE *cfg_file;
char *buffer;

28
src/TLSFAlloc.h

@ -19,10 +19,16 @@ namespace ntr { @@ -19,10 +19,16 @@ namespace ntr {
typedef std::ptrdiff_t diff_type;
template <class U>
struct rebind { typedef TLSFAlloc<U> other; };
struct rebind {
typedef TLSFAlloc<U> other;
};
ptr address (ref value) const {return &value;}
const_ptr address (const_ref value) const {return &value;}
ptr address (ref value) const {
return &value;
}
const_ptr address (const_ref value) const {
return &value;
}
// these are the constructors for same: had I not ported sbrk(2) to winnt,
// the constructor would have generated a memory pool from above.
@ -32,7 +38,9 @@ namespace ntr { @@ -32,7 +38,9 @@ namespace ntr {
~TLSFAlloc() throw() {}
// return maximum number of elements that can be allocated
size_type max_size() const throw() {return std::numeric_limits<std::size_t>::max() / sizeof(T);}
size_type max_size() const throw() {
return std::numeric_limits<std::size_t>::max() / sizeof(T);
}
// allocate but don't initialize num elements of type T
ptr allocate(size_type num, const void* = 0) {
@ -47,12 +55,18 @@ namespace ntr { @@ -47,12 +55,18 @@ namespace ntr {
}
// initialize elements of allocated storage p with value value
// using placement new (defined here)
void construct (ptr p, const T& value) {new (reinterpret_cast<void*>(p))T(value);}
void construct (ptr p, const T& value) {
new (reinterpret_cast<void*>(p))T(value);
}
// placement new
void* operator new (std::size_t size, void*){return allocate(size);}
void* operator new (std::size_t size, void*){
return allocate(size);
}
// destroy elements of initialized storage p
void destroy (ptr p) {p->~T();}
void destroy (ptr p) {
p->~T();
}
// deallocate storage p of deleted elements
void deallocate(ptr p, size_type num) {

3
src/graphics/components/AnimeComponent.cpp

@ -25,7 +25,7 @@ for (int py = 0; py < 1024; py++) { @@ -25,7 +25,7 @@ for (int py = 0; py < 1024; py++) {
}
}
}
#else // this is either non-Windows or using Microsoft C/C++
#else // this is either non-Windows or Microsoft C/C++
RGBAPNMObject *anime = readPPM("anime.pnm");
if (!anime) {
std::cout << "Can't set up anime component, couldn't read anime.pnm" << std::endl;
@ -84,6 +84,7 @@ for (int py = 0; py < 1024; py++) { @@ -84,6 +84,7 @@ for (int py = 0; py < 1024; py++) {
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1024, 1024, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D);
#ifndef __MINGW32__
tlsf_free(anime->m_Ptr);
delete anime;

6
src/main.cpp

@ -78,13 +78,11 @@ bool fileExists(const std::string s) { @@ -78,13 +78,11 @@ bool fileExists(const std::string s) {
}
int main(int argc, char *argv[]) {
// no longer require a URL
/*
if (argc == 1) {
// show help msg when "--help" appears
if (strcmp(argv[1], "--help")==0) {
std::cout << "./netrunner [http://host.tld/|/path/to/file.html] [-log <error|warning|notice|info|debug>]" << std::endl;
return 1;
}
*/
# ifdef _WIN32
init_heap(); // the NT port requires it. We do it at startup now, to allow 2LSF to run at any time
# endif

45
src/networking/HTTPSRequest.cpp

@ -12,8 +12,7 @@ mbedtls_ssl_config conf; @@ -12,8 +12,7 @@ mbedtls_ssl_config conf;
mbedtls_x509_crt cacert;
HTTPSRequest::HTTPSRequest(const std::shared_ptr<URL> u) {
if (!initTLS())
{
if (!initTLS()){
printf("failed to start TLS!");
return;
}
@ -32,13 +31,11 @@ bool HTTPSRequest::sendRequest(std::function<void(const HTTPResponse&)> response @@ -32,13 +31,11 @@ bool HTTPSRequest::sendRequest(std::function<void(const HTTPResponse&)> response
int ret;
unsigned flags;
if(mbedtls_net_connect(&server_fd, host.c_str(),port.c_str(), MBEDTLS_NET_PROTO_TCP) != 0 )
{
if(mbedtls_net_connect(&server_fd, host.c_str(),port.c_str(), MBEDTLS_NET_PROTO_TCP) != 0 ){
return false;
}
if (mbedtls_ssl_config_defaults(&conf,MBEDTLS_SSL_IS_CLIENT,MBEDTLS_SSL_TRANSPORT_STREAM,MBEDTLS_SSL_PRESET_DEFAULT) != 0)
{
if (mbedtls_ssl_config_defaults(&conf,MBEDTLS_SSL_IS_CLIENT,MBEDTLS_SSL_TRANSPORT_STREAM,MBEDTLS_SSL_PRESET_DEFAULT) != 0){
return false;
}
@ -46,51 +43,45 @@ bool HTTPSRequest::sendRequest(std::function<void(const HTTPResponse&)> response @@ -46,51 +43,45 @@ bool HTTPSRequest::sendRequest(std::function<void(const HTTPResponse&)> response
mbedtls_ssl_conf_rng(&conf, mbedtls_ctr_drbg_random, &ctr_drbg);
mbedtls_ssl_conf_ca_chain( &conf, &cacert, nullptr );
if( mbedtls_ssl_setup( &ssl, &conf ) != 0 )
{
if( mbedtls_ssl_setup( &ssl, &conf ) != 0 ){
return false;
}
if(mbedtls_ssl_set_hostname( &ssl, uri->host.c_str() ) != 0 )
{
if(mbedtls_ssl_set_hostname( &ssl, uri->host.c_str() ) != 0 ){
return false;
}
mbedtls_ssl_set_bio( &ssl, &server_fd, mbedtls_net_send, mbedtls_net_recv, nullptr );
int state = mbedtls_ssl_handshake( &ssl );
while(state != 0)
{
if( state != MBEDTLS_ERR_SSL_WANT_READ && state != MBEDTLS_ERR_SSL_WANT_WRITE )
{
while(state != 0){
if( state != MBEDTLS_ERR_SSL_WANT_READ && state != MBEDTLS_ERR_SSL_WANT_WRITE ){
printf( " failed\n ! mbedtls_ssl_handshake returned -0x%x\n\n", -state );
return false;
}
}
if( ( flags = mbedtls_ssl_get_verify_result( &ssl ) ) != 0 )
{
if( ( flags = mbedtls_ssl_get_verify_result( &ssl ) ) != 0 ){
printf("Invalid server cert!");
return false;
}
const std::string request = methodToString(method) + std::string(" ") + document + std::string(" ") + versionToString(version) + std::string("\r\nHost: ") + host + std::string("\r\nUser-Agent: ") + userAgent + std::string("\r\n\r\n");
while( ( state = mbedtls_ssl_write( &ssl, reinterpret_cast<const unsigned char*>(request.c_str()), request.length() ) ) <= 0 )
{
if( state != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE )
{
while( ( state = mbedtls_ssl_write( &ssl, reinterpret_cast<const unsigned char*>(request.c_str()), request.length() ) ) <= 0 ){
if( state != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE ){
printf( " failed\n ! mbedtls_ssl_write returned %d\n\n", state );
return false;
}
}
do
{
do{
ret = mbedtls_ssl_read( &ssl, reinterpret_cast<unsigned char *>(buffer), 512 );
if (ret <= 0)
if (ret <= 0){
break;
else
}
else{
response += std::string(buffer, static_cast<unsigned int>(ret));
}
}
while( ret != 0 );
@ -136,14 +127,12 @@ bool HTTPSRequest::initTLS() @@ -136,14 +127,12 @@ bool HTTPSRequest::initTLS()
const char *seed = "!@netrunner_ssl_seed$%?rvx86_despair##^^%$#@";
mbedtls_entropy_init( &entropy );
if(mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy, reinterpret_cast<const unsigned char*>(seed), strlen(seed) ) != 0 )
{
if(mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy, reinterpret_cast<const unsigned char*>(seed), strlen(seed) ) != 0 ){
return false;
}
int ret = mbedtls_x509_crt_parse_file( &cacert, "ca-bundle.crt");
if( ret < 0 )
{
if( ret < 0 ){
printf( " failed\n ! mbedtls_x509_crt_parse returned -0x%x\n\n", -ret );
return false;
}

1
src/networking/HTTPSRequest.h

@ -10,6 +10,7 @@ @@ -10,6 +10,7 @@
#undef NULL
#define NULL nullptr
#endif
// PolarSSL
#include <mbedtls/ssl.h>
#include <mbedtls/entropy.h>

2
src/pnm.cpp

@ -58,7 +58,7 @@ RGBAPNMObject * readPPM(const char* fileName) { @@ -58,7 +58,7 @@ RGBAPNMObject * readPPM(const char* fileName) {
}
// allocate array for pixels
char* pixels = reinterpret_cast<char*>(tlsf_calloc(size, sizeof(char)));/*new char[size]*/;
char* pixels = reinterpret_cast<char*>(tlsf_calloc(size, sizeof(char))); // used to be: new char[size];
// unformatted read of binary pixel data
res = fread(pixels, sizeof(int), size, fr);

10
src/pnm.h

@ -8,8 +8,14 @@ struct RGBAPNMObject { @@ -8,8 +8,14 @@ struct RGBAPNMObject {
char * m_Ptr;
// fast allocation!
static void* operator new(size_t n){if (void *mem = tlsf_malloc(n)) return mem; throw std::bad_alloc {}; }
static void operator delete(void *p){tlsf_free(p);}
static void* operator new(size_t n){
if (void *mem = tlsf_malloc(n)){
return mem;
} throw std::bad_alloc {};
}
static void operator delete(void *p){
tlsf_free(p);
}
};
RGBAPNMObject * readPPM(const char* fileName);

5
src/tlsf-winnt.h

@ -1,4 +1,4 @@ @@ -1,4 +1,4 @@
/* Heap management routines (including unexec) for NetRunner on Windows NT.
/* Heap management routines for NetRunner on Windows NT.
This file was taken from GNU Emacs.
Copyright (C) 1994, 2001, 2002, 2003, 2004, 2005,
@ -37,7 +37,7 @@ extern int nt_minor_version; @@ -37,7 +37,7 @@ extern int nt_minor_version;
extern int nt_build_number;
enum {
OS_WIN95 = 1,
OS_WIN95 = 1, // cus someone demanded Win32s support, arrrgh (yes, this is a C99 compliant comment)
OS_NT
};
@ -76,6 +76,7 @@ IMAGE_SECTION_HEADER * find_section (char * name, IMAGE_NT_HEADERS * nt_header); @@ -76,6 +76,7 @@ IMAGE_SECTION_HEADER * find_section (char * name, IMAGE_NT_HEADERS * nt_header);
/* Return pointer to section header for section containing the given
relative virtual address. */
IMAGE_SECTION_HEADER * rva_to_section (DWORD rva, IMAGE_NT_HEADERS * nt_header);
#define RVA_TO_PTR(rva) ((unsigned char *)((DWORD)(rva) + (DWORD)GetModuleHandle(NULL)))
#ifndef VALBITS
#define VALBITS (32 - 3)

2
src/tlsf.c

@ -50,6 +50,8 @@ extern "C" { @@ -50,6 +50,8 @@ extern "C" {
*
* - Support for ARMv5 implemented
*
* (22 Aug 2017) despair:
* - Inlined implementation of sbrk(3) for WinNT and Win32s
*/
/*#define USE_SBRK (0) */

Loading…
Cancel
Save