Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
Libretro
libretro-chailove
Commits
1b90e88d
Unverified
Commit
1b90e88d
authored
Sep 02, 2018
by
RobLoach
Committed by
GitHub
Sep 02, 2018
Browse files
Add love.data.encode() (#278)
* Add cppcodec * Add love.data.encode(base64) * Add love.data.encode(hex)
parent
d6ca07eb
Changes
8
Hide whitespace changes
Inline
Side-by-side
.gitmodules
View file @
1b90e88d
...
...
@@ -73,3 +73,8 @@
url = https://github.com/Didstopia/physfs.git
ignore = dirty
branch = master
[submodule "vendor/cppcodec"]
path = vendor/cppcodec
url = https://github.com/tplgy/cppcodec.git
ignore = dirty
branch = master
Makefile.common
View file @
1b90e88d
...
...
@@ -147,6 +147,9 @@ SOURCES_C += $(CORE_DIR)/vendor/libretro-common/utils/md5.c
# TinySHA1
FLAGS
+=
-I
$(CORE_DIR)
/vendor/TinySHA1
# cppcodec
FLAGS
+=
-I
$(CORE_DIR)
/vendor/cppcodec
# ChaiScript
ifeq
($(HAVE_CHAISCRIPT),)
FLAGS
+=
-I
$(CORE_DIR)
/vendor/chaiscript/include
...
...
src/libretro.cpp
View file @
1b90e88d
...
...
@@ -257,7 +257,8 @@ bool retro_serialize(void *data, size_t size) {
return
false
;
}
// Compress the JSON state data.
// Encode the JSON state data.
// state = app->data.encode("string", "base64", state);
state
=
app
->
data
.
compress
(
state
);
// Save the information to the state data.
...
...
@@ -269,7 +270,7 @@ bool retro_serialize(void *data, size_t size) {
* libretro callback; Unserialize the given data and load the state.
*/
bool
retro_unserialize
(
const
void
*
data
,
size_t
size
)
{
if
(
!
ChaiLove
::
hasInstance
())
{
if
(
!
ChaiLove
::
hasInstance
()
||
size
<=
0
)
{
return
false
;
}
std
::
cout
<<
"[ChaiLove] retro_unserialize"
<<
std
::
endl
;
...
...
@@ -286,6 +287,7 @@ bool retro_unserialize(const void *data, size_t size) {
ChaiLove
*
app
=
ChaiLove
::
getInstance
();
// Decompress the state data.
// loadData = app->data.decode("string", "base64", loadData);
loadData
=
app
->
data
.
decompress
(
loadData
);
// Finally, load the string.
...
...
src/love/data.cpp
View file @
1b90e88d
...
...
@@ -5,9 +5,12 @@
#include <string>
#include <sstream>
#include <iostream>
#include <vector>
#include "compat/zlib.h"
#include "utils/md5.h"
#include "TinySHA1.hpp"
#include <cppcodec/base64_default_rfc4648.hpp>
#include <cppcodec/hex_default_lower.hpp>
namespace
love
{
...
...
@@ -93,6 +96,38 @@ std::string data::decompress(const std::string& str) {
return
outstring
;
}
std
::
string
data
::
encode
(
const
std
::
string
&
containerType
,
const
std
::
string
&
format
,
const
std
::
string
&
sourceString
)
{
if
(
format
==
"base64"
)
{
std
::
string
encoded
=
base64
::
encode
(
sourceString
);
return
encoded
;
}
if
(
format
==
"hex"
)
{
std
::
string
encoded
=
hex
::
encode
(
sourceString
);
return
encoded
;
}
std
::
cout
<<
"[ChaiLove] Warning: love.data.encode format not found: "
<<
format
<<
"."
<<
std
::
endl
;
return
sourceString
;
}
std
::
string
data
::
decode
(
const
std
::
string
&
containerType
,
const
std
::
string
&
format
,
const
std
::
string
&
sourceString
)
{
if
(
format
==
"base64"
)
{
std
::
vector
<
unsigned
char
>
decodedVector
=
base64
::
decode
(
sourceString
);
std
::
string
decoded
(
decodedVector
.
begin
(),
decodedVector
.
end
());
return
decoded
;
}
if
(
format
==
"hex"
)
{
std
::
vector
<
unsigned
char
>
decodedVector
=
hex
::
decode
(
sourceString
);
std
::
string
decoded
(
decodedVector
.
begin
(),
decodedVector
.
end
());
return
decoded
;
}
std
::
cout
<<
"[ChaiLove] Warning: love.data.decode format not found: "
<<
format
<<
"."
<<
std
::
endl
;
return
sourceString
;
}
std
::
string
data
::
hash
(
const
std
::
string
&
hashFunction
,
const
std
::
string
&
data
)
{
if
(
hashFunction
==
"md5"
)
{
return
hash_md5
(
data
);
...
...
src/love/data.h
View file @
1b90e88d
...
...
@@ -41,6 +41,32 @@ class data {
*/
std
::
string
decompress
(
const
std
::
string
&
str
);
/**
* Encode Data or a string to a Data or string in one of the EncodeFormats.
*
* @param containerType What type to return the encoded data as. Only supports "string" currently.
* @param format The format of the output data. Can be "base64" or "hex".
* @param sourceString The raw data to encode.
*
* @return String which contains the encoded version of source.
*
* @see love.data.decode
*/
std
::
string
encode
(
const
std
::
string
&
containerType
,
const
std
::
string
&
format
,
const
std
::
string
&
sourceString
);
/**
* Decode Data or a string to a Data or string in one of the EncodeFormats.
*
* @param containerType What type to return the decoded data as. Only supports "string" currently.
* @param format The format of the input data. Can be "base64" or "hex".
* @param sourceString The raw (encoded) data to decode.
*
* @return String which contains the decoded version of source.
*
* @see love.data.encode
*/
std
::
string
decode
(
const
std
::
string
&
containerType
,
const
std
::
string
&
format
,
const
std
::
string
&
sourceString
);
/**
* Compute the message digest of specified string with specified algorithm.
*
...
...
src/love/script.cpp
View file @
1b90e88d
...
...
@@ -344,6 +344,8 @@ script::script(const std::string& file) {
chai
.
add
(
fun
<
std
::
string
,
data
,
const
std
::
string
&
,
int
>
(
&
data
::
compress
),
"compress"
);
chai
.
add
(
fun
(
&
data
::
decompress
),
"decompress"
);
chai
.
add
(
fun
(
&
data
::
hash
),
"hash"
);
chai
.
add
(
fun
(
&
data
::
encode
),
"encode"
);
chai
.
add
(
fun
(
&
data
::
decode
),
"decode"
);
// Ensure the love namespace is imported and ready.
chai
.
import
(
"love"
);
...
...
test/unittests/data.chai
View file @
1b90e88d
...
...
@@ -14,3 +14,23 @@ assert_equal(md5Hash, "b10a8db164e0754105b7a99be72e3fe5", "love.data.hash('md5')
// hash(sha1)
var sha1Hash = love.data.hash("sha1", "Hello World")
assert_equal(sha1Hash, "0a4d55a8d778e5022fab701977c5d840bbc486d0", "love.data.hash('sha1')")
// encode(base64)
var decodedString = "any carnal pleasure"
var encodedString = "YW55IGNhcm5hbCBwbGVhc3VyZQ=="
var encodeResult = love.data.encode("string", "base64", decodedString)
assert_equal(encodeResult, encodedString, "love.data.encode('base64')")
// decode(base64)
var decodeResult = love.data.decode("string", "base64", encodedString)
assert_equal(decodeResult, decodedString, "love.data.decode('base64')")
// encode(hex)
var dehexedString = "Hello World"
var hexedString = "48656c6c6f20576f726c64"
var hexResult = love.data.encode("string", "hex", dehexedString)
assert_equal(hexResult, hexedString, "love.data.encode('hex')")
// decode(hex)
var dedexResult = love.data.decode("string", "hex", hexedString)
assert_equal(dedexResult, dehexedString, "love.data.decode('hex')")
cppcodec
@
302dc28f
Subproject commit 302dc28f8fd5c8bf2ea8d7212aed3be884d5d166
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment