Skip to content

Commit 504af9f

Browse files
committed
init
0 parents  commit 504af9f

File tree

7 files changed

+794
-0
lines changed

7 files changed

+794
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Debug/
2+
.vs/
3+

src/base64.cpp

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
2+
#include <stdint.h>
3+
#include "base64.h"
4+
5+
// 0x0 0x1 0x2 0x3 0x4 0x5 0x6 0x7 0x8 0x9 0xa 0xb 0xc 0xd 0xe 0xf 0x10 0x11 0x12 0x13 0x14 0x15 0x16 0x17 0x18 0x19 0x1a 0x1b 0x1c 0x1d 0x1e 0x1f 0x20 0x21 0x22 0x23 0x24 0x25 0x26 0x27 0x28 0x29 0x2a 0x2b 0x2c 0x2d 0x2e 0x2f 0x30 0x31 0x32 0x33 0x34 0x35 0x36 0x37 0x38 0x39 0x3a 0x3b 0x3c 0x3d 0x3e 0x3f
6+
// 0 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
7+
// A B C D E F G H I J K L M N O P Q R S T U V W X Y Z a b c d e f g h i j k l m n o p q r s t u v w x y z 0 1 2 3 4 5 6 7 8 9 + /
8+
9+
static void encode_unit(const uint8_t *pi, char *po) {
10+
static char table[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
11+
po[0] = table[pi[0] >> 2];
12+
po[1] = table[(pi[0] & 0x03) << 4 | pi[1] >> 4];
13+
po[2] = table[(pi[1] & 0x0F) << 2 | pi[2] >> 6];
14+
po[3] = table[pi[2] & 0x3F];
15+
}
16+
17+
static void decode_unit(const uint8_t *pi, char *po) {
18+
static uint8_t table[] = {
19+
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
20+
// +, , , , /, 0 - 9
21+
62, 0,0,0, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 0,0,0,0,0,0,0,
22+
// A - Z
23+
0, 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, 0,0,0,0,0,0,
24+
// a - z
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
26+
};
27+
28+
uint8_t buf[4] = { table[*pi], table[pi[1]], 0, 0 };
29+
po[0] = buf[0] << 2 | buf[1] >> 4;
30+
if (pi[2] != '=')
31+
buf[2] = table[pi[2]],
32+
po[1] = buf[1] << 4 | buf[2] >> 2;
33+
else
34+
return;
35+
if (pi[3] != '=')
36+
buf[3] = table[pi[3]],
37+
po[2] = buf[2] << 6 | buf[3];
38+
}
39+
40+
namespace base64 {
41+
string encode(const string& str) {
42+
auto size = str.size();
43+
auto q = size / 3, r = size % 3;
44+
string out(q * 4 + (r ? 4 : 0), '\0');
45+
auto po = (char *)out.c_str();
46+
auto pi = (uint8_t *)str.c_str();
47+
// transform
48+
for (; q; q--, pi += 3, po += 4)
49+
encode_unit(pi, po);
50+
// tail
51+
uint8_t buf[3] = { *pi, 0, 0 };
52+
if (r == 1) {
53+
encode_unit(buf, po);
54+
po[2] = po[3] = '=';
55+
} else if (r == 2) {
56+
buf[1] = pi[1];
57+
encode_unit(buf, po);
58+
po[3] = '=';
59+
}
60+
return std::move(out);
61+
}
62+
63+
string decode(const string& str) {
64+
auto size = str.size();
65+
auto q = size / 4;
66+
auto out_size = q * 3;
67+
if (str[size - 1] == '=')
68+
out_size -= str[size - 2] == '=' ? 2 : 1;
69+
string out(out_size, '\0');
70+
auto po = (char *)out.c_str();
71+
auto pi = (uint8_t *)str.c_str();
72+
// transform
73+
for (; q; q--, pi += 4, po += 3)
74+
decode_unit(pi, po);
75+
return std::move(out);
76+
}
77+
78+
}

src/base64.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
#include <string>
3+
4+
namespace base64 {
5+
using namespace std;
6+
string encode(const string& str);
7+
string decode(const string& str);
8+
}

src/main.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
2+
#include "websocket.h"
3+
4+
#include <string>
5+
#include <Windows.h>
6+
7+
const char *opcodes[] = {
8+
"Additional", // 0
9+
"Text", // 1
10+
"Binary", // 2
11+
"", "", "", "", "", // 3-7
12+
"Close", // 8
13+
"Ping", // 9
14+
"Pong", // 0xA
15+
"", "", "", "", "" // 0xB-0xF
16+
};
17+
18+
using namespace std;
19+
using namespace websocket;
20+
21+
int main(int argc, char **argv) {
22+
tstream::server ser(5333);
23+
auto ts = ser.accept();
24+
WebSocketHandler h = move(ts);
25+
if (h.open())
26+
while (1) {
27+
cout << h.getHost() << endl;
28+
cout << h.getKey() << endl;
29+
cout << h.getPath() << endl;
30+
cout << h.getSubProtocol() << endl;
31+
if (h.recv())
32+
cout << h.data() << endl,
33+
h.send(h.data(), true);
34+
getchar();
35+
}
36+
return 0;
37+
}

0 commit comments

Comments
 (0)