<commit_before>// TODO I need to fix some int sizes
#include <obliv_common.h>
#include <obliv_bits.h>
#include <assert.h>
#include <errno.h>      // libgcrypt needs ENOMEM definition
#include <inttypes.h>
#include <stdio.h>      // for protoUseStdio()
#include <string.h>
#include <netinet/ip.h>
#include <netdb.h>
#include <pthread.h>
#include <unistd.h>
#include <gcrypt.h>

// Q: What's with all these casts to and from void* ?
// A: Code generation becomes easier without the need for extraneous casts.
//      Might fix it some day. But user code never sees these anyway.

// Right now, we do not support multiple protocols at the same time
static __thread ProtocolDesc *currentProto;

inline bool known(const OblivBit* o) { return !o->unknown; }

// --------------------------- Transports -----------------------------------
struct stdioTransport
{ ProtocolTransport cb;
  bool needFlush;
};

// Ignores 'dest' parameter. So you can't osend to yourself
static bool* stdioFlushFlag(ProtocolTransport* pt) 
  { return &((struct stdioTransport*)pt)->needFlush; }

static int stdioSend(ProtocolTransport* pt,int dest,const void* s,size_t n)
{ *stdioFlushFlag(pt)=true;
  return fwrite(s,1,n,stdout); 
}

static int stdioRecv(ProtocolTransport* pt,int src,void* s,size_t n)
{ 
  bool *p = stdioFlushFlag(pt);
  if(*p) { fflush(stdout); *p=false; }
  return fread(s,1,n,stdin); 
}

static ProtocolTransport* stdioSubtransport(ProtocolTransport* pt,int x) 
  { return NULL; }

static void stdioCleanup(ProtocolTransport* pt) {}

// Extremely simple, no multiplexing: two parties, one connection
static struct stdioTransport stdioTransport 
  = {{2, 1, 0, stdioSubtransport, stdioSend, stdioRecv, stdioCleanup},false};

void protocolUseStdio(ProtocolDesc* pd)
  { pd->trans = &stdioTransport.cb; }


// TCP connections for 2-Party protocols. Ignorse src/dest parameters
//   since there is only one remote
struct tcp2PTransport
{ ProtocolTransport cb;
  int* socks; // size = cb.maxChannels
  int cursock;
};

static int tcp2PSend(ProtocolTransport* pt,int dest,const void* s,size_t n)
{ int res = write(((struct tcp2PTransport*)pt)->cursock,s,n); 
  if(res<0) perror("TCP write error: ");
  if(res!=n) fprintf(stderr,"TCP write error: only %d bytes of %zd written\n",
                            res,n);
  return res;
}

static int tcp2PRecv(ProtocolTransport* pt,int src,void* s,size_t n)
{ int res;
  do
  { res = read(((struct tcp2PTransport*)pt)->cursock,s,n); 
    if(res<0) { perror("TCP read error: "); return res; }
    n-=res;
  } while(n>0);
  return res;
}

static void tcp2PCleanup(ProtocolTransport* pt)
{ struct tcp2PTransport* t = CAST(pt);
  int i;
  for(i=0;i<t->cb.maxChannels;++i) close(t->socks[i]);
  free(t->socks);
  free(t);
}

static void tcp2PSubCleanup(ProtocolTransport* pt) { free(pt); }

static ProtocolTransport* tcp2PSubtransport(ProtocolTransport* pt,int c)
{
  struct tcp2PTransport *t = CAST(pt), *tres;
  tres = malloc(sizeof(*tres));
  *tres = *t;
  tres->socks = t->socks+c;
  tres->cursock = t->socks[c];
  tres->cb.maxChannels = 1;<commit_msg>Fixed TCP read loop<commit_after>// TODO I need to fix some int sizes
#include <obliv_common.h>
#include <obliv_bits.h>
#include <assert.h>
#include <errno.h>      // libgcrypt needs ENOMEM definition
#include <inttypes.h>
#include <stdio.h>      // for protoUseStdio()
#include <string.h>
#include <netinet/ip.h>
#include <netdb.h>
#include <pthread.h>
#include <unistd.h>
#include <gcrypt.h>

// Q: What's with all these casts to and from void* ?
// A: Code generation becomes easier without the need for extraneous casts.
//      Might fix it some day. But user code never sees these anyway.

// Right now, we do not support multiple protocols at the same time
static __thread ProtocolDesc *currentProto;

inline bool known(const OblivBit* o) { return !o->unknown; }

// --------------------------- Transports -----------------------------------
struct stdioTransport
{ ProtocolTransport cb;
  bool needFlush;
};

// Ignores 'dest' parameter. So you can't osend to yourself
static bool* stdioFlushFlag(ProtocolTransport* pt) 
  { return &((struct stdioTransport*)pt)->needFlush; }

static int stdioSend(ProtocolTransport* pt,int dest,const void* s,size_t n)
{ *stdioFlushFlag(pt)=true;
  return fwrite(s,1,n,stdout); 
}

static int stdioRecv(ProtocolTransport* pt,int src,void* s,size_t n)
{ 
  bool *p = stdioFlushFlag(pt);
  if(*p) { fflush(stdout); *p=false; }
  return fread(s,1,n,stdin); 
}

static ProtocolTransport* stdioSubtransport(ProtocolTransport* pt,int x) 
  { return NULL; }

static void stdioCleanup(ProtocolTransport* pt) {}

// Extremely simple, no multiplexing: two parties, one connection
static struct stdioTransport stdioTransport 
  = {{2, 1, 0, stdioSubtransport, stdioSend, stdioRecv, stdioCleanup},false};

void protocolUseStdio(ProtocolDesc* pd)
  { pd->trans = &stdioTransport.cb; }


// TCP connections for 2-Party protocols. Ignorse src/dest parameters
//   since there is only one remote
struct tcp2PTransport
{ ProtocolTransport cb;
  int* socks; // size = cb.maxChannels
  int cursock;
};

static int tcp2PSend(ProtocolTransport* pt,int dest,const void* s,size_t n)
{ int res = write(((struct tcp2PTransport*)pt)->cursock,s,n); 
  if(res<0) perror("TCP write error: ");
  if(res!=n) fprintf(stderr,"TCP write error: only %d bytes of %zd written\n",
                            res,n);
  return res;
}

static int tcp2PRecv(ProtocolTransport* pt,int src,void* s,size_t n)
{ int res,n2=0;
  do
  { res = read(((struct tcp2PTransport*)pt)->cursock,n2+(char*)s,n-n2); 
    if(res<0) { perror("TCP read error: "); return res; }
    n2+=res;
  } while(n>n2);
  return res;
}

static void tcp2PCleanup(ProtocolTransport* pt)
{ struct tcp2PTransport* t = CAST(pt);
  int i;
  for(i=0;i<t->cb.maxChannels;++i) close(t->socks[i]);
  free(t->socks);
  free(t);
}

static void tcp2PSubCleanup(ProtocolTransport* pt) { free(pt); }

static ProtocolTransport* tcp2PSubtransport(ProtocolTransport* pt,int c)
{
  struct tcp2PTransport *t = CAST(pt), *tres;
  tres = malloc(sizeof(*tres));
  *tres = *t;
  tres->socks = t->socks+c;
  tres->cursock = t->socks[c];
  tres->cb.maxChannels = 1;
{'fraction_non_alphanumeric': 0.15331179321486268, 'fraction_numerical': 0.007592891760904685, 'mean_word_length': 4.00080775444265, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 55, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3715784', 'n_tokens_mistral': 2166, 'n_tokens_neox': 2128, 'n_words': 590}