scripts

misc scripts and tools
git clone git://git.2f30.org/scripts.git
Log | Files | Refs

1strdchk.c (5239B)


      1 /***********************************************************************************
      2  *  1st_read.bin File Checker - Visual Basic to C conversion
      3  *  
      4  *  LyingWake <LyingWake@gmail.com>
      5  *  http://www.consolevision.com/members/fackue/
      6  *
      7  *  This is a port of 1st_read.bin File Checker 1.5's source code
      8  *  from Visual Basic 6.0 to Microsoft Visual C++.
      9  *
     10  *  Thanks to all those that helped; JustBurn, Quzar, GPFerror and
     11  *  and anyond else I forgot.
     12  ***********************************************************************************/
     13 
     14 #include <stdio.h>
     15 #include <string.h>
     16 #include <fcntl.h>
     17 #include <stdlib.h>
     18 
     19 #ifdef _WIN32
     20 	#include <io.h>	
     21 #else
     22 	#include <unistd.h>	
     23 	#define _O_BINARY 0
     24 	#define _O_RDONLY O_RDONLY
     25 #endif
     26 
     27 #define Unscrambled  0
     28 #define Scrambled    1
     29 
     30 #ifndef _WIN32
     31 	long _filelength(int fd) {
     32 		long pos;
     33 		lseek(fd, 0, SEEK_SET);
     34 		pos = lseek(fd, 0, SEEK_END);
     35 		lseek(fd, 0, SEEK_SET);
     36 		return pos;
     37 	}
     38 #endif
     39 
     40 char *memoryspace;  /* A pointer that will be stored the file */
     41 
     42 ////////////////////////////////////////////////////////////////////////////////////
     43 
     44 /* //////////////////////////////////////////////////////////////////// */
     45 /* //                                                                // */
     46 /* //    Function: IdentifyBin                                       // */
     47 /* // Description: Reads file, checks if any given string is inside, // */
     48 /* //              if so, it's unscrambled, else it is scrambled.    // */
     49 /* //                                                                // */
     50 /* //     Returns: integer (rIdentifyBin)                            // */
     51 /* //                                                                // */
     52 /* //////////////////////////////////////////////////////////////////// */
     53 
     54 int IdentifyBin(char *filename) {
     55 	int pFile;
     56 	int rIdentifyBin;
     57 	int bytesreaded;
     58 	int i;
     59 
     60 	char abc1[] = "abcdefghijklmnopqrstuvwxyz1234567890";
     61 	char abc2[] = "abcdefghijklmnopqrstuvwxyz0123456789";
     62 	char abc3[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
     63 	char abc4[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
     64 	char abc5[] = "1234567890abcdefghijklmnopqrstuvwxyz";
     65 	char abc6[] = "0123456789abcdefghijklmnopqrstuvwxyz";
     66 	char abc7[] = "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ";
     67 	char abc8[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
     68 	char temp[] = "#...'...*...-.../...2...4...7...9...;...=...?...A...C...E...G...I...J...L...N...O...Q...R...T...U...W...X...Z...";
     69 	char temp2[] = "0123456789abcdef....(null)..0123456789ABCDEF";
     70 	char punch[] = "PORTDEV INFOENBLSTATRADRTOUTDRQCFUNCEND";
     71 	char tetris[] = "abcdefghijklEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*()";
     72 	char netbsd[] = "$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~";
     73 	char bortmnt[] = "0123456789ABCDEF....Inf.NaN.0123456789abcdef....(null)...";
     74 	char dreamsnes[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ.0123456789-";
     75 
     76 	rIdentifyBin = Scrambled;
     77 
     78 	pFile = open(filename, _O_BINARY | _O_RDONLY);
     79 	if (pFile == -1) {
     80 		return -1;
     81 	}  
     82   
     83 	/* Get the number of bytes in the file */
     84 	bytesreaded = _filelength(pFile); 
     85  
     86 	/* Allocate memory to store the file plus the NULL-termination char */
     87 	memoryspace = (char *)malloc(bytesreaded + 1); 
     88  
     89 	/* Ok, now we read the file */
     90 	bytesreaded = read(pFile, memoryspace, bytesreaded);
     91  
     92 	/* This is the little cheat ;) */
     93 	for (i = 0; i < bytesreaded; i++) { 
     94 		if (memoryspace[i] == 0x00) {
     95 			memoryspace[i] = '.';
     96 		} 
     97 	} 
     98  
     99 	/* We can't forget the NULL-termination char or program will crash! */
    100 	memoryspace[bytesreaded] = '\0'; 
    101 	close(pFile);
    102 
    103 	if (strstr(memoryspace, abc1) ||
    104 	 strstr(memoryspace, abc2) ||
    105 	 strstr(memoryspace, abc3) ||
    106 	 strstr(memoryspace, abc4) ||
    107 	 strstr(memoryspace, abc5) ||
    108 	 strstr(memoryspace, abc6) ||
    109 	 strstr(memoryspace, abc7) ||
    110 	 strstr(memoryspace, abc8) ||
    111 	 strstr(memoryspace, temp) ||
    112 	 strstr(memoryspace, temp2) ||
    113 	 strstr(memoryspace, bortmnt) ||
    114 	 strstr(memoryspace, dreamsnes) ||
    115 	 strstr(memoryspace, tetris) ||
    116 	 strstr(memoryspace, punch) ||
    117 	 strstr(memoryspace, netbsd)) {
    118 		rIdentifyBin = Unscrambled;
    119 	}
    120  
    121 	/* We dont need the memory anymore */
    122 	free(memoryspace);  
    123 
    124 	return rIdentifyBin;
    125 } 
    126 
    127 ////////////////////////////////////////////////////////////////////////////////////
    128 ////////////////////////////////////////////////////////////////////////////////////
    129 ////////////////////////////////////////////////////////////////////////////////////
    130 
    131 int main(int argc, char *argv[]) {
    132 	int i;
    133 	int retval = 0;
    134 
    135 	/* The program was opened without a filename argument */
    136 	if(argc < 2) {
    137 		fprintf(stderr, "1st_read.bin File Checker\nhttp://www.consolevision.com/members/fackue/\n\n");
    138 		fprintf(stderr, "Usage: %s filename\n", argv[0]);
    139 		return 1;
    140 	}
    141 
    142 	/* The program was opened with a filename argument */
    143 	for(i = 1; i < argc; i++) {
    144 		switch(IdentifyBin(argv[i])) {
    145 			case 0:
    146 				printf("%s is unscrambled\n", argv[i]);
    147 				break;
    148 			case 1:
    149 				printf("%s is scrambled\n", argv[i]);
    150 				break;
    151 			default:
    152 				fprintf(stderr, "%s not found\n", argv[i]);
    153 				retval = 1;
    154 				break;
    155 		}
    156 	}
    157 	
    158 	return retval;
    159 }