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
11
12#include <clocale>
13#include <cstdlib>
14#include <cstring>
15#include <limits>
16
17
18
19
20using namespace yarp::os::impl;
21using namespace yarp::os;
22
23namespace {
24YARP_OS_LOG_COMPONENT(NETTYPE, "yarp.os.NetType")
25} // namespace
26
27int NetType::netInt(const yarp::os::Bytes& code)
28{
29 yCAssert(NETTYPE, code.length() == sizeof(NetInt32));
30 NetInt32 tmp;
31 memcpy((char*)(&tmp), code.get(), code.length());
32 return tmp;
33}
34
35bool 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/*
48 PNG's nice and simple CRC code
49 (from http://www.w3.org/TR/PNG-CRCAppendix.html)
50*/
51
52/* Table of CRCs of all 8-bit messages. */
53static unsigned long crc_table[256];
54
55/* Flag: has the table been computed? Initially false. */
56static int crc_table_computed = 0;
57
58/* Make the table for a fast CRC. */
59static void make_crc_table()
60{
61 unsigned long c;
62 int n;
63 int k;
64
65 for (n = 0; n < 256; n++) {
66 c = (unsigned long)n;
67 for (k = 0; k < 8; k++) {
68 if ((c & 1) != 0) {
69 c = 0xedb88320L ^ (c >> 1);
70 } else {
71 c = c >> 1;
72 }
73 }
74 crc_table[n] = c;
75 }
77}
78
79/* Update a running CRC with the bytes buf[0..len-1]--the CRC
80 should be initialized to all 1's, and the transmitted value
81 is the 1's complement of the final running CRC (see the
82 crc() routine below)). */
83
84static unsigned long update_crc(unsigned long crc, unsigned char* buf, size_t len)
85{
86
87 unsigned long c = crc;
88 size_t n;
89
90 if (crc_table_computed == 0) {
92 }
93 for (n = 0; n < len; n++) {
94 c = crc_table[(c ^ buf[n]) & 0xff] ^ (c >> 8);
95 }
96 return c;
97}
98
99/* Return the CRC of the bytes buf[0..len-1]. */
100unsigned long NetType::getCrc(char* buf, size_t len)
101{
102 return update_crc(0xffffffffL, (unsigned char*)buf, len) ^ 0xffffffffL;
103}
static int crc_table_computed
Definition: NetType.cpp:56
static unsigned long crc_table[256]
Definition: NetType.cpp:53
static unsigned long update_crc(unsigned long crc, unsigned char *buf, size_t len)
Definition: NetType.cpp:84
static void make_crc_table()
Definition: NetType.cpp:59
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 unsigned long int getCrc(char *buf, size_t len)
Definition: NetType.cpp:100
#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
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