rle_libc.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * rle_libc.h
  3. *
  4. * Copyright (C) 2020 bzt (bztsrc@gitlab)
  5. *
  6. * Permission is hereby granted, free of charge, to any person
  7. * obtaining a copy of this software and associated documentation
  8. * files (the "Software"), to deal in the Software without
  9. * restriction, including without limitation the rights to use, copy,
  10. * modify, merge, publish, distribute, sublicense, and/or sell copies
  11. * of the Software, and to permit persons to whom the Software is
  12. * furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be
  15. * included in all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  18. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  19. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  20. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  21. * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  22. * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  24. * DEALINGS IN THE SOFTWARE.
  25. *
  26. * @brief An extremely simple (as it should be) RLE library, libc version
  27. *
  28. */
  29. #ifndef _RLE_LIBC_H_
  30. #define _RLE_LIBC_H_
  31. #ifdef __cplusplus
  32. extern "C" {
  33. #endif
  34. #include <stdlib.h> /* realloc */
  35. #include <string.h> /* memcmp, memcpy */
  36. /**
  37. * Encode arbitrary sized members
  38. */
  39. unsigned char *rle_enc(unsigned char *inbuff, int inlen, int membsize, unsigned char *outbuff, int *outlen)
  40. {
  41. int i, k, l, o, a = !outbuff;
  42. if(!inbuff || membsize < 1 || inlen < membsize || inlen % membsize || !outlen) return (void*)0;
  43. /* allocate memory for the worst case scenario */
  44. if(a) { outbuff = (unsigned char *)realloc((void*)0, inlen + (inlen >> 7) + 1); if(!outbuff) return (void*)0; }
  45. k = o = 0; outbuff[o++] = 0;
  46. for(i = 0; i < inlen; i += membsize) {
  47. for(l = 1; l < 128 && i + l < inlen && !memcmp(inbuff + i, inbuff + i + l*membsize, membsize); l++);
  48. if(l > 1) {
  49. l--; if(outbuff[k]) { outbuff[k]--; outbuff[o++] = 0x80 | l; } else outbuff[k] = 0x80 | l;
  50. memcpy(outbuff + o, inbuff + i, membsize); o += membsize; k = o; outbuff[o++] = 0; i += l*membsize; continue;
  51. }
  52. outbuff[k]++; memcpy(outbuff + o, inbuff + i, membsize); o += membsize;
  53. if(outbuff[k] > 127) { outbuff[k]--; k = o; outbuff[o++] = 0; }
  54. }
  55. if(!(outbuff[k] & 0x80)) { if(outbuff[k]) outbuff[k]--; else o--; }
  56. *outlen = o;
  57. /* free the extra memory */
  58. if(a) outbuff = (unsigned char*)realloc(outbuff, o);
  59. return outbuff;
  60. }
  61. /**
  62. * Decode arbitrary sized members
  63. */
  64. unsigned char *rle_dec(unsigned char *inbuff, int inlen, int membsize, unsigned char *outbuff, int *outlen)
  65. {
  66. int l, o = 0, a = !outbuff, s = 0;
  67. unsigned char *end = inbuff + inlen;
  68. if(!inbuff || inlen < 1 + membsize || membsize < 1 || !outlen) return (void*)0;
  69. while(inbuff < end) {
  70. l = ((*inbuff++) & 0x7F) + 1;
  71. /* we don't know the required buffer size in advance, so allocate memory in 64k blocks */
  72. if(a && o + l*membsize + 1 > s) { s+=65536; outbuff = (unsigned char*)realloc(outbuff,s); if(!outbuff) return (void*)0; }
  73. if(inbuff[-1] & 0x80) {
  74. while(l--) { memcpy(outbuff + o, inbuff, membsize); o += membsize; }
  75. inbuff += membsize;
  76. } else { l *= membsize; memcpy(outbuff + o, inbuff, l); o += l; inbuff += l; }
  77. }
  78. *outlen = o;
  79. /* free the extra memory */
  80. if(a && s != o) outbuff = (unsigned char *)realloc(outbuff, o);
  81. return outbuff;
  82. }
  83. #ifdef __cplusplus
  84. }
  85. #endif
  86. #endif