blob: 7a2370b0bbb446a947034452e509c60a06c88179 [file] [log] [blame]
Randall Spanglera3454fc2011-08-23 14:41:18 -07001/* Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
Gaurav Shah37dff842010-08-20 14:30:03 -07002 * 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 Spangler41656c02010-08-23 13:20:07 -07008#include "sysincludes.h"
Gaurav Shah37dff842010-08-20 14:30:03 -07009#include "utility.h"
10
11void* 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 }
Randall Spanglera3454fc2011-08-23 14:41:18 -070016 return d;
Gaurav Shah37dff842010-08-20 14:30:03 -070017}
18
19int SafeMemcmp(const void* s1, const void* s2, size_t n) {
Randall Spangler41656c02010-08-23 13:20:07 -070020 const unsigned char* us1 = s1;
21 const unsigned char* us2 = s2;
Gaurav Shah37dff842010-08-20 14:30:03 -070022 int result = 0;
Randall Spangler41656c02010-08-23 13:20:07 -070023
Gaurav Shah37dff842010-08-20 14:30:03 -070024 if (0 == n)
Randall Spanglera3454fc2011-08-23 14:41:18 -070025 return 0;
Gaurav Shah37dff842010-08-20 14:30:03 -070026
Gaurav Shah37dff842010-08-20 14:30:03 -070027 /* 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}