Improve .bnsf key detection in some cases

This commit is contained in:
bnnm 2022-01-30 13:26:39 +01:00
parent 2db8e56ef2
commit 964397c1fb
4 changed files with 18 additions and 16 deletions

View File

@ -116,7 +116,7 @@ int test_key_g7221(g7221_codec_data* data, off_t start, STREAMFILE* sf) {
/* assumes key was set before this call */
while (test_frames < S14_KEY_MAX_TEST_FRAMES && current_frame < max_frames) {
int score, ok;
int score, res;
size_t bytes;
uint8_t buf[G7221_MAX_FRAME_SIZE];
@ -127,8 +127,8 @@ int test_key_g7221(g7221_codec_data* data, off_t start, STREAMFILE* sf) {
break;
}
ok = g7221_decode_frame(data->ch[cur_ch].handle, buf, data->ch[cur_ch].buffer);
if (!ok) {
res = g7221_decode_frame(data->ch[cur_ch].handle, buf, data->ch[cur_ch].buffer);
if (res < 0) {
total_score = -1;
break;
}

View File

@ -1072,12 +1072,13 @@ static int unpack_frame(int bit_rate, const uint8_t* data, int frame_size, /*int
/* test for errors (in refdec but not Namco's, useful to detect decryption) */
if (test_errors) {
int max_pad_bytes = 0x7; /* usually 0x04 and rarely ~0x07 */
int bits_left = 8 * expected_frame_size - bitpos;
int i, endpos, test_bits;
if (bits_left > 0) {
/* frame must be padded with 1s */
/* frame must be padded with 1s after regular data */
endpos = bitpos;
for (i = 0; i < bits_left; i++) {
int bit = (data_u32[endpos >> 5] >> (31 - (endpos & 0x1F))) & 1;
@ -1087,19 +1088,21 @@ static int unpack_frame(int bit_rate, const uint8_t* data, int frame_size, /*int
return -1;
}
/* extra: test we aren't in the middle of padding (happens with bad keys) */
/* extra: test we aren't in the middle of padding (happens with bad keys)
* After reading the whole frame, last bit position should land near last useful
* data, a few bytes into padding, so check there aren't too many padding bits. */
endpos = bitpos;
test_bits = 8 * 0x04;
test_bits = 8 * max_pad_bytes;
if (test_bits > bitpos)
test_bits = bitpos;
for (i = 0; i < test_bits; i++) {
int bit = (data_u32[endpos >> 5] >> (31 - (endpos & 0x1F))) & 1;
endpos--;
endpos--; /* from last position towards valid data */
if (bit != 1)
break;
}
/* so many 1s isn't very normal */
if (i == test_bits)
return -8;
@ -1186,10 +1189,9 @@ int g7221_decode_frame(g7221_handle* handle, uint8_t* data, int16_t* out_samples
/* Namco also sets number of codes/samples done from unpack_frame/rmlt (ptr arg),
* but they seem unused */
return 1;
fail:
//;printf("S14: fail %i\n", res);
return 0;
fail:
return res;
}
#if 0
@ -1271,7 +1273,7 @@ int g7221_set_key(g7221_handle* handle, const uint8_t* key) {
/* reset new key */
s14aes_set_key(handle->aes, temp_key);
return 1;
fail:
return 0;
fail:
return -1;
}

View File

@ -12,7 +12,7 @@ typedef struct g7221_handle g7221_handle;
/* return a handle for decoding on successful init, NULL on failure */
g7221_handle* g7221_init(int bytes_per_frame);
/* decode a frame, at code_words, into 16-bit PCM in sample_buffer */
/* decode a frame, at code_words, into 16-bit PCM in sample_buffer. returns <0 on error */
int g7221_decode_frame(g7221_handle* handle, uint8_t* data, int16_t* out_samples);
#if 0
@ -26,7 +26,7 @@ void g7221_reset(g7221_handle* handle);
/* free resources */
void g7221_free(g7221_handle* handle);
/* set new key (ignores key on failure) */
/* set new key (ignores key on failure). returns <0 on error */
int g7221_set_key(g7221_handle* handle, const uint8_t* key);
#endif

View File

@ -234,7 +234,7 @@ static void bruteforce_bnsf_key(STREAMFILE* sf, off_t start, g7221_codec_data* d
}
}
//done:
done:
VGM_ASSERT(best_score > 0, "BNSF: best key=%.24s (score=%i)\n", best_key, best_score);
VGM_ASSERT(best_score < 0, "BNSF: key not found\n");