[4] | 1 | /**
|
---|
| 2 | * @file bin.cpp
|
---|
| 3 | * @author myusgun@gmail.com
|
---|
| 4 | * @brief bin
|
---|
| 5 | */
|
---|
| 6 | #include "cf/bin.h"
|
---|
| 7 |
|
---|
| 8 | #include <stdlib.h>
|
---|
| 9 | #include <string.h>
|
---|
| 10 |
|
---|
| 11 | cf::bin::bin(const cf::uint8_t * in,
|
---|
[7] | 12 | const cf::size_t length)
|
---|
[4] | 13 | {
|
---|
[7] | 14 | set(in, length);
|
---|
[4] | 15 | }
|
---|
| 16 |
|
---|
[11] | 17 | cf::bin::bin(const std::string & in)
|
---|
| 18 | : mBin(in.begin(), in.end())
|
---|
[4] | 19 | {
|
---|
| 20 | }
|
---|
| 21 |
|
---|
| 22 | cf::bin::bin(const bin & in)
|
---|
| 23 | : mBin(in.mBin)
|
---|
| 24 | {
|
---|
| 25 | }
|
---|
| 26 |
|
---|
| 27 | cf::void_t cf::bin::clear()
|
---|
| 28 | {
|
---|
| 29 | mBin.clear();
|
---|
| 30 | }
|
---|
| 31 |
|
---|
| 32 | cf::uint8_t * cf::bin::buffer() const
|
---|
| 33 | {
|
---|
| 34 | return const_cast<cf::uint8_t *>(&mBin[0]);
|
---|
| 35 | }
|
---|
| 36 |
|
---|
| 37 | cf::size_t cf::bin::size() const
|
---|
| 38 | {
|
---|
[18] | 39 | return static_cast<cf::size_t>(mBin.size());
|
---|
[4] | 40 | }
|
---|
| 41 |
|
---|
| 42 | cf::void_t cf::bin::resize(const cf::size_t size)
|
---|
| 43 | {
|
---|
| 44 | mBin.resize(size);
|
---|
| 45 | }
|
---|
| 46 |
|
---|
| 47 | cf::void_t cf::bin::set(const cf::uint8_t * in,
|
---|
[7] | 48 | const cf::size_t length)
|
---|
[4] | 49 | {
|
---|
| 50 | if (!in)
|
---|
| 51 | return;
|
---|
| 52 |
|
---|
[7] | 53 | resize(length);
|
---|
| 54 | memcpy(buffer(), in, length);
|
---|
[4] | 55 | }
|
---|
| 56 |
|
---|
| 57 | cf::void_t cf::bin::append(const cf::uint8_t * in,
|
---|
| 58 | const cf::size_t appendedSize)
|
---|
| 59 | {
|
---|
| 60 | const cf::size_t position = size();
|
---|
| 61 |
|
---|
| 62 | resize(size() + appendedSize);
|
---|
| 63 | memcpy(&buffer()[position], in, appendedSize);
|
---|
| 64 | }
|
---|
| 65 |
|
---|
| 66 | cf::void_t cf::bin::append(const cf::bin & in)
|
---|
| 67 | {
|
---|
[10] | 68 | append(in.buffer(), in.size());
|
---|
[4] | 69 | }
|
---|
| 70 |
|
---|
| 71 | cf::bool_t cf::bin::equal(const cf::bin & in) const
|
---|
| 72 | {
|
---|
| 73 | if (size() != in.size())
|
---|
| 74 | return false;
|
---|
| 75 |
|
---|
| 76 | if (size() > 0)
|
---|
| 77 | {
|
---|
| 78 | if (memcmp(buffer(), in.buffer(), size()))
|
---|
| 79 | return false;
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | return true;
|
---|
| 83 | }
|
---|
| 84 |
|
---|
| 85 | cf::bin & cf::bin::operator =(const cf::bin & in)
|
---|
| 86 | {
|
---|
| 87 | mBin = in.mBin;
|
---|
| 88 | return *this;
|
---|
| 89 | }
|
---|
| 90 |
|
---|
[11] | 91 | cf::bin & cf::bin::operator =(const std::string & in)
|
---|
| 92 | {
|
---|
| 93 | mBin.assign(in.begin(), in.end());
|
---|
| 94 | return *this;
|
---|
| 95 | }
|
---|
| 96 |
|
---|
[4] | 97 | cf::void_t cf::bin::operator +=(const cf::bin & in)
|
---|
| 98 | {
|
---|
| 99 | append(in);
|
---|
| 100 | }
|
---|
| 101 |
|
---|
| 102 | cf::bin cf::bin::operator +(const cf::bin & in) const
|
---|
| 103 | {
|
---|
[24] | 104 | bin out(*this);
|
---|
[4] | 105 |
|
---|
| 106 | out.append(in);
|
---|
| 107 |
|
---|
| 108 | return out;
|
---|
| 109 | }
|
---|
| 110 |
|
---|
| 111 | cf::bool_t cf::bin::operator ==(const cf::bin & in) const
|
---|
| 112 | {
|
---|
| 113 | return equal(in);
|
---|
| 114 | }
|
---|
| 115 |
|
---|
| 116 | cf::bool_t cf::bin::operator !=(const cf::bin & in) const
|
---|
| 117 | {
|
---|
| 118 | return !equal(in);
|
---|
| 119 | }
|
---|
| 120 |
|
---|
[11] | 121 | cf::uint8_t cf::bin::operator [](const cf::size_t index) const
|
---|
| 122 | {
|
---|
| 123 | if (index > size())
|
---|
| 124 | THROW_EXCEPTION("invalid index");
|
---|
| 125 |
|
---|
| 126 | return mBin[index];
|
---|
| 127 | }
|
---|
| 128 |
|
---|
[4] | 129 | cf::size_t cf::bin::find(const cf::uint8_t * in,
|
---|
[7] | 130 | const cf::size_t length) const
|
---|
[4] | 131 | {
|
---|
[7] | 132 | cf::size_t limit = size() - length;
|
---|
[4] | 133 |
|
---|
| 134 | if (limit < 0 || !in)
|
---|
| 135 | return -1;
|
---|
| 136 |
|
---|
[7] | 137 | for (cf::size_t iter = 0 ; iter <= limit ; iter++)
|
---|
[4] | 138 | {
|
---|
[7] | 139 | if (!memcmp(buffer() + iter, in, length))
|
---|
[4] | 140 | return iter;
|
---|
| 141 | }
|
---|
| 142 |
|
---|
| 143 | return -1;
|
---|
| 144 | }
|
---|
| 145 |
|
---|
[11] | 146 | std::string cf::bin::toString() const
|
---|
| 147 | {
|
---|
| 148 | return std::string(mBin.begin(), mBin.end());
|
---|
| 149 | }
|
---|
| 150 |
|
---|
[4] | 151 | cf::void_t cf::bin::print(const cf::char_t * prefix,
|
---|
| 152 | const cf::size_t limit,
|
---|
| 153 | FILE * fp) const
|
---|
| 154 | {
|
---|
| 155 | cf::size_t i, j;
|
---|
| 156 | cf::size_t realsize = size() < limit ? size() : limit;
|
---|
| 157 |
|
---|
| 158 | fprintf(fp, "[bin][%s] %lu bytes\n", prefix, size());
|
---|
| 159 | for (i = 0 ; i < realsize ; i += 16)
|
---|
| 160 | {
|
---|
| 161 | fprintf(fp, "%06lx : ", (cf::size_t)i);
|
---|
| 162 |
|
---|
| 163 | for (j = 0; j < 16; j++)
|
---|
| 164 | {
|
---|
| 165 | if (i + j < realsize)
|
---|
| 166 | fprintf(fp, "%02x ", buffer()[i + j]);
|
---|
| 167 | else
|
---|
| 168 | fprintf(fp, " ");
|
---|
| 169 | }
|
---|
| 170 | fprintf(fp, " ");
|
---|
| 171 |
|
---|
| 172 | for (j = 0 ; j < 16 ; j++)
|
---|
| 173 | {
|
---|
| 174 | #define IS_READABLE_CHAR(__c) (' ' <= __c && __c <= '~')
|
---|
| 175 | if (i+j < realsize)
|
---|
| 176 | fprintf(fp, "%c", IS_READABLE_CHAR(buffer()[i + j]) ? buffer()[i + j] : '.');
|
---|
| 177 | }
|
---|
| 178 | fprintf(fp, "\n");
|
---|
| 179 | }
|
---|
| 180 | }
|
---|