-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathSessions.cpp
More file actions
203 lines (160 loc) · 5.74 KB
/
Sessions.cpp
File metadata and controls
203 lines (160 loc) · 5.74 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/*
* Project 64 Legacy - A Nintendo 64 emulator.
*
* (c) Copyright 2023 parasyte (jay@kodewerx.org)
*
* Project64 Legacy Homepage: www.project64-legacy.com
*
* Permission to use, copy, modify and distribute Project64 in both binary and
* source form, for non-commercial purposes, is hereby granted without fee,
* providing that this license information and copyright notice appear with
* all copies and any derived work.
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event shall the authors be held liable for any damages
* arising from the use of this software.
*
* Project64 is freeware for PERSONAL USE only. Commercial users should
* seek permission of the copyright holders first. Commercial use includes
* charging money for Project64 or software derived from Project64.
*
* The copyright holders request that bug fixes and improvements to the code
* should be forwarded to them so if they want them.
*
*/
#include <windows.h>
#include <cstdint>
#include <string>
#include <vector>
#include "cbor-lite/codec.h"
using namespace CborLite;
extern "C" {
#include "Sessions.h"
}
// Constants
constexpr uint64_t SESSION_MEM_BOOKMARKS_VERSION = 1;
// Utility functions
std::string as_path(char *filename) {
auto filepath = std::string(filename);
// Support long file paths.
// SEE: https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-createfilea#parameters
if (filepath.substr(0, 4) != std::string(R"(\\?\)")) {
filepath.insert(0, R"(\\?\)");
}
return filepath;
}
bool read_cbor(std::vector<uint8_t> &buffer, char *filename) {
auto filepath = as_path(filename);
HANDLE file = CreateFile(filepath.c_str(), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (file == INVALID_HANDLE_VALUE) {
return false;
}
DWORD len = GetFileSize(file, NULL);
DWORD bytes_read = 0;
buffer.clear();
buffer.resize(len);
BOOL read_success = (ReadFile(file, buffer.data(), len, &bytes_read, NULL) == TRUE);
CloseHandle(file);
return read_success && bytes_read == len;
}
bool write_cbor(char *filename, std::vector<uint8_t> &buffer) {
auto filepath = as_path(filename);
HANDLE file = CreateFile(filepath.c_str(), GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (file == INVALID_HANDLE_VALUE) {
return false;
}
DWORD bytes_written;
BOOL write_success = (WriteFile(file, buffer.data(), buffer.size(), &bytes_written, NULL) == TRUE);
CloseHandle(file);
return write_success && bytes_written == buffer.size();
}
// C++ implementations
template <typename InputIterator>
size_t decode_mem_bookmark(InputIterator &pos, InputIterator &end, struct MEM_BOOKMARK &bookmark, Flags flags = Flag::none) {
uint64_t num_items = 0;
auto len = decodeArraySize(pos, end, num_items, flags);
if (num_items != 5) {
throw Exception("expected 4 items");
}
auto name = std::string();
len += decodeText(pos, end, name, flags);
// Truncate name to fit in the buffer.
strncpy(bookmark.name, name.c_str(), min(name.size(), sizeof(bookmark.name) - 1));
bookmark.name[sizeof(bookmark.name) - 1] = 0;
uint64_t width_required = 0;
len += decodeUnsigned(pos, end, width_required, flags);
// Truncate 64-bit to 32-bit.
bookmark.width_required = (unsigned int)width_required;
uint64_t selection_range = 0;
len += decodeUnsigned(pos, end, selection_range, flags);
bookmark.selection_range[0].UDW = selection_range;
len += decodeUnsigned(pos, end, selection_range, flags);
bookmark.selection_range[1].UDW = selection_range;
bool is_virtual = true;
len += decodeBool(pos, end, is_virtual, flags);
// Convert bool to int.
bookmark.is_virtual = is_virtual;
return len;
}
template <typename Buffer>
size_t encode_mem_bookmark(Buffer &buffer, const struct MEM_BOOKMARK &bookmark) {
auto len = encodeArraySize(buffer, 5u);
auto name = std::string(bookmark.name);
len += encodeText(buffer, name);
len += encodeUnsigned(buffer, bookmark.width_required);
len += encodeUnsigned(buffer, bookmark.selection_range[0].UDW);
len += encodeUnsigned(buffer, bookmark.selection_range[1].UDW);
bool is_virtual = bookmark.is_virtual;
len += encodeBool(buffer, is_virtual);
return len;
}
// C interfaces
BOOL Session_Load_MemBookmarks(unsigned int *num_bookmarks, struct MEM_BOOKMARK *bookmarks, unsigned int max_bookmarks, char *filename) {
try {
auto buffer = std::vector<uint8_t>();
if (!read_cbor(buffer, filename)) {
return FALSE;
}
auto pos = buffer.begin();
auto end = buffer.end();
// The version 1 schema looks like this:
// [1, ["name", width_required, selection_range_start, selection_range_end, is_virtual], ...]
//
// Example:
// [1,
// ["Off : Debug [0x800015C0]", 138, 2147489216, 2147489226, true],
// ["Play Track Options [0x80001584]", 173, 2147489156, 2147489173, true]
// ]
uint64_t num_items = 0;
decodeArraySize(pos, end, num_items);
if (num_items < 1 || num_items - 1 > max_bookmarks) {
return FALSE;
}
*num_bookmarks = (unsigned int)(num_items - 1);
uint64_t version = 0;
decodeUnsigned(pos, end, version);
if (version != SESSION_MEM_BOOKMARKS_VERSION) {
// NOTICE: Only version 1 is supported today.
return FALSE;
}
for (unsigned int i = 0; i < *num_bookmarks; i++) {
decode_mem_bookmark(pos, end, bookmarks[i]);
}
return TRUE;
} catch (Exception) {
return FALSE;
}
}
BOOL Session_Save_MemBookmarks(char *filename, struct MEM_BOOKMARK *bookmarks, unsigned int num_bookmarks) {
try {
auto buffer = std::vector<uint8_t>();
encodeArraySize(buffer, num_bookmarks + 1);
encodeUnsigned(buffer, SESSION_MEM_BOOKMARKS_VERSION);
for (unsigned int i = 0; i < num_bookmarks; i++) {
encode_mem_bookmark(buffer, bookmarks[i]);
}
return write_cbor(filename, buffer);
} catch (Exception) {
return FALSE;
}
}