~pythonregexp2.7/python/issue2636-01+09-02

« back to all changes in this revision

Viewing changes to Modules/binascii.c

  • Committer: Jeffrey C. "The TimeHorse" Jacobs
  • Date: 2008-09-22 00:16:16 UTC
  • mfrom: (39022.1.34 Regexp-2.7)
  • Revision ID: darklord@timehorse.com-20080922001616-p1wdip9lfp0zl5cu
Merged in changes from the Atomic Grouping / Possessive Qualifiers branch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
141
141
#define BASE64_PAD '='
142
142
 
143
143
/* Max binary chunk size; limited only by available memory */
144
 
#define BASE64_MAXBIN (INT_MAX/2 - sizeof(PyStringObject) - 3)
 
144
#define BASE64_MAXBIN (PY_SSIZE_T_MAX/2 - sizeof(PyStringObject) - 3)
145
145
 
146
146
static unsigned char table_b2a_base64[] =
147
147
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
198
198
        if ( !PyArg_ParseTuple(args, "t#:a2b_uu", &ascii_data, &ascii_len) )
199
199
                return NULL;
200
200
 
 
201
        assert(ascii_len >= 0);
 
202
 
201
203
        /* First byte: binary data length (in bytes) */
202
204
        bin_len = (*ascii_data++ - ' ') & 077;
203
205
        ascii_len--;
351
353
        if ( !PyArg_ParseTuple(args, "t#:a2b_base64", &ascii_data, &ascii_len) )
352
354
                return NULL;
353
355
 
 
356
        assert(ascii_len >= 0);
 
357
 
 
358
        if (ascii_len > PY_SSIZE_T_MAX - 3)
 
359
                return PyErr_NoMemory();
 
360
 
354
361
        bin_len = ((ascii_len+3)/4)*3; /* Upper bound, corrected later */
355
362
 
356
363
        /* Allocate the buffer */
440
447
 
441
448
        if ( !PyArg_ParseTuple(args, "s#:b2a_base64", &bin_data, &bin_len) )
442
449
                return NULL;
 
450
 
 
451
        assert(bin_len >= 0);
 
452
 
443
453
        if ( bin_len > BASE64_MAXBIN ) {
444
454
                PyErr_SetString(Error, "Too much data for base64 line");
445
455
                return NULL;
495
505
        if ( !PyArg_ParseTuple(args, "t#:a2b_hqx", &ascii_data, &len) )
496
506
                return NULL;
497
507
 
 
508
        assert(len >= 0);
 
509
 
 
510
        if (len > PY_SSIZE_T_MAX - 2)
 
511
                return PyErr_NoMemory();
 
512
 
498
513
        /* Allocate a string that is too big (fixed later) 
499
514
           Add two to the initial length to prevent interning which
500
515
           would preclude subsequent resizing.  */
558
573
        if ( !PyArg_ParseTuple(args, "s#:rlecode_hqx", &in_data, &len) )
559
574
                return NULL;
560
575
 
 
576
        assert(len >= 0);
 
577
 
 
578
        if (len > PY_SSIZE_T_MAX / 2 - 2)
 
579
                return PyErr_NoMemory();
 
580
 
561
581
        /* Worst case: output is twice as big as input (fixed later) */
562
582
        if ( (rv=PyString_FromStringAndSize(NULL, len*2+2)) == NULL )
563
583
                return NULL;
607
627
        if ( !PyArg_ParseTuple(args, "s#:b2a_hqx", &bin_data, &len) )
608
628
                return NULL;
609
629
 
 
630
        assert(len >= 0);
 
631
 
 
632
        if (len > PY_SSIZE_T_MAX / 2 - 2)
 
633
                return PyErr_NoMemory();
 
634
 
610
635
        /* Allocate a buffer that is at least large enough */
611
636
        if ( (rv=PyString_FromStringAndSize(NULL, len*2+2)) == NULL )
612
637
                return NULL;
645
670
        if ( !PyArg_ParseTuple(args, "s#:rledecode_hqx", &in_data, &in_len) )
646
671
                return NULL;
647
672
 
 
673
        assert(in_len >= 0);
 
674
 
648
675
        /* Empty string is a special case */
649
676
        if ( in_len == 0 )
650
677
                return PyString_FromString("");
 
678
    else if (in_len > PY_SSIZE_T_MAX / 2)
 
679
        return PyErr_NoMemory();
651
680
 
652
681
        /* Allocate a buffer of reasonable size. Resized when needed */
653
682
        out_len = in_len*2;
673
702
#define OUTBYTE(b) \
674
703
        do { \
675
704
                 if ( --out_len_left < 0 ) { \
 
705
                          if ( out_len > PY_SSIZE_T_MAX / 2) return PyErr_NoMemory(); \
676
706
                          _PyString_Resize(&rv, 2*out_len); \
677
707
                          if ( rv == NULL ) return NULL; \
678
708
                          out_data = (unsigned char *)PyString_AsString(rv) \
741
771
        if ( !PyArg_ParseTuple(args, "s#i:crc_hqx", &bin_data, &len, &crc) )
742
772
                return NULL;
743
773
 
744
 
        while(len--) {
 
774
        while(len-- > 0) {
745
775
                crc=((crc<<8)&0xff00)^crctab_hqx[((crc>>8)&0xff)^*bin_data++];
746
776
        }
747
777
 
901
931
                return NULL;
902
932
 
903
933
        crc = ~ crc;
904
 
        while (len--)
 
934
        while (len-- > 0)
905
935
                crc = crc_32_tab[(crc ^ *bin_data++) & 0xffU] ^ (crc >> 8);
906
936
                /* Note:  (crc >> 8) MUST zero fill on left */
907
937
 
923
953
        if (!PyArg_ParseTuple(args, "s#:b2a_hex", &argbuf, &arglen))
924
954
                return NULL;
925
955
 
 
956
        assert(arglen >= 0);
 
957
        if (arglen > PY_SSIZE_T_MAX / 2)
 
958
                return PyErr_NoMemory();
 
959
 
926
960
        retval = PyString_FromStringAndSize(NULL, arglen*2);
927
961
        if (!retval)
928
962
                return NULL;
980
1014
        if (!PyArg_ParseTuple(args, "s#:a2b_hex", &argbuf, &arglen))
981
1015
                return NULL;
982
1016
 
 
1017
        assert(arglen >= 0);
 
1018
 
983
1019
        /* XXX What should we do about strings with an odd length?  Should
984
1020
         * we add an implicit leading zero, or a trailing zero?  For now,
985
1021
         * raise an exception.