home *** CD-ROM | disk | FTP | other *** search
- /* Xceed Binary Encoding Library - Decode Sample Application
- * Copyright (c) 2001 Xceed Software Inc.
- *
- * [Decode.cpp]
- *
- * This console application shows how to decode a file, encoded using
- * different methods. It specifically demonstrates:
- * - The ReadFile and ProcessFile methods.
- * - The ContinueOnInvalidData, DataFormating and EncodingFormat properties.
- *
- * This file is part of the Xceed Binary Encoding Library sample
- * applications. The source code in this file is only intended as
- * a supplement to Xceed Binary Encoding Library's documentation,
- * and is provided "as is", without warranty of any kind, either
- * expressed or implied.
- */
-
- #include "stdafx.h"
- #include <stdio.h>
- #include <string.h>
- #include <limits.h>
- #include "Decode.h"
-
- //
- // Mapping between command-line and enumeration values
- //
-
- static SEncodingMethod g_pxEncodingMethods[] =
- {
- { "=UU", emUUEncode }, //Default
- { "=XX", emXXEncode },
- { "=Base64", emBase64 },
- { "=Hex", emHexadecimal },
- { "=QP", emQuotedPrintable },
- { "=BHex", emBinHex },
- };
-
-
- //--------------------------------------------------------------------------
- // Entry point of the application
- //--------------------------------------------------------------------------
- int main(int argc, char* argv[])
- {
- // Initializes the COM library on the current thread following the STA model
- CoInitialize( NULL );
-
- try
- {
- // Create an instance of the XceedBinaryEncoding coclass, and
- // use the "I" interface which manipulates Byte array instead of
- // Variants as the "D" interface does.
- IXceedBinaryEncodingPtr piEncoding;
- piEncoding.CreateInstance( CLSID_XceedBinaryEncoding );
-
- // Two BSTR variables that will contain the Input and Output file names.
- _bstr_t bstrInputFileName;
- _bstr_t bstrOutputFileName;
-
- // Extract the command line parameters and initialize the Encoding
- // instance according to the user specification. After this call
- // the Encoding instance piEncoding is ready to decode.
- if( !ExtractParameters( argc, argv,
- piEncoding,
- bstrInputFileName,
- bstrOutputFileName ) )
- {
- ShowHelp();
- return 1;
- }
-
- if( bstrOutputFileName.length() == 0 )
- {
- // An output file name was not provided by the user.
- // Output the result to the console
-
- BYTE* pcDecoded = NULL;
- DWORD dwDecodedSize = 0;
- DWORD dwBytesRead = 0;
-
- // Decode by reading a file and sending the output to a buffer.
- // We specify :
- // - The Source filename, without an offset or size, since we want
- // to decode all the source file.
- // - The processing we want to perform, in this case bfpDecode.
- // - The bEndOfData parameter set to TRUE, since we do all the
- // processing in a single block.
- // - The address of a DWORD that will receive the number of bytes
- // actually read from the file.
- // - The address of a BYTE* and a DWORD that will receive the
- // decoded data and its size.
- piEncoding->ReadFile( bstrInputFileName, 0, 0,
- bfpDecode, TRUE,
- &dwBytesRead, &pcDecoded, &dwDecodedSize );
-
- if( pcDecoded )
- {
- // We simply output the decoded data to the console.
- fwrite( pcDecoded, sizeof( BYTE ), dwDecodedSize, stdout );
-
- // Always free the resulting data using the CoTaskMemFree COM API
- // function.
- CoTaskMemFree( pcDecoded );
- }
- }
- else
- {
- // An output file name was provided by the user.
- // Output the result to the specified file
-
- DWORD dwBytesWritten = 0;
- DWORD dwBytesRead = 0;
-
- // Decode by reading a file and sending the output to another file.
- // We specify :
- // - The source filename, without any offset or size, since we want
- // to decode all the source file.
- // - The processing we want to perform, in this case bfpDecode.
- // - The bEndOfData parameter set to TRUE, since we do all the
- // processing in a single block.
- // - The destination filename, with the bAppend parameter set to FALSE
- // since we want to overwrite any existing file.
- // - The address of two DWORD that will receive the number of bytes
- // actually read from the source, and written to the destination.
- piEncoding->ProcessFile( bstrInputFileName, 0, 0,
- bfpDecode, TRUE,
- bstrOutputFileName, FALSE,
- &dwBytesRead, &dwBytesWritten );
-
- printf( "Successfully decoded file %s to file %s\n",
- ( const char* )bstrInputFileName, ( const char* )bstrOutputFileName );
- }
- }
- catch( const _com_error& err )
- {
- // When using the "#import" directive, the compiler generates wrapper classes
- // around all interface types. These wrapper classes throw exceptions when
- // a method call returns an HRESULT which is a failure code.
- printf( "Error %08x: %s\n", err.Error(), ( const char* )err.Description() );
- }
- catch( ... )
- {
- // Catch any other exceptions
- printf( "An unknown error occured.\n" );
- }
-
- // Close the COM library for the current thread
- CoUninitialize();
-
- return 0;
- }
-
- //--------------------------------------------------------------------------
- // Display usage information
- //--------------------------------------------------------------------------
- void ShowHelp()
- {
- // "12345678901234567890123456789012345678901234567890123456789012345678901234567890"
- printf( "Usage: Decode [options] input_file [output_file]\n\n"
- " input_file: the file to be decoded\n"
- " output_file: the destination file\n\n"
- " options: /m=[UU | XX | Base64 | Hex : Encoding method\n"
- " QP | BHex] if omited, default to UU\n"
- " /i : Continue on invalid data\n"
- " if omitted, will stop at the first invalid data\n"
- " /h or /? : Show this help\n" );
- }
-
- //--------------------------------------------------------------------------
- // Extract commands from the parameters
- // In this function, we call the piEncode interface and let exceptions
- // be caught by the caller.
- // This function returns false if an error occured parsing the command
- // line parameters or if the user requested help.
- //--------------------------------------------------------------------------
- bool ExtractParameters( int argc, char* argv[],
- IXceedBinaryEncodingPtr piDecode,
- _bstr_t& bstrInputFileName,
- _bstr_t& bstrOutputFileName )
- {
- // These variables used to initialize the Encoding format property
- // are set, here, to their default values.
- SEncodingMethod* pxMethod = g_pxEncodingMethods;
- bool bContinueOnInvalidData = false;
-
- bool bFound;
-
- int i = 0;
-
- // We parse each command line parameter
- while( ++i < argc )
- {
- if( argv[ i ][ 0 ] == '/' )
- {
- // The parameter starts with a /
- // Meaning it's an option parameter
- switch( argv[ i ][ 1 ] )
- {
- case 'm':
- case 'M':
- // The user wants to set the encoding method
- bFound = false;
-
- // We go through all the encoding methods in the
- // correspondence table, stopping when we find a
- // match.
- for( pxMethod = g_pxEncodingMethods;
- pxMethod->pszCommandLine != NULL;
- pxMethod++ )
- {
- if( lstrcmpi( argv[ i ] + 2, pxMethod->pszCommandLine ) == 0 )
- {
- bFound = true;
- break;
- }
- }
-
- if( !bFound )
- {
- printf( "Invalid encoding method '%s'\n\n", argv[ i ] );
- return false;
- }
-
- break;
-
- case 'i':
- case 'I':
- // The user wants to continue decoding if invalid data are found
- bContinueOnInvalidData = true;
- break;
-
- default:
- printf( "Unknown command '%s'\n\n", argv[ i ] );
- // Continue
- case 'h':
- case 'H':
- case '?':
- return false;
- }
- }
- }
-
- // Check if the user provided an input file name
- if( argc < 2 || argv[ argc - 1 ][ 0 ] == '/' )
- {
- printf( "You did not specify an input filename\n\n" );
- return false;
- }
- else if( argc < 3 || argv[ argc - 2 ][ 0 ] == '/' )
- {
- // Only an input file was specified
- bstrInputFileName = argv[ argc - 1 ];
- }
- else
- {
- // Both input and output file were specified
- bstrInputFileName = argv[ argc - 2 ];
- bstrOutputFileName = argv[ argc - 1 ];
- }
-
- // According the Encoding method chosen by the user, we set the various
- // properties of an encoding format and prepare the
- // Encoding interface (piDecode).
-
- // The properties common to all encoding format are:
- // ContinueOnInvalidData
-
- // For each encoding method, we begin by creating a temporary instance
- // of the appropriate Encoding format. The properties of this instance
- // are then set and the instance is assign to the EncodingFormat property
- // of the XceedBinaryEncoding instance. This adds a reference to the
- // Encoding format instance so that the instance will not be freed
- // when it will fall out of scope.
-
- switch( pxMethod->eMethod )
- {
- case emUUEncode :
- {
- IXceedUUEncodingFormatPtr piUUFormat;
-
- piUUFormat.CreateInstance( CLSID_XceedUUEncodingFormat );
- piUUFormat->ContinueOnInvalidData = bContinueOnInvalidData ? TRUE : FALSE;
- // The input file has header and footer
- piUUFormat->IncludeHeaderFooter = TRUE;
- piDecode->EncodingFormat = IXceedEncodeDataPtr( piUUFormat );
- }
- break;
-
- case emXXEncode :
- {
- IXceedXXEncodingFormatPtr piXXFormat;
-
- piXXFormat.CreateInstance( CLSID_XceedXXEncodingFormat );
- piXXFormat->ContinueOnInvalidData = bContinueOnInvalidData ? TRUE : FALSE;
- // The input file has header and footer
- piXXFormat->IncludeHeaderFooter = TRUE;
- piDecode->EncodingFormat = IXceedEncodeDataPtr( piXXFormat );
- }
- break;
-
- case emBase64 :
- {
- IXceedBase64EncodingFormatPtr piBase64Format;
-
- piBase64Format.CreateInstance( CLSID_XceedBase64EncodingFormat );
- piBase64Format->ContinueOnInvalidData = bContinueOnInvalidData ? TRUE : FALSE;
- piDecode->EncodingFormat = IXceedEncodeDataPtr( piBase64Format );
- }
- break;
-
- case emHexadecimal :
- {
- IXceedHexaEncodingFormatPtr piHexaFormat;
-
- piHexaFormat.CreateInstance( CLSID_XceedHexaEncodingFormat );
- piHexaFormat->ContinueOnInvalidData = bContinueOnInvalidData ? TRUE : FALSE;
- piDecode->EncodingFormat = IXceedEncodeDataPtr( piHexaFormat );
- }
- break;
-
- case emQuotedPrintable :
- {
- IXceedQuotedPrintableEncodingFormatPtr piQuotedPrintableFormat;
-
- piQuotedPrintableFormat.CreateInstance( CLSID_XceedQuotedPrintableEncodingFormat );
- piQuotedPrintableFormat->ContinueOnInvalidData = bContinueOnInvalidData ? TRUE : FALSE;
- piDecode->EncodingFormat = IXceedEncodeDataPtr( piQuotedPrintableFormat );
- }
- break;
-
- case emBinHex :
- {
- IXceedBinHexEncodingFormatPtr piBinHexFormat;
-
- piBinHexFormat.CreateInstance( CLSID_XceedBinHexEncodingFormat );
- piBinHexFormat->ContinueOnInvalidData = bContinueOnInvalidData ? TRUE : FALSE;
- // The input file has header and footer
- piBinHexFormat->IncludeHeaderFooter = TRUE;
- piDecode->EncodingFormat = IXceedEncodeDataPtr( piBinHexFormat );
- }
- break;
- }
-
- return true;
- }
-
-
- //
- // END_OF_FILE
- //
-