<commit_before>#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>

#include <ccn/ccn.h>
#include <ccn/bloom.h>
#include <ccn/uri.h>
#include <ccn/digest.h>
#include <ccn/keystore.h>
#include <ccn/signing.h>

struct path {
    int count;
    char * comps[];
};
struct path * path_create(char * strpath) {
    char * p = strpath;
    int slash_count = 0;
    int i = 0;
    struct path * path;

    if (strlen(strpath) < 1) {
	return NULL;
    }
    while (*p != '\0') {
	if (*p++ == '/') slash_count++;
    }
    path = malloc(sizeof(int) + sizeof(char *)*(slash_count+1));
    path->comps[0] = strtok(strdup(strpath), "/");
    path->count = 0;
    while (path->comps[i++]) {
	path->comps[i] = strtok(NULL, "/");
	path->count++;
    }
    return path;
}
void path_destroy(struct path **path) {
    free(*path);
    *path = NULL;
}

int 
encode_message(struct ccn_charbuf *message, struct path * name_path, char *data, size_t len, struct ccn_charbuf *signed_info, const void *pkey) {
    struct ccn_charbuf *path = ccn_charbuf_create();
    int i;
    int res;

    if (path == NULL || ccn_name_init(path) == -1) {
	fprintf(stderr, "Failed to allocate or initialize content path\n");
	return -1;
    }

    for (i = 0; i < name_path->count; i++) {
	ccn_name_append_str(path, name_path->comps[i]);
    }

    res = ccn_encode_ContentObject(message, path, signed_info, data, len, NULL, pkey);

    if (res != 0) {
	fprintf(stderr, "Failed to encode ContentObject\n");
    }

    ccn_charbuf_destroy(&path);
    return (res);
}

int 
decode_message(struct ccn_charbuf *message, struct path * name_path, char *data, size_t len, const void *verkey) {
    struct ccn_parsed_ContentObject content;
    struct ccn_indexbuf *comps = ccn_indexbuf_create();
    const unsigned char * content_value;
    char * s = NULL;
    size_t content_length;
    
    int res = 0;
    int i;
    
    memset(&content, 0x33, sizeof(content));

    if (ccn_parse_ContentObject(message->buf, message->length, &content, comps) != 0) {
	printf("Decode failed to parse object\n");
	res = -1;
    }

    if (comps->n-1 != name_path->count) {
	printf("Decode got wrong number of path components: %d vs. %d\n", 
	       (int)(comps->n-1), name_path->count);
	res = -1;
    }
    for (i=0; i<name_path->count; i++) {
	if (ccn_name_comp_strcmp(message->buf, comps, i, name_path->comps[i]) != 0) {
	    printf("Decode mismatch on path component %d\n", i);
	    res = -1;
	}
	s = ccn_name_comp_strdup(message->buf, comps, i);
	if (s == NULL || strcmp(s, name_path->comps[i]) != 0) {
	    printf("Decode mismatch on retrieved path component %d\n", i);
	}
	if (s != NULL) { free(s); s = NULL; }
    }
    if (ccn_content_get_value(message->buf, message->length, &content,
			      &content_value, &content_length) != 0) {
	printf("Cannot retrieve content value\n");
	res = -1;
    } else if (content_length != len) {
	printf("Decode mismatch on content length %d vs. %d\n", 
	       (int)content_length, (int)len);
	res = -1;
    } else if (memcmp(content_value, data, len) != 0) {
	printf("Decode mismatch of content\n");
	res = -1;
    }

    if (ccn_verify_signature(message->buf, message->length, &content, comps, verkey) != 1) {
        printf("Signature did not verify\n");
        res = -1;
    }

    ccn_indexbuf_destroy(&comps);
    return res;
    
}

int
expected_res(int res, char code)
{
    if (code == '*')
        return(1);
    if (code == '-')<commit_msg>Remove unnecessary argument from ccn_verify_signature<commit_after>#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>

#include <ccn/ccn.h>
#include <ccn/bloom.h>
#include <ccn/uri.h>
#include <ccn/digest.h>
#include <ccn/keystore.h>
#include <ccn/signing.h>

struct path {
    int count;
    char * comps[];
};
struct path * path_create(char * strpath) {
    char * p = strpath;
    int slash_count = 0;
    int i = 0;
    struct path * path;

    if (strlen(strpath) < 1) {
	return NULL;
    }
    while (*p != '\0') {
	if (*p++ == '/') slash_count++;
    }
    path = malloc(sizeof(int) + sizeof(char *)*(slash_count+1));
    path->comps[0] = strtok(strdup(strpath), "/");
    path->count = 0;
    while (path->comps[i++]) {
	path->comps[i] = strtok(NULL, "/");
	path->count++;
    }
    return path;
}
void path_destroy(struct path **path) {
    free(*path);
    *path = NULL;
}

int 
encode_message(struct ccn_charbuf *message, struct path * name_path, char *data, size_t len, struct ccn_charbuf *signed_info, const void *pkey) {
    struct ccn_charbuf *path = ccn_charbuf_create();
    int i;
    int res;

    if (path == NULL || ccn_name_init(path) == -1) {
	fprintf(stderr, "Failed to allocate or initialize content path\n");
	return -1;
    }

    for (i = 0; i < name_path->count; i++) {
	ccn_name_append_str(path, name_path->comps[i]);
    }

    res = ccn_encode_ContentObject(message, path, signed_info, data, len, NULL, pkey);

    if (res != 0) {
	fprintf(stderr, "Failed to encode ContentObject\n");
    }

    ccn_charbuf_destroy(&path);
    return (res);
}

int 
decode_message(struct ccn_charbuf *message, struct path * name_path, char *data, size_t len, const void *verkey) {
    struct ccn_parsed_ContentObject content;
    struct ccn_indexbuf *comps = ccn_indexbuf_create();
    const unsigned char * content_value;
    char * s = NULL;
    size_t content_length;
    
    int res = 0;
    int i;
    
    memset(&content, 0x33, sizeof(content));

    if (ccn_parse_ContentObject(message->buf, message->length, &content, comps) != 0) {
	printf("Decode failed to parse object\n");
	res = -1;
    }

    if (comps->n-1 != name_path->count) {
	printf("Decode got wrong number of path components: %d vs. %d\n", 
	       (int)(comps->n-1), name_path->count);
	res = -1;
    }
    for (i=0; i<name_path->count; i++) {
	if (ccn_name_comp_strcmp(message->buf, comps, i, name_path->comps[i]) != 0) {
	    printf("Decode mismatch on path component %d\n", i);
	    res = -1;
	}
	s = ccn_name_comp_strdup(message->buf, comps, i);
	if (s == NULL || strcmp(s, name_path->comps[i]) != 0) {
	    printf("Decode mismatch on retrieved path component %d\n", i);
	}
	if (s != NULL) { free(s); s = NULL; }
    }
    if (ccn_content_get_value(message->buf, message->length, &content,
			      &content_value, &content_length) != 0) {
	printf("Cannot retrieve content value\n");
	res = -1;
    } else if (content_length != len) {
	printf("Decode mismatch on content length %d vs. %d\n", 
	       (int)content_length, (int)len);
	res = -1;
    } else if (memcmp(content_value, data, len) != 0) {
	printf("Decode mismatch of content\n");
	res = -1;
    }

    if (ccn_verify_signature(message->buf, message->length, &content, verkey) != 1) {
        printf("Signature did not verify\n");
        res = -1;
    }

    ccn_indexbuf_destroy(&comps);
    return res;
    
}

int
expected_res(int res, char code)
{
    if (code == '*')
        return(1);
    if (code == '-')
{'fraction_non_alphanumeric': 0.16494990828277128, 'fraction_numerical': 0.009030619444052491, 'mean_word_length': 3.2291169451073984, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 73, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5740259', 'n_tokens_mistral': 2727, 'n_tokens_neox': 2642, 'n_words': 655}