Add the strlen to push to smb_bytes_push_str, return the converted size
[nivanova/samba-autobuild/.git] / source3 / libsmb / clifile.c
index d3819af444825f301f046f079e35c1e392f89dfe..3b6585b7e79875e88386949f8165f24856e1aede 100644 (file)
@@ -66,7 +66,7 @@ static bool cli_link_internal(struct cli_state *cli, const char *oldname, const
                        -1, 0,                          /* fid, flags */
                        &setup, 1, 0,                   /* setup, length, max */
                        param, param_len, 2,            /* param, length, max */
-                       (char *)&data,  data_len, cli->max_xmit /* data, length, max */
+                       data,  data_len, cli->max_xmit /* data, length, max */
                        )) {
                SAFE_FREE(data);
                SAFE_FREE(param);
@@ -781,12 +781,19 @@ int cli_nt_create(struct cli_state *cli, const char *fname, uint32 DesiredAccess
                                FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OPEN, 0x0, 0x0);
 }
 
-uint8_t *smb_bytes_push_str(uint8_t *buf, bool ucs2, const char *str)
+uint8_t *smb_bytes_push_str(uint8_t *buf, bool ucs2,
+                           const char *str, size_t str_len,
+                           size_t *pconverted_size)
 {
-       size_t buflen = talloc_get_size(buf);
+       size_t buflen;
        char *converted;
        size_t converted_size;
 
+       if (buf == NULL) {
+               return NULL;
+       }
+
+       buflen = talloc_get_size(buf);
        /*
         * We're pushing into an SMB buffer, align odd
         */
@@ -801,7 +808,7 @@ uint8_t *smb_bytes_push_str(uint8_t *buf, bool ucs2, const char *str)
 
        if (!convert_string_allocate(talloc_tos(), CH_UNIX,
                                     ucs2 ? CH_UTF16LE : CH_DOS,
-                                    str, strlen(str)+1, &converted,
+                                    str, str_len, &converted,
                                     &converted_size, true)) {
                return NULL;
        }
@@ -809,12 +816,18 @@ uint8_t *smb_bytes_push_str(uint8_t *buf, bool ucs2, const char *str)
        buf = TALLOC_REALLOC_ARRAY(NULL, buf, uint8_t,
                                   buflen + converted_size);
        if (buf == NULL) {
+               TALLOC_FREE(converted);
                return NULL;
        }
 
        memcpy(buf + buflen, converted, converted_size);
 
        TALLOC_FREE(converted);
+
+       if (pconverted_size) {
+               *pconverted_size = converted_size;
+       }
+
        return buf;
 }
 
@@ -884,18 +897,14 @@ struct async_req *cli_open_send(TALLOC_CTX *mem_ctx, struct event_context *ev,
        }
 
        bytes = talloc_array(talloc_tos(), uint8_t, 0);
-       if (bytes == NULL) {
-               return NULL;
-       }
-
-       bytes = smb_bytes_push_str(
-               bytes, (cli->capabilities & CAP_UNICODE) != 0, fname);
+       bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
+                                  strlen(fname)+1, NULL);
        if (bytes == NULL) {
                return NULL;
        }
 
        result = cli_request_send(mem_ctx, ev, cli, SMBopenX, additional_flags,
-                                 15, vwv, talloc_get_size(bytes), bytes);
+                                 15, vwv, 0, talloc_get_size(bytes), bytes);
        TALLOC_FREE(bytes);
        return result;
 }
@@ -908,9 +917,8 @@ NTSTATUS cli_open_recv(struct async_req *req, int *fnum)
        uint8_t *bytes;
        NTSTATUS status;
 
-       SMB_ASSERT(req->state >= ASYNC_REQ_DONE);
-       if (req->state == ASYNC_REQ_ERROR) {
-               return req->status;
+       if (async_req_is_error(req, &status)) {
+               return status;
        }
 
        status = cli_pull_reply(req, &wct, &vwv, &num_bytes, &bytes);
@@ -975,7 +983,7 @@ struct async_req *cli_close_send(TALLOC_CTX *mem_ctx, struct event_context *ev,
        SSVAL(vwv+0, 0, fnum);
        SIVALS(vwv+1, 0, -1);
 
-       return cli_request_send(mem_ctx, ev, cli, SMBclose, 0, 3, vwv,
+       return cli_request_send(mem_ctx, ev, cli, SMBclose, 0, 3, vwv, 0,
                                0, NULL);
 }
 
@@ -985,10 +993,10 @@ NTSTATUS cli_close_recv(struct async_req *req)
        uint16_t *vwv;
        uint16_t num_bytes;
        uint8_t *bytes;
+       NTSTATUS status;
 
-       SMB_ASSERT(req->state >= ASYNC_REQ_DONE);
-       if (req->state == ASYNC_REQ_ERROR) {
-               return req->status;
+       if (async_req_is_error(req, &status)) {
+               return status;
        }
 
        return cli_pull_reply(req, &wct, &vwv, &num_bytes, &bytes);
@@ -1234,7 +1242,7 @@ bool cli_unlock(struct cli_state *cli, int fnum, uint32 offset, uint32 len)
 ****************************************************************************/
 
 bool cli_lock64(struct cli_state *cli, int fnum,
-               SMB_BIG_UINT offset, SMB_BIG_UINT len, int timeout, enum brl_type lock_type)
+               uint64_t offset, uint64_t len, int timeout, enum brl_type lock_type)
 {
        char *p;
         int saved_timeout = cli->timeout;
@@ -1294,7 +1302,7 @@ bool cli_lock64(struct cli_state *cli, int fnum,
  Unlock a file with 64 bit offsets.
 ****************************************************************************/
 
-bool cli_unlock64(struct cli_state *cli, int fnum, SMB_BIG_UINT offset, SMB_BIG_UINT len)
+bool cli_unlock64(struct cli_state *cli, int fnum, uint64_t offset, uint64_t len)
 {
        char *p;
 
@@ -1341,7 +1349,7 @@ bool cli_unlock64(struct cli_state *cli, int fnum, SMB_BIG_UINT offset, SMB_BIG_
 ****************************************************************************/
 
 static bool cli_posix_lock_internal(struct cli_state *cli, int fnum,
-               SMB_BIG_UINT offset, SMB_BIG_UINT len, bool wait_lock, enum brl_type lock_type)
+               uint64_t offset, uint64_t len, bool wait_lock, enum brl_type lock_type)
 {
        unsigned int param_len = 4;
        unsigned int data_len = POSIX_LOCK_DATA_SIZE;
@@ -1412,7 +1420,7 @@ static bool cli_posix_lock_internal(struct cli_state *cli, int fnum,
 ****************************************************************************/
 
 bool cli_posix_lock(struct cli_state *cli, int fnum,
-                       SMB_BIG_UINT offset, SMB_BIG_UINT len,
+                       uint64_t offset, uint64_t len,
                        bool wait_lock, enum brl_type lock_type)
 {
        if (lock_type != READ_LOCK && lock_type != WRITE_LOCK) {
@@ -1425,7 +1433,7 @@ bool cli_posix_lock(struct cli_state *cli, int fnum,
  POSIX Unlock a file.
 ****************************************************************************/
 
-bool cli_posix_unlock(struct cli_state *cli, int fnum, SMB_BIG_UINT offset, SMB_BIG_UINT len)
+bool cli_posix_unlock(struct cli_state *cli, int fnum, uint64_t offset, uint64_t len)
 {
        return cli_posix_lock_internal(cli, fnum, offset, len, False, UNLOCK_LOCK);
 }
@@ -1434,7 +1442,7 @@ bool cli_posix_unlock(struct cli_state *cli, int fnum, SMB_BIG_UINT offset, SMB_
  POSIX Get any lock covering a file.
 ****************************************************************************/
 
-bool cli_posix_getlock(struct cli_state *cli, int fnum, SMB_BIG_UINT *poffset, SMB_BIG_UINT *plen)
+bool cli_posix_getlock(struct cli_state *cli, int fnum, uint64_t *poffset, uint64_t *plen)
 {
        return True;
 }
@@ -1746,7 +1754,7 @@ int cli_ctemp(struct cli_state *cli, const char *path, char **tmp_path)
                if (!path2) {
                        return -1;
                }
-               clistr_pull(cli, path2, p,
+               clistr_pull(cli->inbuf, path2, p,
                            len+1, len, STR_ASCII);
                *tmp_path = path2;
        }