The SWORD Project  1.9.0.svnversion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
XzCompress Class Reference

#include <xzcomprs.h>

+ Inheritance diagram for XzCompress:
+ Collaboration diagram for XzCompress:

Public Member Functions

virtual void decode (void)
 
virtual void encode (void)
 
virtual unsigned long getChars (char *buf, unsigned long len)
 
virtual char * getCompressedBuf (unsigned long *len=0)
 
virtual int getLevel ()
 
virtual char * getUncompressedBuf (unsigned long *len=0)
 
virtual unsigned long sendChars (char *buf, unsigned long len)
 
virtual void setCompressedBuf (unsigned long *len, char *buf=0)
 
virtual void setLevel (int l)
 
virtual void setUncompressedBuf (const char *buf=0, unsigned long *len=0)
 
 XzCompress ()
 
virtual ~XzCompress ()
 

Protected Attributes

char * buf
 
char direct
 
int level
 
unsigned long pos
 
unsigned long slen
 
char * zbuf
 
unsigned long zlen
 
unsigned long zpos
 

Private Attributes

SW_u64 memlimit
 

Detailed Description

Definition at line 34 of file xzcomprs.h.

Constructor & Destructor Documentation

SWORD_NAMESPACE_START XzCompress::XzCompress ( )

Definition at line 39 of file xzcomprs.cpp.

39  : SWCompress() {
40  level = 3;
41 
42  // start with the estimated memory usage for our preset
43  memlimit = lzma_easy_decoder_memusage(level | LZMA_PRESET_EXTREME);
44 
45  // and round up to a power of 2--
46  // bit twiddle hack to determine next greatest power of 2 from:
47  // http://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2
48  memlimit--;
49  memlimit |= memlimit >> 1;
50  memlimit |= memlimit >> 2;
51  memlimit |= memlimit >> 4;
52  memlimit |= memlimit >> 8;
53  memlimit |= memlimit >> 16;
54  memlimit++;
55 
56  // double that for safety's sake
57  memlimit <<= 1;
58 }
SW_u64 memlimit
Definition: xzcomprs.h:37
int level
Definition: swcomprs.h:40
XzCompress::~XzCompress ( )
virtual

Definition at line 65 of file xzcomprs.cpp.

65  {
66 }

Member Function Documentation

void XzCompress::decode ( void  )
virtual

Reimplemented from SWCompress.

Definition at line 131 of file xzcomprs.cpp.

131  {
132  direct = 1; // set direction needed by parent [Get|Send]Chars()
133 
134  // get buffer
135  char chunk[1024];
136  char *zbuf = (char *)calloc(1, 1024);
137  char *chunkbuf = zbuf;
138  int chunklen;
139  unsigned long zlen = 0;
140  while((chunklen = getChars(chunk, 1023))) {
141  memcpy(chunkbuf, chunk, chunklen);
142  zlen += chunklen;
143  if (chunklen < 1023)
144  break;
145  else zbuf = (char *)realloc(zbuf, zlen + 1024);
146  chunkbuf = zbuf + zlen;
147  }
148 
149  //printf("Decoding complength{%ld} uncomp{%ld}\n", zlen, blen);
150  if (zlen) {
151  unsigned long blen = zlen*20; // trust compression is less than 2000%
152  char *buf = new char[blen];
153  //printf("Doing decompress {%s}\n", zbuf);
154  slen = 0;
155  size_t zpos = 0;
156  size_t bpos = 0;
157 
158  switch (lzma_stream_buffer_decode((uint64_t *)&memlimit, 0, NULL, (const uint8_t*)zbuf, &zpos, (size_t)zlen, (uint8_t*)buf, &bpos, (size_t)&blen)){
159  case LZMA_OK: sendChars(buf, bpos); slen = bpos; break;
160  case LZMA_FORMAT_ERROR: fprintf(stderr, "ERROR: format error encountered during decompression.\n"); break;
161  case LZMA_OPTIONS_ERROR: fprintf(stderr, "ERROR: options error encountered during decompression.\n"); break;
162  case LZMA_DATA_ERROR: fprintf(stderr, "ERROR: corrupt data during decompression.\n"); break;
163  case LZMA_NO_CHECK: fprintf(stderr, "ERROR: no_check error encountered during decompression.\n"); break;
164  case LZMA_UNSUPPORTED_CHECK: fprintf(stderr, "ERROR: unsupported_check error encountered during decompression.\n"); break;
165  case LZMA_MEMLIMIT_ERROR: fprintf(stderr, "ERROR: memlimit error encountered during decompression.\n"); break;
166  case LZMA_MEM_ERROR: fprintf(stderr, "ERROR: not enough memory during decompression.\n"); break;
167  case LZMA_BUF_ERROR: fprintf(stderr, "ERROR: not enough room in the out buffer during decompression.\n"); break;
168  case LZMA_PROG_ERROR: fprintf(stderr, "ERROR: program error encountered during decompression.\n"); break;
169  default: fprintf(stderr, "ERROR: an unknown error occurred during decompression.\n"); break;
170  }
171  delete [] buf;
172  }
173  else {
174  fprintf(stderr, "ERROR: no buffer to decompress!\n");
175  }
176  //printf("Finished decoding\n");
177  free (zbuf);
178 }
unsigned long zlen
Definition: swcomprs.h:39
SW_u64 memlimit
Definition: xzcomprs.h:37
unsigned long slen
Definition: swcomprs.h:39
return NULL
Definition: regex.c:7953
free(preg->fastmap)
char * realloc()
char * buf
Definition: swcomprs.h:38
unsigned long zpos
Definition: swcomprs.h:39
virtual unsigned long getChars(char *buf, unsigned long len)
Definition: swcomprs.cpp:121
char direct
Definition: swcomprs.h:38
char * zbuf
Definition: swcomprs.h:38
virtual unsigned long sendChars(char *buf, unsigned long len)
Definition: swcomprs.cpp:141
void XzCompress::encode ( void  )
virtual

Reimplemented from SWCompress.

Definition at line 79 of file xzcomprs.cpp.

79  {
80 
81  direct = 0; // set direction needed by parent [Get|Send]Chars()
82 
83  // get buffer
84  char chunk[1024];
85  char *buf = (char *)calloc(1, 1024);
86  char *chunkbuf = buf;
87  unsigned long chunklen;
88  unsigned long len = 0;
89  while((chunklen = getChars(chunk, 1023))) {
90  memcpy(chunkbuf, chunk, chunklen);
91  len += chunklen;
92  if (chunklen < 1023)
93  break;
94  else buf = (char *)realloc(buf, len + 1024);
95  chunkbuf = buf+len;
96  }
97 
98  zlen = (long)lzma_stream_buffer_bound(len);
99  char *zbuf = new char[zlen+1];
100  size_t zpos = 0;
101 
102  if (len) {
103  //printf("Doing compress\n");
104  switch (lzma_easy_buffer_encode(level | LZMA_PRESET_EXTREME, LZMA_CHECK_CRC64, NULL, (const uint8_t*)buf, (size_t)len, (uint8_t*)zbuf, &zpos, (size_t)zlen)) {
105  case LZMA_OK: sendChars(zbuf, zpos); break;
106  case LZMA_BUF_ERROR: fprintf(stderr, "ERROR: not enough room in the out buffer during compression.\n"); break;
107  case LZMA_UNSUPPORTED_CHECK: fprintf(stderr, "ERROR: unsupported_check error encountered during decompression.\n"); break;
108  case LZMA_OPTIONS_ERROR: fprintf(stderr, "ERROR: options error encountered during decompression.\n"); break;
109  case LZMA_MEM_ERROR: fprintf(stderr, "ERROR: not enough memory during compression.\n"); break;
110  case LZMA_DATA_ERROR: fprintf(stderr, "ERROR: corrupt data during compression.\n"); break;
111  case LZMA_PROG_ERROR: fprintf(stderr, "ERROR: program error encountered during decompression.\n"); break;
112  default: fprintf(stderr, "ERROR: an unknown error occurred during compression.\n"); break;
113  }
114  }
115  else {
116  fprintf(stderr, "ERROR: no buffer to compress\n");
117  }
118  delete [] zbuf;
119  free (buf);
120 }
unsigned long zlen
Definition: swcomprs.h:39
int level
Definition: swcomprs.h:40
return NULL
Definition: regex.c:7953
free(preg->fastmap)
char * realloc()
char * buf
Definition: swcomprs.h:38
unsigned long zpos
Definition: swcomprs.h:39
virtual unsigned long getChars(char *buf, unsigned long len)
Definition: swcomprs.cpp:121
char direct
Definition: swcomprs.h:38
char * zbuf
Definition: swcomprs.h:38
virtual unsigned long sendChars(char *buf, unsigned long len)
Definition: swcomprs.cpp:141
unsigned long SWCompress::getChars ( char *  buf,
unsigned long  len 
)
virtualinherited

Definition at line 121 of file swcomprs.cpp.

121  {
122  if (direct) {
123  len = (((zlen - zpos) > (unsigned)len) ? len : zlen - zpos);
124  if (len > 0) {
125  memmove(ibuf, &zbuf[zpos], len);
126  zpos += len;
127  }
128  }
129  else {
130 // slen = strlen(buf);
131  len = (((slen - pos) > (unsigned)len) ? len : slen - pos);
132  if (len > 0) {
133  memmove(ibuf, &buf[pos], len);
134  pos += len;
135  }
136  }
137  return len;
138 }
unsigned long zlen
Definition: swcomprs.h:39
unsigned long slen
Definition: swcomprs.h:39
char * buf
Definition: swcomprs.h:38
unsigned long zpos
Definition: swcomprs.h:39
char direct
Definition: swcomprs.h:38
unsigned long pos
Definition: swcomprs.h:39
char * zbuf
Definition: swcomprs.h:38
char * SWCompress::getCompressedBuf ( unsigned long *  len = 0)
virtualinherited

Definition at line 111 of file swcomprs.cpp.

111  {
112  if (!zbuf) {
113  direct = 0;
114  encode();
115  }
116  if (len) *len = zlen;
117  return zbuf;
118 }
unsigned long zlen
Definition: swcomprs.h:39
virtual void encode(void)
Definition: swcomprs.cpp:180
char direct
Definition: swcomprs.h:38
char * zbuf
Definition: swcomprs.h:38
virtual int SWCompress::getLevel ( )
inlinevirtualinherited

Definition at line 54 of file swcomprs.h.

54 {return level;};
int level
Definition: swcomprs.h:40
char * SWCompress::getUncompressedBuf ( unsigned long *  len = 0)
virtualinherited

Definition at line 90 of file swcomprs.cpp.

90  {
91  if (!buf) {
92  buf = (char *)calloc(1,1); // be sure we at least allocate an empty buf for return;
93  direct = 1;
94  decode();
95  }
96  if (len) *len = slen;
97  return buf;
98 }
virtual void decode(void)
Definition: swcomprs.cpp:193
unsigned long slen
Definition: swcomprs.h:39
char * buf
Definition: swcomprs.h:38
char direct
Definition: swcomprs.h:38
unsigned long SWCompress::sendChars ( char *  buf,
unsigned long  len 
)
virtualinherited

Definition at line 141 of file swcomprs.cpp.

141  {
142  if (direct) {
143  if (buf) {
144 // slen = strlen(buf);
145  if ((pos + len) > (unsigned)slen) {
146  buf = (char *) realloc(buf, pos + len + 1024);
147  memset(&buf[pos], 0, len + 1024);
148  }
149  }
150  else buf = (char *)calloc(1, len + 1024);
151  memmove(&buf[pos], ibuf, len);
152  pos += len;
153  }
154  else {
155  if (zbuf) {
156  if ((zpos + len) > zlen) {
157  zbuf = (char *) realloc(zbuf, zpos + len + 1024);
158  zlen = zpos + len + 1024;
159  }
160  }
161  else {
162  zbuf = (char *)calloc(1, len + 1024);
163  zlen = len + 1024;
164  }
165  memmove(&zbuf[zpos], ibuf, len);
166  zpos += len;
167  }
168  return len;
169 }
unsigned long zlen
Definition: swcomprs.h:39
unsigned long slen
Definition: swcomprs.h:39
char * realloc()
char * buf
Definition: swcomprs.h:38
unsigned long zpos
Definition: swcomprs.h:39
char direct
Definition: swcomprs.h:38
unsigned long pos
Definition: swcomprs.h:39
char * zbuf
Definition: swcomprs.h:38
void SWCompress::setCompressedBuf ( unsigned long *  len,
char *  buf = 0 
)
virtualinherited

Definition at line 101 of file swcomprs.cpp.

101  {
102  if (ibuf) {
103  init();
104  zbuf = (char *) malloc(*len);
105  memcpy(zbuf, ibuf, *len);
106  zlen = *len;
107  }
108  *len = zlen;
109 }
unsigned long zlen
Definition: swcomprs.h:39
char * malloc()
void init()
Definition: swcomprs.cpp:57
char * zbuf
Definition: swcomprs.h:38
void XzCompress::setLevel ( int  l)
virtual

Reimplemented from SWCompress.

Definition at line 186 of file xzcomprs.cpp.

186  {
187  level = l;
188 
189  // having changed the compression level, we need to adjust our memlimit accordingly,
190  // as in the constructor:
191 
192  // start with the estimated memory usage for our preset
193  memlimit = lzma_easy_decoder_memusage(level | LZMA_PRESET_EXTREME);
194 
195  // and round up to a power of 2--
196  // bit twiddle hack to determine next greatest power of 2 from:
197  // http://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2
198  memlimit--;
199  memlimit |= memlimit >> 1;
200  memlimit |= memlimit >> 2;
201  memlimit |= memlimit >> 4;
202  memlimit |= memlimit >> 8;
203  memlimit |= memlimit >> 16;
204  memlimit++;
205 
206  // double that for safety's sake
207  memlimit <<= 1;
208 };
SW_u64 memlimit
Definition: xzcomprs.h:37
int level
Definition: swcomprs.h:40
void SWCompress::setUncompressedBuf ( const char *  buf = 0,
unsigned long *  len = 0 
)
virtualinherited

Definition at line 75 of file swcomprs.cpp.

75  {
76  if (ibuf) {
77  init();
78  slen = (len) ? *len : strlen(ibuf);
79  buf = (char *) calloc(slen + 1, 1);
80  memcpy(buf, ibuf, slen);
81  }
82  if (!buf) {
83  buf = (char *)calloc(1,1); // be sure we at least allocate an empty buf for return;
84  direct = 1;
85  decode();
86  if (len) *len = slen;
87  }
88 }
virtual void decode(void)
Definition: swcomprs.cpp:193
unsigned long slen
Definition: swcomprs.h:39
char * buf
Definition: swcomprs.h:38
void init()
Definition: swcomprs.cpp:57
char direct
Definition: swcomprs.h:38

Member Data Documentation

char* SWCompress::buf
mutableprotectedinherited

Definition at line 38 of file swcomprs.h.

char SWCompress::direct
mutableprotectedinherited

Definition at line 38 of file swcomprs.h.

int SWCompress::level
protectedinherited

Definition at line 40 of file swcomprs.h.

SW_u64 XzCompress::memlimit
private

Definition at line 37 of file xzcomprs.h.

unsigned long SWCompress::pos
protectedinherited

Definition at line 39 of file swcomprs.h.

unsigned long SWCompress::slen
protectedinherited

Definition at line 39 of file swcomprs.h.

char * SWCompress::zbuf
mutableprotectedinherited

Definition at line 38 of file swcomprs.h.

unsigned long SWCompress::zlen
protectedinherited

Definition at line 39 of file swcomprs.h.

unsigned long SWCompress::zpos
protectedinherited

Definition at line 39 of file swcomprs.h.


The documentation for this class was generated from the following files: