annotate src/libid3tag-0.15.1b/parse.c @ 23:619f715526df sv_v2.1

Update Vamp plugin SDK to 2.5
author Chris Cannam
date Thu, 09 May 2013 10:52:46 +0100
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: parse.c,v 1.9 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 # ifdef HAVE_ASSERT_H
Chris@0 29 # include <assert.h>
Chris@0 30 # endif
Chris@0 31
Chris@0 32 # include <stdlib.h>
Chris@0 33 # include <string.h>
Chris@0 34
Chris@0 35 # include "id3tag.h"
Chris@0 36 # include "parse.h"
Chris@0 37 # include "latin1.h"
Chris@0 38 # include "utf16.h"
Chris@0 39 # include "utf8.h"
Chris@0 40
Chris@0 41 signed long id3_parse_int(id3_byte_t const **ptr, unsigned int bytes)
Chris@0 42 {
Chris@0 43 signed long value = 0;
Chris@0 44
Chris@0 45 assert(bytes >= 1 && bytes <= 4);
Chris@0 46
Chris@0 47 if (**ptr & 0x80)
Chris@0 48 value = ~0;
Chris@0 49
Chris@0 50 switch (bytes) {
Chris@0 51 case 4: value = (value << 8) | *(*ptr)++;
Chris@0 52 case 3: value = (value << 8) | *(*ptr)++;
Chris@0 53 case 2: value = (value << 8) | *(*ptr)++;
Chris@0 54 case 1: value = (value << 8) | *(*ptr)++;
Chris@0 55 }
Chris@0 56
Chris@0 57 return value;
Chris@0 58 }
Chris@0 59
Chris@0 60 unsigned long id3_parse_uint(id3_byte_t const **ptr, unsigned int bytes)
Chris@0 61 {
Chris@0 62 unsigned long value = 0;
Chris@0 63
Chris@0 64 assert(bytes >= 1 && bytes <= 4);
Chris@0 65
Chris@0 66 switch (bytes) {
Chris@0 67 case 4: value = (value << 8) | *(*ptr)++;
Chris@0 68 case 3: value = (value << 8) | *(*ptr)++;
Chris@0 69 case 2: value = (value << 8) | *(*ptr)++;
Chris@0 70 case 1: value = (value << 8) | *(*ptr)++;
Chris@0 71 }
Chris@0 72
Chris@0 73 return value;
Chris@0 74 }
Chris@0 75
Chris@0 76 unsigned long id3_parse_syncsafe(id3_byte_t const **ptr, unsigned int bytes)
Chris@0 77 {
Chris@0 78 unsigned long value = 0;
Chris@0 79
Chris@0 80 assert(bytes == 4 || bytes == 5);
Chris@0 81
Chris@0 82 switch (bytes) {
Chris@0 83 case 5: value = (value << 4) | (*(*ptr)++ & 0x0f);
Chris@0 84 case 4: value = (value << 7) | (*(*ptr)++ & 0x7f);
Chris@0 85 value = (value << 7) | (*(*ptr)++ & 0x7f);
Chris@0 86 value = (value << 7) | (*(*ptr)++ & 0x7f);
Chris@0 87 value = (value << 7) | (*(*ptr)++ & 0x7f);
Chris@0 88 }
Chris@0 89
Chris@0 90 return value;
Chris@0 91 }
Chris@0 92
Chris@0 93 void id3_parse_immediate(id3_byte_t const **ptr, unsigned int bytes,
Chris@0 94 char *value)
Chris@0 95 {
Chris@0 96 assert(value);
Chris@0 97 assert(bytes == 8 || bytes == 4 || bytes == 3);
Chris@0 98
Chris@0 99 switch (bytes) {
Chris@0 100 case 8: *value++ = *(*ptr)++;
Chris@0 101 *value++ = *(*ptr)++;
Chris@0 102 *value++ = *(*ptr)++;
Chris@0 103 *value++ = *(*ptr)++;
Chris@0 104 case 4: *value++ = *(*ptr)++;
Chris@0 105 case 3: *value++ = *(*ptr)++;
Chris@0 106 *value++ = *(*ptr)++;
Chris@0 107 *value++ = *(*ptr)++;
Chris@0 108 }
Chris@0 109
Chris@0 110 *value = 0;
Chris@0 111 }
Chris@0 112
Chris@0 113 id3_latin1_t *id3_parse_latin1(id3_byte_t const **ptr, id3_length_t length,
Chris@0 114 int full)
Chris@0 115 {
Chris@0 116 id3_byte_t const *end;
Chris@0 117 int terminated = 0;
Chris@0 118 id3_latin1_t *latin1;
Chris@0 119
Chris@0 120 end = memchr(*ptr, 0, length);
Chris@0 121 if (end == 0)
Chris@0 122 end = *ptr + length;
Chris@0 123 else {
Chris@0 124 length = end - *ptr;
Chris@0 125 terminated = 1;
Chris@0 126 }
Chris@0 127
Chris@0 128 latin1 = malloc(length + 1);
Chris@0 129 if (latin1) {
Chris@0 130 memcpy(latin1, *ptr, length);
Chris@0 131 latin1[length] = 0;
Chris@0 132
Chris@0 133 if (!full) {
Chris@0 134 id3_latin1_t *check;
Chris@0 135
Chris@0 136 for (check = latin1; *check; ++check) {
Chris@0 137 if (*check == '\n')
Chris@0 138 *check = ' ';
Chris@0 139 }
Chris@0 140 }
Chris@0 141 }
Chris@0 142
Chris@0 143 *ptr += length + terminated;
Chris@0 144
Chris@0 145 return latin1;
Chris@0 146 }
Chris@0 147
Chris@0 148 id3_ucs4_t *id3_parse_string(id3_byte_t const **ptr, id3_length_t length,
Chris@0 149 enum id3_field_textencoding encoding, int full)
Chris@0 150 {
Chris@0 151 id3_ucs4_t *ucs4 = 0;
Chris@0 152 enum id3_utf16_byteorder byteorder = ID3_UTF16_BYTEORDER_ANY;
Chris@0 153
Chris@0 154 switch (encoding) {
Chris@0 155 case ID3_FIELD_TEXTENCODING_ISO_8859_1:
Chris@0 156 ucs4 = id3_latin1_deserialize(ptr, length);
Chris@0 157 break;
Chris@0 158
Chris@0 159 case ID3_FIELD_TEXTENCODING_UTF_16BE:
Chris@0 160 byteorder = ID3_UTF16_BYTEORDER_BE;
Chris@0 161 case ID3_FIELD_TEXTENCODING_UTF_16:
Chris@0 162 ucs4 = id3_utf16_deserialize(ptr, length, byteorder);
Chris@0 163 break;
Chris@0 164
Chris@0 165 case ID3_FIELD_TEXTENCODING_UTF_8:
Chris@0 166 ucs4 = id3_utf8_deserialize(ptr, length);
Chris@0 167 break;
Chris@0 168 }
Chris@0 169
Chris@0 170 if (ucs4 && !full) {
Chris@0 171 id3_ucs4_t *check;
Chris@0 172
Chris@0 173 for (check = ucs4; *check; ++check) {
Chris@0 174 if (*check == '\n')
Chris@0 175 *check = ' ';
Chris@0 176 }
Chris@0 177 }
Chris@0 178
Chris@0 179 return ucs4;
Chris@0 180 }
Chris@0 181
Chris@0 182 id3_byte_t *id3_parse_binary(id3_byte_t const **ptr, id3_length_t length)
Chris@0 183 {
Chris@0 184 id3_byte_t *data;
Chris@0 185
Chris@0 186 if (length == 0)
Chris@0 187 return malloc(1);
Chris@0 188
Chris@0 189 data = malloc(length);
Chris@0 190 if (data)
Chris@0 191 memcpy(data, *ptr, length);
Chris@0 192
Chris@0 193 *ptr += length;
Chris@0 194
Chris@0 195 return data;
Chris@0 196 }