]> git.jsancho.org Git - lugaru.git/blob - Source/pack_private.c
480e2aee39fcf5f6467634602fb15739838911a5
[lugaru.git] / Source / pack_private.c
1 #include "pack_private.h"
2
3 void BinIOPack(void *context, int type, int byte_order, int count)
4 {
5     struct BinIOPackContext *ctx = (struct BinIOPackContext*)context;
6     if (count == -1)
7     {
8         switch (type)
9         {
10             case BinIO_TYPE_IGNORE_BYTE:
11                 {
12                     ctx->buffer += 1;
13                 }
14                 break;
15             case BinIO_TYPE_BYTE:
16                 {
17                     uint8_t value = va_arg(ctx->args, int);
18                     BinIOConvert1(BinIO_HOST_BYTE_ORDER, byte_order, (const uint8_t *)&value, ctx->buffer, 1);
19                     ctx->buffer += 1;
20                 }
21                 break;
22             case BinIO_TYPE_INT16:
23                 {
24                     uint16_t value = va_arg(ctx->args, int);
25                     BinIOConvert2(BinIO_HOST_BYTE_ORDER, byte_order, (const uint8_t *)&value, ctx->buffer, 1);
26                     ctx->buffer += 2;
27                 }
28                 break;
29             case BinIO_TYPE_INT32:
30                 {
31                     int value = va_arg(ctx->args, int);
32                     BinIOConvert4(BinIO_HOST_BYTE_ORDER, byte_order, (const uint8_t *)&value, ctx->buffer, 1);
33                     ctx->buffer += 4;
34                 }
35                 break;
36             case BinIO_TYPE_INT64:
37                 {
38                     uint64_t value = va_arg(ctx->args, uint64_t);
39                     BinIOConvert8(BinIO_HOST_BYTE_ORDER, byte_order, (const uint8_t *)&value, ctx->buffer, 1);
40                     ctx->buffer += 8;
41                 }
42                 break;
43             case BinIO_TYPE_FLOAT32:
44                 {
45                     float32_t value = (float32_t)va_arg(ctx->args, double);
46                     BinIOConvert4(BinIO_HOST_BYTE_ORDER, byte_order, (const uint8_t *)&value, ctx->buffer, 1);
47                     ctx->buffer += 4;
48                 }
49                 break;
50             case BinIO_TYPE_FLOAT64:
51                 {
52                     float64_t value = va_arg(ctx->args, float64_t);
53                     BinIOConvert8(BinIO_HOST_BYTE_ORDER, byte_order, (const uint8_t *)&value, ctx->buffer, 1);
54                     ctx->buffer += 8;
55                 }
56                 break;
57         }
58     }
59     else
60     {
61         switch (type)
62         {
63             case BinIO_TYPE_IGNORE_BYTE:                                                                                                     ctx->buffer += 1 * count; break;
64             case BinIO_TYPE_BYTE:        BinIOConvert1(BinIO_HOST_BYTE_ORDER, byte_order, va_arg(ctx->args, uint8_t *), ctx->buffer, count); ctx->buffer += 1 * count; break;
65             case BinIO_TYPE_INT16:       BinIOConvert2(BinIO_HOST_BYTE_ORDER, byte_order, va_arg(ctx->args, uint8_t *), ctx->buffer, count); ctx->buffer += 2 * count; break;
66             case BinIO_TYPE_INT32:       BinIOConvert4(BinIO_HOST_BYTE_ORDER, byte_order, va_arg(ctx->args, uint8_t *), ctx->buffer, count); ctx->buffer += 4 * count; break;
67             case BinIO_TYPE_INT64:       BinIOConvert8(BinIO_HOST_BYTE_ORDER, byte_order, va_arg(ctx->args, uint8_t *), ctx->buffer, count); ctx->buffer += 8 * count; break;
68             case BinIO_TYPE_FLOAT32:     BinIOConvert4(BinIO_HOST_BYTE_ORDER, byte_order, va_arg(ctx->args, uint8_t *), ctx->buffer, count); ctx->buffer += 4 * count; break;
69             case BinIO_TYPE_FLOAT64:     BinIOConvert8(BinIO_HOST_BYTE_ORDER, byte_order, va_arg(ctx->args, uint8_t *), ctx->buffer, count); ctx->buffer += 8 * count; break;
70         }
71     }
72 }