You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

111 lines
3.2KB

  1. /*
  2. * APE tag handling
  3. * Copyright (c) 2007 Benjamin Zores <ben@geexbox.org>
  4. * based upon libdemac from Dave Chapman.
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. #include "libavutil/intreadwrite.h"
  23. #include "avformat.h"
  24. #define ENABLE_DEBUG 0
  25. #define APE_TAG_VERSION 2000
  26. #define APE_TAG_FOOTER_BYTES 32
  27. #define APE_TAG_FLAG_CONTAINS_HEADER (1 << 31)
  28. #define APE_TAG_FLAG_IS_HEADER (1 << 29)
  29. static int ape_tag_read_field(AVFormatContext *s)
  30. {
  31. ByteIOContext *pb = s->pb;
  32. uint8_t key[1024], value[1024];
  33. uint32_t size, flags;
  34. int i, l, c;
  35. size = get_le32(pb); /* field size */
  36. flags = get_le32(pb); /* field flags */
  37. for (i = 0; i < sizeof(key) - 1; i++) {
  38. c = get_byte(pb);
  39. if (c < 0x20 || c > 0x7E)
  40. break;
  41. else
  42. key[i] = c;
  43. }
  44. key[i] = 0;
  45. if (c != 0) {
  46. av_log(s, AV_LOG_WARNING, "Invalid APE tag key '%s'.\n", key);
  47. return -1;
  48. }
  49. l = FFMIN(size, sizeof(value)-1);
  50. get_buffer(pb, value, l);
  51. value[l] = 0;
  52. url_fskip(pb, size-l);
  53. if (l < size)
  54. av_log(s, AV_LOG_WARNING, "Too long '%s' tag was truncated.\n", key);
  55. av_metadata_set(&s->metadata, key, value);
  56. return 0;
  57. }
  58. void ff_ape_parse_tag(AVFormatContext *s)
  59. {
  60. ByteIOContext *pb = s->pb;
  61. int file_size = url_fsize(pb);
  62. uint32_t val, fields, tag_bytes;
  63. uint8_t buf[8];
  64. int i;
  65. if (file_size < APE_TAG_FOOTER_BYTES)
  66. return;
  67. url_fseek(pb, file_size - APE_TAG_FOOTER_BYTES, SEEK_SET);
  68. get_buffer(pb, buf, 8); /* APETAGEX */
  69. if (strncmp(buf, "APETAGEX", 8)) {
  70. return;
  71. }
  72. val = get_le32(pb); /* APE tag version */
  73. if (val > APE_TAG_VERSION) {
  74. av_log(s, AV_LOG_ERROR, "Unsupported tag version. (>=%d)\n", APE_TAG_VERSION);
  75. return;
  76. }
  77. tag_bytes = get_le32(pb); /* tag size */
  78. if (tag_bytes - APE_TAG_FOOTER_BYTES > (1024 * 1024 * 16)) {
  79. av_log(s, AV_LOG_ERROR, "Tag size is way too big\n");
  80. return;
  81. }
  82. fields = get_le32(pb); /* number of fields */
  83. if (fields > 65536) {
  84. av_log(s, AV_LOG_ERROR, "Too many tag fields (%d)\n", fields);
  85. return;
  86. }
  87. val = get_le32(pb); /* flags */
  88. if (val & APE_TAG_FLAG_IS_HEADER) {
  89. av_log(s, AV_LOG_ERROR, "APE Tag is a header\n");
  90. return;
  91. }
  92. url_fseek(pb, file_size - tag_bytes, SEEK_SET);
  93. for (i=0; i<fields; i++)
  94. if (ape_tag_read_field(s) < 0) break;
  95. }