Beispiel #1
0
TEST(BstrTest, Alloc) {
    bstr *p1;
    p1 = bstr_alloc(10);
    EXPECT_EQ(10, bstr_size(p1));
    EXPECT_EQ(0, bstr_len(p1));
    bstr_free(p1);
}
Beispiel #2
0
TEST(BstrTest, DupC) {
    bstr *p1;
    p1 = bstr_dup_c("arfarf");

    EXPECT_EQ(6, bstr_size(p1));
    EXPECT_EQ(6, bstr_len(p1));
    EXPECT_EQ(0, memcmp("arfarf", bstr_ptr(p1), 6));

    bstr_free(p1);
}
Beispiel #3
0
TEST(BstrTest, ExpandLocal) {
    bstr *p1;
    bstr *p2;

    p1 = bstr_alloc(10);
    p2 = bstr_expand(p1, 100);
    ASSERT_NE(reinterpret_cast<bstr*>(NULL), p2);
    EXPECT_EQ(100, bstr_size(p2));
    EXPECT_EQ(0, bstr_len(p2));

    bstr_free(p2);
}
Beispiel #4
0
TEST(BstrTest, DupEx) {
    bstr *p1;
    bstr *p2;
    p1 = bstr_dup_c("0123456789abcdefghijkl");
    p2 = bstr_dup_ex(p1, 4, 10);

    EXPECT_EQ(10, bstr_size(p2));
    EXPECT_EQ(10, bstr_len(p2));
    EXPECT_EQ(0, memcmp("456789abcd", bstr_ptr(p2),10));

    bstr_free(p1);
    bstr_free(p2);
}
Beispiel #5
0
static uint8_t *GetBufferForTX(htp_tx_t *tx, uint64_t tx_id,
        DetectEngineThreadCtx *det_ctx,
        Flow *f, uint8_t flags, uint32_t *buffer_len)
{
    *buffer_len = 0;

    HttpHeaderThreadData *hdr_td = NULL;
    HttpHeaderBuffer *buf = HttpHeaderGetBufferSpaceForTXID(det_ctx, f, flags,
            tx_id, g_keyword_thread_id, &hdr_td);
    if (unlikely(buf == NULL)) {
        return NULL;
    } else if (buf->len > 0) {
        /* already filled buf, reuse */
        *buffer_len = buf->len;
        return buf->buffer;
    }

    bstr *line = NULL;
    htp_table_t *headers;
    if (flags & STREAM_TOSERVER) {
        if (AppLayerParserGetStateProgress(IPPROTO_TCP, ALPROTO_HTTP, tx, flags) <=
                HTP_REQUEST_HEADERS)
            return NULL;
        line = tx->request_line;
        headers = tx->request_headers;
    } else {
        if (AppLayerParserGetStateProgress(IPPROTO_TCP, ALPROTO_HTTP, tx, flags) <=
                HTP_RESPONSE_HEADERS)
            return NULL;
        headers = tx->response_headers;
        line = tx->response_line;
    }
    if (line == NULL || headers == NULL)
        return NULL;

    size_t line_size = bstr_len(line) + 2;
    if (line_size + buf->len > buf->size) {
        if (HttpHeaderExpandBuffer(hdr_td, buf, line_size) != 0) {
            return NULL;
        }
    }
    memcpy(buf->buffer + buf->len, bstr_ptr(line), bstr_size(line));
    buf->len += bstr_size(line);
    buf->buffer[buf->len++] = '\r';
    buf->buffer[buf->len++] = '\n';

    size_t i = 0;
    size_t no_of_headers = htp_table_size(headers);
    for (; i < no_of_headers; i++) {
        htp_header_t *h = htp_table_get_index(headers, i, NULL);
        size_t size1 = bstr_size(h->name);
        size_t size2 = bstr_size(h->value);
        size_t size = size1 + size2 + 4;
        if (i + 1 == no_of_headers)
            size += 2;
        if (size + buf->len > buf->size) {
            if (HttpHeaderExpandBuffer(hdr_td, buf, size) != 0) {
                return NULL;
            }
        }

        memcpy(buf->buffer + buf->len, bstr_ptr(h->name), bstr_size(h->name));
        buf->len += bstr_size(h->name);
        buf->buffer[buf->len++] = ':';
        buf->buffer[buf->len++] = ' ';
        memcpy(buf->buffer + buf->len, bstr_ptr(h->value), bstr_size(h->value));
        buf->len += bstr_size(h->value);
        buf->buffer[buf->len++] = '\r';
        buf->buffer[buf->len++] = '\n';
        if (i + 1 == no_of_headers) {
            buf->buffer[buf->len++] = '\r';
            buf->buffer[buf->len++] = '\n';
        }
    }

    *buffer_len = buf->len;
    return buf->buffer;
}