NXEngine v1.0.0.6
[NXEngine.git] / siflib / sectStringArray.cpp
blob831403e398bd7fc58d4c8e1394b7777e6537c7d0
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
6 #include "../common/DBuffer.h"
7 #include "../common/DString.h"
8 #include "../common/bufio.h"
9 #include "../common/StringList.h"
11 #include "sectStringArray.h"
12 #include "sectStringArray.fdh"
15 // decode the raw section data into the given StringList object
16 bool SIFStringArraySect::Decode(const uint8_t *data, int datalen, StringList *out)
18 const uint8_t *data_end = data + (datalen - 1);
19 int i, nstrings;
21 nstrings = read_U16(&data, data_end);
22 for(i=0;i<nstrings;i++)
24 if (data > data_end)
26 staterr("SIFStringArraySect::Decode: section corrupt: overran end of data");
27 return 1;
30 DString string;
31 ReadPascalString(&data, data_end, &string);
33 out->AddString(string.String());
36 return 0;
41 uint8_t *SIFStringArraySect::Encode(StringList *strings, int *datalen_out)
43 DBuffer buf;
45 if (strings->CountItems() > 65535)
47 staterr("SIFStringArraySect::Encode: too many strings in list");
48 return NULL;
51 buf.Append16(strings->CountItems());
52 for(int i=0;;i++)
54 const char *str = strings->StringAt(i);
55 if (!str) break;
57 WritePascalString(str, &buf);
60 if (datalen_out) *datalen_out = buf.Length();
61 return buf.TakeData();
65 void c------------------------------() {}
68 void SIFStringArraySect::ReadPascalString(const uint8_t **data, const uint8_t *data_end, DString *out)
70 int len = read_U8(data, data_end);
71 if (len == 255) len = read_U16(data, data_end);
73 out->Clear();
74 for(int i=0;i<len;i++)
76 out->AppendChar(read_U8(data, data_end));
80 void SIFStringArraySect::WritePascalString(const char *str, DBuffer *out)
82 int len = strlen(str);
83 if (len > 65535) len = 65535;
85 if (len < 255)
87 out->Append8(len);
89 else
91 out->Append8(0xff);
92 out->Append16(len);
95 for(int j=0;j<len;j++)
96 out->Append8(str[j]);