Jump to content

Bitwise operator | not working correctly with hex


Nolongerused3921

Recommended Posts

Well its official: I'm stumped... I can't seem to get my bitwise function to work very well at all... I'm not quite sure whats wrong with it exactly, but its not returning the values you would think it would.

The function:
[code]<?php
function perm_check($permissions, $module_permission, $permission_prefix, $action = 1) {
$temp_breakdown = explode(";",$permissions);
$temp_count = count($temp_breakdown);
for ($i = 0; $i != $temp_count; $i++){
if (is_array($temp_breakdown))
$tmp_breakdown = $temp_breakdown[$i];
else
$tmp_breakdown = $temp_breakdown;
if(strstr($tmp_breakdown,$permission_prefix)){
$permission = explode(':',$tmp_breakdown);
$permission = bin2hex($permission[1]);
$module_permission = $module_permission;
break;
}
}

switch ($action) {
case 1:
if ($permission & $module_permission)
$val = 1;
else
$val = 0;
break;
case 2:
$permission = $permission | $module_permission;
$val = $permission;
break;
}
return $val;
}
[/code]
Should probably explain this... Basically the first 15 lines or so are just to split the string ("module_name:0x########;module_name:blah;etc.") up, then gather the correct permission... That part works fine - it even converts the string into real hex (Which stays as a string otherwise).

However, the problem is with the switch and the whole bitwise portion... Whats wrong with it?


Heres what I'm using to test it:
[code]<?php
$groups_permissions = "article:0x00000000;";
$view_thread = 16;
$view_post = 2;
$perms = perm_check($groups_permissions,$view_post,"article",2);
//print $perms;
if ($perms & $view_thread) {
print "Success!";
}
[/code]
Now... You'd think this wouldn't work, right? Nope.... For whatever reason, the if activates.

What am I doing wrong here?
Err.... "Success" is what I get from the source, and it shouldn't return success, seeing as $perms & $view_thread doesn't exist. :(
$perms = perm_check($groups_permissions,$view_post,"article",2)
Basically this does "0x00000000 | $view_post", and assigns the result to $perms.
Now as converted to hex (0x02) or left as decimal, this doesn't work how its supposed to.
Okay well I went through and made sure everything is in hex... I'm getting weird results though....
New function:
[code=php:0]
function perm_check($permissions, $module_permission, $permission_prefix, $action = 1) {
$temp_breakdown = explode(";",$permissions);
$temp_count = count($temp_breakdown);
for ($i = 0; $i != $temp_count; $i++){
if (is_array($temp_breakdown))
$tmp_breakdown = $temp_breakdown[$i];
else
$tmp_breakdown = $temp_breakdown;
if(strstr($tmp_breakdown,$permission_prefix)){
$permission = explode(':',$tmp_breakdown);
$permission = $permission[1];
$module_permission = dechex($module_permission);
break;
} else {
$permission = $permissions;
$module_permission = dechex($module_permission);
}
}

switch ($action) {
case 1:
print "<br>Permission: ".$permission."<br>";
print "Module_permission: ".$module_permission;
if ($permission & $module_permission)
$val = 1;
else
$val = 0;
break;
case 2:
$val = $permission | $module_permission;
$val = dechex($val);
break;
}
return $val;
}
[/code]


New test:
[code=php:0]
$groups_permissions = "article:0x00000000;";
$view_thread = 16;
$view_post = 2;
$perms = perm_check($groups_permissions,$view_post,"article",2);
$perms = perm_check($perms,$view_thread,"article",2);
print $perms;
if (perm_check($perms,$view_thread,"article",1)) {
print "<br>Success!";
}
[/code]

For some reason my result is:
[quote]
1e
Permission: 1e
Module_permission: 10
Success!
[/quote]


...What exactly is 1e doing there? Shouldn't it be 12 (16 (10 in hex) + 2 (02 in hex))? Am I doing something wrong, and if so... What?


Edit: What the ...
I took out $val = dechex($val) in the function, and now I get:
[quote]
3x00000000
Permission: 3x00000000
Module_permission: 10
Success!
[/quote]

I'm 90% sure this is actually working how its supposed to (At least the check is)... But why is it returning 3x00000000?
It's always helpful to put echo statements in your code when trying to debug problems like this.

The bin2hex() function wasn't doing what you thought it was doing. I replaced it with the [url=http://www.php.net/sscanf]sscanf()[/url] function.

Here's my take on your code, with the debug statements left in:
[code]<?php
function perm_check($permissions, $module_permission, $permission_prefix, $action = 1) {
$temp_breakdown = explode(";",$permissions);
$temp_count = count($temp_breakdown);
for ($i = 0; $i < $temp_count; $i++){
$tmp_breakdown = $temp_breakdown[$i];
if(strstr($tmp_breakdown,$permission_prefix)){
$permission = explode(':',$tmp_breakdown);
list($permissionx) = sscanf($permission[1],"%X");
break;
}
}
echo '<pre>' . print_r($permission,true) . '</pre>';
echo sprintf("%08X<br>------<br>",$permissionx);
switch ($action) {
case 1:
if ($permissionx & $module_permission)
$val = 1;
else
$val = 0;
break;
case 2:
$permissiony = $permissionx | $module_permission;
$val = $permissiony;
break;
}
return $val;
}

$groups_permissions = "article:0x00000000;";
$view_thread = 16;
$view_post = 2;
$perms = perm_check($groups_permissions,$view_post,"article",2);
$chk_perms = $perms & $view_thread;
echo sprintf("%08X<br>%08X<br>%08X<br>",$perms,$view_thread,$chk_perms);
if ($chk_perms) {
print "Success!";
}
?>[/code]

Ken
Actually I took the print(s) from the code to clean it up some for posting here.

And I don't understand what you're saying... Are you saying the 0x isn't needed when you use dechex() on a string? So I should remove the 0x before parsing and add it again after parsing...?
I took out the 0x with a preg_match + sscanf, and at least its printing nomral numbers now... But somehow it "loses" a permission or something similar... Its very odd..

Code:
[code=php:0]
function perm_check($permissions, $module_permission, $permission_prefix, $action = 1) {
$temp_breakdown = explode(";",$permissions);
$temp_count = count($temp_breakdown);
for ($i = 0; $i != $temp_count; $i++){
if (is_array($temp_breakdown))
$tmp_breakdown = $temp_breakdown[$i];
else
$tmp_breakdown = $temp_breakdown;
if(strstr($tmp_breakdown,$permission_prefix)){
$permission = explode(':',$tmp_breakdown);
$permission = $permission[1];
//sscanf($permission,"%X",$permission);
//$module_permission = dechex($module_permission);
print "In here!<br>";
break;
} else {
$permission = $permissions;
print "Mod_perm: ".$module_permission."<br>";
$module_permission = dechex($module_permission);
}
}

if (preg_match("/0x/",$permission)) {
sscanf($permission,"%X",$permission);
print "Removing 0x: $permission<br>";
}

print "Permission above switch: ".$permission."<br>";
print "Module_permission above switch: ".$module_permission."<br>";
switch ($action) {
case 1:
print "<br>Permission: ".$permission."<br>";
print "Module_permission: ".$module_permission;
if ($permission & $module_permission)
return 1;
else
return 0;
break;
case 2:
$val = $permission | $module_permission;
return($val);
break;
}
}

$groups_permissions = "article:0x00000000;";
$view_thread = 16;
$view_post = 2;
print "First permission check:";
$perms = perm_check($groups_permissions,$view_post,"article",2);
print "perms in groups: $perms <br>";
print "<br><br>Second permission check:<br>";
$perms = perm_check($perms,$view_thread,"article",2);
print "perms in groups: $perms <br>";
print "<br><br>Third permission check (If):<br>";
if (perm_check($perms,$view_thread,"article",1)) {
print "<br>Success!";
}
[/code]


Output:
[quote]
First permission check:In here!
Removing 0x: 0
Permission above switch: 0
Module_permission above switch: 2
perms in groups: 2


Second permission check:
Mod_perm: 16
Permission above switch: 2
Module_permission above switch: 10
perms in groups: 10


Third permission check (If):
Mod_perm: 16
Permission above switch: 10
Module_permission above switch: 10

Permission: 10
Module_permission: 10
Success!
[/quote]

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.