annotate src/libid3tag-0.15.1b/render.c @ 6:35e00f62c407

Further builds
author Chris Cannam
date Wed, 20 Mar 2013 14:58:12 +0000
parents c7265573341e
children
rev   line source
Chris@0 1 /*
Chris@0 2 * libid3tag - ID3 tag manipulation library
Chris@0 3 * Copyright (C) 2000-2004 Underbit Technologies, Inc.
Chris@0 4 *
Chris@0 5 * This program is free software; you can redistribute it and/or modify
Chris@0 6 * it under the terms of the GNU General Public License as published by
Chris@0 7 * the Free Software Foundation; either version 2 of the License, or
Chris@0 8 * (at your option) any later version.
Chris@0 9 *
Chris@0 10 * This program is distributed in the hope that it will be useful,
Chris@0 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@0 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@0 13 * GNU General Public License for more details.
Chris@0 14 *
Chris@0 15 * You should have received a copy of the GNU General Public License
Chris@0 16 * along with this program; if not, write to the Free Software
Chris@0 17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Chris@0 18 *
Chris@0 19 * $Id: render.c,v 1.11 2004/01/23 09:41:32 rob Exp $
Chris@0 20 */
Chris@0 21
Chris@0 22 # ifdef HAVE_CONFIG_H
Chris@0 23 # include "config.h"
Chris@0 24 # endif
Chris@0 25
Chris@0 26 # include "global.h"
Chris@0 27
Chris@0 28 # include <string.h>
Chris@0 29 # include <stdlib.h>
Chris@0 30
Chris@0 31 # ifdef HAVE_ASSERT_H
Chris@0 32 # include <assert.h>
Chris@0 33 # endif
Chris@0 34
Chris@0 35 # include "id3tag.h"
Chris@0 36 # include "render.h"
Chris@0 37 # include "ucs4.h"
Chris@0 38 # include "latin1.h"
Chris@0 39 # include "utf16.h"
Chris@0 40 # include "utf8.h"
Chris@0 41
Chris@0 42 id3_length_t id3_render_immediate(id3_byte_t **ptr,
Chris@0 43 char const *value, unsigned int bytes)
Chris@0 44 {
Chris@0 45 assert(value);
Chris@0 46 assert(bytes == 8 || bytes == 4 || bytes == 3);
Chris@0 47
Chris@0 48 if (ptr) {
Chris@0 49 switch (bytes) {
Chris@0 50 case 8: *(*ptr)++ = *value++;
Chris@0 51 *(*ptr)++ = *value++;
Chris@0 52 *(*ptr)++ = *value++;
Chris@0 53 *(*ptr)++ = *value++;
Chris@0 54 case 4: *(*ptr)++ = *value++;
Chris@0 55 case 3: *(*ptr)++ = *value++;
Chris@0 56 *(*ptr)++ = *value++;
Chris@0 57 *(*ptr)++ = *value++;
Chris@0 58 }
Chris@0 59 }
Chris@0 60
Chris@0 61 return bytes;
Chris@0 62 }
Chris@0 63
Chris@0 64 id3_length_t id3_render_syncsafe(id3_byte_t **ptr,
Chris@0 65 unsigned long num, unsigned int bytes)
Chris@0 66 {
Chris@0 67 assert(bytes == 4 || bytes == 5);
Chris@0 68
Chris@0 69 if (ptr) {
Chris@0 70 switch (bytes) {
Chris@0 71 case 5: *(*ptr)++ = (num >> 28) & 0x0f;
Chris@0 72 case 4: *(*ptr)++ = (num >> 21) & 0x7f;
Chris@0 73 *(*ptr)++ = (num >> 14) & 0x7f;
Chris@0 74 *(*ptr)++ = (num >> 7) & 0x7f;
Chris@0 75 *(*ptr)++ = (num >> 0) & 0x7f;
Chris@0 76 }
Chris@0 77 }
Chris@0 78
Chris@0 79 return bytes;
Chris@0 80 }
Chris@0 81
Chris@0 82 id3_length_t id3_render_int(id3_byte_t **ptr,
Chris@0 83 signed long num, unsigned int bytes)
Chris@0 84 {
Chris@0 85 assert(bytes >= 1 && bytes <= 4);
Chris@0 86
Chris@0 87 if (ptr) {
Chris@0 88 switch (bytes) {
Chris@0 89 case 4: *(*ptr)++ = num >> 24;
Chris@0 90 case 3: *(*ptr)++ = num >> 16;
Chris@0 91 case 2: *(*ptr)++ = num >> 8;
Chris@0 92 case 1: *(*ptr)++ = num >> 0;
Chris@0 93 }
Chris@0 94 }
Chris@0 95
Chris@0 96 return bytes;
Chris@0 97 }
Chris@0 98
Chris@0 99 id3_length_t id3_render_binary(id3_byte_t **ptr,
Chris@0 100 id3_byte_t const *data, id3_length_t length)
Chris@0 101 {
Chris@0 102 if (data == 0)
Chris@0 103 return 0;
Chris@0 104
Chris@0 105 if (ptr) {
Chris@0 106 memcpy(*ptr, data, length);
Chris@0 107 *ptr += length;
Chris@0 108 }
Chris@0 109
Chris@0 110 return length;
Chris@0 111 }
Chris@0 112
Chris@0 113 id3_length_t id3_render_latin1(id3_byte_t **ptr,
Chris@0 114 id3_latin1_t const *latin1, int terminate)
Chris@0 115 {
Chris@0 116 id3_length_t size;
Chris@0 117
Chris@0 118 if (latin1 == 0)
Chris@0 119 latin1 = "";
Chris@0 120
Chris@0 121 size = id3_latin1_size(latin1);
Chris@0 122 if (!terminate)
Chris@0 123 --size;
Chris@0 124
Chris@0 125 if (ptr) {
Chris@0 126 memcpy(*ptr, latin1, size);
Chris@0 127 *ptr += size;
Chris@0 128 }
Chris@0 129
Chris@0 130 return size;
Chris@0 131 }
Chris@0 132
Chris@0 133 id3_length_t id3_render_string(id3_byte_t **ptr, id3_ucs4_t const *ucs4,
Chris@0 134 enum id3_field_textencoding encoding,
Chris@0 135 int terminate)
Chris@0 136 {
Chris@0 137 enum id3_utf16_byteorder byteorder = ID3_UTF16_BYTEORDER_ANY;
Chris@0 138
Chris@0 139 if (ucs4 == 0)
Chris@0 140 ucs4 = id3_ucs4_empty;
Chris@0 141
Chris@0 142 switch (encoding) {
Chris@0 143 case ID3_FIELD_TEXTENCODING_ISO_8859_1:
Chris@0 144 return id3_latin1_serialize(ptr, ucs4, terminate);
Chris@0 145
Chris@0 146 case ID3_FIELD_TEXTENCODING_UTF_16BE:
Chris@0 147 byteorder = ID3_UTF16_BYTEORDER_BE;
Chris@0 148 case ID3_FIELD_TEXTENCODING_UTF_16:
Chris@0 149 return id3_utf16_serialize(ptr, ucs4, byteorder, terminate);
Chris@0 150
Chris@0 151 case ID3_FIELD_TEXTENCODING_UTF_8:
Chris@0 152 return id3_utf8_serialize(ptr, ucs4, terminate);
Chris@0 153 }
Chris@0 154
Chris@0 155 return 0;
Chris@0 156 }
Chris@0 157
Chris@0 158 id3_length_t id3_render_padding(id3_byte_t **ptr, id3_byte_t value,
Chris@0 159 id3_length_t length)
Chris@0 160 {
Chris@0 161 if (ptr) {
Chris@0 162 memset(*ptr, value, length);
Chris@0 163 *ptr += length;
Chris@0 164 }
Chris@0 165
Chris@0 166 return length;
Chris@0 167 }
Chris@0 168
Chris@0 169 /*
Chris@0 170 * NAME: render->paddedstring()
Chris@0 171 * DESCRIPTION: render a space-padded string using latin1 encoding
Chris@0 172 */
Chris@0 173 id3_length_t id3_render_paddedstring(id3_byte_t **ptr, id3_ucs4_t const *ucs4,
Chris@0 174 id3_length_t length)
Chris@0 175 {
Chris@0 176 id3_ucs4_t padded[31], *data, *end;
Chris@0 177
Chris@0 178 /* latin1 encoding only (this is used for ID3v1 fields) */
Chris@0 179
Chris@0 180 assert(length <= 30);
Chris@0 181
Chris@0 182 data = padded;
Chris@0 183 end = data + length;
Chris@0 184
Chris@0 185 if (ucs4) {
Chris@0 186 while (*ucs4 && end - data > 0) {
Chris@0 187 *data++ = *ucs4++;
Chris@0 188
Chris@0 189 if (data[-1] == '\n')
Chris@0 190 data[-1] = ' ';
Chris@0 191 }
Chris@0 192 }
Chris@0 193
Chris@0 194 while (end - data > 0)
Chris@0 195 *data++ = ' ';
Chris@0 196
Chris@0 197 *data = 0;
Chris@0 198
Chris@0 199 return id3_latin1_serialize(ptr, padded, 0);
Chris@0 200 }