Skip to content

Commit

Permalink
Convert to std::array and std::string. (libmythtv/DVD etc.)
Browse files Browse the repository at this point in the history
  • Loading branch information
linuxdude42 committed Aug 28, 2020
1 parent 818eced commit 18a6515
Show file tree
Hide file tree
Showing 8 changed files with 122 additions and 115 deletions.
19 changes: 7 additions & 12 deletions mythtv/libs/libmythtv/AirPlay/mythairplayserver.cpp
Expand Up @@ -138,23 +138,18 @@ QString AirPlayHardwareId()

QString GenerateNonce(void)
{
int nonceParts[4];
QString nonce;
#if QT_VERSION >= QT_VERSION_CHECK(5,10,0)
auto *randgen = QRandomGenerator::global();
nonceParts[0] = randgen->generate();
nonceParts[1] = randgen->generate();
nonceParts[2] = randgen->generate();
nonceParts[3] = randgen->generate();
std::array<uint32_t,4> nonceParts {
randgen->generate(), randgen->generate(),
randgen->generate(), randgen->generate() };
#else
QTime time = QTime::currentTime();
qsrand((uint)time.msec());
nonceParts[0] = qrand();
nonceParts[1] = qrand();
nonceParts[2] = qrand();
nonceParts[3] = qrand();
std::srand(std::time(nullptr));
std::array<int32_t,4> nonceParts {
std::rand(), std::rand(), std::rand(), std::rand() };
#endif

QString nonce;
nonce = QString::number(nonceParts[0], 16).toUpper();
nonce += QString::number(nonceParts[1], 16).toUpper();
nonce += QString::number(nonceParts[2], 16).toUpper();
Expand Down
76 changes: 36 additions & 40 deletions mythtv/libs/libmythtv/AirPlay/mythraopconnection.cpp
Expand Up @@ -440,16 +440,14 @@ void MythRAOPConnection::SendResendRequest(uint64_t timestamp,
QString("Missed %1 packet(s): expected %2 got %3 ts:%4")
.arg(missed).arg(expected).arg(got).arg(timestamp));

unsigned char req[8];
req[0] = 0x80;
req[1] = RANGE_RESEND | 0x80;
*(uint16_t *)(req + 2) = qToBigEndian(m_seqNum++);
*(uint16_t *)(req + 4) = qToBigEndian(expected); // missed seqnum
*(uint16_t *)(req + 6) = qToBigEndian(missed); // count

if (m_clientControlSocket->writeDatagram((char *)req, sizeof(req),
std::array<uint8_t,8> req { 0x80, RANGE_RESEND | 0x80};
*(uint16_t *)(&req[2]) = qToBigEndian(m_seqNum++);
*(uint16_t *)(&req[4]) = qToBigEndian(expected); // missed seqnum
*(uint16_t *)(&req[6]) = qToBigEndian(missed); // count

if (m_clientControlSocket->writeDatagram((char *)req.data(), req.size(),
m_peerAddress, m_clientControlPort)
== sizeof(req))
== (qint64)req.size())
{
for (uint16_t count = 0; count < missed; count++)
{
Expand Down Expand Up @@ -497,21 +495,17 @@ void MythRAOPConnection::SendTimeRequest(void)
timeval t {};
gettimeofday(&t, nullptr);

unsigned char req[32];
req[0] = 0x80;
req[1] = TIMING_REQUEST | 0x80;
// this is always 0x00 0x07 according to http://blog.technologeek.org/airtunes-v2
// no other value works
req[2] = 0x00;
req[3] = 0x07;
*(uint32_t *)(req + 4) = (uint32_t)0;
*(uint64_t *)(req + 8) = (uint64_t)0;
*(uint64_t *)(req + 16) = (uint64_t)0;
*(uint32_t *)(req + 24) = qToBigEndian((uint32_t)t.tv_sec);
*(uint32_t *)(req + 28) = qToBigEndian((uint32_t)t.tv_usec);

if (m_clientTimingSocket->writeDatagram((char *)req, sizeof(req), m_peerAddress,
m_clientTimingPort) != sizeof(req))
std::array<uint8_t,32> req {
0x80, TIMING_REQUEST | 0x80,
// this is always 0x00 0x07 according to http://blog.technologeek.org/airtunes-v2
// no other value works
0x00, 0x07
};
*(uint32_t *)(&req[24]) = qToBigEndian((uint32_t)t.tv_sec);
*(uint32_t *)(&req[28]) = qToBigEndian((uint32_t)t.tv_usec);

if (m_clientTimingSocket->writeDatagram((char *)req.data(), req.size(), m_peerAddress,
m_clientTimingPort) != (qint64)req.size())
{
LOG(VB_PLAYBACK, LOG_ERR, LOC + "Failed to send resend time request.");
return;
Expand Down Expand Up @@ -613,19 +607,20 @@ uint32_t MythRAOPConnection::decodeAudioPacket(uint8_t type,
return -1;

int aeslen = len & ~0xf;
unsigned char iv[16];
unsigned char decrypted_data[MAX_PACKET_SIZE];
memcpy(iv, m_aesIV.constData(), sizeof(iv));
std::array<uint8_t,16> iv {};
std::array<uint8_t,MAX_PACKET_SIZE> decrypted_data {};
std::copy(m_aesIV.cbegin(), m_aesIV.cbegin() + iv.size(), iv.data());
AES_cbc_encrypt((const unsigned char *)data_in,
decrypted_data, aeslen,
&m_aesKey, iv, AES_DECRYPT);
memcpy(decrypted_data + aeslen, data_in + aeslen, len - aeslen);
decrypted_data.data(), aeslen,
&m_aesKey, iv.data(), AES_DECRYPT);
std::copy(data_in + aeslen, data_in + len - aeslen,
decrypted_data.data() + aeslen);

AVPacket tmp_pkt;
AVCodecContext *ctx = m_codecContext;

av_init_packet(&tmp_pkt);
tmp_pkt.data = decrypted_data;
tmp_pkt.data = decrypted_data.data();
tmp_pkt.size = len;

uint32_t frames_added = 0;
Expand Down Expand Up @@ -971,15 +966,16 @@ void MythRAOPConnection::ProcessRequest(const QStringList &header,
}

int i = 0;
unsigned char from[38];
memcpy(from, challenge.constData(), challenge_size);
std::array<uint8_t,38> from {};
std::copy(challenge.cbegin(), challenge.cbegin() + challenge_size,
from.data());
i += challenge_size;
if (m_socket->localAddress().protocol() ==
QAbstractSocket::IPv4Protocol)
{
uint32_t ip = m_socket->localAddress().toIPv4Address();
ip = qToBigEndian(ip);
memcpy(from + i, &ip, 4);
memcpy(&from[i], &ip, 4);
i += 4;
}
else if (m_socket->localAddress().protocol() ==
Expand All @@ -990,31 +986,31 @@ void MythRAOPConnection::ProcessRequest(const QStringList &header,
"\x00\x00\x00\x00" "\x00\x00\x00\x00" "\x00\x00\xff\xff",
12) == 0)
{
memcpy(from + i, &ip[12], 4);
memcpy(&from[i], &ip[12], 4);
i += 4;
}
else
{
memcpy(from + i, &ip, 16);
memcpy(&from[i], &ip, 16);
i += 16;
}
}
memcpy(from + i, m_hardwareId.constData(), AIRPLAY_HARDWARE_ID_SIZE);
memcpy(&from[i], m_hardwareId.constData(), AIRPLAY_HARDWARE_ID_SIZE);
i += AIRPLAY_HARDWARE_ID_SIZE;

int pad = 32 - i;
if (pad > 0)
{
memset(from + i, 0, pad);
memset(&from[i], 0, pad);
i += pad;
}

LOG(VB_PLAYBACK, LOG_DEBUG, LOC +
QString("Full base64 response: '%1' size %2")
.arg(QByteArray((const char *)from, i).toBase64().constData())
.arg(QByteArray((char *)from.data(), i).toBase64().constData())
.arg(i));

RSA_private_encrypt(i, from, to, LoadKey(), RSA_PKCS1_PADDING);
RSA_private_encrypt(i, from.data(), to, LoadKey(), RSA_PKCS1_PADDING);

QByteArray base64 = QByteArray((const char *)to, tosize).toBase64();
delete[] to;
Expand Down
64 changes: 33 additions & 31 deletions mythtv/libs/libmythtv/DVD/mythdvdbuffer.cpp
Expand Up @@ -22,10 +22,10 @@
#define IncrementButtonVersion if (++m_buttonVersion > 1024) m_buttonVersion = 1;
#define DVD_DRIVE_SPEED 1

static const char *DVDMenuTable[] =
static const std::array<const std::string,8> DVDMenuTable
{
nullptr,
nullptr,
"",
"",
QT_TRANSLATE_NOOP("(DVD menu)", "Title Menu"),
QT_TRANSLATE_NOOP("(DVD menu)", "Root Menu"),
QT_TRANSLATE_NOOP("(DVD menu)", "Subpicture Menu"),
Expand All @@ -35,11 +35,13 @@ static const char *DVDMenuTable[] =
QT_TRANSLATE_NOOP("(DVD menu)", "Part Menu")
};

const QMap<int, int> MythDVDBuffer::kSeekSpeedMap =
{ { 3, 1 }, { 5, 2 }, { 10, 4 }, { 20, 8 },
{ 30, 10 }, { 60, 15 }, { 120, 20 }, { 180, 60 } };

MythDVDBuffer::MythDVDBuffer(const QString &Filename)
: MythOpticalBuffer(kMythBufferDVD)
{
m_seekSpeedMap = { { 3, 1 }, { 5, 2 }, { 10, 4 }, { 20, 8 },
{ 30, 10 }, { 60, 15 }, { 120, 20 }, { 180, 60 } };
MythDVDBuffer::OpenFile(Filename);
}

Expand Down Expand Up @@ -172,8 +174,8 @@ long long MythDVDBuffer::Seek(long long Time)

if (ffrewSkip != 1 && ffrewSkip != 0 && Time != 0)
{
auto it = m_seekSpeedMap.lowerBound(static_cast<int>(labs(Time)));
if (it == m_seekSpeedMap.end())
auto it = kSeekSpeedMap.lowerBound(static_cast<int>(labs(Time)));
if (it == kSeekSpeedMap.end())
seekSpeed = *(it - 1);
else
seekSpeed = *it;
Expand Down Expand Up @@ -235,8 +237,8 @@ void MythDVDBuffer::GetDescForPos(QString &Description) const
{
if (m_inMenu)
{
if ((m_part <= DVD_MENU_MAX) && DVDMenuTable[m_part])
Description = QCoreApplication::translate("(DVD menu)", DVDMenuTable[m_part]);
if ((m_part <= DVD_MENU_MAX) && !DVDMenuTable[m_part].empty())
Description = QCoreApplication::translate("(DVD menu)", DVDMenuTable[m_part].c_str());
}
else
{
Expand Down Expand Up @@ -533,7 +535,7 @@ int MythDVDBuffer::SafeRead(void *Buffer, uint Size)

while ((m_processState != PROCESS_WAIT) && needed)
{
blockBuf = m_dvdBlockWriteBuf;
blockBuf = m_dvdBlockWriteBuf.data();

if (m_processState == PROCESS_REPROCESS)
{
Expand Down Expand Up @@ -567,7 +569,7 @@ int MythDVDBuffer::SafeRead(void *Buffer, uint Size)
}

// release buffer
if (blockBuf != m_dvdBlockWriteBuf)
if (blockBuf != m_dvdBlockWriteBuf.data())
dvdnav_free_cache_block(m_dvdnav, blockBuf);

// debug
Expand Down Expand Up @@ -660,7 +662,7 @@ int MythDVDBuffer::SafeRead(void *Buffer, uint Size)
IncrementButtonVersion

// release buffer
if (blockBuf != m_dvdBlockWriteBuf)
if (blockBuf != m_dvdBlockWriteBuf.data())
dvdnav_free_cache_block(m_dvdnav, blockBuf);
}
break;
Expand All @@ -672,9 +674,11 @@ int MythDVDBuffer::SafeRead(void *Buffer, uint Size)
LOG(VB_PLAYBACK, LOG_DEBUG, LOC + "DVDNAV_SPU_CLUT_CHANGE");

// store the new clut
memcpy(m_clut, blockBuf, 16 * sizeof(uint32_t));
// m_clut = std::to_array(blockBuf); // C++20
std::copy(blockBuf, blockBuf + 16 * sizeof(uint32_t),
m_clut.data());
// release buffer
if (blockBuf != m_dvdBlockWriteBuf)
if (blockBuf != m_dvdBlockWriteBuf.data())
dvdnav_free_cache_block(m_dvdnav, blockBuf);
}
break;
Expand All @@ -701,7 +705,7 @@ int MythDVDBuffer::SafeRead(void *Buffer, uint Size)
.arg(spu->physical_pan_scan).arg(m_curSubtitleTrack));

// release buffer
if (blockBuf != m_dvdBlockWriteBuf)
if (blockBuf != m_dvdBlockWriteBuf.data())
dvdnav_free_cache_block(m_dvdnav, blockBuf);
}
break;
Expand Down Expand Up @@ -729,7 +733,7 @@ int MythDVDBuffer::SafeRead(void *Buffer, uint Size)
}

// release buffer
if (blockBuf != m_dvdBlockWriteBuf)
if (blockBuf != m_dvdBlockWriteBuf.data())
dvdnav_free_cache_block(m_dvdnav, blockBuf);
}
break;
Expand Down Expand Up @@ -881,7 +885,7 @@ int MythDVDBuffer::SafeRead(void *Buffer, uint Size)
}
}
// release buffer
if (blockBuf != m_dvdBlockWriteBuf)
if (blockBuf != m_dvdBlockWriteBuf.data())
dvdnav_free_cache_block(m_dvdnav, blockBuf);
}
break;
Expand Down Expand Up @@ -939,7 +943,7 @@ int MythDVDBuffer::SafeRead(void *Buffer, uint Size)
m_lastcellid = m_cellid = 0;

// release buffer
if (blockBuf != m_dvdBlockWriteBuf)
if (blockBuf != m_dvdBlockWriteBuf.data())
dvdnav_free_cache_block(m_dvdnav, blockBuf);
}
break;
Expand All @@ -966,7 +970,7 @@ int MythDVDBuffer::SafeRead(void *Buffer, uint Size)
.arg(highlight->pts).arg(highlight->buttonN));

// release buffer
if (blockBuf != m_dvdBlockWriteBuf)
if (blockBuf != m_dvdBlockWriteBuf.data())
dvdnav_free_cache_block(m_dvdnav, blockBuf);
}
break;
Expand Down Expand Up @@ -1018,7 +1022,7 @@ int MythDVDBuffer::SafeRead(void *Buffer, uint Size)
}

// release buffer
if (blockBuf != m_dvdBlockWriteBuf)
if (blockBuf != m_dvdBlockWriteBuf.data())
dvdnav_free_cache_block(m_dvdnav, blockBuf);
}
}
Expand Down Expand Up @@ -1053,7 +1057,7 @@ int MythDVDBuffer::SafeRead(void *Buffer, uint Size)
}

// release buffer
if (blockBuf != m_dvdBlockWriteBuf)
if (blockBuf != m_dvdBlockWriteBuf.data())
dvdnav_free_cache_block(m_dvdnav, blockBuf);
}
}
Expand All @@ -1067,7 +1071,7 @@ int MythDVDBuffer::SafeRead(void *Buffer, uint Size)
m_gotStop = true;

// release buffer
if (blockBuf != m_dvdBlockWriteBuf)
if (blockBuf != m_dvdBlockWriteBuf.data())
dvdnav_free_cache_block(m_dvdnav, blockBuf);
}
break;
Expand Down Expand Up @@ -1448,8 +1452,8 @@ bool MythDVDBuffer::DecodeSubtitles(AVSubtitle *Subtitle, int *GotSubtitles,
{
#define GETBE16(p) (((p)[0] << 8) | (p)[1])

uint8_t alpha[4] = {0, 0, 0, 0};
uint8_t palette[4] = {0, 0, 0, 0};
AlphaArray alpha {0, 0, 0, 0};
PaletteArray palette {0, 0, 0, 0};

if (!SpuPkt)
return false;
Expand Down Expand Up @@ -1811,10 +1815,8 @@ uint MythDVDBuffer::ConvertLangCode(uint16_t Code)
if (Code == 0)
return 0;

QChar str2[2];
str2[0] = QChar(Code >> 8);
str2[1] = QChar(Code & 0xff);
QString str3 = iso639_str2_to_str3(QString(str2, 2));
std::array<QChar,2> str2 { Code >> 8, Code & 0xff };
QString str3 = iso639_str2_to_str3(QString(str2.data(), str2.size()));

LOG(VB_PLAYBACK, LOG_INFO, LOC + QString("code: %1; iso639: %2").arg(Code).arg(str3));

Expand Down Expand Up @@ -1978,7 +1980,7 @@ int64_t MythDVDBuffer::GetCurrentTime(void) const
}

/// \brief converts palette values from YUV to RGB
void MythDVDBuffer::GuessPalette(uint32_t *RGBAPalette, const uint8_t *Palette, const uint8_t *Alpha)
void MythDVDBuffer::GuessPalette(uint32_t *RGBAPalette, const PaletteArray& Palette, const AlphaArray& Alpha)
{
memset(RGBAPalette, 0, 16);
for (int i = 0 ; i < 4 ; i++)
Expand Down Expand Up @@ -2053,7 +2055,7 @@ uint MythDVDBuffer::GetNibble(const uint8_t *Buffer, int NibbleOffset)
* \brief Obtained from ffmpeg dvdsubdec.c
* Used to find smallest bounded rectangle
*/
int MythDVDBuffer::IsTransparent(const uint8_t *Buffer, int Pitch, int Num, const uint8_t *Colors)
int MythDVDBuffer::IsTransparent(const uint8_t *Buffer, int Pitch, int Num, const ColorArray& Colors)
{
for (int i = 0; i < Num; i++)
{
Expand All @@ -2070,7 +2072,7 @@ int MythDVDBuffer::IsTransparent(const uint8_t *Buffer, int Pitch, int Num, cons
*/
int MythDVDBuffer::FindSmallestBoundingRectangle(AVSubtitle *Subtitle)
{
uint8_t colors[256] = { 0 };
ColorArray colors {};

if (Subtitle->num_rects == 0 || Subtitle->rects == nullptr ||
Subtitle->rects[0]->w <= 0 || Subtitle->rects[0]->h <= 0)
Expand Down

0 comments on commit 18a6515

Please sign in to comment.