986fc60e9b2901414c2202be93608ce4972a8f11
[platform/core/connectivity/smartcard-service.git] / common / include / ByteArray.h
1 /*
2  * Copyright (c) 2012, 2013 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #ifndef BYTEARRAY_H_
18 #define BYTEARRAY_H_
19
20 /* standard library header */
21 #include <string>
22 #include <stdint.h>
23 #include <stddef.h>
24
25 /* SLP library header */
26
27 /* local header */
28 //#include "Serializable.h"
29
30 #define ARRAY_AND_SIZE(x) (uint8_t *)(&x), sizeof(x)
31
32 using namespace std;
33
34 namespace smartcard_service_api
35 {
36         class ByteArray //: public Serializable
37         {
38         protected:
39                 uint8_t *buffer;
40                 size_t length;
41
42                 bool _assign(uint8_t *array, size_t size);
43                 void save(const char *filePath);
44
45         public:
46                 static ByteArray EMPTY;
47
48                 ByteArray();
49                 ByteArray(const uint8_t *array, size_t size);
50                 ByteArray(const ByteArray &T);
51                 ~ByteArray();
52
53 //              ByteArray serialize();
54 //              void deserialize(ByteArray buffer);
55
56                 bool assign(const uint8_t *array, size_t size);
57                 inline bool setBuffer(const uint8_t *array, size_t size) { return assign(array, size); }
58                 void clear();
59
60                 size_t size() const;
61                 inline size_t getLength() const { return size(); }
62                 uint8_t *getBuffer();
63                 const uint8_t *getBuffer() const;
64                 uint8_t *getBuffer(size_t offset);
65                 const uint8_t *getBuffer(size_t offset) const;
66
67                 uint8_t at(size_t index) const;
68                 uint8_t reverseAt(size_t index) const;
69
70                 size_t extract(uint8_t *array, size_t size) const;
71
72                 const ByteArray sub(size_t offset, size_t size) const;
73
74                 /* operator overloading */
75                 ByteArray &operator =(const ByteArray &T);
76                 ByteArray operator +(const ByteArray &T);
77                 ByteArray &operator +=(const ByteArray &T);
78                 bool operator ==(const ByteArray &T) const;
79                 bool operator !=(const ByteArray &T) const;
80                 bool operator <(const ByteArray &T) const;
81                 bool operator >(const ByteArray &T) const;
82                 uint8_t operator[](size_t index) const;
83
84                 inline bool isEmpty() const { return (buffer == (void *)0 || length == 0); }
85                 const string toString(bool entire) const;
86                 const string toString() const;
87         };
88
89 } /* namespace smartcard_service_api */
90 #endif /* BYTEARRAY_H_ */