IFilter ErrorCode FILTER_E_ACCESS when reading .rtf (Rick text format) from byte array

I use this http://www.codeproject.com/Articles/31944/Implementing-a-TextReader-to-extract-various-files and basically works.

I wrote this test to check if a filter will read as expected from an array of bytes.

private const string ExpectedText = "This is a test!";
[Test]
public void FilterReader_RtfBytes_TextMatch()
{
    var bytes = File.ReadAllBytes(@"Test Documents\DocTest.rtf");
    var reader = new FilterReader(bytes, ".rtf");
    reader.Init();
    var actualText = reader.ReadToEnd();
    StringAssert.Contains(ExpectedText, actualText);
}

Test failed with ErrorCode: FILTER_E_ACCESS , it works fine when I give it a file name.

new FilterReader(@"Test Documents\DocTest.rtf", ".rtf"); <-- works

I am puzzled why this is so. I looked through the code and it seems that the dll rtf filter returns an error. What is even more puzzling.

It works great for other file types, for example; .doc, .docx, .pdf

+3
source share
1 answer

iFilter : FilterReader(byte[] bytes, string extension) IPersistStream , FilterReader(string path, string extension) - IPersistFile .

rtf-ifilter IPersistStream. , , .

, :

  • public void Init() -method
  • public FilterReader(string fileName, string extension, uint blockSize = 0x2000):

    #region Contracts
    
    Contract.Requires(!string.IsNullOrEmpty(fileName));
    Contract.Requires(!string.IsNullOrEmpty(extension));
    Contract.Requires(blockSize > 1);
    
    #endregion
    
    const string rtfExtension = ".rtf";
    
    FileName = fileName;
    Extension = extension;
    BufferSize = blockSize;
    
    _buffer = new char[ActBufferSize];
    
    // ! Take into account that Rtf-file can be loaded only using IPersistFile.
    var doUseIPersistFile = string.Compare(rtfExtension, extension, StringComparison.InvariantCultureIgnoreCase) == 0;
    
    // Initialize _filter instance.
    try
    {
        if (doUseIPersistFile)
        {
            // Load content using IPersistFile.
            _filter = FilterLoader.LoadIFilterFromIPersistFile(FileName, Extension);
        }
        else
        {
            // Load content using IPersistStream.
            using (var stream = new FileStream(path: fileName, mode: FileMode.Open, access: FileAccess.Read, share: FileShare.Read))
            {
                var buffer = new byte[stream.Length];
                stream.Read(buffer, 0, buffer.Length);
    
                _filter = FilterLoader.LoadIFilterFromStream(buffer, Extension);
            }
        }
    }
    catch (FileNotFoundException)
    {
        throw;
    }
    catch (Exception e)
    {
        throw new AggregateException(message: string.Format("Filter Not Found or Loaded for extension '{0}'.", Extension), innerException: e);
    }
    
+2

All Articles