瀏覽代碼

Create pucrunch.h. Remove argument parsing and file input handling from pucrunch.c, take flags, input buffer and output FILE * instead.

git-svn-id: file:///var/svn/tigccpp/trunk@1132 9552661e-59e3-4036-b4f2-dbe53926924f
kevinkofler 17 年之前
父節點
當前提交
ee52b1a9b4
共有 2 個文件被更改,包括 59 次插入131 次删除
  1. 33 131
      tigcc/ld-tigcc/export/pucrunch.c
  2. 26 0
      tigcc/ld-tigcc/export/pucrunch.h

+ 33 - 131
tigcc/ld-tigcc/export/pucrunch.c

@@ -27,15 +27,12 @@
 
 #include "../generic.h"
 #include "../formats/packhead.h"    // compressed header definition
+#include "pucrunch.h"
 
 #define FIXF_MACHMASK  0xff
 #define FIXF_WRAP	   256
 
 
-#define F_VERBOSE    (1<<0)
-#define F_STATS      (1<<1)
-#define F_ERROR      (1<<15)
-
 #ifndef min
 #define min(a,b) ((a<b)?(a):(b))
 #endif
@@ -96,8 +93,6 @@ unsigned char outBuffer[OUT_SIZE];
 int outPointer = 0;
 int bitMask    = 0x80;
 
-int quiet      = 0;
-
 
 void TTPackInit(void) {
     int i;
@@ -153,52 +148,44 @@ void TTPackInit(void) {
 //=============================================================================
 // the packing code
 //=============================================================================
-int SavePack(int flags ATTRIBUTE_UNUSED,int type, unsigned char *data, int size, char *target,
+int SavePack(int flags ATTRIBUTE_UNUSED,int type, unsigned char *data, int size, FILE *fp,
              int start ATTRIBUTE_UNUSED, int escape, unsigned char *rleValues,
              int endAddr ATTRIBUTE_UNUSED, int extraLZPosBits,
              int memStart ATTRIBUTE_UNUSED, int memEnd ATTRIBUTE_UNUSED)
 {
-    FILE *fp = NULL;
-
     int  i;
 
     if (!data)   return 10;
-    if (!target) fp = stdout;
 
     if ((type & FIXF_MACHMASK) == 0) {
         /* Save without decompressor */
 
-        if (fp || (fp = fopen(target, "wb"))) {
-            PackedHeader   cth;
-            RLEEntries     re;
-
-            cth.origsize_lo  = inlen & 0xff;
-            cth.origsize_hi  = (inlen >> 8);
-            cth.magic1       = MAGIC_CHAR1;
-            cth.magic2       = MAGIC_CHAR2;
-            cth.compsize_lo  = (size + rleUsed + sizeof(PackedHeader)) & 0xff;
-            cth.compsize_hi  = (size + rleUsed + sizeof(PackedHeader)) >> 8;
-            cth.esc1         = (escape >> (8-escBits));
-            cth.notused3     = 0; // just to make sure it has a defined value
-            cth.notused4     = 0; // just to make sure it has a defined value
-            cth.esc2         = escBits;
-            cth.gamma1       = maxGamma + 1;
-            cth.gamma2       = (1 << maxGamma);
-            cth.extralz      = extraLZPosBits;
-            cth.notused1     = 0; // just to make sure it has a defined value
-            cth.notused2     = 0; // just to make sure it has a defined value
-            cth.rleentries   = rleUsed;
-
-            for(i=0; i<rleUsed; i++) re.value[i] = rleValues[i+1];
-
-            fwrite(&cth, 1, sizeof(PackedHeader), fp); // write header
-            fwrite(&re,  1, cth.rleentries, fp);       // write rle values
-            fwrite(data, size, 1, fp);                 // write compressed data
-            if(fp != stdout) fclose(fp);
-            return 0;
-        }
-        fprintf(stderr, "ERROR: Could not open %s for writing\n", target);
-        return 10;
+        PackedHeader   cth;
+        RLEEntries     re;
+
+        cth.origsize_lo  = inlen & 0xff;
+        cth.origsize_hi  = (inlen >> 8);
+        cth.magic1       = MAGIC_CHAR1;
+        cth.magic2       = MAGIC_CHAR2;
+        cth.compsize_lo  = (size + rleUsed + sizeof(PackedHeader)) & 0xff;
+        cth.compsize_hi  = (size + rleUsed + sizeof(PackedHeader)) >> 8;
+        cth.esc1         = (escape >> (8-escBits));
+        cth.notused3     = 0; // just to make sure it has a defined value
+        cth.notused4     = 0; // just to make sure it has a defined value
+        cth.esc2         = escBits;
+        cth.gamma1       = maxGamma + 1;
+        cth.gamma2       = (1 << maxGamma);
+        cth.extralz      = extraLZPosBits;
+        cth.notused1     = 0; // just to make sure it has a defined value
+        cth.notused2     = 0; // just to make sure it has a defined value
+        cth.rleentries   = rleUsed;
+
+        for(i=0; i<rleUsed; i++) re.value[i] = rleValues[i+1];
+
+        fwrite(&cth, 1, sizeof(PackedHeader), fp); // write header
+        fwrite(&re,  1, cth.rleentries, fp);       // write rle values
+        fwrite(data, size, 1, fp);                 // write compressed data
+        return 0;
     }
 
     fprintf(stderr, "FATAL: invalid type!!\n");
@@ -1239,6 +1226,7 @@ int PackLz77(int lzsz, int flags, int *startEscape,int endAddr, int memEnd, int
         escMask = (0xff00>>escBits) & 0xff;
     }
 
+
     if (flags & F_VERBOSE) {
         fprintf(stderr, "Optimizing LZ77 and RLE lengths...");
         fflush(stderr);
@@ -1586,17 +1574,11 @@ errorexit:
 //=============================================================================
 // as usual: the main, but a long one ...
 //=============================================================================
-int TTPack(int argc,char *argv[]) {
+int TTPack(int flags, int in_len, unsigned char *in_data, FILE *out_file) {
     int   startAddr   = 0x258;
-    int   flags       = 0;
     int   lzlen       = -1;
-    int   buflen;
-    int   newlen;
     int   startEscape;
     int   n;
-    char *fileIn  = NULL;
-    char *fileOut = NULL;
-    FILE *infp;
 
     unsigned long timeused = clock();
 
@@ -1604,8 +1586,6 @@ int TTPack(int argc,char *argv[]) {
     int   memEnd      = 0x10000;
     int   type        = 0;
 
-    quiet = 0;
-
 
     TTPackInit();
 
@@ -1616,88 +1596,10 @@ int TTPack(int argc,char *argv[]) {
 
     InitValueLen();
 
-    for (n=1; n<argc; n++) {
-        if (!strcmp(argv[n], "-quiet"))  flags &= ~F_VERBOSE, quiet = 1;
-
-        else if (argv[n][0]=='-' || argv[n][0]=='/') {
-            int i = 1;
-
-            while (argv[n][i]) {
-                switch (argv[n][i]) {
-
-                case 's':
-                    flags |= F_STATS;
-                    break;
-
-                case 'v':
-                    flags |= F_VERBOSE;
-                    quiet = 0;
-                    break;
-
-                case 'h':
-                case '?':
-                    flags |= F_ERROR;
-                    break;
-
-                default:
-                    fprintf(stderr, "ERROR: Unknown option \"%c\"\n",argv[n][i]);
-                    flags |= F_ERROR;
-                }
-                i++;
-            }
-        } else {
-            if (!fileIn) {
-                fileIn = argv[n];
-            }
-            else if (!fileOut) {
-                fileOut = argv[n];
-            }
-            else {
-                fprintf(stderr, "ERROR: Only two filenames wanted!\n");
-                flags |= F_ERROR;
-            }
-        }
-    }
-
-    // input and output file
-    if ((flags & F_ERROR) || !fileIn || !fileOut) {
-        return 1;
-    }
-
     if (lzlen == -1) lzlen = DEFAULT_LZLEN;
 
-    if (fileIn) {
-        if (!(infp = fopen(fileIn, "rb"))) {
-            fprintf(stderr, "ERROR: Could not open %s for reading!\n", fileIn);
-            return 1;
-        }
-    }
-    else {
-        fprintf(stderr, "assuming stdin as text input.\nCtrl-C to abort, Ctrl-Z for EOF.\n");
-        infp = stdin;
-    }
-
-    /* Read in the data */
-    inlen  = 0;
-    buflen = 0;
-    indata = NULL;
-    while (1) {
-        if (buflen < inlen + lrange) {
-            unsigned char *tmp = realloc(indata, buflen + lrange);
-            if (!tmp) {
-                free(indata);
-                fprintf(stderr, "ERROR: realloc failed!\n");
-                return 1;
-            }
-            indata = tmp;
-            buflen += lrange;
-        }
-        newlen = fread(indata + inlen, 1, lrange, infp);
-        if (newlen <= 0) break;
-        inlen += newlen;
-    }
-
-    if (infp != stdin) fclose(infp);
+    inlen  = in_len;
+    indata = in_data;
 
 
     if (startAddr + inlen -1 > 0xffff) {
@@ -1731,7 +1633,7 @@ int TTPack(int argc,char *argv[]) {
         // type      ... may vary
         // outBuffer ... static global array (65536 Bytes)
 
-        SavePack(flags,type, outBuffer, outPointer, fileOut,
+        SavePack(flags,type, outBuffer, outPointer, out_file,
                  startAddr, startEscape, rleValues,
                  endAddr, extraLZPosBits,
                  memStart, memEnd);

+ 26 - 0
tigcc/ld-tigcc/export/pucrunch.h

@@ -0,0 +1,26 @@
+/* pucrunch.h: Pucrunch compression code
+
+   Pucrunch 1997-2005 by Pasi 'Albert' Ojala, a1bert@iki.fi
+   Copyright (C) 2000 Thomas Nussbaumer
+   Copyright (C) 2007 Kevin Kofler
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 2, or (at your option)
+   any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program; if not, write to the Free Software Foundation,
+   Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
+
+#include <stdio.h>
+
+#define F_VERBOSE    (1<<0)
+#define F_STATS      (1<<1)
+
+int TTPack(int flags, int in_len, unsigned char *in_data, FILE *out_file);