1 /*****************************************************************************\
3 * Name : object_packers *
4 * Author : Chris Koeritz *
6 *******************************************************************************
7 * Copyright (c) 1995-$now By Author. This program is free software; you can *
8 * redistribute it and/or modify it under the terms of the GNU General Public *
9 * License as published by the Free Software Foundation; either version 2 of *
10 * the License or (at your option) any later version. This is online at: *
11 * http://www.fsf.org/copyleft/gpl.html *
12 * Please send any updates to: fred@gruntose.com *
13 \*****************************************************************************/
15 #include "object_packers.h"
19 using namespace basis;
21 namespace structures {
23 // rotate_in and snag_out do most of the real "work", if any.
25 void rotate_in(byte_array &attach_into, int to_attach, int size_in_bytes)
27 basis::un_int temp = basis::un_int(to_attach);
28 for (int i = 0; i < size_in_bytes; i++) {
29 attach_into += abyte(temp % 0x100);
34 void snag_out(byte_array &eat_from, basis::un_int &accumulator, int size_in_bytes)
37 for (int i = 0; i < size_in_bytes; i++) {
39 accumulator += eat_from[size_in_bytes - i - 1];
41 eat_from.zap(0, size_in_bytes - 1);
46 int packed_size(const byte_array &packed_form)
47 { return 2 * sizeof(int) + packed_form.length(); }
49 void attach(byte_array &packed_form, const byte_array &to_attach)
51 obscure_attach(packed_form, to_attach.length());
52 packed_form += to_attach;
55 bool detach(byte_array &packed_form, byte_array &to_detach)
58 if (!obscure_detach(packed_form, len)) return false;
59 if (packed_form.length() < (int)len) return false;
60 to_detach = packed_form.subarray(0, len - 1);
61 packed_form.zap(0, len - 1);
67 // these are the only "real" attach/detach functions on number types. the
68 // others are all faking it by calling these.
70 void attach(byte_array &packed_form, basis::un_int to_attach)
71 { rotate_in(packed_form, to_attach, 4); }
73 bool detach(byte_array &packed_form, basis::un_int &to_detach)
75 if (packed_form.length() < 4) return false;
77 snag_out(packed_form, temp, 4);
78 to_detach = basis::un_int(temp);
82 void attach(byte_array &packed_form, basis::un_short to_attach)
83 { rotate_in(packed_form, to_attach, 2); }
85 bool detach(byte_array &packed_form, basis::un_short &to_detach)
87 if (packed_form.length() < 2) return false;
89 snag_out(packed_form, temp, 2);
90 to_detach = basis::un_short(temp);
94 void attach(byte_array &packed_form, abyte to_attach)
95 { packed_form += to_attach; }
97 bool detach(byte_array &packed_form, abyte &to_detach)
99 if (packed_form.length() < 1) return false;
100 to_detach = packed_form[0];
101 packed_form.zap(0, 0);
107 void attach(byte_array &packed_form, int to_attach)
108 { attach(packed_form, basis::un_int(to_attach)); }
110 bool detach(byte_array &packed_form, int &to_detach)
111 { return detach(packed_form, (basis::un_int &)to_detach); }
113 void attach(byte_array &packed_form, signed_long to_attach)
114 { attach(packed_form, basis::signed_long(to_attach)); }
116 bool detach(byte_array &packed_form, signed_long &to_detach)
117 { return detach(packed_form, (basis::signed_long &)to_detach); }
119 //void attach(byte_array &packed_form, basis::un_long to_attach)
120 //{ attach(packed_form, basis::un_int(to_attach)); }
122 //bool detach(byte_array &packed_form, basis::un_long &to_detach)
123 //{ return detach(packed_form, (basis::un_int &)to_detach); }
125 //void attach(byte_array &packed_form, long to_attach)
126 //{ attach(packed_form, basis::un_int(to_attach)); }
128 //bool detach(byte_array &packed_form, long &to_detach)
129 //{ return detach(packed_form, (basis::un_int &)to_detach); }
131 void attach(byte_array &packed_form, short to_attach)
132 { attach(packed_form, basis::un_short(to_attach)); }
134 bool detach(byte_array &packed_form, short &to_detach)
135 { return detach(packed_form, (basis::un_short &)to_detach); }
137 void attach(byte_array &packed_form, char to_attach)
138 { attach(packed_form, abyte(to_attach)); }
140 bool detach(byte_array &packed_form, char &to_detach)
141 { return detach(packed_form, (abyte &)to_detach); }
143 void attach(byte_array &packed_form, bool to_attach)
144 { attach(packed_form, abyte(to_attach)); }
148 // can't assume that bool is same size as byte, although it should fit
149 // into a byte just fine.
150 bool detach(byte_array &packed_form, bool &to_detach)
153 if (!detach(packed_form, chomp)) return false;
158 // operates on a number less than 1.0 that we need to snag the next digit
159 // to the right of the decimal point from.
160 double break_off_digit(double &input) {
161 //printf(astring(astring::SPRINTF, "break input=%f\n", input).s());
163 //printf(astring(astring::SPRINTF, "after mult=%f\n", input).s());
164 double mod_part = fmod(input, 1.0);
165 //printf(astring(astring::SPRINTF, "modded=%f\n", mod_part).s());
166 double to_return = input - mod_part;
167 //printf(astring(astring::SPRINTF, "to ret=%f\n", to_return).s());
172 //hmmm: not very efficient! it's just packing and wasting bytes doing it...
173 int packed_size(double to_pack)
176 attach(packed, to_pack);
177 return packed.length();
180 void attach(byte_array &packed_form, double to_pack)
183 double mantissa = frexp(to_pack, &exponent);
184 abyte pos = mantissa < 0.0? false : true;
185 mantissa = fabs(mantissa);
186 //printf("mant=%10.10f pos=%d expon=%d\n", mantissa, int(pos), exponent);
188 attach(packed_form, exponent);
190 // even if the double has 52 bits for mantissa (where ms docs say 44),
191 // a 16 digit bcd encoded number should handle the size (based on size of
193 for (int i = 0; i < 9; i++) {
194 double dig1 = break_off_digit(mantissa);
195 //printf(astring(astring::SPRINTF, "break digit=%d\n", int(dig1)).s());
196 double dig2 = break_off_digit(mantissa);
197 //printf(astring(astring::SPRINTF, "break digit=%d\n", int(dig2)).s());
198 mantis += abyte(dig1 * 16 + dig2);
200 attach(packed_form, mantis);
201 //printf("attach exit\n");
204 bool detach(byte_array &packed_form, double &to_unpack)
206 //printf("detach entry\n");
207 if (packed_form.length() < 1) return false; // no sign byte.
208 abyte pos = packed_form[0];
209 //printf(astring(astring::SPRINTF, "pos=%d\n", int(pos)).s());
210 packed_form.zap(0, 0);
212 if (!detach(packed_form, exponent)) return false;
213 //printf(astring(astring::SPRINTF, "expon=%d\n", exponent).s());
215 if (!detach(packed_form, mantis)) return false;
217 for (int i = mantis.last(); i >= 0; i--) {
218 abyte chop = mantis[i];
219 double dig1 = chop / 16;
220 //printf(astring(astring::SPRINTF, "break digit=%d\n", int(dig1)).s());
221 double dig2 = chop % 16;
222 //printf(astring(astring::SPRINTF, "break digit=%d\n", int(dig2)).s());
228 //printf(astring(astring::SPRINTF, "mant=%10.10f\n", mantissa).s());
229 to_unpack = ldexp(mantissa, exponent);
230 if (!pos) to_unpack = -1.0 * to_unpack;
231 //printf("pos=%d\n", int(pos));
232 //printf(astring(astring::SPRINTF, "to_unpack=%f\n", to_unpack).s());
233 //printf("detach exit\n");
237 void attach(byte_array &packed_form, float to_pack)
238 { attach(packed_form, double(to_pack)); }
240 bool detach(byte_array &packed_form, float &to_unpack)
243 bool to_return = detach(packed_form, real_unpack);
244 to_unpack = (float)real_unpack;
250 void obscure_attach(byte_array &packed_form, un_int to_attach)
252 //printf("initial value=%x\n", to_attach);
253 basis::un_int first_part = 0xfade0000;
254 //printf("first part curr=%x\n", first_part);
255 basis::un_int second_part = 0x0000ce0f;
256 //printf("second part curr=%x\n", second_part);
257 first_part = first_part | (to_attach & 0x0000ffff);
258 //printf("first part now=%x\n", first_part);
259 second_part = second_part | (to_attach & 0xffff0000);
260 //printf("second part now=%x\n", second_part);
261 attach(packed_form, first_part);
262 attach(packed_form, second_part);
265 bool obscure_detach(byte_array &packed_form, un_int &to_detach)
267 basis::un_int first_part;
268 basis::un_int second_part;
269 if (!detach(packed_form, first_part)) return false;
270 if (!detach(packed_form, second_part)) return false;
271 //printf("first part after unpack=%x\n", first_part);
272 //printf("second part after unpack=%x\n", second_part);
273 if (basis::un_int(first_part & 0xffff0000) != basis::un_int(0xfade0000)) return false;
274 //printf("first part with and=%x\n", first_part & 0xffff0000);
275 if (basis::un_int(second_part & 0x0000ffff) != basis::un_int(0x0000ce0f)) return false;
276 //printf("second part with and=%x\n", second_part & 0x0000ffff);
277 to_detach = int( (second_part & 0xffff0000) + (first_part & 0x0000ffff) );
278 //printf("final result=%x\n", to_detach);