File: crearc.cpp

package info (click to toggle)
toppler 1.3-3
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 7,868 kB
  • sloc: cpp: 8,979; ansic: 2,349; makefile: 402; sh: 24
file content (163 lines) | stat: -rw-r--r-- 3,913 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <zlib.h>

#include <string>

#define MAXFNAMELEN 150 /* pathnames need a bot more space */
#define NUMFILES 256
#define BUFPADDING 0

typedef struct filenode {
  char name[MAXFNAMELEN];   // actual file name
  std::string aname;  // name of file in the archive
  long start, size, compressed;
} filenode;

int filesize(FILE *f) {
  long pos = ftell(f);
  int erg;
  fseek(f, 0, SEEK_END);
  erg = ftell(f);
  fseek(f, pos, SEEK_SET);
  return erg;
}

int calcsize(filenode * files, int count) {
  int sz = 0;

  for (int i = 0; i < count; i++)
    sz += 13 + files[i].aname.size();

  printf("%i files %i headersize\n", count, sz);
  return sz;
}


int main(int argc, char *argv[])
{
  filenode files[NUMFILES];
  int t, i, filecount = 0;
  FILE *f;
  FILE *outf;
  unsigned char *buffer, *buffer2, *buffer3;

  long erg;
  long largestsize = 0;

  if (argc < 2) return 1;

  for (t = 2; (t < argc) && (filecount < NUMFILES); t++) {
    strncpy(files[filecount].name, argv[t], MAXFNAMELEN);

    files[filecount].aname = files[filecount].name;
    files[filecount].aname = files[filecount].aname.substr(files[filecount].aname.rfind("/")+1);

    printf("%s -> %s\n", files[filecount].name, files[filecount].aname.c_str());


    f = fopen(argv[t], "rb");
    if (!f)
      printf("Error reading file %s!\n", argv[t]);
    else {
      files[filecount].size = filesize(f);
      if (largestsize < (files[filecount].size + BUFPADDING))
        largestsize = files[filecount].size + BUFPADDING;
      filecount++;
      fclose(f);
    }
  }


  buffer = (unsigned char *)malloc(largestsize);
  buffer2 = (unsigned char *)malloc(largestsize);
  buffer3 = (unsigned char *)malloc(largestsize);

  if (!buffer || !buffer2 || !buffer3) {
    printf("Error allocating memory!\n");
    return 1;
  }

  outf = fopen(argv[1], "wb");

  if (!outf) {
    printf("Error writing file %s!\n", argv[1]);
    return 1;
  }

  fwrite(&filecount, 1, 1, outf);
  fwrite(files, calcsize(files, filecount), 1, outf);

  for (t = 0; t < filecount; t++) {
    f = fopen(files[t].name, "rb");

    files[t].size = filesize(f);
    files[t].start = ftell(outf);

    fread(buffer, files[t].size, 1, f);

    files[t].compressed = largestsize;
    compress2(buffer2, (uLongf *)&(files[t].compressed), buffer, files[t].size, 9);
    erg = largestsize;
    uncompress(buffer3, (uLongf *)&(erg), buffer2, files[t].compressed);

    if (erg != files[t].size)
      printf("Different sizes (%li <-> %li)\n", erg, files[t].size);

    for (i = 0; i < files[t].size; i++)
      if (buffer[i] != buffer3[i]) {
        printf("Error in compression %i\n", i);
        break;
      }

    fwrite(buffer2, files[t].compressed, 1, outf);

    fclose(f);
  }
  rewind(outf);

  fwrite(&filecount, 1, 1, outf);

  for (t = 0; t < filecount; t++) {
    fwrite(files[t].aname.c_str(), files[t].aname.size()+1, 1, outf);

    unsigned char tmp = (files[t].start >> 0) & 0xff;
    fwrite(&tmp, 1, 1, outf);
    tmp = (files[t].start >> 8) & 0xff;
    fwrite(&tmp, 1, 1, outf);
    tmp = (files[t].start >> 16) & 0xff;
    fwrite(&tmp, 1, 1, outf);
    tmp = (files[t].start >> 24) & 0xff;
    fwrite(&tmp, 1, 1, outf);

    tmp = (files[t].size >> 0) & 0xff;
    fwrite(&tmp, 1, 1, outf);
    tmp = (files[t].size >> 8) & 0xff;
    fwrite(&tmp, 1, 1, outf);
    tmp = (files[t].size >> 16) & 0xff;
    fwrite(&tmp, 1, 1, outf);
    tmp = (files[t].size >> 24) & 0xff;
    fwrite(&tmp, 1, 1, outf);

    tmp = (files[t].compressed >> 0) & 0xff;
    fwrite(&tmp, 1, 1, outf);
    tmp = (files[t].compressed >> 8) & 0xff;
    fwrite(&tmp, 1, 1, outf);
    tmp = (files[t].compressed >> 16) & 0xff;
    fwrite(&tmp, 1, 1, outf);
    tmp = (files[t].compressed >> 24) & 0xff;
    fwrite(&tmp, 1, 1, outf);
  }

  fclose(outf);

  free(buffer);
  free(buffer2);
  free(buffer3);

  return 0;
}