1647c734fSRaphael Moreira Zinsly // SPDX-License-Identifier: GPL-2.0-or-later
2647c734fSRaphael Moreira Zinsly
3647c734fSRaphael Moreira Zinsly /* P9 gzip sample code for demonstrating the P9 NX hardware interface.
4647c734fSRaphael Moreira Zinsly * Not intended for productive uses or for performance or compression
5647c734fSRaphael Moreira Zinsly * ratio measurements. For simplicity of demonstration, this sample
6647c734fSRaphael Moreira Zinsly * code compresses in to fixed Huffman blocks only (Deflate btype=1)
7647c734fSRaphael Moreira Zinsly * and has very simple memory management. Dynamic Huffman blocks
8647c734fSRaphael Moreira Zinsly * (Deflate btype=2) are more involved as detailed in the user guide.
9647c734fSRaphael Moreira Zinsly * Note also that /dev/crypto/gzip, VAS and skiboot support are
10647c734fSRaphael Moreira Zinsly * required.
11647c734fSRaphael Moreira Zinsly *
12647c734fSRaphael Moreira Zinsly * Copyright 2020 IBM Corp.
13647c734fSRaphael Moreira Zinsly *
14647c734fSRaphael Moreira Zinsly * https://github.com/libnxz/power-gzip for zlib api and other utils
15647c734fSRaphael Moreira Zinsly *
16647c734fSRaphael Moreira Zinsly * Author: Bulent Abali <abali@us.ibm.com>
17647c734fSRaphael Moreira Zinsly *
18647c734fSRaphael Moreira Zinsly * Definitions of acronyms used here. See
19647c734fSRaphael Moreira Zinsly * P9 NX Gzip Accelerator User's Manual for details:
20647c734fSRaphael Moreira Zinsly * https://github.com/libnxz/power-gzip/blob/develop/doc/power_nx_gzip_um.pdf
21647c734fSRaphael Moreira Zinsly *
22647c734fSRaphael Moreira Zinsly * adler/crc: 32 bit checksums appended to stream tail
23647c734fSRaphael Moreira Zinsly * ce: completion extension
24647c734fSRaphael Moreira Zinsly * cpb: coprocessor parameter block (metadata)
25647c734fSRaphael Moreira Zinsly * crb: coprocessor request block (command)
26647c734fSRaphael Moreira Zinsly * csb: coprocessor status block (status)
27647c734fSRaphael Moreira Zinsly * dht: dynamic huffman table
28647c734fSRaphael Moreira Zinsly * dde: data descriptor element (address, length)
29647c734fSRaphael Moreira Zinsly * ddl: list of ddes
30647c734fSRaphael Moreira Zinsly * dh/fh: dynamic and fixed huffman types
31647c734fSRaphael Moreira Zinsly * fc: coprocessor function code
32647c734fSRaphael Moreira Zinsly * histlen: history/dictionary length
33647c734fSRaphael Moreira Zinsly * history: sliding window of up to 32KB of data
34647c734fSRaphael Moreira Zinsly * lzcount: Deflate LZ symbol counts
35647c734fSRaphael Moreira Zinsly * rembytecnt: remaining byte count
36647c734fSRaphael Moreira Zinsly * sfbt: source final block type; last block's type during decomp
37647c734fSRaphael Moreira Zinsly * spbc: source processed byte count
38647c734fSRaphael Moreira Zinsly * subc: source unprocessed bit count
39647c734fSRaphael Moreira Zinsly * tebc: target ending bit count; valid bits in the last byte
40647c734fSRaphael Moreira Zinsly * tpbc: target processed byte count
41647c734fSRaphael Moreira Zinsly * vas: virtual accelerator switch; the user mode interface
42647c734fSRaphael Moreira Zinsly */
43647c734fSRaphael Moreira Zinsly
44647c734fSRaphael Moreira Zinsly #define _ISOC11_SOURCE // For aligned_alloc()
45647c734fSRaphael Moreira Zinsly #define _DEFAULT_SOURCE // For endian.h
46647c734fSRaphael Moreira Zinsly
47647c734fSRaphael Moreira Zinsly #include <stdio.h>
48647c734fSRaphael Moreira Zinsly #include <stdlib.h>
49647c734fSRaphael Moreira Zinsly #include <string.h>
50647c734fSRaphael Moreira Zinsly #include <unistd.h>
51647c734fSRaphael Moreira Zinsly #include <stdint.h>
52647c734fSRaphael Moreira Zinsly #include <sys/types.h>
53647c734fSRaphael Moreira Zinsly #include <sys/stat.h>
54647c734fSRaphael Moreira Zinsly #include <sys/time.h>
55647c734fSRaphael Moreira Zinsly #include <sys/fcntl.h>
56647c734fSRaphael Moreira Zinsly #include <sys/mman.h>
57647c734fSRaphael Moreira Zinsly #include <endian.h>
58647c734fSRaphael Moreira Zinsly #include <bits/endian.h>
59647c734fSRaphael Moreira Zinsly #include <sys/ioctl.h>
60647c734fSRaphael Moreira Zinsly #include <assert.h>
61647c734fSRaphael Moreira Zinsly #include <errno.h>
62647c734fSRaphael Moreira Zinsly #include <signal.h>
63c6c27e3dSHaren Myneni #include "utils.h"
64647c734fSRaphael Moreira Zinsly #include "nxu.h"
65647c734fSRaphael Moreira Zinsly #include "nx.h"
66647c734fSRaphael Moreira Zinsly
67647c734fSRaphael Moreira Zinsly int nx_dbg;
68647c734fSRaphael Moreira Zinsly FILE *nx_gzip_log;
69647c734fSRaphael Moreira Zinsly
70647c734fSRaphael Moreira Zinsly #define NX_MIN(X, Y) (((X) < (Y)) ? (X) : (Y))
71647c734fSRaphael Moreira Zinsly #define FNAME_MAX 1024
72647c734fSRaphael Moreira Zinsly #define FEXT ".nx.gz"
73647c734fSRaphael Moreira Zinsly
74c6c27e3dSHaren Myneni #define SYSFS_MAX_REQ_BUF_PATH "devices/vio/ibm,compression-v1/nx_gzip_caps/req_max_processed_len"
75c6c27e3dSHaren Myneni
76647c734fSRaphael Moreira Zinsly /*
77647c734fSRaphael Moreira Zinsly * LZ counts returned in the user supplied nx_gzip_crb_cpb_t structure.
78647c734fSRaphael Moreira Zinsly */
compress_fht_sample(char * src,uint32_t srclen,char * dst,uint32_t dstlen,int with_count,struct nx_gzip_crb_cpb_t * cmdp,void * handle)79647c734fSRaphael Moreira Zinsly static int compress_fht_sample(char *src, uint32_t srclen, char *dst,
80647c734fSRaphael Moreira Zinsly uint32_t dstlen, int with_count,
81647c734fSRaphael Moreira Zinsly struct nx_gzip_crb_cpb_t *cmdp, void *handle)
82647c734fSRaphael Moreira Zinsly {
83647c734fSRaphael Moreira Zinsly uint32_t fc;
84647c734fSRaphael Moreira Zinsly
85647c734fSRaphael Moreira Zinsly assert(!!cmdp);
86647c734fSRaphael Moreira Zinsly
87647c734fSRaphael Moreira Zinsly put32(cmdp->crb, gzip_fc, 0); /* clear */
88647c734fSRaphael Moreira Zinsly fc = (with_count) ? GZIP_FC_COMPRESS_RESUME_FHT_COUNT :
89647c734fSRaphael Moreira Zinsly GZIP_FC_COMPRESS_RESUME_FHT;
90647c734fSRaphael Moreira Zinsly putnn(cmdp->crb, gzip_fc, fc);
91647c734fSRaphael Moreira Zinsly putnn(cmdp->cpb, in_histlen, 0); /* resuming with no history */
92647c734fSRaphael Moreira Zinsly memset((void *) &cmdp->crb.csb, 0, sizeof(cmdp->crb.csb));
93647c734fSRaphael Moreira Zinsly
94647c734fSRaphael Moreira Zinsly /* Section 6.6 programming notes; spbc may be in two different
95647c734fSRaphael Moreira Zinsly * places depending on FC.
96647c734fSRaphael Moreira Zinsly */
97647c734fSRaphael Moreira Zinsly if (!with_count)
98647c734fSRaphael Moreira Zinsly put32(cmdp->cpb, out_spbc_comp, 0);
99647c734fSRaphael Moreira Zinsly else
100647c734fSRaphael Moreira Zinsly put32(cmdp->cpb, out_spbc_comp_with_count, 0);
101647c734fSRaphael Moreira Zinsly
102647c734fSRaphael Moreira Zinsly /* Figure 6-3 6-4; CSB location */
103647c734fSRaphael Moreira Zinsly put64(cmdp->crb, csb_address, 0);
104647c734fSRaphael Moreira Zinsly put64(cmdp->crb, csb_address,
105647c734fSRaphael Moreira Zinsly (uint64_t) &cmdp->crb.csb & csb_address_mask);
106647c734fSRaphael Moreira Zinsly
107647c734fSRaphael Moreira Zinsly /* Source direct dde (scatter-gather list) */
108647c734fSRaphael Moreira Zinsly clear_dde(cmdp->crb.source_dde);
109647c734fSRaphael Moreira Zinsly putnn(cmdp->crb.source_dde, dde_count, 0);
110647c734fSRaphael Moreira Zinsly put32(cmdp->crb.source_dde, ddebc, srclen);
111647c734fSRaphael Moreira Zinsly put64(cmdp->crb.source_dde, ddead, (uint64_t) src);
112647c734fSRaphael Moreira Zinsly
113647c734fSRaphael Moreira Zinsly /* Target direct dde (scatter-gather list) */
114647c734fSRaphael Moreira Zinsly clear_dde(cmdp->crb.target_dde);
115647c734fSRaphael Moreira Zinsly putnn(cmdp->crb.target_dde, dde_count, 0);
116647c734fSRaphael Moreira Zinsly put32(cmdp->crb.target_dde, ddebc, dstlen);
117647c734fSRaphael Moreira Zinsly put64(cmdp->crb.target_dde, ddead, (uint64_t) dst);
118647c734fSRaphael Moreira Zinsly
119647c734fSRaphael Moreira Zinsly /* Submit the crb, the job descriptor, to the accelerator */
120647c734fSRaphael Moreira Zinsly return nxu_submit_job(cmdp, handle);
121647c734fSRaphael Moreira Zinsly }
122647c734fSRaphael Moreira Zinsly
123647c734fSRaphael Moreira Zinsly /*
124647c734fSRaphael Moreira Zinsly * Prepares a blank no filename no timestamp gzip header and returns
125647c734fSRaphael Moreira Zinsly * the number of bytes written to buf.
126647c734fSRaphael Moreira Zinsly * Gzip specification at https://tools.ietf.org/html/rfc1952
127647c734fSRaphael Moreira Zinsly */
gzip_header_blank(char * buf)128647c734fSRaphael Moreira Zinsly int gzip_header_blank(char *buf)
129647c734fSRaphael Moreira Zinsly {
130647c734fSRaphael Moreira Zinsly int i = 0;
131647c734fSRaphael Moreira Zinsly
132647c734fSRaphael Moreira Zinsly buf[i++] = 0x1f; /* ID1 */
133647c734fSRaphael Moreira Zinsly buf[i++] = 0x8b; /* ID2 */
134647c734fSRaphael Moreira Zinsly buf[i++] = 0x08; /* CM */
135647c734fSRaphael Moreira Zinsly buf[i++] = 0x00; /* FLG */
136647c734fSRaphael Moreira Zinsly buf[i++] = 0x00; /* MTIME */
137647c734fSRaphael Moreira Zinsly buf[i++] = 0x00; /* MTIME */
138647c734fSRaphael Moreira Zinsly buf[i++] = 0x00; /* MTIME */
139647c734fSRaphael Moreira Zinsly buf[i++] = 0x00; /* MTIME */
140647c734fSRaphael Moreira Zinsly buf[i++] = 0x04; /* XFL 4=fastest */
141647c734fSRaphael Moreira Zinsly buf[i++] = 0x03; /* OS UNIX */
142647c734fSRaphael Moreira Zinsly
143647c734fSRaphael Moreira Zinsly return i;
144647c734fSRaphael Moreira Zinsly }
145647c734fSRaphael Moreira Zinsly
146647c734fSRaphael Moreira Zinsly /*
147647c734fSRaphael Moreira Zinsly * Z_SYNC_FLUSH as described in zlib.h.
148647c734fSRaphael Moreira Zinsly * Returns number of appended bytes
149647c734fSRaphael Moreira Zinsly */
append_sync_flush(char * buf,int tebc,int final)150647c734fSRaphael Moreira Zinsly int append_sync_flush(char *buf, int tebc, int final)
151647c734fSRaphael Moreira Zinsly {
152647c734fSRaphael Moreira Zinsly uint64_t flush;
153647c734fSRaphael Moreira Zinsly int shift = (tebc & 0x7);
154647c734fSRaphael Moreira Zinsly
155647c734fSRaphael Moreira Zinsly if (tebc > 0) {
156647c734fSRaphael Moreira Zinsly /* Last byte is partially full */
157647c734fSRaphael Moreira Zinsly buf = buf - 1;
158647c734fSRaphael Moreira Zinsly *buf = *buf & (unsigned char) ((1<<tebc)-1);
159647c734fSRaphael Moreira Zinsly } else
160647c734fSRaphael Moreira Zinsly *buf = 0;
161647c734fSRaphael Moreira Zinsly flush = ((0x1ULL & final) << shift) | *buf;
162647c734fSRaphael Moreira Zinsly shift = shift + 3; /* BFINAL and BTYPE written */
163647c734fSRaphael Moreira Zinsly shift = (shift <= 8) ? 8 : 16;
164647c734fSRaphael Moreira Zinsly flush |= (0xFFFF0000ULL) << shift; /* Zero length block */
165647c734fSRaphael Moreira Zinsly shift = shift + 32;
166647c734fSRaphael Moreira Zinsly while (shift > 0) {
167647c734fSRaphael Moreira Zinsly *buf++ = (unsigned char) (flush & 0xffULL);
168647c734fSRaphael Moreira Zinsly flush = flush >> 8;
169647c734fSRaphael Moreira Zinsly shift = shift - 8;
170647c734fSRaphael Moreira Zinsly }
171647c734fSRaphael Moreira Zinsly return(((tebc > 5) || (tebc == 0)) ? 5 : 4);
172647c734fSRaphael Moreira Zinsly }
173647c734fSRaphael Moreira Zinsly
174647c734fSRaphael Moreira Zinsly /*
175647c734fSRaphael Moreira Zinsly * Final deflate block bit. This call assumes the block
176647c734fSRaphael Moreira Zinsly * beginning is byte aligned.
177647c734fSRaphael Moreira Zinsly */
set_bfinal(void * buf,int bfinal)178647c734fSRaphael Moreira Zinsly static void set_bfinal(void *buf, int bfinal)
179647c734fSRaphael Moreira Zinsly {
180647c734fSRaphael Moreira Zinsly char *b = buf;
181647c734fSRaphael Moreira Zinsly
182647c734fSRaphael Moreira Zinsly if (bfinal)
183647c734fSRaphael Moreira Zinsly *b = *b | (unsigned char) 0x01;
184647c734fSRaphael Moreira Zinsly else
185647c734fSRaphael Moreira Zinsly *b = *b & (unsigned char) 0xfe;
186647c734fSRaphael Moreira Zinsly }
187647c734fSRaphael Moreira Zinsly
compress_file(int argc,char ** argv,void * handle)188647c734fSRaphael Moreira Zinsly int compress_file(int argc, char **argv, void *handle)
189647c734fSRaphael Moreira Zinsly {
190647c734fSRaphael Moreira Zinsly char *inbuf, *outbuf, *srcbuf, *dstbuf;
191647c734fSRaphael Moreira Zinsly char outname[FNAME_MAX];
192647c734fSRaphael Moreira Zinsly uint32_t srclen, dstlen;
193647c734fSRaphael Moreira Zinsly uint32_t flushlen, chunk;
194647c734fSRaphael Moreira Zinsly size_t inlen, outlen, dsttotlen, srctotlen;
195647c734fSRaphael Moreira Zinsly uint32_t crc, spbc, tpbc, tebc;
196647c734fSRaphael Moreira Zinsly int lzcounts = 0;
197647c734fSRaphael Moreira Zinsly int cc;
198647c734fSRaphael Moreira Zinsly int num_hdr_bytes;
199647c734fSRaphael Moreira Zinsly struct nx_gzip_crb_cpb_t *cmdp;
200647c734fSRaphael Moreira Zinsly uint32_t pagelen = 65536;
201647c734fSRaphael Moreira Zinsly int fault_tries = NX_MAX_FAULTS;
202c6c27e3dSHaren Myneni char buf[32];
203647c734fSRaphael Moreira Zinsly
204647c734fSRaphael Moreira Zinsly cmdp = (void *)(uintptr_t)
205647c734fSRaphael Moreira Zinsly aligned_alloc(sizeof(struct nx_gzip_crb_cpb_t),
206647c734fSRaphael Moreira Zinsly sizeof(struct nx_gzip_crb_cpb_t));
207647c734fSRaphael Moreira Zinsly
208647c734fSRaphael Moreira Zinsly if (argc != 2) {
209647c734fSRaphael Moreira Zinsly fprintf(stderr, "usage: %s <fname>\n", argv[0]);
210647c734fSRaphael Moreira Zinsly exit(-1);
211647c734fSRaphael Moreira Zinsly }
212*8d7253dcSBenjamin Gray if (read_file_alloc(argv[1], &inbuf, &inlen))
213647c734fSRaphael Moreira Zinsly exit(-1);
214647c734fSRaphael Moreira Zinsly fprintf(stderr, "file %s read, %ld bytes\n", argv[1], inlen);
215647c734fSRaphael Moreira Zinsly
216647c734fSRaphael Moreira Zinsly /* Generous output buffer for header/trailer */
217647c734fSRaphael Moreira Zinsly outlen = 2 * inlen + 1024;
218647c734fSRaphael Moreira Zinsly
219647c734fSRaphael Moreira Zinsly assert(NULL != (outbuf = (char *)malloc(outlen)));
220647c734fSRaphael Moreira Zinsly nxu_touch_pages(outbuf, outlen, pagelen, 1);
221647c734fSRaphael Moreira Zinsly
222c6c27e3dSHaren Myneni /*
223c6c27e3dSHaren Myneni * On PowerVM, the hypervisor defines the maximum request buffer
224c6c27e3dSHaren Myneni * size is defined and this value is available via sysfs.
225c6c27e3dSHaren Myneni */
226c6c27e3dSHaren Myneni if (!read_sysfs_file(SYSFS_MAX_REQ_BUF_PATH, buf, sizeof(buf))) {
227c6c27e3dSHaren Myneni chunk = atoi(buf);
228c6c27e3dSHaren Myneni } else {
229c6c27e3dSHaren Myneni /* sysfs entry is not available on PowerNV */
230647c734fSRaphael Moreira Zinsly /* Compress piecemeal in smallish chunks */
231647c734fSRaphael Moreira Zinsly chunk = 1<<22;
232c6c27e3dSHaren Myneni }
233647c734fSRaphael Moreira Zinsly
234647c734fSRaphael Moreira Zinsly /* Write the gzip header to the stream */
235647c734fSRaphael Moreira Zinsly num_hdr_bytes = gzip_header_blank(outbuf);
236647c734fSRaphael Moreira Zinsly dstbuf = outbuf + num_hdr_bytes;
237647c734fSRaphael Moreira Zinsly outlen = outlen - num_hdr_bytes;
238647c734fSRaphael Moreira Zinsly dsttotlen = num_hdr_bytes;
239647c734fSRaphael Moreira Zinsly
240647c734fSRaphael Moreira Zinsly srcbuf = inbuf;
241647c734fSRaphael Moreira Zinsly srctotlen = 0;
242647c734fSRaphael Moreira Zinsly
243647c734fSRaphael Moreira Zinsly /* Init the CRB, the coprocessor request block */
244647c734fSRaphael Moreira Zinsly memset(&cmdp->crb, 0, sizeof(cmdp->crb));
245647c734fSRaphael Moreira Zinsly
246647c734fSRaphael Moreira Zinsly /* Initial gzip crc32 */
247647c734fSRaphael Moreira Zinsly put32(cmdp->cpb, in_crc, 0);
248647c734fSRaphael Moreira Zinsly
249647c734fSRaphael Moreira Zinsly while (inlen > 0) {
250647c734fSRaphael Moreira Zinsly
251647c734fSRaphael Moreira Zinsly /* Submit chunk size source data per job */
252647c734fSRaphael Moreira Zinsly srclen = NX_MIN(chunk, inlen);
253647c734fSRaphael Moreira Zinsly /* Supply large target in case data expands */
254647c734fSRaphael Moreira Zinsly dstlen = NX_MIN(2*srclen, outlen);
255647c734fSRaphael Moreira Zinsly
256647c734fSRaphael Moreira Zinsly /* Page faults are handled by the user code */
257647c734fSRaphael Moreira Zinsly
258647c734fSRaphael Moreira Zinsly /* Fault-in pages; an improved code wouldn't touch so
259647c734fSRaphael Moreira Zinsly * many pages but would try to estimate the
260647c734fSRaphael Moreira Zinsly * compression ratio and adjust both the src and dst
261647c734fSRaphael Moreira Zinsly * touch amounts.
262647c734fSRaphael Moreira Zinsly */
263647c734fSRaphael Moreira Zinsly nxu_touch_pages(cmdp, sizeof(struct nx_gzip_crb_cpb_t), pagelen,
264647c734fSRaphael Moreira Zinsly 1);
265647c734fSRaphael Moreira Zinsly nxu_touch_pages(srcbuf, srclen, pagelen, 0);
266647c734fSRaphael Moreira Zinsly nxu_touch_pages(dstbuf, dstlen, pagelen, 1);
267647c734fSRaphael Moreira Zinsly
268647c734fSRaphael Moreira Zinsly cc = compress_fht_sample(
269647c734fSRaphael Moreira Zinsly srcbuf, srclen,
270647c734fSRaphael Moreira Zinsly dstbuf, dstlen,
271647c734fSRaphael Moreira Zinsly lzcounts, cmdp, handle);
272647c734fSRaphael Moreira Zinsly
273647c734fSRaphael Moreira Zinsly if (cc != ERR_NX_OK && cc != ERR_NX_TPBC_GT_SPBC &&
274f0479c4bSHaren Myneni cc != ERR_NX_AT_FAULT) {
275647c734fSRaphael Moreira Zinsly fprintf(stderr, "nx error: cc= %d\n", cc);
276647c734fSRaphael Moreira Zinsly exit(-1);
277647c734fSRaphael Moreira Zinsly }
278647c734fSRaphael Moreira Zinsly
279647c734fSRaphael Moreira Zinsly /* Page faults are handled by the user code */
280f0479c4bSHaren Myneni if (cc == ERR_NX_AT_FAULT) {
281647c734fSRaphael Moreira Zinsly NXPRT(fprintf(stderr, "page fault: cc= %d, ", cc));
282647c734fSRaphael Moreira Zinsly NXPRT(fprintf(stderr, "try= %d, fsa= %08llx\n",
283647c734fSRaphael Moreira Zinsly fault_tries,
284647c734fSRaphael Moreira Zinsly (unsigned long long) cmdp->crb.csb.fsaddr));
285647c734fSRaphael Moreira Zinsly fault_tries--;
286647c734fSRaphael Moreira Zinsly if (fault_tries > 0) {
287647c734fSRaphael Moreira Zinsly continue;
288647c734fSRaphael Moreira Zinsly } else {
289647c734fSRaphael Moreira Zinsly fprintf(stderr, "error: cannot progress; ");
290647c734fSRaphael Moreira Zinsly fprintf(stderr, "too many faults\n");
291647c734fSRaphael Moreira Zinsly exit(-1);
2920db11461SYang Li }
293647c734fSRaphael Moreira Zinsly }
294647c734fSRaphael Moreira Zinsly
295647c734fSRaphael Moreira Zinsly fault_tries = NX_MAX_FAULTS; /* Reset for the next chunk */
296647c734fSRaphael Moreira Zinsly
297647c734fSRaphael Moreira Zinsly inlen = inlen - srclen;
298647c734fSRaphael Moreira Zinsly srcbuf = srcbuf + srclen;
299647c734fSRaphael Moreira Zinsly srctotlen = srctotlen + srclen;
300647c734fSRaphael Moreira Zinsly
301647c734fSRaphael Moreira Zinsly /* Two possible locations for spbc depending on the function
302647c734fSRaphael Moreira Zinsly * code.
303647c734fSRaphael Moreira Zinsly */
304647c734fSRaphael Moreira Zinsly spbc = (!lzcounts) ? get32(cmdp->cpb, out_spbc_comp) :
305647c734fSRaphael Moreira Zinsly get32(cmdp->cpb, out_spbc_comp_with_count);
306647c734fSRaphael Moreira Zinsly assert(spbc == srclen);
307647c734fSRaphael Moreira Zinsly
308647c734fSRaphael Moreira Zinsly /* Target byte count */
309647c734fSRaphael Moreira Zinsly tpbc = get32(cmdp->crb.csb, tpbc);
310647c734fSRaphael Moreira Zinsly /* Target ending bit count */
311647c734fSRaphael Moreira Zinsly tebc = getnn(cmdp->cpb, out_tebc);
312647c734fSRaphael Moreira Zinsly NXPRT(fprintf(stderr, "compressed chunk %d ", spbc));
313647c734fSRaphael Moreira Zinsly NXPRT(fprintf(stderr, "to %d bytes, tebc= %d\n", tpbc, tebc));
314647c734fSRaphael Moreira Zinsly
315647c734fSRaphael Moreira Zinsly if (inlen > 0) { /* More chunks to go */
316647c734fSRaphael Moreira Zinsly set_bfinal(dstbuf, 0);
317647c734fSRaphael Moreira Zinsly dstbuf = dstbuf + tpbc;
318647c734fSRaphael Moreira Zinsly dsttotlen = dsttotlen + tpbc;
319647c734fSRaphael Moreira Zinsly outlen = outlen - tpbc;
320647c734fSRaphael Moreira Zinsly /* Round up to the next byte with a flush
321647c734fSRaphael Moreira Zinsly * block; do not set the BFINAqL bit.
322647c734fSRaphael Moreira Zinsly */
323647c734fSRaphael Moreira Zinsly flushlen = append_sync_flush(dstbuf, tebc, 0);
324647c734fSRaphael Moreira Zinsly dsttotlen = dsttotlen + flushlen;
325647c734fSRaphael Moreira Zinsly outlen = outlen - flushlen;
326647c734fSRaphael Moreira Zinsly dstbuf = dstbuf + flushlen;
327647c734fSRaphael Moreira Zinsly NXPRT(fprintf(stderr, "added sync_flush %d bytes\n",
328647c734fSRaphael Moreira Zinsly flushlen));
329647c734fSRaphael Moreira Zinsly } else { /* Done */
330647c734fSRaphael Moreira Zinsly /* Set the BFINAL bit of the last block per Deflate
331647c734fSRaphael Moreira Zinsly * specification.
332647c734fSRaphael Moreira Zinsly */
333647c734fSRaphael Moreira Zinsly set_bfinal(dstbuf, 1);
334647c734fSRaphael Moreira Zinsly dstbuf = dstbuf + tpbc;
335647c734fSRaphael Moreira Zinsly dsttotlen = dsttotlen + tpbc;
336647c734fSRaphael Moreira Zinsly outlen = outlen - tpbc;
337647c734fSRaphael Moreira Zinsly }
338647c734fSRaphael Moreira Zinsly
339647c734fSRaphael Moreira Zinsly /* Resuming crc32 for the next chunk */
340647c734fSRaphael Moreira Zinsly crc = get32(cmdp->cpb, out_crc);
341647c734fSRaphael Moreira Zinsly put32(cmdp->cpb, in_crc, crc);
342647c734fSRaphael Moreira Zinsly crc = be32toh(crc);
343647c734fSRaphael Moreira Zinsly }
344647c734fSRaphael Moreira Zinsly
345647c734fSRaphael Moreira Zinsly /* Append crc32 and ISIZE to the end */
346647c734fSRaphael Moreira Zinsly memcpy(dstbuf, &crc, 4);
347647c734fSRaphael Moreira Zinsly memcpy(dstbuf+4, &srctotlen, 4);
348647c734fSRaphael Moreira Zinsly dsttotlen = dsttotlen + 8;
349647c734fSRaphael Moreira Zinsly outlen = outlen - 8;
350647c734fSRaphael Moreira Zinsly
351647c734fSRaphael Moreira Zinsly assert(FNAME_MAX > (strlen(argv[1]) + strlen(FEXT)));
352647c734fSRaphael Moreira Zinsly strcpy(outname, argv[1]);
353647c734fSRaphael Moreira Zinsly strcat(outname, FEXT);
354a974f0c1SBenjamin Gray if (write_file(outname, outbuf, dsttotlen)) {
355647c734fSRaphael Moreira Zinsly fprintf(stderr, "write error: %s\n", outname);
356647c734fSRaphael Moreira Zinsly exit(-1);
357647c734fSRaphael Moreira Zinsly }
358647c734fSRaphael Moreira Zinsly
359647c734fSRaphael Moreira Zinsly fprintf(stderr, "compressed %ld to %ld bytes total, ", srctotlen,
360647c734fSRaphael Moreira Zinsly dsttotlen);
361647c734fSRaphael Moreira Zinsly fprintf(stderr, "crc32 checksum = %08x\n", crc);
362647c734fSRaphael Moreira Zinsly
363647c734fSRaphael Moreira Zinsly if (inbuf != NULL)
364647c734fSRaphael Moreira Zinsly free(inbuf);
365647c734fSRaphael Moreira Zinsly
366647c734fSRaphael Moreira Zinsly if (outbuf != NULL)
367647c734fSRaphael Moreira Zinsly free(outbuf);
368647c734fSRaphael Moreira Zinsly
369647c734fSRaphael Moreira Zinsly return 0;
370647c734fSRaphael Moreira Zinsly }
371647c734fSRaphael Moreira Zinsly
main(int argc,char ** argv)372647c734fSRaphael Moreira Zinsly int main(int argc, char **argv)
373647c734fSRaphael Moreira Zinsly {
374647c734fSRaphael Moreira Zinsly int rc;
375647c734fSRaphael Moreira Zinsly struct sigaction act;
376647c734fSRaphael Moreira Zinsly void *handle;
377647c734fSRaphael Moreira Zinsly
378647c734fSRaphael Moreira Zinsly nx_dbg = 0;
379647c734fSRaphael Moreira Zinsly nx_gzip_log = NULL;
380647c734fSRaphael Moreira Zinsly act.sa_handler = 0;
381647c734fSRaphael Moreira Zinsly act.sa_sigaction = nxu_sigsegv_handler;
382647c734fSRaphael Moreira Zinsly act.sa_flags = SA_SIGINFO;
383647c734fSRaphael Moreira Zinsly act.sa_restorer = 0;
384647c734fSRaphael Moreira Zinsly sigemptyset(&act.sa_mask);
385647c734fSRaphael Moreira Zinsly sigaction(SIGSEGV, &act, NULL);
386647c734fSRaphael Moreira Zinsly
387647c734fSRaphael Moreira Zinsly handle = nx_function_begin(NX_FUNC_COMP_GZIP, 0);
388647c734fSRaphael Moreira Zinsly if (!handle) {
389647c734fSRaphael Moreira Zinsly fprintf(stderr, "Unable to init NX, errno %d\n", errno);
390647c734fSRaphael Moreira Zinsly exit(-1);
391647c734fSRaphael Moreira Zinsly }
392647c734fSRaphael Moreira Zinsly
393647c734fSRaphael Moreira Zinsly rc = compress_file(argc, argv, handle);
394647c734fSRaphael Moreira Zinsly
395647c734fSRaphael Moreira Zinsly nx_function_end(handle);
396647c734fSRaphael Moreira Zinsly
397647c734fSRaphael Moreira Zinsly return rc;
398647c734fSRaphael Moreira Zinsly }
399