xdrpp
RFC4506 XDR compiler and message library
marshal.cc
1 
2 #include <xdrpp/marshal.h>
3 
4 namespace xdr {
5 
6 msg_ptr
7 message_t::alloc(std::size_t size)
8 {
9  // In RPC (see RFC5531 section 11), the high bit means this is the
10  // last record fragment in a record. If the high bit is clear, it
11  // means another fragment follows. We don't currently implement
12  // continuation fragments, and instead always set the last-record
13  // bit to produce a single-fragment record.
14  assert(size < 0x80000000);
15  void *raw = operator new(offsetof(message_t, buf_[size + 4]));
16  if (!raw)
17  throw std::bad_alloc();
18  message_t *m = new (raw) message_t (size);
19  *reinterpret_cast<std::uint32_t *>(m->raw_data()) =
20  swap32le(size32(size) | 0x80000000);
21  return msg_ptr(m);
22 }
23 
24 void
25 marshal_base::get_bytes(const std::uint32_t *&pr, void *buf, std::size_t len)
26 {
27  const char *p = reinterpret_cast<const char *>(pr);
28  std::memcpy(buf, p, len);
29  p += len;
30  while (len & 3) {
31  ++len;
32  if (*p++ != '\0')
33  throw xdr_should_be_zero("Non-zero padding bytes encountered");
34  }
35  pr = reinterpret_cast<const std::uint32_t *>(p);
36 }
37 
38 void
39 marshal_base::put_bytes(std::uint32_t *&pr, const void *buf, std::size_t len)
40 {
41  char *p = reinterpret_cast<char *>(pr);
42  std::memcpy(p, buf, len);
43  p += len;
44  while (len & 3) {
45  ++len;
46  *p++ = '\0';
47  }
48  pr = reinterpret_cast<std::uint32_t *>(p);
49 }
50 
51 }
static void get_bytes(const std::uint32_t *&pr, void *buf, std::size_t len)
Copy len bytes to buf, then consume 0-3 bytes of padding to make the total number of bytes consumed d...
Definition: marshal.cc:25
static msg_ptr alloc(std::size_t size)
Allocate a new buffer.
Definition: marshal.cc:7
Most of the xdrpp library is encapsulated in the xdr namespace.
Definition: arpc.cc:4
Constexpr std::uint32_t swap32le(std::uint32_t v)
Byteswap 32-bit value only on little-endian machines, identity function on big-endian machines...
Definition: endian.h:78
Message buffer, with room at beginning for 4-byte length.
Definition: message.h:24
static void put_bytes(std::uint32_t *&pr, const void *buf, std::size_t len)
Copy len bytes from buf, then add 0-3 zero-valued padding bytes to make the overall marshaled length ...
Definition: marshal.cc:39
Support for marshaling XDR types in the format specified by RFC4506.
Padding bytes that should have contained zero don&#39;t.
Definition: types.h:61
char * raw_data()
4-byte buffer to store size in network byte order, followed by data.
Definition: message.h:41