Oracle's Wrap Utility

来源:互联网 发布:台湾火烧车事件 知乎 编辑:程序博客网 时间:2024/05/01 15:44
 July 22, 2004
Oracle's Wrap Utility
By Steve Callan

One thing that Oracle andUPS have in common is that they both deal with wrapped packages. Why botherwith wrapping packages? One obvious reason is to hide the contents from pryingeyes. You can wrap a real package with nondescript wrapping paper or usetransparent cellophane. This idea of a choice between hiding and exposingpackages (and other objects) in Oracle serves the same purpose as wrapping yourUPS package - you do not want prying eyes to see what's in your Oracle package.

The PL/SQL Wrap Utility

Appendix C of the Oracle 9.2PL/SQL User's Guide and Reference covers the wrap utility. With manyapplications, it is common to see directories full of source code. Businessescan protect their intellectual property by taking proactive measures to protectand safeguard copyrighted material. If a company suspects copyrightinfringement is occurring, it is up to the company to seek legal remedies. Thisapproach to protecting intellectual property (source code) is similar toclosing the barn door after the cows have left. Perhaps the sheer number offiles and lines of code serves as a deterrent to this type of theft.

If you or your company hasdeveloped algorithms that give your product a competitive edge in themarketplace, it makes sense that you would want to protect that code. All thatneeds to be exposed is the input. The internals of what happens to that inputor how the output is produced can be likened to the black box approach intesting. With Oracle's wrap utility, you can turn the inner workings of yourcode into an impenetrable black box, for the most part, to protect yourproperty.

The "for the most part"caveat serves to alert you that wrap has some limitations. The reference guide,in part, states, "String literals, number literals and names of variables,tables and columns remain in plain text within the wrapped file. Wrapping aprocedure helps to hide the algorithm and prevent reverse-engineering, but itis not a way to hide passwords or table names that you want to be secret."

Oracle allows you to wrapthe code inside the following statements:

CREATE [OR REPLACE] FUNCTION function_name
CREATE [OR REPLACE] PROCEDURE procedure_name
CREATE [OR REPLACE] PACKAGE package_name
CREATE [OR REPLACE] PACKAGE BODY package_name
CREATE [OR REPLACE] TYPE type_name ... OBJECT
CREATE [OR REPLACE] TYPE BODY type_name

A common practice is toleave specifications unwrapped and to wrap the implementation of procedures andfunctions within the package body.

Running the Wrap Utility

Wrapping a package isamazingly simple, although there is one pitfall to avoid. Among the 500 plusfiles in the ORACLE_HOME/bin directory you will see wrap or WRAP depending onyour operating system (a lot of the more common files and directories onWindows are upper-cased for ease of readability and identification).

Using wrap is as simple asthis:

c:/ora9i/bin wrap iname=input_file_name 

You can specify an outputfile as a second argument by using this format:

c:/ora9i/bin wrap iname=input_file_name oname=output_file_name

Once wrapped, a packagecannot be unwrapped. With that in mind, and just like what you would do with areal package, do not wrap the package until it is time to ship it. This implieskeeping a source code repository of the original code. If a customer or userreports a bug against a wrapped package, there is no unwrapping it at thecustomer's site. You are going to have to ship another wrapped package with thefix in it. You will be making the bug fix or enhancement with the repositoryfile and wrapping the new file.

An Example of Using Wrap

In a previous article, Iused the DBMS_RANDOM package. A slightly modified bit of code from thatproduces the following:

SQL> CREATE or REPLACE PROCEDURE wrap_it (seed_in NUMBER)
2 IS
3 v_rand INTEGER;
4 BEGIN
5 DBMS_RANDOM.INITIALIZE (seed_in);
6 FOR i IN 1..5 LOOP
7 v_rand := mod(abs(DBMS_RANDOM.RANDOM),45);
8 dbms_output.put_line(i||': '||v_rand);
9 END LOOP;
10 END;
11 /

Procedure created.

SQL> exec wrap_it(123456);
1: 37
2: 36
3: 18
4: 8
5: 32

PL/SQL procedure successfully completed.

I can take the code for thewrap_it procedure and, well, wrap it. The code for the procedure is in a filenamed wrap_example.sql.

C:/ora9i>wrap iname=c:/ora9i/admin/wrap_example.sql

PL/SQL Wrapper: Release 10.1.0.2.0- Production on Thu Jul 15 22:11:18 2004

Copyright (c) 1993, 2004, Oracle. All rights reserved.

Processing c:/ora9i/admin/wrap_example.sql to wrap_example.plb

Note how Oracle changed thefile extension to "plb." Can we see the contents of thewrap_example.plb file? Sure, but will it be completely readable? Let's findout.

Opening wrap_example.plb inmy general-purpose text editor (TextPad, www.textpad.com,it's free, but worth paying for a license), we see the following lines of text:

CREATE or REPLACE PROCEDURE wrap_it wrapped 
a000000
b2
abcd
abcd
abcd
abcd
abcd
abcd
abcd
abcd
abcd
abcd
abcd
abcd
abcd
abcd
abcd
7
e0 f7
uveH+zmPD1snPwtBZmL3T1hkxRcwgy7w154VfC9GAME+MzyOFMq0DNYH29gBazqoJJQ0Xemb
pdRvlWrC2UeKeKiS2uzT80HMAvKIMOYhjXZT4CrU98zgprrwl4jKnFKvFljUAnGx8GHexDSU
XRa3oykCJIUWEovu72mqAm0vttgZB9E/9E6y2HhxKdu1k8arcrHegHYAvF1pwn1e6sCFJg04
QGsN1g1JLYIklPGBDEEZInWt0w==

/

That looks fairlyundecipherable to me. One thing to note is the location of the output file.Using the example above, the new wrap_example.plb file was created in thec:/ora9i (my ORACLE_HOME) directory.

Now we know the procedure works and that the file or script can be wrapped. I will drop the procedure and re-create it using the plb file. If all goeswell, I should be able to execute the procedure as before.

SQL> drop procedure wrap_it;

Procedure dropped.

SQL> @c:/ora9i/wrap_example.plb

Procedure created.

SQL> exec wrap_it(123456);
1: 37
2: 36
3: 18
4: 8
5: 32

PL/SQL procedure successfully completed.

In Closing

The wrap utility is veryeasy to use, so if you have code you would like to protect, wrap it up. If theuncompiled or unwrapped version passes testing, so will the wrapped versionbecause Oracle guarantees portability of the code.

What are some situationswhere you would want to wrap your code? Schema migration or alterationimmediately comes to mind. Often times when there is a database migration(going to a newer version), you will find application developers or softwaremanufacturers releasing a newer version of their product. The point release ormajor release version probably incorporates schema changes. Running wrappedupdate scripts helps protect your work. Keep in mind, though, that if amalicious user is dedicated enough, pretty much any schema can be reverseengineered, but why make it easy for him? Wrap it up and protect your work. Andthat's a wrap.