PageRenderTime 76ms CodeModel.GetById 14ms RepoModel.GetById 2ms app.codeStats 0ms

/libtunepimp-0.5.3/plugins/mp3/id3tag/parse.c

#
C | 196 lines | 136 code | 40 blank | 20 comment | 32 complexity | 1693d24449c6567960c505b1252310ae MD5 | raw file
Possible License(s): LGPL-2.1, LGPL-3.0, GPL-2.0, LGPL-2.0
  1. /*
  2. * libid3tag - ID3 tag manipulation library
  3. * Copyright (C) 2000-2004 Underbit Technologies, Inc.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. *
  19. * $Id: parse.c 1373 2005-05-24 05:29:15Z robert $
  20. */
  21. # ifdef HAVE_CONFIG_H
  22. # include "config.h"
  23. # endif
  24. # include "global.h"
  25. # ifdef HAVE_ASSERT_H
  26. # include <assert.h>
  27. # endif
  28. # include <stdlib.h>
  29. # include <string.h>
  30. # include "id3tag.h"
  31. # include "parse.h"
  32. # include "latin1.h"
  33. # include "utf16.h"
  34. # include "utf8.h"
  35. signed long id3_parse_int(id3_byte_t const **ptr, unsigned int bytes)
  36. {
  37. signed long value = 0;
  38. assert(bytes >= 1 && bytes <= 4);
  39. if (**ptr & 0x80)
  40. value = ~0;
  41. switch (bytes) {
  42. case 4: value = (value << 8) | *(*ptr)++;
  43. case 3: value = (value << 8) | *(*ptr)++;
  44. case 2: value = (value << 8) | *(*ptr)++;
  45. case 1: value = (value << 8) | *(*ptr)++;
  46. }
  47. return value;
  48. }
  49. unsigned long id3_parse_uint(id3_byte_t const **ptr, unsigned int bytes)
  50. {
  51. unsigned long value = 0;
  52. assert(bytes >= 1 && bytes <= 4);
  53. switch (bytes) {
  54. case 4: value = (value << 8) | *(*ptr)++;
  55. case 3: value = (value << 8) | *(*ptr)++;
  56. case 2: value = (value << 8) | *(*ptr)++;
  57. case 1: value = (value << 8) | *(*ptr)++;
  58. }
  59. return value;
  60. }
  61. unsigned long id3_parse_syncsafe(id3_byte_t const **ptr, unsigned int bytes)
  62. {
  63. unsigned long value = 0;
  64. assert(bytes == 4 || bytes == 5);
  65. switch (bytes) {
  66. case 5: value = (value << 4) | (*(*ptr)++ & 0x0f);
  67. case 4: value = (value << 7) | (*(*ptr)++ & 0x7f);
  68. value = (value << 7) | (*(*ptr)++ & 0x7f);
  69. value = (value << 7) | (*(*ptr)++ & 0x7f);
  70. value = (value << 7) | (*(*ptr)++ & 0x7f);
  71. }
  72. return value;
  73. }
  74. void id3_parse_immediate(id3_byte_t const **ptr, unsigned int bytes,
  75. char *value)
  76. {
  77. assert(value);
  78. assert(bytes == 8 || bytes == 4 || bytes == 3);
  79. switch (bytes) {
  80. case 8: *value++ = *(*ptr)++;
  81. *value++ = *(*ptr)++;
  82. *value++ = *(*ptr)++;
  83. *value++ = *(*ptr)++;
  84. case 4: *value++ = *(*ptr)++;
  85. case 3: *value++ = *(*ptr)++;
  86. *value++ = *(*ptr)++;
  87. *value++ = *(*ptr)++;
  88. }
  89. *value = 0;
  90. }
  91. id3_latin1_t *id3_parse_latin1(id3_byte_t const **ptr, id3_length_t length,
  92. int full)
  93. {
  94. id3_byte_t const *end;
  95. int terminated = 0;
  96. id3_latin1_t *latin1;
  97. end = memchr(*ptr, 0, length);
  98. if (end == 0)
  99. end = *ptr + length;
  100. else {
  101. length = end - *ptr;
  102. terminated = 1;
  103. }
  104. latin1 = malloc(length + 1);
  105. if (latin1) {
  106. memcpy(latin1, *ptr, length);
  107. latin1[length] = 0;
  108. if (!full) {
  109. id3_latin1_t *check;
  110. for (check = latin1; *check; ++check) {
  111. if (*check == '\n')
  112. *check = ' ';
  113. }
  114. }
  115. }
  116. *ptr += length + terminated;
  117. return latin1;
  118. }
  119. id3_ucs4_t *id3_parse_string(id3_byte_t const **ptr, id3_length_t length,
  120. enum id3_field_textencoding encoding, int full)
  121. {
  122. id3_ucs4_t *ucs4 = 0;
  123. enum id3_utf16_byteorder byteorder = ID3_UTF16_BYTEORDER_ANY;
  124. switch (encoding) {
  125. case ID3_FIELD_TEXTENCODING_ISO_8859_1:
  126. ucs4 = id3_latin1_deserialize(ptr, length);
  127. break;
  128. case ID3_FIELD_TEXTENCODING_UTF_16BE:
  129. byteorder = ID3_UTF16_BYTEORDER_BE;
  130. case ID3_FIELD_TEXTENCODING_UTF_16:
  131. ucs4 = id3_utf16_deserialize(ptr, length, byteorder);
  132. break;
  133. case ID3_FIELD_TEXTENCODING_UTF_8:
  134. ucs4 = id3_utf8_deserialize(ptr, length);
  135. break;
  136. }
  137. if (ucs4 && !full) {
  138. id3_ucs4_t *check;
  139. for (check = ucs4; *check; ++check) {
  140. if (*check == '\n')
  141. *check = ' ';
  142. }
  143. }
  144. return ucs4;
  145. }
  146. id3_byte_t *id3_parse_binary(id3_byte_t const **ptr, id3_length_t length)
  147. {
  148. id3_byte_t *data;
  149. if (length == 0)
  150. return malloc(1);
  151. data = malloc(length);
  152. if (data)
  153. memcpy(data, *ptr, length);
  154. *ptr += length;
  155. return data;
  156. }