SUMO - Simulation of Urban MObility
storage.cpp
Go to the documentation of this file.
1 /************************************************************************
2  ** This file is part of the network simulator Shawn. **
3  ** Copyright (C) 2004-2007 by the SwarmNet (www.swarmnet.de) project **
4  ** Shawn is free software; you can redistribute it and/or modify it **
5  ** under the terms of the BSD License. Refer to the shawn-licence.txt **
6  ** file in the root of the Shawn source tree for further details. **
7  ************************************************************************
8  ** **
9  ** \author Axel Wegener <wegener@itm.uni-luebeck.de> **
10  ** \author Bjoern Hendriks <hendriks@ibr.cs.tu-bs.de> **
11  ** **
12  ************************************************************************/
13 
14 #include "storage.h"
15 
16 #ifdef BUILD_TCPIP
17 
18 #include <iostream>
19 #include <iterator>
20 #include <sstream>
21 #include <cassert>
22 #include <algorithm>
23 #include <iomanip>
24 
25 
26 //#define NULLITER static_cast<list<unsigned char>::iterator>(0)
27 
28 namespace tcpip
29 {
30 
31  // ----------------------------------------------------------------------
33  {
34  init();
35  }
36 
37 
38  // ----------------------------------------------------------------------
39  Storage::Storage(const unsigned char packet[], int length)
40  {
41  assert(length >= 0); // fixed MB, 2015-04-21
42 
43  store.reserve(length);
44  // Get the content
45  for(int i = 0; i < length; ++i) store.push_back(packet[i]);
46 
47  init();
48  }
49 
50 
51  // ----------------------------------------------------------------------
52  void Storage::init()
53  {
54  // Initialize local variables
55  iter_ = store.begin();
56 
57  short a = 0x0102;
58  unsigned char *p_a = reinterpret_cast<unsigned char*>(&a);
59  bigEndian_ = (p_a[0] == 0x01); // big endian?
60  }
61 
62 
63  // ----------------------------------------------------------------------
65  {}
66 
67 
68  // ----------------------------------------------------------------------
69  bool Storage::valid_pos()
70  {
71  return (iter_ != store.end()); // this implies !store.empty()
72  }
73 
74 
75  // ----------------------------------------------------------------------
76  unsigned int Storage::position() const
77  {
78  // According to C++ standard std::distance will simply compute the iterators
79  // difference for random access iterators as std::vector provides.
80  return static_cast<unsigned int>(std::distance(store.begin(), iter_));
81  }
82 
83 
84  // ----------------------------------------------------------------------
85  void Storage::reset()
86  {
87  store.clear();
88  iter_ = store.begin();
89  }
90 
91 
92  // ----------------------------------------------------------------------
97  unsigned char Storage::readChar() throw(std::invalid_argument)
98  {
99  if ( !valid_pos() )
100  {
101  throw std::invalid_argument("Storage::readChar(): invalid position");
102  }
103  return readCharUnsafe();
104  }
105 
106 
107  // ----------------------------------------------------------------------
111  void Storage::writeChar(unsigned char value) throw()
112  {
113  store.push_back(value);
114  iter_ = store.begin();
115  }
116 
117 
118  // ----------------------------------------------------------------------
123  int Storage::readByte() throw(std::invalid_argument)
124  {
125  int i = static_cast<int>(readChar());
126  if (i < 128) return i;
127  else return (i - 256);
128  }
129 
130 
131  // ----------------------------------------------------------------------
135  void Storage::writeByte(int value) throw(std::invalid_argument)
136  {
137  if (value < -128 || value > 127)
138  {
139  throw std::invalid_argument("Storage::writeByte(): Invalid value, not in [-128, 127]");
140  }
141  writeChar( static_cast<unsigned char>( (value+256) % 256 ) );
142  }
143 
144 
145  // ----------------------------------------------------------------------
150  int Storage::readUnsignedByte() throw(std::invalid_argument)
151  {
152  return static_cast<int>(readChar());
153  }
154 
155 
156  // ----------------------------------------------------------------------
160  void Storage::writeUnsignedByte(int value) throw(std::invalid_argument)
161  {
162  if (value < 0 || value > 255)
163  {
164  throw std::invalid_argument("Storage::writeUnsignedByte(): Invalid value, not in [0, 255]");
165  }
166  writeChar( static_cast<unsigned char>( value ));
167  }
168 
169 
170  // -----------------------------------------------------------------------
175  std::string Storage::readString() throw(std::invalid_argument)
176  {
177  int len = readInt();
178  checkReadSafe(len);
179  StorageType::const_iterator end = iter_;
180  std::advance(end, len);
181  const std::string tmp(iter_, end);
182  iter_ = end;
183  return tmp;
184  }
185 
186 
187  // ----------------------------------------------------------------------
192  void Storage::writeString(const std::string &s) throw()
193  {
194  writeInt(static_cast<int>(s.length()));
195 
196  store.insert(store.end(), s.begin(), s.end());
197  iter_ = store.begin();
198  }
199 
200 
201  // -----------------------------------------------------------------------
206  std::vector<std::string> Storage::readStringList() throw(std::invalid_argument)
207  {
208  std::vector<std::string> tmp;
209  const int len = readInt();
210  tmp.reserve(len);
211  for (int i = 0; i < len; i++)
212  {
213  tmp.push_back(readString());
214  }
215  return tmp;
216  }
217 
218 
219  // ----------------------------------------------------------------------
224  void Storage::writeStringList(const std::vector<std::string> &s) throw()
225  {
226  writeInt(static_cast<int>(s.size()));
227  for (std::vector<std::string>::const_iterator it = s.begin(); it!=s.end() ; it++)
228  {
229  writeString(*it);
230  }
231  }
232 
233 
234  // ----------------------------------------------------------------------
242  int Storage::readShort() throw(std::invalid_argument)
243  {
244  short value = 0;
245  unsigned char *p_value = reinterpret_cast<unsigned char*>(&value);
246  readByEndianess(p_value, 2);
247  return value;
248  }
249 
250 
251  // ----------------------------------------------------------------------
252  void Storage::writeShort( int value ) throw(std::invalid_argument)
253  {
254  if (value < -32768 || value > 32767)
255  {
256  throw std::invalid_argument("Storage::writeShort(): Invalid value, not in [-32768, 32767]");
257  }
258 
259  short svalue = static_cast<short>(value);
260  unsigned char *p_svalue = reinterpret_cast<unsigned char*>(&svalue);
261  writeByEndianess(p_svalue, 2);
262  }
263 
264 
265  // ----------------------------------------------------------------------
273  int Storage::readInt() throw(std::invalid_argument)
274  {
275  int value = 0;
276  unsigned char *p_value = reinterpret_cast<unsigned char*>(&value);
277  readByEndianess(p_value, 4);
278  return value;
279  }
280 
281 
282  // ----------------------------------------------------------------------
283  void Storage::writeInt( int value ) throw()
284  {
285  unsigned char *p_value = reinterpret_cast<unsigned char*>(&value);
286  writeByEndianess(p_value, 4);
287  }
288 
289 
290  // ----------------------------------------------------------------------
298  float Storage::readFloat() throw(std::invalid_argument)
299  {
300  float value = 0;
301  unsigned char *p_value = reinterpret_cast<unsigned char*>(&value);
302  readByEndianess(p_value, 4);
303  return value;
304  }
305 
306 
307  // ----------------------------------------------------------------------
308  void Storage::writeFloat( float value ) throw()
309  {
310  unsigned char *p_value = reinterpret_cast<unsigned char*>(&value);
311  writeByEndianess(p_value, 4);
312  }
313 
314 
315  // ----------------------------------------------------------------------
316  void Storage::writeDouble( double value ) throw ()
317  {
318  unsigned char *p_value = reinterpret_cast<unsigned char*>(&value);
319  writeByEndianess(p_value, 8);
320  }
321 
322 
323  // ----------------------------------------------------------------------
324  double Storage::readDouble( ) throw (std::invalid_argument)
325  {
326  double value = 0;
327  unsigned char *p_value = reinterpret_cast<unsigned char*>(&value);
328  readByEndianess(p_value, 8);
329  return value;
330  }
331 
332 
333  // ----------------------------------------------------------------------
334  void Storage::writePacket(unsigned char* packet, int length)
335  {
336  store.insert(store.end(), &(packet[0]), &(packet[length]));
337  iter_ = store.begin(); // reserve() invalidates iterators
338  }
339 
340 
341  // ----------------------------------------------------------------------
342  void Storage::writePacket(const std::vector<unsigned char> &packet)
343  {
344  std::copy(packet.begin(), packet.end(), std::back_inserter(store));
345  iter_ = store.begin();
346  }
347 
348 
349  // ----------------------------------------------------------------------
351  {
352  // the compiler cannot deduce to use a const_iterator as source
353  store.insert<StorageType::const_iterator>(store.end(), other.iter_, other.store.end());
354  iter_ = store.begin();
355  }
356 
357 
358  // ----------------------------------------------------------------------
359  void Storage::checkReadSafe(unsigned int num) const throw(std::invalid_argument)
360  {
361  if (std::distance(iter_, store.end()) < static_cast<int>(num))
362  {
363  std::ostringstream msg;
364  msg << "tcpip::Storage::readIsSafe: want to read " << num << " bytes from Storage, "
365  << "but only " << std::distance(iter_, store.end()) << " remaining";
366  throw std::invalid_argument(msg.str());
367  }
368  }
369 
370 
371  // ----------------------------------------------------------------------
372  unsigned char Storage::readCharUnsafe()
373  {
374  char hb = *iter_;
375  ++iter_;
376  return hb;
377  }
378 
379 
380  // ----------------------------------------------------------------------
381  void Storage::writeByEndianess(const unsigned char * begin, unsigned int size)
382  {
383  const unsigned char * end = &(begin[size]);
384  if (bigEndian_)
385  store.insert(store.end(), begin, end);
386  else
387  store.insert(store.end(), std::reverse_iterator<const unsigned char *>(end), std::reverse_iterator<const unsigned char *>(begin));
388  iter_ = store.begin();
389  }
390 
391 
392  // ----------------------------------------------------------------------
393  void Storage::readByEndianess(unsigned char * array, int size)
394  {
395  checkReadSafe(size);
396  if (bigEndian_)
397  {
398  for (int i = 0; i < size; ++i)
399  array[i] = readCharUnsafe();
400  }
401  else
402  {
403  for (int i = size - 1; i >= 0; --i)
404  array[i] = readCharUnsafe();
405  }
406  }
407 
408 
409  // ----------------------------------------------------------------------
410  std::string Storage::hexDump() const
411  {
412  static const int width = 2;
413 
414  std::ostringstream dump;
415  // adapt stream attributes
416  // 'showbase' inserts "0x", 'internal' makes leading '0' appear between "0x" and hex digits
417  dump.setf(std::ostream::hex | std::ostream::showbase | std::ostream::internal);
418  dump.fill('0');
419 
420  for(StorageType::const_iterator it = store.begin(); it != store.end(); ++it)
421  {
422  // insert spaces between values
423  if (it != store.begin())
424  dump << " ";
425  dump << std::setw(width) << static_cast<int>(*it);
426  }
427 
428  return dump.str();
429  }
430 
431 }
432 
433 #endif // BUILD_TCPIP
434 
435 /*-----------------------------------------------------------------------
436  * Source $Source: $
437  * Version $Revision: 620 $
438  * Date $Date: 2011-07-08 17:39:10 +0200 (Fri, 08 Jul 2011) $
439  *-----------------------------------------------------------------------
440  * $Log: $
441  *-----------------------------------------------------------------------*/
virtual ~Storage()
Definition: socket.cpp:61
virtual int readShort()
std::string hexDump() const
Dump storage content as series of hex values.
void init()
Used in constructors to initialize local variables.
StorageType::const_iterator end() const
Definition: storage.h:118
virtual std::vector< std::string > readStringList()
virtual double readDouble()
virtual bool valid_pos()
virtual void writePacket(unsigned char *packet, int length)
virtual void writeUnsignedByte(int)
virtual unsigned char readChar()
StorageType store
Definition: storage.h:44
bool bigEndian_
Definition: storage.h:48
virtual void writeInt(int)
void writeByEndianess(const unsigned char *begin, unsigned int size)
Write size elements of array begin according to endianess.
virtual int readUnsignedByte()
virtual void writeChar(unsigned char)
Storage()
Standard Constructor.
void readByEndianess(unsigned char *array, int size)
Read size elements into array according to endianess.
virtual int readInt()
virtual void writeByte(int)
virtual void writeStringList(const std::vector< std::string > &s)
virtual void writeShort(int)
virtual std::string readString()
virtual unsigned int position() const
virtual void writeStorage(tcpip::Storage &store)
StorageType::size_type size() const
Definition: storage.h:115
virtual void writeFloat(float)
virtual void writeString(const std::string &s)
StorageType::const_iterator iter_
Definition: storage.h:45
unsigned char readCharUnsafe()
Read a byte without validity check.
virtual float readFloat()
StorageType::const_iterator begin() const
Definition: storage.h:117
void checkReadSafe(unsigned int num) const
Check if the next num bytes can be read safely.
virtual void writeDouble(double)
virtual int readByte()