10000 Introduce Variated size Stap base class. Add tests for it · postgrespro/libblobstamper@cbc91eb · GitHub
[go: up one dir, main page]

Skip to content

Commit cbc91eb

Browse files
Introduce Variated size Stap base class. Add tests for it
1 parent 21388f9 commit cbc91eb

File tree

5 files changed

+124
-32
lines changed

5 files changed

+124
-32
lines changed

blobstamper/galley.cpp

Lines changed: 38 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,28 @@
55
#include <vector>
66

77

8+
int
9+
GalleySeries::minSize()
10+
{
11+
if (stamp.isFixedSize())
12+
{
13+
return stamp.minSize(); // When size is fixed, series can have only one member with no extra data used
14+
}
15+
else
16+
{
17+
if (stamp.isUnbounded())
18+
{
19+
return stamp.minSize() + ORACLE_SIZE * 2; // One -- count oracle, one -- size oracle
20+
}
21+
else
22+
{
23+
return stamp.minSize() + ORACLE_SIZE; // At leas one element with an oracle
24+
}
25+
}
26+
}
27+
28+
29+
830
std::list<std::string>
931
GalleySeries::ExtractStr(Blob &blob)
1032
{
@@ -108,31 +130,25 @@ GalleySeries::extract_internal(Blob &blob)
108130
}
109131
else
110132
{
111-
printf("Not implemented yet!");
112-
exit(1);
133+
/* Stamp is variated size */
134+
int fixed_size = stamp.minSize();
135+
int var_size = stamp.maxSize() - fixed_size;
136+
ORACLE_STAMP stamp_oracle;
137+
while(1)
138+
{
139+
if(stamp.minSize() + stamp_oracle.minSize() > blob.Size())
140+
break;
141+
ORACLE_TYPE *oracle = (ORACLE_TYPE *) blob.ShiftSingleStampBin(stamp_oracle);
142+
int size = (double) *oracle / ORACLE_MAX * (var_size + 1); /* +1 -- это грубая эмуляция округления вверх. oracle == ORACLE_MAX-1 == 65534 должен дать count_max*/
143+
if (size > var_size) size = var_size; // In case we've hit oracle == ORACLE_MAX boundary
144+
size += fixed_size;
145+
Blob blob2 = blob.ShiftBytes(size);
146+
res.push_back(blob2);
147+
free(oracle);
148+
}
113149
}
114150
}
115151
return res;
116152
}
117153

118154

119-
int
120-
GalleySeries::minSize()
121-
{
122-
if (stamp.isFixedSize())
123-
{
124-
return stamp.minSize(); // When size is fixed, series can have only one member with no extra data used
125-
}
126-
else
127-
{
128-
if (stamp.maxSize() == -1) // if unlimited size
129-
{
130-
return stamp.minSize() + ORACLE_SIZE * 2; // One -- count oracle, one -- size oracle
131-
}
132-
else
133-
{
134-
printf("Not implemented yet!");
135-
exit(1);
136-
}
137-
}
138-
}

blobstamper/stamp.h

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,16 @@ class StampFixed : public StampBase
3030
void * ExtractBin(Blob &blob) override;
3131
};
3232

33+
class StampVariated : public StampBase
34+
{
35+
protected:
36+
int min_size;
37+
int max_size;
38+
public:
39+
virtual int minSize() {return min_size;}
40+
virtual int maxSize() {return max_size;}
41+
};
42+
3343
class StampUnbounded : public StampBase
3444
{
3545
protected:
@@ -39,7 +49,4 @@ class StampUnbounded : public StampBase
3949
virtual int maxSize() {return -1;}
4050
};
4151

42-
43-
44-
4552
#endif /* STAMP_H */

t/100-stamp-base.cpp

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#include "blobstamper/blobstamper.h"
1212
#include "blobstamper/helpers.h"
1313

14-
#include "two-chars-stamp.h"
14+
#include "test-chars-stamps.h"
1515

1616
using namespace TAP;
1717

@@ -28,7 +28,7 @@ main()
2828
char *ptr;
2929
size_t size;
3030

31-
TEST_START(4);
31+
TEST_START(8);
3232

3333
/* Test that ShiftSingleStampStr shifts ok with StampTwoChars stamp */
3434
{ /* 1..3 */
@@ -39,23 +39,53 @@ main()
3939
Blob blob(short_sample, strlen(short_sample));
4040
StampTwoChars stamp;
4141
std::string str = blob.ShiftSingleStampStr(stamp);
42-
ok(str == expected1, "ShiftSingleStampStr: shifts ok");
42+
is(str, expected1, "ShiftSingleStampStr: shifts ok");
4343

4444
blob.DataDup(ptr,size);
4545
ok(size == strlen(expected2), "ShiftSingleStampStr: Remaining blob data size ok");
4646
ok(! memcmp(expected2, ptr, size), "ShiftSingleStampStr: Remaining blob data ok");
4747
free(ptr);
4848
}
4949

50-
/* Chekc that data is shifted till blob data is empty*/
51-
{ /* 8 */
50+
/* Check that data is shifted till blob data is empty*/
51+
{ /* 4 */
5252
char sample_two_bytes[]="12";
5353
std::string expected1 = "12";
5454
Blob blob(sample_two_bytes, strlen(sample_two_bytes));
5555
StampTwoChars stamp;
5656
std::string str = blob.ShiftSingleStampStr(stamp);
57-
ok(str == expected1, "ShiftSingleStampStr: shifts last two bytes ok");
57+
is(str, expected1, "ShiftSingleStampStr: shifts first two bytes ok");
5858
}
59+
/* Checks for variated size stamps*/
60+
61+
{ /* 5,6 */
62+
char sample[]="1234567890";
63+
Blob blob(sample, strlen(sample));
64+
StampSeveralChars stamp; /* accepts from 2 to 8 bytes*/
65+
66+
/* If used alone, is shifts as much bytes as it can. When blob has a lot, it shifts maxSize bytes */
67+
std::string str = blob.ShiftSingleStampStr(stamp);
68+
69+
is(str, "12345678", "variated size stamp shifts as much data as it can");
70+
71+
str = blob.ShiftSingleStampStr(stamp);
72+
is(str, "90", "but will be satisfied with less, unless it is not less than minSize()");
73+
}
74+
75+
{ /* 7,8 */
76+
char sample[]="123456789";
77+
Blob blob(sample, strlen(sample));
78+
StampSeveralChars stamp; /* accepts from 2 to 8 bytes*/
79+
80+
/* If used alone, is shifts as much bytes as it can. When blob has a lot, it shifts maxSize bytes */
81+
std::string str = blob.ShiftSingleStampStr(stamp);
82+
83+
is(str, "12345678", "variated size stamp shifts as much data as it can (take two)");
84+
85+
str = blob.ShiftSingleStampStr(stamp);
86+
is(str, "", "variated size stamp refuses to stamp when it is offered too few data");
87+
}
88+
5989

6090
TEST_END;
6191
}

t/300-galley.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
#include "blobstamper/blobstamper.h"
1010

11-
#include "two-chars-stamp.h"
11+
#include "test-chars-stamps.h"
1212

1313
using namespace TAP;
1414

t/two-chars-stamp.h renamed to t/test-chars-stamps.h

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,42 @@ StampTwoChars::ExtractStr(Blob &blob)
3535
return res;
3636
}
3737

38+
class StampSeveralChars: public StampVariated
39+
{
40+
public:
41+
StampSeveralChars();
42+
std::string ExtractStr(Blob &blob) override;
43+
};
44+
45+
StampSeveralChars::StampSeveralChars()
46+
{
47+
min_size = 2;
48+
max_size = 8;
49+
}
50+
51+
std::string
52+
StampSeveralChars::ExtractStr(Blob &blob)
53+
{
54+
if (blob.Size() < min_size)
55+
return "";
56+
char * buf;
57+
size_t size = max_size;
58+
if (blob.Size() < max_size)
59+
size = blob.Size();
60+
61+
Blob blob2 = blob.ShiftBytes(size);
62+
if (blob2.isEmpty())
63+
return "";
64+
65+
/* Save shited data as string */
66+
/* NEVER do this in prod, as in real live blob is binary and may have 0 in the middle of it */
67+
size_t buf_size;
68+
blob2.DataDup(buf, buf_size);
69+
buf = (char *) realloc((void *)buf, buf_size + 1);
70+
buf[buf_size] = '\0';
71+
std::string res = buf;
72+
free(buf);
73+
74+
return res;
75+
}
76+

0 commit comments

Comments
 (0)
0