flac_format.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  1. /* libFLAC - Free Lossless Audio Codec library
  2. * Copyright (C) 2000,2001,2002,2003,2004,2005,2006,2007 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. #if HAVE_CONFIG_H
  32. # include <config.h>
  33. #endif
  34. #include <stdio.h>
  35. #include <stdlib.h> /* for qsort() */
  36. #include <string.h> /* for memset() */
  37. #include "flac_FLAC_assert.h"
  38. #include "flac_FLAC_format.h"
  39. #include "flac_private_format.h"
  40. #ifndef FLaC__INLINE
  41. #define FLaC__INLINE
  42. #endif
  43. #ifdef min
  44. #undef min
  45. #endif
  46. #define min(a,b) ((a)<(b)?(a):(b))
  47. /* adjust for compilers that can't understand using LLU suffix for uint64_t literals */
  48. #ifdef _MSC_VER
  49. #define FLAC__U64L(x) x
  50. #else
  51. #define FLAC__U64L(x) x##LLU
  52. #endif
  53. /* VERSION should come from configure */
  54. FLAC_API const char *FLAC__VERSION_STRING = "1.2.1"; // ppgb 20071120
  55. FLAC_API const char *FLAC__VENDOR_STRING = "reference libFLAC 1.2.1 20070917"; // ppgb 20071120
  56. FLAC_API const FLAC__byte FLAC__STREAM_SYNC_STRING[4] = { 'f','L','a','C' };
  57. FLAC_API const unsigned FLAC__STREAM_SYNC = 0x664C6143;
  58. FLAC_API const unsigned FLAC__STREAM_SYNC_LEN = 32; /* bits */
  59. FLAC_API const unsigned FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN = 16; /* bits */
  60. FLAC_API const unsigned FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN = 16; /* bits */
  61. FLAC_API const unsigned FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN = 24; /* bits */
  62. FLAC_API const unsigned FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN = 24; /* bits */
  63. FLAC_API const unsigned FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN = 20; /* bits */
  64. FLAC_API const unsigned FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN = 3; /* bits */
  65. FLAC_API const unsigned FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN = 5; /* bits */
  66. FLAC_API const unsigned FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN = 36; /* bits */
  67. FLAC_API const unsigned FLAC__STREAM_METADATA_STREAMINFO_MD5SUM_LEN = 128; /* bits */
  68. FLAC_API const unsigned FLAC__STREAM_METADATA_APPLICATION_ID_LEN = 32; /* bits */
  69. FLAC_API const unsigned FLAC__STREAM_METADATA_SEEKPOINT_SAMPLE_NUMBER_LEN = 64; /* bits */
  70. FLAC_API const unsigned FLAC__STREAM_METADATA_SEEKPOINT_STREAM_OFFSET_LEN = 64; /* bits */
  71. FLAC_API const unsigned FLAC__STREAM_METADATA_SEEKPOINT_FRAME_SAMPLES_LEN = 16; /* bits */
  72. FLAC_API const FLAC__uint64 FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER = FLAC__U64L(0xffffffffffffffff);
  73. FLAC_API const unsigned FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN = 32; /* bits */
  74. FLAC_API const unsigned FLAC__STREAM_METADATA_VORBIS_COMMENT_NUM_COMMENTS_LEN = 32; /* bits */
  75. FLAC_API const unsigned FLAC__STREAM_METADATA_CUESHEET_INDEX_OFFSET_LEN = 64; /* bits */
  76. FLAC_API const unsigned FLAC__STREAM_METADATA_CUESHEET_INDEX_NUMBER_LEN = 8; /* bits */
  77. FLAC_API const unsigned FLAC__STREAM_METADATA_CUESHEET_INDEX_RESERVED_LEN = 3*8; /* bits */
  78. FLAC_API const unsigned FLAC__STREAM_METADATA_CUESHEET_TRACK_OFFSET_LEN = 64; /* bits */
  79. FLAC_API const unsigned FLAC__STREAM_METADATA_CUESHEET_TRACK_NUMBER_LEN = 8; /* bits */
  80. FLAC_API const unsigned FLAC__STREAM_METADATA_CUESHEET_TRACK_ISRC_LEN = 12*8; /* bits */
  81. FLAC_API const unsigned FLAC__STREAM_METADATA_CUESHEET_TRACK_TYPE_LEN = 1; /* bit */
  82. FLAC_API const unsigned FLAC__STREAM_METADATA_CUESHEET_TRACK_PRE_EMPHASIS_LEN = 1; /* bit */
  83. FLAC_API const unsigned FLAC__STREAM_METADATA_CUESHEET_TRACK_RESERVED_LEN = 6+13*8; /* bits */
  84. FLAC_API const unsigned FLAC__STREAM_METADATA_CUESHEET_TRACK_NUM_INDICES_LEN = 8; /* bits */
  85. FLAC_API const unsigned FLAC__STREAM_METADATA_CUESHEET_MEDIA_CATALOG_NUMBER_LEN = 128*8; /* bits */
  86. FLAC_API const unsigned FLAC__STREAM_METADATA_CUESHEET_LEAD_IN_LEN = 64; /* bits */
  87. FLAC_API const unsigned FLAC__STREAM_METADATA_CUESHEET_IS_CD_LEN = 1; /* bit */
  88. FLAC_API const unsigned FLAC__STREAM_METADATA_CUESHEET_RESERVED_LEN = 7+258*8; /* bits */
  89. FLAC_API const unsigned FLAC__STREAM_METADATA_CUESHEET_NUM_TRACKS_LEN = 8; /* bits */
  90. FLAC_API const unsigned FLAC__STREAM_METADATA_PICTURE_TYPE_LEN = 32; /* bits */
  91. FLAC_API const unsigned FLAC__STREAM_METADATA_PICTURE_MIME_TYPE_LENGTH_LEN = 32; /* bits */
  92. FLAC_API const unsigned FLAC__STREAM_METADATA_PICTURE_DESCRIPTION_LENGTH_LEN = 32; /* bits */
  93. FLAC_API const unsigned FLAC__STREAM_METADATA_PICTURE_WIDTH_LEN = 32; /* bits */
  94. FLAC_API const unsigned FLAC__STREAM_METADATA_PICTURE_HEIGHT_LEN = 32; /* bits */
  95. FLAC_API const unsigned FLAC__STREAM_METADATA_PICTURE_DEPTH_LEN = 32; /* bits */
  96. FLAC_API const unsigned FLAC__STREAM_METADATA_PICTURE_COLORS_LEN = 32; /* bits */
  97. FLAC_API const unsigned FLAC__STREAM_METADATA_PICTURE_DATA_LENGTH_LEN = 32; /* bits */
  98. FLAC_API const unsigned FLAC__STREAM_METADATA_IS_LAST_LEN = 1; /* bits */
  99. FLAC_API const unsigned FLAC__STREAM_METADATA_TYPE_LEN = 7; /* bits */
  100. FLAC_API const unsigned FLAC__STREAM_METADATA_LENGTH_LEN = 24; /* bits */
  101. FLAC_API const unsigned FLAC__FRAME_HEADER_SYNC = 0x3ffe;
  102. FLAC_API const unsigned FLAC__FRAME_HEADER_SYNC_LEN = 14; /* bits */
  103. FLAC_API const unsigned FLAC__FRAME_HEADER_RESERVED_LEN = 1; /* bits */
  104. FLAC_API const unsigned FLAC__FRAME_HEADER_BLOCKING_STRATEGY_LEN = 1; /* bits */
  105. FLAC_API const unsigned FLAC__FRAME_HEADER_BLOCK_SIZE_LEN = 4; /* bits */
  106. FLAC_API const unsigned FLAC__FRAME_HEADER_SAMPLE_RATE_LEN = 4; /* bits */
  107. FLAC_API const unsigned FLAC__FRAME_HEADER_CHANNEL_ASSIGNMENT_LEN = 4; /* bits */
  108. FLAC_API const unsigned FLAC__FRAME_HEADER_BITS_PER_SAMPLE_LEN = 3; /* bits */
  109. FLAC_API const unsigned FLAC__FRAME_HEADER_ZERO_PAD_LEN = 1; /* bits */
  110. FLAC_API const unsigned FLAC__FRAME_HEADER_CRC_LEN = 8; /* bits */
  111. FLAC_API const unsigned FLAC__FRAME_FOOTER_CRC_LEN = 16; /* bits */
  112. FLAC_API const unsigned FLAC__ENTROPY_CODING_METHOD_TYPE_LEN = 2; /* bits */
  113. FLAC_API const unsigned FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN = 4; /* bits */
  114. FLAC_API const unsigned FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN = 4; /* bits */
  115. FLAC_API const unsigned FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2_PARAMETER_LEN = 5; /* bits */
  116. FLAC_API const unsigned FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_RAW_LEN = 5; /* bits */
  117. FLAC_API const unsigned FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER = 15; /* == (1<<FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN)-1 */
  118. FLAC_API const unsigned FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2_ESCAPE_PARAMETER = 31; /* == (1<<FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2_PARAMETER_LEN)-1 */
  119. FLAC_API const char * const FLAC__EntropyCodingMethodTypeString[] = {
  120. "PARTITIONED_RICE",
  121. "PARTITIONED_RICE2"
  122. };
  123. FLAC_API const unsigned FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN = 4; /* bits */
  124. FLAC_API const unsigned FLAC__SUBFRAME_LPC_QLP_SHIFT_LEN = 5; /* bits */
  125. FLAC_API const unsigned FLAC__SUBFRAME_ZERO_PAD_LEN = 1; /* bits */
  126. FLAC_API const unsigned FLAC__SUBFRAME_TYPE_LEN = 6; /* bits */
  127. FLAC_API const unsigned FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN = 1; /* bits */
  128. FLAC_API const unsigned FLAC__SUBFRAME_TYPE_CONSTANT_BYTE_ALIGNED_MASK = 0x00;
  129. FLAC_API const unsigned FLAC__SUBFRAME_TYPE_VERBATIM_BYTE_ALIGNED_MASK = 0x02;
  130. FLAC_API const unsigned FLAC__SUBFRAME_TYPE_FIXED_BYTE_ALIGNED_MASK = 0x10;
  131. FLAC_API const unsigned FLAC__SUBFRAME_TYPE_LPC_BYTE_ALIGNED_MASK = 0x40;
  132. FLAC_API const char * const FLAC__SubframeTypeString[] = {
  133. "CONSTANT",
  134. "VERBATIM",
  135. "FIXED",
  136. "LPC"
  137. };
  138. FLAC_API const char * const FLAC__ChannelAssignmentString[] = {
  139. "INDEPENDENT",
  140. "LEFT_SIDE",
  141. "RIGHT_SIDE",
  142. "MID_SIDE"
  143. };
  144. FLAC_API const char * const FLAC__FrameNumberTypeString[] = {
  145. "FRAME_NUMBER_TYPE_FRAME_NUMBER",
  146. "FRAME_NUMBER_TYPE_SAMPLE_NUMBER"
  147. };
  148. FLAC_API const char * const FLAC__MetadataTypeString[] = {
  149. "STREAMINFO",
  150. "PADDING",
  151. "APPLICATION",
  152. "SEEKTABLE",
  153. "VORBIS_COMMENT",
  154. "CUESHEET",
  155. "PICTURE"
  156. };
  157. FLAC_API const char * const FLAC__StreamMetadata_Picture_TypeString[] = {
  158. "Other",
  159. "32x32 pixels 'file icon' (PNG only)",
  160. "Other file icon",
  161. "Cover (front)",
  162. "Cover (back)",
  163. "Leaflet page",
  164. "Media (e.g. label side of CD)",
  165. "Lead artist/lead performer/soloist",
  166. "Artist/performer",
  167. "Conductor",
  168. "Band/Orchestra",
  169. "Composer",
  170. "Lyricist/text writer",
  171. "Recording Location",
  172. "During recording",
  173. "During performance",
  174. "Movie/video screen capture",
  175. "A bright coloured fish",
  176. "Illustration",
  177. "Band/artist logotype",
  178. "Publisher/Studio logotype"
  179. };
  180. FLAC_API FLAC__bool FLAC__format_sample_rate_is_valid(unsigned sample_rate)
  181. {
  182. if(sample_rate == 0 || sample_rate > FLAC__MAX_SAMPLE_RATE) {
  183. return false;
  184. }
  185. else
  186. return true;
  187. }
  188. FLAC_API FLAC__bool FLAC__format_sample_rate_is_subset(unsigned sample_rate)
  189. {
  190. if(
  191. !FLAC__format_sample_rate_is_valid(sample_rate) ||
  192. (
  193. sample_rate >= (1u << 16) &&
  194. !(sample_rate % 1000 == 0 || sample_rate % 10 == 0)
  195. )
  196. ) {
  197. return false;
  198. }
  199. else
  200. return true;
  201. }
  202. /* @@@@ add to unit tests; it is already indirectly tested by the metadata_object tests */
  203. FLAC_API FLAC__bool FLAC__format_seektable_is_legal(const FLAC__StreamMetadata_SeekTable *seek_table)
  204. {
  205. unsigned i;
  206. FLAC__uint64 prev_sample_number = 0;
  207. FLAC__bool got_prev = false;
  208. FLAC__ASSERT(0 != seek_table);
  209. for(i = 0; i < seek_table->num_points; i++) {
  210. if(got_prev) {
  211. if(
  212. seek_table->points[i].sample_number != FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER &&
  213. seek_table->points[i].sample_number <= prev_sample_number
  214. )
  215. return false;
  216. }
  217. prev_sample_number = seek_table->points[i].sample_number;
  218. got_prev = true;
  219. }
  220. return true;
  221. }
  222. /* used as the sort predicate for qsort() */
  223. static int seekpoint_compare_(const FLAC__StreamMetadata_SeekPoint *l, const FLAC__StreamMetadata_SeekPoint *r)
  224. {
  225. /* we don't just 'return l->sample_number - r->sample_number' since the result (FLAC__int64) might overflow an 'int' */
  226. if(l->sample_number == r->sample_number)
  227. return 0;
  228. else if(l->sample_number < r->sample_number)
  229. return -1;
  230. else
  231. return 1;
  232. }
  233. /* @@@@ add to unit tests; it is already indirectly tested by the metadata_object tests */
  234. FLAC_API unsigned FLAC__format_seektable_sort(FLAC__StreamMetadata_SeekTable *seek_table)
  235. {
  236. unsigned i, j;
  237. FLAC__bool first;
  238. FLAC__ASSERT(0 != seek_table);
  239. /* sort the seekpoints */
  240. qsort(seek_table->points, seek_table->num_points, sizeof(FLAC__StreamMetadata_SeekPoint), (int (*)(const void *, const void *))seekpoint_compare_);
  241. /* uniquify the seekpoints */
  242. first = true;
  243. for(i = j = 0; i < seek_table->num_points; i++) {
  244. if(seek_table->points[i].sample_number != FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER) {
  245. if(!first) {
  246. if(seek_table->points[i].sample_number == seek_table->points[j-1].sample_number)
  247. continue;
  248. }
  249. }
  250. first = false;
  251. seek_table->points[j++] = seek_table->points[i];
  252. }
  253. for(i = j; i < seek_table->num_points; i++) {
  254. seek_table->points[i].sample_number = FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER;
  255. seek_table->points[i].stream_offset = 0;
  256. seek_table->points[i].frame_samples = 0;
  257. }
  258. return j;
  259. }
  260. /*
  261. * also disallows non-shortest-form encodings, c.f.
  262. * http://www.unicode.org/versions/corrigendum1.html
  263. * and a more clear explanation at the end of this section:
  264. * http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
  265. */
  266. static FLaC__INLINE unsigned utf8len_(const FLAC__byte *utf8)
  267. {
  268. FLAC__ASSERT(0 != utf8);
  269. if ((utf8[0] & 0x80) == 0) {
  270. return 1;
  271. }
  272. else if ((utf8[0] & 0xE0) == 0xC0 && (utf8[1] & 0xC0) == 0x80) {
  273. if ((utf8[0] & 0xFE) == 0xC0) /* overlong sequence check */
  274. return 0;
  275. return 2;
  276. }
  277. else if ((utf8[0] & 0xF0) == 0xE0 && (utf8[1] & 0xC0) == 0x80 && (utf8[2] & 0xC0) == 0x80) {
  278. if (utf8[0] == 0xE0 && (utf8[1] & 0xE0) == 0x80) /* overlong sequence check */
  279. return 0;
  280. /* illegal surrogates check (U+D800...U+DFFF and U+FFFE...U+FFFF) */
  281. if (utf8[0] == 0xED && (utf8[1] & 0xE0) == 0xA0) /* D800-DFFF */
  282. return 0;
  283. if (utf8[0] == 0xEF && utf8[1] == 0xBF && (utf8[2] & 0xFE) == 0xBE) /* FFFE-FFFF */
  284. return 0;
  285. return 3;
  286. }
  287. else if ((utf8[0] & 0xF8) == 0xF0 && (utf8[1] & 0xC0) == 0x80 && (utf8[2] & 0xC0) == 0x80 && (utf8[3] & 0xC0) == 0x80) {
  288. if (utf8[0] == 0xF0 && (utf8[1] & 0xF0) == 0x80) /* overlong sequence check */
  289. return 0;
  290. return 4;
  291. }
  292. else if ((utf8[0] & 0xFC) == 0xF8 && (utf8[1] & 0xC0) == 0x80 && (utf8[2] & 0xC0) == 0x80 && (utf8[3] & 0xC0) == 0x80 && (utf8[4] & 0xC0) == 0x80) {
  293. if (utf8[0] == 0xF8 && (utf8[1] & 0xF8) == 0x80) /* overlong sequence check */
  294. return 0;
  295. return 5;
  296. }
  297. else if ((utf8[0] & 0xFE) == 0xFC && (utf8[1] & 0xC0) == 0x80 && (utf8[2] & 0xC0) == 0x80 && (utf8[3] & 0xC0) == 0x80 && (utf8[4] & 0xC0) == 0x80 && (utf8[5] & 0xC0) == 0x80) {
  298. if (utf8[0] == 0xFC && (utf8[1] & 0xFC) == 0x80) /* overlong sequence check */
  299. return 0;
  300. return 6;
  301. }
  302. else {
  303. return 0;
  304. }
  305. }
  306. FLAC_API FLAC__bool FLAC__format_vorbiscomment_entry_name_is_legal(const char *name)
  307. {
  308. char c;
  309. for(c = *name; c; c = *(++name))
  310. if(c < 0x20 || c == 0x3d || c > 0x7d)
  311. return false;
  312. return true;
  313. }
  314. FLAC_API FLAC__bool FLAC__format_vorbiscomment_entry_value_is_legal(const FLAC__byte *value, unsigned length)
  315. {
  316. if(length == (unsigned)(-1)) {
  317. while(*value) {
  318. unsigned n = utf8len_(value);
  319. if(n == 0)
  320. return false;
  321. value += n;
  322. }
  323. }
  324. else {
  325. const FLAC__byte *end = value + length;
  326. while(value < end) {
  327. unsigned n = utf8len_(value);
  328. if(n == 0)
  329. return false;
  330. value += n;
  331. }
  332. if(value != end)
  333. return false;
  334. }
  335. return true;
  336. }
  337. FLAC_API FLAC__bool FLAC__format_vorbiscomment_entry_is_legal(const FLAC__byte *entry, unsigned length)
  338. {
  339. const FLAC__byte *s, *end;
  340. for(s = entry, end = s + length; s < end && *s != '='; s++) {
  341. if(*s < 0x20 || *s > 0x7D)
  342. return false;
  343. }
  344. if(s == end)
  345. return false;
  346. s++; /* skip '=' */
  347. while(s < end) {
  348. unsigned n = utf8len_(s);
  349. if(n == 0)
  350. return false;
  351. s += n;
  352. }
  353. if(s != end)
  354. return false;
  355. return true;
  356. }
  357. /* @@@@ add to unit tests; it is already indirectly tested by the metadata_object tests */
  358. FLAC_API FLAC__bool FLAC__format_cuesheet_is_legal(const FLAC__StreamMetadata_CueSheet *cue_sheet, FLAC__bool check_cd_da_subset, const char **violation)
  359. {
  360. unsigned i, j;
  361. if(check_cd_da_subset) {
  362. if(cue_sheet->lead_in < 2 * 44100) {
  363. if(violation) *violation = "CD-DA cue sheet must have a lead-in length of at least 2 seconds";
  364. return false;
  365. }
  366. if(cue_sheet->lead_in % 588 != 0) {
  367. if(violation) *violation = "CD-DA cue sheet lead-in length must be evenly divisible by 588 samples";
  368. return false;
  369. }
  370. }
  371. if(cue_sheet->num_tracks == 0) {
  372. if(violation) *violation = "cue sheet must have at least one track (the lead-out)";
  373. return false;
  374. }
  375. if(check_cd_da_subset && cue_sheet->tracks[cue_sheet->num_tracks-1].number != 170) {
  376. if(violation) *violation = "CD-DA cue sheet must have a lead-out track number 170 (0xAA)";
  377. return false;
  378. }
  379. for(i = 0; i < cue_sheet->num_tracks; i++) {
  380. if(cue_sheet->tracks[i].number == 0) {
  381. if(violation) *violation = "cue sheet may not have a track number 0";
  382. return false;
  383. }
  384. if(check_cd_da_subset) {
  385. if(!((cue_sheet->tracks[i].number >= 1 && cue_sheet->tracks[i].number <= 99) || cue_sheet->tracks[i].number == 170)) {
  386. if(violation) *violation = "CD-DA cue sheet track number must be 1-99 or 170";
  387. return false;
  388. }
  389. }
  390. if(check_cd_da_subset && cue_sheet->tracks[i].offset % 588 != 0) {
  391. if(violation) {
  392. if(i == cue_sheet->num_tracks-1) /* the lead-out track... */
  393. *violation = "CD-DA cue sheet lead-out offset must be evenly divisible by 588 samples";
  394. else
  395. *violation = "CD-DA cue sheet track offset must be evenly divisible by 588 samples";
  396. }
  397. return false;
  398. }
  399. if(i < cue_sheet->num_tracks - 1) {
  400. if(cue_sheet->tracks[i].num_indices == 0) {
  401. if(violation) *violation = "cue sheet track must have at least one index point";
  402. return false;
  403. }
  404. if(cue_sheet->tracks[i].indices[0].number > 1) {
  405. if(violation) *violation = "cue sheet track's first index number must be 0 or 1";
  406. return false;
  407. }
  408. }
  409. for(j = 0; j < cue_sheet->tracks[i].num_indices; j++) {
  410. if(check_cd_da_subset && cue_sheet->tracks[i].indices[j].offset % 588 != 0) {
  411. if(violation) *violation = "CD-DA cue sheet track index offset must be evenly divisible by 588 samples";
  412. return false;
  413. }
  414. if(j > 0) {
  415. if(cue_sheet->tracks[i].indices[j].number != cue_sheet->tracks[i].indices[j-1].number + 1) {
  416. if(violation) *violation = "cue sheet track index numbers must increase by 1";
  417. return false;
  418. }
  419. }
  420. }
  421. }
  422. return true;
  423. }
  424. /* @@@@ add to unit tests; it is already indirectly tested by the metadata_object tests */
  425. FLAC_API FLAC__bool FLAC__format_picture_is_legal(const FLAC__StreamMetadata_Picture *picture, const char **violation)
  426. {
  427. char *p;
  428. FLAC__byte *b;
  429. for(p = picture->mime_type; *p; p++) {
  430. if(*p < 0x20 || *p > 0x7e) {
  431. if(violation) *violation = "MIME type string must contain only printable ASCII characters (0x20-0x7e)";
  432. return false;
  433. }
  434. }
  435. for(b = picture->description; *b; ) {
  436. unsigned n = utf8len_(b);
  437. if(n == 0) {
  438. if(violation) *violation = "description string must be valid UTF-8";
  439. return false;
  440. }
  441. b += n;
  442. }
  443. return true;
  444. }
  445. /*
  446. * These routines are private to libFLAC
  447. */
  448. unsigned FLAC__format_get_max_rice_partition_order(unsigned blocksize, unsigned predictor_order)
  449. {
  450. return
  451. FLAC__format_get_max_rice_partition_order_from_blocksize_limited_max_and_predictor_order(
  452. FLAC__format_get_max_rice_partition_order_from_blocksize(blocksize),
  453. blocksize,
  454. predictor_order
  455. );
  456. }
  457. unsigned FLAC__format_get_max_rice_partition_order_from_blocksize(unsigned blocksize)
  458. {
  459. unsigned max_rice_partition_order = 0;
  460. while(!(blocksize & 1)) {
  461. max_rice_partition_order++;
  462. blocksize >>= 1;
  463. }
  464. return min(FLAC__MAX_RICE_PARTITION_ORDER, max_rice_partition_order);
  465. }
  466. unsigned FLAC__format_get_max_rice_partition_order_from_blocksize_limited_max_and_predictor_order(unsigned limit, unsigned blocksize, unsigned predictor_order)
  467. {
  468. unsigned max_rice_partition_order = limit;
  469. while(max_rice_partition_order > 0 && (blocksize >> max_rice_partition_order) <= predictor_order)
  470. max_rice_partition_order--;
  471. FLAC__ASSERT(
  472. (max_rice_partition_order == 0 && blocksize >= predictor_order) ||
  473. (max_rice_partition_order > 0 && blocksize >> max_rice_partition_order > predictor_order)
  474. );
  475. return max_rice_partition_order;
  476. }
  477. void FLAC__format_entropy_coding_method_partitioned_rice_contents_init(FLAC__EntropyCodingMethod_PartitionedRiceContents *object)
  478. {
  479. FLAC__ASSERT(0 != object);
  480. object->parameters = 0;
  481. object->raw_bits = 0;
  482. object->capacity_by_order = 0;
  483. }
  484. void FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(FLAC__EntropyCodingMethod_PartitionedRiceContents *object)
  485. {
  486. FLAC__ASSERT(0 != object);
  487. if(0 != object->parameters)
  488. free(object->parameters);
  489. if(0 != object->raw_bits)
  490. free(object->raw_bits);
  491. FLAC__format_entropy_coding_method_partitioned_rice_contents_init(object);
  492. }
  493. FLAC__bool FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(FLAC__EntropyCodingMethod_PartitionedRiceContents *object, unsigned max_partition_order)
  494. {
  495. FLAC__ASSERT(0 != object);
  496. FLAC__ASSERT(object->capacity_by_order > 0 || (0 == object->parameters && 0 == object->raw_bits));
  497. if(object->capacity_by_order < max_partition_order) {
  498. if(0 == (object->parameters = (unsigned*)realloc(object->parameters, sizeof(unsigned)*(1 << max_partition_order))))
  499. return false;
  500. if(0 == (object->raw_bits = (unsigned*)realloc(object->raw_bits, sizeof(unsigned)*(1 << max_partition_order))))
  501. return false;
  502. memset(object->raw_bits, 0, sizeof(unsigned)*(1 << max_partition_order));
  503. object->capacity_by_order = max_partition_order;
  504. }
  505. return true;
  506. }