/** Function oyBlob_SetFromData
 *  @memberof oyBlob_s
 *  @brief   set value from a data blob
 *
 *  @param[in]     blob                the data blob
 *  @param[in]     ptr                 copy the data into the blob object
 *  @param[in]     size                data size; 0 means the pointer is not owned by the object.
 *  @param[in]     type                data type; assuming 8 byte with typical
 *                                     4 byte content
 *  @return                            0 - success, 1 - error
 *
 *  @version Oyranos: 0.1.10
 *  @since   2009/01/06 (Oyranos: 0.1.10)
 *  @date    2009/06/12
 */
int            oyBlob_SetFromData    ( oyBlob_s          * blob,
                                       oyPointer           ptr,
                                       size_t              size,
                                       const char        * type )
{
  oyBlob_s_ * s = (oyBlob_s_*)blob;
  int error = 0;

  oyCheckType__m( oyOBJECT_BLOB_S, return 1 )

  if(error <= 0)
  {
    if(s->ptr && !(s->flags & 0x01))
      s->oy_->deallocateFunc_( s->ptr );
    s->size = 0;

    if(size)
    {
      s->ptr = s->oy_->allocateFunc_( size );
      error = !s->ptr;
      s->flags = 0;
    } else
      s->flags = 0x01;
  }

  if(error <= 0)
  {
    if(size)
      error = !memcpy( s->ptr, ptr, size );
    else
      s->ptr = ptr;
  }

  if(error <= 0)
    s->size = size;

  if(error <= 0 && type)
    error = !memcpy( s->type, type, 8 );

  return error;
}

/** Function oyBlob_SetFromStatic
 *  @memberof oyBlob_s
 *  @brief   set value from a data blob
 *
 *  @param[in]     blob                the data blob
 *  @param[in]     ptr                 move the data into the blob object
 *  @param[in]     size                data size
 *  @param[in]     type                data type; assuming 8 byte with typical
 *                                     4 byte content
 *  @return                            0 - success, 1 - error
 *
 *  @version Oyranos: 0.1.10
 *  @since   2009/07/26 (Oyranos: 0.1.10)
 *  @date    2009/07/26
 */
int            oyBlob_SetFromStatic  ( oyBlob_s          * blob,
                                       const oyPointer     ptr,
                                       size_t              size,
                                       const char        * type )
{
  oyBlob_s_ * s = (oyBlob_s_*)blob;
  int error = 0;

  oyCheckType__m( oyOBJECT_BLOB_S, return 1 )

  if(error <= 0)
  {
    if(s->ptr && !(s->flags & 0x01))
      s->oy_->deallocateFunc_( s->ptr );
    s->size = 0;

    s->ptr = ptr;
    error = !s->ptr;
    s->flags = 0x01;
  }

  if(error <= 0)
    s->size = size;

  if(error <= 0 && type)
    error = !memcpy( s->type, type, 8 );

  return error;
}

/** Function oyBlob_GetPointer
 *  @memberof oyBlob_s
 *  @brief   get value from a data blob
 *
 *  @param[in]     blob                the data blob
 *  @return                            the data pointer
 *
 *  @version Oyranos: 0.2.1
 *  @since   2011/01/17 (Oyranos: 0.2.1)
 *  @date    2011/01/17
 */
oyPointer          oyBlob_GetPointer ( oyBlob_s          * blob )
{
  oyBlob_s_ * s = (oyBlob_s_*)blob;
  int error = !s;

  oyCheckType__m( oyOBJECT_BLOB_S, return NULL )

  if(error <= 0)
    return s->ptr;

  return NULL;
}

/** Function oyBlob_GetSize
 *  @memberof oyBlob_s
 *  @brief   get size from a data blob
 *
 *  @param[in]     blob                the data blob
 *  @return                            the data size
 *
 *  @version Oyranos: 0.2.1
 *  @since   2011/01/17 (Oyranos: 0.2.1)
 *  @date    2011/01/17
 */
size_t             oyBlob_GetSize    ( oyBlob_s          * blob )
{
  oyBlob_s_ * s = (oyBlob_s_*)blob;
  int error = !s;

  oyCheckType__m( oyOBJECT_BLOB_S, return 0 )

  if(error <= 0)
    return s->size;

  return 0;
}

/** Function oyBlob_GetType
 *  @memberof oyBlob_s
 *  @brief   get type from a data blob
 *
 *  @param[in]     blob                the data blob
 *  @return                            the data type
 *
 *  @version Oyranos: 0.2.1
 *  @since   2011/01/17 (Oyranos: 0.2.1)
 *  @date    2011/01/17
 */
const char *       oyBlob_GetType    ( oyBlob_s          * blob )
{
  oyBlob_s_ * s = (oyBlob_s_*)blob;
  int error = !s;

  oyCheckType__m( oyOBJECT_BLOB_S, return 0 )

  if(error <= 0)
    return s->type;

  return 0;
}

