66
77#include " libblobstamper.h"
88
9- wflMemCtx static_ctx;
109
11- wflMemCtx *
12- wflCreateMemCtx ()
10+ Blob::Blob (char * data_in,int size_in)
1311{
14- return &static_ctx;
12+ data = data_in;
13+ size = size_in;
14+ begin =0 ;
15+ end = size;
1516}
1617
17-
18- void
19- wflDestroyMemCtx (wflMemCtx * mctx)
18+ bool
19+ Blob::isEmpty ()
2020{
21+ if (! data)return true ;
22+ return false ;
2123}
2224
23-
2425void
25- wflBlobDump (wflBlobDsc* blob)
26+ wflBlobDump (Blob blob)
2627{
27- int length = blob-> end - blob-> begin +1 ;
28+ int length = blob. end - blob. begin +1 ;
2829char * str =(char *)malloc (length +1 );// second +1 is for \0
2930// FIXME проверка null
3031 str[0 ]=' \0 ' ;
3132
32- strncat (str, blob-> data + blob-> begin , length);
33+ strncat (str, blob. data + blob. begin , length);
3334
3435printf (" %s\n " ,str);
3536free (str);
3637}
3738
38- void *
39- wflMalloc (wflMemCtx * mctx,size_t size)
40- {
41- /* just that simple for now*/
42- return malloc ( size );
43- }
44-
45- void
46- wflFree (wflMemCtx * mctx,void * ptr)
47- {
48- /* just that simple for now*/
49- free (ptr);
50- }
51-
52-
53- wflBlobDsc*
54- wflShiftN (wflBlobDsc* blob,size_t n)
39+ Blob
40+ wflShiftN (Blob &blob,size_t n)
5541{
56- wflBlobDsc* new_blob;
57- // FIXME null check here;
58- if (blob->begin + n > blob->end )
59- return NULL ;/* not enough data*/
42+ if (blob.begin + n > blob.end )
43+ {
44+ Blobempty (NULL , -1 );
45+ return empty;/* not enough data*/
46+ }
6047
61- new_blob = (wflBlobDsc*) wflMalloc ( blob-> mctx , sizeof (wflBlobDsc) );
48+ Blob new_blob ( blob. data , blob. size );
6249
63- new_blob->data = blob->data ;
64- new_blob->begin = blob->begin ;
65- new_blob->end = blob->begin + n -1 ;
66- new_blob->mctx = blob->mctx ;
50+ new_blob.begin = blob.begin ;
51+ new_blob.end = blob.begin + n -1 ;
6752
68- blob-> begin += n;
53+ blob. begin += n;
6954
7055return new_blob;
7156}
7257
7358std::string
74- wflShiftDouble (wflBlobDsc* blob)
59+ wflShiftDouble (Blob & blob)
7560{
7661int ret;
7762double * d;
7863char * resc;
7964 std::string res;
8065
8166
82- wflBlobDsc * b2 =wflShiftN (blob,sizeof (double ));
83- if (! b2 )return " " ;
67+ Blob b2 =wflShiftN (blob,sizeof (double ));
68+ if (b2. isEmpty () )return " " ;
8469
85- d = (double *)( (char *)b2->data + b2->begin );
86- wflFree (blob->mctx , b2);
70+ d = (double *)( (char *)b2.data + b2.begin );
8771
8872int size_s =snprintf (nullptr ,0 ," %.999g" , *d) +1 ;
8973if (size_s <=0 )
@@ -112,7 +96,7 @@ wflShiftDouble(wflBlobDsc* blob)
11296}
11397
11498std::string
115- wflShiftPgPoint (wflBlobDsc* blob)
99+ wflShiftPgPoint (Blob & blob)
116100{
117101 std::string res =" " ;
118102 std::string x, y;
@@ -129,7 +113,7 @@ wflShiftPgPoint(wflBlobDsc* blob)
129113
130114
131115std::string
132- wflShiftPgPath (wflBlobDsc* blob)
116+ wflShiftPgPath (Blob & blob)
133117{
134118 std::string res =" " ;
135119 std::string point;
@@ -154,29 +138,19 @@ poly_contain_prepare(char* in, int in_size, char ** res1, char ** res2)
154138{
155139 *res1 =NULL ;
156140 *res2 =NULL ;
157- wflMemCtx * mctx;
158- wflBlobDsc blob;
159- wflBlobDsc * b2;
160141
161142 std::string r1, r2;
143+ Blobblob (in, in_size);
162144
163- mctx =wflCreateMemCtx ();
164-
165- blob.mctx = mctx;
166- blob.data = in;
167- blob.begin =0 ;
168- blob.end = in_size;
169-
170-
171- r1 =wflShiftPgPoint (&blob);
145+ r1 =wflShiftPgPoint (blob);
172146
173147if (r1.empty ())
174148 {
175149fprintf (stderr," Problema1\n " );
176150return 1 ;
177151 }
178152
179- r2 =wflShiftPgPath (& blob);
153+ r2 =wflShiftPgPath (blob);
180154
181155if (r2.empty ())
182156 {
@@ -196,34 +170,19 @@ poly_contain_prepare(char* in, int in_size, char ** res1, char ** res2)
196170int
197171poly_center_prepare (char * in,int in_size,char ** res2)
198172{
199- // *res1 = NULL;
200173 *res2 =NULL ;
201- wflMemCtx * mctx;
202- wflBlobDsc blob;
203- wflBlobDsc * b2;
204174
205175 std::string r2;
176+ Blobblob (in, in_size);
206177
207- mctx =wflCreateMemCtx ();
208-
209- blob.mctx = mctx;
210- blob.data = in;
211- blob.begin =0 ;
212- blob.end = in_size;
213-
214-
215-
216- r2 =wflShiftPgPath (& blob);
178+ r2 =wflShiftPgPath (blob);
217179
218180if (r2.empty ())
219181 {
220182fprintf (stderr," Problema2\n " );
221183return 1 ;
222184 }
223185
224- // *res1 = (char *) malloc(strlen(r1.c_str()) + 1);
225- // memcpy(*res1, r1.c_str(), strlen(r1.c_str()) + 1);
226-
227186 *res2 = (char *)malloc (strlen (r2.c_str ())+1 );
228187memcpy (*res2, r2.c_str (),strlen (r2.c_str ()) +1 );
229188