libcli/smb: move {smb,trans2}_bytes_push_{str,bytes}() to common code
authorStefan Metzmacher <metze@samba.org>
Tue, 25 Oct 2016 11:31:08 +0000 (13:31 +0200)
committerAndreas Schneider <asn@cryptomilk.org>
Tue, 15 Nov 2016 10:00:26 +0000 (11:00 +0100)
Signed-off-by: Stefan Metzmacher <metze@samba.org>
Reviewed-by: Andreas Schneider <asn@samba.org>
libcli/smb/smb1cli_create.c
libcli/smb/smb_util.h
libcli/smb/util.c
source3/libsmb/clifile.c
source3/libsmb/proto.h

index 1963aedf694ea3a2cfb3fdb28588bd79819c2d74..d9887a04cda8eaf6c3bfb4830822134002a6eef9 100644 (file)
 #include "smb_common.h"
 #include "smbXcli_base.h"
 
-static uint8_t *internal_bytes_push_str(uint8_t *buf, bool ucs2,
-                                       const char *str, size_t str_len,
-                                       bool align_odd,
-                                       size_t *pconverted_size)
-{
-       TALLOC_CTX *frame = talloc_stackframe();
-       size_t buflen;
-       char *converted;
-       size_t converted_size;
-
-       /*
-        * This check prevents us from
-        * (re)alloc buf on a NULL TALLOC_CTX.
-        */
-       if (buf == NULL) {
-               TALLOC_FREE(frame);
-               return NULL;
-       }
-
-       buflen = talloc_get_size(buf);
-
-       if (ucs2 &&
-           ((align_odd && (buflen % 2 == 0)) ||
-            (!align_odd && (buflen % 2 == 1)))) {
-               /*
-                * We're pushing into an SMB buffer, align odd
-                */
-               buf = talloc_realloc(NULL, buf, uint8_t, buflen + 1);
-               if (buf == NULL) {
-                       TALLOC_FREE(frame);
-                       return NULL;
-               }
-               buf[buflen] = '\0';
-               buflen += 1;
-       }
-
-       if (!convert_string_talloc(frame, CH_UNIX,
-                                  ucs2 ? CH_UTF16LE : CH_DOS,
-                                  str, str_len, &converted,
-                                  &converted_size)) {
-               TALLOC_FREE(frame);
-               return NULL;
-       }
-
-       buf = talloc_realloc(NULL, buf, uint8_t,
-                            buflen + converted_size);
-       if (buf == NULL) {
-               TALLOC_FREE(frame);
-               return NULL;
-       }
-
-       memcpy(buf + buflen, converted, converted_size);
-
-       TALLOC_FREE(converted);
-
-       if (pconverted_size) {
-               *pconverted_size = converted_size;
-       }
-
-       TALLOC_FREE(frame);
-       return buf;
-}
-
-static uint8_t *smb_bytes_push_str(uint8_t *buf, bool ucs2,
-                                  const char *str, size_t str_len,
-                                  size_t *pconverted_size)
-{
-       return internal_bytes_push_str(buf, ucs2, str, str_len,
-                                      true, pconverted_size);
-}
-
 struct smb1cli_ntcreatex_state {
        uint16_t vwv[24];
        uint16_t fnum;
index 322ecb63973ab19f65d9750e548fc71574a6b80e..6718b6cb54ec76b5f7f443599c438655d235df11 100644 (file)
@@ -25,3 +25,14 @@ mode_t wire_perms_to_unix(uint32_t perms);
 mode_t unix_filetype_from_wire(uint32_t wire_type);
 
 bool smb_buffer_oob(uint32_t bufsize, uint32_t offset, uint32_t length);
+
+uint8_t *smb_bytes_push_str(uint8_t *buf, bool ucs2,
+                           const char *str, size_t str_len,
+                           size_t *pconverted_size);
+uint8_t *smb_bytes_push_bytes(uint8_t *buf, uint8_t prefix,
+                             const uint8_t *bytes, size_t num_bytes);
+uint8_t *trans2_bytes_push_str(uint8_t *buf, bool ucs2,
+                              const char *str, size_t str_len,
+                              size_t *pconverted_size);
+uint8_t *trans2_bytes_push_bytes(uint8_t *buf,
+                                const uint8_t *bytes, size_t num_bytes);
index b02ae00d0d95f24105a7abc3702def68cc628a09..b048cbabb690274c2e3d882023820f44d63b5fb0 100644 (file)
@@ -175,3 +175,143 @@ bool smb_buffer_oob(uint32_t bufsize, uint32_t offset, uint32_t length)
        }
        return false;
 }
+
+/***********************************************************
+ Common function for pushing stings, used by smb_bytes_push_str()
+ and trans_bytes_push_str(). Only difference is the align_odd
+ parameter setting.
+***********************************************************/
+
+static uint8_t *internal_bytes_push_str(uint8_t *buf, bool ucs2,
+                                       const char *str, size_t str_len,
+                                       bool align_odd,
+                                       size_t *pconverted_size)
+{
+       TALLOC_CTX *frame = talloc_stackframe();
+       size_t buflen;
+       char *converted;
+       size_t converted_size;
+
+       /*
+        * This check prevents us from
+        * (re)alloc buf on a NULL TALLOC_CTX.
+        */
+       if (buf == NULL) {
+               TALLOC_FREE(frame);
+               return NULL;
+       }
+
+       buflen = talloc_get_size(buf);
+
+       if (ucs2 &&
+           ((align_odd && (buflen % 2 == 0)) ||
+            (!align_odd && (buflen % 2 == 1)))) {
+               /*
+                * We're pushing into an SMB buffer, align odd
+                */
+               buf = talloc_realloc(NULL, buf, uint8_t, buflen + 1);
+               if (buf == NULL) {
+                       TALLOC_FREE(frame);
+                       return NULL;
+               }
+               buf[buflen] = '\0';
+               buflen += 1;
+       }
+
+       if (!convert_string_talloc(frame, CH_UNIX,
+                                  ucs2 ? CH_UTF16LE : CH_DOS,
+                                  str, str_len, &converted,
+                                  &converted_size)) {
+               TALLOC_FREE(frame);
+               return NULL;
+       }
+
+       buf = talloc_realloc(NULL, buf, uint8_t,
+                            buflen + converted_size);
+       if (buf == NULL) {
+               TALLOC_FREE(frame);
+               return NULL;
+       }
+
+       memcpy(buf + buflen, converted, converted_size);
+
+       TALLOC_FREE(converted);
+
+       if (pconverted_size) {
+               *pconverted_size = converted_size;
+       }
+
+       TALLOC_FREE(frame);
+       return buf;
+}
+
+/***********************************************************
+ Push a string into an SMB buffer, with odd byte alignment
+ if it's a UCS2 string.
+***********************************************************/
+
+uint8_t *smb_bytes_push_str(uint8_t *buf, bool ucs2,
+                           const char *str, size_t str_len,
+                           size_t *pconverted_size)
+{
+       return internal_bytes_push_str(buf, ucs2, str, str_len,
+                                      true, pconverted_size);
+}
+
+uint8_t *smb_bytes_push_bytes(uint8_t *buf, uint8_t prefix,
+                             const uint8_t *bytes, size_t num_bytes)
+{
+       size_t buflen;
+
+       /*
+        * This check prevents us from
+        * (re)alloc buf on a NULL TALLOC_CTX.
+        */
+       if (buf == NULL) {
+               return NULL;
+       }
+       buflen = talloc_get_size(buf);
+
+       buf = talloc_realloc(NULL, buf, uint8_t,
+                            buflen + 1 + num_bytes);
+       if (buf == NULL) {
+               return NULL;
+       }
+       buf[buflen] = prefix;
+       memcpy(&buf[buflen+1], bytes, num_bytes);
+       return buf;
+}
+
+/***********************************************************
+ Same as smb_bytes_push_str(), but without the odd byte
+ align for ucs2 (we're pushing into a param or data block).
+ static for now, although this will probably change when
+ other modules use async trans calls.
+***********************************************************/
+
+uint8_t *trans2_bytes_push_str(uint8_t *buf, bool ucs2,
+                              const char *str, size_t str_len,
+                              size_t *pconverted_size)
+{
+       return internal_bytes_push_str(buf, ucs2, str, str_len,
+                                      false, pconverted_size);
+}
+
+uint8_t *trans2_bytes_push_bytes(uint8_t *buf,
+                                const uint8_t *bytes, size_t num_bytes)
+{
+       size_t buflen;
+
+       if (buf == NULL) {
+               return NULL;
+       }
+       buflen = talloc_get_size(buf);
+
+       buf = talloc_realloc(NULL, buf, uint8_t,
+                            buflen + num_bytes);
+       if (buf == NULL) {
+               return NULL;
+       }
+       memcpy(&buf[buflen], bytes, num_bytes);
+       return buf;
+}
index fca7c9150fd9ca667f20dd39ec8e047a327233d3..5e667bdca93ca2146f79f104d2ab4eedcfbb8317 100644 (file)
 #include "libcli/security/secdesc.h"
 #include "../libcli/smb/smbXcli_base.h"
 
-/***********************************************************
- Common function for pushing stings, used by smb_bytes_push_str()
- and trans_bytes_push_str(). Only difference is the align_odd
- parameter setting.
-***********************************************************/
-
-static uint8_t *internal_bytes_push_str(uint8_t *buf, bool ucs2,
-                               const char *str, size_t str_len,
-                               bool align_odd,
-                               size_t *pconverted_size)
-{
-       size_t buflen;
-       char *converted;
-       size_t converted_size;
-
-       if (buf == NULL) {
-               return NULL;
-       }
-
-       buflen = talloc_get_size(buf);
-
-       if (ucs2 &&
-           ((align_odd && (buflen % 2 == 0)) ||
-            (!align_odd && (buflen % 2 == 1)))) {
-               /*
-                * We're pushing into an SMB buffer, align odd
-                */
-               buf = talloc_realloc(NULL, buf, uint8_t, buflen + 1);
-               if (buf == NULL) {
-                       return NULL;
-               }
-               buf[buflen] = '\0';
-               buflen += 1;
-       }
-
-       if (!convert_string_talloc(talloc_tos(), CH_UNIX,
-                                  ucs2 ? CH_UTF16LE : CH_DOS,
-                                  str, str_len, &converted,
-                                  &converted_size)) {
-               return NULL;
-       }
-
-       buf = talloc_realloc(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;
-}
-
-/***********************************************************
- Push a string into an SMB buffer, with odd byte alignment
- if it's a UCS2 string.
-***********************************************************/
-
-uint8_t *smb_bytes_push_str(uint8_t *buf, bool ucs2,
-                           const char *str, size_t str_len,
-                           size_t *pconverted_size)
-{
-       return internal_bytes_push_str(buf, ucs2, str, str_len,
-                       true, pconverted_size);
-}
-
-uint8_t *smb_bytes_push_bytes(uint8_t *buf, uint8_t prefix,
-                             const uint8_t *bytes, size_t num_bytes)
-{
-       size_t buflen;
-
-       if (buf == NULL) {
-               return NULL;
-       }
-       buflen = talloc_get_size(buf);
-
-       buf = talloc_realloc(NULL, buf, uint8_t,
-                                  buflen + 1 + num_bytes);
-       if (buf == NULL) {
-               return NULL;
-       }
-       buf[buflen] = prefix;
-       memcpy(&buf[buflen+1], bytes, num_bytes);
-       return buf;
-}
-
-/***********************************************************
- Same as smb_bytes_push_str(), but without the odd byte
- align for ucs2 (we're pushing into a param or data block).
- static for now, although this will probably change when
- other modules use async trans calls.
-***********************************************************/
-
-uint8_t *trans2_bytes_push_str(uint8_t *buf, bool ucs2,
-                              const char *str, size_t str_len,
-                              size_t *pconverted_size)
-{
-       return internal_bytes_push_str(buf, ucs2, str, str_len,
-                       false, pconverted_size);
-}
-
-uint8_t *trans2_bytes_push_bytes(uint8_t *buf,
-                                const uint8_t *bytes, size_t num_bytes)
-{
-       size_t buflen;
-
-       if (buf == NULL) {
-               return NULL;
-       }
-       buflen = talloc_get_size(buf);
-
-       buf = talloc_realloc(NULL, buf, uint8_t,
-                            buflen + num_bytes);
-       if (buf == NULL) {
-               return NULL;
-       }
-       memcpy(&buf[buflen], bytes, num_bytes);
-       return buf;
-}
-
 struct cli_setpathinfo_state {
        uint16_t setup;
        uint8_t *param;
index 34c77b66c3ed20baff13b12a5a73197f2e833c1f..6355c2433e7a1006ff97226e03175247bbd176d1 100644 (file)
@@ -376,15 +376,6 @@ NTSTATUS cli_ntcreate(struct cli_state *cli,
                      uint8_t SecurityFlags,
                      uint16_t *pfid,
                      struct smb_create_returns *cr);
-uint8_t *smb_bytes_push_str(uint8_t *buf, bool ucs2, const char *str,
-                           size_t str_len, size_t *pconverted_size);
-uint8_t *smb_bytes_push_bytes(uint8_t *buf, uint8_t prefix,
-                             const uint8_t *bytes, size_t num_bytes);
-uint8_t *trans2_bytes_push_str(uint8_t *buf, bool ucs2,
-                              const char *str, size_t str_len,
-                              size_t *pconverted_size);
-uint8_t *trans2_bytes_push_bytes(uint8_t *buf,
-                                const uint8_t *bytes, size_t num_bytes);
 struct tevent_req *cli_openx_create(TALLOC_CTX *mem_ctx,
                                   struct tevent_context *ev,
                                   struct cli_state *cli, const char *fname,