blob: 2f8c90dc1ad94ec2514595ad9f3d992d5118180f [file] [log] [blame]
Randall Spangler7993f252013-01-29 15:01:12 -08001/* Copyright (c) 2013 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"
Bill Richardson0c3ba242013-03-29 11:09:30 -07009
Gaurav Shah37dff842010-08-20 14:30:03 -070010#include "utility.h"
11
Randall Spangler7993f252013-01-29 15:01:12 -080012int SafeMemcmp(const void *s1, const void *s2, size_t n) {
13 const unsigned char *us1 = s1;
14 const unsigned char *us2 = s2;
15 int result = 0;
Randall Spangler41656c02010-08-23 13:20:07 -070016
Randall Spangler7993f252013-01-29 15:01:12 -080017 if (0 == n)
18 return 0;
Gaurav Shah37dff842010-08-20 14:30:03 -070019
Randall Spangler7993f252013-01-29 15:01:12 -080020 /*
21 * Code snippet without data-dependent branch due to Nate Lawson
22 * (nate@root.org) of Root Labs.
23 */
24 while (n--)
25 result |= *us1++ ^ *us2++;
Gaurav Shah37dff842010-08-20 14:30:03 -070026
Randall Spangler7993f252013-01-29 15:01:12 -080027 return result != 0;
Gaurav Shah37dff842010-08-20 14:30:03 -070028}