compr_rubin.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. * Heavily modified by Russ Dill <Russ.Dill@asu.edu> in an attempt at
  9. * a little more speed.
  10. *
  11. * The original JFFS, from which the design for JFFS2 was derived,
  12. * was designed and implemented by Axis Communications AB.
  13. *
  14. * The contents of this file are subject to the Red Hat eCos Public
  15. * License Version 1.1 (the "Licence"); you may not use this file
  16. * except in compliance with the Licence. You may obtain a copy of
  17. * the Licence at http://www.redhat.com/
  18. *
  19. * Software distributed under the Licence is distributed on an "AS IS"
  20. * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
  21. * See the Licence for the specific language governing rights and
  22. * limitations under the Licence.
  23. *
  24. * The Original Code is JFFS2 - Journalling Flash File System, version 2
  25. *
  26. * Alternatively, the contents of this file may be used under the
  27. * terms of the GNU General Public License version 2 (the "GPL"), in
  28. * which case the provisions of the GPL are applicable instead of the
  29. * above. If you wish to allow the use of your version of this file
  30. * only under the terms of the GPL and not to allow others to use your
  31. * version of this file under the RHEPL, indicate your decision by
  32. * deleting the provisions above and replace them with the notice and
  33. * other provisions required by the GPL. If you do not delete the
  34. * provisions above, a recipient may use your version of this file
  35. * under either the RHEPL or the GPL.
  36. *
  37. * $Id: compr_rubin.c,v 1.2 2002/01/24 22:58:42 rfeany Exp $
  38. *
  39. */
  40. #include <config.h>
  41. #include <jffs2/jffs2.h>
  42. #include <jffs2/compr_rubin.h>
  43. void rubin_do_decompress(unsigned char *bits, unsigned char *in,
  44. unsigned char *page_out, __u32 destlen)
  45. {
  46. register char *curr = (char *)page_out;
  47. char *end = (char *)(page_out + destlen);
  48. register unsigned long temp;
  49. register unsigned long result;
  50. register unsigned long p;
  51. register unsigned long q;
  52. register unsigned long rec_q;
  53. register unsigned long bit;
  54. register long i0;
  55. unsigned long i;
  56. /* init_pushpull */
  57. temp = *(u32 *) in;
  58. bit = 16;
  59. /* init_rubin */
  60. q = 0;
  61. p = (long) (2 * UPPER_BIT_RUBIN);
  62. /* init_decode */
  63. rec_q = (in[0] << 8) | in[1];
  64. while (curr < end) {
  65. /* in byte */
  66. result = 0;
  67. for (i = 0; i < 8; i++) {
  68. /* decode */
  69. while ((q & UPPER_BIT_RUBIN) || ((p + q) <= UPPER_BIT_RUBIN)) {
  70. q &= ~UPPER_BIT_RUBIN;
  71. q <<= 1;
  72. p <<= 1;
  73. rec_q &= ~UPPER_BIT_RUBIN;
  74. rec_q <<= 1;
  75. rec_q |= (temp >> (bit++ ^ 7)) & 1;
  76. if (bit > 31) {
  77. u32 *p = (u32 *)in;
  78. bit = 0;
  79. temp = *(++p);
  80. in = (unsigned char *)p;
  81. }
  82. }
  83. i0 = (bits[i] * p) >> 8;
  84. if (i0 <= 0) i0 = 1;
  85. /* if it fails, it fails, we have our crc
  86. if (i0 >= p) i0 = p - 1; */
  87. result >>= 1;
  88. if (rec_q < q + i0) {
  89. /* result |= 0x00; */
  90. p = i0;
  91. } else {
  92. result |= 0x80;
  93. p -= i0;
  94. q += i0;
  95. }
  96. }
  97. *(curr++) = result;
  98. }
  99. }
  100. void dynrubin_decompress(unsigned char *data_in, unsigned char *cpage_out,
  101. unsigned long sourcelen, unsigned long dstlen)
  102. {
  103. unsigned char bits[8];
  104. int c;
  105. for (c=0; c<8; c++)
  106. bits[c] = (256 - data_in[c]);
  107. rubin_do_decompress(bits, data_in+8, cpage_out, dstlen);
  108. }