|
| 1 | + |
| 2 | + |
| 3 | + |
| 4 | +#include"blob.h" |
| 5 | +#include"stamp.h" |
| 6 | +#include"stamp_atomic.h" |
| 7 | +#include"galley.h" |
| 8 | +#include<vector> |
| 9 | + |
| 10 | + |
| 11 | +std::list<std::string> |
| 12 | +GalleySeries::Extract(Blob &blob) |
| 13 | +{ |
| 14 | + std::list<std::string> res; |
| 15 | + |
| 16 | +if (stamp.isFixedSize()) |
| 17 | + { |
| 18 | +while (1) |
| 19 | + { |
| 20 | + std::string el = blob.ShiftSingleStampStr(stamp); |
| 21 | +if (el.empty()) |
| 22 | +break; |
| 23 | + res.push_back(el); |
| 24 | + } |
| 25 | + } |
| 26 | +else |
| 27 | + { |
| 28 | +if (stamp.maxSize() == -1)// if unlimited size |
| 29 | + { |
| 30 | +/* |
| 31 | + The idea of this part is following: |
| 32 | + We get two bytes from blob, and treat it as an oracle that says in how many parts blob shold be split. |
| 33 | +
|
| 34 | + We use stamp.minSize() and blob.Size() to determinate how many parts are possible at maximum (count_max) |
| 35 | + and normalize oracle so it would fit count_max in any case. Thus we get count_target the number of parts we |
| 36 | + wil have |
| 37 | +
|
| 38 | + Then we shift a size oracle for each part |
| 39 | +
|
| 40 | + Then we normalize size oracles to they in total will give us the size of remaining blob. Here we should |
| 41 | + keep in mind that part cant' be shorter than stamp.minSize() |
| 42 | +
|
| 43 | + Then we chop blob into selected sizes, and stap each chopped part with stamp. |
| 44 | +*/ |
| 45 | + |
| 46 | +/* Getting count oracle and normalze it to fit available size*/ |
| 47 | +size_t count_max = (blob.Size() - ORACLE_SIZE) / (stamp.minSize() + ORACLE_SIZE);//First oracle - for number of items, and second one is oracle for each item size |
| 48 | + ORACLE_STAMP stamp_oracle; |
| 49 | + ORACLE_TYPE *count_oracle; |
| 50 | + count_oracle = (ORACLE_TYPE *) blob.ShiftSingleStampBin(stamp_oracle); |
| 51 | + |
| 52 | + ORACLE_TYPE count_target = count_max * (*count_oracle) / ORACLE_MAX +1;/* +1 -- это грубая эмуляция округления вверх. oracle == ORACLE_MAX-1 == 65534 должен дать count_max*/ |
| 53 | +if (count_target > count_max) count_target = count_max;// В случае если oracle оказался рваен ORACLE_MAX |
| 54 | + |
| 55 | +/* Getting size oracles for each part*/ |
| 56 | + std::vector<ORACLE_TYPE> size_oracles; |
| 57 | +int size_oracle_total =0; |
| 58 | +for(int i =0; i<count_target; i++) |
| 59 | + { |
| 60 | + ORACLE_TYPE *o = (ORACLE_TYPE *) blob.ShiftSingleStampBin(stamp_oracle); |
| 61 | + size_oracles.push_back(*o); |
| 62 | + size_oracle_total += *o; |
| 63 | +free(o); |
| 64 | + } |
| 65 | + |
| 66 | +/* Calculating available vairable size, that will be destributed between parts according to size oracles*/ |
| 67 | +int data_size = blob.Size(); |
| 68 | +int fixed_data_size = stamp.minSize() * count_target; |
| 69 | +int var_data_size = data_size - fixed_data_size; |
| 70 | + |
| 71 | +/* normalizing oracles so they fit total variable size, chop to parts and stamp parts*/ |
| 72 | +float remainder =0;/* we do not want waste bytes because of rounding, so we keep the remainder, and reuse it. Thus we will use all bytes (alomost, may loose last one due to remainder=0.99999)*/ |
| 73 | +for(ORACLE_TYPE o : size_oracles) |
| 74 | + { |
| 75 | +float el_size_f = stamp.minSize() + (float) o / size_oracle_total * var_data_size + remainder; |
| 76 | +int el_size = el_size_f; |
| 77 | + remainder = el_size_f - el_size; |
| 78 | + |
| 79 | + Blob blob2 = blob.ShiftBytes(el_size); |
| 80 | + std::string str = blob2.ShiftSingleStampStr(stamp); |
| 81 | + res.push_back(str); |
| 82 | + } |
| 83 | +free(count_oracle); |
| 84 | + } |
| 85 | +else |
| 86 | + { |
| 87 | +printf("Not implemented yet!"); |
| 88 | +exit(1); |
| 89 | + } |
| 90 | + } |
| 91 | +return res; |
| 92 | +} |
| 93 | + |
| 94 | + |
| 95 | +int |
| 96 | +GalleySeries::minSize() |
| 97 | +{ |
| 98 | +if (stamp.isFixedSize()) |
| 99 | + { |
| 100 | +return stamp.minSize();// When size is fixed, series can have only one member with no extra data used |
| 101 | + } |
| 102 | +else |
| 103 | + { |
| 104 | +if (stamp.maxSize() == -1)// if unlimited size |
| 105 | + { |
| 106 | +return stamp.minSize() + ORACLE_SIZE *2;// One -- count oracle, one -- size oracle |
| 107 | + } |
| 108 | +else |
| 109 | + { |
| 110 | +printf("Not implemented yet!"); |
| 111 | +exit(1); |
| 112 | + } |
| 113 | + } |
| 114 | +} |