Path: blob/21.2-virgl/src/amd/common/ac_msgpack.c
7095 views
/*1* Copyright 2021 Advanced Micro Devices, Inc.2* All Rights Reserved.3*4* Permission is hereby granted, free of charge, to any person obtaining a5* copy of this software and associated documentation files (the "Software"),6* to deal in the Software without restriction, including without limitation7* on the rights to use, copy, modify, merge, publish, distribute, sub8* license, and/or sell copies of the Software, and to permit persons to whom9* the Software is furnished to do so, subject to the following conditions:10*11* The above copyright notice and this permission notice (including the next12* paragraph) shall be included in all copies or substantial portions of the13* Software.14*15* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR16* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,17* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL18* THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,19* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR20* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE21* USE OR OTHER DEALINGS IN THE SOFTWARE.22*23*/2425/**26* \file ac_msgpack.c27*28* This file provides functions to create msgpack formatted data.29* for msgpack specification refer to30* github.com/msgpack/msgpack/blob/master/spec.md31*/3233#include <stdint.h>34#include <stdio.h>35#include <stdlib.h>36#include <stdbool.h>37#include <string.h>38#include "util/u_math.h"39#include "ac_msgpack.h"4041#define MSGPACK_MEM_START_SIZE 0x100042#define MSGPACK_MEM_INC_SIZE 0x10004344#define MSGPACK_FIXMAP_OP 0x8045#define MSGPACK_MAP16_OP 0xde46#define MSGPACK_MAP32_OP 0xdf4748#define MSGPACK_FIXARRAY_OP 0x9049#define MSGPACK_ARRAY16_OP 0xdc50#define MSGPACK_ARRAY32_OP 0xdd5152#define MSGPACK_FIXSTR_OP 0xa053#define MSGPACK_STR8_OP 0xd954#define MSGPACK_STR16_OP 0xda55#define MSGPACK_STR32_OP 0xdb5657#define MSGPACK_UINT8_OP 0xcc58#define MSGPACK_UINT16_OP 0xcd59#define MSGPACK_UINT32_OP 0xce60#define MSGPACK_UINT64_OP 0xcf6162#define MSGPACK_NIL_OP 0xc06364#define MSGPACK_INT8_OP 0xd065#define MSGPACK_INT16_OP 0xd166#define MSGPACK_INT32_OP 0xd267#define MSGPACK_INT64_OP 0xd3686970void ac_msgpack_init(struct ac_msgpack *msgpack)71{72msgpack->mem = malloc(MSGPACK_MEM_START_SIZE);73msgpack->mem_size = MSGPACK_MEM_START_SIZE;74msgpack->offset = 0;75}7677void ac_msgpack_destroy(struct ac_msgpack *msgpack)78{79free(msgpack->mem);80}8182int ac_msgpack_resize_if_required(struct ac_msgpack *msgpack,83uint32_t data_size)84{85if ((msgpack->offset + data_size) > msgpack->mem_size) {86uint32_t new_mem_size;8788new_mem_size = msgpack->mem_size +89MAX2(MSGPACK_MEM_INC_SIZE, data_size);90msgpack->mem = realloc(msgpack->mem, new_mem_size);91if (msgpack->mem == NULL)92return false;9394msgpack->mem_size = new_mem_size;95}9697return true;98}99100void ac_msgpack_add_fixmap_op(struct ac_msgpack *msgpack, uint32_t n)101{102if (n <= 0xf ) {103if (!ac_msgpack_resize_if_required(msgpack, 1))104return;105msgpack->mem[msgpack->offset] = MSGPACK_FIXMAP_OP | n;106msgpack->offset = msgpack->offset + 1;107} else if (n <= 0xffff) {108if (!ac_msgpack_resize_if_required(msgpack, 3))109return;110msgpack->mem[msgpack->offset] = MSGPACK_MAP16_OP;111*((uint16_t*)&msgpack->mem[msgpack->offset + 1]) = util_bswap16(n);112msgpack->offset = msgpack->offset + 3;113} else {114if (!ac_msgpack_resize_if_required(msgpack, 5))115return;116msgpack->mem[msgpack->offset] = MSGPACK_MAP32_OP;117*((unsigned int*)&msgpack->mem[msgpack->offset + 1]) = util_bswap32(n);118msgpack->offset = msgpack->offset + 5;119}120}121122void ac_msgpack_add_fixarray_op(struct ac_msgpack *msgpack, uint32_t n)123{124if (n <= 0xf ) {125if (!ac_msgpack_resize_if_required(msgpack, 1))126return;127msgpack->mem[msgpack->offset] = MSGPACK_FIXARRAY_OP | n;128msgpack->offset = msgpack->offset + 1;129} else if (n <= 0xffff) {130if (!ac_msgpack_resize_if_required(msgpack, 3))131return;132msgpack->mem[msgpack->offset] = MSGPACK_ARRAY16_OP;133*((uint16_t*)&msgpack->mem[msgpack->offset + 1]) = util_bswap16(n);134msgpack->offset = msgpack->offset + 3;135} else {136if (!ac_msgpack_resize_if_required(msgpack, 5))137return;138msgpack->mem[msgpack->offset] = MSGPACK_ARRAY32_OP;139*((uint32_t*)&msgpack->mem[msgpack->offset + 1]) = util_bswap32(n);140msgpack->offset = msgpack->offset + 5;141}142}143144void ac_msgpack_add_fixstr(struct ac_msgpack *msgpack, char *str)145{146uint32_t n;147148n = strlen(str);149150if (n <= 0x1f) {151if (!ac_msgpack_resize_if_required(msgpack, 1 + n))152return;153msgpack->mem[msgpack->offset] = MSGPACK_FIXSTR_OP | n;154msgpack->offset = msgpack->offset + 1;155} else if (n <= 0xff) {156if (!ac_msgpack_resize_if_required(msgpack, 2 + n))157return;158msgpack->mem[msgpack->offset] = MSGPACK_STR8_OP;159msgpack->mem[msgpack->offset + 1] = n;160msgpack->offset = msgpack->offset + 2;161} else if (n <= 0xffff) {162if (!ac_msgpack_resize_if_required(msgpack, 3 + n))163return;164msgpack->mem[msgpack->offset] = MSGPACK_STR16_OP;165*((uint16_t*)&msgpack->mem[msgpack->offset + 1]) = util_bswap16(n);166msgpack->offset = msgpack->offset + 3;167} else {168if (!ac_msgpack_resize_if_required(msgpack, 5 + n))169return;170msgpack->mem[msgpack->offset] = MSGPACK_STR32_OP;171*((uint32_t*)&msgpack->mem[msgpack->offset + 1]) = util_bswap32(n);172msgpack->offset = msgpack->offset + 5;173}174175memcpy (&msgpack->mem[msgpack->offset], str, n);176msgpack->offset = msgpack->offset + n;177}178179void ac_msgpack_add_uint(struct ac_msgpack *msgpack, uint64_t val)180{181if (val <= 0x7f) {182if (!ac_msgpack_resize_if_required(msgpack, 1))183return;184msgpack->mem[msgpack->offset] = val;185msgpack->offset = msgpack->offset + 1;186} else if (val <= 0xff) {187if (!ac_msgpack_resize_if_required(msgpack, 2))188return;189msgpack->mem[msgpack->offset] = MSGPACK_UINT8_OP;190msgpack->mem[msgpack->offset + 1] = val;191msgpack->offset = msgpack->offset + 2;192} else if (val <= 0xffff) {193if (!ac_msgpack_resize_if_required(msgpack, 3))194return;195msgpack->mem[msgpack->offset] = MSGPACK_UINT16_OP;196*((uint16_t*)&msgpack->mem[msgpack->offset + 1]) = util_bswap16(val);197msgpack->offset = msgpack->offset + 3;198} else if (val <= 0xffffffff) {199if (!ac_msgpack_resize_if_required(msgpack, 5))200return;201msgpack->mem[msgpack->offset] = MSGPACK_UINT32_OP;202*((uint32_t*)&msgpack->mem[msgpack->offset + 1]) = util_bswap32(val);203msgpack->offset = msgpack->offset + 5;204} else {205if (!ac_msgpack_resize_if_required(msgpack, 9))206return;207msgpack->mem[msgpack->offset] = MSGPACK_UINT64_OP;208*((uint64_t*)&msgpack->mem[msgpack->offset + 1]) = util_bswap64(val);209msgpack->offset = msgpack->offset + 9;210}211}212213void ac_msgpack_add_int(struct ac_msgpack *msgpack, int64_t val)214{215if ((val >= -0x7f) && (val <= 0x7f)) {216if ((val >= -31) && (val < 0)) {217if (!ac_msgpack_resize_if_required(msgpack, 1))218return;219msgpack->mem[msgpack->offset] = val | MSGPACK_NIL_OP;220msgpack->offset = msgpack->offset + 1;221} else if ((val >= 0) && (val <= 127)) {222if (!ac_msgpack_resize_if_required(msgpack, 1))223return;224msgpack->mem[msgpack->offset] = val;225msgpack->offset = msgpack->offset + 1;226} else {227if (!ac_msgpack_resize_if_required(msgpack, 2))228return;229msgpack->mem[msgpack->offset] = MSGPACK_INT8_OP;230msgpack->mem[msgpack->offset + 1] = val;231msgpack->offset = msgpack->offset + 2;232}233} else if ((val >= -0x7fff) && (val <= 0x7fff)) {234if (!ac_msgpack_resize_if_required(msgpack, 3))235return;236msgpack->mem[msgpack->offset] = MSGPACK_INT16_OP;237*((int16_t*)&msgpack->mem[msgpack->offset + 1]) = util_bswap32(val);238msgpack->offset = msgpack->offset + 3;239} else if ((val >= -0x7fffffff) && (val <= 0x7fffffff)) {240if (!ac_msgpack_resize_if_required(msgpack, 5))241return;242msgpack->mem[msgpack->offset] = MSGPACK_INT32_OP;243*((int32_t*)&msgpack->mem[msgpack->offset + 1]) = util_bswap32(val);244msgpack->offset = msgpack->offset + 5;245} else {246if (!ac_msgpack_resize_if_required(msgpack, 9))247return;248msgpack->mem[msgpack->offset] = MSGPACK_INT64_OP;249*((int64_t*)&msgpack->mem[msgpack->offset + 1]) = util_bswap64(val);250msgpack->offset = msgpack->offset + 9;251}252}253254255