backport more changes from snes9x master

This commit is contained in:
Daryl Borth 2018-08-11 14:58:21 -06:00
parent 856a1ac127
commit 5866fd20db
6 changed files with 1559 additions and 0 deletions

388
source/snes9x/bml.cpp Normal file
View File

@ -0,0 +1,388 @@
#include <vector>
#include <string.h>
#include <stdio.h>
#include "port.h"
#include "bml.h"
static char *strndup_p(char *str, int len)
{
char *buffer;
int n;
buffer = (char *) malloc (len + 1);
if (buffer)
{
for (n = 0; ((n < len) && (str[n] != 0)); n++) buffer[n] = str[n];
buffer[n] = '\0';
}
return buffer;
}
static inline bml_node *bml_node_new (void)
{
bml_node *node = new bml_node;
node->data = NULL;
node->name = NULL;
node->depth = -1;
return node;
}
static inline int islf(char c)
{
return (c == '\r' || c == '\n');
}
static inline int isblank(char c)
{
return (c == ' ' || c == '\t');
}
static inline int isalnum(char c)
{
return ((c >= 'a' && c <= 'z') ||
(c >= 'A' && c <= 'Z') ||
(c >= '0' && c <= '9'));
}
static inline int bml_valid (char c)
{
return (isalnum (c) || c == '-');
}
static char *strndup_trim (char *str, int len)
{
int start;
int end;
for (start = 0; str[start] && start != len && isblank (str[start]); start++) {}
if (!str[start] || start >= len)
return strdup ("");
for (end = len - 1; isblank (str[end]) || str[end] == '\n' || str[end] == '\r'; end--) {}
return strndup_p (str + start, end - start + 1);
}
static inline unsigned int bml_read_depth (char *data)
{
unsigned int depth;
for (depth = 0; isblank (data[depth]); depth++) {}
return depth;
}
static void bml_parse_depth (bml_node *node, char **data)
{
unsigned int depth = bml_read_depth (*data);
*data += depth;
node->depth = depth;
}
static void bml_parse_name (bml_node *node, char **data)
{
int len;
for (len = 0; bml_valid(*(*data + len)); len++) {};
node->name = strndup_trim (*data, len);
*data += len;
}
static void bml_parse_data (bml_node *node, char **data)
{
char *p = *data;
int len;
if (p[0] == '=' && p[1] == '\"')
{
len = 2;
while (p[len] && p[len] != '\"' && !islf (p[len]))
len++;
if (p[len] != '\"')
return;
node->data = strndup_p (p + 2, len - 2);
*data += len + 1;
}
else if (*p == '=')
{
len = 1;
while (p[len] && !islf (p[len]) && p[len] != '"' && p[len] != ' ')
len++;
if (p[len] == '\"')
return;
node->data = strndup_trim (p + 1, len - 1);
*data += len;
}
else if (*p == ':')
{
len = 1;
while (p[len] && !islf (p[len]))
len++;
node->data = strndup_trim (p + 1, len - 1);
*data += len;
}
return;
}
static void bml_skip_empty (char **data)
{
char *p = *data;
while (*p)
{
for (; *p && isblank (*p) ; p++) {}
if (!islf(p[0]) && (p[0] != '/' && p[1] != '/'))
return;
/* Skip comment data */
while (*p && *p != '\r' && *p != '\n')
p++;
/* If we found new-line, try to skip more */
if (*p)
{
p++;
*data = p;
}
}
}
static char *bml_read_line (char **data)
{
char *line;
char *p;
bml_skip_empty (data);
line = *data;
if (line == NULL || *line == '\0')
return NULL;
p = strpbrk (line, "\r\n\0");
if (p == NULL)
return NULL;
if (islf (*p))
{
*p = '\0';
p++;
}
*data = p;
return line;
}
static void bml_parse_attr (bml_node *node, char **data)
{
char *p = *data;
bml_node *n;
int len;
while (*p && !islf (*p))
{
if (*p != ' ')
return;
while (isblank (*p))
p++;
if (p[0] == '/' && p[1] == '/')
break;
n = bml_node_new ();
len = 0;
while (bml_valid (p[len]))
len++;
if (len == 0)
return;
n->name = strndup_trim (p, len);
p += len;
bml_parse_data (n, &p);
n->depth = bml_attr_type;
node->child.push_back (n);
}
*data = p;
}
static int contains_space (char *str)
{
for (int i = 0; str[i]; i++)
{
if (isblank (str[i]))
return 1;
}
return 0;
}
static void bml_print_node (bml_node *node, int depth)
{
int i;
if (!node)
return;
for (i = 0; i < depth * 2; i++)
{
printf (" ");
}
if (node->name)
printf ("%s", node->name);
if (node->data)
{
if (contains_space (node->data))
printf ("=\"%s\"", node->data);
else
printf (": %s", node->data);
}
for (i = 0; i < (int) node->child.size () && node->child[i]->depth == bml_attr_type; i++)
{
if (node->child[i]->name)
{
printf (" %s", node->child[i]->name);
if (node->child[i]->data)
{
if (contains_space (node->child[i]->data))
printf ("=\"%s\"", node->child[i]->data);
else
printf ("=%s", node->child[i]->data);
}
}
}
if (depth >= 0)
printf ("\n");
for (; i < (int) node->child.size(); i++)
{
bml_print_node (node->child[i], depth + 1);
}
if (depth == 0)
printf ("\n");
}
void bml_print_node (bml_node *node)
{
bml_print_node (node, -1);
}
static bml_node *bml_parse_node (char **doc)
{
char *line;
bml_node *node = NULL;
if ((line = bml_read_line (doc)))
{
node = bml_node_new ();
bml_parse_depth (node, &line);
bml_parse_name (node, &line);
bml_parse_data (node, &line);
bml_parse_attr (node, &line);
}
else
return NULL;
bml_skip_empty (doc);
while (*doc && (int) bml_read_depth (*doc) > node->depth)
{
bml_node *child = bml_parse_node (doc);
if (child)
node->child.push_back (child);
bml_skip_empty (doc);
}
return node;
}
void bml_free_node (bml_node *node)
{
delete[] (node->name);
delete[] (node->data);
for (unsigned int i = 0; i < node->child.size(); i++)
{
bml_free_node (node->child[i]);
delete node->child[i];
}
return;
}
bml_node *bml_parse (char **doc)
{
bml_node *root = NULL;
bml_node *node = NULL;
char *ptr = *doc;
root = bml_node_new ();
while ((node = bml_parse_node (&ptr)))
{
root->child.push_back (node);
}
if (!root->child.size())
{
delete root;
root = NULL;
}
return root;
}
bml_node *bml_find_sub (bml_node *n, const char *name)
{
unsigned int i;
for (i = 0; i < n->child.size (); i++)
{
if (!strcasecmp (n->child[i]->name, name))
return n->child[i];
}
return NULL;
}
bml_node *bml_parse_file (const char *filename)
{
FILE *file = NULL;
char *buffer = NULL;
int file_size = 0;
bml_node *node = NULL;
file = fopen (filename, "rb");
if (!file)
return NULL;
fseek (file, 0, SEEK_END);
file_size = ftell (file);
fseek (file, 0, SEEK_SET);
buffer = new char[file_size + 1];
fread (buffer, file_size, 1, file);
buffer[file_size] = '\0';
fclose (file);
node = bml_parse (&buffer);
delete[] buffer;
return node;
}

31
source/snes9x/bml.h Normal file
View File

@ -0,0 +1,31 @@
#ifndef __BML_H
#define __BML_H
#include <vector>
const int bml_attr_type = -2;
typedef struct bml_node
{
char *name;
char *data;
int depth;
std::vector<bml_node *> child;
} bml_node;
bml_node *bml_find_sub (bml_node *node, const char *name);
bml_node *bml_parse_file (const char *filename);
/* Parse character array into BML tree. Destructive to input. */
bml_node *bml_parse (char **buffer);
/* Recursively free bml_node and substructures */
void bml_free_node (bml_node *);
/* Print node structure to stdout */
void bml_print_node (bml_node *);
#endif

178
source/snes9x/sha256.cpp Normal file
View File

@ -0,0 +1,178 @@
/*********************************************************************
* Filename: sha256.c
* Author: Brad Conte (brad AT bradconte.com)
* Copyright:
* Disclaimer: This code is presented "as is" without any guarantees.
* Details: Implementation of the SHA-256 hashing algorithm.
SHA-256 is one of the three algorithms in the SHA2
specification. The others, SHA-384 and SHA-512, are not
offered in this implementation.
Algorithm specification can be found here:
* http://csrc.nist.gov/publications/fips/fips180-2/fips180-2withchangenotice.pdf
This implementation uses little endian byte order.
*********************************************************************/
/*************************** HEADER FILES ***************************/
#include <stdlib.h>
#include <memory.h>
/****************************** MACROS ******************************/
#define ROTLEFT(a,b) (((a) << (b)) | ((a) >> (32-(b))))
#define ROTRIGHT(a,b) (((a) >> (b)) | ((a) << (32-(b))))
#define CH(x,y,z) (((x) & (y)) ^ (~(x) & (z)))
#define MAJ(x,y,z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))
#define EP0(x) (ROTRIGHT(x,2) ^ ROTRIGHT(x,13) ^ ROTRIGHT(x,22))
#define EP1(x) (ROTRIGHT(x,6) ^ ROTRIGHT(x,11) ^ ROTRIGHT(x,25))
#define SIG0(x) (ROTRIGHT(x,7) ^ ROTRIGHT(x,18) ^ ((x) >> 3))
#define SIG1(x) (ROTRIGHT(x,17) ^ ROTRIGHT(x,19) ^ ((x) >> 10))
#define SHA256_BLOCK_SIZE 32 /* SHA256 outputs a 32 byte digest */
/**************************** DATA TYPES ****************************/
typedef unsigned char BYTE; /* 8-bit byte */
typedef unsigned int WORD; /* 32-bit word, change to "long" for 16-bit machines */
typedef struct {
BYTE data[64];
WORD datalen;
unsigned long long bitlen;
WORD state[8];
} SHA256_CTX;
/**************************** VARIABLES *****************************/
static const WORD k[64] = {
0x428a2f98,0x71374491,0xb5c0fbcf,0xe9b5dba5,0x3956c25b,0x59f111f1,0x923f82a4,0xab1c5ed5,
0xd807aa98,0x12835b01,0x243185be,0x550c7dc3,0x72be5d74,0x80deb1fe,0x9bdc06a7,0xc19bf174,
0xe49b69c1,0xefbe4786,0x0fc19dc6,0x240ca1cc,0x2de92c6f,0x4a7484aa,0x5cb0a9dc,0x76f988da,
0x983e5152,0xa831c66d,0xb00327c8,0xbf597fc7,0xc6e00bf3,0xd5a79147,0x06ca6351,0x14292967,
0x27b70a85,0x2e1b2138,0x4d2c6dfc,0x53380d13,0x650a7354,0x766a0abb,0x81c2c92e,0x92722c85,
0xa2bfe8a1,0xa81a664b,0xc24b8b70,0xc76c51a3,0xd192e819,0xd6990624,0xf40e3585,0x106aa070,
0x19a4c116,0x1e376c08,0x2748774c,0x34b0bcb5,0x391c0cb3,0x4ed8aa4a,0x5b9cca4f,0x682e6ff3,
0x748f82ee,0x78a5636f,0x84c87814,0x8cc70208,0x90befffa,0xa4506ceb,0xbef9a3f7,0xc67178f2
};
/*********************** FUNCTION DEFINITIONS ***********************/
void sha256_transform(SHA256_CTX *ctx, const BYTE data[])
{
WORD a, b, c, d, e, f, g, h, i, j, t1, t2, m[64];
for (i = 0, j = 0; i < 16; ++i, j += 4)
m[i] = (data[j] << 24) | (data[j + 1] << 16) | (data[j + 2] << 8) | (data[j + 3]);
for ( ; i < 64; ++i)
m[i] = SIG1(m[i - 2]) + m[i - 7] + SIG0(m[i - 15]) + m[i - 16];
a = ctx->state[0];
b = ctx->state[1];
c = ctx->state[2];
d = ctx->state[3];
e = ctx->state[4];
f = ctx->state[5];
g = ctx->state[6];
h = ctx->state[7];
for (i = 0; i < 64; ++i) {
t1 = h + EP1(e) + CH(e,f,g) + k[i] + m[i];
t2 = EP0(a) + MAJ(a,b,c);
h = g;
g = f;
f = e;
e = d + t1;
d = c;
c = b;
b = a;
a = t1 + t2;
}
ctx->state[0] += a;
ctx->state[1] += b;
ctx->state[2] += c;
ctx->state[3] += d;
ctx->state[4] += e;
ctx->state[5] += f;
ctx->state[6] += g;
ctx->state[7] += h;
}
void sha256_init(SHA256_CTX *ctx)
{
ctx->datalen = 0;
ctx->bitlen = 0;
ctx->state[0] = 0x6a09e667;
ctx->state[1] = 0xbb67ae85;
ctx->state[2] = 0x3c6ef372;
ctx->state[3] = 0xa54ff53a;
ctx->state[4] = 0x510e527f;
ctx->state[5] = 0x9b05688c;
ctx->state[6] = 0x1f83d9ab;
ctx->state[7] = 0x5be0cd19;
}
void sha256_update(SHA256_CTX *ctx, const BYTE data[], size_t len)
{
WORD i;
for (i = 0; i < len; ++i) {
ctx->data[ctx->datalen] = data[i];
ctx->datalen++;
if (ctx->datalen == 64) {
sha256_transform(ctx, ctx->data);
ctx->bitlen += 512;
ctx->datalen = 0;
}
}
}
void sha256_final(SHA256_CTX *ctx, BYTE hash[])
{
WORD i;
i = ctx->datalen;
// Pad whatever data is left in the buffer.
if (ctx->datalen < 56) {
ctx->data[i++] = 0x80;
while (i < 56)
ctx->data[i++] = 0x00;
}
else {
ctx->data[i++] = 0x80;
while (i < 64)
ctx->data[i++] = 0x00;
sha256_transform(ctx, ctx->data);
memset(ctx->data, 0, 56);
}
/* Append to the padding the total message's length in bits and transform. */
ctx->bitlen += ctx->datalen * 8;
ctx->data[63] = ctx->bitlen;
ctx->data[62] = ctx->bitlen >> 8;
ctx->data[61] = ctx->bitlen >> 16;
ctx->data[60] = ctx->bitlen >> 24;
ctx->data[59] = ctx->bitlen >> 32;
ctx->data[58] = ctx->bitlen >> 40;
ctx->data[57] = ctx->bitlen >> 48;
ctx->data[56] = ctx->bitlen >> 56;
sha256_transform(ctx, ctx->data);
/* Since this implementation uses little endian byte ordering and SHA uses big endian,
reverse all the bytes when copying the final state to the output hash. */
for (i = 0; i < 4; ++i) {
hash[i] = (ctx->state[0] >> (24 - i * 8)) & 0x000000ff;
hash[i + 4] = (ctx->state[1] >> (24 - i * 8)) & 0x000000ff;
hash[i + 8] = (ctx->state[2] >> (24 - i * 8)) & 0x000000ff;
hash[i + 12] = (ctx->state[3] >> (24 - i * 8)) & 0x000000ff;
hash[i + 16] = (ctx->state[4] >> (24 - i * 8)) & 0x000000ff;
hash[i + 20] = (ctx->state[5] >> (24 - i * 8)) & 0x000000ff;
hash[i + 24] = (ctx->state[6] >> (24 - i * 8)) & 0x000000ff;
hash[i + 28] = (ctx->state[7] >> (24 - i * 8)) & 0x000000ff;
}
}
void sha256sum(unsigned char *data, unsigned int length, unsigned char *hash)
{
SHA256_CTX ctx;
sha256_init(&ctx);
sha256_update(&ctx, data, length);
sha256_final(&ctx, hash);
}

6
source/snes9x/sha256.h Normal file
View File

@ -0,0 +1,6 @@
#ifndef __SHA256_H
#define __SHA256_H
void sha256sum (unsigned char *data, unsigned int length, unsigned char *hash);
#endif

638
source/snes9x/stream.cpp Normal file
View File

@ -0,0 +1,638 @@
/***********************************************************************************
Snes9x - Portable Super Nintendo Entertainment System (TM) emulator.
(c) Copyright 1996 - 2002 Gary Henderson (gary.henderson@ntlworld.com),
Jerremy Koot (jkoot@snes9x.com)
(c) Copyright 2002 - 2004 Matthew Kendora
(c) Copyright 2002 - 2005 Peter Bortas (peter@bortas.org)
(c) Copyright 2004 - 2005 Joel Yliluoma (http://iki.fi/bisqwit/)
(c) Copyright 2001 - 2006 John Weidman (jweidman@slip.net)
(c) Copyright 2002 - 2006 funkyass (funkyass@spam.shaw.ca),
Kris Bleakley (codeviolation@hotmail.com)
(c) Copyright 2002 - 2010 Brad Jorsch (anomie@users.sourceforge.net),
Nach (n-a-c-h@users.sourceforge.net),
(c) Copyright 2002 - 2011 zones (kasumitokoduck@yahoo.com)
(c) Copyright 2006 - 2007 nitsuja
(c) Copyright 2009 - 2018 BearOso,
OV2
(c) Copyright 2017 qwertymodo
(c) Copyright 2011 - 2017 Hans-Kristian Arntzen,
Daniel De Matteis
(Under no circumstances will commercial rights be given)
BS-X C emulator code
(c) Copyright 2005 - 2006 Dreamer Nom,
zones
C4 x86 assembler and some C emulation code
(c) Copyright 2000 - 2003 _Demo_ (_demo_@zsnes.com),
Nach,
zsKnight (zsknight@zsnes.com)
C4 C++ code
(c) Copyright 2003 - 2006 Brad Jorsch,
Nach
DSP-1 emulator code
(c) Copyright 1998 - 2006 _Demo_,
Andreas Naive (andreasnaive@gmail.com),
Gary Henderson,
Ivar (ivar@snes9x.com),
John Weidman,
Kris Bleakley,
Matthew Kendora,
Nach,
neviksti (neviksti@hotmail.com)
DSP-2 emulator code
(c) Copyright 2003 John Weidman,
Kris Bleakley,
Lord Nightmare (lord_nightmare@users.sourceforge.net),
Matthew Kendora,
neviksti
DSP-3 emulator code
(c) Copyright 2003 - 2006 John Weidman,
Kris Bleakley,
Lancer,
z80 gaiden
DSP-4 emulator code
(c) Copyright 2004 - 2006 Dreamer Nom,
John Weidman,
Kris Bleakley,
Nach,
z80 gaiden
OBC1 emulator code
(c) Copyright 2001 - 2004 zsKnight,
pagefault (pagefault@zsnes.com),
Kris Bleakley
Ported from x86 assembler to C by sanmaiwashi
SPC7110 and RTC C++ emulator code used in 1.39-1.51
(c) Copyright 2002 Matthew Kendora with research by
zsKnight,
John Weidman,
Dark Force
SPC7110 and RTC C++ emulator code used in 1.52+
(c) Copyright 2009 byuu,
neviksti
S-DD1 C emulator code
(c) Copyright 2003 Brad Jorsch with research by
Andreas Naive,
John Weidman
S-RTC C emulator code
(c) Copyright 2001 - 2006 byuu,
John Weidman
ST010 C++ emulator code
(c) Copyright 2003 Feather,
John Weidman,
Kris Bleakley,
Matthew Kendora
Super FX x86 assembler emulator code
(c) Copyright 1998 - 2003 _Demo_,
pagefault,
zsKnight
Super FX C emulator code
(c) Copyright 1997 - 1999 Ivar,
Gary Henderson,
John Weidman
Sound emulator code used in 1.5-1.51
(c) Copyright 1998 - 2003 Brad Martin
(c) Copyright 1998 - 2006 Charles Bilyue'
Sound emulator code used in 1.52+
(c) Copyright 2004 - 2007 Shay Green (gblargg@gmail.com)
S-SMP emulator code used in 1.54+
(c) Copyright 2016 byuu
SH assembler code partly based on x86 assembler code
(c) Copyright 2002 - 2004 Marcus Comstedt (marcus@mc.pp.se)
2xSaI filter
(c) Copyright 1999 - 2001 Derek Liauw Kie Fa
HQ2x, HQ3x, HQ4x filters
(c) Copyright 2003 Maxim Stepin (maxim@hiend3d.com)
NTSC filter
(c) Copyright 2006 - 2007 Shay Green
GTK+ GUI code
(c) Copyright 2004 - 2018 BearOso
Win32 GUI code
(c) Copyright 2003 - 2006 blip,
funkyass,
Matthew Kendora,
Nach,
nitsuja
(c) Copyright 2009 - 2018 OV2
Mac OS GUI code
(c) Copyright 1998 - 2001 John Stiles
(c) Copyright 2001 - 2011 zones
Libretro port
(c) Copyright 2011 - 2017 Hans-Kristian Arntzen,
Daniel De Matteis
(Under no circumstances will commercial rights be given)
Specific ports contains the works of other authors. See headers in
individual files.
Snes9x homepage: http://www.snes9x.com/
Permission to use, copy, modify and/or distribute Snes9x in both binary
and source form, for non-commercial purposes, is hereby granted without
fee, providing that this license information and copyright notice appear
with all copies and any derived work.
This software is provided 'as-is', without any express or implied
warranty. In no event shall the authors be held liable for any damages
arising from the use of this software or it's derivatives.
Snes9x is freeware for PERSONAL USE only. Commercial users should
seek permission of the copyright holders first. Commercial use includes,
but is not limited to, charging money for Snes9x or software derived from
Snes9x, including Snes9x or derivatives in commercial game bundles, and/or
using Snes9x as a promotion for your commercial product.
The copyright holders request that bug fixes and improvements to the code
should be forwarded to them so everyone can benefit from the modifications
in future versions.
Super NES and Super Nintendo Entertainment System are trademarks of
Nintendo Co., Limited and its subsidiary companies.
***********************************************************************************/
// Abstract the details of reading from zip files versus FILE *'s.
#include <string>
#ifdef UNZIP_SUPPORT
# ifdef SYSTEM_ZIP
# include <minizip/unzip.h>
# else
# include "unzip.h"
# endif
#endif
#include "snes9x.h"
#include "stream.h"
// Generic constructor/destructor
Stream::Stream (void)
{
return;
}
Stream::~Stream (void)
{
return;
}
// Generic getline function, based on gets. Reimlpement if you can do better.
char * Stream::getline (void)
{
bool eof;
std::string ret;
ret = getline(eof);
if (ret.size() == 0 && eof)
return (NULL);
return (strdup(ret.c_str()));
}
std::string Stream::getline (bool &eof)
{
char buf[1024];
std::string ret;
eof = false;
ret.clear();
do
{
if (gets(buf, sizeof(buf)) == NULL)
{
eof = true;
break;
}
ret.append(buf);
}
while (*ret.rbegin() != '\n');
return (ret);
}
// snes9x.h FSTREAM Stream
fStream::fStream (FSTREAM f)
{
fp = f;
}
fStream::~fStream (void)
{
return;
}
int fStream::get_char (void)
{
return (GETC_FSTREAM(fp));
}
char * fStream::gets (char *buf, size_t len)
{
return (GETS_FSTREAM(buf, len, fp));
}
size_t fStream::read (void *buf, size_t len)
{
return (READ_FSTREAM(buf, len, fp));
}
size_t fStream::write (void *buf, size_t len)
{
return (WRITE_FSTREAM(buf, len, fp));
}
size_t fStream::pos (void)
{
return (FIND_FSTREAM(fp));
}
size_t fStream::size (void)
{
size_t sz;
REVERT_FSTREAM(fp,0L,SEEK_END);
sz = FIND_FSTREAM(fp);
REVERT_FSTREAM(fp,0L,SEEK_SET);
return sz;
}
int fStream::revert (size_t from, size_t offset)
{
return (REVERT_FSTREAM(fp, offset, from));
}
void fStream::closeStream()
{
CLOSE_FSTREAM(fp);
delete this;
}
// unzip Stream
#ifdef UNZIP_SUPPORT
unzStream::unzStream (unzFile &v)
{
file = v;
pos_in_buf = 0;
buf_pos_in_unzipped = unztell(file);
bytes_in_buf = 0;
// remember start pos for seeks
unzGetFilePos(file, &unz_file_start_pos);
}
unzStream::~unzStream (void)
{
return;
}
size_t unzStream::buffer_remaining()
{
return bytes_in_buf - pos_in_buf;
}
void unzStream::fill_buffer()
{
buf_pos_in_unzipped = unztell(file);
bytes_in_buf = unzReadCurrentFile(file, buffer, unz_BUFFSIZ);
pos_in_buf = 0;
}
int unzStream::get_char (void)
{
unsigned char c;
if (buffer_remaining() <= 0)
{
fill_buffer();
if (bytes_in_buf <= 0)
return (EOF);
}
c = *(buffer + pos_in_buf);
pos_in_buf++;
return ((int) c);
}
char * unzStream::gets (char *buf, size_t len)
{
size_t i;
int c;
for (i = 0; i < len - 1; i++)
{
c = get_char();
if (c == EOF)
{
if (i == 0)
return (NULL);
break;
}
buf[i] = (char) c;
if (buf[i] == '\n')
break;
}
buf[i] = '\0';
return (buf);
}
size_t unzStream::read (void *buf, size_t len)
{
if (len == 0)
return (len);
size_t to_read = len;
uint8 *read_to = (uint8 * )buf;
do
{
size_t in_buffer = buffer_remaining();
if (to_read <= in_buffer)
{
memcpy(read_to, buffer + pos_in_buf, to_read);
pos_in_buf += to_read;
to_read = 0;
break;
}
memcpy(read_to, buffer + pos_in_buf, in_buffer);
to_read -= in_buffer;
fill_buffer();
} while (bytes_in_buf);
return (len - to_read);
}
// not supported
size_t unzStream::write (void *buf, size_t len)
{
return (0);
}
size_t unzStream::pos (void)
{
return buf_pos_in_unzipped + pos_in_buf;
}
size_t unzStream::size (void)
{
unz_file_info info;
unzGetCurrentFileInfo(file,&info,NULL,0,NULL,0,NULL,0);
return info.uncompressed_size;
}
int unzStream::revert (size_t from, size_t offset)
{
size_t target_pos = from + offset;
// new pos inside buffered data
if (target_pos >= buf_pos_in_unzipped && target_pos < buf_pos_in_unzipped + bytes_in_buf)
{
pos_in_buf = target_pos - buf_pos_in_unzipped;
}
else // outside of buffer, reset file and read until pos
{
unzGoToFilePos(file, &unz_file_start_pos);
unzOpenCurrentFile(file); // necessary to reopen after seek
int times_to_read = target_pos / unz_BUFFSIZ + 1;
for( int i = 0; i < times_to_read; i++)
{
fill_buffer();
}
pos_in_buf = target_pos % unz_BUFFSIZ;
}
return 0;
}
void unzStream::closeStream()
{
unzClose(file);
delete this;
}
#endif
// memory Stream
memStream::memStream (uint8 *source, size_t sourceSize)
{
mem = head = source;
msize = remaining = sourceSize;
readonly = false;
}
memStream::memStream (const uint8 *source, size_t sourceSize)
{
mem = head = const_cast<uint8 *>(source);
msize = remaining = sourceSize;
readonly = true;
}
memStream::~memStream (void)
{
return;
}
int memStream::get_char (void)
{
if(!remaining)
return EOF;
remaining--;
return *head++;
}
char * memStream::gets (char *buf, size_t len)
{
size_t i;
int c;
for (i = 0; i < len - 1; i++)
{
c = get_char();
if (c == EOF)
{
if (i == 0)
return (NULL);
break;
}
buf[i] = (char) c;
if (buf[i] == '\n')
break;
}
buf[i] = '\0';
return (buf);
}
size_t memStream::read (void *buf, size_t len)
{
size_t bytes = len < remaining ? len : remaining;
memcpy(buf,head,bytes);
head += bytes;
remaining -= bytes;
return bytes;
}
size_t memStream::write (void *buf, size_t len)
{
if(readonly)
return 0;
size_t bytes = len < remaining ? len : remaining;
memcpy(head,buf,bytes);
head += bytes;
remaining -= bytes;
return bytes;
}
size_t memStream::pos (void)
{
return msize - remaining;
}
size_t memStream::size (void)
{
return msize;
}
int memStream::revert (size_t from, size_t offset)
{
size_t pos = from + offset;
if(pos > msize)
return -1;
head = mem + pos;
remaining = msize - pos;
return 0;
}
void memStream::closeStream()
{
delete [] mem;
delete this;
}
// dummy Stream
nulStream::nulStream (void)
{
bytes_written = 0;
}
nulStream::~nulStream (void)
{
return;
}
int nulStream::get_char (void)
{
return 0;
}
char * nulStream::gets (char *buf, size_t len)
{
*buf = '\0';
return NULL;
}
size_t nulStream::read (void *buf, size_t len)
{
return 0;
}
size_t nulStream::write (void *buf, size_t len)
{
bytes_written += len;
return len;
}
size_t nulStream::pos (void)
{
return 0;
}
size_t nulStream::size (void)
{
return bytes_written;
}
int nulStream::revert (size_t from, size_t offset)
{
bytes_written = from + offset;
return 0;
}
void nulStream::closeStream()
{
delete this;
}
Stream *openStreamFromFSTREAM(const char* filename, const char* mode)
{
FSTREAM f = OPEN_FSTREAM(filename,mode);
if(!f)
return NULL;
return new fStream(f);
}
Stream *reopenStreamFromFd(int fd, const char* mode)
{
FSTREAM f = REOPEN_FSTREAM(fd,mode);
if(!f)
return NULL;
return new fStream(f);
}

318
source/snes9x/stream.h Normal file
View File

@ -0,0 +1,318 @@
/***********************************************************************************
Snes9x - Portable Super Nintendo Entertainment System (TM) emulator.
(c) Copyright 1996 - 2002 Gary Henderson (gary.henderson@ntlworld.com),
Jerremy Koot (jkoot@snes9x.com)
(c) Copyright 2002 - 2004 Matthew Kendora
(c) Copyright 2002 - 2005 Peter Bortas (peter@bortas.org)
(c) Copyright 2004 - 2005 Joel Yliluoma (http://iki.fi/bisqwit/)
(c) Copyright 2001 - 2006 John Weidman (jweidman@slip.net)
(c) Copyright 2002 - 2006 funkyass (funkyass@spam.shaw.ca),
Kris Bleakley (codeviolation@hotmail.com)
(c) Copyright 2002 - 2010 Brad Jorsch (anomie@users.sourceforge.net),
Nach (n-a-c-h@users.sourceforge.net),
(c) Copyright 2002 - 2011 zones (kasumitokoduck@yahoo.com)
(c) Copyright 2006 - 2007 nitsuja
(c) Copyright 2009 - 2018 BearOso,
OV2
(c) Copyright 2017 qwertymodo
(c) Copyright 2011 - 2017 Hans-Kristian Arntzen,
Daniel De Matteis
(Under no circumstances will commercial rights be given)
BS-X C emulator code
(c) Copyright 2005 - 2006 Dreamer Nom,
zones
C4 x86 assembler and some C emulation code
(c) Copyright 2000 - 2003 _Demo_ (_demo_@zsnes.com),
Nach,
zsKnight (zsknight@zsnes.com)
C4 C++ code
(c) Copyright 2003 - 2006 Brad Jorsch,
Nach
DSP-1 emulator code
(c) Copyright 1998 - 2006 _Demo_,
Andreas Naive (andreasnaive@gmail.com),
Gary Henderson,
Ivar (ivar@snes9x.com),
John Weidman,
Kris Bleakley,
Matthew Kendora,
Nach,
neviksti (neviksti@hotmail.com)
DSP-2 emulator code
(c) Copyright 2003 John Weidman,
Kris Bleakley,
Lord Nightmare (lord_nightmare@users.sourceforge.net),
Matthew Kendora,
neviksti
DSP-3 emulator code
(c) Copyright 2003 - 2006 John Weidman,
Kris Bleakley,
Lancer,
z80 gaiden
DSP-4 emulator code
(c) Copyright 2004 - 2006 Dreamer Nom,
John Weidman,
Kris Bleakley,
Nach,
z80 gaiden
OBC1 emulator code
(c) Copyright 2001 - 2004 zsKnight,
pagefault (pagefault@zsnes.com),
Kris Bleakley
Ported from x86 assembler to C by sanmaiwashi
SPC7110 and RTC C++ emulator code used in 1.39-1.51
(c) Copyright 2002 Matthew Kendora with research by
zsKnight,
John Weidman,
Dark Force
SPC7110 and RTC C++ emulator code used in 1.52+
(c) Copyright 2009 byuu,
neviksti
S-DD1 C emulator code
(c) Copyright 2003 Brad Jorsch with research by
Andreas Naive,
John Weidman
S-RTC C emulator code
(c) Copyright 2001 - 2006 byuu,
John Weidman
ST010 C++ emulator code
(c) Copyright 2003 Feather,
John Weidman,
Kris Bleakley,
Matthew Kendora
Super FX x86 assembler emulator code
(c) Copyright 1998 - 2003 _Demo_,
pagefault,
zsKnight
Super FX C emulator code
(c) Copyright 1997 - 1999 Ivar,
Gary Henderson,
John Weidman
Sound emulator code used in 1.5-1.51
(c) Copyright 1998 - 2003 Brad Martin
(c) Copyright 1998 - 2006 Charles Bilyue'
Sound emulator code used in 1.52+
(c) Copyright 2004 - 2007 Shay Green (gblargg@gmail.com)
S-SMP emulator code used in 1.54+
(c) Copyright 2016 byuu
SH assembler code partly based on x86 assembler code
(c) Copyright 2002 - 2004 Marcus Comstedt (marcus@mc.pp.se)
2xSaI filter
(c) Copyright 1999 - 2001 Derek Liauw Kie Fa
HQ2x, HQ3x, HQ4x filters
(c) Copyright 2003 Maxim Stepin (maxim@hiend3d.com)
NTSC filter
(c) Copyright 2006 - 2007 Shay Green
GTK+ GUI code
(c) Copyright 2004 - 2018 BearOso
Win32 GUI code
(c) Copyright 2003 - 2006 blip,
funkyass,
Matthew Kendora,
Nach,
nitsuja
(c) Copyright 2009 - 2018 OV2
Mac OS GUI code
(c) Copyright 1998 - 2001 John Stiles
(c) Copyright 2001 - 2011 zones
Libretro port
(c) Copyright 2011 - 2017 Hans-Kristian Arntzen,
Daniel De Matteis
(Under no circumstances will commercial rights be given)
Specific ports contains the works of other authors. See headers in
individual files.
Snes9x homepage: http://www.snes9x.com/
Permission to use, copy, modify and/or distribute Snes9x in both binary
and source form, for non-commercial purposes, is hereby granted without
fee, providing that this license information and copyright notice appear
with all copies and any derived work.
This software is provided 'as-is', without any express or implied
warranty. In no event shall the authors be held liable for any damages
arising from the use of this software or it's derivatives.
Snes9x is freeware for PERSONAL USE only. Commercial users should
seek permission of the copyright holders first. Commercial use includes,
but is not limited to, charging money for Snes9x or software derived from
Snes9x, including Snes9x or derivatives in commercial game bundles, and/or
using Snes9x as a promotion for your commercial product.
The copyright holders request that bug fixes and improvements to the code
should be forwarded to them so everyone can benefit from the modifications
in future versions.
Super NES and Super Nintendo Entertainment System are trademarks of
Nintendo Co., Limited and its subsidiary companies.
***********************************************************************************/
#ifndef _STREAM_H_
#define _STREAM_H_
#include <string>
class Stream
{
public:
Stream (void);
virtual ~Stream (void);
virtual int get_char (void) = 0;
virtual char * gets (char *, size_t) = 0;
virtual char * getline (void); // free() when done
virtual std::string getline (bool &);
virtual size_t read (void *, size_t) = 0;
virtual size_t write (void *, size_t) = 0;
virtual size_t pos (void) = 0;
virtual size_t size (void) = 0;
virtual int revert (size_t from, size_t offset) = 0;
virtual void closeStream() = 0;
};
class fStream : public Stream
{
public:
fStream (FSTREAM);
virtual ~fStream (void);
virtual int get_char (void);
virtual char * gets (char *, size_t);
virtual size_t read (void *, size_t);
virtual size_t write (void *, size_t);
virtual size_t pos (void);
virtual size_t size (void);
virtual int revert (size_t from, size_t offset);
virtual void closeStream();
private:
FSTREAM fp;
};
#ifdef UNZIP_SUPPORT
# ifdef SYSTEM_ZIP
# include <minizip/unzip.h>
# else
# include "unzip.h"
# endif
#define unz_BUFFSIZ 1024
class unzStream : public Stream
{
public:
unzStream (unzFile &);
virtual ~unzStream (void);
virtual int get_char (void);
virtual char * gets (char *, size_t);
virtual size_t read (void *, size_t);
virtual size_t write (void *, size_t);
virtual size_t pos (void);
virtual size_t size (void);
virtual int revert (size_t from, size_t offset);
virtual void closeStream();
private:
void fill_buffer();
size_t buffer_remaining();
unzFile file;
char buffer[unz_BUFFSIZ];
size_t pos_in_buf;
size_t buf_pos_in_unzipped;
size_t bytes_in_buf;
unz_file_pos unz_file_start_pos;
};
#endif
class memStream : public Stream
{
public:
memStream (uint8 *,size_t);
memStream (const uint8 *,size_t);
virtual ~memStream (void);
virtual int get_char (void);
virtual char * gets (char *, size_t);
virtual size_t read (void *, size_t);
virtual size_t write (void *, size_t);
virtual size_t pos (void);
virtual size_t size (void);
virtual int revert (size_t from, size_t offset);
virtual void closeStream();
private:
uint8 *mem;
size_t msize;
size_t remaining;
uint8 *head;
bool readonly;
};
/* dummy stream that always reads 0 and writes nowhere
but counts bytes written
*/
class nulStream : public Stream
{
public:
nulStream (void);
virtual ~nulStream (void);
virtual int get_char (void);
virtual char * gets (char *, size_t);
virtual size_t read (void *, size_t);
virtual size_t write (void *, size_t);
virtual size_t pos (void);
virtual size_t size (void);
virtual int revert (size_t from, size_t offset);
virtual void closeStream();
private:
size_t bytes_written;
};
Stream *openStreamFromFSTREAM(const char* filename, const char* mode);
Stream *reopenStreamFromFd(int fd, const char* mode);
#endif