├── .gitignore ├── MVPCStruct.xcodeproj └── project.pbxproj ├── MVPCStruct ├── CStruct.swift ├── Info.plist └── MVPCStruct.h ├── MVPCStructTests ├── Info.plist ├── PackTests.swift └── UnpackTests.swift └── README.markdown /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | *.pyc 3 | *.pbxuser 4 | *.mode1v3 5 | *.mode2v3 6 | *.perspectivev3 7 | *.xcuserstate 8 | project.xcworkspace/ 9 | xcuserdata/ 10 | -------------------------------------------------------------------------------- /MVPCStruct.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 46; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | 058E72AE194B36DD002168A2 /* PackTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 058E72AD194B36DD002168A2 /* PackTests.swift */; }; 11 | 058E72B0194B3715002168A2 /* UnpackTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 058E72AF194B3715002168A2 /* UnpackTests.swift */; }; 12 | 05A8B6C2194B1718006366DF /* MVPCStruct.h in Headers */ = {isa = PBXBuildFile; fileRef = 05A8B6C1194B1718006366DF /* MVPCStruct.h */; settings = {ATTRIBUTES = (Public, ); }; }; 13 | 05A8B6C8194B1718006366DF /* MVPCStruct.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 05A8B6BC194B1718006366DF /* MVPCStruct.framework */; }; 14 | 05A8B6D9194B1755006366DF /* CStruct.swift in Sources */ = {isa = PBXBuildFile; fileRef = 05A8B6D8194B1755006366DF /* CStruct.swift */; }; 15 | /* End PBXBuildFile section */ 16 | 17 | /* Begin PBXContainerItemProxy section */ 18 | 05A8B6C9194B1718006366DF /* PBXContainerItemProxy */ = { 19 | isa = PBXContainerItemProxy; 20 | containerPortal = 05A8B6B3194B1718006366DF /* Project object */; 21 | proxyType = 1; 22 | remoteGlobalIDString = 05A8B6BB194B1718006366DF; 23 | remoteInfo = MVPCStruct; 24 | }; 25 | /* End PBXContainerItemProxy section */ 26 | 27 | /* Begin PBXFileReference section */ 28 | 058E72AD194B36DD002168A2 /* PackTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PackTests.swift; sourceTree = ""; }; 29 | 058E72AF194B3715002168A2 /* UnpackTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UnpackTests.swift; sourceTree = ""; }; 30 | 05A8B6BC194B1718006366DF /* MVPCStruct.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = MVPCStruct.framework; sourceTree = BUILT_PRODUCTS_DIR; }; 31 | 05A8B6C0194B1718006366DF /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 32 | 05A8B6C1194B1718006366DF /* MVPCStruct.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MVPCStruct.h; sourceTree = ""; }; 33 | 05A8B6C7194B1718006366DF /* MVPCStructTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = MVPCStructTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; 34 | 05A8B6CD194B1718006366DF /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 35 | 05A8B6D8194B1755006366DF /* CStruct.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CStruct.swift; sourceTree = ""; }; 36 | /* End PBXFileReference section */ 37 | 38 | /* Begin PBXFrameworksBuildPhase section */ 39 | 05A8B6B8194B1718006366DF /* Frameworks */ = { 40 | isa = PBXFrameworksBuildPhase; 41 | buildActionMask = 2147483647; 42 | files = ( 43 | ); 44 | runOnlyForDeploymentPostprocessing = 0; 45 | }; 46 | 05A8B6C4194B1718006366DF /* Frameworks */ = { 47 | isa = PBXFrameworksBuildPhase; 48 | buildActionMask = 2147483647; 49 | files = ( 50 | 05A8B6C8194B1718006366DF /* MVPCStruct.framework in Frameworks */, 51 | ); 52 | runOnlyForDeploymentPostprocessing = 0; 53 | }; 54 | /* End PBXFrameworksBuildPhase section */ 55 | 56 | /* Begin PBXGroup section */ 57 | 05A8B6B2194B1718006366DF = { 58 | isa = PBXGroup; 59 | children = ( 60 | 05A8B6BE194B1718006366DF /* MVPCStruct */, 61 | 05A8B6CB194B1718006366DF /* MVPCStructTests */, 62 | 05A8B6BD194B1718006366DF /* Products */, 63 | ); 64 | sourceTree = ""; 65 | }; 66 | 05A8B6BD194B1718006366DF /* Products */ = { 67 | isa = PBXGroup; 68 | children = ( 69 | 05A8B6BC194B1718006366DF /* MVPCStruct.framework */, 70 | 05A8B6C7194B1718006366DF /* MVPCStructTests.xctest */, 71 | ); 72 | name = Products; 73 | sourceTree = ""; 74 | }; 75 | 05A8B6BE194B1718006366DF /* MVPCStruct */ = { 76 | isa = PBXGroup; 77 | children = ( 78 | 05A8B6C1194B1718006366DF /* MVPCStruct.h */, 79 | 05A8B6D8194B1755006366DF /* CStruct.swift */, 80 | 05A8B6BF194B1718006366DF /* Supporting Files */, 81 | ); 82 | path = MVPCStruct; 83 | sourceTree = ""; 84 | }; 85 | 05A8B6BF194B1718006366DF /* Supporting Files */ = { 86 | isa = PBXGroup; 87 | children = ( 88 | 05A8B6C0194B1718006366DF /* Info.plist */, 89 | ); 90 | name = "Supporting Files"; 91 | sourceTree = ""; 92 | }; 93 | 05A8B6CB194B1718006366DF /* MVPCStructTests */ = { 94 | isa = PBXGroup; 95 | children = ( 96 | 058E72AD194B36DD002168A2 /* PackTests.swift */, 97 | 058E72AF194B3715002168A2 /* UnpackTests.swift */, 98 | 05A8B6CC194B1718006366DF /* Supporting Files */, 99 | ); 100 | path = MVPCStructTests; 101 | sourceTree = ""; 102 | }; 103 | 05A8B6CC194B1718006366DF /* Supporting Files */ = { 104 | isa = PBXGroup; 105 | children = ( 106 | 05A8B6CD194B1718006366DF /* Info.plist */, 107 | ); 108 | name = "Supporting Files"; 109 | sourceTree = ""; 110 | }; 111 | /* End PBXGroup section */ 112 | 113 | /* Begin PBXHeadersBuildPhase section */ 114 | 05A8B6B9194B1718006366DF /* Headers */ = { 115 | isa = PBXHeadersBuildPhase; 116 | buildActionMask = 2147483647; 117 | files = ( 118 | 05A8B6C2194B1718006366DF /* MVPCStruct.h in Headers */, 119 | ); 120 | runOnlyForDeploymentPostprocessing = 0; 121 | }; 122 | /* End PBXHeadersBuildPhase section */ 123 | 124 | /* Begin PBXNativeTarget section */ 125 | 05A8B6BB194B1718006366DF /* MVPCStruct */ = { 126 | isa = PBXNativeTarget; 127 | buildConfigurationList = 05A8B6D2194B1718006366DF /* Build configuration list for PBXNativeTarget "MVPCStruct" */; 128 | buildPhases = ( 129 | 05A8B6B7194B1718006366DF /* Sources */, 130 | 05A8B6B8194B1718006366DF /* Frameworks */, 131 | 05A8B6B9194B1718006366DF /* Headers */, 132 | 05A8B6BA194B1718006366DF /* Resources */, 133 | ); 134 | buildRules = ( 135 | ); 136 | dependencies = ( 137 | ); 138 | name = MVPCStruct; 139 | productName = MVPCStruct; 140 | productReference = 05A8B6BC194B1718006366DF /* MVPCStruct.framework */; 141 | productType = "com.apple.product-type.framework"; 142 | }; 143 | 05A8B6C6194B1718006366DF /* MVPCStructTests */ = { 144 | isa = PBXNativeTarget; 145 | buildConfigurationList = 05A8B6D5194B1718006366DF /* Build configuration list for PBXNativeTarget "MVPCStructTests" */; 146 | buildPhases = ( 147 | 05A8B6C3194B1718006366DF /* Sources */, 148 | 05A8B6C4194B1718006366DF /* Frameworks */, 149 | 05A8B6C5194B1718006366DF /* Resources */, 150 | ); 151 | buildRules = ( 152 | ); 153 | dependencies = ( 154 | 05A8B6CA194B1718006366DF /* PBXTargetDependency */, 155 | ); 156 | name = MVPCStructTests; 157 | productName = MVPCStructTests; 158 | productReference = 05A8B6C7194B1718006366DF /* MVPCStructTests.xctest */; 159 | productType = "com.apple.product-type.bundle.unit-test"; 160 | }; 161 | /* End PBXNativeTarget section */ 162 | 163 | /* Begin PBXProject section */ 164 | 05A8B6B3194B1718006366DF /* Project object */ = { 165 | isa = PBXProject; 166 | attributes = { 167 | LastUpgradeCheck = 0600; 168 | ORGANIZATIONNAME = AutoMac; 169 | TargetAttributes = { 170 | 05A8B6BB194B1718006366DF = { 171 | CreatedOnToolsVersion = 6.0; 172 | }; 173 | 05A8B6C6194B1718006366DF = { 174 | CreatedOnToolsVersion = 6.0; 175 | TestTargetID = 05A8B6BB194B1718006366DF; 176 | }; 177 | }; 178 | }; 179 | buildConfigurationList = 05A8B6B6194B1718006366DF /* Build configuration list for PBXProject "MVPCStruct" */; 180 | compatibilityVersion = "Xcode 3.2"; 181 | developmentRegion = English; 182 | hasScannedForEncodings = 0; 183 | knownRegions = ( 184 | en, 185 | ); 186 | mainGroup = 05A8B6B2194B1718006366DF; 187 | productRefGroup = 05A8B6BD194B1718006366DF /* Products */; 188 | projectDirPath = ""; 189 | projectRoot = ""; 190 | targets = ( 191 | 05A8B6BB194B1718006366DF /* MVPCStruct */, 192 | 05A8B6C6194B1718006366DF /* MVPCStructTests */, 193 | ); 194 | }; 195 | /* End PBXProject section */ 196 | 197 | /* Begin PBXResourcesBuildPhase section */ 198 | 05A8B6BA194B1718006366DF /* Resources */ = { 199 | isa = PBXResourcesBuildPhase; 200 | buildActionMask = 2147483647; 201 | files = ( 202 | ); 203 | runOnlyForDeploymentPostprocessing = 0; 204 | }; 205 | 05A8B6C5194B1718006366DF /* Resources */ = { 206 | isa = PBXResourcesBuildPhase; 207 | buildActionMask = 2147483647; 208 | files = ( 209 | ); 210 | runOnlyForDeploymentPostprocessing = 0; 211 | }; 212 | /* End PBXResourcesBuildPhase section */ 213 | 214 | /* Begin PBXSourcesBuildPhase section */ 215 | 05A8B6B7194B1718006366DF /* Sources */ = { 216 | isa = PBXSourcesBuildPhase; 217 | buildActionMask = 2147483647; 218 | files = ( 219 | 05A8B6D9194B1755006366DF /* CStruct.swift in Sources */, 220 | ); 221 | runOnlyForDeploymentPostprocessing = 0; 222 | }; 223 | 05A8B6C3194B1718006366DF /* Sources */ = { 224 | isa = PBXSourcesBuildPhase; 225 | buildActionMask = 2147483647; 226 | files = ( 227 | 058E72AE194B36DD002168A2 /* PackTests.swift in Sources */, 228 | 058E72B0194B3715002168A2 /* UnpackTests.swift in Sources */, 229 | ); 230 | runOnlyForDeploymentPostprocessing = 0; 231 | }; 232 | /* End PBXSourcesBuildPhase section */ 233 | 234 | /* Begin PBXTargetDependency section */ 235 | 05A8B6CA194B1718006366DF /* PBXTargetDependency */ = { 236 | isa = PBXTargetDependency; 237 | target = 05A8B6BB194B1718006366DF /* MVPCStruct */; 238 | targetProxy = 05A8B6C9194B1718006366DF /* PBXContainerItemProxy */; 239 | }; 240 | /* End PBXTargetDependency section */ 241 | 242 | /* Begin XCBuildConfiguration section */ 243 | 05A8B6D0194B1718006366DF /* Debug */ = { 244 | isa = XCBuildConfiguration; 245 | buildSettings = { 246 | ALWAYS_SEARCH_USER_PATHS = NO; 247 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 248 | CLANG_CXX_LIBRARY = "libc++"; 249 | CLANG_ENABLE_MODULES = YES; 250 | CLANG_ENABLE_OBJC_ARC = YES; 251 | CLANG_WARN_BOOL_CONVERSION = YES; 252 | CLANG_WARN_CONSTANT_CONVERSION = YES; 253 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 254 | CLANG_WARN_EMPTY_BODY = YES; 255 | CLANG_WARN_ENUM_CONVERSION = YES; 256 | CLANG_WARN_INT_CONVERSION = YES; 257 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 258 | CLANG_WARN_UNREACHABLE_CODE = YES; 259 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 260 | COPY_PHASE_STRIP = NO; 261 | CURRENT_PROJECT_VERSION = 1; 262 | ENABLE_STRICT_OBJC_MSGSEND = YES; 263 | GCC_C_LANGUAGE_STANDARD = gnu99; 264 | GCC_DYNAMIC_NO_PIC = NO; 265 | GCC_OPTIMIZATION_LEVEL = 0; 266 | GCC_PREPROCESSOR_DEFINITIONS = ( 267 | "DEBUG=1", 268 | "$(inherited)", 269 | ); 270 | GCC_SYMBOLS_PRIVATE_EXTERN = NO; 271 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 272 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 273 | GCC_WARN_UNDECLARED_SELECTOR = YES; 274 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 275 | GCC_WARN_UNUSED_FUNCTION = YES; 276 | GCC_WARN_UNUSED_VARIABLE = YES; 277 | MACOSX_DEPLOYMENT_TARGET = 10.9; 278 | METAL_ENABLE_DEBUG_INFO = YES; 279 | ONLY_ACTIVE_ARCH = YES; 280 | SDKROOT = macosx; 281 | SWIFT_OPTIMIZATION_LEVEL = "-Onone"; 282 | VERSIONING_SYSTEM = "apple-generic"; 283 | VERSION_INFO_PREFIX = ""; 284 | }; 285 | name = Debug; 286 | }; 287 | 05A8B6D1194B1718006366DF /* Release */ = { 288 | isa = XCBuildConfiguration; 289 | buildSettings = { 290 | ALWAYS_SEARCH_USER_PATHS = NO; 291 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 292 | CLANG_CXX_LIBRARY = "libc++"; 293 | CLANG_ENABLE_MODULES = YES; 294 | CLANG_ENABLE_OBJC_ARC = YES; 295 | CLANG_WARN_BOOL_CONVERSION = YES; 296 | CLANG_WARN_CONSTANT_CONVERSION = YES; 297 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 298 | CLANG_WARN_EMPTY_BODY = YES; 299 | CLANG_WARN_ENUM_CONVERSION = YES; 300 | CLANG_WARN_INT_CONVERSION = YES; 301 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 302 | CLANG_WARN_UNREACHABLE_CODE = YES; 303 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 304 | COPY_PHASE_STRIP = YES; 305 | CURRENT_PROJECT_VERSION = 1; 306 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 307 | ENABLE_NS_ASSERTIONS = NO; 308 | ENABLE_STRICT_OBJC_MSGSEND = YES; 309 | GCC_C_LANGUAGE_STANDARD = gnu99; 310 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 311 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 312 | GCC_WARN_UNDECLARED_SELECTOR = YES; 313 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 314 | GCC_WARN_UNUSED_FUNCTION = YES; 315 | GCC_WARN_UNUSED_VARIABLE = YES; 316 | MACOSX_DEPLOYMENT_TARGET = 10.9; 317 | METAL_ENABLE_DEBUG_INFO = NO; 318 | SDKROOT = macosx; 319 | VERSIONING_SYSTEM = "apple-generic"; 320 | VERSION_INFO_PREFIX = ""; 321 | }; 322 | name = Release; 323 | }; 324 | 05A8B6D3194B1718006366DF /* Debug */ = { 325 | isa = XCBuildConfiguration; 326 | buildSettings = { 327 | CLANG_ENABLE_MODULES = YES; 328 | COMBINE_HIDPI_IMAGES = YES; 329 | DEFINES_MODULE = YES; 330 | DYLIB_COMPATIBILITY_VERSION = 1; 331 | DYLIB_CURRENT_VERSION = 1; 332 | DYLIB_INSTALL_NAME_BASE = "@rpath"; 333 | FRAMEWORK_VERSION = A; 334 | INFOPLIST_FILE = MVPCStruct/Info.plist; 335 | INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks"; 336 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/../Frameworks"; 337 | PRODUCT_NAME = "$(TARGET_NAME)"; 338 | SKIP_INSTALL = YES; 339 | SWIFT_OPTIMIZATION_LEVEL = "-Onone"; 340 | }; 341 | name = Debug; 342 | }; 343 | 05A8B6D4194B1718006366DF /* Release */ = { 344 | isa = XCBuildConfiguration; 345 | buildSettings = { 346 | CLANG_ENABLE_MODULES = YES; 347 | COMBINE_HIDPI_IMAGES = YES; 348 | DEFINES_MODULE = YES; 349 | DYLIB_COMPATIBILITY_VERSION = 1; 350 | DYLIB_CURRENT_VERSION = 1; 351 | DYLIB_INSTALL_NAME_BASE = "@rpath"; 352 | FRAMEWORK_VERSION = A; 353 | INFOPLIST_FILE = MVPCStruct/Info.plist; 354 | INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks"; 355 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/../Frameworks"; 356 | PRODUCT_NAME = "$(TARGET_NAME)"; 357 | SKIP_INSTALL = YES; 358 | }; 359 | name = Release; 360 | }; 361 | 05A8B6D6194B1718006366DF /* Debug */ = { 362 | isa = XCBuildConfiguration; 363 | buildSettings = { 364 | COMBINE_HIDPI_IMAGES = YES; 365 | FRAMEWORK_SEARCH_PATHS = ( 366 | "$(DEVELOPER_FRAMEWORKS_DIR)", 367 | "$(inherited)", 368 | ); 369 | GCC_PREPROCESSOR_DEFINITIONS = ( 370 | "DEBUG=1", 371 | "$(inherited)", 372 | ); 373 | INFOPLIST_FILE = MVPCStructTests/Info.plist; 374 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/../Frameworks"; 375 | METAL_ENABLE_DEBUG_INFO = YES; 376 | PRODUCT_NAME = "$(TARGET_NAME)"; 377 | }; 378 | name = Debug; 379 | }; 380 | 05A8B6D7194B1718006366DF /* Release */ = { 381 | isa = XCBuildConfiguration; 382 | buildSettings = { 383 | COMBINE_HIDPI_IMAGES = YES; 384 | FRAMEWORK_SEARCH_PATHS = ( 385 | "$(DEVELOPER_FRAMEWORKS_DIR)", 386 | "$(inherited)", 387 | ); 388 | INFOPLIST_FILE = MVPCStructTests/Info.plist; 389 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/../Frameworks"; 390 | METAL_ENABLE_DEBUG_INFO = NO; 391 | PRODUCT_NAME = "$(TARGET_NAME)"; 392 | }; 393 | name = Release; 394 | }; 395 | /* End XCBuildConfiguration section */ 396 | 397 | /* Begin XCConfigurationList section */ 398 | 05A8B6B6194B1718006366DF /* Build configuration list for PBXProject "MVPCStruct" */ = { 399 | isa = XCConfigurationList; 400 | buildConfigurations = ( 401 | 05A8B6D0194B1718006366DF /* Debug */, 402 | 05A8B6D1194B1718006366DF /* Release */, 403 | ); 404 | defaultConfigurationIsVisible = 0; 405 | defaultConfigurationName = Release; 406 | }; 407 | 05A8B6D2194B1718006366DF /* Build configuration list for PBXNativeTarget "MVPCStruct" */ = { 408 | isa = XCConfigurationList; 409 | buildConfigurations = ( 410 | 05A8B6D3194B1718006366DF /* Debug */, 411 | 05A8B6D4194B1718006366DF /* Release */, 412 | ); 413 | defaultConfigurationIsVisible = 0; 414 | defaultConfigurationName = Release; 415 | }; 416 | 05A8B6D5194B1718006366DF /* Build configuration list for PBXNativeTarget "MVPCStructTests" */ = { 417 | isa = XCConfigurationList; 418 | buildConfigurations = ( 419 | 05A8B6D6194B1718006366DF /* Debug */, 420 | 05A8B6D7194B1718006366DF /* Release */, 421 | ); 422 | defaultConfigurationIsVisible = 0; 423 | defaultConfigurationName = Release; 424 | }; 425 | /* End XCConfigurationList section */ 426 | }; 427 | rootObject = 05A8B6B3194B1718006366DF /* Project object */; 428 | } 429 | -------------------------------------------------------------------------------- /MVPCStruct/CStruct.swift: -------------------------------------------------------------------------------- 1 | // 2 | // CStruct.swift 3 | // MVPCStruct 4 | // 5 | // Created by Per Olofsson on 2014-06-13. 6 | // Copyright (c) 2014 AutoMac. All rights reserved. 7 | // 8 | 9 | import Cocoa 10 | 11 | // BYTE ORDER SIZE ALIGNMENT 12 | // @ native native native 13 | // = native standard none 14 | // < little-endian standard none 15 | // > big-endian standard none 16 | // ! network (BE) standard none 17 | 18 | 19 | // FORMAT C TYPE SWIFT TYPE SIZE 20 | // x pad byte no value 21 | // c char String of length 1 1 22 | // b signed char Int 1 23 | // B unsigned char UInt 1 24 | // ? _Bool Bool 1 25 | // h short Int 2 26 | // H unsigned short UInt 2 27 | // i int Int 4 28 | // I unsigned int UInt 4 29 | // l long Int 4 30 | // L unsigned long UInt 4 31 | // q long long Int 8 32 | // Q unsigned long long UInt 8 33 | // f float Float 4 34 | // d double Double 8 35 | // s char[] String 36 | // p char[] String 37 | // P void * UInt 4/8 38 | // 39 | // Floats and doubles are packed with IEEE 754 binary32 or binary64 format. 40 | 41 | 42 | // Split a large integer into bytes. 43 | extension Int { 44 | func splitBytes(endianness: CStruct.Endianness, size: Int) -> UInt8[] { 45 | var bytes = UInt8[]() 46 | var shift: Int 47 | var step: Int 48 | if endianness == .LittleEndian { 49 | shift = 0 50 | step = 8 51 | } else { 52 | shift = (size - 1) * 8 53 | step = -8 54 | } 55 | for count in 0..size { 56 | bytes.append(UInt8((self >> shift) & 0xff)) 57 | shift += step 58 | } 59 | return bytes 60 | } 61 | } 62 | extension UInt { 63 | func splitBytes(endianness: CStruct.Endianness, size: Int) -> UInt8[] { 64 | var bytes = UInt8[]() 65 | var shift: Int 66 | var step: Int 67 | if endianness == .LittleEndian { 68 | shift = 0 69 | step = 8 70 | } else { 71 | shift = Int((size - 1) * 8) 72 | step = -8 73 | } 74 | for count in 0..size { 75 | bytes.append(UInt8((self >> UInt(shift)) & 0xff)) 76 | shift = shift + step 77 | } 78 | return bytes 79 | } 80 | } 81 | 82 | 83 | class CStruct: NSObject { 84 | 85 | enum Error: Int { 86 | case Parsing = -1 87 | case Packing = -2 88 | case Unpacking = -3 89 | } 90 | let ERROR_DOMAIN = "se.gu.it.GUStructPacker" 91 | 92 | enum Endianness { 93 | case LittleEndian 94 | case BigEndian 95 | } 96 | 97 | // Packing format strings are parsed to a stream of ops. 98 | enum Ops { 99 | // Stop packing. 100 | case Stop 101 | 102 | // Control endianness. 103 | case SetNativeEndian 104 | case SetLittleEndian 105 | case SetBigEndian 106 | 107 | // Control alignment. 108 | case SetAlign 109 | case UnsetAlign 110 | 111 | // Pad bytes. 112 | case SkipByte 113 | 114 | // Packed values. 115 | case PackChar 116 | case PackInt8 117 | case PackUInt8 118 | case PackBool 119 | case PackInt16 120 | case PackUInt16 121 | case PackInt32 122 | case PackUInt32 123 | case PackInt64 124 | case PackUInt64 125 | case PackFloat 126 | case PackDouble 127 | case PackCString 128 | case PackPString 129 | case PackPointer 130 | } 131 | 132 | var opStream = Ops[]() 133 | 134 | let bytesForValue = [ 135 | Ops.SkipByte: 1, 136 | Ops.PackChar: 1, 137 | Ops.PackInt8: 1, 138 | Ops.PackUInt8: 1, 139 | Ops.PackBool: 1, 140 | Ops.PackInt16: 2, 141 | Ops.PackUInt16: 2, 142 | Ops.PackInt32: 4, 143 | Ops.PackUInt32: 4, 144 | Ops.PackInt64: 8, 145 | Ops.PackUInt64: 8, 146 | Ops.PackFloat: 4, 147 | Ops.PackDouble: 8, 148 | Ops.PackPointer: sizeof(CConstVoidPointer), 149 | ] 150 | 151 | let PAD_BYTE = UInt8(0) 152 | 153 | var platformEndianness: Endianness { 154 | return .LittleEndian 155 | } 156 | 157 | convenience init(format: String) { 158 | self.init() 159 | var error: NSError? 160 | if !self.parseFormat(format, error: &error) { 161 | assert(false, "format string parsing error") 162 | } 163 | } 164 | 165 | 166 | // Unpacking. 167 | 168 | func unpack(data: NSData, format: String, error: NSErrorPointer) -> AnyObject[]? { 169 | if !self.parseFormat(format, error: error) { 170 | return nil 171 | } 172 | return self.unpack(data, error: error) 173 | } 174 | 175 | func unpack(data: NSData, error: NSErrorPointer) -> AnyObject[]? { 176 | 177 | var values = AnyObject[]() 178 | var index = 0 179 | var alignment = true 180 | var endianness = self.platformEndianness 181 | 182 | // Set error message and return nil. 183 | func failure(message: String) -> AnyObject[]? { 184 | if error { 185 | error.memory = NSError(domain: ERROR_DOMAIN, 186 | code: Error.Unpacking.toRaw(), 187 | userInfo: [NSLocalizedDescriptionKey: message]) 188 | } 189 | return nil 190 | } 191 | 192 | // If alignment is requested, skip pad bytes until alignment is 193 | // satisfied. 194 | func skipAlignment(size: Int) { 195 | if alignment { 196 | let mask = size - 1 197 | while (index & mask) != 0 { 198 | index++ 199 | } 200 | } 201 | } 202 | 203 | // Read UInt8 values from data. 204 | func readBytes(count: Int) -> UInt8[]? { 205 | var bytes = UInt8[]() 206 | if index + count > data.length { 207 | return nil 208 | } 209 | let ptr = UnsafePointer(data.bytes) 210 | let unsafeBytes = UnsafeArray(start:ptr + index, length:count) 211 | index += count 212 | for byte in unsafeBytes { 213 | bytes.append(byte) 214 | } 215 | return bytes 216 | } 217 | 218 | // Create integer from bytes. 219 | func intFromBytes(bytes: UInt8[]) -> Int { 220 | var i: Int = 0 221 | for byte in endianness == .LittleEndian ? bytes.reverse() : bytes { 222 | i <<= 8 223 | i |= Int(byte) 224 | } 225 | return i 226 | } 227 | func uintFromBytes(bytes: UInt8[]) -> UInt { 228 | var i: UInt = 0 229 | for byte in endianness == .LittleEndian ? bytes.reverse() : bytes { 230 | i <<= 8 231 | i |= UInt(byte) 232 | } 233 | return i 234 | } 235 | 236 | //var psize = sizeof(Int32) 237 | 238 | for op in self.opStream { 239 | // First check ops that don't consume data. 240 | switch op { 241 | 242 | case .Stop: 243 | return values 244 | 245 | case .SetNativeEndian: 246 | endianness = self.platformEndianness 247 | case .SetLittleEndian: 248 | endianness = .LittleEndian 249 | case .SetBigEndian: 250 | endianness = .BigEndian 251 | 252 | case .SetAlign: 253 | alignment = true 254 | case .UnsetAlign: 255 | alignment = false 256 | 257 | case .PackCString, .PackPString: 258 | assert(false, "cstring/pstring unimplemented") 259 | 260 | case .SkipByte: 261 | if let bytes = readBytes(1) { 262 | // Discard. 263 | } else { 264 | return failure("not enough data for format") 265 | } 266 | default: 267 | let bytesToUnpack = bytesForValue[op]! 268 | if let bytes = readBytes(bytesToUnpack) { 269 | 270 | switch op { 271 | 272 | case .SkipByte: 273 | break 274 | 275 | case .PackChar: 276 | values.append(NSString(format: "%c", bytes[0])) 277 | 278 | case .PackInt8: 279 | values.append(Int(bytes[0])) 280 | 281 | case .PackUInt8: 282 | values.append(UInt(bytes[0])) 283 | 284 | case .PackBool: 285 | if bytes[0] == UInt8(0) { 286 | values.append(false) 287 | } else { 288 | values.append(true) 289 | } 290 | 291 | case .PackInt16, .PackInt32, .PackInt64: 292 | values.append(intFromBytes(bytes)) 293 | 294 | case .PackUInt16, .PackUInt32, .PackUInt64, .PackPointer: 295 | values.append(uintFromBytes(bytes)) 296 | 297 | case .PackFloat, .PackDouble: 298 | assert(false, "float/double unimplemented") 299 | 300 | case .PackCString, .PackPString: 301 | assert(false, "cstring/pstring unimplemented") 302 | 303 | default: 304 | assert(false, "bad op in stream") 305 | } 306 | 307 | } else { 308 | return failure("not enough data for format") 309 | } 310 | } 311 | 312 | } 313 | 314 | return values 315 | } 316 | 317 | 318 | // Packing. 319 | 320 | func pack(values: AnyObject[], format: String, error: NSErrorPointer) -> NSData? { 321 | if !self.parseFormat(format, error: error) { 322 | return nil 323 | } 324 | return self.pack(values, error: error) 325 | } 326 | 327 | func pack(values: AnyObject[], error: NSErrorPointer) -> NSData? { 328 | 329 | var bytes = UInt8[]() 330 | var index = 0 331 | var alignment = true 332 | var endianness = self.platformEndianness 333 | 334 | // Set error message and return nil. 335 | func failure(message: String) -> NSData? { 336 | if error { 337 | error.memory = NSError(domain: ERROR_DOMAIN, 338 | code: Error.Packing.toRaw(), 339 | userInfo: [NSLocalizedDescriptionKey: message]) 340 | } 341 | return nil 342 | } 343 | 344 | // If alignment is requested, emit pad bytes until alignment is 345 | // satisfied. 346 | func padAlignment(size: Int) { 347 | if alignment { 348 | let mask = size - 1 349 | while (bytes.count & mask) != 0 { 350 | bytes.append(PAD_BYTE) 351 | } 352 | } 353 | } 354 | 355 | for op in self.opStream { 356 | // First check ops that don't consume values. 357 | switch op { 358 | 359 | case .Stop: 360 | if index != values.count { 361 | return failure("expected \(index) items for packing, got \(values.count)") 362 | } else { 363 | return NSData(bytes: bytes, length: bytes.count) 364 | } 365 | 366 | case .SetNativeEndian: 367 | endianness = self.platformEndianness 368 | case .SetLittleEndian: 369 | endianness = .LittleEndian 370 | case .SetBigEndian: 371 | endianness = .BigEndian 372 | 373 | case .SetAlign: 374 | alignment = true 375 | case .UnsetAlign: 376 | alignment = false 377 | 378 | case .SkipByte: 379 | bytes.append(PAD_BYTE) 380 | 381 | default: 382 | // No control op found so pop the next value. 383 | if index >= values.count { 384 | return failure("expected at least \(index) items for packing, got \(values.count)") 385 | } 386 | let rawValue: AnyObject = values[index++] 387 | 388 | switch op { 389 | 390 | case .PackChar: 391 | if let str = rawValue as? String { 392 | let codePoint = str.utf16[0] 393 | if codePoint < 128 { 394 | bytes.append(UInt8(codePoint)) 395 | } else { 396 | return failure("char format requires String of length 1") 397 | } 398 | } else { 399 | return failure("char format requires String of length 1") 400 | } 401 | 402 | case .PackInt8: 403 | if let value = rawValue as? Int { 404 | if value >= -0x80 && value <= 0x7f { 405 | bytes.append(UInt8(value & 0xff)) 406 | } else { 407 | return failure("value outside valid range of Int8") 408 | } 409 | } else { 410 | return failure("cannot convert argument to Int") 411 | } 412 | 413 | case .PackUInt8: 414 | if let value = rawValue as? UInt { 415 | if value > 0xff { 416 | return failure("value outside valid range of UInt8") 417 | } else { 418 | bytes.append(UInt8(value)) 419 | } 420 | } else { 421 | return failure("cannot convert argument to UInt") 422 | } 423 | 424 | case .PackBool: 425 | if let value = rawValue as? Bool { 426 | if value { 427 | bytes.append(UInt8(1)) 428 | } else { 429 | bytes.append(UInt8(0)) 430 | } 431 | } else { 432 | return failure("cannot convert argument to Bool") 433 | } 434 | 435 | case .PackInt16: 436 | if let value = rawValue as? Int { 437 | if value >= -0x8000 && value <= 0x7fff { 438 | padAlignment(2) 439 | bytes.extend(value.splitBytes(endianness, size: 2)) 440 | } else { 441 | return failure("value outside valid range of Int16") 442 | } 443 | } else { 444 | return failure("cannot convert argument to Int") 445 | } 446 | 447 | case .PackUInt16: 448 | if let value = rawValue as? UInt { 449 | if value > 0xffff { 450 | return failure("value outside valid range of UInt16") 451 | } else { 452 | padAlignment(2) 453 | bytes.extend(value.splitBytes(endianness, size: 2)) 454 | } 455 | } else { 456 | return failure("cannot convert argument to UInt") 457 | } 458 | 459 | case .PackInt32: 460 | if let value = rawValue as? Int { 461 | if value >= -0x80000000 && value <= 0x7fffffff { 462 | padAlignment(4) 463 | bytes.extend(value.splitBytes(endianness, size: 4)) 464 | } else { 465 | return failure("value outside valid range of Int32") 466 | } 467 | } else { 468 | return failure("cannot convert argument to Int") 469 | } 470 | 471 | case .PackUInt32: 472 | if let value = rawValue as? UInt { 473 | if value > 0xffffffff { 474 | return failure("value outside valid range of UInt32") 475 | } else { 476 | padAlignment(4) 477 | bytes.extend(value.splitBytes(endianness, size: 4)) 478 | } 479 | } else { 480 | return failure("cannot convert argument to UInt") 481 | } 482 | 483 | case .PackInt64: 484 | if let value = rawValue as? Int { 485 | padAlignment(8) 486 | bytes.extend(value.splitBytes(endianness, size: 8)) 487 | } else { 488 | return failure("cannot convert argument to Int") 489 | } 490 | 491 | case .PackUInt64: 492 | if let value = rawValue as? UInt { 493 | padAlignment(8) 494 | bytes.extend(value.splitBytes(endianness, size: 8)) 495 | } else { 496 | return failure("cannot convert argument to UInt") 497 | } 498 | 499 | case .PackFloat, .PackDouble: 500 | assert(false, "float/double unimplemented") 501 | 502 | case .PackCString, .PackPString: 503 | assert(false, "cstring/pstring unimplemented") 504 | 505 | case .PackPointer: 506 | if let value = rawValue as? UInt { 507 | padAlignment(sizeof(CConstVoidPointer)) 508 | bytes.extend(value.splitBytes(endianness, size: sizeof(CConstVoidPointer))) 509 | } else { 510 | return failure("cannot convert argument to UInt") 511 | } 512 | 513 | default: 514 | assert(false, "bad op in stream") 515 | } 516 | 517 | } 518 | 519 | } 520 | 521 | // This is actually never reached, we exit from .Stop. 522 | return NSData(bytes: bytes, length: bytes.count) 523 | } 524 | 525 | func parseFormat(format: String, error: NSErrorPointer) -> Bool { 526 | 527 | var repeat = 0 528 | 529 | opStream.removeAll(keepCapacity: false) 530 | 531 | for c in format { 532 | // First test if the format string contains an integer. In that case 533 | // we feed it into the repeat counter and go to the next character. 534 | if let value = String(c).toInt() { 535 | repeat = repeat * 10 + value 536 | continue 537 | } 538 | // The next step depends on if we've accumulated a repeat count. 539 | if repeat == 0 { 540 | 541 | // With a repeat count of 0 we check for control characters. 542 | switch c { 543 | 544 | // Control endianness. 545 | case "@": 546 | opStream.append(.SetNativeEndian) 547 | opStream.append(.SetAlign) 548 | case "=": 549 | opStream.append(.SetNativeEndian) 550 | opStream.append(.UnsetAlign) 551 | case "<": 552 | opStream.append(.SetLittleEndian) 553 | opStream.append(.UnsetAlign) 554 | case ">", "!": 555 | opStream.append(.SetBigEndian) 556 | opStream.append(.UnsetAlign) 557 | 558 | case " ": 559 | // Whitespace is allowed between formats. 560 | break 561 | 562 | default: 563 | // No control character found so set the repeat count to 1 564 | // and evaluate format characters. 565 | repeat = 1 566 | } 567 | } 568 | 569 | // If we have a repeat count we expect a format character. 570 | if repeat > 0 { 571 | // Add one op for each repeat count. 572 | for i in 0..repeat { 573 | switch c { 574 | case "x": opStream.append(.SkipByte) 575 | case "c": opStream.append(.PackChar) 576 | case "?": opStream.append(.PackBool) 577 | case "b": opStream.append(.PackInt8) 578 | case "B": opStream.append(.PackUInt8) 579 | case "h": opStream.append(.PackInt16) 580 | case "H": opStream.append(.PackUInt16) 581 | case "i", "l": opStream.append(.PackInt32) 582 | case "I", "L": opStream.append(.PackUInt32) 583 | case "q": opStream.append(.PackInt64) 584 | case "Q": opStream.append(.PackUInt64) 585 | case "f": opStream.append(.PackFloat) 586 | case "d": opStream.append(.PackDouble) 587 | case "s": opStream.append(.PackCString) 588 | case "p": opStream.append(.PackPString) 589 | case "P": opStream.append(.PackPointer) 590 | default: 591 | if error { 592 | error.memory = NSError(domain: ERROR_DOMAIN, 593 | code: Error.Parsing.toRaw(), 594 | userInfo: [NSLocalizedDescriptionKey: "bad character in format"]) 595 | } 596 | return false 597 | } 598 | } 599 | } 600 | // Reset the repeat counter. 601 | repeat = 0 602 | } 603 | opStream.append(.Stop) 604 | return true 605 | } 606 | } 607 | -------------------------------------------------------------------------------- /MVPCStruct/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | ${EXECUTABLE_NAME} 9 | CFBundleIdentifier 10 | se.automac.${PRODUCT_NAME:rfc1034identifier} 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | ${PRODUCT_NAME} 15 | CFBundlePackageType 16 | FMWK 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleSignature 20 | ???? 21 | CFBundleVersion 22 | ${CURRENT_PROJECT_VERSION} 23 | NSHumanReadableCopyright 24 | Copyright © 2014 AutoMac. All rights reserved. 25 | NSPrincipalClass 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /MVPCStruct/MVPCStruct.h: -------------------------------------------------------------------------------- 1 | // 2 | // MVPCStruct.h 3 | // MVPCStruct 4 | // 5 | // Created by Per Olofsson on 2014-06-13. 6 | // Copyright (c) 2014 AutoMac. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | //! Project version number for MVPCStruct. 12 | FOUNDATION_EXPORT double MVPCStructVersionNumber; 13 | 14 | //! Project version string for MVPCStruct. 15 | FOUNDATION_EXPORT const unsigned char MVPCStructVersionString[]; 16 | 17 | // In this header, you should import all the public headers of your framework using statements like #import 18 | 19 | 20 | -------------------------------------------------------------------------------- /MVPCStructTests/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | ${EXECUTABLE_NAME} 9 | CFBundleIdentifier 10 | se.automac.${PRODUCT_NAME:rfc1034identifier} 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | ${PRODUCT_NAME} 15 | CFBundlePackageType 16 | BNDL 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleSignature 20 | ???? 21 | CFBundleVersion 22 | 1 23 | 24 | 25 | -------------------------------------------------------------------------------- /MVPCStructTests/PackTests.swift: -------------------------------------------------------------------------------- 1 | // 2 | // PackTests.swift 3 | // MVPCStruct 4 | // 5 | // Created by Per Olofsson on 2014-06-13. 6 | // Copyright (c) 2014 AutoMac. All rights reserved. 7 | // 8 | 9 | import XCTest 10 | import MVPCStruct 11 | 12 | class PackTests: XCTestCase { 13 | 14 | override func setUp() { 15 | super.setUp() 16 | } 17 | 18 | override func tearDown() { 19 | super.tearDown() 20 | } 21 | 22 | func testHello() { 23 | let facit = "Hello".dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false) 24 | var error: NSError? 25 | 26 | let packer = CStruct() 27 | if let result = packer.pack(["H", "e", "l", "l", "o"], format: "ccccc", error: &error) { 28 | XCTAssertEqual(result, facit) 29 | } else { 30 | XCTFail("result is nil") 31 | } 32 | if let result = packer.pack(["H", "e", "l", "l", "o"], format: "5c", error: &error) { 33 | XCTAssertEqual(result, facit) 34 | } else { 35 | XCTFail("result is nil") 36 | } 37 | } 38 | 39 | func testInts() { 40 | var error: NSError? 41 | let signedFacit = NSData(bytes: [0xff, 0xfe, 0xff, 0xfd, 0xff, 0xff, 0xff, 0xfc, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff] as UInt8[], length: 15) 42 | let packer = CStruct() 43 | if let result = packer.pack([-1, -2, -3, -4], format: "HIQ", error: &error) { 92 | XCTAssertEqual(facit, result) 93 | } else { 94 | XCTFail("result is nil") 95 | } 96 | } 97 | 98 | func testBadFormat() { 99 | var error: NSError? 100 | 101 | let packer = CStruct() 102 | 103 | if let result = packer.pack([], format: "4@", error: &error) { 104 | XCTFail("bad format should return nil") 105 | } 106 | if let result = packer.pack([1], format:"1 i", error: &error) { 107 | XCTFail("bad format should return nil") 108 | } 109 | if let result = packer.pack([], format:"i", error: &error) { 110 | XCTFail("bad format should return nil") 111 | } 112 | if let result = packer.pack([1, 2], format:"i", error: &error) { 113 | XCTFail("bad format should return nil") 114 | } 115 | } 116 | 117 | } 118 | -------------------------------------------------------------------------------- /MVPCStructTests/UnpackTests.swift: -------------------------------------------------------------------------------- 1 | // 2 | // UnpackTests.swift 3 | // MVPCStruct 4 | // 5 | // Created by Per Olofsson on 2014-06-13. 6 | // Copyright (c) 2014 AutoMac. All rights reserved. 7 | // 8 | 9 | import XCTest 10 | import MVPCStruct 11 | 12 | class UnpackTests: XCTestCase { 13 | 14 | override func setUp() { 15 | super.setUp() 16 | } 17 | 18 | override func tearDown() { 19 | super.tearDown() 20 | } 21 | 22 | func testHello() { 23 | let helloData = "Hello".dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false) 24 | let facit = ["H", "e", "l", "l", "o"] 25 | var error: NSError? 26 | 27 | let packer = CStruct() 28 | if let result = packer.unpack(helloData, format: "ccccc", error: &error) as? String[] { 29 | for i in 0..facit.count { 30 | XCTAssertEqual(result[i], facit[i]) 31 | } 32 | } 33 | if let result = packer.unpack(helloData, format: "5c", error: &error) as? String[] { 34 | for i in 0..facit.count { 35 | XCTAssertEqual(result[i], facit[i]) 36 | } 37 | } 38 | } 39 | 40 | func testBigEndian() { 41 | var error: NSError? 42 | 43 | let data = NSData(bytes: [0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e] as UInt8[], length: 14) 44 | let facit: UInt[] = [0x0102, 0x03040506, 0x0708090a0b0c0d0e] 45 | 46 | let packer = CStruct() 47 | if let result = packer.unpack(data, format: ">HIQ", error: &error) as? UInt[] { 48 | for i in 0..facit.count { 49 | XCTAssertEqual(facit[i], result[i]) 50 | } 51 | } else { 52 | XCTFail("result is nil") 53 | } 54 | } 55 | 56 | } 57 | -------------------------------------------------------------------------------- /README.markdown: -------------------------------------------------------------------------------- 1 | # C struct handling for Swift 2 | 3 | Class for packing and unpacking C structs in Swift, modeled after the [struct](https://docs.python.org/2/library/struct.html) module in Python. 4 | 5 | ## Sample Code 6 | 7 | import MVPCStruct 8 | 9 | // Receiver expects a message with a header like this: 10 | // typedef struct { 11 | // uint16_t version; // Message format version, currently 0x0100. 12 | // uint16_t reserved; // Reserved for future use. 13 | // uint32_t length; // Length of data in bytes. 14 | // uint8_t data[]; // Binary encoded plist. 15 | // } __attribute__((packed)) mma_msg_t; 16 | func sendMessageHeader(msgData: NSData) -> Bool { 17 | var error: NSError? 18 | 19 | let version = 0x0100 20 | let reserved = 0 21 | 22 | let packer = CStruct(format: "=HHI") 23 | if let packedHeader = packer.pack([version, reserved, msgData.length], error: &error) { 24 | return 8 == send(socket_fd, packedHeader.bytes, packedHeader.length) 25 | } else { 26 | return false 27 | } 28 | } 29 | 30 | # Tasks 31 | 32 | ## Creating CStruct Objects 33 | 34 | ### - initWithFormat: 35 | 36 | init(format: String) 37 | 38 | ## Unpacking data 39 | 40 | ### - unpack:format:error: 41 | 42 | func unpack(data: NSData, format: String, error: NSErrorPointer) -> AnyObject[]? 43 | 44 | ### - unpack:error: 45 | 46 | func unpack(data: NSData, error: NSErrorPointer) -> AnyObject[]? 47 | 48 | ## Packing values 49 | 50 | ### - pack:format:error: 51 | 52 | func pack(values: AnyObject[], format: String, error: NSErrorPointer) -> NSData? 53 | 54 | ### - pack:error: 55 | 56 | func pack(values: AnyObject[], error: NSErrorPointer) -> NSData? { 57 | 58 | 59 | # Format strings 60 | 61 | ## Control characters 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 |
BYTE ORDERSIZEALIGNMENT
@nativenativenative
=nativestandardnone
<little-endianstandardnone
>big-endianstandardnone
!network (BE)standardnone
101 | 102 | ## Format characters 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | 140 | 141 | 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | 150 | 151 | 152 | 153 | 154 | 155 | 156 | 157 | 158 | 159 | 160 | 161 | 162 | 163 | 164 | 165 | 166 | 167 | 168 | 169 | 170 | 171 | 172 | 173 | 174 | 175 | 176 | 177 | 178 | 179 | 180 | 181 | 182 | 183 | 184 | 185 | 186 | 187 | 188 | 189 | 190 | 191 | 192 | 193 | 194 | 195 | 196 | 197 | 198 | 199 | 200 | 201 | 202 | 203 | 204 | 205 | 206 | 207 | 208 | 209 | 210 | 211 | 212 | 213 | 214 | 215 | 216 | 217 | 218 | 219 |
FORMATC TYPESWIFT TYPESIZE
xpad byteno value
ccharString of length 11
bsigned charInt1
Bunsigned charUInt1
?_BoolBool1
hshortInt2
Hunsigned shortUInt2
iintInt4
Iunsigned intUInt4
llongInt4
Lunsigned longUInt4
qlong longInt8
Qunsigned long longUInt8
ffloatFloat4
ddoubleDouble8
schar[]String
pchar[]String
Pvoid *UInt4/8
220 | --------------------------------------------------------------------------------