{"id":4073,"date":"2012-06-19T18:21:23","date_gmt":"2012-06-19T10:21:23","guid":{"rendered":"http:\/\/edsionte.com\/techblog\/?p=4073"},"modified":"2012-07-26T10:47:20","modified_gmt":"2012-07-26T02:47:20","slug":"minilzo%e7%9a%84%e7%ae%80%e5%8d%95%e4%bd%bf%e7%94%a8","status":"publish","type":"post","link":"http:\/\/edsionte.com\/techblog\/archives\/4073","title":{"rendered":"miniLZO\u7684\u57fa\u672c\u4f7f\u7528"},"content":{"rendered":"<p>miniLZO\u662f\u4e00\u79cd\u8f7b\u91cf\u7ea7\u7684\u538b\u7f29\u548c\u89e3\u538b\u7f29\u5e93\uff0c\u5b83\u662f\u57fa\u4e8eLZO\u538b\u7f29\u548c\u89e3\u538b\u7f29\u7b97\u6cd5\u5b9e\u73b0\u7684\u3002LZO\u867d\u7136\u529f\u80fd\u5f3a\u5927\uff0c\u4f46\u662f\u7f16\u8bd1\u540e\u7684\u5e93\u6587\u4ef6\u8f83\u5927\uff0c\u800cminilzo\u7f16\u8bd1\u540e\u7684\u5e93\u5219\u5c0f\u4e8e5kb\uff0c\u56e0\u6b64miniLZO\u4e3a\u90a3\u4e9b\u4ec5\u9700\u8981\u7b80\u5355\u538b\u7f29\u548c\u89e3\u538b\u7f29\u529f\u80fd\u7684\u7a0b\u5e8f\u800c\u8bbe\u8ba1\u3002<\/p>\n<p>\u5b9e\u73b0miniLZO\u7684\u6e90\u7801\u5f88\u7b80\u5355\uff0c\u5305\u542b\u5982\u4e0b\u6587\u4ef6\uff1a<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\nedsionte@virtualPC ~\/minilzo-2.06 $ tree\r\n.\r\n\u251c\u2500\u2500 COPYING\r\n\u251c\u2500\u2500 lzoconf.h\r\n\u251c\u2500\u2500 lzodefs.h\r\n\u251c\u2500\u2500 Makefile\r\n\u251c\u2500\u2500 minilzo.c\r\n\u251c\u2500\u2500 minilzo.h\r\n\u251c\u2500\u2500 README.LZO\r\n\u2514\u2500\u2500 testmini.c\r\n<\/pre>\n<p>\u5728\u7a0b\u5e8f\u4e2d\u52a0\u5165miniLZO\u538b\u7f29\u548c\u89e3\u538b\u7f29\u529f\u80fd\u7684\u65b9\u6cd5\u5f88\u7b80\u5355\uff0c\u53ea\u9700\u5c06\u6e90\u6587\u4ef6minilzo.c\u548c\u5934\u6587\u4ef6lzoconf.h\uff0clzodefs.h\u548cminilzo.h\u62f7\u8d1d\u5230\u6e90\u6587\u4ef6\u76ee\u5f55\u4e0b\uff0c\u5728\u6e90\u4ee3\u7801\u4e2d\u52a0\u5165\u5934\u6587\u4ef6minilzo.h\uff0c\u5e76\u4e14\u5728\u7f16\u8bd1\u65f6\u52a0\u5165miniloz.c\u3002<\/p>\n<p>\u5982\u4f55\u5728\u6e90\u7a0b\u5e8f\u4e2d\u5177\u4f53\u4f7f\u7528miniLZO\u7684\u538b\u7f29\u548c\u89e3\u538b\u7f29\u529f\u80fd\uff1f\u4e0b\u9762\u5c06\u7ed9\u51fa\u4e00\u4e2a\u7b80\u5355\u7684\u5b9e\u4f8b\u7a0b\u5e8f\u6765\u8bf4\u660e\u4f7f\u7528\u65b9\u6cd5\u3002\u8be5\u7a0b\u5e8f\u6240\u6f14\u793a\u7684\u529f\u80fd\u662f\uff1a\u9996\u5148\u5229\u7528minilzo\u63d0\u4f9b\u7684\u538b\u7f29API\u5c06\u6307\u5b9a\u6587\u4ef6\u7684\u524d1024\u5b57\u8282\u8fdb\u884c\u538b\u7f29\uff0c\u5e76\u5c06\u538b\u7f29\u540e\u7684\u5185\u5bb9\u4fdd\u5b58\u5728cfile\u6587\u4ef6\u4e2d\uff1b\u7136\u540e\u5c06\u8be5\u538b\u7f29\u6587\u4ef6\u7684\u5185\u5bb9\u518d\u8fdb\u884c\u89e3\u538b\u7f29\uff0c\u5e76\u5b58\u5165\u53e6\u4e00\u4e2a\u6587\u4ef6dcfile\u4e2d\u3002<\/p>\n<p>\u6b63\u5982\u4e0a\u9762\u6240\u63cf\u8ff0\u7684\u90a3\u6837\uff0c\u8981\u4f7f\u7528miniLZO\uff0c\u5fc5\u987b\u52a0\u5165\u5934\u6587\u4ef6minilzo.h\u3002<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n#include &amp;quot;minilzo.h&amp;quot;\r\n\r\n#define MAX_LEN 1024\r\n\r\n#define HEAP_ALLOC(var,size) \\\r\n    lzo_align_t __LZO_MMODEL var &#x5B; ((size) + (sizeof(lzo_align_t) - 1)) \/ sizeof(lzo_align_t) ]\r\n\r\n\/\/the wkmem is used for compressed\r\nstatic HEAP_ALLOC(wkmem, LZO1X_1_MEM_COMPRESS);\r\n<\/pre>\n<p>\u9664\u6b64\u4e4b\u5916\uff0c\u5b9a\u4e49\u4e00\u4e2a\u5b8fHEAP_ALLOC\uff0c\u5b83\u4e3a\u538b\u7f29\u63d0\u4f9b\u4e00\u4e2a\u5185\u5b58\u7a7a\u95f4\u3002\u8981\u4f7f\u7528\u538b\u7f29\u6216\u89e3\u538b\u7f29\u529f\u80fd\uff0c\u5fc5\u987b\u5148\u901a\u8fc7lzo_init()\u5bf9miniLZO\u5bf9\u5e94\u7684\u5e93\u8fdb\u884c\u521d\u59cb\u5316\u3002<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\nint main()\r\n{\r\n\tint fd;\r\n\tchar *srcbuffer = NULL, *destbuffer = NULL;\r\n\tlzo_uint inlen = MAX_LEN;\r\n\tlzo_uint outlen = 0;\r\n\tlzo_uint newlen = 0;\r\n\r\n\t\/\/init the lzo lib\r\n\tif (lzo_init() != LZO_E_OK) {\r\n\t\tprintf(&amp;quot;lzo_init error.\\n&amp;quot;);\r\n\t\treturn -1;\r\n\t}\r\n<\/pre>\n<p>\u6253\u5f00\u8981\u538b\u7f29\u7684\u6587\u4ef6\uff0c\u5c06\u6570\u636e\u8bfb\u5165\u5185\u5b58\u7684\u7f13\u51b2\u533a\u4e2d\u3002<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n\tif ((fd = open(&amp;quot;.\/minilzo.c&amp;quot;, O_RDONLY)) &amp;lt; 0) {\r\n\t\tprinterror(__LINE__, errno, &amp;quot;open&amp;quot;);\r\n\t}\r\n\r\n\t\/\/alloc the src buffer and dest buffer\r\n\tif ((srcbuffer = malloc(MAX_LEN)) &amp;lt; 0) {\r\n\t\tprinterror(__LINE__, errno, &amp;quot;malloc&amp;quot;);\r\n\t}\r\n\tif ((destbuffer = malloc(MAX_LEN)) &amp;lt; 0) {\r\n\t\tprinterror(__LINE__, errno, &amp;quot;malloc&amp;quot;);\r\n\t}\r\n\tmemset(srcbuffer, 0, MAX_LEN);\r\n\tmemset(destbuffer, 0, MAX_LEN);\r\n\r\n\tif (read(fd, srcbuffer, MAX_LEN) &amp;lt; 0) {\r\n\t\tprinterror(__LINE__, errno, &amp;quot;read&amp;quot;);\r\n\t}\r\n<\/pre>\n<p>\u5229\u7528\u538b\u7f29\u63a5\u53e3lzo1x_1_compress()\u5c06srcbuffer\u4e2d\u957f\u5ea6\u4e3ainlen\u7684\u6570\u636e\u8fdb\u884c\u538b\u7f29\uff0c\u5e76\u5c06\u538b\u7f29\u540e\u7684\u6570\u636e\u4fdd\u5b58\u5728destbuffer\u4e2d\uff0c\u5176\u538b\u7f29\u6570\u636e\u7684\u957f\u5ea6\u8fd4\u56de\u5230\u503c\u7ed3\u679c\u53c2\u6570outlen\u4e2d\u3002\u5176\u4e2dwkmem\u4e3a\u538b\u7f29\u6570\u636e\u65f6\u6240\u9700\u8981\u7684\u5185\u5b58\u7a7a\u95f4\u3002<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n\tint ret;\r\n\tif ((ret = lzo1x_1_compress(srcbuffer, inlen, destbuffer, &amp;amp;outlen, wkmem)) != LZO_E_OK) {\r\n\t\tprintf(&amp;quot;compress error:%d.\\n&amp;quot;, ret);\r\n\t\treturn -1;\r\n\t} else {\r\n\t\tprintf(&amp;quot;compress %lu bytes into %lu bytes.\\n&amp;quot;, (unsigned long)inlen, (unsigned long)outlen);\r\n\t}\r\n\r\n\tif (outlen &amp;gt;= inlen) {\r\n\t\tprintf(&amp;quot;This block contains incompressible data.\\n&amp;quot;);\r\n\t\treturn 0;\r\n\t}\r\n<\/pre>\n<p>\u4e0b\u9762\u5c06\u538b\u7f29\u540e\u7684\u6570\u636e\u4fdd\u5b58\u5230\u65b0\u6587\u4ef6cfile\u4e2d\u3002<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n\tint cfd;\r\n\tif ((cfd = open(&amp;quot;.\/cfile&amp;quot;, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR)) &amp;lt; 0) {\r\n\t\tprinterror(__LINE__, errno, &amp;quot;open&amp;quot;);\r\n\t}\r\n\r\n\tif (write(cfd, destbuffer, outlen) &amp;lt; 0) {\r\n\t\tprinterror(__LINE__, errno, &amp;quot;write&amp;quot;);\r\n\t}\r\n<\/pre>\n<p>\u63a5\u7740\u8fdb\u884c\u89e3\u538b\u7f29\u8fc7\u7a0b\uff0c\u5229\u7528lzo1x_decompress()\u5219\u53ef\u4ee5\u5b8c\u6210\u3002\u5404\u4e2a\u53c2\u6570\u7684\u610f\u4e49\u4e0e\u538b\u7f29\u65f6\u76f8\u540c\uff0c\u53ea\u4e0d\u8fc7\u8981\u8c03\u6574\u6e90\u6570\u636e\u548c\u76ee\u7684\u6570\u636e\u4e4b\u95f4\u7684\u65b9\u5411\u3002<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n\tmemset(srcbuffer, 0, MAX_LEN);\r\n\tif ((ret = lzo1x_decompress(destbuffer, outlen, srcbuffer, &amp;amp;newlen, NULL)) != LZO_E_OK) {\r\n\t\tprintf(&amp;quot;decompress error:%d.\\n&amp;quot;, ret);\r\n\t\treturn -1;\r\n\t} else if ( inlen == newlen) {\r\n\t\tprintf(&amp;quot;decompress %lu bytes into %lu bytes.\\n&amp;quot;, (unsigned long)outlen, (unsigned long)newlen);\r\n\t}\r\n<\/pre>\n<p>\u6700\u540e\u5c06\u89e3\u538b\u7f29\u7684\u6570\u636e\u5199\u5165\u65b0\u6587\u4ef6\u5bf9\u6b64dcfile\u4e2d\u3002<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n\tint dcfd;\r\n\tif ((dcfd = open(&amp;quot;.\/dcfile&amp;quot;, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR)) &amp;lt; 0) {\r\n\t\tprinterror(__LINE__, errno, &amp;quot;open&amp;quot;);\r\n\t}\r\n\r\n\tif (write(dcfd, srcbuffer, newlen) &amp;lt; 0) {\r\n\t\tprinterror(__LINE__, errno, &amp;quot;write&amp;quot;);\r\n\t}\r\n\r\n\tclose(fd);\r\n\tclose(cfd);\r\n\tclose(dcfd);\r\n\tprintf(&amp;quot;compress success!\\n&amp;quot;);\r\n\treturn 0;\r\n}\r\n<\/pre>\n<p>\u4e0a\u8ff0\u4e3a\u6e90\u4ee3\u7801\uff0c\u5982\u679c\u4f7f\u7528make\u7f16\u8bd1\u6587\u4ef6\uff0c\u5219Makefile\u4e3a\uff1a<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\nedsionte@virtualPC ~\/edsionte-code\/lzo $ cat Makefile\r\ntarget:=testlzo\r\nsource:=testlzo.c minilzo.c\r\n\r\ndefault:\r\n\tgcc -o $(target) $(source)\r\nclean:\r\n\trm $(target)\r\n<\/pre>\n<p>\u6267\u884c\u540e\uff0c\u53ef\u67e5\u770b\u4e24\u4e2a\u6587\u4ef6\u7684\u5927\u5c0f\u5dee\u5f02\uff1a<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\nedsionte@virtualPC ~\/edsionte-code\/lzo $ ls -l cfile dcfile\r\n-rw------- 1 edsionte edsionte  265  6\u6708 19 10:23 cfile\r\n-rw------- 1 edsionte edsionte 1024  6\u6708 19 10:23 dcfile\r\n<\/pre>\n<p>\u53ef\u4ee5\u770b\u51faminilzo\u7684\u538b\u7f29\u7387\u5927\u7ea6\u4e3a1\uff1a4\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>miniLZO\u662f\u4e00\u79cd\u8f7b\u91cf\u7ea7\u7684\u538b\u7f29\u548c\u89e3\u538b\u7f29\u5e93\uff0c\u5b83\u662f\u57fa\u4e8eLZO\u538b\u7f29\u548c\u89e3\u538b\u7f29\u7b97\u6cd5\u5b9e\u73b0\u7684\u3002LZO\u867d\u7136\u529f\u80fd\u5f3a\u5927\uff0c\u4f46\u662f\u7f16\u8bd1 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[346,345,348,347],"class_list":["post-4073","post","type-post","status-publish","format-standard","hentry","category-linuxc","tag-minilzo","tag-345","tag-348","tag-347"],"views":24806,"_links":{"self":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/posts\/4073","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/comments?post=4073"}],"version-history":[{"count":0,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/posts\/4073\/revisions"}],"wp:attachment":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/media?parent=4073"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/categories?post=4073"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/tags?post=4073"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}