-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathTypes.cpp
118 lines (94 loc) · 3.41 KB
/
Types.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/**
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0.
*/
#include <aws/crt/Types.h>
#include <aws/common/encoding.h>
namespace Aws
{
namespace Crt
{
ByteBuf ByteBufFromCString(const char *str) noexcept
{
return aws_byte_buf_from_c_str(str);
}
ByteBuf ByteBufFromEmptyArray(const uint8_t *array, size_t len) noexcept
{
return aws_byte_buf_from_empty_array(array, len);
}
ByteBuf ByteBufFromArray(const uint8_t *array, size_t capacity) noexcept
{
return aws_byte_buf_from_array(array, capacity);
}
ByteBuf ByteBufNewCopy(Allocator *alloc, const uint8_t *array, size_t len)
{
ByteBuf retVal;
ByteBuf src = aws_byte_buf_from_array(array, len);
aws_byte_buf_init_copy(&retVal, alloc, &src);
return retVal;
}
ByteBuf ByteBufInit(Allocator *alloc, size_t len)
{
ByteBuf buff;
aws_byte_buf_init(&buff, alloc, len);
return buff;
}
void ByteBufDelete(ByteBuf &buf)
{
aws_byte_buf_clean_up(&buf);
}
ByteCursor ByteCursorFromCString(const char *str) noexcept
{
return aws_byte_cursor_from_c_str(str);
}
ByteCursor ByteCursorFromString(const Crt::String &str) noexcept
{
return aws_byte_cursor_from_array((const void *)str.data(), str.length());
}
ByteCursor ByteCursorFromStringView(const Crt::StringView &str) noexcept
{
return aws_byte_cursor_from_array((const void *)str.data(), str.length());
}
ByteCursor ByteCursorFromByteBuf(const ByteBuf &buf) noexcept
{
return aws_byte_cursor_from_buf(&buf);
}
ByteCursor ByteCursorFromArray(const uint8_t *array, size_t len) noexcept
{
return aws_byte_cursor_from_array(array, len);
}
Vector<uint8_t> Base64Decode(const String &decode) noexcept
{
ByteCursor toDecode = ByteCursorFromString(decode);
size_t allocationSize = 0;
if (AWS_OP_SUCCESS != aws_base64_compute_decoded_len(&toDecode, &allocationSize))
{
return {};
}
Vector<uint8_t> output(allocationSize, 0x00);
ByteBuf tempBuf = aws_byte_buf_from_empty_array(output.data(), output.size());
if (AWS_OP_SUCCESS != aws_base64_decode(&toDecode, &tempBuf))
{
return {};
}
return output;
}
String Base64Encode(const Vector<uint8_t> &encode) noexcept
{
auto toEncode = aws_byte_cursor_from_array((const void *)encode.data(), encode.size());
size_t allocationSize = 0;
if (AWS_OP_SUCCESS != aws_base64_compute_encoded_len(toEncode.len, &allocationSize))
{
return {};
}
String outputStr(allocationSize, 0x00);
auto tempBuf = aws_byte_buf_from_empty_array(outputStr.data(), outputStr.size());
if (AWS_OP_SUCCESS != aws_base64_encode(&toEncode, &tempBuf))
{
return {};
}
AWS_ASSERT(outputStr.length() == tempBuf.len);
return outputStr;
}
} // namespace Crt
} // namespace Aws