]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/spotlight_marshalling.c
Move marshalling functions in own file
[netatalk.git] / etc / afpd / spotlight_marshalling.c
1 /*
2   Copyright (c) 2012 Frank Lahm <franklahm@gmail.com>
3
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.
8
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.
13 */
14
15 #ifdef HAVE_CONFIG_H
16 #include "config.h"
17 #endif /* HAVE_CONFIG_H */
18
19 #include <string.h>
20 #include <strings.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <errno.h>
24 #include <stdbool.h>
25 #include <inttypes.h>
26
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>
35
36 #include "spotlight.h"
37
38 /**************************************************************************************************
39  * RPC data marshalling and unmarshalling
40  **************************************************************************************************/
41
42 /* Spotlight epoch is UNIX epoch minus SPOTLIGHT_TIME_DELTA */
43 #define SPOTLIGHT_TIME_DELTA INT64_C(280878921600U)
44
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
55
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
62
63 #define SUBQ_SAFETY_LIM 20
64
65 /* Forward declarations */
66 static int sl_pack_loop(DALLOC_CTX *query, char *buf, int offset, char *toc_buf, int *toc_idx);
67
68 /*
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.
71 */
72 static uint spotlight_get_utf16_string_encoding(const char *buf, int offset, int query_length, uint encoding) {
73     uint utf16_encoding;
74
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);
81         else
82             byte_order_mark = RSVAL(buf, offset);
83
84         if (byte_order_mark == 0xFFFE) {
85             utf16_encoding = SL_ENC_BIG_ENDIAN | SL_ENC_UTF_16;
86         }
87         else if (byte_order_mark == 0xFEFF) {
88             utf16_encoding = SL_ENC_LITTLE_ENDIAN | SL_ENC_UTF_16;
89         }
90     }
91
92     return utf16_encoding;
93 }
94
95 /**************************************************************************************************
96  * marshalling functions
97  **************************************************************************************************/
98
99 #define SL_OFFSET_DELTA 16
100
101 static uint64_t sl_pack_tag(uint16_t type, uint16_t size_or_count, uint32_t val)
102 {
103     uint64_t tag = ((uint64_t)val << 32) | ((uint64_t)type << 16) | size_or_count;
104     return tag;
105 }
106
107 static int sl_pack_float(double d, char *buf, int offset)
108 {
109     union {
110         double d;
111         uint64_t w;
112     } ieee_fp_union;
113
114     SLVAL(buf, offset, sl_pack_tag(SQ_TYPE_FLOAT, 2, 1));
115     SLVAL(buf, offset + 8, ieee_fp_union.w);
116
117     return offset + 2 * sizeof(uint64_t);
118 }
119
120 static int sl_pack_uint64(uint64_t u, char *buf, int offset)
121 {
122     SLVAL(buf, offset, sl_pack_tag(SQ_TYPE_INT64, 2, 1));
123     SLVAL(buf, offset + 8, u);
124
125     return offset + 2 * sizeof(uint64_t);
126 }
127
128 static int sl_pack_bool(sl_bool_t bl, char *buf, int offset)
129 {
130     SLVAL(buf, offset, sl_pack_tag(SQ_TYPE_BOOL, 1, bl ? 1 : 0));
131
132     return offset + sizeof(uint64_t);
133 }
134
135 static int sl_pack_nil(char *buf, int offset)
136 {
137     SLVAL(buf, offset, sl_pack_tag(SQ_TYPE_NULL, 1, 1));
138
139     return offset + sizeof(uint64_t);
140 }
141
142 static int sl_pack_date(sl_time_t t, char *buf, int offset)
143 {
144     uint64_t data = 0;
145
146     data = (t.tv_sec + SPOTLIGHT_TIME_DELTA) << 24;
147
148     SLVAL(buf, offset, sl_pack_tag(SQ_TYPE_DATE, 2, 1));
149     SLVAL(buf, offset + 8, data);
150
151     return offset + 2 * sizeof(uint64_t);
152 }
153
154 static int sl_pack_uuid(sl_uuid_t *uuid, char *buf, int offset)
155 {
156     SLVAL(buf, offset, sl_pack_tag(SQ_TYPE_UUID, 3, 1));
157     memcpy(buf + offset + 8, uuid, 16);
158
159     return offset + sizeof(uint64_t) + 16;
160 }
161
162 static int sl_pack_CNID(sl_cnids_t *cnids, char *buf, int offset, char *toc_buf, int *toc_idx)
163 {
164     int len = 0, off = 0;
165     int cnid_count = talloc_array_length(cnids->ca_cnids);
166
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));
169     *toc_idx += 1;
170     offset += 8;
171
172     SLVAL(buf, offset, sl_pack_tag(SQ_TYPE_CNIDS, 2 + cnid_count, 8 /* unknown meaning, but always 8 */));
173     offset += 8;
174
175     if (cnid_count > 0) {
176         SLVAL(buf, offset, sl_pack_tag(0x0add, cnid_count, cnids->ca_context));
177         offset += 8;
178
179         for (int i = 0; i < cnid_count; i++) {
180             SLVAL(buf, offset, cnids->ca_cnids->dd_talloc_array[i]);
181             offset += 8;
182         }
183     }
184     
185     return offset;
186 }
187
188 static int sl_pack_array(sl_array_t *array, char *buf, int offset, char *toc_buf, int *toc_idx)
189 {
190     int count = talloc_array_length(array->dd_talloc_array);
191     int octets = (offset + SL_OFFSET_DELTA) / 8;
192
193     LOG(log_maxdebug, logtype_sl, "sl_pack_array: count: %d, offset:%d, octets: %d", count, offset, octets);
194
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));
197     *toc_idx += 1;
198     offset += 8;
199
200     offset = sl_pack_loop(array, buf, offset, toc_buf, toc_idx);
201
202     return offset;
203 }
204
205 static int sl_pack_dict(sl_array_t *dict, char *buf, int offset, char *toc_buf, int *toc_idx)
206 {
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));
209     *toc_idx += 1;
210     offset += 8;
211
212     offset = sl_pack_loop(dict, buf, offset, toc_buf, toc_idx);
213
214     return offset;
215 }
216
217 static int sl_pack_string(char **string, char *buf, int offset, char *toc_buf, int *toc_idx)
218 {
219     int len, octets, used_in_last_octet;
220     char *s = *string;
221     len = strlen(s);
222     octets = (len / 8) + (len & 7 ? 1 : 0);
223     used_in_last_octet = 8 - (octets * 8 - len);
224
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);
227
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));
230     *toc_idx += 1;
231     offset += 8;
232
233     SLVAL(buf, offset, sl_pack_tag(SQ_TYPE_DATA, octets + 1, used_in_last_octet));
234     offset += 8;
235
236     memset(buf + offset, 0, octets * 8);
237     strncpy(buf + offset, s, len);
238     offset += octets * 8;
239
240     return offset;
241 }
242
243 static int sl_pack_loop(DALLOC_CTX *query, char *buf, int offset, char *toc_buf, int *toc_idx)
244 {
245     const char *type;
246
247     for (int n = 0; n < talloc_array_length(query->dd_talloc_array); n++) {
248
249         type = talloc_get_name(query->dd_talloc_array[n]);
250
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")) {
256             uint64_t i;
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")) {
262             sl_bool_t bl;
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")) {
266             double d;
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")) {
272             sl_time_t 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);
279         }
280     }
281
282     return offset;
283 }
284
285 /**************************************************************************************************
286  * unmarshalling functions
287  **************************************************************************************************/
288
289 static uint64_t sl_unpack_uint64(const char *buf, int offset, uint encoding)
290 {
291     if (encoding == SL_ENC_LITTLE_ENDIAN)
292             return LVAL(buf, offset);
293         else
294             return RLVAL(buf, offset);
295 }
296
297 static int sl_unpack_ints(DALLOC_CTX *query, const char *buf, int offset, uint encoding)
298 {
299     int count, i;
300     uint64_t query_data64;
301
302     query_data64 = sl_unpack_uint64(buf, offset, encoding);
303     count = query_data64 >> 32;
304     offset += 8;
305
306     i = 0;
307     while (i++ < count) {
308         query_data64 = sl_unpack_uint64(buf, offset, encoding);
309         dalloc_add(query, &query_data64, uint64_t);
310         offset += 8;
311     }
312
313     return count;
314 }
315
316 static int sl_unpack_date(DALLOC_CTX *query, const char *buf, int offset, uint encoding)
317 {
318     int count, i;
319     uint64_t query_data64;
320     sl_time_t t;
321
322     query_data64 = sl_unpack_uint64(buf, offset, encoding);
323     count = query_data64 >> 32;
324     offset += 8;
325
326     i = 0;
327     while (i++ < count) {
328         query_data64 = sl_unpack_uint64(buf, offset, encoding) >> 24;
329         t.tv_sec = query_data64 - SPOTLIGHT_TIME_DELTA;
330         t.tv_usec = 0;
331         dalloc_add(query, &t, sl_time_t);
332         offset += 8;
333     }
334
335     return count;
336 }
337
338 static int sl_unpack_uuid(DALLOC_CTX *query, const char *buf, int offset, uint encoding)
339 {
340     int count, i;
341     uint64_t query_data64;
342     sl_uuid_t uuid;
343     query_data64 = sl_unpack_uint64(buf, offset, encoding);
344     count = query_data64 >> 32;
345     offset += 8;
346
347     i = 0;
348     while (i++ < count) {
349         memcpy(uuid.sl_uuid, buf + offset, 16);
350         dalloc_add(query, &uuid, sl_uuid_t);
351         offset += 16;
352     }
353
354     return count;
355 }
356
357 static int sl_unpack_floats(DALLOC_CTX *query, const char *buf, int offset, uint encoding)
358 {
359     int count, i;
360     uint64_t query_data64;
361     double fval;
362     union {
363         double d;
364         uint32_t w[2];
365     } ieee_fp_union;
366
367     query_data64 = sl_unpack_uint64(buf, offset, encoding);
368     count = query_data64 >> 32;
369     offset += 8;
370
371     i = 0;
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);
377 #else
378             ieee_fp_union.w[0] = IVAL(buf, offset);
379             ieee_fp_union.w[1] = IVAL(buf, offset + 4);
380 #endif
381         } else {
382 #ifdef WORDS_BIGENDIAN
383             ieee_fp_union.w[0] = RIVAL(buf, offset);
384             ieee_fp_union.w[1] = RIVAL(buf, offset + 4);
385 #else
386             ieee_fp_union.w[0] = RIVAL(buf, offset + 4);
387             ieee_fp_union.w[1] = RIVAL(buf, offset);
388 #endif
389         }
390         dalloc_add(query, &ieee_fp_union.d, double);
391         offset += 8;
392     }
393
394     return count;
395 }
396
397 static int sl_unpack_CNID(DALLOC_CTX *query, const char *buf, int offset, int length, uint encoding)
398 {
399     EC_INIT;
400     int count;
401     uint64_t query_data64;
402     sl_cnids_t cnids;
403
404     EC_NULL( cnids.ca_cnids = talloc_zero(query, DALLOC_CTX) );
405
406     if (length <= 16)
407         /* that's permitted, it's an empty array */
408         goto EC_CLEANUP;
409     
410     query_data64 = sl_unpack_uint64(buf, offset, encoding);
411     count = query_data64 & 0xffff;
412
413     cnids.ca_unkn1 = (query_data64 & 0xffff0000) >> 16;
414     cnids.ca_context = query_data64 >> 32;
415
416     offset += 8;
417
418     while (count --) {
419         query_data64 = sl_unpack_uint64(buf, offset, encoding);
420         dalloc_add(cnids.ca_cnids, &query_data64, uint64_t);
421         offset += 8;
422     }
423
424     dalloc_add(query, &cnids, sl_cnids_t);
425
426 EC_CLEANUP:
427     EC_EXIT;
428 }
429
430 static const char *spotlight_get_qtype_string(uint64_t query_type)
431 {
432     switch (query_type) {
433     case SQ_TYPE_NULL:
434         return "null";
435     case SQ_TYPE_COMPLEX:
436         return "complex";
437     case SQ_TYPE_INT64:
438         return "int64";
439     case SQ_TYPE_BOOL:
440         return "bool";
441     case SQ_TYPE_FLOAT:
442         return "float";
443     case SQ_TYPE_DATA:
444         return "data";
445     case SQ_TYPE_CNIDS:
446         return "CNIDs";
447     default:
448         return "unknown";
449     }
450 }
451
452 static const char *spotlight_get_cpx_qtype_string(uint64_t cpx_query_type)
453 {
454     switch (cpx_query_type) {
455     case SQ_CPX_TYPE_ARRAY:
456         return "array";
457     case SQ_CPX_TYPE_STRING:
458         return "string";
459     case SQ_CPX_TYPE_UTF16_STRING:
460         return "utf-16 string";
461     case SQ_CPX_TYPE_DICT:
462         return "dictionary";
463     case SQ_CPX_TYPE_CNIDS:
464         return "CNIDs";
465     case SQ_CPX_TYPE_FILEMETA:
466         return "FileMeta";
467     default:
468         return "unknown";
469     }
470 }
471
472 static int spotlight_dissect_loop(DALLOC_CTX *query,
473                                   const char *buf,
474                                   uint offset,
475                                   uint count,
476                                   const uint toc_offset,
477                                   const uint encoding)
478 {
479     EC_INIT;
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;
484     uint8_t mark_exists;
485     char *p;
486     int padding, slen;
487
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)
493             EC_FAIL;
494
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;
501
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);
507                 break;
508             }
509
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);
514                 break;
515             }
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)
520                     EC_FAIL;
521                 if ((slen = query_length - 16 - padding) < 1)
522                     EC_FAIL;
523                 p = talloc_strndup(query, buf + offset + 16, slen);
524                 dalloc_add(query, &p, char *);
525                 break;
526
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)
531                     EC_FAIL;
532                 if ((slen = query_length - 16 - padding) < 1)
533                     EC_FAIL;
534
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;
538
539                 EC_NEG1( convert_string_allocate(CH_UCS2, CH_UTF8, buf + offset + (mark_exists ? 18 : 16), slen, &p) );
540                 dalloc_add(query, &p, char *);
541                 break;
542
543             case SQ_CPX_TYPE_FILEMETA:
544                 query_data64 = sl_unpack_uint64(buf, offset + 8, encoding);
545                 query_length += (query_data64 & 0xffff) * 8;
546
547                 if (query_length <= 8) {
548                     EC_FAIL_LOG("SQ_CPX_TYPE_FILEMETA: query_length <= 8%s", "");
549                 } else {
550                     EC_NEG1_LOG( sl_unpack(query, buf + offset + 16) );
551                 }
552                 break;
553
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) );
558                 break;
559             } /* switch (cpx_query_type) */
560
561             count--;
562             break;
563
564         case SQ_TYPE_NULL: {
565             subcount = query_data64 >> 32;
566             if (subcount > 64)
567                 EC_FAIL;
568             sl_nil_t nil = 0;
569             for (i = 0; i < subcount; i++)
570                 dalloc_add(query, &nil, sl_nil_t);
571             count -= subcount;
572             break;
573         }
574         case SQ_TYPE_BOOL: {
575             sl_bool_t b = query_data64 >> 32;
576             dalloc_add(query, &b, sl_bool_t);
577             count--;
578             break;
579         }
580         case SQ_TYPE_INT64:
581             EC_NEG1_LOG( subcount = sl_unpack_ints(query, buf, offset, encoding) );
582             count -= subcount;
583             break;
584         case SQ_TYPE_UUID:
585             EC_NEG1_LOG( subcount = sl_unpack_uuid(query, buf, offset, encoding) );
586             count -= subcount;
587             break;
588         case SQ_TYPE_FLOAT:
589             EC_NEG1_LOG( subcount = sl_unpack_floats(query, buf, offset, encoding) );
590             count -= subcount;
591             break;
592         case SQ_TYPE_DATE:
593             EC_NEG1_LOG( subcount = sl_unpack_date(query, buf, offset, encoding) );
594             count -= subcount;
595             break;
596         default:
597             EC_FAIL;
598         }
599
600         offset += query_length;
601     }
602
603 EC_CLEANUP:
604     if (ret != 0) {
605         offset = -1;
606     }
607     return offset;
608 }
609
610 /**************************************************************************************************
611  * Global functions for packing und unpacking
612  **************************************************************************************************/
613
614 #define MAX_SLQ_DAT 65000
615 #define MAX_SLQ_TOC 2048
616
617 int sl_pack(DALLOC_CTX *query, char *buf)
618 {
619     EC_INIT;
620     char toc_buf[MAX_SLQ_TOC];
621     int toc_index = 0;
622     int len = 0;
623
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);
628
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);
631
632     len += 16 + (toc_index + 1 ) * 8;
633
634 EC_CLEANUP:
635     if (ret != 0)
636         len = -1;
637     return len;
638 }
639
640 int sl_unpack(DALLOC_CTX *query, const char *buf)
641 {
642     EC_INIT;
643     int encoding, i, toc_entries;
644     uint64_t toc_offset, tquerylen, toc_entry;
645
646     if (strncmp(buf, "md031234", 8) == 0)
647         encoding = SL_ENC_BIG_ENDIAN;
648     else
649         encoding = SL_ENC_LITTLE_ENDIAN;
650
651     buf += 8;
652
653     toc_offset = ((sl_unpack_uint64(buf, 0, encoding) >> 32) - 1 ) * 8;
654     if (toc_offset < 0 || (toc_offset > 65000)) {
655         EC_FAIL;
656     }
657
658     buf += 8;
659
660     toc_entries = (int)(sl_unpack_uint64(buf, toc_offset, encoding) & 0xffff);
661
662     EC_NEG1( spotlight_dissect_loop(query, buf, 0, 1, toc_offset + 8, encoding) );
663
664 EC_CLEANUP:
665     EC_EXIT;
666 }