Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions BSD_LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
Copyright (c) 2002-2012, Jouni Malinen <j@w1.fi> and contributors
All Rights Reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.

3. Neither the name(s) of the above-listed copyright holder(s) nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2 changes: 1 addition & 1 deletion rpm/sid.spec
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ rm -f %{buildroot}/%{_libdir}/sid/modules/ucmd/type/*.{a,la}
%multilib_fix_c_header --file %{_includedir}/sid/config.h

%files
%license COPYING
%license COPYING BSD_LICENSE
%{_sbindir}/sid
%config(noreplace) %{_sysconfdir}/sysconfig/sid.sysconfig
%{_udevrulesdir}/00-sid.rules
Expand Down
3 changes: 2 additions & 1 deletion src/base/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ libsidbase_la_SOURCES = mem.c \
comms.c \
util.c \
hash.c \
formatter.c
formatter.c \
base64.c

basedir = $(pkgincludedir)/base

Expand Down
152 changes: 152 additions & 0 deletions src/base/base64.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
/*
* Base64 encoding/decoding (RFC1341)
* Copyright (c) 2005-2011, Jouni Malinen <j@w1.fi>
* Copyright (c) 2021 Red Hat, Inc. All rights reserved.
*
* This software may be distributed under the terms of the BSD license.
* See BSD_LICENSE for more details.
* SPDX-License-Identifier: BSD-3-Clause
*/

#include "base/base64.h"

#include <errno.h>
#include <stdlib.h>
#include <string.h>

static const unsigned char base64_table[65] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

/**
* base64_len_encode - Size necessary for base64_encode
* @in_len: Length of the data to be encoded
* Returns: output length needed to store the base64 encoded data, including
* padding and NULL bytes, or 0 if the buffer overflowed.
*/

size_t base64_len_encode(size_t in_len)
{
size_t out_len = 1; /* NULL termination */

if (!in_len)
return out_len;
out_len += ((in_len - 1) / 3 + 1) * 4; /* 4 bytes for every 3 (rounded up) */
if (out_len < in_len)
return 0;
return out_len;
}

/**
* base64_encode - Base64 encode
* @src: Data to be encoded
* @in_len: Length of the data to be encoded
* @dest: pre-allocated buffer to store the encoded data
* @out_len: Length of the encoded data
* Returns: buffer of out_len bytes of encoded data
*
* Returned buffer is nul terminated to make it easier to use as a C string.
*/
int base64_encode(const unsigned char *src, size_t in_len, unsigned char *dest, size_t out_len)
{
unsigned char * pos;
const unsigned char *end, *in;
size_t check_size;

check_size = base64_len_encode(in_len);
if (check_size == 0 || check_size > out_len)
return -EINVAL;

end = src + in_len;
in = src;
pos = dest;
while (end - in >= 3) {
*pos++ = base64_table[in[0] >> 2];
*pos++ = base64_table[((in[0] & 0x03) << 4) | (in[1] >> 4)];
*pos++ = base64_table[((in[1] & 0x0f) << 2) | (in[2] >> 6)];
*pos++ = base64_table[in[2] & 0x3f];
in += 3;
}

if (end - in) {
*pos++ = base64_table[in[0] >> 2];
if (end - in == 1) {
*pos++ = base64_table[(in[0] & 0x03) << 4];
*pos++ = '=';
} else {
*pos++ = base64_table[((in[0] & 0x03) << 4) | (in[1] >> 4)];
*pos++ = base64_table[(in[1] & 0x0f) << 2];
}
*pos++ = '=';
}

*pos = '\0';
return 0;
}

/**
* base64_decode - Base64 decode
* @src: Data to be decoded
* @len: Length of the data to be decoded
* @out_len: Pointer to output length variable
* Returns: Allocated buffer of out_len bytes of decoded data,
* or %NULL on failure
*
* Caller is responsible for freeing the returned buffer.
*/
unsigned char *base64_decode(const unsigned char *src, size_t len, size_t *out_len)
{
unsigned char dtable[256], *out, *pos, block[4], tmp;
size_t i, count, olen;
int pad = 0;

memset(dtable, 0x80, 256);
for (i = 0; i < sizeof(base64_table) - 1; i++)
dtable[base64_table[i]] = (unsigned char) i;
dtable['='] = 0;

count = 0;
for (i = 0; i < len; i++) {
if (dtable[src[i]] != 0x80)
count++;
}

if (count == 0 || count % 4)
return NULL;

olen = count / 4 * 3;
pos = out = malloc(olen);
if (out == NULL)
return NULL;

count = 0;
for (i = 0; i < len; i++) {
tmp = dtable[src[i]];
if (tmp == 0x80)
continue;

if (src[i] == '=')
pad++;
block[count] = tmp;
count++;
if (count == 4) {
*pos++ = (block[0] << 2) | (block[1] >> 4);
*pos++ = (block[1] << 4) | (block[2] >> 2);
*pos++ = (block[2] << 6) | block[3];
count = 0;
if (pad) {
if (pad == 1)
pos--;
else if (pad == 2)
pos -= 2;
else {
/* Invalid padding */
free(out);
return NULL;
}
break;
}
}
}

*out_len = pos - out;
return out;
}
10 changes: 8 additions & 2 deletions src/base/buffer-type-linear.c
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,10 @@ static const void *_buffer_linear_add(struct buffer *buf, void *data, size_t len
goto out;

start = buf->mem + used;
memcpy(start, data, len);
if (data)
memcpy(start, data, len);
else
memset(start, 0, len);
buf->stat.usage.used = used + len;
out:
if (ret_code)
Expand Down Expand Up @@ -206,6 +209,9 @@ static int _buffer_linear_rewind(struct buffer *buf, size_t pos)
{
size_t min_pos = (buf->stat.spec.mode == BUFFER_MODE_SIZE_PREFIX) ? BUFFER_SIZE_PREFIX_LEN : 0;

if (!buf->stat.usage.used && pos == min_pos)
return 0;

if (pos > buf->stat.usage.used || pos < min_pos)
return -EINVAL;

Expand Down Expand Up @@ -259,7 +265,7 @@ static int _buffer_linear_get_data(struct buffer *buf, const void **data, size_t
if (data)
*data = buf->mem + BUFFER_SIZE_PREFIX_LEN;
if (data_size)
*data_size = buf->stat.usage.used - BUFFER_SIZE_PREFIX_LEN;
*data_size = (buf->stat.usage.used) ? buf->stat.usage.used - BUFFER_SIZE_PREFIX_LEN : 0;
break;
default:
return -ENOTSUP;
Expand Down
9 changes: 6 additions & 3 deletions src/base/buffer-type-vector.c
Original file line number Diff line number Diff line change
Expand Up @@ -184,9 +184,9 @@ const void *_buffer_vector_add(struct buffer *buf, void *data, size_t len, int *
struct iovec *iov;
int r;

if (buf == NULL || buf->mem == NULL) {
if (buf == NULL || buf->mem == NULL || data == NULL) {
if (ret_code)
*ret_code = EINVAL;
*ret_code = -EINVAL;
return NULL;
}

Expand Down Expand Up @@ -221,6 +221,9 @@ int _buffer_vector_rewind(struct buffer *buf, size_t pos)
{
size_t min_pos = (buf->stat.spec.mode == BUFFER_MODE_SIZE_PREFIX) ? 1 : 0;

if (!buf->stat.usage.used && pos == min_pos)
return 0;

if (pos > buf->stat.usage.used || pos < min_pos)
return -EINVAL;

Expand Down Expand Up @@ -269,7 +272,7 @@ int _buffer_vector_get_data(struct buffer *buf, const void **data, size_t *data_
if (data)
*data = buf->mem + VECTOR_ITEM_SIZE;
if (data_size)
*data_size = buf->stat.usage.used - 1;
*data_size = (buf->stat.usage.used) ? buf->stat.usage.used - 1 : 0;
break;
default:
return -ENOTSUP;
Expand Down
8 changes: 2 additions & 6 deletions src/base/buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,6 @@ int buffer_reset(struct buffer *buf)

const void *buffer_add(struct buffer *buf, void *data, size_t len, int *ret_code)
{
if (!data) {
if (*ret_code)
*ret_code = -EINVAL;
return NULL;
}

return _buffer_type_registry[buf->stat.spec.type]->add(buf, data, len, ret_code);
}

Expand All @@ -130,6 +124,8 @@ const void *buffer_vfmt_add(struct buffer *buf, int *ret_code, const char *fmt,
int buffer_rewind(struct buffer *buf, size_t pos, buffer_pos_t whence)
{
if (whence == BUFFER_POS_REL) {
if (pos == 0)
return 0; /* otherwise this fails on an empty size-prefixed buffer */
if (pos > buf->stat.usage.used)
return -EINVAL;

Expand Down
48 changes: 48 additions & 0 deletions src/base/formatter.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
*/
#include "base/formatter.h"

#include "base/base64.h"

#include <inttypes.h>
#include <stdio.h>

Expand All @@ -40,6 +42,14 @@ static void _print_fmt(struct buffer *buf, const char *fmt, ...)
va_end(ap);
}

static void _print_binary(unsigned char *value, size_t len, struct buffer *buf)
{
size_t enc_len = base64_len_encode(len);
const char *ptr = buffer_add(buf, NULL, enc_len, NULL);
base64_encode(value, len, (unsigned char *) ptr, enc_len);
buffer_rewind(buf, 1, BUFFER_POS_REL);
}

void print_indent(int level, struct buffer *buf)
{
for (int i = 0; i < level; i++) {
Expand Down Expand Up @@ -123,6 +133,29 @@ void print_str_field(char *field_name, char *value, output_format_t format, stru
}
}

void print_binary_field(char * field_name,
char * value,
size_t len,
output_format_t format,
struct buffer * buf,
bool trailing_comma,
int level)
{
if (format == JSON) {
print_indent(level, buf);
_print_fmt(buf, "\"%s\": \"", field_name);
_print_binary((unsigned char *) value, len, buf);
_print_fmt(buf, "\"");
if (trailing_comma)
_print_fmt(buf, ",");
_print_fmt(buf, "\n");
} else {
_print_fmt(buf, "%s: ", field_name);
_print_binary((unsigned char *) value, len, buf);
_print_fmt(buf, "\n");
}
}

void print_uint_field(char *field_name, uint value, output_format_t format, struct buffer *buf, bool trailing_comma, int level)
{
if (format == JSON) {
Expand Down Expand Up @@ -219,6 +252,21 @@ void print_str_array_elem(char *value, output_format_t format, struct buffer *bu
}
}

void print_binary_array_elem(char *value, size_t len, output_format_t format, struct buffer *buf, bool trailing_comma, int level)
{
if (format == JSON) {
print_indent(level, buf);
_print_fmt(buf, "\"");
_print_binary((unsigned char *) value, len, buf);
_print_fmt(buf, "\"");
if (trailing_comma)
_print_fmt(buf, ",\n");
} else {
_print_binary((unsigned char *) value, len, buf);
_print_fmt(buf, "\n");
}
}

void print_null_byte(struct buffer *buf)
{
buffer_fmt_add(buf, NULL, "");
Expand Down
19 changes: 19 additions & 0 deletions src/include/base/base64.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* Base64 encoding/decoding (RFC1341)
* Copyright (c) 2005, Jouni Malinen <j@w1.fi>
*
* This software may be distributed under the terms of the BSD license.
* See BSD_LICENSE for more details.
* SPDX-License-Identifier: BSD-3-Clause
*/

#ifndef BASE64_H
#define BASE64_H

#include <stddef.h>

size_t base64_len_encode(size_t in_len);
int base64_encode(const unsigned char *src, size_t len, unsigned char *dest, size_t out_len);
unsigned char *base64_decode(const unsigned char *src, size_t len, size_t *out_len);

#endif /* BASE64_H */
Loading