Gaurav Shah | 37dff84 | 2010-08-20 14:30:03 -0700 | [diff] [blame] | 1 | /* Copyright (c) 2010 The Chromium OS Authors. All rights reserved. |
| 2 | * Use of this source code is governed by a BSD-style license that can be |
| 3 | * found in the LICENSE file. |
| 4 | * |
| 5 | * Utility functions that need to be built as part of the firmware. |
| 6 | */ |
| 7 | |
Randall Spangler | 41656c0 | 2010-08-23 13:20:07 -0700 | [diff] [blame^] | 8 | #include "sysincludes.h" |
Gaurav Shah | 37dff84 | 2010-08-20 14:30:03 -0700 | [diff] [blame] | 9 | #include "utility.h" |
| 10 | |
| 11 | void* Memset(void* d, const uint8_t c, uint64_t n) { |
| 12 | uint8_t* dest = d; /* the only way to keep both cl and gcc happy */ |
| 13 | while (n--) { |
| 14 | *dest++ = c; |
| 15 | } |
| 16 | return dest; |
| 17 | } |
| 18 | |
| 19 | int SafeMemcmp(const void* s1, const void* s2, size_t n) { |
Randall Spangler | 41656c0 | 2010-08-23 13:20:07 -0700 | [diff] [blame^] | 20 | const unsigned char* us1 = s1; |
| 21 | const unsigned char* us2 = s2; |
Gaurav Shah | 37dff84 | 2010-08-20 14:30:03 -0700 | [diff] [blame] | 22 | int result = 0; |
Randall Spangler | 41656c0 | 2010-08-23 13:20:07 -0700 | [diff] [blame^] | 23 | |
Gaurav Shah | 37dff84 | 2010-08-20 14:30:03 -0700 | [diff] [blame] | 24 | if (0 == n) |
| 25 | return 1; |
| 26 | |
Gaurav Shah | 37dff84 | 2010-08-20 14:30:03 -0700 | [diff] [blame] | 27 | /* Code snippet without data-dependent branch due to |
| 28 | * Nate Lawson (nate@root.org) of Root Labs. */ |
| 29 | while (n--) |
| 30 | result |= *us1++ ^ *us2++; |
| 31 | |
| 32 | return result != 0; |
| 33 | } |