001/*
002 * jPOS Project [http://jpos.org]
003 * Copyright (C) 2000-2026 jPOS Software SRL
004 *
005 * This program is free software: you can redistribute it and/or modify
006 * it under the terms of the GNU Affero General Public License as
007 * published by the Free Software Foundation, either version 3 of the
008 * License, or (at your option) any later version.
009 *
010 * This program is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
013 * GNU Affero General Public License for more details.
014 *
015 * You should have received a copy of the GNU Affero General Public License
016 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
017 */
018
019package org.jpos.iso;
020
021/**
022 * EBCDIC version of IFB_LLLNUM
023 * Uses a 2 EBCDIC byte length field
024 *
025 * @author julien.moebs@paybox.net
026 * @version $Id$
027 * @see ISOFieldPackager
028 * @see ISOComponent
029 */
030
031
032public class IFEB_LLLNUM extends ISOFieldPackager {
033    public IFEB_LLLNUM () {
034        super();
035    }
036    /**
037     * @param len - field len
038     * @param description symbolic descrption
039     */
040    public IFEB_LLLNUM (int len, String description) {
041        super(len, description);
042    }
043    /**
044     * @param c - a component
045     * @return packed component
046     * @exception ISOException
047     */
048    public byte[] pack(ISOComponent c) throws ISOException {
049        boolean odd = false;
050        int len;
051        String s = (String) c.getValue();
052        
053        
054        
055        if ((len=s.length()) > getLength() || len>99)   // paranoia settings
056            throw new ISOException(
057            "invalid len "+len +" packing IFEB_LLLNUM field " + c.getKey());
058        
059        
060        //System.out.println("String s = (String) c.getValue(); "+s);
061        
062        
063        
064        // if odd length
065        if ( len%2 ==1 ) {
066            odd = true;
067            len = len/2 +1;
068        } else {
069            odd = false;
070            len = len/2;
071        }
072        
073        //System.out.println("len= "+ len +" s.length()= "+len);
074        
075        String fieldLength = ISOUtil.zeropad(Integer.toString(len), 3);
076        
077        byte [] EBCDIClength = ISOUtil.asciiToEbcdic(fieldLength);
078
079        // bcd stuff
080        byte[] bcd = ISOUtil.str2bcd(s, false);
081        if(odd)
082            bcd[len-1] = (byte) (bcd[len-1] | 0xf);
083        
084
085        //System.out.println("bcd2str "+ISOUtil.bcd2str(bcd, 0, bcd.length*2, false) );
086        
087        byte[] b   = new byte[bcd.length + 3];
088        
089        b[0] = EBCDIClength[0];
090        b[1] = EBCDIClength[1];
091        b[2] = EBCDIClength[2];
092        System.arraycopy(bcd, 0, b, 3, bcd.length);
093        
094        return b;
095    }
096    /**
097     * @param c - the Component to unpack
098     * @param b - binary image
099     * @param offset - starting offset within the binary image
100     * @return consumed bytes
101     * @exception ISOException
102     */
103    public int unpack(ISOComponent c, byte[] b, int offset)
104    throws ISOException {
105        boolean pad = false;
106        
107        
108        int len = (b[offset] & 0x0f) * 100 + (b[offset+1] & 0x0f) * 10 + (b[offset+2] & 0x0f);
109        
110        int tempLen = len*2;
111
112        //System.out.println("len "+ len +"len*2 "+tempLen);
113        
114        
115        // odd handling
116        byte testByte = b[offset+3+len-1];
117        
118        if( (testByte | 0xf0)== 0xff) {
119            // odd length
120            tempLen--;
121        }
122        
123        
124        // bcd line
125        //System.out.println("ISOUtil.bcd2str(b, offset+2, len, pad) "+ISOUtil.bcd2str(b, offset+2, tempLen, pad));
126        
127        c.setValue(ISOUtil.bcd2str(b, offset+3, tempLen, pad));
128        
129        //c.setValue(ISOUtil.ebcdicToAscii(b, offset+2, len));
130        
131        return len+3;
132    }
133    
134    public int getMaxPackedLength() {
135        return getLength()+3;
136    }
137}