Blame gst-libs/gst/tag/id3v2frames.c

Packit 971217
/* -*- Mode: C; tab-width: 2; indent-tabs-mode: t; c-basic-offset: 2 -*- */
Packit 971217
/* Copyright 2006-2008 Tim-Philipp Müller <tim centricular net>
Packit 971217
 * Copyright 2005 Jan Schmidt <thaytan@mad.scientist.com>
Packit 971217
 * Copyright 2002,2003 Scott Wheeler <wheeler@kde.org> (portions from taglib)
Packit 971217
 *
Packit 971217
 * This library is free software; you can redistribute it and/or
Packit 971217
 * modify it under the terms of the GNU Library General Public
Packit 971217
 * License as published by the Free Software Foundation; either
Packit 971217
 * version 2 of the License, or (at your option) any later version.
Packit 971217
 *
Packit 971217
 * This library is distributed in the hope that it will be useful,
Packit 971217
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 971217
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 971217
 * Library General Public License for more details.
Packit 971217
 *
Packit 971217
 * You should have received a copy of the GNU Library General Public
Packit 971217
 * License along with this library; if not, write to the
Packit 971217
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
Packit 971217
 * Boston, MA 02110-1301, USA.
Packit 971217
 */
Packit 971217
Packit 971217
#ifdef HAVE_CONFIG_H
Packit 971217
#include "config.h"
Packit 971217
#endif
Packit 971217
Packit 971217
#include <string.h>
Packit 971217
#include <stdio.h>
Packit 971217
#include <stdlib.h>
Packit 971217
#include <gst/tag/tag.h>
Packit 971217
#include <gst/base/gsttypefindhelper.h>
Packit 971217
Packit 971217
#ifdef HAVE_ZLIB
Packit 971217
#include <zlib.h>
Packit 971217
#endif
Packit 971217
Packit 971217
#include "id3v2.h"
Packit 971217
Packit 971217
#ifndef GST_DISABLE_GST_DEBUG
Packit 971217
#define GST_CAT_DEFAULT id3v2_ensure_debug_category()
Packit 971217
#endif
Packit 971217
Packit 971217
static gboolean parse_comment_frame (ID3TagsWorking * work);
Packit 971217
static gchar *parse_url_link_frame (ID3TagsWorking * work,
Packit 971217
    const gchar ** tag_name);
Packit 971217
static GArray *parse_text_identification_frame (ID3TagsWorking * work);
Packit 971217
static gchar *parse_user_text_identification_frame (ID3TagsWorking * work,
Packit 971217
    const gchar ** tag_name);
Packit 971217
static gchar *parse_unique_file_identifier (ID3TagsWorking * work,
Packit 971217
    const gchar ** tag_name);
Packit 971217
static gboolean parse_relative_volume_adjustment_two (ID3TagsWorking * work);
Packit 971217
static void parse_obsolete_tdat_frame (ID3TagsWorking * work);
Packit 971217
static gboolean id3v2_tag_to_taglist (ID3TagsWorking * work,
Packit 971217
    const gchar * tag_name, const gchar * tag_str);
Packit 971217
/* Parse a single string into an array of gchar* */
Packit 971217
static void parse_split_strings (guint8 encoding, gchar * data, gint data_size,
Packit 971217
    GArray ** out_fields);
Packit 971217
static void free_tag_strings (GArray * fields);
Packit 971217
static gboolean
Packit 971217
id3v2_genre_fields_to_taglist (ID3TagsWorking * work, const gchar * tag_name,
Packit 971217
    GArray * tag_fields);
Packit 971217
static gboolean parse_picture_frame (ID3TagsWorking * work);
Packit 971217
static gboolean parse_private_frame_data (ID3TagsWorking * work);
Packit 971217
Packit 971217
#define ID3V2_ENCODING_ISO8859 0x00
Packit 971217
#define ID3V2_ENCODING_UTF16   0x01
Packit 971217
#define ID3V2_ENCODING_UTF16BE 0x02
Packit 971217
#define ID3V2_ENCODING_UTF8    0x03
Packit 971217
Packit 971217
gboolean
Packit 971217
id3v2_parse_frame (ID3TagsWorking * work)
Packit 971217
{
Packit 971217
  const gchar *tag_name;
Packit 971217
  gboolean result = FALSE;
Packit 971217
  gint i;
Packit 971217
  guint8 *frame_data = work->hdr.frame_data;
Packit 971217
  guint frame_data_size = work->cur_frame_size;
Packit 971217
  gchar *tag_str = NULL;
Packit 971217
  GArray *tag_fields = NULL;
Packit 971217
  guint8 *uu_data = NULL;
Packit 971217
Packit 971217
#ifdef HAVE_ZLIB
Packit 971217
  guint8 *uncompressed_data = NULL;
Packit 971217
#endif
Packit 971217
Packit 971217
  /* Check that the frame id is valid */
Packit 971217
  for (i = 0; i < 5 && work->frame_id[i] != '\0'; i++) {
Packit 971217
    if (!g_ascii_isalnum (work->frame_id[i])) {
Packit 971217
      GST_DEBUG ("Encountered invalid frame_id");
Packit 971217
      return FALSE;
Packit 971217
    }
Packit 971217
  }
Packit 971217
Packit 971217
  /* Can't handle encrypted frames right now (in case we ever do, we'll have
Packit 971217
   * to do the decryption after the un-unsynchronisation and decompression,
Packit 971217
   * not here) */
Packit 971217
  if (work->frame_flags & ID3V2_FRAME_FORMAT_ENCRYPTION) {
Packit 971217
    GST_WARNING ("Encrypted frames are not supported");
Packit 971217
    return FALSE;
Packit 971217
  }
Packit 971217
Packit 971217
  tag_name = gst_tag_from_id3_tag (work->frame_id);
Packit 971217
  if (tag_name == NULL &&
Packit 971217
      strncmp (work->frame_id, "RVA2", 4) != 0 &&
Packit 971217
      strncmp (work->frame_id, "TXXX", 4) != 0 &&
Packit 971217
      strncmp (work->frame_id, "TDAT", 4) != 0 &&
Packit 971217
      strncmp (work->frame_id, "UFID", 4) != 0) {
Packit 971217
    return FALSE;
Packit 971217
  }
Packit 971217
Packit 971217
  if (work->frame_flags & (ID3V2_FRAME_FORMAT_COMPRESSION |
Packit 971217
          ID3V2_FRAME_FORMAT_DATA_LENGTH_INDICATOR)) {
Packit 971217
    if (work->hdr.frame_data_size <= 4)
Packit 971217
      return FALSE;
Packit 971217
    if (ID3V2_VER_MAJOR (work->hdr.version) == 3) {
Packit 971217
      work->parse_size = GST_READ_UINT32_BE (frame_data);
Packit 971217
    } else {
Packit 971217
      work->parse_size = id3v2_read_synch_uint (frame_data, 4);
Packit 971217
    }
Packit 971217
    frame_data += 4;
Packit 971217
    frame_data_size -= 4;
Packit 971217
    GST_LOG ("Un-unsynced data size %d (of %d)", work->parse_size,
Packit 971217
        frame_data_size);
Packit 971217
    if (work->parse_size > frame_data_size) {
Packit 971217
      GST_WARNING ("ID3v2 frame %s data has invalid size %d (>%d)",
Packit 971217
          work->frame_id, work->parse_size, frame_data_size);
Packit 971217
      return FALSE;
Packit 971217
    }
Packit 971217
  }
Packit 971217
Packit 971217
  /* in v2.3 the frame sizes are not syncsafe, so the entire tag had to be
Packit 971217
   * unsynced. In v2.4 the frame sizes are syncsafe so it's just the frame
Packit 971217
   * data that needs un-unsyncing, but not the frame headers. */
Packit 971217
  if (ID3V2_VER_MAJOR (work->hdr.version) == 4) {
Packit 971217
    if ((work->hdr.flags & ID3V2_HDR_FLAG_UNSYNC) != 0 ||
Packit 971217
        ((work->frame_flags & ID3V2_FRAME_FORMAT_UNSYNCHRONISATION) != 0)) {
Packit 971217
      GST_DEBUG ("Un-unsyncing frame %s", work->frame_id);
Packit 971217
      uu_data = id3v2_ununsync_data (frame_data, &frame_data_size);
Packit 971217
      frame_data = uu_data;
Packit 971217
      GST_MEMDUMP ("ID3v2 frame (un-unsyced)", frame_data, frame_data_size);
Packit 971217
    }
Packit 971217
  }
Packit 971217
Packit 971217
  work->parse_size = frame_data_size;
Packit 971217
Packit 971217
  if (work->frame_flags & ID3V2_FRAME_FORMAT_COMPRESSION) {
Packit 971217
#ifdef HAVE_ZLIB
Packit 971217
    uLongf destSize = work->parse_size;
Packit 971217
    Bytef *dest, *src;
Packit 971217
Packit 971217
    uncompressed_data = g_malloc (work->parse_size);
Packit 971217
Packit 971217
    dest = (Bytef *) uncompressed_data;
Packit 971217
    src = (Bytef *) frame_data;
Packit 971217
Packit 971217
    if (uncompress (dest, &destSize, src, frame_data_size) != Z_OK) {
Packit 971217
      g_free (uncompressed_data);
Packit 971217
      g_free (uu_data);
Packit 971217
      return FALSE;
Packit 971217
    }
Packit 971217
    if (destSize != work->parse_size) {
Packit 971217
      GST_WARNING
Packit 971217
          ("Decompressing ID3v2 frame %s did not produce expected size %d bytes (got %lu)",
Packit 971217
          tag_name, work->parse_size, destSize);
Packit 971217
      g_free (uncompressed_data);
Packit 971217
      g_free (uu_data);
Packit 971217
      return FALSE;
Packit 971217
    }
Packit 971217
    work->parse_data = uncompressed_data;
Packit 971217
#else
Packit 971217
    GST_WARNING ("Compressed ID3v2 tag frame could not be decompressed, because"
Packit 971217
        " libgsttag-" GST_API_VERSION " was compiled without zlib support");
Packit 971217
    g_free (uu_data);
Packit 971217
    return FALSE;
Packit 971217
#endif
Packit 971217
  } else {
Packit 971217
    work->parse_data = frame_data;
Packit 971217
  }
Packit 971217
Packit 971217
  if (work->frame_id[0] == 'T') {
Packit 971217
    if (strcmp (work->frame_id, "TDAT") == 0) {
Packit 971217
      parse_obsolete_tdat_frame (work);
Packit 971217
      result = TRUE;
Packit 971217
    } else if (strcmp (work->frame_id, "TXXX") == 0) {
Packit 971217
      /* Handle user text frame */
Packit 971217
      tag_str = parse_user_text_identification_frame (work, &tag_name);
Packit 971217
    } else {
Packit 971217
      /* Text identification frame */
Packit 971217
      tag_fields = parse_text_identification_frame (work);
Packit 971217
    }
Packit 971217
  } else if (work->frame_id[0] == 'W' && strcmp (work->frame_id, "WXXX") != 0) {
Packit 971217
    /* URL link frame: ISO-8859-1 encoded, one frame per tag */
Packit 971217
    tag_str = parse_url_link_frame (work, &tag_name);
Packit 971217
  } else if (!strcmp (work->frame_id, "COMM")) {
Packit 971217
    /* Comment */
Packit 971217
    result = parse_comment_frame (work);
Packit 971217
  } else if (!strcmp (work->frame_id, "APIC")) {
Packit 971217
    /* Attached picture */
Packit 971217
    result = parse_picture_frame (work);
Packit 971217
  } else if (!strcmp (work->frame_id, "RVA2")) {
Packit 971217
    /* Relative volume */
Packit 971217
    result = parse_relative_volume_adjustment_two (work);
Packit 971217
  } else if (!strcmp (work->frame_id, "UFID")) {
Packit 971217
    /* Unique file identifier */
Packit 971217
    tag_str = parse_unique_file_identifier (work, &tag_name);
Packit 971217
  } else if (!strcmp (work->frame_id, "PRIV")) {
Packit 971217
    /* private frame */
Packit 971217
    result = parse_private_frame_data (work);
Packit 971217
  }
Packit 971217
#ifdef HAVE_ZLIB
Packit 971217
  if (work->frame_flags & ID3V2_FRAME_FORMAT_COMPRESSION) {
Packit 971217
    g_free (uncompressed_data);
Packit 971217
    uncompressed_data = NULL;
Packit 971217
    work->parse_data = frame_data;
Packit 971217
  }
Packit 971217
#endif
Packit 971217
Packit 971217
  if (tag_str != NULL) {
Packit 971217
    /* g_print ("Tag %s value %s\n", tag_name, tag_str); */
Packit 971217
    result = id3v2_tag_to_taglist (work, tag_name, tag_str);
Packit 971217
    g_free (tag_str);
Packit 971217
  }
Packit 971217
  if (tag_fields != NULL) {
Packit 971217
    if (strcmp (work->frame_id, "TCON") == 0) {
Packit 971217
      /* Genre strings need special treatment */
Packit 971217
      result |= id3v2_genre_fields_to_taglist (work, tag_name, tag_fields);
Packit 971217
    } else {
Packit 971217
      gint t;
Packit 971217
Packit 971217
      for (t = 0; t < tag_fields->len; t++) {
Packit 971217
        tag_str = g_array_index (tag_fields, gchar *, t);
Packit 971217
        if (tag_str != NULL && tag_str[0] != '\0')
Packit 971217
          result |= id3v2_tag_to_taglist (work, tag_name, tag_str);
Packit 971217
      }
Packit 971217
    }
Packit 971217
    free_tag_strings (tag_fields);
Packit 971217
  }
Packit 971217
Packit 971217
  g_free (uu_data);
Packit 971217
Packit 971217
  return result;
Packit 971217
}
Packit 971217
Packit 971217
static gboolean
Packit 971217
parse_comment_frame (ID3TagsWorking * work)
Packit 971217
{
Packit 971217
  guint dummy;
Packit 971217
  guint8 encoding;
Packit 971217
  gchar language[4];
Packit 971217
  GArray *fields = NULL;
Packit 971217
  gchar *description, *text;
Packit 971217
Packit 971217
  if (work->parse_size < 6)
Packit 971217
    return FALSE;
Packit 971217
Packit 971217
  encoding = work->parse_data[0];
Packit 971217
  language[0] = g_ascii_tolower (work->parse_data[1]);
Packit 971217
  language[1] = g_ascii_tolower (work->parse_data[2]);
Packit 971217
  language[2] = g_ascii_tolower (work->parse_data[3]);
Packit 971217
  language[3] = '\0';
Packit 971217
Packit 971217
  parse_split_strings (encoding, (gchar *) work->parse_data + 4,
Packit 971217
      work->parse_size - 4, &fields);
Packit 971217
Packit 971217
  if (fields == NULL || fields->len < 2) {
Packit 971217
    GST_WARNING ("Failed to decode comment frame");
Packit 971217
    goto fail;
Packit 971217
  }
Packit 971217
  description = g_array_index (fields, gchar *, 0);
Packit 971217
  text = g_array_index (fields, gchar *, 1);
Packit 971217
Packit 971217
  if (!g_utf8_validate (text, -1, NULL)) {
Packit 971217
    GST_WARNING ("Converted string is not valid utf-8");
Packit 971217
    goto fail;
Packit 971217
  }
Packit 971217
Packit 971217
  /* skip our own dummy descriptions (from id3v2mux) */
Packit 971217
  if (strlen (description) > 0 && g_utf8_validate (description, -1, NULL) &&
Packit 971217
      sscanf (description, "c%u", &dummy) != 1) {
Packit 971217
    gchar *s;
Packit 971217
Packit 971217
    /* must be either an ISO-639-1 or ISO-639-2 language code */
Packit 971217
    if (language[0] != '\0' &&
Packit 971217
        g_ascii_isalpha (language[0]) &&
Packit 971217
        g_ascii_isalpha (language[1]) &&
Packit 971217
        (g_ascii_isalpha (language[2]) || language[2] == '\0')) {
Packit 971217
      const gchar *lang_code;
Packit 971217
Packit 971217
      /* prefer two-letter ISO 639-1 code if we have a mapping */
Packit 971217
      lang_code = gst_tag_get_language_code (language);
Packit 971217
      s = g_strdup_printf ("%s[%s]=%s", description,
Packit 971217
          (lang_code) ? lang_code : language, text);
Packit 971217
    } else {
Packit 971217
      s = g_strdup_printf ("%s=%s", description, text);
Packit 971217
    }
Packit 971217
    gst_tag_list_add (work->tags, GST_TAG_MERGE_APPEND,
Packit 971217
        GST_TAG_EXTENDED_COMMENT, s, NULL);
Packit 971217
    g_free (s);
Packit 971217
  } else if (text != NULL && *text != '\0') {
Packit 971217
    gst_tag_list_add (work->tags, GST_TAG_MERGE_APPEND,
Packit 971217
        GST_TAG_COMMENT, text, NULL);
Packit 971217
  } else {
Packit 971217
    goto fail;
Packit 971217
  }
Packit 971217
Packit 971217
  free_tag_strings (fields);
Packit 971217
  return TRUE;
Packit 971217
Packit 971217
fail:
Packit 971217
  {
Packit 971217
    GST_WARNING ("failed to parse COMM frame");
Packit 971217
    free_tag_strings (fields);
Packit 971217
    return FALSE;
Packit 971217
  }
Packit 971217
}
Packit 971217
Packit 971217
static GArray *
Packit 971217
parse_text_identification_frame (ID3TagsWorking * work)
Packit 971217
{
Packit 971217
  guchar encoding;
Packit 971217
  GArray *fields = NULL;
Packit 971217
Packit 971217
  if (work->parse_size < 2)
Packit 971217
    return NULL;
Packit 971217
Packit 971217
  encoding = work->parse_data[0];
Packit 971217
  parse_split_strings (encoding, (gchar *) work->parse_data + 1,
Packit 971217
      work->parse_size - 1, &fields);
Packit 971217
  if (fields) {
Packit 971217
    if (fields->len > 0) {
Packit 971217
      GST_LOG ("Read %d fields from Text ID frame of size %d with encoding %d"
Packit 971217
          ". First is '%s'", fields->len, work->parse_size - 1, encoding,
Packit 971217
          g_array_index (fields, gchar *, 0));
Packit 971217
    } else {
Packit 971217
      GST_LOG ("Read 0 fields from Text ID frame of size %d with encoding %d",
Packit 971217
          work->parse_size - 1, encoding);
Packit 971217
    }
Packit 971217
  }
Packit 971217
Packit 971217
  return fields;
Packit 971217
}
Packit 971217
Packit 971217
static gboolean
Packit 971217
link_is_known_license (const gchar * url)
Packit 971217
{
Packit 971217
  return g_str_has_prefix (url, "http://creativecommons.org/licenses/");
Packit 971217
}
Packit 971217
Packit 971217
static gchar *
Packit 971217
parse_url_link_frame (ID3TagsWorking * work, const gchar ** tag_name)
Packit 971217
{
Packit 971217
  gsize len;
Packit 971217
  gchar *nul, *data, *link;
Packit 971217
Packit 971217
  *tag_name = NULL;
Packit 971217
Packit 971217
  if (work->parse_size == 0)
Packit 971217
    return NULL;
Packit 971217
Packit 971217
  data = (gchar *) work->parse_data;
Packit 971217
  /* if there's more data then the string is long, we only want to parse the
Packit 971217
   * data up to the terminating zero to g_convert and ignore the rest, as
Packit 971217
   * per spec */
Packit 971217
  nul = memchr (data, '\0', work->parse_size);
Packit 971217
  if (nul != NULL) {
Packit 971217
    len = (gsize) (nul - data);
Packit 971217
  } else {
Packit 971217
    len = work->parse_size;
Packit 971217
  }
Packit 971217
Packit 971217
  link = g_convert (data, len, "UTF-8", "ISO-8859-1", NULL, NULL, NULL);
Packit 971217
Packit 971217
  if (link == NULL || !gst_uri_is_valid (link)) {
Packit 971217
    GST_DEBUG ("Invalid URI in %s frame: %s", work->frame_id,
Packit 971217
        GST_STR_NULL (link));
Packit 971217
    g_free (link);
Packit 971217
    return NULL;
Packit 971217
  }
Packit 971217
Packit 971217
  /* we don't know if it's a link to a page that explains the copyright
Packit 971217
   * situation, or a link that points to/represents a license, the ID3 spec
Packit 971217
   * does not separate those two things; for now only put known license URIs
Packit 971217
   * into GST_TAG_LICENSE_URI and everything else into GST_TAG_COPYRIGHT_URI */
Packit 971217
  if (strcmp (work->frame_id, "WCOP") == 0) {
Packit 971217
    if (link_is_known_license (link))
Packit 971217
      *tag_name = GST_TAG_LICENSE_URI;
Packit 971217
    else
Packit 971217
      *tag_name = GST_TAG_COPYRIGHT_URI;
Packit 971217
  } else if (strcmp (work->frame_id, "WOAF") == 0) {
Packit 971217
    /* can't be bothered to create a CONTACT_URI tag for this, so let's just
Packit 971217
     * put into into GST_TAG_CONTACT, which is where it ends up when reading
Packit 971217
     * the info from vorbis comments as well */
Packit 971217
    *tag_name = GST_TAG_CONTACT;
Packit 971217
  }
Packit 971217
Packit 971217
  return link;
Packit 971217
}
Packit 971217
Packit 971217
Packit 971217
static gchar *
Packit 971217
parse_user_text_identification_frame (ID3TagsWorking * work,
Packit 971217
    const gchar ** tag_name)
Packit 971217
{
Packit 971217
  gchar *ret;
Packit 971217
  guchar encoding;
Packit 971217
  GArray *fields = NULL;
Packit 971217
Packit 971217
  *tag_name = NULL;
Packit 971217
Packit 971217
  if (work->parse_size < 2)
Packit 971217
    return NULL;
Packit 971217
Packit 971217
  encoding = work->parse_data[0];
Packit 971217
Packit 971217
  parse_split_strings (encoding, (gchar *) work->parse_data + 1,
Packit 971217
      work->parse_size - 1, &fields);
Packit 971217
Packit 971217
  if (fields == NULL)
Packit 971217
    return NULL;
Packit 971217
Packit 971217
  if (fields->len != 2) {
Packit 971217
    GST_WARNING ("Expected 2 fields in TXXX frame, but got %d", fields->len);
Packit 971217
    free_tag_strings (fields);
Packit 971217
    return NULL;
Packit 971217
  }
Packit 971217
Packit 971217
  *tag_name =
Packit 971217
      gst_tag_from_id3_user_tag ("TXXX", g_array_index (fields, gchar *, 0));
Packit 971217
Packit 971217
  GST_LOG ("TXXX frame of size %d. Mapped descriptor '%s' to GStreamer tag %s",
Packit 971217
      work->parse_size - 1, g_array_index (fields, gchar *, 0),
Packit 971217
      GST_STR_NULL (*tag_name));
Packit 971217
Packit 971217
  if (*tag_name) {
Packit 971217
    ret = g_strdup (g_array_index (fields, gchar *, 1));
Packit 971217
    /* GST_LOG ("%s = %s", *tag_name, GST_STR_NULL (ret)); */
Packit 971217
  } else {
Packit 971217
    ret = NULL;
Packit 971217
  }
Packit 971217
Packit 971217
  free_tag_strings (fields);
Packit 971217
  return ret;
Packit 971217
}
Packit 971217
Packit 971217
static gboolean
Packit 971217
parse_id_string (ID3TagsWorking * work, gchar ** p_str, gint * p_len,
Packit 971217
    gint * p_datalen)
Packit 971217
{
Packit 971217
  gint len, datalen;
Packit 971217
Packit 971217
  if (work->parse_size < 2)
Packit 971217
    return FALSE;
Packit 971217
Packit 971217
  for (len = 0; len < work->parse_size - 1; ++len) {
Packit 971217
    if (work->parse_data[len] == '\0')
Packit 971217
      break;
Packit 971217
  }
Packit 971217
Packit 971217
  datalen = work->parse_size - (len + 1);
Packit 971217
  if (len == 0 || datalen <= 0)
Packit 971217
    return FALSE;
Packit 971217
Packit 971217
  *p_str = g_strndup ((gchar *) work->parse_data, len);
Packit 971217
  *p_len = len;
Packit 971217
  *p_datalen = datalen;
Packit 971217
Packit 971217
  return TRUE;
Packit 971217
}
Packit 971217
Packit 971217
static gboolean
Packit 971217
parse_private_frame_data (ID3TagsWorking * work)
Packit 971217
{
Packit 971217
  GstBuffer *binary_data = NULL;
Packit 971217
  GstStructure *owner_info = NULL;
Packit 971217
  guint8 *owner_str = NULL;
Packit 971217
  gsize owner_len;
Packit 971217
  GstSample *priv_frame = NULL;
Packit 971217
Packit 971217
  if (work->parse_size == 0) {
Packit 971217
    /* private frame data not available */
Packit 971217
    return FALSE;
Packit 971217
  }
Packit 971217
Packit 971217
  owner_str =
Packit 971217
      (guint8 *) memchr ((guint8 *) work->parse_data, 0, work->parse_size);
Packit 971217
Packit 971217
  if (owner_str == NULL) {
Packit 971217
    GST_WARNING ("Invalid PRIV frame received");
Packit 971217
    return FALSE;
Packit 971217
  }
Packit 971217
Packit 971217
  owner_len = (gsize) (owner_str - work->parse_data) + 1;
Packit 971217
Packit 971217
  owner_info =
Packit 971217
      gst_structure_new ("ID3PrivateFrame", "owner", G_TYPE_STRING,
Packit 971217
      work->parse_data, NULL);
Packit 971217
Packit 971217
  binary_data = gst_buffer_new_and_alloc (work->parse_size - owner_len);
Packit 971217
  gst_buffer_fill (binary_data, 0, work->parse_data + owner_len,
Packit 971217
      work->parse_size - owner_len);
Packit 971217
Packit 971217
  priv_frame = gst_sample_new (binary_data, NULL, NULL, owner_info);
Packit 971217
Packit 971217
  gst_tag_list_add (work->tags, GST_TAG_MERGE_APPEND,
Packit 971217
      GST_TAG_PRIVATE_DATA, priv_frame, NULL);
Packit 971217
Packit 971217
  gst_sample_unref (priv_frame);
Packit 971217
  gst_buffer_unref (binary_data);
Packit 971217
Packit 971217
  return TRUE;
Packit 971217
}
Packit 971217
Packit 971217
static gchar *
Packit 971217
parse_unique_file_identifier (ID3TagsWorking * work, const gchar ** tag_name)
Packit 971217
{
Packit 971217
  gint len, datalen;
Packit 971217
  gchar *owner_id, *data, *ret = NULL;
Packit 971217
Packit 971217
  GST_LOG ("parsing UFID frame of size %d", work->parse_size);
Packit 971217
Packit 971217
  if (!parse_id_string (work, &owner_id, &len, &datalen))
Packit 971217
    return NULL;
Packit 971217
Packit 971217
  data = (gchar *) work->parse_data + len + 1;
Packit 971217
  GST_LOG ("UFID owner ID: %s (+ %d bytes of data)", owner_id, datalen);
Packit 971217
Packit 971217
  if (strcmp (owner_id, "http://musicbrainz.org") == 0 &&
Packit 971217
      g_utf8_validate (data, datalen, NULL)) {
Packit 971217
    *tag_name = GST_TAG_MUSICBRAINZ_TRACKID;
Packit 971217
    ret = g_strndup (data, datalen);
Packit 971217
  } else {
Packit 971217
    GST_INFO ("Unknown UFID owner ID: %s", owner_id);
Packit 971217
  }
Packit 971217
  g_free (owner_id);
Packit 971217
Packit 971217
  return ret;
Packit 971217
}
Packit 971217
Packit 971217
/* parse data and return length of the next string in the given encoding,
Packit 971217
 * including the NUL terminator */
Packit 971217
static gint
Packit 971217
scan_encoded_string (guint8 encoding, gchar * data, gint data_size)
Packit 971217
{
Packit 971217
  gint i;
Packit 971217
Packit 971217
  switch (encoding) {
Packit 971217
    case ID3V2_ENCODING_ISO8859:
Packit 971217
    case ID3V2_ENCODING_UTF8:
Packit 971217
      for (i = 0; i < data_size; ++i) {
Packit 971217
        if (data[i] == '\0')
Packit 971217
          return i + 1;
Packit 971217
      }
Packit 971217
      break;
Packit 971217
    case ID3V2_ENCODING_UTF16:
Packit 971217
    case ID3V2_ENCODING_UTF16BE:
Packit 971217
      /* we don't care about BOMs here and treat them as part of the string */
Packit 971217
      /* Find '\0\0' terminator */
Packit 971217
      for (i = 0; i < data_size - 1; i += 2) {
Packit 971217
        if (data[i] == '\0' && data[i + 1] == '\0')
Packit 971217
          return i + 2;
Packit 971217
      }
Packit 971217
      break;
Packit 971217
    default:
Packit 971217
      break;
Packit 971217
  }
Packit 971217
Packit 971217
  return 0;
Packit 971217
}
Packit 971217
Packit 971217
static gboolean
Packit 971217
parse_picture_frame (ID3TagsWorking * work)
Packit 971217
{
Packit 971217
  guint8 txt_encoding, pic_type;
Packit 971217
  gchar *mime_str = NULL;
Packit 971217
  gint len, datalen;
Packit 971217
Packit 971217
  GST_LOG ("APIC frame (ID3v2.%u)", ID3V2_VER_MAJOR (work->hdr.version));
Packit 971217
Packit 971217
  if (work->parse_size < 1 + 1 + 1 + 1 + 1)
Packit 971217
    goto not_enough_data;
Packit 971217
Packit 971217
  txt_encoding = work->parse_data[0];
Packit 971217
  ++work->parse_data;
Packit 971217
  --work->parse_size;
Packit 971217
Packit 971217
  /* Read image format; in early ID3v2 versions this is a fixed-length
Packit 971217
   * 3-character string without terminator; in later versions (>= 2.3.0)
Packit 971217
   * this is a NUL-terminated string of variable length */
Packit 971217
  if (ID3V2_VER_MAJOR (work->hdr.version) < 3) {
Packit 971217
    if (work->parse_size < 3)
Packit 971217
      goto not_enough_data;
Packit 971217
Packit 971217
    mime_str = g_strndup ((gchar *) work->parse_data, 3);
Packit 971217
    len = 3;
Packit 971217
  } else {
Packit 971217
    if (!parse_id_string (work, &mime_str, &len, &datalen))
Packit 971217
      return FALSE;
Packit 971217
    ++len;                      /* for string terminator */
Packit 971217
  }
Packit 971217
Packit 971217
  if (work->parse_size < len + 1 + 1 + 1)
Packit 971217
    goto not_enough_data;
Packit 971217
Packit 971217
  work->parse_data += len;
Packit 971217
  work->parse_size -= len;
Packit 971217
Packit 971217
  /* Read image type */
Packit 971217
  pic_type = work->parse_data[0];
Packit 971217
  ++work->parse_data;
Packit 971217
  --work->parse_size;
Packit 971217
Packit 971217
  GST_LOG ("APIC frame mime type    : %s", GST_STR_NULL (mime_str));
Packit 971217
  GST_LOG ("APIC frame picture type : 0x%02x", (guint) pic_type);
Packit 971217
Packit 971217
  if (work->parse_size < 1 + 1)
Packit 971217
    goto not_enough_data;
Packit 971217
Packit 971217
  len = scan_encoded_string (txt_encoding, (gchar *) work->parse_data,
Packit 971217
      work->parse_size);
Packit 971217
Packit 971217
  if (len < 1)
Packit 971217
    goto error;
Packit 971217
Packit 971217
  /* just skip the description string ... */
Packit 971217
  GST_LOG ("Skipping description string (%d bytes in original coding)", len);
Packit 971217
Packit 971217
  if (work->parse_size < len + 1)
Packit 971217
    goto not_enough_data;
Packit 971217
Packit 971217
  work->parse_data += len;
Packit 971217
  work->parse_size -= len;
Packit 971217
Packit 971217
  GST_DEBUG ("image data is %u bytes", work->parse_size);
Packit 971217
Packit 971217
  if (work->parse_size <= 0)
Packit 971217
    goto not_enough_data;
Packit 971217
Packit 971217
  if (!gst_tag_list_add_id3_image (work->tags, (guint8 *) work->parse_data,
Packit 971217
          work->parse_size, pic_type)) {
Packit 971217
    goto error;
Packit 971217
  }
Packit 971217
Packit 971217
  g_free (mime_str);
Packit 971217
  return TRUE;
Packit 971217
Packit 971217
not_enough_data:
Packit 971217
  {
Packit 971217
    GST_DEBUG ("not enough data, skipping APIC frame");
Packit 971217
    /* fall through to error */
Packit 971217
  }
Packit 971217
error:
Packit 971217
  {
Packit 971217
    GST_DEBUG ("problem parsing APIC frame, skipping");
Packit 971217
    g_free (mime_str);
Packit 971217
    return FALSE;
Packit 971217
  }
Packit 971217
}
Packit 971217
Packit 971217
#define ID3V2_RVA2_CHANNEL_MASTER  1
Packit 971217
Packit 971217
static gboolean
Packit 971217
parse_relative_volume_adjustment_two (ID3TagsWorking * work)
Packit 971217
{
Packit 971217
  const gchar *gain_tag_name = NULL;
Packit 971217
  const gchar *peak_tag_name = NULL;
Packit 971217
  gdouble gain_dB, peak_val;
Packit 971217
  guint64 peak;
Packit 971217
  guint8 *data, chan, peak_bits;
Packit 971217
  gchar *id;
Packit 971217
  gint len, datalen, i;
Packit 971217
Packit 971217
  if (!parse_id_string (work, &id, &len, &datalen))
Packit 971217
    return FALSE;
Packit 971217
Packit 971217
  if (datalen < (1 + 2 + 1)) {
Packit 971217
    GST_WARNING ("broken RVA2 frame, data size only %d bytes", datalen);
Packit 971217
    g_free (id);
Packit 971217
    return FALSE;
Packit 971217
  }
Packit 971217
Packit 971217
  data = work->parse_data + len + 1;
Packit 971217
  chan = GST_READ_UINT8 (data);
Packit 971217
  gain_dB = (gdouble) ((gint16) GST_READ_UINT16_BE (data + 1)) / 512.0;
Packit 971217
  /* The meaning of the peak value is not defined in the ID3v2 spec. However,
Packit 971217
   * the first/only implementation of this seems to have been in XMMS, and
Packit 971217
   * other libs (like mutagen) seem to follow that implementation as well:
Packit 971217
   * see http://bugs.xmms.org/attachment.cgi?id=113&action=view */
Packit 971217
  peak_bits = GST_READ_UINT8 (data + 1 + 2);
Packit 971217
  if (peak_bits > 64) {
Packit 971217
    GST_WARNING ("silly peak precision of %d bits, ignoring", (gint) peak_bits);
Packit 971217
    peak_bits = 0;
Packit 971217
  }
Packit 971217
  data += 1 + 2 + 1;
Packit 971217
  datalen -= 1 + 2 + 1;
Packit 971217
  if (peak_bits == 16) {
Packit 971217
    peak = GST_READ_UINT16_BE (data);
Packit 971217
  } else {
Packit 971217
    peak = 0;
Packit 971217
    for (i = 0; i < (GST_ROUND_UP_8 (peak_bits) / 8) && datalen > 0; ++i) {
Packit 971217
      peak = peak << 8;
Packit 971217
      peak |= GST_READ_UINT8 (data);
Packit 971217
      ++data;
Packit 971217
      --datalen;
Packit 971217
    }
Packit 971217
  }
Packit 971217
Packit 971217
  if (peak_bits > 0) {
Packit 971217
    peak = peak << (64 - GST_ROUND_UP_8 (peak_bits));
Packit 971217
    peak_val =
Packit 971217
        gst_guint64_to_gdouble (peak) /
Packit 971217
        gst_util_guint64_to_gdouble (G_MAXINT64);
Packit 971217
    GST_LOG ("RVA2 frame: id=%s, chan=%u, adj=%.2fdB, peak_bits=%u, peak=%.2f",
Packit 971217
        id, chan, gain_dB, (guint) peak_bits, peak_val);
Packit 971217
  } else {
Packit 971217
    peak_val = 0;
Packit 971217
  }
Packit 971217
Packit 971217
  if (chan == ID3V2_RVA2_CHANNEL_MASTER && strcmp (id, "track") == 0) {
Packit 971217
    gain_tag_name = GST_TAG_TRACK_GAIN;
Packit 971217
    peak_tag_name = GST_TAG_TRACK_PEAK;
Packit 971217
  } else if (chan == ID3V2_RVA2_CHANNEL_MASTER && strcmp (id, "album") == 0) {
Packit 971217
    gain_tag_name = GST_TAG_ALBUM_GAIN;
Packit 971217
    peak_tag_name = GST_TAG_ALBUM_PEAK;
Packit 971217
  } else {
Packit 971217
    GST_INFO ("Unhandled RVA2 frame id '%s' for channel %d", id, chan);
Packit 971217
  }
Packit 971217
Packit 971217
  if (gain_tag_name) {
Packit 971217
    gst_tag_list_add (work->tags, GST_TAG_MERGE_APPEND,
Packit 971217
        gain_tag_name, gain_dB, NULL);
Packit 971217
  }
Packit 971217
  if (peak_tag_name && peak_bits > 0) {
Packit 971217
    gst_tag_list_add (work->tags, GST_TAG_MERGE_APPEND,
Packit 971217
        peak_tag_name, peak_val, NULL);
Packit 971217
  }
Packit 971217
Packit 971217
  g_free (id);
Packit 971217
Packit 971217
  return (gain_tag_name != NULL || peak_tag_name != NULL);
Packit 971217
}
Packit 971217
Packit 971217
static void
Packit 971217
parse_obsolete_tdat_frame (ID3TagsWorking * work)
Packit 971217
{
Packit 971217
  if (work->parse_size >= 5 &&
Packit 971217
      work->parse_data[0] == ID3V2_ENCODING_ISO8859 &&
Packit 971217
      g_ascii_isdigit (work->parse_data[1]) &&
Packit 971217
      g_ascii_isdigit (work->parse_data[2]) &&
Packit 971217
      g_ascii_isdigit (work->parse_data[3]) &&
Packit 971217
      g_ascii_isdigit (work->parse_data[4])) {
Packit 971217
Packit 971217
    guint pending_day = (10 * g_ascii_digit_value (work->parse_data[1])) +
Packit 971217
        g_ascii_digit_value (work->parse_data[2]);
Packit 971217
    guint pending_month = (10 * g_ascii_digit_value (work->parse_data[3])) +
Packit 971217
        g_ascii_digit_value (work->parse_data[4]);
Packit 971217
Packit 971217
    if (pending_day >= 1 && pending_day <= 31 && pending_month >= 1
Packit 971217
        && pending_month <= 12) {
Packit 971217
      GST_LOG ("date (dd/mm) %02u/%02u", pending_day, pending_month);
Packit 971217
      work->pending_day = pending_day;
Packit 971217
      work->pending_month = pending_month;
Packit 971217
    } else {
Packit 971217
      GST_WARNING ("Ignoring invalid ID3v2 TDAT frame (dd/mm) %02u/%02u",
Packit 971217
          pending_day, pending_month);
Packit 971217
    }
Packit 971217
  }
Packit 971217
}
Packit 971217
Packit 971217
static gboolean
Packit 971217
id3v2_tag_to_taglist (ID3TagsWorking * work, const gchar * tag_name,
Packit 971217
    const gchar * tag_str)
Packit 971217
{
Packit 971217
  GType tag_type = gst_tag_get_type (tag_name);
Packit 971217
  GstTagList *tag_list = work->tags;
Packit 971217
Packit 971217
  if (tag_str == NULL)
Packit 971217
    return FALSE;
Packit 971217
Packit 971217
  switch (tag_type) {
Packit 971217
    case G_TYPE_UINT:
Packit 971217
    {
Packit 971217
      gint current, total;
Packit 971217
Packit 971217
      if (sscanf (tag_str, "%d/%d", &current, &total) == 2) {
Packit 971217
        if (total <= 0) {
Packit 971217
          GST_WARNING ("Ignoring invalid value for total %d in tag %s",
Packit 971217
              total, tag_name);
Packit 971217
        } else {
Packit 971217
          if (strcmp (tag_name, GST_TAG_TRACK_NUMBER) == 0) {
Packit 971217
            gst_tag_list_add (tag_list, GST_TAG_MERGE_APPEND,
Packit 971217
                GST_TAG_TRACK_COUNT, total, NULL);
Packit 971217
          } else if (strcmp (tag_name, GST_TAG_ALBUM_VOLUME_NUMBER) == 0) {
Packit 971217
            gst_tag_list_add (tag_list, GST_TAG_MERGE_APPEND,
Packit 971217
                GST_TAG_ALBUM_VOLUME_COUNT, total, NULL);
Packit 971217
          }
Packit 971217
        }
Packit 971217
      } else if (sscanf (tag_str, "%d", &current) != 1) {
Packit 971217
        /* Not an integer in the string */
Packit 971217
        GST_WARNING ("Tag string for tag %s does not contain an integer - "
Packit 971217
            "ignoring", tag_name);
Packit 971217
        break;
Packit 971217
      }
Packit 971217
Packit 971217
      if (current <= 0) {
Packit 971217
        GST_WARNING ("Ignoring invalid value %d in tag %s", current, tag_name);
Packit 971217
      } else {
Packit 971217
        gst_tag_list_add (tag_list, GST_TAG_MERGE_APPEND, tag_name, current,
Packit 971217
            NULL);
Packit 971217
      }
Packit 971217
      break;
Packit 971217
    }
Packit 971217
    case G_TYPE_UINT64:
Packit 971217
    {
Packit 971217
      guint64 tmp;
Packit 971217
Packit 971217
      g_assert (strcmp (tag_name, GST_TAG_DURATION) == 0);
Packit 971217
      tmp = strtoul (tag_str, NULL, 10);
Packit 971217
      if (tmp == 0) {
Packit 971217
        break;
Packit 971217
      }
Packit 971217
      gst_tag_list_add (tag_list, GST_TAG_MERGE_APPEND,
Packit 971217
          GST_TAG_DURATION, tmp * 1000 * 1000, NULL);
Packit 971217
      break;
Packit 971217
    }
Packit 971217
    case G_TYPE_STRING:{
Packit 971217
      const GValue *val;
Packit 971217
      guint i, num;
Packit 971217
Packit 971217
      /* make sure we add each unique string only once per tag, we don't want
Packit 971217
       * to have the same genre in the genre list multiple times, for example,
Packit 971217
       * or the same DiscID in there twice just because it's contained in the
Packit 971217
       * tag multiple times under different TXXX user tags */
Packit 971217
      num = gst_tag_list_get_tag_size (tag_list, tag_name);
Packit 971217
      for (i = 0; i < num; ++i) {
Packit 971217
        val = gst_tag_list_get_value_index (tag_list, tag_name, i);
Packit 971217
        if (val != NULL && strcmp (g_value_get_string (val), tag_str) == 0)
Packit 971217
          break;
Packit 971217
      }
Packit 971217
      if (i == num) {
Packit 971217
        gst_tag_list_add (tag_list, GST_TAG_MERGE_APPEND,
Packit 971217
            tag_name, tag_str, NULL);
Packit 971217
      }
Packit 971217
      break;
Packit 971217
    }
Packit 971217
Packit 971217
    default:{
Packit 971217
      if (tag_type == GST_TYPE_DATE_TIME) {
Packit 971217
        GstDateTime *dt;
Packit 971217
Packit 971217
        /* Dates can be yyyy-MM-dd, yyyy-MM or yyyy */
Packit 971217
        dt = gst_date_time_new_from_iso8601_string (tag_str);
Packit 971217
        if (dt != NULL) {
Packit 971217
          gst_tag_list_add (tag_list, GST_TAG_MERGE_APPEND, tag_name, dt, NULL);
Packit 971217
          gst_date_time_unref (dt);
Packit 971217
        } else {
Packit 971217
          GST_WARNING ("Could not transform '%s' into date", tag_str);
Packit 971217
        }
Packit 971217
      } else {
Packit 971217
        GValue src = { 0, };
Packit 971217
        GValue dest = { 0, };
Packit 971217
Packit 971217
        /* handles anything else */
Packit 971217
        g_value_init (&src, G_TYPE_STRING);
Packit 971217
        g_value_set_string (&src, (const gchar *) tag_str);
Packit 971217
        g_value_init (&dest, tag_type);
Packit 971217
Packit 971217
        if (g_value_transform (&src, &dest)) {
Packit 971217
          gst_tag_list_add_values (tag_list, GST_TAG_MERGE_APPEND,
Packit 971217
              tag_name, &dest, NULL);
Packit 971217
        } else if (tag_type == G_TYPE_DOUBLE) {
Packit 971217
          /* replaygain tags in TXXX frames ... */
Packit 971217
          g_value_set_double (&dest, g_strtod (tag_str, NULL));
Packit 971217
          gst_tag_list_add_values (tag_list, GST_TAG_MERGE_KEEP,
Packit 971217
              tag_name, &dest, NULL);
Packit 971217
          GST_LOG ("Converted string '%s' to double %f", tag_str,
Packit 971217
              g_value_get_double (&dest));
Packit 971217
        } else {
Packit 971217
          GST_WARNING ("Failed to transform tag from string '%s' to type '%s'",
Packit 971217
              tag_str, g_type_name (tag_type));
Packit 971217
        }
Packit 971217
Packit 971217
        g_value_unset (&src;;
Packit 971217
        g_value_unset (&dest);
Packit 971217
      }
Packit 971217
      break;
Packit 971217
    }
Packit 971217
  }
Packit 971217
Packit 971217
  return TRUE;
Packit 971217
}
Packit 971217
Packit 971217
/* Check that an array of characters contains only digits */
Packit 971217
static gboolean
Packit 971217
id3v2_are_digits (const gchar * chars, gint size)
Packit 971217
{
Packit 971217
  gint i;
Packit 971217
Packit 971217
  for (i = 0; i < size; i++) {
Packit 971217
    if (!g_ascii_isdigit (chars[i]))
Packit 971217
      return FALSE;
Packit 971217
  }
Packit 971217
  return TRUE;
Packit 971217
}
Packit 971217
Packit 971217
static gboolean
Packit 971217
id3v2_genre_string_to_taglist (ID3TagsWorking * work, const gchar * tag_name,
Packit 971217
    const gchar * tag_str, gint len)
Packit 971217
{
Packit 971217
  g_return_val_if_fail (tag_str != NULL, FALSE);
Packit 971217
Packit 971217
  /* If it's a number, it might be a defined genre */
Packit 971217
  if (id3v2_are_digits (tag_str, len)) {
Packit 971217
    tag_str = gst_tag_id3_genre_get (strtol (tag_str, NULL, 10));
Packit 971217
    return id3v2_tag_to_taglist (work, tag_name, tag_str);
Packit 971217
  }
Packit 971217
  /* Otherwise it might be "RX" or "CR" */
Packit 971217
  if (len == 2) {
Packit 971217
    if (g_ascii_strncasecmp ("rx", tag_str, len) == 0)
Packit 971217
      return id3v2_tag_to_taglist (work, tag_name, "Remix");
Packit 971217
Packit 971217
    if (g_ascii_strncasecmp ("cr", tag_str, len) == 0)
Packit 971217
      return id3v2_tag_to_taglist (work, tag_name, "Cover");
Packit 971217
  }
Packit 971217
Packit 971217
  /* Otherwise it's a string */
Packit 971217
  return id3v2_tag_to_taglist (work, tag_name, tag_str);
Packit 971217
}
Packit 971217
Packit 971217
static gboolean
Packit 971217
id3v2_genre_fields_to_taglist (ID3TagsWorking * work, const gchar * tag_name,
Packit 971217
    GArray * tag_fields)
Packit 971217
{
Packit 971217
  gchar *tag_str = NULL;
Packit 971217
  gboolean result = FALSE;
Packit 971217
  gint i;
Packit 971217
Packit 971217
  for (i = 0; i < tag_fields->len; i++) {
Packit 971217
    gint len;
Packit 971217
Packit 971217
    tag_str = g_array_index (tag_fields, gchar *, i);
Packit 971217
    if (tag_str == NULL)
Packit 971217
      continue;
Packit 971217
Packit 971217
    len = strlen (tag_str);
Packit 971217
    /* Only supposed to see '(n)' type numeric genre strings in ID3 <= 2.3.0
Packit 971217
     * but apparently we see them in 2.4.0 sometimes too */
Packit 971217
    if (TRUE || work->hdr.version <= 0x300) {   /* <= 2.3.0 */
Packit 971217
      /* Check for genre numbers wrapped in parentheses, possibly
Packit 971217
       * followed by a string */
Packit 971217
      while (len >= 2) {
Packit 971217
        gint pos;
Packit 971217
        gboolean found = FALSE;
Packit 971217
Packit 971217
        /* Double parenthesis ends the numeric genres, but we need
Packit 971217
         * to swallow the first one so we actually output '(' */
Packit 971217
        if (tag_str[0] == '(' && tag_str[1] == '(') {
Packit 971217
          tag_str++;
Packit 971217
          len--;
Packit 971217
          break;
Packit 971217
        }
Packit 971217
Packit 971217
        /* If the first char is not a parenthesis, then stop
Packit 971217
         * looking for parenthesised genre strings */
Packit 971217
        if (tag_str[0] != '(')
Packit 971217
          break;
Packit 971217
Packit 971217
        for (pos = 1; pos < len; pos++) {
Packit 971217
          if (tag_str[pos] == ')') {
Packit 971217
            gchar *tmp_str;
Packit 971217
Packit 971217
            tmp_str = g_strndup (tag_str + 1, pos - 1);
Packit 971217
            result |=
Packit 971217
                id3v2_genre_string_to_taglist (work, tag_name, tmp_str,
Packit 971217
                pos - 1);
Packit 971217
            g_free (tmp_str);
Packit 971217
            tag_str += pos + 1;
Packit 971217
            len -= pos + 1;
Packit 971217
            found = TRUE;
Packit 971217
            break;
Packit 971217
          }
Packit 971217
Packit 971217
          /* If we encounter a non-digit while searching for a closing 
Packit 971217
           * parenthesis, we should not try and interpret this as a 
Packit 971217
           * numeric genre string */
Packit 971217
          if (!g_ascii_isdigit (tag_str[pos]))
Packit 971217
            break;
Packit 971217
        }
Packit 971217
        if (!found)
Packit 971217
          break;                /* There was no closing parenthesis */
Packit 971217
      }
Packit 971217
    }
Packit 971217
Packit 971217
    if (len > 0 && tag_str != NULL)
Packit 971217
      result |= id3v2_genre_string_to_taglist (work, tag_name, tag_str, len);
Packit 971217
  }
Packit 971217
  return result;
Packit 971217
}
Packit 971217
Packit 971217
static gboolean
Packit 971217
find_utf16_bom (gchar * data, gint * p_data_endianness)
Packit 971217
{
Packit 971217
  guint16 marker = (GST_READ_UINT8 (data) << 8) | GST_READ_UINT8 (data + 1);
Packit 971217
Packit 971217
  switch (marker) {
Packit 971217
    case 0xFFFE:
Packit 971217
      *p_data_endianness = G_LITTLE_ENDIAN;
Packit 971217
      return TRUE;
Packit 971217
    case 0xFEFF:
Packit 971217
      *p_data_endianness = G_BIG_ENDIAN;
Packit 971217
      return TRUE;
Packit 971217
    default:
Packit 971217
      break;
Packit 971217
  }
Packit 971217
  return FALSE;
Packit 971217
}
Packit 971217
Packit 971217
static void *
Packit 971217
string_utf8_dup (const gchar * start, const guint size)
Packit 971217
{
Packit 971217
  const gchar *env;
Packit 971217
  gsize bytes_read;
Packit 971217
  gchar *utf8;
Packit 971217
Packit 971217
  /* Should we try the charsets specified
Packit 971217
   * via environment variables FIRST ? */
Packit 971217
  if (g_utf8_validate (start, size, NULL)) {
Packit 971217
    utf8 = g_strndup (start, size);
Packit 971217
    goto beach;
Packit 971217
  }
Packit 971217
Packit 971217
  env = g_getenv ("GST_ID3V1_TAG_ENCODING");
Packit 971217
  if (!env || *env == '\0')
Packit 971217
    env = g_getenv ("GST_ID3_TAG_ENCODING");
Packit 971217
  if (!env || *env == '\0')
Packit 971217
    env = g_getenv ("GST_TAG_ENCODING");
Packit 971217
Packit 971217
  /* Try charsets specified via the environment */
Packit 971217
  if (env && *env != '\0') {
Packit 971217
    gchar **c, **csets;
Packit 971217
Packit 971217
    csets = g_strsplit (env, G_SEARCHPATH_SEPARATOR_S, -1);
Packit 971217
Packit 971217
    for (c = csets; c && *c; ++c) {
Packit 971217
      if ((utf8 =
Packit 971217
              g_convert (start, size, "UTF-8", *c, &bytes_read, NULL, NULL))) {
Packit 971217
        if (bytes_read == size) {
Packit 971217
          GST_DEBUG ("Using charset %s to interpret id3 tags", *c);
Packit 971217
          g_strfreev (csets);
Packit 971217
          goto beach;
Packit 971217
        }
Packit 971217
        g_free (utf8);
Packit 971217
        utf8 = NULL;
Packit 971217
      }
Packit 971217
    }
Packit 971217
  }
Packit 971217
  /* Try current locale (if not UTF-8) */
Packit 971217
  if (!g_get_charset (&env)) {
Packit 971217
    if ((utf8 = g_locale_to_utf8 (start, size, &bytes_read, NULL, NULL))) {
Packit 971217
      if (bytes_read == size) {
Packit 971217
        goto beach;
Packit 971217
      }
Packit 971217
      g_free (utf8);
Packit 971217
      utf8 = NULL;
Packit 971217
    }
Packit 971217
  }
Packit 971217
Packit 971217
  /* Try ISO-8859-1 */
Packit 971217
  utf8 =
Packit 971217
      g_convert (start, size, "UTF-8", "ISO-8859-1", &bytes_read, NULL, NULL);
Packit 971217
  if (utf8 != NULL && bytes_read == size) {
Packit 971217
    goto beach;
Packit 971217
  }
Packit 971217
Packit 971217
  g_free (utf8);
Packit 971217
  return NULL;
Packit 971217
Packit 971217
beach:
Packit 971217
Packit 971217
  g_strchomp (utf8);
Packit 971217
Packit 971217
  return (utf8);
Packit 971217
}
Packit 971217
Packit 971217
static void
Packit 971217
parse_insert_string_field (guint8 encoding, gchar * data, gint data_size,
Packit 971217
    GArray * fields)
Packit 971217
{
Packit 971217
  gchar *field = NULL;
Packit 971217
Packit 971217
  switch (encoding) {
Packit 971217
    case ID3V2_ENCODING_UTF16:
Packit 971217
    case ID3V2_ENCODING_UTF16BE:
Packit 971217
    {
Packit 971217
      gunichar2 *utf16;
Packit 971217
      gint data_endianness;
Packit 971217
      glong n_read = 0, size = 0;
Packit 971217
      guint len, i;
Packit 971217
Packit 971217
      if (encoding == ID3V2_ENCODING_UTF16)
Packit 971217
        data_endianness = G_BYTE_ORDER;
Packit 971217
      else
Packit 971217
        data_endianness = G_BIG_ENDIAN;
Packit 971217
Packit 971217
      /* Sometimes we see strings with multiple BOM markers at the start.
Packit 971217
       * In that case, we assume the innermost one is correct. If that fails
Packit 971217
       * to produce valid UTF-8, we try the other endianness anyway */
Packit 971217
      while (data_size >= 2 && find_utf16_bom (data, &data_endianness)) {
Packit 971217
        data += 2;              /* skip BOM */
Packit 971217
        data_size -= 2;
Packit 971217
      }
Packit 971217
Packit 971217
      if (data_size < 2) {
Packit 971217
        field = g_strdup ("");
Packit 971217
        break;
Packit 971217
      }
Packit 971217
Packit 971217
      /* alloc needed to ensure correct alignment which is required by GLib */
Packit 971217
      len = data_size / 2;
Packit 971217
      utf16 = g_try_new (gunichar2, len + 1);
Packit 971217
      if (utf16 == NULL)
Packit 971217
        break;
Packit 971217
Packit 971217
      memcpy (utf16, data, 2 * len);
Packit 971217
Packit 971217
      GST_LOG ("Trying interpreting data as UTF-16-%s first",
Packit 971217
          (data_endianness == G_LITTLE_ENDIAN) ? "LE" : "BE");
Packit 971217
Packit 971217
      if (data_endianness != G_BYTE_ORDER) {
Packit 971217
        /* convert to native endian UTF-16 */
Packit 971217
        for (i = 0; i < len; ++i)
Packit 971217
          utf16[i] = GUINT16_SWAP_LE_BE (utf16[i]);
Packit 971217
      }
Packit 971217
Packit 971217
      /* convert to UTF-8 */
Packit 971217
      field = g_utf16_to_utf8 (utf16, len, &n_read, &size, NULL);
Packit 971217
      if (field != NULL && n_read > 0 && g_utf8_validate (field, -1, NULL)) {
Packit 971217
        g_free (utf16);
Packit 971217
        break;
Packit 971217
      }
Packit 971217
Packit 971217
      GST_DEBUG ("Trying interpreting data as UTF-16-%s now as fallback",
Packit 971217
          (data_endianness == G_LITTLE_ENDIAN) ? "BE" : "LE");
Packit 971217
Packit 971217
      for (i = 0; i < len; ++i)
Packit 971217
        utf16[i] = GUINT16_SWAP_LE_BE (utf16[i]);
Packit 971217
Packit 971217
      g_free (field);
Packit 971217
      n_read = size = 0;
Packit 971217
Packit 971217
      /* try again */
Packit 971217
      field = g_utf16_to_utf8 (utf16, len, &n_read, &size, NULL);
Packit 971217
      g_free (utf16);
Packit 971217
Packit 971217
      if (field != NULL && n_read > 0 && g_utf8_validate (field, -1, NULL))
Packit 971217
        break;
Packit 971217
Packit 971217
      GST_DEBUG ("Could not convert UTF-16 string to UTF-8");
Packit 971217
      g_free (field);
Packit 971217
      field = NULL;
Packit 971217
      break;
Packit 971217
    }
Packit 971217
    case ID3V2_ENCODING_ISO8859:
Packit 971217
      if (g_utf8_validate (data, data_size, NULL))
Packit 971217
        field = g_strndup (data, data_size);
Packit 971217
      else
Packit 971217
        /* field = g_convert (data, data_size, "UTF-8", "ISO-8859-1",
Packit 971217
           NULL, NULL, NULL); */
Packit 971217
        field = string_utf8_dup (data, data_size);
Packit 971217
      break;
Packit 971217
    default:
Packit 971217
      field = g_strndup (data, data_size);
Packit 971217
      break;
Packit 971217
  }
Packit 971217
Packit 971217
  if (field) {
Packit 971217
    if (g_utf8_validate (field, -1, NULL)) {
Packit 971217
      g_array_append_val (fields, field);
Packit 971217
      return;
Packit 971217
    }
Packit 971217
Packit 971217
    GST_DEBUG ("%s was bad UTF-8 after conversion from encoding %d. Ignoring",
Packit 971217
        field, encoding);
Packit 971217
    g_free (field);
Packit 971217
  }
Packit 971217
}
Packit 971217
Packit 971217
static void
Packit 971217
parse_split_strings (guint8 encoding, gchar * data, gint data_size,
Packit 971217
    GArray ** out_fields)
Packit 971217
{
Packit 971217
  GArray *fields = g_array_new (FALSE, TRUE, sizeof (gchar *));
Packit 971217
  gint text_pos;
Packit 971217
  gint prev = 0;
Packit 971217
Packit 971217
  g_return_if_fail (out_fields != NULL);
Packit 971217
Packit 971217
  switch (encoding) {
Packit 971217
    case ID3V2_ENCODING_ISO8859:
Packit 971217
      for (text_pos = 0; text_pos < data_size; text_pos++) {
Packit 971217
        if (data[text_pos] == 0) {
Packit 971217
          parse_insert_string_field (encoding, data + prev,
Packit 971217
              text_pos - prev, fields);
Packit 971217
          prev = text_pos + 1;
Packit 971217
        }
Packit 971217
      }
Packit 971217
      if (data_size - prev > 0 && data[prev] != 0x00) {
Packit 971217
        parse_insert_string_field (encoding, data + prev,
Packit 971217
            data_size - prev, fields);
Packit 971217
      }
Packit 971217
Packit 971217
      break;
Packit 971217
    case ID3V2_ENCODING_UTF8:
Packit 971217
      for (prev = 0, text_pos = 0; text_pos < data_size; text_pos++) {
Packit 971217
        if (data[text_pos] == '\0') {
Packit 971217
          parse_insert_string_field (encoding, data + prev,
Packit 971217
              text_pos - prev, fields);
Packit 971217
          prev = text_pos + 1;
Packit 971217
        }
Packit 971217
      }
Packit 971217
      if (data_size - prev > 0 && data[prev] != 0x00) {
Packit 971217
        parse_insert_string_field (encoding, data + prev,
Packit 971217
            data_size - prev, fields);
Packit 971217
      }
Packit 971217
      break;
Packit 971217
    case ID3V2_ENCODING_UTF16:
Packit 971217
    case ID3V2_ENCODING_UTF16BE:
Packit 971217
    {
Packit 971217
      /* Find '\0\0' terminator */
Packit 971217
      for (text_pos = 0; text_pos < data_size - 1; text_pos += 2) {
Packit 971217
        if (data[text_pos] == '\0' && data[text_pos + 1] == '\0') {
Packit 971217
          /* found a delimiter */
Packit 971217
          parse_insert_string_field (encoding, data + prev,
Packit 971217
              text_pos - prev, fields);
Packit 971217
          prev = text_pos + 2;
Packit 971217
        }
Packit 971217
      }
Packit 971217
      if (data_size - prev > 1 &&
Packit 971217
          (data[prev] != 0x00 || data[prev + 1] != 0x00)) {
Packit 971217
        /* There were 2 or more non-null chars left, convert those too */
Packit 971217
        parse_insert_string_field (encoding, data + prev,
Packit 971217
            data_size - prev, fields);
Packit 971217
      }
Packit 971217
      break;
Packit 971217
    }
Packit 971217
  }
Packit 971217
  if (fields->len > 0)
Packit 971217
    *out_fields = fields;
Packit 971217
  else
Packit 971217
    g_array_free (fields, TRUE);
Packit 971217
}
Packit 971217
Packit 971217
static void
Packit 971217
free_tag_strings (GArray * fields)
Packit 971217
{
Packit 971217
  if (fields) {
Packit 971217
    gint i;
Packit 971217
    gchar *c;
Packit 971217
Packit 971217
    for (i = 0; i < fields->len; i++) {
Packit 971217
      c = g_array_index (fields, gchar *, i);
Packit 971217
      g_free (c);
Packit 971217
    }
Packit 971217
    g_array_free (fields, TRUE);
Packit 971217
  }
Packit 971217
}