shithub: libtags

ref: 2c7c27be5decdf67bd0770b2ddce538e5d584739
dir: /examples/readtags.c/

View raw version
#include <fcntl.h>
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#define USED(x) (void)x
#include "tags.h"

typedef struct Aux Aux;

struct Aux {
	int fd;
};

static const char *t2s[] = {
	[Talbum] = "album",
	[Talbumgain] = "albumgain",
	[Talbumpeak] = "albumpeak",
	[Tartist] = "artist",
	[Tcomment] = "comment",
	[Tcomposer] = "composer",
	[Tdate] = "date",
	[Tgenre] = "genre",
	[Timage] = "image",
	[Ttitle] = "title",
	[Ttrack] = "track",
	[Ttrackgain] = "trackgain",
	[Ttrackpeak] = "trackpeak",
};

static void
tag(Tagctx *ctx, int t, const char *k, const char *v, int offset, int size, Tagread f)
{
	USED(ctx); USED(k); USED(f);
	if(t == Timage)
		printf("%-12s %s %d %d\n", t2s[t], v, offset, size);
	else if(t == Tunknown)
		printf("%-12s %s\n", k, v);
	else
		printf("%-12s %s\n", t2s[t], v);
}

static void
toc(Tagctx *ctx, int ms, int offset)
{
	USED(ctx); USED(ms); USED(offset);
}

static int
ctxread(Tagctx *ctx, void *buf, int cnt)
{
	Aux *aux = ctx->aux;
	return read(aux->fd, buf, cnt);
}

static int
ctxseek(Tagctx *ctx, int offset, int whence)
{
	Aux *aux = ctx->aux;
	return lseek(aux->fd, offset, whence);
}

int
main(int argc, char **argv)
{
	char buf[256];
	Aux aux;
	Tagctx ctx = {
		.read = ctxread,
		.seek = ctxseek,
		.tag = tag,
		.toc = toc,
		.buf = buf,
		.bufsz = sizeof(buf),
		.aux = &aux,
	};

	if(argc < 2){
		printf("usage: readtags FILE...\n");
		return 1;
	}

#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
	// this block is for fuzzing only, ignore
	while(__AFL_LOOP(10000)){
		if((aux.fd = open(argv[1], O_RDONLY)) >= 0){
			tagsget(&ctx);
			close(aux.fd);
		}
	}
#else
	int i;
	for(i = 1; i < argc; i++){
		printf("*** %s\n", argv[i]);
		if((aux.fd = open(argv[i], O_RDONLY)) < 0)
			perror("failed to open");
		else{
			if(tagsget(&ctx) != 0)
				printf("no tags or failed to read tags\n");
			else{
				if(ctx.duration > 0)
					printf("%-12s %d ms\n", "duration", ctx.duration);
				if(ctx.samplerate > 0)
					printf("%-12s %d\n", "samplerate", ctx.samplerate);
				if(ctx.channels > 0)
					printf("%-12s %d\n", "channels", ctx.channels);
				if(ctx.bitrate > 0)
					printf("%-12s %d\n", "bitrate", ctx.bitrate);
			}
			close(aux.fd);
		}
		printf("\n");
	}
#endif
	return 0;
}