Index: mysql-5.1-runtime/storage/csv/ha_tina.cc =================================================================== --- mysql-5.1-runtime.orig/storage/csv/ha_tina.cc +++ mysql-5.1-runtime/storage/csv/ha_tina.cc @@ -455,6 +455,48 @@ ha_tina::ha_tina(handlerton *hton, TABLE /* + Copy a buffer into the quoted format. +*/ + +static void encode_copy(String *buffer, String *attribute) +{ + const char *ptr, *end_ptr; + uint32 space_needed= attribute->length() + 2; + static const char accept[]= "\"\n\r\\"; + + ptr= end_ptr= attribute->c_ptr_safe(); + + while ((end_ptr= strpbrk(end_ptr, accept))) + end_ptr++, space_needed+= 2; + + buffer->reserve(space_needed, 0); + buffer->append('"'); + + do + { + end_ptr= strpbrk(ptr, accept); + if (end_ptr) + { + char ch= *end_ptr; + if (end_ptr - ptr) + buffer->q_append(ptr, end_ptr - ptr); + if (ch == '\n') + ch= 'n'; + else if (ch == '\r') + ch= 'r'; + buffer->q_append('\\'); + buffer->q_append(ch); + end_ptr++; + } + else if (*ptr) + buffer->append(ptr); + } while ((ptr= end_ptr)); + + buffer->append('"'); +} + + +/* Encode a buffer into the quoted format. */ @@ -469,10 +511,8 @@ int ha_tina::encode_quote(uchar *buf) for (Field **field=table->field ; *field ; field++) { - const char *ptr; - const char *end_ptr; const bool was_null= (*field)->is_null(); - + /* assistance for backwards compatibility in production builds. note: this will not work for ENUM columns. @@ -484,52 +524,14 @@ int ha_tina::encode_quote(uchar *buf) } (*field)->val_str(&attribute,&attribute); - + if (was_null) (*field)->set_null(); if ((*field)->str_needs_quotes()) - { - ptr= attribute.ptr(); - end_ptr= attribute.length() + ptr; - - buffer.append('"'); - - while (ptr < end_ptr) - { - if (*ptr == '"') - { - buffer.append('\\'); - buffer.append('"'); - *ptr++; - } - else if (*ptr == '\r') - { - buffer.append('\\'); - buffer.append('r'); - *ptr++; - } - else if (*ptr == '\\') - { - buffer.append('\\'); - buffer.append('\\'); - *ptr++; - } - else if (*ptr == '\n') - { - buffer.append('\\'); - buffer.append('n'); - *ptr++; - } - else - buffer.append(*ptr++); - } - buffer.append('"'); - } + encode_copy(&buffer, &attribute); else - { buffer.append(attribute); - } buffer.append(','); }