compr_rtime.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:4k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  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.5 2001/03/15 15:38:23 dwmw2 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 <linux/kernel.h>
  48. #include <linux/types.h>
  49. #include <linux/errno.h>
  50. #include <linux/string.h> 
  51. /* _compress returns the compressed size, -1 if bigger */
  52. int rtime_compress(unsigned char *data_in, unsigned char *cpage_out, 
  53.    __u32 *sourcelen, __u32 *dstlen)
  54. {
  55. int positions[256];
  56. int outpos = 0;
  57. int pos=0;
  58. memset(positions,0,sizeof(positions)); 
  59. while (pos < (*sourcelen) && outpos <= (*dstlen)-2) {
  60. int backpos, runlen=0;
  61. unsigned char value;
  62. value = data_in[pos];
  63. cpage_out[outpos++] = data_in[pos++];
  64. backpos = positions[value];
  65. positions[value]=pos;
  66. while ((backpos < pos) && (pos < (*sourcelen)) &&
  67.        (data_in[pos]==data_in[backpos++]) && (runlen<255)) {
  68. pos++;
  69. runlen++;
  70. }
  71. cpage_out[outpos++] = runlen;
  72. }
  73. if (outpos >= pos) {
  74. /* We failed */
  75. return -1;
  76. }
  77. /* Tell the caller how much we managed to compress, and how much space it took */
  78. *sourcelen = pos;
  79. *dstlen = outpos;
  80. return 0;
  81. }    
  82. void rtime_decompress(unsigned char *data_in, unsigned char *cpage_out,
  83.       __u32 srclen, __u32 destlen)
  84. {
  85. int positions[256];
  86. int outpos = 0;
  87. int pos=0;
  88. memset(positions,0,sizeof(positions)); 
  89. while (outpos<destlen) {
  90. unsigned char value;
  91. int backoffs;
  92. int repeat;
  93. value = data_in[pos++];
  94. cpage_out[outpos++] = value; /* first the verbatim copied byte */
  95. repeat = data_in[pos++];
  96. backoffs = positions[value];
  97. positions[value]=outpos;
  98. if (repeat) {
  99. if (backoffs + repeat >= outpos) {
  100. while(repeat) {
  101. cpage_out[outpos++] = cpage_out[backoffs++];
  102. repeat--;
  103. }
  104. } else {
  105. memcpy(&cpage_out[outpos],&cpage_out[backoffs],repeat);
  106. outpos+=repeat;
  107. }
  108. }
  109. }
  110. }