Ticket #23030: patch-stream.c

File patch-stream.c, 2.3 KB (added by jlaurila@…, 15 years ago)
Line 
1--- ../lrzip-0.42-orig/stream.c 2009-11-29 23:17:48.000000000 +0200
2+++ stream.c    2009-12-25 16:19:58.000000000 +0200
3@@ -19,9 +19,9 @@
4 /* multiplex N streams into a file - the streams are passed
5    through different compressors */
6 
7-#include "rzip.h"
8 #include <bzlib.h>
9 #include <zlib.h>
10+#include "rzip.h"
11 /* LZMA C Wrapper */
12 #include "lzma/C/LzmaLib.h"
13 
14@@ -57,6 +57,53 @@
15 
16 static int lzo_compresses(struct stream *s);
17 
18+#ifndef HAS_MEMSTREAM
19+# define fmemopen fake_fmemopen
20+# define open_memstream fake_open_memstream
21+
22+static FILE *fake_fmemopen(void *buf, size_t buflen, char *mode)
23+{
24+        FILE *in;
25+       if (strcmp(mode, "r")) 
26+                fatal("fake_fmemopen only supports mode \"r\".");
27+       in = tmpfile();
28+       if (!in)
29+               return NULL;
30+       if (1 != fwrite(buf, buflen, 1, in))
31+               return NULL;
32+       rewind(in);
33+        return in;
34+}
35+
36+static FILE *fake_open_memstream(char **buf, size_t *length)
37+{
38+        FILE *out;
39+        if (buf == NULL || length == NULL) 
40+               fatal("NULL parameter to fake_open_memstream");
41+       out = tmpfile();
42+       if (!out)
43+               return NULL;
44+       return out;
45+}
46+
47+static int fake_open_memstream_update_buffer(FILE *fp, uchar **buf, size_t *length)
48+{
49+        long original_pos = ftell(fp);
50+        if (0 != fseek(fp, 0, SEEK_END)) 
51+                return -1;
52+       *length = ftell(fp);
53+       rewind(fp);
54+       *buf = (uchar *)malloc(*length);
55+       if (!*buf)
56+               return -1;
57+       if (1 != fread(*buf, *length, 1, fp))
58+               return -1;
59+       if (0 != fseek(fp, original_pos, SEEK_SET)) 
60+               return -1;
61+       return 0;
62+}
63+#endif
64+
65 /*
66   ***** COMPRESSION FUNCTIONS *****
67 
68@@ -84,6 +131,11 @@
69                fatal("Failed to open_memstream in zpaq_compress_buf\n");
70 
71        zpipe_compress(in, out, s->buflen, (int)(control.flags & FLAG_SHOW_PROGRESS));
72+
73+#ifndef HAS_MEMSTREAM
74+       if (0 != fake_open_memstream_update_buffer(out, &c_buf, &dlen))
75+               fatal("Failed to fake_open_memstream_update_buffer in zpaq_compress_buf");
76+#endif
77        fclose(in);
78        fclose(out);
79 
80@@ -284,6 +336,10 @@
81        }
82 
83        zpipe_decompress(in, out, s->buflen, (int)(control.flags & FLAG_SHOW_PROGRESS));
84+#ifndef HAS_MEMSTREAM
85+       if (0 != fake_open_memstream_update_buffer(out, &c_buf, &dlen))
86+               fatal("Failed to fake_open_memstream_update_buffer in zpaq_decompress_buf");
87+#endif
88        fclose(in);
89        fclose(out);
90        free(s->buf);