source: abuse/tags/pd/macabuse/imlib/packet.c @ 49

Last change on this file since 49 was 49, checked in by Sam Hocevar, 11 years ago
  • Imported original public domain release, for future reference.
  • Property svn:keywords set to Id
File size: 1.5 KB
Line 
1#include "packet.hpp"
2#include "jmalloc.hpp"
3#include <stdlib.h>
4#include <string.h>
5
6
7int packet::advance(long offset)
8{
9  ro+=offset;
10  return ro<=rend;
11}
12
13void packet::write_long(ulong x)
14{
15  x=lltl(x);
16  write((uchar *)&x,4);
17}
18
19void packet::write_short(ushort x)
20{
21  x=lstl(x);
22  write((uchar *)&x,2);
23}
24
25void packet::write_byte(uchar x)
26{
27  write(&x,1);
28}
29
30packet::~packet()
31{ jfree(buf); }
32
33packet::packet(int prefix_size)
34{
35  pre_size=prefix_size;
36
37#ifdef MANAGE_MEM
38  int sp=alloc_space;
39  alloc_space=ALLOC_SPACE_STATIC;
40#endif
41
42  buf_size=1000;
43  buf=(uchar *)jmalloc(buf_size,"packet buffer");
44  reset();
45
46#ifdef MANAGE_MEM
47  alloc_space=sp;
48#endif
49}
50
51void packet::get_string(char *st, int len)
52{
53  char *b=(char *)buf+ro;
54  while (len>1 && !eop() && *b)
55  {
56    *st=*b;
57    st++;
58    ro++;
59    b++;
60    len--;
61  }
62  if (*b==0) ro++;
63  *st=0;
64}
65
66void packet::reset()
67{ ro=wo=rend=pre_size; }
68
69void packet::make_bigger(int max)
70{
71  if (buf_size<max)
72  {
73    buf_size=max;
74    buf=(uchar *)jrealloc(buf,max,"packet buffer");
75  }
76}
77
78int packet::read(uchar *buffer, int size)
79{
80  if (size>rend-ro)
81    size=rend-ro;
82
83  if (size>0)
84  {
85    memcpy(buffer,buf+ro,size);
86    ro+=size;
87    return size;
88  } else return 0;
89}
90
91
92int packet::write(uchar *buffer, int size)
93{
94  if (size>buf_size-wo)
95    make_bigger(wo+size);
96  if (size>0)
97  {
98    memcpy(buf+wo,buffer,size);
99    wo+=size;
100    rend=wo;
101    return size;
102  }
103  return 0;
104}
105
106
107
108void packet::insert_into(packet &pk)
109{
110  pk.write(buf+pre_size,rend-pre_size);
111}
112
113
114
Note: See TracBrowser for help on using the repository browser.