1 /* $OpenBSD: nullopen.c,v 1.2 2004/01/22 18:49:35 millert Exp $ */ 2 3 /* 4 * Copyright (c) 2003 Can Erkin Acar 5 * Copyright (c) 1997 Michael Shalayeff 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 * 29 */ 30 31 const char null_rcsid[] = 32 "$OpenBSD: nullopen.c,v 1.2 2004/01/22 18:49:35 millert Exp $"; 33 34 #include <sys/types.h> 35 #include <stdio.h> 36 #include <stdlib.h> 37 #include <errno.h> 38 #include <unistd.h> 39 #include "compress.h" 40 41 typedef struct { 42 off_t total_in; 43 off_t total_out; 44 int fd; 45 int gotmagic; 46 char mode; 47 } null_stream; 48 49 char null_magic[2]; 50 51 52 void * 53 null_open(int fd, const char *mode, char *name, int bits, 54 u_int32_t mtime, int gotmagic) 55 { 56 null_stream *s; 57 58 if (fd < 0 || !mode) 59 return NULL; 60 61 if ((mode[0] != 'r' && mode[0] != 'w') || mode[1] != '\0') { 62 errno = EINVAL; 63 return NULL; 64 } 65 66 if ((s = (null_stream *) calloc(1, sizeof(null_stream))) == NULL) 67 return NULL; 68 69 s->fd = fd; 70 s->gotmagic = gotmagic; 71 s->total_in = s->total_out = 0; 72 s->mode = mode[0]; 73 74 return s; 75 } 76 77 int 78 null_close(void *cookie, struct z_info *info) 79 { 80 null_stream *s = (null_stream*) cookie; 81 int err = 0; 82 83 if (s == NULL) 84 return -1; 85 86 87 if (info != NULL) { 88 info->mtime = 0; 89 info->crc = (u_int32_t)-1; 90 info->hlen = 0; 91 info->total_in = (off_t) s->total_in; 92 info->total_out = (off_t) s->total_out; 93 } 94 95 err = close(s->fd); 96 97 free(s); 98 99 return err; 100 } 101 102 int 103 null_flush(void *cookie, int flush) 104 { 105 null_stream *s = (null_stream*)cookie; 106 107 if (s == NULL || s->mode != 'w') { 108 errno = EBADF; 109 return (-1); 110 } 111 112 return 0; 113 } 114 115 int 116 null_read(void *cookie, char *buf, int len) 117 { 118 null_stream *s = (null_stream*)cookie; 119 120 if (s == NULL) { 121 errno = EBADF; 122 return (-1); 123 } 124 if (s->gotmagic) { 125 if (len < 2) { 126 errno = EBADF; 127 return (-1); 128 } 129 130 buf[0] = null_magic[0]; 131 buf[1] = null_magic[1]; 132 s->gotmagic = 0; 133 134 return (2); 135 } 136 137 return read(s->fd, buf, len); 138 } 139 140 int 141 null_write(void *cookie, const char *buf, int len) 142 { 143 null_stream *s = (null_stream*)cookie; 144 145 if (s == NULL) { 146 errno = EBADF; 147 return (-1); 148 } 149 150 return write(s->fd, buf, len); 151 } 152