How do I re-size a binary file?

Started by
10 comments, last by SlashC++Programmer 8 years, 5 months ago

Hii I'm trying to re-size a binary file. It's seems when I do this it's off by 8 bytes. If I use a different file it might be 3 or 5 bytes.

Here is my re-size code:


		// index the beginning of the new .bik file
		int index = 2160640;

		// files to load
		std::ifstream readFile("baa_logo_run_v5_h264.bik", std::ifstream::binary);
		std::ifstream readFile2("riddeler.bik", std::ifstream::binary);

		// length of old file
		long length = 9972608;

		// get size of readFile
		readFile.seekg(0, readFile.end);
		long size = readFile.tellg();
		readFile.seekg(0);

		// get size of readFile2
		readFile2.seekg(0, readFile2.end);
		long size2 = readFile2.tellg();
		readFile2.seekg(0);

		// allocate memory for file content
		char* readBuffer1 = new char[size];
		char* readBuffer2 = new char[size2];

		// read contents in buffer
		readFile.read(readBuffer1, size);
		readFile2.read(readBuffer2, size2);

		readFile.close();
		readFile2.close();

		// file to write to
		std::ofstream WriteFile("myfile.ty", std::ofstream::binary); // update new bik files

		// .bik file size is less than current bik length
		if (size2 < length)
		{
			// re-size writeBuffer
			long newSize = size - (length - size2);
			char* writeBuffer = new char[newSize];

			// rebuild file again
			for (int i = 0; i < newSize; i++)
			{
				writeBuffer[i] = readBuffer1[i];
			}

			// counters and flag
			int num = 0;
			int num2 = 0;
			int flag = 0;

			// length of the whole file
			for (int i = index; i < newSize; i++)
			{
				// length of the new bik file
				if (num < size2)
				{
					writeBuffer[i] = readBuffer2[num];

				}
				num++;

				// if the new bik is stored then flag = 1
				if (num > size2)
				{
					flag = 1;
				}

				// write the rest of the other files.
				if (flag == 1)
				{
					if (num2 < newSize && index + length + num2 < newSize)
					{
						writeBuffer[i] = readBuffer1[(index + length + num2)];
					}
					num2++;
				}

			}

			// write to file
			WriteFile.write(writeBuffer, newSize);
			WriteFile.close();
			delete[] writeBuffer;

		}

		// end of rebuild file
Advertisement
I'm a bit confused to what exactly you are trying to do.

You are starting with some bik files, which I assume are Bink video files. Then you are chopping these files at some location and assuming the output file will work for something.

That seems fishy at best, but we'll assume you know your data files and the reason you are slicing it at those locations.

This seems buggy:
long newSize = size - (length - size2);
char* writeBuffer = new char[newSize];

You are taking the size of a data file, and subtracting from it the difference between your hard-coded value and your second data file size. Then using that for the size of a buffer, since for some reason you like storing the complete data files in memory.

Your writing code has some fishy conditions, repeatedly checking if each of the numbers -- I assume this is around 2 million to 8 million times -- to verify if it has crossed the threshold. You've got several interesting boundary conditions in the middle, and I'm not certain they do as you expect. But since I don't know what you really expect, I cannot be certain.

It seems to me you need to simplify. Calculate the size of the block you need from the first file. Calculate the size of the block you need from the second file. Then do a single call to a copy or write function to copy those millions of bytes (rather than millions of loops of single byte copy) to get the data from that first file. Then to a single call to a copy or write function to cope those millions of bytes to get the data from that second file.

That will make it much easier logic and will likely identify your issue.

In more code-like terms:


writeBufferPosition = (whatever writeBuffer[index] starts at, or whatever you expected it to really be. );
lengthFromBuffer2 = (your calculation here);
lengthFromBuffer1 = (your calculation here);
buffer1StartOffset = (your calculation here);
memcpy( writeBufferPosition, &readbuffer2[0], lengthFromBuffer2);
writeBufferPosition += lengthFromBuffer2;
memcpy( writeBufferPosition, &readbuffer1[Buffer1StartOffset], lengthFromBuffer1);
writeBufferPosition += lengthFromBuffer1;

Far simpler logic.

Still can't just post the whole thing?

That is so frustrating

void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

Why are you trying to resize "Batman Arkham Asylum" assets?

"The code you write when you learn a new language is shit.
You either already know that and you are wise, or you don’t realize it for many years and you are an idiot. Either way, your learning code is objectively shit." - L. Spiro

"This is called programming. The art of typing shit into an editor/IDE is not programming, it's basically data entry. The part that makes a programmer a programmer is their problem solving skills." - Serapth

"The 'friend' relationship in c++ is the tightest coupling you can give two objects. Friends can reach out and touch your privates." - frob

Why are you trying to resize "Batman Arkham Asylum" assets?


That is interesting...

Why are you trying to resize "Batman Arkham Asylum" assets?


Haven't you heard? It has a 12gb RAM requirement. Clearly OP is trying to fix that.

/troll

throw table_exception("(? ???)? ? ???");


It has a 12gb RAM requirement.

Would have been more funny if ByteTroll didn't say Arkham Asylum, since you're referring to Arkham Knight.

/pedant

Hello to all my stalkers.


It has a 12gb RAM requirement.

Would have been more funny if ByteTroll didn't say Arkham Asylum, since you're referring to Arkham Knight.

/pedant

I don't care, I still found it funny rolleyes.gif

"The code you write when you learn a new language is shit.
You either already know that and you are wise, or you don’t realize it for many years and you are an idiot. Either way, your learning code is objectively shit." - L. Spiro

"This is called programming. The art of typing shit into an editor/IDE is not programming, it's basically data entry. The part that makes a programmer a programmer is their problem solving skills." - Serapth

"The 'friend' relationship in c++ is the tightest coupling you can give two objects. Friends can reach out and touch your privates." - frob

Haven't you heard? It has a 12gb RAM requirement. Clearly OP is trying to fix that.

/troll


See. This is why you don't load the entire thing into memory first, just do it in chunks:

max_block_size = 8*1024*1024; // 8 MiB
buffer = alloc max_block_size
in_data = ...
target_pos = ...
target_next_pos = ...

# copy left side
remaining = target_pos
while (block_sz = (min remaining, max_block_size)) > 0:
  read in_file, buffer, block_sz
  write out_file, buffer, block_sz
  remaining -= block_sz

# insert payload
write out_file, in_data, (sizeof in_data)

# (write additional padding here)

# copy right side
in_file.read_pos = target_next_pos
while (block_sz = read in_file, buffer, max_block_size) > 0:
  write out_file, buffer, block_sz
See? Ansd yoou;re done.

EDIT:
IF you don't have enough ram, then perhaps you should download more ram instead.

Why are you trying to resize "Batman Arkham Asylum" assets?

After half an hour in the chat begging him to explain what he was doing I still don't really know. I think he's trying to make some kind of content mod. He said something about trying to put a riddler video in the place of a batman video, but "it was off by 8 places". I tried to get him to post the whole code body but after three rounds of that I gave up and left.

void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

This topic is closed to new replies.

Advertisement