]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/spotlight_marshalling.c
Fix UTF16 unmarshalling
[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     /* Assumed encoding in absence of a bom is little endian */
104     utf16_encoding = SL_ENC_LITTLE_ENDIAN;
105
106     if (query_length >= 2) {
107         uint16_t byte_order_mark;
108         memcpy(&byte_order_mark, buf + offset, sizeof(uint16_t));
109         if (byte_order_mark == 0xFFFE) {
110             utf16_encoding = SL_ENC_LITTLE_ENDIAN | SL_ENC_UTF_16;
111         }
112         else if (byte_order_mark == 0xFEFF) {
113             utf16_encoding = SL_ENC_BIG_ENDIAN | SL_ENC_UTF_16;
114         }
115     }
116
117     return utf16_encoding;
118 }
119
120 /**************************************************************************************************
121  * marshalling functions
122  **************************************************************************************************/
123
124 #define SL_OFFSET_DELTA 16
125
126 static uint64_t sl_pack_tag(uint16_t type, uint16_t size_or_count, uint32_t val)
127 {
128     uint64_t tag = ((uint64_t)val << 32) | ((uint64_t)type << 16) | size_or_count;
129     return tag;
130 }
131
132 static int sl_pack_float(double d, char *buf, int offset)
133 {
134     EC_INIT;
135
136     union {
137         double d;
138         uint64_t w;
139     } ieee_fp_union;
140
141     EC_ZERO( slvalc(buf, offset, MAX_SLQ_DAT, sl_pack_tag(SQ_TYPE_FLOAT, 2, 1)) );
142     EC_ZERO( slvalc(buf, offset + 8, MAX_SLQ_DAT, ieee_fp_union.w) );
143
144 EC_CLEANUP:
145     if (ret != 0)
146         return -1;
147     return offset + 2 * sizeof(uint64_t);
148 }
149
150 static int sl_pack_uint64(uint64_t u, char *buf, int offset)
151 {
152     EC_INIT;
153
154     EC_ZERO( slvalc(buf, offset, MAX_SLQ_DAT, sl_pack_tag(SQ_TYPE_INT64, 2, 1)) );
155     EC_ZERO( slvalc(buf, offset + 8, MAX_SLQ_DAT, u) );
156
157 EC_CLEANUP:
158     if (ret != 0)
159         return -1;
160     return offset + 2 * sizeof(uint64_t);
161 }
162
163 static int sl_pack_bool(sl_bool_t bl, char *buf, int offset)
164 {
165     EC_INIT;
166
167     EC_ZERO( slvalc(buf, offset, MAX_SLQ_DAT, sl_pack_tag(SQ_TYPE_BOOL, 1, bl ? 1 : 0)) );
168
169 EC_CLEANUP:
170     if (ret != 0)
171         return -1;
172     return offset + sizeof(uint64_t);
173 }
174
175 static int sl_pack_nil(char *buf, int offset)
176 {
177     EC_INIT;
178
179     EC_ZERO( slvalc(buf, offset, MAX_SLQ_DAT, sl_pack_tag(SQ_TYPE_NULL, 1, 1)) );
180
181 EC_CLEANUP:
182     if (ret != 0)
183         return -1;
184     return offset + sizeof(uint64_t);
185 }
186
187 static int sl_pack_date(sl_time_t t, char *buf, int offset)
188 {
189     EC_INIT;
190     uint64_t data = 0;
191
192     data = (t.tv_sec + SPOTLIGHT_TIME_DELTA) << 24;
193
194     EC_ZERO( slvalc(buf, offset, MAX_SLQ_DAT, sl_pack_tag(SQ_TYPE_DATE, 2, 1)) );
195     EC_ZERO( slvalc(buf, offset + 8, MAX_SLQ_DAT, data) );
196
197 EC_CLEANUP:
198     if (ret != 0)
199         return -1;
200     return offset + 2 * sizeof(uint64_t);
201 }
202
203 static int sl_pack_uuid(sl_uuid_t *uuid, char *buf, int offset)
204 {
205     EC_INIT;
206
207     EC_ZERO( slvalc(buf, offset, MAX_SLQ_DAT, sl_pack_tag(SQ_TYPE_UUID, 3, 1)) );
208     if (offset + 8 + 16 >= MAX_SLQ_DAT)
209         EC_FAIL;
210     memcpy(buf + offset + 8, uuid, 16);
211
212 EC_CLEANUP:
213     if (ret != 0)
214         return -1;
215     return offset + sizeof(uint64_t) + 16;
216 }
217
218 static int sl_pack_CNID(sl_cnids_t *cnids, char *buf, int offset, char *toc_buf, int *toc_idx)
219 {
220     EC_INIT;
221     int off = 0, len;
222     int cnid_count = talloc_array_length(cnids->ca_cnids->dd_talloc_array);
223     uint64_t id;
224
225     EC_ZERO( slvalc(toc_buf, *toc_idx * 8, MAX_SLQ_TOC, sl_pack_tag(SQ_CPX_TYPE_CNIDS, (offset + SL_OFFSET_DELTA) / 8, 0)) );
226     EC_ZERO( slvalc(buf, offset, MAX_SLQ_DAT, sl_pack_tag(SQ_TYPE_COMPLEX, 1, *toc_idx + 1)) );
227     *toc_idx += 1;
228     offset += 8;
229
230     len = cnid_count + 1;
231     if (cnid_count > 0)
232         len ++;
233
234     EC_ZERO( slvalc(buf, offset, MAX_SLQ_DAT, sl_pack_tag(SQ_TYPE_CNIDS, len, 8 /* unknown meaning, but always 8 */)) );
235     offset += 8;
236
237     if (cnid_count > 0) {
238         EC_ZERO( slvalc(buf, offset, MAX_SLQ_DAT, sl_pack_tag(0x0add, cnid_count, cnids->ca_context)) );
239         offset += 8;
240
241         for (int i = 0; i < cnid_count; i++) {
242             memcpy(&id, cnids->ca_cnids->dd_talloc_array[i], sizeof(uint64_t));
243             EC_ZERO( slvalc(buf, offset, MAX_SLQ_DAT, id) );
244             offset += 8;
245         }
246     }
247     
248 EC_CLEANUP:
249     if (ret != 0)
250         return -1;
251     return offset;
252 }
253
254 static int sl_pack_array(sl_array_t *array, char *buf, int offset, char *toc_buf, int *toc_idx)
255 {
256     EC_INIT;
257     int count = talloc_array_length(array->dd_talloc_array);
258     int octets = (offset + SL_OFFSET_DELTA) / 8;
259
260     EC_ZERO( slvalc(toc_buf, *toc_idx * 8, MAX_SLQ_TOC, sl_pack_tag(SQ_CPX_TYPE_ARRAY, octets, count)) );
261     EC_ZERO( slvalc(buf, offset, MAX_SLQ_DAT, sl_pack_tag(SQ_TYPE_COMPLEX, 1, *toc_idx + 1)) );
262     *toc_idx += 1;
263     offset += 8;
264
265     EC_NEG1( offset = sl_pack_loop(array, buf, offset, toc_buf, toc_idx) );
266
267 EC_CLEANUP:
268     if (ret != 0)
269         return -1;
270     return offset;
271 }
272
273 static int sl_pack_dict(sl_array_t *dict, char *buf, int offset, char *toc_buf, int *toc_idx)
274 {
275     EC_INIT;
276
277     EC_ZERO( slvalc(toc_buf,
278                     *toc_idx * 8,
279                     MAX_SLQ_TOC,
280                     sl_pack_tag(SQ_CPX_TYPE_DICT,
281                                 (offset + SL_OFFSET_DELTA) / 8,
282                                 talloc_array_length(dict->dd_talloc_array))) );
283     EC_ZERO( slvalc(buf, offset, MAX_SLQ_DAT, sl_pack_tag(SQ_TYPE_COMPLEX, 1, *toc_idx + 1)) );
284     *toc_idx += 1;
285     offset += 8;
286
287     EC_NEG1( offset = sl_pack_loop(dict, buf, offset, toc_buf, toc_idx) );
288
289 EC_CLEANUP:
290     if (ret != 0)
291         return -1;
292     return offset;
293 }
294
295 static int sl_pack_filemeta(sl_filemeta_t *fm, char *buf, int offset, char *toc_buf, int *toc_idx)
296 {
297     EC_INIT;
298     int fmlen;                  /* lenght of filemeta */
299     int saveoff = offset;
300
301     EC_ZERO( slvalc(buf, offset, MAX_SLQ_DAT, sl_pack_tag(SQ_TYPE_COMPLEX, 1, *toc_idx + 1)) );
302     offset += 16;
303
304     EC_NEG1( fmlen = sl_pack(fm, buf + offset) );
305
306     /* Check for empty filemeta array, if it's only 40 bytes, it's only the header but no content */
307     LOG(log_debug, logtype_sl, "fmlen: %d", fmlen);
308     if (fmlen > 40)
309         offset += fmlen;
310     else
311         fmlen = 0;
312
313     EC_ZERO( slvalc(buf, saveoff + 8, MAX_SLQ_DAT, sl_pack_tag(SQ_TYPE_DATA, (fmlen / 8) + 1, 8 /* unknown meaning, but always 8 */)) );
314
315     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)) );
316     *toc_idx += 1;
317
318 EC_CLEANUP:
319     if (ret != 0)
320         return -1;
321     return offset;
322 }
323
324 static int sl_pack_string(char *s, char *buf, int offset, char *toc_buf, int *toc_idx)
325 {
326     EC_INIT;
327     int len, octets, used_in_last_octet;
328
329     len = strlen(s);
330     octets = (len / 8) + (len & 7 ? 1 : 0);
331     used_in_last_octet = 8 - (octets * 8 - len);
332
333     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)) );
334     EC_ZERO( slvalc(buf, offset, MAX_SLQ_DAT, sl_pack_tag(SQ_TYPE_COMPLEX, 1, *toc_idx + 1)) );
335     *toc_idx += 1;
336     offset += 8;
337
338     EC_ZERO( slvalc(buf, offset, MAX_SLQ_DAT, sl_pack_tag(SQ_TYPE_DATA, octets + 1, used_in_last_octet)) );
339     offset += 8;
340
341     if (offset + octets * 8 > MAX_SLQ_DAT)
342         EC_FAIL;
343     memset(buf + offset, 0, octets * 8);
344     strncpy(buf + offset, s, len);
345     offset += octets * 8;
346
347 EC_CLEANUP:
348     if (ret != 0)
349         return -1;
350     return offset;
351 }
352
353 static int sl_pack_loop(DALLOC_CTX *query, char *buf, int offset, char *toc_buf, int *toc_idx)
354 {
355     EC_INIT;
356     const char *type;
357
358     for (int n = 0; n < talloc_array_length(query->dd_talloc_array); n++) {
359
360         type = talloc_get_name(query->dd_talloc_array[n]);
361
362         if (STRCMP(type, ==, "sl_array_t")) {
363             EC_NEG1( offset = sl_pack_array(query->dd_talloc_array[n], buf, offset, toc_buf, toc_idx) );
364         } else if (STRCMP(type, ==, "sl_dict_t")) {
365             EC_NEG1( offset = sl_pack_dict(query->dd_talloc_array[n], buf, offset, toc_buf, toc_idx) );
366         } else if (STRCMP(type, ==, "sl_filemeta_t")) {
367             EC_NEG1( offset = sl_pack_filemeta(query->dd_talloc_array[n], buf, offset, toc_buf, toc_idx) );
368         } else if (STRCMP(type, ==, "uint64_t")) {
369             uint64_t i;
370             memcpy(&i, query->dd_talloc_array[n], sizeof(uint64_t));
371             EC_NEG1( offset = sl_pack_uint64(i, buf, offset) );
372         } else if (STRCMP(type, ==, "char *")) {
373             EC_NEG1( offset = sl_pack_string(query->dd_talloc_array[n], buf, offset, toc_buf, toc_idx) );
374         } else if (STRCMP(type, ==, "sl_bool_t")) {
375             sl_bool_t bl;
376             memcpy(&bl, query->dd_talloc_array[n], sizeof(sl_bool_t));
377             EC_NEG1( offset = sl_pack_bool(bl, buf, offset) );
378         } else if (STRCMP(type, ==, "double")) {
379             double d;
380             memcpy(&d, query->dd_talloc_array[n], sizeof(double));
381             EC_NEG1( offset = sl_pack_float(d, buf, offset) );
382         } else if (STRCMP(type, ==, "sl_nil_t")) {
383             EC_NEG1( offset = sl_pack_nil(buf, offset) );
384         } else if (STRCMP(type, ==, "sl_time_t")) {
385             sl_time_t t;
386             memcpy(&t, query->dd_talloc_array[n], sizeof(sl_time_t));
387             EC_NEG1( offset = sl_pack_date(t, buf, offset) );
388         } else if (STRCMP(type, ==, "sl_uuid_t")) {
389             EC_NEG1( offset = sl_pack_uuid(query->dd_talloc_array[n], buf, offset) );
390         } else if (STRCMP(type, ==, "sl_cnids_t")) {
391             EC_NEG1( offset = sl_pack_CNID(query->dd_talloc_array[n], buf, offset, toc_buf, toc_idx) );
392         }
393     }
394
395 EC_CLEANUP:
396     if (ret != 0)
397         return -1;
398     return offset;
399 }
400
401 /**************************************************************************************************
402  * unmarshalling functions
403  **************************************************************************************************/
404
405 static uint64_t sl_unpack_uint64(const char *buf, int offset, uint encoding)
406 {
407     if (encoding == SL_ENC_LITTLE_ENDIAN)
408             return LVAL(buf, offset);
409         else
410             return RLVAL(buf, offset);
411 }
412
413 static int sl_unpack_ints(DALLOC_CTX *query, const char *buf, int offset, uint encoding)
414 {
415     int count, i;
416     uint64_t query_data64;
417
418     query_data64 = sl_unpack_uint64(buf, offset, encoding);
419     count = query_data64 >> 32;
420     offset += 8;
421
422     i = 0;
423     while (i++ < count) {
424         query_data64 = sl_unpack_uint64(buf, offset, encoding);
425         dalloc_add_copy(query, &query_data64, uint64_t);
426         offset += 8;
427     }
428
429     return count;
430 }
431
432 static int sl_unpack_date(DALLOC_CTX *query, const char *buf, int offset, uint encoding)
433 {
434     int count, i;
435     uint64_t query_data64;
436     sl_time_t t;
437
438     query_data64 = sl_unpack_uint64(buf, offset, encoding);
439     count = query_data64 >> 32;
440     offset += 8;
441
442     i = 0;
443     while (i++ < count) {
444         query_data64 = sl_unpack_uint64(buf, offset, encoding) >> 24;
445         t.tv_sec = query_data64 - SPOTLIGHT_TIME_DELTA;
446         t.tv_usec = 0;
447         dalloc_add_copy(query, &t, sl_time_t);
448         offset += 8;
449     }
450
451     return count;
452 }
453
454 static int sl_unpack_uuid(DALLOC_CTX *query, const char *buf, int offset, uint encoding)
455 {
456     int count, i;
457     uint64_t query_data64;
458     sl_uuid_t uuid;
459     query_data64 = sl_unpack_uint64(buf, offset, encoding);
460     count = query_data64 >> 32;
461     offset += 8;
462
463     i = 0;
464     while (i++ < count) {
465         memcpy(uuid.sl_uuid, buf + offset, 16);
466         dalloc_add_copy(query, &uuid, sl_uuid_t);
467         offset += 16;
468     }
469
470     return count;
471 }
472
473 static int sl_unpack_floats(DALLOC_CTX *query, const char *buf, int offset, uint encoding)
474 {
475     int count, i;
476     uint64_t query_data64;
477     double fval;
478     union {
479         double d;
480         uint32_t w[2];
481     } ieee_fp_union;
482
483     query_data64 = sl_unpack_uint64(buf, offset, encoding);
484     count = query_data64 >> 32;
485     offset += 8;
486
487     i = 0;
488     while (i++ < count) {
489         if (encoding == SL_ENC_LITTLE_ENDIAN) {
490 #ifdef WORDS_BIGENDIAN
491             ieee_fp_union.w[0] = IVAL(buf, offset + 4);
492             ieee_fp_union.w[1] = IVAL(buf, offset);
493 #else
494             ieee_fp_union.w[0] = IVAL(buf, offset);
495             ieee_fp_union.w[1] = IVAL(buf, offset + 4);
496 #endif
497         } else {
498 #ifdef WORDS_BIGENDIAN
499             ieee_fp_union.w[0] = RIVAL(buf, offset);
500             ieee_fp_union.w[1] = RIVAL(buf, offset + 4);
501 #else
502             ieee_fp_union.w[0] = RIVAL(buf, offset + 4);
503             ieee_fp_union.w[1] = RIVAL(buf, offset);
504 #endif
505         }
506         dalloc_add_copy(query, &ieee_fp_union.d, double);
507         offset += 8;
508     }
509
510     return count;
511 }
512
513 static int sl_unpack_CNID(DALLOC_CTX *query, const char *buf, int offset, int length, uint encoding)
514 {
515     EC_INIT;
516     int count;
517     uint64_t query_data64;
518     sl_cnids_t *cnids;
519
520     EC_NULL( cnids = talloc_zero(query, sl_cnids_t) );
521     EC_NULL( cnids->ca_cnids = talloc_zero(cnids, DALLOC_CTX) );
522
523     if (length <= 16)
524         /* that's permitted, it's an empty array */
525         goto EC_CLEANUP;
526     
527     query_data64 = sl_unpack_uint64(buf, offset, encoding);
528     count = query_data64 & 0xffff;
529
530     cnids->ca_unkn1 = (query_data64 & 0xffff0000) >> 16;
531     cnids->ca_context = query_data64 >> 32;
532
533     offset += 8;
534
535     while (count --) {
536         query_data64 = sl_unpack_uint64(buf, offset, encoding);
537         dalloc_add_copy(cnids->ca_cnids, &query_data64, uint64_t);
538         offset += 8;
539     }
540
541     dalloc_add(query, cnids, sl_cnids_t);
542
543 EC_CLEANUP:
544     EC_EXIT;
545 }
546
547 static const char *spotlight_get_qtype_string(uint64_t query_type)
548 {
549     switch (query_type) {
550     case SQ_TYPE_NULL:
551         return "null";
552     case SQ_TYPE_COMPLEX:
553         return "complex";
554     case SQ_TYPE_INT64:
555         return "int64";
556     case SQ_TYPE_BOOL:
557         return "bool";
558     case SQ_TYPE_FLOAT:
559         return "float";
560     case SQ_TYPE_DATA:
561         return "data";
562     case SQ_TYPE_CNIDS:
563         return "CNIDs";
564     default:
565         return "unknown";
566     }
567 }
568
569 static const char *spotlight_get_cpx_qtype_string(uint64_t cpx_query_type)
570 {
571     switch (cpx_query_type) {
572     case SQ_CPX_TYPE_ARRAY:
573         return "array";
574     case SQ_CPX_TYPE_STRING:
575         return "string";
576     case SQ_CPX_TYPE_UTF16_STRING:
577         return "utf-16 string";
578     case SQ_CPX_TYPE_DICT:
579         return "dictionary";
580     case SQ_CPX_TYPE_CNIDS:
581         return "CNIDs";
582     case SQ_CPX_TYPE_FILEMETA:
583         return "FileMeta";
584     default:
585         return "unknown";
586     }
587 }
588
589 static int sl_unpack_cpx(DALLOC_CTX *query,
590                          const char *buf,
591                          const int offset,
592                          uint cpx_query_type,
593                          uint cpx_query_count,
594                          const uint toc_offset,
595                          const uint encoding)
596 {
597     EC_INIT;
598
599     int roffset = offset;
600     uint64_t query_data64;
601     uint unicode_encoding;
602     uint8_t mark_exists;
603     char *p, *tmp;
604     int qlen, used_in_last_block, slen;
605     sl_array_t *sl_array;
606     sl_dict_t *sl_dict;
607     sl_filemeta_t *sl_fm;
608
609     switch (cpx_query_type) {
610     case SQ_CPX_TYPE_ARRAY:
611         sl_array = talloc_zero(query, sl_array_t);
612         EC_NEG1_LOG( roffset = sl_unpack_loop(sl_array, buf, offset, cpx_query_count, toc_offset, encoding) );
613         dalloc_add(query, sl_array, sl_array_t);
614         break;
615
616     case SQ_CPX_TYPE_DICT:
617         sl_dict = talloc_zero(query, sl_dict_t);
618         EC_NEG1_LOG( roffset = sl_unpack_loop(sl_dict, buf, offset, cpx_query_count, toc_offset, encoding) );
619         dalloc_add(query, sl_dict, sl_dict_t);
620         break;
621
622     case SQ_CPX_TYPE_STRING:
623     case SQ_CPX_TYPE_UTF16_STRING:
624         query_data64 = sl_unpack_uint64(buf, offset, encoding);
625         qlen = (query_data64 & 0xffff) * 8;
626         used_in_last_block = query_data64 >> 32;
627         slen = qlen - 8 + used_in_last_block;
628
629         if (cpx_query_type == SQ_CPX_TYPE_STRING) {
630             p = dalloc_strndup(query, buf + offset + 8, slen);
631         } else {
632             unicode_encoding = spotlight_get_utf16_string_encoding(buf, offset + 8, slen, encoding);
633             mark_exists = (unicode_encoding & SL_ENC_UTF_16);
634             if (unicode_encoding & SL_ENC_BIG_ENDIAN)
635                 EC_FAIL_LOG("Unsupported big endian UTF16 string");
636             EC_NEG1( convert_string_allocate(CH_UCS2, CH_UTF8, buf + offset + (mark_exists ? 10 : 8), slen, &tmp) );
637             p = dalloc_strndup(query, tmp, strlen(tmp));
638             free(tmp);
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 }