Debian Codegraph @

socket/image.c

raw symbols SCIP
1 /*
2 * Crossfire -- cooperative multi-player graphical RPG and adventure game
3 *
4 * Copyright (c) 1999-2014 Mark Wedel and the Crossfire Development Team
5 * Copyright (c) 1992 Frank Tore Johansen
6 *
7 * Crossfire is free software and comes with ABSOLUTELY NO WARRANTY. You are
8 * welcome to redistribute it under certain conditions. For details, please
9 * see COPYING and LICENSE.
10 *
11 * The authors can be reached via e-mail at <[email protected]>.
12 */
13
14 /** \file
15 * Image related communication
16 *
17 * \date 2006-03-18
18 *
19 * This file deals with the image related communication to the
20 * client. I've located all the functions in this file - this
21 * localizes it more, and means that we don't need to declare
22 * things like all the structures as globals.
23 */
24
25 #include "global.h"
26
27 #include <stdlib.h>
28 #include <string.h>
29
30 #include "image.h"
31 #include "newserver.h"
32 #include "shared/newclient.h"
33 #include "sproto.h"
34
35 /**
36 * Client has requested pixmap that it somehow missed getting.
37 * This will be called often if the client is
38 * caching images.
39 * @param buff data received from the client.
40 * @param len length of buff, ignored.
41 * @param ns socket to send data to.
42 */
43 void send_face_cmd(char *buff, int len, socket_struct *ns) {
44 long tmpnum;
45 uint16_t faceid;
46
47 if (len <= 0 || !buff) {
48 LOG(llevDebug, "IP '%s' sent bogus send_face_cmd information\n", ns->host);
49 return;
50 }
51
52 tmpnum = atoi(buff);
53 faceid = tmpnum&0xffff;
54
55 if (faceid != 0)
56 esrv_send_face(ns, get_face_by_id(faceid), 1);
57 }
58
59 /**
60 * Sends a face to a client if they are in pixmap mode,
61 * nothing gets sent in bitmap mode.
62 * If nocache is true (nonzero), ignore the cache setting from the client -
63 * this is needed for the askface, in which we really do want to send the
64 * face (and askface is the only place that should be setting it). Otherwise,
65 * we look at the facecache, and if set, send the image name.
66 * @param ns socket to send the date to.
67 * @param face face to send.
68 * @param nocache if 1 then send a 'image2', else depending on client cache setting.
69 */
70 void esrv_send_face(socket_struct *ns, const Face *face, int nocache) {
71 SockList sl;
72 int fallback;
73
74 if (face == NULL) {
75 LOG(llevError, "esrv_send_face NULL??\n");
76 return;
77 }
78
79 SockList_Init(&sl);
80 fallback = get_face_fallback(ns->faceset, face->number);
81
82 if (facesets[fallback].faces[face->number].data == NULL) {
83 LOG(llevError, "esrv_send_face: faces[%d].data == NULL\n", face->number);
84 return;
85 }
86
87 if (ns->facecache && !nocache) {
88 SockList_AddString(&sl, "face2 ");
89 SockList_AddShort(&sl, face->number);
90 SockList_AddChar(&sl, fallback);
91 SockList_AddInt(&sl, facesets[fallback].faces[face->number].checksum);
92 SockList_AddString(&sl, face->name);
93 Send_With_Handling(ns, &sl);
94 } else {
95 SockList_AddString(&sl, "image2 ");
96 SockList_AddInt(&sl, face->number);
97 SockList_AddChar(&sl, fallback);
98 SockList_AddInt(&sl, facesets[fallback].faces[face->number].datalen);
99 SockList_AddData(&sl, facesets[fallback].faces[face->number].data, facesets[fallback].faces[face->number].datalen);
100 Send_With_Handling(ns, &sl);
101 }
102 ns->faces_sent[face->number] |= NS_FACESENT_FACE;
103 SockList_Term(&sl);
104 }
105
106 /**
107 * Sends the number of images, checksum of the face file,
108 * and the image_info file information. See the doc/Developers/protocol
109 * if you want further detail.
110 * @param ns socket to send data to.
111 */
112 void send_image_info(socket_struct *ns) {
113 SockList sl;
114 int i;
115
116 SockList_Init(&sl);
117 SockList_AddPrintf(&sl, "replyinfo image_info\n%d\n%d\n", get_faces_count()-1, bmaps_checksum);
118 for (i = 0; i < MAX_FACE_SETS; i++) {
119 if (facesets[i].prefix) {
120 SockList_AddPrintf(&sl, "%d:%s:%s:%d:%s:%s:%s",
121 i, facesets[i].prefix, facesets[i].fullname,
122 facesets[i].fallback, facesets[i].size,
123 facesets[i].extension, facesets[i].comment);
124 }
125 }
126 Send_With_Handling(ns, &sl);
127 SockList_Term(&sl);
128 }
129
130 /**
131 * Sends requested face information.
132 * \param ns socket to send to
133 * \param params contains first and last index of face
134 *
135 * For each image in [start..stop] sends
136 * - checksum
137 * - name
138 */
139 void send_image_sums(socket_struct *ns, char *params) {
140 unsigned int start, stop;
141 unsigned short i;
142 char *cp;
143 SockList sl;
144
145 SockList_Init(&sl);
146
147 start = atoi(params);
148 for (cp = params; *cp != '\0'; cp++)
149 if (*cp == ' ')
150 break;
151
152 stop = atoi(cp);
153 if (stop < start
154 || *cp == '\0'
155 || (stop-start) > 1000
156 || stop >= get_faces_count()) {
157 SockList_AddPrintf(&sl, "replyinfo image_sums %d %d", start, stop);
158 Send_With_Handling(ns, &sl);
159 SockList_Term(&sl);
160 return;
161 }
162 SockList_AddPrintf(&sl, "replyinfo image_sums %d %d ", start, stop);
163
164 for (i = start; i <= stop; i++) {
165 int faceset;
166 const Face *face = get_face_by_index(i);
167
168 if (SockList_Avail(&sl) < 2+4+1+1+strlen(face->name)+1) {
169 LOG(llevError, "send_image_sums: buffer overflow, rejecting range %d..%d\n", start, stop);
170 SockList_Reset(&sl);
171 SockList_AddPrintf(&sl, "replyinfo image_sums %d %d", start, stop);
172 Send_With_Handling(ns, &sl);
173 SockList_Term(&sl);
174 return;
175 }
176
177 SockList_AddShort(&sl, i);
178 ns->faces_sent[face->number] |= NS_FACESENT_FACE;
179
180 faceset = get_face_fallback(ns->faceset, i);
181 SockList_AddInt(&sl, facesets[faceset].faces[i].checksum);
182 SockList_AddChar(&sl, faceset);
183 SockList_AddLen8Data(&sl, face->name, strlen(face->name)+1);
184 }
185 Send_With_Handling(ns, &sl);
186 SockList_Term(&sl);
187 }