File: util.C

package info (click to toggle)
dxpc 3.9.2-4
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 1,164 kB
  • ctags: 1,252
  • sloc: cpp: 12,036; sh: 2,823; makefile: 77
file content (167 lines) | stat: -rw-r--r-- 3,314 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include "dxpcconf.h"
#include <stdlib.h>
#include <unistd.h>
#include "constants.H"
#include "util.H"

#include <stdio.h>

unsigned int GetUINT(unsigned const char *buffer, int bigEndian)
{
    unsigned int result;

    if (bigEndian)
    {
        result = *buffer;
        result <<= 8;
        result += buffer[1];
    }
    else
    {
        result = buffer[1];
        result <<= 8;
        result += *buffer;
    }
    return result;
}


unsigned int GetULONG(unsigned const char *buffer, int bigEndian)
{
    const unsigned char *next = (bigEndian ? buffer : buffer + 3);
    unsigned int result = 0;

    for (int i = 0; i < 4; i++)
    {
        result <<= 8;
        result += *next;
        if (bigEndian)
            next++;
        else
            next--;
    }
    return result;
}

void PutUINT(unsigned int value, unsigned char *buffer, int bigEndian)
{
    if (bigEndian)
    {
        buffer[1] = (unsigned char) (value & 0xff);
        value >>= 8;
        *buffer = (unsigned char) value;
    }
    else
    {
        *buffer = (unsigned char) (value & 0xff);
        value >>= 8;
        buffer[1] = (unsigned char) value;
    }
}


void PutULONG(unsigned int value, unsigned char *buffer, int bigEndian)
{
    if (bigEndian)
    {
        buffer += 3;
        for (int i = 4; i; i--)
        {
            *buffer-- = (unsigned char) (value & 0xff);
            value >>= 8;
        }
    }
    else
    {
        for (int i = 4; i; i--)
        {
            *buffer++ = (unsigned char) (value & 0xff);
            value >>= 8;
        }
    }
}


unsigned int RoundUp4(unsigned int x)
{
    unsigned int y = x / 4;

    y *= 4;
    if (y != x)
        y += 4;
    return y;
}

void PrintVersionInfo()
{
    COUT << "dxpc - Differential X Protocol Compressor - " <<
        "Version " << DXPC_VERSION_MAJOR << '.' << DXPC_VERSION_MINOR << '.'
        << DXPC_VERSION_PATCH;
    if (DXPC_VERSION_BETA != 0)
        COUT << "beta" << DXPC_VERSION_BETA;
    COUT << ENDL;
    COUT << getLicenseInfo() << ENDL;
}


void DumpMessage(const unsigned char *src, unsigned int numBytes)
{
    for (unsigned int i = 0; i < numBytes; i++)
        COUT << i << '\t' << (unsigned int) (src[i]) << ENDL;
}


const char *GetArg(int &argi, int argc, const char **argv)
{
    const char *nextArg = argv[argi] + 2;       // skip "-" and flag character

    if (*nextArg == 0)
    {
        if (argi + 1 == argc)
            return NULL;
        else
        {
            argi++;
            return argv[argi];
        }
    }
    else
        return nextArg;
}

int WriteAll(int fd, const unsigned char *data, unsigned int length)
{
    unsigned int bytesWritten = 0;

#undef SPEWIT
#ifdef SPEWIT
    unsigned i = 0;

    while (i < length)
    {
        unsigned rem = length - i;

        if (rem > 16)
        {
            rem = 16;
        }
        fprintf(stderr, "%04x: ", i % 16);
        for (unsigned j = 0; j < rem; j++)
        {
            fprintf(stderr, "%02x ", data[i++]);
        }
        fprintf(stderr, "\n");
    }
#endif

    while (bytesWritten < length)
    {
        int result = SOCKWRITE(fd, data + bytesWritten,
                               length - bytesWritten);

        if (result <= 0)
            return -1;
        bytesWritten += result;
    }
    return length;
}