<commit_before>/* -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
/*
 *     Copyright 2015 Couchbase, Inc.
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 */
#include "config.h"
#include "testapp.h"
#include "testapp_assert_helper.h"

#include <mcbp/protocol/framebuilder.h>
#include <memcached/util.h>

/*
 * Set the read/write commands differently than the default values
 * so that we can verify that the override works
 */
static cb::mcbp::ClientOpcode read_command = cb::mcbp::ClientOpcode::Invalid;
static cb::mcbp::ClientOpcode write_command = cb::mcbp::ClientOpcode::Invalid;

off_t mcbp_raw_command(char* buf,
                       size_t bufsz,
                       cb::mcbp::ClientOpcode cmd,
                       const void* key,
                       size_t keylen,
                       const void* dta,
                       size_t dtalen) {
    /* all of the storage commands use the same command layout */
    off_t key_offset;
    protocol_binary_request_no_extras* request =
        reinterpret_cast<protocol_binary_request_no_extras*>(buf);
    EXPECT_GE(bufsz, (sizeof(*request) + keylen + dtalen));

    memset(request, 0, sizeof(*request));
    if (cmd == read_command || cmd == write_command) {
        request->message.header.request.setExtlen(8);
    } else if (cmd == cb::mcbp::ClientOpcode::AuditPut) {
        request->message.header.request.setExtlen(4);
    } else if (cmd == cb::mcbp::ClientOpcode::EwouldblockCtl) {
        request->message.header.request.setExtlen(12);
    } else if (cmd == cb::mcbp::ClientOpcode::SetCtrlToken) {
        request->message.header.request.setExtlen(8);
    }
    request->message.header.request.setMagic(cb::mcbp::Magic::ClientRequest);
    request->message.header.request.setOpcode(cmd);
    request->message.header.request.setKeylen((uint16_t)keylen);
    request->message.header.request.setBodylen((uint32_t)(
            keylen + dtalen + request->message.header.request.getExtlen()));
    request->message.header.request.setOpaque(0xdeadbeef);

    key_offset = sizeof(protocol_binary_request_no_extras) +
                 request->message.header.request.getExtlen();

    if (key != NULL) {
        memcpy(buf + key_offset, key, keylen);
    }
    if (dta != NULL) {
        memcpy(buf + key_offset + keylen, dta, dtalen);
    }

    return (off_t)(sizeof(*request) + keylen + dtalen +
                   request->message.header.request.getExtlen());
}

off_t mcbp_arithmetic_command(char* buf,<commit_msg>Use cb::mcbp::Request in mcbp_raw_command<commit_after>/* -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
/*
 *     Copyright 2015 Couchbase, Inc.
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 */
#include "config.h"
#include "testapp.h"
#include "testapp_assert_helper.h"

#include <mcbp/protocol/framebuilder.h>
#include <memcached/util.h>

/*
 * Set the read/write commands differently than the default values
 * so that we can verify that the override works
 */
static cb::mcbp::ClientOpcode read_command = cb::mcbp::ClientOpcode::Invalid;
static cb::mcbp::ClientOpcode write_command = cb::mcbp::ClientOpcode::Invalid;

off_t mcbp_raw_command(char* buf,
                       size_t bufsz,
                       cb::mcbp::ClientOpcode cmd,
                       const void* key,
                       size_t keylen,
                       const void* dta,
                       size_t dtalen) {
    EXPECT_GE(bufsz, (sizeof(cb::mcbp::Request) + keylen + dtalen));
    off_t key_offset;
    auto& request = *reinterpret_cast<cb::mcbp::Request*>(buf);
    memset(&request, 0, sizeof(request));
    if (cmd == read_command || cmd == write_command) {
        request.setExtlen(8);
    } else if (cmd == cb::mcbp::ClientOpcode::AuditPut) {
        request.setExtlen(4);
    } else if (cmd == cb::mcbp::ClientOpcode::EwouldblockCtl) {
        request.setExtlen(12);
    } else if (cmd == cb::mcbp::ClientOpcode::SetCtrlToken) {
        request.setExtlen(8);
    }
    request.setMagic(cb::mcbp::Magic::ClientRequest);
    request.setOpcode(cmd);
    request.setKeylen((uint16_t)keylen);
    request.setBodylen((uint32_t)(keylen + dtalen + request.getExtlen()));
    request.setOpaque(0xdeadbeef);

    key_offset = sizeof(request) + request.getExtlen();

    if (key != nullptr) {
        memcpy(buf + key_offset, key, keylen);
    }
    if (dta != nullptr) {
        memcpy(buf + key_offset + keylen, dta, dtalen);
    }

    return (off_t)(sizeof(request) + keylen + dtalen + request.getExtlen());
}

off_t mcbp_arithmetic_command(char* buf,
{'fraction_non_alphanumeric': 0.12371681415929203, 'fraction_numerical': 0.007433628318584071, 'mean_word_length': 3.0890014471780027, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2209848', 'n_tokens_mistral': 1782, 'n_tokens_neox': 1769, 'n_words': 433}