opal-up 0.0.4 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -18,128 +18,133 @@
18
18
  #ifndef UWS_WEBSOCKETHANDSHAKE_H
19
19
  #define UWS_WEBSOCKETHANDSHAKE_H
20
20
 
21
- #include <cstdint>
22
21
  #include <cstddef>
22
+ #include <cstdint>
23
23
 
24
24
  namespace uWS {
25
25
 
26
26
  struct WebSocketHandshake {
27
- template <int N, typename T>
28
- struct static_for {
29
- void operator()(uint32_t *a, uint32_t *b) {
30
- static_for<N - 1, T>()(a, b);
31
- T::template f<N - 1>(a, b);
32
- }
33
- };
34
-
35
- template <typename T>
36
- struct static_for<0, T> {
37
- void operator()(uint32_t */*a*/, uint32_t */*hash*/) {}
38
- };
39
-
40
- static inline uint32_t rol(uint32_t value, size_t bits) {return (value << bits) | (value >> (32 - bits));}
41
- static inline uint32_t blk(uint32_t b[16], size_t i) {
42
- return rol(b[(i + 13) & 15] ^ b[(i + 8) & 15] ^ b[(i + 2) & 15] ^ b[i], 1);
27
+ template <int N, typename T> struct static_for {
28
+ void operator()(uint32_t *a, uint32_t *b) {
29
+ static_for<N - 1, T>()(a, b);
30
+ T::template f<N - 1>(a, b);
43
31
  }
44
-
45
- struct Sha1Loop1 {
46
- template <int i>
47
- static inline void f(uint32_t *a, uint32_t *b) {
48
- a[i % 5] += ((a[(3 + i) % 5] & (a[(2 + i) % 5] ^ a[(1 + i) % 5])) ^ a[(1 + i) % 5]) + b[i] + 0x5a827999 + rol(a[(4 + i) % 5], 5);
49
- a[(3 + i) % 5] = rol(a[(3 + i) % 5], 30);
50
- }
51
- };
52
- struct Sha1Loop2 {
53
- template <int i>
54
- static inline void f(uint32_t *a, uint32_t *b) {
55
- b[i] = blk(b, i);
56
- a[(1 + i) % 5] += ((a[(4 + i) % 5] & (a[(3 + i) % 5] ^ a[(2 + i) % 5])) ^ a[(2 + i) % 5]) + b[i] + 0x5a827999 + rol(a[(5 + i) % 5], 5);
57
- a[(4 + i) % 5] = rol(a[(4 + i) % 5], 30);
58
- }
59
- };
60
- struct Sha1Loop3 {
61
- template <int i>
62
- static inline void f(uint32_t *a, uint32_t *b) {
63
- b[(i + 4) % 16] = blk(b, (i + 4) % 16);
64
- a[i % 5] += (a[(3 + i) % 5] ^ a[(2 + i) % 5] ^ a[(1 + i) % 5]) + b[(i + 4) % 16] + 0x6ed9eba1 + rol(a[(4 + i) % 5], 5);
65
- a[(3 + i) % 5] = rol(a[(3 + i) % 5], 30);
66
- }
67
- };
68
- struct Sha1Loop4 {
69
- template <int i>
70
- static inline void f(uint32_t *a, uint32_t *b) {
71
- b[(i + 8) % 16] = blk(b, (i + 8) % 16);
72
- a[i % 5] += (((a[(3 + i) % 5] | a[(2 + i) % 5]) & a[(1 + i) % 5]) | (a[(3 + i) % 5] & a[(2 + i) % 5])) + b[(i + 8) % 16] + 0x8f1bbcdc + rol(a[(4 + i) % 5], 5);
73
- a[(3 + i) % 5] = rol(a[(3 + i) % 5], 30);
74
- }
75
- };
76
- struct Sha1Loop5 {
77
- template <int i>
78
- static inline void f(uint32_t *a, uint32_t *b) {
79
- b[(i + 12) % 16] = blk(b, (i + 12) % 16);
80
- a[i % 5] += (a[(3 + i) % 5] ^ a[(2 + i) % 5] ^ a[(1 + i) % 5]) + b[(i + 12) % 16] + 0xca62c1d6 + rol(a[(4 + i) % 5], 5);
81
- a[(3 + i) % 5] = rol(a[(3 + i) % 5], 30);
82
- }
83
- };
84
- struct Sha1Loop6 {
85
- template <int i>
86
- static inline void f(uint32_t *a, uint32_t *b) {
87
- b[i] += a[4 - i];
88
- }
89
- };
90
-
91
- static inline void sha1(uint32_t hash[5], uint32_t b[16]) {
92
- uint32_t a[5] = {hash[4], hash[3], hash[2], hash[1], hash[0]};
93
- static_for<16, Sha1Loop1>()(a, b);
94
- static_for<4, Sha1Loop2>()(a, b);
95
- static_for<20, Sha1Loop3>()(a, b);
96
- static_for<20, Sha1Loop4>()(a, b);
97
- static_for<20, Sha1Loop5>()(a, b);
98
- static_for<5, Sha1Loop6>()(a, hash);
32
+ };
33
+
34
+ template <typename T> struct static_for<0, T> {
35
+ void operator()(uint32_t * /*a*/, uint32_t * /*hash*/) {}
36
+ };
37
+
38
+ static inline uint32_t rol(uint32_t value, size_t bits) {
39
+ return (value << bits) | (value >> (32 - bits));
40
+ }
41
+ static inline uint32_t blk(uint32_t b[16], size_t i) {
42
+ return rol(b[(i + 13) & 15] ^ b[(i + 8) & 15] ^ b[(i + 2) & 15] ^ b[i], 1);
43
+ }
44
+
45
+ struct Sha1Loop1 {
46
+ template <int i> static inline void f(uint32_t *a, uint32_t *b) {
47
+ a[i % 5] += ((a[(3 + i) % 5] & (a[(2 + i) % 5] ^ a[(1 + i) % 5])) ^
48
+ a[(1 + i) % 5]) +
49
+ b[i] + 0x5a827999 + rol(a[(4 + i) % 5], 5);
50
+ a[(3 + i) % 5] = rol(a[(3 + i) % 5], 30);
99
51
  }
100
-
101
- static inline void base64(unsigned char *src, char *dst) {
102
- const char *b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
103
- for (int i = 0; i < 18; i += 3) {
104
- *dst++ = b64[(src[i] >> 2) & 63];
105
- *dst++ = b64[((src[i] & 3) << 4) | ((src[i + 1] & 240) >> 4)];
106
- *dst++ = b64[((src[i + 1] & 15) << 2) | ((src[i + 2] & 192) >> 6)];
107
- *dst++ = b64[src[i + 2] & 63];
108
- }
109
- *dst++ = b64[(src[18] >> 2) & 63];
110
- *dst++ = b64[((src[18] & 3) << 4) | ((src[19] & 240) >> 4)];
111
- *dst++ = b64[((src[19] & 15) << 2)];
112
- *dst++ = '=';
52
+ };
53
+ struct Sha1Loop2 {
54
+ template <int i> static inline void f(uint32_t *a, uint32_t *b) {
55
+ b[i] = blk(b, i);
56
+ a[(1 + i) % 5] += ((a[(4 + i) % 5] & (a[(3 + i) % 5] ^ a[(2 + i) % 5])) ^
57
+ a[(2 + i) % 5]) +
58
+ b[i] + 0x5a827999 + rol(a[(5 + i) % 5], 5);
59
+ a[(4 + i) % 5] = rol(a[(4 + i) % 5], 30);
60
+ }
61
+ };
62
+ struct Sha1Loop3 {
63
+ template <int i> static inline void f(uint32_t *a, uint32_t *b) {
64
+ b[(i + 4) % 16] = blk(b, (i + 4) % 16);
65
+ a[i % 5] += (a[(3 + i) % 5] ^ a[(2 + i) % 5] ^ a[(1 + i) % 5]) +
66
+ b[(i + 4) % 16] + 0x6ed9eba1 + rol(a[(4 + i) % 5], 5);
67
+ a[(3 + i) % 5] = rol(a[(3 + i) % 5], 30);
113
68
  }
69
+ };
70
+ struct Sha1Loop4 {
71
+ template <int i> static inline void f(uint32_t *a, uint32_t *b) {
72
+ b[(i + 8) % 16] = blk(b, (i + 8) % 16);
73
+ a[i % 5] += (((a[(3 + i) % 5] | a[(2 + i) % 5]) & a[(1 + i) % 5]) |
74
+ (a[(3 + i) % 5] & a[(2 + i) % 5])) +
75
+ b[(i + 8) % 16] + 0x8f1bbcdc + rol(a[(4 + i) % 5], 5);
76
+ a[(3 + i) % 5] = rol(a[(3 + i) % 5], 30);
77
+ }
78
+ };
79
+ struct Sha1Loop5 {
80
+ template <int i> static inline void f(uint32_t *a, uint32_t *b) {
81
+ b[(i + 12) % 16] = blk(b, (i + 12) % 16);
82
+ a[i % 5] += (a[(3 + i) % 5] ^ a[(2 + i) % 5] ^ a[(1 + i) % 5]) +
83
+ b[(i + 12) % 16] + 0xca62c1d6 + rol(a[(4 + i) % 5], 5);
84
+ a[(3 + i) % 5] = rol(a[(3 + i) % 5], 30);
85
+ }
86
+ };
87
+ struct Sha1Loop6 {
88
+ template <int i> static inline void f(uint32_t *a, uint32_t *b) {
89
+ b[i] += a[4 - i];
90
+ }
91
+ };
92
+
93
+ static inline void sha1(uint32_t hash[5], uint32_t b[16]) {
94
+ uint32_t a[5] = {hash[4], hash[3], hash[2], hash[1], hash[0]};
95
+ static_for<16, Sha1Loop1>()(a, b);
96
+ static_for<4, Sha1Loop2>()(a, b);
97
+ static_for<20, Sha1Loop3>()(a, b);
98
+ static_for<20, Sha1Loop4>()(a, b);
99
+ static_for<20, Sha1Loop5>()(a, b);
100
+ static_for<5, Sha1Loop6>()(a, hash);
101
+ }
102
+
103
+ static inline void base64(unsigned char *src, char *dst) {
104
+ const char *b64 =
105
+ "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
106
+ for (int i = 0; i < 18; i += 3) {
107
+ *dst++ = b64[(src[i] >> 2) & 63];
108
+ *dst++ = b64[((src[i] & 3) << 4) | ((src[i + 1] & 240) >> 4)];
109
+ *dst++ = b64[((src[i + 1] & 15) << 2) | ((src[i + 2] & 192) >> 6)];
110
+ *dst++ = b64[src[i + 2] & 63];
111
+ }
112
+ *dst++ = b64[(src[18] >> 2) & 63];
113
+ *dst++ = b64[((src[18] & 3) << 4) | ((src[19] & 240) >> 4)];
114
+ *dst++ = b64[((src[19] & 15) << 2)];
115
+ *dst++ = '=';
116
+ }
114
117
 
115
118
  public:
116
- static inline void generate(const char input[24], char output[28]) {
117
- uint32_t b_output[5] = {
118
- 0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476, 0xc3d2e1f0
119
- };
120
- uint32_t b_input[16] = {
121
- 0, 0, 0, 0, 0, 0, 0x32353845, 0x41464135, 0x2d453931, 0x342d3437, 0x44412d39,
122
- 0x3543412d, 0x43354142, 0x30444338, 0x35423131, 0x80000000
123
- };
124
-
125
- for (int i = 0; i < 6; i++) {
126
- b_input[i] = (uint32_t) ((input[4 * i + 3] & 0xff) | (input[4 * i + 2] & 0xff) << 8 | (input[4 * i + 1] & 0xff) << 16 | (input[4 * i + 0] & 0xff) << 24);
127
- }
128
- sha1(b_output, b_input);
129
- uint32_t last_b[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 480};
130
- sha1(b_output, last_b);
131
- for (int i = 0; i < 5; i++) {
132
- uint32_t tmp = b_output[i];
133
- char *bytes = (char *) &b_output[i];
134
- bytes[3] = (char) (tmp & 0xff);
135
- bytes[2] = (char) ((tmp >> 8) & 0xff);
136
- bytes[1] = (char) ((tmp >> 16) & 0xff);
137
- bytes[0] = (char) ((tmp >> 24) & 0xff);
138
- }
139
- base64((unsigned char *) b_output, output);
119
+ static inline void generate(const char input[24], char output[28]) {
120
+ uint32_t b_output[5] = {0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476,
121
+ 0xc3d2e1f0};
122
+ uint32_t b_input[16] = {0, 0, 0, 0,
123
+ 0, 0, 0x32353845, 0x41464135,
124
+ 0x2d453931, 0x342d3437, 0x44412d39, 0x3543412d,
125
+ 0x43354142, 0x30444338, 0x35423131, 0x80000000};
126
+
127
+ for (int i = 0; i < 6; i++) {
128
+ b_input[i] = (uint32_t)((input[4 * i + 3] & 0xff) |
129
+ (input[4 * i + 2] & 0xff) << 8 |
130
+ (input[4 * i + 1] & 0xff) << 16 |
131
+ (input[4 * i + 0] & 0xff) << 24);
132
+ }
133
+ sha1(b_output, b_input);
134
+ uint32_t last_b[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 480};
135
+ sha1(b_output, last_b);
136
+ for (int i = 0; i < 5; i++) {
137
+ uint32_t tmp = b_output[i];
138
+ char *bytes = (char *)&b_output[i];
139
+ bytes[3] = (char)(tmp & 0xff);
140
+ bytes[2] = (char)((tmp >> 8) & 0xff);
141
+ bytes[1] = (char)((tmp >> 16) & 0xff);
142
+ bytes[0] = (char)((tmp >> 24) & 0xff);
140
143
  }
144
+ base64((unsigned char *)b_output, output);
145
+ }
141
146
  };
142
147
 
143
- }
148
+ } // namespace uWS
144
149
 
145
150
  #endif // UWS_WEBSOCKETHANDSHAKE_H