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