"Fossies" - the Fresh Open Source Software Archive 
Member "tcpflow-1.6.1/src/be13_api/sbuf_private.h" (19 Feb 2021, 4752 Bytes) of package /linux/misc/tcpflow-1.6.1.tar.gz:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style:
standard) with prefixed line numbers and
code folding option.
Alternatively you can here
view or
download the uninterpreted source code file.
For more information about "sbuf_private.h" see the
Fossies "Dox" file reference documentation.
1 /* -*- mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 #ifndef SBUF_PRIVATE_H
3 #define SBUF_PRIVATE_H
4
5 #include <unistd.h>
6
7 #ifdef HAVE_MMAP
8 #include <sys/mman.h>
9 #endif
10
11 inline int sbuf_t::memcmp(const uint8_t *cbuf,size_t at,size_t len) const {
12 if(left(at) < len) throw sbuf_t::range_exception_t();
13 return ::memcmp(this->buf+at,cbuf,len);
14 }
15
16
17 /**
18 * Unsigned get interfaces
19 */
20 inline uint8_t sbuf_t::get8u(size_t i) const {
21 if(i+1>bufsize) throw sbuf_t::range_exception_t();
22 return this->buf[i];
23 }
24
25 inline uint16_t sbuf_t::get16u(size_t i) const {
26 if(i+2>bufsize) throw sbuf_t::range_exception_t();
27 return 0
28 | (uint16_t)(this->buf[i+0]<<0)
29 | (uint16_t)(this->buf[i+1]<<8);
30 }
31
32 inline uint32_t sbuf_t::get32u(size_t i) const {
33 if(i+4>bufsize) throw sbuf_t::range_exception_t();
34 return 0
35 | (uint32_t)(this->buf[i+0]<<0)
36 | (uint32_t)(this->buf[i+1]<<8)
37 | (uint32_t)(this->buf[i+2]<<16)
38 | (uint32_t)(this->buf[i+3]<<24);
39 }
40
41 inline uint64_t sbuf_t::get64u(size_t i) const {
42 if(i+8>bufsize) throw sbuf_t::range_exception_t();
43 return 0
44 | ((uint64_t)(this->buf[i+0])<<0)
45 | ((uint64_t)(this->buf[i+1])<<8)
46 | ((uint64_t)(this->buf[i+2])<<16)
47 | ((uint64_t)(this->buf[i+3])<<24)
48 | ((uint64_t)(this->buf[i+4])<<32)
49 | ((uint64_t)(this->buf[i+5])<<40)
50 | ((uint64_t)(this->buf[i+6])<<48)
51 | ((uint64_t)(this->buf[i+7])<<56);
52 }
53
54 inline uint8_t sbuf_t::get8uBE(size_t i) const {
55 if(i+1>bufsize) throw sbuf_t::range_exception_t();
56 return this->buf[i];
57 }
58
59 inline uint16_t sbuf_t::get16uBE(size_t i) const {
60 if(i+2>bufsize) throw sbuf_t::range_exception_t();
61 return 0
62 | (uint16_t)(this->buf[i+1]<<0)
63 | (uint16_t)(this->buf[i+0]<<8);
64 }
65
66 inline uint32_t sbuf_t::get32uBE(size_t i) const {
67 if(i+4>bufsize) throw sbuf_t::range_exception_t();
68 return 0
69 | (uint32_t)(this->buf[i+3]<<0)
70 | (uint32_t)(this->buf[i+2]<<8)
71 | (uint32_t)(this->buf[i+1]<<16)
72 | (uint32_t)(this->buf[i+0]<<24);
73 }
74
75 inline uint64_t sbuf_t::get64uBE(size_t i) const {
76 if(i+8>bufsize) throw sbuf_t::range_exception_t();
77 return 0
78 | ((uint64_t)(this->buf[i+7])<<0)
79 | ((uint64_t)(this->buf[i+6])<<8)
80 | ((uint64_t)(this->buf[i+5])<<16)
81 | ((uint64_t)(this->buf[i+4])<<24)
82 | ((uint64_t)(this->buf[i+3])<<32)
83 | ((uint64_t)(this->buf[i+2])<<40)
84 | ((uint64_t)(this->buf[i+1])<<48)
85 | ((uint64_t)(this->buf[i+0])<<56);
86 }
87
88 inline uint8_t sbuf_t::get8u(size_t i,sbuf_t::byte_order_t bo) const {
89 return bo==BO_LITTLE_ENDIAN ? get8u(i) : get8uBE(i);
90 }
91
92 inline uint16_t sbuf_t::get16u(size_t i,sbuf_t::byte_order_t bo) const {
93 return bo==BO_LITTLE_ENDIAN ? get16u(i) : get16uBE(i);
94 }
95
96 inline uint32_t sbuf_t::get32u(size_t i,sbuf_t::byte_order_t bo) const {
97 return bo==BO_LITTLE_ENDIAN ? get32u(i) : get32uBE(i);
98 }
99
100 inline uint64_t sbuf_t::get64u(size_t i,sbuf_t::byte_order_t bo) const {
101 return bo==BO_LITTLE_ENDIAN ? get64u(i) : get64uBE(i);
102 }
103
104 /**
105 * Signed get interfaces simply call the unsigned interfaces and
106 * the return gets cast.
107 */
108 inline int8_t sbuf_t::get8i(size_t i) const { return get8u(i);}
109 inline int16_t sbuf_t::get16i(size_t i) const { return get16u(i);}
110 inline int32_t sbuf_t::get32i(size_t i) const { return get32u(i);}
111 inline int64_t sbuf_t::get64i(size_t i) const { return get64u(i);}
112 inline int8_t sbuf_t::get8iBE(size_t i) const { return get8uBE(i);}
113 inline int16_t sbuf_t::get16iBE(size_t i) const { return get16uBE(i);}
114 inline int32_t sbuf_t::get32iBE(size_t i) const { return get32uBE(i);}
115 inline int64_t sbuf_t::get64iBE(size_t i) const { return get64uBE(i);}
116
117 inline int8_t sbuf_t::get8i(size_t i,sbuf_t::byte_order_t bo) const {
118 return bo==BO_LITTLE_ENDIAN ? get8u(i) : get8uBE(i);
119 }
120
121 inline int16_t sbuf_t::get16i(size_t i,sbuf_t::byte_order_t bo) const {
122 return bo==BO_LITTLE_ENDIAN ? get16u(i) : get16uBE(i);
123 }
124
125 inline int32_t sbuf_t::get32i(size_t i,sbuf_t::byte_order_t bo) const {
126 return bo==BO_LITTLE_ENDIAN ? get32u(i) : get32uBE(i);
127 }
128
129 inline int64_t sbuf_t::get64i(size_t i,sbuf_t::byte_order_t bo) const {
130 return bo==BO_LITTLE_ENDIAN ? get64u(i) : get64uBE(i);
131 }
132
133 inline void sbuf_t::release()
134 {
135 #ifdef HAVE_MMAP
136 if(should_unmap && buf){
137 munmap((void *)buf,bufsize);
138 should_unmap = false;
139 buf = 0;
140 }
141 #endif
142 if(should_close && fd>0){
143 ::close(fd);
144 should_close = false;
145 fd=0;
146 }
147 if(should_free && buf){
148 free((void *)buf);
149 should_free = false;
150 buf = 0;
151 }
152 page_number = 0;
153 bufsize = 0;
154 pagesize = 0;
155 }
156
157 #endif