forked from brett19/php-couchbase
-
Notifications
You must be signed in to change notification settings - Fork 38
/
datainfo.h
197 lines (178 loc) · 4.49 KB
/
datainfo.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/**
* Copyright 2016-2019 Couchbase, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef DATATYPE_H_
#define DATATYPE_H_
enum {
FMT_RESERVED = 0,
FMT_PRIVATE = 1,
FMT_JSON = 2,
FMT_BINARY = 3,
FMT_UTF8 = 4,
FMT_MAX_VAL = 255,
// PHP Private Types
FMT_PHPSER,
FMT_IGBINARY,
FMT_STRING,
FMT_LONG,
FMT_DOUBLE,
FMT_BOOL
} format_t;
enum {
CPR_NONE = 0,
CPR_GZIP = 1,
CPR_BZIP = 2,
CPR_LZO = 3,
CPR_SNAPPY = 4,
CPR_FASTLZ = 5,
CPR_ZLIB = 6,
CPR_MAX_VAL = 255
} compression_t;
enum {
OLDFMT_STRING = 0,
OLDFMT_LONG = 1,
OLDFMT_DOUBLE = 2,
OLDFMT_BOOL = 3,
OLDFMT_SERIALIZED = 4,
OLDFMT_IGBINARY = 5,
OLDFMT_JSON = 6
} oldformat_t;
enum { OLDCPR_NONE = 0, OLDCPR_ZLIB = 1, OLDCPR_FASTLZ = 2 } oldcompression_t;
typedef struct datainfo_st {
unsigned short format;
unsigned short compression;
} datainfo_t;
static lcb_uint32_t make_oldflags(datainfo_t dt)
{
lcb_uint32_t out = 0;
switch (dt.format) {
case FMT_STRING:
out |= ((OLDFMT_STRING << 0) & 0x000F);
break;
case FMT_LONG:
out |= ((OLDFMT_LONG << 0) & 0x000F);
break;
case FMT_DOUBLE:
out |= ((OLDFMT_DOUBLE << 0) & 0x000F);
break;
case FMT_BOOL:
out |= ((OLDFMT_BOOL << 0) & 0x000F);
break;
case FMT_PHPSER:
out |= ((OLDFMT_SERIALIZED << 0) & 0x000F);
break;
case FMT_IGBINARY:
out |= ((OLDFMT_IGBINARY << 0) & 0x000F);
break;
case FMT_JSON:
out |= ((OLDFMT_JSON << 0) & 0x000F);
break;
}
switch (dt.compression) {
case CPR_NONE:
out |= ((OLDCPR_NONE << 5) & 0x00E0);
break;
case CPR_ZLIB:
out |= ((OLDCPR_ZLIB << 5) & 0x00E0);
break;
case CPR_FASTLZ:
out |= ((OLDCPR_ZLIB << 5) & 0x00E0);
break;
}
out |= ((dt.format << 16) & 0x00FF0000);
return out;
}
static datainfo_t parse_oldflags(lcb_uint32_t flags)
{
lcb_uint32_t format = (flags & 0x000F) >> 0;
lcb_uint32_t compression = (flags & 0x00E0) >> 5;
datainfo_t out = {0, 0};
switch (format) {
case OLDFMT_STRING:
out.format = FMT_STRING;
break;
case OLDFMT_LONG:
out.format = FMT_LONG;
break;
case OLDFMT_DOUBLE:
out.format = FMT_DOUBLE;
break;
case OLDFMT_BOOL:
out.format = FMT_BOOL;
break;
case OLDFMT_SERIALIZED:
out.format = FMT_PHPSER;
break;
case OLDFMT_IGBINARY:
out.format = FMT_IGBINARY;
break;
case OLDFMT_JSON:
out.format = FMT_JSON;
break;
}
switch (compression) {
case OLDCPR_NONE:
out.compression = CPR_NONE;
break;
case OLDCPR_ZLIB:
out.compression = CPR_ZLIB;
break;
case OLDCPR_FASTLZ:
out.compression = CPR_FASTLZ;
break;
}
return out;
}
static uint8_t make_datatype(datainfo_t dt)
{
uint8_t value;
if (dt.format > FMT_MAX_VAL) {
value |= ((FMT_PRIVATE & 0x7) << 0);
} else {
value |= ((dt.format & 0x7) << 0);
}
value |= ((dt.compression & 0x7) << 5);
return value;
}
static datainfo_t parse_datatype(uint8_t datatype)
{
datainfo_t out;
out.format = ((datatype >> 0) & 0x7);
out.compression = ((datatype >> 5) & 0x7);
return out;
}
static lcb_uint32_t make_flags(datainfo_t dt)
{
lcb_uint32_t flags = 0;
flags |= (make_oldflags(dt) << 0) & 0x000000FF;
flags |= (make_datatype(dt) << 24) & 0xFF000000;
return flags;
}
static datainfo_t get_datainfo(lcb_uint32_t flags, uint8_t datatype)
{
datainfo_t out;
out = parse_datatype(datatype);
if (out.format == 0) {
out = parse_oldflags(flags);
} else if (out.format == FMT_PRIVATE) {
out.format = ((flags & 0x00FF0000) >> 16);
}
if (out.format == 0) {
out.format = FMT_BINARY;
out.compression = CPR_NONE;
}
return out;
}
#endif /* DATATYPE_H_ */