Logo Search packages:      
Sourcecode: highlight version File versions  Download package

bool highlight::CodeGenerator::validateInputStream (  )  [private]

Returns:
true if input is no binary stream

Definition at line 663 of file codegenerator.cpp.

References in.

Referenced by generateFile(), and generateStringFromFile().

      {
            if ( !in ) return false;

            // it is not possible to move stream pointer back with stdin
            if ( ( int ) in->tellg() == -1 ) // -1 : stdin
                  return true;

            // Sources: http://en.wikipedia.org/wiki/Magic_number_(programming)
            // Magic configuration of "file"
            // This is intended for web plugins - only check filetypes often found in the net
            char magic_gif[]    = {'G','I','F','8', 0};
            char magic_png[]    = {0x89,'P','N','G', 0};
            char magic_java[]   = {0xCA,0xFE,0xBA,0xBE, 0};
            char magic_jpeg[]   = {0xFF,0xD8,0xFF, 0};
            char magic_bmp[]    = {'B','M', 0};
            char magic_pdf[]    = {'%','P','D','F', 0};
            char magic_utf8[]   = {0xEF,0xBB,0xBF,0};
            char magic_rar[]    = {'R','a','r','!', 0};
            char magic_zip[]    = {'P','K',0x03,0x04, 0};
            char magic_ace[]    = {'*','*','A','C','E','*','*', 0};
            char magic_tgz[]    = {0x8b,0x1f, 0x00, 0x08, 0};
            char magic_bzip[]   = {'B','Z', 0};

            char* magic_table[] = {magic_utf8,
                                   magic_gif, magic_png, magic_jpeg, magic_bmp, magic_pdf,
                                   magic_java,
                                   magic_rar, magic_zip, magic_ace, magic_tgz, magic_bzip,
                                   0
                                  };

            char buffer [10]={0};
            in->read ( buffer,8 );  //only read the first 8 bytes of input stream

            int magic_index=0;
            while ( magic_table[magic_index] )
            {
                  if ( !strncmp ( buffer, magic_table[magic_index], strlen ( magic_table[magic_index] ) ) )
                  {
                        break;
                  }
                  magic_index++;
            }
            int streamReadPos=0;
            if ( magic_table[magic_index] == magic_utf8 )
            {
                  //setEncoding("utf-8");
                  streamReadPos=3; // remove UTF-8 magic number from output
            }

            in -> seekg ( streamReadPos, ios::beg );
            in-> clear();  // clear fail bit to continue reading

            return !magic_table[magic_index] // points to 0 if no pattern was found
                   || magic_table[magic_index] == magic_utf8;
      }


Generated by  Doxygen 1.6.0   Back to index