/src/postgis/liblwgeom/varint.c
Line | Count | Source |
1 | | /********************************************************************** |
2 | | * |
3 | | * PostGIS - Spatial Types for PostgreSQL |
4 | | * http://postgis.net |
5 | | * |
6 | | * PostGIS is free software: you can redistribute it and/or modify |
7 | | * it under the terms of the GNU General Public License as published by |
8 | | * the Free Software Foundation, either version 2 of the License, or |
9 | | * (at your option) any later version. |
10 | | * |
11 | | * PostGIS is distributed in the hope that it will be useful, |
12 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | | * GNU General Public License for more details. |
15 | | * |
16 | | * You should have received a copy of the GNU General Public License |
17 | | * along with PostGIS. If not, see <http://www.gnu.org/licenses/>. |
18 | | * |
19 | | ********************************************************************** |
20 | | * |
21 | | * Copyright (C) 2014 Sandro Santilli <strk@kbt.io> |
22 | | * Copyright (C) 2013 Nicklas Avén |
23 | | * Copyright (C) 2026 Darafei Praliaskouski <me@komzpa.net> |
24 | | * |
25 | | **********************************************************************/ |
26 | | |
27 | | #include "varint.h" |
28 | | #include "lwgeom_log.h" |
29 | | #include "liblwgeom.h" |
30 | | |
31 | | /* -------------------------------------------------------------------------------- */ |
32 | | |
33 | | static size_t |
34 | | _varint_u64_encode_buf(uint64_t val, uint8_t *buf) |
35 | 0 | { |
36 | 0 | uint8_t grp; |
37 | 0 | uint64_t q = val; |
38 | 0 | uint8_t *ptr = buf; |
39 | 0 | while (1) |
40 | 0 | { |
41 | | /* We put the 7 least significant bits in grp */ |
42 | 0 | grp = 0x7f & q; |
43 | | /* We rightshift our input value 7 bits */ |
44 | | /* which means that the 7 next least significant bits */ |
45 | | /* becomes the 7 least significant */ |
46 | 0 | q = q >> 7; |
47 | | /* Check if, after our rightshifting, we still have */ |
48 | | /* anything to read in our input value. */ |
49 | 0 | if ( q > 0 ) |
50 | 0 | { |
51 | | /* In the next line quite a lot is happening. */ |
52 | | /* Since there is more to read in our input value */ |
53 | | /* we signal that by setting the most significant bit */ |
54 | | /* in our byte to 1. */ |
55 | | /* Then we put that byte in our buffer and move the pointer */ |
56 | | /* forward one step */ |
57 | 0 | *ptr = 0x80 | grp; |
58 | 0 | ptr++; |
59 | 0 | } |
60 | 0 | else |
61 | 0 | { |
62 | | /* The same as above, but since there is nothing more */ |
63 | | /* to read in our input value we leave the most significant bit unset */ |
64 | 0 | *ptr = grp; |
65 | 0 | ptr++; |
66 | 0 | return ptr - buf; |
67 | 0 | } |
68 | 0 | } |
69 | | /* This cannot happen */ |
70 | 0 | lwerror("%s: Got out of infinite loop. Consciousness achieved.", __func__); |
71 | 0 | return (size_t)0; |
72 | 0 | } |
73 | | |
74 | | |
75 | | size_t |
76 | | varint_u64_encode_buf(uint64_t val, uint8_t *buf) |
77 | 0 | { |
78 | 0 | return _varint_u64_encode_buf(val, buf); |
79 | 0 | } |
80 | | |
81 | | |
82 | | size_t |
83 | | varint_u32_encode_buf(uint32_t val, uint8_t *buf) |
84 | 0 | { |
85 | 0 | return _varint_u64_encode_buf((uint64_t)val, buf); |
86 | 0 | } |
87 | | |
88 | | size_t |
89 | | varint_s64_encode_buf(int64_t val, uint8_t *buf) |
90 | 0 | { |
91 | 0 | return _varint_u64_encode_buf(zigzag64(val), buf); |
92 | 0 | } |
93 | | |
94 | | size_t |
95 | | varint_s32_encode_buf(int32_t val, uint8_t *buf) |
96 | 0 | { |
97 | 0 | return _varint_u64_encode_buf((uint64_t)zigzag32(val), buf); |
98 | 0 | } |
99 | | |
100 | | /* Read from signed 64bit varint */ |
101 | | int64_t |
102 | | varint_s64_decode(const uint8_t *the_start, const uint8_t *the_end, size_t *size) |
103 | 1.32M | { |
104 | 1.32M | return unzigzag64(varint_u64_decode(the_start, the_end, size)); |
105 | 1.32M | } |
106 | | |
107 | | /* Read from unsigned 64bit varint */ |
108 | | uint64_t |
109 | | varint_u64_decode(const uint8_t *the_start, const uint8_t *the_end, size_t *size) |
110 | 1.71M | { |
111 | 1.71M | uint64_t nVal = 0; |
112 | 1.71M | int nShift = 0; |
113 | 1.71M | uint8_t nByte; |
114 | 1.71M | const uint8_t *ptr = the_start; |
115 | | |
116 | | /* Check so we don't read beyond the twkb */ |
117 | 1.75M | while( ptr < the_end ) |
118 | 1.75M | { |
119 | 1.75M | nByte = *ptr; |
120 | | /* Hibit is set, so this isn't the last byte */ |
121 | 1.75M | if (nByte & 0x80) |
122 | 41.2k | { |
123 | 41.2k | if (nShift >= 63) |
124 | 29 | { |
125 | 29 | *size = 0; |
126 | 29 | lwerror("%s: varint exceeds 64 bits", __func__); |
127 | 29 | return 0; |
128 | 29 | } |
129 | | /* We get here when there is more to read in the input varInt */ |
130 | | /* Here we take the least significant 7 bits of the read */ |
131 | | /* byte and put it in the most significant place in the result variable. */ |
132 | 41.2k | nVal |= ((uint64_t)(nByte & 0x7f)) << nShift; |
133 | | /* move the "cursor" of the input buffer step (8 bits) */ |
134 | 41.2k | ptr++; |
135 | | /* move the cursor in the resulting variable (7 bits) */ |
136 | 41.2k | nShift += 7; |
137 | 41.2k | } |
138 | 1.71M | else |
139 | 1.71M | { |
140 | 1.71M | if (nShift >= 64 || (nShift == 63 && nByte > 1)) |
141 | 16 | { |
142 | 16 | *size = 0; |
143 | 16 | lwerror("%s: varint exceeds 64 bits", __func__); |
144 | 16 | return 0; |
145 | 16 | } |
146 | | /* move the "cursor" one step */ |
147 | 1.71M | ptr++; |
148 | | /* Move the last read byte to the most significant */ |
149 | | /* place in the result and return the whole result */ |
150 | 1.71M | *size = ptr - the_start; |
151 | 1.71M | return nVal | ((uint64_t)nByte << nShift); |
152 | 1.71M | } |
153 | 1.75M | } |
154 | 373 | lwerror("%s: varint extends past end of buffer", __func__); |
155 | 373 | *size = 0; |
156 | 373 | return 0; |
157 | 1.71M | } |
158 | | |
159 | | size_t |
160 | | varint_size(const uint8_t *the_start, const uint8_t *the_end) |
161 | 540k | { |
162 | 540k | const uint8_t *ptr = the_start; |
163 | | |
164 | | /* Check so we don't read beyond the twkb */ |
165 | 1.55M | while( ptr < the_end ) |
166 | 1.55M | { |
167 | | /* Hibit is set, this isn't the last byte */ |
168 | 1.55M | if (*ptr & 0x80) |
169 | 1.01M | { |
170 | 1.01M | ptr++; |
171 | 1.01M | } |
172 | 540k | else |
173 | 540k | { |
174 | 540k | ptr++; |
175 | 540k | return ptr - the_start; |
176 | 540k | } |
177 | 1.55M | } |
178 | 166 | return 0; |
179 | 540k | } |
180 | | |
181 | | uint64_t zigzag64(int64_t val) |
182 | 0 | { |
183 | 0 | return val >= 0 ? |
184 | 0 | ((uint64_t)val) << 1 : |
185 | 0 | ((((uint64_t)(-1 - val)) << 1) | 0x01); |
186 | 0 | } |
187 | | |
188 | | uint32_t zigzag32(int32_t val) |
189 | 0 | { |
190 | 0 | return val >= 0 ? |
191 | 0 | ((uint32_t)val) << 1 : |
192 | 0 | ((((uint32_t)(-1 - val)) << 1) | 0x01); |
193 | 0 | } |
194 | | |
195 | | uint8_t zigzag8(int8_t val) |
196 | 0 | { |
197 | 0 | return val >= 0 ? |
198 | 0 | ((uint8_t)val) << 1 : |
199 | 0 | ((((uint8_t)(-1 - val)) << 1) | 0x01); |
200 | 0 | } |
201 | | |
202 | | int64_t unzigzag64(uint64_t val) |
203 | 1.32M | { |
204 | 1.32M | return !(val & 0x01) ? |
205 | 595k | ((int64_t)(val >> 1)) : |
206 | 1.32M | (-1 * (int64_t)((val+1) >> 1)); |
207 | 1.32M | } |
208 | | |
209 | | int32_t unzigzag32(uint32_t val) |
210 | 0 | { |
211 | 0 | return !(val & 0x01) ? |
212 | 0 | ((int32_t)(val >> 1)) : |
213 | 0 | (-1 * (int32_t)((val+1) >> 1)); |
214 | 0 | } |
215 | | |
216 | | int8_t unzigzag8(uint8_t val) |
217 | 47.7k | { |
218 | 47.7k | return !(val & 0x01) ? |
219 | 17.6k | ((int8_t)(val >> 1)) : |
220 | 47.7k | (-1 * (int8_t)((val+1) >> 1)); |
221 | 47.7k | } |