diff options
author | Max <post@wickenrode.com> | 2015-10-02 16:31:39 +0200 |
---|---|---|
committer | Max <post@wickenrode.com> | 2015-10-02 16:31:39 +0200 |
commit | 11f5bf55b3d6ecb9afe3a19d60aa9669bd449e42 (patch) | |
tree | 44cc40fbdc59d26f7a98ed975189b45671a676f6 /Frameworks/SPMySQLFramework/Source/SPMySQLResult Categories | |
parent | c5878bdad37e353b5411d4b1a3e5233213d72a1b (diff) | |
download | sequelpro-11f5bf55b3d6ecb9afe3a19d60aa9669bd449e42.tar.gz sequelpro-11f5bf55b3d6ecb9afe3a19d60aa9669bd449e42.tar.bz2 sequelpro-11f5bf55b3d6ecb9afe3a19d60aa9669bd449e42.zip |
Fix conversion of BIT fields (fixes #2254)
Diffstat (limited to 'Frameworks/SPMySQLFramework/Source/SPMySQLResult Categories')
-rw-r--r-- | Frameworks/SPMySQLFramework/Source/SPMySQLResult Categories/Data Conversion.m | 27 |
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]; |