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: render.c,v 1.11 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: # include Chris@0: # include Chris@0: Chris@0: # ifdef HAVE_ASSERT_H Chris@0: # include Chris@0: # endif Chris@0: Chris@0: # include "id3tag.h" Chris@0: # include "render.h" Chris@0: # include "ucs4.h" Chris@0: # include "latin1.h" Chris@0: # include "utf16.h" Chris@0: # include "utf8.h" Chris@0: Chris@0: id3_length_t id3_render_immediate(id3_byte_t **ptr, Chris@0: char const *value, unsigned int bytes) Chris@0: { Chris@0: assert(value); Chris@0: assert(bytes == 8 || bytes == 4 || bytes == 3); Chris@0: Chris@0: if (ptr) { Chris@0: switch (bytes) { Chris@0: case 8: *(*ptr)++ = *value++; Chris@0: *(*ptr)++ = *value++; Chris@0: *(*ptr)++ = *value++; Chris@0: *(*ptr)++ = *value++; Chris@0: case 4: *(*ptr)++ = *value++; Chris@0: case 3: *(*ptr)++ = *value++; Chris@0: *(*ptr)++ = *value++; Chris@0: *(*ptr)++ = *value++; Chris@0: } Chris@0: } Chris@0: Chris@0: return bytes; Chris@0: } Chris@0: Chris@0: id3_length_t id3_render_syncsafe(id3_byte_t **ptr, Chris@0: unsigned long num, unsigned int bytes) Chris@0: { Chris@0: assert(bytes == 4 || bytes == 5); Chris@0: Chris@0: if (ptr) { Chris@0: switch (bytes) { Chris@0: case 5: *(*ptr)++ = (num >> 28) & 0x0f; Chris@0: case 4: *(*ptr)++ = (num >> 21) & 0x7f; Chris@0: *(*ptr)++ = (num >> 14) & 0x7f; Chris@0: *(*ptr)++ = (num >> 7) & 0x7f; Chris@0: *(*ptr)++ = (num >> 0) & 0x7f; Chris@0: } Chris@0: } Chris@0: Chris@0: return bytes; Chris@0: } Chris@0: Chris@0: id3_length_t id3_render_int(id3_byte_t **ptr, Chris@0: signed long num, unsigned int bytes) Chris@0: { Chris@0: assert(bytes >= 1 && bytes <= 4); Chris@0: Chris@0: if (ptr) { Chris@0: switch (bytes) { Chris@0: case 4: *(*ptr)++ = num >> 24; Chris@0: case 3: *(*ptr)++ = num >> 16; Chris@0: case 2: *(*ptr)++ = num >> 8; Chris@0: case 1: *(*ptr)++ = num >> 0; Chris@0: } Chris@0: } Chris@0: Chris@0: return bytes; Chris@0: } Chris@0: Chris@0: id3_length_t id3_render_binary(id3_byte_t **ptr, Chris@0: id3_byte_t const *data, id3_length_t length) Chris@0: { Chris@0: if (data == 0) Chris@0: return 0; Chris@0: Chris@0: if (ptr) { Chris@0: memcpy(*ptr, data, length); Chris@0: *ptr += length; Chris@0: } Chris@0: Chris@0: return length; Chris@0: } Chris@0: Chris@0: id3_length_t id3_render_latin1(id3_byte_t **ptr, Chris@0: id3_latin1_t const *latin1, int terminate) Chris@0: { Chris@0: id3_length_t size; Chris@0: Chris@0: if (latin1 == 0) Chris@0: latin1 = ""; Chris@0: Chris@0: size = id3_latin1_size(latin1); Chris@0: if (!terminate) Chris@0: --size; Chris@0: Chris@0: if (ptr) { Chris@0: memcpy(*ptr, latin1, size); Chris@0: *ptr += size; Chris@0: } Chris@0: Chris@0: return size; Chris@0: } Chris@0: Chris@0: id3_length_t id3_render_string(id3_byte_t **ptr, id3_ucs4_t const *ucs4, Chris@0: enum id3_field_textencoding encoding, Chris@0: int terminate) Chris@0: { Chris@0: enum id3_utf16_byteorder byteorder = ID3_UTF16_BYTEORDER_ANY; Chris@0: Chris@0: if (ucs4 == 0) Chris@0: ucs4 = id3_ucs4_empty; Chris@0: Chris@0: switch (encoding) { Chris@0: case ID3_FIELD_TEXTENCODING_ISO_8859_1: Chris@0: return id3_latin1_serialize(ptr, ucs4, terminate); 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: return id3_utf16_serialize(ptr, ucs4, byteorder, terminate); Chris@0: Chris@0: case ID3_FIELD_TEXTENCODING_UTF_8: Chris@0: return id3_utf8_serialize(ptr, ucs4, terminate); Chris@0: } Chris@0: Chris@0: return 0; Chris@0: } Chris@0: Chris@0: id3_length_t id3_render_padding(id3_byte_t **ptr, id3_byte_t value, Chris@0: id3_length_t length) Chris@0: { Chris@0: if (ptr) { Chris@0: memset(*ptr, value, length); Chris@0: *ptr += length; Chris@0: } Chris@0: Chris@0: return length; Chris@0: } Chris@0: Chris@0: /* Chris@0: * NAME: render->paddedstring() Chris@0: * DESCRIPTION: render a space-padded string using latin1 encoding Chris@0: */ Chris@0: id3_length_t id3_render_paddedstring(id3_byte_t **ptr, id3_ucs4_t const *ucs4, Chris@0: id3_length_t length) Chris@0: { Chris@0: id3_ucs4_t padded[31], *data, *end; Chris@0: Chris@0: /* latin1 encoding only (this is used for ID3v1 fields) */ Chris@0: Chris@0: assert(length <= 30); Chris@0: Chris@0: data = padded; Chris@0: end = data + length; Chris@0: Chris@0: if (ucs4) { Chris@0: while (*ucs4 && end - data > 0) { Chris@0: *data++ = *ucs4++; Chris@0: Chris@0: if (data[-1] == '\n') Chris@0: data[-1] = ' '; Chris@0: } Chris@0: } Chris@0: Chris@0: while (end - data > 0) Chris@0: *data++ = ' '; Chris@0: Chris@0: *data = 0; Chris@0: Chris@0: return id3_latin1_serialize(ptr, padded, 0); Chris@0: }