2010-04-23 Jb Evain <jbevain@novell.com>
[mcs.git] / tools / mkbundle / template_z.c
blob7aac318bb88f91034569adbdc887bd2d593aa457
1 static MonoBundledAssembly **bundled;
3 #include <stdlib.h>
4 #include <string.h>
5 #include <zlib.h>
7 static int
8 my_inflate (const Byte *compr, uLong compr_len, Byte *uncompr, uLong uncompr_len)
10 int err;
11 z_stream stream;
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;
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;
31 err = inflateEnd (&stream);
32 if (err != Z_OK)
33 return 3;
35 if (stream.total_out != uncompr_len)
36 return 4;
38 return 0;
41 void mono_mkbundle_init ()
43 CompressedAssembly **ptr;
44 MonoBundledAssembly **bundled_ptr;
45 Bytef *buffer;
46 int nbundles;
48 install_dll_config_files ();
50 ptr = (CompressedAssembly **) compressed;
51 nbundles = 0;
52 while (*ptr++ != NULL)
53 nbundles++;
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;
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);
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++;
80 *bundled_ptr = NULL;
81 mono_register_bundled_assemblies((const MonoBundledAssembly **) bundled);