What follows is the first almost useful C program I've successfully written since an Intro to Programming class in college. I'm hoping to slowly maybe work my way up to Objective-C/Cocoa development, but figured I'd start by re-teaching myself the basics. I write PHP for a living, so some things, particularly pointers, are kind of tough to get my head around.
The below is my attempt at implementing the standard tripcode algorithm. Here are some particular areas for which I'd like some (ugh) pointers:
- There's one compiler warning I couldn't get rid of. The line
char *tripped = tripify(argv[x]);
in themain
function causes "Passing argument 1 of 'tripify' discards qualifiers from pointer target type." What does this mean? I'm guessing maybe it means thatargv[x]
is a value, instead of a pointer to a value; but if that's the case, why does the code work anyway? - Does the
static
keyword do what I think it does - not initialize the variable on successive function calls, but just reuses the previous value? This is whatstatic
does in PHP, but I'm not sure if it's analogous. - Is there a better way to do the bit about replacing characters in the salt than how I did it? That bit strikes me as very awkward. Is there an equivalent of PHP's
strtr()
lurking about somewhere? - The actual tripcode algorithm is not very secure. It's a
bugfeature of the algorithm that I'm well aware of and that's not what I'm asking about here.
#include <stdio.h>
#include <unistd.h> // crypt()
#include <string.h> // strcat(), strncpy()
#include <stdlib.h> // calloc()
char *tripify (char *code) {
char *saltprep = calloc(255, sizeof(char));
// Set saltprep to the code appended with "H.."
strcat(saltprep, code);
strcat(saltprep, "H..");
// Get the second and third chars of the saltprep to use as salt
char *salt = calloc(2, sizeof(char));
strncpy(salt, saltprep + 1, 2);
// Replace certain characters in the salt with a corresponding letter
static char saltreplacefrom[13] = ":;<=>?@[\\]^_`";
static char saltreplaceto[13] = "ABCDEFGabcdef";
for (char replacex = 0; replacex < 13; replacex++) {
if (salt[0] == saltreplacefrom[replacex]) {
salt[0] = saltreplaceto[replacex];
}
if (salt[1] == saltreplacefrom[replacex]) {
salt[1] = saltreplaceto[replacex];
}
}
// Crypt the code with the salt
char *crypted = crypt(code, salt);
// Get the last ten characters of the crypted code
char *crypted_trimmed = calloc(10, sizeof(char));
strncpy(crypted_trimmed, crypted + 3, 10);
// Return
return crypted_trimmed;
}
int main (int argc, const char* argv[]) {
if (argc > 1) {
for (int x = 1; x < argc; x++) {
// Get tripcoded value
char *tripped = tripify(argv[x]);
// Print input followed by tripcoded value
printf("%s: %s\n", argv[x], tripped);
}
}
return 0;
}