Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 59494779 authored by Robert Greenwalt's avatar Robert Greenwalt
Browse files

Add ability to quote responses as needed.

It's up to the protocol to know when quotes are required.  In the future
the response should probably all be binary blobs with lengths.

bug:6353048
Change-Id: I3f9b48ab0f4e6746d75cdc9c0c84d33f38f63661
parent 545ab460
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -64,6 +64,9 @@ public:
    void incRef();
    bool decRef(); // returns true at 0 (but note: SocketClient already deleted)

    // return a new string in quotes with '\\' and '\"' escaped for "my arg" transmissions
    static char *quoteArg(const char *arg);

private:
    // Send null-terminated C strings
    int sendMsg(const char *msg);
+23 −0
Original line number Diff line number Diff line
@@ -107,6 +107,29 @@ int SocketClient::sendCode(int code) {
    return sendData(buf, sizeof(buf));
}

char *SocketClient::quoteArg(const char *arg) {
    int len = strlen(arg);
    char *result = (char *)malloc(len * 2 + 3);
    char *current = result;
    const char *end = arg + len;

    *(current++) = '"';
    while (arg < end) {
        switch (*arg) {
        case '\\':
        case '"':
            *(current++) = '\\'; // fallthrough
        default:
            *(current++) = *(arg++);
        }
    }
    *(current++) = '"';
    *(current++) = '\0';
    result = (char *)realloc(result, current-result);
    return result;
}


int SocketClient::sendMsg(const char *msg) {
    if (mSocket < 0) {
        errno = EHOSTUNREACH;