-
Notifications
You must be signed in to change notification settings - Fork 3
/
cmp.c
109 lines (84 loc) · 3.05 KB
/
cmp.c
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
/*
* cmp.c
* Copyright (C) 2008, Tomasz Koziara (t.koziara AT gmail.com)
* --------------------------------------------------------------
* compression / decompression
*/
/* This file is part of Solfec.
* Solfec is free software: you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the
* Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
* Solfec is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with Solfec. If not, see <http://www.gnu.org/licenses/>. */
#include <stdlib.h>
#include <string.h>
#include "ext/fastlz.h"
#include "cmp.h"
#include "alg.h"
#include "err.h"
/* compress doubles and integers into an array of integers */
int* compress (CMP_ALG alg, double *d, int doubles, int *i, int ints, int *size)
{
int length, outsize, remainder;
char *input, *output;
length = sizeof (double [doubles]) + sizeof (int [ints]);
ERRMEM (input = malloc (length));
memcpy (input , d, sizeof (double [doubles]));
memcpy (input + sizeof (double [doubles]), i, sizeof (int [ints]));
if (length >= 16)
{
outsize = (int) (1.1 * (double) length);
outsize = MAX (66, outsize);
ERRMEM (output = malloc (outsize));
outsize = fastlz_compress (input, length, output);
free (input);
}
else
{
outsize = length;
output = input;
}
remainder = outsize % sizeof (int);
outsize += sizeof (int [4]) + (remainder ? sizeof (int) - remainder : 0);
ERRMEM (output = realloc (output, outsize));
*size = outsize / sizeof (int);
((int*) output) [(*size) - 4] = alg;
((int*) output) [(*size) - 3] = doubles;
((int*) output) [(*size) - 2] = ints;
((int*) output) [(*size) - 1] = remainder;
return (int*) output;
}
/* decompress doubles and integers from an array of integers */
void decompress (int *input, int size, double **d, int *doubles, int **i, int *ints)
{
int length, outsize, remainder;
char *output;
//CMP_ALG alg;
//alg = input [size - 4];
*doubles = input [size - 3];
*ints = input [size - 2];
remainder = input [size - 1];
if (*doubles) { ERRMEM (*d = malloc (sizeof (double [*doubles]))); }
else *d = NULL;
if (*ints) { ERRMEM (*i = malloc (sizeof (int [*ints]))); }
else *i = NULL;
outsize = sizeof (int) * size;
length = outsize - sizeof (int [4]) + (remainder ? sizeof (int) - remainder : 0);
if (length >= 16)
{
outsize = sizeof (double [*doubles]) + sizeof (int [*ints]);
ERRMEM (output = malloc (outsize));
fastlz_decompress (input, length, output, outsize);
}
else output = (char*) input;
if (*d) memcpy (*d, output, sizeof (double [*doubles]));
if (*i) memcpy (*i, output + sizeof (double [*doubles]), sizeof (int [*ints]));
if (output != (char*) input) free (output);
}