-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathkikcodes.cpp
96 lines (70 loc) · 2.34 KB
/
kikcodes.cpp
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
#include "kikcodes.h"
#include "kikcode_encoding.h"
#include <cstring>
#include <iostream>
using namespace std;
int kikCodeEncodeUsername(
unsigned char *out_data,
const char *username,
const unsigned int username_length,
const unsigned short nonce,
const unsigned int colour_code)
{
UsernameKikCode kik_code(string(username, username_length), nonce, (KikCode::Colour)colour_code);
kik_code.encode(out_data);
return KIK_CODE_RESULT_SUCCESS;
}
int kikCodeEncodeGroup(
unsigned char *out_data,
const unsigned char *invite_code,
const unsigned int colour_code)
{
GroupKikCode kik_code(string((const char *)invite_code, 20), (KikCode::Colour)colour_code);
kik_code.encode(out_data);
return KIK_CODE_RESULT_SUCCESS;
}
int kikCodeEncodeRemote(
unsigned char *out_data,
const unsigned char *key,
const unsigned int colour_code)
{
RemoteKikCode kik_code(string((const char *)key, 20), (KikCode::Colour)colour_code);
kik_code.encode(out_data);
return KIK_CODE_RESULT_SUCCESS;
}
int kikCodeDecode(
const unsigned char *data,
unsigned int *out_type,
KikCodePayload *out_payload,
unsigned int *out_colour_code)
{
KikCode *kik_code = KikCode::parse(data);
if (!kik_code) {
return KIK_CODE_RESULT_ERROR;
}
*out_type = (unsigned int)kik_code->type();
*out_colour_code = (unsigned int)kik_code->colour();
switch (kik_code->type()) {
case KikCode::Type::Username: {
UsernameKikCode *username_code = (UsernameKikCode *)kik_code;
string username = username_code->username();
memcpy(out_payload->username.username, username.c_str(), username.length());
out_payload->username.username[username.length()] = '\0';
out_payload->username.username_length = username.length();
out_payload->username.nonce = username_code->nonce();
break;
}
case KikCode::Type::Remote: {
RemoteKikCode *remote_code = (RemoteKikCode *)kik_code;
memcpy(out_payload->remote.payload, remote_code->payload().c_str(), 20);
break;
}
case KikCode::Type::Group: {
GroupKikCode *group_code = (GroupKikCode *)kik_code;
memcpy(out_payload->group.invite_code, group_code->inviteCode().c_str(), 20);
break;
}
}
delete kik_code;
return KIK_CODE_RESULT_SUCCESS;
}