Skip to content

Commit 6d8607a

Browse files
Rework code that deals with chopping blob into pieces... Now you should point only size values
1 parent 13caa8a commit 6d8607a

File tree

10 files changed

+42
-42
lines changed

10 files changed

+42
-42
lines changed

blobstamper/blob.cpp

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -46,46 +46,40 @@ Blob::Dump()
4646
hexdump(data + begin, length);
4747
}
4848

49+
4950
std::shared_ptr<Blob>
50-
Blob::ShiftBytes(size_t n)
51+
Blob::Chop(size_t chop_size)
5152
{
52-
if (this->Size() < n)
53+
if (this->Size() < chop_size)
5354
{
5455
throw OutOfData();
5556
}
5657

57-
std::shared_ptr<Blob> new_blob = std::make_shared<Blob>(this->data, size);
58-
59-
new_blob->begin = begin; /* FIXME this should go private once */
60-
new_blob->end = begin + n - 1;
61-
62-
begin += n;
58+
std::shared_ptr<Blob> new_blob = std::make_shared<Blob>(this->data + begin, chop_size);
59+
begin += chop_size;
6360

6461
return new_blob;
6562
}
6663

67-
std::vector<char>
68-
Blob::ChopBlank(StampBase &stamp)
64+
std::shared_ptr<Blob>
65+
Blob::Chop(size_t min_size, size_t max_size)
6966
{
70-
if (stamp.minSize() > this->Size())
67+
if (this->Size() < min_size)
7168
{
7269
throw OutOfData();
7370
}
74-
size_t res_size;
75-
if (stamp.isUnbounded())
76-
{
77-
res_size = this->Size();
78-
} else
79-
{
80-
res_size = stamp.maxSize();
81-
if (res_size > this->Size())
82-
res_size = this->Size();
83-
}
84-
std::vector<char> res((char*)this->data + this->begin, (char*)this->data + this->begin + res_size);
85-
this->begin += res_size;
86-
return res;
71+
if (this->Size() >= max_size)
72+
return this->Chop(max_size);
73+
74+
return this->Chop(this->Size());
8775
}
8876

77+
std::vector<char>
78+
Blob::AsByteVector()
79+
{
80+
std::vector<char> res(data + begin, data + begin + size);
81+
return res;
82+
}
8983

9084
size_t
9185
Blob::Size()

blobstamper/blob.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,11 @@ class Blob
3939
bool isEmpty ();
4040
size_t Size();
4141
void Dump();
42-
std::shared_ptr<Blob> ShiftBytes(size_t n);
43-
std::vector<char> ChopBlank(StampBase &stmp);
42+
43+
std::vector<char> AsByteVector();
44+
std::shared_ptr<Blob> Chop(size_t chop_size);
45+
std::shared_ptr<Blob> Chop(size_t min_size, size_t max_size);
46+
4447
void DataDup(char *& data_out, size_t& size_out);
4548
std::vector<char> asVector();
4649
std::string asString(); /* Should not be used in prod, for tests and examples only*/

blobstamper/galley.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ GalleyVectorBase::extract_internal(std::shared_ptr<Blob> blob)
8787
int size = stamp.minSize();
8888
while (blob->Size() >= size)
8989
{
90-
std::shared_ptr<Blob> el = blob->ShiftBytes(size);
90+
std::shared_ptr<Blob> el = blob->Chop(size);
9191
res.push_back(el);
9292
}
9393
}
@@ -151,7 +151,7 @@ GalleyVectorBase::extract_internal(std::shared_ptr<Blob> blob)
151151
int el_size = el_size_f;
152152
remainder = el_size_f - el_size;
153153

154-
std::shared_ptr<Blob> blob2 = blob->ShiftBytes(el_size);
154+
std::shared_ptr<Blob> blob2 = blob->Chop(el_size);
155155
res.push_back(blob2);
156156
}
157157
}
@@ -171,7 +171,7 @@ GalleyVectorBase::extract_internal(std::shared_ptr<Blob> blob)
171171
int size = (double) oracle / ORACLE_MAX * (var_size + 1); /* +1 -- это грубая эмуляция округления вверх. oracle == ORACLE_MAX-1 == 65534 должен дать count_max*/
172172
if (size > var_size) size = var_size; // In case we've hit oracle == ORACLE_MAX boundary
173173
size += fixed_size;
174-
std::shared_ptr<Blob> blob2 = blob->ShiftBytes(size);
174+
std::shared_ptr<Blob> blob2 = blob->Chop(size);
175175
res.push_back(blob2);
176176
}
177177
}
@@ -341,7 +341,7 @@ GalleySetBase::extract_internal(std::shared_ptr<Blob> blob)
341341
unbounded_remainder = len - el_size;
342342
el_size +=s.minSize();
343343
}
344-
std::shared_ptr<Blob> blob2 = blob->ShiftBytes(el_size);
344+
std::shared_ptr<Blob> blob2 = blob->Chop(el_size);
345345
res.push_back(blob2);
346346
}
347347
return res;

blobstamper/stamp.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,5 +45,5 @@ StampBase::Load(std::shared_ptr<Blob> blob)
4545
if (res_size > blob->Size())
4646
res_size = blob->Size();
4747
}
48-
bitten_blob = blob->ShiftBytes(res_size);
48+
bitten_blob = blob->Chop(res_size);
4949
}

blobstamper/stamp_arithm.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ StampArithm<T>::ExtractStr(std::shared_ptr<Blob> blob)
4242
template<class T> T
4343
StampArithm<T>::ExtractValue(std::shared_ptr<Blob> blob)
4444
{
45-
std::vector<char> v = blob->ChopBlank(*this); /* Chop out blank of maxSize */
45+
std::vector<char> v = blob->Chop(sizeof(T))->AsByteVector(); /* Chop out blank of type's size */
4646
T *pT = (T *) &v[0]; /* And interpret it as value of arithmetic type */
4747
return *pT;
4848
}

blobstamper/stamp_text.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@
2121
std::string
2222
StampTextPulp::ExtractStr(std::shared_ptr<Blob> blob)
2323
{
24-
25-
std::vector<char> data = blob->ChopBlank(*this);
24+
std::vector<char> data = blob->Chop(minSize(), maxSize())->AsByteVector();
2625

2726
std::vector<char>::iterator the_iterator;
2827

t/001-blob-generic.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ main()
5656
char expected2[]="123";
5757

5858
std::shared_ptr<Blob> blob1 = std::make_shared<Blob>(short_sample,strlen(short_sample));
59-
std::shared_ptr<Blob> blob2 = blob1->ShiftBytes(3);
59+
std::shared_ptr<Blob> blob2 = blob1->Chop(3);
6060

6161
blob1->DataDup(ptr,size);
6262
ok(size == strlen(expected1), "Blob shifted data size ok");
@@ -73,7 +73,7 @@ main()
7373
std::shared_ptr<Blob> blob = std::make_shared<Blob>(my_data, strlen(my_data));
7474
try
7575
{
76-
std::shared_ptr<Blob> blob_res = blob->ShiftBytes(99999);
76+
std::shared_ptr<Blob> blob_res = blob->Chop(99999);
7777
ok(false, "Shift too many bytes");
7878
}
7979
catch (OutOfData)

t/320-galley-recursion-experiments.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ printf("___________________________ %i\n", blob->Size());
292292

293293
printf("llll - %i %i \n", split_data[0], split_data[1]);
294294

295-
std::shared_ptr<Blob> blob_left = blob->ShiftBytes(split_data[0]+ 2*stamp_char.minSize() );
295+
std::shared_ptr<Blob> blob_left = blob->Chop(split_data[0]+ 2*stamp_char.minSize() );
296296

297297
printf("~~~ %i\n",variant_n);
298298

@@ -303,7 +303,7 @@ std::string
303303
TestRNode2::do_recursion(std::shared_ptr<Blob> blob)
304304
{
305305
try{
306-
std::shared_ptr<Blob> tmp = blob->ShiftBytes(1);
306+
std::shared_ptr<Blob> tmp = blob->Chop(1);
307307
}
308308
catch (OutOfData)
309309
{
@@ -318,7 +318,7 @@ std::string
318318
TestRNode3::do_recursion(std::shared_ptr<Blob> blob)
319319
{
320320
try{
321-
std::shared_ptr<Blob> tmp = blob->ShiftBytes(1);
321+
std::shared_ptr<Blob> tmp = blob->Chop(1);
322322
}
323323
catch (OutOfData)
324324
{

t/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Запуск отдельного теста:
2+
3+
```
4+
LD_LIBRARY_PATH=../libtappp ./001-blob-generic.t
5+
```

t/test-chars-stamps.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ std::string
3232
StampTwoChars::ExtractStr(std::shared_ptr<Blob> blob)
3333
{
3434
/* Chopping suitable data chunk from blob */
35-
std::vector<char> data = blob->ChopBlank(*this);
35+
std::vector<char> data = blob->Chop(minSize(), maxSize())->AsByteVector();
3636

3737
size_t buf_size = data.size() + 1;
3838
char * buf = (char *) malloc(buf_size);
@@ -59,8 +59,7 @@ class StampSeveralChars: public StampBaseStr
5959
std::string
6060
StampSeveralChars::ExtractStr(std::shared_ptr<Blob> blob)
6161
{
62-
63-
std::vector<char> data = blob->ChopBlank(*this);
62+
std::vector<char> data = blob->Chop(minSize(), maxSize())->AsByteVector();
6463
/* Save optained data as string */
6564
/* NEVER do this in prod, as in real live blob is binary and may have 0 in the middle of it */
6665
char * buf;

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy