File: AGCommandProcessor.c

package info (click to toggle)
agsync 0.2-pre-9
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 2,044 kB
  • ctags: 1,183
  • sloc: ansic: 9,979; sh: 8,390; makefile: 86
file content (917 lines) | stat: -rw-r--r-- 29,644 bytes parent folder | download | duplicates (12)
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
/* The contents of this file are subject to the Mozilla Public License
 * Version 1.0 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 * License for the specific language governing rights and limitations
 * under the License.
 *
 * The Original Code is Mobile Application Link.
 *
 * The Initial Developer of the Original Code is AvantGo, Inc.
 * Portions created by AvantGo, Inc. are Copyright (C) 1997-1999
 * AvantGo, Inc. All Rights Reserved.
 *
 * Contributor(s):
 */

#include <AGCommandProcessor.h>
#include <AGUtil.h>
#include <AGDigest.h>

ExportFunc AGCommandProcessor *AGCommandProcessorNew(AGServerConfig *server)
{
    AGCommandProcessor *tmp;
    tmp = (AGCommandProcessor *)malloc(sizeof(AGCommandProcessor));
    if(tmp)
        AGCommandProcessorInit(tmp, server);
    return tmp;
}

ExportFunc AGCommandProcessor *AGCommandProcessorInit(
                                            AGCommandProcessor *processor,
                                            AGServerConfig *server)
{
    bzero(processor, sizeof(AGCommandProcessor));
    processor->serverConfig = server;
    return processor;
}

ExportFunc void AGCommandProcessorFinalize(AGCommandProcessor *processor)
{
    bzero(processor, sizeof(AGCommandProcessor));
}

ExportFunc void AGCommandProcessorFree(AGCommandProcessor *processor)
{
    AGCommandProcessorFinalize(processor);
    free(processor);
}

ExportFunc int32 AGCommandProcessorStart(AGCommandProcessor *processor)
{
    processor->syncAgain = FALSE;
    return 0;
}

ExportFunc AGBool AGCommandProcessorShouldSyncAgain(
                                                AGCommandProcessor *processor)
{
    return processor->syncAgain;
}

ExportFunc AGPerformCommandFunc AGCommandProcessorGetPerformFunc(
                                                AGCommandProcessor *processor)
{
    return (AGPerformCommandFunc)&AGCPPerformCommand;
}

static int32 parseDATABASECONFIG(void *out, AGReader *r, 
                                                int32 len, int32 *errCode)
{
    char *dbname;
    AGBool sendRecordPlatformData;
    AGDBConfigType config;
    int32 platformDataLength;
    void *platformData;
    int32 result;

    AGReadDATABASECONFIG(r, &dbname, 
                            &config, 
                            &sendRecordPlatformData, 
                            &platformDataLength, 
                            &platformData);

    result = AGCPDatabaseConfig((AGCommandProcessor *)out, errCode, 
                                dbname, config, sendRecordPlatformData,
                                platformDataLength, platformData);
    if(dbname)
        free(dbname);
    if(platformDataLength)
        free(platformData);
    return result;
}

ExportFunc int32 AGCPDatabaseConfig(AGCommandProcessor *out, 
                                     int32 *returnErrorCode,
                                     char *dbname, 
                                     AGDBConfigType config, 
                                     AGBool sendRecordPlatformData, 
                                     int32 platformDataLength,
                                     void *platformData)
{
    AGDBConfig *dbconfig = NULL;
    void *tmp = NULL;

    if(!dbname) {
        return AGCLIENT_ERR;
    }

    if(config == AG_DONTSEND_CFG) {
        dbconfig = AGServerConfigDeleteDBConfigNamed(out->serverConfig, dbname);
        if(dbconfig)
            AGDBConfigFree(dbconfig);
    } else {
        if(platformDataLength) {
            tmp = malloc(platformDataLength);
            bcopy(platformData, tmp, platformDataLength);
        }
    
        dbconfig = AGDBConfigNew(strdup(dbname), config, 
                                sendRecordPlatformData, 
                                platformDataLength, tmp, NULL);
        AGServerConfigAddDBConfig(out->serverConfig, dbconfig);
    }
    return AGCLIENT_CONTINUE;
}

static int32 parseSERVERCONFIG(void *out, AGReader *r, 
                                int32 len, int32 *errCode)
{
    char *friendlyName = NULL, *userUrl = NULL, *message = NULL, 
        *serverUri = NULL;
    AGBool clientShouldHashPasswords, allowSecureClientConnect;
    uint32 connectTimeout, writeTimeout, readTimeout;
    int32 result;

    AGReadSERVERCONFIG(r, &friendlyName, &userUrl,  &message, &serverUri,
                       &clientShouldHashPasswords,
                       &allowSecureClientConnect,
                       &connectTimeout,
                       &writeTimeout, 
                       &readTimeout);

    result = AGCPServerConfig((AGCommandProcessor *)out, 
                            errCode,
                            friendlyName, 
                            userUrl,
                            message,
                            serverUri, 
                            clientShouldHashPasswords, 
                            allowSecureClientConnect, 
                            connectTimeout, 
                            writeTimeout,
                            readTimeout);
    if (friendlyName)
        free(friendlyName);
    if (userUrl)
        free(userUrl);
    if (message)
        free(message);
    if (serverUri)
        free(serverUri);
    return result;
}

ExportFunc int32 AGCPServerConfig(AGCommandProcessor *out, 
                                  int32 *returnErrorCode,
                                  char *friendlyName, 
                                  char *userUrl,
                                  char *message,
                                  char *serverUri, 
                                  AGBool clientShouldHashPasswords, 
                                  AGBool allowSecureClientConnect, 
                                  uint32 connectTimeout, 
                                  uint32 writeTimeout, 
                                  uint32 readTimeout)
{
    if(!out->serverConfig) {
        return AGCLIENT_ERR;    
    }

    if (out->serverConfig->friendlyName)
        free(out->serverConfig->friendlyName);
    if (out->serverConfig->userUrl)
        free(out->serverConfig->userUrl);
    if (out->serverConfig->description)
        free(out->serverConfig->description);
    if (out->serverConfig->serverUri)
        free(out->serverConfig->serverUri);
    out->serverConfig->friendlyName = NULL;
    out->serverConfig->userUrl = NULL;
    out->serverConfig->description = NULL;
    out->serverConfig->serverUri = NULL;

    if (friendlyName)
        out->serverConfig->friendlyName = strdup(friendlyName);
    if(userUrl)
        out->serverConfig->userUrl = strdup(userUrl);
    if(message)
       out->serverConfig->description = strdup(message);
    if(serverUri)
       out->serverConfig->serverUri = strdup(serverUri);
    AGServerConfigChangeHashPasswordState(out->serverConfig,
        (uint8)(clientShouldHashPasswords
        ? AG_HASH_PASSWORD_YES
        : AG_HASH_PASSWORD_NO));
    out->serverConfig->allowSecureConnection = allowSecureClientConnect;
    out->serverConfig->connectTimeout = connectTimeout;
    out->serverConfig->writeTimeout = writeTimeout;
    out->serverConfig->readTimeout = readTimeout;
    return AGCLIENT_CONTINUE;
}

static int32 parseCOOKIE(void *out, AGReader *r, int32 len, int32 *errCode)
{
    int32 cookieLen = 0, result;
    void *cookie;

    AGReadCOOKIE(r, &cookieLen, &cookie);
    result = AGCPCookie((AGCommandProcessor *)out, errCode,
                        cookieLen, cookie);
    if(cookieLen)
        free(cookie);
    return result;
}

ExportFunc int32 AGCPCookie(AGCommandProcessor *out, 
                                     int32 *returnErrorCode,
                                     int32 cookieLength,
                                     void *cookie)
{
    void *tmp = NULL;

    if(!out->serverConfig) {
        return AGCLIENT_ERR;
    }

    if(out->serverConfig->sequenceCookie != NULL) {
        free(out->serverConfig->sequenceCookie);
        out->serverConfig->sequenceCookie = NULL;
        out->serverConfig->sequenceCookieLength = 0;
    }
    
    if(cookieLength) {
        tmp = malloc(cookieLength);
        bcopy(cookie, tmp, cookieLength);
    }
    out->serverConfig->sequenceCookie = (uint8*)tmp;
    out->serverConfig->sequenceCookieLength = cookieLength;
    return AGCLIENT_CONTINUE;
}

static int32 parseNONCE(void *out, AGReader *r, int32 len, int32 *errCode)
{
    uint8 nonce[16];

    AGReadNONCE(r, nonce);
    return AGCPNonce((AGCommandProcessor *)out, errCode, nonce);
}

ExportFunc int32 AGCPNonce(AGCommandProcessor *out, 
                                    int32 *returnErrorCode,
                                    uint8 nonce[16])
{
    if(!out->serverConfig)
        return AGCLIENT_ERR;

    if(AG_HASH_PASSWORD_UNKNOWN == out->serverConfig->hashPassword) {
        AGServerConfigChangeHashPasswordState(out->serverConfig,
            (uint8)(AGDigestNull(nonce)
            ? AG_HASH_PASSWORD_NO
            : AG_HASH_PASSWORD_YES));
    }

    bcopy(nonce, out->serverConfig->nonce, 16);
    return AGCLIENT_CONTINUE;
}

static int32 parseTASK(void *out, AGReader *r, int32 len, int32 *errCode)
{
    char *currentTask = NULL;
    int32 result;
    AGBool bufferable = FALSE;

    AGReadTASK(r, &currentTask, &bufferable);
    result = AGCPTask((AGCommandProcessor *)out, errCode, currentTask, bufferable);
    if(currentTask)
        free(currentTask);
    return result;
}

ExportFunc int32 AGCPTask(AGCommandProcessor *out, 
                                    int32 *returnErrorCode,
                                    char *currentTask,
                                    AGBool bufferable)
{
    int32 rc = AGCLIENT_CONTINUE;

    if(out->commands.performTaskFunc) {
        rc = (*out->commands.performTaskFunc)(out->commands.out, 
                                        returnErrorCode, currentTask, bufferable);
    }
    return rc;
}


static int32 parseITEM(void *out, AGReader *r, int32 len, int32 *errCode)
{
    int32 currentItemNumber = 0;
    int32 totalItemCount = 0;
    char *currentItem = NULL;
    int32 result;

    AGReadITEM(r, &currentItemNumber, &totalItemCount, &currentItem);
    result = AGCPItem((AGCommandProcessor *)out, errCode,
                        currentItemNumber,
                        totalItemCount, 
                        currentItem);
    if(currentItem)
        free(currentItem);
    return result;
}

ExportFunc int32 AGCPItem(AGCommandProcessor *out, 
                            int32 *returnErrorCode,
                            int32 currentItemNumber,
                            int32 totalItemCount,
                            char *currentItem)
{
    int32 rc = AGCLIENT_CONTINUE;

    if(out->commands.performItemFunc) {
        rc = (*out->commands.performItemFunc)(out->commands.out, returnErrorCode, 
                                currentItemNumber, totalItemCount, currentItem);
    }
    return rc;
}

static int32 parseDELETEDATABASE(void *out, AGReader *r,
                                    int32 len, int32 *errCode)
{
    char *dbname;
    int32 result;

    AGReadDELETEDATABASE(r, &dbname);
    result = AGCPDeleteDatabase((AGCommandProcessor *)out, errCode, dbname);
    if(dbname)
        free(dbname);
    return result;
}

ExportFunc int32 AGCPDeleteDatabase(AGCommandProcessor *out, 
                                            int32 *returnErrorCode,
                                             char *dbname)
{
    int32 rc = AGCLIENT_CONTINUE;

    if(out->commands.performDeleteDatabaseFunc) {
        rc = (*out->commands.performDeleteDatabaseFunc)(out->commands.out, 
                                                    returnErrorCode, dbname);
    }
    return rc;
}


static int32 parseOPENDATABASE(void *out, AGReader *r, int32 len, int32 *errCode)
{
    char *dbname = NULL;
    int32 result;

    AGReadOPENDATABASE(r, &dbname);
    result = AGCPOpenDatabase((AGCommandProcessor *)out, errCode, dbname);
    if(dbname)
       free(dbname);
    return result;
}

ExportFunc int32 AGCPOpenDatabase(AGCommandProcessor *out, 
                                            int32 *returnErrorCode,
                                            char *dbname)
{
    int32 rc = AGCLIENT_CONTINUE;

    out->currentDb = AGServerConfigGetDBConfigNamed(out->serverConfig,
                                                            dbname);
    if(!out->currentDb)
        return rc;

    if(out->commands.performOpenDatabaseFunc) {
        rc = (*out->commands.performOpenDatabaseFunc)(out->commands.out, 
                                                    returnErrorCode, dbname);
    }

    // if we're getting an openDB from the server, that must mean
    // that they've gotten our last set of newids. Clear them now,
    // so that subsequent RECR commands can add them properly.
    if(out->currentDb) {
        AGDBConfigSetNewIds(out->currentDb, NULL);
    }
    return rc;
}


static int32 parseCLOSEDATABASE(void *out, AGReader *r, int32 len, int32 *errCode)
{
    AGReadCLOSEDATABASE(r);
    return AGCPCloseDatabase((AGCommandProcessor *)out, errCode);
}

ExportFunc int32 AGCPCloseDatabase(AGCommandProcessor *out, 
                                                    int32 *returnErrorCode)
{
    int32 rc = AGCLIENT_CONTINUE;

    if(out->commands.performCloseDatabaseFunc) {
        rc = (*out->commands.performCloseDatabaseFunc)(out->commands.out, 
                                                            returnErrorCode);
    }
    return rc;
}

static int32 parseCLEARMODS(void *out, AGReader *r, int32 len, int32 *errCode)
{
    AGReadCLEARMODS(r);
    return AGCPClearMods((AGCommandProcessor *)out, errCode);
}

ExportFunc int32 AGCPClearMods(AGCommandProcessor *out, 
                                                    int32 *returnErrorCode)
{
    int32 rc = AGCLIENT_CONTINUE;

    if(out->commands.performClearModsFunc) {
        rc = (*out->commands.performClearModsFunc)(out->commands.out, 
                                                            returnErrorCode);
    }
    return rc;
}

static int32 parseGOODBYE(void *out, AGReader *r, int32 len, int32 *errCode)
{
    AGSyncStatus syncStatus = AG_DONE_STATUS;
    int32 errorCode = 0, result;
    char *errorMessage = NULL;

    AGReadGOODBYE(r, &syncStatus, &errorCode, &errorMessage);
    result = AGCPGoodbye((AGCommandProcessor *)out, errCode, syncStatus, 
                    errorCode, errorMessage);
    if(errorMessage)
       free(errorMessage);
    return result;
}

ExportFunc int32 AGCPGoodbye(AGCommandProcessor *out, 
                                int32 *returnErrorCode,
                                AGSyncStatus syncStatus,
                                int32 errorCode,
                                char *errorMessage)
{
    int32 rc = AGCLIENT_CONTINUE;

    if(out->commands.performGoodbyeFunc) {
        rc = (*out->commands.performGoodbyeFunc)(out->commands.out, 
                                returnErrorCode,
                                syncStatus, errorCode, errorMessage);
    }

    if(syncStatus == AG_CALLAGAIN_STATUS)
        out->syncAgain = TRUE;
    return rc;
}

static int32 parseSENDDEVICEINFO(void *out, AGReader *r, 
                                    int32 len, int32 *errCode)
{
    AGBool send;

    AGReadSENDDEVICEINFO(r, &send);
    return AGCPSendDeviceInfo((AGCommandProcessor *)out, errCode, send);
}

ExportFunc int32 AGCPSendDeviceInfo(AGCommandProcessor *out, 
                                             int32 *returnErrorCode,
                                             AGBool send)
{
    out->serverConfig->sendDeviceInfo = send;
    return AGCLIENT_CONTINUE;
}

static int32 parseRECORD(void *out, AGReader *r, int32 len, int32 *errCode)
{
    int32 uid, newId = 0;
    AGRecordStatus mod;
    int32 recordDataLength, result;
    void *recordData;
    int32 platformDataLength;
    void *platformData;

#ifndef __palmos__
    AGReadRECORD(r, &uid, &mod,
        &recordDataLength, &recordData,
        &platformDataLength, &platformData);
#else
    AGReadRECORDNoCopy((AGBufferReader *)r, &uid, &mod,
                    &recordDataLength, &recordData,
                    &platformDataLength, &platformData);
#endif
    result = AGCPRecord((AGCommandProcessor *)out, errCode, &newId,
                                     uid,
                                     mod,
                                     recordDataLength,
                                     recordData,
                                     platformDataLength,
                                     platformData);

#ifndef __palmos__
    if(recordData)
       free(recordData);
    if(platformData)
       free(platformData);
#else
    // __palmos__ did not copy the recordData and platformData
    // so don't free it
#endif

    return result;
}

ExportFunc int32 AGCPRecord(AGCommandProcessor *out, 
                                     int32 *returnErrorCode,
                                     int32 *newUID,
                                     int32 uid,
                                     AGRecordStatus mod,
                                     int32 recordDataLength,
                                     void *recordData,
                                     int32 platformDataLength,
                                     void *platformData)
{
    int32 rc = AGCLIENT_CONTINUE;

    if(out->commands.performRecordFunc) {
        rc = (*out->commands.performRecordFunc)(out->commands.out,
                                     returnErrorCode,
                                     newUID,
                                     uid,
                                     mod,
                                     recordDataLength,
                                     recordData,
                                     platformDataLength,
                                     platformData);
    }

    if(mod == AG_RECORD_NEW_TEMPORARY_UID) {
        //NOTE: It needs to record this mapping between the temporary and
        //the real uid to the dbconfig.
        if(out->currentDb) {
            AGDBConfigAppendNewId(out->currentDb, uid, *newUID);
        }
    }
    return rc;
}


static int32 parseEXPANSION(void *out, AGReader *r, int32 len, int32 *errCode)
{
    int32 command, commandLen, result;
    void *buf = NULL;

    AGReadEXPANSION(r, &command, &commandLen, &buf);
    result = AGCPExpansion((AGCommandProcessor *)out, errCode, command, 
                                                commandLen, buf);
    if(buf)
        free(buf);
    return result;
}

static int32 parseEXPANSION_RESOURCE(void *out,
                                     AGReader *r,
                                     int32 len,
                                     int32 *errCode)
{
    int32 type, resourceLen, result;
    void *buf = NULL;

    AGReadEXPANSION_RESOURCE(r, &type, &resourceLen, &buf);
    result = AGCPExpansionResource((AGCommandProcessor *)out,
        errCode,
        type,
        resourceLen,
        buf);
    if(buf)
        free(buf);
    return result;
}

static int32 parseEXPANSION_CHANGESERVERCONFIG(void *out,
                                               AGReader *r,
                                               int32 len,
                                               int32 *errCode)
{
    int32 result;
    AGBool disableServer;
    int32 flags;
    char *serverName;
    int16 serverPort;
    char *userName;
    int32 passwordLen;
    uint8 *password;
    AGBool connectSecurely;
    AGBool notRemovable;

    AGReadEXPANSION_CHANGESERVERCONFIG(r,
        &disableServer,
        &flags,
        &serverName,
        &serverPort,
        &userName,
        &passwordLen,
        &password,
        &connectSecurely,
        &notRemovable);
    result = AGCPExpansionChangeServerConfig((AGCommandProcessor *)out,
        errCode,
        disableServer,
        flags,
        serverName,
        serverPort,
        userName,
        passwordLen,
        password,
        connectSecurely,
        notRemovable);
    if(serverName)
        free(serverName);
    if(userName)
        free(userName);
    if(password)
        free(password);
    return result;
}

static int32 performExpansionCommand(AGCommandProcessor *out, 
                                     int32 *errCode,
                                     int32 expansionCommand, 
                                     int32 commandLength,
                                     AGReader *reader)
{
    int32 rc = AGCLIENT_CONTINUE;

    switch((AGExpansionCommand)expansionCommand) {
    case AG_EXPANSION_RESOURCE:
        rc = parseEXPANSION_RESOURCE(out, reader, commandLength, errCode);
        break;
    case AG_EXPANSION_CHANGESERVERCONFIG:
        rc = parseEXPANSION_CHANGESERVERCONFIG(out,
            reader,
            commandLength,
            errCode);
        break;
    default:
        break; // ok not to understand unknown expansion commands.
    }
    return rc;
}

ExportFunc int32 AGCPExpansion(AGCommandProcessor *out, int32 *returnErrorCode,
                                        int32 expansionCommand, 
                                        int32 commandLength,
                                        void *commandBytes)
{
    int32 rc = AGCLIENT_CONTINUE;
    AGBufferReader * er = NULL;

    if(out->commands.performExpansionFunc) {
        rc = (*out->commands.performExpansionFunc)(out->commands.out,
                                returnErrorCode,
                                expansionCommand,
                                commandLength,
                                commandBytes);
    }

    /* Format of expansion commands is MAL command within MAL command,
    meaning that commandBytes should be interpreted exactly like a normal
    MAL command, with one exception:  only expansion commands should be
    embedded in an EXPANSION command (not non-expansion MAL commands). */

    er = AGBufferReaderNew((uint8*)commandBytes);
    if (NULL != er) {

        rc = performExpansionCommand(out,
            returnErrorCode,
            expansionCommand,
            commandLength,
            (AGReader*)er);

        AGBufferReaderFree(er);

    }

    return rc;
}

ExportFunc int32 AGCPExpansionResource(AGCommandProcessor *out,
                                       int32 *returnErrorCode,
                                       int32 resourceType, 
                                       int32 resourceLen,
                                       void *resource)
{
    int32 rc = AGCLIENT_CONTINUE;

    if(out->commands.performExpansionResourceFunc) {
        rc = (*out->commands.performExpansionResourceFunc)(out->commands.out,
                                returnErrorCode,
                                resourceType,
                                resourceLen,
                                resource);
    }

    switch ((AGResourceType)resourceType) {

    case AG_EXPANSION_RESOURCE_SERVERTYPE: {

        if (NULL != resource) {
            
            /* Todo: probably want to move this outside case when we have
            more than one resource type. */
            AGBufferReader * r = NULL;
        
            /* If we got a SERVERTYPE command, replace the current
            serverType no matter what (even if server sent empty
            string) */
            if (NULL != out->serverConfig->serverType) {
                free(out->serverConfig->serverType);
                out->serverConfig->serverType = NULL;
            }

            r = AGBufferReaderNew((uint8*)resource);
            if (NULL != r) {
                out->serverConfig->serverType = AGReadCString((AGReader*)r);
                AGBufferReaderFree(r);
            }

        }

        break;
    }
    default:
        break; // ok not to understand unknown resources.

    }

    return rc;
}

int32 AGCPExpansionChangeServerConfig(AGCommandProcessor *out,
                                      int32 *returnErrorCode,
                                      AGBool disableServer,
                                      int32 flags,
                                      char *serverName,
                                      int16 serverPort,
                                      char *userName,
                                      int32 passwordLen,
                                      uint8 *password,
                                      AGBool connectSecurely,
                                      AGBool notRemovable)
{
    int32 rc = AGCLIENT_CONTINUE;

    if(out->commands.performExpansionChSCFunc) {
        rc = (*out->commands.performExpansionChSCFunc)(out->commands.out,
                                returnErrorCode,
                                disableServer,
                                flags,
                                serverName,
                                serverPort,
                                userName,
                                passwordLen,
                                password,
                                connectSecurely,
                                notRemovable);
    }

    if (disableServer)
        out->serverConfig->disabled = TRUE;

    if (flags & AG_CHSC_SERVERNAME) {
        if (NULL != out->serverConfig->serverName)
            free(out->serverConfig->serverName);
        out->serverConfig->serverName = (NULL != serverName)
            ? strdup(serverName) : NULL;
    }

    if (flags & AG_CHSC_SERVERPORT)
        out->serverConfig->serverPort = serverPort;

    if (flags & AG_CHSC_USERNAME) {
        if (NULL != out->serverConfig->userName)
            free(out->serverConfig->userName);
        out->serverConfig->userName = (NULL != userName)
            ? strdup(userName) : NULL;
    }

    if (flags & AG_CHSC_PASSWORD) {
        if (AG_HASH_PASSWORD_YES == out->serverConfig->hashPassword) {
            bzero((uint8*)out->serverConfig->password, 16);
            /*
            In this case passwordLen should *always* be 16, but
            for completeness we'll try not to choke if it's something else.
            */
            bcopy(password,
                out->serverConfig->password,
                min(16, passwordLen));

        } else {
            if (NULL != out->serverConfig->cleartextPassword)
                free(out->serverConfig->cleartextPassword);
            out->serverConfig->cleartextPassword = (NULL != password)
                ? strdup((char *) password) : NULL;
        }
    }

    if (flags & AG_CHSC_CONNECTSECURELY)
        out->serverConfig->connectSecurely = connectSecurely;

    if (flags & AG_CHSC_NOTREMOVABLE)
        out->serverConfig->notRemovable = notRemovable;

    return rc;
}

static int32 parseEND(void *out, AGReader *r, int32 len, int32 *errCode)
{
    AGReadEND(r);
    return AGCPEnd((AGCommandProcessor *)out, errCode);
}

ExportFunc int32 AGCPEnd(AGCommandProcessor *out,  int32 *returnErrorCode)
{
    int32 rc = AGCLIENT_IDLE;

    if(out->commands.performEndFunc) {
        rc = (*out->commands.performEndFunc)(out->commands.out, returnErrorCode);
    }
    return rc;
}


ExportFunc int32 AGCPPerformCommand(AGCommandProcessor *out, 
                                            int32 *errCode,
                                            AGReader *reader)
{
    uint32 command;
    uint32 length;
    int32 rc = AGCLIENT_CONTINUE;
#ifdef DEBUG_COMMANDS
    char *name;
#endif
    command = AGReadCompactInt(reader);
    length = AGReadCompactInt(reader);

#ifdef DEBUG_COMMANDS
{
    name = AGProtocolCommandName(command);
    if(name == NULL) {
        name = "UNKNOWN";
    }
        
    printf("%s(%u, %u)\n", name, command, length);
}
#endif

    switch((AGCommand)command) {
    case AG_END_CMD:            
        rc = parseEND(out, reader, length, errCode); break;
    case AG_EXPANSION_CMD:      
        rc = parseEXPANSION(out, reader, length, errCode); break;
    case AG_DATABASECONFIG_CMD: 
        rc = parseDATABASECONFIG(out, reader, length, errCode); break;
    case AG_SERVERCONFIG_CMD:   
        rc = parseSERVERCONFIG(out, reader, length, errCode); break;
    case AG_COOKIE_CMD:         
        rc = parseCOOKIE(out, reader, length, errCode); break;
    case AG_NONCE_CMD:          
        rc = parseNONCE(out, reader, length, errCode); break;
    case AG_TASK_CMD:           
        rc = parseTASK(out, reader, length, errCode); break;
    case AG_ITEM_CMD:           
        rc = parseITEM(out, reader, length, errCode); break;
    case AG_DELETEDATABASE_CMD: 
        rc = parseDELETEDATABASE(out, reader, length, errCode); break;
    case AG_OPENDATABASE_CMD:   
        rc = parseOPENDATABASE(out, reader, length, errCode); break;
    case AG_CLOSEDATABASE_CMD:  
        rc = parseCLOSEDATABASE(out, reader, length, errCode); break;
    case AG_CLEARMODS_CMD:      
        rc = parseCLEARMODS(out, reader, length, errCode); break;
    case AG_RECORD_CMD:         
        rc = parseRECORD(out, reader, length, errCode); break;
    case AG_GOODBYE_CMD:        
        rc = parseGOODBYE(out, reader, length, errCode); break;
    case AG_SENDDEVICEINFO_CMD: 
        rc = parseSENDDEVICEINFO(out, reader, length, errCode); break;

    case AG_HELLO_CMD:      
    case AG_DEVICEINFO_CMD:      
    case AG_NEWIDS_CMD:      
    default:
        #ifdef DEBUG_COMMANDS
        printf("\tThe server should have NEVER sent a %s command.\n", name);
        #endif
        rc = AGCLIENT_ERR; 
        break;
    }
    return rc;
}