File:  [ELWIX - Embedded LightWeight unIX -] / libelwix / example / test_pack.c
Revision 1.6: download - view: text, annotated - select for diffs - revision graph
Mon Apr 15 10:42:26 2024 UTC (7 months, 1 week ago) by misho
Branches: MAIN
CVS tags: elwix6_6, elwix6_5, elwix6_4, elwix6_3, elwix6_2, elwix6_1, elwix5_12, HEAD, ELWIX6_5, ELWIX6_4, ELWIX6_2, ELWIX6_1, ELWIX6_0, ELWIX5_11
Version 5.11

#include <stdio.h>
#include <string.h>
#include <elwix.h>


int
main(int argc, char **argv)
{
	rpack_t *rp;
	register int i;
	char n8 = '*';
	short n16 = 0xdeba;
	int n32 = 0xdeadbeef;
	long long int n64 = 0x123456789abcdef0;
	char *str;

	if (argc < 2)
		return 1;

	rp = rpack_create(argv[1], strlen(argv[1]) + 1);
	if (!rp) {
		printf("#%d - %s\n", elwix_GetErrno(), elwix_GetError());
		return 2;
	} else
		printf("create raw packet %p -> %s(%zu)\n", rp, rp->r_buf, rp->r_len);

	printf("test rpack_data::\n");
	for (i = 0; i < RPACK_LEN(rp); i++) {
		if (i == 4) {
			str = rpack_data(rp, "F0ST4T4", 8);
			str[7] = 0;
			printf("\t%d) %s\n", i, str ? str : NULL);
			i += 8;
		} else {
			str = rpack_data(rp, NULL, 1);
			printf("\t%d) %c\n", i, str ? *str : 0xff);
		}
		e_free(str);
	}
	RPACK_REWIND(rp);
	for (i = 0; i < RPACK_LEN(rp); i++) {
		str = rpack_data(rp, NULL, 1);
		printf("\t%d) %c\n", i, str ? *str : 0xff);
		e_free(str);
	}
	RPACK_REWIND(rp);

	printf("test rpack_raw::\n");
	for (i = 0; i < RPACK_LEN(rp); i++) {
		if (i == 4) {
			str = rpack_rdata(rp, "F0ST4T4", 8);
			str[7] = 0;
			printf("\t%d) %s\n", i, str ? str : NULL);
			i += 8;
		} else {
			str = rpack_rdata(rp, NULL, 1);
			printf("\t%d) %c\n", i, str ? *str : 0xff);
		}
		e_free(str);
	}
	RPACK_REWIND(rp);
	for (i = 0; i < RPACK_LEN(rp); i++) {
		str = rpack_rdata(rp, NULL, 1);
		printf("\t%d) %c\n", i, str ? *str : 0xff);
		e_free(str);
	}
	RPACK_REWIND(rp);


	printf("test rpack_uint8::\n");
	for (i = 0; i < RPACK_LEN(rp); i++)
		if (i == 4)
			printf("\t%d) %c\n", i, rpack_uint8(rp, (uint8_t*) &n8));
		else
			printf("\t%d) %c\n", i, rpack_uint8(rp, NULL));
	RPACK_REWIND(rp);
	for (i = 0; i < RPACK_LEN(rp); i++)
		printf("\t%d) %c\n", i, rpack_uint8(rp, NULL));
	RPACK_REWIND(rp);

	printf("test rpack_uint16::\n");
	for (i = 0; i < RPACK_LEN(rp); i++)
		if (i == 4)
			printf("\t%d) %x\n", i, rpack_uint16(rp, (uint16_t*) &n16, 0));
		else
			printf("\t%d) %x\n", i, rpack_uint16(rp, NULL, 0));
	RPACK_REWIND(rp);
	for (i = 0; i < RPACK_LEN(rp); i++)
		printf("\t%d) %x\n", i, rpack_uint16(rp, NULL, 0));
	RPACK_REWIND(rp);

	printf("test rpack_ruint16::\n");
	for (i = 0; i < RPACK_LEN(rp); i++)
		if (i == 4)
			printf("\t%d) %x\n", i, rpack_ruint16(rp, (uint16_t*) &n16, 0));
		else
			printf("\t%d) %x\n", i, rpack_ruint16(rp, NULL, 0));
	RPACK_REWIND(rp);
	for (i = 0; i < RPACK_LEN(rp); i++)
		printf("\t%d) %x\n", i, rpack_ruint16(rp, NULL, 0));
	RPACK_REWIND(rp);

	printf("test rpack_uint24::\n");
	for (i = 0; i < RPACK_LEN(rp); i++)
		if (i == 4)
			printf("\t%d) %x\n", i, rpack_uint24(rp, (uint32_t*) &n32, 0));
		else
			printf("\t%d) %x\n", i, rpack_uint24(rp, NULL, 0));
	RPACK_REWIND(rp);
	for (i = 0; i < RPACK_LEN(rp); i++)
		printf("\t%d) %x\n", i, rpack_uint24(rp, NULL, 0));
	RPACK_REWIND(rp);

	printf("test rpack_ruint24::\n");
	for (i = 0; i < RPACK_LEN(rp); i++)
		if (i == 4)
			printf("\t%d) %x\n", i, rpack_ruint24(rp, (uint32_t*) &n32, 0));
		else
			printf("\t%d) %x\n", i, rpack_ruint24(rp, NULL, 0));
	RPACK_REWIND(rp);
	for (i = 0; i < RPACK_LEN(rp); i++)
		printf("\t%d) %x\n", i, rpack_ruint24(rp, NULL, 0));
	RPACK_REWIND(rp);

	printf("test rpack_uint32::\n");
	for (i = 0; i < RPACK_LEN(rp); i++)
		if (i == 4)
			printf("\t%d) %x\n", i, rpack_uint32(rp, (uint32_t*) &n32, 0));
		else
			printf("\t%d) %x\n", i, rpack_uint32(rp, NULL, 0));
	RPACK_REWIND(rp);
	for (i = 0; i < RPACK_LEN(rp); i++)
		printf("\t%d) %x\n", i, rpack_uint32(rp, NULL, 0));
	RPACK_REWIND(rp);

	printf("test rpack_ruint32::\n");
	for (i = 0; i < RPACK_LEN(rp); i++)
		if (i == 4)
			printf("\t%d) %x\n", i, rpack_ruint32(rp, (uint32_t*) &n32, 0));
		else
			printf("\t%d) %x\n", i, rpack_ruint32(rp, NULL, 0));
	RPACK_REWIND(rp);
	for (i = 0; i < RPACK_LEN(rp); i++)
		printf("\t%d) %x\n", i, rpack_ruint32(rp, NULL, 0));
	RPACK_REWIND(rp);

	printf("test rpack_uint64::\n");
	for (i = 0; i < RPACK_LEN(rp); i++)
		if (i == 4)
			printf("\t%d) %lx\n", i, rpack_uint64(rp, (uint64_t*) &n64, 0));
		else
			printf("\t%d) %lx\n", i, rpack_uint64(rp, NULL, 0));
	RPACK_REWIND(rp);
	for (i = 0; i < RPACK_LEN(rp); i++)
		printf("\t%d) %lx\n", i, rpack_uint64(rp, NULL, 0));
	RPACK_REWIND(rp);

	printf("test rpack_ruint64::\n");
	for (i = 0; i < RPACK_LEN(rp); i++)
		if (i == 4)
			printf("\t%d) %lx\n", i, rpack_ruint64(rp, (uint64_t*) &n64, 0));
		else
			printf("\t%d) %lx\n", i, rpack_ruint64(rp, NULL, 0));
	RPACK_REWIND(rp);
	for (i = 0; i < RPACK_LEN(rp); i++)
		printf("\t%d) %lx\n", i, rpack_ruint64(rp, NULL, 0));
	RPACK_REWIND(rp);

	rpack_destroy(&rp);
	printf("destroy packet %p\n", rp);
	return 0;
}

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>