aboutsummaryrefslogtreecommitdiffstats
path: root/Frameworks/SPMySQLFramework/Source/SPMySQLResult Categories
diff options
context:
space:
mode:
Diffstat (limited to 'Frameworks/SPMySQLFramework/Source/SPMySQLResult Categories')
-rw-r--r--Frameworks/SPMySQLFramework/Source/SPMySQLResult Categories/Data Conversion.m27
1 files changed, 13 insertions, 14 deletions
diff --git a/Frameworks/SPMySQLFramework/Source/SPMySQLResult Categories/Data Conversion.m b/Frameworks/SPMySQLFramework/Source/SPMySQLResult Categories/Data Conversion.m
index 639ff0b9..8c5b9181 100644
--- a/Frameworks/SPMySQLFramework/Source/SPMySQLResult Categories/Data Conversion.m
+++ b/Frameworks/SPMySQLFramework/Source/SPMySQLResult Categories/Data Conversion.m
@@ -209,27 +209,26 @@ static inline NSString * _bitStringWithBytes(const char *bytes, NSUInteger lengt
return nil;
}
- // Ensure padLength is never lower than the length
- if (padLength < bitLength) {
- padLength = bitLength;
- }
-
+ // use whatever is smaller. padLength comes from BIT(x), bitLength from the actual bytes transmitted.
+ // if bitLength < padLength it means the value is smaller than what the field can accomodate.
+ // if bitLength > padLength it means BIT(x) is not a full n bytes long and was extended by mysqls storage.
+ // In that case the additional bits should still be 0 as mysql does not allow to set bits over the size of x.
+ bitLength = MIN(bitLength,padLength);
// Generate a nul-terminated C string representation of the binary data
char *cStringBuffer = malloc(padLength + 1);
- cStringBuffer[padLength] = '\0';
+ memset(cStringBuffer, '0', padLength);
while (i < bitLength)
{
+ // start with the least significant bit (the rightmost bit in the last byte) and move left
+ unsigned char bitInByteMask = i % 8; // 0-7, the cycle is 0,1,...,7,0,...
+ unsigned long bytesOffset = (length - 1) - (i >> 3); // i>>3 == floor(i/8)
++i;
-
- cStringBuffer[padLength - i] = ((bytes[length - 1 - (i >> 3)] >> (i & 0x7)) & 1 ) ? '1' : '0';
+ cStringBuffer[padLength - i] = ((bytes[bytesOffset] & (1 << bitInByteMask)) != 0) ? '1' : '0';
}
-
- while (i++ < padLength)
- {
- cStringBuffer[padLength - i] = '0';
- }
-
+
+ cStringBuffer[padLength] = '\0';
+
// Convert to a string
NSString *returnString = [NSString stringWithUTF8String:cStringBuffer];