h_comp_zlib.c revision 272343
197403Sobrien/* $NetBSD: h_comp_zlib.c,v 1.1 2014/01/14 17:51:39 pgoyette Exp $ */ 297403Sobrien 397403Sobrien/*- 497403Sobrien * Copyright (c) 2014 The NetBSD Foundation, Inc. 5132720Skan * All rights reserved. 697403Sobrien * 7132720Skan * Redistribution and use in source and binary forms, with or without 897403Sobrien * modification, are permitted provided that the following conditions 997403Sobrien * are met: 1097403Sobrien * 1. Redistributions of source code must retain the above copyright 1197403Sobrien * notice, this list of conditions and the following disclaimer. 12132720Skan * 2. Redistributions in binary form must reproduce the above copyright 1397403Sobrien * notice, this list of conditions and the following disclaimer in the 1497403Sobrien * documentation and/or other materials provided with the distribution. 1597403Sobrien * 1697403Sobrien * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 1797403Sobrien * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 18132720Skan * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 1997403Sobrien * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 2097403Sobrien * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 2197403Sobrien * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 2297403Sobrien * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 2397403Sobrien * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 2497403Sobrien * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 2597403Sobrien * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 2697403Sobrien * POSSIBILITY OF SUCH DAMAGE. 2797403Sobrien */ 2897403Sobrien 2997403Sobrien#include <err.h> 3097403Sobrien#include <fcntl.h> 3197403Sobrien#include <string.h> 3297403Sobrien#include <zlib.h> 3397403Sobrien 3497403Sobrien#include <sys/ioctl.h> 3597403Sobrien#include <sys/time.h> 3697403Sobrien 3797403Sobrien#include <crypto/cryptodev.h> 3897403Sobrien 3997403Sobrienchar text[10000] = {0}; 4097403Sobrien 4197403Sobrienint 4297403Sobrienmain(void) 4397403Sobrien{ 4497403Sobrien int fd, res; 4597403Sobrien struct session_op cs; 4697403Sobrien struct crypt_op co1; 4797403Sobrien unsigned char buf1[10000], buf2[10000]; 4897403Sobrien z_stream z; 4997403Sobrien 5097403Sobrien fd = open("/dev/crypto", O_RDWR, 0); 5197403Sobrien if (fd < 0) 5297403Sobrien err(1, "open"); 5397403Sobrien memset(&cs, 0, sizeof(cs)); 5497403Sobrien cs.comp_alg = CRYPTO_DEFLATE_COMP; 5597403Sobrien res = ioctl(fd, CIOCGSESSION, &cs); 5697403Sobrien if (res < 0) 57 err(1, "CIOCGSESSION"); 58 59 memset(&co1, 0, sizeof(co1)); 60 co1.ses = cs.ses; 61 co1.op = COP_COMP; 62 co1.len = sizeof(text); 63 co1.src = text; 64 co1.dst = buf1; 65 co1.dst_len = sizeof(buf1); 66 co1.flags = COP_F_BATCH; 67 res = ioctl(fd, CIOCCRYPT, &co1); 68 if (res < 0) 69 err(1, "CIOCCRYPT"); 70 71 memset(&z, 0, sizeof(z)); 72 z.next_in = buf1; 73 z.avail_in = co1.dst_len; 74 z.zalloc = Z_NULL; 75 z.zfree = Z_NULL; 76 z.opaque = 0; 77 z.next_out = buf2; 78 z.avail_out = sizeof(buf2); 79 res = inflateInit2(&z, -15); 80 if (res != Z_OK) 81 errx(1, "inflateInit: %d", res); 82 do { 83 res = inflate(&z, Z_SYNC_FLUSH); 84 } while (res == Z_OK); 85 if (res != Z_STREAM_END) 86 errx(1, "inflate: %d", res); 87 if (z.total_out != sizeof(text)) 88 errx(1, "decomp len %lu", z.total_out); 89 if (memcmp(buf2, text, sizeof(text))) 90 errx(1, "decomp data mismatch"); 91 return 0; 92} 93