14b22b933Srs200217 /* -*- Mode: C; tab-width: 4 -*- 24b22b933Srs200217 * 34b22b933Srs200217 * Copyright (c) 2003-2004, Apple Computer, Inc. All rights reserved. 44b22b933Srs200217 * 54b22b933Srs200217 * Redistribution and use in source and binary forms, with or without 64b22b933Srs200217 * modification, are permitted provided that the following conditions are met: 74b22b933Srs200217 * 84b22b933Srs200217 * 1. Redistributions of source code must retain the above copyright notice, 94b22b933Srs200217 * this list of conditions and the following disclaimer. 104b22b933Srs200217 * 2. Redistributions in binary form must reproduce the above copyright notice, 114b22b933Srs200217 * this list of conditions and the following disclaimer in the documentation 124b22b933Srs200217 * and/or other materials provided with the distribution. 134b22b933Srs200217 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of its 144b22b933Srs200217 * contributors may be used to endorse or promote products derived from this 154b22b933Srs200217 * software without specific prior written permission. 164b22b933Srs200217 * 174b22b933Srs200217 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY 184b22b933Srs200217 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 194b22b933Srs200217 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 204b22b933Srs200217 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY 214b22b933Srs200217 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 224b22b933Srs200217 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 234b22b933Srs200217 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 244b22b933Srs200217 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 254b22b933Srs200217 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 264b22b933Srs200217 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 274b22b933Srs200217 */ 284b22b933Srs200217 294b22b933Srs200217 #include "dnssd_ipc.h" 304b22b933Srs200217 31*5ffb0c9bSToomas Soome #if defined(_WIN32) 32*5ffb0c9bSToomas Soome 33*5ffb0c9bSToomas Soome char *win32_strerror(int inErrorCode) 34*5ffb0c9bSToomas Soome { 35*5ffb0c9bSToomas Soome static char buffer[1024]; 36*5ffb0c9bSToomas Soome DWORD n; 37*5ffb0c9bSToomas Soome memset(buffer, 0, sizeof(buffer)); 38*5ffb0c9bSToomas Soome n = FormatMessageA( 39*5ffb0c9bSToomas Soome FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, 40*5ffb0c9bSToomas Soome NULL, 41*5ffb0c9bSToomas Soome (DWORD) inErrorCode, 42*5ffb0c9bSToomas Soome MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), 43*5ffb0c9bSToomas Soome buffer, 44*5ffb0c9bSToomas Soome sizeof(buffer), 45*5ffb0c9bSToomas Soome NULL); 46*5ffb0c9bSToomas Soome if (n > 0) 47*5ffb0c9bSToomas Soome { 48*5ffb0c9bSToomas Soome // Remove any trailing CR's or LF's since some messages have them. 49*5ffb0c9bSToomas Soome while ((n > 0) && isspace(((unsigned char *) buffer)[n - 1])) 50*5ffb0c9bSToomas Soome buffer[--n] = '\0'; 51*5ffb0c9bSToomas Soome } 52*5ffb0c9bSToomas Soome return buffer; 53*5ffb0c9bSToomas Soome } 54*5ffb0c9bSToomas Soome 55*5ffb0c9bSToomas Soome #endif 56*5ffb0c9bSToomas Soome 57*5ffb0c9bSToomas Soome void put_uint32(const uint32_t l, char **ptr) 584b22b933Srs200217 { 594b22b933Srs200217 (*ptr)[0] = (char)((l >> 24) & 0xFF); 604b22b933Srs200217 (*ptr)[1] = (char)((l >> 16) & 0xFF); 614b22b933Srs200217 (*ptr)[2] = (char)((l >> 8) & 0xFF); 624b22b933Srs200217 (*ptr)[3] = (char)((l ) & 0xFF); 634b22b933Srs200217 *ptr += sizeof(uint32_t); 644b22b933Srs200217 } 654b22b933Srs200217 66*5ffb0c9bSToomas Soome uint32_t get_uint32(const char **ptr, const char *end) 67*5ffb0c9bSToomas Soome { 68*5ffb0c9bSToomas Soome if (!*ptr || *ptr + sizeof(uint32_t) > end) 69*5ffb0c9bSToomas Soome { 70*5ffb0c9bSToomas Soome *ptr = NULL; 71*5ffb0c9bSToomas Soome return(0); 72*5ffb0c9bSToomas Soome } 73*5ffb0c9bSToomas Soome else 744b22b933Srs200217 { 754b22b933Srs200217 uint8_t *p = (uint8_t*) *ptr; 764b22b933Srs200217 *ptr += sizeof(uint32_t); 774b22b933Srs200217 return((uint32_t) ((uint32_t)p[0] << 24 | (uint32_t)p[1] << 16 | (uint32_t)p[2] << 8 | p[3])); 784b22b933Srs200217 } 79*5ffb0c9bSToomas Soome } 804b22b933Srs200217 81*5ffb0c9bSToomas Soome void put_uint16(uint16_t s, char **ptr) 824b22b933Srs200217 { 834b22b933Srs200217 (*ptr)[0] = (char)((s >> 8) & 0xFF); 844b22b933Srs200217 (*ptr)[1] = (char)((s ) & 0xFF); 854b22b933Srs200217 *ptr += sizeof(uint16_t); 864b22b933Srs200217 } 874b22b933Srs200217 88*5ffb0c9bSToomas Soome uint16_t get_uint16(const char **ptr, const char *end) 89*5ffb0c9bSToomas Soome { 90*5ffb0c9bSToomas Soome if (!*ptr || *ptr + sizeof(uint16_t) > end) 91*5ffb0c9bSToomas Soome { 92*5ffb0c9bSToomas Soome *ptr = NULL; 93*5ffb0c9bSToomas Soome return(0); 94*5ffb0c9bSToomas Soome } 95*5ffb0c9bSToomas Soome else 964b22b933Srs200217 { 974b22b933Srs200217 uint8_t *p = (uint8_t*) *ptr; 984b22b933Srs200217 *ptr += sizeof(uint16_t); 994b22b933Srs200217 return((uint16_t) ((uint16_t)p[0] << 8 | p[1])); 1004b22b933Srs200217 } 101*5ffb0c9bSToomas Soome } 1024b22b933Srs200217 1034b22b933Srs200217 int put_string(const char *str, char **ptr) 1044b22b933Srs200217 { 1054b22b933Srs200217 if (!str) str = ""; 1064b22b933Srs200217 strcpy(*ptr, str); 1074b22b933Srs200217 *ptr += strlen(str) + 1; 1084b22b933Srs200217 return 0; 1094b22b933Srs200217 } 1104b22b933Srs200217 111*5ffb0c9bSToomas Soome int get_string(const char **ptr, const char *const end, char *buffer, int buflen) 1124b22b933Srs200217 { 113*5ffb0c9bSToomas Soome if (!*ptr) 114*5ffb0c9bSToomas Soome { 115*5ffb0c9bSToomas Soome *buffer = 0; 116*5ffb0c9bSToomas Soome return(-1); 117*5ffb0c9bSToomas Soome } 118*5ffb0c9bSToomas Soome else 119*5ffb0c9bSToomas Soome { 120*5ffb0c9bSToomas Soome char *lim = buffer + buflen; // Calculate limit 121*5ffb0c9bSToomas Soome while (*ptr < end && buffer < lim) 122*5ffb0c9bSToomas Soome { 123*5ffb0c9bSToomas Soome char c = *buffer++ = *(*ptr)++; 124*5ffb0c9bSToomas Soome if (c == 0) return(0); // Success 125*5ffb0c9bSToomas Soome } 126*5ffb0c9bSToomas Soome if (buffer == lim) buffer--; 127*5ffb0c9bSToomas Soome *buffer = 0; // Failed, so terminate string, 128*5ffb0c9bSToomas Soome *ptr = NULL; // clear pointer, 129*5ffb0c9bSToomas Soome return(-1); // and return failure indication 130*5ffb0c9bSToomas Soome } 1314b22b933Srs200217 } 1324b22b933Srs200217 1334b22b933Srs200217 void put_rdata(const int rdlen, const unsigned char *rdata, char **ptr) 1344b22b933Srs200217 { 1354b22b933Srs200217 memcpy(*ptr, rdata, rdlen); 1364b22b933Srs200217 *ptr += rdlen; 1374b22b933Srs200217 } 1384b22b933Srs200217 139*5ffb0c9bSToomas Soome const char *get_rdata(const char **ptr, const char *end, int rdlen) 1404b22b933Srs200217 { 141*5ffb0c9bSToomas Soome if (!*ptr || *ptr + rdlen > end) 142*5ffb0c9bSToomas Soome { 143*5ffb0c9bSToomas Soome *ptr = NULL; 144*5ffb0c9bSToomas Soome return(0); 145*5ffb0c9bSToomas Soome } 146*5ffb0c9bSToomas Soome else 147*5ffb0c9bSToomas Soome { 148*5ffb0c9bSToomas Soome const char *rd = *ptr; 1494b22b933Srs200217 *ptr += rdlen; 1504b22b933Srs200217 return rd; 1514b22b933Srs200217 } 152*5ffb0c9bSToomas Soome } 1534b22b933Srs200217 1544b22b933Srs200217 void ConvertHeaderBytes(ipc_msg_hdr *hdr) 1554b22b933Srs200217 { 1564b22b933Srs200217 hdr->version = htonl(hdr->version); 1574b22b933Srs200217 hdr->datalen = htonl(hdr->datalen); 158*5ffb0c9bSToomas Soome hdr->ipc_flags = htonl(hdr->ipc_flags); 1594b22b933Srs200217 hdr->op = htonl(hdr->op ); 1604b22b933Srs200217 hdr->reg_index = htonl(hdr->reg_index); 1614b22b933Srs200217 } 162