* FileSystemInfo.cs: corrected COM visibility of UTC properties
[mono.git] / mcs / class / Npgsql / Npgsql / PGUtil.cs
1 // created on 1/6/2002 at 22:27
2
3 // Npgsql.PGUtil.cs
4 //
5 // Author:
6 //      Francisco Jr. (fxjrlists@yahoo.com.br)
7 //
8 //      Copyright (C) 2002 The Npgsql Development Team
9 //      npgsql-general@gborg.postgresql.org
10 //      http://gborg.postgresql.org/project/npgsql/projdisplay.php
11 //
12 // This library is free software; you can redistribute it and/or
13 // modify it under the terms of the GNU Lesser General Public
14 // License as published by the Free Software Foundation; either
15 // version 2.1 of the License, or (at your option) any later version.
16 //
17 // This library is distributed in the hope that it will be useful,
18 // but WITHOUT ANY WARRANTY; without even the implied warranty of
19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20 // Lesser General Public License for more details.
21 //
22 // You should have received a copy of the GNU Lesser General Public
23 // License along with this library; if not, write to the Free Software
24 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
25
26
27 using System;
28 using System.IO;
29 using System.Text;
30 using System.Net.Sockets;
31 using System.Net;
32 using System.Resources;
33
34 namespace Npgsql
35 {
36
37
38     internal struct ProtocolVersion
39     {
40         public const Int32 Version2 = 131072;
41         public const Int32 Version3 = 196608;
42     }
43
44 internal enum FormatCode:
45     short
46     {
47         Text = 0,
48         Binary = 1
49     }
50
51
52     ///<summary>
53     /// This class provides many util methods to handle
54     /// reading and writing of PostgreSQL protocol messages.
55     /// </summary>
56     /// [FIXME] Does this name fully represent the class responsability?
57     /// Should it be abstract or with a private constructor to prevent
58     /// creating instances?
59
60     //
61     internal sealed class PGUtil
62     {
63
64         // Logging related values
65         private static readonly String CLASSNAME = "PGUtil";
66         private static ResourceManager resman = new ResourceManager(typeof(PGUtil));
67
68         ///<summary>
69         /// This method gets a C NULL terminated string from the network stream.
70         /// It keeps reading a byte in each time until a NULL byte is returned.
71         /// It returns the resultant string of bytes read.
72         /// This string is sent from backend.
73         /// </summary>
74
75         public static String ReadString(Stream network_stream, Encoding encoding)
76         {
77             NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "ReadString");
78
79             // [FIXME] Is 512 enough?
80             Byte[] buffer = new Byte[512];
81             Byte b;
82             Int16 counter = 0;
83
84
85             // [FIXME] Is this cast always safe?
86             b = (Byte)network_stream.ReadByte();
87             while(b != 0)
88             {
89                 buffer[counter] = b;
90                 counter++;
91                 b = (Byte)network_stream.ReadByte();
92             }
93             String string_read = encoding.GetString(buffer, 0, counter);
94             NpgsqlEventLog.LogMsg(resman, "Log_StringRead", LogLevel.Debug, string_read);
95             return string_read;
96         }
97
98         ///<summary>
99         /// This method writes a C NULL terminated string to the network stream.
100         /// It appends a NULL terminator to the end of the String.
101         /// </summary>
102
103         public static void WriteString(String the_string, Stream network_stream, Encoding encoding)
104         {
105             NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "WriteString");
106
107             network_stream.Write(encoding.GetBytes(the_string + '\x00') , 0, encoding.GetByteCount(the_string) + 1);
108         }
109
110         ///<summary>
111         /// This method writes a C NULL terminated string limited in length to the
112         /// backend server.
113         /// It pads the string with null bytes to the size specified.
114         /// </summary>
115
116         public static void WriteLimString(String the_string, Int32 n, Stream network_stream, Encoding encoding)
117         {
118             NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "WriteLimString");
119
120             // [FIXME] Parameters should be validated. And what about strings
121             // larger than or equal to n?
122
123             // Pad the string to the specified value.
124             String string_padded = the_string.PadRight(n, '\x00');
125
126             network_stream.Write(encoding.GetBytes(string_padded), 0, n);
127         }
128
129         public static void CheckedStreamRead(Stream stream, Byte[] buffer, Int32 offset, Int32 size)
130         {
131             Int32 bytes_from_stream = 0;
132             Int32 total_bytes_read = 0;
133             do
134             {
135                 bytes_from_stream = stream.Read(buffer, offset + total_bytes_read, size);
136                 total_bytes_read += bytes_from_stream;
137                 size -= bytes_from_stream;
138             }
139             while(size > 0);
140
141         }
142
143
144         public static void WriteInt32(Stream stream, Int32 value)
145         {
146             stream.Write(BitConverter.GetBytes(IPAddress.HostToNetworkOrder(value)), 0, 4);
147         }
148
149         public static Int32 ReadInt32(Stream stream, Byte[] buffer)
150         {
151             CheckedStreamRead(stream, buffer, 0, 4);
152             return IPAddress.NetworkToHostOrder(BitConverter.ToInt32(buffer, 0));
153
154         }
155
156         public static void WriteInt16(Stream stream, Int16 value)
157         {
158             stream.Write(BitConverter.GetBytes(IPAddress.HostToNetworkOrder(value)), 0, 2);
159         }
160
161         public static Int16 ReadInt16(Stream stream, Byte[] buffer)
162         {
163             CheckedStreamRead(stream, buffer, 0, 2);
164             return IPAddress.NetworkToHostOrder(BitConverter.ToInt16(buffer, 0));
165
166         }
167
168
169         /*public static void WriteQueryToStream( String query, Stream stream, Encoding encoding )
170         {
171                 NpgsqlEventLog.LogMsg( CLASSNAME + query, LogLevel.Debug  );
172                 // Send the query to server.
173                 // Write the byte 'Q' to identify a query message.
174                 stream.WriteByte((Byte)'Q');
175                 
176                 // Write the query. In this case it is the CommandText text.
177                 // It is a string terminated by a C NULL character.
178                 stream.Write(encoding.GetBytes(query + '\x00') , 0, query.Length + 1);
179                 
180                 // Send bytes.
181                 stream.Flush();
182                 
183         }
184
185         public static Int32 ProtocolVersionMajor(Int32 protocolVersion)
186         {
187           return (protocolVersion >> 16) & 0xffff;
188         }
189
190         public static Int32 ProtocolVersionMinor(Int32 protocolVersion)
191         {
192           return protocolVersion & 0xffff;
193         }*/
194
195
196
197     }
198 }