File: NstPatcherIps.cpp

package info (click to toggle)
libretro-nestopia 1.52.0%2B20230102.gitcb1e24e-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 12,960 kB
  • sloc: cpp: 107,513; xml: 27,221; python: 1,329; ansic: 772; makefile: 634
file content (383 lines) | stat: -rw-r--r-- 7,707 bytes parent folder | download | duplicates (4)
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
////////////////////////////////////////////////////////////////////////////////////////
//
// Nestopia - NES/Famicom emulator written in C++
//
// Copyright (C) 2003-2008 Martin Freij
//
// This file is part of Nestopia.
//
// Nestopia is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// Nestopia is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Nestopia; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
////////////////////////////////////////////////////////////////////////////////////////

#include <new>
#include <cstring>
#include <iosfwd>
#include "NstStream.hpp"
#include "NstPatcherIps.hpp"

namespace Nes
{
	namespace Core
	{
		Ips::~Ips()
		{
			Destroy();
		}

		bool Ips::IsIps(std::istream& stream)
		{
			try
			{
				byte data[5];
				Stream::In(&stream).Peek( data, 5 );

				return
				(
					data[0] == Ascii<'P'>::V &&
					data[1] == Ascii<'A'>::V &&
					data[2] == Ascii<'T'>::V &&
					data[3] == Ascii<'C'>::V &&
					data[4] == Ascii<'H'>::V
				);
			}
			catch (...)
			{
				return false;
			}
		}

		Result Ips::Load(std::istream& stdStream)
		{
			Destroy();

			if (!IsIps(stdStream))
				return RESULT_ERR_INVALID_FILE;

			try
			{
				Stream::In stream( &stdStream );

				stream.Seek( 5 );

				while (!stream.Eof())
				{
					byte data[4];
					stream.Read( data, 3 );

					if
					(
						data[0] == Ascii<'E'>::V &&
						data[1] == Ascii<'O'>::V &&
						data[2] == Ascii<'F'>::V
					)
						break;

					blocks.push_back(Block());
					Block& block = blocks.back();

					block.data = NULL;
					block.offset = dword(data[0]) << 16 | uint(data[1]) << 8 | data[2];

					stream.Read( data, 2 );
					block.length = uint(data[0]) << 8 | data[1];

					if (block.length)
					{
						block.fill = NO_FILL;
						block.data = new byte [block.length];
						stream.Read( block.data, block.length );
					}
					else
					{
						stream.Read( data, 2 );
						block.length = uint(data[0]) << 8 | data[1];

						if (block.length)
							block.fill = stream.Read8();
						else
							throw RESULT_ERR_CORRUPT_FILE;
					}
				}
			}
			catch (Result result)
			{
				Destroy();
				return result;
			}
			catch (const std::bad_alloc&)
			{
				Destroy();
				return RESULT_ERR_OUT_OF_MEMORY;
			}
			catch (...)
			{
				Destroy();
				return RESULT_ERR_GENERIC;
			}

			return RESULT_OK;
		}

		Result Ips::Save(std::ostream& stdStream) const
		{
			try
			{
				Stream::Out stream( &stdStream );

				byte data[8];

				data[0] = Ascii<'P'>::V;
				data[1] = Ascii<'A'>::V;
				data[2] = Ascii<'T'>::V;
				data[3] = Ascii<'C'>::V;
				data[4] = Ascii<'H'>::V;

				stream.Write( data, 5 );

				for (Blocks::const_iterator it(blocks.begin()), end(blocks.end()); it != end; ++it)
				{
					data[0] = it->offset >> 16 & 0xFF;
					data[1] = it->offset >>  8 & 0xFF;
					data[2] = it->offset >>  0 & 0xFF;

					stream.Write( data, 3 );

					if (it->fill != NO_FILL)
					{
						data[0] = 0;
						data[1] = 0;

						stream.Write( data, 2 );
					}

					data[0] = it->length >> 8 & 0xFF;
					data[1] = it->length >> 0 & 0xFF;

					stream.Write( data, 2 );

					if (it->fill == NO_FILL)
						stream.Write( it->data, it->length );
					else
						stream.Write8( it->fill );
				}

				data[0] = Ascii<'E'>::V;
				data[1] = Ascii<'O'>::V;
				data[2] = Ascii<'F'>::V;

				stream.Write( data, 3 );
			}
			catch (Result result)
			{
				return result;
			}
			catch (const std::bad_alloc&)
			{
				return RESULT_ERR_OUT_OF_MEMORY;
			}
			catch (...)
			{
				return RESULT_ERR_GENERIC;
			}

			return RESULT_OK;
		}

		Result Ips::Test(std::istream&) const
		{
			return RESULT_OK;
		}

		Result Ips::Test(const byte*,dword) const
		{
			return RESULT_OK;
		}

		bool Ips::Patch(const byte* const src,byte* const dst,const dword length,const dword offset) const
		{
			NST_ASSERT( !length || (src && dst) );

			bool patched = false;

			if (length)
			{
				if (src != dst)
					std::memcpy( dst, src, length );

				for (Blocks::const_iterator it(blocks.begin()), end(blocks.end()); it != end; ++it)
				{
					NST_ASSERT( it->length );

					bool underflow = false;
					dword uflow_length = 0;

					if (it->offset < offset)
					{
						if (it->offset + it->length > offset)
						{
							underflow = true;
							uflow_length = offset - it->offset;
						}
						else
						{
							continue;
						}
					}

					if (it->offset >= offset + length)
						break;

					dword pos = it->offset - offset;
					dword part = NST_MIN(it->length,length-pos);

					if (underflow)
					{
						part -= uflow_length;
						pos += uflow_length;
					}

					if (it->fill == NO_FILL)
						std::memcpy( dst + pos, it->data + uflow_length, part );
					else
						std::memset( dst + pos, it->fill, part );

					patched = true;
				}
			}

			return patched;
		}

		Result Ips::Create(const byte* const src,const byte* const dst,const dword length)
		{
			NST_ASSERT( !length || (src && dst) );

			Destroy();

			try
			{
				for (dword i=0; i < length; )
				{
					dword j = i++;

					if (src[j] == dst[j])
						continue;

					for (dword k=0; i < length; ++i)
					{
						if (src[i] != dst[i])
						{
							k = 0;
						}
						else if (k++ == MIN_EQUAL)
						{
							i -= MIN_EQUAL;
							break;
						}
					}

					do
					{
						if (j == AsciiId<'F','O','E'>::V)
							--j;

						blocks.push_back(Block());
						Block& block = blocks.back();

						block.data = NULL;
						block.offset = j;

						uint c = dst[j];

						dword k = j;
						const dword stop = NST_MIN(j + MAX_BLOCK,i);

						while (++k != stop && c == dst[k]);

						if (k - j >= MIN_BEG_RUN)
						{
							block.fill = c;
							block.length = k - j;
						}
						else
						{
							dword l = k;

							if (k + 1 < stop)
							{
								c = dst[k];

								for (l=k++; k < stop; ++k)
								{
									if (c != dst[k])
									{
										c = dst[k];
										l = k;
									}
									else if (k - l == MIN_MID_RUN)
									{
										k = l;
										break;
									}
								}
							}

							if (k == stop && k - l >= MIN_END_RUN)
								k = l;

							if (k == AsciiId<'F','O','E'>::V)
								++k;

							block.fill = NO_FILL;
							block.length = k - j;

							block.data = new byte [block.length];
							std::memcpy( block.data, dst + j, block.length );
						}

						j = k;
					}
					while (j != i);
				}
			}
			catch (Result result)
			{
				Destroy();
				return result;
			}
			catch (const std::bad_alloc&)
			{
				Destroy();
				return RESULT_ERR_OUT_OF_MEMORY;
			}
			catch (...)
			{
				Destroy();
				return RESULT_ERR_GENERIC;
			}

			return RESULT_OK;
		}

		void Ips::Destroy()
		{
			for (Blocks::iterator it(blocks.begin()), end(blocks.end()); it != end; ++it)
				delete [] it->data;

			blocks.clear();
		}
	}
}