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.tools.tar;
025:
026: /**
027: * This class provides static utility methods to work with byte streams.
028: *
029: */
030: public class TarUtils {
031:
032: /**
033: * Parse an octal string from a header buffer. This is used for the
034: * file permission mode value.
035: *
036: * @param header The header buffer from which to parse.
037: * @param offset The offset into the buffer from which to parse.
038: * @param length The number of header bytes to parse.
039: * @return The long value of the octal string.
040: */
041: public static long parseOctal(byte[] header, int offset, int length) {
042: long result = 0;
043: boolean stillPadding = true;
044: int end = offset + length;
045:
046: for (int i = offset; i < end; ++i) {
047: if (header[i] == 0) {
048: break;
049: }
050:
051: if (header[i] == (byte) ' ' || header[i] == '0') {
052: if (stillPadding) {
053: continue;
054: }
055:
056: if (header[i] == (byte) ' ') {
057: break;
058: }
059: }
060:
061: stillPadding = false;
062: result = (result << 3) + (header[i] - '0');
063: }
064:
065: return result;
066: }
067:
068: /**
069: * Parse an entry name from a header buffer.
070: *
071: * @param header The header buffer from which to parse.
072: * @param offset The offset into the buffer from which to parse.
073: * @param length The number of header bytes to parse.
074: * @return The header's entry name.
075: */
076: public static StringBuffer parseName(byte[] header, int offset,
077: int length) {
078: StringBuffer result = new StringBuffer(length);
079: int end = offset + length;
080:
081: for (int i = offset; i < end; ++i) {
082: if (header[i] == 0) {
083: break;
084: }
085:
086: result.append((char) header[i]);
087: }
088:
089: return result;
090: }
091:
092: /**
093: * Determine the number of bytes in an entry name.
094: *
095: * @param name The header name from which to parse.
096: * @param buf The buffer from which to parse.
097: * @param offset The offset into the buffer from which to parse.
098: * @param length The number of header bytes to parse.
099: * @return The number of bytes in a header's entry name.
100: */
101: public static int getNameBytes(StringBuffer name, byte[] buf,
102: int offset, int length) {
103: int i;
104:
105: for (i = 0; i < length && i < name.length(); ++i) {
106: buf[offset + i] = (byte) name.charAt(i);
107: }
108:
109: for (; i < length; ++i) {
110: buf[offset + i] = 0;
111: }
112:
113: return offset + length;
114: }
115:
116: /**
117: * Parse an octal integer from a header buffer.
118: *
119: * @param value The header value
120: * @param buf The buffer from which to parse.
121: * @param offset The offset into the buffer from which to parse.
122: * @param length The number of header bytes to parse.
123: * @return The integer value of the octal bytes.
124: */
125: public static int getOctalBytes(long value, byte[] buf, int offset,
126: int length) {
127: int idx = length - 1;
128:
129: buf[offset + idx] = 0;
130: --idx;
131: buf[offset + idx] = (byte) ' ';
132: --idx;
133:
134: if (value == 0) {
135: buf[offset + idx] = (byte) '0';
136: --idx;
137: } else {
138: for (long val = value; idx >= 0 && val > 0; --idx) {
139: buf[offset + idx] = (byte) ((byte) '0' + (byte) (val & 7));
140: val = val >> 3;
141: }
142: }
143:
144: for (; idx >= 0; --idx) {
145: buf[offset + idx] = (byte) ' ';
146: }
147:
148: return offset + length;
149: }
150:
151: /**
152: * Parse an octal long integer from a header buffer.
153: *
154: * @param value The header value
155: * @param buf The buffer from which to parse.
156: * @param offset The offset into the buffer from which to parse.
157: * @param length The number of header bytes to parse.
158: * @return The long value of the octal bytes.
159: */
160: public static int getLongOctalBytes(long value, byte[] buf,
161: int offset, int length) {
162: byte[] temp = new byte[length + 1];
163:
164: getOctalBytes(value, temp, 0, length + 1);
165: System.arraycopy(temp, 0, buf, offset, length);
166:
167: return offset + length;
168: }
169:
170: /**
171: * Parse the checksum octal integer from a header buffer.
172: *
173: * @param value The header value
174: * @param buf The buffer from which to parse.
175: * @param offset The offset into the buffer from which to parse.
176: * @param length The number of header bytes to parse.
177: * @return The integer value of the entry's checksum.
178: */
179: public static int getCheckSumOctalBytes(long value, byte[] buf,
180: int offset, int length) {
181: getOctalBytes(value, buf, offset, length);
182:
183: buf[offset + length - 1] = (byte) ' ';
184: buf[offset + length - 2] = 0;
185:
186: return offset + length;
187: }
188:
189: /**
190: * Compute the checksum of a tar entry header.
191: *
192: * @param buf The tar entry's header buffer.
193: * @return The computed checksum.
194: */
195: public static long computeCheckSum(byte[] buf) {
196: long sum = 0;
197:
198: for (int i = 0; i < buf.length; ++i) {
199: sum += 255 & buf[i];
200: }
201:
202: return sum;
203: }
204: }
|