11 #define hash_init 5381
13 uint32_t
hash_djb2(
const uint8_t * str, uint32_t hash) {
17 hash = ((hash << 5) + hash) ^ c;
22 void usage(
const char * binname) {
23 printf(
"Usage: %s [-d <dir>] [outfile]\n", binname);
27 void processdir(DIR * dirp,
const char * curpath,
FILE * outfile,
const char * prefix) {
33 uint32_t size, w, hash;
37 while ((ent = readdir(dirp))) {
38 strcpy(fullpath, prefix);
39 strcat(fullpath,
"/");
40 strcat(fullpath, curpath);
41 strcat(fullpath, ent->d_name);
43 if (GetFileAttributes(fullpath) & FILE_ATTRIBUTE_DIRECTORY) {
45 if (ent->d_type == DT_DIR) {
47 if (strcmp(ent->d_name,
".") == 0)
49 if (strcmp(ent->d_name,
"..") == 0)
51 strcat(fullpath,
"/");
52 rec_dirp = opendir(fullpath);
53 processdir(rec_dirp, fullpath + strlen(prefix) + 1, outfile, prefix);
56 hash =
hash_djb2((
const uint8_t *) ent->d_name, cur_hash);
57 infile = fopen(fullpath,
"rb");
59 perror(
"opening input file");
62 b = (hash >> 0) & 0xff; fwrite(&b, 1, 1, outfile);
63 b = (hash >> 8) & 0xff; fwrite(&b, 1, 1, outfile);
64 b = (hash >> 16) & 0xff; fwrite(&b, 1, 1, outfile);
65 b = (hash >> 24) & 0xff; fwrite(&b, 1, 1, outfile);
69 b = (size >> 0) & 0xff; fwrite(&b, 1, 1, outfile);
70 b = (size >> 8) & 0xff; fwrite(&b, 1, 1, outfile);
71 b = (size >> 16) & 0xff; fwrite(&b, 1, 1, outfile);
72 b = (size >> 24) & 0xff; fwrite(&b, 1, 1, outfile);
74 w = size > 16 * 1024 ? 16 * 1024 : size;
75 w = fread(buf, 1, w, infile);
76 fwrite(buf, 1, w, outfile);
84 int main(
int argc,
char ** argv) {
85 char * binname = *argv++;
87 char * outname =
NULL;
93 while ((o = *argv++)) {
114 outfile = fopen(outname,
"wb");
117 perror(
"opening output file");
121 dirp = opendir(dirname);
123 perror(
"opening directory");
128 fwrite(&z, 1, 8, outfile);