5 // Created by Mitch Adler on 6/18/12.
6 // Copyright (c) 2012 Apple Inc. All rights reserved.
11 #include "utilities/SecCFRelease.h"
12 #include "utilities/der_plist.h"
13 #include "utilities/der_plist_internal.h"
15 #include <corecrypto/ccder.h>
16 #include <CoreFoundation/CoreFoundation.h>
19 const uint8_t* der_decode_string(CFAllocatorRef allocator
, CFOptionFlags mutability
,
20 CFStringRef
* string
, CFErrorRef
*error
,
21 const uint8_t* der
, const uint8_t *der_end
)
26 size_t payload_size
= 0;
27 const uint8_t *payload
= ccder_decode_tl(CCDER_UTF8_STRING
, &payload_size
, der
, der_end
);
29 if (NULL
== payload
|| (der_end
- payload
) < payload_size
){
30 SecCFDERCreateError(kSecDERErrorUnknownEncoding
, CFSTR("Unknown string encoding"), NULL
, error
);
34 *string
= CFStringCreateWithBytes(allocator
, payload
, payload_size
, kCFStringEncodingUTF8
, false);
36 if (NULL
== *string
) {
37 SecCFDERCreateError(kSecDERErrorAllocationFailure
, CFSTR("String allocation failed"), NULL
, error
);
41 return payload
+ payload_size
;
45 size_t der_sizeof_string(CFStringRef str
, CFErrorRef
*error
)
47 const CFIndex str_length
= CFStringGetLength(str
);
48 const CFIndex maximum
= CFStringGetMaximumSizeForEncoding(str_length
, kCFStringEncodingUTF8
);
50 CFIndex encodedLen
= 0;
51 CFIndex converted
= CFStringGetBytes(str
, CFRangeMake(0, str_length
), kCFStringEncodingUTF8
, 0, false, NULL
, maximum
, &encodedLen
);
53 return ccder_sizeof(CCDER_UTF8_STRING
, (converted
== str_length
) ? encodedLen
: 0);
57 uint8_t* der_encode_string(CFStringRef string
, CFErrorRef
*error
,
58 const uint8_t *der
, uint8_t *der_end
)
60 // Obey the NULL allowed rules.
64 const CFIndex str_length
= CFStringGetLength(string
);
66 ptrdiff_t der_space
= der_end
- der
;
67 CFIndex bytes_used
= 0;
68 uint8_t *buffer
= der_end
- der_space
;
69 CFIndex converted
= CFStringGetBytes(string
, CFRangeMake(0, str_length
), kCFStringEncodingUTF8
, 0, false, buffer
, der_space
, &bytes_used
);
70 if (converted
!= str_length
){
71 SecCFDERCreateError(kSecDERErrorUnderlyingError
, CFSTR("String extraction failed"), NULL
, error
);
75 return ccder_encode_tl(CCDER_UTF8_STRING
, bytes_used
, der
,
76 ccder_encode_body(bytes_used
, buffer
, der
, der_end
));