common: prevent buffer overflow
[supercollider.git] / server / scsynth / SC_Lib.cpp
blob43d102c8258e644b2627527a400872012c88b134
1 /*
2 SuperCollider real time audio synthesis system
3 Copyright (c) 2002 James McCartney. All rights reserved.
4 http://www.audiosynth.com
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "SC_Lib.h"
23 #include "SC_Lib_Cintf.h"
24 #include "SC_ComPort.h"
25 #include "SC_SequencedCommand.h"
26 #include "scsynthsend.h"
27 #include <string.h>
28 #include <ctype.h>
29 #include "SC_Prototypes.h"
30 #include "SC_Str4.h"
33 void SendDone(ReplyAddress *inReply, const char *inCommandName)
35 small_scpacket packet;
36 packet.adds("/done");
37 packet.maketags(2);
38 packet.addtag(',');
39 packet.addtag('s');
40 packet.adds(inCommandName);
41 SendReply(inReply, packet.data(), packet.size());
44 void SendDoneWithIntValue(ReplyAddress *inReply, const char *inCommandName, int value)
46 small_scpacket packet;
47 packet.adds("/done");
48 packet.maketags(3);
49 packet.addtag(',');
50 packet.addtag('s');
51 packet.adds(inCommandName);
52 packet.addtag('i');
53 packet.addi(value);
54 SendReply(inReply, packet.data(), packet.size());
57 void SendFailure(ReplyAddress *inReply, const char *inCommandName, const char *errString)
59 small_scpacket packet;
60 packet.adds("/fail");
61 packet.maketags(3);
62 packet.addtag(',');
63 packet.addtag('s');
64 packet.addtag('s');
65 packet.adds(inCommandName);
66 packet.adds(errString);
67 SendReply(inReply, packet.data(), packet.size());
70 void SendFailureWithBufnum(ReplyAddress *inReply, const char *inCommandName, const char *errString, uint32 index)
72 small_scpacket packet;
73 packet.adds("/fail");
74 packet.maketags(4);
75 packet.addtag(',');
76 packet.addtag('s');
77 packet.addtag('s');
78 packet.adds(inCommandName);
79 packet.adds(errString);
80 packet.addtag('i');
81 packet.addi((int)index);
82 SendReply(inReply, packet.data(), packet.size());
85 void ReportLateness(ReplyAddress *inReply, float32 seconds)
87 small_scpacket packet;
88 packet.adds("/late");
89 packet.maketags(2);
90 packet.addtag(',');
91 packet.addtag('f');
92 packet.addf(seconds);
93 SendReply(inReply, packet.data(), packet.size());
96 SC_NamedObj::SC_NamedObj()
98 SetName("?");
101 SC_NamedObj::~SC_NamedObj()
104 void SC_NamedObj::SetName(const int32 *inName)
106 if (str4len(inName) > (int)kSCNameLen) return;
107 str4cpy(mName, inName);
108 mHash = Hash(mName);
111 void SC_NamedObj::SetName(const char *inName)
113 if (str4len(inName) > (int)kSCNameLen) return;
114 str4cpy(mName, inName);
115 mHash = Hash(mName);
118 SC_LibCmd::SC_LibCmd(SC_CommandFunc inFunc) : mFunc(inFunc)
121 SCErr SC_LibCmd::Perform(struct World *inWorld, int inSize, char *inData, ReplyAddress *inReply)
123 SCErr err;
124 // int kSendError = 1; // i.e., 0x01 | 0x02;
125 try {
126 err = (mFunc)(inWorld, inSize, inData, inReply);
127 } catch (int iexc) {
128 err = iexc;
129 } catch (std::exception& exc) {
130 if(inWorld->mLocalErrorNotification <= 0 && inWorld->mErrorNotification) {
131 CallSendFailureCommand(inWorld, (char*)Name(), exc.what(), inReply);
132 scprintf("FAILURE %s %s\n", (char*)Name(), exc.what());
134 return kSCErr_Failed;
135 } catch (...) {
136 err = kSCErr_Failed;
138 if (err && (inWorld->mLocalErrorNotification <= 0 && inWorld->mErrorNotification)) {
139 char errstr[25];
140 SC_ErrorString(err, errstr);
141 CallSendFailureCommand(inWorld, (char*)Name(), errstr, inReply);
142 scprintf("FAILURE %s %s\n", (char*)Name(), errstr);
144 return err;
147 SCErr NewCommand(const char *inPath, uint32 inCommandNumber, SC_CommandFunc inFunc)
149 char path[256];
150 sprintf(path, "/%s", inPath);
152 SC_LibCmd *cmd = new SC_LibCmd(inFunc);
153 cmd->SetName(path);
154 gCmdLib->Add(cmd);
156 // support OSC commands without the leading slash
157 SC_LibCmd *cmd2 = new SC_LibCmd(inFunc);
158 cmd2->SetName(inPath);
159 gCmdLib->Add(cmd2);
161 // support integer OSC commands
162 gCmdArray[inCommandNumber] = cmd;
164 return kSCErr_None;