├── README.md ├── setup.py ├── quicklz.h ├── LICENSE ├── quicklzpy.c └── quicklz.c /README.md: -------------------------------------------------------------------------------- 1 | # python-quicklz 2 | Python module implementing QuickLZ compression 3 | 4 | A clone of QuickLZ (the FAST compression library) 5 | http://www.quicklz.com/ 6 | 7 | https://github.com/robottwo/quicklz 8 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | from distutils.core import setup, Extension 2 | 3 | setup( 4 | name = "quicklz", 5 | version = "1.5.0", 6 | description="QuickLZ Bindings for Python", 7 | author='Sergey Dryabzhinsky', 8 | author_email='sergey.dryabzhinsky@gmail.com', 9 | url='https://github.com/sergey-dryabzhinsky/python-quicklz', 10 | ext_modules = [ 11 | Extension( 12 | "quicklz", 13 | ["quicklz.c", "quicklzpy.c"], 14 | extra_compile_args=[ 15 | "-O2", 16 | "-std=c99", 17 | "-Wall", 18 | "-W", 19 | "-Wundef", 20 | # try fortification 21 | # "-DFORTIFY_SOURCE=2", "-fstack-protector", 22 | # try hard CPU optimization 23 | # "-march=native", 24 | # try Graphite 25 | # "-floop-interchange", "-floop-block", "-floop-strip-mine", "-ftree-loop-distribution", 26 | ] 27 | ) 28 | ], 29 | classifiers=[ 30 | 'License :: OSI Approved :: BSD License', 31 | 'Intended Audience :: Developers', 32 | 'Programming Language :: C', 33 | 'Programming Language :: Python', 34 | 'Programming Language :: Python :: 2.6', 35 | 'Programming Language :: Python :: 2.7', 36 | 'Programming Language :: Python :: 3.2', 37 | 'Programming Language :: Python :: 3.3', 38 | 'Programming Language :: Python :: 3.4', 39 | ], 40 | ) 41 | -------------------------------------------------------------------------------- /quicklz.h: -------------------------------------------------------------------------------- 1 | #ifndef QLZ_HEADER 2 | #define QLZ_HEADER 3 | 4 | // Fast data compression library 5 | // Copyright (C) 2006-2011 Lasse Mikkel Reinhold 6 | // lar@quicklz.com 7 | // 8 | // QuickLZ can be used for free under the GPL 1, 2 or 3 license (where anything 9 | // released into public must be open source) or under a commercial license if such 10 | // has been acquired (see http://www.quicklz.com/order.html). The commercial license 11 | // does not cover derived or ported versions created by third parties under GPL. 12 | 13 | // You can edit following user settings. Data must be decompressed with the same 14 | // setting of QLZ_COMPRESSION_LEVEL and QLZ_STREAMING_BUFFER as it was compressed 15 | // (see manual). If QLZ_STREAMING_BUFFER > 0, scratch buffers must be initially 16 | // zeroed out (see manual). First #ifndef makes it possible to define settings from 17 | // the outside like the compiler command line. 18 | 19 | // 1.5.0 final 20 | 21 | #ifndef QLZ_COMPRESSION_LEVEL 22 | 23 | // 1 gives fastest compression speed. 3 gives fastest decompression speed and best 24 | // compression ratio. 25 | //#define QLZ_COMPRESSION_LEVEL 1 26 | //#define QLZ_COMPRESSION_LEVEL 2 27 | #define QLZ_COMPRESSION_LEVEL 3 28 | 29 | // If > 0, zero out both states prior to first call to qlz_compress() or qlz_decompress() 30 | // and decompress packets in the same order as they were compressed 31 | //#define QLZ_STREAMING_BUFFER 0 32 | //#define QLZ_STREAMING_BUFFER 100000 33 | //#define QLZ_STREAMING_BUFFER 1000000 34 | #define QLZ_STREAMING_BUFFER 1048576 35 | 36 | // Guarantees that decompression of corrupted data cannot crash. Decreases decompression 37 | // speed 10-20%. Compression speed not affected. 38 | #define QLZ_MEMORY_SAFE 39 | #endif 40 | 41 | #define QLZ_VERSION_MAJOR 1 42 | #define QLZ_VERSION_MINOR 5 43 | #define QLZ_VERSION_REVISION 0 44 | 45 | // Using size_t, memset() and memcpy() 46 | #include 47 | 48 | // Verify compression level 49 | #if QLZ_COMPRESSION_LEVEL != 1 && QLZ_COMPRESSION_LEVEL != 2 && QLZ_COMPRESSION_LEVEL != 3 50 | #error QLZ_COMPRESSION_LEVEL must be 1, 2 or 3 51 | #endif 52 | 53 | typedef unsigned int ui32; 54 | typedef unsigned short int ui16; 55 | 56 | // Decrease QLZ_POINTERS for level 3 to increase compression speed. Do not touch any other values! 57 | #if QLZ_COMPRESSION_LEVEL == 1 58 | #define QLZ_POINTERS 1 59 | #define QLZ_HASH_VALUES 4096 60 | #elif QLZ_COMPRESSION_LEVEL == 2 61 | #define QLZ_POINTERS 4 62 | #define QLZ_HASH_VALUES 2048 63 | #elif QLZ_COMPRESSION_LEVEL == 3 64 | #define QLZ_POINTERS 16 65 | #define QLZ_HASH_VALUES 4096 66 | #endif 67 | 68 | // Detect if pointer size is 64-bit. It's not fatal if some 64-bit target is not detected because this is only for adding an optional 64-bit optimization. 69 | #if defined _LP64 || defined __LP64__ || defined __64BIT__ || _ADDR64 || defined _WIN64 || defined __arch64__ || __WORDSIZE == 64 || (defined __sparc && defined __sparcv9) || defined __x86_64 || defined __amd64 || defined __x86_64__ || defined _M_X64 || defined _M_IA64 || defined __ia64 || defined __IA64__ 70 | #define QLZ_PTR_64 71 | #endif 72 | 73 | // hash entry 74 | typedef struct 75 | { 76 | #if QLZ_COMPRESSION_LEVEL == 1 77 | ui32 cache; 78 | #if defined QLZ_PTR_64 && QLZ_STREAMING_BUFFER == 0 79 | unsigned int offset; 80 | #else 81 | const unsigned char *offset; 82 | #endif 83 | #else 84 | const unsigned char *offset[QLZ_POINTERS]; 85 | #endif 86 | 87 | } qlz_hash_compress; 88 | 89 | typedef struct 90 | { 91 | #if QLZ_COMPRESSION_LEVEL == 1 92 | const unsigned char *offset; 93 | #else 94 | const unsigned char *offset[QLZ_POINTERS]; 95 | #endif 96 | } qlz_hash_decompress; 97 | 98 | 99 | // states 100 | typedef struct 101 | { 102 | #if QLZ_STREAMING_BUFFER > 0 103 | unsigned char stream_buffer[QLZ_STREAMING_BUFFER]; 104 | #endif 105 | size_t stream_counter; 106 | qlz_hash_compress hash[QLZ_HASH_VALUES]; 107 | unsigned char hash_counter[QLZ_HASH_VALUES]; 108 | } qlz_state_compress; 109 | 110 | 111 | #if QLZ_COMPRESSION_LEVEL == 1 || QLZ_COMPRESSION_LEVEL == 2 112 | typedef struct 113 | { 114 | #if QLZ_STREAMING_BUFFER > 0 115 | unsigned char stream_buffer[QLZ_STREAMING_BUFFER]; 116 | #endif 117 | qlz_hash_decompress hash[QLZ_HASH_VALUES]; 118 | unsigned char hash_counter[QLZ_HASH_VALUES]; 119 | size_t stream_counter; 120 | } qlz_state_decompress; 121 | #elif QLZ_COMPRESSION_LEVEL == 3 122 | typedef struct 123 | { 124 | #if QLZ_STREAMING_BUFFER > 0 125 | unsigned char stream_buffer[QLZ_STREAMING_BUFFER]; 126 | #endif 127 | #if QLZ_COMPRESSION_LEVEL <= 2 128 | qlz_hash_decompress hash[QLZ_HASH_VALUES]; 129 | #endif 130 | size_t stream_counter; 131 | } qlz_state_decompress; 132 | #endif 133 | 134 | 135 | // Support for compilers other than gcc. 136 | #ifndef __inline 137 | #define __inline inline 138 | #endif 139 | 140 | #if defined (__cplusplus) 141 | extern "C" { 142 | #endif 143 | 144 | // Public functions of QuickLZ 145 | size_t qlz_size_decompressed(const char *source); 146 | size_t qlz_size_compressed(const char *source); 147 | size_t qlz_compress(const void *source, char *destination, size_t size, qlz_state_compress *state); 148 | size_t qlz_decompress(const char *source, void *destination, qlz_state_decompress *state); 149 | int qlz_get_setting(int setting); 150 | 151 | #if defined (__cplusplus) 152 | } 153 | #endif 154 | 155 | #endif 156 | 157 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | 203 | -------------------------------------------------------------------------------- /quicklzpy.c: -------------------------------------------------------------------------------- 1 | /** 2 | This is a python module to access to compression and decompression 3 | in the QuickLZ library. This module only provides a few entry points from 4 | Python: 5 | 6 | The main functions: 7 | qlz_compress() 8 | qlz_decompress() 9 | qlz_size_decompressed() 10 | qlz_size_compressed() 11 | 12 | The state object: 13 | QLZStateCompress 14 | 15 | 16 | **/ 17 | #include 18 | 19 | #if PY_MAJOR_VERSION >= 3 20 | #define IS_PY3K 21 | #endif 22 | 23 | #include "quicklz.h" 24 | #if QLZ_STREAMING_BUFFER == 0 25 | #error Define QLZ_STREAMING_BUFFER to a non-zero value for this module 26 | #endif 27 | 28 | struct module_state { 29 | PyObject *error; 30 | }; 31 | 32 | #if PY_MAJOR_VERSION >= 3 33 | #define GETSTATE(m) ((struct module_state*)PyModule_GetState(m)) 34 | #else 35 | #define GETSTATE(m) (&_state) 36 | static struct module_state _state; 37 | #endif 38 | 39 | /* if support for Python 2.5 is dropped the bytesobject.h will do this for us */ 40 | #if PY_MAJOR_VERSION < 3 41 | #define PyBytes_FromStringAndSize PyString_FromStringAndSize 42 | #define _PyBytes_Resize _PyString_Resize 43 | #define PyBytes_AS_STRING PyString_AS_STRING 44 | 45 | #ifndef PyVarObject_HEAD_INIT 46 | #define PyVarObject_HEAD_INIT(type, size) \ 47 | PyObject_HEAD_INIT(type) size, 48 | #endif 49 | #ifndef Py_TYPE 50 | #define Py_TYPE(ob) (((PyObject*)(ob))->ob_type) 51 | #endif 52 | 53 | #endif 54 | 55 | static PyTypeObject QLZStateCompressType; 56 | static PyTypeObject QLZStateDecompressType; 57 | 58 | typedef struct { 59 | PyObject_HEAD 60 | qlz_state_compress* value; 61 | } qlz_state_compress_Type; 62 | 63 | typedef struct { 64 | PyObject_HEAD 65 | qlz_state_decompress* value; 66 | } qlz_state_decompress_Type; 67 | 68 | PyObject * 69 | qlz_state_compress_NEW(PyTypeObject *self) 70 | { 71 | qlz_state_compress_Type *object = (qlz_state_compress_Type *)self->tp_alloc(self, 0); 72 | if (object != NULL) { 73 | object->value = (qlz_state_compress *)malloc(sizeof(qlz_state_compress)); 74 | memset(object->value, 0, sizeof(qlz_state_compress)); 75 | } 76 | return (PyObject *)object; 77 | } 78 | 79 | void 80 | qlz_state_compress_dealloc(PyObject *self) 81 | { 82 | free(((qlz_state_compress_Type*)self)->value); 83 | Py_TYPE(self)->tp_free((PyObject*)self); 84 | } 85 | 86 | /* This is the Python constructor */ 87 | PyObject * 88 | qlz_state_compress_new(PyTypeObject *self, PyObject *args, PyObject *kwds) 89 | { 90 | return qlz_state_compress_NEW(self); 91 | } 92 | 93 | PyObject *qlz_py_getattr(PyObject *self, char * attrname) 94 | { 95 | PyErr_SetString(PyExc_AttributeError, attrname); 96 | return NULL; 97 | } 98 | 99 | PyObject * 100 | qlz_state_decompress_NEW(PyTypeObject *self) 101 | { 102 | qlz_state_decompress_Type *object = (qlz_state_decompress_Type *)self->tp_alloc(self, 0); 103 | if (object != NULL) { 104 | object->value = (qlz_state_decompress *)malloc(sizeof(qlz_state_decompress)); 105 | memset(object->value, 0, sizeof(qlz_state_decompress)); 106 | } 107 | return (PyObject *)object; 108 | } 109 | 110 | void 111 | qlz_state_decompress_dealloc(PyObject *self) 112 | { 113 | free(((qlz_state_decompress_Type*)self)->value); 114 | Py_TYPE(self)->tp_free((PyObject*)self); 115 | } 116 | 117 | /* This is the Python constructor */ 118 | PyObject * 119 | qlz_state_decompress_new(PyTypeObject *self, PyObject *args, PyObject *kwds) 120 | { 121 | return qlz_state_decompress_NEW(self); 122 | } 123 | 124 | PyObject *qlz_getattr(PyObject *self, char * attrname) 125 | { 126 | PyErr_SetString(PyExc_AttributeError, attrname); 127 | return NULL; 128 | } 129 | 130 | int qlz_py_setattr(PyObject *self, char *attrname, PyObject *value) 131 | { 132 | PyErr_SetString(PyExc_AttributeError, attrname); 133 | return -1; 134 | } 135 | 136 | int qlz_compare(PyObject *self, PyObject *other) 137 | { 138 | // 'is a' comparison 139 | return self == other; 140 | } 141 | 142 | int qlz_c_print(PyObject *self, FILE *fp, int flags) 143 | { 144 | fprintf(fp, "QLZStateCompress(%ld)", (long)self); 145 | return 0; 146 | } 147 | 148 | PyObject *qlz_c_str(PyObject *self) 149 | { 150 | char buf[100]; 151 | sprintf(buf, "QLZStateCompress(%ld)", (long)self); 152 | #if PY_MAJOR_VERSION >= 3 153 | return PyBytes_FromString(buf); 154 | #else 155 | return PyString_FromString(buf); 156 | #endif 157 | } 158 | 159 | int qlz_d_print(PyObject *self, FILE *fp, int flags) 160 | { 161 | fprintf(fp, "QLZStateDecompress(%ld)", (long)self); 162 | return 0; 163 | } 164 | 165 | PyObject *qlz_d_str(PyObject *self) 166 | { 167 | char buf[100]; 168 | sprintf(buf, "QLZStateDecompress(%ld)", (long)self); 169 | #if PY_MAJOR_VERSION >= 3 170 | return PyBytes_FromString(buf); 171 | #else 172 | return PyString_FromString(buf); 173 | #endif 174 | } 175 | 176 | long 177 | qlz_hash(PyObject *self) 178 | { 179 | return (long)self; 180 | } 181 | 182 | /** 183 | * The qlz_state_compress python object 184 | */ 185 | static PyTypeObject QLZStateCompressType = { 186 | PyVarObject_HEAD_INIT(NULL, 0) 187 | "QLZStateCompress", /* char *tp_name; */ 188 | sizeof(qlz_state_compress_Type),/* int tp_basicsize; */ 189 | 0, /* int tp_itemsize; /* not used much */ 190 | (destructor)qlz_state_compress_dealloc, /* destructor tp_dealloc; */ 191 | qlz_c_print, /* printfunc tp_print; */ 192 | qlz_py_getattr, /* getattrfunc tp_getattr; /* __getattr__ */ 193 | qlz_py_setattr, /* setattrfunc tp_setattr; /* __setattr__ */ 194 | qlz_compare, /* cmpfunc tp_compare; /* __cmp__ */ 195 | qlz_c_str, /* reprfunc tp_repr; /* __repr__ */ 196 | 0, /* PyNumberMethods *tp_as_number; */ 197 | 0, /* PySequenceMethods *tp_as_sequence; */ 198 | 0, /* PyMappingMethods *tp_as_mapping; */ 199 | qlz_hash, /* hashfunc tp_hash; /* __hash__ */ 200 | 0, /* ternaryfunc tp_call; /* __call__ */ 201 | qlz_c_str, /* reprfunc tp_str; /* __str__ */ 202 | 0, /* tp_getattro */ 203 | 0, /* tp_setattro */ 204 | 0, /* tp_as_buffer */ 205 | Py_TPFLAGS_DEFAULT, /* tp_flags */ 206 | "quicklz objects", /* tp_doc */ 207 | 0, /* tp_traverse */ 208 | 0, /* tp_clear */ 209 | qlz_compare, /* tp_richcompare */ 210 | 0, /* tp_weaklistoffset */ 211 | 0, /* tp_iter */ 212 | 0, /* tp_iternext */ 213 | 0, /* tp_methods */ 214 | 0, /* tp_members */ 215 | 0, /* tp_getset */ 216 | 0, /* tp_base */ 217 | 0, /* tp_dict */ 218 | 0, /* tp_descr_get */ 219 | 0, /* tp_descr_set */ 220 | 0, /* tp_dictoffset */ 221 | 0, /* tp_init */ 222 | 0, /* tp_alloc */ 223 | qlz_state_compress_new, /* tp_new */ 224 | }; 225 | 226 | /** 227 | * The qlz_state_compress python object 228 | */ 229 | static PyTypeObject QLZStateDecompressType = { 230 | PyVarObject_HEAD_INIT(NULL, 0) 231 | "QLZStateDecompress", /* char *tp_name; */ 232 | sizeof(qlz_state_decompress_Type),/* int tp_basicsize; */ 233 | 0, /* int tp_itemsize; /* not used much */ 234 | (destructor)qlz_state_decompress_dealloc,/* destructor tp_dealloc; */ 235 | qlz_d_print, /* printfunc tp_print; */ 236 | qlz_py_getattr, /* getattrfunc tp_getattr; /* __getattr__ */ 237 | qlz_py_setattr, /* setattrfunc tp_setattr; /* __setattr__ */ 238 | qlz_compare, /* cmpfunc tp_compare; /* __cmp__ */ 239 | qlz_d_str, /* reprfunc tp_repr; /* __repr__ */ 240 | 0, /* PyNumberMethods *tp_as_number; */ 241 | 0, /* PySequenceMethods *tp_as_sequence; */ 242 | 0, /* PyMappingMethods *tp_as_mapping; */ 243 | qlz_hash, /* hashfunc tp_hash; /* __hash__ */ 244 | 0, /* ternaryfunc tp_call; /* __call__ */ 245 | qlz_d_str, /* reprfunc tp_str; /* __str__ */ 246 | 0, /* tp_getattro */ 247 | 0, /* tp_setattro */ 248 | 0, /* tp_as_buffer */ 249 | Py_TPFLAGS_DEFAULT, /* tp_flags */ 250 | "quicklz objects", /* tp_doc */ 251 | 0, /* tp_traverse */ 252 | 0, /* tp_clear */ 253 | qlz_compare, /* tp_richcompare */ 254 | 0, /* tp_weaklistoffset */ 255 | 0, /* tp_iter */ 256 | 0, /* tp_iternext */ 257 | 0, /* tp_methods */ 258 | 0, /* tp_members */ 259 | 0, /* tp_getset */ 260 | 0, /* tp_base */ 261 | 0, /* tp_dict */ 262 | 0, /* tp_descr_get */ 263 | 0, /* tp_descr_set */ 264 | 0, /* tp_dictoffset */ 265 | 0, /* tp_init */ 266 | 0, /* tp_alloc */ 267 | qlz_state_decompress_new, /* tp_new */ 268 | }; 269 | 270 | 271 | PyObject *qlz_size_decompressed_py(PyObject *self, PyObject *args) 272 | { 273 | PyObject *result = NULL; 274 | char* buffer; 275 | int buffer_length; 276 | 277 | #if PY_MAJOR_VERSION >= 3 278 | if (PyArg_ParseTuple(args, "y#", &buffer, &buffer_length)) { 279 | #else 280 | if (PyArg_ParseTuple(args, "s#", &buffer, &buffer_length)) { 281 | #endif 282 | result = Py_BuildValue("i", qlz_size_decompressed(buffer)); 283 | } /* otherwise there is an error, 284 | * the exception already raised by PyArg_ParseTuple, and NULL is 285 | * returned. 286 | */ 287 | return result; 288 | } 289 | 290 | PyObject *qlz_size_compressed_py(PyObject *self, PyObject *args) 291 | { 292 | PyObject *result = NULL; 293 | char* buffer; 294 | int buffer_length; 295 | 296 | #if PY_MAJOR_VERSION >= 3 297 | if (PyArg_ParseTuple(args, "y#", &buffer, &buffer_length)) { 298 | #else 299 | if (PyArg_ParseTuple(args, "s#", &buffer, &buffer_length)) { 300 | #endif 301 | result = Py_BuildValue("i", qlz_size_compressed(buffer)); 302 | } /* otherwise there is an error, 303 | * the exception already raised by PyArg_ParseTuple, and NULL is 304 | * returned. 305 | */ 306 | return result; 307 | } 308 | 309 | PyObject *qlz_compress_py(PyObject *self, PyObject *args) 310 | { 311 | PyObject *result = NULL; 312 | PyObject *state = NULL; 313 | char* buffer; 314 | char* compressed_buffer; 315 | int buffer_length; 316 | int size_compressed; 317 | 318 | #if PY_MAJOR_VERSION >= 3 319 | if (PyArg_ParseTuple(args, "y#O!", 320 | &buffer, &buffer_length, 321 | &QLZStateCompressType, &state)) 322 | #else 323 | if (PyArg_ParseTuple(args, "s#O!", 324 | &buffer, &buffer_length, 325 | &QLZStateCompressType, &state)) 326 | #endif 327 | { 328 | compressed_buffer = (char*)malloc(buffer_length+400); 329 | size_compressed = qlz_compress(buffer, compressed_buffer, 330 | buffer_length, 331 | ((qlz_state_compress_Type*)state)->value); 332 | #if PY_MAJOR_VERSION >= 3 333 | result = Py_BuildValue("y#", compressed_buffer, size_compressed); 334 | #else 335 | result = Py_BuildValue("s#", compressed_buffer, size_compressed); 336 | #endif 337 | free(compressed_buffer); 338 | } /* otherwise there is an error, 339 | * the exception already raised by PyArg_ParseTuple, and NULL is 340 | * returned. 341 | */ 342 | return result; 343 | } 344 | 345 | PyObject *qlz_decompress_py(PyObject *self, PyObject *args) 346 | { 347 | PyObject *result = NULL; 348 | PyObject *state = NULL; 349 | char* buffer; 350 | char* decompressed_buffer; 351 | int buffer_length; 352 | int size_decompressed; 353 | 354 | #if PY_MAJOR_VERSION >= 3 355 | if (PyArg_ParseTuple(args, "y#O!", 356 | &buffer, &buffer_length, 357 | &QLZStateDecompressType, &state)) 358 | #else 359 | if (PyArg_ParseTuple(args, "s#O!", 360 | &buffer, &buffer_length, 361 | &QLZStateDecompressType, &state)) 362 | #endif 363 | { 364 | size_decompressed = qlz_size_decompressed(buffer); 365 | decompressed_buffer = (char*)malloc(size_decompressed); 366 | 367 | qlz_decompress(buffer, decompressed_buffer, 368 | ((qlz_state_decompress_Type*)state)->value); 369 | 370 | #if PY_MAJOR_VERSION >= 3 371 | result = Py_BuildValue("y#", decompressed_buffer, size_decompressed); 372 | #else 373 | result = Py_BuildValue("s#", decompressed_buffer, size_decompressed); 374 | #endif 375 | free(decompressed_buffer); 376 | } /* otherwise there is an error, 377 | * the exception already raised by PyArg_ParseTuple, and NULL is 378 | * returned. 379 | */ 380 | return result; 381 | } 382 | 383 | 384 | PyMethodDef QuicklzMethods[] = { 385 | /* {"QLZStateCompress", qlz_state_compress_new, METH_VARARGS, 386 | "An internal object for tracking streaming compression.\n"}, 387 | 388 | {"QLZStateDecompress", qlz_state_decompress_new, METH_VARARGS, 389 | "An internal object for tracking streaming decompression.\n"}, 390 | */ 391 | {"qlz_size_decompressed", qlz_size_decompressed_py, METH_VARARGS, 392 | "qlz_size_decompressed(compressed_data)\n" 393 | "\n" 394 | "How many bytes would the uncompressed data in this chunk be?\n"}, 395 | {"qlz_size_compressed", qlz_size_compressed_py, METH_VARARGS, 396 | "qlz_size_compressed(compressed_data)\n" 397 | "\n" 398 | "How many bytes is the compressed data in this chunk?\n"}, 399 | 400 | {"qlz_compress", qlz_compress_py, METH_VARARGS, 401 | "qlz_compress(raw_data, state)\n" 402 | "Compress a chunk of data using QuickLZ.\n" 403 | "\n" 404 | "If the same state object is used to compress data sequentially,\n" 405 | "then chunks must also be decompressed using a state object in the same\n" 406 | "sequence.\n" 407 | "\n" 408 | "@param raw_data: string-like\n" 409 | "@param state: a QLZStateCompress object.\n"}, 410 | 411 | {"qlz_decompress", qlz_decompress_py, METH_VARARGS, 412 | "qlz_decompress(compressed_chunk, state)\n" 413 | "Decompress a chunk of data using QuickLZ." 414 | "\n" 415 | "If the same state object is used to compress data sequentially,\n" 416 | "then chunks must also be decompressed using a state object in the same\n" 417 | "sequence.\n" 418 | "\n" 419 | "@param compressed_chunk: string-like\n" 420 | "@param state: a QLZStateDecompress object.\n"}, 421 | 422 | {NULL, NULL}, 423 | }; 424 | 425 | 426 | #if PY_MAJOR_VERSION >= 3 427 | 428 | static int myextension_traverse(PyObject *m, visitproc visit, void *arg) { 429 | Py_VISIT(GETSTATE(m)->error); 430 | return 0; 431 | } 432 | 433 | static int myextension_clear(PyObject *m) { 434 | Py_CLEAR(GETSTATE(m)->error); 435 | return 0; 436 | } 437 | 438 | 439 | static struct PyModuleDef moduledef = { 440 | PyModuleDef_HEAD_INIT, 441 | "quicklz", 442 | "QuickLZ module", 443 | sizeof(struct module_state), 444 | QuicklzMethods, 445 | NULL, 446 | myextension_traverse, 447 | myextension_clear, 448 | NULL 449 | }; 450 | 451 | #define INITERROR return NULL 452 | 453 | PyObject * 454 | PyInit_quicklz(void) 455 | #else 456 | 457 | #define INITERROR return 458 | 459 | void 460 | initquicklz(void) 461 | #endif 462 | { 463 | 464 | if (PyType_Ready(&QLZStateCompressType) < 0) 465 | return ; 466 | if (PyType_Ready(&QLZStateDecompressType) < 0) 467 | return ; 468 | 469 | #if PY_MAJOR_VERSION >= 3 470 | PyObject *module = PyModule_Create(&moduledef); 471 | #else 472 | PyObject *module = Py_InitModule("quicklz", QuicklzMethods); 473 | #endif 474 | 475 | if (module == NULL) 476 | INITERROR; 477 | struct module_state *st = GETSTATE(module); 478 | 479 | st->error = PyErr_NewException("quicklz.Error", NULL, NULL); 480 | if (st->error == NULL) { 481 | Py_DECREF(module); 482 | INITERROR; 483 | } 484 | 485 | Py_INCREF(&QLZStateCompressType); 486 | PyModule_AddObject(module, "QLZStateCompress", (PyObject *)&QLZStateCompressType); 487 | 488 | Py_INCREF(&QLZStateDecompressType); 489 | PyModule_AddObject(module, "QLZStateDecompress", (PyObject *)&QLZStateDecompressType); 490 | 491 | #if PY_MAJOR_VERSION >= 3 492 | return module; 493 | #endif 494 | } 495 | -------------------------------------------------------------------------------- /quicklz.c: -------------------------------------------------------------------------------- 1 | // Fast data compression library 2 | // Copyright (C) 2006-2011 Lasse Mikkel Reinhold 3 | // lar@quicklz.com 4 | // 5 | // QuickLZ can be used for free under the GPL 1, 2 or 3 license (where anything 6 | // released into public must be open source) or under a commercial license if such 7 | // has been acquired (see http://www.quicklz.com/order.html). The commercial license 8 | // does not cover derived or ported versions created by third parties under GPL. 9 | 10 | // 1.5.0 final 11 | 12 | #include "quicklz.h" 13 | 14 | #if QLZ_VERSION_MAJOR != 1 || QLZ_VERSION_MINOR != 5 || QLZ_VERSION_REVISION != 0 15 | #error quicklz.c and quicklz.h have different versions 16 | #endif 17 | 18 | #if (defined(__X86__) || defined(__i386__) || defined(i386) || defined(_M_IX86) || defined(__386__) || defined(__x86_64__) || defined(_M_X64)) 19 | #define X86X64 20 | #endif 21 | 22 | #define MINOFFSET 2 23 | #define UNCONDITIONAL_MATCHLEN 6 24 | #define UNCOMPRESSED_END 4 25 | #define CWORD_LEN 4 26 | 27 | #if QLZ_COMPRESSION_LEVEL == 1 && defined QLZ_PTR_64 && QLZ_STREAMING_BUFFER == 0 28 | #define OFFSET_BASE source 29 | #define CAST (ui32)(size_t) 30 | #else 31 | #define OFFSET_BASE 0 32 | #define CAST 33 | #endif 34 | 35 | int qlz_get_setting(int setting) 36 | { 37 | switch (setting) 38 | { 39 | case 0: return QLZ_COMPRESSION_LEVEL; 40 | case 1: return sizeof(qlz_state_compress); 41 | case 2: return sizeof(qlz_state_decompress); 42 | case 3: return QLZ_STREAMING_BUFFER; 43 | #ifdef QLZ_MEMORY_SAFE 44 | case 6: return 1; 45 | #else 46 | case 6: return 0; 47 | #endif 48 | case 7: return QLZ_VERSION_MAJOR; 49 | case 8: return QLZ_VERSION_MINOR; 50 | case 9: return QLZ_VERSION_REVISION; 51 | } 52 | return -1; 53 | } 54 | 55 | #if QLZ_COMPRESSION_LEVEL == 1 56 | static int same(const unsigned char *src, size_t n) 57 | { 58 | while(n > 0 && *(src + n) == *src) 59 | n--; 60 | return n == 0 ? 1 : 0; 61 | } 62 | #endif 63 | 64 | static void reset_table_compress(qlz_state_compress *state) 65 | { 66 | int i; 67 | for(i = 0; i < QLZ_HASH_VALUES; i++) 68 | { 69 | #if QLZ_COMPRESSION_LEVEL == 1 70 | state->hash[i].offset = 0; 71 | #else 72 | state->hash_counter[i] = 0; 73 | #endif 74 | } 75 | } 76 | 77 | static void reset_table_decompress(qlz_state_decompress *state) 78 | { 79 | int i; 80 | (void)state; 81 | (void)i; 82 | #if QLZ_COMPRESSION_LEVEL == 2 83 | for(i = 0; i < QLZ_HASH_VALUES; i++) 84 | { 85 | state->hash_counter[i] = 0; 86 | } 87 | #endif 88 | } 89 | 90 | static __inline ui32 hash_func(ui32 i) 91 | { 92 | #if QLZ_COMPRESSION_LEVEL == 2 93 | return ((i >> 9) ^ (i >> 13) ^ i) & (QLZ_HASH_VALUES - 1); 94 | #else 95 | return ((i >> 12) ^ i) & (QLZ_HASH_VALUES - 1); 96 | #endif 97 | } 98 | 99 | static __inline ui32 fast_read(void const *src, ui32 bytes) 100 | { 101 | #ifndef X86X64 102 | unsigned char *p = (unsigned char*)src; 103 | switch (bytes) 104 | { 105 | case 4: 106 | return(*p | *(p + 1) << 8 | *(p + 2) << 16 | *(p + 3) << 24); 107 | case 3: 108 | return(*p | *(p + 1) << 8 | *(p + 2) << 16); 109 | case 2: 110 | return(*p | *(p + 1) << 8); 111 | case 1: 112 | return(*p); 113 | } 114 | return 0; 115 | #else 116 | if (bytes >= 1 && bytes <= 4) 117 | return *((ui32*)src); 118 | else 119 | return 0; 120 | #endif 121 | } 122 | 123 | static __inline ui32 hashat(const unsigned char *src) 124 | { 125 | ui32 fetch, hash; 126 | fetch = fast_read(src, 3); 127 | hash = hash_func(fetch); 128 | return hash; 129 | } 130 | 131 | static __inline void fast_write(ui32 f, void *dst, size_t bytes) 132 | { 133 | #ifndef X86X64 134 | unsigned char *p = (unsigned char*)dst; 135 | 136 | switch (bytes) 137 | { 138 | case 4: 139 | *p = (unsigned char)f; 140 | *(p + 1) = (unsigned char)(f >> 8); 141 | *(p + 2) = (unsigned char)(f >> 16); 142 | *(p + 3) = (unsigned char)(f >> 24); 143 | return; 144 | case 3: 145 | *p = (unsigned char)f; 146 | *(p + 1) = (unsigned char)(f >> 8); 147 | *(p + 2) = (unsigned char)(f >> 16); 148 | return; 149 | case 2: 150 | *p = (unsigned char)f; 151 | *(p + 1) = (unsigned char)(f >> 8); 152 | return; 153 | case 1: 154 | *p = (unsigned char)f; 155 | return; 156 | } 157 | #else 158 | switch (bytes) 159 | { 160 | case 4: 161 | *((ui32*)dst) = f; 162 | return; 163 | case 3: 164 | *((ui32*)dst) = f; 165 | return; 166 | case 2: 167 | *((ui16 *)dst) = (ui16)f; 168 | return; 169 | case 1: 170 | *((unsigned char*)dst) = (unsigned char)f; 171 | return; 172 | } 173 | #endif 174 | } 175 | 176 | 177 | size_t qlz_size_decompressed(const char *source) 178 | { 179 | ui32 n, r; 180 | n = (((*source) & 2) == 2) ? 4 : 1; 181 | r = fast_read(source + 1 + n, n); 182 | r = r & (0xffffffff >> ((4 - n)*8)); 183 | return r; 184 | } 185 | 186 | size_t qlz_size_compressed(const char *source) 187 | { 188 | ui32 n, r; 189 | n = (((*source) & 2) == 2) ? 4 : 1; 190 | r = fast_read(source + 1, n); 191 | r = r & (0xffffffff >> ((4 - n)*8)); 192 | return r; 193 | } 194 | 195 | size_t qlz_size_header(const char *source) 196 | { 197 | size_t n = 2*((((*source) & 2) == 2) ? 4 : 1) + 1; 198 | return n; 199 | } 200 | 201 | 202 | static __inline void memcpy_up(unsigned char *dst, const unsigned char *src, ui32 n) 203 | { 204 | // Caution if modifying memcpy_up! Overlap of dst and src must be special handled. 205 | #ifndef X86X64 206 | unsigned char *end = dst + n; 207 | while(dst < end) 208 | { 209 | *dst = *src; 210 | dst++; 211 | src++; 212 | } 213 | #else 214 | ui32 f = 0; 215 | do 216 | { 217 | *(ui32 *)(dst + f) = *(ui32 *)(src + f); 218 | f += MINOFFSET + 1; 219 | } 220 | while (f < n); 221 | #endif 222 | } 223 | 224 | static __inline void update_hash(qlz_state_decompress *state, const unsigned char *s) 225 | { 226 | #if QLZ_COMPRESSION_LEVEL == 1 227 | ui32 hash; 228 | hash = hashat(s); 229 | state->hash[hash].offset = s; 230 | state->hash_counter[hash] = 1; 231 | #elif QLZ_COMPRESSION_LEVEL == 2 232 | ui32 hash; 233 | unsigned char c; 234 | hash = hashat(s); 235 | c = state->hash_counter[hash]; 236 | state->hash[hash].offset[c & (QLZ_POINTERS - 1)] = s; 237 | c++; 238 | state->hash_counter[hash] = c; 239 | #endif 240 | (void)state; 241 | (void)s; 242 | } 243 | 244 | #if QLZ_COMPRESSION_LEVEL <= 2 245 | static void update_hash_upto(qlz_state_decompress *state, unsigned char **lh, const unsigned char *max) 246 | { 247 | while(*lh < max) 248 | { 249 | (*lh)++; 250 | update_hash(state, *lh); 251 | } 252 | } 253 | #endif 254 | 255 | static size_t qlz_compress_core(const unsigned char *source, unsigned char *destination, size_t size, qlz_state_compress *state) 256 | { 257 | const unsigned char *last_byte = source + size - 1; 258 | const unsigned char *src = source; 259 | unsigned char *cword_ptr = destination; 260 | unsigned char *dst = destination + CWORD_LEN; 261 | ui32 cword_val = 1U << 31; 262 | const unsigned char *last_matchstart = last_byte - UNCONDITIONAL_MATCHLEN - UNCOMPRESSED_END; 263 | ui32 fetch = 0; 264 | unsigned int lits = 0; 265 | 266 | (void) lits; 267 | 268 | if(src <= last_matchstart) 269 | fetch = fast_read(src, 3); 270 | 271 | while(src <= last_matchstart) 272 | { 273 | if ((cword_val & 1) == 1) 274 | { 275 | // store uncompressed if compression ratio is too low 276 | if (src > source + (size >> 1) && dst - destination > src - source - ((src - source) >> 5)) 277 | return 0; 278 | 279 | fast_write((cword_val >> 1) | (1U << 31), cword_ptr, CWORD_LEN); 280 | 281 | cword_ptr = dst; 282 | dst += CWORD_LEN; 283 | cword_val = 1U << 31; 284 | fetch = fast_read(src, 3); 285 | } 286 | #if QLZ_COMPRESSION_LEVEL == 1 287 | { 288 | const unsigned char *o; 289 | ui32 hash, cached; 290 | 291 | hash = hash_func(fetch); 292 | cached = fetch ^ state->hash[hash].cache; 293 | state->hash[hash].cache = fetch; 294 | 295 | o = state->hash[hash].offset + OFFSET_BASE; 296 | state->hash[hash].offset = CAST(src - OFFSET_BASE); 297 | 298 | #ifdef X86X64 299 | if ((cached & 0xffffff) == 0 && o != OFFSET_BASE && (src - o > MINOFFSET || (src == o + 1 && lits >= 3 && src > source + 3 && same(src - 3, 6)))) 300 | { 301 | if(cached != 0) 302 | { 303 | #else 304 | if (cached == 0 && o != OFFSET_BASE && (src - o > MINOFFSET || (src == o + 1 && lits >= 3 && src > source + 3 && same(src - 3, 6)))) 305 | { 306 | if (*(o + 3) != *(src + 3)) 307 | { 308 | #endif 309 | hash <<= 4; 310 | cword_val = (cword_val >> 1) | (1U << 31); 311 | fast_write((3 - 2) | hash, dst, 2); 312 | src += 3; 313 | dst += 2; 314 | } 315 | else 316 | { 317 | const unsigned char *old_src = src; 318 | size_t matchlen; 319 | hash <<= 4; 320 | 321 | cword_val = (cword_val >> 1) | (1U << 31); 322 | src += 4; 323 | 324 | if(*(o + (src - old_src)) == *src) 325 | { 326 | src++; 327 | if(*(o + (src - old_src)) == *src) 328 | { 329 | size_t q = last_byte - UNCOMPRESSED_END - (src - 5) + 1; 330 | size_t remaining = q > 255 ? 255 : q; 331 | src++; 332 | while(*(o + (src - old_src)) == *src && (size_t)(src - old_src) < remaining) 333 | src++; 334 | } 335 | } 336 | 337 | matchlen = src - old_src; 338 | if (matchlen < 18) 339 | { 340 | fast_write((ui32)(matchlen - 2) | hash, dst, 2); 341 | dst += 2; 342 | } 343 | else 344 | { 345 | fast_write((ui32)(matchlen << 16) | hash, dst, 3); 346 | dst += 3; 347 | } 348 | } 349 | fetch = fast_read(src, 3); 350 | lits = 0; 351 | } 352 | else 353 | { 354 | lits++; 355 | *dst = *src; 356 | src++; 357 | dst++; 358 | cword_val = (cword_val >> 1); 359 | #ifdef X86X64 360 | fetch = fast_read(src, 3); 361 | #else 362 | fetch = (fetch >> 8 & 0xffff) | (*(src + 2) << 16); 363 | #endif 364 | } 365 | } 366 | #elif QLZ_COMPRESSION_LEVEL >= 2 367 | { 368 | const unsigned char *o, *offset2; 369 | ui32 hash, matchlen, k, m, best_k = 0; 370 | unsigned char c; 371 | size_t remaining = (last_byte - UNCOMPRESSED_END - src + 1) > 255 ? 255 : (last_byte - UNCOMPRESSED_END - src + 1); 372 | (void)best_k; 373 | 374 | 375 | //hash = hashat(src); 376 | fetch = fast_read(src, 3); 377 | hash = hash_func(fetch); 378 | 379 | c = state->hash_counter[hash]; 380 | 381 | offset2 = state->hash[hash].offset[0]; 382 | if(offset2 < src - MINOFFSET && c > 0 && ((fast_read(offset2, 3) ^ fetch) & 0xffffff) == 0) 383 | { 384 | matchlen = 3; 385 | if(*(offset2 + matchlen) == *(src + matchlen)) 386 | { 387 | matchlen = 4; 388 | while(*(offset2 + matchlen) == *(src + matchlen) && matchlen < remaining) 389 | matchlen++; 390 | } 391 | } 392 | else 393 | matchlen = 0; 394 | for(k = 1; k < QLZ_POINTERS && c > k; k++) 395 | { 396 | o = state->hash[hash].offset[k]; 397 | #if QLZ_COMPRESSION_LEVEL == 3 398 | if(((fast_read(o, 3) ^ fetch) & 0xffffff) == 0 && o < src - MINOFFSET) 399 | #elif QLZ_COMPRESSION_LEVEL == 2 400 | if(*(src + matchlen) == *(o + matchlen) && ((fast_read(o, 3) ^ fetch) & 0xffffff) == 0 && o < src - MINOFFSET) 401 | #endif 402 | { 403 | m = 3; 404 | while(*(o + m) == *(src + m) && m < remaining) 405 | m++; 406 | #if QLZ_COMPRESSION_LEVEL == 3 407 | if ((m > matchlen) || (m == matchlen && o > offset2)) 408 | #elif QLZ_COMPRESSION_LEVEL == 2 409 | if (m > matchlen) 410 | #endif 411 | { 412 | offset2 = o; 413 | matchlen = m; 414 | best_k = k; 415 | } 416 | } 417 | } 418 | o = offset2; 419 | state->hash[hash].offset[c & (QLZ_POINTERS - 1)] = src; 420 | c++; 421 | state->hash_counter[hash] = c; 422 | 423 | #if QLZ_COMPRESSION_LEVEL == 3 424 | if(matchlen > 2 && src - o < 131071) 425 | { 426 | ui32 u; 427 | size_t offset = src - o; 428 | 429 | for(u = 1; u < matchlen; u++) 430 | { 431 | hash = hashat(src + u); 432 | c = state->hash_counter[hash]++; 433 | state->hash[hash].offset[c & (QLZ_POINTERS - 1)] = src + u; 434 | } 435 | 436 | cword_val = (cword_val >> 1) | (1U << 31); 437 | src += matchlen; 438 | 439 | if(matchlen == 3 && offset <= 63) 440 | { 441 | *dst = (unsigned char)(offset << 2); 442 | dst++; 443 | } 444 | else if (matchlen == 3 && offset <= 16383) 445 | { 446 | ui32 f = (ui32)((offset << 2) | 1); 447 | fast_write(f, dst, 2); 448 | dst += 2; 449 | } 450 | else if (matchlen <= 18 && offset <= 1023) 451 | { 452 | ui32 f = ((matchlen - 3) << 2) | ((ui32)offset << 6) | 2; 453 | fast_write(f, dst, 2); 454 | dst += 2; 455 | } 456 | 457 | else if(matchlen <= 33) 458 | { 459 | ui32 f = ((matchlen - 2) << 2) | ((ui32)offset << 7) | 3; 460 | fast_write(f, dst, 3); 461 | dst += 3; 462 | } 463 | else 464 | { 465 | ui32 f = ((matchlen - 3) << 7) | ((ui32)offset << 15) | 3; 466 | fast_write(f, dst, 4); 467 | dst += 4; 468 | } 469 | } 470 | else 471 | { 472 | *dst = *src; 473 | src++; 474 | dst++; 475 | cword_val = (cword_val >> 1); 476 | } 477 | #elif QLZ_COMPRESSION_LEVEL == 2 478 | 479 | if(matchlen > 2) 480 | { 481 | cword_val = (cword_val >> 1) | (1U << 31); 482 | src += matchlen; 483 | 484 | if (matchlen < 10) 485 | { 486 | ui32 f = best_k | ((matchlen - 2) << 2) | (hash << 5); 487 | fast_write(f, dst, 2); 488 | dst += 2; 489 | } 490 | else 491 | { 492 | ui32 f = best_k | (matchlen << 16) | (hash << 5); 493 | fast_write(f, dst, 3); 494 | dst += 3; 495 | } 496 | } 497 | else 498 | { 499 | *dst = *src; 500 | src++; 501 | dst++; 502 | cword_val = (cword_val >> 1); 503 | } 504 | #endif 505 | } 506 | #endif 507 | } 508 | while (src <= last_byte) 509 | { 510 | if ((cword_val & 1) == 1) 511 | { 512 | fast_write((cword_val >> 1) | (1U << 31), cword_ptr, CWORD_LEN); 513 | cword_ptr = dst; 514 | dst += CWORD_LEN; 515 | cword_val = 1U << 31; 516 | } 517 | #if QLZ_COMPRESSION_LEVEL < 3 518 | if (src <= last_byte - 3) 519 | { 520 | #if QLZ_COMPRESSION_LEVEL == 1 521 | ui32 hash, fetch; 522 | fetch = fast_read(src, 3); 523 | hash = hash_func(fetch); 524 | state->hash[hash].offset = CAST(src - OFFSET_BASE); 525 | state->hash[hash].cache = fetch; 526 | #elif QLZ_COMPRESSION_LEVEL == 2 527 | ui32 hash; 528 | unsigned char c; 529 | hash = hashat(src); 530 | c = state->hash_counter[hash]; 531 | state->hash[hash].offset[c & (QLZ_POINTERS - 1)] = src; 532 | c++; 533 | state->hash_counter[hash] = c; 534 | #endif 535 | } 536 | #endif 537 | *dst = *src; 538 | src++; 539 | dst++; 540 | cword_val = (cword_val >> 1); 541 | } 542 | 543 | while((cword_val & 1) != 1) 544 | cword_val = (cword_val >> 1); 545 | 546 | fast_write((cword_val >> 1) | (1U << 31), cword_ptr, CWORD_LEN); 547 | 548 | // min. size must be 9 bytes so that the qlz_size functions can take 9 bytes as argument 549 | return dst - destination < 9 ? 9 : dst - destination; 550 | } 551 | 552 | static size_t qlz_decompress_core(const unsigned char *source, unsigned char *destination, size_t size, qlz_state_decompress *state, const unsigned char *history) 553 | { 554 | const unsigned char *src = source + qlz_size_header((const char *)source); 555 | unsigned char *dst = destination; 556 | const unsigned char *last_destination_byte = destination + size - 1; 557 | ui32 cword_val = 1; 558 | const unsigned char *last_matchstart = last_destination_byte - UNCONDITIONAL_MATCHLEN - UNCOMPRESSED_END; 559 | unsigned char *last_hashed = destination - 1; 560 | const unsigned char *last_source_byte = source + qlz_size_compressed((const char *)source) - 1; 561 | static const ui32 bitlut[16] = {4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0}; 562 | 563 | (void) last_source_byte; 564 | (void) last_hashed; 565 | (void) state; 566 | (void) history; 567 | 568 | for(;;) 569 | { 570 | ui32 fetch; 571 | 572 | if (cword_val == 1) 573 | { 574 | #ifdef QLZ_MEMORY_SAFE 575 | if(src + CWORD_LEN - 1 > last_source_byte) 576 | return 0; 577 | #endif 578 | cword_val = fast_read(src, CWORD_LEN); 579 | src += CWORD_LEN; 580 | } 581 | 582 | #ifdef QLZ_MEMORY_SAFE 583 | if(src + 4 - 1 > last_source_byte) 584 | return 0; 585 | #endif 586 | 587 | fetch = fast_read(src, 4); 588 | 589 | if ((cword_val & 1) == 1) 590 | { 591 | ui32 matchlen; 592 | const unsigned char *offset2; 593 | 594 | #if QLZ_COMPRESSION_LEVEL == 1 595 | ui32 hash; 596 | cword_val = cword_val >> 1; 597 | hash = (fetch >> 4) & 0xfff; 598 | offset2 = (const unsigned char *)(size_t)state->hash[hash].offset; 599 | 600 | if((fetch & 0xf) != 0) 601 | { 602 | matchlen = (fetch & 0xf) + 2; 603 | src += 2; 604 | } 605 | else 606 | { 607 | matchlen = *(src + 2); 608 | src += 3; 609 | } 610 | 611 | #elif QLZ_COMPRESSION_LEVEL == 2 612 | ui32 hash; 613 | unsigned char c; 614 | cword_val = cword_val >> 1; 615 | hash = (fetch >> 5) & 0x7ff; 616 | c = (unsigned char)(fetch & 0x3); 617 | offset2 = state->hash[hash].offset[c]; 618 | 619 | if((fetch & (28)) != 0) 620 | { 621 | matchlen = ((fetch >> 2) & 0x7) + 2; 622 | src += 2; 623 | } 624 | else 625 | { 626 | matchlen = *(src + 2); 627 | src += 3; 628 | } 629 | 630 | #elif QLZ_COMPRESSION_LEVEL == 3 631 | ui32 offset; 632 | cword_val = cword_val >> 1; 633 | if ((fetch & 3) == 0) 634 | { 635 | offset = (fetch & 0xff) >> 2; 636 | matchlen = 3; 637 | src++; 638 | } 639 | else if ((fetch & 2) == 0) 640 | { 641 | offset = (fetch & 0xffff) >> 2; 642 | matchlen = 3; 643 | src += 2; 644 | } 645 | else if ((fetch & 1) == 0) 646 | { 647 | offset = (fetch & 0xffff) >> 6; 648 | matchlen = ((fetch >> 2) & 15) + 3; 649 | src += 2; 650 | } 651 | else if ((fetch & 127) != 3) 652 | { 653 | offset = (fetch >> 7) & 0x1ffff; 654 | matchlen = ((fetch >> 2) & 0x1f) + 2; 655 | src += 3; 656 | } 657 | else 658 | { 659 | offset = (fetch >> 15); 660 | matchlen = ((fetch >> 7) & 255) + 3; 661 | src += 4; 662 | } 663 | 664 | offset2 = dst - offset; 665 | #endif 666 | 667 | #ifdef QLZ_MEMORY_SAFE 668 | if(offset2 < history || offset2 > dst - MINOFFSET - 1) 669 | return 0; 670 | 671 | if(matchlen > (ui32)(last_destination_byte - dst - UNCOMPRESSED_END + 1)) 672 | return 0; 673 | #endif 674 | 675 | memcpy_up(dst, offset2, matchlen); 676 | dst += matchlen; 677 | 678 | #if QLZ_COMPRESSION_LEVEL <= 2 679 | update_hash_upto(state, &last_hashed, dst - matchlen); 680 | last_hashed = dst - 1; 681 | #endif 682 | } 683 | else 684 | { 685 | if (dst < last_matchstart) 686 | { 687 | unsigned int n = bitlut[cword_val & 0xf]; 688 | #ifdef X86X64 689 | *(ui32 *)dst = *(ui32 *)src; 690 | #else 691 | memcpy_up(dst, src, 4); 692 | #endif 693 | cword_val = cword_val >> n; 694 | dst += n; 695 | src += n; 696 | #if QLZ_COMPRESSION_LEVEL <= 2 697 | update_hash_upto(state, &last_hashed, dst - 3); 698 | #endif 699 | } 700 | else 701 | { 702 | while(dst <= last_destination_byte) 703 | { 704 | if (cword_val == 1) 705 | { 706 | src += CWORD_LEN; 707 | cword_val = 1U << 31; 708 | } 709 | #ifdef QLZ_MEMORY_SAFE 710 | if(src >= last_source_byte + 1) 711 | return 0; 712 | #endif 713 | *dst = *src; 714 | dst++; 715 | src++; 716 | cword_val = cword_val >> 1; 717 | } 718 | 719 | #if QLZ_COMPRESSION_LEVEL <= 2 720 | update_hash_upto(state, &last_hashed, last_destination_byte - 3); // todo, use constant 721 | #endif 722 | return size; 723 | } 724 | 725 | } 726 | } 727 | } 728 | 729 | size_t qlz_compress(const void *source, char *destination, size_t size, qlz_state_compress *state) 730 | { 731 | size_t r; 732 | ui32 compressed; 733 | size_t base; 734 | 735 | if(size == 0 || size > 0xffffffff - 400) 736 | return 0; 737 | 738 | if(size < 216) 739 | base = 3; 740 | else 741 | base = 9; 742 | 743 | #if QLZ_STREAMING_BUFFER > 0 744 | if (state->stream_counter + size - 1 >= QLZ_STREAMING_BUFFER) 745 | #endif 746 | { 747 | reset_table_compress(state); 748 | r = base + qlz_compress_core((const unsigned char *)source, (unsigned char*)destination + base, size, state); 749 | #if QLZ_STREAMING_BUFFER > 0 750 | reset_table_compress(state); 751 | #endif 752 | if(r == base) 753 | { 754 | memcpy(destination + base, source, size); 755 | r = size + base; 756 | compressed = 0; 757 | } 758 | else 759 | { 760 | compressed = 1; 761 | } 762 | state->stream_counter = 0; 763 | } 764 | #if QLZ_STREAMING_BUFFER > 0 765 | else 766 | { 767 | unsigned char *src = state->stream_buffer + state->stream_counter; 768 | 769 | memcpy(src, source, size); 770 | r = base + qlz_compress_core(src, (unsigned char*)destination + base, size, state); 771 | 772 | if(r == base) 773 | { 774 | memcpy(destination + base, src, size); 775 | r = size + base; 776 | compressed = 0; 777 | reset_table_compress(state); 778 | } 779 | else 780 | { 781 | compressed = 1; 782 | } 783 | state->stream_counter += size; 784 | } 785 | #endif 786 | if(base == 3) 787 | { 788 | *destination = (unsigned char)(0 | compressed); 789 | *(destination + 1) = (unsigned char)r; 790 | *(destination + 2) = (unsigned char)size; 791 | } 792 | else 793 | { 794 | *destination = (unsigned char)(2 | compressed); 795 | fast_write((ui32)r, destination + 1, 4); 796 | fast_write((ui32)size, destination + 5, 4); 797 | } 798 | 799 | *destination |= (QLZ_COMPRESSION_LEVEL << 2); 800 | *destination |= (1 << 6); 801 | *destination |= ((QLZ_STREAMING_BUFFER == 0 ? 0 : (QLZ_STREAMING_BUFFER == 100000 ? 1 : (QLZ_STREAMING_BUFFER == 1000000 ? 2 : 3))) << 4); 802 | 803 | // 76543210 804 | // 01SSLLHC 805 | 806 | return r; 807 | } 808 | 809 | size_t qlz_decompress(const char *source, void *destination, qlz_state_decompress *state) 810 | { 811 | size_t dsiz = qlz_size_decompressed(source); 812 | 813 | #if QLZ_STREAMING_BUFFER > 0 814 | if (state->stream_counter + qlz_size_decompressed(source) - 1 >= QLZ_STREAMING_BUFFER) 815 | #endif 816 | { 817 | if((*source & 1) == 1) 818 | { 819 | reset_table_decompress(state); 820 | dsiz = qlz_decompress_core((const unsigned char *)source, (unsigned char *)destination, dsiz, state, (const unsigned char *)destination); 821 | } 822 | else 823 | { 824 | memcpy(destination, source + qlz_size_header(source), dsiz); 825 | } 826 | state->stream_counter = 0; 827 | reset_table_decompress(state); 828 | } 829 | #if QLZ_STREAMING_BUFFER > 0 830 | else 831 | { 832 | unsigned char *dst = state->stream_buffer + state->stream_counter; 833 | if((*source & 1) == 1) 834 | { 835 | dsiz = qlz_decompress_core((const unsigned char *)source, dst, dsiz, state, (const unsigned char *)state->stream_buffer); 836 | } 837 | else 838 | { 839 | memcpy(dst, source + qlz_size_header(source), dsiz); 840 | reset_table_decompress(state); 841 | } 842 | memcpy(destination, dst, dsiz); 843 | state->stream_counter += dsiz; 844 | } 845 | #endif 846 | return dsiz; 847 | } 848 | 849 | --------------------------------------------------------------------------------