Document of c-modernization-kit (porter) 1.0.0
Loading...
Searching...
No Matches
compress_windows.c
Go to the documentation of this file.
1
23
24#ifdef _WIN32
25
26#include <string.h>
27
28#include <winsock2.h>
29#include <windows.h>
30#include <compressapi.h>
31
32#include "compress.h"
33
34/* doxygen コメントはヘッダに記載 */
35int potr_compress(uint8_t *dst,
36 size_t *dst_len,
37 const uint8_t *src,
38 size_t src_len)
39{
40 COMPRESSOR_HANDLE h;
41 uint32_t orig_len_nbo;
42 SIZE_T cmp_len;
43 ULONG block_size;
44 BOOL ok;
45
46 if (dst == NULL || dst_len == NULL || src == NULL || src_len == 0)
47 {
48 return -1;
49 }
50
51 if (*dst_len < POTR_COMPRESS_HEADER_SIZE + 1U)
52 {
53 return -1;
54 }
55
56 /* 先頭 4 バイトに元サイズ (NBO) を書く */
57 orig_len_nbo = htonl((uint32_t)src_len);
58 memcpy(dst, &orig_len_nbo, POTR_COMPRESS_HEADER_SIZE);
59
60 /* MSZIP Block Mode (COMPRESS_RAW) で raw DEFLATE を生成 */
61 if (!CreateCompressor(COMPRESS_ALGORITHM_MSZIP | COMPRESS_RAW, NULL, &h))
62 {
63 return -1;
64 }
65
66 /* ブロックサイズをデータサイズ以上に設定して単一ブロックとして処理する */
67 if (src_len > 32768U)
68 {
69 block_size = (ULONG)src_len;
70 }
71 else
72 {
73 block_size = 32768U;
74 }
75 (void)SetCompressorInformation(h,
76 COMPRESS_INFORMATION_CLASS_BLOCK_SIZE,
77 &block_size,
78 sizeof(block_size));
79
80 ok = Compress(h,
81 src,
82 (SIZE_T)src_len,
84 (SIZE_T)(*dst_len - POTR_COMPRESS_HEADER_SIZE),
85 &cmp_len);
86 CloseCompressor(h);
87
88 if (!ok)
89 {
90 return -1;
91 }
92
93 *dst_len = POTR_COMPRESS_HEADER_SIZE + (size_t)cmp_len;
94 return 0;
95}
96
97/* doxygen コメントはヘッダに記載 */
98int potr_decompress(uint8_t *dst,
99 size_t *dst_len,
100 const uint8_t *src,
101 size_t src_len)
102{
103 DECOMPRESSOR_HANDLE h;
104 uint32_t orig_len_nbo;
105 uint32_t orig_len;
106 SIZE_T out_len;
107 ULONG block_size;
108 BOOL ok;
109
110 if (dst == NULL || dst_len == NULL || src == NULL
111 || src_len <= POTR_COMPRESS_HEADER_SIZE)
112 {
113 return -1;
114 }
115
116 /* 先頭 4 バイトから元サイズを取得 */
117 memcpy(&orig_len_nbo, src, POTR_COMPRESS_HEADER_SIZE);
118 orig_len = ntohl(orig_len_nbo);
119
120 if (*dst_len < (size_t)orig_len)
121 {
122 return -1;
123 }
124
125 /* MSZIP Block Mode (COMPRESS_RAW) で解凍 */
126 if (!CreateDecompressor(COMPRESS_ALGORITHM_MSZIP | COMPRESS_RAW, NULL, &h))
127 {
128 return -1;
129 }
130
131 /* 圧縮時に設定したブロックサイズと揃える */
132 if (orig_len > 32768U)
133 {
134 block_size = orig_len;
135 }
136 else
137 {
138 block_size = 32768U;
139 }
140 (void)SetDecompressorInformation(h,
141 COMPRESS_INFORMATION_CLASS_BLOCK_SIZE,
142 &block_size,
143 sizeof(block_size));
144
145 ok = Decompress(h,
147 (SIZE_T)(src_len - POTR_COMPRESS_HEADER_SIZE),
148 dst,
149 (SIZE_T)*dst_len,
150 &out_len);
151 CloseDecompressor(h);
152
153 if (!ok)
154 {
155 return -1;
156 }
157
158 *dst_len = (size_t)out_len;
159 return 0;
160}
161
162#endif /* _WIN32 */
データ圧縮・解凍モジュールの内部ヘッダー。
#define POTR_COMPRESS_HEADER_SIZE
圧縮ペイロード先頭に付加する元サイズフィールドのバイト数。
Definition compress.h:39
int potr_decompress(uint8_t *dst, size_t *dst_len, const uint8_t *src, size_t src_len)
圧縮データを解凍します。
int potr_compress(uint8_t *dst, size_t *dst_len, const uint8_t *src, size_t src_len)
データを圧縮します。