File indexing completed on 2024-12-01 07:32:39

0001 /*
0002     SPDX-FileCopyrightText: 2006-2009 Sebastian Trueg <trueg@k3b.org>
0003     SPDX-FileCopyrightText: 1998-2009 Sebastian Trueg <trueg@k3b.org>
0004 
0005     SPDX-License-Identifier: GPL-2.0-or-later
0006 */
0007 
0008 #include "k3bchecksumpipe.h"
0009 
0010 #include <QDebug>
0011 #include <QCryptographicHash>
0012 
0013 #include <unistd.h>
0014 
0015 
0016 class K3b::ChecksumPipe::Private
0017 {
0018 public:
0019     Private()
0020         : checksumType(MD5), md5(QCryptographicHash::Md5) {
0021     }
0022 
0023     void update( const char* in, qint64 len ) {
0024         switch( checksumType ) {
0025         case MD5:
0026             md5.addData( in, len );
0027             break;
0028         }
0029     }
0030 
0031     void reset() {
0032         switch( checksumType ) {
0033         case MD5:
0034             md5.reset();
0035             break;
0036         }
0037     }
0038 
0039     int checksumType;
0040 
0041     QCryptographicHash md5;
0042 };
0043 
0044 
0045 K3b::ChecksumPipe::ChecksumPipe()
0046     : K3b::ActivePipe()
0047 {
0048     d = new Private();
0049 }
0050 
0051 
0052 K3b::ChecksumPipe::~ChecksumPipe()
0053 {
0054     delete d;
0055 }
0056 
0057 
0058 bool K3b::ChecksumPipe::open( bool closeWhenDone )
0059 {
0060     return open( MD5, closeWhenDone );
0061 }
0062 
0063 
0064 bool K3b::ChecksumPipe::open( Type type, bool closeWhenDone )
0065 {
0066     d->reset();
0067     d->checksumType = type;
0068     return K3b::ActivePipe::open( closeWhenDone );
0069 }
0070 
0071 
0072 QByteArray K3b::ChecksumPipe::checksum() const
0073 {
0074     switch( d->checksumType ) {
0075     case MD5:
0076         return d->md5.result().toHex();
0077     }
0078 
0079     return QByteArray();
0080 }
0081 
0082 
0083 qint64 K3b::ChecksumPipe::writeData( const char* data, qint64 max )
0084 {
0085     d->update( data, max );
0086     return K3b::ActivePipe::writeData( data, max );
0087 }
0088 
0089 
0090 bool K3b::ChecksumPipe::open( OpenMode mode )
0091 {
0092     return ActivePipe::open( mode );
0093 }
0094 
0095 #include "moc_k3bchecksumpipe.cpp"