xref: /openbsd-src/usr.bin/compress/nullopen.c (revision 379ad95a0e3fd925b2ebdd77a8586fa5be370a03)
1*379ad95aStedu /*	$OpenBSD: nullopen.c,v 1.6 2016/09/03 11:41:10 tedu Exp $	*/
296ff3415Sderaadt 
396ff3415Sderaadt /*
496ff3415Sderaadt  * Copyright (c) 2003 Can Erkin Acar
596ff3415Sderaadt  * Copyright (c) 1997 Michael Shalayeff
696ff3415Sderaadt  * All rights reserved.
796ff3415Sderaadt  *
896ff3415Sderaadt  * Redistribution and use in source and binary forms, with or without
996ff3415Sderaadt  * modification, are permitted provided that the following conditions
1096ff3415Sderaadt  * are met:
1196ff3415Sderaadt  * 1. Redistributions of source code must retain the above copyright
1296ff3415Sderaadt  *    notice, this list of conditions and the following disclaimer.
1396ff3415Sderaadt  * 2. Redistributions in binary form must reproduce the above copyright
1496ff3415Sderaadt  *    notice, this list of conditions and the following disclaimer in the
1596ff3415Sderaadt  *    documentation and/or other materials provided with the distribution.
1696ff3415Sderaadt  *
1796ff3415Sderaadt  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1896ff3415Sderaadt  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
1996ff3415Sderaadt  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2096ff3415Sderaadt  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2196ff3415Sderaadt  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2296ff3415Sderaadt  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2396ff3415Sderaadt  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2496ff3415Sderaadt  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2596ff3415Sderaadt  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2696ff3415Sderaadt  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2796ff3415Sderaadt  * SUCH DAMAGE.
2896ff3415Sderaadt  *
2996ff3415Sderaadt  */
3096ff3415Sderaadt 
3196ff3415Sderaadt #include <sys/types.h>
3296ff3415Sderaadt #include <stdio.h>
3396ff3415Sderaadt #include <stdlib.h>
3496ff3415Sderaadt #include <errno.h>
3596ff3415Sderaadt #include <unistd.h>
3696ff3415Sderaadt #include "compress.h"
3796ff3415Sderaadt 
3896ff3415Sderaadt typedef struct {
3996ff3415Sderaadt 	off_t 	  total_in;
4096ff3415Sderaadt 	off_t 	  total_out;
4196ff3415Sderaadt 	int   	  fd;
4296ff3415Sderaadt 	int       gotmagic;
4396ff3415Sderaadt 	char	  mode;
4496ff3415Sderaadt } null_stream;
4596ff3415Sderaadt 
4696ff3415Sderaadt char null_magic[2];
4796ff3415Sderaadt 
4896ff3415Sderaadt 
4996ff3415Sderaadt void *
null_ropen(int fd,char * name,int gotmagic)50*379ad95aStedu null_ropen(int fd, char *name, int gotmagic)
5196ff3415Sderaadt {
5296ff3415Sderaadt 	null_stream *s;
5396ff3415Sderaadt 
54*379ad95aStedu 	if (fd < 0)
5596ff3415Sderaadt 		return NULL;
5696ff3415Sderaadt 
57cfff592fSderaadt 	if ((s = calloc(1, sizeof(null_stream))) == NULL)
5896ff3415Sderaadt 		return NULL;
5996ff3415Sderaadt 
6096ff3415Sderaadt 	s->fd = fd;
6196ff3415Sderaadt 	s->gotmagic = gotmagic;
6296ff3415Sderaadt 	s->total_in = s->total_out = 0;
63*379ad95aStedu 	s->mode = 'r';
64*379ad95aStedu 
65*379ad95aStedu 	return s;
66*379ad95aStedu }
67*379ad95aStedu 
68*379ad95aStedu void *
null_wopen(int fd,char * name,int bits,u_int32_t mtime)69*379ad95aStedu null_wopen(int fd, char *name, int bits, u_int32_t mtime)
70*379ad95aStedu {
71*379ad95aStedu 	null_stream *s;
72*379ad95aStedu 
73*379ad95aStedu 	if (fd < 0)
74*379ad95aStedu 		return NULL;
75*379ad95aStedu 
76*379ad95aStedu 	if ((s = calloc(1, sizeof(null_stream))) == NULL)
77*379ad95aStedu 		return NULL;
78*379ad95aStedu 
79*379ad95aStedu 	s->fd = fd;
80*379ad95aStedu 	s->gotmagic = 0;
81*379ad95aStedu 	s->total_in = s->total_out = 0;
82*379ad95aStedu 	s->mode = 'w';
8396ff3415Sderaadt 
8496ff3415Sderaadt 	return s;
8596ff3415Sderaadt }
8696ff3415Sderaadt 
8796ff3415Sderaadt int
null_close(void * cookie,struct z_info * info,const char * name,struct stat * sb)88766a4c19Sotto null_close(void *cookie, struct z_info *info, const char *name, struct stat *sb)
8996ff3415Sderaadt {
9096ff3415Sderaadt 	null_stream *s = (null_stream*) cookie;
9196ff3415Sderaadt 	int err = 0;
9296ff3415Sderaadt 
9396ff3415Sderaadt 	if (s == NULL)
9496ff3415Sderaadt 		return -1;
9596ff3415Sderaadt 
9696ff3415Sderaadt 
9796ff3415Sderaadt 	if (info != NULL) {
9896ff3415Sderaadt 		info->mtime = 0;
9996ff3415Sderaadt 		info->crc =  (u_int32_t)-1;
10096ff3415Sderaadt 		info->hlen = 0;
10196ff3415Sderaadt 		info->total_in = (off_t) s->total_in;
10296ff3415Sderaadt 		info->total_out = (off_t) s->total_out;
10396ff3415Sderaadt 	}
10496ff3415Sderaadt 
105766a4c19Sotto 	setfile(name, s->fd, sb);
10696ff3415Sderaadt 	err = close(s->fd);
10796ff3415Sderaadt 
10896ff3415Sderaadt 	free(s);
10996ff3415Sderaadt 
11096ff3415Sderaadt 	return err;
11196ff3415Sderaadt }
11296ff3415Sderaadt 
11396ff3415Sderaadt int
null_flush(void * cookie,int flush)11496ff3415Sderaadt null_flush(void *cookie, int flush)
11596ff3415Sderaadt {
11696ff3415Sderaadt 	null_stream *s = (null_stream*)cookie;
11796ff3415Sderaadt 
11896ff3415Sderaadt 	if (s == NULL || s->mode != 'w') {
11996ff3415Sderaadt 		errno = EBADF;
12096ff3415Sderaadt 		return (-1);
12196ff3415Sderaadt 	}
12296ff3415Sderaadt 
12396ff3415Sderaadt 	return 0;
12496ff3415Sderaadt }
12596ff3415Sderaadt 
12696ff3415Sderaadt int
null_read(void * cookie,char * buf,int len)12796ff3415Sderaadt null_read(void *cookie, char *buf, int len)
12896ff3415Sderaadt {
12996ff3415Sderaadt 	null_stream *s = (null_stream*)cookie;
13096ff3415Sderaadt 
13196ff3415Sderaadt 	if (s == NULL) {
13296ff3415Sderaadt 		errno = EBADF;
13396ff3415Sderaadt 		return (-1);
13496ff3415Sderaadt 	}
13596ff3415Sderaadt 	if (s->gotmagic) {
13696ff3415Sderaadt 		if (len < 2) {
13796ff3415Sderaadt 			errno = EBADF;
13896ff3415Sderaadt 			return (-1);
13996ff3415Sderaadt 		}
14096ff3415Sderaadt 
14196ff3415Sderaadt 		buf[0] = null_magic[0];
14296ff3415Sderaadt 		buf[1] = null_magic[1];
14396ff3415Sderaadt 		s->gotmagic = 0;
14496ff3415Sderaadt 
14596ff3415Sderaadt 		return (2);
14696ff3415Sderaadt 	}
14796ff3415Sderaadt 
14896ff3415Sderaadt 	return read(s->fd, buf, len);
14996ff3415Sderaadt }
15096ff3415Sderaadt 
15196ff3415Sderaadt int
null_write(void * cookie,const char * buf,int len)15296ff3415Sderaadt null_write(void *cookie, const char *buf, int len)
15396ff3415Sderaadt {
15496ff3415Sderaadt 	null_stream *s = (null_stream*)cookie;
15596ff3415Sderaadt 
15696ff3415Sderaadt 	if (s == NULL) {
15796ff3415Sderaadt 		errno = EBADF;
15896ff3415Sderaadt 		return (-1);
15996ff3415Sderaadt 	}
16096ff3415Sderaadt 
16196ff3415Sderaadt 	return write(s->fd, buf, len);
16296ff3415Sderaadt }
163