]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/spotlight.c
Marshalling basically works
[netatalk.git] / etc / afpd / spotlight.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 /* FPSpotlightRPC subcommand codes */
43 #define SPOTLIGHT_CMD_VOLPATH 1
44 #define SPOTLIGHT_CMD_FLAGS   2
45 #define SPOTLIGHT_CMD_RPC     3
46
47 /* Spotlight epoch is UNIX epoch minus SPOTLIGHT_TIME_DELTA */
48 #define SPOTLIGHT_TIME_DELTA INT64_C(280878921600U)
49
50 #define SQ_TYPE_NULL    0x0000
51 #define SQ_TYPE_COMPLEX 0x0200
52 #define SQ_TYPE_INT64   0x8400
53 #define SQ_TYPE_BOOL    0x0100
54 #define SQ_TYPE_FLOAT   0x8500
55 #define SQ_TYPE_DATA    0x0700
56 #define SQ_TYPE_CNIDS   0x8700
57 #define SQ_TYPE_UUID    0x0e00
58 #define SQ_TYPE_DATE    0x8600
59 #define SQ_TYPE_TOC     0x8800
60
61 #define SQ_CPX_TYPE_ARRAY           0x0a00
62 #define SQ_CPX_TYPE_STRING          0x0c00
63 #define SQ_CPX_TYPE_UTF16_STRING    0x1c00
64 #define SQ_CPX_TYPE_DICT            0x0d00
65 #define SQ_CPX_TYPE_CNIDS           0x1a00
66 #define SQ_CPX_TYPE_FILEMETA        0x1b00
67
68 #define SUBQ_SAFETY_LIM 20
69
70 /* Can be ored and used as flags */
71 #define SL_ENC_LITTLE_ENDIAN 1
72 #define SL_ENC_BIG_ENDIAN    2
73 #define SL_ENC_UTF_16        4
74
75 /* Forward declarations */
76 static int dissect_spotlight(DALLOC_CTX *query, const char *buf);
77 static int sl_pack_loop(DALLOC_CTX *query, char *buf, int offset, char *toc_buf, int *toc_idx);
78
79 /* Helper functions and stuff */
80 static const char *neststrings[] = {
81     "",
82     "    ",
83     "        ",
84     "            ",
85     "                ",
86     "                    ",
87     "                        "
88 };
89
90 static int dd_dump(DALLOC_CTX *dd, int nestinglevel)
91 {
92     const char *type;
93
94     LOG(log_debug, logtype_sl, "%s1: %s(#%d): {", neststrings[nestinglevel], talloc_get_name(dd), talloc_array_length(dd->dd_talloc_array));
95
96     for (int n = 0; n < talloc_array_length(dd->dd_talloc_array); n++) {
97
98         type = talloc_get_name(dd->dd_talloc_array[n]);
99
100         if (STRCMP(type, ==, "DALLOC_CTX")
101                    || STRCMP(type, ==, "sl_array_t")
102                    || STRCMP(type, ==, "sl_dict_t")) {
103             dd_dump(dd->dd_talloc_array[n], nestinglevel + 1);
104         } else if (STRCMP(type, ==, "uint64_t")) {
105             uint64_t i;
106             memcpy(&i, dd->dd_talloc_array[n], sizeof(uint64_t));
107             LOG(log_debug, logtype_sl, "%s%u:\t0x%04x", neststrings[nestinglevel + 1], n + 1, i);
108         } else if (STRCMP(type, ==, "int64_t")) {
109             int64_t i;
110             memcpy(&i, dd->dd_talloc_array[n], sizeof(int64_t));
111             LOG(log_debug, logtype_sl, "%s%d:\t%" PRId64, neststrings[nestinglevel + 1], n + 1, i);
112         } else if (STRCMP(type, ==, "uint32_t")) {
113             uint32_t i;
114             memcpy(&i, dd->dd_talloc_array[n], sizeof(uint32_t));
115             LOG(log_debug, logtype_sl, "%s%d:\t%" PRIu32, neststrings[nestinglevel + 1], n + 1, i);
116         } else if (STRCMP(type, ==, "char *")) {
117             char *s;
118             memcpy(&s, dd->dd_talloc_array[n], sizeof(char *));
119             LOG(log_debug, logtype_sl, "%s%d:\t%s", neststrings[nestinglevel + 1], n + +1, s);
120         } else if (STRCMP(type, ==, "sl_bool_t")) {
121             sl_bool_t bl;
122             memcpy(&bl, dd->dd_talloc_array[n], sizeof(sl_bool_t));
123             LOG(log_debug, logtype_sl, "%s%d:\t%s", neststrings[nestinglevel + 1], n + +1, bl ? "true" : "false");
124         } else if (STRCMP(type, ==, "sl_cnids_t")) {
125             sl_cnids_t cnids;
126             memcpy(&cnids, dd->dd_talloc_array[n], sizeof(sl_cnids_t));
127             LOG(log_debug, logtype_sl, "%s%d:\tunkn1: %" PRIu16 ", unkn2: %" PRIu32,
128                    neststrings[nestinglevel + 1], n + 1, cnids.ca_unkn1, cnids.ca_context);
129             if (cnids.ca_cnids)
130                 dd_dump(cnids.ca_cnids, nestinglevel + 1);
131         }
132     }
133     LOG(log_debug, logtype_sl, "%s}", neststrings[nestinglevel]);
134 }
135
136 /*
137 * Returns the UTF-16 string encoding, by checking the 2-byte byte order mark.
138 * If there is no byte order mark, -1 is returned.
139 */
140 static uint spotlight_get_utf16_string_encoding(const char *buf, int offset, int query_length, uint encoding) {
141     uint utf16_encoding;
142
143     /* check for byte order mark */
144     utf16_encoding = SL_ENC_BIG_ENDIAN;
145     if (query_length >= 2) {
146         uint16_t byte_order_mark;
147         if (encoding == SL_ENC_LITTLE_ENDIAN)
148             byte_order_mark = SVAL(buf, offset);
149         else
150             byte_order_mark = RSVAL(buf, offset);
151
152         if (byte_order_mark == 0xFFFE) {
153             utf16_encoding = SL_ENC_BIG_ENDIAN | SL_ENC_UTF_16;
154         }
155         else if (byte_order_mark == 0xFEFF) {
156             utf16_encoding = SL_ENC_LITTLE_ENDIAN | SL_ENC_UTF_16;
157         }
158     }
159
160     return utf16_encoding;
161 }
162
163 /**************************************************************************************************
164  * marshalling functions
165  **************************************************************************************************/
166
167 #define SL_OFFSET_DELTA 16
168
169 static uint64_t sl_pack_tag(uint16_t type, uint16_t size_or_count, uint32_t val)
170 {
171     uint64_t tag = ((uint64_t)val << 32) | ((uint64_t)type << 16) | size_or_count;
172     return tag;
173 }
174
175 static int sl_pack_float(double d, char *buf, int offset)
176 {
177     union {
178         double d;
179         uint64_t w;
180     } ieee_fp_union;
181
182     SLVAL(buf, offset, sl_pack_tag(SQ_TYPE_FLOAT, 2, 1));
183     SLVAL(buf, offset + 8, ieee_fp_union.w);
184
185     return offset + 2 * sizeof(uint64_t);
186 }
187
188 static int sl_pack_uint64(uint64_t u, char *buf, int offset)
189 {
190     SLVAL(buf, offset, sl_pack_tag(SQ_TYPE_INT64, 2, 1));
191     SLVAL(buf, offset + 8, u);
192
193     return offset + 2 * sizeof(uint64_t);
194 }
195
196 static int sl_pack_bool(sl_bool_t bl, char *buf, int offset)
197 {
198     SLVAL(buf, offset, sl_pack_tag(SQ_TYPE_BOOL, 1, bl ? 1 : 0));
199
200     return offset + sizeof(uint64_t);
201 }
202
203 static int sl_pack_nil(char *buf, int offset)
204 {
205     SLVAL(buf, offset, sl_pack_tag(SQ_TYPE_NULL, 1, 1));
206
207     return offset + sizeof(uint64_t);
208 }
209
210 static int sl_pack_date(sl_time_t t, char *buf, int offset)
211 {
212     uint64_t data = 0;
213
214     data = (t.tv_sec + SPOTLIGHT_TIME_DELTA) << 24;
215
216     SLVAL(buf, offset, sl_pack_tag(SQ_TYPE_DATE, 2, 1));
217     SLVAL(buf, offset + 8, data);
218
219     return offset + 2 * sizeof(uint64_t);
220 }
221
222 static int sl_pack_uuid(sl_uuid_t *uuid, char *buf, int offset)
223 {
224     SLVAL(buf, offset, sl_pack_tag(SQ_TYPE_UUID, 3, 1));
225     memcpy(buf + offset + 8, uuid, 16);
226
227     return offset + sizeof(uint64_t) + 16;
228 }
229
230 static int sl_pack_CNID(sl_cnids_t *cnids, char *buf, int offset, char *toc_buf, int *toc_idx)
231 {
232     int len = 0, off = 0;
233     int cnid_count = talloc_array_length(cnids->ca_cnids);
234
235     SLVAL(toc_buf, *toc_idx * 8, sl_pack_tag(SQ_CPX_TYPE_CNIDS, (offset + SL_OFFSET_DELTA) / 8, cnid_count));
236     SLVAL(buf, offset, sl_pack_tag(SQ_TYPE_COMPLEX, 1, *toc_idx + 1));
237     *toc_idx += 1;
238     offset += 8;
239
240     SLVAL(buf, offset, sl_pack_tag(SQ_TYPE_CNIDS, 2 + cnid_count, 8 /* unknown meaning, but always 8 */));
241     offset += 8;
242
243     if (cnid_count > 0) {
244         SLVAL(buf, offset, sl_pack_tag(0x0add, cnid_count, cnids->ca_context));
245         offset += 8;
246
247         for (int i = 0; i < cnid_count; i++) {
248             SLVAL(buf, offset, cnids->ca_cnids->dd_talloc_array[i]);
249             offset += 8;
250         }
251     }
252     
253     return offset;
254 }
255
256 static int sl_pack_array(sl_array_t *array, char *buf, int offset, char *toc_buf, int *toc_idx)
257 {
258     int count = talloc_array_length(array->dd_talloc_array);
259     int octets = (offset + SL_OFFSET_DELTA) / 8;
260
261     LOG(log_maxdebug, logtype_sl, "sl_pack_array: count: %d, offset:%d, octets: %d", count, offset, octets);
262
263     SLVAL(toc_buf, *toc_idx * 8, sl_pack_tag(SQ_CPX_TYPE_ARRAY, octets, count));
264     SLVAL(buf, offset, sl_pack_tag(SQ_TYPE_COMPLEX, 1, *toc_idx + 1));
265     *toc_idx += 1;
266     offset += 8;
267
268     offset = sl_pack_loop(array, buf, offset, toc_buf, toc_idx);
269
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     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)));
276     SLVAL(buf, offset, sl_pack_tag(SQ_TYPE_COMPLEX, 1, *toc_idx + 1));
277     *toc_idx += 1;
278     offset += 8;
279
280     offset = sl_pack_loop(dict, buf, offset, toc_buf, toc_idx);
281
282     return offset;
283 }
284
285 static int sl_pack_string(char **string, char *buf, int offset, char *toc_buf, int *toc_idx)
286 {
287     int len, octets, used_in_last_octet;
288     char *s = *string;
289     len = strlen(s);
290     octets = (len / 8) + (len & 7 ? 1 : 0);
291     used_in_last_octet = 8 - (octets * 8 - len);
292
293     LOG(log_maxdebug, logtype_sl, "sl_pack_string(\"%s\"): len: %d, octets: %d, used_in_last_octet: %d",
294         s, len, octets, used_in_last_octet);
295
296     SLVAL(toc_buf, *toc_idx * 8, sl_pack_tag(SQ_CPX_TYPE_STRING, (offset + SL_OFFSET_DELTA) / 8, used_in_last_octet));
297     SLVAL(buf, offset, sl_pack_tag(SQ_TYPE_COMPLEX, 1, *toc_idx + 1));
298     *toc_idx += 1;
299     offset += 8;
300
301     SLVAL(buf, offset, sl_pack_tag(SQ_TYPE_DATA, octets + 1, used_in_last_octet));
302     offset += 8;
303
304     memset(buf + offset, 0, octets * 8);
305     strncpy(buf + offset, s, len);
306     offset += octets * 8;
307
308     return offset;
309 }
310
311 static int sl_pack_loop(DALLOC_CTX *query, char *buf, int offset, char *toc_buf, int *toc_idx)
312 {
313     const char *type;
314
315     for (int n = 0; n < talloc_array_length(query->dd_talloc_array); n++) {
316
317         type = talloc_get_name(query->dd_talloc_array[n]);
318
319         if (STRCMP(type, ==, "sl_array_t")) {
320             offset = sl_pack_array(query->dd_talloc_array[n], buf, offset, toc_buf, toc_idx);
321         } else if (STRCMP(type, ==, "sl_dict_t")) {
322             offset = sl_pack_dict(query->dd_talloc_array[n], buf, offset, toc_buf, toc_idx);
323         } else if (STRCMP(type, ==, "uint64_t")) {
324             uint64_t i;
325             memcpy(&i, query->dd_talloc_array[n], sizeof(uint64_t));
326             offset = sl_pack_uint64(i, buf, offset);
327         } else if (STRCMP(type, ==, "char *")) {
328             offset = sl_pack_string(query->dd_talloc_array[n], buf, offset, toc_buf, toc_idx);
329         } else if (STRCMP(type, ==, "sl_bool_t")) {
330             sl_bool_t bl;
331             memcpy(&bl, query->dd_talloc_array[n], sizeof(sl_bool_t));
332             offset = sl_pack_bool(bl, buf, offset);
333         } else if (STRCMP(type, ==, "double")) {
334             double d;
335             memcpy(&d, query->dd_talloc_array[n], sizeof(double));
336             offset = sl_pack_float(d, buf, offset);
337         } else if (STRCMP(type, ==, "sl_nil_t")) {
338             offset = sl_pack_nil(buf, offset);
339         } else if (STRCMP(type, ==, "sl_time_t")) {
340             sl_time_t t;
341             memcpy(&t, query->dd_talloc_array[n], sizeof(sl_time_t));
342             offset = sl_pack_date(t, buf, offset);
343         } else if (STRCMP(type, ==, "sl_uuid_t")) {
344             offset = sl_pack_uuid(query->dd_talloc_array[n], buf, offset);
345         } else if (STRCMP(type, ==, "sl_cnids_t")) {
346             offset = sl_pack_CNID(query->dd_talloc_array[n], buf, offset, toc_buf, toc_idx);
347         }
348     }
349
350     return offset;
351 }
352
353 #define MAX_SLQ_DAT 65000
354 #define MAX_SLQ_TOC 2048
355
356 static int sl_pack(DALLOC_CTX *query, char *buf)
357 {
358     EC_INIT;
359     char toc_buf[MAX_SLQ_TOC];
360     int toc_index = 0;
361     int len = 0;
362
363     memcpy(buf, "432130dm", 8);
364     EC_NEG1_LOG( len = sl_pack_loop(query, buf + 16, 0, toc_buf + 8, &toc_index) );
365     SIVAL(buf, 8, len / 8 + 1 + toc_index + 1);
366     SIVAL(buf, 12, len / 8 + 1);
367
368     SLVAL(toc_buf, 0, sl_pack_tag(SQ_TYPE_TOC, toc_index + 1, 0));
369     memcpy(buf + 16 + len, toc_buf, (toc_index + 1 ) * 8);
370
371     len += 16 + (toc_index + 1 ) * 8;
372
373 EC_CLEANUP:
374     if (ret != 0)
375         len = -1;
376     return len;
377 }
378
379 /**************************************************************************************************
380  * unmarshalling functions
381  **************************************************************************************************/
382
383 static uint64_t sl_unpack_uint64(const char *buf, int offset, uint encoding)
384 {
385     if (encoding == SL_ENC_LITTLE_ENDIAN)
386             return LVAL(buf, offset);
387         else
388             return RLVAL(buf, offset);
389 }
390
391 static int sl_unpack_ints(DALLOC_CTX *query, const char *buf, int offset, uint encoding)
392 {
393     int count, i;
394     uint64_t query_data64;
395
396     query_data64 = sl_unpack_uint64(buf, offset, encoding);
397     count = query_data64 >> 32;
398     offset += 8;
399
400     i = 0;
401     while (i++ < count) {
402         query_data64 = sl_unpack_uint64(buf, offset, encoding);
403         dalloc_add(query, &query_data64, uint64_t);
404         offset += 8;
405     }
406
407     return count;
408 }
409
410 static int sl_unpack_date(DALLOC_CTX *query, const char *buf, int offset, uint encoding)
411 {
412     int count, i;
413     uint64_t query_data64;
414     sl_time_t t;
415
416     query_data64 = sl_unpack_uint64(buf, offset, encoding);
417     count = query_data64 >> 32;
418     offset += 8;
419
420     i = 0;
421     while (i++ < count) {
422         query_data64 = sl_unpack_uint64(buf, offset, encoding) >> 24;
423         t.tv_sec = query_data64 - SPOTLIGHT_TIME_DELTA;
424         t.tv_usec = 0;
425         dalloc_add(query, &t, sl_time_t);
426         offset += 8;
427     }
428
429     return count;
430 }
431
432 static int sl_unpack_uuid(DALLOC_CTX *query, const char *buf, int offset, uint encoding)
433 {
434     int count, i;
435     uint64_t query_data64;
436     sl_uuid_t uuid;
437     query_data64 = sl_unpack_uint64(buf, offset, encoding);
438     count = query_data64 >> 32;
439     offset += 8;
440
441     i = 0;
442     while (i++ < count) {
443         memcpy(uuid.sl_uuid, buf + offset, 16);
444         dalloc_add(query, &uuid, sl_uuid_t);
445         offset += 16;
446     }
447
448     return count;
449 }
450
451 static int sl_unpack_floats(DALLOC_CTX *query, const char *buf, int offset, uint encoding)
452 {
453     int count, i;
454     uint64_t query_data64;
455     double fval;
456     union {
457         double d;
458         uint32_t w[2];
459     } ieee_fp_union;
460
461     query_data64 = sl_unpack_uint64(buf, offset, encoding);
462     count = query_data64 >> 32;
463     offset += 8;
464
465     i = 0;
466     while (i++ < count) {
467         if (encoding == SL_ENC_LITTLE_ENDIAN) {
468 #ifdef WORDS_BIGENDIAN
469             ieee_fp_union.w[0] = IVAL(buf, offset + 4);
470             ieee_fp_union.w[1] = IVAL(buf, offset);
471 #else
472             ieee_fp_union.w[0] = IVAL(buf, offset);
473             ieee_fp_union.w[1] = IVAL(buf, offset + 4);
474 #endif
475         } else {
476 #ifdef WORDS_BIGENDIAN
477             ieee_fp_union.w[0] = RIVAL(buf, offset);
478             ieee_fp_union.w[1] = RIVAL(buf, offset + 4);
479 #else
480             ieee_fp_union.w[0] = RIVAL(buf, offset + 4);
481             ieee_fp_union.w[1] = RIVAL(buf, offset);
482 #endif
483         }
484         dalloc_add(query, &ieee_fp_union.d, double);
485         offset += 8;
486     }
487
488     return count;
489 }
490
491 static int sl_unpack_CNID(DALLOC_CTX *query, const char *buf, int offset, int length, uint encoding)
492 {
493     EC_INIT;
494     int count;
495     uint64_t query_data64;
496     sl_cnids_t cnids;
497
498     EC_NULL( cnids.ca_cnids = talloc_zero(query, DALLOC_CTX) );
499
500     if (length <= 16)
501         /* that's permitted, it's an empty array */
502         goto EC_CLEANUP;
503     
504     query_data64 = sl_unpack_uint64(buf, offset, encoding);
505     count = query_data64 & 0xffff;
506
507     cnids.ca_unkn1 = (query_data64 & 0xffff0000) >> 16;
508     cnids.ca_context = query_data64 >> 32;
509
510     offset += 8;
511
512     while (count --) {
513         query_data64 = sl_unpack_uint64(buf, offset, encoding);
514         dalloc_add(cnids.ca_cnids, &query_data64, uint64_t);
515         offset += 8;
516     }
517
518     dalloc_add(query, &cnids, sl_cnids_t);
519
520 EC_CLEANUP:
521     EC_EXIT;
522 }
523
524 static const char *spotlight_get_qtype_string(uint64_t query_type)
525 {
526     switch (query_type) {
527     case SQ_TYPE_NULL:
528         return "null";
529     case SQ_TYPE_COMPLEX:
530         return "complex";
531     case SQ_TYPE_INT64:
532         return "int64";
533     case SQ_TYPE_BOOL:
534         return "bool";
535     case SQ_TYPE_FLOAT:
536         return "float";
537     case SQ_TYPE_DATA:
538         return "data";
539     case SQ_TYPE_CNIDS:
540         return "CNIDs";
541     default:
542         return "unknown";
543     }
544 }
545
546 static const char *spotlight_get_cpx_qtype_string(uint64_t cpx_query_type)
547 {
548     switch (cpx_query_type) {
549     case SQ_CPX_TYPE_ARRAY:
550         return "array";
551     case SQ_CPX_TYPE_STRING:
552         return "string";
553     case SQ_CPX_TYPE_UTF16_STRING:
554         return "utf-16 string";
555     case SQ_CPX_TYPE_DICT:
556         return "dictionary";
557     case SQ_CPX_TYPE_CNIDS:
558         return "CNIDs";
559     case SQ_CPX_TYPE_FILEMETA:
560         return "FileMeta";
561     default:
562         return "unknown";
563     }
564 }
565
566 static int spotlight_dissect_loop(DALLOC_CTX *query,
567                                   const char *buf,
568                                   uint offset,
569                                   uint count,
570                                   const uint toc_offset,
571                                   const uint encoding)
572 {
573     EC_INIT;
574     int i, toc_index, query_length;
575     uint subcount, cpx_query_type, cpx_query_count;
576     uint64_t query_data64, query_type;
577     uint unicode_encoding;
578     uint8_t mark_exists;
579     char *p;
580     int padding, slen;
581
582     while (count > 0 && (offset < toc_offset)) {
583         query_data64 = sl_unpack_uint64(buf, offset, encoding);
584         query_length = (query_data64 & 0xffff) * 8;
585         query_type = (query_data64 & 0xffff0000) >> 16;
586         if (query_length == 0)
587             EC_FAIL;
588
589         switch (query_type) {
590         case SQ_TYPE_COMPLEX:
591             toc_index = (query_data64 >> 32) - 1;
592             query_data64 = sl_unpack_uint64(buf, toc_offset + toc_index * 8, encoding);
593             cpx_query_type = (query_data64 & 0xffff0000) >> 16;
594             cpx_query_count = query_data64 >> 32;
595
596             switch (cpx_query_type) {
597             case SQ_CPX_TYPE_ARRAY: {
598                 sl_array_t *sl_arrary = talloc_zero(query, sl_array_t);
599                 EC_NEG1_LOG( offset = spotlight_dissect_loop(sl_arrary, buf, offset + 8, cpx_query_count, toc_offset, encoding) );
600                 dalloc_add(query, sl_arrary, sl_array_t);
601                 break;
602             }
603
604             case SQ_CPX_TYPE_DICT: {
605                 sl_dict_t *sl_dict = talloc_zero(query, sl_dict_t);
606                 EC_NEG1_LOG( offset = spotlight_dissect_loop(sl_dict, buf, offset + 8, cpx_query_count, toc_offset, encoding) );
607                 dalloc_add(query, sl_dict, sl_dict_t);
608                 break;
609             }
610             case SQ_CPX_TYPE_STRING:
611                 query_data64 = sl_unpack_uint64(buf, offset + 8, encoding);
612                 query_length += (query_data64 & 0xffff) * 8;
613                 if ((padding = 8 - (query_data64 >> 32)) < 0)
614                     EC_FAIL;
615                 if ((slen = query_length - 16 - padding) < 1)
616                     EC_FAIL;
617                 p = talloc_strndup(query, buf + offset + 16, slen);
618                 dalloc_add(query, &p, char *);
619                 break;
620
621             case SQ_CPX_TYPE_UTF16_STRING:
622                 query_data64 = sl_unpack_uint64(buf, offset + 8, encoding);
623                 query_length += (query_data64 & 0xffff) * 8;
624                 if ((padding = 8 - (query_data64 >> 32)) < 0)
625                     EC_FAIL;
626                 if ((slen = query_length - 16 - padding) < 1)
627                     EC_FAIL;
628
629                 unicode_encoding = spotlight_get_utf16_string_encoding(buf, offset + 16, slen, encoding);
630                 mark_exists = (unicode_encoding & SL_ENC_UTF_16);
631                 unicode_encoding &= ~SL_ENC_UTF_16;
632
633                 EC_NEG1( convert_string_allocate(CH_UCS2, CH_UTF8, buf + offset + (mark_exists ? 18 : 16), slen, &p) );
634                 dalloc_add(query, &p, char *);
635                 break;
636
637             case SQ_CPX_TYPE_FILEMETA:
638                 query_data64 = sl_unpack_uint64(buf, offset + 8, encoding);
639                 query_length += (query_data64 & 0xffff) * 8;
640
641                 if (query_length <= 8) {
642                     EC_FAIL_LOG("SQ_CPX_TYPE_FILEMETA: query_length <= 8%s", "");
643                 } else {
644                     EC_NEG1_LOG( dissect_spotlight(query, buf + offset + 16) );
645                 }
646                 break;
647
648             case SQ_CPX_TYPE_CNIDS:
649                 query_data64 = sl_unpack_uint64(buf, offset + 8, encoding);
650                 query_length += (query_data64 & 0xffff) * 8;
651                 EC_NEG1_LOG( sl_unpack_CNID(query, buf, offset + 16, query_length, encoding) );
652                 break;
653             } /* switch (cpx_query_type) */
654
655             count--;
656             break;
657
658         case SQ_TYPE_NULL: {
659             subcount = query_data64 >> 32;
660             if (subcount > 64)
661                 EC_FAIL;
662             sl_nil_t nil = 0;
663             for (i = 0; i < subcount; i++)
664                 dalloc_add(query, &nil, sl_nil_t);
665             count -= subcount;
666             break;
667         }
668         case SQ_TYPE_BOOL: {
669             sl_bool_t b = query_data64 >> 32;
670             dalloc_add(query, &b, sl_bool_t);
671             count--;
672             break;
673         }
674         case SQ_TYPE_INT64:
675             EC_NEG1_LOG( subcount = sl_unpack_ints(query, buf, offset, encoding) );
676             count -= subcount;
677             break;
678         case SQ_TYPE_UUID:
679             EC_NEG1_LOG( subcount = sl_unpack_uuid(query, buf, offset, encoding) );
680             count -= subcount;
681             break;
682         case SQ_TYPE_FLOAT:
683             EC_NEG1_LOG( subcount = sl_unpack_floats(query, buf, offset, encoding) );
684             count -= subcount;
685             break;
686         case SQ_TYPE_DATE:
687             EC_NEG1_LOG( subcount = sl_unpack_date(query, buf, offset, encoding) );
688             count -= subcount;
689             break;
690         default:
691             EC_FAIL;
692         }
693
694         offset += query_length;
695     }
696
697 EC_CLEANUP:
698     if (ret != 0) {
699         offset = -1;
700     }
701     return offset;
702 }
703
704 static int dissect_spotlight(DALLOC_CTX *query, const char *buf)
705 {
706     EC_INIT;
707     int encoding, i, toc_entries;
708     uint64_t toc_offset, tquerylen, toc_entry;
709
710     if (strncmp(buf, "md031234", 8) == 0)
711         encoding = SL_ENC_BIG_ENDIAN;
712     else
713         encoding = SL_ENC_LITTLE_ENDIAN;
714
715     buf += 8;
716
717     toc_offset = ((sl_unpack_uint64(buf, 0, encoding) >> 32) - 1 ) * 8;
718     if (toc_offset < 0 || (toc_offset > 65000)) {
719         EC_FAIL;
720     }
721
722     buf += 8;
723
724     toc_entries = (int)(sl_unpack_uint64(buf, toc_offset, encoding) & 0xffff);
725
726     EC_NEG1( spotlight_dissect_loop(query, buf, 0, 1, toc_offset + 8, encoding) );
727
728 EC_CLEANUP:
729     EC_EXIT;
730 }
731
732 /**************************************************************************************************
733  * AFP functions
734  **************************************************************************************************/
735 int afp_spotlight_rpc(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
736 {
737     EC_INIT;
738     TALLOC_CTX *tmp_ctx = talloc_new(NULL);
739     uint16_t vid;
740     int cmd;
741     int endianess = SL_ENC_LITTLE_ENDIAN;
742     struct vol      *vol;
743     DALLOC_CTX *query;
744
745     *rbuflen = 0;
746
747     ibuf += 2;
748     ibuflen -= 2;
749
750     vid = SVAL(ibuf, 0);
751     LOG(log_debug, logtype_sl, "afp_spotlight_rpc(vid: %" PRIu16 ")", vid);
752
753     if ((vol = getvolbyvid(vid)) == NULL) {
754         LOG(log_error, logtype_sl, "afp_spotlight_rpc: bad volume id: %" PRIu16 ")", vid);
755         ret = AFPERR_ACCESS;
756         goto EC_CLEANUP;
757     }
758
759     /*    IVAL(ibuf, 2): unknown, always 0x00008004, some flags ? */
760
761     cmd = RIVAL(ibuf, 6);
762     LOG(log_debug, logtype_sl, "afp_spotlight_rpc(cmd: %d)", cmd);
763
764     /*    IVAL(ibuf, 10: unknown, always 0x00000000 */
765
766     switch (cmd) {
767
768     case SPOTLIGHT_CMD_VOLPATH: {
769         RSIVAL(rbuf, 0, ntohs(vid));
770         RSIVAL(rbuf, 4, 0);
771         int len = strlen(vol->v_path) + 1;
772         strncpy(rbuf + 8, vol->v_path, len);
773         *rbuflen += 8 + len;
774         break;
775     }
776     case SPOTLIGHT_CMD_FLAGS:
777         RSIVAL(rbuf, 0, 0x0100006b); /* Whatever this value means... flags? */
778         *rbuflen += 4;
779         break;
780
781     case SPOTLIGHT_CMD_RPC: {
782         DALLOC_CTX *query;
783         EC_NULL( query = talloc_zero(tmp_ctx, DALLOC_CTX) );
784         (void)dissect_spotlight(query, ibuf + 22);
785         dd_dump(query, 0);
786         break;
787     }
788     }
789
790 EC_CLEANUP:
791     talloc_free(tmp_ctx);
792     if (ret != AFP_OK) {
793         return AFPERR_MISC;
794     }
795     EC_EXIT;
796 }
797
798 /**************************************************************************************************
799  * Testing
800  **************************************************************************************************/
801
802 #ifdef SPOT_TEST_MAIN
803
804 int main(int argc, char **argv)
805 {
806     EC_INIT;
807     TALLOC_CTX *mem_ctx = talloc_new(NULL);
808     DALLOC_CTX *dd = talloc_zero(mem_ctx, DALLOC_CTX);
809     int64_t i;
810
811     set_processname("spot");
812     setuplog("default:info,spotlight:debug", "/dev/tty");
813
814     LOG(log_info, logtype_sl, "Start");
815
816 #if 0
817     i = 2;
818     dalloc_add(dd, &i, int64_t);
819
820     i = 1;
821     dalloc_add(dd, &i, int64_t);
822
823
824     char *str = talloc_strdup(dd, "hello world");
825     dalloc_add(dd, &str, char *);
826
827     sl_bool_t b = true;
828     dalloc_add(dd, &b, sl_bool_t);
829
830     b = false;
831     dalloc_add(dd, &b, sl_bool_t);
832
833
834     /* add a nested array */
835     DALLOC_CTX *nested = talloc_zero(dd, DALLOC_CTX);
836     i = 3;
837     dalloc_add(nested, &i, int64_t);
838     dalloc_add(dd, nested, DALLOC_CTX);
839
840     /* test an allocated CNID array */
841     uint32_t id = 16;
842     sl_cnids_t *cnids = talloc_zero(dd, sl_cnids_t);
843
844     cnids->ca_cnids = talloc_zero(cnids, DALLOC_CTX);
845
846     cnids->ca_unkn1 = 1;
847     cnids->ca_unkn2 = 2;
848
849     dalloc_add(cnids->ca_cnids, &id, uint32_t);
850     dalloc_add(dd, cnids, sl_cnids_t);
851
852     /* Now the Spotlight types */
853     sl_array_t *sl_arrary = talloc_zero(dd, sl_array_t);
854     i = 1234;
855     dalloc_add(sl_arrary, &i, int64_t);
856
857     sl_dict_t *sl_dict = talloc_zero(dd, sl_dict_t);
858     i = 5678;
859     dalloc_add(sl_dict, &i, int64_t);
860     dalloc_add(sl_arrary, sl_dict, sl_dict_t);
861
862     dalloc_add(dd, sl_arrary, sl_array_t);
863 #endif
864
865     /* now parse a real spotlight packet */
866     char ibuf[8192];
867     char rbuf[8192];
868     int fd;
869     size_t len;
870     DALLOC_CTX *query;
871
872     EC_NULL( query = talloc_zero(mem_ctx, DALLOC_CTX) );
873
874     EC_NEG1_LOG( fd = open("test.bin", O_RDONLY) );
875     EC_NEG1_LOG( len = read(fd, ibuf, 8192) );
876     close(fd);
877     EC_NEG1_LOG( dissect_spotlight(query, ibuf + 24) );
878
879     /* Now dump the whole thing */
880     dd_dump(query, 0);
881
882     int qlen;
883     char buf[MAX_SLQ_DAT];
884     EC_NEG1_LOG( qlen = sl_pack(query, buf) );
885
886     EC_NEG1_LOG( fd = open("test.bin", O_RDWR) );
887     lseek(fd, 24, SEEK_SET);
888     write(fd, buf, qlen);
889     close(fd);
890
891 EC_CLEANUP:
892     if (mem_ctx) {
893         talloc_free(mem_ctx);
894         mem_ctx = NULL;
895     }
896     EC_EXIT;
897 }
898 #endif