Back to index

lightdm  1.3.2
x-common.c
Go to the documentation of this file.
00001 #include <string.h>
00002 
00003 #include "x-common.h"
00004 
00005 gsize
00006 pad (gsize length)
00007 {
00008     if (length % 4 == 0)
00009         return 0;
00010     return 4 - length % 4;
00011 }
00012 
00013 void
00014 read_padding (gsize length, gsize *offset)
00015 {
00016     *offset += length;
00017 }
00018 
00019 guint8
00020 read_card8 (const guint8 *buffer, gsize buffer_length, gsize *offset)
00021 {
00022     if (*offset >= buffer_length)
00023         return 0;
00024     (*offset)++;
00025     return buffer[*offset - 1];
00026 }
00027 
00028 guint16
00029 read_card16 (const guint8 *buffer, gsize buffer_length, guint8 byte_order, gsize *offset)
00030 {
00031     guint8 a, b;
00032 
00033     a = read_card8 (buffer, buffer_length, offset);
00034     b = read_card8 (buffer, buffer_length, offset);
00035     if (byte_order == X_BYTE_ORDER_MSB)
00036         return a << 8 | b;
00037     else
00038         return b << 8 | a;
00039 }
00040 
00041 guint32
00042 read_card32 (const guint8 *buffer, gsize buffer_length, guint8 byte_order, gsize *offset)
00043 {
00044     guint8 a, b, c, d;
00045 
00046     a = read_card8 (buffer, buffer_length, offset);
00047     b = read_card8 (buffer, buffer_length, offset);
00048     c = read_card8 (buffer, buffer_length, offset);
00049     d = read_card8 (buffer, buffer_length, offset);
00050     if (byte_order == X_BYTE_ORDER_MSB)
00051         return a << 24 | b << 16 | c << 8 | d;
00052     else
00053         return d << 24 | c << 16 | b << 8 | a;
00054 }
00055 
00056 guint8 *
00057 read_string8 (const guint8 *buffer, gsize buffer_length, gsize string_length, gsize *offset)
00058 {
00059     guint8 *string;
00060     int i;
00061 
00062     string = g_malloc (string_length + 1);
00063     for (i = 0; i < string_length; i++)
00064         string[i] = read_card8 (buffer, buffer_length, offset);
00065     string[i] = '\0';
00066     return string;
00067 }
00068 
00069 gchar *
00070 read_string (const guint8 *buffer, gsize buffer_length, gsize string_length, gsize *offset)
00071 {
00072     return (gchar *) read_string8 (buffer, buffer_length, string_length, offset);
00073 }
00074 
00075 gchar *
00076 read_padded_string (const guint8 *buffer, gsize buffer_length, gsize string_length, gsize *offset)
00077 {
00078     guint8 *value;
00079     value = read_string8 (buffer, buffer_length, string_length, offset);
00080     read_padding (pad (string_length), offset);
00081     return (gchar *) value;
00082 }
00083 
00084 void
00085 write_card8 (guint8 *buffer, gsize buffer_length, guint8 value, gsize *offset)
00086 {
00087     if (*offset >= buffer_length)
00088         return;
00089     buffer[*offset] = value;
00090     (*offset)++;
00091 }
00092 
00093 void
00094 write_padding (guint8 *buffer, gsize buffer_length, gsize length, gsize *offset)
00095 {
00096     gsize i;
00097     for (i = 0; i < length; i++)
00098         write_card8 (buffer, buffer_length, 0, offset);
00099 }
00100 
00101 void
00102 write_card16 (guint8 *buffer, gsize buffer_length, guint8 byte_order, guint16 value, gsize *offset)
00103 {
00104     if (byte_order == X_BYTE_ORDER_MSB)
00105     {
00106         write_card8 (buffer, buffer_length, value >> 8, offset);
00107         write_card8 (buffer, buffer_length, value & 0xFF, offset);
00108     }
00109     else
00110     {
00111         write_card8 (buffer, buffer_length, value & 0xFF, offset);
00112         write_card8 (buffer, buffer_length, value >> 8, offset);
00113     }
00114 }
00115 
00116 void
00117 write_card32 (guint8 *buffer, gsize buffer_length, guint8 byte_order, guint32 value, gsize *offset)
00118 {
00119     if (byte_order == X_BYTE_ORDER_MSB)
00120     {
00121         write_card8 (buffer, buffer_length, value >> 24, offset);
00122         write_card8 (buffer, buffer_length, (value >> 16) & 0xFF, offset);
00123         write_card8 (buffer, buffer_length, (value >> 8) & 0xFF, offset);
00124         write_card8 (buffer, buffer_length, value & 0xFF, offset);
00125     }
00126     else
00127     {
00128         write_card8 (buffer, buffer_length, value & 0xFF, offset);
00129         write_card8 (buffer, buffer_length, (value >> 8) & 0xFF, offset);
00130         write_card8 (buffer, buffer_length, (value >> 16) & 0xFF, offset);
00131         write_card8 (buffer, buffer_length, value >> 24, offset);
00132     }
00133 }
00134 
00135 void
00136 write_string8 (guint8 *buffer, gsize buffer_length, const guint8 *value, gsize value_length, gsize *offset)
00137 {
00138     gsize i;
00139     for (i = 0; i < value_length; i++)
00140         write_card8 (buffer, buffer_length, value[i], offset);
00141 }
00142 
00143 gsize
00144 padded_string_length (const gchar *value)
00145 {
00146     return (strlen (value) + pad (strlen (value))) / 4;
00147 }
00148 
00149 void
00150 write_string (guint8 *buffer, gsize buffer_length, const gchar *value, gsize *offset)
00151 {
00152     write_string8 (buffer, buffer_length, (guint8 *) value, strlen (value), offset);
00153 }
00154 
00155 void
00156 write_padded_string (guint8 *buffer, gsize buffer_length, const gchar *value, gsize *offset)
00157 {
00158     write_string8 (buffer, buffer_length, (guint8 *) value, strlen (value), offset);
00159     write_padding (buffer, buffer_length, pad (strlen (value)), offset);
00160 }