Skip to content

Commit

Permalink
Fixed some Analysis warnings, fixed minor memory leak
Browse files Browse the repository at this point in the history
  • Loading branch information
Lukas Kukacka committed Nov 19, 2013
1 parent 35afeb5 commit fea75d1
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 112 deletions.
9 changes: 2 additions & 7 deletions .gitignore
Expand Up @@ -8,19 +8,14 @@

xcuserdata

PLBlocks.framework/*
build/*

.DS_Store
profile
.svn

Calpol.xcodeproj/project.xcworkspace/xcuserdata/baldophpourprix.xcuserdatad/UserInterfaceState.xcuserstate

Calpol.xcodeproj/project.xcworkspace/xcuserdata/baldophpourprix.xcuserdatad/UserInterfaceState.xcuserstate

/.DS_Store
/Calpol/.DS_Store
Calpol/CPSectionView.m

/FTCoreText.xcodeproj/project.xcworkspace/xcuserdata/fuerteinternational.xcuserdatad/UserInterfaceState.xcuserstate
/FTCoreText.xcodeproj/project.xcworkspace/xcuserdata/fuerteinternational.xcuserdatad/UserInterfaceState.xcuserstate
FTCTExample/FTCTExample.xcodeproj/project.xcworkspace/xcshareddata/FTCTExample.xccheckout
Expand Up @@ -5,34 +5,34 @@
<key>IDESourceControlProjectFavoriteDictionaryKey</key>
<false/>
<key>IDESourceControlProjectIdentifier</key>
<string>1C41FDAF-255B-4874-A4C5-5FEC36D3872A</string>
<string>95F1FEB7-E73C-47E9-8098-6B0E2D7BBE8B</string>
<key>IDESourceControlProjectName</key>
<string>FTCTExample</string>
<key>IDESourceControlProjectOriginsDictionary</key>
<dict>
<key>750F407C-4645-4DF9-9919-B97AFEC6C79B</key>
<string>https://github.com/FuerteInternational/FTCoreText</string>
<key>8169EECB-14CE-4163-9B6D-F1780B0472D0</key>
<string>ssh://github.com/FuerteInternational/FTCoreText.git</string>
</dict>
<key>IDESourceControlProjectPath</key>
<string>FTCTExample/FTCTExample.xcodeproj/project.xcworkspace</string>
<key>IDESourceControlProjectRelativeInstallPathDictionary</key>
<dict>
<key>750F407C-4645-4DF9-9919-B97AFEC6C79B</key>
<key>8169EECB-14CE-4163-9B6D-F1780B0472D0</key>
<string>../../..</string>
</dict>
<key>IDESourceControlProjectURL</key>
<string>https://github.com/FuerteInternational/FTCoreText</string>
<string>ssh://github.com/FuerteInternational/FTCoreText.git</string>
<key>IDESourceControlProjectVersion</key>
<integer>110</integer>
<key>IDESourceControlProjectWCCIdentifier</key>
<string>750F407C-4645-4DF9-9919-B97AFEC6C79B</string>
<string>8169EECB-14CE-4163-9B6D-F1780B0472D0</string>
<key>IDESourceControlProjectWCConfigurations</key>
<array>
<dict>
<key>IDESourceControlRepositoryExtensionIdentifierKey</key>
<string>public.vcs.git</string>
<key>IDESourceControlWCCIdentifierKey</key>
<string>750F407C-4645-4DF9-9919-B97AFEC6C79B</string>
<string>8169EECB-14CE-4163-9B6D-F1780B0472D0</string>
<key>IDESourceControlWCCName</key>
<string>FTCoreText</string>
</dict>
Expand Down
176 changes: 78 additions & 98 deletions FTCoreText/FTCoreTextView.m
Expand Up @@ -17,7 +17,7 @@

@interface NSData (FTCoreTextAdditions)

+ (NSData *)ftct_base64DataFromString:(NSString *)string;
+ (NSData *)ftct_dataWithBase64EncodedString:(NSString *)string;

@end

Expand Down Expand Up @@ -552,7 +552,7 @@ - (NSDictionary *)dataForPoint:(CGPoint)point activeRects:(NSArray **)activeRect
baselineOrigin.y = CGRectGetHeight(self.frame) - baselineOrigin.y;

CGRect lineFrame = CGRectMake(baselineOrigin.x, baselineOrigin.y - ascent, lineWidth, ascent + descent);
CGRect actualRect;
CGRect actualRect = CGRectZero;
actualRect.size.height = lineFrame.size.height;
actualRect.origin.y = lineFrame.origin.y;

Expand Down Expand Up @@ -811,7 +811,7 @@ - (void)processText
UIImage *img =nil;
if ([elementContent hasPrefix:@"base64:"])
{
NSData *myImgData = [NSData ftct_base64DataFromString:[elementContent substringFromIndex:7]];
NSData *myImgData = [NSData ftct_dataWithBase64EncodedString:[elementContent substringFromIndex:7]];
img = [UIImage imageWithData:myImgData];
}
else
Expand Down Expand Up @@ -1292,7 +1292,7 @@ - (void)drawImages
UIImage *img =nil;
if ([imageNode.imageName hasPrefix:@"base64:"])
{
NSData* myImgData = [NSData ftct_base64DataFromString:[imageNode.imageName substringFromIndex:7]];
NSData* myImgData = [NSData ftct_dataWithBase64EncodedString:[imageNode.imageName substringFromIndex:7]];
img = [UIImage imageWithData:myImgData];
}
else
Expand Down Expand Up @@ -1432,6 +1432,8 @@ - (NSString*)getTextInLineByRange:(NSRange)range
CGPathRelease(mainPath);

NSArray *lines = (__bridge NSArray *)CTFrameGetLines(ctframe);
CFRelease(ctframe);

NSInteger lineCount = [lines count];
if (lineCount != 0)
{
Expand All @@ -1447,7 +1449,7 @@ - (NSString*)getTextInLineByRange:(NSRange)range
}
}
}
CFRelease(ctframe);

return @"";
}

Expand All @@ -1467,112 +1469,90 @@ - (NSString *)stringByAppendingTagName:(NSString *)tagName



@implementation NSData (NSDataAdditions)
@implementation NSData (FTCoreTextAdditions)

/**
* Code by Alex Reynolds from StackOverflow discussion here:
* http://stackoverflow.com/a/800976/2378431
*/
+ (NSData *)ftct_base64DataFromString:(NSString *)string
//
// This method's implementation is copyied from Nick Lockwood's Base64
// Header of the method has been altered to prevent naming collisions
//
// Version 1.1
//
// Created by Nick Lockwood on 12/01/2012.
// Copyright (C) 2012 Charcoal Design
//
// Distributed under the permissive zlib License
// Get the latest version from here:
//
// https://github.com/nicklockwood/Base64
//
// This software is provided 'as-is', without any express or implied
// warranty. In no event will the authors be held liable for any damages
// arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it
// freely, subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented; you must not
// claim that you wrote the original software. If you use this software
// in a product, an acknowledgment in the product documentation would be
// appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such, and must not be
// misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source distribution.
//

+ (NSData *)ftct_dataWithBase64EncodedString:(NSString *)string
{
unsigned long ixtext, lentext;
unsigned char ch, inbuf[4], outbuf[3];
short i, ixinbuf;
Boolean flignore, flendtext = false;
const unsigned char *tempcstring;
NSMutableData *theData;

if (string == nil)
const char lookup[] =
{
return [NSData data];
}

ixtext = 0;

tempcstring = (const unsigned char *)[string UTF8String];
99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99,
99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99,
99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 62, 99, 99, 99, 63,
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 99, 99, 99, 99, 99, 99,
99, 0, 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, 99, 99, 99, 99, 99,
99, 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, 99, 99, 99, 99, 99
};

lentext = [string length];
NSData *inputData = [string dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSUInteger inputLength = [inputData length];
const unsigned char *inputBytes = [inputData bytes];

theData = [NSMutableData dataWithCapacity: lentext];
NSUInteger maxOutputLength = (inputLength / 4 + 1) * 3;
NSMutableData *outputData = [NSMutableData dataWithLength:maxOutputLength];
unsigned char *outputBytes = (unsigned char *)[outputData mutableBytes];

ixinbuf = 0;

while (true)
NSUInteger accumulator = 0;
NSUInteger outputLength = 0;
unsigned char accumulated[] = {0, 0, 0, 0};
for (NSUInteger i = 0; i < inputLength; i++)
{
if (ixtext >= lentext) {
break;
}

ch = tempcstring [ixtext++];

flignore = false;

if ((ch >= 'A') && (ch <= 'Z')) {
ch = ch - 'A';
}
else if ((ch >= 'a') && (ch <= 'z')) {
ch = ch - 'a' + 26;
}
else if ((ch >= '0') && (ch <= '9')) {
ch = ch - '0' + 52;
}
else if (ch == '+') {
ch = 62;
}
else if (ch == '=') {
flendtext = true;
}
else if (ch == '/') {
ch = 63;
}
else {
flignore = true;
}

if (!flignore)
unsigned char decoded = lookup[inputBytes[i] & 0x7F];
if (decoded != 99)
{
short ctcharsinbuf = 3;
Boolean flbreak = false;

if (flendtext)
accumulated[accumulator] = decoded;
if (accumulator == 3)
{
if (ixinbuf == 0) {
break;
}

if ((ixinbuf == 1) || (ixinbuf == 2)) {
ctcharsinbuf = 1;
}
else {
ctcharsinbuf = 2;
}

ixinbuf = 3;
flbreak = true;
}

inbuf [ixinbuf++] = ch;

if (ixinbuf == 4)
{
ixinbuf = 0;

outbuf[0] = (inbuf[0] << 2) | ((inbuf[1] & 0x30) >> 4);
outbuf[1] = ((inbuf[1] & 0x0F) << 4) | ((inbuf[2] & 0x3C) >> 2);
outbuf[2] = ((inbuf[2] & 0x03) << 6) | (inbuf[3] & 0x3F);

for (i = 0; i < ctcharsinbuf; i++){
[theData appendBytes: &outbuf[i] length: 1];
}
}

if (flbreak){
break;
outputBytes[outputLength++] = (accumulated[0] << 2) | (accumulated[1] >> 4);
outputBytes[outputLength++] = (accumulated[1] << 4) | (accumulated[2] >> 2);
outputBytes[outputLength++] = (accumulated[2] << 6) | accumulated[3];
}
accumulator = (accumulator + 1) % 4;
}
}

return theData;
//handle left-over data
if (accumulator > 0) outputBytes[outputLength] = (accumulated[0] << 2) | (accumulated[1] >> 4);
if (accumulator > 1) outputBytes[++outputLength] = (accumulated[1] << 4) | (accumulated[2] >> 2);
if (accumulator > 2) outputLength++;

//truncate data to match actual output length
outputData.length = outputLength;
return outputLength? outputData: nil;
}

@end

0 comments on commit fea75d1

Please sign in to comment.