Marshaller.cpp 4.12 KB
Newer Older
1
2
3
#include "castor/tape/aggregator/Marshaller.hpp"

#include <arpa/inet.h>
4
5
#include <errno.h>
#include <iostream>
6
7
8
#include <string.h>


9
10
11
//------------------------------------------------------------------------------
// marshallUint32
//------------------------------------------------------------------------------
12
13
void castor::tape::aggregator::Marshaller::marshallUint32(uint32_t src,
  char * &dst) throw (castor::exception::Exception) {
14

15
16
  if(dst == NULL) {
    castor::exception::Exception ex(EINVAL);
17

18
19
20
    ex.getMessage() << "Pointer to destination buffer is NULL";
    throw ex;
  }
21

22
23
24
25
26
  src = htonl(src);

  memcpy(dst, &src, sizeof(src));

  dst += sizeof(src);
27
28
29
}


30
//------------------------------------------------------------------------------
31
// unmarshallInt32
32
//------------------------------------------------------------------------------
33
34
35
36
37
void castor::tape::aggregator::Marshaller::unmarshallUint32(char * &src,
  size_t &srcLen, uint32_t &dst) throw(castor::exception::Exception) {

  if(src == NULL) {
    castor::exception::Exception ex(EINVAL);
38

39
40
41
    ex.getMessage() << "Pointer to source buffer is NULL";
    throw ex;
  }
42

43
44
45
46
47
48
49
50
51
52
53
54
55
  if(srcLen < sizeof(dst)) {
    castor::exception::Exception ex(EINVAL);

    ex.getMessage() << "Source buffer length is too small: Expected length: "
      << sizeof(dst) << ": Actual length: " << srcLen;
    throw ex;
  }

  memcpy(&dst, src, sizeof(dst));
  dst = ntohl(dst);

  src    += sizeof(dst);
  srcLen -= sizeof(dst);
56
57
58
}


59
60
61
//------------------------------------------------------------------------------
// marshallString
//------------------------------------------------------------------------------
62
63
64
65
66
67
68
69
70
void castor::tape::aggregator::Marshaller::marshallString(const char * src,
  char * &dst) throw(castor::exception::Exception) {

  if(dst == NULL) {
    castor::exception::Exception ex(EINVAL);

    ex.getMessage() << "Pointer to destination buffer is NULL";
    throw ex;
  }
71

72
  strcpy(dst, src);
73

74
  dst += strlen(src) + 1;
75
76
77
}


78
//------------------------------------------------------------------------------
79
// marshallString
80
//------------------------------------------------------------------------------
81
82
83
84
85
void castor::tape::aggregator::Marshaller::marshallString(
  const std::string &src, char * &dst) throw(castor::exception::Exception) {

  if(dst == NULL) {
    castor::exception::Exception ex(EINVAL);
86

87
88
89
    ex.getMessage() << "Pointer to destination buffer is NULL";
    throw ex;
  }
90

91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
  strcpy(dst, src.c_str());

  dst += strlen(src.c_str()) + 1;
}


//------------------------------------------------------------------------------
// unmarshallString
//------------------------------------------------------------------------------
void castor::tape::aggregator::Marshaller::unmarshallString(char * &src,
  size_t &srcLen, char *dst, const size_t dstLen)
  throw (castor::exception::Exception) {

  if(src == NULL) {
    castor::exception::Exception ex(EINVAL);

    ex.getMessage() << "Pointer to source buffer is NULL";
    throw ex;
  }

  if(srcLen == 0) {
    castor::exception::Exception ex(EINVAL);

    ex.getMessage() << "Source buffer length is 0";
    throw ex;
  }

  if(dst == NULL) {
    castor::exception::Exception ex(EINVAL);

    ex.getMessage() << "Pointer to destination buffer is NULL";
    throw ex;
  }

  if(dstLen == 0) {
    castor::exception::Exception ex(EINVAL);

    ex.getMessage() << "Destination buffer length is 0";
    throw ex;
  }

  // Calculate the maximum number of bytes that could be unmarshalled
  size_t maxlen = 0;
  if(srcLen < dstLen) {
    maxlen = srcLen;
  } else {
    maxlen = dstLen;
  }

  bool strTerminatorReached = false;

  // While there are potential bytes to copy and the string terminator has not
  // been reached
  for(size_t i=0; i<maxlen && !strTerminatorReached; i++) {
    // If the string terminator has been reached
    if((*dst++ = *src++) == '\0') {
      strTerminatorReached = true;
    }

    srcLen--;
  }

  // If all potential bytes were copied but the string terminator was not
  // reached
  if(!strTerminatorReached) {
    castor::exception::Exception ex(EINVAL);

    ex.getMessage() << "String terminator of source buffer was not reached";
    throw ex;
  }
161
}