Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit7184da1

Browse files
Now report out of data case with OutOfData exeption
1 parentb2a0ae4 commit7184da1

File tree

8 files changed

+124
-45
lines changed

8 files changed

+124
-45
lines changed

‎blobstamper/blob.cpp‎

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,12 @@ Blob::Dump()
3131
Blob
3232
Blob::ShiftBytes(size_t n)
3333
{
34-
if (begin + n -1 > end)
34+
if (this->Size() < n)
3535
{
36-
Blobempty(NULL, -1);
37-
return empty;/* not enough data*/
36+
throwOutOfData();
3837
}
3938

40-
Blobnew_blob(data, size);
39+
Blobnew_blob(this->data, size);
4140

4241
new_blob.begin = begin;/* FIXME this should go private once*/
4342
new_blob.end = begin + n -1;
@@ -47,6 +46,29 @@ Blob::ShiftBytes(size_t n)
4746
return new_blob;
4847
}
4948

49+
std::vector<char>
50+
Blob::ChopBlank(StampBase &stamp)
51+
{
52+
if (stamp.minSize() >this->Size())
53+
{
54+
throwOutOfData();
55+
}
56+
size_t res_size;
57+
if (stamp.isUnbounded())
58+
{
59+
res_size =this->Size();
60+
}else
61+
{
62+
res_size = stamp.maxSize();
63+
if (res_size >this->Size())
64+
res_size =this->Size();
65+
}
66+
std::vector<char>res((char*)this->data +this->begin, (char*)this->data +this->begin + res_size);
67+
this->begin += res_size;
68+
return res;
69+
}
70+
71+
5072
size_t
5173
Blob::Size()
5274
{
@@ -77,9 +99,9 @@ Blob::DataDup(char *& data_out, size_t& size_out)
7799
std::vector<char>
78100
Blob::asVector()
79101
{
80-
std::vector<char>res(Size());
102+
std::vector<char>res( (char *)data + begin, (char*)data + begin +Size());
81103

82-
memcpy(&res[0], data + begin,Size());
104+
// memcpy(&res[0], data + begin, Size());
83105
return res;
84106
}
85107

‎blobstamper/blob.h‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,20 @@ class Blob
2222
size_tSize();
2323
voidDump();
2424
BlobShiftBytes(size_t n);
25+
std::vector<char>ChopBlank(StampBase &stmp);
2526
voidDataDup(char *& data_out,size_t& size_out);
2627
std::vector<char>asVector();
2728

29+
2830
std::vector<char>ShiftSingleStampBin(StampBase &stmp);
2931
std::stringShiftSingleStampStr(StampBase &stmp);
32+
33+
34+
};
35+
36+
classOutOfData/*An exeption. Experemental for now*/
37+
{
38+
3039
};
3140

3241
#endif/*BLOB_H*/

‎blobstamper/galley.cpp‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,16 +60,17 @@ GalleySeries::ExtractBin(Blob &blob)
6060
std::list<Blob>
6161
GalleySeries::extract_internal(Blob &blob)
6262
{
63+
if (blob.Size()<stamp.minSize())
64+
{
65+
throwOutOfData();/* FIXME: May be later add option that allows empty lists if needed*/
66+
}
6367
std::list<Blob> res;
64-
6568
if (stamp.isFixedSize())
6669
{
6770
int size = stamp.minSize();
68-
while (1)
71+
while (blob.Size() >= size)
6972
{
7073
Blob el = blob.ShiftBytes(size);
71-
if (el.isEmpty())
72-
break;
7374
res.push_back(el);
7475
}
7576
}
@@ -201,7 +202,7 @@ GalleyVector::extract_internal(Blob &blob)
201202

202203
if(fixed_total_size > blob.Size())/* Not enought data case*/
203204
{
204-
return res;
205+
throwOutOfData();
205206
}
206207

207208
int avaliable_nonfixed_size = blob.Size() - fixed_total_size;/* The ammount of data available for non-fixed part of variated or unbounded stamps*/

‎blobstamper/stamp.cpp‎

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,7 @@
1515
std::vector<char>
1616
StampFixed::ExtractBin(Blob &blob)
1717
{
18-
Blob blob2 = blob.ShiftBytes(size);
19-
20-
if (blob2.isEmpty())/* original blob does not have enought data*/
21-
{
22-
std::vector<char>empty(0);
23-
return empty;
24-
}
25-
return blob2.asVector();
18+
std::vector<char> res = blob.ChopBlank(*this);
19+
return res;
2620
}
2721

‎t/001-blob-generic.cpp‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,19 @@ main()
5353
{/* 7*/
5454
/* Check that shifting too many bytes return empty result*/
5555
Blobblob(my_data,strlen(my_data));
56-
Blob blob_res = blob.ShiftBytes(99999);
57-
ok(blob_res.isEmpty(),"Shifting too many bytes gives empty result");
56+
try
57+
{
58+
Blob blob_res = blob.ShiftBytes(99999);
59+
ok(false,"Shift too many bytes");
60+
}
61+
catch (OutOfData)
62+
{
63+
ok(true,"Shift too many bytes");
64+
}
65+
catch (...)//Any othe exeption
66+
{
67+
ok(false,"Shift too many bytes");
68+
}
5869
}
5970

6071

‎t/100-stamp-base.cpp‎

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ main()
2828
char *ptr;
2929
size_t size;
3030

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

3333
/* Test that ShiftSingleStampStr shifts ok with StampTwoChars stamp*/
3434
{/* 1..3*/
@@ -82,10 +82,54 @@ main()
8282

8383
is(str,"12345678","variated size stamp shifts as much data as it can (take two)");
8484

85-
str = blob.ShiftSingleStampStr(stamp);
86-
is(str,"","variated size stamp refuses to stamp when it is offered too few data");
85+
try {
86+
std::string str = blob.ShiftSingleStampStr(stamp);
87+
ok(false,"Variated stamp, not enough data");
88+
}
89+
catch (OutOfData)
90+
{
91+
ok(true,"Variated stamp, not enough data");
92+
}
93+
catch (...)//Any other exeption
94+
{
95+
ok(false,"Variated stamp, not enough data");
96+
}
8797
}
8898

99+
{/* 9*/
100+
char sample[]="1";
101+
Blobblob(sample,strlen(sample));
102+
StampTwoChars stamp;
103+
try {
104+
std::string str = blob.ShiftSingleStampStr(stamp);
105+
ok(false,"Fixed stamp, not enough data");
106+
}
107+
catch (OutOfData)
108+
{
109+
ok(true,"Fixed stamp, not enough data");
110+
}
111+
catch (...)//Any other exeption
112+
{
113+
ok(false,"Fixed stamp, not enough data");
114+
}
115+
}
89116

117+
{/* 10*/
118+
char sample[]="1";
119+
Blobblob(sample,strlen(sample));
120+
StampTwoCharsList stamp;
121+
try {
122+
std::string str = blob.ShiftSingleStampStr(stamp);
123+
ok(false,"Unbounded stamp, not enough data");
124+
}
125+
catch (OutOfData)
126+
{
127+
ok(true,"Unbounded stamp, not enough data");
128+
}
129+
catch (...)//Any other exeption
130+
{
131+
ok(false,"Unbounded stamp, not enough data");
132+
}
133+
}
90134
TEST_END;
91135
}

‎t/300-galley.cpp‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -434,10 +434,18 @@ main()
434434
stamps.push_back(f_stamp);
435435

436436
GalleyVectorgalley(stamps);
437-
std::vector<std::string> res = galley.ExtractStr(blob);
438-
std::string str;
439-
440-
is(res.size(),0,"GalleyVector, mixed type stamps 4: Fails when not enught data");
437+
try{
438+
std::vector<std::string> res = galley.ExtractStr(blob);
439+
ok(false,"Galley Vector, not enough data");
440+
}
441+
catch (OutOfData)
442+
{
443+
ok(true,"Galley Vector, not enough data");
444+
}
445+
catch (...)//Any other exeption
446+
{
447+
ok(false,"Galley Vector, not enough data");
448+
}
441449

442450
is(blob.Size(),strlen(sample) ,"GalleyVector 4: will use keep all data when applying galley fails");
443451

‎t/test-chars-stamps.h‎

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -51,23 +51,15 @@ StampSeveralChars::StampSeveralChars()
5151
std::string
5252
StampSeveralChars::ExtractStr(Blob &blob)
5353
{
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();
6054

61-
Blob blob2 = blob.ShiftBytes(size);
62-
if (blob2.isEmpty())
63-
return"";
64-
65-
/* Save shited data as string*/
55+
std::vector<char> data = blob.ChopBlank(*this);
56+
/* Save optained data as string*/
6657
/* 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';
58+
char * buf;
59+
buf = (char *)malloc(data.size() +1);
60+
memcpy((void *) buf, (void *) &data[0], data.size());
61+
buf[data.size()] ='\0';
62+
7163
std::string res = buf;
7264
free(buf);
7365

@@ -98,8 +90,6 @@ StampTwoCharsList::ExtractStr(Blob &blob)
9890
res = res + point;
9991
}
10092

101-
if (res.empty())return"";
102-
10393
res ="(" + res +")";
10494
return res;
10595
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp