[4] | 1 | /**
|
---|
| 2 | * @file bin.h
|
---|
| 3 | * @author myusgun@gmail.com
|
---|
| 4 | * @brief bin
|
---|
| 5 | */
|
---|
| 6 | #ifndef __bin_h__
|
---|
| 7 | #define __bin_h__
|
---|
| 8 |
|
---|
| 9 | #include "cf/types.h"
|
---|
| 10 | #include "cf/exception.h"
|
---|
| 11 |
|
---|
| 12 | #include <vector>
|
---|
[11] | 13 | #include <string>
|
---|
[4] | 14 | #include <stdio.h>
|
---|
| 15 |
|
---|
| 16 | namespace cf
|
---|
| 17 | {
|
---|
| 18 | /**
|
---|
| 19 | * binary
|
---|
| 20 | */
|
---|
| 21 | class bin
|
---|
| 22 | {
|
---|
| 23 | private:
|
---|
| 24 | typedef std::vector<cf::uint8_t> bin_t;
|
---|
| 25 |
|
---|
| 26 | bin_t mBin;
|
---|
| 27 |
|
---|
| 28 | public:
|
---|
| 29 | /**
|
---|
| 30 | * shallow-copy constructor(default constructor)
|
---|
| 31 | * @param in binary
|
---|
[7] | 32 | * @param length binary size
|
---|
[4] | 33 | */
|
---|
| 34 | bin(const cf::uint8_t * in = NULL,
|
---|
[7] | 35 | const cf::size_t length = 0);
|
---|
[4] | 36 |
|
---|
| 37 | /**
|
---|
| 38 | * asciiz-byte shallow-copy constructor
|
---|
| 39 | * @param in asciiz-byte
|
---|
| 40 | */
|
---|
[11] | 41 | bin(const std::string & in);
|
---|
[4] | 42 |
|
---|
| 43 | /**
|
---|
| 44 | * copy constructor
|
---|
| 45 | * @param in an instance of bin(not constant)
|
---|
| 46 | */
|
---|
| 47 | bin(const bin & in);
|
---|
| 48 |
|
---|
| 49 | /**
|
---|
| 50 | * reset binary. fill with zero
|
---|
| 51 | */
|
---|
| 52 | cf::void_t clear();
|
---|
| 53 |
|
---|
| 54 | /**
|
---|
| 55 | * get binary
|
---|
| 56 | * @return address of binary
|
---|
| 57 | */
|
---|
| 58 | cf::uint8_t * buffer() const;
|
---|
| 59 |
|
---|
| 60 | /**
|
---|
| 61 | * get binary size
|
---|
| 62 | * @return size of binary
|
---|
| 63 | */
|
---|
| 64 | cf::size_t size() const;
|
---|
| 65 |
|
---|
| 66 | /**
|
---|
| 67 | * is empty
|
---|
| 68 | * @return is empty ?
|
---|
| 69 | */
|
---|
| 70 | cf::bool_t empty() const;
|
---|
| 71 |
|
---|
| 72 | /**
|
---|
| 73 | * resize
|
---|
| 74 | * @param size new size
|
---|
| 75 | */
|
---|
| 76 | cf::void_t resize(const cf::size_t size);
|
---|
| 77 |
|
---|
| 78 | /**
|
---|
| 79 | * set
|
---|
| 80 | * @param in data
|
---|
[7] | 81 | * @param length size
|
---|
[4] | 82 | */
|
---|
| 83 | cf::void_t set(const cf::uint8_t * in,
|
---|
[7] | 84 | const cf::size_t length);
|
---|
[4] | 85 |
|
---|
| 86 | /**
|
---|
| 87 | * copy to rear of buffer
|
---|
| 88 | * @param in binary
|
---|
| 89 | * @param appendedSize binary size
|
---|
| 90 | */
|
---|
| 91 | cf::void_t append(const cf::uint8_t * in,
|
---|
| 92 | const cf::size_t appendedSize);
|
---|
| 93 |
|
---|
| 94 | /**
|
---|
| 95 | * copy to rear of buffer
|
---|
| 96 | * @param in bin instance
|
---|
| 97 | * @see bin::operator +=()
|
---|
| 98 | */
|
---|
| 99 | cf::void_t append(const bin & in);
|
---|
| 100 |
|
---|
| 101 | /**
|
---|
| 102 | * compare binary
|
---|
| 103 | * @return true; false
|
---|
| 104 | * @param in bin instance
|
---|
| 105 | * @see bin::operator ==()
|
---|
| 106 | */
|
---|
| 107 | cf::bool_t equal(const bin & in) const;
|
---|
| 108 |
|
---|
| 109 | /**
|
---|
| 110 | * find in binary
|
---|
| 111 | * @return on success, position; otherwise, (cf::size_t)-1
|
---|
| 112 | * @param in binary
|
---|
[7] | 113 | * @param length binary size
|
---|
[4] | 114 | */
|
---|
| 115 | cf::size_t find(const cf::uint8_t * in,
|
---|
[7] | 116 | const cf::size_t length) const;
|
---|
[4] | 117 |
|
---|
| 118 | /**
|
---|
[11] | 119 | * set
|
---|
[4] | 120 | * @param in bin
|
---|
| 121 | */
|
---|
| 122 | bin & operator =(const bin & in);
|
---|
| 123 |
|
---|
| 124 | /**
|
---|
[11] | 125 | * set
|
---|
| 126 | * @param in string
|
---|
| 127 | */
|
---|
| 128 | bin & operator =(const std::string & in);
|
---|
| 129 |
|
---|
| 130 | /**
|
---|
[4] | 131 | * copy to rear of buffer
|
---|
| 132 | * @param in bin instance
|
---|
| 133 | * @see bin::append()
|
---|
| 134 | */
|
---|
| 135 | cf::void_t operator +=(const bin & in);
|
---|
| 136 |
|
---|
| 137 | /**
|
---|
| 138 | * combine binary
|
---|
| 139 | * @return combined instance
|
---|
| 140 | */
|
---|
| 141 | bin operator +(const bin & in) const;
|
---|
| 142 |
|
---|
| 143 | /**
|
---|
| 144 | * compare binary
|
---|
| 145 | * @return true; false
|
---|
| 146 | * @param in bin instance
|
---|
| 147 | * @see bin::equal()
|
---|
| 148 | */
|
---|
| 149 | cf::bool_t operator ==(const bin & in) const;
|
---|
| 150 |
|
---|
| 151 | /**
|
---|
| 152 | * compare binary
|
---|
| 153 | * @return true; false
|
---|
| 154 | * @param in bin instance
|
---|
| 155 | * @see bin::equal()
|
---|
| 156 | */
|
---|
| 157 | cf::bool_t operator !=(const bin & in) const;
|
---|
| 158 |
|
---|
| 159 | /**
|
---|
[11] | 160 | * get a byte
|
---|
| 161 | * @return a byte
|
---|
| 162 | * @param index index
|
---|
| 163 | */
|
---|
| 164 | cf::uint8_t operator [](const cf::size_t index) const;
|
---|
| 165 |
|
---|
| 166 | /**
|
---|
| 167 | * get as string
|
---|
| 168 | * @return string
|
---|
| 169 | */
|
---|
| 170 | std::string toString() const;
|
---|
| 171 |
|
---|
| 172 | /**
|
---|
[4] | 173 | * dump binary to file-pointer
|
---|
| 174 | * @param prefix prefix for output
|
---|
| 175 | * @param limit [option] limit
|
---|
| 176 | * @param fp [option] file-pointer
|
---|
| 177 | */
|
---|
| 178 | cf::void_t print(const cf::char_t * prefix = "",
|
---|
| 179 | const cf::size_t limit = -1,
|
---|
| 180 | FILE * fp = stdout) const;
|
---|
| 181 | };
|
---|
| 182 | }
|
---|
| 183 |
|
---|
| 184 | #endif // #ifndef __bin_h__
|
---|