~ubuntu-branches/ubuntu/trusty/virtualbox-lts-xenial/trusty-updates

« back to all changes in this revision

Viewing changes to src/VBox/Runtime/common/checksum/RTSha1Digest.cpp

  • Committer: Package Import Robot
  • Author(s): Gianfranco Costamagna
  • Date: 2016-02-23 14:28:26 UTC
  • Revision ID: package-import@ubuntu.com-20160223142826-bdu69el2z6wa2a44
Tags: upstream-4.3.36-dfsg
ImportĀ upstreamĀ versionĀ 4.3.36-dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* $Id: RTSha1Digest.cpp $ */
 
2
/** @file
 
3
 * IPRT - SHA1 digest creation
 
4
 *
 
5
 * @todo Replace this with generic RTCrDigest based implementation. Too much
 
6
 *       stupid code duplication.
 
7
 */
 
8
 
 
9
/*
 
10
 * Copyright (C) 2009-2010 Oracle Corporation
 
11
 *
 
12
 * This file is part of VirtualBox Open Source Edition (OSE), as
 
13
 * available from http://www.virtualbox.org. This file is free software;
 
14
 * you can redistribute it and/or modify it under the terms of the GNU
 
15
 * General Public License (GPL) as published by the Free Software
 
16
 * Foundation, in version 2 as it comes in the "COPYING" file of the
 
17
 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
 
18
 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
 
19
 *
 
20
 * The contents of this file may alternatively be used under the terms
 
21
 * of the Common Development and Distribution License Version 1.0
 
22
 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
 
23
 * VirtualBox OSE distribution, in which case the provisions of the
 
24
 * CDDL are applicable instead of those of the GPL.
 
25
 *
 
26
 * You may elect to license modified versions of this file under the
 
27
 * terms and conditions of either the GPL or the CDDL or both.
 
28
 */
 
29
 
 
30
 
 
31
/*******************************************************************************
 
32
*   Header Files                                                               *
 
33
*******************************************************************************/
 
34
#include "internal/iprt.h"
 
35
#include <iprt/sha.h>
 
36
 
 
37
#include <iprt/alloca.h>
 
38
#include <iprt/assert.h>
 
39
#include <iprt/mem.h>
 
40
#include <iprt/string.h>
 
41
#include <iprt/file.h>
 
42
 
 
43
 
 
44
RTR3DECL(int) RTSha1Digest(void* pvBuf, size_t cbBuf, char **ppszDigest, PFNRTPROGRESS pfnProgressCallback, void *pvUser)
 
45
{
 
46
    /* Validate input */
 
47
    AssertPtrReturn(pvBuf, VERR_INVALID_POINTER);
 
48
    AssertPtrReturn(ppszDigest, VERR_INVALID_POINTER);
 
49
    AssertPtrNullReturn(pfnProgressCallback, VERR_INVALID_PARAMETER);
 
50
 
 
51
    int rc = VINF_SUCCESS;
 
52
    *ppszDigest = NULL;
 
53
 
 
54
    /* Initialize the hash context. */
 
55
    RTSHA1CONTEXT Ctx;
 
56
    RTSha1Init(&Ctx);
 
57
 
 
58
    /* Buffer size for progress callback */
 
59
    double rdMulti = 100.0 / (cbBuf ? cbBuf : 1);
 
60
 
 
61
    /* Working buffer */
 
62
    char *pvTmp = (char*)pvBuf;
 
63
 
 
64
    /* Process the memory in blocks */
 
65
    size_t cbReadTotal = 0;
 
66
    for (;;)
 
67
    {
 
68
        size_t cbRead = RT_MIN(cbBuf - cbReadTotal, _1M);
 
69
        RTSha1Update(&Ctx, pvTmp, cbRead);
 
70
        cbReadTotal += cbRead;
 
71
        pvTmp += cbRead;
 
72
 
 
73
        /* Call the progress callback if one is defined */
 
74
        if (pfnProgressCallback)
 
75
        {
 
76
            rc = pfnProgressCallback((unsigned)(cbReadTotal * rdMulti), pvUser);
 
77
            if (RT_FAILURE(rc))
 
78
                break; /* canceled */
 
79
        }
 
80
        /* Finished? */
 
81
        if (cbReadTotal == cbBuf)
 
82
            break;
 
83
    }
 
84
    if (RT_SUCCESS(rc))
 
85
    {
 
86
        /* Finally calculate & format the SHA1 sum */
 
87
        uint8_t abHash[RTSHA1_HASH_SIZE];
 
88
        RTSha1Final(&Ctx, abHash);
 
89
 
 
90
        char *pszDigest;
 
91
        rc = RTStrAllocEx(&pszDigest, RTSHA1_DIGEST_LEN + 1);
 
92
        if (RT_SUCCESS(rc))
 
93
        {
 
94
            rc = RTSha1ToString(abHash, pszDigest, RTSHA1_DIGEST_LEN + 1);
 
95
            if (RT_SUCCESS(rc))
 
96
                *ppszDigest = pszDigest;
 
97
            else
 
98
                RTStrFree(pszDigest);
 
99
        }
 
100
    }
 
101
 
 
102
    return rc;
 
103
}
 
104
 
 
105
RTR3DECL(int) RTSha1DigestFromFile(const char *pszFile, char **ppszDigest, PFNRTPROGRESS pfnProgressCallback, void *pvUser)
 
106
{
 
107
    /* Validate input */
 
108
    AssertPtrReturn(pszFile, VERR_INVALID_POINTER);
 
109
    AssertPtrReturn(ppszDigest, VERR_INVALID_POINTER);
 
110
    AssertPtrNullReturn(pfnProgressCallback, VERR_INVALID_PARAMETER);
 
111
 
 
112
    *ppszDigest = NULL;
 
113
 
 
114
    /* Open the file to calculate a SHA1 sum of */
 
115
    RTFILE hFile;
 
116
    int rc = RTFileOpen(&hFile, pszFile, RTFILE_O_OPEN | RTFILE_O_READ | RTFILE_O_DENY_WRITE);
 
117
    if (RT_FAILURE(rc))
 
118
        return rc;
 
119
 
 
120
    /* Fetch the file size. Only needed if there is a progress callback. */
 
121
    double rdMulti = 0;
 
122
    if (pfnProgressCallback)
 
123
    {
 
124
        uint64_t cbFile;
 
125
        rc = RTFileGetSize(hFile, &cbFile);
 
126
        if (RT_FAILURE(rc))
 
127
        {
 
128
            RTFileClose(hFile);
 
129
            return rc;
 
130
        }
 
131
        rdMulti = 100.0 / (cbFile ? cbFile : 1);
 
132
    }
 
133
 
 
134
    /* Allocate a reasonably large buffer, fall back on a tiny one. */
 
135
    void  *pvBufFree;
 
136
    size_t cbBuf = _1M;
 
137
    void  *pvBuf = pvBufFree = RTMemTmpAlloc(cbBuf);
 
138
    if (!pvBuf)
 
139
    {
 
140
        cbBuf = 0x1000;
 
141
        pvBuf = alloca(cbBuf);
 
142
    }
 
143
 
 
144
    /* Initialize the hash context. */
 
145
    RTSHA1CONTEXT Ctx;
 
146
    RTSha1Init(&Ctx);
 
147
 
 
148
    /* Read that file in blocks */
 
149
    size_t cbReadTotal = 0;
 
150
    for (;;)
 
151
    {
 
152
        size_t cbRead;
 
153
        rc = RTFileRead(hFile, pvBuf, cbBuf, &cbRead);
 
154
        if (RT_FAILURE(rc) || !cbRead)
 
155
            break;
 
156
        RTSha1Update(&Ctx, pvBuf, cbRead);
 
157
        cbReadTotal += cbRead;
 
158
 
 
159
        /* Call the progress callback if one is defined */
 
160
        if (pfnProgressCallback)
 
161
        {
 
162
            rc = pfnProgressCallback((unsigned)(cbReadTotal * rdMulti), pvUser);
 
163
            if (RT_FAILURE(rc))
 
164
                break; /* canceled */
 
165
        }
 
166
    }
 
167
    RTMemTmpFree(pvBufFree);
 
168
    RTFileClose(hFile);
 
169
 
 
170
    if (RT_FAILURE(rc))
 
171
        return rc;
 
172
 
 
173
    /* Finally calculate & format the SHA1 sum */
 
174
    uint8_t abHash[RTSHA1_HASH_SIZE];
 
175
    RTSha1Final(&Ctx, abHash);
 
176
 
 
177
    char *pszDigest;
 
178
    rc = RTStrAllocEx(&pszDigest, RTSHA1_DIGEST_LEN + 1);
 
179
    if (RT_SUCCESS(rc))
 
180
    {
 
181
        rc = RTSha1ToString(abHash, pszDigest, RTSHA1_DIGEST_LEN + 1);
 
182
        if (RT_SUCCESS(rc))
 
183
            *ppszDigest = pszDigest;
 
184
        else
 
185
            RTStrFree(pszDigest);
 
186
    }
 
187
 
 
188
    return rc;
 
189
}
 
190