~ubuntu-branches/ubuntu/quantal/pytables/quantal

« back to all changes in this revision

Viewing changes to src/H5Zlzo.c

  • Committer: Bazaar Package Importer
  • Author(s): Sebastian Dröge
  • Date: 2005-11-27 20:25:34 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20051127202534-l8jzyd8357krw40h
Tags: 1.1.1-1ubuntu1
* Sync with Debian:
  + Use python 2.4 as default

Show diffs side-by-side

added added

removed removed

Lines of Context:
4
4
#include "utils.h"
5
5
#include "tables.h"
6
6
 
 
7
#ifdef HAVE_LZO2_LIB
 
8
#define HAVE_LZO_LIB      /* The API for LZO and LZO2 are mostly identical */
 
9
#endif
 
10
 
7
11
#ifdef HAVE_LZO_LIB
8
12
#   include "lzo1x.h"
9
 
 
10
 
/* Test with assembler versions (see later) */
11
 
 
12
 
/* LZO_EXTERN_CDECL(int) */
13
 
/* _lzo1x_decompress_asm_safe( const lzo_byte *src, lzo_uint  src_len, */
14
 
/*                          lzo_byte *dst, lzo_uintp dst_len, */
15
 
/*                          lzo_voidp wrkmem /\* NOT USED *\/ ); */
16
 
 
17
 
 
18
 
void *wrkmem;
19
 
 
20
13
#endif
21
14
 
22
15
/* #undef DEBUG */
23
16
 
24
17
/* Activate the checksum. It is safer and takes only a 1% more of
25
18
   space and a 2% more of CPU (but sometimes is faster than without
26
 
   checksum, which is almost negligible.  F. Alted 2003/07/22
27
 
  
 
19
   checksum, which is almost negligible.  F. Altet 2003/07/22
 
20
 
28
21
   Added code for pytables 0.5 backward compatibility.
29
 
   F. Alted 2003/07/28
 
22
   F. Altet 2003/07/28
30
23
 
31
24
   Added code for saving the uncompressed length buffer as well.
32
 
   F. Alted 2003/07/29
 
25
   F. Altet 2003/07/29
33
26
 
34
27
*/
35
28
 
36
29
/* Ok. from pytables 0.8 on I decided to let the user select the
37
30
   fletcher32 checksum provided in HDF5 1.6 or higher. So, even though
38
31
   the CHECKSUM support here seems pretty stable it will be disabled.
39
 
   F. Alted 2004/01/02 */
40
 
#undef CHECKSUM                        
 
32
   F. Altet 2004/01/02 */
 
33
#undef CHECKSUM
41
34
 
42
35
int register_lzo(void) {
43
36
 
44
37
#ifdef HAVE_LZO_LIB
45
38
 
46
39
  herr_t status;
47
 
  /* Feed the filter_class data structure */
48
 
  /* 1.6.2 */
49
 
  H5Z_class_t filter_class = {
50
 
    (H5Z_filter_t)FILTER_LZO,   /* filter_id */
51
 
    "lzo",                      /* comment */
52
 
    NULL,                       /* can_apply_func */
53
 
    NULL,                       /* set_local_func */
54
 
    (H5Z_func_t)lzo_deflate     /* filter_func */
55
 
  };
56
 
  /* 1.7.x */
57
 
/*   H5Z_class_t filter_class = { */
58
 
/*     H5Z_CLASS_T_VERS,           /\* H5Z_class_t version *\/ */
59
 
/*     (H5Z_filter_t)FILTER_LZO,        /\* filter_id *\/ */
60
 
/*     1, 1,                       /\* Encoding and decoding enabled *\/ */
61
 
/*     "lzo",                   /\* comment *\/ */
62
 
/*     NULL,                       /\* can_apply_func *\/ */
63
 
/*     NULL,                       /\* set_local_func *\/ */
64
 
/*     (H5Z_func_t)lzo_deflate     /\* filter_func *\/ */
65
 
/*   }; */
66
40
 
 
41
#if H5_VERS_MAJOR == 1 && H5_VERS_MINOR < 7
 
42
   /* 1.6.x */
 
43
  H5Z_class_t filter_class = {
 
44
    (H5Z_filter_t)(FILTER_LZO),    /* filter_id */
 
45
    "lzo",                         /* comment */
 
46
    NULL,                          /* can_apply_func */
 
47
    NULL,                          /* set_local_func */
 
48
    (H5Z_func_t)(lzo_deflate)      /* filter_func */
 
49
  };
 
50
#else
 
51
   /* 1.7.x */
 
52
  H5Z_class_t filter_class = {
 
53
    H5Z_CLASS_T_VERS,             /* H5Z_class_t version */
 
54
    (H5Z_filter_t)(FILTER_LZO),   /* filter_id */
 
55
    1, 1,                         /* Encoding and decoding enabled */
 
56
    "lzo",                        /* comment */
 
57
    NULL,                         /* can_apply_func */
 
58
    NULL,                         /* set_local_func */
 
59
    (H5Z_func_t)(lzo_deflate)     /* filter_func */
 
60
  };
 
61
#endif /* if H5_VERSION < "1.7" */
67
62
 
68
63
  /* Init the LZO library */
69
64
  if (lzo_init()!=LZO_E_OK)
71
66
 
72
67
  /* Register the lzo compressor */
73
68
  status = H5Zregister(&filter_class);
74
 
  
75
 
  /* Book a buffer for the compression */
76
 
  wrkmem = (void *)malloc(LZO1X_1_MEM_COMPRESS);
77
 
   
 
69
 
78
70
  return LZO_VERSION; /* lib is available */
79
71
 
80
72
#else
104
96
{
105
97
  size_t ret_value = 0;
106
98
#ifdef HAVE_LZO_LIB
107
 
  void *outbuf = NULL;
 
99
  void *outbuf = NULL, *wrkmem = NULL;
108
100
  int status;
109
101
  size_t  nalloc = *buf_size;
110
102
  lzo_uint out_len = (lzo_uint) nalloc;
147
139
    if ((object_type == Table && object_version >= 20) ||
148
140
        object_type != Table) {
149
141
      nbytes -= 4;              /* Point to uncompressed buffer length */
150
 
      memcpy(&nalloc, ((char *)(*buf)+nbytes), 4);
 
142
      memcpy(&nalloc, ((unsigned char *)(*buf)+nbytes), 4);
151
143
      out_len = nalloc;
152
144
      nbytes -= 4;              /* Point to the checksum */
153
145
#ifdef DEBUG
217
209
      checksum=lzo_adler32(lzo_adler32(0,NULL,0), outbuf, out_len);
218
210
  
219
211
      /* Compare */
220
 
      if (memcmp(&checksum, (char*)(*buf)+nbytes, 4)) {
 
212
      if (memcmp(&checksum, (unsigned char*)(*buf)+nbytes, 4)) {
221
213
        ret_value = 0; /*fail*/
222
214
        fprintf(stderr,"Checksum failed!.\n");
223
215
        goto done;
240
232
    lzo_byte *z_src = (lzo_byte*)(*buf);
241
233
    lzo_byte *z_dst;         /*destination buffer            */
242
234
    lzo_uint z_src_nbytes = (lzo_uint)(nbytes);
243
 
    lzo_uint z_dst_nbytes = (lzo_uint)(nbytes + (nbytes / 64) + 16 + 3);
 
235
    /* The next was the original computation for worst-case expansion */
 
236
    /* I don't know why the difference with LZO1*. Perhaps some wrong docs in
 
237
       LZO package? */
 
238
/*     lzo_uint z_dst_nbytes = (lzo_uint)(nbytes + (nbytes / 64) + 16 + 3); */
 
239
    /* The next is for LZO1* algorithms */
 
240
/*     lzo_uint z_dst_nbytes = (lzo_uint)(nbytes + (nbytes / 16) + 64 + 3); */
 
241
    /* The next is for LZO2* algorithms. This will be the default */
 
242
    lzo_uint z_dst_nbytes = (lzo_uint)(nbytes + (nbytes / 8) + 128 + 3);
244
243
 
245
244
#ifdef CHECKSUM
246
245
    if ((object_type == Table && object_version >= 20) ||
256
255
    }
257
256
 
258
257
    /* Compress this buffer */
259
 
/*     status = lzo1x_1_compress (z_src, z_src_nbytes, z_dst, &z_dst_nbytes, */
260
 
/*                             wrkmem); */
 
258
    wrkmem = malloc(LZO1X_1_MEM_COMPRESS);
 
259
    if (wrkmem == NULL) {
 
260
      fprintf(stderr, "Memory allocation failed for lzo compression\n");
 
261
      ret_value = 0;
 
262
      goto done;
 
263
    }
 
264
 
261
265
    status = lzo1x_1_compress (z_src, z_src_nbytes, z_dst, &z_dst_nbytes,
262
266
                               wrkmem);
263
267
 
 
268
    free(wrkmem);
 
269
    wrkmem = NULL;
 
270
 
264
271
#ifdef CHECKSUM
265
272
    if ((object_type == Table && object_version >= 20) ||
266
273
        object_type != Table) {
270
277
#endif
271
278
      /* Append checksum of *uncompressed* data at the end */
272
279
      checksum = lzo_adler32(lzo_adler32(0,NULL,0), *buf, nbytes);
273
 
      memcpy((char*)(z_dst)+z_dst_nbytes, &checksum, 4);
274
 
      memcpy((char*)(z_dst)+z_dst_nbytes+4, &nbytes, 4);
 
280
      memcpy((unsigned char*)(z_dst)+z_dst_nbytes, &checksum, 4);
 
281
      memcpy((unsigned char*)(z_dst)+z_dst_nbytes+4, &nbytes, 4);
275
282
      z_dst_nbytes += (lzo_uint)4+4;
276
283
      nbytes += 4+4;
277
284
    }