summaryrefslogtreecommitdiffstats
path: root/debian/htdig/htdig-3.2.0b6/htlib/HtPack.cc
blob: 8026622d8a95108a7bc63686376e2ba20ce06ce4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
//
// HtPack.cc
//
// HtPack: Compress and uncompress data in e.g. simple structures.
//	   The structure must have the layout defined in the ABI;
//	   the layout the compiler generates.
//
// Part of the ht://Dig package   <http://www.htdig.org/>
// Copyright (c) 1999-2004 The ht://Dig Group
// For copyright details, see the file COPYING in your distribution
// or the GNU Library General Public License (LGPL) version 2 or later 
// <http://www.gnu.org/copyleft/lgpl.html>
//
// $Id: HtPack.cc,v 1.8 2004/05/28 13:15:20 lha Exp $
//

#ifdef HAVE_CONFIG_H
#include "htconfig.h"
#endif /* HAVE_CONFIG_H */

#include "HtPack.h"

#include <ctype.h>
#include <stdlib.h>

// For the moment, these formats are accepted:
// "i" native int, with most compressed value 0
// "u" unsigned int, with most compressed value 0
// "c" unsigned int, with most compressed value 1.
//
//  If someone adds other formats (and uses them), please note
// that structure padding may give surprising effects on some
// (most) platforms, for example if you try to unpack a
// structure with the imagined signature "isi" (int, short, int).
//  You will want to solve that portably.
//
// Compression is done to 2 bits description (overhead) each,
// plus variable-sized data.
//  Theoretically, different formats can use different number of
// bits in the description with a few changes.
//  The description is located in a byte before every four
// "fields".
String
htPack(const char format[], const char *data)
{
  const char *s = format;

  // We insert the encodings by number, rather than shifting and
  // inserting at the "bottom".	 This should make it faster for
  // decoding, which presumably is more important than the speed
  // of encoding.
  int code_no = 0;

  // Make a wild guess that we will compress some ordinary sized
  // struct.  This guess only has speed effects.
  String compressed(60);

  // Accumulated codes.
  unsigned int description = 0;

  // Store the encoding here.  We cannot use a char *, as the
  // string may be reallocated and moved.
  int code_index = 0;

  // Make place for the first codes.
  compressed << '\0';

  // Format string loop.
  while (*s)
  {
    int fchar = *s++;
    int n;

    if (isdigit(*s))
    {
      char* t;
      n = strtol(s, &t, 10);
      s = t;
    }
    else
      n = 1;

    // Loop over N in e.g. "iN" (default 1).
    while (n--)
    {
      // Format character handling.
      switch (fchar)
      {
	case 'c':
	{
	  // We compress an unsigned int with the most common
	  // value 1 as this:
	  // 00 - value is 1.
	  // 01 - value fits in unsigned char - appended.
	  // 10 - value fits in unsigned short - appended.
	  // 11 - just plain unsigned int - appended (you lose).
	  unsigned int value;

	  // Initialize, but allow disalignment.
	  memcpy(&value, data, sizeof value);
	  data += sizeof(unsigned int);

	  int mycode;
	  if (value == 1)
	  {
	    mycode = 0;
	  }
	  else
	  {
	    unsigned char charvalue = (unsigned char) value;
	    unsigned short shortvalue = (unsigned short) value;
	    if (value == charvalue)
	    {
	      mycode = 1;
	      compressed << charvalue;
	    }
	    else if (value == shortvalue)
	    {
	      mycode = 2;
	      compressed.append((char *) &shortvalue, sizeof shortvalue);
	    }
	    else
	    {
	      mycode = 3;
	      compressed.append((char *) &value, sizeof value);
	    }
	  }

	  description |= mycode << (2*code_no++);
	}
	break;

	case 'i':
	{
	  // We compress a (signed) int as follows:
	  // 00 - value is 0.
	  // 01 - value fits in char - appended.
	  // 10 - value fits in short - appended.
	  // 11 - just plain int - appended (you lose).
	  int value;

	  // Initialize, but allow disalignment.
	  memcpy(&value, data, sizeof value);
	  data += sizeof(int);

	  int mycode;
	  if (value == 0)
	  {
	    mycode = 0;
	  }
	  else
	  {
	    char charvalue = char(value);
	    short shortvalue = short(value);
	    if (value == charvalue)
	    {
	      mycode = 1;
	      compressed << charvalue;
	    }
	    else if (value == shortvalue)
	    {
	      mycode = 2;
	      compressed.append((char *) &shortvalue, sizeof shortvalue);
	    }
	    else
	    {
	      mycode = 3;
	      compressed.append((char *) &value, sizeof value);
	    }
	  }

	  description |= mycode << (2*code_no++);
	}
	break;

	case 'u':
	{
	  // We compress an unsigned int like an int:
	  // 00 - value is 0.
	  // 01 - value fits in unsigned char - appended.
	  // 10 - value fits in unsigned short - appended.
	  // 11 - just plain unsigned int - appended (you lose).
	  unsigned int value;

	  // Initialize, but allow disalignment.
	  memcpy(&value, data, sizeof value);
	  data += sizeof(unsigned int);

	  int mycode;
	  if (value == 0)
	  {
	    mycode = 0;
	  }
	  else
	  {
	    unsigned char charvalue = (unsigned char) value;
	    unsigned short shortvalue = (unsigned short) value;
	    if (value == charvalue)
	    {
	      mycode = 1;
	      compressed << charvalue;
	    }
	    else if (value == shortvalue)
	    {
	      mycode = 2;
	      compressed.append((char *) &shortvalue, sizeof shortvalue);
	    }
	    else
	    {
	      mycode = 3;
	      compressed.append((char *) &value, sizeof value);
	    }
	  }

	  description |= mycode << (2*code_no++);
	}
	break;

	default:
#ifndef NOSTREAM
#ifdef DEBUG
	  if (1)
	    cerr << "Invalid char \'" << char(fchar)
		 << "\' in pack format \"" << format << "\""
		 << endl;
	  return "";
#endif
#endif
	  ; // Must always have a statement after a label.
      }

      // Assuming 8-bit chars here.  Flush encodings after 4 (2 bits
      // each) or when the code-string is consumed.
      if (code_no == 4 || (n == 0 && *s == 0))
      {
	char *codepos = compressed.get() + code_index;

	*codepos = description;
	description = 0;
	code_no = 0;

	if (n || *s)
	{
	  // If more data to be encoded, then we need a new place to
	  // store the encodings.
	  code_index = compressed.length();
	  compressed << '\0';
	}
      }
    }
  }

  return compressed;
}


// Reverse the effect of htPack.
String
htUnpack(const char format[], const char *data)
{
  const char *s = format;

  // The description needs to be renewed immediately.
  unsigned int description = 1;

  // Make a wild guess about that we decompress to some ordinary
  // sized struct and assume the cost of allocation some extra
  // memory is much less than the cost of allocating more.
  // This guess only has speed effects.
  String decompressed(60);

  // Format string loop.
  while (*s)
  {
    int fchar = *s++;
    int n;

    if (isdigit(*s))
    {
      char* t;
      n = strtol(s, &t, 10);
      s = t;
    }
    else
      n = 1;

    // Loop over N in e.g. "iN" (default 1).
    while (n--)
    {
      // Time to renew description?
      if (description == 1)
	description = 256 | *data++;

      // Format character handling.
      switch (fchar)
      {
	case 'c':
	{
	  // An unsigned int with the most common value 1 is
          // compressed as follows:
	  // 00 - value is 1.
	  // 01 - value fits in unsigned char - appended.
	  // 10 - value fits in unsigned short - appended.
	  // 11 - just plain unsigned int - appended (you lose).
	  unsigned int value;

	  switch (description & 3)
	  {
	    case 0:
	    value = 1;
	    break;

	    case 1:
	    {
	      unsigned char charvalue;
	      memcpy(&charvalue, data, sizeof charvalue);
	      value = charvalue;
	      data++;
	    }
	    break;

	    case 2:
	    {
	      unsigned short int shortvalue;
	      memcpy(&shortvalue, data, sizeof shortvalue);
	      value = shortvalue;
	      data += sizeof shortvalue;
	    }
	    break;

	    case 3:
	    {
	      memcpy(&value, data, sizeof value);
	      data += sizeof value;
	    }
	    break;
	  }
	  decompressed.append((char *) &value, sizeof value);
	}
	break;

	case 'i':
	{
	  // A (signed) int is compressed as follows:
	  // 00 - value is 0.
	  // 01 - value fits in char - appended.
	  // 10 - value fits in short - appended.
	  // 11 - just plain int - appended (you lose).
	  int value;

	  switch (description & 3)
	  {
	    case 0:
	    value = 0;
	    break;

	    case 1:
	    {
	      char charvalue;
	      memcpy(&charvalue, data, sizeof charvalue);
	      value = charvalue;
	      data++;
	    }
	    break;

	    case 2:
	    {
	      short int shortvalue;
	      memcpy(&shortvalue, data, sizeof shortvalue);
	      value = shortvalue;
	      data += sizeof shortvalue;
	    }
	    break;

	    case 3:
	    {
	      memcpy(&value, data, sizeof value);
	      data += sizeof value;
	    }
	    break;
	  }
	  decompressed.append((char *) &value, sizeof value);
	}
	break;

	case 'u':
	{
	  // An unsigned int is compressed as follows:
	  // 00 - value is 0.
	  // 01 - value fits in unsigned char - appended.
	  // 10 - value fits in unsigned short - appended.
	  // 11 - just plain unsigned int - appended (you lose).
	  unsigned int value;

	  switch (description & 3)
	  {
	    case 0:
	    value = 0;
	    break;

	    case 1:
	    {
	      unsigned char charvalue;
	      memcpy(&charvalue, data, sizeof charvalue);
	      value = charvalue;
	      data++;
	    }
	    break;

	    case 2:
	    {
	      unsigned short int shortvalue;
	      memcpy(&shortvalue, data, sizeof shortvalue);
	      value = shortvalue;
	      data += sizeof shortvalue;
	    }
	    break;

	    case 3:
	    {
	      memcpy(&value, data, sizeof value);
	      data += sizeof value;
	    }
	    break;
	  }
	  decompressed.append((char *) &value, sizeof value);
	}
	break;

	default:
#ifndef NOSTREAM
#ifdef DEBUG
	  if (1)
	    cerr << "Invalid char \'" << char(fchar)
		 << "\' in unpack format \"" << format << "\""
		 << endl;
	  return "";
#endif
#endif
	  ; // Must always have a statement after a label.
      }

      description >>= 2;
    }
  }

  return decompressed;
}

// End of HtPack.cc