| 1 |
/* |
| 2 |
* id3tag.c -- Write ID3 version 1 and 2 tags. |
| 3 |
* |
| 4 |
* Copyright (C) 2000 Don Melton |
| 5 |
* Copyright (C) 2011-2017 Robert Hegemann |
| 6 |
* |
| 7 |
* This library is free software; you can redistribute it and/or |
| 8 |
* modify it under the terms of the GNU Library General Public |
| 9 |
* License as published by the Free Software Foundation; either |
| 10 |
* version 2 of the License, or (at your option) any later version. |
| 11 |
* |
| 12 |
* This library is distributed in the hope that it will be useful, |
| 13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 |
* Library General Public License for more details. |
| 16 |
* |
| 17 |
* You should have received a copy of the GNU Library General Public |
| 18 |
* License along with this library; if not, write to the Free Software |
| 19 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. |
| 20 |
*/ |
| 21 |
|
| 22 |
/* |
| 23 |
* HISTORY: This source file is part of LAME (see https://lame.sourceforge.io/) |
| 24 |
* and was originally adapted by Conrad Sanderson <[email protected]> |
| 25 |
* from mp3info by Ricardo Cerqueira <[email protected]> to write only ID3 version 1 |
| 26 |
* tags. Don Melton <[email protected]> COMPLETELY rewrote it to support version |
| 27 |
* 2 tags and be more conformant to other standards while remaining flexible. |
| 28 |
* |
| 29 |
* NOTE: See http://id3.org/ for more information about ID3 tag formats. |
| 30 |
*/ |
| 31 |
|
| 32 |
/* $Id$ */ |
| 33 |
|
| 34 |
#ifdef HAVE_CONFIG_H |
| 35 |
#include <config.h> |
| 36 |
#endif |
| 37 |
|
| 38 |
#ifdef STDC_HEADERS |
| 39 |
# include <stddef.h> |
| 40 |
# include <stdlib.h> |
| 41 |
# include <string.h> |
| 42 |
# include <ctype.h> |
| 43 |
#else |
| 44 |
# ifndef HAVE_STRCHR |
| 45 |
# define strchr index |
| 46 |
# define strrchr rindex |
| 47 |
# endif |
| 48 |
char *strchr(), *strrchr(); |
| 49 |
# ifndef HAVE_MEMCPY |
| 50 |
# define memcpy(d, s, n) bcopy ((s), (d), (n)) |
| 51 |
# endif |
| 52 |
#endif |
| 53 |
|
| 54 |
|
| 55 |
#include "lame.h" |
| 56 |
#include "machine.h" |
| 57 |
#include "encoder.h" |
| 58 |
#include "id3tag.h" |
| 59 |
#include "lame_global_flags.h" |
| 60 |
#include "util.h" |
| 61 |
#include "bitstream.h" |
| 62 |
|
| 63 |
|
| 64 |
static const char *const genre_names[] = { |
| 65 |
/* |
| 66 |
* NOTE: The spelling of these genre names is identical to those found in |
| 67 |
* Winamp and mp3info. |
| 68 |
*/ |
| 69 |
"Blues", "Classic Rock", "Country", "Dance", "Disco", "Funk", "Grunge", |
| 70 |
"Hip-Hop", "Jazz", "Metal", "New Age", "Oldies", "Other", "Pop", "R&B", |
| 71 |
"Rap", "Reggae", "Rock", "Techno", "Industrial", "Alternative", "Ska", |
| 72 |
"Death Metal", "Pranks", "Soundtrack", "Euro-Techno", "Ambient", "Trip-Hop", |
| 73 |
"Vocal", "Jazz+Funk", "Fusion", "Trance", "Classical", "Instrumental", |
| 74 |
"Acid", "House", "Game", "Sound Clip", "Gospel", "Noise", "Alternative Rock", |
| 75 |
"Bass", "Soul", "Punk", "Space", "Meditative", "Instrumental Pop", |
| 76 |
"Instrumental Rock", "Ethnic", "Gothic", "Darkwave", "Techno-Industrial", |
| 77 |
"Electronic", "Pop-Folk", "Eurodance", "Dream", "Southern Rock", "Comedy", |
| 78 |
"Cult", "Gangsta", "Top 40", "Christian Rap", "Pop/Funk", "Jungle", |
| 79 |
"Native US", "Cabaret", "New Wave", "Psychedelic", "Rave", |
| 80 |
"Showtunes", "Trailer", "Lo-Fi", "Tribal", "Acid Punk", "Acid Jazz", |
| 81 |
"Polka", "Retro", "Musical", "Rock & Roll", "Hard Rock", "Folk", |
| 82 |
"Folk-Rock", "National Folk", "Swing", "Fast Fusion", "Bebob", "Latin", |
| 83 |
"Revival", "Celtic", "Bluegrass", "Avantgarde", "Gothic Rock", |
| 84 |
"Progressive Rock", "Psychedelic Rock", "Symphonic Rock", "Slow Rock", |
| 85 |
"Big Band", "Chorus", "Easy Listening", "Acoustic", "Humour", "Speech", |
| 86 |
"Chanson", "Opera", "Chamber Music", "Sonata", "Symphony", "Booty Bass", |
| 87 |
"Primus", "Porn Groove", "Satire", "Slow Jam", "Club", "Tango", "Samba", |
| 88 |
"Folklore", "Ballad", "Power Ballad", "Rhythmic Soul", "Freestyle", "Duet", |
| 89 |
"Punk Rock", "Drum Solo", "A Cappella", "Euro-House", "Dance Hall", |
| 90 |
"Goa", "Drum & Bass", "Club-House", "Hardcore", "Terror", "Indie", |
| 91 |
"BritPop", "Negerpunk", "Polsk Punk", "Beat", "Christian Gangsta", |
| 92 |
"Heavy Metal", "Black Metal", "Crossover", "Contemporary Christian", |
| 93 |
"Christian Rock", "Merengue", "Salsa", "Thrash Metal", "Anime", "JPop", |
| 94 |
"SynthPop" |
| 95 |
}; |
| 96 |
|
| 97 |
#define GENRE_NAME_COUNT \ |
| 98 |
((int)(sizeof genre_names / sizeof (const char *const))) |
| 99 |
|
| 100 |
static const int genre_alpha_map[] = { |
| 101 |
123, 34, 74, 73, 99, 20, 40, 26, 145, 90, 116, 41, 135, 85, 96, 138, 89, 0, |
| 102 |
107, 132, 65, 88, 104, 102, 97, 136, 61, 141, 32, 1, 112, 128, 57, 140, 2, |
| 103 |
139, 58, 3, 125, 50, 22, 4, 55, 127, 122, 120, 98, 52, 48, 54, 124, 25, 84, |
| 104 |
80, 115, 81, 119, 5, 30, 36, 59, 126, 38, 49, 91, 6, 129, 79, 137, 7, 35, |
| 105 |
100, 131, 19, 33, 46, 47, 8, 29, 146, 63, 86, 71, 45, 142, 9, 77, 82, 64, |
| 106 |
133, 10, 66, 39, 11, 103, 12, 75, 134, 13, 53, 62, 109, 117, 23, 108, 92, |
| 107 |
67, 93, 43, 121, 15, 68, 14, 16, 76, 87, 118, 17, 78, 143, 114, 110, 69, 21, |
| 108 |
111, 95, 105, 42, 37, 24, 56, 44, 101, 83, 94, 106, 147, 113, 18, 51, 130, |
| 109 |
144, 60, 70, 31, 72, 27, 28 |
| 110 |
}; |
| 111 |
|
| 112 |
#define GENRE_ALPHA_COUNT ((int)(sizeof genre_alpha_map / sizeof (int))) |
| 113 |
|
| 114 |
#define GENRE_INDEX_OTHER 12 |
| 115 |
|
| 116 |
|
| 117 |
#define FRAME_ID(a, b, c, d) \ |
| 118 |
( ((unsigned long)(a) << 24) \ |
| 119 |
| ((unsigned long)(b) << 16) \ |
| 120 |
| ((unsigned long)(c) << 8) \ |
| 121 |
| ((unsigned long)(d) << 0) ) |
| 122 |
|
| 123 |
typedef enum UsualStringIDs { ID_TITLE = FRAME_ID('T', 'I', 'T', '2') |
| 124 |
, ID_ARTIST = FRAME_ID('T', 'P', 'E', '1') |
| 125 |
, ID_ALBUM = FRAME_ID('T', 'A', 'L', 'B') |
| 126 |
, ID_GENRE = FRAME_ID('T', 'C', 'O', 'N') |
| 127 |
, ID_ENCODER = FRAME_ID('T', 'S', 'S', 'E') |
| 128 |
, ID_PLAYLENGTH = FRAME_ID('T', 'L', 'E', 'N') |
| 129 |
, ID_COMMENT = FRAME_ID('C', 'O', 'M', 'M') /* full text string */ |
| 130 |
} UsualStringIDs; |
| 131 |
|
| 132 |
typedef enum NumericStringIDs { ID_DATE = FRAME_ID('T', 'D', 'A', 'T') /* "ddMM" */ |
| 133 |
, ID_TIME = FRAME_ID('T', 'I', 'M', 'E') /* "hhmm" */ |
| 134 |
, ID_TPOS = FRAME_ID('T', 'P', 'O', 'S') /* '0'-'9' and '/' allowed */ |
| 135 |
, ID_TRACK = FRAME_ID('T', 'R', 'C', 'K') /* '0'-'9' and '/' allowed */ |
| 136 |
, ID_YEAR = FRAME_ID('T', 'Y', 'E', 'R') /* "yyyy" */ |
| 137 |
} NumericStringIDs; |
| 138 |
|
| 139 |
typedef enum MiscIDs { ID_TXXX = FRAME_ID('T', 'X', 'X', 'X') |
| 140 |
, ID_WXXX = FRAME_ID('W', 'X', 'X', 'X') |
| 141 |
, ID_SYLT = FRAME_ID('S', 'Y', 'L', 'T') |
| 142 |
, ID_APIC = FRAME_ID('A', 'P', 'I', 'C') |
| 143 |
, ID_GEOB = FRAME_ID('G', 'E', 'O', 'B') |
| 144 |
, ID_PCNT = FRAME_ID('P', 'C', 'N', 'T') |
| 145 |
, ID_AENC = FRAME_ID('A', 'E', 'N', 'C') |
| 146 |
, ID_LINK = FRAME_ID('L', 'I', 'N', 'K') |
| 147 |
, ID_ENCR = FRAME_ID('E', 'N', 'C', 'R') |
| 148 |
, ID_GRID = FRAME_ID('G', 'R', 'I', 'D') |
| 149 |
, ID_PRIV = FRAME_ID('P', 'R', 'I', 'V') |
| 150 |
, ID_USLT = FRAME_ID('U', 'S', 'L', 'T') /* full text string */ |
| 151 |
, ID_USER = FRAME_ID('U', 'S', 'E', 'R') /* full text string */ |
| 152 |
, ID_PCST = FRAME_ID('P', 'C', 'S', 'T') /* iTunes Podcast indicator, only presence important */ |
| 153 |
, ID_WFED = FRAME_ID('W', 'F', 'E', 'D') /* iTunes Podcast URL as TEXT FRAME !!! violates standard */ |
| 154 |
} MiscIDs; |
| 155 |
|
| 156 |
|
| 157 |
static int |
| 158 |
frame_id_matches(int id, int mask) |
| 159 |
{ |
| 160 |
int result = 0, i, window = 0xff; |
| 161 |
for (i = 0; i < 4; ++i, window <<= 8) { |
| 162 |
int const mw = (mask & window); |
| 163 |
int const iw = (id & window); |
| 164 |
if (mw != 0 && mw != iw) { |
| 165 |
result |= iw; |
| 166 |
} |
| 167 |
} |
| 168 |
return result; |
| 169 |
} |
| 170 |
|
| 171 |
static int |
| 172 |
isFrameIdMatching(int id, int mask) |
| 173 |
{ |
| 174 |
return frame_id_matches(id, mask) == 0 ? 1 : 0; |
| 175 |
} |
| 176 |
|
| 177 |
static int |
| 178 |
test_tag_spec_flags(lame_internal_flags const *gfc, unsigned int tst) |
| 179 |
{ |
| 180 |
return (gfc->tag_spec.flags & tst) != 0u ? 1 : 0; |
| 181 |
} |
| 182 |
|
| 183 |
#if 0 |
| 184 |
static void |
| 185 |
debug_tag_spec_flags(lame_internal_flags * gfc, const char* info) |
| 186 |
{ |
| 187 |
MSGF(gfc, "%s\n", info); |
| 188 |
MSGF(gfc, "CHANGED_FLAG : %d\n", test_tag_spec_flags(gfc, CHANGED_FLAG )); |
| 189 |
MSGF(gfc, "ADD_V2_FLAG : %d\n", test_tag_spec_flags(gfc, ADD_V2_FLAG )); |
| 190 |
MSGF(gfc, "V1_ONLY_FLAG : %d\n", test_tag_spec_flags(gfc, V1_ONLY_FLAG )); |
| 191 |
MSGF(gfc, "V2_ONLY_FLAG : %d\n", test_tag_spec_flags(gfc, V2_ONLY_FLAG )); |
| 192 |
MSGF(gfc, "SPACE_V1_FLAG : %d\n", test_tag_spec_flags(gfc, SPACE_V1_FLAG)); |
| 193 |
MSGF(gfc, "PAD_V2_FLAG : %d\n", test_tag_spec_flags(gfc, PAD_V2_FLAG )); |
| 194 |
// adding debugging functionality for the ID3v2.4 flag |
| 195 |
MSGF(gfc, "V2_4_UTF8_FLAG: %d\n", test_tag_spec_flags(gfc, V2_4_UTF8_FLAG)); |
| 196 |
} |
| 197 |
#endif |
| 198 |
|
| 199 |
static int |
| 200 |
is_lame_internal_flags_null(lame_t gfp) |
| 201 |
{ |
| 202 |
return (gfp && gfp->internal_flags) ? 0 : 1; |
| 203 |
} |
| 204 |
|
| 205 |
// moved this function up so it can be used in copyV1ToV2 (for ID3v2.4 support) |
| 206 |
static char const* |
| 207 |
id3v2_get_language(lame_t gfp) |
| 208 |
{ |
| 209 |
lame_internal_flags const* gfc = gfp ? gfp->internal_flags : 0; |
| 210 |
if (gfc) return gfc->tag_spec.language; |
| 211 |
return 0; |
| 212 |
} |
| 213 |
|
| 214 |
static int |
| 215 |
id3v2_add_ucs2_lng(lame_t gfp, uint32_t frame_id, unsigned short const *desc, unsigned short const *text); |
| 216 |
static int |
| 217 |
id3v2_add_latin1_lng(lame_t gfp, uint32_t frame_id, char const *desc, char const *text); |
| 218 |
|
| 219 |
// forward-declare the id3v2_add_utf8_lng function to be used in copyV1ToV2 |
| 220 |
static int |
| 221 |
id3v2_add_utf8_lng(lame_t gfp, uint32_t frame_id, char const *desc, char const *text); |
| 222 |
|
| 223 |
|
| 224 |
static void |
| 225 |
copyV1ToV2(lame_t gfp, int frame_id, char const *s) |
| 226 |
{ |
| 227 |
lame_internal_flags *gfc = gfp != 0 ? gfp->internal_flags : 0; |
| 228 |
if (gfc != 0) { |
| 229 |
unsigned int flags = gfc->tag_spec.flags; |
| 230 |
|
| 231 |
// add V2 tag: either ID3v2.3 or ID3v2.4 according to |
| 232 |
// whether V2_4_UTF8_FLAG is used |
| 233 |
|
| 234 |
// if we are writing UTF-8 ID3v2.4 tag |
| 235 |
if (test_tag_spec_flags(gfc, V2_4_UTF8_FLAG)) { |
| 236 |
id3v2_add_utf8_lng(gfp, frame_id, 0, s); |
| 237 |
} else { // if we are writing latin1 ID3v2.3 tag |
| 238 |
id3v2_add_latin1_lng(gfp, frame_id, 0, s); |
| 239 |
} |
| 240 |
gfc->tag_spec.flags = flags; |
| 241 |
#if 0 |
| 242 |
debug_tag_spec_flags(gfc, "copyV1ToV2"); |
| 243 |
#endif |
| 244 |
} |
| 245 |
} |
| 246 |
|
| 247 |
|
| 248 |
static void |
| 249 |
id3v2AddLameVersion(lame_t gfp) |
| 250 |
{ |
| 251 |
char buffer[1024]; |
| 252 |
const char *b = get_lame_os_bitness(); |
| 253 |
const char *v = get_lame_version(); |
| 254 |
const char *u = get_lame_url(); |
| 255 |
const size_t lenb = strlen(b); |
| 256 |
|
| 257 |
if (lenb > 0) { |
| 258 |
sprintf(buffer, "LAME %s version %s (%s)", b, v, u); |
| 259 |
} |
| 260 |
else { |
| 261 |
sprintf(buffer, "LAME version %s (%s)", v, u); |
| 262 |
} |
| 263 |
copyV1ToV2(gfp, ID_ENCODER, buffer); |
| 264 |
} |
| 265 |
|
| 266 |
static void |
| 267 |
id3v2AddAudioDuration(lame_t gfp, double ms) |
| 268 |
{ |
| 269 |
SessionConfig_t const *const cfg = &gfp->internal_flags->cfg; /* caller checked pointers */ |
| 270 |
char buffer[1024]; |
| 271 |
double const max_ulong = MAX_U_32_NUM; |
| 272 |
unsigned long playlength_ms; |
| 273 |
|
| 274 |
ms *= 1000; |
| 275 |
ms /= cfg->samplerate_in; |
| 276 |
if (ms > max_ulong) { |
| 277 |
playlength_ms = max_ulong; |
| 278 |
} |
| 279 |
else if (ms < 0) { |
| 280 |
playlength_ms = 0; |
| 281 |
} |
| 282 |
else { |
| 283 |
playlength_ms = ms; |
| 284 |
} |
| 285 |
sprintf(buffer, "%lu", playlength_ms); |
| 286 |
copyV1ToV2(gfp, ID_PLAYLENGTH, buffer); |
| 287 |
} |
| 288 |
|
| 289 |
void |
| 290 |
id3tag_genre_list(void (*handler) (int, const char *, void *), void *cookie) |
| 291 |
{ |
| 292 |
if (handler) { |
| 293 |
int i; |
| 294 |
for (i = 0; i < GENRE_NAME_COUNT; ++i) { |
| 295 |
if (i < GENRE_ALPHA_COUNT) { |
| 296 |
int j = genre_alpha_map[i]; |
| 297 |
handler(j, genre_names[j], cookie); |
| 298 |
} |
| 299 |
} |
| 300 |
} |
| 301 |
} |
| 302 |
|
| 303 |
#define GENRE_NUM_UNKNOWN 255 |
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
void |
| 308 |
id3tag_init(lame_t gfp) |
| 309 |
{ |
| 310 |
lame_internal_flags *gfc = 0; |
| 311 |
|
| 312 |
if (is_lame_internal_flags_null(gfp)) { |
| 313 |
return; |
| 314 |
} |
| 315 |
gfc = gfp->internal_flags; |
| 316 |
free_id3tag(gfc); |
| 317 |
memset(&gfc->tag_spec, 0, sizeof gfc->tag_spec); |
| 318 |
gfc->tag_spec.genre_id3v1 = GENRE_NUM_UNKNOWN; |
| 319 |
gfc->tag_spec.padding_size = 128; |
| 320 |
id3v2AddLameVersion(gfp); |
| 321 |
} |
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
void |
| 326 |
id3tag_add_v2(lame_t gfp) |
| 327 |
{ |
| 328 |
lame_internal_flags *gfc = 0; |
| 329 |
|
| 330 |
if (is_lame_internal_flags_null(gfp)) { |
| 331 |
return; |
| 332 |
} |
| 333 |
gfc = gfp->internal_flags; |
| 334 |
gfc->tag_spec.flags &= ~V1_ONLY_FLAG; |
| 335 |
gfc->tag_spec.flags |= ADD_V2_FLAG; |
| 336 |
} |
| 337 |
|
| 338 |
/** |
| 339 |
* function to enable writing ID3v2.4 with UTF-8 characters |
| 340 |
* in addition to ID3v1 tags |
| 341 |
* */ |
| 342 |
void |
| 343 |
id3tag_add_v2_4_UTF8(lame_t gfp) |
| 344 |
{ |
| 345 |
lame_internal_flags *gfc = 0; |
| 346 |
|
| 347 |
if (is_lame_internal_flags_null(gfp)) { |
| 348 |
return; |
| 349 |
} |
| 350 |
gfc = gfp->internal_flags; |
| 351 |
// use both ID3v1 and ID3v2 tags |
| 352 |
gfc->tag_spec.flags &= ~V1_ONLY_FLAG; |
| 353 |
gfc->tag_spec.flags |= ADD_V2_FLAG; |
| 354 |
|
| 355 |
// for ID3v2 tags, specifically use ID3v2.4 with UTF-8 characters |
| 356 |
gfc->tag_spec.flags |= V2_4_UTF8_FLAG; |
| 357 |
} |
| 358 |
|
| 359 |
void |
| 360 |
id3tag_v1_only(lame_t gfp) |
| 361 |
{ |
| 362 |
lame_internal_flags *gfc = 0; |
| 363 |
|
| 364 |
if (is_lame_internal_flags_null(gfp)) { |
| 365 |
return; |
| 366 |
} |
| 367 |
gfc = gfp->internal_flags; |
| 368 |
gfc->tag_spec.flags &= ~(ADD_V2_FLAG | V2_ONLY_FLAG); |
| 369 |
gfc->tag_spec.flags |= V1_ONLY_FLAG; |
| 370 |
} |
| 371 |
|
| 372 |
void |
| 373 |
id3tag_v2_only(lame_t gfp) |
| 374 |
{ |
| 375 |
lame_internal_flags *gfc = 0; |
| 376 |
|
| 377 |
if (is_lame_internal_flags_null(gfp)) { |
| 378 |
return; |
| 379 |
} |
| 380 |
gfc = gfp->internal_flags; |
| 381 |
gfc->tag_spec.flags &= ~V1_ONLY_FLAG; |
| 382 |
gfc->tag_spec.flags |= V2_ONLY_FLAG; |
| 383 |
} |
| 384 |
|
| 385 |
/** |
| 386 |
* function to enable writing ID3v2.4 with UTF-8 characters |
| 387 |
* exclusively (without ID3v1 tags) |
| 388 |
* */ |
| 389 |
void |
| 390 |
id3tag_v2_4_UTF8_only(lame_t gfp) |
| 391 |
{ |
| 392 |
lame_internal_flags *gfc = 0; |
| 393 |
|
| 394 |
if (is_lame_internal_flags_null(gfp)) { |
| 395 |
return; |
| 396 |
} |
| 397 |
gfc = gfp->internal_flags; |
| 398 |
|
| 399 |
// use ID3v2 tags and not ID3v1 |
| 400 |
gfc->tag_spec.flags &= ~V1_ONLY_FLAG; |
| 401 |
gfc->tag_spec.flags |= V2_ONLY_FLAG; |
| 402 |
|
| 403 |
// for ID3v2 tags, specifically use ID3v2.4 with UTF-8 characters |
| 404 |
gfc->tag_spec.flags |= V2_4_UTF8_FLAG; |
| 405 |
} |
| 406 |
|
| 407 |
void |
| 408 |
id3tag_space_v1(lame_t gfp) |
| 409 |
{ |
| 410 |
lame_internal_flags *gfc = 0; |
| 411 |
|
| 412 |
if (is_lame_internal_flags_null(gfp)) { |
| 413 |
return; |
| 414 |
} |
| 415 |
gfc = gfp->internal_flags; |
| 416 |
gfc->tag_spec.flags &= ~V2_ONLY_FLAG; |
| 417 |
gfc->tag_spec.flags |= SPACE_V1_FLAG; |
| 418 |
} |
| 419 |
|
| 420 |
void |
| 421 |
id3tag_pad_v2(lame_t gfp) |
| 422 |
{ |
| 423 |
id3tag_set_pad(gfp, 128); |
| 424 |
} |
| 425 |
|
| 426 |
void |
| 427 |
id3tag_set_pad(lame_t gfp, size_t n) |
| 428 |
{ |
| 429 |
lame_internal_flags *gfc = 0; |
| 430 |
|
| 431 |
if (is_lame_internal_flags_null(gfp)) { |
| 432 |
return; |
| 433 |
} |
| 434 |
gfc = gfp->internal_flags; |
| 435 |
gfc->tag_spec.flags &= ~V1_ONLY_FLAG; |
| 436 |
gfc->tag_spec.flags |= PAD_V2_FLAG; |
| 437 |
gfc->tag_spec.flags |= ADD_V2_FLAG; |
| 438 |
gfc->tag_spec.padding_size = (unsigned int)n; |
| 439 |
} |
| 440 |
|
| 441 |
static int |
| 442 |
hasUcs2ByteOrderMarker(unsigned short bom) |
| 443 |
{ |
| 444 |
if (bom == 0xFFFEu || bom == 0xFEFFu) { |
| 445 |
return 1; |
| 446 |
} |
| 447 |
return 0; |
| 448 |
} |
| 449 |
|
| 450 |
|
| 451 |
static unsigned short |
| 452 |
swap_bytes(unsigned short w) |
| 453 |
{ |
| 454 |
return (0xff00u & (w << 8)) | (0x00ffu & (w >> 8)); |
| 455 |
} |
| 456 |
|
| 457 |
|
| 458 |
static unsigned short |
| 459 |
toLittleEndian(unsigned short bom, unsigned short c) |
| 460 |
{ |
| 461 |
if (bom == 0xFFFEu) { |
| 462 |
return swap_bytes(c); |
| 463 |
} |
| 464 |
return c; |
| 465 |
} |
| 466 |
|
| 467 |
static unsigned short |
| 468 |
fromLatin1Char(const unsigned short* s, unsigned short c) |
| 469 |
{ |
| 470 |
if (s[0] == 0xFFFEu) { |
| 471 |
return swap_bytes(c); |
| 472 |
} |
| 473 |
return c; |
| 474 |
} |
| 475 |
|
| 476 |
|
| 477 |
static size_t |
| 478 |
local_strdup(char **dst, const char *src) |
| 479 |
{ |
| 480 |
if (dst == 0) { |
| 481 |
return 0; |
| 482 |
} |
| 483 |
free(*dst); |
| 484 |
*dst = 0; |
| 485 |
if (src != 0) { |
| 486 |
size_t n; |
| 487 |
for (n = 0; src[n] != 0; ++n) { /* calc src string length */ |
| 488 |
} |
| 489 |
if (n > 0) { /* string length without zero termination */ |
| 490 |
assert(sizeof(*src) == sizeof(**dst)); |
| 491 |
*dst = lame_calloc(char, n + 1); |
| 492 |
if (*dst != 0) { |
| 493 |
memcpy(*dst, src, n * sizeof(**dst)); |
| 494 |
(*dst)[n] = 0; |
| 495 |
return n; |
| 496 |
} |
| 497 |
} |
| 498 |
} |
| 499 |
return 0; |
| 500 |
} |
| 501 |
|
| 502 |
static size_t |
| 503 |
local_ucs2_strdup(unsigned short **dst, unsigned short const *src) |
| 504 |
{ |
| 505 |
if (dst == 0) { |
| 506 |
return 0; |
| 507 |
} |
| 508 |
free(*dst); /* free old string pointer */ |
| 509 |
*dst = 0; |
| 510 |
if (src != 0) { |
| 511 |
size_t n; |
| 512 |
for (n = 0; src[n] != 0; ++n) { /* calc src string length */ |
| 513 |
} |
| 514 |
if (n > 0) { /* string length without zero termination */ |
| 515 |
assert(sizeof(*src) >= 2); |
| 516 |
assert(sizeof(*src) == sizeof(**dst)); |
| 517 |
*dst = lame_calloc(unsigned short, n + 1); |
| 518 |
if (*dst != 0) { |
| 519 |
memcpy(*dst, src, n * sizeof(**dst)); |
| 520 |
(*dst)[n] = 0; |
| 521 |
return n; |
| 522 |
} |
| 523 |
} |
| 524 |
} |
| 525 |
return 0; |
| 526 |
} |
| 527 |
|
| 528 |
|
| 529 |
static size_t |
| 530 |
local_ucs2_strlen(unsigned short const *s) |
| 531 |
{ |
| 532 |
size_t n = 0; |
| 533 |
if (s != 0) { |
| 534 |
while (*s++) { |
| 535 |
++n; |
| 536 |
} |
| 537 |
} |
| 538 |
return n; |
| 539 |
} |
| 540 |
|
| 541 |
|
| 542 |
static size_t |
| 543 |
local_ucs2_substr(unsigned short** dst, unsigned short const* src, size_t start, size_t end) |
| 544 |
{ |
| 545 |
size_t const len = 1 + 1 + ((start < end) ? (end - start) : 0); |
| 546 |
size_t n = 0; |
| 547 |
unsigned short *ptr = lame_calloc(unsigned short, len); |
| 548 |
*dst = ptr; |
| 549 |
if (ptr == 0 || src == 0) { |
| 550 |
return 0; |
| 551 |
} |
| 552 |
if (hasUcs2ByteOrderMarker(src[0])) { |
| 553 |
ptr[n++] = src[0]; |
| 554 |
if (start == 0) { |
| 555 |
++start; |
| 556 |
} |
| 557 |
} |
| 558 |
while (start < end) { |
| 559 |
ptr[n++] = src[start++]; |
| 560 |
} |
| 561 |
ptr[n] = 0; |
| 562 |
return n; |
| 563 |
} |
| 564 |
|
| 565 |
static int |
| 566 |
local_ucs2_pos(unsigned short const* str, unsigned short c) |
| 567 |
{ |
| 568 |
int i; |
| 569 |
for (i = 0; str != 0 && str[i] != 0; ++i) { |
| 570 |
if (str[i] == c) { |
| 571 |
return i; |
| 572 |
} |
| 573 |
} |
| 574 |
return -1; |
| 575 |
} |
| 576 |
|
| 577 |
static int |
| 578 |
local_char_pos(char const* str, char c) |
| 579 |
{ |
| 580 |
int i; |
| 581 |
for (i = 0; str != 0 && str[i] != 0; ++i) { |
| 582 |
if (str[i] == c) { |
| 583 |
return i; |
| 584 |
} |
| 585 |
} |
| 586 |
return -1; |
| 587 |
} |
| 588 |
|
| 589 |
static int |
| 590 |
maybeLatin1(unsigned short const* text) |
| 591 |
{ |
| 592 |
if (text) { |
| 593 |
unsigned short bom = *text++; |
| 594 |
while (*text) { |
| 595 |
unsigned short c = toLittleEndian(bom, *text++); |
| 596 |
if (c > 0x00fe) return 0; |
| 597 |
} |
| 598 |
} |
| 599 |
return 1; |
| 600 |
} |
| 601 |
|
| 602 |
static int searchGenre(char const* genre); |
| 603 |
static int sloppySearchGenre(char const* genre); |
| 604 |
|
| 605 |
static int |
| 606 |
lookupGenre(char const* genre) |
| 607 |
{ |
| 608 |
char *str; |
| 609 |
int num = strtol(genre, &str, 10); |
| 610 |
/* is the input a string or a valid number? */ |
| 611 |
if (*str) { |
| 612 |
num = searchGenre(genre); |
| 613 |
if (num == GENRE_NAME_COUNT) { |
| 614 |
num = sloppySearchGenre(genre); |
| 615 |
} |
| 616 |
if (num == GENRE_NAME_COUNT) { |
| 617 |
return -2; /* no common genre text found */ |
| 618 |
} |
| 619 |
} |
| 620 |
else { |
| 621 |
if ((num < 0) || (num >= GENRE_NAME_COUNT)) { |
| 622 |
return -1; /* number unknown */ |
| 623 |
} |
| 624 |
} |
| 625 |
return num; |
| 626 |
} |
| 627 |
|
| 628 |
static unsigned char * |
| 629 |
writeLoBytes(unsigned char *frame, unsigned short const *str, size_t n); |
| 630 |
|
| 631 |
static char* |
| 632 |
local_strdup_utf16_to_latin1(unsigned short const* utf16) |
| 633 |
{ |
| 634 |
size_t len = local_ucs2_strlen(utf16); |
| 635 |
unsigned char* latin1 = lame_calloc(unsigned char, len+1); |
| 636 |
writeLoBytes(latin1, utf16, len); |
| 637 |
return (char*)latin1; |
| 638 |
} |
| 639 |
|
| 640 |
|
| 641 |
static int |
| 642 |
id3tag_set_genre_utf8(lame_t gfp, unsigned short const* text) |
| 643 |
{ |
| 644 |
lame_internal_flags* gfc = gfp->internal_flags; |
| 645 |
int ret; |
| 646 |
if (text == 0) { |
| 647 |
return -3; |
| 648 |
} |
| 649 |
if (maybeLatin1(text)) { |
| 650 |
char* latin1 = local_strdup_utf16_to_latin1(text); |
| 651 |
int num = lookupGenre(latin1); |
| 652 |
free(latin1); |
| 653 |
if (num == -1) return -1; /* number out of range */ |
| 654 |
if (num >= 0) { /* common genre found */ |
| 655 |
gfc->tag_spec.flags |= CHANGED_FLAG; |
| 656 |
gfc->tag_spec.genre_id3v1 = num; |
| 657 |
copyV1ToV2(gfp, ID_GENRE, genre_names[num]); |
| 658 |
return 0; |
| 659 |
} |
| 660 |
} |
| •661 |
ret = id3v2_add_utf8_lng(gfp, ID_GENRE, 0, (const char *) text); |
| 662 |
if (ret == 0) { |
| 663 |
gfc->tag_spec.flags |= CHANGED_FLAG; |
| 664 |
gfc->tag_spec.genre_id3v1 = GENRE_INDEX_OTHER; |
| 665 |
} |
| 666 |
return ret; |
| 667 |
} |
| 668 |
|
| 669 |
static int |
| 670 |
id3tag_set_genre_utf16(lame_t gfp, unsigned short const* text) |
| 671 |
{ |
| 672 |
lame_internal_flags* gfc = gfp->internal_flags; |
| 673 |
int ret; |
| 674 |
if (text == 0) { |
| 675 |
return -3; |
| 676 |
} |
| 677 |
if (!hasUcs2ByteOrderMarker(text[0])) { |
| 678 |
return -3; |
| 679 |
} |
| 680 |
if (maybeLatin1(text)) { |
| 681 |
char* latin1 = local_strdup_utf16_to_latin1(text); |
| 682 |
int num = lookupGenre(latin1); |
| 683 |
free(latin1); |
| 684 |
if (num == -1) return -1; /* number out of range */ |
| 685 |
if (num >= 0) { /* common genre found */ |
| 686 |
gfc->tag_spec.flags |= CHANGED_FLAG; |
| 687 |
gfc->tag_spec.genre_id3v1 = num; |
| 688 |
copyV1ToV2(gfp, ID_GENRE, genre_names[num]); |
| 689 |
return 0; |
| 690 |
} |
| 691 |
} |
| 692 |
ret = id3v2_add_ucs2_lng(gfp, ID_GENRE, 0, text); |
| 693 |
if (ret == 0) { |
| 694 |
gfc->tag_spec.flags |= CHANGED_FLAG; |
| 695 |
gfc->tag_spec.genre_id3v1 = GENRE_INDEX_OTHER; |
| 696 |
} |
| 697 |
return ret; |
| 698 |
} |
| 699 |
|
| 700 |
/* |
| 701 |
Some existing options for ID3 tag can be specified by --tv option |
| 702 |
as follows. |
| 703 |
--tt <value>, --tv TIT2=value |
| 704 |
--ta <value>, --tv TPE1=value |
| 705 |
--tl <value>, --tv TALB=value |
| 706 |
--ty <value>, --tv TYER=value |
| 707 |
--tn <value>, --tv TRCK=value |
| 708 |
--tg <value>, --tv TCON=value |
| 709 |
(although some are not exactly same)*/ |
| 710 |
|
| 711 |
int |
| 712 |
id3tag_set_albumart(lame_t gfp, const char *image, size_t size) |
| 713 |
{ |
| 714 |
int mimetype = MIMETYPE_NONE; |
| 715 |
lame_internal_flags *gfc = 0; |
| 716 |
|
| 717 |
if (is_lame_internal_flags_null(gfp)) { |
| 718 |
return 0; |
| 719 |
} |
| 720 |
gfc = gfp->internal_flags; |
| 721 |
|
| 722 |
if (image != 0) { |
| 723 |
unsigned char const *data = (unsigned char const *) image; |
| 724 |
/* determine MIME type from the actual image data */ |
| 725 |
if (2 < size && data[0] == 0xFF && data[1] == 0xD8) { |
| 726 |
mimetype = MIMETYPE_JPEG; |
| 727 |
} |
| 728 |
else if (4 < size && data[0] == 0x89 && strncmp((const char *) &data[1], "PNG", 3) == 0) { |
| 729 |
mimetype = MIMETYPE_PNG; |
| 730 |
} |
| 731 |
else if (4 < size && strncmp((const char *) data, "GIF8", 4) == 0) { |
| 732 |
mimetype = MIMETYPE_GIF; |
| 733 |
} |
| 734 |
else { |
| 735 |
return -1; |
| 736 |
} |
| 737 |
} |
| 738 |
if (gfc->tag_spec.albumart != 0) { |
| 739 |
free(gfc->tag_spec.albumart); |
| 740 |
gfc->tag_spec.albumart = 0; |
| 741 |
gfc->tag_spec.albumart_size = 0; |
| 742 |
gfc->tag_spec.albumart_mimetype = MIMETYPE_NONE; |
| 743 |
} |
| 744 |
if (size < 1 || mimetype == MIMETYPE_NONE) { |
| 745 |
return 0; |
| 746 |
} |
| 747 |
gfc->tag_spec.albumart = lame_calloc(unsigned char, size); |
| 748 |
if (gfc->tag_spec.albumart != 0) { |
| 749 |
memcpy(gfc->tag_spec.albumart, image, size); |
| 750 |
gfc->tag_spec.albumart_size = (unsigned int)size; |
| 751 |
gfc->tag_spec.albumart_mimetype = mimetype; |
| 752 |
gfc->tag_spec.flags |= CHANGED_FLAG; |
| 753 |
id3tag_add_v2(gfp); |
| 754 |
} |
| 755 |
return 0; |
| 756 |
} |
| 757 |
|
| 758 |
static unsigned char * |
| 759 |
set_4_byte_value(unsigned char *bytes, uint32_t value) |
| 760 |
{ |
| 761 |
int i; |
| 762 |
for (i = 3; i >= 0; --i) { |
| 763 |
bytes[i] = value & 0xffUL; |
| 764 |
value >>= 8; |
| 765 |
} |
| 766 |
return bytes + 4; |
| 767 |
} |
| 768 |
|
| 769 |
static uint32_t |
| 770 |
toID3v2TagId(char const *s) |
| 771 |
{ |
| 772 |
unsigned int i, x = 0; |
| 773 |
if (s == 0) { |
| 774 |
return 0; |
| 775 |
} |
| 776 |
for (i = 0; i < 4 && s[i] != 0; ++i) { |
| 777 |
char const c = s[i]; |
| 778 |
unsigned int const u = 0x0ff & c; |
| 779 |
x <<= 8; |
| 780 |
x |= u; |
| 781 |
if (c < 'A' || 'Z' < c) { |
| 782 |
if (c < '0' || '9' < c) { |
| 783 |
return 0; |
| 784 |
} |
| 785 |
} |
| 786 |
} |
| 787 |
return x; |
| 788 |
} |
| 789 |
|
| 790 |
static uint32_t |
| 791 |
toID3v2TagId_ucs2(unsigned short const *s) |
| 792 |
{ |
| 793 |
unsigned int i, x = 0; |
| 794 |
unsigned short bom = 0; |
| 795 |
if (s == 0) { |
| 796 |
return 0; |
| 797 |
} |
| 798 |
bom = s[0]; |
| 799 |
if (hasUcs2ByteOrderMarker(bom)) { |
| 800 |
++s; |
| 801 |
} |
| 802 |
for (i = 0; i < 4 && s[i] != 0; ++i) { |
| 803 |
unsigned short const c = toLittleEndian(bom, s[i]); |
| 804 |
if (c < 'A' || 'Z' < c) { |
| 805 |
if (c < '0' || '9' < c) { |
| 806 |
return 0; |
| 807 |
} |
| 808 |
} |
| 809 |
x <<= 8; |
| 810 |
x |= c; |
| 811 |
} |
| 812 |
return x; |
| 813 |
} |
| 814 |
|
| 815 |
#if 0 |
| 816 |
static int |
| 817 |
isNumericString(uint32_t frame_id) |
| 818 |
{ |
| 819 |
switch (frame_id) { |
| 820 |
case ID_DATE: |
| 821 |
case ID_TIME: |
| 822 |
case ID_TPOS: |
| 823 |
case ID_TRACK: |
| 824 |
case ID_YEAR: |
| 825 |
return 1; |
| 826 |
} |
| 827 |
return 0; |
| 828 |
} |
| 829 |
#endif |
| 830 |
|
| 831 |
static int |
| 832 |
isMultiFrame(uint32_t frame_id) |
| 833 |
{ |
| 834 |
switch (frame_id) { |
| 835 |
case ID_TXXX: |
| 836 |
case ID_WXXX: |
| 837 |
case ID_COMMENT: |
| 838 |
case ID_SYLT: |
| 839 |
case ID_APIC: |
| 840 |
case ID_GEOB: |
| 841 |
case ID_PCNT: |
| 842 |
case ID_AENC: |
| 843 |
case ID_LINK: |
| 844 |
case ID_ENCR: |
| 845 |
case ID_GRID: |
| 846 |
case ID_PRIV: |
| 847 |
return 1; |
| 848 |
} |
| 849 |
return 0; |
| 850 |
} |
| 851 |
|
| 852 |
#if 0 |
| 853 |
static int |
| 854 |
isFullTextString(int frame_id) |
| 855 |
{ |
| 856 |
switch (frame_id) { |
| 857 |
case ID_VSLT: |
| 858 |
case ID_COMMENT: |
| 859 |
return 1; |
| 860 |
} |
| 861 |
return 0; |
| 862 |
} |
| 863 |
#endif |
| 864 |
|
| 865 |
static FrameDataNode * |
| 866 |
findNode(id3tag_spec const *tag, uint32_t frame_id, FrameDataNode const *last) |
| 867 |
{ |
| 868 |
FrameDataNode *node = last ? last->nxt : tag->v2_head; |
| 869 |
while (node != 0) { |
| 870 |
if (node->fid == frame_id) { |
| 871 |
return node; |
| 872 |
} |
| 873 |
node = node->nxt; |
| 874 |
} |
| 875 |
return 0; |
| 876 |
} |
| 877 |
|
| 878 |
static void |
| 879 |
appendNode(id3tag_spec * tag, FrameDataNode * node) |
| 880 |
{ |
| 881 |
if (tag->v2_tail == 0 || tag->v2_head == 0) { |
| 882 |
tag->v2_head = node; |
| 883 |
tag->v2_tail = node; |
| 884 |
} |
| 885 |
else { |
| 886 |
tag->v2_tail->nxt = node; |
| 887 |
tag->v2_tail = node; |
| 888 |
} |
| 889 |
} |
| 890 |
|
| 891 |
static void |
| 892 |
setLang(char *dst, char const *src) |
| 893 |
{ |
| 894 |
int i; |
| 895 |
if (src == 0 || src[0] == 0) { |
| 896 |
dst[0] = 'e'; |
| 897 |
dst[1] = 'n'; |
| 898 |
dst[2] = 'g'; |
| 899 |
} |
| 900 |
else { |
| 901 |
for (i = 0; i < 3 && src && *src; ++i) { |
| 902 |
dst[i] = src[i]; |
| 903 |
} |
| 904 |
for (; i < 3; ++i) { |
| 905 |
dst[i] = ' '; |
| 906 |
} |
| 907 |
} |
| 908 |
} |
| 909 |
|
| 910 |
static int |
| 911 |
isSameLang(char const *l1, char const *l2) |
| 912 |
{ |
| 913 |
char d[3]; |
| 914 |
int i; |
| 915 |
setLang(d, l2); |
| 916 |
for (i = 0; i < 3; ++i) { |
| 917 |
char a = tolower(l1[i]); |
| 918 |
char b = tolower(d[i]); |
| 919 |
if (a < ' ') |
| 920 |
a = ' '; |
| 921 |
if (b < ' ') |
| 922 |
b = ' '; |
| 923 |
if (a != b) { |
| 924 |
return 0; |
| 925 |
} |
| 926 |
} |
| 927 |
return 1; |
| 928 |
} |
| 929 |
|
| 930 |
static int |
| 931 |
isSameDescriptor(FrameDataNode const *node, char const *dsc) |
| 932 |
{ |
| 933 |
size_t i; |
| 934 |
if (node->dsc.enc == 1 && node->dsc.dim > 0) { |
| 935 |
return 0; |
| 936 |
} |
| 937 |
for (i = 0; i < node->dsc.dim; ++i) { |
| 938 |
if (!dsc || node->dsc.ptr.l[i] != dsc[i]) { |
| 939 |
return 0; |
| 940 |
} |
| 941 |
} |
| 942 |
return 1; |
| 943 |
} |
| 944 |
|
| 945 |
static int |
| 946 |
isSameDescriptorUcs2(FrameDataNode const *node, unsigned short const *dsc) |
| 947 |
{ |
| 948 |
size_t i; |
| 949 |
if (node->dsc.enc != 1 && node->dsc.dim > 0) { |
| 950 |
return 0; |
| 951 |
} |
| 952 |
for (i = 0; i < node->dsc.dim; ++i) { |
| 953 |
if (!dsc || node->dsc.ptr.u[i] != dsc[i]) { |
| 954 |
return 0; |
| 955 |
} |
| 956 |
} |
| 957 |
return 1; |
| 958 |
} |
| 959 |
|
| 960 |
static int |
| 961 |
id3v2_add_ucs2(lame_t gfp, uint32_t frame_id, char const *lng, unsigned short const *desc, unsigned short const *text) |
| 962 |
{ |
| 963 |
lame_internal_flags *gfc = gfp != 0 ? gfp->internal_flags : 0; |
| 964 |
if (gfc != 0) { |
| 965 |
FrameDataNode *node = findNode(&gfc->tag_spec, frame_id, 0); |
| 966 |
char lang[4]; |
| 967 |
setLang(lang, lng); |
| 968 |
if (isMultiFrame(frame_id)) { |
| 969 |
while (node) { |
| 970 |
if (isSameLang(node->lng, lang)) { |
| 971 |
if (isSameDescriptorUcs2(node, desc)) { |
| 972 |
break; |
| 973 |
} |
| 974 |
} |
| 975 |
node = findNode(&gfc->tag_spec, frame_id, node); |
| 976 |
} |
| 977 |
} |
| 978 |
if (node == 0) { |
| 979 |
node = lame_calloc(FrameDataNode, 1); |
| 980 |
if (node == 0) { |
| 981 |
return -254; /* memory problem */ |
| 982 |
} |
| 983 |
appendNode(&gfc->tag_spec, node); |
| 984 |
} |
| 985 |
node->fid = frame_id; |
| 986 |
setLang(node->lng, lang); |
| 987 |
node->dsc.dim = local_ucs2_strdup(&node->dsc.ptr.u, desc); |
| 988 |
node->dsc.enc = 1; |
| 989 |
node->txt.dim = local_ucs2_strdup(&node->txt.ptr.u, text); |
| 990 |
node->txt.enc = 1; |
| 991 |
gfc->tag_spec.flags |= (CHANGED_FLAG | ADD_V2_FLAG); |
| 992 |
return 0; |
| 993 |
} |
| 994 |
return -255; |
| 995 |
} |
| 996 |
|
| 997 |
/* |
| 998 |
* internal helper, adds encoded text to node. |
| 999 |
* For latin1 (enc=0) or UTF-8 (enc=3). |
| 1000 |
*/ |
| 1001 |
static int |
| 1002 |
id3v2_add_enc(lame_t gfp, uint32_t frame_id, char const *lng, char const *desc, char const *text, int enc) |
| 1003 |
{ |
| 1004 |
lame_internal_flags *gfc = gfp != 0 ? gfp->internal_flags : 0; |
| 1005 |
if (gfc != 0) { |
| 1006 |
FrameDataNode *node = findNode(&gfc->tag_spec, frame_id, 0); |
| 1007 |
char lang[4]; |
| 1008 |
setLang(lang, lng); |
| 1009 |
if (isMultiFrame(frame_id)) { |
| 1010 |
while (node) { |
| 1011 |
if (isSameLang(node->lng, lang)) { |
| 1012 |
if (isSameDescriptor(node, desc)) { |
| 1013 |
break; |
| 1014 |
} |
| 1015 |
} |
| 1016 |
node = findNode(&gfc->tag_spec, frame_id, node); |
| 1017 |
} |
| 1018 |
} |
| 1019 |
if (node == 0) { |
| 1020 |
node = lame_calloc(FrameDataNode, 1); |
| 1021 |
if (node == 0) { |
| 1022 |
return -254; /* memory problem */ |
| 1023 |
} |
| 1024 |
appendNode(&gfc->tag_spec, node); |
| 1025 |
} |
| 1026 |
node->fid = frame_id; |
| 1027 |
setLang(node->lng, lang); |
| 1028 |
node->dsc.dim = local_strdup(&node->dsc.ptr.l, desc); |
| 1029 |
node->dsc.enc = enc; // set specified encoding |
| 1030 |
node->txt.dim = local_strdup(&node->txt.ptr.l, text); |
| 1031 |
node->txt.enc = enc; // set specified encoding |
| 1032 |
gfc->tag_spec.flags |= (CHANGED_FLAG | ADD_V2_FLAG); |
| 1033 |
return 0; |
| 1034 |
} |
| 1035 |
return -255; |
| 1036 |
} |
| 1037 |
|
| 1038 |
// this function now uses id3v2_add_enc |
| 1039 |
static int |
| 1040 |
id3v2_add_latin1(lame_t gfp, uint32_t frame_id, char const *lng, char const *desc, char const *text) |
| 1041 |
{ |
| 1042 |
// call internal helper with encoding = 0 (which indicates latin1) |
| 1043 |
int enc = 0; |
| 1044 |
return id3v2_add_enc(gfp, frame_id, lng, desc, text, enc); |
| 1045 |
} |
| 1046 |
|
| 1047 |
// function to add UTF-8-encoded text to node |
| 1048 |
static int |
| 1049 |
id3v2_add_utf8(lame_t gfp, uint32_t frame_id, char const *lng, char const *desc, char const *text) |
| 1050 |
{ |
| 1051 |
// call internal helper with encoding = 3 (which indicates UTF8) |
| 1052 |
int enc = 3; |
| 1053 |
return id3v2_add_enc(gfp, frame_id, lng, desc, text, enc); |
| 1054 |
} |
| 1055 |
|
| 1056 |
static int |
| 1057 |
id3v2_add_utf8_lng(lame_t gfp, uint32_t frame_id, char const *desc, char const *text) |
| 1058 |
{ |
| 1059 |
char const* lang = id3v2_get_language(gfp); |
| 1060 |
return id3v2_add_utf8(gfp, frame_id, lang, desc, text); |
| 1061 |
} |
| 1062 |
|
| 1063 |
static int |
| 1064 |
id3v2_add_ucs2_lng(lame_t gfp, uint32_t frame_id, unsigned short const *desc, unsigned short const *text) |
| 1065 |
{ |
| 1066 |
char const* lang = id3v2_get_language(gfp); |
| 1067 |
return id3v2_add_ucs2(gfp, frame_id, lang, desc, text); |
| 1068 |
} |
| 1069 |
|
| 1070 |
static int |
| 1071 |
id3v2_add_latin1_lng(lame_t gfp, uint32_t frame_id, char const *desc, char const *text) |
| 1072 |
{ |
| 1073 |
char const* lang = id3v2_get_language(gfp); |
| 1074 |
return id3v2_add_latin1(gfp, frame_id, lang, desc, text); |
| 1075 |
} |
| 1076 |
|
| 1077 |
static int |
| 1078 |
id3tag_set_userinfo_latin1(lame_t gfp, uint32_t id, char const *fieldvalue) |
| 1079 |
{ |
| 1080 |
char const separator = '='; |
| 1081 |
int rc = -7; |
| 1082 |
int a = local_char_pos(fieldvalue, separator); |
| 1083 |
if (a >= 0) { |
| 1084 |
char* dup = 0; |
| 1085 |
local_strdup(&dup, fieldvalue); |
| 1086 |
dup[a] = 0; |
| 1087 |
rc = id3v2_add_latin1_lng(gfp, id, dup, dup+a+1); |
| 1088 |
free(dup); |
| 1089 |
} |
| 1090 |
return rc; |
| 1091 |
} |
| 1092 |
|
| 1093 |
static int |
| 1094 |
id3tag_set_userinfo_utf8(lame_t gfp, uint32_t id, char const *fieldvalue) |
| 1095 |
{ |
| 1096 |
char const separator = '='; |
| 1097 |
int rc = -7; |
| 1098 |
int a = local_char_pos(fieldvalue, separator); |
| 1099 |
if (a >= 0) { |
| 1100 |
char* dup = 0; |
| 1101 |
local_strdup(&dup, fieldvalue); |
| 1102 |
dup[a] = 0; |
| 1103 |
rc = id3v2_add_utf8_lng(gfp, id, dup, dup+a+1); |
| 1104 |
free(dup); |
| 1105 |
} |
| 1106 |
return rc; |
| 1107 |
} |
| 1108 |
|
| 1109 |
static int |
| 1110 |
id3tag_set_userinfo_ucs2(lame_t gfp, uint32_t id, unsigned short const *fieldvalue) |
| 1111 |
{ |
| 1112 |
unsigned short const separator = fromLatin1Char(fieldvalue,'='); |
| 1113 |
int rc = -7; |
| 1114 |
size_t b = local_ucs2_strlen(fieldvalue); |
| 1115 |
int a = local_ucs2_pos(fieldvalue, separator); |
| 1116 |
if (a >= 0) { |
| 1117 |
unsigned short* dsc = 0, *val = 0; |
| 1118 |
local_ucs2_substr(&dsc, fieldvalue, 0, a); |
| 1119 |
local_ucs2_substr(&val, fieldvalue, a+1, b); |
| 1120 |
rc = id3v2_add_ucs2_lng(gfp, id, dsc, val); |
| 1121 |
free(dsc); |
| 1122 |
free(val); |
| 1123 |
} |
| 1124 |
return rc; |
| 1125 |
} |
| 1126 |
|
| 1127 |
int |
| 1128 |
id3tag_set_textinfo_utf8(lame_t gfp, char const *id, unsigned short const *text) |
| 1129 |
{ |
| 1130 |
uint32_t const frame_id = toID3v2TagId(id); |
| 1131 |
if (frame_id == 0) { |
| 1132 |
return -1; |
| 1133 |
} |
| 1134 |
if (is_lame_internal_flags_null(gfp)) { |
| 1135 |
return 0; |
| 1136 |
} |
| 1137 |
if (text == 0) { |
| 1138 |
return 0; |
| 1139 |
} |
| 1140 |
if (frame_id == ID_TXXX || frame_id == ID_WXXX || frame_id == ID_COMMENT) { |
| •1141 |
return id3tag_set_userinfo_utf8(gfp, frame_id, (const char *) text); |
| 1142 |
} |
| 1143 |
if (frame_id == ID_GENRE) { |
| 1144 |
return id3tag_set_genre_utf8(gfp, text); |
| 1145 |
} |
| 1146 |
if (frame_id == ID_PCST) { |
| •1147 |
return id3v2_add_utf8_lng(gfp, frame_id, 0, (const char *) text); |
| 1148 |
} |
| 1149 |
if (frame_id == ID_USER) { |
| •1150 |
return id3v2_add_utf8_lng(gfp, frame_id, (const char *) text, 0); |
| 1151 |
} |
| 1152 |
if (frame_id == ID_WFED) { |
| •1153 |
return id3v2_add_utf8_lng(gfp, frame_id, (const char *) text, 0); /* iTunes expects WFED to be a text frame */ |
| 1154 |
} |
| 1155 |
if (isFrameIdMatching(frame_id, FRAME_ID('T', 0, 0, 0)) |
| 1156 |
||isFrameIdMatching(frame_id, FRAME_ID('W', 0, 0, 0))) { |
| 1157 |
#if 0 |
| 1158 |
if (isNumericString(frame_id)) { |
| 1159 |
return -2; /* must be Latin-1 encoded */ |
| 1160 |
} |
| 1161 |
#endif |
| •1162 |
return id3v2_add_utf8_lng(gfp, frame_id, 0, (const char *) text); |
| 1163 |
} |
| 1164 |
return -255; /* not supported by now */ |
| 1165 |
} |
| 1166 |
|
| 1167 |
int |
| 1168 |
id3tag_set_textinfo_utf16(lame_t gfp, char const *id, unsigned short const *text) |
| 1169 |
{ |
| 1170 |
uint32_t const frame_id = toID3v2TagId(id); |
| 1171 |
if (frame_id == 0) { |
| 1172 |
return -1; |
| 1173 |
} |
| 1174 |
if (is_lame_internal_flags_null(gfp)) { |
| 1175 |
return 0; |
| 1176 |
} |
| 1177 |
if (text == 0) { |
| 1178 |
return 0; |
| 1179 |
} |
| 1180 |
if (!hasUcs2ByteOrderMarker(text[0])) { |
| 1181 |
return -3; /* BOM missing */ |
| 1182 |
} |
| 1183 |
if (frame_id == ID_TXXX || frame_id == ID_WXXX || frame_id == ID_COMMENT) { |
| 1184 |
return id3tag_set_userinfo_ucs2(gfp, frame_id, text); |
| 1185 |
} |
| 1186 |
if (frame_id == ID_GENRE) { |
| 1187 |
return id3tag_set_genre_utf16(gfp, text); |
| 1188 |
} |
| 1189 |
if (frame_id == ID_PCST) { |
| 1190 |
return id3v2_add_ucs2_lng(gfp, frame_id, 0, text); |
| 1191 |
} |
| 1192 |
if (frame_id == ID_USER) { |
| 1193 |
return id3v2_add_ucs2_lng(gfp, frame_id, text, 0); |
| 1194 |
} |
| 1195 |
if (frame_id == ID_WFED) { |
| 1196 |
return id3v2_add_ucs2_lng(gfp, frame_id, text, 0); /* iTunes expects WFED to be a text frame */ |
| 1197 |
} |
| 1198 |
if (isFrameIdMatching(frame_id, FRAME_ID('T', 0, 0, 0)) |
| 1199 |
||isFrameIdMatching(frame_id, FRAME_ID('W', 0, 0, 0))) { |
| 1200 |
#if 0 |
| 1201 |
if (isNumericString(frame_id)) { |
| 1202 |
return -2; /* must be Latin-1 encoded */ |
| 1203 |
} |
| 1204 |
#endif |
| 1205 |
return id3v2_add_ucs2_lng(gfp, frame_id, 0, text); |
| 1206 |
} |
| 1207 |
return -255; /* not supported by now */ |
| 1208 |
} |
| 1209 |
|
| 1210 |
extern int |
| 1211 |
id3tag_set_textinfo_ucs2(lame_t gfp, char const *id, unsigned short const *text); |
| 1212 |
|
| 1213 |
int |
| 1214 |
id3tag_set_textinfo_ucs2(lame_t gfp, char const *id, unsigned short const *text) |
| 1215 |
{ |
| 1216 |
return id3tag_set_textinfo_utf16(gfp, id, text); |
| 1217 |
} |
| 1218 |
|
| 1219 |
int |
| 1220 |
id3tag_set_textinfo_latin1(lame_t gfp, char const *id, char const *text) |
| 1221 |
{ |
| 1222 |
uint32_t const frame_id = toID3v2TagId(id); |
| 1223 |
if (frame_id == 0) { |
| 1224 |
return -1; |
| 1225 |
} |
| 1226 |
if (is_lame_internal_flags_null(gfp)) { |
| 1227 |
return 0; |
| 1228 |
} |
| 1229 |
if (text == 0) { |
| 1230 |
return 0; |
| 1231 |
} |
| 1232 |
if (frame_id == ID_TXXX || frame_id == ID_WXXX || frame_id == ID_COMMENT) { |
| 1233 |
return id3tag_set_userinfo_latin1(gfp, frame_id, text); |
| 1234 |
} |
| 1235 |
if (frame_id == ID_GENRE) { |
| 1236 |
return id3tag_set_genre(gfp, text); |
| 1237 |
} |
| 1238 |
if (frame_id == ID_PCST) { |
| 1239 |
return id3v2_add_latin1_lng(gfp, frame_id, 0, text); |
| 1240 |
} |
| 1241 |
if (frame_id == ID_USER) { |
| 1242 |
return id3v2_add_latin1_lng(gfp, frame_id, text, 0); |
| 1243 |
} |
| 1244 |
if (frame_id == ID_WFED) { |
| 1245 |
return id3v2_add_latin1_lng(gfp, frame_id, text, 0); /* iTunes expects WFED to be a text frame */ |
| 1246 |
} |
| 1247 |
if (isFrameIdMatching(frame_id, FRAME_ID('T', 0, 0, 0)) |
| 1248 |
||isFrameIdMatching(frame_id, FRAME_ID('W', 0, 0, 0))) { |
| 1249 |
return id3v2_add_latin1_lng(gfp, frame_id, 0, text); |
| 1250 |
} |
| 1251 |
return -255; /* not supported by now */ |
| 1252 |
} |
| 1253 |
|
| 1254 |
|
| 1255 |
int |
| 1256 |
id3tag_set_comment_latin1(lame_t gfp, char const *lang, char const *desc, char const *text) |
| 1257 |
{ |
| 1258 |
if (is_lame_internal_flags_null(gfp)) { |
| 1259 |
return 0; |
| 1260 |
} |
| 1261 |
return id3v2_add_latin1(gfp, ID_COMMENT, lang, desc, text); |
| 1262 |
} |
| 1263 |
|
| 1264 |
int |
| 1265 |
id3tag_set_comment_utf8(lame_t gfp, char const *lang, char const *desc, char const *text) |
| 1266 |
{ |
| 1267 |
if (is_lame_internal_flags_null(gfp)) { |
| 1268 |
return 0; |
| 1269 |
} |
| 1270 |
return id3v2_add_utf8(gfp, ID_COMMENT, lang, desc, text); |
| 1271 |
} |
| 1272 |
|
| 1273 |
|
| 1274 |
int |
| 1275 |
id3tag_set_comment_utf16(lame_t gfp, char const *lang, unsigned short const *desc, unsigned short const *text) |
| 1276 |
{ |
| 1277 |
if (is_lame_internal_flags_null(gfp)) { |
| 1278 |
return 0; |
| 1279 |
} |
| 1280 |
return id3v2_add_ucs2(gfp, ID_COMMENT, lang, desc, text); |
| 1281 |
} |
| 1282 |
|
| 1283 |
extern int |
| 1284 |
id3tag_set_comment_ucs2(lame_t gfp, char const *lang, unsigned short const *desc, unsigned short const *text); |
| 1285 |
|
| 1286 |
|
| 1287 |
int |
| 1288 |
id3tag_set_comment_ucs2(lame_t gfp, char const *lang, unsigned short const *desc, unsigned short const *text) |
| 1289 |
{ |
| 1290 |
if (is_lame_internal_flags_null(gfp)) { |
| 1291 |
return 0; |
| 1292 |
} |
| 1293 |
return id3tag_set_comment_utf16(gfp, lang, desc, text); |
| 1294 |
} |
| 1295 |
|
| 1296 |
|
| 1297 |
void |
| 1298 |
id3tag_set_title(lame_t gfp, const char *title) |
| 1299 |
{ |
| 1300 |
lame_internal_flags *gfc = gfp != 0 ? gfp->internal_flags : 0; |
| 1301 |
if (gfc && title && *title) { |
| 1302 |
local_strdup(&gfc->tag_spec.title, title); |
| 1303 |
gfc->tag_spec.flags |= CHANGED_FLAG; |
| 1304 |
copyV1ToV2(gfp, ID_TITLE, title); |
| 1305 |
} |
| 1306 |
} |
| 1307 |
|
| 1308 |
void |
| 1309 |
id3tag_set_artist(lame_t gfp, const char *artist) |
| 1310 |
{ |
| 1311 |
lame_internal_flags *gfc = gfp != 0 ? gfp->internal_flags : 0; |
| 1312 |
if (gfc && artist && *artist) { |
| 1313 |
local_strdup(&gfc->tag_spec.artist, artist); |
| 1314 |
gfc->tag_spec.flags |= CHANGED_FLAG; |
| 1315 |
copyV1ToV2(gfp, ID_ARTIST, artist); |
| 1316 |
} |
| 1317 |
} |
| 1318 |
|
| 1319 |
void |
| 1320 |
id3tag_set_album(lame_t gfp, const char *album) |
| 1321 |
{ |
| 1322 |
lame_internal_flags *gfc = gfp != 0 ? gfp->internal_flags : 0; |
| 1323 |
if (gfc && album && *album) { |
| 1324 |
local_strdup(&gfc->tag_spec.album, album); |
| 1325 |
gfc->tag_spec.flags |= CHANGED_FLAG; |
| 1326 |
copyV1ToV2(gfp, ID_ALBUM, album); |
| 1327 |
} |
| 1328 |
} |
| 1329 |
|
| 1330 |
void |
| 1331 |
id3tag_set_year(lame_t gfp, const char *year) |
| 1332 |
{ |
| 1333 |
lame_internal_flags *gfc = gfp != 0 ? gfp->internal_flags : 0; |
| 1334 |
if (gfc && year && *year) { |
| 1335 |
int num = atoi(year); |
| 1336 |
if (num < 0) { |
| 1337 |
num = 0; |
| 1338 |
} |
| 1339 |
/* limit a year to 4 digits so it fits in a version 1 tag */ |
| 1340 |
if (num > 9999) { |
| 1341 |
num = 9999; |
| 1342 |
} |
| 1343 |
if (num) { |
| 1344 |
gfc->tag_spec.year = num; |
| 1345 |
gfc->tag_spec.flags |= CHANGED_FLAG; |
| 1346 |
} |
| 1347 |
copyV1ToV2(gfp, ID_YEAR, year); |
| 1348 |
} |
| 1349 |
} |
| 1350 |
|
| 1351 |
void |
| 1352 |
id3tag_set_comment(lame_t gfp, const char *comment) |
| 1353 |
{ |
| 1354 |
lame_internal_flags *gfc = gfp != 0 ? gfp->internal_flags : 0; |
| 1355 |
if (gfc && comment && *comment) { |
| 1356 |
local_strdup(&gfc->tag_spec.comment, comment); |
| 1357 |
gfc->tag_spec.flags |= CHANGED_FLAG; |
| 1358 |
{ |
| 1359 |
uint32_t const flags = gfc->tag_spec.flags; |
| 1360 |
|
| 1361 |
// add V2 comment tag: either ID3v2.3 or ID3v2.4 |
| 1362 |
// according to whether V2_4_UTF8_FLAG is used |
| 1363 |
|
| 1364 |
// if we are writing UTF-8 ID3v2.4 tag |
| 1365 |
if (test_tag_spec_flags(gfc, V2_4_UTF8_FLAG)) { |
| 1366 |
char const* lang = id3v2_get_language(gfp); |
| 1367 |
id3v2_add_utf8(gfp, ID_COMMENT, lang, "", comment); |
| 1368 |
} else { // if we are writing latin1 ID3v2.3 tag |
| 1369 |
id3v2_add_latin1_lng(gfp, ID_COMMENT, "", comment); |
| 1370 |
} |
| 1371 |
|
| 1372 |
gfc->tag_spec.flags = flags; |
| 1373 |
} |
| 1374 |
} |
| 1375 |
} |
| 1376 |
|
| 1377 |
int |
| 1378 |
id3tag_set_track(lame_t gfp, const char *track) |
| 1379 |
{ |
| 1380 |
char const *trackcount; |
| 1381 |
lame_internal_flags *gfc = gfp != 0 ? gfp->internal_flags : 0; |
| 1382 |
int ret = 0; |
| 1383 |
|
| 1384 |
if (gfc && track && *track) { |
| 1385 |
int num = atoi(track); |
| 1386 |
/* check for valid ID3v1 track number range */ |
| 1387 |
if (num < 1 || num > 255) { |
| 1388 |
num = 0; |
| 1389 |
ret = -1; /* track number out of ID3v1 range, ignored for ID3v1 */ |
| 1390 |
gfc->tag_spec.flags |= (CHANGED_FLAG | ADD_V2_FLAG); |
| 1391 |
} |
| 1392 |
if (num) { |
| 1393 |
gfc->tag_spec.track_id3v1 = num; |
| 1394 |
gfc->tag_spec.flags |= CHANGED_FLAG; |
| 1395 |
} |
| 1396 |
/* Look for the total track count after a "/", same restrictions */ |
| 1397 |
trackcount = strchr(track, '/'); |
| 1398 |
if (trackcount && *trackcount) { |
| 1399 |
gfc->tag_spec.flags |= (CHANGED_FLAG | ADD_V2_FLAG); |
| 1400 |
} |
| 1401 |
copyV1ToV2(gfp, ID_TRACK, track); |
| 1402 |
} |
| 1403 |
return ret; |
| 1404 |
} |
| 1405 |
|
| 1406 |
/* would use real "strcasecmp" but it isn't portable */ |
| 1407 |
static int |
| 1408 |
local_strcasecmp(const char *s1, const char *s2) |
| 1409 |
{ |
| 1410 |
unsigned char c1; |
| 1411 |
unsigned char c2; |
| 1412 |
do { |
| 1413 |
c1 = tolower(*s1); |
| 1414 |
c2 = tolower(*s2); |
| 1415 |
if (!c1) { |
| 1416 |
break; |
| 1417 |
} |
| 1418 |
++s1; |
| 1419 |
++s2; |
| 1420 |
} while (c1 == c2); |
| 1421 |
return c1 - c2; |
| 1422 |
} |
| 1423 |
|
| 1424 |
|
| 1425 |
static |
| 1426 |
const char* nextUpperAlpha(const char* p, char x) |
| 1427 |
{ |
| 1428 |
char c; |
| 1429 |
for(c = toupper(*p); *p != 0; c = toupper(*++p)) { |
| 1430 |
if ('A' <= c && c <= 'Z') { |
| 1431 |
if (c != x) { |
| 1432 |
return p; |
| 1433 |
} |
| 1434 |
} |
| 1435 |
} |
| 1436 |
return p; |
| 1437 |
} |
| 1438 |
|
| 1439 |
|
| 1440 |
static int |
| 1441 |
sloppyCompared(const char* p, const char* q) |
| 1442 |
{ |
| 1443 |
char cp, cq; |
| 1444 |
p = nextUpperAlpha(p, 0); |
| 1445 |
q = nextUpperAlpha(q, 0); |
| 1446 |
cp = toupper(*p); |
| 1447 |
cq = toupper(*q); |
| 1448 |
while (cp == cq) { |
| 1449 |
if (cp == 0) { |
| 1450 |
return 1; |
| 1451 |
} |
| 1452 |
if (p[1] == '.') { /* some abbrevation */ |
| 1453 |
while (*q && *q++ != ' ') { |
| 1454 |
} |
| 1455 |
} |
| 1456 |
p = nextUpperAlpha(p, cp); |
| 1457 |
q = nextUpperAlpha(q, cq); |
| 1458 |
cp = toupper(*p); |
| 1459 |
cq = toupper(*q); |
| 1460 |
} |
| 1461 |
return 0; |
| 1462 |
} |
| 1463 |
|
| 1464 |
|
| 1465 |
static int |
| 1466 |
sloppySearchGenre(const char *genre) |
| 1467 |
{ |
| 1468 |
int i; |
| 1469 |
for (i = 0; i < GENRE_NAME_COUNT; ++i) { |
| 1470 |
if (sloppyCompared(genre, genre_names[i])) { |
| 1471 |
return i; |
| 1472 |
} |
| 1473 |
} |
| 1474 |
return GENRE_NAME_COUNT; |
| 1475 |
} |
| 1476 |
|
| 1477 |
|
| 1478 |
static int |
| 1479 |
searchGenre(const char* genre) |
| 1480 |
{ |
| 1481 |
int i; |
| 1482 |
for (i = 0; i < GENRE_NAME_COUNT; ++i) { |
| 1483 |
if (!local_strcasecmp(genre, genre_names[i])) { |
| 1484 |
return i; |
| 1485 |
} |
| 1486 |
} |
| 1487 |
return GENRE_NAME_COUNT; |
| 1488 |
} |
| 1489 |
|
| 1490 |
|
| 1491 |
int |
| 1492 |
id3tag_set_genre(lame_t gfp, const char *genre) |
| 1493 |
{ |
| 1494 |
lame_internal_flags *gfc = gfp != 0 ? gfp->internal_flags : 0; |
| 1495 |
int ret = 0; |
| 1496 |
if (gfc && genre && *genre) { |
| 1497 |
int const num = lookupGenre(genre); |
| 1498 |
if (num == -1) return num; |
| 1499 |
gfc->tag_spec.flags |= CHANGED_FLAG; |
| 1500 |
if (num >= 0) { |
| 1501 |
gfc->tag_spec.genre_id3v1 = num; |
| 1502 |
genre = genre_names[num]; |
| 1503 |
} |
| 1504 |
else { |
| 1505 |
gfc->tag_spec.genre_id3v1 = GENRE_INDEX_OTHER; |
| 1506 |
gfc->tag_spec.flags |= ADD_V2_FLAG; |
| 1507 |
} |
| 1508 |
copyV1ToV2(gfp, ID_GENRE, genre); |
| 1509 |
} |
| 1510 |
return ret; |
| 1511 |
} |
| 1512 |
|
| 1513 |
|
| 1514 |
static size_t |
| 1515 |
sizeOfNode(FrameDataNode const *node) |
| 1516 |
{ |
| 1517 |
size_t n = 0; |
| 1518 |
if (node) { |
| 1519 |
n = 10; /* header size */ |
| 1520 |
n += 1; /* text encoding flag */ |
| 1521 |
switch (node->txt.enc) { |
| 1522 |
default: |
| 1523 |
case 0: |
| 1524 |
if (node->dsc.dim > 0) { |
| 1525 |
n += node->dsc.dim + 1; |
| 1526 |
} |
| 1527 |
n += node->txt.dim; |
| 1528 |
break; |
| 1529 |
case 1: |
| 1530 |
if (node->dsc.dim > 0) { |
| 1531 |
n += (node->dsc.dim+1) * 2; |
| 1532 |
} |
| 1533 |
n += node->txt.dim * 2; |
| 1534 |
break; |
| 1535 |
} |
| 1536 |
} |
| 1537 |
return n; |
| 1538 |
} |
| 1539 |
|
| 1540 |
static size_t |
| 1541 |
sizeOfCommentNode(FrameDataNode const *node) |
| 1542 |
{ |
| 1543 |
size_t n = 0; |
| 1544 |
if (node) { |
| 1545 |
n = 10; /* header size */ |
| 1546 |
n += 1; /* text encoding flag */ |
| 1547 |
n += 3; /* language */ |
| 1548 |
switch (node->dsc.enc) { |
| 1549 |
default: |
| 1550 |
case 0: |
| 1551 |
n += 1 + node->dsc.dim; |
| 1552 |
break; |
| 1553 |
case 1: |
| 1554 |
n += 2 + node->dsc.dim * 2; |
| 1555 |
break; |
| 1556 |
} |
| 1557 |
switch (node->txt.enc) { |
| 1558 |
default: |
| 1559 |
case 0: |
| 1560 |
n += node->txt.dim; |
| 1561 |
break; |
| 1562 |
case 1: |
| 1563 |
n += node->txt.dim * 2; |
| 1564 |
break; |
| 1565 |
} |
| 1566 |
} |
| 1567 |
return n; |
| 1568 |
} |
| 1569 |
|
| 1570 |
static size_t |
| 1571 |
sizeOfWxxxNode(FrameDataNode const *node) |
| 1572 |
{ |
| 1573 |
size_t n = 0; |
| 1574 |
if (node) { |
| 1575 |
n = 10; /* header size */ |
| 1576 |
if (node->dsc.dim > 0) { |
| 1577 |
n += 1; /* text encoding flag */ |
| 1578 |
switch (node->dsc.enc) { |
| 1579 |
default: |
| 1580 |
case 0: |
| 1581 |
n += 1 + node->dsc.dim; |
| 1582 |
break; |
| 1583 |
case 1: |
| 1584 |
n += 2 + node->dsc.dim * 2; |
| 1585 |
break; |
| 1586 |
} |
| 1587 |
} |
| 1588 |
if (node->txt.dim > 0) { |
| 1589 |
switch (node->txt.enc) { |
| 1590 |
default: |
| 1591 |
case 0: |
| 1592 |
n += node->txt.dim; |
| 1593 |
break; |
| 1594 |
case 1: |
| 1595 |
n += node->txt.dim - 1; /* UCS2 -> Latin1, skip BOM */ |
| 1596 |
break; |
| 1597 |
} |
| 1598 |
} |
| 1599 |
} |
| 1600 |
return n; |
| 1601 |
} |
| 1602 |
|
| 1603 |
static unsigned char * |
| 1604 |
writeChars(unsigned char *frame, char const *str, size_t n) |
| 1605 |
{ |
| 1606 |
while (n--) { |
| 1607 |
*frame++ = *str++; |
| 1608 |
} |
| 1609 |
return frame; |
| 1610 |
} |
| 1611 |
|
| 1612 |
static unsigned char * |
| 1613 |
writeUcs2s(unsigned char *frame, unsigned short const *str, size_t n) |
| 1614 |
{ |
| 1615 |
if (n > 0) { |
| 1616 |
unsigned short const bom = *str; |
| 1617 |
while (n--) { |
| 1618 |
unsigned short const c = toLittleEndian(bom, *str++); |
| 1619 |
*frame++ = 0x00ffu & c; |
| 1620 |
*frame++ = 0x00ffu & (c >> 8); |
| 1621 |
} |
| 1622 |
} |
| 1623 |
return frame; |
| 1624 |
} |
| 1625 |
|
| 1626 |
static unsigned char * |
| 1627 |
writeLoBytes(unsigned char *frame, unsigned short const *str, size_t n) |
| 1628 |
{ |
| 1629 |
if (n > 0) { |
| 1630 |
unsigned short const bom = *str; |
| 1631 |
if (hasUcs2ByteOrderMarker(bom)) { |
| 1632 |
str++; n--; /* skip BOM */ |
| 1633 |
} |
| 1634 |
while (n--) { |
| 1635 |
unsigned short const c = toLittleEndian(bom, *str++); |
| 1636 |
if (c < 0x0020u || 0x00ffu < c) { |
| 1637 |
*frame++ = 0x0020; /* blank */ |
| 1638 |
} |
| 1639 |
else { |
| 1640 |
*frame++ = c; |
| 1641 |
} |
| 1642 |
} |
| 1643 |
} |
| 1644 |
return frame; |
| 1645 |
} |
| 1646 |
|
| 1647 |
static unsigned char * |
| 1648 |
set_frame_comment(unsigned char *frame, FrameDataNode const *node) |
| 1649 |
{ |
| 1650 |
size_t const n = sizeOfCommentNode(node); |
| 1651 |
if (n > 10) { |
| 1652 |
frame = set_4_byte_value(frame, node->fid); |
| 1653 |
frame = set_4_byte_value(frame, (uint32_t) (n - 10)); |
| 1654 |
/* clear 2-byte header flags */ |
| 1655 |
*frame++ = 0; |
| 1656 |
*frame++ = 0; |
| 1657 |
/* use the specified encoding descriptor byte */ |
| 1658 |
*frame++ = node->txt.enc; |
| 1659 |
/* 3 bytes language */ |
| 1660 |
*frame++ = node->lng[0]; |
| 1661 |
*frame++ = node->lng[1]; |
| 1662 |
*frame++ = node->lng[2]; |
| 1663 |
/* descriptor with zero byte(s) separator */ |
| 1664 |
if (node->dsc.enc != 1) { |
| 1665 |
frame = writeChars(frame, node->dsc.ptr.l, node->dsc.dim); |
| 1666 |
*frame++ = 0; |
| 1667 |
} |
| 1668 |
else { |
| 1669 |
frame = writeUcs2s(frame, node->dsc.ptr.u, node->dsc.dim); |
| 1670 |
*frame++ = 0; |
| 1671 |
*frame++ = 0; |
| 1672 |
} |
| 1673 |
/* comment full text */ |
| 1674 |
if (node->txt.enc != 1) { |
| 1675 |
frame = writeChars(frame, node->txt.ptr.l, node->txt.dim); |
| 1676 |
} |
| 1677 |
else { |
| 1678 |
frame = writeUcs2s(frame, node->txt.ptr.u, node->txt.dim); |
| 1679 |
} |
| 1680 |
} |
| 1681 |
return frame; |
| 1682 |
} |
| 1683 |
|
| 1684 |
static unsigned char * |
| 1685 |
set_frame_custom2(unsigned char *frame, FrameDataNode const *node) |
| 1686 |
{ |
| 1687 |
size_t const n = sizeOfNode(node); |
| 1688 |
if (n > 10) { |
| 1689 |
frame = set_4_byte_value(frame, node->fid); |
| 1690 |
frame = set_4_byte_value(frame, (unsigned long) (n - 10)); |
| 1691 |
/* clear 2-byte header flags */ |
| 1692 |
*frame++ = 0; |
| 1693 |
*frame++ = 0; |
| 1694 |
/* use the specified encoding descriptor byte */ |
| 1695 |
*frame++ = node->txt.enc; |
| 1696 |
if (node->dsc.dim > 0) { |
| 1697 |
if (node->dsc.enc != 1) { |
| 1698 |
frame = writeChars(frame, node->dsc.ptr.l, node->dsc.dim); |
| 1699 |
*frame++ = 0; |
| 1700 |
} |
| 1701 |
else { |
| 1702 |
frame = writeUcs2s(frame, node->dsc.ptr.u, node->dsc.dim); |
| 1703 |
*frame++ = 0; |
| 1704 |
*frame++ = 0; |
| 1705 |
} |
| 1706 |
} |
| 1707 |
if (node->txt.enc != 1) { |
| 1708 |
frame = writeChars(frame, node->txt.ptr.l, node->txt.dim); |
| 1709 |
} |
| 1710 |
else { |
| 1711 |
frame = writeUcs2s(frame, node->txt.ptr.u, node->txt.dim); |
| 1712 |
} |
| 1713 |
} |
| 1714 |
return frame; |
| 1715 |
} |
| 1716 |
|
| 1717 |
static unsigned char * |
| 1718 |
set_frame_wxxx(unsigned char *frame, FrameDataNode const *node) |
| 1719 |
{ |
| 1720 |
size_t const n = sizeOfWxxxNode(node); |
| 1721 |
if (n > 10) { |
| 1722 |
frame = set_4_byte_value(frame, node->fid); |
| 1723 |
frame = set_4_byte_value(frame, (unsigned long) (n - 10)); |
| 1724 |
/* clear 2-byte header flags */ |
| 1725 |
*frame++ = 0; |
| 1726 |
*frame++ = 0; |
| 1727 |
if (node->dsc.dim > 0) { |
| 1728 |
/* use the specified encoding descriptor byte */ |
| 1729 |
*frame++ = node->dsc.enc; |
| 1730 |
if (node->dsc.enc != 1) { |
| 1731 |
frame = writeChars(frame, node->dsc.ptr.l, node->dsc.dim); |
| 1732 |
*frame++ = 0; |
| 1733 |
} |
| 1734 |
else { |
| 1735 |
frame = writeUcs2s(frame, node->dsc.ptr.u, node->dsc.dim); |
| 1736 |
*frame++ = 0; |
| 1737 |
*frame++ = 0; |
| 1738 |
} |
| 1739 |
} |
| 1740 |
if (node->txt.enc != 1) { |
| 1741 |
frame = writeChars(frame, node->txt.ptr.l, node->txt.dim); |
| 1742 |
} |
| 1743 |
else { |
| 1744 |
frame = writeLoBytes(frame, node->txt.ptr.u, node->txt.dim); |
| 1745 |
} |
| 1746 |
} |
| 1747 |
return frame; |
| 1748 |
} |
| 1749 |
|
| 1750 |
static unsigned char * |
| 1751 |
set_frame_apic(unsigned char *frame, const char *mimetype, const unsigned char *data, size_t size) |
| 1752 |
{ |
| 1753 |
/* ID3v2.3 standard APIC frame: |
| 1754 |
* <Header for 'Attached picture', ID: "APIC"> |
| 1755 |
* Text encoding $xx |
| 1756 |
* MIME type <text string> $00 |
| 1757 |
* Picture type $xx |
| 1758 |
* Description <text string according to encoding> $00 (00) |
| 1759 |
* Picture data <binary data> |
| 1760 |
*/ |
| 1761 |
if (mimetype && data && size) { |
| 1762 |
frame = set_4_byte_value(frame, FRAME_ID('A', 'P', 'I', 'C')); |
| 1763 |
frame = set_4_byte_value(frame, (unsigned long) (4 + strlen(mimetype) + size)); |
| 1764 |
/* clear 2-byte header flags */ |
| 1765 |
*frame++ = 0; |
| 1766 |
*frame++ = 0; |
| 1767 |
/* clear 1 encoding descriptor byte to indicate ISO-8859-1 format */ |
| 1768 |
*frame++ = 0; |
| 1769 |
/* copy mime_type */ |
| 1770 |
while (*mimetype) { |
| 1771 |
*frame++ = *mimetype++; |
| 1772 |
} |
| 1773 |
*frame++ = 0; |
| 1774 |
/* set picture type to 0 */ |
| 1775 |
*frame++ = 0; |
| 1776 |
/* empty description field */ |
| 1777 |
*frame++ = 0; |
| 1778 |
/* copy the image data */ |
| 1779 |
while (size--) { |
| 1780 |
*frame++ = *data++; |
| 1781 |
} |
| 1782 |
} |
| 1783 |
return frame; |
| 1784 |
} |
| 1785 |
|
| 1786 |
int |
| 1787 |
id3tag_set_fieldvalue(lame_t gfp, const char *fieldvalue) |
| 1788 |
{ |
| 1789 |
if (is_lame_internal_flags_null(gfp)) { |
| 1790 |
return 0; |
| 1791 |
} |
| 1792 |
if (fieldvalue && *fieldvalue) { |
| 1793 |
if (strlen(fieldvalue) < 5 || fieldvalue[4] != '=') { |
| 1794 |
return -1; |
| 1795 |
} |
| 1796 |
return id3tag_set_textinfo_latin1(gfp, fieldvalue, &fieldvalue[5]); |
| 1797 |
} |
| 1798 |
return 0; |
| 1799 |
} |
| 1800 |
|
| 1801 |
int |
| 1802 |
id3tag_set_fieldvalue_utf16(lame_t gfp, const unsigned short *fieldvalue) |
| 1803 |
{ |
| 1804 |
if (is_lame_internal_flags_null(gfp)) { |
| 1805 |
return 0; |
| 1806 |
} |
| 1807 |
if (fieldvalue && *fieldvalue) { |
| 1808 |
size_t dx = hasUcs2ByteOrderMarker(fieldvalue[0]); |
| 1809 |
unsigned short const separator = fromLatin1Char(fieldvalue, '='); |
| 1810 |
char fid[5] = {0,0,0,0,0}; |
| 1811 |
uint32_t const frame_id = toID3v2TagId_ucs2(fieldvalue); |
| 1812 |
if (local_ucs2_strlen(fieldvalue) < (5+dx) || fieldvalue[4+dx] != separator) { |
| 1813 |
return -1; |
| 1814 |
} |
| 1815 |
fid[0] = (frame_id >> 24) & 0x0ff; |
| 1816 |
fid[1] = (frame_id >> 16) & 0x0ff; |
| 1817 |
fid[2] = (frame_id >> 8) & 0x0ff; |
| 1818 |
fid[3] = frame_id & 0x0ff; |
| 1819 |
if (frame_id != 0) { |
| 1820 |
unsigned short* txt = 0; |
| 1821 |
int rc; |
| 1822 |
local_ucs2_substr(&txt, fieldvalue, dx+5, local_ucs2_strlen(fieldvalue)); |
| 1823 |
rc = id3tag_set_textinfo_utf16(gfp, fid, txt); |
| 1824 |
free(txt); |
| 1825 |
return rc; |
| 1826 |
} |
| 1827 |
} |
| 1828 |
return -1; |
| 1829 |
} |
| 1830 |
|
| 1831 |
extern int |
| 1832 |
id3tag_set_fieldvalue_ucs2(lame_t gfp, const unsigned short *fieldvalue); |
| 1833 |
|
| 1834 |
int |
| 1835 |
id3tag_set_fieldvalue_ucs2(lame_t gfp, const unsigned short *fieldvalue) |
| 1836 |
{ |
| 1837 |
if (is_lame_internal_flags_null(gfp)) { |
| 1838 |
return 0; |
| 1839 |
} |
| 1840 |
return id3tag_set_fieldvalue_utf16(gfp, fieldvalue); |
| 1841 |
} |
| 1842 |
|
| 1843 |
size_t |
| 1844 |
lame_get_id3v2_tag(lame_t gfp, unsigned char *buffer, size_t size) |
| 1845 |
{ |
| 1846 |
lame_internal_flags *gfc = 0; |
| 1847 |
|
| 1848 |
if (is_lame_internal_flags_null(gfp)) { |
| 1849 |
return 0; |
| 1850 |
} |
| 1851 |
gfc = gfp->internal_flags; |
| 1852 |
if (test_tag_spec_flags(gfc, V1_ONLY_FLAG)) { |
| 1853 |
return 0; |
| 1854 |
} |
| 1855 |
#if 0 |
| 1856 |
debug_tag_spec_flags(gfc, "lame_get_id3v2_tag"); |
| 1857 |
#endif |
| 1858 |
{ |
| 1859 |
int usev2 = test_tag_spec_flags(gfc, ADD_V2_FLAG | V2_ONLY_FLAG); |
| 1860 |
/* calculate length of four fields which may not fit in verion 1 tag */ |
| 1861 |
size_t title_length = gfc->tag_spec.title ? strlen(gfc->tag_spec.title) : 0; |
| 1862 |
size_t artist_length = gfc->tag_spec.artist ? strlen(gfc->tag_spec.artist) : 0; |
| 1863 |
size_t album_length = gfc->tag_spec.album ? strlen(gfc->tag_spec.album) : 0; |
| 1864 |
size_t comment_length = gfc->tag_spec.comment ? strlen(gfc->tag_spec.comment) : 0; |
| 1865 |
/* write tag if explicitly requested or if fields overflow */ |
| 1866 |
if ((title_length > 30) |
| 1867 |
|| (artist_length > 30) |
| 1868 |
|| (album_length > 30) |
| 1869 |
|| (comment_length > 30) |
| 1870 |
|| (gfc->tag_spec.track_id3v1 && (comment_length > 28))) { |
| 1871 |
usev2 = 1; |
| 1872 |
} |
| 1873 |
if (usev2) { |
| 1874 |
size_t tag_size; |
| 1875 |
unsigned char *p; |
| 1876 |
size_t adjusted_tag_size; |
| 1877 |
const char *albumart_mime = NULL; |
| 1878 |
static const char *mime_jpeg = "image/jpeg"; |
| 1879 |
static const char *mime_png = "image/png"; |
| 1880 |
static const char *mime_gif = "image/gif"; |
| 1881 |
|
| 1882 |
if (gfp->num_samples != MAX_U_32_NUM) { |
| 1883 |
id3v2AddAudioDuration(gfp, gfp->num_samples); |
| 1884 |
} |
| 1885 |
|
| 1886 |
/* calulate size of tag starting with 10-byte tag header */ |
| 1887 |
tag_size = 10; |
| 1888 |
if (gfc->tag_spec.albumart && gfc->tag_spec.albumart_size) { |
| 1889 |
switch (gfc->tag_spec.albumart_mimetype) { |
| 1890 |
case MIMETYPE_JPEG: |
| 1891 |
albumart_mime = mime_jpeg; |
| 1892 |
break; |
| 1893 |
case MIMETYPE_PNG: |
| 1894 |
albumart_mime = mime_png; |
| 1895 |
break; |
| 1896 |
case MIMETYPE_GIF: |
| 1897 |
albumart_mime = mime_gif; |
| 1898 |
break; |
| 1899 |
} |
| 1900 |
if (albumart_mime) { |
| 1901 |
tag_size += 10 + 4 + strlen(albumart_mime) + gfc->tag_spec.albumart_size; |
| 1902 |
} |
| 1903 |
} |
| 1904 |
{ |
| 1905 |
id3tag_spec *tag = &gfc->tag_spec; |
| 1906 |
if (tag->v2_head != 0) { |
| 1907 |
FrameDataNode *node; |
| 1908 |
for (node = tag->v2_head; node != 0; node = node->nxt) { |
| 1909 |
if (node->fid == ID_COMMENT || node->fid == ID_USER) { |
| 1910 |
tag_size += sizeOfCommentNode(node); |
| 1911 |
} |
| 1912 |
else if (isFrameIdMatching(node->fid, FRAME_ID('W',0,0,0))) { |
| 1913 |
tag_size += sizeOfWxxxNode(node); |
| 1914 |
} |
| 1915 |
else { |
| 1916 |
tag_size += sizeOfNode(node); |
| 1917 |
} |
| 1918 |
} |
| 1919 |
} |
| 1920 |
} |
| 1921 |
if (test_tag_spec_flags(gfc, PAD_V2_FLAG)) { |
| 1922 |
/* add some bytes of padding */ |
| 1923 |
tag_size += gfc->tag_spec.padding_size; |
| 1924 |
} |
| 1925 |
if (size < tag_size) { |
| 1926 |
return tag_size; |
| 1927 |
} |
| 1928 |
if (buffer == 0) { |
| 1929 |
return 0; |
| 1930 |
} |
| 1931 |
p = buffer; |
| 1932 |
/* set tag header starting with file identifier */ |
| 1933 |
*p++ = 'I'; |
| 1934 |
*p++ = 'D'; |
| 1935 |
*p++ = '3'; |
| 1936 |
|
| 1937 |
/* set version number word */ |
| 1938 |
// set it according to whether V2_4_UTF8_FLAG is used |
| 1939 |
if (test_tag_spec_flags(gfc, V2_4_UTF8_FLAG)) // if using ID3v2.4.0 |
| 1940 |
{ |
| 1941 |
*p++ = 4; |
| 1942 |
*p++ = 0; |
| 1943 |
} else { // if using ID3v2.3.0 |
| 1944 |
*p++ = 3; |
| 1945 |
*p++ = 0; |
| 1946 |
} |
| 1947 |
|
| 1948 |
/* clear flags byte */ |
| 1949 |
*p++ = 0; |
| 1950 |
/* calculate and set tag size = total size - header size */ |
| 1951 |
adjusted_tag_size = tag_size - 10; |
| 1952 |
/* encode adjusted size into four bytes where most significant |
| 1953 |
* bit is clear in each byte, for 28-bit total */ |
| 1954 |
*p++ = (unsigned char) ((adjusted_tag_size >> 21) & 0x7fu); |
| 1955 |
*p++ = (unsigned char) ((adjusted_tag_size >> 14) & 0x7fu); |
| 1956 |
*p++ = (unsigned char) ((adjusted_tag_size >> 7) & 0x7fu); |
| 1957 |
*p++ = (unsigned char) (adjusted_tag_size & 0x7fu); |
| 1958 |
|
| 1959 |
/* |
| 1960 |
* NOTE: The remainder of the tag (frames and padding, if any) |
| 1961 |
* are not "unsynchronized" to prevent false MPEG audio headers |
| 1962 |
* from appearing in the bitstream. Why? Well, most players |
| 1963 |
* and utilities know how to skip the ID3 version 2 tag by now |
| 1964 |
* even if they don't read its contents, and it's actually |
| 1965 |
* very unlikely that such a false "sync" pattern would occur |
| 1966 |
* in just the simple text frames added here. |
| 1967 |
*/ |
| 1968 |
|
| 1969 |
/* set each frame in tag */ |
| 1970 |
{ |
| 1971 |
id3tag_spec *tag = &gfc->tag_spec; |
| 1972 |
if (tag->v2_head != 0) { |
| 1973 |
FrameDataNode *node; |
| 1974 |
for (node = tag->v2_head; node != 0; node = node->nxt) { |
| 1975 |
if (node->fid == ID_COMMENT || node->fid == ID_USER) { |
| 1976 |
p = set_frame_comment(p, node); |
| 1977 |
} |
| 1978 |
else if (isFrameIdMatching(node->fid,FRAME_ID('W',0,0,0))) { |
| 1979 |
p = set_frame_wxxx(p, node); |
| 1980 |
} |
| 1981 |
else { |
| 1982 |
p = set_frame_custom2(p, node); |
| 1983 |
} |
| 1984 |
} |
| 1985 |
} |
| 1986 |
} |
| 1987 |
if (albumart_mime) { |
| 1988 |
p = set_frame_apic(p, albumart_mime, gfc->tag_spec.albumart, |
| 1989 |
gfc->tag_spec.albumart_size); |
| 1990 |
} |
| 1991 |
/* clear any padding bytes */ |
| 1992 |
memset(p, 0, tag_size - (p - buffer)); |
| 1993 |
return tag_size; |
| 1994 |
} |
| 1995 |
} |
| 1996 |
return 0; |
| 1997 |
} |
| 1998 |
|
| 1999 |
int |
| 2000 |
id3tag_write_v2(lame_t gfp) |
| 2001 |
{ |
| 2002 |
lame_internal_flags *gfc = 0; |
| 2003 |
|
| 2004 |
if (is_lame_internal_flags_null(gfp)) { |
| 2005 |
return 0; |
| 2006 |
} |
| 2007 |
gfc = gfp->internal_flags; |
| 2008 |
#if 0 |
| 2009 |
debug_tag_spec_flags(gfc, "write v2"); |
| 2010 |
#endif |
| 2011 |
if (test_tag_spec_flags(gfc, V1_ONLY_FLAG)) { |
| 2012 |
return 0; |
| 2013 |
} |
| 2014 |
if (test_tag_spec_flags(gfc, CHANGED_FLAG)) { |
| 2015 |
unsigned char *tag = 0; |
| 2016 |
size_t tag_size, n; |
| 2017 |
|
| 2018 |
n = lame_get_id3v2_tag(gfp, 0, 0); |
| 2019 |
tag = lame_calloc(unsigned char, n); |
| 2020 |
if (tag == 0) { |
| 2021 |
return -1; |
| 2022 |
} |
| 2023 |
tag_size = lame_get_id3v2_tag(gfp, tag, n); |
| 2024 |
if (tag_size > n) { |
| 2025 |
free(tag); |
| 2026 |
return -1; |
| 2027 |
} |
| 2028 |
else { |
| 2029 |
size_t i; |
| 2030 |
/* write tag directly into bitstream at current position */ |
| 2031 |
for (i = 0; i < tag_size; ++i) { |
| 2032 |
add_dummy_byte(gfc, tag[i], 1); |
| 2033 |
} |
| 2034 |
} |
| 2035 |
free(tag); |
| 2036 |
return (int) tag_size; /* ok, tag should not exceed 2GB */ |
| 2037 |
} |
| 2038 |
return 0; |
| 2039 |
} |
| 2040 |
|
| 2041 |
static unsigned char * |
| 2042 |
set_text_field(unsigned char *field, const char *text, size_t size, int pad) |
| 2043 |
{ |
| 2044 |
while (size--) { |
| 2045 |
if (text && *text) { |
| 2046 |
*field++ = *text++; |
| 2047 |
} |
| 2048 |
else { |
| 2049 |
*field++ = pad; |
| 2050 |
} |
| 2051 |
} |
| 2052 |
return field; |
| 2053 |
} |
| 2054 |
|
| 2055 |
size_t |
| 2056 |
lame_get_id3v1_tag(lame_t gfp, unsigned char *buffer, size_t size) |
| 2057 |
{ |
| 2058 |
size_t const tag_size = 128; |
| 2059 |
lame_internal_flags *gfc; |
| 2060 |
|
| 2061 |
if (gfp == 0) { |
| 2062 |
return 0; |
| 2063 |
} |
| 2064 |
if (size < tag_size) { |
| 2065 |
return tag_size; |
| 2066 |
} |
| 2067 |
gfc = gfp->internal_flags; |
| 2068 |
if (gfc == 0) { |
| 2069 |
return 0; |
| 2070 |
} |
| 2071 |
if (buffer == 0) { |
| 2072 |
return 0; |
| 2073 |
} |
| 2074 |
if (test_tag_spec_flags(gfc, V2_ONLY_FLAG)) { |
| 2075 |
return 0; |
| 2076 |
} |
| 2077 |
if (test_tag_spec_flags(gfc, CHANGED_FLAG)) { |
| 2078 |
unsigned char *p = buffer; |
| 2079 |
int pad = test_tag_spec_flags(gfc, SPACE_V1_FLAG) ? ' ' : 0; |
| 2080 |
char year[5]; |
| 2081 |
|
| 2082 |
/* set tag identifier */ |
| 2083 |
*p++ = 'T'; |
| 2084 |
*p++ = 'A'; |
| 2085 |
*p++ = 'G'; |
| 2086 |
/* set each field in tag */ |
| 2087 |
p = set_text_field(p, gfc->tag_spec.title, 30, pad); |
| 2088 |
p = set_text_field(p, gfc->tag_spec.artist, 30, pad); |
| 2089 |
p = set_text_field(p, gfc->tag_spec.album, 30, pad); |
| 2090 |
sprintf(year, "%d", gfc->tag_spec.year); |
| 2091 |
p = set_text_field(p, gfc->tag_spec.year ? year : NULL, 4, pad); |
| 2092 |
/* limit comment field to 28 bytes if a track is specified */ |
| 2093 |
p = set_text_field(p, gfc->tag_spec.comment, gfc->tag_spec.track_id3v1 ? 28 : 30, pad); |
| 2094 |
if (gfc->tag_spec.track_id3v1) { |
| 2095 |
/* clear the next byte to indicate a version 1.1 tag */ |
| 2096 |
*p++ = 0; |
| 2097 |
*p++ = gfc->tag_spec.track_id3v1; |
| 2098 |
} |
| 2099 |
*p++ = gfc->tag_spec.genre_id3v1; |
| 2100 |
return tag_size; |
| 2101 |
} |
| 2102 |
return 0; |
| 2103 |
} |
| 2104 |
|
| 2105 |
int |
| 2106 |
id3tag_write_v1(lame_t gfp) |
| 2107 |
{ |
| 2108 |
lame_internal_flags* gfc = 0; |
| 2109 |
size_t i, n, m; |
| 2110 |
unsigned char tag[128]; |
| 2111 |
|
| 2112 |
if (is_lame_internal_flags_null(gfp)) { |
| 2113 |
return 0; |
| 2114 |
} |
| 2115 |
gfc = gfp->internal_flags; |
| 2116 |
|
| 2117 |
m = sizeof(tag); |
| 2118 |
n = lame_get_id3v1_tag(gfp, tag, m); |
| 2119 |
if (n > m) { |
| 2120 |
return 0; |
| 2121 |
} |
| 2122 |
/* write tag directly into bitstream at current position */ |
| 2123 |
for (i = 0; i < n; ++i) { |
| 2124 |
add_dummy_byte(gfc, tag[i], 1); |
| 2125 |
} |
| 2126 |
return (int) n; /* ok, tag has fixed size of 128 bytes, well below 2GB */ |
| 2127 |
} |