summaryrefslogtreecommitdiff
path: root/crypto-search.cpp
blob: 164c001232184d172873743be3bf205cad14fa17 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*
 *  PSX-Tools Bundle Pack
 *  Copyright (C) 2002 Nicolas "Pixel" Noble
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include <stdio.h>
#include <stdlib.h>
#ifdef __linux__
#include <values.h>
#else
#define MAXINT INT_MAX
#define MININT INT_MIN
#endif
#include <string.h>
#include "generic.h"
#include "Input.h"
#include "Main.h"

CODE_BEGINS
int startup(void) throw (GeneralException) {
    int size, mind, maxd, delta, len, i, j;
    char * buffer, * str;
    Handle * h;

    if (argc != 3) {
	printm(M_BARE, "Usage: %s <inputfile> <needle>\n", argv[0]);
	exit(-1);
    }
    
    verbosity = M_INFO;
    
    h = new Input(argv[1]);
    
    str = argv[2];
    size = h->GetSize();

#if 0
    printm(M_STATUS, "Requesting memory (%i bytes)\n", size);
    
    if (!(buffer = (char *) malloc(size))) {
	printm(M_ERROR, "Not enough memory.\n");
	delete h;
	exit(-1);
    }

    printm(M_STATUS, "Loading file...\n");
    h->read(buffer, size);
#else
    printm(M_STATUS, "Mapping file\n");

    buffer = (char *) h->mmap();
#endif
    printm(M_STATUS, "Done, initialising the search.\n");
    len = strlen(argv[2]);
    
    mind = MININT;
    maxd = MAXINT;
    
    for (i = 0; i < len; i++) {
	mind = (mind < (-str[i])) ? -str[i] : mind;
	maxd = (maxd > (255 - str[i])) ? 255 - str[i] : maxd;
    }
    
    printm(M_STATUS, "Min Delta = %i, Max Delta = %i\n", mind, maxd);
    
    printm(M_STATUS, "Beginning searching...\n");
    
    for (i = 0; i < (size - len); i++) {
	delta = buffer[i] - str[0];
	if ((delta >= mind) && (delta <= maxd)) {
	    int good = 1;
	    for (j = 1; j < len; j++) {
		if (((delta + str[j]) != buffer[i + j]) && (str[j] != '.')) {
		    good = 0;
		    break;
		}
	    }
	    if (good) {
		printm(M_STATUS, "Found a needle at %i = 0x%08x with delta %i:", i, i, delta);
		for (j = 0; j < len; j++) {
		    printm(M_BARE, "%c", buffer[j + i]);
		}
		printm(M_BARE, "\n");
	    }
	}
    }
    
    delete h;
    
    return 0;
}
CODE_ENDS