int Ack(int m, int n)
{
    if (m == 0)
        return n + 1;
    if (n == 0)
        return Ack(m - 1, 1);
    return Ack(m-1, Ack(m, n - 1));
}
Ejemplo n.º 2
0
int
Ack(int M, int N) {
    assume(M >= 0);
    assume(N >= 0);
    if (M == 0) return( N + 1 );
    if (N == 0) return( Ack(M - 1, 1) );
    return( Ack(M - 1, Ack(M, (N - 1))) );
}
Ejemplo n.º 3
0
Archivo: t5.c Proyecto: kingfree/haut
int Ack(int m, int n)
{
    if (m == 0) {
        return n + 1;
    } else if (n == 0) {
        return Ack(m - 1, 1);
    } else if (m > 0 && n > 0) {
        return Ack(m - 1, Ack(m, n - 1));
    } else {
        return 1;
    }
}
Ejemplo n.º 4
0
int Ack (int LowerBound, int UpperBound) {
/*
/* Performs recursive calls for test purposes.
*/

   if (LowerBound == 0)
      return (UpperBound+1);
   else if (UpperBound == 0)
      return (Ack(LowerBound-1, 1));
   else
       return (Ack(LowerBound-1, Ack(LowerBound, UpperBound-1)));
}
Ejemplo n.º 5
0
static bool
OnKeyDown(gcc_unused WndForm &Sender, unsigned key_code)
{
  switch (key_code){
    case VK_ESCAPE:
      Hide();
    return true;

#ifdef GNAV
    case VK_APP1:
    case '6':
      Ack();
    return true;

    case VK_APP2:
    case '7':
      Ack1();
    return true;

    case VK_APP3:
    case '8':
      Ack2();
    return true;

    case VK_APP4:
    case '9':
      Enable();
    return true;
#endif

  default:
    return false;
  }
}
Ejemplo n.º 6
0
// create a new connection reading a file from server
void TFTPServer::ConnectRead(char* buff) {
    extern LaosFileSystem sd;
    remote_ip = client.get_address();
    remote_port = client.get_port();
    Ack(0);
    blockcnt = 0;
    dupcnt = 0;

    sprintf(filename, "%s", &buff[2]);
    
    if (modeOctet(buff))
        fp = sd.openfile(filename, "rb");
    else
        fp = sd.openfile(filename, "r");
    if (fp == NULL) {
        state  = listen;
        Err("Could not read file");
    } else {
        // file ready for reading
        blockcnt = 0;
        state = reading;
        #ifdef TFTP_DEBUG
            char debugmsg[256];
            sprintf(debugmsg, "Listen: Requested file %s from TFTP connection %d.%d.%d.%d port %d",
                filename, clientIp[0], clientIp[1], clientIp[2], clientIp[3], clientPort);
            TFTP_DEBUG(debugmsg);
        #endif
        getBlock();
        sendBlock();
    }
}
Ejemplo n.º 7
0
// create a new connection writing a file to the server
void TFTPServer::ConnectWrite(char* buff) {
    extern LaosFileSystem sd;
    remote_ip = client.get_address();
    remote_port = client.get_port();
    Ack(0);
    blockcnt = 0;
    dupcnt = 0;

    sprintf(filename, "%s", &buff[2]);
    sd.shorten(filename, MAXFILESIZE);
    if (modeOctet(buff))
        fp = sd.openfile(filename, "wb");
    else
        fp = sd.openfile(filename, "w");
    if (fp == NULL) {
        Err("Could not open file to write");
        state  = listen;
        strcpy(remote_ip,"");
    } else {
        // file ready for writing
        blockcnt = 0;
        state = writing;
        #ifdef TFTP_DEBUG 
            char debugmsg[256];
            sprintf(debugmsg, "Listen: Incoming file %s on TFTP connection from %d.%d.%d.%d clientPort %d",
                filename, clientIp[0], clientIp[1], clientIp[2], clientIp[3], clientPort);
            TFTP_DEBUG(debugmsg);
        #endif
    }
}
Ejemplo n.º 8
0
int
main(int argc, char *argv[]) {
    int n = ((argc == 2) ? atoi(argv[1]) : 8);

    printf("Ack(3,%d): %d\n", n, Ack(3, n));
    return(0);
}
Ejemplo n.º 9
0
/*    for comparison of compilers.
*/
int main () {
  int Result;

  Result = Ack (3,6);
  printf ("    - Numerical result of test is %d\n", Result);
  return 0;
}
Ejemplo n.º 10
0
//********************************************************************
// Name:  MailConnect
// Input:   None
// Output:  None
// Description: Connect to the mail host and receive the welcome message.
// Author/Date:  jcar 20/9/96
// History:
//********************************************************************
int MailConnect()
{

	int res;
	TLS_VARS;


	// Create Socket
	if ((GLOBAL(sc) = socket(PF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET)
		return (FAILED_TO_OBTAIN_SOCKET_HANDLE);

	// Get our own host name
	if (gethostname(GLOBAL(LocalHost), HOST_NAME_LEN))
		return (FAILED_TO_GET_HOSTNAME);

	// Resolve the servers IP
	//if (!isdigit(GLOBAL(MailHost)[0])||!gethostbyname(GLOBAL(MailHost)))
	//{
	//	return (FAILED_TO_RESOLVE_HOST);
	//}

	// Connect to server
	GLOBAL(sock_in).sin_family = AF_INET;
	GLOBAL(sock_in).sin_port = htons(25);
	GLOBAL(sock_in).sin_addr.S_un.S_addr = GetAddr(GLOBAL(MailHost));

	if (connect(GLOBAL(sc), (LPSOCKADDR) & GLOBAL(sock_in), sizeof(GLOBAL(sock_in))))
		return (FAILED_TO_CONNECT);

	// receive Server welcome message
	res = Ack();
	return (res);
}
Ejemplo n.º 11
0
/**
  * @brief  Description "模拟I2C读RTC8025函数"
  * @param  pdata  		读取的数据缓冲区指针
  * @param  addr		读取的器件内存地址
  * @param  count		读取的器件数据大小
  * @retval bool		RTC8025读函数是否成功
  */
bool RTC8025_Read(uint8_t *pData, uint16_t addr, uint16_t count)
{
    uint8_t 	addr_h = 0, addr_l = 0;
    uint16_t 	i;
    uAddress = AI2C_RX8025_ADDRESS;

    addr_l = ((addr % 16) << 4);

    addr_h = uAddress + addr_h;
    if (count == 0)
    {
        return true;
    }
	//! START condition sent by Master
    if (!Start())
        return false;
	//! Slave address + write specification
    SendByte(addr_h);
	//! Confirmation response from Master
    if (!WaitAck())
    {
        Stop();
        return false;
    }
//	theAI2C.SendByte(addr_h);
//	theAI2C.WaitAck();
    SendByte(addr_l);
    WaitAck();
	//! RESTART condition sent by Master
    Start();
	//! Indicates next byte will be read
    SendByte((addr_h) | 0x01);
    WaitAck();
	//! Data is read from the specified start address
    for (i = 0; i < count - 1; i ++)
    {
        RecvByte((pData + i));
        Ack(false);
    }
    RecvByte(pData + count - 1);
	//! Master does not respond
    Ack(true);//接收结束,发送非应答信号
	//! STOP condition sent by Master
    Stop();
	return true;
}
Ejemplo n.º 12
0
static TCHAR ReceiveByteAck(void)
{
   TCHAR lb = ReceiveByte();
   SDAOUT;		 /* leave SDA low for acknowledge */    
   Ack();
   SDAIN;		 /* set SDA high again */   
   return lb;
} /* end ReceiveByteAck() */
Ejemplo n.º 13
0
Archivo: t5.c Proyecto: kingfree/haut
int main()
{
    int m, n;
    inputi(m);
    inputi(n);
    printf("Ackerman(%d, %d) = %d\n", m, n, Ack(m, n));
    return 0;
}
Ejemplo n.º 14
0
//********************************************************************
// Name:  TSendMail::~TSendMail
// Input:
// Output:
// Description: DESTRUCTOR
// Author/Date:  jcar 20/9/96
// History:
//********************************************************************
void TSMClose()
{
	TLS_VARS;

	Post("QUIT\r\n");
	Ack();
	// to guarantee that the cleanup is not made twice and
	// compomise the rest of the application if sockets are used
	// elesewhere
}
Ejemplo n.º 15
0
int main(){

  int x1 = 4;
  int x2 = 1;

  int a = Ack(x1,x2);
  printf("Ack(%d,%d)=%d\n",x1,x2,a);

  return (0);
}
Ejemplo n.º 16
0
bool MoonLiteDRO::Handshake()
{
    if (Ack())
    {
        LOGF_INFO("%s is online. Getting focus parameters...", getDeviceName());
        return true;
    }

    LOG_INFO("Handshake failed. please ensure MoonLite controller is powered and the port is correct.");
    return false;
}
Ejemplo n.º 17
0
bool MoonLite::Handshake()
{
    if (Ack())
    {
        DEBUG(INDI::Logger::DBG_SESSION, "MoonLite is online. Getting focus parameters...");
        return true;
    }

    DEBUG(INDI::Logger::DBG_SESSION, "Error retreiving data from MoonLite, please ensure MoonLite controller is powered and the port is correct.");
    return false;
}
Ejemplo n.º 18
0
bool MoonLite::Handshake()
{
    if (Ack())
    {
        LOG_INFO("MoonLite is online. Getting focus parameters...");
        return true;
    }

    LOG_INFO(
          "Error retreiving data from MoonLite, please ensure MoonLite controller is powered and the port is correct.");
    return false;
}
Ejemplo n.º 19
0
		/* 128 is safe here, the specifier in snprintf isn't longer than that */
		*error_message = ecalloc(1, HOST_NAME_LEN + 128);
		snprintf(*error_message, HOST_NAME_LEN + 128,
			"Failed to connect to mailserver at \"%s\" port %d, verify your \"SMTP\" "
			"and \"smtp_port\" setting in php.ini or use ini_set()",
			PW32G(mail_host), !INI_INT("smtp_port") ? 25 : INI_INT("smtp_port"));
		return FAILURE;
	} else {
		ret = SendText(RPath, Subject, mailTo, mailCc, mailBcc, data, headers ? ZSTR_VAL(headers_trim) : NULL, headers ? ZSTR_VAL(headers_lc) : NULL, error_message);
		TSMClose();
		if (RPath) {
			efree(RPath);
		}
		if (headers) {
			zend_string_free(headers_trim);
			zend_string_free(headers_lc);
		}
		if (ret != SUCCESS) {
			*error = ret;
			return FAILURE;
		}
		return SUCCESS;
	}
}

//********************************************************************
// Name:  TSendMail::~TSendMail
// Input:
// Output:
// Description: DESTRUCTOR
// Author/Date:  jcar 20/9/96
// History:
//********************************************************************/
PHPAPI void TSMClose()
{
	Post("QUIT\r\n");
	Ack(NULL);
	/* to guarantee that the cleanup is not made twice and
	   compomise the rest of the application if sockets are used
	   elesewhere
	*/

	shutdown(PW32G(mail_socket), 0);
	closesocket(PW32G(mail_socket));
}
Ejemplo n.º 20
0
Archivo: acc.cpp Proyecto: Kreyl/nute
uint8_t i2c_t::WriteReadBuf(uint8_t Addr, uint8_t *PW, uint16_t SzW, uint8_t *PR, uint16_t SzR) {
    uint8_t Rslt = 0;
    chSysLock();
    if(Start() != 0) {
        Rslt = 1;
        goto End;
    }
    // Write Addr with Write bit (0)
    WriteByte(Addr<<1);
    if(!IsAcked()) {
        Rslt = 2;
        Uart.Printf("Addr NACK\r");
        goto End;
    }
    chSysUnlock();

    // Write data
    chSysLock();
    while(SzW--) {
        WriteByte(*PW++);
        if(!IsAcked()) {
            Rslt = 3;
            Uart.Printf("NACK\r");
            goto End;
        }
    }
    chSysUnlock();

    // Send repeated start
    chSysLock();
    Start();
    // Write Addr with Read bit (1)
    WriteByte((Addr<<1) | 0x01);
    if(!IsAcked()) {
        Rslt = 4;
        Uart.Printf("Addr NACK\r");
        goto End;
    }
    chSysUnlock();

    // Read data
    chSysLock();
    while(SzR) {
        *PR++ = ReadByte();
        if(SzR != 1) Ack();
        else Nack();
        SzR--;
    }
    End:
    Stop();
    chSysUnlock();
    return Rslt;
}
Ejemplo n.º 21
0
bool SestoSenso::Handshake()
{
    if (Ack())
    {
        DEBUG(INDI::Logger::DBG_SESSION, "SestoSenso is online. Getting focus parameters...");
        return true;
    }

    DEBUG(INDI::Logger::DBG_SESSION,
          "Error retreiving data from SestoSenso, please ensure SestoSenso controller is powered and the port is correct.");
    return false;
}
Ejemplo n.º 22
0
/**
  * @brief  Description "模拟I2C读函数"
  * @param  pdata  		读取的数据缓冲区指针
  * @param  addr		读取的器件内存地址
  * @param  count		读取的器件数据大小
  * @retval bool		I2C读函数是否成功
  */
bool AI2C_Read(uint8_t *pData, uint16_t addr, uint16_t count)
{
	uint8_t		device_addr = 0;
	uint8_t 	addr_msb = 0, addr_lsb = 0;
    uint16_t 	i;
	uAddress = AI2C_FRAM_ADDRESS;
//    CAI2C		theAI2C(0xA0);
	/*            从机ID   + 器件选择 + 读写选择  */
	device_addr = uAddress + 0x0		+ 0;
    addr_msb = ((addr / 256)&0X3F);
    addr_lsb = (addr % 256);
    if (count == 0)
    {
        return true;
    }
    if (!Start())
        return false;
    SendByte(device_addr);
    if (!WaitAck())
    {
        Stop();
        return false;
    }
    SendByte(addr_msb);
    WaitAck();
    SendByte(addr_lsb);
    WaitAck();
    Start();
    SendByte(device_addr|0X01);
    WaitAck();
    for (i = 0; i < count - 1; i ++)
    {
        RecvByte((pData + i));
        Ack(false);
    }
    RecvByte(pData + count - 1);
    Ack(true);//接收结束,发送非应答信号
    Stop();
	return true;
}
Ejemplo n.º 23
0
int main(void)
{
   int m,n;
   int result;
   int i,repeat;

   scanf("%d",&repeat);
   for(i=1; i<=repeat; i++)
   {
       scanf("%d%d", &m, &n);
       result = Ack(m,n);
       printf("Ackerman(%d,%d)=%d\n", m, n, result);
   }
}
Ejemplo n.º 24
0
int Ack(int x1,int x2){
  while(1){
    if(x1==0 && x2!=0){
      x2 += 1;
      break;
    }
    else if(x1!=0 && x2==0){
      x1 -= 1;
      x2 = 1;
    }
    else{
      x1 -= 1;
      x2 = Ack(x1+1,x2-1);
    }
  }
  return(x2);
}
Ejemplo n.º 25
0
void ShooterGame::ProcessMessage(std::string address, std::string message)
{
	//printf("%s: %s\n", address.c_str(), message.c_str());
	std::string debug = message;
	std::string ack;
	auto pos = message.find_first_of('|');
	ack = message.substr(0, pos);
	message = message.substr(pos + 1);

	if (ack[0] == 'A') Ack(address, message);
	else
	{
		if (MartEngine::Args::Get()[1] == "Server")
		{
			if (mConnections[address] == nullptr)
			{
				mConnections[address] = SocketAddressFactory::CreateIPv4FromString(address);
				IgnoreListPtr ignore = std::make_shared<IgnoreList>(*(new IgnoreList(20)));
				mIgnoreList[mConnections[address]] = ignore;
				SlidingWindowPtr messages = std::make_shared<SlidingWindow>(*(new SlidingWindow(20)));
				mMessageQueue[mConnections[address]] = messages;
				for (const auto& str : onJoinMessages) messages->Add(str);
				messages->Add("OnConnect|" + address);
			}
		}

		int ackNumber = std::stoi(ack);
		SendAck(address, ack);
		IgnoreListPtr ignoreList = mIgnoreList[mConnections[address]];
		if (!ignoreList->IsIgnoring(ackNumber))
		{
			auto pos = message.find_first_of('|');
			std::string function = message.substr(0, pos);
			if (mFunctions[function]) mFunctions[function](address, message.substr(pos + 1));
			ignoreList->Ignore(ackNumber);
		}
	}
}
Ejemplo n.º 26
0
/************************************************************************************
 *
* ***********************************************************************************/
bool ScopeDome::Handshake()
{
    return Ack();
}
Ejemplo n.º 27
0
/*********************************************************************
// Name:  MailConnect
// Input:   None
// Output:  None
// Description: Connect to the mail host and receive the welcome message.
// Author/Date:  jcar 20/9/96
// History:
//********************************************************************/
static int MailConnect()
{

	int res, namelen;
	short portnum;
	struct hostent *ent;
	IN_ADDR addr;
#ifdef HAVE_IPV6
	IN6_ADDR addr6;
#endif
	SOCKADDR_IN sock_in;

#if SENDMAIL_DEBUG
return 0;
#endif

	/* Create Socket */
	if ((PW32G(mail_socket) = socket(PF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET) {
		return (FAILED_TO_OBTAIN_SOCKET_HANDLE);
	}

	/* Get our own host name */
	if (gethostname(PW32G(mail_local_host), HOST_NAME_LEN)) {
		closesocket(PW32G(mail_socket));
		return (FAILED_TO_GET_HOSTNAME);
	}

	ent = gethostbyname(PW32G(mail_local_host));

	if (!ent) {
		closesocket(PW32G(mail_socket));
		return (FAILED_TO_GET_HOSTNAME);
	}

	namelen = (int)strlen(ent->h_name);

#ifdef HAVE_IPV6
	if (inet_pton(AF_INET, ent->h_name, &addr) == 1 || inet_pton(AF_INET6, ent->h_name, &addr6) == 1)
#else
	if (inet_pton(AF_INET, ent->h_name, &addr) == 1)
#endif
	{
		if (namelen + 2 >= HOST_NAME_LEN) {
			closesocket(PW32G(mail_socket));
			return (FAILED_TO_GET_HOSTNAME);
		}

		strcpy(PW32G(mail_local_host), "[");
		strcpy(PW32G(mail_local_host) + 1, ent->h_name);
		strcpy(PW32G(mail_local_host) + namelen + 1, "]");
	} else {
		if (namelen >= HOST_NAME_LEN) {
			closesocket(PW32G(mail_socket));
			return (FAILED_TO_GET_HOSTNAME);
		}

		strcpy(PW32G(mail_local_host), ent->h_name);
	}

	/* Resolve the servers IP */
	/*
	if (!isdigit(PW32G(mail_host)[0])||!gethostbyname(PW32G(mail_host)))
	{
		return (FAILED_TO_RESOLVE_HOST);
	}
	*/

	portnum = (short) INI_INT("smtp_port");
	if (!portnum) {
		portnum = 25;
	}

	/* Connect to server */
	sock_in.sin_family = AF_INET;
	sock_in.sin_port = htons(portnum);
	sock_in.sin_addr.S_un.S_addr = GetAddr(PW32G(mail_host));

	if (connect(PW32G(mail_socket), (LPSOCKADDR) & sock_in, sizeof(sock_in))) {
		closesocket(PW32G(mail_socket));
		return (FAILED_TO_CONNECT);
	}

	/* receive Server welcome message */
	res = Ack(NULL);
	return (res);
}
Ejemplo n.º 28
0
/*********************************************************************
// Name:  SendText
// Input:       1) RPath:   return path of the message
//                                  Is used to fill the "Return-Path" and the
//                                  "X-Sender" fields of the message.
//                  2) Subject: Subject field of the message. If NULL is given
//                                  the subject is set to "No Subject"
//                  3) mailTo:  Destination address
//                  4) data:        Null terminated string containing the data to be send.
//                  5,6) headers of the message. Note that the second
//                  parameter, headers_lc, is actually a lowercased version of
//                  headers. The should match exactly (in terms of length),
//                  only differ in case
// Output:      Error code or SUCCESS
// Description:
// Author/Date:  jcar 20/9/96
// History:
//*******************************************************************/
static int SendText(char *RPath, char *Subject, char *mailTo, char *mailCc, char *mailBcc, char *data,
			 char *headers, char *headers_lc, char **error_message)
{
	int res;
	char *p;
	char *tempMailTo, *token, *pos1, *pos2;
	char *server_response = NULL;
	char *stripped_header  = NULL;
	zend_string *data_cln;

	/* check for NULL parameters */
	if (data == NULL)
		return (BAD_MSG_CONTENTS);
	if (mailTo == NULL)
		return (BAD_MSG_DESTINATION);
	if (RPath == NULL)
		return (BAD_MSG_RPATH);

	/* simple checks for the mailto address */
	/* have ampersand ? */
	/* mfischer, 20020514: I commented this out because it really
	   seems bogus. Only a username for example may still be a
	   valid address at the destination system.
	if (strchr(mailTo, '@') == NULL)
		return (BAD_MSG_DESTINATION);
	*/

	snprintf(PW32G(mail_buffer), sizeof(PW32G(mail_buffer)), "HELO %s\r\n", PW32G(mail_local_host));

	/* in the beginning of the dialog */
	/* attempt reconnect if the first Post fail */
	if ((res = Post(PW32G(mail_buffer))) != SUCCESS) {
		int err = MailConnect();
		if (0 != err) {
			return (FAILED_TO_SEND);
		}

		if ((res = Post(PW32G(mail_buffer))) != SUCCESS) {
			return (res);
		}
	}
	if ((res = Ack(&server_response)) != SUCCESS) {
		SMTP_ERROR_RESPONSE(server_response);
		return (res);
	}

	SMTP_SKIP_SPACE(RPath);
	FormatEmailAddress(PW32G(mail_buffer), RPath, "MAIL FROM:<%s>\r\n");
	if ((res = Post(PW32G(mail_buffer))) != SUCCESS) {
		return (res);
	}
	if ((res = Ack(&server_response)) != SUCCESS) {
		SMTP_ERROR_RESPONSE(server_response);
		return W32_SM_SENDMAIL_FROM_MALFORMED;
	}

	tempMailTo = estrdup(mailTo);
	/* Send mail to all rcpt's */
	token = strtok(tempMailTo, ",");
	while (token != NULL)
	{
		SMTP_SKIP_SPACE(token);
		FormatEmailAddress(PW32G(mail_buffer), token, "RCPT TO:<%s>\r\n");
		if ((res = Post(PW32G(mail_buffer))) != SUCCESS) {
			efree(tempMailTo);
			return (res);
		}
		if ((res = Ack(&server_response)) != SUCCESS) {
			SMTP_ERROR_RESPONSE(server_response);
			efree(tempMailTo);
			return (res);
		}
		token = strtok(NULL, ",");
	}
	efree(tempMailTo);

	if (mailCc && *mailCc) {
		tempMailTo = estrdup(mailCc);
		/* Send mail to all rcpt's */
		token = strtok(tempMailTo, ",");
		while (token != NULL)
		{
			SMTP_SKIP_SPACE(token);
			FormatEmailAddress(PW32G(mail_buffer), token, "RCPT TO:<%s>\r\n");
			if ((res = Post(PW32G(mail_buffer))) != SUCCESS) {
				efree(tempMailTo);
				return (res);
			}
			if ((res = Ack(&server_response)) != SUCCESS) {
				SMTP_ERROR_RESPONSE(server_response);
				efree(tempMailTo);
				return (res);
			}
			token = strtok(NULL, ",");
		}
		efree(tempMailTo);
	}
	/* Send mail to all Cc rcpt's */
	else if (headers && (pos1 = strstr(headers_lc, "cc:")) && ((pos1 == headers_lc) || (*(pos1-1) == '\n'))) {
		/* Real offset is memaddress from the original headers + difference of
		 * string found in the lowercase headrs + 3 characters to jump over
		 * the cc: */
		pos1 = headers + (pos1 - headers_lc) + 3;
		if (NULL == (pos2 = strstr(pos1, "\r\n"))) {
			tempMailTo = estrndup(pos1, strlen(pos1));
		} else {
			tempMailTo = estrndup(pos1, pos2 - pos1);
		}

		token = strtok(tempMailTo, ",");
		while (token != NULL)
		{
			SMTP_SKIP_SPACE(token);
			FormatEmailAddress(PW32G(mail_buffer), token, "RCPT TO:<%s>\r\n");
			if ((res = Post(PW32G(mail_buffer))) != SUCCESS) {
				efree(tempMailTo);
				return (res);
			}
			if ((res = Ack(&server_response)) != SUCCESS) {
				SMTP_ERROR_RESPONSE(server_response);
				efree(tempMailTo);
				return (res);
			}
			token = strtok(NULL, ",");
		}
		efree(tempMailTo);
	}

	/* Send mail to all Bcc rcpt's
	   This is basically a rip of the Cc code above.
	   Just don't forget to remove the Bcc: from the header afterwards. */
	if (mailBcc && *mailBcc) {
		tempMailTo = estrdup(mailBcc);
		/* Send mail to all rcpt's */
		token = strtok(tempMailTo, ",");
		while (token != NULL)
		{
			SMTP_SKIP_SPACE(token);
			FormatEmailAddress(PW32G(mail_buffer), token, "RCPT TO:<%s>\r\n");
			if ((res = Post(PW32G(mail_buffer))) != SUCCESS) {
				efree(tempMailTo);
				return (res);
			}
			if ((res = Ack(&server_response)) != SUCCESS) {
				SMTP_ERROR_RESPONSE(server_response);
				efree(tempMailTo);
				return (res);
			}
			token = strtok(NULL, ",");
		}
		efree(tempMailTo);
	}
	else if (headers) {
		if ((pos1 = strstr(headers_lc, "bcc:")) && (pos1 == headers_lc || *(pos1-1) == '\n')) {
			/* Real offset is memaddress from the original headers + difference of
			 * string found in the lowercase headrs + 4 characters to jump over
			 * the bcc: */
			pos1 = headers + (pos1 - headers_lc) + 4;
			if (NULL == (pos2 = strstr(pos1, "\r\n"))) {
				tempMailTo = estrndup(pos1, strlen(pos1));
				/* Later, when we remove the Bcc: out of the
				   header we know it was the last thing. */
				pos2 = pos1;
			} else {
				tempMailTo = estrndup(pos1, pos2 - pos1);
			}

			token = strtok(tempMailTo, ",");
			while (token != NULL)
			{
				SMTP_SKIP_SPACE(token);
				FormatEmailAddress(PW32G(mail_buffer), token, "RCPT TO:<%s>\r\n");
				if ((res = Post(PW32G(mail_buffer))) != SUCCESS) {
					efree(tempMailTo);
					return (res);
				}
				if ((res = Ack(&server_response)) != SUCCESS) {
					SMTP_ERROR_RESPONSE(server_response);
					efree(tempMailTo);
					return (res);
				}
				token = strtok(NULL, ",");
			}
			efree(tempMailTo);

			/* Now that we've identified that we've a Bcc list,
			   remove it from the current header. */
			stripped_header = ecalloc(1, strlen(headers));
			/* headers = point to string start of header
			   pos1    = pointer IN headers where the Bcc starts
			   '4'     = Length of the characters 'bcc:'
			   Because we've added +4 above for parsing the Emails
			   we've to subtract them here. */
			memcpy(stripped_header, headers, pos1 - headers - 4);
			if (pos1 != pos2) {
				/* if pos1 != pos2 , pos2 points to the rest of the headers.
				   Since pos1 != pos2 if "\r\n" was found, we know those characters
				   are there and so we jump over them (else we would generate a new header
				   which would look like "\r\n\r\n". */
				memcpy(stripped_header + (pos1 - headers - 4), pos2 + 2, strlen(pos2) - 2);
			}
		}
	}

	/* Simplify the code that we create a copy of stripped_header no matter if
	   we actually strip something or not. So we've a single efree() later. */
	if (headers && !stripped_header) {
		stripped_header = estrndup(headers, strlen(headers));
	}

	if ((res = Post("DATA\r\n")) != SUCCESS) {
		if (stripped_header) {
			efree(stripped_header);
		}
		return (res);
	}
	if ((res = Ack(&server_response)) != SUCCESS) {
		SMTP_ERROR_RESPONSE(server_response);
		if (stripped_header) {
			efree(stripped_header);
		}
		return (res);
	}

	/* send message header */
	if (Subject == NULL) {
		res = PostHeader(RPath, "No Subject", mailTo, stripped_header);
	} else {
		res = PostHeader(RPath, Subject, mailTo, stripped_header);
	}
	if (stripped_header) {
		efree(stripped_header);
	}
	if (res != SUCCESS) {
		return (res);
	}

	/* Escape \n. sequences
	 * We use php_str_to_str() and not php_str_replace_in_subject(), since the latter
	 * uses ZVAL as it's parameters */
	data_cln = php_str_to_str(data, strlen(data), PHP_WIN32_MAIL_DOT_PATTERN, sizeof(PHP_WIN32_MAIL_DOT_PATTERN) - 1,
					PHP_WIN32_MAIL_DOT_REPLACE, sizeof(PHP_WIN32_MAIL_DOT_REPLACE) - 1);
	if (!data_cln) {
		data_cln = ZSTR_EMPTY_ALLOC();
	}

	/* send message contents in 1024 chunks */
	{
		char c, *e2, *e = ZSTR_VAL(data_cln) + ZSTR_LEN(data_cln);
		p = ZSTR_VAL(data_cln);

		while (e - p > 1024) {
			e2 = p + 1024;
			c = *e2;
			*e2 = '\0';
			if ((res = Post(p)) != SUCCESS) {
				zend_string_free(data_cln);
				return(res);
			}
			*e2 = c;
			p = e2;
		}
		if ((res = Post(p)) != SUCCESS) {
			zend_string_free(data_cln);
			return(res);
		}
	}

	zend_string_free(data_cln);

	/*send termination dot */
	if ((res = Post("\r\n.\r\n")) != SUCCESS)
		return (res);
	if ((res = Ack(&server_response)) != SUCCESS) {
		SMTP_ERROR_RESPONSE(server_response);
		return (res);
	}

	return (SUCCESS);
}
Ejemplo n.º 29
0
Archivo: ack.c Proyecto: bloff/shootout
int
Ack(jint M, jint N) {
   if(M == 0) return(N + 1);
   if(N == 0) return(Ack(M - 1, 1));
   return(Ack(M - 1, Ack(M, (N - 1))));
}
Ejemplo n.º 30
0
Archivo: ack.c Proyecto: bloff/shootout
JNIEXPORT jint JNICALL 
Java_ack_Ack(JNIEnv *env, jclass obj, jint m, jint n) {
   return Ack(m, n);
}