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