md4c.tcl

Go to the documentation of this file.
00001 /*  md4c.tcl - Copyright (C) 2003 Pat Thoyts <patthoyts@users.sourceforge.net>*/
00002 /* */
00003 /*  This provides a C implementation of MD4 using the sample code from RFC1320*/
00004 /*  and wrapping this up in a Tcl package.*/
00005 /* */
00006 /*  The tcl interface code is based upon the md5c code from critcl by JCW.*/
00007 /* */
00008 /*  INSTALLATION*/
00009 /*  ------------*/
00010 /*  This package uses critcl (http://wiki.tcl.tk/critcl). To build do:*/
00011 /*   critcl -libdir <your-tcl-lib-dir> -pkg md4c md4c*/
00012 /* */
00013 /*  $Id: md4c.tcl,v 1.4 2004/01/15 06:36:13 andreas_kupries Exp $*/
00014 
00015 package require critcl
00016 package provide md4c 1.0.0
00017 
00018 critcl::cheaders md4.h
00019 critcl::csources md4.c
00020 
00021 namespace ::md4 {
00022 
00023     critcl::ccode {
00024         /* include "md4.h"*/
00025 
00026         /*
00027          * define a Tcl object type for the MD4 state 
00028          */
00029         static Tcl_ObjType md4_type;
00030     
00031         static void md4_free_rep(Tcl_Obj *obj)
00032         {
00033             MD4_CTX *ctx = (MD4_CTX *)obj->internalRep.otherValuePtr;
00034             Tcl_Free((char *)ctx);
00035         }
00036 
00037         static void md4_dup_rep(Tcl_Obj *obj, Tcl_Obj *dup)
00038         {
00039             MD4_CTX *ctx = (MD4_CTX *)obj->internalRep.otherValuePtr;
00040             dup->internalRep.otherValuePtr = (MD4_CTX *)Tcl_Alloc(sizeof(MD4_CTX));
00041             memcpy(dup->internalRep.otherValuePtr, ctx, sizeof(MD4_CTX));
00042             dup->typePtr = &md4_type;
00043         }
00044 
00045         static void md4_string_rep(Tcl_Obj* obj)
00046         {
00047             unsigned char buf[16];
00048             Tcl_Obj* temp;
00049             char* str;
00050             MD4_CTX *dup = (MD4_CTX *)obj->internalRep.otherValuePtr;
00051             
00052             MD4Final(buf, dup);
00053             
00054             /* convert via a byte array to properly handle null bytes */
00055             temp = Tcl_NewByteArrayObj(buf, sizeof buf);
00056             Tcl_IncrRefCount(temp);
00057             
00058             str = Tcl_GetStringFromObj(temp, &obj->length);
00059             obj->bytes = Tcl_Alloc(obj->length + 1);
00060             memcpy(obj->bytes, str, obj->length + 1);
00061             
00062             Tcl_DecrRefCount(temp);
00063         }
00064     
00065         static int md4_from_any(Tcl_Interp* interp, Tcl_Obj* obj)
00066         {
00067             /* assert(0); */
00068             return TCL_ERROR;
00069         }
00070         
00071         static Tcl_ObjType md4_type = {
00072             "md4c", md4_free_rep, md4_dup_rep, md4_string_rep, md4_from_any
00073         };
00074 
00075     }
00076 
00077     critcl::ccommand md4c {dummy interp objc objv} {
00078         MD4_CTX *ctx;
00079         unsigned char* data;
00080         int size;
00081         Tcl_Obj* obj;
00082         
00083         /* Tcl_RegisterObjType(&md4_type); */
00084         
00085         if (objc < 2 || objc > 3) {
00086             Tcl_WrongNumArgs(interp, 1, objv, "data ?context?");
00087             return TCL_ERROR;
00088         }
00089         
00090         if (objc == 3) {
00091             if (objv[2]->typePtr != &md4_type 
00092                 && md4_from_any(interp, objv[2]) != TCL_OK)
00093                 return TCL_ERROR;
00094             obj = objv[2];
00095             if (Tcl_IsShared(obj))
00096                 obj = Tcl_DuplicateObj(obj);
00097         } else {
00098             obj = Tcl_NewObj();
00099             ctx = (MD4_CTX *)Tcl_Alloc(sizeof(MD4_CTX));
00100             MD4Init(ctx);
00101         
00102             if (obj->typePtr != NULL && obj->typePtr->freeIntRepProc != NULL)
00103                 obj->typePtr->freeIntRepProc(obj);
00104         
00105             obj->internalRep.otherValuePtr = ctx;
00106             obj->typePtr = &md4_type;
00107         }
00108     
00109         Tcl_SetObjResult(interp, obj);
00110         Tcl_IncrRefCount(obj); //!! huh?
00111         
00112         Tcl_InvalidateStringRep(obj);
00113         ctx = (MD4_CTX *)obj->internalRep.otherValuePtr;
00114     
00115         data = Tcl_GetByteArrayFromObj(objv[1], &size);
00116         MD4Update(ctx, data, size);
00117     
00118         return TCL_OK;
00119     }
00120 }
00121 
00122 /*  Local variables:*/
00123 /*    mode: tcl*/
00124 /*    indent-tabs-mode: nil*/
00125 /*  End:*/
00126 

Generated on 21 Sep 2010 for Gui by  doxygen 1.6.1