File: CFData.c

package info (click to toggle)
gnustep-corebase 0.1.1%2B20230710-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,608 kB
  • sloc: ansic: 25,359; objc: 4,347; cpp: 75; xml: 49; makefile: 24; sh: 7
file content (382 lines) | stat: -rw-r--r-- 9,099 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
/* CFData.c
   
   Copyright (C) 2011 Free Software Foundation, Inc.
   
   Written by: Stefan Bidigaray
   Date: September, 2011
   
   This file is part of GNUstep CoreBase Library.
   
   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with this library; see the file COPYING.LIB.
   If not, see <http://www.gnu.org/licenses/> or write to the 
   Free Software Foundation, 51 Franklin Street, Fifth Floor, 
   Boston, MA 02110-1301, USA.
*/

#include <assert.h>
#include <string.h>

#include "CoreFoundation/CFRuntime.h"
#include "CoreFoundation/CFBase.h"
#include "CoreFoundation/CFData.h"

#include "GSPrivate.h"
#include "GSObjCRuntime.h"

struct __CFData
{
  CFRuntimeBase  _parent;
  const UInt8   *_contents;
  CFIndex        _length;
  CFHashCode     _hash;
  CFAllocatorRef _deallocator;
};

struct __CFMutableData
{
  CFRuntimeBase  _parent;
  UInt8         *_contents;
  CFIndex        _length;
  CFHashCode     _hash;
  CFAllocatorRef _allocator;
  CFIndex        _capacity;
};

static CFTypeID _kCFDataTypeID = 0;

/* These are some masks to access the data in CFRuntimeBase's _flags.info
   field. */
enum
{
  _kCFDataIsMutable = (1<<0),
  _kCFDataFreeBytes = (1<<1)
};

CF_INLINE Boolean
CFDataIsMutable (CFDataRef d)
{
  return ((CFRuntimeBase *)d)->_flags.info & _kCFDataIsMutable ? true : false;
}

CF_INLINE Boolean
CFDataFreeBytes (CFDataRef d)
{
  return ((CFRuntimeBase *)d)->_flags.info & _kCFDataFreeBytes ? true : false;
}

CF_INLINE void
CFDataSetMutable (CFDataRef d)
{
  ((CFRuntimeBase *)d)->_flags.info |= _kCFDataIsMutable;
}

CF_INLINE void
CFDataSetFreeBytes (CFDataRef d)
{
  ((CFRuntimeBase *)d)->_flags.info |= _kCFDataFreeBytes;
}

static void
CFDataFinalize (CFTypeRef cf)
{
  CFDataRef d = (CFDataRef)cf;
  
  if (CFDataFreeBytes(d))
    {
      CFAllocatorDeallocate (d->_deallocator, (void*)d->_contents);
      CFRelease (d->_deallocator);
    }
}

static Boolean
CFDataEqual (CFTypeRef cf1, CFTypeRef cf2)
{
  CFDataRef d1 = cf1;
  CFDataRef d2 = cf2;
  CFIndex length = CFDataGetLength (d1);
  
  if (length != CFDataGetLength(d2))
    return false;
  
  return (memcmp (d1->_contents, d2->_contents, length) == 0);
}

static CFHashCode
CFDataHash (CFTypeRef cf)
{
  struct __CFData *d = (struct __CFData*)cf;
  if (d->_hash == 0)
    d->_hash = GSHashBytes (d->_contents, d->_length);
  
  return d->_hash;
}

static const CFRuntimeClass CFDataClass =
{
  0,
  "CFData",
  NULL,
  (CFTypeRef (*)(CFAllocatorRef, CFTypeRef))CFDataCreateCopy,
  CFDataFinalize,
  CFDataEqual,
  CFDataHash,
  NULL,
  NULL
};

void CFDataInitialize (void)
{
  _kCFDataTypeID = _CFRuntimeRegisterClass(&CFDataClass);
}



#define CFDATA_SIZE sizeof(struct __CFData) - sizeof(CFRuntimeBase)

CFTypeID
CFDataGetTypeID (void)
{
  return _kCFDataTypeID;
}

static CFDataRef
CFDataCreate_internal (CFAllocatorRef allocator, const UInt8 *bytes,
  CFIndex length, CFAllocatorRef bytesDealloc, Boolean copy)
{
  struct __CFData *newData;
  CFIndex size;
  
  size = CFDATA_SIZE + (copy == true ? length : 0);
  
  if (allocator == NULL)
    allocator = CFAllocatorGetDefault ();
  
  newData = (struct __CFData*)_CFRuntimeCreateInstance (allocator,
    _kCFDataTypeID, size, NULL);
  if (newData)
    {
      newData->_length = length;
      
      if (copy)
        {
          memcpy (&(newData[1]), bytes, length);
          bytes = (const UInt8*)&(newData[1]);
        }
      else
        {
          if (bytesDealloc == NULL)
            bytesDealloc = CFAllocatorGetDefault ();
          newData->_deallocator = CFRetain(bytesDealloc);
          CFDataSetFreeBytes (newData);
        }
      newData->_contents = bytes;
    }
  
  return (CFDataRef)newData;
}

CFDataRef
CFDataCreate (CFAllocatorRef allocator, const UInt8 *bytes, CFIndex length)
{
  return CFDataCreate_internal (allocator, bytes, length, NULL, true);
}

CFDataRef
CFDataCreateWithBytesNoCopy (CFAllocatorRef allocator, const UInt8 *bytes,
  CFIndex length, CFAllocatorRef bytesDealloc)
{
  return CFDataCreate_internal (allocator, bytes, length, bytesDealloc, false);
}

CFDataRef
CFDataCreateCopy (CFAllocatorRef allocator, CFDataRef d)
{
  return CFDataCreate_internal (allocator, CFDataGetBytePtr(d),
    CFDataGetLength(d), NULL, true);
}

const UInt8 *
CFDataGetBytePtr (CFDataRef d)
{
  CF_OBJC_FUNCDISPATCHV(_kCFDataTypeID, const UInt8 *, d, "bytes");
  
  return d->_contents;
}

void
CFDataGetBytes (CFDataRef d, CFRange range, UInt8 *buffer)
{
  CF_OBJC_FUNCDISPATCHV(_kCFDataTypeID, void, d, "getBytes:range:", buffer,
    range);
  
  assert (range.location + range.length <= d->_length);
  memcpy (buffer, d->_contents + range.location, range.length);
}

CFIndex
CFDataGetLength (CFDataRef d)
{
  CF_OBJC_FUNCDISPATCHV(_kCFDataTypeID, CFIndex, d, "length");
  
  return d->_length;
}



#define DEFAULT_CAPACITY 16
#define CFMUTABLEDATA_SIZE \
  sizeof(struct __CFMutableData) - sizeof(CFRuntimeBase)

static void
CFDataCheckCapacityAndGrow (CFMutableDataRef data, CFIndex capacity)
{
  struct __CFMutableData *d = (struct __CFMutableData*)data;
  
  if (capacity > d->_capacity)
    {
      d->_contents = CFAllocatorReallocate (d->_allocator, d->_contents,
        capacity, 0);
      d->_capacity = capacity;
    }
}

CFMutableDataRef
CFDataCreateMutable (CFAllocatorRef allocator, CFIndex capacity)
{
  struct __CFMutableData *newData;
  
  if (allocator == NULL)
    allocator = CFAllocatorGetDefault ();
  
  newData = (struct __CFMutableData*)_CFRuntimeCreateInstance (allocator,
    _kCFDataTypeID, CFMUTABLEDATA_SIZE, NULL);
  
  if (newData)
    {
      if (capacity < DEFAULT_CAPACITY)
        capacity = DEFAULT_CAPACITY;
      newData->_capacity = capacity;
      newData->_allocator = CFRetain(allocator);
      newData->_contents = CFAllocatorAllocate (allocator, capacity, 0);
      
      CFDataSetMutable ((CFDataRef)newData);
      CFDataSetFreeBytes((CFDataRef)newData);
    }
  
  return (CFMutableDataRef)newData;
}

CFMutableDataRef
CFDataCreateMutableCopy (CFAllocatorRef allocator, CFIndex capacity,
  CFDataRef d)
{
  CFMutableDataRef newData;
  CFIndex length;
  
  length = CFDataGetLength (d);
  newData =
    CFDataCreateMutable (allocator, capacity > length ? capacity : length);
  
  memcpy ((UInt8*)newData->_contents, CFDataGetBytePtr(d), length);
  newData->_length = length;
  
  return newData;
}

void
CFDataAppendBytes (CFMutableDataRef d, const UInt8 *bytes, CFIndex length)
{
  CF_OBJC_FUNCDISPATCHV(_kCFDataTypeID, void, d, "appendBytes:length:", bytes,
    length);
  
  CFDataReplaceBytes (d, CFRangeMake(d->_length, 0), bytes, length);
}

void
CFDataDeleteBytes (CFMutableDataRef d, CFRange range)
{
  CFDataReplaceBytes(d, range, NULL, 0);
}

UInt8 *
CFDataGetMutableBytePtr (CFMutableDataRef d)
{
  CF_OBJC_FUNCDISPATCHV(_kCFDataTypeID, UInt8 *, d, "mutableBytes");
  
  if (!CFDataIsMutable(d))
    return NULL;
  
  return ((struct __CFMutableData*)d)->_contents;
}

void
CFDataIncreaseLength (CFMutableDataRef d, CFIndex length)
{
  CF_OBJC_FUNCDISPATCHV(_kCFDataTypeID, void, d, "increaseLengthBy:", length);
  
  CFDataSetLength (d, d->_length + length);
}

void
CFDataReplaceBytes (CFMutableDataRef d, CFRange range,
                    const UInt8 *newBytes, CFIndex newLength)
{
  struct __CFMutableData *md;
  CFIndex newBufLen;
  
  CF_OBJC_FUNCDISPATCHV(_kCFDataTypeID, void, d,
    "replaceBytesInRange:withBytes:length:", range, newBytes, newLength);
  
  if (!CFDataIsMutable(d))
    return;
  
  md = (struct __CFMutableData*)d;
  assert (range.location + range.length <= md->_capacity);
  
  newBufLen = range.location + newLength;
  CFDataCheckCapacityAndGrow (d, newBufLen);
  
  if (newLength != range.length && range.location + range.length < newBufLen)
    {
      UInt8 *moveFrom = md->_contents + range.location + range.length;
      UInt8 *moveTo = md->_contents + range.location + newLength;
      CFIndex moveLength = d->_length - (range.location + range.length);
      memmove (moveTo, moveFrom, moveLength);
    }
  if (newLength > 0)
    memmove (md->_contents + range.location, newBytes, newLength);
  
  md->_length = newBufLen;
  md->_hash = 0;
}

void
CFDataSetLength (CFMutableDataRef d, CFIndex length)
{
  struct __CFMutableData *md;
  
  CF_OBJC_FUNCDISPATCHV(_kCFDataTypeID, void, d, "setLength:", length);
  
  if (!CFDataIsMutable(d))
    return;
  
  CFDataCheckCapacityAndGrow (d, length);
  
  md = (struct __CFMutableData*)d;
  if (md->_length < length)
    memset (md->_contents + md->_length, 0, length - md->_length);
  
  md->_length = length;
}