Commit 62788e0e authored by Philipp Schafft's avatar Philipp Schafft 🦁
Browse files

Feature: Added support for decoding picture tags

parent 99e77eff
/*
* Copyright (C) 2021 Philipp Schafft <lion@lion.leolix.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#ifndef __PICTURE_H__
#define __PICTURE_H__
#ifdef __cplusplus
extern "C" {
#endif
#include <stddef.h>
typedef enum {
FLAC_PICTURE_INVALID = -1,
FLAC_PICTURE_OTHER = 0,
FLAC_PICTURE_FILE_ICON = 1,
FLAC_PICTURE_OTHER_FILE_ICON = 2,
FLAC_PICTURE_COVER_FRONT = 3,
FLAC_PICTURE_COVER_BACK = 4,
FLAC_PICTURE_LEAFLET_PAGE = 5,
FLAC_PICTURE_MEDIA = 6,
FLAC_PICTURE_LEAD = 7,
FLAC_PICTURE_ARTIST = 8,
FLAC_PICTURE_CONDUCTOR = 9,
FLAC_PICTURE_BAND = 10,
FLAC_PICTURE_COMPOSER = 11,
FLAC_PICTURE_LYRICIST = 12,
FLAC_PICTURE_RECORDING_LOCATION = 13,
FLAC_PICTURE_DURING_RECORDING = 14,
FLAC_PICTURE_DURING_PREFORMANCE = 15,
FLAC_PICTURE_SCREEN_CAPTURE = 16,
FLAC_PICTURE_A_BRIGHT_COLOURED_FISH = 17,
FLAC_PICTURE_ILLUSTRATION = 18,
FLAC_PICTURE_BAND_LOGOTYPE = 19,
FLAC_PICTURE_PUBLISHER_LOGOTYPE = 20
} flac_picture_type;
typedef struct {
flac_picture_type type;
const char *media_type;
const char *description;
unsigned int width;
unsigned int height;
unsigned int depth;
unsigned int colors;
const void *binary;
size_t binary_length;
const char *uri;
void *private_data;
size_t private_data_length;
} flac_picture_t;
flac_picture_t * flac_picture_parse_from_base64(const char *str);
void flac_picture_free(flac_picture_t *picture);
#ifdef __cplusplus
}
#endif
#endif
......@@ -2,7 +2,7 @@
AM_CPPFLAGS = -I$(top_srcdir)/include
noinst_LIBRARIES = libutf8.a libgetopt.a libbase64.a
noinst_LIBRARIES = libutf8.a libgetopt.a libbase64.a libpicture.a
libutf8_a_SOURCES = charset.c charset.h iconvert.c utf8.c
......@@ -10,6 +10,9 @@ libgetopt_a_SOURCES = getopt.c getopt1.c
libbase64_a_SOURCES = base64.c
libpicture_a_SOURCES = picture.c
libpicture_a_LIBADD = libbase64.a
EXTRA_DIST = charmaps.h makemap.c charset_test.c charsetmap.h
debug:
......
/*
* Copyright (C) 2021 Philipp Schafft <lion@lion.leolix.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include "base64.h"
#include "picture.h"
static uint32_t read32be(unsigned char *buf)
{
uint32_t ret = 0;
ret = (ret << 8) | *buf++;
ret = (ret << 8) | *buf++;
ret = (ret << 8) | *buf++;
ret = (ret << 8) | *buf++;
return ret;
}
static flac_picture_t * flac_picture_parse_eat(void *data, size_t len)
{
size_t expected_length = 32; // 8*32 bit
size_t offset = 0;
flac_picture_t *ret;
uint32_t tmp;
if (len < expected_length)
return NULL;
ret = calloc(1, sizeof(*ret));
if (!ret)
return NULL;
ret->private_data = data;
ret->private_data_length = len;
ret->type = read32be(data);
/*
const char *media_type;
const char *description;
unsigned int width;
unsigned int height;
unsigned int depth;
unsigned int colors;
const void *binary;
size_t binary_length;
const char *uri;
*/
tmp = read32be(data+4);
expected_length += tmp;
if (len < expected_length) {
free(ret);
return NULL;
}
ret->media_type = data + 8;
offset = 8 + tmp;
tmp = read32be(data + offset);
expected_length += tmp;
if (len < expected_length) {
free(ret);
return NULL;
}
*(char*)(data + offset) = 0;
offset += 4;
ret->description = data + offset;
offset += tmp;
ret->width = read32be(data + offset);
*(char*)(data + offset) = 0;
offset += 4;
ret->height = read32be(data + offset);
offset += 4;
ret->depth = read32be(data + offset);
offset += 4;
ret->colors = read32be(data + offset);
offset += 4;
ret->binary_length = read32be(data + offset);
expected_length += ret->binary_length;
if (len < expected_length) {
free(ret);
return NULL;
}
offset += 4;
ret->binary = data + offset;
if (strcmp(ret->media_type, "-->") == 0) {
// Note: it is ensured ret->binary[ret->binary_length] == 0.
ret->media_type = NULL;
ret->uri = ret->binary;
ret->binary = NULL;
ret->binary_length = 0;
}
return ret;
}
flac_picture_t * flac_picture_parse_from_base64(const char *str)
{
flac_picture_t *ret;
void *data;
size_t len;
if (!str || !*str)
return NULL;
if (base64_decode(str, &data, &len) != 0)
return NULL;
ret = flac_picture_parse_eat(data, len);
if (!ret) {
free(data);
return NULL;
}
return ret;
}
void flac_picture_free(flac_picture_t *picture)
{
if (!picture)
return;
free(picture->private_data);
free(picture);
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment