Commit 65ace06e authored by chiaming2000's avatar chiaming2000
Browse files

Added crc32c checksum tag calculation utility.

This will be made available for Java API and simulator only after
protocol buffer defines/adds Algorithm enum token for CRC32C.  
parent 302e91da
Loading
Loading
Loading
Loading
+3 −1
Original line number Diff line number Diff line
@@ -25,7 +25,7 @@ import java.util.zip.Checksum;
import com.google.protobuf.ByteString;

/**
 * Kinetic tag calculation with CRC32 checksum algorithm utils.
 * Kinetic tag calculation with CRC32 checksum algorithm util.
 * 
 * @author chiaming
 *
@@ -35,8 +35,10 @@ public class Crc32TagCalc implements KineticTagCalc {
    private final static Logger logger = Logger.getLogger(Crc32TagCalc.class
            .getName());

    // crc32 algo
    private static String myName = "CRC32";

    // crc32 instance
    private Checksum crc32 = null;

    public Crc32TagCalc() {
+74 −0
Original line number Diff line number Diff line
/**
 * Copyright (C) 2014 Seagate Technology.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */
package com.seagate.kinetic.common.lib;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.handler.codec.compression.Snappy;

import java.nio.ByteBuffer;
import java.util.logging.Logger;

import com.google.protobuf.ByteString;

/**
 * Kinetic tag calculation with CRC32C checksum algorithm util.
 * 
 * @author chiaming
 *
 */
public class Crc32cTagCalc implements KineticTagCalc {

    private final static Logger logger = Logger.getLogger(Crc32cTagCalc.class
            .getName());

    // crc32c algo
    private static String myName = "CRC32c";

    public Crc32cTagCalc() {
        logger.info(myName + " checksum isntance instantiated ...");
    }

    @Override
    public ByteString calculateTag(byte[] value) {

        try {
            
            // netty io bytebuf
            ByteBuf bb = Unpooled.wrappedBuffer(value);
            
            // calculate crc32c checksum
            int cval = Snappy.calculateChecksum(bb);

            // convert to byte[]
            byte[] checkSum = ByteBuffer.allocate(4).putInt(cval).array();

            // convert to bytestring and return
            return ByteString.copyFrom(checkSum);

        } finally {
            ;
        }
    }

    @Override
    public String getAlgoName() {
        return myName;
    }

}
+27 −2
Original line number Diff line number Diff line
@@ -125,9 +125,9 @@ public class MessageDigestUtil {
    }

    /**
     * Get SHA2 instance.
     * Get CRC32 instance.
     * 
     * @return SHA2 instance.
     * @return CRC32 instance.
     */
    public static KineticTagCalc getCrc32Instance() {

@@ -148,6 +148,30 @@ public class MessageDigestUtil {
        return crc32;
    }

    /**
     * Get CRC32C instance.
     * 
     * @return CRC32C instance.
     */
    public static KineticTagCalc getCrc32cInstance() {

        // check if constructed
        if (crc32 != null) {
            return crc32;
        }

        // sync
        synchronized (MessageDigestUtil.class) {

            // check if already constructed
            if (crc32 == null) {
                crc32 = new Crc32cTagCalc();
            }
        }

        return crc32;
    }


    public static KineticTagCalc getInstance(Algorithm algo) {

@@ -158,6 +182,7 @@ public class MessageDigestUtil {
            return getSha2Instance();
        case CRC32:
            return getCrc32Instance();
            // return getCrc32cInstance();
        default:
            throw new java.lang.UnsupportedOperationException(
                    "unsupported algorithm., name = " + algo.name());