compr_rtime.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. * JFFS2 -- Journalling Flash File System, Version 2.
  3. *
  4. * Copyright (C) 2001-2003 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. * Very simple lz77-ish encoder.
  11. *
  12. * Theory of operation: Both encoder and decoder have a list of "last
  13. * occurrences" for every possible source-value; after sending the
  14. * first source-byte, the second byte indicated the "run" length of
  15. * matches
  16. *
  17. * The algorithm is intended to only send "whole bytes", no bit-messing.
  18. *
  19. */
  20. #include <stdint.h>
  21. #include <string.h>
  22. #include "compr.h"
  23. /* _compress returns the compressed size, -1 if bigger */
  24. static int jffs2_rtime_compress(unsigned char *data_in, unsigned char *cpage_out,
  25. uint32_t *sourcelen, uint32_t *dstlen)
  26. {
  27. short positions[256];
  28. int outpos = 0;
  29. int pos=0;
  30. memset(positions,0,sizeof(positions));
  31. while (pos < (*sourcelen) && outpos+2 <= (*dstlen)) {
  32. int backpos, runlen=0;
  33. unsigned char value;
  34. value = data_in[pos];
  35. cpage_out[outpos++] = data_in[pos++];
  36. backpos = positions[value];
  37. positions[value]=pos;
  38. while ((backpos < pos) && (pos < (*sourcelen)) &&
  39. (data_in[pos]==data_in[backpos++]) && (runlen<255)) {
  40. pos++;
  41. runlen++;
  42. }
  43. cpage_out[outpos++] = runlen;
  44. }
  45. if (outpos >= pos) {
  46. /* We failed */
  47. return -1;
  48. }
  49. /* Tell the caller how much we managed to compress, and how much space it took */
  50. *sourcelen = pos;
  51. *dstlen = outpos;
  52. return 0;
  53. }
  54. static int jffs2_rtime_decompress(unsigned char *data_in, unsigned char *cpage_out,
  55. __attribute__((unused)) uint32_t srclen, uint32_t destlen)
  56. {
  57. short positions[256];
  58. int outpos = 0;
  59. int pos=0;
  60. memset(positions,0,sizeof(positions));
  61. while (outpos<destlen) {
  62. unsigned char value;
  63. int backoffs;
  64. int repeat;
  65. value = data_in[pos++];
  66. cpage_out[outpos++] = value; /* first the verbatim copied byte */
  67. repeat = data_in[pos++];
  68. backoffs = positions[value];
  69. positions[value]=outpos;
  70. if (repeat) {
  71. if (backoffs + repeat >= outpos) {
  72. while(repeat) {
  73. cpage_out[outpos++] = cpage_out[backoffs++];
  74. repeat--;
  75. }
  76. } else {
  77. memcpy(&cpage_out[outpos],&cpage_out[backoffs],repeat);
  78. outpos+=repeat;
  79. }
  80. }
  81. }
  82. return 0;
  83. }
  84. static struct jffs2_compressor jffs2_rtime_comp = {
  85. .priority = JFFS2_RTIME_PRIORITY,
  86. .name = "rtime",
  87. .disabled = 0,
  88. .compr = JFFS2_COMPR_RTIME,
  89. .compress = &jffs2_rtime_compress,
  90. .decompress = &jffs2_rtime_decompress,
  91. };
  92. int jffs2_rtime_init(void)
  93. {
  94. return jffs2_register_compressor(&jffs2_rtime_comp);
  95. }
  96. void jffs2_rtime_exit(void)
  97. {
  98. jffs2_unregister_compressor(&jffs2_rtime_comp);
  99. }