<commit_before>/*=                     -*- c-basic-offset: 4; indent-tabs-mode: nil; -*-
 *
 * libhsync -- the library for network deltas
 * $Id$
 * 
 * Copyright (C) 1999, 2000, 2001 by <NAME> <<EMAIL>>
 * Copyright (C) 1999 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.
 */


/*
 * readsums.c -- Load signatures from a file into an ::hs_signature_t.
 */

#include <config.h>

#include <assert.h>

#ifdef HAVE_STDINT_H
#include <stdint.h>
#endif

#include <sys/types.h>
#include <limits.h>
#include <inttypes.h>
#include <stdlib.h>

#include "hsync.h"
#include "sumset.h"
#include "job.h"
#include "trace.h"
#include "netint.h"
#include "protocol.h"
#include "util.h"


static hs_result hs_loadsig_s_weak(hs_job_t *job);
static hs_result hs_loadsig_s_strong(hs_job_t *job);



/**
 * Add a just-read-in checksum pair to the signature block.
 */
static hs_result hs_loadsig_add_sum(hs_job_t *job, uint32_t weak,
                                    char const *strong)
{
    size_t              new_size;
    hs_signature_t      *sig = job->signature;
    hs_block_sig_t      *asignature;

    sig->count++;
    new_size = sig->count * sizeof(hs_block_sig_t);

    sig->block_sigs = realloc(sig->block_sigs, new_size);
    
    if (sig->block_sigs == NULL) {
        return HS_MEM_ERROR;
    }
    asignature = &(sig->block_sigs[sig->count - 1]);

    asignature->weak_sum = weak;
    asignature->i = sig->count;

    memcpy(asignature->strong_sum, strong, sig->strong_sum_len);

    return HS_RUNNING;
}


static hs_result hs_loadsig_s_weak(hs_job_t *job)
{
    int                 l;
    hs_result           result;

    if ((result = hs_suck_n32(job->stream, &l)) != HS_DONE)
        return result;

    job->weak_sig = l;

    job->statefn = hs_loadsig_s_strong;

    return HS_RUNNING;
}


static hs_result hs_loadsig_s_strong(hs_job_t *job)
{
    return HS_UNIMPLEMENTED;
}


static hs_result hs_loadsig_s_stronglen(hs_job_t *job)
{
    int                 l;
    hs_result           result;

    if ((result = hs_suck_n32(job->stream, &l)) != HS_DONE)
        return result;
    job->strong_sum_len = l;
    
    if (l < 0  ||  l > HS_MD4_LENGTH) {
        hs_error("strong sum length %d is implausible", l);
        return HS_CORRUPT;
    }

    job->signature = hs_alloc_struct(hs_signature_t);
    job->signature->block_len = job->block_len;
    
    hs_trace("allocated sigset_t (strong_sum_len=%d, block_len=%d)",
             job->strong_sum_len, job->block_len);

    job->statefn = hs_loadsig_s_weak;
    
    return HS_RUNNING;
}


static hs_result hs_loadsig_s_blocklen(hs_job_t *job)
{
    int                 l;
    hs_result           result;

    if ((result = hs_suck_n32(job->stream, &l)) != HS_DONE)
        return result;
    job->block_len = l;<commit_msg>Also load strong signatures into signature set. Show block signatures as they're loaded.<commit_after>/*=                     -*- c-basic-offset: 4; indent-tabs-mode: nil; -*-
 *
 * libhsync -- the library for network deltas
 * $Id$
 * 
 * Copyright (C) 1999, 2000, 2001 by <NAME> <<EMAIL>>
 * Copyright (C) 1999 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.
 */


/*
 * readsums.c -- Load signatures from a file into an ::hs_signature_t.
 */

#include <config.h>

#include <assert.h>

#ifdef HAVE_STDINT_H
#include <stdint.h>
#endif

#include <sys/types.h>
#include <limits.h>
#include <inttypes.h>
#include <stdlib.h>

#include "hsync.h"
#include "sumset.h"
#include "job.h"
#include "trace.h"
#include "netint.h"
#include "protocol.h"
#include "util.h"
#include "stream.h"


static hs_result hs_loadsig_s_weak(hs_job_t *job);
static hs_result hs_loadsig_s_strong(hs_job_t *job);



/**
 * Add a just-read-in checksum pair to the signature block.
 */
static hs_result hs_loadsig_add_sum(hs_job_t *job, hs_strong_sum_t *strong)
{
    size_t              new_size;
    hs_signature_t      *sig = job->signature;
    hs_block_sig_t      *asignature;
    char                hexbuf[HS_MD4_LENGTH * 2 + 2];

    sig->count++;
    new_size = sig->count * sizeof(hs_block_sig_t);

    sig->block_sigs = realloc(sig->block_sigs, new_size);
    
    if (sig->block_sigs == NULL) {
        return HS_MEM_ERROR;
    }
    asignature = &(sig->block_sigs[sig->count - 1]);

    asignature->weak_sum = job->weak_sig;
    asignature->i = sig->count;

    memcpy(asignature->strong_sum, strong, sig->strong_sum_len);
    hs_hexify(hexbuf, strong, sig->strong_sum_len);

    hs_trace("read in checksum: weak=%#x, strong=%s", asignature->weak_sum,
             hexbuf);

    return HS_RUNNING;
}


static hs_result hs_loadsig_s_weak(hs_job_t *job)
{
    int                 l;
    hs_result           result;

    if ((result = hs_suck_n32(job->stream, &l)) != HS_DONE)
        return result;

    job->weak_sig = l;

    job->statefn = hs_loadsig_s_strong;

    return HS_RUNNING;
}



static hs_result hs_loadsig_s_strong(hs_job_t *job)
{
    hs_result           result;
    hs_strong_sum_t     *strongsum;

    result = hs_scoop_read(job->stream, job->signature->strong_sum_len,
                           (void **) &strongsum);
    if (result != HS_DONE) return result;

    job->statefn = hs_loadsig_s_weak;

    return hs_loadsig_add_sum(job, strongsum);
}



static hs_result hs_loadsig_s_stronglen(hs_job_t *job)
{
    int                 l;
    hs_result           result;

    if ((result = hs_suck_n32(job->stream, &l)) != HS_DONE)
        return result;
    job->strong_sum_len = l;
    
    if (l < 0  ||  l > HS_MD4_LENGTH) {
        hs_error("strong sum length %d is implausible", l);
        return HS_CORRUPT;
    }

    job->signature = hs_alloc_struct(hs_signature_t);
    job->signature->block_len = job->block_len;
    job->signature->strong_sum_len = job->strong_sum_len;
    
    hs_trace("allocated sigset_t (strong_sum_len=%d, block_len=%d)",
             job->strong_sum_len, job->block_len);

    job->statefn = hs_loadsig_s_weak;
    
    return HS_RUNNING;
}


static hs_result hs_loadsig_s_blocklen(hs_job_t *job)
{
    int                 l;
    hs_result           result;

    if ((result = hs_suck_n32(job->stream, &l)) != HS_DONE)
        return result;
    job->block_len = l;
{'fraction_non_alphanumeric': 0.11244555892833576, 'fraction_numerical': 0.010162333377326119, 'mean_word_length': 2.8663265306122447, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 95, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5035443', 'n_tokens_mistral': 2794, 'n_tokens_neox': 2582, 'n_words': 695}