1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
|
/* Copyright (C) 2001 Chris Vaill
This file is part of nid3lib.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
/*
* comment manipulation routines
*/
#define _POSIX_C_SOURCE 2
#include "config.h"
#if STDC_HEADERS
# include <stdlib.h>
# include <string.h>
#else
# if HAVE_STDLIB_H
# include <stdlib.h>
# endif
# if HAVE_STRING_H
# include <string.h>
# else
# ifndef HAVE_MEMCPY
# define memcpy(d,s,n) bcopy((s),(d),(n))
# define memmove(d,s,n) bcopy((s),(d),(n))
# endif
# endif
#endif
#if HAVE_ERRNO_H
# include <errno.h>
#endif
#include "nid3P.h"
static const char *
_comment_id(id3_t tag)
{
switch (id3_get_version(tag)) {
case ID3_VERSION_2_2:
return "COM";
case ID3_VERSION_2_3:
case ID3_VERSION_2_4:
return "COMM";
}
return NULL;
}
/* returns nonzero iff a and b hold equivalent strings in encoding enc. */
static int
_desc_eq(const char *a, const char *b, int size, enum id3_text_encoding enc)
{
int i = 0;
switch (enc) {
case ID3_TEXT_UTF16:
case ID3_TEXT_UTF16BE:
while (i < size) {
if (a[i] != b[i] || a[i+1] != b[i+1])
return 0;
if (a[i] == '\0' && a[i+1] == '\0')
break;
i += 2;
}
break;
case ID3_TEXT_UTF8:
case ID3_TEXT_ISO:
while (i < size) {
if (a[i] != b[i])
return 0;
if (a[i] == '\0')
break;
i++;
}
break;
}
return 1;
}
/* extracts the comment text from the comment frame's data block */
static char *
_get_comment_text(id3_frame_t f)
{
unsigned char *s = id3_frame_get_raw(f);
if (s == NULL)
return NULL;
switch (s[0]) {
case ID3_TEXT_UTF16:
case ID3_TEXT_UTF16BE:
s += 4;
while ((s - f->data) <= f->sz && !(s[0] == '\0' && s[1] == '\0'))
s += 2;
if (!(s[0] == '\0' && s[1] == '\0'))
return NULL;
s += 2;
break;
case ID3_TEXT_UTF8:
case ID3_TEXT_ISO:
s += 4;
while ((s - f->data) <= f->sz && s[0] != '\0')
s++;
if (s[0] != '\0')
return NULL;
s++;
break;
}
return s;
}
/*
* Find a comment frame with the given description string and
* language. If desc or lang is NULL, it is ignored as a matching
* criterion.
*/
static id3_frame_t
_get_comment_frame(id3_t tag, const char *desc, const char *lang)
{
const char *id = _comment_id(tag);
char *s;
id3_frame_t f;
int nframes;
nframes = id3_frame_count(tag); /* make sure headers are read */
if (nframes == -1)
return NULL;
for (f = tag->frame_hd; f; f = f->next) {
if (strcmp(f->id, id) == 0) {
s = id3_frame_get_raw(f);
if (s == NULL)
continue;
if (desc && !_desc_eq(desc, s + 4, f->sz - 4, s[0]))
continue; /* content description doesn't match */
if (lang && memcmp(lang, s + 1, 3) != 0)
continue; /* language doesn't match */
return f;
}
}
return NULL;
}
char *
id3_comment_get(id3_t tag, const char *desc, const char *lang)
{
char *s = NULL;
id3_frame_t f = _get_comment_frame(tag, desc, lang);
if (f) {
s = _get_comment_text(f);
} else if (tag->has_v1tag && tag->v1tag[97] != '\0') {
strncpy(tag->v1buf, tag->v1tag + 97, 30);
tag->v1buf[30] = '\0';
s = tag->v1buf;
}
return s;
}
int
id3_comment_set(id3_t tag, const char *text, const char *desc,
const char *lang, enum id3_text_encoding enc)
{
id3_frame_t f;
unsigned char *data;
const char *id;
int desclen, textoffset, textlen, sz;
if (desc == NULL)
desc = "\0\0";
if (lang == NULL)
lang = "XXX";
/* calculate size of new frame */
desclen = textlen = 0;
switch (enc) {
case ID3_TEXT_UTF16:
case ID3_TEXT_UTF16BE:
while (!(desc[desclen] == '\0' && desc[desclen+1] == '\0'))
desclen += 2;
while (!(text[textlen] == '\0' && text[textlen+1] == '\0'))
textlen += 2;
textoffset = 4 + desclen + 2;
sz = 4 + desclen + 2 + textlen + 2;
break;
case ID3_TEXT_UTF8:
case ID3_TEXT_ISO:
while (desc[desclen] != '\0')
desclen++;
while (text[textlen] != '\0')
textlen++;
textoffset = 4 + desclen + 1;
sz = 4 + desclen + 1 + textlen + 2;
break;
default:
errno = EINVAL;
return -1;
}
/* set up the frame */
f = _get_comment_frame(tag, desc, lang);
if (f) {
if (f->data) {
if (f->sz < sz - 2) {
data = f->data;
f->data = (unsigned char *)calloc(sz, 1);
if (f->data == NULL) {
f->data = data;
return -1;
}
} else {
memset(f->data, 0, f->sz);
}
} else {
f->data = (unsigned char *)calloc(sz, 1);
if (f->data == NULL)
return -1;
}
} else {
id = _comment_id(tag);
f = id3_frame_add(tag, id);
if (f == NULL)
return -1;
f->data = (unsigned char *)calloc(sz, 1);
if (f->data == NULL)
return -1;
}
f->sz = sz - 2;
f->data[0] = (unsigned char)enc;
memcpy(f->data + 1, lang, 3);
memcpy(f->data + 4, desc, desclen);
memcpy(f->data + textoffset, text, textlen);
if (enc == ID3_TEXT_ISO)
strncpy(tag->v1tag + 97, text, 30);
return 0;
}
|