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.

131 lines
3.9KB

  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 Libav.
  7. *
  8. * Libav 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. * Libav 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 Libav; 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. value[c] = 0;
  75. av_dict_set(&s->metadata, key, value, AV_DICT_DONT_STRDUP_VAL);
  76. }
  77. return 0;
  78. }
  79. void ff_ape_parse_tag(AVFormatContext *s)
  80. {
  81. AVIOContext *pb = s->pb;
  82. int file_size = avio_size(pb);
  83. uint32_t val, fields, tag_bytes;
  84. uint8_t buf[8];
  85. int i;
  86. if (file_size < APE_TAG_FOOTER_BYTES)
  87. return;
  88. avio_seek(pb, file_size - APE_TAG_FOOTER_BYTES, SEEK_SET);
  89. avio_read(pb, buf, 8); /* APETAGEX */
  90. if (strncmp(buf, "APETAGEX", 8)) {
  91. return;
  92. }
  93. val = avio_rl32(pb); /* APE tag version */
  94. if (val > APE_TAG_VERSION) {
  95. av_log(s, AV_LOG_ERROR, "Unsupported tag version. (>=%d)\n", APE_TAG_VERSION);
  96. return;
  97. }
  98. tag_bytes = avio_rl32(pb); /* tag size */
  99. if (tag_bytes - APE_TAG_FOOTER_BYTES > (1024 * 1024 * 16)) {
  100. av_log(s, AV_LOG_ERROR, "Tag size is way too big\n");
  101. return;
  102. }
  103. fields = avio_rl32(pb); /* number of fields */
  104. if (fields > 65536) {
  105. av_log(s, AV_LOG_ERROR, "Too many tag fields (%d)\n", fields);
  106. return;
  107. }
  108. val = avio_rl32(pb); /* flags */
  109. if (val & APE_TAG_FLAG_IS_HEADER) {
  110. av_log(s, AV_LOG_ERROR, "APE Tag is a header\n");
  111. return;
  112. }
  113. avio_seek(pb, file_size - tag_bytes, SEEK_SET);
  114. for (i=0; i<fields; i++)
  115. if (ape_tag_read_field(s) < 0) break;
  116. }