[382] | 1 | /* |
---|
| 2 | * main.c: main function |
---|
| 3 | * $Id: main.c 414 2005-01-04 17:07:22Z 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; |
---|
| 29 | int debug = 1; |
---|
| 30 | |
---|
[381] | 31 | int main(int argc, char *argv[]) |
---|
| 32 | { |
---|
[388] | 33 | char *mode = "auto"; |
---|
[400] | 34 | int c; |
---|
| 35 | int digit_optind = 0; |
---|
[381] | 36 | |
---|
[392] | 37 | argv0 = argv[0]; |
---|
| 38 | |
---|
[388] | 39 | for(;;) |
---|
[381] | 40 | { |
---|
[388] | 41 | int this_option_optind = optind ? optind : 1; |
---|
[391] | 42 | #ifdef HAVE_GETOPT_LONG |
---|
[388] | 43 | int option_index = 0; |
---|
| 44 | static struct option long_options[] = |
---|
| 45 | { |
---|
| 46 | { "mode", 1, 0, 'm' }, |
---|
| 47 | { "help", 0, 0, 'h' }, |
---|
[392] | 48 | { "quiet", 0, 0, 'q' }, |
---|
[388] | 49 | { "version", 0, 0, 'v' }, |
---|
| 50 | { 0, 0, 0, 0 } |
---|
| 51 | }; |
---|
| 52 | |
---|
[392] | 53 | c = getopt_long(argc, argv, "hm:qv", long_options, &option_index); |
---|
[391] | 54 | #else |
---|
[392] | 55 | c = getopt(argc, argv, "hm:qv"); |
---|
[391] | 56 | #endif |
---|
[388] | 57 | if(c == -1) |
---|
| 58 | break; |
---|
| 59 | |
---|
| 60 | switch(c) |
---|
| 61 | { |
---|
| 62 | case 'h': /* --help */ |
---|
| 63 | printf("Usage: %s [OPTION]... FILE...\n", argv[0]); |
---|
[391] | 64 | #ifdef HAVE_GETOPT_LONG |
---|
[388] | 65 | printf(" -m, --mode force operating mode\n"); |
---|
[392] | 66 | printf(" -q, --quiet do not print information messages\n"); |
---|
[388] | 67 | printf(" -h, --help display this help and exit\n"); |
---|
| 68 | printf(" -v, --version output version information and exit\n"); |
---|
[391] | 69 | #else |
---|
| 70 | printf(" -m force operating mode\n"); |
---|
[392] | 71 | printf(" -q do not print information messages\n"); |
---|
[391] | 72 | printf(" -h display this help and exit\n"); |
---|
| 73 | printf(" -v output version information and exit\n"); |
---|
| 74 | #endif |
---|
[388] | 75 | return 0; |
---|
| 76 | case 'm': /* --mode */ |
---|
| 77 | mode = optarg; |
---|
| 78 | break; |
---|
[392] | 79 | case 'q': /* --quiet */ |
---|
| 80 | debug = 0; |
---|
| 81 | break; |
---|
[388] | 82 | case 'v': /* --version */ |
---|
[391] | 83 | printf("pwntcha (captcha decoder) %s\n", VERSION); |
---|
[388] | 84 | printf("Written by Sam Hocevar.\n"); |
---|
| 85 | printf("\n"); |
---|
| 86 | printf("Copyright (C) 2004-2005 Sam Hocevar <sam@zoy.org>\n"); |
---|
| 87 | printf("This is free software; see the source for copying conditions. There is NO\n"); |
---|
| 88 | printf("warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n"); |
---|
| 89 | return 0; |
---|
| 90 | case '?': |
---|
[391] | 91 | printf(MOREINFO, argv[0]); |
---|
| 92 | return 1; |
---|
[388] | 93 | default: |
---|
| 94 | printf("%s: invalid option -- %i\n", argv[0], c); |
---|
[391] | 95 | printf(MOREINFO, argv[0]); |
---|
[388] | 96 | return 1; |
---|
| 97 | } |
---|
[381] | 98 | } |
---|
| 99 | |
---|
[388] | 100 | if(optind >= argc) |
---|
[387] | 101 | { |
---|
[388] | 102 | printf("%s: too few arguments\n", argv[0]); |
---|
[391] | 103 | printf(MOREINFO, argv[0]); |
---|
[388] | 104 | return 1; |
---|
[387] | 105 | } |
---|
| 106 | |
---|
[388] | 107 | for(; optind < argc; optind++) |
---|
[387] | 108 | { |
---|
[388] | 109 | char *input = argv[optind], *result; |
---|
| 110 | struct image *img; |
---|
[402] | 111 | int count; |
---|
[388] | 112 | |
---|
| 113 | img = image_load(argv[optind]); |
---|
| 114 | if(!img) |
---|
| 115 | { |
---|
[392] | 116 | dprintf("cannot load %s\n", input); |
---|
[388] | 117 | printf("\n"); |
---|
| 118 | continue; |
---|
| 119 | } |
---|
| 120 | |
---|
[402] | 121 | count = filter_count(img); |
---|
| 122 | dprintf("image size %ix%i, %i colours\n", |
---|
| 123 | img->width, img->height, count); |
---|
[401] | 124 | |
---|
[389] | 125 | if(!strcmp(mode, "test")) |
---|
| 126 | result = decode_test(img); |
---|
| 127 | else if(!strcmp(mode, "phpbb")) |
---|
| 128 | result = decode_phpbb(img); |
---|
[400] | 129 | else if(!strcmp(mode, "scode")) |
---|
| 130 | result = decode_scode(img); |
---|
[389] | 131 | else if(!strcmp(mode, "slashdot")) |
---|
| 132 | result = decode_slashdot(img); |
---|
[414] | 133 | else if(!strcmp(mode, "vbulletin")) |
---|
| 134 | result = decode_vbulletin(img); |
---|
[389] | 135 | else |
---|
| 136 | { |
---|
| 137 | if(img->width == 320 && img->height == 50) |
---|
[392] | 138 | { |
---|
[408] | 139 | dprintf("autodetected phpBB captcha\n"); |
---|
[389] | 140 | result = decode_phpbb(img); |
---|
[392] | 141 | } |
---|
[402] | 142 | else if((img->height == 25 || img->height == 30) |
---|
| 143 | && count < 10) |
---|
[400] | 144 | { |
---|
[408] | 145 | dprintf("autodetected scode captcha\n"); |
---|
[400] | 146 | result = decode_scode(img); |
---|
| 147 | } |
---|
[389] | 148 | else if(img->height == 69) |
---|
[392] | 149 | { |
---|
[408] | 150 | dprintf("autodetected slashdot captcha\n"); |
---|
[389] | 151 | result = decode_slashdot(img); |
---|
[392] | 152 | } |
---|
[414] | 153 | else if(img->height == 61) |
---|
| 154 | { |
---|
| 155 | dprintf("autodetected vbulletin captcha\n"); |
---|
| 156 | result = decode_vbulletin(img); |
---|
| 157 | } |
---|
[389] | 158 | else |
---|
| 159 | { |
---|
[392] | 160 | dprintf("could not guess captcha type\n"); |
---|
[389] | 161 | printf("\n"); |
---|
| 162 | image_free(img); |
---|
| 163 | continue; |
---|
| 164 | } |
---|
| 165 | } |
---|
| 166 | |
---|
| 167 | image_free(img); |
---|
| 168 | |
---|
[388] | 169 | if(!result) |
---|
| 170 | { |
---|
[392] | 171 | dprintf("sorry, decoding failed\n"); |
---|
[388] | 172 | printf("\n"); |
---|
| 173 | continue; |
---|
| 174 | } |
---|
| 175 | |
---|
| 176 | printf("%s\n", result); |
---|
| 177 | free(result); |
---|
[387] | 178 | } |
---|
[381] | 179 | |
---|
| 180 | return 0; |
---|
| 181 | } |
---|
| 182 | |
---|
[392] | 183 | void dprintf(const char *fmt, ...) |
---|
| 184 | { |
---|
| 185 | va_list args; |
---|
| 186 | |
---|
| 187 | if(!debug) |
---|
| 188 | return; |
---|
| 189 | |
---|
| 190 | va_start(args, fmt); |
---|
| 191 | fprintf(stderr, "%s: ", argv0); |
---|
| 192 | vfprintf(stderr, fmt, args); |
---|
| 193 | va_end(args); |
---|
| 194 | } |
---|
| 195 | |
---|