how to force 1 byte padding when writing a binary file on windows - works on mac, not on win

StackOverflow https://stackoverflow.com/questions/5850309

  •  27-10-2019
  •  | 
  •  

Question

this is quite hard to explain out of context but i am going to try because its driving me nuts.

i am trying to write a binary file to represent the program and bank state of a vst audio plugin, based on the vst 2.4 spec - a program is the parameter values for one sound and a bank is a collection of programs (in my case 32). My program saving/loading code works fine on windows and mac. My bank saving code works fine on mac - I can save the state from my plugin, and open it via a vst-host's recall mechanisms. The files i create on mac are loadable by mac hosts and windows hosts, showing that this is saving the "correct" vst bank file format. On windows however, I get extra bytes in the vst bank file and it won't load via the host mechanism. I assume this is because on windows there is some padding. It doesn't seem to happen in the vst program files, which are smaller. I have tried #pragma pack(push, 1) in many different places to no avail. Can anyone suggest what I might do to fix this or what might be the cause?

thanks

mac hex good: mac hex good

win hex bad: win hex bad

Here is the code. The vst fxb file format wants big endian data hence the byte swap stuff. More info here: http://forum.cockos.com/showthread.php?t=78573

bool IPlugBase::SaveBankAsFXB(const char* defaultFileName)
{
  if (mGraphics)
  {
    WDL_String fileName(defaultFileName, strlen(defaultFileName));
    mGraphics->PromptForFile(&fileName, IGraphics::kFileSave, "", "fxb");

    if (fileName.GetLength()) 
    {
      FILE* fp = fopen(fileName.Get(), "w");

      VstInt32 chunkMagic = WDL_bswap_if_le('CcnK');
      VstInt32 byteSize = 0;
      VstInt32 fxbMagic;
      VstInt32 fxbVersion = WDL_bswap_if_le(kFXBVersionNum);
      VstInt32 pluginID = WDL_bswap_if_le(GetUniqueID());
      VstInt32 pluginVersion = WDL_bswap_if_le(GetEffectVersion(true));
      VstInt32 numPgms =  WDL_bswap_if_le(NPresets());
      VstInt32 currentPgm = WDL_bswap_if_le(GetCurrentPresetIdx());
      char future[124];
      memset(future, 0, 124);

      unsigned int bnkHeaderSize = 80;
      unsigned int pgmHeaderSize = 84;
      unsigned int pgmSize;
      unsigned int bnkSize; // total size in bytes

      unsigned int bytePos = 0;
      unsigned char *bnk;

      if (DoesStateChunks()) 
      {
              //TODO
      }
      else 
      {
        pgmSize = NParams() * 4;
        bnkSize = bnkHeaderSize + (NPresets() * (pgmHeaderSize + pgmSize));

        bnk = new unsigned char[bnkSize];

        fxbMagic = WDL_bswap_if_le('FxBk');

        // fxb header
        memcpy(bnk + bytePos, &chunkMagic, 4);
        bytePos += 4;
        memcpy(bnk + bytePos, &byteSize, 4);
        bytePos += 4;
        memcpy(bnk + bytePos, &fxbMagic, 4);
        bytePos += 4;
        memcpy(bnk + bytePos, &fxbVersion, 4);
        bytePos += 4;
        memcpy(bnk + bytePos, &pluginID, 4);
        bytePos += 4;
        memcpy(bnk + bytePos, &pluginVersion, 4);
        bytePos += 4;
        memcpy(bnk + bytePos, &numPgms, 4);
        bytePos += 4;
        memcpy(bnk + bytePos, &currentPgm, 4);
        bytePos += 4;
        memcpy(bnk + bytePos, &future, 124);
        bytePos += 124;

        VstInt32 fxpMagic = WDL_bswap_if_le('FxCk'); 
        VstInt32 fxpVersion = WDL_bswap_if_le(kFXPVersionNum);
        VstInt32 numParams = WDL_bswap_if_le(NParams());

        for (int p = 0; p < NPresets(); p++) 
        {
          IPreset* pPreset = mPresets.Get(p);

          char prgName[28];
          memset(prgName, 0, 28);
          strcpy(prgName, pPreset->mName);

          //fxp header
          memcpy(bnk + bytePos, &chunkMagic, 4);
          bytePos += 4;
          memcpy(bnk + bytePos, &byteSize, 4);
          bytePos += 4;
          memcpy(bnk + bytePos, &fxpMagic, 4);
          bytePos += 4;
          memcpy(bnk + bytePos, &fxpVersion, 4);
          bytePos += 4;
          memcpy(bnk + bytePos, &pluginID, 4);
          bytePos += 4;
          memcpy(bnk + bytePos, &pluginVersion, 4);
          bytePos += 4;
          memcpy(bnk + bytePos, &numParams, 4);
          bytePos += 4;
          memcpy(bnk + bytePos, &prgName, 28);
          bytePos += 28;

          //fxp data
          for (int i = 0; i< NParams(); i++) 
          {
            double v;
            pPreset->mChunk.Get(&v, i * sizeof(double));
            WDL_EndianFloat v32;
            v32.f = (float) mParams.Get(i)->GetNormalized(v);
            unsigned int swapped = WDL_bswap_if_le(v32.int32);

            memcpy(bnk + bytePos, &swapped, 4);
            bytePos += 4;
          }
        }
      }

      fwrite(bnk, bnkSize, 1, fp);
      fclose(fp);

      return true;
    }
    return false;
  }
  return false;
}
Was it helpful?

Solution

Try changing your fopen call to:

FILE* fp = fopen(fileName.Get(), "wb");

The b requests binary mode, which should prevent any type of text end-of-line marker processing.

See the fopen documentation on MSDN to see what sort of translation can happen in text mode.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top