]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/spotlight_marshalling.c
Remove accesive logging
[netatalk.git] / etc / afpd / spotlight_marshalling.c
1 /*
2   Copyright (c) 2012 Frank Lahm <franklahm@gmail.com>
3
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU General Public License as published by
6   the Free Software Foundation; either version 2 of the License, or
7   (at your option) any later version.
8
9   This program is distributed in the hope that it will be useful,
10   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   GNU General Public License for more details.
13 */
14
15 #ifdef HAVE_CONFIG_H
16 #include "config.h"
17 #endif /* HAVE_CONFIG_H */
18
19 #include <string.h>
20 #include <strings.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <errno.h>
24 #include <stdbool.h>
25 #include <inttypes.h>
26
27 #include <atalk/errchk.h>
28 #include <atalk/util.h>
29 #include <atalk/logger.h>
30 #include <atalk/talloc.h>
31 #include <atalk/dalloc.h>
32 #include <atalk/byteorder.h>
33 #include <atalk/netatalk_conf.h>
34 #include <atalk/volume.h>
35
36 #include "spotlight.h"
37
38 /**************************************************************************************************
39  * RPC data marshalling and unmarshalling
40  **************************************************************************************************/
41
42 /* Spotlight epoch is UNIX epoch minus SPOTLIGHT_TIME_DELTA */
43 #define SPOTLIGHT_TIME_DELTA INT64_C(280878921600U)
44
45 #define SQ_TYPE_NULL    0x0000
46 #define SQ_TYPE_COMPLEX 0x0200
47 #define SQ_TYPE_INT64   0x8400
48 #define SQ_TYPE_BOOL    0x0100
49 #define SQ_TYPE_FLOAT   0x8500
50 #define SQ_TYPE_DATA    0x0700
51 #define SQ_TYPE_CNIDS   0x8700
52 #define SQ_TYPE_UUID    0x0e00
53 #define SQ_TYPE_DATE    0x8600
54 #define SQ_TYPE_TOC     0x8800
55
56 #define SQ_CPX_TYPE_ARRAY           0x0a00
57 #define SQ_CPX_TYPE_STRING          0x0c00
58 #define SQ_CPX_TYPE_UTF16_STRING    0x1c00
59 #define SQ_CPX_TYPE_DICT            0x0d00
60 #define SQ_CPX_TYPE_CNIDS           0x1a00
61 #define SQ_CPX_TYPE_FILEMETA        0x1b00
62
63 #define SUBQ_SAFETY_LIM 20
64
65 /* Forward declarations */
66 static int sl_pack_loop(DALLOC_CTX *query, char *buf, int offset, char *toc_buf, int *toc_idx);
67 static int sl_unpack_loop(DALLOC_CTX *query, const char *buf, int offset, uint count, const uint toc_offset, const uint encoding);
68
69 /*
70 * Returns the UTF-16 string encoding, by checking the 2-byte byte order mark.
71 * If there is no byte order mark, -1 is returned.
72 */
73 static uint spotlight_get_utf16_string_encoding(const char *buf, int offset, int query_length, uint encoding) {
74     uint utf16_encoding;
75
76     /* check for byte order mark */
77     utf16_encoding = SL_ENC_BIG_ENDIAN;
78     if (query_length >= 2) {
79         uint16_t byte_order_mark;
80         if (encoding == SL_ENC_LITTLE_ENDIAN)
81             byte_order_mark = SVAL(buf, offset);
82         else
83             byte_order_mark = RSVAL(buf, offset);
84
85         if (byte_order_mark == 0xFFFE) {
86             utf16_encoding = SL_ENC_BIG_ENDIAN | SL_ENC_UTF_16;
87         }
88         else if (byte_order_mark == 0xFEFF) {
89             utf16_encoding = SL_ENC_LITTLE_ENDIAN | SL_ENC_UTF_16;
90         }
91     }
92
93     return utf16_encoding;
94 }
95
96 /**************************************************************************************************
97  * marshalling functions
98  **************************************************************************************************/
99
100 #define SL_OFFSET_DELTA 16
101
102 static uint64_t sl_pack_tag(uint16_t type, uint16_t size_or_count, uint32_t val)
103 {
104     uint64_t tag = ((uint64_t)val << 32) | ((uint64_t)type << 16) | size_or_count;
105     return tag;
106 }
107
108 static int sl_pack_float(double d, char *buf, int offset)
109 {
110     union {
111         double d;
112         uint64_t w;
113     } ieee_fp_union;
114
115     SLVAL(buf, offset, sl_pack_tag(SQ_TYPE_FLOAT, 2, 1));
116     SLVAL(buf, offset + 8, ieee_fp_union.w);
117
118     return offset + 2 * sizeof(uint64_t);
119 }
120
121 static int sl_pack_uint64(uint64_t u, char *buf, int offset)
122 {
123     SLVAL(buf, offset, sl_pack_tag(SQ_TYPE_INT64, 2, 1));
124     SLVAL(buf, offset + 8, u);
125
126     return offset + 2 * sizeof(uint64_t);
127 }
128
129 static int sl_pack_bool(sl_bool_t bl, char *buf, int offset)
130 {
131     SLVAL(buf, offset, sl_pack_tag(SQ_TYPE_BOOL, 1, bl ? 1 : 0));
132
133     return offset + sizeof(uint64_t);
134 }
135
136 static int sl_pack_nil(char *buf, int offset)
137 {
138     SLVAL(buf, offset, sl_pack_tag(SQ_TYPE_NULL, 1, 1));
139
140     return offset + sizeof(uint64_t);
141 }
142
143 static int sl_pack_date(sl_time_t t, char *buf, int offset)
144 {
145     uint64_t data = 0;
146
147     data = (t.tv_sec + SPOTLIGHT_TIME_DELTA) << 24;
148
149     SLVAL(buf, offset, sl_pack_tag(SQ_TYPE_DATE, 2, 1));
150     SLVAL(buf, offset + 8, data);
151
152     return offset + 2 * sizeof(uint64_t);
153 }
154
155 static int sl_pack_uuid(sl_uuid_t *uuid, char *buf, int offset)
156 {
157     SLVAL(buf, offset, sl_pack_tag(SQ_TYPE_UUID, 3, 1));
158     memcpy(buf + offset + 8, uuid, 16);
159
160     return offset + sizeof(uint64_t) + 16;
161 }
162
163 static int sl_pack_CNID(sl_cnids_t *cnids, char *buf, int offset, char *toc_buf, int *toc_idx)
164 {
165     int len = 0, off = 0;
166     int cnid_count = talloc_array_length(cnids->ca_cnids);
167     uint64_t id;
168
169     SLVAL(toc_buf, *toc_idx * 8, sl_pack_tag(SQ_CPX_TYPE_CNIDS, (offset + SL_OFFSET_DELTA) / 8, 0 /* cnid_count */));
170     SLVAL(buf, offset, sl_pack_tag(SQ_TYPE_COMPLEX, 1, *toc_idx + 1));
171     *toc_idx += 1;
172     offset += 8;
173
174     SLVAL(buf, offset, sl_pack_tag(SQ_TYPE_CNIDS, 2 + cnid_count, 8 /* unknown meaning, but always 8 */));
175     offset += 8;
176
177     if (cnid_count > 0) {
178         SLVAL(buf, offset, sl_pack_tag(0x0add, cnid_count, cnids->ca_context));
179         offset += 8;
180
181         for (int i = 0; i < cnid_count; i++) {
182             memcpy(&id, cnids->ca_cnids->dd_talloc_array[i], sizeof(uint64_t));
183             SLVAL(buf, offset, id);
184             offset += 8;
185         }
186     }
187     
188     return offset;
189 }
190
191 static int sl_pack_array(sl_array_t *array, char *buf, int offset, char *toc_buf, int *toc_idx)
192 {
193     int count = talloc_array_length(array->dd_talloc_array);
194     int octets = (offset + SL_OFFSET_DELTA) / 8;
195
196     SLVAL(toc_buf, *toc_idx * 8, sl_pack_tag(SQ_CPX_TYPE_ARRAY, octets, count));
197     SLVAL(buf, offset, sl_pack_tag(SQ_TYPE_COMPLEX, 1, *toc_idx + 1));
198     *toc_idx += 1;
199     offset += 8;
200
201     offset = sl_pack_loop(array, buf, offset, toc_buf, toc_idx);
202
203     return offset;
204 }
205
206 static int sl_pack_dict(sl_array_t *dict, char *buf, int offset, char *toc_buf, int *toc_idx)
207 {
208     SLVAL(toc_buf, *toc_idx * 8, sl_pack_tag(SQ_CPX_TYPE_DICT, (offset + SL_OFFSET_DELTA) / 8, talloc_array_length(dict->dd_talloc_array)));
209     SLVAL(buf, offset, sl_pack_tag(SQ_TYPE_COMPLEX, 1, *toc_idx + 1));
210     *toc_idx += 1;
211     offset += 8;
212
213     offset = sl_pack_loop(dict, buf, offset, toc_buf, toc_idx);
214
215     return offset;
216 }
217
218 static int sl_pack_filemeta(sl_filemeta_t *fm, char *buf, int offset, char *toc_buf, int *toc_idx)
219 {
220     int fmlen;                  /* lenght of filemeta */
221     int saveoff = offset;
222
223     SLVAL(buf, offset, sl_pack_tag(SQ_TYPE_COMPLEX, 1, *toc_idx + 1));
224     offset += 16;
225
226     fmlen = sl_pack(fm, buf + offset);
227     offset += fmlen;
228
229     SLVAL(buf, saveoff + 8, sl_pack_tag(SQ_TYPE_DATA, (fmlen / 8) + 1, 8 /* unknown meaning, but always 8 */));
230
231     SLVAL(toc_buf, *toc_idx * 8, sl_pack_tag(SQ_CPX_TYPE_FILEMETA, (saveoff + SL_OFFSET_DELTA) / 8, fmlen / 8));
232     *toc_idx += 1;
233
234     return offset;
235 }
236
237 static int sl_pack_string(char *s, char *buf, int offset, char *toc_buf, int *toc_idx)
238 {
239     int len, octets, used_in_last_octet;
240     len = strlen(s);
241     octets = (len / 8) + (len & 7 ? 1 : 0);
242     used_in_last_octet = 8 - (octets * 8 - len);
243
244     SLVAL(toc_buf, *toc_idx * 8, sl_pack_tag(SQ_CPX_TYPE_STRING, (offset + SL_OFFSET_DELTA) / 8, used_in_last_octet));
245     SLVAL(buf, offset, sl_pack_tag(SQ_TYPE_COMPLEX, 1, *toc_idx + 1));
246     *toc_idx += 1;
247     offset += 8;
248
249     SLVAL(buf, offset, sl_pack_tag(SQ_TYPE_DATA, octets + 1, used_in_last_octet));
250     offset += 8;
251
252     memset(buf + offset, 0, octets * 8);
253     strncpy(buf + offset, s, len);
254     offset += octets * 8;
255
256     return offset;
257 }
258
259 static int sl_pack_loop(DALLOC_CTX *query, char *buf, int offset, char *toc_buf, int *toc_idx)
260 {
261     const char *type;
262
263     for (int n = 0; n < talloc_array_length(query->dd_talloc_array); n++) {
264
265         type = talloc_get_name(query->dd_talloc_array[n]);
266
267         if (STRCMP(type, ==, "sl_array_t")) {
268             offset = sl_pack_array(query->dd_talloc_array[n], buf, offset, toc_buf, toc_idx);
269         } else if (STRCMP(type, ==, "sl_dict_t")) {
270             offset = sl_pack_dict(query->dd_talloc_array[n], buf, offset, toc_buf, toc_idx);
271         } else if (STRCMP(type, ==, "sl_filemeta_t")) {
272             offset = sl_pack_filemeta(query->dd_talloc_array[n], buf, offset, toc_buf, toc_idx);
273         } else if (STRCMP(type, ==, "uint64_t")) {
274             uint64_t i;
275             memcpy(&i, query->dd_talloc_array[n], sizeof(uint64_t));
276             offset = sl_pack_uint64(i, buf, offset);
277         } else if (STRCMP(type, ==, "char *")) {
278             offset = sl_pack_string(query->dd_talloc_array[n], buf, offset, toc_buf, toc_idx);
279         } else if (STRCMP(type, ==, "sl_bool_t")) {
280             sl_bool_t bl;
281             memcpy(&bl, query->dd_talloc_array[n], sizeof(sl_bool_t));
282             offset = sl_pack_bool(bl, buf, offset);
283         } else if (STRCMP(type, ==, "double")) {
284             double d;
285             memcpy(&d, query->dd_talloc_array[n], sizeof(double));
286             offset = sl_pack_float(d, buf, offset);
287         } else if (STRCMP(type, ==, "sl_nil_t")) {
288             offset = sl_pack_nil(buf, offset);
289         } else if (STRCMP(type, ==, "sl_time_t")) {
290             sl_time_t t;
291             memcpy(&t, query->dd_talloc_array[n], sizeof(sl_time_t));
292             offset = sl_pack_date(t, buf, offset);
293         } else if (STRCMP(type, ==, "sl_uuid_t")) {
294             offset = sl_pack_uuid(query->dd_talloc_array[n], buf, offset);
295         } else if (STRCMP(type, ==, "sl_cnids_t")) {
296             offset = sl_pack_CNID(query->dd_talloc_array[n], buf, offset, toc_buf, toc_idx);
297         }
298     }
299
300     return offset;
301 }
302
303 /**************************************************************************************************
304  * unmarshalling functions
305  **************************************************************************************************/
306
307 static uint64_t sl_unpack_uint64(const char *buf, int offset, uint encoding)
308 {
309     if (encoding == SL_ENC_LITTLE_ENDIAN)
310             return LVAL(buf, offset);
311         else
312             return RLVAL(buf, offset);
313 }
314
315 static int sl_unpack_ints(DALLOC_CTX *query, const char *buf, int offset, uint encoding)
316 {
317     int count, i;
318     uint64_t query_data64;
319
320     query_data64 = sl_unpack_uint64(buf, offset, encoding);
321     count = query_data64 >> 32;
322     offset += 8;
323
324     i = 0;
325     while (i++ < count) {
326         query_data64 = sl_unpack_uint64(buf, offset, encoding);
327         dalloc_add_copy(query, &query_data64, uint64_t);
328         offset += 8;
329     }
330
331     return count;
332 }
333
334 static int sl_unpack_date(DALLOC_CTX *query, const char *buf, int offset, uint encoding)
335 {
336     int count, i;
337     uint64_t query_data64;
338     sl_time_t t;
339
340     query_data64 = sl_unpack_uint64(buf, offset, encoding);
341     count = query_data64 >> 32;
342     offset += 8;
343
344     i = 0;
345     while (i++ < count) {
346         query_data64 = sl_unpack_uint64(buf, offset, encoding) >> 24;
347         t.tv_sec = query_data64 - SPOTLIGHT_TIME_DELTA;
348         t.tv_usec = 0;
349         dalloc_add_copy(query, &t, sl_time_t);
350         offset += 8;
351     }
352
353     return count;
354 }
355
356 static int sl_unpack_uuid(DALLOC_CTX *query, const char *buf, int offset, uint encoding)
357 {
358     int count, i;
359     uint64_t query_data64;
360     sl_uuid_t uuid;
361     query_data64 = sl_unpack_uint64(buf, offset, encoding);
362     count = query_data64 >> 32;
363     offset += 8;
364
365     i = 0;
366     while (i++ < count) {
367         memcpy(uuid.sl_uuid, buf + offset, 16);
368         dalloc_add_copy(query, &uuid, sl_uuid_t);
369         offset += 16;
370     }
371
372     return count;
373 }
374
375 static int sl_unpack_floats(DALLOC_CTX *query, const char *buf, int offset, uint encoding)
376 {
377     int count, i;
378     uint64_t query_data64;
379     double fval;
380     union {
381         double d;
382         uint32_t w[2];
383     } ieee_fp_union;
384
385     query_data64 = sl_unpack_uint64(buf, offset, encoding);
386     count = query_data64 >> 32;
387     offset += 8;
388
389     i = 0;
390     while (i++ < count) {
391         if (encoding == SL_ENC_LITTLE_ENDIAN) {
392 #ifdef WORDS_BIGENDIAN
393             ieee_fp_union.w[0] = IVAL(buf, offset + 4);
394             ieee_fp_union.w[1] = IVAL(buf, offset);
395 #else
396             ieee_fp_union.w[0] = IVAL(buf, offset);
397             ieee_fp_union.w[1] = IVAL(buf, offset + 4);
398 #endif
399         } else {
400 #ifdef WORDS_BIGENDIAN
401             ieee_fp_union.w[0] = RIVAL(buf, offset);
402             ieee_fp_union.w[1] = RIVAL(buf, offset + 4);
403 #else
404             ieee_fp_union.w[0] = RIVAL(buf, offset + 4);
405             ieee_fp_union.w[1] = RIVAL(buf, offset);
406 #endif
407         }
408         dalloc_add_copy(query, &ieee_fp_union.d, double);
409         offset += 8;
410     }
411
412     return count;
413 }
414
415 static int sl_unpack_CNID(DALLOC_CTX *query, const char *buf, int offset, int length, uint encoding)
416 {
417     EC_INIT;
418     int count;
419     uint64_t query_data64;
420     sl_cnids_t *cnids;
421
422     EC_NULL( cnids = talloc_zero(query, sl_cnids_t) );
423     EC_NULL( cnids->ca_cnids = talloc_zero(cnids, DALLOC_CTX) );
424
425     if (length <= 16)
426         /* that's permitted, it's an empty array */
427         goto EC_CLEANUP;
428     
429     query_data64 = sl_unpack_uint64(buf, offset, encoding);
430     count = query_data64 & 0xffff;
431
432     cnids->ca_unkn1 = (query_data64 & 0xffff0000) >> 16;
433     cnids->ca_context = query_data64 >> 32;
434
435     offset += 8;
436
437     while (count --) {
438         query_data64 = sl_unpack_uint64(buf, offset, encoding);
439         dalloc_add_copy(cnids->ca_cnids, &query_data64, uint64_t);
440         offset += 8;
441     }
442
443     dalloc_add(query, cnids, sl_cnids_t);
444
445 EC_CLEANUP:
446     EC_EXIT;
447 }
448
449 static const char *spotlight_get_qtype_string(uint64_t query_type)
450 {
451     switch (query_type) {
452     case SQ_TYPE_NULL:
453         return "null";
454     case SQ_TYPE_COMPLEX:
455         return "complex";
456     case SQ_TYPE_INT64:
457         return "int64";
458     case SQ_TYPE_BOOL:
459         return "bool";
460     case SQ_TYPE_FLOAT:
461         return "float";
462     case SQ_TYPE_DATA:
463         return "data";
464     case SQ_TYPE_CNIDS:
465         return "CNIDs";
466     default:
467         return "unknown";
468     }
469 }
470
471 static const char *spotlight_get_cpx_qtype_string(uint64_t cpx_query_type)
472 {
473     switch (cpx_query_type) {
474     case SQ_CPX_TYPE_ARRAY:
475         return "array";
476     case SQ_CPX_TYPE_STRING:
477         return "string";
478     case SQ_CPX_TYPE_UTF16_STRING:
479         return "utf-16 string";
480     case SQ_CPX_TYPE_DICT:
481         return "dictionary";
482     case SQ_CPX_TYPE_CNIDS:
483         return "CNIDs";
484     case SQ_CPX_TYPE_FILEMETA:
485         return "FileMeta";
486     default:
487         return "unknown";
488     }
489 }
490
491 static int sl_unpack_cpx(DALLOC_CTX *query,
492                          const char *buf,
493                          const int offset,
494                          uint cpx_query_type,
495                          uint cpx_query_count,
496                          const uint toc_offset,
497                          const uint encoding)
498 {
499     EC_INIT;
500
501     int roffset = offset;
502     uint64_t query_data64;
503     uint unicode_encoding;
504     uint8_t mark_exists;
505     char *p;
506     int qlen, used_in_last_block, slen;
507     sl_array_t *sl_array;
508     sl_dict_t *sl_dict;
509     sl_filemeta_t *sl_fm;
510
511     switch (cpx_query_type) {
512     case SQ_CPX_TYPE_ARRAY:
513         sl_array = talloc_zero(query, sl_array_t);
514         EC_NEG1_LOG( roffset = sl_unpack_loop(sl_array, buf, offset, cpx_query_count, toc_offset, encoding) );
515         dalloc_add(query, sl_array, sl_array_t);
516         break;
517
518     case SQ_CPX_TYPE_DICT:
519         sl_dict = talloc_zero(query, sl_dict_t);
520         EC_NEG1_LOG( roffset = sl_unpack_loop(sl_dict, buf, offset, cpx_query_count, toc_offset, encoding) );
521         dalloc_add(query, sl_dict, sl_dict_t);
522         break;
523
524     case SQ_CPX_TYPE_STRING:
525     case SQ_CPX_TYPE_UTF16_STRING:
526         query_data64 = sl_unpack_uint64(buf, offset, encoding);
527         qlen = (query_data64 & 0xffff) * 8;
528         used_in_last_block = query_data64 >> 32;
529         slen = qlen - 8 + used_in_last_block;
530
531         if (cpx_query_type == SQ_CPX_TYPE_STRING) {
532             p = dalloc_strndup(query, buf + offset + 8, slen);
533         } else {
534             unicode_encoding = spotlight_get_utf16_string_encoding(buf, offset + 8, slen, encoding);
535             mark_exists = (unicode_encoding & SL_ENC_UTF_16);
536             unicode_encoding &= ~SL_ENC_UTF_16;
537             EC_NEG1( convert_string_allocate(CH_UCS2, CH_UTF8, buf + offset + (mark_exists ? 18 : 16), slen, &p) );
538         }
539
540         dalloc_add(query, p, char *);
541         roffset += qlen;
542         break;
543
544     case SQ_CPX_TYPE_FILEMETA:
545         query_data64 = sl_unpack_uint64(buf, offset, encoding);
546         qlen = (query_data64 & 0xffff) * 8;
547         if (qlen <= 8) {
548             EC_FAIL_LOG("SQ_CPX_TYPE_FILEMETA: query_length <= 8: %d", qlen);
549         } else {
550             sl_fm = talloc_zero(query, sl_filemeta_t);
551             EC_NEG1_LOG( sl_unpack(sl_fm, buf + offset + 8) );
552             dalloc_add(query, sl_fm, sl_filemeta_t);
553         }
554         roffset += qlen;
555         break;
556
557     case SQ_CPX_TYPE_CNIDS:
558         query_data64 = sl_unpack_uint64(buf, offset, encoding);
559         qlen = (query_data64 & 0xffff) * 8;
560         EC_NEG1_LOG( sl_unpack_CNID(query, buf, offset + 8, qlen, encoding) );
561         roffset += qlen;
562         break;
563
564     default:
565         EC_FAIL;
566     }
567             
568 EC_CLEANUP:
569     if (ret != 0)
570         roffset = -1;
571     return roffset;
572 }
573
574 static int sl_unpack_loop(DALLOC_CTX *query,
575                           const char *buf,
576                           int offset,
577                           uint count,
578                           const uint toc_offset,
579                           const uint encoding)
580 {
581     EC_INIT;
582     int i, toc_index, query_length;
583     uint subcount;
584     uint64_t query_data64, query_type;
585     uint cpx_query_type, cpx_query_count;
586     sl_nil_t nil;
587     sl_bool_t b;
588
589     while (count > 0 && (offset < toc_offset)) {
590         query_data64 = sl_unpack_uint64(buf, offset, encoding);
591         query_length = (query_data64 & 0xffff) * 8;
592         query_type = (query_data64 & 0xffff0000) >> 16;
593         if (query_length == 0)
594             EC_FAIL;
595
596         switch (query_type) {
597         case SQ_TYPE_COMPLEX:
598             toc_index = (query_data64 >> 32) - 1;
599             query_data64 = sl_unpack_uint64(buf, toc_offset + toc_index * 8, encoding);
600             cpx_query_type = (query_data64 & 0xffff0000) >> 16;
601             cpx_query_count = query_data64 >> 32;
602
603             EC_NEG1_LOG( offset = sl_unpack_cpx(query, buf, offset + 8, cpx_query_type, cpx_query_count, toc_offset, encoding));
604             count--;
605             break;
606         case SQ_TYPE_NULL:
607             subcount = query_data64 >> 32;
608             if (subcount > 64)
609                 EC_FAIL;
610             nil = 0;
611             for (i = 0; i < subcount; i++)
612                 dalloc_add_copy(query, &nil, sl_nil_t);
613             offset += query_length;
614             count -= subcount;
615             break;
616         case SQ_TYPE_BOOL:
617             b = query_data64 >> 32;
618             dalloc_add_copy(query, &b, sl_bool_t);
619             offset += query_length;
620             count--;
621             break;
622         case SQ_TYPE_INT64:
623             EC_NEG1_LOG( subcount = sl_unpack_ints(query, buf, offset, encoding) );
624             offset += query_length;
625             count -= subcount;
626             break;
627         case SQ_TYPE_UUID:
628             EC_NEG1_LOG( subcount = sl_unpack_uuid(query, buf, offset, encoding) );
629             offset += query_length;
630             count -= subcount;
631             break;
632         case SQ_TYPE_FLOAT:
633             EC_NEG1_LOG( subcount = sl_unpack_floats(query, buf, offset, encoding) );
634             offset += query_length;
635             count -= subcount;
636             break;
637         case SQ_TYPE_DATE:
638             EC_NEG1_LOG( subcount = sl_unpack_date(query, buf, offset, encoding) );
639             offset += query_length;
640             count -= subcount;
641             break;
642         default:
643             EC_FAIL;
644         }
645     }
646
647 EC_CLEANUP:
648     if (ret != 0) {
649         offset = -1;
650     }
651     return offset;
652 }
653
654 /**************************************************************************************************
655  * Global functions for packing und unpacking
656  **************************************************************************************************/
657
658 #define MAX_SLQ_DAT 65000
659 #define MAX_SLQ_TOC 2048
660
661 int sl_pack(DALLOC_CTX *query, char *buf)
662 {
663     EC_INIT;
664     char toc_buf[MAX_SLQ_TOC];
665     int toc_index = 0;
666     int len = 0;
667
668     memcpy(buf, "432130dm", 8);
669     EC_NEG1_LOG( len = sl_pack_loop(query, buf + 16, 0, toc_buf + 8, &toc_index) );
670     SIVAL(buf, 8, len / 8 + 1 + toc_index + 1);
671     SIVAL(buf, 12, len / 8 + 1);
672
673     SLVAL(toc_buf, 0, sl_pack_tag(SQ_TYPE_TOC, toc_index + 1, 0));
674     memcpy(buf + 16 + len, toc_buf, (toc_index + 1 ) * 8);
675
676     len += 16 + (toc_index + 1 ) * 8;
677
678 EC_CLEANUP:
679     if (ret != 0)
680         len = -1;
681     return len;
682 }
683
684 int sl_unpack(DALLOC_CTX *query, const char *buf)
685 {
686     EC_INIT;
687     int encoding, i, toc_entries;
688     uint64_t toc_offset, tquerylen, toc_entry;
689
690     if (strncmp(buf, "md031234", 8) == 0)
691         encoding = SL_ENC_BIG_ENDIAN;
692     else
693         encoding = SL_ENC_LITTLE_ENDIAN;
694
695     buf += 8;
696
697     toc_offset = ((sl_unpack_uint64(buf, 0, encoding) >> 32) - 1 ) * 8;
698     if (toc_offset < 0 || (toc_offset > 65000)) {
699         EC_FAIL;
700     }
701
702     buf += 8;
703
704     toc_entries = (int)(sl_unpack_uint64(buf, toc_offset, encoding) & 0xffff);
705
706     EC_NEG1( sl_unpack_loop(query, buf, 0, 1, toc_offset + 8, encoding) );
707
708 EC_CLEANUP:
709     EC_EXIT;
710 }