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