blob: 131510c80e44b31aeaf949a1080d92132d3b523f [file] [log] [blame]
Robert Greenwalt47e4ceb2012-03-26 15:36:57 -07001/* -*- Mode: C; tab-width: 4 -*-
2 *
3 * Copyright (c) 2003-2004, Apple Computer, Inc. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of its
14 * contributors may be used to endorse or promote products derived from this
15 * software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include "dnssd_ipc.h"
30
31#if defined(_WIN32)
32
33char *win32_strerror(int inErrorCode)
34 {
35 static char buffer[1024];
36 DWORD n;
37 memset(buffer, 0, sizeof(buffer));
38 n = FormatMessageA(
39 FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
40 NULL,
41 (DWORD) inErrorCode,
42 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
43 buffer,
44 sizeof(buffer),
45 NULL);
46 if (n > 0)
47 {
48 // Remove any trailing CR's or LF's since some messages have them.
49 while ((n > 0) && isspace(((unsigned char *) buffer)[n - 1]))
50 buffer[--n] = '\0';
51 }
52 return buffer;
53 }
54
55#endif
56
57void put_uint32(const uint32_t l, char **ptr)
58 {
59 (*ptr)[0] = (char)((l >> 24) & 0xFF);
60 (*ptr)[1] = (char)((l >> 16) & 0xFF);
61 (*ptr)[2] = (char)((l >> 8) & 0xFF);
62 (*ptr)[3] = (char)((l ) & 0xFF);
63 *ptr += sizeof(uint32_t);
64 }
65
66uint32_t get_uint32(const char **ptr, const char *end)
67 {
68 if (!*ptr || *ptr + sizeof(uint32_t) > end)
69 {
70 *ptr = NULL;
71 return(0);
72 }
73 else
74 {
75 uint8_t *p = (uint8_t*) *ptr;
76 *ptr += sizeof(uint32_t);
77 return((uint32_t) ((uint32_t)p[0] << 24 | (uint32_t)p[1] << 16 | (uint32_t)p[2] << 8 | p[3]));
78 }
79 }
80
81void put_uint16(uint16_t s, char **ptr)
82 {
83 (*ptr)[0] = (char)((s >> 8) & 0xFF);
84 (*ptr)[1] = (char)((s ) & 0xFF);
85 *ptr += sizeof(uint16_t);
86 }
87
88uint16_t get_uint16(const char **ptr, const char *end)
89 {
90 if (!*ptr || *ptr + sizeof(uint16_t) > end)
91 {
92 *ptr = NULL;
93 return(0);
94 }
95 else
96 {
97 uint8_t *p = (uint8_t*) *ptr;
98 *ptr += sizeof(uint16_t);
99 return((uint16_t) ((uint16_t)p[0] << 8 | p[1]));
100 }
101 }
102
103int put_string(const char *str, char **ptr)
104 {
105 if (!str) str = "";
106 strcpy(*ptr, str);
107 *ptr += strlen(str) + 1;
108 return 0;
109 }
110
111int get_string(const char **ptr, const char *const end, char *buffer, int buflen)
112 {
113 if (!*ptr)
114 {
115 *buffer = 0;
116 return(-1);
117 }
118 else
119 {
120 char *lim = buffer + buflen; // Calculate limit
121 while (*ptr < end && buffer < lim)
122 {
123 char c = *buffer++ = *(*ptr)++;
124 if (c == 0) return(0); // Success
125 }
126 if (buffer == lim) buffer--;
127 *buffer = 0; // Failed, so terminate string,
128 *ptr = NULL; // clear pointer,
129 return(-1); // and return failure indication
130 }
131 }
132
133void put_rdata(const int rdlen, const unsigned char *rdata, char **ptr)
134 {
135 memcpy(*ptr, rdata, rdlen);
136 *ptr += rdlen;
137 }
138
139const char *get_rdata(const char **ptr, const char *end, int rdlen)
140 {
141 if (!*ptr || *ptr + rdlen > end)
142 {
143 *ptr = NULL;
144 return(0);
145 }
146 else
147 {
148 const char *rd = *ptr;
149 *ptr += rdlen;
150 return rd;
151 }
152 }
153
154void ConvertHeaderBytes(ipc_msg_hdr *hdr)
155 {
156 hdr->version = htonl(hdr->version);
157 hdr->datalen = htonl(hdr->datalen);
158 hdr->ipc_flags = htonl(hdr->ipc_flags);
159 hdr->op = htonl(hdr->op );
160 hdr->reg_index = htonl(hdr->reg_index);
161 }