📄 cryptlibconverter.py
字号:
jobject processStatusReturnCryptObjectInfo(JNIEnv *env, int status, CRYPT_OBJECT_INFO returnValue){ jclass exClass; jmethodID exConstructor; jbyteArray salt; jobject obj; if (status < cryptlib_crypt_OK) return NULL; exClass = (*env)->FindClass(env, "cryptlib/CRYPT_OBJECT_INFO"); if (exClass == 0) { printf("java_jni.c:processStatusReturnCryptObjectInfo - no class?!\n"); return NULL; } exConstructor = (*env)->GetMethodID(env, exClass, "<init>", "(IIII[B)V"); if (exConstructor == 0) { printf("java_jni.c:processStatusReturnCryptObjectInfo - no constructor?!\n"); return NULL; } salt = (*env)->NewByteArray(env, returnValue.saltSize); (*env)->SetByteArrayRegion(env, salt, 0, returnValue.saltSize, returnValue.salt); obj = (*env)->NewObject(env, exClass, exConstructor, returnValue.objectType, returnValue.cryptAlgo, returnValue.cryptMode, returnValue.hashAlgo, salt); if (obj == 0) { printf("java_jni.c:processStatusReturnCryptObjectInfo - no object?!\n"); return NULL; } return obj;}int checkIndicesArray(JNIEnv *env, jbyteArray array, int sequenceOffset, int sequenceLength){ jsize arrayLength; jclass exClass; if (array == NULL) { if (sequenceOffset == 0) return 1; else { exClass = (*env)->FindClass(env, "java/lang/ArrayIndexOutOfBoundsException"); if (exClass == 0) printf("java_jni.c:checkIndicesArray - no class?!\n"); else if ((*env)->ThrowNew(env, exClass, "") < 0) printf("java_jni.c:checkIndicesArray - failed to throw?!\n"); return 0; } } arrayLength = (*env)->GetArrayLength(env, array); if (sequenceOffset < 0 || sequenceOffset >= arrayLength || sequenceOffset + sequenceLength > arrayLength) { exClass = (*env)->FindClass(env, "java/lang/ArrayIndexOutOfBoundsException"); if (exClass == 0) printf("java_jni.c:checkIndicesArray - no class?!\n"); else if ((*env)->ThrowNew(env, exClass, "") < 0) printf("java_jni.c:checkIndicesArray - failed to throw?!\n"); return 0; } return 1;}int getPointerArray(JNIEnv* env, jbyteArray array, jbyte** bytesPtrPtr){ jboolean isCopy; if (array == NULL) { (*bytesPtrPtr) = NULL; return 1; } (*bytesPtrPtr) = (*env)->GetByteArrayElements(env, array, &isCopy); if (*bytesPtrPtr == NULL) { printf("java_jni.c:getPointer - failed to get elements of array?!\n"); return 0; } return 1;}void releasePointerArray(JNIEnv* env,jbyteArray array, jbyte* bytesPtr){ if (bytesPtr == NULL) return; (*env)->ReleaseByteArrayElements(env, array, bytesPtr, 0);}int checkIndicesNIO(JNIEnv *env, jobject byteBuffer, int sequenceOffset, int sequenceLength){ jlong byteBufferLength; jclass exClass; if (byteBuffer == NULL) { if (sequenceOffset == 0) return 1; else { exClass = (*env)->FindClass(env, "java/lang/ArrayIndexOutOfBoundsException"); if (exClass == 0) printf("java_jni.c:checkIndicesNIO - no class?!\n"); else if ((*env)->ThrowNew(env, exClass, "") < 0) printf("java_jni.c:checkIndicesNIO - failed to throw?!\n"); return 0; } } byteBufferLength = (*env)->GetDirectBufferCapacity(env, byteBuffer); if (byteBufferLength == -1) { exClass = (*env)->FindClass(env, "java/lang/UnsupportedOperationException"); if (exClass == 0) printf("java_jni.c:checkIndicesNIO - no class?!\n"); else if ((*env)->ThrowNew(env, exClass,"Either a non-direct ByteBuffer was passed or your JVM doesn't support JNI access to direct ByteBuffers") < 0) printf("java_jni.c:checkIndicesNIO - failed to throw?!\n"); return 0; } if (sequenceOffset < 0 || sequenceOffset >= byteBufferLength || sequenceOffset + sequenceLength > byteBufferLength) { exClass = (*env)->FindClass(env, "java/lang/ArrayIndexOutOfBoundsException"); if (exClass == 0) printf("java_jni.c:checkIndicesNIO - no class?!\n"); else if ((*env)->ThrowNew(env, exClass, "") < 0) printf("java_jni.c:checkIndicesNIO - failed to throw?!\n"); return 0; } return 1;}int getPointerNIO(JNIEnv* env, jobject byteBuffer, jbyte** bytesPtrPtr){ jclass exClass; if (byteBuffer == NULL) { (*bytesPtrPtr) = NULL; return 1; } (*bytesPtrPtr) = (*env)->GetDirectBufferAddress(env, byteBuffer); if (*bytesPtrPtr == NULL) { exClass = (*env)->FindClass(env, "java/lang/UnsupportedOperationException"); if (exClass == 0) printf("java_jni.c:getPointerNIO - no class?!\n"); else if ((*env)->ThrowNew(env, exClass, "Your JVM doesn't support JNI access to direct ByteBuffers") < 0) printf("java_jni.c:getPointerNIO - failed to throw?!\n"); return 0; } return 1;}void releasePointerNIO(JNIEnv* env,jbyteArray array, jbyte* bytesPtr){}int getPointerString(JNIEnv* env, jstring str, jbyte** bytesPtrPtr){ jboolean isCopy; jsize strLength; const jbyte* rawBytesPtr; jclass exClass;#ifdef __WINCE__ int status;#endif // __WINCE__ if (str == NULL) { (*bytesPtrPtr) = NULL; return 1; } rawBytesPtr = (*env)->GetStringUTFChars(env, str, &isCopy); if (rawBytesPtr == NULL) { printf("java_jni.c:getPointerString - failed to get elements of String?!\n"); return 0; } strLength = (*env)->GetStringUTFLength(env, str);#ifdef __WINCE__ (*bytesPtrPtr) = (jbyte*)malloc(strLength*2+2); // this is unicode, therefore \0 is two bytes long#else (*bytesPtrPtr) = (jbyte*)malloc(strLength+1);#endif // __WINCE__ if (*bytesPtrPtr == NULL) { exClass = (*env)->FindClass(env, "java/lang/OutOfMemoryError"); if (exClass == 0) printf("java_jni.c:getPointerString - no class?!\n"); else if ((*env)->ThrowNew(env, exClass, "") < 0) printf("java_jni.c:getPointerString - failed to throw?!\n"); (*env)->ReleaseStringUTFChars(env, str, rawBytesPtr); return 0; }#ifdef __WINCE__ status = asciiToUnicode (*bytesPtrPtr, strLength*2+2, rawBytesPtr, strLength+1); if (status == CRYPT_ERROR_BADDATA) { (*env)->ReleaseStringUTFChars(env, str, rawBytesPtr); return 0; }#else memcpy(*bytesPtrPtr, rawBytesPtr, strLength); (*bytesPtrPtr)[strLength] = 0;#endif // __WINCE__ (*env)->ReleaseStringUTFChars(env, str, rawBytesPtr); return 1;}void releasePointerString(JNIEnv* env, jstring str, jbyte* bytesPtr){ if (bytesPtr == NULL) return; free(bytesPtr);}%s#endif /* USE_JAVA */""" % (s, s2) functionPattern = re.compile(r"JNIEXPORT ([^ \t]+) JNICALL Java_cryptlib_crypt_([^ \t\n]+)\n[ \t]*\(([^\)]*)\);") functionMatch = functionPattern.search(s) while functionMatch: #print functionMatch.groups() #Extract the returnType, name, and prototype for this function function = functionMatch.group() functionReturnType, functionName, functionPrototype = functionMatch.groups() #Handle special-case AddRandom(pollType) function if functionName == "AddRandom__I": p = ParamStruct() p.type = "int" p.isPtr = 0 p.isOut = 0 p.category = "intType" p.name = "NULL" p.rawIndex = 0 p2 = ParamStruct() p2.type = "int" p2.isPtr = 0 p2.isOut = 0 p2.category = "intType" p2.name = "pollType" p2.rawIndex = 1 rawParamStructs = [p,p2] newParamStructs = [p2,p2] voidTag = "Array" functionName = functionName.split("__")[0] returnName = None discardName = None else: #Strip JNI decoration off function name #But record which variety of tagged helper functions to use if functionName.find("__") != -1: if functionName.find("ByteBuffer") != -1: voidTag = "NIO" else: voidTag = "Array" functionName = functionName.split("__")[0] #Look up the parameters we've previously determined for this function rawParamStructs = rawParamStructsDict[functionName] newParamStructs = newParamStructsDict[functionName] if newReturnStructsDict.get(functionName): returnName = newReturnStructsDict.get(functionName).name returnType = newReturnStructsDict.get(functionName).type returnCategory = newReturnStructsDict.get(functionName).category else: returnName = None if newDiscardedStructsDict.get(functionName): discardName = newDiscardedStructsDict.get(functionName).name else: discardName = None #for p in newParamStructs: print " "+str(p) #Substitute parameter names into the function prototype newFunctionParams = expandFunctionPrototype(functionPrototype, newParamStructs) startIndex = functionMatch.start(3) - functionMatch.start() endIndex = functionMatch.end(3) - functionMatch.start() function = function[ : startIndex] + newFunctionParams + function[ endIndex : ] newFunctionBody = "\nint status = 0;\n" arguments = "" for p in rawParamStructs: if p.name == returnName or p.name == discardName: arguments += "&" if p.isPtr and p.type=="void": arguments += "%sPtr + %sOffset, " % (p.name, p.name) elif p.isPtr and p.type=="char": arguments += "%sPtr, " % p.name else: arguments += p.name + ", " arguments = arguments[:-2] if returnName: if returnCategory == "intType" or returnType=="int": newFunctionBody += "jint %s = 0;\n" % returnName else: newFunctionBody += returnType +" %s;\n" % returnName if discardName: newFunctionBody += "jint %s = 0;\n" % discardName #If we have arrays, add the code to handle them arrayNames = [p.name for p in newParamStructs if p.isPtr] charArrayNames = [p.name for p in newParamStructs if p.isPtr and p.type=="char"] voidArrayNames = [p.name for p in newParamStructs if p.isPtr and p.type=="void"] outVoidArrayNames = [p.name for p in newParamStructs if p.isPtr and p.type=="void" and p.isOut] if arrayNames: #Declare C pointers to retrieve array contents for n in arrayNames: newFunctionBody += "jbyte* " + n + "Ptr = 0;\n" newFunctionBody += "\n" #Retrieve the contents for strings #We need to do this first cause in one case this char* is needed as an argument if charArrayNames: for n in charArrayNames: newFunctionBody += "if (!getPointerString(env, %(n)s, &%(n)sPtr))\n\tgoto finish;\n" % vars() newFunctionBody += "\n" #Query the length of output data #CryptPopData is the only exception that produces output data #but doesn't require this cause an explicit length is passed in if outVoidArrayNames and functionName.find("PopData") == -1: for n in outVoidArrayNames: argumentsWithNull = arguments.replace("%sPtr + %sOffset" % (n,n), "NULL") newFunctionBody += "if (!processStatus(env, crypt%s(%s)))\n\tgoto finish;\n" % (functionName, argumentsWithNull) newFunctionBody += "\n" elif functionName.find("PopData") != -1: newFunctionBody += "//CryptPopData is a special case that doesn't have the length querying call\n\n" if voidArrayNames: for n in voidArrayNames: index = [p.name for p in newParamStructs].index(n) if n in outVoidArrayNames:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -