-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathanycall.cpp
executable file
·104 lines (81 loc) · 2.71 KB
/
anycall.cpp
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
/*
* Copyright 2016 xiaokangwang
* Copyright 2016-2017 Alex Zhang aka. ztc1997
*
* This program 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.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/
#define LOG_TAG "anycall"
#include <stdlib.h>
#include <binder/IBinder.h>
#include <binder/BpBinder.h>
#include <binder/IServiceManager.h>
#include <binder/IPCThreadState.h>
#include "base64.h"
#include "retval.h"
using namespace android;
unsigned char *callMethod(sp<IBinder> binder, int code, char *encodedDate)
{
size_t size;
unsigned char *result = base64_decode(reinterpret_cast<const unsigned char *>(encodedDate),
strlen(encodedDate), &size);
Parcel data, reply;
data.setDataSize(size);
data.setDataPosition(0);
void *raw = data.writeInplace(size);
memmove(raw, result, size);
status_t st = binder->remoteBinder()->transact(code, data, &reply);
unsigned char *output = base64_encode(reply.data(), reply.dataSize(), &size);
return output;
}
int main(int argc, char **argv)
{
if (argc < 2)
{
printf("Missing parameters.\n");
return ERROR_MISSING_PARAMETERS;
}
String16 token = String16(argv[1]);
sp<IServiceManager> sm = defaultServiceManager();
if (sm == 0)
{
printf("Failed to get ServiceManager.\n");
return ERROR_FAILED_TO_GET_SERVICE_MANAGER;
}
sp<IBinder> binder = sm->getService(String16(token));
if (binder == 0)
{
printf("Failed to get service, the service may not be running.\n");
return ERROR_FAILED_TO_GET_SERVICE;
}
if (argc == 4)
{
int code = atoi(argv[2]);
char *encodedDate = argv[3];
unsigned char *output = callMethod(binder, code, encodedDate);
printf("%s\n", output);
return 0;
}
for (;;)
{
char input[256];
fgets(input, sizeof(input), stdin);
if (strcmp(strtok(input, "\n"), "exit") == 0)
break;
int code = atoi(strtok(input, " "));
char *encodedDate = strtok(NULL, "\n");
unsigned char *output = callMethod(binder, code, encodedDate);
printf("%s\n", output);
}
return 0;
}