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);
67 static int sl_unpack_loop(DALLOC_CTX *query, const char *buf, int offset, uint count, const uint toc_offset, const uint encoding);
70 * Returns the UTF-16 string encoding, by checking the 2-byte byte order mark.
71 * If there is no byte order mark, -1 is returned.
73 static uint spotlight_get_utf16_string_encoding(const char *buf, int offset, int query_length, uint encoding) {
76 /* check for byte order mark */
77 utf16_encoding = SL_ENC_BIG_ENDIAN;
78 if (query_length >= 2) {
79 uint16_t byte_order_mark;
80 if (encoding == SL_ENC_LITTLE_ENDIAN)
81 byte_order_mark = SVAL(buf, offset);
83 byte_order_mark = RSVAL(buf, offset);
85 if (byte_order_mark == 0xFFFE) {
86 utf16_encoding = SL_ENC_BIG_ENDIAN | SL_ENC_UTF_16;
88 else if (byte_order_mark == 0xFEFF) {
89 utf16_encoding = SL_ENC_LITTLE_ENDIAN | SL_ENC_UTF_16;
93 return utf16_encoding;
96 /**************************************************************************************************
97 * marshalling functions
98 **************************************************************************************************/
100 #define SL_OFFSET_DELTA 16
102 static uint64_t sl_pack_tag(uint16_t type, uint16_t size_or_count, uint32_t val)
104 uint64_t tag = ((uint64_t)val << 32) | ((uint64_t)type << 16) | size_or_count;
108 static int sl_pack_float(double d, char *buf, int offset)
115 SLVAL(buf, offset, sl_pack_tag(SQ_TYPE_FLOAT, 2, 1));
116 SLVAL(buf, offset + 8, ieee_fp_union.w);
118 return offset + 2 * sizeof(uint64_t);
121 static int sl_pack_uint64(uint64_t u, char *buf, int offset)
123 SLVAL(buf, offset, sl_pack_tag(SQ_TYPE_INT64, 2, 1));
124 SLVAL(buf, offset + 8, u);
126 return offset + 2 * sizeof(uint64_t);
129 static int sl_pack_bool(sl_bool_t bl, char *buf, int offset)
131 SLVAL(buf, offset, sl_pack_tag(SQ_TYPE_BOOL, 1, bl ? 1 : 0));
133 return offset + sizeof(uint64_t);
136 static int sl_pack_nil(char *buf, int offset)
138 SLVAL(buf, offset, sl_pack_tag(SQ_TYPE_NULL, 1, 1));
140 return offset + sizeof(uint64_t);
143 static int sl_pack_date(sl_time_t t, char *buf, int offset)
147 data = (t.tv_sec + SPOTLIGHT_TIME_DELTA) << 24;
149 SLVAL(buf, offset, sl_pack_tag(SQ_TYPE_DATE, 2, 1));
150 SLVAL(buf, offset + 8, data);
152 return offset + 2 * sizeof(uint64_t);
155 static int sl_pack_uuid(sl_uuid_t *uuid, char *buf, int offset)
157 SLVAL(buf, offset, sl_pack_tag(SQ_TYPE_UUID, 3, 1));
158 memcpy(buf + offset + 8, uuid, 16);
160 return offset + sizeof(uint64_t) + 16;
163 static int sl_pack_CNID(sl_cnids_t *cnids, char *buf, int offset, char *toc_buf, int *toc_idx)
165 int len = 0, off = 0;
166 int cnid_count = talloc_array_length(cnids->ca_cnids);
169 SLVAL(toc_buf, *toc_idx * 8, sl_pack_tag(SQ_CPX_TYPE_CNIDS, (offset + SL_OFFSET_DELTA) / 8, 0 /* cnid_count */));
170 SLVAL(buf, offset, sl_pack_tag(SQ_TYPE_COMPLEX, 1, *toc_idx + 1));
174 SLVAL(buf, offset, sl_pack_tag(SQ_TYPE_CNIDS, 2 + cnid_count, 8 /* unknown meaning, but always 8 */));
177 if (cnid_count > 0) {
178 SLVAL(buf, offset, sl_pack_tag(0x0add, cnid_count, cnids->ca_context));
181 for (int i = 0; i < cnid_count; i++) {
182 memcpy(&id, cnids->ca_cnids->dd_talloc_array[i], sizeof(uint64_t));
183 SLVAL(buf, offset, id);
191 static int sl_pack_array(sl_array_t *array, char *buf, int offset, char *toc_buf, int *toc_idx)
193 int count = talloc_array_length(array->dd_talloc_array);
194 int octets = (offset + SL_OFFSET_DELTA) / 8;
196 LOG(log_maxdebug, logtype_sl, "sl_pack_array: count: %d, offset:%d, octets: %d", count, offset, octets);
198 SLVAL(toc_buf, *toc_idx * 8, sl_pack_tag(SQ_CPX_TYPE_ARRAY, octets, count));
199 SLVAL(buf, offset, sl_pack_tag(SQ_TYPE_COMPLEX, 1, *toc_idx + 1));
203 offset = sl_pack_loop(array, buf, offset, toc_buf, toc_idx);
208 static int sl_pack_dict(sl_array_t *dict, char *buf, int offset, char *toc_buf, int *toc_idx)
210 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)));
211 SLVAL(buf, offset, sl_pack_tag(SQ_TYPE_COMPLEX, 1, *toc_idx + 1));
215 offset = sl_pack_loop(dict, buf, offset, toc_buf, toc_idx);
220 static int sl_pack_filemeta(sl_filemeta_t *fm, char *buf, int offset, char *toc_buf, int *toc_idx)
222 int fmlen; /* lenght of filemeta */
223 int saveoff = offset + 8;
225 SLVAL(buf, offset, sl_pack_tag(SQ_TYPE_COMPLEX, 1, *toc_idx + 1));
228 fmlen = sl_pack(fm, buf + offset);
231 SLVAL(buf, saveoff, sl_pack_tag(SQ_TYPE_DATA, (fmlen / 8) + 1, 8 /* unknown meaning, but always 8 */));
233 SLVAL(toc_buf, *toc_idx * 8, sl_pack_tag(SQ_CPX_TYPE_FILEMETA, (offset + SL_OFFSET_DELTA) / 8, fmlen / 8));
239 static int sl_pack_string(char **string, char *buf, int offset, char *toc_buf, int *toc_idx)
241 int len, octets, used_in_last_octet;
244 octets = (len / 8) + (len & 7 ? 1 : 0);
245 used_in_last_octet = 8 - (octets * 8 - len);
247 LOG(log_maxdebug, logtype_sl, "sl_pack_string(\"%s\"): len: %d, octets: %d, used_in_last_octet: %d",
248 s, len, octets, used_in_last_octet);
250 SLVAL(toc_buf, *toc_idx * 8, sl_pack_tag(SQ_CPX_TYPE_STRING, (offset + SL_OFFSET_DELTA) / 8, used_in_last_octet));
251 SLVAL(buf, offset, sl_pack_tag(SQ_TYPE_COMPLEX, 1, *toc_idx + 1));
255 SLVAL(buf, offset, sl_pack_tag(SQ_TYPE_DATA, octets + 1, used_in_last_octet));
258 memset(buf + offset, 0, octets * 8);
259 strncpy(buf + offset, s, len);
260 offset += octets * 8;
265 static int sl_pack_loop(DALLOC_CTX *query, char *buf, int offset, char *toc_buf, int *toc_idx)
269 for (int n = 0; n < talloc_array_length(query->dd_talloc_array); n++) {
271 type = talloc_get_name(query->dd_talloc_array[n]);
273 if (STRCMP(type, ==, "sl_array_t")) {
274 offset = sl_pack_array(query->dd_talloc_array[n], buf, offset, toc_buf, toc_idx);
275 } else if (STRCMP(type, ==, "sl_dict_t")) {
276 offset = sl_pack_dict(query->dd_talloc_array[n], buf, offset, toc_buf, toc_idx);
277 } else if (STRCMP(type, ==, "uint64_t")) {
279 memcpy(&i, query->dd_talloc_array[n], sizeof(uint64_t));
280 offset = sl_pack_uint64(i, buf, offset);
281 } else if (STRCMP(type, ==, "char *")) {
282 offset = sl_pack_string(query->dd_talloc_array[n], buf, offset, toc_buf, toc_idx);
283 } else if (STRCMP(type, ==, "sl_bool_t")) {
285 memcpy(&bl, query->dd_talloc_array[n], sizeof(sl_bool_t));
286 offset = sl_pack_bool(bl, buf, offset);
287 } else if (STRCMP(type, ==, "double")) {
289 memcpy(&d, query->dd_talloc_array[n], sizeof(double));
290 offset = sl_pack_float(d, buf, offset);
291 } else if (STRCMP(type, ==, "sl_nil_t")) {
292 offset = sl_pack_nil(buf, offset);
293 } else if (STRCMP(type, ==, "sl_time_t")) {
295 memcpy(&t, query->dd_talloc_array[n], sizeof(sl_time_t));
296 offset = sl_pack_date(t, buf, offset);
297 } else if (STRCMP(type, ==, "sl_uuid_t")) {
298 offset = sl_pack_uuid(query->dd_talloc_array[n], buf, offset);
299 } else if (STRCMP(type, ==, "sl_cnids_t")) {
300 offset = sl_pack_CNID(query->dd_talloc_array[n], buf, offset, toc_buf, toc_idx);
307 /**************************************************************************************************
308 * unmarshalling functions
309 **************************************************************************************************/
311 static uint64_t sl_unpack_uint64(const char *buf, int offset, uint encoding)
313 if (encoding == SL_ENC_LITTLE_ENDIAN)
314 return LVAL(buf, offset);
316 return RLVAL(buf, offset);
319 static int sl_unpack_ints(DALLOC_CTX *query, const char *buf, int offset, uint encoding)
322 uint64_t query_data64;
324 query_data64 = sl_unpack_uint64(buf, offset, encoding);
325 count = query_data64 >> 32;
329 while (i++ < count) {
330 query_data64 = sl_unpack_uint64(buf, offset, encoding);
331 dalloc_add(query, &query_data64, uint64_t);
338 static int sl_unpack_date(DALLOC_CTX *query, const char *buf, int offset, uint encoding)
341 uint64_t query_data64;
344 query_data64 = sl_unpack_uint64(buf, offset, encoding);
345 count = query_data64 >> 32;
349 while (i++ < count) {
350 query_data64 = sl_unpack_uint64(buf, offset, encoding) >> 24;
351 t.tv_sec = query_data64 - SPOTLIGHT_TIME_DELTA;
353 dalloc_add(query, &t, sl_time_t);
360 static int sl_unpack_uuid(DALLOC_CTX *query, const char *buf, int offset, uint encoding)
363 uint64_t query_data64;
365 query_data64 = sl_unpack_uint64(buf, offset, encoding);
366 count = query_data64 >> 32;
370 while (i++ < count) {
371 memcpy(uuid.sl_uuid, buf + offset, 16);
372 dalloc_add(query, &uuid, sl_uuid_t);
379 static int sl_unpack_floats(DALLOC_CTX *query, const char *buf, int offset, uint encoding)
382 uint64_t query_data64;
389 query_data64 = sl_unpack_uint64(buf, offset, encoding);
390 count = query_data64 >> 32;
394 while (i++ < count) {
395 if (encoding == SL_ENC_LITTLE_ENDIAN) {
396 #ifdef WORDS_BIGENDIAN
397 ieee_fp_union.w[0] = IVAL(buf, offset + 4);
398 ieee_fp_union.w[1] = IVAL(buf, offset);
400 ieee_fp_union.w[0] = IVAL(buf, offset);
401 ieee_fp_union.w[1] = IVAL(buf, offset + 4);
404 #ifdef WORDS_BIGENDIAN
405 ieee_fp_union.w[0] = RIVAL(buf, offset);
406 ieee_fp_union.w[1] = RIVAL(buf, offset + 4);
408 ieee_fp_union.w[0] = RIVAL(buf, offset + 4);
409 ieee_fp_union.w[1] = RIVAL(buf, offset);
412 dalloc_add(query, &ieee_fp_union.d, double);
419 static int sl_unpack_CNID(DALLOC_CTX *query, const char *buf, int offset, int length, uint encoding)
423 uint64_t query_data64;
426 EC_NULL( cnids.ca_cnids = talloc_zero(query, DALLOC_CTX) );
429 /* that's permitted, it's an empty array */
432 query_data64 = sl_unpack_uint64(buf, offset, encoding);
433 count = query_data64 & 0xffff;
435 cnids.ca_unkn1 = (query_data64 & 0xffff0000) >> 16;
436 cnids.ca_context = query_data64 >> 32;
441 query_data64 = sl_unpack_uint64(buf, offset, encoding);
442 dalloc_add(cnids.ca_cnids, &query_data64, uint64_t);
446 dalloc_add(query, &cnids, sl_cnids_t);
452 static const char *spotlight_get_qtype_string(uint64_t query_type)
454 switch (query_type) {
457 case SQ_TYPE_COMPLEX:
474 static const char *spotlight_get_cpx_qtype_string(uint64_t cpx_query_type)
476 switch (cpx_query_type) {
477 case SQ_CPX_TYPE_ARRAY:
479 case SQ_CPX_TYPE_STRING:
481 case SQ_CPX_TYPE_UTF16_STRING:
482 return "utf-16 string";
483 case SQ_CPX_TYPE_DICT:
485 case SQ_CPX_TYPE_CNIDS:
487 case SQ_CPX_TYPE_FILEMETA:
494 static int sl_unpack_cpx(DALLOC_CTX *query,
498 uint cpx_query_count,
499 const uint toc_offset,
504 int roffset = offset;
505 uint64_t query_data64;
506 uint unicode_encoding;
509 int qlen, used_in_last_block, slen;
510 sl_array_t *sl_array;
512 sl_filemeta_t *sl_fm;
514 switch (cpx_query_type) {
515 case SQ_CPX_TYPE_ARRAY:
516 sl_array = talloc_zero(query, sl_array_t);
517 EC_NEG1_LOG( roffset = sl_unpack_loop(sl_array, buf, offset, cpx_query_count, toc_offset, encoding) );
518 dalloc_add(query, sl_array, sl_array_t);
521 case SQ_CPX_TYPE_DICT:
522 sl_dict = talloc_zero(query, sl_dict_t);
523 EC_NEG1_LOG( roffset = sl_unpack_loop(sl_dict, buf, offset, cpx_query_count, toc_offset, encoding) );
524 dalloc_add(query, sl_dict, sl_dict_t);
527 case SQ_CPX_TYPE_STRING:
528 case SQ_CPX_TYPE_UTF16_STRING:
529 query_data64 = sl_unpack_uint64(buf, offset, encoding);
530 qlen = (query_data64 & 0xffff) * 8;
531 used_in_last_block = query_data64 >> 32;
532 slen = qlen - 8 + used_in_last_block;
534 if (cpx_query_type == SQ_CPX_TYPE_STRING) {
535 p = talloc_strndup(query, buf + offset + 8, slen);
537 unicode_encoding = spotlight_get_utf16_string_encoding(buf, offset + 8, slen, encoding);
538 mark_exists = (unicode_encoding & SL_ENC_UTF_16);
539 unicode_encoding &= ~SL_ENC_UTF_16;
540 EC_NEG1( convert_string_allocate(CH_UCS2, CH_UTF8, buf + offset + (mark_exists ? 18 : 16), slen, &p) );
543 dalloc_add(query, &p, char *);
547 case SQ_CPX_TYPE_FILEMETA:
548 query_data64 = sl_unpack_uint64(buf, offset, encoding);
549 qlen = (query_data64 & 0xffff) * 8;
551 EC_FAIL_LOG("SQ_CPX_TYPE_FILEMETA: query_length <= 8: %d", qlen);
553 sl_fm = talloc_zero(query, sl_filemeta_t);
554 EC_NEG1_LOG( sl_unpack(sl_fm, buf + offset + 8) );
555 dalloc_add(query, sl_fm, sl_filemeta_t);
560 case SQ_CPX_TYPE_CNIDS:
561 query_data64 = sl_unpack_uint64(buf, offset, encoding);
562 qlen = (query_data64 & 0xffff) * 8;
563 EC_NEG1_LOG( sl_unpack_CNID(query, buf, offset + 8, qlen, encoding) );
577 static int sl_unpack_loop(DALLOC_CTX *query,
581 const uint toc_offset,
585 int i, toc_index, query_length;
587 uint64_t query_data64, query_type;
588 uint cpx_query_type, cpx_query_count;
592 while (count > 0 && (offset < toc_offset)) {
593 query_data64 = sl_unpack_uint64(buf, offset, encoding);
594 query_length = (query_data64 & 0xffff) * 8;
595 query_type = (query_data64 & 0xffff0000) >> 16;
596 if (query_length == 0)
599 switch (query_type) {
600 case SQ_TYPE_COMPLEX:
601 toc_index = (query_data64 >> 32) - 1;
602 query_data64 = sl_unpack_uint64(buf, toc_offset + toc_index * 8, encoding);
603 cpx_query_type = (query_data64 & 0xffff0000) >> 16;
604 cpx_query_count = query_data64 >> 32;
606 EC_NEG1_LOG( offset = sl_unpack_cpx(query, buf, offset + 8, cpx_query_type, cpx_query_count, toc_offset, encoding));
610 subcount = query_data64 >> 32;
614 for (i = 0; i < subcount; i++)
615 dalloc_add(query, &nil, sl_nil_t);
616 offset += query_length;
620 b = query_data64 >> 32;
621 dalloc_add(query, &b, sl_bool_t);
622 offset += query_length;
626 EC_NEG1_LOG( subcount = sl_unpack_ints(query, buf, offset, encoding) );
627 offset += query_length;
631 EC_NEG1_LOG( subcount = sl_unpack_uuid(query, buf, offset, encoding) );
632 offset += query_length;
636 EC_NEG1_LOG( subcount = sl_unpack_floats(query, buf, offset, encoding) );
637 offset += query_length;
641 EC_NEG1_LOG( subcount = sl_unpack_date(query, buf, offset, encoding) );
642 offset += query_length;
657 /**************************************************************************************************
658 * Global functions for packing und unpacking
659 **************************************************************************************************/
661 #define MAX_SLQ_DAT 65000
662 #define MAX_SLQ_TOC 2048
664 int sl_pack(DALLOC_CTX *query, char *buf)
667 char toc_buf[MAX_SLQ_TOC];
671 memcpy(buf, "432130dm", 8);
672 EC_NEG1_LOG( len = sl_pack_loop(query, buf + 16, 0, toc_buf + 8, &toc_index) );
673 SIVAL(buf, 8, len / 8 + 1 + toc_index + 1);
674 SIVAL(buf, 12, len / 8 + 1);
676 SLVAL(toc_buf, 0, sl_pack_tag(SQ_TYPE_TOC, toc_index + 1, 0));
677 memcpy(buf + 16 + len, toc_buf, (toc_index + 1 ) * 8);
679 len += 16 + (toc_index + 1 ) * 8;
687 int sl_unpack(DALLOC_CTX *query, const char *buf)
690 int encoding, i, toc_entries;
691 uint64_t toc_offset, tquerylen, toc_entry;
693 if (strncmp(buf, "md031234", 8) == 0)
694 encoding = SL_ENC_BIG_ENDIAN;
696 encoding = SL_ENC_LITTLE_ENDIAN;
700 toc_offset = ((sl_unpack_uint64(buf, 0, encoding) >> 32) - 1 ) * 8;
701 if (toc_offset < 0 || (toc_offset > 65000)) {
707 toc_entries = (int)(sl_unpack_uint64(buf, toc_offset, encoding) & 0xffff);
709 EC_NEG1( sl_unpack_loop(query, buf, 0, 1, toc_offset + 8, encoding) );