Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
openeuler
anbox
提交
c5f1a272
A
anbox
项目概览
openeuler
/
anbox
通知
24
Star
1
Fork
1
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
A
anbox
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
c5f1a272
编写于
5月 10, 2017
作者:
S
Simon Fels
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Implement BinaryWriter class to allow writing binary data
上级
ee38b6a1
变更
5
显示空白变更内容
内联
并排
Showing
5 changed file
with
265 addition
and
0 deletion
+265
-0
src/CMakeLists.txt
src/CMakeLists.txt
+1
-0
src/anbox/common/binary_writer.cpp
src/anbox/common/binary_writer.cpp
+106
-0
src/anbox/common/binary_writer.h
src/anbox/common/binary_writer.h
+57
-0
tests/anbox/common/CMakeLists.txt
tests/anbox/common/CMakeLists.txt
+1
-0
tests/anbox/common/binary_writer_tests.cpp
tests/anbox/common/binary_writer_tests.cpp
+100
-0
未找到文件。
src/CMakeLists.txt
浏览文件 @
c5f1a272
...
...
@@ -79,6 +79,7 @@ set(SOURCES
anbox/common/loop_device.cpp
anbox/common/loop_device_allocator.cpp
anbox/common/mount_entry.cpp
anbox/common/binary_writer.cpp
anbox/testing/gtest_utils.h
...
...
src/anbox/common/binary_writer.cpp
0 → 100644
浏览文件 @
c5f1a272
/*
* Copyright (C) 2016 Thomas Voss <thomas.voss.bochum@gmail.com>
* Simon Fels <morphis@gravedo.de>
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 3, as published
* by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranties of
* MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "anbox/common/binary_writer.h"
#include <cstring>
#include <stdexcept>
#include <boost/endian/buffers.hpp>
namespace
{
bool
is_little_endian
()
{
std
::
uint32_t
v
=
1
;
return
(
*
reinterpret_cast
<
std
::
uint8_t
*>
(
&
v
)
==
1
);
}
}
namespace
anbox
{
namespace
common
{
BinaryWriter
::
BinaryWriter
(
std
::
vector
<
std
::
uint8_t
>::
iterator
begin
,
std
::
vector
<
std
::
uint8_t
>::
iterator
end
)
:
begin_
{
begin
},
current_
{
begin
},
end_
{
end
},
byte_order_
{
is_little_endian
()
?
Order
::
Little
:
Order
::
Big
}
{}
void
BinaryWriter
::
set_byte_order
(
Order
order
)
{
byte_order_
=
order
;
}
void
BinaryWriter
::
write_unsigned_short
(
std
::
uint16_t
value
)
{
if
(
current_
+
sizeof
(
value
)
>
end_
)
throw
std
::
out_of_range
{
"Write buffer exhausted"
};
std
::
uint16_t
v
=
value
;
switch
(
byte_order_
)
{
case
Order
::
Big
:
v
=
boost
::
endian
::
native_to_big
(
value
);
break
;
case
Order
::
Little
:
v
=
boost
::
endian
::
native_to_little
(
value
);
break
;
default:
break
;
}
*
reinterpret_cast
<
std
::
uint16_t
*>
(
&
(
*
current_
))
=
v
;
current_
+=
sizeof
(
v
);
}
void
BinaryWriter
::
write_unsigned_long
(
std
::
uint32_t
value
)
{
if
(
current_
+
sizeof
(
value
)
>
end_
)
throw
std
::
out_of_range
{
"Write buffer exhausted"
};
std
::
uint32_t
v
=
value
;
switch
(
byte_order_
)
{
case
Order
::
Big
:
v
=
boost
::
endian
::
native_to_big
(
value
);
break
;
case
Order
::
Little
:
v
=
boost
::
endian
::
native_to_little
(
value
);
break
;
default:
break
;
}
*
reinterpret_cast
<
std
::
uint32_t
*>
(
&
(
*
current_
))
=
v
;
current_
+=
sizeof
(
v
);
}
void
BinaryWriter
::
write_string
(
const
char
*
s
,
std
::
size_t
size
)
{
if
(
current_
+
size
>
end_
)
throw
std
::
out_of_range
{
"Write buffer exhausted"
};
memcpy
(
&
(
*
current_
),
s
,
size
);
current_
+=
size
;
}
void
BinaryWriter
::
write_string_with_size
(
const
std
::
string
&
str
)
{
write_string_with_size
(
str
.
c_str
(),
str
.
length
());
}
void
BinaryWriter
::
write_string_with_size
(
const
char
*
s
,
std
::
size_t
size
)
{
write_unsigned_short
(
size
);
write_string
(
s
,
size
);
}
std
::
size_t
BinaryWriter
::
bytes_written
()
const
{
return
current_
-
begin_
;
}
}
// namespace common
}
// namespace anbox
src/anbox/common/binary_writer.h
0 → 100644
浏览文件 @
c5f1a272
/*
* Copyright (C) 2016 Thomas Voss <thomas.voss.bochum@gmail.com>
* Simon Fels <morphis@gravedo.de>
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 3, as published
* by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranties of
* MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#ifndef ANBOX_COMMON_BINARY_WRITER_H_
#define ANBOX_COMMON_BINARY_WRITER_H_
#include <cstdint>
#include <vector>
#include <string>
namespace
anbox
{
namespace
common
{
class
BinaryWriter
{
public:
enum
class
Order
{
Big
,
Little
,
};
explicit
BinaryWriter
(
std
::
vector
<
std
::
uint8_t
>::
iterator
begin_
,
std
::
vector
<
std
::
uint8_t
>::
iterator
end_
);
void
set_byte_order
(
Order
order
);
void
write_unsigned_short
(
std
::
uint16_t
value
);
void
write_unsigned_long
(
std
::
uint32_t
value
);
void
write_string
(
const
char
*
s
,
std
::
size_t
size
);
void
write_string_with_size
(
const
std
::
string
&
str
);
void
write_string_with_size
(
const
char
*
s
,
std
::
size_t
size
);
std
::
size_t
bytes_written
()
const
;
private:
std
::
vector
<
std
::
uint8_t
>::
iterator
begin_
;
std
::
vector
<
std
::
uint8_t
>::
iterator
current_
;
std
::
vector
<
std
::
uint8_t
>::
iterator
end_
;
Order
byte_order_
;
};
}
// namespace common
}
// namespace anbox
#endif
tests/anbox/common/CMakeLists.txt
浏览文件 @
c5f1a272
...
...
@@ -2,3 +2,4 @@ ANBOX_ADD_TEST(message_channel_tests message_channel_tests.cpp)
ANBOX_ADD_TEST
(
small_vector_tests small_vector_tests.cpp
)
ANBOX_ADD_TEST
(
type_traits_tests type_traits_tests.cpp
)
ANBOX_ADD_TEST
(
scope_ptr_tests scope_ptr_tests.cpp
)
ANBOX_ADD_TEST
(
binary_writer_tests binary_writer_tests.cpp
)
tests/anbox/common/binary_writer_tests.cpp
0 → 100644
浏览文件 @
c5f1a272
/*
* Copyright (C) 2017 Simon Fels <morphis@gravedo.de>
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 3, as published
* by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranties of
* MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "anbox/common/binary_writer.h"
#include <gmock/gmock.h>
namespace
ac
=
anbox
::
common
;
using
namespace
::
testing
;
TEST
(
BinaryWriter
,
WritesUnsignedLong
)
{
std
::
vector
<
std
::
uint8_t
>
buffer
;
buffer
.
resize
(
sizeof
(
std
::
uint32_t
)
*
2
);
ac
::
BinaryWriter
writer
(
buffer
.
begin
(),
buffer
.
end
());
writer
.
write_unsigned_long
(
0x10
);
writer
.
write_unsigned_long
(
0x3322
);
ASSERT_THAT
(
buffer
,
ElementsAre
(
0x10
,
0x00
,
0x00
,
0x00
,
0x22
,
0x33
,
0x00
,
0x00
));
}
TEST
(
BinaryWriter
,
WriteUnsignedLongFailsWithExhaustedError
)
{
std
::
vector
<
std
::
uint8_t
>
buffer
;
ac
::
BinaryWriter
writer
(
buffer
.
begin
(),
buffer
.
end
());
EXPECT_THROW
(
writer
.
write_unsigned_long
(
0x11
),
std
::
out_of_range
);
}
TEST
(
BinaryWriter
,
WriteUnsignedLongWithChangedBinaryOrder
)
{
std
::
vector
<
std
::
uint8_t
>
buffer
;
buffer
.
resize
(
sizeof
(
std
::
uint32_t
));
ac
::
BinaryWriter
writer
(
buffer
.
begin
(),
buffer
.
end
());
writer
.
set_byte_order
(
ac
::
BinaryWriter
::
Order
::
Big
);
writer
.
write_unsigned_long
(
0x11223344
);
ASSERT_THAT
(
buffer
,
ElementsAre
(
0x11
,
0x22
,
0x33
,
0x44
));
buffer
.
clear
();
buffer
.
resize
(
sizeof
(
std
::
uint32_t
));
writer
=
ac
::
BinaryWriter
(
buffer
.
begin
(),
buffer
.
end
());
writer
.
set_byte_order
(
ac
::
BinaryWriter
::
Order
::
Little
);
writer
.
write_unsigned_long
(
0x11223344
);
ASSERT_THAT
(
buffer
,
ElementsAre
(
0x44
,
0x33
,
0x22
,
0x11
));
}
TEST
(
BinaryWriter
,
WriteUnsignedShort
)
{
std
::
vector
<
std
::
uint8_t
>
buffer
;
buffer
.
resize
(
sizeof
(
std
::
uint16_t
)
*
2
);
ac
::
BinaryWriter
writer
(
buffer
.
begin
(),
buffer
.
end
());
writer
.
write_unsigned_short
(
0x10
);
writer
.
write_unsigned_short
(
0x3322
);
ASSERT_THAT
(
buffer
,
ElementsAre
(
0x10
,
0x00
,
0x22
,
0x33
));
}
TEST
(
BinaryWriter
,
WriteUnsignedShortFailsWithExhaustedError
)
{
std
::
vector
<
std
::
uint8_t
>
buffer
;
ac
::
BinaryWriter
writer
(
buffer
.
begin
(),
buffer
.
end
());
EXPECT_THROW
(
writer
.
write_unsigned_short
(
0x11
),
std
::
out_of_range
);
}
TEST
(
BinaryWriter
,
WriteUnsignedShortWithChangedBinaryOrder
)
{
std
::
vector
<
std
::
uint8_t
>
buffer
;
buffer
.
resize
(
sizeof
(
std
::
uint16_t
));
ac
::
BinaryWriter
writer
(
buffer
.
begin
(),
buffer
.
end
());
writer
.
set_byte_order
(
ac
::
BinaryWriter
::
Order
::
Big
);
writer
.
write_unsigned_short
(
0x1122
);
ASSERT_THAT
(
buffer
,
ElementsAre
(
0x11
,
0x22
));
buffer
.
clear
();
buffer
.
resize
(
sizeof
(
std
::
uint16_t
));
writer
=
ac
::
BinaryWriter
(
buffer
.
begin
(),
buffer
.
end
());
writer
.
set_byte_order
(
ac
::
BinaryWriter
::
Order
::
Little
);
writer
.
write_unsigned_short
(
0x1122
);
ASSERT_THAT
(
buffer
,
ElementsAre
(
0x22
,
0x11
));
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录