Skip to content
项目
群组
代码片段
帮助
正在加载...
登录
切换导航
M
mediasoup
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
聂卓夫
mediasoup
Commits
d37056cd
提交
d37056cd
authored
2月 27, 2020
作者:
Iñaki Baz Castillo
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
C++ Base64 works :)
上级
3b39ca9e
显示空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
48 行增加
和
78 行删除
+48
-78
Utils.hpp
worker/include/Utils.hpp
+4
-4
String.cpp
worker/src/Utils/String.cpp
+28
-63
TestString.cpp
worker/test/src/Utils/TestString.cpp
+16
-11
没有找到文件。
worker/include/Utils.hpp
浏览文件 @
d37056cd
...
@@ -295,10 +295,10 @@ namespace Utils
...
@@ -295,10 +295,10 @@ namespace Utils
{
{
public
:
public
:
static
void
ToLowerCase
(
std
::
string
&
str
);
static
void
ToLowerCase
(
std
::
string
&
str
);
static
unsigned
char
*
Base64Encode
(
const
unsigned
char
*
data
,
size_t
len
,
size_t
*
outL
en
);
static
std
::
string
Base64Encode
(
const
uint8_t
*
data
,
size_t
l
en
);
static
unsigned
char
*
Base64Encode
(
const
std
::
string
&
data
,
size_t
*
outLen
);
static
std
::
string
Base64Encode
(
const
std
::
string
&
str
);
static
u
nsigned
char
*
Base64Decode
(
const
unsigned
char
*
data
,
size_t
len
,
size_t
*
outLen
);
static
u
int8_t
*
Base64Decode
(
const
uint8_t
*
data
,
size_t
len
,
size_t
*
outLen
);
static
u
nsigned
char
*
Base64Decode
(
const
std
::
string
&
data
,
size_t
*
outLen
);
static
u
int8_t
*
Base64Decode
(
const
std
::
string
&
data
,
size_t
*
outLen
);
};
};
inline
void
String
::
ToLowerCase
(
std
::
string
&
str
)
inline
void
String
::
ToLowerCase
(
std
::
string
&
str
)
...
...
worker/src/Utils/String.cpp
浏览文件 @
d37056cd
...
@@ -12,50 +12,37 @@
...
@@ -12,50 +12,37 @@
// #define MS_LOG_DEV_LEVEL 3
// #define MS_LOG_DEV_LEVEL 3
#include "Logger.hpp"
#include "Logger.hpp"
#include "MediaSoupErrors.hpp"
#include "Utils.hpp"
#include "Utils.hpp"
#include <cstring> // std::memset()
#include <cstring> // std::memset()
/* Static. */
/* Static. */
static
constexpr
size_t
BufferOutSize
{
65536
};
static
constexpr
size_t
BufferOutSize
{
65536
};
static
u
nsigned
char
BufferOut
[
BufferOutSize
];
static
u
int8_t
BufferOut
[
BufferOutSize
];
static
const
u
nsigned
char
Base64Table
[
65
]
=
static
const
u
int8_t
Base64Table
[
65
]
=
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
;
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
;
namespace
Utils
namespace
Utils
{
{
/**
std
::
string
Utils
::
String
::
Base64Encode
(
const
uint8_t
*
data
,
size_t
len
)
* @param data - Data to be encoded.
* @param len - Length of the data to be encoded.
* @param outLen - Pointer to output length variable.
*
* @returns Buffer of outLen bytes of encoded data, or nulllptr on failure.
*
* Returned buffer is nul terminated to make it easier to use as a C string.
* The nul terminator is not included in outLen.
*/
unsigned
char
*
Utils
::
String
::
Base64Encode
(
const
unsigned
char
*
data
,
size_t
len
,
size_t
*
outLen
)
{
{
MS_TRACE
();
MS_TRACE
();
unsigned
char
*
out
=
BufferOut
;
const
uint8_t
*
out
=
BufferOut
;
u
nsigned
char
*
pos
;
u
int8_t
*
pos
;
const
u
nsigned
char
*
end
;
const
u
int8_t
*
end
;
const
u
nsigned
char
*
in
;
const
u
int8_t
*
in
;
size_t
olen
;
size_t
olen
;
int
lineLen
;
olen
=
len
*
4
/
3
+
4
;
// 3-byte blocks to 4-byte.
olen
=
len
*
4
/
3
+
4
;
// 3-byte blocks to 4-byte.
olen
+=
olen
/
72
;
// line feeds.
olen
++
;
// Nul termination.
if
(
olen
<
len
)
if
(
olen
<
len
)
return
nullptr
;
// Integer overflow.
MS_THROW_TYPE_ERROR
(
"integer overflow"
);
end
=
data
+
len
;
end
=
data
+
len
;
in
=
data
;
in
=
data
;
pos
=
out
;
pos
=
const_cast
<
uint8_t
*>
(
out
);
lineLen
=
0
;
while
(
end
-
in
>=
3
)
while
(
end
-
in
>=
3
)
{
{
...
@@ -64,13 +51,6 @@ namespace Utils
...
@@ -64,13 +51,6 @@ namespace Utils
*
pos
++
=
Base64Table
[((
in
[
1
]
&
0x0f
)
<<
2
)
|
(
in
[
2
]
>>
6
)];
*
pos
++
=
Base64Table
[((
in
[
1
]
&
0x0f
)
<<
2
)
|
(
in
[
2
]
>>
6
)];
*
pos
++
=
Base64Table
[
in
[
2
]
&
0x3f
];
*
pos
++
=
Base64Table
[
in
[
2
]
&
0x3f
];
in
+=
3
;
in
+=
3
;
lineLen
+=
4
;
if
(
lineLen
>=
72
)
{
*
pos
++
=
'\n'
;
lineLen
=
0
;
}
}
}
if
(
end
-
in
)
if
(
end
-
in
)
...
@@ -89,43 +69,29 @@ namespace Utils
...
@@ -89,43 +69,29 @@ namespace Utils
}
}
*
pos
++
=
'='
;
*
pos
++
=
'='
;
lineLen
+=
4
;
}
}
if
(
lineLen
)
return
std
::
string
(
reinterpret_cast
<
const
char
*>
(
out
),
pos
-
out
);
*
pos
++
=
'\n'
;
*
pos
=
'\0'
;
if
(
outLen
)
*
outLen
=
pos
-
out
;
return
out
;
}
}
unsigned
char
*
Utils
::
String
::
Base64Encode
(
const
std
::
string
&
data
,
size_t
*
outLen
)
std
::
string
Utils
::
String
::
Base64Encode
(
const
std
::
string
&
str
)
{
{
MS_TRACE
();
MS_TRACE
();
return
Base64Encode
(
reinterpret_cast
<
const
unsigned
char
*>
(
data
.
c_str
()),
data
.
size
(),
outLen
);
auto
*
data
=
reinterpret_cast
<
const
uint8_t
*>
(
str
.
c_str
());
return
Base64Encode
(
data
,
str
.
size
());
}
}
/**
uint8_t
*
Utils
::
String
::
Base64Decode
(
const
uint8_t
*
data
,
size_t
len
,
size_t
*
outLen
)
* @param data - Data to be decoded.
* @param len - Length of the data to be decoded.
* @param outLen - Pointer to output length variable.
*
* @returns Buffer of outLen bytes of decoded data, or nullptr on failure.
*/
unsigned
char
*
Utils
::
String
::
Base64Decode
(
const
unsigned
char
*
data
,
size_t
len
,
size_t
*
outLen
)
{
{
MS_TRACE
();
MS_TRACE
();
u
nsigned
char
dtable
[
256
];
u
int8_t
dtable
[
256
];
u
nsigned
char
*
out
=
BufferOut
;
u
int8_t
*
out
=
BufferOut
;
u
nsigned
char
*
pos
;
u
int8_t
*
pos
;
u
nsigned
char
block
[
4
];
u
int8_t
block
[
4
];
u
nsigned
char
tmp
;
u
int8_t
tmp
;
size_t
i
;
size_t
i
;
size_t
count
;
size_t
count
;
size_t
olen
;
size_t
olen
;
...
@@ -135,9 +101,9 @@ namespace Utils
...
@@ -135,9 +101,9 @@ namespace Utils
for
(
i
=
0
;
i
<
sizeof
(
Base64Table
)
-
1
;
++
i
)
for
(
i
=
0
;
i
<
sizeof
(
Base64Table
)
-
1
;
++
i
)
{
{
dtable
[
Base64Table
[
i
]]
=
static_cast
<
u
nsigned
char
>
(
i
);
dtable
[
Base64Table
[
i
]]
=
static_cast
<
u
int8_t
>
(
i
);
}
}
dtable
[
static_cast
<
unsigned
char
>
(
'='
)
]
=
0
;
dtable
[
uint8_t
{
'='
}
]
=
0
;
count
=
0
;
count
=
0
;
for
(
i
=
0
;
i
<
len
;
++
i
)
for
(
i
=
0
;
i
<
len
;
++
i
)
...
@@ -180,10 +146,7 @@ namespace Utils
...
@@ -180,10 +146,7 @@ namespace Utils
else
if
(
pad
==
2
)
else
if
(
pad
==
2
)
pos
-=
2
;
pos
-=
2
;
else
else
{
MS_THROW_TYPE_ERROR
(
"integer padding"
);
// Invalid padding.
return
nullptr
;
}
break
;
break
;
}
}
...
@@ -195,10 +158,12 @@ namespace Utils
...
@@ -195,10 +158,12 @@ namespace Utils
return
out
;
return
out
;
}
}
u
nsigned
char
*
Utils
::
String
::
Base64Decode
(
const
std
::
string
&
data
,
size_t
*
outLen
)
u
int8_t
*
Utils
::
String
::
Base64Decode
(
const
std
::
string
&
str
,
size_t
*
outLen
)
{
{
MS_TRACE
();
MS_TRACE
();
return
Base64Decode
(
reinterpret_cast
<
const
unsigned
char
*>
(
data
.
c_str
()),
data
.
size
(),
outLen
);
auto
*
data
=
reinterpret_cast
<
const
uint8_t
*>
(
str
.
c_str
());
return
Base64Decode
(
data
,
str
.
size
(),
outLen
);
}
}
}
// namespace Utils
}
// namespace Utils
worker/test/src/Utils/TestString.cpp
浏览文件 @
d37056cd
...
@@ -20,27 +20,32 @@ SCENARIO("String::ToLowerCase()")
...
@@ -20,27 +20,32 @@ SCENARIO("String::ToLowerCase()")
SCENARIO
(
"String::Base64Encode()"
)
SCENARIO
(
"String::Base64Encode()"
)
{
{
std
::
string
data
;
std
::
string
data
;
size_t
outLen
;
unsigned
char
*
encodedPtr
;
unsigned
char
*
decodedPtr
;
std
::
string
encoded
;
std
::
string
encoded
;
size_t
outLen
;
uint8_t
*
decodedPtr
;
std
::
string
decoded
;
std
::
string
decoded
;
data
=
"abcd"
;
data
=
"abcd"
;
encodedPtr
=
String
::
Base64Encode
(
data
,
&
outLen
);
encoded
=
String
::
Base64Encode
(
data
);
encoded
=
std
::
string
(
reinterpret_cast
<
char
*>
(
encodedPtr
),
outLen
-
1
);
// TODO
decodedPtr
=
String
::
Base64Decode
(
encoded
,
std
::
addressof
(
outLen
));
decodedPtr
=
String
::
Base64Decode
(
encoded
,
&
outLen
);
decoded
=
std
::
string
(
reinterpret_cast
<
char
*>
(
decodedPtr
),
outLen
);
decoded
=
std
::
string
(
reinterpret_cast
<
char
*>
(
decodedPtr
),
outLen
);
REQUIRE
(
encoded
==
"YWJjZA=="
);
REQUIRE
(
encoded
==
"YWJjZA=="
);
REQUIRE
(
decoded
==
data
);
REQUIRE
(
decoded
==
data
);
data
=
"Iñaki"
;
data
=
"Iñaki"
;
encodedPtr
=
String
::
Base64Encode
(
data
,
&
outLen
);
encoded
=
String
::
Base64Encode
(
data
);
encoded
=
std
::
string
(
reinterpret_cast
<
char
*>
(
encodedPtr
),
outLen
-
1
);
// TODO
decodedPtr
=
String
::
Base64Decode
(
encoded
,
std
::
addressof
(
outLen
));
decodedPtr
=
String
::
Base64Decode
(
encoded
,
&
outLen
);
decoded
=
std
::
string
(
reinterpret_cast
<
char
*>
(
decodedPtr
),
outLen
);
decoded
=
std
::
string
(
reinterpret_cast
<
char
*>
(
decodedPtr
),
outLen
);
REQUIRE
(
encoded
==
"ScOxYWtp"
);
REQUIRE
(
encoded
==
"ScOxYWtp"
);
REQUIRE
(
decoded
==
data
);
REQUIRE
(
decoded
==
data
);
data
=
"kjsh 23 å∫∂ is89 ∫¶ §∂¶ i823y kjahsd 234u asd kasjhdii7682342 asdkjhaskjsahd k jashd kajsdhaksjdh skadhkjhkjh askdjhasdkjahs uyqiwey aså∫∂¢∞¬∫∂ ashksajdh kjasdhkajshda s kjahsdkjas 987897as897 97898623 9s kjsgå∫∂ 432å∫ƒ∂ å∫#¢ ouyqwiuyais kajsdhiuye ajshkkSAH SDFYÑÑÑ å∫∂Ω 87253847b asdbuiasdi as kasuœæ€
\n
321"
;
encoded
=
String
::
Base64Encode
(
data
);
decodedPtr
=
String
::
Base64Decode
(
encoded
,
std
::
addressof
(
outLen
));
decoded
=
std
::
string
(
reinterpret_cast
<
char
*>
(
decodedPtr
),
outLen
);
REQUIRE
(
encoded
==
"a2pzaCAyMyDDpeKIq+KIgiBpczg5IOKIq8K2IMKn4oiCwrYgaTgyM3kga2phaHNkIDIzNHUgYXNkIGthc2poZGlpNzY4MjM0MiBhc2Rramhhc2tqc2FoZCAgIGsgamFzaGQga2Fqc2RoYWtzamRoIHNrYWRoa2poa2poICAgICAgIGFza2RqaGFzZGtqYWhzIHV5cWl3ZXkgYXPDpeKIq+KIgsKi4oiewqziiKviiIIgYXNoa3NhamRoIGtqYXNkaGthanNoZGEgcyBramFoc2RramFzIDk4Nzg5N2FzODk3IDk3ODk4NjIzIDlzIGtqc2fDpeKIq+KIgiA0MzLDpeKIq8aS4oiCIMOl4oirI8KiIG91eXF3aXV5YWlzIGthanNkaGl1eWUgIGFqc2hra1NBSCBTREZZw5HDkcORIMOl4oir4oiCzqkgODcyNTM4NDdiIGFzZGJ1aWFzZGkgYXMga2FzdcWTw6bigqwKMzIx"
);
REQUIRE
(
decoded
==
data
);
}
}
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论