@@ -2,6 +2,7 @@ const std = @import("std");
22const  Allocator  =  std .mem .Allocator ;
33const  Multicodec  =  @import ("multicodec.zig" ).Multicodec ;
44const  Multihash  =  @import ("multihash.zig" ).Multihash ;
5+ const  varint  =  @import ("unsigned_varint.zig" );
56
67pub  const  Error  =  error {
78    UnknownCodec ,
@@ -84,115 +85,40 @@ pub fn Cid(comptime S: usize) type {
8485            }
8586        }
8687
87-         // pub fn fromBytes(allocator: Allocator, bytes: []const u8) !@This() { 
88-         //     if (CidVersion.isV0Binary(bytes)) { 
89-         //         return @This().init(allocator, .V0, 0x70, bytes[2..]); 
90-         //     } 
91-         // 
92-         //     if (bytes.len < 2) { 
93-         //         return Error.InputTooShort; 
94-         //     } 
95-         // 
96-         //     const version = try std.meta.intToEnum(CidVersion, bytes[0]); 
97-         //     const codec = bytes[1]; 
98-         // 
99-         //     return @This().init(allocator, version, codec, bytes[2..]); 
100-         // } 
88+         pub  fn  readBytes (allocator : Allocator , reader : anytype ) ! Cid  {
89+             const  version  =  try  varint .decode_stream (reader , u64 );
90+             const  codec  =  try  varint .decode_stream (reader , u64 );
10191
92+             // CIDv0 has the fixed `0x12 0x20` prefix 
93+             if  (version  ==  0x12  and  codec  ==  0x20 ) {
94+                 var  digest : [32 ]u8  =  undefined ;
95+                 try  reader .readNoEof (& digest );
96+                 const  mh  =  try  Multihash (32 ).wrap (version , & digest );
97+                 return  newV0 (allocator , mh );
98+             }
99+ 
100+             const  ver  =  try  CidVersion .fromInt (version );
101+             switch  (ver ) {
102+                 .V0  = >  return  Error .InvalidExplicitCidV0 ,
103+                 .V1  = >  {
104+                     const  mh  =  try  Multihash (32 ).read (reader );
105+                     return  Cid .init (allocator , ver , codec , mh .getDigest ());
106+                 },
107+             }
108+         }
109+ 
110+         fn  writeBytesV1 (self : * const  Cid , writer : anytype ) ! usize  {
111+             const  version_written = try  varint .encode_stream (writer ,u64 ,self .version .toInt ());
112+             const  codec_written = try  varint .encode_stream (writer ,u64 ,self .codec );
102113
114+             const  written : usize  =  version_written  +  codec_written ;
115+ 
116+             return  written ;
117+         }
103118
104-         // pub fn toBytes(self: @This()) ![]u8 { 
105-         //     var buf = try self.allocator.alloc(u8, 2 + self.hash.len); 
106-         // 
107-         //     switch (self.version) { 
108-         //         .V0 => { 
109-         //             buf[0] = 0x12; 
110-         //             buf[1] = 0x20; 
111-         //         }, 
112-         //         .V1 => { 
113-         //             buf[0] = @as(u8, @intCast(@intFromEnum(self.version))); 
114-         //             buf[1] = @as(u8, @intCast(self.codec)); 
115-         //         }, 
116-         //     } 
117-         // 
118-         //     @memcpy(buf[2..], &self.hash); 
119-         //     return buf; 
120-         // } 
121119    };
122120}
123- // pub const Codec = enum(u64) { 
124- //     Raw = 0x55, 
125- //     DagPb = 0x70, 
126- //     DagCbor = 0x71, 
127- // 
128- //     pub fn fromInt(value: u64) Error!Codec { 
129- //         return switch (value) { 
130- //             0x55 => .Raw, 
131- //             0x70 => .DagPb, 
132- //             0x71 => .DagCbor, 
133- //             else => Error.UnknownCodec, 
134- //         }; 
135- //     } 
136- // }; 
137- // 
138- // pub const Cid = struct { 
139- //     version: CidVersion, 
140- //     codec: Codec, 
141- //     hash: []const u8, 
142- //     allocator: Allocator, 
143- // 
144- //     pub fn init(allocator: Allocator, version: CidVersion, codec: Codec, hash: []const u8) !Cid { 
145- //         if (version == .V0 and codec != .DagPb) { 
146- //             return Error.InvalidCidV0Codec; 
147- //         } 
148- // 
149- //         const hash_copy = try allocator.dupe(u8, hash); 
150- //         return Cid{ 
151- //             .version = version, 
152- //             .codec = codec, 
153- //             .hash = hash_copy, 
154- //             .allocator = allocator, 
155- //         }; 
156- //     } 
157- // 
158- //     pub fn fromBytes(allocator: Allocator, bytes: []const u8) !Cid { 
159- //         if (CidVersion.isV0Binary(bytes)) { 
160- //             return Cid.init(allocator, .V0, .DagPb, bytes[2..]); 
161- //         } 
162- // 
163- //         if (bytes.len < 2) { 
164- //             return Error.InputTooShort; 
165- //         } 
166- // 
167- //         const version = try std.meta.intToEnum(CidVersion, bytes[0]); 
168- //         const codec = try Codec.fromInt(bytes[1]); 
169- // 
170- //         return Cid.init(allocator, version, codec, bytes[2..]); 
171- //     } 
172- // 
173- //     pub fn deinit(self: *Cid) void { 
174- //         self.allocator.free(self.hash); 
175- //     } 
176- // 
177- //     pub fn toBytes(self: Cid) ![]u8 { 
178- //         var buf = try self.allocator.alloc(u8, 2 + self.hash.len); 
179- // 
180- //         switch (self.version) { 
181- //             .V0 => { 
182- //                 buf[0] = 0x12; 
183- //                 buf[1] = 0x20; 
184- //             }, 
185- //             .V1 => { 
186- //                 buf[0] = @as(u8, @intCast(@intFromEnum(self.version))); 
187- //                 buf[1] = @as(u8, @intCast(@intFromEnum(self.codec))); 
188- //             }, 
189- //         } 
190- // 
191- //         std.mem.copyForwards(u8, buf[2..], self.hash); 
192- //         return buf; 
193- //     } 
194- // }; 
195- // 
121+ 
196122// test "CID basic operations" { 
197123//     const testing = std.testing; 
198124//     const allocator = testing.allocator; 
0 commit comments