Changeset 448


Ignore:
Timestamp:
01/10/05 16:31:33 (8 years ago)
Author:
sam
Message:
  • Use font_load_* for all decoders.
Location:
pwntcha/trunk/src
Files:
1 added
11 edited
1 moved

Legend:

Unmodified
Added
Removed
  • pwntcha/trunk/src/Makefile.am

    r445 r448  
    44pwntcha_SOURCES = \ 
    55    main.c \ 
     6    filter.c \ 
     7    font.c \ 
    68    image.c \ 
    7     filters.c \ 
    89    common.h \ 
    910    authimage.c \ 
  • pwntcha/trunk/src/authimage.c

    r445 r448  
    1919#include "common.h" 
    2020 
    21 #define FONTNAME "font_authimage.png" 
    22 static struct image *font = NULL; 
    23  
    2421/* Main function */ 
    2522char *decode_authimage(struct image *img) 
    2623{ 
    27     char *all = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; 
     24    static struct font *font = NULL; 
    2825    char *result; 
    2926    struct image *tmp; 
     
    3229    if(!font) 
    3330    { 
    34         char fontname[BUFSIZ]; 
    35         sprintf(fontname, "%s/%s", share, FONTNAME); 
    36         font = image_load(fontname); 
     31        font = font_load_fixed("font_authimage.png", 
     32                               "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"); 
    3733        if(!font) 
    38         { 
    39             fprintf(stderr, "cannot load font %s\n", fontname); 
    4034            exit(-1); 
    41         } 
    4235    } 
    4336 
     
    4639    memset(result, '\0', 7); 
    4740 
    48     /* half the captchas are inverse video; we set them back to normal */ 
     41    /* double the captcha size for better accuracy in the rotation */ 
    4942    tmp = image_dup(img); 
    5043    filter_scale(tmp, 2.0); 
     
    5245    filter_equalize(tmp, r * 3 / 4); 
    5346    filter_smooth(tmp); 
     47    filter_equalize(tmp, 220); 
    5448 
    5549    for(i = 0; i < 6; i++) 
    5650    { 
    5751        int mindiff = INT_MAX, minch = -1, ch; 
    58         for(ch = 0; ch < 36; ch++) 
     52        for(ch = 0; ch < font->size; ch++) 
    5953        { 
    6054            int diff = 0; 
     
    6761                    newy = 33.0 - (x + 6 * i) * 18.0 / 34.0 + y * 42.0 / 6.0 + 0.5; 
    6862                    getpixel(tmp, newx, newy, &r, &g, &b); 
    69                     getpixel(font, x + 6 * ch, y, &r2, &g, &b); 
    70                     r = (r < 220) ? 0 : 255; 
     63                    getpixel(font->img, x + 6 * ch, y, &r2, &g, &b); 
    7164                    diff += (r - r2) * (r - r2); 
    7265                } 
     
    7871            } 
    7972        } 
    80         result[i] = all[minch]; 
     73        result[i] = font->glyphs[minch].c; 
    8174    } 
    8275 
  • pwntcha/trunk/src/clubic.c

    r445 r448  
    2121 
    2222/* Our macros */ 
    23 #define FONTNAME "font_clubic.png" 
    24 static struct image *font = NULL; 
    2523char *result; 
    2624 
     
    2927{ 
    3028    struct image *tmp; 
    31  
    32     if(!font) 
    33     { 
    34         char fontname[BUFSIZ]; 
    35         sprintf(fontname, "%s/%s", share, FONTNAME); 
    36         font = image_load(fontname); 
    37         if(!font) 
    38         { 
    39             fprintf(stderr, "cannot load font %s\n", fontname); 
    40             exit(-1); 
    41         } 
    42     } 
    4329 
    4430    /* clubic captchas have 6 characters */ 
     
    5743static void find_glyphs(struct image *img) 
    5844{ 
    59     char all[] = "0123456789"; 
    60     struct 
    61     { 
    62         int xmin, xmax, ymin, ymax; 
    63         int count; 
    64     } 
    65     glyphs[10]; 
     45    static struct font *font = NULL; 
    6646    struct image *tmp; 
    6747    int x, y, i = 0; 
    6848    int r, g, b; 
    69     int xmin, xmax, ymin, ymax, incell = 0, count = 0, startx = 0, cur = 0; 
     49    int xmin, xmax, ymin, ymax, startx = 0, cur = 0; 
    7050    int distmin, distx, disty, distch; 
     51 
     52    if(!font) 
     53    { 
     54        font = font_load_variable("font_clubic.png", "0123456789"); 
     55        if(!font) 
     56            exit(1); 
     57    } 
    7158 
    7259    tmp = image_new(img->width, img->height); 
     
    7966        } 
    8067 
    81     for(x = 0; x < font->width; x++) 
    82     { 
    83         int found = 0; 
    84         for(y = 0; y < font->height; y++) 
    85         { 
    86             getpixel(font, x, y, &r, &g, &b); 
    87             if(r < 128) 
    88             { 
    89                 found = 1; 
    90                 count += (255 - r); 
    91             } 
    92         } 
    93         if(found && !incell) 
    94         { 
    95             incell = 1; 
    96             xmin = x; 
    97         } 
    98         else if(!found && incell) 
    99         { 
    100             incell = 0; 
    101             xmax = x; 
    102             ymin = 0; 
    103             ymax = font->height; 
    104             glyphs[i].xmin = xmin; 
    105             glyphs[i].xmax = xmax; 
    106             glyphs[i].ymin = ymin; 
    107             glyphs[i].ymax = ymax; 
    108             glyphs[i].count = count; 
    109             count = 0; 
    110             i++; 
    111         } 
    112     } 
    113  
    114     if(i != 10) 
    115     { 
    116         printf("error: could not find 10 glyphs in font\n"); 
    117         exit(-1); 
    118     } 
    119  
    12068    while(cur < 6) 
    12169    { 
    12270        /* Try to find 1st letter */ 
    12371        distmin = INT_MAX; 
    124         for(i = 0; i < 10; i++) 
     72        for(i = 0; i < font->size; i++) 
    12573        { 
    12674            int localmin = INT_MAX, localx, localy; 
    127             xmin = glyphs[i].xmin; 
    128             ymin = glyphs[i].ymin; 
    129             xmax = glyphs[i].xmax; 
    130             ymax = glyphs[i].ymax; 
     75            xmin = font->glyphs[i].xmin; 
     76            ymin = font->glyphs[i].ymin; 
     77            xmax = font->glyphs[i].xmax; 
     78            ymax = font->glyphs[i].ymax; 
    13179            for(y = -4; y < 4; y++) 
    13280                for(x = startx; x < startx + 4; x++) 
     
    13886                        { 
    13987                            int r2; 
    140                             getgray(font, xmin + z, ymin + t, &r); 
     88                            getgray(font->img, xmin + z, ymin + t, &r); 
    14189                            getgray(img, x + z, y + t, &r2); 
    14290                            dist += abs(r - r2); 
    14391                        } 
    144                     dist = dist * 128 / glyphs[i].count; 
     92                    dist = dist * 128 / font->glyphs[i].count; 
    14593                    if(dist < localmin) 
    14694                    { 
     
    160108 
    161109        /* Print min glyph */ 
    162         xmin = glyphs[distch].xmin; 
    163         ymin = glyphs[distch].ymin; 
    164         xmax = glyphs[distch].xmax; 
    165         ymax = glyphs[distch].ymax; 
     110        xmin = font->glyphs[distch].xmin; 
     111        ymin = font->glyphs[distch].ymin; 
     112        xmax = font->glyphs[distch].xmax; 
     113        ymax = font->glyphs[distch].ymax; 
    166114        for(y = 0; y < ymax - ymin; y++) 
    167115            for(x = 0; x < xmax - xmin; x++) 
    168116            { 
    169                 getpixel(font, xmin + x, ymin + y, &r, &g, &b); 
    170                 if(r > 128) continue; 
     117                getpixel(font->img, xmin + x, ymin + y, &r, &g, &b); 
     118                if(r > 128) 
     119                    continue; 
    171120                setpixel(tmp, distx + x, disty + y, r, g, b); 
    172121            } 
    173122 
    174123        startx = distx + xmax - xmin; 
    175         result[cur++] = all[distch]; 
     124        result[cur++] = font->glyphs[distch].c; 
    176125    } 
    177126 
  • pwntcha/trunk/src/common.h

    r446 r448  
    2626        int xmin, xmax, ymin, ymax; 
    2727        int count; /* Black pixel count */ 
     28        char c; 
    2829    } *glyphs; 
     30    int size; 
    2931}; 
    3032 
     
    5860int setpixel(struct image *img, int x, int y, int r, int g, int b); 
    5961 
     62/* font operations */ 
     63struct font *font_load_fixed(char *file, char *chars); 
     64struct font *font_load_variable(char *file, char *chars); 
     65void font_free(struct font *font); 
     66 
    6067/* image filters */ 
    6168void filter_flood_fill(struct image *img, int x, int y, int r, int g, int b); 
  • pwntcha/trunk/src/linuxfr.c

    r445 r448  
    1818#include "common.h" 
    1919 
    20 #define FONTNAME "font_linuxfr.png" 
    21 static struct image *font = NULL; 
    22  
    2320/* Main function */ 
    2421char *decode_linuxfr(struct image *img) 
    2522{ 
    26     char all[] = "abcdefghijklmnopqrstuvwxyz" 
    27                  "ABCDEFGHIJKLMNOPQRSTUVWXYZ" 
    28                  "0123456789"; 
     23    static struct font *font = NULL; 
    2924    char *result; 
    3025    struct image *tmp; 
    3126    int x, y, r, g, b, i, j, c; 
    32     int *stats = malloc(img->height * sizeof(int)); 
     27    int *stats; 
    3328 
    3429    if(!font) 
    3530    { 
    36         char fontname[BUFSIZ]; 
    37         sprintf(fontname, "%s/%s", share, FONTNAME); 
    38         font = image_load(fontname); 
     31        font = font_load_fixed("font_linuxfr.png", 
     32                               "abcdefghijklmnopqrstuvwxyz" 
     33                               "ABCDEFGHIJKLMNOPQRSTUVWXYZ" 
     34                               "0123456789"); 
    3935        if(!font) 
    40         { 
    41             fprintf(stderr, "cannot load font %s\n", fontname); 
    4236            exit(-1); 
    43         } 
    4437    } 
    4538 
     
    4740    result = malloc(8 * sizeof(char)); 
    4841    memset(result, '\0', 8); 
     42 
     43    stats = malloc(img->height * sizeof(int)); 
    4944 
    5045    tmp = image_dup(img); 
     
    115110                int r2, g2, b2, ch; 
    116111                int minerror = INT_MAX; 
    117                 for(ch = 0; ch < 62; ch++) 
     112                for(ch = 0; ch < font->size; ch++) 
    118113                { 
    119114                    int error = 0, goodch = 1; 
     
    122117                        { 
    123118                            getpixel(tmp, x + c * 9 + i, y + j, &r, &g, &b); 
    124                             getpixel(font, ch * 9 + i, j, &r2, &g2, &b2); 
     119                            getpixel(font->img, ch * 9 + i, j, &r2, &g2, &b2); 
    125120                            /* Only die if font is black and image is white */ 
    126121                            if(r > r2) 
     
    135130                    { 
    136131                        minerror = error; 
    137                         result[c] = all[ch]; 
     132                        result[c] = font->glyphs[ch].c; 
    138133                        result[c+1] = '\0'; 
    139134                    } 
  • pwntcha/trunk/src/main.c

    r444 r448  
    6464        { 
    6565        case 'h': /* --help */ 
    66             printf("Usage: %s [OPTION]... FILE...\n", argv[0]); 
     66            printf("Usage: %s [OPTION]... IMAGE...\n", argv[0]); 
    6767#ifdef HAVE_GETOPT_LONG 
    6868            printf("  -m, --mode <mode>  force operating mode\n"); 
  • pwntcha/trunk/src/phpbb.c

    r445 r448  
    1818#include "common.h" 
    1919 
    20 /* Our macros */ 
    21 #define FONTNAME "font_phpbb.png" 
    22 static struct image *font = NULL; 
    23  
    2420/* Main function */ 
    2521char *decode_phpbb(struct image *img) 
    2622{ 
    27     char all[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ123456789"; 
     23    static struct font *font = NULL; 
    2824    char *result; 
    2925    struct image *tmp1, *tmp2; 
     
    3531    if(!font) 
    3632    { 
    37         char fontname[BUFSIZ]; 
    38         sprintf(fontname, "%s/%s", share, FONTNAME); 
    39         font = image_load(fontname); 
     33        font = font_load_fixed("font_phpbb.png", 
     34                               "ABCDEFGHIJKLMNOPQRSTUVWXYZ123456789"); 
    4035        if(!font) 
    41         { 
    42             fprintf(stderr, "cannot load font %s\n", fontname); 
    4336            exit(-1); 
    44         } 
    4537    } 
    4638 
     
    6961        /* Try to find 1st letter */ 
    7062        distmin = INT_MAX; 
    71         for(i = 0; i < 35; i++) 
     63        for(i = 0; i < font->size; i++) 
    7264        { 
    7365            int localmin = INT_MAX, localx, localy; 
    74             xmin = i * 40; 
    75             ymin = 0; 
    76             xmax = i * 40 + 40; 
    77             ymax = 40; 
    78             for(y = 0; y < img->height - 40; y++) 
     66            xmin = font->glyphs[i].xmin; 
     67            ymin = font->glyphs[i].ymin; 
     68            xmax = font->glyphs[i].xmax; 
     69            ymax = font->glyphs[i].ymax; 
     70            for(y = 0; y < img->height - (ymax - ymin); y++) 
    7971            { 
    8072                x = offset - 3; 
     
    9183                        { 
    9284                            int r2; 
    93                             getgray(font, xmin + z, ymin + t, &r); 
     85                            getgray(font->img, xmin + z, ymin + t, &r); 
    9486                            getgray(tmp1, x + z, y + t, &r2); 
    9587                            if(r > r2) 
     
    116108 
    117109        /* Print min glyph (debug) */ 
    118         xmin = distch * 40; 
    119         ymin = 0; 
    120         xmax = distch * 40 + 40; 
    121         ymax = 40; 
     110        xmin = font->glyphs[distch].xmin; 
     111        ymin = font->glyphs[distch].ymin; 
     112        xmax = font->glyphs[distch].xmax; 
     113        ymax = font->glyphs[distch].ymax; 
    122114        for(y = 0; y < ymax - ymin; y++) 
    123115            for(x = 0; x < xmax - xmin; x++) 
    124116            { 
    125117                int r2; 
    126                 getpixel(font, xmin + x, ymin + y, &r2, &g, &b); 
    127                 if(r2 > 128) continue; 
     118                getpixel(font->img, xmin + x, ymin + y, &r2, &g, &b); 
     119                if(r2 > 128) 
     120                    continue; 
    128121                getpixel(tmp2, distx + x, disty + y, &r, &g, &b); 
    129122                setpixel(tmp2, distx + x, disty + y, r2, g, b); 
     
    131124 
    132125        offset = distx + xmax - xmin; 
    133         result[cur] = all[distch]; 
     126        result[cur] = font->glyphs[distch].c; 
    134127    } 
    135128 
  • pwntcha/trunk/src/slashdot.c

    r446 r448  
    2121static void count_objects(struct image *img); 
    2222static void rotate(struct image *img); 
    23 static void cut_cells(struct image *img); 
    2423static void find_glyphs(struct image *img); 
    25  
    26 /* Our macros */ 
    27 #define FONTNAME "font_slashdot.png" 
    28  
    29 struct font font; 
    30 struct glyph glyphs[22]; 
    3124 
    3225/* Global stuff */ 
     
    136129        } 
    137130    } 
    138  
    139 #if 0 
    140     { CvPoint A, B; 
    141       A.x = (objlist[first].xmin + objlist[first].xmax) / 2; 
    142       A.y = (objlist[first].ymin + objlist[first].ymax) / 2; 
    143       B.x = (objlist[last].xmin + objlist[last].xmax) / 2; 
    144       B.y = (objlist[last].ymin + objlist[last].ymax) / 2; 
    145       cvLine(tmp, A, B, 0, 2.0, 0); 
    146     } 
    147 #endif 
    148131 
    149132    image_swap(img, tmp); 
     
    196179} 
    197180 
    198 static void cut_cells(struct image *img) 
    199 { 
    200     struct image *tmp; 
    201     int x, y; 
    202     int r, g, b; 
    203  
    204     tmp = image_new(img->width, img->height); 
    205  
    206     for(y = 0; y < img->height; y++) 
    207         for(x = 0; x < img->width; x++) 
    208         { 
    209             getpixel(img, x, y, &r, &g, &b); 
    210             setpixel(tmp, x, y, r, g, b); 
    211         } 
    212  
    213     for(x = 0; x < img->width; x++) 
    214     { 
    215         setpixel(tmp, x, 0, 255, 255, 255); 
    216         setpixel(tmp, x, img->height - 1, 255, 255, 255); 
    217     } 
    218  
    219     for(y = 0; y < img->height; y++) 
    220         for(x = 0; x < 7; x++) 
    221         { 
    222             setpixel(tmp, x * img->width / 7, y, 255, 255, 255); 
    223             setpixel(tmp, (x + 1) * img->width / 7 - 1, y, 255, 255, 255); 
    224         } 
    225  
    226     image_swap(img, tmp); 
    227     image_free(tmp); 
    228 } 
    229  
    230181static void find_glyphs(struct image *img) 
    231182{ 
    232     char all[] = "abcdefgijkmnpqrstvwxyz"; 
     183    static struct font *font = NULL; 
    233184    struct image *tmp; 
    234185    int x, y, i = 0; 
    235186    int r, g, b; 
    236     int xmin, xmax, ymin, ymax, incell = 0, count = 0, startx = 0, cur = 0; 
     187    int xmin, xmax, ymin, ymax, startx = 0, cur = 0; 
    237188    int distmin, distx, disty, distch; 
    238189 
    239     if(!font.img) 
    240     { 
    241         char fontname[BUFSIZ]; 
    242         sprintf(fontname, "%s/%s", share, FONTNAME); 
    243         font.img = image_load(fontname); 
    244         if(!font.img) 
    245         { 
    246             fprintf(stderr, "cannot load font %s\n", fontname); 
    247             exit(-1); 
    248         } 
    249         font.glyphs = glyphs; 
     190    if(!font) 
     191    { 
     192        font = font_load_variable("font_slashdot.png", "abcdefgijkmnpqrstvwxyz"); 
     193        if(!font) 
     194            exit(1); 
    250195    } 
    251196 
     
    259204        } 
    260205 
    261     for(x = 0; x < font.img->width; x++) 
    262     { 
    263         int found = 0; 
    264         for(y = 0; y < font.img->height; y++) 
    265         { 
    266             getpixel(font.img, x, y, &r, &g, &b); 
    267             if(r < 128) 
    268             { 
    269                 found = 1; 
    270                 count += (255 - r); 
    271             } 
    272         } 
    273         if(found && !incell) 
    274         { 
    275             incell = 1; 
    276             xmin = x; 
    277         } 
    278         else if(!found && incell) 
    279         { 
    280             incell = 0; 
    281             xmax = x; 
    282 #if 0 
    283             ymin = font.img->height; 
    284             ymax = 0; 
    285             for(y = 0; y < font.img->height; y++) 
    286             { 
    287                 int newx; 
    288                 int gotit = 0; 
    289                 for(newx = xmin; newx < xmax; newx++) 
    290                 { 
    291                     getpixel(font.img, newx, y, &r, &g, &b); 
    292                     if(r < 128) 
    293                     { 
    294                         gotit = 1; 
    295                         break; 
    296                     } 
    297                 } 
    298                 if(gotit) 
    299                 { 
    300                     if(ymin > y) ymin = y; 
    301                     if(ymax <= y) ymax = y + 1; 
    302                 } 
    303             } 
    304 #else 
    305             ymin = 0; 
    306             ymax = font.img->height; 
    307 #endif 
    308             font.glyphs[i].xmin = xmin; 
    309             font.glyphs[i].xmax = xmax; 
    310             font.glyphs[i].ymin = ymin; 
    311             font.glyphs[i].ymax = ymax; 
    312             font.glyphs[i].count = count; 
    313             count = 0; 
    314             i++; 
    315         } 
    316     } 
    317  
    318     if(i != 22) 
    319     { 
    320         printf("error: could not find 22 glyphs in font\n"); 
    321         exit(-1); 
    322     } 
    323  
    324206    while(cur < 7) 
    325207    { 
    326208        /* Try to find 1st letter */ 
    327209        distmin = INT_MAX; 
    328         for(i = 0; i < 22; i++) 
     210        for(i = 0; i < font->size; i++) 
    329211        { 
    330212            int localmin = INT_MAX, localx, localy; 
    331 //if(all[i] == 'i') continue; 
    332             xmin = font.glyphs[i].xmin; 
    333             ymin = font.glyphs[i].ymin; 
    334             xmax = font.glyphs[i].xmax; 
    335             ymax = font.glyphs[i].ymax; 
    336             //printf("trying to find %c (%i×%i) - ", all[i], xmax - xmin, ymax - ymin); 
     213            xmin = font->glyphs[i].xmin; 
     214            ymin = font->glyphs[i].ymin; 
     215            xmax = font->glyphs[i].xmax; 
     216            ymax = font->glyphs[i].ymax; 
    337217            for(y = -5; y < 5; y++) 
    338218                for(x = startx - 5; x < startx + 5; x++) 
     
    344224                        { 
    345225                            int r2; 
    346                             getgray(font.img, xmin + z, ymin + t, &r); 
     226                            getgray(font->img, xmin + z, ymin + t, &r); 
    347227                            getgray(img, x + z, y + t, &r2); 
    348228                            dist += abs(r - r2); 
     
    350230    //                printf("%i %i -> %i\n", x, y, dist); 
    351231                    //dist /= sqrt(xmax - xmin); 
    352                     dist = dist * 128 / font.glyphs[i].count; 
     232                    dist = dist * 128 / font->glyphs[i].count; 
    353233                    if(dist < localmin) 
    354234                    { 
     
    368248        } 
    369249 
    370         //fprintf(stderr, "%i (%i,%i)\n", distmin, distx - startx, disty); 
    371         //printf("min diff: %c - %i (%i, %i)\n", all[distch], distmin, distx, disty); 
    372  
    373         /* Print min glyph */ 
    374         xmin = font.glyphs[distch].xmin; 
    375         ymin = font.glyphs[distch].ymin; 
    376         xmax = font.glyphs[distch].xmax; 
    377         ymax = font.glyphs[distch].ymax; 
     250        /* Draw best glyph in picture (debugging purposes) */ 
     251        xmin = font->glyphs[distch].xmin; 
     252        ymin = font->glyphs[distch].ymin; 
     253        xmax = font->glyphs[distch].xmax; 
     254        ymax = font->glyphs[distch].ymax; 
    378255        for(y = 0; y < ymax - ymin; y++) 
    379256            for(x = 0; x < xmax - xmin; x++) 
    380257            { 
    381                 getpixel(font.img, xmin + x, ymin + y, &r, &g, &b); 
     258                getpixel(font->img, xmin + x, ymin + y, &r, &g, &b); 
    382259                if(r > 128) continue; 
    383260                setpixel(tmp, distx + x, disty + y, r, g, b); 
     
    385262 
    386263        startx = distx + xmax - xmin; 
    387         result[cur++] = all[distch]; 
     264        result[cur++] = font->glyphs[distch].c; 
    388265    } 
    389266 
  • pwntcha/trunk/src/test.c

    r445 r448  
    1919#include "common.h" 
    2020 
    21 /* Our macros */ 
    22 #define FONTNAME "font_phpbb.png" 
    23  
    24 static void find_glyphs(struct image *img); 
    25  
    26 /* Global stuff */ 
    27 struct { int xmin, ymin, xmax, ymax; } objlist[100]; 
    28 int objects, first, last; 
    29 char *result; 
    30  
    3121/* Main function */ 
    3222char *decode_test(struct image *img) 
    3323{ 
     24    char *result; 
    3425    struct image *tmp; 
    35  
    36     /* Initialise local data */ 
    37     objects = 0; 
    38     first = -1; 
    39     last = -1; 
    4026 
    4127    /* phpBB captchas have 6 characters */ 
    4228    result = malloc(7 * sizeof(char)); 
     29    result[0] = 0; 
    4330 
    4431    tmp = image_dup(img); 
     
    4734    filter_equalize(tmp, 130); 
    4835    filter_median(tmp); 
    49     find_glyphs(tmp); 
    5036 
    5137    image_free(tmp); 
     
    5642/* The following functions are local */ 
    5743 
    58 static void find_glyphs(struct image *img) 
    59 { 
    60     char all[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ123456789"; 
    61     struct image *tmp, *font; 
    62     int x, y, i = 0; 
    63     int r, g, b; 
    64     int xmin, xmax, ymin, ymax, cur = 0, offset = -1; 
    65     int distmin, distx, disty, distch; 
    66  
    67     if(!font) 
    68     { 
    69         char fontname[BUFSIZ]; 
    70         sprintf(fontname, "%s/%s", share, FONTNAME); 
    71         font = image_load(fontname); 
    72         if(!font) 
    73         { 
    74             fprintf(stderr, "cannot load font %s\n", fontname); 
    75             exit(-1); 
    76         } 
    77     } 
    78  
    79     tmp = image_new(img->width, img->height); 
    80  
    81     for(x = 0; x < img->width; x++) 
    82         for(y = 0; y < img->height; y++) 
    83         { 
    84             getpixel(img, x, y, &r, &g, &b); 
    85             setpixel(tmp, x, y, 255, g, 255); 
    86             if(r == 0 && offset == -1) 
    87                 offset = x; 
    88         } 
    89  
    90     strcpy(result, "       "); 
    91  
    92     while(cur < 6) 
    93     { 
    94         /* Try to find 1st letter */ 
    95         distmin = INT_MAX; 
    96         for(i = 0; i < 35; i++) 
    97         { 
    98             int localmin = INT_MAX, localx, localy; 
    99             xmin = i * 40; 
    100             ymin = 0; 
    101             xmax = i * 40 + 40; 
    102             ymax = 40; 
    103             for(y = 0; y < img->height - 40; y++) 
    104             { 
    105                 x = offset - 5; 
    106                 if(cur == 0) 
    107                     x -= 15; 
    108                 if(x < 0) 
    109                     x = 0; 
    110                 for(; x < offset + 10; x++) 
    111                 { 
    112                     int z, t, dist; 
    113                     dist = 0; 
    114                     for(t = 0; t < ymax - ymin; t++) 
    115                         for(z = 0; z < xmax - xmin; z++) 
    116                         { 
    117                             int r2; 
    118                             getgray(font, xmin + z, ymin + t, &r); 
    119                             getgray(img, x + z, y + t, &r2); 
    120                             dist += abs(r - r2); 
    121                         } 
    122                     if(dist < localmin) 
    123                     { 
    124                         localmin = dist; 
    125                         localx = x; 
    126                         localy = y; 
    127                     } 
    128                 } 
    129             } 
    130             if(localmin < distmin) 
    131             { 
    132                 distmin = localmin; 
    133                 distx = localx; 
    134                 disty = localy; 
    135                 distch = i; 
    136             } 
    137         } 
    138  
    139         /* Print min glyph (debug) */ 
    140         xmin = distch * 40; 
    141         ymin = 0; 
    142         xmax = distch * 40 + 40; 
    143         ymax = 40; 
    144         for(y = 0; y < ymax - ymin; y++) 
    145             for(x = 0; x < xmax - xmin; x++) 
    146             { 
    147                 getpixel(font, xmin + x, ymin + y, &r, &g, &b); 
    148                 if(r > 128) continue; 
    149                 setpixel(tmp, distx + x, disty + y, r, g, b); 
    150             } 
    151  
    152         offset = distx + xmax - xmin; 
    153         result[cur++] = all[distch]; 
    154     } 
    155  
    156     image_swap(img, tmp); 
    157     image_free(tmp); 
    158 } 
    159  
  • pwntcha/trunk/src/vbulletin.c

    r445 r448  
    1818#include "common.h" 
    1919 
    20 #define FONTNAME "font_vbulletin.png" 
    21 static struct image *font = NULL; 
    22  
    2320/* Main function */ 
    2421char *decode_vbulletin(struct image *img) 
    2522{ 
    26     char all[] = "2346789ABCDEFGHJKLMNPRTWXYZ"; 
     23    static struct font *font = NULL; 
    2724    char *result; 
    2825    struct image *tmp; 
     
    3229    if(!font) 
    3330    { 
    34         char fontname[BUFSIZ]; 
    35         sprintf(fontname, "%s/%s", share, FONTNAME); 
    36         font = image_load(fontname); 
     31        font = font_load_fixed("font_vbulletin.png", 
     32                               "2346789ABCDEFGHJKLMNPRTWXYZ"); 
    3733        if(!font) 
    38         { 
    39             fprintf(stderr, "cannot load font %s\n", fontname); 
    4034            exit(-1); 
    41         } 
    4235    } 
    4336 
     
    115108    for(i = 0; i < 6; i++) 
    116109    { 
    117         struct image *new = image_dup(tmp); 
    118110        int mindist = INT_MAX, min = -1; 
    119         filter_crop(new, limits[i], 15, limits[i] + 11, 45); 
    120         for(j = 0; j < 27; j++) 
     111        for(j = 0; j < font->size; j++) 
    121112        { 
    122113            int dist = 0; 
    123             for(y = 0; y < new->height; y++) 
    124                 for(x = 0; x < new->width; x++) 
     114            for(y = 0; y < 11; y++) 
     115                for(x = 0; x < 30; x++) 
    125116                { 
    126117                    int r2, g2, b2; 
    127                     getpixel(font, 12 * j + x, y, &r, &g, &b); 
    128                     getpixel(new, x, y, &r2, &g2, &b2); 
     118                    getpixel(font->img, 12 * j + x, y, &r, &g, &b); 
     119                    getpixel(tmp, limits[i] + x, 15 + y, &r2, &g2, &b2); 
    129120                    dist += (r - r2) * (r - r2); 
    130121                } 
     
    135126            } 
    136127        } 
    137         image_free(new); 
    138         result[i] = all[min]; 
     128        result[i] = font->glyphs[min].c; 
    139129    } 
    140130 
  • pwntcha/trunk/src/xanga.c

    r445 r448  
    2121static void fill_white_holes(struct image *img); 
    2222 
    23 /* Our macros */ 
    24 #define FACTOR 1 
    25 #define FONTNAME "font_xanga.png" // use with FACTOR = 1 
    26 //#define FONTNAME "font.png" // use with FACTOR = 2 
    27 //#define FONTNAME "font_dilated.png" // use with FACTOR = 2 
    28 static struct image *font = NULL; 
    29  
    30 /* Global stuff */ 
    31 struct { int xmin, ymin, xmax, ymax; } objlist[100]; 
    32 int objects, first, last; 
    33 char *result; 
    34  
    3523/* Main function */ 
    3624char *decode_xanga(struct image *img) 
    3725{ 
     26    static struct font *font1 = NULL, *font2 = NULL, *font3 = NULL; 
    3827    struct image *tmp; 
     28    char *result; 
    3929 
    40     /* Initialise local data */ 
    41     objects = 0; 
    42     first = -1; 
    43     last = -1; 
     30    if(!font1) 
     31    { 
     32        font1 = font_load_variable("font_freemonobold_32_az.bmp", 
     33                                   "abcdefghijklmnopqrstuvwxyz"); 
     34        if(!font1) 
     35            exit(1); 
     36    } 
     37 
     38    if(!font2) 
     39    { 
     40        font2 = font_load_variable("font_freemonobold_32_az.bmp", 
     41                                   "abcdefghijklmnopqrstuvwxyz"); 
     42        if(!font2) 
     43            exit(1); 
     44    } 
     45 
     46    if(!font3) 
     47    { 
     48        font3 = font_load_variable("font_freemonobold_32_az.bmp", 
     49                                   "abcdefghijklmnopqrstuvwxyz"); 
     50        if(!font3) 
     51            exit(1); 
     52    } 
    4453 
    4554    /* Xanga captchas have 7 characters */ 
     
    6978 
    7079    /* Invert rotation and find glyphs */ 
    71     rotate(tmp); 
    7280    filter_median(tmp); 
    7381 
     
    8795{ 
    8896    struct image *tmp; 
    89     int x, y, i; 
     97    int x, y; 
    9098    int r, g, b; 
    9199 
Note: See TracChangeset for help on using the changeset viewer.