apetag.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. static int ape_tag_read_field(AVFormatContext *s)
  31. {
  32. AVIOContext *pb = s->pb;
  33. uint8_t key[1024], *value;
  34. uint32_t size;
  35. int i, c;
  36. size = avio_rl32(pb); /* field size */
  37. avio_skip(pb, 4); /* field flags */
  38. for (i = 0; i < sizeof(key) - 1; i++) {
  39. c = avio_r8(pb);
  40. if (c < 0x20 || c > 0x7E)
  41. break;
  42. else
  43. key[i] = c;
  44. }
  45. key[i] = 0;
  46. if (c != 0) {
  47. av_log(s, AV_LOG_WARNING, "Invalid APE tag key '%s'.\n", key);
  48. return -1;
  49. }
  50. if (size > INT32_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
  51. av_log(s, AV_LOG_ERROR, "APE tag size too large.\n");
  52. return AVERROR_INVALIDDATA;
  53. }
  54. value = av_malloc(size+1);
  55. if (!value)
  56. return AVERROR(ENOMEM);
  57. avio_read(pb, value, size);
  58. value[size] = 0;
  59. av_dict_set(&s->metadata, key, value, AV_DICT_DONT_STRDUP_VAL);
  60. return 0;
  61. }
  62. void ff_ape_parse_tag(AVFormatContext *s)
  63. {
  64. AVIOContext *pb = s->pb;
  65. int64_t file_size = avio_size(pb);
  66. uint32_t val, fields, tag_bytes;
  67. uint8_t buf[8];
  68. int i;
  69. if (file_size < APE_TAG_FOOTER_BYTES)
  70. return;
  71. avio_seek(pb, file_size - APE_TAG_FOOTER_BYTES, SEEK_SET);
  72. avio_read(pb, buf, 8); /* APETAGEX */
  73. if (strncmp(buf, "APETAGEX", 8)) {
  74. return;
  75. }
  76. val = avio_rl32(pb); /* APE tag version */
  77. if (val > APE_TAG_VERSION) {
  78. av_log(s, AV_LOG_ERROR, "Unsupported tag version. (>=%d)\n", APE_TAG_VERSION);
  79. return;
  80. }
  81. tag_bytes = avio_rl32(pb); /* tag size */
  82. if (tag_bytes - APE_TAG_FOOTER_BYTES > (1024 * 1024 * 16)) {
  83. av_log(s, AV_LOG_ERROR, "Tag size is way too big\n");
  84. return;
  85. }
  86. fields = avio_rl32(pb); /* number of fields */
  87. if (fields > 65536) {
  88. av_log(s, AV_LOG_ERROR, "Too many tag fields (%d)\n", fields);
  89. return;
  90. }
  91. val = avio_rl32(pb); /* flags */
  92. if (val & APE_TAG_FLAG_IS_HEADER) {
  93. av_log(s, AV_LOG_ERROR, "APE Tag is a header\n");
  94. return;
  95. }
  96. avio_seek(pb, file_size - tag_bytes, SEEK_SET);
  97. for (i=0; i<fields; i++)
  98. if (ape_tag_read_field(s) < 0) break;
  99. }