filter.old
author Lluís Batlle <viric@viric.name>
Thu, 20 Mar 2014 16:33:27 +0100
changeset 97 eea77d5a624c
parent 34 7486e269b794
permissions -rw-r--r--
Merging the savefile branch. I hope it works; I even don't remember it.

#include <stdlib.h>
#include <string.h>
#include "main.h"

#include "filter.h"

static struct FString new_fstring()
{
    fs->p = p;
    fs->len = len;
    fs->actual_pos = 0;
    fs->next_try = (int *) malloc(
            sizeof(*fs->next_try) * len);
}

static void prepare_next_try(char *str, int len, int *next_try)
{
    int s; /* skip */

    /* at x = next_try[s], it means how many x characters
     * have we matched, if we have matched (s+1)? */
    next_try[0] = 0; /* not used */
    for (s = 2; s <= len; ++s)
    {
        int i;
        /* compare [0..s-1] to [len - s, len-1] */
        /* strlen(cmp) = s */
        /* String compare, exit when not equal, offset in 'i' */
        for(i=0; i < s; ++i)
            if (str[len-s-i] != str[i])
                break;
        next_try[s-1] = i;
    }
}

void add_fstring(struct FilterRules *fr, char *p, int len, Fcallback cb)
{
    struct FString *fs;
    fr->nstrings += 1;
    fr->strings = (struct FString **) realloc(fr->strings,
            sizeof(*fr->strings) * fr->nstrings);
    if (fr->strings == 0) error("realloc failed.");

    /* Fill the last created space */
    fs = (struct FString *) malloc(sizeof(*fs));
    fr->strings[fr->nstrings-1] = fs;
    if (fs == 0) error("malloc failed.");

    new_fstring(fs, p, len);

    /* Prepare the next_try array */
    prepare_next_try(p, len, fs->next_try);

    fs->callback = cb;

    /* We will keep the fbuffers allocated for the longest
     * string possible */
    if (len > fr->fbuffer_allocated)
    {
        fr->fbuffer_allocated = len;
        fr->fbuffer = realloc(fr->fbuffer, len);
    }
}

struct FilterRules * new_filter_rules()
{
    struct FilterRules *fr;
    fr = (struct FilterRules *) malloc(sizeof(*fr));
    if (fr == 0) error("malloc failed.");

    fr->nstrings = 0;
    fr->strings = 0; /* null for realloc */
    fr->fbuffer = 0;
    fr->fbuffer_len = 0;
    fr->fbuffer_allocated = 0;

    return fr;
}

/* We want to access index even before the 'buffer' limits.
 * Those are at fr->fbuffer, and referred as -X indexes. */
static int get_char(struct FilterRules *fr, const unsigned char *buffer, int pos)
{
    if (pos >= 0)
        return buffer[pos];
    else
    {
        if (-pos > fr->fbuffer_len)
            return -1;
        return fr->fbuffer[fr->fbuffer_len - pos];
    }
}

static int set_char(struct FilterRules *fr, unsigned char *buffer, int pos, int c)
{
    if (pos >= 0)
    {
        buffer[pos] = c;
        return buffer[pos];
    }
    else
    {
        if (-pos > fr->fbuffer_len)
            return -1;
        fr->fbuffer[fr->fbuffer_len - pos] = c;
        return fr->fbuffer[fr->fbuffer_len - pos];
    }
}

/* Return 1 if matches */
static int filter_string(struct FilterRules *fr, struct FString *fs,
        char *obuf, int *olen, const unsigned char *buffer, int pos)
{
    while (fs->actual_pos > 0 && fs->p[fs->actual_pos] != buffer[pos])
    {
        /* next_try maps 1..len to 0..len-1 */
        fs->actual_pos = fs->next_try[fs->actual_pos-1];
    }
    if (fs->p[fs->actual_pos] == buffer[pos])
        fs->actual_pos += 1;
    if (fs->actual_pos == fs->len)
    {
        /* String matches */
        int shift;
        shift = pos - fs->len;
        /* Call callback */
        fs->callback(fr, fs, obuf, olen, buffer, shift);
        return 1;
    }
    return 0;
}

static void reset_actual_pos(struct FilterRules *fr)
{
    int i;
    for (i=0; i < fr->nstrings; ++i)
    {
            struct FString *fs;
            fs = fr->strings[i];
            fs->actual_pos = 0;
    }
}

void filter_stream(struct FilterRules *fr, char *obuf, int *olen, const char *buffer, int len)
{
    int i;

    int ipos;

    *olen = 0;
    ipos = - fr->fbuffer_len;
    for(i=0; i< len; ++i)
    { /* each char */
        int j;
        int old_maxlen = fr->maxlen_found;
        int found;
        fr->maxlen_found = 0;
        for (j=0; j < fr->nstrings; ++j)
        {
            struct FString *fs;
            fs = fr->strings[j];
            found = filter_string(fr, fs, obuf, olen, buffer, i);
            if (found)
                break;
            fr->maxlen_found = max(fr->maxlen_found, fs->actual_pos);
        }
        /* Some chars may not be needed for the filters
         * already. */
        if (found)
        {
            reset_actual_pos(fr);
        }
        else
        {
            for(j=0; j < old_maxlen + 1 - fr->maxlen_found; ++j)
            {
                obuf[(*olen)++] = get_char(fr, buffer, ipos++);
            }
        }
    }
    memcpy(fr->fbuffer, buffer + len - fr->maxlen_found, fr->maxlen_found);
    fr->fbuffer_len = fr->maxlen_found;
}

void filter_flush(struct FilterRules *fr, char *obuf, int *olen)
{
    memcpy(obuf, fr->fbuffer, fr->fbuffer_len);
    *olen = fr->fbuffer_len;
    fr->fbuffer_len = 0;
}