compr_rtime.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. * JFFS2 -- Journalling Flash File System, Version 2.
  3. *
  4. * Copyright © 2001-2007 Red Hat, Inc.
  5. * Copyright © 2004-2010 David Woodhouse <dwmw2@infradead.org>
  6. *
  7. * Created by Arjan van de Ven <arjanv@redhat.com>
  8. *
  9. * For licensing information, see the file 'LICENCE' in this directory.
  10. *
  11. *
  12. *
  13. * Very simple lz77-ish encoder.
  14. *
  15. * Theory of operation: Both encoder and decoder have a list of "last
  16. * occurrences" for every possible source-value; after sending the
  17. * first source-byte, the second byte indicated the "run" length of
  18. * matches
  19. *
  20. * The algorithm is intended to only send "whole bytes", no bit-messing.
  21. *
  22. */
  23. #include <linux/kernel.h>
  24. #include <linux/types.h>
  25. #include <linux/errno.h>
  26. #include <linux/string.h>
  27. #include <linux/jffs2.h>
  28. #include "compr.h"
  29. /* _compress returns the compressed size, -1 if bigger */
  30. static int jffs2_rtime_compress(unsigned char *data_in,
  31. unsigned char *cpage_out,
  32. uint32_t *sourcelen, uint32_t *dstlen)
  33. {
  34. unsigned short positions[256];
  35. int outpos = 0;
  36. int pos=0;
  37. memset(positions,0,sizeof(positions));
  38. while (pos < (*sourcelen) && outpos <= (*dstlen)-2) {
  39. int backpos, runlen=0;
  40. unsigned char value;
  41. value = data_in[pos];
  42. cpage_out[outpos++] = data_in[pos++];
  43. backpos = positions[value];
  44. positions[value]=pos;
  45. while ((backpos < pos) && (pos < (*sourcelen)) &&
  46. (data_in[pos]==data_in[backpos++]) && (runlen<255)) {
  47. pos++;
  48. runlen++;
  49. }
  50. cpage_out[outpos++] = runlen;
  51. }
  52. if (outpos >= pos) {
  53. /* We failed */
  54. return -1;
  55. }
  56. /* Tell the caller how much we managed to compress, and how much space it took */
  57. *sourcelen = pos;
  58. *dstlen = outpos;
  59. return 0;
  60. }
  61. static int jffs2_rtime_decompress(unsigned char *data_in,
  62. unsigned char *cpage_out,
  63. uint32_t srclen, uint32_t destlen)
  64. {
  65. unsigned short positions[256];
  66. int outpos = 0;
  67. int pos=0;
  68. memset(positions,0,sizeof(positions));
  69. while (outpos<destlen) {
  70. unsigned char value;
  71. int backoffs;
  72. int repeat;
  73. value = data_in[pos++];
  74. cpage_out[outpos++] = value; /* first the verbatim copied byte */
  75. repeat = data_in[pos++];
  76. backoffs = positions[value];
  77. positions[value]=outpos;
  78. if (repeat) {
  79. if (backoffs + repeat >= outpos) {
  80. while(repeat) {
  81. cpage_out[outpos++] = cpage_out[backoffs++];
  82. repeat--;
  83. }
  84. } else {
  85. memcpy(&cpage_out[outpos],&cpage_out[backoffs],repeat);
  86. outpos+=repeat;
  87. }
  88. }
  89. }
  90. return 0;
  91. }
  92. static struct jffs2_compressor jffs2_rtime_comp = {
  93. .priority = JFFS2_RTIME_PRIORITY,
  94. .name = "rtime",
  95. .compr = JFFS2_COMPR_RTIME,
  96. .compress = &jffs2_rtime_compress,
  97. .decompress = &jffs2_rtime_decompress,
  98. #ifdef JFFS2_RTIME_DISABLED
  99. .disabled = 1,
  100. #else
  101. .disabled = 0,
  102. #endif
  103. };
  104. int jffs2_rtime_init(void)
  105. {
  106. return jffs2_register_compressor(&jffs2_rtime_comp);
  107. }
  108. void jffs2_rtime_exit(void)
  109. {
  110. jffs2_unregister_compressor(&jffs2_rtime_comp);
  111. }