rle_opt.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /*
  2. * rle_opt.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, dependency-free versions
  27. *
  28. */
  29. /**
  30. * Encode bytes
  31. */
  32. void rle_enc8(unsigned char *inbuff, int inlen, unsigned char *outbuff, int *outlen)
  33. {
  34. int i, k, l, o;
  35. if(!inbuff || inlen < 1 || !outbuff || !outlen) return;
  36. k = o = 0; outbuff[o++] = 0;
  37. for(i = 0; i < inlen; i++) {
  38. for(l = 1; l < 128 && i + l < inlen && inbuff[i] == inbuff[i + l]; l++);
  39. if(l > 1) {
  40. l--; if(outbuff[k]) { outbuff[k]--; outbuff[o++] = 0x80 | l; } else outbuff[k] = 0x80 | l;
  41. outbuff[o++] = inbuff[i]; k = o; outbuff[o++] = 0; i += l; continue;
  42. }
  43. outbuff[k]++; outbuff[o++] = inbuff[i];
  44. if(outbuff[k] > 127) { outbuff[k]--; k = o; outbuff[o++] = 0; }
  45. }
  46. if(!(outbuff[k] & 0x80)) { if(outbuff[k]) outbuff[k]--; else o--; }
  47. *outlen = o;
  48. }
  49. /**
  50. * Encode double bytes
  51. */
  52. void rle_enc16(unsigned char *inbuff, int inlen, unsigned char *outbuff, int *outlen)
  53. {
  54. unsigned short int *data = (unsigned short int *)inbuff;
  55. int i, k, l, o;
  56. inlen >>= 1;
  57. if(!inbuff || inlen < 1 || !outbuff || !outlen) return;
  58. k = o = 0; outbuff[o++] = 0;
  59. for(i = 0; i < inlen; i++) {
  60. for(l = 1; l < 128 && i + l < inlen && data[i] == data[i + l]; l++);
  61. if(l > 1) {
  62. l--; if(outbuff[k]) { outbuff[k]--; outbuff[o++] = 0x80 | l; } else outbuff[k] = 0x80 | l;
  63. *((unsigned short int*)(outbuff + o)) = data[i]; o += 2; k = o; outbuff[o++] = 0; i += l; continue;
  64. }
  65. outbuff[k]++; *((unsigned short int*)(outbuff + o)) = data[i]; o += 2;
  66. if(outbuff[k] > 127) { outbuff[k]--; k = o; outbuff[o++] = 0; }
  67. }
  68. if(!(outbuff[k] & 0x80)) { if(outbuff[k]) outbuff[k]--; else o--; }
  69. *outlen = o;
  70. }
  71. /**
  72. * Encode triple bytes
  73. */
  74. void rle_enc24(unsigned char *inbuff, int inlen, unsigned char *outbuff, int *outlen)
  75. {
  76. int i, k, l, o;
  77. if(!inbuff || inlen < 3 || !outbuff || !outlen) return;
  78. k = o = 0; outbuff[o++] = 0;
  79. for(i = 0; i < inlen; i += 3) {
  80. for(l = 1; l < 128 && i + l < inlen && inbuff[i] == inbuff[i + l*3] && inbuff[i+1] == inbuff[i+1 + l*3] &&
  81. inbuff[i+2] == inbuff[i+2 + l*3]; l++);
  82. if(l > 1) {
  83. l--; if(outbuff[k]) { outbuff[k]--; outbuff[o++] = 0x80 | l; } else outbuff[k] = 0x80 | l;
  84. outbuff[o] = inbuff[i]; outbuff[o+1] = inbuff[i+1]; outbuff[o+2] = inbuff[i+2]; o += 3; k = o; outbuff[o++] = 0;
  85. i += l*3;
  86. continue;
  87. }
  88. outbuff[k]++; outbuff[o] = inbuff[i]; outbuff[o+1] = inbuff[i+1]; outbuff[o+2] = inbuff[i+2]; o += 3;
  89. if(outbuff[k] > 127) { outbuff[k]--; k = o; outbuff[o++] = 0; }
  90. }
  91. if(!(outbuff[k] & 0x80)) { if(outbuff[k]) outbuff[k]--; else o--; }
  92. *outlen = o;
  93. }
  94. /**
  95. * Encode quad bytes
  96. */
  97. void rle_enc32(unsigned char *inbuff, int inlen, unsigned char *outbuff, int *outlen)
  98. {
  99. unsigned int *data = (unsigned int *)inbuff;
  100. int i, k, l, o;
  101. inlen >>= 2;
  102. if(!inbuff || inlen < 1 || !outbuff || !outlen) return;
  103. k = o = 0; outbuff[o++] = 0;
  104. for(i = 0; i < inlen; i++) {
  105. for(l = 1; l < 128 && i + l < inlen && data[i] == data[i + l]; l++);
  106. if(l > 1) {
  107. l--; if(outbuff[k]) { outbuff[k]--; outbuff[o++] = 0x80 | l; } else outbuff[k] = 0x80 | l;
  108. *((unsigned int*)(outbuff + o)) = data[i]; o += 4; k = o; outbuff[o++] = 0; i += l; continue;
  109. }
  110. outbuff[k]++; *((unsigned int*)(outbuff + o)) = data[i]; o += 4;
  111. if(outbuff[k] > 127) { outbuff[k]--; k = o; outbuff[o++] = 0; }
  112. }
  113. if(!(outbuff[k] & 0x80)) { if(outbuff[k]) outbuff[k]--; else o--; }
  114. *outlen = o;
  115. }
  116. /**
  117. * Decode bytes
  118. */
  119. void rle_dec8(unsigned char *inbuff, int inlen, unsigned char *outbuff, int *outlen)
  120. {
  121. int l, o = 0;
  122. unsigned char *end = inbuff + inlen;
  123. if(!inbuff || inlen < 2 || !outbuff || !outlen) return;
  124. while(inbuff < end) {
  125. l = ((*inbuff++) & 0x7F) + 1;
  126. if(inbuff[-1] & 0x80) {
  127. while(l--) outbuff[o++] = *inbuff;
  128. inbuff++;
  129. } else while(l--) outbuff[o++] = *inbuff++;
  130. }
  131. *outlen = o;
  132. }
  133. /**
  134. * Decode double bytes
  135. */
  136. void rle_dec16(unsigned char *inbuff, int inlen, unsigned char *outbuff, int *outlen)
  137. {
  138. int l, o = 0;
  139. unsigned char *end = inbuff + inlen;
  140. if(!inbuff || inlen < 3 || !outbuff || !outlen) return;
  141. while(inbuff < end) {
  142. l = ((*inbuff++) & 0x7F) + 1;
  143. if(inbuff[-1] & 0x80) {
  144. while(l--) { *((unsigned short int*)(outbuff + o)) = *((unsigned short int*)(inbuff)); o += 2; }
  145. inbuff += 2;
  146. } else while(l--) { *((unsigned short int*)(outbuff + o)) = *((unsigned short int*)(inbuff)); o += 2; inbuff += 2; }
  147. }
  148. *outlen = o;
  149. }
  150. /**
  151. * Decode triple bytes
  152. */
  153. void rle_dec24(unsigned char *inbuff, int inlen, unsigned char *outbuff, int *outlen)
  154. {
  155. int l, o = 0;
  156. unsigned char *end = inbuff + inlen;
  157. if(!inbuff || inlen < 4 || !outbuff || !outlen) return;
  158. while(inbuff < end) {
  159. l = ((*inbuff++) & 0x7F) + 1;
  160. if(inbuff[-1] & 0x80) {
  161. while(l--) { outbuff[o] = inbuff[0]; outbuff[o+1] = inbuff[1]; outbuff[o+2] = inbuff[2]; o += 3; }
  162. inbuff += 3;
  163. } else { l *= 3; while(l--) outbuff[o++] = *inbuff++; }
  164. }
  165. *outlen = o;
  166. }
  167. /**
  168. * Decode quad bytes
  169. */
  170. void rle_dec32(unsigned char *inbuff, int inlen, unsigned char *outbuff, int *outlen)
  171. {
  172. int l, o = 0;
  173. unsigned char *end = inbuff + inlen;
  174. if(!inbuff || inlen < 5 || !outbuff || !outlen) return;
  175. while(inbuff < end) {
  176. l = ((*inbuff++) & 0x7F) + 1;
  177. if(inbuff[-1] & 0x80) {
  178. while(l--) { *((unsigned int*)(outbuff + o)) = *((unsigned int*)(inbuff)); o += 4; }
  179. inbuff += 4;
  180. } else
  181. while(l--) { *((unsigned int*)(outbuff + o)) = *((unsigned int*)(inbuff)); o += 4; inbuff += 4; }
  182. }
  183. *outlen = o;
  184. }