wavelength 1.0.0
Loading...
Searching...
No Matches
codecheck.h
Go to the documentation of this file.
1#pragma once
2
3#ifdef VIDEO_STANDARD_PAL
4
5#include <memory>
6
7#include <libezctl.h>
8#include <sifrpc.h>
9
10#include "os/system.h"
11#include "script/dataarray.h"
12
13int main(int argc, char **argv);
14
27class CodeCheck {
28public:
34 static void Create() {
35 sStart = std::make_unique<const unsigned char *>();
36 sEnd = std::make_unique<const unsigned char *>();
37 sExpected = std::make_unique<int>();
38 sCursor = std::make_unique<const unsigned char *>();
39 sCrc = std::make_unique<unsigned int>();
40 }
41
43 static void Destroy() {
44 sStart.reset();
45 sEnd.reset();
46 sExpected.reset();
47 sCursor.reset();
48 sCrc.reset();
49 sTable.reset();
50 }
51
57 static void Poll() {
58 DataArray *pXs = SystemConfig()->FindArray("xs", false);
59 const int nMain = pXs->FindArray("main", false)->Int(1);
60 const int nOffset = pXs->FindArray("offset", false)->Int(1);
61 const int nSize = pXs->FindArray("size", false)->Int(1);
62 const int nCrc = pXs->FindArray("crc", false)->Int(1);
63 // Taking main()'s address is what the check is for.
64 const auto *pStart = reinterpret_cast<const unsigned char *>(&main) + (nOffset - nMain);
65 *sStart = pStart;
66 *sEnd = pStart + nSize;
67 *sExpected = nCrc;
68 if (sTableBuilt == 0) {
69 BuildTable();
70 sTableBuilt = 1;
71 *sCursor = pStart;
72 *sCrc = kCrcInitial;
73 }
74
75 const unsigned char *pCursor = *sCursor;
76 const unsigned char *pLimit = pCursor + kBytesPerPoll;
77 const unsigned char *pStop = *sEnd < pLimit ? *sEnd : pLimit;
78 unsigned int crc = *sCrc;
79 for (int nLeft = pStop - pCursor; nLeft-- != 0;) {
80 crc = sTable[(crc ^ *pCursor++) & kByteMask] ^ (crc >> kBitsPerByte);
81 }
82 *sCrc = crc;
83 *sCursor = pCursor;
84 if (pCursor != *sEnd) {
85 return;
86 }
87 if (~crc != static_cast<unsigned int>(*sExpected)) {
88 sceSifCallRpc(ezNetCtlGetClient(),
89 EZ_NETCTL_FUNCTION_SET_NET_CHECKED,
90 0,
91 nullptr,
92 0,
93 nullptr,
94 0,
95 nullptr,
96 nullptr);
97 }
98 *sCursor = *sStart;
99 *sCrc = kCrcInitial;
100 }
101
102private:
104 static std::unique_ptr<const unsigned char *> sStart;
105
107 static std::unique_ptr<const unsigned char *> sEnd;
108
110 static std::unique_ptr<const unsigned char *> sCursor;
111
113 static std::unique_ptr<unsigned int> sCrc;
114
116 static std::unique_ptr<int> sExpected;
117
119 static std::unique_ptr<unsigned int[]> sTable;
120
122 static int sTableBuilt;
123
124 static constexpr int kTableSize = 256;
125 static constexpr unsigned int kPolynomial = 0x04c11db7;
126 static constexpr unsigned int kTopBit = 0x80000000;
127 static constexpr unsigned int kCrcInitial = 0xffffffff;
128 static constexpr unsigned int kByteMask = 0xff;
129 static constexpr int kBitsPerByte = 8;
130 static constexpr int kBitsPerWord = 32;
131 static constexpr int kBytesPerPoll = 0x100000;
132
133 // Reverse the order of the low nBits bits of value.
134 static unsigned int Reflect(unsigned int value, int nBits) {
135 unsigned int reflected = 0;
136 for (int bit = 1; bit < nBits + 1; ++bit) {
137 if ((value & 1) != 0) {
138 reflected |= 1 << (nBits - bit);
139 }
140 value >>= 1;
141 }
142 return reflected;
143 }
144
145 // Build the table of the reflected CRC-32 one bit at a time.
146 static void BuildTable() {
147 sTable = std::make_unique<unsigned int[]>(kTableSize);
148 for (int i = 0; i < kTableSize; ++i) {
149 sTable[i] = Reflect(i, kBitsPerByte) << (kBitsPerWord - kBitsPerByte);
150 for (int bit = 0; bit < kBitsPerByte; ++bit) {
151 sTable[i] =
152 (sTable[i] & kTopBit) != 0 ? (sTable[i] << 1) ^ kPolynomial : sTable[i] << 1;
153 }
154 sTable[i] = Reflect(sTable[i], kBitsPerWord);
155 }
156 }
157};
158
159#endif
int Int(int nIndex) const
Report a node as an integer.
DataArray * FindArray(const char *pszTag, bool bFail) const
Find the child array whose first node is a tag.
DataArray * SystemConfig()
Report the configuration SystemInit() read.