Skip to content

Commit

Permalink
Apply base85 encoding and decoding
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 731877970
  • Loading branch information
ggli-google authored and copybara-github committed Feb 27, 2025
1 parent 528c714 commit fe6ad71
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#import "internal/platform/implementation/apple/Mediums/BLEv2/GNCBLEError.h"
#import "internal/platform/implementation/apple/Mediums/BLEv2/GNCBLEGATTCharacteristic.h"
#import "internal/platform/implementation/apple/Mediums/BLEv2/GNCPeripheralManager.h"
#import "internal/platform/implementation/apple/Mediums/BLEv2/NSData+GNCBase85.h"
#import "internal/platform/implementation/apple/Mediums/BLEv2/NSData+GNCWebSafeBase64.h"
#import "GoogleToolboxForMac/GTMLogger.h"

Expand Down Expand Up @@ -203,16 +204,27 @@ - (void)startAdvertisingData:(NSDictionary<CBUUID *, NSData *> *)serviceData
// data is unavailable.
CBUUID *serviceUUID = [serviceData.allKeys objectAtIndex:0];
NSData *value = [serviceData objectForKey:serviceUUID];
#if defined(NC_IOS_SDK)
NSString *encoded = [value base85EncodedString];
#else
NSString *encoded = [value webSafeBase64EncodedString];
#endif // defined(NC_IOS_SDK)

// Apple only "guarantees" (best effort) 28 bytes of advertisement data. Base64 encoding
// increases the size of the original data so we must truncate it to ensure it still meets the
// 28 byte limit. Since we have a 2-byte service UUID and the header for local name and service
// UUID is 2 bytes each, that leaves us with a maximum of 22 bytes for the local name. However,
// it seems in practice we can only reliably advertise a 20-byte local name on iOS.
#if defined(NC_IOS_SDK)
// DCT is using 22 bytes as length limit.
if (encoded.length > 22) {
encoded = [encoded substringToIndex:22];
}
#else
if (encoded.length > 20) {
encoded = [encoded substringToIndex:20];
}
#endif // defined(NC_IOS_SDK)

_advertisementData = @{
CBAdvertisementDataLocalNameKey : encoded,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#import "internal/platform/implementation/apple/Mediums/BLEv2/GNCBLEGATTServer.h"
#import "internal/platform/implementation/apple/Mediums/BLEv2/GNCCentralManager.h"
#import "internal/platform/implementation/apple/Mediums/BLEv2/GNCPeripheral.h"
#import "internal/platform/implementation/apple/Mediums/BLEv2/NSData+GNCBase85.h"
#import "internal/platform/implementation/apple/Mediums/BLEv2/NSData+GNCWebSafeBase64.h"

NS_ASSUME_NONNULL_BEGIN
Expand Down Expand Up @@ -209,7 +210,11 @@ - (void)internalStartScanningIfPoweredOn {
if (!localName) {
return @{};
}
#if defined(NC_IOS_SDK)
NSData *data = [[NSData alloc] initWithBase85EncodedString:localName];
#else
NSData *data = [[NSData alloc] initWithWebSafeBase64EncodedString:localName];
#endif // defined(NC_IOS_SDK)

// A Nearby Apple advertisement should only have a single service, so simply grab the first one if
// it exists.
Expand Down

0 comments on commit fe6ad71

Please sign in to comment.