Hacking Android (pipes) stdout and stderr redirection

Hacking Android (pipes) stdout and stderr redirection

Postby Antonio Linares » Mon Mar 04, 2019 7:23 am

https://codelab.wordpress.com/2014/11/03/how-to-use-standard-output-streams-for-logging-in-android-apps/

https://www.bignerdranch.com/blog/hijacking-for-fun-and-profit/

I have found this great code from Oscar Hernández Suárez:
https://groups.google.com/d/msg/harbour-users/oktsDgxmSKs/1YNBsuvHM6kJ

hb_processOpen() lets us manage pipes from Harbour

Hello.

This is an proof of concept of using an interface to communicate with a Java application using JSON.

----8<----
REQUEST HB_GT_STD_DEFAULT

PROCEDURE Main()
LOCAL hProcess
LOCAL hStdIn
LOCAL hStdOut
LOCAL hStdErr

LOCAL nSize := 1024
LOCAL cBuffer

LOCAL nReadedErr
LOCAL nReadedOut

LOCAL hInput := { => }
LOCAL n
LOCAL hPerson
LOCAL hDecode

LOCAL cInput
LOCAL cError := ""
LOCAL cOutput := ""

hInput[ "id" ] := 1001
hInput[ "list" ] := {}

FOR n := 1 TO 1000
hPerson := { => }
hPerson[ "name" ] := "John"
hPerson[ "surname" ] := "Smith" + Chr( 13 ) + Chr( 10 ) + " and Smith"

AAdd( hInput[ "list" ], hPerson )
NEXT

?
BEGIN SEQUENCE
cInput := hb_JsonEncode( hInput, .f. ) + Chr( 10 )
? "INPUT:", Len( cInput )

hProcess := hb_processOpen( "java.exe -cp JSON.jar;. HJAVA", @hStdIn, @hStdOut, @hStdErr, .t. )
? "WRITE:", FWrite( hStdIn, cInput )

DO WHILE .T.
? "WAITING..."
cBuffer := Space( nSize )
nReadedErr := hb_PRead( hStdErr, @cBuffer, nSize, 0 )

IF nReadedErr > 0
cErr += Left( cBuffer, nReadedErr )
ENDIF

cBuffer := Space( nSize )
nReadedOut := hb_PRead( hStdOut, @cBuffer, nSize, 0 )

IF nReadedOut > 0
cOutput += Left( cBuffer, nReadedOut )
ENDIF

IF nReadedErr < 0 .AND. nReadedOut < 0
EXIT
ENDIF
ENDDO

IF Len( cOutput ) > 0
hDecode := { => }
hb_JsonDecode( cOutput, @hDecode )
? hb_JsonEncode( hDecode, .t. )
ENDIF
IF Len( cError ) > 0
? cError
ENDIF

ALWAYS

hb_processClose( hProcess, .t. ) // kill if needed
? "ret=" + hb_ntos( hb_processValue( hProcess ) ) // wait and return exit code

FClose( hStdIn )
FClose( hStdOut )
FClose( hStdErr )

END SEQUENCE
RETURN
---->8----




Source "HJAVA.java":
----8<----
import java.util.Scanner; // JRE 1.5

import org.json.JSONObject;
import org.json.JSONException;

class HJAVA
{
public static void main( String[] args )
{
Scanner scanner = new Scanner( System.in );
String line = scanner.nextLine();
String result;

try
{
JSONObject json = new JSONObject( line );
json.put( "status", 200 );
json.put( "status_text", "OK" );

resultado = json.toString();
}
catch ( JSONException e )
{
try
{
JSONObject json = new JSONObject();
json.put( "status", 500 );
json.put( "status_text", e.toString() );

result = json.toString();
}
catch ( JSONException ee )
{
result = "";
}
}
System.out.print( result );
System.exit( 0 );
}
}
---->8----

A problem I've seen is when calling hb_ProcessClose() in Windows, if the process executes other processes, they are not finished. We should use the winapi TerminateJobObject and similar.

Sorry for the example so cumbersome, i have little time available and I could not reduce it more. I hope you can get the idea.

Regards.
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42080
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Return to Utilities / Utilidades

Who is online

Users browsing this forum: No registered users and 8 guests