<commit_before>/*
 * EMULAB-COPYRIGHT
 * Copyright (c) 2000-2002 University of Utah and the Flux Group.
 * All rights reserved.
 */

/*
 * Usage: imageunzip <input file>
 *
 * Writes the uncompressed data to stdout.
 *
 * XXX: Be nice to use pwrite. That would simplify the code a alot, but
 * you cannot pwrite to a device that does not support seek (stdout),
 * and I want to retain that capability.
 */

#include <stdio.h>
#include <fcntl.h>
#include <assert.h>
#include <unistd.h>
#include <zlib.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/disklabel.h>
#include "imagehdr.h"

/*
 * In slice mode, we read the DOS MBR to find out where the slice is on
 * the raw disk, and then seek to that spot. This avoids sillyness in
 * the BSD kernel having to do with disklabels. 
 *
 * These numbers are in sectors.
 */
static long		outputminsec	= 0;
static long		outputmaxsec	= 0;
static long long	outputmaxsize	= 0;	/* Sanity check */

/* Why is this not defined in a public header file? */
#define BOOT_MAGIC	0xAA55

#define CHECK_ERR(err, msg) { \
    if (err != Z_OK) { \
        fprintf(stderr, "%s error: %d\n", msg, err); \
        exit(1); \
    } \
}

#define SECSIZE 512
#define BSIZE	(32 * 1024)
#define OUTSIZE (3 * BSIZE)
char		inbuf[BSIZE], outbuf[OUTSIZE + SECSIZE], zeros[BSIZE];

static int	 infd, outfd;
static int	 doseek = 0;
static int	 debug  = 0;
static long long total  = 0;
static char	 chunkbuf[SUBBLOCKSIZE];

int		inflate_subblock(char *);
void		writezeros(off_t zcount);

#ifdef linux
#define devlseek	lseek
#define devwrite	write
#else
static inline off_t devlseek(int fd, off_t off, int whence)
{
	off_t noff;
	assert((off & (SECSIZE-1)) == 0);
	noff = lseek(fd, off, whence);
	assert(noff == (off_t)-1 || (noff & (SECSIZE-1)) == 0);
	return noff;
}

static inline int devwrite(int fd, const void *buf, size_t size)
{
	assert((size & (SECSIZE-1)) == 0);
	assert(outputmaxsize == 0 || (total + size) <= outputmaxsize);
	return write(fd, buf, size);
}

static inline int devread(int fd, void *buf, size_t size)
{
	assert((size & (SECSIZE-1)) == 0);
	return read(fd, buf, size);
}
#endif

static void
usage(void)
{
	fprintf(stderr, "usage: "
		"imageunzip [-d] [-s #] <input filename> [output filename]\n"
		" -s slice        Output to DOS slice (DOS numbering 1-4)\n"
		"                 NOTE: Must specify a raw disk device.\n"
		" -d              Turn on progressive levels of debugging\n");
	exit(1);
}	

#ifndef FRISBEE
int
main(int argc, char **argv)
{
	int		ch, slice = 0;
	struct timeval  stamp, estamp;

	while ((ch = getopt(argc, argv, "dhs:")) != -1)
		switch(ch) {
		case 'd':
			debug++;
			break;

		case 's':
			slice = atoi(optarg);
			break;

		case 'h':
		case '?':
		default:
			usage();
		}
	argc -= optind;
	argv += optind;

	if (argc < 1 || argc > 2)
		usage();
	if (argc == 1 && slice) {
		fprintf(stderr, "Cannot specify a slice when using stdout!\n");
		usage();
	}

	if (strcmp(argv[0], "-")) {
		if ((infd = open(argv[0], O_RDONLY, 0666)) < 0) {
			perror("opening input file");
			exit(1);
		}
	}
	else
		infd = fileno(stdin);

	if (argc == 2) {
		if ((outfd =
		     open(argv[1], O_RDWR|O_CREAT|O_TRUNC, 0666)) < 0) {
			perror("opening output file");
			exit(1);
		}
		doseek = 1;
	}
	else
		outfd = fileno(stdout);

	if (slice) {
		off_t	minseek;
		
		if (readmbr(slice)) {
			fprintf(stderr, "Failed to read MBR\n");
			exit(1);
		}
		minseek = ((off_t) outputminsec) * SECSIZE;
		
		if (lseek(outfd, minseek, SEEK_SET) < 0) {
			perror("Setting seek pointer to slice");
			exit(1);
		}
	}
	
	gettimeofday(&stamp, 0);
	
	while (1) {
		int	count = sizeof(chunkbuf);
		char	*bp   = chunkbuf;
		<commit_msg>Add debugging options as an aid for Russ.<commit_after>/*
 * EMULAB-COPYRIGHT
 * Copyright (c) 2000-2002 University of Utah and the Flux Group.
 * All rights reserved.
 */

/*
 * Usage: imageunzip <input file>
 *
 * Writes the uncompressed data to stdout.
 *
 * XXX: Be nice to use pwrite. That would simplify the code a alot, but
 * you cannot pwrite to a device that does not support seek (stdout),
 * and I want to retain that capability.
 */

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <assert.h>
#include <unistd.h>
#include <zlib.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/disklabel.h>
#include "imagehdr.h"

/*
 * In slice mode, we read the DOS MBR to find out where the slice is on
 * the raw disk, and then seek to that spot. This avoids sillyness in
 * the BSD kernel having to do with disklabels. 
 *
 * These numbers are in sectors.
 */
static long		outputminsec	= 0;
static long		outputmaxsec	= 0;
static long long	outputmaxsize	= 0;	/* Sanity check */

/* Why is this not defined in a public header file? */
#define BOOT_MAGIC	0xAA55

#define CHECK_ERR(err, msg) { \
    if (err != Z_OK) { \
        fprintf(stderr, "%s error: %d\n", msg, err); \
        exit(1); \
    } \
}

#define SECSIZE 512
#define BSIZE	(32 * 1024)
#define OUTSIZE (3 * BSIZE)
char		inbuf[BSIZE], outbuf[OUTSIZE + SECSIZE], zeros[BSIZE];

static int	 infd, outfd;
static int	 doseek = 0;
static int	 dofill = 0;
static unsigned	 fillpat= 0;
static int	 debug  = 0;
static long long total  = 0;
static char	 chunkbuf[SUBBLOCKSIZE];

int		inflate_subblock(char *);
void		writezeros(off_t zcount);

#ifdef linux
#define devlseek	lseek
#define devwrite	write
#else
static inline off_t devlseek(int fd, off_t off, int whence)
{
	off_t noff;
	assert((off & (SECSIZE-1)) == 0);
	noff = lseek(fd, off, whence);
	assert(noff == (off_t)-1 || (noff & (SECSIZE-1)) == 0);
	return noff;
}

static inline int devwrite(int fd, const void *buf, size_t size)
{
	assert((size & (SECSIZE-1)) == 0);
	assert(outputmaxsize == 0 || (total + size) <= outputmaxsize);
	return write(fd, buf, size);
}

static inline int devread(int fd, void *buf, size_t size)
{
	assert((size & (SECSIZE-1)) == 0);
	return read(fd, buf, size);
}
#endif

static void
usage(void)
{
	fprintf(stderr, "usage: "
		"imageunzip [-d] [-s #] <input filename> [output filename]\n"
		" -s slice        Output to DOS slice (DOS numbering 1-4)\n"
		"                 NOTE: Must specify a raw disk device.\n"
		" -z              Write zeros to free blocks.\n"
		" -p pattern      Write 32 bit pattern to free blocks.\n"
		"                 NOTE: Use -z/-p to avoid seeking.\n"
		" -d              Turn on progressive levels of debugging\n");
	exit(1);
}	

#ifndef FRISBEE
int
main(int argc, char **argv)
{
	int		i, ch, slice = 0;
	struct timeval  stamp, estamp;

	while ((ch = getopt(argc, argv, "dhs:zp:")) != -1)
		switch(ch) {
		case 'd':
			debug++;
			break;

		case 's':
			slice = atoi(optarg);
			break;

		case 'p':
			fillpat = strtoul(optarg, NULL, 0);
		case 'z':
			dofill++;
			break;

		case 'h':
		case '?':
		default:
			usage();
		}
	argc -= optind;
	argv += optind;

	if (argc < 1 || argc > 2)
		usage();
	if (argc == 1 && slice) {
		fprintf(stderr, "Cannot specify a slice when using stdout!\n");
		usage();
	}

	if (fillpat) {
		unsigned	*bp = (unsigned *) &zeros;

		for (i = 0; i < sizeof(zeros)/sizeof(unsigned); i++)
			*bp++ = fillpat;
	}

	if (strcmp(argv[0], "-")) {
		if ((infd = open(argv[0], O_RDONLY, 0666)) < 0) {
			perror("opening input file");
			exit(1);
		}
	}
	else
		infd = fileno(stdin);

	if (argc == 2) {
		if ((outfd =
		     open(argv[1], O_RDWR|O_CREAT|O_TRUNC, 0666)) < 0) {
			perror("opening output file");
			exit(1);
		}
		doseek = !dofill;
	}
	else
		outfd = fileno(stdout);

	if (slice) {
		off_t	minseek;
		
		if (readmbr(slice)) {
			fprintf(stderr, "Failed to read MBR\n");
			exit(1);
		}
		minseek = ((off_t) outputminsec) * SECSIZE;
		
		if (lseek(outfd, minseek, SEEK_SET) < 0) {
			perror("Setting seek pointer to slice");
			exit(1);
		}
	}
	
	gettimeofday(&stamp, 0);
	
	while (1) {
		int	count = sizeof(chunkbuf);
		char	*bp   = chunkbuf;
		
{'fraction_non_alphanumeric': 0.14978365996436752, 'fraction_numerical': 0.01794349707304658, 'mean_word_length': 3.1211326691137913, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 26, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7187124', 'n_tokens_mistral': 3276, 'n_tokens_neox': 2999, 'n_words': 943}