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

Windows CE

开发平台:

C/C++

  1. /* libFLAC - Free Lossless Audio Codec library
  2.  * Copyright (C) 2001,2002,2003,2004,2005  Josh Coalson
  3.  *
  4.  * Redistribution and use in source and binary forms, with or without
  5.  * modification, are permitted provided that the following conditions
  6.  * are met:
  7.  *
  8.  * - Redistributions of source code must retain the above copyright
  9.  * notice, this list of conditions and the following disclaimer.
  10.  *
  11.  * - Redistributions in binary form must reproduce the above copyright
  12.  * notice, this list of conditions and the following disclaimer in the
  13.  * documentation and/or other materials provided with the distribution.
  14.  *
  15.  * - Neither the name of the Xiph.org Foundation nor the names of its
  16.  * contributors may be used to endorse or promote products derived from
  17.  * this software without specific prior written permission.
  18.  *
  19.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20.  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21.  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22.  * A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR
  23.  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  24.  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  25.  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  26.  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  27.  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  28.  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  29.  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30.  */
  31. #include <stdlib.h>
  32. #include <string.h>
  33. #include "private/metadata.h"
  34. #include "FLAC/assert.h"
  35. /****************************************************************************
  36.  *
  37.  * Local routines
  38.  *
  39.  ***************************************************************************/
  40. static FLAC__bool copy_bytes_(FLAC__byte **to, const FLAC__byte *from, unsigned bytes)
  41. {
  42. if(bytes > 0 && 0 != from) {
  43. FLAC__byte *x;
  44. if(0 == (x = (FLAC__byte*)malloc(bytes)))
  45. return false;
  46. memcpy(x, from, bytes);
  47. *to = x;
  48. }
  49. else {
  50. FLAC__ASSERT(0 == from);
  51. FLAC__ASSERT(bytes == 0);
  52. *to = 0;
  53. }
  54. return true;
  55. }
  56. static FLAC__bool ensure_null_terminated_(FLAC__byte **entry, unsigned length)
  57. {
  58. FLAC__byte *x = (FLAC__byte*)realloc(*entry, length+1);
  59. if(0 != x) {
  60. x[length] = '';
  61. *entry = x;
  62. return true;
  63. }
  64. else
  65. return false;
  66. }
  67. static FLAC__bool copy_vcentry_(FLAC__StreamMetadata_VorbisComment_Entry *to, const FLAC__StreamMetadata_VorbisComment_Entry *from)
  68. {
  69. to->length = from->length;
  70. if(0 == from->entry) {
  71. FLAC__ASSERT(from->length == 0);
  72. to->entry = 0;
  73. }
  74. else {
  75. FLAC__byte *x;
  76. FLAC__ASSERT(from->length > 0);
  77. if(0 == (x = (FLAC__byte*)malloc(from->length+1)))
  78. return false;
  79. memcpy(x, from->entry, from->length);
  80. x[from->length] = '';
  81. to->entry = x;
  82. }
  83. return true;
  84. }
  85. static FLAC__bool copy_track_(FLAC__StreamMetadata_CueSheet_Track *to, const FLAC__StreamMetadata_CueSheet_Track *from)
  86. {
  87. memcpy(to, from, sizeof(FLAC__StreamMetadata_CueSheet_Track));
  88. if(0 == from->indices) {
  89. FLAC__ASSERT(from->num_indices == 0);
  90. }
  91. else {
  92. FLAC__StreamMetadata_CueSheet_Index *x;
  93. FLAC__ASSERT(from->num_indices > 0);
  94. if(0 == (x = (FLAC__StreamMetadata_CueSheet_Index*)malloc(from->num_indices * sizeof(FLAC__StreamMetadata_CueSheet_Index))))
  95. return false;
  96. memcpy(x, from->indices, from->num_indices * sizeof(FLAC__StreamMetadata_CueSheet_Index));
  97. to->indices = x;
  98. }
  99. return true;
  100. }
  101. static void seektable_calculate_length_(FLAC__StreamMetadata *object)
  102. {
  103. FLAC__ASSERT(0 != object);
  104. FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
  105. object->length = object->data.seek_table.num_points * FLAC__STREAM_METADATA_SEEKPOINT_LENGTH;
  106. }
  107. static FLAC__StreamMetadata_SeekPoint *seekpoint_array_new_(unsigned num_points)
  108. {
  109. FLAC__StreamMetadata_SeekPoint *object_array;
  110. FLAC__ASSERT(num_points > 0);
  111. object_array = (FLAC__StreamMetadata_SeekPoint*)malloc(num_points * sizeof(FLAC__StreamMetadata_SeekPoint));
  112. if(0 != object_array) {
  113. unsigned i;
  114. for(i = 0; i < num_points; i++) {
  115. object_array[i].sample_number = FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER;
  116. object_array[i].stream_offset = 0;
  117. object_array[i].frame_samples = 0;
  118. }
  119. }
  120. return object_array;
  121. }
  122. static void vorbiscomment_calculate_length_(FLAC__StreamMetadata *object)
  123. {
  124. unsigned i;
  125. FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
  126. object->length = (FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN) / 8;
  127. object->length += object->data.vorbis_comment.vendor_string.length;
  128. object->length += (FLAC__STREAM_METADATA_VORBIS_COMMENT_NUM_COMMENTS_LEN) / 8;
  129. for(i = 0; i < object->data.vorbis_comment.num_comments; i++) {
  130. object->length += (FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN / 8);
  131. object->length += object->data.vorbis_comment.comments[i].length;
  132. }
  133. }
  134. static FLAC__StreamMetadata_VorbisComment_Entry *vorbiscomment_entry_array_new_(unsigned num_comments)
  135. {
  136. FLAC__ASSERT(num_comments > 0);
  137. return (FLAC__StreamMetadata_VorbisComment_Entry*)calloc(num_comments, sizeof(FLAC__StreamMetadata_VorbisComment_Entry));
  138. }
  139. static void vorbiscomment_entry_array_delete_(FLAC__StreamMetadata_VorbisComment_Entry *object_array, unsigned num_comments)
  140. {
  141. unsigned i;
  142. FLAC__ASSERT(0 != object_array && num_comments > 0);
  143. for(i = 0; i < num_comments; i++)
  144. if(0 != object_array[i].entry)
  145. free(object_array[i].entry);
  146. if(0 != object_array)
  147. free(object_array);
  148. }
  149. static FLAC__StreamMetadata_VorbisComment_Entry *vorbiscomment_entry_array_copy_(const FLAC__StreamMetadata_VorbisComment_Entry *object_array, unsigned num_comments)
  150. {
  151. FLAC__StreamMetadata_VorbisComment_Entry *return_array;
  152. FLAC__ASSERT(0 != object_array);
  153. FLAC__ASSERT(num_comments > 0);
  154. return_array = vorbiscomment_entry_array_new_(num_comments);
  155. if(0 != return_array) {
  156. unsigned i;
  157. for(i = 0; i < num_comments; i++) {
  158. if(!copy_vcentry_(return_array+i, object_array+i)) {
  159. vorbiscomment_entry_array_delete_(return_array, num_comments);
  160. return 0;
  161. }
  162. }
  163. }
  164. return return_array;
  165. }
  166. static FLAC__bool vorbiscomment_set_entry_(FLAC__StreamMetadata *object, FLAC__StreamMetadata_VorbisComment_Entry *dest, const FLAC__StreamMetadata_VorbisComment_Entry *src, FLAC__bool copy)
  167. {
  168. FLAC__byte *save;
  169. FLAC__ASSERT(0 != object);
  170. FLAC__ASSERT(0 != dest);
  171. FLAC__ASSERT(0 != src);
  172. FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
  173. FLAC__ASSERT((0 != src->entry && src->length > 0) || (0 == src->entry && src->length == 0));
  174. save = dest->entry;
  175. if(0 != src->entry && src->length > 0) {
  176. if(copy) {
  177. /* do the copy first so that if we fail we leave the dest object untouched */
  178. if(!copy_vcentry_(dest, src))
  179. return false;
  180. }
  181. else {
  182. /* we have to make sure that the string we're taking over is null-terminated */
  183. /*
  184.  * Stripping the const from src->entry is OK since we're taking
  185.  * ownership of the pointer.  This is a hack around a deficiency
  186.  * in the API where the same function is used for 'copy' and
  187.  * 'own', but the source entry is a const pointer.  If we were
  188.  * precise, the 'own' flavor would be a separate function with a
  189.  * non-const source pointer.  But it's not, so we hack away.
  190.  */
  191. if(!ensure_null_terminated_((FLAC__byte**)(&src->entry), src->length))
  192. return false;
  193. *dest = *src;
  194. }
  195. }
  196. else {
  197. /* the src is null */
  198. *dest = *src;
  199. }
  200. if(0 != save)
  201. free(save);
  202. vorbiscomment_calculate_length_(object);
  203. return true;
  204. }
  205. static int vorbiscomment_find_entry_from_(const FLAC__StreamMetadata *object, unsigned offset, const char *field_name, unsigned field_name_length)
  206. {
  207. unsigned i;
  208. FLAC__ASSERT(0 != object);
  209. FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
  210. FLAC__ASSERT(0 != field_name);
  211. for(i = offset; i < object->data.vorbis_comment.num_comments; i++) {
  212. if(FLAC__metadata_object_vorbiscomment_entry_matches(object->data.vorbis_comment.comments[i], field_name, field_name_length))
  213. return (int)i;
  214. }
  215. return -1;
  216. }
  217. static void cuesheet_calculate_length_(FLAC__StreamMetadata *object)
  218. {
  219. unsigned i;
  220. FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_CUESHEET);
  221. object->length = (
  222. FLAC__STREAM_METADATA_CUESHEET_MEDIA_CATALOG_NUMBER_LEN +
  223. FLAC__STREAM_METADATA_CUESHEET_LEAD_IN_LEN +
  224. FLAC__STREAM_METADATA_CUESHEET_IS_CD_LEN +
  225. FLAC__STREAM_METADATA_CUESHEET_RESERVED_LEN +
  226. FLAC__STREAM_METADATA_CUESHEET_NUM_TRACKS_LEN
  227. ) / 8;
  228. object->length += object->data.cue_sheet.num_tracks * (
  229. FLAC__STREAM_METADATA_CUESHEET_TRACK_OFFSET_LEN +
  230. FLAC__STREAM_METADATA_CUESHEET_TRACK_NUMBER_LEN +
  231. FLAC__STREAM_METADATA_CUESHEET_TRACK_ISRC_LEN +
  232. FLAC__STREAM_METADATA_CUESHEET_TRACK_TYPE_LEN +
  233. FLAC__STREAM_METADATA_CUESHEET_TRACK_PRE_EMPHASIS_LEN +
  234. FLAC__STREAM_METADATA_CUESHEET_TRACK_RESERVED_LEN +
  235. FLAC__STREAM_METADATA_CUESHEET_TRACK_NUM_INDICES_LEN
  236. ) / 8;
  237. for(i = 0; i < object->data.cue_sheet.num_tracks; i++) {
  238. object->length += object->data.cue_sheet.tracks[i].num_indices * (
  239. FLAC__STREAM_METADATA_CUESHEET_INDEX_OFFSET_LEN +
  240. FLAC__STREAM_METADATA_CUESHEET_INDEX_NUMBER_LEN +
  241. FLAC__STREAM_METADATA_CUESHEET_INDEX_RESERVED_LEN
  242. ) / 8;
  243. }
  244. }
  245. static FLAC__StreamMetadata_CueSheet_Index *cuesheet_track_index_array_new_(unsigned num_indices)
  246. {
  247. FLAC__ASSERT(num_indices > 0);
  248. return (FLAC__StreamMetadata_CueSheet_Index*)calloc(num_indices, sizeof(FLAC__StreamMetadata_CueSheet_Index));
  249. }
  250. static FLAC__StreamMetadata_CueSheet_Track *cuesheet_track_array_new_(unsigned num_tracks)
  251. {
  252. FLAC__ASSERT(num_tracks > 0);
  253. return (FLAC__StreamMetadata_CueSheet_Track*)calloc(num_tracks, sizeof(FLAC__StreamMetadata_CueSheet_Track));
  254. }
  255. static void cuesheet_track_array_delete_(FLAC__StreamMetadata_CueSheet_Track *object_array, unsigned num_tracks)
  256. {
  257. unsigned i;
  258. FLAC__ASSERT(0 != object_array && num_tracks > 0);
  259. for(i = 0; i < num_tracks; i++) {
  260. if(0 != object_array[i].indices) {
  261. FLAC__ASSERT(object_array[i].num_indices > 0);
  262. free(object_array[i].indices);
  263. }
  264. }
  265. if(0 != object_array)
  266. free(object_array);
  267. }
  268. static FLAC__StreamMetadata_CueSheet_Track *cuesheet_track_array_copy_(const FLAC__StreamMetadata_CueSheet_Track *object_array, unsigned num_tracks)
  269. {
  270. FLAC__StreamMetadata_CueSheet_Track *return_array;
  271. FLAC__ASSERT(0 != object_array);
  272. FLAC__ASSERT(num_tracks > 0);
  273. return_array = cuesheet_track_array_new_(num_tracks);
  274. if(0 != return_array) {
  275. unsigned i;
  276. for(i = 0; i < num_tracks; i++) {
  277. if(!copy_track_(return_array+i, object_array+i)) {
  278. cuesheet_track_array_delete_(return_array, num_tracks);
  279. return 0;
  280. }
  281. }
  282. }
  283. return return_array;
  284. }
  285. static FLAC__bool cuesheet_set_track_(FLAC__StreamMetadata *object, FLAC__StreamMetadata_CueSheet_Track *dest, const FLAC__StreamMetadata_CueSheet_Track *src, FLAC__bool copy)
  286. {
  287. FLAC__StreamMetadata_CueSheet_Index *save;
  288. FLAC__ASSERT(0 != object);
  289. FLAC__ASSERT(0 != dest);
  290. FLAC__ASSERT(0 != src);
  291. FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_CUESHEET);
  292. FLAC__ASSERT((0 != src->indices && src->num_indices > 0) || (0 == src->indices && src->num_indices == 0));
  293. save = dest->indices;
  294. /* do the copy first so that if we fail we leave the object untouched */
  295. if(copy) {
  296. if(!copy_track_(dest, src))
  297. return false;
  298. }
  299. else {
  300. *dest = *src;
  301. }
  302. if(0 != save)
  303. free(save);
  304. cuesheet_calculate_length_(object);
  305. return true;
  306. }
  307. /****************************************************************************
  308.  *
  309.  * Metadata object routines
  310.  *
  311.  ***************************************************************************/
  312. FLAC_API FLAC__StreamMetadata *FLAC__metadata_object_new(FLAC__MetadataType type)
  313. {
  314. FLAC__StreamMetadata *object;
  315. if(type > FLAC__MAX_METADATA_TYPE_CODE)
  316. return 0;
  317. object = (FLAC__StreamMetadata*)calloc(1, sizeof(FLAC__StreamMetadata));
  318. if(0 != object) {
  319. object->is_last = false;
  320. object->type = type;
  321. switch(type) {
  322. case FLAC__METADATA_TYPE_STREAMINFO:
  323. object->length = FLAC__STREAM_METADATA_STREAMINFO_LENGTH;
  324. break;
  325. case FLAC__METADATA_TYPE_PADDING:
  326. /* calloc() took care of this for us:
  327. object->length = 0;
  328. */
  329. break;
  330. case FLAC__METADATA_TYPE_APPLICATION:
  331. object->length = FLAC__STREAM_METADATA_APPLICATION_ID_LEN / 8;
  332. /* calloc() took care of this for us:
  333. object->data.application.data = 0;
  334. */
  335. break;
  336. case FLAC__METADATA_TYPE_SEEKTABLE:
  337. /* calloc() took care of this for us:
  338. object->length = 0;
  339. object->data.seek_table.num_points = 0;
  340. object->data.seek_table.points = 0;
  341. */
  342. break;
  343. case FLAC__METADATA_TYPE_VORBIS_COMMENT:
  344. {
  345. object->data.vorbis_comment.vendor_string.length = (unsigned)strlen(FLAC__VENDOR_STRING);
  346. if(!copy_bytes_(&object->data.vorbis_comment.vendor_string.entry, (const FLAC__byte*)FLAC__VENDOR_STRING, object->data.vorbis_comment.vendor_string.length+1)) {
  347. free(object);
  348. return 0;
  349. }
  350. vorbiscomment_calculate_length_(object);
  351. }
  352. break;
  353. case FLAC__METADATA_TYPE_CUESHEET:
  354. cuesheet_calculate_length_(object);
  355. break;
  356. default:
  357. /* calloc() took care of this for us:
  358. object->length = 0;
  359. object->data.unknown.data = 0;
  360. */
  361. break;
  362. }
  363. }
  364. return object;
  365. }
  366. FLAC_API FLAC__StreamMetadata *FLAC__metadata_object_clone(const FLAC__StreamMetadata *object)
  367. {
  368. FLAC__StreamMetadata *to;
  369. FLAC__ASSERT(0 != object);
  370. if(0 != (to = FLAC__metadata_object_new(object->type))) {
  371. to->is_last = object->is_last;
  372. to->type = object->type;
  373. to->length = object->length;
  374. switch(to->type) {
  375. case FLAC__METADATA_TYPE_STREAMINFO:
  376. memcpy(&to->data.stream_info, &object->data.stream_info, sizeof(FLAC__StreamMetadata_StreamInfo));
  377. break;
  378. case FLAC__METADATA_TYPE_PADDING:
  379. break;
  380. case FLAC__METADATA_TYPE_APPLICATION:
  381. memcpy(&to->data.application.id, &object->data.application.id, FLAC__STREAM_METADATA_APPLICATION_ID_LEN / 8);
  382. if(!copy_bytes_(&to->data.application.data, object->data.application.data, object->length - FLAC__STREAM_METADATA_APPLICATION_ID_LEN / 8)) {
  383. FLAC__metadata_object_delete(to);
  384. return 0;
  385. }
  386. break;
  387. case FLAC__METADATA_TYPE_SEEKTABLE:
  388. to->data.seek_table.num_points = object->data.seek_table.num_points;
  389. if(!copy_bytes_((FLAC__byte**)&to->data.seek_table.points, (FLAC__byte*)object->data.seek_table.points, object->data.seek_table.num_points * sizeof(FLAC__StreamMetadata_SeekPoint))) {
  390. FLAC__metadata_object_delete(to);
  391. return 0;
  392. }
  393. break;
  394. case FLAC__METADATA_TYPE_VORBIS_COMMENT:
  395. if(0 != to->data.vorbis_comment.vendor_string.entry) {
  396. free(to->data.vorbis_comment.vendor_string.entry);
  397. to->data.vorbis_comment.vendor_string.entry = 0;
  398. }
  399. if(!copy_vcentry_(&to->data.vorbis_comment.vendor_string, &object->data.vorbis_comment.vendor_string)) {
  400. FLAC__metadata_object_delete(to);
  401. return 0;
  402. }
  403. if(object->data.vorbis_comment.num_comments == 0) {
  404. FLAC__ASSERT(0 == object->data.vorbis_comment.comments);
  405. to->data.vorbis_comment.comments = 0;
  406. }
  407. else {
  408. FLAC__ASSERT(0 != object->data.vorbis_comment.comments);
  409. to->data.vorbis_comment.comments = vorbiscomment_entry_array_copy_(object->data.vorbis_comment.comments, object->data.vorbis_comment.num_comments);
  410. if(0 == to->data.vorbis_comment.comments) {
  411. FLAC__metadata_object_delete(to);
  412. return 0;
  413. }
  414. }
  415. to->data.vorbis_comment.num_comments = object->data.vorbis_comment.num_comments;
  416. break;
  417. case FLAC__METADATA_TYPE_CUESHEET:
  418. memcpy(&to->data.cue_sheet, &object->data.cue_sheet, sizeof(FLAC__StreamMetadata_CueSheet));
  419. if(object->data.cue_sheet.num_tracks == 0) {
  420. FLAC__ASSERT(0 == object->data.cue_sheet.tracks);
  421. }
  422. else {
  423. FLAC__ASSERT(0 != object->data.cue_sheet.tracks);
  424. to->data.cue_sheet.tracks = cuesheet_track_array_copy_(object->data.cue_sheet.tracks, object->data.cue_sheet.num_tracks);
  425. if(0 == to->data.cue_sheet.tracks) {
  426. FLAC__metadata_object_delete(to);
  427. return 0;
  428. }
  429. }
  430. break;
  431. default:
  432. if(!copy_bytes_(&to->data.unknown.data, object->data.unknown.data, object->length)) {
  433. FLAC__metadata_object_delete(to);
  434. return 0;
  435. }
  436. break;
  437. }
  438. }
  439. return to;
  440. }
  441. void FLAC__metadata_object_delete_data(FLAC__StreamMetadata *object)
  442. {
  443. FLAC__ASSERT(0 != object);
  444. switch(object->type) {
  445. case FLAC__METADATA_TYPE_STREAMINFO:
  446. case FLAC__METADATA_TYPE_PADDING:
  447. break;
  448. case FLAC__METADATA_TYPE_APPLICATION:
  449. if(0 != object->data.application.data) {
  450. free(object->data.application.data);
  451. object->data.application.data = 0;
  452. }
  453. break;
  454. case FLAC__METADATA_TYPE_SEEKTABLE:
  455. if(0 != object->data.seek_table.points) {
  456. free(object->data.seek_table.points);
  457. object->data.seek_table.points = 0;
  458. }
  459. break;
  460. case FLAC__METADATA_TYPE_VORBIS_COMMENT:
  461. if(0 != object->data.vorbis_comment.vendor_string.entry) {
  462. free(object->data.vorbis_comment.vendor_string.entry);
  463. object->data.vorbis_comment.vendor_string.entry = 0;
  464. }
  465. if(0 != object->data.vorbis_comment.comments) {
  466. FLAC__ASSERT(object->data.vorbis_comment.num_comments > 0);
  467. vorbiscomment_entry_array_delete_(object->data.vorbis_comment.comments, object->data.vorbis_comment.num_comments);
  468. }
  469. break;
  470. case FLAC__METADATA_TYPE_CUESHEET:
  471. if(0 != object->data.cue_sheet.tracks) {
  472. FLAC__ASSERT(object->data.cue_sheet.num_tracks > 0);
  473. cuesheet_track_array_delete_(object->data.cue_sheet.tracks, object->data.cue_sheet.num_tracks);
  474. }
  475. break;
  476. default:
  477. if(0 != object->data.unknown.data) {
  478. free(object->data.unknown.data);
  479. object->data.unknown.data = 0;
  480. }
  481. break;
  482. }
  483. }
  484. FLAC_API void FLAC__metadata_object_delete(FLAC__StreamMetadata *object)
  485. {
  486. FLAC__metadata_object_delete_data(object);
  487. free(object);
  488. }
  489. static FLAC__bool compare_block_data_streaminfo_(const FLAC__StreamMetadata_StreamInfo *block1, const FLAC__StreamMetadata_StreamInfo *block2)
  490. {
  491. if(block1->min_blocksize != block2->min_blocksize)
  492. return false;
  493. if(block1->max_blocksize != block2->max_blocksize)
  494. return false;
  495. if(block1->min_framesize != block2->min_framesize)
  496. return false;
  497. if(block1->max_framesize != block2->max_framesize)
  498. return false;
  499. if(block1->sample_rate != block2->sample_rate)
  500. return false;
  501. if(block1->channels != block2->channels)
  502. return false;
  503. if(block1->bits_per_sample != block2->bits_per_sample)
  504. return false;
  505. if(block1->total_samples != block2->total_samples)
  506. return false;
  507. if(0 != memcmp(block1->md5sum, block2->md5sum, 16))
  508. return false;
  509. return true;
  510. }
  511. static FLAC__bool compare_block_data_application_(const FLAC__StreamMetadata_Application *block1, const FLAC__StreamMetadata_Application *block2, unsigned block_length)
  512. {
  513. FLAC__ASSERT(0 != block1);
  514. FLAC__ASSERT(0 != block2);
  515. FLAC__ASSERT(block_length >= sizeof(block1->id));
  516. if(0 != memcmp(block1->id, block2->id, sizeof(block1->id)))
  517. return false;
  518. if(0 != block1->data && 0 != block2->data)
  519. return 0 == memcmp(block1->data, block2->data, block_length - sizeof(block1->id));
  520. else
  521. return block1->data == block2->data;
  522. }
  523. static FLAC__bool compare_block_data_seektable_(const FLAC__StreamMetadata_SeekTable *block1, const FLAC__StreamMetadata_SeekTable *block2)
  524. {
  525. unsigned i;
  526. FLAC__ASSERT(0 != block1);
  527. FLAC__ASSERT(0 != block2);
  528. if(block1->num_points != block2->num_points)
  529. return false;
  530. if(0 != block1->points && 0 != block2->points) {
  531. for(i = 0; i < block1->num_points; i++) {
  532. if(block1->points[i].sample_number != block2->points[i].sample_number)
  533. return false;
  534. if(block1->points[i].stream_offset != block2->points[i].stream_offset)
  535. return false;
  536. if(block1->points[i].frame_samples != block2->points[i].frame_samples)
  537. return false;
  538. }
  539. return true;
  540. }
  541. else
  542. return block1->points == block2->points;
  543. }
  544. static FLAC__bool compare_block_data_vorbiscomment_(const FLAC__StreamMetadata_VorbisComment *block1, const FLAC__StreamMetadata_VorbisComment *block2)
  545. {
  546. unsigned i;
  547. if(block1->vendor_string.length != block2->vendor_string.length)
  548. return false;
  549. if(0 != block1->vendor_string.entry && 0 != block2->vendor_string.entry) {
  550. if(0 != memcmp(block1->vendor_string.entry, block2->vendor_string.entry, block1->vendor_string.length))
  551. return false;
  552. }
  553. else if(block1->vendor_string.entry != block2->vendor_string.entry)
  554. return false;
  555. if(block1->num_comments != block2->num_comments)
  556. return false;
  557. for(i = 0; i < block1->num_comments; i++) {
  558. if(0 != block1->comments[i].entry && 0 != block2->comments[i].entry) {
  559. if(0 != memcmp(block1->comments[i].entry, block2->comments[i].entry, block1->comments[i].length))
  560. return false;
  561. }
  562. else if(block1->comments[i].entry != block2->comments[i].entry)
  563. return false;
  564. }
  565. return true;
  566. }
  567. static FLAC__bool compare_block_data_cuesheet_(const FLAC__StreamMetadata_CueSheet *block1, const FLAC__StreamMetadata_CueSheet *block2)
  568. {
  569. unsigned i, j;
  570. if(0 != strcmp(block1->media_catalog_number, block2->media_catalog_number))
  571. return false;
  572. if(block1->lead_in != block2->lead_in)
  573. return false;
  574. if(block1->is_cd != block2->is_cd)
  575. return false;
  576. if(block1->num_tracks != block2->num_tracks)
  577. return false;
  578. if(0 != block1->tracks && 0 != block2->tracks) {
  579. FLAC__ASSERT(block1->num_tracks > 0);
  580. for(i = 0; i < block1->num_tracks; i++) {
  581. if(block1->tracks[i].offset != block2->tracks[i].offset)
  582. return false;
  583. if(block1->tracks[i].number != block2->tracks[i].number)
  584. return false;
  585. if(0 != memcmp(block1->tracks[i].isrc, block2->tracks[i].isrc, sizeof(block1->tracks[i].isrc)))
  586. return false;
  587. if(block1->tracks[i].type != block2->tracks[i].type)
  588. return false;
  589. if(block1->tracks[i].pre_emphasis != block2->tracks[i].pre_emphasis)
  590. return false;
  591. if(block1->tracks[i].num_indices != block2->tracks[i].num_indices)
  592. return false;
  593. if(0 != block1->tracks[i].indices && 0 != block2->tracks[i].indices) {
  594. FLAC__ASSERT(block1->tracks[i].num_indices > 0);
  595. for(j = 0; j < block1->tracks[i].num_indices; j++) {
  596. if(block1->tracks[i].indices[j].offset != block2->tracks[i].indices[j].offset)
  597. return false;
  598. if(block1->tracks[i].indices[j].number != block2->tracks[i].indices[j].number)
  599. return false;
  600. }
  601. }
  602. else if(block1->tracks[i].indices != block2->tracks[i].indices)
  603. return false;
  604. }
  605. }
  606. else if(block1->tracks != block2->tracks)
  607. return false;
  608. return true;
  609. }
  610. static FLAC__bool compare_block_data_unknown_(const FLAC__StreamMetadata_Unknown *block1, const FLAC__StreamMetadata_Unknown *block2, unsigned block_length)
  611. {
  612. FLAC__ASSERT(0 != block1);
  613. FLAC__ASSERT(0 != block2);
  614. if(0 != block1->data && 0 != block2->data)
  615. return 0 == memcmp(block1->data, block2->data, block_length);
  616. else
  617. return block1->data == block2->data;
  618. }
  619. FLAC_API FLAC__bool FLAC__metadata_object_is_equal(const FLAC__StreamMetadata *block1, const FLAC__StreamMetadata *block2)
  620. {
  621. FLAC__ASSERT(0 != block1);
  622. FLAC__ASSERT(0 != block2);
  623. if(block1->type != block2->type) {
  624. return false;
  625. }
  626. if(block1->is_last != block2->is_last) {
  627. return false;
  628. }
  629. if(block1->length != block2->length) {
  630. return false;
  631. }
  632. switch(block1->type) {
  633. case FLAC__METADATA_TYPE_STREAMINFO:
  634. return compare_block_data_streaminfo_(&block1->data.stream_info, &block2->data.stream_info);
  635. case FLAC__METADATA_TYPE_PADDING:
  636. return true; /* we don't compare the padding guts */
  637. case FLAC__METADATA_TYPE_APPLICATION:
  638. return compare_block_data_application_(&block1->data.application, &block2->data.application, block1->length);
  639. case FLAC__METADATA_TYPE_SEEKTABLE:
  640. return compare_block_data_seektable_(&block1->data.seek_table, &block2->data.seek_table);
  641. case FLAC__METADATA_TYPE_VORBIS_COMMENT:
  642. return compare_block_data_vorbiscomment_(&block1->data.vorbis_comment, &block2->data.vorbis_comment);
  643. case FLAC__METADATA_TYPE_CUESHEET:
  644. return compare_block_data_cuesheet_(&block1->data.cue_sheet, &block2->data.cue_sheet);
  645. default:
  646. return compare_block_data_unknown_(&block1->data.unknown, &block2->data.unknown, block1->length);
  647. }
  648. }
  649. FLAC_API FLAC__bool FLAC__metadata_object_application_set_data(FLAC__StreamMetadata *object, FLAC__byte *data, unsigned length, FLAC__bool copy)
  650. {
  651. FLAC__byte *save;
  652. FLAC__ASSERT(0 != object);
  653. FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_APPLICATION);
  654. FLAC__ASSERT((0 != data && length > 0) || (0 == data && length == 0 && copy == false));
  655. save = object->data.application.data;
  656. /* do the copy first so that if we fail we leave the object untouched */
  657. if(copy) {
  658. if(!copy_bytes_(&object->data.application.data, data, length))
  659. return false;
  660. }
  661. else {
  662. object->data.application.data = data;
  663. }
  664. if(0 != save)
  665. free(save);
  666. object->length = FLAC__STREAM_METADATA_APPLICATION_ID_LEN / 8 + length;
  667. return true;
  668. }
  669. FLAC_API FLAC__bool FLAC__metadata_object_seektable_resize_points(FLAC__StreamMetadata *object, unsigned new_num_points)
  670. {
  671. FLAC__ASSERT(0 != object);
  672. FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
  673. if(0 == object->data.seek_table.points) {
  674. FLAC__ASSERT(object->data.seek_table.num_points == 0);
  675. if(0 == new_num_points)
  676. return true;
  677. else if(0 == (object->data.seek_table.points = seekpoint_array_new_(new_num_points)))
  678. return false;
  679. }
  680. else {
  681. const unsigned old_size = object->data.seek_table.num_points * sizeof(FLAC__StreamMetadata_SeekPoint);
  682. const unsigned new_size = new_num_points * sizeof(FLAC__StreamMetadata_SeekPoint);
  683. FLAC__ASSERT(object->data.seek_table.num_points > 0);
  684. if(new_size == 0) {
  685. free(object->data.seek_table.points);
  686. object->data.seek_table.points = 0;
  687. }
  688. else if(0 == (object->data.seek_table.points = (FLAC__StreamMetadata_SeekPoint*)realloc(object->data.seek_table.points, new_size)))
  689. return false;
  690. /* if growing, set new elements to placeholders */
  691. if(new_size > old_size) {
  692. unsigned i;
  693. for(i = object->data.seek_table.num_points; i < new_num_points; i++) {
  694. object->data.seek_table.points[i].sample_number = FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER;
  695. object->data.seek_table.points[i].stream_offset = 0;
  696. object->data.seek_table.points[i].frame_samples = 0;
  697. }
  698. }
  699. }
  700. object->data.seek_table.num_points = new_num_points;
  701. seektable_calculate_length_(object);
  702. return true;
  703. }
  704. FLAC_API void FLAC__metadata_object_seektable_set_point(FLAC__StreamMetadata *object, unsigned point_num, FLAC__StreamMetadata_SeekPoint point)
  705. {
  706. FLAC__ASSERT(0 != object);
  707. FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
  708. FLAC__ASSERT(point_num < object->data.seek_table.num_points);
  709. object->data.seek_table.points[point_num] = point;
  710. }
  711. FLAC_API FLAC__bool FLAC__metadata_object_seektable_insert_point(FLAC__StreamMetadata *object, unsigned point_num, FLAC__StreamMetadata_SeekPoint point)
  712. {
  713. int i;
  714. FLAC__ASSERT(0 != object);
  715. FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
  716. FLAC__ASSERT(point_num <= object->data.seek_table.num_points);
  717. if(!FLAC__metadata_object_seektable_resize_points(object, object->data.seek_table.num_points+1))
  718. return false;
  719. /* move all points >= point_num forward one space */
  720. for(i = (int)object->data.seek_table.num_points-1; i > (int)point_num; i--)
  721. object->data.seek_table.points[i] = object->data.seek_table.points[i-1];
  722. FLAC__metadata_object_seektable_set_point(object, point_num, point);
  723. seektable_calculate_length_(object);
  724. return true;
  725. }
  726. FLAC_API FLAC__bool FLAC__metadata_object_seektable_delete_point(FLAC__StreamMetadata *object, unsigned point_num)
  727. {
  728. unsigned i;
  729. FLAC__ASSERT(0 != object);
  730. FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
  731. FLAC__ASSERT(point_num < object->data.seek_table.num_points);
  732. /* move all points > point_num backward one space */
  733. for(i = point_num; i < object->data.seek_table.num_points-1; i++)
  734. object->data.seek_table.points[i] = object->data.seek_table.points[i+1];
  735. return FLAC__metadata_object_seektable_resize_points(object, object->data.seek_table.num_points-1);
  736. }
  737. FLAC_API FLAC__bool FLAC__metadata_object_seektable_is_legal(const FLAC__StreamMetadata *object)
  738. {
  739. FLAC__ASSERT(0 != object);
  740. FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
  741. return FLAC__format_seektable_is_legal(&object->data.seek_table);
  742. }
  743. FLAC_API FLAC__bool FLAC__metadata_object_seektable_template_append_placeholders(FLAC__StreamMetadata *object, unsigned num)
  744. {
  745. FLAC__ASSERT(0 != object);
  746. FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
  747. if(num > 0)
  748. /* WATCHOUT: we rely on the fact that growing the array adds PLACEHOLDERS at the end */
  749. return FLAC__metadata_object_seektable_resize_points(object, object->data.seek_table.num_points + num);
  750. else
  751. return true;
  752. }
  753. FLAC_API FLAC__bool FLAC__metadata_object_seektable_template_append_point(FLAC__StreamMetadata *object, FLAC__uint64 sample_number)
  754. {
  755. FLAC__StreamMetadata_SeekTable *seek_table;
  756. FLAC__ASSERT(0 != object);
  757. FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
  758. seek_table = &object->data.seek_table;
  759. if(!FLAC__metadata_object_seektable_resize_points(object, seek_table->num_points + 1))
  760. return false;
  761. seek_table->points[seek_table->num_points - 1].sample_number = sample_number;
  762. seek_table->points[seek_table->num_points - 1].stream_offset = 0;
  763. seek_table->points[seek_table->num_points - 1].frame_samples = 0;
  764. return true;
  765. }
  766. FLAC_API FLAC__bool FLAC__metadata_object_seektable_template_append_points(FLAC__StreamMetadata *object, FLAC__uint64 sample_numbers[], unsigned num)
  767. {
  768. FLAC__ASSERT(0 != object);
  769. FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
  770. FLAC__ASSERT(0 != sample_numbers || num == 0);
  771. if(num > 0) {
  772. FLAC__StreamMetadata_SeekTable *seek_table = &object->data.seek_table;
  773. unsigned i, j;
  774. i = seek_table->num_points;
  775. if(!FLAC__metadata_object_seektable_resize_points(object, seek_table->num_points + num))
  776. return false;
  777. for(j = 0; j < num; i++, j++) {
  778. seek_table->points[i].sample_number = sample_numbers[j];
  779. seek_table->points[i].stream_offset = 0;
  780. seek_table->points[i].frame_samples = 0;
  781. }
  782. }
  783. return true;
  784. }
  785. FLAC_API FLAC__bool FLAC__metadata_object_seektable_template_append_spaced_points(FLAC__StreamMetadata *object, unsigned num, FLAC__uint64 total_samples)
  786. {
  787. FLAC__ASSERT(0 != object);
  788. FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
  789. FLAC__ASSERT(total_samples > 0);
  790. if(num > 0) {
  791. FLAC__StreamMetadata_SeekTable *seek_table = &object->data.seek_table;
  792. unsigned i, j;
  793. i = seek_table->num_points;
  794. if(!FLAC__metadata_object_seektable_resize_points(object, seek_table->num_points + num))
  795. return false;
  796. for(j = 0; j < num; i++, j++) {
  797. seek_table->points[i].sample_number = total_samples * (FLAC__uint64)j / (FLAC__uint64)num;
  798. seek_table->points[i].stream_offset = 0;
  799. seek_table->points[i].frame_samples = 0;
  800. }
  801. }
  802. return true;
  803. }
  804. FLAC_API FLAC__bool FLAC__metadata_object_seektable_template_sort(FLAC__StreamMetadata *object, FLAC__bool compact)
  805. {
  806. unsigned unique;
  807. FLAC__ASSERT(0 != object);
  808. FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
  809. unique = FLAC__format_seektable_sort(&object->data.seek_table);
  810. return !compact || FLAC__metadata_object_seektable_resize_points(object, unique);
  811. }
  812. FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_set_vendor_string(FLAC__StreamMetadata *object, FLAC__StreamMetadata_VorbisComment_Entry entry, FLAC__bool copy)
  813. {
  814. if(!FLAC__format_vorbiscomment_entry_value_is_legal(entry.entry, entry.length))
  815. return false;
  816. return vorbiscomment_set_entry_(object, &object->data.vorbis_comment.vendor_string, &entry, copy);
  817. }
  818. FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_resize_comments(FLAC__StreamMetadata *object, unsigned new_num_comments)
  819. {
  820. FLAC__ASSERT(0 != object);
  821. FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
  822. if(0 == object->data.vorbis_comment.comments) {
  823. FLAC__ASSERT(object->data.vorbis_comment.num_comments == 0);
  824. if(0 == new_num_comments)
  825. return true;
  826. else if(0 == (object->data.vorbis_comment.comments = vorbiscomment_entry_array_new_(new_num_comments)))
  827. return false;
  828. }
  829. else {
  830. const unsigned old_size = object->data.vorbis_comment.num_comments * sizeof(FLAC__StreamMetadata_VorbisComment_Entry);
  831. const unsigned new_size = new_num_comments * sizeof(FLAC__StreamMetadata_VorbisComment_Entry);
  832. FLAC__ASSERT(object->data.vorbis_comment.num_comments > 0);
  833. /* if shrinking, free the truncated entries */
  834. if(new_num_comments < object->data.vorbis_comment.num_comments) {
  835. unsigned i;
  836. for(i = new_num_comments; i < object->data.vorbis_comment.num_comments; i++)
  837. if(0 != object->data.vorbis_comment.comments[i].entry)
  838. free(object->data.vorbis_comment.comments[i].entry);
  839. }
  840. if(new_size == 0) {
  841. free(object->data.vorbis_comment.comments);
  842. object->data.vorbis_comment.comments = 0;
  843. }
  844. else if(0 == (object->data.vorbis_comment.comments = (FLAC__StreamMetadata_VorbisComment_Entry*)realloc(object->data.vorbis_comment.comments, new_size)))
  845. return false;
  846. /* if growing, zero all the length/pointers of new elements */
  847. if(new_size > old_size)
  848. memset(object->data.vorbis_comment.comments + object->data.vorbis_comment.num_comments, 0, new_size - old_size);
  849. }
  850. object->data.vorbis_comment.num_comments = new_num_comments;
  851. vorbiscomment_calculate_length_(object);
  852. return true;
  853. }
  854. FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_set_comment(FLAC__StreamMetadata *object, unsigned comment_num, FLAC__StreamMetadata_VorbisComment_Entry entry, FLAC__bool copy)
  855. {
  856. FLAC__ASSERT(0 != object);
  857. FLAC__ASSERT(comment_num < object->data.vorbis_comment.num_comments);
  858. if(!FLAC__format_vorbiscomment_entry_is_legal(entry.entry, entry.length))
  859. return false;
  860. return vorbiscomment_set_entry_(object, &object->data.vorbis_comment.comments[comment_num], &entry, copy);
  861. }
  862. FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_insert_comment(FLAC__StreamMetadata *object, unsigned comment_num, FLAC__StreamMetadata_VorbisComment_Entry entry, FLAC__bool copy)
  863. {
  864. FLAC__StreamMetadata_VorbisComment *vc;
  865. FLAC__ASSERT(0 != object);
  866. FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
  867. FLAC__ASSERT(comment_num <= object->data.vorbis_comment.num_comments);
  868. if(!FLAC__format_vorbiscomment_entry_is_legal(entry.entry, entry.length))
  869. return false;
  870. vc = &object->data.vorbis_comment;
  871. if(!FLAC__metadata_object_vorbiscomment_resize_comments(object, vc->num_comments+1))
  872. return false;
  873. /* move all comments >= comment_num forward one space */
  874. memmove(&vc->comments[comment_num+1], &vc->comments[comment_num], sizeof(FLAC__StreamMetadata_VorbisComment_Entry)*(vc->num_comments-1-comment_num));
  875. vc->comments[comment_num].length = 0;
  876. vc->comments[comment_num].entry = 0;
  877. return FLAC__metadata_object_vorbiscomment_set_comment(object, comment_num, entry, copy);
  878. }
  879. FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_append_comment(FLAC__StreamMetadata *object, FLAC__StreamMetadata_VorbisComment_Entry entry, FLAC__bool copy)
  880. {
  881. FLAC__ASSERT(0 != object);
  882. FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
  883. return FLAC__metadata_object_vorbiscomment_insert_comment(object, object->data.vorbis_comment.num_comments, entry, copy);
  884. }
  885. FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_replace_comment(FLAC__StreamMetadata *object, FLAC__StreamMetadata_VorbisComment_Entry entry, FLAC__bool all, FLAC__bool copy)
  886. {
  887. FLAC__ASSERT(0 != entry.entry && entry.length > 0);
  888. if(!FLAC__format_vorbiscomment_entry_is_legal(entry.entry, entry.length))
  889. return false;
  890. {
  891. int i;
  892. unsigned field_name_length;
  893. const FLAC__byte *eq = (FLAC__byte*)memchr(entry.entry, '=', entry.length);
  894. FLAC__ASSERT(0 != eq);
  895. if(0 == eq)
  896. return false; /* double protection */
  897. field_name_length = eq-entry.entry;
  898. if((i = vorbiscomment_find_entry_from_(object, 0, entry.entry, field_name_length)) >= 0) {
  899. unsigned index = (unsigned)i;
  900. if(!FLAC__metadata_object_vorbiscomment_set_comment(object, index, entry, copy))
  901. return false;
  902. if(all && (index+1 < object->data.vorbis_comment.num_comments)) {
  903. for(i = vorbiscomment_find_entry_from_(object, index+1, entry.entry, field_name_length); i >= 0; ) {
  904. if(!FLAC__metadata_object_vorbiscomment_delete_comment(object, (unsigned)i))
  905. return false;
  906. if((unsigned)i < object->data.vorbis_comment.num_comments)
  907. i = vorbiscomment_find_entry_from_(object, (unsigned)i, entry.entry, field_name_length);
  908. else
  909. i = -1;
  910. }
  911. }
  912. return true;
  913. }
  914. else
  915. return FLAC__metadata_object_vorbiscomment_append_comment(object, entry, copy);
  916. }
  917. }
  918. FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_delete_comment(FLAC__StreamMetadata *object, unsigned comment_num)
  919. {
  920. FLAC__StreamMetadata_VorbisComment *vc;
  921. FLAC__ASSERT(0 != object);
  922. FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
  923. FLAC__ASSERT(comment_num < object->data.vorbis_comment.num_comments);
  924. vc = &object->data.vorbis_comment;
  925. /* free the comment at comment_num */
  926. if(0 != vc->comments[comment_num].entry)
  927. free(vc->comments[comment_num].entry);
  928. /* move all comments > comment_num backward one space */
  929. memmove(&vc->comments[comment_num], &vc->comments[comment_num+1], sizeof(FLAC__StreamMetadata_VorbisComment_Entry)*(vc->num_comments-comment_num-1));
  930. vc->comments[vc->num_comments-1].length = 0;
  931. vc->comments[vc->num_comments-1].entry = 0;
  932. return FLAC__metadata_object_vorbiscomment_resize_comments(object, vc->num_comments-1);
  933. }
  934. FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_entry_from_name_value_pair(FLAC__StreamMetadata_VorbisComment_Entry *entry, const char *field_name, const char *field_value)
  935. {
  936. FLAC__ASSERT(0 != entry);
  937. FLAC__ASSERT(0 != field_name);
  938. FLAC__ASSERT(0 != field_value);
  939. if(!FLAC__format_vorbiscomment_entry_name_is_legal(field_name))
  940. return false;
  941. if(!FLAC__format_vorbiscomment_entry_value_is_legal(field_value, (unsigned)(-1)))
  942. return false;
  943. {
  944. const size_t nn = strlen(field_name);
  945. const size_t nv = strlen(field_value);
  946. entry->length = nn + 1 /*=*/ + nv;
  947. if(0 == (entry->entry = (FLAC__byte*)malloc(entry->length+1)))
  948. return false;
  949. memcpy(entry->entry, field_name, nn);
  950. entry->entry[nn] = '=';
  951. memcpy(entry->entry+nn+1, field_value, nv);
  952. entry->entry[entry->length] = '';
  953. }
  954. return true;
  955. }
  956. FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_entry_to_name_value_pair(const FLAC__StreamMetadata_VorbisComment_Entry entry, char **field_name, char **field_value)
  957. {
  958. FLAC__ASSERT(0 != entry.entry && entry.length > 0);
  959. FLAC__ASSERT(0 != field_name);
  960. FLAC__ASSERT(0 != field_value);
  961. if(!FLAC__format_vorbiscomment_entry_is_legal(entry.entry, entry.length))
  962. return false;
  963. {
  964. const FLAC__byte *eq = (FLAC__byte*)memchr(entry.entry, '=', entry.length);
  965. const size_t nn = eq-entry.entry;
  966. const size_t nv = entry.length-nn-1; /* -1 for the '=' */
  967. FLAC__ASSERT(0 != eq);
  968. if(0 == eq)
  969. return false; /* double protection */
  970. if(0 == (*field_name = (char*)malloc(nn+1)))
  971. return false;
  972. if(0 == (*field_value = (char*)malloc(nv+1))) {
  973. free(*field_name);
  974. return false;
  975. }
  976. memcpy(*field_name, entry.entry, nn);
  977. memcpy(*field_value, entry.entry+nn+1, nv);
  978. (*field_name)[nn] = '';
  979. (*field_value)[nv] = '';
  980. }
  981. return true;
  982. }
  983. FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_entry_matches(const FLAC__StreamMetadata_VorbisComment_Entry entry, const char *field_name, unsigned field_name_length)
  984. {
  985. FLAC__ASSERT(0 != entry.entry && entry.length > 0);
  986. {
  987. const FLAC__byte *eq = (FLAC__byte*)memchr(entry.entry, '=', entry.length);
  988. #if defined _MSC_VER || defined __MINGW32__
  989. #define FLAC__STRNCASECMP _strnicmp
  990. #elif defined __palmos__
  991. #define FLAC__STRNCASECMP tcsnicmp
  992. #else
  993. #define FLAC__STRNCASECMP strncasecmp
  994. #endif
  995. return (0 != eq && (unsigned)(eq-entry.entry) == field_name_length && 0 == FLAC__STRNCASECMP(field_name, (const char *)entry.entry, field_name_length));
  996. #undef FLAC__STRNCASECMP
  997. }
  998. }
  999. FLAC_API int FLAC__metadata_object_vorbiscomment_find_entry_from(const FLAC__StreamMetadata *object, unsigned offset, const char *field_name)
  1000. {
  1001. FLAC__ASSERT(0 != field_name);
  1002. return vorbiscomment_find_entry_from_(object, offset, field_name, strlen(field_name));
  1003. }
  1004. FLAC_API int FLAC__metadata_object_vorbiscomment_remove_entry_matching(FLAC__StreamMetadata *object, const char *field_name)
  1005. {
  1006. const unsigned field_name_length = strlen(field_name);
  1007. unsigned i;
  1008. FLAC__ASSERT(0 != object);
  1009. FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
  1010. for(i = 0; i < object->data.vorbis_comment.num_comments; i++) {
  1011. if(FLAC__metadata_object_vorbiscomment_entry_matches(object->data.vorbis_comment.comments[i], field_name, field_name_length)) {
  1012. if(!FLAC__metadata_object_vorbiscomment_delete_comment(object, i))
  1013. return -1;
  1014. else
  1015. return 1;
  1016. }
  1017. }
  1018. return 0;
  1019. }
  1020. FLAC_API int FLAC__metadata_object_vorbiscomment_remove_entries_matching(FLAC__StreamMetadata *object, const char *field_name)
  1021. {
  1022. FLAC__bool ok = true;
  1023. unsigned matching = 0;
  1024. const unsigned field_name_length = strlen(field_name);
  1025. int i;
  1026. FLAC__ASSERT(0 != object);
  1027. FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
  1028. /* must delete from end to start otherwise it will interfere with our iteration */
  1029. for(i = (int)object->data.vorbis_comment.num_comments - 1; ok && i >= 0; i--) {
  1030. if(FLAC__metadata_object_vorbiscomment_entry_matches(object->data.vorbis_comment.comments[i], field_name, field_name_length)) {
  1031. matching++;
  1032. ok &= FLAC__metadata_object_vorbiscomment_delete_comment(object, (unsigned)i);
  1033. }
  1034. }
  1035. return ok? (int)matching : -1;
  1036. }
  1037. FLAC_API FLAC__StreamMetadata_CueSheet_Track *FLAC__metadata_object_cuesheet_track_new()
  1038. {
  1039. return (FLAC__StreamMetadata_CueSheet_Track*)calloc(1, sizeof(FLAC__StreamMetadata_CueSheet_Track));
  1040. }
  1041. FLAC_API FLAC__StreamMetadata_CueSheet_Track *FLAC__metadata_object_cuesheet_track_clone(const FLAC__StreamMetadata_CueSheet_Track *object)
  1042. {
  1043. FLAC__StreamMetadata_CueSheet_Track *to;
  1044. FLAC__ASSERT(0 != object);
  1045. if(0 != (to = FLAC__metadata_object_cuesheet_track_new())) {
  1046. if(!copy_track_(to, object)) {
  1047. FLAC__metadata_object_cuesheet_track_delete(to);
  1048. return 0;
  1049. }
  1050. }
  1051. return to;
  1052. }
  1053. void FLAC__metadata_object_cuesheet_track_delete_data(FLAC__StreamMetadata_CueSheet_Track *object)
  1054. {
  1055. FLAC__ASSERT(0 != object);
  1056. if(0 != object->indices) {
  1057. FLAC__ASSERT(object->num_indices > 0);
  1058. free(object->indices);
  1059. }
  1060. }
  1061. FLAC_API void FLAC__metadata_object_cuesheet_track_delete(FLAC__StreamMetadata_CueSheet_Track *object)
  1062. {
  1063. FLAC__metadata_object_cuesheet_track_delete_data(object);
  1064. free(object);
  1065. }
  1066. FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_track_resize_indices(FLAC__StreamMetadata *object, unsigned track_num, unsigned new_num_indices)
  1067. {
  1068. FLAC__StreamMetadata_CueSheet_Track *track;
  1069. FLAC__ASSERT(0 != object);
  1070. FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_CUESHEET);
  1071. FLAC__ASSERT(track_num < object->data.cue_sheet.num_tracks);
  1072. track = &object->data.cue_sheet.tracks[track_num];
  1073. if(0 == track->indices) {
  1074. FLAC__ASSERT(track->num_indices == 0);
  1075. if(0 == new_num_indices)
  1076. return true;
  1077. else if(0 == (track->indices = cuesheet_track_index_array_new_(new_num_indices)))
  1078. return false;
  1079. }
  1080. else {
  1081. const unsigned old_size = track->num_indices * sizeof(FLAC__StreamMetadata_CueSheet_Index);
  1082. const unsigned new_size = new_num_indices * sizeof(FLAC__StreamMetadata_CueSheet_Index);
  1083. FLAC__ASSERT(track->num_indices > 0);
  1084. if(new_size == 0) {
  1085. free(track->indices);
  1086. track->indices = 0;
  1087. }
  1088. else if(0 == (track->indices = (FLAC__StreamMetadata_CueSheet_Index*)realloc(track->indices, new_size)))
  1089. return false;
  1090. /* if growing, zero all the lengths/pointers of new elements */
  1091. if(new_size > old_size)
  1092. memset(track->indices + track->num_indices, 0, new_size - old_size);
  1093. }
  1094. track->num_indices = new_num_indices;
  1095. cuesheet_calculate_length_(object);
  1096. return true;
  1097. }
  1098. FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_track_insert_index(FLAC__StreamMetadata *object, unsigned track_num, unsigned index_num, FLAC__StreamMetadata_CueSheet_Index index)
  1099. {
  1100. FLAC__StreamMetadata_CueSheet_Track *track;
  1101. FLAC__ASSERT(0 != object);
  1102. FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_CUESHEET);
  1103. FLAC__ASSERT(track_num < object->data.cue_sheet.num_tracks);
  1104. FLAC__ASSERT(index_num <= object->data.cue_sheet.tracks[track_num].num_indices);
  1105. track = &object->data.cue_sheet.tracks[track_num];
  1106. if(!FLAC__metadata_object_cuesheet_track_resize_indices(object, track_num, track->num_indices+1))
  1107. return false;
  1108. /* move all indices >= index_num forward one space */
  1109. memmove(&track->indices[index_num+1], &track->indices[index_num], sizeof(FLAC__StreamMetadata_CueSheet_Index)*(track->num_indices-1-index_num));
  1110. track->indices[index_num] = index;
  1111. cuesheet_calculate_length_(object);
  1112. return true;
  1113. }
  1114. FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_track_insert_blank_index(FLAC__StreamMetadata *object, unsigned track_num, unsigned index_num)
  1115. {
  1116. FLAC__StreamMetadata_CueSheet_Index index;
  1117. memset(&index, 0, sizeof(index));
  1118. return FLAC__metadata_object_cuesheet_track_insert_index(object, track_num, index_num, index);
  1119. }
  1120. FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_track_delete_index(FLAC__StreamMetadata *object, unsigned track_num, unsigned index_num)
  1121. {
  1122. FLAC__StreamMetadata_CueSheet_Track *track;
  1123. FLAC__ASSERT(0 != object);
  1124. FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_CUESHEET);
  1125. FLAC__ASSERT(track_num < object->data.cue_sheet.num_tracks);
  1126. FLAC__ASSERT(index_num < object->data.cue_sheet.tracks[track_num].num_indices);
  1127. track = &object->data.cue_sheet.tracks[track_num];
  1128. /* move all indices > index_num backward one space */
  1129. memmove(&track->indices[index_num], &track->indices[index_num+1], sizeof(FLAC__StreamMetadata_CueSheet_Index)*(track->num_indices-index_num-1));
  1130. FLAC__metadata_object_cuesheet_track_resize_indices(object, track_num, track->num_indices-1);
  1131. cuesheet_calculate_length_(object);
  1132. return true;
  1133. }
  1134. FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_resize_tracks(FLAC__StreamMetadata *object, unsigned new_num_tracks)
  1135. {
  1136. FLAC__ASSERT(0 != object);
  1137. FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_CUESHEET);
  1138. if(0 == object->data.cue_sheet.tracks) {
  1139. FLAC__ASSERT(object->data.cue_sheet.num_tracks == 0);
  1140. if(0 == new_num_tracks)
  1141. return true;
  1142. else if(0 == (object->data.cue_sheet.tracks = cuesheet_track_array_new_(new_num_tracks)))
  1143. return false;
  1144. }
  1145. else {
  1146. const unsigned old_size = object->data.cue_sheet.num_tracks * sizeof(FLAC__StreamMetadata_CueSheet_Track);
  1147. const unsigned new_size = new_num_tracks * sizeof(FLAC__StreamMetadata_CueSheet_Track);
  1148. FLAC__ASSERT(object->data.cue_sheet.num_tracks > 0);
  1149. /* if shrinking, free the truncated entries */
  1150. if(new_num_tracks < object->data.cue_sheet.num_tracks) {
  1151. unsigned i;
  1152. for(i = new_num_tracks; i < object->data.cue_sheet.num_tracks; i++)
  1153. if(0 != object->data.cue_sheet.tracks[i].indices)
  1154. free(object->data.cue_sheet.tracks[i].indices);
  1155. }
  1156. if(new_size == 0) {
  1157. free(object->data.cue_sheet.tracks);
  1158. object->data.cue_sheet.tracks = 0;
  1159. }
  1160. else if(0 == (object->data.cue_sheet.tracks = (FLAC__StreamMetadata_CueSheet_Track*)realloc(object->data.cue_sheet.tracks, new_size)))
  1161. return false;
  1162. /* if growing, zero all the lengths/pointers of new elements */
  1163. if(new_size > old_size)
  1164. memset(object->data.cue_sheet.tracks + object->data.cue_sheet.num_tracks, 0, new_size - old_size);
  1165. }
  1166. object->data.cue_sheet.num_tracks = new_num_tracks;
  1167. cuesheet_calculate_length_(object);
  1168. return true;
  1169. }
  1170. FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_set_track(FLAC__StreamMetadata *object, unsigned track_num, FLAC__StreamMetadata_CueSheet_Track *track, FLAC__bool copy)
  1171. {
  1172. FLAC__ASSERT(0 != object);
  1173. FLAC__ASSERT(track_num < object->data.cue_sheet.num_tracks);
  1174. return cuesheet_set_track_(object, object->data.cue_sheet.tracks + track_num, track, copy);
  1175. }
  1176. FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_insert_track(FLAC__StreamMetadata *object, unsigned track_num, FLAC__StreamMetadata_CueSheet_Track *track, FLAC__bool copy)
  1177. {
  1178. FLAC__StreamMetadata_CueSheet *cs;
  1179. FLAC__ASSERT(0 != object);
  1180. FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_CUESHEET);
  1181. FLAC__ASSERT(track_num <= object->data.cue_sheet.num_tracks);
  1182. cs = &object->data.cue_sheet;
  1183. if(!FLAC__metadata_object_cuesheet_resize_tracks(object, cs->num_tracks+1))
  1184. return false;
  1185. /* move all tracks >= track_num forward one space */
  1186. memmove(&cs->tracks[track_num+1], &cs->tracks[track_num], sizeof(FLAC__StreamMetadata_CueSheet_Track)*(cs->num_tracks-1-track_num));
  1187. cs->tracks[track_num].num_indices = 0;
  1188. cs->tracks[track_num].indices = 0;
  1189. return FLAC__metadata_object_cuesheet_set_track(object, track_num, track, copy);
  1190. }
  1191. FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_insert_blank_track(FLAC__StreamMetadata *object, unsigned track_num)
  1192. {
  1193. FLAC__StreamMetadata_CueSheet_Track track;
  1194. memset(&track, 0, sizeof(track));
  1195. return FLAC__metadata_object_cuesheet_insert_track(object, track_num, &track, /*copy=*/false);
  1196. }
  1197. FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_delete_track(FLAC__StreamMetadata *object, unsigned track_num)
  1198. {
  1199. FLAC__StreamMetadata_CueSheet *cs;
  1200. FLAC__ASSERT(0 != object);
  1201. FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_CUESHEET);
  1202. FLAC__ASSERT(track_num < object->data.cue_sheet.num_tracks);
  1203. cs = &object->data.cue_sheet;
  1204. /* free the track at track_num */
  1205. if(0 != cs->tracks[track_num].indices)
  1206. free(cs->tracks[track_num].indices);
  1207. /* move all tracks > track_num backward one space */
  1208. memmove(&cs->tracks[track_num], &cs->tracks[track_num+1], sizeof(FLAC__StreamMetadata_CueSheet_Track)*(cs->num_tracks-track_num-1));
  1209. cs->tracks[cs->num_tracks-1].num_indices = 0;
  1210. cs->tracks[cs->num_tracks-1].indices = 0;
  1211. return FLAC__metadata_object_cuesheet_resize_tracks(object, cs->num_tracks-1);
  1212. }
  1213. FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_is_legal(const FLAC__StreamMetadata *object, FLAC__bool check_cd_da_subset, const char **violation)
  1214. {
  1215. FLAC__ASSERT(0 != object);
  1216. FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_CUESHEET);
  1217. return FLAC__format_cuesheet_is_legal(&object->data.cue_sheet, check_cd_da_subset, violation);
  1218. }