You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

930 lines
27 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Copyright (c) 2014, Peter Thorson. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are met:
  6. * * Redistributions of source code must retain the above copyright
  7. * notice, this list of conditions and the following disclaimer.
  8. * * Redistributions in binary form must reproduce the above copyright
  9. * notice, this list of conditions and the following disclaimer in the
  10. * documentation and/or other materials provided with the distribution.
  11. * * Neither the name of the WebSocket++ Project nor the
  12. * names of its contributors may be used to endorse or promote products
  13. * derived from this software without specific prior written permission.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  18. * ARE DISCLAIMED. IN NO EVENT SHALL PETER THORSON BE LIABLE FOR ANY
  19. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  20. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  21. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  22. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  24. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. *
  26. */
  27. #ifndef WEBSOCKETPP_FRAME_HPP
  28. #define WEBSOCKETPP_FRAME_HPP
  29. #include <algorithm>
  30. #include <string>
  31. #include <websocketpp/common/network.hpp>
  32. #include <websocketpp/common/system_error.hpp>
  33. #include <websocketpp/utilities.hpp>
  34. namespace websocketpp
  35. {
  36. /// Data structures and utility functions for manipulating WebSocket frames
  37. /**
  38. * namespace frame provides a number of data structures and utility functions
  39. * for reading, writing, and manipulating binary encoded WebSocket frames.
  40. */
  41. namespace frame
  42. {
  43. /// Minimum length of a WebSocket frame header.
  44. static const unsigned int BASIC_HEADER_LENGTH = 2;
  45. /// Maximum length of a WebSocket header
  46. static const unsigned int MAX_HEADER_LENGTH = 14;
  47. /// Maximum length of the variable portion of the WebSocket header
  48. static const unsigned int MAX_EXTENDED_HEADER_LENGTH = 12;
  49. /// Two byte conversion union
  50. union uint16_converter
  51. {
  52. uint16_t i;
  53. uint8_t c[2];
  54. };
  55. /// Four byte conversion union
  56. union uint32_converter
  57. {
  58. uint32_t i;
  59. uint8_t c[4];
  60. };
  61. /// Eight byte conversion union
  62. union uint64_converter
  63. {
  64. uint64_t i;
  65. uint8_t c[8];
  66. };
  67. /// Constants and utility functions related to WebSocket opcodes
  68. /**
  69. * WebSocket Opcodes are 4 bits. See RFC6455 section 5.2.
  70. */
  71. namespace opcode
  72. {
  73. enum value
  74. {
  75. continuation = 0x0,
  76. text = 0x1,
  77. binary = 0x2,
  78. rsv3 = 0x3,
  79. rsv4 = 0x4,
  80. rsv5 = 0x5,
  81. rsv6 = 0x6,
  82. rsv7 = 0x7,
  83. close = 0x8,
  84. ping = 0x9,
  85. pong = 0xA,
  86. control_rsvb = 0xB,
  87. control_rsvc = 0xC,
  88. control_rsvd = 0xD,
  89. control_rsve = 0xE,
  90. control_rsvf = 0xF,
  91. CONTINUATION = 0x0,
  92. TEXT = 0x1,
  93. BINARY = 0x2,
  94. RSV3 = 0x3,
  95. RSV4 = 0x4,
  96. RSV5 = 0x5,
  97. RSV6 = 0x6,
  98. RSV7 = 0x7,
  99. CLOSE = 0x8,
  100. PING = 0x9,
  101. PONG = 0xA,
  102. CONTROL_RSVB = 0xB,
  103. CONTROL_RSVC = 0xC,
  104. CONTROL_RSVD = 0xD,
  105. CONTROL_RSVE = 0xE,
  106. CONTROL_RSVF = 0xF
  107. };
  108. /// Check if an opcode is reserved
  109. /**
  110. * @param v The opcode to test.
  111. * @return Whether or not the opcode is reserved.
  112. */
  113. inline bool reserved(value v)
  114. {
  115. return (v >= rsv3 && v <= rsv7) || (v >= control_rsvb && v <= control_rsvf);
  116. }
  117. /// Check if an opcode is invalid
  118. /**
  119. * Invalid opcodes are negative or require greater than 4 bits to store.
  120. *
  121. * @param v The opcode to test.
  122. * @return Whether or not the opcode is invalid.
  123. */
  124. inline bool invalid(value v)
  125. {
  126. return (v > 0xF || v < 0);
  127. }
  128. /// Check if an opcode is for a control frame
  129. /**
  130. * @param v The opcode to test.
  131. * @return Whether or not the opcode is a control opcode.
  132. */
  133. inline bool is_control(value v)
  134. {
  135. return v >= 0x8;
  136. }
  137. } // namespace opcode
  138. /// Constants related to frame and payload limits
  139. namespace limits
  140. {
  141. /// Minimum length of a WebSocket frame header.
  142. static const unsigned int basic_header_length = 2;
  143. /// Maximum length of a WebSocket header
  144. static const unsigned int max_header_length = 14;
  145. /// Maximum length of the variable portion of the WebSocket header
  146. static const unsigned int max_extended_header_length = 12;
  147. /// Maximum size of a basic WebSocket payload
  148. static const uint8_t payload_size_basic = 125;
  149. /// Maximum size of an extended WebSocket payload (basic payload = 126)
  150. static const uint16_t payload_size_extended = 0xFFFF; // 2^16, 65535
  151. /// Maximum size of a jumbo WebSocket payload (basic payload = 127)
  152. static const uint64_t payload_size_jumbo = 0x7FFFFFFFFFFFFFFFLL; // 2^63
  153. /// Maximum size of close frame reason
  154. /**
  155. * This is payload_size_basic - 2 bytes (as first two bytes are used for
  156. * the close code
  157. */
  158. static const uint8_t close_reason_size = 123;
  159. } // namespace limits
  160. // masks for fields in the basic header
  161. static const uint8_t BHB0_OPCODE = 0x0F;
  162. static const uint8_t BHB0_RSV3 = 0x10;
  163. static const uint8_t BHB0_RSV2 = 0x20;
  164. static const uint8_t BHB0_RSV1 = 0x40;
  165. static const uint8_t BHB0_FIN = 0x80;
  166. static const uint8_t BHB1_PAYLOAD = 0x7F;
  167. static const uint8_t BHB1_MASK = 0x80;
  168. static const uint8_t payload_size_code_16bit = 0x7E; // 126
  169. static const uint8_t payload_size_code_64bit = 0x7F; // 127
  170. typedef uint32_converter masking_key_type;
  171. /// The constant size component of a WebSocket frame header
  172. struct basic_header
  173. {
  174. basic_header() :
  175. b0(0x00),
  176. b1(0x00)
  177. {
  178. }
  179. basic_header(uint8_t p0, uint8_t p1) :
  180. b0(p0),
  181. b1(p1)
  182. {
  183. }
  184. basic_header(opcode::value op, uint64_t size, bool fin, bool mask, bool rsv1 = false, bool rsv2 = false, bool rsv3 = false) :
  185. b0(0x00),
  186. b1(0x00)
  187. {
  188. if (fin)
  189. {
  190. b0 |= BHB0_FIN;
  191. }
  192. if (rsv1)
  193. {
  194. b0 |= BHB0_RSV1;
  195. }
  196. if (rsv2)
  197. {
  198. b0 |= BHB0_RSV2;
  199. }
  200. if (rsv3)
  201. {
  202. b0 |= BHB0_RSV3;
  203. }
  204. b0 |= (op & BHB0_OPCODE);
  205. if (mask)
  206. {
  207. b1 |= BHB1_MASK;
  208. }
  209. uint8_t basic_value;
  210. if (size <= limits::payload_size_basic)
  211. {
  212. basic_value = static_cast<uint8_t>(size);
  213. }
  214. else if (size <= limits::payload_size_extended)
  215. {
  216. basic_value = payload_size_code_16bit;
  217. }
  218. else
  219. {
  220. basic_value = payload_size_code_64bit;
  221. }
  222. b1 |= basic_value;
  223. }
  224. uint8_t b0;
  225. uint8_t b1;
  226. };
  227. /// The variable size component of a WebSocket frame header
  228. struct extended_header
  229. {
  230. extended_header()
  231. {
  232. std::fill_n(this->bytes, MAX_EXTENDED_HEADER_LENGTH, 0x00);
  233. }
  234. extended_header(uint64_t payload_size)
  235. {
  236. std::fill_n(this->bytes, MAX_EXTENDED_HEADER_LENGTH, 0x00);
  237. copy_payload(payload_size);
  238. }
  239. extended_header(uint64_t payload_size, uint32_t masking_key)
  240. {
  241. std::fill_n(this->bytes, MAX_EXTENDED_HEADER_LENGTH, 0x00);
  242. // Copy payload size
  243. int offset = copy_payload(payload_size);
  244. // Copy Masking Key
  245. uint32_converter temp32;
  246. temp32.i = masking_key;
  247. std::copy(temp32.c, temp32.c + 4, bytes + offset);
  248. }
  249. uint8_t bytes[MAX_EXTENDED_HEADER_LENGTH];
  250. private:
  251. int copy_payload(uint64_t payload_size)
  252. {
  253. int payload_offset = 0;
  254. if (payload_size <= limits::payload_size_basic)
  255. {
  256. payload_offset = 8;
  257. }
  258. else if (payload_size <= limits::payload_size_extended)
  259. {
  260. payload_offset = 6;
  261. }
  262. uint64_converter temp64;
  263. temp64.i = lib::net::_htonll(payload_size);
  264. std::copy(temp64.c + payload_offset, temp64.c + 8, bytes);
  265. return 8 - payload_offset;
  266. }
  267. };
  268. bool get_fin(const basic_header & h);
  269. void set_fin(basic_header & h, bool value);
  270. bool get_rsv1(const basic_header & h);
  271. void set_rsv1(basic_header & h, bool value);
  272. bool get_rsv2(const basic_header & h);
  273. void set_rsv2(basic_header & h, bool value);
  274. bool get_rsv3(const basic_header & h);
  275. void set_rsv3(basic_header & h, bool value);
  276. opcode::value get_opcode(const basic_header & h);
  277. bool get_masked(const basic_header & h);
  278. void set_masked(basic_header & h, bool value);
  279. uint8_t get_basic_size(const basic_header &);
  280. size_t get_header_len(const basic_header &);
  281. unsigned int get_masking_key_offset(const basic_header &);
  282. std::string write_header(const basic_header &, const extended_header &);
  283. masking_key_type get_masking_key(const basic_header &, const extended_header &);
  284. uint16_t get_extended_size(const extended_header &);
  285. uint64_t get_jumbo_size(const extended_header &);
  286. uint64_t get_payload_size(const basic_header &, const extended_header &);
  287. size_t prepare_masking_key(const masking_key_type & key);
  288. size_t circshift_prepared_key(size_t prepared_key, size_t offset);
  289. // Functions for performing xor based masking and unmasking
  290. template <typename input_iter, typename output_iter> void byte_mask(input_iter b, input_iter e, output_iter o, const masking_key_type & key, size_t key_offset = 0);
  291. template <typename iter_type> void byte_mask(iter_type b, iter_type e, const masking_key_type & key, size_t key_offset = 0);
  292. void word_mask_exact(uint8_t * input, uint8_t * output, size_t length, const masking_key_type & key);
  293. void word_mask_exact(uint8_t * data, size_t length, const masking_key_type & key);
  294. size_t word_mask_circ(uint8_t * input, uint8_t * output, size_t length, size_t prepared_key);
  295. size_t word_mask_circ(uint8_t * data, size_t length, size_t prepared_key);
  296. /// Check whether the frame's FIN bit is set.
  297. /**
  298. * @param [in] h The basic header to extract from.
  299. * @return True if the header's fin bit is set.
  300. */
  301. inline bool get_fin(const basic_header & h)
  302. {
  303. return ((h.b0 & BHB0_FIN) == BHB0_FIN);
  304. }
  305. /// Set the frame's FIN bit
  306. /**
  307. * @param [out] h Header to set.
  308. * @param [in] value Value to set it to.
  309. */
  310. inline void set_fin(basic_header & h, bool value)
  311. {
  312. h.b0 = (value ? h.b0 | BHB0_FIN : h.b0 & ~BHB0_FIN);
  313. }
  314. /// check whether the frame's RSV1 bit is set
  315. /**
  316. * @param [in] h The basic header to extract from.
  317. * @return True if the header's RSV1 bit is set.
  318. */
  319. inline bool get_rsv1(const basic_header & h)
  320. {
  321. return ((h.b0 & BHB0_RSV1) == BHB0_RSV1);
  322. }
  323. /// Set the frame's RSV1 bit
  324. /**
  325. * @param [out] h Header to set.
  326. * @param [in] value Value to set it to.
  327. */
  328. inline void set_rsv1(basic_header & h, bool value)
  329. {
  330. h.b0 = (value ? h.b0 | BHB0_RSV1 : h.b0 & ~BHB0_RSV1);
  331. }
  332. /// check whether the frame's RSV2 bit is set
  333. /**
  334. * @param [in] h The basic header to extract from.
  335. * @return True if the header's RSV2 bit is set.
  336. */
  337. inline bool get_rsv2(const basic_header & h)
  338. {
  339. return ((h.b0 & BHB0_RSV2) == BHB0_RSV2);
  340. }
  341. /// Set the frame's RSV2 bit
  342. /**
  343. * @param [out] h Header to set.
  344. * @param [in] value Value to set it to.
  345. */
  346. inline void set_rsv2(basic_header & h, bool value)
  347. {
  348. h.b0 = (value ? h.b0 | BHB0_RSV2 : h.b0 & ~BHB0_RSV2);
  349. }
  350. /// check whether the frame's RSV3 bit is set
  351. /**
  352. * @param [in] h The basic header to extract from.
  353. * @return True if the header's RSV3 bit is set.
  354. */
  355. inline bool get_rsv3(const basic_header & h)
  356. {
  357. return ((h.b0 & BHB0_RSV3) == BHB0_RSV3);
  358. }
  359. /// Set the frame's RSV3 bit
  360. /**
  361. * @param [out] h Header to set.
  362. * @param [in] value Value to set it to.
  363. */
  364. inline void set_rsv3(basic_header & h, bool value)
  365. {
  366. h.b0 = (value ? h.b0 | BHB0_RSV3 : h.b0 & ~BHB0_RSV3);
  367. }
  368. /// Extract opcode from basic header
  369. /**
  370. * @param [in] h The basic header to extract from.
  371. * @return The opcode value of the header.
  372. */
  373. inline opcode::value get_opcode(const basic_header & h)
  374. {
  375. return opcode::value(h.b0 & BHB0_OPCODE);
  376. }
  377. /// check whether the frame is masked
  378. /**
  379. * @param [in] h The basic header to extract from.
  380. * @return True if the header mask bit is set.
  381. */
  382. inline bool get_masked(const basic_header & h)
  383. {
  384. return ((h.b1 & BHB1_MASK) == BHB1_MASK);
  385. }
  386. /// Set the frame's MASK bit
  387. /**
  388. * @param [out] h Header to set.
  389. * @param value Value to set it to.
  390. */
  391. inline void set_masked(basic_header & h, bool value)
  392. {
  393. h.b1 = (value ? h.b1 | BHB1_MASK : h.b1 & ~BHB1_MASK);
  394. }
  395. /// Extracts the raw payload length specified in the basic header
  396. /**
  397. * A basic WebSocket frame header contains a 7 bit value that represents the
  398. * payload size. There are two reserved values that are used to indicate that
  399. * the actual payload size will not fit in 7 bits and that the full payload
  400. * size is included in a separate field. The values are as follows:
  401. *
  402. * PAYLOAD_SIZE_CODE_16BIT (0x7E) indicates that the actual payload is less
  403. * than 16 bit
  404. *
  405. * PAYLOAD_SIZE_CODE_64BIT (0x7F) indicates that the actual payload is less
  406. * than 63 bit
  407. *
  408. * @param [in] h Basic header to read value from.
  409. * @return The exact size encoded in h.
  410. */
  411. inline uint8_t get_basic_size(const basic_header & h)
  412. {
  413. return h.b1 & BHB1_PAYLOAD;
  414. }
  415. /// Calculates the full length of the header based on the first bytes.
  416. /**
  417. * A WebSocket frame header always has at least two bytes. Encoded within the
  418. * first two bytes is all the information necessary to calculate the full
  419. * (variable) header length. get_header_len() calculates the full header
  420. * length for the given two byte basic header.
  421. *
  422. * @param h Basic frame header to extract size from.
  423. * @return Full length of the extended header.
  424. */
  425. inline size_t get_header_len(const basic_header & h)
  426. {
  427. // TODO: check extensions?
  428. // masking key offset represents the space used for the extended length
  429. // fields
  430. size_t size = BASIC_HEADER_LENGTH + get_masking_key_offset(h);
  431. // If the header is masked there is a 4 byte masking key
  432. if (get_masked(h))
  433. {
  434. size += 4;
  435. }
  436. return size;
  437. }
  438. /// Calculate the offset location of the masking key within the extended header
  439. /**
  440. * Calculate the offset location of the masking key within the extended header
  441. * using information from its corresponding basic header
  442. *
  443. * @param h Corresponding basic header to calculate from.
  444. *
  445. * @return byte offset of the first byte of the masking key
  446. */
  447. inline unsigned int get_masking_key_offset(const basic_header & h)
  448. {
  449. if (get_basic_size(h) == payload_size_code_16bit)
  450. {
  451. return 2;
  452. }
  453. else if (get_basic_size(h) == payload_size_code_64bit)
  454. {
  455. return 8;
  456. }
  457. else
  458. {
  459. return 0;
  460. }
  461. }
  462. /// Generate a properly sized contiguous string that encodes a full frame header
  463. /**
  464. * Copy the basic header h and extended header e into a properly sized
  465. * contiguous frame header string for the purposes of writing out to the wire.
  466. *
  467. * @param h The basic header to include
  468. * @param e The extended header to include
  469. *
  470. * @return A contiguous string containing h and e
  471. */
  472. inline std::string prepare_header(const basic_header & h, const extended_header & e)
  473. {
  474. std::string ret;
  475. ret.push_back(char(h.b0));
  476. ret.push_back(char(h.b1));
  477. ret.append(reinterpret_cast<const char *>(e.bytes), get_header_len(h) - BASIC_HEADER_LENGTH);
  478. return ret;
  479. }
  480. /// Extract the masking key from a frame header
  481. /**
  482. * Note that while read and written as an integer at times, this value is not
  483. * an integer and should never be interpreted as one. Big and little endian
  484. * machines will generate and store masking keys differently without issue as
  485. * long as the integer values remain irrelivant.
  486. *
  487. * @param h The basic header to extract from
  488. * @param e The extended header to extract from
  489. *
  490. * @return The masking key as an integer.
  491. */
  492. inline masking_key_type get_masking_key(const basic_header & h, const extended_header & e)
  493. {
  494. masking_key_type temp32;
  495. if (! get_masked(h))
  496. {
  497. temp32.i = 0;
  498. }
  499. else
  500. {
  501. unsigned int offset = get_masking_key_offset(h);
  502. std::copy(e.bytes + offset, e.bytes + offset + 4, temp32.c);
  503. }
  504. return temp32;
  505. }
  506. /// Extract the extended size field from an extended header
  507. /**
  508. * It is the responsibility of the caller to verify that e is a valid extended
  509. * header. This function assumes that e contains an extended payload size.
  510. *
  511. * @param e The extended header to extract from
  512. *
  513. * @return The size encoded in the extended header in host byte order
  514. */
  515. inline uint16_t get_extended_size(const extended_header & e)
  516. {
  517. uint16_converter temp16;
  518. std::copy(e.bytes, e.bytes + 2, temp16.c);
  519. return ntohs(temp16.i);
  520. }
  521. /// Extract the jumbo size field from an extended header
  522. /**
  523. * It is the responsibility of the caller to verify that e is a valid extended
  524. * header. This function assumes that e contains a jumbo payload size.
  525. *
  526. * @param e The extended header to extract from
  527. *
  528. * @return The size encoded in the extended header in host byte order
  529. */
  530. inline uint64_t get_jumbo_size(const extended_header & e)
  531. {
  532. uint64_converter temp64;
  533. std::copy(e.bytes, e.bytes + 8, temp64.c);
  534. return lib::net::_ntohll(temp64.i);
  535. }
  536. /// Extract the full payload size field from a WebSocket header
  537. /**
  538. * It is the responsibility of the caller to verify that h and e together
  539. * represent a valid WebSocket frame header. This function assumes only that h
  540. * and e are valid. It uses information in the basic header to determine where
  541. * to look for the payload_size
  542. *
  543. * @param h The basic header to extract from
  544. * @param e The extended header to extract from
  545. *
  546. * @return The size encoded in the combined header in host byte order.
  547. */
  548. inline uint64_t get_payload_size(const basic_header & h, const extended_header & e)
  549. {
  550. uint8_t val = get_basic_size(h);
  551. if (val <= limits::payload_size_basic)
  552. {
  553. return val;
  554. }
  555. else if (val == payload_size_code_16bit)
  556. {
  557. return get_extended_size(e);
  558. }
  559. else
  560. {
  561. return get_jumbo_size(e);
  562. }
  563. }
  564. /// Extract a masking key into a value the size of a machine word.
  565. /**
  566. * Machine word size must be 4 or 8.
  567. *
  568. * @param key Masking key to extract from
  569. *
  570. * @return prepared key as a machine word
  571. */
  572. inline size_t prepare_masking_key(const masking_key_type & key)
  573. {
  574. size_t low_bits = static_cast<size_t>(key.i);
  575. if (sizeof(size_t) == 8)
  576. {
  577. uint64_t high_bits = static_cast<size_t>(key.i);
  578. return static_cast<size_t>((high_bits << 32) | low_bits);
  579. }
  580. else
  581. {
  582. return low_bits;
  583. }
  584. }
  585. /// circularly shifts the supplied prepared masking key by offset bytes
  586. /**
  587. * Prepared_key must be the output of prepare_masking_key with the associated
  588. * restrictions on the machine word size. offset must be greater than or equal
  589. * to zero and less than sizeof(size_t).
  590. */
  591. inline size_t circshift_prepared_key(size_t prepared_key, size_t offset)
  592. {
  593. if (offset == 0)
  594. {
  595. return prepared_key;
  596. }
  597. if (lib::net::is_little_endian())
  598. {
  599. size_t temp = prepared_key << (sizeof(size_t) - offset) * 8;
  600. return (prepared_key >> offset * 8) | temp;
  601. }
  602. else
  603. {
  604. size_t temp = prepared_key >> (sizeof(size_t) - offset) * 8;
  605. return (prepared_key << offset * 8) | temp;
  606. }
  607. }
  608. /// Byte by byte mask/unmask
  609. /**
  610. * Iterator based byte by byte masking and unmasking for WebSocket payloads.
  611. * Performs masking in place using the supplied key offset by the supplied
  612. * offset number of bytes.
  613. *
  614. * This function is simple and can be done in place on input with arbitrary
  615. * lengths and does not vary based on machine word size. It is slow.
  616. *
  617. * @param b Beginning iterator to start masking
  618. *
  619. * @param e Ending iterator to end masking
  620. *
  621. * @param o Beginning iterator to store masked results
  622. *
  623. * @param key 32 bit key to mask with.
  624. *
  625. * @param key_offset offset value to start masking at.
  626. */
  627. template <typename input_iter, typename output_iter> void byte_mask(input_iter first, input_iter last, output_iter result, const masking_key_type & key, size_t key_offset)
  628. {
  629. size_t key_index = key_offset % 4;
  630. while (first != last)
  631. {
  632. *result = *first ^ key.c[key_index++];
  633. key_index %= 4;
  634. ++result;
  635. ++first;
  636. }
  637. }
  638. /// Byte by byte mask/unmask (in place)
  639. /**
  640. * Iterator based byte by byte masking and unmasking for WebSocket payloads.
  641. * Performs masking in place using the supplied key offset by the supplied
  642. * offset number of bytes.
  643. *
  644. * This function is simple and can be done in place on input with arbitrary
  645. * lengths and does not vary based on machine word size. It is slow.
  646. *
  647. * @param b Beginning iterator to start masking
  648. *
  649. * @param e Ending iterator to end masking
  650. *
  651. * @param key 32 bit key to mask with.
  652. *
  653. * @param key_offset offset value to start masking at.
  654. */
  655. template <typename iter_type> void byte_mask(iter_type b, iter_type e, const masking_key_type & key, size_t key_offset)
  656. {
  657. byte_mask(b, e, b, key, key_offset);
  658. }
  659. /// Exact word aligned mask/unmask
  660. /**
  661. * Balanced combination of byte by byte and circular word by word masking.
  662. * Best used to mask complete messages at once. Has much higher setup costs than
  663. * word_mask_circ but works with exact sized buffers.
  664. *
  665. * Buffer based word by word masking and unmasking for WebSocket payloads.
  666. * Masking is done in word by word chunks with the remainder not divisible by
  667. * the word size done byte by byte.
  668. *
  669. * input and output must both be at least length bytes. Exactly length bytes
  670. * will be written.
  671. *
  672. * @param input buffer to mask or unmask
  673. *
  674. * @param output buffer to store the output. May be the same as input.
  675. *
  676. * @param length length of data buffer
  677. *
  678. * @param key Masking key to use
  679. */
  680. inline void word_mask_exact(uint8_t * input, uint8_t * output, size_t length, const masking_key_type & key)
  681. {
  682. size_t prepared_key = prepare_masking_key(key);
  683. size_t n = length / sizeof(size_t);
  684. size_t * input_word = reinterpret_cast<size_t *>(input);
  685. size_t * output_word = reinterpret_cast<size_t *>(output);
  686. for (size_t i = 0; i < n; i++)
  687. {
  688. output_word[i] = input_word[i] ^ prepared_key;
  689. }
  690. for (size_t i = n * sizeof(size_t); i < length; i++)
  691. {
  692. output[i] = input[i] ^ key.c[i % 4];
  693. }
  694. }
  695. /// Exact word aligned mask/unmask (in place)
  696. /**
  697. * In place version of word_mask_exact
  698. *
  699. * @see word_mask_exact
  700. *
  701. * @param data buffer to read and write from
  702. *
  703. * @param length length of data buffer
  704. *
  705. * @param key Masking key to use
  706. */
  707. inline void word_mask_exact(uint8_t * data, size_t length, const masking_key_type & key)
  708. {
  709. word_mask_exact(data, data, length, key);
  710. }
  711. /// Circular word aligned mask/unmask
  712. /**
  713. * Performs a circular mask/unmask in word sized chunks using pre-prepared keys
  714. * that store state between calls. Best for providing streaming masking or
  715. * unmasking of small chunks at a time of a larger message. Requires that the
  716. * underlying allocated size of the data buffer be a multiple of the word size.
  717. * Data in the buffer after `length` will be overwritten only with the same
  718. * values that were originally present.
  719. *
  720. * Buffer based word by word masking and unmasking for WebSocket payloads.
  721. * Performs masking in place using the supplied key. Casts the data buffer to
  722. * an array of size_t's and performs masking word by word. The underlying
  723. * buffer size must be a muliple of the word size.
  724. *
  725. * word_mask returns a copy of prepared_key circularly shifted based on the
  726. * length value. The returned value may be fed back into word_mask when more
  727. * data is available.
  728. *
  729. * input and output must both have length at least:
  730. * ceil(length/sizeof(size_t))*sizeof(size_t)
  731. * Exactly that many bytes will be written, although only exactly length bytes
  732. * will be changed (trailing bytes will be replaced without masking)
  733. *
  734. * @param data Character buffer to mask
  735. *
  736. * @param length Length of data
  737. *
  738. * @param prepared_key Prepared key to use.
  739. *
  740. * @return the prepared_key shifted to account for the input length
  741. */
  742. inline size_t word_mask_circ(uint8_t * input, uint8_t * output, size_t length, size_t prepared_key)
  743. {
  744. size_t n = length / sizeof(size_t); // whole words
  745. size_t l = length - (n * sizeof(size_t)); // remaining bytes
  746. size_t * input_word = reinterpret_cast<size_t *>(input);
  747. size_t * output_word = reinterpret_cast<size_t *>(output);
  748. // mask word by word
  749. for (size_t i = 0; i < n; i++)
  750. {
  751. output_word[i] = input_word[i] ^ prepared_key;
  752. }
  753. // mask partial word at the end
  754. size_t start = length - l;
  755. uint8_t * byte_key = reinterpret_cast<uint8_t *>(&prepared_key);
  756. for (size_t i = 0; i < l; ++i)
  757. {
  758. output[start + i] = input[start + i] ^ byte_key[i];
  759. }
  760. return circshift_prepared_key(prepared_key, l);
  761. }
  762. /// Circular word aligned mask/unmask (in place)
  763. /**
  764. * In place version of word_mask_circ
  765. *
  766. * @see word_mask_circ
  767. *
  768. * @param data Character buffer to read from and write to
  769. *
  770. * @param length Length of data
  771. *
  772. * @param prepared_key Prepared key to use.
  773. *
  774. * @return the prepared_key shifted to account for the input length
  775. */
  776. inline size_t word_mask_circ(uint8_t * data, size_t length, size_t prepared_key)
  777. {
  778. return word_mask_circ(data, data, length, prepared_key);
  779. }
  780. /// Circular byte aligned mask/unmask
  781. /**
  782. * Performs a circular mask/unmask in byte sized chunks using pre-prepared keys
  783. * that store state between calls. Best for providing streaming masking or
  784. * unmasking of small chunks at a time of a larger message. Requires that the
  785. * underlying allocated size of the data buffer be a multiple of the word size.
  786. * Data in the buffer after `length` will be overwritten only with the same
  787. * values that were originally present.
  788. *
  789. * word_mask returns a copy of prepared_key circularly shifted based on the
  790. * length value. The returned value may be fed back into byte_mask when more
  791. * data is available.
  792. *
  793. * @param data Character buffer to mask
  794. *
  795. * @param length Length of data
  796. *
  797. * @param prepared_key Prepared key to use.
  798. *
  799. * @return the prepared_key shifted to account for the input length
  800. */
  801. inline size_t byte_mask_circ(uint8_t * input, uint8_t * output, size_t length, size_t prepared_key)
  802. {
  803. uint32_converter key;
  804. key.i = prepared_key;
  805. for (size_t i = 0; i < length; ++i)
  806. {
  807. output[i] = input[i] ^ key.c[i % 4];
  808. }
  809. return circshift_prepared_key(prepared_key, length % 4);
  810. }
  811. /// Circular byte aligned mask/unmask (in place)
  812. /**
  813. * In place version of byte_mask_circ
  814. *
  815. * @see byte_mask_circ
  816. *
  817. * @param data Character buffer to read from and write to
  818. *
  819. * @param length Length of data
  820. *
  821. * @param prepared_key Prepared key to use.
  822. *
  823. * @return the prepared_key shifted to account for the input length
  824. */
  825. inline size_t byte_mask_circ(uint8_t * data, size_t length, size_t prepared_key)
  826. {
  827. return byte_mask_circ(data, data, length, prepared_key);
  828. }
  829. } // namespace frame
  830. } // namespace websocketpp
  831. #endif // WEBSOCKETPP_FRAME_HPP