From 4dc0badfc04e1ffac6462b0cf5ee2d285d87f9bd Mon Sep 17 00:00:00 2001 From: "jan.nijtmans" Date: Wed, 18 Dec 2024 21:28:42 +0000 Subject: [PATCH] Combine ckalloc's for context storage to a single ckalloc. Add "const", some more code cleanup --- ChangeLog | 2 +- Makefile.in | 4 +-- generic/sample.c | 20 ++++++------- generic/tclsample.c | 72 ++++++++++++++++++++++----------------------- tea/packages.txt | 6 ++-- 5 files changed, 52 insertions(+), 52 deletions(-) diff --git a/ChangeLog b/ChangeLog index 23f8673..a93c92a 100644 --- a/ChangeLog +++ b/ChangeLog @@ -270,7 +270,7 @@ * Makefile.in: Subst TCLSH_PROG from PATH as SYSTEM_TCLSH and subst BUILD_TCLSH and BUILD_WISH. Use SYSTEM_TCLSH for the - pkgIndex.tcl generation target. + pkgIndex.tcl generation target. * configure: Regen. * configure.in: Invoke new TEA_BUILD_TCLSH and TEA_BUILD_WISH macros defined in tcl.m4. diff --git a/Makefile.in b/Makefile.in index 9e65ece..a9d9817 100644 --- a/Makefile.in +++ b/Makefile.in @@ -301,7 +301,7 @@ $(PKG_STUB_LIB_FILE): $(PKG_STUB_OBJECTS) # you will have to modify the paths to reflect this: # # sample.$(OBJEXT): $(srcdir)/generic/sample.c -# $(COMPILE) -c `@CYGPATH@ $(srcdir)/generic/sample.c` -o $@ +# $(COMPILE) -c `@CYGPATH@ $(srcdir)/generic/sample.c` -o $@ # # Setting the VPATH variable to a list of paths will cause the makefile # to look into these paths when resolving .c to .obj dependencies. @@ -418,7 +418,7 @@ install-lib-binaries: binaries lib=`basename $$p|sed -e 's/.[^.]*$$//'`.lib; \ if test -f $$lib; then \ echo " $(INSTALL_DATA) $$lib $(DESTDIR)$(pkglibdir)/$$lib"; \ - $(INSTALL_DATA) $$lib "$(DESTDIR)$(pkglibdir)/$$lib"; \ + $(INSTALL_DATA) $$lib "$(DESTDIR)$(pkglibdir)/$$lib"; \ fi; \ fi; \ fi; \ diff --git a/generic/sample.c b/generic/sample.c index f74f54b..1f787a4 100644 --- a/generic/sample.c +++ b/generic/sample.c @@ -235,11 +235,11 @@ SHA1Update( } context->count[1] += (len >> 29); if ((j + len) > 63) { - memcpy(&context->buffer[j], data, (i = 64-j)); - SHA1Transform(context->state, context->buffer); - for ( ; i + 63 < len; i += 64) { + memcpy(&context->buffer[j], data, (i = 64-j)); + SHA1Transform(context->state, context->buffer); + for ( ; i + 63 < len; i += 64) { #ifdef CANWRITEDATA - SHA1Transform(context->state, &data[i]); + SHA1Transform(context->state, &data[i]); #else /* @@ -247,10 +247,10 @@ SHA1Update( */ memcpy(&context->buffer[0], &data[i], 64); - SHA1Transform(context->state, context->buffer); + SHA1Transform(context->state, context->buffer); #endif - } - j = 0; + } + j = 0; } else { i = 0; } @@ -288,12 +288,12 @@ void SHA1Final( * This statement is independent of the endianness */ - finalcount[i] = (unsigned char)((context->count[(i >= 4 ? 0 : 1)] + finalcount[i] = (unsigned char)((context->count[(i >= 4 ? 0 : 1)] >> ((3-(i & 3)) * 8) ) & 255); } SHA1Update(context, (unsigned char *)"\200", 1); while ((context->count[0] & 504) != 448) { - SHA1Update(context, (unsigned char *)"\0", 1); + SHA1Update(context, (unsigned char *)"\0", 1); } /* @@ -302,7 +302,7 @@ void SHA1Final( SHA1Update(context, finalcount, 8); for (i = 0; i < 20; i++) { - digest[i] = (unsigned char) + digest[i] = (unsigned char) ((context->state[i>>2] >> ((3-(i & 3)) * 8) ) & 255); } diff --git a/generic/tclsample.c b/generic/tclsample.c index 45279a7..a655e4d 100644 --- a/generic/tclsample.c +++ b/generic/tclsample.c @@ -26,11 +26,15 @@ #define TCL_READ_CHUNK_SIZE 4096 static const unsigned char itoa64f[] = - "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_,"; + "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_,"; static int numcontexts = 0; -static SHA1_CTX *sha1Contexts = NULL; -static Tcl_Size *ctxtotalRead = NULL; +typedef struct { + SHA1_CTX ctx; + Tcl_Size totalRead; +} sha1CmdContext; +static sha1CmdContext *sha1CmdContexts = NULL; + static int Sha1_Cmd(void *clientData, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[]); @@ -72,7 +76,7 @@ Sha1_Cmd( Tcl_Channel copychan = NULL; int mode; int contextnum = 0; -#define sha1Context (sha1Contexts[contextnum]) +#define sha1Context (sha1CmdContexts[contextnum].ctx) char *bufPtr; Tcl_WideInt maxbytes = 0; int doinit = 1; @@ -89,7 +93,7 @@ Sha1_Cmd( char buf[129]; unsigned char digest[DIGESTSIZE]; - static const char *options[] = { + static const char *const options[] = { "-chan", "-copychan", "-final", "-init", "-log2base", "-maxbytes", "-string", "-update", NULL }; @@ -114,7 +118,7 @@ Sha1_Cmd( switch ((enum ShaOpts) index) { case SHAOPT_INIT: for (contextnum = 1; contextnum < numcontexts; contextnum++) { - if (ctxtotalRead[contextnum] == -1) { + if (sha1CmdContexts[contextnum].totalRead == -1) { break; } } @@ -124,15 +128,13 @@ Sha1_Cmd( */ numcontexts++; - sha1Contexts = (SHA1_CTX *) ckrealloc((void *) sha1Contexts, - numcontexts * sizeof(SHA1_CTX)); - ctxtotalRead = (Tcl_Size *)ckrealloc(ctxtotalRead, - numcontexts * sizeof(Tcl_Size)); + sha1CmdContexts = (sha1CmdContext *)ckrealloc((void *)sha1CmdContexts, + numcontexts * sizeof(sha1CmdContext)); } - ctxtotalRead[contextnum] = 0; + sha1CmdContexts[contextnum].totalRead = 0; SHA1Init(&sha1Context); snprintf(buf, sizeof(buf), "sha1%d", contextnum); - Tcl_AppendResult(interp, buf, NULL); + Tcl_AppendResult(interp, buf, (char *)NULL); return TCL_OK; case SHAOPT_CHAN: chan = Tcl_GetChannel(interp, Tcl_GetString(objv[a]), &mode); @@ -141,7 +143,7 @@ Sha1_Cmd( } if ((mode & TCL_READABLE) == 0) { Tcl_AppendResult(interp, "chan \"", Tcl_GetString(objv[a]), - "\" wasn't opened for reading", NULL); + "\" wasn't opened for reading", (char *)NULL); return TCL_ERROR; } continue; @@ -152,7 +154,7 @@ Sha1_Cmd( } if ((mode & TCL_WRITABLE) == 0) { Tcl_AppendResult(interp, "copychan \"", Tcl_GetString(objv[a]), - "\" wasn't opened for writing", NULL); + "\" wasn't opened for writing", (char *)NULL); return TCL_ERROR; } continue; @@ -166,7 +168,7 @@ Sha1_Cmd( } else if ((log2base < 1) || (log2base > 6)) { Tcl_AppendResult(interp, "parameter to -log2base \"", Tcl_GetString(objv[a]), - "\" must be integer in range 1...6", NULL); + "\" must be integer in range 1...6", (char *)NULL); return TCL_ERROR; } continue; @@ -189,9 +191,9 @@ Sha1_Cmd( if (descriptorObj != NULL) { if ((sscanf(Tcl_GetString(descriptorObj), "sha1%d", &contextnum) != 1) || (contextnum >= numcontexts) || - (ctxtotalRead[contextnum] == -1)) { + (sha1CmdContexts[contextnum].totalRead == -1)) { Tcl_AppendResult(interp, "invalid sha1 descriptor \"", - Tcl_GetString(descriptorObj), "\"", NULL); + Tcl_GetString(descriptorObj), "\"", (char *)NULL); return TCL_ERROR; } } @@ -220,13 +222,13 @@ Sha1_Cmd( ckfree(bufPtr); Tcl_AppendResult(interp, Tcl_GetString(objv[0]), ": ", Tcl_GetChannelName(chan), Tcl_PosixError(interp), - NULL); + (char *)NULL); return TCL_ERROR; } totalRead += n; - SHA1Update(&sha1Context, (unsigned char *) bufPtr, n); + SHA1Update(&sha1Context, (unsigned char *)bufPtr, n); if (copychan != NULL) { n = Tcl_Write(copychan, bufPtr, n); @@ -234,7 +236,7 @@ Sha1_Cmd( ckfree(bufPtr); Tcl_AppendResult(interp, Tcl_GetString(objv[0]), ": ", Tcl_GetChannelName(copychan), - Tcl_PosixError(interp), NULL); + Tcl_PosixError(interp), (char *)NULL); return TCL_ERROR; } } @@ -250,13 +252,13 @@ Sha1_Cmd( } if (!dofinal) { - ctxtotalRead[contextnum] += totalRead; + sha1CmdContexts[contextnum].totalRead += totalRead; Tcl_SetObjResult(interp, Tcl_NewWideIntObj(totalRead)); return TCL_OK; } if (stringObj == NULL) { - totalRead += ctxtotalRead[contextnum]; + totalRead += sha1CmdContexts[contextnum].totalRead; Tcl_SetObjResult(interp, Tcl_NewWideIntObj(totalRead)); } @@ -282,22 +284,22 @@ Sha1_Cmd( } mask = (2 << (n-1)) - 1; while (1) { - bits <<= n; - if (offset <= n) { - if (i == DIGESTSIZE) { + bits <<= n; + if (offset <= n) { + if (i == DIGESTSIZE) { break; } - bits += (digest[i++] << (n - offset)); - offset += 8; - } - offset -= n; - buf[j++] = itoa64f[(bits>>8)&mask]; + bits += (digest[i++] << (n - offset)); + offset += 8; + } + offset -= n; + buf[j++] = itoa64f[(bits>>8)&mask]; } buf[j++] = itoa64f[(bits>>8)&mask]; buf[j++] = '\0'; - Tcl_AppendResult(interp, buf, NULL); + Tcl_AppendResult(interp, buf, (char *)NULL); if (contextnum > 0) { - ctxtotalRead[contextnum] = -1; + sha1CmdContexts[contextnum].totalRead = -1; } return TCL_OK; @@ -322,7 +324,7 @@ Sha1_Cmd( Tcl_GetString(objv[0]), " ?-log2base log2base? -final descriptor\n", " The default log2base is 4 (hex)", - NULL); + (char *)NULL); return TCL_ERROR; } @@ -433,9 +435,7 @@ Sample_Init( NULL, NULL); numcontexts = 1; - sha1Contexts = (SHA1_CTX *) ckalloc(sizeof(SHA1_CTX)); - ctxtotalRead = (Tcl_Size *) ckalloc(sizeof(Tcl_Size)); - ctxtotalRead[0] = 0; + sha1CmdContexts = (sha1CmdContext *)ckalloc(sizeof(sha1CmdContext)); return TCL_OK; } diff --git a/tea/packages.txt b/tea/packages.txt index f2c9635..7a2b65d 100644 --- a/tea/packages.txt +++ b/tea/packages.txt @@ -114,13 +114,13 @@ int Sample_Init(Tcl_Interp *interp) { if (Tcl_InitStubs(interp, "8.6-", 0) == NULL) { - return TCL_ERROR; + return TCL_ERROR; } if (Tcl_PkgRequire(interp, "Tcl", "8.6-", 0) == NULL) { - return TCL_ERROR; + return TCL_ERROR; } if (Tcl_PkgProvide(interp, "sample", VERSION) != TCL_OK) { - return TCL_ERROR; + return TCL_ERROR; } Tcl_CreateObjCommand(interp, "sha1", Sha1, NULL, NULL);