1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this file,
5  * You can obtain one at http://mozilla.org/MPL/2.0/. */
6 
7 #include "GMPUtils.h"
8 
9 #include "GMPService.h"
10 #include "VideoLimits.h"
11 #include "mozIGeckoMediaPluginService.h"
12 #include "mozilla/Base64.h"
13 #include "nsCOMPtr.h"
14 #include "nsCRTGlue.h"
15 #include "nsDirectoryServiceDefs.h"
16 #include "nsIConsoleService.h"
17 #include "nsIFile.h"
18 #include "nsLiteralString.h"
19 #include "prio.h"
20 
21 namespace mozilla {
22 
SplitAt(const char * aDelims,const nsACString & aInput,nsTArray<nsCString> & aOutTokens)23 void SplitAt(const char* aDelims, const nsACString& aInput,
24              nsTArray<nsCString>& aOutTokens) {
25   nsAutoCString str(aInput);
26   char* end = str.BeginWriting();
27   const char* start = nullptr;
28   while (!!(start = NS_strtok(aDelims, &end))) {
29     aOutTokens.AppendElement(nsCString(start));
30   }
31 }
32 
ToHexString(const uint8_t * aBytes,uint32_t aLength)33 nsCString ToHexString(const uint8_t* aBytes, uint32_t aLength) {
34   static const char hex[] = {'0', '1', '2', '3', '4', '5', '6', '7',
35                              '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
36   nsCString str;
37   for (uint32_t i = 0; i < aLength; i++) {
38     char buf[3];
39     buf[0] = hex[(aBytes[i] & 0xf0) >> 4];
40     buf[1] = hex[aBytes[i] & 0x0f];
41     buf[2] = 0;
42     str.AppendASCII(buf);
43   }
44   return str;
45 }
46 
ToHexString(const nsTArray<uint8_t> & aBytes)47 nsCString ToHexString(const nsTArray<uint8_t>& aBytes) {
48   return ToHexString(aBytes.Elements(), aBytes.Length());
49 }
50 
FileExists(nsIFile * aFile)51 bool FileExists(nsIFile* aFile) {
52   bool exists = false;
53   return aFile && NS_SUCCEEDED(aFile->Exists(&exists)) && exists;
54 }
55 
DirectoryEnumerator(nsIFile * aPath,Mode aMode)56 DirectoryEnumerator::DirectoryEnumerator(nsIFile* aPath, Mode aMode)
57     : mMode(aMode) {
58   aPath->GetDirectoryEntries(getter_AddRefs(mIter));
59 }
60 
Next()61 already_AddRefed<nsIFile> DirectoryEnumerator::Next() {
62   if (!mIter) {
63     return nullptr;
64   }
65   bool hasMore = false;
66   while (NS_SUCCEEDED(mIter->HasMoreElements(&hasMore)) && hasMore) {
67     nsCOMPtr<nsISupports> supports;
68     nsresult rv = mIter->GetNext(getter_AddRefs(supports));
69     if (NS_FAILED(rv)) {
70       continue;
71     }
72 
73     nsCOMPtr<nsIFile> path(do_QueryInterface(supports, &rv));
74     if (NS_FAILED(rv)) {
75       continue;
76     }
77 
78     if (mMode == DirsOnly) {
79       bool isDirectory = false;
80       rv = path->IsDirectory(&isDirectory);
81       if (NS_FAILED(rv) || !isDirectory) {
82         continue;
83       }
84     }
85     return path.forget();
86   }
87   return nullptr;
88 }
89 
ReadIntoArray(nsIFile * aFile,nsTArray<uint8_t> & aOutDst,size_t aMaxLength)90 bool ReadIntoArray(nsIFile* aFile, nsTArray<uint8_t>& aOutDst,
91                    size_t aMaxLength) {
92   if (!FileExists(aFile)) {
93     return false;
94   }
95 
96   PRFileDesc* fd = nullptr;
97   nsresult rv = aFile->OpenNSPRFileDesc(PR_RDONLY, 0, &fd);
98   if (NS_WARN_IF(NS_FAILED(rv))) {
99     return false;
100   }
101 
102   int32_t length = PR_Seek(fd, 0, PR_SEEK_END);
103   PR_Seek(fd, 0, PR_SEEK_SET);
104 
105   if (length < 0 || (size_t)length > aMaxLength) {
106     NS_WARNING("EME file is longer than maximum allowed length");
107     PR_Close(fd);
108     return false;
109   }
110   aOutDst.SetLength(length);
111   int32_t bytesRead = PR_Read(fd, aOutDst.Elements(), length);
112   PR_Close(fd);
113   return (bytesRead == length);
114 }
115 
ReadIntoString(nsIFile * aFile,nsCString & aOutDst,size_t aMaxLength)116 bool ReadIntoString(nsIFile* aFile, nsCString& aOutDst, size_t aMaxLength) {
117   nsTArray<uint8_t> buf;
118   bool rv = ReadIntoArray(aFile, buf, aMaxLength);
119   if (rv) {
120     buf.AppendElement(0);  // Append null terminator, required by nsC*String.
121     aOutDst = nsDependentCString((const char*)buf.Elements(), buf.Length() - 1);
122   }
123   return rv;
124 }
125 
Init(nsIFile * aInfoFile)126 bool GMPInfoFileParser::Init(nsIFile* aInfoFile) {
127   nsTArray<nsCString> lines;
128   static const size_t MAX_GMP_INFO_FILE_LENGTH = 5 * 1024;
129 
130   nsAutoCString info;
131   if (!ReadIntoString(aInfoFile, info, MAX_GMP_INFO_FILE_LENGTH)) {
132     NS_WARNING("Failed to read info file in GMP process.");
133     return false;
134   }
135 
136   // Note: we pass "\r\n" to SplitAt so that we'll split lines delimited
137   // by \n (Unix), \r\n (Windows) and \r (old MacOSX).
138   SplitAt("\r\n", info, lines);
139 
140   for (nsCString line : lines) {
141     // Field name is the string up to but not including the first ':'
142     // character on the line.
143     int32_t colon = line.FindChar(':');
144     if (colon <= 0) {
145       // Not allowed to be the first character.
146       // Info field name must be at least one character.
147       continue;
148     }
149     nsAutoCString key(Substring(line, 0, colon));
150     ToLowerCase(key);
151     key.Trim(" ");
152 
153     auto value = MakeUnique<nsCString>(Substring(line, colon + 1));
154     value->Trim(" ");
155     mValues.InsertOrUpdate(
156         key,
157         std::move(value));  // Hashtable assumes ownership of value.
158   }
159 
160   return true;
161 }
162 
Contains(const nsCString & aKey) const163 bool GMPInfoFileParser::Contains(const nsCString& aKey) const {
164   nsCString key(aKey);
165   ToLowerCase(key);
166   return mValues.Contains(key);
167 }
168 
Get(const nsCString & aKey) const169 nsCString GMPInfoFileParser::Get(const nsCString& aKey) const {
170   MOZ_ASSERT(Contains(aKey));
171   nsCString key(aKey);
172   ToLowerCase(key);
173   nsCString* p = nullptr;
174   if (mValues.Get(key, &p)) {
175     return nsCString(*p);
176   }
177   return ""_ns;
178 }
179 
HaveGMPFor(const nsCString & aAPI,nsTArray<nsCString> && aTags)180 bool HaveGMPFor(const nsCString& aAPI, nsTArray<nsCString>&& aTags) {
181   nsCOMPtr<mozIGeckoMediaPluginService> mps =
182       do_GetService("@mozilla.org/gecko-media-plugin-service;1");
183   if (NS_WARN_IF(!mps)) {
184     return false;
185   }
186 
187   bool hasPlugin = false;
188   if (NS_FAILED(mps->HasPluginForAPI(aAPI, &aTags, &hasPlugin))) {
189     return false;
190   }
191   return hasPlugin;
192 }
193 
LogToConsole(const nsAString & aMsg)194 void LogToConsole(const nsAString& aMsg) {
195   nsCOMPtr<nsIConsoleService> console(
196       do_GetService("@mozilla.org/consoleservice;1"));
197   if (!console) {
198     NS_WARNING("Failed to log message to console.");
199     return;
200   }
201   nsAutoString msg(aMsg);
202   console->LogStringMessage(msg.get());
203 }
204 
GetGMPThread()205 already_AddRefed<nsISerialEventTarget> GetGMPThread() {
206   RefPtr<gmp::GeckoMediaPluginService> service =
207       gmp::GeckoMediaPluginService::GetGeckoMediaPluginService();
208   nsCOMPtr<nsISerialEventTarget> thread =
209       service ? service->GetGMPThread() : nullptr;
210   return thread.forget();
211 }
212 
Align16(size_t aNumber)213 static size_t Align16(size_t aNumber) {
214   const size_t mask = 15;  // Alignment - 1.
215   return (aNumber + mask) & ~mask;
216 }
217 
I420FrameBufferSizePadded(int32_t aWidth,int32_t aHeight)218 size_t I420FrameBufferSizePadded(int32_t aWidth, int32_t aHeight) {
219   if (aWidth <= 0 || aHeight <= 0 || aWidth > MAX_VIDEO_WIDTH ||
220       aHeight > MAX_VIDEO_HEIGHT) {
221     return 0;
222   }
223 
224   size_t ySize = Align16(aWidth) * Align16(aHeight);
225   return ySize + (ySize / 4) * 2;
226 }
227 
228 }  // namespace mozilla
229