Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(20)

Side by Side Diff: webrtc/base/win32.cc

Issue 2718663005: Replace NULL with nullptr or null in webrtc/base/. (Closed)
Patch Set: Fixing Windows and formatting issues. Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « webrtc/base/win32.h ('k') | webrtc/base/win32filesystem.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. 2 * Copyright 2004 The WebRTC Project Authors. All rights reserved.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license 4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source 5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found 6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may 7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree. 8 * be found in the AUTHORS file in the root of the source tree.
9 */ 9 */
10 10
(...skipping 17 matching lines...) Expand all
28 static int inet_pton_v4(const char* src, void* dst); 28 static int inet_pton_v4(const char* src, void* dst);
29 static int inet_pton_v6(const char* src, void* dst); 29 static int inet_pton_v6(const char* src, void* dst);
30 30
31 // Implementation of inet_ntop (create a printable representation of an 31 // Implementation of inet_ntop (create a printable representation of an
32 // ip address). XP doesn't have its own inet_ntop, and 32 // ip address). XP doesn't have its own inet_ntop, and
33 // WSAAddressToString requires both IPv6 to be installed and for Winsock 33 // WSAAddressToString requires both IPv6 to be installed and for Winsock
34 // to be initialized. 34 // to be initialized.
35 const char* win32_inet_ntop(int af, const void *src, 35 const char* win32_inet_ntop(int af, const void *src,
36 char* dst, socklen_t size) { 36 char* dst, socklen_t size) {
37 if (!src || !dst) { 37 if (!src || !dst) {
38 return NULL; 38 return nullptr;
39 } 39 }
40 switch (af) { 40 switch (af) {
41 case AF_INET: { 41 case AF_INET: {
42 return inet_ntop_v4(src, dst, size); 42 return inet_ntop_v4(src, dst, size);
43 } 43 }
44 case AF_INET6: { 44 case AF_INET6: {
45 return inet_ntop_v6(src, dst, size); 45 return inet_ntop_v6(src, dst, size);
46 } 46 }
47 } 47 }
48 return NULL; 48 return nullptr;
49 } 49 }
50 50
51 // As above, but for inet_pton. Implements inet_pton for v4 and v6. 51 // As above, but for inet_pton. Implements inet_pton for v4 and v6.
52 // Note that our inet_ntop will output normal 'dotted' v4 addresses only. 52 // Note that our inet_ntop will output normal 'dotted' v4 addresses only.
53 int win32_inet_pton(int af, const char* src, void* dst) { 53 int win32_inet_pton(int af, const char* src, void* dst) {
54 if (!src || !dst) { 54 if (!src || !dst) {
55 return 0; 55 return 0;
56 } 56 }
57 if (af == AF_INET) { 57 if (af == AF_INET) {
58 return inet_pton_v4(src, dst); 58 return inet_pton_v4(src, dst);
59 } else if (af == AF_INET6) { 59 } else if (af == AF_INET6) {
60 return inet_pton_v6(src, dst); 60 return inet_pton_v6(src, dst);
61 } 61 }
62 return -1; 62 return -1;
63 } 63 }
64 64
65 // Helper function for inet_ntop for IPv4 addresses. 65 // Helper function for inet_ntop for IPv4 addresses.
66 // Outputs "dotted-quad" decimal notation. 66 // Outputs "dotted-quad" decimal notation.
67 const char* inet_ntop_v4(const void* src, char* dst, socklen_t size) { 67 const char* inet_ntop_v4(const void* src, char* dst, socklen_t size) {
68 if (size < INET_ADDRSTRLEN) { 68 if (size < INET_ADDRSTRLEN) {
69 return NULL; 69 return nullptr;
70 } 70 }
71 const struct in_addr* as_in_addr = 71 const struct in_addr* as_in_addr =
72 reinterpret_cast<const struct in_addr*>(src); 72 reinterpret_cast<const struct in_addr*>(src);
73 rtc::sprintfn(dst, size, "%d.%d.%d.%d", 73 rtc::sprintfn(dst, size, "%d.%d.%d.%d",
74 as_in_addr->S_un.S_un_b.s_b1, 74 as_in_addr->S_un.S_un_b.s_b1,
75 as_in_addr->S_un.S_un_b.s_b2, 75 as_in_addr->S_un.S_un_b.s_b2,
76 as_in_addr->S_un.S_un_b.s_b3, 76 as_in_addr->S_un.S_un_b.s_b3,
77 as_in_addr->S_un.S_un_b.s_b4); 77 as_in_addr->S_un.S_un_b.s_b4);
78 return dst; 78 return dst;
79 } 79 }
80 80
81 // Helper function for inet_ntop for IPv6 addresses. 81 // Helper function for inet_ntop for IPv6 addresses.
82 const char* inet_ntop_v6(const void* src, char* dst, socklen_t size) { 82 const char* inet_ntop_v6(const void* src, char* dst, socklen_t size) {
83 if (size < INET6_ADDRSTRLEN) { 83 if (size < INET6_ADDRSTRLEN) {
84 return NULL; 84 return nullptr;
85 } 85 }
86 const uint16_t* as_shorts = reinterpret_cast<const uint16_t*>(src); 86 const uint16_t* as_shorts = reinterpret_cast<const uint16_t*>(src);
87 int runpos[8]; 87 int runpos[8];
88 int current = 1; 88 int current = 1;
89 int max = 0; 89 int max = 0;
90 int maxpos = -1; 90 int maxpos = -1;
91 int run_array_size = arraysize(runpos); 91 int run_array_size = arraysize(runpos);
92 // Run over the address marking runs of 0s. 92 // Run over the address marking runs of 0s.
93 for (int i = 0; i < run_array_size; ++i) { 93 for (int i = 0; i < run_array_size; ++i) {
94 if (as_shorts[i] == 0) { 94 if (as_shorts[i] == 0) {
(...skipping 216 matching lines...) Expand 10 before | Expand all | Expand 10 after
311 // FILETIME of that time/date, then we add/subtract in appropriate units to 311 // FILETIME of that time/date, then we add/subtract in appropriate units to
312 // convert to/from unix time. 312 // convert to/from unix time.
313 // The units of FILETIME are 100ns intervals, so by multiplying by or dividing 313 // The units of FILETIME are 100ns intervals, so by multiplying by or dividing
314 // by 10000000, we can convert to/from seconds. 314 // by 10000000, we can convert to/from seconds.
315 // 315 //
316 // FileTime = UnixTime*10000000 + FileTime(1970) 316 // FileTime = UnixTime*10000000 + FileTime(1970)
317 // UnixTime = (FileTime-FileTime(1970))/10000000 317 // UnixTime = (FileTime-FileTime(1970))/10000000
318 // 318 //
319 319
320 void FileTimeToUnixTime(const FILETIME& ft, time_t* ut) { 320 void FileTimeToUnixTime(const FILETIME& ft, time_t* ut) {
321 RTC_DCHECK(NULL != ut); 321 RTC_DCHECK(nullptr != ut);
322 322
323 // FILETIME has an earlier date base than time_t (1/1/1970), so subtract off 323 // FILETIME has an earlier date base than time_t (1/1/1970), so subtract off
324 // the difference. 324 // the difference.
325 SYSTEMTIME base_st; 325 SYSTEMTIME base_st;
326 memset(&base_st, 0, sizeof(base_st)); 326 memset(&base_st, 0, sizeof(base_st));
327 base_st.wDay = 1; 327 base_st.wDay = 1;
328 base_st.wMonth = 1; 328 base_st.wMonth = 1;
329 base_st.wYear = 1970; 329 base_st.wYear = 1970;
330 330
331 FILETIME base_ft; 331 FILETIME base_ft;
332 SystemTimeToFileTime(&base_st, &base_ft); 332 SystemTimeToFileTime(&base_st, &base_ft);
333 333
334 ULARGE_INTEGER base_ul, current_ul; 334 ULARGE_INTEGER base_ul, current_ul;
335 memcpy(&base_ul, &base_ft, sizeof(FILETIME)); 335 memcpy(&base_ul, &base_ft, sizeof(FILETIME));
336 memcpy(&current_ul, &ft, sizeof(FILETIME)); 336 memcpy(&current_ul, &ft, sizeof(FILETIME));
337 337
338 // Divide by big number to convert to seconds, then subtract out the 1970 338 // Divide by big number to convert to seconds, then subtract out the 1970
339 // base date value. 339 // base date value.
340 const ULONGLONG RATIO = 10000000; 340 const ULONGLONG RATIO = 10000000;
341 *ut = static_cast<time_t>((current_ul.QuadPart - base_ul.QuadPart) / RATIO); 341 *ut = static_cast<time_t>((current_ul.QuadPart - base_ul.QuadPart) / RATIO);
342 } 342 }
343 343
344 void UnixTimeToFileTime(const time_t& ut, FILETIME* ft) { 344 void UnixTimeToFileTime(const time_t& ut, FILETIME* ft) {
345 RTC_DCHECK(NULL != ft); 345 RTC_DCHECK(nullptr != ft);
346 346
347 // FILETIME has an earlier date base than time_t (1/1/1970), so add in 347 // FILETIME has an earlier date base than time_t (1/1/1970), so add in
348 // the difference. 348 // the difference.
349 SYSTEMTIME base_st; 349 SYSTEMTIME base_st;
350 memset(&base_st, 0, sizeof(base_st)); 350 memset(&base_st, 0, sizeof(base_st));
351 base_st.wDay = 1; 351 base_st.wDay = 1;
352 base_st.wMonth = 1; 352 base_st.wMonth = 1;
353 base_st.wYear = 1970; 353 base_st.wYear = 1970;
354 354
355 FILETIME base_ft; 355 FILETIME base_ft;
(...skipping 10 matching lines...) Expand all
366 memcpy(ft, &current_ul, sizeof(FILETIME)); 366 memcpy(ft, &current_ul, sizeof(FILETIME));
367 } 367 }
368 368
369 bool Utf8ToWindowsFilename(const std::string& utf8, std::wstring* filename) { 369 bool Utf8ToWindowsFilename(const std::string& utf8, std::wstring* filename) {
370 // TODO: Integrate into fileutils.h 370 // TODO: Integrate into fileutils.h
371 // TODO: Handle wide and non-wide cases via TCHAR? 371 // TODO: Handle wide and non-wide cases via TCHAR?
372 // TODO: Skip \\?\ processing if the length is not > MAX_PATH? 372 // TODO: Skip \\?\ processing if the length is not > MAX_PATH?
373 // TODO: Write unittests 373 // TODO: Write unittests
374 374
375 // Convert to Utf16 375 // Convert to Utf16
376 int wlen = ::MultiByteToWideChar(CP_UTF8, 0, utf8.c_str(), 376 int wlen =
377 static_cast<int>(utf8.length() + 1), NULL, 377 ::MultiByteToWideChar(CP_UTF8, 0, utf8.c_str(),
378 0); 378 static_cast<int>(utf8.length() + 1), nullptr, 0);
379 if (0 == wlen) { 379 if (0 == wlen) {
380 return false; 380 return false;
381 } 381 }
382 wchar_t* wfilename = STACK_ARRAY(wchar_t, wlen); 382 wchar_t* wfilename = STACK_ARRAY(wchar_t, wlen);
383 if (0 == ::MultiByteToWideChar(CP_UTF8, 0, utf8.c_str(), 383 if (0 == ::MultiByteToWideChar(CP_UTF8, 0, utf8.c_str(),
384 static_cast<int>(utf8.length() + 1), 384 static_cast<int>(utf8.length() + 1),
385 wfilename, wlen)) { 385 wfilename, wlen)) {
386 return false; 386 return false;
387 } 387 }
388 // Replace forward slashes with backslashes 388 // Replace forward slashes with backslashes
389 std::replace(wfilename, wfilename + wlen, L'/', L'\\'); 389 std::replace(wfilename, wfilename + wlen, L'/', L'\\');
390 // Convert to complete filename 390 // Convert to complete filename
391 DWORD full_len = ::GetFullPathName(wfilename, 0, NULL, NULL); 391 DWORD full_len = ::GetFullPathName(wfilename, 0, nullptr, nullptr);
392 if (0 == full_len) { 392 if (0 == full_len) {
393 return false; 393 return false;
394 } 394 }
395 wchar_t* filepart = NULL; 395 wchar_t* filepart = nullptr;
396 wchar_t* full_filename = STACK_ARRAY(wchar_t, full_len + 6); 396 wchar_t* full_filename = STACK_ARRAY(wchar_t, full_len + 6);
397 wchar_t* start = full_filename + 6; 397 wchar_t* start = full_filename + 6;
398 if (0 == ::GetFullPathName(wfilename, full_len, start, &filepart)) { 398 if (0 == ::GetFullPathName(wfilename, full_len, start, &filepart)) {
399 return false; 399 return false;
400 } 400 }
401 // Add long-path prefix 401 // Add long-path prefix
402 const wchar_t kLongPathPrefix[] = L"\\\\?\\UNC"; 402 const wchar_t kLongPathPrefix[] = L"\\\\?\\UNC";
403 if ((start[0] != L'\\') || (start[1] != L'\\')) { 403 if ((start[0] != L'\\') || (start[1] != L'\\')) {
404 // Non-unc path: <pathname> 404 // Non-unc path: <pathname>
405 // Becomes: \\?\<pathname> 405 // Becomes: \\?\<pathname>
(...skipping 23 matching lines...) Expand all
429 return true; 429 return true;
430 } 430 }
431 return false; 431 return false;
432 } 432 }
433 433
434 bool GetCurrentProcessIntegrityLevel(int* level) { 434 bool GetCurrentProcessIntegrityLevel(int* level) {
435 bool ret = false; 435 bool ret = false;
436 HANDLE process = ::GetCurrentProcess(), token; 436 HANDLE process = ::GetCurrentProcess(), token;
437 if (OpenProcessToken(process, TOKEN_QUERY | TOKEN_QUERY_SOURCE, &token)) { 437 if (OpenProcessToken(process, TOKEN_QUERY | TOKEN_QUERY_SOURCE, &token)) {
438 DWORD size; 438 DWORD size;
439 if (!GetTokenInformation(token, TokenIntegrityLevel, NULL, 0, &size) && 439 if (!GetTokenInformation(token, TokenIntegrityLevel, nullptr, 0, &size) &&
440 GetLastError() == ERROR_INSUFFICIENT_BUFFER) { 440 GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
441
442 char* buf = STACK_ARRAY(char, size); 441 char* buf = STACK_ARRAY(char, size);
443 TOKEN_MANDATORY_LABEL* til = 442 TOKEN_MANDATORY_LABEL* til =
444 reinterpret_cast<TOKEN_MANDATORY_LABEL*>(buf); 443 reinterpret_cast<TOKEN_MANDATORY_LABEL*>(buf);
445 if (GetTokenInformation(token, TokenIntegrityLevel, til, size, &size)) { 444 if (GetTokenInformation(token, TokenIntegrityLevel, til, size, &size)) {
446 445
447 DWORD count = *GetSidSubAuthorityCount(til->Label.Sid); 446 DWORD count = *GetSidSubAuthorityCount(til->Label.Sid);
448 *level = *GetSidSubAuthority(til->Label.Sid, count - 1); 447 *level = *GetSidSubAuthority(til->Label.Sid, count - 1);
449 ret = true; 448 ret = true;
450 } 449 }
451 } 450 }
452 CloseHandle(token); 451 CloseHandle(token);
453 } 452 }
454 return ret; 453 return ret;
455 } 454 }
456 455
457 } // namespace rtc 456 } // namespace rtc
OLDNEW
« no previous file with comments | « webrtc/base/win32.h ('k') | webrtc/base/win32filesystem.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698