14b22b93rs/* -*- Mode: C; tab-width: 4 -*- 24b22b93rs * 3cda73f6Toomas Soome * Copyright (c) 2003-2011 Apple Inc. All rights reserved. 44b22b93rs * 55ffb0c9Toomas Soome * Redistribution and use in source and binary forms, with or without 64b22b93rs * modification, are permitted provided that the following conditions are met: 74b22b93rs * 85ffb0c9Toomas Soome * 1. Redistributions of source code must retain the above copyright notice, 95ffb0c9Toomas Soome * this list of conditions and the following disclaimer. 105ffb0c9Toomas Soome * 2. Redistributions in binary form must reproduce the above copyright notice, 115ffb0c9Toomas Soome * this list of conditions and the following disclaimer in the documentation 125ffb0c9Toomas Soome * and/or other materials provided with the distribution. 13cda73f6Toomas Soome * 3. Neither the name of Apple Inc. ("Apple") nor the names of its 145ffb0c9Toomas Soome * contributors may be used to endorse or promote products derived from this 155ffb0c9Toomas Soome * software without specific prior written permission. 164b22b93rs * 175ffb0c9Toomas Soome * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY 185ffb0c9Toomas Soome * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 195ffb0c9Toomas Soome * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 205ffb0c9Toomas Soome * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY 215ffb0c9Toomas Soome * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 225ffb0c9Toomas Soome * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 235ffb0c9Toomas Soome * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 245ffb0c9Toomas Soome * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 255ffb0c9Toomas Soome * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 264b22b93rs * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 274b22b93rs */ 284b22b93rs 294b22b93rs#include "dnssd_ipc.h" 304b22b93rs 315ffb0c9Toomas Soome#if defined(_WIN32) 325ffb0c9Toomas Soome 335ffb0c9Toomas Soomechar *win32_strerror(int inErrorCode) 345ffb0c9Toomas Soome{ 355ffb0c9Toomas Soome static char buffer[1024]; 365ffb0c9Toomas Soome DWORD n; 375ffb0c9Toomas Soome memset(buffer, 0, sizeof(buffer)); 385ffb0c9Toomas Soome n = FormatMessageA( 395ffb0c9Toomas Soome FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, 405ffb0c9Toomas Soome NULL, 415ffb0c9Toomas Soome (DWORD) inErrorCode, 425ffb0c9Toomas Soome MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), 435ffb0c9Toomas Soome buffer, 445ffb0c9Toomas Soome sizeof(buffer), 455ffb0c9Toomas Soome NULL); 465ffb0c9Toomas Soome if (n > 0) 475ffb0c9Toomas Soome { 485ffb0c9Toomas Soome // Remove any trailing CR's or LF's since some messages have them. 495ffb0c9Toomas Soome while ((n > 0) && isspace(((unsigned char *) buffer)[n - 1])) 505ffb0c9Toomas Soome buffer[--n] = '\0'; 515ffb0c9Toomas Soome } 525ffb0c9Toomas Soome return buffer; 535ffb0c9Toomas Soome} 545ffb0c9Toomas Soome 555ffb0c9Toomas Soome#endif 565ffb0c9Toomas Soome 575ffb0c9Toomas Soomevoid put_uint32(const uint32_t l, char **ptr) 585ffb0c9Toomas Soome{ 595ffb0c9Toomas Soome (*ptr)[0] = (char)((l >> 24) & 0xFF); 605ffb0c9Toomas Soome (*ptr)[1] = (char)((l >> 16) & 0xFF); 615ffb0c9Toomas Soome (*ptr)[2] = (char)((l >> 8) & 0xFF); 625ffb0c9Toomas Soome (*ptr)[3] = (char)((l ) & 0xFF); 635ffb0c9Toomas Soome *ptr += sizeof(uint32_t); 645ffb0c9Toomas Soome} 655ffb0c9Toomas Soome 665ffb0c9Toomas Soomeuint32_t get_uint32(const char **ptr, const char *end) 675ffb0c9Toomas Soome{ 685ffb0c9Toomas Soome if (!*ptr || *ptr + sizeof(uint32_t) > end) 695ffb0c9Toomas Soome { 705ffb0c9Toomas Soome *ptr = NULL; 715ffb0c9Toomas Soome return(0); 725ffb0c9Toomas Soome } 735ffb0c9Toomas Soome else 745ffb0c9Toomas Soome { 755ffb0c9Toomas Soome uint8_t *p = (uint8_t*) *ptr; 765ffb0c9Toomas Soome *ptr += sizeof(uint32_t); 775ffb0c9Toomas Soome return((uint32_t) ((uint32_t)p[0] << 24 | (uint32_t)p[1] << 16 | (uint32_t)p[2] << 8 | p[3])); 785ffb0c9Toomas Soome } 795ffb0c9Toomas Soome} 805ffb0c9Toomas Soome 815ffb0c9Toomas Soomevoid put_uint16(uint16_t s, char **ptr) 825ffb0c9Toomas Soome{ 835ffb0c9Toomas Soome (*ptr)[0] = (char)((s >> 8) & 0xFF); 845ffb0c9Toomas Soome (*ptr)[1] = (char)((s ) & 0xFF); 855ffb0c9Toomas Soome *ptr += sizeof(uint16_t); 865ffb0c9Toomas Soome} 875ffb0c9Toomas Soome 885ffb0c9Toomas Soomeuint16_t get_uint16(const char **ptr, const char *end) 895ffb0c9Toomas Soome{ 905ffb0c9Toomas Soome if (!*ptr || *ptr + sizeof(uint16_t) > end) 915ffb0c9Toomas Soome { 925ffb0c9Toomas Soome *ptr = NULL; 935ffb0c9Toomas Soome return(0); 945ffb0c9Toomas Soome } 955ffb0c9Toomas Soome else 965ffb0c9Toomas Soome { 975ffb0c9Toomas Soome uint8_t *p = (uint8_t*) *ptr; 985ffb0c9Toomas Soome *ptr += sizeof(uint16_t); 995ffb0c9Toomas Soome return((uint16_t) ((uint16_t)p[0] << 8 | p[1])); 1005ffb0c9Toomas Soome } 1015ffb0c9Toomas Soome} 1024b22b93rs 1034b22b93rsint put_string(const char *str, char **ptr) 1045ffb0c9Toomas Soome{ 1055ffb0c9Toomas Soome if (!str) str = ""; 1065ffb0c9Toomas Soome strcpy(*ptr, str); 1075ffb0c9Toomas Soome *ptr += strlen(str) + 1; 1085ffb0c9Toomas Soome return 0; 1095ffb0c9Toomas Soome} 1105ffb0c9Toomas Soome 1115ffb0c9Toomas Soomeint get_string(const char **ptr, const char *const end, char *buffer, int buflen) 1125ffb0c9Toomas Soome{ 1135ffb0c9Toomas Soome if (!*ptr) 1145ffb0c9Toomas Soome { 1155ffb0c9Toomas Soome *buffer = 0; 1165ffb0c9Toomas Soome return(-1); 1175ffb0c9Toomas Soome } 1185ffb0c9Toomas Soome else 1195ffb0c9Toomas Soome { 1205ffb0c9Toomas Soome char *lim = buffer + buflen; // Calculate limit 1215ffb0c9Toomas Soome while (*ptr < end && buffer < lim) 1225ffb0c9Toomas Soome { 1235ffb0c9Toomas Soome char c = *buffer++ = *(*ptr)++; 1245ffb0c9Toomas Soome if (c == 0) return(0); // Success 1255ffb0c9Toomas Soome } 1265ffb0c9Toomas Soome if (buffer == lim) buffer--; 1275ffb0c9Toomas Soome *buffer = 0; // Failed, so terminate string, 1285ffb0c9Toomas Soome *ptr = NULL; // clear pointer, 1295ffb0c9Toomas Soome return(-1); // and return failure indication 1305ffb0c9Toomas Soome } 1315ffb0c9Toomas Soome} 1324b22b93rs 1334b22b93rsvoid put_rdata(const int rdlen, const unsigned char *rdata, char **ptr) 1345ffb0c9Toomas Soome{ 1355ffb0c9Toomas Soome memcpy(*ptr, rdata, rdlen); 1365ffb0c9Toomas Soome *ptr += rdlen; 1375ffb0c9Toomas Soome} 1385ffb0c9Toomas Soome 1395ffb0c9Toomas Soomeconst char *get_rdata(const char **ptr, const char *end, int rdlen) 1405ffb0c9Toomas Soome{ 1415ffb0c9Toomas Soome if (!*ptr || *ptr + rdlen > end) 1425ffb0c9Toomas Soome { 1435ffb0c9Toomas Soome *ptr = NULL; 1445ffb0c9Toomas Soome return(0); 1455ffb0c9Toomas Soome } 1465ffb0c9Toomas Soome else 1475ffb0c9Toomas Soome { 1485ffb0c9Toomas Soome const char *rd = *ptr; 1495ffb0c9Toomas Soome *ptr += rdlen; 1505ffb0c9Toomas Soome return rd; 1515ffb0c9Toomas Soome } 1525ffb0c9Toomas Soome} 1534b22b93rs 1544b22b93rsvoid ConvertHeaderBytes(ipc_msg_hdr *hdr) 1555ffb0c9Toomas Soome{ 1565ffb0c9Toomas Soome hdr->version = htonl(hdr->version); 1575ffb0c9Toomas Soome hdr->datalen = htonl(hdr->datalen); 1585ffb0c9Toomas Soome hdr->ipc_flags = htonl(hdr->ipc_flags); 1595ffb0c9Toomas Soome hdr->op = htonl(hdr->op ); 1605ffb0c9Toomas Soome hdr->reg_index = htonl(hdr->reg_index); 1615ffb0c9Toomas Soome} 162