29
Fri, Nov
0 New Articles

RPG Building Blcoks

RPG
Typography
  • Smaller Small Medium Big Bigger
  • Default Helvetica Segoe Georgia Times

About Translating Strings

Here’s a simple way to “externalize” the XLATE op code.

RPG’s XLATE op code is flexible, but it depends on program-defined tables to do its work. The subprocedure XLATE presented in this article uses external table objects (*TBL) to do the same thing, thus simplifying the programmer’s job and reducing the chances for error. As a bonus, you get to learn how to create your own translation tables.

Translating strings has always been one of those chores you wish you didn’t have to do. To be sure, the Translate (XLATE) op code helps somewhat by allowing you to provide a program-described translation table. Still, in this age of object-oriented programming, using program-described pseudo-objects is cumbersome.

That’s why I decided to improve on the op code and create a subprocedure of the same name (XLATE) that can translate any given string using an external table object (*TBL). For illustration purposes, let’s imagine you want to cypher some data using the popular ROT13 cryptographical scheme.

ROT13 is simple, so it’s easy to break, but it will do for illustration. Its premise is that all letters of the alphabet are rotated 13 positions. Since there are 26 letters in the English alphabet, it turns out that applying ROT13 twice restores the original string as it was—well, originally. To give you an idea, A becomes N, B becomes O, all the way to M, which becomes Z. Then N becomes A, O becomes B, all the way to Z, which becomes M.

Creating the Table

OS/400 gives us the tool to create an external table: the Create Table (CRTTBL) command. Although you can create a table without using source code, by specifying SRCFILE(*PROMPT), you’re probably better off writing the source code and then using

that to create the table. The reason is simple: You may want to document a table, clone it to make one almost like it, whatever.

Table source code (source type TBL) must have exactly eight records of 64 bytes each. You can use any standard 92-byte source file, however. In the first record, you must code the hexadecimal values of each of the first 32 characters in the EBCDIC collating sequence (X’00’ to X’2F’). The rest of the records must contain the hex values of the remaining characters.

Then, you need to make changes to whatever values you want translated. In my ROT13 example, I change X’81’ (lowercase a) to X’95’ (lowercase n) and X’C1’ (capital
A) to X’B5’ (capital N), and then I make similar changes for the other 25 letters, in both lowercase and uppercase. The resulting source code is listed in Figure 1. You compile this code with the following command:

CRTTBL TBL(xxx/ROT13) +

SRCFILE(xxx/QTBLSRC)

The Subprocedure XLATE

Figure 2 shows the prototype I’ll use for subprocedure XLATE. XLATE is designed to process strings of any length (up to the RPG limit of 32,767 bytes). It needs two parameters: the input string (i.e., the string you want to translate) and the name of the table object (in our case, the name is ROT13). XLATE then returns the translated string as a string of varying length up to 32,767 bytes.

Figure 3 shows the source code for RPG IV module XLATEMOD. Its workings are extraordinarily simple. The actual translation is performed by system API QDCXLATE, which uses fixed-length strings and requires, as its first parameter, the length of the input string. Since I designed XLATE to use varying-length strings, the calculation of this length can be performed by built-in function %LEN. After QDCXLATE returns control to my RPG function, indicator 01 is on if QDCXLATE ended in error (as would be the case if the table you supplied did not exist); in that case, QDCXLATE returns a hexadecimal FF; otherwise, it returns the actual translated string.

You’ll notice the VARYING keywords in the D-specs; those define character strings of varying length. If you’re using a version prior to V4R2, you won’t be able to use VARYING. In that case, you can remove the keywords and make sure to pass XLATE strings of fixed size. Also, in that case, consider not translating the blank space to anything different so that the inevitable trailing blanks (of the fixed-size strings) aren’t converted to anything weird.

Finally, Figure 4 gives you an idea how to use XLATE. This test program receives a character string of up to 32 bytes, translates it, and displays the translated value. To do so, it invokes my function XLATE, supplying a table name of ROT13. To run the test program, execute the following command:

CALL XLATETEST ‘string’

In this command, ‘string’ is the character string (up to 32 bytes) you want translated.

More Tables

Now that you know how to code table source code, you can create your own tables to suit your translation requirements. But don’t reinvent the wheel! OS/400 contains a few predefined tables you can use right away. For example, QSYSTRNTBL in QSYS translates all lowercase letters to uppercase, QASCII translates EBCDIC to ASCII, and QEBCDIC does the opposite. If you need translation within a CL program, you can call QDCXLATE directly from it.

Finally, a warning. First, do not rely on ROT13 to keep your information confidential. As I mentioned earlier, it’s a weak encryption algorithm that any fourth grader can break.

000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F
202122232425262728292A2B2C2D2E2F303132333435363738393A3B3C3D3E3F
404142434445464748494A4B4C4D4E4F505152535455565758595A5B5C5D5E5F
606162636465666768696A6B6C6D6E6F707172737475767778797A7B7C7D7E7F
809596979899A2A3A4A58A8B8C8D8E8F90A6A7A8A981828384859A9B9C9D9E9F
A0A18687888991929394AAABACADAEAFB0B1B2B3B4B5B6B7B8B9BABBBCBDBEBF
C0D5D6D7D8D9E2E3E4E5CACBCCCDCECFD0E6E7E8E9C1C2C3C4C5DADBDCDDDEDF
E0E1C6C7C8C9D1D2D3D4EAEBECEDEEEFF0F1F2F3F4F5F6F7F8F9FAFBFCFDFEFF

Figure 1: Source member ROT13

D xlate PR 32767A VARYING

D input 32767A VARYING VALUE

D tbl 10A VALUE

Figure 2: Prototype for XLATE subprocedure (XLATEPRO)

*=================================================================

* To compile:

*

* CRTRPGMOD MODULE(XXX/XLATEMOD) SRCFILE(XXX/QRPGLESRC) +

* TEXT(‘String translation module’)

* CRTSRVPGM SRVPGM(XXX/XLATE) MODULE(XXX/XLATEMOD) +

* TEXT(‘String translation service program’) +

* EXPORT(*ALL) ACTGRP(*CALLER)

*

*=================================================================

H NOMAIN

* Note: In the /COPY statement that follows,

* change the library and file name to suit your needs.

/COPY MAGWORK/SOURCE,XLATEPRO

P xlate B EXPORT

D xlate PI 32767A VARYING

D input 32767A VARYING VALUE

D tbl 10A VALUE

D output S 32767A VARYING

D work S 32767A

D worklen S 5P 0

C EVAL worklen = %LEN(input)

C EVAL work = %SUBST(input:1:worklen)

C CALL ‘QDCXLATE’ 01

C PARM worklen

C PARM work

C PARM tbl

C IF *IN01

C EVAL output = X’FF’

C ELSE

C EVAL output = %SUBST(work:1:worklen)

C ENDIF

C RETURN output

P xlate E

Figure 3: Source for module XLATEMOD

*=================================================================

* To compile:

*

* CRTRPGMOD MODULE(XXX/XLATETEST) SRCFILE(XXX/QRPGLESRC) +

* TEXT(‘Test for XLATE’)

* CRTPGM PGM(XXX/XLATETEST) MODULE(XXX/XLATETEST) +

* TEXT(‘Test for XLATE’) +

* BNDSRVPGM(XXX/XLATE) ACTGRP(*CALLER)

*

*=================================================================

D input S 32A

D output S 32A

* Note: In the /COPY statement that follows,

* change the library and file name to suit your needs.

/COPY MAGWORK/SOURCE,XLATEPRO

C *ENTRY PLIST

C PARM input

C EVAL output = XLATE(input:’ROT13’)

C output DSPLY

C EVAL *INLR = *ON

Figure 4: Source for test program XLATETEST

BLOG COMMENTS POWERED BY DISQUS

LATEST COMMENTS

Support MC Press Online

$

Book Reviews

Resource Center

  • SB Profound WC 5536 Have you been wondering about Node.js? Our free Node.js Webinar Series takes you from total beginner to creating a fully-functional IBM i Node.js business application. You can find Part 1 here. In Part 2 of our free Node.js Webinar Series, Brian May teaches you the different tooling options available for writing code, debugging, and using Git for version control. Brian will briefly discuss the different tools available, and demonstrate his preferred setup for Node development on IBM i or any platform. Attend this webinar to learn:

  • SB Profound WP 5539More than ever, there is a demand for IT to deliver innovation. Your IBM i has been an essential part of your business operations for years. However, your organization may struggle to maintain the current system and implement new projects. The thousands of customers we've worked with and surveyed state that expectations regarding the digital footprint and vision of the company are not aligned with the current IT environment.

  • SB HelpSystems ROBOT Generic IBM announced the E1080 servers using the latest Power10 processor in September 2021. The most powerful processor from IBM to date, Power10 is designed to handle the demands of doing business in today’s high-tech atmosphere, including running cloud applications, supporting big data, and managing AI workloads. But what does Power10 mean for your data center? In this recorded webinar, IBMers Dan Sundt and Dylan Boday join IBM Power Champion Tom Huntington for a discussion on why Power10 technology is the right strategic investment if you run IBM i, AIX, or Linux. In this action-packed hour, Tom will share trends from the IBM i and AIX user communities while Dan and Dylan dive into the tech specs for key hardware, including:

  • Magic MarkTRY the one package that solves all your document design and printing challenges on all your platforms. Produce bar code labels, electronic forms, ad hoc reports, and RFID tags – without programming! MarkMagic is the only document design and print solution that combines report writing, WYSIWYG label and forms design, and conditional printing in one integrated product. Make sure your data survives when catastrophe hits. Request your trial now!  Request Now.

  • SB HelpSystems ROBOT GenericForms of ransomware has been around for over 30 years, and with more and more organizations suffering attacks each year, it continues to endure. What has made ransomware such a durable threat and what is the best way to combat it? In order to prevent ransomware, organizations must first understand how it works.

  • SB HelpSystems ROBOT GenericIT security is a top priority for businesses around the world, but most IBM i pros don’t know where to begin—and most cybersecurity experts don’t know IBM i. In this session, Robin Tatam explores the business impact of lax IBM i security, the top vulnerabilities putting IBM i at risk, and the steps you can take to protect your organization. If you’re looking to avoid unexpected downtime or corrupted data, you don’t want to miss this session.

  • SB HelpSystems ROBOT GenericCan you trust all of your users all of the time? A typical end user receives 16 malicious emails each month, but only 17 percent of these phishing campaigns are reported to IT. Once an attack is underway, most organizations won’t discover the breach until six months later. A staggering amount of damage can occur in that time. Despite these risks, 93 percent of organizations are leaving their IBM i systems vulnerable to cybercrime. In this on-demand webinar, IBM i security experts Robin Tatam and Sandi Moore will reveal:

  • FORTRA Disaster protection is vital to every business. Yet, it often consists of patched together procedures that are prone to error. From automatic backups to data encryption to media management, Robot automates the routine (yet often complex) tasks of iSeries backup and recovery, saving you time and money and making the process safer and more reliable. Automate your backups with the Robot Backup and Recovery Solution. Key features include:

  • FORTRAManaging messages on your IBM i can be more than a full-time job if you have to do it manually. Messages need a response and resources must be monitored—often over multiple systems and across platforms. How can you be sure you won’t miss important system events? Automate your message center with the Robot Message Management Solution. Key features include:

  • FORTRAThe thought of printing, distributing, and storing iSeries reports manually may reduce you to tears. Paper and labor costs associated with report generation can spiral out of control. Mountains of paper threaten to swamp your files. Robot automates report bursting, distribution, bundling, and archiving, and offers secure, selective online report viewing. Manage your reports with the Robot Report Management Solution. Key features include:

  • FORTRAFor over 30 years, Robot has been a leader in systems management for IBM i. With batch job creation and scheduling at its core, the Robot Job Scheduling Solution reduces the opportunity for human error and helps you maintain service levels, automating even the biggest, most complex runbooks. Manage your job schedule with the Robot Job Scheduling Solution. Key features include:

  • LANSA Business users want new applications now. Market and regulatory pressures require faster application updates and delivery into production. Your IBM i developers may be approaching retirement, and you see no sure way to fill their positions with experienced developers. In addition, you may be caught between maintaining your existing applications and the uncertainty of moving to something new.

  • LANSAWhen it comes to creating your business applications, there are hundreds of coding platforms and programming languages to choose from. These options range from very complex traditional programming languages to Low-Code platforms where sometimes no traditional coding experience is needed. Download our whitepaper, The Power of Writing Code in a Low-Code Solution, and:

  • LANSASupply Chain is becoming increasingly complex and unpredictable. From raw materials for manufacturing to food supply chains, the journey from source to production to delivery to consumers is marred with inefficiencies, manual processes, shortages, recalls, counterfeits, and scandals. In this webinar, we discuss how:

  • The MC Resource Centers bring you the widest selection of white papers, trial software, and on-demand webcasts for you to choose from. >> Review the list of White Papers, Trial Software or On-Demand Webcast at the MC Press Resource Center. >> Add the items to yru Cart and complet he checkout process and submit

  • Profound Logic Have you been wondering about Node.js? Our free Node.js Webinar Series takes you from total beginner to creating a fully-functional IBM i Node.js business application.

  • SB Profound WC 5536Join us for this hour-long webcast that will explore:

  • Fortra IT managers hoping to find new IBM i talent are discovering that the pool of experienced RPG programmers and operators or administrators with intimate knowledge of the operating system and the applications that run on it is small. This begs the question: How will you manage the platform that supports such a big part of your business? This guide offers strategies and software suggestions to help you plan IT staffing and resources and smooth the transition after your AS/400 talent retires. Read on to learn: