🗒️
notes
  • Journal
  • URLs
  • Java Card
    • SCP02
    • Rapid Notes
    • _FIXVALS_
    • Mifare
    • Chain Of Trust
  • Encoding
    • CBEFF
    • Bytes
  • Snippets
    • JNI_OnLoad
  • float to byte[]
  • Protobuf
  • C/C++
    • Containers
    • Basics
    • JNI
    • gcov
    • Castings
  • chess
    • Untitled
  • Compression
    • Untitled
  • Snippets
    • Untitled
  • Build Systems
    • Maven
    • Windows
  • Gradle
  • CMake
  • Java
    • Untitled
    • Certificates
  • Android
    • Mifare
  • Python
    • ctypes
  • WebSub
    • References
  • Spring Boot
    • Form-based Authentication
    • Basic Access Authentication
    • JWT Authentication
  • QR Code
    • Denso QR Code
  • Philosophical Inquiry
    • First
  • XML
    • xmlstarlet
Powered by GitBook
On this page

Was this helpful?

  1. Snippets

Untitled

Java:

public static byte[] reverse(byte[] arr)
{
    byte[] buf = new byte[arr.length];
    int len = arr.length;
    for (int i = 0; i < len; i++) {
        buf[i] = arr[len - 1 - i];
    }
    return buf;
}

public void set_facediff(float value) 
{
    byte[] buf = reverse(ByteBuffer.allocate(4).putFloat(value).array());
    byte[] cmd = new byte[1];
    cmd[0] = 0x00;
    ByteArrayOutputStream bos = new ByteArrayOutputStream();

    try {
        bos.write(cmd);
        bos.write(buf);
    } catch (IOException e) {
        e.printStackTrace();
        return;
    }

    byte[] iobuf = bos.toByteArray();
    ioctl(ctx, iobuf);

}

public float get_facediff()
{
    float facediff = 0.0f;
    byte[] cmd = new byte[5];
    cmd[0] = 0x01;
    ioctl(ctx, cmd);

    byte[] b = reverse(Arrays.copyOfRange(cmd, 1,cmd.length));
    ByteBuffer buffer = ByteBuffer.wrap(b);
    facediff = buffer.getFloat();

    return facediff;
}

Python:

import ctypes                                                                                                                              
import sys                                                                                                                                 
                                                                                                                                           
inputf = sys.argv[1]                                                                                                                       
data = open(inputf,'rb').read()                                                                                                            
                                                                                                                                           
lib = ctypes.CDLL('/root/demo/functions.so') 
lib.f(data,len(data))                                                                                                                      
                                                                                                                                           
print("\n*** END ***\n")   

C:

#include <stdio.h>                                                                                                                         
#include <unistd.h>                                                                                                                        
                                                                                                                                           
int f(unsigned char* buf, int len)                                                                                                         
{                                                                                                                                          
    printf("\n*** fast computation inside C ***\n");                                                                                       
    sleep(3);                                                                                                                              
                                                                                                                                           
    for (int i = 0; i < len; i++) {                                                                                                        
        printf("%02x ",buf[i]);                                                                                                            
        if ((i % 16) == 0) {                                                                                                               
            printf("\n");                                                                                                                  
        }                                                                                                                                  
    }                                                                                                                                      
    printf("\n");                                                                                                                          
    fflush(stdout);                                                                                                                                                
    return len;                                                                                                                            
}   

Don't forget to flush the remaining bytes less than 16!

PreviousUntitledNextMaven

Last updated 4 years ago

Was this helpful?