view src/common.c @ 88:af4ed765c1ac

*: add IUP GUI a lot simpler than win32, but I assume there's more going on in the background that I don't know about :)
author Paper <paper@tflc.us>
date Mon, 14 Jul 2025 02:39:24 -0400
parents 8f90d5addda9
children
line wrap: on
line source

#include "common.h"

#include <string.h>
#include <stdlib.h>

/* hardcoded magic values; stored at 0x18... */
static const uint8_t magic_veg[16] = {0xEF, 0x29, 0xC4, 0x46, 0x4A, 0x90, 0xD2, 0x11, 0x87, 0x22, 0x00, 0xC0, 0x4F, 0x8E, 0xDB, 0x8A};
static const uint8_t magic_vf[16]  = {0xF6, 0x1B, 0x3C, 0x53, 0x35, 0xD6, 0xF3, 0x43, 0x8A, 0x90, 0x64, 0xB8, 0x87, 0x23, 0x1F, 0x7F};

int set_file_information(FILE *target, uint8_t version, enum types type)
{
	const uint8_t* magic = (type == TYPES_VF) ? magic_vf : magic_veg;

	if (fseek(target, 0x46, SEEK_SET))
		return -1;

	if (fputc(version, target) == EOF)
		return -1;

	if (fseek(target, 0x18, SEEK_SET))
		return -1;

	if (fwrite(magic, sizeof(*magic), 16, target) < 16)
		return -1;

	return 0;
}

int get_file_information(FILE *input, uint8_t *version, enum types *type)
{
	uint8_t magic[16] = {0};

	if (fseek(input, 0x46, SEEK_SET))
		return -1;

	*version = fgetc(input);

	if (fseek(input, 0x18, SEEK_SET))
		return -1;

	/* read the WHOLE magic, then memcmp */
	if (fread(magic, sizeof(*magic), ARRAY_SIZE(magic), input) < ARRAY_SIZE(magic))
		return -1;

	if (!memcmp(magic, magic_veg, ARRAY_SIZE(magic))) {
		*type = TYPES_VEG;
	} else if (!memcmp(magic, magic_vf, ARRAY_SIZE(magic))) {
		*type = TYPES_VF;
	} else {
		*type = TYPES_UNKNOWN;
	}

	return 0;
}

/* source needs read permissions, target needs write permissions, both must be in binary mode */
int copy_file(FILE *source, FILE *target)
{
	char ch[4096];

	while (!feof(source)) {
		size_t b = fread(ch, 1, sizeof(ch), source);
		if (b && (fwrite(ch, 1, b, target) < b))
			return -1;
	}

	return 0;
}

/* ------------------------------------------------------------------------ */

char *str_ndup(const char *str, size_t sz)
{
	char *x;

	x = malloc(sz + 1);
	if (!x)
		return NULL;

	memcpy(x, str, sz);
	x[sz] = 0;

	return x;
}

char *str_dup(const char *str)
{
	return str_ndup(str, strlen(str));
}

/* ------------------------------------------------------------------------ */

const char *type_names[] = {
	[TYPES_VF] = "Movie Studio",
	[TYPES_VEG] = "Vegas Pro",
	[TYPES_UNKNOWN] = "Unknown",
};

/* ------------------------------------------------------------------------ */

char *msvpvf_vasprintf(const char *format, va_list ap)
{
	va_list ap2;
	int needed_size;
	char *output = NULL;

	va_copy(ap2, ap);

	needed_size = vsnprintf(NULL, 0, format, ap);
	if (needed_size < 0) {
		va_end(ap2);
		return NULL;
	}

	output = malloc((needed_size + 1) * sizeof(char));
	if (!output) {
		va_end(ap2);
		return NULL;
	}

	needed_size = vsnprintf(output, needed_size + 1, format, ap2);
	if (needed_size < 0) {
		free(output);
		va_end(ap2);
		return NULL;
	}

	va_end(ap2);

	return output;
}

char *msvpvf_asprintf(const char *format, ...)
{
	char *x;
	va_list ap;

	va_start(ap, format);
	x = msvpvf_vasprintf(format, ap);
	va_end(ap);

	return x;
}