1

I'm working with OpenSSL and therefore am having to work with some UnsafeMutablePointer objects.

I'm running the following code

  var x509_REQ : UnsafeMutablePointer<X509_REQ> = someObject
  let b = BIO_new(BIO_s_mem())
  let size = 3000

  let status = i2d_X509_REQ_bio(b, x509_REQ)

  guard status == 1 else{
        BIO_free(b)
        return "Failed to execute i2d_X509_REQ_bio command"
  }

  let bp = UnsafeMutableRawPointer.allocate(byteCount: size, alignment: 1)
  BIO_read(b, bp, Int32(size))
  BIO_free(b)

  let data = Data(bytes: bp, count: size)

The above code converts an OpenSSL X509_REQ object into a DER using the OpenSSL method i2d_X509_REQ_bio. The problem I'm having is the BIO_read command and the UnsafeMutablePointer storage object both need a size count for the number of bytes. Does anybody know the correct way to get the length of bytes of an object pointed to by a UnsafeMutablePointer<Any> in Swift? (I'm hard-coding an arbitrary number right now, 3000, which is very bad) The X509_Req object doesn't have any size or count helper methods on it, and digging into Apple's documentation I'm not seeing a clear way to find the length of an object at a pointer.


Why don't these daemon threads get killed?

I was learning about sharing data between threads and I stumbled upon this different problem. As far as I understand, daemon threads are killed upon completion of main thread. The simple code is below:

import threading
from time import sleep

def thread(Nr):
    global x
    lock.acquire()
    x = Nr
    print(x)
    sleep(4)
    print(x)
    lock.release()

    return 0

#################################################################################

x = 2
lock = threading.Lock()

for i in range(6):
    #print("Thread Nr: ", i)
    arg1 = i
    t = threading.Thread(target = thread, args = (arg1,), name = arg1)
    t.setDaemon(True)
    print("new thread started : %s" % (str(threading.current_thread().ident)))    
    t.start()
    sleep(1)

print("Main thread end")

I'm starting 6 threads and this is my output in IDLE python 3.7.2:

new thread started : 940
0
new thread started : 940
new thread started : 940
new thread started : 940
new thread started : 9400

1
new thread started : 940
Main thread end
>>> 1
2
2
3
3
4
4
5
5

So, as you can see the threads continue to run after the main thread even if they are deamonic. One interesting thing I discovered is that they dont print anything after "Main thread end" if they are run from windows cmd instead of IDLE.

Does anyone know what is happening here?

Thanks :)

4

1 回答 1

1

指针只是一个内存地址,没有任何关于它所指向的内存区域大小的信息。

但是——如果我正确理解了这个任务——你真正需要的是写入基于内存的 BIO 的字节数。这BIO_get_mem_data就是为了。不幸的是,OpenSSL 库将其实现为宏

# define BIO_get_mem_data(b,pp)  BIO_ctrl(b,BIO_CTRL_INFO,0,(char *)pp)

它没有导入 Swift,所以你必须BIO_ctrl直接调用。

示例(为简洁起见省略了错误检查):

// Create memory-based BIO:
let membio = BIO_new(BIO_s_mem())

// Write to BIO:
let status = i2d_X509_REQ_bio(membio, x509_REQ)

// Get pointer to the start of the memory BIOs data
// and amount of data available:
var ptr: UnsafeMutableRawPointer?
let len = BIO_ctrl(membio, BIO_CTRL_INFO, 0, &ptr)

// Create `Data` object from that memory region:
let data = Data(bytes: ptr!, count: len)

// Release BIO:
BIO_vfree(membio)
于 2019-02-06T21:31:49.150 回答