blob: db67e4d20b541f06e04954d8c2a42e920de644f3 [file] [log] [blame]
yuezonghe824eb0c2024-06-27 02:32:26 -07001/*
2 * JFFS2 -- Journalling Flash File System, Version 2.
3 *
4 * For licensing information, see the file 'LICENCE' in this directory.
5 *
6 * JFFS2 wrapper to the LZMA C SDK
7 *
8 */
9
10#include <linux/lzma.h>
11//#include "compr.h"
12
13#ifdef __KERNEL__
14 //static DEFINE_MUTEX(deflate_mutex);
15#endif
16
17CLzmaEncHandle *p;
18Byte propsEncoded[LZMA_PROPS_SIZE];
19SizeT propsSize = sizeof(propsEncoded);
20
21STATIC void lzma_free_workspace(void)
22{
23 LzmaEnc_Destroy(p, &lzma_alloc, &lzma_alloc);
24}
25
26STATIC int lzma_alloc_workspace(CLzmaEncProps *props)
27{
28 if ((p = (CLzmaEncHandle *)LzmaEnc_Create(&lzma_alloc)) == NULL)
29 {
30 PRINT_ERROR("Failed to allocate lzma deflate workspace\n");
31 return -ENOMEM;
32 }
33
34 if (LzmaEnc_SetProps(p, props) != SZ_OK)
35 {
36 lzma_free_workspace();
37 return -1;
38 }
39
40 if (LzmaEnc_WriteProperties(p, propsEncoded, &propsSize) != SZ_OK)
41 {
42 lzma_free_workspace();
43 return -1;
44 }
45
46 return 0;
47}
48
49STATIC int jffs2_lzma_compress(unsigned char *data_in, unsigned char *cpage_out,
50 uint32_t *sourcelen, uint32_t *dstlen)
51{
52 SizeT compress_size = (SizeT)(*dstlen);
53 int ret;
54
55 #ifdef __KERNEL__
56 //mutex_lock(&deflate_mutex);
57 #endif
58
59 ret = LzmaEnc_MemEncode(p, cpage_out, &compress_size, data_in, *sourcelen,
60 0, NULL, &lzma_alloc, &lzma_alloc);
61
62 #ifdef __KERNEL__
63 //mutex_unlock(&deflate_mutex);
64 #endif
65
66 if (ret != SZ_OK)
67 return -1;
68
69 *dstlen = (uint32_t)compress_size;
70
71 return 0;
72}
73
74int jffs2_lzma_decompress(unsigned char *data_in, unsigned char *cpage_out,
75 uint32_t srclen, uint32_t destlen)
76{
77 int ret;
78 SizeT dl = (SizeT)destlen;
79 SizeT sl = (SizeT)srclen;
80 ELzmaStatus status;
81
82 ret = LzmaDecode(cpage_out, &dl, data_in, &sl, propsEncoded,
83 propsSize, LZMA_FINISH_ANY, &status, &lzma_alloc);
84
85 if (ret != SZ_OK || status == LZMA_STATUS_NOT_FINISHED || dl != (SizeT)destlen)
86 return -1;
87
88 return 0;
89}
90#if 0
91static struct jffs2_compressor jffs2_lzma_comp = {
92 .priority = JFFS2_LZMA_PRIORITY,
93 .name = "lzma",
94 .compr = JFFS2_COMPR_LZMA,
95 .compress = &jffs2_lzma_compress,
96 .decompress = &jffs2_lzma_decompress,
97 .disabled = 0,
98};
99#endif
100int jffs2_lzma_init(void)
101{
102 int ret;
103 CLzmaEncProps props;
104 LzmaEncProps_Init(&props);
105
106 props.dictSize = LZMA_BEST_DICT(0x2000);
107 props.level = LZMA_BEST_LEVEL;
108 props.lc = LZMA_BEST_LC;
109 props.lp = LZMA_BEST_LP;
110 props.pb = LZMA_BEST_PB;
111 props.fb = LZMA_BEST_FB;
112
113 ret = lzma_alloc_workspace(&props);
114 if (ret < 0)
115 return ret;
116
117 //ret = jffs2_register_compressor(&jffs2_lzma_comp);
118 if (ret)
119 lzma_free_workspace();
120
121 return ret;
122}
123
124void jffs2_lzma_exit(void)
125{
126 //jffs2_unregister_compressor(&jffs2_lzma_comp);
127 lzma_free_workspace();
128}