| 114 | // to validate. |
| 115 | |
| 116 | DWORD DecodeProdKey( |
| 117 | LPSTR lpstrEncodedChars, // ptr to 25-character secure CD-Key |
| 118 | LPBYTE pbPublicKey, // pointer to the public key |
| 119 | LONG cbPublicKey, // size of the public key in bytes |
| 120 | LPSTR lpstrDigits, // custom achDigits array |
| 121 | LPBYTE pbBinCdKey, // if non-NULL return binary version of secure CD-Key |
| 122 | int cbBinCdKey) |
| 123 | { |
| 124 | BOOL fOk = TRUE; |
| 125 | DWORD dwBinData = INVALID_PID; |
| 126 | |
| 127 | const int iBase = 24; |
| 128 | |
| 129 | // 64 bytes is enough space to decode 111 encoded characters (when iBase is 24) |
| 130 | BYTE abDecodedBytes[64] = {0}; |
| 131 | int iDecodedBytes; // index |
| 132 | int iDecodedBytesMax = 0; // index of highest byte used |
| 133 | |
| 134 | const int iDecodedPidBitCnt = 31; |
| 135 | BYTE abDecodedPid[iDecodedPidBitCnt/8 + (0 != iDecodedPidBitCnt/8)] = {0}; |
| 136 | |
| 137 | // Number of bits that can be encoded in 1000 base 24 digits |
| 138 | // 1000 * log(24, 2) |
| 139 | const int iBitsPerKChar = 4585; |
| 140 | |
| 141 | char achDigits[iBase+1] = "BCDFGHJKMPQRTVWXY2346789"; |
| 142 | int iDigits; // index |
| 143 | |
| 144 | int iBitCnt = (int)(((long)(lstrlenA(lpstrEncodedChars) - StrCountCharA(lpstrEncodedChars, '-') ) * iBitsPerKChar) / 1000); |
| 145 | int iByteCnt = iBitCnt/8 + (0 != iBitCnt%8); |
| 146 | |
| 147 | int iSigBitCnt = iBitCnt - iDecodedPidBitCnt; |
| 148 | int iSigByteCnt = iSigBitCnt/8 + (0 != iSigBitCnt%8); |
| 149 | |
| 150 | if (NULL == lpstrDigits) |
| 151 | { |
| 152 | lpstrDigits = achDigits; |
| 153 | } |
| 154 | else |
| 155 | { |
| 156 | fOk = (lstrlenA(achDigits) == lstrlenA(lpstrDigits)); |
| 157 | if (fOk) |
| 158 | { |
| 159 | StrUpperA(lpstrDigits); |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | // We requie at least 25 input characters to leave enough bits for the signature |
| 164 | // and we make sure we're not going to overrun our buffer |
| 165 | |
| 166 | if (fOk && 25 <= lstrlenA(lpstrEncodedChars) && iByteCnt < sizeof(abDecodedBytes)) |
| 167 | { |
| 168 | StrUpperA(lpstrEncodedChars); |
| 169 | |
| 170 | // first we fill abDecodedBytes with the binary data |
| 171 | |
| 172 | LPCSTR lpstrEncodedCharsCurr = lpstrEncodedChars; |
| 173 | char chCurEncoded = *lpstrEncodedCharsCurr; |
no test coverage detected