1 /* Copyright 2001 Rien Croonenborghs, Ben Kibbey, Shaun Jackman, Ivan Brozovic
2
3 This file is part of lcab.
4 lcab is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
8 lcab is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12 You should have received a copy of the GNU General Public License
13 along with lcab; if not, write to the Free Software
14 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15 */
16
17 #ifndef CHECKSUMH
18 #include "checksum.h"
19 #endif
20 #include "cwrite.h"
21 //#include "checksum.h"
22
23 // write header of cabfile
cheaderwrite(struct cheader * ch,FILE * fpout)24 int cheaderwrite(struct cheader *ch, FILE *fpout)
25 {
26 int error=0;
27 if(!fwrite((char *) &ch->sig, 4, 1, fpout)) error=1;
28 if(!fwrite((char *) &ch->res1, 4, 1, fpout)) error=1;
29 if(!fwrite((char *) &ch->size, 4, 1, fpout)) error=1;
30 if(!fwrite((char *) &ch->res2, 4, 1, fpout)) error=1;
31 if(!fwrite((char *) &ch->offsetfiles, 4, 1, fpout)) error=1;
32 if(!fwrite((char *) &ch->res3, 4, 1, fpout)) error=1;
33 if(!fwrite((char *) &ch->versionMIN, 1, 1, fpout)) error=1;
34 if(!fwrite((char *) &ch->versionMAJ, 1, 1, fpout)) error=1;
35 if(!fwrite((char *) &ch->nfolders, 2, 1, fpout)) error=1;
36 if(!fwrite((char *) &ch->nfiles, 2, 1, fpout)) error=1;
37 if(!fwrite((char *) &ch->flags, 2, 1, fpout)) error=1;
38 if(!fwrite((char *) &ch->setID, 2, 1, fpout)) error=1;
39 if(!fwrite((char *) &ch->cabID, 2, 1, fpout)) error=1;
40
41 return !error;
42 }
43
44 // write folder-entry of cabfile
cfolderwrite(struct cfolder * cf,FILE * fpout)45 int cfolderwrite(struct cfolder *cf, FILE *fpout)
46 {
47 int error=0;
48
49 if(!fwrite((char *) &cf->offsetdata, 4, 1, fpout)) error=1;
50 if(!fwrite((char *) &cf->ndatab, 2, 1, fpout)) error=1;
51 if(!fwrite((char *) &cf->typecomp, 2, 1, fpout)) error=1;
52
53 return !error;
54 }
55
56
57 // writre file-entry of cabfile
cfilewrite(struct cfile * cf,FILE * fpout)58 int cfilewrite( struct cfile *cf, FILE *fpout )
59 {
60 int error=0;
61
62 //printf("writing: %s (%d)\n", &cf->name, strlen( &cf->name ) );
63
64 if(!fwrite((char *) &cf->usize, 4, 1, fpout)) error=1;
65 if(!fwrite((char *) &cf->uoffset, 4, 1, fpout)) error=1;
66 if(!fwrite((char *) &cf->index, 2, 1, fpout)) error=1;
67 if(!fwrite((char *) &cf->date, 2, 1, fpout)) error=1;
68 if(!fwrite((char *) &cf->time, 2, 1, fpout)) error=1;
69 if(!fwrite((char *) &cf->fattr, 2, 1, fpout)) error=1;
70 if(!fwrite((char *) &cf->name, strlen(cf->name)+1, 1, fpout)) error=1;
71
72 return !error;
73 }
74
75
76 // calculate checksum for datablocks
77 // write cabdata-header and data itself
writedata(struct cdata * cd,long pos,FILE * fpout,FILE * fptemp)78 long writedata( struct cdata *cd, long pos, FILE *fpout, FILE *fptemp )
79 {
80 char *tmp;
81 CHECKSUM csum=0;
82
83 fseek( fptemp,pos, SEEK_SET );
84 tmp = (byte *) calloc( cd->ncbytes, sizeof(byte) );
85 fread( tmp, cd->ncbytes, 1, fptemp );
86
87 // compute checksum
88 csum = compute_checksum( (byte *) &cd->ncbytes,
89 sizeof( cd->ncbytes ) + sizeof( cd->nubytes ),
90 compute_checksum( tmp, cd->ncbytes, 0 ) );
91 cd->checksum = csum;
92 // file header
93 fwrite((char *) &cd->checksum, 4, 1, fpout);
94 fwrite((char *) &cd->ncbytes, 2, 1, fpout);
95 fwrite((char *) &cd->nubytes, 2, 1, fpout);
96 // data
97 fwrite((byte *) tmp, cd->ncbytes, 1, fpout);
98
99 free( tmp );
100 pos = ftell( fptemp );
101 return pos;
102 }
103
cdatawrite(struct cdata * cd,FILE * fpout,long pos,FILE * fptemp)104 long cdatawrite( struct cdata *cd, FILE *fpout, long pos, FILE *fptemp )
105 {
106 pos = writedata( cd, pos, fpout,fptemp );
107 if(pos!=-1)
108 {
109 return pos;
110 }
111 else
112 {
113 return pos;
114 }
115 }
116
117
118