custom_helpers.c 3.33 KB
Newer Older
1 2 3 4
#include "clar_libgit2.h"
#include "posix.h"
#include "filter.h"
#include "git2/sys/filter.h"
5
#include "custom_helpers.h"
6 7 8 9 10 11

#define VERY_SECURE_ENCRYPTION(b) ((b) ^ 0xff)

int bitflip_filter_apply(
	git_filter     *self,
	void          **payload,
12 13
	git_str        *to,
	const git_str  *from,
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
	const git_filter_source *source)
{
	const unsigned char *src = (const unsigned char *)from->ptr;
	unsigned char *dst;
	size_t i;

	GIT_UNUSED(self); GIT_UNUSED(payload);

	/* verify that attribute path match worked as expected */
	cl_assert_equal_i(
		0, git__strncmp("hero", git_filter_source_path(source), 4));

	if (!from->size)
		return 0;

29
	cl_git_pass(git_str_grow(to, from->size));
30 31 32 33 34 35 36 37 38 39 40

	dst = (unsigned char *)to->ptr;

	for (i = 0; i < from->size; i++)
		dst[i] = VERY_SECURE_ENCRYPTION(src[i]);

	to->size = from->size;

	return 0;
}

41 42 43 44 45 46 47 48 49 50 51
static int bitflip_filter_stream(
	git_writestream **out,
	git_filter *self,
	void **payload,
	const git_filter_source *src,
	git_writestream *next)
{
	return git_filter_buffered_stream_new(out,
		self, bitflip_filter_apply, NULL, payload, src, next);
}

52 53 54 55 56 57 58 59 60 61 62 63 64
static void bitflip_filter_free(git_filter *f)
{
	git__free(f);
}

git_filter *create_bitflip_filter(void)
{
	git_filter *filter = git__calloc(1, sizeof(git_filter));
	cl_assert(filter);

	filter->version = GIT_FILTER_VERSION;
	filter->attributes = "+bitflip";
	filter->shutdown = bitflip_filter_free;
65
	filter->stream = bitflip_filter_stream;
66 67 68 69 70 71 72 73

	return filter;
}


int reverse_filter_apply(
	git_filter     *self,
	void          **payload,
74 75
	git_str        *to,
	const git_str  *from,
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
	const git_filter_source *source)
{
	const unsigned char *src = (const unsigned char *)from->ptr;
	const unsigned char *end = src + from->size;
	unsigned char *dst;

	GIT_UNUSED(self); GIT_UNUSED(payload); GIT_UNUSED(source);

	/* verify that attribute path match worked as expected */
	cl_assert_equal_i(
		0, git__strncmp("hero", git_filter_source_path(source), 4));

	if (!from->size)
		return 0;

91
	cl_git_pass(git_str_grow(to, from->size));
92 93 94 95 96 97 98 99 100 101 102

	dst = (unsigned char *)to->ptr + from->size - 1;

	while (src < end)
		*dst-- = *src++;

	to->size = from->size;

	return 0;
}

103 104 105 106 107 108 109 110 111 112 113
static int reverse_filter_stream(
	git_writestream **out,
	git_filter *self,
	void **payload,
	const git_filter_source *src,
	git_writestream *next)
{
	return git_filter_buffered_stream_new(out,
		self, reverse_filter_apply, NULL, payload, src, next);
}

114 115 116 117 118 119 120 121 122 123 124 125 126
static void reverse_filter_free(git_filter *f)
{
	git__free(f);
}

git_filter *create_reverse_filter(const char *attrs)
{
	git_filter *filter = git__calloc(1, sizeof(git_filter));
	cl_assert(filter);

	filter->version = GIT_FILTER_VERSION;
	filter->attributes = attrs;
	filter->shutdown = reverse_filter_free;
127
	filter->stream = reverse_filter_stream;
128 129 130

	return filter;
}
131

132
static int erroneous_filter_stream(
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
	git_writestream **out,
	git_filter *self,
	void **payload,
	const git_filter_source *src,
	git_writestream *next)
{
	GIT_UNUSED(out);
	GIT_UNUSED(self);
	GIT_UNUSED(payload);
	GIT_UNUSED(src);
	GIT_UNUSED(next);
	return -1;
}

static void erroneous_filter_free(git_filter *f)
{
	git__free(f);
}

git_filter *create_erroneous_filter(const char *attrs)
{
	git_filter *filter = git__calloc(1, sizeof(git_filter));
	cl_assert(filter);

	filter->version = GIT_FILTER_VERSION;
	filter->attributes = attrs;
	filter->stream = erroneous_filter_stream;
	filter->shutdown = erroneous_filter_free;

	return filter;
}