[vlc-commits] chromecast: escape meta sent via JSON

2018-02-22 Thread Thomas Guillem
vlc/vlc-3.0 | branch: master | Thomas Guillem  | Wed Feb 21 
09:30:46 2018 +0100| [a6afb7c1cd6a0546906945cd56c55e665b0a8637] | committer: 
Thomas Guillem

chromecast: escape meta sent via JSON

(cherry picked from commit d26e9a1b266aead0b2692b4a1081136cf6ccea69)
Signed-off-by: Thomas Guillem 

> http://git.videolan.org/gitweb.cgi/vlc/vlc-3.0.git/?a=commit;h=a6afb7c1cd6a0546906945cd56c55e665b0a8637
---

 .../chromecast/chromecast_communication.cpp| 91 ++
 1 file changed, 58 insertions(+), 33 deletions(-)

diff --git a/modules/stream_out/chromecast/chromecast_communication.cpp 
b/modules/stream_out/chromecast/chromecast_communication.cpp
index ed62681f21..90550c1616 100644
--- a/modules/stream_out/chromecast/chromecast_communication.cpp
+++ b/modules/stream_out/chromecast/chromecast_communication.cpp
@@ -32,6 +32,8 @@
 # include 
 #endif
 
+#include 
+
 ChromecastCommunication::ChromecastCommunication( vlc_object_t* p_module, 
const char* targetIP, unsigned int devicePort )
 : m_module( p_module )
 , m_creds( NULL )
@@ -255,6 +257,29 @@ unsigned ChromecastCommunication::msgPlayerGetStatus( 
const std::string& destina
 return pushMediaPlayerMessage( destinationId, ss ) == VLC_SUCCESS ? id : 
kInvalidId;
 }
 
+static std::string escape_json(const std::string )
+{
+/* Control characters ('\x00' to '\x1f'), '"' and '\"  must be escaped */
+std::ostringstream o;
+for (std::string::const_iterator c = s.begin(); c != s.end(); c++)
+{
+if (*c == '"' || *c == '\\' || ('\x00' <= *c && *c <= '\x1f'))
+o << "\\u"
+  << std::hex << std::setw(4) << std::setfill('0') << (int)*c;
+else
+o << *c;
+}
+return o.str();
+}
+
+static std::string meta_get_escaped(const vlc_meta_t *p_meta, vlc_meta_type_t 
type)
+{
+const char *psz = vlc_meta_Get(p_meta, type);
+if (!psz)
+return std::string();
+return escape_json(std::string(psz));
+}
+
 std::string ChromecastCommunication::GetMedia( unsigned int i_port,
const std::string& mime,
const vlc_meta_t *p_meta )
@@ -263,55 +288,55 @@ std::string ChromecastCommunication::GetMedia( unsigned 
int i_port,
 
 bool b_music = strncmp(mime.c_str(), "audio", strlen("audio")) == 0;
 
-const char *psz_title = NULL;
-const char *psz_artwork = NULL;
-const char *psz_artist = NULL;
-const char *psz_album = NULL;
-const char *psz_albumartist = NULL;
-const char *psz_tracknumber = NULL;
-const char *psz_discnumber = NULL;
+std::string title;
+std::string artwork;
+std::string artist;
+std::string album;
+std::string albumartist;
+std::string tracknumber;
+std::string discnumber;
 
 if( p_meta )
 {
-psz_title = vlc_meta_Get( p_meta, vlc_meta_Title );
-psz_artwork = vlc_meta_Get( p_meta, vlc_meta_ArtworkURL );
+title = meta_get_escaped( p_meta, vlc_meta_Title );
+artwork = meta_get_escaped( p_meta, vlc_meta_ArtworkURL );
 
-if( b_music && psz_title )
+if( b_music && !title.empty() )
 {
-psz_artist = vlc_meta_Get( p_meta, vlc_meta_Artist );
-psz_album = vlc_meta_Get( p_meta, vlc_meta_Album );
-psz_albumartist = vlc_meta_Get( p_meta, vlc_meta_AlbumArtist );
-psz_tracknumber = vlc_meta_Get( p_meta, vlc_meta_TrackNumber );
-psz_discnumber = vlc_meta_Get( p_meta, vlc_meta_DiscNumber );
+artist = meta_get_escaped( p_meta, vlc_meta_Artist );
+album = meta_get_escaped( p_meta, vlc_meta_Album );
+albumartist = meta_get_escaped( p_meta, vlc_meta_AlbumArtist );
+tracknumber = meta_get_escaped( p_meta, vlc_meta_TrackNumber );
+discnumber = meta_get_escaped( p_meta, vlc_meta_DiscNumber );
 }
-if( !psz_title )
+if( title.empty() )
 {
-psz_title = vlc_meta_Get( p_meta, vlc_meta_NowPlaying );
-if( !psz_title )
-psz_title = vlc_meta_Get( p_meta, vlc_meta_ESNowPlaying );
+title = meta_get_escaped( p_meta, vlc_meta_NowPlaying );
+if( title.empty() )
+title = meta_get_escaped( p_meta, vlc_meta_ESNowPlaying );
 }
 
-if ( psz_title )
+if ( !title.empty() )
 {
 ss << "\"metadata\":{"
<< " \"metadataType\":" << ( b_music ? "3" : "0" )
-   << ",\"title\":\"" << psz_title << "\"";
+   << ",\"title\":\"" << title << "\"";
 if( b_music )
 {
-if( psz_artist )
-ss << ",\"artist\":\"" << psz_artist << "\"";
-if( psz_album )
-ss << ",\"album\":\"" << psz_album << "\"";
-if( psz_albumartist )
-ss << 

[vlc-commits] chromecast: escape meta sent via JSON

2018-02-22 Thread Thomas Guillem
vlc | branch: master | Thomas Guillem  | Wed Feb 21 09:30:46 
2018 +0100| [d26e9a1b266aead0b2692b4a1081136cf6ccea69] | committer: Thomas 
Guillem

chromecast: escape meta sent via JSON

> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=d26e9a1b266aead0b2692b4a1081136cf6ccea69
---

 .../chromecast/chromecast_communication.cpp| 91 ++
 1 file changed, 58 insertions(+), 33 deletions(-)

diff --git a/modules/stream_out/chromecast/chromecast_communication.cpp 
b/modules/stream_out/chromecast/chromecast_communication.cpp
index 66d08d731c..2574df3a46 100644
--- a/modules/stream_out/chromecast/chromecast_communication.cpp
+++ b/modules/stream_out/chromecast/chromecast_communication.cpp
@@ -32,6 +32,8 @@
 # include 
 #endif
 
+#include 
+
 ChromecastCommunication::ChromecastCommunication( vlc_object_t* p_module, 
const char* targetIP, unsigned int devicePort )
 : m_module( p_module )
 , m_creds( NULL )
@@ -253,6 +255,29 @@ unsigned ChromecastCommunication::msgPlayerGetStatus( 
const std::string& destina
 return pushMediaPlayerMessage( destinationId, ss ) == VLC_SUCCESS ? id : 
kInvalidId;
 }
 
+static std::string escape_json(const std::string )
+{
+/* Control characters ('\x00' to '\x1f'), '"' and '\"  must be escaped */
+std::ostringstream o;
+for (std::string::const_iterator c = s.begin(); c != s.end(); c++)
+{
+if (*c == '"' || *c == '\\' || ('\x00' <= *c && *c <= '\x1f'))
+o << "\\u"
+  << std::hex << std::setw(4) << std::setfill('0') << (int)*c;
+else
+o << *c;
+}
+return o.str();
+}
+
+static std::string meta_get_escaped(const vlc_meta_t *p_meta, vlc_meta_type_t 
type)
+{
+const char *psz = vlc_meta_Get(p_meta, type);
+if (!psz)
+return std::string();
+return escape_json(std::string(psz));
+}
+
 std::string ChromecastCommunication::GetMedia( unsigned int i_port,
const std::string& mime,
const vlc_meta_t *p_meta )
@@ -261,55 +286,55 @@ std::string ChromecastCommunication::GetMedia( unsigned 
int i_port,
 
 bool b_music = strncmp(mime.c_str(), "audio", strlen("audio")) == 0;
 
-const char *psz_title = NULL;
-const char *psz_artwork = NULL;
-const char *psz_artist = NULL;
-const char *psz_album = NULL;
-const char *psz_albumartist = NULL;
-const char *psz_tracknumber = NULL;
-const char *psz_discnumber = NULL;
+std::string title;
+std::string artwork;
+std::string artist;
+std::string album;
+std::string albumartist;
+std::string tracknumber;
+std::string discnumber;
 
 if( p_meta )
 {
-psz_title = vlc_meta_Get( p_meta, vlc_meta_Title );
-psz_artwork = vlc_meta_Get( p_meta, vlc_meta_ArtworkURL );
+title = meta_get_escaped( p_meta, vlc_meta_Title );
+artwork = meta_get_escaped( p_meta, vlc_meta_ArtworkURL );
 
-if( b_music && psz_title )
+if( b_music && !title.empty() )
 {
-psz_artist = vlc_meta_Get( p_meta, vlc_meta_Artist );
-psz_album = vlc_meta_Get( p_meta, vlc_meta_Album );
-psz_albumartist = vlc_meta_Get( p_meta, vlc_meta_AlbumArtist );
-psz_tracknumber = vlc_meta_Get( p_meta, vlc_meta_TrackNumber );
-psz_discnumber = vlc_meta_Get( p_meta, vlc_meta_DiscNumber );
+artist = meta_get_escaped( p_meta, vlc_meta_Artist );
+album = meta_get_escaped( p_meta, vlc_meta_Album );
+albumartist = meta_get_escaped( p_meta, vlc_meta_AlbumArtist );
+tracknumber = meta_get_escaped( p_meta, vlc_meta_TrackNumber );
+discnumber = meta_get_escaped( p_meta, vlc_meta_DiscNumber );
 }
-if( !psz_title )
+if( title.empty() )
 {
-psz_title = vlc_meta_Get( p_meta, vlc_meta_NowPlaying );
-if( !psz_title )
-psz_title = vlc_meta_Get( p_meta, vlc_meta_ESNowPlaying );
+title = meta_get_escaped( p_meta, vlc_meta_NowPlaying );
+if( title.empty() )
+title = meta_get_escaped( p_meta, vlc_meta_ESNowPlaying );
 }
 
-if ( psz_title )
+if ( !title.empty() )
 {
 ss << "\"metadata\":{"
<< " \"metadataType\":" << ( b_music ? "3" : "0" )
-   << ",\"title\":\"" << psz_title << "\"";
+   << ",\"title\":\"" << title << "\"";
 if( b_music )
 {
-if( psz_artist )
-ss << ",\"artist\":\"" << psz_artist << "\"";
-if( psz_album )
-ss << ",\"album\":\"" << psz_album << "\"";
-if( psz_albumartist )
-ss << ",\"albumArtist\":\"" << psz_albumartist << "\"";
-if( psz_tracknumber )
-ss << ",\"trackNumber\":\"" <<