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