1030cabe0Smrg/*
2030cabe0Smrg * Copyright (c) 2014 Google Inc.
3030cabe0Smrg *
4030cabe0Smrg * Permission to use, copy, modify, and distribute this software for any
5030cabe0Smrg * purpose with or without fee is hereby granted, provided that the above
6030cabe0Smrg * copyright notice and this permission notice appear in all copies.
7030cabe0Smrg *
8030cabe0Smrg * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9030cabe0Smrg * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10030cabe0Smrg * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11030cabe0Smrg * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12030cabe0Smrg * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13030cabe0Smrg * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14030cabe0Smrg * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15030cabe0Smrg */
16030cabe0Smrg
17030cabe0Smrg#include <limits.h>
18030cabe0Smrg#include <string.h>
19030cabe0Smrg#include <X11/Xfuncproto.h>
20030cabe0Smrg#include <dix-config.h>
21030cabe0Smrg#include "os.h"
22030cabe0Smrg
23030cabe0Smrgint
24030cabe0Smrgtimingsafe_memcmp(const void *b1, const void *b2, size_t len)
25030cabe0Smrg{
26030cabe0Smrg        const unsigned char *p1 = b1, *p2 = b2;
27030cabe0Smrg        size_t i;
28030cabe0Smrg        int res = 0, done = 0;
29030cabe0Smrg
30030cabe0Smrg        for (i = 0; i < len; i++) {
31030cabe0Smrg                /* lt is -1 if p1[i] < p2[i]; else 0. */
32030cabe0Smrg                int lt = (p1[i] - p2[i]) >> CHAR_BIT;
33030cabe0Smrg
34030cabe0Smrg                /* gt is -1 if p1[i] > p2[i]; else 0. */
35030cabe0Smrg                int gt = (p2[i] - p1[i]) >> CHAR_BIT;
36030cabe0Smrg
37030cabe0Smrg                /* cmp is 1 if p1[i] > p2[i]; -1 if p1[i] < p2[i]; else 0. */
38030cabe0Smrg                int cmp = lt - gt;
39030cabe0Smrg
40030cabe0Smrg                /* set res = cmp if !done. */
41030cabe0Smrg                res |= cmp & ~done;
42030cabe0Smrg
43030cabe0Smrg                /* set done if p1[i] != p2[i]. */
44030cabe0Smrg                done |= lt | gt;
45030cabe0Smrg        }
46030cabe0Smrg
47030cabe0Smrg        return (res);
48030cabe0Smrg}
49