Skip to content

Commit b001565

Browse files
committed
Replace GoogleToolboxForMac with a GoogleUtilities subspec
1 parent f40da9a commit b001565

File tree

9 files changed

+1055
-962
lines changed

9 files changed

+1055
-962
lines changed

Example/Core/Tests/FIRNetworkTest.m

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616

1717
#import "GTMHTTPServer.h"
1818

19-
#import <GoogleToolboxForMac/GTMNSData+zlib.h>
2019
#import <GoogleUtilities/GULNetwork.h>
20+
#import <GoogleUtilities/GULNSData+zlib.h>
2121
#import <GoogleUtilities/GULReachabilityChecker.h>
2222

2323
@interface GULNetwork ()
@@ -119,6 +119,7 @@ - (void)testSessionNetwork_POST_foreground {
119119
XCTestExpectation *expectation = [self expectationWithDescription:@"Expect block is called"];
120120

121121
NSData *uncompressedData = [@"Google" dataUsingEncoding:NSUTF8StringEncoding];
122+
122123
NSURL *url =
123124
[NSURL URLWithString:[NSString stringWithFormat:@"http://localhost:%d/2", _httpServer.port]];
124125
_statusCode = 200;
@@ -946,7 +947,7 @@ - (void)verifyRequest {
946947

947948
// Test whether the request is compressed correctly.
948949
NSData *requestBody = [_request body];
949-
NSData *decompressedRequestData = [NSData gtm_dataByInflatingData:requestBody error:NULL];
950+
NSData *decompressedRequestData = [NSData gul_dataByInflatingGzippedData:requestBody error:NULL];
950951
NSString *requestString =
951952
[[NSString alloc] initWithData:decompressedRequestData encoding:NSUTF8StringEncoding];
952953
XCTAssertEqualObjects(requestString, @"Google", @"Request is not compressed correctly.");

Example/Firebase.xcodeproj/project.pbxproj

Lines changed: 767 additions & 838 deletions
Large diffs are not rendered by default.

Example/Messaging/Tests/FIRMessagingConnectionTest.m

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
#import <OCMock/OCMock.h>
2020

2121
#import "Protos/GtalkCore.pbobjc.h"
22-
#import <GoogleToolboxForMac/GTMDefines.h>
2322

2423
#import "FIRMessagingClient.h"
2524
#import "FIRMessagingConnection.h"
@@ -374,7 +373,7 @@ - (void)_disconnectSocket {
374373
}
375374

376375
- (void)_sendData:(NSData *)data withTag:(int8_t)tag rmqId:(NSString *)rmqId {
377-
_GTMDevLog(@"FIRMessaging Socket: Send data with Tag: %d rmq: %@", tag, rmqId);
376+
NSLog(@"FIRMessaging Socket: Send data with Tag: %d rmq: %@", tag, rmqId);
378377
if (currentProtoSendTag > 0) {
379378
XCTAssertEqual(tag, currentProtoSendTag);
380379
}

Example/Messaging/Tests/FIRMessagingFakeSocket.m

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
#import "FIRMessagingConstants.h"
2020
#import "FIRMessagingDefines.h"
21-
#import <GoogleToolboxForMac/GTMDefines.h>
2221

2322
@interface FIRMessagingSecureSocket() <NSStreamDelegate>
2423

@@ -78,7 +77,7 @@ - (void)connectToHost:(NSString *)host
7877
}
7978

8079
- (void)openStream:(NSStream *)stream {
81-
_GTMDevAssert(stream, @"Cannot open nil stream");
80+
NSAssert(stream, @"Cannot open nil stream");
8281
if (stream) {
8382
stream.delegate = self;
8483
[stream scheduleInRunLoop:self.runLoop forMode:NSDefaultRunLoopMode];
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Copyright 2018 Google
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#import <Foundation/Foundation.h>
16+
17+
/// This is a copy of Google Toolbox for Mac library to avoid creating an extra framework.
18+
19+
// NOTE: For 64bit, none of these apis handle input sizes >32bits, they will return nil when given
20+
// such data. To handle data of that size you really should be streaming it rather then doing it all
21+
// in memory.
22+
23+
@interface NSData (GULGzip)
24+
25+
/// Returns an data as the result of decompressing the payload of |data|.The data to decompress must
26+
/// be a gzipped payloads.
27+
+ (NSData *)gul_dataByInflatingGzippedData:(NSData *)data error:(NSError **)error;
28+
29+
/// Returns an compressed data with the result of gzipping the payload of |data|. Uses the default
30+
/// compression level.
31+
+ (NSData *)gul_dataByGzippingData:(NSData *)data error:(NSError **)error;
32+
33+
FOUNDATION_EXPORT NSString *const GULNSDataZlibErrorDomain;
34+
FOUNDATION_EXPORT NSString *const GULNSDataZlibErrorKey; // NSNumber
35+
FOUNDATION_EXPORT NSString *const GULNSDataZlibRemainingBytesKey; // NSNumber
36+
37+
typedef NS_ENUM(NSInteger, GULNSDataZlibError) {
38+
GULNSDataZlibErrorGreaterThan32BitsToCompress = 1024,
39+
// An internal zlib error.
40+
// GULNSDataZlibErrorKey will contain the error value.
41+
// NSLocalizedDescriptionKey may contain an error string from zlib.
42+
// Look in zlib.h for list of errors.
43+
GULNSDataZlibErrorInternal,
44+
// There was left over data in the buffer that was not used.
45+
// GULNSDataZlibRemainingBytesKey will contain number of remaining bytes.
46+
GULNSDataZlibErrorDataRemaining
47+
};
48+
49+
@end
Lines changed: 207 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,207 @@
1+
// Copyright 2018 Google
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#import "GULNSData+zlib.h"
16+
17+
#import <zlib.h>
18+
19+
#define kChunkSize 1024
20+
#define Z_DEFAULT_COMPRESSION (-1)
21+
22+
NSString *const GULNSDataZlibErrorDomain = @"com.google.GULNSDataZlibErrorDomain";
23+
NSString *const GULNSDataZlibErrorKey = @"GULNSDataZlibErrorKey";
24+
NSString *const GULNSDataZlibRemainingBytesKey = @"GULNSDataZlibRemainingBytesKey";
25+
26+
@implementation NSData (GULGzip)
27+
28+
+ (NSData *)gul_dataByInflatingGzippedData:(NSData *)data error:(NSError **)error {
29+
const void *bytes = [data bytes];
30+
NSUInteger length = [data length];
31+
if (!bytes || !length) {
32+
return nil;
33+
}
34+
35+
#if defined(__LP64__) && __LP64__
36+
// Don't support > 32bit length for 64 bit, see note in header.
37+
if (length > UINT_MAX) {
38+
return nil;
39+
}
40+
#endif
41+
42+
z_stream strm;
43+
bzero(&strm, sizeof(z_stream));
44+
45+
// Setup the input.
46+
strm.avail_in = (unsigned int)length;
47+
strm.next_in = (unsigned char *)bytes;
48+
49+
int windowBits = 15; // 15 to enable any window size
50+
windowBits += 32; // and +32 to enable zlib or gzip header detection.
51+
52+
int retCode;
53+
if ((retCode = inflateInit2(&strm, windowBits)) != Z_OK) {
54+
if (error) {
55+
NSDictionary *userInfo = [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:retCode]
56+
forKey:GULNSDataZlibErrorKey];
57+
*error = [NSError errorWithDomain:GULNSDataZlibErrorDomain
58+
code:GULNSDataZlibErrorInternal
59+
userInfo:userInfo];
60+
}
61+
return nil;
62+
}
63+
64+
// Hint the size at 4x the input size.
65+
NSMutableData *result = [NSMutableData dataWithCapacity:(length * 4)];
66+
unsigned char output[kChunkSize];
67+
68+
// Loop to collect the data.
69+
do {
70+
// Update what we're passing in.
71+
strm.avail_out = kChunkSize;
72+
strm.next_out = output;
73+
retCode = inflate(&strm, Z_NO_FLUSH);
74+
if ((retCode != Z_OK) && (retCode != Z_STREAM_END)) {
75+
if (error) {
76+
NSMutableDictionary *userInfo =
77+
[NSMutableDictionary dictionaryWithObject:[NSNumber numberWithInt:retCode]
78+
forKey:GULNSDataZlibErrorKey];
79+
if (strm.msg) {
80+
NSString *message = [NSString stringWithUTF8String:strm.msg];
81+
if (message) {
82+
[userInfo setObject:message forKey:NSLocalizedDescriptionKey];
83+
}
84+
}
85+
*error = [NSError errorWithDomain:GULNSDataZlibErrorDomain
86+
code:GULNSDataZlibErrorInternal
87+
userInfo:userInfo];
88+
}
89+
inflateEnd(&strm);
90+
return nil;
91+
}
92+
// Collect what we got.
93+
unsigned gotBack = kChunkSize - strm.avail_out;
94+
if (gotBack > 0) {
95+
[result appendBytes:output length:gotBack];
96+
}
97+
98+
} while (retCode == Z_OK);
99+
100+
// Make sure there wasn't more data tacked onto the end of a valid compressed stream.
101+
if (strm.avail_in != 0) {
102+
if (error) {
103+
NSDictionary *userInfo =
104+
[NSDictionary dictionaryWithObject:[NSNumber numberWithUnsignedInt:strm.avail_in]
105+
forKey:GULNSDataZlibRemainingBytesKey];
106+
*error = [NSError errorWithDomain:GULNSDataZlibErrorDomain
107+
code:GULNSDataZlibErrorDataRemaining
108+
userInfo:userInfo];
109+
}
110+
result = nil;
111+
}
112+
// The only way out of the loop was by hitting the end of the stream.
113+
NSAssert(retCode == Z_STREAM_END,
114+
@"Thought we finished inflate w/o getting a result of stream end, code %d", retCode);
115+
116+
// Clean up.
117+
inflateEnd(&strm);
118+
119+
return result;
120+
}
121+
122+
+ (NSData *)gul_dataByGzippingData:(NSData *)data error:(NSError **)error {
123+
const void *bytes = [data bytes];
124+
NSUInteger length = [data length];
125+
126+
int level = Z_DEFAULT_COMPRESSION;
127+
if (!bytes || !length) {
128+
return nil;
129+
}
130+
131+
#if defined(__LP64__) && __LP64__
132+
// Don't support > 32bit length for 64 bit, see note in header.
133+
if (length > UINT_MAX) {
134+
if (error) {
135+
*error = [NSError errorWithDomain:GULNSDataZlibErrorDomain
136+
code:GULNSDataZlibErrorGreaterThan32BitsToCompress
137+
userInfo:nil];
138+
}
139+
return nil;
140+
}
141+
#endif
142+
143+
z_stream strm;
144+
bzero(&strm, sizeof(z_stream));
145+
146+
int memLevel = 8; // Default.
147+
int windowBits = 15 + 16; // Enable gzip header instead of zlib header.
148+
149+
int retCode;
150+
if ((retCode = deflateInit2(&strm, level, Z_DEFLATED, windowBits, memLevel,
151+
Z_DEFAULT_STRATEGY)) != Z_OK) {
152+
if (error) {
153+
NSDictionary *userInfo = [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:retCode]
154+
forKey:GULNSDataZlibErrorKey];
155+
*error = [NSError errorWithDomain:GULNSDataZlibErrorDomain
156+
code:GULNSDataZlibErrorInternal
157+
userInfo:userInfo];
158+
}
159+
return nil;
160+
}
161+
162+
// Hint the size at 1/4 the input size.
163+
NSMutableData *result = [NSMutableData dataWithCapacity:(length / 4)];
164+
unsigned char output[kChunkSize];
165+
166+
// Setup the input.
167+
strm.avail_in = (unsigned int)length;
168+
strm.next_in = (unsigned char *)bytes;
169+
170+
// Collect the data.
171+
do {
172+
// update what we're passing in
173+
strm.avail_out = kChunkSize;
174+
strm.next_out = output;
175+
retCode = deflate(&strm, Z_FINISH);
176+
if ((retCode != Z_OK) && (retCode != Z_STREAM_END)) {
177+
if (error) {
178+
NSDictionary *userInfo = [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:retCode]
179+
forKey:GULNSDataZlibErrorKey];
180+
*error = [NSError errorWithDomain:GULNSDataZlibErrorDomain
181+
code:GULNSDataZlibErrorInternal
182+
userInfo:userInfo];
183+
}
184+
deflateEnd(&strm);
185+
return nil;
186+
}
187+
// Collect what we got.
188+
unsigned gotBack = kChunkSize - strm.avail_out;
189+
if (gotBack > 0) {
190+
[result appendBytes:output length:gotBack];
191+
}
192+
193+
} while (retCode == Z_OK);
194+
195+
// If the loop exits, it used all input and the stream ended.
196+
NSAssert(strm.avail_in == 0,
197+
@"Should have finished deflating without using all input, %u bytes left", strm.avail_in);
198+
NSAssert(retCode == Z_STREAM_END,
199+
@"thought we finished deflate w/o getting a result of stream end, code %d", retCode);
200+
201+
// Clean up.
202+
deflateEnd(&strm);
203+
204+
return result;
205+
}
206+
207+
@end

Firebase/Utilities/Network/GULNetwork.m

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,10 @@
1616
#import "Private/GULNetworkMessageCode.h"
1717

1818
#import <GoogleUtilities/GULLogger.h>
19+
#import <GoogleUtilities/GULNSData+zlib.h>
20+
#import <GoogleUtilities/GULReachabilityChecker.h>
1921
#import "Private/GULMutableDictionary.h"
2022
#import "Private/GULNetworkConstants.h"
21-
#import "Private/GULReachabilityChecker.h"
22-
23-
#import <GoogleToolboxForMac/GTMNSData+zlib.h>
2423

2524
/// Constant string for request header Content-Encoding.
2625
static NSString *const kGULNetworkContentCompressionKey = @"Content-Encoding";
@@ -66,7 +65,6 @@ - (instancetype)initWithReachabilityHost:(NSString *)reachabilityHost {
6665
if (self) {
6766
// Setup reachability.
6867
_reachability = [[GULReachabilityChecker alloc] initWithReachabilityDelegate:self
69-
loggerDelegate:self
7068
withHost:reachabilityHost];
7169
if (![_reachability start]) {
7270
return nil;
@@ -116,7 +114,7 @@ - (NSString *)postURL:(NSURL *)url
116114
}
117115

118116
NSError *compressError = nil;
119-
NSData *compressedData = [NSData gtm_dataByGzippingData:payload error:&compressError];
117+
NSData *compressedData = [NSData gul_dataByGzippingData:payload error:&compressError];
120118
if (!compressedData || compressError) {
121119
if (compressError || payload.length > 0) {
122120
// If the payload is not empty but it fails to compress the payload, something has been wrong.

0 commit comments

Comments
 (0)