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