Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:reformatted invalid_utf8 to make it conform a bit better to existing style
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | invalid_utf8_table
Files: files | file ages | folders
SHA1:dd3bb22cd726f26e785776d50f605a469a03870d
User & Date: sdr 2016-06-11 00:41:59
Context
2016-06-11
05:23
Further coding style improvements for the new invalid_utf8() function. Also fixes several C99-isms. check-in: 2fb7d59b user: mistachkin tags: invalid_utf8_table
00:41
reformatted invalid_utf8 to make it conform a bit better to existing style check-in: dd3bb22c user: sdr tags: invalid_utf8_table
00:13
merged from trunk check-in: 4f906e53 user: sdr tags: invalid_utf8_table
Changes

Changes to src/lookslike.c.

141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
...
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
** except for the "overlong form" of \u0000 (Modified UTF-8)
** which is not considered invalid here: Some languages like
** Java and Tcl use it. This function also considers valid
** the derivatives CESU-8 & WTF-8 (as described in the same
** wikipedia article referenced previously).
*/

int invalid_utf8(const Blob *pContent)
{
  /* definitions for various utf-8 sequence lengths */
  static unsigned char def_2a[] = { 2, 0xC0, 0xC0, 0x80, 0x80 };
  static unsigned char def_2b[] = { 2, 0xC2, 0xDF, 0x80, 0xBF };
  static unsigned char def_3a[] = { 3, 0xE0, 0xE0, 0xA0, 0xBF, 0x80, 0xBF };
  static unsigned char def_3b[] = { 3, 0xE1, 0xEF, 0x80, 0xBF, 0x80, 0xBF };
  static unsigned char def_4a[] = { 4, 0xF0, 0xF0, 0x90, 0xBF, 0x80, 0xBF, 0x80, 0xBF };
  static unsigned char def_4b[] = { 4, 0xF1, 0xF3, 0x80, 0xBF, 0x80, 0xBF, 0x80, 0xBF };
................................................................................
  /* a table used for quick lookup of the definition that goes with a particular lead byte */
  static unsigned char* lb_tab[256] = { NULL };

  /* a pointer to the table; NULL means not yet setup */
  static unsigned char** lb_ptr = NULL;

  /* if the table pointer hasn't been initialized */
  if (lb_ptr == NULL)
  {
    lb_ptr = lb_tab;

    /* for each definition, set the lead byte table pointer to the proper definition */
    unsigned char** pp = def_arr;
    while (*pp != NULL)
    {
      unsigned char lo = pp[0][1];
      unsigned char hi = pp[0][2];
      unsigned char i;
      for (i = lo; i <= hi; ++i)
        lb_ptr[i] = pp[0];
      ++pp;
    }
................................................................................
  }

  /* buffer pointer and size */
  const unsigned char *z = (unsigned char *)blob_buffer(pContent);
  unsigned int n = blob_size(pContent);

  /* while we haven't checked all the bytes in the buffer */
  while (n > 0)
  {
    /* ascii is trivial */
    if (*z < 0x80)
    {
      ++z;
      --n;
    }
    else
    {
      /* get the definition for this lead byte */
      unsigned char* def = lb_ptr[*z++];
      unsigned char i, len;

      /* if the definition doesn't exist, return invalid */
      if (!def)
        return LOOK_INVALID;

      /* get the expected sequence length */
      len = *def;

      /* if there aren't enough bytes left, return invalid */
      if (n < len)
        return LOOK_INVALID;

      /* skip the length & lead byte range */
      def += 3;

      /* we already know byte #0 is good, so check the remaining bytes */
      for (i = 1; i < len; ++i)
      {
        /* if the byte is outside the allowed range for this definition, return invalid */
        if ((*z < *def++) || (*z++ > *def++))
          return LOOK_INVALID;
      }

      /* advance to the next sequence */
      n -= len;
    }
  }

  /* we made it all the way through the buffer so it's not invalid */







|
<







 







|
<




|
<







 







|
|

|
<


<
|
<





<
|





<
|






<



<







141
142
143
144
145
146
147
148

149
150
151
152
153
154
155
...
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
** except for the "overlong form" of \u0000 (Modified UTF-8)
** which is not considered invalid here: Some languages like
** Java and Tcl use it. This function also considers valid
** the derivatives CESU-8 & WTF-8 (as described in the same
** wikipedia article referenced previously).
*/

int invalid_utf8(const Blob *pContent) {

  /* definitions for various utf-8 sequence lengths */
  static unsigned char def_2a[] = { 2, 0xC0, 0xC0, 0x80, 0x80 };
  static unsigned char def_2b[] = { 2, 0xC2, 0xDF, 0x80, 0xBF };
  static unsigned char def_3a[] = { 3, 0xE0, 0xE0, 0xA0, 0xBF, 0x80, 0xBF };
  static unsigned char def_3b[] = { 3, 0xE1, 0xEF, 0x80, 0xBF, 0x80, 0xBF };
  static unsigned char def_4a[] = { 4, 0xF0, 0xF0, 0x90, 0xBF, 0x80, 0xBF, 0x80, 0xBF };
  static unsigned char def_4b[] = { 4, 0xF1, 0xF3, 0x80, 0xBF, 0x80, 0xBF, 0x80, 0xBF };
................................................................................
  /* a table used for quick lookup of the definition that goes with a particular lead byte */
  static unsigned char* lb_tab[256] = { NULL };

  /* a pointer to the table; NULL means not yet setup */
  static unsigned char** lb_ptr = NULL;

  /* if the table pointer hasn't been initialized */
  if (lb_ptr == NULL) {

    lb_ptr = lb_tab;

    /* for each definition, set the lead byte table pointer to the proper definition */
    unsigned char** pp = def_arr;
    while (*pp != NULL) {

      unsigned char lo = pp[0][1];
      unsigned char hi = pp[0][2];
      unsigned char i;
      for (i = lo; i <= hi; ++i)
        lb_ptr[i] = pp[0];
      ++pp;
    }
................................................................................
  }

  /* buffer pointer and size */
  const unsigned char *z = (unsigned char *)blob_buffer(pContent);
  unsigned int n = blob_size(pContent);

  /* while we haven't checked all the bytes in the buffer */
  while (n > 0) {

    /* ascii is trivial */
    if (*z < 0x80) {

      ++z;
      --n;

    } else {

      /* get the definition for this lead byte */
      unsigned char* def = lb_ptr[*z++];
      unsigned char i, len;

      /* if the definition doesn't exist, return invalid */

      if (!def) return LOOK_INVALID;

      /* get the expected sequence length */
      len = *def;

      /* if there aren't enough bytes left, return invalid */

      if (n < len) return LOOK_INVALID;

      /* skip the length & lead byte range */
      def += 3;

      /* we already know byte #0 is good, so check the remaining bytes */
      for (i = 1; i < len; ++i)

        /* if the byte is outside the allowed range for this definition, return invalid */
        if ((*z < *def++) || (*z++ > *def++))
          return LOOK_INVALID;


      /* advance to the next sequence */
      n -= len;
    }
  }

  /* we made it all the way through the buffer so it's not invalid */