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 | */ |
---|
11 | |
---|
12 | #include <stdio.h> |
---|
13 | #include <stdlib.h> |
---|
14 | #include <string.h> |
---|
15 | #include <getopt.h> |
---|
16 | #include <stdarg.h> |
---|
17 | |
---|
18 | #include "config.h" |
---|
19 | #include "common.h" |
---|
20 | |
---|
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 | |
---|
27 | /* Used for the debug messages */ |
---|
28 | char *argv0 = NULL; |
---|
29 | char *share = NULL; |
---|
30 | int debug = 1; |
---|
31 | |
---|
32 | int main(int argc, char *argv[]) |
---|
33 | { |
---|
34 | char *mode = "auto"; |
---|
35 | int c; |
---|
36 | int digit_optind = 0; |
---|
37 | |
---|
38 | argv0 = argv[0]; |
---|
39 | share = "share"; |
---|
40 | |
---|
41 | for(;;) |
---|
42 | { |
---|
43 | int this_option_optind = optind ? optind : 1; |
---|
44 | #ifdef HAVE_GETOPT_LONG |
---|
45 | int option_index = 0; |
---|
46 | static struct option long_options[] = |
---|
47 | { |
---|
48 | { "help", 0, 0, 'h' }, |
---|
49 | { "mode", 1, 0, 'm' }, |
---|
50 | { "share", 1, 0, 's' }, |
---|
51 | { "quiet", 0, 0, 'q' }, |
---|
52 | { "version", 0, 0, 'v' }, |
---|
53 | { 0, 0, 0, 0 } |
---|
54 | }; |
---|
55 | |
---|
56 | c = getopt_long(argc, argv, "hm:s:qv", long_options, &option_index); |
---|
57 | #else |
---|
58 | c = getopt(argc, argv, "hm:s:qv"); |
---|
59 | #endif |
---|
60 | if(c == -1) |
---|
61 | break; |
---|
62 | |
---|
63 | switch(c) |
---|
64 | { |
---|
65 | case 'h': /* --help */ |
---|
66 | printf("Usage: %s [OPTION]... FILE...\n", argv[0]); |
---|
67 | #ifdef HAVE_GETOPT_LONG |
---|
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"); |
---|
73 | #else |
---|
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"); |
---|
79 | #endif |
---|
80 | return 0; |
---|
81 | case 'm': /* --mode */ |
---|
82 | mode = optarg; |
---|
83 | break; |
---|
84 | case 'q': /* --quiet */ |
---|
85 | debug = 0; |
---|
86 | break; |
---|
87 | case 's': /* --quiet */ |
---|
88 | share = optarg; |
---|
89 | break; |
---|
90 | case 'v': /* --version */ |
---|
91 | printf("pwntcha (captcha decoder) %s\n", VERSION); |
---|
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 '?': |
---|
99 | printf(MOREINFO, argv[0]); |
---|
100 | return 1; |
---|
101 | default: |
---|
102 | printf("%s: invalid option -- %i\n", argv[0], c); |
---|
103 | printf(MOREINFO, argv[0]); |
---|
104 | return 1; |
---|
105 | } |
---|
106 | } |
---|
107 | |
---|
108 | if(optind >= argc) |
---|
109 | { |
---|
110 | printf("%s: too few arguments\n", argv[0]); |
---|
111 | printf(MOREINFO, argv[0]); |
---|
112 | return 1; |
---|
113 | } |
---|
114 | |
---|
115 | for(; optind < argc; optind++) |
---|
116 | { |
---|
117 | char *input = argv[optind], *result; |
---|
118 | struct image *img; |
---|
119 | int count; |
---|
120 | |
---|
121 | img = image_load(argv[optind]); |
---|
122 | if(!img) |
---|
123 | { |
---|
124 | dprintf("cannot load %s\n", input); |
---|
125 | printf("\n"); |
---|
126 | continue; |
---|
127 | } |
---|
128 | |
---|
129 | count = filter_count(img); |
---|
130 | dprintf("image size %ix%i, %i colours\n", |
---|
131 | img->width, img->height, count); |
---|
132 | |
---|
133 | if(!strcmp(mode, "test")) |
---|
134 | result = decode_test(img); |
---|
135 | else if(!strcmp(mode, "authimage")) |
---|
136 | result = decode_authimage(img); |
---|
137 | else if(!strcmp(mode, "clubic")) |
---|
138 | result = decode_clubic(img); |
---|
139 | else if(!strcmp(mode, "linuxfr")) |
---|
140 | result = decode_linuxfr(img); |
---|
141 | else if(!strcmp(mode, "phpbb")) |
---|
142 | result = decode_phpbb(img); |
---|
143 | else if(!strcmp(mode, "scode")) |
---|
144 | result = decode_scode(img); |
---|
145 | else if(!strcmp(mode, "slashdot")) |
---|
146 | result = decode_slashdot(img); |
---|
147 | else if(!strcmp(mode, "vbulletin")) |
---|
148 | result = decode_vbulletin(img); |
---|
149 | else if(!strcmp(mode, "xanga")) |
---|
150 | result = decode_xanga(img); |
---|
151 | else |
---|
152 | { |
---|
153 | if(img->width == 155 && img->height == 50) |
---|
154 | { |
---|
155 | dprintf("autodetected authimage captcha\n"); |
---|
156 | result = decode_authimage(img); |
---|
157 | } |
---|
158 | else if(img->width == 100 && img->height == 40 && count < 6) |
---|
159 | { |
---|
160 | dprintf("autodetected linuxfr captcha\n"); |
---|
161 | result = decode_linuxfr(img); |
---|
162 | } |
---|
163 | else if(img->width == 320 && img->height == 50) |
---|
164 | { |
---|
165 | dprintf("autodetected phpBB captcha\n"); |
---|
166 | result = decode_phpbb(img); |
---|
167 | } |
---|
168 | else if(img->width == 170 && img->height == 50) |
---|
169 | { |
---|
170 | dprintf("autodetected Xanga captcha\n"); |
---|
171 | result = decode_xanga(img); |
---|
172 | } |
---|
173 | else if(img->height <= 40 && count < 10) |
---|
174 | { |
---|
175 | dprintf("autodetected scode/trencaspammers captcha\n"); |
---|
176 | result = decode_scode(img); |
---|
177 | } |
---|
178 | else if(img->height <= 30 && count < 100) |
---|
179 | { |
---|
180 | dprintf("autodetected clubic captcha\n"); |
---|
181 | result = decode_clubic(img); |
---|
182 | } |
---|
183 | else if(img->height == 69) |
---|
184 | { |
---|
185 | dprintf("autodetected slashdot captcha\n"); |
---|
186 | result = decode_slashdot(img); |
---|
187 | } |
---|
188 | else if(img->height == 61) |
---|
189 | { |
---|
190 | dprintf("autodetected vbulletin captcha\n"); |
---|
191 | result = decode_vbulletin(img); |
---|
192 | } |
---|
193 | else |
---|
194 | { |
---|
195 | dprintf("could not guess captcha type\n"); |
---|
196 | printf("\n"); |
---|
197 | image_free(img); |
---|
198 | continue; |
---|
199 | } |
---|
200 | } |
---|
201 | |
---|
202 | image_free(img); |
---|
203 | |
---|
204 | if(!result) |
---|
205 | { |
---|
206 | dprintf("sorry, decoding failed\n"); |
---|
207 | printf("\n"); |
---|
208 | continue; |
---|
209 | } |
---|
210 | |
---|
211 | printf("%s\n", result); |
---|
212 | free(result); |
---|
213 | } |
---|
214 | |
---|
215 | return 0; |
---|
216 | } |
---|
217 | |
---|
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 | |
---|