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