]> git.proxmox.com Git - libgit2.git/blame - tests/filter/custom_helpers.c
New upstream version 1.4.3+dfsg.1
[libgit2.git] / tests / filter / custom_helpers.c
CommitLineData
63924435
ET
1#include "clar_libgit2.h"
2#include "posix.h"
3#include "filter.h"
63924435 4#include "git2/sys/filter.h"
e579e0f7 5#include "custom_helpers.h"
63924435
ET
6
7#define VERY_SECURE_ENCRYPTION(b) ((b) ^ 0xff)
8
9int bitflip_filter_apply(
10 git_filter *self,
11 void **payload,
e579e0f7
MB
12 git_str *to,
13 const git_str *from,
63924435
ET
14 const git_filter_source *source)
15{
16 const unsigned char *src = (const unsigned char *)from->ptr;
17 unsigned char *dst;
18 size_t i;
19
20 GIT_UNUSED(self); GIT_UNUSED(payload);
21
22 /* verify that attribute path match worked as expected */
23 cl_assert_equal_i(
24 0, git__strncmp("hero", git_filter_source_path(source), 4));
25
26 if (!from->size)
27 return 0;
28
e579e0f7 29 cl_git_pass(git_str_grow(to, from->size));
63924435
ET
30
31 dst = (unsigned char *)to->ptr;
32
33 for (i = 0; i < from->size; i++)
34 dst[i] = VERY_SECURE_ENCRYPTION(src[i]);
35
36 to->size = from->size;
37
38 return 0;
39}
40
c25aa7cd
PP
41static int bitflip_filter_stream(
42 git_writestream **out,
43 git_filter *self,
44 void **payload,
45 const git_filter_source *src,
46 git_writestream *next)
47{
48 return git_filter_buffered_stream_new(out,
49 self, bitflip_filter_apply, NULL, payload, src, next);
50}
51
63924435
ET
52static void bitflip_filter_free(git_filter *f)
53{
54 git__free(f);
55}
56
57git_filter *create_bitflip_filter(void)
58{
59 git_filter *filter = git__calloc(1, sizeof(git_filter));
60 cl_assert(filter);
61
62 filter->version = GIT_FILTER_VERSION;
63 filter->attributes = "+bitflip";
64 filter->shutdown = bitflip_filter_free;
c25aa7cd 65 filter->stream = bitflip_filter_stream;
63924435
ET
66
67 return filter;
68}
69
70
71int reverse_filter_apply(
72 git_filter *self,
73 void **payload,
e579e0f7
MB
74 git_str *to,
75 const git_str *from,
63924435
ET
76 const git_filter_source *source)
77{
78 const unsigned char *src = (const unsigned char *)from->ptr;
79 const unsigned char *end = src + from->size;
80 unsigned char *dst;
81
82 GIT_UNUSED(self); GIT_UNUSED(payload); GIT_UNUSED(source);
83
84 /* verify that attribute path match worked as expected */
85 cl_assert_equal_i(
86 0, git__strncmp("hero", git_filter_source_path(source), 4));
87
88 if (!from->size)
89 return 0;
90
e579e0f7 91 cl_git_pass(git_str_grow(to, from->size));
63924435
ET
92
93 dst = (unsigned char *)to->ptr + from->size - 1;
94
95 while (src < end)
96 *dst-- = *src++;
97
98 to->size = from->size;
99
100 return 0;
101}
102
c25aa7cd
PP
103static int reverse_filter_stream(
104 git_writestream **out,
105 git_filter *self,
106 void **payload,
107 const git_filter_source *src,
108 git_writestream *next)
109{
110 return git_filter_buffered_stream_new(out,
111 self, reverse_filter_apply, NULL, payload, src, next);
112}
113
63924435
ET
114static void reverse_filter_free(git_filter *f)
115{
116 git__free(f);
117}
118
119git_filter *create_reverse_filter(const char *attrs)
120{
121 git_filter *filter = git__calloc(1, sizeof(git_filter));
122 cl_assert(filter);
123
124 filter->version = GIT_FILTER_VERSION;
125 filter->attributes = attrs;
126 filter->shutdown = reverse_filter_free;
c25aa7cd 127 filter->stream = reverse_filter_stream;
63924435
ET
128
129 return filter;
130}
cf07db2f 131
e579e0f7 132static int erroneous_filter_stream(
cf07db2f
PS
133 git_writestream **out,
134 git_filter *self,
135 void **payload,
136 const git_filter_source *src,
137 git_writestream *next)
138{
139 GIT_UNUSED(out);
140 GIT_UNUSED(self);
141 GIT_UNUSED(payload);
142 GIT_UNUSED(src);
143 GIT_UNUSED(next);
144 return -1;
145}
146
147static void erroneous_filter_free(git_filter *f)
148{
149 git__free(f);
150}
151
152git_filter *create_erroneous_filter(const char *attrs)
153{
154 git_filter *filter = git__calloc(1, sizeof(git_filter));
155 cl_assert(filter);
156
157 filter->version = GIT_FILTER_VERSION;
158 filter->attributes = attrs;
159 filter->stream = erroneous_filter_stream;
160 filter->shutdown = erroneous_filter_free;
161
162 return filter;
163}