Skip to content

Commit

Permalink
Merge pull request #283 from g-maxime/remove-chunk
Browse files Browse the repository at this point in the history
CLI: Add --remove-chunks= option to remove non-editable fields
  • Loading branch information
g-maxime authored Oct 29, 2024
2 parents a66a64a + 38d9ba4 commit e754442
Show file tree
Hide file tree
Showing 20 changed files with 183 additions and 2 deletions.
5 changes: 5 additions & 0 deletions Source/CLI/CLI_Help.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,11 @@ std::string Help()
ToDisplay<<"--MD5-Verify Verify MD5 for audio data"<<std::endl;
ToDisplay<<"--MD5-Embed Embed MD5 for audio data"<<std::endl;
ToDisplay<<"--MD5-Embed-Overwrite Embed MD5 for audio data - Allow overwriting"<<std::endl;
ToDisplay<<"*******************************************************************************"<<std::endl;
ToDisplay<<""<<std::endl;
ToDisplay<<"--remove-chunks= Remove specified items from the WAVE chunk tree (comma separated list)"<<std::endl;
ToDisplay<<" Chunk identifier must be 4 characters long and is case sensitive"<<std::endl;
ToDisplay<<" (separate levels with '/' e.g. INFO/IART)"<<std::endl;
ToDisplay<<""<<std::endl;

return ToDisplay.str();
Expand Down
46 changes: 45 additions & 1 deletion Source/CLI/CommandLine_Parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,9 @@ int Parse(Core &C, string &Argument)
OPTION("--md5-verify", MD5_Verify)
OPTION("--md5-embed-overwrite", MD5_Embed_Overwrite)
OPTION("--md5-embed", MD5_Embed)


OPTION("--remove-chunks=", Chunks_Remove)

//Default
OPTION("--", Default)
OPTION("-", Default)
Expand Down Expand Up @@ -802,6 +804,48 @@ CL_OPTION(MD5_Embed_Overwrite)
return -2; //Continue
}

//***************************************************************************
// Options - Others
//***************************************************************************

//---------------------------------------------------------------------------
CL_OPTION(Chunks_Remove)
{
ZtringList Fields;
Fields.Separator_Set(0, __T(","));
Fields.Write(Ztring().From_Local(string().assign(Argument, 16, std::string::npos)));

for (auto Field : Fields)
{
ZtringList Chunks;
Chunks.Separator_Set(0, __T("/"));
Chunks.Write(Field);

size_t Level=0;
for (auto Chunk :Chunks)
{
if (Chunk.empty()) // handle duplicate slashes
continue;

Level++;
if (Chunk.size()!=4)
{
std::cerr<<"Invalid chunk identifier: "<<Chunk.To_UTF8()<<std::endl;
return 1;
}
else if (!Level && (Chunk==__T("data") || Chunk==__T("ds64") || Chunk==__T("fmt ")))
{
std::cerr<<"Unable to remove mandatory chunk: "<<Chunk.To_UTF8()<<std::endl;
return 1;
}
}

C.In_Chunk_Remove(Field.To_UTF8());
}

return -2;
}

//***************************************************************************
// Options - Default
//***************************************************************************
Expand Down
1 change: 1 addition & 0 deletions Source/CLI/CommandLine_Parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ CL_OPTION(Fallback_Encoding);
CL_OPTION(Write_CodePage);
CL_OPTION(In_CSET_Remove);
CL_OPTION(Ignore_File_Encoding);
CL_OPTION(Chunks_Remove);

//---------------------------------------------------------------------------

Expand Down
13 changes: 13 additions & 0 deletions Source/Common/Core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,7 @@ float Core::Menu_File_Open_Files_Finish_Middle ()
Handler->second.In_cue__XML=In_cue__XML;
Handler->second.In_cue__FileName=In_cue__FileName;
Handler->second.In_CSET_Remove=In_CSET_Remove;
Handler->second.In_Chunks_Remove= Handler_Default.In_Chunks_Remove;

//Settings - Adding default Core values if the Core value does not exist yet (from --xxx=)
for (map<string, Ztring>::iterator In_Core_Item=Handler_Default.In_Core.begin(); In_Core_Item!=Handler_Default.In_Core.end(); In_Core_Item++)
Expand Down Expand Up @@ -384,6 +385,12 @@ float Core::Menu_File_Open_Files_Finish_Middle ()

bool IsModified_Old=Handler->second.Riff->IsModified_Get();

for (size_t Pos=0; Pos<Handler->second.In_Chunks_Remove.size(); Pos++)
{
Handler->second.Riff->Remove_Chunk(Handler->second.In_Chunks_Remove[Pos].To_UTF8());
StdAll(Handler);
}

//Modifying file with --xxx values
if (!Handler->second.In_Core.empty())
{
Expand Down Expand Up @@ -1281,6 +1288,12 @@ bool Core::In_Core_Add (const string &Field, const string &Value)
return true;
}

//---------------------------------------------------------------------------
bool Core::In_Chunk_Remove(const string &Field)
{
Handler_Default.In_Chunks_Remove.push_back(Ztring().From_UTF8(Field));
return true;
}
//---------------------------------------------------------------------------
string Core::Out_Core_Read (const string &FileName, const string &Field)
{
Expand Down
2 changes: 2 additions & 0 deletions Source/Common/Core.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ class Core : public Thread
void Batch_Launch_End (); //Detailed version
bool In_Core_Add (const string &FileName, const string &Field, const string &Value);
bool In_Core_Add (const string &Field, const string &Value);
bool In_Chunk_Remove (const string &Field);
string Out_Core_Read (const string &FileName, const string &Field);

//Configuration
Expand Down Expand Up @@ -199,6 +200,7 @@ class Core : public Thread
bool In_cue__XML;
string In_cue__FileName;
bool In_CSET_Remove;
ZtringList In_Chunks_Remove;

handler()
{
Expand Down
4 changes: 4 additions & 0 deletions Source/Riff/Riff_Base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#ifdef MACSTORE
#include "Common/Mac_Helpers.h"
#include "ZenLib/Dir.h"
#include "Riff_Base.h"
#endif

using namespace std;
Expand Down Expand Up @@ -273,6 +274,9 @@ void Riff_Base::Modify (int32u Chunk_Name_1, int32u Chunk_Name_2, int32u Chunk_N
//---------------------------------------------------------------------------
void Riff_Base::Modify_Internal_Subs (int32u Chunk_Name_1, int32u Chunk_Name_2, int32u Chunk_Name_3)
{
if (Chunk.Content.IsRemovable)
return;

//Special case: multiples chunks
if (Global->adtl && (Chunk_Name_1==Elements::WAVE_adtl_labl
|| Chunk_Name_1==Elements::WAVE_adtl_note
Expand Down
2 changes: 1 addition & 1 deletion Source/Riff/Riff_Base.h
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,7 @@ protected :
//---------------------------------------------------------------------------
//Read/Write
virtual void Read_Internal ();
virtual void Modify_Internal () {}
virtual void Modify_Internal () {};
virtual size_t Insert_Internal (int32u) {return Subs.size();}
virtual void Write_Internal () ;
void Write_Internal (const int8u* Buffer, size_t Buffer_Size);
Expand Down
3 changes: 3 additions & 0 deletions Source/Riff/Riff_Chunks_WAVE_CSET.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ void Riff_WAVE_CSET::Read_Internal ()
//---------------------------------------------------------------------------
void Riff_WAVE_CSET::Modify_Internal ()
{
if (Chunk.Content.IsRemovable)
return;

if (!Global->CSET)
{
Chunk.Content.IsRemovable=true;
Expand Down
3 changes: 3 additions & 0 deletions Source/Riff/Riff_Chunks_WAVE_INFO_xxxx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ void Riff_WAVE_INFO_xxxx::Read_Internal ()
//---------------------------------------------------------------------------
void Riff_WAVE_INFO_xxxx::Modify_Internal ()
{
if (Chunk.Content.IsRemovable)
return;

string Field=Ztring().From_CC4(Chunk.Header.Name).MakeUpperCase().To_UTF8();
if (Global->INFO->Strings[Field].empty())
{
Expand Down
3 changes: 3 additions & 0 deletions Source/Riff/Riff_Chunks_WAVE_MD5_.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ void Riff_WAVE_MD5_::Read_Internal ()
//---------------------------------------------------------------------------
void Riff_WAVE_MD5_::Modify_Internal ()
{
if (Chunk.Content.IsRemovable)
return;

if (!Global->EmbedMD5_AuthorizeOverWritting && !(Global->MD5Stored && !Global->MD5Stored->Strings["md5stored"].empty()))
return; //Should never happen (test in Riff_Handler), but in case of.

Expand Down
3 changes: 3 additions & 0 deletions Source/Riff/Riff_Chunks_WAVE__PMX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ void Riff_WAVE__PMX::Read_Internal ()
//---------------------------------------------------------------------------
void Riff_WAVE__PMX::Modify_Internal ()
{
if (Chunk.Content.IsRemovable)
return;

if (Global->XMP==NULL
|| Global->XMP->Strings["xmp"].empty())
{
Expand Down
3 changes: 3 additions & 0 deletions Source/Riff/Riff_Chunks_WAVE_aXML.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ void Riff_WAVE_axml::Read_Internal ()
//---------------------------------------------------------------------------
void Riff_WAVE_axml::Modify_Internal ()
{
if (Chunk.Content.IsRemovable)
return;

if (Global->aXML==NULL
|| Global->aXML->Strings["axml"].empty())
{
Expand Down
3 changes: 3 additions & 0 deletions Source/Riff/Riff_Chunks_WAVE_adtl_labl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ void Riff_WAVE_adtl_labl::Read_Internal ()
//---------------------------------------------------------------------------
void Riff_WAVE_adtl_labl::Modify_Internal ()
{
if (Chunk.Content.IsRemovable)
return;

if (!Global->adtl || Global->adtl->labels.empty() || Global->adtl->labelsIndex>=Global->adtl->labels.size())
{
Chunk.Content.IsRemovable=true;
Expand Down
3 changes: 3 additions & 0 deletions Source/Riff/Riff_Chunks_WAVE_adtl_ltxt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ void Riff_WAVE_adtl_ltxt::Read_Internal ()
//---------------------------------------------------------------------------
void Riff_WAVE_adtl_ltxt::Modify_Internal ()
{
if (Chunk.Content.IsRemovable)
return;

if (!Global->adtl || Global->adtl->texts.empty() || Global->adtl->textsIndex>=Global->adtl->texts.size())
{
Chunk.Content.IsRemovable=true;
Expand Down
3 changes: 3 additions & 0 deletions Source/Riff/Riff_Chunks_WAVE_adtl_note.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ void Riff_WAVE_adtl_note::Read_Internal ()
//---------------------------------------------------------------------------
void Riff_WAVE_adtl_note::Modify_Internal ()
{
if (Chunk.Content.IsRemovable)
return;

if (!Global->adtl || Global->adtl->notes.empty() || Global->adtl->notesIndex>=Global->adtl->notes.size())
{
Chunk.Content.IsRemovable=true;
Expand Down
3 changes: 3 additions & 0 deletions Source/Riff/Riff_Chunks_WAVE_bext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,9 @@ void Riff_WAVE_bext::Read_Internal ()
//---------------------------------------------------------------------------
void Riff_WAVE_bext::Modify_Internal ()
{
if (Chunk.Content.IsRemovable)
return;

if (Global->bext==NULL
|| (Global->bext->Strings["description"].empty()
&& Global->bext->Strings["originator"].empty()
Expand Down
3 changes: 3 additions & 0 deletions Source/Riff/Riff_Chunks_WAVE_cue_.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ void Riff_WAVE_cue_::Read_Internal ()
//---------------------------------------------------------------------------
void Riff_WAVE_cue_::Modify_Internal ()
{
if (Chunk.Content.IsRemovable)
return;

if (Global->cue_==NULL || Global->cue_->points.empty())
{
Chunk.Content.IsRemovable=true;
Expand Down
3 changes: 3 additions & 0 deletions Source/Riff/Riff_Chunks_WAVE_iXML.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ void Riff_WAVE_iXML::Read_Internal ()
//---------------------------------------------------------------------------
void Riff_WAVE_iXML::Modify_Internal ()
{
if (Chunk.Content.IsRemovable)
return;

if (Global->iXML==NULL
|| Global->iXML->Strings["ixml"].empty())
{
Expand Down
76 changes: 76 additions & 0 deletions Source/Riff/Riff_Handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1509,6 +1509,14 @@ bool Riff_Handler::Remove(const string &Field)
return Remove_Internal(Field);
}

//---------------------------------------------------------------------------
bool Riff_Handler::Remove_Chunk(const string &Field)
{
CriticalSectionLocker CSL(CS);

return Remove_Chunk_Internal(Field);
}

//---------------------------------------------------------------------------
bool Riff_Handler::Remove_Internal(const string &Field)
{
Expand Down Expand Up @@ -1564,6 +1572,74 @@ bool Riff_Handler::Remove_Internal(const string &Field)
return Set(Field, string(), *Chunk_Strings, Chunk_Name2_Get(Field), Chunk_Name3_Get(Field));
}

//---------------------------------------------------------------------------
bool Riff_Handler::Remove_Chunk_Helper(Riff_Base* Parent, const string& Path, vector<int32u>& ParentsCC4)
{
if (!Parent || Path.empty())
return false;

size_t Separator=Path.find_first_of('/');
string Field=Path.substr(0, Separator);

if (Field.empty()) // handle duplicate slashes
return Remove_Chunk_Helper(Parent, Path.substr(Separator+1), ParentsCC4);

if (Field.size()!=4)
{
Errors<<"Invalid chunk identifier: "<<Field<<endl;
return false;
}

int32u CC4=Ztring().From_UTF8(Field).To_CC4();
if (!CC4)
{
Errors<<"Invalid chunk identifier: "<<Field<<std::endl;
return false;
}

if (ParentsCC4.size()==1 && (CC4==Elements::WAVE_data || CC4==Elements::WAVE_ds64 || CC4==Elements::WAVE_fmt_))
{
Errors<<"Unable to remove mandatory chunk: "<<Field<<endl;
return false;
}

ParentsCC4.push_back(CC4);
for (size_t Pos=0; Pos<Parent->Subs.size(); Pos++)
{
if (Parent->Subs[Pos]->Header_Name_Get()==CC4)
{
if (Separator==string::npos)
{
Parent->Subs[Pos]->Chunk.Content.IsRemovable=true;
}
else
{
if (!Remove_Chunk_Helper(Parent->Subs[Pos], Path.substr(Separator+1), ParentsCC4))
return false;
}
}
}

return true;
}

//---------------------------------------------------------------------------
bool Riff_Handler::Remove_Chunk_Internal(const string &Field)
{
//Integrity
if (Chunks==NULL)
{
Errors<<"(No file name): Internal error"<<endl;
return false;
}

vector<int32u> CC4;
bool ToReturn=Remove_Chunk_Helper(Chunks, "WAVE/" + Field, CC4);
Chunks->Modify(CC4.size()>0?CC4[0]:NULL, CC4.size()>1?CC4[1]:NULL, CC4.size()>2?CC4[2]:NULL);

return ToReturn;
}

//---------------------------------------------------------------------------
bool Riff_Handler::IsModified(const string &Field)
{
Expand Down
3 changes: 3 additions & 0 deletions Source/Riff/Riff_Handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ class Riff_Handler
string Get (const string &Field);
bool Set (const string &Field, const string &Value, rules Rules);
bool Remove (const string &Field);
bool Remove_Chunk (const string &Field);
string History (const string &Field);
bool IsOriginal (const string &Field, const string &Value);
bool IsValid (const string &Field, const string &Value, rules Rules, bool IgnoreCoherency=false);
Expand Down Expand Up @@ -163,6 +164,8 @@ class Riff_Handler
string Get_Internal (const string &Field);
bool Set_Internal (const string &Field, const string &Value, rules Rules);
bool Remove_Internal (const string &Field);
bool Remove_Chunk_Helper (Riff_Base *Parent, const string &Path, vector<int32u>& ParentsCC4);
bool Remove_Chunk_Internal (const string &Field);
bool IsValid_Internal (const string &Field, const string &Value, rules Rules, bool IgnoreCoherency=false);
bool IsOriginal_Internal (const string &Field, const string &Value);
bool IsModified_Internal (const string &Field);
Expand Down

0 comments on commit e754442

Please sign in to comment.