ArpitB

Read Binary File

Aug 5th, 2019
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.34 KB | None | 0 0
  1. // File Path of downloaded dafsa.bin in firefox stable - Ubuntu:
  2. // file:///home/arpit/.cache/mozilla/firefox/843zsjle.default/settings/main/public-suffix-list/dafsa.bin
  3.  
  4. // File Path of downloaded dafsa.bin in firefox nightly -  Ubuntu:
  5. // file:///home/arpit/Development/moz/mozilla-central/obj-x86_64-pc-linux-gnu/tmp/profile-default/settings/main/public-suffix-list/dafsa.bin
  6.  
  7. // File Path of downloaded dafsa.bin in firefox stable - Windows:
  8. // file://C:/Users/rpitt/AppData/Local/Mozilla/Firefox/Profiles/6fm1js29.default-1564744451242/settings/main/public-suffix-list/dafsa.bin
  9.  
  10. I considered using IFstream instead of fopen.
  11. - IFstream handles char16_t* natively, fopen does not.
  12.  
  13.  
  14. Current Method:
  15.  
  16. #include "mozilla/FStream.h"
  17.  
  18. NS_IMETHODIMP nsEffectiveTLDService::Observe(nsISupports* aSubject,
  19.                                              const char* aTopic,
  20.                                              const char16_t* aData) {
  21. // aData is like one of the examples mentioned above
  22.  
  23.   if (nsCRT::strcmp(aTopic, "public-suffix-list-updated") == 0) {
  24.    
  25.     const char16_t* fileURI = (char16_t*)((char*)aData + 7); // cut "file://" from aData
  26.     IFstream dafsaFile;
  27.  
  28.     dafsaFile.open(fileName, ifstream::binary | ifstream::ate);
  29.     if (dafsaFile.is_open()) {
  30.         unsigned int length = dafsaFile.tellg();
  31.         dafsaFile.seekg(ifstream::beg);
  32.         unsigned char kDafsaRemote[length];
  33.         dafsaFile.read((char*)kDafsaRemote, length);
  34.  
  35.         mGraph = mozilla::Dafsa(kDafsaRemote);
  36.      
  37.     }
  38.     dafsaFile.close();
  39.   return NS_OK;
  40. }
  41.  
  42. Previous Method:
  43.  
  44. NS_IMETHODIMP nsEffectiveTLDService::Observe(nsISupports* aSubject,
  45.                                              const char* aTopic,
  46.                                              const char16_t* aData) {
  47.  
  48.   if (nsCRT::strcmp(aTopic, "public-suffix-list-updated") == 0) {
  49.    
  50.     const char* fileURI = (char*) aData +7;  // cut "file://" from aData
  51.     FILE* fp = fopen(fileURI, "rb");
  52.     if(fp){
  53.       fseek(fp, 0, SEEK_END);
  54.       unsigned int size = ftell(fp);
  55.       rewind(fp);
  56.    
  57.       // Initialise kDafsaRemote with the data read from dafsa.bin
  58.       unsigned char kDafsaRemote[size];
  59.       fread(kDafsaRemote, size, 1, fp);
  60.  
  61.      nsEffectiveTLDService:: mGraph = mozilla::Dafsa(kDafsaRemote);
  62.     }
  63.  
  64.     fclose(fp);
  65.  
  66.   return NS_OK;
  67. }
Add Comment
Please, Sign In to add comment