int PhraseAdd(uint32_t action, const char *phrase, const char *reason) { LPPHRASE lpPhrase; lpPhrase = malloc(sizeof(PHRASE)); strncpy(lpPhrase->phrase, phrase, sizeof(lpPhrase->phrase)); lpPhrase->phrase[sizeof(lpPhrase->phrase) - 1] = 0; strncpy(lpPhrase->reason, reason, sizeof(lpPhrase->reason)); lpPhrase->reason[sizeof(lpPhrase->reason) - 1] = 0; switch (action) { case 'KCIK': //kick lpPhrase->action = ACT_KICK; break; case 'NAB': //ban lpPhrase->action = ACT_BAN; break; case 'TIHS': //shit lpPhrase->action = ACT_SHIT; break; case 'YAS': //say lpPhrase->action = ACT_SAY; break; default: return 0; //printf("unsupported action \'%s\'\n", asdf); } phrases = RadixInsert(phrases, phrase, lpPhrase, 0); nphrases++; return 1; }
/* Procedure om een radix search trie (ja dat klopt!) te bouwen */ void RadixInsert(unsigned short v, unsigned short i, unsigned short b, struct node **p) { struct node *x; if (!*p) { *p = (struct node *)MALLOC(sizeof(struct node)); if (*p) { (*p)->t = nLeaf; (*p)->key = v; (*p)->info = i; (*p)->l = NULL; (*p)->r = NULL; } } else if ((*p)->t == nBranch) { if (v & 1 << b) RadixInsert(v, i, b - 1, &(*p)->r); else RadixInsert(v, i, b - 1, &(*p)->l); } else { x = (struct node *)CALLOC(sizeof(struct node), 1); if (x) { x->t = nBranch; if ((*p)->key & 1 << b) x->r = *p; else x->l= *p; *p = x; RadixInsert(v, i, b, &x); } } } /* RadixInsert */
long DecodeHuffman(const void *inData, void **outData) { struct node *root, *n; unsigned short *code; unsigned char *ascii, *sp, *dp; long i, j, l; struct HuffmanHeader *h; h = (struct HuffmanHeader *)inData; h->hSourceLength = ntohl(h->hSourceLength); h->hCompressedLength = ntohl(h->hCompressedLength); h->hCodeCount = ntohs(h->hCodeCount); *outData = NULL; /* Check de header, we willen geen bogus data decoderen... */ if (h->hVersion != kVersion) return -1; /* pointers naar de verschillende data onderdelen */ code = (unsigned short *)((char *)inData + kHuffmanHeaderSize); ascii = (unsigned char *)(code + h->hCodeCount); /* Bouw de decodeer Trie, dit is een radix search trie */ root = NULL; for (i = 0; i < h->hCodeCount; i++) RadixInsert(ntohs(code[i]), ascii[i], 15, &root); /* Initialiseer de data */ sp = ascii + h->hCodeCount; dp = (unsigned char *)MALLOC(h->hSourceLength); if (dp) *outData = dp; else return -1; /* en start decoderen */ l = 0; n = root; for (i = 0; i < h->hCompressedLength && l < h->hSourceLength; i++) { for (j = 7; j >= 0; j--) { n = (sp[i] & (1 << j)) ? n->r : n->l; if (!n) { FREE(dp); *outData = NULL; return -1; } if (n->t == nLeaf) { if (l < h->hSourceLength) { dp[l] = n->info; if (++l == h->hSourceLength) break; n = root; } } } } DeleteTrie(root); return h->hSourceLength; } /* DecodeHuffman */