mirror of
https://github.com/samba-team/samba.git
synced 2024-12-22 13:34:15 +03:00
libcli/smb: move {smb,trans2}_bytes_push_{str,bytes}() to common code
Signed-off-by: Stefan Metzmacher <metze@samba.org> Reviewed-by: Andreas Schneider <asn@samba.org>
This commit is contained in:
parent
482d3b35e9
commit
7999e6f6c0
@ -24,77 +24,6 @@
|
||||
#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;
|
||||
|
@ -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);
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -29,133 +29,6 @@
|
||||
#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;
|
||||
|
@ -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,
|
||||
|
Loading…
Reference in New Issue
Block a user