File: util.C

package info (click to toggle)
dxpc 3.8.0-5
  • links: PTS
  • area: non-free
  • in suites: woody
  • size: 672 kB
  • ctags: 780
  • sloc: cpp: 10,145; sh: 173; makefile: 87
file content (153 lines) | stat: -rw-r--r-- 2,716 bytes parent folder | download | duplicates (2)
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
#include <iostream.h>
#include <stdlib.h>
#include <unistd.h>
#include "constants.H"
#include "util.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 << "Copyright (c) 1995,1996 Brian Pane" << endl <<
    "Copyright (c) 1996,1997 Zachary Vonler" << endl 
     << "3.8.0 released by Kevin Vigor" << 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 *const *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;
  while (bytesWritten < length)
  {
    int result =::write(fd, data + bytesWritten,
			length - bytesWritten);
    if (result <= 0)
      return -1;
    bytesWritten += result;
  }
  return length;
}