Skip to content

Commit

Permalink
[ISM] reorganize ProtectionParser
Browse files Browse the repository at this point in the history
  • Loading branch information
peak3d committed May 23, 2019
1 parent 51662f7 commit 95b2ca7
Show file tree
Hide file tree
Showing 5 changed files with 174 additions and 96 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Expand Up @@ -16,6 +16,7 @@ set(ADP_SOURCES
src/parser/SmoothTree.cpp
src/parser/TTML.cpp
src/parser/WebVTT.cpp
src/parser/PRProtectionParser.cpp
src/common/AdaptiveStream.cpp
src/helpers.cpp
src/oscompat.cpp
Expand All @@ -38,6 +39,7 @@ set(ADP_HEADERS
src/parser/SmoothTree.h
src/parser/TTML.h
src/parser/WebVTT.h
src/parser/PRProtectionParser.h
src/TSReader.h
src/log.h
src/aes_decrypter.h
Expand Down
122 changes: 122 additions & 0 deletions src/parser/PRProtectionParser.cpp
@@ -0,0 +1,122 @@
/*
* Copyright (C) 2019 peak3d
* http://www.peak3d.de
*
* This Program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This Program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* <http://www.gnu.org/licenses/>.
*
*/

#include "PRProtectionParser.h"
#include "expat.h"
#include "../helpers.h"
#include <string.h>

namespace adaptive
{

/*----------------------------------------------------------------------
| expat protection start
+---------------------------------------------------------------------*/
static void XMLCALL
protection_start(void *data, const char *el, const char **attr)
{
PRProtectionParser *parser(reinterpret_cast<PRProtectionParser*>(data));
parser->m_strXMLText.clear();
}

/*----------------------------------------------------------------------
| expat protection text
+---------------------------------------------------------------------*/
static void XMLCALL
protection_text(void *data, const char *s, int len)
{
PRProtectionParser *parser(reinterpret_cast<PRProtectionParser*>(data));
parser->m_strXMLText += std::string(s, len);
}

/*----------------------------------------------------------------------
| expat protection end
+---------------------------------------------------------------------*/
static void XMLCALL
protection_end(void *data, const char *el)
{
PRProtectionParser *parser(reinterpret_cast<PRProtectionParser*>(data));
if (strcmp(el, "KID") == 0)
{
uint8_t buffer[32];
unsigned int buffer_size(32);
b64_decode(parser->m_strXMLText.data(), parser->m_strXMLText.size(), buffer, buffer_size);

if (buffer_size == 16)
{
char kid[17]; kid[16] = 0;
prkid2wvkid(reinterpret_cast<const char *>(buffer), kid);
parser->setKID(kid);
}
}
else if (strcmp(el, "LA_URL") == 0)
{
parser->setLicenseURL(parser->m_strXMLText);
}
}

PRProtectionParser::PRProtectionParser(std::string wwrmheader)
{
if (wwrmheader.empty())
return;

//(p)repair the content
std::string::size_type pos = 0;
while ((pos = wwrmheader.find('\n', 0)) != std::string::npos)
wwrmheader.erase(pos, 1);

while (wwrmheader.size() & 3)
wwrmheader += "=";

unsigned int xml_size = wwrmheader.size();
uint8_t *buffer = (uint8_t*)malloc(xml_size), *xml_start(buffer);

if (!b64_decode(wwrmheader.c_str(), xml_size, buffer, xml_size))
{
free(buffer);
return;
}

m_strPSSH = std::string(reinterpret_cast<char*>(buffer), xml_size);

while (xml_size && *xml_start != '<')
{
xml_start++;
xml_size--;
}

XML_Parser pp = XML_ParserCreate("UTF-16");
if (!pp)
{
free(buffer);
return;
}

XML_SetUserData(pp, (void*)this);
XML_SetElementHandler(pp, protection_start, protection_end);
XML_SetCharacterDataHandler(pp, protection_text);

bool done(false);
XML_Parse(pp, (const char*)(xml_start), xml_size, done);

XML_ParserFree(pp);
free(buffer);
}


}//namespace
44 changes: 44 additions & 0 deletions src/parser/PRProtectionParser.h
@@ -0,0 +1,44 @@
/*
* Copyright (C) 2019 peak3d
* http://www.peak3d.de
*
* This Program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This Program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* <http://www.gnu.org/licenses/>.
*
*/

#pragma once

#include <string>

namespace adaptive
{

class PRProtectionParser
{
public:
PRProtectionParser(std::string wrmheader);
std::string getKID() const { return m_strKID; };
std::string getLicenseURL() const { return m_strLicenseURL; };
std::string getPSSH() const { return m_strPSSH; };

void setKID(const std::string kid) { m_strKID = kid; };
void setLicenseURL(const std::string licenseURL) { m_strLicenseURL = licenseURL; };

std::string m_strXMLText;
private:
std::string m_strKID;
std::string m_strLicenseURL;
std::string m_strPSSH;
};

} // namespace
100 changes: 6 additions & 94 deletions src/parser/SmoothTree.cpp
Expand Up @@ -24,6 +24,7 @@

#include "SmoothTree.h"
#include "../oscompat.h"
#include "PRProtectionParser.h"
#include "../helpers.h"

using namespace adaptive;
Expand Down Expand Up @@ -279,7 +280,11 @@ end(void *data, const char *el)
else if (strcmp(el, "Protection") == 0)
{
dash->currentNode_ &= ~(SmoothTree::SSMNODE_PROTECTION| SmoothTree::SSMNODE_PROTECTIONTEXT);
dash->parse_protection();
PRProtectionParser parser(dash->strXMLText_);
dash->current_defaultKID_ = parser.getKID();
dash->license_url_ = parser.getLicenseURL();
dash->current_pssh_ = parser.getPSSH();
dash->strXMLText_.clear();
}
}
else if (dash->currentNode_ & SmoothTree::SSMNODE_STREAMINDEX)
Expand All @@ -302,50 +307,7 @@ end(void *data, const char *el)
}
}

/*----------------------------------------------------------------------
| expat protection start
+---------------------------------------------------------------------*/
static void XMLCALL
protection_start(void *data, const char *el, const char **attr)
{
SmoothTree *dash(reinterpret_cast<SmoothTree*>(data));
dash->strXMLText_.clear();
}

/*----------------------------------------------------------------------
| expat protection text
+---------------------------------------------------------------------*/
static void XMLCALL
protection_text(void *data, const char *s, int len)
{
SmoothTree *dash(reinterpret_cast<SmoothTree*>(data));
dash->strXMLText_ += std::string(s, len);
}

/*----------------------------------------------------------------------
| expat protection end
+---------------------------------------------------------------------*/
static void XMLCALL
protection_end(void *data, const char *el)
{
SmoothTree *dash(reinterpret_cast<SmoothTree*>(data));
if (strcmp(el, "KID") == 0)
{
uint8_t buffer[32];
unsigned int buffer_size(32);
b64_decode(dash->strXMLText_.data(), dash->strXMLText_.size(), buffer, buffer_size);

if (buffer_size == 16)
{
dash->current_defaultKID_.resize(16);
prkid2wvkid(reinterpret_cast<const char *>(buffer), &dash->current_defaultKID_[0]);
}
}
else if (strcmp(el, "LA_URL") == 0)
{
dash->license_url_ = dash->strXMLText_;
}
}

/*----------------------------------------------------------------------
| SmoothTree
Expand Down Expand Up @@ -410,53 +372,3 @@ bool SmoothTree::write_data(void *buffer, size_t buffer_size, void *opaque)
return false;
return true;
}

void SmoothTree::parse_protection()
{
if (strXMLText_.empty())
return;

//(p)repair the content
std::string::size_type pos = 0;
while ((pos = strXMLText_.find('\n', 0)) != std::string::npos)
strXMLText_.erase(pos, 1);

while (strXMLText_.size() & 3)
strXMLText_ += "=";

unsigned int xml_size = strXMLText_.size();
uint8_t *buffer = (uint8_t*)malloc(xml_size), *xml_start(buffer);

if (!b64_decode(strXMLText_.c_str(), xml_size, buffer, xml_size))
{
free(buffer);
return;
}

current_pssh_ = std::string(reinterpret_cast<char*>(buffer), xml_size);

while (xml_size && *xml_start != '<')
{
xml_start++;
xml_size--;
}

XML_Parser pp = XML_ParserCreate("UTF-16");
if (!pp)
{
free(buffer);
return;
}

XML_SetUserData(pp, (void*)this);
XML_SetElementHandler(pp, protection_start, protection_end);
XML_SetCharacterDataHandler(pp, protection_text);

bool done(false);
XML_Parse(pp, (const char*)(xml_start), xml_size, done);

XML_ParserFree(pp);
free(buffer);

strXMLText_.clear();
}
2 changes: 0 additions & 2 deletions src/parser/SmoothTree.h
Expand Up @@ -30,8 +30,6 @@ namespace adaptive
virtual bool open(const std::string &url, const std::string &manifestUpdateParam) override;
virtual bool write_data(void *buffer, size_t buffer_size, void *opaque) override;

void parse_protection();

enum
{
SSMNODE_SSM = 1 << 0,
Expand Down

0 comments on commit 95b2ca7

Please sign in to comment.