Interface.xs

filter: Statementstmt 77.0% 77 / 100 Branchbran 64.5% 31 / 48 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 72.9% 108 / 148 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« blib/lib/Vcdiff/Xdelta3.pm ↑ Summary inc/xdelta3.h »
1 #include "EXTERN.h"
2 #include "perl.h"
3 #include "XSUB.h"
4  
5 #include <stdio.h>
6 #include <stdlib.h>
7  
8 #include "xdelta3.h"
9  
10 // FIXME: figure out the right value for this: maybe dynamic?
11 #define BUF_SIZE 65536
12  
13 #ifndef MIN
14 #define MIN(x,y) ((x)<(y)?(x):(y))
15 #endif
16  
17 129 int encode_decode(int encode,
18 int source_fd, SV *source_sv,
19 int input_fd, SV *input_sv,
20 int output_fd, SV *output_sv) {
21 int ret;
22 ssize_t ssize_ret;
23 129 int error = 0;
24 xd3_stream stream;
25 xd3_config config;
26 xd3_source source;
27 129 char *ibuf = NULL;
28 129 int ibuf_len = 0;
29 129 char *source_str = NULL;
30 129 size_t source_str_size = 0;
31 129 char *input_str = NULL;
32 129 size_t input_str_size = 0;
33  
34 129 memset(&stream, 0, sizeof (stream));
35 129 memset(&source, 0, sizeof (source));
36  
37 129 xd3_init_config(&config, 0);
38 129 config.winsize = BUF_SIZE;
39 129 ▶ if (xd3_config_stream(&stream, &config)) {
Branch: 1 branch
BranchTrueFalseTotal
if (xd3_config_stream(&stream, &config)) { 0 129 129
40 // Nothing to clean up so just return error straight away
41 0 return 1;
42 }
43  
44 129 source.blksize = BUF_SIZE;
45 129 source.curblkno = 0;
46  
47 129 ▶ if (source_fd != -1) {
Branch: 1 branch
BranchTrueFalseTotal
if (source_fd != -1) { 65 64 129
48 65 source.curblk = malloc(source.blksize);
49 65 ▶ if (!source.curblk) {
Branch: 1 branch
BranchTrueFalseTotal
if (!source.curblk) { 0 65 65
50 0 error = 2;
51 0 goto bail;
52 }
53 65 ▶ if (lseek(source_fd, 0, SEEK_SET) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (lseek(source_fd, 0, SEEK_SET) < 0) { 1 64 65
54 1 error = 3;
55 1 goto bail;
56 }
57 64 ssize_ret = read(source_fd, (void*)source.curblk, source.blksize);
58 64 ▶ if (ssize_ret < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (ssize_ret < 0) { 0 64 64
59 0 error = 4;
60 0 goto bail;
61 } else {
62 64 source.onblk = (size_t) ssize_ret;
63 }
64 } else {
65 64 source_str_size = SvCUR(source_sv);
66 64 source_str = SvPV(source_sv, source_str_size);
67 64 source.curblk = (uint8_t *) source_str;
68 64 source.onblk = MIN(source.blksize, source_str_size);
69 }
70  
71 128 xd3_set_source(&stream, &source);
72  
73 128 ▶ if (input_fd != -1) {
Branch: 1 branch
BranchTrueFalseTotal
if (input_fd != -1) { 64 64 128
74 64 ibuf = malloc(BUF_SIZE);
75 64 ▶ if (!ibuf) {
Branch: 1 branch
BranchTrueFalseTotal
if (!ibuf) { 0 64 64
76 0 error = 5;
77 0 goto bail;
78 }
79 } else {
80 64 input_str_size = SvCUR(input_sv);
81 64 input_str = SvPV(input_sv, input_str_size);
82 64 ibuf = input_str;
83 64 ibuf_len = 0;
84 }
85  
86 do {
87 188 ▶ if (input_fd != -1) {
Branch: 1 branch
BranchTrueFalseTotal
if (input_fd != -1) { 64 124 188
88 64 ibuf_len = read(input_fd, ibuf, BUF_SIZE);
89 64 ▶ if (ibuf_len < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (ibuf_len < 0) { 0 64 64
90 0 error = 6;
91 0 goto bail;
92 }
93 } else {
94 124 ibuf += ibuf_len;
95 124 ibuf_len = MIN(BUF_SIZE, input_str_size - (ibuf - input_str));
96 }
97  
98 188 ▶ if (ibuf_len < BUF_SIZE) {
Branch: 1 branch
BranchTrueFalseTotal
if (ibuf_len < BUF_SIZE) { 128 60 188
99 128 xd3_set_flags(&stream, XD3_FLUSH | stream.flags);
100 }
101 188 xd3_avail_input(&stream, (uint8_t *) ibuf, ibuf_len);
102  
103 1032 process:
104 1220 ▶ if (encode)
Branch: 1 branch
BranchTrueFalseTotal
if (encode) 784 436 1220
105 784 ret = xd3_encode_input(&stream);
106 else
107 436 ret = xd3_decode_input(&stream);
108  
109 1220 switch (ret) {
110 188 case XD3_INPUT:
111 188 continue;
112  
113 506 case XD3_OUTPUT:
114 506 ▶ if (output_fd != -1) {
Branch: 1 branch
BranchTrueFalseTotal
if (output_fd != -1) { 253 253 506
115 253 ssize_ret = write(output_fd, stream.next_out, stream.avail_out);
116 253 ▶ if (ssize_ret < 0 || ((size_t)ssize_ret) != stream.avail_out) {
Branch: 2 branches
BranchTrueFalseTotal
if (ssize_ret < 0 || ((size_t)ssize_ret) != stream.avail_out) { 253 0 253
if (ssize_ret < 0 || ((size_t)ssize_ret) != stream.avail_out) { 0 253 253
117 0 error = 7;
118 0 goto bail;
119 }
120 } else {
121 253 sv_catpvn(output_sv, (char *) stream.next_out, stream.avail_out);
122 }
123  
124 506 xd3_consume_output(&stream);
125  
126 506 goto process;
127  
128 30 case XD3_GETSRCBLK:
129 30 source.curblkno = source.getblkno;
130  
131 30 ▶ if (source_fd != -1) {
Branch: 1 branch
BranchTrueFalseTotal
if (source_fd != -1) { 0 30 30
132 0 ▶ if (lseek(source_fd, source.blksize * source.getblkno, SEEK_SET) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (lseek(source_fd, source.blksize * source.getblkno, SEEK_SET) < 0) { 0 0 0
133 0 error = 3;
134 0 goto bail;
135 }
136 0 ssize_ret = read(source_fd, (void*)source.curblk, source.blksize);
137 0 ▶ if (ssize_ret < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (ssize_ret < 0) { 0 0 0
138 0 error = 4;
139 0 goto bail;
140 } else {
141 0 source.onblk = (size_t) ssize_ret;
142 }
143 } else {
144 30 source.curblk = (uint8_t *) (source_str + (source.blksize * source.getblkno));
145 30 source.onblk = MIN(source.blksize, source_str_size - (source.blksize * source.getblkno));
146 }
147  
148 30 goto process;
149  
150 496 case XD3_GOTHEADER:
151 case XD3_WINSTART:
152 case XD3_WINFINISH:
153 496 goto process;
154  
155 0 default:
156 // These values start at -17703 and go down
157 0 error = ret;
158 0 goto bail;
159 }
160  
161 188 ▶ } while (ibuf_len == BUF_SIZE);
Branch: 1 branch
BranchTrueFalseTotal
} while (ibuf_len == BUF_SIZE); 60 128 188
162  
163 128 bail:
164 129 ▶ if (source_fd != -1 && source.curblk) {
Branch: 2 branches
BranchTrueFalseTotal
if (source_fd != -1 && source.curblk) { 65 64 129
if (source_fd != -1 && source.curblk) { 65 0 65
165 65 free((void*)source.curblk);
166 }
167  
168 129 ▶ if (input_fd != -1 && ibuf) {
Branch: 2 branches
BranchTrueFalseTotal
if (input_fd != -1 && ibuf) { 64 65 129
if (input_fd != -1 && ibuf) { 64 0 64
169 64 free(ibuf);
170 }
171  
172 129 ▶ if (xd3_close_stream(&stream) && !error) {
Branch: 2 branches
BranchTrueFalseTotal
if (xd3_close_stream(&stream) && !error) { 0 129 129
if (xd3_close_stream(&stream) && !error) { 0 0 0
173 0 error = 8;
174 }
175 129 xd3_free_stream(&stream);
176  
177 129 return error;
178 }
179  
180  
181  
182 MODULE = Vcdiff::Xdelta3 PACKAGE = Vcdiff::Xdelta3
183  
184 PROTOTYPES: ENABLE
185  
186  
187  
188 int
189 _encode(source_fd, source_sv, input_fd, input_sv, output_fd, output_sv)
190 int source_fd
191 SV *source_sv
192 int input_fd
193 SV *input_sv
194 int output_fd
195 SV *output_sv
196 CODE:
197 65 RETVAL = encode_decode(1,
198 source_fd, source_sv,
199 input_fd, input_sv,
200 output_fd, output_sv);
201  
202 OUTPUT:
203 RETVAL
204  
205  
206  
207 int
208 _decode(source_fd, source_sv, input_fd, input_sv, output_fd, output_sv)
209 int source_fd
210 SV *source_sv
211 int input_fd
212 SV *input_sv
213 int output_fd
214 SV *output_sv
215 CODE:
216 64 RETVAL = encode_decode(0,
217 source_fd, source_sv,
218 input_fd, input_sv,
219 output_fd, output_sv);
220  
221 OUTPUT:
222 RETVAL
« blib/lib/Vcdiff/Xdelta3.pm ↑ Summary inc/xdelta3.h »