vorbiscomment.c
上传用户:wstnjxml
上传日期:2014-04-03
资源大小:7248k
文件大小:4k
源码类别:

Windows CE

开发平台:

C/C++

  1. /* flac - Command-line FLAC encoder/decoder
  2.  * Copyright (C) 2002,2003,2004,2005  Josh Coalson
  3.  *
  4.  * This program is free software; you can redistribute it and/or
  5.  * modify it under the terms of the GNU General Public License
  6.  * as published by the Free Software Foundation; either version 2
  7.  * of the License, or (at your option) any later version.
  8.  *
  9.  * This program is distributed in the hope that it will be useful,
  10.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.  * GNU General Public License for more details.
  13.  *
  14.  * You should have received a copy of the GNU General Public License
  15.  * along with this program; if not, write to the Free Software
  16.  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  17.  */
  18. #if HAVE_CONFIG_H
  19. #  include <config.h>
  20. #endif
  21. #include "vorbiscomment.h"
  22. #include "FLAC/assert.h"
  23. #include "FLAC/metadata.h"
  24. #include "share/utf8.h"
  25. #include <ctype.h>
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <string.h>
  29. /*
  30.  * This struct and the following 4 static functions are copied from
  31.  * ../metaflac/main.c.  Maybe someday there will be a convenience
  32.  * library for Vorbis comment parsing.
  33.  */
  34. typedef struct {
  35. char *field; /* the whole field as passed on the command line, i.e. "NAME=VALUE" */
  36. char *field_name;
  37. /* according to the vorbis spec, field values can contain  so simple C strings are not enough here */
  38. unsigned field_value_length;
  39. char *field_value;
  40. } Argument_VcField;
  41. static void die(const char *message)
  42. {
  43. FLAC__ASSERT(0 != message);
  44. fprintf(stderr, "ERROR: %sn", message);
  45. exit(1);
  46. }
  47. static char *local_strdup(const char *source)
  48. {
  49. char *ret;
  50. FLAC__ASSERT(0 != source);
  51. if(0 == (ret = strdup(source)))
  52. die("out of memory during strdup()");
  53. return ret;
  54. }
  55. static FLAC__bool parse_vorbis_comment_field(const char *field_ref, char **field, char **name, char **value, unsigned *length, const char **violation)
  56. {
  57. static const char * const violations[] = {
  58. "field name contains invalid character",
  59. "field contains no '=' character"
  60. };
  61. char *p, *q, *s;
  62. if(0 != field)
  63. *field = local_strdup(field_ref);
  64. s = local_strdup(field_ref);
  65. if(0 == (p = strchr(s, '='))) {
  66. free(s);
  67. *violation = violations[1];
  68. return false;
  69. }
  70. *p++ = '';
  71. for(q = s; *q; q++) {
  72. if(*q < 0x20 || *q > 0x7d || *q == 0x3d) {
  73. free(s);
  74. *violation = violations[0];
  75. return false;
  76. }
  77. }
  78. *name = local_strdup(s);
  79. *value = local_strdup(p);
  80. *length = strlen(p);
  81. free(s);
  82. return true;
  83. }
  84. /* slight modification: no 'filename' arg, and errors are passed back in 'violation' instead of printed to stderr */
  85. static FLAC__bool set_vc_field(FLAC__StreamMetadata *block, const Argument_VcField *field, FLAC__bool *needs_write, FLAC__bool raw, const char **violation)
  86. {
  87. FLAC__StreamMetadata_VorbisComment_Entry entry;
  88. char *converted;
  89. FLAC__bool needs_free = false;
  90. FLAC__ASSERT(0 != block);
  91. FLAC__ASSERT(block->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
  92. FLAC__ASSERT(0 != field);
  93. FLAC__ASSERT(0 != needs_write);
  94. if(raw) {
  95. entry.entry = (FLAC__byte *)field->field;
  96. }
  97. else if(utf8_encode(field->field, &converted) >= 0) {
  98. entry.entry = (FLAC__byte *)converted;
  99. needs_free = true;
  100. }
  101. else {
  102. *violation = "couldn't convert comment to UTF-8";
  103. return false;
  104. }
  105. entry.length = strlen((const char *)entry.entry);
  106. if(!FLAC__metadata_object_vorbiscomment_append_comment(block, entry, /*copy=*/true)) {
  107. if(needs_free)
  108. free(converted);
  109. *violation = "memory allocation failure";
  110. return false;
  111. }
  112. else {
  113. *needs_write = true;
  114. if(needs_free)
  115. free(converted);
  116. return true;
  117. }
  118. }
  119. /*
  120.  * The rest of the code is novel
  121.  */
  122. static void free_field(Argument_VcField *obj)
  123. {
  124. if(0 != obj->field)
  125. free(obj->field);
  126. if(0 != obj->field_name)
  127. free(obj->field_name);
  128. if(0 != obj->field_value)
  129. free(obj->field_value);
  130. }
  131. FLAC__bool flac__vorbiscomment_add(FLAC__StreamMetadata *block, const char *comment, const char **violation)
  132. {
  133. Argument_VcField parsed;
  134. FLAC__bool dummy;
  135. FLAC__ASSERT(0 != block);
  136. FLAC__ASSERT(block->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
  137. FLAC__ASSERT(0 != comment);
  138. memset(&parsed, 0, sizeof(parsed));
  139. if(!parse_vorbis_comment_field(comment, &(parsed.field), &(parsed.field_name), &(parsed.field_value), &(parsed.field_value_length), violation)) {
  140. free_field(&parsed);
  141. return false;
  142. }
  143. if(!set_vc_field(block, &parsed, &dummy, /*raw=*/false, violation)) {
  144. free_field(&parsed);
  145. return false;
  146. }
  147. else {
  148. free_field(&parsed);
  149. return true;
  150. }
  151. }