2 Copyright (c) 2012 Frank Lahm <franklahm@gmail.com>
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.
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.
17 #endif /* HAVE_CONFIG_H */
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>
36 #include "spotlight.h"
38 /**************************************************************************************************
39 * RPC data marshalling and unmarshalling
40 **************************************************************************************************/
42 /* FPSpotlightRPC subcommand codes */
43 #define SPOTLIGHT_CMD_VOLPATH 1
44 #define SPOTLIGHT_CMD_FLAGS 2
45 #define SPOTLIGHT_CMD_RPC 3
47 /* Spotlight epoch is UNIX epoch minus SPOTLIGHT_TIME_DELTA */
48 #define SPOTLIGHT_TIME_DELTA INT64_C(280878921600U)
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
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
68 #define SUBQ_SAFETY_LIM 20
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
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);
79 /* Helper functions and stuff */
80 static const char *neststrings[] = {
90 static int dd_dump(DALLOC_CTX *dd, int nestinglevel)
94 LOG(log_debug, logtype_sl, "%s1: %s(#%d): {", neststrings[nestinglevel], talloc_get_name(dd), talloc_array_length(dd->dd_talloc_array));
96 for (int n = 0; n < talloc_array_length(dd->dd_talloc_array); n++) {
98 type = talloc_get_name(dd->dd_talloc_array[n]);
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")) {
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")) {
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")) {
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 *")) {
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")) {
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")) {
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);
130 dd_dump(cnids.ca_cnids, nestinglevel + 1);
133 LOG(log_debug, logtype_sl, "%s}", neststrings[nestinglevel]);
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.
140 static uint spotlight_get_utf16_string_encoding(const char *buf, int offset, int query_length, uint encoding) {
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);
150 byte_order_mark = RSVAL(buf, offset);
152 if (byte_order_mark == 0xFFFE) {
153 utf16_encoding = SL_ENC_BIG_ENDIAN | SL_ENC_UTF_16;
155 else if (byte_order_mark == 0xFEFF) {
156 utf16_encoding = SL_ENC_LITTLE_ENDIAN | SL_ENC_UTF_16;
160 return utf16_encoding;
163 /**************************************************************************************************
164 * marshalling functions
165 **************************************************************************************************/
167 #define SL_OFFSET_DELTA 16
169 static uint64_t sl_pack_tag(uint16_t type, uint16_t size_or_count, uint32_t val)
171 uint64_t tag = ((uint64_t)val << 32) | ((uint64_t)type << 16) | size_or_count;
175 static int sl_pack_float(double d, char *buf, int offset)
182 SLVAL(buf, offset, sl_pack_tag(SQ_TYPE_FLOAT, 2, 1));
183 SLVAL(buf, offset + 8, ieee_fp_union.w);
185 return offset + 2 * sizeof(uint64_t);
188 static int sl_pack_uint64(uint64_t u, char *buf, int offset)
190 SLVAL(buf, offset, sl_pack_tag(SQ_TYPE_INT64, 2, 1));
191 SLVAL(buf, offset + 8, u);
193 return offset + 2 * sizeof(uint64_t);
196 static int sl_pack_bool(sl_bool_t bl, char *buf, int offset)
198 SLVAL(buf, offset, sl_pack_tag(SQ_TYPE_BOOL, 1, bl ? 1 : 0));
200 return offset + sizeof(uint64_t);
203 static int sl_pack_nil(char *buf, int offset)
205 SLVAL(buf, offset, sl_pack_tag(SQ_TYPE_NULL, 1, 1));
207 return offset + sizeof(uint64_t);
210 static int sl_pack_date(sl_time_t t, char *buf, int offset)
214 data = (t.tv_sec + SPOTLIGHT_TIME_DELTA) << 24;
216 SLVAL(buf, offset, sl_pack_tag(SQ_TYPE_DATE, 2, 1));
217 SLVAL(buf, offset + 8, data);
219 return offset + 2 * sizeof(uint64_t);
222 static int sl_pack_uuid(sl_uuid_t *uuid, char *buf, int offset)
224 SLVAL(buf, offset, sl_pack_tag(SQ_TYPE_UUID, 3, 1));
225 memcpy(buf + offset + 8, uuid, 16);
227 return offset + sizeof(uint64_t) + 16;
230 static int sl_pack_CNID(sl_cnids_t *cnids, char *buf, int offset, char *toc_buf, int *toc_idx)
232 int len = 0, off = 0;
233 int cnid_count = talloc_array_length(cnids->ca_cnids);
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));
240 SLVAL(buf, offset, sl_pack_tag(SQ_TYPE_CNIDS, 2 + cnid_count, 8 /* unknown meaning, but always 8 */));
243 if (cnid_count > 0) {
244 SLVAL(buf, offset, sl_pack_tag(0x0add, cnid_count, cnids->ca_context));
247 for (int i = 0; i < cnid_count; i++) {
248 SLVAL(buf, offset, cnids->ca_cnids->dd_talloc_array[i]);
256 static int sl_pack_array(sl_array_t *array, char *buf, int offset, char *toc_buf, int *toc_idx)
258 int count = talloc_array_length(array->dd_talloc_array);
259 int octets = (offset + SL_OFFSET_DELTA) / 8;
261 LOG(log_maxdebug, logtype_sl, "sl_pack_array: count: %d, offset:%d, octets: %d", count, offset, octets);
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));
268 offset = sl_pack_loop(array, buf, offset, toc_buf, toc_idx);
273 static int sl_pack_dict(sl_array_t *dict, char *buf, int offset, char *toc_buf, int *toc_idx)
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));
280 offset = sl_pack_loop(dict, buf, offset, toc_buf, toc_idx);
285 static int sl_pack_string(char **string, char *buf, int offset, char *toc_buf, int *toc_idx)
287 int len, octets, used_in_last_octet;
290 octets = (len / 8) + (len & 7 ? 1 : 0);
291 used_in_last_octet = 8 - (octets * 8 - len);
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);
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));
301 SLVAL(buf, offset, sl_pack_tag(SQ_TYPE_DATA, octets + 1, used_in_last_octet));
304 memset(buf + offset, 0, octets * 8);
305 strncpy(buf + offset, s, len);
306 offset += octets * 8;
311 static int sl_pack_loop(DALLOC_CTX *query, char *buf, int offset, char *toc_buf, int *toc_idx)
315 for (int n = 0; n < talloc_array_length(query->dd_talloc_array); n++) {
317 type = talloc_get_name(query->dd_talloc_array[n]);
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")) {
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")) {
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")) {
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")) {
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);
353 #define MAX_SLQ_DAT 65000
354 #define MAX_SLQ_TOC 2048
356 static int sl_pack(DALLOC_CTX *query, char *buf)
359 char toc_buf[MAX_SLQ_TOC];
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);
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);
371 len += 16 + (toc_index + 1 ) * 8;
379 /**************************************************************************************************
380 * unmarshalling functions
381 **************************************************************************************************/
383 static uint64_t sl_unpack_uint64(const char *buf, int offset, uint encoding)
385 if (encoding == SL_ENC_LITTLE_ENDIAN)
386 return LVAL(buf, offset);
388 return RLVAL(buf, offset);
391 static int sl_unpack_ints(DALLOC_CTX *query, const char *buf, int offset, uint encoding)
394 uint64_t query_data64;
396 query_data64 = sl_unpack_uint64(buf, offset, encoding);
397 count = query_data64 >> 32;
401 while (i++ < count) {
402 query_data64 = sl_unpack_uint64(buf, offset, encoding);
403 dalloc_add(query, &query_data64, uint64_t);
410 static int sl_unpack_date(DALLOC_CTX *query, const char *buf, int offset, uint encoding)
413 uint64_t query_data64;
416 query_data64 = sl_unpack_uint64(buf, offset, encoding);
417 count = query_data64 >> 32;
421 while (i++ < count) {
422 query_data64 = sl_unpack_uint64(buf, offset, encoding) >> 24;
423 t.tv_sec = query_data64 - SPOTLIGHT_TIME_DELTA;
425 dalloc_add(query, &t, sl_time_t);
432 static int sl_unpack_uuid(DALLOC_CTX *query, const char *buf, int offset, uint encoding)
435 uint64_t query_data64;
437 query_data64 = sl_unpack_uint64(buf, offset, encoding);
438 count = query_data64 >> 32;
442 while (i++ < count) {
443 memcpy(uuid.sl_uuid, buf + offset, 16);
444 dalloc_add(query, &uuid, sl_uuid_t);
451 static int sl_unpack_floats(DALLOC_CTX *query, const char *buf, int offset, uint encoding)
454 uint64_t query_data64;
461 query_data64 = sl_unpack_uint64(buf, offset, encoding);
462 count = query_data64 >> 32;
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);
472 ieee_fp_union.w[0] = IVAL(buf, offset);
473 ieee_fp_union.w[1] = IVAL(buf, offset + 4);
476 #ifdef WORDS_BIGENDIAN
477 ieee_fp_union.w[0] = RIVAL(buf, offset);
478 ieee_fp_union.w[1] = RIVAL(buf, offset + 4);
480 ieee_fp_union.w[0] = RIVAL(buf, offset + 4);
481 ieee_fp_union.w[1] = RIVAL(buf, offset);
484 dalloc_add(query, &ieee_fp_union.d, double);
491 static int sl_unpack_CNID(DALLOC_CTX *query, const char *buf, int offset, int length, uint encoding)
495 uint64_t query_data64;
498 EC_NULL( cnids.ca_cnids = talloc_zero(query, DALLOC_CTX) );
501 /* that's permitted, it's an empty array */
504 query_data64 = sl_unpack_uint64(buf, offset, encoding);
505 count = query_data64 & 0xffff;
507 cnids.ca_unkn1 = (query_data64 & 0xffff0000) >> 16;
508 cnids.ca_context = query_data64 >> 32;
513 query_data64 = sl_unpack_uint64(buf, offset, encoding);
514 dalloc_add(cnids.ca_cnids, &query_data64, uint64_t);
518 dalloc_add(query, &cnids, sl_cnids_t);
524 static const char *spotlight_get_qtype_string(uint64_t query_type)
526 switch (query_type) {
529 case SQ_TYPE_COMPLEX:
546 static const char *spotlight_get_cpx_qtype_string(uint64_t cpx_query_type)
548 switch (cpx_query_type) {
549 case SQ_CPX_TYPE_ARRAY:
551 case SQ_CPX_TYPE_STRING:
553 case SQ_CPX_TYPE_UTF16_STRING:
554 return "utf-16 string";
555 case SQ_CPX_TYPE_DICT:
557 case SQ_CPX_TYPE_CNIDS:
559 case SQ_CPX_TYPE_FILEMETA:
566 static int spotlight_dissect_loop(DALLOC_CTX *query,
570 const uint toc_offset,
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;
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)
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;
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);
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);
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)
615 if ((slen = query_length - 16 - padding) < 1)
617 p = talloc_strndup(query, buf + offset + 16, slen);
618 dalloc_add(query, &p, char *);
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)
626 if ((slen = query_length - 16 - padding) < 1)
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;
633 EC_NEG1( convert_string_allocate(CH_UCS2, CH_UTF8, buf + offset + (mark_exists ? 18 : 16), slen, &p) );
634 dalloc_add(query, &p, char *);
637 case SQ_CPX_TYPE_FILEMETA:
638 query_data64 = sl_unpack_uint64(buf, offset + 8, encoding);
639 query_length += (query_data64 & 0xffff) * 8;
641 if (query_length <= 8) {
642 EC_FAIL_LOG("SQ_CPX_TYPE_FILEMETA: query_length <= 8%s", "");
644 EC_NEG1_LOG( dissect_spotlight(query, buf + offset + 16) );
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) );
653 } /* switch (cpx_query_type) */
659 subcount = query_data64 >> 32;
663 for (i = 0; i < subcount; i++)
664 dalloc_add(query, &nil, sl_nil_t);
669 sl_bool_t b = query_data64 >> 32;
670 dalloc_add(query, &b, sl_bool_t);
675 EC_NEG1_LOG( subcount = sl_unpack_ints(query, buf, offset, encoding) );
679 EC_NEG1_LOG( subcount = sl_unpack_uuid(query, buf, offset, encoding) );
683 EC_NEG1_LOG( subcount = sl_unpack_floats(query, buf, offset, encoding) );
687 EC_NEG1_LOG( subcount = sl_unpack_date(query, buf, offset, encoding) );
694 offset += query_length;
704 static int dissect_spotlight(DALLOC_CTX *query, const char *buf)
707 int encoding, i, toc_entries;
708 uint64_t toc_offset, tquerylen, toc_entry;
710 if (strncmp(buf, "md031234", 8) == 0)
711 encoding = SL_ENC_BIG_ENDIAN;
713 encoding = SL_ENC_LITTLE_ENDIAN;
717 toc_offset = ((sl_unpack_uint64(buf, 0, encoding) >> 32) - 1 ) * 8;
718 if (toc_offset < 0 || (toc_offset > 65000)) {
724 toc_entries = (int)(sl_unpack_uint64(buf, toc_offset, encoding) & 0xffff);
726 EC_NEG1( spotlight_dissect_loop(query, buf, 0, 1, toc_offset + 8, encoding) );
732 /**************************************************************************************************
734 **************************************************************************************************/
735 int afp_spotlight_rpc(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
738 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
741 int endianess = SL_ENC_LITTLE_ENDIAN;
751 LOG(log_debug, logtype_sl, "afp_spotlight_rpc(vid: %" PRIu16 ")", vid);
753 if ((vol = getvolbyvid(vid)) == NULL) {
754 LOG(log_error, logtype_sl, "afp_spotlight_rpc: bad volume id: %" PRIu16 ")", vid);
759 /* IVAL(ibuf, 2): unknown, always 0x00008004, some flags ? */
761 cmd = RIVAL(ibuf, 6);
762 LOG(log_debug, logtype_sl, "afp_spotlight_rpc(cmd: %d)", cmd);
764 /* IVAL(ibuf, 10: unknown, always 0x00000000 */
768 case SPOTLIGHT_CMD_VOLPATH: {
769 RSIVAL(rbuf, 0, ntohs(vid));
771 int len = strlen(vol->v_path) + 1;
772 strncpy(rbuf + 8, vol->v_path, len);
776 case SPOTLIGHT_CMD_FLAGS:
777 RSIVAL(rbuf, 0, 0x0100006b); /* Whatever this value means... flags? */
781 case SPOTLIGHT_CMD_RPC: {
783 EC_NULL( query = talloc_zero(tmp_ctx, DALLOC_CTX) );
784 (void)dissect_spotlight(query, ibuf + 22);
791 talloc_free(tmp_ctx);
798 /**************************************************************************************************
800 **************************************************************************************************/
802 #ifdef SPOT_TEST_MAIN
804 int main(int argc, char **argv)
807 TALLOC_CTX *mem_ctx = talloc_new(NULL);
808 DALLOC_CTX *dd = talloc_zero(mem_ctx, DALLOC_CTX);
811 set_processname("spot");
812 setuplog("default:info,spotlight:debug", "/dev/tty");
814 LOG(log_info, logtype_sl, "Start");
818 dalloc_add(dd, &i, int64_t);
821 dalloc_add(dd, &i, int64_t);
824 char *str = talloc_strdup(dd, "hello world");
825 dalloc_add(dd, &str, char *);
828 dalloc_add(dd, &b, sl_bool_t);
831 dalloc_add(dd, &b, sl_bool_t);
834 /* add a nested array */
835 DALLOC_CTX *nested = talloc_zero(dd, DALLOC_CTX);
837 dalloc_add(nested, &i, int64_t);
838 dalloc_add(dd, nested, DALLOC_CTX);
840 /* test an allocated CNID array */
842 sl_cnids_t *cnids = talloc_zero(dd, sl_cnids_t);
844 cnids->ca_cnids = talloc_zero(cnids, DALLOC_CTX);
849 dalloc_add(cnids->ca_cnids, &id, uint32_t);
850 dalloc_add(dd, cnids, sl_cnids_t);
852 /* Now the Spotlight types */
853 sl_array_t *sl_arrary = talloc_zero(dd, sl_array_t);
855 dalloc_add(sl_arrary, &i, int64_t);
857 sl_dict_t *sl_dict = talloc_zero(dd, sl_dict_t);
859 dalloc_add(sl_dict, &i, int64_t);
860 dalloc_add(sl_arrary, sl_dict, sl_dict_t);
862 dalloc_add(dd, sl_arrary, sl_array_t);
865 /* now parse a real spotlight packet */
872 EC_NULL( query = talloc_zero(mem_ctx, DALLOC_CTX) );
874 EC_NEG1_LOG( fd = open("test.bin", O_RDONLY) );
875 EC_NEG1_LOG( len = read(fd, ibuf, 8192) );
877 EC_NEG1_LOG( dissect_spotlight(query, ibuf + 24) );
879 /* Now dump the whole thing */
883 char buf[MAX_SLQ_DAT];
884 EC_NEG1_LOG( qlen = sl_pack(query, buf) );
886 EC_NEG1_LOG( fd = open("test.bin", O_RDWR) );
887 lseek(fd, 24, SEEK_SET);
888 write(fd, buf, qlen);
893 talloc_free(mem_ctx);