-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmem_read.h
66 lines (60 loc) · 2.39 KB
/
mem_read.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#pragma once
/******************************************************************************
* Copyright © 2021 Komodo Core Deveelopers *
* *
* See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at *
* the top-level directory of this distribution for the individual copyright *
* holder information and the developer policies on copyright and licensing. *
* *
* Unless otherwise agreed in a custom licensing agreement, no part of the *
* SuperNET software, including this file may be copied, modified, propagated *
* or distributed except according to the terms contained in the LICENSE file *
* *
* Removal or modification of this copyright notice is prohibited. *
* *
******************************************************************************/
// #include "komodo_structs.h" // for parse_error
#include <cstdint>
#include <string>
#include "events-tests.h"
/***
* These read a chunk of memory with some built-in type safety
*/
template<class T>
std::size_t mem_read(T& dest, uint8_t *filedata, long &fpos, long datalen)
{
if (fpos + sizeof(T) <= datalen)
{
memcpy( &dest, &filedata[fpos], sizeof(T) );
fpos += sizeof(T);
return sizeof(T);
}
throw komodo::parse_error("Invalid size: " + std::to_string(sizeof(T)) );
}
template<class T, std::size_t N>
std::size_t mem_read(T(&dest)[N], uint8_t *filedata, long& fpos, long datalen)
{
std::size_t sz = sizeof(T) * N;
if (fpos + sz <= datalen)
{
memcpy( &dest, &filedata[fpos], sz );
fpos += sz;
return sz;
}
throw komodo::parse_error("Invalid size: " + std::to_string( sz ) );
}
/****
* Read a size that is less than the array length
*/
template<class T, std::size_t N>
std::size_t mem_nread(T(&dest)[N], size_t num_elements, uint8_t *filedata, long& fpos, long datalen)
{
std::size_t sz = sizeof(T) * num_elements;
if (fpos + sz <= datalen)
{
memcpy( &dest, &filedata[fpos], sz );
fpos += sz;
return sz;
}
throw komodo::parse_error("Invalid size: " + std::to_string(sz));
}