From 084e9cc3557aacb6bd6cf2476bad9295badc64a8 Mon Sep 17 00:00:00 2001 From: Frank Lahm Date: Mon, 2 Jul 2012 17:13:33 +0200 Subject: [PATCH] Some refactoring --- etc/afpd/spotlight.c | 46 ++++---- etc/afpd/spotlight_marshalling.c | 183 ++++++++++++++++++------------- 2 files changed, 127 insertions(+), 102 deletions(-) diff --git a/etc/afpd/spotlight.c b/etc/afpd/spotlight.c index e56aca63..2d9d6315 100644 --- a/etc/afpd/spotlight.c +++ b/etc/afpd/spotlight.c @@ -66,14 +66,6 @@ static int dd_dump(DALLOC_CTX *dd, int nestinglevel) uint64_t i; memcpy(&i, dd->dd_talloc_array[n], sizeof(uint64_t)); LOG(log_debug, logtype_sl, "%suint64_t: 0x%04x", neststrings[nestinglevel + 1], i); - } else if (STRCMP(type, ==, "int64_t")) { - int64_t i; - memcpy(&i, dd->dd_talloc_array[n], sizeof(int64_t)); - LOG(log_debug, logtype_sl, "%sint64_t: %" PRId64, neststrings[nestinglevel + 1], i); - } else if (STRCMP(type, ==, "uint32_t")) { - uint32_t i; - memcpy(&i, dd->dd_talloc_array[n], sizeof(uint32_t)); - LOG(log_debug, logtype_sl, "%s%s: %" PRIu32, neststrings[nestinglevel + 1], type, i); } else if (STRCMP(type, ==, "char *")) { char *s; memcpy(&s, dd->dd_talloc_array[n], sizeof(char *)); @@ -98,7 +90,7 @@ static int dd_dump(DALLOC_CTX *dd, int nestinglevel) * Spotlight RPC functions **************************************************************************************************/ -static int sl_rpc_fetchPropertiesForContext(AFPObj *obj, const DALLOC_CTX *query, DALLOC_CTX *reply, const struct vol *v) +static int sl_rpc_fetchPropertiesForContext(const AFPObj *obj, const DALLOC_CTX *query, DALLOC_CTX *reply, const struct vol *v) { EC_INIT; @@ -149,6 +141,13 @@ EC_CLEANUP: EC_EXIT; } +static int sl_rpc_openQuery(const AFPObj *obj, const DALLOC_CTX *query, DALLOC_CTX *reply, const struct vol *v) +{ + EC_INIT; +EC_CLEANUP: + EC_EXIT; +} + /************************************************************************************************** * AFP functions **************************************************************************************************/ @@ -214,6 +213,8 @@ int afp_spotlight_rpc(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_ if (STRCMP(*cmd, ==, "fetchPropertiesForContext:")) { EC_ZERO_LOG( sl_rpc_fetchPropertiesForContext(obj, query, reply, vol) ); + } else if (STRCMP(*cmd, ==, "openQueryWithParams:forContext:")) { + EC_ZERO_LOG( sl_rpc_openQuery(obj, query, reply, vol) ); } else if (STRCMP(*cmd, ==, "fetchQueryResultsForContext:")) { uint64_t *p; if ((p = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "uint64_t", 1)) != NULL) { @@ -258,7 +259,7 @@ int main(int argc, char **argv) EC_INIT; TALLOC_CTX *mem_ctx = talloc_new(NULL); DALLOC_CTX *dd = talloc_zero(mem_ctx, DALLOC_CTX); - int64_t i; + uint64_t i; set_processname("spot"); setuplog("default:info,spotlight:debug", "/dev/tty"); @@ -267,10 +268,10 @@ int main(int argc, char **argv) #if 0 i = 2; - dalloc_add(dd, &i, int64_t); + dalloc_add(dd, &i, uint64_t); i = 1; - dalloc_add(dd, &i, int64_t); + dalloc_add(dd, &i, uint64_t); char *str = talloc_strdup(dd, "hello world"); @@ -286,11 +287,11 @@ int main(int argc, char **argv) /* add a nested array */ DALLOC_CTX *nested = talloc_zero(dd, DALLOC_CTX); i = 3; - dalloc_add(nested, &i, int64_t); + dalloc_add(nested, &i, uint64_t); dalloc_add(dd, nested, DALLOC_CTX); /* test an allocated CNID array */ - uint32_t id = 16; + uint64_t id = 16; sl_cnids_t *cnids = talloc_zero(dd, sl_cnids_t); cnids->ca_cnids = talloc_zero(cnids, DALLOC_CTX); @@ -298,26 +299,24 @@ int main(int argc, char **argv) cnids->ca_unkn1 = 1; cnids->ca_unkn2 = 2; - dalloc_add(cnids->ca_cnids, &id, uint32_t); + dalloc_add(cnids->ca_cnids, &id, uint64_t); dalloc_add(dd, cnids, sl_cnids_t); #endif /* Now the Spotlight types */ sl_array_t *sl_arrary = talloc_zero(dd, sl_array_t); - i = 1234; - dalloc_add(sl_arrary, &i, int64_t); + i = 0x1234; + dalloc_add(sl_arrary, &i, uint64_t); sl_dict_t *sl_dict = talloc_zero(dd, sl_dict_t); - i = 5678; - dalloc_add(sl_dict, &i, int64_t); + i = 0x5678; + dalloc_add(sl_dict, &i, uint64_t); dalloc_add(sl_arrary, sl_dict, sl_dict_t); dalloc_add(dd, sl_arrary, sl_array_t); dd_dump(dd, 0); - -#if 0 /* now parse a real spotlight packet */ char ibuf[8192]; char rbuf[8192]; @@ -327,14 +326,15 @@ int main(int argc, char **argv) EC_NULL( query = talloc_zero(mem_ctx, DALLOC_CTX) ); - EC_NEG1_LOG( fd = open("spotlight-packet.bin", O_RDONLY) ); + EC_NEG1_LOG( fd = open("openQuery-packet.bin", O_RDONLY) ); EC_NEG1_LOG( len = read(fd, ibuf, 8192) ); close(fd); - EC_NEG1_LOG( dissect_spotlight(query, ibuf + 24) ); + EC_NEG1_LOG( sl_unpack(query, ibuf + 24) ); /* Now dump the whole thing */ dd_dump(query, 0); +#if 0 int qlen; char buf[MAX_SLQ_DAT]; EC_NEG1_LOG( qlen = sl_pack(query, buf) ); diff --git a/etc/afpd/spotlight_marshalling.c b/etc/afpd/spotlight_marshalling.c index 47586a58..d338b590 100644 --- a/etc/afpd/spotlight_marshalling.c +++ b/etc/afpd/spotlight_marshalling.c @@ -64,6 +64,7 @@ /* Forward declarations */ static int sl_pack_loop(DALLOC_CTX *query, char *buf, int offset, char *toc_buf, int *toc_idx); +static int sl_unpack_loop(DALLOC_CTX *query, const char *buf, int offset, uint count, const uint toc_offset, const uint encoding); /* * Returns the UTF-16 string encoding, by checking the 2-byte byte order mark. @@ -469,21 +470,102 @@ static const char *spotlight_get_cpx_qtype_string(uint64_t cpx_query_type) } } -static int spotlight_dissect_loop(DALLOC_CTX *query, - const char *buf, - uint offset, - uint count, - const uint toc_offset, - const uint encoding) +static int sl_unpack_cpx(DALLOC_CTX *query, + const char *buf, + const int offset, + uint cpx_query_type, + uint cpx_query_count, + const uint toc_offset, + const uint encoding) { EC_INIT; - int i, toc_index, query_length; - uint subcount, cpx_query_type, cpx_query_count; - uint64_t query_data64, query_type; + + int roffset = offset; + uint64_t query_data64; uint unicode_encoding; uint8_t mark_exists; char *p; - int padding, slen; + int qlen, padding, slen; + sl_array_t *sl_arrary; + sl_dict_t *sl_dict; + + switch (cpx_query_type) { + case SQ_CPX_TYPE_ARRAY: + sl_arrary = talloc_zero(query, sl_array_t); + EC_NEG1_LOG( roffset = sl_unpack_loop(sl_arrary, buf, offset, cpx_query_count, toc_offset, encoding) ); + dalloc_add(query, sl_arrary, sl_array_t); + break; + + case SQ_CPX_TYPE_DICT: + sl_dict = talloc_zero(query, sl_dict_t); + EC_NEG1_LOG( roffset = sl_unpack_loop(sl_dict, buf, offset, cpx_query_count, toc_offset, encoding) ); + dalloc_add(query, sl_dict, sl_dict_t); + break; + + case SQ_CPX_TYPE_STRING: + case SQ_CPX_TYPE_UTF16_STRING: + query_data64 = sl_unpack_uint64(buf, offset, encoding); + qlen = (query_data64 & 0xffff) * 8; + if ((padding = 8 - (query_data64 >> 32)) < 0) + EC_FAIL; + if ((slen = qlen - 8 - padding) < 1) + EC_FAIL; + + if (cpx_query_type == SQ_CPX_TYPE_STRING) { + p = talloc_strndup(query, buf + offset + 8, slen); + } else { + unicode_encoding = spotlight_get_utf16_string_encoding(buf, offset + 8, 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 *); + roffset += qlen; + break; + + case SQ_CPX_TYPE_FILEMETA: + query_data64 = sl_unpack_uint64(buf, offset, encoding); + qlen = (query_data64 & 0xffff) * 8; + if (qlen <= 8) { + EC_FAIL_LOG("SQ_CPX_TYPE_FILEMETA: query_length <= 8: %d", qlen); + } else { + EC_NEG1_LOG( sl_unpack(query, buf + offset + 8) ); + } + roffset += qlen; + break; + + case SQ_CPX_TYPE_CNIDS: + query_data64 = sl_unpack_uint64(buf, offset, encoding); + qlen = (query_data64 & 0xffff) * 8; + EC_NEG1_LOG( sl_unpack_CNID(query, buf, offset + 8, qlen, encoding) ); + roffset += qlen; + break; + + default: + EC_FAIL; + } + +EC_CLEANUP: + if (ret != 0) + roffset = -1; + return roffset; +} + +static int sl_unpack_loop(DALLOC_CTX *query, + const char *buf, + int offset, + uint count, + const uint toc_offset, + const uint encoding) +{ + EC_INIT; + int i, toc_index, query_length; + uint subcount; + uint64_t query_data64, query_type; + uint cpx_query_type, cpx_query_count; + sl_nil_t nil; + sl_bool_t b; while (count > 0 && (offset < toc_offset)) { query_data64 = sl_unpack_uint64(buf, offset, encoding); @@ -499,105 +581,48 @@ static int spotlight_dissect_loop(DALLOC_CTX *query, 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( sl_unpack(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) */ - + EC_NEG1_LOG( offset = sl_unpack_cpx(query, buf, offset + 8, cpx_query_type, cpx_query_count, toc_offset, encoding)); count--; break; - - case SQ_TYPE_NULL: { + case SQ_TYPE_NULL: subcount = query_data64 >> 32; if (subcount > 64) EC_FAIL; - sl_nil_t nil = 0; + nil = 0; for (i = 0; i < subcount; i++) dalloc_add(query, &nil, sl_nil_t); + offset += query_length; count -= subcount; break; - } - case SQ_TYPE_BOOL: { - sl_bool_t b = query_data64 >> 32; + case SQ_TYPE_BOOL: + b = query_data64 >> 32; dalloc_add(query, &b, sl_bool_t); + offset += query_length; count--; break; - } case SQ_TYPE_INT64: EC_NEG1_LOG( subcount = sl_unpack_ints(query, buf, offset, encoding) ); + offset += query_length; count -= subcount; break; case SQ_TYPE_UUID: EC_NEG1_LOG( subcount = sl_unpack_uuid(query, buf, offset, encoding) ); + offset += query_length; count -= subcount; break; case SQ_TYPE_FLOAT: EC_NEG1_LOG( subcount = sl_unpack_floats(query, buf, offset, encoding) ); + offset += query_length; count -= subcount; break; case SQ_TYPE_DATE: EC_NEG1_LOG( subcount = sl_unpack_date(query, buf, offset, encoding) ); + offset += query_length; count -= subcount; break; default: EC_FAIL; } - - offset += query_length; } EC_CLEANUP: @@ -659,7 +684,7 @@ int sl_unpack(DALLOC_CTX *query, const char *buf) toc_entries = (int)(sl_unpack_uint64(buf, toc_offset, encoding) & 0xffff); - EC_NEG1( spotlight_dissect_loop(query, buf, 0, 1, toc_offset + 8, encoding) ); + EC_NEG1( sl_unpack_loop(query, buf, 0, 1, toc_offset + 8, encoding) ); EC_CLEANUP: EC_EXIT; -- 2.39.2