-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Move NSData
to ByteString
conversion onto ByteString
companion
#1131
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,221 @@ | ||
/* | ||
* Copyright (C) 2020 Square, Inc. | ||
* Copyright (C) 2018 Square, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package okio | ||
|
||
import kotlinx.cinterop.UnsafeNumber | ||
import kotlinx.cinterop.addressOf | ||
import kotlinx.cinterop.usePinned | ||
import okio.internal.HashFunction | ||
import okio.internal.Hmac | ||
import okio.internal.Md5 | ||
import okio.internal.Sha1 | ||
import okio.internal.Sha256 | ||
import okio.internal.Sha512 | ||
import okio.internal.commonBase64 | ||
import okio.internal.commonBase64Url | ||
import okio.internal.commonCompareTo | ||
import okio.internal.commonCopyInto | ||
import okio.internal.commonDecodeBase64 | ||
import okio.internal.commonDecodeHex | ||
import okio.internal.commonEncodeUtf8 | ||
import okio.internal.commonEndsWith | ||
import okio.internal.commonEquals | ||
import okio.internal.commonGetByte | ||
import okio.internal.commonGetSize | ||
import okio.internal.commonHashCode | ||
import okio.internal.commonHex | ||
import okio.internal.commonIndexOf | ||
import okio.internal.commonInternalArray | ||
import okio.internal.commonLastIndexOf | ||
import okio.internal.commonOf | ||
import okio.internal.commonRangeEquals | ||
import okio.internal.commonStartsWith | ||
import okio.internal.commonSubstring | ||
import okio.internal.commonToAsciiLowercase | ||
import okio.internal.commonToAsciiUppercase | ||
import okio.internal.commonToByteArray | ||
import okio.internal.commonToByteString | ||
import okio.internal.commonToString | ||
import okio.internal.commonUtf8 | ||
import okio.internal.commonWrite | ||
import platform.Foundation.NSData | ||
import platform.posix.memcpy | ||
|
||
@OptIn(UnsafeNumber::class) | ||
fun NSData.toByteString(): ByteString { | ||
val data = this | ||
val size = data.length.toInt() | ||
return if (size != 0) { | ||
ByteString( | ||
ByteArray(size).apply { | ||
usePinned { pinned -> | ||
memcpy(pinned.addressOf(0), data.bytes, data.length) | ||
} | ||
actual open class ByteString | ||
internal actual constructor( | ||
internal actual val data: ByteArray | ||
) : Comparable<ByteString> { | ||
@Suppress("SetterBackingFieldAssignment") | ||
internal actual var hashCode: Int = 0 // 0 if unknown. | ||
set(value) { | ||
// Do nothing to avoid IllegalImmutabilityException. | ||
} | ||
@Suppress("SetterBackingFieldAssignment") | ||
internal actual var utf8: String? = null | ||
set(value) { | ||
// Do nothing to avoid IllegalImmutabilityException. | ||
} | ||
|
||
actual open fun utf8(): String = commonUtf8() | ||
|
||
actual open fun base64(): String = commonBase64() | ||
|
||
actual open fun base64Url(): String = commonBase64Url() | ||
|
||
actual open fun hex(): String = commonHex() | ||
|
||
actual fun md5() = digest(Md5()) | ||
|
||
actual fun sha1() = digest(Sha1()) | ||
|
||
actual fun sha256() = digest(Sha256()) | ||
|
||
actual fun sha512() = digest(Sha512()) | ||
|
||
/** Returns the 160-bit SHA-1 HMAC of this byte string. */ | ||
actual fun hmacSha1(key: ByteString) = digest(Hmac.sha1(key)) | ||
|
||
/** Returns the 256-bit SHA-256 HMAC of this byte string. */ | ||
actual fun hmacSha256(key: ByteString) = digest(Hmac.sha256(key)) | ||
|
||
/** Returns the 512-bit SHA-512 HMAC of this byte string. */ | ||
actual fun hmacSha512(key: ByteString) = digest(Hmac.sha512(key)) | ||
|
||
internal open fun digest(hashFunction: HashFunction): ByteString { | ||
hashFunction.update(data, 0, size) | ||
val digestBytes = hashFunction.digest() | ||
return ByteString(digestBytes) | ||
} | ||
|
||
actual open fun toAsciiLowercase(): ByteString = commonToAsciiLowercase() | ||
|
||
actual open fun toAsciiUppercase(): ByteString = commonToAsciiUppercase() | ||
|
||
actual open fun substring(beginIndex: Int, endIndex: Int): ByteString = | ||
commonSubstring(beginIndex, endIndex) | ||
|
||
internal actual open fun internalGet(pos: Int): Byte { | ||
if (pos >= size || pos < 0) throw ArrayIndexOutOfBoundsException("size=$size pos=$pos") | ||
return commonGetByte(pos) | ||
} | ||
|
||
actual operator fun get(index: Int): Byte = internalGet(index) | ||
|
||
actual val size | ||
get() = getSize() | ||
|
||
internal actual open fun getSize() = commonGetSize() | ||
|
||
actual open fun toByteArray() = commonToByteArray() | ||
|
||
internal actual open fun internalArray() = commonInternalArray() | ||
|
||
internal actual open fun write(buffer: Buffer, offset: Int, byteCount: Int) = | ||
commonWrite(buffer, offset, byteCount) | ||
|
||
actual open fun rangeEquals( | ||
offset: Int, | ||
other: ByteString, | ||
otherOffset: Int, | ||
byteCount: Int | ||
): Boolean = commonRangeEquals(offset, other, otherOffset, byteCount) | ||
|
||
actual open fun rangeEquals( | ||
offset: Int, | ||
other: ByteArray, | ||
otherOffset: Int, | ||
byteCount: Int | ||
): Boolean = commonRangeEquals(offset, other, otherOffset, byteCount) | ||
|
||
actual open fun copyInto( | ||
offset: Int, | ||
target: ByteArray, | ||
targetOffset: Int, | ||
byteCount: Int | ||
) = commonCopyInto(offset, target, targetOffset, byteCount) | ||
|
||
actual fun startsWith(prefix: ByteString) = commonStartsWith(prefix) | ||
|
||
actual fun startsWith(prefix: ByteArray) = commonStartsWith(prefix) | ||
|
||
actual fun endsWith(suffix: ByteString) = commonEndsWith(suffix) | ||
|
||
actual fun endsWith(suffix: ByteArray) = commonEndsWith(suffix) | ||
|
||
actual fun indexOf(other: ByteString, fromIndex: Int) = indexOf(other.internalArray(), fromIndex) | ||
|
||
actual open fun indexOf(other: ByteArray, fromIndex: Int) = commonIndexOf(other, fromIndex) | ||
|
||
actual fun lastIndexOf(other: ByteString, fromIndex: Int) = commonLastIndexOf(other, fromIndex) | ||
|
||
actual open fun lastIndexOf(other: ByteArray, fromIndex: Int) = commonLastIndexOf(other, fromIndex) | ||
|
||
actual override fun equals(other: Any?) = commonEquals(other) | ||
|
||
actual override fun hashCode() = commonHashCode() | ||
|
||
actual override fun compareTo(other: ByteString) = commonCompareTo(other) | ||
|
||
/** | ||
* Returns a human-readable string that describes the contents of this byte string. Typically this | ||
* is a string like `[text=Hello]` or `[hex=0000ffff]`. | ||
*/ | ||
actual override fun toString() = commonToString() | ||
|
||
actual companion object { | ||
actual val EMPTY: ByteString = ByteString(byteArrayOf()) | ||
|
||
actual fun of(vararg data: Byte) = commonOf(data) | ||
|
||
actual fun ByteArray.toByteString(offset: Int, byteCount: Int): ByteString = | ||
commonToByteString(offset, byteCount) | ||
|
||
actual fun String.encodeUtf8(): ByteString = commonEncodeUtf8() | ||
|
||
actual fun String.decodeBase64(): ByteString? = commonDecodeBase64() | ||
|
||
actual fun String.decodeHex() = commonDecodeHex() | ||
|
||
@OptIn(UnsafeNumber::class) | ||
@CName("of") | ||
fun NSData.toByteString(): ByteString { | ||
val data = this | ||
val size = data.length.toInt() | ||
return if (size != 0) { | ||
ByteString( | ||
ByteArray(size).apply { | ||
usePinned { pinned -> | ||
memcpy(pinned.addressOf(0), data.bytes, data.length) | ||
} | ||
} | ||
) | ||
} else { | ||
EMPTY | ||
} | ||
) | ||
} else { | ||
ByteString.EMPTY | ||
} | ||
} | ||
} | ||
|
||
@Deprecated( | ||
message = "Moved to ByteString companion object", | ||
replaceWith = ReplaceWith("this.toByteString()", "okio.ByteString.Companion.toByteString"), | ||
) | ||
fun NSData.toByteString(): ByteString { | ||
with(ByteString) { | ||
return toByteString() | ||
} | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This old file was deleted, the other file moved here with the same name, and the extension hand written at the bottom. It is not actually changed but git thinks it's being smart.