Skip to content

Commit

Permalink
Add slice/to_vector helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
romainthomas committed Sep 8, 2024
1 parent e8811ec commit fae9ae7
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 21 deletions.
20 changes: 5 additions & 15 deletions include/LIEF/BinaryStream/SpanStream.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "LIEF/BinaryStream/BinaryStream.hpp"

namespace LIEF {
class VectorStream;
class SpanStream : public BinaryStream {
public:
using BinaryStream::p;
Expand Down Expand Up @@ -64,21 +65,8 @@ class SpanStream : public BinaryStream {

SpanStream() = delete;

SpanStream(const SpanStream& other) :
SpanStream(other.data_)
{
pos_ = other.pos_;
}

SpanStream& operator=(const SpanStream& other) {
if (this == &other) {
return *this;
}
stype_ = STREAM_TYPE::SPAN;
data_ = other.data_;
pos_ = other.pos_;
return *this;
}
SpanStream(const SpanStream& other) = default;
SpanStream& operator=(const SpanStream& other) = default;

SpanStream(SpanStream&& other) noexcept = default;
SpanStream& operator=(SpanStream&& other) noexcept = default;
Expand Down Expand Up @@ -116,6 +104,8 @@ class SpanStream : public BinaryStream {
return data_.subspan(offset, data_.size() - offset);
}

std::unique_ptr<VectorStream> to_vector() const;

static bool classof(const BinaryStream& stream) {
return stream.type() == BinaryStream::STREAM_TYPE::SPAN;
}
Expand Down
10 changes: 7 additions & 3 deletions include/LIEF/BinaryStream/VectorStream.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef VECTOR_BINARY_STREAM_H
#define VECTOR_BINARY_STREAM_H
#ifndef LIEF_VECTOR_STREAM_H
#define LIEF_VECTOR_STREAM_H

#include <vector>
#include <string>
Expand All @@ -23,6 +23,7 @@
#include "LIEF/BinaryStream/BinaryStream.hpp"

namespace LIEF {
class SpanStream;
class VectorStream : public BinaryStream {
public:
using BinaryStream::p;
Expand Down Expand Up @@ -67,10 +68,13 @@ class VectorStream : public BinaryStream {
}

const uint8_t* end() const override {

return this->binary_.data() + this->binary_.size();
}


std::unique_ptr<SpanStream> slice(uint32_t offset, size_t size) const;
std::unique_ptr<SpanStream> slice(uint32_t offset) const;

static bool classof(const BinaryStream& stream) {
return stream.type() == STREAM_TYPE::VECTOR;
}
Expand Down
7 changes: 4 additions & 3 deletions src/BinaryStream/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
target_sources(LIB_LIEF PRIVATE
BinaryStream.cpp
ASN1Reader.cpp
VectorStream.cpp
BinaryStream.cpp
Convert.cpp
FileStream.cpp
MemoryStream.cpp
Convert.cpp
SpanStream.cpp
VectorStream.cpp
)
25 changes: 25 additions & 0 deletions src/BinaryStream/SpanStream.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/* Copyright 2017 - 2024 R. Thomas
* Copyright 2017 - 2024 Quarkslab
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "LIEF/BinaryStream/VectorStream.hpp"
#include "LIEF/BinaryStream/SpanStream.hpp"
namespace LIEF {

std::unique_ptr<VectorStream> SpanStream::to_vector() const {
return std::make_unique<VectorStream>(content());
}

}
14 changes: 14 additions & 0 deletions src/BinaryStream/VectorStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

#include "logging.hpp"
#include "LIEF/BinaryStream/VectorStream.hpp"
#include "LIEF/BinaryStream/SpanStream.hpp"

namespace LIEF {

Expand All @@ -39,5 +40,18 @@ result<VectorStream> VectorStream::from_file(const std::string& file) {
return VectorStream{std::move(data)};
}

std::unique_ptr<SpanStream> VectorStream::slice(uint32_t offset, size_t size) const {
if (offset > binary_.size() || (offset + size) > binary_.size()) {
return nullptr;
}
const uint8_t* start = binary_.data() + offset;
return std::unique_ptr<SpanStream>(new SpanStream(start, size));
}


std::unique_ptr<SpanStream> VectorStream::slice(uint32_t offset) const {
return slice(offset, binary_.size() - offset);
}

}

0 comments on commit fae9ae7

Please sign in to comment.