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 198 199 200 201 202 203 204 205 206 207 208 209 210 211
|
/**********************************************************************
*
* PostGIS - Spatial Types for PostgreSQL
* http://postgis.net
*
* PostGIS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* PostGIS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with PostGIS. If not, see <http://www.gnu.org/licenses/>.
*
**********************************************************************
*
* Copyright (C) 2014 Sandro Santilli <strk@kbt.io>
* Copyright (C) 2013 Nicklas Avén
*
**********************************************************************/
#include "varint.h"
#include "lwgeom_log.h"
#include "liblwgeom.h"
/* -------------------------------------------------------------------------------- */
static size_t
_varint_u64_encode_buf(uint64_t val, uint8_t *buf)
{
uint8_t grp;
uint64_t q = val;
uint8_t *ptr = buf;
while (1)
{
/* We put the 7 least significant bits in grp */
grp = 0x7f & q;
/* We rightshift our input value 7 bits */
/* which means that the 7 next least significant bits */
/* becomes the 7 least significant */
q = q >> 7;
/* Check if, after our rightshifting, we still have */
/* anything to read in our input value. */
if ( q > 0 )
{
/* In the next line quite a lot is happening. */
/* Since there is more to read in our input value */
/* we signal that by setting the most significant bit */
/* in our byte to 1. */
/* Then we put that byte in our buffer and move the pointer */
/* forward one step */
*ptr = 0x80 | grp;
ptr++;
}
else
{
/* The same as above, but since there is nothing more */
/* to read in our input value we leave the most significant bit unset */
*ptr = grp;
ptr++;
return ptr - buf;
}
}
/* This cannot happen */
lwerror("%s: Got out of infinite loop. Consciousness achieved.", __func__);
return (size_t)0;
}
size_t
varint_u64_encode_buf(uint64_t val, uint8_t *buf)
{
return _varint_u64_encode_buf(val, buf);
}
size_t
varint_u32_encode_buf(uint32_t val, uint8_t *buf)
{
return _varint_u64_encode_buf((uint64_t)val, buf);
}
size_t
varint_s64_encode_buf(int64_t val, uint8_t *buf)
{
return _varint_u64_encode_buf(zigzag64(val), buf);
}
size_t
varint_s32_encode_buf(int32_t val, uint8_t *buf)
{
return _varint_u64_encode_buf((uint64_t)zigzag32(val), buf);
}
/* Read from signed 64bit varint */
int64_t
varint_s64_decode(const uint8_t *the_start, const uint8_t *the_end, size_t *size)
{
return unzigzag64(varint_u64_decode(the_start, the_end, size));
}
/* Read from unsigned 64bit varint */
uint64_t
varint_u64_decode(const uint8_t *the_start, const uint8_t *the_end, size_t *size)
{
uint64_t nVal = 0;
int nShift = 0;
uint8_t nByte;
const uint8_t *ptr = the_start;
/* Check so we don't read beyond the twkb */
while( ptr < the_end )
{
nByte = *ptr;
/* Hibit is set, so this isn't the last byte */
if (nByte & 0x80)
{
/* We get here when there is more to read in the input varInt */
/* Here we take the least significant 7 bits of the read */
/* byte and put it in the most significant place in the result variable. */
nVal |= ((uint64_t)(nByte & 0x7f)) << nShift;
/* move the "cursor" of the input buffer step (8 bits) */
ptr++;
/* move the cursor in the resulting variable (7 bits) */
nShift += 7;
}
else
{
/* move the "cursor" one step */
ptr++;
/* Move the last read byte to the most significant */
/* place in the result and return the whole result */
*size = ptr - the_start;
return nVal | ((uint64_t)nByte << nShift);
}
}
lwerror("%s: varint extends past end of buffer", __func__);
*size = 0;
return 0;
}
size_t
varint_size(const uint8_t *the_start, const uint8_t *the_end)
{
const uint8_t *ptr = the_start;
/* Check so we don't read beyond the twkb */
while( ptr < the_end )
{
/* Hibit is set, this isn't the last byte */
if (*ptr & 0x80)
{
ptr++;
}
else
{
ptr++;
return ptr - the_start;
}
}
return 0;
}
uint64_t zigzag64(int64_t val)
{
return val >= 0 ?
((uint64_t)val) << 1 :
((((uint64_t)(-1 - val)) << 1) | 0x01);
}
uint32_t zigzag32(int32_t val)
{
return val >= 0 ?
((uint32_t)val) << 1 :
((((uint32_t)(-1 - val)) << 1) | 0x01);
}
uint8_t zigzag8(int8_t val)
{
return val >= 0 ?
((uint8_t)val) << 1 :
((((uint8_t)(-1 - val)) << 1) | 0x01);
}
int64_t unzigzag64(uint64_t val)
{
return !(val & 0x01) ?
((int64_t)(val >> 1)) :
(-1 * (int64_t)((val+1) >> 1));
}
int32_t unzigzag32(uint32_t val)
{
return !(val & 0x01) ?
((int32_t)(val >> 1)) :
(-1 * (int32_t)((val+1) >> 1));
}
int8_t unzigzag8(uint8_t val)
{
return !(val & 0x01) ?
((int8_t)(val >> 1)) :
(-1 * (int8_t)((val+1) >> 1));
}
|