pqcomprim.c 3.94 KB
Newer Older
1 2 3
#include <stdlib.h>
#include <stdio.h>

4 5 6
#include "postgres.h"
#include "libpq/pqcomm.h"

7 8 9 10
#ifdef        HAVE_ENDIAN_H
#  include    <endian.h>
#endif

11 12 13 14 15

/* --------------------------------------------------------------------- */
/* Is the other way around than system ntoh/hton, so we roll our own
	here */
	
16 17
#ifndef		BYTE_ORDER
#error BYTE_ORDER must be defined as LITTLE_ENDIAN, BIG_ENDIAN or PDP_ENDIAN
18 19
#endif

20 21 22 23 24 25 26
#if BYTE_ORDER == LITTLE_ENDIAN
#  define ntoh_s(n) n
#  define ntoh_l(n) n
#  define hton_s(n) n
#  define hton_l(n) n
#else	/* BYTE_ORDER != LITTLE_ENDIAN */
#  if BYTE_ORDER == BIG_ENDIAN
27
#    define ntoh_s(n) (u_short)(((u_char *) &n)[0] << 8 | ((u_char *) &n)[1])
28 29
#    define ntoh_l(n) (u_long)(((u_char *)&n)[0] << 24 | \
							((u_char *)&n)[1] << 16 | \
30
      	             	   ((u_char *)&n)[2] << 8 | ((u_char *)&n)[3])
31
#    define hton_s(n) (u_short)(((u_char *) &n)[2] << 8 | ((u_char *) &n)[3])
32 33 34 35 36 37 38 39 40 41 42 43
#    define hton_l(n) (ntoh_l(n))
#  else	/* BYTE_ORDER != BIG_ENDIAN */
#    if BYTE_ORDER == PDP_ENDIAN
#      #error PDP_ENDIAN macros not written yet
#    else	/* BYTE_ORDER !=  anything known */
#      #error BYTE_ORDER not defined as anything understood
#    endif	/* BYTE_ORDER == PDP_ENDIAN */
#  endif	/* BYTE_ORDER == BIG_ENDIAN */
#endif		/* BYTE_ORDER == LITTLE_ENDIAN */

/* --------------------------------------------------------------------- */
int pqPutShort(int integer, FILE *f)
44 45 46 47 48 49 50 51 52 53 54 55
    {
    int retval = 0;
    u_short n;
		
    n = hton_s(integer);
    if(fwrite(&n, sizeof(u_short), 1, f) != 1)
    	retval = EOF;
    
    return retval;
    }

/* --------------------------------------------------------------------- */
56
int pqPutLong(int integer, FILE *f)
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
    {
    int retval = 0;
    u_long n;
		
    n = hton_l(integer);
    if(fwrite(&n, sizeof(u_long), 1, f) != 1)
    	retval = EOF;
    
    return retval;
    }
    
/* --------------------------------------------------------------------- */
int pqGetShort(int *result, FILE *f)
    {
    int retval = 0;
    u_short n;

    if(fread(&n, sizeof(u_short), 1, f) != 1)
    	retval = EOF;
			
    *result = ntoh_s(n);
    return retval;
    }

/* --------------------------------------------------------------------- */
int pqGetLong(int *result, FILE *f)
    {
    int retval = 0;
    u_long n;
		
    if(fread(&n, sizeof(u_long), 1, f) != 1)
    	retval = EOF;
			
    *result = ntoh_l(n);
    return retval;
    }

/* --------------------------------------------------------------------- */
/* pqGetNBytes: Read a chunk of exactly len bytes in buffer s.
	Return 0 if ok.
*/
98
int pqGetNBytes(char *s, size_t len, FILE *f)
99 100 101 102 103 104 105 106 107 108 109 110 111 112
	{
	int cnt;

	if (f == NULL)
		return EOF;
  
	cnt = fread(s, 1, len, f);
	s[cnt] = '\0';
   /* mjl: actually needs up to len+1 bytes, is this okay? XXX */

	return (cnt == len) ? 0 : EOF;
	}

/* --------------------------------------------------------------------- */
113
int pqPutNBytes(const char *s, size_t len, FILE *f)
114 115 116 117 118 119 120 121 122 123 124
	{    
	if (f == NULL)
		return 0;

   if(fwrite(s, 1, len, f) != len)
	    return EOF;

	return 0;
	}
	
/* --------------------------------------------------------------------- */
125
int pqGetString(char *s, size_t len, FILE *f)
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
	{
	int c;

	if (f == NULL)
	return EOF;
  
	while (len-- && (c = getc(f)) != EOF && c)
		*s++ = c;
	*s = '\0';
   /* mjl: actually needs up to len+1 bytes, is this okay? XXX */

	return 0;
	}

/* --------------------------------------------------------------------- */
int pqPutString(const char *s, FILE *f)
	{
	if (f == NULL)
		return 0;
  
	if (fputs(s, f) == EOF)
		return EOF;

	fputc('\0', f); /* important to send an ending \0 since backend expects it */
	fflush(f);

	return 0;
	}

/* --------------------------------------------------------------------- */
int pqGetByte(FILE *f)
	{
	return getc(f);
	}
	
/* --------------------------------------------------------------------- */
162
int pqPutByte(int c, FILE *f)
163 164 165 166 167 168 169 170
	{
	if(!f)	return 0;
	
	return (putc(c, f) == c) ? 0 : EOF;
	}
	
/* --------------------------------------------------------------------- */