2018-12-12 01:50:03 +00:00
|
|
|
#ifndef LLARP_MEM_HPP
|
|
|
|
#define LLARP_MEM_HPP
|
2019-01-10 19:41:51 +00:00
|
|
|
|
2019-02-02 23:12:42 +00:00
|
|
|
#include <util/buffer.hpp>
|
2019-01-10 19:41:51 +00:00
|
|
|
#include <util/mem.h>
|
2018-12-12 02:52:51 +00:00
|
|
|
|
2018-12-12 01:50:03 +00:00
|
|
|
#include <cctype>
|
|
|
|
#include <cstdio>
|
|
|
|
#include <memory>
|
|
|
|
|
|
|
|
namespace llarp
|
|
|
|
{
|
|
|
|
void
|
2020-04-07 18:38:56 +00:00
|
|
|
Zero(void* ptr, size_t sz);
|
2018-12-12 01:50:03 +00:00
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
template <typename T>
|
2018-12-12 01:50:03 +00:00
|
|
|
void
|
2020-04-07 18:38:56 +00:00
|
|
|
dumphex(const uint8_t* t)
|
2018-12-12 01:50:03 +00:00
|
|
|
{
|
|
|
|
size_t idx = 0;
|
2020-04-07 18:38:56 +00:00
|
|
|
while (idx < sizeof(T))
|
2018-12-12 01:50:03 +00:00
|
|
|
{
|
|
|
|
printf("%.2x ", t[idx++]);
|
2020-04-07 18:38:56 +00:00
|
|
|
if (idx % 8 == 0)
|
2018-12-12 01:50:03 +00:00
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
template <typename T, size_t align = 128>
|
2018-12-12 01:50:03 +00:00
|
|
|
void
|
2020-04-07 18:38:56 +00:00
|
|
|
DumpBufferHex(const T& buff)
|
2018-12-12 01:50:03 +00:00
|
|
|
{
|
|
|
|
size_t idx = 0;
|
|
|
|
printf("buffer of size %zu\n", buff.sz);
|
2020-04-07 18:38:56 +00:00
|
|
|
while (idx < buff.sz)
|
2018-12-12 01:50:03 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (buff.base + idx == buff.cur)
|
2018-12-12 01:50:03 +00:00
|
|
|
{
|
|
|
|
#ifndef _WIN32
|
|
|
|
printf("%c[1;31m", 27);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
printf("%.2x", buff.base[idx]);
|
2020-04-07 18:38:56 +00:00
|
|
|
if (buff.base + idx == buff.cur)
|
2018-12-12 01:50:03 +00:00
|
|
|
{
|
|
|
|
#ifndef _WIN32
|
|
|
|
printf("%c[0;0m", 27);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
++idx;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (idx % align == 0)
|
2018-12-12 01:50:03 +00:00
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
fflush(stdout);
|
|
|
|
}
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
template <typename T, size_t align = 128>
|
2018-12-12 01:50:03 +00:00
|
|
|
void
|
2020-04-07 18:38:56 +00:00
|
|
|
DumpBuffer(const T& buff)
|
2018-12-12 01:50:03 +00:00
|
|
|
{
|
|
|
|
size_t idx = 0;
|
|
|
|
printf("buffer of size %zu\n", buff.sz);
|
2020-04-07 18:38:56 +00:00
|
|
|
while (idx < buff.sz)
|
2018-12-12 01:50:03 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (buff.base + idx == buff.cur)
|
2018-12-12 01:50:03 +00:00
|
|
|
{
|
|
|
|
#ifndef _WIN32
|
|
|
|
printf("%c[1;31m", 27);
|
|
|
|
#endif
|
|
|
|
}
|
2020-04-07 18:38:56 +00:00
|
|
|
if (std::isprint(buff.base[idx]))
|
2018-12-12 01:50:03 +00:00
|
|
|
{
|
|
|
|
printf("%c", buff.base[idx]);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
printf(".");
|
|
|
|
}
|
2020-04-07 18:38:56 +00:00
|
|
|
if (buff.base + idx == buff.cur)
|
2018-12-12 01:50:03 +00:00
|
|
|
{
|
|
|
|
#ifndef _WIN32
|
|
|
|
printf("%c[0;0m", 27);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
++idx;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (idx % align == 0)
|
2018-12-12 01:50:03 +00:00
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
fflush(stdout);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace llarp
|
|
|
|
|
|
|
|
#endif
|