<commit_before>/**
 * @file ccn_match.c
 * @brief Support for the match predicate between interest and content.
 * 
 * Part of the CCNx C Library.
 *
 * Copyright (C) 2008, 2009 Palo Alto Research Center, Inc.
 *
 * This library is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License version 2.1
 * as published by the Free Software Foundation.
 * This library 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 library;
 * if not, write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301 USA.
 */
#include <stdlib.h>
#include <string.h>
#include <ccn/bloom.h>
#include <ccn/ccn.h>
#include <ccn/charbuf.h>
#include <ccn/coding.h>
#include <ccn/digest.h>

/**
 * Compute the digest of the entire ContentObject if necessary,
 * caching the result in pc->digest, pc->digest_bytes.
 */
void
ccn_digest_ContentObject(const unsigned char *content_object,
                         struct ccn_parsed_ContentObject *pc)
{
    int res;
    struct ccn_digest *d = NULL;

    if (pc->magic < 20080000) abort();
    if (pc->digest_bytes == sizeof(pc->digest))
        return;
    if (pc->digest_bytes != 0) abort();
    d = ccn_digest_create(CCN_DIGEST_SHA256);
    ccn_digest_init(d);
    res = ccn_digest_update(d, content_object, pc->offset[CCN_PCO_E]);
    if (res < 0) abort();
    res = ccn_digest_final(d, pc->digest, sizeof(pc->digest));
    if (res < 0) abort();
    if (pc->digest_bytes != 0) abort();
    pc->digest_bytes = sizeof(pc->digest);
    ccn_digest_destroy(&d);
}

static int
ccn_pubid_matches(const unsigned char *content_object,
                  struct ccn_parsed_ContentObject *pc,
                  const unsigned char *interest_msg,
                  const struct ccn_parsed_interest *pi)
{
    struct ccn_buf_decoder decoder;
    struct ccn_buf_decoder *d;
    int pubidstart;
    int pubidbytes;
    int contentpubidstart = 0;
    int contentpubidbytes = 0;
    pubidstart = pi->offset[CCN_PI_B_PublisherIDKeyDigest];
    pubidbytes = pi->offset[CCN_PI_E_PublisherIDKeyDigest] - pubidstart;
    if (pubidbytes > 0) {
        d = ccn_buf_decoder_start(&decoder,
                                  content_object +
                                   pc->offset[CCN_PCO_B_PublisherPublicKeyDigest],
                                  (pc->offset[CCN_PCO_E_PublisherPublicKeyDigest] -
                                   pc->offset[CCN_PCO_B_PublisherPublicKeyDigest]));
        ccn_buf_advance(d);
        if (ccn_buf_match_some_blob(d)) {
            contentpubidstart = d->decoder.token_index;
            ccn_buf_advance(d);
            contentpubidbytes = d->decoder.token_index - contentpubidstart;
        }
        if (pubidbytes != contentpubidbytes)
            return(0); // This is fishy
        if (0 != memcmp(interest_msg + pubidstart,
                        d->buf + contentpubidstart,
                        pubidbytes))
            return(0);
    }
    return(1);
}

/**
 * Test for a match between a ContentObject and an Interest
 *
 * @param content_object        ccnb-encoded ContentObject
 * @param content_object_size   its size in bytes
 * @param implicit_content_digest boolean indicating whether the
 *                              final name component is implicit (as in
 *                              the on-wire format) or explicit (as within
 *                              ccnd's content store).
 * @param pc                    Valid parse information may be provided to
 *                              speed things up. If NULL it will be
 *                              reconstructed internally.
 * @param interest_msg          ccnb-encoded Interest
 * @param interest_msg_size     its size in bytes
 * @param pi                    see _pc_
 *
 * @result 1 if the ccnb-encoded content_object matches the 
 *           ccnb-encoded interest_msg, otherwise 0.
 */
int
ccn_content_matches_interest(const unsigned char *content_object,
                             size_t content_object_size,
                             int implicit_content_digest,
                             struct ccn_parsed_ContentObject *pc,
                             const unsigned char *interest_msg,
                             size_t interest_msg_size,
                             const struct ccn_parsed_interest *pi)
{
    struct ccn_parsed_ContentObject pc_store;
    struct ccn_parsed_interest pi_store;
    int res;
    int ncomps;
    int prefixstart;
    int prefixbytes;
    int namecompstart;
    int namecompbytes;
    int checkdigest = 0;
    struct ccn_buf_decoder decoder;
    struct ccn_buf_decoder *d;
    const unsigned char *nextcomp;
    size_t nextcomp_size = 0;
    const unsigned char *comp;
    size_t comp_size = 0;
    const unsigned char *bloom;
    size_t bloom_size = 0;
    unsigned char match_any[2] = "-";
    if (pc == NULL) {
        res = ccn_parse_ContentObject(content_object, content_object_size,
                                      &pc_store, NULL);
        if (res < 0) return(0);
        pc = &pc_store;
    }
    if (pi == NULL) {
        res = ccn_parse_interest(interest_msg, interest_msg_size,
                                 &pi_store, NULL);<commit_msg>Refs #100634 Remove lint.  Suppress spurious uninitialized access warning with explicit initialization.<commit_after>/**
 * @file ccn_match.c
 * @brief Support for the match predicate between interest and content.
 * 
 * Part of the CCNx C Library.
 *
 * Copyright (C) 2008, 2009 Palo Alto Research Center, Inc.
 *
 * This library is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License version 2.1
 * as published by the Free Software Foundation.
 * This library 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 library;
 * if not, write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301 USA.
 */
#include <stdlib.h>
#include <string.h>
#include <ccn/bloom.h>
#include <ccn/ccn.h>
#include <ccn/charbuf.h>
#include <ccn/coding.h>
#include <ccn/digest.h>

/**
 * Compute the digest of the entire ContentObject if necessary,
 * caching the result in pc->digest, pc->digest_bytes.
 */
void
ccn_digest_ContentObject(const unsigned char *content_object,
                         struct ccn_parsed_ContentObject *pc)
{
    int res;
    struct ccn_digest *d = NULL;

    if (pc->magic < 20080000) abort();
    if (pc->digest_bytes == sizeof(pc->digest))
        return;
    if (pc->digest_bytes != 0) abort();
    d = ccn_digest_create(CCN_DIGEST_SHA256);
    ccn_digest_init(d);
    res = ccn_digest_update(d, content_object, pc->offset[CCN_PCO_E]);
    if (res < 0) abort();
    res = ccn_digest_final(d, pc->digest, sizeof(pc->digest));
    if (res < 0) abort();
    if (pc->digest_bytes != 0) abort();
    pc->digest_bytes = sizeof(pc->digest);
    ccn_digest_destroy(&d);
}

static int
ccn_pubid_matches(const unsigned char *content_object,
                  struct ccn_parsed_ContentObject *pc,
                  const unsigned char *interest_msg,
                  const struct ccn_parsed_interest *pi)
{
    struct ccn_buf_decoder decoder;
    struct ccn_buf_decoder *d;
    int pubidstart;
    int pubidbytes;
    int contentpubidstart = 0;
    int contentpubidbytes = 0;
    pubidstart = pi->offset[CCN_PI_B_PublisherIDKeyDigest];
    pubidbytes = pi->offset[CCN_PI_E_PublisherIDKeyDigest] - pubidstart;
    if (pubidbytes > 0) {
        d = ccn_buf_decoder_start(&decoder,
                                  content_object +
                                   pc->offset[CCN_PCO_B_PublisherPublicKeyDigest],
                                  (pc->offset[CCN_PCO_E_PublisherPublicKeyDigest] -
                                   pc->offset[CCN_PCO_B_PublisherPublicKeyDigest]));
        ccn_buf_advance(d);
        if (ccn_buf_match_some_blob(d)) {
            contentpubidstart = d->decoder.token_index;
            ccn_buf_advance(d);
            contentpubidbytes = d->decoder.token_index - contentpubidstart;
        }
        if (pubidbytes != contentpubidbytes)
            return(0); // This is fishy
        if (0 != memcmp(interest_msg + pubidstart,
                        d->buf + contentpubidstart,
                        pubidbytes))
            return(0);
    }
    return(1);
}

/**
 * Test for a match between a ContentObject and an Interest
 *
 * @param content_object        ccnb-encoded ContentObject
 * @param content_object_size   its size in bytes
 * @param implicit_content_digest boolean indicating whether the
 *                              final name component is implicit (as in
 *                              the on-wire format) or explicit (as within
 *                              ccnd's content store).
 * @param pc                    Valid parse information may be provided to
 *                              speed things up. If NULL it will be
 *                              reconstructed internally.
 * @param interest_msg          ccnb-encoded Interest
 * @param interest_msg_size     its size in bytes
 * @param pi                    see _pc_
 *
 * @result 1 if the ccnb-encoded content_object matches the 
 *           ccnb-encoded interest_msg, otherwise 0.
 */
int
ccn_content_matches_interest(const unsigned char *content_object,
                             size_t content_object_size,
                             int implicit_content_digest,
                             struct ccn_parsed_ContentObject *pc,
                             const unsigned char *interest_msg,
                             size_t interest_msg_size,
                             const struct ccn_parsed_interest *pi)
{
    struct ccn_parsed_ContentObject pc_store;
    struct ccn_parsed_interest pi_store;
    int res;
    int ncomps;
    int prefixstart;
    int prefixbytes;
    int namecompstart;
    int namecompbytes;
    int checkdigest = 0;
    struct ccn_buf_decoder decoder;
    struct ccn_buf_decoder *d;
    const unsigned char *nextcomp;
    size_t nextcomp_size = 0;
    const unsigned char *comp = NULL;
    size_t comp_size = 0;
    const unsigned char *bloom;
    size_t bloom_size = 0;
    unsigned char match_any[2] = "-";
    if (pc == NULL) {
        res = ccn_parse_ContentObject(content_object, content_object_size,
                                      &pc_store, NULL);
        if (res < 0) return(0);
        pc = &pc_store;
    }
    if (pi == NULL) {
        res = ccn_parse_interest(interest_msg, interest_msg_size,
                                 &pi_store, NULL);
{'fraction_non_alphanumeric': 0.07911534657916029, 'fraction_numerical': 0.009889418322395037, 'mean_word_length': 2.197470537510779, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 59, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5274461', 'n_tokens_mistral': 3357, 'n_tokens_neox': 3148, 'n_words': 949}