[382] | 1 | /* |
---|
| 2 | * main.c: main function |
---|
| 3 | * $Id: main.c 444 2005-01-10 00:43:36Z sam $ |
---|
| 4 | * |
---|
| 5 | * Copyright: (c) 2004 Sam Hocevar <sam@zoy.org> |
---|
| 6 | * This program is free software; you can redistribute it and/or |
---|
| 7 | * modify it under the terms of the Do What The Fuck You Want To |
---|
| 8 | * Public License as published by Banlu Kemiyatorn. See |
---|
| 9 | * http://sam.zoy.org/projects/COPYING.WTFPL for more details. |
---|
| 10 | */ |
---|
[381] | 11 | |
---|
| 12 | #include <stdio.h> |
---|
| 13 | #include <stdlib.h> |
---|
[389] | 14 | #include <string.h> |
---|
[388] | 15 | #include <getopt.h> |
---|
[392] | 16 | #include <stdarg.h> |
---|
[381] | 17 | |
---|
| 18 | #include "config.h" |
---|
| 19 | #include "common.h" |
---|
| 20 | |
---|
[391] | 21 | #ifdef HAVE_GETOPT_LONG |
---|
| 22 | # define MOREINFO "Try `%s --help' for more information.\n" |
---|
| 23 | #else |
---|
| 24 | # define MOREINFO "Try `%s -h' for more information.\n" |
---|
| 25 | #endif |
---|
| 26 | |
---|
[392] | 27 | /* Used for the debug messages */ |
---|
| 28 | char *argv0 = NULL; |
---|
[430] | 29 | char *share = NULL; |
---|
[392] | 30 | int debug = 1; |
---|
| 31 | |
---|
[381] | 32 | int main(int argc, char *argv[]) |
---|
| 33 | { |
---|
[388] | 34 | char *mode = "auto"; |
---|
[400] | 35 | int c; |
---|
| 36 | int digit_optind = 0; |
---|
[381] | 37 | |
---|
[392] | 38 | argv0 = argv[0]; |
---|
[430] | 39 | share = "share"; |
---|
[392] | 40 | |
---|
[388] | 41 | for(;;) |
---|
[381] | 42 | { |
---|
[388] | 43 | int this_option_optind = optind ? optind : 1; |
---|
[391] | 44 | #ifdef HAVE_GETOPT_LONG |
---|
[388] | 45 | int option_index = 0; |
---|
| 46 | static struct option long_options[] = |
---|
| 47 | { |
---|
[430] | 48 | { "help", 0, 0, 'h' }, |
---|
[388] | 49 | { "mode", 1, 0, 'm' }, |
---|
[430] | 50 | { "share", 1, 0, 's' }, |
---|
[392] | 51 | { "quiet", 0, 0, 'q' }, |
---|
[388] | 52 | { "version", 0, 0, 'v' }, |
---|
| 53 | { 0, 0, 0, 0 } |
---|
| 54 | }; |
---|
| 55 | |
---|
[430] | 56 | c = getopt_long(argc, argv, "hm:s:qv", long_options, &option_index); |
---|
[391] | 57 | #else |
---|
[430] | 58 | c = getopt(argc, argv, "hm:s:qv"); |
---|
[391] | 59 | #endif |
---|
[388] | 60 | if(c == -1) |
---|
| 61 | break; |
---|
| 62 | |
---|
| 63 | switch(c) |
---|
| 64 | { |
---|
| 65 | case 'h': /* --help */ |
---|
| 66 | printf("Usage: %s [OPTION]... FILE...\n", argv[0]); |
---|
[391] | 67 | #ifdef HAVE_GETOPT_LONG |
---|
[430] | 68 | printf(" -m, --mode <mode> force operating mode\n"); |
---|
| 69 | printf(" -s, --share <dir> specify shared dir\n"); |
---|
| 70 | printf(" -q, --quiet do not print information messages\n"); |
---|
| 71 | printf(" -h, --help display this help and exit\n"); |
---|
| 72 | printf(" -v, --version output version information and exit\n"); |
---|
[391] | 73 | #else |
---|
[430] | 74 | printf(" -m <mode> force operating mode\n"); |
---|
| 75 | printf(" -s <dir> specify shared dir\n"); |
---|
| 76 | printf(" -q do not print information messages\n"); |
---|
| 77 | printf(" -h display this help and exit\n"); |
---|
| 78 | printf(" -v output version information and exit\n"); |
---|
[391] | 79 | #endif |
---|
[388] | 80 | return 0; |
---|
| 81 | case 'm': /* --mode */ |
---|
| 82 | mode = optarg; |
---|
| 83 | break; |
---|
[392] | 84 | case 'q': /* --quiet */ |
---|
| 85 | debug = 0; |
---|
| 86 | break; |
---|
[430] | 87 | case 's': /* --quiet */ |
---|
| 88 | share = optarg; |
---|
| 89 | break; |
---|
[388] | 90 | case 'v': /* --version */ |
---|
[391] | 91 | printf("pwntcha (captcha decoder) %s\n", VERSION); |
---|
[388] | 92 | printf("Written by Sam Hocevar.\n"); |
---|
| 93 | printf("\n"); |
---|
| 94 | printf("Copyright (C) 2004-2005 Sam Hocevar <sam@zoy.org>\n"); |
---|
| 95 | printf("This is free software; see the source for copying conditions. There is NO\n"); |
---|
| 96 | printf("warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n"); |
---|
| 97 | return 0; |
---|
| 98 | case '?': |
---|
[391] | 99 | printf(MOREINFO, argv[0]); |
---|
| 100 | return 1; |
---|
[388] | 101 | default: |
---|
| 102 | printf("%s: invalid option -- %i\n", argv[0], c); |
---|
[391] | 103 | printf(MOREINFO, argv[0]); |
---|
[388] | 104 | return 1; |
---|
| 105 | } |
---|
[381] | 106 | } |
---|
| 107 | |
---|
[388] | 108 | if(optind >= argc) |
---|
[387] | 109 | { |
---|
[388] | 110 | printf("%s: too few arguments\n", argv[0]); |
---|
[391] | 111 | printf(MOREINFO, argv[0]); |
---|
[388] | 112 | return 1; |
---|
[387] | 113 | } |
---|
| 114 | |
---|
[388] | 115 | for(; optind < argc; optind++) |
---|
[387] | 116 | { |
---|
[388] | 117 | char *input = argv[optind], *result; |
---|
| 118 | struct image *img; |
---|
[402] | 119 | int count; |
---|
[388] | 120 | |
---|
| 121 | img = image_load(argv[optind]); |
---|
| 122 | if(!img) |
---|
| 123 | { |
---|
[392] | 124 | dprintf("cannot load %s\n", input); |
---|
[388] | 125 | printf("\n"); |
---|
| 126 | continue; |
---|
| 127 | } |
---|
| 128 | |
---|
[402] | 129 | count = filter_count(img); |
---|
| 130 | dprintf("image size %ix%i, %i colours\n", |
---|
| 131 | img->width, img->height, count); |
---|
[401] | 132 | |
---|
[389] | 133 | if(!strcmp(mode, "test")) |
---|
| 134 | result = decode_test(img); |
---|
[424] | 135 | else if(!strcmp(mode, "authimage")) |
---|
| 136 | result = decode_authimage(img); |
---|
[426] | 137 | else if(!strcmp(mode, "clubic")) |
---|
| 138 | result = decode_clubic(img); |
---|
[419] | 139 | else if(!strcmp(mode, "linuxfr")) |
---|
| 140 | result = decode_linuxfr(img); |
---|
[389] | 141 | else if(!strcmp(mode, "phpbb")) |
---|
| 142 | result = decode_phpbb(img); |
---|
[400] | 143 | else if(!strcmp(mode, "scode")) |
---|
| 144 | result = decode_scode(img); |
---|
[389] | 145 | else if(!strcmp(mode, "slashdot")) |
---|
| 146 | result = decode_slashdot(img); |
---|
[414] | 147 | else if(!strcmp(mode, "vbulletin")) |
---|
| 148 | result = decode_vbulletin(img); |
---|
[444] | 149 | else if(!strcmp(mode, "xanga")) |
---|
| 150 | result = decode_xanga(img); |
---|
[389] | 151 | else |
---|
| 152 | { |
---|
[424] | 153 | if(img->width == 155 && img->height == 50) |
---|
[392] | 154 | { |
---|
[424] | 155 | dprintf("autodetected authimage captcha\n"); |
---|
| 156 | result = decode_authimage(img); |
---|
| 157 | } |
---|
| 158 | else if(img->width == 100 && img->height == 40 && count < 6) |
---|
| 159 | { |
---|
[419] | 160 | dprintf("autodetected linuxfr captcha\n"); |
---|
| 161 | result = decode_linuxfr(img); |
---|
| 162 | } |
---|
| 163 | else if(img->width == 320 && img->height == 50) |
---|
| 164 | { |
---|
[408] | 165 | dprintf("autodetected phpBB captcha\n"); |
---|
[389] | 166 | result = decode_phpbb(img); |
---|
[392] | 167 | } |
---|
[444] | 168 | else if(img->width == 170 && img->height == 50) |
---|
| 169 | { |
---|
| 170 | dprintf("autodetected Xanga captcha\n"); |
---|
| 171 | result = decode_xanga(img); |
---|
| 172 | } |
---|
[424] | 173 | else if(img->height <= 40 && count < 10) |
---|
[400] | 174 | { |
---|
[424] | 175 | dprintf("autodetected scode/trencaspammers captcha\n"); |
---|
[400] | 176 | result = decode_scode(img); |
---|
| 177 | } |
---|
[426] | 178 | else if(img->height <= 30 && count < 100) |
---|
| 179 | { |
---|
| 180 | dprintf("autodetected clubic captcha\n"); |
---|
| 181 | result = decode_clubic(img); |
---|
| 182 | } |
---|
[389] | 183 | else if(img->height == 69) |
---|
[392] | 184 | { |
---|
[408] | 185 | dprintf("autodetected slashdot captcha\n"); |
---|
[389] | 186 | result = decode_slashdot(img); |
---|
[392] | 187 | } |
---|
[414] | 188 | else if(img->height == 61) |
---|
| 189 | { |
---|
| 190 | dprintf("autodetected vbulletin captcha\n"); |
---|
| 191 | result = decode_vbulletin(img); |
---|
| 192 | } |
---|
[389] | 193 | else |
---|
| 194 | { |
---|
[392] | 195 | dprintf("could not guess captcha type\n"); |
---|
[389] | 196 | printf("\n"); |
---|
| 197 | image_free(img); |
---|
| 198 | continue; |
---|
| 199 | } |
---|
| 200 | } |
---|
| 201 | |
---|
| 202 | image_free(img); |
---|
| 203 | |
---|
[388] | 204 | if(!result) |
---|
| 205 | { |
---|
[392] | 206 | dprintf("sorry, decoding failed\n"); |
---|
[388] | 207 | printf("\n"); |
---|
| 208 | continue; |
---|
| 209 | } |
---|
| 210 | |
---|
| 211 | printf("%s\n", result); |
---|
| 212 | free(result); |
---|
[387] | 213 | } |
---|
[381] | 214 | |
---|
| 215 | return 0; |
---|
| 216 | } |
---|
| 217 | |
---|
[392] | 218 | void dprintf(const char *fmt, ...) |
---|
| 219 | { |
---|
| 220 | va_list args; |
---|
| 221 | |
---|
| 222 | if(!debug) |
---|
| 223 | return; |
---|
| 224 | |
---|
| 225 | va_start(args, fmt); |
---|
| 226 | fprintf(stderr, "%s: ", argv0); |
---|
| 227 | vfprintf(stderr, fmt, args); |
---|
| 228 | va_end(args); |
---|
| 229 | } |
---|
| 230 | |
---|