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