summaryrefslogtreecommitdiff
path: root/src/api/cifs_pc_to_net.h
blob: 2587bd2e7824a7c8bde23f6f22abf0e36289fabd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
//------------------------------------------------------------------------------
// Author: Andrey Kuznetsov
// Description: Converters to LE for CIFS v2 constants.
// Copyright (c) 2015 EPAM Systems
//------------------------------------------------------------------------------
/*
    This file is part of Nfstrace.

    Nfstrace is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, version 2 of the License.

    Nfstrace is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Nfstrace.  If not, see <http://www.gnu.org/licenses/>.
*/
//------------------------------------------------------------------------------
#ifndef PC_TO_NET_H
#define PC_TO_NET_H
//------------------------------------------------------------------------------
#include <cstdint>
//------------------------------------------------------------------------------
#if defined(__linux__) || defined(__GNU__)
#include <endian.h>
#define NFSTRACE_BYTE_ORDER __BYTE_ORDER
#define NFSTRACE_BIG_ENDIAN __BIG_ENDIAN
#define NFSTRACE_LITTLE_ENDIAN __LITTLE_ENDIAN
#else
#include <sys/param.h>
#include <machine/endian.h>
#define NFSTRACE_BYTE_ORDER BYTE_ORDER
#define NFSTRACE_BIG_ENDIAN BIG_ENDIAN
#define NFSTRACE_LITTLE_ENDIAN LITTLE_ENDIAN
#endif
//------------------------------------------------------------------------------
namespace NST
{
namespace API
{
namespace SMBv2
{

# if NFSTRACE_BYTE_ORDER == NFSTRACE_BIG_ENDIAN

// TODO: rename this function template to LEconstant2host

template<class T>
constexpr T pc_to_net(T t)
{
    static_assert(t == T{}, "try to not use pc_to_net w/o specialization");
    return t;
}

template<>
constexpr std::uint64_t pc_to_net(std::uint64_t t)
{
    return ((t & 0xFF00000000000000) >> 56)
         | ((t & 0x00FF000000000000) >> 40)
         | ((t & 0x0000FF0000000000) >> 24)
         | ((t & 0x000000FF00000000) >> 8)
         | ((t & 0x00000000FF000000) << 8)
         | ((t & 0x0000000000FF0000) << 24)
         | ((t & 0x000000000000FF00) << 40)
         | ((t & 0x00000000000000FF) << 56);
}

template<>
constexpr uint32_t pc_to_net(uint32_t t)
{
    return ((t & 0xFF000000) >> 24)
         | ((t & 0x00FF0000) >> 8)
         | ((t & 0x0000FF00) << 8)
         | ((t & 0x000000FF) << 24);
}

template<>
constexpr uint16_t pc_to_net(uint16_t t)
{
    return ((t & 0xFF00) >> 8)
         | ((t & 0x00FF) << 8);
}

template<>
constexpr std::uint8_t pc_to_net(std::uint8_t v)
{
    return v;
}

template<>
constexpr std::int64_t pc_to_net(std::int64_t v)
{
    return pc_to_net((std::uint64_t)v);
}

template<>
constexpr std::int32_t pc_to_net(std::int32_t v)
{
    return pc_to_net((std::uint32_t)v);
}

template<>
constexpr std::int16_t pc_to_net(std::int16_t v)
{
    return pc_to_net((std::uint16_t)v);
}

template<>
constexpr std::int8_t pc_to_net(std::int8_t v)
{
    return pc_to_net((std::uint8_t)v);
}

# else
#  if NFSTRACE_BYTE_ORDER == NFSTRACE_LITTLE_ENDIAN

/*!
 * Does nothing for Intel
 */
template<class T>
constexpr T pc_to_net(T t)
{
    return t;
}

#  endif
#endif

} // SMBv2
} // API
} // NST
//------------------------------------------------------------------------------
#endif//PC_TO_NET_H
//------------------------------------------------------------------------------