- int i, toc_index, query_length;
- uint subcount, cpx_query_type, cpx_query_count;
- uint64_t query_data64, query_type;
- uint unicode_encoding;
- uint8_t mark_exists;
- char *p;
- int padding, slen;
-
- while (count > 0 && (offset < toc_offset)) {
- query_data64 = sl_unpack_uint64(buf, offset, encoding);
- query_length = (query_data64 & 0xffff) * 8;
- query_type = (query_data64 & 0xffff0000) >> 16;
- if (query_length == 0)
- EC_FAIL;
-
- switch (query_type) {
- case SQ_TYPE_COMPLEX:
- toc_index = (query_data64 >> 32) - 1;
- query_data64 = sl_unpack_uint64(buf, toc_offset + toc_index * 8, encoding);
- cpx_query_type = (query_data64 & 0xffff0000) >> 16;
- cpx_query_count = query_data64 >> 32;
-
- switch (cpx_query_type) {
- case SQ_CPX_TYPE_ARRAY: {
- sl_array_t *sl_arrary = talloc_zero(query, sl_array_t);
- EC_NEG1_LOG( offset = spotlight_dissect_loop(sl_arrary, buf, offset + 8, cpx_query_count, toc_offset, encoding) );
- dalloc_add(query, sl_arrary, sl_array_t);
- break;
- }
-
- case SQ_CPX_TYPE_DICT: {
- sl_dict_t *sl_dict = talloc_zero(query, sl_dict_t);
- EC_NEG1_LOG( offset = spotlight_dissect_loop(sl_dict, buf, offset + 8, cpx_query_count, toc_offset, encoding) );
- dalloc_add(query, sl_dict, sl_dict_t);
- break;
- }
- case SQ_CPX_TYPE_STRING:
- query_data64 = sl_unpack_uint64(buf, offset + 8, encoding);
- query_length += (query_data64 & 0xffff) * 8;
- if ((padding = 8 - (query_data64 >> 32)) < 0)
- EC_FAIL;
- if ((slen = query_length - 16 - padding) < 1)
- EC_FAIL;
- p = talloc_strndup(query, buf + offset + 16, slen);
- dalloc_add(query, &p, char *);
- break;
-
- case SQ_CPX_TYPE_UTF16_STRING:
- query_data64 = sl_unpack_uint64(buf, offset + 8, encoding);
- query_length += (query_data64 & 0xffff) * 8;
- if ((padding = 8 - (query_data64 >> 32)) < 0)
- EC_FAIL;
- if ((slen = query_length - 16 - padding) < 1)
- EC_FAIL;
-
- unicode_encoding = spotlight_get_utf16_string_encoding(buf, offset + 16, slen, encoding);
- mark_exists = (unicode_encoding & SL_ENC_UTF_16);
- unicode_encoding &= ~SL_ENC_UTF_16;
-
- EC_NEG1( convert_string_allocate(CH_UCS2, CH_UTF8, buf + offset + (mark_exists ? 18 : 16), slen, &p) );
- dalloc_add(query, &p, char *);
- break;
-
- case SQ_CPX_TYPE_FILEMETA:
- query_data64 = sl_unpack_uint64(buf, offset + 8, encoding);
- query_length += (query_data64 & 0xffff) * 8;
-
- if (query_length <= 8) {
- EC_FAIL_LOG("SQ_CPX_TYPE_FILEMETA: query_length <= 8%s", "");
- } else {
- EC_NEG1_LOG( dissect_spotlight(query, buf + offset + 16) );
- }
- break;
-
- case SQ_CPX_TYPE_CNIDS:
- query_data64 = sl_unpack_uint64(buf, offset + 8, encoding);
- query_length += (query_data64 & 0xffff) * 8;
- EC_NEG1_LOG( sl_unpack_CNID(query, buf, offset + 16, query_length, encoding) );
- break;
- } /* switch (cpx_query_type) */
-
- count--;
- break;
-
- case SQ_TYPE_NULL: {
- subcount = query_data64 >> 32;
- if (subcount > 64)
- EC_FAIL;
- sl_nil_t nil = 0;
- for (i = 0; i < subcount; i++)
- dalloc_add(query, &nil, sl_nil_t);
- count -= subcount;
- break;
- }
- case SQ_TYPE_BOOL: {
- sl_bool_t b = query_data64 >> 32;
- dalloc_add(query, &b, sl_bool_t);
- count--;
- break;
- }
- case SQ_TYPE_INT64:
- EC_NEG1_LOG( subcount = sl_unpack_ints(query, buf, offset, encoding) );
- count -= subcount;
- break;
- case SQ_TYPE_UUID:
- EC_NEG1_LOG( subcount = sl_unpack_uuid(query, buf, offset, encoding) );
- count -= subcount;
- break;
- case SQ_TYPE_FLOAT:
- EC_NEG1_LOG( subcount = sl_unpack_floats(query, buf, offset, encoding) );
- count -= subcount;
- break;
- case SQ_TYPE_DATE:
- EC_NEG1_LOG( subcount = sl_unpack_date(query, buf, offset, encoding) );
- count -= subcount;
- break;
- default:
- EC_FAIL;
- }
-
- offset += query_length;
- }
+ slq_t *slq = NULL;
+ uint64_t *uint64, ctx1, ctx2;
+ sl_array_t *array;
+
+ /* Context */
+ EC_NULL_LOG (uint64 = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "uint64_t", 1) );
+ ctx1 = *uint64;
+ EC_NULL_LOG (uint64 = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "uint64_t", 2) );
+ ctx2 = *uint64;
+
+ /* Get query for context and free it */
+ EC_NULL_LOG( slq = slq_for_ctx(ctx1, ctx2) );
+ if (slq->slq_state != SLQ_STATE_DONE)
+ LOG(log_warning, logtype_sl, "Closing active query");
+ sl_module_export->sl_mod_end_search(slq);
+ slq_remove(slq);
+ talloc_free(slq);
+ slq = NULL;
+
+ array = talloc_zero(reply, sl_array_t);
+ uint64_t sl_res = 0;
+ dalloc_add_copy(array, &sl_res, uint64_t);
+ dalloc_add(reply, array, sl_array_t);