All pastes #1015645 Raw Edit

Someone

public text v1 · immutable
#1015645 ·published 2008-05-12 11:16 UTC
rendered paste body
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <errno.h>
#include <sys/param.h>

#define BUFSIZE	(2048 * 8192) /* must be an even factor of 2^30 */

typedef struct {
	char name[32];
	char file_name[15];
	char part;
	char media;
	char pad1[4];
	char unk;
	char pad2[10];
} usbex_info_t;

void error_exit(const char *msg)
{
	printf("%s: %s\n", strerror(errno), msg);
	exit(-1);
}

unsigned int crc32(char *string)
{
	int crc, table, count, byte;
	unsigned int crctab[256];

	for (table=0; table<256; table++)
	{
		crc = table << 24;

		for (count=8; count>0; count--)
		{
			if (crc < 0) crc = crc << 1;
			else crc = (crc << 1) ^ 0x04C11DB7;
		}
		crctab[255-table] = crc;
	}

	do {
		byte = string[count++];
		crc = crctab[byte ^ ((crc >> 24) & 0xFF)] ^ ((crc << 8) & 0xFFFFFF00);
	} while (string[count-1] != 0);

	return crc;
}

int main(int argc, char **argv)
{
	int fdin, fdout, ret, i, part = 0;
	char outfile[MAXPATHLEN + 1];
	void *buffer;
	usbex_info_t img_cnf;
	unsigned int crc;

	if(argc != 6)
	{
		printf("usage: ui_install SOURCE DEST TITLE EXECFILE CD/DVD\nExample: ui_install /dev/cdrom /mnt/usbdisk My\\ very\\ 1st\\ demo SLUS_123.45 CD\n");
		return 1;
	}

	if(strlen(argv[3]) > 31)
	{
		printf("Title %s is too long", argv[3]);
		exit(-1);
	}

	if(!strcasecmp(argv[5], "CD"))
		img_cnf.media = 0x12;
	else if(!strcasecmp(argv[5], "DVD"))
		img_cnf.media = 0x14;
	else
	{
		printf("Invalid media type %s", argv[5]);
		exit(-1);
	}
	strncpy(img_cnf.file_name, "ul.", 3);
	strncpy(&img_cnf.file_name[3], argv[4], 11);
	strncpy(img_cnf.name, argv[3], 31);
	img_cnf.unk = 0x08;

	crc = crc32(argv[3]);
	if((fdin = open(argv[1], O_RDONLY)) == -1)
		error_exit(argv[1]);
	buffer = (void *)malloc(BUFSIZE);

	sprintf(outfile, "%s/ul.%08X.%s.%02X", argv[2], crc, argv[4], part);
	if((fdout = open(outfile, O_WRONLY | O_TRUNC | O_CREAT)) == -1)
		error_exit(outfile);

	for(i = 0; ret = read(fdin, buffer, BUFSIZE); i += ret)
	{
		if(ret == -1)
			error_exit("read");
		if(i == (1<<30))
		{
			close(fdout);
			sprintf(outfile, "%s/ul.%08X.%s.%02X", argv[2], crc, argv[4], ++part);
			if((fdout = open(outfile, O_WRONLY | O_TRUNC | O_CREAT)) == -1)
				error_exit(outfile);
			i = 0;
		}
		if(write(fdout, buffer, ret) == -1)
			error_exit("write");
	}
	free(buffer);
	close(fdin);
	close(fdout);

	img_cnf.part = ++part;
	sprintf(outfile, "%s/ul.cfg", argv[2]);
	if((fdout = open(outfile, O_WRONLY | O_APPEND | O_CREAT)) == -1)
		error_exit(outfile);

	write(fdout, &img_cnf, sizeof(img_cnf));
	close(fdout);
	return 0;
}