1 /* |
1 /* |
2 * Copyright (c) 2005, 2016, Oracle and/or its affiliates. All rights reserved. |
2 * Copyright (c) 2005, 2018, Oracle and/or its affiliates. All rights reserved. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 * |
4 * |
5 * This code is free software; you can redistribute it and/or modify it |
5 * This code is free software; you can redistribute it and/or modify it |
6 * under the terms of the GNU General Public License version 2 only, as |
6 * under the terms of the GNU General Public License version 2 only, as |
7 * published by the Free Software Foundation. Oracle designates this |
7 * published by the Free Software Foundation. Oracle designates this |
1596 |
1596 |
1597 /* |
1597 /* |
1598 * Convert an array in big-endian byte order into little-endian byte order. |
1598 * Convert an array in big-endian byte order into little-endian byte order. |
1599 */ |
1599 */ |
1600 int convertToLittleEndian(JNIEnv *env, jbyteArray source, jbyte* destination, |
1600 int convertToLittleEndian(JNIEnv *env, jbyteArray source, jbyte* destination, |
1601 int destinationLength) { |
1601 int destinationLength) { |
1602 |
1602 |
1603 int sourceLength = env->GetArrayLength(source); |
1603 int result = -1; |
1604 |
1604 jbyte* sourceBytes = NULL; |
1605 jbyte* sourceBytes = env->GetByteArrayElements(source, 0); |
1605 |
1606 if (sourceBytes == NULL) { |
1606 __try { |
1607 return -1; |
1607 int sourceLength = env->GetArrayLength(source); |
1608 } |
1608 |
1609 |
1609 sourceBytes = env->GetByteArrayElements(source, 0); |
1610 int copyLen = sourceLength; |
1610 if (sourceBytes == NULL) { |
1611 if (sourceLength > destinationLength) { |
1611 __leave; |
1612 // source might include an extra sign byte |
1612 } |
1613 if (sourceLength == destinationLength + 1 && sourceBytes[0] == 0) { |
1613 |
1614 copyLen--; |
1614 int copyLen = sourceLength; |
1615 } else { |
1615 if (sourceLength > destinationLength) { |
1616 return -1; |
1616 // source might include an extra sign byte |
1617 } |
1617 if (sourceLength == destinationLength + 1 && sourceBytes[0] == 0) { |
1618 } |
1618 copyLen--; |
1619 |
1619 } else { |
1620 // Copy bytes from the end of the source array to the beginning of the |
1620 __leave; |
1621 // destination array (until the destination array is full). |
1621 } |
1622 // This ensures that the sign byte from the source array will be excluded. |
1622 } |
1623 for (int i = 0; i < copyLen; i++) { |
1623 |
1624 destination[i] = sourceBytes[sourceLength - 1 - i]; |
1624 // Copy bytes from the end of the source array to the beginning of the |
1625 } |
1625 // destination array (until the destination array is full). |
1626 if (copyLen < destinationLength) { |
1626 // This ensures that the sign byte from the source array will be excluded. |
1627 memset(destination + copyLen, 0, destinationLength - copyLen); |
1627 for (int i = 0; i < copyLen; i++) { |
1628 } |
1628 destination[i] = sourceBytes[sourceLength - 1 - i]; |
1629 |
1629 } |
1630 env->ReleaseByteArrayElements(source, sourceBytes, JNI_ABORT); |
1630 if (copyLen < destinationLength) { |
1631 |
1631 memset(destination + copyLen, 0, destinationLength - copyLen); |
1632 return destinationLength; |
1632 } |
|
1633 result = destinationLength; |
|
1634 } __finally { |
|
1635 // Clean up. |
|
1636 if (sourceBytes) { |
|
1637 env->ReleaseByteArrayElements(source, sourceBytes, JNI_ABORT); |
|
1638 } |
|
1639 } |
|
1640 |
|
1641 return result; |
1633 } |
1642 } |
1634 |
1643 |
1635 /* |
1644 /* |
1636 * The Microsoft Base Cryptographic Provider supports public-key BLOBs |
1645 * The Microsoft Base Cryptographic Provider supports public-key BLOBs |
1637 * that have the following format: |
1646 * that have the following format: |