FRAQRUtils.m revision eea09ddc6e4b4287103c8500027f3e65b5470280
9ebd9a731316dfd624ce3bcc4ea6519d10899936Ken Stubbings/*
9ebd9a731316dfd624ce3bcc4ea6519d10899936Ken Stubbings * The contents of this file are subject to the terms of the Common Development and
9ebd9a731316dfd624ce3bcc4ea6519d10899936Ken Stubbings * Distribution License (the License). You may not use this file except in compliance with the
9ebd9a731316dfd624ce3bcc4ea6519d10899936Ken Stubbings * License.
9ebd9a731316dfd624ce3bcc4ea6519d10899936Ken Stubbings *
9ebd9a731316dfd624ce3bcc4ea6519d10899936Ken Stubbings * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
9ebd9a731316dfd624ce3bcc4ea6519d10899936Ken Stubbings * specific language governing permission and limitations under the License.
9ebd9a731316dfd624ce3bcc4ea6519d10899936Ken Stubbings *
9ebd9a731316dfd624ce3bcc4ea6519d10899936Ken Stubbings * When distributing Covered Software, include this CDDL Header Notice in each file and include
9ebd9a731316dfd624ce3bcc4ea6519d10899936Ken Stubbings * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
9ebd9a731316dfd624ce3bcc4ea6519d10899936Ken Stubbings * Header, with the fields enclosed by brackets [] replaced by your own identifying
9ebd9a731316dfd624ce3bcc4ea6519d10899936Ken Stubbings * information: "Portions copyright [year] [name of copyright owner]".
9ebd9a731316dfd624ce3bcc4ea6519d10899936Ken Stubbings *
9ebd9a731316dfd624ce3bcc4ea6519d10899936Ken Stubbings * Copyright 2016 ForgeRock AS.
9ebd9a731316dfd624ce3bcc4ea6519d10899936Ken Stubbings */
9ebd9a731316dfd624ce3bcc4ea6519d10899936Ken Stubbings
721bb987c406979bcfe705fa1ca8d54497d40fcbRobert Wapshott
9ebd9a731316dfd624ce3bcc4ea6519d10899936Ken Stubbings#import "FRAQRUtils.h"
9ebd9a731316dfd624ce3bcc4ea6519d10899936Ken Stubbings
2708b42676edf3d2e8f85b3c22b9e3be3cf43eb8Diego Colantonistatic NSString * const VALID_BASE64_CHARACTERS = @"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
9ebd9a731316dfd624ce3bcc4ea6519d10899936Ken Stubbings
9ebd9a731316dfd624ce3bcc4ea6519d10899936Ken Stubbings@implementation FRAQRUtils
9ebd9a731316dfd624ce3bcc4ea6519d10899936Ken Stubbings
2708b42676edf3d2e8f85b3c22b9e3be3cf43eb8Diego Colantoni+ (NSString *)replaceCharactersForURLDecoding:(NSString *)content {
9ebd9a731316dfd624ce3bcc4ea6519d10899936Ken Stubbings NSString * fixed = [content stringByReplacingOccurrencesOfString:@"-" withString:@"+"];
465ea459a87d4605e145d8f45b6a9c104b696e3bCraig McDonnell return [fixed stringByReplacingOccurrencesOfString:@"_" withString:@"/"];
465ea459a87d4605e145d8f45b6a9c104b696e3bCraig McDonnell}
465ea459a87d4605e145d8f45b6a9c104b696e3bCraig McDonnell
2708b42676edf3d2e8f85b3c22b9e3be3cf43eb8Diego Colantoni+ (NSData *)decodeURL:(NSString *) content {
465ea459a87d4605e145d8f45b6a9c104b696e3bCraig McDonnell NSString * fixed = [FRAQRUtils replaceCharactersForURLDecoding:content];
c0a2f8c6e84ddf9a597f19e5f161382b0e2cf81bDiego Colantoni [self pad:fixed];
9ebd9a731316dfd624ce3bcc4ea6519d10899936Ken Stubbings
9ebd9a731316dfd624ce3bcc4ea6519d10899936Ken Stubbings return [FRAQRUtils decodeBase64:fixed];
9ebd9a731316dfd624ce3bcc4ea6519d10899936Ken Stubbings}
9ebd9a731316dfd624ce3bcc4ea6519d10899936Ken Stubbings
2708b42676edf3d2e8f85b3c22b9e3be3cf43eb8Diego Colantoni+ (NSData *)decodeBase64:(NSString *) base64String {
a4c96a094a3d715ef793b60cfabfdb2ee088b319Diego Colantoni if (!base64String) {
a4c96a094a3d715ef793b60cfabfdb2ee088b319Diego Colantoni return nil;
a4c96a094a3d715ef793b60cfabfdb2ee088b319Diego Colantoni }
a4c96a094a3d715ef793b60cfabfdb2ee088b319Diego Colantoni
c0a2f8c6e84ddf9a597f19e5f161382b0e2cf81bDiego Colantoni base64String = [self pad:base64String];
465ea459a87d4605e145d8f45b6a9c104b696e3bCraig McDonnell return [[NSData alloc] initWithBase64EncodedString:base64String options:0];
9ebd9a731316dfd624ce3bcc4ea6519d10899936Ken Stubbings}
9ebd9a731316dfd624ce3bcc4ea6519d10899936Ken Stubbings
eea09ddc6e4b4287103c8500027f3e65b5470280Diego Colantoni+ (NSString *)decode:(NSString *)content {
eea09ddc6e4b4287103c8500027f3e65b5470280Diego Colantoni return [content stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
9ebd9a731316dfd624ce3bcc4ea6519d10899936Ken Stubbings}
9ebd9a731316dfd624ce3bcc4ea6519d10899936Ken Stubbings
eea09ddc6e4b4287103c8500027f3e65b5470280Diego Colantoni+ (NSString *)pad:(NSString *)content {
eea09ddc6e4b4287103c8500027f3e65b5470280Diego Colantoni int paddSize = (4 - ([content length] % 4)) % 4;
c0a2f8c6e84ddf9a597f19e5f161382b0e2cf81bDiego Colantoni
c0a2f8c6e84ddf9a597f19e5f161382b0e2cf81bDiego Colantoni if(paddSize != 0) {
c0a2f8c6e84ddf9a597f19e5f161382b0e2cf81bDiego Colantoni for(int i = 0; i < paddSize; ++i) {
eea09ddc6e4b4287103c8500027f3e65b5470280Diego Colantoni content = [content stringByAppendingString:@"="];
c0a2f8c6e84ddf9a597f19e5f161382b0e2cf81bDiego Colantoni }
c0a2f8c6e84ddf9a597f19e5f161382b0e2cf81bDiego Colantoni }
c0a2f8c6e84ddf9a597f19e5f161382b0e2cf81bDiego Colantoni
eea09ddc6e4b4287103c8500027f3e65b5470280Diego Colantoni return content;
c0a2f8c6e84ddf9a597f19e5f161382b0e2cf81bDiego Colantoni}
c0a2f8c6e84ddf9a597f19e5f161382b0e2cf81bDiego Colantoni
2708b42676edf3d2e8f85b3c22b9e3be3cf43eb8Diego Colantoni+ (BOOL)isBase64:(NSString *)content {
2708b42676edf3d2e8f85b3c22b9e3be3cf43eb8Diego Colantoni static NSCharacterSet *invertedBase64CharacterSet = nil;
2708b42676edf3d2e8f85b3c22b9e3be3cf43eb8Diego Colantoni if (invertedBase64CharacterSet == nil) {
2708b42676edf3d2e8f85b3c22b9e3be3cf43eb8Diego Colantoni invertedBase64CharacterSet = [[NSCharacterSet characterSetWithCharactersInString:VALID_BASE64_CHARACTERS] invertedSet];
2708b42676edf3d2e8f85b3c22b9e3be3cf43eb8Diego Colantoni }
2708b42676edf3d2e8f85b3c22b9e3be3cf43eb8Diego Colantoni return [content rangeOfCharacterFromSet:invertedBase64CharacterSet options:NSLiteralSearch].location == NSNotFound;
2708b42676edf3d2e8f85b3c22b9e3be3cf43eb8Diego Colantoni}
2708b42676edf3d2e8f85b3c22b9e3be3cf43eb8Diego Colantoni
9ebd9a731316dfd624ce3bcc4ea6519d10899936Ken Stubbings@end