A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
tag-buffer.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2008 INRIA
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
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  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
19  */
20 #include "tag-buffer.h"
21 #include "ns3/assert.h"
22 #include <string.h>
23 
24 namespace ns3 {
25 
26 #ifndef TAG_BUFFER_USE_INLINE
27 
28 void
29 TagBuffer::WriteU8 (uint8_t v)
30 {
31  NS_ASSERT (m_current + 1 <= m_end);
32  *m_current = v;
33  m_current++;
34 }
35 
36 void
37 TagBuffer::WriteU16 (uint16_t data)
38 {
39  WriteU8 ((data >> 0) & 0xff);
40  WriteU8 ((data >> 8) & 0xff);
41 }
42 void
43 TagBuffer::WriteU32 (uint32_t data)
44 {
45  WriteU8 ((data >> 0) & 0xff);
46  WriteU8 ((data >> 8) & 0xff);
47  WriteU8 ((data >> 16) & 0xff);
48  WriteU8 ((data >> 24) & 0xff);
49 }
50 
51 
52 uint8_t
53 TagBuffer::ReadU8 (void)
54 {
55  NS_ASSERT (m_current + 1 <= m_end);
56  uint8_t v;
57  v = *m_current;
58  m_current++;
59  return v;
60 }
61 
62 uint16_t
63 TagBuffer::ReadU16 (void)
64 {
65  uint8_t byte0 = ReadU8 ();
66  uint8_t byte1 = ReadU8 ();
67  uint16_t data = byte1;
68  data <<= 8;
69  data |= byte0;
70  return data;
71 }
72 uint32_t
73 TagBuffer::ReadU32 (void)
74 {
75  uint8_t byte0 = ReadU8 ();
76  uint8_t byte1 = ReadU8 ();
77  uint8_t byte2 = ReadU8 ();
78  uint8_t byte3 = ReadU8 ();
79  uint32_t data = byte3;
80  data <<= 8;
81  data |= byte2;
82  data <<= 8;
83  data |= byte1;
84  data <<= 8;
85  data |= byte0;
86  return data;
87 }
88 
89 #endif /* TAG_BUFFER_USE_INLINE */
90 
91 
92 void
93 TagBuffer::WriteU64 (uint64_t data)
94 {
95  WriteU8 ((data >> 0) & 0xff);
96  WriteU8 ((data >> 8) & 0xff);
97  WriteU8 ((data >> 16) & 0xff);
98  WriteU8 ((data >> 24) & 0xff);
99  WriteU8 ((data >> 32) & 0xff);
100  WriteU8 ((data >> 40) & 0xff);
101  WriteU8 ((data >> 48) & 0xff);
102  WriteU8 ((data >> 56) & 0xff);
103 }
104 void
106 {
107  uint8_t *buf = (uint8_t *)&v;
108  for (uint32_t i = 0; i < sizeof (double); ++i, ++buf)
109  {
110  WriteU8 (*buf);
111  }
112 }
113 void
114 TagBuffer::Write (const uint8_t *buffer, uint32_t size)
115 {
116  for (uint32_t i = 0; i < size; ++i, ++buffer)
117  {
118  WriteU8 (*buffer);
119  }
120 }
121 uint64_t
123 {
124  uint8_t byte0 = ReadU8 ();
125  uint8_t byte1 = ReadU8 ();
126  uint8_t byte2 = ReadU8 ();
127  uint8_t byte3 = ReadU8 ();
128  uint8_t byte4 = ReadU8 ();
129  uint8_t byte5 = ReadU8 ();
130  uint8_t byte6 = ReadU8 ();
131  uint8_t byte7 = ReadU8 ();
132  uint64_t data = byte7;
133  data <<= 8;
134  data |= byte6;
135  data <<= 8;
136  data |= byte5;
137  data <<= 8;
138  data |= byte4;
139  data <<= 8;
140  data |= byte3;
141  data <<= 8;
142  data |= byte2;
143  data <<= 8;
144  data |= byte1;
145  data <<= 8;
146  data |= byte0;
147 
148  return data;
149 }
150 double
152 {
153  double v;
154  uint8_t *buf = (uint8_t *)&v;
155  for (uint32_t i = 0; i < sizeof (double); ++i, ++buf)
156  {
157  *buf = ReadU8 ();
158  }
159  return v;
160 }
161 void
162 TagBuffer::Read (uint8_t *buffer, uint32_t size)
163 {
164  for (uint32_t i = 0; i < size; ++i, ++buffer)
165  {
166  *buffer = ReadU8 ();
167  }
168 }
169 TagBuffer::TagBuffer (uint8_t *start, uint8_t *end)
170  : m_current (start),
171  m_end (end)
172 {
173 }
174 
175 void
176 TagBuffer::TrimAtEnd (uint32_t trim)
177 {
178  NS_ASSERT (m_current <= (m_end - trim));
179  m_end -= trim;
180 }
181 
182 void
184 {
185  NS_ASSERT (o.m_end >= o.m_current);
187  uintptr_t size = o.m_end - o.m_current;
188  NS_ASSERT (size <= (uintptr_t)(m_end - m_current));
189  memcpy (m_current, o.m_current, size);
190  m_current += size;
191 }
192 
193 } // namespace ns3
194