aboutsummaryrefslogtreecommitdiffhomepage
path: root/Firebase/Database/Utilities/FUtilities.m
blob: befe87461e2c7150166c8a6629e031e0d5ed95a8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
/*
 * Copyright 2017 Google
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#import "FIRLogger.h"
#import "FUtilities.h"
#import "FStringUtilities.h"
#import "FConstants.h"
#import "FAtomicNumber.h"

#define ARC4RANDOM_MAX 0x100000000
#define INTEGER_32_MIN (-2147483648)
#define INTEGER_32_MAX 2147483647

#pragma mark -
#pragma mark C functions

static FLogLevel logLevel = FLogLevelInfo; // Default log level is info
static NSMutableDictionary* options = nil;

BOOL FFIsLoggingEnabled(FLogLevel level) {
    return level >= logLevel;
}

void firebaseJobsTroll(void) {
    FFLog(@"I-RDB095001", @"password super secret; JFK conspiracy; Hello there! Having fun digging through Firebase? We're always hiring! jobs@firebase.com");
}

#pragma mark -
#pragma mark Private property and singleton specification

@interface FUtilities() {

}

@property (nonatomic, strong) FAtomicNumber* localUid;

+ (FUtilities*)singleton;

@end

@implementation FUtilities

@synthesize localUid;

- (id)init
{
    self = [super init];
    if (self) {
        self.localUid = [[FAtomicNumber alloc] init];
    }
    return self;
}

// TODO: We really want to be able to set the log level
+ (void) setLoggingEnabled:(BOOL)enabled {
    logLevel = enabled ? FLogLevelDebug : FLogLevelInfo;
}

+ (BOOL) getLoggingEnabled {
    return logLevel == FLogLevelDebug;
}

+ (FUtilities*) singleton
{
    static dispatch_once_t pred = 0;
    __strong static id _sharedObject = nil;
    dispatch_once(&pred, ^{
        _sharedObject = [[self alloc] init]; // or some other init method
    });
    return _sharedObject;
}

// Refactor as a category of NSString
+ (NSArray *) splitString:(NSString *) str intoMaxSize:(const unsigned int) size {
    if(str.length <= size) {
        return [NSArray arrayWithObject:str];
    }

    NSMutableArray* dataSegs = [[NSMutableArray alloc] init];
    for(int c = 0; c < str.length; c += size) {
        if (c + size > str.length) {
            int rangeStart = c;
            unsigned long rangeLength = size - ((c + size) - str.length);
            [dataSegs addObject:[str substringWithRange:NSMakeRange(rangeStart, rangeLength)]];
        }
        else {
            int rangeStart = c;
            int rangeLength = size;
            [dataSegs addObject:[str substringWithRange:NSMakeRange(rangeStart, rangeLength)]];
        }
    }
    return dataSegs;
}

+ (NSNumber *) LUIDGenerator {
    FUtilities* f = [FUtilities singleton];
    return [f.localUid getAndIncrement];
}

+ (NSString *) decodePath:(NSString *)pathString {
    NSMutableArray* decodedPieces = [[NSMutableArray alloc] init];
    NSArray* pieces = [pathString componentsSeparatedByString:@"/"];
    for (NSString* piece in pieces) {
        if (piece.length > 0) {
            [decodedPieces addObject:[FStringUtilities urlDecoded:piece]];
        }
    }
    return [NSString stringWithFormat:@"/%@", [decodedPieces componentsJoinedByString:@"/"]];
}

+ (FParsedUrl *) parseUrl:(NSString *)url {
    NSString* original = url;
    //NSURL* n = [[NSURL alloc] initWithString:url]

    NSString* host;
    NSString* namespace;
    bool secure;

    NSString* scheme = nil;
    FPath* path = nil;
    NSRange colonIndex = [url rangeOfString:@"//"];
    if (colonIndex.location != NSNotFound) {
        scheme = [url substringToIndex:colonIndex.location - 1];
        url = [url substringFromIndex:colonIndex.location + 2];
    }
    NSInteger slashIndex = [url rangeOfString:@"/"].location;
    if (slashIndex == NSNotFound) {
        slashIndex = url.length;
    }

    host = [[url substringToIndex:slashIndex] lowercaseString];
    if (slashIndex >= url.length) {
        url = @"";
    } else {
        url = [url substringFromIndex:slashIndex + 1];
    }

    NSArray *parts = [host componentsSeparatedByString:@"."];
    if([parts count] == 3) {
        NSInteger colonIndex = [[parts objectAtIndex:2] rangeOfString:@":"].location;
        if (colonIndex != NSNotFound) {
            // we have a port, use the provided scheme
            secure = [scheme isEqualToString:@"https"];
        } else {
            secure = YES;
        }

        namespace = [[parts objectAtIndex:0] lowercaseString];
        NSString* pathString = [self decodePath:[NSString stringWithFormat:@"/%@", url]];
        path = [[FPath alloc] initWith:pathString];
    }
    else {
        [NSException raise:@"No Firebase database specified." format:@"No Firebase database found for input: %@", url];
    }

    FRepoInfo* repoInfo = [[FRepoInfo alloc] initWithHost:host isSecure:secure withNamespace:namespace];

    FFLog(@"I-RDB095002", @"---> Parsed (%@) to: (%@,%@); ns=(%@); path=(%@)", original, [repoInfo description], [repoInfo connectionURL], repoInfo.namespace, [path description]);

    FParsedUrl* parsedUrl = [[FParsedUrl alloc] init];
    parsedUrl.repoInfo = repoInfo;
    parsedUrl.path = path;

    return parsedUrl;
}

/*
 case str: JString => priString + "string:" + str.s;
 case bool: JBool => priString + "boolean:" + bool.value;
 case double: JDouble => priString + "number:" + double.num;
 case int: JInt => priString + "number:" + int.num;
 case _ => {
 error("Leaf node has value '" + data.value + "' of invalid type '" + data.value.getClass.toString + "'");
 "";
 }
 */

+ (NSString *) getJavascriptType:(id)obj {
    if ([obj isKindOfClass:[NSDictionary class]]) {
        return kJavaScriptObject;
    } else if([obj isKindOfClass:[NSString class]]) {
        return kJavaScriptString;
    }
    else if ([obj isKindOfClass:[NSNumber class]]) {
        // We used to just compare to @encode(BOOL) as suggested at
        // http://stackoverflow.com/questions/2518761/get-type-of-nsnumber, but on arm64, @encode(BOOL) returns "B"
        // instead of "c" even though objCType still returns 'c' (signed char).  So check both.
        if(strcmp([obj objCType], @encode(BOOL)) == 0 ||
           strcmp([obj objCType], @encode(signed char)) == 0) {
            return kJavaScriptBoolean;
        }
        else {
            return kJavaScriptNumber;
        }
    }
    else {
        return kJavaScriptNull;
    }
}

+ (NSError *) errorForStatus:(NSString *)status andReason:(NSString *)reason {
    static dispatch_once_t pred = 0;
    __strong static NSDictionary* errorMap = nil;
    __strong static NSDictionary* errorCodes = nil;
    dispatch_once(&pred, ^{
        errorMap = @{
            @"permission_denied": @"Permission Denied",
            @"unavailable": @"Service is unavailable",
            kFErrorWriteCanceled: @"Write cancelled by user"
        };
        errorCodes = @{
            @"permission_denied": @1,
            @"unavailable": @2,
            kFErrorWriteCanceled: @3
        };
    });

    if ([status isEqualToString:kFWPResponseForActionStatusOk]) {
        return nil;
    } else {
        NSInteger code;
        NSString* desc = nil;
        if (reason) {
            desc = reason;
        } else if ([errorMap objectForKey:status] != nil) {
            desc = [errorMap objectForKey:status];
        } else {
            desc = status;
        }

        if ([errorCodes objectForKey:status] != nil) {
            NSNumber* num = [errorCodes objectForKey:status];
            code = [num integerValue];
        } else {
            // XXX what to do here?
            code = 9999;
        }

        return [[NSError alloc] initWithDomain:kFErrorDomain code:code userInfo:@{NSLocalizedDescriptionKey: desc}];
    }
}

+ (NSNumber *) intForString:(NSString *)string {
    static NSCharacterSet *notDigits = nil;
    if (!notDigits) {
        notDigits = [[NSCharacterSet decimalDigitCharacterSet] invertedSet];
    }
    if ([string rangeOfCharacterFromSet:notDigits].length == 0) {
        NSInteger num;
        NSScanner* scanner = [NSScanner scannerWithString:string];
        if ([scanner scanInteger:&num]) {
            return [NSNumber numberWithInteger:num];
        }
    }
    return nil;
}

+ (NSString *) ieee754StringForNumber:(NSNumber *)val {
    double d = [val doubleValue];
    NSData* data = [NSData dataWithBytes:&d length:sizeof(double)];
    NSMutableString* str = [[NSMutableString alloc] init];
    const unsigned char* buffer = (const unsigned char*)[data bytes];
    for (int i = 0; i < data.length; i++) {
        unsigned char byte = buffer[7 - i];
        [str appendFormat:@"%02x", byte];
    }
    return str;
}

static inline BOOL tryParseStringToInt(__unsafe_unretained NSString* str, NSInteger* integer) {
    // First do some cheap checks (NOTE: The below checks are significantly faster than an equivalent regex :-( ).
    NSUInteger length = str.length;
    if (length > 11 || length == 0) {
        return NO;
    }
    long long value = 0;
    BOOL negative = NO;
    NSUInteger i = 0;
    if ([str characterAtIndex:0] == '-') {
        if (length == 1) {
            return NO;
        }
        negative = YES;
        i = 1;
    }
    for(; i < length; i++) {
        unichar c = [str characterAtIndex:i];
        // Must be a digit, or '-' if it's the first char.
        if (c < '0' || c > '9') {
            return NO;
        } else {
            int charValue = c - '0';
            value = value*10 + charValue;
        }
    }

    value = (negative) ? -value : value;

    if (value < INTEGER_32_MIN || value > INTEGER_32_MAX) {
        return NO;
    } else {
        *integer = (NSInteger)value;
        return YES;
    }
}

+ (NSString *) maxName {
    static dispatch_once_t once;
    static NSString *maxName;
    dispatch_once(&once, ^{
        maxName = [[NSString alloc] initWithFormat:@"[MAX_NAME]"];
    });
    return maxName;
}

+ (NSString *) minName {
    static dispatch_once_t once;
    static NSString *minName;
    dispatch_once(&once, ^{
        minName = [[NSString alloc] initWithFormat:@"[MIN_NAME]"];
    });
    return minName;
}

+ (NSComparisonResult) compareKey:(NSString *)a toKey:(NSString *)b {
    if (a == b) {
        return NSOrderedSame;
    } else if (a == [FUtilities minName] || b == [FUtilities maxName]) {
        return NSOrderedAscending;
    } else if (b == [FUtilities minName] || a == [FUtilities maxName]) {
        return NSOrderedDescending;
    } else {
        NSInteger aAsInt, bAsInt;
        if (tryParseStringToInt(a, &aAsInt)) {
            if (tryParseStringToInt(b, &bAsInt)) {
                if (aAsInt > bAsInt) {
                    return NSOrderedDescending;
                } else if (aAsInt < bAsInt) {
                    return NSOrderedAscending;
                } else if (a.length > b.length) {
                    return NSOrderedDescending;
                } else if (a.length < b.length) {
                    return NSOrderedAscending;
                } else {
                    return NSOrderedSame;
                }
            } else {
                return (NSComparisonResult) NSOrderedAscending;
            }
        } else if (tryParseStringToInt(b, &bAsInt)) {
            return (NSComparisonResult) NSOrderedDescending;
        } else {
            // Perform literal character by character search to prevent a > b && b > a issues.
            // Note that calling -(NSString *)decomposedStringWithCanonicalMapping also works.
            return [a compare:b options:NSLiteralSearch];
        }
    }
}

+ (NSComparator) keyComparator {
    return ^NSComparisonResult(__unsafe_unretained NSString *a, __unsafe_unretained NSString *b) {
        return [FUtilities compareKey:a toKey:b];
    };
}

+ (NSComparator) stringComparator {
    return ^NSComparisonResult(__unsafe_unretained NSString *a, __unsafe_unretained NSString *b) {
        return [a compare:b];
    };
}

+ (double) randomDouble {
    return ((double) arc4random() / ARC4RANDOM_MAX);
}

@end