jimmyconti / nettopologysuite

Automatically exported from code.google.com/p/nettopologysuite
0 stars 0 forks source link

DbaseFileHeader encoding bug #164

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
// read the field name              
char[] buffer = new char[11];
buffer = reader.ReadChars(11);
string name = new string(buffer);
int nullPoint = name.IndexOf((char)0);
if (nullPoint != -1)
   name = name.Substring(0, nullPoint);
_fieldDescriptions[i].Name = name;

The field Name is 11 bytes, not 11 chars.

It's should read the field name below this:

byte[] buffer = new byte[11];
buffer = reader.ReadBytes(11);
name = _encoding.GetString(buffer);
int nullPoint = name.IndexOf((char)0);
if (nullPoint != -1)
   name = name.Substring(0, nullPoint);
_fieldDescriptions[i].Name = name;

Original issue reported on code.google.com by roc....@gmail.com on 15 Nov 2013 at 1:02

GoogleCodeExporter commented 9 years ago
I don't know wry, but I was sure that a char is an unsignet 8-bit value type, 
exactly like a byte... but looks that isn't the case: 
http://msdn.microsoft.com/en-us/library/7sx7t66b(v=vs.120).aspx

So I think that maybe reading 11 chars can generate an "overflow", i.e: reading 
outsite the extent of "name" field. 
I'm right?

Original comment by diegogu...@gmail.com on 15 Nov 2013 at 4:04

GoogleCodeExporter commented 9 years ago
fixed with changeset 
1081:https://code.google.com/p/nettopologysuite/source/detail?r=1081

Original comment by diegogu...@gmail.com on 15 Nov 2013 at 4:15

GoogleCodeExporter commented 9 years ago
thanks for reporting this

Original comment by diegogu...@gmail.com on 15 Nov 2013 at 4:16

GoogleCodeExporter commented 9 years ago
This is a same bug whih the WriteHeader method

                // write the field name
                for (int j = 0; j < FieldNameMaxLength; j++)
                {
                    if (_fieldDescriptions[i].Name.Length > j)
                        writer.Write((byte)_fieldDescriptions[i].Name[j]);
                    else writer.Write((byte)0);
                }

the field name bytes value should read by the _encoding member:
byte[] buffer = _encoding.GetBytes(_fieldDescriptions[i].Name)

then check the length of the buffer

Original comment by roc....@gmail.com on 16 Nov 2013 at 1:00

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
char store length depends on the encoding..

Original comment by roc....@gmail.com on 16 Nov 2013 at 2:05

GoogleCodeExporter commented 9 years ago
please take a look at commit 1082: 
https://code.google.com/p/nettopologysuite/source/detail?r=1082

Original comment by diegogu...@gmail.com on 17 Nov 2013 at 10:43

GoogleCodeExporter commented 9 years ago
There are still a bug, the field name is just 11 bytes, need check that.

Original comment by roc....@gmail.com on 17 Nov 2013 at 11:15

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
now should be ok https://code.google.com/p/nettopologysuite/source/detail?r=1085

Original comment by diegogu...@gmail.com on 17 Nov 2013 at 1:33

GoogleCodeExporter commented 9 years ago
i.e

                // write the field name
                string fieldName = _fieldDescriptions[i].Name;
                while (_encoding.GetByteCount(fieldName) > 11)
                    fieldName = fieldName.Substring(0, fieldName.Length - 1);
                writer.Write(_encoding.GetBytes(fieldName));

Original comment by roc....@gmail.com on 18 Nov 2013 at 11:36

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
Sorry, It's should like:

                // write the field name
                string fieldName = _fieldDescriptions[i].Name;

                // make sure the field name data length is not bigger than 11
                while (_encoding.GetByteCount(fieldName) > 11)
                    fieldName = fieldName.Substring(0, fieldName.Length - 1);

                byte[] buffer = new byte[11];
                byte[] nameData = _encoding.GetBytes(fieldName);
                Array.Copy(nameData, buffer, nameData.Length);
                writer.Write(buffer);

Original comment by roc....@gmail.com on 18 Nov 2013 at 12:23

GoogleCodeExporter commented 9 years ago
hope the code it's right now :)
https://code.google.com/p/nettopologysuite/source/detail?r=1086

Original comment by diegogu...@gmail.com on 18 Nov 2013 at 1:26