summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--crypto-search.cpp91
1 files changed, 91 insertions, 0 deletions
diff --git a/crypto-search.cpp b/crypto-search.cpp
new file mode 100644
index 0000000..8775095
--- /dev/null
+++ b/crypto-search.cpp
@@ -0,0 +1,91 @@
+/*
+ * 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>
+#include <values.h>
+#include <string.h>
+#include <unistd.h>
+#include "generic.h"
+#include "fileutils.h"
+
+int main(int argc, char ** argv) {
+ int h, size, mind, maxd, delta, len, i, j;
+ char * buffer, * str;
+
+ if (argc != 3) {
+ printm(M_BARE, "Usage: %s <inputfile> <needle>\n", argv[0]);
+ exit(-1);
+ }
+
+ verbosity = M_INFO;
+
+ if ((h = open(argv[1], O_RDONLY)) < 0) {
+ printm(M_ERROR, "Unable to open file %s\n", argv[1]);
+ exit(-1);
+ }
+
+ str = argv[2];
+ size = filesize(h);
+
+ printm(M_STATUS, "Requesting memory (%i bytes)\n", size);
+
+ if (!(buffer = (char *) malloc(size))) {
+ printm(M_ERROR, "Not enough memory.\n");
+ exit(-1);
+ }
+
+ printm(M_STATUS, "Loading file...\n");
+ read(h, buffer, size);
+
+ 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");
+ }
+ }
+ }
+}