~ubuntu-branches/ubuntu/natty/python2.6/natty-security

« back to all changes in this revision

Viewing changes to Objects/unicodeobject.c

  • Committer: Bazaar Package Importer
  • Author(s): Scott Kitterman
  • Date: 2010-06-24 00:31:14 UTC
  • mfrom: (10.1.18 sid)
  • Revision ID: james.westby@ubuntu.com-20100624003114-jmwmbudlpucl6ip3
Tags: 2.6.5+20100616-1ubuntu1
* Merge from Debian Unstable.  Remaining Ubuntu changes:
  - Add new symbols to libpython.symbols.in
  - Re-enable the profiled build on all architectures
  - Priority for python2.6-minimal is required instead of optional
  - python2.6-minimal and python2.6 Conflict python-central
    (<< 0.6.11ubuntu6)

Show diffs side-by-side

added added

removed removed

Lines of Context:
2085
2085
    PyUnicodeObject *unicode;
2086
2086
    Py_UNICODE *p;
2087
2087
#ifndef Py_UNICODE_WIDE
2088
 
    int i, pairs;
 
2088
    int pairs = 0;
2089
2089
#else
2090
2090
    const int pairs = 0;
2091
2091
#endif
2092
 
    const unsigned char *q, *e;
 
2092
    const unsigned char *q, *e, *qq;
2093
2093
    int bo = 0;       /* assume native ordering by default */
2094
2094
    const char *errmsg = "";
2095
2095
    /* Offsets from q for retrieving bytes in the right order. */
2100
2100
#endif
2101
2101
    PyObject *errorHandler = NULL;
2102
2102
    PyObject *exc = NULL;
 
2103
    
 
2104
    q = (unsigned char *)s;
 
2105
    e = q + size;
 
2106
 
 
2107
    if (byteorder)
 
2108
        bo = *byteorder;
 
2109
 
 
2110
    /* Check for BOM marks (U+FEFF) in the input and adjust current
 
2111
       byte order setting accordingly. In native mode, the leading BOM
 
2112
       mark is skipped, in all other modes, it is copied to the output
 
2113
       stream as-is (giving a ZWNBSP character). */
 
2114
    if (bo == 0) {
 
2115
        if (size >= 4) {
 
2116
            const Py_UCS4 bom = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) |
 
2117
                (q[iorder[1]] << 8) | q[iorder[0]];
 
2118
#ifdef BYTEORDER_IS_LITTLE_ENDIAN
 
2119
            if (bom == 0x0000FEFF) {
 
2120
                q += 4;
 
2121
                bo = -1;
 
2122
            }
 
2123
            else if (bom == 0xFFFE0000) {
 
2124
                q += 4;
 
2125
                bo = 1;
 
2126
            }
 
2127
#else
 
2128
            if (bom == 0x0000FEFF) {
 
2129
                q += 4;
 
2130
                bo = 1;
 
2131
            }
 
2132
            else if (bom == 0xFFFE0000) {
 
2133
                q += 4;
 
2134
                bo = -1;
 
2135
            }
 
2136
#endif
 
2137
        }
 
2138
    }
 
2139
 
 
2140
    if (bo == -1) {
 
2141
        /* force LE */
 
2142
        iorder[0] = 0;
 
2143
        iorder[1] = 1;
 
2144
        iorder[2] = 2;
 
2145
        iorder[3] = 3;
 
2146
    }
 
2147
    else if (bo == 1) {
 
2148
        /* force BE */
 
2149
        iorder[0] = 3;
 
2150
        iorder[1] = 2;
 
2151
        iorder[2] = 1;
 
2152
        iorder[3] = 0;
 
2153
    }
 
2154
 
2103
2155
    /* On narrow builds we split characters outside the BMP into two
2104
2156
       codepoints => count how much extra space we need. */
2105
2157
#ifndef Py_UNICODE_WIDE
2106
 
    for (i = pairs = 0; i < size/4; i++)
2107
 
        if (((Py_UCS4 *)s)[i] >= 0x10000)
 
2158
    for (qq = q; qq < e; qq += 4)
 
2159
        if (qq[iorder[2]] != 0 || qq[iorder[3]] != 0)
2108
2160
            pairs++;
2109
2161
#endif
2110
2162
 
2117
2169
 
2118
2170
    /* Unpack UTF-32 encoded data */
2119
2171
    p = unicode->str;
2120
 
    q = (unsigned char *)s;
2121
 
    e = q + size;
2122
 
 
2123
 
    if (byteorder)
2124
 
        bo = *byteorder;
2125
 
 
2126
 
    /* Check for BOM marks (U+FEFF) in the input and adjust current
2127
 
       byte order setting accordingly. In native mode, the leading BOM
2128
 
       mark is skipped, in all other modes, it is copied to the output
2129
 
       stream as-is (giving a ZWNBSP character). */
2130
 
    if (bo == 0) {
2131
 
        if (size >= 4) {
2132
 
            const Py_UCS4 bom = (q[iorder[3]] << 24) | (q[iorder[2]] << 16) |
2133
 
                (q[iorder[1]] << 8) | q[iorder[0]];
2134
 
#ifdef BYTEORDER_IS_LITTLE_ENDIAN
2135
 
            if (bom == 0x0000FEFF) {
2136
 
                q += 4;
2137
 
                bo = -1;
2138
 
            }
2139
 
            else if (bom == 0xFFFE0000) {
2140
 
                q += 4;
2141
 
                bo = 1;
2142
 
            }
2143
 
#else
2144
 
            if (bom == 0x0000FEFF) {
2145
 
                q += 4;
2146
 
                bo = 1;
2147
 
            }
2148
 
            else if (bom == 0xFFFE0000) {
2149
 
                q += 4;
2150
 
                bo = -1;
2151
 
            }
2152
 
#endif
2153
 
        }
2154
 
    }
2155
 
 
2156
 
    if (bo == -1) {
2157
 
        /* force LE */
2158
 
        iorder[0] = 0;
2159
 
        iorder[1] = 1;
2160
 
        iorder[2] = 2;
2161
 
        iorder[3] = 3;
2162
 
    }
2163
 
    else if (bo == 1) {
2164
 
        /* force BE */
2165
 
        iorder[0] = 3;
2166
 
        iorder[1] = 2;
2167
 
        iorder[2] = 1;
2168
 
        iorder[3] = 0;
2169
 
    }
2170
2172
 
2171
2173
    while (q < e) {
2172
2174
        Py_UCS4 ch;