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. *
  6. * Created by Arjan van de Ven <arjanv@redhat.com>
  7. *
  8. * For licensing information, see the file 'LICENCE' in this directory.
  9. *
  10. *
  11. *
  12. * Very simple lz77-ish encoder.
  13. *
  14. * Theory of operation: Both encoder and decoder have a list of "last
  15. * occurrences" for every possible source-value; after sending the
  16. * first source-byte, the second byte indicated the "run" length of
  17. * matches
  18. *
  19. * The algorithm is intended to only send "whole bytes", no bit-messing.
  20. *
  21. */
  22. #include <linux/kernel.h>
  23. #include <linux/types.h>
  24. #include <linux/errno.h>
  25. #include <linux/string.h>
  26. #include <linux/jffs2.h>
  27. #include "compr.h"
  28. /* _compress returns the compressed size, -1 if bigger */
  29. static int jffs2_rtime_compress(unsigned char *data_in,
  30. unsigned char *cpage_out,
  31. uint32_t *sourcelen, uint32_t *dstlen,
  32. void *model)
  33. {
  34. 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. void *model)
  65. {
  66. short positions[256];
  67. int outpos = 0;
  68. int pos=0;
  69. memset(positions,0,sizeof(positions));
  70. while (outpos<destlen) {
  71. unsigned char value;
  72. int backoffs;
  73. int repeat;
  74. value = data_in[pos++];
  75. cpage_out[outpos++] = value; /* first the verbatim copied byte */
  76. repeat = data_in[pos++];
  77. backoffs = positions[value];
  78. positions[value]=outpos;
  79. if (repeat) {
  80. if (backoffs + repeat >= outpos) {
  81. while(repeat) {
  82. cpage_out[outpos++] = cpage_out[backoffs++];
  83. repeat--;
  84. }
  85. } else {
  86. memcpy(&cpage_out[outpos],&cpage_out[backoffs],repeat);
  87. outpos+=repeat;
  88. }
  89. }
  90. }
  91. return 0;
  92. }
  93. static struct jffs2_compressor jffs2_rtime_comp = {
  94. .priority = JFFS2_RTIME_PRIORITY,
  95. .name = "rtime",
  96. .compr = JFFS2_COMPR_RTIME,
  97. .compress = &jffs2_rtime_compress,
  98. .decompress = &jffs2_rtime_decompress,
  99. #ifdef JFFS2_RTIME_DISABLED
  100. .disabled = 1,
  101. #else
  102. .disabled = 0,
  103. #endif
  104. };
  105. int jffs2_rtime_init(void)
  106. {
  107. return jffs2_register_compressor(&jffs2_rtime_comp);
  108. }
  109. void jffs2_rtime_exit(void)
  110. {
  111. jffs2_unregister_compressor(&jffs2_rtime_comp);
  112. }