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