1 /* 2 Copyright (c) 2015-2021 Timur Gafarov 3 4 Boost Software License - Version 1.0 - August 17th, 2003 5 6 Permission is hereby granted, free of charge, to any person or organization 7 obtaining a copy of the software and accompanying documentation covered by 8 this license (the "Software") to use, reproduce, display, distribute, 9 execute, and transmit the Software, and to prepare derivative works of the 10 Software, and to permit third-parties to whom the Software is furnished to 11 do so, all subject to the following: 12 13 The copyright notices in the Software and this entire statement, including 14 the above license grant, this restriction and the following disclaimer, 15 must be included in all copies of the Software, in whole or in part, and 16 all derivative works of the Software, unless such copies or derivative 17 works are solely in the form of machine-executable object code generated by 18 a source language processor. 19 20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 23 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 24 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 25 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 26 DEALINGS IN THE SOFTWARE. 27 */ 28 29 /** 30 * Variable-sized integer type 31 * 32 * Copyright: Timur Gafarov 2015-2021. 33 * License: $(LINK2 boost.org/LICENSE_1_0.txt, Boost License 1.0). 34 * Authors: Timur Gafarov 35 */ 36 module dlib.coding.varint; 37 38 /** 39 * Protobuf-style variable-sized integer 40 */ 41 struct Varint 42 { 43 ubyte[8] buffer; 44 size_t size; 45 46 /// Returns bytes of an integer 47 ubyte[] bytes() return 48 { 49 return buffer[0..size]; 50 } 51 } 52 53 /** 54 * Returns size in bytes necessary to store an integer 55 */ 56 size_t getVarintSize(ulong n) 57 { 58 enum ulong N1 = 128; 59 enum ulong N2 = 16384; 60 enum ulong N3 = 2097152; 61 enum ulong N4 = 268435456; 62 enum ulong N5 = 34359738368; 63 enum ulong N6 = 4398046511104; 64 enum ulong N7 = 562949953421312; 65 enum ulong N8 = 72057594037927936; 66 67 return ( 68 n < N1 ? 1 69 : n < N2 ? 2 70 : n < N3 ? 3 71 : n < N4 ? 4 72 : n < N5 ? 5 73 : n < N6 ? 6 74 : n < N7 ? 7 75 : 8 76 ); 77 } 78 79 /** 80 * Encode a fixed-size integer to Varint 81 */ 82 Varint encodeVarint(ulong n) 83 { 84 Varint res; 85 res.size = getVarintSize(n); 86 ubyte* ptr = res.buffer.ptr; 87 88 for (uint i = 0; i < res.size; i++) 89 { 90 if (i == res.size - 1) 91 *(ptr++) = n & 0xFF; 92 else 93 *(ptr++) = cast(ubyte)(n | 0x80); 94 n >>= 7; 95 } 96 97 return res; 98 } 99 100 /** 101 * Decode Varing to fixed-size integer 102 */ 103 ulong decodeVarint(Varint vint) 104 { 105 ulong result = 0; 106 int bits = 0; 107 ubyte* ptr = vint.buffer.ptr; 108 ulong ll; 109 110 while (*ptr & 0x80) 111 { 112 ll = *ptr; 113 result += ((ll & 0x7F) << bits); 114 ptr++; 115 bits += 7; 116 } 117 118 ll = *ptr; 119 result += ((ll & 0x7F) << bits); 120 121 return result; 122 }