MCPcopy Create free account
hub / github.com/DeNA/PacketProxy / fromString

Method fromString

src/main/java/core/org/xbill/DNS/utils/base32.java:147–211  ·  view source on GitHub ↗

Convert a base32-encoded String to binary data @param str A String containing the encoded data @return An array containing the binary data, or null if the string is invalid

(String str)

Source from the content-addressed store, hash-verified

145 * @return An array containing the binary data, or null if the string is invalid
146 */
147public byte[]
148fromString(String str) {
149 ByteArrayOutputStream bs = new ByteArrayOutputStream();
150 byte [] raw = str.getBytes();
151 for (int i = 0; i < raw.length; i++)
152 {
153 char c = (char) raw[i];
154 if (!Character.isWhitespace(c)) {
155 c = Character.toUpperCase(c);
156 bs.write((byte) c);
157 }
158 }
159
160 if (padding) {
161 if (bs.size() % 8 != 0)
162 return null;
163 } else {
164 while (bs.size() % 8 != 0)
165 bs.write('=');
166 }
167
168 byte [] in = bs.toByteArray();
169
170 bs.reset();
171 DataOutputStream ds = new DataOutputStream(bs);
172
173 for (int i = 0; i < in.length / 8; i++) {
174 short[] s = new short[8];
175 int[] t = new int[5];
176
177 int padlen = 8;
178 for (int j = 0; j < 8; j++) {
179 char c = (char) in[i * 8 + j];
180 if (c == '=')
181 break;
182 s[j] = (short) alphabet.indexOf(in[i * 8 + j]);
183 if (s[j] < 0)
184 return null;
185 padlen--;
186 }
187 int blocklen = paddingToBlockLen(padlen);
188 if (blocklen < 0)
189 return null;
190
191 // all 5 bits of 1st, high 3 (of 5) of 2nd
192 t[0] = (s[0] << 3) | s[1] >> 2;
193 // lower 2 of 2nd, all 5 of 3rd, high 1 of 4th
194 t[1] = ((s[1] & 0x03) << 6) | (s[2] << 1) | (s[3] >> 4);
195 // lower 4 of 4th, high 4 of 5th
196 t[2] = ((s[3] & 0x0F) << 4) | ((s[4] >> 1) & 0x0F);
197 // lower 1 of 5th, all 5 of 6th, high 2 of 7th
198 t[3] = (s[4] << 7) | (s[5] << 2) | (s[6] >> 3);
199 // lower 3 of 7th, all of 8th
200 t[4] = ((s[6] & 0x07) << 5) | s[7];
201
202 try {
203 for (int j = 0; j < blocklen; j++)
204 ds.writeByte((byte) (t[j] & 0xFF));

Callers

nothing calls this directly

Calls 7

paddingToBlockLenMethod · 0.95
indexOfMethod · 0.80
getBytesMethod · 0.65
writeMethod · 0.65
sizeMethod · 0.45
toByteArrayMethod · 0.45
resetMethod · 0.45

Tested by

no test coverage detected