Merge pull request #819 from brendanzagaeski/patch-1
[mono.git] / mcs / tools / mkbundle / template_z.c
1 static MonoBundledAssembly **bundled;
2
3 #include <stdio.h>
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
18         // http://www.zlib.net/manual.html
19         err = inflateInit2 (&stream, 16+MAX_WBITS);
20         if (err != Z_OK)
21                 return 1;
22
23         for (;;) {
24                 stream.next_out = uncompr;
25                 stream.avail_out = (uInt) uncompr_len;
26                 err = inflate (&stream, Z_NO_FLUSH);
27                 if (err == Z_STREAM_END) break;
28                 if (err != Z_OK) {
29                         printf ("%d\n", err);
30                         return 2;
31                 }
32         }
33
34         err = inflateEnd (&stream);
35         if (err != Z_OK)
36                 return 3;
37
38         if (stream.total_out != uncompr_len)
39                 return 4;
40         
41         return 0;
42 }
43
44 void mono_mkbundle_init ()
45 {
46         CompressedAssembly **ptr;
47         MonoBundledAssembly **bundled_ptr;
48         Bytef *buffer;
49         int nbundles;
50
51         install_dll_config_files ();
52
53         ptr = (CompressedAssembly **) compressed;
54         nbundles = 0;
55         while (*ptr++ != NULL)
56                 nbundles++;
57
58         bundled = (MonoBundledAssembly **) malloc (sizeof (MonoBundledAssembly *) * (nbundles + 1));
59         bundled_ptr = bundled;
60         ptr = (CompressedAssembly **) compressed;
61         while (*ptr != NULL) {
62                 uLong real_size;
63                 uLongf zsize;
64                 int result;
65                 MonoBundledAssembly *current;
66
67                 real_size = (*ptr)->assembly.size;
68                 zsize = (*ptr)->compressed_size;
69                 buffer = (Bytef *) malloc (real_size);
70                 result = my_inflate ((*ptr)->assembly.data, zsize, buffer, real_size);
71                 if (result != 0) {
72                         fprintf (stderr, "mkbundle: Error %d decompressing data for %s\n", result, (*ptr)->assembly.name);
73                         exit (1);
74                 }
75                 (*ptr)->assembly.data = buffer;
76                 current = (MonoBundledAssembly *) malloc (sizeof (MonoBundledAssembly));
77                 memcpy (current, *ptr, sizeof (MonoBundledAssembly));
78                 current->name = (*ptr)->assembly.name;
79                 *bundled_ptr = current;
80                 bundled_ptr++;
81                 ptr++;
82         }
83         *bundled_ptr = NULL;
84         mono_register_bundled_assemblies((const MonoBundledAssembly **) bundled);
85 }