YARP
Yet Another Robot Platform
NetType.cpp
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
3  * SPDX-FileCopyrightText: 2006-2010 RobotCub Consortium
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #include <yarp/os/NetType.h>
8 
9 #include <yarp/os/ManagedBytes.h>
11 
12 #include <clocale>
13 #include <cstdlib>
14 #include <cstring>
15 #include <limits>
16 
17 
18 
19 
20 using namespace yarp::os::impl;
21 using namespace yarp::os;
22 
23 namespace {
24 YARP_OS_LOG_COMPONENT(NETTYPE, "yarp.os.NetType")
25 } // namespace
26 
28 {
29  yCAssert(NETTYPE, code.length() == sizeof(NetInt32));
30  NetInt32 tmp;
31  memcpy((char*)(&tmp), code.get(), code.length());
32  return tmp;
33 }
34 
35 bool NetType::netInt(int data, yarp::os::Bytes& code)
36 {
37  NetInt32 i = data;
38  yarp::os::Bytes b((char*)(&i), sizeof(i));
39  if (code.length() != sizeof(i)) {
40  yCError(NETTYPE, "not enough room for integer");
41  return false;
42  }
43  memcpy(code.get(), b.get(), code.length());
44  return true;
45 }
46 
47 #ifndef YARP_NO_DEPRECATED // Since YARP 3.5
48 std::string NetType::toHexString(int x)
49 {
51 }
52 
53 std::string NetType::toHexString(long x)
54 {
56 }
57 
58 std::string NetType::toHexString(unsigned int x)
59 {
61 }
62 
63 std::string NetType::toString(int x)
64 {
66 }
67 
68 std::string NetType::toString(long x)
69 {
71 }
72 
73 std::string NetType::toString(unsigned int x)
74 {
76 }
77 
78 
79 int NetType::toInt(const std::string& x)
80 {
81  return yarp::conf::numeric::from_string<int>(x);
82 }
83 
84 
86 {
88 }
89 
91 {
93 }
94 
96 {
97  return yarp::conf::numeric::from_string<yarp::conf::float32_t>(s);
98 }
99 
101 {
102  return yarp::conf::numeric::from_string<yarp::conf::float64_t>(s);
103 }
104 
106 {
107  return yarp::conf::numeric::from_string<yarp::conf::float32_t>(s);
108 }
109 
111 {
112  return yarp::conf::numeric::from_string<yarp::conf::float64_t>(s);
113 }
114 #endif
115 
116 /*
117  PNG's nice and simple CRC code
118  (from http://www.w3.org/TR/PNG-CRCAppendix.html)
119 */
120 
121 /* Table of CRCs of all 8-bit messages. */
122 static unsigned long crc_table[256];
123 
124 /* Flag: has the table been computed? Initially false. */
125 static int crc_table_computed = 0;
126 
127 /* Make the table for a fast CRC. */
128 static void make_crc_table()
129 {
130  unsigned long c;
131  int n;
132  int k;
133 
134  for (n = 0; n < 256; n++) {
135  c = (unsigned long)n;
136  for (k = 0; k < 8; k++) {
137  if ((c & 1) != 0) {
138  c = 0xedb88320L ^ (c >> 1);
139  } else {
140  c = c >> 1;
141  }
142  }
143  crc_table[n] = c;
144  }
145  crc_table_computed = 1;
146 }
147 
148 /* Update a running CRC with the bytes buf[0..len-1]--the CRC
149  should be initialized to all 1's, and the transmitted value
150  is the 1's complement of the final running CRC (see the
151  crc() routine below)). */
152 
153 static unsigned long update_crc(unsigned long crc, unsigned char* buf, size_t len)
154 {
155 
156  unsigned long c = crc;
157  size_t n;
158 
159  if (crc_table_computed == 0) {
160  make_crc_table();
161  }
162  for (n = 0; n < len; n++) {
163  c = crc_table[(c ^ buf[n]) & 0xff] ^ (c >> 8);
164  }
165  return c;
166 }
167 
168 /* Return the CRC of the bytes buf[0..len-1]. */
169 unsigned long NetType::getCrc(char* buf, size_t len)
170 {
171  return update_crc(0xffffffffL, (unsigned char*)buf, len) ^ 0xffffffffL;
172 }
static int crc_table_computed
Definition: NetType.cpp:125
static unsigned long crc_table[256]
Definition: NetType.cpp:122
static unsigned long update_crc(unsigned long crc, unsigned char *buf, size_t len)
Definition: NetType.cpp:153
static void make_crc_table()
Definition: NetType.cpp:128
A simple abstraction for a block of bytes.
Definition: Bytes.h:24
size_t length() const
Definition: Bytes.cpp:22
const char * get() const
Definition: Bytes.cpp:27
static int netInt(const yarp::os::Bytes &code)
Definition: NetType.cpp:27
static std::string toString(int x)
Definition: NetType.cpp:63
static yarp::conf::float64_t toFloat64(const std::string &s)
Definition: NetType.cpp:100
static std::string toHexString(int x)
Definition: NetType.cpp:48
static unsigned long int getCrc(char *buf, size_t len)
Definition: NetType.cpp:169
static int toInt(const std::string &x)
Definition: NetType.cpp:79
static yarp::conf::float32_t toFloat32(const std::string &s)
Definition: NetType.cpp:95
#define yCError(component,...)
Definition: LogComponent.h:213
#define yCAssert(component, x)
Definition: LogComponent.h:240
#define YARP_OS_LOG_COMPONENT(name, name_string)
Definition: LogComponent.h:29
std::string to_hex_string(IntegerType i)
Definition: numeric.h:321
std::string to_string(IntegerType x)
Definition: numeric.h:115
double float64_t
Definition: numeric.h:77
float float32_t
Definition: numeric.h:76
The components from which ports and connections are built.
An interface to the operating system, including Port based communication.
std::int32_t NetInt32
Definition of the NetInt32 type.
Definition: NetInt32.h:29