⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cfs_tool.c

📁 基于nucleus实时操作系统的webserver源代码
💻 C
📖 第 1 页 / 共 5 页
字号:
/************************************************************************
*                                                                          
*    CopyrIght (c)  1993 - 2001 Accelerated Technology, Inc.               
*                                                                          
* PROPRIETARY RIGHTS of Accelerated Technology are involved in the subject 
* matter of this material.  All manufacturing, reproduction, use and sales 
* rights pertaining to this subject matter are governed by the license     
* agreement.  The recipient of this software implicity accepts the terms   
* of the license.                                                          
*                                                                          
*************************************************************************/

/************************************************************************
*                                                                       
* FILE NAME                                             VERSION          
*                                                                       
*       cfs_tool.c - Compression File System Tool         1.5      
*                                                                       
* COMPONENT                                                             
*         
*       Nucleus WebServ                                                             
*                                                                       
* DESCRIPTION                                                           
*                                                                       
*       This file holds functions that are used in the process of        
*       compressing and decompressing files.                             
*                                                                       
* DATA STRUCTURES                                                       
*                                                                       
*       Totals                          A global array to hold the       
*                                       cumulative totals.               
*                                                                       
* FUNCTIONS                                                             
*                                                                       
*       CFS_Compress                    Compresses a file in memory or   
*                                       magnetic media.
*       CFS_Decompress                  Decompresses a file in memory or 
*                                       magnetic media.                  
*       CFS_Fatal_Error                 Prints an Error message to a     
*                                       file.                            
*       CFS_Close_Output_Bit_File       Closes Output Bit File.          
*       CFS_Compress_File               Compresses Input Bit File.       
*       CFS_Open_Input_Bit_File         Open Input Bit file.             
*       CFS_Open_Output_Bit_File        Open the Output Bit file.        
*       CFS_Expand_File                 Expand compressed file.          
*       CFS_Build_Model                 Builds the compressed file model.
*       CFS_Init_Arithmetic_Encoder     Initializes the state of the     
*                                       Arithmetic Encoder.              
*       CFS_Convert_Int_To_Symbol       Converts an integer count into a 
*                                       symbol structure.                
*       CFS_Encode_Symbol               Encodes a symbol for file        
*                                       compression.                     
*       CFS_Flush_Arithmetic_Encoder    Flushes Arithmetic encoder.      
*       CFS_Output_Bits                 Outputs a bit based on a count   
*                                       and a specific code.             
*       CFS_Input_Counts                Reads the number of counts from  
*                                       the input file builds the table  
*                                       of cumulative counts.            
*       CFS_Init_Arithmetic_Decoder     Initializes the state of the     
*                                       Arithmetic Decoder.              
*       CFS_Get_Symbol_Scale            Retrieves symbol scale.          
*       CFS_Get_Current_Count           Retrieves current Count.         
*       CFS_Convert_Symbol_To_Int       Converts symbol to integer.      
*       CFS_Remove_Symbol_From_Stream   Removes a charcter from the input
*                                       stream.                          
*       CFS_Buf_Putc                    Puts a character in a buffer.    
*       CFS_Count_Bytes                 Counts number of bytes.          
*       CFS_Scale_Counts                Scales the number of byte counts.
*       CFS_Output_Counts               Outputs the byte counts.         
*       CFS_Build_Totals                Builds the table of cumulative   
*                                       totals.                          
*       CFS_Buf_Getc                    Gets a character from the input  
*                                       bit_file.                        
*       CFS_Input_Bit                   Reads a bit from an input file   
*                                       stream.                          
*       CFS_Input_Bits                  Inputs a bit based on a count.   
*       CFS_Output_Bit                  Outputs a Specific bit in the    
*                                       file stream.                     
*                                                                       
* DEPENDENCIES                                                          
*                                                                       
*       nu_websrv.h           
*                                                                       
************************************************************************/

#include "webserv/inc/nu_websr.h"

#ifdef WS_FILE_COMPRESSION
 
/*
 * Internal function prototypes.
 */
static VOID            CFS_Compress_File( WS_BIT_FILE * buf_input, WS_BIT_FILE * buf_output );
static VOID            CFS_Expand_File( WS_BIT_FILE *buf_input, WS_BIT_FILE *buf_output );
static VOID            CFS_Open_Input_Bit_File( WS_BIT_FILE *name,CHAR * buf, INT32 length );
static VOID            CFS_Open_Output_Bit_File( WS_BIT_FILE *name, CHAR * buf );
static VOID            CFS_Output_Bit( WS_BIT_FILE *bit_file, INT bit );
static VOID            CFS_Output_Bits( WS_BIT_FILE *bit_file, UINT32 code, INT count );
static INT             CFS_Input_Bit( WS_BIT_FILE *bit_file );
static VOID            CFS_Close_Output_Bit_File( WS_BIT_FILE *bit_file );
static INT             CFS_Buf_Getc( WS_BIT_FILE *bitfile );
static INT             CFS_Buf_Putc( unsigned int out, WS_BIT_FILE *bitfile );
static VOID            CFS_Build_Model( WS_BIT_FILE *input, WS_BIT_FILE *output );
static VOID            CFS_Scale_Counts( UINT32 counts[], UINT8 scaled_counts[] );
static VOID            CFS_Build_Totals( UINT8 scaled_counts[] );
static VOID            CFS_Count_Bytes( WS_BIT_FILE *input, UINT32 counts[] );
static VOID            CFS_Output_Counts( WS_BIT_FILE *output, UINT8 scaled_counts[] );
static VOID            CFS_Input_Counts( WS_BIT_FILE *input );
static VOID            CFS_Convert_Int_To_Symbol( INT symbol, WS_SYMBOL *s );
static VOID            CFS_Get_Symbol_Scale( WS_SYMBOL *s );
static INT             CFS_Convert_Symbol_To_Int( INT count, WS_SYMBOL *s );
static VOID            CFS_Init_Arithmetic_Encoder( VOID );
static VOID            CFS_Encode_Symbol( WS_BIT_FILE *stream, WS_SYMBOL *s );
static VOID            CFS_Flush_Arithmetic_Encoder( WS_BIT_FILE *stream );
static INT16           CFS_Get_Current_Count( WS_SYMBOL *s );
static VOID            CFS_Init_Arithmetic_Decoder( WS_BIT_FILE *stream );
static VOID            CFS_Remove_Symbol_From_Stream( WS_BIT_FILE *stream, WS_SYMBOL *s );
static VOID            CFS_Fatal_Error( CHAR *s);

#define CFS_END_OF_STREAM 256
#define CFS_PACIFIER_COUNT 2047

#ifndef SEEK_SET
#define SEEK_SET 0
#endif

/*
 * These four variables define the current state of the arithmetic
 * coder/decoder.  
 */
static UINT16 CFS_Code;                     /* The present input code value     */
static UINT16 CFS_Low;                      /* Start of the current code range  */
static UINT16 CFS_High;                     /* End of the current code range    */
static INT32 CFS_Underflow_Bits;            /* Number of underflow bits pending */

static INT16 CFS_Totals[258];               /* The cumulative totals */

/************************************************************************
*                                                                       
* FUNCTION                                                              
*                                                                       
*      CFS_Compress                                                      
*                                                                       
* DESCRIPTION                                                           
*                                                                       
*      Function used to compress an html, gif, jpeg, or text files. This
*      function compresses the files for the in memory file system and  
*      os dependent file system.                                        
*                                                                       
* INPUTS                                                                
*                                                                       
*      *inbuf                   Pointer to the input buffer where the       
*                               file is coming from. Usually     
*                               memory.                          
*      length                   The length of the data to be     
*                               compressed.                      
*      mode                     Flag that idicates whether       
*                               Not Output or Output             
*      *outbuf                  Pointer to the output buffer where the      
*                               the memory is to be stored.      
*                                                                       
* OUTPUTS                                                               
*                                                                       
*      out.length+1             Pointer to next position to be   
*                               compressed.                      
*
************************************************************************/
INT32 CFS_Compress( INT mode, CHAR *inbuf, CHAR *outbuf, INT32 length )
{
    WS_BIT_FILE out;
    WS_BIT_FILE in;

    CFS_Open_Output_Bit_File( &out, outbuf );
    out.mode = mode;
    CFS_Open_Input_Bit_File( &in, inbuf, length );

    CFS_Compress_File( &in, &out );
    CFS_Close_Output_Bit_File( &out );

    return( out.length + 1 );
}

/************************************************************************
*                                                                       
* FUNCTION                                                              
*                                                                       
*       CFS_Decompress                                                    
*                                                                       
* DESCRIPTION                                                           
*                                                                       
*       Function to decompress files resident in the file system.                                        
*                                                                       
* INPUTS                                                                
*                                                                       
*       *req                    Pointer to Request structure that
*                               holds all information pertaining 
*                               to  the HTTP request.            
*       *inbuf                  Pointer to the input buffer to be 
*                               decompressed. 
*       *outbuf                 Pointer to the output buffer of 
*                               decompressed file.                            
*       ilen                    Length of the input buffer.      
*       olen                    Length of the output buffer.     
*                                                                       
* OUTPUTS                                                               
*                                                                       
*       NU_SUCCESS
*                                                                       
***********************************************************************/
INT CFS_Decompress(WS_REQUEST *req, CHAR *inbuf, CHAR *outbuf, INT32 inlen)
{
    WS_BIT_FILE in;
    WS_BIT_FILE out;

#ifdef NU_WEBSERV_DEBUG 
    printf( "CFS_Decompress(%x %x %d) \n", inbuf, outbuf, inlen );
#endif

    CFS_Open_Output_Bit_File( &out, outbuf );
    CFS_Open_Input_Bit_File( &in, inbuf, inlen );

    out.fd = req;
    out.mode = WS_NET_OUTPUT | WS_DONT_OUTPUT;
    out.length = 0;

    CFS_Expand_File( &in, &out );
    CFS_Close_Output_Bit_File( &out );

    return(NU_SUCCESS);
}

/************************************************************************
*                                                                       
* FUNCTION                                                              
*                                                                       
*       CFS_Compress_File                                                     
*                                                                       

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -