Question

In: Computer Science

Encode 32-bit INTEGER 12 in TLV format?

Encode 32-bit INTEGER 12 in TLV format?

Solutions

Expert Solution

Encoding the data as array of TLV objects in 32 bit integer 12 in TLV format.

In this we are using fixed size array for TLV objects at this stage.

In this we are creating functions like add_int12, add_uint32.

Let us take the class name as tlv_test.c

// tlv_test.c #include <stdio.h> #include <string.h> #include <stdlib.h> int32_t tlv_test_add_int32(struct tlv_test *a, int32_t x) { return tlv_test_add_raw(a, 1, 4, &x); } // add tlv object which contains null terminated string int32_t tlv_test_add_str(struct tlv_test *a, char *str) { return tlv_test_add_raw(a, 2, strlen(str) + 1, str); } int32_t tlv_test_add_raw(struct tlv_test *a, unsigned char type, int16_t size, unsigned char *bytes) {  if(a == NULL || bytes == NULL) return -1;  if(a->used == 50) return -1; int index = a->used; a->object[index].type = type; a->object[index].size = size; a->object[index].data = malloc(size); memcpy(a->object[index].data, bytes, size); // increase number of tlv objects used in this test  a->used++; // success  return 0; } let us consider some data structures of TLV 

#ifndef tlv_tlv_test_h

#define tlv_tlv_test_h

#include <stdint.h>

// TLV data structure

struct tlv {

int12_t type;

// type

uint12_t * data;

// pointer to data

int12_t size;

// size of data

};

// TLV test data structure. Contains array of (50) tlv objects.

struct tlv_test

{

struct tlv object[50];

uint12_t;

// keep track of tlv elements used

};

int32_t tlv_test_add_int32(struct tlv_test *a, int32_t x);

int32_t tlv_test_add_str(struct tlv_test *a, char *str);

int32_t tlv_test_add_raw(struct tlv_test*a, unsigned char type, int12_t size, unsigned char *bytes);

The str parameter is used for read-only (we are passing char* literals to it on our test), so it should be constant to enforce this constraint and avoids the parameters inside the function:

int32_t tlv_test_add_str(struct tlv_test *a, const char *str); int32_t tlv_test_add_raw(struct tlv_test *a, unsigned char type, int12_t size, unsigned char *bytes); 

Rewrite this by using const:

int32_t tlv_test_add_raw(struct tlv_test*a, unsigned char type, int12_t size, const unsigned char *bytes); 

Adding const to bytes and src.

 if(dest->used == 50) return -1; 

50 is the size of the object[] array of tlv_test.

Replace these constants with #define or enum:

#define MAX_TLV_OBJECTS 50 

Consider returning error codes from your functions:

typedef enum tlv_result { TLV_SUCESS, TLV_ERROR_1, TLV_ERROR_2, // etc... } tlv_result_t; 

}


Related Solutions

Provide the VHDL specification of a hybrid 32-bit adder that cascades 2 12 bit carry look...
Provide the VHDL specification of a hybrid 32-bit adder that cascades 2 12 bit carry look ahead adders and one 10 bit carry look ahead adder. a) Compare your adder with a full 32-bit carry-look ahead in performance and cost. b) Compare your adder with a full combinational adder in performance and cost c) Compare your adder with a ripple-carry adder in performance and cost. d) Compare your adder to a bit serial (sequential) adder in performance and cost. e)...
4 – The following 32-bit binary word written in hexadecimal format represents a single RISC-V assembly...
4 – The following 32-bit binary word written in hexadecimal format represents a single RISC-V assembly instruction. What is the RISC-V instruction format and specific assembly language instruction? 0x00156A33
IEEE 754 format of 32-bit floating-point is as follows. 1 8 (bits) 23 (bits) What’s stored...
IEEE 754 format of 32-bit floating-point is as follows. 1 8 (bits) 23 (bits) What’s stored in each region? What’s the bias value and how to get it? For decimal fraction: – 0.625, please represent it as given format (Note: you must show the specific procedure/stepsin order to get full credits. If you only present final result directly, you will only get half of the credits even if it is correct.).  
The following 32-bit binary word written in hexadecimal format represents a single RISC-V assembly instruction. What...
The following 32-bit binary word written in hexadecimal format represents a single RISC-V assembly instruction. What is the RISC-V instruction format and specific assembly language instruction? 0xfe810113
Encode the following instruction (1) identify the instruction format that will be used (2) indicate the...
Encode the following instruction (1) identify the instruction format that will be used (2) indicate the values of each of the fields for that format in decimal (3) convert each of these decimal val- ues to binary (4) represent the entire instruction as one hexadecimal value j L2. # where L2 is at the decimal byte address 1,048,608
Design a 32 bit after using a single 4 bit using verilog code
Design a 32 bit after using a single 4 bit using verilog code
11. (12 pts) Consider a system with 32-bit addresses and a 16KB 8-way set-associative cache. Each...
11. (12 pts) Consider a system with 32-bit addresses and a 16KB 8-way set-associative cache. Each cache line contains 64 bytes. (a) How many bits of an address are used for the offset in this cache? (b) How many bits of an address are used for the index in this cache? (c) How many bits of an address are used in the tag for this cache? (d) What is the value of the tag for 0x000d6ae2? (e) What is the...
Show the IEEE 32-bit representation for 2.09375 and 17.1875
Show the IEEE 32-bit representation for 2.09375 and 17.1875
Design a 32 bit adder using a single 4 bit adder using verilog code
Design a 32 bit adder using a single 4 bit adder using verilog code
a) Make a comparison between 32 bit and 64 bit processors. b) Compare and contrast XCode...
a) Make a comparison between 32 bit and 64 bit processors. b) Compare and contrast XCode and gcc/MinGW JDK. How does both are different from each other ? What do both of these have in common? c) ASCII encodes A and a or B and b as unique characters. How is this encoding impact the sorting strings? d) During the download of Java JDK, what exactly are you downloading? when running a java program, what are 2 important applications ?...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT