Though all arguments are correct, it returns the right value of items read, the function fread_s doesn’t story anything in “bytes” it’s empty. It’s also empty when I swap 10485760 & 1. Does anyone know what causes this problem? There are no problems with the file at all.
float EncryptBig(CRYPTIN* handle)
{
int i, index = 0;
float calc;
char* bytes;
i = (handle->size - handle->huidig);
if ((i-10485760) < 0)
{
bytes = (char*)malloc(i);
if (bytes == NULL)
{
fcloseall();
free(handle);
return 100.0f;
}
fread_s(&bytes, i, 1, i, handle->bestand); // Here and down below
fclose(handle->bestand);
for (index = 0; index < i; index++)
{
__asm
{
mov eax, dword ptr [bytes]
add eax, dword ptr [index]
mov cl, byte ptr [eax]
xor cl, 101
xor cl, 53
not cl
mov byte ptr [eax], cl
mov eax, dword ptr [index]
add eax, 1
mov dword ptr [index], eax
}
}
fwrite(bytes, 1, i, handle->nieuwbstnd);
fclose(handle->nieuwbstnd);
free(handle);
free(bytes);
return 100.0f;
}
if (handle->huidig == 0)
{
fseek(handle->bestand, 0, SEEK_SET);
fseek(handle->nieuwbstnd, 0, SEEK_SET);
}
bytes = (char*)malloc(10485760);
if (bytes == NULL)
{
fcloseall();
free(handle);
return 100.0f;
}
fread_s(bytes, 10485760, 10485760, 1, handle->bestand); // Here
for (index = 0; index < 10485760; index++)
{
__asm
{
mov eax, dword ptr [bytes]
add eax, dword ptr [index]
mov cl, byte ptr [eax]
xor cl, 101
xor cl, 53
not cl
mov byte ptr [eax], cl
mov eax, dword ptr [index]
add eax, 1
mov dword ptr [index], eax
}
}
fwrite(bytes, 1, 10485760, handle->bestand);
free(bytes);
handle->huidig += 10485760;
handle->positie += 10485760;
fseek(handle->bestand, handle->huidig, SEEK_SET);
fseek(handle->nieuwbstnd, handle->positie, SEEK_SET);
calc = (float)handle->huidig;
calc /= (float)handle->size;
calc *= 100.0f;
if (calc >= 100.0)
{
fclose(handle->bestand);
fclose(handle->nieuwbstnd);
free(handle);
}
return calc;
}
EDIT: Solved
Not sure if this is your specific problem but it is something wrong with your code.
When you do an
freadcall, you specify how many “objects” you want to read and the object size, thenfreadwill read up to that many objects. But it will read an exact number of those objects.So, when you try to read a million one-byte objects from a seven-byte file, you’ll get seven of them and the return code will reflect this. However, if you try to read one million-byte object, you’ll get nothing at all, and the return code will reflect that.
The reason I emphasize the return value is that you may get back less objects than you wanted (such as if you ask for 1000 one-byte objects where there’s only 900 left in the file), so blindly writing the expected amount to the output file is a no-no. You need to check the return code and act on that.
In addition, as a commenter points out, you appear to be writing back to the input file in one of your output statements:
That’s unlikely to end well 🙂 It should probably be: