Skip to content

Commit 0ecd60a

Browse files
committed
Fix endianness issues in Foundation
1 parent bd65071 commit 0ecd60a

File tree

2 files changed

+31
-19
lines changed

2 files changed

+31
-19
lines changed

CoreFoundation/Base.subproj/CFInternal.h

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,13 @@ CF_PRIVATE Boolean __CFProcessIsRestricted();
352352
CF_EXPORT void * __CFConstantStringClassReferencePtr;
353353
CF_EXPORT void *__CFConstantStringClassReference[];
354354

355+
#if __CF_BIG_ENDIAN__
356+
#define CFINFO {0x00, 0x00, 0x07, 0xc8}
357+
#elif __CF_LITTLE_ENDIAN__
358+
#define CFINFO {0xc8, 0x07, 0x00, 0x00}
359+
#endif
360+
361+
355362
#ifdef __CONSTANT_CFSTRINGS__
356363

357364
#if DEPLOYMENT_RUNTIME_SWIFT
@@ -362,59 +369,53 @@ CF_EXPORT void *__CFConstantStringClassReference[];
362369
#define CONST_STRING_SECTION
363370
#endif
364371

372+
365373
// TODO: Pinned retain count for constants?
366374
#define CONST_STRING_DECL(S, V) \
367-
const struct __CFConstStr __##S CONST_STRING_SECTION = {{(uintptr_t)&__CFConstantStringClassReference, _CF_CONSTANT_OBJECT_STRONG_RC, 0, {0xc8, 0x07, 0x00, 0x00}}, (uint8_t *)(V), sizeof(V) - 1}; \
375+
const struct __CFConstStr __##S CONST_STRING_SECTION = {{(uintptr_t)&__CFConstantStringClassReference, _CF_CONSTANT_OBJECT_STRONG_RC, 0, CFINFO}, (uint8_t *)(V), sizeof(V) - 1}; \
368376
const CFStringRef S = (CFStringRef)&__##S;
369377

370378
#define PE_CONST_STRING_DECL(S, V) \
371-
const static struct __CFConstStr __##S CONST_STRING_SECTION = {{(uintptr_t)&__CFConstantStringClassReference, _CF_CONSTANT_OBJECT_STRONG_RC, 0, {0xc8, 0x07, 0x00, 0x00}}, (uint8_t *)(V), sizeof(V) - 1}; \
379+
const static struct __CFConstStr __##S CONST_STRING_SECTION = {{(uintptr_t)&__CFConstantStringClassReference, _CF_CONSTANT_OBJECT_STRONG_RC, 0, CFINFO}, (uint8_t *)(V), sizeof(V) - 1}; \
372380
CF_PRIVATE const CFStringRef S = (CFStringRef)&__##S;
373381

382+
374383
#else
375384

376385
#define CONST_STRING_DECL(S, V) \
377-
const struct __CFConstStr __##S = {{(uintptr_t)&__CFConstantStringClassReference, _CFSWIFT_RC_INIT {0xc8, 0x07, 0x00, 0x00}}, (uint8_t *)(V), sizeof(V) - 1}; \
386+
const struct __CFConstStr __##S = {{(uintptr_t)&__CFConstantStringClassReference, _CFSWIFT_RC_INIT CFINFO}, (uint8_t *)(V), sizeof(V) - 1}; \
378387
const CFStringRef S = (CFStringRef)&__##S;
379388

380389
#define PE_CONST_STRING_DECL(S, V) \
381-
const static struct __CFConstStr __##S = {{(uintptr_t)&__CFConstantStringClassReference, _CFSWIFT_RC_INIT {0xc8, 0x07, 0x00, 0x00}}, (uint8_t *)(V), sizeof(V) - 1}; \
390+
const static struct __CFConstStr __##S = {{(uintptr_t)&__CFConstantStringClassReference, _CFSWIFT_RC_INIT CFINFO}, (uint8_t *)(V), sizeof(V) - 1}; \
382391
CF_PRIVATE const CFStringRef S = (CFStringRef)&__##S;
383392

393+
384394
#endif
385395

386396
#else
387397

388398
struct CF_CONST_STRING {
389399
CFRuntimeBase _base;
390400
uint8_t *_ptr;
401+
#if defined(__LP64__) && defined(__BIG_ENDIAN__)
402+
uint64_t _length;
403+
#else
391404
uint32_t _length;
405+
#endif
392406
};
393407

394408
CF_EXPORT int __CFConstantStringClassReference[];
395409

396410
/* CFNetwork also has a copy of the CONST_STRING_DECL macro (for use on platforms without constant string support in cc); please warn cfnetwork-core@group.apple.com of any necessary changes to this macro. -- REW, 1/28/2002 */
397411

398-
#if __CF_BIG_ENDIAN__
399-
400-
#define CONST_STRING_DECL(S, V) \
401-
static struct CF_CONST_STRING __ ## S ## __ = {{(uintptr_t)&__CFConstantStringClassReference, {0x00, 0x00, 0x07, 0xc8}}, (uint8_t *)V, sizeof(V) - 1}; \
402-
const CFStringRef S = (CFStringRef) & __ ## S ## __;
403-
#define PE_CONST_STRING_DECL(S, V) \
404-
static struct CF_CONST_STRING __ ## S ## __ = {{(uintptr_t)&__CFConstantStringClassReference, {0x00, 0x00, 0x07, 0xc8}}, (uint8_t *)V, sizeof(V) - 1}; \
405-
CF_PRIVATE const CFStringRef S = (CFStringRef) & __ ## S ## __;
406-
407-
#elif __CF_LITTLE_ENDIAN__
408-
409412
#define CONST_STRING_DECL(S, V) \
410-
static struct CF_CONST_STRING __ ## S ## __ = {{(uintptr_t)&__CFConstantStringClassReference, {0xc8, 0x07, 0x00, 0x00}}, (uint8_t *)(V), sizeof(V) - 1}; \
413+
static struct CF_CONST_STRING __ ## S ## __ = {{(uintptr_t)&__CFConstantStringClassReference, CFINFO}, (uint8_t *)(V), sizeof(V) - 1}; \
411414
const CFStringRef S = (CFStringRef) & __ ## S ## __;
412415
#define PE_CONST_STRING_DECL(S, V) \
413-
static struct CF_CONST_STRING __ ## S ## __ = {{(uintptr_t)&__CFConstantStringClassReference, {0xc8, 0x07, 0x00, 0x00}}, (uint8_t *)(V), sizeof(V) - 1}; \
416+
static struct CF_CONST_STRING __ ## S ## __ = {{(uintptr_t)&__CFConstantStringClassReference, CFINFO}, (uint8_t *)(V), sizeof(V) - 1}; \
414417
CF_PRIVATE const CFStringRef S = (CFStringRef) & __ ## S ## __;
415418

416-
#endif
417-
418419
#endif // __CONSTANT_CFSTRINGS__
419420

420421
CF_EXPORT bool __CFOASafe;

CoreFoundation/String.subproj/CFString.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,11 @@ struct __CFConstStr {
165165
uint8_t _pad[4];
166166
} _base;
167167
uint8_t *_ptr;
168+
#if defined(__LP64__) && defined(__BIG_ENDIAN__)
169+
uint64_t _length;
170+
#else
168171
uint32_t _length;
172+
#endif
169173
};
170174

171175
#if DEPLOYMENT_TARGET_LINUX
@@ -174,10 +178,17 @@ struct __CFConstStr {
174178
#define CONST_STRING_LITERAL_SECTION
175179
#endif
176180

181+
#if __BIG_ENDIAN__
182+
#define CFSTR(cStr) ({ \
183+
static struct __CFConstStr str CONST_STRING_LITERAL_SECTION = {{(uintptr_t)&__CFConstantStringClassReference, _CF_CONSTANT_OBJECT_STRONG_RC, 0, {0x00, 0x00, 0x07, 0xc8}, {0x00, 0x00, 0x00, 0x00}}, (uint8_t *)(cStr), sizeof(cStr) - 1}; \
184+
(CFStringRef)&str; \
185+
})
186+
#else
177187
#define CFSTR(cStr) ({ \
178188
static struct __CFConstStr str CONST_STRING_LITERAL_SECTION = {{(uintptr_t)&__CFConstantStringClassReference, _CF_CONSTANT_OBJECT_STRONG_RC, 0, {0xc8, 0x07, 0x00, 0x00}, {0x00, 0x00, 0x00, 0x00}}, (uint8_t *)(cStr), sizeof(cStr) - 1}; \
179189
(CFStringRef)&str; \
180190
})
191+
#endif
181192

182193
#else
183194

0 commit comments

Comments
 (0)