File: packet.c

package info (click to toggle)
abuse 2.00-12
  • links: PTS
  • area: main
  • in suites: slink
  • size: 12,708 kB
  • ctags: 15,389
  • sloc: ansic: 115,852; cpp: 6,792; lisp: 2,066; sh: 1,734; makefile: 1,601; asm: 264
file content (114 lines) | stat: -rw-r--r-- 1,529 bytes parent folder | download | duplicates (7)
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
#include "packet.hpp"
#include "jmalloc.hpp"
#include <stdlib.h>
#include <string.h>


int packet::advance(long offset)
{
  ro+=offset;
  return ro<=rend;
}

void packet::write_long(ulong x)
{
  x=lltl(x);
  write((uchar *)&x,4);
}

void packet::write_short(ushort x)
{
  x=lstl(x);
  write((uchar *)&x,2);
}

void packet::write_byte(uchar x)
{
  write(&x,1);
}

packet::~packet()
{ jfree(buf); }

packet::packet(int prefix_size) 
{ 
  pre_size=prefix_size;

#ifdef MANAGE_MEM
  int sp=alloc_space;
  alloc_space=ALLOC_SPACE_STATIC;
#endif

  buf_size=1000;
  buf=(uchar *)jmalloc(buf_size,"packet buffer");
  reset(); 

#ifdef MANAGE_MEM
  alloc_space=sp;
#endif
}

void packet::get_string(char *st, int len)
{
  char *b=(char *)buf+ro;
  while (len>1 && !eop() && *b)
  {
    *st=*b; 
    st++;
    ro++;
    b++;
    len--;
  }
  if (*b==0) ro++;
  *st=0;
}

void packet::reset() 
{ ro=wo=rend=pre_size; }

void packet::make_bigger(int max)
{
  if (buf_size<max)
  {
    buf_size=max;
    buf=(uchar *)jrealloc(buf,max,"packet buffer"); 
  }
}

int packet::read(uchar *buffer, int size)
{
  if (size>rend-ro)
    size=rend-ro;

  if (size>0)
  {
    memcpy(buffer,buf+ro,size);
    ro+=size;
    return size;
  } else return 0;
}


int packet::write(uchar *buffer, int size)
{
  if (size>buf_size-wo)
    make_bigger(wo+size);
  if (size>0)
  {
    memcpy(buf+wo,buffer,size);
    wo+=size;
    rend=wo;
    return size;
  }
  return 0;
}



void packet::insert_into(packet &pk)
{
  pk.write(buf+pre_size,rend-pre_size);
}