File: decompresswmf.c

package info (click to toggle)
abiword 0.7.7-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 20,604 kB
  • ctags: 18,358
  • sloc: cpp: 88,791; ansic: 66,296; sh: 7,777; makefile: 3,397; xml: 687; perl: 361; awk: 273; sed: 36; csh: 28
file content (127 lines) | stat: -rw-r--r-- 2,337 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include "config.h"

#if defined(SYSTEM_ZLIB) && defined(HAVE_MMAP)
#include <zlib.h>
#include <sys/mman.h>
#endif

#include <stdio.h>
#include <errno.h>
#include "wv.h"

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

/*
written by thisguy@somewhere.com who doesnt want his name in the source
*/

/*
theres some notes in the notes dir on compression
*/

int setdecom(void)
    {
#ifdef SYSTEM_ZLIB
	return(1);
#endif
	wvError(("libwv was not compiled against zlib, so wmf files cannot be decompress\n"));
	return(0);
	}

int decompress(FILE *inputfile,FILE *outputfile,U32 inlen,U32 outlen)
	{
#if defined(SYSTEM_ZLIB) && defined(HAVE_MMAP)
	char *compr;
	char *uncompr;
	int err;
	uLong uncomprLen, comprLen;
	
	
	char *input,*output;
	int out;
	int in;
	
	

	if (inputfile == NULL)
		{
		wvError(("danger, file to decompress is NULL\n"));
		return(-1);
		}

	in = fileno(inputfile);

	input = mmap(0,inlen,PROT_READ|PROT_WRITE,MAP_SHARED,in,0);

	if (input == (char *)-1)
		{
		wvError(("unable to mmap inputfile\n"));
		return(-1);
		}

	out = fileno(outputfile);
	lseek(out,outlen,SEEK_SET);
	if (out == -1)
		{
		wvError(("unable to create outputfile\n"));
		munmap(input,inlen);
		exit(-1);
		}
	if (-1 == write(out,"0",1))
		{
		wvError(("unable to write to outputfile\n"));
		munmap(input,inlen);
		close(out);
		exit(-1);
		}
	lseek(out,0,SEEK_SET);

	output = mmap(0,outlen,PROT_READ|PROT_WRITE,MAP_SHARED,out,0);

	if (output == (char *)-1)
		{
		wvError(("map out failed\n"));
		wvError(("%s\n",strerror(errno)));
		munmap(input,inlen);
		close(out);
		exit(-1);
		}

	/* set the size of the file*/
	comprLen = inlen;

	/* Read in the file contents */
	compr = input;
	uncompr = output;
	if (compr == NULL) 
		{
		wvError(("no mem to decompress wmf files\n"));
		return(-1);
		}
	if (uncompr == NULL) 
		{
		wvError(("no mem to decompress wmf files\n"));
		return(-1);
		}
	
	uncomprLen = outlen;	/* This was the trick :( */

	wvTrace(("len is %d %d\n",uncomprLen,comprLen));

	err = uncompress(uncompr, &uncomprLen, compr, comprLen);

	munmap(input,inlen);
	munmap(output,outlen);

	if (err != Z_OK) 
		{
		wvError(("decompress error: %d\n", err)); 
		return(-1); 
		} 
#else
	wvError(("System does not have mmap, if you are so inclined rewrite decompresswmf to not require this\n"));
#endif
	return 0;
	}