source: branches/gsoc09-logging/base/src/pextlib1.0/sha1cmd.c @ 59527

Last change on this file since 59527 was 59527, checked in by blb@…, 8 years ago

Merge from trunk (may be a few missed bits to be picked up later)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 3.1 KB
Line 
1/*
2 * sha1cmd.c
3 * $Id: sha1cmd.c 59527 2009-10-16 05:29:39Z blb@macports.org $
4 * Copied from md5cmd.c 20040903 EH
5 *
6 * Copyright (c) 2002 - 2003 Apple Computer, Inc.
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 *    notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 *    notice, this list of conditions and the following disclaimer in the
16 *    documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of Apple Computer, Inc. nor the names of its contributors
18 *    may be used to endorse or promote products derived from this software
19 *    without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
25 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
32 */
33
34#if HAVE_CONFIG_H
35#include <config.h>
36#endif
37
38#include <errno.h>
39#include <fcntl.h>
40#include <stdlib.h>
41#include <string.h>
42#include <unistd.h>
43
44#include <tcl.h>
45
46#include "sha1cmd.h"
47
48#if HAVE_COMMONCRYPTO_COMMONDIGEST_H
49
50#define COMMON_DIGEST_FOR_OPENSSL
51#include <CommonCrypto/CommonDigest.h>
52
53#include "md_wrappers.h"
54CHECKSUMEnd(SHA1_, SHA_CTX, SHA_DIGEST_LENGTH)
55CHECKSUMFile(SHA1_, SHA_CTX)
56
57#elif defined(HAVE_LIBMD)
58#include <sys/types.h>
59#include <sha.h>
60#else
61#error CommonCrypto or libmd required
62#endif
63
64int SHA1Cmd(ClientData clientData UNUSED, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[])
65{
66        char *file, *action;
67        char buf[2*SHA_DIGEST_LENGTH + 1];
68        const char usage_message[] = "Usage: sha1 file ?file?";
69        const char error_message[] = "Could not open file: ";
70        Tcl_Obj *tcl_result;
71
72        if (objc != 3) {
73                Tcl_WrongNumArgs(interp, 1, objv, "action ?file?");
74                return TCL_ERROR;
75        }
76
77        /*
78         * Only the 'file' action is currently supported
79         */
80        action = Tcl_GetString(objv[1]);
81        if (strcmp(action, "file") != 0) {
82                tcl_result = Tcl_NewStringObj(usage_message, sizeof(usage_message) - 1);
83                Tcl_SetObjResult(interp, tcl_result);
84                return TCL_ERROR;
85        }
86        file = Tcl_GetString(objv[2]);
87
88        if (!SHA1_File(file, buf)) {
89                tcl_result = Tcl_NewStringObj(error_message, sizeof(error_message) - 1);
90                Tcl_AppendObjToObj(tcl_result, Tcl_NewStringObj(file, -1));
91                Tcl_SetObjResult(interp, tcl_result);
92                return TCL_ERROR;
93        }
94        tcl_result = Tcl_NewStringObj(buf, sizeof(buf) - 1);
95        Tcl_SetObjResult(interp, tcl_result);
96        return TCL_OK;
97}
Note: See TracBrowser for help on using the repository browser.