compr_rtime.c 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * JFFS2 -- Journalling Flash File System, Version 2.
  3. *
  4. * Copyright (C) 2001 Red Hat, Inc.
  5. *
  6. * Created by Arjan van de Ven <arjanv@redhat.com>
  7. *
  8. * The original JFFS, from which the design for JFFS2 was derived,
  9. * was designed and implemented by Axis Communications AB.
  10. *
  11. * The contents of this file are subject to the Red Hat eCos Public
  12. * License Version 1.1 (the "Licence"); you may not use this file
  13. * except in compliance with the Licence. You may obtain a copy of
  14. * the Licence at http://www.redhat.com/
  15. *
  16. * Software distributed under the Licence is distributed on an "AS IS"
  17. * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
  18. * See the Licence for the specific language governing rights and
  19. * limitations under the Licence.
  20. *
  21. * The Original Code is JFFS2 - Journalling Flash File System, version 2
  22. *
  23. * Alternatively, the contents of this file may be used under the
  24. * terms of the GNU General Public License version 2 (the "GPL"), in
  25. * which case the provisions of the GPL are applicable instead of the
  26. * above. If you wish to allow the use of your version of this file
  27. * only under the terms of the GPL and not to allow others to use your
  28. * version of this file under the RHEPL, indicate your decision by
  29. * deleting the provisions above and replace them with the notice and
  30. * other provisions required by the GPL. If you do not delete the
  31. * provisions above, a recipient may use your version of this file
  32. * under either the RHEPL or the GPL.
  33. *
  34. * $Id: compr_rtime.c,v 1.2 2002/01/24 22:58:42 rfeany Exp $
  35. *
  36. *
  37. * Very simple lz77-ish encoder.
  38. *
  39. * Theory of operation: Both encoder and decoder have a list of "last
  40. * occurances" for every possible source-value; after sending the
  41. * first source-byte, the second byte indicated the "run" length of
  42. * matches
  43. *
  44. * The algorithm is intended to only send "whole bytes", no bit-messing.
  45. *
  46. */
  47. #include <config.h>
  48. #if defined(CONFIG_CMD_JFFS2)
  49. #include <jffs2/jffs2.h>
  50. void rtime_decompress(unsigned char *data_in, unsigned char *cpage_out,
  51. u32 srclen, u32 destlen)
  52. {
  53. int positions[256];
  54. int outpos;
  55. int pos;
  56. int i;
  57. outpos = pos = 0;
  58. for (i = 0; i < 256; positions[i++] = 0);
  59. while (outpos<destlen) {
  60. unsigned char value;
  61. int backoffs;
  62. int repeat;
  63. value = data_in[pos++];
  64. cpage_out[outpos++] = value; /* first the verbatim copied byte */
  65. repeat = data_in[pos++];
  66. backoffs = positions[value];
  67. positions[value]=outpos;
  68. if (repeat) {
  69. if (backoffs + repeat >= outpos) {
  70. while(repeat) {
  71. cpage_out[outpos++] = cpage_out[backoffs++];
  72. repeat--;
  73. }
  74. } else {
  75. for (i = 0; i < repeat; i++)
  76. *(cpage_out + outpos + i) = *(cpage_out + backoffs + i);
  77. outpos+=repeat;
  78. }
  79. }
  80. }
  81. }
  82. #endif