| 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 "safeWriter.h" |
| 21 |
#include "../ta-log.h" |
| 22 |
|
| 23 |
#define WRITER_BUF_SIZE 16384 |
| 24 |
|
| 25 |
unsigned char* SafeWriter::getFinalBuf() { |
| 26 |
return buf; |
| 27 |
} |
| 28 |
|
| 29 |
void SafeWriter::checkSize(size_t amount) { |
| 30 |
while ((curSeek+amount)>=bufLen) { |
| 31 |
size_t newSize=WRITER_BUF_SIZE*(1+((curSeek+amount)/WRITER_BUF_SIZE)); |
| 32 |
if (newSize<(bufLen+WRITER_BUF_SIZE)) { |
| 33 |
logE("REPORT NOW: newSize is too small! case 1... %d<%d",(int)newSize,(int)(bufLen+WRITER_BUF_SIZE)); |
| 34 |
} |
| 35 |
if (newSize<(curSeek+amount)) { |
| 36 |
logE("REPORT NOW: newSize is too small! case 2... %d<%d",(int)newSize,(int)(curSeek+amount)); |
| 37 |
} |
| 38 |
unsigned char* newBuf=new unsigned char[newSize]; |
| 39 |
memcpy(newBuf,buf,bufLen); |
| 40 |
delete[] buf; |
| 41 |
buf=newBuf; |
| 42 |
bufLen=newSize; |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
bool SafeWriter::seek(ssize_t where, int whence) { |
| 47 |
ssize_t supposed; |
| 48 |
switch (whence) { |
| 49 |
case SEEK_SET: |
| 50 |
supposed=where; |
| 51 |
break; |
| 52 |
case SEEK_CUR: |
| 53 |
supposed=curSeek+where; |
| 54 |
break; |
| 55 |
case SEEK_END: |
| 56 |
supposed=len+where; |
| 57 |
break; |
| 58 |
default: |
| 59 |
return false; |
| 60 |
} |
| 61 |
if (supposed<0) supposed=0; |
| 62 |
if (supposed>(ssize_t)len) supposed=len; |
| 63 |
curSeek=supposed; |
| 64 |
return true; |
| 65 |
} |
| 66 |
|
| 67 |
size_t SafeWriter::tell() { |
| 68 |
return curSeek; |
| 69 |
} |
| 70 |
|
| 71 |
size_t SafeWriter::size() { |
| 72 |
return len; |
| 73 |
} |
| 74 |
|
| 75 |
int SafeWriter::write(const void* what, size_t count) { |
| 76 |
if (!operative) return 0; |
| 77 |
checkSize(count); |
| 78 |
memcpy(buf+curSeek,what,count); |
| 79 |
curSeek+=count; |
| 80 |
if (curSeek>len) len=curSeek; |
| 81 |
return count; |
| 82 |
} |
| 83 |
|
| 84 |
int SafeWriter::writeC(signed char val) { |
| 85 |
return write(&val,1); |
| 86 |
} |
| 87 |
|
| 88 |
#ifdef TA_BIG_ENDIAN |
| 89 |
int SafeWriter::writeS_BE(short val) { |
| 90 |
return write(&val,2); |
| 91 |
} |
| 92 |
|
| 93 |
int SafeWriter::writeS(short val) { |
| 94 |
unsigned char bytes[2]{(unsigned char)((val>>8)&0xff), (unsigned char)(val&0xff)}; |
| 95 |
return write(bytes,2); |
| 96 |
} |
| 97 |
|
| 98 |
int SafeWriter::writeI_BE(int val) { |
| 99 |
return write(&val,4); |
| 100 |
} |
| 101 |
|
| 102 |
int SafeWriter::writeI(int val) { |
| 103 |
unsigned char bytes[4]; |
| 104 |
bytes[0]=((unsigned int)val)&0xff; |
| 105 |
bytes[1]=(((unsigned int)val)>>8)&0xff; |
| 106 |
bytes[2]=(((unsigned int)val)>>16)&0xff; |
| 107 |
bytes[3]=(((unsigned int)val)>>24)&0xff; |
| 108 |
return write(bytes,4); |
| 109 |
} |
| 110 |
|
| 111 |
int SafeWriter::writeL(int64_t val) { |
| 112 |
unsigned char bytes[8]; |
| 113 |
bytes[0]=((uint64_t)val)&0xff; |
| 114 |
bytes[1]=(((uint64_t)val)>>8)&0xff; |
| 115 |
bytes[2]=(((uint64_t)val)>>16)&0xff; |
| 116 |
bytes[3]=(((uint64_t)val)>>24)&0xff; |
| 117 |
bytes[4]=(((uint64_t)val)>>32)&0xff; |
| 118 |
bytes[5]=(((uint64_t)val)>>40)&0xff; |
| 119 |
bytes[6]=(((uint64_t)val)>>48)&0xff; |
| 120 |
bytes[7]=(((uint64_t)val)>>56)&0xff; |
| 121 |
return write(bytes,8); |
| 122 |
} |
| 123 |
|
| 124 |
int SafeWriter::writeF(float val) { |
| 125 |
unsigned char bytes[4]; |
| 126 |
bytes[0]=((unsigned char*)(&val))[3]; |
| 127 |
bytes[1]=((unsigned char*)(&val))[2]; |
| 128 |
bytes[2]=((unsigned char*)(&val))[1]; |
| 129 |
bytes[3]=((unsigned char*)(&val))[0]; |
| 130 |
return write(bytes,4); |
| 131 |
} |
| 132 |
|
| 133 |
int SafeWriter::writeD(double val) { |
| 134 |
unsigned char bytes[8]; |
| 135 |
bytes[0]=((unsigned char*)(&val))[7]; |
| 136 |
bytes[1]=((unsigned char*)(&val))[6]; |
| 137 |
bytes[2]=((unsigned char*)(&val))[5]; |
| 138 |
bytes[3]=((unsigned char*)(&val))[4]; |
| 139 |
bytes[4]=((unsigned char*)(&val))[3]; |
| 140 |
bytes[5]=((unsigned char*)(&val))[2]; |
| 141 |
bytes[6]=((unsigned char*)(&val))[1]; |
| 142 |
bytes[7]=((unsigned char*)(&val))[0]; |
| 143 |
return write(bytes,8); |
| 144 |
} |
| 145 |
#else |
| 146 |
int SafeWriter::writeS(short val) { |
| 147 |
return write(&val,2); |
| 148 |
} |
| 149 |
|
| 150 |
int SafeWriter::writeS_BE(short val) { |
| 151 |
unsigned char bytes[2]{(unsigned char)((val>>8)&0xff), (unsigned char)(val&0xff)}; |
| 152 |
return write(bytes,2); |
| 153 |
} |
| 154 |
|
| 155 |
int SafeWriter::writeI(int val) { |
| 156 |
return write(&val,4); |
| 157 |
} |
| 158 |
|
| 159 |
int SafeWriter::writeI_BE(int val) { |
| 160 |
unsigned char bytes[4] { |
| 161 |
(unsigned char)((val>>24)&0xff), |
| 162 |
(unsigned char)((val>>16)&0xff), |
| 163 |
(unsigned char)((val>>8)&0xff), |
| 164 |
(unsigned char)(val&0xff) |
| 165 |
}; |
| 166 |
return write(bytes,4); |
| 167 |
} |
| 168 |
|
| 169 |
int SafeWriter::writeL(int64_t val) { |
| 170 |
return write(&val,8); |
| 171 |
} |
| 172 |
|
| 173 |
int SafeWriter::writeF(float val) { |
| 174 |
return write(&val,4); |
| 175 |
} |
| 176 |
|
| 177 |
int SafeWriter::writeD(double val) { |
| 178 |
return write(&val,8); |
| 179 |
} |
| 180 |
#endif |
| 181 |
|
| 182 |
int SafeWriter::writeWString(WString val, bool pascal) { |
| 183 |
if (pascal) { |
| 184 |
writeS((unsigned short)val.size()); |
| 185 |
for (wchar_t& i: val) { |
| 186 |
writeS(i); |
| 187 |
} |
| 188 |
return 2+val.size()*2; |
| 189 |
} else { |
| 190 |
for (wchar_t& i: val) { |
| 191 |
writeS(i); |
| 192 |
} |
| 193 |
writeS(0); |
| 194 |
return 2+val.size()*2; |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
int SafeWriter::writeText(String val) { |
| 199 |
return write(val.c_str(),val.size()); |
| 200 |
} |
| 201 |
|
| 202 |
int SafeWriter::writeString(String val, bool pascal) { |
| 203 |
if (pascal) { |
| 204 |
writeC((unsigned char)val.size()); |
| 205 |
return write(val.c_str(),val.size())+1; |
| 206 |
} else { |
| 207 |
return write(val.c_str(),val.size()+1); |
| 208 |
} |
| 209 |
} |
| 210 |
|
| 211 |
void SafeWriter::init() { |
| 212 |
if (operative) return; |
| 213 |
buf=new unsigned char[WRITER_BUF_SIZE]; |
| 214 |
bufLen=WRITER_BUF_SIZE; |
| 215 |
len=0; |
| 216 |
curSeek=0; |
| 217 |
operative=true; |
| 218 |
} |
| 219 |
|
| 220 |
SafeReader* SafeWriter::toReader() { |
| 221 |
return new SafeReader(buf,len); |
| 222 |
} |
| 223 |
|
| 224 |
void SafeWriter::finish() { |
| 225 |
if (!operative) return; |
| 226 |
delete[] buf; |
| 227 |
buf=NULL; |
| 228 |
operative=false; |
| 229 |
} |
| 230 |
|
| 231 |
void SafeWriter::disown() { |
| 232 |
if (!operative) return; |
| 233 |
buf=NULL; |
| 234 |
operative=false; |
| 235 |
} |