|
| 1 | +// Copyright 2013 The Flutter Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +#include "flutter/shell/platform/darwin/common/framework/Source/FlutterStandardCodecHelper.h" |
| 6 | +#include <stdint.h> |
| 7 | + |
| 8 | +void FlutterStandardCodecHelperReadAlignment(unsigned long* location, |
| 9 | + uint8_t alignment) { |
| 10 | + uint8_t mod = *location % alignment; |
| 11 | + if (mod) { |
| 12 | + *location += (alignment - mod); |
| 13 | + } |
| 14 | +} |
| 15 | + |
| 16 | +static uint8_t PeekByte(unsigned long location, CFDataRef data) { |
| 17 | + uint8_t result; |
| 18 | + CFRange range = CFRangeMake(location, 1); |
| 19 | + CFDataGetBytes(data, range, &result); |
| 20 | + return result; |
| 21 | +} |
| 22 | + |
| 23 | +void FlutterStandardCodecHelperReadBytes(unsigned long* location, |
| 24 | + unsigned long length, |
| 25 | + void* destination, |
| 26 | + CFDataRef data) { |
| 27 | + CFRange range = CFRangeMake(*location, length); |
| 28 | + CFDataGetBytes(data, range, destination); |
| 29 | + *location += length; |
| 30 | +} |
| 31 | + |
| 32 | +uint8_t FlutterStandardCodecHelperReadByte(unsigned long* location, |
| 33 | + CFDataRef data) { |
| 34 | + uint8_t value; |
| 35 | + FlutterStandardCodecHelperReadBytes(location, 1, &value, data); |
| 36 | + return value; |
| 37 | +} |
| 38 | + |
| 39 | +uint32_t FlutterStandardCodecHelperReadSize(unsigned long* location, |
| 40 | + CFDataRef data) { |
| 41 | + uint8_t byte = FlutterStandardCodecHelperReadByte(location, data); |
| 42 | + if (byte < 254) { |
| 43 | + return (uint32_t)byte; |
| 44 | + } else if (byte == 254) { |
| 45 | + UInt16 value; |
| 46 | + FlutterStandardCodecHelperReadBytes(location, 2, &value, data); |
| 47 | + return value; |
| 48 | + } else { |
| 49 | + UInt32 value; |
| 50 | + FlutterStandardCodecHelperReadBytes(location, 4, &value, data); |
| 51 | + return value; |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +static CFDataRef ReadDataNoCopy(unsigned long* location, |
| 56 | + unsigned long length, |
| 57 | + CFDataRef data) { |
| 58 | + CFDataRef result = CFDataCreateWithBytesNoCopy( |
| 59 | + kCFAllocatorDefault, CFDataGetBytePtr(data) + *location, length, |
| 60 | + kCFAllocatorNull); |
| 61 | + *location += length; |
| 62 | + return CFAutorelease(result); |
| 63 | +} |
| 64 | + |
| 65 | +CFStringRef FlutterStandardCodecHelperReadUTF8(unsigned long* location, |
| 66 | + CFDataRef data) { |
| 67 | + uint32_t size = FlutterStandardCodecHelperReadSize(location, data); |
| 68 | + CFDataRef bytes = ReadDataNoCopy(location, size, data); |
| 69 | + CFStringRef result = CFStringCreateFromExternalRepresentation( |
| 70 | + kCFAllocatorDefault, bytes, kCFStringEncodingUTF8); |
| 71 | + return CFAutorelease(result); |
| 72 | +} |
| 73 | + |
| 74 | +// Peeks ahead to see if we are reading a standard type. If so, recurse |
| 75 | +// directly to FlutterStandardCodecHelperReadValueOfType, otherwise recurse to |
| 76 | +// objc. |
| 77 | +static inline CFTypeRef FastReadValue( |
| 78 | + unsigned long* location, |
| 79 | + CFDataRef data, |
| 80 | + CFTypeRef (*ReadValue)(CFTypeRef), |
| 81 | + CFTypeRef (*ReadTypedDataOfType)(FlutterStandardField, CFTypeRef), |
| 82 | + CFTypeRef user_data) { |
| 83 | + uint8_t type = PeekByte(*location, data); |
| 84 | + if (FlutterStandardFieldIsStandardType(type)) { |
| 85 | + *location += 1; |
| 86 | + return FlutterStandardCodecHelperReadValueOfType( |
| 87 | + location, data, type, ReadValue, ReadTypedDataOfType, user_data); |
| 88 | + } else { |
| 89 | + return ReadValue(user_data); |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +CFTypeRef FlutterStandardCodecHelperReadValueOfType( |
| 94 | + unsigned long* location, |
| 95 | + CFDataRef data, |
| 96 | + uint8_t type, |
| 97 | + CFTypeRef (*ReadValue)(CFTypeRef), |
| 98 | + CFTypeRef (*ReadTypedDataOfType)(FlutterStandardField, CFTypeRef), |
| 99 | + CFTypeRef user_data) { |
| 100 | + FlutterStandardField field = (FlutterStandardField)type; |
| 101 | + switch (field) { |
| 102 | + case FlutterStandardFieldNil: |
| 103 | + return nil; |
| 104 | + case FlutterStandardFieldTrue: |
| 105 | + return kCFBooleanTrue; |
| 106 | + case FlutterStandardFieldFalse: |
| 107 | + return kCFBooleanFalse; |
| 108 | + case FlutterStandardFieldInt32: { |
| 109 | + int32_t value; |
| 110 | + FlutterStandardCodecHelperReadBytes(location, 4, &value, data); |
| 111 | + return CFAutorelease( |
| 112 | + CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &value)); |
| 113 | + } |
| 114 | + case FlutterStandardFieldInt64: { |
| 115 | + int64_t value; |
| 116 | + FlutterStandardCodecHelperReadBytes(location, 8, &value, data); |
| 117 | + return CFAutorelease( |
| 118 | + CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt64Type, &value)); |
| 119 | + } |
| 120 | + case FlutterStandardFieldFloat64: { |
| 121 | + Float64 value; |
| 122 | + FlutterStandardCodecHelperReadAlignment(location, 8); |
| 123 | + FlutterStandardCodecHelperReadBytes(location, 8, &value, data); |
| 124 | + return CFAutorelease( |
| 125 | + CFNumberCreate(kCFAllocatorDefault, kCFNumberDoubleType, &value)); |
| 126 | + } |
| 127 | + case FlutterStandardFieldIntHex: |
| 128 | + case FlutterStandardFieldString: |
| 129 | + return FlutterStandardCodecHelperReadUTF8(location, data); |
| 130 | + case FlutterStandardFieldUInt8Data: |
| 131 | + case FlutterStandardFieldInt32Data: |
| 132 | + case FlutterStandardFieldInt64Data: |
| 133 | + case FlutterStandardFieldFloat32Data: |
| 134 | + case FlutterStandardFieldFloat64Data: |
| 135 | + return ReadTypedDataOfType(field, user_data); |
| 136 | + case FlutterStandardFieldList: { |
| 137 | + UInt32 length = FlutterStandardCodecHelperReadSize(location, data); |
| 138 | + CFMutableArrayRef array = CFArrayCreateMutable( |
| 139 | + kCFAllocatorDefault, length, &kCFTypeArrayCallBacks); |
| 140 | + for (UInt32 i = 0; i < length; i++) { |
| 141 | + CFTypeRef value = FastReadValue(location, data, ReadValue, |
| 142 | + ReadTypedDataOfType, user_data); |
| 143 | + CFArrayAppendValue(array, (value == nil ? kCFNull : value)); |
| 144 | + } |
| 145 | + return CFAutorelease(array); |
| 146 | + } |
| 147 | + case FlutterStandardFieldMap: { |
| 148 | + UInt32 size = FlutterStandardCodecHelperReadSize(location, data); |
| 149 | + CFMutableDictionaryRef dict = CFDictionaryCreateMutable( |
| 150 | + kCFAllocatorDefault, size, &kCFTypeDictionaryKeyCallBacks, |
| 151 | + &kCFTypeDictionaryValueCallBacks); |
| 152 | + for (UInt32 i = 0; i < size; i++) { |
| 153 | + CFTypeRef key = FastReadValue(location, data, ReadValue, |
| 154 | + ReadTypedDataOfType, user_data); |
| 155 | + CFTypeRef val = FastReadValue(location, data, ReadValue, |
| 156 | + ReadTypedDataOfType, user_data); |
| 157 | + CFDictionaryAddValue(dict, (key == nil ? kCFNull : key), |
| 158 | + (val == nil ? kCFNull : val)); |
| 159 | + } |
| 160 | + return CFAutorelease(dict); |
| 161 | + } |
| 162 | + default: |
| 163 | + // Malformed message. |
| 164 | + assert(false); |
| 165 | + } |
| 166 | +} |
0 commit comments