Cocoa/iPhone app/static library embedded resource file RTB v0.1 released

Source: Internet
Author: User

At a glance

In software development, you may need to embed the resource files used into binary execution files, for example, generate a single execution file, prevent confidential or copyright information from being viewed or modified by the PE tool, and embed image resources into the static library. In Mac OSX cocoa or iOS development, compile the generated product. an app is an app package, which is actually a folder. Right-click show package contents or remove it. after the app is expanded, you can double-click it to view the package content. modifying any resource files in the package will not affect the normal operation of the program, to submit the program to App Store, run codesign after modifying the resource file (this Dev tool is installed when xcode is installed) and re-sign the Code:

$ codesign -fvs "Your Identity" path/to/appfile.app

The elfcodesigner I wrote earlier is based on Codesign.

Therefore, embedding binary resource files is also an effective protection method.

Based on the measures used in my previous windows development, I have come up with three solutions:

  • Base64 encoding.
    N methods provided to a netizen on csdn a long time ago. At that time, his requirement was to codec some Chinese and English string data. The base64 solution I provided at that time was simple and effective. The base64 method also applies to embedded resources that are to be discussed today.
    Nsstring and nsdata can be converted to each other. uiimage also provides the initwithdata: creation method. Therefore, the image data is base64-encoded and stored in an nsstring constant, perform base64 decoding and then [uiimage initwithdata:].
    This method is useful when processing small amounts of data, and there are also mature nsdata + base64 categories online for use.
  • Compression encryption for PNG and other resources
    When the encrypted PNG image is viewed with a common image viewer, only an invalid image such as a transparent or blank image with Spot spots is displayed. However, the image is displayed normally when the program is running, this encryption method is widely used in the development of games and applications such as J2EE and IOS. There are many existing encryption tools on the Internet, but they can be used to reverse restore the source image.
  • Convert to bytes array.
    Most resource embedding uses this scheme. The principle is very simple. Each character in the resource file is converted into a hexadecimal bytes []. almost all languages provide direct conversion from bytes [] to Data.
    Most hexadecimal editors can save hex results as files. It is difficult to decrypt or modify the image. You need to know the basic format of the image, the skillful UE and other hexadecimal editors, guesses, and luck.

Recently, the company needs to embed some image resources into the static library. I have considered it better to use the bytes method. Cocoa native supports it without leaving room for modification. Some simple icons are drawn Using CG. In this way, the Public Library has only one. A file and several necessary. H files.

In the afternoon, when I was idle, I started xcode and wrote my first C program running on Mac OS on the new MacBook.

Release Notes

RTB (resource to bytes) is a command line tool that converts binary resource files to bytes arrays to facilitate embedded resources in programs.

Due to the rush of time, RTB has only been tested on Mac OS sl, and the corresponding applications have been tested in Mac OS desktop programs and iOS apps. Later, the applications will be changed to cross-platform ones. No bugs have been found.

Usage: open terminal, CD to the directory where RTB is located, and run

$./RTB image.png

Will generate

Unsigned char image_png [] = {.....};
Unsigned int maid = 16045;

The variable name is based on the resource file name. The prefix "_" is added at the beginning of the number. The non-English and numeric characters in the file name are converted to underscores "_" (isalnum () add the suffix "_ len" to the array variable name as the variable name of the array length.

For example, "123te st5.png" will generate variable names unsigned char _ 123te ____ st5_png [] and unsigned int _ 123te ____ st5_png_len.

Example

Run RTB to generate a. h file:

$./RTB test.png> test.png. h

Create an iPhone project for window baseand upload the test.png. h file to the project.
-(Bool) Application: didfinishlaunchingwitexceptions:
Create a uiimage and add it to an imageview:

 1 #import "test.png.h"
2
3 //............
4 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
5 {
6 [self.window makeKeyAndVisible];
7
8 unsigned char *imgBytes = test_png;
9 NSUInteger imgLenght = test_png_len;
10 NSData *imgData = [NSData dataWithBytesNoCopy:imgBytes length:imgLenght freeWhenDone:NO];
11 // UIImage *image = [UIImage imageWithData:imgData];
12 // or
13 UIImage *image = [[UIImage alloc] initWithData:imgData];
14
15 UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
16 imageView.frame = self.window.frame;
17 imageView.contentMode = UIViewContentModeCenter;
18 [self.window addSubview:imageView];
19 [imageView release];
20
21 [image release];
22 return YES;
23 }

The cause is that test.png has been replaced when the program crashes. the array in H is loaded into the memory. Therefore, you can use the datawithbytesnocopy method of nsdata. You do not need to copy it again. You do not need to release it after converting it to nsdata. Therefore, the freewhendonw parameter value is no.

Code Review

I don't need to post code because the C language is very bad. Comments and blank lines add up to more than 80 lines. The core function code is the fopen resource file:

If (FP = fopen (argv [1], "R "))! = NULL)

From the file header (GETC (FP) to EOF, fprintf (stdout, "0x % 02x", CH ):

For (P = 0; (length <0 | P <length) & (CH = GETC (FP ))! = EOF; P ++)
{
Char * c = P? ", \ N ":"";
Fprintf (FPO, "% s0x % 02x", (P % Cols )? ",": C, CH );
}

Download

Included in cocoa-utilities: https://github.com/Sundae/Cocoa-Utilities



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.