Bug 1318965 - Improves the logging functions provided in ClearKeyUtils r?cpearce draft
authorJay Harris <jharris@mozilla.com>
Wed, 21 Dec 2016 14:01:41 +1300
changeset 452465 fe1fd31faf3e53f4d1d1a3a08621280735162a0e
parent 452464 5f95b1f524b98b83fc0d962e84ca2325275068a2
child 540236 1f56a094687d8deda0e16d9764a1c838c3d3a7b3
push id39414
push userbmo:jharris@mozilla.com
push dateWed, 21 Dec 2016 20:40:40 +0000
reviewerscpearce
bugs1318965
milestone53.0a1
Bug 1318965 - Improves the logging functions provided in ClearKeyUtils r?cpearce MozReview-Commit-ID: LRimaj7hMh7
media/gmp-clearkey/0.1/ClearKeySessionManager.cpp
media/gmp-clearkey/0.1/ClearKeyUtils.cpp
media/gmp-clearkey/0.1/ClearKeyUtils.h
--- a/media/gmp-clearkey/0.1/ClearKeySessionManager.cpp
+++ b/media/gmp-clearkey/0.1/ClearKeySessionManager.cpp
@@ -88,16 +88,20 @@ ClearKeySessionManager::CreateSession(ui
 
   // If we haven't loaded, don't do this yet
   if (MaybeDeferTillInitialized(deferrer)) {
     return;
   }
 
 
   CK_LOGD("ClearKeySessionManager::CreateSession type:%s", aInitDataType);
+  CK_LOGARRAY("ClearKeySessionManager::CreateSession initdata:%s",
+              aInitData,
+              aInitDataSize);
+
   // initDataType must be "cenc", "keyids", or "webm".
   if (aInitDataType != InitDataType::kCenc &&
       aInitDataType != InitDataType::kKeyIds &&
       aInitDataType != InitDataType::kWebM) {
 
     string message = "initDataType is not supported by ClearKey";
     mHost->OnRejectPromise(aPromiseId,
                            Error::kNotSupportedError,
--- a/media/gmp-clearkey/0.1/ClearKeyUtils.cpp
+++ b/media/gmp-clearkey/0.1/ClearKeyUtils.cpp
@@ -16,16 +16,17 @@
 
 #include "ClearKeyUtils.h"
 
 #include <algorithm>
 #include <assert.h>
 #include <stdlib.h>
 #include <ctype.h>
 #include <memory.h>
+#include <sstream>
 #include <stdarg.h>
 #include <stdint.h>
 #include <vector>
 
 #include "ArrayUtils.h"
 #include "BigEndian.h"
 #include "ClearKeyBase64.h"
 // This include is required in order for content_decryption_module to work
@@ -65,28 +66,22 @@ CK_Log(const char* aFmt, ...)
   }
 
   out = nullptr;
 }
 
 void CK_LogArray(const char* prepend,
                  const uint8_t* aData,
                  const uint32_t aDataSize) {
-  const size_t bufferSize = 1024;
-  size_t remain = bufferSize;
-  char buffer[bufferSize] = { 0 };
-  char* start = &buffer[0];
+  // If the data is valid ascii, use that. Otherwise print the hex
+  string data = ClearKeyUtils::PrintableAsString(aData, aDataSize) ?
+                string(aData, aData + aDataSize) :
+                ClearKeyUtils::ToHexString(aData, aDataSize);
 
-  for (uint32_t i = 0; i < aDataSize; ++i) {
-    uint32_t wrote = snprintf(start, remain, "%hhX", aData[i]);
-    remain -= wrote;
-    start += wrote;
-  }
-
-  CK_Log("%s%s", prepend, buffer);
+  CK_LOGD("%s%s", prepend, data.c_str());
 }
 
 static void
 IncrementIV(vector<uint8_t>& aIV) {
   using mozilla::BigEndian;
 
   assert(aIV.size() == 16);
   BigEndian::writeUint64(&aIV[8], BigEndian::readUint64(&aIV[8]) + 1);
@@ -570,8 +565,30 @@ ClearKeyUtils::IsValidSessionId(const ch
   }
   for (uint32_t i = 0; i < aLength; i++) {
     if (!isdigit(aBuff[i])) {
       return false;
     }
   }
   return true;
 }
+
+string ClearKeyUtils::ToHexString(const uint8_t * aBytes, uint32_t aLength)
+{
+  stringstream ss;
+  ss << std::showbase << std::uppercase << std::hex;
+  for (uint32_t i = 0; i < aLength; ++i) {
+    ss << std::hex << static_cast<uint32_t>(aBytes[i]);
+    ss << " ";
+  }
+
+  return ss.str();
+}
+
+bool ClearKeyUtils::PrintableAsString(const uint8_t* aBytes, uint32_t aLength)
+{
+  for (uint32_t i = 0; i < aLength; ++i) {
+    if (!isprint(aBytes[i])) {
+      return false;
+    }
+  }
+  return true;
+}
--- a/media/gmp-clearkey/0.1/ClearKeyUtils.h
+++ b/media/gmp-clearkey/0.1/ClearKeyUtils.h
@@ -28,18 +28,18 @@
 #include "content_decryption_module.h"
 #include "gmp-api/gmp-decryption.h"
 
 #if 0
 void CK_Log(const char* aFmt, ...);
 #define CK_LOGE(...) CK_Log(__VA_ARGS__)
 #define CK_LOGD(...) CK_Log(__VA_ARGS__)
 #define CK_LOGW(...) CK_Log(__VA_ARGS__)
-#define CK_LOGARRAY(APREPEND, ADATA, ADATA_SIZE) CK_LogArray(APREPEND,
-  ADATA,
+#define CK_LOGARRAY(APREPEND, ADATA, ADATA_SIZE) CK_LogArray(APREPEND, \
+  ADATA, \
   ADATA_SIZE)
 #else
 // Note: Enabling logging slows things down a LOT
 #define CK_LOGE(...)
 #define CK_LOGD(...)
 #define CK_LOGW(...)
 #define CK_LOGARRAY(APREPEND, ADATA, ADATA_SIZE)
 #endif
@@ -83,16 +83,21 @@ public:
                              cdm::SessionType aSessionType);
 
   static bool ParseJWK(const uint8_t* aKeyData, uint32_t aKeyDataSize,
                        std::vector<KeyIdPair>& aOutKeys,
                        cdm::SessionType aSessionType);
   static const char* SessionTypeToString(cdm::SessionType aSessionType);
 
   static bool IsValidSessionId(const char* aBuff, uint32_t aLength);
+
+  static std::string ToHexString(const uint8_t * aBytes, uint32_t aLength);
+
+  // Determines whether an array of bytes can be printed as a string
+  static bool PrintableAsString(const uint8_t* aBytes, uint32_t aLength);
 };
 
 template<class Container, class Element>
 inline bool
 Contains(const Container& aContainer, const Element& aElement)
 {
   return aContainer.find(aElement) != aContainer.end();
 }