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.

135 lines
4.0KB

  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 "libavutil/dict.h"
  24. #include "avformat.h"
  25. #include "apetag.h"
  26. #define APE_TAG_VERSION 2000
  27. #define APE_TAG_FOOTER_BYTES 32
  28. #define APE_TAG_FLAG_CONTAINS_HEADER (1 << 31)
  29. #define APE_TAG_FLAG_IS_HEADER (1 << 29)
  30. #define APE_TAG_FLAG_IS_BINARY (1 << 1)
  31. static int ape_tag_read_field(AVFormatContext *s)
  32. {
  33. AVIOContext *pb = s->pb;
  34. uint8_t key[1024], *value;
  35. uint32_t size, flags;
  36. int i, c;
  37. size = avio_rl32(pb); /* field size */
  38. flags = avio_rl32(pb); /* field flags */
  39. for (i = 0; i < sizeof(key) - 1; i++) {
  40. c = avio_r8(pb);
  41. if (c < 0x20 || c > 0x7E)
  42. break;
  43. else
  44. key[i] = c;
  45. }
  46. key[i] = 0;
  47. if (c != 0) {
  48. av_log(s, AV_LOG_WARNING, "Invalid APE tag key '%s'.\n", key);
  49. return -1;
  50. }
  51. if (size >= UINT_MAX)
  52. return -1;
  53. if (flags & APE_TAG_FLAG_IS_BINARY) {
  54. uint8_t filename[1024];
  55. AVStream *st = avformat_new_stream(s, NULL);
  56. if (!st)
  57. return AVERROR(ENOMEM);
  58. avio_get_str(pb, INT_MAX, filename, sizeof(filename));
  59. st->codec->extradata = av_malloc(size + FF_INPUT_BUFFER_PADDING_SIZE);
  60. if (!st->codec->extradata)
  61. return AVERROR(ENOMEM);
  62. if (avio_read(pb, st->codec->extradata, size) != size) {
  63. av_freep(&st->codec->extradata);
  64. return AVERROR(EIO);
  65. }
  66. st->codec->extradata_size = size;
  67. av_dict_set(&st->metadata, key, filename, 0);
  68. st->codec->codec_type = AVMEDIA_TYPE_ATTACHMENT;
  69. } else {
  70. value = av_malloc(size+1);
  71. if (!value)
  72. return AVERROR(ENOMEM);
  73. c = avio_read(pb, value, size);
  74. if (c < 0) {
  75. av_free(value);
  76. return c;
  77. }
  78. value[c] = 0;
  79. av_dict_set(&s->metadata, key, value, AV_DICT_DONT_STRDUP_VAL);
  80. }
  81. return 0;
  82. }
  83. void ff_ape_parse_tag(AVFormatContext *s)
  84. {
  85. AVIOContext *pb = s->pb;
  86. int file_size = avio_size(pb);
  87. uint32_t val, fields, tag_bytes;
  88. uint8_t buf[8];
  89. int i;
  90. if (file_size < APE_TAG_FOOTER_BYTES)
  91. return;
  92. avio_seek(pb, file_size - APE_TAG_FOOTER_BYTES, SEEK_SET);
  93. avio_read(pb, buf, 8); /* APETAGEX */
  94. if (strncmp(buf, "APETAGEX", 8)) {
  95. return;
  96. }
  97. val = avio_rl32(pb); /* APE tag version */
  98. if (val > APE_TAG_VERSION) {
  99. av_log(s, AV_LOG_ERROR, "Unsupported tag version. (>=%d)\n", APE_TAG_VERSION);
  100. return;
  101. }
  102. tag_bytes = avio_rl32(pb); /* tag size */
  103. if (tag_bytes - APE_TAG_FOOTER_BYTES > (1024 * 1024 * 16)) {
  104. av_log(s, AV_LOG_ERROR, "Tag size is way too big\n");
  105. return;
  106. }
  107. fields = avio_rl32(pb); /* number of fields */
  108. if (fields > 65536) {
  109. av_log(s, AV_LOG_ERROR, "Too many tag fields (%d)\n", fields);
  110. return;
  111. }
  112. val = avio_rl32(pb); /* flags */
  113. if (val & APE_TAG_FLAG_IS_HEADER) {
  114. av_log(s, AV_LOG_ERROR, "APE Tag is a header\n");
  115. return;
  116. }
  117. avio_seek(pb, file_size - tag_bytes, SEEK_SET);
  118. for (i=0; i<fields; i++)
  119. if (ape_tag_read_field(s) < 0) break;
  120. }