13
Wed, Nov
5 New Articles

When CCSID Constants Vary, Part II

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

Don't let a small thing like a literal string completely ruin your multi-national program.

 

The variations of CCSID are a vast topic and are in fact the main issue to consider during globalization of an application. You will find many pages on the Web on this topic (search for Unicode, CCSID, globalization, or national language support). Some of the most interesting are the ones from the IBM i Information Center.

 

In my previous article, I showed how a simple line of RPG code can turn a program haywire just because the JOB CCSID has changed. In this article, I will explain how to handle literal strings, which are sensitive to CCSID variations.

 

This simple line of RPG code made my MAIL program misbehave because the literal string '@' is hard-coded in the source code:

 

// * one @ is mandatory

i = %scan ('@':email);

 

 

The Coded Character Set Identifier (CCSID) is a table that assigns hexadecimal codes to a list of pictures (the picture of each printable character). Click for an example. This list of pictures is called the character set.

 

For more information about characters, globalization, and the i, have a look at the V5R2 iSeries Information Center. Yes, I said V5R2! Globalization is not really a new problem.

 

The character set decomposes itself in three parts:

 

  • The invariant character set
  • The portable character set
  • The remainder

 

The invariant character set is composed of the following characters:

 

0 1 2 3 4 5 6 7 8 9 - % & ( ) * , . / : ; ? _ ' " + < = > a A b B c C d D e E f F g G h H i I j J k K l L m M n N o O p P q Qr R s S t T u U v V w W x X y Y z Z

 

These characters almost always have the same hexadecimal code (there are some exceptions).

 

The invariant character set is named CCSID(640). The CCSID(640) contains all these characters, and solely these.

 

The portable character set is the character set that makes the C compilers crazy, in particular in the UNIX world. Details are in V5R2 iSeries Information Center

 

It is composed of these 13 characters: $^ ~ #@ [\] {|}! and the accent grave (`). Note: Wikipedia includes the invariant CSet into the portable CSet.

About My RPG Programs

  • In the MAIL program, the "at" sign @ is an issue.
  • In the PDF-generation program, the brackets ([ and ]) are an issue.
  • In the RTF-generation program, the braces ({ and }) are an issue.

 

All these characters are in the portable character set. The solution consists, therefore, in determining the correct value of the constants for the program, according to the CCSID of the job.

 

Constants can vary? Yes. Hence the title of this series of articles.

 

In all programs, in all constants, must appear only the characters that are in the invariant character set—what makes them effectively constants.

 

Yes, but for my @, what to do?

How Do I Fix the Problem?

To fix this problem, I need to have a correct @—that is, the correct hexadecimal value that corresponds to the @, depending on the actual CCSID of the job. Once I have the correct hexadecimal code of the @, I will be able to verify the email address without getting confusing error messages.

 

To get the correct value of the @ according to job, I need…

  • A known value that is "fixed" (meaning hard-coded, unchangeable)
  • An adaptation process

 

I'll describe that now.

The Solution, Data Side

I added these declarations into an /INCLUDE:

 

dPortableCharInz  ds                  qualified 

d   Dollar                       1    inz(x'5B')

d   AccentAcute                  1    inz(x'BE')

d   Caret                        1    inz(x'5F')

d   Tilde                        1    inz(x'A1')

d   NumberSign                   1    inz(x'7B')

d   AtSign                       1    inz(x'7C')

d   LeftBracket                  1    inz(x'4A')

d   BackSlash                    1    inz(x'E0')

d   RightBracket                 1    inz(x'5A')

d   LeftBrace                    1    inz(x'C0')

d   LogicalOr                    1    inz(x'BB')

d   RightBrace                   1    inz(x'D0')

d   ExclamationPoint...                         

d                                1    inz(x'4F')

d   CCSID                        5s 0 inz(500)  

 

and

 

d PortableChar    ds                  qualified

d   Dollar                       1    inz('_')

d   AccentAcute                  1    inz('_')

d   Caret                        1    inz('_')

d   Tilde                        1    inz('_')

d   NumberSign                   1    inz('_')

d   AtSign                       1    inz('_')

d   LeftBracket                  1    inz('_')

d   BackSlash                    1    inz('_')

d   RightBracket                 1    inz('_')

d   LeftBrace                    1    inz('_')

d   LogicalOr                    1    inz('_')

d   RightBrace                   1    inz('_')

d   ExclamationPoint...                       

d                                1            

d   CCSID                        5s 0 inz(0)

 

 

I also added into this /INCLUDE the prototypes for the IBM-supplied iConv API and the CONVCCSID procedure (a wrapper I wrote).

 

What is the objective?

 

PortableCharInz is in fact a hexadecimal constant whose values are chosen according to the CCSID 500. The CCSID 500 is the basic CCSID for America and West Europe. I could have coded this string as a constant, but it would be a lot less legible.

The Solution, Code Side

The idea is to use, at the beginning of the program, PortableCharInz (whose content is stationary, known, and in CCSID 500) to convert it to the current job CCSID.

 

Like this:

 

PortableChar = ConvCcsid(PortablecharInz.ccsid:0:PortableCharInz);  

 

From there, the modification to bring in the program MAIL is the following:

 

// * one @ is mandatory               

i = %scan(portablechar.AtSign:email);

 

And the program runs correctly again.

 

The complete RPG source code of ConvCcsid is in JP4INC.MBR here. The complete RPG code of MAIL is available here.

 

This second article has described the solution. We now are sure there is some method to take care of the Job CCSID. In the next (and final) article of this series, I will share some tips and tricks I found while solving the bug with the @.

 

Below, you will find the included prototypes for the IBM i APIs and my wrapper, copied from the include JP4INC.

 

These are the most important prototypes and data declarations:

 

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

 * Type definitions for Code Conversion APIs                       

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

D  iconv_t        ds                  based(pDummy)                

d                                     qualified                    

D   rc                          10i 0                              

D   cd                          10i 0 dim(12)                      

D iconvtoCode     ds                  qualified                     

D         ccsid                 10i 0 inz(500)                     

D         convA                 10i 0                              

D         subA                  10i 0                              

D         shftA                 10i 0                              

D         lnOpt                 10i 0                              

D         erOpt                 10i 0                              

D         res                   12a   inz(*ALLx'00')               

D iconvfromCode   ds                  qualified                    

D          ccsid                10i 0 inz(0)                       

D          convA                10i 0 inz(0)                       

D          subA                 10i 0 inz(0)                               

D          shftA                10i 0 inz(1)                              

D          lnOpt                10i 0 inz(0)                              

D          erOpt                10i 0 inz(0)                              

D          res                  12a   inz(*ALLx'00')                      

 

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

 * Prototype for iconv_open()--Code Conversion Allocation API             

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

D iconv_open      pr                  extproc('QtqIconvOpen') like(iconv_t)

D  pToCode                        *   value                               

D  pFromCode                      *   value                                

 

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

 * Prototype for iconv()--Code Conversion API                             

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

D iconv           pr            10i 0 extproc('iconv')                    

D   cd                                value  like(iconv_t)                

D   pInBuf                        *   const                               

D   inBytesLft                  10i 0                                     

D   pOutBuf                       *   const                               

D   outBytesLft                 10i 0                                     

 

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

 * Prototype for iconv_close()--Code Conversion Deallocation API   

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

D iconv_close     pr            10i 0 extproc('iconv_close')       

D   cd                                value  like(iconv_t)         

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

 

d convccsid       pr          1000    varying     

d                               10i 0 const       

d                               10i 0 const       

d                             1000    const varying

 

This is the procedure for ConvCCSID:

 

P convccsid       b                   export                   

d                 pi          1000    varying                

d  InCcsid                      10i 0 const                  

d OutCcsid                      10i 0 const                  

d  InString_p                 1000    const varying          

d  InString       s           1000    static varying         

d OutString       s           1000    static varying         

d inLen           s             10i 0 static                 

d OutLen          s             10i 0 inz(1000)              

D hIconv          ds                  likeds(iconv_t) inz    

d errcode         ds                  likeds(ErrorCodeHandler)

d                                     inz(*likeds)           

d rc              s             10i 0                        

d ToCCSID         s             10i 0                        

 /free                                                        

  ToCCSID = OutCCSID;                                        

  if ToCCSID = 0;                                            

  // system info                                             

     if not jp4.GotOsVersion;                                

     reset ErrorCodeHandler;                                 

     APIlen    = %size(PRDR0100) ;                              

     APIformat = 'PRDR0100'      ;                              

     osinfo='*OPSYS *CUR  0000*CODE     ' ;                     

     rtvprdinf(PRDR0100                                         

      : APIlen                                                  

      : APIformat                                                

      : osinfo                                                  

      : ErrorCodeHandler );                                     

        if (ErrorCodeHandler.available>0);                      

           message(ErrorCodeHandler.msgid:ErrorCodeHandler.msgdta

              :'':'QCPFMSG':'*ESCAPE');                         

        endif;                                                  

        jp4.gotosversion = true;                                

        jp4.OSVersion=prdr0100.Release_level;                   

     endif;                                                     

                                                                

     if jp4.OSVersion < 'V6R1M0';                               

     // get job info - iconv V5R4 does not handle ccsid 65535       

        if not jp4.GotJobi04 ;                                  

        reset ErrorCodeHandler;                                 

        RtvJobA ( JOBI0400                                        

               : %Size( JOBI0400 )                                    

               : 'JOBI0400'                                           

               : '*'                                                  

               : *Blank                                                

               : ECH                                                  

               );                                                     

           if (ErrorCodeHandler.available>0);                         

              message(ErrorCodeHandler.msgid:ErrorCodeHandler.msgdta  

                 :'':'QCPFMSG':'*ESCAPE');                            

           endif;                                                     

           jp4.GotJobi04  =true;                                      

           endif;                                                     

           if jobi0400.CodedcharactersetID=65535;             

              jp4.jobccsid=jobi0400.Defaultcodedcharactersetidentifier;

           else;                                                      

              jp4.jobccsid=jobi0400.CodedcharactersetID        ;      

           endif;                                                     

        ToCCSID = jp4.jobccsid;                                       

     else;                                                            

     // v6r1 handles ccsid 65535                               

     endif;                                                     

  endif;                                                       

                                                               

   iconvfromCode.ccsid =  InCcsid  ;                           

   iconvtoCode.ccsid =    ToCCSID    ;                          

   hIconv = iconv_open(%addr(iconvtoCode) :                    

                       %addr(iconvfromCode) ) ;                

   if hiconv.rc <> 0;                                          

     if errno() <> 0;                                           

        message(errnomsg(errno()):'':'*LIBL':'QCPFMSG':'*DIAG');

        return '';                                             

     endif;                                                    

   endif;                                                       

  if jp4.OSVersion < 'V6R1M0';                                 

  // iconv does not return the job ccsid                       

  else;                                                        

  if toccsid = 0;                                               

  jp4.jobccsid=HICONV.CD(2);                                   

  endif;                                                       

   InString = InString_P;                                    

   InLen = %len(InString)  ;                                 

   OutString = '';                                           

   %len(OutString)=OutLen;                                   

   rc = iconv(   hIconv :                                    

       %addr(InString   )+2 :                                

       Inlen :                                               

       %addr(OutString  )+2 :                                

       Outlen );                                             

    if rc< 0;                                                

       if c_errno <> 0;                                      

          message(errnomsg(c_errno):'':'*LIBL':'QCPFMSG') ;  

          return '';                                         

       endif;                                                

    endif;                                                   

    // outlen = first unused position into outstring

    %len(OutString)=%size(OutString)-OutLen-2;               

    iconv_close(   hiconv);                                  

  return OutString ;                                         

  begsr *pssr;                                                          

  monitor;                                                              

     dumpcallstack();                                                   

     // debug mode ?                                                    

     clear errcode;                                                     

     errcode.provided =%size(errcode);                                   

     debugmode.dbgattr='*DEBUGJOB';                                     

     debugging=true;                                                    

     RtvDbgAttr ( debugmode.DbgAttr :                                   

        debugmode.RtnAttr :                                             

        errcode );                                                      

     if (errcode.available>0);                                          

        if errcode.msgid   ='CPF9541';                                  

           debugging=false;                                             

        else;                                                           

           message(errcode.msgid :errcode.msgdta  :'':'QCPFMSG':'*DIAG');

        endif;                                                          

     endif;                                                             

     if debugging ;                                                     

        dump;                                                            

     endif;    

     on-error; 

     endmon;   

  endsr;       

 /end-free     

P                 e

 

as/400, os/400, iseries, system i, i5/os, ibm i, power systems, 6.1, 7.1, V7, V6R1

Jean-Paul Lamontre

Jean-Paul Lamontre has been working on IBM machines since 1976. His first was a 3/15 with 128K RAM (the biggest machine of the county). His first program was an RPG program, no more than 15 lines. It never compiled, and nobody ever understood why.

 

Currently, Jean-Paul divides has work time between two companies.

 

For Cilasoft, which offers the Cilasoft Audit and Security suite, he is the director of development. The Cilasoft suite is a cornerstore to any company's compliance process.

 

For Resolution, which offers Xcase, a database engineering suite, he is the CTO of the IBM i department. Xcase allows developers to modernize a DDS database to DDL, discover and implement implicit relationships, and manage SQL databases using an advanced GUI.

 

Jean-Paul also publishes some free tools on his personal Web site. Most popular are SQL2XLS, SPLF2PDF, and MAIL.

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: