source: abuse/trunk/src/imlib/packet.cpp @ 129

Last change on this file since 129 was 129, checked in by Sam Hocevar, 14 years ago
  • Get rid of jmalloc and replace it with standard malloc. Modern operating systems certainly perform a lot better than this custom implementation, and we have superior tools (eg. valgrind) to debug and profile memory usage without interfering with the code itself.
File size: 1.6 KB
Line 
1/*
2 *  Abuse - dark 2D side-scrolling platform game
3 *  Copyright (c) 1995 Crack dot Com
4 *
5 *  This software was released into the Public Domain. As with most public
6 *  domain software, no warranty is made or implied by Crack dot Com or
7 *  Jonathan Clark.
8 */
9
10#include "config.h"
11
12#include <stdlib.h>
13#include <string.h>
14
15#include "packet.hpp"
16
17int packet::advance(int32_t offset)
18{
19  ro+=offset;
20  return ro<=rend;
21}
22
23void packet::write_uint32(uint32_t x)
24{
25  x=lltl(x);
26  write((uint8_t *)&x,4);
27}
28
29void packet::write_uint16(uint16_t x)
30{
31  x=lstl(x);
32  write((uint8_t *)&x,2);
33}
34
35void packet::write_uint8(uint8_t x)
36{
37  write(&x,1);
38}
39
40packet::~packet()
41{ free(buf); }
42
43packet::packet(int prefix_size)
44{
45  pre_size=prefix_size;
46
47  buf_size=1000;
48  buf=(uint8_t *)malloc(buf_size);
49  reset();
50}
51
52void packet::get_string(char *st, int len)
53{
54  char *b=(char *)buf+ro;
55  while (len>1 && !eop() && *b)
56  {
57    *st=*b;
58    st++;
59    ro++;
60    b++;
61    len--;
62  }
63  if (*b==0) ro++;
64  *st=0;
65}
66
67void packet::reset()
68{ ro=wo=rend=pre_size; }
69
70void packet::make_bigger(int max)
71{
72  if (buf_size<max)
73  {
74    buf_size=max;
75    buf=(uint8_t *)realloc(buf,max);
76  }
77}
78
79int packet::read(uint8_t *buffer, int size)
80{
81  if (size>rend-ro)
82    size=rend-ro;
83
84  if (size>0)
85  {
86    memcpy(buffer,buf+ro,size);
87    ro+=size;
88    return size;
89  } else return 0;
90}
91
92
93int packet::write(uint8_t *buffer, int size)
94{
95  if (size>buf_size-wo)
96    make_bigger(wo+size);
97  if (size>0)
98  {
99    memcpy(buf+wo,buffer,size);
100    wo+=size;
101    rend=wo;
102    return size;
103  }
104  return 0;
105}
106
107
108
109void packet::insert_into(packet &pk)
110{
111  pk.write(buf+pre_size,rend-pre_size);
112}
113
114
115
Note: See TracBrowser for help on using the repository browser.