1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim:set ts=2 sw=2 sts=2 et cindent: */
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
5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 
7 #include "common.h"
8 
9 #include <windows.h>
10 
GetCurrentTimestamp()11 ULONGLONG GetCurrentTimestamp() {
12   FILETIME filetime;
13   GetSystemTimeAsFileTime(&filetime);
14   ULARGE_INTEGER integerTime;
15   integerTime.u.LowPart = filetime.dwLowDateTime;
16   integerTime.u.HighPart = filetime.dwHighDateTime;
17   return integerTime.QuadPart;
18 }
19 
20 // Passing a zero as the second argument (or omitting it) causes the function
21 // to get the current time rather than using a passed value.
SecondsPassedSince(ULONGLONG initialTime,ULONGLONG currentTime)22 ULONGLONG SecondsPassedSince(ULONGLONG initialTime,
23                              ULONGLONG currentTime /* = 0 */) {
24   if (currentTime == 0) {
25     currentTime = GetCurrentTimestamp();
26   }
27   // Since this is returning an unsigned value, let's make sure we don't try to
28   // return anything negative
29   if (initialTime >= currentTime) {
30     return 0;
31   }
32 
33   // These timestamps are expressed in 100-nanosecond intervals
34   return (currentTime - initialTime) / 10  // To microseconds
35          / 1000                            // To milliseconds
36          / 1000;                           // To seconds
37 }
38 
GenerateUUIDStr()39 FilePathResult GenerateUUIDStr() {
40   UUID uuid;
41   RPC_STATUS status = UuidCreate(&uuid);
42   if (status != RPC_S_OK) {
43     HRESULT hr = MAKE_HRESULT(1, FACILITY_RPC, status);
44     LOG_ERROR(hr);
45     return FilePathResult(mozilla::WindowsError::FromHResult(hr));
46   }
47 
48   // 39 == length of a UUID string including braces and NUL.
49   wchar_t guidBuf[39] = {};
50   if (StringFromGUID2(uuid, guidBuf, 39) != 39) {
51     LOG_ERROR(HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER));
52     return FilePathResult(
53         mozilla::WindowsError::FromWin32Error(ERROR_INSUFFICIENT_BUFFER));
54   }
55 
56   // Remove the curly braces.
57   return std::wstring(guidBuf + 1, guidBuf + 37);
58 }
59