forked from cSploit/android
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathByteBuffer.java
More file actions
143 lines (110 loc) · 3.34 KB
/
Copy pathByteBuffer.java
File metadata and controls
143 lines (110 loc) · 3.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
* This file is part of the dSploit.
*
* Copyleft of Simone Margaritelli aka evilsocket <evilsocket@gmail.com>
*
* dSploit is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* dSploit is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with dSploit. If not, see <http://www.gnu.org/licenses/>.
*/
package org.csploit.android.net;
import org.csploit.android.core.System;
import org.mozilla.universalchardet.UniversalDetector;
import java.io.UnsupportedEncodingException;
import java.util.Arrays;
public class ByteBuffer{
private byte[] mBuffer = null;
private UniversalDetector mCharsetDetector = null;
public ByteBuffer(){
mCharsetDetector = new UniversalDetector(null);
}
public ByteBuffer(byte[] buffer){
mCharsetDetector = new UniversalDetector(null);
setData(buffer);
}
public void replace(byte[] search, byte[] replace){
int idx = indexOf(search),
slen = search.length,
rlen = replace.length;
byte[] left, right;
ByteBuffer buffer;
while(idx != -1){
left = Arrays.copyOfRange(mBuffer, 0, idx);
right = Arrays.copyOfRange(mBuffer, idx + slen, mBuffer.length);
buffer = new ByteBuffer(left);
buffer.append(replace, rlen);
buffer.append(right, right.length);
mBuffer = buffer.mBuffer;
idx = indexOf(search);
}
}
public void append(byte[] buffer, int length){
byte[] chunk = Arrays.copyOfRange(buffer, 0, length),
reallcd;
int i, j;
mCharsetDetector.handleData(buffer, 0, length);
if(mBuffer == null)
mBuffer = chunk;
else{
reallcd = new byte[mBuffer.length + length];
for(i = 0; i < mBuffer.length; i++)
reallcd[i] = mBuffer[i];
for(j = 0; j < length; i++, j++)
reallcd[i] = chunk[j];
mBuffer = reallcd;
}
}
public int indexOf(byte[] pattern, int start){
int i, j, plen = pattern.length, stop = mBuffer.length - plen;
loop:
for(i = start; i < stop; i++){
if(pattern[0] == mBuffer[i]){
for(j = 1; j < plen; j++){
if(pattern[j] != mBuffer[i + j])
continue loop;
}
return i;
}
}
return -1;
}
public int indexOf(byte[] pattern){
return indexOf(pattern, 0);
}
public String toString(){
mCharsetDetector.dataEnd();
try{
if(isEmpty())
return "";
else{
String charset = mCharsetDetector.getDetectedCharset();
return new String(mBuffer, charset != null ? charset : "UTF-8");
}
} catch(UnsupportedEncodingException e){
System.errorLogging(e);
}
return new String(mBuffer);
}
public byte[] getData(){
return mBuffer;
}
public void setData(byte[] buffer){
mBuffer = buffer;
mCharsetDetector.handleData(buffer, 0, buffer.length);
}
public int getLength(){
return (mBuffer == null ? 0 : mBuffer.length);
}
public boolean isEmpty(){
return getLength() == 0;
}
}