3 #include "serialization.hpp"
12 uint8_t readbyte(const char* buf
, uint64_t& pos
, uint64_t size
)
15 (stringfmt() << "Attempted to read byte past the end of patch (" << pos
<< " >= "
16 << size
<< ").").throwex();
17 return static_cast<uint8_t>(buf
[pos
++]);
20 struct ips_patcher
: public rom_patcher
22 ~ips_patcher() throw();
23 bool identify(const std::vector
<char>& patch
) throw();
24 void dopatch(std::vector
<char>& out
, const std::vector
<char>& original
,
25 const std::vector
<char>& patch
, int32_t offset
) throw(std::bad_alloc
, std::runtime_error
);
28 ips_patcher::~ips_patcher() throw()
32 bool ips_patcher::identify(const std::vector
<char>& patch
) throw()
34 return (patch
.size() > 5 && patch
[0] == 'P' && patch
[1] == 'A' && patch
[2] == 'T' && patch
[3] == 'C' &&
38 void ips_patcher::dopatch(std::vector
<char>& out
, const std::vector
<char>& original
,
39 const std::vector
<char>& patch
, int32_t offset
) throw(std::bad_alloc
, std::runtime_error
)
43 const char* _patch
= &patch
[0];
44 size_t psize
= patch
.size();
50 uint32_t off
= 0, l
= 0;
51 off
|= static_cast<uint32_t>(readbyte(_patch
, ioffset
, psize
)) << 16;
52 off
|= static_cast<uint32_t>(readbyte(_patch
, ioffset
, psize
)) << 8;
53 off
|= static_cast<uint32_t>(readbyte(_patch
, ioffset
, psize
));
56 l
|= static_cast<uint32_t>(readbyte(_patch
, ioffset
, psize
)) << 8;
57 l
|= static_cast<uint32_t>(readbyte(_patch
, ioffset
, psize
));
60 l
|= static_cast<uint32_t>(readbyte(_patch
, ioffset
, psize
)) << 8;
61 l
|= static_cast<uint32_t>(readbyte(_patch
, ioffset
, psize
));
62 b
= readbyte(_patch
, ioffset
, psize
);
69 uint32_t noffset
= static_cast<uint32_t>(-offset
);
70 uint32_t fromoff
= min(noffset
, off
);
72 extra
= min(noffset
- fromoff
, l
);
75 if(off
+ l
>= out
.size())
79 for(uint64_t i
= 0; i
< l
; i
++)
80 out
[off
+ i
] = readbyte(_patch
, ioffset
, psize
);
82 for(uint64_t i
= 0; i
< l
; i
++)