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 /* Spotlight epoch is UNIX epoch minus SPOTLIGHT_TIME_DELTA */
43 #define SPOTLIGHT_TIME_DELTA INT64_C(280878921600U)
45 #define SQ_TYPE_NULL 0x0000
46 #define SQ_TYPE_COMPLEX 0x0200
47 #define SQ_TYPE_INT64 0x8400
48 #define SQ_TYPE_BOOL 0x0100
49 #define SQ_TYPE_FLOAT 0x8500
50 #define SQ_TYPE_DATA 0x0700
51 #define SQ_TYPE_CNIDS 0x8700
52 #define SQ_TYPE_UUID 0x0e00
53 #define SQ_TYPE_DATE 0x8600
54 #define SQ_TYPE_TOC 0x8800
56 #define SQ_CPX_TYPE_ARRAY 0x0a00
57 #define SQ_CPX_TYPE_STRING 0x0c00
58 #define SQ_CPX_TYPE_UTF16_STRING 0x1c00
59 #define SQ_CPX_TYPE_DICT 0x0d00
60 #define SQ_CPX_TYPE_CNIDS 0x1a00
61 #define SQ_CPX_TYPE_FILEMETA 0x1b00
63 #define SUBQ_SAFETY_LIM 20
65 /* Forward declarations */
66 static int sl_pack_loop(DALLOC_CTX *query, char *buf, int offset, char *toc_buf, int *toc_idx);
69 * Returns the UTF-16 string encoding, by checking the 2-byte byte order mark.
70 * If there is no byte order mark, -1 is returned.
72 static uint spotlight_get_utf16_string_encoding(const char *buf, int offset, int query_length, uint encoding) {
75 /* check for byte order mark */
76 utf16_encoding = SL_ENC_BIG_ENDIAN;
77 if (query_length >= 2) {
78 uint16_t byte_order_mark;
79 if (encoding == SL_ENC_LITTLE_ENDIAN)
80 byte_order_mark = SVAL(buf, offset);
82 byte_order_mark = RSVAL(buf, offset);
84 if (byte_order_mark == 0xFFFE) {
85 utf16_encoding = SL_ENC_BIG_ENDIAN | SL_ENC_UTF_16;
87 else if (byte_order_mark == 0xFEFF) {
88 utf16_encoding = SL_ENC_LITTLE_ENDIAN | SL_ENC_UTF_16;
92 return utf16_encoding;
95 /**************************************************************************************************
96 * marshalling functions
97 **************************************************************************************************/
99 #define SL_OFFSET_DELTA 16
101 static uint64_t sl_pack_tag(uint16_t type, uint16_t size_or_count, uint32_t val)
103 uint64_t tag = ((uint64_t)val << 32) | ((uint64_t)type << 16) | size_or_count;
107 static int sl_pack_float(double d, char *buf, int offset)
114 SLVAL(buf, offset, sl_pack_tag(SQ_TYPE_FLOAT, 2, 1));
115 SLVAL(buf, offset + 8, ieee_fp_union.w);
117 return offset + 2 * sizeof(uint64_t);
120 static int sl_pack_uint64(uint64_t u, char *buf, int offset)
122 SLVAL(buf, offset, sl_pack_tag(SQ_TYPE_INT64, 2, 1));
123 SLVAL(buf, offset + 8, u);
125 return offset + 2 * sizeof(uint64_t);
128 static int sl_pack_bool(sl_bool_t bl, char *buf, int offset)
130 SLVAL(buf, offset, sl_pack_tag(SQ_TYPE_BOOL, 1, bl ? 1 : 0));
132 return offset + sizeof(uint64_t);
135 static int sl_pack_nil(char *buf, int offset)
137 SLVAL(buf, offset, sl_pack_tag(SQ_TYPE_NULL, 1, 1));
139 return offset + sizeof(uint64_t);
142 static int sl_pack_date(sl_time_t t, char *buf, int offset)
146 data = (t.tv_sec + SPOTLIGHT_TIME_DELTA) << 24;
148 SLVAL(buf, offset, sl_pack_tag(SQ_TYPE_DATE, 2, 1));
149 SLVAL(buf, offset + 8, data);
151 return offset + 2 * sizeof(uint64_t);
154 static int sl_pack_uuid(sl_uuid_t *uuid, char *buf, int offset)
156 SLVAL(buf, offset, sl_pack_tag(SQ_TYPE_UUID, 3, 1));
157 memcpy(buf + offset + 8, uuid, 16);
159 return offset + sizeof(uint64_t) + 16;
162 static int sl_pack_CNID(sl_cnids_t *cnids, char *buf, int offset, char *toc_buf, int *toc_idx)
164 int len = 0, off = 0;
165 int cnid_count = talloc_array_length(cnids->ca_cnids);
167 SLVAL(toc_buf, *toc_idx * 8, sl_pack_tag(SQ_CPX_TYPE_CNIDS, (offset + SL_OFFSET_DELTA) / 8, cnid_count));
168 SLVAL(buf, offset, sl_pack_tag(SQ_TYPE_COMPLEX, 1, *toc_idx + 1));
172 SLVAL(buf, offset, sl_pack_tag(SQ_TYPE_CNIDS, 2 + cnid_count, 8 /* unknown meaning, but always 8 */));
175 if (cnid_count > 0) {
176 SLVAL(buf, offset, sl_pack_tag(0x0add, cnid_count, cnids->ca_context));
179 for (int i = 0; i < cnid_count; i++) {
180 SLVAL(buf, offset, cnids->ca_cnids->dd_talloc_array[i]);
188 static int sl_pack_array(sl_array_t *array, char *buf, int offset, char *toc_buf, int *toc_idx)
190 int count = talloc_array_length(array->dd_talloc_array);
191 int octets = (offset + SL_OFFSET_DELTA) / 8;
193 LOG(log_maxdebug, logtype_sl, "sl_pack_array: count: %d, offset:%d, octets: %d", count, offset, octets);
195 SLVAL(toc_buf, *toc_idx * 8, sl_pack_tag(SQ_CPX_TYPE_ARRAY, octets, count));
196 SLVAL(buf, offset, sl_pack_tag(SQ_TYPE_COMPLEX, 1, *toc_idx + 1));
200 offset = sl_pack_loop(array, buf, offset, toc_buf, toc_idx);
205 static int sl_pack_dict(sl_array_t *dict, char *buf, int offset, char *toc_buf, int *toc_idx)
207 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)));
208 SLVAL(buf, offset, sl_pack_tag(SQ_TYPE_COMPLEX, 1, *toc_idx + 1));
212 offset = sl_pack_loop(dict, buf, offset, toc_buf, toc_idx);
217 static int sl_pack_string(char **string, char *buf, int offset, char *toc_buf, int *toc_idx)
219 int len, octets, used_in_last_octet;
222 octets = (len / 8) + (len & 7 ? 1 : 0);
223 used_in_last_octet = 8 - (octets * 8 - len);
225 LOG(log_maxdebug, logtype_sl, "sl_pack_string(\"%s\"): len: %d, octets: %d, used_in_last_octet: %d",
226 s, len, octets, used_in_last_octet);
228 SLVAL(toc_buf, *toc_idx * 8, sl_pack_tag(SQ_CPX_TYPE_STRING, (offset + SL_OFFSET_DELTA) / 8, used_in_last_octet));
229 SLVAL(buf, offset, sl_pack_tag(SQ_TYPE_COMPLEX, 1, *toc_idx + 1));
233 SLVAL(buf, offset, sl_pack_tag(SQ_TYPE_DATA, octets + 1, used_in_last_octet));
236 memset(buf + offset, 0, octets * 8);
237 strncpy(buf + offset, s, len);
238 offset += octets * 8;
243 static int sl_pack_loop(DALLOC_CTX *query, char *buf, int offset, char *toc_buf, int *toc_idx)
247 for (int n = 0; n < talloc_array_length(query->dd_talloc_array); n++) {
249 type = talloc_get_name(query->dd_talloc_array[n]);
251 if (STRCMP(type, ==, "sl_array_t")) {
252 offset = sl_pack_array(query->dd_talloc_array[n], buf, offset, toc_buf, toc_idx);
253 } else if (STRCMP(type, ==, "sl_dict_t")) {
254 offset = sl_pack_dict(query->dd_talloc_array[n], buf, offset, toc_buf, toc_idx);
255 } else if (STRCMP(type, ==, "uint64_t")) {
257 memcpy(&i, query->dd_talloc_array[n], sizeof(uint64_t));
258 offset = sl_pack_uint64(i, buf, offset);
259 } else if (STRCMP(type, ==, "char *")) {
260 offset = sl_pack_string(query->dd_talloc_array[n], buf, offset, toc_buf, toc_idx);
261 } else if (STRCMP(type, ==, "sl_bool_t")) {
263 memcpy(&bl, query->dd_talloc_array[n], sizeof(sl_bool_t));
264 offset = sl_pack_bool(bl, buf, offset);
265 } else if (STRCMP(type, ==, "double")) {
267 memcpy(&d, query->dd_talloc_array[n], sizeof(double));
268 offset = sl_pack_float(d, buf, offset);
269 } else if (STRCMP(type, ==, "sl_nil_t")) {
270 offset = sl_pack_nil(buf, offset);
271 } else if (STRCMP(type, ==, "sl_time_t")) {
273 memcpy(&t, query->dd_talloc_array[n], sizeof(sl_time_t));
274 offset = sl_pack_date(t, buf, offset);
275 } else if (STRCMP(type, ==, "sl_uuid_t")) {
276 offset = sl_pack_uuid(query->dd_talloc_array[n], buf, offset);
277 } else if (STRCMP(type, ==, "sl_cnids_t")) {
278 offset = sl_pack_CNID(query->dd_talloc_array[n], buf, offset, toc_buf, toc_idx);
285 /**************************************************************************************************
286 * unmarshalling functions
287 **************************************************************************************************/
289 static uint64_t sl_unpack_uint64(const char *buf, int offset, uint encoding)
291 if (encoding == SL_ENC_LITTLE_ENDIAN)
292 return LVAL(buf, offset);
294 return RLVAL(buf, offset);
297 static int sl_unpack_ints(DALLOC_CTX *query, const char *buf, int offset, uint encoding)
300 uint64_t query_data64;
302 query_data64 = sl_unpack_uint64(buf, offset, encoding);
303 count = query_data64 >> 32;
307 while (i++ < count) {
308 query_data64 = sl_unpack_uint64(buf, offset, encoding);
309 dalloc_add(query, &query_data64, uint64_t);
316 static int sl_unpack_date(DALLOC_CTX *query, const char *buf, int offset, uint encoding)
319 uint64_t query_data64;
322 query_data64 = sl_unpack_uint64(buf, offset, encoding);
323 count = query_data64 >> 32;
327 while (i++ < count) {
328 query_data64 = sl_unpack_uint64(buf, offset, encoding) >> 24;
329 t.tv_sec = query_data64 - SPOTLIGHT_TIME_DELTA;
331 dalloc_add(query, &t, sl_time_t);
338 static int sl_unpack_uuid(DALLOC_CTX *query, const char *buf, int offset, uint encoding)
341 uint64_t query_data64;
343 query_data64 = sl_unpack_uint64(buf, offset, encoding);
344 count = query_data64 >> 32;
348 while (i++ < count) {
349 memcpy(uuid.sl_uuid, buf + offset, 16);
350 dalloc_add(query, &uuid, sl_uuid_t);
357 static int sl_unpack_floats(DALLOC_CTX *query, const char *buf, int offset, uint encoding)
360 uint64_t query_data64;
367 query_data64 = sl_unpack_uint64(buf, offset, encoding);
368 count = query_data64 >> 32;
372 while (i++ < count) {
373 if (encoding == SL_ENC_LITTLE_ENDIAN) {
374 #ifdef WORDS_BIGENDIAN
375 ieee_fp_union.w[0] = IVAL(buf, offset + 4);
376 ieee_fp_union.w[1] = IVAL(buf, offset);
378 ieee_fp_union.w[0] = IVAL(buf, offset);
379 ieee_fp_union.w[1] = IVAL(buf, offset + 4);
382 #ifdef WORDS_BIGENDIAN
383 ieee_fp_union.w[0] = RIVAL(buf, offset);
384 ieee_fp_union.w[1] = RIVAL(buf, offset + 4);
386 ieee_fp_union.w[0] = RIVAL(buf, offset + 4);
387 ieee_fp_union.w[1] = RIVAL(buf, offset);
390 dalloc_add(query, &ieee_fp_union.d, double);
397 static int sl_unpack_CNID(DALLOC_CTX *query, const char *buf, int offset, int length, uint encoding)
401 uint64_t query_data64;
404 EC_NULL( cnids.ca_cnids = talloc_zero(query, DALLOC_CTX) );
407 /* that's permitted, it's an empty array */
410 query_data64 = sl_unpack_uint64(buf, offset, encoding);
411 count = query_data64 & 0xffff;
413 cnids.ca_unkn1 = (query_data64 & 0xffff0000) >> 16;
414 cnids.ca_context = query_data64 >> 32;
419 query_data64 = sl_unpack_uint64(buf, offset, encoding);
420 dalloc_add(cnids.ca_cnids, &query_data64, uint64_t);
424 dalloc_add(query, &cnids, sl_cnids_t);
430 static const char *spotlight_get_qtype_string(uint64_t query_type)
432 switch (query_type) {
435 case SQ_TYPE_COMPLEX:
452 static const char *spotlight_get_cpx_qtype_string(uint64_t cpx_query_type)
454 switch (cpx_query_type) {
455 case SQ_CPX_TYPE_ARRAY:
457 case SQ_CPX_TYPE_STRING:
459 case SQ_CPX_TYPE_UTF16_STRING:
460 return "utf-16 string";
461 case SQ_CPX_TYPE_DICT:
463 case SQ_CPX_TYPE_CNIDS:
465 case SQ_CPX_TYPE_FILEMETA:
472 static int spotlight_dissect_loop(DALLOC_CTX *query,
476 const uint toc_offset,
480 int i, toc_index, query_length;
481 uint subcount, cpx_query_type, cpx_query_count;
482 uint64_t query_data64, query_type;
483 uint unicode_encoding;
488 while (count > 0 && (offset < toc_offset)) {
489 query_data64 = sl_unpack_uint64(buf, offset, encoding);
490 query_length = (query_data64 & 0xffff) * 8;
491 query_type = (query_data64 & 0xffff0000) >> 16;
492 if (query_length == 0)
495 switch (query_type) {
496 case SQ_TYPE_COMPLEX:
497 toc_index = (query_data64 >> 32) - 1;
498 query_data64 = sl_unpack_uint64(buf, toc_offset + toc_index * 8, encoding);
499 cpx_query_type = (query_data64 & 0xffff0000) >> 16;
500 cpx_query_count = query_data64 >> 32;
502 switch (cpx_query_type) {
503 case SQ_CPX_TYPE_ARRAY: {
504 sl_array_t *sl_arrary = talloc_zero(query, sl_array_t);
505 EC_NEG1_LOG( offset = spotlight_dissect_loop(sl_arrary, buf, offset + 8, cpx_query_count, toc_offset, encoding) );
506 dalloc_add(query, sl_arrary, sl_array_t);
510 case SQ_CPX_TYPE_DICT: {
511 sl_dict_t *sl_dict = talloc_zero(query, sl_dict_t);
512 EC_NEG1_LOG( offset = spotlight_dissect_loop(sl_dict, buf, offset + 8, cpx_query_count, toc_offset, encoding) );
513 dalloc_add(query, sl_dict, sl_dict_t);
516 case SQ_CPX_TYPE_STRING:
517 query_data64 = sl_unpack_uint64(buf, offset + 8, encoding);
518 query_length += (query_data64 & 0xffff) * 8;
519 if ((padding = 8 - (query_data64 >> 32)) < 0)
521 if ((slen = query_length - 16 - padding) < 1)
523 p = talloc_strndup(query, buf + offset + 16, slen);
524 dalloc_add(query, &p, char *);
527 case SQ_CPX_TYPE_UTF16_STRING:
528 query_data64 = sl_unpack_uint64(buf, offset + 8, encoding);
529 query_length += (query_data64 & 0xffff) * 8;
530 if ((padding = 8 - (query_data64 >> 32)) < 0)
532 if ((slen = query_length - 16 - padding) < 1)
535 unicode_encoding = spotlight_get_utf16_string_encoding(buf, offset + 16, slen, encoding);
536 mark_exists = (unicode_encoding & SL_ENC_UTF_16);
537 unicode_encoding &= ~SL_ENC_UTF_16;
539 EC_NEG1( convert_string_allocate(CH_UCS2, CH_UTF8, buf + offset + (mark_exists ? 18 : 16), slen, &p) );
540 dalloc_add(query, &p, char *);
543 case SQ_CPX_TYPE_FILEMETA:
544 query_data64 = sl_unpack_uint64(buf, offset + 8, encoding);
545 query_length += (query_data64 & 0xffff) * 8;
547 if (query_length <= 8) {
548 EC_FAIL_LOG("SQ_CPX_TYPE_FILEMETA: query_length <= 8%s", "");
550 EC_NEG1_LOG( sl_unpack(query, buf + offset + 16) );
554 case SQ_CPX_TYPE_CNIDS:
555 query_data64 = sl_unpack_uint64(buf, offset + 8, encoding);
556 query_length += (query_data64 & 0xffff) * 8;
557 EC_NEG1_LOG( sl_unpack_CNID(query, buf, offset + 16, query_length, encoding) );
559 } /* switch (cpx_query_type) */
565 subcount = query_data64 >> 32;
569 for (i = 0; i < subcount; i++)
570 dalloc_add(query, &nil, sl_nil_t);
575 sl_bool_t b = query_data64 >> 32;
576 dalloc_add(query, &b, sl_bool_t);
581 EC_NEG1_LOG( subcount = sl_unpack_ints(query, buf, offset, encoding) );
585 EC_NEG1_LOG( subcount = sl_unpack_uuid(query, buf, offset, encoding) );
589 EC_NEG1_LOG( subcount = sl_unpack_floats(query, buf, offset, encoding) );
593 EC_NEG1_LOG( subcount = sl_unpack_date(query, buf, offset, encoding) );
600 offset += query_length;
610 /**************************************************************************************************
611 * Global functions for packing und unpacking
612 **************************************************************************************************/
614 #define MAX_SLQ_DAT 65000
615 #define MAX_SLQ_TOC 2048
617 int sl_pack(DALLOC_CTX *query, char *buf)
620 char toc_buf[MAX_SLQ_TOC];
624 memcpy(buf, "432130dm", 8);
625 EC_NEG1_LOG( len = sl_pack_loop(query, buf + 16, 0, toc_buf + 8, &toc_index) );
626 SIVAL(buf, 8, len / 8 + 1 + toc_index + 1);
627 SIVAL(buf, 12, len / 8 + 1);
629 SLVAL(toc_buf, 0, sl_pack_tag(SQ_TYPE_TOC, toc_index + 1, 0));
630 memcpy(buf + 16 + len, toc_buf, (toc_index + 1 ) * 8);
632 len += 16 + (toc_index + 1 ) * 8;
640 int sl_unpack(DALLOC_CTX *query, const char *buf)
643 int encoding, i, toc_entries;
644 uint64_t toc_offset, tquerylen, toc_entry;
646 if (strncmp(buf, "md031234", 8) == 0)
647 encoding = SL_ENC_BIG_ENDIAN;
649 encoding = SL_ENC_LITTLE_ENDIAN;
653 toc_offset = ((sl_unpack_uint64(buf, 0, encoding) >> 32) - 1 ) * 8;
654 if (toc_offset < 0 || (toc_offset > 65000)) {
660 toc_entries = (int)(sl_unpack_uint64(buf, toc_offset, encoding) & 0xffff);
662 EC_NEG1( spotlight_dissect_loop(query, buf, 0, 1, toc_offset + 8, encoding) );