Files

68 lines
2.1 KiB
Objective-C

#import "MMSMCParser.h"
#import <string.h>
@implementation MMSMCParser
+ (float)decodeSP78:(const uint8_t *)bytes {
if (!bytes) return 0.0f;
int16_t raw = (int16_t)(((uint16_t)bytes[0] << 8) | (uint16_t)bytes[1]);
return (float)raw / 256.0f;
}
+ (float)decodeFPE2:(const uint8_t *)bytes {
if (!bytes) return 0.0f;
uint16_t raw = ((uint16_t)bytes[0] << 8) | (uint16_t)bytes[1];
return (float)raw / 4.0f;
}
+ (float)decodeFLT:(const uint8_t *)bytes {
if (!bytes) return 0.0f;
float val = 0.0f;
memcpy(&val, bytes, sizeof(float));
return val;
}
+ (uint8_t)decodeUI8:(const uint8_t *)bytes {
if (!bytes) return 0;
return bytes[0];
}
+ (uint16_t)decodeUI16:(const uint8_t *)bytes {
if (!bytes) return 0;
return ((uint16_t)bytes[0] << 8) | (uint16_t)bytes[1];
}
+ (uint32_t)decodeUI32:(const uint8_t *)bytes {
if (!bytes) return 0;
return ((uint32_t)bytes[0] << 24) |
((uint32_t)bytes[1] << 16) |
((uint32_t)bytes[2] << 8) |
(uint32_t)bytes[3];
}
+ (nullable NSNumber *)decodeValueWithDataType:(NSString *)dataType bytes:(const uint8_t *)bytes size:(NSUInteger)size {
if (!bytes || size == 0) return nil;
NSString *trimmedType = [dataType stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
if ([trimmedType isEqualToString:@"sp78"]) {
if (size >= 2) return @([self decodeSP78:bytes]);
} else if ([trimmedType isEqualToString:@"fpe2"]) {
if (size >= 2) return @([self decodeFPE2:bytes]);
} else if ([trimmedType isEqualToString:@"flt"]) {
if (size >= 4) return @([self decodeFLT:bytes]);
} else if ([trimmedType isEqualToString:@"ui8"]) {
if (size >= 1) return @([self decodeUI8:bytes]);
} else if ([trimmedType isEqualToString:@"ui16"]) {
if (size >= 2) return @([self decodeUI16:bytes]);
} else if ([trimmedType isEqualToString:@"ui32"]) {
if (size >= 4) return @([self decodeUI32:bytes]);
} else if ([trimmedType isEqualToString:@"flag"]) {
if (size >= 1) return @(bytes[0] != 0);
}
return nil;
}
@end