view compal/melody-extr/extr-table.c @ 391:a40557e5b35f

compal/melody-extr: wrote table extractor
author Mychaela Falconia <falcon@freecalypso.org>
date Fri, 01 Apr 2022 05:04:23 +0000
parents
children
line wrap: on
line source

/*
 * This program extracts the table of built-in melodies from a Mot C1xx
 * fw image, the table that is called ringToneInfo_s[] in the special
 * fw version with symbols.
 */

#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>

FILE *binf;

static void
open_and_seek_input(binfname, offset_arg)
	char *binfname, *offset_arg;
{
	u_long offset;

	binf = fopen(binfname, "r");
	if (!binf) {
		perror(binfname);
		exit(1);
	}
	offset = strtoul(offset_arg, 0, 0);
	fseek(binf, offset, SEEK_SET);
}

static unsigned
extr_32le(bin)
	u_char *bin;
{
	return (bin[0]) | (bin[1] << 8) | (bin[2] << 16) | (bin[3] << 24);
}

static void
process_record(idx)
	unsigned idx;
{
	u_char record[16];
	unsigned addr, nent, time_fudge;
	int freq_offset;

	if (fread(&record, sizeof record, 1, binf) != 1) {
		fprintf(stderr, "error reading from binary file\n");
		exit(1);
	}
	addr = extr_32le(record);
	nent = extr_32le(record + 4);
	freq_offset = extr_32le(record + 8);
	time_fudge = extr_32le(record + 12);
	printf("%u\t0x%06X\t%u\t%d\t%u\n", idx, addr, nent, freq_offset,
		time_fudge);
}

main(argc, argv)
	char **argv;
{
	unsigned n, num_entries;

	if (argc != 4) {
		fprintf(stderr, "usage: %s fw-image-file offset num-entries\n",
			argv[0]);
		exit(1);
	}
	open_and_seek_input(argv[1], argv[2]);
	num_entries = strtoul(argv[3], 0, 0);
	for (n = 0; n < num_entries; n++)
		process_record(n + 1);
	exit(0);
}