MockDLL.c~ 7.48 KB
Newer Older
Steve Arendt's avatar
Steve Arendt committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349

// COPYRIGHT_BEGIN
//  DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
//
//  Copyright (C) 2008-2011, Cable Television Laboratories, Inc.
//
//  This program is free software; you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, version 2. This program is distributed
//  in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
//  even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
//  PURPOSE. See the GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License along
//  with this program.  If not, see  <http://www.gnu.org/licenses/>.
//
//  Please contact CableLabs if you need additional information or
//  have any questions.
//
//      CableLabs
//      858 Coal Creek Cir
//      Louisville, CO 80027-9750
//      303 661-9100
//      oc-mail@cablelabs.com
//
//  If you or the company you represent has a separate agreement with CableLabs
//  concerning the use of this code, your rights and obligations with respect
//  to this code shall be as set forth therein. No license is granted hereunder
//  for any other purpose.
// COPYRIGHT_END


#include "mock_sync.h"

#include <stdio.h>
#include <string.h> // memcpy, strrchr, strlen

#if defined( WIN32 )
    #define snprintf _snprintf
#endif


#include "MockDLL.h"

#define DTCPIP_SERVER_SESSION_START 65536
#define DTCPIP_SERVER_SESSIONS_MAX  sizeof(uint32_t)

static struct
{
    mock_Mutex mutex;
    uint32_t  sessions;
}
server_data;



int dtcpip_cmn_init(const char* storage_path)
{
    return 0;
}

void dtcpip_cmn_get_version(char* string, size_t length)
{
    snprintf(string, length, "NO-OP DTCP-IP library for OCAP Reference Implementation.");
}

int dtcpip_src_init(unsigned short dtcp_port)
{
    int ret_code = -1;

    if (mock_mutexNew(&server_data.mutex) != MPE_SUCCESS)
    {
        ret_code = -2;
    }
    else
    {
        ret_code = 0;
    }

    return ret_code;
}

int dtcpip_src_open(int* session_handle, int is_audio_only)
{
    int ret_code = -1;


    if (session_handle == NULL)
    {
        ret_code = -2;
    }
    else
    {
        int i = 0;
        uint32_t session = 0;

        mock_mutexAcquire(server_data.mutex);

        for (i = 0; i < DTCPIP_SERVER_SESSIONS_MAX; i++)
        {
            session = 1 << i;
            if ((server_data.sessions & session) == 0)
            {
                break;
            }
        }

        if (i == DTCPIP_SERVER_SESSIONS_MAX)
        {
            ret_code = -3;
        }
        else
        {
            server_data.sessions ^= session;
            *session_handle = (int) (DTCPIP_SERVER_SESSION_START + i);
            ret_code = 0;
        }
        mock_mutexRelease(server_data.mutex);
    }

    return ret_code;
}

int dtcpip_src_alloc_encrypt(int session_handle,
                 unsigned char cci,
                 char* cleartext_data, size_t cleartext_size,
                 char** encrypted_data, size_t* encrypted_size)
{
    int ret_code = -1;

    uint32_t session = 1 << ((uint32_t) session_handle - DTCPIP_SERVER_SESSION_START);

    mock_mutexAcquire(server_data.mutex);

    if ((server_data.sessions & session) == 0)
    {
        ret_code = -2;
    }
    else if (cleartext_data == NULL && cleartext_size > 0) // only read if we have data
    {
        ret_code = -3;
    }
    else if (encrypted_data == NULL && cleartext_size > 0) // only written if we have data
    {
        ret_code = -4;
    }
    else if (encrypted_size == NULL) // always written
    {
        ret_code = -5;
    }
    else if (cleartext_size == 0)
    {
        *encrypted_size = 0;
        ret_code = 0;
    }
    else if ((*encrypted_data = malloc(cleartext_size)) == NULL)
    {
        ret_code = -6;
    }
    else
    {
        memcpy(*encrypted_data, cleartext_data, cleartext_size);
        *encrypted_size = cleartext_size;
        ret_code = 0;
    }

    mock_mutexRelease(server_data.mutex);

    return ret_code;
}

int dtcpip_src_free(char* encrypted_data)
{
    free((void *)encrypted_data);
    return 0;
}

int dtcpip_src_close(int session_handle)
{
    int ret_code = -1;

    uint32_t session = 1 << ((uint32_t) session_handle - DTCPIP_SERVER_SESSION_START);


    mock_mutexAcquire(server_data.mutex);

    if ((server_data.sessions & session) == 0)
    {
        ret_code = -2;
    }
    else
    {
        server_data.sessions ^= session;
        ret_code = 0;
    }

    mock_mutexRelease(server_data.mutex);

    return ret_code;
}

#define DTCPIP_PLAYER_SESSION_START 16777216
#define DTCPIP_PLAYER_SESSIONS_MAX  sizeof(uint32_t)

static struct
{
    mock_Mutex mutex;
    uint32_t  sessions;
}
player_data;

int dtcpip_snk_init(void)
{
    int ret_code = -1;

    if (mock_mutexNew(&player_data.mutex) != MPE_SUCCESS)
    {
        ret_code = -2;
    }
    else
    {
        player_data.sessions = 0;
        ret_code = 0;
    }

    return ret_code;
}

int dtcpip_snk_open(
                 char* ip_addr, unsigned short ip_port,
                 int *session_handle)
{
    int ret_code = -1;

    if (ip_addr == NULL)
    {
        ret_code = -2;
    }
    else if (session_handle == NULL)
    {
        ret_code = -3;
    }
    else
    {
        int i = 0;
        uint32_t session = 0;

        mock_mutexAcquire(player_data.mutex);

        for (i = 0; i < DTCPIP_PLAYER_SESSIONS_MAX; i++)
        {
            session = 1 << i;
            if ((player_data.sessions & session) == 0)
            {
                break;
            }
        }

        if (i == DTCPIP_PLAYER_SESSIONS_MAX)
        {
            ret_code = -4;
        }
        else
        {
            player_data.sessions ^= session;
            *session_handle = (int) (DTCPIP_PLAYER_SESSION_START + i);
            ret_code = 0;
        }
        mock_mutexRelease(player_data.mutex);
    }

    return ret_code;
}

int dtcpip_snk_alloc_decrypt(int session_handle,
                 char* encrypted_data, size_t encrypted_size,
                 char** cleartext_data, size_t* cleartext_size)
{
    int ret_code = -1;

    uint32_t session = 1 << ((uint32_t) session_handle - DTCPIP_PLAYER_SESSION_START);

    mock_mutexAcquire(player_data.mutex);

    if ((player_data.sessions & session) == 0)
    {
        ret_code = -2;
    }
    else if (encrypted_data == NULL && encrypted_size > 0) // only read if we have data
    {
        ret_code = -3;
    }
    else if (cleartext_data == NULL && encrypted_size > 0) // only written if we have data
    {
        ret_code = -4;
    }
    else if (cleartext_size == NULL) // always written
    {
        ret_code = -5;
    }
    else if (encrypted_size == 0)
    {
        *cleartext_size = 0;
        ret_code = 0;
    }
    else if ((*cleartext_data = malloc(encrypted_size)) == NULL)
    {
        ret_code = -6;
    }
    else
    {
        memcpy(*cleartext_data, encrypted_data, encrypted_size);
        *cleartext_size = encrypted_size;
        ret_code = 0;
    }

    mock_mutexRelease(player_data.mutex);

    return ret_code;
}

int dtcpip_snk_free(char* cleartext_data)
{
    free(cleartext_data);
    return 0;
}

int dtcpip_snk_close(int session_handle)
{
    int ret_code = -1;

    uint32_t session = 1 << ((uint32_t) session_handle - DTCPIP_PLAYER_SESSION_START);

    mock_mutexAcquire(player_data.mutex);

    if ((player_data.sessions & session) == 0)
    {
        ret_code = -2;
    }
    else
    {
        player_data.sessions ^= session;
        ret_code = 0;
    }

    mock_mutexRelease(player_data.mutex);

    return ret_code;
}