]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/spotlight_marshalling.c
1df560662943d0768cd0fd64b95659498dff63f5
[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     LOG(log_maxdebug, logtype_sl, "sl_pack_array: count: %d, offset:%d, octets: %d", count, offset, octets);
197
198     SLVAL(toc_buf, *toc_idx * 8, sl_pack_tag(SQ_CPX_TYPE_ARRAY, octets, count));
199     SLVAL(buf, offset, sl_pack_tag(SQ_TYPE_COMPLEX, 1, *toc_idx + 1));
200     *toc_idx += 1;
201     offset += 8;
202
203     offset = sl_pack_loop(array, buf, offset, toc_buf, toc_idx);
204
205     return offset;
206 }
207
208 static int sl_pack_dict(sl_array_t *dict, char *buf, int offset, char *toc_buf, int *toc_idx)
209 {
210     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)));
211     SLVAL(buf, offset, sl_pack_tag(SQ_TYPE_COMPLEX, 1, *toc_idx + 1));
212     *toc_idx += 1;
213     offset += 8;
214
215     offset = sl_pack_loop(dict, buf, offset, toc_buf, toc_idx);
216
217     return offset;
218 }
219
220 static int sl_pack_filemeta(sl_filemeta_t *fm, char *buf, int offset, char *toc_buf, int *toc_idx)
221 {
222     int fmlen;                  /* lenght of filemeta */
223     int saveoff = offset + 8;
224
225     SLVAL(buf, offset, sl_pack_tag(SQ_TYPE_COMPLEX, 1, *toc_idx + 1));
226     offset += 16;
227
228     fmlen = sl_pack(fm, buf + offset);
229     offset += fmlen;
230
231     SLVAL(buf, saveoff, sl_pack_tag(SQ_TYPE_DATA, (fmlen / 8) + 1, 8 /* unknown meaning, but always 8 */));
232
233     SLVAL(toc_buf, *toc_idx * 8, sl_pack_tag(SQ_CPX_TYPE_FILEMETA, (offset + SL_OFFSET_DELTA) / 8, fmlen / 8));
234     *toc_idx += 1;
235
236     return offset;
237 }
238
239 static int sl_pack_string(char *s, char *buf, int offset, char *toc_buf, int *toc_idx)
240 {
241     int len, octets, used_in_last_octet;
242     len = strlen(s);
243     octets = (len / 8) + (len & 7 ? 1 : 0);
244     used_in_last_octet = 8 - (octets * 8 - len);
245
246     LOG(log_maxdebug, logtype_sl, "sl_pack_string(\"%s\"): len: %d, octets: %d, used_in_last_octet: %d",
247         s, len, octets, used_in_last_octet);
248
249     SLVAL(toc_buf, *toc_idx * 8, sl_pack_tag(SQ_CPX_TYPE_STRING, (offset + SL_OFFSET_DELTA) / 8, used_in_last_octet));
250     SLVAL(buf, offset, sl_pack_tag(SQ_TYPE_COMPLEX, 1, *toc_idx + 1));
251     *toc_idx += 1;
252     offset += 8;
253
254     SLVAL(buf, offset, sl_pack_tag(SQ_TYPE_DATA, octets + 1, used_in_last_octet));
255     offset += 8;
256
257     memset(buf + offset, 0, octets * 8);
258     strncpy(buf + offset, s, len);
259     offset += octets * 8;
260
261     return offset;
262 }
263
264 static int sl_pack_loop(DALLOC_CTX *query, char *buf, int offset, char *toc_buf, int *toc_idx)
265 {
266     const char *type;
267
268     for (int n = 0; n < talloc_array_length(query->dd_talloc_array); n++) {
269
270         type = talloc_get_name(query->dd_talloc_array[n]);
271
272         if (STRCMP(type, ==, "sl_array_t")) {
273             offset = sl_pack_array(query->dd_talloc_array[n], buf, offset, toc_buf, toc_idx);
274         } else if (STRCMP(type, ==, "sl_dict_t")) {
275             offset = sl_pack_dict(query->dd_talloc_array[n], buf, offset, toc_buf, toc_idx);
276         } else if (STRCMP(type, ==, "sl_filemeta_t")) {
277             offset = sl_pack_filemeta(query->dd_talloc_array[n], buf, offset, toc_buf, toc_idx);
278         } else if (STRCMP(type, ==, "uint64_t")) {
279             uint64_t i;
280             memcpy(&i, query->dd_talloc_array[n], sizeof(uint64_t));
281             offset = sl_pack_uint64(i, buf, offset);
282         } else if (STRCMP(type, ==, "char *")) {
283             offset = sl_pack_string(query->dd_talloc_array[n], buf, offset, toc_buf, toc_idx);
284         } else if (STRCMP(type, ==, "sl_bool_t")) {
285             sl_bool_t bl;
286             memcpy(&bl, query->dd_talloc_array[n], sizeof(sl_bool_t));
287             offset = sl_pack_bool(bl, buf, offset);
288         } else if (STRCMP(type, ==, "double")) {
289             double d;
290             memcpy(&d, query->dd_talloc_array[n], sizeof(double));
291             offset = sl_pack_float(d, buf, offset);
292         } else if (STRCMP(type, ==, "sl_nil_t")) {
293             offset = sl_pack_nil(buf, offset);
294         } else if (STRCMP(type, ==, "sl_time_t")) {
295             sl_time_t t;
296             memcpy(&t, query->dd_talloc_array[n], sizeof(sl_time_t));
297             offset = sl_pack_date(t, buf, offset);
298         } else if (STRCMP(type, ==, "sl_uuid_t")) {
299             offset = sl_pack_uuid(query->dd_talloc_array[n], buf, offset);
300         } else if (STRCMP(type, ==, "sl_cnids_t")) {
301             offset = sl_pack_CNID(query->dd_talloc_array[n], buf, offset, toc_buf, toc_idx);
302         }
303     }
304
305     return offset;
306 }
307
308 /**************************************************************************************************
309  * unmarshalling functions
310  **************************************************************************************************/
311
312 static uint64_t sl_unpack_uint64(const char *buf, int offset, uint encoding)
313 {
314     if (encoding == SL_ENC_LITTLE_ENDIAN)
315             return LVAL(buf, offset);
316         else
317             return RLVAL(buf, offset);
318 }
319
320 static int sl_unpack_ints(DALLOC_CTX *query, const char *buf, int offset, uint encoding)
321 {
322     int count, i;
323     uint64_t query_data64;
324
325     query_data64 = sl_unpack_uint64(buf, offset, encoding);
326     count = query_data64 >> 32;
327     offset += 8;
328
329     i = 0;
330     while (i++ < count) {
331         query_data64 = sl_unpack_uint64(buf, offset, encoding);
332         dalloc_add_copy(query, &query_data64, uint64_t);
333         offset += 8;
334     }
335
336     return count;
337 }
338
339 static int sl_unpack_date(DALLOC_CTX *query, const char *buf, int offset, uint encoding)
340 {
341     int count, i;
342     uint64_t query_data64;
343     sl_time_t t;
344
345     query_data64 = sl_unpack_uint64(buf, offset, encoding);
346     count = query_data64 >> 32;
347     offset += 8;
348
349     i = 0;
350     while (i++ < count) {
351         query_data64 = sl_unpack_uint64(buf, offset, encoding) >> 24;
352         t.tv_sec = query_data64 - SPOTLIGHT_TIME_DELTA;
353         t.tv_usec = 0;
354         dalloc_add_copy(query, &t, sl_time_t);
355         offset += 8;
356     }
357
358     return count;
359 }
360
361 static int sl_unpack_uuid(DALLOC_CTX *query, const char *buf, int offset, uint encoding)
362 {
363     int count, i;
364     uint64_t query_data64;
365     sl_uuid_t uuid;
366     query_data64 = sl_unpack_uint64(buf, offset, encoding);
367     count = query_data64 >> 32;
368     offset += 8;
369
370     i = 0;
371     while (i++ < count) {
372         memcpy(uuid.sl_uuid, buf + offset, 16);
373         dalloc_add_copy(query, &uuid, sl_uuid_t);
374         offset += 16;
375     }
376
377     return count;
378 }
379
380 static int sl_unpack_floats(DALLOC_CTX *query, const char *buf, int offset, uint encoding)
381 {
382     int count, i;
383     uint64_t query_data64;
384     double fval;
385     union {
386         double d;
387         uint32_t w[2];
388     } ieee_fp_union;
389
390     query_data64 = sl_unpack_uint64(buf, offset, encoding);
391     count = query_data64 >> 32;
392     offset += 8;
393
394     i = 0;
395     while (i++ < count) {
396         if (encoding == SL_ENC_LITTLE_ENDIAN) {
397 #ifdef WORDS_BIGENDIAN
398             ieee_fp_union.w[0] = IVAL(buf, offset + 4);
399             ieee_fp_union.w[1] = IVAL(buf, offset);
400 #else
401             ieee_fp_union.w[0] = IVAL(buf, offset);
402             ieee_fp_union.w[1] = IVAL(buf, offset + 4);
403 #endif
404         } else {
405 #ifdef WORDS_BIGENDIAN
406             ieee_fp_union.w[0] = RIVAL(buf, offset);
407             ieee_fp_union.w[1] = RIVAL(buf, offset + 4);
408 #else
409             ieee_fp_union.w[0] = RIVAL(buf, offset + 4);
410             ieee_fp_union.w[1] = RIVAL(buf, offset);
411 #endif
412         }
413         dalloc_add_copy(query, &ieee_fp_union.d, double);
414         offset += 8;
415     }
416
417     return count;
418 }
419
420 static int sl_unpack_CNID(DALLOC_CTX *query, const char *buf, int offset, int length, uint encoding)
421 {
422     EC_INIT;
423     int count;
424     uint64_t query_data64;
425     sl_cnids_t *cnids;
426
427     EC_NULL( cnids = talloc_zero(query, sl_cnids_t) );
428     EC_NULL( cnids->ca_cnids = talloc_zero(cnids, DALLOC_CTX) );
429
430     if (length <= 16)
431         /* that's permitted, it's an empty array */
432         goto EC_CLEANUP;
433     
434     query_data64 = sl_unpack_uint64(buf, offset, encoding);
435     count = query_data64 & 0xffff;
436
437     cnids->ca_unkn1 = (query_data64 & 0xffff0000) >> 16;
438     cnids->ca_context = query_data64 >> 32;
439
440     offset += 8;
441
442     while (count --) {
443         query_data64 = sl_unpack_uint64(buf, offset, encoding);
444         dalloc_add_copy(cnids->ca_cnids, &query_data64, uint64_t);
445         offset += 8;
446     }
447
448     dalloc_add(query, cnids, sl_cnids_t);
449
450 EC_CLEANUP:
451     EC_EXIT;
452 }
453
454 static const char *spotlight_get_qtype_string(uint64_t query_type)
455 {
456     switch (query_type) {
457     case SQ_TYPE_NULL:
458         return "null";
459     case SQ_TYPE_COMPLEX:
460         return "complex";
461     case SQ_TYPE_INT64:
462         return "int64";
463     case SQ_TYPE_BOOL:
464         return "bool";
465     case SQ_TYPE_FLOAT:
466         return "float";
467     case SQ_TYPE_DATA:
468         return "data";
469     case SQ_TYPE_CNIDS:
470         return "CNIDs";
471     default:
472         return "unknown";
473     }
474 }
475
476 static const char *spotlight_get_cpx_qtype_string(uint64_t cpx_query_type)
477 {
478     switch (cpx_query_type) {
479     case SQ_CPX_TYPE_ARRAY:
480         return "array";
481     case SQ_CPX_TYPE_STRING:
482         return "string";
483     case SQ_CPX_TYPE_UTF16_STRING:
484         return "utf-16 string";
485     case SQ_CPX_TYPE_DICT:
486         return "dictionary";
487     case SQ_CPX_TYPE_CNIDS:
488         return "CNIDs";
489     case SQ_CPX_TYPE_FILEMETA:
490         return "FileMeta";
491     default:
492         return "unknown";
493     }
494 }
495
496 static int sl_unpack_cpx(DALLOC_CTX *query,
497                          const char *buf,
498                          const int offset,
499                          uint cpx_query_type,
500                          uint cpx_query_count,
501                          const uint toc_offset,
502                          const uint encoding)
503 {
504     EC_INIT;
505
506     int roffset = offset;
507     uint64_t query_data64;
508     uint unicode_encoding;
509     uint8_t mark_exists;
510     char *p;
511     int qlen, used_in_last_block, slen;
512     sl_array_t *sl_array;
513     sl_dict_t *sl_dict;
514     sl_filemeta_t *sl_fm;
515
516     switch (cpx_query_type) {
517     case SQ_CPX_TYPE_ARRAY:
518         sl_array = talloc_zero(query, sl_array_t);
519         EC_NEG1_LOG( roffset = sl_unpack_loop(sl_array, buf, offset, cpx_query_count, toc_offset, encoding) );
520         dalloc_add(query, sl_array, sl_array_t);
521         break;
522
523     case SQ_CPX_TYPE_DICT:
524         sl_dict = talloc_zero(query, sl_dict_t);
525         EC_NEG1_LOG( roffset = sl_unpack_loop(sl_dict, buf, offset, cpx_query_count, toc_offset, encoding) );
526         dalloc_add(query, sl_dict, sl_dict_t);
527         break;
528
529     case SQ_CPX_TYPE_STRING:
530     case SQ_CPX_TYPE_UTF16_STRING:
531         query_data64 = sl_unpack_uint64(buf, offset, encoding);
532         qlen = (query_data64 & 0xffff) * 8;
533         used_in_last_block = query_data64 >> 32;
534         slen = qlen - 8 + used_in_last_block;
535
536         if (cpx_query_type == SQ_CPX_TYPE_STRING) {
537             p = dalloc_strndup(query, buf + offset + 8, slen);
538         } else {
539             unicode_encoding = spotlight_get_utf16_string_encoding(buf, offset + 8, slen, encoding);
540             mark_exists = (unicode_encoding & SL_ENC_UTF_16);
541             unicode_encoding &= ~SL_ENC_UTF_16;
542             EC_NEG1( convert_string_allocate(CH_UCS2, CH_UTF8, buf + offset + (mark_exists ? 18 : 16), slen, &p) );
543         }
544
545         dalloc_add(query, p, char *);
546         roffset += qlen;
547         break;
548
549     case SQ_CPX_TYPE_FILEMETA:
550         query_data64 = sl_unpack_uint64(buf, offset, encoding);
551         qlen = (query_data64 & 0xffff) * 8;
552         if (qlen <= 8) {
553             EC_FAIL_LOG("SQ_CPX_TYPE_FILEMETA: query_length <= 8: %d", qlen);
554         } else {
555             sl_fm = talloc_zero(query, sl_filemeta_t);
556             EC_NEG1_LOG( sl_unpack(sl_fm, buf + offset + 8) );
557             dalloc_add(query, sl_fm, sl_filemeta_t);
558         }
559         roffset += qlen;
560         break;
561
562     case SQ_CPX_TYPE_CNIDS:
563         query_data64 = sl_unpack_uint64(buf, offset, encoding);
564         qlen = (query_data64 & 0xffff) * 8;
565         EC_NEG1_LOG( sl_unpack_CNID(query, buf, offset + 8, qlen, encoding) );
566         roffset += qlen;
567         break;
568
569     default:
570         EC_FAIL;
571     }
572             
573 EC_CLEANUP:
574     if (ret != 0)
575         roffset = -1;
576     return roffset;
577 }
578
579 static int sl_unpack_loop(DALLOC_CTX *query,
580                           const char *buf,
581                           int offset,
582                           uint count,
583                           const uint toc_offset,
584                           const uint encoding)
585 {
586     EC_INIT;
587     int i, toc_index, query_length;
588     uint subcount;
589     uint64_t query_data64, query_type;
590     uint cpx_query_type, cpx_query_count;
591     sl_nil_t nil;
592     sl_bool_t b;
593
594     while (count > 0 && (offset < toc_offset)) {
595         query_data64 = sl_unpack_uint64(buf, offset, encoding);
596         query_length = (query_data64 & 0xffff) * 8;
597         query_type = (query_data64 & 0xffff0000) >> 16;
598         if (query_length == 0)
599             EC_FAIL;
600
601         switch (query_type) {
602         case SQ_TYPE_COMPLEX:
603             toc_index = (query_data64 >> 32) - 1;
604             query_data64 = sl_unpack_uint64(buf, toc_offset + toc_index * 8, encoding);
605             cpx_query_type = (query_data64 & 0xffff0000) >> 16;
606             cpx_query_count = query_data64 >> 32;
607
608             EC_NEG1_LOG( offset = sl_unpack_cpx(query, buf, offset + 8, cpx_query_type, cpx_query_count, toc_offset, encoding));
609             count--;
610             break;
611         case SQ_TYPE_NULL:
612             subcount = query_data64 >> 32;
613             if (subcount > 64)
614                 EC_FAIL;
615             nil = 0;
616             for (i = 0; i < subcount; i++)
617                 dalloc_add_copy(query, &nil, sl_nil_t);
618             offset += query_length;
619             count -= subcount;
620             break;
621         case SQ_TYPE_BOOL:
622             b = query_data64 >> 32;
623             dalloc_add_copy(query, &b, sl_bool_t);
624             offset += query_length;
625             count--;
626             break;
627         case SQ_TYPE_INT64:
628             EC_NEG1_LOG( subcount = sl_unpack_ints(query, buf, offset, encoding) );
629             offset += query_length;
630             count -= subcount;
631             break;
632         case SQ_TYPE_UUID:
633             EC_NEG1_LOG( subcount = sl_unpack_uuid(query, buf, offset, encoding) );
634             offset += query_length;
635             count -= subcount;
636             break;
637         case SQ_TYPE_FLOAT:
638             EC_NEG1_LOG( subcount = sl_unpack_floats(query, buf, offset, encoding) );
639             offset += query_length;
640             count -= subcount;
641             break;
642         case SQ_TYPE_DATE:
643             EC_NEG1_LOG( subcount = sl_unpack_date(query, buf, offset, encoding) );
644             offset += query_length;
645             count -= subcount;
646             break;
647         default:
648             EC_FAIL;
649         }
650     }
651
652 EC_CLEANUP:
653     if (ret != 0) {
654         offset = -1;
655     }
656     return offset;
657 }
658
659 /**************************************************************************************************
660  * Global functions for packing und unpacking
661  **************************************************************************************************/
662
663 #define MAX_SLQ_DAT 65000
664 #define MAX_SLQ_TOC 2048
665
666 int sl_pack(DALLOC_CTX *query, char *buf)
667 {
668     EC_INIT;
669     char toc_buf[MAX_SLQ_TOC];
670     int toc_index = 0;
671     int len = 0;
672
673     memcpy(buf, "432130dm", 8);
674     EC_NEG1_LOG( len = sl_pack_loop(query, buf + 16, 0, toc_buf + 8, &toc_index) );
675     SIVAL(buf, 8, len / 8 + 1 + toc_index + 1);
676     SIVAL(buf, 12, len / 8 + 1);
677
678     SLVAL(toc_buf, 0, sl_pack_tag(SQ_TYPE_TOC, toc_index + 1, 0));
679     memcpy(buf + 16 + len, toc_buf, (toc_index + 1 ) * 8);
680
681     len += 16 + (toc_index + 1 ) * 8;
682
683 EC_CLEANUP:
684     if (ret != 0)
685         len = -1;
686     return len;
687 }
688
689 int sl_unpack(DALLOC_CTX *query, const char *buf)
690 {
691     EC_INIT;
692     int encoding, i, toc_entries;
693     uint64_t toc_offset, tquerylen, toc_entry;
694
695     if (strncmp(buf, "md031234", 8) == 0)
696         encoding = SL_ENC_BIG_ENDIAN;
697     else
698         encoding = SL_ENC_LITTLE_ENDIAN;
699
700     buf += 8;
701
702     toc_offset = ((sl_unpack_uint64(buf, 0, encoding) >> 32) - 1 ) * 8;
703     if (toc_offset < 0 || (toc_offset > 65000)) {
704         EC_FAIL;
705     }
706
707     buf += 8;
708
709     toc_entries = (int)(sl_unpack_uint64(buf, toc_offset, encoding) & 0xffff);
710
711     EC_NEG1( sl_unpack_loop(query, buf, 0, 1, toc_offset + 8, encoding) );
712
713 EC_CLEANUP:
714     EC_EXIT;
715 }