hdpt 发表于 2004-10-31 22:07:29

大家看看这些代码...是否可以移植到x86上面来用.

rt

/* $Id: xunpak.c,v 3.2 1995/11/17 07:42:06 hampton Exp $
* $Source: /swtal/cherf.111/ios/boot/xunpak.c,v $
*------------------------------------------------------------------
* LZHUF.C English version 1.0
*
* Based on Japanese version 29-NOV-1988
* LZSS coded by Haruhiko OKUMURA
* Adaptive Huffman Coding coded by Haruyasu YOSHIZAKI
* Edited and translated to English by Kenji RIKITAKE
*------------------------------------------------------------------
* $Log: xunpak.c,v $
* Revision 3.21995/11/1707:42:06hampton
* Remove old entries from the RCS header logs.
*
* Revision 3.11995/11/0909:05:09shaker
* Bump version numbers from 2.x to 3.x.
*
* Revision 2.11995/06/0719:13:04hampton
* Bump version numbers from 1.x to 2.x.
*
*------------------------------------------------------------------
* $Endlog$
*/

#ifndef LIB
#include <stdio.h>
#include <string.h>
#include <ctype.h>

/* These values are Turbo-C dependent;
   EXIT_SUCCESS, EXIT_FAILURE
   renamed by Kenji */

#define EXIT_OK 0
#define EXIT_FAILED -1

   FILE*infile, *outfile;
#endif

   unsigned char *inptr, *outptr, *instart, *outstart;
   long inlength;
#define putc(c,file) *outptr++ = c

unsigned long inttextsize = 0, codesize = 0, printcount = 0;

void Error(char *message)
{
#ifndef LIB
        printf("\n%s\n", message);
        exit(EXIT_FAILED);
#endif
}

/* LZSS Parameters */

#define N                4096        /* Size of string buffer */
#define F                60        /* Size of look-ahead buffer */
#define THRESHOLD        2
#define NIL                N        /* End of tree's node*/

/* Huffman coding parameters */

#define N_CHAR        (256 - THRESHOLD + F)
                                /* character code (= 0..N_CHAR-1) */
#define T                 (N_CHAR * 2 - 1)        /* Size of table */
#define R                 (T - 1)                        /* root position */
#define MAX_FREQ        0x8000
                                        /* update when cumulative frequency */
                                        /* reaches to this value */

typedef unsigned char uchar;

/*
* Tables for encoding/decoding upper 6 bits of
* sliding dictionary pointer
*/

/* decoder table */
uchar d_code = {
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
/*40*/        0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
        0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,
        0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,
        0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03,
        0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03,
/*80*/        0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,
        0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
        0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
        0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
        0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08,
/*120*/        0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09,
        0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A,
        0x0B, 0x0B, 0x0B, 0x0B, 0x0B, 0x0B, 0x0B, 0x0B,
        0x0C, 0x0C, 0x0C, 0x0C, 0x0D, 0x0D, 0x0D, 0x0D,
        0x0E, 0x0E, 0x0E, 0x0E, 0x0F, 0x0F, 0x0F, 0x0F,
/*160*/        0x10, 0x10, 0x10, 0x10, 0x11, 0x11, 0x11, 0x11,
        0x12, 0x12, 0x12, 0x12, 0x13, 0x13, 0x13, 0x13,
        0x14, 0x14, 0x14, 0x14, 0x15, 0x15, 0x15, 0x15,
        0x16, 0x16, 0x16, 0x16, 0x17, 0x17, 0x17, 0x17,
        0x18, 0x18, 0x19, 0x19, 0x1A, 0x1A, 0x1B, 0x1B,
        0x1C, 0x1C, 0x1D, 0x1D, 0x1E, 0x1E, 0x1F, 0x1F,
        0x20, 0x20, 0x21, 0x21, 0x22, 0x22, 0x23, 0x23,
        0x24, 0x24, 0x25, 0x25, 0x26, 0x26, 0x27, 0x27,
        0x28, 0x28, 0x29, 0x29, 0x2A, 0x2A, 0x2B, 0x2B,
        0x2C, 0x2C, 0x2D, 0x2D, 0x2E, 0x2E, 0x2F, 0x2F,
        0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
        0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x3E, 0x3F,
};
#ifdef V0
uchar d_len = {
        0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03,
        0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03,
        0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03,
        0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03,
        0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,
        0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,
        0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,
        0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,
        0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,
        0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,
        0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
        0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
        0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
        0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
        0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
        0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
        0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
        0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
        0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
        0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
        0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
        0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
        0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
        0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
        0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
        0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
        0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
        0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
        0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
        0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
        0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08,
        0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08,
};
#endif
unsigned char text_buf;
short match_position, match_length;

unsigned short freq;        /* cumulative freq table */

/*
* pointing parent nodes.
* area are pointers for leaves
*/
short prnt;

/* pointing children nodes (son[], son[] + 1)*/
short son;

unsigned short getbuf = 0;
uchar getlen = 0;

unsigned short GetBit(void)        /* get one bit */
{
        short i;

        while (getlen <= 8) {
                if (inlength > 0) {
                  i = *inptr++; inlength--;
                } else i = 0;
                getbuf |= i << (8 - getlen);
                getlen += 8;
        }
        i = getbuf;
        getbuf <<= 1;
        getlen--;
        return (i < 0);
}


int GetByte(void)        /* get a byte */
{
        unsigned short i;

        while (getlen <= 8) {
                i = inlength > 0 ? inlength--, *inptr++ : 0;
                getbuf |= (unsigned short) i << (8 - getlen);
                getlen += 8;
        }
        (unsigned short) i = getbuf;
        getbuf <<= 8;
        getlen -= 8;
        return((unsigned short) i >> 8);
}


/* initialize freq tree */

void StartHuff()
{
        short i, j;

        for (i = 0; i < N_CHAR; i++) {
                freq = 1;
                son = i + T;
                prnt = i;
        }
        i = 0; j = N_CHAR;
        while (j <= R) {
                freq = freq + freq;
                son = i;
                prnt = prnt = j;
                i += 2; j++;
        }
        freq = 0xffff;
        prnt = 0;
}


/* reconstruct freq tree */

void reconst()
{
        short i, j, k;
        unsigned short f, l;

        /* halven cumulative freq for leaf nodes */
        j = 0;
        for (i = 0; i < T; i++) {
                if (son >= T) {
                        freq = (freq + 1) / 2;
                        son = son;
                        j++;
                }
        }
        /* make a tree : first, connect children nodes */
        for (i = 0, j = N_CHAR; j < T; i += 2, j++) {
                k = i + 1;
                f = freq = freq + freq;
                for (k = j - 1; f < freq; k--);
                k++;
                l = (j - k) * sizeof(short);
               
                (void)bcopy(&freq, &freq, l);
                freq = f;
                bcopy(&son, &son, l);
                son = i;
        }
        /* connect parent nodes */
        for (i = 0; i < T; i++) {
                if ((k = son) >= T) {
                        prnt = i;
                } else {
                        prnt = prnt = i;
                }
        }
}


/* update freq tree */

void update(short c)
{
        short i, j, k, l;

        if (freq == MAX_FREQ) {
                reconst();
        }
        c = prnt;
        do {
                k = ++freq;

                /* swap nodes to keep the tree freq-ordered */
                if (k > freq) {
                        while (k > freq[++l]);
                        l--;
                        freq = freq;
                        freq = k;

                        i = son;
                        prnt = l;
                        if (i < T) prnt = l;

                        j = son;
                        son = i;

                        prnt = c;
                        if (j < T) prnt = c;
                        son = j;

                        c = l;
                }
        } while ((c = prnt) != 0);        /* do it until reaching the root */
}

short DecodeChar()
{
        unsigned short c;

        c = son;

        /*
       * start searching tree from the root to leaves.
       * choose node #(son[]) if input bit == 0
       * else choose #(son[]+1) (input bit == 1)
       */
        while (c < T) {
                c += GetBit();
                c = son;
        }
        c -= T;
        update(c);
        return(c);
}

short DecodePosition()
{
        unsigned short i, j, c;

        /* decode upper 6 bits from given table */
        i = GetByte();
        c = (unsigned short)(d_code) << 6;
#ifdef V0
        j = d_len;
#else
        if (i < 32) j = 3;
        else if (i < 80) j = 4;
        else if (i < 144) j = 5;
        else if (i < 192) j = 6;
        else if (i < 240) j = 7;
        else j = 8;
#endif
        /* input lower 6 bits directly */
        j -= 2;
        while (j--) {
                i = (i << 1) + GetBit();
        }
        return(c | i & 0x3f);
}

void Decode(void)/* Decoding/Uncompressing */
{
        shorti, j, k, r, c;
        unsigned long intcount;

        bcopy(inptr, &textsize, sizeof textsize);
        inlength -= sizeof textsize;
        inptr += sizeof textsize;
#ifndef LIB
        fprintf(stderr, "\nOriginal file size %ld", textsize);
#endif

        if (textsize == 0)
                return;
        StartHuff();
        for (i = 0; i < N - F; i++)
                text_buf = 0;
        r = N - F;
        for (count = 0; count < textsize; ) {
                c = DecodeChar();
                if (c < 256) {
                        putc(c, outfile);
                        text_buf = c;
                        r &= (N - 1);
                        count++;
                } else {
                        i = (r - DecodePosition() - 1) & (N - 1);
                        j = c - 255 + THRESHOLD;
                        for (k = 0; k < j; k++) {
                                c = text_buf[(i + k) & (N - 1)];
                                putc(c, outfile);
                                text_buf = c;
                                r &= (N - 1);
                                count++;
                        }
                }
        }
        printf("%12ld\n", count);
}

#ifndef LIB
int main(int argc, char *argv[])
{
        char*s;

        printf("\nSizes of data structures:");
        printf("\ntext_buf = %6d", sizeof text_buf);
        printf("\nfreq =   %6d", sizeof freq);
        printf("\nprnt =   %6d", sizeof prnt);
        printf("\nson=   %6d\n\n", sizeof son);

        if (argc != 4) {
                printf("Usage:lzhuf e(compression)|d(uncompression)"
                        " infile outfile\n");
                return EXIT_FAILED;
        }
        if ((s = argv, s || strpbrk(s, "DEde") == NULL)
       || (s = argv, (infile= fopen(s, "rb")) == NULL)
       || (s = argv, (outfile = fopen(s, "wb")) == NULL)) {
                printf("$@HHH(J %s\n", s);
                return EXIT_FAILED;
        }
        fseek(infile, 0L, 2);
        textsize = ftell(infile);
        rewind(infile);
        fprintf(stderr,"\nInput file size %ld", textsize);
        inptr = malloc(textsize + 1000);
        instart = inptr;
        if (fread(inptr, textsize, 1, infile) < 1)
                Error("Unable to read");/* read size of original text */
        outstart = malloc(textsize * 2);
        outptr = outstart;
        inlength = textsize;
        Decode();
        fwrite(outstart, textsize, 1, outfile);
        fclose(infile);
        fclose(outfile);
        free(instart);
        free(outstart);
        return EXIT_OK;
}
#else
uncompress(in,out,len)
    unsigned char *in, *out;
    long len;
{
    inptr = in;
    outptr = out;
    inlength = len;
    Decode();
}
#endif

hdpt 发表于 2004-10-31 22:08:43

/* $Id: cpu.h,v 3.8.4.3 1996/04/08 18:13:27 smackie Exp $
* $Source: /release/112/cvs/Xboot/cpu.h,v $
*------------------------------------------------------------------
* cpu.h -- define the cpu types and associated strings
*
* August 1986, Greg Satz
*
* Copyright (c) 1986-1996,97 by cisco Systems, Inc.
* All rights reserved.
*------------------------------------------------------------------
* $Log: cpu.h,v $
* Revision 3.8.4.31996/04/0818:13:27smackie
* Add new CPU types for LightStream and Grand Junction. (CSCdi53907)
* Branch: California_branch
*
* Revision 3.8.4.21996/04/0502:41:42yoko
* CSCdi53681:add new cpu types
* Branch: California_branch
* define new cpu types and emt calls
*
* Revision 3.8.4.11996/03/2618:24:35enf
* CSCdi52111:Need to reserve #defines for new platform
* Branch: California_branch
* Define CPU_VOLCANO
*
* Revision 3.81996/03/0923:33:31jturner
* CSCdi51191:Make cpu type addition
* Grab cpu type for new platform development.
*
* Revision 3.71996/01/3021:10:55vandys
* CSCdi46080:Deep six CS500 in source base
*
* Revision 3.61996/01/1201:34:51lcheung
* add new cpu type to arkansas
* CSCdi46793:add new cpu type
*
* Revision 3.51995/12/3123:25:04anil
* i86 CPU
*
* Revision 3.41995/12/0317:54:49thille
* CSCdi44905:ciscopro targets not in 11.1
* Put them in.
*
* Revision 3.31995/11/2019:35:09mbeesley
* CSCdi43065:need to reserve cpu types for future development
*
* Revision 3.21995/11/1707:40:58hampton
* Remove old entries from the RCS header logs.
*
* Revision 3.11995/11/0909:04:05shaker
* Bump version numbers from 2.x to 3.x.
*
* Revision 2.41995/11/0820:13:45shaker
* Merge Arkansas_branch into 11.1 mainline.
*
* Revision 2.31995/10/2000:53:57ljiang
* CSCdi42394:Add vendor codes for cisco partners
*
* Revision 2.21995/10/0422:57:24hampton
* Added a new define for an unknown processor family.
*
* Revision 2.11995/06/0719:11:38hampton
* Bump version numbers from 1.x to 2.x.
*
*------------------------------------------------------------------
* $Endlog$
*/

#define        CPU_SMI8        0        /* never used -- predates cisco */
#define        CPU_CADLINC        1        /* never used -- predates cisco */
#define        CPU_SMI10        2        /* never used -- predates cisco */
#define        CPU_FTI10        3        /* never used -- predates cisco */
#define        CPU_CSC1        4        /* also called P20, 68000 @ 10MHz */
#define        CPU_UNIX        5        /* we are a UNIX user mode process */
#define        CPU_CSC2        6        /* also called PIPPIN, 68020 @ 12MHz */
#define        CPU_MPU12        7        /* never used */
#define        CPU_CSC3        8        /* 68020 @ 30 MHz */
#define CPU_STS1        9        /* 68010 in CMC Small Terminal Server */
#define CPU_PAN                10        /* IGS (was called Pancake, or HYBRIDGE II),
                                 16 MHz 68020 */
#define CPU_MERLOT        11        /* Merlot (Token ring IGS), 20 Mhz 68030 */
#define CPU_LEMONADE        12        /* Lemonade (16 line terminal server) (RIP) */
#define        CPU_CSC4        13        /* 68EC040 @ 25 MHz */
#define        CPU_XX        14        /* XX (the 4000), 40 Mhz 68030 */
#define CPU_IGS_BRUT    15        /* IGS-BRUT,68030 20 MHz, w/ no CPU memory */
#define CPU_RP1                16        /* 68EC040 @ 25 MHz */
#define CPU_BASS        17        /* Bass and Pinot */
#define CPU_CRISTAL        18        /* Cristal (3 port merlot) */
#define CPU_CANCUN        19        /* Cancun like Cristal with ASIC's and BRUT */
                                /* Timers */
#define CPU_SIERRA      20      /* The C4000 with the Orion chip (R4600) */
#define CPU_RSP                21        /* integrated route-switch processor */
#define CPU_SAPPHIRE    22      /* 68360 aka "QUICC chip" */
#define CPU_SYNALC        23        /* Synergy ATM Line Card (25MHz 68EC030) */
#define CPU_VIP                24        /* R4600 based IP running IOS */
#define CPU_C7100        25        /* Predator */
#define CPU_i86                26        /* Intel x86    */
#define CPU_RHINO       27      /* LS1010 ATM Switch*/
#define CPU_BRASIL      28      /* The AS5200, a Cancun superset. Define used */
                              /* only for Brasil family type creation. */
#define CPU_VOLCANO        29        /* 68360 (C100X) + Wan Module slot */
#define CPU_C3500       30      /* MARs   */
#define CPU_NP1         31      /* LightStream NP1 - 25Mhz 68040 */
#define CPU_ASP         32      /* LightStream ASP - 75Mhz PowerPC 603 */
#define CPU_MALIBU      33      /* Grand Junction FE Switch - PowerPC 403 */
#define CPU_RINCON      34      /* StrataCom RINCON Access Switch - 33MHz
                                 MC68360 */
#define CPU_JANEIRO   35      /* AS5300, an access server router based on
                                 Sierra platform */
#define CPU_BFRP      36      /* BFR Route Processor */
#define CPU_BFLC      37      /* BFR Line Card */

#define NITRO         42      /* Dial Aggregation Cards - Nitro */

/****************************************************************
* if you add cpu types, please add entries in delay_table
* for spin wait delays in ../sys/os/delay_table.c, and ../boot/asm.S.
* and not to forget to add the string to cpu.c.
* and to the chassis MIB definitions and code as well
* thank you for your consideration.
****************************************************************/

#define CPU_UNKNOWN        43        /* LAST VALUE - unknown processor */

#define        CPU_CISCOPRO_MASK 0x80        /* Mask CPU type for CiscoPro platforms
                                   to hide the CPU's true identity,
                                   but only for EMT_PROCTYPE calls */

/*
* CPU family defines.As family grow please update the following list
*/

#define FAMILY_UNIX   CPU_UNIX
#define FAMILY_CSC2   CPU_CSC2
#define FAMILY_RP1      CPU_RP1
#define FAMILY_PAN      CPU_PAN
#define FAMILY_XX       CPU_XX
#define FAMILY_SIERRA        CPU_SIERRA
#define FAMILY_RSP      CPU_RSP
#define FAMILY_C1000        CPU_SAPPHIRE
#define FAMILY_SYNALC        CPU_SYNALC
#define FAMILY_VIP        CPU_VIP
#define FAMILY_C7100        CPU_C7100
#define FAMILY_i86                CPU_i86
#define FAMILY_BRASIL   CPU_BRASIL
#define FAMILY_VOLCANO        CPU_VOLCANO
#define FAMILY_C3500    CPU_C3500
#define FAMILY_NP1      CPU_NP1
#define FAMILY_ASP      CPU_ASP
#define FAMILY_MALIBU   CPU_MALIBU
#define FAMILY_JANEIROCPU_JANEIRO
#define FAMILY_UNKNOWN        -1

/*
* VENDOR types defines.As vendors grow, please update the following list
*/
#define VENDOR_CISCO                1
#define VENDOR_SYNOPTICS        2
#define VENDOR_CHIPCOM                3
#define VENDOR_CABLETRON        4
#define VENDOR_DEC            5
#define VENDOR_NCR                6
#define VENDOR_USROBOTICS       7
#define VENDOR_ALCATEL          8
#define VENDOR_NEC            9
#define VENDOR_DSC            10
#define VENDOR_MICROCOM         11
#define VENDOR_OKI            12
#define VENDOR_UBN            13
#define VENDOR_HP               14
#define VENDOR_MAX            15
/****************************************************************
* if you add a vendor, you will need to update chassisPartner in the Chassis
* MIB, enumerations must match, never change an enumeration, only add them.
* Mail to [email protected] for enumeration numbering assistant.
*
* Also, you'll need to add the vendor string to the les/init_les.c.
*/

#define IS_FAMILY(family)        ((family) == cpu_family)

#define IS_CPU(cpu)                ((cpu) == cpu_type)

#define IS_VENDOR(a)                ((a) == cookie->vendor)

hdpt 发表于 2004-10-31 22:10:14

就是cisco的IOS....11.2.8的代码..
如果需要我全部发上来..
有40多M.

suowei1979 发表于 2004-11-1 09:40:20

差不多吧

hdpt 发表于 2004-11-2 01:50:25

那么可以移植吗?
页: [1]
查看完整版本: 大家看看这些代码...是否可以移植到x86上面来用.