Magic numbers '&' operator problem


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Magic numbers '&' operator problem
# 1  
Old 07-04-2011
Magic numbers '&' operator problem

Hello everyone,

on the man page of "magic(5)"
There is explanation
"&, to specify that the value from the file must have set all of the bits that are set in the specified value" .

My question is that what is the difference between '&' and equal operator '=' ? I tested it with file command but I do not understand the work way of it. For example, I prepared a magic.mime named 'mymagic' including only one line that is " 0 ubyte &0x00 filetypededected" and I create a file named 'myfile' setting its first byte to \x4e . when i call 'file -m mymagic myfile' it prints filetypededected so how can it be?
# 2  
Old 07-05-2011
Quote:
Originally Posted by segmentation
"&, to specify that the value from the file must have set all of the bits that are set in the specified value" .

" 0 ubyte &0x00 filetypededected"
I don't have ubyte on my man page, what system are you using? Ignoring that, &0x00 says to scan 0x00 looking only at the 1 bits. Any 1 bits appearing in 0x00 must be matched by the corresponding bit in the byte under test. Since &0x00 has no 1 bits, all bytes will match it. In contrast, =0x00 means that only a byte of zero matches.
# 3  
Old 07-05-2011
Thank you very much friend it works now. "ubyte" is not writing on man pages but it is mentioned in somewhere in magic.mime database files and ulong, ubelong ... , as well. So "^" operator is vice versa of "&" we care about 0 bits, isn't it?


Aaa, I tested "^" operator for 0 bits this time but I have not idea about it.

In magic file, I inserted 0xd3 which is 11010011 in binary form
and in my file which I will test has 0xb1 on his first byte which is 10110001.
I operate ^ operator as "Or" and "exOr" but i could not get any idea.Could you help for this operator?

Last edited by segmentation; 07-05-2011 at 04:50 PM.. Reason: wrong guess
# 4  
Old 07-05-2011
"&0x1" means low order bit must be 1
"^0x1" means low order bit must be 0

And you're right, I have ubyte in my files but not on my man page.
# 5  
Old 07-05-2011
Sorry, I do not understand functionality of ^ operator by this sentences
" "^0x1" means low order bit must be 0 "

I posted above and will write again clearly.

In my magic.mime there is only one line
"0 byte ^0xd3 file\ detected"

and in my test file named myfile, the first byte is 0xb1.

0xd3 = 11010011
0xb1 = 10110001

when I write 'file -m magic.mime myfile'
it prints "file detected".

According to your explanation there should be 0 bits on testing byte corresponding 1 bits in magic.mime but in example above there is nothing like that.
# 6  
Old 07-05-2011
Xor is a bitwise operator:
0^0=0
0^1=1
1^0=1
1^1=0

If you have multiple 1 bits it is no longer a simple test.
Code:
0xd3 = 11010011
0xb1 = 10110001
       --------
0x62 = 01100010

It's not real obvious why you would do that. But the result is a match. Do you know C language? If so it is like:
Code:
a=0xd3;
b=0xb1;
if (a ^ b)   printf("match\n");

Make them both 0xb1 and the test will fail.
Code:
$
$ cat /tmp/mymagic
0 byte ^0xb1 filedetected
$ od -tx1 /tmp/x
0000000 b1 0a
0000002
$ file -m /tmp/mymagic /tmp/x
/tmp/x: ISO-8859 text
$

# 7  
Old 07-06-2011
I choose a=0x22 and b=0x32.
In C they match but in magic test not matched. When we think like C language it is similar to not equal but it does not work every time. I am implementing filetype detection in erlang so I need functionalities of magic operators.I still have problem with ^ operator. Smilie

Edit:
The problem is solved.
At least one 0 bit should be placed in the test file instead of corresponding 1 bits in magic file.

Thank you very much for your helps.

Last edited by segmentation; 07-06-2011 at 08:41 AM.. Reason: Problem Solved
 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Linux Platform - NDM Script - && Operator

Hi All, I have a requirement where i need to NDM 3 files from LINUX to Mainframe system & trigger a job in mainframe once the 3 files are transmitted successfully. I am getting an error message in the && operator (the code component where i am checking whether step 1/2/3 are completed). ... (2 Replies)
Discussion started by: dsfreddie
2 Replies

2. UNIX for Dummies Questions & Answers

Magic numbers string/B problem

Hello, In manpage magic(5) " The “B” flag compacts whitespace in the target, which must contain at least one whitespace character. If the magic has n consecutive blanks, the target needs at least n consecutive blanks to match. The “b” flag treats every blank in the target as an optional... (4 Replies)
Discussion started by: segmentation
4 Replies

3. Shell Programming and Scripting

How to write If statement using && and operator in Unix

Hi What is the syntax for if statement using && and || operator? if && ] || here its giving me an error to this if statement any suggestion?? (2 Replies)
Discussion started by: Avi
2 Replies

4. Shell Programming and Scripting

Alternative to && operator

"Both cases have to be true for the expression to evaluate "true". is that not so. the following code does a read from a txt file, an then suppose to do the deletion ,but although the condition exists, its not performing the deletion && echo "\n\t Deleting printer ${prt} !!!" if !... (5 Replies)
Discussion started by: ggoliath
5 Replies

5. UNIX for Dummies Questions & Answers

problem with pipe operator

hi i am having issues with extra pipe. i have a data file and i need to remove the extra pipe in the(example 4th and 7thline) in datafile. there are many other line and filed like this which i need to remove from files. The sample data is below: 270 31|455004|24/03/2010|0001235|72 271... (3 Replies)
Discussion started by: abhi_n123
3 Replies

6. UNIX for Dummies Questions & Answers

Solaris magic number problem

Hello, When I boot up my ancient SUNOS 5 system. it stops at the OK prompt and complains about a bad magic number. I have told that I need run fsck but I cannot seem to do so from the OK prompt. How can I get into a diagnostic mode so I can run fsck? Thanks, (1 Reply)
Discussion started by: mojoman
1 Replies

7. UNIX for Dummies Questions & Answers

Problem unary operator expected

I get the following error ./get_NE05: line 42: while do echo ${STRING_NAME} J=1 if ; then EXT=0$I else EXT=$I fi while do echo $I-$J #calculating last occurrence OCCURRENCE=`grep -io "${STRING_NAME}"... (3 Replies)
Discussion started by: f_o_555
3 Replies

8. Shell Programming and Scripting

multiple conditions in if using && operator

VARIABLE="project" if && ] then echo "VARIABLE is not empty" fi this is not working what is wrong in the syntax?? (2 Replies)
Discussion started by: codeman007
2 Replies

9. Programming

Problem about c++ new operator

#include <iostream.h> class test { private: int i; public: inline test(int m) { i = m; } inline int get_i() { return i; } }; int main() { test * a = new test(2); (3 Replies)
Discussion started by: xbjxbj
3 Replies
Login or Register to Ask a Question