add Htolsb and Lsbtoh write/read methods
authorMathieu Lacage <mathieu.lacage@sophia.inria.fr>
Fri, 28 Mar 2008 18:45:10 -0700
changeset 2792 2854c5a3795b
parent 2791 a2ab6517516e
child 2793 10a571d9a9f7
add Htolsb and Lsbtoh write/read methods
src/common/buffer.cc
src/common/buffer.h
--- a/src/common/buffer.cc	Fri Mar 28 17:35:50 2008 -0700
+++ b/src/common/buffer.cc	Fri Mar 28 18:45:10 2008 -0700
@@ -762,6 +762,33 @@
   WriteU8 (data & 0xff);
 }
 void 
+Buffer::Iterator::WriteHtolsbU16 (uint16_t data)
+{
+  WriteU8 ((data >> 0) & 0xff);
+  WriteU8 ((data >> 8) & 0xff);
+}
+void 
+Buffer::Iterator::WriteHtolsbU32 (uint32_t data)
+{
+  WriteU8 ((data >> 0) & 0xff);
+  WriteU8 ((data >> 8) & 0xff);
+  WriteU8 ((data >> 16) & 0xff);
+  WriteU8 ((data >> 24) & 0xff);
+}
+void 
+Buffer::Iterator::WriteHtolsbU64 (uint64_t data)
+{
+  WriteU8 ((data >> 0) & 0xff);
+  WriteU8 ((data >> 8) & 0xff);
+  WriteU8 ((data >> 16) & 0xff);
+  WriteU8 ((data >> 24) & 0xff);
+  WriteU8 ((data >> 32) & 0xff);
+  WriteU8 ((data >> 40) & 0xff);
+  WriteU8 ((data >> 48) & 0xff);
+  WriteU8 ((data >> 54) & 0xff);
+}
+
+void 
 Buffer::Iterator::WriteHtonU16 (uint16_t data)
 {
   WriteU8 ((data >> 8) & 0xff);
@@ -895,6 +922,61 @@
   retval |= ReadU8 ();
   return retval;
 }
+uint16_t 
+Buffer::Iterator::ReadLsbtohU16 (void)
+{
+  uint8_t byte0 = ReadU8 ();
+  uint8_t byte1 = ReadU8 ();
+  uint16_t data = byte1;
+  data <<= 8;
+  data |= byte0;
+  return data;
+}
+uint32_t 
+Buffer::Iterator::ReadLsbtohU32 (void)
+{
+  uint8_t byte0 = ReadU8 ();
+  uint8_t byte1 = ReadU8 ();
+  uint8_t byte2 = ReadU8 ();
+  uint8_t byte3 = ReadU8 ();
+  uint32_t data = byte3;
+  data <<= 8;
+  data |= byte2;
+  data <<= 8;
+  data |= byte1;
+  data <<= 8;
+  data |= byte0;
+  return data;
+}
+uint64_t 
+Buffer::Iterator::ReadLsbtohU64 (void)
+{
+  uint8_t byte0 = ReadU8 ();
+  uint8_t byte1 = ReadU8 ();
+  uint8_t byte2 = ReadU8 ();
+  uint8_t byte3 = ReadU8 ();
+  uint8_t byte4 = ReadU8 ();
+  uint8_t byte5 = ReadU8 ();
+  uint8_t byte6 = ReadU8 ();
+  uint8_t byte7 = ReadU8 ();
+  uint32_t data = byte7;
+  data <<= 8;
+  data |= byte6;
+  data <<= 8;
+  data |= byte5;
+  data <<= 8;
+  data |= byte4;
+  data <<= 8;
+  data |= byte3;
+  data <<= 8;
+  data |= byte2;
+  data <<= 8;
+  data |= byte1;
+  data <<= 8;
+  data |= byte0;
+
+  return data;
+}
 void 
 Buffer::Iterator::Read (uint8_t *buffer, uint32_t size)
 {
--- a/src/common/buffer.h	Fri Mar 28 17:35:50 2008 -0700
+++ b/src/common/buffer.h	Fri Mar 28 18:45:10 2008 -0700
@@ -91,12 +91,14 @@
  *
  * A simple state invariant is that m_start <= m_zeroStart <= m_zeroEnd <= m_end
  */
-class Buffer {
+class Buffer 
+{
 public:
   /**
    * \brief iterator in a Buffer instance
    */
-  class Iterator {
+  class Iterator 
+  {
   public:
       Iterator ();
       /**
@@ -188,6 +190,30 @@
        * by two bytes. The data is written in network order and the
        * input data is expected to be in host order.
        */
+      void WriteHtolsbU16 (uint16_t data);
+      /**
+       * \param data data to write in buffer
+       *
+       * Write the data in buffer and avance the iterator position
+       * by four bytes. The data is written in least significant byte order and the
+       * input data is expected to be in host order.
+       */
+      void WriteHtolsbU32 (uint32_t data);
+      /**
+       * \param data data to write in buffer
+       *
+       * Write the data in buffer and avance the iterator position
+       * by eight bytes. The data is written in least significant byte order and the
+       * input data is expected to be in host order.
+       */
+      void WriteHtolsbU64 (uint64_t data);
+      /**
+       * \param data data to write in buffer
+       *
+       * Write the data in buffer and avance the iterator position
+       * by two bytes. The data is written in least significant byte order and the
+       * input data is expected to be in host order.
+       */
       void WriteHtonU16 (uint16_t data);
       /**
        * \param data data to write in buffer
@@ -282,6 +308,30 @@
        */
       uint64_t ReadNtohU64 (void);
       /**
+       * \return the two bytes read in the buffer.
+       *
+       * Read data and advance the Iterator by the number of bytes
+       * read.
+       * The data is read in network format and return in host format.
+       */
+      uint16_t ReadLsbtohU16 (void);
+      /**
+       * \return the four bytes read in the buffer.
+       *
+       * Read data and advance the Iterator by the number of bytes
+       * read.
+       * The data is read in network format and return in host format.
+       */
+      uint32_t ReadLsbtohU32 (void);
+      /**
+       * \return the eight bytes read in the buffer.
+       *
+       * Read data and advance the Iterator by the number of bytes
+       * read.
+       * The data is read in network format and return in host format.
+       */
+      uint64_t ReadLsbtohU64 (void);
+      /**
        * \param buffer buffer to copy data into
        * \param size number of bytes to copy
        *