1 | /**
|
---|
2 | * @file bin.h
|
---|
3 | * @author myusgun@gmail.com
|
---|
4 | * @brief bin
|
---|
5 | */
|
---|
6 | #ifndef __cf_bin_h__
|
---|
7 | #define __cf_bin_h__
|
---|
8 |
|
---|
9 | #include "cf/types.h"
|
---|
10 | #include "cf/exception.h"
|
---|
11 |
|
---|
12 | #include <vector>
|
---|
13 | #include <string>
|
---|
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
|
---|
32 | * @param length binary size
|
---|
33 | */
|
---|
34 | bin(const cf::uint8_t * in = NULL,
|
---|
35 | const cf::size_t length = 0);
|
---|
36 |
|
---|
37 | /**
|
---|
38 | * asciiz-byte shallow-copy constructor
|
---|
39 | * @param in asciiz-byte
|
---|
40 | */
|
---|
41 | bin(const std::string & in);
|
---|
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 | * resize
|
---|
68 | * @param size new size
|
---|
69 | */
|
---|
70 | cf::void_t resize(const cf::size_t size);
|
---|
71 |
|
---|
72 | /**
|
---|
73 | * set
|
---|
74 | * @param in data
|
---|
75 | * @param length size
|
---|
76 | */
|
---|
77 | cf::void_t set(const cf::uint8_t * in,
|
---|
78 | const cf::size_t length);
|
---|
79 |
|
---|
80 | /**
|
---|
81 | * copy to rear of buffer
|
---|
82 | * @param in binary
|
---|
83 | * @param appendedSize binary size
|
---|
84 | */
|
---|
85 | cf::void_t append(const cf::uint8_t * in,
|
---|
86 | const cf::size_t appendedSize);
|
---|
87 |
|
---|
88 | /**
|
---|
89 | * copy to rear of buffer
|
---|
90 | * @param in bin instance
|
---|
91 | * @see bin::operator +=()
|
---|
92 | */
|
---|
93 | cf::void_t append(const bin & in);
|
---|
94 |
|
---|
95 | /**
|
---|
96 | * compare binary
|
---|
97 | * @return true; false
|
---|
98 | * @param in bin instance
|
---|
99 | * @see bin::operator ==()
|
---|
100 | */
|
---|
101 | cf::bool_t equal(const bin & in) const;
|
---|
102 |
|
---|
103 | /**
|
---|
104 | * find in binary
|
---|
105 | * @return on success, position; otherwise, (cf::size_t)-1
|
---|
106 | * @param in binary
|
---|
107 | * @param length binary size
|
---|
108 | */
|
---|
109 | cf::size_t find(const cf::uint8_t * in,
|
---|
110 | const cf::size_t length) const;
|
---|
111 |
|
---|
112 | /**
|
---|
113 | * set
|
---|
114 | * @param in bin
|
---|
115 | */
|
---|
116 | bin & operator =(const bin & in);
|
---|
117 |
|
---|
118 | /**
|
---|
119 | * set
|
---|
120 | * @param in string
|
---|
121 | */
|
---|
122 | bin & operator =(const std::string & in);
|
---|
123 |
|
---|
124 | /**
|
---|
125 | * copy to rear of buffer
|
---|
126 | * @param in bin instance
|
---|
127 | * @see bin::append()
|
---|
128 | */
|
---|
129 | cf::void_t operator +=(const bin & in);
|
---|
130 |
|
---|
131 | /**
|
---|
132 | * combine binary
|
---|
133 | * @return combined instance
|
---|
134 | */
|
---|
135 | bin operator +(const bin & in) const;
|
---|
136 |
|
---|
137 | /**
|
---|
138 | * compare binary
|
---|
139 | * @return true; false
|
---|
140 | * @param in bin instance
|
---|
141 | * @see bin::equal()
|
---|
142 | */
|
---|
143 | cf::bool_t operator ==(const bin & in) const;
|
---|
144 |
|
---|
145 | /**
|
---|
146 | * compare binary
|
---|
147 | * @return true; false
|
---|
148 | * @param in bin instance
|
---|
149 | * @see bin::equal()
|
---|
150 | */
|
---|
151 | cf::bool_t operator !=(const bin & in) const;
|
---|
152 |
|
---|
153 | /**
|
---|
154 | * get a byte
|
---|
155 | * @return a byte
|
---|
156 | * @param index index
|
---|
157 | */
|
---|
158 | cf::uint8_t operator [](const cf::size_t index) const;
|
---|
159 |
|
---|
160 | /**
|
---|
161 | * get as string
|
---|
162 | * @return string
|
---|
163 | */
|
---|
164 | std::string toString() const;
|
---|
165 |
|
---|
166 | /**
|
---|
167 | * dump binary to file-pointer
|
---|
168 | * @param prefix prefix for output
|
---|
169 | * @param limit [option] limit
|
---|
170 | * @param fp [option] file-pointer
|
---|
171 | */
|
---|
172 | cf::void_t print(const cf::char_t * prefix = "",
|
---|
173 | const cf::size_t limit = -1,
|
---|
174 | FILE * fp = stdout) const;
|
---|
175 | };
|
---|
176 | }
|
---|
177 |
|
---|
178 | #endif // #ifndef __cf_bin_h__
|
---|