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