#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

typedef unsigned long u32;
typedef unsigned char u8;

u32
myhash(char *s, u32 salt)
{
    salt |= (~salt << 16);
    while (*s) {
        salt = salt ^ ((salt << 7) + *s + (salt >> 5));
        ++s;
    }
    return salt;
}

#define SALT_BITS   12
#define BUCKET_BITS  4
#define MAX_BUCKETS 16
#define MAX_STRINGS 32
int numBuckets = 4;

char *OK[MAX_STRINGS];
char *NOT[MAX_STRINGS];
u32 numOK, numNOT;

u32 OKhash[MAX_STRINGS];
u32 NOThash[MAX_STRINGS];

u32 OKbuckets[MAX_BUCKETS];
u32 NOTbuckets[MAX_BUCKETS];
u32 results[MAX_BUCKETS];

int
firstBit(int x)
{
    int i;
    for (i = 0; i < 32; ++i) {
        if (x & (1 << i)) return i;
    }
    return -1;
}

void
readFile(char *filename, char *array[], u32 *n)
{
    FILE *fp;
    char buf[256];
    if ((fp = fopen(filename, "r")) == NULL) {
        perror("fopen");
        exit(-1);
    }
    while (fgets(buf, sizeof(buf), fp) != NULL) {
        char *p;
        if ((p = strchr(buf, '\n')) != NULL) {
            *p = 0;
        }
        array[*n] = strdup(buf);
        (*n)++;
    }
}


static void
__packBits(u32 offset, u32 bits, u32 value, u8 *bitfield)
{
    int i;

    for (i = 0; i < bits; ++i) {
        if (value & (1 << i)) {
            bitfield[offset >> 3] |= 1 << (offset & 7);
        }
        offset++;
    }
}

static u8 bitfield[32];

int
main(int argc, char *argv[])
{
    int salt;

    if (argc != 3) {
        fprintf(stderr, "Usage: %s <allow IPC> <not allow IPC>\n", argv[0]);
        return -1;
    }

    readFile(argv[1], OK, &numOK);
    readFile(argv[2], NOT, &numNOT);

    while(numBuckets < MAX_BUCKETS) {
        for (salt = 0; salt < (1 << SALT_BITS); ++salt) {
            int i, j;

            memset(OKbuckets, 0, sizeof(OKbuckets));
            memset(NOTbuckets, 0, sizeof(NOTbuckets));
            /* Hash the strings and place them in appropriate buckets */
            for (i = 0; i < numOK; ++i) {
                OKhash[i] = myhash(OK[i], salt);
                OKbuckets[OKhash[i] % numBuckets] |= 1 << i;
            }
            for (i = 0; i < numNOT; ++i) {
                NOThash[i] = myhash(NOT[i], salt);
                NOTbuckets[NOThash[i] % numBuckets] |= 1 << i;
            }

            /* For each bucket, try to find a single bit which determines the
               state of that bucket */
            for (i = 0; i < numBuckets; ++i) {
                u32 OKbits = 0xffffffff;
                for (j = 0; j < 32; ++j) {
                    if (OKbuckets[i] & (1LL << j)) {
                        OKbits &= OKhash[j];         
                    }
                }
                u32 NOTbits = 0;
                for (j = 0; j < 32; ++j) {
                    if (NOTbuckets[i] & (1LL << j)) {
                        NOTbits |= NOThash[j];         
                    }
                }

                if ((OKbits & NOTbits) != OKbits) {
                    results[i] = (OKbits & NOTbits) ^ OKbits;
                } else {
                    break;
                }
            }
            if (i == numBuckets) {
                fprintf(stderr, "found a salt: %08x %d buckets\n", salt, numBuckets);
                for (j = 0; j < numBuckets; ++j) {
                    fprintf(stderr, "[%d]: %08lx %d\n", j, results[j], firstBit(results[j]));
                }
                __packBits(0, SALT_BITS, salt, bitfield);
                __packBits(SALT_BITS, BUCKET_BITS, numBuckets, bitfield);
                for (j = 0; j < numBuckets; ++j) {
                    __packBits(SALT_BITS+BUCKET_BITS+5*j, 5, firstBit(results[j]), bitfield);
                }
                write(fileno(stdout), bitfield, sizeof(bitfield));
                return 0;
            }
        }
        numBuckets++;
    }
    
    return 0;
}
