title: How to get length of a UnsafeMutablePointer<Object> written to a OpenSSL BIO in Swift?
body: <p>I'm working with OpenSSL and therefore am having to work with some <code>UnsafeMutablePointer</code> objects. </p>
  
  <p>I'm running the following code</p>
  
  <pre><code>  var x509_REQ : UnsafeMutablePointer&lt;X509_REQ&gt; = 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)
  </code></pre>
  
  <p>The above code converts an OpenSSL <code>X509_REQ</code> object into a <code>DER</code> using the OpenSSL method <code>i2d_X509_REQ_bio</code>. The problem I'm having is the <code>BIO_read</code> command and the <code>UnsafeMutablePointer</code> 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 <code>UnsafeMutablePointer&lt;Any&gt;</code> in Swift? (I'm hard-coding an arbitrary number right now, 3000, which is very bad) The <code>X509_Req</code> object doesn't have any <code>size</code> or  <code>count</code> 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. </p>