source: branches/release_2_0/base/src/pextlib1.0/sha256cmd.c @ 81465

Last change on this file since 81465 was 81465, checked in by jmr@…, 9 years ago

merge r81464 from trunk:

add progress output to images_to_archives.tcl (#30347)

File size: 4.0 KB
Line 
1/*
2 * sha256cmd.c
3 * $Id: sha256cmd.c 81465 2011-07-31 09:42:32Z jmr@macports.org $
4 *
5 * Copyright (c) 2009, 2011 The MacPorts Project
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of The MacPorts Project nor the names of its contributors
17 *    may be used to endorse or promote products derived from this software
18 *    without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33#if HAVE_CONFIG_H
34#include <config.h>
35#endif
36
37#include <errno.h>
38#include <fcntl.h>
39#include <stdlib.h>
40#include <string.h>
41#include <unistd.h>
42
43#include <tcl.h>
44
45#include "sha256cmd.h"
46
47#if HAVE_COMMONCRYPTO_COMMONDIGEST_H
48
49#define COMMON_DIGEST_FOR_OPENSSL
50#include <CommonCrypto/CommonDigest.h>
51
52/* Tiger compatibility */
53#ifndef SHA256_DIGEST_LENGTH
54#define SHA256_DIGEST_LENGTH            CC_SHA256_DIGEST_LENGTH
55#define SHA256_CTX                      CC_SHA256_CTX
56#define SHA256_Init(c)                  CC_SHA256_Init(c)
57#define SHA256_Update(c,d,l)            CC_SHA256_Update(c,d,l)
58#define SHA256_Final(m, c)              CC_SHA256_Final(m,c)
59#endif
60
61#include "md_wrappers.h"
62CHECKSUMEnd(SHA256_, SHA256_CTX, SHA256_DIGEST_LENGTH)
63CHECKSUMFile(SHA256_, SHA256_CTX)
64
65#elif defined(HAVE_LIBMD) && defined(HAVE_SHA256_H) && !defined(__FreeBSD__) /*dumps core*/
66#include <sys/types.h>
67#include <sha256.h>
68#ifndef SHA256_DIGEST_LENGTH
69#define SHA256_DIGEST_LENGTH 32
70#endif
71#elif defined(HAVE_LIBCRYPTO) && defined(HAVE_OPENSSL_SHA_H) && defined(HAVE_SHA256_UPDATE)
72#include <openssl/sha.h>
73
74#include "md_wrappers.h"
75CHECKSUMEnd(SHA256_, SHA256_CTX, SHA256_DIGEST_LENGTH)
76CHECKSUMFile(SHA256_, SHA256_CTX)
77#else
78/*
79 * let's use our own version of sha256* libraries.
80 */
81#include <sys/types.h>
82#include "sha2.h"
83#include "sha2.c"
84
85#include "md_wrappers.h"
86CHECKSUMEnd(SHA256_, SHA256_CTX, SHA256_DIGEST_LENGTH)
87CHECKSUMFile(SHA256_, SHA256_CTX)
88#endif
89
90int SHA256Cmd(ClientData clientData UNUSED, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[])
91{
92        char *file, *action;
93        char buf[2*SHA256_DIGEST_LENGTH + 1];
94        const char usage_message[] = "Usage: sha256 file";
95        const char error_message[] = "Could not open file: ";
96        Tcl_Obj *tcl_result;
97
98        if (objc != 3) {
99                Tcl_WrongNumArgs(interp, 1, objv, "action ?file?");
100                return TCL_ERROR;
101        }
102
103        /*
104         * Only the 'file' action is currently supported
105         */
106        action = Tcl_GetString(objv[1]);
107        if (strcmp(action, "file") != 0) {
108                tcl_result = Tcl_NewStringObj(usage_message, sizeof(usage_message) - 1);
109                Tcl_SetObjResult(interp, tcl_result);
110                return TCL_ERROR;
111        }
112        file = Tcl_GetString(objv[2]);
113
114        if (!SHA256_File(file, buf)) {
115                tcl_result = Tcl_NewStringObj(error_message, sizeof(error_message) - 1);
116                Tcl_AppendObjToObj(tcl_result, Tcl_NewStringObj(file, -1));
117                Tcl_SetObjResult(interp, tcl_result);
118                return TCL_ERROR;
119        }
120        tcl_result = Tcl_NewStringObj(buf, sizeof(buf) - 1);
121        Tcl_SetObjResult(interp, tcl_result);
122        return TCL_OK;
123}
Note: See TracBrowser for help on using the repository browser.