001    /*
002     *  Licensed to the Apache Software Foundation (ASF) under one or more
003     *  contributor license agreements.  See the NOTICE file distributed with
004     *  this work for additional information regarding copyright ownership.
005     *  The ASF licenses this file to You under the Apache License, Version 2.0
006     *  (the "License"); you may not use this file except in compliance with
007     *  the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     *  Unless required by applicable law or agreed to in writing, software
012     *  distributed under the License is distributed on an "AS IS" BASIS,
013     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     *  See the License for the specific language governing permissions and
015     *  limitations under the License.
016     *
017     */
018    
019    /*
020     * This package is based on the work done by Timothy Gerard Endres
021     * (time@ice.com) to whom the Ant project is very grateful for his great code.
022     */
023    
024    package org.apache.activemq.console.command.store.tar;
025    
026    /**
027     * This class provides static utility methods to work with byte streams.
028     *
029     */
030    // CheckStyle:HideUtilityClassConstructorCheck OFF (bc)
031    public class TarUtils {
032    
033        private static final int BYTE_MASK = 255;
034    
035        /**
036         * Parse an octal string from a header buffer. This is used for the
037         * file permission mode value.
038         *
039         * @param header The header buffer from which to parse.
040         * @param offset The offset into the buffer from which to parse.
041         * @param length The number of header bytes to parse.
042         * @return The long value of the octal string.
043         */
044        public static long parseOctal(byte[] header, int offset, int length) {
045            long    result = 0;
046            boolean stillPadding = true;
047            int     end = offset + length;
048    
049            for (int i = offset; i < end; ++i) {
050                if (header[i] == 0) {
051                    break;
052                }
053    
054                if (header[i] == (byte) ' ' || header[i] == '0') {
055                    if (stillPadding) {
056                        continue;
057                    }
058    
059                    if (header[i] == (byte) ' ') {
060                        break;
061                    }
062                }
063    
064                stillPadding = false;
065                // CheckStyle:MagicNumber OFF
066                result = (result << 3) + (header[i] - '0');
067                // CheckStyle:MagicNumber ON
068            }
069    
070            return result;
071        }
072    
073        /**
074         * Parse an entry name from a header buffer.
075         *
076         * @param header The header buffer from which to parse.
077         * @param offset The offset into the buffer from which to parse.
078         * @param length The number of header bytes to parse.
079         * @return The header's entry name.
080         */
081        public static StringBuffer parseName(byte[] header, int offset, int length) {
082            StringBuffer result = new StringBuffer(length);
083            int          end = offset + length;
084    
085            for (int i = offset; i < end; ++i) {
086                if (header[i] == 0) {
087                    break;
088                }
089    
090                result.append((char) header[i]);
091            }
092    
093            return result;
094        }
095    
096        /**
097         * Determine the number of bytes in an entry name.
098         *
099         * @param name The header name from which to parse.
100         * @param buf The buffer from which to parse.
101         * @param offset The offset into the buffer from which to parse.
102         * @param length The number of header bytes to parse.
103         * @return The number of bytes in a header's entry name.
104         */
105        public static int getNameBytes(StringBuffer name, byte[] buf, int offset, int length) {
106            int i;
107    
108            for (i = 0; i < length && i < name.length(); ++i) {
109                buf[offset + i] = (byte) name.charAt(i);
110            }
111    
112            for (; i < length; ++i) {
113                buf[offset + i] = 0;
114            }
115    
116            return offset + length;
117        }
118    
119        /**
120         * Parse an octal integer from a header buffer.
121         *
122         * @param value The header value
123         * @param buf The buffer from which to parse.
124         * @param offset The offset into the buffer from which to parse.
125         * @param length The number of header bytes to parse.
126         * @return The integer value of the octal bytes.
127         */
128        public static int getOctalBytes(long value, byte[] buf, int offset, int length) {
129            int    idx = length - 1;
130    
131            buf[offset + idx] = 0;
132            --idx;
133            buf[offset + idx] = (byte) ' ';
134            --idx;
135    
136            if (value == 0) {
137                buf[offset + idx] = (byte) '0';
138                --idx;
139            } else {
140                for (long val = value; idx >= 0 && val > 0; --idx) {
141                    // CheckStyle:MagicNumber OFF
142                    buf[offset + idx] = (byte) ((byte) '0' + (byte) (val & 7));
143                    val = val >> 3;
144                    // CheckStyle:MagicNumber ON
145                }
146            }
147    
148            for (; idx >= 0; --idx) {
149                buf[offset + idx] = (byte) ' ';
150            }
151    
152            return offset + length;
153        }
154    
155        /**
156         * Parse an octal long integer from a header buffer.
157         *
158         * @param value The header value
159         * @param buf The buffer from which to parse.
160         * @param offset The offset into the buffer from which to parse.
161         * @param length The number of header bytes to parse.
162         * @return The long value of the octal bytes.
163         */
164        public static int getLongOctalBytes(long value, byte[] buf, int offset, int length) {
165            byte[] temp = new byte[length + 1];
166    
167            getOctalBytes(value, temp, 0, length + 1);
168            System.arraycopy(temp, 0, buf, offset, length);
169    
170            return offset + length;
171        }
172    
173        /**
174         * Parse the checksum octal integer from a header buffer.
175         *
176         * @param value The header value
177         * @param buf The buffer from which to parse.
178         * @param offset The offset into the buffer from which to parse.
179         * @param length The number of header bytes to parse.
180         * @return The integer value of the entry's checksum.
181         */
182        public static int getCheckSumOctalBytes(long value, byte[] buf, int offset, int length) {
183            getOctalBytes(value, buf, offset, length);
184    
185            buf[offset + length - 1] = (byte) ' ';
186            buf[offset + length - 2] = 0;
187    
188            return offset + length;
189        }
190    
191        /**
192         * Compute the checksum of a tar entry header.
193         *
194         * @param buf The tar entry's header buffer.
195         * @return The computed checksum.
196         */
197        public static long computeCheckSum(byte[] buf) {
198            long sum = 0;
199    
200            for (int i = 0; i < buf.length; ++i) {
201                sum += BYTE_MASK & buf[i];
202            }
203    
204            return sum;
205        }
206    }