<commit_before>/*=                                     -*- c-file-style: "linux" -*-
 *
 * libhsync -- the library for network deltas
 * $Id$
 * 
 * Copyright (C) 2000 by <NAME> <<EMAIL>>
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */


                              /*
                               * This is Tranquility.
                               */


#include "config.h"

#include <assert.h>
#include <stdlib.h>
#include <stdio.h>

#include "hsync.h"
#include "util.h"
#include "trace.h"
#include "protocol.h"
#include "netint.h"
#include "command.h"
#include "prototab.h"


const int HS_PATCH_TAG = 201210;


enum hs_patch_state {
        hs_s_header,
        hs_s_run
};


struct hs_patch_job {
        int          dogtag;
	hs_stream_t *stream;
        int         (*statefn)(hs_patch_job_t *);
};



/* Local prototypes for state functions. */
static int _hs_patch_s_complete(hs_patch_job_t *UNUSED(job));



static void _hs_patch_check(const hs_patch_job_t *job)
{
        assert(job->dogtag == HS_PATCH_TAG);
}




/*
 * Called when we're trying to read the first byte of a command.  Once
 * we've taken that in, we can know how much data to read to get the
 * arguments.
 */
static int _hs_patch_s_cmd(hs_patch_job_t *job)
{
        int op;
        enum hs_result result;
        enum hs_op_kind kind;

        if ((result = _hs_suck_n8(job->stream, &op)) != HS_OK)
                return result;

        assert(op >= 0 && op <= 0xff);
        kind = _hs_prototab[op].kind;
        _hs_trace("got command byte 0x%02x (%s)", op, _hs_op_kind_name(kind));

        switch (kind) {
        case HS_KIND_EOF:
                job->statefn = _hs_patch_s_complete;
                return HS_OK;
                break;
        default:
                _hs_error("bogus command byte 0x%02x", op);
                return HS_BAD_MAGIC;
        }
}



/*
 * Called after encountering EOF on the patch.
 */
static int _hs_patch_s_complete(hs_patch_job_t *UNUSED(job))
{
        return HS_OK;
}


/*
 * Called while we're trying to read the header of the patch.
 */
static int _hs_patch_s_header(hs_patch_job_t *job)
{
        int v;
        int result;

        if ((result =_hs_suck_n32(job->stream, &v)) != HS_OK)
                return result;

        if (v != HS_DELTA_MAGIC)
                return HS_BAD_MAGIC;

        _hs_trace("got patch magic %#10x", v);

        job->statefn = _hs_patch_s_cmd;

        return HS_RUN_OK;
}

<commit_msg>Add a much cooler patch implementation; if we flesh out the rest of the state functions I think we will be there.<commit_after>/*=                                     -*- c-file-style: "linux" -*-
 *
 * libhsync -- the library for network deltas
 * $Id$
 * 
 * Copyright (C) 2000 by <NAME> <<EMAIL>>
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */


                              /*
                               * This is Tranquility.
                               */


#include "config.h"

#include <assert.h>
#include <stdlib.h>
#include <stdio.h>

#include "hsync.h"
#include "util.h"
#include "trace.h"
#include "protocol.h"
#include "netint.h"
#include "command.h"
#include "prototab.h"
#include "stream.h"


const int HS_PATCH_TAG = 201210;


enum hs_patch_state {
        hs_s_header,
        hs_s_run
};


struct hs_patch_job {
        int          dogtag;
	hs_stream_t *stream;
        enum hs_result (*statefn)(hs_patch_job_t *);

        /* Command byte currently being processed, if any, and lengths
         * of expected parameters. */
        int op, len1, len2;
        int param1, param2;

        enum hs_op_kind kind;
};



/* Local prototypes for state functions. */
static enum hs_result _hs_patch_s_complete(hs_patch_job_t *);
static enum hs_result _hs_patch_s_cmdbyte(hs_patch_job_t *);
static enum hs_result _hs_patch_s_params(hs_patch_job_t *);
static enum hs_result _hs_patch_s_run(hs_patch_job_t *);



static void _hs_patch_check(const hs_patch_job_t *job)
{
        assert(job->dogtag == HS_PATCH_TAG);
}




/*
 * Called when we're trying to read the first byte of a command.  Once
 * we've taken that in, we can know how much data to read to get the
 * arguments.
 */
static enum hs_result _hs_patch_s_cmdbyte(hs_patch_job_t *job)
{
        enum hs_result result;
        
        if ((result = _hs_suck_n8(job->stream, &job->op)) != HS_OK)
                return result;

        assert(job->op >= 0 && job->op <= 0xff);
        job->kind = _hs_prototab[job->op].kind;
        job->len1 = _hs_prototab[job->op].len_1;
        job->len2 = _hs_prototab[job->op].len_2;
        
        _hs_trace("got command byte 0x%02x (%s)", job->op,
                  _hs_op_kind_name(job->kind));

        if (job->len1)
                job->statefn = _hs_patch_s_params;
        else
                job->statefn = _hs_patch_s_run;

        return HS_OK;
}


/*
 * Called after reading a command byte to pull in its parameters and
 * then setup to execute the command.
 */
static enum hs_result _hs_patch_s_params(hs_patch_job_t *job)
{
        enum hs_result result;
        int len = job->len1 + job->len2;
        void *p;

        assert(len);

        result = _hs_scoop_readahead(job->stream, len, &p);
        if (result != HS_OK)
                return result;

        /* we now must have LEN bytes buffered */
        result = _hs_suck_netint(job->stream, job->len1, &job->param1);
        /* shouldn't fail, since we already checked */
        assert(result == HS_OK);

        if (job->len2) {
                result = _hs_suck_netint(job->stream, job->len2, &job->param2);
                assert(result == HS_OK);
        }

        job->statefn = _hs_patch_s_run;

        return HS_OK;
}



/*
 * Called when we've read in the whole command and we need to execute it.
 */
static enum hs_result _hs_patch_s_run(hs_patch_job_t *job)
{
        switch (job->kind) {
        case HS_KIND_EOF:
                job->statefn = _hs_patch_s_complete;
                return HS_OK;
        default:
                _hs_error("bogus command 0x%02x", job->op);
                return HS_BAD_MAGIC;
        }
}



/*
 * Called after encountering EOF on the patch.
 */
static enum hs_result _hs_patch_s_complete(hs_patch_job_t *UNUSED(job))
{
        return HS_OK;
}


/*
 * Called while we're trying to read the header of the patch.
 */
static enum hs_result _hs_patch_s_header(hs_patch_job_t *job)
{
        int v;
        int result;

        if ((result =_hs_suck_n32(job->stream, &v)) != HS_OK)
                return result;

        if (v != HS_DELTA_MAGIC)
                return HS_BAD_MAGIC;

        _hs_trace("got patch magic %#10x", v);

        job->statefn = _hs_patch_s_cmdbyte;

        return HS_RUN_OK;
}


{'fraction_non_alphanumeric': 0.09821428571428571, 'fraction_numerical': 0.010168650793650794, 'mean_word_length': 2.2678282009724473, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 54, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6127563', 'n_tokens_mistral': 2760, 'n_tokens_neox': 2540, 'n_words': 795}