Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Code critique

Name: Anonymous 2012-07-09 10:29

A tripcode calculator for 4chan (ver 0.5)

criticisms and suggestions welcome
http://pastebin.com/wHGMwJdB

ver2 will be a threaded trip code explorer
ver3 will be a CUDA trip code explorer

Name: Anonymous 2012-07-10 2:00

Your code in idiomatic style, written by a C master.

#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define __USE_GNU
#include <crypt.h>
#undef __USE_GNU

static char swap[128];
static bool sub[128];
static size_t sublen[128];
static char *subtext[128];

static void maketables() {
    sub['&'] = true;
    sublen['&'] = 5;
    subtext['&'] = "&amp;";
   
    sub['\"'] = true;
    sublen['\"'] = 6;
    subtext['\"'] = "&quot;";
   
    sub['\''] = true;
    sublen['\''] = 5;
    subtext['\''] = "'";
   
    sub['<'] = true;
    sublen['<'] = 4;
    subtext['<'] = "&lt;";
   
    sub['>'] = true;
    sublen['>'] = 4;
    subtext['>'] = "&gt;";

    static char *chars = "./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz";
    static char *swapin = ":;<=>?@[\\]^_`";
    static char *swapout = "ABCDEFGabcdef";
    for(int i = 0; i < 128; i++) swap[i] = i;
    for(int i = 0; i < 128; i++) if(!strchr(chars, i)) swap[i] = '.';
    for(size_t i = 0; swapin[i]; i++) swap[swapin[i]] = swapout[i];
}

bool tripcalc(char *in, char out[10]) {
    static bool tables;
    if(!tables && ++tables) maketables();

    size_t length = 0;
    for(size_t i = 0; in[i]; i++) {
        if(!sub[in[i]]) length++;
        else length += sublen[in[i]];
    }
    char *sub = malloc(length);
    if(!sub) return false;
    size_t pos = 0;
    for(size_t i = 0; in[i]; i++) {
        if(!sub[in[i]]) sub[pos++] = in[i];
        else {
            memcpy(sub + pos, subtext[in[i]], sublen[in[i]]);
            pos += sublen[in[i]];
        }
    }
   
    char salt[2] = "HH";
    if(sub[0] && sub[1] && sub[2]) memcpy(salt, sub + 1, 2);
    else if(sub[0] && sub[1]) salt[0] = sub[1];
    else salt[1] = '.';
    salt[0] = swap[salt[0]];
    salt[1] = swap[salt[1]];
   
    struct crypt_data data;
    data.initialized = 0;
    char *trip = crypt_r(sub, salt, &data);
    free(sub);
    if(!trip) return false;
    memcpy(out, trip + strlen(trip) - 10, 10);
    return true;
}

int main(int argc, char **argv) {
    static char trip[10];
#pragma omp parallel for
    for(int i = 1; i < argc; i++) {
        if(tripcalc(argv[i], trip)) printf("%s:%.10s\n", argv[i], trip);
    }
}

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List