Niotso  git revision 558726a9f13d7c3423a683dd2f4323589b66c310
The New Implementation of The Sims Online
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
iffparser.h
Go to the documentation of this file.
1 /*
2  FileHandler - General-purpose file handling library for Niotso
3  iffparser.h - Copyright (c) 2012 Niotso Project <http://niotso.org/>
4  Author(s): Fatbag <X-Fi6@phppoll.org>
5 
6  Permission to use, copy, modify, and/or distribute this software for any
7  purpose with or without fee is hereby granted, provided that the above
8  copyright notice and this permission notice appear in all copies.
9 
10  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18 
19 #include "iff.h"
20 
21 typedef struct {
22  const uint8_t * Buffer;
23  size_t Size;
24  const uint8_t * StartPos;
25  size_t TotalSize;
26  uint8_t Endian; /* 0 = little, 1 = big */
27 } bytestream;
28 
29 static __inline void set_bytestream(bytestream * b, const uint8_t * Buffer, size_t Size){
30  b->Buffer = Buffer;
31  b->Size = Size;
32  b->StartPos = Buffer;
33  b->TotalSize = Size;
34  b->Endian = 0;
35 }
36 
37 static __inline uint32_t read_uint32(bytestream * b){
38  unsigned value = (b->Endian == 0) ? read_uint32le(b->Buffer) : read_uint32be(b->Buffer);
39  b->Buffer += 4;
40  b->Size -= 4;
41  return value;
42 }
43 
44 static __inline uint16_t read_uint16(bytestream * b){
45  unsigned value = (b->Endian == 0) ? read_uint16le(b->Buffer) : read_uint16be(b->Buffer);
46  b->Buffer += 2;
47  b->Size -= 2;
48  return value;
49 }
50 
51 static __inline uint8_t read_uint8(bytestream * b){
52  uint8_t value = b->Buffer[0];
53  b->Buffer += 1;
54  b->Size -= 1;
55  return value;
56 }
57 
58 static __inline float read_float(bytestream *b){
59  union { float f; uint32_t v; } value;
60  value.v = read_uint32(b);
61  return value.f;
62 }
63 
64 static __inline int skipbytes(bytestream * b, size_t bytes){
65  if(b->Size < bytes) return 0;
66  b->Buffer += bytes; b->Size -= bytes;
67  return 1;
68 }
69 
70 static __inline int seekto(bytestream * b, size_t Position){
71  if(Position > b->TotalSize) return 0;
72  b->Buffer = b->StartPos + Position;
73  b->Size = b->TotalSize - Position;
74  return 1;
75 }
76 
77 static __inline size_t read_c_string(bytestream * b, char ** dest){
78  size_t length;
79  for(length=0; length != b->Size && b->Buffer[length]; length++);
80  if(length == b->Size) return 0;
81 
82  if(length != 0){
83  *dest = malloc(length+1);
84  if(*dest == NULL) return 0;
85  strcpy(*dest, (char*) b->Buffer);
86  }
87 
88  b->Buffer += length + 1;
89  b->Size -= length + 1;
90  return length + 1;
91 }
92 
93 static __inline size_t read_pascal_string(bytestream * b, char ** dest){
94  size_t length;
95  if(!b->Size) return 0;
96  length = b->Buffer[0];
97  if(length >= b->Size) return 0;
98 
99  if(length > 0){
100  *dest = malloc(length+1);
101  if(*dest == NULL) return 0;
102  memcpy(*dest, b->Buffer+1, length);
103  (*dest)[length] = 0x00;
104  }
105 
106  b->Buffer += 1 + length;
107  b->Size -= 1 + length;
108  return 1 + length;
109 }
110 
111 static __inline size_t read_pascal2_string(bytestream * b, char ** dest){
112  size_t length;
113  int countbytes = 1;
114  if(!b->Size) return 0;
115  length = b->Buffer[0];
116 
117  if(length > 127){
118  /* 2-byte length */
119  if(b->Size == 1) return 0;
120  length = (length&127) | (b->Buffer[1]<<7);
121  countbytes++;
122  }
123 
124  if(countbytes+length > b->Size) return 0;
125 
126  if(length != 0){
127  *dest = malloc(length+1);
128  if(*dest == NULL) return 0;
129  memcpy(*dest, b->Buffer+countbytes, length);
130  (*dest)[length] = 0x00;
131  }
132 
133  b->Buffer += countbytes + length;
134  b->Size -= countbytes + length;
135  return countbytes + length;
136 }
137 
138 static __inline size_t skip_padding(bytestream * b){
139  size_t padding = 0;
140  while(b->Size && b->Buffer[0] == 0xA3){
141  padding++;
142  b->Buffer++;
143  b->Size--;
144  }
145  return padding;
146 }