| 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" |
| 54 | #include "cfile.h" |
| 55 | #include "pserror.h" |
| 56 | #include <string.h> |
| 57 | |
| 58 | OSFArchive::OSFArchive() { |
| 59 | m_fp = NULL; |
| 60 | m_length = 0; |
| 61 | m_type = 0; |
| 62 | m_comp = 0; |
| 63 | m_flags = 0; |
| 64 | m_rate = 0; |
| 65 | m_writemode = false; |
| 66 | m_name[0] = 0; |
| 67 | } |
| 68 | |
| 69 | OSFArchive::~OSFArchive() { |
| 70 | if (m_fp) { |
| 71 | cfclose(m_fp); |
| 72 | m_fp = NULL; |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | bool OSFArchive::Open(const char *filename, bool write) { |
| 77 | char tag[8]; |
| 78 | |
| 79 | ASSERT(!m_fp); |
| 80 | |
| 81 | try { |
| 82 | if (write) |
| 83 | m_fp = cfopen(filename, "wb"); |
| 84 | else |
| 85 | m_fp = cfopen(filename, "rb"); |
| 86 | |
| 87 | if (!m_fp) |
| 88 | return false; |
| 89 | |
| 90 | m_writemode = write; |
| 91 | |
| 92 | if (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 | cfseek(m_fp, -OSF_HDR_SIZE, SEEK_END); |
| 98 | cf_ReadBytes((uint8_t *)tag, strlen(OSF_TAG), m_fp); |
| 99 | if (strcmp(OSF_TAG, tag) != 0) { |
| 100 | mprintf(0, "Illegal OSF file format for %s.\n", filename); |
| 101 | cfclose(m_fp); |
| 102 | m_fp = NULL; |
| 103 | return false; |
| 104 | } |
| 105 | |
| 106 | // read in rest of header. |
| 107 | m_type = (uint8_t)cf_ReadByte(m_fp); |
| 108 | m_comp = (uint8_t)cf_ReadByte(m_fp); |
| 109 | m_flags = (uint8_t)cf_ReadByte(m_fp); |
| 110 | m_rate = (uint32_t)cf_ReadByte(m_fp); |
| 111 | |
| 112 | if (m_type == OSF_DIGITAL_STRM) { |
| 113 | if (m_rate == 11) |
| 114 | m_rate = 11025; |
| 115 | else if (m_rate == 22) |
| 116 | m_rate = 22050; |
| 117 | else if (m_rate == 44) |
| 118 | m_rate = 44100; |
| 119 | |
| 120 | // read in aux header based off of type. |
| 121 | m_hdr.digi.measure = (uint32_t)cf_ReadInt(m_fp); |
| 122 | } else { |
| 123 | mprintf(0, "Unsupported OSF file type in %s!\n", filename); |
| 124 | cfclose(m_fp); |
| 125 | m_fp = NULL; |
| 126 | return false; |
| 127 | } |
| 128 | |
| 129 | // read length. |
| 130 | m_length = (uint32_t)cf_ReadInt(m_fp); |
| 131 | |
| 132 | cfseek(m_fp, -OSF_HDR_TITLE_OFS, SEEK_END); |
| 133 | if (!cf_ReadBytes((uint8_t *)m_name, OSF_HDR_TITLE_LEN, m_fp)) { |
| 134 | mprintf(0, "Stream title not found for %s.\n", filename); |
| 135 | cfclose(m_fp); |
| 136 | m_fp = NULL; |
| 137 | return false; |
| 138 | } |
| 139 | } catch (...) { |
| 140 | if (m_fp) { |
| 141 | try { |
| 142 | cfclose(m_fp); |
| 143 | } catch (...) { |
| 144 | } |
| 145 | m_fp = NULL; |
| 146 | } |
| 147 | return false; |
| 148 | } |
| 149 | |
| 150 | m_datastart = 0; |
| 151 | |
| 152 | OSFArchive::Rewind(); |
| 153 | |
| 154 | return true; |
| 155 | } |
| 156 | |
| 157 | void OSFArchive::Close() { |
| 158 | if (m_fp) { |
| 159 | try { |
| 160 | cfclose(m_fp); |
| 161 | } catch (...) { |
| 162 | } |
| 163 | m_fp = NULL; |
| 164 | } |
| 165 | |
| 166 | m_writemode = false; |
| 167 | } |
| 168 | |
| 169 | void OSFArchive::Rewind() { |
| 170 | try { |
| 171 | cfseek(m_fp, m_datastart, SEEK_SET); |
| 172 | } catch (...) { |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | // write out operations. |
| 177 | bool OSFArchive::SaveHeader(uint8_t type, uint8_t comp, uint8_t flags, uint32_t rate, uint32_t length, void *hdr, const char *name) { |
| 178 | char chbuf[OSF_HDR_SIZE]; |
| 179 | int hdrstart, hdrcur; |
| 180 | |
| 181 | if (!m_fp || !m_writemode) |
| 182 | return false; |
| 183 | |
| 184 | // write out data. |
| 185 | try { |
| 186 | char osftitle[OSF_HDR_TITLE_LEN]; |
| 187 | memset((uint8_t *)chbuf, 0, OSF_HDR_SIZE); |
| 188 | cf_WriteBytes((uint8_t *)chbuf, OSF_HDR_SIZE, m_fp); |
| 189 | cfseek(m_fp, -OSF_HDR_SIZE, SEEK_CUR); |
| 190 | |
| 191 | hdrstart = cftell(m_fp); |
| 192 | if (!cf_WriteBytes((uint8_t *)OSF_TAG, strlen(OSF_TAG), m_fp)) |
| 193 | return false; |
| 194 | |
| 195 | cf_WriteByte(m_fp, (int8_t)type); |
| 196 | cf_WriteByte(m_fp, (int8_t)comp); |
| 197 | cf_WriteByte(m_fp, (int8_t)flags); |
| 198 | |
| 199 | if (type == OSF_DIGITAL_STRM) { |
| 200 | tOSFDigiHdr *digihdr = (tOSFDigiHdr *)hdr; |
| 201 | cf_WriteByte(m_fp, (rate == 11025) ? 11 : (rate == 22050) ? 22 : (rate == 44100) ? 44 : 0); |
| 202 | cf_WriteInt(m_fp, (int)digihdr->measure); |
| 203 | } else { |
| 204 | return false; |
| 205 | } |
| 206 | |
| 207 | cf_WriteInt(m_fp, (int)length); |
| 208 | hdrcur = cftell(m_fp); |
| 209 | |
| 210 | if ((hdrcur - hdrstart) > OSF_HDR_TITLE_OFS) { |
| 211 | mprintf(0, "stream header too big for title!\n"); |
| 212 | return false; |
| 213 | } |
| 214 | |
| 215 | cfseek(m_fp, OSF_HDR_TITLE_OFS - (hdrcur - hdrstart), SEEK_CUR); |
| 216 | strncpy(osftitle, name, OSF_HDR_TITLE_LEN); |
| 217 | osftitle[OSF_HDR_TITLE_LEN - 1] = 0; |
| 218 | if (!cf_WriteBytes((uint8_t *)name, OSF_HDR_TITLE_LEN, m_fp)) |
| 219 | return false; |
| 220 | } catch (...) { |
| 221 | return false; |
| 222 | } |
| 223 | |
| 224 | return true; |
| 225 | } |
| 226 | |
| 227 | bool OSFArchive::WriteBlock(uint8_t *blk, int size) { return (cf_WriteBytes(blk, size, m_fp) < size) ? false : true; } |
| 228 | |
| 229 | // read in operations. |
| 230 | int OSFArchive::Read(uint8_t *blk, int size) { |
| 231 | int retval; |
| 232 | try { |
| 233 | retval = cf_ReadBytes(blk, size, m_fp); |
| 234 | } catch (...) { |
| 235 | retval = 0; |
| 236 | } |
| 237 | return retval; |
| 238 | } |