void extract( POLE::Storage* storage, char* stream_name, char* outfile )
{
  POLE::Stream* stream = new POLE::Stream( storage, stream_name );
  if( !stream ) return;
  if( stream->fail() ) return;

  std::ofstream file;
  file.open( outfile, std::ios::binary|std::ios::out );

  unsigned char buffer[16];
  for( ;; )
  {
      unsigned read = stream->read( buffer, sizeof( buffer ) );
      file.write( (const char*)buffer, read  );
      if( read < sizeof( buffer ) ) break;
  }
  file.close();

  delete stream;
}
void dump( POLE::Storage* storage, char* stream_name )
{
  POLE::Stream* stream = new POLE::Stream( storage, stream_name );
  if( !stream ) return;
  if( stream->fail() ) return;

  // std::cout << "Size: " << stream->size() << " bytes" << std::endl;
  unsigned char buffer[16];
  for( ;; )
  {
      unsigned read = stream->read( buffer, sizeof( buffer ) );
      for( unsigned i = 0; i < read; i++ )
        printf( "%02x ", buffer[i] );
      std::cout << "    ";
      for( unsigned i = 0; i < read; i++ )
        printf( "%c", ((buffer[i]>=32)&&(buffer[i]<128)) ? buffer[i] : '.' );
      std::cout << std::endl;
      if( read < sizeof( buffer ) ) break;
  }

  delete stream;
}
void visit( int indent, POLE::Storage* storage, std::string path )
{
  std::list<std::string> entries;
  entries = storage->entries( path );

  std::list<std::string>::iterator it;
  for( it = entries.begin(); it != entries.end(); ++it )
  {
    std::string name = *it;
    std::string fullname = path + name;
    for( int j = 0; j < indent; j++ ) std::cout << "    ";
    POLE::Stream* ss = new POLE::Stream( storage, fullname );
    std::cout << name;
    if( ss ) if( !ss->fail() )std::cout << "  (" << ss->size() << ")";
    std::cout << std::endl;
    delete ss;

    if( storage->isDirectory( fullname ) )
      visit( indent+1, storage, fullname + "/" );
  }

}
// Extract encryption data from Microsoft Word file
// Place 48 bytes at record_out:
// 16 byte Salt, followed by
// 16 byte EncryptedVerifier, followed by
// 16 byte EncryptedVerifierHash
extern "C" void extract_doc(const char *file_name, unsigned char *record_out) {
  int n; // Used for number of bytes read
  POLE::Storage* storage = new POLE::Storage(file_name );
  storage->open();
  if( storage->result() != POLE::Storage::Ok )
  {
    std::cout << "Error on file " << std::endl;
    exit(1);
    return;
  }

  // Check File Information Block to see if document is encrypted

  POLE::Stream* stream = new POLE::Stream( storage, "WordDocument" );
  if (!stream || stream->fail() ) {
	std::cerr << "Could not open WordDocument stream\n";
	exit(1);
  }

  // FibBase should be at the beginning of the WordDocument stream
  unsigned char wIdent[2];
  n = stream->read(wIdent, 2);
  if (n != 2) return;

  if (! (wIdent[0] == 0xEC && wIdent[1] == 0xA5 )) {
    std::cerr << "FibBase not found\n";
    exit(1);
    return;
  }

  // fEncrypted is in 12th byte of FibBase. 2 bytes have been read
  // already and 12 = 2 + 10.
  unsigned char byte;
  for (int i = 1; i <= 10; i++) {
  	n = stream->read(&byte, 1);
	if (n != 1) {
		std::cerr << "Error reading FibBase\n";
		exit(1);
	}
  }

  if ((byte & 0x01) == 0) {
	std::cerr << "File is not encrypted\n";
	exit(1);
  }

  // Look for encryption header in 1Table or 0Table stream
  // See http://msdn.microsoft.com/en-us/library/dd923367(v=office.12).aspx
  // and http://msdn.microsoft.com/en-us/library/dd908560(v=office.12).aspx

  delete stream;
  stream = new POLE::Stream( storage, "1Table" );
  if (!stream || stream->fail() ) {
	stream = new POLE::Stream( storage, "0Table" );
  }
  if (!stream || stream->fail() ) {
	std::cerr << "Couldn't open 1Table or 0Table stream\n";
	exit(1);
  }
  unsigned char EncryptionHeader[52];
  n = stream->read(EncryptionHeader, 52);
  memcpy(record_out, EncryptionHeader + 4, 48);
}
KoFilter::ConversionStatus HancomWordImport::convert( const QByteArray& from, const QByteArray& to )
{
  if ( from != "application/x-hwp" )
    return KoFilter::NotImplemented;

  if ( to != "application/vnd.oasis.opendocument.text" )
    return KoFilter::NotImplemented;

  d->inputFile = m_chain->inputFile();
  d->outputFile = m_chain->outputFile();
  d->paragraphs.clear();

  POLE::Storage storage( QFile::encodeName(d->inputFile) );
  if( !storage.open() )
    return KoFilter::WrongFormat;

  POLE::Stream* stream;
  stream = new POLE::Stream( &storage, "/PrvText" );
  if( stream->fail() || (stream->size() == 0) )
  {
    delete stream;
    return KoFilter::WrongFormat;
  }

  int len = stream->size() / 2;
  QString plaindoc;
  plaindoc.reserve( len );

  unsigned char* buf = new unsigned char [stream->size()];
  stream->read( buf, stream->size());
  for(int i = 0; i < len; i++ )
    plaindoc.append( QChar((int)readU16(buf + i*2) ) );
  delete[] buf;
  delete stream;

  // split into paragraphs
  d->paragraphs = QStringList::split( "\n", plaindoc, true );

  // create output store
  KoStore* storeout;
  storeout = KoStore::createStore( d->outputFile, KoStore::Write,
    "application/vnd.oasis.opendocument.text", KoStore::Zip );

  if ( !storeout )
  {
    kWarning() << "Couldn't open the requested file.";
    return KoFilter::FileNotFound;
  }

  if ( !storeout->open( "styles.xml" ) )
  {
    kWarning() << "Couldn't open the file 'styles.xml'.";
    return KoFilter::CreationError;
  }
  storeout->write( d->createStyles() );
  storeout->close();

  if ( !storeout->open( "content.xml" ) )
  {
    kWarning() << "Couldn't open the file 'content.xml'.";
    return KoFilter::CreationError;
  }
  storeout->write( d->createContent() );
  storeout->close();

  // store document manifest
  storeout->enterDirectory( "META-INF" );
  if ( !storeout->open( "manifest.xml" ) )
  {
     kWarning() << "Couldn't open the file 'META-INF/manifest.xml'.";
     return KoFilter::CreationError;
  }
  storeout->write( d->createManifest() );
  storeout->close();

  // we are done!
  d->inputFile.clear();
  d->outputFile.clear();
  delete storeout;

  return KoFilter::OK;
}