FRAQRUtils.m revision 9ebd9a731316dfd624ce3bcc4ea6519d10899936
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
9ebd9a731316dfd624ce3bcc4ea6519d10899936Ken Stubbings#import <Foundation/Foundation.h>
9ebd9a731316dfd624ce3bcc4ea6519d10899936Ken Stubbings#import "FRAQRUtils.h"
9ebd9a731316dfd624ce3bcc4ea6519d10899936Ken Stubbings
9ebd9a731316dfd624ce3bcc4ea6519d10899936Ken Stubbings
9ebd9a731316dfd624ce3bcc4ea6519d10899936Ken Stubbings@implementation FRAQRUtils
9ebd9a731316dfd624ce3bcc4ea6519d10899936Ken Stubbings
9ebd9a731316dfd624ce3bcc4ea6519d10899936Ken Stubbings+ (NSString *) decodeURL:(NSString *) content {
9ebd9a731316dfd624ce3bcc4ea6519d10899936Ken Stubbings NSString * fixed = [content stringByReplacingOccurrencesOfString:@"-" withString:@"+"];
9ebd9a731316dfd624ce3bcc4ea6519d10899936Ken Stubbings fixed = [fixed stringByReplacingOccurrencesOfString:@"_" withString:@"/"];
9ebd9a731316dfd624ce3bcc4ea6519d10899936Ken Stubbings int paddSize = (4 - ([fixed length] % 4)) % 4;
9ebd9a731316dfd624ce3bcc4ea6519d10899936Ken Stubbings
9ebd9a731316dfd624ce3bcc4ea6519d10899936Ken Stubbings if(paddSize != 0) {
9ebd9a731316dfd624ce3bcc4ea6519d10899936Ken Stubbings for(int i = 0; i < paddSize; ++i) {
9ebd9a731316dfd624ce3bcc4ea6519d10899936Ken Stubbings fixed = [fixed stringByAppendingString:@"="];
9ebd9a731316dfd624ce3bcc4ea6519d10899936Ken Stubbings }
9ebd9a731316dfd624ce3bcc4ea6519d10899936Ken Stubbings }
9ebd9a731316dfd624ce3bcc4ea6519d10899936Ken Stubbings
9ebd9a731316dfd624ce3bcc4ea6519d10899936Ken Stubbings return [FRAQRUtils decodeBase64:fixed];
9ebd9a731316dfd624ce3bcc4ea6519d10899936Ken Stubbings}
9ebd9a731316dfd624ce3bcc4ea6519d10899936Ken Stubbings
9ebd9a731316dfd624ce3bcc4ea6519d10899936Ken Stubbings+ (NSString *) decodeBase64:(NSString *) base64String {
9ebd9a731316dfd624ce3bcc4ea6519d10899936Ken Stubbings NSData *decodedData = [[NSData alloc] initWithBase64EncodedString:base64String options:0];
9ebd9a731316dfd624ce3bcc4ea6519d10899936Ken Stubbings NSString *decodedString = [[NSString alloc] initWithData:decodedData encoding:NSUTF8StringEncoding];
9ebd9a731316dfd624ce3bcc4ea6519d10899936Ken Stubbings return decodedString;
9ebd9a731316dfd624ce3bcc4ea6519d10899936Ken Stubbings}
9ebd9a731316dfd624ce3bcc4ea6519d10899936Ken Stubbings
9ebd9a731316dfd624ce3bcc4ea6519d10899936Ken Stubbings+ (NSString *) decode:(NSString *) str {
9ebd9a731316dfd624ce3bcc4ea6519d10899936Ken Stubbings if (str == nil) {
9ebd9a731316dfd624ce3bcc4ea6519d10899936Ken Stubbings return nil;
9ebd9a731316dfd624ce3bcc4ea6519d10899936Ken Stubbings }
9ebd9a731316dfd624ce3bcc4ea6519d10899936Ken Stubbings const char *tmp = [str UTF8String];
9ebd9a731316dfd624ce3bcc4ea6519d10899936Ken Stubbings NSMutableString *ret = [[NSMutableString alloc] init];
9ebd9a731316dfd624ce3bcc4ea6519d10899936Ken Stubbings for (NSUInteger i = 0; i < str.length; i++) {
9ebd9a731316dfd624ce3bcc4ea6519d10899936Ken Stubbings if (tmp[i] != '%' || !ishex(tmp[i + 1]) || !ishex(tmp[i + 2])) {
9ebd9a731316dfd624ce3bcc4ea6519d10899936Ken Stubbings [ret appendFormat:@"%c", tmp[i]];
9ebd9a731316dfd624ce3bcc4ea6519d10899936Ken Stubbings continue;
9ebd9a731316dfd624ce3bcc4ea6519d10899936Ken Stubbings }
9ebd9a731316dfd624ce3bcc4ea6519d10899936Ken Stubbings
9ebd9a731316dfd624ce3bcc4ea6519d10899936Ken Stubbings uint8_t c = 0;
9ebd9a731316dfd624ce3bcc4ea6519d10899936Ken Stubbings c |= fromhex(tmp[++i]) << 4;
9ebd9a731316dfd624ce3bcc4ea6519d10899936Ken Stubbings c |= fromhex(tmp[++i]);
9ebd9a731316dfd624ce3bcc4ea6519d10899936Ken Stubbings
9ebd9a731316dfd624ce3bcc4ea6519d10899936Ken Stubbings [ret appendFormat:@"%c", c];
9ebd9a731316dfd624ce3bcc4ea6519d10899936Ken Stubbings }
9ebd9a731316dfd624ce3bcc4ea6519d10899936Ken Stubbings
9ebd9a731316dfd624ce3bcc4ea6519d10899936Ken Stubbings return ret;
9ebd9a731316dfd624ce3bcc4ea6519d10899936Ken Stubbings}
9ebd9a731316dfd624ce3bcc4ea6519d10899936Ken Stubbings
9ebd9a731316dfd624ce3bcc4ea6519d10899936Ken Stubbingsstatic BOOL ishex(char c) {
9ebd9a731316dfd624ce3bcc4ea6519d10899936Ken Stubbings if (c >= '0' && c <= '9') {
9ebd9a731316dfd624ce3bcc4ea6519d10899936Ken Stubbings return YES;
9ebd9a731316dfd624ce3bcc4ea6519d10899936Ken Stubbings }
9ebd9a731316dfd624ce3bcc4ea6519d10899936Ken Stubbings if (c >= 'A' && c <= 'F') {
9ebd9a731316dfd624ce3bcc4ea6519d10899936Ken Stubbings return YES;
9ebd9a731316dfd624ce3bcc4ea6519d10899936Ken Stubbings }
9ebd9a731316dfd624ce3bcc4ea6519d10899936Ken Stubbings if (c >= 'a' && c <= 'f') {
9ebd9a731316dfd624ce3bcc4ea6519d10899936Ken Stubbings return YES;
9ebd9a731316dfd624ce3bcc4ea6519d10899936Ken Stubbings }
9ebd9a731316dfd624ce3bcc4ea6519d10899936Ken Stubbings return NO;
9ebd9a731316dfd624ce3bcc4ea6519d10899936Ken Stubbings}
9ebd9a731316dfd624ce3bcc4ea6519d10899936Ken Stubbings
9ebd9a731316dfd624ce3bcc4ea6519d10899936Ken Stubbingsstatic uint8_t fromhex(char c) {
9ebd9a731316dfd624ce3bcc4ea6519d10899936Ken Stubbings if (c >= '0' && c <= '9') {
9ebd9a731316dfd624ce3bcc4ea6519d10899936Ken Stubbings return c - '0';
9ebd9a731316dfd624ce3bcc4ea6519d10899936Ken Stubbings }
9ebd9a731316dfd624ce3bcc4ea6519d10899936Ken Stubbings if (c >= 'A' && c <= 'F') {
9ebd9a731316dfd624ce3bcc4ea6519d10899936Ken Stubbings return c - 'A' + 10;
9ebd9a731316dfd624ce3bcc4ea6519d10899936Ken Stubbings }
9ebd9a731316dfd624ce3bcc4ea6519d10899936Ken Stubbings if (c >= 'a' && c <= 'f') {
9ebd9a731316dfd624ce3bcc4ea6519d10899936Ken Stubbings return c - 'a' + 10;
9ebd9a731316dfd624ce3bcc4ea6519d10899936Ken Stubbings }
9ebd9a731316dfd624ce3bcc4ea6519d10899936Ken Stubbings return 0;
9ebd9a731316dfd624ce3bcc4ea6519d10899936Ken Stubbings}
9ebd9a731316dfd624ce3bcc4ea6519d10899936Ken Stubbings
9ebd9a731316dfd624ce3bcc4ea6519d10899936Ken Stubbings@end