serialize.h 3.06 KB
Newer Older
1
2
3
4
5
6
7
#ifdef HAVE_CONFIG_H
#  include "config.h"
#endif

#ifndef SERIALIZE_H
#define SERIALIZE_H

8
9
10
#include <string.h>

#include "cdi.h"
11
#ifndef  CDI_CKSUM_H_
12
#include "cdi_cksum.h"
13
#endif
14
#ifndef  ERROR_H
15
#include "error.h"
16
#endif
17

18
19
20
21
/*
 * Generic interfaces for (de-)marshalling
 */
int serializeGetSize(int count, int datatype, void *context);
22
void serializePack(const void *data, int count, int datatype,
23
                   void *buf, int buf_size, int *position, void *context);
24
void serializeUnpack(const void *buf, int buf_size, int *position,
25
26
27
                     void *data, int count, int datatype, void *context);

/*
28
 * (de-)marshalling function for common data structures
29
 */
30
31
32
33
34
35
36
37
38
39
static inline int
serializeStrTabGetPackSize(const char **strTab, int numStr,
                           void *context)
{
  xassert(numStr >= 0);
  int packBuffSize = 0;
  for (size_t i = 0; i < (size_t)numStr; ++i)
  {
    size_t len = strlen(strTab[i]);
    packBuffSize +=
40
41
      serializeGetSize(1, CDI_DATATYPE_INT, context)
      + serializeGetSize((int)len, CDI_DATATYPE_TXT, context);
42
43
  }
  packBuffSize +=
44
    serializeGetSize(1, CDI_DATATYPE_UINT32, context);
45
46
  return packBuffSize;
}
47

48
49
50
51
52
53
54
55
56
static inline void
serializeStrTabPack(const char **strTab, int numStr,
                    void *buf, int buf_size, int *position, void *context)
{
  uint32_t d = 0;
  xassert(numStr >= 0);
  for (size_t i = 0; i < (size_t)numStr; ++i)
  {
    int len = (int)strlen(strTab[i]);
57
    serializePack(&len, 1, CDI_DATATYPE_INT,
58
                  buf, buf_size, position, context);
59
    serializePack(strTab[i], len, CDI_DATATYPE_TXT,
60
                  buf, buf_size, position, context);
61
    d ^= cdiCheckSum(CDI_DATATYPE_TXT, len, strTab[i]);
62
  }
63
  serializePack(&d, 1, CDI_DATATYPE_UINT32,
64
65
                buf, buf_size, position, context);
}
66

67
68
69
70
71
72
73
74
75
76
static inline void
serializeStrTabUnpack(const void *buf, int buf_size, int *position,
                      char **strTab, int numStr, void *context)
{
  uint32_t d, d2 = 0;
  xassert(numStr >= 0);
  for (size_t i = 0; i < (size_t)numStr; ++i)
    {
      int len;
      serializeUnpack(buf, buf_size, position,
77
                      &len, 1, CDI_DATATYPE_INT, context);
78
      serializeUnpack(buf, buf_size, position,
79
                      strTab[i], len, CDI_DATATYPE_TXT, context);
80
      strTab[i][len] = '\0';
81
      d2 ^= cdiCheckSum(CDI_DATATYPE_TXT, len, strTab[i]);
82
83
    }
  serializeUnpack(buf, buf_size, position,
84
                  &d, 1, CDI_DATATYPE_UINT32, context);
85
86
  xassert(d == d2);
}
87
88
89
90
91

/*
 * Interfaces for marshalling within a single memory domain
 */
int serializeGetSizeInCore(int count, int datatype, void *context);
92
void serializePackInCore(const void *data, int count, int datatype,
93
                          void *buf, int buf_size, int *position, void *context);
94
95
void serializeUnpackInCore(const void *buf, int buf_size, int *position,
                           void *data, int count, int datatype, void *context);
96
97

#endif
98
99
100
101
102
103
104
105
106
/*
 * Local Variables:
 * c-file-style: "Java"
 * c-basic-offset: 2
 * indent-tabs-mode: nil
 * show-trailing-whitespace: t
 * require-trailing-newline: t
 * End:
 */