Blob.cpp revision debd02e3cb441e34f5b63bbfbf69e404f8135159
1// This may look like C code, but it is really -*- C++ -*-
2//
3// Copyright Bob Friesenhahn, 1999, 2000, 2001, 2002, 2004
4//
5// Implementation of Blob
6//
7
8#define MAGICKCORE_IMPLEMENTATION  1
9#define MAGICK_PLUSPLUS_IMPLEMENTATION 1
10
11#include "Magick++/Include.h"
12#include "Magick++/Blob.h"
13#include "Magick++/BlobRef.h"
14
15#include <string.h>
16
17Magick::Blob::Blob(void)
18  : _blobRef(new Magick::BlobRef(0,0))
19{
20}
21
22Magick::Blob::Blob(const void* data_,size_t length_)
23  : _blobRef(new Magick::BlobRef(data_, length_))
24{
25}
26
27Magick::Blob::Blob(const Magick::Blob& blob_)
28  : _blobRef(blob_._blobRef)
29{
30  // Increase reference count
31  _blobRef->increase();
32}
33
34Magick::Blob::~Blob()
35{
36  if (_blobRef->decrease() == 0)
37    delete _blobRef;
38
39  _blobRef=(Magick::BlobRef *) NULL;
40}
41
42Magick::Blob& Magick::Blob::operator=(const Magick::Blob& blob_)
43{
44  if (this != &blob_)
45    {
46      blob_._blobRef->increase();
47      if (_blobRef->decrease() == 0)
48        delete _blobRef;
49
50      _blobRef=blob_._blobRef;
51    }
52  return(*this);
53}
54
55void Magick::Blob::base64(const std::string base64_)
56{
57  size_t
58    length;
59
60  unsigned char
61    *decoded;
62
63  decoded=Base64Decode(base64_.c_str(),&length);
64
65  if(decoded)
66    updateNoCopy(static_cast<void*>(decoded),length,
67      Magick::Blob::MallocAllocator);
68}
69
70std::string Magick::Blob::base64(void) const
71{
72  size_t
73    encoded_length;
74
75  char
76    *encoded;
77
78  std::string
79    result;
80
81  encoded_length=0;
82  encoded=Base64Encode(static_cast<const unsigned char*>(data()),length(),
83    &encoded_length);
84
85  if(encoded)
86    {
87      result=std::string(encoded,encoded_length);
88      encoded=(char *) RelinquishMagickMemory(encoded);
89      return result;
90    }
91
92  return(std::string());
93}
94
95const void* Magick::Blob::data(void) const
96{
97  return(_blobRef->data);
98}
99
100size_t Magick::Blob::length(void) const
101{
102  return(_blobRef->length);
103}
104
105void Magick::Blob::update(const void* data_,size_t length_)
106{
107  if (_blobRef->decrease() == 0)
108    delete _blobRef;
109
110  _blobRef=new Magick::BlobRef(data_,length_);
111}
112
113void Magick::Blob::updateNoCopy(void* data_,size_t length_,
114  Magick::Blob::Allocator allocator_)
115{
116  if (_blobRef->decrease() == 0)
117    delete _blobRef;
118
119  _blobRef=new Magick::BlobRef((const void*) NULL,0);
120  _blobRef->data=data_;
121  _blobRef->length=length_;
122  _blobRef->allocator=allocator_;
123}
124
125