| 1 |
/** |
| 2 |
* Furnace Tracker - multi-system chiptune tracker |
| 3 |
* Copyright (C) 2021-2025 tildearrow and contributors |
| 4 |
* |
| 5 |
* This program is free software; you can redistribute it and/or modify |
| 6 |
* it under the terms of the GNU General Public License as published by |
| 7 |
* the Free Software Foundation; either version 2 of the License, or |
| 8 |
* (at your option) any later version. |
| 9 |
* |
| 10 |
* This program is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
* GNU General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU General Public License along |
| 16 |
* with this program; if not, write to the Free Software Foundation, Inc., |
| 17 |
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 |
*/ |
| 19 |
|
| 20 |
#include "fileOpsCommon.h" |
| 21 |
|
| 22 |
bool DivEngine::load(unsigned char* f, size_t slen, const char* nameHint) { |
| 23 |
unsigned char* file; |
| 24 |
size_t len; |
| 25 |
if (slen<21) { |
| 26 |
logE("too small!"); |
| 27 |
lastError="file is too small"; |
| 28 |
delete[] f; |
| 29 |
return false; |
| 30 |
} |
| 31 |
|
| 32 |
if (!systemsRegistered) registerSystems(); |
| 33 |
|
| 34 |
// step 0: get extension of file |
| 35 |
String extS; |
| 36 |
if (nameHint!=NULL) { |
| 37 |
const char* ext=strrchr(nameHint,'.'); |
| 38 |
if (ext!=NULL) { |
| 39 |
for (; *ext; ext++) { |
| 40 |
char i=*ext; |
| 41 |
if (i>='A' && i<='Z') { |
| 42 |
i+='a'-'A'; |
| 43 |
} |
| 44 |
extS+=i; |
| 45 |
} |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
// step 1: try loading as a zlib-compressed file |
| 50 |
logD("trying zlib..."); |
| 51 |
try { |
| 52 |
z_stream zl; |
| 53 |
memset(&zl,0,sizeof(z_stream)); |
| 54 |
|
| 55 |
zl.avail_in=slen; |
| 56 |
zl.next_in=(Bytef*)f; |
| 57 |
zl.zalloc=NULL; |
| 58 |
zl.zfree=NULL; |
| 59 |
zl.opaque=NULL; |
| 60 |
|
| 61 |
int nextErr; |
| 62 |
nextErr=inflateInit(&zl); |
| 63 |
if (nextErr!=Z_OK) { |
| 64 |
if (zl.msg==NULL) { |
| 65 |
logD("zlib error: unknown! %d",nextErr); |
| 66 |
} else { |
| 67 |
logD("zlib error: %s",zl.msg); |
| 68 |
} |
| 69 |
inflateEnd(&zl); |
| 70 |
lastError="not a .dmf/.fur song"; |
| 71 |
throw NotZlibException(0); |
| 72 |
} |
| 73 |
|
| 74 |
std::vector<InflateBlock*> blocks; |
| 75 |
while (true) { |
| 76 |
InflateBlock* ib=new InflateBlock(DIV_READ_SIZE); |
| 77 |
zl.next_out=ib->buf; |
| 78 |
zl.avail_out=ib->len; |
| 79 |
|
| 80 |
nextErr=inflate(&zl,Z_SYNC_FLUSH); |
| 81 |
if (nextErr!=Z_OK && nextErr!=Z_STREAM_END) { |
| 82 |
if (zl.msg==NULL) { |
| 83 |
logD("zlib error: unknown error! %d",nextErr); |
| 84 |
lastError="unknown decompression error"; |
| 85 |
} else { |
| 86 |
logD("zlib inflate: %s",zl.msg); |
| 87 |
lastError=fmt::sprintf("decompression error: %s",zl.msg); |
| 88 |
} |
| 89 |
for (InflateBlock* i: blocks) delete i; |
| 90 |
blocks.clear(); |
| 91 |
delete ib; |
| 92 |
inflateEnd(&zl); |
| 93 |
throw NotZlibException(0); |
| 94 |
} |
| 95 |
ib->blockSize=ib->len-zl.avail_out; |
| 96 |
blocks.push_back(ib); |
| 97 |
if (nextErr==Z_STREAM_END) { |
| 98 |
break; |
| 99 |
} |
| 100 |
} |
| 101 |
nextErr=inflateEnd(&zl); |
| 102 |
if (nextErr!=Z_OK) { |
| 103 |
if (zl.msg==NULL) { |
| 104 |
logD("zlib end error: unknown error! %d",nextErr); |
| 105 |
lastError="unknown decompression finish error"; |
| 106 |
} else { |
| 107 |
logD("zlib end: %s",zl.msg); |
| 108 |
lastError=fmt::sprintf("decompression finish error: %s",zl.msg); |
| 109 |
} |
| 110 |
for (InflateBlock* i: blocks) delete i; |
| 111 |
blocks.clear(); |
| 112 |
throw NotZlibException(0); |
| 113 |
} |
| 114 |
|
| 115 |
size_t finalSize=0; |
| 116 |
size_t curSeek=0; |
| 117 |
for (InflateBlock* i: blocks) { |
| 118 |
finalSize+=i->blockSize; |
| 119 |
} |
| 120 |
if (finalSize<1) { |
| 121 |
logD("compressed too small!"); |
| 122 |
lastError="file too small"; |
| 123 |
for (InflateBlock* i: blocks) delete i; |
| 124 |
blocks.clear(); |
| 125 |
throw NotZlibException(0); |
| 126 |
} |
| 127 |
file=new unsigned char[finalSize]; |
| 128 |
for (InflateBlock* i: blocks) { |
| 129 |
memcpy(&file[curSeek],i->buf,i->blockSize); |
| 130 |
curSeek+=i->blockSize; |
| 131 |
delete i; |
| 132 |
} |
| 133 |
blocks.clear(); |
| 134 |
len=finalSize; |
| 135 |
delete[] f; |
| 136 |
} catch (NotZlibException& e) { |
| 137 |
logD("not zlib. loading as raw..."); |
| 138 |
file=f; |
| 139 |
len=slen; |
| 140 |
} |
| 141 |
|
| 142 |
// step 2: try loading as .fur, .dmf, or another magic-ful format |
| 143 |
if (memcmp(file,DIV_DMF_MAGIC,16)==0) { |
| 144 |
return loadDMF(file,len); |
| 145 |
} else if (memcmp(file,DIV_FTM_MAGIC,18)==0) { |
| 146 |
return loadFTM(file,len,(extS==".dnm"),false,(extS==".eft")); |
| 147 |
} else if (memcmp(file,DIV_DNM_MAGIC,21)==0) { |
| 148 |
return loadFTM(file,len,true,true,false); |
| 149 |
} else if (memcmp(file,DIV_FUR_MAGIC,16)==0) { |
| 150 |
return loadFur(file,len); |
| 151 |
} else if (memcmp(file,DIV_FUR_MAGIC_DS0,16)==0) { |
| 152 |
return loadFur(file,len,DIV_FUR_VARIANT_B); |
| 153 |
} else if (memcmp(file,DIV_FC13_MAGIC,4)==0 || memcmp(file,DIV_FC14_MAGIC,4)==0) { |
| 154 |
return loadFC(file,len); |
| 155 |
} else if (memcmp(file,DIV_TFM_MAGIC,8)==0) { |
| 156 |
return loadTFMv2(file,len); |
| 157 |
} else if (memcmp(file,DIV_IT_MAGIC,4)==0) { |
| 158 |
return loadIT(file,len); |
| 159 |
} else if (len>=48) { |
| 160 |
if (memcmp(&file[0x2c],DIV_S3M_MAGIC,4)==0) { |
| 161 |
return loadS3M(file,len); |
| 162 |
} else if (memcmp(file,DIV_XM_MAGIC,17)==0) { |
| 163 |
return loadXM(file,len); |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
// step 3: try loading as .mod or TFEv1 (if the file extension matches) |
| 168 |
if (extS==".tfe") { |
| 169 |
return loadTFMv1(file,len); |
| 170 |
} else if (loadMod(file,len)) { |
| 171 |
delete[] f; |
| 172 |
return true; |
| 173 |
} |
| 174 |
|
| 175 |
// step 4: not a valid file |
| 176 |
logE("not a valid module!"); |
| 177 |
lastError="not a compatible song"; |
| 178 |
delete[] file; |
| 179 |
return false; |
| 180 |
} |