flac_metadata_iterators.c 116 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356
  1. /* libFLAC - Free Lossless Audio Codec library
  2. * Copyright (C) 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 <errno.h>
  35. #include <stdio.h>
  36. #include <stdlib.h>
  37. #include <string.h>
  38. #if defined _WIN32
  39. #include <utime.h> /* for utime() */
  40. #include <io.h> /* for chmod() */
  41. #include <sys/types.h> /* for off_t */
  42. #else
  43. #include <sys/types.h> /* some flavors of BSD (like OS X) require this to get time_t */
  44. #include <utime.h> /* for utime() */
  45. #include <unistd.h> /* for chown(), unlink() */
  46. #endif
  47. #include <sys/stat.h> /* for stat(), maybe chmod() */
  48. #include "flac_private_metadata.h"
  49. #include "flac_FLAC_assert.h"
  50. #include "flac_FLAC_stream_decoder.h"
  51. #include "flac_share_alloc.h"
  52. #ifdef max
  53. #undef max
  54. #endif
  55. #define max(a,b) ((a)>(b)?(a):(b))
  56. #ifdef min
  57. #undef min
  58. #endif
  59. #define min(a,b) ((a)<(b)?(a):(b))
  60. /****************************************************************************
  61. *
  62. * Local function declarations
  63. *
  64. ***************************************************************************/
  65. static void pack_uint32_(FLAC__uint32 val, FLAC__byte *b, unsigned bytes);
  66. static void pack_uint32_little_endian_(FLAC__uint32 val, FLAC__byte *b, unsigned bytes);
  67. static void pack_uint64_(FLAC__uint64 val, FLAC__byte *b, unsigned bytes);
  68. static FLAC__uint32 unpack_uint32_(FLAC__byte *b, unsigned bytes);
  69. static FLAC__uint32 unpack_uint32_little_endian_(FLAC__byte *b, unsigned bytes);
  70. static FLAC__uint64 unpack_uint64_(FLAC__byte *b, unsigned bytes);
  71. static FLAC__bool read_metadata_block_header_(FLAC__Metadata_SimpleIterator *iterator);
  72. static FLAC__bool read_metadata_block_data_(FLAC__Metadata_SimpleIterator *iterator, FLAC__StreamMetadata *block);
  73. static FLAC__bool read_metadata_block_header_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__bool *is_last, FLAC__MetadataType *type, unsigned *length);
  74. static FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__IOCallback_Seek seek_cb, FLAC__StreamMetadata *block);
  75. static FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_streaminfo_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__StreamMetadata_StreamInfo *block);
  76. static FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_padding_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Seek seek_cb, FLAC__StreamMetadata_Padding *block, unsigned block_length);
  77. static FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_application_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__StreamMetadata_Application *block, unsigned block_length);
  78. static FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_seektable_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__StreamMetadata_SeekTable *block, unsigned block_length);
  79. static FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_vorbis_comment_entry_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__StreamMetadata_VorbisComment_Entry *entry);
  80. static FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_vorbis_comment_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__StreamMetadata_VorbisComment *block);
  81. static FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_cuesheet_track_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__StreamMetadata_CueSheet_Track *track);
  82. static FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_cuesheet_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__StreamMetadata_CueSheet *block);
  83. static FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_picture_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__StreamMetadata_Picture *block);
  84. static FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_unknown_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__StreamMetadata_Unknown *block, unsigned block_length);
  85. static FLAC__bool write_metadata_block_header_(FILE *file, FLAC__Metadata_SimpleIteratorStatus *status, const FLAC__StreamMetadata *block);
  86. static FLAC__bool write_metadata_block_data_(FILE *file, FLAC__Metadata_SimpleIteratorStatus *status, const FLAC__StreamMetadata *block);
  87. static FLAC__bool write_metadata_block_header_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata *block);
  88. static FLAC__bool write_metadata_block_data_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata *block);
  89. static FLAC__bool write_metadata_block_data_streaminfo_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata_StreamInfo *block);
  90. static FLAC__bool write_metadata_block_data_padding_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata_Padding *block, unsigned block_length);
  91. static FLAC__bool write_metadata_block_data_application_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata_Application *block, unsigned block_length);
  92. static FLAC__bool write_metadata_block_data_seektable_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata_SeekTable *block);
  93. static FLAC__bool write_metadata_block_data_vorbis_comment_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata_VorbisComment *block);
  94. static FLAC__bool write_metadata_block_data_cuesheet_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata_CueSheet *block);
  95. static FLAC__bool write_metadata_block_data_picture_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata_Picture *block);
  96. static FLAC__bool write_metadata_block_data_unknown_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata_Unknown *block, unsigned block_length);
  97. static FLAC__bool write_metadata_block_stationary_(FLAC__Metadata_SimpleIterator *iterator, const FLAC__StreamMetadata *block);
  98. static FLAC__bool write_metadata_block_stationary_with_padding_(FLAC__Metadata_SimpleIterator *iterator, FLAC__StreamMetadata *block, unsigned padding_length, FLAC__bool padding_is_last);
  99. static FLAC__bool rewrite_whole_file_(FLAC__Metadata_SimpleIterator *iterator, FLAC__StreamMetadata *block, FLAC__bool append);
  100. static void simple_iterator_push_(FLAC__Metadata_SimpleIterator *iterator);
  101. static FLAC__bool simple_iterator_pop_(FLAC__Metadata_SimpleIterator *iterator);
  102. static unsigned seek_to_first_metadata_block_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__IOCallback_Seek seek_cb);
  103. static unsigned seek_to_first_metadata_block_(FILE *f);
  104. static FLAC__bool simple_iterator_copy_file_prefix_(FLAC__Metadata_SimpleIterator *iterator, FILE **tempfile, char **tempfilename, FLAC__bool append);
  105. static FLAC__bool simple_iterator_copy_file_postfix_(FLAC__Metadata_SimpleIterator *iterator, FILE **tempfile, char **tempfilename, int fixup_is_last_code, off_t fixup_is_last_flag_offset, FLAC__bool backup);
  106. static FLAC__bool copy_n_bytes_from_file_(FILE *file, FILE *tempfile, off_t bytes, FLAC__Metadata_SimpleIteratorStatus *status);
  107. static FLAC__bool copy_n_bytes_from_file_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__IOHandle temp_handle, FLAC__IOCallback_Write temp_write_cb, off_t bytes, FLAC__Metadata_SimpleIteratorStatus *status);
  108. static FLAC__bool copy_remaining_bytes_from_file_(FILE *file, FILE *tempfile, FLAC__Metadata_SimpleIteratorStatus *status);
  109. static FLAC__bool copy_remaining_bytes_from_file_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__IOCallback_Eof eof_cb, FLAC__IOHandle temp_handle, FLAC__IOCallback_Write temp_write_cb, FLAC__Metadata_SimpleIteratorStatus *status);
  110. static FLAC__bool open_tempfile_(const char *filename, const char *tempfile_path_prefix, FILE **tempfile, char **tempfilename, FLAC__Metadata_SimpleIteratorStatus *status);
  111. static FLAC__bool transport_tempfile_(const char *filename, FILE **tempfile, char **tempfilename, FLAC__Metadata_SimpleIteratorStatus *status);
  112. static void cleanup_tempfile_(FILE **tempfile, char **tempfilename);
  113. static FLAC__bool get_file_stats_(const char *filename, struct stat *stats);
  114. static void set_file_stats_(const char *filename, struct stat *stats);
  115. static int fseek_wrapper_(FLAC__IOHandle handle, FLAC__int64 offset, int whence);
  116. static FLAC__int64 ftell_wrapper_(FLAC__IOHandle handle);
  117. static FLAC__Metadata_ChainStatus get_equivalent_status_(FLAC__Metadata_SimpleIteratorStatus status);
  118. #ifdef FLAC__VALGRIND_TESTING
  119. static size_t local__fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream)
  120. {
  121. size_t ret = fwrite(ptr, size, nmemb, stream);
  122. if(!ferror(stream))
  123. fflush(stream);
  124. return ret;
  125. }
  126. #else
  127. #define local__fwrite fwrite
  128. #endif
  129. /****************************************************************************
  130. *
  131. * Level 0 implementation
  132. *
  133. ***************************************************************************/
  134. static FLAC__StreamDecoderWriteStatus write_callback_(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data);
  135. static void metadata_callback_(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data);
  136. static void error_callback_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data);
  137. typedef struct {
  138. FLAC__bool got_error;
  139. FLAC__StreamMetadata *object;
  140. } level0_client_data;
  141. static FLAC__StreamMetadata *get_one_metadata_block_(const char *filename, FLAC__MetadataType type)
  142. {
  143. level0_client_data cd;
  144. FLAC__StreamDecoder *decoder;
  145. FLAC__ASSERT(0 != filename);
  146. cd.got_error = false;
  147. cd.object = 0;
  148. decoder = FLAC__stream_decoder_new();
  149. if(0 == decoder)
  150. return 0;
  151. FLAC__stream_decoder_set_md5_checking(decoder, false);
  152. FLAC__stream_decoder_set_metadata_ignore_all(decoder);
  153. FLAC__stream_decoder_set_metadata_respond(decoder, type);
  154. if(FLAC__stream_decoder_init_file(decoder, filename, write_callback_, metadata_callback_, error_callback_, &cd) != FLAC__STREAM_DECODER_INIT_STATUS_OK || cd.got_error) {
  155. (void)FLAC__stream_decoder_finish(decoder);
  156. FLAC__stream_decoder_delete(decoder);
  157. return 0;
  158. }
  159. if(!FLAC__stream_decoder_process_until_end_of_metadata(decoder) || cd.got_error) {
  160. (void)FLAC__stream_decoder_finish(decoder);
  161. FLAC__stream_decoder_delete(decoder);
  162. if(0 != cd.object)
  163. FLAC__metadata_object_delete(cd.object);
  164. return 0;
  165. }
  166. (void)FLAC__stream_decoder_finish(decoder);
  167. FLAC__stream_decoder_delete(decoder);
  168. return cd.object;
  169. }
  170. FLAC_API FLAC__bool FLAC__metadata_get_streaminfo(const char *filename, FLAC__StreamMetadata *streaminfo)
  171. {
  172. FLAC__StreamMetadata *object;
  173. FLAC__ASSERT(0 != filename);
  174. FLAC__ASSERT(0 != streaminfo);
  175. object = get_one_metadata_block_(filename, FLAC__METADATA_TYPE_STREAMINFO);
  176. if (object) {
  177. /* can just copy the contents since STREAMINFO has no internal structure */
  178. *streaminfo = *object;
  179. FLAC__metadata_object_delete(object);
  180. return true;
  181. }
  182. else {
  183. return false;
  184. }
  185. }
  186. FLAC_API FLAC__bool FLAC__metadata_get_tags(const char *filename, FLAC__StreamMetadata **tags)
  187. {
  188. FLAC__ASSERT(0 != filename);
  189. FLAC__ASSERT(0 != tags);
  190. *tags = get_one_metadata_block_(filename, FLAC__METADATA_TYPE_VORBIS_COMMENT);
  191. return 0 != *tags;
  192. }
  193. FLAC_API FLAC__bool FLAC__metadata_get_cuesheet(const char *filename, FLAC__StreamMetadata **cuesheet)
  194. {
  195. FLAC__ASSERT(0 != filename);
  196. FLAC__ASSERT(0 != cuesheet);
  197. *cuesheet = get_one_metadata_block_(filename, FLAC__METADATA_TYPE_CUESHEET);
  198. return 0 != *cuesheet;
  199. }
  200. FLAC__StreamDecoderWriteStatus write_callback_(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data)
  201. {
  202. (void)decoder, (void)frame, (void)buffer, (void)client_data;
  203. return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
  204. }
  205. void metadata_callback_(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data)
  206. {
  207. level0_client_data *cd = (level0_client_data *)client_data;
  208. (void)decoder;
  209. /*
  210. * we assume we only get here when the one metadata block we were
  211. * looking for was passed to us
  212. */
  213. if(!cd->got_error && 0 == cd->object) {
  214. if(0 == (cd->object = FLAC__metadata_object_clone(metadata)))
  215. cd->got_error = true;
  216. }
  217. }
  218. void error_callback_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data)
  219. {
  220. level0_client_data *cd = (level0_client_data *)client_data;
  221. (void)decoder;
  222. if(status != FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC)
  223. cd->got_error = true;
  224. }
  225. FLAC_API FLAC__bool FLAC__metadata_get_picture(const char *filename, FLAC__StreamMetadata **picture, FLAC__StreamMetadata_Picture_Type type, const char *mime_type, const FLAC__byte *description, unsigned max_width, unsigned max_height, unsigned max_depth, unsigned max_colors)
  226. {
  227. FLAC__Metadata_SimpleIterator *it;
  228. FLAC__uint64 max_area_seen = 0;
  229. FLAC__uint64 max_depth_seen = 0;
  230. FLAC__ASSERT(0 != filename);
  231. FLAC__ASSERT(0 != picture);
  232. *picture = 0;
  233. it = FLAC__metadata_simple_iterator_new();
  234. if(0 == it)
  235. return false;
  236. if(!FLAC__metadata_simple_iterator_init(it, filename, /*read_only=*/true, /*preserve_file_stats=*/true)) {
  237. FLAC__metadata_simple_iterator_delete(it);
  238. return false;
  239. }
  240. do {
  241. if(FLAC__metadata_simple_iterator_get_block_type(it) == FLAC__METADATA_TYPE_PICTURE) {
  242. FLAC__StreamMetadata *obj = FLAC__metadata_simple_iterator_get_block(it);
  243. FLAC__uint64 area = (FLAC__uint64)obj->data.picture.width * (FLAC__uint64)obj->data.picture.height;
  244. /* check constraints */
  245. if(
  246. (type == (FLAC__StreamMetadata_Picture_Type)(-1) || type == obj->data.picture.type) &&
  247. (mime_type == 0 || !strcmp(mime_type, obj->data.picture.mime_type)) &&
  248. (description == 0 || !strcmp((const char *)description, (const char *)obj->data.picture.description)) &&
  249. obj->data.picture.width <= max_width &&
  250. obj->data.picture.height <= max_height &&
  251. obj->data.picture.depth <= max_depth &&
  252. obj->data.picture.colors <= max_colors &&
  253. (area > max_area_seen || (area == max_area_seen && obj->data.picture.depth > max_depth_seen))
  254. ) {
  255. if(*picture)
  256. FLAC__metadata_object_delete(*picture);
  257. *picture = obj;
  258. max_area_seen = area;
  259. max_depth_seen = obj->data.picture.depth;
  260. }
  261. else {
  262. FLAC__metadata_object_delete(obj);
  263. }
  264. }
  265. } while(FLAC__metadata_simple_iterator_next(it));
  266. FLAC__metadata_simple_iterator_delete(it);
  267. return (0 != *picture);
  268. }
  269. /****************************************************************************
  270. *
  271. * Level 1 implementation
  272. *
  273. ***************************************************************************/
  274. #define SIMPLE_ITERATOR_MAX_PUSH_DEPTH (1+4)
  275. /* 1 for initial offset, +4 for our own personal use */
  276. struct FLAC__Metadata_SimpleIterator {
  277. FILE *file;
  278. char *filename, *tempfile_path_prefix;
  279. struct stat stats;
  280. FLAC__bool has_stats;
  281. FLAC__bool is_writable;
  282. FLAC__Metadata_SimpleIteratorStatus status;
  283. off_t offset[SIMPLE_ITERATOR_MAX_PUSH_DEPTH];
  284. off_t first_offset; /* this is the offset to the STREAMINFO block */
  285. unsigned depth;
  286. /* this is the metadata block header of the current block we are pointing to: */
  287. FLAC__bool is_last;
  288. FLAC__MetadataType type;
  289. unsigned length;
  290. };
  291. FLAC_API const char * const FLAC__Metadata_SimpleIteratorStatusString[] = {
  292. "FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK",
  293. "FLAC__METADATA_SIMPLE_ITERATOR_STATUS_ILLEGAL_INPUT",
  294. "FLAC__METADATA_SIMPLE_ITERATOR_STATUS_ERROR_OPENING_FILE",
  295. "FLAC__METADATA_SIMPLE_ITERATOR_STATUS_NOT_A_FLAC_FILE",
  296. "FLAC__METADATA_SIMPLE_ITERATOR_STATUS_NOT_WRITABLE",
  297. "FLAC__METADATA_SIMPLE_ITERATOR_STATUS_BAD_METADATA",
  298. "FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR",
  299. "FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR",
  300. "FLAC__METADATA_SIMPLE_ITERATOR_STATUS_WRITE_ERROR",
  301. "FLAC__METADATA_SIMPLE_ITERATOR_STATUS_RENAME_ERROR",
  302. "FLAC__METADATA_SIMPLE_ITERATOR_STATUS_UNLINK_ERROR",
  303. "FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR",
  304. "FLAC__METADATA_SIMPLE_ITERATOR_STATUS_INTERNAL_ERROR"
  305. };
  306. FLAC_API FLAC__Metadata_SimpleIterator *FLAC__metadata_simple_iterator_new(void)
  307. {
  308. FLAC__Metadata_SimpleIterator *iterator = (FLAC__Metadata_SimpleIterator*)calloc(1, sizeof(FLAC__Metadata_SimpleIterator));
  309. if(0 != iterator) {
  310. iterator->file = 0;
  311. iterator->filename = 0;
  312. iterator->tempfile_path_prefix = 0;
  313. iterator->has_stats = false;
  314. iterator->is_writable = false;
  315. iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK;
  316. iterator->first_offset = iterator->offset[0] = -1;
  317. iterator->depth = 0;
  318. }
  319. return iterator;
  320. }
  321. static void simple_iterator_free_guts_(FLAC__Metadata_SimpleIterator *iterator)
  322. {
  323. FLAC__ASSERT(0 != iterator);
  324. if(0 != iterator->file) {
  325. fclose(iterator->file);
  326. iterator->file = 0;
  327. if(iterator->has_stats)
  328. set_file_stats_(iterator->filename, &iterator->stats);
  329. }
  330. if(0 != iterator->filename) {
  331. free(iterator->filename);
  332. iterator->filename = 0;
  333. }
  334. if(0 != iterator->tempfile_path_prefix) {
  335. free(iterator->tempfile_path_prefix);
  336. iterator->tempfile_path_prefix = 0;
  337. }
  338. }
  339. FLAC_API void FLAC__metadata_simple_iterator_delete(FLAC__Metadata_SimpleIterator *iterator)
  340. {
  341. FLAC__ASSERT(0 != iterator);
  342. simple_iterator_free_guts_(iterator);
  343. free(iterator);
  344. }
  345. FLAC_API FLAC__Metadata_SimpleIteratorStatus FLAC__metadata_simple_iterator_status(FLAC__Metadata_SimpleIterator *iterator)
  346. {
  347. FLAC__Metadata_SimpleIteratorStatus status;
  348. FLAC__ASSERT(0 != iterator);
  349. status = iterator->status;
  350. iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK;
  351. return status;
  352. }
  353. static FLAC__bool simple_iterator_prime_input_(FLAC__Metadata_SimpleIterator *iterator, FLAC__bool read_only)
  354. {
  355. unsigned ret;
  356. FLAC__ASSERT(0 != iterator);
  357. if(read_only || 0 == (iterator->file = fopen(iterator->filename, "r+b"))) {
  358. iterator->is_writable = false;
  359. if(read_only || errno == EACCES) {
  360. if(0 == (iterator->file = fopen(iterator->filename, "rb"))) {
  361. iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_ERROR_OPENING_FILE;
  362. return false;
  363. }
  364. }
  365. else {
  366. iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_ERROR_OPENING_FILE;
  367. return false;
  368. }
  369. }
  370. else {
  371. iterator->is_writable = true;
  372. }
  373. ret = seek_to_first_metadata_block_(iterator->file);
  374. switch(ret) {
  375. case 0:
  376. iterator->depth = 0;
  377. iterator->first_offset = iterator->offset[iterator->depth] = ftello(iterator->file);
  378. return read_metadata_block_header_(iterator);
  379. case 1:
  380. iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
  381. return false;
  382. case 2:
  383. iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR;
  384. return false;
  385. case 3:
  386. iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_NOT_A_FLAC_FILE;
  387. return false;
  388. default:
  389. FLAC__ASSERT(0);
  390. return false;
  391. }
  392. }
  393. #if 0
  394. @@@ If we decide to finish implementing this, put this comment back in metadata.h
  395. /*
  396. * The 'tempfile_path_prefix' allows you to specify a directory where
  397. * tempfiles should go. Remember that if your metadata edits cause the
  398. * FLAC file to grow, the entire file will have to be rewritten. If
  399. * 'tempfile_path_prefix' is NULL, the temp file will be written in the
  400. * same directory as the original FLAC file. This makes replacing the
  401. * original with the tempfile fast but requires extra space in the same
  402. * partition for the tempfile. If space is a problem, you can pass a
  403. * directory name belonging to a different partition in
  404. * 'tempfile_path_prefix'. Note that you should use the forward slash
  405. * '/' as the directory separator. A trailing slash is not needed; it
  406. * will be added automatically.
  407. */
  408. FLAC__bool FLAC__metadata_simple_iterator_init(FLAC__Metadata_SimpleIterator *iterator, const char *filename, FLAC__bool preserve_file_stats, const char *tempfile_path_prefix);
  409. #endif
  410. FLAC_API FLAC__bool FLAC__metadata_simple_iterator_init(FLAC__Metadata_SimpleIterator *iterator, const char *filename, FLAC__bool read_only, FLAC__bool preserve_file_stats)
  411. {
  412. const char *tempfile_path_prefix = 0; /*@@@ search for comments near 'rename(...)' for what it will take to finish implementing this */
  413. FLAC__ASSERT(0 != iterator);
  414. FLAC__ASSERT(0 != filename);
  415. simple_iterator_free_guts_(iterator);
  416. if(!read_only && preserve_file_stats)
  417. iterator->has_stats = get_file_stats_(filename, &iterator->stats);
  418. if(0 == (iterator->filename = strdup(filename))) {
  419. iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR;
  420. return false;
  421. }
  422. if(0 != tempfile_path_prefix && 0 == (iterator->tempfile_path_prefix = strdup(tempfile_path_prefix))) {
  423. iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR;
  424. return false;
  425. }
  426. return simple_iterator_prime_input_(iterator, read_only);
  427. }
  428. FLAC_API FLAC__bool FLAC__metadata_simple_iterator_is_writable(const FLAC__Metadata_SimpleIterator *iterator)
  429. {
  430. FLAC__ASSERT(0 != iterator);
  431. FLAC__ASSERT(0 != iterator->file);
  432. return iterator->is_writable;
  433. }
  434. FLAC_API FLAC__bool FLAC__metadata_simple_iterator_next(FLAC__Metadata_SimpleIterator *iterator)
  435. {
  436. FLAC__ASSERT(0 != iterator);
  437. FLAC__ASSERT(0 != iterator->file);
  438. if(iterator->is_last)
  439. return false;
  440. if(0 != fseeko(iterator->file, iterator->length, SEEK_CUR)) {
  441. iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR;
  442. return false;
  443. }
  444. iterator->offset[iterator->depth] = ftello(iterator->file);
  445. return read_metadata_block_header_(iterator);
  446. }
  447. FLAC_API FLAC__bool FLAC__metadata_simple_iterator_prev(FLAC__Metadata_SimpleIterator *iterator)
  448. {
  449. off_t this_offset;
  450. FLAC__ASSERT(0 != iterator);
  451. FLAC__ASSERT(0 != iterator->file);
  452. if(iterator->offset[iterator->depth] == iterator->first_offset)
  453. return false;
  454. if(0 != fseeko(iterator->file, iterator->first_offset, SEEK_SET)) {
  455. iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR;
  456. return false;
  457. }
  458. this_offset = iterator->first_offset;
  459. if(!read_metadata_block_header_(iterator))
  460. return false;
  461. /* we ignore any error from ftello() and catch it in fseeko() */
  462. while(ftello(iterator->file) + (off_t)iterator->length < iterator->offset[iterator->depth]) {
  463. if(0 != fseeko(iterator->file, iterator->length, SEEK_CUR)) {
  464. iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR;
  465. return false;
  466. }
  467. this_offset = ftello(iterator->file);
  468. if(!read_metadata_block_header_(iterator))
  469. return false;
  470. }
  471. iterator->offset[iterator->depth] = this_offset;
  472. return true;
  473. }
  474. /*@@@@add to tests*/
  475. FLAC_API FLAC__bool FLAC__metadata_simple_iterator_is_last(const FLAC__Metadata_SimpleIterator *iterator)
  476. {
  477. FLAC__ASSERT(0 != iterator);
  478. FLAC__ASSERT(0 != iterator->file);
  479. return iterator->is_last;
  480. }
  481. /*@@@@add to tests*/
  482. FLAC_API off_t FLAC__metadata_simple_iterator_get_block_offset(const FLAC__Metadata_SimpleIterator *iterator)
  483. {
  484. FLAC__ASSERT(0 != iterator);
  485. FLAC__ASSERT(0 != iterator->file);
  486. return iterator->offset[iterator->depth];
  487. }
  488. FLAC_API FLAC__MetadataType FLAC__metadata_simple_iterator_get_block_type(const FLAC__Metadata_SimpleIterator *iterator)
  489. {
  490. FLAC__ASSERT(0 != iterator);
  491. FLAC__ASSERT(0 != iterator->file);
  492. return iterator->type;
  493. }
  494. /*@@@@add to tests*/
  495. FLAC_API unsigned FLAC__metadata_simple_iterator_get_block_length(const FLAC__Metadata_SimpleIterator *iterator)
  496. {
  497. FLAC__ASSERT(0 != iterator);
  498. FLAC__ASSERT(0 != iterator->file);
  499. return iterator->length;
  500. }
  501. /*@@@@add to tests*/
  502. FLAC_API FLAC__bool FLAC__metadata_simple_iterator_get_application_id(FLAC__Metadata_SimpleIterator *iterator, FLAC__byte *id)
  503. {
  504. const unsigned id_bytes = FLAC__STREAM_METADATA_APPLICATION_ID_LEN / 8;
  505. FLAC__ASSERT(0 != iterator);
  506. FLAC__ASSERT(0 != iterator->file);
  507. FLAC__ASSERT(0 != id);
  508. if(iterator->type != FLAC__METADATA_TYPE_APPLICATION) {
  509. iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_ILLEGAL_INPUT;
  510. return false;
  511. }
  512. if(fread(id, 1, id_bytes, iterator->file) != id_bytes) {
  513. iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
  514. return false;
  515. }
  516. /* back up */
  517. if(0 != fseeko(iterator->file, -((int)id_bytes), SEEK_CUR)) {
  518. iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR;
  519. return false;
  520. }
  521. return true;
  522. }
  523. FLAC_API FLAC__StreamMetadata *FLAC__metadata_simple_iterator_get_block(FLAC__Metadata_SimpleIterator *iterator)
  524. {
  525. FLAC__StreamMetadata *block = FLAC__metadata_object_new(iterator->type);
  526. FLAC__ASSERT(0 != iterator);
  527. FLAC__ASSERT(0 != iterator->file);
  528. if(0 != block) {
  529. block->is_last = iterator->is_last;
  530. block->length = iterator->length;
  531. if(!read_metadata_block_data_(iterator, block)) {
  532. FLAC__metadata_object_delete(block);
  533. return 0;
  534. }
  535. /* back up to the beginning of the block data to stay consistent */
  536. if(0 != fseeko(iterator->file, iterator->offset[iterator->depth] + FLAC__STREAM_METADATA_HEADER_LENGTH, SEEK_SET)) {
  537. iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR;
  538. FLAC__metadata_object_delete(block);
  539. return 0;
  540. }
  541. }
  542. else
  543. iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR;
  544. return block;
  545. }
  546. FLAC_API FLAC__bool FLAC__metadata_simple_iterator_set_block(FLAC__Metadata_SimpleIterator *iterator, FLAC__StreamMetadata *block, FLAC__bool use_padding)
  547. {
  548. FLAC__ASSERT_DECLARATION(off_t debug_target_offset = iterator->offset[iterator->depth];)
  549. FLAC__bool ret;
  550. FLAC__ASSERT(0 != iterator);
  551. FLAC__ASSERT(0 != iterator->file);
  552. FLAC__ASSERT(0 != block);
  553. if(!iterator->is_writable) {
  554. iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_NOT_WRITABLE;
  555. return false;
  556. }
  557. if(iterator->type == FLAC__METADATA_TYPE_STREAMINFO || block->type == FLAC__METADATA_TYPE_STREAMINFO) {
  558. if(iterator->type != block->type) {
  559. iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_ILLEGAL_INPUT;
  560. return false;
  561. }
  562. }
  563. block->is_last = iterator->is_last;
  564. if(iterator->length == block->length)
  565. return write_metadata_block_stationary_(iterator, block);
  566. else if(iterator->length > block->length) {
  567. if(use_padding && iterator->length >= FLAC__STREAM_METADATA_HEADER_LENGTH + block->length) {
  568. ret = write_metadata_block_stationary_with_padding_(iterator, block, iterator->length - FLAC__STREAM_METADATA_HEADER_LENGTH - block->length, block->is_last);
  569. FLAC__ASSERT(!ret || iterator->offset[iterator->depth] == debug_target_offset);
  570. FLAC__ASSERT(!ret || ftello(iterator->file) == debug_target_offset + (off_t)FLAC__STREAM_METADATA_HEADER_LENGTH);
  571. return ret;
  572. }
  573. else {
  574. ret = rewrite_whole_file_(iterator, block, /*append=*/false);
  575. FLAC__ASSERT(!ret || iterator->offset[iterator->depth] == debug_target_offset);
  576. FLAC__ASSERT(!ret || ftello(iterator->file) == debug_target_offset + (off_t)FLAC__STREAM_METADATA_HEADER_LENGTH);
  577. return ret;
  578. }
  579. }
  580. else /* iterator->length < block->length */ {
  581. unsigned padding_leftover = 0;
  582. FLAC__bool padding_is_last = false;
  583. if(use_padding) {
  584. /* first see if we can even use padding */
  585. if(iterator->is_last) {
  586. use_padding = false;
  587. }
  588. else {
  589. const unsigned extra_padding_bytes_required = block->length - iterator->length;
  590. simple_iterator_push_(iterator);
  591. if(!FLAC__metadata_simple_iterator_next(iterator)) {
  592. (void)simple_iterator_pop_(iterator);
  593. return false;
  594. }
  595. if(iterator->type != FLAC__METADATA_TYPE_PADDING) {
  596. use_padding = false;
  597. }
  598. else {
  599. if(FLAC__STREAM_METADATA_HEADER_LENGTH + iterator->length == extra_padding_bytes_required) {
  600. padding_leftover = 0;
  601. block->is_last = iterator->is_last;
  602. }
  603. else if(iterator->length < extra_padding_bytes_required)
  604. use_padding = false;
  605. else {
  606. padding_leftover = FLAC__STREAM_METADATA_HEADER_LENGTH + iterator->length - extra_padding_bytes_required;
  607. padding_is_last = iterator->is_last;
  608. block->is_last = false;
  609. }
  610. }
  611. if(!simple_iterator_pop_(iterator))
  612. return false;
  613. }
  614. }
  615. if(use_padding) {
  616. if(padding_leftover == 0) {
  617. ret = write_metadata_block_stationary_(iterator, block);
  618. FLAC__ASSERT(!ret || iterator->offset[iterator->depth] == debug_target_offset);
  619. FLAC__ASSERT(!ret || ftello(iterator->file) == debug_target_offset + (off_t)FLAC__STREAM_METADATA_HEADER_LENGTH);
  620. return ret;
  621. }
  622. else {
  623. FLAC__ASSERT(padding_leftover >= FLAC__STREAM_METADATA_HEADER_LENGTH);
  624. ret = write_metadata_block_stationary_with_padding_(iterator, block, padding_leftover - FLAC__STREAM_METADATA_HEADER_LENGTH, padding_is_last);
  625. FLAC__ASSERT(!ret || iterator->offset[iterator->depth] == debug_target_offset);
  626. FLAC__ASSERT(!ret || ftello(iterator->file) == debug_target_offset + (off_t)FLAC__STREAM_METADATA_HEADER_LENGTH);
  627. return ret;
  628. }
  629. }
  630. else {
  631. ret = rewrite_whole_file_(iterator, block, /*append=*/false);
  632. FLAC__ASSERT(!ret || iterator->offset[iterator->depth] == debug_target_offset);
  633. FLAC__ASSERT(!ret || ftello(iterator->file) == debug_target_offset + (off_t)FLAC__STREAM_METADATA_HEADER_LENGTH);
  634. return ret;
  635. }
  636. }
  637. }
  638. FLAC_API FLAC__bool FLAC__metadata_simple_iterator_insert_block_after(FLAC__Metadata_SimpleIterator *iterator, FLAC__StreamMetadata *block, FLAC__bool use_padding)
  639. {
  640. unsigned padding_leftover = 0;
  641. FLAC__bool padding_is_last = false;
  642. FLAC__ASSERT_DECLARATION(off_t debug_target_offset = iterator->offset[iterator->depth] + FLAC__STREAM_METADATA_HEADER_LENGTH + iterator->length;)
  643. FLAC__bool ret;
  644. FLAC__ASSERT(0 != iterator);
  645. FLAC__ASSERT(0 != iterator->file);
  646. FLAC__ASSERT(0 != block);
  647. if(!iterator->is_writable)
  648. return false;
  649. if(block->type == FLAC__METADATA_TYPE_STREAMINFO) {
  650. iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_ILLEGAL_INPUT;
  651. return false;
  652. }
  653. block->is_last = iterator->is_last;
  654. if(use_padding) {
  655. /* first see if we can even use padding */
  656. if(iterator->is_last) {
  657. use_padding = false;
  658. }
  659. else {
  660. simple_iterator_push_(iterator);
  661. if(!FLAC__metadata_simple_iterator_next(iterator)) {
  662. (void)simple_iterator_pop_(iterator);
  663. return false;
  664. }
  665. if(iterator->type != FLAC__METADATA_TYPE_PADDING) {
  666. use_padding = false;
  667. }
  668. else {
  669. if(iterator->length == block->length) {
  670. padding_leftover = 0;
  671. block->is_last = iterator->is_last;
  672. }
  673. else if(iterator->length < FLAC__STREAM_METADATA_HEADER_LENGTH + block->length)
  674. use_padding = false;
  675. else {
  676. padding_leftover = iterator->length - block->length;
  677. padding_is_last = iterator->is_last;
  678. block->is_last = false;
  679. }
  680. }
  681. if(!simple_iterator_pop_(iterator))
  682. return false;
  683. }
  684. }
  685. if(use_padding) {
  686. /* move to the next block, which is suitable padding */
  687. if(!FLAC__metadata_simple_iterator_next(iterator))
  688. return false;
  689. if(padding_leftover == 0) {
  690. ret = write_metadata_block_stationary_(iterator, block);
  691. FLAC__ASSERT(iterator->offset[iterator->depth] == debug_target_offset);
  692. FLAC__ASSERT(ftello(iterator->file) == debug_target_offset + (off_t)FLAC__STREAM_METADATA_HEADER_LENGTH);
  693. return ret;
  694. }
  695. else {
  696. FLAC__ASSERT(padding_leftover >= FLAC__STREAM_METADATA_HEADER_LENGTH);
  697. ret = write_metadata_block_stationary_with_padding_(iterator, block, padding_leftover - FLAC__STREAM_METADATA_HEADER_LENGTH, padding_is_last);
  698. FLAC__ASSERT(iterator->offset[iterator->depth] == debug_target_offset);
  699. FLAC__ASSERT(ftello(iterator->file) == debug_target_offset + (off_t)FLAC__STREAM_METADATA_HEADER_LENGTH);
  700. return ret;
  701. }
  702. }
  703. else {
  704. ret = rewrite_whole_file_(iterator, block, /*append=*/true);
  705. FLAC__ASSERT(iterator->offset[iterator->depth] == debug_target_offset);
  706. FLAC__ASSERT(ftello(iterator->file) == debug_target_offset + (off_t)FLAC__STREAM_METADATA_HEADER_LENGTH);
  707. return ret;
  708. }
  709. }
  710. FLAC_API FLAC__bool FLAC__metadata_simple_iterator_delete_block(FLAC__Metadata_SimpleIterator *iterator, FLAC__bool use_padding)
  711. {
  712. FLAC__ASSERT_DECLARATION(off_t debug_target_offset = iterator->offset[iterator->depth];)
  713. FLAC__bool ret;
  714. if(iterator->type == FLAC__METADATA_TYPE_STREAMINFO) {
  715. iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_ILLEGAL_INPUT;
  716. return false;
  717. }
  718. if(use_padding) {
  719. FLAC__StreamMetadata *padding = FLAC__metadata_object_new(FLAC__METADATA_TYPE_PADDING);
  720. if(0 == padding) {
  721. iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR;
  722. return false;
  723. }
  724. padding->length = iterator->length;
  725. if(!FLAC__metadata_simple_iterator_set_block(iterator, padding, false)) {
  726. FLAC__metadata_object_delete(padding);
  727. return false;
  728. }
  729. FLAC__metadata_object_delete(padding);
  730. if(!FLAC__metadata_simple_iterator_prev(iterator))
  731. return false;
  732. FLAC__ASSERT(iterator->offset[iterator->depth] + (off_t)FLAC__STREAM_METADATA_HEADER_LENGTH + (off_t)iterator->length == debug_target_offset);
  733. FLAC__ASSERT(ftello(iterator->file) + (off_t)iterator->length == debug_target_offset);
  734. return true;
  735. }
  736. else {
  737. ret = rewrite_whole_file_(iterator, 0, /*append=*/false);
  738. FLAC__ASSERT(iterator->offset[iterator->depth] + (off_t)FLAC__STREAM_METADATA_HEADER_LENGTH + (off_t)iterator->length == debug_target_offset);
  739. FLAC__ASSERT(ftello(iterator->file) + (off_t)iterator->length == debug_target_offset);
  740. return ret;
  741. }
  742. }
  743. /****************************************************************************
  744. *
  745. * Level 2 implementation
  746. *
  747. ***************************************************************************/
  748. typedef struct FLAC__Metadata_Node {
  749. FLAC__StreamMetadata *data;
  750. struct FLAC__Metadata_Node *prev, *next;
  751. } FLAC__Metadata_Node;
  752. struct FLAC__Metadata_Chain {
  753. char *filename; /* will be NULL if using callbacks */
  754. FLAC__bool is_ogg;
  755. FLAC__Metadata_Node *head;
  756. FLAC__Metadata_Node *tail;
  757. unsigned nodes;
  758. FLAC__Metadata_ChainStatus status;
  759. off_t first_offset, last_offset;
  760. /*
  761. * This is the length of the chain initially read from the FLAC file.
  762. * it is used to compare against the current length to decide whether
  763. * or not the whole file has to be rewritten.
  764. */
  765. off_t initial_length;
  766. /* @@@ hacky, these are currently only needed by ogg reader */
  767. FLAC__IOHandle handle;
  768. FLAC__IOCallback_Read read_cb;
  769. };
  770. struct FLAC__Metadata_Iterator {
  771. FLAC__Metadata_Chain *chain;
  772. FLAC__Metadata_Node *current;
  773. };
  774. FLAC_API const char * const FLAC__Metadata_ChainStatusString[] = {
  775. "FLAC__METADATA_CHAIN_STATUS_OK",
  776. "FLAC__METADATA_CHAIN_STATUS_ILLEGAL_INPUT",
  777. "FLAC__METADATA_CHAIN_STATUS_ERROR_OPENING_FILE",
  778. "FLAC__METADATA_CHAIN_STATUS_NOT_A_FLAC_FILE",
  779. "FLAC__METADATA_CHAIN_STATUS_NOT_WRITABLE",
  780. "FLAC__METADATA_CHAIN_STATUS_BAD_METADATA",
  781. "FLAC__METADATA_CHAIN_STATUS_READ_ERROR",
  782. "FLAC__METADATA_CHAIN_STATUS_SEEK_ERROR",
  783. "FLAC__METADATA_CHAIN_STATUS_WRITE_ERROR",
  784. "FLAC__METADATA_CHAIN_STATUS_RENAME_ERROR",
  785. "FLAC__METADATA_CHAIN_STATUS_UNLINK_ERROR",
  786. "FLAC__METADATA_CHAIN_STATUS_MEMORY_ALLOCATION_ERROR",
  787. "FLAC__METADATA_CHAIN_STATUS_INTERNAL_ERROR",
  788. "FLAC__METADATA_CHAIN_STATUS_INVALID_CALLBACKS",
  789. "FLAC__METADATA_CHAIN_STATUS_READ_WRITE_MISMATCH",
  790. "FLAC__METADATA_CHAIN_STATUS_WRONG_WRITE_CALL"
  791. };
  792. static FLAC__Metadata_Node *node_new_(void)
  793. {
  794. return (FLAC__Metadata_Node*)calloc(1, sizeof(FLAC__Metadata_Node));
  795. }
  796. static void node_delete_(FLAC__Metadata_Node *node)
  797. {
  798. FLAC__ASSERT(0 != node);
  799. if(0 != node->data)
  800. FLAC__metadata_object_delete(node->data);
  801. free(node);
  802. }
  803. static void chain_init_(FLAC__Metadata_Chain *chain)
  804. {
  805. FLAC__ASSERT(0 != chain);
  806. chain->filename = 0;
  807. chain->is_ogg = false;
  808. chain->head = chain->tail = 0;
  809. chain->nodes = 0;
  810. chain->status = FLAC__METADATA_CHAIN_STATUS_OK;
  811. chain->initial_length = 0;
  812. chain->read_cb = 0;
  813. }
  814. static void chain_clear_(FLAC__Metadata_Chain *chain)
  815. {
  816. FLAC__Metadata_Node *node, *next;
  817. FLAC__ASSERT(0 != chain);
  818. for(node = chain->head; node; ) {
  819. next = node->next;
  820. node_delete_(node);
  821. node = next;
  822. }
  823. if(0 != chain->filename)
  824. free(chain->filename);
  825. chain_init_(chain);
  826. }
  827. static void chain_append_node_(FLAC__Metadata_Chain *chain, FLAC__Metadata_Node *node)
  828. {
  829. FLAC__ASSERT(0 != chain);
  830. FLAC__ASSERT(0 != node);
  831. FLAC__ASSERT(0 != node->data);
  832. node->next = node->prev = 0;
  833. node->data->is_last = true;
  834. if(0 != chain->tail)
  835. chain->tail->data->is_last = false;
  836. if(0 == chain->head)
  837. chain->head = node;
  838. else {
  839. FLAC__ASSERT(0 != chain->tail);
  840. chain->tail->next = node;
  841. node->prev = chain->tail;
  842. }
  843. chain->tail = node;
  844. chain->nodes++;
  845. }
  846. static void chain_remove_node_(FLAC__Metadata_Chain *chain, FLAC__Metadata_Node *node)
  847. {
  848. FLAC__ASSERT(0 != chain);
  849. FLAC__ASSERT(0 != node);
  850. if(node == chain->head)
  851. chain->head = node->next;
  852. else
  853. node->prev->next = node->next;
  854. if(node == chain->tail)
  855. chain->tail = node->prev;
  856. else
  857. node->next->prev = node->prev;
  858. if(0 != chain->tail)
  859. chain->tail->data->is_last = true;
  860. chain->nodes--;
  861. }
  862. static void chain_delete_node_(FLAC__Metadata_Chain *chain, FLAC__Metadata_Node *node)
  863. {
  864. chain_remove_node_(chain, node);
  865. node_delete_(node);
  866. }
  867. static off_t chain_calculate_length_(FLAC__Metadata_Chain *chain)
  868. {
  869. const FLAC__Metadata_Node *node;
  870. off_t length = 0;
  871. for(node = chain->head; node; node = node->next)
  872. length += (FLAC__STREAM_METADATA_HEADER_LENGTH + node->data->length);
  873. return length;
  874. }
  875. static void iterator_insert_node_(FLAC__Metadata_Iterator *iterator, FLAC__Metadata_Node *node)
  876. {
  877. FLAC__ASSERT(0 != node);
  878. FLAC__ASSERT(0 != node->data);
  879. FLAC__ASSERT(0 != iterator);
  880. FLAC__ASSERT(0 != iterator->current);
  881. FLAC__ASSERT(0 != iterator->chain);
  882. FLAC__ASSERT(0 != iterator->chain->head);
  883. FLAC__ASSERT(0 != iterator->chain->tail);
  884. node->data->is_last = false;
  885. node->prev = iterator->current->prev;
  886. node->next = iterator->current;
  887. if(0 == node->prev)
  888. iterator->chain->head = node;
  889. else
  890. node->prev->next = node;
  891. iterator->current->prev = node;
  892. iterator->chain->nodes++;
  893. }
  894. static void iterator_insert_node_after_(FLAC__Metadata_Iterator *iterator, FLAC__Metadata_Node *node)
  895. {
  896. FLAC__ASSERT(0 != node);
  897. FLAC__ASSERT(0 != node->data);
  898. FLAC__ASSERT(0 != iterator);
  899. FLAC__ASSERT(0 != iterator->current);
  900. FLAC__ASSERT(0 != iterator->chain);
  901. FLAC__ASSERT(0 != iterator->chain->head);
  902. FLAC__ASSERT(0 != iterator->chain->tail);
  903. iterator->current->data->is_last = false;
  904. node->prev = iterator->current;
  905. node->next = iterator->current->next;
  906. if(0 == node->next)
  907. iterator->chain->tail = node;
  908. else
  909. node->next->prev = node;
  910. node->prev->next = node;
  911. iterator->chain->tail->data->is_last = true;
  912. iterator->chain->nodes++;
  913. }
  914. /* return true iff node and node->next are both padding */
  915. static FLAC__bool chain_merge_adjacent_padding_(FLAC__Metadata_Chain *chain, FLAC__Metadata_Node *node)
  916. {
  917. if(node->data->type == FLAC__METADATA_TYPE_PADDING && 0 != node->next && node->next->data->type == FLAC__METADATA_TYPE_PADDING) {
  918. const unsigned growth = FLAC__STREAM_METADATA_HEADER_LENGTH + node->next->data->length;
  919. node->data->length += growth;
  920. chain_delete_node_(chain, node->next);
  921. return true;
  922. }
  923. else
  924. return false;
  925. }
  926. /* Returns the new length of the chain, or 0 if there was an error. */
  927. /* WATCHOUT: This can get called multiple times before a write, so
  928. * it should still work when this happens.
  929. */
  930. /* WATCHOUT: Make sure to also update the logic in
  931. * FLAC__metadata_chain_check_if_tempfile_needed() if the logic here changes.
  932. */
  933. static off_t chain_prepare_for_write_(FLAC__Metadata_Chain *chain, FLAC__bool use_padding)
  934. {
  935. off_t current_length = chain_calculate_length_(chain);
  936. if(use_padding) {
  937. /* if the metadata shrank and the last block is padding, we just extend the last padding block */
  938. if(current_length < chain->initial_length && chain->tail->data->type == FLAC__METADATA_TYPE_PADDING) {
  939. const off_t delta = chain->initial_length - current_length;
  940. chain->tail->data->length += delta;
  941. current_length += delta;
  942. FLAC__ASSERT(current_length == chain->initial_length);
  943. }
  944. /* if the metadata shrank more than 4 bytes then there's room to add another padding block */
  945. else if(current_length + (off_t)FLAC__STREAM_METADATA_HEADER_LENGTH <= chain->initial_length) {
  946. FLAC__StreamMetadata *padding;
  947. FLAC__Metadata_Node *node;
  948. if(0 == (padding = FLAC__metadata_object_new(FLAC__METADATA_TYPE_PADDING))) {
  949. chain->status = FLAC__METADATA_CHAIN_STATUS_MEMORY_ALLOCATION_ERROR;
  950. return 0;
  951. }
  952. padding->length = chain->initial_length - (FLAC__STREAM_METADATA_HEADER_LENGTH + current_length);
  953. if(0 == (node = node_new_())) {
  954. FLAC__metadata_object_delete(padding);
  955. chain->status = FLAC__METADATA_CHAIN_STATUS_MEMORY_ALLOCATION_ERROR;
  956. return 0;
  957. }
  958. node->data = padding;
  959. chain_append_node_(chain, node);
  960. current_length = chain_calculate_length_(chain);
  961. FLAC__ASSERT(current_length == chain->initial_length);
  962. }
  963. /* if the metadata grew but the last block is padding, try cutting the padding to restore the original length so we don't have to rewrite the whole file */
  964. else if(current_length > chain->initial_length) {
  965. const off_t delta = current_length - chain->initial_length;
  966. if(chain->tail->data->type == FLAC__METADATA_TYPE_PADDING) {
  967. /* if the delta is exactly the size of the last padding block, remove the padding block */
  968. if((off_t)chain->tail->data->length + (off_t)FLAC__STREAM_METADATA_HEADER_LENGTH == delta) {
  969. chain_delete_node_(chain, chain->tail);
  970. current_length = chain_calculate_length_(chain);
  971. FLAC__ASSERT(current_length == chain->initial_length);
  972. }
  973. /* if there is at least 'delta' bytes of padding, trim the padding down */
  974. else if((off_t)chain->tail->data->length >= delta) {
  975. chain->tail->data->length -= delta;
  976. current_length -= delta;
  977. FLAC__ASSERT(current_length == chain->initial_length);
  978. }
  979. }
  980. }
  981. }
  982. return current_length;
  983. }
  984. static FLAC__bool chain_read_cb_(FLAC__Metadata_Chain *chain, FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__IOCallback_Seek seek_cb, FLAC__IOCallback_Tell tell_cb)
  985. {
  986. FLAC__Metadata_Node *node;
  987. FLAC__ASSERT(0 != chain);
  988. /* we assume we're already at the beginning of the file */
  989. switch(seek_to_first_metadata_block_cb_(handle, read_cb, seek_cb)) {
  990. case 0:
  991. break;
  992. case 1:
  993. chain->status = FLAC__METADATA_CHAIN_STATUS_READ_ERROR;
  994. return false;
  995. case 2:
  996. chain->status = FLAC__METADATA_CHAIN_STATUS_SEEK_ERROR;
  997. return false;
  998. case 3:
  999. chain->status = FLAC__METADATA_CHAIN_STATUS_NOT_A_FLAC_FILE;
  1000. return false;
  1001. default:
  1002. FLAC__ASSERT(0);
  1003. return false;
  1004. }
  1005. {
  1006. FLAC__int64 pos = tell_cb(handle);
  1007. if(pos < 0) {
  1008. chain->status = FLAC__METADATA_CHAIN_STATUS_READ_ERROR;
  1009. return false;
  1010. }
  1011. chain->first_offset = (off_t)pos;
  1012. }
  1013. {
  1014. FLAC__bool is_last;
  1015. FLAC__MetadataType type;
  1016. unsigned length;
  1017. do {
  1018. node = node_new_();
  1019. if(0 == node) {
  1020. chain->status = FLAC__METADATA_CHAIN_STATUS_MEMORY_ALLOCATION_ERROR;
  1021. return false;
  1022. }
  1023. if(!read_metadata_block_header_cb_(handle, read_cb, &is_last, &type, &length)) {
  1024. chain->status = FLAC__METADATA_CHAIN_STATUS_READ_ERROR;
  1025. return false;
  1026. }
  1027. node->data = FLAC__metadata_object_new(type);
  1028. if(0 == node->data) {
  1029. node_delete_(node);
  1030. chain->status = FLAC__METADATA_CHAIN_STATUS_MEMORY_ALLOCATION_ERROR;
  1031. return false;
  1032. }
  1033. node->data->is_last = is_last;
  1034. node->data->length = length;
  1035. chain->status = get_equivalent_status_(read_metadata_block_data_cb_(handle, read_cb, seek_cb, node->data));
  1036. if(chain->status != FLAC__METADATA_CHAIN_STATUS_OK) {
  1037. node_delete_(node);
  1038. return false;
  1039. }
  1040. chain_append_node_(chain, node);
  1041. } while(!is_last);
  1042. }
  1043. {
  1044. FLAC__int64 pos = tell_cb(handle);
  1045. if(pos < 0) {
  1046. chain->status = FLAC__METADATA_CHAIN_STATUS_READ_ERROR;
  1047. return false;
  1048. }
  1049. chain->last_offset = (off_t)pos;
  1050. }
  1051. chain->initial_length = chain_calculate_length_(chain);
  1052. return true;
  1053. }
  1054. static FLAC__StreamDecoderReadStatus chain_read_ogg_read_cb_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], size_t *bytes, void *client_data)
  1055. {
  1056. FLAC__Metadata_Chain *chain = (FLAC__Metadata_Chain*)client_data;
  1057. (void)decoder;
  1058. if(*bytes > 0 && chain->status == FLAC__METADATA_CHAIN_STATUS_OK) {
  1059. *bytes = chain->read_cb(buffer, sizeof(FLAC__byte), *bytes, chain->handle);
  1060. if(*bytes == 0)
  1061. return FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM;
  1062. else
  1063. return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
  1064. }
  1065. else
  1066. return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
  1067. }
  1068. static FLAC__StreamDecoderWriteStatus chain_read_ogg_write_cb_(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data)
  1069. {
  1070. (void)decoder, (void)frame, (void)buffer, (void)client_data;
  1071. return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
  1072. }
  1073. static void chain_read_ogg_metadata_cb_(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data)
  1074. {
  1075. FLAC__Metadata_Chain *chain = (FLAC__Metadata_Chain*)client_data;
  1076. FLAC__Metadata_Node *node;
  1077. (void)decoder;
  1078. node = node_new_();
  1079. if(0 == node) {
  1080. chain->status = FLAC__METADATA_CHAIN_STATUS_MEMORY_ALLOCATION_ERROR;
  1081. return;
  1082. }
  1083. node->data = FLAC__metadata_object_clone(metadata);
  1084. if(0 == node->data) {
  1085. node_delete_(node);
  1086. chain->status = FLAC__METADATA_CHAIN_STATUS_MEMORY_ALLOCATION_ERROR;
  1087. return;
  1088. }
  1089. chain_append_node_(chain, node);
  1090. }
  1091. static void chain_read_ogg_error_cb_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data)
  1092. {
  1093. FLAC__Metadata_Chain *chain = (FLAC__Metadata_Chain*)client_data;
  1094. (void)decoder, (void)status;
  1095. chain->status = FLAC__METADATA_CHAIN_STATUS_INTERNAL_ERROR; /*@@@ maybe needs better error code */
  1096. }
  1097. static FLAC__bool chain_read_ogg_cb_(FLAC__Metadata_Chain *chain, FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb)
  1098. {
  1099. FLAC__StreamDecoder *decoder;
  1100. FLAC__ASSERT(0 != chain);
  1101. /* we assume we're already at the beginning of the file */
  1102. chain->handle = handle;
  1103. chain->read_cb = read_cb;
  1104. if(0 == (decoder = FLAC__stream_decoder_new())) {
  1105. chain->status = FLAC__METADATA_CHAIN_STATUS_MEMORY_ALLOCATION_ERROR;
  1106. return false;
  1107. }
  1108. FLAC__stream_decoder_set_metadata_respond_all(decoder);
  1109. if(FLAC__stream_decoder_init_ogg_stream(decoder, chain_read_ogg_read_cb_, /*seek_callback=*/0, /*tell_callback=*/0, /*length_callback=*/0, /*eof_callback=*/0, chain_read_ogg_write_cb_, chain_read_ogg_metadata_cb_, chain_read_ogg_error_cb_, chain) != FLAC__STREAM_DECODER_INIT_STATUS_OK) {
  1110. FLAC__stream_decoder_delete(decoder);
  1111. chain->status = FLAC__METADATA_CHAIN_STATUS_INTERNAL_ERROR; /*@@@ maybe needs better error code */
  1112. return false;
  1113. }
  1114. chain->first_offset = 0; /*@@@ wrong; will need to be set correctly to implement metadata writing for Ogg FLAC */
  1115. if(!FLAC__stream_decoder_process_until_end_of_metadata(decoder))
  1116. chain->status = FLAC__METADATA_CHAIN_STATUS_INTERNAL_ERROR; /*@@@ maybe needs better error code */
  1117. if(chain->status != FLAC__METADATA_CHAIN_STATUS_OK) {
  1118. FLAC__stream_decoder_delete(decoder);
  1119. return false;
  1120. }
  1121. FLAC__stream_decoder_delete(decoder);
  1122. chain->last_offset = 0; /*@@@ wrong; will need to be set correctly to implement metadata writing for Ogg FLAC */
  1123. chain->initial_length = chain_calculate_length_(chain);
  1124. return true;
  1125. }
  1126. static FLAC__bool chain_rewrite_metadata_in_place_cb_(FLAC__Metadata_Chain *chain, FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, FLAC__IOCallback_Seek seek_cb)
  1127. {
  1128. FLAC__Metadata_Node *node;
  1129. FLAC__ASSERT(0 != chain);
  1130. FLAC__ASSERT(0 != chain->head);
  1131. if(0 != seek_cb(handle, chain->first_offset, SEEK_SET)) {
  1132. chain->status = FLAC__METADATA_CHAIN_STATUS_SEEK_ERROR;
  1133. return false;
  1134. }
  1135. for(node = chain->head; node; node = node->next) {
  1136. if(!write_metadata_block_header_cb_(handle, write_cb, node->data)) {
  1137. chain->status = FLAC__METADATA_CHAIN_STATUS_WRITE_ERROR;
  1138. return false;
  1139. }
  1140. if(!write_metadata_block_data_cb_(handle, write_cb, node->data)) {
  1141. chain->status = FLAC__METADATA_CHAIN_STATUS_WRITE_ERROR;
  1142. return false;
  1143. }
  1144. }
  1145. /*FLAC__ASSERT(fflush(), ftello() == chain->last_offset);*/
  1146. chain->status = FLAC__METADATA_CHAIN_STATUS_OK;
  1147. return true;
  1148. }
  1149. static FLAC__bool chain_rewrite_metadata_in_place_(FLAC__Metadata_Chain *chain)
  1150. {
  1151. FILE *file;
  1152. FLAC__bool ret;
  1153. FLAC__ASSERT(0 != chain->filename);
  1154. if(0 == (file = fopen(chain->filename, "r+b"))) {
  1155. chain->status = FLAC__METADATA_CHAIN_STATUS_ERROR_OPENING_FILE;
  1156. return false;
  1157. }
  1158. /* chain_rewrite_metadata_in_place_cb_() sets chain->status for us */
  1159. ret = chain_rewrite_metadata_in_place_cb_(chain, (FLAC__IOHandle)file, (FLAC__IOCallback_Write)fwrite, fseek_wrapper_);
  1160. fclose(file);
  1161. return ret;
  1162. }
  1163. static FLAC__bool chain_rewrite_file_(FLAC__Metadata_Chain *chain, const char *tempfile_path_prefix)
  1164. {
  1165. FILE *f, *tempfile;
  1166. char *tempfilename;
  1167. FLAC__Metadata_SimpleIteratorStatus status;
  1168. const FLAC__Metadata_Node *node;
  1169. FLAC__ASSERT(0 != chain);
  1170. FLAC__ASSERT(0 != chain->filename);
  1171. FLAC__ASSERT(0 != chain->head);
  1172. /* copy the file prefix (data up to first metadata block */
  1173. if(0 == (f = fopen(chain->filename, "rb"))) {
  1174. chain->status = FLAC__METADATA_CHAIN_STATUS_ERROR_OPENING_FILE;
  1175. return false;
  1176. }
  1177. if(!open_tempfile_(chain->filename, tempfile_path_prefix, &tempfile, &tempfilename, &status)) {
  1178. chain->status = get_equivalent_status_(status);
  1179. cleanup_tempfile_(&tempfile, &tempfilename);
  1180. return false;
  1181. }
  1182. if(!copy_n_bytes_from_file_(f, tempfile, chain->first_offset, &status)) {
  1183. chain->status = get_equivalent_status_(status);
  1184. cleanup_tempfile_(&tempfile, &tempfilename);
  1185. return false;
  1186. }
  1187. /* write the metadata */
  1188. for(node = chain->head; node; node = node->next) {
  1189. if(!write_metadata_block_header_(tempfile, &status, node->data)) {
  1190. chain->status = get_equivalent_status_(status);
  1191. return false;
  1192. }
  1193. if(!write_metadata_block_data_(tempfile, &status, node->data)) {
  1194. chain->status = get_equivalent_status_(status);
  1195. return false;
  1196. }
  1197. }
  1198. /*FLAC__ASSERT(fflush(), ftello() == chain->last_offset);*/
  1199. /* copy the file postfix (everything after the metadata) */
  1200. if(0 != fseeko(f, chain->last_offset, SEEK_SET)) {
  1201. cleanup_tempfile_(&tempfile, &tempfilename);
  1202. chain->status = FLAC__METADATA_CHAIN_STATUS_SEEK_ERROR;
  1203. return false;
  1204. }
  1205. if(!copy_remaining_bytes_from_file_(f, tempfile, &status)) {
  1206. cleanup_tempfile_(&tempfile, &tempfilename);
  1207. chain->status = get_equivalent_status_(status);
  1208. return false;
  1209. }
  1210. /* move the tempfile on top of the original */
  1211. (void)fclose(f);
  1212. if(!transport_tempfile_(chain->filename, &tempfile, &tempfilename, &status))
  1213. return false;
  1214. return true;
  1215. }
  1216. /* assumes 'handle' is already at beginning of file */
  1217. static FLAC__bool chain_rewrite_file_cb_(FLAC__Metadata_Chain *chain, FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__IOCallback_Seek seek_cb, FLAC__IOCallback_Eof eof_cb, FLAC__IOHandle temp_handle, FLAC__IOCallback_Write temp_write_cb)
  1218. {
  1219. FLAC__Metadata_SimpleIteratorStatus status;
  1220. const FLAC__Metadata_Node *node;
  1221. FLAC__ASSERT(0 != chain);
  1222. FLAC__ASSERT(0 == chain->filename);
  1223. FLAC__ASSERT(0 != chain->head);
  1224. /* copy the file prefix (data up to first metadata block */
  1225. if(!copy_n_bytes_from_file_cb_(handle, read_cb, temp_handle, temp_write_cb, chain->first_offset, &status)) {
  1226. chain->status = get_equivalent_status_(status);
  1227. return false;
  1228. }
  1229. /* write the metadata */
  1230. for(node = chain->head; node; node = node->next) {
  1231. if(!write_metadata_block_header_cb_(temp_handle, temp_write_cb, node->data)) {
  1232. chain->status = FLAC__METADATA_CHAIN_STATUS_WRITE_ERROR;
  1233. return false;
  1234. }
  1235. if(!write_metadata_block_data_cb_(temp_handle, temp_write_cb, node->data)) {
  1236. chain->status = FLAC__METADATA_CHAIN_STATUS_WRITE_ERROR;
  1237. return false;
  1238. }
  1239. }
  1240. /*FLAC__ASSERT(fflush(), ftello() == chain->last_offset);*/
  1241. /* copy the file postfix (everything after the metadata) */
  1242. if(0 != seek_cb(handle, chain->last_offset, SEEK_SET)) {
  1243. chain->status = FLAC__METADATA_CHAIN_STATUS_SEEK_ERROR;
  1244. return false;
  1245. }
  1246. if(!copy_remaining_bytes_from_file_cb_(handle, read_cb, eof_cb, temp_handle, temp_write_cb, &status)) {
  1247. chain->status = get_equivalent_status_(status);
  1248. return false;
  1249. }
  1250. return true;
  1251. }
  1252. FLAC_API FLAC__Metadata_Chain *FLAC__metadata_chain_new(void)
  1253. {
  1254. FLAC__Metadata_Chain *chain = (FLAC__Metadata_Chain*)calloc(1, sizeof(FLAC__Metadata_Chain));
  1255. if(0 != chain)
  1256. chain_init_(chain);
  1257. return chain;
  1258. }
  1259. FLAC_API void FLAC__metadata_chain_delete(FLAC__Metadata_Chain *chain)
  1260. {
  1261. FLAC__ASSERT(0 != chain);
  1262. chain_clear_(chain);
  1263. free(chain);
  1264. }
  1265. FLAC_API FLAC__Metadata_ChainStatus FLAC__metadata_chain_status(FLAC__Metadata_Chain *chain)
  1266. {
  1267. FLAC__Metadata_ChainStatus status;
  1268. FLAC__ASSERT(0 != chain);
  1269. status = chain->status;
  1270. chain->status = FLAC__METADATA_CHAIN_STATUS_OK;
  1271. return status;
  1272. }
  1273. static FLAC__bool chain_read_(FLAC__Metadata_Chain *chain, const char *filename, FLAC__bool is_ogg)
  1274. {
  1275. FILE *file;
  1276. FLAC__bool ret;
  1277. FLAC__ASSERT(0 != chain);
  1278. FLAC__ASSERT(0 != filename);
  1279. chain_clear_(chain);
  1280. if(0 == (chain->filename = strdup(filename))) {
  1281. chain->status = FLAC__METADATA_CHAIN_STATUS_MEMORY_ALLOCATION_ERROR;
  1282. return false;
  1283. }
  1284. chain->is_ogg = is_ogg;
  1285. if(0 == (file = fopen(filename, "rb"))) {
  1286. chain->status = FLAC__METADATA_CHAIN_STATUS_ERROR_OPENING_FILE;
  1287. return false;
  1288. }
  1289. /* the function also sets chain->status for us */
  1290. ret = is_ogg?
  1291. chain_read_ogg_cb_(chain, file, (FLAC__IOCallback_Read)fread) :
  1292. chain_read_cb_(chain, file, (FLAC__IOCallback_Read)fread, fseek_wrapper_, ftell_wrapper_)
  1293. ;
  1294. fclose(file);
  1295. return ret;
  1296. }
  1297. FLAC_API FLAC__bool FLAC__metadata_chain_read(FLAC__Metadata_Chain *chain, const char *filename)
  1298. {
  1299. return chain_read_(chain, filename, /*is_ogg=*/false);
  1300. }
  1301. /*@@@@add to tests*/
  1302. FLAC_API FLAC__bool FLAC__metadata_chain_read_ogg(FLAC__Metadata_Chain *chain, const char *filename)
  1303. {
  1304. return chain_read_(chain, filename, /*is_ogg=*/true);
  1305. }
  1306. static FLAC__bool chain_read_with_callbacks_(FLAC__Metadata_Chain *chain, FLAC__IOHandle handle, FLAC__IOCallbacks callbacks, FLAC__bool is_ogg)
  1307. {
  1308. FLAC__bool ret;
  1309. FLAC__ASSERT(0 != chain);
  1310. chain_clear_(chain);
  1311. if (0 == callbacks.read || 0 == callbacks.seek || 0 == callbacks.tell) {
  1312. chain->status = FLAC__METADATA_CHAIN_STATUS_INVALID_CALLBACKS;
  1313. return false;
  1314. }
  1315. chain->is_ogg = is_ogg;
  1316. /* rewind */
  1317. if(0 != callbacks.seek(handle, 0, SEEK_SET)) {
  1318. chain->status = FLAC__METADATA_CHAIN_STATUS_SEEK_ERROR;
  1319. return false;
  1320. }
  1321. /* the function also sets chain->status for us */
  1322. ret = is_ogg?
  1323. chain_read_ogg_cb_(chain, handle, callbacks.read) :
  1324. chain_read_cb_(chain, handle, callbacks.read, callbacks.seek, callbacks.tell)
  1325. ;
  1326. return ret;
  1327. }
  1328. FLAC_API FLAC__bool FLAC__metadata_chain_read_with_callbacks(FLAC__Metadata_Chain *chain, FLAC__IOHandle handle, FLAC__IOCallbacks callbacks)
  1329. {
  1330. return chain_read_with_callbacks_(chain, handle, callbacks, /*is_ogg=*/false);
  1331. }
  1332. /*@@@@add to tests*/
  1333. FLAC_API FLAC__bool FLAC__metadata_chain_read_ogg_with_callbacks(FLAC__Metadata_Chain *chain, FLAC__IOHandle handle, FLAC__IOCallbacks callbacks)
  1334. {
  1335. return chain_read_with_callbacks_(chain, handle, callbacks, /*is_ogg=*/true);
  1336. }
  1337. FLAC_API FLAC__bool FLAC__metadata_chain_check_if_tempfile_needed(FLAC__Metadata_Chain *chain, FLAC__bool use_padding)
  1338. {
  1339. /* This does all the same checks that are in chain_prepare_for_write_()
  1340. * but doesn't actually alter the chain. Make sure to update the logic
  1341. * here if chain_prepare_for_write_() changes.
  1342. */
  1343. const off_t current_length = chain_calculate_length_(chain);
  1344. FLAC__ASSERT(0 != chain);
  1345. if(use_padding) {
  1346. /* if the metadata shrank and the last block is padding, we just extend the last padding block */
  1347. if(current_length < chain->initial_length && chain->tail->data->type == FLAC__METADATA_TYPE_PADDING)
  1348. return false;
  1349. /* if the metadata shrank more than 4 bytes then there's room to add another padding block */
  1350. else if(current_length + (off_t)FLAC__STREAM_METADATA_HEADER_LENGTH <= chain->initial_length)
  1351. return false;
  1352. /* if the metadata grew but the last block is padding, try cutting the padding to restore the original length so we don't have to rewrite the whole file */
  1353. else if(current_length > chain->initial_length) {
  1354. const off_t delta = current_length - chain->initial_length;
  1355. if(chain->tail->data->type == FLAC__METADATA_TYPE_PADDING) {
  1356. /* if the delta is exactly the size of the last padding block, remove the padding block */
  1357. if((off_t)chain->tail->data->length + (off_t)FLAC__STREAM_METADATA_HEADER_LENGTH == delta)
  1358. return false;
  1359. /* if there is at least 'delta' bytes of padding, trim the padding down */
  1360. else if((off_t)chain->tail->data->length >= delta)
  1361. return false;
  1362. }
  1363. }
  1364. }
  1365. return (current_length != chain->initial_length);
  1366. }
  1367. FLAC_API FLAC__bool FLAC__metadata_chain_write(FLAC__Metadata_Chain *chain, FLAC__bool use_padding, FLAC__bool preserve_file_stats)
  1368. {
  1369. struct stat stats;
  1370. const char *tempfile_path_prefix = 0;
  1371. off_t current_length;
  1372. FLAC__ASSERT(0 != chain);
  1373. if (chain->is_ogg) { /* cannot write back to Ogg FLAC yet */
  1374. chain->status = FLAC__METADATA_CHAIN_STATUS_INTERNAL_ERROR;
  1375. return false;
  1376. }
  1377. if (0 == chain->filename) {
  1378. chain->status = FLAC__METADATA_CHAIN_STATUS_READ_WRITE_MISMATCH;
  1379. return false;
  1380. }
  1381. current_length = chain_prepare_for_write_(chain, use_padding);
  1382. /* a return value of 0 means there was an error; chain->status is already set */
  1383. if (0 == current_length)
  1384. return false;
  1385. if(preserve_file_stats)
  1386. get_file_stats_(chain->filename, &stats);
  1387. if(current_length == chain->initial_length) {
  1388. if(!chain_rewrite_metadata_in_place_(chain))
  1389. return false;
  1390. }
  1391. else {
  1392. if(!chain_rewrite_file_(chain, tempfile_path_prefix))
  1393. return false;
  1394. /* recompute lengths and offsets */
  1395. {
  1396. const FLAC__Metadata_Node *node;
  1397. chain->initial_length = current_length;
  1398. chain->last_offset = chain->first_offset;
  1399. for(node = chain->head; node; node = node->next)
  1400. chain->last_offset += (FLAC__STREAM_METADATA_HEADER_LENGTH + node->data->length);
  1401. }
  1402. }
  1403. if(preserve_file_stats)
  1404. set_file_stats_(chain->filename, &stats);
  1405. return true;
  1406. }
  1407. FLAC_API FLAC__bool FLAC__metadata_chain_write_with_callbacks(FLAC__Metadata_Chain *chain, FLAC__bool use_padding, FLAC__IOHandle handle, FLAC__IOCallbacks callbacks)
  1408. {
  1409. off_t current_length;
  1410. FLAC__ASSERT(0 != chain);
  1411. if (chain->is_ogg) { /* cannot write back to Ogg FLAC yet */
  1412. chain->status = FLAC__METADATA_CHAIN_STATUS_INTERNAL_ERROR;
  1413. return false;
  1414. }
  1415. if (0 != chain->filename) {
  1416. chain->status = FLAC__METADATA_CHAIN_STATUS_READ_WRITE_MISMATCH;
  1417. return false;
  1418. }
  1419. if (0 == callbacks.write || 0 == callbacks.seek) {
  1420. chain->status = FLAC__METADATA_CHAIN_STATUS_INVALID_CALLBACKS;
  1421. return false;
  1422. }
  1423. if (FLAC__metadata_chain_check_if_tempfile_needed(chain, use_padding)) {
  1424. chain->status = FLAC__METADATA_CHAIN_STATUS_WRONG_WRITE_CALL;
  1425. return false;
  1426. }
  1427. current_length = chain_prepare_for_write_(chain, use_padding);
  1428. /* a return value of 0 means there was an error; chain->status is already set */
  1429. if (0 == current_length)
  1430. return false;
  1431. FLAC__ASSERT(current_length == chain->initial_length);
  1432. return chain_rewrite_metadata_in_place_cb_(chain, handle, callbacks.write, callbacks.seek);
  1433. }
  1434. FLAC_API FLAC__bool FLAC__metadata_chain_write_with_callbacks_and_tempfile(FLAC__Metadata_Chain *chain, FLAC__bool use_padding, FLAC__IOHandle handle, FLAC__IOCallbacks callbacks, FLAC__IOHandle temp_handle, FLAC__IOCallbacks temp_callbacks)
  1435. {
  1436. off_t current_length;
  1437. FLAC__ASSERT(0 != chain);
  1438. if (chain->is_ogg) { /* cannot write back to Ogg FLAC yet */
  1439. chain->status = FLAC__METADATA_CHAIN_STATUS_INTERNAL_ERROR;
  1440. return false;
  1441. }
  1442. if (0 != chain->filename) {
  1443. chain->status = FLAC__METADATA_CHAIN_STATUS_READ_WRITE_MISMATCH;
  1444. return false;
  1445. }
  1446. if (0 == callbacks.read || 0 == callbacks.seek || 0 == callbacks.eof) {
  1447. chain->status = FLAC__METADATA_CHAIN_STATUS_INVALID_CALLBACKS;
  1448. return false;
  1449. }
  1450. if (0 == temp_callbacks.write) {
  1451. chain->status = FLAC__METADATA_CHAIN_STATUS_INVALID_CALLBACKS;
  1452. return false;
  1453. }
  1454. if (!FLAC__metadata_chain_check_if_tempfile_needed(chain, use_padding)) {
  1455. chain->status = FLAC__METADATA_CHAIN_STATUS_WRONG_WRITE_CALL;
  1456. return false;
  1457. }
  1458. current_length = chain_prepare_for_write_(chain, use_padding);
  1459. /* a return value of 0 means there was an error; chain->status is already set */
  1460. if (0 == current_length)
  1461. return false;
  1462. FLAC__ASSERT(current_length != chain->initial_length);
  1463. /* rewind */
  1464. if(0 != callbacks.seek(handle, 0, SEEK_SET)) {
  1465. chain->status = FLAC__METADATA_CHAIN_STATUS_SEEK_ERROR;
  1466. return false;
  1467. }
  1468. if(!chain_rewrite_file_cb_(chain, handle, callbacks.read, callbacks.seek, callbacks.eof, temp_handle, temp_callbacks.write))
  1469. return false;
  1470. /* recompute lengths and offsets */
  1471. {
  1472. const FLAC__Metadata_Node *node;
  1473. chain->initial_length = current_length;
  1474. chain->last_offset = chain->first_offset;
  1475. for(node = chain->head; node; node = node->next)
  1476. chain->last_offset += (FLAC__STREAM_METADATA_HEADER_LENGTH + node->data->length);
  1477. }
  1478. return true;
  1479. }
  1480. FLAC_API void FLAC__metadata_chain_merge_padding(FLAC__Metadata_Chain *chain)
  1481. {
  1482. FLAC__Metadata_Node *node;
  1483. FLAC__ASSERT(0 != chain);
  1484. for(node = chain->head; node; ) {
  1485. if(!chain_merge_adjacent_padding_(chain, node))
  1486. node = node->next;
  1487. }
  1488. }
  1489. FLAC_API void FLAC__metadata_chain_sort_padding(FLAC__Metadata_Chain *chain)
  1490. {
  1491. FLAC__Metadata_Node *node, *save;
  1492. unsigned i;
  1493. FLAC__ASSERT(0 != chain);
  1494. /*
  1495. * Don't try and be too smart... this simple algo is good enough for
  1496. * the small number of nodes that we deal with.
  1497. */
  1498. for(i = 0, node = chain->head; i < chain->nodes; i++) {
  1499. if(node->data->type == FLAC__METADATA_TYPE_PADDING) {
  1500. save = node->next;
  1501. chain_remove_node_(chain, node);
  1502. chain_append_node_(chain, node);
  1503. node = save;
  1504. }
  1505. else {
  1506. node = node->next;
  1507. }
  1508. }
  1509. FLAC__metadata_chain_merge_padding(chain);
  1510. }
  1511. FLAC_API FLAC__Metadata_Iterator *FLAC__metadata_iterator_new(void)
  1512. {
  1513. FLAC__Metadata_Iterator *iterator = (FLAC__Metadata_Iterator*)calloc(1, sizeof(FLAC__Metadata_Iterator));
  1514. /* calloc() implies:
  1515. iterator->current = 0;
  1516. iterator->chain = 0;
  1517. */
  1518. return iterator;
  1519. }
  1520. FLAC_API void FLAC__metadata_iterator_delete(FLAC__Metadata_Iterator *iterator)
  1521. {
  1522. FLAC__ASSERT(0 != iterator);
  1523. free(iterator);
  1524. }
  1525. FLAC_API void FLAC__metadata_iterator_init(FLAC__Metadata_Iterator *iterator, FLAC__Metadata_Chain *chain)
  1526. {
  1527. FLAC__ASSERT(0 != iterator);
  1528. FLAC__ASSERT(0 != chain);
  1529. FLAC__ASSERT(0 != chain->head);
  1530. iterator->chain = chain;
  1531. iterator->current = chain->head;
  1532. }
  1533. FLAC_API FLAC__bool FLAC__metadata_iterator_next(FLAC__Metadata_Iterator *iterator)
  1534. {
  1535. FLAC__ASSERT(0 != iterator);
  1536. if(0 == iterator->current || 0 == iterator->current->next)
  1537. return false;
  1538. iterator->current = iterator->current->next;
  1539. return true;
  1540. }
  1541. FLAC_API FLAC__bool FLAC__metadata_iterator_prev(FLAC__Metadata_Iterator *iterator)
  1542. {
  1543. FLAC__ASSERT(0 != iterator);
  1544. if(0 == iterator->current || 0 == iterator->current->prev)
  1545. return false;
  1546. iterator->current = iterator->current->prev;
  1547. return true;
  1548. }
  1549. FLAC_API FLAC__MetadataType FLAC__metadata_iterator_get_block_type(const FLAC__Metadata_Iterator *iterator)
  1550. {
  1551. FLAC__ASSERT(0 != iterator);
  1552. FLAC__ASSERT(0 != iterator->current);
  1553. FLAC__ASSERT(0 != iterator->current->data);
  1554. return iterator->current->data->type;
  1555. }
  1556. FLAC_API FLAC__StreamMetadata *FLAC__metadata_iterator_get_block(FLAC__Metadata_Iterator *iterator)
  1557. {
  1558. FLAC__ASSERT(0 != iterator);
  1559. FLAC__ASSERT(0 != iterator->current);
  1560. return iterator->current->data;
  1561. }
  1562. FLAC_API FLAC__bool FLAC__metadata_iterator_set_block(FLAC__Metadata_Iterator *iterator, FLAC__StreamMetadata *block)
  1563. {
  1564. FLAC__ASSERT(0 != iterator);
  1565. FLAC__ASSERT(0 != block);
  1566. return FLAC__metadata_iterator_delete_block(iterator, false) && FLAC__metadata_iterator_insert_block_after(iterator, block);
  1567. }
  1568. FLAC_API FLAC__bool FLAC__metadata_iterator_delete_block(FLAC__Metadata_Iterator *iterator, FLAC__bool replace_with_padding)
  1569. {
  1570. FLAC__Metadata_Node *save;
  1571. FLAC__ASSERT(0 != iterator);
  1572. FLAC__ASSERT(0 != iterator->current);
  1573. if(0 == iterator->current->prev) {
  1574. FLAC__ASSERT(iterator->current->data->type == FLAC__METADATA_TYPE_STREAMINFO);
  1575. return false;
  1576. }
  1577. save = iterator->current->prev;
  1578. if(replace_with_padding) {
  1579. FLAC__metadata_object_delete_data(iterator->current->data);
  1580. iterator->current->data->type = FLAC__METADATA_TYPE_PADDING;
  1581. }
  1582. else {
  1583. chain_delete_node_(iterator->chain, iterator->current);
  1584. }
  1585. iterator->current = save;
  1586. return true;
  1587. }
  1588. FLAC_API FLAC__bool FLAC__metadata_iterator_insert_block_before(FLAC__Metadata_Iterator *iterator, FLAC__StreamMetadata *block)
  1589. {
  1590. FLAC__Metadata_Node *node;
  1591. FLAC__ASSERT(0 != iterator);
  1592. FLAC__ASSERT(0 != iterator->current);
  1593. FLAC__ASSERT(0 != block);
  1594. if(block->type == FLAC__METADATA_TYPE_STREAMINFO)
  1595. return false;
  1596. if(0 == iterator->current->prev) {
  1597. FLAC__ASSERT(iterator->current->data->type == FLAC__METADATA_TYPE_STREAMINFO);
  1598. return false;
  1599. }
  1600. if(0 == (node = node_new_()))
  1601. return false;
  1602. node->data = block;
  1603. iterator_insert_node_(iterator, node);
  1604. iterator->current = node;
  1605. return true;
  1606. }
  1607. FLAC_API FLAC__bool FLAC__metadata_iterator_insert_block_after(FLAC__Metadata_Iterator *iterator, FLAC__StreamMetadata *block)
  1608. {
  1609. FLAC__Metadata_Node *node;
  1610. FLAC__ASSERT(0 != iterator);
  1611. FLAC__ASSERT(0 != iterator->current);
  1612. FLAC__ASSERT(0 != block);
  1613. if(block->type == FLAC__METADATA_TYPE_STREAMINFO)
  1614. return false;
  1615. if(0 == (node = node_new_()))
  1616. return false;
  1617. node->data = block;
  1618. iterator_insert_node_after_(iterator, node);
  1619. iterator->current = node;
  1620. return true;
  1621. }
  1622. /****************************************************************************
  1623. *
  1624. * Local function definitions
  1625. *
  1626. ***************************************************************************/
  1627. void pack_uint32_(FLAC__uint32 val, FLAC__byte *b, unsigned bytes)
  1628. {
  1629. unsigned i;
  1630. b += bytes;
  1631. for(i = 0; i < bytes; i++) {
  1632. *(--b) = (FLAC__byte)(val & 0xff);
  1633. val >>= 8;
  1634. }
  1635. }
  1636. void pack_uint32_little_endian_(FLAC__uint32 val, FLAC__byte *b, unsigned bytes)
  1637. {
  1638. unsigned i;
  1639. for(i = 0; i < bytes; i++) {
  1640. *(b++) = (FLAC__byte)(val & 0xff);
  1641. val >>= 8;
  1642. }
  1643. }
  1644. void pack_uint64_(FLAC__uint64 val, FLAC__byte *b, unsigned bytes)
  1645. {
  1646. unsigned i;
  1647. b += bytes;
  1648. for(i = 0; i < bytes; i++) {
  1649. *(--b) = (FLAC__byte)(val & 0xff);
  1650. val >>= 8;
  1651. }
  1652. }
  1653. FLAC__uint32 unpack_uint32_(FLAC__byte *b, unsigned bytes)
  1654. {
  1655. FLAC__uint32 ret = 0;
  1656. unsigned i;
  1657. for(i = 0; i < bytes; i++)
  1658. ret = (ret << 8) | (FLAC__uint32)(*b++);
  1659. return ret;
  1660. }
  1661. FLAC__uint32 unpack_uint32_little_endian_(FLAC__byte *b, unsigned bytes)
  1662. {
  1663. FLAC__uint32 ret = 0;
  1664. unsigned i;
  1665. b += bytes;
  1666. for(i = 0; i < bytes; i++)
  1667. ret = (ret << 8) | (FLAC__uint32)(*--b);
  1668. return ret;
  1669. }
  1670. FLAC__uint64 unpack_uint64_(FLAC__byte *b, unsigned bytes)
  1671. {
  1672. FLAC__uint64 ret = 0;
  1673. unsigned i;
  1674. for(i = 0; i < bytes; i++)
  1675. ret = (ret << 8) | (FLAC__uint64)(*b++);
  1676. return ret;
  1677. }
  1678. FLAC__bool read_metadata_block_header_(FLAC__Metadata_SimpleIterator *iterator)
  1679. {
  1680. FLAC__ASSERT(0 != iterator);
  1681. FLAC__ASSERT(0 != iterator->file);
  1682. if(!read_metadata_block_header_cb_((FLAC__IOHandle)iterator->file, (FLAC__IOCallback_Read)fread, &iterator->is_last, &iterator->type, &iterator->length)) {
  1683. iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
  1684. return false;
  1685. }
  1686. return true;
  1687. }
  1688. FLAC__bool read_metadata_block_data_(FLAC__Metadata_SimpleIterator *iterator, FLAC__StreamMetadata *block)
  1689. {
  1690. FLAC__ASSERT(0 != iterator);
  1691. FLAC__ASSERT(0 != iterator->file);
  1692. iterator->status = read_metadata_block_data_cb_((FLAC__IOHandle)iterator->file, (FLAC__IOCallback_Read)fread, fseek_wrapper_, block);
  1693. return (iterator->status == FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK);
  1694. }
  1695. FLAC__bool read_metadata_block_header_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__bool *is_last, FLAC__MetadataType *type, unsigned *length)
  1696. {
  1697. FLAC__byte raw_header[FLAC__STREAM_METADATA_HEADER_LENGTH];
  1698. if(read_cb(raw_header, 1, FLAC__STREAM_METADATA_HEADER_LENGTH, handle) != FLAC__STREAM_METADATA_HEADER_LENGTH)
  1699. return false;
  1700. *is_last = raw_header[0] & 0x80? true : false;
  1701. *type = (FLAC__MetadataType)(raw_header[0] & 0x7f);
  1702. *length = unpack_uint32_(raw_header + 1, 3);
  1703. /* Note that we don't check:
  1704. * if(iterator->type >= FLAC__METADATA_TYPE_UNDEFINED)
  1705. * we just will read in an opaque block
  1706. */
  1707. return true;
  1708. }
  1709. FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__IOCallback_Seek seek_cb, FLAC__StreamMetadata *block)
  1710. {
  1711. switch(block->type) {
  1712. case FLAC__METADATA_TYPE_STREAMINFO:
  1713. return read_metadata_block_data_streaminfo_cb_(handle, read_cb, &block->data.stream_info);
  1714. case FLAC__METADATA_TYPE_PADDING:
  1715. return read_metadata_block_data_padding_cb_(handle, seek_cb, &block->data.padding, block->length);
  1716. case FLAC__METADATA_TYPE_APPLICATION:
  1717. return read_metadata_block_data_application_cb_(handle, read_cb, &block->data.application, block->length);
  1718. case FLAC__METADATA_TYPE_SEEKTABLE:
  1719. return read_metadata_block_data_seektable_cb_(handle, read_cb, &block->data.seek_table, block->length);
  1720. case FLAC__METADATA_TYPE_VORBIS_COMMENT:
  1721. return read_metadata_block_data_vorbis_comment_cb_(handle, read_cb, &block->data.vorbis_comment);
  1722. case FLAC__METADATA_TYPE_CUESHEET:
  1723. return read_metadata_block_data_cuesheet_cb_(handle, read_cb, &block->data.cue_sheet);
  1724. case FLAC__METADATA_TYPE_PICTURE:
  1725. return read_metadata_block_data_picture_cb_(handle, read_cb, &block->data.picture);
  1726. default:
  1727. return read_metadata_block_data_unknown_cb_(handle, read_cb, &block->data.unknown, block->length);
  1728. }
  1729. }
  1730. FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_streaminfo_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__StreamMetadata_StreamInfo *block)
  1731. {
  1732. FLAC__byte buffer[FLAC__STREAM_METADATA_STREAMINFO_LENGTH], *b;
  1733. if(read_cb(buffer, 1, FLAC__STREAM_METADATA_STREAMINFO_LENGTH, handle) != FLAC__STREAM_METADATA_STREAMINFO_LENGTH)
  1734. return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
  1735. b = buffer;
  1736. /* we are using hardcoded numbers for simplicity but we should
  1737. * probably eventually write a bit-level unpacker and use the
  1738. * _STREAMINFO_ constants.
  1739. */
  1740. block->min_blocksize = unpack_uint32_(b, 2); b += 2;
  1741. block->max_blocksize = unpack_uint32_(b, 2); b += 2;
  1742. block->min_framesize = unpack_uint32_(b, 3); b += 3;
  1743. block->max_framesize = unpack_uint32_(b, 3); b += 3;
  1744. block->sample_rate = (unpack_uint32_(b, 2) << 4) | ((unsigned)(b[2] & 0xf0) >> 4);
  1745. block->channels = (unsigned)((b[2] & 0x0e) >> 1) + 1;
  1746. block->bits_per_sample = ((((unsigned)(b[2] & 0x01)) << 4) | (((unsigned)(b[3] & 0xf0)) >> 4)) + 1;
  1747. block->total_samples = (((FLAC__uint64)(b[3] & 0x0f)) << 32) | unpack_uint64_(b+4, 4);
  1748. memcpy(block->md5sum, b+8, 16);
  1749. return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK;
  1750. }
  1751. FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_padding_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Seek seek_cb, FLAC__StreamMetadata_Padding *block, unsigned block_length)
  1752. {
  1753. (void)block; /* nothing to do; we don't care about reading the padding bytes */
  1754. if(0 != seek_cb(handle, block_length, SEEK_CUR))
  1755. return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR;
  1756. return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK;
  1757. }
  1758. FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_application_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__StreamMetadata_Application *block, unsigned block_length)
  1759. {
  1760. const unsigned id_bytes = FLAC__STREAM_METADATA_APPLICATION_ID_LEN / 8;
  1761. if(read_cb(block->id, 1, id_bytes, handle) != id_bytes)
  1762. return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
  1763. if(block_length < id_bytes)
  1764. return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
  1765. block_length -= id_bytes;
  1766. if(block_length == 0) {
  1767. block->data = 0;
  1768. }
  1769. else {
  1770. if(0 == (block->data = (FLAC__byte*)malloc(block_length)))
  1771. return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR;
  1772. if(read_cb(block->data, 1, block_length, handle) != block_length)
  1773. return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
  1774. }
  1775. return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK;
  1776. }
  1777. FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_seektable_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__StreamMetadata_SeekTable *block, unsigned block_length)
  1778. {
  1779. unsigned i;
  1780. FLAC__byte buffer[FLAC__STREAM_METADATA_SEEKPOINT_LENGTH];
  1781. FLAC__ASSERT(block_length % FLAC__STREAM_METADATA_SEEKPOINT_LENGTH == 0);
  1782. block->num_points = block_length / FLAC__STREAM_METADATA_SEEKPOINT_LENGTH;
  1783. if(block->num_points == 0)
  1784. block->points = 0;
  1785. else if(0 == (block->points = (FLAC__StreamMetadata_SeekPoint*)safe_malloc_mul_2op_(block->num_points, /*times*/sizeof(FLAC__StreamMetadata_SeekPoint))))
  1786. return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR;
  1787. for(i = 0; i < block->num_points; i++) {
  1788. if(read_cb(buffer, 1, FLAC__STREAM_METADATA_SEEKPOINT_LENGTH, handle) != FLAC__STREAM_METADATA_SEEKPOINT_LENGTH)
  1789. return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
  1790. /* some MAGIC NUMBERs here */
  1791. block->points[i].sample_number = unpack_uint64_(buffer, 8);
  1792. block->points[i].stream_offset = unpack_uint64_(buffer+8, 8);
  1793. block->points[i].frame_samples = unpack_uint32_(buffer+16, 2);
  1794. }
  1795. return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK;
  1796. }
  1797. FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_vorbis_comment_entry_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__StreamMetadata_VorbisComment_Entry *entry)
  1798. {
  1799. const unsigned entry_length_len = FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN / 8;
  1800. FLAC__byte buffer[4]; /* magic number is asserted below */
  1801. FLAC__ASSERT(FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN / 8 == sizeof(buffer));
  1802. if(read_cb(buffer, 1, entry_length_len, handle) != entry_length_len)
  1803. return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
  1804. entry->length = unpack_uint32_little_endian_(buffer, entry_length_len);
  1805. if(0 != entry->entry)
  1806. free(entry->entry);
  1807. if(entry->length == 0) {
  1808. entry->entry = 0;
  1809. }
  1810. else {
  1811. if(0 == (entry->entry = (FLAC__byte*)safe_malloc_add_2op_(entry->length, /*+*/1)))
  1812. return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR;
  1813. if(read_cb(entry->entry, 1, entry->length, handle) != entry->length)
  1814. return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
  1815. entry->entry[entry->length] = '\0';
  1816. }
  1817. return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK;
  1818. }
  1819. FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_vorbis_comment_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__StreamMetadata_VorbisComment *block)
  1820. {
  1821. unsigned i;
  1822. FLAC__Metadata_SimpleIteratorStatus status;
  1823. const unsigned num_comments_len = FLAC__STREAM_METADATA_VORBIS_COMMENT_NUM_COMMENTS_LEN / 8;
  1824. FLAC__byte buffer[4]; /* magic number is asserted below */
  1825. FLAC__ASSERT(FLAC__STREAM_METADATA_VORBIS_COMMENT_NUM_COMMENTS_LEN / 8 == sizeof(buffer));
  1826. if(FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK != (status = read_metadata_block_data_vorbis_comment_entry_cb_(handle, read_cb, &(block->vendor_string))))
  1827. return status;
  1828. if(read_cb(buffer, 1, num_comments_len, handle) != num_comments_len)
  1829. return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
  1830. block->num_comments = unpack_uint32_little_endian_(buffer, num_comments_len);
  1831. if(block->num_comments == 0) {
  1832. block->comments = 0;
  1833. }
  1834. else if(0 == (block->comments = (FLAC__StreamMetadata_VorbisComment_Entry*)calloc(block->num_comments, sizeof(FLAC__StreamMetadata_VorbisComment_Entry))))
  1835. return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR;
  1836. for(i = 0; i < block->num_comments; i++) {
  1837. if(FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK != (status = read_metadata_block_data_vorbis_comment_entry_cb_(handle, read_cb, block->comments + i)))
  1838. return status;
  1839. }
  1840. return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK;
  1841. }
  1842. FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_cuesheet_track_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__StreamMetadata_CueSheet_Track *track)
  1843. {
  1844. unsigned i, len;
  1845. FLAC__byte buffer[32]; /* asserted below that this is big enough */
  1846. FLAC__ASSERT(sizeof(buffer) >= sizeof(FLAC__uint64));
  1847. FLAC__ASSERT(sizeof(buffer) >= FLAC__STREAM_METADATA_CUESHEET_INDEX_RESERVED_LEN/8);
  1848. FLAC__ASSERT(sizeof(buffer) >= (FLAC__STREAM_METADATA_CUESHEET_TRACK_TYPE_LEN + FLAC__STREAM_METADATA_CUESHEET_TRACK_PRE_EMPHASIS_LEN + FLAC__STREAM_METADATA_CUESHEET_TRACK_RESERVED_LEN) / 8);
  1849. FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_TRACK_OFFSET_LEN % 8 == 0);
  1850. len = FLAC__STREAM_METADATA_CUESHEET_TRACK_OFFSET_LEN / 8;
  1851. if(read_cb(buffer, 1, len, handle) != len)
  1852. return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
  1853. track->offset = unpack_uint64_(buffer, len);
  1854. FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_TRACK_NUMBER_LEN % 8 == 0);
  1855. len = FLAC__STREAM_METADATA_CUESHEET_TRACK_NUMBER_LEN / 8;
  1856. if(read_cb(buffer, 1, len, handle) != len)
  1857. return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
  1858. track->number = (FLAC__byte)unpack_uint32_(buffer, len);
  1859. FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_TRACK_ISRC_LEN % 8 == 0);
  1860. len = FLAC__STREAM_METADATA_CUESHEET_TRACK_ISRC_LEN / 8;
  1861. if(read_cb(track->isrc, 1, len, handle) != len)
  1862. return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
  1863. FLAC__ASSERT((FLAC__STREAM_METADATA_CUESHEET_TRACK_TYPE_LEN + FLAC__STREAM_METADATA_CUESHEET_TRACK_PRE_EMPHASIS_LEN + FLAC__STREAM_METADATA_CUESHEET_TRACK_RESERVED_LEN) % 8 == 0);
  1864. len = (FLAC__STREAM_METADATA_CUESHEET_TRACK_TYPE_LEN + FLAC__STREAM_METADATA_CUESHEET_TRACK_PRE_EMPHASIS_LEN + FLAC__STREAM_METADATA_CUESHEET_TRACK_RESERVED_LEN) / 8;
  1865. if(read_cb(buffer, 1, len, handle) != len)
  1866. return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
  1867. FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_TRACK_TYPE_LEN == 1);
  1868. FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_TRACK_PRE_EMPHASIS_LEN == 1);
  1869. track->type = buffer[0] >> 7;
  1870. track->pre_emphasis = (buffer[0] >> 6) & 1;
  1871. FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_TRACK_NUM_INDICES_LEN % 8 == 0);
  1872. len = FLAC__STREAM_METADATA_CUESHEET_TRACK_NUM_INDICES_LEN / 8;
  1873. if(read_cb(buffer, 1, len, handle) != len)
  1874. return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
  1875. track->num_indices = (FLAC__byte)unpack_uint32_(buffer, len);
  1876. if(track->num_indices == 0) {
  1877. track->indices = 0;
  1878. }
  1879. else if(0 == (track->indices = (FLAC__StreamMetadata_CueSheet_Index*)calloc(track->num_indices, sizeof(FLAC__StreamMetadata_CueSheet_Index))))
  1880. return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR;
  1881. for(i = 0; i < track->num_indices; i++) {
  1882. FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_INDEX_OFFSET_LEN % 8 == 0);
  1883. len = FLAC__STREAM_METADATA_CUESHEET_INDEX_OFFSET_LEN / 8;
  1884. if(read_cb(buffer, 1, len, handle) != len)
  1885. return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
  1886. track->indices[i].offset = unpack_uint64_(buffer, len);
  1887. FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_INDEX_NUMBER_LEN % 8 == 0);
  1888. len = FLAC__STREAM_METADATA_CUESHEET_INDEX_NUMBER_LEN / 8;
  1889. if(read_cb(buffer, 1, len, handle) != len)
  1890. return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
  1891. track->indices[i].number = (FLAC__byte)unpack_uint32_(buffer, len);
  1892. FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_INDEX_RESERVED_LEN % 8 == 0);
  1893. len = FLAC__STREAM_METADATA_CUESHEET_INDEX_RESERVED_LEN / 8;
  1894. if(read_cb(buffer, 1, len, handle) != len)
  1895. return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
  1896. }
  1897. return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK;
  1898. }
  1899. FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_cuesheet_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__StreamMetadata_CueSheet *block)
  1900. {
  1901. unsigned i, len;
  1902. FLAC__Metadata_SimpleIteratorStatus status;
  1903. FLAC__byte buffer[1024]; /* MSVC needs a constant expression so we put a magic number and assert */
  1904. FLAC__ASSERT((FLAC__STREAM_METADATA_CUESHEET_IS_CD_LEN + FLAC__STREAM_METADATA_CUESHEET_RESERVED_LEN)/8 <= sizeof(buffer));
  1905. FLAC__ASSERT(sizeof(FLAC__uint64) <= sizeof(buffer));
  1906. FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_MEDIA_CATALOG_NUMBER_LEN % 8 == 0);
  1907. len = FLAC__STREAM_METADATA_CUESHEET_MEDIA_CATALOG_NUMBER_LEN / 8;
  1908. if(read_cb(block->media_catalog_number, 1, len, handle) != len)
  1909. return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
  1910. FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_LEAD_IN_LEN % 8 == 0);
  1911. len = FLAC__STREAM_METADATA_CUESHEET_LEAD_IN_LEN / 8;
  1912. if(read_cb(buffer, 1, len, handle) != len)
  1913. return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
  1914. block->lead_in = unpack_uint64_(buffer, len);
  1915. FLAC__ASSERT((FLAC__STREAM_METADATA_CUESHEET_IS_CD_LEN + FLAC__STREAM_METADATA_CUESHEET_RESERVED_LEN) % 8 == 0);
  1916. len = (FLAC__STREAM_METADATA_CUESHEET_IS_CD_LEN + FLAC__STREAM_METADATA_CUESHEET_RESERVED_LEN) / 8;
  1917. if(read_cb(buffer, 1, len, handle) != len)
  1918. return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
  1919. block->is_cd = buffer[0]&0x80? true : false;
  1920. FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_NUM_TRACKS_LEN % 8 == 0);
  1921. len = FLAC__STREAM_METADATA_CUESHEET_NUM_TRACKS_LEN / 8;
  1922. if(read_cb(buffer, 1, len, handle) != len)
  1923. return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
  1924. block->num_tracks = unpack_uint32_(buffer, len);
  1925. if(block->num_tracks == 0) {
  1926. block->tracks = 0;
  1927. }
  1928. else if(0 == (block->tracks = (FLAC__StreamMetadata_CueSheet_Track*)calloc(block->num_tracks, sizeof(FLAC__StreamMetadata_CueSheet_Track))))
  1929. return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR;
  1930. for(i = 0; i < block->num_tracks; i++) {
  1931. if(FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK != (status = read_metadata_block_data_cuesheet_track_cb_(handle, read_cb, block->tracks + i)))
  1932. return status;
  1933. }
  1934. return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK;
  1935. }
  1936. static FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_picture_cstring_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__byte **data, FLAC__uint32 *length, FLAC__uint32 length_len)
  1937. {
  1938. FLAC__byte buffer[sizeof(FLAC__uint32)];
  1939. FLAC__ASSERT(0 != data);
  1940. FLAC__ASSERT(length_len%8 == 0);
  1941. length_len /= 8; /* convert to bytes */
  1942. FLAC__ASSERT(sizeof(buffer) >= length_len);
  1943. if(read_cb(buffer, 1, length_len, handle) != length_len)
  1944. return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
  1945. *length = unpack_uint32_(buffer, length_len);
  1946. if(0 != *data)
  1947. free(*data);
  1948. if(0 == (*data = (FLAC__byte*)safe_malloc_add_2op_(*length, /*+*/1)))
  1949. return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR;
  1950. if(*length > 0) {
  1951. if(read_cb(*data, 1, *length, handle) != *length)
  1952. return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
  1953. }
  1954. (*data)[*length] = '\0';
  1955. return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK;
  1956. }
  1957. FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_picture_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__StreamMetadata_Picture *block)
  1958. {
  1959. FLAC__Metadata_SimpleIteratorStatus status;
  1960. FLAC__byte buffer[4]; /* asserted below that this is big enough */
  1961. FLAC__uint32 len;
  1962. FLAC__ASSERT(sizeof(buffer) >= FLAC__STREAM_METADATA_PICTURE_TYPE_LEN/8);
  1963. FLAC__ASSERT(sizeof(buffer) >= FLAC__STREAM_METADATA_PICTURE_WIDTH_LEN/8);
  1964. FLAC__ASSERT(sizeof(buffer) >= FLAC__STREAM_METADATA_PICTURE_HEIGHT_LEN/8);
  1965. FLAC__ASSERT(sizeof(buffer) >= FLAC__STREAM_METADATA_PICTURE_DEPTH_LEN/8);
  1966. FLAC__ASSERT(sizeof(buffer) >= FLAC__STREAM_METADATA_PICTURE_COLORS_LEN/8);
  1967. FLAC__ASSERT(FLAC__STREAM_METADATA_PICTURE_TYPE_LEN % 8 == 0);
  1968. len = FLAC__STREAM_METADATA_PICTURE_TYPE_LEN / 8;
  1969. if(read_cb(buffer, 1, len, handle) != len)
  1970. return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
  1971. block->type = (FLAC__StreamMetadata_Picture_Type)unpack_uint32_(buffer, len);
  1972. if((status = read_metadata_block_data_picture_cstring_cb_(handle, read_cb, (FLAC__byte**)(&(block->mime_type)), &len, FLAC__STREAM_METADATA_PICTURE_MIME_TYPE_LENGTH_LEN)) != FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK)
  1973. return status;
  1974. if((status = read_metadata_block_data_picture_cstring_cb_(handle, read_cb, &(block->description), &len, FLAC__STREAM_METADATA_PICTURE_DESCRIPTION_LENGTH_LEN)) != FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK)
  1975. return status;
  1976. FLAC__ASSERT(FLAC__STREAM_METADATA_PICTURE_WIDTH_LEN % 8 == 0);
  1977. len = FLAC__STREAM_METADATA_PICTURE_WIDTH_LEN / 8;
  1978. if(read_cb(buffer, 1, len, handle) != len)
  1979. return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
  1980. block->width = unpack_uint32_(buffer, len);
  1981. FLAC__ASSERT(FLAC__STREAM_METADATA_PICTURE_HEIGHT_LEN % 8 == 0);
  1982. len = FLAC__STREAM_METADATA_PICTURE_HEIGHT_LEN / 8;
  1983. if(read_cb(buffer, 1, len, handle) != len)
  1984. return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
  1985. block->height = unpack_uint32_(buffer, len);
  1986. FLAC__ASSERT(FLAC__STREAM_METADATA_PICTURE_DEPTH_LEN % 8 == 0);
  1987. len = FLAC__STREAM_METADATA_PICTURE_DEPTH_LEN / 8;
  1988. if(read_cb(buffer, 1, len, handle) != len)
  1989. return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
  1990. block->depth = unpack_uint32_(buffer, len);
  1991. FLAC__ASSERT(FLAC__STREAM_METADATA_PICTURE_COLORS_LEN % 8 == 0);
  1992. len = FLAC__STREAM_METADATA_PICTURE_COLORS_LEN / 8;
  1993. if(read_cb(buffer, 1, len, handle) != len)
  1994. return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
  1995. block->colors = unpack_uint32_(buffer, len);
  1996. /* for convenience we use read_metadata_block_data_picture_cstring_cb_() even though it adds an extra terminating NUL we don't use */
  1997. if((status = read_metadata_block_data_picture_cstring_cb_(handle, read_cb, &(block->data), &(block->data_length), FLAC__STREAM_METADATA_PICTURE_DATA_LENGTH_LEN)) != FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK)
  1998. return status;
  1999. return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK;
  2000. }
  2001. FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_unknown_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__StreamMetadata_Unknown *block, unsigned block_length)
  2002. {
  2003. if(block_length == 0) {
  2004. block->data = 0;
  2005. }
  2006. else {
  2007. if(0 == (block->data = (FLAC__byte*)malloc(block_length)))
  2008. return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR;
  2009. if(read_cb(block->data, 1, block_length, handle) != block_length)
  2010. return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
  2011. }
  2012. return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK;
  2013. }
  2014. FLAC__bool write_metadata_block_header_(FILE *file, FLAC__Metadata_SimpleIteratorStatus *status, const FLAC__StreamMetadata *block)
  2015. {
  2016. FLAC__ASSERT(0 != file);
  2017. FLAC__ASSERT(0 != status);
  2018. if(!write_metadata_block_header_cb_((FLAC__IOHandle)file, (FLAC__IOCallback_Write)fwrite, block)) {
  2019. *status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_WRITE_ERROR;
  2020. return false;
  2021. }
  2022. return true;
  2023. }
  2024. FLAC__bool write_metadata_block_data_(FILE *file, FLAC__Metadata_SimpleIteratorStatus *status, const FLAC__StreamMetadata *block)
  2025. {
  2026. FLAC__ASSERT(0 != file);
  2027. FLAC__ASSERT(0 != status);
  2028. if (write_metadata_block_data_cb_((FLAC__IOHandle)file, (FLAC__IOCallback_Write)fwrite, block)) {
  2029. *status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK;
  2030. return true;
  2031. }
  2032. else {
  2033. *status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_WRITE_ERROR;
  2034. return false;
  2035. }
  2036. }
  2037. FLAC__bool write_metadata_block_header_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata *block)
  2038. {
  2039. FLAC__byte buffer[FLAC__STREAM_METADATA_HEADER_LENGTH];
  2040. FLAC__ASSERT(block->length < (1u << FLAC__STREAM_METADATA_LENGTH_LEN));
  2041. buffer[0] = (block->is_last? 0x80 : 0) | (FLAC__byte)block->type;
  2042. pack_uint32_(block->length, buffer + 1, 3);
  2043. if(write_cb(buffer, 1, FLAC__STREAM_METADATA_HEADER_LENGTH, handle) != FLAC__STREAM_METADATA_HEADER_LENGTH)
  2044. return false;
  2045. return true;
  2046. }
  2047. FLAC__bool write_metadata_block_data_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata *block)
  2048. {
  2049. FLAC__ASSERT(0 != block);
  2050. switch(block->type) {
  2051. case FLAC__METADATA_TYPE_STREAMINFO:
  2052. return write_metadata_block_data_streaminfo_cb_(handle, write_cb, &block->data.stream_info);
  2053. case FLAC__METADATA_TYPE_PADDING:
  2054. return write_metadata_block_data_padding_cb_(handle, write_cb, &block->data.padding, block->length);
  2055. case FLAC__METADATA_TYPE_APPLICATION:
  2056. return write_metadata_block_data_application_cb_(handle, write_cb, &block->data.application, block->length);
  2057. case FLAC__METADATA_TYPE_SEEKTABLE:
  2058. return write_metadata_block_data_seektable_cb_(handle, write_cb, &block->data.seek_table);
  2059. case FLAC__METADATA_TYPE_VORBIS_COMMENT:
  2060. return write_metadata_block_data_vorbis_comment_cb_(handle, write_cb, &block->data.vorbis_comment);
  2061. case FLAC__METADATA_TYPE_CUESHEET:
  2062. return write_metadata_block_data_cuesheet_cb_(handle, write_cb, &block->data.cue_sheet);
  2063. case FLAC__METADATA_TYPE_PICTURE:
  2064. return write_metadata_block_data_picture_cb_(handle, write_cb, &block->data.picture);
  2065. default:
  2066. return write_metadata_block_data_unknown_cb_(handle, write_cb, &block->data.unknown, block->length);
  2067. }
  2068. }
  2069. FLAC__bool write_metadata_block_data_streaminfo_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata_StreamInfo *block)
  2070. {
  2071. FLAC__byte buffer[FLAC__STREAM_METADATA_STREAMINFO_LENGTH];
  2072. const unsigned channels1 = block->channels - 1;
  2073. const unsigned bps1 = block->bits_per_sample - 1;
  2074. /* we are using hardcoded numbers for simplicity but we should
  2075. * probably eventually write a bit-level packer and use the
  2076. * _STREAMINFO_ constants.
  2077. */
  2078. pack_uint32_(block->min_blocksize, buffer, 2);
  2079. pack_uint32_(block->max_blocksize, buffer+2, 2);
  2080. pack_uint32_(block->min_framesize, buffer+4, 3);
  2081. pack_uint32_(block->max_framesize, buffer+7, 3);
  2082. buffer[10] = (block->sample_rate >> 12) & 0xff;
  2083. buffer[11] = (block->sample_rate >> 4) & 0xff;
  2084. buffer[12] = ((block->sample_rate & 0x0f) << 4) | (channels1 << 1) | (bps1 >> 4);
  2085. buffer[13] = (FLAC__byte)(((bps1 & 0x0f) << 4) | ((block->total_samples >> 32) & 0x0f));
  2086. pack_uint32_((FLAC__uint32)block->total_samples, buffer+14, 4);
  2087. memcpy(buffer+18, block->md5sum, 16);
  2088. if(write_cb(buffer, 1, FLAC__STREAM_METADATA_STREAMINFO_LENGTH, handle) != FLAC__STREAM_METADATA_STREAMINFO_LENGTH)
  2089. return false;
  2090. return true;
  2091. }
  2092. FLAC__bool write_metadata_block_data_padding_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata_Padding *block, unsigned block_length)
  2093. {
  2094. unsigned i, n = block_length;
  2095. FLAC__byte buffer[1024];
  2096. (void)block;
  2097. memset(buffer, 0, 1024);
  2098. for(i = 0; i < n/1024; i++)
  2099. if(write_cb(buffer, 1, 1024, handle) != 1024)
  2100. return false;
  2101. n %= 1024;
  2102. if(write_cb(buffer, 1, n, handle) != n)
  2103. return false;
  2104. return true;
  2105. }
  2106. FLAC__bool write_metadata_block_data_application_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata_Application *block, unsigned block_length)
  2107. {
  2108. const unsigned id_bytes = FLAC__STREAM_METADATA_APPLICATION_ID_LEN / 8;
  2109. if(write_cb(block->id, 1, id_bytes, handle) != id_bytes)
  2110. return false;
  2111. block_length -= id_bytes;
  2112. if(write_cb(block->data, 1, block_length, handle) != block_length)
  2113. return false;
  2114. return true;
  2115. }
  2116. FLAC__bool write_metadata_block_data_seektable_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata_SeekTable *block)
  2117. {
  2118. unsigned i;
  2119. FLAC__byte buffer[FLAC__STREAM_METADATA_SEEKPOINT_LENGTH];
  2120. for(i = 0; i < block->num_points; i++) {
  2121. /* some MAGIC NUMBERs here */
  2122. pack_uint64_(block->points[i].sample_number, buffer, 8);
  2123. pack_uint64_(block->points[i].stream_offset, buffer+8, 8);
  2124. pack_uint32_(block->points[i].frame_samples, buffer+16, 2);
  2125. if(write_cb(buffer, 1, FLAC__STREAM_METADATA_SEEKPOINT_LENGTH, handle) != FLAC__STREAM_METADATA_SEEKPOINT_LENGTH)
  2126. return false;
  2127. }
  2128. return true;
  2129. }
  2130. FLAC__bool write_metadata_block_data_vorbis_comment_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata_VorbisComment *block)
  2131. {
  2132. unsigned i;
  2133. const unsigned entry_length_len = FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN / 8;
  2134. const unsigned num_comments_len = FLAC__STREAM_METADATA_VORBIS_COMMENT_NUM_COMMENTS_LEN / 8;
  2135. FLAC__byte buffer[4]; /* magic number is asserted below */
  2136. FLAC__ASSERT(max(FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN, FLAC__STREAM_METADATA_VORBIS_COMMENT_NUM_COMMENTS_LEN) / 8 == sizeof(buffer));
  2137. pack_uint32_little_endian_(block->vendor_string.length, buffer, entry_length_len);
  2138. if(write_cb(buffer, 1, entry_length_len, handle) != entry_length_len)
  2139. return false;
  2140. if(write_cb(block->vendor_string.entry, 1, block->vendor_string.length, handle) != block->vendor_string.length)
  2141. return false;
  2142. pack_uint32_little_endian_(block->num_comments, buffer, num_comments_len);
  2143. if(write_cb(buffer, 1, num_comments_len, handle) != num_comments_len)
  2144. return false;
  2145. for(i = 0; i < block->num_comments; i++) {
  2146. pack_uint32_little_endian_(block->comments[i].length, buffer, entry_length_len);
  2147. if(write_cb(buffer, 1, entry_length_len, handle) != entry_length_len)
  2148. return false;
  2149. if(write_cb(block->comments[i].entry, 1, block->comments[i].length, handle) != block->comments[i].length)
  2150. return false;
  2151. }
  2152. return true;
  2153. }
  2154. FLAC__bool write_metadata_block_data_cuesheet_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata_CueSheet *block)
  2155. {
  2156. unsigned i, j, len;
  2157. FLAC__byte buffer[1024]; /* asserted below that this is big enough */
  2158. FLAC__ASSERT(sizeof(buffer) >= sizeof(FLAC__uint64));
  2159. FLAC__ASSERT(sizeof(buffer) >= FLAC__STREAM_METADATA_CUESHEET_RESERVED_LEN/8);
  2160. FLAC__ASSERT(sizeof(buffer) >= (FLAC__STREAM_METADATA_CUESHEET_TRACK_TYPE_LEN + FLAC__STREAM_METADATA_CUESHEET_TRACK_PRE_EMPHASIS_LEN + FLAC__STREAM_METADATA_CUESHEET_IS_CD_LEN + FLAC__STREAM_METADATA_CUESHEET_TRACK_RESERVED_LEN)/8);
  2161. FLAC__ASSERT(sizeof(buffer) >= FLAC__STREAM_METADATA_CUESHEET_INDEX_RESERVED_LEN/8);
  2162. FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_MEDIA_CATALOG_NUMBER_LEN % 8 == 0);
  2163. len = FLAC__STREAM_METADATA_CUESHEET_MEDIA_CATALOG_NUMBER_LEN / 8;
  2164. if(write_cb(block->media_catalog_number, 1, len, handle) != len)
  2165. return false;
  2166. FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_LEAD_IN_LEN % 8 == 0);
  2167. len = FLAC__STREAM_METADATA_CUESHEET_LEAD_IN_LEN / 8;
  2168. pack_uint64_(block->lead_in, buffer, len);
  2169. if(write_cb(buffer, 1, len, handle) != len)
  2170. return false;
  2171. FLAC__ASSERT((FLAC__STREAM_METADATA_CUESHEET_IS_CD_LEN + FLAC__STREAM_METADATA_CUESHEET_RESERVED_LEN) % 8 == 0);
  2172. len = (FLAC__STREAM_METADATA_CUESHEET_IS_CD_LEN + FLAC__STREAM_METADATA_CUESHEET_RESERVED_LEN) / 8;
  2173. memset(buffer, 0, len);
  2174. if(block->is_cd)
  2175. buffer[0] |= 0x80;
  2176. if(write_cb(buffer, 1, len, handle) != len)
  2177. return false;
  2178. FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_NUM_TRACKS_LEN % 8 == 0);
  2179. len = FLAC__STREAM_METADATA_CUESHEET_NUM_TRACKS_LEN / 8;
  2180. pack_uint32_(block->num_tracks, buffer, len);
  2181. if(write_cb(buffer, 1, len, handle) != len)
  2182. return false;
  2183. for(i = 0; i < block->num_tracks; i++) {
  2184. FLAC__StreamMetadata_CueSheet_Track *track = block->tracks + i;
  2185. FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_TRACK_OFFSET_LEN % 8 == 0);
  2186. len = FLAC__STREAM_METADATA_CUESHEET_TRACK_OFFSET_LEN / 8;
  2187. pack_uint64_(track->offset, buffer, len);
  2188. if(write_cb(buffer, 1, len, handle) != len)
  2189. return false;
  2190. FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_TRACK_NUMBER_LEN % 8 == 0);
  2191. len = FLAC__STREAM_METADATA_CUESHEET_TRACK_NUMBER_LEN / 8;
  2192. pack_uint32_(track->number, buffer, len);
  2193. if(write_cb(buffer, 1, len, handle) != len)
  2194. return false;
  2195. FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_TRACK_ISRC_LEN % 8 == 0);
  2196. len = FLAC__STREAM_METADATA_CUESHEET_TRACK_ISRC_LEN / 8;
  2197. if(write_cb(track->isrc, 1, len, handle) != len)
  2198. return false;
  2199. FLAC__ASSERT((FLAC__STREAM_METADATA_CUESHEET_TRACK_TYPE_LEN + FLAC__STREAM_METADATA_CUESHEET_TRACK_PRE_EMPHASIS_LEN + FLAC__STREAM_METADATA_CUESHEET_TRACK_RESERVED_LEN) % 8 == 0);
  2200. len = (FLAC__STREAM_METADATA_CUESHEET_TRACK_TYPE_LEN + FLAC__STREAM_METADATA_CUESHEET_TRACK_PRE_EMPHASIS_LEN + FLAC__STREAM_METADATA_CUESHEET_TRACK_RESERVED_LEN) / 8;
  2201. memset(buffer, 0, len);
  2202. buffer[0] = (track->type << 7) | (track->pre_emphasis << 6);
  2203. if(write_cb(buffer, 1, len, handle) != len)
  2204. return false;
  2205. FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_TRACK_NUM_INDICES_LEN % 8 == 0);
  2206. len = FLAC__STREAM_METADATA_CUESHEET_TRACK_NUM_INDICES_LEN / 8;
  2207. pack_uint32_(track->num_indices, buffer, len);
  2208. if(write_cb(buffer, 1, len, handle) != len)
  2209. return false;
  2210. for(j = 0; j < track->num_indices; j++) {
  2211. FLAC__StreamMetadata_CueSheet_Index *index = track->indices + j;
  2212. FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_INDEX_OFFSET_LEN % 8 == 0);
  2213. len = FLAC__STREAM_METADATA_CUESHEET_INDEX_OFFSET_LEN / 8;
  2214. pack_uint64_(index->offset, buffer, len);
  2215. if(write_cb(buffer, 1, len, handle) != len)
  2216. return false;
  2217. FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_INDEX_NUMBER_LEN % 8 == 0);
  2218. len = FLAC__STREAM_METADATA_CUESHEET_INDEX_NUMBER_LEN / 8;
  2219. pack_uint32_(index->number, buffer, len);
  2220. if(write_cb(buffer, 1, len, handle) != len)
  2221. return false;
  2222. FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_INDEX_RESERVED_LEN % 8 == 0);
  2223. len = FLAC__STREAM_METADATA_CUESHEET_INDEX_RESERVED_LEN / 8;
  2224. memset(buffer, 0, len);
  2225. if(write_cb(buffer, 1, len, handle) != len)
  2226. return false;
  2227. }
  2228. }
  2229. return true;
  2230. }
  2231. FLAC__bool write_metadata_block_data_picture_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata_Picture *block)
  2232. {
  2233. unsigned len;
  2234. size_t slen;
  2235. FLAC__byte buffer[4]; /* magic number is asserted below */
  2236. FLAC__ASSERT(0 == FLAC__STREAM_METADATA_PICTURE_TYPE_LEN%8);
  2237. FLAC__ASSERT(0 == FLAC__STREAM_METADATA_PICTURE_MIME_TYPE_LENGTH_LEN%8);
  2238. FLAC__ASSERT(0 == FLAC__STREAM_METADATA_PICTURE_DESCRIPTION_LENGTH_LEN%8);
  2239. FLAC__ASSERT(0 == FLAC__STREAM_METADATA_PICTURE_WIDTH_LEN%8);
  2240. FLAC__ASSERT(0 == FLAC__STREAM_METADATA_PICTURE_HEIGHT_LEN%8);
  2241. FLAC__ASSERT(0 == FLAC__STREAM_METADATA_PICTURE_DEPTH_LEN%8);
  2242. FLAC__ASSERT(0 == FLAC__STREAM_METADATA_PICTURE_COLORS_LEN%8);
  2243. FLAC__ASSERT(0 == FLAC__STREAM_METADATA_PICTURE_DATA_LENGTH_LEN%8);
  2244. FLAC__ASSERT(sizeof(buffer) >= FLAC__STREAM_METADATA_PICTURE_TYPE_LEN/8);
  2245. FLAC__ASSERT(sizeof(buffer) >= FLAC__STREAM_METADATA_PICTURE_MIME_TYPE_LENGTH_LEN/8);
  2246. FLAC__ASSERT(sizeof(buffer) >= FLAC__STREAM_METADATA_PICTURE_DESCRIPTION_LENGTH_LEN/8);
  2247. FLAC__ASSERT(sizeof(buffer) >= FLAC__STREAM_METADATA_PICTURE_WIDTH_LEN/8);
  2248. FLAC__ASSERT(sizeof(buffer) >= FLAC__STREAM_METADATA_PICTURE_HEIGHT_LEN/8);
  2249. FLAC__ASSERT(sizeof(buffer) >= FLAC__STREAM_METADATA_PICTURE_DEPTH_LEN/8);
  2250. FLAC__ASSERT(sizeof(buffer) >= FLAC__STREAM_METADATA_PICTURE_COLORS_LEN/8);
  2251. FLAC__ASSERT(sizeof(buffer) >= FLAC__STREAM_METADATA_PICTURE_DATA_LENGTH_LEN/8);
  2252. len = FLAC__STREAM_METADATA_PICTURE_TYPE_LEN/8;
  2253. pack_uint32_(block->type, buffer, len);
  2254. if(write_cb(buffer, 1, len, handle) != len)
  2255. return false;
  2256. len = FLAC__STREAM_METADATA_PICTURE_MIME_TYPE_LENGTH_LEN/8;
  2257. slen = strlen(block->mime_type);
  2258. pack_uint32_(slen, buffer, len);
  2259. if(write_cb(buffer, 1, len, handle) != len)
  2260. return false;
  2261. if(write_cb(block->mime_type, 1, slen, handle) != slen)
  2262. return false;
  2263. len = FLAC__STREAM_METADATA_PICTURE_DESCRIPTION_LENGTH_LEN/8;
  2264. slen = strlen((const char *)block->description);
  2265. pack_uint32_(slen, buffer, len);
  2266. if(write_cb(buffer, 1, len, handle) != len)
  2267. return false;
  2268. if(write_cb(block->description, 1, slen, handle) != slen)
  2269. return false;
  2270. len = FLAC__STREAM_METADATA_PICTURE_WIDTH_LEN/8;
  2271. pack_uint32_(block->width, buffer, len);
  2272. if(write_cb(buffer, 1, len, handle) != len)
  2273. return false;
  2274. len = FLAC__STREAM_METADATA_PICTURE_HEIGHT_LEN/8;
  2275. pack_uint32_(block->height, buffer, len);
  2276. if(write_cb(buffer, 1, len, handle) != len)
  2277. return false;
  2278. len = FLAC__STREAM_METADATA_PICTURE_DEPTH_LEN/8;
  2279. pack_uint32_(block->depth, buffer, len);
  2280. if(write_cb(buffer, 1, len, handle) != len)
  2281. return false;
  2282. len = FLAC__STREAM_METADATA_PICTURE_COLORS_LEN/8;
  2283. pack_uint32_(block->colors, buffer, len);
  2284. if(write_cb(buffer, 1, len, handle) != len)
  2285. return false;
  2286. len = FLAC__STREAM_METADATA_PICTURE_DATA_LENGTH_LEN/8;
  2287. pack_uint32_(block->data_length, buffer, len);
  2288. if(write_cb(buffer, 1, len, handle) != len)
  2289. return false;
  2290. if(write_cb(block->data, 1, block->data_length, handle) != block->data_length)
  2291. return false;
  2292. return true;
  2293. }
  2294. FLAC__bool write_metadata_block_data_unknown_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata_Unknown *block, unsigned block_length)
  2295. {
  2296. if(write_cb(block->data, 1, block_length, handle) != block_length)
  2297. return false;
  2298. return true;
  2299. }
  2300. FLAC__bool write_metadata_block_stationary_(FLAC__Metadata_SimpleIterator *iterator, const FLAC__StreamMetadata *block)
  2301. {
  2302. if(0 != fseeko(iterator->file, iterator->offset[iterator->depth], SEEK_SET)) {
  2303. iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR;
  2304. return false;
  2305. }
  2306. if(!write_metadata_block_header_(iterator->file, &iterator->status, block))
  2307. return false;
  2308. if(!write_metadata_block_data_(iterator->file, &iterator->status, block))
  2309. return false;
  2310. if(0 != fseeko(iterator->file, iterator->offset[iterator->depth], SEEK_SET)) {
  2311. iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR;
  2312. return false;
  2313. }
  2314. return read_metadata_block_header_(iterator);
  2315. }
  2316. FLAC__bool write_metadata_block_stationary_with_padding_(FLAC__Metadata_SimpleIterator *iterator, FLAC__StreamMetadata *block, unsigned padding_length, FLAC__bool padding_is_last)
  2317. {
  2318. FLAC__StreamMetadata *padding;
  2319. if(0 != fseeko(iterator->file, iterator->offset[iterator->depth], SEEK_SET)) {
  2320. iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR;
  2321. return false;
  2322. }
  2323. block->is_last = false;
  2324. if(!write_metadata_block_header_(iterator->file, &iterator->status, block))
  2325. return false;
  2326. if(!write_metadata_block_data_(iterator->file, &iterator->status, block))
  2327. return false;
  2328. if(0 == (padding = FLAC__metadata_object_new(FLAC__METADATA_TYPE_PADDING)))
  2329. return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR;
  2330. padding->is_last = padding_is_last;
  2331. padding->length = padding_length;
  2332. if(!write_metadata_block_header_(iterator->file, &iterator->status, padding)) {
  2333. FLAC__metadata_object_delete(padding);
  2334. return false;
  2335. }
  2336. if(!write_metadata_block_data_(iterator->file, &iterator->status, padding)) {
  2337. FLAC__metadata_object_delete(padding);
  2338. return false;
  2339. }
  2340. FLAC__metadata_object_delete(padding);
  2341. if(0 != fseeko(iterator->file, iterator->offset[iterator->depth], SEEK_SET)) {
  2342. iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR;
  2343. return false;
  2344. }
  2345. return read_metadata_block_header_(iterator);
  2346. }
  2347. FLAC__bool rewrite_whole_file_(FLAC__Metadata_SimpleIterator *iterator, FLAC__StreamMetadata *block, FLAC__bool append)
  2348. {
  2349. FILE *tempfile;
  2350. char *tempfilename;
  2351. int fixup_is_last_code = 0; /* 0 => no need to change any is_last flags */
  2352. off_t fixup_is_last_flag_offset = -1;
  2353. FLAC__ASSERT(0 != block || append == false);
  2354. if(iterator->is_last) {
  2355. if(append) {
  2356. fixup_is_last_code = 1; /* 1 => clear the is_last flag at the following offset */
  2357. fixup_is_last_flag_offset = iterator->offset[iterator->depth];
  2358. }
  2359. else if(0 == block) {
  2360. simple_iterator_push_(iterator);
  2361. if(!FLAC__metadata_simple_iterator_prev(iterator)) {
  2362. (void)simple_iterator_pop_(iterator);
  2363. return false;
  2364. }
  2365. fixup_is_last_code = -1; /* -1 => set the is_last the flag at the following offset */
  2366. fixup_is_last_flag_offset = iterator->offset[iterator->depth];
  2367. if(!simple_iterator_pop_(iterator))
  2368. return false;
  2369. }
  2370. }
  2371. if(!simple_iterator_copy_file_prefix_(iterator, &tempfile, &tempfilename, append))
  2372. return false;
  2373. if(0 != block) {
  2374. if(!write_metadata_block_header_(tempfile, &iterator->status, block)) {
  2375. cleanup_tempfile_(&tempfile, &tempfilename);
  2376. return false;
  2377. }
  2378. if(!write_metadata_block_data_(tempfile, &iterator->status, block)) {
  2379. cleanup_tempfile_(&tempfile, &tempfilename);
  2380. return false;
  2381. }
  2382. }
  2383. if(!simple_iterator_copy_file_postfix_(iterator, &tempfile, &tempfilename, fixup_is_last_code, fixup_is_last_flag_offset, block==0))
  2384. return false;
  2385. if(append)
  2386. return FLAC__metadata_simple_iterator_next(iterator);
  2387. return true;
  2388. }
  2389. void simple_iterator_push_(FLAC__Metadata_SimpleIterator *iterator)
  2390. {
  2391. FLAC__ASSERT(iterator->depth+1 < SIMPLE_ITERATOR_MAX_PUSH_DEPTH);
  2392. iterator->offset[iterator->depth+1] = iterator->offset[iterator->depth];
  2393. iterator->depth++;
  2394. }
  2395. FLAC__bool simple_iterator_pop_(FLAC__Metadata_SimpleIterator *iterator)
  2396. {
  2397. FLAC__ASSERT(iterator->depth > 0);
  2398. iterator->depth--;
  2399. if(0 != fseeko(iterator->file, iterator->offset[iterator->depth], SEEK_SET)) {
  2400. iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR;
  2401. return false;
  2402. }
  2403. return read_metadata_block_header_(iterator);
  2404. }
  2405. /* return meanings:
  2406. * 0: ok
  2407. * 1: read error
  2408. * 2: seek error
  2409. * 3: not a FLAC file
  2410. */
  2411. unsigned seek_to_first_metadata_block_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__IOCallback_Seek seek_cb)
  2412. {
  2413. FLAC__byte buffer[4];
  2414. size_t n;
  2415. unsigned i;
  2416. FLAC__ASSERT(FLAC__STREAM_SYNC_LENGTH == sizeof(buffer));
  2417. /* skip any id3v2 tag */
  2418. errno = 0;
  2419. n = read_cb(buffer, 1, 4, handle);
  2420. if(errno)
  2421. return 1;
  2422. else if(n != 4)
  2423. return 3;
  2424. else if(0 == memcmp(buffer, "ID3", 3)) {
  2425. unsigned tag_length = 0;
  2426. /* skip to the tag length */
  2427. if(seek_cb(handle, 2, SEEK_CUR) < 0)
  2428. return 2;
  2429. /* read the length */
  2430. for(i = 0; i < 4; i++) {
  2431. if(read_cb(buffer, 1, 1, handle) < 1 || buffer[0] & 0x80)
  2432. return 1;
  2433. tag_length <<= 7;
  2434. tag_length |= (buffer[0] & 0x7f);
  2435. }
  2436. /* skip the rest of the tag */
  2437. if(seek_cb(handle, tag_length, SEEK_CUR) < 0)
  2438. return 2;
  2439. /* read the stream sync code */
  2440. errno = 0;
  2441. n = read_cb(buffer, 1, 4, handle);
  2442. if(errno)
  2443. return 1;
  2444. else if(n != 4)
  2445. return 3;
  2446. }
  2447. /* check for the fLaC signature */
  2448. if(0 == memcmp(FLAC__STREAM_SYNC_STRING, buffer, FLAC__STREAM_SYNC_LENGTH))
  2449. return 0;
  2450. else
  2451. return 3;
  2452. }
  2453. unsigned seek_to_first_metadata_block_(FILE *f)
  2454. {
  2455. return seek_to_first_metadata_block_cb_((FLAC__IOHandle)f, (FLAC__IOCallback_Read)fread, fseek_wrapper_);
  2456. }
  2457. FLAC__bool simple_iterator_copy_file_prefix_(FLAC__Metadata_SimpleIterator *iterator, FILE **tempfile, char **tempfilename, FLAC__bool append)
  2458. {
  2459. const off_t offset_end = append? iterator->offset[iterator->depth] + (off_t)FLAC__STREAM_METADATA_HEADER_LENGTH + (off_t)iterator->length : iterator->offset[iterator->depth];
  2460. if(0 != fseeko(iterator->file, 0, SEEK_SET)) {
  2461. iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR;
  2462. return false;
  2463. }
  2464. if(!open_tempfile_(iterator->filename, iterator->tempfile_path_prefix, tempfile, tempfilename, &iterator->status)) {
  2465. cleanup_tempfile_(tempfile, tempfilename);
  2466. return false;
  2467. }
  2468. if(!copy_n_bytes_from_file_(iterator->file, *tempfile, offset_end, &iterator->status)) {
  2469. cleanup_tempfile_(tempfile, tempfilename);
  2470. return false;
  2471. }
  2472. return true;
  2473. }
  2474. FLAC__bool simple_iterator_copy_file_postfix_(FLAC__Metadata_SimpleIterator *iterator, FILE **tempfile, char **tempfilename, int fixup_is_last_code, off_t fixup_is_last_flag_offset, FLAC__bool backup)
  2475. {
  2476. off_t save_offset = iterator->offset[iterator->depth];
  2477. FLAC__ASSERT(0 != *tempfile);
  2478. if(0 != fseeko(iterator->file, save_offset + (off_t)FLAC__STREAM_METADATA_HEADER_LENGTH + (off_t)iterator->length, SEEK_SET)) {
  2479. cleanup_tempfile_(tempfile, tempfilename);
  2480. iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR;
  2481. return false;
  2482. }
  2483. if(!copy_remaining_bytes_from_file_(iterator->file, *tempfile, &iterator->status)) {
  2484. cleanup_tempfile_(tempfile, tempfilename);
  2485. return false;
  2486. }
  2487. if(fixup_is_last_code != 0) {
  2488. /*
  2489. * if code == 1, it means a block was appended to the end so
  2490. * we have to clear the is_last flag of the previous block
  2491. * if code == -1, it means the last block was deleted so
  2492. * we have to set the is_last flag of the previous block
  2493. */
  2494. /* MAGIC NUMBERs here; we know the is_last flag is the high bit of the byte at this location */
  2495. FLAC__byte x;
  2496. if(0 != fseeko(*tempfile, fixup_is_last_flag_offset, SEEK_SET)) {
  2497. cleanup_tempfile_(tempfile, tempfilename);
  2498. iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR;
  2499. return false;
  2500. }
  2501. if(fread(&x, 1, 1, *tempfile) != 1) {
  2502. cleanup_tempfile_(tempfile, tempfilename);
  2503. iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
  2504. return false;
  2505. }
  2506. if(fixup_is_last_code > 0) {
  2507. FLAC__ASSERT(x & 0x80);
  2508. x &= 0x7f;
  2509. }
  2510. else {
  2511. FLAC__ASSERT(!(x & 0x80));
  2512. x |= 0x80;
  2513. }
  2514. if(0 != fseeko(*tempfile, fixup_is_last_flag_offset, SEEK_SET)) {
  2515. cleanup_tempfile_(tempfile, tempfilename);
  2516. iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR;
  2517. return false;
  2518. }
  2519. if(local__fwrite(&x, 1, 1, *tempfile) != 1) {
  2520. cleanup_tempfile_(tempfile, tempfilename);
  2521. iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_WRITE_ERROR;
  2522. return false;
  2523. }
  2524. }
  2525. (void)fclose(iterator->file);
  2526. if(!transport_tempfile_(iterator->filename, tempfile, tempfilename, &iterator->status))
  2527. return false;
  2528. if(iterator->has_stats)
  2529. set_file_stats_(iterator->filename, &iterator->stats);
  2530. if(!simple_iterator_prime_input_(iterator, !iterator->is_writable))
  2531. return false;
  2532. if(backup) {
  2533. while(iterator->offset[iterator->depth] + (off_t)FLAC__STREAM_METADATA_HEADER_LENGTH + (off_t)iterator->length < save_offset)
  2534. if(!FLAC__metadata_simple_iterator_next(iterator))
  2535. return false;
  2536. return true;
  2537. }
  2538. else {
  2539. /* move the iterator to it's original block faster by faking a push, then doing a pop_ */
  2540. FLAC__ASSERT(iterator->depth == 0);
  2541. iterator->offset[0] = save_offset;
  2542. iterator->depth++;
  2543. return simple_iterator_pop_(iterator);
  2544. }
  2545. }
  2546. FLAC__bool copy_n_bytes_from_file_(FILE *file, FILE *tempfile, off_t bytes, FLAC__Metadata_SimpleIteratorStatus *status)
  2547. {
  2548. FLAC__byte buffer[8192];
  2549. size_t n;
  2550. FLAC__ASSERT(bytes >= 0);
  2551. while(bytes > 0) {
  2552. n = min(sizeof(buffer), (size_t)bytes);
  2553. if(fread(buffer, 1, n, file) != n) {
  2554. *status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
  2555. return false;
  2556. }
  2557. if(local__fwrite(buffer, 1, n, tempfile) != n) {
  2558. *status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_WRITE_ERROR;
  2559. return false;
  2560. }
  2561. bytes -= n;
  2562. }
  2563. return true;
  2564. }
  2565. FLAC__bool copy_n_bytes_from_file_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__IOHandle temp_handle, FLAC__IOCallback_Write temp_write_cb, off_t bytes, FLAC__Metadata_SimpleIteratorStatus *status)
  2566. {
  2567. FLAC__byte buffer[8192];
  2568. size_t n;
  2569. FLAC__ASSERT(bytes >= 0);
  2570. while(bytes > 0) {
  2571. n = min(sizeof(buffer), (size_t)bytes);
  2572. if(read_cb(buffer, 1, n, handle) != n) {
  2573. *status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
  2574. return false;
  2575. }
  2576. if(temp_write_cb(buffer, 1, n, temp_handle) != n) {
  2577. *status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_WRITE_ERROR;
  2578. return false;
  2579. }
  2580. bytes -= n;
  2581. }
  2582. return true;
  2583. }
  2584. FLAC__bool copy_remaining_bytes_from_file_(FILE *file, FILE *tempfile, FLAC__Metadata_SimpleIteratorStatus *status)
  2585. {
  2586. FLAC__byte buffer[8192];
  2587. size_t n;
  2588. while(!feof(file)) {
  2589. n = fread(buffer, 1, sizeof(buffer), file);
  2590. if(n == 0 && !feof(file)) {
  2591. *status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
  2592. return false;
  2593. }
  2594. if(n > 0 && local__fwrite(buffer, 1, n, tempfile) != n) {
  2595. *status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_WRITE_ERROR;
  2596. return false;
  2597. }
  2598. }
  2599. return true;
  2600. }
  2601. FLAC__bool copy_remaining_bytes_from_file_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__IOCallback_Eof eof_cb, FLAC__IOHandle temp_handle, FLAC__IOCallback_Write temp_write_cb, FLAC__Metadata_SimpleIteratorStatus *status)
  2602. {
  2603. FLAC__byte buffer[8192];
  2604. size_t n;
  2605. while(!eof_cb(handle)) {
  2606. n = read_cb(buffer, 1, sizeof(buffer), handle);
  2607. if(n == 0 && !eof_cb(handle)) {
  2608. *status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
  2609. return false;
  2610. }
  2611. if(n > 0 && temp_write_cb(buffer, 1, n, temp_handle) != n) {
  2612. *status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_WRITE_ERROR;
  2613. return false;
  2614. }
  2615. }
  2616. return true;
  2617. }
  2618. FLAC__bool open_tempfile_(const char *filename, const char *tempfile_path_prefix, FILE **tempfile, char **tempfilename, FLAC__Metadata_SimpleIteratorStatus *status)
  2619. {
  2620. static const char *tempfile_suffix = ".metadata_edit";
  2621. if(0 == tempfile_path_prefix) {
  2622. if(0 == (*tempfilename = (char*)safe_malloc_add_3op_(strlen(filename), /*+*/strlen(tempfile_suffix), /*+*/1))) {
  2623. *status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR;
  2624. return false;
  2625. }
  2626. strcpy(*tempfilename, filename);
  2627. strcat(*tempfilename, tempfile_suffix);
  2628. }
  2629. else {
  2630. const char *p = strrchr(filename, '/');
  2631. if(0 == p)
  2632. p = filename;
  2633. else
  2634. p++;
  2635. if(0 == (*tempfilename = (char*)safe_malloc_add_4op_(strlen(tempfile_path_prefix), /*+*/strlen(p), /*+*/strlen(tempfile_suffix), /*+*/2))) {
  2636. *status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR;
  2637. return false;
  2638. }
  2639. strcpy(*tempfilename, tempfile_path_prefix);
  2640. strcat(*tempfilename, "/");
  2641. strcat(*tempfilename, p);
  2642. strcat(*tempfilename, tempfile_suffix);
  2643. }
  2644. if(0 == (*tempfile = fopen(*tempfilename, "w+b"))) {
  2645. *status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_ERROR_OPENING_FILE;
  2646. return false;
  2647. }
  2648. return true;
  2649. }
  2650. FLAC__bool transport_tempfile_(const char *filename, FILE **tempfile, char **tempfilename, FLAC__Metadata_SimpleIteratorStatus *status)
  2651. {
  2652. FLAC__ASSERT(0 != filename);
  2653. FLAC__ASSERT(0 != tempfile);
  2654. FLAC__ASSERT(0 != *tempfile);
  2655. FLAC__ASSERT(0 != tempfilename);
  2656. FLAC__ASSERT(0 != *tempfilename);
  2657. FLAC__ASSERT(0 != status);
  2658. (void)fclose(*tempfile);
  2659. *tempfile = 0;
  2660. #if defined _MSC_VER || defined __BORLANDC__ || defined __MINGW32__ || defined __EMX__
  2661. /* on some flavors of windows, rename() will fail if the destination already exists */
  2662. if(unlink(filename) < 0) {
  2663. cleanup_tempfile_(tempfile, tempfilename);
  2664. *status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_UNLINK_ERROR;
  2665. return false;
  2666. }
  2667. #endif
  2668. /*@@@ to fully support the tempfile_path_prefix we need to update this piece to actually copy across filesystems instead of just rename(): */
  2669. if(0 != rename(*tempfilename, filename)) {
  2670. cleanup_tempfile_(tempfile, tempfilename);
  2671. *status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_RENAME_ERROR;
  2672. return false;
  2673. }
  2674. cleanup_tempfile_(tempfile, tempfilename);
  2675. return true;
  2676. }
  2677. void cleanup_tempfile_(FILE **tempfile, char **tempfilename)
  2678. {
  2679. if(0 != *tempfile) {
  2680. (void)fclose(*tempfile);
  2681. *tempfile = 0;
  2682. }
  2683. if(0 != *tempfilename) {
  2684. (void)unlink(*tempfilename);
  2685. free(*tempfilename);
  2686. *tempfilename = 0;
  2687. }
  2688. }
  2689. FLAC__bool get_file_stats_(const char *filename, struct stat *stats)
  2690. {
  2691. FLAC__ASSERT(0 != filename);
  2692. FLAC__ASSERT(0 != stats);
  2693. return (0 == stat(filename, stats));
  2694. }
  2695. void set_file_stats_(const char *filename, struct stat *stats)
  2696. {
  2697. struct utimbuf srctime;
  2698. FLAC__ASSERT(0 != filename);
  2699. FLAC__ASSERT(0 != stats);
  2700. srctime.actime = stats->st_atime;
  2701. srctime.modtime = stats->st_mtime;
  2702. (void)chmod(filename, stats->st_mode);
  2703. (void)utime(filename, &srctime);
  2704. #if !defined _WIN32 && !defined __BORLANDC__ && !defined __MINGW32__ && !defined __EMX__
  2705. (void)chown(filename, stats->st_uid, -1);
  2706. (void)chown(filename, -1, stats->st_gid);
  2707. #endif
  2708. }
  2709. int fseek_wrapper_(FLAC__IOHandle handle, FLAC__int64 offset, int whence)
  2710. {
  2711. return fseeko((FILE*)handle, (off_t)offset, whence);
  2712. }
  2713. FLAC__int64 ftell_wrapper_(FLAC__IOHandle handle)
  2714. {
  2715. return ftello((FILE*)handle);
  2716. }
  2717. FLAC__Metadata_ChainStatus get_equivalent_status_(FLAC__Metadata_SimpleIteratorStatus status)
  2718. {
  2719. switch(status) {
  2720. case FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK:
  2721. return FLAC__METADATA_CHAIN_STATUS_OK;
  2722. case FLAC__METADATA_SIMPLE_ITERATOR_STATUS_ILLEGAL_INPUT:
  2723. return FLAC__METADATA_CHAIN_STATUS_ILLEGAL_INPUT;
  2724. case FLAC__METADATA_SIMPLE_ITERATOR_STATUS_ERROR_OPENING_FILE:
  2725. return FLAC__METADATA_CHAIN_STATUS_ERROR_OPENING_FILE;
  2726. case FLAC__METADATA_SIMPLE_ITERATOR_STATUS_NOT_A_FLAC_FILE:
  2727. return FLAC__METADATA_CHAIN_STATUS_NOT_A_FLAC_FILE;
  2728. case FLAC__METADATA_SIMPLE_ITERATOR_STATUS_NOT_WRITABLE:
  2729. return FLAC__METADATA_CHAIN_STATUS_NOT_WRITABLE;
  2730. case FLAC__METADATA_SIMPLE_ITERATOR_STATUS_BAD_METADATA:
  2731. return FLAC__METADATA_CHAIN_STATUS_BAD_METADATA;
  2732. case FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR:
  2733. return FLAC__METADATA_CHAIN_STATUS_READ_ERROR;
  2734. case FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR:
  2735. return FLAC__METADATA_CHAIN_STATUS_SEEK_ERROR;
  2736. case FLAC__METADATA_SIMPLE_ITERATOR_STATUS_WRITE_ERROR:
  2737. return FLAC__METADATA_CHAIN_STATUS_WRITE_ERROR;
  2738. case FLAC__METADATA_SIMPLE_ITERATOR_STATUS_RENAME_ERROR:
  2739. return FLAC__METADATA_CHAIN_STATUS_RENAME_ERROR;
  2740. case FLAC__METADATA_SIMPLE_ITERATOR_STATUS_UNLINK_ERROR:
  2741. return FLAC__METADATA_CHAIN_STATUS_UNLINK_ERROR;
  2742. case FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR:
  2743. return FLAC__METADATA_CHAIN_STATUS_MEMORY_ALLOCATION_ERROR;
  2744. case FLAC__METADATA_SIMPLE_ITERATOR_STATUS_INTERNAL_ERROR:
  2745. default:
  2746. return FLAC__METADATA_CHAIN_STATUS_INTERNAL_ERROR;
  2747. }
  2748. }