src/snappy.cpp

filter: Statementstmt 81.6% 40 / 49 Branchbran 45.0% 18 / 40 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 65.1% 58 / 89 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« buildtmp/StatProfiler.c ↑ Summary src/tracefile.cpp »
1 #if SNAPPY
2  
3 #include "snappy.h"
4  
5 using namespace devel::statprofiler;
6 using namespace std;
7  
8  
9 5605 SnappyInput::SnappyInput(int max_size)
10 {
11 5605 snappy_max = csnappy_max_compressed_length(max_size);
12 5605 snappy_input = new char[snappy_max];
13 5605 snappy_end = snappy_input;
14 5605 }
15  
16 5604 SnappyInput::~SnappyInput()
17 {
18 5604 ▶ delete[] snappy_input;
Branch: 1 branch
BranchTrueFalseTotal
delete[] snappy_input; 5604 0 5604
19 5604 }
20  
21 10189 int SnappyInput::read(std::FILE *fh, char *buffer, size_t size)
22 {
23 10189 ▶ int snappy_read = fread(snappy_end, 1, snappy_input + snappy_max - snappy_end, fh);
Branch: 1 branch
BranchTrueFalseTotal
int snappy_read = fread(snappy_end, 1, snappy_input + snappy_max - snappy_end, fh); 10189 0 10189
24 10189 snappy_end += snappy_read;
25  
26 10189 ▶ if (snappy_end == snappy_input)
Branch: 1 branch
BranchTrueFalseTotal
if (snappy_end == snappy_input) 2650 7539 10189
27 2650 return 0;
28  
29 7539 int packet =
30 7539 (snappy_input[0] & 0xff) << 8 |
31 7539 (snappy_input[1] & 0xff);
32 uint32_t decompressed;
33 7539 ▶ int used = csnappy_get_uncompressed_length(snappy_input + 2, snappy_end - (snappy_input + 2), &decompressed);
Branch: 1 branch
BranchTrueFalseTotal
int used = csnappy_get_uncompressed_length(snappy_input + 2, snappy_end - (snappy_input + 2), &decompressed); 7539 0 7539
34  
35 7539 ▶ if (used < CSNAPPY_E_OK) {
Branch: 1 branch
BranchTrueFalseTotal
if (used < CSNAPPY_E_OK) { 0 7539 7539
36 0 ▶ warn("Error decoding length for snappy packet: %d", used);
Branch: 1 branch
BranchTrueFalseTotal
warn("Error decoding length for snappy packet: %d", used); 0 0 0
37 0 snappy_end = snappy_input;
38 0 return 0;
39 }
40  
41 7539 ▶ if (decompressed > size) {
Branch: 1 branch
BranchTrueFalseTotal
if (decompressed > size) { 0 7539 7539
42 0 ▶ warn("Decompressed packet data would overflow input buffer");
Branch: 1 branch
BranchTrueFalseTotal
warn("Decompressed packet data would overflow input buffer"); 0 0 0
43 0 snappy_end = snappy_input;
44 0 return 0;
45 }
46  
47 7539 uint32_t bytes = decompressed;
48 7539 ▶ int ok = csnappy_decompress_noheader(snappy_input + 2 + used, packet - used, buffer, &bytes);
Branch: 1 branch
BranchTrueFalseTotal
int ok = csnappy_decompress_noheader(snappy_input + 2 + used, packet - used, buffer, &bytes); 7539 0 7539
49  
50 7539 ▶ if (ok < CSNAPPY_E_OK) {
Branch: 1 branch
BranchTrueFalseTotal
if (ok < CSNAPPY_E_OK) { 0 7539 7539
51 0 ▶ warn("Error decompressing snappy packet: %d", ok);
Branch: 1 branch
BranchTrueFalseTotal
warn("Error decompressing snappy packet: %d", ok); 0 0 0
52 0 snappy_end = snappy_input;
53 0 return 0;
54 }
55  
56 7539 size_t remaining = snappy_end - (snappy_input + 2 + packet);
57 7539 memmove(snappy_input, snappy_input + 2 + packet, remaining);
58 7539 snappy_end = snappy_input + remaining;
59  
60 7539 return bytes;
61 }
62  
63  
64 58 SnappyOutput::SnappyOutput(int max_size)
65 {
66 58 snappy_output = new char[csnappy_max_compressed_length(max_size)];
67 58 snappy_workmem = new char[CSNAPPY_WORKMEM_BYTES];
68 58 }
69  
70 58 SnappyOutput::~SnappyOutput()
71 {
72 58 ▶ delete[] snappy_output;
Branch: 1 branch
BranchTrueFalseTotal
delete[] snappy_output; 58 0 58
73 58 ▶ delete[] snappy_workmem;
Branch: 1 branch
BranchTrueFalseTotal
delete[] snappy_workmem; 58 0 58
74 58 }
75  
76 2232 int SnappyOutput::write(FILE *fh, const char *buffer, size_t size)
77 {
78 uint32_t compressed_length;
79  
80 2232 csnappy_compress(buffer, size,
81 snappy_output, &compressed_length,
82 2232 ▶ snappy_workmem,
Branch: 1 branch
BranchTrueFalseTotal
snappy_workmem, 2232 0 2232
83 CSNAPPY_WORKMEM_BYTES_POWER_OF_TWO);
84  
85 return
86 2232 ▶ fputc(compressed_length >> 8, fh) != EOF &&
Branch: 1 branch
BranchTrueFalseTotal
fputc(compressed_length >> 8, fh) != EOF && 2232 0 2232
87 4464 ▶ fputc(compressed_length & 0xff, fh) != EOF &&
Branch: 3 branches
BranchTrueFalseTotal
fputc(compressed_length & 0xff, fh) != EOF && 2232 0 2232
fputc(compressed_length & 0xff, fh) != EOF && 2232 0 2232
fputc(compressed_length & 0xff, fh) != EOF && 2232 0 2232
88 4464 ▶ fwrite(snappy_output, 1, compressed_length, fh) == compressed_length;
Branch: 2 branches
BranchTrueFalseTotal
fwrite(snappy_output, 1, compressed_length, fh) == compressed_length; 2232 0 2232
fwrite(snappy_output, 1, compressed_length, fh) == compressed_length; 2232 0 2232
89 }
90  
91 #endif
« buildtmp/StatProfiler.c ↑ Summary src/tracefile.cpp »