LCOV - code coverage report
Current view: top level - src/dblib - bcp.c (source / functions) Hit Total Coverage
Test: FreeTDS coverage Lines: 488 935 52.2 %
Date: 2026-03-24 22:22:09 Functions: 26 35 74.3 %

          Line data    Source code
       1             : /* FreeTDS - Library of routines accessing Sybase and Microsoft databases
       2             :  * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005  Brian Bruns
       3             :  * Copyright (C) 2010, 2011  Frediano Ziglio
       4             :  *
       5             :  * This library is free software; you can redistribute it and/or
       6             :  * modify it under the terms of the GNU Library General Public
       7             :  * License as published by the Free Software Foundation; either
       8             :  * version 2 of the License, or (at your option) any later version.
       9             :  *
      10             :  * This library is distributed in the hope that it will be useful,
      11             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      12             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      13             :  * Library General Public License for more details.
      14             :  *
      15             :  * You should have received a copy of the GNU Library General Public
      16             :  * License along with this library; if not, write to the
      17             :  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
      18             :  * Boston, MA 02111-1307, USA.
      19             :  */
      20             : 
      21             : #include <config.h>
      22             : 
      23             : #include <stdarg.h>
      24             : #include <stdio.h>
      25             : #include <assert.h>
      26             : 
      27             : #if HAVE_STRING_H
      28             : #include <string.h>
      29             : #endif /* HAVE_STRING_H */
      30             : 
      31             : #if HAVE_STDLIB_H
      32             : #include <stdlib.h>
      33             : #endif /* HAVE_STDLIB_H */
      34             : 
      35             : #if HAVE_UNISTD_H
      36             : #include <unistd.h>
      37             : #endif /* HAVE_UNISTD_H */
      38             : 
      39             : #ifdef _WIN32
      40             : #include <io.h>
      41             : #endif
      42             : 
      43             : #include <freetds/tds.h>
      44             : #include <freetds/tds/checks.h>
      45             : #include <freetds/tds/iconv.h>
      46             : #include <freetds/tds/convert.h>
      47             : #include <freetds/bytes.h>
      48             : #include <freetds/tds/stream.h>
      49             : #include <freetds/utils/string.h>
      50             : #include <freetds/encodings.h>
      51             : #include <freetds/replacements.h>
      52             : #include <sybfront.h>
      53             : #include <sybdb.h>
      54             : #include <syberror.h>
      55             : #include <dblib.h>
      56             : 
      57             : #define BCP_DEFAULT_DATEFMT "%Y-%m-%d %H:%M:%S.%z"
      58             : 
      59             : #define HOST_COL_CONV_ERROR 1
      60             : #define HOST_COL_NULL_ERROR 2
      61             : 
      62             : #ifdef HAVE_FSEEKO
      63             : typedef off_t offset_type;
      64             : #elif defined(_WIN32) || defined(_WIN64)
      65             : /* win32 version */
      66             : typedef __int64 offset_type;
      67             : # if defined(HAVE__FSEEKI64) && defined(HAVE__FTELLI64)
      68             : #  define fseeko(f,o,w) _fseeki64((f),o,w)
      69             : #  define ftello(f) _ftelli64((f))
      70             : # else
      71             : #  define fseeko(f,o,w) (_lseeki64(fileno(f),o,w) == -1 ? -1 : 0)
      72             : #  define ftello(f) _telli64(fileno(f))
      73             : # endif
      74             : #else
      75             : /* use old version */
      76             : #define fseeko(f,o,w) fseek(f,o,w)
      77             : #define ftello(f) ftell(f)
      78             : typedef long offset_type;
      79             : #endif
      80             : 
      81             : static void _bcp_free_storage(DBPROCESS * dbproc);
      82             : static void _bcp_free_columns(DBPROCESS * dbproc);
      83             : static void _bcp_null_error(TDSBCPINFO *bcpinfo, int index, int offset);
      84             : static TDSRET _bcp_get_col_data(TDSBCPINFO * bcpinfo, TDSCOLUMN * bindcol, int index, int offset);
      85             : static TDSRET _bcp_no_get_col_data(TDSBCPINFO * bcpinfo, TDSCOLUMN * bindcol, int index, int offset);
      86             : 
      87             : static int rtrim(char *, int);
      88             : static int rtrim_u16(uint16_t *str, int len, uint16_t space);
      89             : static STATUS _bcp_read_hostfile(DBPROCESS * dbproc, FILE * hostfile, bool *row_error, bool skip);
      90             : static int _bcp_readfmt_colinfo(DBPROCESS * dbproc, char *buf, BCP_HOSTCOLINFO * ci);
      91             : static int _bcp_get_term_var(const BYTE * pdata, const BYTE * term, int term_len);
      92             : static int _bcp_strftime(char *buf, size_t maxsize, const char *format, const TDSDATEREC * dr, int prec);
      93             : 
      94             : /*
      95             :  * "If a host file is being used ... the default data formats are as follows:
      96             :  *
      97             :  *  > The order, type, length and number of the columns in the host file are 
      98             :  *    assumed to be identical to the order, type and number of the columns in the database table.
      99             :  *  > If a given database column's data is fixed-length, 
     100             :  *    then the host file's  data column will also be fixed-length. 
     101             :  *  > If a given database column's data is variable-length or may contain null values, 
     102             :  *    the host file's data column will be prefixed by 
     103             :  *      a 4-byte length value for SYBTEXT and SYBIMAGE data types, and 
     104             :  *      a 1-byte length value for all other types.
     105             :  *  > There are no terminators of any kind between host file columns."
     106             :  */
     107             : 
     108             : static void
     109         328 : init_hostfile_columns(DBPROCESS *dbproc)
     110             : {
     111         328 :         const int ncols = dbproc->bcpinfo->bindinfo->num_cols;
     112             :         RETCODE erc;
     113             :         int icol;
     114             :         
     115         328 :         if (ncols == 0)
     116             :                 return;
     117             :                 
     118         328 :         if ((erc = bcp_columns(dbproc, ncols)) != SUCCEED) {
     119           0 :                 assert(erc == SUCCEED);
     120             :                 return;
     121             :         }
     122             :                 
     123        2016 :         for (icol = 0; icol < ncols; icol++) {
     124        1688 :                 const TDSCOLUMN *pcol = dbproc->bcpinfo->bindinfo->columns[icol];
     125        1688 :                 int prefixlen = 0, termlen = 0;
     126             :                 
     127        1688 :                 switch (pcol->column_type) {
     128             :                 case SYBTEXT:
     129             :                 case SYBIMAGE:
     130             :                         prefixlen = 4;
     131             :                         break;
     132        1548 :                 default:
     133        1548 :                         prefixlen = dbvarylen(dbproc, icol+1)? 1 : 0;
     134             :                 }
     135             :                 
     136        1688 :                 erc = bcp_colfmt(dbproc, icol+1, pcol->column_type, prefixlen, pcol->column_size, NULL, termlen, icol+1);
     137             :                 
     138        1688 :                 assert(erc == SUCCEED);
     139             :                 if (erc != SUCCEED)
     140             :                         return;
     141             :         }
     142             : }
     143             : 
     144             : 
     145             : /** 
     146             :  * \ingroup dblib_bcp
     147             :  * \brief Prepare for bulk copy operation on a table
     148             :  *
     149             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
     150             :  * \param tblname the name of the table receiving or providing the data.
     151             :  * \param hfile the data file opposite the table, if any.  
     152             :  * \param errfile the "error file" captures messages and, if errors are encountered, 
     153             :  *      copies of any rows that could not be written to the table.
     154             :  * \param direction one of 
     155             :  *              - \b DB_IN writing to the table
     156             :  *              - \b DB_OUT writing to the host file
     157             :  *              .
     158             :  * \remarks bcp_init() sets the host file data format and acquires the table metadata.
     159             :  *      It is called before the other bulk copy functions. 
     160             :  *
     161             :  *      When writing to a table, bcp can use as its data source a data file (\a hfile), 
     162             :  *      or program data in an application's variables.  In the latter case, call bcp_bind() 
     163             :  *      to associate your data with the appropriate table column.  
     164             :  * \return SUCCEED or FAIL.
     165             :  * \sa  BCP_SETL(), bcp_bind(), bcp_done(), bcp_exec()
     166             :  */
     167             : RETCODE
     168         366 : bcp_init(DBPROCESS * dbproc, const char *tblname, const char *hfile, const char *errfile, int direction)
     169             : {
     170         366 :         tdsdump_log(TDS_DBG_FUNC, "bcp_init(%p, %s, %s, %s, %d)\n", 
     171             :                         dbproc, tblname? tblname:"NULL", hfile? hfile:"NULL", errfile? errfile:"NULL", direction);
     172         366 :         CHECK_CONN(FAIL);
     173             : 
     174             :         /* 
     175             :          * Validate other parameters 
     176             :          */
     177         366 :         if (dbproc->tds_socket->conn->tds_version < 0x500) {
     178           0 :                 dbperror(dbproc, SYBETDSVER, 0);
     179           0 :                 return FAIL;
     180             :         }
     181             : 
     182         366 :         if (tblname == NULL) {
     183           0 :                 dbperror(dbproc, SYBEBCITBNM, 0);
     184           0 :                 return FAIL;
     185             :         }
     186             : 
     187         422 :         if (direction != DB_QUERYOUT && !IS_TDS7_PLUS(dbproc->tds_socket->conn) &&
     188          56 :             strlen(tblname) > 92) {  /* 30.30.30 for Sybase */
     189           0 :                 dbperror(dbproc, SYBEBCITBLEN, 0);
     190           0 :                 return FAIL;
     191             :         }
     192             : 
     193         366 :         if (direction != DB_IN && direction != DB_OUT && direction != DB_QUERYOUT) {
     194           0 :                 dbperror(dbproc, SYBEBDIO, 0);
     195           0 :                 return FAIL;
     196             :         }
     197             : 
     198             :         /* Free previously allocated storage in dbproc & initialise flags, etc. */
     199         366 :         _bcp_free_storage(dbproc);
     200             : 
     201             :         /* Allocate storage */
     202         366 :         dbproc->bcpinfo = tds_alloc_bcpinfo();
     203         366 :         if (dbproc->bcpinfo == NULL)
     204             :                 goto memory_error;
     205             : 
     206         366 :         if (!tds_dstr_copy(&dbproc->bcpinfo->tablename, tblname))
     207             :                 goto memory_error;
     208             : 
     209         366 :         dbproc->bcpinfo->direction = direction;
     210             : 
     211         366 :         dbproc->bcpinfo->xfer_init = false;
     212         366 :         dbproc->bcpinfo->bind_count = 0;
     213             : 
     214         366 :         if (TDS_FAILED(tds_bcp_init(dbproc->tds_socket, dbproc->bcpinfo))) {
     215             :                 /* TODO return proper error */
     216             :                 /* Attempt to use Bulk Copy with a non-existent Server table (might be why ...) */
     217           0 :                 dbperror(dbproc, SYBEBCNT, 0);
     218           0 :                 return FAIL;
     219             :         }
     220             : 
     221             :         /* Prepare default hostfile columns */
     222             :         
     223         366 :         if (hfile == NULL) {
     224          38 :                 dbproc->hostfileinfo = NULL;
     225          38 :                 return SUCCEED;
     226             :         }
     227             : 
     228         328 :         dbproc->hostfileinfo = tds_new0(BCP_HOSTFILEINFO, 1);
     229             : 
     230         328 :         if (dbproc->hostfileinfo == NULL)
     231             :                 goto memory_error;
     232         328 :         dbproc->hostfileinfo->maxerrs = 10;
     233         328 :         dbproc->hostfileinfo->firstrow = 1;
     234         328 :         if ((dbproc->hostfileinfo->hostfile = strdup(hfile)) == NULL)
     235             :                 goto memory_error;
     236             : 
     237         328 :         if (errfile != NULL)
     238         328 :                 if ((dbproc->hostfileinfo->errorfile = strdup(errfile)) == NULL)
     239             :                         goto memory_error;
     240             : 
     241         328 :         init_hostfile_columns(dbproc);
     242             : 
     243         328 :         return SUCCEED;
     244             : 
     245           0 : memory_error:
     246           0 :         _bcp_free_storage(dbproc);
     247           0 :         dbperror(dbproc, SYBEMEM, ENOMEM);
     248           0 :         return FAIL;
     249             : }
     250             : 
     251             : 
     252             : /** 
     253             :  * \ingroup dblib_bcp
     254             :  * \brief Set the length of a host variable to be written to a table.
     255             :  * 
     256             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
     257             :  * \param varlen size of the variable, in bytes, or
     258             :  *      - \b 0 indicating NULL
     259             :  *      - \b -1 indicating size is determined by the prefix or terminator.  
     260             :  *              (If both a prefix and a terminator are present, bcp is supposed to use the smaller of the 
     261             :  *               two.  This feature might or might not actually work.)
     262             :  * \param table_column the number of the column in the table (starting with 1, not zero).
     263             :  * 
     264             :  * \return SUCCEED or FAIL.
     265             :  * \sa  bcp_bind(), bcp_colptr(), bcp_sendrow() 
     266             :  */
     267             : RETCODE
     268         130 : bcp_collen(DBPROCESS * dbproc, DBINT varlen, int table_column)
     269             : {
     270             :         TDSCOLUMN *bcpcol;
     271             : 
     272         130 :         tdsdump_log(TDS_DBG_FUNC, "bcp_collen(%p, %d, %d)\n", dbproc, varlen, table_column);
     273             :         
     274         130 :         CHECK_CONN(FAIL);
     275         130 :         CHECK_PARAMETER(dbproc->bcpinfo, SYBEBCPI, FAIL);            /* not initialized */
     276         130 :         DBPERROR_RETURN(dbproc->bcpinfo->direction != DB_IN, SYBEBCPN)    /* not inbound */
     277         130 :         DBPERROR_RETURN(dbproc->hostfileinfo != NULL, SYBEBCPI)              /* cannot have data file */
     278         130 :         CHECK_PARAMETER(0 < table_column && 
     279             :                             table_column <= dbproc->bcpinfo->bindinfo->num_cols, SYBECNOR, FAIL);
     280             :         
     281         130 :         bcpcol = dbproc->bcpinfo->bindinfo->columns[table_column - 1];
     282             : 
     283             :         /* Sybase library does not check for NULL here, only sending, so don't
     284             :          * check and send SYBEBCNN */
     285         130 :         bcpcol->column_bindlen = varlen;
     286             : 
     287         130 :         return SUCCEED;
     288             : }
     289             : 
     290             : /** 
     291             :  * \ingroup dblib_bcp
     292             :  * \brief Indicate how many columns are to be found in the datafile.
     293             :  * 
     294             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
     295             :  * \param host_colcount count of columns in the datafile, irrespective of how many you intend to use. 
     296             :  * \remarks This function describes the file as it is, not how it will be used.  
     297             :  * 
     298             :  * \return SUCCEED or FAIL.  It's hard to see how it could fail.  
     299             :  * \sa  bcp_colfmt()    
     300             :  */
     301             : RETCODE
     302         656 : bcp_columns(DBPROCESS * dbproc, int host_colcount)
     303             : {
     304             :         int i;
     305             : 
     306         656 :         tdsdump_log(TDS_DBG_FUNC, "bcp_columns(%p, %d)\n", dbproc, host_colcount);
     307         656 :         CHECK_CONN(FAIL);
     308         656 :         CHECK_PARAMETER(dbproc->bcpinfo, SYBEBCPI, FAIL);
     309         656 :         CHECK_PARAMETER(dbproc->hostfileinfo, SYBEBIVI, FAIL);
     310             : 
     311         656 :         if (host_colcount < 1) {
     312           0 :                 dbperror(dbproc, SYBEBCFO, 0);
     313           0 :                 return FAIL;
     314             :         }
     315             : 
     316         656 :         _bcp_free_columns(dbproc);
     317             : 
     318         656 :         dbproc->hostfileinfo->host_columns = tds_new0(BCP_HOSTCOLINFO *, host_colcount);
     319         656 :         if (dbproc->hostfileinfo->host_columns == NULL) {
     320           0 :                 dbperror(dbproc, SYBEMEM, ENOMEM);
     321           0 :                 return FAIL;
     322             :         }
     323             : 
     324         656 :         dbproc->hostfileinfo->host_colcount = host_colcount;
     325             : 
     326        4032 :         for (i = 0; i < host_colcount; i++) {
     327        3376 :                 dbproc->hostfileinfo->host_columns[i] = tds_new0(BCP_HOSTCOLINFO, 1);
     328        3376 :                 if (dbproc->hostfileinfo->host_columns[i] == NULL) {
     329           0 :                         dbproc->hostfileinfo->host_colcount = i;
     330           0 :                         _bcp_free_columns(dbproc);
     331           0 :                         dbperror(dbproc, SYBEMEM, ENOMEM);
     332           0 :                         return FAIL;
     333             :                 }
     334             :         }
     335             : 
     336             :         return SUCCEED;
     337             : }
     338             : 
     339             : /** 
     340             :  * \ingroup dblib_bcp
     341             :  * \brief Specify the format of a datafile prior to writing to a table.
     342             :  * 
     343             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
     344             :  * \param host_colnum datafile column number (starting with 1, not zero).
     345             :  * \param host_type dataype token describing the data type in \a host_colnum.  E.g. SYBCHAR for character data.
     346             :  * \param host_prefixlen size of the prefix in the datafile column, if any.  For delimited files: zero.  
     347             :  *                      May be 0, 1, 2, or 4 bytes.  The prefix will be read as an integer (not a character string) from the 
     348             :  *                      data file, and will be interpreted the data size of that column, in bytes.  
     349             :  * \param host_collen maximum size of datafile column, exclusive of any prefix/terminator.  Just the data, ma'am.  
     350             :  *              Special values:
     351             :  *                      - \b 0 indicates NULL.  
     352             :  *                      - \b -1 for fixed-length non-null datatypes
     353             :  *                      - \b -1 for variable-length datatypes (e.g. SYBCHAR) where the length is established 
     354             :  *                              by a prefix/terminator.  
     355             :  * \param host_term the sequence of characters that will serve as a column terminator (delimiter) in the datafile.  
     356             :  *                      Often a tab character, but can be any string of any length.  Zero indicates no terminator.  
     357             :  *                      Special characters:
     358             :  *                              - \b '\\0' terminator is an ASCII NUL.
     359             :  *                              - \b '\\t' terminator is an ASCII TAB.
     360             :  *                              - \b '\\n' terminator is an ASCII NL.
     361             :  * \param host_termlen the length of \a host_term, in bytes. 
     362             :  * \param table_colnum Nth column, starting at 1, in the table that maps to \a host_colnum.  
     363             :  *      If there is a column in the datafile that does not map to a table column, set \a table_colnum to zero.  
     364             :  *
     365             :  *\remarks  bcp_colfmt() is called once for each column in the datafile, as specified with bcp_columns().  
     366             :  * In so doing, you describe to FreeTDS how to parse each line of your datafile, and where to send each field.  
     367             :  *
     368             :  * When a prefix or terminator is used with variable-length data, \a host_collen may have one of three values:
     369             :  *              - \b positive indicating the maximum number of bytes to be used
     370             :  *              - \b 0 indicating NULL
     371             :  *              - \b -1 indicating no maximum; all data, as described by the prefix/terminator will be used.  
     372             :  *              .
     373             :  * \return SUCCEED or FAIL.
     374             :  * \sa  bcp_batch(), bcp_bind(), bcp_colfmt_ps(), bcp_collen(), bcp_colptr(), bcp_columns(),
     375             :  *      bcp_control(), bcp_done(), bcp_exec(), bcp_init(), bcp_sendrow
     376             :  */
     377             : RETCODE
     378        3376 : bcp_colfmt(DBPROCESS * dbproc, int host_colnum, int host_type, int host_prefixlen, DBINT host_collen, const BYTE * host_term,
     379             :            int host_termlen, int table_colnum)
     380             : {
     381             :         BCP_HOSTCOLINFO *hostcol;
     382        3376 :         BYTE *terminator = NULL;
     383             : 
     384        3376 :         tdsdump_log(TDS_DBG_FUNC, "bcp_colfmt(%p, %d, %d, %d, %d, %p, %d, %d)\n", 
     385             :                     dbproc, host_colnum, host_type, host_prefixlen, (int) host_collen, host_term, host_termlen, table_colnum);
     386        3376 :         CHECK_CONN(FAIL);
     387        3376 :         CHECK_PARAMETER(dbproc->bcpinfo, SYBEBCPI, FAIL);
     388        3376 :         CHECK_PARAMETER(dbproc->hostfileinfo, SYBEBIVI, FAIL);
     389             : 
     390             :         /* Microsoft specifies a "file_termlen" of zero if there's no terminator */
     391        3376 :         if (dbproc->msdblib && host_termlen == 0)
     392             :                 host_termlen = -1;
     393             : 
     394        3376 :         if (host_termlen < 0)
     395           0 :                 host_termlen = -1;
     396             : 
     397        3376 :         if (dbproc->hostfileinfo->host_colcount == 0) {
     398           0 :                 dbperror(dbproc, SYBEBCBC, 0);
     399           0 :                 return FAIL;
     400             :         }
     401             : 
     402        3376 :         if (host_colnum < 1) {
     403           0 :                 dbperror(dbproc, SYBEBCFO, 0);
     404           0 :                 return FAIL;
     405             :         }
     406             : 
     407        3376 :         if (host_colnum > dbproc->hostfileinfo->host_colcount) {
     408           0 :                 dbperror(dbproc, SYBECNOR, 0);
     409           0 :                 return FAIL;
     410             :         }
     411             : 
     412        3376 :         if (host_prefixlen != 0 && host_prefixlen != 1 && host_prefixlen != 2 && host_prefixlen != 4 && host_prefixlen != -1) {
     413           0 :                 dbperror(dbproc, SYBEBCPREF, 0);
     414           0 :                 return FAIL;
     415             :         }
     416             : 
     417             :         /* if column is not copied you cannot specify destination type */
     418        3376 :         if (table_colnum <= 0 && host_type == 0) {
     419           0 :                 dbperror(dbproc, SYBEBCPCTYP, 0);
     420           0 :                 return FAIL;
     421             :         }
     422             : 
     423        6752 :         if (table_colnum > 0 && !is_tds_type_valid(host_type)) {
     424           0 :                 dbperror(dbproc, SYBEUDTY, 0);
     425           0 :                 return FAIL;
     426             :         }
     427             : 
     428        3376 :         if (host_type && host_prefixlen == 0 && host_collen == -1 && host_termlen == -1 && !is_fixed_type(host_type)) {
     429           0 :                 dbperror(dbproc, SYBEVDPT, 0);
     430           0 :                 return FAIL;
     431             :         }
     432             : 
     433        3376 :         if (host_collen < -1) {
     434           0 :                 dbperror(dbproc, SYBEBCHLEN, 0);
     435           0 :                 return FAIL;
     436             :         }
     437             : 
     438             :         /* No official error message.  Fix and warn. */
     439        3376 :         if (is_fixed_type(host_type) && (host_collen != -1 && host_collen != 0)) {
     440         696 :                 tdsdump_log(TDS_DBG_FUNC,
     441             :                             "bcp_colfmt: changing host_collen to -1 from %d for fixed type %d.\n", 
     442             :                             host_collen, host_type);
     443             :                 host_collen = -1;
     444             :         }
     445             : 
     446             :         /* 
     447             :          * If there's a positive terminator length, we need a valid terminator pointer.
     448             :          * If the terminator length is 0 or -1, then there's no terminator.
     449             :          */
     450        3376 :         if (host_term == NULL && host_termlen > 0) {
     451           0 :                 dbperror(dbproc, SYBEVDPT, 0);  /* "all variable-length data must have either a length-prefix ..." */
     452           0 :                 return FAIL;
     453             :         }
     454             : 
     455        3376 :         hostcol = dbproc->hostfileinfo->host_columns[host_colnum - 1];
     456             : 
     457             :         /* TODO add precision scale and join with bcp_colfmt_ps */
     458        3376 :         if (host_term && host_termlen > 0) {
     459        1648 :                 if ((terminator = tds_new(BYTE, host_termlen)) == NULL) {
     460           0 :                         dbperror(dbproc, SYBEMEM, errno);
     461           0 :                         return FAIL;
     462             :                 }
     463        1648 :                 memcpy(terminator, host_term, host_termlen);
     464             :         }
     465        3376 :         hostcol->host_column = host_colnum;
     466        3376 :         hostcol->datatype = host_type ? (TDS_SERVER_TYPE) host_type : TDS_INVALID_TYPE;
     467        3376 :         hostcol->prefix_len = host_prefixlen;
     468        3376 :         hostcol->column_len = host_collen;
     469        3376 :         free(hostcol->terminator);
     470        3376 :         hostcol->terminator = terminator;
     471        3376 :         hostcol->term_len = host_termlen;
     472        3376 :         hostcol->tab_colnum = table_colnum;
     473             : 
     474        3376 :         return SUCCEED;
     475             : }
     476             : 
     477             : /** 
     478             :  * \ingroup dblib_bcp
     479             :  * \brief Specify the format of a host file for bulk copy purposes, 
     480             :  *      with precision and scale support for numeric and decimal columns.
     481             :  * 
     482             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
     483             :  * \param host_colnum datafile column number (starting with 1, not zero).
     484             :  * \param host_type dataype token describing the data type in \a host_colnum.  E.g. SYBCHAR for character data.
     485             :  * \param host_prefixlen size of the prefix in the datafile column, if any.  For delimited files: zero.  
     486             :  *                      May be 0, 1, 2, or 4 bytes.  The prefix will be read as an integer (not a character string) from the 
     487             :  *                      data file, and will be interpreted the data size of that column, in bytes.  
     488             :  * \param host_collen maximum size of datafile column, exclusive of any prefix/terminator.  Just the data, ma'am.  
     489             :  *              Special values:
     490             :  *                      - \b 0 indicates NULL.  
     491             :  *                      - \b -1 for fixed-length non-null datatypes
     492             :  *                      - \b -1 for variable-length datatypes (e.g. SYBCHAR) where the length is established 
     493             :  *                              by a prefix/terminator.  
     494             :  * \param host_term the sequence of characters that will serve as a column terminator (delimiter) in the datafile.  
     495             :  *                      Often a tab character, but can be any string of any length.  Zero indicates no terminator.  
     496             :  *                      Special characters:
     497             :  *                              - \b '\\0' terminator is an ASCII NUL.
     498             :  *                              - \b '\\t' terminator is an ASCII TAB.
     499             :  *                              - \b '\\n' terminator is an ASCII NL.
     500             :  * \param host_termlen the length of \a host_term, in bytes. 
     501             :  * \param table_colnum Nth column, starting at 1, in the table that maps to \a host_colnum.  
     502             :  *      If there is a column in the datafile that does not map to a table column, set \a table_colnum to zero.  
     503             :  * \param typeinfo something
     504             :  * \todo Not implemented.
     505             :  * \return SUCCEED or FAIL.
     506             :  * \sa  bcp_batch(), bcp_bind(), bcp_colfmt(), bcp_collen(), bcp_colptr(), bcp_columns(),
     507             :  *      bcp_control(), bcp_done(), bcp_exec(), bcp_init(), bcp_sendrow
     508             :  */
     509             : RETCODE
     510           0 : bcp_colfmt_ps(DBPROCESS * dbproc, int host_colnum, int host_type,
     511             :               int host_prefixlen, DBINT host_collen, BYTE * host_term, int host_termlen, int table_colnum, DBTYPEINFO * typeinfo)
     512             : {
     513           0 :         tdsdump_log(TDS_DBG_FUNC, "UNIMPLEMENTED: bcp_colfmt_ps(%p, %d, %d, %d, %d, %p, %d, %d, %p)\n",
     514             :                     dbproc, host_colnum, host_type, host_prefixlen, (int) host_collen,
     515             :                     host_term, host_termlen, table_colnum, typeinfo);
     516           0 :         CHECK_CONN(FAIL);
     517           0 :         CHECK_PARAMETER(dbproc->bcpinfo, SYBEBCPI, FAIL);
     518             :         
     519             :         /* dbperror(dbproc, , 0);        Illegal precision specified */
     520             : 
     521             :         /* TODO see bcp_colfmt */
     522             :         return FAIL;
     523             : }
     524             : 
     525             : 
     526             : /** 
     527             :  * \ingroup dblib_bcp
     528             :  * \brief Set BCP options for uploading a datafile
     529             :  * 
     530             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
     531             :  * \param field symbolic constant indicating the option to be set, one of:
     532             :  *              - \b BCPMAXERRS Maximum errors tolerated before quitting. The default is 10.
     533             :  *              - \b BCPFIRST The first row to read in the datafile. The default is 1. 
     534             :  *              - \b BCPLAST The last row to read in the datafile. The default is to copy all rows. A value of
     535             :  *                      -1 resets this field to its default?
     536             :  *              - \b BCPBATCH The number of rows per batch.  Default is 0, meaning a single batch. 
     537             :  * \param value The value for \a field.
     538             :  *
     539             :  * \remarks These options control the behavior of bcp_exec().  
     540             :  * When writing to a table from application host memory variables, 
     541             :  * program logic controls error tolerance and batch size. 
     542             :  * 
     543             :  * \return SUCCEED or FAIL.
     544             :  * \sa  bcp_batch(), bcp_bind(), bcp_colfmt(), bcp_collen(), bcp_colptr(), bcp_columns(), bcp_done(), bcp_exec(), bcp_options()
     545             :  */
     546             : RETCODE
     547           0 : bcp_control(DBPROCESS * dbproc, int field, DBINT value)
     548             : {
     549           0 :         tdsdump_log(TDS_DBG_FUNC, "bcp_control(%p, %d, %d)\n", dbproc, field, value);
     550           0 :         CHECK_CONN(FAIL);
     551           0 :         CHECK_PARAMETER(dbproc->bcpinfo, SYBEBCPI, FAIL);
     552             : 
     553           0 :         if (field == BCPKEEPIDENTITY) {
     554           0 :                 dbproc->bcpinfo->identity_insert_on = (value != 0);
     555           0 :                 return SUCCEED;
     556             :         }
     557             : 
     558           0 :         CHECK_PARAMETER(dbproc->hostfileinfo, SYBEBIVI, FAIL);
     559             : 
     560           0 :         switch (field) {
     561             : 
     562           0 :         case BCPMAXERRS:
     563           0 :                 if (value < 1)
     564           0 :                         value = 10;
     565           0 :                 dbproc->hostfileinfo->maxerrs = value;
     566           0 :                 break;
     567           0 :         case BCPFIRST:
     568           0 :                 if (value < 1)
     569           0 :                         value = 1;
     570           0 :                 dbproc->hostfileinfo->firstrow = value;
     571           0 :                 break;
     572           0 :         case BCPLAST:
     573           0 :                 dbproc->hostfileinfo->lastrow = value;
     574           0 :                 break;
     575           0 :         case BCPBATCH:
     576           0 :                 dbproc->hostfileinfo->batch = value;
     577           0 :                 break;
     578             : 
     579           0 :         default:
     580           0 :                 dbperror(dbproc, SYBEIFNB, 0);
     581           0 :                 return FAIL;
     582             :         }
     583             :         return SUCCEED;
     584             : }
     585             : 
     586             : /*
     587             :  * \ingroup dblib_bcp
     588             :  * \brief Get BCP batch option
     589             :  *
     590             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
     591             :  * \remarks This function is specific to FreeTDS.
     592             :  *
     593             :  * \return the value that was set by bcp_control.
     594             :  * \sa  bcp_batch(), bcp_control()
     595             :  */
     596             : int
     597           0 : bcp_getbatchsize(DBPROCESS * dbproc)
     598             : {
     599           0 :         CHECK_CONN(-1);
     600           0 :         CHECK_PARAMETER(dbproc->hostfileinfo, SYBEBCPI, -1);
     601           0 :         return dbproc->hostfileinfo->batch;
     602             : }
     603             : 
     604             : /*
     605             :  * \ingroup dblib_bcp
     606             :  * \brief Get number of columns in the BCP data file
     607             :  *
     608             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
     609             :  * \remarks This function is specific to FreeTDS.
     610             :  *
     611             :  * \return the value that was set by bcp_columns(). This represents the number
     612             :  *     of columns of data in the saved data file. Usually the same as the number
     613             :  *     of columns in the database table, but could be fewer if a Format File is
     614             :  *     in use which only selects a subset of the table columns.
     615             :  *
     616             :  * \sa bcp_columns()
     617             :  */
     618             : int
     619           0 : bcp_gethostcolcount(DBPROCESS *dbproc)
     620             : {
     621           0 :         CHECK_CONN(-1);
     622           0 :         CHECK_PARAMETER(dbproc->hostfileinfo, SYBEBCPI, -1);
     623           0 :         return dbproc->hostfileinfo->host_colcount;
     624             : }
     625             : 
     626             : /** 
     627             :  * \ingroup dblib_bcp
     628             :  * \brief Set bulk copy options.
     629             :  * 
     630             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
     631             :  * \param option symbolic constant indicating the option to be set, one of:
     632             :  *              - \b BCPLABELED Not implemented.
     633             :  *              - \b BCPHINTS The hint to be passed when the bulk-copy begins.  
     634             :  * \param value The string constant for \a option a/k/a the hint.  One of:
     635             :  *              - \b ORDER The data are ordered in accordance with the table's clustered index.
     636             :  *              - \b ROWS_PER_BATCH The batch size
     637             :  *              - \b KILOBYTES_PER_BATCH The approximate number of kilobytes to use for a batch size
     638             :  *              - \b TABLOCK Lock the table
     639             :  *              - \b CHECK_CONSTRAINTS Apply constraints
     640             :  *              - \b FIRE_TRIGGERS Fire any INSERT triggers on the target table
     641             :  * \param valuelen The length of \a value. Ignored for fixed-length option types.
     642             :  * 
     643             :  * \return SUCCEED or FAIL.
     644             :  * \sa  bcp_control(), 
     645             :  *      bcp_exec(), 
     646             :  * \todo Simplify.
     647             :  */
     648             : RETCODE
     649           2 : bcp_options(DBPROCESS * dbproc, int option, BYTE * value, int valuelen)
     650             : {
     651             :         int i;
     652             :         static const char *const hints[] = {
     653             :                 "ORDER", "ROWS_PER_BATCH", "KILOBYTES_PER_BATCH", "TABLOCK", "CHECK_CONSTRAINTS",
     654             :                 "FIRE_TRIGGERS", "KEEP_NULLS", NULL
     655             :         };
     656             : 
     657           2 :         tdsdump_log(TDS_DBG_FUNC, "bcp_options(%p, %d, %p, %d)\n", dbproc, option, value, valuelen);
     658           2 :         CHECK_CONN(FAIL);
     659           2 :         CHECK_PARAMETER(dbproc->bcpinfo, SYBEBCPI, FAIL);
     660           2 :         CHECK_NULP(value, "bcp_options", 3, FAIL);
     661             : 
     662           2 :         switch (option) {
     663           0 :         case BCPLABELED:
     664           0 :                 tdsdump_log(TDS_DBG_FUNC, "UNIMPLEMENTED bcp option: BCPLABELED\n");
     665             :                 break;
     666           2 :         case BCPHINTS:
     667           2 :                 if (!value || valuelen <= 0)
     668             :                         break;
     669             : 
     670          10 :                 for (i = 0; hints[i]; i++) {    /* look up hint */
     671          12 :                         if (strncasecmp((char *) value, hints[i], strlen(hints[i])) == 0) {
     672           2 :                                 if (!tds_dstr_copy(&dbproc->bcpinfo->hint, hints[i]))
     673             :                                         return FAIL;
     674           2 :                                 return SUCCEED;
     675             :                         }
     676             :                 }
     677           0 :                 tdsdump_log(TDS_DBG_FUNC, "failed, no such hint\n");
     678             :                 break;
     679           0 :         default:
     680           0 :                 tdsdump_log(TDS_DBG_FUNC, "UNIMPLEMENTED bcp option: %u\n", option);
     681             :                 break;
     682             :         }
     683           0 :         return FAIL;
     684             : }
     685             : 
     686             : /** 
     687             :  * \ingroup dblib_bcp
     688             :  * \brief Override bcp_bind() by pointing to a different host variable.
     689             :  * 
     690             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
     691             :  * \param colptr The pointer, the address of your variable. 
     692             :  * \param table_column The 1-based column ordinal in the table.  
     693             :  * \remarks Use between calls to bcp_sendrow().  After calling bcp_colptr(), 
     694             :  *              subsequent calls to bcp_sendrow() will bind to the new address.  
     695             :  * \return SUCCEED or FAIL.
     696             :  * \sa  bcp_bind(), bcp_collen(), bcp_sendrow() 
     697             :  */
     698             : RETCODE
     699           0 : bcp_colptr(DBPROCESS * dbproc, BYTE * colptr, int table_column)
     700             : {
     701             :         TDSCOLUMN *curcol;
     702             : 
     703           0 :         tdsdump_log(TDS_DBG_FUNC, "bcp_colptr(%p, %p, %d)\n", dbproc, colptr, table_column);
     704           0 :         CHECK_CONN(FAIL);
     705           0 :         CHECK_PARAMETER(dbproc->bcpinfo, SYBEBCPI, FAIL);
     706           0 :         CHECK_PARAMETER(dbproc->bcpinfo->bindinfo, SYBEBCPI, FAIL);
     707             :         /* colptr can be NULL */
     708             : 
     709           0 :         if (dbproc->bcpinfo->direction != DB_IN) {
     710           0 :                 dbperror(dbproc, SYBEBCPN, 0);
     711           0 :                 return FAIL;
     712             :         }
     713           0 :         if (table_column <= 0 || table_column > dbproc->bcpinfo->bindinfo->num_cols) {
     714           0 :                 dbperror(dbproc, SYBEBCPN, 0);
     715           0 :                 return FAIL;
     716             :         }
     717             :         
     718           0 :         curcol = dbproc->bcpinfo->bindinfo->columns[table_column - 1];
     719           0 :         curcol->column_varaddr = (TDS_CHAR *)colptr;
     720             : 
     721           0 :         return SUCCEED;
     722             : }
     723             : 
     724             : 
     725             : /** 
     726             :  * \ingroup dblib_bcp
     727             :  * \brief See if BCP_SETL() was used to set the LOGINREC for BCP work.  
     728             :  * 
     729             :  * \param login Address of the LOGINREC variable to be passed to dbopen(). 
     730             :  * 
     731             :  * \return TRUE or FALSE.
     732             :  * \sa  BCP_SETL(), bcp_init(), dblogin(), dbopen()
     733             :  */
     734             : DBBOOL
     735          10 : bcp_getl(LOGINREC * login)
     736             : {
     737          10 :         TDSLOGIN *tdsl = login->tds_login;
     738             : 
     739          10 :         tdsdump_log(TDS_DBG_FUNC, "bcp_getl(%p)\n", login);
     740             : 
     741          10 :         return (tdsl->bulk_copy);
     742             : }
     743             : 
     744             : /**
     745             :  * Convert column for output (usually to a file)
     746             :  * Conversion is slightly different from input as:
     747             :  * - date is formatted differently;
     748             :  * - you have to set properly numeric while on input the column metadata are
     749             :  *   used;
     750             :  * - we need to make sure buffer is always at least a minimum bytes.
     751             :  */
     752             : static int
     753        1648 : _bcp_convert_out(DBPROCESS * dbproc, TDSCOLUMN *curcol, BCP_HOSTCOLINFO *hostcol, TDS_UCHAR **p_data, const char *bcpdatefmt)
     754             : {
     755             :         BYTE *src;
     756             :         int srclen;
     757             :         int buflen;
     758        1648 :         int srctype = tds_get_conversion_type(curcol->column_type, curcol->column_size);
     759             : 
     760        1648 :         src = curcol->column_data;
     761        1648 :         if (is_blob_col(curcol))
     762         122 :                 src = (BYTE *) ((TDSBLOB *) src)->textvalue;
     763             : 
     764        1648 :         srclen = curcol->column_cur_size;
     765             : 
     766             :         /*
     767             :          * if we are converting datetime to string, need to override any
     768             :          * date time formats already established
     769             :          */
     770        1648 :         if (is_datetime_type(srctype) && is_ascii_type(hostcol->datatype)) {
     771             :                 TDSDATEREC when;
     772             : 
     773             :                 /* Some date/time types have variable precision (e.g. MS datetime2),
     774             :                  * and set curcol->column_prec with that precision.
     775             :                  * DATETIME is printed with precision 3 by MS BCP & ASE BCP however
     776             :                  * curcol->column_prec is not set for that type.
     777             :                  * (It might be better to set column_prec at load time for all
     778             :                  * of the datetime types...)
     779             :                  */
     780          36 :                 int prec = curcol->column_prec ? curcol->column_prec : 3;
     781             : 
     782          36 :                 tds_datecrack(srctype, src, &when);
     783          36 :                 buflen = _bcp_strftime((char *) *p_data, 256, bcpdatefmt, &when, prec);
     784        1612 :         } else if (srclen == 0 && is_variable_type(curcol->column_type)
     785          46 :                    && is_ascii_type(hostcol->datatype)) {
     786             :                 /*
     787             :                  * An empty string is denoted in the output file by a single ASCII NUL
     788             :                  * byte that we request by specifying a destination length of -1.  (Not
     789             :                  * to be confused with a database NULL, which is denoted in the output
     790             :                  * file with an empty string!)
     791             :                  */
     792          46 :                 (*p_data)[0] = 0;
     793          46 :                 buflen = 1;
     794        1566 :         } else if (is_numeric_type(hostcol->datatype)) {
     795           0 :                 TDS_NUMERIC *num = (TDS_NUMERIC *) (*p_data);
     796           0 :                 if (is_numeric_type(srctype)) {
     797           0 :                         TDS_NUMERIC *nsrc = (TDS_NUMERIC *) src;
     798           0 :                         num->precision = nsrc->precision;
     799           0 :                         num->scale = nsrc->scale;
     800             :                 } else {
     801           0 :                         num->precision = 18;
     802           0 :                         num->scale = 0;
     803             :                 }
     804           0 :                 buflen = tds_convert(dbproc->tds_socket->conn->tds_ctx, srctype, src, srclen, hostcol->datatype, (CONV_RESULT *) num);
     805           0 :                 if (buflen > 0)
     806           0 :                         buflen = tds_numeric_bytes_per_prec[num->precision] + 2;
     807        1566 :         } else if (!is_variable_type(hostcol->datatype)) {
     808          10 :                 buflen = tds_convert(dbproc->tds_socket->conn->tds_ctx, srctype, src, srclen, hostcol->datatype, (CONV_RESULT *) (*p_data));
     809             :         } else {
     810             :                 CONV_RESULT cr;
     811             : 
     812             :                 /*
     813             :                  * For null columns, the above work to determine the output buffer size is moot,
     814             :                  * because bcpcol->data_size is zero, so dbconvert() won't write anything,
     815             :                  * and returns zero.
     816             :                  */
     817        1556 :                 buflen = tds_convert(dbproc->tds_socket->conn->tds_ctx, srctype, src, srclen, hostcol->datatype, (CONV_RESULT *) &cr);
     818        1556 :                 if (buflen < 0)
     819           0 :                         return buflen;
     820             : 
     821        1556 :                 if (buflen >= 256) {
     822          30 :                         free(*p_data);
     823          30 :                         *p_data = (TDS_UCHAR *) cr.c;
     824             :                 } else {
     825        1526 :                         memcpy(*p_data, cr.c, buflen);
     826        1526 :                         free(cr.c);
     827             :                 }
     828             : 
     829             :                 /*
     830             :                  * Special case:  When outputting database varchar data
     831             :                  * (either varchar or nullable char) conversion may have
     832             :                  * trimmed trailing blanks such that nothing is left.
     833             :                  * In this case we need to put a single blank to the output file.
     834             :                  */
     835        1556 :                 if (is_char_type(curcol->column_type) && srclen > 0 && buflen == 0) {
     836           0 :                         strcpy ((char *) (*p_data), " ");
     837           0 :                         buflen = 1;
     838             :                 }
     839             :         }
     840             :         return buflen;
     841             : }
     842             : 
     843             : static int
     844             : bcp_cache_prefix_len(BCP_HOSTCOLINFO *hostcol, const TDSCOLUMN *curcol)
     845             : {
     846             :         int plen;
     847             : 
     848           0 :         if (type_has_textptr(hostcol->datatype))
     849             :                 plen = 4;
     850           0 :         else if (is_numeric_type(hostcol->datatype))
     851             :                 plen = 1;
     852           0 :         else if (!is_fixed_type(hostcol->datatype))
     853             :                 plen = 2;
     854           0 :         else if (curcol->column_nullable)
     855             :                 plen = 1;
     856             :         else
     857           0 :                 plen = 0;
     858             :         /* cache */
     859           0 :         return hostcol->prefix_len = plen;
     860             : }
     861             : 
     862             : static RETCODE
     863        1862 : bcp_write_prefix(TDSFILEOUTSTREAM *hoststream, BCP_HOSTCOLINFO *hostcol, TDSCOLUMN *curcol, int buflen)
     864             : {
     865             :         union {
     866             :                 TDS_TINYINT ti;
     867             :                 TDS_SMALLINT si;
     868             :                 TDS_INT li;
     869             :         } u;
     870             :         int plen;
     871             : 
     872             :         /* compute prefix len if needed */
     873        1862 :         if ((plen = hostcol->prefix_len) == -1)
     874           0 :                 plen = bcp_cache_prefix_len(hostcol, curcol);
     875             : 
     876             :         /* output prefix to file */
     877        1862 :         switch (plen) {
     878             :         default:
     879             :                 return SUCCEED;
     880          10 :         case 1:
     881          10 :                 u.ti = buflen;
     882          10 :                 break;
     883           0 :         case 2:
     884           0 :                 u.si = buflen;
     885           0 :                 break;
     886          10 :         case 4:
     887          10 :                 u.li = buflen;
     888          10 :                 break;
     889             :         }
     890          20 :         return TDS_SUCCEED(tds_fileout_stream_put(hoststream, &u, plen)) ? SUCCEED : FAIL;
     891             : }
     892             : 
     893             : /**
     894             :  * \ingroup dblib_bcp_internal
     895             :  * \brief
     896             :  *
     897             :  *
     898             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
     899             :  * \param rows_copied
     900             :  *
     901             :  * \return SUCCEED or FAIL.
     902             :  * \sa  BCP_SETL(), bcp_batch(), bcp_bind(), bcp_colfmt(), bcp_colfmt_ps(), bcp_collen(), bcp_colptr(), bcp_columns(), bcp_control(), bcp_done(), bcp_exec(), bcp_getl(), bcp_init(), bcp_moretext(), bcp_options(), bcp_readfmt(), bcp_sendrow()
     903             :  */
     904             : static RETCODE
     905         164 : _bcp_exec_out(DBPROCESS * dbproc, DBINT * rows_copied)
     906             : {
     907             :         TDSFILEOUTSTREAM hoststream;
     908         164 :         FILE *hostfp = NULL;
     909         164 :         TDS_UCHAR *data = NULL;
     910             :         int i;
     911             : 
     912             :         TDSSOCKET *tds;
     913             :         TDSRESULTINFO *resinfo;
     914         164 :         TDSCOLUMN *curcol = NULL;
     915             :         BCP_HOSTCOLINFO *hostcol;
     916             :         int buflen;
     917             : 
     918             :         TDS_INT result_type;
     919             : 
     920             :         int row_of_query;
     921             :         int rows_written;
     922             :         const char *bcpdatefmt;
     923             :         TDSRET tdsret;
     924             : 
     925         164 :         tdsdump_log(TDS_DBG_FUNC, "_bcp_exec_out(%p, %p)\n", dbproc, rows_copied);
     926         164 :         assert(dbproc);
     927         164 :         assert(rows_copied);
     928             : 
     929         164 :         tds = dbproc->tds_socket;
     930         164 :         assert(tds);
     931             : 
     932             :         /* See if they want a BCP date format different from the default.
     933             :          * The ASE bcp client does use the locale for bcp out; but all bcp clients
     934             :          * appear to successfully read this format for input, regardless of locale.
     935             :          */
     936         164 :         bcpdatefmt = getenv("FREEBCP_DATEFMT");
     937         164 :         if (bcpdatefmt && !strcmp(bcpdatefmt, BCP_DEFAULT_DATEFMT))
     938           0 :                 bcpdatefmt = NULL;
     939             : 
     940         164 :         if (dbproc->bcpinfo->direction == DB_QUERYOUT ) {
     941           0 :                 if (TDS_FAILED(tds_submit_query(tds, tds_dstr_cstr(&dbproc->bcpinfo->tablename))))
     942             :                         return FAIL;
     943             :         } else {
     944             :                 /* TODO quote if needed */
     945         328 :                 if (TDS_FAILED(tds_submit_queryf(tds, "select * from %s", tds_dstr_cstr(&dbproc->bcpinfo->tablename))))
     946             :                         return FAIL;
     947             :         }
     948             : 
     949         164 :         tdsret = tds_process_tokens(tds, &result_type, NULL, TDS_TOKEN_RESULTS);
     950         164 :         if (TDS_FAILED(tdsret))
     951             :                 return FAIL;
     952             : 
     953         164 :         if (!tds->res_info) {
     954             :                 /* TODO flush/cancel to keep consistent state */
     955             :                 return FAIL;
     956             :         }
     957             : 
     958         164 :         resinfo = tds->res_info;
     959             : 
     960         164 :         row_of_query = 0;
     961         164 :         rows_written = 0;
     962             : 
     963             :         /* allocate at least 256 bytes */
     964             :         /* allocate data for buffer conversion */
     965         164 :         data = tds_new(TDS_UCHAR, 256);
     966         164 :         if (!data) {
     967           0 :                 dbperror(dbproc, SYBEMEM, errno);
     968             :                 goto Cleanup;
     969             :         }
     970             : 
     971             :         /*
     972             :          * TODO above we allocate many buffer just to convert and store
     973             :          * to file.. avoid all that passages...
     974             :          */
     975         164 :         if (!(hostfp = fopen(dbproc->hostfileinfo->hostfile, "w"))) {
     976           0 :                 dbperror(dbproc, SYBEBCUO, errno);
     977           0 :                 goto Cleanup;
     978             :         }
     979         164 :         tds_fileout_stream_init(&hoststream, hostfp, _IOFBF);
     980             : 
     981             :         /* fetch a row of data from the server */
     982             : 
     983         704 :         while (tds_process_tokens(tds, &result_type, NULL, TDS_STOPAT_ROWFMT|TDS_RETURN_DONE|TDS_RETURN_ROW|TDS_RETURN_COMPUTE)
     984             :                 == TDS_SUCCESS) {
     985             : 
     986         540 :                 if (result_type != TDS_ROW_RESULT && result_type != TDS_COMPUTE_RESULT)
     987             :                         break;
     988             : 
     989         376 :                 row_of_query++;
     990             : 
     991             :                 /* skip rows outside of the firstrow/lastrow range, if specified */
     992         376 :                 if (dbproc->hostfileinfo->firstrow > row_of_query ||
     993             :                                                       row_of_query > TDS_MAX(dbproc->hostfileinfo->lastrow, 0x7FFFFFFF))
     994           0 :                         continue;
     995             : 
     996             :                 /* Go through the hostfile columns, finding those that relate to database columns. */
     997        1862 :                 for (i = 0; i < dbproc->hostfileinfo->host_colcount; i++) {
     998        1862 :                         hostcol = dbproc->hostfileinfo->host_columns[i];
     999        1862 :                         if (hostcol->tab_colnum < 1 || hostcol->tab_colnum > resinfo->num_cols)
    1000           0 :                                 continue;
    1001             : 
    1002        1862 :                         curcol = resinfo->columns[hostcol->tab_colnum - 1];
    1003             : 
    1004        1862 :                         if (curcol->column_cur_size < 0) {
    1005             :                                 buflen = 0;
    1006             :                         } else {
    1007        1648 :                                 buflen = _bcp_convert_out(dbproc, curcol, hostcol, &data, bcpdatefmt);
    1008             :                         }
    1009        1648 :                         if (buflen < 0) {
    1010           0 :                                 _dblib_convert_err(dbproc, buflen);
    1011           0 :                                 goto Cleanup;
    1012             :                         }
    1013             : 
    1014             :                         /* The prefix */
    1015        1862 :                         if (bcp_write_prefix(&hoststream, hostcol, curcol, buflen) != SUCCEED)
    1016             :                                 goto write_error;
    1017             : 
    1018             :                         /* The data */
    1019        1862 :                         if (hostcol->column_len != -1) {
    1020           0 :                                 buflen = TDS_MIN(buflen, hostcol->column_len);
    1021             :                         }
    1022             : 
    1023        1862 :                         if (buflen > 0) {
    1024        1648 :                                 if (!TDS_SUCCEED(tds_fileout_stream_put(&hoststream, data, buflen)))
    1025             :                                         goto write_error;
    1026             :                         }
    1027             : 
    1028             :                         /* The terminator */
    1029        1862 :                         if (hostcol->terminator && hostcol->term_len > 0) {
    1030        1842 :                                 if (!TDS_SUCCEED(tds_fileout_stream_put(&hoststream, hostcol->terminator, hostcol->term_len)))
    1031             :                                         goto write_error;
    1032             :                         }
    1033             :                 }
    1034         376 :                 rows_written++;
    1035             :         }
    1036             : 
    1037         164 :         if (TDS_FAILED(tds_fileout_stream_flush(&hoststream)) || fclose(hostfp) != 0) {
    1038           0 :                 dbperror(dbproc, SYBEBCUC, errno);
    1039           0 :                 goto Cleanup;
    1040             :         }
    1041         164 :         hostfp = NULL;
    1042             : 
    1043         164 :         if (row_of_query + 1 < dbproc->hostfileinfo->firstrow) {
    1044             :                 /*
    1045             :                  * The table which bulk-copy is attempting to
    1046             :                  * copy to a host-file is shorter than the
    1047             :                  * number of rows which bulk-copy was instructed to skip.
    1048             :                  */
    1049             :                 /* TODO reset TDSSOCKET state */
    1050           0 :                 dbperror(dbproc, SYBETTS, 0);
    1051             :                 goto Cleanup;
    1052             :         }
    1053             : 
    1054         164 :         *rows_copied = rows_written;
    1055         164 :         free(data);
    1056         164 :         return SUCCEED;
    1057             : 
    1058           0 : write_error:
    1059           0 :         dbperror(dbproc, SYBEBCWE, errno);
    1060             : 
    1061           0 : Cleanup:
    1062           0 :         if (hostfp)
    1063           0 :                 fclose(hostfp);
    1064           0 :         free(data);
    1065           0 :         return FAIL;
    1066             : }
    1067             : 
    1068             : static STATUS
    1069         164 : _bcp_check_eof(DBPROCESS * dbproc, FILE *file, int icol)
    1070             : {
    1071         164 :         int errnum = errno;
    1072             : 
    1073         164 :         tdsdump_log(TDS_DBG_FUNC, "_bcp_check_eof(%p, %p, %d)\n", dbproc, file, icol);
    1074         164 :         assert(dbproc);
    1075         164 :         assert(file);
    1076             : 
    1077         164 :         if (feof(file)) {
    1078         164 :                 if (icol == 0) {
    1079         164 :                         tdsdump_log(TDS_DBG_FUNC, "Normal end-of-file reached while loading bcp data file.\n");
    1080             :                         return NO_MORE_ROWS;
    1081             :                 }
    1082           0 :                 dbperror(dbproc, SYBEBEOF, errnum);
    1083           0 :                 return FAIL;
    1084             :         } 
    1085           0 :         dbperror(dbproc, SYBEBCRE, errnum);
    1086           0 :         return FAIL;
    1087             : }
    1088             : 
    1089             : /**
    1090             :  * Convert column for input to a table
    1091             :  */
    1092             : static TDSRET
    1093        4452 : _bcp_convert_in(DBPROCESS *dbproc, TDS_SERVER_TYPE srctype, const TDS_CHAR *src, TDS_UINT srclen,
    1094             :                 TDS_SERVER_TYPE desttype, TDSCOLUMN *col)
    1095             : {
    1096        4452 :         bool variable = true;
    1097             :         CONV_RESULT cr, *p_cr;
    1098             :         TDS_INT len;
    1099        4452 :         BCPCOLDATA *coldata = col->bcp_column_data;
    1100             : 
    1101        4452 :         coldata->is_null = false;
    1102             : 
    1103        4452 :         if (!is_variable_type(desttype)) {
    1104        3134 :                 variable = false;
    1105        3134 :                 p_cr = (CONV_RESULT *) coldata->data;
    1106             :         } else {
    1107             :                 p_cr = &cr;
    1108             :         }
    1109             : 
    1110        4452 :         len = tds_convert(dbproc->tds_socket->conn->tds_ctx, srctype, src, srclen, desttype, p_cr);
    1111        4452 :         if (len < 0) {
    1112           0 :                 _dblib_convert_err(dbproc, len);
    1113             :                 return TDS_FAIL;
    1114             :         }
    1115             : 
    1116        4452 :         coldata->datalen = len;
    1117        4452 :         if (variable) {
    1118        1318 :                 if (len > col->on_server.column_size) {
    1119           0 :                         dbperror(dbproc, SYBECOFL, 0);
    1120             :                         return TDS_FAIL;
    1121             :                 }
    1122        1318 :                 free(coldata->data);
    1123        1318 :                 coldata->data = (TDS_UCHAR *) cr.c;
    1124             :         }
    1125             :         return TDS_SUCCESS;
    1126             : }
    1127             : 
    1128             : static void
    1129        4452 : rtrim_bcpcol(TDSCOLUMN *bcpcol)
    1130             : {
    1131             :         /* trim trailing blanks from character data */
    1132        4452 :         if (is_ascii_type(bcpcol->on_server.column_type)) {
    1133             :                 /* A single NUL byte indicates an empty string. */
    1134        1166 :                 if (bcpcol->bcp_column_data->datalen == 1
    1135         206 :                     && bcpcol->bcp_column_data->data[0] == '\0') {
    1136           0 :                         bcpcol->bcp_column_data->datalen = 0;
    1137           0 :                         return;
    1138             :                 }
    1139        2332 :                 bcpcol->bcp_column_data->datalen = rtrim((char *) bcpcol->bcp_column_data->data,
    1140             :                                                                   bcpcol->bcp_column_data->datalen);
    1141        1166 :                 return;
    1142             :         }
    1143             : 
    1144             :         /* unicode part */
    1145        3286 :         if (is_unicode_type(bcpcol->on_server.column_type)) {
    1146             :                 uint16_t *data, space;
    1147             : 
    1148          68 :                 if (!bcpcol->char_conv || bcpcol->char_conv->to.charset.min_bytes_per_char != 2)
    1149           6 :                         return;
    1150             : 
    1151          68 :                 data = (uint16_t *) bcpcol->bcp_column_data->data;
    1152             :                 /* A single NUL byte indicates an empty string. */
    1153          68 :                 if (bcpcol->bcp_column_data->datalen == 2 && data[0] == 0) {
    1154           6 :                         bcpcol->bcp_column_data->datalen = 0;
    1155           6 :                         return;
    1156             :                 }
    1157          62 :                 switch (bcpcol->char_conv->to.charset.canonic) {
    1158           0 :                 case TDS_CHARSET_UTF_16BE:
    1159             :                 case TDS_CHARSET_UCS_2BE:
    1160           0 :                         TDS_PUT_A2BE(&space, 0x20);
    1161           0 :                         break;
    1162          62 :                 case TDS_CHARSET_UTF_16LE:
    1163             :                 case TDS_CHARSET_UCS_2LE:
    1164          62 :                         TDS_PUT_A2LE(&space, 0x20);
    1165          62 :                         break;
    1166             :                 default:
    1167             :                         return;
    1168             :                 }
    1169         124 :                 bcpcol->bcp_column_data->datalen = rtrim_u16(data, bcpcol->bcp_column_data->datalen, space);
    1170             :         }
    1171             : }
    1172             : 
    1173             : /** 
    1174             :  * \ingroup dblib_bcp_internal
    1175             :  * \brief 
    1176             :  *
    1177             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    1178             :  * \param hostfile 
    1179             :  * \param row_error set to true if an error is detected in the row
    1180             :  * \param skip skip the row
    1181             :  * 
    1182             :  * \return MORE_ROWS, NO_MORE_ROWS, or FAIL.
    1183             :  * \sa  BCP_SETL(), bcp_batch(), bcp_bind(), bcp_colfmt(), bcp_colfmt_ps(), bcp_collen(), bcp_colptr(), bcp_columns(), bcp_control(), bcp_done(), bcp_exec(), bcp_getl(), bcp_init(), bcp_moretext(), bcp_options(), bcp_readfmt(), bcp_sendrow()
    1184             :  */
    1185             : static STATUS
    1186         540 : _bcp_read_hostfile(DBPROCESS * dbproc, FILE * hostfile, bool *row_error, bool skip)
    1187             : {
    1188             :         int i;
    1189             : 
    1190         540 :         tdsdump_log(TDS_DBG_FUNC, "_bcp_read_hostfile(%p, %p, %p, %d)\n", dbproc, hostfile, row_error, skip);
    1191         540 :         assert(dbproc);
    1192         540 :         assert(hostfile);
    1193         540 :         assert(row_error);
    1194             : 
    1195             :         /* for each host file column defined by calls to bcp_colfmt */
    1196             : 
    1197        4264 :         for (i = 0; i < dbproc->hostfileinfo->host_colcount; i++) {
    1198        2026 :                 TDSCOLUMN *bcpcol = NULL;
    1199             :                 BCP_HOSTCOLINFO *hostcol;
    1200             :                 TDS_CHAR *coldata;
    1201        2026 :                 int collen = 0;
    1202        2026 :                 bool data_is_null = false;
    1203             :                 offset_type col_start;
    1204             : 
    1205        2026 :                 tdsdump_log(TDS_DBG_FUNC, "parsing host column %d\n", i + 1);
    1206        2026 :                 hostcol = dbproc->hostfileinfo->host_columns[i];
    1207             : 
    1208        2026 :                 hostcol->column_error = 0;
    1209             : 
    1210             :                 /* 
    1211             :                  * If this host file column contains table data,
    1212             :                  * find the right element in the table/column list.  
    1213             :                  */
    1214        2026 :                 if (hostcol->tab_colnum > 0) {
    1215        2026 :                         if (hostcol->tab_colnum > dbproc->bcpinfo->bindinfo->num_cols) {
    1216           0 :                                 tdsdump_log(TDS_DBG_FUNC, "error: file wider than table: %d/%d\n", 
    1217             :                                                           i+1, dbproc->bcpinfo->bindinfo->num_cols);
    1218           0 :                                 dbperror(dbproc, SYBEBEOF, 0);
    1219           0 :                                 return FAIL;
    1220             :                         }
    1221        2026 :                         tdsdump_log(TDS_DBG_FUNC, "host column %d uses bcpcol %d (%p)\n", 
    1222             :                                                   i+1, hostcol->tab_colnum, bcpcol);
    1223        2026 :                         bcpcol = dbproc->bcpinfo->bindinfo->columns[hostcol->tab_colnum - 1];
    1224        2026 :                         assert(bcpcol != NULL);
    1225             :                 }
    1226             : 
    1227             :                 /* detect prefix len */
    1228        2026 :                 if (bcpcol && hostcol->prefix_len == -1)
    1229           0 :                         bcp_cache_prefix_len(hostcol, bcpcol);
    1230             : 
    1231             :                 /* a prefix length, if extant, specifies how many bytes to read */
    1232        2026 :                 if (hostcol->prefix_len > 0) {
    1233             :                         union {
    1234             :                                 TDS_TINYINT ti;
    1235             :                                 TDS_SMALLINT si;
    1236             :                                 TDS_INT li;
    1237             :                         } u;
    1238             : 
    1239          30 :                         switch (hostcol->prefix_len) {
    1240          20 :                         case 1:
    1241          20 :                                 if (fread(&u.ti, 1, 1, hostfile) != 1)
    1242          20 :                                         return _bcp_check_eof(dbproc, hostfile, i);
    1243          10 :                                 collen = u.ti ? u.ti : -1;
    1244          10 :                                 break;
    1245           0 :                         case 2:
    1246           0 :                                 if (fread(&u.si, 2, 1, hostfile) != 1)
    1247           0 :                                         return _bcp_check_eof(dbproc, hostfile, i);
    1248           0 :                                 collen = u.si;
    1249           0 :                                 break;
    1250          10 :                         case 4:
    1251          10 :                                 if (fread(&u.li, 4, 1, hostfile) != 1)
    1252           0 :                                         return _bcp_check_eof(dbproc, hostfile, i);
    1253          10 :                                 collen = u.li;
    1254          10 :                                 break;
    1255           0 :                         default:
    1256             :                                 /* FIXME return error, remember that prefix_len can be 3 */
    1257           0 :                                 assert(hostcol->prefix_len <= 4);
    1258             :                                 break;
    1259             :                         }
    1260             : 
    1261             :                         /* TODO test all NULL types */
    1262             :                         /* TODO for < -1 error */
    1263          20 :                         if (collen <= -1) {
    1264             :                                 data_is_null = true;
    1265             :                                 collen = 0;
    1266             :                         }
    1267             :                 }
    1268             : 
    1269             :                 /* if (Max) column length specified take that into consideration. (Meaning what, exactly?) */
    1270             : 
    1271        2016 :                 if (!data_is_null && hostcol->column_len >= 0) {
    1272           0 :                         if (hostcol->column_len == 0)
    1273             :                                 data_is_null = true;
    1274           0 :                         else if (collen)
    1275           0 :                                 collen = TDS_MIN(hostcol->column_len, collen);
    1276             :                         else
    1277             :                                 collen = hostcol->column_len;
    1278             :                 }
    1279             : 
    1280        2016 :                 tdsdump_log(TDS_DBG_FUNC, "prefix_len = %d collen = %d \n", hostcol->prefix_len, collen);
    1281             : 
    1282             :                 /* Fixed Length data - this overrides anything else specified */
    1283             : 
    1284        2016 :                 if (is_fixed_type(hostcol->datatype))
    1285          10 :                         collen = tds_get_size_by_type(hostcol->datatype);
    1286             : 
    1287        2016 :                 col_start = ftello(hostfile);
    1288             : 
    1289             :                 /*
    1290             :                  * The data file either contains prefixes stating the length, or is delimited.  
    1291             :                  * If delimited, we "measure" the field by looking for the terminator, then read it, 
    1292             :                  * and set collen to the field's post-iconv size.  
    1293             :                  */
    1294        2016 :                 if (hostcol->term_len > 0) { /* delimited data file */
    1295             :                         size_t col_bytes;
    1296             :                         TDSRET conv_res;
    1297             : 
    1298             :                         /* 
    1299             :                          * Read and convert the data
    1300             :                          */
    1301        1996 :                         coldata = NULL;
    1302        3992 :                         conv_res = tds_bcp_fread(dbproc->tds_socket, bcpcol ? bcpcol->char_conv : NULL, hostfile,
    1303        1996 :                                                  (const char *) hostcol->terminator, hostcol->term_len, &coldata, &col_bytes);
    1304             : 
    1305        1996 :                         if (TDS_FAILED(conv_res)) {
    1306           0 :                                 tdsdump_log(TDS_DBG_FUNC, "col %d: error converting %ld bytes!\n",
    1307             :                                                         (i+1), (long) collen);
    1308           0 :                                 *row_error = true;
    1309           0 :                                 free(coldata);
    1310           0 :                                 dbperror(dbproc, SYBEBCOR, 0);
    1311           0 :                                 return FAIL;
    1312             :                         }
    1313             : 
    1314        1996 :                         if (conv_res == TDS_NO_MORE_RESULTS) {
    1315         154 :                                 free(coldata);
    1316         154 :                                 return _bcp_check_eof(dbproc, hostfile, i);
    1317             :                         }
    1318             : 
    1319        1842 :                         if (col_bytes > 0x7fffffffl) {
    1320           0 :                                 free(coldata);
    1321           0 :                                 *row_error = true;
    1322           0 :                                 tdsdump_log(TDS_DBG_FUNC, "data from file is too large!\n");
    1323           0 :                                 dbperror(dbproc, SYBEBCOR, 0);
    1324           0 :                                 return FAIL;
    1325             :                         }
    1326             : 
    1327        1842 :                         collen = (int)col_bytes;
    1328        1842 :                         if (collen == 0)
    1329         220 :                                 data_is_null = true;
    1330             : 
    1331             :                         /* To support empty strings and keep compatibility with older files empty strings
    1332             :                          * are encoded as a single NUL byte. */
    1333        1842 :                         if (collen == 1 && coldata[0] == '\0')
    1334          40 :                                 collen = 0;
    1335             : 
    1336             :                         /*
    1337             :                          * TODO:  
    1338             :                          *    Dates are a problem.  In theory, we should be able to read non-English dates, which
    1339             :                          *    would contain non-ASCII characters.  One might suppose we should convert date
    1340             :                          *    strings to ISO-8859-1 (or another canonical form) here, because tds_convert() can't be
    1341             :                          *    expected to deal with encodings. But instead date strings are read verbatim and 
    1342             :                          *    passed to tds_convert() without even waving to iconv().  For English dates, this works, 
    1343             :                          *    because English dates expressed as UTF-8 strings are indistinguishable from the ASCII.  
    1344             :                          */
    1345             :                 } else {        /* unterminated field */
    1346             : 
    1347          20 :                         coldata = tds_new(TDS_CHAR, 1 + collen);
    1348          20 :                         if (coldata == NULL) {
    1349           0 :                                 *row_error = true;
    1350           0 :                                 dbperror(dbproc, SYBEMEM, errno);
    1351           0 :                                 return FAIL;
    1352             :                         }
    1353             : 
    1354          20 :                         coldata[collen] = 0;
    1355          20 :                         if (collen) {
    1356             :                                 /* 
    1357             :                                  * Read and convert the data
    1358             :                                  * TODO: Call tds_bcp_fread() instead of fread(3).
    1359             :                                  *       The columns should each have their iconv cd set, and noncharacter data
    1360             :                                  *       should have -1 as the iconv cd, causing tds_bcp_fread() to not attempt
    1361             :                                  *       any conversion.  We do not need a datatype switch here to decide what to do.  
    1362             :                                  *       As of 0.62, this *should* actually work.  All that remains is to change the
    1363             :                                  *       call and test it. 
    1364             :                                  */
    1365          20 :                                 tdsdump_log(TDS_DBG_FUNC, "Reading %d bytes from hostfile.\n", collen);
    1366          20 :                                 if (fread(coldata, collen, 1, hostfile) != 1) {
    1367           0 :                                         free(coldata);
    1368           0 :                                         return _bcp_check_eof(dbproc, hostfile, i);
    1369             :                                 }
    1370             :                         }
    1371             :                 }
    1372             : 
    1373             :                 /* 
    1374             :                  * At this point, however the field was read, however big it was, its address is coldata and its size is collen.
    1375             :                  */
    1376        1862 :                 tdsdump_log(TDS_DBG_FUNC, "Data read from hostfile: collen is now %d, data_is_null is %d\n", collen, data_is_null);
    1377        1862 :                 if (!skip && bcpcol) {
    1378        1862 :                         if (data_is_null) {
    1379         220 :                                 bcpcol->bcp_column_data->is_null = true;
    1380         220 :                                 bcpcol->bcp_column_data->datalen = 0;
    1381             :                         } else {
    1382             :                                 TDSRET rc;
    1383             :                                 TDS_SERVER_TYPE desttype;
    1384             : 
    1385        1642 :                                 desttype = tds_get_conversion_type(bcpcol->column_type, bcpcol->column_size);
    1386             : 
    1387        1642 :                                 rc = _bcp_convert_in(dbproc, hostcol->datatype, (const TDS_CHAR*) coldata, collen,
    1388             :                                                      desttype, bcpcol);
    1389        1642 :                                 if (TDS_FAILED(rc)) {
    1390           0 :                                         hostcol->column_error = HOST_COL_CONV_ERROR;
    1391           0 :                                         *row_error = true;
    1392           0 :                                         tdsdump_log(TDS_DBG_FUNC, 
    1393             :                                                 "_bcp_read_hostfile failed to convert %d bytes at offset 0x%" PRIx64 " in the data file.\n",
    1394             :                                                     collen, (TDS_INT8) col_start);
    1395             :                                 }
    1396             : 
    1397        1642 :                                 rtrim_bcpcol(bcpcol);
    1398             :                         }
    1399             : #if USING_SYBEBCNN
    1400             :                         if (!hostcol->column_error) {
    1401             :                                 if (bcpcol->bcp_column_data->datalen <= 0) {   /* Are we trying to insert a NULL ? */
    1402             :                                         if (!bcpcol->column_nullable) {
    1403             :                                                 /* too bad if the column is not nullable */
    1404             :                                                 hostcol->column_error = HOST_COL_NULL_ERROR;
    1405             :                                                 *row_error = true;
    1406             :                                                 dbperror(dbproc, SYBEBCNN, 0);
    1407             :                                         }
    1408             :                                 }
    1409             :                         }
    1410             : #endif
    1411             :                 }
    1412        1862 :                 free(coldata);
    1413             :         }
    1414             :         return MORE_ROWS;
    1415             : }
    1416             : 
    1417             : /** 
    1418             :  * \ingroup dblib_bcp
    1419             :  * \brief Write data in host variables to the table.  
    1420             :  * 
    1421             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    1422             :  * 
    1423             :  * \remarks Call bcp_bind() first to describe the variables to be used.  
    1424             :  *      Use bcp_batch() to commit sets of rows. 
    1425             :  *      After sending the last row call bcp_done().
    1426             :  * \return SUCCEED or FAIL.
    1427             :  * \sa  bcp_batch(), bcp_bind(), bcp_colfmt(), bcp_collen(), bcp_colptr(), bcp_columns(), 
    1428             :  *      bcp_control(), bcp_done(), bcp_exec(), bcp_init(), bcp_moretext(), bcp_options()
    1429             :  */
    1430             : RETCODE
    1431         180 : bcp_sendrow(DBPROCESS * dbproc)
    1432             : {
    1433             :         TDSSOCKET *tds;
    1434             : 
    1435         180 :         tdsdump_log(TDS_DBG_FUNC, "bcp_sendrow(%p)\n", dbproc);
    1436         180 :         CHECK_CONN(FAIL);
    1437         180 :         CHECK_PARAMETER(dbproc->bcpinfo, SYBEBCPI, FAIL);
    1438             : 
    1439         180 :         tds = dbproc->tds_socket;
    1440             : 
    1441         180 :         if (dbproc->bcpinfo->direction != DB_IN) {
    1442           0 :                 dbperror(dbproc, SYBEBCPN, 0);
    1443           0 :                 return FAIL;
    1444             :         }
    1445             : 
    1446         180 :         if (dbproc->hostfileinfo != NULL) {
    1447           0 :                 dbperror(dbproc, SYBEBCPB, 0);
    1448           0 :                 return FAIL;
    1449             :         }
    1450             : 
    1451             :         /* 
    1452             :          * The first time sendrow is called after bcp_init,
    1453             :          * there is a certain amount of initialisation to be done.
    1454             :          */
    1455         180 :         if (!dbproc->bcpinfo->xfer_init) {
    1456             : 
    1457             :                 /* The start_copy function retrieves details of the table's columns */
    1458          28 :                 if (TDS_FAILED(tds_bcp_start_copy_in(tds, dbproc->bcpinfo))) {
    1459           0 :                         dbperror(dbproc, SYBEBULKINSERT, 0);
    1460           0 :                         return FAIL;
    1461             :                 }
    1462             : 
    1463          28 :                 dbproc->bcpinfo->xfer_init = true;
    1464             : 
    1465             :         }
    1466             : 
    1467         180 :         dbproc->bcpinfo->parent = dbproc;
    1468         180 :         return TDS_FAILED(tds_bcp_send_record(dbproc->tds_socket, dbproc->bcpinfo,
    1469         180 :                           _bcp_get_col_data, _bcp_null_error, 0)) ? FAIL : SUCCEED;
    1470             : }
    1471             : 
    1472             : 
    1473             : /** 
    1474             :  * \ingroup dblib_bcp_internal
    1475             :  * \brief 
    1476             :  *
    1477             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    1478             :  * \param rows_copied 
    1479             :  * 
    1480             :  * \return SUCCEED or FAIL.
    1481             :  * \sa  BCP_SETL(), bcp_batch(), bcp_bind(), bcp_colfmt(), bcp_colfmt_ps(), bcp_collen(), bcp_colptr(), bcp_columns(), bcp_control(), bcp_done(), bcp_exec(), bcp_getl(), bcp_init(), bcp_moretext(), bcp_options(), bcp_readfmt(), bcp_sendrow()
    1482             :  */
    1483             : static RETCODE
    1484         164 : _bcp_exec_in(DBPROCESS * dbproc, DBINT * rows_copied)
    1485             : {
    1486         164 :         FILE *hostfile, *errfile = NULL;
    1487         164 :         TDSSOCKET *tds = dbproc->tds_socket;
    1488             :         BCP_HOSTCOLINFO *hostcol;
    1489             :         STATUS ret;
    1490             : 
    1491             :         int i, row_of_hostfile, rows_written_so_far;
    1492             :         int row_error_count;
    1493             :         bool row_error;
    1494             :         offset_type row_start, row_end;
    1495             :         offset_type error_row_size;
    1496         164 :         const size_t chunk_size = 0x20000u;
    1497             :         
    1498         164 :         tdsdump_log(TDS_DBG_FUNC, "_bcp_exec_in(%p, %p)\n", dbproc, rows_copied);
    1499             :         assert(dbproc);
    1500         164 :         assert(rows_copied);
    1501             : 
    1502         164 :         *rows_copied = 0;
    1503             :         
    1504         164 :         if (!(hostfile = fopen(dbproc->hostfileinfo->hostfile, "r"))) {
    1505           0 :                 dbperror(dbproc, SYBEBCUO, 0);
    1506           0 :                 return FAIL;
    1507             :         }
    1508             : 
    1509         164 :         if (TDS_FAILED(tds_bcp_start_copy_in(tds, dbproc->bcpinfo))) {
    1510           0 :                 fclose(hostfile);
    1511           0 :                 return FAIL;
    1512             :         }
    1513             : 
    1514         164 :         row_of_hostfile = 0;
    1515         164 :         rows_written_so_far = 0;
    1516             : 
    1517         164 :         row_error_count = 0;
    1518         164 :         dbproc->bcpinfo->parent = dbproc;
    1519             : 
    1520             :         for (;;) {
    1521             :                 bool skip;
    1522             : 
    1523         540 :                 row_start = ftello(hostfile);
    1524         540 :                 row_error = false;
    1525             : 
    1526         540 :                 row_of_hostfile++;
    1527             : 
    1528             :                 if (row_of_hostfile > TDS_MAX(dbproc->hostfileinfo->lastrow, 0x7FFFFFFF))
    1529             :                         break;
    1530             : 
    1531         540 :                 skip = dbproc->hostfileinfo->firstrow > row_of_hostfile;
    1532         540 :                 ret = _bcp_read_hostfile(dbproc, hostfile, &row_error, skip);
    1533         540 :                 if (ret != MORE_ROWS)
    1534             :                         break;
    1535             : 
    1536         376 :                 if (row_error) {
    1537             :                         int count;
    1538             : 
    1539           0 :                         if (errfile == NULL && dbproc->hostfileinfo->errorfile) {
    1540           0 :                                 if (!(errfile = fopen(dbproc->hostfileinfo->errorfile, "w"))) {
    1541           0 :                                         fclose(hostfile);
    1542           0 :                                         dbperror(dbproc, SYBEBUOE, 0);
    1543           0 :                                         return FAIL;
    1544             :                                 }
    1545             :                         }
    1546             : 
    1547           0 :                         if (errfile != NULL) {
    1548             :                                 char *row_in_error = NULL;
    1549             : 
    1550           0 :                                 for (i = 0; i < dbproc->hostfileinfo->host_colcount; i++) {
    1551           0 :                                         hostcol = dbproc->hostfileinfo->host_columns[i];
    1552           0 :                                         if (hostcol->column_error == HOST_COL_CONV_ERROR) {
    1553           0 :                                                 count = fprintf(errfile, 
    1554             :                                                         "#@ data conversion error on host data file Row %d Column %d\n",
    1555             :                                                         row_of_hostfile, i + 1);
    1556           0 :                                                 if( count < 0 ) {
    1557           0 :                                                         dbperror(dbproc, SYBEBWEF, errno);
    1558             :                                                 }
    1559           0 :                                         } else if (hostcol->column_error == HOST_COL_NULL_ERROR) {
    1560           0 :                                                 count = fprintf(errfile, "#@ Attempt to bulk-copy a NULL value into Server column"
    1561             :                                                                 " which does not accept NULL values. Row %d, Column %d\n",
    1562             :                                                                 row_of_hostfile, i + 1);
    1563           0 :                                                 if( count < 0 ) {
    1564           0 :                                                         dbperror(dbproc, SYBEBWEF, errno);
    1565             :                                                 }
    1566             : 
    1567             :                                         }
    1568             :                                 }
    1569             : 
    1570           0 :                                 row_end = ftello(hostfile);
    1571             : 
    1572             :                                 /* error data can be very long so split in chunks */
    1573           0 :                                 error_row_size = row_end - row_start;
    1574           0 :                                 fseeko(hostfile, row_start, SEEK_SET);
    1575             : 
    1576           0 :                                 while (error_row_size > 0) {
    1577           0 :                                         size_t chunk = TDS_MIN((size_t) error_row_size, chunk_size);
    1578             :                                         size_t written;
    1579             : 
    1580           0 :                                         if (!row_in_error) {
    1581           0 :                                                 if ((row_in_error = tds_new(char, chunk)) == NULL) {
    1582           0 :                                                         dbperror(dbproc, SYBEMEM, errno);
    1583             :                                                 }
    1584             :                                         }
    1585             : 
    1586           0 :                                         if (fread(row_in_error, 1, chunk, hostfile) != chunk)
    1587           0 :                                                 tdsdump_log(TDS_DBG_ERROR, "BILL fread failed after fseek\n");
    1588             : 
    1589           0 :                                         written = fwrite(row_in_error, 1, chunk, errfile);
    1590           0 :                                         if (written < chunk)
    1591           0 :                                                 dbperror(dbproc, SYBEBWEF, errno);
    1592             : 
    1593           0 :                                         error_row_size -= chunk;
    1594             :                                 }
    1595           0 :                                 free(row_in_error);
    1596             : 
    1597           0 :                                 fseeko(hostfile, row_end, SEEK_SET);
    1598           0 :                                 count = fprintf(errfile, "\n");
    1599           0 :                                 if( count < 0 ) {
    1600           0 :                                         dbperror(dbproc, SYBEBWEF, errno);
    1601             :                                 }
    1602             :                         }
    1603           0 :                         row_error_count++;
    1604           0 :                         if (row_error_count >= dbproc->hostfileinfo->maxerrs)
    1605             :                                 break;
    1606           0 :                         continue;
    1607             :                 }
    1608             : 
    1609         376 :                 if (skip)
    1610           0 :                         continue;
    1611             : 
    1612         376 :                 if (TDS_SUCCEED(tds_bcp_send_record(dbproc->tds_socket, dbproc->bcpinfo,
    1613             :                                                     _bcp_no_get_col_data, _bcp_null_error, 0))) {
    1614             : 
    1615         376 :                         rows_written_so_far++;
    1616             : 
    1617         376 :                         if (dbproc->hostfileinfo->batch > 0 && rows_written_so_far == dbproc->hostfileinfo->batch) {
    1618           0 :                                 if (TDS_FAILED(tds_bcp_done(tds, &rows_written_so_far))) {
    1619           0 :                                         if (errfile)
    1620           0 :                                                 fclose(errfile);
    1621           0 :                                         fclose(hostfile);
    1622           0 :                                         return FAIL;
    1623             :                                 }
    1624             : 
    1625           0 :                                 *rows_copied += rows_written_so_far;
    1626           0 :                                 rows_written_so_far = 0;
    1627             : 
    1628           0 :                                 dbperror(dbproc, SYBEBBCI, 0); /* batch copied to server */
    1629             : 
    1630           0 :                                 tds_bcp_start(tds, dbproc->bcpinfo);
    1631             :                         }
    1632             :                 }
    1633             :         }
    1634             :         
    1635         164 :         if (row_error_count == 0 && row_of_hostfile < dbproc->hostfileinfo->firstrow) {
    1636             :                 /* "The BCP hostfile '%1!' contains only %2! rows.  */
    1637           0 :                 dbperror(dbproc, SYBEBCSA, 0, dbproc->hostfileinfo->hostfile, row_of_hostfile); 
    1638             :         }
    1639             : 
    1640         164 :         if (errfile &&  0 != fclose(errfile) ) {
    1641           0 :                 dbperror(dbproc, SYBEBUCE, 0);
    1642             :         }
    1643             : 
    1644         164 :         if (fclose(hostfile) != 0) {
    1645           0 :                 dbperror(dbproc, SYBEBCUC, 0);
    1646           0 :                 ret = FAIL;
    1647             :         }
    1648             : 
    1649         164 :         tds_bcp_done(tds, &rows_written_so_far);
    1650         164 :         *rows_copied += rows_written_so_far;
    1651             : 
    1652         164 :         return ret == NO_MORE_ROWS? SUCCEED : FAIL;     /* (ret is returned from _bcp_read_hostfile) */
    1653             : }
    1654             : 
    1655             : /** 
    1656             :  * \ingroup dblib_bcp
    1657             :  * \brief Write a datafile to a table. 
    1658             :  *
    1659             :  * 
    1660             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    1661             :  * \param rows_copied bcp_exec will write the count of rows successfully written to this address. 
    1662             :  *      If \a rows_copied is NULL, it will be ignored by db-lib. 
    1663             :  *
    1664             :  * \return SUCCEED or FAIL.
    1665             :  * \sa  bcp_batch(), bcp_bind(), bcp_colfmt(), bcp_collen(), bcp_colptr(), bcp_columns(),
    1666             :  *      bcp_control(), bcp_done(), bcp_init(), bcp_sendrow()
    1667             :  */
    1668             : RETCODE
    1669         328 : bcp_exec(DBPROCESS * dbproc, DBINT *rows_copied)
    1670             : {
    1671             :         DBINT dummy_copied;
    1672         328 :         RETCODE ret = FAIL;
    1673             : 
    1674         328 :         tdsdump_log(TDS_DBG_FUNC, "bcp_exec(%p, %p)\n", dbproc, rows_copied);
    1675         328 :         CHECK_CONN(FAIL);
    1676         328 :         CHECK_PARAMETER(dbproc->bcpinfo, SYBEBCPI, FAIL);
    1677         328 :         CHECK_PARAMETER(dbproc->hostfileinfo, SYBEBCVH, FAIL);
    1678             : 
    1679         328 :         if (rows_copied == NULL) /* NULL means we should ignore it */
    1680           0 :                 rows_copied = &dummy_copied;
    1681             : 
    1682         328 :         if (dbproc->bcpinfo->direction == DB_OUT || dbproc->bcpinfo->direction == DB_QUERYOUT) {
    1683         164 :                 ret = _bcp_exec_out(dbproc, rows_copied);
    1684         164 :         } else if (dbproc->bcpinfo->direction == DB_IN) {
    1685         164 :                 ret = _bcp_exec_in(dbproc, rows_copied);
    1686             :         }
    1687         328 :         _bcp_free_storage(dbproc);
    1688             :         
    1689         328 :         return ret;
    1690             : }
    1691             : 
    1692             : /** 
    1693             :  * \ingroup dblib_bcp_internal
    1694             :  * \brief 
    1695             :  *
    1696             :  * \param buffer 
    1697             :  * \param size 
    1698             :  * \param f 
    1699             :  * 
    1700             :  * \return SUCCEED or FAIL.
    1701             :  * \sa  BCP_SETL(), bcp_batch(), bcp_bind(), bcp_colfmt(), bcp_colfmt_ps(), bcp_collen(), bcp_colptr(), bcp_columns(), bcp_control(), bcp_done(), bcp_exec(), bcp_getl(), bcp_init(), bcp_moretext(), bcp_options(), bcp_readfmt(), bcp_sendrow()
    1702             :  */
    1703             : static char *
    1704           0 : _bcp_fgets(char *buffer, int size, FILE *f)
    1705             : {
    1706           0 :         char *p = fgets(buffer, size, f);
    1707           0 :         if (p == NULL) 
    1708             :                 return p;
    1709             : 
    1710             :         /* discard newline */
    1711           0 :         p = strchr(buffer, 0) - 1;
    1712           0 :         if (p >= buffer && *p == '\n')
    1713           0 :                 *p = 0;
    1714             :         return buffer;
    1715             : }
    1716             : 
    1717             : /** 
    1718             :  * \ingroup dblib_bcp
    1719             :  * \brief Read a format definition file.
    1720             :  *
    1721             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    1722             :  * \param filename Name that will be passed to fopen(3).  
    1723             :  * 
    1724             :  * \remarks Reads a format file and calls bcp_columns() and bcp_colfmt() as needed. 
    1725             :  *
    1726             :  * \return SUCCEED or FAIL.
    1727             :  * \sa  bcp_colfmt(), bcp_colfmt_ps(), bcp_columns(), bcp_writefmt()
    1728             :  */
    1729             : RETCODE
    1730           0 : bcp_readfmt(DBPROCESS * dbproc, const char filename[])
    1731             : {
    1732             :         BCP_HOSTCOLINFO hostcol[1];
    1733             :         FILE *ffile;
    1734             :         char buffer[1024];
    1735           0 :         float lf_version = 0.0;
    1736           0 :         int li_numcols = 0;
    1737           0 :         int colinfo_count = 0;
    1738             : 
    1739           0 :         tdsdump_log(TDS_DBG_FUNC, "bcp_readfmt(%p, %s)\n", dbproc, filename? filename:"NULL");
    1740           0 :         CHECK_CONN(FAIL);
    1741           0 :         CHECK_PARAMETER(dbproc->bcpinfo, SYBEBCPI, FAIL);
    1742           0 :         CHECK_NULP(filename, "bcp_readfmt", 2, FAIL);
    1743             : 
    1744           0 :         memset(hostcol, 0, sizeof(hostcol));
    1745             : 
    1746           0 :         if ((ffile = fopen(filename, "r")) == NULL) {
    1747           0 :                 dbperror(dbproc, SYBEBUOF, 0);
    1748           0 :                 goto Cleanup;
    1749             :         }
    1750             : 
    1751           0 :         if ((_bcp_fgets(buffer, sizeof(buffer), ffile)) != NULL) {
    1752           0 :                 lf_version = (float)atof(buffer);
    1753           0 :         } else if (ferror(ffile)) {
    1754           0 :                 dbperror(dbproc, SYBEBRFF, errno);
    1755           0 :                 goto Cleanup;
    1756             :         }
    1757             : 
    1758           0 :         if ((_bcp_fgets(buffer, sizeof(buffer), ffile)) != NULL) {
    1759           0 :                 li_numcols = atoi(buffer);
    1760           0 :         } else if (ferror(ffile)) {
    1761           0 :                 dbperror(dbproc, SYBEBRFF, errno);
    1762           0 :                 goto Cleanup;
    1763             :         }
    1764             : 
    1765           0 :         if (li_numcols <= 0)
    1766             :                 goto Cleanup;
    1767             : 
    1768           0 :         if (bcp_columns(dbproc, li_numcols) == FAIL)
    1769             :                 goto Cleanup;
    1770             : 
    1771             :         do {
    1772           0 :                 memset(hostcol, 0, sizeof(hostcol));
    1773             : 
    1774           0 :                 if (_bcp_fgets(buffer, sizeof(buffer), ffile) == NULL)
    1775             :                         goto Cleanup;
    1776             : 
    1777           0 :                 if (!_bcp_readfmt_colinfo(dbproc, buffer, hostcol))
    1778             :                         goto Cleanup;
    1779             : 
    1780           0 :                 if (bcp_colfmt(dbproc, hostcol->host_column, hostcol->datatype,
    1781             :                                hostcol->prefix_len, hostcol->column_len,
    1782           0 :                                hostcol->terminator, hostcol->term_len, hostcol->tab_colnum) == FAIL) {
    1783             :                         goto Cleanup;
    1784             :                 }
    1785             : 
    1786           0 :                 TDS_ZERO_FREE(hostcol->terminator);
    1787           0 :         } while (++colinfo_count < li_numcols);
    1788             : 
    1789           0 :         if (ferror(ffile)) {
    1790           0 :                 dbperror(dbproc, SYBEBRFF, errno);
    1791           0 :                 goto Cleanup;
    1792             :         }
    1793             :         
    1794           0 :         if (fclose(ffile) != 0) {
    1795           0 :                 dbperror(dbproc, SYBEBUCF, 0);
    1796             :                 /* even if failure is returned ffile is no more valid */
    1797           0 :                 ffile = NULL;
    1798           0 :                 goto Cleanup;
    1799             :         }
    1800           0 :         ffile = NULL;
    1801             : 
    1802           0 :         if (colinfo_count != li_numcols)
    1803             :                 goto Cleanup;
    1804             : 
    1805             :         return SUCCEED;
    1806             : 
    1807           0 : Cleanup:
    1808           0 :         TDS_ZERO_FREE(hostcol->terminator);
    1809           0 :         _bcp_free_columns(dbproc);
    1810           0 :         if (ffile)
    1811           0 :                 fclose(ffile);
    1812             :         return FAIL;
    1813             : }
    1814             : 
    1815             : /** 
    1816             :  * \ingroup dblib_bcp_internal
    1817             :  * \brief 
    1818             :  *
    1819             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    1820             :  * \param buf 
    1821             :  * \param ci 
    1822             :  * 
    1823             :  * \return SUCCEED or FAIL.
    1824             :  * \sa  BCP_SETL(), bcp_batch(), bcp_bind(), bcp_colfmt(), bcp_colfmt_ps(), bcp_collen(), bcp_colptr(), bcp_columns(), bcp_control(), bcp_done(), bcp_exec(), bcp_getl(), bcp_init(), bcp_moretext(), bcp_options(), bcp_readfmt(), bcp_sendrow()
    1825             :  */
    1826             : static int
    1827           0 : _bcp_readfmt_colinfo(DBPROCESS * dbproc, char *buf, BCP_HOSTCOLINFO * ci)
    1828             : {
    1829             :         char *tok;
    1830             :         int whichcol;
    1831             :         char term[30];
    1832             :         int i;
    1833             :         char *lasts;
    1834             : 
    1835             :         enum nextcol
    1836             :         {
    1837             :                 HOST_COLUMN,
    1838             :                 DATATYPE,
    1839             :                 PREFIX_LEN,
    1840             :                 COLUMN_LEN,
    1841             :                 TERMINATOR,
    1842             :                 TAB_COLNUM,
    1843             :                 NO_MORE_COLS
    1844             :         };
    1845             : 
    1846           0 :         assert(dbproc);
    1847           0 :         assert(buf);
    1848           0 :         assert(ci);
    1849           0 :         tdsdump_log(TDS_DBG_FUNC, "_bcp_readfmt_colinfo(%p, %s, %p)\n", dbproc, buf, ci);
    1850             : 
    1851           0 :         tok = strtok_r(buf, " \t", &lasts);
    1852           0 :         whichcol = HOST_COLUMN;
    1853             : 
    1854             :         /* TODO use a better way to get an int atoi is very error prone */
    1855           0 :         while (tok != NULL && whichcol != NO_MORE_COLS) {
    1856           0 :                 switch (whichcol) {
    1857             : 
    1858           0 :                 case HOST_COLUMN:
    1859           0 :                         ci->host_column = atoi(tok);
    1860             : 
    1861           0 :                         if (ci->host_column < 1) {
    1862           0 :                                 dbperror(dbproc, SYBEBIHC, 0);
    1863           0 :                                 return (FALSE);
    1864             :                         }
    1865             : 
    1866             :                         whichcol = DATATYPE;
    1867             :                         break;
    1868             : 
    1869           0 :                 case DATATYPE:
    1870           0 :                         if (strcmp(tok, "SYBCHAR") == 0)
    1871           0 :                                 ci->datatype = SYBCHAR;
    1872           0 :                         else if (strcmp(tok, "SYBTEXT") == 0)
    1873           0 :                                 ci->datatype = SYBTEXT;
    1874           0 :                         else if (strcmp(tok, "SYBBINARY") == 0)
    1875           0 :                                 ci->datatype = SYBBINARY;
    1876           0 :                         else if (strcmp(tok, "SYBIMAGE") == 0)
    1877           0 :                                 ci->datatype = SYBIMAGE;
    1878           0 :                         else if (strcmp(tok, "SYBINT1") == 0)
    1879           0 :                                 ci->datatype = SYBINT1;
    1880           0 :                         else if (strcmp(tok, "SYBINT2") == 0)
    1881           0 :                                 ci->datatype = SYBINT2;
    1882           0 :                         else if (strcmp(tok, "SYBINT4") == 0)
    1883           0 :                                 ci->datatype = SYBINT4;
    1884           0 :                         else if (strcmp(tok, "SYBINT8") == 0)
    1885           0 :                                 ci->datatype = SYBINT8;
    1886           0 :                         else if (strcmp(tok, "SYBFLT8") == 0)
    1887           0 :                                 ci->datatype = SYBFLT8;
    1888           0 :                         else if (strcmp(tok, "SYBREAL") == 0)
    1889           0 :                                 ci->datatype = SYBREAL;
    1890           0 :                         else if (strcmp(tok, "SYBBIT") == 0)
    1891           0 :                                 ci->datatype = SYBBIT;
    1892           0 :                         else if (strcmp(tok, "SYBNUMERIC") == 0)
    1893           0 :                                 ci->datatype = SYBNUMERIC;
    1894           0 :                         else if (strcmp(tok, "SYBDECIMAL") == 0)
    1895           0 :                                 ci->datatype = SYBDECIMAL;
    1896           0 :                         else if (strcmp(tok, "SYBMONEY") == 0)
    1897           0 :                                 ci->datatype = SYBMONEY;
    1898           0 :                         else if (strcmp(tok, "SYBMONEY4") == 0)
    1899           0 :                                 ci->datatype = SYBMONEY4;
    1900           0 :                         else if (strcmp(tok, "SYBDATETIME") == 0)
    1901           0 :                                 ci->datatype = SYBDATETIME;
    1902           0 :                         else if (strcmp(tok, "SYBDATETIME4") == 0)
    1903           0 :                                 ci->datatype = SYBDATETIME4;
    1904             :                         /* TODO SQL* for MS
    1905             :                            SQLNCHAR SQLBIGINT SQLTINYINT SQLSMALLINT
    1906             :                            SQLUNIQUEID SQLVARIANT SQLUDT */
    1907             :                         else {
    1908           0 :                                 dbperror(dbproc, SYBEBUDF, 0);
    1909           0 :                                 return (FALSE);
    1910             :                         }
    1911             : 
    1912             :                         whichcol = PREFIX_LEN;
    1913             :                         break;
    1914             : 
    1915           0 :                 case PREFIX_LEN:
    1916           0 :                         ci->prefix_len = atoi(tok);
    1917           0 :                         whichcol = COLUMN_LEN;
    1918           0 :                         break;
    1919           0 :                 case COLUMN_LEN:
    1920           0 :                         ci->column_len = atoi(tok);
    1921           0 :                         whichcol = TERMINATOR;
    1922           0 :                         break;
    1923           0 :                 case TERMINATOR:
    1924             : 
    1925           0 :                         if (*tok++ != '\"')
    1926             :                                 return (FALSE);
    1927             : 
    1928           0 :                         for (i = 0; *tok != '\"' && i < sizeof(term); i++) {
    1929           0 :                                 if (*tok == '\\') {
    1930           0 :                                         tok++;
    1931           0 :                                         switch (*tok) {
    1932           0 :                                         case 't':
    1933           0 :                                                 term[i] = '\t';
    1934           0 :                                                 break;
    1935           0 :                                         case 'n':
    1936           0 :                                                 term[i] = '\n';
    1937           0 :                                                 break;
    1938           0 :                                         case 'r':
    1939           0 :                                                 term[i] = '\r';
    1940           0 :                                                 break;
    1941           0 :                                         case '\\':
    1942           0 :                                                 term[i] = '\\';
    1943           0 :                                                 break;
    1944           0 :                                         case '0':
    1945           0 :                                                 term[i] = '\0';
    1946           0 :                                                 break;
    1947             :                                         default:
    1948             :                                                 return (FALSE);
    1949             :                                         }
    1950           0 :                                         tok++;
    1951             :                                 } else
    1952           0 :                                         term[i] = *tok++;
    1953             :                         }
    1954             : 
    1955           0 :                         if (*tok != '\"')
    1956             :                                 return (FALSE);
    1957             : 
    1958           0 :                         ci->term_len = i;
    1959           0 :                         TDS_ZERO_FREE(ci->terminator);
    1960           0 :                         if (i > 0) {
    1961           0 :                                 if ((ci->terminator = tds_new(BYTE, i)) == NULL) {
    1962           0 :                                         dbperror(dbproc, SYBEMEM, errno);
    1963           0 :                                         return FALSE;
    1964             :                                 }
    1965           0 :                                 memcpy(ci->terminator, term, i);
    1966             :                         }
    1967             : 
    1968             :                         whichcol = TAB_COLNUM;
    1969             :                         break;
    1970             : 
    1971           0 :                 case TAB_COLNUM:
    1972           0 :                         ci->tab_colnum = atoi(tok);
    1973           0 :                         whichcol = NO_MORE_COLS;
    1974           0 :                         break;
    1975             : 
    1976             :                 }
    1977           0 :                 tok = strtok_r(NULL, " \t", &lasts);
    1978             :         }
    1979           0 :         if (whichcol == NO_MORE_COLS)
    1980             :                 return (TRUE);
    1981             :         else
    1982           0 :                 return (FALSE);
    1983             : }
    1984             : 
    1985             : #if defined(DBLIB_UNIMPLEMENTED)
    1986             : /** 
    1987             :  * \ingroup dblib_bcp
    1988             :  * \brief Write a format definition file. Not Implemented. 
    1989             :  *
    1990             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    1991             :  * \param filename Name that would be passed to fopen(3).  
    1992             :  * 
    1993             :  * \remarks Reads a format file and calls bcp_columns() and bcp_colfmt() as needed. 
    1994             :  * \a FreeTDS includes freebcp, a utility to copy data to or from a host file. 
    1995             :  *
    1996             :  * \todo For completeness, \a freebcp ought to be able to create format files, but that functionality 
    1997             :  *      is currently lacking, as is bcp_writefmt().
    1998             :  * \todo See the vendors' documentation for the format of these files.
    1999             :  *
    2000             :  * \return SUCCEED or FAIL.
    2001             :  * \sa  bcp_colfmt(), bcp_colfmt_ps(), bcp_columns(), bcp_readfmt()
    2002             :  */
    2003             : RETCODE
    2004             : bcp_writefmt(DBPROCESS * dbproc, const char filename[])
    2005             : {
    2006             :         tdsdump_log(TDS_DBG_FUNC, "UNIMPLEMENTED: bcp_writefmt(%p, %s)\n", dbproc, filename? filename:"NULL");
    2007             :         CHECK_CONN(FAIL);
    2008             :         CHECK_PARAMETER(dbproc->bcpinfo, SYBEBCPI, FAIL);
    2009             :         CHECK_NULP(filename, "bcp_writefmt", 2, FAIL);
    2010             : 
    2011             : #if 0
    2012             :         dbperror(dbproc, SYBEBUFF, errno);      /* bcp: Unable to create format file */
    2013             :         dbperror(dbproc, SYBEBWFF, errno);      /* I/O error while writing bcp format file */
    2014             : #endif
    2015             : 
    2016             :         return FAIL;
    2017             : }
    2018             : 
    2019             : /** 
    2020             :  * \ingroup dblib_bcp
    2021             :  * \brief Write some text or image data to the server.  Not implemented, sadly.  
    2022             :  *
    2023             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    2024             :  * \param size How much to write, in bytes. 
    2025             :  * \param text Address of the data to be written. 
    2026             :  * \remarks For a SYBTEXT or SYBIMAGE column, bcp_bind() can be called with 
    2027             :  *      a NULL varaddr parameter.  If it is, bcp_sendrow() will return control
    2028             :  *      to the application after the non-text data have been sent.  The application then calls 
    2029             :  *      bcp_moretext() -- usually in a loop -- to send the text data in manageable chunks.  
    2030             :  * \todo implement bcp_moretext().
    2031             :  * \return SUCCEED or FAIL.
    2032             :  * \sa  bcp_bind(), bcp_sendrow(), dbmoretext(), dbwritetext()
    2033             :  */
    2034             : RETCODE
    2035             : bcp_moretext(DBPROCESS * dbproc, DBINT size, BYTE * text)
    2036             : {
    2037             :         tdsdump_log(TDS_DBG_FUNC, "UNIMPLEMENTED: bcp_moretext(%p, %d, %p)\n", dbproc, size, text);
    2038             :         CHECK_CONN(FAIL);
    2039             :         CHECK_PARAMETER(dbproc->bcpinfo, SYBEBCPI, FAIL);
    2040             :         CHECK_NULP(text, "bcp_moretext", 3, FAIL);
    2041             :         
    2042             : #if 0
    2043             :         dbperror(dbproc, SYBEBCMTXT, 0); 
    2044             :                 /* bcp_moretext may be used only when there is at least one text or image column in the server table */
    2045             :         dbperror(dbproc, SYBEBTMT, 0); 
    2046             :                 /* Attempt to send too much text data via the bcp_moretext call */
    2047             : #endif
    2048             :         return FAIL;
    2049             : }
    2050             : #endif
    2051             : 
    2052             : /** 
    2053             :  * \ingroup dblib_bcp
    2054             :  * \brief Commit a set of rows to the table. 
    2055             :  * 
    2056             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    2057             :  * \remarks If not called, bcp_done() will cause the rows to be saved.  
    2058             :  * \return Count of rows saved, or -1 on error.
    2059             :  * \sa  bcp_bind(), bcp_done(), bcp_sendrow()
    2060             :  */
    2061             : DBINT
    2062          10 : bcp_batch(DBPROCESS * dbproc)
    2063             : {
    2064          10 :         int rows_copied = 0;
    2065             : 
    2066          10 :         tdsdump_log(TDS_DBG_FUNC, "bcp_batch(%p)\n", dbproc);
    2067          10 :         CHECK_CONN(-1);
    2068          10 :         CHECK_PARAMETER(dbproc->bcpinfo, SYBEBCPI, -1);
    2069             : 
    2070          10 :         if (TDS_FAILED(tds_bcp_done(dbproc->tds_socket, &rows_copied)))
    2071             :                 return -1;
    2072             : 
    2073          10 :         tds_bcp_start(dbproc->tds_socket, dbproc->bcpinfo);
    2074             : 
    2075          10 :         return rows_copied;
    2076             : }
    2077             : 
    2078             : /** 
    2079             :  * \ingroup dblib_bcp
    2080             :  * \brief Conclude the transfer of data from program variables.
    2081             :  * 
    2082             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    2083             :  * \remarks Do not overlook this function.  According to Sybase, failure to call bcp_done() 
    2084             :  * "will result in unpredictable errors".
    2085             :  * \return As with bcp_batch(), the count of rows saved, or -1 on error.
    2086             :  * \sa  bcp_batch(), bcp_bind(), bcp_moretext(), bcp_sendrow()
    2087             :  */
    2088             : DBINT
    2089          38 : bcp_done(DBPROCESS * dbproc)
    2090             : {
    2091             :         int rows_copied;
    2092             : 
    2093          38 :         tdsdump_log(TDS_DBG_FUNC, "bcp_done(%p)\n", dbproc);
    2094          38 :         CHECK_CONN(-1);
    2095             :         
    2096          38 :         if (!(dbproc->bcpinfo))
    2097             :                 return -1;
    2098             : 
    2099          38 :         if (TDS_FAILED(tds_bcp_done(dbproc->tds_socket, &rows_copied)))
    2100             :                 return -1;
    2101             : 
    2102          20 :         _bcp_free_storage(dbproc);
    2103             : 
    2104          20 :         return rows_copied;
    2105             : }
    2106             : 
    2107             : /** 
    2108             :  * \ingroup dblib_bcp
    2109             :  * \brief Bind a program host variable to a database column
    2110             :  * 
    2111             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    2112             :  * \param varaddr address of host variable
    2113             :  * \param prefixlen length of any prefix found at the beginning of \a varaddr, in bytes.  
    2114             :         Use zero for fixed-length datatypes. 
    2115             :  * \param varlen bytes of data in \a varaddr.  Zero for NULL, -1 for fixed-length datatypes. 
    2116             :  * \param terminator byte sequence that marks the end of the data in \a varaddr
    2117             :  * \param termlen length of \a terminator
    2118             :  * \param vartype datatype of the host variable
    2119             :  * \param table_column Nth column, starting at 1, in the table.
    2120             :  * 
    2121             :  * \remarks The order of operation is:
    2122             :  *      - bcp_init() with \a hfile == NULL and \a direction == DB_IN.
    2123             :  *      - bcp_bind(), once per column you want to write to
    2124             :  *      - bcp_batch(), optionally, to commit a set of rows
    2125             :  *      - bcp_done() 
    2126             :  * \return SUCCEED or FAIL.
    2127             :  * \sa  bcp_batch(), bcp_colfmt(), bcp_collen(), bcp_colptr(), bcp_columns(), bcp_control(), 
    2128             :  *      bcp_done(), bcp_exec(), bcp_moretext(), bcp_sendrow() 
    2129             :  */
    2130             : RETCODE
    2131         324 : bcp_bind(DBPROCESS * dbproc, BYTE * varaddr, int prefixlen, DBINT varlen,
    2132             :          BYTE * terminator, int termlen, int db_vartype, int table_column)
    2133             : {
    2134             :         TDS_SERVER_TYPE vartype;
    2135             :         TDSCOLUMN *colinfo;
    2136             : 
    2137         324 :         tdsdump_log(TDS_DBG_FUNC, "bcp_bind(%p, %p, %d, %d -- %p, %d, %s, %d)\n", 
    2138             :                                                 dbproc, varaddr, prefixlen, varlen, 
    2139             :                                                 terminator, termlen, dbprtype(db_vartype), table_column);
    2140         324 :         CHECK_CONN(FAIL);
    2141         324 :         CHECK_PARAMETER(dbproc->bcpinfo, SYBEBCPI, FAIL);
    2142         648 :         DBPERROR_RETURN(db_vartype != 0 && !is_tds_type_valid(db_vartype), SYBEUDTY);
    2143         324 :         vartype = (TDS_SERVER_TYPE) db_vartype;
    2144             : 
    2145         324 :         if (dbproc->hostfileinfo != NULL) {
    2146           0 :                 dbperror(dbproc, SYBEBCPB, 0);
    2147           0 :                 return FAIL;
    2148             :         }
    2149             : 
    2150         324 :         if (dbproc->bcpinfo->direction != DB_IN) {
    2151           0 :                 dbperror(dbproc, SYBEBCPN, 0);
    2152           0 :                 return FAIL;
    2153             :         }
    2154             : 
    2155         324 :         if (varlen < -1) {
    2156           0 :                 dbperror(dbproc, SYBEBCVLEN, 0);
    2157           0 :                 return FAIL;
    2158             :         }
    2159             : 
    2160         324 :         if (prefixlen != 0 && prefixlen != 1 && prefixlen != 2 && prefixlen != 4) {
    2161           0 :                 dbperror(dbproc, SYBEBCBPREF, 0);
    2162           0 :                 return FAIL;
    2163             :         }
    2164             : 
    2165         324 :         if (prefixlen == 0 && varlen == -1 && termlen == -1 && !is_fixed_type(vartype)) {
    2166           0 :                 tdsdump_log(TDS_DBG_FUNC, "bcp_bind(): non-fixed type %d requires prefix or terminator\n", vartype);
    2167             :                 return FAIL;
    2168             :         }
    2169             : 
    2170         324 :         if (is_fixed_type(vartype) && (varlen != -1 && varlen != 0)) {
    2171           0 :                 dbperror(dbproc, SYBEBCIT, 0);
    2172           0 :                 return FAIL;
    2173             :         }
    2174             : 
    2175         324 :         if (table_column <= 0 ||  table_column > dbproc->bcpinfo->bindinfo->num_cols) {
    2176           0 :                 dbperror(dbproc, SYBECNOR, 0);
    2177           0 :                 return FAIL;
    2178             :         }
    2179             :         
    2180         324 :         if (varaddr == NULL && (prefixlen != 0 || termlen != 0)) {
    2181           0 :                 dbperror(dbproc, SYBEBCBNPR, 0);
    2182           0 :                 return FAIL;
    2183             :         }
    2184             : 
    2185         324 :         colinfo = dbproc->bcpinfo->bindinfo->columns[table_column - 1];
    2186             : 
    2187             :         /* If varaddr is NULL and varlen greater than 0, the table column type must be SYBTEXT or SYBIMAGE 
    2188             :                 and the program variable type must be SYBTEXT, SYBCHAR, SYBIMAGE or SYBBINARY */
    2189         324 :         if (varaddr == NULL && varlen > 0) {
    2190           0 :                 int fOK = (colinfo->column_type == SYBTEXT || colinfo->column_type == SYBIMAGE) &&
    2191           0 :                           (vartype == SYBTEXT || vartype == SYBCHAR || vartype == SYBIMAGE || vartype == SYBBINARY );
    2192             :                 if( !fOK ) {
    2193           0 :                         dbperror(dbproc, SYBEBCBNTYP, 0);
    2194           0 :                         tdsdump_log(TDS_DBG_FUNC, "bcp_bind: SYBEBCBNTYP: column=%d and vartype=%d (should fail?)\n", 
    2195             :                                                         colinfo->column_type, vartype);
    2196             :                         /* return FAIL; */
    2197             :                 }
    2198             :         }
    2199             : 
    2200         324 :         colinfo->column_varaddr  = (char *)varaddr;
    2201         324 :         colinfo->column_bindtype = vartype;
    2202         324 :         colinfo->column_bindlen  = varlen;
    2203         324 :         colinfo->bcp_prefix_len = prefixlen;
    2204             : 
    2205         324 :         TDS_ZERO_FREE(colinfo->bcp_terminator);
    2206         324 :         colinfo->bcp_term_len = 0;
    2207         324 :         if (termlen > 0) {
    2208          54 :                 if ((colinfo->bcp_terminator =  tds_new(TDS_CHAR, termlen)) == NULL) {
    2209           0 :                         dbperror(dbproc, SYBEMEM, errno);
    2210           0 :                         return FAIL;
    2211             :                 }
    2212          54 :                 memcpy(colinfo->bcp_terminator, terminator, termlen);
    2213          54 :                 colinfo->bcp_term_len = termlen;
    2214             :         }
    2215             : 
    2216             :         return SUCCEED;
    2217             : }
    2218             : 
    2219             : static void
    2220           0 : _bcp_null_error(TDSBCPINFO *bcpinfo, int index TDS_UNUSED, int offset TDS_UNUSED)
    2221             : {
    2222           0 :         DBPROCESS *dbproc = (DBPROCESS *) bcpinfo->parent;
    2223           0 :         dbperror(dbproc, SYBEBCNN, 0);
    2224           0 : }
    2225             : 
    2226             : /** 
    2227             :  * \ingroup dblib_bcp_internal
    2228             :  * \brief For a bcp in from program variables, get the data from the host variable
    2229             :  *
    2230             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    2231             :  * \param bindcol 
    2232             :  * 
    2233             :  * \return TDS_SUCCESS or TDS_FAIL.
    2234             :  * \sa  _bcp_add_fixed_columns, _bcp_add_variable_columns, _bcp_send_bcp_record
    2235             :  */
    2236             : static TDSRET
    2237        4138 : _bcp_get_col_data(TDSBCPINFO *bcpinfo, TDSCOLUMN *bindcol, int index TDS_UNUSED, int offset TDS_UNUSED)
    2238             : {
    2239             :         TDS_SERVER_TYPE coltype, desttype;
    2240             :         int collen;
    2241             :         int bytes_read;
    2242             :         BYTE *dataptr;
    2243        4138 :         DBPROCESS *dbproc = (DBPROCESS *) bcpinfo->parent;
    2244             :         TDSRET rc;
    2245             : 
    2246        4138 :         tdsdump_log(TDS_DBG_FUNC, "_bcp_get_col_data(%p, %p)\n", bcpinfo, bindcol);
    2247        4138 :         CHECK_CONN(TDS_FAIL);
    2248        4138 :         CHECK_NULP(bindcol, "_bcp_get_col_data", 2, TDS_FAIL);
    2249             : 
    2250        4138 :         dataptr = (BYTE *) bindcol->column_varaddr;
    2251             : 
    2252        4138 :         collen = 0;
    2253             : 
    2254             :         /* If a prefix length specified, read the correct  amount of data. */
    2255             : 
    2256        4138 :         if (bindcol->bcp_prefix_len > 0) {
    2257             : 
    2258           0 :                 switch (bindcol->bcp_prefix_len) {
    2259           0 :                 case 1:
    2260           0 :                         collen = TDS_GET_UA1(dataptr);
    2261           0 :                         dataptr += 1;
    2262           0 :                         break;
    2263           0 :                 case 2:
    2264           0 :                         collen = (TDS_SMALLINT) TDS_GET_UA2(dataptr);
    2265           0 :                         dataptr += 2;
    2266           0 :                         break;
    2267           0 :                 case 4:
    2268           0 :                         collen = (TDS_INT) TDS_GET_UA4(dataptr);
    2269           0 :                         dataptr += 4;
    2270           0 :                         break;
    2271             :                 }
    2272           0 :                 if (collen <= 0)
    2273             :                         goto null_data;
    2274             :         }
    2275             : 
    2276             :         /* if (Max) column length specified take that into consideration. */
    2277             : 
    2278        4138 :         if (bindcol->column_bindlen >= 0) {
    2279        2800 :                 if (bindcol->column_bindlen == 0)
    2280             :                         goto null_data;
    2281        1500 :                 if (collen)
    2282           0 :                         collen = TDS_MIN(bindcol->column_bindlen, collen);
    2283             :                 else
    2284             :                         collen = bindcol->column_bindlen;
    2285             :         }
    2286             : 
    2287        2838 :         desttype = tds_get_conversion_type(bindcol->column_type, bindcol->column_size);
    2288             : 
    2289        2838 :         coltype = bindcol->column_bindtype == 0 ? desttype : (TDS_SERVER_TYPE) bindcol->column_bindtype;
    2290             : 
    2291             :         /* Fixed Length data - this overrides anything else specified */
    2292        2838 :         if (is_fixed_type(coltype))
    2293         750 :                 collen = tds_get_size_by_type(coltype);
    2294             : 
    2295             :         /* read the data, finally */
    2296             : 
    2297        2838 :         if (bindcol->bcp_term_len > 0) {  /* terminated field */
    2298          88 :                 bytes_read = _bcp_get_term_var(dataptr, (BYTE *)bindcol->bcp_terminator, bindcol->bcp_term_len);
    2299             : 
    2300          88 :                 if (collen <= 0 || bytes_read < collen)
    2301          88 :                         collen = bytes_read;
    2302             : 
    2303          88 :                 if (collen == 0)
    2304             :                         goto null_data;
    2305             :         }
    2306             : 
    2307        2810 :         if (collen < 0)
    2308           0 :                 collen = (int) strlen((char *) dataptr);
    2309             : 
    2310        2810 :         rc = _bcp_convert_in(dbproc, coltype, (const TDS_CHAR *) dataptr, collen, desttype, bindcol);
    2311        2810 :         if (TDS_FAILED(rc))
    2312             :                 return rc;
    2313        2810 :         rtrim_bcpcol(bindcol);
    2314             : 
    2315        2810 :         return TDS_SUCCESS;
    2316             : 
    2317        1328 : null_data:
    2318        1328 :         bindcol->bcp_column_data->datalen = 0;
    2319        1328 :         bindcol->bcp_column_data->is_null = true;
    2320        1328 :         return TDS_SUCCESS;
    2321             : }
    2322             : 
    2323             : /**
    2324             :  * Function to read data from file. In this case is empty as data
    2325             :  * are already on bcp_column_data
    2326             :  */
    2327             : static TDSRET
    2328        1878 : _bcp_no_get_col_data(TDSBCPINFO *bcpinfo TDS_UNUSED, TDSCOLUMN *bindcol TDS_UNUSED, int index TDS_UNUSED, int offset TDS_UNUSED)
    2329             : {
    2330        1878 :         return TDS_SUCCESS;
    2331             : }
    2332             : 
    2333             : /**
    2334             :  * Get the data for bcp-in from program variables, where the program data
    2335             :  * have been identified as character terminated,  
    2336             :  * This is a low-level, internal function.  Call it correctly.  
    2337             :  */
    2338             : /** 
    2339             :  * \ingroup dblib_bcp_internal
    2340             :  * \brief 
    2341             :  *
    2342             :  * \param pdata 
    2343             :  * \param term 
    2344             :  * \param term_len 
    2345             :  * 
    2346             :  * \return data length.
    2347             :  */
    2348             : static int
    2349          88 : _bcp_get_term_var(const BYTE * pdata, const BYTE * term, int term_len)
    2350             : {
    2351             :         int bufpos;
    2352             : 
    2353          88 :         assert(term_len > 0);
    2354             : 
    2355             :         /* if bufpos becomes negative, we probably failed to find the terminator */
    2356         330 :         for (bufpos = 0; bufpos >= 0 && memcmp(pdata, term, term_len) != 0; pdata++) {
    2357         330 :                 bufpos++;
    2358             :         }
    2359             :         
    2360             :         assert(bufpos >= 0);
    2361          88 :         return bufpos;
    2362             : }
    2363             : 
    2364             : /** 
    2365             :  * \ingroup dblib_bcp_internal
    2366             :  * \brief trim a string of trailing blanks 
    2367             :  *
    2368             :  *      Replaces spaces at the end of a string with NULs
    2369             :  * \param str pointer to a character buffer (not null-terminated)
    2370             :  * \param len size of the \a str in bytes 
    2371             :  * 
    2372             :  * \return modified length
    2373             :  */
    2374             : static int
    2375             : rtrim(char *str, int len)
    2376             : {
    2377        1166 :         char *p = str + len - 1;
    2378             : 
    2379        1660 :         while (p > str && *p == ' ') {
    2380         494 :                 *p-- = '\0';
    2381             :         }
    2382        1166 :         return (int)(1 + p - str);
    2383             : }
    2384             : 
    2385             : static int
    2386             : rtrim_u16(uint16_t *str, int len, uint16_t space)
    2387             : {
    2388          62 :         uint16_t *p = str + len / 2 - 1;
    2389             : 
    2390          62 :         while (p > str && *p == space) {
    2391           0 :                 *p-- = '\0';
    2392             :         }
    2393          62 :         return (int)(1 + p - str) * 2;
    2394             : }
    2395             : 
    2396             : /** 
    2397             :  * \ingroup dblib_bcp_internal
    2398             :  * \brief 
    2399             :  *
    2400             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    2401             :  */
    2402             : static void
    2403         984 : _bcp_free_columns(DBPROCESS * dbproc)
    2404             : {
    2405             :         int i;
    2406             : 
    2407         984 :         tdsdump_log(TDS_DBG_FUNC, "_bcp_free_columns(%p)\n", dbproc);
    2408         984 :         assert(dbproc && dbproc->hostfileinfo);
    2409             : 
    2410         984 :         if (dbproc->hostfileinfo->host_columns) {
    2411        3376 :                 for (i = 0; i < dbproc->hostfileinfo->host_colcount; i++) {
    2412        3376 :                         TDS_ZERO_FREE(dbproc->hostfileinfo->host_columns[i]->terminator);
    2413        3376 :                         TDS_ZERO_FREE(dbproc->hostfileinfo->host_columns[i]);
    2414             :                 }
    2415         656 :                 TDS_ZERO_FREE(dbproc->hostfileinfo->host_columns);
    2416         656 :                 dbproc->hostfileinfo->host_colcount = 0;
    2417             :         }
    2418         984 : }
    2419             : 
    2420             : /** 
    2421             :  * \ingroup dblib_bcp_internal
    2422             :  * \brief 
    2423             :  *
    2424             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    2425             :  * 
    2426             :  * \sa  bcp_done(), bcp_exec(), bcp_init()
    2427             :  */
    2428             : static void
    2429         714 : _bcp_free_storage(DBPROCESS * dbproc)
    2430             : {
    2431         714 :         tdsdump_log(TDS_DBG_FUNC, "_bcp_free_storage(%p)\n", dbproc);
    2432         714 :         assert(dbproc);
    2433             : 
    2434         714 :         if (dbproc->hostfileinfo) {
    2435         328 :                 TDS_ZERO_FREE(dbproc->hostfileinfo->hostfile);
    2436         328 :                 TDS_ZERO_FREE(dbproc->hostfileinfo->errorfile);
    2437         328 :                 _bcp_free_columns(dbproc);
    2438         328 :                 TDS_ZERO_FREE(dbproc->hostfileinfo);
    2439             :         }
    2440             : 
    2441         714 :         tds_free_bcpinfo(dbproc->bcpinfo);
    2442         714 :         dbproc->bcpinfo = NULL;
    2443         714 : }
    2444             : 
    2445             : /** Fast strftime for default bcp format */
    2446             : static int
    2447          36 : _bcp_strftime(char *buf, size_t maxsize, const char *format, const TDSDATEREC *dr, int prec)
    2448             : {
    2449          36 :         char *pbuf = buf;
    2450             : 
    2451          36 :         if (format)
    2452           0 :                 return (int) tds_strftime(pbuf, maxsize, format, dr, prec);
    2453             : 
    2454          36 :         pbuf += tds_u32toa_fast(pbuf, dr->year);
    2455             : 
    2456             :         /* The following "& 63" are to avoid overflows if values are corrupted */
    2457             : 
    2458          36 :         *pbuf++ = '-';
    2459          36 :         tds_02d_fast(pbuf, (dr->month + 1) & 63);
    2460          36 :         pbuf += 2;
    2461             : 
    2462          36 :         *pbuf++ = '-';
    2463          36 :         tds_02d_fast(pbuf, dr->day & 63);
    2464          36 :         pbuf += 2;
    2465             : 
    2466          36 :         *pbuf++ = ' ';
    2467          36 :         tds_02d_fast(pbuf, dr->hour & 63);
    2468          36 :         pbuf += 2;
    2469             : 
    2470          36 :         *pbuf++ = ':';
    2471          36 :         tds_02d_fast(pbuf, dr->minute & 63);
    2472          36 :         pbuf += 2;
    2473             : 
    2474          36 :         *pbuf++ = ':';
    2475          36 :         tds_02d_fast(pbuf, dr->second & 63);
    2476          36 :         pbuf += 2;
    2477             : 
    2478          36 :         if (prec > 0) {
    2479             :                 char ibuf[10];
    2480             : 
    2481          36 :                 *pbuf++ = '.';
    2482             : 
    2483          36 :                 memset(ibuf, '0', sizeof(ibuf));
    2484             :                 /* Mask off a negative sign (input should not contain this anyway) */
    2485          36 :                 tds_u32toa_fast_right(ibuf, dr->decimicrosecond & 0x7FFFFFFF);
    2486             :                 /* Skip first 3 digits here, as decimicrosecond should not exceed 9,999,999 */
    2487          36 :                 memcpy(pbuf, ibuf + 3, prec);
    2488          36 :                 pbuf += prec;
    2489             :         }
    2490          36 :         *pbuf = '\0';
    2491             : 
    2492             : #if ENABLE_EXTRA_CHECKS
    2493             :         {
    2494             :                 char tbuf[256];
    2495             : 
    2496          36 :                 tds_strftime(tbuf, sizeof(tbuf), BCP_DEFAULT_DATEFMT, dr, prec);
    2497          36 :                 if (strcmp(tbuf, (char *) buf)) {
    2498           0 :                         fprintf(stderr, "_bcp_strftime(%s) does not match tds_strftime(%s)\n", tbuf, (char *) buf);
    2499           0 :                         tds_extra_assert(!"_bcp_strftime mismatch");
    2500             :                 }
    2501             :         }
    2502             : #endif
    2503          36 :         return (int) (pbuf - buf);
    2504             : }

Generated by: LCOV version 1.13