The phone.dat file is a binary format structured as follows:
File Structure
| Section | Size | Description |
|---|
| Header | 8 bytes | 4 bytes for version, 4 bytes for the offset of the first index |
| Record Area | Variable | Contains the actual location data |
| Index Area | Variable | Contains the lookup indices |
Data Formats
Record Area
Each record is a null-terminated string in the format:
<Province>|<City>|<Postcode>|<Long-distance Area Code>\0
Index Area
Each index entry is exactly 9 bytes (<iiB format):
<First 7 digits of phone number><Offset to Record Area><Card Type>
Card Type Mapping
When parsing the index, the Card Type byte corresponds to:
1: China Mobile (移动)2: China Unicom (联通)3: China Telecom (电信)4: Telecom Virtual Operator (电信虚拟运营商)5: Unicom Virtual Operator (联通虚拟运营商)6: Mobile Virtual Operator (移动虚拟运营商)
Parsing Logic
- Parse the 8-byte header to find the offset of the first index in the Index Area.
- Perform a binary search in the Index Area using the first 7 digits of the phone number to find the corresponding offset in the Record Area.
- Navigate to the offset in the Record Area and read the data until the null terminator (
\0) is encountered.
| 4 bytes | <- phone.dat version
------------
| 4 bytes | <- Offset to the first index
-----------------------
| offset - 8 | <- Record Area
-----------------------
| index | <- Index Area