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

Pages: 1-

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-09 11:22

-1, Not JavaScript

Name: Anonymous 2012-07-09 12:57

>>2
Nigga, u serious?

Name: Anonymous 2012-07-09 18:29

>>1
Why do you malloc a string of constant length?

Name: Anonymous 2012-07-09 18:33

malloc(3 * sizeof(char));
printf("mango");

Name: Anonymous 2012-07-09 19:25

This fails with tripcodes that contain one of the characters &, <, > or ". It also fails for typical UTF-8 input since you don't translate to SJIS.

ver2 will be a threaded trip code explorer
It's an embarrassingly parallel task so you could just add a fork call in there and now you're utilizing all the cores.

ver3 will be a CUDA trip code explorer
On current hardware this is not feasible due to lack of registers, try searching "crypt on cuda" or "des on cuda", results have been abysmal with typical speedup of just 2 over a single cpu core.

Name: Anonymous 2012-07-09 19:40

On current hardware this is not feasible due to lack of registers

Not on a SPARC bitch

Name: Anonymous 2012-07-09 20:35

Use C++. This will be a horrible, slow mess in C.

Name: Anonymous 2012-07-09 22:39

>>4
Why do you bully malloc()?

Name: Anonymous 2012-07-09 23:08

>>9
There is no reason at all to malloc a 3 byte string, you are stupid

Name: Anonymous 2012-07-09 23:26

check

Name: Anonymous 2012-07-10 0:11

>>11
Dubz acknowledged, carry on. Over.`

>2012
>not using the greatest language in the world, C++

Name: Anonymous 2012-07-10 0:24

>>4
>>5
Salt was originally a dynamically allocated string because I thought the length of salt depended on the length of the input. When I realised this wasn't so I just modified the malloc statement. Lazy of me.

>printf("mango");
That's just some stupid debug code.

>>6
I'm aware of the special character and UTF-8 encoding shortcomings. Any ideas about how to use UTF-8 in C and converting SJIS?

Name: Anonymous 2012-07-10 1:21

>>3
i'm sure he is

javascript is the future; you're doing yourself a disservice by not using it exclusively

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);
    }
}

Name: Anonymous 2012-07-10 2:02

Why the openmp doesn't work on GPUs?

Name: Anonymous 2012-07-10 2:12

>>15
PIG DISGUSTING!
Thanks for reminding me that if it ain't Lisp, it's crap.

Name: Anonymous 2012-07-10 2:19

>>17
caadaddadddaddaddadaddadddr

Name: Anonymous 2012-07-10 3:55

>>7
Not on a SPARC bitch
Do you even know what CUDA is?

Name: Anonymous 2012-07-10 3:59

>>13
Any ideas about how to use UTF-8 in C and converting SJIS?
Use iconv.

>>16
Why the openmp doesn't work on GPUs?
Check out mint, it translates annotated C (like openmp) to CUDA. Seems to work well enough on stencil methods at least, I don't know if it has been used on other stuff.

Name: Anonymous 2012-07-10 4:19

>>16
But just accelerating that outer loop on the GPU wouldn't work (at least not efficiently) in this case, you have to implement some kind of bitslice scheme but as stated earlier the current hardware simply doesn't have enough registers/fast enough shared memory for this to efficient.

Name: Anonymous 2012-07-10 4:57

>>15
OP here. Thanks for posting that. It has given me a few ideas on how to implement a few missing features.
I'm not a fan of using programming idioms. It makes the code too dense, though this might be because I'm not an expert.

Name: Anonymous 2012-07-10 11:15

>>19
no :(

Name: Anonymous 2012-07-10 12:11

Ver. \inf will be a quantum trip code explorer.

Name: Nyuu~ 2012-07-10 13:54

Kouda~!

Name: Anonymous 2012-07-10 14:42

>not using OpenCL

Name: Anonymous 2012-07-10 15:20

`
>not going back to /g/

Name: Ordinary Western Wizard !ZmaRIsaGOY 2012-07-12 11:28

http://pastebin.com/mEqCcaLx
Why is this so slow? It barely checks over 1 millions of tripcodes per second on my AMD64 i5 2500 system running glibc 2.15 and Linux 3.3.7. Could I get a huge speedup by using a specialised crypt function?

Name: Anonymous 2012-07-12 11:37

>>28
You are using a specialized crypt function. If you mean using the kernel's crypto stuff, that won't give you jack schitt. Seeing as how tripcodes used DES, I bet someone with an EE background could make a deep cracker.

Name: Anonymous 2012-07-12 11:58

>>29
This may surprise you, but !MILKRIBS4k was actually the director of the EFF.

Name: Anonymous 2012-07-12 12:38

>>28
You're not using a bitslicing algorithm.

Name: Anonymous 2012-07-12 13:37

>>28
asking why a program runs slow
Oh right, performance profilers are probably non-free.

Name: Anonymous 2012-07-12 14:03

>>32
Fuck you Visual Studio faggot.

Name: Anonymous 2012-07-12 14:19

>>32
sysprof is free, and awesome.

Name: Anonymous 2012-07-12 16:50

>>32-34
Profiling won't tell you much in this case, of course the bottleneck is going to be the crypt, he could profile the shit out of DES_fcrypt but the algorithm itself is fundamentally broken. Even if he changes to a modern algorithm it will still be the bottleneck.

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