BaseJetCryptCodecHandler.java

/*
Copyright (c) 2013 James Ahlborn

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package com.healthmarketscience.jackcess.crypt.impl;

import java.nio.ByteBuffer;

import com.healthmarketscience.jackcess.crypt.util.StreamCipherCompat;
import com.healthmarketscience.jackcess.crypt.util.StreamCipherFactory;
import com.healthmarketscience.jackcess.impl.CodecHandler;
import com.healthmarketscience.jackcess.impl.PageChannel;


/**
 * Base CodecHandler support for Jet RC4 encryption based CodecHandlers.
 *
 * @author James Ahlborn
 */
public abstract class BaseJetCryptCodecHandler extends BaseCryptCodecHandler
{
  private StreamCipherCompat _engine;

  protected BaseJetCryptCodecHandler(PageChannel channel, byte[] encodingKey) {
    super(channel, encodingKey);
  }

  @Override
  public boolean canEncodePartialPage() {
    // RC4 ciphers are not influenced by the page contents, so we can easily
    // encode part of the buffer.
    return true;
  }

  @Override
  public boolean canDecodeInline() {
    // RC4 ciphers can decode on top of the input buffer
    return true;
  }

  @Override
  protected final StreamCipherCompat getStreamCipher() {
    if(_engine == null) {
      _engine = StreamCipherFactory.newRC4Engine();
    }
    return _engine;
  }

  @Override
  public void decodePage(ByteBuffer inPage, ByteBuffer outPage, int pageNumber) {
    if(!isEncryptedPage(pageNumber)) {
      // not encoded
      return;
    }

    streamDecrypt(inPage, pageNumber);
  }

  @Override
  public ByteBuffer encodePage(ByteBuffer buffer, int pageNumber,
                               int pageOffset) {
    if(!isEncryptedPage(pageNumber)) {
      // not encoded
      return buffer;
    }

    return streamEncrypt(buffer, pageNumber, pageOffset);
  }

  private boolean isEncryptedPage(int pageNumber) {
    return ((pageNumber > 0) && (pageNumber <= getMaxEncodedPage()));
  }

  protected abstract int getMaxEncodedPage();
}