]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/spotlight_marshalling.c
Some refactoring
[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 static int sl_unpack_loop(DALLOC_CTX *query, const char *buf, int offset, uint count, const uint toc_offset, const uint encoding);
68
69 /*
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.
72 */
73 static uint spotlight_get_utf16_string_encoding(const char *buf, int offset, int query_length, uint encoding) {
74     uint utf16_encoding;
75
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);
82         else
83             byte_order_mark = RSVAL(buf, offset);
84
85         if (byte_order_mark == 0xFFFE) {
86             utf16_encoding = SL_ENC_BIG_ENDIAN | SL_ENC_UTF_16;
87         }
88         else if (byte_order_mark == 0xFEFF) {
89             utf16_encoding = SL_ENC_LITTLE_ENDIAN | SL_ENC_UTF_16;
90         }
91     }
92
93     return utf16_encoding;
94 }
95
96 /**************************************************************************************************
97  * marshalling functions
98  **************************************************************************************************/
99
100 #define SL_OFFSET_DELTA 16
101
102 static uint64_t sl_pack_tag(uint16_t type, uint16_t size_or_count, uint32_t val)
103 {
104     uint64_t tag = ((uint64_t)val << 32) | ((uint64_t)type << 16) | size_or_count;
105     return tag;
106 }
107
108 static int sl_pack_float(double d, char *buf, int offset)
109 {
110     union {
111         double d;
112         uint64_t w;
113     } ieee_fp_union;
114
115     SLVAL(buf, offset, sl_pack_tag(SQ_TYPE_FLOAT, 2, 1));
116     SLVAL(buf, offset + 8, ieee_fp_union.w);
117
118     return offset + 2 * sizeof(uint64_t);
119 }
120
121 static int sl_pack_uint64(uint64_t u, char *buf, int offset)
122 {
123     SLVAL(buf, offset, sl_pack_tag(SQ_TYPE_INT64, 2, 1));
124     SLVAL(buf, offset + 8, u);
125
126     return offset + 2 * sizeof(uint64_t);
127 }
128
129 static int sl_pack_bool(sl_bool_t bl, char *buf, int offset)
130 {
131     SLVAL(buf, offset, sl_pack_tag(SQ_TYPE_BOOL, 1, bl ? 1 : 0));
132
133     return offset + sizeof(uint64_t);
134 }
135
136 static int sl_pack_nil(char *buf, int offset)
137 {
138     SLVAL(buf, offset, sl_pack_tag(SQ_TYPE_NULL, 1, 1));
139
140     return offset + sizeof(uint64_t);
141 }
142
143 static int sl_pack_date(sl_time_t t, char *buf, int offset)
144 {
145     uint64_t data = 0;
146
147     data = (t.tv_sec + SPOTLIGHT_TIME_DELTA) << 24;
148
149     SLVAL(buf, offset, sl_pack_tag(SQ_TYPE_DATE, 2, 1));
150     SLVAL(buf, offset + 8, data);
151
152     return offset + 2 * sizeof(uint64_t);
153 }
154
155 static int sl_pack_uuid(sl_uuid_t *uuid, char *buf, int offset)
156 {
157     SLVAL(buf, offset, sl_pack_tag(SQ_TYPE_UUID, 3, 1));
158     memcpy(buf + offset + 8, uuid, 16);
159
160     return offset + sizeof(uint64_t) + 16;
161 }
162
163 static int sl_pack_CNID(sl_cnids_t *cnids, char *buf, int offset, char *toc_buf, int *toc_idx)
164 {
165     int len = 0, off = 0;
166     int cnid_count = talloc_array_length(cnids->ca_cnids);
167
168     SLVAL(toc_buf, *toc_idx * 8, sl_pack_tag(SQ_CPX_TYPE_CNIDS, (offset + SL_OFFSET_DELTA) / 8, cnid_count));
169     SLVAL(buf, offset, sl_pack_tag(SQ_TYPE_COMPLEX, 1, *toc_idx + 1));
170     *toc_idx += 1;
171     offset += 8;
172
173     SLVAL(buf, offset, sl_pack_tag(SQ_TYPE_CNIDS, 2 + cnid_count, 8 /* unknown meaning, but always 8 */));
174     offset += 8;
175
176     if (cnid_count > 0) {
177         SLVAL(buf, offset, sl_pack_tag(0x0add, cnid_count, cnids->ca_context));
178         offset += 8;
179
180         for (int i = 0; i < cnid_count; i++) {
181             SLVAL(buf, offset, cnids->ca_cnids->dd_talloc_array[i]);
182             offset += 8;
183         }
184     }
185     
186     return offset;
187 }
188
189 static int sl_pack_array(sl_array_t *array, char *buf, int offset, char *toc_buf, int *toc_idx)
190 {
191     int count = talloc_array_length(array->dd_talloc_array);
192     int octets = (offset + SL_OFFSET_DELTA) / 8;
193
194     LOG(log_maxdebug, logtype_sl, "sl_pack_array: count: %d, offset:%d, octets: %d", count, offset, octets);
195
196     SLVAL(toc_buf, *toc_idx * 8, sl_pack_tag(SQ_CPX_TYPE_ARRAY, octets, count));
197     SLVAL(buf, offset, sl_pack_tag(SQ_TYPE_COMPLEX, 1, *toc_idx + 1));
198     *toc_idx += 1;
199     offset += 8;
200
201     offset = sl_pack_loop(array, buf, offset, toc_buf, toc_idx);
202
203     return offset;
204 }
205
206 static int sl_pack_dict(sl_array_t *dict, char *buf, int offset, char *toc_buf, int *toc_idx)
207 {
208     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)));
209     SLVAL(buf, offset, sl_pack_tag(SQ_TYPE_COMPLEX, 1, *toc_idx + 1));
210     *toc_idx += 1;
211     offset += 8;
212
213     offset = sl_pack_loop(dict, buf, offset, toc_buf, toc_idx);
214
215     return offset;
216 }
217
218 static int sl_pack_string(char **string, char *buf, int offset, char *toc_buf, int *toc_idx)
219 {
220     int len, octets, used_in_last_octet;
221     char *s = *string;
222     len = strlen(s);
223     octets = (len / 8) + (len & 7 ? 1 : 0);
224     used_in_last_octet = 8 - (octets * 8 - len);
225
226     LOG(log_maxdebug, logtype_sl, "sl_pack_string(\"%s\"): len: %d, octets: %d, used_in_last_octet: %d",
227         s, len, octets, used_in_last_octet);
228
229     SLVAL(toc_buf, *toc_idx * 8, sl_pack_tag(SQ_CPX_TYPE_STRING, (offset + SL_OFFSET_DELTA) / 8, used_in_last_octet));
230     SLVAL(buf, offset, sl_pack_tag(SQ_TYPE_COMPLEX, 1, *toc_idx + 1));
231     *toc_idx += 1;
232     offset += 8;
233
234     SLVAL(buf, offset, sl_pack_tag(SQ_TYPE_DATA, octets + 1, used_in_last_octet));
235     offset += 8;
236
237     memset(buf + offset, 0, octets * 8);
238     strncpy(buf + offset, s, len);
239     offset += octets * 8;
240
241     return offset;
242 }
243
244 static int sl_pack_loop(DALLOC_CTX *query, char *buf, int offset, char *toc_buf, int *toc_idx)
245 {
246     const char *type;
247
248     for (int n = 0; n < talloc_array_length(query->dd_talloc_array); n++) {
249
250         type = talloc_get_name(query->dd_talloc_array[n]);
251
252         if (STRCMP(type, ==, "sl_array_t")) {
253             offset = sl_pack_array(query->dd_talloc_array[n], buf, offset, toc_buf, toc_idx);
254         } else if (STRCMP(type, ==, "sl_dict_t")) {
255             offset = sl_pack_dict(query->dd_talloc_array[n], buf, offset, toc_buf, toc_idx);
256         } else if (STRCMP(type, ==, "uint64_t")) {
257             uint64_t i;
258             memcpy(&i, query->dd_talloc_array[n], sizeof(uint64_t));
259             offset = sl_pack_uint64(i, buf, offset);
260         } else if (STRCMP(type, ==, "char *")) {
261             offset = sl_pack_string(query->dd_talloc_array[n], buf, offset, toc_buf, toc_idx);
262         } else if (STRCMP(type, ==, "sl_bool_t")) {
263             sl_bool_t bl;
264             memcpy(&bl, query->dd_talloc_array[n], sizeof(sl_bool_t));
265             offset = sl_pack_bool(bl, buf, offset);
266         } else if (STRCMP(type, ==, "double")) {
267             double d;
268             memcpy(&d, query->dd_talloc_array[n], sizeof(double));
269             offset = sl_pack_float(d, buf, offset);
270         } else if (STRCMP(type, ==, "sl_nil_t")) {
271             offset = sl_pack_nil(buf, offset);
272         } else if (STRCMP(type, ==, "sl_time_t")) {
273             sl_time_t t;
274             memcpy(&t, query->dd_talloc_array[n], sizeof(sl_time_t));
275             offset = sl_pack_date(t, buf, offset);
276         } else if (STRCMP(type, ==, "sl_uuid_t")) {
277             offset = sl_pack_uuid(query->dd_talloc_array[n], buf, offset);
278         } else if (STRCMP(type, ==, "sl_cnids_t")) {
279             offset = sl_pack_CNID(query->dd_talloc_array[n], buf, offset, toc_buf, toc_idx);
280         }
281     }
282
283     return offset;
284 }
285
286 /**************************************************************************************************
287  * unmarshalling functions
288  **************************************************************************************************/
289
290 static uint64_t sl_unpack_uint64(const char *buf, int offset, uint encoding)
291 {
292     if (encoding == SL_ENC_LITTLE_ENDIAN)
293             return LVAL(buf, offset);
294         else
295             return RLVAL(buf, offset);
296 }
297
298 static int sl_unpack_ints(DALLOC_CTX *query, const char *buf, int offset, uint encoding)
299 {
300     int count, i;
301     uint64_t query_data64;
302
303     query_data64 = sl_unpack_uint64(buf, offset, encoding);
304     count = query_data64 >> 32;
305     offset += 8;
306
307     i = 0;
308     while (i++ < count) {
309         query_data64 = sl_unpack_uint64(buf, offset, encoding);
310         dalloc_add(query, &query_data64, uint64_t);
311         offset += 8;
312     }
313
314     return count;
315 }
316
317 static int sl_unpack_date(DALLOC_CTX *query, const char *buf, int offset, uint encoding)
318 {
319     int count, i;
320     uint64_t query_data64;
321     sl_time_t t;
322
323     query_data64 = sl_unpack_uint64(buf, offset, encoding);
324     count = query_data64 >> 32;
325     offset += 8;
326
327     i = 0;
328     while (i++ < count) {
329         query_data64 = sl_unpack_uint64(buf, offset, encoding) >> 24;
330         t.tv_sec = query_data64 - SPOTLIGHT_TIME_DELTA;
331         t.tv_usec = 0;
332         dalloc_add(query, &t, sl_time_t);
333         offset += 8;
334     }
335
336     return count;
337 }
338
339 static int sl_unpack_uuid(DALLOC_CTX *query, const char *buf, int offset, uint encoding)
340 {
341     int count, i;
342     uint64_t query_data64;
343     sl_uuid_t uuid;
344     query_data64 = sl_unpack_uint64(buf, offset, encoding);
345     count = query_data64 >> 32;
346     offset += 8;
347
348     i = 0;
349     while (i++ < count) {
350         memcpy(uuid.sl_uuid, buf + offset, 16);
351         dalloc_add(query, &uuid, sl_uuid_t);
352         offset += 16;
353     }
354
355     return count;
356 }
357
358 static int sl_unpack_floats(DALLOC_CTX *query, const char *buf, int offset, uint encoding)
359 {
360     int count, i;
361     uint64_t query_data64;
362     double fval;
363     union {
364         double d;
365         uint32_t w[2];
366     } ieee_fp_union;
367
368     query_data64 = sl_unpack_uint64(buf, offset, encoding);
369     count = query_data64 >> 32;
370     offset += 8;
371
372     i = 0;
373     while (i++ < count) {
374         if (encoding == SL_ENC_LITTLE_ENDIAN) {
375 #ifdef WORDS_BIGENDIAN
376             ieee_fp_union.w[0] = IVAL(buf, offset + 4);
377             ieee_fp_union.w[1] = IVAL(buf, offset);
378 #else
379             ieee_fp_union.w[0] = IVAL(buf, offset);
380             ieee_fp_union.w[1] = IVAL(buf, offset + 4);
381 #endif
382         } else {
383 #ifdef WORDS_BIGENDIAN
384             ieee_fp_union.w[0] = RIVAL(buf, offset);
385             ieee_fp_union.w[1] = RIVAL(buf, offset + 4);
386 #else
387             ieee_fp_union.w[0] = RIVAL(buf, offset + 4);
388             ieee_fp_union.w[1] = RIVAL(buf, offset);
389 #endif
390         }
391         dalloc_add(query, &ieee_fp_union.d, double);
392         offset += 8;
393     }
394
395     return count;
396 }
397
398 static int sl_unpack_CNID(DALLOC_CTX *query, const char *buf, int offset, int length, uint encoding)
399 {
400     EC_INIT;
401     int count;
402     uint64_t query_data64;
403     sl_cnids_t cnids;
404
405     EC_NULL( cnids.ca_cnids = talloc_zero(query, DALLOC_CTX) );
406
407     if (length <= 16)
408         /* that's permitted, it's an empty array */
409         goto EC_CLEANUP;
410     
411     query_data64 = sl_unpack_uint64(buf, offset, encoding);
412     count = query_data64 & 0xffff;
413
414     cnids.ca_unkn1 = (query_data64 & 0xffff0000) >> 16;
415     cnids.ca_context = query_data64 >> 32;
416
417     offset += 8;
418
419     while (count --) {
420         query_data64 = sl_unpack_uint64(buf, offset, encoding);
421         dalloc_add(cnids.ca_cnids, &query_data64, uint64_t);
422         offset += 8;
423     }
424
425     dalloc_add(query, &cnids, sl_cnids_t);
426
427 EC_CLEANUP:
428     EC_EXIT;
429 }
430
431 static const char *spotlight_get_qtype_string(uint64_t query_type)
432 {
433     switch (query_type) {
434     case SQ_TYPE_NULL:
435         return "null";
436     case SQ_TYPE_COMPLEX:
437         return "complex";
438     case SQ_TYPE_INT64:
439         return "int64";
440     case SQ_TYPE_BOOL:
441         return "bool";
442     case SQ_TYPE_FLOAT:
443         return "float";
444     case SQ_TYPE_DATA:
445         return "data";
446     case SQ_TYPE_CNIDS:
447         return "CNIDs";
448     default:
449         return "unknown";
450     }
451 }
452
453 static const char *spotlight_get_cpx_qtype_string(uint64_t cpx_query_type)
454 {
455     switch (cpx_query_type) {
456     case SQ_CPX_TYPE_ARRAY:
457         return "array";
458     case SQ_CPX_TYPE_STRING:
459         return "string";
460     case SQ_CPX_TYPE_UTF16_STRING:
461         return "utf-16 string";
462     case SQ_CPX_TYPE_DICT:
463         return "dictionary";
464     case SQ_CPX_TYPE_CNIDS:
465         return "CNIDs";
466     case SQ_CPX_TYPE_FILEMETA:
467         return "FileMeta";
468     default:
469         return "unknown";
470     }
471 }
472
473 static int sl_unpack_cpx(DALLOC_CTX *query,
474                          const char *buf,
475                          const int offset,
476                          uint cpx_query_type,
477                          uint cpx_query_count,
478                          const uint toc_offset,
479                          const uint encoding)
480 {
481     EC_INIT;
482
483     int roffset = offset;
484     uint64_t query_data64;
485     uint unicode_encoding;
486     uint8_t mark_exists;
487     char *p;
488     int qlen, padding, slen;
489     sl_array_t *sl_arrary;
490     sl_dict_t *sl_dict;
491
492     switch (cpx_query_type) {
493     case SQ_CPX_TYPE_ARRAY:
494         sl_arrary = talloc_zero(query, sl_array_t);
495         EC_NEG1_LOG( roffset = sl_unpack_loop(sl_arrary, buf, offset, cpx_query_count, toc_offset, encoding) );
496         dalloc_add(query, sl_arrary, sl_array_t);
497         break;
498
499     case SQ_CPX_TYPE_DICT:
500         sl_dict = talloc_zero(query, sl_dict_t);
501         EC_NEG1_LOG( roffset = sl_unpack_loop(sl_dict, buf, offset, cpx_query_count, toc_offset, encoding) );
502         dalloc_add(query, sl_dict, sl_dict_t);
503         break;
504
505     case SQ_CPX_TYPE_STRING:
506     case SQ_CPX_TYPE_UTF16_STRING:
507         query_data64 = sl_unpack_uint64(buf, offset, encoding);
508         qlen = (query_data64 & 0xffff) * 8;
509         if ((padding = 8 - (query_data64 >> 32)) < 0)
510             EC_FAIL;
511         if ((slen = qlen - 8 - padding) < 1)
512             EC_FAIL;
513
514         if (cpx_query_type == SQ_CPX_TYPE_STRING) {
515             p = talloc_strndup(query, buf + offset + 8, slen);
516         } else {
517             unicode_encoding = spotlight_get_utf16_string_encoding(buf, offset + 8, slen, encoding);
518             mark_exists = (unicode_encoding & SL_ENC_UTF_16);
519             unicode_encoding &= ~SL_ENC_UTF_16;
520             EC_NEG1( convert_string_allocate(CH_UCS2, CH_UTF8, buf + offset + (mark_exists ? 18 : 16), slen, &p) );
521         }
522
523         dalloc_add(query, &p, char *);
524         roffset += qlen;
525         break;
526
527     case SQ_CPX_TYPE_FILEMETA:
528         query_data64 = sl_unpack_uint64(buf, offset, encoding);
529         qlen = (query_data64 & 0xffff) * 8;
530         if (qlen <= 8) {
531             EC_FAIL_LOG("SQ_CPX_TYPE_FILEMETA: query_length <= 8: %d", qlen);
532         } else {
533             EC_NEG1_LOG( sl_unpack(query, buf + offset + 8) );
534         }
535         roffset += qlen;
536         break;
537
538     case SQ_CPX_TYPE_CNIDS:
539         query_data64 = sl_unpack_uint64(buf, offset, encoding);
540         qlen = (query_data64 & 0xffff) * 8;
541         EC_NEG1_LOG( sl_unpack_CNID(query, buf, offset + 8, qlen, encoding) );
542         roffset += qlen;
543         break;
544
545     default:
546         EC_FAIL;
547     }
548             
549 EC_CLEANUP:
550     if (ret != 0)
551         roffset = -1;
552     return roffset;
553 }
554
555 static int sl_unpack_loop(DALLOC_CTX *query,
556                           const char *buf,
557                           int offset,
558                           uint count,
559                           const uint toc_offset,
560                           const uint encoding)
561 {
562     EC_INIT;
563     int i, toc_index, query_length;
564     uint subcount;
565     uint64_t query_data64, query_type;
566     uint cpx_query_type, cpx_query_count;
567     sl_nil_t nil;
568     sl_bool_t b;
569
570     while (count > 0 && (offset < toc_offset)) {
571         query_data64 = sl_unpack_uint64(buf, offset, encoding);
572         query_length = (query_data64 & 0xffff) * 8;
573         query_type = (query_data64 & 0xffff0000) >> 16;
574         if (query_length == 0)
575             EC_FAIL;
576
577         switch (query_type) {
578         case SQ_TYPE_COMPLEX:
579             toc_index = (query_data64 >> 32) - 1;
580             query_data64 = sl_unpack_uint64(buf, toc_offset + toc_index * 8, encoding);
581             cpx_query_type = (query_data64 & 0xffff0000) >> 16;
582             cpx_query_count = query_data64 >> 32;
583
584             EC_NEG1_LOG( offset = sl_unpack_cpx(query, buf, offset + 8, cpx_query_type, cpx_query_count, toc_offset, encoding));
585             count--;
586             break;
587         case SQ_TYPE_NULL:
588             subcount = query_data64 >> 32;
589             if (subcount > 64)
590                 EC_FAIL;
591             nil = 0;
592             for (i = 0; i < subcount; i++)
593                 dalloc_add(query, &nil, sl_nil_t);
594             offset += query_length;
595             count -= subcount;
596             break;
597         case SQ_TYPE_BOOL:
598             b = query_data64 >> 32;
599             dalloc_add(query, &b, sl_bool_t);
600             offset += query_length;
601             count--;
602             break;
603         case SQ_TYPE_INT64:
604             EC_NEG1_LOG( subcount = sl_unpack_ints(query, buf, offset, encoding) );
605             offset += query_length;
606             count -= subcount;
607             break;
608         case SQ_TYPE_UUID:
609             EC_NEG1_LOG( subcount = sl_unpack_uuid(query, buf, offset, encoding) );
610             offset += query_length;
611             count -= subcount;
612             break;
613         case SQ_TYPE_FLOAT:
614             EC_NEG1_LOG( subcount = sl_unpack_floats(query, buf, offset, encoding) );
615             offset += query_length;
616             count -= subcount;
617             break;
618         case SQ_TYPE_DATE:
619             EC_NEG1_LOG( subcount = sl_unpack_date(query, buf, offset, encoding) );
620             offset += query_length;
621             count -= subcount;
622             break;
623         default:
624             EC_FAIL;
625         }
626     }
627
628 EC_CLEANUP:
629     if (ret != 0) {
630         offset = -1;
631     }
632     return offset;
633 }
634
635 /**************************************************************************************************
636  * Global functions for packing und unpacking
637  **************************************************************************************************/
638
639 #define MAX_SLQ_DAT 65000
640 #define MAX_SLQ_TOC 2048
641
642 int sl_pack(DALLOC_CTX *query, char *buf)
643 {
644     EC_INIT;
645     char toc_buf[MAX_SLQ_TOC];
646     int toc_index = 0;
647     int len = 0;
648
649     memcpy(buf, "432130dm", 8);
650     EC_NEG1_LOG( len = sl_pack_loop(query, buf + 16, 0, toc_buf + 8, &toc_index) );
651     SIVAL(buf, 8, len / 8 + 1 + toc_index + 1);
652     SIVAL(buf, 12, len / 8 + 1);
653
654     SLVAL(toc_buf, 0, sl_pack_tag(SQ_TYPE_TOC, toc_index + 1, 0));
655     memcpy(buf + 16 + len, toc_buf, (toc_index + 1 ) * 8);
656
657     len += 16 + (toc_index + 1 ) * 8;
658
659 EC_CLEANUP:
660     if (ret != 0)
661         len = -1;
662     return len;
663 }
664
665 int sl_unpack(DALLOC_CTX *query, const char *buf)
666 {
667     EC_INIT;
668     int encoding, i, toc_entries;
669     uint64_t toc_offset, tquerylen, toc_entry;
670
671     if (strncmp(buf, "md031234", 8) == 0)
672         encoding = SL_ENC_BIG_ENDIAN;
673     else
674         encoding = SL_ENC_LITTLE_ENDIAN;
675
676     buf += 8;
677
678     toc_offset = ((sl_unpack_uint64(buf, 0, encoding) >> 32) - 1 ) * 8;
679     if (toc_offset < 0 || (toc_offset > 65000)) {
680         EC_FAIL;
681     }
682
683     buf += 8;
684
685     toc_entries = (int)(sl_unpack_uint64(buf, toc_offset, encoding) & 0xffff);
686
687     EC_NEG1( sl_unpack_loop(query, buf, 0, 1, toc_offset + 8, encoding) );
688
689 EC_CLEANUP:
690     EC_EXIT;
691 }