1 /*-
2  * Copyright (c) 2000-2005 MAEKAWA Masahide <maekawa@cvsync.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the author nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #include <sys/types.h>
31 #include <sys/stat.h>
32 
33 #include <limits.h>
34 
35 #include "compat_stdbool.h"
36 #include "compat_stdint.h"
37 #include "compat_inttypes.h"
38 #include "compat_limits.h"
39 #include "basedef.h"
40 
41 #include "attribute.h"
42 #include "cvsync.h"
43 #include "cvsync_attr.h"
44 
45 size_t
attr_rcs_encode_dir(uint8_t * buffer,size_t bufsize,uint16_t mode)46 attr_rcs_encode_dir(uint8_t *buffer, size_t bufsize, uint16_t mode)
47 {
48 	if (bufsize < RCS_ATTRLEN_DIR)
49 		return (0);
50 
51 	SetWord(buffer, mode);
52 
53 	return (RCS_ATTRLEN_DIR);
54 }
55 
56 size_t
attr_rcs_encode_file(uint8_t * buffer,size_t bufsize,time_t mtime,off_t size,uint16_t mode)57 attr_rcs_encode_file(uint8_t *buffer, size_t bufsize, time_t mtime, off_t size,
58 		 uint16_t mode)
59 {
60 	if (bufsize < RCS_ATTRLEN_FILE)
61 		return (0);
62 
63 	SetDDWord(buffer, mtime);
64 	SetDDWord(&buffer[8], size);
65 	SetWord(&buffer[16], mode);
66 
67 	return (RCS_ATTRLEN_FILE);
68 }
69 
70 size_t
attr_rcs_encode_rcs(uint8_t * buffer,size_t bufsize,time_t mtime,uint16_t mode)71 attr_rcs_encode_rcs(uint8_t *buffer, size_t bufsize, time_t mtime,
72 		    uint16_t mode)
73 {
74 	if (bufsize < RCS_ATTRLEN_RCS)
75 		return (0);
76 
77 	SetDDWord(buffer, mtime);
78 	SetWord(&buffer[8], mode);
79 
80 	return (RCS_ATTRLEN_RCS);
81 }
82 
83 bool
attr_rcs_decode_dir(uint8_t * buffer,size_t bufsize,struct cvsync_attr * cap)84 attr_rcs_decode_dir(uint8_t *buffer, size_t bufsize, struct cvsync_attr *cap)
85 {
86 	if (bufsize < RCS_ATTRLEN_DIR)
87 		return (false);
88 
89 	cap->ca_mode = GetWord(buffer);
90 
91 	return (true);
92 }
93 
94 bool
attr_rcs_decode_file(uint8_t * buffer,size_t bufsize,struct cvsync_attr * cap)95 attr_rcs_decode_file(uint8_t *buffer, size_t bufsize, struct cvsync_attr *cap)
96 {
97 	if (bufsize < RCS_ATTRLEN_FILE)
98 		return (false);
99 
100 	cap->ca_mtime = (int64_t)GetDDWord(buffer);
101 	cap->ca_size = GetDDWord(&buffer[8]);
102 	cap->ca_mode = GetWord(&buffer[16]);
103 
104 	return (true);
105 }
106 
107 bool
attr_rcs_decode_rcs(uint8_t * buffer,size_t bufsize,struct cvsync_attr * cap)108 attr_rcs_decode_rcs(uint8_t *buffer, size_t bufsize, struct cvsync_attr *cap)
109 {
110 	if (bufsize < RCS_ATTRLEN_RCS)
111 		return (false);
112 
113 	cap->ca_mtime = (int64_t)GetDDWord(buffer);
114 	cap->ca_mode = GetWord(&buffer[8]);
115 
116 	return (true);
117 }
118