[mkbundle] Remove Mono.Posix, ICSharpCode.SharpZipLib deps
[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         /* To decompress gzip format: http://stackoverflow.com/a/1838702/83444 */
17         err = inflateInit2 (&stream, 16+MAX_WBITS);
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 void mono_mkbundle_init ()
43 {
44         CompressedAssembly **ptr;
45         MonoBundledAssembly **bundled_ptr;
46         Bytef *buffer;
47         int nbundles;
48
49         install_dll_config_files ();
50
51         ptr = (CompressedAssembly **) compressed;
52         nbundles = 0;
53         while (*ptr++ != NULL)
54                 nbundles++;
55
56         bundled = (MonoBundledAssembly **) malloc (sizeof (MonoBundledAssembly *) * (nbundles + 1));
57         bundled_ptr = bundled;
58         ptr = (CompressedAssembly **) compressed;
59         while (*ptr != NULL) {
60                 uLong real_size;
61                 uLongf zsize;
62                 int result;
63                 MonoBundledAssembly *current;
64
65                 real_size = (*ptr)->assembly.size;
66                 zsize = (*ptr)->compressed_size;
67                 buffer = (Bytef *) malloc (real_size);
68                 result = my_inflate ((*ptr)->assembly.data, zsize, buffer, real_size);
69                 if (result != 0) {
70                         fprintf (stderr, "Error %d decompresing data for %s\n", result, (*ptr)->assembly.name);
71                         exit (1);
72                 }
73                 (*ptr)->assembly.data = buffer;
74                 current = (MonoBundledAssembly *) malloc (sizeof (MonoBundledAssembly));
75                 memcpy (current, *ptr, sizeof (MonoBundledAssembly));
76                 current->name = (*ptr)->assembly.name;
77                 *bundled_ptr = current;
78                 bundled_ptr++;
79                 ptr++;
80         }
81         *bundled_ptr = NULL;
82         mono_register_bundled_assemblies((const MonoBundledAssembly **) bundled);
83 }