diff options
Diffstat (limited to 'generic/tclBinary.c')
-rw-r--r-- | generic/tclBinary.c | 186 |
1 files changed, 74 insertions, 112 deletions
diff --git a/generic/tclBinary.c b/generic/tclBinary.c index c692b40..94ea600 100644 --- a/generic/tclBinary.c +++ b/generic/tclBinary.c @@ -22,8 +22,8 @@ * special conditions in the parsing of a format specifier. */ -#define BINARY_ALL -1 /* Use all elements in the argument. */ -#define BINARY_NOCOUNT -2 /* No count was specified in format. */ +#define BINARY_ALL ((size_t)-1) /* Use all elements in the argument. */ +#define BINARY_NOCOUNT ((size_t)-2) /* No count was specified in format. */ /* * The following flags may be ORed together and returned by GetFormatSpec @@ -57,14 +57,11 @@ static void DupByteArrayInternalRep(Tcl_Obj *srcPtr, Tcl_Obj *copyPtr); -static void DupProperByteArrayInternalRep(Tcl_Obj *srcPtr, - Tcl_Obj *copyPtr); static int FormatNumber(Tcl_Interp *interp, int type, Tcl_Obj *src, unsigned char **cursorPtr); static void FreeByteArrayInternalRep(Tcl_Obj *objPtr); -static void FreeProperByteArrayInternalRep(Tcl_Obj *objPtr); static int GetFormatSpec(const char **formatPtr, char *cmdPtr, - int *countPtr, int *flagsPtr); + size_t *countPtr, int *flagsPtr); static Tcl_Obj * ScanNumber(unsigned char *buffer, int type, int flags, Tcl_HashTable **numberCachePtr); static int SetByteArrayFromAny(Tcl_Interp *interp, @@ -73,7 +70,7 @@ static void UpdateStringOfByteArray(Tcl_Obj *listPtr); static void DeleteScanNumberCache(Tcl_HashTable *numberCachePtr); static int NeedReversing(int format); static void CopyNumber(const void *from, void *to, - unsigned length, int type); + size_t length, int type); /* Binary ensemble commands */ static int BinaryFormatCmd(ClientData clientData, Tcl_Interp *interp, @@ -222,7 +219,7 @@ static const EnsembleImplMap decodeMap[] = { * Bytearrays should simply be usable as bytearrays without a kabuki * dance of testing. * - * The Tcl_ObjType "properByteArrayType" is (nearly) a correct + * The "Pure" ByteArray type is (nearly) a correct * implementation of bytearrays. Any Tcl value with the type * properByteArrayType can have its bytearray value fetched and * used with confidence that acting on that value is equivalent to @@ -248,10 +245,10 @@ static const EnsembleImplMap decodeMap[] = { * over which bytearray values can be useful in the meanwhile. */ -static const Tcl_ObjType properByteArrayType = { +const Tcl_ObjType tclPureByteArrayType = { "bytearray", - FreeProperByteArrayInternalRep, - DupProperByteArrayInternalRep, + FreeByteArrayInternalRep, + DupByteArrayInternalRep, UpdateStringOfByteArray, NULL }; @@ -285,14 +282,7 @@ typedef struct { ((TclOffset(ByteArray, bytes) + (len))) #define GET_BYTEARRAY(irPtr) ((ByteArray *) (irPtr)->twoPtrValue.ptr1) #define SET_BYTEARRAY(irPtr, baPtr) \ - (irPtr)->twoPtrValue.ptr1 = (void *) (baPtr) - -int -TclIsPureByteArray( - Tcl_Obj * objPtr) -{ - return (NULL != Tcl_FetchIntRep(objPtr, &properByteArrayType)); -} + (irPtr)->twoPtrValue.ptr1 = (baPtr) /* *---------------------------------------------------------------------- @@ -420,7 +410,7 @@ Tcl_SetByteArrayObj( } SET_BYTEARRAY(&ir, byteArrayPtr); - Tcl_StoreIntRep(objPtr, &properByteArrayType, &ir); + Tcl_StoreIntRep(objPtr, &tclPureByteArrayType, &ir); } /* @@ -448,15 +438,15 @@ Tcl_GetByteArrayFromObj( * array of bytes in the ByteArray object. */ { ByteArray *baPtr; - const Tcl_ObjIntRep *irPtr = Tcl_FetchIntRep(objPtr, &properByteArrayType); + const Tcl_ObjIntRep *irPtr = TclFetchIntRep(objPtr, &tclPureByteArrayType); if (irPtr == NULL) { - irPtr = Tcl_FetchIntRep(objPtr, &tclByteArrayType); + irPtr = TclFetchIntRep(objPtr, &tclByteArrayType); if (irPtr == NULL) { SetByteArrayFromAny(NULL, objPtr); - irPtr = Tcl_FetchIntRep(objPtr, &properByteArrayType); + irPtr = TclFetchIntRep(objPtr, &tclPureByteArrayType); if (irPtr == NULL) { - irPtr = Tcl_FetchIntRep(objPtr, &tclByteArrayType); + irPtr = TclFetchIntRep(objPtr, &tclByteArrayType); } } } @@ -502,14 +492,14 @@ Tcl_SetByteArrayLength( Tcl_Panic("%s called with shared object", "Tcl_SetByteArrayLength"); } - irPtr = Tcl_FetchIntRep(objPtr, &properByteArrayType); + irPtr = TclFetchIntRep(objPtr, &tclPureByteArrayType); if (irPtr == NULL) { - irPtr = Tcl_FetchIntRep(objPtr, &tclByteArrayType); + irPtr = TclFetchIntRep(objPtr, &tclByteArrayType); if (irPtr == NULL) { SetByteArrayFromAny(NULL, objPtr); - irPtr = Tcl_FetchIntRep(objPtr, &properByteArrayType); + irPtr = TclFetchIntRep(objPtr, &tclPureByteArrayType); if (irPtr == NULL) { - irPtr = Tcl_FetchIntRep(objPtr, &tclByteArrayType); + irPtr = TclFetchIntRep(objPtr, &tclByteArrayType); } } } @@ -553,15 +543,12 @@ SetByteArrayFromAny( ByteArray *byteArrayPtr; Tcl_ObjIntRep ir; - if (Tcl_FetchIntRep(objPtr, &properByteArrayType)) { - return TCL_OK; - } - if (Tcl_FetchIntRep(objPtr, &tclByteArrayType)) { + if ((objPtr->typePtr == &tclPureByteArrayType) + || (objPtr->typePtr == &tclByteArrayType)) { return TCL_OK; } - src = TclGetString(objPtr); - length = objPtr->length; + src = TclGetStringFromObj(objPtr, &length); srcEnd = src + length; byteArrayPtr = Tcl_Alloc(BYTEARRAY_SIZE(length)); @@ -577,7 +564,7 @@ SetByteArrayFromAny( SET_BYTEARRAY(&ir, byteArrayPtr); Tcl_StoreIntRep(objPtr, - improper ? &tclByteArrayType : &properByteArrayType, &ir); + improper ? &tclByteArrayType : &tclPureByteArrayType, &ir); return TCL_OK; } @@ -602,14 +589,7 @@ static void FreeByteArrayInternalRep( Tcl_Obj *objPtr) /* Object with internal rep to free. */ { - Tcl_Free(GET_BYTEARRAY(Tcl_FetchIntRep(objPtr, &tclByteArrayType))); -} - -static void -FreeProperByteArrayInternalRep( - Tcl_Obj *objPtr) /* Object with internal rep to free. */ -{ - Tcl_Free(GET_BYTEARRAY(Tcl_FetchIntRep(objPtr, &properByteArrayType))); + Tcl_Free(GET_BYTEARRAY(&(objPtr->internalRep))); } /* @@ -638,28 +618,7 @@ DupByteArrayInternalRep( ByteArray *srcArrayPtr, *copyArrayPtr; Tcl_ObjIntRep ir; - srcArrayPtr = GET_BYTEARRAY(Tcl_FetchIntRep(srcPtr, &tclByteArrayType)); - length = srcArrayPtr->used; - - copyArrayPtr = Tcl_Alloc(BYTEARRAY_SIZE(length)); - copyArrayPtr->used = length; - copyArrayPtr->allocated = length; - memcpy(copyArrayPtr->bytes, srcArrayPtr->bytes, length); - - SET_BYTEARRAY(&ir, copyArrayPtr); - Tcl_StoreIntRep(copyPtr, &tclByteArrayType, &ir); -} - -static void -DupProperByteArrayInternalRep( - Tcl_Obj *srcPtr, /* Object with internal rep to copy. */ - Tcl_Obj *copyPtr) /* Object with internal rep to set. */ -{ - size_t length; - ByteArray *srcArrayPtr, *copyArrayPtr; - Tcl_ObjIntRep ir; - - srcArrayPtr = GET_BYTEARRAY(Tcl_FetchIntRep(srcPtr, &properByteArrayType)); + srcArrayPtr = GET_BYTEARRAY(&(srcPtr->internalRep)); length = srcArrayPtr->used; copyArrayPtr = Tcl_Alloc(BYTEARRAY_SIZE(length)); @@ -668,7 +627,7 @@ DupProperByteArrayInternalRep( memcpy(copyArrayPtr->bytes, srcArrayPtr->bytes, length); SET_BYTEARRAY(&ir, copyArrayPtr); - Tcl_StoreIntRep(copyPtr, &properByteArrayType, &ir); + Tcl_StoreIntRep(copyPtr, srcPtr->typePtr, &ir); } /* @@ -693,7 +652,7 @@ UpdateStringOfByteArray( Tcl_Obj *objPtr) /* ByteArray object whose string rep to * update. */ { - const Tcl_ObjIntRep *irPtr = Tcl_FetchIntRep(objPtr, &properByteArrayType); + const Tcl_ObjIntRep *irPtr = TclFetchIntRep(objPtr, &tclPureByteArrayType); ByteArray *byteArrayPtr = GET_BYTEARRAY(irPtr); unsigned char *src = byteArrayPtr->bytes; size_t i, length = byteArrayPtr->used; @@ -763,14 +722,14 @@ TclAppendBytesToByteArray( return; } - irPtr = Tcl_FetchIntRep(objPtr, &properByteArrayType); + irPtr = TclFetchIntRep(objPtr, &tclPureByteArrayType); if (irPtr == NULL) { - irPtr = Tcl_FetchIntRep(objPtr, &tclByteArrayType); + irPtr = TclFetchIntRep(objPtr, &tclByteArrayType); if (irPtr == NULL) { SetByteArrayFromAny(NULL, objPtr); - irPtr = Tcl_FetchIntRep(objPtr, &properByteArrayType); + irPtr = TclFetchIntRep(objPtr, &tclPureByteArrayType); if (irPtr == NULL) { - irPtr = Tcl_FetchIntRep(objPtr, &tclByteArrayType); + irPtr = TclFetchIntRep(objPtr, &tclByteArrayType); } } } @@ -876,7 +835,7 @@ BinaryFormatCmd( int value = 0; /* Current integer value to be packed. * Initialized to avoid compiler warning. */ char cmd; /* Current format character. */ - int count; /* Count associated with current format + size_t count; /* Count associated with current format * character. */ int flags; /* Format field flags */ const char *format; /* Pointer to current position in format @@ -888,7 +847,8 @@ BinaryFormatCmd( * cursor has visited.*/ const char *errorString; const char *errorValue, *str; - int offset, size, length; + int offset, size; + size_t length; if (objc < 2) { Tcl_WrongNumArgs(interp, 1, objv, "formatString ?arg ...?"); @@ -927,7 +887,7 @@ BinaryFormatCmd( goto badIndex; } if (count == BINARY_ALL) { - TclGetByteArrayFromObj(objv[arg], &count); + (void)TclGetByteArrayFromObj(objv[arg], &count); } else if (count == BINARY_NOCOUNT) { count = 1; } @@ -999,7 +959,7 @@ BinaryFormatCmd( if (count == BINARY_ALL) { count = listc; - } else if (count > listc) { + } else if (count > (size_t)listc) { Tcl_SetObjResult(interp, Tcl_NewStringObj( "number of elements in list does not match count", -1)); @@ -1023,16 +983,16 @@ BinaryFormatCmd( if (count == BINARY_NOCOUNT) { count = 1; } - if ((count > offset) || (count == BINARY_ALL)) { + if ((count > (size_t)offset) || (count == BINARY_ALL)) { count = offset; } - if (offset > length) { + if (offset > (int)length) { length = offset; } offset -= count; break; case '@': - if (offset > length) { + if (offset > (int)length) { length = offset; } if (count == BINARY_ALL) { @@ -1048,7 +1008,7 @@ BinaryFormatCmd( goto badField; } } - if (offset > length) { + if (offset > (int)length) { length = offset; } if (length == 0) { @@ -1062,7 +1022,7 @@ BinaryFormatCmd( resultPtr = Tcl_NewObj(); buffer = Tcl_SetByteArrayLength(resultPtr, length); - memset(buffer, 0, (size_t) length); + memset(buffer, 0, length); /* * Pack the data into the result object. Note that we can skip the @@ -1099,10 +1059,10 @@ BinaryFormatCmd( count = 1; } if (length >= count) { - memcpy(cursor, bytes, (size_t) count); + memcpy(cursor, bytes, count); } else { - memcpy(cursor, bytes, (size_t) length); - memset(cursor + length, pad, (size_t) (count - length)); + memcpy(cursor, bytes, length); + memset(cursor + length, pad, count - length); } cursor += count; break; @@ -1111,7 +1071,7 @@ BinaryFormatCmd( case 'B': { unsigned char *last; - str = Tcl_GetStringFromObj(objv[arg], &length); + str = TclGetStringFromObj(objv[arg], &length); arg++; if (count == BINARY_ALL) { count = length; @@ -1125,7 +1085,7 @@ BinaryFormatCmd( value = 0; errorString = "binary"; if (cmd == 'B') { - for (offset = 0; offset < count; offset++) { + for (offset = 0; (size_t)offset < count; offset++) { value <<= 1; if (str[offset] == '1') { value |= 1; @@ -1140,7 +1100,7 @@ BinaryFormatCmd( } } } else { - for (offset = 0; offset < count; offset++) { + for (offset = 0; (size_t)offset < count; offset++) { value >>= 1; if (str[offset] == '1') { value |= 128; @@ -1173,7 +1133,7 @@ BinaryFormatCmd( unsigned char *last; int c; - str = Tcl_GetStringFromObj(objv[arg], &length); + str = TclGetStringFromObj(objv[arg], &length); arg++; if (count == BINARY_ALL) { count = length; @@ -1187,7 +1147,7 @@ BinaryFormatCmd( value = 0; errorString = "hexadecimal"; if (cmd == 'H') { - for (offset = 0; offset < count; offset++) { + for (offset = 0; (size_t)offset < count; offset++) { value <<= 4; if (!isxdigit(UCHAR(str[offset]))) { /* INTL: digit */ errorValue = str; @@ -1208,7 +1168,7 @@ BinaryFormatCmd( } } } else { - for (offset = 0; offset < count; offset++) { + for (offset = 0; (size_t)offset < count; offset++) { value >>= 4; if (!isxdigit(UCHAR(str[offset]))) { /* INTL: digit */ @@ -1279,7 +1239,7 @@ BinaryFormatCmd( } } arg++; - for (i = 0; i < count; i++) { + for (i = 0; (size_t)i < count; i++) { if (FormatNumber(interp, cmd, listv[i], &cursor)!=TCL_OK) { Tcl_DecrRefCount(resultPtr); return TCL_ERROR; @@ -1291,7 +1251,7 @@ BinaryFormatCmd( if (count == BINARY_NOCOUNT) { count = 1; } - memset(cursor, 0, (size_t) count); + memset(cursor, 0, count); cursor += count; break; case 'X': @@ -1301,7 +1261,7 @@ BinaryFormatCmd( if (count == BINARY_NOCOUNT) { count = 1; } - if ((count == BINARY_ALL) || (count > (cursor - buffer))) { + if ((count == BINARY_ALL) || (count > (size_t)(cursor - buffer))) { cursor = buffer; } else { cursor -= count; @@ -1381,7 +1341,7 @@ BinaryScanCmd( int value = 0; /* Current integer value to be packed. * Initialized to avoid compiler warning. */ char cmd; /* Current format character. */ - int count; /* Count associated with current format + size_t count; /* Count associated with current format * character. */ int flags; /* Format field flags */ const char *format; /* Pointer to current position in format @@ -1390,7 +1350,8 @@ BinaryScanCmd( unsigned char *buffer; /* Start of result buffer. */ const char *errorString; const char *str; - int offset, size, length; + int offset, size; + size_t length; int i; Tcl_Obj *valuePtr, *elementPtr; @@ -1429,7 +1390,7 @@ BinaryScanCmd( if (count == BINARY_NOCOUNT) { count = 1; } - if (count > (length - offset)) { + if (count > length - offset) { goto done; } } @@ -1488,7 +1449,7 @@ BinaryScanCmd( if (count == BINARY_NOCOUNT) { count = 1; } - if (count > (length - offset) * 8) { + if (count > (size_t)(length - offset) * 8) { goto done; } } @@ -1498,7 +1459,7 @@ BinaryScanCmd( dest = TclGetString(valuePtr); if (cmd == 'b') { - for (i = 0; i < count; i++) { + for (i = 0; (size_t)i < count; i++) { if (i % 8) { value >>= 1; } else { @@ -1507,7 +1468,7 @@ BinaryScanCmd( *dest++ = (char) ((value & 1) ? '1' : '0'); } } else { - for (i = 0; i < count; i++) { + for (i = 0; (size_t)i < count; i++) { if (i % 8) { value <<= 1; } else { @@ -1553,7 +1514,7 @@ BinaryScanCmd( dest = TclGetString(valuePtr); if (cmd == 'h') { - for (i = 0; i < count; i++) { + for (i = 0; (size_t)i < count; i++) { if (i % 2) { value >>= 4; } else { @@ -1562,7 +1523,7 @@ BinaryScanCmd( *dest++ = hexdigit[value & 0xf]; } } else { - for (i = 0; i < count; i++) { + for (i = 0; (size_t)i < count; i++) { if (i % 2) { value <<= 4; } else { @@ -1619,7 +1580,7 @@ BinaryScanCmd( goto badIndex; } if (count == BINARY_NOCOUNT) { - if ((length - offset) < size) { + if ((length - offset) < (size_t)size) { goto done; } valuePtr = ScanNumber(buffer+offset, cmd, flags, @@ -1634,7 +1595,7 @@ BinaryScanCmd( } valuePtr = Tcl_NewObj(); src = buffer + offset; - for (i = 0; i < count; i++) { + for (i = 0; (size_t)i < count; i++) { elementPtr = ScanNumber(src, cmd, flags, &numberCachePtr); src += size; Tcl_ListObjAppendElement(NULL, valuePtr, elementPtr); @@ -1655,7 +1616,7 @@ BinaryScanCmd( if (count == BINARY_NOCOUNT) { count = 1; } - if ((count == BINARY_ALL) || (count > (length - offset))) { + if ((count == BINARY_ALL) || (count > length - offset)) { offset = length; } else { offset += count; @@ -1665,7 +1626,7 @@ BinaryScanCmd( if (count == BINARY_NOCOUNT) { count = 1; } - if ((count == BINARY_ALL) || (count > offset)) { + if ((count == BINARY_ALL) || (count > (size_t)offset)) { offset = 0; } else { offset -= count; @@ -1749,7 +1710,7 @@ static int GetFormatSpec( const char **formatPtr, /* Pointer to format string. */ char *cmdPtr, /* Pointer to location of command char. */ - int *countPtr, /* Pointer to repeat count value. */ + size_t *countPtr, /* Pointer to repeat count value. */ int *flagsPtr) /* Pointer to field flags */ { /* @@ -1915,7 +1876,7 @@ static void CopyNumber( const void *from, /* source */ void *to, /* destination */ - unsigned length, /* Number of bytes to copy */ + size_t length, /* Number of bytes to copy */ int type) /* What type of thing are we copying? */ { switch (NeedReversing(type)) { @@ -2017,7 +1978,7 @@ FormatNumber( */ if (Tcl_GetDoubleFromObj(interp, src, &dvalue) != TCL_OK) { - const Tcl_ObjIntRep *irPtr = Tcl_FetchIntRep(src, &tclDoubleType); + const Tcl_ObjIntRep *irPtr = TclFetchIntRep(src, &tclDoubleType); if (irPtr == NULL) { return TCL_ERROR; } @@ -2037,7 +1998,7 @@ FormatNumber( */ if (Tcl_GetDoubleFromObj(interp, src, &dvalue) != TCL_OK) { - const Tcl_ObjIntRep *irPtr = Tcl_FetchIntRep(src, &tclDoubleType); + const Tcl_ObjIntRep *irPtr = TclFetchIntRep(src, &tclDoubleType); if (irPtr == NULL) { return TCL_ERROR; } @@ -2445,7 +2406,7 @@ BinaryEncodeHex( Tcl_Obj *resultObj = NULL; unsigned char *data = NULL; unsigned char *cursor = NULL; - int offset = 0, count = 0; + size_t offset = 0, count = 0; if (objc != 2) { Tcl_WrongNumArgs(interp, 1, objv, "data"); @@ -2611,7 +2572,8 @@ BinaryEncode64( int maxlen = 0; const char *wrapchar = "\n"; size_t wrapcharlen = 1; - int offset, i, index, size, outindex = 0, count = 0; + int i, index, size, outindex = 0; + size_t offset, count = 0; enum {OPT_MAXLEN, OPT_WRAPCHAR }; static const char *const optStrings[] = { "-maxlen", "-wrapchar", NULL }; @@ -2714,11 +2676,11 @@ BinaryEncodeUu( { Tcl_Obj *resultObj; unsigned char *data, *start, *cursor; - int offset, count, rawLength, n, i, j, bits, index; + int rawLength, n, i, bits, index; int lineLength = 61; const unsigned char SingleNewline[] = { (unsigned char) '\n' }; const unsigned char *wrapchar = SingleNewline; - int wrapcharlen = sizeof(SingleNewline); + size_t j, offset, count, wrapcharlen = sizeof(SingleNewline); enum { OPT_MAXLEN, OPT_WRAPCHAR }; static const char *const optStrings[] = { "-maxlen", "-wrapchar", NULL }; |