18 changes: 8 additions & 10 deletions mythtv/libs/libmythbase/mythcoreutil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,7 @@ QByteArray gzipCompress(const QByteArray& data)
if (data.length() == 0)
return QByteArray();

static constexpr int kChunkSize = 1024;
char out[kChunkSize];
std::array <char,1024> out {};

// allocate inflate state
z_stream strm;
Expand All @@ -200,8 +199,8 @@ QByteArray gzipCompress(const QByteArray& data)
// run deflate()
do
{
strm.avail_out = kChunkSize;
strm.next_out = (Bytef*)(out);
strm.avail_out = out.size();
strm.next_out = (Bytef*)(out.data());

ret = deflate(&strm, Z_FINISH);

Expand All @@ -216,7 +215,7 @@ QByteArray gzipCompress(const QByteArray& data)
return QByteArray();
}

result.append(out, kChunkSize - strm.avail_out);
result.append(out.data(), out.size() - strm.avail_out);
}
while (strm.avail_out == 0);

Expand All @@ -232,8 +231,7 @@ QByteArray gzipUncompress(const QByteArray &data)
if (data.length() == 0)
return QByteArray();

static constexpr int kChunkSize = 1024;
char out[kChunkSize];
std::array<char,1024> out {};

// allocate inflate state
z_stream strm;
Expand All @@ -253,8 +251,8 @@ QByteArray gzipUncompress(const QByteArray &data)

do
{
strm.avail_out = kChunkSize;
strm.next_out = (Bytef*)out;
strm.avail_out = out.size();
strm.next_out = (Bytef*)out.data();
ret = inflate(&strm, Z_NO_FLUSH);

Q_ASSERT(ret != Z_STREAM_ERROR); // state not clobbered
Expand All @@ -268,7 +266,7 @@ QByteArray gzipUncompress(const QByteArray &data)
return QByteArray();
}

result.append(out, kChunkSize - strm.avail_out);
result.append(out.data(), out.size() - strm.avail_out);
}
while (strm.avail_out == 0);

Expand Down
2 changes: 1 addition & 1 deletion mythtv/libs/libmythbase/mythhdd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ MythHDD *MythHDD::Get(QObject* par, const char* devicePath, bool SuperMount,
* \param AllowEject True if the user is allowed to eject the media.
* \return new MythHDD instance.
*/
MythHDD::MythHDD(QObject *par, const char *DevicePath,
MythHDD::MythHDD(QObject *par, const QString& DevicePath,
bool SuperMount, bool AllowEject)
: MythMediaDevice(par, DevicePath, SuperMount, AllowEject)
{
Expand Down
2 changes: 1 addition & 1 deletion mythtv/libs/libmythbase/mythhdd.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
class MBASE_PUBLIC MythHDD : public MythMediaDevice
{
public:
MythHDD(QObject* par, const char* DevicePath,
MythHDD(QObject* par, const QString& DevicePath,
bool SuperMount, bool AllowEject);

MythMediaStatus checkMedia(void) override; // MythMediaDevice
Expand Down
5 changes: 3 additions & 2 deletions mythtv/libs/libmythbase/mythmedia.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// C header
#include <fcntl.h>
#include <unistd.h>
#include <utility>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/param.h>
Expand Down Expand Up @@ -72,9 +73,9 @@ MythMediaEvent::~MythMediaEvent()

ext_to_media_t MythMediaDevice::s_ext_to_media;

MythMediaDevice::MythMediaDevice(QObject* par, const char* DevicePath,
MythMediaDevice::MythMediaDevice(QObject* par, QString DevicePath,
bool SuperMount, bool AllowEject)
: QObject(par), m_devicePath(DevicePath),
: QObject(par), m_devicePath(std::move(DevicePath)),
m_allowEject(AllowEject), m_superMount(SuperMount)
{
m_realDevice = getSymlinkTarget(m_devicePath);
Expand Down
2 changes: 1 addition & 1 deletion mythtv/libs/libmythbase/mythmedia.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class MBASE_PUBLIC MythMediaDevice : public QObject
friend class MonitorThreadDarwin; // and trigger posting of MythMediaEvents

public:
MythMediaDevice(QObject* par, const char* DevicePath, bool SuperMount,
MythMediaDevice(QObject* par, QString DevicePath, bool SuperMount,
bool AllowEject);

const QString& getMountPath() const { return m_mountPath; }
Expand Down
19 changes: 9 additions & 10 deletions mythtv/libs/libmythbase/mythmiscutil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "mythmiscutil.h"

// C++ headers
#include <array>
#include <cerrno>
#include <cstdlib>
#include <ctime>
Expand Down Expand Up @@ -75,16 +76,14 @@ bool getUptime(time_t &uptime)

#elif defined(__FreeBSD__) || CONFIG_DARWIN

int mib[2];
std::array<int,2> mib { CTL_KERN, KERN_BOOTTIME };
struct timeval bootTime;
size_t len;

// Uptime is calculated. Get this machine's boot time
// and subtract it from the current machine time
len = sizeof(bootTime);
mib[0] = CTL_KERN;
mib[1] = KERN_BOOTTIME;
if (sysctl(mib, 2, &bootTime, &len, nullptr, 0) == -1)
if (sysctl(mib.data(), 2, &bootTime, &len, nullptr, 0) == -1)
{
LOG(VB_GENERAL, LOG_ERR, "sysctl() error");
return false;
Expand Down Expand Up @@ -633,10 +632,9 @@ QString FileHash(const QString& filename)

bool WakeOnLAN(const QString& MAC)
{
char msg[1024] = "\xFF\xFF\xFF\xFF\xFF\xFF";
int msglen = 6;
std::vector<char> msg(6, static_cast<char>(0xFF));
std::array<char,6> macaddr {};
QStringList tokens = MAC.split(':');
int macaddr[6];

if (tokens.size() != 6)
{
Expand All @@ -658,16 +656,17 @@ bool WakeOnLAN(const QString& MAC)
}
}

msg.reserve(1024);
for (int x = 0; x < 16; x++)
for (int y : macaddr)
msg[msglen++] = y;
msg.insert(msg.end(), macaddr.cbegin(), macaddr.cend());

LOG(VB_NETWORK, LOG_INFO,
QString("WakeOnLan(): Sending WOL packet to %1").arg(MAC));

QUdpSocket udp_socket;
qlonglong msglen = msg.size();
return udp_socket.writeDatagram(
msg, msglen, QHostAddress::Broadcast, 32767) == msglen;
msg.data(), msglen, QHostAddress::Broadcast, 32767) == msglen;
}

// Wake up either by command or by MAC address
Expand Down
3 changes: 2 additions & 1 deletion mythtv/libs/libmythbase/mythsystemlegacy.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
// review and some cleanup.

// C headers
#include <array>
#include <cstdint>
#include <ctime>

Expand Down Expand Up @@ -190,7 +191,7 @@ class MBASE_PUBLIC MythSystemLegacy : public QObject
int m_ioprio {0};

Setting m_settings;
QBuffer m_stdbuff[3];
std::array<QBuffer,3> m_stdbuff;
};

MBASE_PUBLIC uint myth_system(const QString &command,
Expand Down
20 changes: 9 additions & 11 deletions mythtv/libs/libmythbase/mythsystemunix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ void MythSystemLegacyIOHandler::run(void)
void MythSystemLegacyIOHandler::HandleRead(int fd, QBuffer *buff)
{
errno = 0;
int len = read(fd, &m_readbuf, 65536);
int len = read(fd, m_readbuf.data(), m_readbuf.size());
if( len <= 0 )
{
if( errno != EAGAIN )
Expand All @@ -159,7 +159,7 @@ void MythSystemLegacyIOHandler::HandleRead(int fd, QBuffer *buff)
}
else
{
buff->buffer().append(m_readbuf, len);
buff->buffer().append(m_readbuf.data(), len);

// Get the corresponding MythSystemLegacy instance, and the stdout/stderr
// type
Expand Down Expand Up @@ -770,20 +770,18 @@ void MythSystemLegacyUnix::Fork(time_t timeout)
QString LOC_ERR = QString("myth_system('%1'): Error: ").arg(GetLogCmd());

// For use in the child
char locerr[MAX_BUFLEN];
strncpy(locerr, LOC_ERR.toUtf8().constData(), MAX_BUFLEN);
locerr[MAX_BUFLEN-1] = '\0';
std::string locerr = qPrintable(LOC_ERR);

LOG(VB_SYSTEM, LOG_DEBUG, QString("Launching: %1").arg(GetLogCmd()));

int p_stdin[] = {-1,-1};
int p_stdout[] = {-1,-1};
int p_stderr[] = {-1,-1};
std::array<int,2> p_stdin {-1,-1};
std::array<int,2> p_stdout {-1,-1};
std::array<int,2> p_stderr {-1,-1};

/* set up pipes */
if( GetSetting("UseStdin") )
{
if( pipe(p_stdin) == -1 )
if( pipe(p_stdin.data()) == -1 )
{
LOG(VB_SYSTEM, LOG_ERR, LOC_ERR + "stdin pipe() failed");
SetStatus( GENERIC_EXIT_NOT_OK );
Expand Down Expand Up @@ -811,7 +809,7 @@ void MythSystemLegacyUnix::Fork(time_t timeout)
}
if( GetSetting("UseStdout") )
{
if( pipe(p_stdout) == -1 )
if( pipe(p_stdout.data()) == -1 )
{
LOG(VB_SYSTEM, LOG_ERR, LOC_ERR + "stdout pipe() failed");
SetStatus( GENERIC_EXIT_NOT_OK );
Expand Down Expand Up @@ -839,7 +837,7 @@ void MythSystemLegacyUnix::Fork(time_t timeout)
}
if( GetSetting("UseStderr") )
{
if( pipe(p_stderr) == -1 )
if( pipe(p_stderr.data()) == -1 )
{
LOG(VB_SYSTEM, LOG_ERR, LOC_ERR + "stderr pipe() failed");
SetStatus( GENERIC_EXIT_NOT_OK );
Expand Down
5 changes: 3 additions & 2 deletions mythtv/libs/libmythbase/mythsystemunix.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#ifndef MYTHSYSTEM_UNIX_H
#define MYTHSYSTEM_UNIX_H

#include <array>
#include <csignal>
#include <sys/select.h>

Expand Down Expand Up @@ -54,7 +55,7 @@ class MythSystemLegacyIOHandler: public MThread
fd_set m_fds {};
int m_maxfd {-1};
bool m_read {true};
char m_readbuf[65536] {0};
std::array<char,65536> m_readbuf {};
};

// spawn separate thread for signals to prevent manager
Expand Down Expand Up @@ -113,7 +114,7 @@ class MBASE_PUBLIC MythSystemLegacyUnix : public MythSystemLegacyPrivate
pid_t m_pid {0};
time_t m_timeout {0};

int m_stdpipe[3] {-1,-1, -1};
std::array<int,3> m_stdpipe {-1, -1, -1};
};

#endif // MYTHSYSTEM_UNIX_H
Expand Down
34 changes: 12 additions & 22 deletions mythtv/libs/libmythbase/signalhandling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <cstdint>
#include <cstdlib> // for free
#include <iostream>
#include <string>
#include <sys/types.h>
#include <unistd.h>
#ifndef _WIN32
Expand All @@ -20,39 +21,28 @@ using namespace std;
#include "exitcodes.h"
#include "signalhandling.h"

int SignalHandler::s_sigFd[2];
std::array<int,2> SignalHandler::s_sigFd;
volatile bool SignalHandler::s_exit_program = false;
QMutex SignalHandler::s_singletonLock;
SignalHandler *SignalHandler::s_singleton;

// We may need to write out signal info using just the write() function
// so we create an array of C strings + measure their lengths.
#define SIG_STR_COUNT 256
char *sig_str[SIG_STR_COUNT];
uint sig_str_len[SIG_STR_COUNT];
std::array<std::string,SIG_STR_COUNT> sig_str;

static void sig_str_init(int sig, const char *name)
static void sig_str_init(size_t sig, const char *name)
{
if (sig < SIG_STR_COUNT)
{
char line[128];

if (sig_str[sig])
free(sig_str[sig]);
snprintf(line, 128, "Handling %s\n", name);
line[127] = '\0';
sig_str[sig] = strdup(line);
sig_str_len[sig] = strlen(line);
}
if (sig >= sig_str.size())
return;

sig_str[sig] = qPrintable(QString("Handling %1\n").arg(name));
}

static void sig_str_init(void)
{
for (int i = 0; i < SIG_STR_COUNT; i++)
{
sig_str[i] = nullptr;
for (size_t i = 0; i < sig_str.size(); i++)
sig_str_init(i, qPrintable(QString("Signal %1").arg(i)));
}
}

QList<int> SignalHandler::s_defaultHandlerList;
Expand Down Expand Up @@ -88,7 +78,7 @@ SignalHandler::SignalHandler(QList<int> &signallist, QObject *parent) :
s_defaultHandlerList << SIGRTMIN;
#endif

if (::socketpair(AF_UNIX, SOCK_STREAM, 0, s_sigFd))
if (::socketpair(AF_UNIX, SOCK_STREAM, 0, s_sigFd.data()))
{
cerr << "Couldn't create socketpair" << endl;
return;
Expand Down Expand Up @@ -266,8 +256,8 @@ void SignalHandler::signalHandler(int signum, siginfo_t *info, void *context)
// we need to stick to system calls that are known to be
// signal-safe. write is, the other two aren't.
int d = 0;
if (signum < SIG_STR_COUNT)
d+=::write(STDERR_FILENO, sig_str[signum], sig_str_len[signum]);
if (signum < static_cast<int>(sig_str.size()))
d+=::write(STDERR_FILENO, sig_str[signum].c_str(), sig_str[signum].size());
(void) d; // quiet ignoring return value warning.
}

Expand Down
3 changes: 2 additions & 1 deletion mythtv/libs/libmythbase/signalhandling.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <QList>
#include <QMap>

#include <array>
#include <cstdint>
#include <csignal>
#include <unistd.h>
Expand Down Expand Up @@ -47,7 +48,7 @@ class MBASE_PUBLIC SignalHandler: public QObject
~SignalHandler() override;
void SetHandlerPrivate(int signum, SigHandlerFunc handler);

static int s_sigFd[2];
static std::array<int,2> s_sigFd;
static volatile bool s_exit_program;
QSocketNotifier *m_notifier {nullptr};
char *m_sigStack {nullptr};
Expand Down
14 changes: 7 additions & 7 deletions mythtv/libs/libmythbase/unzip.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1084,7 +1084,7 @@ UnZip::ErrorCode UnzipPrivate::extractFile(const QString& path, ZipEntryP& entry
return UnZip::SeekFailed;

// Encryption keys
quint32 keys[3];
keyset keys;

if (entry.isEncrypted())
{
Expand Down Expand Up @@ -1289,7 +1289,7 @@ int UnzipPrivate::decryptByte(quint32 key2)
/*!
\internal Update the encryption keys with the next byte of plain text
*/
void UnzipPrivate::updateKeys(quint32* keys, int c) const
void UnzipPrivate::updateKeys(keyset keys, int c) const
{
keys[0] = CRC32(keys[0], c);
keys[1] += keys[0] & 0xff;
Expand All @@ -1301,7 +1301,7 @@ void UnzipPrivate::updateKeys(quint32* keys, int c) const
\internal Initialize the encryption keys and the random header according to
the given password.
*/
void UnzipPrivate::initKeys(const QString& pwd, quint32* keys) const
void UnzipPrivate::initKeys(const QString& pwd, keyset keys) const
{
keys[0] = 305419896L;
keys[1] = 591751049L;
Expand All @@ -1320,7 +1320,7 @@ void UnzipPrivate::initKeys(const QString& pwd, quint32* keys) const
The \p file parameter can be used in the user interface or for debugging purposes
as it is the name of the encrypted file for wich the password is being tested.
*/
UnZip::ErrorCode UnzipPrivate::testPassword(quint32* keys, const QString& file, const ZipEntryP& header)
UnZip::ErrorCode UnzipPrivate::testPassword(keyset keys, const QString& file, const ZipEntryP& header)
{
Q_UNUSED(file);

Expand All @@ -1339,7 +1339,7 @@ UnZip::ErrorCode UnzipPrivate::testPassword(quint32* keys, const QString& file,
/*!
\internal Tests a set of keys on the encryption header.
*/
bool UnzipPrivate::testKeys(const ZipEntryP& header, quint32* keys)
bool UnzipPrivate::testKeys(const ZipEntryP& header, keyset keys)
{
char lastByte = 0;

Expand All @@ -1358,7 +1358,7 @@ bool UnzipPrivate::testKeys(const ZipEntryP& header, quint32* keys)
/*!
\internal Decrypts an array of bytes long \p read.
*/
void UnzipPrivate::decryptBytes(quint32* keys, char* buffer, qint64 read) const
void UnzipPrivate::decryptBytes(keyset keys, char* buffer, qint64 read) const
{
for (int i=0; i<(int)read; ++i)
updateKeys(keys, buffer[i] ^= decryptByte(keys[2]));
Expand All @@ -1367,7 +1367,7 @@ void UnzipPrivate::decryptBytes(quint32* keys, char* buffer, qint64 read) const
/*!
\internal Converts date and time values from ZIP format to a QDateTime object.
*/
QDateTime UnzipPrivate::convertDateTime(const unsigned char date[2], const unsigned char time[2])
QDateTime UnzipPrivate::convertDateTime(const std::array<uint8_t,2> &date, const std::array<uint8_t,2> &time)
{
QDateTime dt;

Expand Down
14 changes: 8 additions & 6 deletions mythtv/libs/libmythbase/unzip_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@

#include <QtGlobal>

using keyset = std::array<quint32,3>;

// zLib authors suggest using larger buffers (128K or 256K) for (de)compression (especially for inflate())
// we use a 256K buffer here - if you want to use this code on a pre-iceage mainframe please change it ;)
#define UNZIP_READ_BUFFER (256*1024)
Expand Down Expand Up @@ -92,21 +94,21 @@ class UnzipPrivate
UnZip::ErrorCode extractFile(const QString& path, ZipEntryP& entry, const QDir& dir, UnZip::ExtractionOptions options);
UnZip::ErrorCode extractFile(const QString& path, ZipEntryP& entry, QIODevice* device, UnZip::ExtractionOptions options);

UnZip::ErrorCode testPassword(quint32* keys, const QString& file, const ZipEntryP& header);
bool testKeys(const ZipEntryP& header, quint32* keys);
UnZip::ErrorCode testPassword(keyset keys, const QString& file, const ZipEntryP& header);
bool testKeys(const ZipEntryP& header, keyset keys);

bool createDirectory(const QString& path);

inline void decryptBytes(quint32* keys, char* buffer, qint64 read) const;
inline void decryptBytes(keyset keys, char* buffer, qint64 read) const;

static inline quint32 getULong(const unsigned char* data, quint32 offset) ;
static inline quint64 getULLong(const unsigned char* data, quint32 offset) ;
static inline quint16 getUShort(const unsigned char* data, quint32 offset) ;
static inline int decryptByte(quint32 key2) ;
inline void updateKeys(quint32* keys, int c) const;
inline void initKeys(const QString& pwd, quint32* keys) const;
inline void updateKeys(keyset keys, int c) const;
inline void initKeys(const QString& pwd, keyset keys) const;

static inline QDateTime convertDateTime(const unsigned char date[2], const unsigned char time[2]) ;
static inline QDateTime convertDateTime(const std::array<uint8_t,2> &date, const std::array<uint8_t,2> &time) ;
};

#endif // OSDAB_UNZIP_P_H
14 changes: 8 additions & 6 deletions mythtv/libs/libmythbase/zipentry_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
#ifndef OSDAB_ZIPENTRY_P_H
#define OSDAB_ZIPENTRY_P_H

#include <array>

#include <QtGlobal>
#include <QString>

Expand All @@ -49,21 +51,21 @@ class ZipEntryP
{
lhOffset = 0;
dataOffset = 0;
gpFlag[0] = gpFlag[1] = 0;
gpFlag.fill(0);
compMethod = 0;
modTime[0] = modTime[1] = 0;
modDate[0] = modDate[1] = 0;
modTime.fill(0);
modDate.fill(0);
crc = 0;
szComp = szUncomp = 0;
lhEntryChecked = false;
}

quint32 lhOffset; // Offset of the local header record for this entry
quint32 dataOffset; // Offset of the file data for this entry
unsigned char gpFlag[2] {}; // General purpose flag
std::array<uint8_t,2> gpFlag {}; // General purpose flag
quint16 compMethod; // Compression method
unsigned char modTime[2] {}; // Last modified time
unsigned char modDate[2] {}; // Last modified date
std::array<uint8_t,2> modTime {}; // Last modified time
std::array<uint8_t,2> modDate {}; // Last modified date
quint32 crc; // CRC32
quint32 szComp; // Compressed file size
quint32 szUncomp; // Uncompressed file size
Expand Down
1 change: 0 additions & 1 deletion mythtv/libs/libmythtv/recorders/DeviceReadBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ using namespace std;

#include "DeviceReadBuffer.h"
#include "mythcorecontext.h"
#include "mythbaseutil.h"
#include "mythlogging.h"
#include "tspacket.h"
#include "mthread.h"
Expand Down
5 changes: 3 additions & 2 deletions mythtv/libs/libmythtv/recorders/DeviceReadBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <QWaitCondition>
#include <QString>

#include "mythbaseutil.h"
#include "mythtimer.h"
#include "tspacket.h"
#include "mthread.h"
Expand Down Expand Up @@ -84,8 +85,8 @@ class DeviceReadBuffer : protected MThread

QString m_videoDevice;
int m_streamFd {-1};
mutable int m_wakePipe[2] {-1,-1};
mutable long m_wakePipeFlags[2] {0,0};
mutable pipe_fd_array m_wakePipe {-1,-1};
mutable pipe_flag_array m_wakePipeFlags {0,0};

DeviceReaderCB *m_readerCB {nullptr};

Expand Down