2020-08-14 22:04:26 +00:00
|
|
|
/*
|
2020-09-08 00:58:40 +00:00
|
|
|
* Copyright (c) 2020, yzrh <yzrh@noema.org>
|
2020-08-14 22:04:26 +00:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "cnki.h"
|
|
|
|
|
|
|
|
int
|
|
|
|
cnki_kdh(cnki_t **param)
|
|
|
|
{
|
|
|
|
if (*param == NULL)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
if ((*param)->stat > 0)
|
|
|
|
printf("Begin 'KDH' decryption\n");
|
|
|
|
|
|
|
|
fseek((*param)->fp_i, 0, SEEK_END);
|
|
|
|
|
|
|
|
long size = ftell((*param)->fp_i);
|
|
|
|
|
|
|
|
fseek((*param)->fp_i, ADDRESS_KDH_BODY, SEEK_SET);
|
|
|
|
|
|
|
|
const char key[] = KEY_KDH;
|
|
|
|
const int key_len = KEY_KDH_LENGTH;
|
|
|
|
long key_cur = 0;
|
|
|
|
|
|
|
|
char buf[(*param)->size_buf];
|
|
|
|
|
2020-12-30 17:06:55 +00:00
|
|
|
FILE *tmp = tmpfile();
|
|
|
|
|
|
|
|
if (tmp == NULL)
|
|
|
|
return 1;
|
|
|
|
|
2020-08-14 22:04:26 +00:00
|
|
|
for (;;) {
|
|
|
|
fread(buf, (*param)->size_buf, 1, (*param)->fp_i);
|
|
|
|
|
|
|
|
for (int i = 0; i < (*param)->size_buf; i++) {
|
|
|
|
buf[i] ^= key[key_cur % key_len];
|
|
|
|
key_cur++;
|
|
|
|
}
|
|
|
|
|
2020-12-30 17:06:55 +00:00
|
|
|
fwrite(buf, (*param)->size_buf, 1, tmp);
|
2020-08-14 22:04:26 +00:00
|
|
|
|
|
|
|
if (ftell((*param)->fp_i) == size)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((*param)->stat > 0)
|
2020-12-30 17:06:55 +00:00
|
|
|
printf("Decrypted %ld byte(s)\n", ftell(tmp));
|
|
|
|
|
|
|
|
fseek(tmp, 0, SEEK_SET);
|
|
|
|
|
|
|
|
FILE *orig = (*param)->fp_i;
|
|
|
|
(*param)->fp_i = tmp;
|
|
|
|
|
|
|
|
cnki_pdf(param);
|
|
|
|
|
|
|
|
(*param)->fp_i = orig;
|
|
|
|
fclose(tmp);
|
|
|
|
|
|
|
|
if ((*param)->stat > 0)
|
|
|
|
printf("Conversion ended\n");
|
2020-08-14 22:04:26 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|