Difference between revisions of "ADF"

From GTAMods Wiki
Jump to navigation Jump to search
m
 
(One intermediate revision by the same user not shown)
Line 24: Line 24:
 
if (argc != 3)
 
if (argc != 3)
 
{
 
{
printf("Usage: adftool <infile> <outfile>\n");
+
printf("Usage: adftool <inputFile> <outputFile>\n");
 
return 1;
 
return 1;
 
}
 
}
 
 
 
// open input file
 
// open input file
FILE *infile = fopen(argv[1], "rb");
+
FILE *inputFile = fopen(argv[1], "rb");
if (!infile)
+
if (!inputFile)
 
{
 
{
 
printf("Failed to open %s!\n", argv[1]);
 
printf("Failed to open %s!\n", argv[1]);
Line 37: Line 37:
 
 
 
// open output file
 
// open output file
FILE *outfile = fopen(argv[2], "wb");
+
FILE *outputFile = fopen(argv[2], "wb");
if (!outfile)
+
if (!outputFile)
 
{
 
{
 
printf("Failed to open %s!\n", argv[2]);
 
printf("Failed to open %s!\n", argv[2]);
fclose(infile);
+
fclose(inputFile);
 
return 1;
 
return 1;
 
}
 
}
 
 
// allocate buffer (8 MB)
+
// allocate buffer (16 MB)
const size_t buffer_size = 1024 * 1024 * 8;
+
const size_t bufferSize = 1024 * 1024 * 16;
char *buffer = (char *)malloc(buffer_size);
+
char *buffer = (char *)malloc(bufferSize);
 
if (!buffer)
 
if (!buffer)
 
{
 
{
 
printf("Failed to allocate buffer!\n");
 
printf("Failed to allocate buffer!\n");
fclose(infile);
+
fclose(inputFile);
fclose(outfile);
+
fclose(outputFile);
 
return 1;
 
return 1;
 
}
 
}
Line 58: Line 58:
 
// read input file into buffer block by block
 
// read input file into buffer block by block
 
size_t size;
 
size_t size;
while ((size = fread(buffer, 1, buffer_size, infile)) > 0)
+
while ((size = fread(buffer, 1, bufferSize, inputFile)) > 0)
 
{
 
{
 
// xor buffer against 0x22
 
// xor buffer against 0x22
Line 68: Line 68:
 
 
 
// write buffer to output file
 
// write buffer to output file
fwrite(buffer, 1, size, outfile);
+
fwrite(buffer, 1, size, outputFile);
 
}
 
}
 
 
 
// deallocate buffer and close files
 
// deallocate buffer and close files
 
free(buffer);
 
free(buffer);
fclose(infile);
+
fclose(inputFile);
fclose(outfile);
+
fclose(outputFile);
 
 
 
return 0;
 
return 0;

Latest revision as of 15:45, 2 April 2015

The ADF file format is used for the radio stations in Vice City. It is equal to a raw MP3 file with no ID3 tag (though a tag will likely be ignored) with a very simple encryption: every byte should be XOR-ed against the value 0x22.

Example

This is an example of performing a XOR operation on a byte within a file. The first byte in an ADF file is 0xDD. In binary, that would be 0b11011101. Perform a XOR on that against the value 0x22, which is 0b100010 in binary. If the digit in 0xDD corresponds to 0x22's digit "1", then switch the digit to its opposite. If the digit in 0xDD corresponds to 0x22's digit "0", then leave the digit alone. The result would be 0b11111111, which is 0xFF.

0xDD 0b11011101
0x22 0b00100010
     0b11111111 = 0xFF

Another example, performing a XOR on 0x10 against 0x22:

0x10 0b00010000
0x22 0b00100010
     0b00110010 = 0x32

When this is done to every byte within the ADF file, the file will result in a file that is listenable in standard audio players.

Below is an example of a simple C++ program:

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
	// check command-line arguments
	if (argc != 3)
	{
		printf("Usage: adftool <inputFile> <outputFile>\n");
		return 1;
	}
	
	// open input file
	FILE *inputFile = fopen(argv[1], "rb");
	if (!inputFile)
	{
		printf("Failed to open %s!\n", argv[1]);
		return 1;
	}
	
	// open output file
	FILE *outputFile = fopen(argv[2], "wb");
	if (!outputFile)
	{
		printf("Failed to open %s!\n", argv[2]);
		fclose(inputFile);
		return 1;
	}
	
	// allocate buffer (16 MB)
	const size_t bufferSize = 1024 * 1024 * 16;
	char *buffer = (char *)malloc(bufferSize);
	if (!buffer)
	{
		printf("Failed to allocate buffer!\n");
		fclose(inputFile);
		fclose(outputFile);
		return 1;
	}
	
	// read input file into buffer block by block
	size_t size;
	while ((size = fread(buffer, 1, bufferSize, inputFile)) > 0)
	{
		// xor buffer against 0x22
		char *iterator = buffer;
		for (size_t i = 0; i < size; i++)
		{
			*iterator++ ^= 0x22;
		}
		
		// write buffer to output file
		fwrite(buffer, 1, size, outputFile);
	}
	
	// deallocate buffer and close files
	free(buffer);
	fclose(inputFile);
	fclose(outputFile);
	
	return 0;
}

External links