#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
#define BITS 4
int CompareWith(char *name, unsigned char *buffer1, int bytes, unsigned long stopAfter) {
FILE *file;
unsigned char *buffer2;
unsigned long fileLen;
//Open file
file = fopen(name, "rb");
if (!file)
{
fprintf(stderr, "Unable to open file %s\n", name);
return -1;
}
//Get file length
fseek(file, 0, SEEK_END);
fileLen=ftell(file);
fseek(file, 0, SEEK_SET);
//Allocate memory
buffer2=(unsigned char *)malloc(fileLen+1);
if (!buffer2)
{
fprintf(stderr, "Memory error!\n");
fclose(file);
return -1;
}
//Read file contents into buffer2
fread(buffer2, fileLen, 1, file);
fclose(file);
int i;
int j;
unsigned long long number1;
unsigned long long number2;
unsigned long long diff;
for(i=0;i<stopAfter;i+=bytes) {
number1 = number2 = 0;
for(j=0;j<bytes;j++) {
number1 += (unsigned long long)(buffer1[i+j]) << j*8;
number2 += (unsigned long long)(buffer2[i+j]) << j*8;
}
diff = (number1>number2) ? (number1-number2) : (number2-number1);
printf("%i: %llu\n", i/bytes + 1, diff);
}
free(buffer1);
free(buffer2);
return i/bytes;
}
int main(int argc, char **argv)
{
if(argc < 2)
{
printf("Error! No filename specified!\n");
return 1;
}
int bytes = BITS;
if(argc > 2) {
int ai = 0;
for(ai=0;ai<argc;ai++) {
if(!strcmp("--bytes", argv[ai]) && argc > ai) {
bytes = atoi(argv[ai+1]);
}
}
}
int maxBytes = sizeof(long long);
if(bytes > maxBytes) {
printf("Too many bytes!\nAutosetting to %i...\n", maxBytes);
bytes = maxBytes;
}
FILE *file;
unsigned char *buffer;
unsigned long fileLen;
//Open file
file = fopen(argv[1], "rb");
if (!file)
{
fprintf(stderr, "Unable to open file %s\n", argv[1]);
return 1;
}
//Get file length
fseek(file, 0, SEEK_END);
fileLen=ftell(file);
fseek(file, 0, SEEK_SET);
//Allocate memory
buffer=(unsigned char *)malloc(fileLen+1);
if (!buffer)
{
fprintf(stderr, "Memory error!\n");
fclose(file);
return 1;
}
//Read file contents into buffer
fread(buffer, fileLen, 1, file);
fclose(file);
unsigned long stopAfter = fileLen;
if(argc > 2) {
int aj = 0;
for(aj=0;aj<argc;aj++) {
if(!strcmp("--numbers", argv[aj]) && argc > aj) {
stopAfter = atoi(argv[aj+1]) * bytes;
}
}
}
if(stopAfter > fileLen) {
stopAfter = fileLen;
}
int signalsOnly = 0;
if(argc > 2) {
int ak = 0;
for(ak=0;ak<argc;ak++) {
if(!strcmp("--compare", argv[ak]) && argc > ak) {
int bytesCompared = CompareWith(argv[ak+1], buffer, bytes, stopAfter);
if(bytesCompared > 0) {
printf("\nCompared %i %i-bit numbers from %s and %s\n", bytesCompared, bytes * 8, argv[1], argv[ak+1]);
return 0;
} else {
return 1;
}
} else if(!strcmp("--signals", argv[ak])) {
signalsOnly = 1;
}
}
}
int i;
int j;
unsigned long long number;
int skippingList;
for(i=0;i<stopAfter;i+=bytes) {
if( i>1416 && i<3496 ) {
if(!skippingList) {
printf("(Skipped maplist)\n");
skippingList = 1;
}
} else {
skippingList = 0;
number = 0;
for(j=0;j<bytes;j++) {
number += (unsigned long long)(buffer[i+j]) << j*8;
}
if(bytes == 4 && number == ('C' + ('A'<<8) + ('C'<<16) + ('A'<<24))) {
//CACA seems to change it :P
printf("Found possible signaling number %llu @ %i\n", number, i/bytes);
i -= 2; //Offset slightly
bytes = 6;
}
if(bytes == 6 && number == (0xA0UL + (0xBDUL<<8) + (0xE5UL<<16) + (0x07UL<<24) + (0x07UL<<32))) {
printf("Found possible signaling number %llu @ %i\n", number, i/bytes);
bytes = 8;
}
if( !signalsOnly ) {
printf("%i: %llu (%i-bit)\n", i/bytes + 1, number, bytes * 8);
}
}
}
printf("\nRead %i %i-bit numbers from %s\n", i/bytes, bytes * 8, argv[1]);
free(buffer);
return 0;
}