OSDN Git Service

2013.10.24
[uclinux-h8/uClinux-dist.git] / user / ramimage / expand.c
1 /* expand.c: expand a file with holes into another
2  *
3  * Copyright (C) 1998  Kenneth Albanowski <kjahds@kjahds.com>,
4  *                     D. Jeff Dionne <jeff@lineo.ca>,
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * gerg@snapgear.com -- 9/4/1999 -- hacked to be stand alone program.
12  */
13  
14 #include <unistd.h>
15 #include <stdlib.h>
16 #include <stdio.h>
17 #include <sys/types.h>
18 #include <sys/stat.h>
19 #include <fcntl.h>
20
21
22 #if 0
23 #define ntohl(x) (x)
24 #endif
25
26 int
27 expand(char *from, char *to)
28 {
29   int fdi;
30   int fdo;
31   unsigned int pos, prepos;
32   unsigned int len, prelen, n;
33   unsigned int count;
34   char *buf;
35
36   count = 0;
37
38   if ((fdi = open(from,O_RDONLY)) < 0) {
39     fprintf(stderr,"Can't open compressed file %s\n",from);
40     return 0;
41   }
42
43   if ((fdo = open(to,O_WRONLY | O_CREAT | O_TRUNC, 0666)) < 0) {
44     fprintf(stderr,"Can't open expanded file %s\n",to);
45     close(fdi);
46     return 0;
47   }
48   
49   
50   
51   if (!(buf = malloc(2048))) {
52     fprintf(stderr,"can't allocate memory\n");
53     close(fdi);
54     close(fdo);
55     return 0;
56   }
57
58   /* Prefill */
59   read(fdi,(char *)&len,4);
60 fprintf(stderr, "TOTAL LEN=%x", len);
61   len = ntohl(len);
62 fprintf(stderr, "[%x]\n", len);
63
64   memset(buf, 0, 2048);
65   while(len>0) {
66         n = (len > 2048) ? 2048 : len;
67         write(fdo, buf, n);
68         len -= n;
69   }
70   lseek(fdo, 0, SEEK_SET);
71   
72   /* ZRLE */
73   while (read(fdi,&pos,4) == 4) {
74     if (read(fdi,&len,4) != 4) break;
75 prepos = pos;
76 prelen = len;
77     pos = ntohl(pos);
78     len = ntohl(len);
79 fprintf(stderr, "POS=%x[%x]:LEN=%x[%x]", prepos, pos, prelen, len);
80     
81     lseek(fdo,pos,SEEK_SET);
82 fprintf(stderr, "    -->    DATA=%x\n", buf[0]);
83 fflush(stderr);
84     read(fdi,buf,len);
85     write(fdo,buf,len);
86   }
87
88   close(fdi);
89   close(fdo);
90   free(buf);
91 }
92
93 int main(int argc, char *argv[])
94 {
95         if (argc != 3) {
96                 printf("usage: expand <from-file> <to-file>\n");
97                 exit(1);
98         }
99
100         printf("expand: from=%s to=%s\n", argv[1], argv[2]);
101         expand(argv[1], argv[2]);
102         exit(0);
103 }
104