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    /** Default constructor. */
034    public IFEB_LLLNUM () {
035        super();
036    }
037    /**
038     * Constructs a packager with the given length and description.
039     * @param len - field len
040     * @param description symbolic descrption
041     */
042    public IFEB_LLLNUM (int len, String description) {
043        super(len, description);
044    }
045    /**
046     * @param c - a component
047     * @return packed component
048     * @exception ISOException on ISO processing error
049     */
050    public byte[] pack(ISOComponent c) throws ISOException {
051        boolean odd = false;
052        int len;
053        String s = (String) c.getValue();
054        
055        
056        
057        if ((len=s.length()) > getLength() || len>99)   // paranoia settings
058            throw new ISOException(
059            "invalid len "+len +" packing IFEB_LLLNUM field " + c.getKey());
060        
061        
062        //System.out.println("String s = (String) c.getValue(); "+s);
063        
064        
065        
066        // if odd length
067        if ( len%2 ==1 ) {
068            odd = true;
069            len = len/2 +1;
070        } else {
071            odd = false;
072            len = len/2;
073        }
074        
075        //System.out.println("len= "+ len +" s.length()= "+len);
076        
077        String fieldLength = ISOUtil.zeropad(Integer.toString(len), 3);
078        
079        byte [] EBCDIClength = ISOUtil.asciiToEbcdic(fieldLength);
080
081        // bcd stuff
082        byte[] bcd = ISOUtil.str2bcd(s, false);
083        if(odd)
084            bcd[len-1] = (byte) (bcd[len-1] | 0xf);
085        
086
087        //System.out.println("bcd2str "+ISOUtil.bcd2str(bcd, 0, bcd.length*2, false) );
088        
089        byte[] b   = new byte[bcd.length + 3];
090        
091        b[0] = EBCDIClength[0];
092        b[1] = EBCDIClength[1];
093        b[2] = EBCDIClength[2];
094        System.arraycopy(bcd, 0, b, 3, bcd.length);
095        
096        return b;
097    }
098    /**
099     * @param c - the Component to unpack
100     * @param b - binary image
101     * @param offset - starting offset within the binary image
102     * @return consumed bytes
103     * @exception ISOException on ISO processing error
104     */
105    public int unpack(ISOComponent c, byte[] b, int offset)
106    throws ISOException {
107        boolean pad = false;
108        
109        
110        int len = (b[offset] & 0x0f) * 100 + (b[offset+1] & 0x0f) * 10 + (b[offset+2] & 0x0f);
111        
112        int tempLen = len*2;
113
114        //System.out.println("len "+ len +"len*2 "+tempLen);
115        
116        
117        // odd handling
118        byte testByte = b[offset+3+len-1];
119        
120        if( (testByte | 0xf0)== 0xff) {
121            // odd length
122            tempLen--;
123        }
124        
125        
126        // bcd line
127        //System.out.println("ISOUtil.bcd2str(b, offset+2, len, pad) "+ISOUtil.bcd2str(b, offset+2, tempLen, pad));
128        
129        c.setValue(ISOUtil.bcd2str(b, offset+3, tempLen, pad));
130        
131        //c.setValue(ISOUtil.ebcdicToAscii(b, offset+2, len));
132        
133        return len+3;
134    }
135    
136    public int getMaxPackedLength() {
137        return getLength()+3;
138    }
139}