1111#include " util/multibyte.h"
1212#include " util/tape_file_interface.h"
1313
14+ #include < algorithm>
1415#include < cassert>
1516#include < cstring>
1617
1718// #define VERBOSE LOG_GENERAL
1819// #define LOG_OUTPUT_FUNC osd_printf_info
1920#include " logmacro.h"
2021
21- DEFINE_DEVICE_TYPE (NSCSI_TAPE, nscsi_tape_device, " scsi_tape " , " SCSI tape " );
22+ namespace {
2223
2324// ////////////////////////////////////////////////////////////////////////////
2425
@@ -29,6 +30,19 @@ static constexpr u32 TAPE_DEFAULT_FIXED_BLOCK_LEN = 512;
2930static constexpr int TAPE_RW_BUF_ID = 2 ;
3031static constexpr int TAPE_PL_BUF_ID = 3 ;
3132
33+
34+ template <size_t N, typename T>
35+ void clear_response (uint8_t (&buf)[N], T len)
36+ {
37+ assert (std::size (buf) >= len);
38+ std::fill_n (buf, len, 0 );
39+ }
40+
41+ } // anonymous namespace
42+
43+
44+ DEFINE_DEVICE_TYPE (NSCSI_TAPE, nscsi_tape_device, " scsi_tape" , " SCSI tape" );
45+
3246// ////////////////////////////////////////////////////////////////////////////
3347
3448// construction
@@ -230,8 +244,7 @@ void nscsi_tape_device::handle_inquiry(const u8 lun) // mandatory; SCSI-2 sectio
230244 if (vpd_enable || page_code) // error: we don't support vital product data or pages other than 0
231245 return report_bad_cdb_field ();
232246
233- assert (sizeof (scsi_cmdbuf) >= 36 );
234- memset (scsi_cmdbuf, 0 , 36 );
247+ clear_response (scsi_cmdbuf, 36 );
235248 scsi_cmdbuf[0 ] = (lun == 0 ) ? 0x01 : 0x7f ; // we support tape device on LUN 0 only, per 7.5.3(a)
236249 scsi_cmdbuf[1 ] = 0x80 ; // we support removing tape
237250 scsi_cmdbuf[2 ] = 0x02 ; // we're compliant with SCSI-2 only
@@ -365,8 +378,7 @@ void nscsi_tape_device::handle_mode_sense_6() // mandatory; SCSI-2 sections 8.2.
365378 return report_no_saving_params ();
366379
367380 const u8 resp_len = bd_disable ? 4 : 12 ; // response length
368- assert (sizeof (scsi_cmdbuf) >= resp_len);
369- memset (scsi_cmdbuf, 0 , resp_len);
381+ clear_response (scsi_cmdbuf, resp_len);
370382 scsi_cmdbuf[0 ] = resp_len - 1 ; // mode data length does not include itself, per SCSI-2 section 8.3.3
371383 scsi_cmdbuf[2 ] = (m_has_tape && m_image->get_file ()->is_read_only ()) ? 0x80 : 0 ; // device-specific parameter
372384 scsi_cmdbuf[3 ] = bd_disable ? 0 : 8 ; // block descriptor length
@@ -612,8 +624,7 @@ void nscsi_tape_device::handle_read_block_limits() // mandatory; SCSI-2 section
612624 if ((scsi_cmdbuf[1 ] & 0x1f ) || scsi_cmdbuf[2 ] || scsi_cmdbuf[3 ] || scsi_cmdbuf[4 ]) // error: reserved bits set
613625 return report_bad_cdb_field ();
614626
615- assert (sizeof (scsi_cmdbuf) >= 6 );
616- memset (scsi_cmdbuf, 0 , 6 );
627+ clear_response (scsi_cmdbuf, 6 );
617628 put_u24be (&scsi_cmdbuf[1 ], 0xffffff ); // 16MB; "maximum block length limit"
618629 put_u16be (&scsi_cmdbuf[4 ], m_fixed_block_len); // "minimum block length limit"
619630 scsi_data_in (SBUF_MAIN, 6 );
@@ -664,8 +675,7 @@ void nscsi_tape_device::handle_read_position() // optional; SCSI-2 section 10.2.
664675 const bool bpu = status == tape_status::UNKNOWN
665676 || status == tape_status::UNKNOWN_EW
666677 || status == tape_status::EOM;
667- assert (sizeof (scsi_cmdbuf) >= 20 );
668- memset (scsi_cmdbuf, 0 , 20 );
678+ clear_response (scsi_cmdbuf, 20 );
669679 scsi_cmdbuf[0 ] = (bom ? 0x80 : 0 ) // is position at BOM
670680 | (eom ? 0x40 : 0 ) // is position between EW and EOM
671681 | (bpu ? 0x04 : 0 ); // is next block address invalid; "block position unknown"
0 commit comments