| 1 | /* |
| 2 | * Descent 3 |
| 3 | * Copyright (C) 2024 Parallax Software |
| 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 3 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 |
| 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | |
| 18 | --- HISTORICAL COMMENTS FOLLOW --- |
| 19 | |
| 20 | * $Logfile: /DescentIII/Main/streamaudio/osfarchive.cpp $ |
| 21 | * $Revision: 7 $ |
| 22 | * $Date: 4/14/99 1:47a $ |
| 23 | * $Author: Jeff $ |
| 24 | * |
| 25 | * OSF File Reader |
| 26 | * |
| 27 | * $Log: /DescentIII/Main/streamaudio/osfarchive.cpp $ |
| 28 | * |
| 29 | * 7 4/14/99 1:47a Jeff |
| 30 | * fixed case mismatched #includes |
| 31 | * |
| 32 | * 6 4/02/99 4:32p Samir |
| 33 | * fixed problem with really long stream names screwing up the osf header. |
| 34 | * |
| 35 | * 5 4/01/99 4:50p Matt |
| 36 | * Took out Warning() function, chaning those calls to mprintf() |
| 37 | * |
| 38 | * 4 3/09/99 6:36p Samir |
| 39 | * try catch blocks. |
| 40 | * |
| 41 | * 3 10/21/98 8:13p Samir |
| 42 | * fixed music utils problems. |
| 43 | * |
| 44 | * 2 7/24/98 5:18p Samir |
| 45 | * moved OSF header to end of file. |
| 46 | * |
| 47 | * 1 7/20/98 2:54p Samir |
| 48 | * initial revision. |
| 49 | * |
| 50 | * $NoKeywords: $ |
| 51 | */ |
| 52 | |
| 53 | #include "streamaudio.h"cxx . . $ `<file>/stream_audio/streamaudio.h`/ |
| 54 | #include "cfile.h"cxx . . $ `<file>/cfile/cfile.h`/ |
| 55 | #include "pserror.h"cxx . . $ `<file>/misc/pserror.h`/ |
| 56 | #include <string.h> |
| 57 | |
| 58 | OSFArchivecxx . . $ OSFArchive#::OSFArchivecxx . . $ OSFArchive#OSFArchive(49f6e7a06ebc5aa8).() { |
| 59 | m_fpcxx . . $ OSFArchive#m_fp. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!; |
| 60 | m_lengthcxx . . $ OSFArchive#m_length. = 0; |
| 61 | m_typecxx . . $ OSFArchive#m_type. = 0; |
| 62 | m_compcxx . . $ OSFArchive#m_comp. = 0; |
| 63 | m_flagscxx . . $ OSFArchive#m_flags. = 0; |
| 64 | m_ratecxx . . $ OSFArchive#m_rate. = 0; |
| 65 | m_writemodecxx . . $ OSFArchive#m_writemode. = false; |
| 66 | m_namecxx . . $ OSFArchive#m_name.[0] = 0; |
| 67 | } |
| 68 | |
| 69 | OSFArchivecxx . . $ OSFArchive#::~cxx . . $ OSFArchive#`~OSFArchive`(ced63f7c635d850d).OSFArchivecxx . . $ OSFArchive#() { |
| 70 | if (m_fpcxx . . $ OSFArchive#m_fp.) { |
| 71 | cfclosecxx . . $ cfclose(3639e799710acb68).(m_fpcxx . . $ OSFArchive#m_fp.); |
| 72 | m_fpcxx . . $ OSFArchive#m_fp. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!; |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | bool OSFArchivecxx . . $ OSFArchive#::Opencxx . . $ OSFArchive#Open(e96b1cf346ae2c6).(const char *filename, bool write) { |
| 77 | char tag[8]; |
| 78 | |
| 79 | ASSERTcxx . . $ `misc/pserror.h:246:9`!(!m_fp); |
| 80 | |
| 81 | try { |
| 82 | if (write) |
| 83 | m_fpcxx . . $ OSFArchive#m_fp. = cfopencxx . . $ cfopen(7c7b94e455f1ae56).(filenamecxx . . $ std/filesystem/__cxx11/path#path(835bc3a16b770d4d)., "wb"); |
| 84 | else |
| 85 | m_fpcxx . . $ OSFArchive#m_fp. = cfopencxx . . $ cfopen(7c7b94e455f1ae56).(filenamecxx . . $ std/filesystem/__cxx11/path#path(835bc3a16b770d4d)., "rb"); |
| 86 | |
| 87 | if (!m_fpcxx . . $ OSFArchive#m_fp.) |
| 88 | return false; |
| 89 | |
| 90 | m_writemodecxx . . $ OSFArchive#m_writemode. = write; |
| 91 | |
| 92 | if (m_writemodecxx . . $ OSFArchive#m_writemode.) |
| 93 | return true; |
| 94 | |
| 95 | // okay, if we are in read mode, then we read in the header NOW |
| 96 | memset(tag, 0, sizeof(tag)); |
| 97 | cfseekcxx . . $ cfseek(f9992d249f032805).(m_fpcxx . . $ OSFArchive#m_fp., -OSF_HDR_SIZEcxx . . $ `stream_audio/streamaudio.h:154:9`!, SEEK_ENDcxx . . $ `<external>/232824719582829767/stdio.h:112:9`!); |
| 98 | cf_ReadBytescxx . . $ cf_ReadBytes(de6325ef3380a744).((uint8_tcxx . . $ uint8_t# *)tag, strlen(OSF_TAGcxx . . $ `stream_audio/streamaudio.h:155:9`!), m_fpcxx . . $ OSFArchive#m_fp.); |
| 99 | if (strcmp(OSF_TAGcxx . . $ `stream_audio/streamaudio.h:155:9`!, tag) != 0) { |
| 100 | mprintfcxx . . $ `ddebug/mono.h:82:9`!(0, "Illegal OSF file format for %s.\n", filename); |
| 101 | cfclosecxx . . $ cfclose(3639e799710acb68).(m_fpcxx . . $ OSFArchive#m_fp.); |
| 102 | m_fpcxx . . $ OSFArchive#m_fp. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!; |
| 103 | return false; |
| 104 | } |
| 105 | |
| 106 | // read in rest of header. |
| 107 | m_typecxx . . $ OSFArchive#m_type. = (uint8_tcxx . . $ uint8_t#)cf_ReadBytecxx . . $ cf_ReadByte(dcbbf0b0e13758ec).(m_fpcxx . . $ OSFArchive#m_fp.); |
| 108 | m_compcxx . . $ OSFArchive#m_comp. = (uint8_tcxx . . $ uint8_t#)cf_ReadBytecxx . . $ cf_ReadByte(dcbbf0b0e13758ec).(m_fpcxx . . $ OSFArchive#m_fp.); |
| 109 | m_flagscxx . . $ OSFArchive#m_flags. = (uint8_tcxx . . $ uint8_t#)cf_ReadBytecxx . . $ cf_ReadByte(dcbbf0b0e13758ec).(m_fpcxx . . $ OSFArchive#m_fp.); |
| 110 | m_ratecxx . . $ OSFArchive#m_rate. = (uint32_tcxx . . $ uint32_t#)cf_ReadBytecxx . . $ cf_ReadByte(dcbbf0b0e13758ec).(m_fpcxx . . $ OSFArchive#m_fp.); |
| 111 | |
| 112 | if (m_typecxx . . $ OSFArchive#m_type. == OSF_DIGITAL_STRMcxx . . $ `stream_audio/streamaudio.h:159:9`!) { |
| 113 | if (m_ratecxx . . $ OSFArchive#m_rate. == 11) |
| 114 | m_ratecxx . . $ OSFArchive#m_rate. = 11025; |
| 115 | else if (m_ratecxx . . $ OSFArchive#m_rate. == 22) |
| 116 | m_ratecxx . . $ OSFArchive#m_rate. = 22050; |
| 117 | else if (m_ratecxx . . $ OSFArchive#m_rate. == 44) |
| 118 | m_ratecxx . . $ OSFArchive#m_rate. = 44100; |
| 119 | |
| 120 | // read in aux header based off of type. |
| 121 | m_hdrcxx . . $ OSFArchive#m_hdr..digicxx . . $ OSFArchive#$anonymous_type_0#digi..measurecxx . . $ tOSFDigiHdr#measure. = (uint32_tcxx . . $ uint32_t#)cf_ReadIntcxx . . $ cf_ReadInt(d60d6f5e30f0c96a).(m_fpcxx . . $ OSFArchive#m_fp.); |
| 122 | } else { |
| 123 | mprintfcxx . . $ `ddebug/mono.h:82:9`!(0, "Unsupported OSF file type in %s!\n", filename); |
| 124 | cfclosecxx . . $ cfclose(3639e799710acb68).(m_fpcxx . . $ OSFArchive#m_fp.); |
| 125 | m_fpcxx . . $ OSFArchive#m_fp. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!; |
| 126 | return false; |
| 127 | } |
| 128 | |
| 129 | // read length. |
| 130 | m_lengthcxx . . $ OSFArchive#m_length. = (uint32_tcxx . . $ uint32_t#)cf_ReadIntcxx . . $ cf_ReadInt(d60d6f5e30f0c96a).(m_fpcxx . . $ OSFArchive#m_fp.); |
| 131 | |
| 132 | cfseekcxx . . $ cfseek(f9992d249f032805).(m_fpcxx . . $ OSFArchive#m_fp., -OSF_HDR_TITLE_OFScxx . . $ `stream_audio/streamaudio.h:156:9`!, SEEK_ENDcxx . . $ `<external>/232824719582829767/stdio.h:112:9`!); |
| 133 | if (!cf_ReadBytescxx . . $ cf_ReadBytes(de6325ef3380a744).((uint8_tcxx . . $ uint8_t# *)m_namecxx . . $ OSFArchive#m_name., OSF_HDR_TITLE_LENcxx . . $ `stream_audio/streamaudio.h:157:9`!, m_fpcxx . . $ OSFArchive#m_fp.)) { |
| 134 | mprintfcxx . . $ `ddebug/mono.h:82:9`!(0, "Stream title not found for %s.\n", filename); |
| 135 | cfclosecxx . . $ cfclose(3639e799710acb68).(m_fpcxx . . $ OSFArchive#m_fp.); |
| 136 | m_fpcxx . . $ OSFArchive#m_fp. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!; |
| 137 | return false; |
| 138 | } |
| 139 | } catch (...) { |
| 140 | if (m_fpcxx . . $ OSFArchive#m_fp.) { |
| 141 | try { |
| 142 | cfclosecxx . . $ cfclose(3639e799710acb68).(m_fpcxx . . $ OSFArchive#m_fp.); |
| 143 | } catch (...) { |
| 144 | } |
| 145 | m_fpcxx . . $ OSFArchive#m_fp. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!; |
| 146 | } |
| 147 | return false; |
| 148 | } |
| 149 | |
| 150 | m_datastartcxx . . $ OSFArchive#m_datastart. = 0; |
| 151 | |
| 152 | OSFArchivecxx . . $ OSFArchive#::Rewindcxx . . $ OSFArchive#Rewind(49f6e7a06ebc5aa8).(); |
| 153 | |
| 154 | return true; |
| 155 | } |
| 156 | |
| 157 | void OSFArchivecxx . . $ OSFArchive#::Closecxx . . $ OSFArchive#Close(49f6e7a06ebc5aa8).() { |
| 158 | if (m_fpcxx . . $ OSFArchive#m_fp.) { |
| 159 | try { |
| 160 | cfclosecxx . . $ cfclose(3639e799710acb68).(m_fpcxx . . $ OSFArchive#m_fp.); |
| 161 | } catch (...) { |
| 162 | } |
| 163 | m_fpcxx . . $ OSFArchive#m_fp. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!; |
| 164 | } |
| 165 | |
| 166 | m_writemodecxx . . $ OSFArchive#m_writemode. = false; |
| 167 | } |
| 168 | |
| 169 | void OSFArchivecxx . . $ OSFArchive#::Rewindcxx . . $ OSFArchive#Rewind(49f6e7a06ebc5aa8).() { |
| 170 | try { |
| 171 | cfseekcxx . . $ cfseek(f9992d249f032805).(m_fpcxx . . $ OSFArchive#m_fp., m_datastartcxx . . $ OSFArchive#m_datastart., SEEK_SETcxx . . $ `<external>/232824719582829767/stdio.h:110:9`!); |
| 172 | } catch (...) { |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | // write out operations. |
| 177 | bool OSFArchivecxx . . $ OSFArchive#::SaveHeadercxx . . $ OSFArchive#SaveHeader(f1c414a58a83a2e8).(uint8_tcxx . . $ uint8_t# type, uint8_tcxx . . $ uint8_t# comp, uint8_tcxx . . $ uint8_t# flags, uint32_tcxx . . $ uint32_t# rate, uint32_tcxx . . $ uint32_t# length, void *hdr, const char *name) { |
| 178 | char chbuf[OSF_HDR_SIZEcxx . . $ `stream_audio/streamaudio.h:154:9`!]; |
| 179 | int hdrstart, hdrcur; |
| 180 | |
| 181 | if (!m_fpcxx . . $ OSFArchive#m_fp. || !m_writemodecxx . . $ OSFArchive#m_writemode.) |
| 182 | return false; |
| 183 | |
| 184 | // write out data. |
| 185 | try { |
| 186 | char osftitle[OSF_HDR_TITLE_LENcxx . . $ `stream_audio/streamaudio.h:157:9`!]; |
| 187 | memset((uint8_tcxx . . $ uint8_t# *)chbuf, 0, OSF_HDR_SIZEcxx . . $ `stream_audio/streamaudio.h:154:9`!); |
| 188 | cf_WriteBytescxx . . $ cf_WriteBytes(b35058f74b39c3a6).((uint8_tcxx . . $ uint8_t# *)chbuf, OSF_HDR_SIZEcxx . . $ `stream_audio/streamaudio.h:154:9`!, m_fpcxx . . $ OSFArchive#m_fp.); |
| 189 | cfseekcxx . . $ cfseek(f9992d249f032805).(m_fpcxx . . $ OSFArchive#m_fp., -OSF_HDR_SIZEcxx . . $ `stream_audio/streamaudio.h:154:9`!, SEEK_CURcxx . . $ `<external>/232824719582829767/stdio.h:111:9`!); |
| 190 | |
| 191 | hdrstart = cftellcxx . . $ cftell(65b457210a6528a1).(m_fpcxx . . $ OSFArchive#m_fp.); |
| 192 | if (!cf_WriteBytescxx . . $ cf_WriteBytes(b35058f74b39c3a6).((uint8_tcxx . . $ uint8_t# *)OSF_TAGcxx . . $ `stream_audio/streamaudio.h:155:9`!, strlen(OSF_TAGcxx . . $ `stream_audio/streamaudio.h:155:9`!), m_fpcxx . . $ OSFArchive#m_fp.)) |
| 193 | return false; |
| 194 | |
| 195 | cf_WriteBytecxx . . $ cf_WriteByte(17377ebd5835367f).(m_fpcxx . . $ OSFArchive#m_fp., (int8_tcxx . . $ int8_t#)type); |
| 196 | cf_WriteBytecxx . . $ cf_WriteByte(17377ebd5835367f).(m_fpcxx . . $ OSFArchive#m_fp., (int8_tcxx . . $ int8_t#)comp); |
| 197 | cf_WriteBytecxx . . $ cf_WriteByte(17377ebd5835367f).(m_fpcxx . . $ OSFArchive#m_fp., (int8_tcxx . . $ int8_t#)flags); |
| 198 | |
| 199 | if (type == OSF_DIGITAL_STRMcxx . . $ `stream_audio/streamaudio.h:159:9`!) { |
| 200 | tOSFDigiHdrcxx . . $ tOSFDigiHdr# *digihdr = (tOSFDigiHdrcxx . . $ tOSFDigiHdr# *)hdr; |
| 201 | cf_WriteBytecxx . . $ cf_WriteByte(17377ebd5835367f).(m_fpcxx . . $ OSFArchive#m_fp., (rate == 11025) ? 11 : (rate == 22050) ? 22 : (rate == 44100) ? 44 : 0); |
| 202 | cf_WriteIntcxx . . $ cf_WriteInt(966989f25543eb13).(m_fpcxx . . $ OSFArchive#m_fp., (int)digihdr->measurecxx . . $ tOSFDigiHdr#measure.); |
| 203 | } else { |
| 204 | return false; |
| 205 | } |
| 206 | |
| 207 | cf_WriteIntcxx . . $ cf_WriteInt(966989f25543eb13).(m_fpcxx . . $ OSFArchive#m_fp., (int)length); |
| 208 | hdrcur = cftellcxx . . $ cftell(65b457210a6528a1).(m_fpcxx . . $ OSFArchive#m_fp.); |
| 209 | |
| 210 | if ((hdrcur - hdrstart) > OSF_HDR_TITLE_OFScxx . . $ `stream_audio/streamaudio.h:156:9`!) { |
| 211 | mprintfcxx . . $ `ddebug/mono.h:82:9`!(0, "stream header too big for title!\n"); |
| 212 | return false; |
| 213 | } |
| 214 | |
| 215 | cfseekcxx . . $ cfseek(f9992d249f032805).(m_fpcxx . . $ OSFArchive#m_fp., OSF_HDR_TITLE_OFScxx . . $ `stream_audio/streamaudio.h:156:9`! - (hdrcur - hdrstart), SEEK_CURcxx . . $ `<external>/232824719582829767/stdio.h:111:9`!); |
| 216 | strncpy(osftitle, name, OSF_HDR_TITLE_LENcxx . . $ `stream_audio/streamaudio.h:157:9`!); |
| 217 | osftitle[OSF_HDR_TITLE_LENcxx . . $ `stream_audio/streamaudio.h:157:9`! - 1] = 0; |
| 218 | if (!cf_WriteBytescxx . . $ cf_WriteBytes(b35058f74b39c3a6).((uint8_tcxx . . $ uint8_t# *)name, OSF_HDR_TITLE_LENcxx . . $ `stream_audio/streamaudio.h:157:9`!, m_fpcxx . . $ OSFArchive#m_fp.)) |
| 219 | return false; |
| 220 | } catch (...) { |
| 221 | return false; |
| 222 | } |
| 223 | |
| 224 | return true; |
| 225 | } |
| 226 | |
| 227 | bool OSFArchivecxx . . $ OSFArchive#::WriteBlockcxx . . $ OSFArchive#WriteBlock(6a46171b248bf29b).(uint8_tcxx . . $ uint8_t# *blk, int size) { return (cf_WriteBytescxx . . $ cf_WriteBytes(b35058f74b39c3a6).(blk, size, m_fpcxx . . $ OSFArchive#m_fp.) < size) ? false : true; } |
| 228 | |
| 229 | // read in operations. |
| 230 | int OSFArchivecxx . . $ OSFArchive#::Readcxx . . $ OSFArchive#Read(7405b416d0535413).(uint8_tcxx . . $ uint8_t# *blk, int size) { |
| 231 | int retval; |
| 232 | try { |
| 233 | retval = cf_ReadBytescxx . . $ cf_ReadBytes(de6325ef3380a744).(blk, size, m_fpcxx . . $ OSFArchive#m_fp.); |
| 234 | } catch (...) { |
| 235 | retval = 0; |
| 236 | } |
| 237 | return retval; |
| 238 | } |