Exemplo n.º 1
0
Format* GetType(void* file_pointer, size_t file_size, const string& filename) {
  if (depth > max_depth)
    return new Format(file_pointer, file_size);

  if (!filename.empty()) {
    size_t dot = filename.find_last_of('.');
    if (dot != string::npos) {
      string ext = filename.substr(dot + 1);
      // toupper
      for (auto& c : ext)
        c &= ~0x20;

      if (ext == "HTML" || ext == "HTM" || ext == "JS" || ext == "CSS") {
        VerbosePrint(ext, " detected.");
        return new DataURI(file_pointer, file_size);
      }
      if (ext == "VCF" || ext == "VCARD") {
        VerbosePrint(ext, " detected.");
        return new Vcf(file_pointer, file_size);
      }
      if (ext == "MHT" || ext == "MHTML" || ext == "MIM" || ext == "MIME" || ext == "EML") {
        VerbosePrint(ext, " detected.");
        return new Mime(file_pointer, file_size);
      }
    }
  }
  if (memcmp(file_pointer, Png::header_magic, sizeof(Png::header_magic)) == 0) {
    VerbosePrint("PNG detected.");
    return new Png(file_pointer, file_size);
  } else if (memcmp(file_pointer, Jpeg::header_magic, sizeof(Jpeg::header_magic)) == 0) {
    VerbosePrint("JPEG detected.");
    return new Jpeg(file_pointer, file_size);
  } else if (memcmp(file_pointer, Lua::header_magic, sizeof(Lua::header_magic)) == 0) {
    VerbosePrint("Lua detected.");
    return new Lua(file_pointer, file_size);
  } else if (memcmp(file_pointer, Zip::header_magic, sizeof(Zip::header_magic)) == 0) {
    VerbosePrint("ZIP detected.");
    return new Zip(file_pointer, file_size);
  } else if (memcmp(file_pointer, Pe::header_magic, sizeof(Pe::header_magic)) == 0) {
    VerbosePrint("PE detected.");
    return new Pe(file_pointer, file_size);
  } else if (memcmp(file_pointer, Gz::header_magic, sizeof(Gz::header_magic)) == 0) {
    VerbosePrint("GZ detected.");
    return new Gz(file_pointer, file_size);
  } else if (memcmp(file_pointer, Ico::header_magic, sizeof(Ico::header_magic)) == 0) {
    VerbosePrint("ICO detected.");
    return new Ico(file_pointer, file_size);
  } else if (memcmp(file_pointer, Dwf::header_magic, sizeof(Dwf::header_magic)) == 0) {
    VerbosePrint("DWF detected.");
    return new Dwf(file_pointer, file_size);
  } else if (memcmp(file_pointer, Gft::header_magic, sizeof(Gft::header_magic)) == 0) {
    VerbosePrint("GFT detected.");
    return new Gft(file_pointer, file_size);
  } else if (memcmp(file_pointer, Rdb::header_magic, sizeof(Rdb::header_magic)) == 0) {
    VerbosePrint("RDB detected.");
    return new Rdb(file_pointer, file_size);
  } else if (memcmp(file_pointer, Swf::header_magic, sizeof(Swf::header_magic)) == 0 ||
             memcmp(file_pointer, Swf::header_magic_deflate, sizeof(Swf::header_magic_deflate)) == 0 ||
             memcmp(file_pointer, Swf::header_magic_lzma, sizeof(Swf::header_magic_lzma)) == 0) {
    VerbosePrint("SWF detected.");
    return new Swf(file_pointer, file_size);
  } else {
    // Search for vcard magic which might not be at the very beginning.
    const string vcard_magic = "BEGIN:VCARD";
    const char* fp = static_cast<char*>(file_pointer);
    const char* search_end = fp + std::min(static_cast<size_t>(1024), file_size);
    if (std::search(fp, search_end, vcard_magic.begin(), vcard_magic.end()) < search_end) {
      VerbosePrint("VCF detected.");
      return new Vcf(file_pointer, file_size);
    }

    // tar file does not have header magic
    // ustar is optional
    {
      Tar* t = new Tar(file_pointer, file_size);
      // checking first record checksum
      if (t->IsValid()) {
        VerbosePrint("tar detected.");
        return t;
      }
      delete t;
    }

    // XML file does not have header magic
    // have to parse and see if there are any errors.
    {
      Xml* x = new Xml(file_pointer, file_size);
      if (x->IsValid()) {
        VerbosePrint("XML detected.");
        return x;
      }
      delete x;
    }
  }

  VerbosePrint("Format not supported!");
  // for unsupported format, just memmove it.
  return new Format(file_pointer, file_size);
}
Exemplo n.º 2
0
Format *GetType(void *file_pointer, size_t file_size, const string& filename)
{
    if (depth > max_depth)
    {
        return new Format(file_pointer, file_size);
    }

    if (!filename.empty())
    {
        size_t dot = filename.find_last_of('.');
        if (dot != string::npos)
        {
            string ext = filename.substr(dot + 1);
            // toupper
            for (auto &c : ext)
                c &= ~0x20;

            if (ext == "HTML" ||
                ext == "HTM" ||
                ext == "JS" ||
                ext == "CSS")
            {
                if (is_verbose)
                {
                    cout << ext << " detected." << endl;
                }
                return new DataURI(file_pointer, file_size);
            }
        }

    }
    if (memcmp(file_pointer, Png::header_magic, sizeof(Png::header_magic)) == 0)
    {
        if (is_verbose)
        {
            cout << "PNG detected." << endl;
        }
        return new Png(file_pointer, file_size);
    }
    else if (memcmp(file_pointer, Jpeg::header_magic, sizeof(Jpeg::header_magic)) == 0)
    {
        if (is_verbose)
        {
            cout << "JPEG detected." << endl;
        }
        return new Jpeg(file_pointer, file_size);
    }
    else if (memcmp(file_pointer, Lua::header_magic, sizeof(Lua::header_magic)) == 0)
    {
        if (is_verbose)
        {
            cout << "Lua detected." << endl;
        }
        return new Lua(file_pointer, file_size);
    }
    else if (memcmp(file_pointer, Zip::header_magic, sizeof(Zip::header_magic)) == 0)
    {
        if (is_verbose)
        {
            cout << "ZIP detected." << endl;
        }
        return new Zip(file_pointer, file_size);
    }
    else if (memcmp(file_pointer, Pe::header_magic, sizeof(Pe::header_magic)) == 0)
    {
        if (is_verbose)
        {
            cout << "PE detected." << endl;
        }
        return new Pe(file_pointer, file_size);
    }
    else if (memcmp(file_pointer, Gz::header_magic, sizeof(Gz::header_magic)) == 0)
    {
        if (is_verbose)
        {
            cout << "GZ detected." << endl;
        }
        return new Gz(file_pointer, file_size);
    }
    else if (memcmp(file_pointer, Ico::header_magic, sizeof(Ico::header_magic)) == 0)
    {
        if (is_verbose)
        {
            cout << "ICO detected." << endl;
        }
        return new Ico(file_pointer, file_size);
    }
    else if (memcmp(file_pointer, Dwf::header_magic, sizeof(Dwf::header_magic)) == 0)
    {
        if (is_verbose)
        {
            cout << "DWF detected." << endl;
        }
        return new Dwf(file_pointer, file_size);
    }
    else if (memcmp(file_pointer, Gft::header_magic, sizeof(Gft::header_magic)) == 0)
    {
        if (is_verbose)
        {
            cout << "GFT detected." << endl;
        }
        return new Gft(file_pointer, file_size);
    }
    else if (memcmp(file_pointer, Rdb::header_magic, sizeof(Rdb::header_magic)) == 0)
    {
        if (is_verbose)
        {
            cout << "RDB detected." << endl;
        }
        return new Rdb(file_pointer, file_size);
    }
    else if (memcmp(file_pointer, Swf::header_magic, sizeof(Swf::header_magic)) == 0 ||
             memcmp(file_pointer, Swf::header_magic_deflate, sizeof(Swf::header_magic_deflate)) == 0 ||
             memcmp(file_pointer, Swf::header_magic_lzma, sizeof(Swf::header_magic_lzma)) == 0)
    {
        if (is_verbose)
        {
            cout << "SWF detected." << endl;
        }
        return new Swf(file_pointer, file_size);
    }
    else
    {
        // tar file does not have header magic
        // ustar is optional
        {
            Tar *t = new Tar(file_pointer, file_size);
            // checking first record checksum
            if (t->IsValid())
            {
                if (is_verbose)
                {
                    cout << "tar detected." << endl;
                }
                return t;
            }
            delete t;
        }

        // XML file does not have header magic
        // have to parse and see if there are any errors.
        {
            Xml *x = new Xml(file_pointer, file_size);
            if (x->IsValid())
            {
                if (is_verbose)
                {
                    cout << "XML detected." << endl;
                }
                return x;
            }
            delete x;
        }
    }

    if (is_verbose)
    {
        cout << "Format not supported!" << endl;
    }
    // for unsupported format, just memmove it.
    return new Format(file_pointer, file_size);
}