]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/spotlight.c
f37e226e1038a79f82b44c067cb8da67683e0e79
[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));
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     SLVAL(toc_buf, *toc_idx * 8, sl_pack_tag(SQ_CPX_TYPE_ARRAY, (offset + SL_OFFSET_DELTA) / 8, talloc_array_length(array->dd_talloc_array)));
259     SLVAL(buf, offset, sl_pack_tag(SQ_TYPE_COMPLEX, 1, *toc_idx));
260     *toc_idx += 1;
261     offset += 8;
262
263     offset = sl_pack_loop(array, buf, offset, toc_buf, toc_idx);
264
265     return offset;
266 }
267
268 static int sl_pack_dict(sl_array_t *dict, char *buf, int offset, char *toc_buf, int *toc_idx)
269 {
270     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)));
271     SLVAL(buf, offset, sl_pack_tag(SQ_TYPE_COMPLEX, 1, *toc_idx));
272     *toc_idx += 1;
273     offset += 8;
274
275     offset = sl_pack_loop(dict, buf, offset, toc_buf, toc_idx);
276
277     return offset;
278 }
279
280 static int sl_pack_string(char *s, char *buf, int offset, char *toc_buf, int *toc_idx)
281 {
282     int len, octets, used_in_last_octet;
283     len = strlen(s);
284     octets = (len / 8) + (len & 7 ? 1 : 0);
285     used_in_last_octet = 8 - (octets * 8 - len);
286
287     SLVAL(toc_buf, *toc_idx * 8, sl_pack_tag(SQ_CPX_TYPE_DICT, (offset + SL_OFFSET_DELTA) / 8, used_in_last_octet));
288     SLVAL(buf, offset, sl_pack_tag(SQ_TYPE_COMPLEX, 1, *toc_idx));
289     *toc_idx += 1;
290     offset += 8;
291
292     SLVAL(buf, offset, sl_pack_tag(SQ_TYPE_DATA, octets + 1, used_in_last_octet));
293     offset += 8;
294
295     memset(buf + offset, 0, octets * 8);
296     strncpy(buf + offset, s, len);
297     offset += octets * 8;
298
299     return offset;
300 }
301
302 static int sl_pack_loop(DALLOC_CTX *query, char *buf, int offset, char *toc_buf, int *toc_idx)
303 {
304     const char *type;
305
306     for (int n = 0; n < talloc_array_length(query->dd_talloc_array); n++) {
307
308         type = talloc_get_name(query->dd_talloc_array[n]);
309
310         if (STRCMP(type, ==, "sl_array_t")) {
311             offset = sl_pack_array(query->dd_talloc_array[n], buf, offset, toc_buf, toc_idx);
312         } else if (STRCMP(type, ==, "sl_dict_t")) {
313             offset = sl_pack_dict(query->dd_talloc_array[n], buf, offset, toc_buf, toc_idx);
314         } else if (STRCMP(type, ==, "uint64_t")) {
315             uint64_t i;
316             memcpy(&i, query->dd_talloc_array[n], sizeof(uint64_t));
317             offset = sl_pack_uint64(i, buf, offset);
318         } else if (STRCMP(type, ==, "char *")) {
319             offset = sl_pack_string(query->dd_talloc_array[n], buf, offset, toc_buf, toc_idx);
320         } else if (STRCMP(type, ==, "sl_bool_t")) {
321             sl_bool_t bl;
322             memcpy(&bl, query->dd_talloc_array[n], sizeof(sl_bool_t));
323             offset = sl_pack_bool(bl, buf, offset);
324         } else if (STRCMP(type, ==, "double")) {
325             double d;
326             memcpy(&d, query->dd_talloc_array[n], sizeof(double));
327             offset = sl_pack_float(d, buf, offset);
328         } else if (STRCMP(type, ==, "sl_nil_t")) {
329             offset = sl_pack_nil(buf, offset);
330         } else if (STRCMP(type, ==, "sl_time_t")) {
331             sl_time_t t;
332             memcpy(&t, query->dd_talloc_array[n], sizeof(sl_time_t));
333             offset = sl_pack_date(t, buf, offset);
334         } else if (STRCMP(type, ==, "sl_uuid_t")) {
335             offset = sl_pack_uuid(query->dd_talloc_array[n], buf, offset);
336         } else if (STRCMP(type, ==, "sl_cnids_t")) {
337             offset = sl_pack_CNID(query->dd_talloc_array[n], buf, offset, toc_buf, toc_idx);
338         }
339     }
340
341     return offset;
342 }
343
344 #define MAX_SLQ_DAT 65000
345 #define MAX_SLQ_TOC 2048
346
347 static int sl_pack(DALLOC_CTX *query, char *buf)
348 {
349     EC_INIT;
350     char toc_buf[MAX_SLQ_TOC];
351     int toc_index = 0;
352     int len = 0;
353
354     memcpy(buf, "432130dm", 8);
355     EC_NEG1_LOG( len = sl_pack_loop(query, buf + 16, 0, toc_buf, &toc_index) );
356     SIVAL(buf, 8, len / 8 + toc_index + 1);
357     SIVAL(buf, 12, toc_index + 1);
358
359     SLVAL(toc_buf, 0, sl_pack_tag(SQ_TYPE_TOC, toc_index + 1, 0));
360     memcpy(buf + 16 + len, toc_buf, (toc_index + 1 ) * 8);
361
362     len += 16 + (toc_index + 1 ) * 8;
363
364 EC_CLEANUP:
365     if (ret != 0)
366         len = -1;
367     return len;
368 }
369
370 /**************************************************************************************************
371  * unmarshalling functions
372  **************************************************************************************************/
373
374 static uint64_t sl_unpack_uint64(const char *buf, int offset, uint encoding)
375 {
376     if (encoding == SL_ENC_LITTLE_ENDIAN)
377             return LVAL(buf, offset);
378         else
379             return RLVAL(buf, offset);
380 }
381
382 static int sl_unpack_ints(DALLOC_CTX *query, const char *buf, int offset, uint encoding)
383 {
384     int count, i;
385     uint64_t query_data64;
386
387     query_data64 = sl_unpack_uint64(buf, offset, encoding);
388     count = query_data64 >> 32;
389     offset += 8;
390
391     i = 0;
392     while (i++ < count) {
393         query_data64 = sl_unpack_uint64(buf, offset, encoding);
394         dalloc_add(query, &query_data64, uint64_t);
395         offset += 8;
396     }
397
398     return count;
399 }
400
401 static int sl_unpack_date(DALLOC_CTX *query, const char *buf, int offset, uint encoding)
402 {
403     int count, i;
404     uint64_t query_data64;
405     sl_time_t t;
406
407     query_data64 = sl_unpack_uint64(buf, offset, encoding);
408     count = query_data64 >> 32;
409     offset += 8;
410
411     i = 0;
412     while (i++ < count) {
413         query_data64 = sl_unpack_uint64(buf, offset, encoding) >> 24;
414         t.tv_sec = query_data64 - SPOTLIGHT_TIME_DELTA;
415         t.tv_usec = 0;
416         dalloc_add(query, &t, sl_time_t);
417         offset += 8;
418     }
419
420     return count;
421 }
422
423 static int sl_unpack_uuid(DALLOC_CTX *query, const char *buf, int offset, uint encoding)
424 {
425     int count, i;
426     uint64_t query_data64;
427     sl_uuid_t uuid;
428     query_data64 = sl_unpack_uint64(buf, offset, encoding);
429     count = query_data64 >> 32;
430     offset += 8;
431
432     i = 0;
433     while (i++ < count) {
434         memcpy(uuid.sl_uuid, buf + offset, 16);
435         dalloc_add(query, &uuid, sl_uuid_t);
436         offset += 16;
437     }
438
439     return count;
440 }
441
442 static int sl_unpack_floats(DALLOC_CTX *query, const char *buf, int offset, uint encoding)
443 {
444     int count, i;
445     uint64_t query_data64;
446     double fval;
447     union {
448         double d;
449         uint32_t w[2];
450     } ieee_fp_union;
451
452     query_data64 = sl_unpack_uint64(buf, offset, encoding);
453     count = query_data64 >> 32;
454     offset += 8;
455
456     i = 0;
457     while (i++ < count) {
458         if (encoding == SL_ENC_LITTLE_ENDIAN) {
459 #ifdef WORDS_BIGENDIAN
460             ieee_fp_union.w[0] = IVAL(buf, offset + 4);
461             ieee_fp_union.w[1] = IVAL(buf, offset);
462 #else
463             ieee_fp_union.w[0] = IVAL(buf, offset);
464             ieee_fp_union.w[1] = IVAL(buf, offset + 4);
465 #endif
466         } else {
467 #ifdef WORDS_BIGENDIAN
468             ieee_fp_union.w[0] = RIVAL(buf, offset);
469             ieee_fp_union.w[1] = RIVAL(buf, offset + 4);
470 #else
471             ieee_fp_union.w[0] = RIVAL(buf, offset + 4);
472             ieee_fp_union.w[1] = RIVAL(buf, offset);
473 #endif
474         }
475         dalloc_add(query, &ieee_fp_union.d, double);
476         offset += 8;
477     }
478
479     return count;
480 }
481
482 static int sl_unpack_CNID(DALLOC_CTX *query, const char *buf, int offset, int length, uint encoding)
483 {
484     EC_INIT;
485     int count;
486     uint64_t query_data64;
487     sl_cnids_t cnids;
488
489     EC_NULL( cnids.ca_cnids = talloc_zero(query, DALLOC_CTX) );
490
491     if (length <= 16)
492         /* that's permitted, it's an empty array */
493         goto EC_CLEANUP;
494     
495     query_data64 = sl_unpack_uint64(buf, offset, encoding);
496     count = query_data64 & 0xffff;
497
498     cnids.ca_unkn1 = (query_data64 & 0xffff0000) >> 16;
499     cnids.ca_context = query_data64 >> 32;
500
501     offset += 8;
502
503     while (count --) {
504         query_data64 = sl_unpack_uint64(buf, offset, encoding);
505         dalloc_add(cnids.ca_cnids, &query_data64, uint64_t);
506         offset += 8;
507     }
508
509     dalloc_add(query, &cnids, sl_cnids_t);
510
511 EC_CLEANUP:
512     EC_EXIT;
513 }
514
515 static const char *spotlight_get_qtype_string(uint64_t query_type)
516 {
517     switch (query_type) {
518     case SQ_TYPE_NULL:
519         return "null";
520     case SQ_TYPE_COMPLEX:
521         return "complex";
522     case SQ_TYPE_INT64:
523         return "int64";
524     case SQ_TYPE_BOOL:
525         return "bool";
526     case SQ_TYPE_FLOAT:
527         return "float";
528     case SQ_TYPE_DATA:
529         return "data";
530     case SQ_TYPE_CNIDS:
531         return "CNIDs";
532     default:
533         return "unknown";
534     }
535 }
536
537 static const char *spotlight_get_cpx_qtype_string(uint64_t cpx_query_type)
538 {
539     switch (cpx_query_type) {
540     case SQ_CPX_TYPE_ARRAY:
541         return "array";
542     case SQ_CPX_TYPE_STRING:
543         return "string";
544     case SQ_CPX_TYPE_UTF16_STRING:
545         return "utf-16 string";
546     case SQ_CPX_TYPE_DICT:
547         return "dictionary";
548     case SQ_CPX_TYPE_CNIDS:
549         return "CNIDs";
550     case SQ_CPX_TYPE_FILEMETA:
551         return "FileMeta";
552     default:
553         return "unknown";
554     }
555 }
556
557 static int spotlight_dissect_loop(DALLOC_CTX *query,
558                                   const char *buf,
559                                   uint offset,
560                                   uint count,
561                                   const uint toc_offset,
562                                   const uint encoding)
563 {
564     EC_INIT;
565     int i, toc_index, query_length;
566     uint subcount, cpx_query_type, cpx_query_count;
567     uint64_t query_data64, query_type;
568     uint unicode_encoding;
569     uint8_t mark_exists;
570     char *p;
571     int padding, slen;
572
573     while (count > 0 && (offset < toc_offset)) {
574         query_data64 = sl_unpack_uint64(buf, offset, encoding);
575         query_length = (query_data64 & 0xffff) * 8;
576         query_type = (query_data64 & 0xffff0000) >> 16;
577         if (query_length == 0)
578             EC_FAIL;
579
580         switch (query_type) {
581         case SQ_TYPE_COMPLEX:
582             toc_index = (query_data64 >> 32) - 1;
583             query_data64 = sl_unpack_uint64(buf, toc_offset + toc_index * 8, encoding);
584             cpx_query_type = (query_data64 & 0xffff0000) >> 16;
585             cpx_query_count = query_data64 >> 32;
586
587             switch (cpx_query_type) {
588             case SQ_CPX_TYPE_ARRAY: {
589                 sl_array_t *sl_arrary = talloc_zero(query, sl_array_t);
590                 EC_NEG1_LOG( offset = spotlight_dissect_loop(sl_arrary, buf, offset + 8, cpx_query_count, toc_offset, encoding) );
591                 dalloc_add(query, sl_arrary, sl_array_t);
592                 break;
593             }
594
595             case SQ_CPX_TYPE_DICT: {
596                 sl_dict_t *sl_dict = talloc_zero(query, sl_dict_t);
597                 EC_NEG1_LOG( offset = spotlight_dissect_loop(sl_dict, buf, offset + 8, cpx_query_count, toc_offset, encoding) );
598                 dalloc_add(query, sl_dict, sl_dict_t);
599                 break;
600             }
601             case SQ_CPX_TYPE_STRING:
602                 query_data64 = sl_unpack_uint64(buf, offset + 8, encoding);
603                 query_length += (query_data64 & 0xffff) * 8;
604                 if ((padding = 8 - (query_data64 >> 32)) < 0)
605                     EC_FAIL;
606                 if ((slen = query_length - 16 - padding) < 1)
607                     EC_FAIL;
608                 p = talloc_strndup(query, buf + offset + 16, slen);
609                 dalloc_add(query, &p, char *);
610                 break;
611
612             case SQ_CPX_TYPE_UTF16_STRING:
613                 query_data64 = sl_unpack_uint64(buf, offset + 8, encoding);
614                 query_length += (query_data64 & 0xffff) * 8;
615                 if ((padding = 8 - (query_data64 >> 32)) < 0)
616                     EC_FAIL;
617                 if ((slen = query_length - 16 - padding) < 1)
618                     EC_FAIL;
619
620                 unicode_encoding = spotlight_get_utf16_string_encoding(buf, offset + 16, slen, encoding);
621                 mark_exists = (unicode_encoding & SL_ENC_UTF_16);
622                 unicode_encoding &= ~SL_ENC_UTF_16;
623
624                 EC_NEG1( convert_string_allocate(CH_UCS2, CH_UTF8, buf + offset + (mark_exists ? 18 : 16), slen, &p) );
625                 dalloc_add(query, &p, char *);
626                 break;
627
628             case SQ_CPX_TYPE_FILEMETA:
629                 query_data64 = sl_unpack_uint64(buf, offset + 8, encoding);
630                 query_length += (query_data64 & 0xffff) * 8;
631
632                 if (query_length <= 8) {
633                     EC_FAIL_LOG("SQ_CPX_TYPE_FILEMETA: query_length <= 8%s", "");
634                 } else {
635                     EC_NEG1_LOG( dissect_spotlight(query, buf + offset + 16) );
636                 }
637                 break;
638
639             case SQ_CPX_TYPE_CNIDS:
640                 query_data64 = sl_unpack_uint64(buf, offset + 8, encoding);
641                 query_length += (query_data64 & 0xffff) * 8;
642                 EC_NEG1_LOG( sl_unpack_CNID(query, buf, offset + 16, query_length, encoding) );
643                 break;
644             } /* switch (cpx_query_type) */
645
646             count--;
647             break;
648
649         case SQ_TYPE_NULL: {
650             subcount = query_data64 >> 32;
651             if (subcount > 64)
652                 EC_FAIL;
653             sl_nil_t nil = 0;
654             for (i = 0; i < subcount; i++)
655                 dalloc_add(query, &nil, sl_nil_t);
656             count -= subcount;
657             break;
658         }
659         case SQ_TYPE_BOOL: {
660             sl_bool_t b = query_data64 >> 32;
661             dalloc_add(query, &b, sl_bool_t);
662             count--;
663             break;
664         }
665         case SQ_TYPE_INT64:
666             EC_NEG1_LOG( subcount = sl_unpack_ints(query, buf, offset, encoding) );
667             count -= subcount;
668             break;
669         case SQ_TYPE_UUID:
670             EC_NEG1_LOG( subcount = sl_unpack_uuid(query, buf, offset, encoding) );
671             count -= subcount;
672             break;
673         case SQ_TYPE_FLOAT:
674             EC_NEG1_LOG( subcount = sl_unpack_floats(query, buf, offset, encoding) );
675             count -= subcount;
676             break;
677         case SQ_TYPE_DATE:
678             EC_NEG1_LOG( subcount = sl_unpack_date(query, buf, offset, encoding) );
679             count -= subcount;
680             break;
681         default:
682             EC_FAIL;
683         }
684
685         offset += query_length;
686     }
687
688 EC_CLEANUP:
689     if (ret != 0) {
690         offset = -1;
691     }
692     return offset;
693 }
694
695 static int dissect_spotlight(DALLOC_CTX *query, const char *buf)
696 {
697     EC_INIT;
698     int encoding, i, toc_entries;
699     uint64_t toc_offset, tquerylen, toc_entry;
700
701     if (strncmp(buf, "md031234", 8) == 0)
702         encoding = SL_ENC_BIG_ENDIAN;
703     else
704         encoding = SL_ENC_LITTLE_ENDIAN;
705
706     buf += 8;
707
708     toc_offset = ((sl_unpack_uint64(buf, 0, encoding) >> 32) - 1 ) * 8;
709     if (toc_offset < 0 || (toc_offset > 65000)) {
710         EC_FAIL;
711     }
712
713     buf += 8;
714
715     toc_entries = (int)(sl_unpack_uint64(buf, toc_offset, encoding) & 0xffff);
716
717     EC_NEG1( spotlight_dissect_loop(query, buf, 0, 1, toc_offset + 8, encoding) );
718
719 EC_CLEANUP:
720     EC_EXIT;
721 }
722
723 /**************************************************************************************************
724  * AFP functions
725  **************************************************************************************************/
726 int afp_spotlight_rpc(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
727 {
728     EC_INIT;
729     TALLOC_CTX *tmp_ctx = talloc_new(NULL);
730     uint16_t vid;
731     int cmd;
732     int endianess = SL_ENC_LITTLE_ENDIAN;
733     struct vol      *vol;
734     DALLOC_CTX *query;
735
736     *rbuflen = 0;
737
738     ibuf += 2;
739     ibuflen -= 2;
740
741     vid = SVAL(ibuf, 0);
742     LOG(log_debug, logtype_sl, "afp_spotlight_rpc(vid: %" PRIu16 ")", vid);
743
744     if ((vol = getvolbyvid(vid)) == NULL) {
745         LOG(log_error, logtype_sl, "afp_spotlight_rpc: bad volume id: %" PRIu16 ")", vid);
746         ret = AFPERR_ACCESS;
747         goto EC_CLEANUP;
748     }
749
750     /*    IVAL(ibuf, 2): unknown, always 0x00008004, some flags ? */
751
752     cmd = RIVAL(ibuf, 6);
753     LOG(log_debug, logtype_sl, "afp_spotlight_rpc(cmd: %d)", cmd);
754
755     /*    IVAL(ibuf, 10: unknown, always 0x00000000 */
756
757     switch (cmd) {
758
759     case SPOTLIGHT_CMD_VOLPATH: {
760         RSIVAL(rbuf, 0, ntohs(vid));
761         RSIVAL(rbuf, 4, 0);
762         int len = strlen(vol->v_path) + 1;
763         strncpy(rbuf + 8, vol->v_path, len);
764         *rbuflen += 8 + len;
765         break;
766     }
767     case SPOTLIGHT_CMD_FLAGS:
768         RSIVAL(rbuf, 0, 0x0100006b); /* Whatever this value means... flags? */
769         *rbuflen += 4;
770         break;
771
772     case SPOTLIGHT_CMD_RPC: {
773         DALLOC_CTX *query;
774         EC_NULL( query = talloc_zero(tmp_ctx, DALLOC_CTX) );
775         (void)dissect_spotlight(query, ibuf + 22);
776         dd_dump(query, 0);
777         break;
778     }
779     }
780
781 EC_CLEANUP:
782     talloc_free(tmp_ctx);
783     if (ret != AFP_OK) {
784         return AFPERR_MISC;
785     }
786     EC_EXIT;
787 }
788
789 /**************************************************************************************************
790  * Testing
791  **************************************************************************************************/
792
793 #ifdef SPOT_TEST_MAIN
794
795 int main(int argc, char **argv)
796 {
797     EC_INIT;
798     TALLOC_CTX *mem_ctx = talloc_new(NULL);
799     DALLOC_CTX *dd = talloc_zero(mem_ctx, DALLOC_CTX);
800     int64_t i;
801
802     set_processname("spot");
803     setuplog("default:info,spotlight:debug", "/dev/tty");
804
805     LOG(log_info, logtype_sl, "Start");
806
807 #if 0
808     i = 2;
809     dalloc_add(dd, &i, int64_t);
810
811     i = 1;
812     dalloc_add(dd, &i, int64_t);
813
814
815     char *str = talloc_strdup(dd, "hello world");
816     dalloc_add(dd, &str, char *);
817
818     sl_bool_t b = true;
819     dalloc_add(dd, &b, sl_bool_t);
820
821     b = false;
822     dalloc_add(dd, &b, sl_bool_t);
823
824
825     /* add a nested array */
826     DALLOC_CTX *nested = talloc_zero(dd, DALLOC_CTX);
827     i = 3;
828     dalloc_add(nested, &i, int64_t);
829     dalloc_add(dd, nested, DALLOC_CTX);
830
831     /* test an allocated CNID array */
832     uint32_t id = 16;
833     sl_cnids_t *cnids = talloc_zero(dd, sl_cnids_t);
834
835     cnids->ca_cnids = talloc_zero(cnids, DALLOC_CTX);
836
837     cnids->ca_unkn1 = 1;
838     cnids->ca_unkn2 = 2;
839
840     dalloc_add(cnids->ca_cnids, &id, uint32_t);
841     dalloc_add(dd, cnids, sl_cnids_t);
842
843     /* Now the Spotlight types */
844     sl_array_t *sl_arrary = talloc_zero(dd, sl_array_t);
845     i = 1234;
846     dalloc_add(sl_arrary, &i, int64_t);
847
848     sl_dict_t *sl_dict = talloc_zero(dd, sl_dict_t);
849     i = 5678;
850     dalloc_add(sl_dict, &i, int64_t);
851     dalloc_add(sl_arrary, sl_dict, sl_dict_t);
852
853     dalloc_add(dd, sl_arrary, sl_array_t);
854 #endif
855
856     /* now parse a real spotlight packet */
857     char ibuf[8192];
858     char rbuf[8192];
859     int fd;
860     size_t len;
861     DALLOC_CTX *query;
862
863     EC_NULL( query = talloc_zero(mem_ctx, DALLOC_CTX) );
864
865     EC_NEG1_LOG( fd = open("spotlight-packet2.bin", O_RDONLY) );
866     EC_NEG1_LOG( len = read(fd, ibuf, 8192) );
867     close(fd);
868     EC_NEG1_LOG( dissect_spotlight(query, ibuf + 24) );
869
870     /* Now dump the whole thing */
871     dd_dump(query, 0);
872
873     int qlen;
874     char buf[MAX_SLQ_DAT];
875     EC_NEG1_LOG( qlen = sl_pack(query, buf) );
876
877     EC_NEG1_LOG( fd = open("tmp", O_RDWR | O_CREAT, 0644) );
878     write(fd, buf, qlen);
879     close(fd);
880
881     SLVAL(buf, 0, INT64_C(0x0102030405060708U));
882
883 #if 0
884     (((unsigned char *)((buf)))[(0)])         = (unsigned char)((((uint64_t)(0x0102030405060708UL))&0xFFFFFFFF&0xFFFF) &0xFF);
885     (((unsigned char *)((buf)))[(0)+1])       = (unsigned char)((((uint64_t)(0x0102030405060708UL))&0xFFFFFFFF&0xFFFF) >> 8);
886     (((unsigned char *)((buf)))[(0)+2])       = (unsigned char)((((uint64_t)(0x0102030405060708UL))&0xFFFFFFFF>>16) & 0xFF);
887     (((unsigned char *)((buf)))[(0)+2 +1])    = (unsigned char)((((uint64_t)(0x0102030405060708UL))&0xFFFFFFFF>>16)>>8);
888     (((unsigned char *)((buf)))[(0)+4])       = (unsigned char)((((uint64_t)(0x0102030405060708UL))>>32&0xFFFF)&0xFF);
889     (((unsigned char *)((buf)))[(0)+4 +1])    = (unsigned char)((((uint64_t)(0x0102030405060708UL))>>32&0xFFFF)>>8);
890     (((unsigned char *)((buf)))[(0)+4 +2])    = (unsigned char)((((uint64_t)(0x0102030405060708UL))>>32>>16)&0xFF);
891     (((unsigned char *)((buf)))[(0)+4 +2 +1]) = (unsigned char)((((uint64_t)(0x0102030405060708UL))>>32>>16)>>8);
892 #endif
893
894     EC_NEG1_LOG( fd = open("tmp2", O_RDWR | O_CREAT, 0644) );
895     write(fd, buf, 8);
896     close(fd);
897
898 EC_CLEANUP:
899     if (mem_ctx) {
900         talloc_free(mem_ctx);
901         mem_ctx = NULL;
902     }
903     EC_EXIT;
904 }
905 #endif