home *** CD-ROM | disk | FTP | other *** search
/ RISC DISC 2 / RISC_DISC_2.iso / commercial / oregan / baofdemo / Examples_!UnSqueeze_c_UnSqueeze < prev    next >
Encoding:
Text File  |  1994-01-03  |  2.8 KB  |  79 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <stdarg.h>
  5. #include <time.h>
  6. #include "kernel.h"
  7.  
  8. extern void call_unsqueeze(void *);
  9.  
  10. static unsigned *ImgWords;
  11. static unsigned squeezed_len;
  12. static unsigned unsqueezed_len;
  13. /*==================================================================*/
  14. static void fail(char *errmsg, ...)
  15. {
  16.         va_list ap;
  17.         va_start(ap, errmsg);
  18.         vfprintf(stderr, errmsg, ap);
  19.         fprintf(stderr, "\n");
  20.         va_end(ap);
  21.         exit(1);
  22. }
  23. /*==================================================================*/
  24. static void load_img(char *filename)
  25. {
  26.         _kernel_osfile_block finfo;
  27.         if (_kernel_osfile(5,filename,&finfo) != 1)
  28.                 fail("Error opening '%s'", filename);
  29.         squeezed_len  = finfo.start;
  30.         ImgWords = malloc(squeezed_len);
  31.         if (ImgWords==0)
  32.                 fail("Out of memory loading file '%s'",filename);
  33.         finfo.load =(int)(ImgWords);
  34.         finfo.exec=0;
  35.         if (_kernel_osfile(16,filename,&finfo)<0)
  36.                 fail("Error reading '%s'", filename);
  37. }
  38. /*==================================================================*/
  39. static void save_img(char *filename)
  40. {
  41.         _kernel_osfile_block finfo;
  42.         finfo.load =0xff8;      /* absolute file type */
  43.         finfo.start=(int) ImgWords;
  44.         finfo.end  = finfo.start + unsqueezed_len;
  45.         if (_kernel_osfile(10, filename, &finfo)<0)
  46.                 fail("Failed to write %s\n", filename);
  47. }
  48. /*==================================================================*/
  49. int main(int argc, char *argv[])
  50. {
  51.         char *infile, *outfile;
  52.         time_t timer;
  53.  
  54.         if (argc!=3) fail("Syntax: UnSqueeze <AIF image> <AIF image>");
  55.         infile = argv[1];
  56.         outfile= argv[2];
  57.         timer = clock();
  58.         load_img(infile);
  59.         timer = clock() - timer;
  60.         if (ImgWords[4]!=0xEF000011)   /* SWI OS_Exit */
  61.                 fail("File '%s' is not an AIF image!", infile);
  62.         if (ImgWords[0]==0xFB000000)   /* BLNV address */
  63.                 fail("File '%s' is not squeezed!", infile);
  64.         unsqueezed_len = ImgWords[5] + ImgWords[6];     /* AIF header ReadOnly+ReadWrite */
  65.         /* allow a 32k buffer for decompression workspace */
  66.         ImgWords = realloc(ImgWords, unsqueezed_len + 32*1024);
  67.         if (ImgWords==0)
  68.                 fail("Out of memory trying to unsqueeze '%s'", infile);
  69.         printf("-- Loaded %d bytes in %d csec\n", squeezed_len, timer);
  70.         printf("-- Unsqueezing '%s' to '%s'\n", infile, outfile);
  71.         timer = clock();
  72.         call_unsqueeze(ImgWords);
  73.         timer = clock() - timer;
  74.         if (timer==0) timer++;
  75.         printf("-- Uncompressed size %d, Compressed size %d\n", unsqueezed_len, squeezed_len);
  76.         printf("-- Uncompression took %d csec, %d bytes/cpusec\n", timer, unsqueezed_len/timer );
  77.         save_img(outfile);
  78. }
  79.