| 1 | /* |
| 2 | Copyright 2015 - 2026 Esri |
| 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 | A local copy of the license and additional notices are located with the |
| 17 | source distribution at: |
| 18 | |
| 19 | http://github.com/Esri/lerc/ |
| 20 | |
| 21 | Contributors: Thomas Maurer |
| 22 | */ |
| 23 | |
| 24 | #include "Defines.h" |
| 25 | #include "RLE.h" |
| 26 | #include <cstring> |
| 27 | |
| 28 | USING_NAMESPACE_LERC |
| 29 | |
| 30 | // -------------------------------------------------------------------------- ; |
| 31 | |
| 32 | size_t RLE::computeNumBytesRLE(const Byte* arr, size_t numBytes) const |
| 33 | { |
| 34 | if (arr == nullptr || numBytes == 0) |
| 35 | return 0; |
| 36 | |
| 37 | const Byte* ptr = arr; |
| 38 | size_t sum = 0; |
| 39 | size_t cntOdd = 0; |
| 40 | size_t cntEven = 0; |
| 41 | size_t cntTotal = 0; |
| 42 | bool bOdd = true; |
| 43 | |
| 44 | while (cntTotal < numBytes - 1) |
| 45 | { |
| 46 | if (*ptr != *(ptr + 1)) |
| 47 | { |
| 48 | if (bOdd) |
| 49 | { |
| 50 | cntOdd++; |
| 51 | } |
| 52 | else // switch to odd mode |
| 53 | { |
| 54 | sum += 2 + 1; |
| 55 | bOdd = true; |
| 56 | cntOdd = 0; |
| 57 | cntEven = 0; |
| 58 | } |
| 59 | } |
| 60 | else |
| 61 | { |
| 62 | if (!bOdd) |
| 63 | { |
| 64 | cntEven++; |
| 65 | } |
| 66 | else |
| 67 | { |
| 68 | // count if we have enough equal bytes so it is worth switching to even |
| 69 | bool foundEnough = false; |
| 70 | if (cntTotal + m_minNumEven < numBytes) |
| 71 | { |
| 72 | int i = 1; |
| 73 | while (i < m_minNumEven && ptr[i] == ptr[0]) i++; |
| 74 | foundEnough = i < m_minNumEven ? false : true; |
| 75 | } |
| 76 | |
| 77 | if (!foundEnough) // stay in odd mode |
| 78 | { |
| 79 | cntOdd++; |
| 80 | } |
| 81 | else // switch to even mode |
| 82 | { |
| 83 | if (cntOdd > 0) |
| 84 | sum += 2 + cntOdd; |
| 85 | bOdd = false; |
| 86 | cntOdd = 0; |
| 87 | cntEven = 0; |
| 88 | cntEven++; |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | ptr++; |
| 93 | cntTotal++; |
| 94 | |
| 95 | if (cntOdd == 32767) // prevent short counters from overflow |
| 96 | { |
| 97 | sum += 2 + 32767; |
| 98 | cntOdd = 0; |
| 99 | } |
| 100 | if (cntEven == 32767) |
| 101 | { |
| 102 | sum += 2 + 1; |
| 103 | cntEven = 0; |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | // don't forget the last byte |
| 108 | if (bOdd) |
| 109 | { |
| 110 | cntOdd++; |
| 111 | sum += 2 + cntOdd; |
| 112 | } |
| 113 | else |
| 114 | { |
| 115 | sum += 2 + 1; |
| 116 | } |
| 117 | |
| 118 | return sum + 2; // EOF short |
| 119 | } |
| 120 | |
| 121 | // -------------------------------------------------------------------------- ; |
| 122 | |
| 123 | bool RLE::compress(const Byte* arr, size_t numBytes, |
| 124 | Byte** arrRLE, size_t& numBytesRLE, bool verify) const |
| 125 | { |
| 126 | if (arr == nullptr || numBytes == 0) |
| 127 | return false; |
| 128 | |
| 129 | numBytesRLE = computeNumBytesRLE(arr, numBytes); |
| 130 | |
| 131 | *arrRLE = new Byte[numBytesRLE]; |
| 132 | if (!*arrRLE) |
| 133 | return false; |
| 134 | |
| 135 | const Byte* srcPtr = arr; |
| 136 | Byte* cntPtr = *arrRLE; |
| 137 | Byte* dstPtr = cntPtr + 2; |
| 138 | //size_t sum = 0; |
| 139 | size_t cntOdd = 0; |
| 140 | size_t cntEven = 0; |
| 141 | size_t cntTotal = 0; |
| 142 | bool bOdd = true; |
| 143 | |
| 144 | while (cntTotal < numBytes - 1) |
| 145 | { |
| 146 | if (*srcPtr != *(srcPtr + 1)) |
| 147 | { |
| 148 | *dstPtr++ = *srcPtr; |
| 149 | if (bOdd) |
| 150 | { |
| 151 | cntOdd++; |
| 152 | } |
| 153 | else // switch to odd mode |
| 154 | { |
| 155 | cntEven++; |
| 156 | writeCount(-(short)cntEven, &cntPtr, &dstPtr); // - sign for even cnts |
| 157 | //sum += 2 + 1; |
| 158 | bOdd = true; |
| 159 | cntOdd = 0; |
| 160 | cntEven = 0; |
| 161 | } |
| 162 | } |
| 163 | else |
| 164 | { |
| 165 | if (!bOdd) |
| 166 | { |
| 167 | cntEven++; |
| 168 | } |
| 169 | else |
| 170 | { |
| 171 | // count if we have enough equal bytes so it is worth switching to even |
| 172 | bool foundEnough = false; |
| 173 | if (cntTotal + m_minNumEven < numBytes) |
| 174 | { |
| 175 | int i = 1; |
| 176 | while (i < m_minNumEven && srcPtr[i] == srcPtr[0]) i++; |
| 177 | foundEnough = i < m_minNumEven ? false : true; |
| 178 | } |
| 179 | |
| 180 | if (!foundEnough) // stay in odd mode |
| 181 | { |
| 182 | *dstPtr++ = *srcPtr; |
| 183 | cntOdd++; |
| 184 | } |
| 185 | else // switch to even mode |
| 186 | { |
| 187 | if (cntOdd > 0) |
| 188 | { |
| 189 | writeCount((short)cntOdd, &cntPtr, &dstPtr); // + sign for odd cnts |
| 190 | //sum += 2 + cntOdd; |
| 191 | } |
| 192 | bOdd = false; |
| 193 | cntOdd = 0; |
| 194 | cntEven = 0; |
| 195 | cntEven++; |
| 196 | } |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | if (cntOdd == 32767) // prevent short counters from overflow |
| 201 | { |
| 202 | writeCount((short)cntOdd, &cntPtr, &dstPtr); |
| 203 | //sum += 2 + 32767; |
| 204 | cntOdd = 0; |
| 205 | } |
| 206 | if (cntEven == 32767) |
| 207 | { |
| 208 | *dstPtr++ = *srcPtr; |
| 209 | writeCount(-(short)cntEven, &cntPtr, &dstPtr); |
| 210 | //sum += 2 + 1; |
| 211 | cntEven = 0; |
| 212 | } |
| 213 | |
| 214 | srcPtr++; |
| 215 | cntTotal++; |
| 216 | } |
| 217 | |
| 218 | // don't forget the last byte |
| 219 | *dstPtr++ = *srcPtr; |
| 220 | if (bOdd) |
| 221 | { |
| 222 | cntOdd++; |
| 223 | writeCount((short)cntOdd, &cntPtr, &dstPtr); |
| 224 | //sum += 2 + cntOdd; |
| 225 | } |
| 226 | else |
| 227 | { |
| 228 | cntEven++; |
| 229 | writeCount(-(short)cntEven, &cntPtr, &dstPtr); |
| 230 | //sum += 2 + 1; |
| 231 | } |
| 232 | |
| 233 | writeCount(-32768, &cntPtr, &dstPtr); // write end of stream symbol |
| 234 | //sum += 2; |
| 235 | |
| 236 | if (verify) |
| 237 | { |
| 238 | Byte* arr2 = nullptr; |
| 239 | size_t numBytes2 = 0; |
| 240 | if (!decompress(*arrRLE, numBytesRLE, &arr2, numBytes2) || numBytes2 != numBytes) |
| 241 | { |
| 242 | delete[] arr2; |
| 243 | return false; |
| 244 | } |
| 245 | int nCheck = memcmp(arr, arr2, numBytes); |
| 246 | delete[] arr2; |
| 247 | if (nCheck != 0) |
| 248 | return false; |
| 249 | } |
| 250 | |
| 251 | return true; |
| 252 | } |
| 253 | |
| 254 | // -------------------------------------------------------------------------- ; |
| 255 | |
| 256 | bool RLE::decompress(const Byte* arrRLE, size_t nBytesRemainingIn, Byte** arr, size_t& numBytes) |
| 257 | { |
| 258 | if (!arrRLE || nBytesRemainingIn < 2) |
| 259 | return false; |
| 260 | |
| 261 | // first count the encoded bytes |
| 262 | const Byte* srcPtr = arrRLE; |
| 263 | size_t nBytesRemaining = nBytesRemainingIn - 2; |
| 264 | size_t sum = 0; |
| 265 | |
| 266 | short cnt = readCount(&srcPtr); |
| 267 | while (cnt != -32768) |
| 268 | { |
| 269 | sum += cnt < 0 ? -cnt : cnt; |
| 270 | size_t n = cnt > 0 ? cnt : 1; |
| 271 | |
| 272 | if (nBytesRemaining < n + 2) |
| 273 | return false; |
| 274 | |
| 275 | srcPtr += n; |
| 276 | cnt = readCount(&srcPtr); |
| 277 | |
| 278 | nBytesRemaining -= n + 2; |
| 279 | } |
| 280 | |
| 281 | numBytes = sum; |
| 282 | |
| 283 | if (numBytes == 0) |
| 284 | { |
| 285 | *arr = nullptr; |
| 286 | return false; |
| 287 | } |
| 288 | |
| 289 | *arr = new Byte[numBytes]; |
| 290 | if (!*arr) |
| 291 | return false; |
| 292 | |
| 293 | return decompress(arrRLE, nBytesRemainingIn, *arr, numBytes); |
| 294 | } |
| 295 | |
| 296 | // -------------------------------------------------------------------------- ; |
| 297 | |
| 298 | bool RLE::decompress(const Byte* arrRLE, size_t nBytesRemaining, Byte* arr, size_t arrSize) |
| 299 | { |
| 300 | if (!arrRLE || !arr || nBytesRemaining < 2) |
| 301 | return false; |
| 302 | |
| 303 | const Byte* srcPtr = arrRLE; |
| 304 | size_t arrIdx = 0; |
| 305 | nBytesRemaining -= 2; |
| 306 | |
| 307 | short cnt = readCount(&srcPtr); |
| 308 | while (cnt != -32768) |
| 309 | { |
| 310 | int i = (cnt <= 0) ? -cnt : cnt; |
| 311 | size_t m = (cnt <= 0) ? 1 : (size_t)i; // <= not < to fail gracefully in case of corrupted blob for old version <= 2 which had no checksum |
| 312 | |
| 313 | if (nBytesRemaining < m + 2 || arrIdx + i > arrSize) |
| 314 | return false; |
| 315 | |
| 316 | if (cnt > 0) |
| 317 | { |
| 318 | while (i--) arr[arrIdx++] = *srcPtr++; |
| 319 | } |
| 320 | else |
| 321 | { |
| 322 | Byte b = *srcPtr++; |
| 323 | while (i--) arr[arrIdx++] = b; |
| 324 | } |
| 325 | |
| 326 | nBytesRemaining -= m + 2; |
| 327 | cnt = readCount(&srcPtr); |
| 328 | } |
| 329 | |
| 330 | return true; |
| 331 | } |
| 332 | |
| 333 | // -------------------------------------------------------------------------- ; |
| 334 | |
| 335 | void RLE::writeCount(short cnt, Byte** ppCnt, Byte** ppDst) |
| 336 | { |
| 337 | SWAP_2(cnt); // write short's in little endian byte order, always |
| 338 | memcpy(*ppCnt, &cnt, sizeof(short)); |
| 339 | *ppCnt = *ppDst; |
| 340 | *ppDst += 2; |
| 341 | } |
| 342 | |
| 343 | // -------------------------------------------------------------------------- ; |
| 344 | |
| 345 | short RLE::readCount(const Byte** ppCnt) |
| 346 | { |
| 347 | short cnt; |
| 348 | memcpy(&cnt, *ppCnt, sizeof(short)); |
| 349 | SWAP_2(cnt); |
| 350 | *ppCnt += 2; |
| 351 | return cnt; |
| 352 | } |
| 353 | |
| 354 | // -------------------------------------------------------------------------- ; |
| 355 |