sha1c.tcl
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007 package require critcl;
00008 package provide sha1c 2.0.3
00009
00010 critcl::cheaders sha1.h;
00011 critcl::csources sha1.c;
00012
00013 if {$tcl_platform(byteOrder) eq "littleEndian"} {
00014 byteOrder = 1234
00015 } else {
00016 byteOrder = 4321
00017 }
00018 critcl::cheaders -DTCL_BYTE_ORDER=$byteOrder
00019
00020 namespace ::sha1 {
00021
00022 critcl::ccode {
00023
00024
00025
00026
00027 static
00028 Tcl_ObjType sha1_type;
00029
00030 static void
00031 sha1_free_rep(Tcl_Obj* obj)
00032 {
00033 SHA1_CTX* mp = (SHA1_CTX*) obj->internalRep.otherValuePtr;
00034 free(mp);
00035 }
00036
00037 static void
00038 sha1_dup_rep(Tcl_Obj* obj, Tcl_Obj* dup)
00039 {
00040 SHA1_CTX* mp = (SHA1_CTX*) obj->internalRep.otherValuePtr;
00041 dup->internalRep.otherValuePtr = malloc(sizeof *mp);
00042 memcpy(dup->internalRep.otherValuePtr, mp, sizeof *mp);
00043 dup->typePtr = &sha1_type;
00044 }
00045
00046 static void
00047 sha1_string_rep(Tcl_Obj* obj)
00048 {
00049 unsigned char buf[20];
00050 Tcl_Obj* temp;
00051 char* str;
00052 SHA1_CTX dup = *(SHA1_CTX*) obj->internalRep.otherValuePtr;
00053
00054 SHA1Final(buf, &dup);
00055
00056
00057 temp = Tcl_NewByteArrayObj(buf, sizeof buf);
00058 Tcl_IncrRefCount(temp);
00059
00060 str = Tcl_GetStringFromObj(temp, &obj->length);
00061 obj->bytes = Tcl_Alloc(obj->length + 1);
00062 memcpy(obj->bytes, str, obj->length + 1);
00063
00064 Tcl_DecrRefCount(temp);
00065 }
00066
00067 static int
00068 sha1_from_any(Tcl_Interp* ip, Tcl_Obj* obj)
00069 {
00070 assert(0);
00071 return TCL_ERROR;
00072 }
00073
00074 static
00075 Tcl_ObjType sha1_type = {
00076 "sha1c", sha1_free_rep, sha1_dup_rep, sha1_string_rep,
00077 sha1_from_any
00078 };
00079 }
00080
00081 critcl::ccommand sha1c {dummy ip objc objv} {
00082 SHA1_CTX* mp;
00083 unsigned char* data;
00084 int size;
00085 Tcl_Obj* obj;
00086
00087 if (objc < 2 || objc > 3) {
00088 Tcl_WrongNumArgs(ip, 1, objv, "data ?context?");
00089 return TCL_ERROR;
00090 }
00091
00092 if (objc == 3) {
00093 if (objv[2]->typePtr != &sha1_type
00094 && sha1_from_any(ip, objv[2]) != TCL_OK)
00095 return TCL_ERROR;
00096 obj = objv[2];
00097 if (Tcl_IsShared(obj))
00098 obj = Tcl_DuplicateObj(obj);
00099 } else {
00100 obj = Tcl_NewObj();
00101 mp = (SHA1_CTX*) malloc(sizeof *mp);
00102 SHA1Init(mp);
00103
00104 if (obj->typePtr != NULL && obj->typePtr->freeIntRepProc != NULL)
00105 obj->typePtr->freeIntRepProc(obj);
00106
00107 obj->internalRep.otherValuePtr = mp;
00108 obj->typePtr = &sha1_type;
00109 }
00110
00111 Tcl_SetObjResult(ip, obj);
00112 Tcl_IncrRefCount(obj);
00113
00114 Tcl_InvalidateStringRep(obj);
00115 mp = (SHA1_CTX*) obj->internalRep.otherValuePtr;
00116
00117 data = Tcl_GetByteArrayFromObj(objv[1], &size);
00118 SHA1Update(mp, data, size);
00119
00120 return TCL_OK;
00121 }
00122 }
00123