• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

NX:About MAGIC flag in the Data file

 
xi ruo
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
db-1x2.db:
There is the discription in yy DBExplain document:
Start of file
4 byte numeric, magic cookie value identifies this as a data file
4 byte numeric, offset to start of record zero
2 byte numeric, number of fields in each record

What's the Magic cookie value???
 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Pie
Posts: 12007
215
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Xi
The magic cookie is just a number that you can use to identify that the file is likely to be a database file and is likely to be in your expected format.
If you determine what the magic cookie is in the file you have now, then each time you open the file you can verify that the magic cookie is still the same. That way, if you accidentally got someone else's database, or you tried to open some other file, you should notice immediately that the cookie number does not match, and you can stop processing immediately. Given that the cookie in your file is 4 bytes long, there is very little chance of any other file having the right cookie value.
If you have ever looked at the first four bytes of a compiled Java class, you should notice that that it contains 0xCAFEBABE (of course, since it is little endian, it appears as 0xFECABEBA). This is the magic cookie that the JVM looks for to ensure that it is actually loading a class file and not some other file.
Regards, Andrew
(Jim: I feel a FAQ coming on! )
 
xi ruo
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Andrew:
Thanks for your answer!!!
How do I ensure my MAGIC flag?
My DBfile is A non-relational database file.

The output is "253"!!!
So,What's the MAGIC flag???
thanks a lot!!!
 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Pie
Posts: 12007
215
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Xi
You are correct - the database provided is non relational (for which we can all be glad).
In reading your post, I felt that you could be asking two different questions:
  • What should you be using as the magic cookie.
    You should use the value 253 that you just read from your file. If you ever open a file and the first int you read is not 253 then you can stop proceessing because you know that the value found does not match your magic cookie.
    Other people will have different magic cookies. For instance the database file I have has a magic cookie of 256.
  • Why is this MAGIC?
    The term "Magic Cookie" has been around for a very long time, so I do not know the exact meaning of it. Here's my understanding:
    I think most people are aware of the concept of "fortune cookies" - a hard shell baked around a message. In computer terms cookies form the same function - the cookie contains a message of some sort, usually identification. For example, the cookies that are used to identify you when you log in to JavaRanch, so that new postings get highlighted. In these cases, the cookie generally is logical - it might be a date and time stamp. It will make sense to whoever is working on the code.
    But a magic cookie has no such meaning. Why do you have the value 253 and I have the value 256? No reason: it just happened to be that way "by magic".


  • So hopefully one of those two answers helped you. Probably the other one was just a waste of your time - sorry
    Regards, Andrew
     
    xi ruo
    Ranch Hand
    Posts: 40
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi Andrew:
    Your help avail me much.
    thanks a lot.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic