Source file: /~heha/secret/eagle_legalizer.zip/Linux/Eagle_LegaliZer.c

/**************************************************************************************\
 *                                                                                    *
 * Eagle LegaliZer: Makes any "pirate-marked" Eagle File become an "Original" one. ;D *
 * By ViKT0RY - 2005                                                                  *
 * http://www.vikt0ry.com                                                             *
 *                                                                                    *
\**************************************************************************************/ 

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

#define posxy(x,y) printf(" x1B[%d;%df",(x),(y)); 

unsigned long matriz[] = {
0x046667FB, 0x04676492, 0x046E691E, 0x046E6968,
0x046F69B7, 0x046F698C, 0x046F69EE, 0x046C6E21,
0x046C6E28, 0x046C6E3C, 0x045E6D7C, 0x04437212,
0x044172BE, 0x04467285, 0x04636351, 0x046363E0,
0x046166A2, 0x0461674D, 0x0464655D, 0x046F6989,
0x04536EE5, 0x045A6C7A, 0x045D6DFE, 0x05737297,
0x04556C22, 0x04506EF4, 0x057E7008, 0x0565761E,
0x046363E3, 0x057973DB, 0x057F701D, 0x057C7072,
0x04606600, 0x04636067, 0x046465E1, 0x045C6DBA,
0x055077D8, 0x06697CE8, 0x065743F7, 0x076B4B45,
0x04676560, 0x056A7674, 0x066D7DDF, 0x07584E04,
0x07434FF4, 0x0065539F, 0x006553EF, 0x006B53D1,
0x005D5641, 0x046B6BDF, 0x00405736, 0x04666415,
0x01645A98, 0x017E5565, 0x04606177, 0x01785528,
0x027F5D23, 0x9B8D9DDB, 0x026823C2, 0x03702528,
0x03632BFE, 0x035B2C09, 0x035F2D25, 0x0C75303B,
0x0C6136CF, 0x00000000
};

unsigned long startvalue=0;

char make_checksum(char *p) {
	int i=0, j=0;
	unsigned long checksum=0;
	char byte=0, value=0, xorvalue=0;
	
	xorvalue=*(char *)(p + 4);

	p+=0x18;

	for(i=0;i<2;i++) {
		checksum=(int)*(p + i * 0x18 + j);
		j+=2;
		while(j<0x18) {
			checksum=checksum << 1;
			if((checksum & 0x80000000)!=0) {
				checksum=checksum ^ 0x80000001;
			}
			checksum+=(char)*(p + i * 24 + j);
			j++;
		}
		j=0;
		byte=((checksum & 0xFF000000) >> 24) ^ ((checksum & 0xFF0000) >> 16) ^ ((checksum & 0xFF00) >> 8) ^ (checksum & 0xFF);
		value+=byte;
	}
	value=value ^ xorvalue;
	return value;
}

unsigned long random_number(unsigned long min, unsigned long max) {
	srand((unsigned int)time(NULL));
	return (min + rand() % (max - min));
}

int checkdate(unsigned long value) {
	int magic=0;
	int years=104;

	magic=(value >> 0x10) & 0xFF;
	if(magic > 0x35) {
		return 0;
	} else {
		magic=(value >> 0x18) & 0xFF;
		if(magic < 0x5F) {
			return 0;
		} else {
			years+=0xA;
			if(magic > years) {
				return 0;
			} else {
				if((value & 0xFFFF)==0) {
					return 0;
				} else {
					magic-=0x5E;
					magic*=0xBB8;
					/* magic should be 0x2EE0 */
					if(*(unsigned int short *)(&value) >= magic) {
						return 0;
					} else {
						return 1;
					}
				}
			}
		}
	}

}

int make_second_xor(unsigned long value) {
	int i=0;

	value=matriz[i] ^ 0x64726224;
	while(matriz[i]!=0) {
		if(value==matriz[i]) {
			return (i + 0x12D);
		} else {
			i++;
		}
	}

	return 0;

}

void crypt(unsigned long *first, unsigned long second) {
	*first=((((((~*first) >> 8) ^ second) >> 8) ^ second) >> 8) ^ second;
}

void make_first_xor(char *p) {
	int j=0;
	char xorvalue=0xFC;

	for(j=0;j<9;j++) {
		*(p + j)=*(p + j) ^ xorvalue;
		xorvalue+=0x42;
	}

}

void get_magic(unsigned long *magic, char *magic_checksum) {
	unsigned long last_value1=0;
	unsigned long last_value2=0;
	unsigned long ret=0;
	int j=0;
	char *p=0;
	unsigned char a=0, b=0;
	unsigned int short value1=0, value2=0;
	char xorvalue=0;

	//*magic=random_number(0x9d800000, 0xB5000000);
	*magic=startvalue;	
	
	while(ret==0) {
		
		*magic+=1;

		if(*magic < 0x9d800000) {
			*magic=0x9d800000;
		}
		
		/*
		if(*magic % 0x01000000 == 0) {
			printf("Calculating... Current value: 0x%X\n", *magic);
		}
		*/

		last_value1=*magic;

		xorvalue=0xFC;
		p=(char *)&last_value1;
		for(j=0;j<4;j++) {
			*(p + j)=*(p + j) ^ xorvalue;
			xorvalue+=0x42;
		}

		p=(char *)&last_value1;
		a=*(unsigned char *)(p + 3);
		b=*(unsigned char *)(p + 2);
		if((a < 0x5F) || (b > 0x34) || (a > 0x72)) {
			ret=0;
			continue;
		}

		value1=*(unsigned int short *)(&last_value1);
		value2=(unsigned int short)0xBB0 * (a - 0x5E);
		if(value1 > value2) {
			ret=0;
			continue;
		}

		last_value2=last_value1;
		crypt(&last_value2,last_value2);
		*magic_checksum=(last_value2 & 0xFF) ^ xorvalue;
		
		ret=make_second_xor(*magic);

		if(ret!=0) {
			ret=0;
			continue;
		}

		ret=*magic ^ 0x68637324;

		if(ret==0x0A7A6DA0 || ret==0x096962F4 || ret==0x0E533B97 || ret==0x044F621B || ret==0x9447572 || ret==0x376279D8) {
			ret=0;
			continue;
		}

		ret++;
	}
	
}

long filesize(FILE *pf)
{
     long j;
     
     fseek(pf, 0, SEEK_END);
     j=ftell(pf);
     rewind(pf);
     
     return j;
}

int fixfile(char *filename){

	/* This function fixes the file so that Legal Eagle can open it :D */

	FILE *myfile;
	char *lpBaseAddress;

	unsigned long magic[2]={ 0, 0 };
	unsigned long offset=2;
	unsigned long temp=0;
	unsigned long tamano=0;
	char magic_checksum[2]={ 0, 0 };
	char checksum=0;
	char flag=0;
	int i=0,j=0;
	int short number_of_checkpoints=0;

	myfile=fopen(filename, "rb+");
	if(!myfile) {
		printf("Couldn't open '%s' for writing!\n", filename);
		return -1;
	}

	tamano=filesize(myfile) + 1;
	lpBaseAddress=malloc(tamano);
	fread(lpBaseAddress, tamano - 1, 1, myfile);

	/*********************************************************************************/

	get_magic(&magic[0], &magic_checksum[0]);
	get_magic(&magic[1], &magic_checksum[1]);

	number_of_checkpoints=*(int short *)(lpBaseAddress + offset);
	offset+=22;
	
	for(i=number_of_checkpoints;i>=0;i--) {
		flag=*(char *)(lpBaseAddress + offset);
		if(flag == 0x11) {
			offset+=2;
			for(j=0;j<2;j++) {
				*(unsigned long *)(lpBaseAddress + offset) = (unsigned long)magic[j];
				offset+=4;
				temp=magic_checksum[j] & 0xFF;
				temp=(temp & 0xFFF0FFFF) | 0x90000;
				*(unsigned long *)(lpBaseAddress + offset) = (unsigned long) temp;
				offset+=4;
				*(int short *)(lpBaseAddress + offset) = (int short) 0;
				offset+=2;
				*(char *)(lpBaseAddress + offset) = (char) 0;
				offset++;
			}
		}
	}

	checksum=make_checksum((char *)lpBaseAddress);

	offset=0x0D;

	*(lpBaseAddress + offset)=checksum;
	
	*(lpBaseAddress + offset - 1)=0;

	/*********************************************************************************/

	rewind(myfile);
	fwrite(lpBaseAddress, 1, tamano, myfile);
	fclose(myfile);
	free(lpBaseAddress);

	return 1;

}

int checkfile(char *filename){

	/* This function makes the same comprobations as original Eagle source :D */

	FILE *myfile;
	char *lpBaseAddress=0;

	unsigned long offset=0;
	int i=0;

	char flag=0;
	int check=1;
	int counter=0;
	int version=0;
	int result=0;
	char checksum=0;
	unsigned long temp=0;
	unsigned char checksum2=0;
	int short number_of_checkpoints=0;

	unsigned long last_value=0;
	unsigned long tamano=0;
	char buffer[11];
	unsigned long *first_dword=(unsigned long *)buffer;
	unsigned long *second_dword=(unsigned long *)(buffer + 4);
	int short *word=(int short *)(buffer + 8);
	char *byte=(char *)(buffer + 10);
	char ret1=0, ret2=0, tempxor=0;
	
	myfile=fopen(filename, "rb");
	if(!myfile) {
		printf("Couldn't open '%s' for writing!\n", filename);
		return -1;
	}

	tamano=filesize(myfile) + 1;
	lpBaseAddress=malloc(tamano);
	fread(lpBaseAddress, tamano - 1, 1, myfile);
	
	/*********************************************************************************/

	flag=*(char *)lpBaseAddress;

	if(flag != 0x10) {
		printf("File seems to have an improper format!\n\n");
		return 2;
	}

	offset+=2;

	number_of_checkpoints=*(int short *)(lpBaseAddress + offset);

	offset+=6;

	version=(*(int *)(lpBaseAddress + offset)) & 0xFFFF;
	version=(version >> 8) + ((version & 0xFF) << 8);

	offset+=4;

	checksum2=*(char *)(lpBaseAddress + offset);

	offset++;

	checksum=*(char *)(lpBaseAddress + offset);

	offset+=11;

	for(i=number_of_checkpoints;i>=0;i--) {
		flag=*(char *)(lpBaseAddress + offset);
		if(flag == 0x11) {
			offset+=2;
			counter=0;
			while(counter<2) {
				*first_dword=*(unsigned long *)(lpBaseAddress + offset);
				offset+=4;
				startvalue=*first_dword;
				*second_dword=*(unsigned long *)(lpBaseAddress + offset);
				offset+=4;
				*word=*(int short *)(lpBaseAddress + offset);
				offset+=2;
				*byte=*(char *)(lpBaseAddress + offset);
				offset++;

				make_first_xor(buffer);

				last_value=*first_dword;

				if(*first_dword != 0) {
					crypt(&last_value, *first_dword);
					ret1=last_value & 0xFF;
					crypt(&last_value, *first_dword);
					ret2=last_value & 0xFF;
					tempxor=(*second_dword & 0xFF);
					if(ret1 != tempxor) {
						return 0;
					}
					if(make_second_xor(*first_dword)!=0) {
						return 0;
					}
					if((*first_dword ^ 0x68637324) != 0x0A7A6DA0) {
						if(((*second_dword & 0xF0000) >> 16)==1) {
							if(checkdate(*first_dword)==0) {
								return 0;
							} else {
								if(check!=0) {
									if(version >= 0x40A) {
										temp=(*first_dword ^ 0x68637324);
										if(temp != 0x096962F4) {
											if(temp != 0x0E533B97) {
												if(temp != 0x044F621B) {
													if(temp != 0x9447572) {
														if(temp != 0x376279D8) {
															check=0;
														} else {
															result=0;
															goto end;
														}
													} else {
														result=0;
														goto end;
													}
												} else {
													result=0;
													goto end;
												}
											} else {
												result=0;
												goto end;
											}
										} else {
											result=0;
											goto end;
										}
									} else {
										temp=(*first_dword ^ 0x68637324);
										if(temp != 0x0E533B97) {
												if(temp != 0x044F621B) {
													if(temp != 0x9447572) {
														if(temp != 0x376279D8) {
															check=0;
														} else {
															result=0;
															goto end;
														}
													} else {
														result=0;
														goto end;
													}
												} else {
													result=0;
													goto end;
												}
											} else {
												result=0;
												goto end;
											}
									}
								} else {
									temp=(*first_dword ^ 0x68637324);
									if(temp != 0x0E533B97) {
											if(temp != 0x044F621B) {
												if(temp != 0x9447572) {
													if(temp != 0x376279D8) {
														check=0;
													} else {
														result=0;
														goto end;
													}
												} else {
													result=0;
													goto end;
												}
											} else {
												result=0;
												goto end;
											}
										} else {
											result=0;
											goto end;
									}
								}
							}
						} else {
							result=0;
							goto end;
						}
					} else {
						result=0;
						goto end;
					}

				}

				counter++;

			}

		}
	}

	if(offset==0x18 || check==1) {
		return 0;
	}

	if(make_checksum((char *)lpBaseAddress) != checksum) {
		return 0;
	}
	
	if((checksum2 == 0x80) || (checksum2==0x40)) {
		return 0;
	}
	
	result=1;

	/*********************************************************************************/

end:

	fclose(myfile);
	free(lpBaseAddress);

	return result;

}

int main(int argc, char* argv[])
{

	int ret=0;
	int i=0;
	int force=0;

	printf("\n");
	printf("/¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯\\\n");
	printf("|                                 |\n");
	printf("| Eagle LegaliZer v1.2 by ViKT0RY |\n");
	printf("|                                 |\n");
	printf("| http://www.vikt0ry.com          |\n");
	printf("|                                 |\n");
	printf("\\_________________________________/\n\n");

	if(argc>1){
		if(!strcasecmp(argv[1], "-f")){
			force=1;
		}
	}

	if((force && argc < 3) || (!force && argc < 2)) {
		printf("You must specify a file to patch!\n\nUsage: Eagle_LegaliZer.exe [-f] File.[sch]|[brd]|[lbr] ...\n");
		printf("\nThis means that you can legalize any schema, board or library, use the parameter -f to force repair.\n");
		return 0;
	}
	
	for(i=1;i<argc;i++){

		if(force && i==1) {
			i++;
		}

		ret=checkfile(argv[i]);
	
		if(ret==1) {
			if(!force) {
				printf("The file '%s' seems to be legal...\n", argv[i]);
				continue;
			}
		} else if(ret==2 || ret==-1) {
			continue;
		}
	
		ret=fixfile(argv[i]);
		if(ret==0) {
			printf("Couldn't fix the file '%s'! :(\n",argv[i]);
		} else {
			printf("File '%s' fixed successfully! :D\n", argv[i]);
		}

	}

	return 0;

}

Detected encoding: ANSI (CP1252)4
Wrong umlauts? - Assume file is ANSI (CP1252) encoded