need to move these files over to another computer. dont think they should get in anyone else's way
This commit is contained in:
parent
279dc1e6b8
commit
024462c221
|
@ -250,6 +250,7 @@ void NDS_setTouchPos(u16 x, u16 y)
|
||||||
{
|
{
|
||||||
nds.touchX = (x <<4);
|
nds.touchX = (x <<4);
|
||||||
nds.touchY = (y <<4);
|
nds.touchY = (y <<4);
|
||||||
|
nds.isTouch = 1;
|
||||||
|
|
||||||
MMU.ARM7_REG[0x136] &= 0xBF;
|
MMU.ARM7_REG[0x136] &= 0xBF;
|
||||||
}
|
}
|
||||||
|
@ -258,6 +259,7 @@ void NDS_releaseTouch(void)
|
||||||
{
|
{
|
||||||
nds.touchX = 0;
|
nds.touchX = 0;
|
||||||
nds.touchY = 0;
|
nds.touchY = 0;
|
||||||
|
nds.isTouch = 0;
|
||||||
|
|
||||||
MMU.ARM7_REG[0x136] |= 0x40;
|
MMU.ARM7_REG[0x136] |= 0x40;
|
||||||
}
|
}
|
||||||
|
@ -477,6 +479,7 @@ void NDS_Reset( void)
|
||||||
nds.diff = 0;
|
nds.diff = 0;
|
||||||
nds.lignerendu = FALSE;
|
nds.lignerendu = FALSE;
|
||||||
nds.touchX = nds.touchY = 0;
|
nds.touchX = nds.touchY = 0;
|
||||||
|
nds.isTouch = 0;
|
||||||
|
|
||||||
MMU_write16(0, 0x04000130, 0x3FF);
|
MMU_write16(0, 0x04000130, 0x3FF);
|
||||||
MMU_write16(1, 0x04000130, 0x3FF);
|
MMU_write16(1, 0x04000130, 0x3FF);
|
||||||
|
@ -1571,3 +1574,106 @@ NDS_exec(s32 nb, BOOL force)
|
||||||
|
|
||||||
return nds.cycles;
|
return nds.cycles;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void NDS_setPadFromMovie(u16 pad)
|
||||||
|
{
|
||||||
|
#define FIX(b,n) (((pad>>n)&1)!=0)
|
||||||
|
NDS_setPad(
|
||||||
|
FIX(pad,0),
|
||||||
|
FIX(pad,1),
|
||||||
|
FIX(pad,2),
|
||||||
|
FIX(pad,3),
|
||||||
|
FIX(pad,4),
|
||||||
|
FIX(pad,5),
|
||||||
|
FIX(pad,6),
|
||||||
|
FIX(pad,7),
|
||||||
|
FIX(pad,8),
|
||||||
|
FIX(pad,9),
|
||||||
|
FIX(pad,10),
|
||||||
|
FIX(pad,11),
|
||||||
|
FIX(pad,12)
|
||||||
|
);
|
||||||
|
#undef FIX
|
||||||
|
}
|
||||||
|
|
||||||
|
void NDS_setPad(bool R,bool L,bool D,bool U,bool T,bool S,bool B,bool A,bool Y,bool X,bool W,bool E,bool G)
|
||||||
|
{
|
||||||
|
|
||||||
|
//this macro is the opposite of what you would expect
|
||||||
|
#define FIX(b) (b?0:0x80)
|
||||||
|
|
||||||
|
int r = FIX(R);
|
||||||
|
int l = FIX(L);
|
||||||
|
int d = FIX(D);
|
||||||
|
int u = FIX(U);
|
||||||
|
int t = FIX(T);
|
||||||
|
int s = FIX(S);
|
||||||
|
int b = FIX(B);
|
||||||
|
int a = FIX(A);
|
||||||
|
int y = FIX(Y);
|
||||||
|
int x = FIX(X);
|
||||||
|
int w = FIX(W);
|
||||||
|
int e = FIX(E);
|
||||||
|
int g = FIX(G);
|
||||||
|
|
||||||
|
u16 pad = (0 |
|
||||||
|
((a) >> 7) |
|
||||||
|
((b) >> 6) |
|
||||||
|
((s) >> 5) |
|
||||||
|
((t) >> 4) |
|
||||||
|
((r) >> 3) |
|
||||||
|
((l) >> 2) |
|
||||||
|
((u) >> 1) |
|
||||||
|
((d)) |
|
||||||
|
((r) << 1) |
|
||||||
|
((l) << 2)) ;
|
||||||
|
|
||||||
|
((u16 *)ARM9Mem.ARM9_REG)[0x130>>1] = (u16)pad;
|
||||||
|
((u16 *)MMU.ARM7_REG)[0x130>>1] = (u16)pad;
|
||||||
|
|
||||||
|
u16 padExt = (((u16 *)MMU.ARM7_REG)[0x136>>1] & 0x00F0) |
|
||||||
|
((x) >> 7) |
|
||||||
|
((y) >> 6) |
|
||||||
|
((g) >> 4) |
|
||||||
|
0x0034;
|
||||||
|
|
||||||
|
((u16 *)MMU.ARM7_REG)[0x136>>1] = (u16)padExt;
|
||||||
|
|
||||||
|
|
||||||
|
//put into the format we want for the movie system
|
||||||
|
//RLDUTSBAYXWEG
|
||||||
|
#undef FIX
|
||||||
|
#define FIX(b) (b?1:0)
|
||||||
|
|
||||||
|
r = FIX(R);
|
||||||
|
l = FIX(L);
|
||||||
|
d = FIX(D);
|
||||||
|
u = FIX(U);
|
||||||
|
t = FIX(T);
|
||||||
|
s = FIX(S);
|
||||||
|
b = FIX(B);
|
||||||
|
a = FIX(A);
|
||||||
|
y = FIX(Y);
|
||||||
|
x = FIX(X);
|
||||||
|
w = FIX(W);
|
||||||
|
e = FIX(E);
|
||||||
|
g = FIX(G);
|
||||||
|
|
||||||
|
|
||||||
|
nds.pad =
|
||||||
|
(FIX(r)<<0)|
|
||||||
|
(FIX(l)<<1)|
|
||||||
|
(FIX(d)<<2)|
|
||||||
|
(FIX(u)<<3)|
|
||||||
|
(FIX(t)<<4)|
|
||||||
|
(FIX(s)<<5)|
|
||||||
|
(FIX(b)<<6)|
|
||||||
|
(FIX(a)<<7)|
|
||||||
|
(FIX(y)<<8)|
|
||||||
|
(FIX(x)<<9)|
|
||||||
|
(FIX(w)<<10)|
|
||||||
|
(FIX(e)<<11)|
|
||||||
|
(FIX(g)<<12);
|
||||||
|
|
||||||
|
// TODO: low power IRQ
|
||||||
|
}
|
||||||
|
|
|
@ -118,6 +118,8 @@ typedef struct
|
||||||
|
|
||||||
u16 touchX;
|
u16 touchX;
|
||||||
u16 touchY;
|
u16 touchY;
|
||||||
|
BOOL isTouch;
|
||||||
|
u16 pad;
|
||||||
|
|
||||||
//this is not essential NDS runtime state.
|
//this is not essential NDS runtime state.
|
||||||
//it was perhaps a mistake to put it here.
|
//it was perhaps a mistake to put it here.
|
||||||
|
@ -187,6 +189,8 @@ NDS_header * NDS_getROMHeader(void);
|
||||||
|
|
||||||
void NDS_setTouchPos(u16 x, u16 y);
|
void NDS_setTouchPos(u16 x, u16 y);
|
||||||
void NDS_releaseTouch(void);
|
void NDS_releaseTouch(void);
|
||||||
|
void NDS_setPad(bool R,bool L,bool D,bool U,bool T,bool S,bool B,bool A,bool Y,bool X,bool W,bool E,bool G);
|
||||||
|
void NDS_setPadFromMovie(u16 pad);
|
||||||
|
|
||||||
int NDS_LoadROM(const char *filename, int bmtype, u32 bmsize,
|
int NDS_LoadROM(const char *filename, int bmtype, u32 bmsize,
|
||||||
const char *cflash_disk_image_file);
|
const char *cflash_disk_image_file);
|
||||||
|
|
|
@ -215,7 +215,7 @@ protected:
|
||||||
|
|
||||||
int overflow(int c)
|
int overflow(int c)
|
||||||
{
|
{
|
||||||
expand(-1);
|
expand((size_t)-1);
|
||||||
dosync(c);
|
dosync(c);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
|
@ -159,6 +159,7 @@ SFORMAT SF_NDS[]={
|
||||||
{ "_LIG", 4, 1, &nds.lignerendu},
|
{ "_LIG", 4, 1, &nds.lignerendu},
|
||||||
{ "_TPX", 2, 1, &nds.touchX},
|
{ "_TPX", 2, 1, &nds.touchX},
|
||||||
{ "_TPY", 2, 1, &nds.touchY},
|
{ "_TPY", 2, 1, &nds.touchY},
|
||||||
|
{ "_TPB", 4, 1, &nds.isTouch},
|
||||||
{ 0 }
|
{ 0 }
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -647,7 +648,7 @@ static bool savestate_save(std::ostream* outstream, int compressionLevel)
|
||||||
//save the length of the file
|
//save the length of the file
|
||||||
u32 len = ms.size();
|
u32 len = ms.size();
|
||||||
|
|
||||||
u32 comprlen = -1;
|
u32 comprlen = 0xFFFFFFFF;
|
||||||
u8* cbuf = (u8*)ms.buf();
|
u8* cbuf = (u8*)ms.buf();
|
||||||
|
|
||||||
#ifdef HAVE_LIBZ
|
#ifdef HAVE_LIBZ
|
||||||
|
|
|
@ -26,7 +26,7 @@
|
||||||
#define DESMUME_NAME_AND_VERSION DESMUME_NAME " " DESMUME_VERSION_STRING " " VERSION
|
#define DESMUME_NAME_AND_VERSION DESMUME_NAME " " DESMUME_VERSION_STRING " " VERSION
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
#define strcasecmp(x,y) stricmp(x,y)
|
#define strcasecmp(x,y) _stricmp(x,y)
|
||||||
#else
|
#else
|
||||||
#define WINAPI
|
#define WINAPI
|
||||||
#endif
|
#endif
|
||||||
|
@ -239,5 +239,39 @@ inline double u64_to_double(u64 u) {
|
||||||
return fuxor.b;
|
return fuxor.b;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
///stores a 32bit value into the provided byte array in guaranteed little endian form
|
||||||
|
inline void en32lsb(u8 *buf, u32 morp)
|
||||||
|
{
|
||||||
|
buf[0]=morp;
|
||||||
|
buf[1]=morp>>8;
|
||||||
|
buf[2]=morp>>16;
|
||||||
|
buf[3]=morp>>24;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void en16lsb(u8* buf, u16 morp)
|
||||||
|
{
|
||||||
|
buf[0]=morp;
|
||||||
|
buf[1]=morp>>8;
|
||||||
|
}
|
||||||
|
|
||||||
|
///unpacks a 64bit little endian value from the provided byte array into host byte order
|
||||||
|
inline u64 de64lsb(u8 *morp)
|
||||||
|
{
|
||||||
|
return morp[0]|(morp[1]<<8)|(morp[2]<<16)|(morp[3]<<24)|((u64)morp[4]<<32)|((u64)morp[5]<<40)|((u64)morp[6]<<48)|((u64)morp[7]<<56);
|
||||||
|
}
|
||||||
|
|
||||||
|
///unpacks a 32bit little endian value from the provided byte array into host byte order
|
||||||
|
inline u32 de32lsb(u8 *morp)
|
||||||
|
{
|
||||||
|
return morp[0]|(morp[1]<<8)|(morp[2]<<16)|(morp[3]<<24);
|
||||||
|
}
|
||||||
|
|
||||||
|
///unpacks a 16bit little endian value from the provided byte array into host byte order
|
||||||
|
inline u16 de16lsb(u8 *morp)
|
||||||
|
{
|
||||||
|
return morp[0]|(morp[1]<<8);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -0,0 +1,539 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2001-2004 Unicode, Inc.
|
||||||
|
*
|
||||||
|
* Disclaimer
|
||||||
|
*
|
||||||
|
* This source code is provided as is by Unicode, Inc. No claims are
|
||||||
|
* made as to fitness for any particular purpose. No warranties of any
|
||||||
|
* kind are expressed or implied. The recipient agrees to determine
|
||||||
|
* applicability of information provided. If this file has been
|
||||||
|
* purchased on magnetic or optical media from Unicode, Inc., the
|
||||||
|
* sole remedy for any claim will be exchange of defective media
|
||||||
|
* within 90 days of receipt.
|
||||||
|
*
|
||||||
|
* Limitations on Rights to Redistribute This Code
|
||||||
|
*
|
||||||
|
* Unicode, Inc. hereby grants the right to freely use the information
|
||||||
|
* supplied in this file in the creation of products supporting the
|
||||||
|
* Unicode Standard, and to make copies of this file in any form
|
||||||
|
* for internal or external distribution as long as this notice
|
||||||
|
* remains attached.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/* ---------------------------------------------------------------------
|
||||||
|
|
||||||
|
Conversions between UTF32, UTF-16, and UTF-8. Source code file.
|
||||||
|
Author: Mark E. Davis, 1994.
|
||||||
|
Rev History: Rick McGowan, fixes & updates May 2001.
|
||||||
|
Sept 2001: fixed const & error conditions per
|
||||||
|
mods suggested by S. Parent & A. Lillich.
|
||||||
|
June 2002: Tim Dodd added detection and handling of incomplete
|
||||||
|
source sequences, enhanced error detection, added casts
|
||||||
|
to eliminate compiler warnings.
|
||||||
|
July 2003: slight mods to back out aggressive FFFE detection.
|
||||||
|
Jan 2004: updated switches in from-UTF8 conversions.
|
||||||
|
Oct 2004: updated to use UNI_MAX_LEGAL_UTF32 in UTF-32 conversions.
|
||||||
|
|
||||||
|
See the header file "ConvertUTF.h" for complete documentation.
|
||||||
|
|
||||||
|
------------------------------------------------------------------------ */
|
||||||
|
|
||||||
|
|
||||||
|
#include "ConvertUTF.h"
|
||||||
|
#ifdef CVTUTF_DEBUG
|
||||||
|
#include <stdio.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
static const int halfShift = 10; /* used for shifting by 10 bits */
|
||||||
|
|
||||||
|
static const UTF32 halfBase = 0x0010000UL;
|
||||||
|
static const UTF32 halfMask = 0x3FFUL;
|
||||||
|
|
||||||
|
#define UNI_SUR_HIGH_START (UTF32)0xD800
|
||||||
|
#define UNI_SUR_HIGH_END (UTF32)0xDBFF
|
||||||
|
#define UNI_SUR_LOW_START (UTF32)0xDC00
|
||||||
|
#define UNI_SUR_LOW_END (UTF32)0xDFFF
|
||||||
|
#define false 0
|
||||||
|
#define true 1
|
||||||
|
|
||||||
|
/* --------------------------------------------------------------------- */
|
||||||
|
|
||||||
|
ConversionResult ConvertUTF32toUTF16 (
|
||||||
|
const UTF32** sourceStart, const UTF32* sourceEnd,
|
||||||
|
UTF16** targetStart, UTF16* targetEnd, ConversionFlags flags) {
|
||||||
|
ConversionResult result = conversionOK;
|
||||||
|
const UTF32* source = *sourceStart;
|
||||||
|
UTF16* target = *targetStart;
|
||||||
|
while (source < sourceEnd) {
|
||||||
|
UTF32 ch;
|
||||||
|
if (target >= targetEnd) {
|
||||||
|
result = targetExhausted; break;
|
||||||
|
}
|
||||||
|
ch = *source++;
|
||||||
|
if (ch <= UNI_MAX_BMP) { /* Target is a character <= 0xFFFF */
|
||||||
|
/* UTF-16 surrogate values are illegal in UTF-32; 0xffff or 0xfffe are both reserved values */
|
||||||
|
if (ch >= UNI_SUR_HIGH_START && ch <= UNI_SUR_LOW_END) {
|
||||||
|
if (flags == strictConversion) {
|
||||||
|
--source; /* return to the illegal value itself */
|
||||||
|
result = sourceIllegal;
|
||||||
|
break;
|
||||||
|
} else {
|
||||||
|
*target++ = UNI_REPLACEMENT_CHAR;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
*target++ = (UTF16)ch; /* normal case */
|
||||||
|
}
|
||||||
|
} else if (ch > UNI_MAX_LEGAL_UTF32) {
|
||||||
|
if (flags == strictConversion) {
|
||||||
|
result = sourceIllegal;
|
||||||
|
} else {
|
||||||
|
*target++ = UNI_REPLACEMENT_CHAR;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
/* target is a character in range 0xFFFF - 0x10FFFF. */
|
||||||
|
if (target + 1 >= targetEnd) {
|
||||||
|
--source; /* Back up source pointer! */
|
||||||
|
result = targetExhausted; break;
|
||||||
|
}
|
||||||
|
ch -= halfBase;
|
||||||
|
*target++ = (UTF16)((ch >> halfShift) + UNI_SUR_HIGH_START);
|
||||||
|
*target++ = (UTF16)((ch & halfMask) + UNI_SUR_LOW_START);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
*sourceStart = source;
|
||||||
|
*targetStart = target;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* --------------------------------------------------------------------- */
|
||||||
|
|
||||||
|
ConversionResult ConvertUTF16toUTF32 (
|
||||||
|
const UTF16** sourceStart, const UTF16* sourceEnd,
|
||||||
|
UTF32** targetStart, UTF32* targetEnd, ConversionFlags flags) {
|
||||||
|
ConversionResult result = conversionOK;
|
||||||
|
const UTF16* source = *sourceStart;
|
||||||
|
UTF32* target = *targetStart;
|
||||||
|
UTF32 ch, ch2;
|
||||||
|
while (source < sourceEnd) {
|
||||||
|
const UTF16* oldSource = source; /* In case we have to back up because of target overflow. */
|
||||||
|
ch = *source++;
|
||||||
|
/* If we have a surrogate pair, convert to UTF32 first. */
|
||||||
|
if (ch >= UNI_SUR_HIGH_START && ch <= UNI_SUR_HIGH_END) {
|
||||||
|
/* If the 16 bits following the high surrogate are in the source buffer... */
|
||||||
|
if (source < sourceEnd) {
|
||||||
|
ch2 = *source;
|
||||||
|
/* If it's a low surrogate, convert to UTF32. */
|
||||||
|
if (ch2 >= UNI_SUR_LOW_START && ch2 <= UNI_SUR_LOW_END) {
|
||||||
|
ch = ((ch - UNI_SUR_HIGH_START) << halfShift)
|
||||||
|
+ (ch2 - UNI_SUR_LOW_START) + halfBase;
|
||||||
|
++source;
|
||||||
|
} else if (flags == strictConversion) { /* it's an unpaired high surrogate */
|
||||||
|
--source; /* return to the illegal value itself */
|
||||||
|
result = sourceIllegal;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
} else { /* We don't have the 16 bits following the high surrogate. */
|
||||||
|
--source; /* return to the high surrogate */
|
||||||
|
result = sourceExhausted;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
} else if (flags == strictConversion) {
|
||||||
|
/* UTF-16 surrogate values are illegal in UTF-32 */
|
||||||
|
if (ch >= UNI_SUR_LOW_START && ch <= UNI_SUR_LOW_END) {
|
||||||
|
--source; /* return to the illegal value itself */
|
||||||
|
result = sourceIllegal;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (target >= targetEnd) {
|
||||||
|
source = oldSource; /* Back up source pointer! */
|
||||||
|
result = targetExhausted; break;
|
||||||
|
}
|
||||||
|
*target++ = ch;
|
||||||
|
}
|
||||||
|
*sourceStart = source;
|
||||||
|
*targetStart = target;
|
||||||
|
#ifdef CVTUTF_DEBUG
|
||||||
|
if (result == sourceIllegal) {
|
||||||
|
fprintf(stderr, "ConvertUTF16toUTF32 illegal seq 0x%04x,%04x\n", ch, ch2);
|
||||||
|
fflush(stderr);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* --------------------------------------------------------------------- */
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Index into the table below with the first byte of a UTF-8 sequence to
|
||||||
|
* get the number of trailing bytes that are supposed to follow it.
|
||||||
|
* Note that *legal* UTF-8 values can't have 4 or 5-bytes. The table is
|
||||||
|
* left as-is for anyone who may want to do such conversion, which was
|
||||||
|
* allowed in earlier algorithms.
|
||||||
|
*/
|
||||||
|
static const char trailingBytesForUTF8[256] = {
|
||||||
|
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
|
||||||
|
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
|
||||||
|
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
|
||||||
|
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
|
||||||
|
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
|
||||||
|
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
|
||||||
|
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
|
||||||
|
2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, 3,3,3,3,3,3,3,3,4,4,4,4,5,5,5,5
|
||||||
|
};
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Magic values subtracted from a buffer value during UTF8 conversion.
|
||||||
|
* This table contains as many values as there might be trailing bytes
|
||||||
|
* in a UTF-8 sequence.
|
||||||
|
*/
|
||||||
|
static const UTF32 offsetsFromUTF8[6] = { 0x00000000UL, 0x00003080UL, 0x000E2080UL,
|
||||||
|
0x03C82080UL, 0xFA082080UL, 0x82082080UL };
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Once the bits are split out into bytes of UTF-8, this is a mask OR-ed
|
||||||
|
* into the first byte, depending on how many bytes follow. There are
|
||||||
|
* as many entries in this table as there are UTF-8 sequence types.
|
||||||
|
* (I.e., one byte sequence, two byte... etc.). Remember that sequencs
|
||||||
|
* for *legal* UTF-8 will be 4 or fewer bytes total.
|
||||||
|
*/
|
||||||
|
static const UTF8 firstByteMark[7] = { 0x00, 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC };
|
||||||
|
|
||||||
|
/* --------------------------------------------------------------------- */
|
||||||
|
|
||||||
|
/* The interface converts a whole buffer to avoid function-call overhead.
|
||||||
|
* Constants have been gathered. Loops & conditionals have been removed as
|
||||||
|
* much as possible for efficiency, in favor of drop-through switches.
|
||||||
|
* (See "Note A" at the bottom of the file for equivalent code.)
|
||||||
|
* If your compiler supports it, the "isLegalUTF8" call can be turned
|
||||||
|
* into an inline function.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/* --------------------------------------------------------------------- */
|
||||||
|
|
||||||
|
ConversionResult ConvertUTF16toUTF8 (
|
||||||
|
const UTF16** sourceStart, const UTF16* sourceEnd,
|
||||||
|
UTF8** targetStart, UTF8* targetEnd, ConversionFlags flags) {
|
||||||
|
ConversionResult result = conversionOK;
|
||||||
|
const UTF16* source = *sourceStart;
|
||||||
|
UTF8* target = *targetStart;
|
||||||
|
while (source < sourceEnd) {
|
||||||
|
UTF32 ch;
|
||||||
|
unsigned short bytesToWrite = 0;
|
||||||
|
const UTF32 byteMask = 0xBF;
|
||||||
|
const UTF32 byteMark = 0x80;
|
||||||
|
const UTF16* oldSource = source; /* In case we have to back up because of target overflow. */
|
||||||
|
ch = *source++;
|
||||||
|
/* If we have a surrogate pair, convert to UTF32 first. */
|
||||||
|
if (ch >= UNI_SUR_HIGH_START && ch <= UNI_SUR_HIGH_END) {
|
||||||
|
/* If the 16 bits following the high surrogate are in the source buffer... */
|
||||||
|
if (source < sourceEnd) {
|
||||||
|
UTF32 ch2 = *source;
|
||||||
|
/* If it's a low surrogate, convert to UTF32. */
|
||||||
|
if (ch2 >= UNI_SUR_LOW_START && ch2 <= UNI_SUR_LOW_END) {
|
||||||
|
ch = ((ch - UNI_SUR_HIGH_START) << halfShift)
|
||||||
|
+ (ch2 - UNI_SUR_LOW_START) + halfBase;
|
||||||
|
++source;
|
||||||
|
} else if (flags == strictConversion) { /* it's an unpaired high surrogate */
|
||||||
|
--source; /* return to the illegal value itself */
|
||||||
|
result = sourceIllegal;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
} else { /* We don't have the 16 bits following the high surrogate. */
|
||||||
|
--source; /* return to the high surrogate */
|
||||||
|
result = sourceExhausted;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
} else if (flags == strictConversion) {
|
||||||
|
/* UTF-16 surrogate values are illegal in UTF-32 */
|
||||||
|
if (ch >= UNI_SUR_LOW_START && ch <= UNI_SUR_LOW_END) {
|
||||||
|
--source; /* return to the illegal value itself */
|
||||||
|
result = sourceIllegal;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/* Figure out how many bytes the result will require */
|
||||||
|
if (ch < (UTF32)0x80) { bytesToWrite = 1;
|
||||||
|
} else if (ch < (UTF32)0x800) { bytesToWrite = 2;
|
||||||
|
} else if (ch < (UTF32)0x10000) { bytesToWrite = 3;
|
||||||
|
} else if (ch < (UTF32)0x110000) { bytesToWrite = 4;
|
||||||
|
} else { bytesToWrite = 3;
|
||||||
|
ch = UNI_REPLACEMENT_CHAR;
|
||||||
|
}
|
||||||
|
|
||||||
|
target += bytesToWrite;
|
||||||
|
if (target > targetEnd) {
|
||||||
|
source = oldSource; /* Back up source pointer! */
|
||||||
|
target -= bytesToWrite; result = targetExhausted; break;
|
||||||
|
}
|
||||||
|
switch (bytesToWrite) { /* note: everything falls through. */
|
||||||
|
case 4: *--target = (UTF8)((ch | byteMark) & byteMask); ch >>= 6;
|
||||||
|
case 3: *--target = (UTF8)((ch | byteMark) & byteMask); ch >>= 6;
|
||||||
|
case 2: *--target = (UTF8)((ch | byteMark) & byteMask); ch >>= 6;
|
||||||
|
case 1: *--target = (UTF8)(ch | firstByteMark[bytesToWrite]);
|
||||||
|
}
|
||||||
|
target += bytesToWrite;
|
||||||
|
}
|
||||||
|
*sourceStart = source;
|
||||||
|
*targetStart = target;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* --------------------------------------------------------------------- */
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Utility routine to tell whether a sequence of bytes is legal UTF-8.
|
||||||
|
* This must be called with the length pre-determined by the first byte.
|
||||||
|
* If not calling this from ConvertUTF8to*, then the length can be set by:
|
||||||
|
* length = trailingBytesForUTF8[*source]+1;
|
||||||
|
* and the sequence is illegal right away if there aren't that many bytes
|
||||||
|
* available.
|
||||||
|
* If presented with a length > 4, this returns false. The Unicode
|
||||||
|
* definition of UTF-8 goes up to 4-byte sequences.
|
||||||
|
*/
|
||||||
|
|
||||||
|
static Boolean isLegalUTF8(const UTF8 *source, int length) {
|
||||||
|
UTF8 a;
|
||||||
|
const UTF8 *srcptr = source+length;
|
||||||
|
switch (length) {
|
||||||
|
default: return false;
|
||||||
|
/* Everything else falls through when "true"... */
|
||||||
|
case 4: if ((a = (*--srcptr)) < 0x80 || a > 0xBF) return false;
|
||||||
|
case 3: if ((a = (*--srcptr)) < 0x80 || a > 0xBF) return false;
|
||||||
|
case 2: if ((a = (*--srcptr)) > 0xBF) return false;
|
||||||
|
|
||||||
|
switch (*source) {
|
||||||
|
/* no fall-through in this inner switch */
|
||||||
|
case 0xE0: if (a < 0xA0) return false; break;
|
||||||
|
case 0xED: if (a > 0x9F) return false; break;
|
||||||
|
case 0xF0: if (a < 0x90) return false; break;
|
||||||
|
case 0xF4: if (a > 0x8F) return false; break;
|
||||||
|
default: if (a < 0x80) return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
case 1: if (*source >= 0x80 && *source < 0xC2) return false;
|
||||||
|
}
|
||||||
|
if (*source > 0xF4) return false;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* --------------------------------------------------------------------- */
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Exported function to return whether a UTF-8 sequence is legal or not.
|
||||||
|
* This is not used here; it's just exported.
|
||||||
|
*/
|
||||||
|
Boolean isLegalUTF8Sequence(const UTF8 *source, const UTF8 *sourceEnd) {
|
||||||
|
int length = trailingBytesForUTF8[*source]+1;
|
||||||
|
if (source+length > sourceEnd) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return isLegalUTF8(source, length);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* --------------------------------------------------------------------- */
|
||||||
|
|
||||||
|
ConversionResult ConvertUTF8toUTF16 (
|
||||||
|
const UTF8** sourceStart, const UTF8* sourceEnd,
|
||||||
|
UTF16** targetStart, UTF16* targetEnd, ConversionFlags flags) {
|
||||||
|
ConversionResult result = conversionOK;
|
||||||
|
const UTF8* source = *sourceStart;
|
||||||
|
UTF16* target = *targetStart;
|
||||||
|
while (source < sourceEnd) {
|
||||||
|
UTF32 ch = 0;
|
||||||
|
unsigned short extraBytesToRead = trailingBytesForUTF8[*source];
|
||||||
|
if (source + extraBytesToRead >= sourceEnd) {
|
||||||
|
result = sourceExhausted; break;
|
||||||
|
}
|
||||||
|
/* Do this check whether lenient or strict */
|
||||||
|
if (! isLegalUTF8(source, extraBytesToRead+1)) {
|
||||||
|
result = sourceIllegal;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
/*
|
||||||
|
* The cases all fall through. See "Note A" below.
|
||||||
|
*/
|
||||||
|
switch (extraBytesToRead) {
|
||||||
|
case 5: ch += *source++; ch <<= 6; /* remember, illegal UTF-8 */
|
||||||
|
case 4: ch += *source++; ch <<= 6; /* remember, illegal UTF-8 */
|
||||||
|
case 3: ch += *source++; ch <<= 6;
|
||||||
|
case 2: ch += *source++; ch <<= 6;
|
||||||
|
case 1: ch += *source++; ch <<= 6;
|
||||||
|
case 0: ch += *source++;
|
||||||
|
}
|
||||||
|
ch -= offsetsFromUTF8[extraBytesToRead];
|
||||||
|
|
||||||
|
if (target >= targetEnd) {
|
||||||
|
source -= (extraBytesToRead+1); /* Back up source pointer! */
|
||||||
|
result = targetExhausted; break;
|
||||||
|
}
|
||||||
|
if (ch <= UNI_MAX_BMP) { /* Target is a character <= 0xFFFF */
|
||||||
|
/* UTF-16 surrogate values are illegal in UTF-32 */
|
||||||
|
if (ch >= UNI_SUR_HIGH_START && ch <= UNI_SUR_LOW_END) {
|
||||||
|
if (flags == strictConversion) {
|
||||||
|
source -= (extraBytesToRead+1); /* return to the illegal value itself */
|
||||||
|
result = sourceIllegal;
|
||||||
|
break;
|
||||||
|
} else {
|
||||||
|
*target++ = UNI_REPLACEMENT_CHAR;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
*target++ = (UTF16)ch; /* normal case */
|
||||||
|
}
|
||||||
|
} else if (ch > UNI_MAX_UTF16) {
|
||||||
|
if (flags == strictConversion) {
|
||||||
|
result = sourceIllegal;
|
||||||
|
source -= (extraBytesToRead+1); /* return to the start */
|
||||||
|
break; /* Bail out; shouldn't continue */
|
||||||
|
} else {
|
||||||
|
*target++ = UNI_REPLACEMENT_CHAR;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
/* target is a character in range 0xFFFF - 0x10FFFF. */
|
||||||
|
if (target + 1 >= targetEnd) {
|
||||||
|
source -= (extraBytesToRead+1); /* Back up source pointer! */
|
||||||
|
result = targetExhausted; break;
|
||||||
|
}
|
||||||
|
ch -= halfBase;
|
||||||
|
*target++ = (UTF16)((ch >> halfShift) + UNI_SUR_HIGH_START);
|
||||||
|
*target++ = (UTF16)((ch & halfMask) + UNI_SUR_LOW_START);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
*sourceStart = source;
|
||||||
|
*targetStart = target;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* --------------------------------------------------------------------- */
|
||||||
|
|
||||||
|
ConversionResult ConvertUTF32toUTF8 (
|
||||||
|
const UTF32** sourceStart, const UTF32* sourceEnd,
|
||||||
|
UTF8** targetStart, UTF8* targetEnd, ConversionFlags flags) {
|
||||||
|
ConversionResult result = conversionOK;
|
||||||
|
const UTF32* source = *sourceStart;
|
||||||
|
UTF8* target = *targetStart;
|
||||||
|
while (source < sourceEnd) {
|
||||||
|
UTF32 ch;
|
||||||
|
unsigned short bytesToWrite = 0;
|
||||||
|
const UTF32 byteMask = 0xBF;
|
||||||
|
const UTF32 byteMark = 0x80;
|
||||||
|
ch = *source++;
|
||||||
|
if (flags == strictConversion ) {
|
||||||
|
/* UTF-16 surrogate values are illegal in UTF-32 */
|
||||||
|
if (ch >= UNI_SUR_HIGH_START && ch <= UNI_SUR_LOW_END) {
|
||||||
|
--source; /* return to the illegal value itself */
|
||||||
|
result = sourceIllegal;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/*
|
||||||
|
* Figure out how many bytes the result will require. Turn any
|
||||||
|
* illegally large UTF32 things (> Plane 17) into replacement chars.
|
||||||
|
*/
|
||||||
|
if (ch < (UTF32)0x80) { bytesToWrite = 1;
|
||||||
|
} else if (ch < (UTF32)0x800) { bytesToWrite = 2;
|
||||||
|
} else if (ch < (UTF32)0x10000) { bytesToWrite = 3;
|
||||||
|
} else if (ch <= UNI_MAX_LEGAL_UTF32) { bytesToWrite = 4;
|
||||||
|
} else { bytesToWrite = 3;
|
||||||
|
ch = UNI_REPLACEMENT_CHAR;
|
||||||
|
result = sourceIllegal;
|
||||||
|
}
|
||||||
|
|
||||||
|
target += bytesToWrite;
|
||||||
|
if (target > targetEnd) {
|
||||||
|
--source; /* Back up source pointer! */
|
||||||
|
target -= bytesToWrite; result = targetExhausted; break;
|
||||||
|
}
|
||||||
|
switch (bytesToWrite) { /* note: everything falls through. */
|
||||||
|
case 4: *--target = (UTF8)((ch | byteMark) & byteMask); ch >>= 6;
|
||||||
|
case 3: *--target = (UTF8)((ch | byteMark) & byteMask); ch >>= 6;
|
||||||
|
case 2: *--target = (UTF8)((ch | byteMark) & byteMask); ch >>= 6;
|
||||||
|
case 1: *--target = (UTF8) (ch | firstByteMark[bytesToWrite]);
|
||||||
|
}
|
||||||
|
target += bytesToWrite;
|
||||||
|
}
|
||||||
|
*sourceStart = source;
|
||||||
|
*targetStart = target;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* --------------------------------------------------------------------- */
|
||||||
|
|
||||||
|
ConversionResult ConvertUTF8toUTF32 (
|
||||||
|
const UTF8** sourceStart, const UTF8* sourceEnd,
|
||||||
|
UTF32** targetStart, UTF32* targetEnd, ConversionFlags flags) {
|
||||||
|
ConversionResult result = conversionOK;
|
||||||
|
const UTF8* source = *sourceStart;
|
||||||
|
UTF32* target = *targetStart;
|
||||||
|
while (source < sourceEnd) {
|
||||||
|
UTF32 ch = 0;
|
||||||
|
unsigned short extraBytesToRead = trailingBytesForUTF8[*source];
|
||||||
|
if (source + extraBytesToRead >= sourceEnd) {
|
||||||
|
result = sourceExhausted; break;
|
||||||
|
}
|
||||||
|
/* Do this check whether lenient or strict */
|
||||||
|
if (! isLegalUTF8(source, extraBytesToRead+1)) {
|
||||||
|
result = sourceIllegal;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
/*
|
||||||
|
* The cases all fall through. See "Note A" below.
|
||||||
|
*/
|
||||||
|
switch (extraBytesToRead) {
|
||||||
|
case 5: ch += *source++; ch <<= 6;
|
||||||
|
case 4: ch += *source++; ch <<= 6;
|
||||||
|
case 3: ch += *source++; ch <<= 6;
|
||||||
|
case 2: ch += *source++; ch <<= 6;
|
||||||
|
case 1: ch += *source++; ch <<= 6;
|
||||||
|
case 0: ch += *source++;
|
||||||
|
}
|
||||||
|
ch -= offsetsFromUTF8[extraBytesToRead];
|
||||||
|
|
||||||
|
if (target >= targetEnd) {
|
||||||
|
source -= (extraBytesToRead+1); /* Back up the source pointer! */
|
||||||
|
result = targetExhausted; break;
|
||||||
|
}
|
||||||
|
if (ch <= UNI_MAX_LEGAL_UTF32) {
|
||||||
|
/*
|
||||||
|
* UTF-16 surrogate values are illegal in UTF-32, and anything
|
||||||
|
* over Plane 17 (> 0x10FFFF) is illegal.
|
||||||
|
*/
|
||||||
|
if (ch >= UNI_SUR_HIGH_START && ch <= UNI_SUR_LOW_END) {
|
||||||
|
if (flags == strictConversion) {
|
||||||
|
source -= (extraBytesToRead+1); /* return to the illegal value itself */
|
||||||
|
result = sourceIllegal;
|
||||||
|
break;
|
||||||
|
} else {
|
||||||
|
*target++ = UNI_REPLACEMENT_CHAR;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
*target++ = ch;
|
||||||
|
}
|
||||||
|
} else { /* i.e., ch > UNI_MAX_LEGAL_UTF32 */
|
||||||
|
result = sourceIllegal;
|
||||||
|
*target++ = UNI_REPLACEMENT_CHAR;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
*sourceStart = source;
|
||||||
|
*targetStart = target;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ---------------------------------------------------------------------
|
||||||
|
|
||||||
|
Note A.
|
||||||
|
The fall-through switches in UTF-8 reading code save a
|
||||||
|
temp variable, some decrements & conditionals. The switches
|
||||||
|
are equivalent to the following loop:
|
||||||
|
{
|
||||||
|
int tmpBytesToRead = extraBytesToRead+1;
|
||||||
|
do {
|
||||||
|
ch += *source++;
|
||||||
|
--tmpBytesToRead;
|
||||||
|
if (tmpBytesToRead) ch <<= 6;
|
||||||
|
} while (tmpBytesToRead > 0);
|
||||||
|
}
|
||||||
|
In UTF-8 writing code, the switches on "bytesToWrite" are
|
||||||
|
similarly unrolled loops.
|
||||||
|
|
||||||
|
--------------------------------------------------------------------- */
|
|
@ -0,0 +1,149 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2001-2004 Unicode, Inc.
|
||||||
|
*
|
||||||
|
* Disclaimer
|
||||||
|
*
|
||||||
|
* This source code is provided as is by Unicode, Inc. No claims are
|
||||||
|
* made as to fitness for any particular purpose. No warranties of any
|
||||||
|
* kind are expressed or implied. The recipient agrees to determine
|
||||||
|
* applicability of information provided. If this file has been
|
||||||
|
* purchased on magnetic or optical media from Unicode, Inc., the
|
||||||
|
* sole remedy for any claim will be exchange of defective media
|
||||||
|
* within 90 days of receipt.
|
||||||
|
*
|
||||||
|
* Limitations on Rights to Redistribute This Code
|
||||||
|
*
|
||||||
|
* Unicode, Inc. hereby grants the right to freely use the information
|
||||||
|
* supplied in this file in the creation of products supporting the
|
||||||
|
* Unicode Standard, and to make copies of this file in any form
|
||||||
|
* for internal or external distribution as long as this notice
|
||||||
|
* remains attached.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/* ---------------------------------------------------------------------
|
||||||
|
|
||||||
|
Conversions between UTF32, UTF-16, and UTF-8. Header file.
|
||||||
|
|
||||||
|
Several funtions are included here, forming a complete set of
|
||||||
|
conversions between the three formats. UTF-7 is not included
|
||||||
|
here, but is handled in a separate source file.
|
||||||
|
|
||||||
|
Each of these routines takes pointers to input buffers and output
|
||||||
|
buffers. The input buffers are const.
|
||||||
|
|
||||||
|
Each routine converts the text between *sourceStart and sourceEnd,
|
||||||
|
putting the result into the buffer between *targetStart and
|
||||||
|
targetEnd. Note: the end pointers are *after* the last item: e.g.
|
||||||
|
*(sourceEnd - 1) is the last item.
|
||||||
|
|
||||||
|
The return result indicates whether the conversion was successful,
|
||||||
|
and if not, whether the problem was in the source or target buffers.
|
||||||
|
(Only the first encountered problem is indicated.)
|
||||||
|
|
||||||
|
After the conversion, *sourceStart and *targetStart are both
|
||||||
|
updated to point to the end of last text successfully converted in
|
||||||
|
the respective buffers.
|
||||||
|
|
||||||
|
Input parameters:
|
||||||
|
sourceStart - pointer to a pointer to the source buffer.
|
||||||
|
The contents of this are modified on return so that
|
||||||
|
it points at the next thing to be converted.
|
||||||
|
targetStart - similarly, pointer to pointer to the target buffer.
|
||||||
|
sourceEnd, targetEnd - respectively pointers to the ends of the
|
||||||
|
two buffers, for overflow checking only.
|
||||||
|
|
||||||
|
These conversion functions take a ConversionFlags argument. When this
|
||||||
|
flag is set to strict, both irregular sequences and isolated surrogates
|
||||||
|
will cause an error. When the flag is set to lenient, both irregular
|
||||||
|
sequences and isolated surrogates are converted.
|
||||||
|
|
||||||
|
Whether the flag is strict or lenient, all illegal sequences will cause
|
||||||
|
an error return. This includes sequences such as: <F4 90 80 80>, <C0 80>,
|
||||||
|
or <A0> in UTF-8, and values above 0x10FFFF in UTF-32. Conformant code
|
||||||
|
must check for illegal sequences.
|
||||||
|
|
||||||
|
When the flag is set to lenient, characters over 0x10FFFF are converted
|
||||||
|
to the replacement character; otherwise (when the flag is set to strict)
|
||||||
|
they constitute an error.
|
||||||
|
|
||||||
|
Output parameters:
|
||||||
|
The value "sourceIllegal" is returned from some routines if the input
|
||||||
|
sequence is malformed. When "sourceIllegal" is returned, the source
|
||||||
|
value will point to the illegal value that caused the problem. E.g.,
|
||||||
|
in UTF-8 when a sequence is malformed, it points to the start of the
|
||||||
|
malformed sequence.
|
||||||
|
|
||||||
|
Author: Mark E. Davis, 1994.
|
||||||
|
Rev History: Rick McGowan, fixes & updates May 2001.
|
||||||
|
Fixes & updates, Sept 2001.
|
||||||
|
|
||||||
|
------------------------------------------------------------------------ */
|
||||||
|
|
||||||
|
/* ---------------------------------------------------------------------
|
||||||
|
The following 4 definitions are compiler-specific.
|
||||||
|
The C standard does not guarantee that wchar_t has at least
|
||||||
|
16 bits, so wchar_t is no less portable than unsigned short!
|
||||||
|
All should be unsigned values to avoid sign extension during
|
||||||
|
bit mask & shift operations.
|
||||||
|
------------------------------------------------------------------------ */
|
||||||
|
|
||||||
|
typedef unsigned long UTF32; /* at least 32 bits */
|
||||||
|
typedef unsigned short UTF16; /* at least 16 bits */
|
||||||
|
typedef unsigned char UTF8; /* typically 8 bits */
|
||||||
|
typedef unsigned char Boolean; /* 0 or 1 */
|
||||||
|
|
||||||
|
/* Some fundamental constants */
|
||||||
|
#define UNI_REPLACEMENT_CHAR (UTF32)0x0000FFFD
|
||||||
|
#define UNI_MAX_BMP (UTF32)0x0000FFFF
|
||||||
|
#define UNI_MAX_UTF16 (UTF32)0x0010FFFF
|
||||||
|
#define UNI_MAX_UTF32 (UTF32)0x7FFFFFFF
|
||||||
|
#define UNI_MAX_LEGAL_UTF32 (UTF32)0x0010FFFF
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
conversionOK, /* conversion successful */
|
||||||
|
sourceExhausted, /* partial character in source, but hit end */
|
||||||
|
targetExhausted, /* insuff. room in target for conversion */
|
||||||
|
sourceIllegal /* source sequence is illegal/malformed */
|
||||||
|
} ConversionResult;
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
strictConversion = 0,
|
||||||
|
lenientConversion
|
||||||
|
} ConversionFlags;
|
||||||
|
|
||||||
|
/* This is for C++ and does no harm in C */
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
ConversionResult ConvertUTF8toUTF16 (
|
||||||
|
const UTF8** sourceStart, const UTF8* sourceEnd,
|
||||||
|
UTF16** targetStart, UTF16* targetEnd, ConversionFlags flags);
|
||||||
|
|
||||||
|
ConversionResult ConvertUTF16toUTF8 (
|
||||||
|
const UTF16** sourceStart, const UTF16* sourceEnd,
|
||||||
|
UTF8** targetStart, UTF8* targetEnd, ConversionFlags flags);
|
||||||
|
|
||||||
|
ConversionResult ConvertUTF8toUTF32 (
|
||||||
|
const UTF8** sourceStart, const UTF8* sourceEnd,
|
||||||
|
UTF32** targetStart, UTF32* targetEnd, ConversionFlags flags);
|
||||||
|
|
||||||
|
ConversionResult ConvertUTF32toUTF8 (
|
||||||
|
const UTF32** sourceStart, const UTF32* sourceEnd,
|
||||||
|
UTF8** targetStart, UTF8* targetEnd, ConversionFlags flags);
|
||||||
|
|
||||||
|
ConversionResult ConvertUTF16toUTF32 (
|
||||||
|
const UTF16** sourceStart, const UTF16* sourceEnd,
|
||||||
|
UTF32** targetStart, UTF32* targetEnd, ConversionFlags flags);
|
||||||
|
|
||||||
|
ConversionResult ConvertUTF32toUTF16 (
|
||||||
|
const UTF32** sourceStart, const UTF32* sourceEnd,
|
||||||
|
UTF16** targetStart, UTF16* targetEnd, ConversionFlags flags);
|
||||||
|
|
||||||
|
Boolean isLegalUTF8Sequence(const UTF8 *source, const UTF8 *sourceEnd);
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* --------------------------------------------------------------------- */
|
|
@ -0,0 +1,48 @@
|
||||||
|
#include "guid.h"
|
||||||
|
#include "types.h"
|
||||||
|
|
||||||
|
void Desmume_Guid::newGuid()
|
||||||
|
{
|
||||||
|
for(int i=0;i<size;i++)
|
||||||
|
data[i] = rand();
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string Desmume_Guid::toString()
|
||||||
|
{
|
||||||
|
char buf[37];
|
||||||
|
sprintf(buf,"%08X-%04X-%04X-%04X-%02X%02X%02X%02X%02X%02X",
|
||||||
|
de32lsb(data),de16lsb(data+4),de16lsb(data+6),de16lsb(data+8),data[10],data[11],data[12],data[13],data[14],data[15]);
|
||||||
|
return std::string(buf);
|
||||||
|
}
|
||||||
|
|
||||||
|
Desmume_Guid Desmume_Guid::fromString(std::string str)
|
||||||
|
{
|
||||||
|
Desmume_Guid ret;
|
||||||
|
ret.scan(str);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8 Desmume_Guid::hexToByte(char** ptrptr)
|
||||||
|
{
|
||||||
|
char a = toupper(**ptrptr);
|
||||||
|
(*ptrptr)++;
|
||||||
|
char b = toupper(**ptrptr);
|
||||||
|
(*ptrptr)++;
|
||||||
|
if(a>='A') a=a-'A'+10;
|
||||||
|
else a-='0';
|
||||||
|
if(b>='A') b=b-'A'+10;
|
||||||
|
else b-='0';
|
||||||
|
return ((unsigned char)a<<4)|(unsigned char)b;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Desmume_Guid::scan(std::string& str)
|
||||||
|
{
|
||||||
|
char* endptr = (char*)str.c_str();
|
||||||
|
en32lsb(data,strtoul(endptr,&endptr,16));
|
||||||
|
en16lsb(data+4,strtoul(endptr+1,&endptr,16));
|
||||||
|
en16lsb(data+6,strtoul(endptr+1,&endptr,16));
|
||||||
|
en16lsb(data+8,strtoul(endptr+1,&endptr,16));
|
||||||
|
endptr++;
|
||||||
|
for(int i=0;i<6;i++)
|
||||||
|
data[10+i] = hexToByte(&endptr);
|
||||||
|
}
|
|
@ -0,0 +1,18 @@
|
||||||
|
#ifndef _guid_h_
|
||||||
|
#define _guid_h_
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
#include "types.h"
|
||||||
|
#include "valuearray.h"
|
||||||
|
|
||||||
|
struct Desmume_Guid : public ValueArray<u8,16>
|
||||||
|
{
|
||||||
|
void newGuid();
|
||||||
|
std::string toString();
|
||||||
|
static Desmume_Guid fromString(std::string str);
|
||||||
|
static uint8 hexToByte(char** ptrptr);
|
||||||
|
void scan(std::string& str);
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
#endif
|
|
@ -0,0 +1,248 @@
|
||||||
|
/// \file
|
||||||
|
/// \brief RFC 1321 compliant MD5 implementation,
|
||||||
|
/// RFC 1321 compliant MD5 implementation,
|
||||||
|
/// by Christophe Devine <devine@cr0.net>;
|
||||||
|
/// this program is licensed under the GPL.
|
||||||
|
|
||||||
|
//Modified October 3, 2003, to remove testing code, and add include of "types.h".
|
||||||
|
//Added simple MD5 to ASCII string conversion function.
|
||||||
|
// -Xodnizel
|
||||||
|
|
||||||
|
#include <string.h>
|
||||||
|
#include "../types.h"
|
||||||
|
#include "md5.h"
|
||||||
|
|
||||||
|
typedef u8 uint8;
|
||||||
|
typedef u16 uint16;
|
||||||
|
typedef u32 uint32;
|
||||||
|
|
||||||
|
#define GET_UINT32(n,b,i) \
|
||||||
|
{ \
|
||||||
|
(n) = ( (uint32) (b)[(i) + 3] << 24 ) \
|
||||||
|
| ( (uint32) (b)[(i) + 2] << 16 ) \
|
||||||
|
| ( (uint32) (b)[(i) + 1] << 8 ) \
|
||||||
|
| ( (uint32) (b)[(i) ] ); \
|
||||||
|
}
|
||||||
|
|
||||||
|
#define PUT_UINT32(n,b,i) \
|
||||||
|
{ \
|
||||||
|
(b)[(i) ] = (uint8) ( (n) ); \
|
||||||
|
(b)[(i) + 1] = (uint8) ( (n) >> 8 ); \
|
||||||
|
(b)[(i) + 2] = (uint8) ( (n) >> 16 ); \
|
||||||
|
(b)[(i) + 3] = (uint8) ( (n) >> 24 ); \
|
||||||
|
}
|
||||||
|
|
||||||
|
void md5_starts( struct md5_context *ctx )
|
||||||
|
{
|
||||||
|
ctx->total[0] = 0;
|
||||||
|
ctx->total[1] = 0;
|
||||||
|
ctx->state[0] = 0x67452301;
|
||||||
|
ctx->state[1] = 0xEFCDAB89;
|
||||||
|
ctx->state[2] = 0x98BADCFE;
|
||||||
|
ctx->state[3] = 0x10325476;
|
||||||
|
}
|
||||||
|
|
||||||
|
void md5_process( struct md5_context *ctx, uint8 data[64] )
|
||||||
|
{
|
||||||
|
uint32 A, B, C, D, X[16];
|
||||||
|
|
||||||
|
GET_UINT32( X[0], data, 0 );
|
||||||
|
GET_UINT32( X[1], data, 4 );
|
||||||
|
GET_UINT32( X[2], data, 8 );
|
||||||
|
GET_UINT32( X[3], data, 12 );
|
||||||
|
GET_UINT32( X[4], data, 16 );
|
||||||
|
GET_UINT32( X[5], data, 20 );
|
||||||
|
GET_UINT32( X[6], data, 24 );
|
||||||
|
GET_UINT32( X[7], data, 28 );
|
||||||
|
GET_UINT32( X[8], data, 32 );
|
||||||
|
GET_UINT32( X[9], data, 36 );
|
||||||
|
GET_UINT32( X[10], data, 40 );
|
||||||
|
GET_UINT32( X[11], data, 44 );
|
||||||
|
GET_UINT32( X[12], data, 48 );
|
||||||
|
GET_UINT32( X[13], data, 52 );
|
||||||
|
GET_UINT32( X[14], data, 56 );
|
||||||
|
GET_UINT32( X[15], data, 60 );
|
||||||
|
|
||||||
|
#define S(x,n) ((x << n) | ((x & 0xFFFFFFFF) >> (32 - n)))
|
||||||
|
|
||||||
|
#define P(a,b,c,d,k,s,t) \
|
||||||
|
{ \
|
||||||
|
a += F(b,c,d) + X[k] + t; a = S(a,s) + b; \
|
||||||
|
}
|
||||||
|
|
||||||
|
A = ctx->state[0];
|
||||||
|
B = ctx->state[1];
|
||||||
|
C = ctx->state[2];
|
||||||
|
D = ctx->state[3];
|
||||||
|
|
||||||
|
#define F(x,y,z) (z ^ (x & (y ^ z)))
|
||||||
|
|
||||||
|
P( A, B, C, D, 0, 7, 0xD76AA478 );
|
||||||
|
P( D, A, B, C, 1, 12, 0xE8C7B756 );
|
||||||
|
P( C, D, A, B, 2, 17, 0x242070DB );
|
||||||
|
P( B, C, D, A, 3, 22, 0xC1BDCEEE );
|
||||||
|
P( A, B, C, D, 4, 7, 0xF57C0FAF );
|
||||||
|
P( D, A, B, C, 5, 12, 0x4787C62A );
|
||||||
|
P( C, D, A, B, 6, 17, 0xA8304613 );
|
||||||
|
P( B, C, D, A, 7, 22, 0xFD469501 );
|
||||||
|
P( A, B, C, D, 8, 7, 0x698098D8 );
|
||||||
|
P( D, A, B, C, 9, 12, 0x8B44F7AF );
|
||||||
|
P( C, D, A, B, 10, 17, 0xFFFF5BB1 );
|
||||||
|
P( B, C, D, A, 11, 22, 0x895CD7BE );
|
||||||
|
P( A, B, C, D, 12, 7, 0x6B901122 );
|
||||||
|
P( D, A, B, C, 13, 12, 0xFD987193 );
|
||||||
|
P( C, D, A, B, 14, 17, 0xA679438E );
|
||||||
|
P( B, C, D, A, 15, 22, 0x49B40821 );
|
||||||
|
|
||||||
|
#undef F
|
||||||
|
|
||||||
|
#define F(x,y,z) (y ^ (z & (x ^ y)))
|
||||||
|
|
||||||
|
P( A, B, C, D, 1, 5, 0xF61E2562 );
|
||||||
|
P( D, A, B, C, 6, 9, 0xC040B340 );
|
||||||
|
P( C, D, A, B, 11, 14, 0x265E5A51 );
|
||||||
|
P( B, C, D, A, 0, 20, 0xE9B6C7AA );
|
||||||
|
P( A, B, C, D, 5, 5, 0xD62F105D );
|
||||||
|
P( D, A, B, C, 10, 9, 0x02441453 );
|
||||||
|
P( C, D, A, B, 15, 14, 0xD8A1E681 );
|
||||||
|
P( B, C, D, A, 4, 20, 0xE7D3FBC8 );
|
||||||
|
P( A, B, C, D, 9, 5, 0x21E1CDE6 );
|
||||||
|
P( D, A, B, C, 14, 9, 0xC33707D6 );
|
||||||
|
P( C, D, A, B, 3, 14, 0xF4D50D87 );
|
||||||
|
P( B, C, D, A, 8, 20, 0x455A14ED );
|
||||||
|
P( A, B, C, D, 13, 5, 0xA9E3E905 );
|
||||||
|
P( D, A, B, C, 2, 9, 0xFCEFA3F8 );
|
||||||
|
P( C, D, A, B, 7, 14, 0x676F02D9 );
|
||||||
|
P( B, C, D, A, 12, 20, 0x8D2A4C8A );
|
||||||
|
|
||||||
|
#undef F
|
||||||
|
|
||||||
|
#define F(x,y,z) (x ^ y ^ z)
|
||||||
|
|
||||||
|
P( A, B, C, D, 5, 4, 0xFFFA3942 );
|
||||||
|
P( D, A, B, C, 8, 11, 0x8771F681 );
|
||||||
|
P( C, D, A, B, 11, 16, 0x6D9D6122 );
|
||||||
|
P( B, C, D, A, 14, 23, 0xFDE5380C );
|
||||||
|
P( A, B, C, D, 1, 4, 0xA4BEEA44 );
|
||||||
|
P( D, A, B, C, 4, 11, 0x4BDECFA9 );
|
||||||
|
P( C, D, A, B, 7, 16, 0xF6BB4B60 );
|
||||||
|
P( B, C, D, A, 10, 23, 0xBEBFBC70 );
|
||||||
|
P( A, B, C, D, 13, 4, 0x289B7EC6 );
|
||||||
|
P( D, A, B, C, 0, 11, 0xEAA127FA );
|
||||||
|
P( C, D, A, B, 3, 16, 0xD4EF3085 );
|
||||||
|
P( B, C, D, A, 6, 23, 0x04881D05 );
|
||||||
|
P( A, B, C, D, 9, 4, 0xD9D4D039 );
|
||||||
|
P( D, A, B, C, 12, 11, 0xE6DB99E5 );
|
||||||
|
P( C, D, A, B, 15, 16, 0x1FA27CF8 );
|
||||||
|
P( B, C, D, A, 2, 23, 0xC4AC5665 );
|
||||||
|
|
||||||
|
#undef F
|
||||||
|
|
||||||
|
#define F(x,y,z) (y ^ (x | ~z))
|
||||||
|
|
||||||
|
P( A, B, C, D, 0, 6, 0xF4292244 );
|
||||||
|
P( D, A, B, C, 7, 10, 0x432AFF97 );
|
||||||
|
P( C, D, A, B, 14, 15, 0xAB9423A7 );
|
||||||
|
P( B, C, D, A, 5, 21, 0xFC93A039 );
|
||||||
|
P( A, B, C, D, 12, 6, 0x655B59C3 );
|
||||||
|
P( D, A, B, C, 3, 10, 0x8F0CCC92 );
|
||||||
|
P( C, D, A, B, 10, 15, 0xFFEFF47D );
|
||||||
|
P( B, C, D, A, 1, 21, 0x85845DD1 );
|
||||||
|
P( A, B, C, D, 8, 6, 0x6FA87E4F );
|
||||||
|
P( D, A, B, C, 15, 10, 0xFE2CE6E0 );
|
||||||
|
P( C, D, A, B, 6, 15, 0xA3014314 );
|
||||||
|
P( B, C, D, A, 13, 21, 0x4E0811A1 );
|
||||||
|
P( A, B, C, D, 4, 6, 0xF7537E82 );
|
||||||
|
P( D, A, B, C, 11, 10, 0xBD3AF235 );
|
||||||
|
P( C, D, A, B, 2, 15, 0x2AD7D2BB );
|
||||||
|
P( B, C, D, A, 9, 21, 0xEB86D391 );
|
||||||
|
|
||||||
|
#undef F
|
||||||
|
|
||||||
|
ctx->state[0] += A;
|
||||||
|
ctx->state[1] += B;
|
||||||
|
ctx->state[2] += C;
|
||||||
|
ctx->state[3] += D;
|
||||||
|
}
|
||||||
|
|
||||||
|
void md5_update( struct md5_context *ctx, uint8 *input, uint32 length )
|
||||||
|
{
|
||||||
|
uint32 left, fill;
|
||||||
|
|
||||||
|
if( ! length ) return;
|
||||||
|
|
||||||
|
left = ( ctx->total[0] >> 3 ) & 0x3F;
|
||||||
|
fill = 64 - left;
|
||||||
|
|
||||||
|
ctx->total[0] += length << 3;
|
||||||
|
ctx->total[1] += length >> 29;
|
||||||
|
|
||||||
|
ctx->total[0] &= 0xFFFFFFFF;
|
||||||
|
ctx->total[1] += ctx->total[0] < ( length << 3 );
|
||||||
|
|
||||||
|
if( left && length >= fill )
|
||||||
|
{
|
||||||
|
memcpy( (void *) (ctx->buffer + left), (void *) input, fill );
|
||||||
|
md5_process( ctx, ctx->buffer );
|
||||||
|
length -= fill;
|
||||||
|
input += fill;
|
||||||
|
left = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
while( length >= 64 )
|
||||||
|
{
|
||||||
|
md5_process( ctx, input );
|
||||||
|
length -= 64;
|
||||||
|
input += 64;
|
||||||
|
}
|
||||||
|
|
||||||
|
if( length )
|
||||||
|
{
|
||||||
|
memcpy( (void *) (ctx->buffer + left), (void *) input, length );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static uint8 md5_padding[64] =
|
||||||
|
{
|
||||||
|
0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||||
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||||
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||||
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
|
||||||
|
};
|
||||||
|
|
||||||
|
void md5_finish( struct md5_context *ctx, uint8 digest[16] )
|
||||||
|
{
|
||||||
|
uint32 last, padn;
|
||||||
|
uint8 msglen[8];
|
||||||
|
|
||||||
|
PUT_UINT32( ctx->total[0], msglen, 0 );
|
||||||
|
PUT_UINT32( ctx->total[1], msglen, 4 );
|
||||||
|
|
||||||
|
last = ( ctx->total[0] >> 3 ) & 0x3F;
|
||||||
|
padn = ( last < 56 ) ? ( 56 - last ) : ( 120 - last );
|
||||||
|
|
||||||
|
md5_update( ctx, md5_padding, padn );
|
||||||
|
md5_update( ctx, msglen, 8 );
|
||||||
|
|
||||||
|
PUT_UINT32( ctx->state[0], digest, 0 );
|
||||||
|
PUT_UINT32( ctx->state[1], digest, 4 );
|
||||||
|
PUT_UINT32( ctx->state[2], digest, 8 );
|
||||||
|
PUT_UINT32( ctx->state[3], digest, 12 );
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/* Uses a static buffer, so beware of how it's used. */
|
||||||
|
char *md5_asciistr(MD5DATA& md5)
|
||||||
|
{
|
||||||
|
uint8* digest = md5.data;
|
||||||
|
static char str[33];
|
||||||
|
static char trans[16]={'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
|
||||||
|
int x;
|
||||||
|
|
||||||
|
for(x=0;x<16;x++)
|
||||||
|
{
|
||||||
|
str[x*2]=trans[digest[x]&0x0F];
|
||||||
|
str[x*2+1]=trans[digest[x]>>4];
|
||||||
|
}
|
||||||
|
return(str);
|
||||||
|
}
|
|
@ -0,0 +1,23 @@
|
||||||
|
#ifndef _MD5_H
|
||||||
|
#define _MD5_H
|
||||||
|
|
||||||
|
#include "types.h"
|
||||||
|
#include "valuearray.h"
|
||||||
|
|
||||||
|
struct md5_context
|
||||||
|
{
|
||||||
|
u32 total[2];
|
||||||
|
u32 state[4];
|
||||||
|
u8 buffer[64];
|
||||||
|
};
|
||||||
|
|
||||||
|
typedef ValueArray<uint8,16> MD5DATA;
|
||||||
|
|
||||||
|
void md5_starts( struct md5_context *ctx );
|
||||||
|
void md5_update( struct md5_context *ctx, u8 *input, u32 length );
|
||||||
|
void md5_finish( struct md5_context *ctx, u8 digest[16] );
|
||||||
|
|
||||||
|
/* Uses a static buffer, so beware of how it's used. */
|
||||||
|
char *md5_asciistr(MD5DATA& md5);
|
||||||
|
|
||||||
|
#endif /* md5.h */
|
|
@ -0,0 +1,21 @@
|
||||||
|
#ifndef _VALUEARRAY_H_
|
||||||
|
#define _VALUEARRAY_H_
|
||||||
|
|
||||||
|
template<typename T, int N>
|
||||||
|
struct ValueArray
|
||||||
|
{
|
||||||
|
T data[N];
|
||||||
|
T &operator[](int index) { return data[index]; }
|
||||||
|
static const int size = N;
|
||||||
|
bool operator!=(ValueArray<T,N> &other) { return !operator==(other); }
|
||||||
|
bool operator==(ValueArray<T,N> &other)
|
||||||
|
{
|
||||||
|
for(int i=0;i<size;i++)
|
||||||
|
if(data[i] != other[i])
|
||||||
|
return false;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
|
@ -0,0 +1,688 @@
|
||||||
|
//taken from fceux on 10/27/08
|
||||||
|
|
||||||
|
#include "xstring.h"
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
///Upper case routine. Returns number of characters modified
|
||||||
|
int str_ucase(char *str) {
|
||||||
|
unsigned int i=0,j=0; //mbg merge 7/17/06 changed to unsigned int
|
||||||
|
|
||||||
|
while (i < strlen(str)) {
|
||||||
|
if ((str[i] >= 'a') && (str[i] <= 'z')) {
|
||||||
|
str[i] &= ~0x20;
|
||||||
|
j++;
|
||||||
|
}
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
return j;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
///Lower case routine. Returns number of characters modified
|
||||||
|
int str_lcase(char *str) {
|
||||||
|
unsigned int i=0,j=0; //mbg merge 7/17/06 changed to unsigned int
|
||||||
|
|
||||||
|
while (i < strlen(str)) {
|
||||||
|
if ((str[i] >= 'A') && (str[i] <= 'Z')) {
|
||||||
|
str[i] |= 0x20;
|
||||||
|
j++;
|
||||||
|
}
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
return j;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
///White space-trimming routine
|
||||||
|
|
||||||
|
///Removes whitespace from left side of string, depending on the flags set (See STRIP_x definitions in xstring.h)
|
||||||
|
///Returns number of characters removed
|
||||||
|
int str_ltrim(char *str, int flags) {
|
||||||
|
unsigned int i=0; //mbg merge 7/17/06 changed to unsigned int
|
||||||
|
|
||||||
|
while (str[0]) {
|
||||||
|
if ((str[0] != ' ') || (str[0] != '\t') || (str[0] != '\r') || (str[0] != '\n')) break;
|
||||||
|
|
||||||
|
if ((flags & STRIP_SP) && (str[0] == ' ')) {
|
||||||
|
i++;
|
||||||
|
strcpy(str,str+1);
|
||||||
|
}
|
||||||
|
if ((flags & STRIP_TAB) && (str[0] == '\t')) {
|
||||||
|
i++;
|
||||||
|
strcpy(str,str+1);
|
||||||
|
}
|
||||||
|
if ((flags & STRIP_CR) && (str[0] == '\r')) {
|
||||||
|
i++;
|
||||||
|
strcpy(str,str+1);
|
||||||
|
}
|
||||||
|
if ((flags & STRIP_LF) && (str[0] == '\n')) {
|
||||||
|
i++;
|
||||||
|
strcpy(str,str+1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
///White space-trimming routine
|
||||||
|
|
||||||
|
///Removes whitespace from right side of string, depending on the flags set (See STRIP_x definitions in xstring.h)
|
||||||
|
///Returns number of characters removed
|
||||||
|
int str_rtrim(char *str, int flags) {
|
||||||
|
unsigned int i=0; //mbg merge 7/17/06 changed to unsigned int
|
||||||
|
|
||||||
|
while (strlen(str)) {
|
||||||
|
if ((str[strlen(str)-1] != ' ') ||
|
||||||
|
(str[strlen(str)-1] != '\t') ||
|
||||||
|
(str[strlen(str)-1] != '\r') ||
|
||||||
|
(str[strlen(str)-1] != '\n')) break;
|
||||||
|
|
||||||
|
if ((flags & STRIP_SP) && (str[0] == ' ')) {
|
||||||
|
i++;
|
||||||
|
str[strlen(str)-1] = 0;
|
||||||
|
}
|
||||||
|
if ((flags & STRIP_TAB) && (str[0] == '\t')) {
|
||||||
|
i++;
|
||||||
|
str[strlen(str)-1] = 0;
|
||||||
|
}
|
||||||
|
if ((flags & STRIP_CR) && (str[0] == '\r')) {
|
||||||
|
i++;
|
||||||
|
str[strlen(str)-1] = 0;
|
||||||
|
}
|
||||||
|
if ((flags & STRIP_LF) && (str[0] == '\n')) {
|
||||||
|
i++;
|
||||||
|
str[strlen(str)-1] = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
///White space-stripping routine
|
||||||
|
|
||||||
|
///Removes whitespace depending on the flags set (See STRIP_x definitions in xstring.h)
|
||||||
|
///Returns number of characters removed, or -1 on error
|
||||||
|
int str_strip(char *str, int flags) {
|
||||||
|
unsigned int i=0,j=0; //mbg merge 7/17/06 changed to unsigned int
|
||||||
|
char *astr,chr;
|
||||||
|
|
||||||
|
if (!strlen(str)) return -1;
|
||||||
|
if (!(flags & (STRIP_SP|STRIP_TAB|STRIP_CR|STRIP_LF))) return -1;
|
||||||
|
if (!(astr = (char*)malloc(strlen(str)+1))) return -1;
|
||||||
|
while (i < strlen(str)) {
|
||||||
|
chr = str[i++];
|
||||||
|
if ((flags & STRIP_SP) && (chr == ' ')) chr = 0;
|
||||||
|
if ((flags & STRIP_TAB) && (chr == '\t')) chr = 0;
|
||||||
|
if ((flags & STRIP_CR) && (chr == '\r')) chr = 0;
|
||||||
|
if ((flags & STRIP_LF) && (chr == '\n')) chr = 0;
|
||||||
|
|
||||||
|
if (chr) astr[j++] = chr;
|
||||||
|
}
|
||||||
|
astr[j] = 0;
|
||||||
|
strcpy(str,astr);
|
||||||
|
free(astr);
|
||||||
|
return j;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
///Character replacement routine
|
||||||
|
|
||||||
|
///Replaces all instances of 'search' with 'replace'
|
||||||
|
///Returns number of characters modified
|
||||||
|
int chr_replace(char *str, char search, char replace) {
|
||||||
|
unsigned int i=0,j=0; //mbg merge 7/17/06 changed to unsigned int
|
||||||
|
|
||||||
|
while (i < strlen(str)) {
|
||||||
|
if (str[i] == search) {
|
||||||
|
str[i] = replace;
|
||||||
|
j++;
|
||||||
|
}
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
return j;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
///Sub-String replacement routine
|
||||||
|
|
||||||
|
///Replaces all instances of 'search' with 'replace'
|
||||||
|
///Returns number of sub-strings modified, or -1 on error
|
||||||
|
int str_replace(char *str, char *search, char *replace) {
|
||||||
|
unsigned int i=0,j=0; //mbg merge 7/17/06 changed to unsigned int
|
||||||
|
int searchlen,replacelen;
|
||||||
|
char *astr;
|
||||||
|
|
||||||
|
searchlen = strlen(search);
|
||||||
|
replacelen = strlen(replace);
|
||||||
|
if ((!strlen(str)) || (!searchlen)) return -1; //note: allow *replace to have a length of zero!
|
||||||
|
if (!(astr = (char*)malloc(strlen(str)+1))) return -1;
|
||||||
|
while (i < strlen(str)) {
|
||||||
|
if (!strncmp(str+i,search,searchlen)) {
|
||||||
|
if (replacelen) memcpy(astr+j,replace,replacelen);
|
||||||
|
i += searchlen;
|
||||||
|
j += replacelen;
|
||||||
|
}
|
||||||
|
else astr[j++] = str[i++];
|
||||||
|
}
|
||||||
|
astr[j] = 0;
|
||||||
|
strcpy(str,astr);
|
||||||
|
free(astr);
|
||||||
|
return j;
|
||||||
|
}
|
||||||
|
|
||||||
|
static const struct Base64Table
|
||||||
|
{
|
||||||
|
Base64Table()
|
||||||
|
{
|
||||||
|
size_t a=0;
|
||||||
|
for(a=0; a<256; ++a) data[a] = 0xFF; // mark everything as invalid by default
|
||||||
|
// create value->ascii mapping
|
||||||
|
a=0;
|
||||||
|
for(unsigned char c='A'; c<='Z'; ++c) data[a++] = c; // 0..25
|
||||||
|
for(unsigned char c='a'; c<='z'; ++c) data[a++] = c; // 26..51
|
||||||
|
for(unsigned char c='0'; c<='9'; ++c) data[a++] = c; // 52..61
|
||||||
|
data[62] = '+'; // 62
|
||||||
|
data[63] = '/'; // 63
|
||||||
|
// create ascii->value mapping (but due to overlap, write it to highbit region)
|
||||||
|
for(a=0; a<64; ++a) data[data[a]^0x80] = a; //
|
||||||
|
data[((unsigned char)'=') ^ 0x80] = 0;
|
||||||
|
}
|
||||||
|
unsigned char operator[] (size_t pos) const { return data[pos]; }
|
||||||
|
private:
|
||||||
|
unsigned char data[256];
|
||||||
|
} Base64Table;
|
||||||
|
|
||||||
|
///Converts the provided data to a string in a standard, user-friendly, round-trippable format
|
||||||
|
std::string BytesToString(const void* data, int len)
|
||||||
|
{
|
||||||
|
char temp[16];
|
||||||
|
if(len==1) {
|
||||||
|
sprintf(temp,"%d",*(const unsigned char*)data);
|
||||||
|
return temp;
|
||||||
|
} else if(len==2) {
|
||||||
|
sprintf(temp,"%d",*(const unsigned short*)data);
|
||||||
|
return temp;
|
||||||
|
} else if(len==4) {
|
||||||
|
sprintf(temp,"%d",*(const unsigned int*)data);
|
||||||
|
return temp;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string ret;
|
||||||
|
if(1) // use base64
|
||||||
|
{
|
||||||
|
const unsigned char* src = (const unsigned char*)data;
|
||||||
|
ret = "base64:";
|
||||||
|
for(int n; len > 0; len -= n)
|
||||||
|
{
|
||||||
|
unsigned char input[3] = {0,0,0};
|
||||||
|
for(n=0; n<3 && n<len; ++n)
|
||||||
|
input[n] = *src++;
|
||||||
|
unsigned char output[4] =
|
||||||
|
{
|
||||||
|
Base64Table[ input[0] >> 2 ],
|
||||||
|
Base64Table[ ((input[0] & 0x03) << 4) | (input[1] >> 4) ],
|
||||||
|
n<2 ? '=' : Base64Table[ ((input[1] & 0x0F) << 2) | (input[2] >> 6) ],
|
||||||
|
n<3 ? '=' : Base64Table[ input[2] & 0x3F ]
|
||||||
|
};
|
||||||
|
ret.append(output, output+4);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else // use hex
|
||||||
|
{
|
||||||
|
ret.resize(len*2+2);
|
||||||
|
ret[0] = '0';
|
||||||
|
ret[1] = 'x';
|
||||||
|
for(int i=0;i<len;i++)
|
||||||
|
{
|
||||||
|
int a = (((const unsigned char*)data)[i]>>4);
|
||||||
|
int b = (((const unsigned char*)data)[i])&15;
|
||||||
|
if(a>9) a += 'A'-10;
|
||||||
|
else a += '0';
|
||||||
|
if(b>9) b += 'A'-10;
|
||||||
|
else b += '0';
|
||||||
|
ret[2+i*2] = a;
|
||||||
|
ret[2+i*2+1] = b;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
///returns -1 if this is not a hex string
|
||||||
|
int HexStringToBytesLength(const std::string& str)
|
||||||
|
{
|
||||||
|
if(str.size()>2 && str[0] == '0' && toupper(str[1]) == 'X')
|
||||||
|
return str.size()/2-1;
|
||||||
|
else return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int Base64StringToBytesLength(const std::string& str)
|
||||||
|
{
|
||||||
|
if(str.size() < 7 || (str.size()-7) % 4 || str.substr(0,7) != "base64:") return -1;
|
||||||
|
|
||||||
|
size_t c = ((str.size() - 7) / 4) * 3;
|
||||||
|
if(str[str.size()-1] == '=') { --c;
|
||||||
|
if(str[str.size()-2] == '=') --c; }
|
||||||
|
return c;
|
||||||
|
}
|
||||||
|
|
||||||
|
///parses a string in the same format as BytesToString
|
||||||
|
///returns true if success.
|
||||||
|
bool StringToBytes(const std::string& str, void* data, int len)
|
||||||
|
{
|
||||||
|
if(str.substr(0,7) == "base64:")
|
||||||
|
{
|
||||||
|
// base64
|
||||||
|
unsigned char* tgt = (unsigned char*)data;
|
||||||
|
for(size_t pos = 7; pos < str.size() && len > 0; )
|
||||||
|
{
|
||||||
|
unsigned char input[4], converted[4];
|
||||||
|
for(int i=0; i<4; ++i)
|
||||||
|
{
|
||||||
|
if(pos >= str.size() && i > 0) return false; // invalid data
|
||||||
|
input[i] = str[pos++];
|
||||||
|
if(input[i] & 0x80) return false; // illegal character
|
||||||
|
converted[i] = Base64Table[input[i]^0x80];
|
||||||
|
if(converted[i] & 0x80) return false; // illegal character
|
||||||
|
}
|
||||||
|
unsigned char outpacket[3] =
|
||||||
|
{
|
||||||
|
(converted[0] << 2) | (converted[1] >> 4),
|
||||||
|
(converted[1] << 4) | (converted[2] >> 2),
|
||||||
|
(converted[2] << 6) | (converted[3])
|
||||||
|
};
|
||||||
|
int outlen = (input[2] == '=') ? 1 : (input[3] == '=' ? 2 : 3);
|
||||||
|
if(outlen > len) outlen = len;
|
||||||
|
memcpy(tgt, outpacket, outlen);
|
||||||
|
tgt += outlen;
|
||||||
|
len -= outlen;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if(str.size()>2 && str[0] == '0' && toupper(str[1]) == 'X')
|
||||||
|
{
|
||||||
|
// hex
|
||||||
|
int amt = len;
|
||||||
|
int bytesAvailable = str.size()/2;
|
||||||
|
if(bytesAvailable < amt)
|
||||||
|
amt = bytesAvailable;
|
||||||
|
const char* cstr = str.c_str()+2;
|
||||||
|
for(int i=0;i<amt;i++) {
|
||||||
|
char a = toupper(cstr[i*2]);
|
||||||
|
char b = toupper(cstr[i*2+1]);
|
||||||
|
if(a>='A') a=a-'A'+10;
|
||||||
|
else a-='0';
|
||||||
|
if(b>='A') b=b-'A'+10;
|
||||||
|
else b-='0';
|
||||||
|
unsigned char val = ((unsigned char)a<<4)|(unsigned char)b;
|
||||||
|
((unsigned char*)data)[i] = val;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(len==1) {
|
||||||
|
int x = atoi(str.c_str());
|
||||||
|
*(unsigned char*)data = x;
|
||||||
|
return true;
|
||||||
|
} else if(len==2) {
|
||||||
|
int x = atoi(str.c_str());
|
||||||
|
*(unsigned short*)data = x;
|
||||||
|
return true;
|
||||||
|
} else if(len==4) {
|
||||||
|
int x = atoi(str.c_str());
|
||||||
|
*(unsigned int*)data = x;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
//we can't handle it
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
/// \brief convert input string into vector of string tokens
|
||||||
|
///
|
||||||
|
/// \note consecutive delimiters will be treated as single delimiter
|
||||||
|
/// \note delimiters are _not_ included in return data
|
||||||
|
///
|
||||||
|
/// \param input string to be parsed
|
||||||
|
/// \param delims list of delimiters.
|
||||||
|
|
||||||
|
std::vector<std::string> tokenize_str(const std::string & str,
|
||||||
|
const std::string & delims=", \t")
|
||||||
|
{
|
||||||
|
using namespace std;
|
||||||
|
// Skip delims at beginning, find start of first token
|
||||||
|
string::size_type lastPos = str.find_first_not_of(delims, 0);
|
||||||
|
// Find next delimiter @ end of token
|
||||||
|
string::size_type pos = str.find_first_of(delims, lastPos);
|
||||||
|
|
||||||
|
// output vector
|
||||||
|
vector<string> tokens;
|
||||||
|
|
||||||
|
while (string::npos != pos || string::npos != lastPos)
|
||||||
|
{
|
||||||
|
// Found a token, add it to the vector.
|
||||||
|
tokens.push_back(str.substr(lastPos, pos - lastPos));
|
||||||
|
// Skip delims. Note the "not_of". this is beginning of token
|
||||||
|
lastPos = str.find_first_not_of(delims, pos);
|
||||||
|
// Find next delimiter at end of token.
|
||||||
|
pos = str.find_first_of(delims, lastPos);
|
||||||
|
}
|
||||||
|
|
||||||
|
return tokens;
|
||||||
|
}
|
||||||
|
|
||||||
|
//this code was taken from WINE (LGPL)
|
||||||
|
//http://google.com/codesearch?hl=en&q=+lang:c+splitpath+show:CPvw9Z-euls:_RSotQzmLeU:KGzljMEYFbY&sa=N&cd=9&ct=rc&cs_p=http://gentoo.osuosl.org/distfiles/Wine-20050524.tar.gz&cs_f=wine-20050524/programs/winefile/splitpath.c
|
||||||
|
void splitpath(const char* path, char* drv, char* dir, char* name, char* ext)
|
||||||
|
{
|
||||||
|
const char* end; /* end of processed string */
|
||||||
|
const char* p; /* search pointer */
|
||||||
|
const char* s; /* copy pointer */
|
||||||
|
|
||||||
|
/* extract drive name */
|
||||||
|
if (path[0] && path[1]==':') {
|
||||||
|
if (drv) {
|
||||||
|
*drv++ = *path++;
|
||||||
|
*drv++ = *path++;
|
||||||
|
*drv = '\0';
|
||||||
|
} else path+=2;
|
||||||
|
} else if (drv)
|
||||||
|
*drv = '\0';
|
||||||
|
|
||||||
|
/* search for end of string or stream separator */
|
||||||
|
for(end=path; *end && *end!=':'; )
|
||||||
|
end++;
|
||||||
|
|
||||||
|
/* search for begin of file extension */
|
||||||
|
for(p=end; p>path && *--p!='\\' && *p!='/'; )
|
||||||
|
if (*p == '.') {
|
||||||
|
end = p;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ext)
|
||||||
|
for(s=end; (*ext=*s++); )
|
||||||
|
ext++;
|
||||||
|
else
|
||||||
|
for(s=end; *s++; ) {}
|
||||||
|
|
||||||
|
/* search for end of directory name */
|
||||||
|
for(p=end; p>path; )
|
||||||
|
if (*--p=='\\' || *p=='/') {
|
||||||
|
p++;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (name) {
|
||||||
|
for(s=p; s<end; )
|
||||||
|
*name++ = *s++;
|
||||||
|
|
||||||
|
*name = '\0';
|
||||||
|
} else
|
||||||
|
for(s=p; s<end; )
|
||||||
|
*s++;
|
||||||
|
|
||||||
|
if (dir) {
|
||||||
|
for(s=path; s<p; )
|
||||||
|
*dir++ = *s++;
|
||||||
|
|
||||||
|
*dir = '\0';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//mbg 5/12/08
|
||||||
|
//for the curious, I tested U16ToHexStr and it was 10x faster than printf.
|
||||||
|
//so the author of these dedicated functions is not insane, and I will leave them.
|
||||||
|
|
||||||
|
static char TempArray[11];
|
||||||
|
|
||||||
|
uint16 FastStrToU16(char* s, bool& valid)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
uint16 v=0;
|
||||||
|
for(i=0; i < 4; i++)
|
||||||
|
{
|
||||||
|
if(s[i] == 0) return v;
|
||||||
|
v<<=4;
|
||||||
|
if(s[i] >= '0' && s[i] <= '9')
|
||||||
|
{
|
||||||
|
v+=s[i]-'0';
|
||||||
|
}
|
||||||
|
else if(s[i] >= 'a' && s[i] <= 'f')
|
||||||
|
{
|
||||||
|
v+=s[i]-'a'+10;
|
||||||
|
}
|
||||||
|
else if(s[i] >= 'A' && s[i] <= 'F')
|
||||||
|
{
|
||||||
|
v+=s[i]-'A'+10;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
valid = false;
|
||||||
|
return 0xFFFF;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
valid = true;
|
||||||
|
return v;
|
||||||
|
}
|
||||||
|
|
||||||
|
char *U8ToDecStr(uint8 a)
|
||||||
|
{
|
||||||
|
TempArray[0] = '0' + a/100;
|
||||||
|
TempArray[1] = '0' + (a%100)/10;
|
||||||
|
TempArray[2] = '0' + (a%10);
|
||||||
|
TempArray[3] = 0;
|
||||||
|
return TempArray;
|
||||||
|
}
|
||||||
|
|
||||||
|
char *U16ToDecStr(uint16 a)
|
||||||
|
{
|
||||||
|
TempArray[0] = '0' + a/10000;
|
||||||
|
TempArray[1] = '0' + (a%10000)/1000;
|
||||||
|
TempArray[2] = '0' + (a%1000)/100;
|
||||||
|
TempArray[3] = '0' + (a%100)/10;
|
||||||
|
TempArray[4] = '0' + (a%10);
|
||||||
|
TempArray[5] = 0;
|
||||||
|
return TempArray;
|
||||||
|
}
|
||||||
|
|
||||||
|
char *U32ToDecStr(char* buf, uint32 a)
|
||||||
|
{
|
||||||
|
buf[0] = '0' + a/1000000000;
|
||||||
|
buf[1] = '0' + (a%1000000000)/100000000;
|
||||||
|
buf[2] = '0' + (a%100000000)/10000000;
|
||||||
|
buf[3] = '0' + (a%10000000)/1000000;
|
||||||
|
buf[4] = '0' + (a%1000000)/100000;
|
||||||
|
buf[5] = '0' + (a%100000)/10000;
|
||||||
|
buf[6] = '0' + (a%10000)/1000;
|
||||||
|
buf[7] = '0' + (a%1000)/100;
|
||||||
|
buf[8] = '0' + (a%100)/10;
|
||||||
|
buf[9] = '0' + (a%10);
|
||||||
|
buf[10] = 0;
|
||||||
|
return buf;
|
||||||
|
}
|
||||||
|
char *U32ToDecStr(uint32 a)
|
||||||
|
{
|
||||||
|
return U32ToDecStr(TempArray,a);
|
||||||
|
}
|
||||||
|
|
||||||
|
char *U16ToHexStr(uint16 a)
|
||||||
|
{
|
||||||
|
TempArray[0] = a/4096 > 9?'A'+a/4096-10:'0' + a/4096;
|
||||||
|
TempArray[1] = (a%4096)/256 > 9?'A'+(a%4096)/256 - 10:'0' + (a%4096)/256;
|
||||||
|
TempArray[2] = (a%256)/16 > 9?'A'+(a%256)/16 - 10:'0' + (a%256)/16;
|
||||||
|
TempArray[3] = a%16 > 9?'A'+(a%16) - 10:'0' + (a%16);
|
||||||
|
TempArray[4] = 0;
|
||||||
|
return TempArray;
|
||||||
|
}
|
||||||
|
|
||||||
|
char *U8ToHexStr(uint8 a)
|
||||||
|
{
|
||||||
|
TempArray[0] = a/16 > 9?'A'+a/16 - 10:'0' + a/16;
|
||||||
|
TempArray[1] = a%16 > 9?'A'+(a%16) - 10:'0' + (a%16);
|
||||||
|
TempArray[2] = 0;
|
||||||
|
return TempArray;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string stditoa(int n)
|
||||||
|
{
|
||||||
|
char tempbuf[16];
|
||||||
|
sprintf(tempbuf, "%d", n);
|
||||||
|
return tempbuf;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
std::string readNullTerminatedAscii(std::istream* is)
|
||||||
|
{
|
||||||
|
std::string ret;
|
||||||
|
ret.reserve(50);
|
||||||
|
for(;;)
|
||||||
|
{
|
||||||
|
int c = is->get();
|
||||||
|
if(c == 0) break;
|
||||||
|
else ret += (char)c;
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
// replace all instances of victim with replacement
|
||||||
|
std::string mass_replace(const std::string &source, const std::string &victim, const std::string &replacement)
|
||||||
|
{
|
||||||
|
std::string answer = source;
|
||||||
|
std::string::size_type j = 0;
|
||||||
|
while ((j = answer.find(victim, j)) != std::string::npos )
|
||||||
|
answer.replace(j, victim.length(), replacement);
|
||||||
|
return answer;
|
||||||
|
}
|
||||||
|
|
||||||
|
//http://www.codeproject.com/KB/string/UtfConverter.aspx
|
||||||
|
#include "ConvertUTF.h"
|
||||||
|
namespace UtfConverter
|
||||||
|
{
|
||||||
|
std::wstring FromUtf8(const std::string& utf8string)
|
||||||
|
{
|
||||||
|
size_t widesize = utf8string.length();
|
||||||
|
if (sizeof(wchar_t) == 2)
|
||||||
|
{
|
||||||
|
wchar_t* widestringnative = new wchar_t[widesize+1];
|
||||||
|
const UTF8* sourcestart = reinterpret_cast<const UTF8*>(utf8string.c_str());
|
||||||
|
const UTF8* sourceend = sourcestart + widesize;
|
||||||
|
UTF16* targetstart = reinterpret_cast<UTF16*>(widestringnative);
|
||||||
|
UTF16* targetend = targetstart + widesize+1;
|
||||||
|
ConversionResult res = ConvertUTF8toUTF16(&sourcestart, sourceend, &targetstart, targetend, strictConversion);
|
||||||
|
if (res != conversionOK)
|
||||||
|
{
|
||||||
|
delete [] widestringnative;
|
||||||
|
throw std::exception();
|
||||||
|
}
|
||||||
|
*targetstart = 0;
|
||||||
|
std::wstring resultstring(widestringnative);
|
||||||
|
delete [] widestringnative;
|
||||||
|
return resultstring;
|
||||||
|
}
|
||||||
|
else if (sizeof(wchar_t) == 4)
|
||||||
|
{
|
||||||
|
wchar_t* widestringnative = new wchar_t[widesize];
|
||||||
|
const UTF8* sourcestart = reinterpret_cast<const UTF8*>(utf8string.c_str());
|
||||||
|
const UTF8* sourceend = sourcestart + widesize;
|
||||||
|
UTF32* targetstart = reinterpret_cast<UTF32*>(widestringnative);
|
||||||
|
UTF32* targetend = targetstart + widesize;
|
||||||
|
ConversionResult res = ConvertUTF8toUTF32(&sourcestart, sourceend, &targetstart, targetend, strictConversion);
|
||||||
|
if (res != conversionOK)
|
||||||
|
{
|
||||||
|
delete [] widestringnative;
|
||||||
|
throw std::exception();
|
||||||
|
}
|
||||||
|
*targetstart = 0;
|
||||||
|
std::wstring resultstring(widestringnative);
|
||||||
|
delete [] widestringnative;
|
||||||
|
return resultstring;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
throw std::exception();
|
||||||
|
}
|
||||||
|
return L"";
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string ToUtf8(const std::wstring& widestring)
|
||||||
|
{
|
||||||
|
size_t widesize = widestring.length();
|
||||||
|
|
||||||
|
if (sizeof(wchar_t) == 2)
|
||||||
|
{
|
||||||
|
size_t utf8size = 3 * widesize + 1;
|
||||||
|
char* utf8stringnative = new char[utf8size];
|
||||||
|
const UTF16* sourcestart = reinterpret_cast<const UTF16*>(widestring.c_str());
|
||||||
|
const UTF16* sourceend = sourcestart + widesize;
|
||||||
|
UTF8* targetstart = reinterpret_cast<UTF8*>(utf8stringnative);
|
||||||
|
UTF8* targetend = targetstart + utf8size;
|
||||||
|
ConversionResult res = ConvertUTF16toUTF8(&sourcestart, sourceend, &targetstart, targetend, strictConversion);
|
||||||
|
if (res != conversionOK)
|
||||||
|
{
|
||||||
|
delete [] utf8stringnative;
|
||||||
|
throw std::exception();
|
||||||
|
}
|
||||||
|
*targetstart = 0;
|
||||||
|
std::string resultstring(utf8stringnative);
|
||||||
|
delete [] utf8stringnative;
|
||||||
|
return resultstring;
|
||||||
|
}
|
||||||
|
else if (sizeof(wchar_t) == 4)
|
||||||
|
{
|
||||||
|
size_t utf8size = 4 * widesize + 1;
|
||||||
|
char* utf8stringnative = new char[utf8size];
|
||||||
|
const UTF32* sourcestart = reinterpret_cast<const UTF32*>(widestring.c_str());
|
||||||
|
const UTF32* sourceend = sourcestart + widesize;
|
||||||
|
UTF8* targetstart = reinterpret_cast<UTF8*>(utf8stringnative);
|
||||||
|
UTF8* targetend = targetstart + utf8size;
|
||||||
|
ConversionResult res = ConvertUTF32toUTF8(&sourcestart, sourceend, &targetstart, targetend, strictConversion);
|
||||||
|
if (res != conversionOK)
|
||||||
|
{
|
||||||
|
delete [] utf8stringnative;
|
||||||
|
throw std::exception();
|
||||||
|
}
|
||||||
|
*targetstart = 0;
|
||||||
|
std::string resultstring(utf8stringnative);
|
||||||
|
delete [] utf8stringnative;
|
||||||
|
return resultstring;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
throw std::exception();
|
||||||
|
}
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//convert a std::string to std::wstring
|
||||||
|
std::wstring mbstowcs(std::string str)
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
return UtfConverter::FromUtf8(str);
|
||||||
|
} catch(std::exception) {
|
||||||
|
return L"(failed UTF-8 conversion)";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string wcstombs(std::wstring str)
|
||||||
|
{
|
||||||
|
return UtfConverter::ToUtf8(str);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//TODO - dont we already have another function that can do this
|
||||||
|
std::string getExtension(const char* input) {
|
||||||
|
char buf[1024];
|
||||||
|
strcpy(buf,input);
|
||||||
|
char* dot=strrchr(buf,'.');
|
||||||
|
if(!dot)
|
||||||
|
return "";
|
||||||
|
char ext [512];
|
||||||
|
strcpy(ext, dot+1);
|
||||||
|
int k, extlen=strlen(ext);
|
||||||
|
for(k=0;k<extlen;k++)
|
||||||
|
ext[k]=tolower(ext[k]);
|
||||||
|
return ext;
|
||||||
|
}
|
||||||
|
|
|
@ -0,0 +1,113 @@
|
||||||
|
//taken from fceux on 10/27/08
|
||||||
|
|
||||||
|
#ifndef _STRINGUTIL_H_
|
||||||
|
#define _STRINGUTIL_H_
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <vector>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
#include "types.h"
|
||||||
|
|
||||||
|
|
||||||
|
//definitions for str_strip() flags
|
||||||
|
#define STRIP_SP 0x01 // space
|
||||||
|
#define STRIP_TAB 0x02 // tab
|
||||||
|
#define STRIP_CR 0x04 // carriage return
|
||||||
|
#define STRIP_LF 0x08 // line feed
|
||||||
|
|
||||||
|
|
||||||
|
int str_ucase(char *str);
|
||||||
|
int str_lcase(char *str);
|
||||||
|
int str_ltrim(char *str, int flags);
|
||||||
|
int str_rtrim(char *str, int flags);
|
||||||
|
int str_strip(char *str, int flags);
|
||||||
|
int chr_replace(char *str, char search, char replace);
|
||||||
|
int str_replace(char *str, char *search, char *replace);
|
||||||
|
|
||||||
|
int HexStringToBytesLength(const std::string& str);
|
||||||
|
int Base64StringToBytesLength(const std::string& str);
|
||||||
|
std::string BytesToString(const void* data, int len);
|
||||||
|
bool StringToBytes(const std::string& str, void* data, int len);
|
||||||
|
|
||||||
|
std::vector<std::string> tokenize_str(const std::string & str,const std::string & delims);
|
||||||
|
void splitpath(const char* path, char* drv, char* dir, char* name, char* ext);
|
||||||
|
|
||||||
|
uint16 FastStrToU16(char* s, bool& valid);
|
||||||
|
char *U16ToDecStr(uint16 a);
|
||||||
|
char *U32ToDecStr(uint32 a);
|
||||||
|
char *U32ToDecStr(char* buf, uint32 a);
|
||||||
|
char *U8ToDecStr(uint8 a);
|
||||||
|
char *U8ToHexStr(uint8 a);
|
||||||
|
char *U16ToHexStr(uint16 a);
|
||||||
|
|
||||||
|
std::string stditoa(int n);
|
||||||
|
|
||||||
|
std::string readNullTerminatedAscii(std::istream* is);
|
||||||
|
|
||||||
|
//extracts a decimal uint from an istream
|
||||||
|
template<typename T> T templateIntegerDecFromIstream(std::istream* is)
|
||||||
|
{
|
||||||
|
unsigned int ret = 0;
|
||||||
|
bool pre = true;
|
||||||
|
|
||||||
|
for(;;)
|
||||||
|
{
|
||||||
|
int c = is->get();
|
||||||
|
if(c == -1) return ret;
|
||||||
|
int d = c - '0';
|
||||||
|
if((d<0 || d>9))
|
||||||
|
{
|
||||||
|
if(!pre)
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
pre = false;
|
||||||
|
ret *= 10;
|
||||||
|
ret += d;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
is->unget();
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline u32 u32DecFromIstream(std::istream* is) { return templateIntegerDecFromIstream<u32>(is); }
|
||||||
|
inline u64 u64DecFromIstream(std::istream* is) { return templateIntegerDecFromIstream<u64>(is); }
|
||||||
|
|
||||||
|
//puts an optionally 0-padded decimal integer of type T into the ostream (0-padding is quicker)
|
||||||
|
template<typename T, int DIGITS, bool PAD> void putdec(std::ostream* os, T dec)
|
||||||
|
{
|
||||||
|
char temp[DIGITS];
|
||||||
|
int ctr = 0;
|
||||||
|
for(int i=0;i<DIGITS;i++)
|
||||||
|
{
|
||||||
|
int quot = dec/10;
|
||||||
|
int rem = dec%10;
|
||||||
|
temp[DIGITS-1-i] = '0' + rem;
|
||||||
|
if(!PAD)
|
||||||
|
{
|
||||||
|
if(rem != 0) ctr = i;
|
||||||
|
}
|
||||||
|
dec = quot;
|
||||||
|
}
|
||||||
|
if(!PAD)
|
||||||
|
os->write(temp+DIGITS-ctr-1,ctr+1);
|
||||||
|
else
|
||||||
|
os->write(temp,DIGITS);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string mass_replace(const std::string &source, const std::string &victim, const std::string &replacement);
|
||||||
|
|
||||||
|
std::wstring mbstowcs(std::string str);
|
||||||
|
std::string wcstombs(std::wstring str);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
//TODO - dont we already have another function that can do this
|
||||||
|
std::string getExtension(const char* input);
|
||||||
|
|
||||||
|
|
||||||
|
#endif
|
|
@ -58,9 +58,11 @@
|
||||||
ExceptionHandling="1"
|
ExceptionHandling="1"
|
||||||
BufferSecurityCheck="false"
|
BufferSecurityCheck="false"
|
||||||
EnableEnhancedInstructionSet="0"
|
EnableEnhancedInstructionSet="0"
|
||||||
|
WarningLevel="1"
|
||||||
DebugInformationFormat="4"
|
DebugInformationFormat="4"
|
||||||
CallingConvention="0"
|
CallingConvention="0"
|
||||||
CompileAs="0"
|
CompileAs="0"
|
||||||
|
DisableSpecificWarnings="4244"
|
||||||
/>
|
/>
|
||||||
<Tool
|
<Tool
|
||||||
Name="VCManagedResourceCompilerTool"
|
Name="VCManagedResourceCompilerTool"
|
||||||
|
@ -740,6 +742,46 @@
|
||||||
>
|
>
|
||||||
</File>
|
</File>
|
||||||
</Filter>
|
</Filter>
|
||||||
|
<Filter
|
||||||
|
Name="utils"
|
||||||
|
>
|
||||||
|
<File
|
||||||
|
RelativePath="..\utils\ConvertUTF.c"
|
||||||
|
>
|
||||||
|
</File>
|
||||||
|
<File
|
||||||
|
RelativePath="..\utils\ConvertUTF.h"
|
||||||
|
>
|
||||||
|
</File>
|
||||||
|
<File
|
||||||
|
RelativePath="..\utils\guid.cpp"
|
||||||
|
>
|
||||||
|
</File>
|
||||||
|
<File
|
||||||
|
RelativePath="..\utils\guid.h"
|
||||||
|
>
|
||||||
|
</File>
|
||||||
|
<File
|
||||||
|
RelativePath="..\utils\md5.cpp"
|
||||||
|
>
|
||||||
|
</File>
|
||||||
|
<File
|
||||||
|
RelativePath="..\utils\md5.h"
|
||||||
|
>
|
||||||
|
</File>
|
||||||
|
<File
|
||||||
|
RelativePath="..\utils\valuearray.h"
|
||||||
|
>
|
||||||
|
</File>
|
||||||
|
<File
|
||||||
|
RelativePath="..\utils\xstring.cpp"
|
||||||
|
>
|
||||||
|
</File>
|
||||||
|
<File
|
||||||
|
RelativePath="..\utils\xstring.h"
|
||||||
|
>
|
||||||
|
</File>
|
||||||
|
</Filter>
|
||||||
<File
|
<File
|
||||||
RelativePath="..\ARM9.h"
|
RelativePath="..\ARM9.h"
|
||||||
>
|
>
|
||||||
|
@ -1011,6 +1053,14 @@
|
||||||
RelativePath="..\MMU.h"
|
RelativePath="..\MMU.h"
|
||||||
>
|
>
|
||||||
</File>
|
</File>
|
||||||
|
<File
|
||||||
|
RelativePath="..\movie.cpp"
|
||||||
|
>
|
||||||
|
</File>
|
||||||
|
<File
|
||||||
|
RelativePath="..\movie.h"
|
||||||
|
>
|
||||||
|
</File>
|
||||||
<File
|
<File
|
||||||
RelativePath="..\NDSSystem.cpp"
|
RelativePath="..\NDSSystem.cpp"
|
||||||
>
|
>
|
||||||
|
|
|
@ -29,6 +29,7 @@
|
||||||
#include "..\MMU.h"
|
#include "..\MMU.h"
|
||||||
#include "..\common.h"
|
#include "..\common.h"
|
||||||
#include "resource.h"
|
#include "resource.h"
|
||||||
|
#include "NDSSystem.h"
|
||||||
|
|
||||||
// ==================================================== emu input
|
// ==================================================== emu input
|
||||||
// =======================================================================
|
// =======================================================================
|
||||||
|
@ -174,6 +175,21 @@ const char *DIkeysNames[0xEF] =
|
||||||
};
|
};
|
||||||
const char *DIJoyNames[0x04] = { "JUp", "JDown", "JLeft", "JRight" };
|
const char *DIJoyNames[0x04] = { "JUp", "JDown", "JLeft", "JRight" };
|
||||||
|
|
||||||
|
#define KEY_A 0
|
||||||
|
#define KEY_B 1
|
||||||
|
#define KEY_SELECT 2
|
||||||
|
#define KEY_START 3
|
||||||
|
#define KEY_RIGHT 4
|
||||||
|
#define KEY_LEFT 5
|
||||||
|
#define KEY_UP 6
|
||||||
|
#define KEY_DOWN 7
|
||||||
|
#define KEY_R 8
|
||||||
|
#define KEY_L 9
|
||||||
|
#define KEY_X 10
|
||||||
|
#define KEY_Y 11
|
||||||
|
#define KEY_DEBUG 12
|
||||||
|
|
||||||
|
|
||||||
char *keyPadNames [MAXKEYPAD] = { "A", "B", "SELECT", "START",
|
char *keyPadNames [MAXKEYPAD] = { "A", "B", "SELECT", "START",
|
||||||
"RIGHT", "LEFT", "UP", "DOWN",
|
"RIGHT", "LEFT", "UP", "DOWN",
|
||||||
"R", "L", "X", "Y", "DEBUG", "FOLD", "POWER" };
|
"R", "L", "X", "Y", "DEBUG", "FOLD", "POWER" };
|
||||||
|
@ -386,30 +402,21 @@ void NDS_inputPost(BOOL paused, LPSTR buf)
|
||||||
{
|
{
|
||||||
if (paused) return;
|
if (paused) return;
|
||||||
|
|
||||||
u16 pad = (0 |
|
bool R = (buf[keyPad[KEY_RIGHT]] & 0x80)!=0;
|
||||||
((~buf[keyPad[0]] & 0x80) >> 7) |
|
bool L = (buf[keyPad[KEY_LEFT]] & 0x80)!=0;
|
||||||
((~buf[keyPad[1]] & 0x80) >> 6) |
|
bool D = (buf[keyPad[KEY_DOWN]] & 0x80)!=0;
|
||||||
((~buf[keyPad[2]] & 0x80) >> 5) |
|
bool U = (buf[keyPad[KEY_UP]] & 0x80)!=0;
|
||||||
((~buf[keyPad[3]] & 0x80) >> 4) |
|
bool T = (buf[keyPad[KEY_START]] & 0x80)!=0;
|
||||||
((~buf[keyPad[4]] & 0x80) >> 3) |
|
bool S = (buf[keyPad[KEY_SELECT]] & 0x80)!=0;
|
||||||
((~buf[keyPad[5]] & 0x80) >> 2) |
|
bool B = (buf[keyPad[KEY_B]] & 0x80)!=0;
|
||||||
((~buf[keyPad[6]] & 0x80) >> 1) |
|
bool A = (buf[keyPad[KEY_A]] & 0x80)!=0;
|
||||||
((~buf[keyPad[7]] & 0x80)) |
|
bool Y = (buf[keyPad[KEY_Y]] & 0x80)!=0;
|
||||||
((~buf[keyPad[8]] & 0x80) << 1) |
|
bool X = (buf[keyPad[KEY_X]] & 0x80)!=0;
|
||||||
((~buf[keyPad[9]] & 0x80) << 2)) ;
|
bool W = (buf[keyPad[KEY_L]] & 0x80)!=0;
|
||||||
|
bool E = (buf[keyPad[KEY_R]] & 0x80)!=0;
|
||||||
|
bool G = (buf[keyPad[KEY_DEBUG]] & 0x80)!=0;
|
||||||
|
|
||||||
((u16 *)ARM9Mem.ARM9_REG)[0x130>>1] = (u16)pad;
|
NDS_setPad( R, L, D, U, T, S, B, A, Y, X, W, E, G);
|
||||||
((u16 *)MMU.ARM7_REG)[0x130>>1] = (u16)pad;
|
|
||||||
|
|
||||||
u16 padExt = (((u16 *)MMU.ARM7_REG)[0x136>>1] & 0x00F0) |
|
|
||||||
((~buf[keyPad[10]] & 0x80) >> 7) |
|
|
||||||
((~buf[keyPad[11]] & 0x80) >> 6) |
|
|
||||||
((~buf[keyPad[12]] & 0x80) >> 4) |
|
|
||||||
0x0034;
|
|
||||||
|
|
||||||
((u16 *)MMU.ARM7_REG)[0x136>>1] = (u16)padExt;
|
|
||||||
|
|
||||||
// TODO: low power IRQ
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO
|
// TODO
|
||||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -31,6 +31,7 @@ const char* __stdcall DXGetErrorDescription8A(HRESULT hr);
|
||||||
#include "SPU.h"
|
#include "SPU.h"
|
||||||
#include "snddx.h"
|
#include "snddx.h"
|
||||||
#include "CWindow.h"
|
#include "CWindow.h"
|
||||||
|
#include "windriver.h"
|
||||||
|
|
||||||
int SNDDXInit(int buffersize);
|
int SNDDXInit(int buffersize);
|
||||||
void SNDDXDeInit();
|
void SNDDXDeInit();
|
||||||
|
@ -71,11 +72,14 @@ static volatile bool terminated;
|
||||||
extern CRITICAL_SECTION win_sync;
|
extern CRITICAL_SECTION win_sync;
|
||||||
extern volatile int win_sound_samplecounter;
|
extern volatile int win_sound_samplecounter;
|
||||||
|
|
||||||
DWORD WINAPI SNDDXThread( LPVOID lpParameter)
|
DWORD WINAPI SNDDXThread( LPVOID )
|
||||||
{
|
{
|
||||||
for(;;) {
|
for(;;) {
|
||||||
if(terminate) break;
|
if(terminate) break;
|
||||||
SPU_Emulate_user();
|
{
|
||||||
|
Lock lock;
|
||||||
|
SPU_Emulate_user();
|
||||||
|
}
|
||||||
Sleep(10);
|
Sleep(10);
|
||||||
}
|
}
|
||||||
terminated = true;
|
terminated = true;
|
||||||
|
@ -229,10 +233,13 @@ void SNDDXUpdateAudio(s16 *buffer, u32 num_samples)
|
||||||
DWORD buffer1_size, buffer2_size;
|
DWORD buffer1_size, buffer2_size;
|
||||||
DWORD status;
|
DWORD status;
|
||||||
|
|
||||||
EnterCriticalSection(&win_sync);
|
int samplecounter;
|
||||||
int samplecounter = win_sound_samplecounter -= num_samples;
|
{
|
||||||
LeaveCriticalSection(&win_sync);
|
Lock lock;
|
||||||
bool silence = (samplecounter<-44100*15/60); //behind by more than a quarter second -> silence
|
samplecounter = win_sound_samplecounter -= num_samples;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool silence = (samplecounter<-44100*15/60); //behind by more than a quarter second -> silence
|
||||||
|
|
||||||
IDirectSoundBuffer8_GetStatus(lpDSB2, &status);
|
IDirectSoundBuffer8_GetStatus(lpDSB2, &status);
|
||||||
|
|
||||||
|
|
|
@ -7,4 +7,10 @@
|
||||||
|
|
||||||
extern WINCLASS *MainWindow;
|
extern WINCLASS *MainWindow;
|
||||||
|
|
||||||
|
class Lock {
|
||||||
|
public:
|
||||||
|
Lock();
|
||||||
|
~Lock();
|
||||||
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
Loading…
Reference in New Issue