Lua: memory.hash_region

This commit is contained in:
Ilari Liusvaara 2012-05-10 15:28:54 +03:00
parent 2327483fb1
commit b8c95999cb
3 changed files with 38 additions and 0 deletions

View file

@ -3407,6 +3407,15 @@ Writes the specified value (negative values undergo 2's complement) to specified
address (as a quadword).
\end_layout
\begin_layout Subsubsection
memory.hash_region(number base, number size)
\end_layout
\begin_layout Standard
Hash specified number of bytes starting from specified address and return
the SHA-256.
\end_layout
\begin_layout Subsection
Table _SYSTEM
\end_layout

View file

@ -1719,6 +1719,11 @@ result.
Writes the specified value (negative values undergo 2's
complement) to specified address (as a quadword).
8.8.16 memory.hash_region(number base, number size)
Hash specified number of bytes starting from specified address
and return the SHA-256.
8.9 Table _SYSTEM
Contains copy of global variables from time of Lua

View file

@ -1,6 +1,7 @@
#include "lua/internal.hpp"
#include "core/memorymanip.hpp"
#include "core/rom.hpp"
#include "library/sha256.hpp"
namespace
{
@ -95,6 +96,29 @@ namespace
return 1;
});
#define BLOCKSIZE 256
function_ptr_luafun hashmemory("memory.hash_region", [](lua_State* LS, const std::string& fname) -> int {
std::string hash;
uint32_t addr = get_numeric_argument<uint32_t>(LS, 1, fname.c_str());
uint32_t size = get_numeric_argument<uint32_t>(LS, 2, fname.c_str());
char buffer[BLOCKSIZE];
sha256 h;
while(size > BLOCKSIZE) {
for(size_t i = 0; i < BLOCKSIZE; i++)
buffer[i] = memory_read_byte(addr + i);
h.write(buffer, BLOCKSIZE);
addr += BLOCKSIZE;
size -= BLOCKSIZE;
}
for(size_t i = 0; i < size; i++)
buffer[i] = memory_read_byte(addr + i);
h.write(buffer, size);
hash = h.read();
lua_pushlstring(LS, hash.c_str(), 64);
return 1;
});
lua_read_memory<uint8_t, uint8_t, memory_read_byte> rub("memory.readbyte");
lua_read_memory<int8_t, uint8_t, memory_read_byte> rsb("memory.readsbyte");
lua_read_memory<uint16_t, uint16_t, memory_read_word> ruw("memory.readword");