Chris@0: /* Chris@0: * libid3tag - ID3 tag manipulation library Chris@0: * Copyright (C) 2000-2004 Underbit Technologies, Inc. Chris@0: * Chris@0: * This program is free software; you can redistribute it and/or modify Chris@0: * it under the terms of the GNU General Public License as published by Chris@0: * the Free Software Foundation; either version 2 of the License, or Chris@0: * (at your option) any later version. Chris@0: * Chris@0: * This program is distributed in the hope that it will be useful, Chris@0: * but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@0: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@0: * GNU General Public License for more details. Chris@0: * Chris@0: * You should have received a copy of the GNU General Public License Chris@0: * along with this program; if not, write to the Free Software Chris@0: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA Chris@0: * Chris@0: * $Id: parse.c,v 1.9 2004/01/23 09:41:32 rob Exp $ Chris@0: */ Chris@0: Chris@0: # ifdef HAVE_CONFIG_H Chris@0: # include "config.h" Chris@0: # endif Chris@0: Chris@0: # include "global.h" Chris@0: Chris@0: # ifdef HAVE_ASSERT_H Chris@0: # include Chris@0: # endif Chris@0: Chris@0: # include Chris@0: # include Chris@0: Chris@0: # include "id3tag.h" Chris@0: # include "parse.h" Chris@0: # include "latin1.h" Chris@0: # include "utf16.h" Chris@0: # include "utf8.h" Chris@0: Chris@0: signed long id3_parse_int(id3_byte_t const **ptr, unsigned int bytes) Chris@0: { Chris@0: signed long value = 0; Chris@0: Chris@0: assert(bytes >= 1 && bytes <= 4); Chris@0: Chris@0: if (**ptr & 0x80) Chris@0: value = ~0; Chris@0: Chris@0: switch (bytes) { Chris@0: case 4: value = (value << 8) | *(*ptr)++; Chris@0: case 3: value = (value << 8) | *(*ptr)++; Chris@0: case 2: value = (value << 8) | *(*ptr)++; Chris@0: case 1: value = (value << 8) | *(*ptr)++; Chris@0: } Chris@0: Chris@0: return value; Chris@0: } Chris@0: Chris@0: unsigned long id3_parse_uint(id3_byte_t const **ptr, unsigned int bytes) Chris@0: { Chris@0: unsigned long value = 0; Chris@0: Chris@0: assert(bytes >= 1 && bytes <= 4); Chris@0: Chris@0: switch (bytes) { Chris@0: case 4: value = (value << 8) | *(*ptr)++; Chris@0: case 3: value = (value << 8) | *(*ptr)++; Chris@0: case 2: value = (value << 8) | *(*ptr)++; Chris@0: case 1: value = (value << 8) | *(*ptr)++; Chris@0: } Chris@0: Chris@0: return value; Chris@0: } Chris@0: Chris@0: unsigned long id3_parse_syncsafe(id3_byte_t const **ptr, unsigned int bytes) Chris@0: { Chris@0: unsigned long value = 0; Chris@0: Chris@0: assert(bytes == 4 || bytes == 5); Chris@0: Chris@0: switch (bytes) { Chris@0: case 5: value = (value << 4) | (*(*ptr)++ & 0x0f); Chris@0: case 4: value = (value << 7) | (*(*ptr)++ & 0x7f); Chris@0: value = (value << 7) | (*(*ptr)++ & 0x7f); Chris@0: value = (value << 7) | (*(*ptr)++ & 0x7f); Chris@0: value = (value << 7) | (*(*ptr)++ & 0x7f); Chris@0: } Chris@0: Chris@0: return value; Chris@0: } Chris@0: Chris@0: void id3_parse_immediate(id3_byte_t const **ptr, unsigned int bytes, Chris@0: char *value) Chris@0: { Chris@0: assert(value); Chris@0: assert(bytes == 8 || bytes == 4 || bytes == 3); Chris@0: Chris@0: switch (bytes) { Chris@0: case 8: *value++ = *(*ptr)++; Chris@0: *value++ = *(*ptr)++; Chris@0: *value++ = *(*ptr)++; Chris@0: *value++ = *(*ptr)++; Chris@0: case 4: *value++ = *(*ptr)++; Chris@0: case 3: *value++ = *(*ptr)++; Chris@0: *value++ = *(*ptr)++; Chris@0: *value++ = *(*ptr)++; Chris@0: } Chris@0: Chris@0: *value = 0; Chris@0: } Chris@0: Chris@0: id3_latin1_t *id3_parse_latin1(id3_byte_t const **ptr, id3_length_t length, Chris@0: int full) Chris@0: { Chris@0: id3_byte_t const *end; Chris@0: int terminated = 0; Chris@0: id3_latin1_t *latin1; Chris@0: Chris@0: end = memchr(*ptr, 0, length); Chris@0: if (end == 0) Chris@0: end = *ptr + length; Chris@0: else { Chris@0: length = end - *ptr; Chris@0: terminated = 1; Chris@0: } Chris@0: Chris@0: latin1 = malloc(length + 1); Chris@0: if (latin1) { Chris@0: memcpy(latin1, *ptr, length); Chris@0: latin1[length] = 0; Chris@0: Chris@0: if (!full) { Chris@0: id3_latin1_t *check; Chris@0: Chris@0: for (check = latin1; *check; ++check) { Chris@0: if (*check == '\n') Chris@0: *check = ' '; Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: *ptr += length + terminated; Chris@0: Chris@0: return latin1; Chris@0: } Chris@0: Chris@0: id3_ucs4_t *id3_parse_string(id3_byte_t const **ptr, id3_length_t length, Chris@0: enum id3_field_textencoding encoding, int full) Chris@0: { Chris@0: id3_ucs4_t *ucs4 = 0; Chris@0: enum id3_utf16_byteorder byteorder = ID3_UTF16_BYTEORDER_ANY; Chris@0: Chris@0: switch (encoding) { Chris@0: case ID3_FIELD_TEXTENCODING_ISO_8859_1: Chris@0: ucs4 = id3_latin1_deserialize(ptr, length); Chris@0: break; Chris@0: Chris@0: case ID3_FIELD_TEXTENCODING_UTF_16BE: Chris@0: byteorder = ID3_UTF16_BYTEORDER_BE; Chris@0: case ID3_FIELD_TEXTENCODING_UTF_16: Chris@0: ucs4 = id3_utf16_deserialize(ptr, length, byteorder); Chris@0: break; Chris@0: Chris@0: case ID3_FIELD_TEXTENCODING_UTF_8: Chris@0: ucs4 = id3_utf8_deserialize(ptr, length); Chris@0: break; Chris@0: } Chris@0: Chris@0: if (ucs4 && !full) { Chris@0: id3_ucs4_t *check; Chris@0: Chris@0: for (check = ucs4; *check; ++check) { Chris@0: if (*check == '\n') Chris@0: *check = ' '; Chris@0: } Chris@0: } Chris@0: Chris@0: return ucs4; Chris@0: } Chris@0: Chris@0: id3_byte_t *id3_parse_binary(id3_byte_t const **ptr, id3_length_t length) Chris@0: { Chris@0: id3_byte_t *data; Chris@0: Chris@0: if (length == 0) Chris@0: return malloc(1); Chris@0: Chris@0: data = malloc(length); Chris@0: if (data) Chris@0: memcpy(data, *ptr, length); Chris@0: Chris@0: *ptr += length; Chris@0: Chris@0: return data; Chris@0: }