1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
|
/* xdelta3 - delta compression tools and library -*- Mode: C++ -*-
Copyright 2016 Joshua MacDonald
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
class Segment {
public:
Segment(size_t size, MTRandom *rand)
: size_(size),
seed_(rand->Rand32()),
seed_offset_(0),
data_(NULL) {
CHECK_GT(size_, 0);
}
Segment(size_t size, uint32_t seed)
: size_(size),
seed_(seed),
seed_offset_(0),
data_(NULL) {
CHECK_GT(size_, 0);
}
Segment(size_t size, uint8_t *data)
: size_(size),
seed_(0),
seed_offset_(0),
data_(data) {
CHECK_GT(size_, 0);
}
size_t Size() const {
return size_;
}
Segment Subseg(size_t start, size_t size) const {
CHECK_LE(start + size, size_);
if (data_) {
return Segment(size, data_ + start);
} else {
return Segment(size, seed_, seed_offset_ + start);
}
}
void Fill(size_t seg_offset, size_t size, uint8_t *data) const {
CHECK_LE(seg_offset + size, size_);
if (data_) {
memcpy(data, data_ + seg_offset, size);
} else {
size_t skip = seg_offset + seed_offset_;
MTRandom gen(seed_);
MTRandom8 gen8(&gen);
while (skip--) {
gen8.Rand8();
}
for (size_t i = 0; i < size; i++) {
data[i] = gen8.Rand8();
}
}
}
string ToString() const {
string r;
if (data_) {
for (size_t i = 0; i < size_; i++) {
char buf[10];
sprintf(buf, "%02x ", data_[i]);
r.append(buf);
}
} else {
char buf[256];
sprintf(buf, "size=%ld,seed=%ud,skip=%ld", size_, seed_, seed_offset_);
r.append(buf);
}
return r;
}
private:
// Used by Subseg()
Segment(size_t size, uint32_t seed, size_t seed_offset)
: size_(size),
seed_(seed),
seed_offset_(seed_offset),
data_(NULL) {
CHECK_GT(size_, 0);
}
size_t size_; // Size of this segment
// For random segments
uint32_t seed_; // Seed used for generating byte sequence
size_t seed_offset_; // Seed positions the sequence this many bytes
// before its beginning.
// For literal segments (data is not owned)
uint8_t *data_;
};
typedef map<xoff_t, Segment> SegmentMap;
typedef typename SegmentMap::const_iterator ConstSegmentMapIterator;
typedef typename SegmentMap::iterator SegmentMapIterator;
|