summaryrefslogtreecommitdiffstats
path: root/Objects/bytesobject.c
blob: 36b442450082621434099ada95265798770a17b4 (plain)
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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
/* Bytes object implementation */

/* XXX TO DO: optimizations */

#define PY_SSIZE_T_CLEAN
#include "Python.h"

/* Direct API functions */

PyObject *
PyBytes_FromObject(PyObject *input)
{
    return PyObject_CallFunctionObjArgs((PyObject *)&PyBytes_Type,
                                        input, NULL);
}

PyObject *
PyBytes_FromStringAndSize(const char *bytes, Py_ssize_t size)
{
    PyBytesObject *new;

    assert(size >= 0);

    new = PyObject_New(PyBytesObject, &PyBytes_Type);
    if (new == NULL)
        return NULL;

    new->ob_size = size;
    if (size == 0)
        new->ob_bytes = NULL;
    else {
        new->ob_bytes = PyMem_Malloc(size);
        if (new->ob_bytes == NULL) {
            Py_DECREF(new);
            return NULL;
        }
        if (bytes != NULL)
            memcpy(new->ob_bytes, bytes, size);
    }
    
    return (PyObject *)new;
}

Py_ssize_t
PyBytes_Size(PyObject *self)
{
    assert(self != NULL);
    assert(PyBytes_Check(self));

    return ((PyBytesObject *)self)->ob_size;
}

char  *
PyBytes_AsString(PyObject *self)
{
    assert(self != NULL);
    assert(PyBytes_Check(self));

    return ((PyBytesObject *)self)->ob_bytes;
}

int
PyBytes_Resize(PyObject *self, Py_ssize_t size)
{
    void *sval;

    assert(self != NULL);
    assert(PyBytes_Check(self));
    assert(size >= 0);

    sval = PyMem_Realloc(((PyBytesObject *)self)->ob_bytes, size);
    if (sval == NULL) {
        PyErr_NoMemory();
        return -1;
    }

    ((PyBytesObject *)self)->ob_bytes = sval;
    ((PyBytesObject *)self)->ob_size = size;

    return 0;
}

/* Functions stuffed into the type object */

static Py_ssize_t
bytes_length(PyBytesObject *self)
{
    return self->ob_size;
}

static PyObject *
bytes_concat(PyBytesObject *self, PyObject *other)
{
    PyBytesObject *result;
    Py_ssize_t mysize;
    Py_ssize_t size;

    if (!PyBytes_Check(other)) {
        PyErr_Format(PyExc_TypeError,
                     "can't concat bytes to %.100s", other->ob_type->tp_name);
        return NULL;
    }
    
    mysize = self->ob_size;
    size = mysize + ((PyBytesObject *)other)->ob_size;
    if (size < 0)
        return PyErr_NoMemory();
    result = (PyBytesObject *) PyBytes_FromStringAndSize(NULL, size);
    if (result != NULL) {
        memcpy(result->ob_bytes, self->ob_bytes, self->ob_size);
        memcpy(result->ob_bytes + self->ob_size,
               ((PyBytesObject *)other)->ob_bytes,
               ((PyBytesObject *)other)->ob_size);
    }
    return (PyObject *)result;
}

static PyObject *
bytes_repeat(PyBytesObject *self, Py_ssize_t count)
{
    PyBytesObject *result;
    Py_ssize_t mysize;
    Py_ssize_t size;

    if (count < 0)
        count = 0;
    mysize = self->ob_size;
    size = mysize * count;
    if (count != 0 && size / count != mysize)
        return PyErr_NoMemory();
    result = (PyBytesObject *)PyBytes_FromStringAndSize(NULL,  size);
    if (result != NULL && size != 0) {
        if (mysize == 1)
            memset(result->ob_bytes, self->ob_bytes[0], size);
        else {
            int i;
            for (i = 0; i < count; i++)
                memcpy(result->ob_bytes + i*mysize, self->ob_bytes, mysize);
        }
    }
    return (PyObject *)result;
}

static PyObject *
bytes_getitem(PyBytesObject *self, Py_ssize_t i)
{
    if (i < 0)
        i += self->ob_size;
    if (i < 0 || i >= self->ob_size) {
        PyErr_SetString(PyExc_IndexError, "bytes index out of range");
        return NULL;
    }
    return PyInt_FromLong((unsigned char)(self->ob_bytes[i]));
}

static PyObject *
bytes_getslice(PyBytesObject *self, Py_ssize_t lo, Py_ssize_t hi)
{
    if (lo < 0)
        lo = 0;
    if (hi > self->ob_size)
        hi = self->ob_size;
    if (lo >= hi)
        lo = hi = 0;
    return PyBytes_FromStringAndSize(self->ob_bytes + lo, hi - lo);
}

static int
bytes_setslice(PyBytesObject *self, Py_ssize_t lo, Py_ssize_t hi, 
               PyObject *values)
{
    int avail;
    int needed;
    char *bytes;

    if (values == NULL) {
        bytes = NULL;
        needed = 0;
    }
    else if (values == (PyObject *)self || !PyBytes_Check(values)) {
        /* Make a copy an call this function recursively */
        int err;
        values = PyBytes_FromObject(values);
        if (values == NULL)
            return -1;
        err = bytes_setslice(self, lo, hi, values);
        Py_DECREF(values);
        return err;
    }
    else {
        assert(PyBytes_Check(values));
        bytes = ((PyBytesObject *)values)->ob_bytes;
        needed = ((PyBytesObject *)values)->ob_size;
    }

    if (lo < 0)
        lo = 0;
    if (hi > self->ob_size)
        hi = self->ob_size;

    avail = hi - lo;
    if (avail < 0)
        lo = hi = avail = 0;

    if (avail != needed) {
        if (avail > needed) {
            /*
              0   lo               hi               old_size
              |   |<----avail----->|<-----tomove------>|
              |   |<-needed->|<-----tomove------>|
              0   lo      new_hi              new_size
            */
            memmove(self->ob_bytes + lo + needed, self->ob_bytes + hi,
                    self->ob_size - hi);
        }
        if (PyBytes_Resize((PyObject *)self, 
                           self->ob_size + needed - avail) < 0)
            return -1;
        if (avail < needed) {
            /*
              0   lo        hi               old_size
              |   |<-avail->|<-----tomove------>|
              |   |<----needed---->|<-----tomove------>|
              0   lo            new_hi              new_size
             */
            memmove(self->ob_bytes + lo + needed, self->ob_bytes + hi,
                    self->ob_size - lo - needed);
        }
    }

    if (needed > 0)
        memcpy(self->ob_bytes + lo, bytes, needed);

    return 0;
}

static int
bytes_setitem(PyBytesObject *self, Py_ssize_t i, PyObject *value)
{
    Py_ssize_t ival;

    if (i < 0)
        i += self->ob_size;

    if (i < 0 || i >= self->ob_size) {
        PyErr_SetString(PyExc_IndexError, "bytes index out of range");
        return -1;
    }

    if (value == NULL)
        return bytes_setslice(self, i, i+1, NULL);

    ival = PyNumber_Index(value);
    if (ival == -1 && PyErr_Occurred())
        return -1;

    if (ival < 0 || ival >= 256) {
        PyErr_SetString(PyExc_ValueError, "byte must be in range(0, 256)");
        return -1;
    }

    self->ob_bytes[i] = ival;
    return 0;
}

static long
bytes_nohash(PyObject *self)
{
    PyErr_SetString(PyExc_TypeError, "bytes objects are unhashable");
    return -1;
}

static int
bytes_init(PyBytesObject *self, PyObject *args, PyObject *kwds)
{
    static char *kwlist[] = {"source", "encoding", "errors", 0};
    PyObject *arg = NULL;
    const char *encoding = NULL;
    const char *errors = NULL;
    Py_ssize_t count;
    PyObject *it;
    PyObject *(*iternext)(PyObject *);

    /* Empty previous contents (yes, do this first of all!) */
    if (PyBytes_Resize((PyObject *)self, 0) < 0)
        return -1;

    /* Parse arguments */
    if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:bytes", kwlist,
                                     &arg, &encoding, &errors))
        return -1;

    /* Make a quick exit if no first argument */
    if (arg == NULL) {
        if (encoding != NULL || errors != NULL) {
            PyErr_SetString(PyExc_TypeError,
                            "encoding or errors without sequence argument");
            return -1;
        }
        return 0;
    }

    if (PyUnicode_Check(arg)) {
        /* Encode via the codec registry */
        PyObject *encoded;
        char *bytes;
        Py_ssize_t size;
        if (encoding == NULL)
            encoding = PyUnicode_GetDefaultEncoding();
        encoded = PyCodec_Encode(arg, encoding, errors);
        if (encoded == NULL)
            return -1;
        if (!PyString_Check(encoded)) {
            PyErr_Format(PyExc_TypeError,
                "encoder did not return a string object (type=%.400s)",
                encoded->ob_type->tp_name);
            Py_DECREF(encoded);
            return -1;
        }
        bytes = PyString_AS_STRING(encoded);
        size = PyString_GET_SIZE(encoded);
        if (PyBytes_Resize((PyObject *)self, size) < 0) {
            Py_DECREF(encoded);
            return -1;
        }
        memcpy(self->ob_bytes, bytes, size);
        Py_DECREF(encoded);
        return 0;
    }

    /* If it's not unicode, there can't be encoding or errors */
    if (encoding != NULL || errors != NULL) {
        PyErr_SetString(PyExc_TypeError,
                        "encoding or errors without a string argument");
        return -1;
    }

    /* Is it an int? */
    count = PyNumber_Index(arg);
    if (count == -1 && PyErr_Occurred())
        PyErr_Clear();
    else {
        if (count < 0) {
            PyErr_SetString(PyExc_ValueError, "negative count");
            return -1;
        }
        if (count > 0) {
            if (PyBytes_Resize((PyObject *)self, count))
                return -1;
            memset(self->ob_bytes, 0, count);
        }
        return 0;
    }

    if (PyObject_CheckReadBuffer(arg)) {
        const void *bytes;
        Py_ssize_t size;
        if (PyObject_AsReadBuffer(arg, &bytes, &size) < 0)
            return -1;
        if (PyBytes_Resize((PyObject *)self, size) < 0)
            return -1;
        memcpy(self->ob_bytes, bytes, size);
        return 0;
    }

    /* XXX Optimize this if the arguments is a list, tuple */

    /* Get the iterator */
    it = PyObject_GetIter(arg);
    if (it == NULL)
        return -1;
    iternext = *it->ob_type->tp_iternext;

    /* Run the iterator to exhaustion */
    for (;;) {
        PyObject *item;
        Py_ssize_t value;

        /* Get the next item */
        item = iternext(it);
        if (item == NULL) {
            if (PyErr_Occurred()) {
                if (!PyErr_ExceptionMatches(PyExc_StopIteration))
                    goto error;
                PyErr_Clear();
            }
            break;
        }

        /* Interpret it as an int (__index__) */
        value = PyNumber_Index(item);
        Py_DECREF(item);
        if (value == -1 && PyErr_Occurred())
            goto error;

        /* Range check */
        if (value < 0 || value >= 256) {
            PyErr_SetString(PyExc_ValueError,
                            "bytes must be in range(0, 256)");
            goto error;
        }

        /* Append the byte */
        /* XXX Speed this up */
        if (PyBytes_Resize((PyObject *)self, self->ob_size+1) < 0)
            goto error;
        self->ob_bytes[self->ob_size-1] = value;
    }

    /* Clean up and return success */
    Py_DECREF(it);
    return 0;

 error:
    /* Error handling when it != NULL */
    Py_DECREF(it);
    return -1;
}

static PyObject *
bytes_repr(PyBytesObject *self)
{
    PyObject *list;
    PyObject *str;
    PyObject *result;
    int err;
    int i;

    if (self->ob_size == 0)
        return PyString_FromString("bytes()");

    list = PyList_New(0);
    if (list == NULL)
        return NULL;

    str = PyString_FromString("bytes([");
    if (str == NULL)
        goto error;

    err = PyList_Append(list, str);
    Py_DECREF(str);
    if (err < 0)
        goto error;

    for (i = 0; i < self->ob_size; i++) {
        char buffer[20];
        sprintf(buffer, ", 0x%02x", (unsigned char) (self->ob_bytes[i]));
        str = PyString_FromString((i == 0) ? buffer+2 : buffer);
        if (str == NULL)
            goto error;
        err = PyList_Append(list, str);
        Py_DECREF(str);
        if (err < 0)
            goto error;
    }

    str = PyString_FromString("])");
    if (str == NULL)
        goto error;

    err = PyList_Append(list, str);
    Py_DECREF(str);
    if (err < 0)
        goto error;
    
    str = PyString_FromString("");
    if (str == NULL)
        goto error;

    result = _PyString_Join(str, list);
    Py_DECREF(str);
    Py_DECREF(list);
    return result;

 error:
    /* Error handling when list != NULL  */
    Py_DECREF(list);
    return NULL;
}

static PyObject *
bytes_str(PyBytesObject *self)
{
    return PyString_FromStringAndSize(self->ob_bytes, self->ob_size);
}

static PyObject *
bytes_richcompare(PyBytesObject *self, PyBytesObject *other, int op)
{
    PyObject *res;
    int minsize;
    int cmp;

    if (!PyBytes_Check(self) || !PyBytes_Check(other)) {
        Py_INCREF(Py_NotImplemented);
        return Py_NotImplemented;
    }

    if (self->ob_size != other->ob_size && (op == Py_EQ || op == Py_NE)) {
        /* Shortcut: if the lengths differ, the objects differ */
        cmp = (op == Py_NE);
    }
    else {
        minsize = self->ob_size;
        if (other->ob_size < minsize)
            minsize = other->ob_size;

        cmp = memcmp(self->ob_bytes, other->ob_bytes, minsize);
        /* In ISO C, memcmp() guarantees to use unsigned bytes! */

        if (cmp == 0) {
            if (self->ob_size < other->ob_size)
                cmp = -1;
            else if (self->ob_size > other->ob_size)
                cmp = 1;
        }

        switch (op) {
        case Py_LT: cmp = cmp <  0; break;
        case Py_LE: cmp = cmp <= 0; break;
        case Py_EQ: cmp = cmp == 0; break;
        case Py_NE: cmp = cmp != 0; break;
        case Py_GT: cmp = cmp >  0; break;
        case Py_GE: cmp = cmp >= 0; break;
        }
    }

    res = cmp ? Py_True : Py_False;
    Py_INCREF(res);
    return res;
}

static void
bytes_dealloc(PyBytesObject *self)
{
    if (self->ob_bytes != 0) {
        PyMem_Free(self->ob_bytes);
    }
    self->ob_type->tp_free((PyObject *)self);
}

static Py_ssize_t
bytes_getbuffer(PyBytesObject *self, Py_ssize_t index, const void **ptr)
{
    if (index != 0) {
        PyErr_SetString(PyExc_SystemError,
                        "accessing non-existent string segment");
        return -1;
    }
    *ptr = (void *)self->ob_bytes;
    return self->ob_size;
}

static Py_ssize_t
bytes_getsegcount(PyStringObject *self, Py_ssize_t *lenp)
{
    if (lenp)
        *lenp = self->ob_size;
    return 1;
}

PyDoc_STRVAR(decode_doc,
"B.decode([encoding[,errors]]) -> unicode obect.\n\
\n\
Decodes B using the codec registered for encoding. encoding defaults\n\
to the default encoding. errors may be given to set a different error\n\
handling scheme. Default is 'strict' meaning that encoding errors raise\n\
a UnicodeDecodeError. Other possible values are 'ignore' and 'replace'\n\
as well as any other name registerd with codecs.register_error that is\n\
able to handle UnicodeDecodeErrors.");

static PyObject *
bytes_decode(PyObject *self, PyObject *args)
{ 
    const char *encoding = NULL;
    const char *errors = NULL;

    if (!PyArg_ParseTuple(args, "|ss:decode", &encoding, &errors))
        return NULL;
    if (encoding == NULL)
        encoding = PyUnicode_GetDefaultEncoding();
    return PyCodec_Decode(self, encoding, errors);
}

static PySequenceMethods bytes_as_sequence = {
    (lenfunc)bytes_length,              /*sq_length*/
    (binaryfunc)bytes_concat,           /*sq_concat*/
    (ssizeargfunc)bytes_repeat,         /*sq_repeat*/
    (ssizeargfunc)bytes_getitem,        /*sq_item*/
    (ssizessizeargfunc)bytes_getslice,  /*sq_slice*/
    (ssizeobjargproc)bytes_setitem,     /*sq_ass_item*/
    (ssizessizeobjargproc)bytes_setslice, /* sq_ass_slice */
#if 0
    (objobjproc)bytes_contains,         /* sq_contains */
    (binaryfunc)bytes_inplace_concat,   /* sq_inplace_concat */
    (ssizeargfunc)bytes_inplace_repeat, /* sq_inplace_repeat */
#endif
};

static PyMappingMethods bytes_as_mapping = {
    (lenfunc)bytes_length,
    (binaryfunc)0,
    0,
};

static PyBufferProcs bytes_as_buffer = {
    (readbufferproc)bytes_getbuffer,
    (writebufferproc)bytes_getbuffer,
    (segcountproc)bytes_getsegcount,
    /* XXX Bytes are not characters! But we need to implement
       bf_getcharbuffer() so we can be used as 't#' argument to codecs. */
    (charbufferproc)bytes_getbuffer,
};

static PyMethodDef
bytes_methods[] = {
    {"decode", (PyCFunction)bytes_decode, METH_VARARGS, decode_doc},
    {NULL,     NULL}
};

PyDoc_STRVAR(bytes_doc,
"bytes([iterable]) -> new array of bytes.\n\
\n\
If an argument is given it must be an iterable yielding ints in range(256).");

PyTypeObject PyBytes_Type = {
    PyObject_HEAD_INIT(&PyType_Type)
    0,
    "bytes",
    sizeof(PyBytesObject),
    0,
    (destructor)bytes_dealloc,          /* tp_dealloc */
    0,                                  /* tp_print */
    0,                                  /* tp_getattr */
    0,                                  /* tp_setattr */
    0,                                  /* tp_compare */
    (reprfunc)bytes_repr,               /* tp_repr */
    0,                                  /* tp_as_number */
    &bytes_as_sequence,                 /* tp_as_sequence */
    &bytes_as_mapping,                  /* tp_as_mapping */
    bytes_nohash,                       /* tp_hash */
    0,                                  /* tp_call */
    (reprfunc)bytes_str,                /* tp_str */
    PyObject_GenericGetAttr,            /* tp_getattro */
    0,                                  /* tp_setattro */
    &bytes_as_buffer,                   /* tp_as_buffer */
    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES, /* tp_flags */ 
                                        /* bytes is 'final' or 'sealed' */
    bytes_doc,                          /* tp_doc */
    0,                                  /* tp_traverse */
    0,                                  /* tp_clear */
    (richcmpfunc)bytes_richcompare,     /* tp_richcompare */
    0,                                  /* tp_weaklistoffset */
    0,                                  /* tp_iter */
    0,                                  /* tp_iternext */
    bytes_methods,                      /* tp_methods */
    0,                                  /* tp_members */
    0,                                  /* tp_getset */
    0,                                  /* tp_base */
    0,                                  /* tp_dict */
    0,                                  /* tp_descr_get */
    0,                                  /* tp_descr_set */
    0,                                  /* tp_dictoffset */
    (initproc)bytes_init,               /* tp_init */
    PyType_GenericAlloc,                /* tp_alloc */
    PyType_GenericNew,                  /* tp_new */
    PyObject_Del,                       /* tp_free */
};