In .:
[mono.git] / mcs / tools / mkbundle / template_z.c
1 static MonoBundledAssembly **bundled;
2 int mono_main (int argc, char* argv[]);
3
4 #include <stdlib.h>
5 #include <string.h>
6 #include <zlib.h>
7
8 static int
9 my_inflate (const Byte *compr, uLong compr_len, Byte *uncompr, uLong uncompr_len)
10 {
11         int err;
12         z_stream stream;
13
14         memset (&stream, 0, sizeof (z_stream));
15         stream.next_in = (Byte *) compr;
16         stream.avail_in = (uInt) compr_len;
17         err = inflateInit (&stream);
18         if (err != Z_OK)
19                 return 1;
20
21         for (;;) {
22                 stream.next_out = uncompr;
23                 stream.avail_out = (uInt) uncompr_len;
24                 err = inflate (&stream, Z_NO_FLUSH);
25                 if (err == Z_STREAM_END) break;
26                 if (err != Z_OK) {
27                         printf ("%d\n", err);
28                         return 2;
29                 }
30         }
31
32         err = inflateEnd (&stream);
33         if (err != Z_OK)
34                 return 3;
35
36         if (stream.total_out != uncompr_len)
37                 return 4;
38         
39         return 0;
40 }
41
42 int main (int argc, char* argv[])
43 {
44         char **newargs = (char **) malloc (sizeof (char *) * (argc + 2));
45         int i;
46         CompressedAssembly **ptr;
47         MonoBundledAssembly **bundled_ptr;
48         Bytef *buffer;
49         int nbundles;
50
51         newargs [0] = argv [0];
52         newargs [1] = image_name;
53         for (i = 1; i < argc; i++)
54                 newargs [i+1] = argv [i];
55         newargs [++i] = NULL;
56
57         if (config_dir != NULL && getenv ("MONO_CFG_DIR") == NULL)
58                 mono_set_dirs (getenv ("MONO_PATH"), config_dir);
59
60         install_dll_config_files ();
61
62         ptr = (CompressedAssembly **) compressed;
63         nbundles = 0;
64         while (*ptr++ != NULL)
65                 nbundles++;
66
67         bundled = (MonoBundledAssembly **) malloc (sizeof (MonoBundledAssembly *) * (nbundles + 1));
68         bundled_ptr = bundled;
69         ptr = (CompressedAssembly **) compressed;
70         while (*ptr != NULL) {
71                 uLong real_size;
72                 uLongf zsize;
73                 int result;
74                 MonoBundledAssembly *current;
75
76                 real_size = (*ptr)->assembly.size;
77                 zsize = (*ptr)->compressed_size;
78                 buffer = (Bytef *) malloc (real_size);
79                 result = my_inflate ((*ptr)->assembly.data, zsize, buffer, real_size);
80                 if (result != 0) {
81                         fprintf (stderr, "Error %d decompresing data for %s\n", result, (*ptr)->assembly.name);
82                         exit (1);
83                 }
84                 (*ptr)->assembly.data = buffer;
85                 current = (MonoBundledAssembly *) malloc (sizeof (MonoBundledAssembly));
86                 memcpy (current, *ptr, sizeof (MonoBundledAssembly));
87                 current->name = (*ptr)->assembly.name;
88                 *bundled_ptr = current;
89                 bundled_ptr++;
90                 ptr++;
91         }
92         *bundled_ptr = NULL;
93         mono_register_bundled_assemblies((const MonoBundledAssembly **) bundled);
94         return mono_main (argc+1, newargs);
95 }