Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
com.ccwangluo.accelerator
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
sheteng
com.ccwangluo.accelerator
Commits
4579ba1a
Commit
4579ba1a
authored
Dec 27, 2012
by
Max Lv
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix #1 use merge sort instead
parent
de894f08
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
122 additions
and
16 deletions
+122
-16
jni/shadowsocks/encrypt.c
jni/shadowsocks/encrypt.c
+122
-16
No files found.
jni/shadowsocks/encrypt.c
View file @
4579ba1a
...
...
@@ -3,10 +3,124 @@
#include <openssl/md5.h>
typedef
unsigned
char
uint8_t
;
typedef
unsigned
int
uint32_t
;
typedef
unsigned
long
long
uint64_t
;
static
int
random_compare
(
const
void
*
_x
,
const
void
*
_y
)
{
uint32_t
i
=
_i
;
uint64_t
a
=
_a
;
uint8_t
x
=
*
((
uint8_t
*
)
_x
);
uint8_t
y
=
*
((
uint8_t
*
)
_y
);
return
(
a
%
(
x
+
i
)
-
a
%
(
y
+
i
));
}
static
void
merge
(
uint8_t
*
left
,
int
llength
,
uint8_t
*
right
,
int
rlength
)
{
/* Temporary memory locations for the 2 segments of the array to merge. */
uint8_t
*
ltmp
=
(
uint8_t
*
)
malloc
(
llength
*
sizeof
(
uint8_t
));
uint8_t
*
rtmp
=
(
uint8_t
*
)
malloc
(
rlength
*
sizeof
(
uint8_t
));
/*
* Pointers to the elements being sorted in the temporary memory locations.
*/
uint8_t
*
ll
=
ltmp
;
uint8_t
*
rr
=
rtmp
;
uint8_t
*
result
=
left
;
/*
* Copy the segment of the array to be merged into the temporary memory
* locations.
*/
memcpy
(
ltmp
,
left
,
llength
*
sizeof
(
uint8_t
));
memcpy
(
rtmp
,
right
,
rlength
*
sizeof
(
uint8_t
));
while
(
llength
>
0
&&
rlength
>
0
)
{
if
(
random_compare
(
ll
,
rr
)
<=
0
)
{
/*
* Merge the first element from the left back into the main array
* if it is smaller or equal to the one on the right.
*/
*
result
=
*
ll
;
++
ll
;
--
llength
;
}
else
{
/*
* Merge the first element from the right back into the main array
* if it is smaller than the one on the left.
*/
*
result
=
*
rr
;
++
rr
;
--
rlength
;
}
++
result
;
}
/*
* All the elements from either the left or the right temporary array
* segment have been merged back into the main array. Append the remaining
* elements from the other temporary array back into the main array.
*/
if
(
llength
>
0
)
while
(
llength
>
0
)
{
/* Appending the rest of the left temporary array. */
*
result
=
*
ll
;
++
result
;
++
ll
;
--
llength
;
}
else
while
(
rlength
>
0
)
{
/* Appending the rest of the right temporary array. */
*
result
=
*
rr
;
++
result
;
++
rr
;
--
rlength
;
}
/* Release the memory used for the temporary arrays. */
free
(
ltmp
);
free
(
rtmp
);
}
static
void
mergesort
(
uint8_t
array
[],
int
length
)
{
/* This is the middle index and also the length of the right array. */
uint8_t
middle
;
/*
* Pointers to the beginning of the left and right segment of the array
* to be merged.
*/
uint8_t
*
left
,
*
right
;
/* Length of the left segment of the array to be merged. */
int
llength
;
if
(
length
<=
1
)
return
;
/* Let integer division truncate the value. */
middle
=
length
/
2
;
llength
=
length
-
middle
;
/*
* Set the pointers to the appropriate segments of the array to be merged.
*/
left
=
array
;
right
=
array
+
llength
;
mergesort
(
left
,
llength
);
mergesort
(
right
,
middle
);
merge
(
left
,
llength
,
right
,
middle
);
}
void
encrypt
(
char
*
buf
,
int
len
)
{
char
*
end
=
buf
+
len
;
while
(
buf
<
end
)
{
*
buf
=
(
char
)
encrypt_table
[(
u
nsigned
char
)
*
buf
];
*
buf
=
(
char
)
encrypt_table
[(
u
int8_t
)
*
buf
];
buf
++
;
}
}
...
...
@@ -14,7 +128,7 @@ void encrypt(char *buf, int len) {
void
decrypt
(
char
*
buf
,
int
len
)
{
char
*
end
=
buf
+
len
;
while
(
buf
<
end
)
{
*
buf
=
(
char
)
decrypt_table
[(
u
nsigned
char
)
*
buf
];
*
buf
=
(
char
)
decrypt_table
[(
u
int8_t
)
*
buf
];
buf
++
;
}
}
...
...
@@ -34,27 +148,19 @@ int recv_decrypt(int sock, char *buf, int len, int flags) {
return
result
;
}
static
int
random_compare
(
const
void
*
_x
,
const
void
*
_y
)
{
unsigned
int
i
=
_i
;
unsigned
long
long
a
=
_a
;
unsigned
char
x
=
*
((
unsigned
char
*
)
_x
);
unsigned
char
y
=
*
((
unsigned
char
*
)
_y
);
return
(
a
%
(
x
+
i
)
-
a
%
(
y
+
i
));
}
void
get_table
(
const
char
*
key
)
{
u
nsigned
char
*
table
=
encrypt_table
;
u
nsigned
char
*
tmp_hash
;
tmp_hash
=
MD5
((
const
u
nsigned
char
*
)
key
,
strlen
(
key
),
NULL
);
_a
=
*
(
u
nsigned
long
long
*
)
tmp_hash
;
u
nsigned
in
t
i
;
u
int8_t
*
table
=
encrypt_table
;
u
int8_t
*
tmp_hash
;
tmp_hash
=
MD5
((
const
u
int8_t
*
)
key
,
strlen
(
key
),
NULL
);
_a
=
*
(
u
int64_t
*
)
tmp_hash
;
u
int32_
t
i
;
for
(
i
=
0
;
i
<
256
;
++
i
)
{
table
[
i
]
=
i
;
}
for
(
i
=
1
;
i
<
1024
;
++
i
)
{
_i
=
i
;
qsort
(
table
,
256
,
sizeof
(
unsigned
char
),
random_compare
);
mergesort
(
table
,
256
);
}
for
(
i
=
0
;
i
<
256
;
++
i
)
{
// gen decrypt table from encrypt table
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment