mirror of
https://github.com/openappsec/openappsec.git
synced 2025-09-30 11:44:29 +03:00
First release of open-appsec source code
This commit is contained in:
53
core/include/general/buffer/char_iterator.h
Executable file
53
core/include/general/buffer/char_iterator.h
Executable file
@@ -0,0 +1,53 @@
|
||||
// Copyright (C) 2022 Check Point Software Technologies Ltd. All rights reserved.
|
||||
|
||||
// Licensed under the Apache License, Version 2.0 (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.
|
||||
|
||||
#ifndef __BUFFER_CHAR_ITERATOR_H__
|
||||
#define __BUFFER_CHAR_ITERATOR_H__
|
||||
|
||||
#include <iterator>
|
||||
|
||||
class Buffer::CharIterator
|
||||
{
|
||||
public:
|
||||
using value_type = u_char;
|
||||
|
||||
CharIterator() {}
|
||||
void operator++();
|
||||
void operator++(int) { ++(*this); }
|
||||
void operator+=(uint);
|
||||
CharIterator operator+(uint) const;
|
||||
bool operator==(const CharIterator &other) const;
|
||||
bool operator!=(const CharIterator &other) const { return !((*this)==other); }
|
||||
const u_char & operator*() const;
|
||||
|
||||
private:
|
||||
friend class Buffer;
|
||||
CharIterator(const SegIterator &_cur, const SegIterator &_end, uint _offset);
|
||||
CharIterator(const SegIterator &_end);
|
||||
SegIterator cur_seg, end_seg;
|
||||
const u_char *ptr = nullptr;
|
||||
uint offset = 0, size = 0;
|
||||
};
|
||||
|
||||
namespace std
|
||||
{
|
||||
|
||||
template <>
|
||||
struct iterator_traits<Buffer::CharIterator>
|
||||
{
|
||||
using value_type = u_char;
|
||||
};
|
||||
|
||||
}; // namespace std
|
||||
|
||||
#endif // __BUFFER_CHAR_ITERATOR_H__
|
71
core/include/general/buffer/data_container.h
Executable file
71
core/include/general/buffer/data_container.h
Executable file
@@ -0,0 +1,71 @@
|
||||
// Copyright (C) 2022 Check Point Software Technologies Ltd. All rights reserved.
|
||||
|
||||
// Licensed under the Apache License, Version 2.0 (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.
|
||||
|
||||
#ifndef __BUFFER_DATA_CONTAINER_H__
|
||||
#define __BUFFER_DATA_CONTAINER_H__
|
||||
|
||||
class Buffer::DataContainer
|
||||
{
|
||||
public:
|
||||
DataContainer() : ptr(nullptr), len(0) {}
|
||||
DataContainer(std::vector<u_char> &&_vec);
|
||||
DataContainer(const u_char *_ptr, uint _len, MemoryType _type);
|
||||
DataContainer(const DataContainer &) = delete;
|
||||
DataContainer(DataContainer &&) = delete;
|
||||
const u_char * data() const { return ptr; }
|
||||
uint size() const { return len; }
|
||||
|
||||
// The "checkOnwership" method returns the place where the current memory type is owned by the I/S.
|
||||
// This makes it possible to check if the memory has been moved (from VOLATILE to ONWED).
|
||||
bool * checkOnwership() { return &is_owned; }
|
||||
|
||||
void
|
||||
takeOwnership()
|
||||
{
|
||||
vec = std::vector<u_char>(ptr, ptr + len);
|
||||
ptr = vec.data();
|
||||
is_owned = true;
|
||||
}
|
||||
|
||||
template<class Archive>
|
||||
void
|
||||
save(Archive &ar, uint32_t) const
|
||||
{
|
||||
if (is_owned) {
|
||||
ar(vec);
|
||||
} else {
|
||||
std::vector<u_char> data(ptr, ptr + len);
|
||||
ar(data);
|
||||
}
|
||||
}
|
||||
|
||||
template<class Archive>
|
||||
void
|
||||
load(Archive &ar, uint32_t)
|
||||
{
|
||||
ar(vec);
|
||||
is_owned = true;
|
||||
ptr = vec.data();
|
||||
len = vec.size();
|
||||
}
|
||||
|
||||
private:
|
||||
// If the memory is OWNED (not STATIC or VOLATILE), the "vec" member is holding it - otherwise it is empty.
|
||||
std::vector<u_char> vec;
|
||||
// The "ptr" member points to the the beginning of the data, regardless of the type of memory.
|
||||
const u_char *ptr = nullptr;
|
||||
uint len = 0;
|
||||
bool is_owned = true;
|
||||
};
|
||||
|
||||
#endif // __BUFFER_DATA_CONTAINER_H__
|
46
core/include/general/buffer/helper_functions.h
Executable file
46
core/include/general/buffer/helper_functions.h
Executable file
@@ -0,0 +1,46 @@
|
||||
// Copyright (C) 2022 Check Point Software Technologies Ltd. All rights reserved.
|
||||
|
||||
// Licensed under the Apache License, Version 2.0 (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.
|
||||
|
||||
#ifndef __BUFFER_HELPER_FUNCTIONS_H__
|
||||
#define __BUFFER_HELPER_FUNCTIONS_H__
|
||||
|
||||
// Function to allow comparison with types that have data (of types char or u_char) and size
|
||||
template <typename T>
|
||||
bool
|
||||
operator==(const Buffer &buf, const T &t)
|
||||
{
|
||||
return buf.isEqual(t.data(), t.size());
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
bool
|
||||
operator==(const T &t, const Buffer &buf)
|
||||
{
|
||||
return buf.isEqual(t.data(), t.size());
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
bool
|
||||
operator!=(const Buffer &buf, const T &t)
|
||||
{
|
||||
return !buf.isEqual(t.data(), t.size());
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
bool
|
||||
operator!=(const T &t, const Buffer &buf)
|
||||
{
|
||||
return !buf.isEqual(t.data(), t.size());
|
||||
}
|
||||
|
||||
#endif // __BUFFER_HELPER_FUNCTIONS_H__
|
53
core/include/general/buffer/internal_ptr.h
Executable file
53
core/include/general/buffer/internal_ptr.h
Executable file
@@ -0,0 +1,53 @@
|
||||
// Copyright (C) 2022 Check Point Software Technologies Ltd. All rights reserved.
|
||||
|
||||
// Licensed under the Apache License, Version 2.0 (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.
|
||||
|
||||
#ifndef __BUFFER_INTERNAL_PTR_H__
|
||||
#define __BUFFER_INTERNAL_PTR_H__
|
||||
|
||||
template <typename T>
|
||||
class Buffer::InternalPtr
|
||||
{
|
||||
public:
|
||||
InternalPtr(const InternalPtr &) = default;
|
||||
InternalPtr(InternalPtr &&o) : ptr(o.ptr), ref(std::move(o.ref)) { o.ptr = nullptr; }
|
||||
InternalPtr & operator=(const InternalPtr &) = default;
|
||||
InternalPtr &
|
||||
operator=(InternalPtr &&o)
|
||||
{
|
||||
ptr = o.ptr;
|
||||
ref = std::move(o.ref);
|
||||
o.ptr = nullptr;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
operator const T *() const { return ptr; }
|
||||
const T & operator*() const { dbgAssert(ptr != nullptr) << "Accessing a moved pointer"; return *ptr; }
|
||||
const T * operator->() const { return ptr; }
|
||||
|
||||
private:
|
||||
friend class Buffer;
|
||||
|
||||
InternalPtr(const T *_ptr, const std::shared_ptr<DataContainer> &data) : ptr(_ptr), ref(data) {}
|
||||
template<typename O>
|
||||
InternalPtr(InternalPtr<O> &&other)
|
||||
:
|
||||
ptr(reinterpret_cast<const T *>(other.ptr)),
|
||||
ref(std::move(other.ref))
|
||||
{}
|
||||
|
||||
const T *ptr;
|
||||
std::shared_ptr<DataContainer> ref;
|
||||
};
|
||||
|
||||
#endif // __BUFFER_INTERNAL_PTR_H__
|
75
core/include/general/buffer/segment.h
Executable file
75
core/include/general/buffer/segment.h
Executable file
@@ -0,0 +1,75 @@
|
||||
// Copyright (C) 2022 Check Point Software Technologies Ltd. All rights reserved.
|
||||
|
||||
// Licensed under the Apache License, Version 2.0 (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.
|
||||
|
||||
#ifndef __BUFFER_SEGMENT_H__
|
||||
#define __BUFFER_SEGMENT_H__
|
||||
|
||||
class Buffer::Segment
|
||||
{
|
||||
public:
|
||||
Segment() {}
|
||||
Segment(std::vector<u_char> &&_vec);
|
||||
Segment(const u_char *_ptr, uint _len, MemoryType _type);
|
||||
~Segment();
|
||||
Segment(const Segment &);
|
||||
Segment(Segment &&);
|
||||
Segment & operator=(const Segment &);
|
||||
Segment & operator=(Segment &&);
|
||||
|
||||
const u_char * data() const;
|
||||
uint size() const { return len; }
|
||||
|
||||
template<class Archive>
|
||||
void
|
||||
save(Archive &ar, uint32_t) const
|
||||
{
|
||||
ar(data_container, offset, len);
|
||||
}
|
||||
|
||||
template<class Archive>
|
||||
void
|
||||
load(Archive &ar, uint32_t)
|
||||
{
|
||||
// In the usual case, the `load` method is called on a newly default constructed object.
|
||||
// However, since there is no guarantee that will always be the case, we need to make sure to handle the case
|
||||
// where the object the data is loaded to is currently used as a PRIMARY.
|
||||
if (type==Volatility::PRIMARY && !data_container.unique()) {
|
||||
data_container->takeOwnership();
|
||||
}
|
||||
|
||||
ar(data_container, offset, len);
|
||||
|
||||
type = Volatility::NONE;
|
||||
is_owned = nullptr;
|
||||
ptr = data_container->data() + offset;
|
||||
}
|
||||
|
||||
private:
|
||||
friend class Buffer;
|
||||
|
||||
// The "data_container" is the smart pointer to the actual memory.
|
||||
std::shared_ptr<DataContainer> data_container;
|
||||
// The "offset" and "len" members are used to indicate what part of the shared memory the segment refers to.
|
||||
uint offset = 0, len = 0;
|
||||
|
||||
// The "type" member holds the volatility status of the memory.
|
||||
Volatility type = Volatility::NONE;
|
||||
// The "is_owned" member is used in case of SECONDARY volatility to check if ownership of the memory was taken.
|
||||
// It a pointer to `data_container->is_owned` if the segement is SECONDARY, and nullptr if it isn't.
|
||||
bool *is_owned = nullptr;
|
||||
// The "ptr" member is used to gain access to the memory directly without going to through the shared memory
|
||||
// pointer (fast path).
|
||||
const u_char *ptr = nullptr;
|
||||
};
|
||||
|
||||
#endif // __BUFFER_SEGMENT_H__
|
Reference in New Issue
Block a user