bool run_crc_err_test() { prep_buffers(); uint32_t ecc = ecc_generate(buf1); int bit_idx = rand()%24; ecc ^= 0x1 << bit_idx; return ecc_verify(buf2, ecc); }
bool run_1_err_test() { prep_buffers(); uint32_t ecc = ecc_generate(buf1); int byte_idx = rand()%512; int bit_idx = rand()%8; buf2[byte_idx] ^= 0x01 << bit_idx; bool v = ecc_verify(buf2, ecc); if (v) { if (!check_buffers()) { printf("VERIFIED but not FIXED\n"); return false; } } return v; }
bool run_2_err_test() { prep_buffers(); uint32_t ecc = ecc_generate(buf1); int byte_idx = rand()%512; int bit_idx = rand()%8; int byte2_idx = rand()%512; int bit2_idx = rand()%8; while ((byte2_idx == byte_idx) && (bit_idx == bit2_idx)) { byte2_idx = rand()%512; bit2_idx = rand()%8; } buf2[byte_idx] ^= 0x01 << bit_idx; buf2[byte2_idx] ^= 0x01 << bit2_idx; bool v = ecc_verify(buf2, ecc); return v == false; }
// core functionality virtual UINT32 compress(const UINT8 *src, UINT32 srclen, UINT8 *dest) { // determine header bytes UINT32 frames = srclen / CD_FRAME_SIZE; UINT32 complen_bytes = (srclen < 65536) ? 2 : 3; UINT32 ecc_bytes = (frames + 7) / 8; UINT32 header_bytes = ecc_bytes + complen_bytes; // clear out destination header memset(dest, 0, header_bytes); // copy audio data followed by subcode data for (UINT32 framenum = 0; framenum < frames; framenum++) { memcpy(&m_buffer[framenum * CD_MAX_SECTOR_DATA], &src[framenum * CD_FRAME_SIZE], CD_MAX_SECTOR_DATA); memcpy(&m_buffer[frames * CD_MAX_SECTOR_DATA + framenum * CD_MAX_SUBCODE_DATA], &src[framenum * CD_FRAME_SIZE + CD_MAX_SECTOR_DATA], CD_MAX_SUBCODE_DATA); // clear out ECC data if we can UINT8 *sector = &m_buffer[framenum * CD_MAX_SECTOR_DATA]; if (memcmp(sector, s_cd_sync_header, sizeof(s_cd_sync_header)) == 0 && ecc_verify(sector)) { dest[framenum / 8] |= 1 << (framenum % 8); memset(sector, 0, sizeof(s_cd_sync_header)); ecc_clear(sector); } } // encode the base portion UINT32 complen = m_base_compressor.compress(&m_buffer[0], frames * CD_MAX_SECTOR_DATA, &dest[header_bytes]); if (complen >= srclen) throw CHDERR_COMPRESSION_ERROR; // write compressed length dest[ecc_bytes + 0] = complen >> ((complen_bytes - 1) * 8); dest[ecc_bytes + 1] = complen >> ((complen_bytes - 2) * 8); if (complen_bytes > 2) dest[ecc_bytes + 2] = complen >> ((complen_bytes - 3) * 8); // encode the subcode return header_bytes + complen + m_subcode_compressor.compress(&m_buffer[frames * CD_MAX_SECTOR_DATA], frames * CD_MAX_SUBCODE_DATA, &dest[header_bytes + complen]); }
and vice versa. Should return a True/False depending on the \ success of the verification call\n\ "; static PyObject *py_verify(PyObject *self, PyObject *args, PyObject *kwargs) { PyObject *temp_state, *temp_keypair; ECC_State state; ECC_KeyPair keypair; char *data, *signature; if (!PyArg_ParseTuple(args, "ssOO", &data, &signature, &temp_keypair, &temp_state)) { return NULL; } state = (ECC_State)(PyCObject_AsVoidPtr(temp_state)); keypair = (ECC_KeyPair)(PyCObject_AsVoidPtr(temp_keypair)); if (ecc_verify(data, signature, keypair, state)) Py_RETURN_TRUE; Py_RETURN_FALSE; }
bool run_no_err_test() { prep_buffers(); uint32_t ecc = ecc_generate(buf1); return ecc_verify(buf2, ecc); }