- Notifications
You must be signed in to change notification settings - Fork 7.8k
/
Copy pathsafe_bcmp.c
44 lines (38 loc) · 1.57 KB
/
safe_bcmp.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/*
+----------------------------------------------------------------------+
| Copyright (c) The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| https://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: David Carlier <devnexen@gmail.com> |
+----------------------------------------------------------------------+
*/
#include"php.h"
#include<string.h>
/*
* Returns 0 if both inputs match, non-zero if they don't.
* Returns -1 early if inputs do not have the same lengths.
*
*/
PHPAPIintphp_safe_bcmp(constzend_string*a, constzend_string*b)
{
constvolatileunsigned char*ua= (constvolatileunsigned char*)ZSTR_VAL(a);
constvolatileunsigned char*ub= (constvolatileunsigned char*)ZSTR_VAL(b);
size_ti=0;
intr=0;
if (ZSTR_LEN(a) !=ZSTR_LEN(b)) {
return-1;
}
/* This is security sensitive code. Do not optimize this for speed. */
while (i<ZSTR_LEN(a)) {
r |= ua[i] ^ ub[i];
++i;
}
returnr;
}